aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/inode.c
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/inode.c
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/inode.c')
-rw-r--r--fs/btrfs/inode.c258
1 files changed, 117 insertions, 141 deletions
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;