Skip to contentSkip to conversation

btrfs orphan support

PATCH In review Josef Bacik 2 participants 3 messages Jul 9, 2008 – Jul 10, 2008

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs orphan support

Hello,

This patch makes it so btrfs can handle unlink's and truncates that are interrupted. On unlink/truncate an orphan item is added with the location of the inode. When the truncation/deletion is completed the orphan item is removed. If a crash happens in between the orphaned inodes are processed at root lookup time. This also catches the case where the inode deletion may have occured but the orphan item wasn't removed. Tested with a bunch of stuff to make sure everything is working. Thank you,

btrfs_inode.h +3 -0
diff -r 4c1c1ae077ef btrfs_inode.h
@@ -37,6 +37,9 @@ struct btrfs_inode {
 	struct posix_acl *i_acl;
 	struct posix_acl *i_default_acl;
 
+	/* for keeping track of orphaned inodes */
+	struct list_head i_orphan;
+
 	u64 ordered_trans;
 	/*
 	 * transid of the trans_handle that last modified this inode
ctree.h +20 -1
diff -r 4c1c1ae077ef ctree.h
@@ -68,6 +68,9 @@ extern struct kmem_cache *btrfs_path_cac
 /* directory objectid inside the root tree */
 #define BTRFS_ROOT_TREE_DIR_OBJECTID 6ULL
 
+/* orhpan objectid for tracking unlinked/truncated files */
+#define BTRFS_ORPHAN_OBJECTID -5ULL
+
 /*
  * All files have objectids higher than this.
  */
@@ -102,7 +105,8 @@ extern struct kmem_cache *btrfs_path_cac
 #define BTRFS_FT_SOCK		6
 #define BTRFS_FT_SYMLINK	7
 #define BTRFS_FT_XATTR		8
-#define BTRFS_FT_MAX		9
+#define BTRFS_FT_ORPHAN		9
+#define BTRFS_FT_MAX		10
 
 /*
  * the key defines the order in the tree, and so it also defines (optimal)
@@ -621,6 +625,10 @@ struct btrfs_root {
 
 	/* the dirty list is only used by non-reference counted roots */
 	struct list_head dirty_list;
+
+	/* orphan crap */
+	struct mutex orphan_mutex;
+	struct list_head orphan_list;
 };
 
 /*
@@ -632,6 +640,7 @@ struct btrfs_root {
 #define BTRFS_INODE_ITEM_KEY		1
 #define BTRFS_INODE_REF_KEY		2
 #define BTRFS_XATTR_ITEM_KEY		8
+#define BTRFS_ORPHAN_ITEM_KEY		9
 /* reserve 2-15 close to the inode for later flexibility */
 
 /*
@@ -1523,6 +1532,15 @@ struct btrfs_dir_item *btrfs_lookup_xatt
 					  struct btrfs_path *path, u64 dir,
 					  const char *name, u16 name_len,
 					  int mod);
+int btrfs_insert_orphan_item(struct btrfs_trans_handle *trans,
+			     struct btrfs_root *root, u64 offset,
+			     struct btrfs_key *location);
+struct btrfs_dir_item *btrfs_lookup_orphan_item(struct btrfs_trans_handle
+						*trans,
+						struct btrfs_root *root,
+						struct btrfs_path *path,
+						u64 offset, int mod);
+
 /* inode-map.c */
 int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
 			     struct btrfs_root *fs_root,
@@ -1617,6 +1635,7 @@ int btrfs_update_inode(struct btrfs_tran
 int btrfs_update_inode(struct btrfs_trans_handle *trans,
 			      struct btrfs_root *root,
 			      struct inode *inode);
+struct inode *btrfs_lookup_orphan_dir(struct btrfs_root *root);
 
 /* ioctl.c */
 long btrfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
dir-item.c +78 -0

84 lines not on this page · raw message

disk-io.c +2 -0
diff -r 4c1c1ae077ef disk-io.c
@@ -728,7 +728,9 @@ static int __setup_root(u32 nodesize, u3
 	root->in_sysfs = 0;
 
 	INIT_LIST_HEAD(&root->dirty_list);
+	INIT_LIST_HEAD(&root->orphan_list);
 	spin_lock_init(&root->node_lock);
+	mutex_init(&root->orphan_mutex);
 	mutex_init(&root->objectid_mutex);
 	memset(&root->root_key, 0, sizeof(root->root_key));
 	memset(&root->root_item, 0, sizeof(root->root_item));
inode.c +273 -4

387 lines not on this page · raw message

#1 lore raw
Quoted 10 lines · Josef Bacik

Hello,

This patch makes it so btrfs can handle unlink's and truncates that are interrupted. On unlink/truncate an orphan item is added with the location of the inode. When the truncation/deletion is completed the orphan item is removed. If a crash happens in between the orphaned inodes are processed at root lookup time. This also catches the case where the inode deletion may have occured but the orphan item wasn't removed. Tested with a bunch of stuff to make sure everything is working. Thank you,

Fantastic, thanks Josef. A few comments below.

ctree.h struct btrfs_root { · line 629

Quoted 7 lines

d so it also defines (optimal)

ctree.h +2 -0
@@ -621,6 +625,10 @@ struct btrfs_root {
 
 	/* the dirty list is only used by non-reference counted roots */
 	struct list_head dirty_list;
+
+	/* orphan crap */

Crap is worse than no comments at all ;)

Quoted 14 lines
dir-item.c +13 -0
+int btrfs_insert_orphan_item(struct btrfs_trans_handle *trans,
+			     struct btrfs_root *root, u64 offset,
+			     struct btrfs_key *location)
+{
+	struct btrfs_disk_key disk_key;
+	struct btrfs_dir_item *dir_item;
+	struct btrfs_path *path;
+	struct btrfs_key key;
+	struct extent_buffer *leaf;
+	int ret = 0;
+	u32 data_size;
+
+	key.objectid = BTRFS_ORPHAN_OBJECTID;

dir-item.c out: · line 206

Quoted 10 lines
dir-item.c +10 -0
+	btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
+	key.offset = offset;
+
+	path = btrfs_alloc_path();
+	if (!path)
+		return -ENOMEM;
+
+	data_size = sizeof(*dir_item);
+	dir_item = insert_with_overflow(trans, root, path, &key,
+					data_size, "", 0);

I thought your plan to move away from dir items would use a new item type completely.

We only really need to store the objectid of the inode, which is already encoded in the key offset field. So, you could try an 0 length item and we can fix up any problems that result in the btree code (I think it'll tolerate this).

inode.c zeroit: · line 613

Quoted 7 lines
inode.c +7 -0
+int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
+{
+	struct btrfs_root *root = BTRFS_I(inode)->root;
+	struct btrfs_key key;
+	int ret = 0;
+
+	mutex_lock(&root->orphan_mutex);

This orphan mutex is going to serialize things pretty badly, since you could be doing IO with it held. Could you please make this a spin lock held only during list manipulation?

The inode i_mutex should protect us from inserting/removing the same inode at the same time into the orphan index.

Thanks again, this is great.

-chris

#2 lore raw
Quoted 68 lines · Chris Mason
Quoted 10 lines · Josef Bacik

Hello,

This patch makes it so btrfs can handle unlink's and truncates that are interrupted. On unlink/truncate an orphan item is added with the location of the inode. When the truncation/deletion is completed the orphan item is removed. If a crash happens in between the orphaned inodes are processed at root lookup time. This also catches the case where the inode deletion may have occured but the orphan item wasn't removed. Tested with a bunch of stuff to make sure everything is working. Thank you,

Fantastic, thanks Josef. A few comments below.

Review struct btrfs_root { · line 629

Quoted 7 lines

d so it also defines (optimal)

(quoted diff) +2 -0
@@ -621,6 +625,10 @@ struct btrfs_root {
 
 	/* the dirty list is only used by non-reference counted roots */
 	struct list_head dirty_list;
+
+	/* orphan crap */

Crap is worse than no comments at all ;)

Quoted 14 lines
(quoted diff) +13 -0
+int btrfs_insert_orphan_item(struct btrfs_trans_handle *trans,
+			     struct btrfs_root *root, u64 offset,
+			     struct btrfs_key *location)
+{
+	struct btrfs_disk_key disk_key;
+	struct btrfs_dir_item *dir_item;
+	struct btrfs_path *path;
+	struct btrfs_key key;
+	struct extent_buffer *leaf;
+	int ret = 0;
+	u32 data_size;
+
+	key.objectid = BTRFS_ORPHAN_OBJECTID;

Review struct btrfs_root {

Quoted 10 lines
(quoted diff) +10 -0
+	btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
+	key.offset = offset;
+
+	path = btrfs_alloc_path();
+	if (!path)
+		return -ENOMEM;
+
+	data_size = sizeof(*dir_item);
+	dir_item = insert_with_overflow(trans, root, path, &key,
+					data_size, "", 0);

I thought your plan to move away from dir items would use a new item type completely.

We only really need to store the objectid of the inode, which is already encoded in the key offset field. So, you could try an 0 length item and we can fix up any problems that result in the btree code (I think it'll tolerate this).

Review

Quoted 7 lines
(quoted diff) +7 -0
+int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
+{
+	struct btrfs_root *root = BTRFS_I(inode)->root;
+	struct btrfs_key key;
+	int ret = 0;
+
+	mutex_lock(&root->orphan_mutex);

This orphan mutex is going to serialize things pretty badly, since you could be doing IO with it held. Could you please make this a spin lock held only during list manipulation?

Quoted 3 lines

The inode i_mutex should protect us from inserting/removing the same inode at the same time into the orphan index.

Hello,

Ok I've updated my patch with your suggestions. It also has the bugfix for insert empty item I tripped over. Thanks much,

Josef

Makefile +1 -1
diff -r 4c1c1ae077ef Makefile
@@ -6,7 +6,7 @@ btrfs-y := super.o ctree.o extent-tree.o
 	   hash.o file-item.o inode-item.o inode-map.o disk-io.o \
 	   transaction.o bit-radix.o inode.o file.o tree-defrag.o \
 	   extent_map.o sysfs.o struct-funcs.o xattr.o ordered-data.o \
-	   extent_io.o volumes.o async-thread.o ioctl.o locking.o
+	   extent_io.o volumes.o async-thread.o ioctl.o locking.o orphan.o
 
 btrfs-$(CONFIG_FS_POSIX_ACL)	+= acl.o
 else
btrfs_inode.h +3 -0
diff -r 4c1c1ae077ef btrfs_inode.h
@@ -37,6 +37,9 @@ struct btrfs_inode {
 	struct posix_acl *i_acl;
 	struct posix_acl *i_default_acl;
 
+	/* for keeping track of orphaned inodes */
+	struct list_head i_orphan;
+
 	u64 ordered_trans;
 	/*
 	 * transid of the trans_handle that last modified this inode
ctree.c +1 -1
diff -r 4c1c1ae077ef ctree.c
@@ -2626,7 +2626,7 @@ int btrfs_insert_empty_items(struct btrf
 		total_data += data_size[i];
 	}
 
-	total_size = total_data + (nr - 1) * sizeof(struct btrfs_item);
+	total_size = total_data + (nr * sizeof(struct btrfs_item));
 	ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
 	if (ret == 0) {
 		return -EEXIST;
ctree.h +14 -0
diff -r 4c1c1ae077ef ctree.h
@@ -67,6 +67,9 @@ extern struct kmem_cache *btrfs_path_cac
 
 /* directory objectid inside the root tree */
 #define BTRFS_ROOT_TREE_DIR_OBJECTID 6ULL
+
+/* orhpan objectid for tracking unlinked/truncated files */
+#define BTRFS_ORPHAN_OBJECTID -5ULL
 
 /*
  * All files have objectids higher than this.
@@ -621,6 +624,9 @@ struct btrfs_root {
 
 	/* the dirty list is only used by non-reference counted roots */
 	struct list_head dirty_list;
+
+	spinlock_t orphan_lock;
+	struct list_head orphan_list;
 };
 
 /*
@@ -632,6 +638,7 @@ struct btrfs_root {
 #define BTRFS_INODE_ITEM_KEY		1
 #define BTRFS_INODE_REF_KEY		2
 #define BTRFS_XATTR_ITEM_KEY		8
+#define BTRFS_ORPHAN_ITEM_KEY		9
 /* reserve 2-15 close to the inode for later flexibility */
 
 /*
@@ -1523,6 +1530,13 @@ struct btrfs_dir_item *btrfs_lookup_xatt
 					  struct btrfs_path *path, u64 dir,
 					  const char *name, u16 name_len,
 					  int mod);
+
+/* orphan.c */
+int btrfs_insert_orphan_item(struct btrfs_trans_handle *trans,
+			     struct btrfs_root *root, u64 offset);
+int btrfs_del_orphan_item(struct btrfs_trans_handle *trans,
+			  struct btrfs_root *root, u64 offset);
+
 /* inode-map.c */
 int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
 			     struct btrfs_root *fs_root,
disk-io.c +2 -0
diff -r 4c1c1ae077ef disk-io.c
@@ -728,7 +728,9 @@ static int __setup_root(u32 nodesize, u3
 	root->in_sysfs = 0;
 
 	INIT_LIST_HEAD(&root->dirty_list);
+	INIT_LIST_HEAD(&root->orphan_list);
 	spin_lock_init(&root->node_lock);
+	spin_lock_init(&root->orphan_lock);
 	mutex_init(&root->objectid_mutex);
 	memset(&root->root_key, 0, sizeof(root->root_key));
 	memset(&root->root_item, 0, sizeof(root->root_item));
inode.c +233 -4

347 lines not on this page · raw message

orphan.c new file +67 -0

67 lines not on this page · raw message