alistair23-linux/fs/9p
Eryu Guan ac89b2ef9b 9p: don't maintain dir i_nlink if the exported fs doesn't either
If the exported filesystem dir on 9p server doesn't maintain accurate
i_nlink count, e.g.  always reports i_nlink as 1, then 9p should not
maintain nlink count either, otherwise drop_link would report warning
with i_nlink being zero.

For example:

 - overlayfs sets nlink to 1 for merged dir

 - ext4 (with dir_nlink feature enabled) sets nlink to 1 if a dir has
   more than EXT4_LINK_MAX (65000) links.

In this case, everytime a stat(2) call (getattr) on such exported dirs
on 9p client side, the i_nlink gets reset to 1, then operations like
rmdir(2), unlink(2) and rename(2) would cause the dir nlink to go to
zero (then negative), which results in warnings in drop_nlink() and/or
inc_nlink() calls.

This can be reproduced easily as the following steps:

 - export a merged overlayfs dir via qemu virtfs to guest

 - mount the exported virtfs in guest

 - create two sub-directories in the root dir of the mounted 9pfs

 - stat the root dir of 9pfs, this resets nlink to 1

 - remove all subdirs, the second unlink/rmdir would trigger warning

  ------------[ cut here ]------------
  WARNING: CPU: 3 PID: 1284 at fs/inode.c:282 drop_nlink+0x3e/0x50
  ...
  Call Trace:
    dump_stack+0x63/0x81
    __warn+0xcb/0xf0
    warn_slowpath_null+0x1d/0x20
    drop_nlink+0x3e/0x50
    v9fs_remove+0xaa/0x130 [9p]
    v9fs_vfs_rmdir+0x13/0x20 [9p]
    vfs_rmdir+0xb7/0x130
    do_rmdir+0x1b8/0x230
    SyS_unlinkat+0x22/0x30
    do_syscall_64+0x67/0x180
  ---[ end trace 43758d8ba91e603b ]---

Fix it by leaving i_nlink to be 1 and don't drop nlink if a directory
has nlink <= 2, which indicates that the underlying exported fs doesn't
maintain nlink count accurately.  This follows what ext4 does in
ext4_dec_count().

Link: http://lkml.kernel.org/r/20180312053829.4367-1-eguan@linux.alibaba.com
Signed-off-by: Eryu Guan <eguan@linux.alibaba.com>
Reviewed-by: Yiwen Jiang <jiangyiwen@huawei.com>
Tested-by: Roman Kapl <code@rkapl.cz>
Cc: Caspar Zhang <caspar@linux.alibaba.com>
Cc: Eric Van Hensbergen <ericvh@gmail.com>
Cc: Ron Minnich <rminnich@sandia.gov>
Cc: Latchesar Ionkov <lucho@ionkov.net>
Cc: <v9fs-developer@lists.sourceforge.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-04-05 21:36:22 -07:00
..
acl.c 9p: fix a potential acl leak 2017-02-22 16:41:27 -08:00
acl.h 9p: switch v9fs_set_create_acl() to inode+fid, do it before d_instantiate() 2013-02-26 02:46:07 -05:00
cache.c fscache: remove unused ->now_uncached callback 2017-09-06 17:27:26 -07:00
cache.h fs: 9p: cache.h: Add #define of include guard 2015-11-11 02:19:50 -05:00
fid.c 9p: constify ->d_name handling 2017-01-12 04:01:17 -05:00
fid.h 9p: use clone_fid() 2016-08-03 11:12:12 -04:00
Kconfig fs/*/Kconfig: drop links to 404-compliant http://acl.bestbits.at 2018-01-01 12:45:37 -07:00
Makefile License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
v9fs.c 9p: Implement show_options 2017-07-11 06:08:58 -04:00
v9fs.h 9p: Implement show_options 2017-07-11 06:08:58 -04:00
v9fs_vfs.h 9p: switch p9_client_read() to passing struct iov_iter * 2015-04-11 22:28:27 -04:00
vfs_addr.c 9p: set page uptodate when required in write_end() 2017-10-11 09:30:08 -07:00
vfs_dentry.c VFS: normal filesystems (and lustre): d_inode() annotations 2015-04-15 15:06:57 -04:00
vfs_dir.c 9p: switch to ->iterate_shared() 2016-05-09 11:41:16 -04:00
vfs_file.c Writeback error handling fixes for v4.14 2017-09-06 14:11:03 -07:00
vfs_inode.c 9p: don't maintain dir i_nlink if the exported fs doesn't either 2018-04-05 21:36:22 -07:00
vfs_inode_dotl.c fs/9p: Compare qid.path in v9fs_test_inode 2017-10-23 23:10:01 -04:00
vfs_super.c Rename superblock flags (MS_xyz -> SB_xyz) 2017-11-27 13:05:09 -08:00
xattr.c 9p: use clone_fid() 2016-08-03 11:12:12 -04:00
xattr.h 9p: xattr simplifications 2015-11-13 20:34:33 -05:00