+ 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) {
+ unsigned int min_size = EXT2_DIR_REC_LEN(
+ ext2fs_dirent_name_len(dirbuf));
+ if (min_size > (char *)top - (char *)d)
+ return EXT2_ET_DIR_NO_SPACE_FOR_CSUM;
+ d->rec_len = (char *)top - (char *)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;
+ /*
+ * If there's not enough bytes to start the "second" dir block
+ * (in the EA space) then truncate everything to the first block.
+ */
+ if (old_size > EXT4_MIN_INLINE_DATA_SIZE &&
+ old_size < EXT4_MIN_INLINE_DATA_SIZE +
+ EXT2_DIR_REC_LEN(1)) {
+ old_size = EXT4_MIN_INLINE_DATA_SIZE;
+ new_size = old_size;
+ } else
+ /* Increase to the next four-byte boundary for salvaging */
+ 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) {
+ /* Or we can't, so truncate. */
+ 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_block2(ext2_filsys fs,
+ struct ext2_db_entry2 *db,
+ void *priv_data)
+{
+ int err;
+ struct check_dir_struct *cd = priv_data;
+
+ if (cd->ra_entries && cd->list_offset >= cd->next_ra_off) {
+ err = e2fsck_readahead_dblist(fs,
+ E2FSCK_RA_DBLIST_IGNORE_BLOCKCNT,
+ fs->dblist,
+ cd->list_offset + cd->ra_entries / 8,
+ cd->ra_entries);
+ if (err)
+ cd->ra_entries = 0;
+ cd->next_ra_off = cd->list_offset + (cd->ra_entries * 7 / 8);
+ }
+
+ err = check_dir_block(fs, db, priv_data);
+ cd->list_offset++;
+ return err;