X-Git-Url: https://git.whamcloud.com/?a=blobdiff_plain;f=e2fsck%2Fpass2.c;h=7aaebce8f51eabd148c3cd6f4891a76a219e36a8;hb=d02d019557d993c8dfe2f7d0fea841101f0d9e61;hp=0ef9637369bf33551a609aac7a519da8706ca2ad;hpb=60203cb17147d1fdca9482f295be1f3de6d01240;p=tools%2Fe2fsprogs.git diff --git a/e2fsck/pass2.c b/e2fsck/pass2.c index 0ef9637..7aaebce 100644 --- a/e2fsck/pass2.c +++ b/e2fsck/pass2.c @@ -166,7 +166,8 @@ void e2fsck_pass2(e2fsck_t ctx) for (i=0; (dx_dir = e2fsck_dx_dir_info_iter(ctx, &i)) != 0;) { if (ctx->flags & E2F_FLAG_SIGNAL_MASK) return; - if (dx_dir->numblocks == 0) + if (e2fsck_dir_will_be_rehashed(ctx, dx_dir->ino) || + dx_dir->numblocks == 0) continue; clear_problem_context(&pctx); bad_dir = 0; @@ -665,7 +666,8 @@ clear_and_exit: static void salvage_directory(ext2_filsys fs, struct ext2_dir_entry *dirent, struct ext2_dir_entry *prev, - unsigned int *offset) + unsigned int *offset, + unsigned int block_len) { char *cp = (char *) dirent; int left; @@ -673,7 +675,7 @@ static void salvage_directory(ext2_filsys fs, unsigned int name_len = ext2fs_dirent_name_len(dirent); (void) ext2fs_get_rec_len(fs, dirent, &rec_len); - left = fs->blocksize - *offset - rec_len; + left = block_len - *offset - rec_len; /* * Special case of directory entry of size 8: copy what's left @@ -703,7 +705,7 @@ static void salvage_directory(ext2_filsys fs, * previous directory entry absorb the invalid one. */ if (prev && rec_len && (rec_len % 4) == 0 && - (*offset + rec_len <= fs->blocksize)) { + (*offset + rec_len <= block_len)) { (void) ext2fs_get_rec_len(fs, prev, &prev_rec_len); prev_rec_len += rec_len; (void) ext2fs_set_rec_len(fs, prev_rec_len, prev); @@ -718,11 +720,11 @@ static void salvage_directory(ext2_filsys fs, */ if (prev) { (void) ext2fs_get_rec_len(fs, prev, &prev_rec_len); - prev_rec_len += fs->blocksize - *offset; + prev_rec_len += block_len - *offset; (void) ext2fs_set_rec_len(fs, prev_rec_len, prev); *offset = fs->blocksize; } else { - rec_len = fs->blocksize - *offset; + rec_len = block_len - *offset; (void) ext2fs_set_rec_len(fs, rec_len, dirent); ext2fs_dirent_set_name_len(dirent, 0); ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN); @@ -739,6 +741,90 @@ static int is_last_entry(ext2_filsys fs, int inline_data_size, return (offset < fs->blocksize - csum_size); } +#define NEXT_DIRENT(d) ((void *)((char *)(d) + (d)->rec_len)) +static errcode_t insert_dirent_tail(ext2_filsys fs, void *dirbuf) +{ + struct ext2_dir_entry *d; + void *top; + struct ext2_dir_entry_tail *t; + + d = dirbuf; + top = EXT2_DIRENT_TAIL(dirbuf, fs->blocksize); + + while (d->rec_len && !(d->rec_len & 0x3) && NEXT_DIRENT(d) <= top) + d = NEXT_DIRENT(d); + + if (d != top) { + size_t min_size = EXT2_DIR_REC_LEN( + ext2fs_dirent_name_len(dirbuf)); + if (min_size > top - (void *)d) + return EXT2_ET_DIR_NO_SPACE_FOR_CSUM; + d->rec_len = top - (void *)d; + } + + t = (struct ext2_dir_entry_tail *)top; + if (t->det_reserved_zero1 || + t->det_rec_len != sizeof(struct ext2_dir_entry_tail) || + t->det_reserved_name_len != EXT2_DIR_NAME_LEN_CSUM) + ext2fs_initialize_dirent_tail(fs, t); + + return 0; +} +#undef NEXT_DIRENT + +static errcode_t fix_inline_dir_size(e2fsck_t ctx, ext2_ino_t ino, + size_t *inline_data_size, + struct problem_context *pctx, + char *buf) +{ + ext2_filsys fs = ctx->fs; + struct ext2_inode inode; + size_t new_size, old_size; + errcode_t retval; + + old_size = *inline_data_size; + new_size = old_size + (4 - (old_size & 3)); + memset(buf + old_size, 0, new_size - old_size); + retval = ext2fs_inline_data_set(fs, ino, 0, buf, new_size); + if (retval == EXT2_ET_INLINE_DATA_NO_SPACE) { + new_size -= 4; + retval = ext2fs_inline_data_set(fs, ino, 0, buf, new_size); + if (retval) { + if (fix_problem(ctx, PR_2_FIX_INLINE_DIR_FAILED, + pctx)) { + new_size = 0; + goto write_inode; + } + goto err; + } + } else if (retval) { + if (fix_problem(ctx, PR_2_FIX_INLINE_DIR_FAILED, + pctx)) { + new_size = 0; + goto write_inode; + } + goto err; + } + +write_inode: + retval = ext2fs_read_inode(fs, ino, &inode); + if (retval) + goto err; + + retval = ext2fs_inode_size_set(fs, &inode, new_size); + if (retval) + goto err; + if (new_size == 0) + inode.i_flags &= ~EXT4_INLINE_DATA_FL; + retval = ext2fs_write_inode(fs, ino, &inode); + if (retval) + goto err; + *inline_data_size = new_size; + +err: + return retval; +} + static int check_dir_block(ext2_filsys fs, struct ext2_db_entry2 *db, void *priv_data) @@ -837,11 +923,32 @@ static int check_dir_block(ext2_filsys fs, #endif ehandler_operation(_("reading directory block")); - if (inline_data_size) + if (inline_data_size) { cd->pctx.errcode = ext2fs_inline_data_get(fs, ino, 0, buf, 0); - else + if (cd->pctx.errcode) + goto inline_read_fail; +#ifdef WORDS_BIGENDIAN + *((__u32 *)buf) = ext2fs_le32_to_cpu(*((__u32 *)buf)); + cd->pctx.errcode = ext2fs_dirent_swab_in2(fs, + buf + EXT4_INLINE_DATA_DOTDOT_SIZE, + inline_data_size - EXT4_INLINE_DATA_DOTDOT_SIZE, + 0); +#endif + } else cd->pctx.errcode = ext2fs_read_dir_block4(fs, block_nr, buf, 0, ino); +inline_read_fail: + pctx.ino = ino; + pctx.num = inline_data_size; + if ((inline_data_size & 3) && + fix_problem(ctx, PR_2_BAD_INLINE_DIR_SIZE, &pctx)) { + errcode_t err = fix_inline_dir_size(ctx, ino, + &inline_data_size, &pctx, + buf); + if (err) + return DIRENT_ABORT; + + } ehandler_operation(0); if (cd->pctx.errcode == EXT2_ET_DIR_CORRUPTED) cd->pctx.errcode = 0; /* We'll handle this ourselves */ @@ -864,6 +971,7 @@ static int check_dir_block(ext2_filsys fs, dx_dir = e2fsck_get_dx_dir_info(ctx, ino); if (dx_dir && dx_dir->numblocks) { if (db->blockcnt >= dx_dir->numblocks) { + pctx.dir = ino; if (fix_problem(ctx, PR_2_UNEXPECTED_HTREE_BLOCK, &pctx)) { clear_htree(ctx, ino); @@ -911,32 +1019,20 @@ static int check_dir_block(ext2_filsys fs, out_htree: #endif /* ENABLE_HTREE */ - /* Verify checksum. */ - if (is_leaf && de_csum_size && !inline_data_size) { - /* No space for csum? Rebuild dirs in pass 3A. */ - if (!ext2fs_dirent_has_tail(fs, (struct ext2_dir_entry *)buf)) { - de_csum_size = 0; - if (e2fsck_dir_will_be_rehashed(ctx, ino)) - goto skip_checksum; - if (!fix_problem(cd->ctx, PR_2_LEAF_NODE_MISSING_CSUM, - &cd->pctx)) - goto skip_checksum; - e2fsck_rehash_dir_later(ctx, ino); - goto skip_checksum; - } - if (failed_csum) { - char *buf2; - if (!fix_problem(cd->ctx, PR_2_LEAF_NODE_CSUM_INVALID, - &cd->pctx)) - goto skip_checksum; - ext2fs_new_dir_block(fs, - db->blockcnt == 0 ? ino : 0, - EXT2_ROOT_INO, &buf2); - memcpy(buf, buf2, fs->blocksize); - ext2fs_free_mem(&buf2); - dir_modified++; + /* Leaf node with no space for csum? Rebuild dirs in pass 3A. */ + if (is_leaf && !inline_data_size && failed_csum && + !ext2fs_dirent_has_tail(fs, (struct ext2_dir_entry *)buf)) { + de_csum_size = 0; + if (e2fsck_dir_will_be_rehashed(ctx, ino)) { failed_csum = 0; + goto skip_checksum; } + if (!fix_problem(cd->ctx, PR_2_LEAF_NODE_MISSING_CSUM, + &cd->pctx)) + goto skip_checksum; + e2fsck_rehash_dir_later(ctx, ino); + failed_csum = 0; + goto skip_checksum; } /* htree nodes don't use fake dirents to store checksums */ if (!is_leaf) @@ -952,16 +1048,63 @@ skip_checksum: problem = 0; if (!inline_data_size || dot_state > 1) { + size_t max_block_size = fs->blocksize - de_csum_size; + + if (inline_data_size) + max_block_size = inline_data_size; dirent = (struct ext2_dir_entry *) (buf + offset); (void) ext2fs_get_rec_len(fs, dirent, &rec_len); cd->pctx.dirent = dirent; cd->pctx.num = offset; if (((offset + rec_len) > fs->blocksize) || + (inline_data_size > 0 && + (offset + rec_len) > inline_data_size) || (rec_len < 12) || ((rec_len % 4) != 0) || ((ext2fs_dirent_name_len(dirent) + 8) > rec_len)) { - if (fix_problem(ctx, PR_2_DIR_CORRUPTED, &cd->pctx)) { - salvage_directory(fs, dirent, prev, &offset); + if (fix_problem(ctx, PR_2_DIR_CORRUPTED, + &cd->pctx)) { +#ifdef WORDS_BIGENDIAN + /* + * On big-endian systems, if the dirent + * swap routine finds a rec_len that it + * doesn't like, it continues + * processing the block as if rec_len + * == 8. This means that the name + * field gets byte swapped, which means + * that salvage will not detect the + * correct name length (unless the name + * has a length that's an exact + * multiple of four bytes), and it'll + * discard the entry (unnecessarily) + * and the rest of the dirent block. + * Therefore, swap the rest of the + * block back to disk order, run + * salvage, and re-swap anything after + * the salvaged dirent. + */ + int need_reswab = 0; + if (rec_len < 8 || rec_len % 4) { + need_reswab = 1; + ext2fs_dirent_swab_in2(fs, + ((char *)dirent) + 8, + max_block_size - offset - 8, + 0); + } +#endif + salvage_directory(fs, dirent, prev, + &offset, + max_block_size); +#ifdef WORDS_BIGENDIAN + if (need_reswab) { + (void) ext2fs_get_rec_len(fs, + dirent, &rec_len); + ext2fs_dirent_swab_in2(fs, + ((char *)dirent) + offset + rec_len, + max_block_size - offset - rec_len, + 0); + } +#endif dir_modified++; continue; } else @@ -1230,8 +1373,19 @@ skip_checksum: if (!inline_data_size || dot_state > 1) { offset += rec_len; } else { - if (dot_state == 1) + if (dot_state == 1) { offset = 4; + /* + * If we get here, we're checking an inline + * directory and we've just checked a (fake) + * dotdot entry that we created on the stack. + * Therefore set 'prev' to NULL so that if we + * call salvage_directory on the next entry, + * it won't try to absorb the next entry into + * the on-stack dotdot entry. + */ + prev = NULL; + } } dot_state++; } while (is_last_entry(fs, inline_data_size, offset, de_csum_size)); @@ -1271,25 +1425,47 @@ skip_checksum: } } if (dir_modified) { + int flags, will_rehash; /* leaf block with no tail? Rehash dirs later. */ if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super, EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) && is_leaf && - !ext2fs_dirent_has_tail(fs, (struct ext2_dir_entry *)buf)) + !inline_data_size && + !ext2fs_dirent_has_tail(fs, (struct ext2_dir_entry *)buf)) { + if (insert_dirent_tail(fs, buf) == 0) + goto write_and_fix; e2fsck_rehash_dir_later(ctx, ino); + } write_and_fix: - if (e2fsck_dir_will_be_rehashed(ctx, ino)) + will_rehash = e2fsck_dir_will_be_rehashed(ctx, ino); + if (will_rehash) { + flags = ctx->fs->flags; ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS; + } if (inline_data_size) { +#ifdef WORDS_BIGENDIAN + *((__u32 *)buf) = ext2fs_le32_to_cpu(*((__u32 *)buf)); + cd->pctx.errcode = ext2fs_dirent_swab_out2(fs, + buf + EXT4_INLINE_DATA_DOTDOT_SIZE, + inline_data_size - + EXT4_INLINE_DATA_DOTDOT_SIZE, + 0); + if (cd->pctx.errcode && + !fix_problem(ctx, PR_2_WRITE_DIRBLOCK, &cd->pctx)) + goto abort_free_dict; +#endif cd->pctx.errcode = ext2fs_inline_data_set(fs, ino, 0, buf, inline_data_size); } else cd->pctx.errcode = ext2fs_write_dir_block4(fs, block_nr, buf, 0, ino); - if (e2fsck_dir_will_be_rehashed(ctx, ino)) - ctx->fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS; + if (will_rehash) + ctx->fs->flags = (flags & + EXT2_FLAG_IGNORE_CSUM_ERRORS) | + (ctx->fs->flags & + ~EXT2_FLAG_IGNORE_CSUM_ERRORS); if (cd->pctx.errcode) { if (!fix_problem(ctx, PR_2_WRITE_DIRBLOCK, &cd->pctx)) @@ -1336,7 +1512,8 @@ static int deallocate_inode_block(ext2_filsys fs, if ((*block_nr < fs->super->s_first_data_block) || (*block_nr >= ext2fs_blocks_count(fs->super))) return 0; - ext2fs_block_alloc_stats2(fs, *block_nr, -1); + if ((*block_nr % EXT2FS_CLUSTER_RATIO(fs)) == 0) + ext2fs_block_alloc_stats2(fs, *block_nr, -1); p->num++; return 0; } @@ -1387,6 +1564,10 @@ static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf) if (!ext2fs_inode_has_valid_blocks2(fs, &inode)) goto clear_inode; + /* Inline data inodes don't have blocks to iterate */ + if (inode.i_flags & EXT4_INLINE_DATA_FL) + goto clear_inode; + if (LINUX_S_ISREG(inode.i_mode) && ext2fs_needs_large_file_feature(EXT2_I_SIZE(&inode))) ctx->large_files--; @@ -1564,7 +1745,6 @@ int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir, return 0; } - /* * allocate_dir_block --- this function allocates a new directory * block for a particular inode; this is done if a directory has @@ -1577,7 +1757,7 @@ static int allocate_dir_block(e2fsck_t ctx, struct problem_context *pctx) { ext2_filsys fs = ctx->fs; - blk64_t blk; + blk64_t blk = 0; char *block; struct ext2_inode inode; @@ -1593,11 +1773,18 @@ static int allocate_dir_block(e2fsck_t ctx, /* * First, find a free block */ - pctx->errcode = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk); - if (pctx->errcode) { - pctx->str = "ext2fs_new_block"; - fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx); - return 1; + e2fsck_read_inode(ctx, db->ino, &inode, "allocate_dir_block"); + pctx->errcode = ext2fs_map_cluster_block(fs, db->ino, &inode, + db->blockcnt, &blk); + if (pctx->errcode || blk == 0) { + blk = ext2fs_find_inode_goal(fs, db->ino, &inode, db->blockcnt); + pctx->errcode = ext2fs_new_block2(fs, blk, + ctx->block_found_map, &blk); + if (pctx->errcode) { + pctx->str = "ext2fs_new_block"; + fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx); + return 1; + } } ext2fs_mark_block_bitmap2(ctx->block_found_map, blk); ext2fs_mark_block_bitmap2(fs->block_map, blk); @@ -1629,10 +1816,16 @@ static int allocate_dir_block(e2fsck_t ctx, /* * Update the inode block count */ - e2fsck_read_inode(ctx, db->ino, &inode, "allocate_dir_block"); ext2fs_iblk_add_blocks(fs, &inode, 1); - if (inode.i_size < (db->blockcnt+1) * fs->blocksize) - inode.i_size = (db->blockcnt+1) * fs->blocksize; + if (EXT2_I_SIZE(&inode) < (db->blockcnt+1) * fs->blocksize) { + pctx->errcode = ext2fs_inode_size_set(fs, &inode, + (db->blockcnt+1) * fs->blocksize); + if (pctx->errcode) { + pctx->str = "ext2fs_inode_size_set"; + fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx); + return 1; + } + } e2fsck_write_inode(ctx, db->ino, &inode, "allocate_dir_block"); /*