Whamcloud - gitweb
LU-11446 e2fsck: check trusted.link when fixing nlink
authorLi Dongyang <dongyangli@ddn.com>
Tue, 30 Mar 2021 11:22:40 +0000 (22:22 +1100)
committerLi Dongyang <dongyangli@ddn.com>
Fri, 28 Apr 2023 01:21:35 +0000 (11:21 +1000)
The inode link count could be higher than what is stored in the
local MDT inode because of remote file links from DNE MDTs.
If we find a mismatched link count, look up the "trusted.link"
xattr.  If it exists, do a sanity check on it, and use the
leh_reccount stored there if larger than the local link count.

If leh_overflow_time is set, then the "trusted.link" xattr may
not hold all of the links, so assume the maximum of available
link counts is valid until LFSCK clears leh_overflow_time.

If the inode is not linked into the namespace, link it into
lost+found before checking trusted.link for the DNE link count.

Update the f_trusted_link test case to include a locally
unreferenced inode that has a trusted.link xattr.  The
inode should be linked into lost+found because of the xattr,
even if it has no blocks/data, and the link cound should be
extracted from the trusted.link xattr.

Signed-off-by: Li Dongyang <dongyangli@ddn.com>
Change-Id: I213d816a92043c348eb55374aaa98e98957ccf23
Reviewed-on: https://review.whamcloud.com/43169
Reviewed-by: Artem Blagodarenko <artem.blagodarenko@hpe.com>
Reviewed-by: Andreas Dilger <adilger@whamcloud.com>
e2fsck/pass4.c
tests/f_trusted_link/expect.1 [new file with mode: 0644]
tests/f_trusted_link/expect.2 [new file with mode: 0644]
tests/f_trusted_link/image.gz [new file with mode: 0644]
tests/f_trusted_link/name [new file with mode: 0644]

index d2dda02..60cc587 100644 (file)
@@ -17,6 +17,7 @@
 #include "config.h"
 #include "e2fsck.h"
 #include "problem.h"
+#include "ext2fs/lfsck.h"
 #include <ext2fs/ext2_ext_attr.h>
 
 /*
@@ -140,6 +141,73 @@ static void check_ea_inode(e2fsck_t ctx, ext2_ino_t i, ext2_ino_t *last_ino,
        }
 }
 
+static errcode_t check_link_ea(e2fsck_t ctx, ext2_ino_t ino,
+                              ext2_ino_t *last_ino,
+                              struct ext2_inode_large *inode,
+                              __u16 *link_counted)
+{
+       struct ext2_xattr_handle *handle;
+       struct link_ea_header *leh;
+       void *buf;
+       size_t ea_len;
+       errcode_t retval;
+
+       if (*last_ino != ino) {
+               e2fsck_read_inode_full(ctx, ino, EXT2_INODE(inode),
+                                      EXT2_INODE_SIZE(ctx->fs->super),
+                                      "pass4: get link ea count");
+               *last_ino = ino;
+       }
+
+       retval = ext2fs_xattrs_open(ctx->fs, ino, &handle);
+       if (retval)
+               return retval;
+
+       retval = ext2fs_xattrs_read_inode(handle, inode);
+       if (retval)
+               goto err;
+
+       retval = ext2fs_xattr_get(handle, EXT2_ATTR_INDEX_TRUSTED_PREFIX
+                                 LUSTRE_XATTR_MDT_LINK, &buf, &ea_len);
+       if (retval)
+               goto err;
+
+       leh = (struct link_ea_header *)buf;
+       if (leh->leh_magic == ext2fs_swab32(LINK_EA_MAGIC)) {
+               leh->leh_magic = LINK_EA_MAGIC;
+               leh->leh_reccount = ext2fs_swab32(leh->leh_reccount);
+               leh->leh_len = ext2fs_swab64(leh->leh_len);
+       }
+       if (leh->leh_magic != LINK_EA_MAGIC) {
+               retval = EINVAL;
+               goto err_free;
+       }
+       if (leh->leh_reccount == 0 && !leh->leh_overflow_time) {
+               retval = ENODATA;
+               goto err_free;
+       }
+       if (leh->leh_len > ea_len) {
+               retval = EINVAL;
+               goto err_free;
+       }
+
+       /* if linkEA overflowed and does not hold all links, assume *some*
+        * links exist until LFSCK is next run and resets leh_overflow_time */
+       if (leh->leh_overflow_time) {
+               if (inode->i_links_count > *link_counted)
+                       *link_counted = inode->i_links_count;
+               else if (*link_counted == 0)
+                       *link_counted = 1111;
+       }
+       if (leh->leh_reccount > *link_counted)
+               *link_counted = leh->leh_reccount;
+err_free:
+       ext2fs_free_mem(&buf);
+err:
+       ext2fs_xattrs_close(&handle);
+       return retval;
+}
+
 void e2fsck_pass4(e2fsck_t ctx)
 {
        ext2_filsys fs = ctx->fs;
@@ -232,6 +300,7 @@ void e2fsck_pass4(e2fsck_t ctx)
                                            &link_count);
                        ext2fs_icount_fetch(ctx->inode_count, i,
                                            &link_counted);
+                       check_link_ea(ctx, i, &last_ino, inode, &link_counted);
                }
                isdir = ext2fs_test_inode_bitmap2(ctx->inode_dir_map, i);
                if (isdir && (link_counted > EXT2_LINK_MAX)) {
@@ -243,6 +312,9 @@ void e2fsck_pass4(e2fsck_t ctx)
                        }
                        link_counted = 1;
                }
+               if (link_counted != link_count)
+                       check_link_ea(ctx, i, &last_ino, inode, &link_counted);
+
                if (link_counted != link_count) {
                        int fix_nlink = 0;
 
diff --git a/tests/f_trusted_link/expect.1 b/tests/f_trusted_link/expect.1
new file mode 100644 (file)
index 0000000..1063d22
--- /dev/null
@@ -0,0 +1,16 @@
+Pass 1: Checking inodes, blocks, and sizes
+Pass 2: Checking directory structure
+Pass 3: Checking directory connectivity
+Pass 4: Checking reference counts
+Inode 15 ref count is 7, should be 6.  Fix? yes
+
+Inode 16 ref count is 3, should be 6.  Fix? yes
+
+Unattached inode 18
+Connect to /lost+found? yes
+
+Pass 5: Checking group summary information
+
+test_filesys: ***** FILE SYSTEM WAS MODIFIED *****
+test_filesys: 18/4096 files (0.0% non-contiguous), 6424/16384 blocks
+Exit status is 1
diff --git a/tests/f_trusted_link/expect.2 b/tests/f_trusted_link/expect.2
new file mode 100644 (file)
index 0000000..a8ff12a
--- /dev/null
@@ -0,0 +1,7 @@
+Pass 1: Checking inodes, blocks, and sizes
+Pass 2: Checking directory structure
+Pass 3: Checking directory connectivity
+Pass 4: Checking reference counts
+Pass 5: Checking group summary information
+test_filesys: 18/4096 files (0.0% non-contiguous), 6424/16384 blocks
+Exit status is 0
diff --git a/tests/f_trusted_link/image.gz b/tests/f_trusted_link/image.gz
new file mode 100644 (file)
index 0000000..8e25873
Binary files /dev/null and b/tests/f_trusted_link/image.gz differ
diff --git a/tests/f_trusted_link/name b/tests/f_trusted_link/name
new file mode 100644 (file)
index 0000000..a6e424b
--- /dev/null
@@ -0,0 +1 @@
+inode nlink according to trusted.link xattr