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) > (int) 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 (ext2fs_has_feature_metadata_csum(fs->super))
222 csum_size = sizeof(struct ext2_dir_entry_tail);
224 while (offset < buflen - 8) {
225 dirent = (struct ext2_dir_entry *) (ctx->buf + offset);
226 if (ext2fs_get_rec_len(fs, dirent, &rec_len))
228 if (((offset + rec_len) > buflen) ||
230 ((rec_len % 4) != 0) ||
231 ((ext2fs_dirent_name_len(dirent)+8) > (int) rec_len)) {
232 ctx->errcode = EXT2_ET_DIR_CORRUPTED;
235 if (!dirent->inode) {
237 * We just need to check metadata_csum when this
238 * dir hasn't inline data. That means that 'buflen'
239 * should be blocksize.
242 (offset == buflen - csum_size) &&
243 (dirent->rec_len == csum_size) &&
244 (dirent->name_len == EXT2_DIR_NAME_LEN_CSUM)) {
245 if (!(ctx->flags & DIRENT_FLAG_INCLUDE_CSUM))
247 entry = DIRENT_CHECKSUM;
248 } else if (!(ctx->flags & DIRENT_FLAG_INCLUDE_EMPTY))
252 ret = (ctx->func)(ctx->dir,
253 (next_real_entry > offset) ?
254 DIRENT_DELETED_FILE : entry,
258 if (entry < DIRENT_OTHER_FILE)
261 if (ret & DIRENT_CHANGED) {
262 if (ext2fs_get_rec_len(fs, dirent, &rec_len))
266 if (ret & DIRENT_ABORT) {
271 if (next_real_entry == offset)
272 next_real_entry += rec_len;
274 if (ctx->flags & DIRENT_FLAG_INCLUDE_REMOVED) {
275 size = (ext2fs_dirent_name_len(dirent) + 11) & ~3;
277 if (rec_len != size) {
278 unsigned int final_offset;
280 final_offset = offset + rec_len;
282 while (offset < final_offset &&
283 !ext2fs_validate_entry(fs, ctx->buf,
295 ctx->errcode = ext2fs_write_dir_block4(fs, *blocknr,
302 * return BLOCK_INLINE_DATA_CHANGED to notify caller
303 * that inline data has been changed.
305 retval = BLOCK_INLINE_DATA_CHANGED;
309 return retval | BLOCK_ABORT;