#include "e2fsck.h"
#include "problem.h"
-#include "dict.h"
+#include "support/dict.h"
/* Define an extension to the ext2 library's block count information */
#define BLOCK_COUNT_EXTATTR (-5)
struct dup_inode {
ext2_ino_t dir;
int num_dupblocks;
- struct ext2_inode inode;
+ struct ext2_inode_large inode;
struct cluster_el *cluster_list;
};
* Add a duplicate block record
*/
static void add_dupe(e2fsck_t ctx, ext2_ino_t ino, blk64_t cluster,
- struct ext2_inode *inode)
+ struct ext2_inode_large *inode)
{
dnode_t *n;
struct dup_cluster *db;
pass1d(ctx, block_buf);
print_resource_track(ctx, "Pass 1d", &rtrack, ctx->fs->io);
+ if (ext2fs_has_feature_shared_blocks(ctx->fs->super) &&
+ (ctx->options & E2F_OPT_UNSHARE_BLOCKS)) {
+ /*
+ * If we successfully managed to unshare all blocks, unset the
+ * shared block feature.
+ */
+ blk64_t next;
+ int result = ext2fs_find_first_set_block_bitmap2(
+ ctx->block_dup_map,
+ ctx->fs->super->s_first_data_block,
+ ext2fs_blocks_count(ctx->fs->super) - 1,
+ &next);
+ if (result == ENOENT && !(ctx->options & E2F_OPT_NO)) {
+ ext2fs_clear_feature_shared_blocks(ctx->fs->super);
+ ext2fs_mark_super_dirty(ctx->fs);
+ }
+ }
+
/*
* Time to free all of the accumulated data structures that we
* don't need anymore.
e2fsck_t ctx;
ext2_ino_t ino;
int dup_blocks;
- blk64_t cur_cluster;
- struct ext2_inode *inode;
+ blk64_t cur_cluster, phys_cluster;
+ blk64_t last_blk;
+ struct ext2_inode_large *inode;
struct problem_context *pctx;
};
{
ext2_filsys fs = ctx->fs;
ext2_ino_t ino = 0;
- struct ext2_inode inode;
+ struct ext2_inode_large inode;
ext2_inode_scan scan;
struct process_block_struct pb;
struct problem_context pctx;
+ problem_t op;
clear_problem_context(&pctx);
ctx->flags |= E2F_FLAG_ABORT;
return;
}
- ctx->stashed_inode = &inode;
+ ctx->stashed_inode = EXT2_INODE(&inode);
pb.ctx = ctx;
pb.pctx = &pctx;
pctx.str = "pass1b";
if (e2fsck_mmp_update(fs))
fatal_error(ctx, 0);
}
- pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
+ pctx.errcode = ext2fs_get_next_inode_full(scan, &ino,
+ EXT2_INODE(&inode), sizeof(inode));
if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)
continue;
if (pctx.errcode) {
+ pctx.ino = ino;
fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
ctx->flags |= E2F_FLAG_ABORT;
return;
pb.dup_blocks = 0;
pb.inode = &inode;
pb.cur_cluster = ~0;
+ pb.phys_cluster = ~0;
+ pb.last_blk = 0;
+ pb.pctx->blk = pb.pctx->blk2 = 0;
- if (ext2fs_inode_has_valid_blocks2(fs, &inode) ||
+ if (ext2fs_inode_has_valid_blocks2(fs, EXT2_INODE(&inode)) ||
(ino == EXT2_BAD_INO))
pctx.errcode = ext2fs_block_iterate3(fs, ino,
BLOCK_FLAG_READ_ONLY, block_buf,
process_pass1b_block, &pb);
/* If the feature is not set, attrs will be cleared later anyway */
- if ((fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR) &&
- ext2fs_file_acl_block(fs, &inode)) {
- blk64_t blk = ext2fs_file_acl_block(fs, &inode);
+ if (ext2fs_has_feature_xattr(fs->super) &&
+ ext2fs_file_acl_block(fs, EXT2_INODE(&inode))) {
+ blk64_t blk = ext2fs_file_acl_block(fs, EXT2_INODE(&inode));
process_pass1b_block(fs, &blk,
BLOCK_COUNT_EXTATTR, 0, 0, &pb);
- ext2fs_file_acl_block_set(fs, &inode, blk);
+ ext2fs_file_acl_block_set(fs, EXT2_INODE(&inode), blk);
}
if (pb.dup_blocks) {
+ if (ino != EXT2_BAD_INO) {
+ op = pctx.blk == pctx.blk2 ?
+ PR_1B_DUP_BLOCK : PR_1B_DUP_RANGE;
+ fix_problem(ctx, op, pb.pctx);
+ }
end_problem_latch(ctx, PR_LATCH_DBLOCK);
if (ino >= EXT2_FIRST_INODE(fs->super) ||
ino == EXT2_ROOT_INO)
{
struct process_block_struct *p;
e2fsck_t ctx;
- blk64_t lc;
+ blk64_t lc, pc;
+ problem_t op;
- if (HOLE_BLKADDR(*block_nr))
+ if (*block_nr == 0)
return 0;
p = (struct process_block_struct *) priv_data;
ctx = p->ctx;
lc = EXT2FS_B2C(fs, blockcnt);
+ pc = EXT2FS_B2C(fs, *block_nr);
if (!ext2fs_test_block_bitmap2(ctx->block_dup_map, *block_nr))
goto finish;
/* OK, this is a duplicate block */
if (p->ino != EXT2_BAD_INO) {
- p->pctx->blk = *block_nr;
- fix_problem(ctx, PR_1B_DUP_BLOCK, p->pctx);
+ if (p->last_blk + 1 != *block_nr) {
+ if (p->last_blk) {
+ op = p->pctx->blk == p->pctx->blk2 ?
+ PR_1B_DUP_BLOCK :
+ PR_1B_DUP_RANGE;
+ fix_problem(ctx, op, p->pctx);
+ }
+ p->pctx->blk = *block_nr;
+ }
+ p->pctx->blk2 = *block_nr;
+ p->last_blk = *block_nr;
}
p->dup_blocks++;
ext2fs_mark_inode_bitmap2(inode_dup_map, p->ino);
- if (lc != p->cur_cluster)
+ /*
+ * Qualifications for submitting a block for duplicate processing:
+ * It's an extent/indirect block (and has a negative logical offset);
+ * we've crossed a logical cluster boundary; or the physical cluster
+ * suddenly changed, which indicates that blocks in a logical cluster
+ * are mapped to multiple physical clusters.
+ */
+ if (blockcnt < 0 || lc != p->cur_cluster || pc != p->phys_cluster)
add_dupe(ctx, p->ino, EXT2FS_B2C(fs, *block_nr), p->inode);
finish:
p->cur_cluster = lc;
+ p->phys_cluster = pc;
return 0;
}
/*
* Report the inode that we are working on
*/
- pctx.inode = &p->inode;
+ pctx.inode = EXT2_INODE(&p->inode);
pctx.ino = ino;
pctx.dir = p->dir;
pctx.blkcount = p->num_dupblocks;
/*
* Report the inode that we are sharing with
*/
- pctx.inode = &t->inode;
+ pctx.inode = EXT2_INODE(&t->inode);
pctx.ino = shared[i];
pctx.dir = t->dir;
fix_problem(ctx, PR_1D_DUP_FILE_LIST, &pctx);
}
- if (file_ok) {
+ /*
+ * Even if the file shares blocks with itself, we still need to
+ * clone the blocks.
+ */
+ if (file_ok && (meta_data ? shared_len+1 : shared_len) != 0) {
fix_problem(ctx, PR_1D_DUP_BLOCKS_DEALT, &pctx);
continue;
}
- if (fix_problem(ctx, PR_1D_CLONE_QUESTION, &pctx)) {
+ if ((ctx->options & E2F_OPT_UNSHARE_BLOCKS) ||
+ fix_problem(ctx, PR_1D_CLONE_QUESTION, &pctx)) {
pctx.errcode = clone_file(ctx, ino, p, block_buf);
if (pctx.errcode)
fix_problem(ctx, PR_1D_CLONE_ERROR, &pctx);
else
continue;
}
- if (fix_problem(ctx, PR_1D_DELETE_QUESTION, &pctx))
+ /*
+ * Note: When unsharing blocks, we don't prompt to delete
+ * files. If the clone operation fails than the unshare
+ * operation should fail too.
+ */
+ if (!(ctx->options & E2F_OPT_UNSHARE_BLOCKS) &&
+ fix_problem(ctx, PR_1D_DELETE_QUESTION, &pctx))
delete_file(ctx, ino, p, block_buf);
else
ext2fs_unmark_valid(fs);
pb = (struct process_block_struct *) priv_data;
ctx = pb->ctx;
- if (HOLE_BLKADDR(*block_nr))
+ if (*block_nr == 0)
return 0;
c = EXT2FS_B2C(fs, *block_nr);
if (ext2fs_test_block_bitmap2(ctx->block_dup_map, *block_nr)) {
n = dict_lookup(&clstr_dict, INT_TO_VOIDPTR(c));
if (n) {
- p = (struct dup_cluster *) dnode_get(n);
- if (lc != pb->cur_cluster)
+ if (lc != pb->cur_cluster) {
+ p = (struct dup_cluster *) dnode_get(n);
decrement_badcount(ctx, *block_nr, p);
+ pb->dup_blocks++;
+ }
} else
com_err("delete_file_block", 0,
_("internal error: can't find dup_blk for %llu\n"),
*block_nr);
} else {
- ext2fs_unmark_block_bitmap2(ctx->block_found_map, *block_nr);
- ext2fs_block_alloc_stats2(fs, *block_nr, -1);
+ if ((*block_nr % EXT2FS_CLUSTER_RATIO(ctx->fs)) == 0)
+ ext2fs_block_alloc_stats2(fs, *block_nr, -1);
pb->dup_blocks++;
}
pb->cur_cluster = lc;
pctx.str = "delete_file";
pb.cur_cluster = ~0;
- if (ext2fs_inode_has_valid_blocks2(fs, &dp->inode))
+ if (ext2fs_inode_has_valid_blocks2(fs, EXT2_INODE(&dp->inode)))
pctx.errcode = ext2fs_block_iterate3(fs, ino,
BLOCK_FLAG_READ_ONLY,
block_buf,
quota_data_inodes(ctx->qctx, &dp->inode, ino, -1);
/* Inode may have changed by block_iterate, so reread it */
- e2fsck_read_inode(ctx, ino, &dp->inode, "delete_file");
- e2fsck_clear_inode(ctx, ino, &dp->inode, 0, "delete_file");
- if (ext2fs_file_acl_block(fs, &dp->inode) &&
- (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) {
+ e2fsck_read_inode_full(ctx, ino, EXT2_INODE(&dp->inode),
+ sizeof(dp->inode), "delete_file");
+ e2fsck_clear_inode(ctx, ino, EXT2_INODE(&dp->inode), 0, "delete_file");
+ if (ext2fs_file_acl_block(fs, EXT2_INODE(&dp->inode)) &&
+ ext2fs_has_feature_xattr(fs->super)) {
+ blk64_t file_acl_block = ext2fs_file_acl_block(fs,
+ EXT2_INODE(&dp->inode));
+
count = 1;
- pctx.errcode = ext2fs_adjust_ea_refcount3(fs,
- ext2fs_file_acl_block(fs, &dp->inode),
+ pctx.errcode = ext2fs_adjust_ea_refcount3(fs, file_acl_block,
block_buf, -1, &count, ino);
if (pctx.errcode == EXT2_ET_BAD_EA_BLOCK_NUM) {
pctx.errcode = 0;
count = 1;
}
if (pctx.errcode) {
- pctx.blk = ext2fs_file_acl_block(fs, &dp->inode);
+ pctx.blk = file_acl_block;
fix_problem(ctx, PR_1B_ADJ_EA_REFCOUNT, &pctx);
}
/*
*/
if ((count == 0) ||
ext2fs_test_block_bitmap2(ctx->block_dup_map,
- ext2fs_file_acl_block(fs, &dp->inode))) {
- blk64_t blk = ext2fs_file_acl_block(fs, &dp->inode);
- delete_file_block(fs, &blk,
+ file_acl_block)) {
+ delete_file_block(fs, &file_acl_block,
BLOCK_COUNT_EXTATTR, 0, 0, &pb);
- ext2fs_file_acl_block_set(fs, &dp->inode, blk);
- quota_data_sub(ctx->qctx, &dp->inode, ino, fs->blocksize);
+ ext2fs_file_acl_block_set(fs, EXT2_INODE(&dp->inode),
+ file_acl_block);
+ quota_data_sub(ctx->qctx, &dp->inode, ino,
+ fs->blocksize);
}
}
}
errcode_t errcode;
blk64_t dup_cluster;
blk64_t alloc_block;
- ext2_ino_t dir;
+ ext2_ino_t dir, ino;
char *buf;
e2fsck_t ctx;
+ struct ext2_inode_large *inode;
+
+ struct dup_cluster *save_dup_cluster;
+ blk64_t save_blocknr;
};
+/*
+ * Decrement the bad count *after* we've shown that (a) we can allocate a
+ * replacement block and (b) remap the file blocks. Unfortunately, there's no
+ * way to find out if the remap succeeded until either the next
+ * clone_file_block() call (an error when remapping the block after returning
+ * BLOCK_CHANGED will halt the iteration) or after block_iterate() returns.
+ * Otherwise, it's possible that we decrease the badcount once in preparation
+ * to remap, then the remap fails (either we can't find a replacement block or
+ * we have to split the extent tree and can't find a new extent block), so we
+ * delete the file, which decreases the badcount again.
+ */
+static void deferred_dec_badcount(struct clone_struct *cs)
+{
+ if (!cs->save_dup_cluster)
+ return;
+ decrement_badcount(cs->ctx, cs->save_blocknr, cs->save_dup_cluster);
+ cs->save_dup_cluster = NULL;
+}
+
static int clone_file_block(ext2_filsys fs,
blk64_t *block_nr,
e2_blkcnt_t blockcnt,
int ref_offset EXT2FS_ATTR((unused)),
void *priv_data)
{
- struct dup_cluster *p;
+ struct dup_cluster *p = NULL;
blk64_t new_block;
errcode_t retval;
struct clone_struct *cs = (struct clone_struct *) priv_data;
e2fsck_t ctx;
blk64_t c;
int is_meta = 0;
+ int should_write = 1;
ctx = cs->ctx;
+ deferred_dec_badcount(cs);
- if (HOLE_BLKADDR(*block_nr))
+ if (*block_nr == 0)
return 0;
+ if (ext2fs_has_feature_shared_blocks(ctx->fs->super) &&
+ (ctx->options & E2F_OPT_UNSHARE_BLOCKS) &&
+ (ctx->options & E2F_OPT_NO))
+ should_write = 0;
+
c = EXT2FS_B2C(fs, blockcnt);
if (check_if_fs_cluster(ctx, EXT2FS_B2C(fs, *block_nr)))
is_meta = 1;
}
p = (struct dup_cluster *) dnode_get(n);
- if (!is_meta)
- decrement_badcount(ctx, *block_nr, p);
cs->dup_cluster = c;
-
+ /*
+ * Let's try an implied cluster allocation. If we get the same
+ * cluster back, then we need to find a new block; otherwise,
+ * we're merely fixing the problem of one logical cluster being
+ * mapped to multiple physical clusters.
+ */
+ new_block = 0;
+ retval = ext2fs_map_cluster_block(fs, cs->ino,
+ EXT2_INODE(cs->inode),
+ blockcnt, &new_block);
+ if (retval == 0 && new_block != 0 &&
+ EXT2FS_B2C(ctx->fs, new_block) !=
+ EXT2FS_B2C(ctx->fs, *block_nr))
+ goto cluster_alloc_ok;
retval = ext2fs_new_block2(fs, 0, ctx->block_found_map,
&new_block);
if (retval) {
cs->errcode = retval;
return BLOCK_ABORT;
}
+ if (ext2fs_has_feature_shared_blocks(fs->super)) {
+ /*
+ * Update the block stats so we don't get a prompt to fix block
+ * counts in the final pass.
+ */
+ ext2fs_block_alloc_stats2(fs, new_block, +1);
+ }
+cluster_alloc_ok:
cs->alloc_block = new_block;
got_block:
cs->errcode = retval;
return BLOCK_ABORT;
}
- retval = io_channel_write_blk64(fs->io, new_block, 1, cs->buf);
- if (retval) {
- cs->errcode = retval;
- return BLOCK_ABORT;
+ if (should_write) {
+ retval = io_channel_write_blk64(fs->io, new_block, 1, cs->buf);
+ if (retval) {
+ cs->errcode = retval;
+ return BLOCK_ABORT;
+ }
}
+ cs->save_dup_cluster = (is_meta ? NULL : p);
+ cs->save_blocknr = *block_nr;
*block_nr = new_block;
ext2fs_mark_block_bitmap2(ctx->block_found_map, new_block);
ext2fs_mark_block_bitmap2(fs->block_map, new_block);
+
+ if (!should_write) {
+ /* Don't try to change extent information; we want e2fsck to
+ * return success.
+ */
+ return 0;
+ }
return BLOCK_CHANGED;
}
return 0;
cs.dup_cluster = ~0;
cs.alloc_block = 0;
cs.ctx = ctx;
+ cs.ino = ino;
+ cs.inode = &dp->inode;
+ cs.save_dup_cluster = NULL;
+ cs.save_blocknr = 0;
retval = ext2fs_get_mem(fs->blocksize, &cs.buf);
if (retval)
return retval;
pctx.ino = ino;
pctx.str = "clone_file";
- if (ext2fs_inode_has_valid_blocks2(fs, &dp->inode))
+ if (ext2fs_inode_has_valid_blocks2(fs, EXT2_INODE(&dp->inode)))
pctx.errcode = ext2fs_block_iterate3(fs, ino, 0, block_buf,
clone_file_block, &cs);
+ deferred_dec_badcount(&cs);
ext2fs_mark_bb_dirty(fs);
if (pctx.errcode) {
fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
goto errout;
}
/* The inode may have changed on disk, so we have to re-read it */
- e2fsck_read_inode(ctx, ino, &dp->inode, "clone file EA");
- blk = ext2fs_file_acl_block(fs, &dp->inode);
+ e2fsck_read_inode_full(ctx, ino, EXT2_INODE(&dp->inode),
+ sizeof(dp->inode), "clone file EA");
+ blk = ext2fs_file_acl_block(fs, EXT2_INODE(&dp->inode));
new_blk = blk;
if (blk && (clone_file_block(fs, &new_blk,
BLOCK_COUNT_EXTATTR, 0, 0, &cs) ==
BLOCK_CHANGED)) {
- ext2fs_file_acl_block_set(fs, &dp->inode, new_blk);
- e2fsck_write_inode(ctx, ino, &dp->inode, "clone file EA");
+ ext2fs_file_acl_block_set(fs, EXT2_INODE(&dp->inode), new_blk);
+ e2fsck_write_inode_full(ctx, ino, EXT2_INODE(&dp->inode),
+ sizeof(dp->inode), "clone file EA");
/*
* If we cloned the EA block, find all other inodes
- * which refered to that EA block, and modify
+ * which referred to that EA block, and modify
* them to point to the new EA block.
*/
n = dict_lookup(&clstr_dict,
goto errout;
}
di = (struct dup_inode *) dnode_get(n);
- if (ext2fs_file_acl_block(fs, &di->inode) == blk) {
- ext2fs_file_acl_block_set(fs, &di->inode,
- ext2fs_file_acl_block(fs, &dp->inode));
- e2fsck_write_inode(ctx, ino_el->inode,
- &di->inode, "clone file EA");
+ if (ext2fs_file_acl_block(fs,
+ EXT2_INODE(&di->inode)) == blk) {
+ ext2fs_file_acl_block_set(fs,
+ EXT2_INODE(&di->inode),
+ ext2fs_file_acl_block(fs, EXT2_INODE(&dp->inode)));
+ e2fsck_write_inode_full(ctx, ino_el->inode,
+ EXT2_INODE(&di->inode),
+ sizeof(di->inode), "clone file EA");
decrement_badcount(ctx, blk, dc);
}
}