Skip to content

Commit 4df27c4

Browse files
Yan, Zhengchrismason-xx
authored andcommitted
Btrfs: change how subvolumes are organized
btrfs allows subvolumes and snapshots anywhere in the directory tree. If we snapshot a subvolume that contains a link to other subvolume called subvolA, subvolA can be accessed through both the original subvolume and the snapshot. This is similar to creating hard link to directory, and has the very similar problems. The aim of this patch is enforcing there is only one access point to each subvolume. Only the first directory entry (the one added when the subvolume/snapshot was created) is treated as valid access point. The first directory entry is distinguished by checking root forward reference. If the corresponding root forward reference is missing, we know the entry is not the first one. This patch also adds snapshot/subvolume rename support, the code allows rename subvolume link across subvolumes. Signed-off-by: Yan Zheng <zheng.yan@oracle.com> Signed-off-by: Chris Mason <chris.mason@oracle.com>
1 parent 13a8a7c commit 4df27c4

9 files changed

Lines changed: 459 additions & 168 deletions

File tree

‎fs/btrfs/btrfs_inode.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ struct btrfs_inode {
138138
* of these.
139139
*/
140140
unsigned ordered_data_close:1;
141+
unsigned dummy_inode:1;
141142

142143
struct inode vfs_inode;
143144
};

‎fs/btrfs/ctree.h‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ struct btrfs_ordered_sum;
114114
*/
115115
#define BTRFS_DEV_ITEMS_OBJECTID 1ULL
116116

117+
#define BTRFS_BTREE_INODE_OBJECTID 1
118+
119+
#define BTRFS_EMPTY_SUBVOL_DIR_OBJECTID 2
120+
117121
/*
118122
* we can actually store much bigger names, but lets not confuse the rest
119123
* of linux
@@ -792,6 +796,8 @@ struct btrfs_fs_info {
792796

793797
/* the log root tree is a directory of all the other log roots */
794798
struct btrfs_root *log_root_tree;
799+
800+
spinlock_t fs_roots_radix_lock;
795801
struct radix_tree_root fs_roots_radix;
796802

797803
/* block group cache stuff */
@@ -1011,6 +1017,8 @@ struct btrfs_root {
10111017
u64 highest_objectid;
10121018
int ref_cows;
10131019
int track_dirty;
1020+
int in_radix;
1021+
10141022
u64 defrag_trans_start;
10151023
struct btrfs_key defrag_progress;
10161024
struct btrfs_key defrag_max;
@@ -2111,12 +2119,15 @@ int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
21112119
struct extent_buffer *parent);
21122120
/* root-item.c */
21132121
int btrfs_find_root_ref(struct btrfs_root *tree_root,
2114-
struct btrfs_path *path,
2115-
u64 root_id, u64 ref_id);
2122+
struct btrfs_path *path,
2123+
u64 root_id, u64 ref_id);
21162124
int btrfs_add_root_ref(struct btrfs_trans_handle *trans,
21172125
struct btrfs_root *tree_root,
2118-
u64 root_id, u8 type, u64 ref_id,
2119-
u64 dirid, u64 sequence,
2126+
u64 root_id, u64 ref_id, u64 dirid, u64 sequence,
2127+
const char *name, int name_len);
2128+
int btrfs_del_root_ref(struct btrfs_trans_handle *trans,
2129+
struct btrfs_root *tree_root,
2130+
u64 root_id, u64 ref_id, u64 dirid, u64 *sequence,
21202131
const char *name, int name_len);
21212132
int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
21222133
struct btrfs_key *key);
@@ -2149,6 +2160,10 @@ btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
21492160
struct btrfs_path *path, u64 dir,
21502161
u64 objectid, const char *name, int name_len,
21512162
int mod);
2163+
struct btrfs_dir_item *
2164+
btrfs_search_dir_index_item(struct btrfs_root *root,
2165+
struct btrfs_path *path, u64 dirid,
2166+
const char *name, int name_len);
21522167
struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
21532168
struct btrfs_path *path,
21542169
const char *name, int name_len);
@@ -2171,6 +2186,7 @@ int btrfs_insert_orphan_item(struct btrfs_trans_handle *trans,
21712186
struct btrfs_root *root, u64 offset);
21722187
int btrfs_del_orphan_item(struct btrfs_trans_handle *trans,
21732188
struct btrfs_root *root, u64 offset);
2189+
int btrfs_find_orphan_item(struct btrfs_root *root, u64 offset);
21742190

21752191
/* inode-map.c */
21762192
int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
@@ -2243,6 +2259,10 @@ int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
22432259
int btrfs_add_link(struct btrfs_trans_handle *trans,
22442260
struct inode *parent_inode, struct inode *inode,
22452261
const char *name, int name_len, int add_backref, u64 index);
2262+
int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2263+
struct btrfs_root *root,
2264+
struct inode *dir, u64 objectid,
2265+
const char *name, int name_len);
22462266
int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
22472267
struct btrfs_root *root,
22482268
struct inode *inode, u64 new_size,

‎fs/btrfs/dir-item.c‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,53 @@ btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
281281
return btrfs_match_dir_item_name(root, path, name, name_len);
282282
}
283283

284+
struct btrfs_dir_item *
285+
btrfs_search_dir_index_item(struct btrfs_root *root,
286+
struct btrfs_path *path, u64 dirid,
287+
const char *name, int name_len)
288+
{
289+
struct extent_buffer *leaf;
290+
struct btrfs_dir_item *di;
291+
struct btrfs_key key;
292+
u32 nritems;
293+
int ret;
294+
295+
key.objectid = dirid;
296+
key.type = BTRFS_DIR_INDEX_KEY;
297+
key.offset = 0;
298+
299+
ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
300+
if (ret < 0)
301+
return ERR_PTR(ret);
302+
303+
leaf = path->nodes[0];
304+
nritems = btrfs_header_nritems(leaf);
305+
306+
while (1) {
307+
if (path->slots[0] >= nritems) {
308+
ret = btrfs_next_leaf(root, path);
309+
if (ret < 0)
310+
return ERR_PTR(ret);
311+
if (ret > 0)
312+
break;
313+
leaf = path->nodes[0];
314+
nritems = btrfs_header_nritems(leaf);
315+
continue;
316+
}
317+
318+
btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
319+
if (key.objectid != dirid || key.type != BTRFS_DIR_INDEX_KEY)
320+
break;
321+
322+
di = btrfs_match_dir_item_name(root, path, name, name_len);
323+
if (di)
324+
return di;
325+
326+
path->slots[0]++;
327+
}
328+
return NULL;
329+
}
330+
284331
struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
285332
struct btrfs_root *root,
286333
struct btrfs_path *path, u64 dir,

‎fs/btrfs/disk-io.c‎

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
static struct extent_io_ops btree_extent_io_ops;
4343
static void end_workqueue_fn(struct btrfs_work *work);
44+
static void free_fs_root(struct btrfs_root *root);
4445

4546
static atomic_t btrfs_bdi_num = ATOMIC_INIT(0);
4647

@@ -951,14 +952,16 @@ static int find_and_setup_root(struct btrfs_root *tree_root,
951952
root, fs_info, objectid);
952953
ret = btrfs_find_last_root(tree_root, objectid,
953954
&root->root_item, &root->root_key);
955+
if (ret > 0)
956+
return -ENOENT;
954957
BUG_ON(ret);
955958

956959
generation = btrfs_root_generation(&root->root_item);
957960
blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
958961
root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
959962
blocksize, generation);
960-
root->commit_root = btrfs_root_node(root);
961963
BUG_ON(!root->node);
964+
root->commit_root = btrfs_root_node(root);
962965
return 0;
963966
}
964967

@@ -1176,39 +1179,66 @@ struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
11761179
return fs_info->dev_root;
11771180
if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
11781181
return fs_info->csum_root;
1179-
1182+
again:
1183+
spin_lock(&fs_info->fs_roots_radix_lock);
11801184
root = radix_tree_lookup(&fs_info->fs_roots_radix,
11811185
(unsigned long)location->objectid);
1186+
spin_unlock(&fs_info->fs_roots_radix_lock);
11821187
if (root)
11831188
return root;
11841189

1190+
ret = btrfs_find_orphan_item(fs_info->tree_root, location->objectid);
1191+
if (ret == 0)
1192+
ret = -ENOENT;
1193+
if (ret < 0)
1194+
return ERR_PTR(ret);
1195+
11851196
root = btrfs_read_fs_root_no_radix(fs_info->tree_root, location);
11861197
if (IS_ERR(root))
11871198
return root;
11881199

1200+
WARN_ON(btrfs_root_refs(&root->root_item) == 0);
11891201
set_anon_super(&root->anon_super, NULL);
11901202

1203+
ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
1204+
if (ret)
1205+
goto fail;
1206+
1207+
spin_lock(&fs_info->fs_roots_radix_lock);
11911208
ret = radix_tree_insert(&fs_info->fs_roots_radix,
11921209
(unsigned long)root->root_key.objectid,
11931210
root);
1211+
if (ret == 0)
1212+
root->in_radix = 1;
1213+
spin_unlock(&fs_info->fs_roots_radix_lock);
1214+
radix_tree_preload_end();
11941215
if (ret) {
1195-
free_extent_buffer(root->node);
1196-
kfree(root);
1197-
return ERR_PTR(ret);
1216+
if (ret == -EEXIST) {
1217+
free_fs_root(root);
1218+
goto again;
1219+
}
1220+
goto fail;
11981221
}
1199-
if (!(fs_info->sb->s_flags & MS_RDONLY)) {
1200-
ret = btrfs_find_dead_roots(fs_info->tree_root,
1201-
root->root_key.objectid);
1202-
BUG_ON(ret);
1222+
1223+
ret = btrfs_find_dead_roots(fs_info->tree_root,
1224+
root->root_key.objectid);
1225+
WARN_ON(ret);
1226+
1227+
if (!(fs_info->sb->s_flags & MS_RDONLY))
12031228
btrfs_orphan_cleanup(root);
1204-
}
1229+
12051230
return root;
1231+
fail:
1232+
free_fs_root(root);
1233+
return ERR_PTR(ret);
12061234
}
12071235

12081236
struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
12091237
struct btrfs_key *location,
12101238
const char *name, int namelen)
12111239
{
1240+
return btrfs_read_fs_root_no_name(fs_info, location);
1241+
#if 0
12121242
struct btrfs_root *root;
12131243
int ret;
12141244

@@ -1225,17 +1255,17 @@ struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
12251255
kfree(root);
12261256
return ERR_PTR(ret);
12271257
}
1228-
#if 0
1258+
12291259
ret = btrfs_sysfs_add_root(root);
12301260
if (ret) {
12311261
free_extent_buffer(root->node);
12321262
kfree(root->name);
12331263
kfree(root);
12341264
return ERR_PTR(ret);
12351265
}
1236-
#endif
12371266
root->in_sysfs = 1;
12381267
return root;
1268+
#endif
12391269
}
12401270

12411271
static int btrfs_congested_fn(void *congested_data, int bdi_bits)
@@ -2229,20 +2259,25 @@ int write_ctree_super(struct btrfs_trans_handle *trans,
22292259

22302260
int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
22312261
{
2232-
WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree));
2262+
spin_lock(&fs_info->fs_roots_radix_lock);
22332263
radix_tree_delete(&fs_info->fs_roots_radix,
22342264
(unsigned long)root->root_key.objectid);
2265+
spin_unlock(&fs_info->fs_roots_radix_lock);
2266+
free_fs_root(root);
2267+
return 0;
2268+
}
2269+
2270+
static void free_fs_root(struct btrfs_root *root)
2271+
{
2272+
WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree));
22352273
if (root->anon_super.s_dev) {
22362274
down_write(&root->anon_super.s_umount);
22372275
kill_anon_super(&root->anon_super);
22382276
}
2239-
if (root->node)
2240-
free_extent_buffer(root->node);
2241-
if (root->commit_root)
2242-
free_extent_buffer(root->commit_root);
2277+
free_extent_buffer(root->node);
2278+
free_extent_buffer(root->commit_root);
22432279
kfree(root->name);
22442280
kfree(root);
2245-
return 0;
22462281
}
22472282

22482283
static int del_fs_roots(struct btrfs_fs_info *fs_info)

0 commit comments

Comments
 (0)