• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  linux/fs/ext4/dir.c
4  *
5  * Copyright (C) 1992, 1993, 1994, 1995
6  * Remy Card (card@masi.ibp.fr)
7  * Laboratoire MASI - Institut Blaise Pascal
8  * Universite Pierre et Marie Curie (Paris VI)
9  *
10  *  from
11  *
12  *  linux/fs/minix/dir.c
13  *
14  *  Copyright (C) 1991, 1992  Linus Torvalds
15  *
16  *  ext4 directory handling functions
17  *
18  *  Big-endian to little-endian byte-swapping/bitmaps by
19  *        David S. Miller (davem@caip.rutgers.edu), 1995
20  *
21  * Hash Tree Directory indexing (c) 2001  Daniel Phillips
22  *
23  */
24 
25 #include <linux/fs.h>
26 #include <linux/buffer_head.h>
27 #include <linux/slab.h>
28 #include <linux/iversion.h>
29 #include <linux/unicode.h>
30 #include "ext4.h"
31 #include "xattr.h"
32 
33 static int ext4_dx_readdir(struct file *, struct dir_context *);
34 
35 /**
36  * is_dx_dir() - check if a directory is using htree indexing
37  * @inode: directory inode
38  *
39  * Check if the given dir-inode refers to an htree-indexed directory
40  * (or a directory which could potentially get converted to use htree
41  * indexing).
42  *
43  * Return 1 if it is a dx dir, 0 if not
44  */
is_dx_dir(struct inode * inode)45 static int is_dx_dir(struct inode *inode)
46 {
47 	struct super_block *sb = inode->i_sb;
48 
49 	if (ext4_has_feature_dir_index(inode->i_sb) &&
50 	    ((ext4_test_inode_flag(inode, EXT4_INODE_INDEX)) ||
51 	     ((inode->i_size >> sb->s_blocksize_bits) == 1) ||
52 	     ext4_has_inline_data(inode)))
53 		return 1;
54 
55 	return 0;
56 }
57 
58 /*
59  * Return 0 if the directory entry is OK, and 1 if there is a problem
60  *
61  * Note: this is the opposite of what ext2 and ext3 historically returned...
62  *
63  * bh passed here can be an inode block or a dir data block, depending
64  * on the inode inline data flag.
65  */
__ext4_check_dir_entry(const char * function,unsigned int line,struct inode * dir,struct file * filp,struct ext4_dir_entry_2 * de,struct buffer_head * bh,char * buf,int size,unsigned int offset)66 int __ext4_check_dir_entry(const char *function, unsigned int line,
67 			   struct inode *dir, struct file *filp,
68 			   struct ext4_dir_entry_2 *de,
69 			   struct buffer_head *bh, char *buf, int size,
70 			   unsigned int offset)
71 {
72 	const char *error_msg = NULL;
73 	const int rlen = ext4_rec_len_from_disk(de->rec_len,
74 						dir->i_sb->s_blocksize);
75 	const int next_offset = ((char *) de - buf) + rlen;
76 
77 	if (unlikely(rlen < EXT4_DIR_REC_LEN(1)))
78 		error_msg = "rec_len is smaller than minimal";
79 	else if (unlikely(rlen % 4 != 0))
80 		error_msg = "rec_len % 4 != 0";
81 	else if (unlikely(rlen < EXT4_DIR_REC_LEN(de->name_len)))
82 		error_msg = "rec_len is too small for name_len";
83 	else if (unlikely(next_offset > size))
84 		error_msg = "directory entry overrun";
85 	else if (unlikely(next_offset > size - EXT4_DIR_REC_LEN(1) &&
86 			  next_offset != size))
87 		error_msg = "directory entry too close to block end";
88 	else if (unlikely(le32_to_cpu(de->inode) >
89 			le32_to_cpu(EXT4_SB(dir->i_sb)->s_es->s_inodes_count)))
90 		error_msg = "inode out of bounds";
91 	else if (unlikely(next_offset == size && de->name_len == 1 &&
92 			  de->name[0] == '.'))
93 		error_msg = "'.' directory cannot be the last in data block";
94 	else
95 		return 0;
96 
97 	if (filp)
98 		ext4_error_file(filp, function, line, bh->b_blocknr,
99 				"bad entry in directory: %s - offset=%u, "
100 				"inode=%u, rec_len=%d, name_len=%d, size=%d",
101 				error_msg, offset, le32_to_cpu(de->inode),
102 				rlen, de->name_len, size);
103 	else
104 		ext4_error_inode(dir, function, line, bh->b_blocknr,
105 				"bad entry in directory: %s - offset=%u, "
106 				"inode=%u, rec_len=%d, name_len=%d, size=%d",
107 				 error_msg, offset, le32_to_cpu(de->inode),
108 				 rlen, de->name_len, size);
109 
110 	return 1;
111 }
112 
ext4_readdir(struct file * file,struct dir_context * ctx)113 static int ext4_readdir(struct file *file, struct dir_context *ctx)
114 {
115 	unsigned int offset;
116 	int i;
117 	struct ext4_dir_entry_2 *de;
118 	int err;
119 	struct inode *inode = file_inode(file);
120 	struct super_block *sb = inode->i_sb;
121 	struct buffer_head *bh = NULL;
122 	struct fscrypt_str fstr = FSTR_INIT(NULL, 0);
123 
124 	if (IS_ENCRYPTED(inode)) {
125 		err = fscrypt_get_encryption_info(inode);
126 		if (err)
127 			return err;
128 	}
129 
130 	if (is_dx_dir(inode)) {
131 		err = ext4_dx_readdir(file, ctx);
132 		if (err != ERR_BAD_DX_DIR) {
133 			return err;
134 		}
135 		/* Can we just clear INDEX flag to ignore htree information? */
136 		if (!ext4_has_metadata_csum(sb)) {
137 			/*
138 			 * We don't set the inode dirty flag since it's not
139 			 * critical that it gets flushed back to the disk.
140 			 */
141 			ext4_clear_inode_flag(inode, EXT4_INODE_INDEX);
142 		}
143 	}
144 
145 	if (ext4_has_inline_data(inode)) {
146 		int has_inline_data = 1;
147 		err = ext4_read_inline_dir(file, ctx,
148 					   &has_inline_data);
149 		if (has_inline_data)
150 			return err;
151 	}
152 
153 	if (IS_ENCRYPTED(inode)) {
154 		err = fscrypt_fname_alloc_buffer(EXT4_NAME_LEN, &fstr);
155 		if (err < 0)
156 			return err;
157 	}
158 
159 	while (ctx->pos < inode->i_size) {
160 		struct ext4_map_blocks map;
161 
162 		if (fatal_signal_pending(current)) {
163 			err = -ERESTARTSYS;
164 			goto errout;
165 		}
166 		cond_resched();
167 		offset = ctx->pos & (sb->s_blocksize - 1);
168 		map.m_lblk = ctx->pos >> EXT4_BLOCK_SIZE_BITS(sb);
169 		map.m_len = 1;
170 		err = ext4_map_blocks(NULL, inode, &map, 0);
171 		if (err == 0) {
172 			/* m_len should never be zero but let's avoid
173 			 * an infinite loop if it somehow is */
174 			if (map.m_len == 0)
175 				map.m_len = 1;
176 			ctx->pos += map.m_len * sb->s_blocksize;
177 			continue;
178 		}
179 		if (err > 0) {
180 			pgoff_t index = map.m_pblk >>
181 					(PAGE_SHIFT - inode->i_blkbits);
182 			if (!ra_has_index(&file->f_ra, index))
183 				page_cache_sync_readahead(
184 					sb->s_bdev->bd_inode->i_mapping,
185 					&file->f_ra, file,
186 					index, 1);
187 			file->f_ra.prev_pos = (loff_t)index << PAGE_SHIFT;
188 			bh = ext4_bread(NULL, inode, map.m_lblk, 0);
189 			if (IS_ERR(bh)) {
190 				err = PTR_ERR(bh);
191 				bh = NULL;
192 				goto errout;
193 			}
194 		}
195 
196 		if (!bh) {
197 			/* corrupt size?  Maybe no more blocks to read */
198 			if (ctx->pos > inode->i_blocks << 9)
199 				break;
200 			ctx->pos += sb->s_blocksize - offset;
201 			continue;
202 		}
203 
204 		/* Check the checksum */
205 		if (!buffer_verified(bh) &&
206 		    !ext4_dirblock_csum_verify(inode, bh)) {
207 			EXT4_ERROR_FILE(file, 0, "directory fails checksum "
208 					"at offset %llu",
209 					(unsigned long long)ctx->pos);
210 			ctx->pos += sb->s_blocksize - offset;
211 			brelse(bh);
212 			bh = NULL;
213 			continue;
214 		}
215 		set_buffer_verified(bh);
216 
217 		/* If the dir block has changed since the last call to
218 		 * readdir(2), then we might be pointing to an invalid
219 		 * dirent right now.  Scan from the start of the block
220 		 * to make sure. */
221 		if (!inode_eq_iversion(inode, file->f_version)) {
222 			for (i = 0; i < sb->s_blocksize && i < offset; ) {
223 				de = (struct ext4_dir_entry_2 *)
224 					(bh->b_data + i);
225 				/* It's too expensive to do a full
226 				 * dirent test each time round this
227 				 * loop, but we do have to test at
228 				 * least that it is non-zero.  A
229 				 * failure will be detected in the
230 				 * dirent test below. */
231 				if (ext4_rec_len_from_disk(de->rec_len,
232 					sb->s_blocksize) < EXT4_DIR_REC_LEN(1))
233 					break;
234 				i += ext4_rec_len_from_disk(de->rec_len,
235 							    sb->s_blocksize);
236 			}
237 			offset = i;
238 			ctx->pos = (ctx->pos & ~(sb->s_blocksize - 1))
239 				| offset;
240 			file->f_version = inode_query_iversion(inode);
241 		}
242 
243 		while (ctx->pos < inode->i_size
244 		       && offset < sb->s_blocksize) {
245 			de = (struct ext4_dir_entry_2 *) (bh->b_data + offset);
246 			if (ext4_check_dir_entry(inode, file, de, bh,
247 						 bh->b_data, bh->b_size,
248 						 offset)) {
249 				/*
250 				 * On error, skip to the next block
251 				 */
252 				ctx->pos = (ctx->pos |
253 						(sb->s_blocksize - 1)) + 1;
254 				break;
255 			}
256 			offset += ext4_rec_len_from_disk(de->rec_len,
257 					sb->s_blocksize);
258 			if (le32_to_cpu(de->inode)) {
259 				if (!IS_ENCRYPTED(inode)) {
260 					if (!dir_emit(ctx, de->name,
261 					    de->name_len,
262 					    le32_to_cpu(de->inode),
263 					    get_dtype(sb, de->file_type)))
264 						goto done;
265 				} else {
266 					int save_len = fstr.len;
267 					struct fscrypt_str de_name =
268 							FSTR_INIT(de->name,
269 								de->name_len);
270 
271 					/* Directory is encrypted */
272 					err = fscrypt_fname_disk_to_usr(inode,
273 						0, 0, &de_name, &fstr);
274 					de_name = fstr;
275 					fstr.len = save_len;
276 					if (err)
277 						goto errout;
278 					if (!dir_emit(ctx,
279 					    de_name.name, de_name.len,
280 					    le32_to_cpu(de->inode),
281 					    get_dtype(sb, de->file_type)))
282 						goto done;
283 				}
284 			}
285 			ctx->pos += ext4_rec_len_from_disk(de->rec_len,
286 						sb->s_blocksize);
287 		}
288 		if ((ctx->pos < inode->i_size) && !dir_relax_shared(inode))
289 			goto done;
290 		brelse(bh);
291 		bh = NULL;
292 		offset = 0;
293 	}
294 done:
295 	err = 0;
296 errout:
297 	fscrypt_fname_free_buffer(&fstr);
298 	brelse(bh);
299 	return err;
300 }
301 
is_32bit_api(void)302 static inline int is_32bit_api(void)
303 {
304 #ifdef CONFIG_COMPAT
305 	return in_compat_syscall();
306 #else
307 	return (BITS_PER_LONG == 32);
308 #endif
309 }
310 
311 /*
312  * These functions convert from the major/minor hash to an f_pos
313  * value for dx directories
314  *
315  * Upper layer (for example NFS) should specify FMODE_32BITHASH or
316  * FMODE_64BITHASH explicitly. On the other hand, we allow ext4 to be mounted
317  * directly on both 32-bit and 64-bit nodes, under such case, neither
318  * FMODE_32BITHASH nor FMODE_64BITHASH is specified.
319  */
hash2pos(struct file * filp,__u32 major,__u32 minor)320 static inline loff_t hash2pos(struct file *filp, __u32 major, __u32 minor)
321 {
322 	if ((filp->f_mode & FMODE_32BITHASH) ||
323 	    (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
324 		return major >> 1;
325 	else
326 		return ((__u64)(major >> 1) << 32) | (__u64)minor;
327 }
328 
pos2maj_hash(struct file * filp,loff_t pos)329 static inline __u32 pos2maj_hash(struct file *filp, loff_t pos)
330 {
331 	if ((filp->f_mode & FMODE_32BITHASH) ||
332 	    (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
333 		return (pos << 1) & 0xffffffff;
334 	else
335 		return ((pos >> 32) << 1) & 0xffffffff;
336 }
337 
pos2min_hash(struct file * filp,loff_t pos)338 static inline __u32 pos2min_hash(struct file *filp, loff_t pos)
339 {
340 	if ((filp->f_mode & FMODE_32BITHASH) ||
341 	    (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
342 		return 0;
343 	else
344 		return pos & 0xffffffff;
345 }
346 
347 /*
348  * Return 32- or 64-bit end-of-file for dx directories
349  */
ext4_get_htree_eof(struct file * filp)350 static inline loff_t ext4_get_htree_eof(struct file *filp)
351 {
352 	if ((filp->f_mode & FMODE_32BITHASH) ||
353 	    (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
354 		return EXT4_HTREE_EOF_32BIT;
355 	else
356 		return EXT4_HTREE_EOF_64BIT;
357 }
358 
359 
360 /*
361  * ext4_dir_llseek() calls generic_file_llseek_size to handle htree
362  * directories, where the "offset" is in terms of the filename hash
363  * value instead of the byte offset.
364  *
365  * Because we may return a 64-bit hash that is well beyond offset limits,
366  * we need to pass the max hash as the maximum allowable offset in
367  * the htree directory case.
368  *
369  * For non-htree, ext4_llseek already chooses the proper max offset.
370  */
ext4_dir_llseek(struct file * file,loff_t offset,int whence)371 static loff_t ext4_dir_llseek(struct file *file, loff_t offset, int whence)
372 {
373 	struct inode *inode = file->f_mapping->host;
374 	int dx_dir = is_dx_dir(inode);
375 	loff_t ret, htree_max = ext4_get_htree_eof(file);
376 
377 	if (likely(dx_dir))
378 		ret = generic_file_llseek_size(file, offset, whence,
379 						    htree_max, htree_max);
380 	else
381 		ret = ext4_llseek(file, offset, whence);
382 	file->f_version = inode_peek_iversion(inode) - 1;
383 	return ret;
384 }
385 
386 /*
387  * This structure holds the nodes of the red-black tree used to store
388  * the directory entry in hash order.
389  */
390 struct fname {
391 	__u32		hash;
392 	__u32		minor_hash;
393 	struct rb_node	rb_hash;
394 	struct fname	*next;
395 	__u32		inode;
396 	__u8		name_len;
397 	__u8		file_type;
398 	char		name[];
399 };
400 
401 /*
402  * This functoin implements a non-recursive way of freeing all of the
403  * nodes in the red-black tree.
404  */
free_rb_tree_fname(struct rb_root * root)405 static void free_rb_tree_fname(struct rb_root *root)
406 {
407 	struct fname *fname, *next;
408 
409 	rbtree_postorder_for_each_entry_safe(fname, next, root, rb_hash)
410 		while (fname) {
411 			struct fname *old = fname;
412 			fname = fname->next;
413 			kfree(old);
414 		}
415 
416 	*root = RB_ROOT;
417 }
418 
419 
ext4_htree_create_dir_info(struct file * filp,loff_t pos)420 static struct dir_private_info *ext4_htree_create_dir_info(struct file *filp,
421 							   loff_t pos)
422 {
423 	struct dir_private_info *p;
424 
425 	p = kzalloc(sizeof(*p), GFP_KERNEL);
426 	if (!p)
427 		return NULL;
428 	p->curr_hash = pos2maj_hash(filp, pos);
429 	p->curr_minor_hash = pos2min_hash(filp, pos);
430 	return p;
431 }
432 
ext4_htree_free_dir_info(struct dir_private_info * p)433 void ext4_htree_free_dir_info(struct dir_private_info *p)
434 {
435 	free_rb_tree_fname(&p->root);
436 	kfree(p);
437 }
438 
439 /*
440  * Given a directory entry, enter it into the fname rb tree.
441  *
442  * When filename encryption is enabled, the dirent will hold the
443  * encrypted filename, while the htree will hold decrypted filename.
444  * The decrypted filename is passed in via ent_name.  parameter.
445  */
ext4_htree_store_dirent(struct file * dir_file,__u32 hash,__u32 minor_hash,struct ext4_dir_entry_2 * dirent,struct fscrypt_str * ent_name)446 int ext4_htree_store_dirent(struct file *dir_file, __u32 hash,
447 			     __u32 minor_hash,
448 			    struct ext4_dir_entry_2 *dirent,
449 			    struct fscrypt_str *ent_name)
450 {
451 	struct rb_node **p, *parent = NULL;
452 	struct fname *fname, *new_fn;
453 	struct dir_private_info *info;
454 	int len;
455 
456 	info = dir_file->private_data;
457 	p = &info->root.rb_node;
458 
459 	/* Create and allocate the fname structure */
460 	len = sizeof(struct fname) + ent_name->len + 1;
461 	new_fn = kzalloc(len, GFP_KERNEL);
462 	if (!new_fn)
463 		return -ENOMEM;
464 	new_fn->hash = hash;
465 	new_fn->minor_hash = minor_hash;
466 	new_fn->inode = le32_to_cpu(dirent->inode);
467 	new_fn->name_len = ent_name->len;
468 	new_fn->file_type = dirent->file_type;
469 	memcpy(new_fn->name, ent_name->name, ent_name->len);
470 
471 	while (*p) {
472 		parent = *p;
473 		fname = rb_entry(parent, struct fname, rb_hash);
474 
475 		/*
476 		 * If the hash and minor hash match up, then we put
477 		 * them on a linked list.  This rarely happens...
478 		 */
479 		if ((new_fn->hash == fname->hash) &&
480 		    (new_fn->minor_hash == fname->minor_hash)) {
481 			new_fn->next = fname->next;
482 			fname->next = new_fn;
483 			return 0;
484 		}
485 
486 		if (new_fn->hash < fname->hash)
487 			p = &(*p)->rb_left;
488 		else if (new_fn->hash > fname->hash)
489 			p = &(*p)->rb_right;
490 		else if (new_fn->minor_hash < fname->minor_hash)
491 			p = &(*p)->rb_left;
492 		else /* if (new_fn->minor_hash > fname->minor_hash) */
493 			p = &(*p)->rb_right;
494 	}
495 
496 	rb_link_node(&new_fn->rb_hash, parent, p);
497 	rb_insert_color(&new_fn->rb_hash, &info->root);
498 	return 0;
499 }
500 
501 
502 
503 /*
504  * This is a helper function for ext4_dx_readdir.  It calls filldir
505  * for all entres on the fname linked list.  (Normally there is only
506  * one entry on the linked list, unless there are 62 bit hash collisions.)
507  */
call_filldir(struct file * file,struct dir_context * ctx,struct fname * fname)508 static int call_filldir(struct file *file, struct dir_context *ctx,
509 			struct fname *fname)
510 {
511 	struct dir_private_info *info = file->private_data;
512 	struct inode *inode = file_inode(file);
513 	struct super_block *sb = inode->i_sb;
514 
515 	if (!fname) {
516 		ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: comm %s: "
517 			 "called with null fname?!?", __func__, __LINE__,
518 			 inode->i_ino, current->comm);
519 		return 0;
520 	}
521 	ctx->pos = hash2pos(file, fname->hash, fname->minor_hash);
522 	while (fname) {
523 		if (!dir_emit(ctx, fname->name,
524 				fname->name_len,
525 				fname->inode,
526 				get_dtype(sb, fname->file_type))) {
527 			info->extra_fname = fname;
528 			return 1;
529 		}
530 		fname = fname->next;
531 	}
532 	return 0;
533 }
534 
ext4_dx_readdir(struct file * file,struct dir_context * ctx)535 static int ext4_dx_readdir(struct file *file, struct dir_context *ctx)
536 {
537 	struct dir_private_info *info = file->private_data;
538 	struct inode *inode = file_inode(file);
539 	struct fname *fname;
540 	int ret = 0;
541 
542 	if (!info) {
543 		info = ext4_htree_create_dir_info(file, ctx->pos);
544 		if (!info)
545 			return -ENOMEM;
546 		file->private_data = info;
547 	}
548 
549 	if (ctx->pos == ext4_get_htree_eof(file))
550 		return 0;	/* EOF */
551 
552 	/* Some one has messed with f_pos; reset the world */
553 	if (info->last_pos != ctx->pos) {
554 		free_rb_tree_fname(&info->root);
555 		info->curr_node = NULL;
556 		info->extra_fname = NULL;
557 		info->curr_hash = pos2maj_hash(file, ctx->pos);
558 		info->curr_minor_hash = pos2min_hash(file, ctx->pos);
559 	}
560 
561 	/*
562 	 * If there are any leftover names on the hash collision
563 	 * chain, return them first.
564 	 */
565 	if (info->extra_fname) {
566 		if (call_filldir(file, ctx, info->extra_fname))
567 			goto finished;
568 		info->extra_fname = NULL;
569 		goto next_node;
570 	} else if (!info->curr_node)
571 		info->curr_node = rb_first(&info->root);
572 
573 	while (1) {
574 		/*
575 		 * Fill the rbtree if we have no more entries,
576 		 * or the inode has changed since we last read in the
577 		 * cached entries.
578 		 */
579 		if ((!info->curr_node) ||
580 		    !inode_eq_iversion(inode, file->f_version)) {
581 			info->curr_node = NULL;
582 			free_rb_tree_fname(&info->root);
583 			file->f_version = inode_query_iversion(inode);
584 			ret = ext4_htree_fill_tree(file, info->curr_hash,
585 						   info->curr_minor_hash,
586 						   &info->next_hash);
587 			if (ret < 0)
588 				goto finished;
589 			if (ret == 0) {
590 				ctx->pos = ext4_get_htree_eof(file);
591 				break;
592 			}
593 			info->curr_node = rb_first(&info->root);
594 		}
595 
596 		fname = rb_entry(info->curr_node, struct fname, rb_hash);
597 		info->curr_hash = fname->hash;
598 		info->curr_minor_hash = fname->minor_hash;
599 		if (call_filldir(file, ctx, fname))
600 			break;
601 	next_node:
602 		info->curr_node = rb_next(info->curr_node);
603 		if (info->curr_node) {
604 			fname = rb_entry(info->curr_node, struct fname,
605 					 rb_hash);
606 			info->curr_hash = fname->hash;
607 			info->curr_minor_hash = fname->minor_hash;
608 		} else {
609 			if (info->next_hash == ~0) {
610 				ctx->pos = ext4_get_htree_eof(file);
611 				break;
612 			}
613 			info->curr_hash = info->next_hash;
614 			info->curr_minor_hash = 0;
615 		}
616 	}
617 finished:
618 	info->last_pos = ctx->pos;
619 	return ret < 0 ? ret : 0;
620 }
621 
ext4_dir_open(struct inode * inode,struct file * filp)622 static int ext4_dir_open(struct inode * inode, struct file * filp)
623 {
624 	if (IS_ENCRYPTED(inode))
625 		return fscrypt_get_encryption_info(inode) ? -EACCES : 0;
626 	return 0;
627 }
628 
ext4_release_dir(struct inode * inode,struct file * filp)629 static int ext4_release_dir(struct inode *inode, struct file *filp)
630 {
631 	if (filp->private_data)
632 		ext4_htree_free_dir_info(filp->private_data);
633 
634 	return 0;
635 }
636 
ext4_check_all_de(struct inode * dir,struct buffer_head * bh,void * buf,int buf_size)637 int ext4_check_all_de(struct inode *dir, struct buffer_head *bh, void *buf,
638 		      int buf_size)
639 {
640 	struct ext4_dir_entry_2 *de;
641 	int rlen;
642 	unsigned int offset = 0;
643 	char *top;
644 
645 	de = (struct ext4_dir_entry_2 *)buf;
646 	top = buf + buf_size;
647 	while ((char *) de < top) {
648 		if (ext4_check_dir_entry(dir, NULL, de, bh,
649 					 buf, buf_size, offset))
650 			return -EFSCORRUPTED;
651 		rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
652 		de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
653 		offset += rlen;
654 	}
655 	if ((char *) de > top)
656 		return -EFSCORRUPTED;
657 
658 	return 0;
659 }
660 
661 const struct file_operations ext4_dir_operations = {
662 	.llseek		= ext4_dir_llseek,
663 	.read		= generic_read_dir,
664 	.iterate_shared	= ext4_readdir,
665 	.unlocked_ioctl = ext4_ioctl,
666 #ifdef CONFIG_COMPAT
667 	.compat_ioctl	= ext4_compat_ioctl,
668 #endif
669 	.fsync		= ext4_sync_file,
670 	.open		= ext4_dir_open,
671 	.release	= ext4_release_dir,
672 };
673 
674 #ifdef CONFIG_UNICODE
675 const struct dentry_operations ext4_dentry_ops = {
676 	.d_hash = generic_ci_d_hash,
677 	.d_compare = generic_ci_d_compare,
678 };
679 #endif
680