+int ext2fs_inode_scan_flags(ext2_inode_scan scan, int set_flags,
+ int clear_flags)
+{
+ int old_flags;
+
+ if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
+ return 0;
+
+ old_flags = scan->scan_flags;
+ scan->scan_flags &= ~clear_flags;
+ scan->scan_flags |= set_flags;
+ return old_flags;
+}
+
+/*
+ * This function is called by ext2fs_get_next_inode when it needs to
+ * get ready to read in a new blockgroup.
+ */
+static errcode_t get_next_blockgroup(ext2_inode_scan scan)
+{
+ ext2_filsys fs = scan->fs;
+
+ scan->current_group++;
+ scan->groups_left--;
+
+ scan->current_block = ext2fs_inode_table_loc(scan->fs,
+ scan->current_group);
+ scan->current_inode = scan->current_group *
+ EXT2_INODES_PER_GROUP(fs->super);
+
+ scan->bytes_left = 0;
+ scan->inodes_left = EXT2_INODES_PER_GROUP(fs->super);
+ scan->blocks_left = fs->inode_blocks_per_group;
+ if (ext2fs_has_group_desc_csum(fs)) {
+ __u32 unused = ext2fs_bg_itable_unused(fs, scan->current_group);
+ if (scan->inodes_left > unused)
+ scan->inodes_left -= unused;
+ else
+ scan->inodes_left = 0;
+ scan->blocks_left =
+ (scan->inodes_left +
+ (fs->blocksize / scan->inode_size - 1)) *
+ scan->inode_size / fs->blocksize;
+ }
+
+ return 0;
+}
+
+errcode_t ext2fs_inode_scan_goto_blockgroup(ext2_inode_scan scan,
+ int group)
+{
+ scan->current_group = group - 1;
+ scan->groups_left = scan->fs->group_desc_count - group;
+ return get_next_blockgroup(scan);
+}
+
+/*
+ * This function is called by get_next_blocks() to check for bad
+ * blocks in the inode table.
+ *
+ * This function assumes that badblocks_list->list is sorted in
+ * increasing order.
+ */
+static errcode_t check_for_inode_bad_blocks(ext2_inode_scan scan,
+ blk64_t *num_blocks)
+{
+ blk64_t blk = scan->current_block;
+ badblocks_list bb = scan->fs->badblocks;
+
+ /*
+ * If the inode table is missing, then obviously there are no
+ * bad blocks. :-)
+ */
+ if (blk == 0)
+ return 0;
+
+ /*
+ * If the current block is greater than the bad block listed
+ * in the bad block list, then advance the pointer until this
+ * is no longer the case. If we run out of bad blocks, then
+ * we don't need to do any more checking!
+ */
+ while (blk > bb->list[scan->bad_block_ptr]) {
+ if (++scan->bad_block_ptr >= bb->num) {
+ scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
+ return 0;
+ }
+ }
+
+ /*
+ * If the current block is equal to the bad block listed in
+ * the bad block list, then handle that one block specially.
+ * (We could try to handle runs of bad blocks, but that
+ * only increases CPU efficiency by a small amount, at the
+ * expense of a huge expense of code complexity, and for an
+ * uncommon case at that.)
+ */
+ if (blk == bb->list[scan->bad_block_ptr]) {
+ scan->scan_flags |= EXT2_SF_BAD_INODE_BLK;
+ *num_blocks = 1;
+ if (++scan->bad_block_ptr >= bb->num)
+ scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
+ return 0;
+ }
+
+ /*
+ * If there is a bad block in the range that we're about to
+ * read in, adjust the number of blocks to read so that we we
+ * don't read in the bad block. (Then the next block to read
+ * will be the bad block, which is handled in the above case.)
+ */
+ if ((blk + *num_blocks) > bb->list[scan->bad_block_ptr])
+ *num_blocks = (int) (bb->list[scan->bad_block_ptr] - blk);
+
+ return 0;
+}
+
+static int block_map_looks_insane(ext2_filsys fs,
+ struct ext2_inode_large *inode)
+{
+ unsigned int i, bad;
+
+ /* We're only interested in block mapped files, dirs, and symlinks */
+ if ((inode->i_flags & EXT4_INLINE_DATA_FL) ||
+ (inode->i_flags & EXT4_EXTENTS_FL))
+ return 0;
+ if (!LINUX_S_ISREG(inode->i_mode) &&
+ !LINUX_S_ISLNK(inode->i_mode) &&
+ !LINUX_S_ISDIR(inode->i_mode))
+ return 0;
+ if (LINUX_S_ISLNK(inode->i_mode) &&
+ EXT2_I_SIZE(inode) <= sizeof(inode->i_block))
+ return 0;
+
+ /* Unused inodes probably aren't insane */
+ if (inode->i_links_count == 0)
+ return 0;
+
+ /* See if more than half the block maps are insane */
+ for (i = 0, bad = 0; i < EXT2_N_BLOCKS; i++)
+ if (inode->i_block[i] != 0 &&
+ (inode->i_block[i] < fs->super->s_first_data_block ||
+ inode->i_block[i] >= ext2fs_blocks_count(fs->super)))
+ bad++;
+ return bad > EXT2_N_BLOCKS / 2;
+}
+
+static int extent_head_looks_insane(struct ext2_inode_large *inode)
+{
+ if (!(inode->i_flags & EXT4_EXTENTS_FL) ||
+ ext2fs_extent_header_verify(inode->i_block,
+ sizeof(inode->i_block)) == 0)
+ return 0;
+ return 1;
+}
+
+/*
+ * 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
+}
+
+/*
+ * This function is called by ext2fs_get_next_inode when it needs to
+ * read in more blocks from the current blockgroup's inode table.
+ */
+static errcode_t get_next_blocks(ext2_inode_scan scan)
+{
+ blk64_t num_blocks;
+ errcode_t retval;
+
+ /*
+ * Figure out how many blocks to read; we read at most
+ * inode_buffer_blocks, and perhaps less if there aren't that
+ * many blocks left to read.
+ */
+ num_blocks = scan->inode_buffer_blocks;
+ if (num_blocks > scan->blocks_left)
+ num_blocks = scan->blocks_left;
+
+ /*
+ * If the past block "read" was a bad block, then mark the
+ * left-over extra bytes as also being bad.
+ */
+ if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK) {
+ if (scan->bytes_left)
+ scan->scan_flags |= EXT2_SF_BAD_EXTRA_BYTES;
+ scan->scan_flags &= ~EXT2_SF_BAD_INODE_BLK;
+ }
+
+ /*
+ * Do inode bad block processing, if necessary.
+ */
+ if (scan->scan_flags & EXT2_SF_CHK_BADBLOCKS) {
+ retval = check_for_inode_bad_blocks(scan, &num_blocks);
+ if (retval)
+ return retval;
+ }
+
+ if ((scan->scan_flags & EXT2_SF_BAD_INODE_BLK) ||
+ (scan->current_block == 0)) {
+ memset(scan->inode_buffer, 0,
+ (size_t) num_blocks * scan->fs->blocksize);
+ } else {
+ retval = io_channel_read_blk64(scan->fs->io,
+ scan->current_block,
+ (int) num_blocks,
+ scan->inode_buffer);
+ if (retval)
+ return EXT2_ET_NEXT_INODE_READ;
+ }
+ check_inode_block_sanity(scan, num_blocks);
+
+ scan->ptr = scan->inode_buffer;
+ scan->bytes_left = num_blocks * scan->fs->blocksize;
+
+ scan->blocks_left -= num_blocks;
+ if (scan->current_block)
+ scan->current_block += num_blocks;
+
+ return 0;
+}
+
+#if 0
+/*
+ * Returns 1 if the entire inode_buffer has a non-zero size and
+ * contains all zeros. (Not just deleted inodes, since that means
+ * that part of the inode table was used at one point; we want all
+ * zeros, which means that the inode table is pristine.)
+ */
+static inline int is_empty_scan(ext2_inode_scan scan)
+{
+ int i;
+
+ if (scan->bytes_left == 0)
+ return 0;
+
+ for (i=0; i < scan->bytes_left; i++)
+ if (scan->ptr[i])
+ return 0;
+ return 1;
+}
+#endif
+
+errcode_t ext2fs_get_next_inode_full(ext2_inode_scan scan, ext2_ino_t *ino,
+ struct ext2_inode *inode, int bufsize)