public inbox for stable@vger.kernel.org
 help / color / mirror / Atom feed
From: David Sterba <dsterba@suse.com>
To: stable@vger.kernel.org
Subject: [PATCH 17/21] Btrfs: fix for incorrect directory entries after fsync log replay
Date: Fri,  3 Jun 2016 17:42:44 +0200	[thread overview]
Message-ID: <1464968564-6952-1-git-send-email-dsterba@suse.com> (raw)
In-Reply-To: <20160603154006.GP29147@suse.cz>

From: Filipe Manana <fdmanana@suse.com>

commit 657ed1aa4898c8304500e0d13f240d5a67e8be5f upstream.

If we move a directory to a new parent and later log that parent and don't
explicitly log the old parent, when we replay the log we can end up with
entries for the moved directory in both the old and new parent directories.
Besides being ilegal to have directories with multiple hard links in linux,
it also resulted in the leaving the inode item with a link count of 1.
A similar issue also happens if we move a regular file - after the log tree
is replayed the file has a link in both the old and new parent directories,
when it should be only at the new directory.

Sample reproducer:

  $ mkfs.btrfs -f /dev/sdc
  $ mount /dev/sdc /mnt
  $ mkdir /mnt/x
  $ mkdir /mnt/y
  $ touch /mnt/x/foo
  $ mkdir /mnt/y/z
  $ sync
  $ ln /mnt/x/foo /mnt/x/bar
  $ mv /mnt/y/z /mnt/x/z
  < power fail >
  $ mount /dev/sdc /mnt
  $ ls -1Ri /mnt
  /mnt:
  257 x
  258 y

  /mnt/x:
  259 bar
  259 foo
  260 z

  /mnt/x/z:

  /mnt/y:
  260 z

  /mnt/y/z:

  $ umount /dev/sdc
  $ btrfs check /dev/sdc
  Checking filesystem on /dev/sdc
  UUID: a67e2c4a-a4b4-4fdc-b015-9d9af1e344be
  checking extents
  checking free space cache
  checking fs roots
  root 5 inode 260 errors 2000, link count wrong
        unresolved ref dir 257 index 4 namelen 1 name z filetype 2 errors 0
        unresolved ref dir 258 index 2 namelen 1 name z filetype 2 errors 0
  (...)

Attempting to remove the directory becomes impossible:

  $ mount /dev/sdc /mnt
  $ rmdir /mnt/y/z
  $ ls -lh /mnt/y
  ls: cannot access /mnt/y/z: No such file or directory
  total 0
  d????????? ? ? ? ?            ? z
  $ rmdir /mnt/x/z
  rmdir: failed to remove ‘/mnt/x/z’: Stale file handle
  $ ls -lh /mnt/x
  ls: cannot access /mnt/x/z: Stale file handle
  total 0
  -rw-r--r-- 2 root root 0 Apr  6 18:06 bar
  -rw-r--r-- 2 root root 0 Apr  6 18:06 foo
  d????????? ? ?    ?    ?            ? z

So make sure that on rename we set the last_unlink_trans value for our
inode, even if it's a directory, to the value of the current transaction's
ID and that if the new parent directory is logged that we fallback to a
transaction commit.

A test case for fstests is being submitted as well.

Signed-off-by: Filipe Manana <fdmanana@suse.com>
---
 fs/btrfs/tree-log.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
index 58ae0a2ce65c..23df70e7f884 100644
--- a/fs/btrfs/tree-log.c
+++ b/fs/btrfs/tree-log.c
@@ -5277,11 +5277,16 @@ static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
 			if (IS_ERR(dir_inode))
 				continue;
 
+			if (ctx)
+				ctx->log_new_dentries = false;
 			ret = btrfs_log_inode(trans, root, dir_inode,
 					      LOG_INODE_ALL, 0, LLONG_MAX, ctx);
 			if (!ret &&
 			    btrfs_must_commit_transaction(trans, dir_inode))
 				ret = 1;
+			if (!ret && ctx && ctx->log_new_dentries)
+				ret = log_new_dir_dentries(trans, root,
+							   dir_inode, ctx);
 			iput(dir_inode);
 			if (ret)
 				goto out;
@@ -5651,11 +5656,9 @@ void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
 	 * into the file.  When the file is logged we check it and
 	 * don't log the parents if the file is fully on disk.
 	 */
-	if (S_ISREG(inode->i_mode)) {
-		mutex_lock(&BTRFS_I(inode)->log_mutex);
-		BTRFS_I(inode)->last_unlink_trans = trans->transid;
-		mutex_unlock(&BTRFS_I(inode)->log_mutex);
-	}
+	mutex_lock(&BTRFS_I(inode)->log_mutex);
+	BTRFS_I(inode)->last_unlink_trans = trans->transid;
+	mutex_unlock(&BTRFS_I(inode)->log_mutex);
 
 	/*
 	 * if this directory was already logged any new
-- 
2.7.1


  parent reply	other threads:[~2016-06-03 15:43 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-03 15:40 Btrfs stable patches for 4.5.x David Sterba
2016-06-03 15:42 ` [PATCH 01/21] btrfs: bugfix: handle FS_IOC32_{GETFLAGS,SETFLAGS,GETVERSION} in btrfs_ioctl David Sterba
2016-06-03 15:42 ` [PATCH 02/21] Btrfs: do not create empty block group if we have allocated data David Sterba
2016-06-03 15:42 ` [PATCH 03/21] btrfs: allow balancing to dup with multi-device David Sterba
2016-06-03 15:42 ` [PATCH 04/21] btrfs: fix mixed block count of available space David Sterba
2016-06-03 15:42 ` [PATCH 05/21] btrfs: avoid overflowing f_bfree David Sterba
2016-06-03 15:42 ` [PATCH 06/21] btrfs: fix lock dep warning, move scratch dev out of device_list_mutex and uuid_mutex David Sterba
2016-06-03 15:42 ` [PATCH 07/21] btrfs: add read-only check to sysfs handler of features David Sterba
2016-06-03 15:42 ` [PATCH 08/21] btrfs: add check to sysfs handler of label David Sterba
2016-06-03 15:42 ` [PATCH 09/21] Btrfs: fix divide error upon chunk's stripe_len David Sterba
2016-06-03 15:42 ` [PATCH 10/21] Btrfs: remove BUG_ON()'s in btrfs_map_block David Sterba
2016-06-03 15:42 ` [PATCH 11/21] btrfs: fix lock dep warning move scratch super outside of chunk_mutex David Sterba
2016-06-03 15:42 ` [PATCH 12/21] btrfs: add write protection to SET_FEATURES ioctl David Sterba
2016-06-03 15:42 ` [PATCH 13/21] btrfs: fix int32 overflow in shrink_delalloc() David Sterba
2016-06-03 15:42 ` [PATCH 14/21] Btrfs: fix fspath error deallocation David Sterba
2016-06-03 15:42 ` [PATCH 15/21] btrfs: fix memory leak during RAID 5/6 device replacement David Sterba
2016-06-03 15:42 ` [PATCH 16/21] btrfs: pass the right error code to the btrfs_std_error David Sterba
2016-06-03 15:42 ` David Sterba [this message]
2016-06-03 15:42 ` [PATCH 18/21] Btrfs: fix empty symlink after creating symlink and fsync parent dir David Sterba
2016-06-03 15:42 ` [PATCH 19/21] Btrfs: fix unexpected return value of fiemap David Sterba
2016-06-03 15:42 ` [PATCH 20/21] btrfs: scrub: Set bbio to NULL before calling btrfs_map_block David Sterba
2016-06-03 15:42 ` [PATCH 21/21] btrfs: make state preallocation more speculative in __set_extent_bit David Sterba
2016-06-05 21:48 ` Btrfs stable patches for 4.5.x Greg KH

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=1464968564-6952-1-git-send-email-dsterba@suse.com \
    --to=dsterba@suse.com \
    --cc=stable@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox