diff mbox

Btrfs-progs: repair missing dir index

Message ID 1412275839-8131-1-git-send-email-jbacik@fb.com (mailing list archive)
State Superseded, archived
Headers show

Commit Message

Josef Bacik Oct. 2, 2014, 6:50 p.m. UTC
If we have an inode backref entry then we know enough to add back a missing dir
index.  When messing with the inode backrefs we need to do all of that first
before we process the inode recs themselves as we may clear errors on the inode
recs as we fix the directory indexes.  This adds the framework for fixing
backref errors and fixes missing dir index issues.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
---
 cmds-check.c                          | 131 +++++++++++++++++++++++++++++++++-
 tests/fsck-tests/004-no-dir-index.img | Bin 0 -> 4096 bytes
 2 files changed, 129 insertions(+), 2 deletions(-)
 create mode 100644 tests/fsck-tests/004-no-dir-index.img

diff --git a/tests/fsck-tests/004-no-dir-index.img b/tests/fsck-tests/004-no-dir-index.img
new file mode 100644
index 0000000000000000000000000000000000000000..6f2483e6e4285e6703096b67c5153b3da3309b7b
GIT binary patch
literal 4096
zcmeH|c{CJUAIEJ`%Gf3$Yh{TN(O65bwZcpzM#hrFj3rNlG!&ySmKlmlLp5Y4%QQq8
z3`QlwWEuM!qM{Lxb%uGF^PcmZ^PJ~-|9;PTpL_24-p~Erd+t5I@4e?<JR&vdi;8O<
z&wq;0cFW7H+aSD6JUnN)s>am@t^x&j!i`)W=1!q;d5+8PxO{tuc^bIt#*J|CANj|D
ze;oMVaUgVXXo8{^!z;RWI&1#nFXU<u7NZaAarQrf@bD18>zYkZ5>2AIq9!8@J!EA8
zXY=Hw$+BtVRMY{<_{I-+mBhd5?7sWjOecAB_c`)7%H`0>IPoUFIMuiJv=a}oFFH5s
zobIMmgCB5y6zEbxp0^{s_;2ame#h%0aVJVFkmr%Dk49Q@_er4cAvLJn9zYA<<*&XC
ze)lAVGsUm-O5%2jZO%$5O^JG>BBgibcmNZlpxVM#yha8nF<1ZcY({zf*Kf*t>->d_
zfZ0VM=yd1P4~bKh?g{O~LEW-VL?J*{OHPqB8p;3Eq;fcn$5YF=W5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comments

David Sterba Oct. 10, 2014, 8:43 a.m. UTC | #1
On Thu, Oct 02, 2014 at 02:50:39PM -0400, Josef Bacik wrote:
> +static int repair_inode_backrefs(struct btrfs_root *root,
> +				 struct inode_record *rec,
> +				 struct cache_tree *inode_cache)
> +{
> +	struct btrfs_trans_handle *trans;
> +	struct btrfs_path *path;
> +	struct inode_backref *tmp, *backref;
> +	u64 root_dirid = btrfs_root_dirid(&root->root_item);
> +	int ret;

Changed to ret = 1,

> +	list_for_each_entry_safe(backref, tmp, &rec->backrefs, list) {
[...]
> +	}

gcc prints warning about possibly unitilized ret here:

> +	BUG_ON(repaired && ret); /* Poor mans transaction abort */

So it would abort in case the list_for_each loop does not execute for
some reason.
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Josef Bacik Oct. 10, 2014, 10:48 a.m. UTC | #2
I'm redoing this patch a bit so don't take it yet. Thanks,

Josef

David Sterba <dsterba@suse.cz> wrote:


On Thu, Oct 02, 2014 at 02:50:39PM -0400, Josef Bacik wrote:
> +static int repair_inode_backrefs(struct btrfs_root *root,
> +                              struct inode_record *rec,
> +                              struct cache_tree *inode_cache)
> +{
> +     struct btrfs_trans_handle *trans;
> +     struct btrfs_path *path;
> +     struct inode_backref *tmp, *backref;
> +     u64 root_dirid = btrfs_root_dirid(&root->root_item);
> +     int ret;

Changed to ret = 1,

> +     list_for_each_entry_safe(backref, tmp, &rec->backrefs, list) {
[...]
> +     }

gcc prints warning about possibly unitilized ret here:

> +     BUG_ON(repaired && ret); /* Poor mans transaction abort */

So it would abort in case the list_for_each loop does not execute for
some reason.
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/cmds-check.c b/cmds-check.c
index b962b15..5b8417c 100644
--- a/cmds-check.c
+++ b/cmds-check.c
@@ -1504,15 +1504,121 @@  static int repair_inode_orphan_item(struct btrfs_trans_handle *trans,
 	return ret;
 }
 
+static int add_missing_dir_index(struct btrfs_trans_handle *trans,
+				 struct btrfs_root *root,
+				 struct cache_tree *inode_cache,
+				 struct btrfs_path *path,
+				 struct inode_record *rec,
+				 struct inode_backref *backref)
+{
+	struct btrfs_dir_item *dir_item;
+	struct extent_buffer *leaf;
+	struct btrfs_key key;
+	struct btrfs_disk_key disk_key;
+	struct inode_record *dir_rec;
+	unsigned long name_ptr;
+	u32 data_size = sizeof(*dir_item) + backref->namelen;
+	int ret;
+
+	printf("repairing missing dir index item for inode %llu\n",
+	       (unsigned long long)rec->ino);
+	key.objectid = backref->dir;
+	key.type = BTRFS_DIR_INDEX_KEY;
+	key.offset = backref->index;
+
+	ret = btrfs_insert_empty_item(trans, root, path, &key, data_size);
+	if (ret)
+		return ret;
+
+	leaf = path->nodes[0];
+	dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
+
+	disk_key.objectid = cpu_to_le64(rec->ino);
+	disk_key.type = BTRFS_INODE_ITEM_KEY;
+	disk_key.offset = 0;
+
+	btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
+	btrfs_set_dir_type(leaf, dir_item, imode_to_type(rec->imode));
+	btrfs_set_dir_data_len(leaf, dir_item, 0);
+	btrfs_set_dir_name_len(leaf, dir_item, backref->namelen);
+	name_ptr = (unsigned long)(dir_item + 1);
+	write_extent_buffer(leaf, backref->name, name_ptr, backref->namelen);
+	btrfs_mark_buffer_dirty(leaf);
+	btrfs_release_path(path);
+	backref->found_dir_index = 1;
+
+	dir_rec = get_inode_rec(inode_cache, backref->dir, 0);
+	if (!dir_rec)
+		return 0;
+	dir_rec->found_size += backref->namelen;
+	if (dir_rec->found_size == dir_rec->isize &&
+	    (dir_rec->errors & I_ERR_DIR_ISIZE_WRONG))
+		dir_rec->errors &= ~I_ERR_DIR_ISIZE_WRONG;
+	if (dir_rec->found_size != dir_rec->isize)
+		dir_rec->errors |= I_ERR_DIR_ISIZE_WRONG;
+
+	return 0;
+
+}
+
+static int repair_inode_backrefs(struct btrfs_root *root,
+				 struct inode_record *rec,
+				 struct cache_tree *inode_cache)
+{
+	struct btrfs_trans_handle *trans;
+	struct btrfs_path *path;
+	struct inode_backref *tmp, *backref;
+	u64 root_dirid = btrfs_root_dirid(&root->root_item);
+	int ret;
+	int repaired = 0;
+
+	path = btrfs_alloc_path();
+	if (!path)
+		return -ENOMEM;
+
+	trans = btrfs_start_transaction(root, 1);
+	if (IS_ERR(trans)) {
+		btrfs_free_path(path);
+		return PTR_ERR(trans);
+	}
+
+	list_for_each_entry_safe(backref, tmp, &rec->backrefs, list) {
+		/* Index 0 for root dir's are special, don't mess with it */
+		if (rec->ino == root_dirid && backref->index == 0)
+			continue;
+
+		if (!backref->found_dir_index && backref->found_inode_ref) {
+			ret = add_missing_dir_index(trans, root, inode_cache,
+						    path, rec, backref);
+			if (!ret)
+				repaired = 1;
+			else
+				break;
+		}
+
+		if (backref->found_dir_item && backref->found_dir_index) {
+			if (!backref->errors && backref->found_inode_ref) {
+				list_del(&backref->list);
+				free(backref);
+			}
+		}
+	}
+
+	BUG_ON(repaired && ret); /* Poor mans transaction abort */
+	btrfs_commit_transaction(trans, root);
+	btrfs_free_path(path);
+
+	return ret;
+}
+
 static int try_repair_inode(struct btrfs_root *root, struct inode_record *rec)
 {
 	struct btrfs_trans_handle *trans;
 	struct btrfs_path *path;
 	int ret = 0;
 
-	/* So far we just fix dir isize wrong */
 	if (!(rec->errors & (I_ERR_DIR_ISIZE_WRONG | I_ERR_NO_ORPHAN_ITEM)))
-		return 1;
+		return rec->errors;
 
 	path = btrfs_alloc_path();
 	if (!path)
@@ -1550,6 +1656,27 @@  static int check_inode_recs(struct btrfs_root *root,
 		return 0;
 	}
 
+	/*
+	 * We need to repair backrefs first because we could change some of the
+	 * errors in the inode recs.
+	 *
+	 * For example, if we were missing a dir index then the directories
+	 * isize would be wrong, so if we fixed the isize to what we thought it
+	 * would be and then fixed the backref we'd still have a invalid fs, so
+	 * we need to add back the dir index and then check to see if the isize
+	 * is still wrong.
+	 */
+	cache = search_cache_extent(inode_cache, 0);
+	while (repair && cache) {
+		node = container_of(cache, struct ptr_node, cache);
+		rec = node->data;
+		cache = next_cache_extent(cache);
+
+		if (list_empty(&rec->backrefs))
+			continue;
+		repair_inode_backrefs(root, rec, inode_cache);
+	}
+
 	rec = get_inode_rec(inode_cache, root_dirid, 0);
 	if (rec) {
 		ret = check_root_dir(rec);