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