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