Whamcloud - gitweb
libext2fs: return an error when byte swapping a corrupted dirblock block
[tools/e2fsprogs.git] / e2fsck / pass4.c
index e44fc69..8c2d2f1 100644 (file)
@@ -11,7 +11,7 @@
  * Pass 4 frees the following data structures:
  *     - A bitmap of which inodes are in bad blocks.   (inode_bb_map)
  *     - A bitmap of which inodes are imagic inodes.   (inode_imagic_map)
- *     - A bitmap of EA inodes.                        (inode_ea_map)
+ *     - Ref counts for ea_inodes.                     (ea_inode_refs)
  */
 
 #include "config.h"
@@ -40,20 +40,6 @@ static int disconnect_inode(e2fsck_t ctx, ext2_ino_t i,
        if (EXT2_INODE_SIZE(fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
                extra_size = inode->i_extra_isize;
 
-       if (inode->i_flags & EXT4_EA_INODE_FL) {
-               if (ext2fs_test_inode_bitmap2(ctx->inode_ea_map, i)) {
-                       ext2fs_icount_store(ctx->inode_count, i, 1);
-                       return 0;
-               } else {
-                       /* 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),
-                                          "disconnect_inode");
-                       ext2fs_icount_store(ctx->inode_link_info, i, 0);
-               }
-       }
-
        clear_problem_context(&pctx);
        pctx.ino = i;
        pctx.inode = EXT2_INODE(inode);
@@ -101,6 +87,52 @@ static int disconnect_inode(e2fsck_t ctx, ext2_ino_t i,
        return 0;
 }
 
+static void check_ea_inode(e2fsck_t ctx, ext2_ino_t i,
+                          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 (!(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)
+               return;
+
+       /*
+        * There are some attribute references, link_counted is now considered
+        * to be 1.
+        */
+       *link_counted = 1;
+
+       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.
+        */
+       if (ref_count != actual_refs && inode->i_ctime != inode->i_atime) {
+               struct problem_context pctx;
+
+               clear_problem_context(&pctx);
+               pctx.ino = i;
+               pctx.num = ref_count;
+               pctx.num2 = actual_refs;
+               if (fix_problem(ctx, PR_4_EA_INODE_REF_COUNT, &pctx)) {
+                       ext2fs_set_ea_inode_ref(EXT2_INODE(inode), actual_refs);
+                       e2fsck_write_inode(ctx, i, EXT2_INODE(inode), "pass4");
+               }
+       }
+}
 
 void e2fsck_pass4(e2fsck_t ctx)
 {
@@ -113,6 +145,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;
 
@@ -136,6 +169,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)
@@ -168,6 +203,16 @@ void e2fsck_pass4(e2fsck_t ctx)
                        continue;
                ext2fs_icount_fetch(ctx->inode_link_info, i, &link_count);
                ext2fs_icount_fetch(ctx->inode_count, i, &link_counted);
+
+               if (link_counted == 0) {
+                       /*
+                        * link_counted is expected to be 0 for an ea_inode.
+                        * check_ea_inode() will update link_counted if
+                        * necessary.
+                        */
+                       check_ea_inode(ctx, i, inode, &link_counted);
+               }
+
                if (link_counted == 0) {
                        if (!buf)
                                buf = e2fsck_allocate_memory(ctx,
@@ -182,9 +227,18 @@ void e2fsck_pass4(e2fsck_t ctx)
                                            &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) {
+                       int fix_nlink = 0;
+
                        e2fsck_read_inode_full(ctx, i, EXT2_INODE(inode),
                                               inode_size, "pass4");
                        pctx.ino = i;
@@ -198,10 +252,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),
@@ -211,10 +275,10 @@ void e2fsck_pass4(e2fsck_t ctx)
        }
        ext2fs_free_icount(ctx->inode_link_info); ctx->inode_link_info = 0;
        ext2fs_free_icount(ctx->inode_count); ctx->inode_count = 0;
-       ext2fs_free_inode_bitmap(ctx->inode_ea_map);
-       ctx->inode_ea_map = 0;
        ext2fs_free_inode_bitmap(ctx->inode_bb_map);
        ctx->inode_bb_map = 0;
+       ea_refcount_free(ctx->ea_inode_refs);
+       ctx->ea_inode_refs = 0;
        ext2fs_free_inode_bitmap(ctx->inode_imagic_map);
        ctx->inode_imagic_map = 0;
 errout: