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