Skip to content

Commit ec8a6b1

Browse files
maharmstonekdave
authored andcommitted
btrfs-progs: mkfs: add ro flag to --subvol
Adds a flag to mkfs.btrfs --subvol to allow subvolumes to be created readonly. Signed-off-by: Mark Harmstone <[email protected]>
1 parent fa70df7 commit ec8a6b1

File tree

7 files changed

+21
-6
lines changed

7 files changed

+21
-6
lines changed

Documentation/mkfs.btrfs.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ OPTIONS
163163
*flags* is an optional comma-separated list of modifiers. Valid choices are:
164164

165165
* *default*: create as default subvolume (this can only be specified once)
166+
* *ro*: create as readonly subvolume
166167

167168
--shrink
168169
Shrink the filesystem to its minimal size, only works with *--rootdir* option.

common/root-tree-utils.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ int btrfs_make_root_dir(struct btrfs_trans_handle *trans,
6666
* The created tree root would have its root_ref as 1.
6767
* Thus for subvolumes caller needs to properly add ROOT_BACKREF items.
6868
*/
69-
int btrfs_make_subvolume(struct btrfs_trans_handle *trans, u64 objectid)
69+
int btrfs_make_subvolume(struct btrfs_trans_handle *trans, u64 objectid,
70+
bool readonly)
7071
{
7172
struct btrfs_fs_info *fs_info = trans->fs_info;
7273
struct btrfs_root *root;
@@ -98,6 +99,13 @@ int btrfs_make_subvolume(struct btrfs_trans_handle *trans, u64 objectid)
9899
ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
99100
if (ret < 0)
100101
goto error;
102+
103+
btrfs_set_stack_inode_flags(&root->root_item.inode,
104+
BTRFS_INODE_ROOT_ITEM_INIT);
105+
106+
if (readonly)
107+
btrfs_set_root_flags(&root->root_item, BTRFS_ROOT_SUBVOL_RDONLY);
108+
101109
ret = btrfs_update_root(trans, fs_info->tree_root, &root->root_key,
102110
&root->root_item);
103111
if (ret < 0)

common/root-tree-utils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121

2222
int btrfs_make_root_dir(struct btrfs_trans_handle *trans,
2323
struct btrfs_root *root, u64 objectid);
24-
int btrfs_make_subvolume(struct btrfs_trans_handle *trans, u64 objectid);
24+
int btrfs_make_subvolume(struct btrfs_trans_handle *trans, u64 objectid,
25+
bool readonly);
2526
int btrfs_link_subvolume(struct btrfs_trans_handle *trans,
2627
struct btrfs_root *parent_root,
2728
u64 parent_dir, const char *name,

convert/main.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,13 +1022,14 @@ static int init_btrfs(struct btrfs_mkfs_config *cfg, struct btrfs_root *root,
10221022
BTRFS_FIRST_FREE_OBJECTID);
10231023

10241024
/* subvol for fs image file */
1025-
ret = btrfs_make_subvolume(trans, CONV_IMAGE_SUBVOL_OBJECTID);
1025+
ret = btrfs_make_subvolume(trans, CONV_IMAGE_SUBVOL_OBJECTID, false);
10261026
if (ret < 0) {
10271027
error("failed to create subvolume image root: %d", ret);
10281028
goto err;
10291029
}
10301030
/* subvol for data relocation tree */
1031-
ret = btrfs_make_subvolume(trans, BTRFS_DATA_RELOC_TREE_OBJECTID);
1031+
ret = btrfs_make_subvolume(trans, BTRFS_DATA_RELOC_TREE_OBJECTID,
1032+
false);
10321033
if (ret < 0) {
10331034
error("failed to create DATA_RELOC root: %d", ret);
10341035
goto err;

mkfs/main.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,8 @@ static int parse_subvol_flags(struct rootdir_subvol *subvol, const char *flags)
10361036

10371037
if (!strcmp(buf, "default")) {
10381038
subvol->is_default = true;
1039+
} else if (!strcmp(buf, "ro")) {
1040+
subvol->readonly = true;
10391041
} else if (buf[0] != 0) {
10401042
error("unrecognized subvol flag \"%s\"", buf);
10411043
ret = 1;
@@ -1988,7 +1990,8 @@ int BOX_MAIN(mkfs)(int argc, char **argv)
19881990
goto out;
19891991
}
19901992

1991-
ret = btrfs_make_subvolume(trans, BTRFS_DATA_RELOC_TREE_OBJECTID);
1993+
ret = btrfs_make_subvolume(trans, BTRFS_DATA_RELOC_TREE_OBJECTID,
1994+
false);
19921995
if (ret) {
19931996
error("unable to create data reloc tree: %d", ret);
19941997
goto out;

mkfs/rootdir.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ static int ftw_add_subvol(const char *full_path, const struct stat *st,
430430

431431
subvol_id = next_subvol_id++;
432432

433-
ret = btrfs_make_subvolume(g_trans, subvol_id);
433+
ret = btrfs_make_subvolume(g_trans, subvol_id, subvol->readonly);
434434
if (ret < 0) {
435435
errno = -ret;
436436
error("failed to create subvolume: %m");

mkfs/rootdir.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ struct rootdir_subvol {
3333
char *dir;
3434
char *full_path;
3535
bool is_default;
36+
bool readonly;
3637
};
3738

3839
int btrfs_mkfs_fill_dir(struct btrfs_trans_handle *trans, const char *source_dir,

0 commit comments

Comments
 (0)