aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Mason <chris.mason@oracle.com>2008-11-17 21:14:24 -0500
committerChris Mason <chris.mason@oracle.com>2008-11-17 21:14:24 -0500
commitea9e8b11bd1252dcbc23afefcf1a52ec6aa3c113 (patch)
tree3573c8d79e1936e8c921a3be77abda35124e0d78
parent0660b5af3f7ac0fac69de975914e1f4a3a586fb3 (diff)
Btrfs: prevent loops in the directory tree when creating snapshots
For a directory tree: /mnt/subvolA/subvolB btrfsctl -s /mnt/subvolA/subvolB /mnt Will create a directory loop with subvolA under subvolB. This commit uses the forward refs for each subvol and snapshot to error out before creating the loop. Signed-off-by: Chris Mason <chris.mason@oracle.com>
-rw-r--r--fs/btrfs/ctree.h3
-rw-r--r--fs/btrfs/disk-io.c5
-rw-r--r--fs/btrfs/ioctl.c50
-rw-r--r--fs/btrfs/root-tree.c16
4 files changed, 73 insertions, 1 deletions
diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index ad2cbe63503c..70b3dbb4de12 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -1801,6 +1801,9 @@ int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
1801 struct extent_buffer *node, 1801 struct extent_buffer *node,
1802 struct extent_buffer *parent); 1802 struct extent_buffer *parent);
1803/* root-item.c */ 1803/* root-item.c */
1804int btrfs_find_root_ref(struct btrfs_root *tree_root,
1805 struct btrfs_path *path,
1806 u64 root_id, u64 ref_id);
1804int btrfs_add_root_ref(struct btrfs_trans_handle *trans, 1807int btrfs_add_root_ref(struct btrfs_trans_handle *trans,
1805 struct btrfs_root *tree_root, 1808 struct btrfs_root *tree_root,
1806 u64 root_id, u8 type, u64 ref_id, 1809 u64 root_id, u8 type, u64 ref_id,
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 8d7866b733d6..e18250a6fd2d 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -1129,7 +1129,7 @@ struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
1129 kfree(root); 1129 kfree(root);
1130 return ERR_PTR(ret); 1130 return ERR_PTR(ret);
1131 } 1131 }
1132 1132#if 0
1133 ret = btrfs_sysfs_add_root(root); 1133 ret = btrfs_sysfs_add_root(root);
1134 if (ret) { 1134 if (ret) {
1135 free_extent_buffer(root->node); 1135 free_extent_buffer(root->node);
@@ -1137,6 +1137,7 @@ struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
1137 kfree(root); 1137 kfree(root);
1138 return ERR_PTR(ret); 1138 return ERR_PTR(ret);
1139 } 1139 }
1140#endif
1140 root->in_sysfs = 1; 1141 root->in_sysfs = 1;
1141 return root; 1142 return root;
1142} 1143}
@@ -1963,8 +1964,10 @@ int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
1963 down_write(&root->anon_super.s_umount); 1964 down_write(&root->anon_super.s_umount);
1964 kill_anon_super(&root->anon_super); 1965 kill_anon_super(&root->anon_super);
1965 } 1966 }
1967#if 0
1966 if (root->in_sysfs) 1968 if (root->in_sysfs)
1967 btrfs_sysfs_del_root(root); 1969 btrfs_sysfs_del_root(root);
1970#endif
1968 if (root->node) 1971 if (root->node)
1969 free_extent_buffer(root->node); 1972 free_extent_buffer(root->node);
1970 if (root->commit_root) 1973 if (root->commit_root)
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index 536ae8837801..8828109fa58e 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -284,6 +284,56 @@ static noinline int btrfs_mksubvol(struct path *parent, char *name,
284 * subvolume with specific mode bits. 284 * subvolume with specific mode bits.
285 */ 285 */
286 if (snap_src) { 286 if (snap_src) {
287 struct dentry *dir = dentry->d_parent;
288 struct dentry *test = dir->d_parent;
289 struct btrfs_path *path = btrfs_alloc_path();
290 int ret;
291 u64 test_oid;
292 u64 parent_oid = BTRFS_I(dir->d_inode)->root->root_key.objectid;
293
294 test_oid = snap_src->root_key.objectid;
295
296 ret = btrfs_find_root_ref(snap_src->fs_info->tree_root,
297 path, parent_oid, test_oid);
298 if (ret == 0)
299 goto create;
300 btrfs_release_path(snap_src->fs_info->tree_root, path);
301
302 /* we need to make sure we aren't creating a directory loop
303 * by taking a snapshot of something that has our current
304 * subvol in its directory tree. So, this loops through
305 * the dentries and checks the forward refs for each subvolume
306 * to see if is references the subvolume where we are
307 * placing this new snapshot.
308 */
309 while(1) {
310 if (!test ||
311 dir == snap_src->fs_info->sb->s_root ||
312 test == snap_src->fs_info->sb->s_root ||
313 test->d_inode->i_sb != snap_src->fs_info->sb) {
314 break;
315 }
316 if (S_ISLNK(test->d_inode->i_mode)) {
317 printk("Symlink in snapshot path, failed\n");
318 error = -EMLINK;
319 btrfs_free_path(path);
320 goto out_drop_write;
321 }
322 test_oid =
323 BTRFS_I(test->d_inode)->root->root_key.objectid;
324 ret = btrfs_find_root_ref(snap_src->fs_info->tree_root,
325 path, test_oid, parent_oid);
326 if (ret == 0) {
327 printk("Snapshot creation failed, looping\n");
328 error = -EMLINK;
329 btrfs_free_path(path);
330 goto out_drop_write;
331 }
332 btrfs_release_path(snap_src->fs_info->tree_root, path);
333 test = test->d_parent;
334 }
335create:
336 btrfs_free_path(path);
287 error = create_snapshot(snap_src, dentry, name, namelen); 337 error = create_snapshot(snap_src, dentry, name, namelen);
288 } else { 338 } else {
289 error = create_subvol(BTRFS_I(parent->dentry->d_inode)->root, 339 error = create_subvol(BTRFS_I(parent->dentry->d_inode)->root,
diff --git a/fs/btrfs/root-tree.c b/fs/btrfs/root-tree.c
index e9be3abcb368..dbe20d4c6ea4 100644
--- a/fs/btrfs/root-tree.c
+++ b/fs/btrfs/root-tree.c
@@ -300,6 +300,22 @@ int btrfs_del_root_ref(struct btrfs_trans_handle *trans,
300 return ret; 300 return ret;
301} 301}
302 302
303int btrfs_find_root_ref(struct btrfs_root *tree_root,
304 struct btrfs_path *path,
305 u64 root_id, u64 ref_id)
306{
307 struct btrfs_key key;
308 int ret;
309
310 key.objectid = root_id;
311 key.type = BTRFS_ROOT_REF_KEY;
312 key.offset = ref_id;
313
314 ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
315 return ret;
316}
317
318
303/* 319/*
304 * add a btrfs_root_ref item. type is either BTRFS_ROOT_REF_KEY 320 * add a btrfs_root_ref item. type is either BTRFS_ROOT_REF_KEY
305 * or BTRFS_ROOT_BACKREF_KEY. 321 * or BTRFS_ROOT_BACKREF_KEY.