+
+/*
+ * This function releases an inode. Returns 1 if an inconsistency was
+ * found. If the inode has a link count, then it is being truncated and
+ * not deleted.
+ */
+static int release_inode_blocks(e2fsck_t ctx, ext2_ino_t ino,
+ struct ext2_inode_large *inode, char *block_buf,
+ struct problem_context *pctx)
+{
+ struct process_block_struct pb;
+ ext2_filsys fs = ctx->fs;
+ blk64_t blk;
+ errcode_t retval;
+ __u32 count;
+
+ if (!ext2fs_inode_has_valid_blocks2(fs, EXT2_INODE(inode)))
+ return 0;
+
+ pb.buf = block_buf + 3 * ctx->fs->blocksize;
+ pb.ctx = ctx;
+ pb.abort = 0;
+ pb.errcode = 0;
+ pb.pctx = pctx;
+ pb.last_cluster = 0;
+ pb.inode = inode;
+ if (inode->i_links_count) {
+ pb.truncating = 1;
+ pb.truncate_block = (e2_blkcnt_t)
+ ((EXT2_I_SIZE(inode) + fs->blocksize - 1) /
+ fs->blocksize);
+ pb.truncate_offset = inode->i_size % fs->blocksize;
+ } else {
+ pb.truncating = 0;
+ pb.truncate_block = 0;
+ pb.truncate_offset = 0;
+ }
+ pb.truncated_blocks = 0;
+ retval = ext2fs_block_iterate3(fs, ino, BLOCK_FLAG_DEPTH_TRAVERSE,
+ block_buf, release_inode_block, &pb);
+ if (retval) {
+ com_err("release_inode_blocks", retval,
+ _("while calling ext2fs_block_iterate for inode %u"),
+ ino);
+ return 1;
+ }
+ if (pb.abort)
+ return 1;
+
+ /* Refresh the inode since ext2fs_block_iterate may have changed it */
+ e2fsck_read_inode_full(ctx, ino, EXT2_INODE(inode), sizeof(*inode),
+ "release_inode_blocks");
+
+ if (pb.truncated_blocks)
+ ext2fs_iblk_sub_blocks(fs, EXT2_INODE(inode),
+ pb.truncated_blocks);
+
+ blk = ext2fs_file_acl_block(fs, EXT2_INODE(inode));
+ if (blk) {
+ retval = ext2fs_adjust_ea_refcount3(fs, blk, block_buf, -1,
+ &count, ino);
+ if (retval == EXT2_ET_BAD_EA_BLOCK_NUM) {
+ retval = 0;
+ count = 1;
+ }
+ if (retval) {
+ com_err("release_inode_blocks", retval,
+ _("while calling ext2fs_adjust_ea_refcount2 for inode %u"),
+ ino);
+ return 1;
+ }
+ if (count == 0) {
+ if (ctx->qctx)
+ quota_data_sub(ctx->qctx, inode, 0,
+ ctx->fs->blocksize);
+ ext2fs_block_alloc_stats2(fs, blk, -1);
+ ctx->free_blocks++;
+ }
+ ext2fs_file_acl_block_set(fs, EXT2_INODE(inode), 0);
+ }
+ return 0;
+}
+
+/* Load all quota data in preparation for orphan clearing. */
+static errcode_t e2fsck_read_all_quotas(e2fsck_t ctx)
+{
+ ext2_ino_t qf_ino;
+ enum quota_type qtype;
+ errcode_t retval = 0;
+
+ if (!ext2fs_has_feature_quota(ctx->fs->super))
+ return retval;
+
+ retval = quota_init_context(&ctx->qctx, ctx->fs, 0);
+ if (retval)
+ return retval;
+
+ for (qtype = 0 ; qtype < MAXQUOTAS; qtype++) {
+ qf_ino = *quota_sb_inump(ctx->fs->super, qtype);
+ if (qf_ino == 0)
+ continue;
+
+ retval = quota_read_all_dquots(ctx->qctx, qf_ino, qtype,
+ QREAD_USAGE | QREAD_LIMITS);
+ if (retval)
+ break;
+ }
+ if (retval)
+ quota_release_context(&ctx->qctx);
+ return retval;
+}
+
+/* Write all the quota info to disk. */
+static errcode_t e2fsck_write_all_quotas(e2fsck_t ctx)
+{
+ struct problem_context pctx;
+ enum quota_type qtype;
+
+ if (!ext2fs_has_feature_quota(ctx->fs->super))
+ return 0;
+
+ clear_problem_context(&pctx);
+ for (qtype = 0 ; qtype < MAXQUOTAS; qtype++) {
+ pctx.num = qtype;
+ pctx.errcode = quota_write_inode(ctx->qctx, 1 << qtype);
+ if (pctx.errcode) {
+ fix_problem(ctx, PR_6_WRITE_QUOTAS, &pctx);
+ break;
+ }
+ }
+
+ quota_release_context(&ctx->qctx);
+ return pctx.errcode;
+}
+
+/*
+ * This function releases all of the orphan inodes. It returns 1 if
+ * it hit some error, and 0 on success.
+ */
+static int release_orphan_inodes(e2fsck_t ctx)
+{
+ ext2_filsys fs = ctx->fs;
+ ext2_ino_t ino, next_ino;
+ struct ext2_inode_large inode;
+ struct problem_context pctx;
+ char *block_buf;
+
+ if ((ino = fs->super->s_last_orphan) == 0)
+ return 0;
+
+ clear_problem_context(&pctx);
+ pctx.errcode = e2fsck_read_all_quotas(ctx);
+ if (pctx.errcode) {
+ fix_problem(ctx, PR_0_QUOTA_INIT_CTX, &pctx);
+ return 1;
+ }
+
+ /*
+ * Win or lose, we won't be using the head of the orphan inode
+ * list again.
+ */
+ fs->super->s_last_orphan = 0;
+ ext2fs_mark_super_dirty(fs);
+
+ /*
+ * If the filesystem contains errors, don't run the orphan
+ * list, since the orphan list can't be trusted; and we're
+ * going to be running a full e2fsck run anyway...
+ */
+ if (fs->super->s_state & EXT2_ERROR_FS) {
+ if (ctx->qctx)
+ quota_release_context(&ctx->qctx);
+ return 0;
+ }
+
+ if ((ino < EXT2_FIRST_INODE(fs->super)) ||
+ (ino > fs->super->s_inodes_count)) {
+ clear_problem_context(&pctx);
+ pctx.ino = ino;
+ fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_HEAD_INODE, &pctx);
+ goto err_qctx;
+ }
+
+ block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 4,
+ "block iterate buffer");
+ e2fsck_read_bitmaps(ctx);
+
+ while (ino) {
+ e2fsck_read_inode_full(ctx, ino, EXT2_INODE(&inode),
+ sizeof(inode), "release_orphan_inodes");
+ clear_problem_context(&pctx);
+ pctx.ino = ino;
+ pctx.inode = EXT2_INODE(&inode);
+ pctx.str = inode.i_links_count ? _("Truncating") :
+ _("Clearing");
+
+ fix_problem(ctx, PR_0_ORPHAN_CLEAR_INODE, &pctx);
+
+ next_ino = inode.i_dtime;
+ if (next_ino &&
+ ((next_ino < EXT2_FIRST_INODE(fs->super)) ||
+ (next_ino > fs->super->s_inodes_count))) {
+ pctx.ino = next_ino;
+ fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_INODE, &pctx);
+ goto err_buf;
+ }
+
+ if (release_inode_blocks(ctx, ino, &inode, block_buf, &pctx))
+ goto err_buf;
+
+ if (!inode.i_links_count) {
+ if (ctx->qctx)
+ quota_data_inodes(ctx->qctx, &inode, ino, -1);
+ ext2fs_inode_alloc_stats2(fs, ino, -1,
+ LINUX_S_ISDIR(inode.i_mode));
+ ctx->free_inodes++;
+ inode.i_dtime = ctx->now;
+ } else {
+ inode.i_dtime = 0;
+ }
+ e2fsck_write_inode_full(ctx, ino, EXT2_INODE(&inode),
+ sizeof(inode), "delete_file");
+ ino = next_ino;
+ }
+ ext2fs_free_mem(&block_buf);
+ pctx.errcode = e2fsck_write_all_quotas(ctx);
+ if (pctx.errcode)
+ goto err;
+ return 0;
+err_buf:
+ ext2fs_free_mem(&block_buf);
+err_qctx:
+ if (ctx->qctx)
+ quota_release_context(&ctx->qctx);
+err:
+ return 1;
+}
+
+/*
+ * Check the resize inode to make sure it is sane. We check both for
+ * the case where on-line resizing is not enabled (in which case the
+ * resize inode should be cleared) as well as the case where on-line
+ * resizing is enabled.
+ */
+void check_resize_inode(e2fsck_t ctx)
+{
+ ext2_filsys fs = ctx->fs;
+ struct ext2_inode inode;
+ struct problem_context pctx;
+ int i, gdt_off, ind_off;
+ dgrp_t j;
+ blk_t blk, pblk;
+ blk_t expect; /* for resize inode, which is 32-bit only */
+ __u32 *dind_buf = 0, *ind_buf;
+ errcode_t retval;
+
+ clear_problem_context(&pctx);
+
+ if (ext2fs_has_feature_resize_inode(fs->super) &&
+ ext2fs_has_feature_meta_bg(fs->super) &&
+ fix_problem(ctx, PR_0_DISABLE_RESIZE_INODE, &pctx)) {
+ ext2fs_clear_feature_resize_inode(fs->super);
+ fs->super->s_reserved_gdt_blocks = 0;
+ ext2fs_mark_super_dirty(fs);
+ }
+
+ /*
+ * If the resize inode feature isn't set, then
+ * s_reserved_gdt_blocks must be zero.
+ */
+ if (!ext2fs_has_feature_resize_inode(fs->super)) {
+ if (fs->super->s_reserved_gdt_blocks) {
+ pctx.num = fs->super->s_reserved_gdt_blocks;
+ if (fix_problem(ctx, PR_0_NONZERO_RESERVED_GDT_BLOCKS,
+ &pctx)) {
+ fs->super->s_reserved_gdt_blocks = 0;
+ ext2fs_mark_super_dirty(fs);
+ }
+ }
+ }
+
+ /* Read the resize inode */
+ pctx.ino = EXT2_RESIZE_INO;
+ retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
+ if (retval) {
+ if (ext2fs_has_feature_resize_inode(fs->super))
+ ctx->flags |= E2F_FLAG_RESIZE_INODE;
+ return;
+ }
+
+ /*
+ * If the resize inode feature isn't set, check to make sure
+ * the resize inode is cleared; then we're done.
+ */
+ if (!ext2fs_has_feature_resize_inode(fs->super)) {
+ for (i=0; i < EXT2_N_BLOCKS; i++) {
+ if (inode.i_block[i])
+ break;
+ }
+ if ((i < EXT2_N_BLOCKS) &&
+ fix_problem(ctx, PR_0_CLEAR_RESIZE_INODE, &pctx)) {
+ memset(&inode, 0, sizeof(inode));
+ e2fsck_write_inode(ctx, EXT2_RESIZE_INO, &inode,
+ "clear_resize");
+ }
+ return;
+ }
+
+ /*
+ * The resize inode feature is enabled; check to make sure the
+ * only block in use is the double indirect block
+ */
+ blk = inode.i_block[EXT2_DIND_BLOCK];
+ for (i=0; i < EXT2_N_BLOCKS; i++) {
+ if (i != EXT2_DIND_BLOCK && inode.i_block[i])
+ break;
+ }
+ if ((i < EXT2_N_BLOCKS) || !blk || !inode.i_links_count ||
+ !(inode.i_mode & LINUX_S_IFREG) ||
+ (blk < fs->super->s_first_data_block ||
+ blk >= ext2fs_blocks_count(fs->super))) {
+ resize_inode_invalid:
+ if (fix_problem(ctx, PR_0_RESIZE_INODE_INVALID, &pctx)) {
+ memset(&inode, 0, sizeof(inode));
+ e2fsck_write_inode(ctx, EXT2_RESIZE_INO, &inode,
+ "clear_resize");
+ ctx->flags |= E2F_FLAG_RESIZE_INODE;
+ }
+ if (!(ctx->options & E2F_OPT_READONLY)) {
+ fs->super->s_state &= ~EXT2_VALID_FS;
+ ext2fs_mark_super_dirty(fs);
+ }
+ goto cleanup;
+ }
+ dind_buf = (__u32 *) e2fsck_allocate_memory(ctx, fs->blocksize * 2,
+ "resize dind buffer");
+ ind_buf = (__u32 *) ((char *) dind_buf + fs->blocksize);
+
+ retval = ext2fs_read_ind_block(fs, blk, dind_buf);
+ if (retval)
+ goto resize_inode_invalid;
+
+ gdt_off = fs->desc_blocks;
+ pblk = fs->super->s_first_data_block + 1 + fs->desc_blocks;
+ if (fs->blocksize == 1024 && fs->super->s_first_data_block == 0)
+ pblk++; /* Deal with 1024 blocksize bigalloc fs */
+ for (i = 0; i < fs->super->s_reserved_gdt_blocks / 4;
+ i++, gdt_off++, pblk++) {
+ gdt_off %= fs->blocksize/4;
+ if (dind_buf[gdt_off] != pblk)
+ goto resize_inode_invalid;
+ retval = ext2fs_read_ind_block(fs, pblk, ind_buf);
+ if (retval)
+ goto resize_inode_invalid;
+ ind_off = 0;
+ for (j = 1; j < fs->group_desc_count; j++) {
+ if (!ext2fs_bg_has_super(fs, j))
+ continue;
+ expect = pblk + EXT2_GROUPS_TO_BLOCKS(fs->super, j);
+ if (ind_buf[ind_off] != expect)
+ goto resize_inode_invalid;
+ ind_off++;
+ }
+ }
+
+cleanup:
+ if (dind_buf)
+ ext2fs_free_mem(&dind_buf);
+
+ }
+
+/*
+ * This function checks the dirhash signed/unsigned hint if necessary.
+ */
+static void e2fsck_fix_dirhash_hint(e2fsck_t ctx)
+{
+ struct ext2_super_block *sb = ctx->fs->super;
+ struct problem_context pctx;
+ char c;
+
+ if ((ctx->options & E2F_OPT_READONLY) ||
+ !ext2fs_has_feature_dir_index(sb) ||
+ (sb->s_flags & (EXT2_FLAGS_SIGNED_HASH|EXT2_FLAGS_UNSIGNED_HASH)))
+ return;
+
+ c = (char) 255;
+
+ clear_problem_context(&pctx);
+ if (fix_problem(ctx, PR_0_DIRHASH_HINT, &pctx)) {
+ if (((int) c) == -1) {
+ sb->s_flags |= EXT2_FLAGS_SIGNED_HASH;
+ } else {
+ sb->s_flags |= EXT2_FLAGS_UNSIGNED_HASH;
+ }
+ ext2fs_mark_super_dirty(ctx->fs);
+ }
+}
+