2 * dir_iterate.c --- ext2fs directory iteration operations
4 * Copyright (C) 1993, 1994, 1994, 1995, 1996, 1997 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
25 #define EXT4_MAX_REC_LEN ((1<<16)-1)
27 errcode_t ext2fs_get_rec_len(ext2_filsys fs,
28 struct ext2_dir_entry *dirent,
29 unsigned int *rec_len)
31 unsigned int len = dirent->rec_len;
33 if (fs->blocksize < 65536)
35 else if (len == EXT4_MAX_REC_LEN || len == 0)
36 *rec_len = fs->blocksize;
38 *rec_len = (len & 65532) | ((len & 3) << 16);
42 errcode_t ext2fs_set_rec_len(ext2_filsys fs,
44 struct ext2_dir_entry *dirent)
46 if ((len > fs->blocksize) || (fs->blocksize > (1 << 18)) || (len & 3))
49 dirent->rec_len = len;
52 if (len == fs->blocksize) {
53 if (fs->blocksize == 65536)
54 dirent->rec_len = EXT4_MAX_REC_LEN;
58 dirent->rec_len = (len & 65532) | ((len >> 16) & 3);
63 * This function checks to see whether or not a potential deleted
64 * directory entry looks valid. What we do is check the deleted entry
65 * and each successive entry to make sure that they all look valid and
66 * that the last deleted entry ends at the beginning of the next
67 * undeleted entry. Returns 1 if the deleted entry looks valid, zero
70 static int ext2fs_validate_entry(ext2_filsys fs, char *buf,
72 unsigned int final_offset)
74 struct ext2_dir_entry *dirent;
76 #define DIRENT_MIN_LENGTH 12
78 while ((offset < final_offset) &&
79 (offset <= fs->blocksize - DIRENT_MIN_LENGTH)) {
80 dirent = (struct ext2_dir_entry *)(buf + offset);
81 if (ext2fs_get_rec_len(fs, dirent, &rec_len))
85 ((rec_len % 4) != 0) ||
86 ((ext2fs_dirent_name_len(dirent)+8) > rec_len))
89 return (offset == final_offset);
92 errcode_t ext2fs_dir_iterate2(ext2_filsys fs,
96 int (*func)(ext2_ino_t dir,
98 struct ext2_dir_entry *dirent,
105 struct dir_context ctx;
108 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
110 retval = ext2fs_check_directory(fs, dir);
119 retval = ext2fs_get_mem(fs->blocksize, &ctx.buf);
124 ctx.priv_data = priv_data;
126 retval = ext2fs_block_iterate3(fs, dir, BLOCK_FLAG_READ_ONLY, 0,
127 ext2fs_process_dir_block, &ctx);
129 ext2fs_free_mem(&ctx.buf);
130 if (retval == EXT2_ET_INLINE_DATA_CANT_ITERATE) {
131 (void) ext2fs_inline_data_dir_iterate(fs, dir, &ctx);
140 int (*func)(struct ext2_dir_entry *dirent,
148 static int xlate_func(ext2_ino_t dir EXT2FS_ATTR((unused)),
149 int entry EXT2FS_ATTR((unused)),
150 struct ext2_dir_entry *dirent, int offset,
151 int blocksize, char *buf, void *priv_data)
153 struct xlate *xl = (struct xlate *) priv_data;
155 return (*xl->func)(dirent, offset, blocksize, buf, xl->real_private);
158 errcode_t ext2fs_dir_iterate(ext2_filsys fs,
162 int (*func)(struct ext2_dir_entry *dirent,
171 xl.real_private = priv_data;
174 return ext2fs_dir_iterate2(fs, dir, flags, block_buf,
180 * Helper function which is private to this module. Used by
181 * ext2fs_dir_iterate() and ext2fs_dblist_dir_iterate()
183 int ext2fs_process_dir_block(ext2_filsys fs,
185 e2_blkcnt_t blockcnt,
186 blk64_t ref_block EXT2FS_ATTR((unused)),
187 int ref_offset EXT2FS_ATTR((unused)),
190 struct dir_context *ctx = (struct dir_context *) priv_data;
191 unsigned int offset = 0;
192 unsigned int next_real_entry = 0;
196 unsigned int rec_len, size, buflen;
198 struct ext2_dir_entry *dirent;
201 errcode_t retval = 0;
206 entry = blockcnt ? DIRENT_OTHER_FILE : DIRENT_DOT_FILE;
208 /* If a dir has inline data, we don't need to read block */
209 inline_data = !!(ctx->flags & DIRENT_FLAG_INCLUDE_INLINE_DATA);
211 ctx->errcode = ext2fs_read_dir_block4(fs, *blocknr, ctx->buf, 0,
215 /* If we handle a normal dir, we traverse the entire block */
216 buflen = fs->blocksize;
218 buflen = ctx->buflen;
221 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
222 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
223 csum_size = sizeof(struct ext2_dir_entry_tail);
225 while (offset < buflen - 8) {
226 dirent = (struct ext2_dir_entry *) (ctx->buf + offset);
227 if (ext2fs_get_rec_len(fs, dirent, &rec_len))
229 if (((offset + rec_len) > buflen) ||
231 ((rec_len % 4) != 0) ||
232 ((ext2fs_dirent_name_len(dirent)+8) > rec_len)) {
233 ctx->errcode = EXT2_ET_DIR_CORRUPTED;
236 if (!dirent->inode) {
238 * We just need to check metadata_csum when this
239 * dir hasn't inline data. That means that 'buflen'
240 * should be blocksize.
243 (offset == buflen - csum_size) &&
244 (dirent->rec_len == csum_size) &&
245 (dirent->name_len == EXT2_DIR_NAME_LEN_CSUM)) {
246 if (!(ctx->flags & DIRENT_FLAG_INCLUDE_CSUM))
248 entry = DIRENT_CHECKSUM;
249 } else if (!(ctx->flags & DIRENT_FLAG_INCLUDE_EMPTY))
253 ret = (ctx->func)(ctx->dir,
254 (next_real_entry > offset) ?
255 DIRENT_DELETED_FILE : entry,
259 if (entry < DIRENT_OTHER_FILE)
262 if (ret & DIRENT_CHANGED) {
263 if (ext2fs_get_rec_len(fs, dirent, &rec_len))
267 if (ret & DIRENT_ABORT) {
272 if (next_real_entry == offset)
273 next_real_entry += rec_len;
275 if (ctx->flags & DIRENT_FLAG_INCLUDE_REMOVED) {
276 size = (ext2fs_dirent_name_len(dirent) + 11) & ~3;
278 if (rec_len != size) {
279 unsigned int final_offset;
281 final_offset = offset + rec_len;
283 while (offset < final_offset &&
284 !ext2fs_validate_entry(fs, ctx->buf,
296 ctx->errcode = ext2fs_write_dir_block4(fs, *blocknr,
303 * return BLOCK_INLINE_DATA_CHANGED to notify caller
304 * that inline data has been changed.
306 retval = BLOCK_INLINE_DATA_CHANGED;
310 return retval | BLOCK_ABORT;