Skip to content

Commit a62332e

Browse files
chrismason-xxDavid Woodhouse
authored andcommitted
Add a readonly flag open_ctree to force RO opens
1 parent aaf38b3 commit a62332e

File tree

9 files changed

+38
-21
lines changed

9 files changed

+38
-21
lines changed

btrfsck.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ int main(int ac, char **av) {
746746
cache_tree_init(&nodes);
747747
cache_tree_init(&reada);
748748

749-
root = open_ctree(av[1], 0);
749+
root = open_ctree(av[1], 0, 0);
750750

751751
bits_nr = 1024;
752752
bits = malloc(bits_nr * sizeof(struct block_info));

convert.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2189,7 +2189,7 @@ int do_convert(const char *devname, int datacsum, int packing, int noxattr)
21892189
fprintf(stderr, "unable to update system chunk\n");
21902190
goto fail;
21912191
}
2192-
root = open_ctree_fd(fd, devname, super_bytenr);
2192+
root = open_ctree_fd(fd, devname, super_bytenr, O_RDWR);
21932193
if (!root) {
21942194
fprintf(stderr, "unable to open ctree\n");
21952195
goto fail;
@@ -2251,7 +2251,7 @@ int do_convert(const char *devname, int datacsum, int packing, int noxattr)
22512251
goto fail;
22522252
}
22532253

2254-
root = open_ctree_fd(fd, devname, 0);
2254+
root = open_ctree_fd(fd, devname, 0, O_RDWR);
22552255
if (!root) {
22562256
fprintf(stderr, "unable to open ctree\n");
22572257
goto fail;
@@ -2349,7 +2349,7 @@ int do_rollback(const char *devname, int force)
23492349
fprintf(stderr, "unable to open %s\n", devname);
23502350
goto fail;
23512351
}
2352-
root = open_ctree_fd(fd, devname, 0);
2352+
root = open_ctree_fd(fd, devname, 0, O_RDWR);
23532353
if (!root) {
23542354
fprintf(stderr, "unable to open ctree\n");
23552355
goto fail;

ctree.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,7 @@ struct btrfs_fs_info {
515515
struct btrfs_fs_devices *fs_devices;
516516
struct list_head space_info;
517517
int system_allocs;
518+
int readonly;
518519
};
519520

520521
/*

debug-tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ int main(int ac, char **av)
130130
if (ac != 1)
131131
print_usage();
132132

133-
root = open_ctree(av[optind], 0);
133+
root = open_ctree(av[optind], 0, 0);
134134
if (!root) {
135135
fprintf(stderr, "unable to open %s\n", av[optind]);
136136
exit(1);

dir-test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ int main(int ac, char **av)
435435
struct btrfs_trans_handle *trans;
436436
radix_tree_init();
437437

438-
root = open_ctree(av[ac-1], &super);
438+
root = open_ctree(av[ac-1], &super, 0);
439439
trans = btrfs_start_transaction(root, 1);
440440

441441
dir_oid = btrfs_super_root_dir(&super);
@@ -478,7 +478,7 @@ int main(int ac, char **av)
478478
btrfs_header_level(&root->node->node.header),
479479
btrfs_header_nritems(&root->node->node.header));
480480
close_ctree(root, &super);
481-
root = open_ctree("dbfile", &super);
481+
root = open_ctree("dbfile", &super, 0);
482482
}
483483
while(count--) {
484484
ret = ops[op](trans, root, &radix);

disk-io.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -454,22 +454,27 @@ struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
454454
return root;
455455
}
456456

457-
struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr)
457+
struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr, int writes)
458458
{
459459
int fp;
460460
struct btrfs_root *root;
461+
int flags = O_CREAT | O_RDWR;
461462

462-
fp = open(filename, O_CREAT | O_RDWR, 0600);
463+
if (!writes)
464+
flags = O_RDONLY;
465+
466+
fp = open(filename, flags, 0600);
463467
if (fp < 0) {
464468
return NULL;
465469
}
466-
root = open_ctree_fd(fp, filename, sb_bytenr);
470+
root = open_ctree_fd(fp, filename, sb_bytenr, writes);
467471
close(fp);
468472

469473
return root;
470474
}
471475

472-
struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr)
476+
struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
477+
int writes)
473478
{
474479
u32 sectorsize;
475480
u32 nodesize;
@@ -510,6 +515,9 @@ struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr)
510515
fs_info->chunk_root = chunk_root;
511516
fs_info->dev_root = dev_root;
512517

518+
if (!writes)
519+
fs_info->readonly = 1;
520+
513521
extent_io_tree_init(&fs_info->extent_cache);
514522
extent_io_tree_init(&fs_info->free_space_cache);
515523
extent_io_tree_init(&fs_info->block_group_cache);
@@ -527,7 +535,10 @@ struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr)
527535
__setup_root(4096, 4096, 4096, 4096, tree_root,
528536
fs_info, BTRFS_ROOT_TREE_OBJECTID);
529537

530-
ret = btrfs_open_devices(fs_devices, O_RDWR);
538+
if (writes)
539+
ret = btrfs_open_devices(fs_devices, O_RDWR);
540+
else
541+
ret = btrfs_open_devices(fs_devices, O_RDONLY);
531542
BUG_ON(ret);
532543

533544
ret = btrfs_bootstrap_super_map(&fs_info->mapping_tree, fs_devices);
@@ -656,6 +667,10 @@ int write_ctree_super(struct btrfs_trans_handle *trans,
656667
int ret;
657668
struct btrfs_root *tree_root = root->fs_info->tree_root;
658669
struct btrfs_root *chunk_root = root->fs_info->chunk_root;
670+
671+
if (root->fs_info->readonly)
672+
return 0;
673+
659674
btrfs_set_super_generation(&root->fs_info->super_copy,
660675
trans->transid);
661676
btrfs_set_super_root(&root->fs_info->super_copy,

disk-io.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
3131
u64 bytenr, u32 blocksize);
3232
int clean_tree_block(struct btrfs_trans_handle *trans,
3333
struct btrfs_root *root, struct extent_buffer *buf);
34-
struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr);
35-
struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr);
34+
struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr, int writes);
35+
struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
36+
int writes);
3637
int close_ctree(struct btrfs_root *root);
3738
int write_ctree_super(struct btrfs_trans_handle *trans,
3839
struct btrfs_root *root);

mkfs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ static int make_root_dir(int fd, const char *device_name) {
7777
u64 chunk_size = 0;
7878
int ret;
7979

80-
root = open_ctree_fd(fd, device_name, 0);
80+
root = open_ctree_fd(fd, device_name, 0, O_RDWR);
8181

8282
if (!root) {
8383
fprintf(stderr, "ctree init failed\n");
@@ -408,7 +408,7 @@ int main(int ac, char **av)
408408
fprintf(stderr, "failed to setup the root directory\n");
409409
exit(1);
410410
}
411-
root = open_ctree(file, 0);
411+
root = open_ctree(file, 0, O_RDWR);
412412
root->fs_info->alloc_start = alloc_start;
413413
trans = btrfs_start_transaction(root, 1);
414414

quick-test.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ int main(int ac, char **av) {
5050

5151
radix_tree_init();
5252

53-
root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET);
53+
root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, O_RDWR);
5454
trans = btrfs_start_transaction(root, 1);
5555
srand(55);
5656
btrfs_set_key_type(&ins, BTRFS_STRING_ITEM_KEY);
@@ -73,7 +73,7 @@ int main(int ac, char **av) {
7373
btrfs_commit_transaction(trans, root);
7474
close_ctree(root);
7575
exit(1);
76-
root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET);
76+
root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, O_RDWR);
7777
printf("starting search\n");
7878
srand(55);
7979
for (i = 0; i < run_size; i++) {
@@ -92,7 +92,7 @@ int main(int ac, char **av) {
9292
}
9393
close_ctree(root);
9494

95-
root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET);
95+
root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, O_RDWR);
9696
printf("node %p level %d total ptrs %d free spc %lu\n", root->node,
9797
btrfs_header_level(root->node),
9898
btrfs_header_nritems(root->node),
@@ -120,7 +120,7 @@ int main(int ac, char **av) {
120120
btrfs_commit_transaction(trans, root);
121121
close_ctree(root);
122122

123-
root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET);
123+
root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, O_RDWR);
124124
trans = btrfs_start_transaction(root, 1);
125125
srand(128);
126126
for (i = 0; i < run_size; i++) {
@@ -136,7 +136,7 @@ int main(int ac, char **av) {
136136
btrfs_commit_transaction(trans, root);
137137
close_ctree(root);
138138

139-
root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET);
139+
root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, O_RDWR);
140140
srand(128);
141141
printf("starting search2\n");
142142
for (i = 0; i < run_size; i++) {

0 commit comments

Comments
 (0)