/* * This function increments the inode version number * * This may be used one day by the NFS server */ static void inc_inode_version(struct inode *inode, struct ext2_group_desc *gdp, int mode) { unsigned long inode_block; struct buf *bh; struct ext2_inode *raw_inode; inode_block = gdp->bg_inode_table + (((inode->i_number - 1) % EXT2_INODES_PER_GROUP(inode->i_sb)) / EXT2_INODES_PER_BLOCK(inode->i_sb)); bh = bread (inode->i_sb->s_dev, dbtob(inode_block), inode->i_sb->s_blocksize); if (!bh) { kprintf ("inc_inode_version Cannot load inode table block - " "inode=%lu, inode_block=%lu\n", inode->i_number, inode_block); inode->u.ext2_i.i_version = 1; return; } raw_inode = ((struct ext2_inode *) bh->b_data) + (((inode->i_number - 1) % EXT2_INODES_PER_GROUP(inode->i_sb)) % EXT2_INODES_PER_BLOCK(inode->i_sb)); raw_inode->i_version++; inode->u.ext2_i.i_version = raw_inode->i_version; bdwrite (bh); }
/* * This will try to discard number 'count' inodes starting at * inode number 'start' within the 'group'. Note that 'start' * is 1-based, it means that we need to adjust it by -1 in this * function to compute right offset in the particular inode table. */ static void e2fsck_discard_inodes(e2fsck_t ctx, dgrp_t group, ext2_ino_t start, int count) { ext2_filsys fs = ctx->fs; blk64_t blk, num; /* * Sanity check for 'start' */ if ((start < 1) || (start > EXT2_INODES_PER_GROUP(fs->super))) { printf("PROGRAMMING ERROR: Got start %d outside of group %d!" " Disabling discard\n", start, group); ctx->options &= ~E2F_OPT_DISCARD; } /* * Do not attempt to discard if E2F_OPT_DISCARD is not set. And also * skip the discard on this group if discard does not zero data. * The reason is that if the inode table is not zeroed discard would * no help us since we need to zero it anyway, or if the inode table * is zeroed then the read after discard would not be deterministic * anyway and we would not be able to assume that this inode table * was zeroed anymore so we would have to zero it again, which does * not really make sense. */ if (!(ctx->options & E2F_OPT_DISCARD) || !io_channel_discard_zeroes_data(fs->io)) return; /* * Start is inode number within the group which starts * counting from 1, so we need to adjust it. */ start -= 1; /* * We can discard only blocks containing only unused * inodes in the table. */ blk = DIV_ROUND_UP(start, EXT2_INODES_PER_BLOCK(fs->super)); count -= (blk * EXT2_INODES_PER_BLOCK(fs->super) - start); blk += ext2fs_inode_table_loc(fs, group); num = count / EXT2_INODES_PER_BLOCK(fs->super); if (num > 0) e2fsck_discard_blocks(ctx, blk, num); }
int ext2_ino2blk( struct ext2_super_block *fs, struct ext2_group_desc *gd, int ino) { int group; int blk; group = (ino - 1) / EXT2_INODES_PER_GROUP(fs); blk = le32_to_cpu(gd[group].bg_inode_table) + (((ino - 1) % EXT2_INODES_PER_GROUP(fs)) / EXT2_INODES_PER_BLOCK(fs)); return blk; }
static const struct ext2_inode * ext2_get_inode(struct fs_info *fs, int inr) { const struct ext2_group_desc *desc; const char *data; uint32_t inode_group, inode_offset; uint32_t block_num, block_off; inr--; inode_group = inr / EXT2_INODES_PER_GROUP(fs); inode_offset = inr % EXT2_INODES_PER_GROUP(fs); desc = ext2_get_group_desc(fs, inode_group); if (!desc) return NULL; block_num = desc->bg_inode_table + inode_offset / EXT2_INODES_PER_BLOCK(fs); block_off = inode_offset % EXT2_INODES_PER_BLOCK(fs); data = get_cache(fs->fs_dev, block_num); return (const struct ext2_inode *) (data + block_off * EXT2_SB(fs)->s_inode_size); }
errcode_t ext2fs_get_next_inode_full(ext2_inode_scan scan, ext2_ino_t *ino, struct ext2_inode *inode, int bufsize) { errcode_t retval; int extra_bytes = 0; int length; struct ext2_inode_large *iptr = (struct ext2_inode_large *)inode; char *iblock_status; unsigned int iblk; EXT2_CHECK_MAGIC(scan, EXT2_ET_MAGIC_INODE_SCAN); length = EXT2_INODE_SIZE(scan->fs->super); iblock_status = SCAN_BLOCK_STATUS(scan); /* * Do we need to start reading a new block group? */ if (scan->inodes_left <= 0) { force_new_group: if (scan->done_group) { retval = (scan->done_group) (scan->fs, scan, scan->current_group, scan->done_group_data); if (retval) return retval; } if (scan->groups_left <= 0) { *ino = 0; return 0; } retval = get_next_blockgroup(scan); if (retval) return retval; } /* * These checks are done outside the above if statement so * they can be done for block group #0. */ if ((scan->scan_flags & EXT2_SF_DO_LAZY) && (ext2fs_bg_flags_test(scan->fs, scan->current_group, EXT2_BG_INODE_UNINIT) )) goto force_new_group; if (scan->inodes_left == 0) goto force_new_group; if (scan->current_block == 0) { if (scan->scan_flags & EXT2_SF_SKIP_MISSING_ITABLE) { goto force_new_group; } else return EXT2_ET_MISSING_INODE_TABLE; } /* * Have we run out of space in the inode buffer? If so, we * need to read in more blocks. */ if (scan->bytes_left < scan->inode_size) { memcpy(scan->temp_buffer, scan->ptr, scan->bytes_left); extra_bytes = scan->bytes_left; retval = get_next_blocks(scan); if (retval) return retval; #if 0 /* * XXX test Need check for used inode somehow. * (Note: this is hard.) */ if (is_empty_scan(scan)) goto force_new_group; #endif } if (bufsize < length) { retval = ext2fs_get_mem(length, &iptr); if (retval) return retval; } retval = 0; iblk = scan->current_inode % EXT2_INODES_PER_GROUP(scan->fs->super) / EXT2_INODES_PER_BLOCK(scan->fs->super) % scan->inode_buffer_blocks; if (extra_bytes) { memcpy(scan->temp_buffer+extra_bytes, scan->ptr, scan->inode_size - extra_bytes); scan->ptr += scan->inode_size - extra_bytes; scan->bytes_left -= scan->inode_size - extra_bytes; /* Verify the inode checksum. */ if (!(iblock_status[iblk] & IBLOCK_STATUS_CSUMS_OK) && !(scan->fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) && !ext2fs_inode_csum_verify(scan->fs, scan->current_inode + 1, (struct ext2_inode_large *)scan->temp_buffer)) retval = EXT2_ET_INODE_CSUM_INVALID; #ifdef WORDS_BIGENDIAN memset(iptr, 0, length); ext2fs_swap_inode_full(scan->fs, (struct ext2_inode_large *) iptr, (struct ext2_inode_large *) scan->temp_buffer, 0, length); #else memcpy(iptr, scan->temp_buffer, length); #endif if (scan->scan_flags & EXT2_SF_BAD_EXTRA_BYTES) retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE; scan->scan_flags &= ~EXT2_SF_BAD_EXTRA_BYTES; } else { /* Verify the inode checksum. */ if (!(iblock_status[iblk] & IBLOCK_STATUS_CSUMS_OK) && !(scan->fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) && !ext2fs_inode_csum_verify(scan->fs, scan->current_inode + 1, (struct ext2_inode_large *)scan->ptr)) retval = EXT2_ET_INODE_CSUM_INVALID; #ifdef WORDS_BIGENDIAN memset(iptr, 0, length); ext2fs_swap_inode_full(scan->fs, (struct ext2_inode_large *) iptr, (struct ext2_inode_large *) scan->ptr, 0, length); #else memcpy(iptr, scan->ptr, length); #endif scan->ptr += scan->inode_size; scan->bytes_left -= scan->inode_size; if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK) retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE; } if ((iblock_status[iblk] & IBLOCK_STATUS_INSANE) && (retval == 0 || retval == EXT2_ET_INODE_CSUM_INVALID)) retval = EXT2_ET_INODE_IS_GARBAGE; scan->inodes_left--; scan->current_inode++; *ino = scan->current_inode; if (iptr != (struct ext2_inode_large *)inode) { memcpy(inode, iptr, bufsize); ext2fs_free_mem(&iptr); } return retval; }
/* * Check all the inodes that we just read into the buffer. Record what we * find here -- currently, we can observe that all checksums are ok; more * than half the inodes are insane; or no conclusions at all. */ static void check_inode_block_sanity(ext2_inode_scan scan, blk64_t num_blocks) { ext2_ino_t ino, inodes_to_scan; unsigned int badness, checksum_failures; unsigned int inodes_in_buf, inodes_per_block; char *p; struct ext2_inode_large *inode; char *block_status; unsigned int blk, bad_csum; if (!(scan->scan_flags & EXT2_SF_WARN_GARBAGE_INODES)) return; inodes_to_scan = scan->inodes_left; inodes_in_buf = num_blocks * scan->fs->blocksize / scan->inode_size; if (inodes_to_scan > inodes_in_buf) inodes_to_scan = inodes_in_buf; p = (char *) scan->inode_buffer; ino = scan->current_inode + 1; checksum_failures = badness = 0; block_status = SCAN_BLOCK_STATUS(scan); memset(block_status, 0, scan->inode_buffer_blocks); inodes_per_block = EXT2_INODES_PER_BLOCK(scan->fs->super); if (inodes_per_block < 2) return; #ifdef WORDS_BIGENDIAN if (ext2fs_get_mem(EXT2_INODE_SIZE(scan->fs->super), &inode)) return; #endif while (inodes_to_scan > 0) { blk = (p - (char *)scan->inode_buffer) / scan->fs->blocksize; bad_csum = ext2fs_inode_csum_verify(scan->fs, ino, (struct ext2_inode_large *) p) == 0; #ifdef WORDS_BIGENDIAN ext2fs_swap_inode_full(scan->fs, (struct ext2_inode_large *) inode, (struct ext2_inode_large *) p, 0, EXT2_INODE_SIZE(scan->fs->super)); #else inode = (struct ext2_inode_large *) p; #endif /* Is this inode insane? */ if (bad_csum) { checksum_failures++; badness++; } else if (extent_head_looks_insane(inode) || block_map_looks_insane(scan->fs, inode)) badness++; /* If more than half are insane, declare the whole block bad */ if (badness > inodes_per_block / 2) { unsigned int ino_adj; block_status[blk] |= IBLOCK_STATUS_INSANE; ino_adj = inodes_per_block - ((ino - 1) % inodes_per_block); if (ino_adj > inodes_to_scan) ino_adj = inodes_to_scan; inodes_to_scan -= ino_adj; p += scan->inode_size * ino_adj; ino += ino_adj; checksum_failures = badness = 0; continue; } if ((ino % inodes_per_block) == 0) { if (checksum_failures == 0) block_status[blk] |= IBLOCK_STATUS_CSUMS_OK; checksum_failures = badness = 0; } inodes_to_scan--; p += scan->inode_size; ino++; }; #ifdef WORDS_BIGENDIAN ext2fs_free_mem(&inode); #endif }
int ext2_itoo( struct ext2_super_block *fs, int ino) { return (ino - 1) % EXT2_INODES_PER_BLOCK(fs); }