diff mbox

[01/15] btrfs-progs: lowmem check: introduce repair_inode_item_mismatch()

Message ID 20180126083519.28373-2-suy.fnst@cn.fujitsu.com (mailing list archive)
State New, archived
Headers show

Commit Message

Su Yue Jan. 26, 2018, 8:35 a.m. UTC
New function repair_inode_item_mismatch() only changes inode item'
filetype.

Signed-off-by: Su Yue <suy.fnst@cn.fujitsu.com>
---
 cmds-check.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)
diff mbox

Patch

diff --git a/cmds-check.c b/cmds-check.c
index f302724dd840..e3505a7f9d6b 100644
--- a/cmds-check.c
+++ b/cmds-check.c
@@ -4913,6 +4913,58 @@  static void print_inode_ref_err(struct btrfs_root *root, struct btrfs_key *key,
 		      namebuf, filetype);
 }
 
+/*
+ * Change inode mode.
+ *
+ * Returns 0 means success.
+ * Returns <0 means error.
+ */
+static int repair_inode_item_mismatch(struct btrfs_root *root, u64 ino,
+				      u8 filetype)
+{
+	struct btrfs_key key;
+	struct btrfs_trans_handle *trans;
+	struct btrfs_path path;
+	struct btrfs_inode_item *ii;
+	u32 mode;
+	int ret;
+
+	key.objectid = ino;
+	key.type = BTRFS_INODE_ITEM_KEY;
+	key.offset = 0;
+
+	btrfs_init_path(&path);
+	trans = btrfs_start_transaction(root, 1);
+	if (IS_ERR(trans)) {
+		ret = -EIO;
+		goto out;
+	}
+
+	ret = btrfs_search_slot(trans, root, &key, &path, 0, 1);
+	if (ret > 0)
+		ret = -ENOENT;
+	if (ret)
+		goto fail;
+
+	ii = btrfs_item_ptr(path.nodes[0], path.slots[0],
+			    struct btrfs_inode_item);
+	mode = btrfs_inode_mode(path.nodes[0], ii);
+	mode &= ~S_IFMT;
+	mode |= btrfs_type_to_imode(filetype);
+
+	btrfs_set_inode_mode(path.nodes[0], ii, mode);
+	btrfs_mark_buffer_dirty(path.nodes[0]);
+	ret = 0;
+fail:
+	btrfs_commit_transaction(trans, root);
+out:
+	btrfs_release_path(&path);
+	if (ret)
+		error("failed to repair root %llu INODE ITEM[%llu] MISMATCH",
+		      root->objectid, ino);
+	return ret;
+}
+
 /*
  * Insert the missing inode item.
  *