Skip to contentSkip to conversation

btrfs orphan code

RFC PATCH In review Josef Bacik 2 participants 2 messages Jun 24, 2008 – Jun 25, 2008 Btrfs file system

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 btrfs orphan code

Hello,

I want to throw this out here now that I've got most of the heavy lifting done for this code to make sure what I'm doing is ok for now. I've added an ORPHAN_DIR item key to have a hidden dir per root. Right now it just does it for whatever the default root is on mount, but I'm going to fix that to do the orphan dir check/creation on lookup of a subvolume root. I also changed btrfs_insert_dir_item to take an index flag to indicate whether or not we want to add a DIR_INDEX item along with the dir item. Let me know if there are any glaring design problems with what I've done. Thanks much,

Josef

btrfs_inode.h +3 -0
diff -r 99b12e2db0f8 btrfs_inode.h
@@ -36,6 +36,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 +18 -3
diff -r 99b12e2db0f8 ctree.h
@@ -103,7 +103,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_DIR	9
+#define BTRFS_FT_MAX		10
 
 /*
  * the key defines the order in the tree, and so it also defines (optimal)
@@ -613,6 +614,10 @@ struct btrfs_root {
 
 	/* the dirty list is only used by non-reference counted roots */
 	struct list_head dirty_list;
+
+	/* orphan crap */
+	struct inode *orphan_dir;
+	struct list_head orphan_list;
 };
 
 /*
@@ -624,6 +629,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_DIR_ITEM_KEY	9
 /* reserve 2-15 close to the inode for later flexibility */
 
 /*
@@ -1485,7 +1491,7 @@ int btrfs_find_dead_roots(struct btrfs_r
 /* dir-item.c */
 int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
 			  *root, const char *name, int name_len, u64 dir,
-			  struct btrfs_key *location, u8 type);
+			  struct btrfs_key *location, u8 type, int index);
 struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
 					     struct btrfs_root *root,
 					     struct btrfs_path *path, u64 dir,
@@ -1513,6 +1519,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_dir_item(struct btrfs_trans_handle *trans,
+				 struct btrfs_root *root,
+				 struct btrfs_key *location);
+struct btrfs_dir_item *btrfs_lookup_orphan_dir_item(struct btrfs_trans_handle
+						    *trans,
+						    struct btrfs_root *root,
+						    struct btrfs_path *path,
+						    int mod);
+
 /* inode-map.c */
 int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
 			     struct btrfs_root *fs_root,
@@ -1607,7 +1622,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 +83 -2

103 lines not on this page · raw message

disk-io.c +6 -0
diff -r 99b12e2db0f8 disk-io.c
@@ -701,8 +701,10 @@ static int __setup_root(u32 nodesize, u3
 			struct btrfs_fs_info *fs_info,
 			u64 objectid)
 {
+	printk(KERN_ERR "setting up root %p\n", root);
 	root->node = NULL;
 	root->inode = NULL;
+	root->orphan_dir = NULL;
 	root->commit_root = NULL;
 	root->sectorsize = sectorsize;
 	root->nodesize = nodesize;
@@ -720,6 +722,7 @@ static int __setup_root(u32 nodesize, u3
 	root->in_sysfs = 0;
 
 	INIT_LIST_HEAD(&root->dirty_list);
+	INIT_LIST_HEAD(&root->orphan_list);
 	memset(&root->root_key, 0, sizeof(root->root_key));
 	memset(&root->root_item, 0, sizeof(root->root_item));
 	memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
@@ -1515,6 +1518,8 @@ int btrfs_free_fs_root(struct btrfs_fs_i
 			  (unsigned long)root->root_key.objectid);
 	if (root->in_sysfs)
 		btrfs_sysfs_del_root(root);
+	if (root->orphan_dir)
+		iput(root->orphan_dir);
 	if (root->inode)
 		iput(root->inode);
 	if (root->node)
@@ -1603,6 +1608,7 @@ int close_ctree(struct btrfs_root *root)
 	btrfs_stop_workers(&fs_info->submit_workers);
 
 	iput(fs_info->btree_inode);
+
 #if 0
 	while(!list_empty(&fs_info->hashers)) {
 		struct btrfs_hasher *hasher;
inode.c +271 -6

325 lines not on this page · raw message

ioctl.c +1 -1
diff -r 99b12e2db0f8 ioctl.c
@@ -128,7 +128,7 @@ static noinline int create_subvol(struct
 	dir = root->fs_info->sb->s_root->d_inode;
 	ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
 				    name, namelen, dir->i_ino, &key,
-				    BTRFS_FT_DIR);
+				    BTRFS_FT_DIR, 0);
 	if (ret)
 		goto fail;
 
super.c +17 -0
diff -r 99b12e2db0f8 super.c
@@ -339,6 +339,7 @@ static int btrfs_fill_super(struct super
 		goto fail_close;
 	}
 
+
 	/* this does the super kobj at the same time */
 	err = btrfs_sysfs_add_super(tree_root->fs_info);
 	if (err)
@@ -467,6 +468,22 @@ static int btrfs_get_sb(struct file_syst
 		deactivate_super(s);
 		error = -ENXIO;
 		goto error;
+	}
+
+	/* we don't want to do orphan stuff on a rdonly fs */
+	if (!(s->s_flags & MS_RDONLY)) {
+		struct btrfs_root *tree_root = BTRFS_I(root->d_inode)->root;
+		struct inode *orphan_dir;
+
+		orphan_dir = btrfs_lookup_orphan_dir(tree_root);
+		if (IS_ERR(orphan_dir)) {
+			error = PTR_ERR(orphan_dir);
+			printk(KERN_ERR "error reading orphan dir: %d\n", error);
+			//goto error;
+		} else {
+			printk(KERN_ERR "setting orphan_dir for root %p\n", tree_root);
+			tree_root->orphan_dir = orphan_dir;
+		}
 	}
 
 	mnt->mnt_sb = s;
transaction.c +1 -1
diff -r 99b12e2db0f8 transaction.c
@@ -600,7 +600,7 @@ static noinline int create_pending_snaps
 	ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
 				    pending->name, namelen,
 				    root->fs_info->sb->s_root->d_inode->i_ino,
-				    &key, BTRFS_FT_DIR);
+				    &key, BTRFS_FT_DIR, 0);
 
 	if (ret)
 		goto fail;
#1 lore raw
Quoted 11 lines · Josef Bacik

Hello,

I want to throw this out here now that I've got most of the heavy lifting done for this code to make sure what I'm doing is ok for now. I've added an ORPHAN_DIR item key to have a hidden dir per root. Right now it just does it for whatever the default root is on mount, but I'm going to fix that to do the orphan dir check/creation on lookup of a subvolume root. I also changed btrfs_insert_dir_item to take an index flag to indicate whether or not we want to add a DIR_INDEX item along with the dir item. Let me know if there are any glaring design problems with what I've done. Thanks much,

Ok, I've poked my head out of the locking code now, this is a pretty good start. But, the plan you mentioned on irc to use a special item type instead of a directory is a better way to go.

After that, we'll have to hammer on it a bit to flush out any races between relinking a recently unlinked file and the orphan code.

Thanks a lot for tackling this.

-chris