Whamcloud - gitweb
LU-11446 e2fsck: check trusted.link when fixing nlink
[tools/e2fsprogs.git] / e2fsck / pass4.c
index 663f87a..f048fff 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>
 
 /*
@@ -26,7 +27,7 @@
  * This subroutine returns 1 then the caller shouldn't bother with the
  * rest of the pass 4 tests.
  */
-static int disconnect_inode(e2fsck_t ctx, ext2_ino_t i,
+static int disconnect_inode(e2fsck_t ctx, ext2_ino_t i, ext2_ino_t *last_ino,
                            struct ext2_inode_large *inode)
 {
        ext2_filsys fs = ctx->fs;
@@ -34,9 +35,12 @@ static int disconnect_inode(e2fsck_t ctx, ext2_ino_t i,
        __u32 eamagic = 0;
        int extra_size = 0;
 
-       e2fsck_read_inode_full(ctx, i, EXT2_INODE(inode),
-                              EXT2_INODE_SIZE(fs->super),
-                              "pass4: disconnect_inode");
+       if (*last_ino != i) {
+               e2fsck_read_inode_full(ctx, i, EXT2_INODE(inode),
+                                      EXT2_INODE_SIZE(fs->super),
+                                      "pass4: disconnect_inode");
+               *last_ino = i;
+       }
        if (EXT2_INODE_SIZE(fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
                extra_size = inode->i_extra_isize;
 
@@ -75,6 +79,7 @@ static int disconnect_inode(e2fsck_t ctx, ext2_ino_t i,
        if (fix_problem(ctx, PR_4_UNATTACHED_INODE, &pctx)) {
                if (e2fsck_reconnect_file(ctx, i))
                        ext2fs_unmark_valid(fs);
+               *last_ino = 0;
        } else {
                /*
                 * If we don't attach the inode, then skip the
@@ -88,51 +93,29 @@ static int disconnect_inode(e2fsck_t ctx, ext2_ino_t i,
 }
 
 /*
- * Get/set ref functions below could later be moved to somewhere in lib/ext2fs/.
- * Currently the only user is e2fsck so we rather not expose it in common
- * library until there are more users.
+ * This function is called when link_counted is zero. So this may not
+ * be an xattr inode at all. Return immediately if EA_INODE flag is not
+ * set.
  */
-static __u64 ea_inode_get_ref(struct ext2_inode_large *inode)
-{
-       return ((__u64)inode->i_ctime << 32) | inode->osd1.linux1.l_i_version;
-}
-
-static void ea_inode_set_ref(struct ext2_inode_large *inode, __u64 ref_count)
-{
-       inode->i_ctime = (__u32)(ref_count >> 32);
-       inode->osd1.linux1.l_i_version = (__u32)ref_count;
-}
-
-static void check_ea_inode(e2fsck_t ctx, ext2_ino_t i,
+static void check_ea_inode(e2fsck_t ctx, ext2_ino_t i, ext2_ino_t *last_ino,
                           struct ext2_inode_large *inode, __u16 *link_counted)
 {
        __u64 actual_refs = 0;
        __u64 ref_count;
 
-       /*
-        * This function is called when link_counted is zero. So this may not
-        * be an xattr inode at all. Return immediately if EA_INODE flag is not
-        * set.
-        */
-       e2fsck_read_inode_full(ctx, i, EXT2_INODE(inode),
-                              EXT2_INODE_SIZE(ctx->fs->super),
-                              "pass4: check_ea_inode");
+       if (*last_ino != i) {
+               e2fsck_read_inode_full(ctx, i, EXT2_INODE(inode),
+                                      EXT2_INODE_SIZE(ctx->fs->super),
+                                      "pass4: check_ea_inode");
+               *last_ino = i;
+       }
        if (!(inode->i_flags & EXT4_EA_INODE_FL))
                return;
 
        if (ctx->ea_inode_refs)
                ea_refcount_fetch(ctx->ea_inode_refs, i, &actual_refs);
-       if (!actual_refs) {
-               /*
-                * There are no attribute references to the ea_inode.
-                * Zero the link count so that when  inode is linked to
-                * lost+found it has correct link count.
-                */
-               inode->i_links_count = 0;
-               e2fsck_write_inode(ctx, i, EXT2_INODE(inode), "check_ea_inode");
-               ext2fs_icount_store(ctx->inode_link_info, i, 0);
+       if (!actual_refs)
                return;
-       }
 
        /*
         * There are some attribute references, link_counted is now considered
@@ -140,7 +123,7 @@ static void check_ea_inode(e2fsck_t ctx, ext2_ino_t i,
         */
        *link_counted = 1;
 
-       ref_count = ea_inode_get_ref(inode);
+       ref_count = ext2fs_get_ea_inode_ref(EXT2_INODE(inode));
 
        /* Old Lustre-style xattr inodes do not have a stored refcount.
         * However, their i_ctime and i_atime should be the same.
@@ -153,12 +136,79 @@ static void check_ea_inode(e2fsck_t ctx, ext2_ino_t i,
                pctx.num = ref_count;
                pctx.num2 = actual_refs;
                if (fix_problem(ctx, PR_4_EA_INODE_REF_COUNT, &pctx)) {
-                       ea_inode_set_ref(inode, actual_refs);
+                       ext2fs_set_ea_inode_ref(EXT2_INODE(inode), actual_refs);
                        e2fsck_write_inode(ctx, i, EXT2_INODE(inode), "pass4");
                }
        }
 }
 
+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;
@@ -170,6 +220,7 @@ void e2fsck_pass4(e2fsck_t ctx)
 #endif
        struct problem_context  pctx;
        __u16   link_count, link_counted;
+       int dir_nlink_fs;
        char    *buf = 0;
        dgrp_t  group, maxgroup;
 
@@ -193,6 +244,8 @@ void e2fsck_pass4(e2fsck_t ctx)
        if (!(ctx->options & E2F_OPT_PREEN))
                fix_problem(ctx, PR_4_PASS_HEADER, &pctx);
 
+       dir_nlink_fs = ext2fs_has_feature_dir_nlink(fs->super);
+
        group = 0;
        maxgroup = fs->group_desc_count;
        if (ctx->progress)
@@ -202,7 +255,8 @@ void e2fsck_pass4(e2fsck_t ctx)
        inode = e2fsck_allocate_memory(ctx, inode_size, "scratch inode");
 
        /* Protect loop from wrap-around if s_inodes_count maxed */
-       for (i=1; i <= fs->super->s_inodes_count && i > 0; i++) {
+       for (i = 1; i <= fs->super->s_inodes_count && i > 0; i++) {
+               ext2_ino_t last_ino = 0;
                int isdir;
 
                if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
@@ -232,7 +286,7 @@ void e2fsck_pass4(e2fsck_t ctx)
                         * check_ea_inode() will update link_counted if
                         * necessary.
                         */
-                       check_ea_inode(ctx, i, inode, &link_counted);
+                       check_ea_inode(ctx, i, &last_ino, inode, &link_counted);
                }
 
                if (link_counted == 0) {
@@ -241,19 +295,36 @@ void e2fsck_pass4(e2fsck_t ctx)
                                     fs->blocksize, "bad_inode buffer");
                        if (e2fsck_process_bad_inode(ctx, 0, i, buf))
                                continue;
-                       if (disconnect_inode(ctx, i, inode))
+                       if (disconnect_inode(ctx, i, &last_ino, inode))
                                continue;
                        ext2fs_icount_fetch(ctx->inode_link_info, i,
                                            &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))
+               if (isdir && (link_counted > EXT2_LINK_MAX)) {
+                       if (!dir_nlink_fs &&
+                           fix_problem(ctx, PR_4_DIR_NLINK_FEATURE, &pctx)) {
+                               ext2fs_set_feature_dir_nlink(fs->super);
+                               ext2fs_mark_super_dirty(fs);
+                               dir_nlink_fs = 1;
+                       }
                        link_counted = 1;
+               }
+               if (link_counted != link_count)
+                       check_link_ea(ctx, i, &last_ino, inode, &link_counted);
+
                if (link_counted != link_count) {
-                       e2fsck_read_inode_full(ctx, i, EXT2_INODE(inode),
-                                              inode_size, "pass4");
+                       int fix_nlink = 0;
+
+                       if (last_ino != i) {
+                               e2fsck_read_inode_full(ctx, i,
+                                                      EXT2_INODE(inode),
+                                                      inode_size, "pass4");
+                               last_ino = i;
+                       }
                        pctx.ino = i;
                        pctx.inode = EXT2_INODE(inode);
                        if ((link_count != inode->i_links_count) && !isdir &&
@@ -265,10 +336,20 @@ void e2fsck_pass4(e2fsck_t ctx)
                        pctx.num = link_counted;
                        /* i_link_count was previously exceeded, but no longer
                         * is, fix this but don't consider it an error */
-                       if ((isdir && link_counted > 1 &&
-                            (inode->i_flags & EXT2_INDEX_FL) &&
-                            link_count == 1 && !(ctx->options & E2F_OPT_NO)) ||
-                           fix_problem(ctx, PR_4_BAD_REF_COUNT, &pctx)) {
+                       if (isdir && link_counted > 1 &&
+                           (inode->i_flags & EXT2_INDEX_FL) &&
+                           link_count == 1) {
+                               if ((ctx->options & E2F_OPT_READONLY) == 0) {
+                                       fix_nlink =
+                                               fix_problem(ctx,
+                                                       PR_4_DIR_OVERFLOW_REF_COUNT,
+                                                       &pctx);
+                               }
+                       } else {
+                               fix_nlink = fix_problem(ctx, PR_4_BAD_REF_COUNT,
+                                               &pctx);
+                       }
+                       if (fix_nlink) {
                                inode->i_links_count = link_counted;
                                e2fsck_write_inode_full(ctx, i,
                                                        EXT2_INODE(inode),