All of lore.kernel.org
 help / color / mirror / Atom feed
From: Josef Bacik <jbacik@fb.com>
To: <dsterba@suse.cz>, <linux-btrfs@vger.kernel.org>
Subject: [PATCH 09/11] Btrfs-progs: fix missing inode items
Date: Fri, 31 Oct 2014 14:01:27 -0400	[thread overview]
Message-ID: <1414778489-4049-10-git-send-email-jbacik@fb.com> (raw)
In-Reply-To: <1414778489-4049-1-git-send-email-jbacik@fb.com>

If we have all the other items but no inode item we can recreate it for the most
part, with the exception of the permissions and ownership.  Add this ability to
btrfsck.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
---
 cmds-check.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 76 insertions(+)

diff --git a/cmds-check.c b/cmds-check.c
index be75dcb..a30db8d 100644
--- a/cmds-check.c
+++ b/cmds-check.c
@@ -1727,6 +1727,61 @@ static int delete_dir_index(struct btrfs_root *root,
 	return ret;
 }
 
+static int create_inode_item(struct btrfs_root *root,
+			     struct inode_record *rec,
+			     struct inode_backref *backref, int root_dir)
+{
+	struct btrfs_trans_handle *trans;
+	struct btrfs_inode_item inode_item;
+	time_t now = time(NULL);
+	int ret;
+
+	trans = btrfs_start_transaction(root, 1);
+	if (IS_ERR(trans)) {
+		ret = PTR_ERR(trans);
+		return ret;
+	}
+
+	fprintf(stderr, "root %llu inode %llu recreating inode item, this may "
+		"be incomplete, please check permissions and content after "
+		"the fsck completes.\n", (unsigned long long)root->objectid,
+		(unsigned long long)rec->ino);
+
+	memset(&inode_item, 0, sizeof(inode_item));
+	btrfs_set_stack_inode_generation(&inode_item, trans->transid);
+	if (root_dir)
+		btrfs_set_stack_inode_nlink(&inode_item, 1);
+	else
+		btrfs_set_stack_inode_nlink(&inode_item, rec->found_link);
+	btrfs_set_stack_inode_nbytes(&inode_item, rec->found_size);
+	if (rec->found_dir_item) {
+		if (rec->found_file_extent)
+			fprintf(stderr, "root %llu inode %llu has both a dir "
+				"item and extents, unsure if it is a dir or a "
+				"regular file so setting it as a directory\n",
+				(unsigned long long)root->objectid,
+				(unsigned long long)rec->ino);
+		btrfs_set_stack_inode_mode(&inode_item, S_IFDIR | 0755);
+		btrfs_set_stack_inode_size(&inode_item, rec->found_size);
+	} else if (!rec->found_dir_item) {
+		btrfs_set_stack_inode_size(&inode_item, rec->extent_end);
+		btrfs_set_stack_inode_mode(&inode_item, S_IFREG | 0755);
+	}
+	btrfs_set_stack_timespec_sec(&inode_item.atime, now);
+	btrfs_set_stack_timespec_nsec(&inode_item.atime, 0);
+	btrfs_set_stack_timespec_sec(&inode_item.ctime, now);
+	btrfs_set_stack_timespec_nsec(&inode_item.ctime, 0);
+	btrfs_set_stack_timespec_sec(&inode_item.mtime, now);
+	btrfs_set_stack_timespec_nsec(&inode_item.mtime, 0);
+	btrfs_set_stack_timespec_sec(&inode_item.otime, 0);
+	btrfs_set_stack_timespec_nsec(&inode_item.otime, 0);
+
+	ret = btrfs_insert_inode(trans, root, rec->ino, &inode_item);
+	BUG_ON(ret);
+	btrfs_commit_transaction(trans, root);
+	return 0;
+}
+
 static int repair_inode_backrefs(struct btrfs_root *root,
 				 struct inode_record *rec,
 				 struct cache_tree *inode_cache,
@@ -1738,6 +1793,15 @@ static int repair_inode_backrefs(struct btrfs_root *root,
 	int repaired = 0;
 
 	list_for_each_entry_safe(backref, tmp, &rec->backrefs, list) {
+		if (!delete && rec->ino == root_dirid) {
+			if (!rec->found_inode_item) {
+				ret = create_inode_item(root, rec, backref, 1);
+				if (ret)
+					break;
+				repaired++;
+			}
+		}
+
 		/* Index 0 for root dir's are special, don't mess with it */
 		if (rec->ino == root_dirid && backref->index == 0)
 			continue;
@@ -1799,6 +1863,18 @@ static int repair_inode_backrefs(struct btrfs_root *root,
 			btrfs_commit_transaction(trans, root);
 			repaired++;
 		}
+
+		if (!delete && (backref->found_inode_ref &&
+				backref->found_dir_index &&
+				backref->found_dir_item &&
+				!(backref->errors & REF_ERR_INDEX_UNMATCH) &&
+				!rec->found_inode_item)) {
+			ret = create_inode_item(root, rec, backref, 0);
+			if (ret)
+				break;
+			repaired++;
+		}
+
 	}
 	return ret ? ret : repaired;
 }
-- 
1.8.3.1


  parent reply	other threads:[~2014-10-31 18:02 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-31 18:01 [GIT PULL] Various btrfsck updates Josef Bacik
2014-10-31 18:01 ` [PATCH 01/11] Btrfs-progs: add the ability to delete items Josef Bacik
2014-10-31 18:01 ` [PATCH 03/11] Btrfs-progs: allow fsck to take the tree bytenr Josef Bacik
2014-11-04 21:58   ` Ansgar Hockmann-Stolle
2014-10-31 18:01 ` [PATCH 04/11] Btrfs-progs: don't fail on log tree opening with PARTIAL Josef Bacik
2014-10-31 18:01 ` [PATCH 05/11] Btrfs-progs: spit out the broken file when ignoring errors Josef Bacik
2014-10-31 18:01 ` [PATCH 06/11] Btrfs-progs: make zero-log use partial open Josef Bacik
2014-10-31 18:01 ` [PATCH 07/11] Btrfs-progs: add a message to know zero log ran successfully Josef Bacik
2014-10-31 18:01 ` [PATCH 08/11] Btrfs-progs: add ability to replace missing dir item/dir indexes Josef Bacik
2014-10-31 18:01 ` Josef Bacik [this message]
2014-10-31 18:01 ` [PATCH 10/11] Btrfs-progs: create missing root dirid Josef Bacik

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1414778489-4049-10-git-send-email-jbacik@fb.com \
    --to=jbacik@fb.com \
    --cc=dsterba@suse.cz \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.