Whamcloud - gitweb
libext2fs: return an error when byte swapping a corrupted dirblock block
[tools/e2fsprogs.git] / e2fsck / pass4.c
index 9a491b1..8c2d2f1 100644 (file)
@@ -145,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;
 
@@ -168,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)
@@ -224,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;
@@ -240,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),