aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs
diff options
context:
space:
mode:
authorChris Mason <chris.mason@oracle.com>2007-12-12 14:38:19 -0500
committerChris Mason <chris.mason@oracle.com>2008-09-25 11:03:58 -0400
commit3954401fa6013bb2f2c8758b903e9bffcf25b64b (patch)
treecc9e732db0466d35c7b7b42caef6e6bc994f940c /fs/btrfs
parentd8d5f3e16d1ae4fe9b93312e083f2c04a95520f0 (diff)
Btrfs: Add back pointers from the inode to the directory that references it
Signed-off-by: Chris Mason <chris.mason@oracle.com>
Diffstat (limited to 'fs/btrfs')
-rw-r--r--fs/btrfs/ctree.h21
-rw-r--r--fs/btrfs/inode-item.c136
-rw-r--r--fs/btrfs/inode.c258
3 files changed, 272 insertions, 143 deletions
diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index cb1b156d954e..035fa1550343 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -53,7 +53,7 @@ extern struct kmem_cache *btrfs_path_cachep;
53#define BTRFS_CSUM_SIZE 32 53#define BTRFS_CSUM_SIZE 32
54/* four bytes for CRC32 */ 54/* four bytes for CRC32 */
55#define BTRFS_CRC32_SIZE 4 55#define BTRFS_CRC32_SIZE 4
56#define BTRFS_EMPTY_DIR_SIZE 6 56#define BTRFS_EMPTY_DIR_SIZE 0
57 57
58#define BTRFS_FT_UNKNOWN 0 58#define BTRFS_FT_UNKNOWN 0
59#define BTRFS_FT_REG_FILE 1 59#define BTRFS_FT_REG_FILE 1
@@ -206,6 +206,11 @@ struct btrfs_extent_ref {
206 __le64 offset; 206 __le64 offset;
207} __attribute__ ((__packed__)); 207} __attribute__ ((__packed__));
208 208
209struct btrfs_inode_ref {
210 __le16 name_len;
211 /* name goes here */
212} __attribute__ ((__packed__));
213
209struct btrfs_inode_timespec { 214struct btrfs_inode_timespec {
210 __le64 sec; 215 __le64 sec;
211 __le32 nsec; 216 __le32 nsec;
@@ -379,7 +384,8 @@ struct btrfs_root {
379 * the FS 384 * the FS
380 */ 385 */
381#define BTRFS_INODE_ITEM_KEY 1 386#define BTRFS_INODE_ITEM_KEY 1
382#define BTRFS_XATTR_ITEM_KEY 2 387#define BTRFS_INODE_REF_KEY 2
388#define BTRFS_XATTR_ITEM_KEY 8
383/* reserve 2-15 close to the inode for later flexibility */ 389/* reserve 2-15 close to the inode for later flexibility */
384 390
385/* 391/*
@@ -486,6 +492,9 @@ BTRFS_SETGET_STACK_FUNCS(block_group_used, struct btrfs_block_group_item,
486BTRFS_SETGET_FUNCS(disk_block_group_used, struct btrfs_block_group_item, 492BTRFS_SETGET_FUNCS(disk_block_group_used, struct btrfs_block_group_item,
487 used, 64); 493 used, 64);
488 494
495/* struct btrfs_inode_ref */
496BTRFS_SETGET_FUNCS(inode_ref_name_len, struct btrfs_inode_ref, name_len, 16);
497
489/* struct btrfs_inode_item */ 498/* struct btrfs_inode_item */
490BTRFS_SETGET_FUNCS(inode_generation, struct btrfs_inode_item, generation, 64); 499BTRFS_SETGET_FUNCS(inode_generation, struct btrfs_inode_item, generation, 64);
491BTRFS_SETGET_FUNCS(inode_size, struct btrfs_inode_item, size, 64); 500BTRFS_SETGET_FUNCS(inode_size, struct btrfs_inode_item, size, 64);
@@ -1043,6 +1052,14 @@ int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
1043int btrfs_find_highest_inode(struct btrfs_root *fs_root, u64 *objectid); 1052int btrfs_find_highest_inode(struct btrfs_root *fs_root, u64 *objectid);
1044 1053
1045/* inode-item.c */ 1054/* inode-item.c */
1055int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,
1056 struct btrfs_root *root,
1057 const char *name, int name_len,
1058 u64 inode_objectid, u64 ref_objectid);
1059int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
1060 struct btrfs_root *root,
1061 const char *name, int name_len,
1062 u64 inode_objectid, u64 ref_objectid);
1046int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans, 1063int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans,
1047 struct btrfs_root *root, 1064 struct btrfs_root *root,
1048 struct btrfs_path *path, u64 objectid); 1065 struct btrfs_path *path, u64 objectid);
diff --git a/fs/btrfs/inode-item.c b/fs/btrfs/inode-item.c
index 35d2608f8918..cba30b6cc6fe 100644
--- a/fs/btrfs/inode-item.c
+++ b/fs/btrfs/inode-item.c
@@ -20,6 +20,142 @@
20#include "disk-io.h" 20#include "disk-io.h"
21#include "transaction.h" 21#include "transaction.h"
22 22
23int find_name_in_backref(struct btrfs_path *path, const char * name,
24 int name_len, struct btrfs_inode_ref **ref_ret)
25{
26 struct extent_buffer *leaf;
27 struct btrfs_inode_ref *ref;
28 unsigned long ptr;
29 unsigned long name_ptr;
30 u32 item_size;
31 u32 cur_offset = 0;
32 int len;
33
34 leaf = path->nodes[0];
35 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
36 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
37 while (cur_offset < item_size) {
38 ref = (struct btrfs_inode_ref *)(ptr + cur_offset);
39 len = btrfs_inode_ref_name_len(leaf, ref);
40 name_ptr = (unsigned long)(ref + 1);
41 cur_offset += len + sizeof(*ref);
42 if (len != name_len)
43 continue;
44 if (memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0) {
45 *ref_ret = ref;
46 return 1;
47 }
48 }
49 return 0;
50}
51
52int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
53 struct btrfs_root *root,
54 const char *name, int name_len,
55 u64 inode_objectid, u64 ref_objectid)
56{
57 struct btrfs_path *path;
58 struct btrfs_key key;
59 struct btrfs_inode_ref *ref;
60 struct extent_buffer *leaf;
61 unsigned long ptr;
62 unsigned long item_start;
63 u32 item_size;
64 u32 sub_item_len;
65 int ret;
66 int del_len = name_len + sizeof(*ref);
67
68 key.objectid = inode_objectid;
69 key.offset = ref_objectid;
70 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
71
72 path = btrfs_alloc_path();
73 if (!path)
74 return -ENOMEM;
75
76 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
77 if (ret > 0) {
78 ret = -ENOENT;
79 goto out;
80 } else if (ret < 0) {
81 goto out;
82 }
83 if (!find_name_in_backref(path, name, name_len, &ref)) {
84 ret = -ENOENT;
85 goto out;
86 }
87 leaf = path->nodes[0];
88 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
89 if (del_len == item_size) {
90 ret = btrfs_del_item(trans, root, path);
91 goto out;
92 }
93 ptr = (unsigned long)ref;
94 sub_item_len = name_len + sizeof(*ref);
95 item_start = btrfs_item_ptr_offset(leaf, path->slots[0]);
96 memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
97 item_size - (ptr + sub_item_len - item_start));
98 ret = btrfs_truncate_item(trans, root, path,
99 item_size - sub_item_len, 1);
100 BUG_ON(ret);
101out:
102 btrfs_free_path(path);
103 return ret;
104}
105
106int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,
107 struct btrfs_root *root,
108 const char *name, int name_len,
109 u64 inode_objectid, u64 ref_objectid)
110{
111 struct btrfs_path *path;
112 struct btrfs_key key;
113 struct btrfs_inode_ref *ref;
114 unsigned long ptr;
115 int ret;
116 int ins_len = name_len + sizeof(*ref);
117
118 key.objectid = inode_objectid;
119 key.offset = ref_objectid;
120 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
121
122 path = btrfs_alloc_path();
123 if (!path)
124 return -ENOMEM;
125
126 ret = btrfs_insert_empty_item(trans, root, path, &key,
127 ins_len);
128 if (ret == -EEXIST) {
129 u32 old_size;
130
131 if (find_name_in_backref(path, name, name_len, &ref))
132 goto out;
133
134 old_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
135 ret = btrfs_extend_item(trans, root, path, ins_len);
136 BUG_ON(ret);
137 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
138 struct btrfs_inode_ref);
139 ref = (struct btrfs_inode_ref *)((unsigned long)ref + old_size);
140 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
141 ptr = (unsigned long)(ref + 1);
142 ret = 0;
143 } else if (ret < 0) {
144 goto out;
145 } else {
146 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
147 struct btrfs_inode_ref);
148 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
149 ptr = (unsigned long)(ref + 1);
150 }
151 write_extent_buffer(path->nodes[0], name, ptr, name_len);
152 btrfs_mark_buffer_dirty(path->nodes[0]);
153
154out:
155 btrfs_free_path(path);
156 return ret;
157}
158
23int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans, 159int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans,
24 struct btrfs_root *root, 160 struct btrfs_root *root,
25 struct btrfs_path *path, u64 objectid) 161 struct btrfs_path *path, u64 objectid)
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 03fea037667e..cefe740b6c79 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -404,6 +404,17 @@ static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
404 ret = btrfs_delete_one_dir_name(trans, root, path, di); 404 ret = btrfs_delete_one_dir_name(trans, root, path, di);
405 405
406 dentry->d_inode->i_ctime = dir->i_ctime; 406 dentry->d_inode->i_ctime = dir->i_ctime;
407 if (!S_ISLNK(dentry->d_inode->i_mode)) {
408 ret = btrfs_del_inode_ref(trans, root, name, name_len,
409 dentry->d_inode->i_ino,
410 dentry->d_parent->d_inode->i_ino);
411 if (ret) {
412 printk("failed to delete reference to %.*s, "
413 "inode %lu parent %lu\n", name_len, name,
414 dentry->d_inode->i_ino,
415 dentry->d_parent->d_inode->i_ino);
416 }
417 }
407err: 418err:
408 btrfs_free_path(path); 419 btrfs_free_path(path);
409 if (!ret) { 420 if (!ret) {
@@ -445,75 +456,27 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
445 int err; 456 int err;
446 int ret; 457 int ret;
447 struct btrfs_root *root = BTRFS_I(dir)->root; 458 struct btrfs_root *root = BTRFS_I(dir)->root;
448 struct btrfs_path *path;
449 struct btrfs_key key;
450 struct btrfs_trans_handle *trans; 459 struct btrfs_trans_handle *trans;
451 struct btrfs_key found_key;
452 int found_type;
453 struct extent_buffer *leaf;
454 char *goodnames = "..";
455 unsigned long nr; 460 unsigned long nr;
456 461
457 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) 462 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
458 return -ENOTEMPTY; 463 return -ENOTEMPTY;
459 464
460 path = btrfs_alloc_path();
461 BUG_ON(!path);
462 mutex_lock(&root->fs_info->fs_mutex); 465 mutex_lock(&root->fs_info->fs_mutex);
463 trans = btrfs_start_transaction(root, 1); 466 trans = btrfs_start_transaction(root, 1);
464
465 btrfs_set_trans_block_group(trans, dir); 467 btrfs_set_trans_block_group(trans, dir);
466 key.objectid = inode->i_ino;
467 key.offset = (u64)-1;
468 key.type = (u8)-1;
469 while(1) {
470 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
471 if (ret < 0) {
472 err = ret;
473 goto out;
474 }
475 BUG_ON(ret == 0);
476 if (path->slots[0] == 0) {
477 err = -ENOENT;
478 goto out;
479 }
480 path->slots[0]--;
481 leaf = path->nodes[0];
482 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
483 found_type = btrfs_key_type(&found_key);
484 if (found_key.objectid != inode->i_ino) {
485 err = -ENOENT;
486 goto out;
487 }
488 if ((found_type != BTRFS_DIR_ITEM_KEY &&
489 found_type != BTRFS_DIR_INDEX_KEY) ||
490 (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
491 !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
492 err = -ENOTEMPTY;
493 goto out;
494 }
495 ret = btrfs_del_item(trans, root, path);
496 BUG_ON(ret);
497
498 if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
499 break;
500 btrfs_release_path(root, path);
501 }
502 ret = 0;
503 btrfs_release_path(root, path);
504 468
505 /* now the directory is empty */ 469 /* now the directory is empty */
506 err = btrfs_unlink_trans(trans, root, dir, dentry); 470 err = btrfs_unlink_trans(trans, root, dir, dentry);
507 if (!err) { 471 if (!err) {
508 inode->i_size = 0; 472 inode->i_size = 0;
509 } 473 }
510out: 474
511 btrfs_release_path(root, path);
512 btrfs_free_path(path);
513 nr = trans->blocks_used; 475 nr = trans->blocks_used;
514 ret = btrfs_end_transaction(trans, root); 476 ret = btrfs_end_transaction(trans, root);
515 mutex_unlock(&root->fs_info->fs_mutex); 477 mutex_unlock(&root->fs_info->fs_mutex);
516 btrfs_btree_balance_dirty(root, nr); 478 btrfs_btree_balance_dirty(root, nr);
479
517 if (ret && !err) 480 if (ret && !err)
518 err = ret; 481 err = ret;
519 return err; 482 return err;
@@ -887,21 +850,59 @@ static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
887 struct btrfs_root *root = BTRFS_I(dir)->root; 850 struct btrfs_root *root = BTRFS_I(dir)->root;
888 int ret = 0; 851 int ret = 0;
889 852
853 if (namelen == 1 && strcmp(name, ".") == 0) {
854 location->objectid = dir->i_ino;
855 location->type = BTRFS_INODE_ITEM_KEY;
856 location->offset = 0;
857 return 0;
858 }
890 path = btrfs_alloc_path(); 859 path = btrfs_alloc_path();
891 BUG_ON(!path); 860 BUG_ON(!path);
861
862 if (namelen == 1 && strcmp(name, "..") == 0) {
863 struct btrfs_key key;
864 struct extent_buffer *leaf;
865 u32 nritems;
866 int slot;
867
868 key.objectid = dir->i_ino;
869 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
870 key.offset = 0;
871 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
872 BUG_ON(ret == 0);
873 ret = 0;
874
875 leaf = path->nodes[0];
876 slot = path->slots[0];
877 nritems = btrfs_header_nritems(leaf);
878 if (slot >= nritems)
879 goto out_err;
880
881 btrfs_item_key_to_cpu(leaf, &key, slot);
882 if (key.objectid != dir->i_ino ||
883 key.type != BTRFS_INODE_REF_KEY) {
884 goto out_err;
885 }
886 location->objectid = key.offset;
887 location->type = BTRFS_INODE_ITEM_KEY;
888 location->offset = 0;
889 goto out;
890 }
891
892 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name, 892 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
893 namelen, 0); 893 namelen, 0);
894 if (IS_ERR(di)) 894 if (IS_ERR(di))
895 ret = PTR_ERR(di); 895 ret = PTR_ERR(di);
896 if (!di || IS_ERR(di)) { 896 if (!di || IS_ERR(di)) {
897 location->objectid = 0; 897 goto out_err;
898 goto out;
899 } 898 }
900 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); 899 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
901out: 900out:
902 btrfs_release_path(root, path);
903 btrfs_free_path(path); 901 btrfs_free_path(path);
904 return ret; 902 return ret;
903out_err:
904 location->objectid = 0;
905 goto out;
905} 906}
906 907
907/* 908/*
@@ -1053,13 +1054,50 @@ static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1053 if (root->fs_info->tree_root == root) 1054 if (root->fs_info->tree_root == root)
1054 key_type = BTRFS_DIR_ITEM_KEY; 1055 key_type = BTRFS_DIR_ITEM_KEY;
1055 1056
1057 /* special case for "." */
1058 if (filp->f_pos == 0) {
1059 over = filldir(dirent, ".", 1,
1060 1, inode->i_ino,
1061 DT_DIR);
1062 if (over)
1063 return 0;
1064 filp->f_pos = 1;
1065 }
1066
1056 mutex_lock(&root->fs_info->fs_mutex); 1067 mutex_lock(&root->fs_info->fs_mutex);
1057 key.objectid = inode->i_ino; 1068 key.objectid = inode->i_ino;
1069 path = btrfs_alloc_path();
1070 path->reada = 2;
1071
1072 /* special case for .., just use the back ref */
1073 if (filp->f_pos == 1) {
1074 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1075 key.offset = 0;
1076 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1077 BUG_ON(ret == 0);
1078 leaf = path->nodes[0];
1079 slot = path->slots[0];
1080 nritems = btrfs_header_nritems(leaf);
1081 if (slot >= nritems) {
1082 btrfs_release_path(root, path);
1083 goto read_dir_items;
1084 }
1085 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1086 btrfs_release_path(root, path);
1087 if (found_key.objectid != key.objectid ||
1088 found_key.type != BTRFS_INODE_REF_KEY)
1089 goto read_dir_items;
1090 over = filldir(dirent, "..", 2,
1091 2, found_key.offset, DT_DIR);
1092 if (over)
1093 goto nopos;
1094 filp->f_pos = 2;
1095 }
1096
1097read_dir_items:
1058 btrfs_set_key_type(&key, key_type); 1098 btrfs_set_key_type(&key, key_type);
1059 key.offset = filp->f_pos; 1099 key.offset = filp->f_pos;
1060 1100
1061 path = btrfs_alloc_path();
1062 path->reada = 2;
1063 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 1101 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1064 if (ret < 0) 1102 if (ret < 0)
1065 goto err; 1103 goto err;
@@ -1255,6 +1293,13 @@ static int btrfs_add_link(struct btrfs_trans_handle *trans,
1255 dentry->d_parent->d_inode->i_ino, 1293 dentry->d_parent->d_inode->i_ino,
1256 &key, btrfs_inode_type(inode)); 1294 &key, btrfs_inode_type(inode));
1257 if (ret == 0) { 1295 if (ret == 0) {
1296 if (!S_ISLNK(inode->i_mode)) {
1297 ret = btrfs_insert_inode_ref(trans, root,
1298 dentry->d_name.name,
1299 dentry->d_name.len,
1300 inode->i_ino,
1301 dentry->d_parent->d_inode->i_ino);
1302 }
1258 parent_inode = dentry->d_parent->d_inode; 1303 parent_inode = dentry->d_parent->d_inode;
1259 parent_inode->i_size += dentry->d_name.len * 2; 1304 parent_inode->i_size += dentry->d_name.len * 2;
1260 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; 1305 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
@@ -1430,35 +1475,6 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1430 return err; 1475 return err;
1431} 1476}
1432 1477
1433static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
1434 struct btrfs_root *root,
1435 u64 objectid, u64 dirid)
1436{
1437 int ret;
1438 char buf[2];
1439 struct btrfs_key key;
1440
1441 buf[0] = '.';
1442 buf[1] = '.';
1443
1444 key.objectid = objectid;
1445 key.offset = 0;
1446 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1447
1448 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
1449 &key, BTRFS_FT_DIR);
1450 if (ret)
1451 goto error;
1452
1453 key.objectid = dirid;
1454 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
1455 &key, BTRFS_FT_DIR);
1456 if (ret)
1457 goto error;
1458error:
1459 return ret;
1460}
1461
1462static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) 1478static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1463{ 1479{
1464 struct inode *inode; 1480 struct inode *inode;
@@ -1496,11 +1512,7 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1496 inode->i_fop = &btrfs_dir_file_operations; 1512 inode->i_fop = &btrfs_dir_file_operations;
1497 btrfs_set_trans_block_group(trans, inode); 1513 btrfs_set_trans_block_group(trans, inode);
1498 1514
1499 err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino); 1515 inode->i_size = 0;
1500 if (err)
1501 goto out_fail;
1502
1503 inode->i_size = 6;
1504 err = btrfs_update_inode(trans, root, inode); 1516 err = btrfs_update_inode(trans, root, inode);
1505 if (err) 1517 if (err)
1506 goto out_fail; 1518 goto out_fail;
@@ -1971,6 +1983,12 @@ static int create_subvol(struct btrfs_root *root, char *name, int namelen)
1971 if (ret) 1983 if (ret)
1972 goto fail; 1984 goto fail;
1973 1985
1986 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
1987 name, namelen, objectid,
1988 root->fs_info->sb->s_root->d_inode->i_ino);
1989 if (ret)
1990 goto fail;
1991
1974 ret = btrfs_commit_transaction(trans, root); 1992 ret = btrfs_commit_transaction(trans, root);
1975 if (ret) 1993 if (ret)
1976 goto fail_commit; 1994 goto fail_commit;
@@ -1989,12 +2007,10 @@ static int create_subvol(struct btrfs_root *root, char *name, int namelen)
1989 inode->i_fop = &btrfs_dir_file_operations; 2007 inode->i_fop = &btrfs_dir_file_operations;
1990 new_root->inode = inode; 2008 new_root->inode = inode;
1991 2009
1992 ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid); 2010 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
1993 if (ret) 2011 new_dirid);
1994 goto fail;
1995
1996 inode->i_nlink = 1; 2012 inode->i_nlink = 1;
1997 inode->i_size = 6; 2013 inode->i_size = 0;
1998 ret = btrfs_update_inode(trans, new_root, inode); 2014 ret = btrfs_update_inode(trans, new_root, inode);
1999 if (ret) 2015 if (ret)
2000 goto fail; 2016 goto fail;
@@ -2068,6 +2084,13 @@ static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2068 if (ret) 2084 if (ret)
2069 goto fail; 2085 goto fail;
2070 2086
2087 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2088 name, namelen, objectid,
2089 root->fs_info->sb->s_root->d_inode->i_ino);
2090
2091 if (ret)
2092 goto fail;
2093
2071 ret = btrfs_inc_root_ref(trans, root, objectid); 2094 ret = btrfs_inc_root_ref(trans, root, objectid);
2072 if (ret) 2095 if (ret)
2073 goto fail; 2096 goto fail;
@@ -2338,7 +2361,6 @@ static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2338 struct inode *old_inode = old_dentry->d_inode; 2361 struct inode *old_inode = old_dentry->d_inode;
2339 struct timespec ctime = CURRENT_TIME; 2362 struct timespec ctime = CURRENT_TIME;
2340 struct btrfs_path *path; 2363 struct btrfs_path *path;
2341 struct btrfs_dir_item *di;
2342 int ret; 2364 int ret;
2343 2365
2344 if (S_ISDIR(old_inode->i_mode) && new_inode && 2366 if (S_ISDIR(old_inode->i_mode) && new_inode &&
@@ -2361,52 +2383,6 @@ static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2361 new_dir->i_ctime = new_dir->i_mtime = ctime; 2383 new_dir->i_ctime = new_dir->i_mtime = ctime;
2362 old_inode->i_ctime = ctime; 2384 old_inode->i_ctime = ctime;
2363 2385
2364 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
2365 struct btrfs_key *location = &BTRFS_I(new_dir)->location;
2366 struct btrfs_key old_parent_key;
2367 di = btrfs_lookup_dir_item(trans, root, path, old_inode->i_ino,
2368 "..", 2, -1);
2369 if (IS_ERR(di)) {
2370 ret = PTR_ERR(di);
2371 goto out_fail;
2372 }
2373 if (!di) {
2374 ret = -ENOENT;
2375 goto out_fail;
2376 }
2377 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &old_parent_key);
2378 ret = btrfs_del_item(trans, root, path);
2379 if (ret) {
2380 goto out_fail;
2381 }
2382 btrfs_release_path(root, path);
2383
2384 di = btrfs_lookup_dir_index_item(trans, root, path,
2385 old_inode->i_ino,
2386 old_parent_key.objectid,
2387 "..", 2, -1);
2388 if (IS_ERR(di)) {
2389 ret = PTR_ERR(di);
2390 goto out_fail;
2391 }
2392 if (!di) {
2393 ret = -ENOENT;
2394 goto out_fail;
2395 }
2396 ret = btrfs_del_item(trans, root, path);
2397 if (ret) {
2398 goto out_fail;
2399 }
2400 btrfs_release_path(root, path);
2401
2402 ret = btrfs_insert_dir_item(trans, root, "..", 2,
2403 old_inode->i_ino, location,
2404 BTRFS_FT_DIR);
2405 if (ret)
2406 goto out_fail;
2407 }
2408
2409
2410 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry); 2386 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2411 if (ret) 2387 if (ret)
2412 goto out_fail; 2388 goto out_fail;