xref: /linux/fs/ext4/namei.c (revision dedf8d3fe774ccfb0cbe5b4d07cecd46a62f48bd)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  linux/fs/ext4/namei.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/namei.c
13  *
14  *  Copyright (C) 1991, 1992  Linus Torvalds
15  *
16  *  Big-endian to little-endian byte-swapping/bitmaps by
17  *        David S. Miller (davem@caip.rutgers.edu), 1995
18  *  Directory entry file type support and forward compatibility hooks
19  *	for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
20  *  Hash Tree Directory indexing (c)
21  *	Daniel Phillips, 2001
22  *  Hash Tree Directory indexing porting
23  *	Christopher Li, 2002
24  *  Hash Tree Directory indexing cleanup
25  *	Theodore Ts'o, 2002
26  */
27 
28 #include <linux/fs.h>
29 #include <linux/pagemap.h>
30 #include <linux/time.h>
31 #include <linux/fcntl.h>
32 #include <linux/stat.h>
33 #include <linux/string.h>
34 #include <linux/quotaops.h>
35 #include <linux/buffer_head.h>
36 #include <linux/bio.h>
37 #include <linux/iversion.h>
38 #include <linux/unicode.h>
39 #include <kunit/static_stub.h>
40 #include "ext4.h"
41 #include "ext4_jbd2.h"
42 
43 #include "xattr.h"
44 #include "acl.h"
45 #include "namei.h"
46 
47 #include <trace/events/ext4.h>
48 /*
49  * define how far ahead to read directories while searching them.
50  */
51 #define NAMEI_RA_CHUNKS  2
52 #define NAMEI_RA_BLOCKS  4
53 #define NAMEI_RA_SIZE	     (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
54 
55 static struct buffer_head *ext4_append(handle_t *handle,
56 					struct inode *inode,
57 					ext4_lblk_t *block)
58 {
59 	struct ext4_map_blocks map;
60 	struct buffer_head *bh;
61 	int err;
62 
63 	if (unlikely(EXT4_SB(inode->i_sb)->s_max_dir_size_kb &&
64 		     ((inode->i_size >> 10) >=
65 		      EXT4_SB(inode->i_sb)->s_max_dir_size_kb)))
66 		return ERR_PTR(-ENOSPC);
67 
68 	*block = inode->i_size >> inode->i_sb->s_blocksize_bits;
69 	map.m_lblk = *block;
70 	map.m_len = 1;
71 
72 	/*
73 	 * We're appending new directory block. Make sure the block is not
74 	 * allocated yet, otherwise we will end up corrupting the
75 	 * directory.
76 	 */
77 	err = ext4_map_blocks(NULL, inode, &map, 0);
78 	if (err < 0)
79 		return ERR_PTR(err);
80 	if (err) {
81 		EXT4_ERROR_INODE(inode, "Logical block already allocated");
82 		return ERR_PTR(-EFSCORRUPTED);
83 	}
84 
85 	bh = ext4_bread(handle, inode, *block, EXT4_GET_BLOCKS_CREATE);
86 	if (IS_ERR(bh))
87 		return bh;
88 	inode->i_size += inode->i_sb->s_blocksize;
89 	EXT4_I(inode)->i_disksize = inode->i_size;
90 	err = ext4_mark_inode_dirty(handle, inode);
91 	if (err)
92 		goto out;
93 	BUFFER_TRACE(bh, "get_write_access");
94 	err = ext4_journal_get_write_access(handle, inode->i_sb, bh,
95 					    EXT4_JTR_NONE);
96 	if (err)
97 		goto out;
98 	return bh;
99 
100 out:
101 	brelse(bh);
102 	ext4_std_error(inode->i_sb, err);
103 	return ERR_PTR(err);
104 }
105 
106 static int ext4_dx_csum_verify(struct inode *inode,
107 			       struct ext4_dir_entry *dirent);
108 
109 #define ext4_read_dirblock(inode, block, type) \
110 	__ext4_read_dirblock((inode), (block), (type), __func__, __LINE__)
111 
112 static struct buffer_head *__ext4_read_dirblock(struct inode *inode,
113 						ext4_lblk_t block,
114 						enum dirblock_type type,
115 						const char *func,
116 						unsigned int line)
117 {
118 	struct buffer_head *bh;
119 	struct ext4_dir_entry *dirent;
120 	int is_dx_block = 0;
121 
122 	if (block >= inode->i_size >> inode->i_blkbits) {
123 		ext4_error_inode(inode, func, line, block,
124 		       "Attempting to read directory block (%u) that is past i_size (%llu)",
125 		       block, inode->i_size);
126 		return ERR_PTR(-EFSCORRUPTED);
127 	}
128 
129 	if (ext4_simulate_fail(inode->i_sb, EXT4_SIM_DIRBLOCK_EIO))
130 		bh = ERR_PTR(-EIO);
131 	else
132 		bh = ext4_bread(NULL, inode, block, 0);
133 	if (IS_ERR(bh)) {
134 		__ext4_warning(inode->i_sb, func, line,
135 			       "inode #%llu: lblock %lu: comm %s: "
136 			       "error %pe reading directory block",
137 			       inode->i_ino, (unsigned long)block,
138 			       current->comm, bh);
139 
140 		return bh;
141 	}
142 	/* The first directory block must not be a hole. */
143 	if (!bh && (type == INDEX || type == DIRENT_HTREE || block == 0)) {
144 		ext4_error_inode(inode, func, line, block,
145 				 "Directory hole found for htree %s block %u",
146 				 (type == INDEX) ? "index" : "leaf", block);
147 		return ERR_PTR(-EFSCORRUPTED);
148 	}
149 	if (!bh)
150 		return NULL;
151 	dirent = (struct ext4_dir_entry *) bh->b_data;
152 	/* Determine whether or not we have an index block */
153 	if (is_dx(inode)) {
154 		if (block == 0)
155 			is_dx_block = 1;
156 		else if (ext4_rec_len_from_disk(dirent->rec_len,
157 						inode->i_sb->s_blocksize) ==
158 			 inode->i_sb->s_blocksize)
159 			is_dx_block = 1;
160 	}
161 	if (!is_dx_block && type == INDEX) {
162 		ext4_error_inode(inode, func, line, block,
163 		       "directory leaf block found instead of index block");
164 		brelse(bh);
165 		return ERR_PTR(-EFSCORRUPTED);
166 	}
167 	if (!ext4_has_feature_metadata_csum(inode->i_sb) ||
168 	    buffer_verified(bh))
169 		return bh;
170 
171 	/*
172 	 * An empty leaf block can get mistaken for a index block; for
173 	 * this reason, we can only check the index checksum when the
174 	 * caller is sure it should be an index block.
175 	 */
176 	if (is_dx_block && type == INDEX) {
177 		if (ext4_dx_csum_verify(inode, dirent) &&
178 		    !ext4_simulate_fail(inode->i_sb, EXT4_SIM_DIRBLOCK_CRC))
179 			set_buffer_verified(bh);
180 		else {
181 			ext4_error_inode_err(inode, func, line, block,
182 					     EFSBADCRC,
183 					     "Directory index failed checksum");
184 			brelse(bh);
185 			return ERR_PTR(-EFSBADCRC);
186 		}
187 	}
188 	if (!is_dx_block) {
189 		if (ext4_dirblock_csum_verify(inode, bh) &&
190 		    !ext4_simulate_fail(inode->i_sb, EXT4_SIM_DIRBLOCK_CRC))
191 			set_buffer_verified(bh);
192 		else {
193 			ext4_error_inode_err(inode, func, line, block,
194 					     EFSBADCRC,
195 					     "Directory block failed checksum");
196 			brelse(bh);
197 			return ERR_PTR(-EFSBADCRC);
198 		}
199 	}
200 	return bh;
201 }
202 
203 #ifdef DX_DEBUG
204 #define dxtrace(command) command
205 #else
206 #define dxtrace(command)
207 #endif
208 
209 static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
210 		struct ext4_filename *fname,
211 		struct ext4_dir_entry_2 **res_dir);
212 static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
213 			     struct inode *dir, struct inode *inode);
214 
215 /* checksumming functions */
216 void ext4_initialize_dirent_tail(struct buffer_head *bh,
217 				 unsigned int blocksize)
218 {
219 	struct ext4_dir_entry_tail *t = EXT4_DIRENT_TAIL(bh->b_data, blocksize);
220 
221 	memset(t, 0, sizeof(struct ext4_dir_entry_tail));
222 	t->det_rec_len = ext4_rec_len_to_disk(
223 			sizeof(struct ext4_dir_entry_tail), blocksize);
224 	t->det_reserved_ft = EXT4_FT_DIR_CSUM;
225 }
226 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_initialize_dirent_tail);
227 
228 /* Walk through a dirent block to find a checksum "dirent" at the tail */
229 static struct ext4_dir_entry_tail *get_dirent_tail(struct inode *inode,
230 						   struct buffer_head *bh)
231 {
232 	struct ext4_dir_entry_tail *t;
233 	int blocksize = EXT4_BLOCK_SIZE(inode->i_sb);
234 
235 #ifdef PARANOID
236 	struct ext4_dir_entry *d, *top;
237 
238 	d = (struct ext4_dir_entry *)bh->b_data;
239 	top = (struct ext4_dir_entry *)(bh->b_data +
240 		(blocksize - sizeof(struct ext4_dir_entry_tail)));
241 	while (d < top && ext4_rec_len_from_disk(d->rec_len, blocksize))
242 		d = (struct ext4_dir_entry *)(((void *)d) +
243 		    ext4_rec_len_from_disk(d->rec_len, blocksize));
244 
245 	if (d != top)
246 		return NULL;
247 
248 	t = (struct ext4_dir_entry_tail *)d;
249 #else
250 	t = EXT4_DIRENT_TAIL(bh->b_data, EXT4_BLOCK_SIZE(inode->i_sb));
251 #endif
252 
253 	if (t->det_reserved_zero1 ||
254 	    (ext4_rec_len_from_disk(t->det_rec_len, blocksize) !=
255 	     sizeof(struct ext4_dir_entry_tail)) ||
256 	    t->det_reserved_zero2 ||
257 	    t->det_reserved_ft != EXT4_FT_DIR_CSUM)
258 		return NULL;
259 
260 	return t;
261 }
262 
263 static __le32 ext4_dirblock_csum(struct inode *inode, void *dirent, int size)
264 {
265 	struct ext4_inode_info *ei = EXT4_I(inode);
266 	__u32 csum;
267 
268 	csum = ext4_chksum(ei->i_csum_seed, (__u8 *)dirent, size);
269 	return cpu_to_le32(csum);
270 }
271 
272 #define warn_no_space_for_csum(inode)					\
273 	__warn_no_space_for_csum((inode), __func__, __LINE__)
274 
275 static void __warn_no_space_for_csum(struct inode *inode, const char *func,
276 				     unsigned int line)
277 {
278 	__ext4_warning_inode(inode, func, line,
279 		"No space for directory leaf checksum. Please run e2fsck -D.");
280 }
281 
282 int ext4_dirblock_csum_verify(struct inode *inode, struct buffer_head *bh)
283 {
284 	struct ext4_dir_entry_tail *t;
285 
286 	if (!ext4_has_feature_metadata_csum(inode->i_sb))
287 		return 1;
288 
289 	t = get_dirent_tail(inode, bh);
290 	if (!t) {
291 		warn_no_space_for_csum(inode);
292 		return 0;
293 	}
294 
295 	if (t->det_checksum != ext4_dirblock_csum(inode, bh->b_data,
296 						  (char *)t - bh->b_data))
297 		return 0;
298 
299 	return 1;
300 }
301 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_dirblock_csum_verify);
302 
303 static void ext4_dirblock_csum_set(struct inode *inode,
304 				 struct buffer_head *bh)
305 {
306 	struct ext4_dir_entry_tail *t;
307 
308 	if (!ext4_has_feature_metadata_csum(inode->i_sb))
309 		return;
310 
311 	t = get_dirent_tail(inode, bh);
312 	if (!t) {
313 		warn_no_space_for_csum(inode);
314 		return;
315 	}
316 
317 	t->det_checksum = ext4_dirblock_csum(inode, bh->b_data,
318 					     (char *)t - bh->b_data);
319 }
320 
321 int ext4_handle_dirty_dirblock(handle_t *handle,
322 			       struct inode *inode,
323 			       struct buffer_head *bh)
324 {
325 	ext4_dirblock_csum_set(inode, bh);
326 	return ext4_handle_dirty_metadata(handle, inode, bh);
327 }
328 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_handle_dirty_dirblock);
329 
330 static struct dx_countlimit *get_dx_countlimit(struct inode *inode,
331 					       struct ext4_dir_entry *dirent,
332 					       int *offset)
333 {
334 	struct ext4_dir_entry *dp;
335 	struct dx_root_info *root;
336 	int count_offset;
337 	int blocksize = EXT4_BLOCK_SIZE(inode->i_sb);
338 	unsigned int rlen = ext4_rec_len_from_disk(dirent->rec_len, blocksize);
339 
340 	if (rlen == blocksize)
341 		count_offset = 8;
342 	else if (rlen == 12) {
343 		dp = (struct ext4_dir_entry *)(((void *)dirent) + 12);
344 		if (ext4_rec_len_from_disk(dp->rec_len, blocksize) != blocksize - 12)
345 			return NULL;
346 		root = (struct dx_root_info *)(((void *)dp + 12));
347 		if (root->reserved_zero ||
348 		    root->info_length != sizeof(struct dx_root_info))
349 			return NULL;
350 		count_offset = 32;
351 	} else
352 		return NULL;
353 
354 	if (offset)
355 		*offset = count_offset;
356 	return (struct dx_countlimit *)(((void *)dirent) + count_offset);
357 }
358 
359 static __le32 ext4_dx_csum(struct inode *inode, struct ext4_dir_entry *dirent,
360 			   int count_offset, int count, struct dx_tail *t)
361 {
362 	struct ext4_inode_info *ei = EXT4_I(inode);
363 	__u32 csum;
364 	int size;
365 	__u32 dummy_csum = 0;
366 	int offset = offsetof(struct dx_tail, dt_checksum);
367 
368 	size = count_offset + (count * sizeof(struct dx_entry));
369 	csum = ext4_chksum(ei->i_csum_seed, (__u8 *)dirent, size);
370 	csum = ext4_chksum(csum, (__u8 *)t, offset);
371 	csum = ext4_chksum(csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
372 
373 	return cpu_to_le32(csum);
374 }
375 
376 static int ext4_dx_csum_verify(struct inode *inode,
377 			       struct ext4_dir_entry *dirent)
378 {
379 	struct dx_countlimit *c;
380 	struct dx_tail *t;
381 	int count_offset, limit, count;
382 
383 	if (!ext4_has_feature_metadata_csum(inode->i_sb))
384 		return 1;
385 
386 	c = get_dx_countlimit(inode, dirent, &count_offset);
387 	if (!c) {
388 		EXT4_ERROR_INODE(inode, "dir seems corrupt?  Run e2fsck -D.");
389 		return 0;
390 	}
391 	limit = le16_to_cpu(c->limit);
392 	count = le16_to_cpu(c->count);
393 	if (count_offset + (limit * sizeof(struct dx_entry)) >
394 	    EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
395 		warn_no_space_for_csum(inode);
396 		return 0;
397 	}
398 	t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
399 
400 	if (t->dt_checksum != ext4_dx_csum(inode, dirent, count_offset,
401 					    count, t))
402 		return 0;
403 	return 1;
404 }
405 
406 static void ext4_dx_csum_set(struct inode *inode, struct ext4_dir_entry *dirent)
407 {
408 	struct dx_countlimit *c;
409 	struct dx_tail *t;
410 	int count_offset, limit, count;
411 
412 	if (!ext4_has_feature_metadata_csum(inode->i_sb))
413 		return;
414 
415 	c = get_dx_countlimit(inode, dirent, &count_offset);
416 	if (!c) {
417 		EXT4_ERROR_INODE(inode, "dir seems corrupt?  Run e2fsck -D.");
418 		return;
419 	}
420 	limit = le16_to_cpu(c->limit);
421 	count = le16_to_cpu(c->count);
422 	if (count_offset + (limit * sizeof(struct dx_entry)) >
423 	    EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
424 		warn_no_space_for_csum(inode);
425 		return;
426 	}
427 	t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
428 
429 	t->dt_checksum = ext4_dx_csum(inode, dirent, count_offset, count, t);
430 }
431 
432 static inline int ext4_handle_dirty_dx_node(handle_t *handle,
433 					    struct inode *inode,
434 					    struct buffer_head *bh)
435 {
436 	ext4_dx_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
437 	return ext4_handle_dirty_metadata(handle, inode, bh);
438 }
439 
440 /*
441  * Debug
442  */
443 #ifdef DX_DEBUG
444 static void dx_show_index(char * label, struct dx_entry *entries)
445 {
446 	int i, n = dx_get_count (entries);
447 	printk(KERN_DEBUG "%s index", label);
448 	for (i = 0; i < n; i++) {
449 		printk(KERN_CONT " %x->%lu",
450 		       i ? dx_get_hash(entries + i) : 0,
451 		       (unsigned long)dx_get_block(entries + i));
452 	}
453 	printk(KERN_CONT "\n");
454 }
455 
456 struct stats
457 {
458 	unsigned names;
459 	unsigned space;
460 	unsigned bcount;
461 };
462 
463 static struct stats dx_show_leaf(struct inode *dir,
464 				struct dx_hash_info *hinfo,
465 				struct ext4_dir_entry_2 *de,
466 				int size, int show_names)
467 {
468 	unsigned names = 0, space = 0;
469 	char *base = (char *) de;
470 	struct dx_hash_info h = *hinfo;
471 
472 	printk("names: ");
473 	while ((char *) de < base + size)
474 	{
475 		if (de->inode)
476 		{
477 			if (show_names)
478 			{
479 #ifdef CONFIG_FS_ENCRYPTION
480 				int len;
481 				char *name;
482 				struct fscrypt_str fname_crypto_str =
483 					FSTR_INIT(NULL, 0);
484 				int res = 0;
485 
486 				name  = de->name;
487 				len = de->name_len;
488 				if (!IS_ENCRYPTED(dir)) {
489 					/* Directory is not encrypted */
490 					(void) ext4fs_dirhash(dir, de->name,
491 						de->name_len, &h);
492 					printk("%.*s:(U)%x.%u ", len,
493 					       name, h.hash,
494 					       (unsigned) ((char *) de
495 							   - base));
496 				} else {
497 					struct fscrypt_str de_name =
498 						FSTR_INIT(name, len);
499 
500 					/* Directory is encrypted */
501 					res = fscrypt_fname_alloc_buffer(
502 						len, &fname_crypto_str);
503 					if (res)
504 						printk(KERN_WARNING "Error "
505 							"allocating crypto "
506 							"buffer--skipping "
507 							"crypto\n");
508 					res = fscrypt_fname_disk_to_usr(dir,
509 						0, 0, &de_name,
510 						&fname_crypto_str);
511 					if (res) {
512 						printk(KERN_WARNING "Error "
513 							"converting filename "
514 							"from disk to usr"
515 							"\n");
516 						name = "??";
517 						len = 2;
518 					} else {
519 						name = fname_crypto_str.name;
520 						len = fname_crypto_str.len;
521 					}
522 					if (IS_CASEFOLDED(dir))
523 						h.hash = EXT4_DIRENT_HASH(de);
524 					else
525 						(void) ext4fs_dirhash(dir,
526 							de->name,
527 							de->name_len, &h);
528 					printk("%.*s:(E)%x.%u ", len, name,
529 					       h.hash, (unsigned) ((char *) de
530 								   - base));
531 					fscrypt_fname_free_buffer(
532 							&fname_crypto_str);
533 				}
534 #else
535 				int len = de->name_len;
536 				char *name = de->name;
537 				(void) ext4fs_dirhash(dir, de->name,
538 						      de->name_len, &h);
539 				printk("%.*s:%x.%u ", len, name, h.hash,
540 				       (unsigned) ((char *) de - base));
541 #endif
542 			}
543 			space += ext4_dir_rec_len(de->name_len, dir);
544 			names++;
545 		}
546 		de = ext4_next_entry(de, size);
547 	}
548 	printk(KERN_CONT "(%i)\n", names);
549 	return (struct stats) { names, space, 1 };
550 }
551 
552 struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
553 			     struct dx_entry *entries, int levels)
554 {
555 	unsigned blocksize = dir->i_sb->s_blocksize;
556 	unsigned count = dx_get_count(entries), names = 0, space = 0, i;
557 	unsigned bcount = 0;
558 	struct buffer_head *bh;
559 	printk("%i indexed blocks...\n", count);
560 	for (i = 0; i < count; i++, entries++)
561 	{
562 		ext4_lblk_t block = dx_get_block(entries);
563 		ext4_lblk_t hash  = i ? dx_get_hash(entries): 0;
564 		u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
565 		struct stats stats;
566 		printk("%s%3u:%03u hash %8x/%8x ",levels?"":"   ", i, block, hash, range);
567 		bh = ext4_bread(NULL,dir, block, 0);
568 		if (IS_ERR_OR_NULL(bh))
569 			continue;
570 		stats = levels?
571 		   dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
572 		   dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *)
573 			bh->b_data, blocksize, 0);
574 		names += stats.names;
575 		space += stats.space;
576 		bcount += stats.bcount;
577 		brelse(bh);
578 	}
579 	if (bcount)
580 		printk(KERN_DEBUG "%snames %u, fullness %u (%u%%)\n",
581 		       levels ? "" : "   ", names, space/bcount,
582 		       (space/bcount)*100/blocksize);
583 	return (struct stats) { names, space, bcount};
584 }
585 
586 /*
587  * Linear search cross check
588  */
589 static inline void htree_rep_invariant_check(struct dx_entry *at,
590 					     struct dx_entry *target,
591 					     u32 hash, unsigned int n)
592 {
593 	while (n--) {
594 		dxtrace(printk(KERN_CONT ","));
595 		if (dx_get_hash(++at) > hash) {
596 			at--;
597 			break;
598 		}
599 	}
600 	ASSERT(at == target - 1);
601 }
602 #else /* DX_DEBUG */
603 static inline void htree_rep_invariant_check(struct dx_entry *at,
604 					     struct dx_entry *target,
605 					     u32 hash, unsigned int n)
606 {
607 }
608 #endif /* DX_DEBUG */
609 
610 /*
611  * Probe for a directory leaf block to search.
612  *
613  * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
614  * error in the directory index, and the caller should fall back to
615  * searching the directory normally.  The callers of dx_probe **MUST**
616  * check for this error code, and make sure it never gets reflected
617  * back to userspace.
618  */
619 static struct dx_frame *
620 dx_probe(struct ext4_filename *fname, struct inode *dir,
621 	 struct dx_hash_info *hinfo, struct dx_frame *frame_in)
622 {
623 	unsigned count, indirect, level, i;
624 	struct dx_entry *at, *entries, *p, *q, *m;
625 	struct dx_root *root;
626 	struct dx_frame *frame = frame_in;
627 	struct dx_frame *ret_err = ERR_PTR(ERR_BAD_DX_DIR);
628 	u32 hash;
629 	ext4_lblk_t block;
630 	ext4_lblk_t blocks[EXT4_HTREE_LEVEL];
631 
632 	memset(frame_in, 0, EXT4_HTREE_LEVEL * sizeof(frame_in[0]));
633 	frame->bh = ext4_read_dirblock(dir, 0, INDEX);
634 	if (IS_ERR(frame->bh))
635 		return (struct dx_frame *) frame->bh;
636 
637 	root = (struct dx_root *) frame->bh->b_data;
638 	if (root->info.hash_version != DX_HASH_TEA &&
639 	    root->info.hash_version != DX_HASH_HALF_MD4 &&
640 	    root->info.hash_version != DX_HASH_LEGACY &&
641 	    root->info.hash_version != DX_HASH_SIPHASH) {
642 		ext4_warning_inode(dir, "Unrecognised inode hash code %u",
643 				   root->info.hash_version);
644 		goto fail;
645 	}
646 	if (ext4_hash_in_dirent(dir)) {
647 		if (root->info.hash_version != DX_HASH_SIPHASH) {
648 			ext4_warning_inode(dir,
649 				"Hash in dirent, but hash is not SIPHASH");
650 			goto fail;
651 		}
652 	} else {
653 		if (root->info.hash_version == DX_HASH_SIPHASH) {
654 			ext4_warning_inode(dir,
655 				"Hash code is SIPHASH, but hash not in dirent");
656 			goto fail;
657 		}
658 	}
659 	if (fname)
660 		hinfo = &fname->hinfo;
661 	hinfo->hash_version = root->info.hash_version;
662 	if (hinfo->hash_version <= DX_HASH_TEA)
663 		hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
664 	hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
665 	/* hash is already computed for encrypted casefolded directory */
666 	if (fname && fname_name(fname) &&
667 	    !(IS_ENCRYPTED(dir) && IS_CASEFOLDED(dir))) {
668 		int ret = ext4fs_dirhash(dir, fname_name(fname),
669 					 fname_len(fname), hinfo);
670 		if (ret < 0) {
671 			ret_err = ERR_PTR(ret);
672 			goto fail;
673 		}
674 	}
675 	hash = hinfo->hash;
676 
677 	if (root->info.unused_flags & 1) {
678 		ext4_warning_inode(dir, "Unimplemented hash flags: %#06x",
679 				   root->info.unused_flags);
680 		goto fail;
681 	}
682 
683 	indirect = root->info.indirect_levels;
684 	if (indirect >= ext4_dir_htree_level(dir->i_sb)) {
685 		ext4_warning(dir->i_sb,
686 			     "Directory (ino: %llu) htree depth %#06x exceed"
687 			     "supported value", dir->i_ino,
688 			     ext4_dir_htree_level(dir->i_sb));
689 		if (ext4_dir_htree_level(dir->i_sb) < EXT4_HTREE_LEVEL) {
690 			ext4_warning(dir->i_sb, "Enable large directory "
691 						"feature to access it");
692 		}
693 		goto fail;
694 	}
695 
696 	entries = (struct dx_entry *)(((char *)&root->info) +
697 				      root->info.info_length);
698 
699 	if (dx_get_limit(entries) != dx_root_limit(dir,
700 						   root->info.info_length)) {
701 		ext4_warning_inode(dir, "dx entry: limit %u != root limit %u",
702 				   dx_get_limit(entries),
703 				   dx_root_limit(dir, root->info.info_length));
704 		goto fail;
705 	}
706 
707 	dxtrace(printk("Look up %x", hash));
708 	level = 0;
709 	blocks[0] = 0;
710 	while (1) {
711 		count = dx_get_count(entries);
712 		if (!count || count > dx_get_limit(entries)) {
713 			ext4_warning_inode(dir,
714 					   "dx entry: count %u beyond limit %u",
715 					   count, dx_get_limit(entries));
716 			goto fail;
717 		}
718 
719 		p = entries + 1;
720 		q = entries + count - 1;
721 		while (p <= q) {
722 			m = p + (q - p) / 2;
723 			dxtrace(printk(KERN_CONT "."));
724 			if (dx_get_hash(m) > hash)
725 				q = m - 1;
726 			else
727 				p = m + 1;
728 		}
729 
730 		htree_rep_invariant_check(entries, p, hash, count - 1);
731 
732 		at = p - 1;
733 		dxtrace(printk(KERN_CONT " %x->%u\n",
734 			       at == entries ? 0 : dx_get_hash(at),
735 			       dx_get_block(at)));
736 		frame->entries = entries;
737 		frame->at = at;
738 
739 		block = dx_get_block(at);
740 		for (i = 0; i <= level; i++) {
741 			if (blocks[i] == block) {
742 				ext4_warning_inode(dir,
743 					"dx entry: tree cycle block %u points back to block %u",
744 					blocks[level], block);
745 				goto fail;
746 			}
747 		}
748 		if (++level > indirect)
749 			return frame;
750 		blocks[level] = block;
751 		frame++;
752 		frame->bh = ext4_read_dirblock(dir, block, INDEX);
753 		if (IS_ERR(frame->bh)) {
754 			ret_err = (struct dx_frame *) frame->bh;
755 			frame->bh = NULL;
756 			goto fail;
757 		}
758 
759 		entries = ((struct dx_node *) frame->bh->b_data)->entries;
760 
761 		if (dx_get_limit(entries) != dx_node_limit(dir)) {
762 			ext4_warning_inode(dir,
763 				"dx entry: limit %u != node limit %u",
764 				dx_get_limit(entries), dx_node_limit(dir));
765 			goto fail;
766 		}
767 	}
768 fail:
769 	while (frame >= frame_in) {
770 		brelse(frame->bh);
771 		frame--;
772 	}
773 
774 	if (ret_err == ERR_PTR(ERR_BAD_DX_DIR))
775 		ext4_warning_inode(dir,
776 			"Corrupt directory, running e2fsck is recommended");
777 	return ret_err;
778 }
779 
780 static void dx_release(struct dx_frame *frames)
781 {
782 	struct dx_root_info *info;
783 	int i;
784 	unsigned int indirect_levels;
785 
786 	if (frames[0].bh == NULL)
787 		return;
788 
789 	info = &((struct dx_root *)frames[0].bh->b_data)->info;
790 	/* save local copy, "info" may be freed after brelse() */
791 	indirect_levels = info->indirect_levels;
792 	for (i = 0; i <= indirect_levels; i++) {
793 		if (frames[i].bh == NULL)
794 			break;
795 		brelse(frames[i].bh);
796 		frames[i].bh = NULL;
797 	}
798 }
799 
800 /*
801  * This function increments the frame pointer to search the next leaf
802  * block, and reads in the necessary intervening nodes if the search
803  * should be necessary.  Whether or not the search is necessary is
804  * controlled by the hash parameter.  If the hash value is even, then
805  * the search is only continued if the next block starts with that
806  * hash value.  This is used if we are searching for a specific file.
807  *
808  * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
809  *
810  * This function returns 1 if the caller should continue to search,
811  * or 0 if it should not.  If there is an error reading one of the
812  * index blocks, it will a negative error code.
813  *
814  * If start_hash is non-null, it will be filled in with the starting
815  * hash of the next page.
816  */
817 static int ext4_htree_next_block(struct inode *dir, __u32 hash,
818 				 struct dx_frame *frame,
819 				 struct dx_frame *frames,
820 				 __u32 *start_hash)
821 {
822 	struct dx_frame *p;
823 	struct buffer_head *bh;
824 	int num_frames = 0;
825 	__u32 bhash;
826 
827 	p = frame;
828 	/*
829 	 * Find the next leaf page by incrementing the frame pointer.
830 	 * If we run out of entries in the interior node, loop around and
831 	 * increment pointer in the parent node.  When we break out of
832 	 * this loop, num_frames indicates the number of interior
833 	 * nodes need to be read.
834 	 */
835 	while (1) {
836 		if (++(p->at) < p->entries + dx_get_count(p->entries))
837 			break;
838 		if (p == frames)
839 			return 0;
840 		num_frames++;
841 		p--;
842 	}
843 
844 	/*
845 	 * If the hash is 1, then continue only if the next page has a
846 	 * continuation hash of any value.  This is used for readdir
847 	 * handling.  Otherwise, check to see if the hash matches the
848 	 * desired continuation hash.  If it doesn't, return since
849 	 * there's no point to read in the successive index pages.
850 	 */
851 	bhash = dx_get_hash(p->at);
852 	if (start_hash)
853 		*start_hash = bhash;
854 	if ((hash & 1) == 0) {
855 		if ((bhash & ~1) != hash)
856 			return 0;
857 	}
858 	/*
859 	 * If the hash is HASH_NB_ALWAYS, we always go to the next
860 	 * block so no check is necessary
861 	 */
862 	while (num_frames--) {
863 		bh = ext4_read_dirblock(dir, dx_get_block(p->at), INDEX);
864 		if (IS_ERR(bh))
865 			return PTR_ERR(bh);
866 		p++;
867 		brelse(p->bh);
868 		p->bh = bh;
869 		p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
870 	}
871 	return 1;
872 }
873 
874 
875 /*
876  * This function fills a red-black tree with information from a
877  * directory block.  It returns the number directory entries loaded
878  * into the tree.  If there is an error it is returned in err.
879  */
880 static int htree_dirblock_to_tree(struct file *dir_file,
881 				  struct inode *dir, ext4_lblk_t block,
882 				  struct dx_hash_info *hinfo,
883 				  __u32 start_hash, __u32 start_minor_hash)
884 {
885 	struct buffer_head *bh;
886 	struct ext4_dir_entry_2 *de, *top;
887 	int err = 0, count = 0;
888 	struct fscrypt_str fname_crypto_str = FSTR_INIT(NULL, 0), tmp_str;
889 	int csum = ext4_has_feature_metadata_csum(dir->i_sb);
890 
891 	dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
892 							(unsigned long)block));
893 	bh = ext4_read_dirblock(dir, block, DIRENT_HTREE);
894 	if (IS_ERR(bh))
895 		return PTR_ERR(bh);
896 
897 	de = (struct ext4_dir_entry_2 *) bh->b_data;
898 	/* csum entries are not larger in the casefolded encrypted case */
899 	top = (struct ext4_dir_entry_2 *) ((char *) de +
900 					   dir->i_sb->s_blocksize -
901 					   ext4_dir_rec_len(0,
902 							   csum ? NULL : dir));
903 	/* Check if the directory is encrypted */
904 	if (IS_ENCRYPTED(dir)) {
905 		err = fscrypt_prepare_readdir(dir);
906 		if (err < 0) {
907 			brelse(bh);
908 			return err;
909 		}
910 		err = fscrypt_fname_alloc_buffer(EXT4_NAME_LEN,
911 						 &fname_crypto_str);
912 		if (err < 0) {
913 			brelse(bh);
914 			return err;
915 		}
916 	}
917 
918 	for (; de < top; de = ext4_next_entry(de, dir->i_sb->s_blocksize)) {
919 		if (ext4_check_dir_entry(dir, NULL, de, bh,
920 				bh->b_data, bh->b_size,
921 				EXT4_LBLK_TO_B(dir, block)
922 					 + ((char *)de - bh->b_data))) {
923 			/* silently ignore the rest of the block */
924 			break;
925 		}
926 		if (ext4_hash_in_dirent(dir)) {
927 			if (de->name_len && de->inode) {
928 				hinfo->hash = EXT4_DIRENT_HASH(de);
929 				hinfo->minor_hash = EXT4_DIRENT_MINOR_HASH(de);
930 			} else {
931 				hinfo->hash = 0;
932 				hinfo->minor_hash = 0;
933 			}
934 		} else {
935 			err = ext4fs_dirhash(dir, de->name,
936 					     de->name_len, hinfo);
937 			if (err < 0) {
938 				count = err;
939 				goto errout;
940 			}
941 		}
942 		if ((hinfo->hash < start_hash) ||
943 		    ((hinfo->hash == start_hash) &&
944 		     (hinfo->minor_hash < start_minor_hash)))
945 			continue;
946 		if (de->inode == 0)
947 			continue;
948 		if (!IS_ENCRYPTED(dir)) {
949 			tmp_str.name = de->name;
950 			tmp_str.len = de->name_len;
951 			err = ext4_htree_store_dirent(dir_file,
952 				   hinfo->hash, hinfo->minor_hash, de,
953 				   &tmp_str);
954 		} else {
955 			int save_len = fname_crypto_str.len;
956 			struct fscrypt_str de_name = FSTR_INIT(de->name,
957 								de->name_len);
958 
959 			/* Directory is encrypted */
960 			err = fscrypt_fname_disk_to_usr(dir, hinfo->hash,
961 					hinfo->minor_hash, &de_name,
962 					&fname_crypto_str);
963 			if (err) {
964 				count = err;
965 				goto errout;
966 			}
967 			err = ext4_htree_store_dirent(dir_file,
968 				   hinfo->hash, hinfo->minor_hash, de,
969 					&fname_crypto_str);
970 			fname_crypto_str.len = save_len;
971 		}
972 		if (err != 0) {
973 			count = err;
974 			goto errout;
975 		}
976 		count++;
977 	}
978 errout:
979 	brelse(bh);
980 	fscrypt_fname_free_buffer(&fname_crypto_str);
981 	return count;
982 }
983 
984 
985 /*
986  * This function fills a red-black tree with information from a
987  * directory.  We start scanning the directory in hash order, starting
988  * at start_hash and start_minor_hash.
989  *
990  * This function returns the number of entries inserted into the tree,
991  * or a negative error code.
992  */
993 int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
994 			 __u32 start_minor_hash, __u32 *next_hash)
995 {
996 	struct dx_hash_info hinfo;
997 	struct ext4_dir_entry_2 *de;
998 	struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
999 	struct inode *dir;
1000 	ext4_lblk_t block;
1001 	int count = 0;
1002 	int ret, err;
1003 	__u32 hashval;
1004 	struct fscrypt_str tmp_str;
1005 
1006 	KUNIT_STATIC_STUB_REDIRECT(ext4_htree_fill_tree, dir_file, start_hash,
1007 				   start_minor_hash, next_hash);
1008 
1009 	dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n",
1010 		       start_hash, start_minor_hash));
1011 	dir = file_inode(dir_file);
1012 	if (!(ext4_test_inode_flag(dir, EXT4_INODE_INDEX))) {
1013 		if (ext4_hash_in_dirent(dir))
1014 			hinfo.hash_version = DX_HASH_SIPHASH;
1015 		else
1016 			hinfo.hash_version =
1017 					EXT4_SB(dir->i_sb)->s_def_hash_version;
1018 		if (hinfo.hash_version <= DX_HASH_TEA)
1019 			hinfo.hash_version +=
1020 				EXT4_SB(dir->i_sb)->s_hash_unsigned;
1021 		hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
1022 		if (ext4_has_inline_data(dir)) {
1023 			int has_inline_data = 1;
1024 			count = ext4_inlinedir_to_tree(dir_file, dir, 0,
1025 						       &hinfo, start_hash,
1026 						       start_minor_hash,
1027 						       &has_inline_data);
1028 			if (has_inline_data) {
1029 				*next_hash = ~0;
1030 				return count;
1031 			}
1032 		}
1033 		count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
1034 					       start_hash, start_minor_hash);
1035 		*next_hash = ~0;
1036 		return count;
1037 	}
1038 	hinfo.hash = start_hash;
1039 	hinfo.minor_hash = 0;
1040 	frame = dx_probe(NULL, dir, &hinfo, frames);
1041 	if (IS_ERR(frame))
1042 		return PTR_ERR(frame);
1043 
1044 	/* Add '.' and '..' from the htree header */
1045 	if (!start_hash && !start_minor_hash) {
1046 		de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
1047 		tmp_str.name = de->name;
1048 		tmp_str.len = de->name_len;
1049 		err = ext4_htree_store_dirent(dir_file, 0, 0,
1050 					      de, &tmp_str);
1051 		if (err != 0)
1052 			goto errout;
1053 		count++;
1054 	}
1055 	if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
1056 		de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
1057 		de = ext4_next_entry(de, dir->i_sb->s_blocksize);
1058 		tmp_str.name = de->name;
1059 		tmp_str.len = de->name_len;
1060 		err = ext4_htree_store_dirent(dir_file, 2, 0,
1061 					      de, &tmp_str);
1062 		if (err != 0)
1063 			goto errout;
1064 		count++;
1065 	}
1066 
1067 	while (1) {
1068 		if (fatal_signal_pending(current)) {
1069 			err = -ERESTARTSYS;
1070 			goto errout;
1071 		}
1072 		cond_resched();
1073 		block = dx_get_block(frame->at);
1074 		ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
1075 					     start_hash, start_minor_hash);
1076 		if (ret < 0) {
1077 			err = ret;
1078 			goto errout;
1079 		}
1080 		count += ret;
1081 		hashval = ~0;
1082 		ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
1083 					    frame, frames, &hashval);
1084 		*next_hash = hashval;
1085 		if (ret < 0) {
1086 			err = ret;
1087 			goto errout;
1088 		}
1089 		/*
1090 		 * Stop if:  (a) there are no more entries, or
1091 		 * (b) we have inserted at least one entry and the
1092 		 * next hash value is not a continuation
1093 		 */
1094 		if ((ret == 0) ||
1095 		    (count && ((hashval & 1) == 0)))
1096 			break;
1097 	}
1098 	dx_release(frames);
1099 	dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, "
1100 		       "next hash: %x\n", count, *next_hash));
1101 	return count;
1102 errout:
1103 	dx_release(frames);
1104 	return (err);
1105 }
1106 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_htree_fill_tree);
1107 
1108 static inline int search_dirblock(struct buffer_head *bh,
1109 				  struct inode *dir,
1110 				  struct ext4_filename *fname,
1111 				  unsigned int offset,
1112 				  struct ext4_dir_entry_2 **res_dir)
1113 {
1114 	return ext4_search_dir(bh, bh->b_data, dir->i_sb->s_blocksize, dir,
1115 			       fname, offset, res_dir);
1116 }
1117 
1118 /*
1119  * Directory block splitting, compacting
1120  */
1121 
1122 /*
1123  * Create map of hash values, offsets, and sizes, stored at end of block.
1124  * Returns number of entries mapped.
1125  */
1126 static int dx_make_map(struct inode *dir, struct buffer_head *bh,
1127 		       struct dx_hash_info *hinfo,
1128 		       struct dx_map_entry *map_tail)
1129 {
1130 	int count = 0;
1131 	struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)bh->b_data;
1132 	unsigned int buflen = bh->b_size;
1133 	char *base = bh->b_data;
1134 	struct dx_hash_info h = *hinfo;
1135 	int blocksize = EXT4_BLOCK_SIZE(dir->i_sb);
1136 
1137 	if (ext4_has_feature_metadata_csum(dir->i_sb))
1138 		buflen -= sizeof(struct ext4_dir_entry_tail);
1139 
1140 	while ((char *) de < base + buflen) {
1141 		if (ext4_check_dir_entry(dir, NULL, de, bh, base, buflen,
1142 					 ((char *)de) - base))
1143 			return -EFSCORRUPTED;
1144 		if (de->name_len && de->inode) {
1145 			if (ext4_hash_in_dirent(dir))
1146 				h.hash = EXT4_DIRENT_HASH(de);
1147 			else {
1148 				int err = ext4fs_dirhash(dir, de->name,
1149 						     de->name_len, &h);
1150 				if (err < 0)
1151 					return err;
1152 			}
1153 			map_tail--;
1154 			map_tail->hash = h.hash;
1155 			map_tail->offs = ((char *) de - base)>>2;
1156 			map_tail->size = ext4_rec_len_from_disk(de->rec_len,
1157 								blocksize);
1158 			count++;
1159 			cond_resched();
1160 		}
1161 		de = ext4_next_entry(de, blocksize);
1162 	}
1163 	return count;
1164 }
1165 
1166 /* Sort map by hash value */
1167 static void dx_sort_map (struct dx_map_entry *map, unsigned count)
1168 {
1169 	struct dx_map_entry *p, *q, *top = map + count - 1;
1170 	int more;
1171 	/* Combsort until bubble sort doesn't suck */
1172 	while (count > 2) {
1173 		count = count*10/13;
1174 		if (count - 9 < 2) /* 9, 10 -> 11 */
1175 			count = 11;
1176 		for (p = top, q = p - count; q >= map; p--, q--)
1177 			if (p->hash < q->hash)
1178 				swap(*p, *q);
1179 	}
1180 	/* Garden variety bubble sort */
1181 	do {
1182 		more = 0;
1183 		q = top;
1184 		while (q-- > map) {
1185 			if (q[1].hash >= q[0].hash)
1186 				continue;
1187 			swap(*(q+1), *q);
1188 			more = 1;
1189 		}
1190 	} while(more);
1191 }
1192 
1193 static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
1194 {
1195 	struct dx_entry *entries = frame->entries;
1196 	struct dx_entry *old = frame->at, *new = old + 1;
1197 	int count = dx_get_count(entries);
1198 
1199 	ASSERT(count < dx_get_limit(entries));
1200 	ASSERT(old < entries + count);
1201 	memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
1202 	dx_set_hash(new, hash);
1203 	dx_set_block(new, block);
1204 	dx_set_count(entries, count + 1);
1205 }
1206 
1207 #if IS_ENABLED(CONFIG_UNICODE)
1208 int ext4_fname_setup_ci_filename(struct inode *dir, const struct qstr *iname,
1209 				  struct ext4_filename *name)
1210 {
1211 	struct qstr *cf_name = &name->cf_name;
1212 	unsigned char *buf;
1213 	struct dx_hash_info *hinfo = &name->hinfo;
1214 	int len;
1215 
1216 	if (!IS_CASEFOLDED(dir) ||
1217 	    (IS_ENCRYPTED(dir) && !fscrypt_has_encryption_key(dir))) {
1218 		cf_name->name = NULL;
1219 		return 0;
1220 	}
1221 
1222 	buf = kmalloc(EXT4_NAME_LEN, GFP_NOFS);
1223 	if (!buf)
1224 		return -ENOMEM;
1225 
1226 	len = utf8_casefold(dir->i_sb->s_encoding, iname, buf, EXT4_NAME_LEN);
1227 	if (len <= 0) {
1228 		kfree(buf);
1229 		buf = NULL;
1230 	}
1231 	cf_name->name = buf;
1232 	cf_name->len = (unsigned) len;
1233 
1234 	if (!IS_ENCRYPTED(dir))
1235 		return 0;
1236 
1237 	hinfo->hash_version = DX_HASH_SIPHASH;
1238 	hinfo->seed = NULL;
1239 	if (cf_name->name)
1240 		return ext4fs_dirhash(dir, cf_name->name, cf_name->len, hinfo);
1241 	else
1242 		return ext4fs_dirhash(dir, iname->name, iname->len, hinfo);
1243 }
1244 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_fname_setup_ci_filename);
1245 #endif
1246 
1247 /*
1248  * Test whether a directory entry matches the filename being searched for.
1249  *
1250  * Return: %true if the directory entry matches, otherwise %false.
1251  */
1252 static bool ext4_match(struct inode *parent,
1253 			      const struct ext4_filename *fname,
1254 			      struct ext4_dir_entry_2 *de)
1255 {
1256 	struct fscrypt_name f;
1257 
1258 	if (!de->inode)
1259 		return false;
1260 
1261 	f.usr_fname = fname->usr_fname;
1262 	f.disk_name = fname->disk_name;
1263 #ifdef CONFIG_FS_ENCRYPTION
1264 	f.crypto_buf = fname->crypto_buf;
1265 #endif
1266 
1267 #if IS_ENABLED(CONFIG_UNICODE)
1268 	if (IS_CASEFOLDED(parent) &&
1269 	    (!IS_ENCRYPTED(parent) || fscrypt_has_encryption_key(parent))) {
1270 		/*
1271 		 * Just checking IS_ENCRYPTED(parent) below is not
1272 		 * sufficient to decide whether one can use the hash for
1273 		 * skipping the string comparison, because the key might
1274 		 * have been added right after
1275 		 * ext4_fname_setup_ci_filename().  In this case, a hash
1276 		 * mismatch will be a false negative.  Therefore, make
1277 		 * sure cf_name was properly initialized before
1278 		 * considering the calculated hash.
1279 		 */
1280 		if (sb_no_casefold_compat_fallback(parent->i_sb) &&
1281 		    IS_ENCRYPTED(parent) && fname->cf_name.name &&
1282 		    (fname->hinfo.hash != EXT4_DIRENT_HASH(de) ||
1283 		     fname->hinfo.minor_hash != EXT4_DIRENT_MINOR_HASH(de)))
1284 			return false;
1285 		/*
1286 		 * Treat comparison errors as not a match.  The
1287 		 * only case where it happens is on a disk
1288 		 * corruption or ENOMEM.
1289 		 */
1290 
1291 		return generic_ci_match(parent, fname->usr_fname,
1292 					&fname->cf_name, de->name,
1293 					de->name_len) > 0;
1294 	}
1295 #endif
1296 
1297 	return fscrypt_match_name(&f, de->name, de->name_len);
1298 }
1299 
1300 /*
1301  * Returns 0 if not found, -EFSCORRUPTED on failure, and 1 on success
1302  */
1303 int ext4_search_dir(struct buffer_head *bh, char *search_buf, int buf_size,
1304 		    struct inode *dir, struct ext4_filename *fname,
1305 		    unsigned int offset, struct ext4_dir_entry_2 **res_dir)
1306 {
1307 	struct ext4_dir_entry_2 * de;
1308 	char * dlimit;
1309 	int de_len;
1310 
1311 	de = (struct ext4_dir_entry_2 *)search_buf;
1312 	dlimit = search_buf + buf_size;
1313 	while ((char *) de < dlimit - EXT4_BASE_DIR_LEN) {
1314 		/* this code is executed quadratically often */
1315 		/* do minimal checking `by hand' */
1316 		if (de->name + de->name_len <= dlimit &&
1317 		    (!ext4_hash_in_dirent(dir) ||
1318 		     (char *)de + ext4_dir_rec_len(de->name_len, dir) <= dlimit) &&
1319 		    ext4_match(dir, fname, de)) {
1320 			/* found a match - just to be sure, do
1321 			 * a full check */
1322 			if (ext4_check_dir_entry(dir, NULL, de, bh, search_buf,
1323 						 buf_size, offset))
1324 				return -EFSCORRUPTED;
1325 			*res_dir = de;
1326 			return 1;
1327 		}
1328 		/* prevent looping on a bad block */
1329 		de_len = ext4_rec_len_from_disk(de->rec_len,
1330 						dir->i_sb->s_blocksize);
1331 		if (de_len <= 0)
1332 			return -EFSCORRUPTED;
1333 		offset += de_len;
1334 		de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
1335 	}
1336 	return 0;
1337 }
1338 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_search_dir);
1339 
1340 static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block,
1341 			       struct ext4_dir_entry *de)
1342 {
1343 	struct super_block *sb = dir->i_sb;
1344 
1345 	if (!is_dx(dir))
1346 		return 0;
1347 	if (block == 0)
1348 		return 1;
1349 	if (de->inode == 0 &&
1350 	    ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) ==
1351 			sb->s_blocksize)
1352 		return 1;
1353 	return 0;
1354 }
1355 
1356 /*
1357  *	__ext4_find_entry()
1358  *
1359  * finds an entry in the specified directory with the wanted name. It
1360  * returns the cache buffer in which the entry was found, and the entry
1361  * itself (as a parameter - res_dir). It does NOT read the inode of the
1362  * entry - you'll have to do that yourself if you want to.
1363  *
1364  * The returned buffer_head has ->b_count elevated.  The caller is expected
1365  * to brelse() it when appropriate.
1366  */
1367 static struct buffer_head *__ext4_find_entry(struct inode *dir,
1368 					     struct ext4_filename *fname,
1369 					     struct ext4_dir_entry_2 **res_dir,
1370 					     int *inlined)
1371 {
1372 	struct super_block *sb;
1373 	struct buffer_head *bh_use[NAMEI_RA_SIZE];
1374 	struct buffer_head *bh, *ret = NULL;
1375 	ext4_lblk_t start, block;
1376 	const u8 *name = fname->usr_fname->name;
1377 	size_t ra_max = 0;	/* Number of bh's in the readahead
1378 				   buffer, bh_use[] */
1379 	size_t ra_ptr = 0;	/* Current index into readahead
1380 				   buffer */
1381 	ext4_lblk_t  nblocks;
1382 	int i, namelen, retval;
1383 
1384 	*res_dir = NULL;
1385 	sb = dir->i_sb;
1386 	namelen = fname->usr_fname->len;
1387 	if (namelen > EXT4_NAME_LEN)
1388 		return NULL;
1389 
1390 	if (ext4_has_inline_data(dir)) {
1391 		int has_inline_data = 1;
1392 		ret = ext4_find_inline_entry(dir, fname, res_dir,
1393 					     &has_inline_data);
1394 		if (inlined)
1395 			*inlined = has_inline_data;
1396 		if (has_inline_data || IS_ERR(ret))
1397 			goto cleanup_and_exit;
1398 	}
1399 
1400 	if ((namelen <= 2) && (name[0] == '.') &&
1401 	    (name[1] == '.' || name[1] == '\0')) {
1402 		/*
1403 		 * "." or ".." will only be in the first block
1404 		 * NFS may look up ".."; "." should be handled by the VFS
1405 		 */
1406 		block = start = 0;
1407 		nblocks = 1;
1408 		goto restart;
1409 	}
1410 	if (is_dx(dir)) {
1411 		ret = ext4_dx_find_entry(dir, fname, res_dir);
1412 		/*
1413 		 * On success, or if the error was file not found,
1414 		 * return.  Otherwise, fall back to doing a search the
1415 		 * old fashioned way.
1416 		 */
1417 		if (IS_ERR(ret) && PTR_ERR(ret) == ERR_BAD_DX_DIR)
1418 			dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
1419 				       "falling back\n"));
1420 		else if (!sb_no_casefold_compat_fallback(dir->i_sb) &&
1421 			 *res_dir == NULL && IS_CASEFOLDED(dir))
1422 			dxtrace(printk(KERN_DEBUG "ext4_find_entry: casefold "
1423 				       "failed, falling back\n"));
1424 		else
1425 			goto cleanup_and_exit;
1426 		ret = NULL;
1427 	}
1428 	nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
1429 	if (!nblocks) {
1430 		ret = NULL;
1431 		goto cleanup_and_exit;
1432 	}
1433 	start = EXT4_I(dir)->i_dir_start_lookup;
1434 	if (start >= nblocks)
1435 		start = 0;
1436 	block = start;
1437 restart:
1438 	do {
1439 		/*
1440 		 * We deal with the read-ahead logic here.
1441 		 */
1442 		cond_resched();
1443 		if (ra_ptr >= ra_max) {
1444 			/* Refill the readahead buffer */
1445 			ra_ptr = 0;
1446 			if (block < start)
1447 				ra_max = start - block;
1448 			else
1449 				ra_max = nblocks - block;
1450 			ra_max = min(ra_max, ARRAY_SIZE(bh_use));
1451 			retval = ext4_bread_batch(dir, block, ra_max,
1452 						  false /* wait */, bh_use);
1453 			if (retval) {
1454 				ret = ERR_PTR(retval);
1455 				ra_max = 0;
1456 				goto cleanup_and_exit;
1457 			}
1458 		}
1459 		if ((bh = bh_use[ra_ptr++]) == NULL)
1460 			goto next;
1461 		wait_on_buffer(bh);
1462 		if (!buffer_uptodate(bh)) {
1463 			EXT4_ERROR_INODE_ERR(dir, EIO,
1464 					     "reading directory lblock %lu",
1465 					     (unsigned long) block);
1466 			brelse(bh);
1467 			ret = ERR_PTR(-EIO);
1468 			goto cleanup_and_exit;
1469 		}
1470 		if (!buffer_verified(bh) &&
1471 		    !is_dx_internal_node(dir, block,
1472 					 (struct ext4_dir_entry *)bh->b_data) &&
1473 		    !ext4_dirblock_csum_verify(dir, bh)) {
1474 			EXT4_ERROR_INODE_ERR(dir, EFSBADCRC,
1475 					     "checksumming directory "
1476 					     "block %lu", (unsigned long)block);
1477 			brelse(bh);
1478 			ret = ERR_PTR(-EFSBADCRC);
1479 			goto cleanup_and_exit;
1480 		}
1481 		set_buffer_verified(bh);
1482 		i = search_dirblock(bh, dir, fname,
1483 				    EXT4_LBLK_TO_B(dir, block), res_dir);
1484 		if (i == 1) {
1485 			EXT4_I(dir)->i_dir_start_lookup = block;
1486 			ret = bh;
1487 			goto cleanup_and_exit;
1488 		} else {
1489 			brelse(bh);
1490 			if (i < 0) {
1491 				ret = ERR_PTR(i);
1492 				goto cleanup_and_exit;
1493 			}
1494 		}
1495 	next:
1496 		if (++block >= nblocks)
1497 			block = 0;
1498 	} while (block != start);
1499 
1500 	/*
1501 	 * If the directory has grown while we were searching, then
1502 	 * search the last part of the directory before giving up.
1503 	 */
1504 	block = nblocks;
1505 	nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
1506 	if (block < nblocks) {
1507 		start = 0;
1508 		goto restart;
1509 	}
1510 
1511 cleanup_and_exit:
1512 	/* Clean up the read-ahead blocks */
1513 	for (; ra_ptr < ra_max; ra_ptr++)
1514 		brelse(bh_use[ra_ptr]);
1515 	return ret;
1516 }
1517 
1518 static struct buffer_head *ext4_find_entry(struct inode *dir,
1519 					   const struct qstr *d_name,
1520 					   struct ext4_dir_entry_2 **res_dir,
1521 					   int *inlined)
1522 {
1523 	int err;
1524 	struct ext4_filename fname;
1525 	struct buffer_head *bh;
1526 
1527 	err = ext4_fname_setup_filename(dir, d_name, 1, &fname);
1528 	if (err == -ENOENT)
1529 		return NULL;
1530 	if (err)
1531 		return ERR_PTR(err);
1532 
1533 	bh = __ext4_find_entry(dir, &fname, res_dir, inlined);
1534 
1535 	ext4_fname_free_filename(&fname);
1536 	return bh;
1537 }
1538 
1539 static struct buffer_head *ext4_lookup_entry(struct inode *dir,
1540 					     struct dentry *dentry,
1541 					     struct ext4_dir_entry_2 **res_dir)
1542 {
1543 	int err;
1544 	struct ext4_filename fname;
1545 	struct buffer_head *bh;
1546 
1547 	err = ext4_fname_prepare_lookup(dir, dentry, &fname);
1548 	if (err == -ENOENT)
1549 		return NULL;
1550 	if (err)
1551 		return ERR_PTR(err);
1552 
1553 	bh = __ext4_find_entry(dir, &fname, res_dir, NULL);
1554 
1555 	ext4_fname_free_filename(&fname);
1556 	return bh;
1557 }
1558 
1559 static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
1560 			struct ext4_filename *fname,
1561 			struct ext4_dir_entry_2 **res_dir)
1562 {
1563 	struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
1564 	struct buffer_head *bh;
1565 	ext4_lblk_t block;
1566 	int retval;
1567 
1568 #ifdef CONFIG_FS_ENCRYPTION
1569 	*res_dir = NULL;
1570 #endif
1571 	frame = dx_probe(fname, dir, NULL, frames);
1572 	if (IS_ERR(frame))
1573 		return ERR_CAST(frame);
1574 	do {
1575 		block = dx_get_block(frame->at);
1576 		bh = ext4_read_dirblock(dir, block, DIRENT_HTREE);
1577 		if (IS_ERR(bh))
1578 			goto errout;
1579 
1580 		retval = search_dirblock(bh, dir, fname,
1581 					 EXT4_LBLK_TO_B(dir, block), res_dir);
1582 		if (retval == 1)
1583 			goto success;
1584 		brelse(bh);
1585 		if (retval < 0) {
1586 			bh = ERR_PTR(ERR_BAD_DX_DIR);
1587 			goto errout;
1588 		}
1589 
1590 		/* Check to see if we should continue to search */
1591 		retval = ext4_htree_next_block(dir, fname->hinfo.hash, frame,
1592 					       frames, NULL);
1593 		if (retval < 0) {
1594 			ext4_warning_inode(dir,
1595 				"error %d reading directory index block",
1596 				retval);
1597 			bh = ERR_PTR(retval);
1598 			goto errout;
1599 		}
1600 	} while (retval == 1);
1601 
1602 	bh = NULL;
1603 errout:
1604 	dxtrace(printk(KERN_DEBUG "%s not found\n", fname->usr_fname->name));
1605 success:
1606 	dx_release(frames);
1607 	return bh;
1608 }
1609 
1610 static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
1611 {
1612 	struct inode *inode;
1613 	struct ext4_dir_entry_2 *de = NULL;
1614 	struct buffer_head *bh;
1615 
1616 	if (dentry->d_name.len > EXT4_NAME_LEN)
1617 		return ERR_PTR(-ENAMETOOLONG);
1618 
1619 	bh = ext4_lookup_entry(dir, dentry, &de);
1620 	if (IS_ERR(bh))
1621 		return ERR_CAST(bh);
1622 	inode = NULL;
1623 	if (bh) {
1624 		__u32 ino = le32_to_cpu(de->inode);
1625 		brelse(bh);
1626 		if (!ext4_valid_inum(dir->i_sb, ino)) {
1627 			EXT4_ERROR_INODE(dir, "bad inode number: %u", ino);
1628 			return ERR_PTR(-EFSCORRUPTED);
1629 		}
1630 		if (unlikely(ino == dir->i_ino)) {
1631 			EXT4_ERROR_INODE(dir, "'%pd' linked to parent dir",
1632 					 dentry);
1633 			return ERR_PTR(-EFSCORRUPTED);
1634 		}
1635 		inode = ext4_iget(dir->i_sb, ino, EXT4_IGET_NORMAL);
1636 		if (inode == ERR_PTR(-ESTALE)) {
1637 			EXT4_ERROR_INODE(dir,
1638 					 "deleted inode referenced: %u",
1639 					 ino);
1640 			return ERR_PTR(-EFSCORRUPTED);
1641 		}
1642 		if (!IS_ERR(inode) && IS_ENCRYPTED(dir) &&
1643 		    (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
1644 		    !fscrypt_has_permitted_context(dir, inode)) {
1645 			ext4_warning(inode->i_sb,
1646 				     "Inconsistent encryption contexts: %llu/%llu",
1647 				     dir->i_ino, inode->i_ino);
1648 			iput(inode);
1649 			return ERR_PTR(-EPERM);
1650 		}
1651 	}
1652 
1653 	if (IS_ENABLED(CONFIG_UNICODE) && !inode && IS_CASEFOLDED(dir)) {
1654 		/* Eventually we want to call d_add_ci(dentry, NULL)
1655 		 * for negative dentries in the encoding case as
1656 		 * well.  For now, prevent the negative dentry
1657 		 * from being cached.
1658 		 */
1659 		return NULL;
1660 	}
1661 
1662 	return d_splice_alias(inode, dentry);
1663 }
1664 
1665 
1666 struct dentry *ext4_get_parent(struct dentry *child)
1667 {
1668 	__u32 ino;
1669 	struct ext4_dir_entry_2 * de = NULL;
1670 	struct buffer_head *bh;
1671 
1672 	bh = ext4_find_entry(d_inode(child), &dotdot_name, &de, NULL);
1673 	if (IS_ERR(bh))
1674 		return ERR_CAST(bh);
1675 	if (!bh)
1676 		return ERR_PTR(-ENOENT);
1677 	ino = le32_to_cpu(de->inode);
1678 	brelse(bh);
1679 
1680 	if (!ext4_valid_inum(child->d_sb, ino)) {
1681 		EXT4_ERROR_INODE(d_inode(child),
1682 				 "bad parent inode number: %u", ino);
1683 		return ERR_PTR(-EFSCORRUPTED);
1684 	}
1685 
1686 	return d_obtain_alias(ext4_iget(child->d_sb, ino, EXT4_IGET_NORMAL));
1687 }
1688 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_get_parent);
1689 
1690 /*
1691  * Move count entries from end of map between two memory locations.
1692  * Returns pointer to last entry moved.
1693  */
1694 static struct ext4_dir_entry_2 *
1695 dx_move_dirents(struct inode *dir, char *from, char *to,
1696 		struct dx_map_entry *map, int count,
1697 		unsigned blocksize)
1698 {
1699 	unsigned rec_len = 0;
1700 
1701 	while (count--) {
1702 		struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)
1703 						(from + (map->offs<<2));
1704 		rec_len = ext4_dir_rec_len(de->name_len, dir);
1705 
1706 		memcpy (to, de, rec_len);
1707 		((struct ext4_dir_entry_2 *) to)->rec_len =
1708 				ext4_rec_len_to_disk(rec_len, blocksize);
1709 
1710 		/* wipe dir_entry excluding the rec_len field */
1711 		de->inode = 0;
1712 		memset(&de->name_len, 0, ext4_rec_len_from_disk(de->rec_len,
1713 								blocksize) -
1714 					 offsetof(struct ext4_dir_entry_2,
1715 								name_len));
1716 
1717 		map++;
1718 		to += rec_len;
1719 	}
1720 	return (struct ext4_dir_entry_2 *) (to - rec_len);
1721 }
1722 
1723 /*
1724  * Compact each dir entry in the range to the minimal rec_len.
1725  * Returns pointer to last entry in range.
1726  */
1727 static struct ext4_dir_entry_2 *dx_pack_dirents(struct inode *dir, char *base,
1728 							unsigned int blocksize)
1729 {
1730 	struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
1731 	unsigned rec_len = 0;
1732 
1733 	prev = to = de;
1734 	while ((char*)de < base + blocksize) {
1735 		next = ext4_next_entry(de, blocksize);
1736 		if (de->inode && de->name_len) {
1737 			rec_len = ext4_dir_rec_len(de->name_len, dir);
1738 			if (de > to)
1739 				memmove(to, de, rec_len);
1740 			to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
1741 			prev = to;
1742 			to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
1743 		}
1744 		de = next;
1745 	}
1746 	return prev;
1747 }
1748 
1749 /*
1750  * Split a full leaf block to make room for a new dir entry.
1751  * Allocate a new block, and move entries so that they are approx. equally full.
1752  * Returns pointer to de in block into which the new entry will be inserted.
1753  */
1754 static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
1755 			struct buffer_head **bh,struct dx_frame *frame,
1756 			struct dx_hash_info *hinfo)
1757 {
1758 	unsigned blocksize = dir->i_sb->s_blocksize;
1759 	unsigned continued;
1760 	int count;
1761 	struct buffer_head *bh2;
1762 	ext4_lblk_t newblock;
1763 	u32 hash2;
1764 	struct dx_map_entry *map;
1765 	char *data1 = (*bh)->b_data, *data2;
1766 	unsigned split, move, size;
1767 	struct ext4_dir_entry_2 *de = NULL, *de2;
1768 	int	csum_size = 0;
1769 	int	err = 0, i;
1770 
1771 	if (ext4_has_feature_metadata_csum(dir->i_sb))
1772 		csum_size = sizeof(struct ext4_dir_entry_tail);
1773 
1774 	bh2 = ext4_append(handle, dir, &newblock);
1775 	if (IS_ERR(bh2)) {
1776 		brelse(*bh);
1777 		*bh = NULL;
1778 		return ERR_CAST(bh2);
1779 	}
1780 
1781 	BUFFER_TRACE(*bh, "get_write_access");
1782 	err = ext4_journal_get_write_access(handle, dir->i_sb, *bh,
1783 					    EXT4_JTR_NONE);
1784 	if (err)
1785 		goto journal_error;
1786 
1787 	BUFFER_TRACE(frame->bh, "get_write_access");
1788 	err = ext4_journal_get_write_access(handle, dir->i_sb, frame->bh,
1789 					    EXT4_JTR_NONE);
1790 	if (err)
1791 		goto journal_error;
1792 
1793 	data2 = bh2->b_data;
1794 
1795 	/* create map in the end of data2 block */
1796 	map = (struct dx_map_entry *) (data2 + blocksize);
1797 	count = dx_make_map(dir, *bh, hinfo, map);
1798 	if (count < 0) {
1799 		err = count;
1800 		goto journal_error;
1801 	}
1802 	map -= count;
1803 	dx_sort_map(map, count);
1804 	/* Ensure that neither split block is over half full */
1805 	size = 0;
1806 	move = 0;
1807 	for (i = count-1; i >= 0; i--) {
1808 		/* is more than half of this entry in 2nd half of the block? */
1809 		if (size + map[i].size/2 > blocksize/2)
1810 			break;
1811 		size += map[i].size;
1812 		move++;
1813 	}
1814 	/*
1815 	 * map index at which we will split
1816 	 *
1817 	 * If the sum of active entries didn't exceed half the block size, just
1818 	 * split it in half by count; each resulting block will have at least
1819 	 * half the space free.
1820 	 */
1821 	if (i >= 0)
1822 		split = count - move;
1823 	else
1824 		split = count/2;
1825 
1826 	if (WARN_ON_ONCE(split == 0)) {
1827 		/* Should never happen, but avoid out-of-bounds access below */
1828 		ext4_error_inode_block(dir, (*bh)->b_blocknr, 0,
1829 			"bad indexed directory? hash=%08x:%08x count=%d move=%u",
1830 			hinfo->hash, hinfo->minor_hash, count, move);
1831 		err = -EFSCORRUPTED;
1832 		goto out;
1833 	}
1834 
1835 	hash2 = map[split].hash;
1836 	continued = hash2 == map[split - 1].hash;
1837 	dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
1838 			(unsigned long)dx_get_block(frame->at),
1839 					hash2, split, count-split));
1840 
1841 	/* Fancy dance to stay within two buffers */
1842 	de2 = dx_move_dirents(dir, data1, data2, map + split, count - split,
1843 			      blocksize);
1844 	de = dx_pack_dirents(dir, data1, blocksize);
1845 	de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
1846 					   (char *) de,
1847 					   blocksize);
1848 	de2->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
1849 					    (char *) de2,
1850 					    blocksize);
1851 	if (csum_size) {
1852 		ext4_initialize_dirent_tail(*bh, blocksize);
1853 		ext4_initialize_dirent_tail(bh2, blocksize);
1854 	}
1855 
1856 	dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data1,
1857 			blocksize, 1));
1858 	dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data2,
1859 			blocksize, 1));
1860 
1861 	/* Which block gets the new entry? */
1862 	if (hinfo->hash >= hash2) {
1863 		swap(*bh, bh2);
1864 		de = de2;
1865 	}
1866 	dx_insert_block(frame, hash2 + continued, newblock);
1867 	err = ext4_handle_dirty_dirblock(handle, dir, bh2);
1868 	if (err)
1869 		goto journal_error;
1870 	err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
1871 	if (err)
1872 		goto journal_error;
1873 	brelse(bh2);
1874 	dxtrace(dx_show_index("frame", frame->entries));
1875 	return de;
1876 
1877 journal_error:
1878 	ext4_std_error(dir->i_sb, err);
1879 out:
1880 	brelse(*bh);
1881 	brelse(bh2);
1882 	*bh = NULL;
1883 	return ERR_PTR(err);
1884 }
1885 
1886 int ext4_find_dest_de(struct inode *dir, struct buffer_head *bh,
1887 		      void *buf, int buf_size,
1888 		      struct ext4_filename *fname,
1889 		      struct ext4_dir_entry_2 **dest_de)
1890 {
1891 	struct ext4_dir_entry_2 *de;
1892 	unsigned short reclen = ext4_dir_rec_len(fname_len(fname), dir);
1893 	int nlen, rlen;
1894 	unsigned int offset = 0;
1895 	char *top;
1896 
1897 	de = buf;
1898 	top = buf + buf_size - reclen;
1899 	while ((char *) de <= top) {
1900 		if (ext4_check_dir_entry(dir, NULL, de, bh,
1901 					 buf, buf_size, offset))
1902 			return -EFSCORRUPTED;
1903 		if (ext4_match(dir, fname, de))
1904 			return -EEXIST;
1905 		nlen = ext4_dir_rec_len(de->name_len, dir);
1906 		rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
1907 		if ((de->inode ? rlen - nlen : rlen) >= reclen)
1908 			break;
1909 		de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
1910 		offset += rlen;
1911 	}
1912 	if ((char *) de > top)
1913 		return -ENOSPC;
1914 
1915 	*dest_de = de;
1916 	return 0;
1917 }
1918 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_find_dest_de);
1919 
1920 void ext4_insert_dentry(struct inode *dir,
1921 			struct inode *inode,
1922 			struct ext4_dir_entry_2 *de,
1923 			int buf_size,
1924 			struct ext4_filename *fname)
1925 {
1926 
1927 	int nlen, rlen;
1928 
1929 	nlen = ext4_dir_rec_len(de->name_len, dir);
1930 	rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
1931 	if (de->inode) {
1932 		struct ext4_dir_entry_2 *de1 =
1933 			(struct ext4_dir_entry_2 *)((char *)de + nlen);
1934 		de1->rec_len = ext4_rec_len_to_disk(rlen - nlen, buf_size);
1935 		de->rec_len = ext4_rec_len_to_disk(nlen, buf_size);
1936 		de = de1;
1937 	}
1938 	de->file_type = EXT4_FT_UNKNOWN;
1939 	de->inode = cpu_to_le32(inode->i_ino);
1940 	ext4_set_de_type(inode->i_sb, de, inode->i_mode);
1941 	de->name_len = fname_len(fname);
1942 	memcpy(de->name, fname_name(fname), fname_len(fname));
1943 	if (ext4_hash_in_dirent(dir)) {
1944 		struct dx_hash_info *hinfo = &fname->hinfo;
1945 
1946 		EXT4_DIRENT_HASHES(de)->hash = cpu_to_le32(hinfo->hash);
1947 		EXT4_DIRENT_HASHES(de)->minor_hash =
1948 						cpu_to_le32(hinfo->minor_hash);
1949 	}
1950 }
1951 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_insert_dentry);
1952 
1953 /*
1954  * Add a new entry into a directory (leaf) block.  If de is non-NULL,
1955  * it points to a directory entry which is guaranteed to be large
1956  * enough for new directory entry.  If de is NULL, then
1957  * add_dirent_to_buf will attempt search the directory block for
1958  * space.  It will return -ENOSPC if no space is available, and -EIO
1959  * and -EEXIST if directory entry already exists.
1960  */
1961 static int add_dirent_to_buf(handle_t *handle, struct ext4_filename *fname,
1962 			     struct inode *dir,
1963 			     struct inode *inode, struct ext4_dir_entry_2 *de,
1964 			     struct buffer_head *bh)
1965 {
1966 	unsigned int	blocksize = dir->i_sb->s_blocksize;
1967 	int		csum_size = 0;
1968 	int		err, err2;
1969 
1970 	if (ext4_has_feature_metadata_csum(inode->i_sb))
1971 		csum_size = sizeof(struct ext4_dir_entry_tail);
1972 
1973 	if (!de) {
1974 		err = ext4_find_dest_de(dir, bh, bh->b_data,
1975 					blocksize - csum_size, fname, &de);
1976 		if (err)
1977 			return err;
1978 	}
1979 	BUFFER_TRACE(bh, "get_write_access");
1980 	err = ext4_journal_get_write_access(handle, dir->i_sb, bh,
1981 					    EXT4_JTR_NONE);
1982 	if (err) {
1983 		ext4_std_error(dir->i_sb, err);
1984 		return err;
1985 	}
1986 
1987 	/* By now the buffer is marked for journaling */
1988 	ext4_insert_dentry(dir, inode, de, blocksize, fname);
1989 
1990 	/*
1991 	 * XXX shouldn't update any times until successful
1992 	 * completion of syscall, but too many callers depend
1993 	 * on this.
1994 	 *
1995 	 * XXX similarly, too many callers depend on
1996 	 * ext4_new_inode() setting the times, but error
1997 	 * recovery deletes the inode, so the worst that can
1998 	 * happen is that the times are slightly out of date
1999 	 * and/or different from the directory change time.
2000 	 */
2001 	inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
2002 	ext4_update_dx_flag(dir);
2003 	inode_inc_iversion(dir);
2004 	err2 = ext4_mark_inode_dirty(handle, dir);
2005 	BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
2006 	err = ext4_handle_dirty_dirblock(handle, dir, bh);
2007 	if (err)
2008 		ext4_std_error(dir->i_sb, err);
2009 	return err ? err : err2;
2010 }
2011 
2012 static bool ext4_check_dx_root(struct inode *dir, struct dx_root *root)
2013 {
2014 	struct fake_dirent *fde;
2015 	const char *error_msg;
2016 	unsigned int rlen;
2017 	unsigned int blocksize = dir->i_sb->s_blocksize;
2018 	char *blockend = (char *)root + dir->i_sb->s_blocksize;
2019 
2020 	fde = &root->dot;
2021 	if (unlikely(fde->name_len != 1)) {
2022 		error_msg = "invalid name_len for '.'";
2023 		goto corrupted;
2024 	}
2025 	if (unlikely(strncmp(root->dot_name, ".", fde->name_len))) {
2026 		error_msg = "invalid name for '.'";
2027 		goto corrupted;
2028 	}
2029 	rlen = ext4_rec_len_from_disk(fde->rec_len, blocksize);
2030 	if (unlikely((char *)fde + rlen >= blockend)) {
2031 		error_msg = "invalid rec_len for '.'";
2032 		goto corrupted;
2033 	}
2034 
2035 	fde = &root->dotdot;
2036 	if (unlikely(fde->name_len != 2)) {
2037 		error_msg = "invalid name_len for '..'";
2038 		goto corrupted;
2039 	}
2040 	if (unlikely(strncmp(root->dotdot_name, "..", fde->name_len))) {
2041 		error_msg = "invalid name for '..'";
2042 		goto corrupted;
2043 	}
2044 	rlen = ext4_rec_len_from_disk(fde->rec_len, blocksize);
2045 	if (unlikely((char *)fde + rlen >= blockend)) {
2046 		error_msg = "invalid rec_len for '..'";
2047 		goto corrupted;
2048 	}
2049 
2050 	return true;
2051 
2052 corrupted:
2053 	EXT4_ERROR_INODE(dir, "Corrupt dir, %s, running e2fsck is recommended",
2054 			 error_msg);
2055 	return false;
2056 }
2057 
2058 /*
2059  * This converts a one block unindexed directory to a 3 block indexed
2060  * directory, and adds the dentry to the indexed directory.
2061  */
2062 static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname,
2063 			    struct inode *dir,
2064 			    struct inode *inode, struct buffer_head *bh)
2065 {
2066 	struct buffer_head *bh2;
2067 	struct dx_root	*root;
2068 	struct dx_frame	frames[EXT4_HTREE_LEVEL], *frame;
2069 	struct dx_entry *entries;
2070 	struct ext4_dir_entry_2	*de, *de2;
2071 	char		*data2, *top;
2072 	unsigned	len;
2073 	int		retval;
2074 	unsigned	blocksize;
2075 	ext4_lblk_t  block;
2076 	struct fake_dirent *fde;
2077 	int csum_size = 0;
2078 
2079 	if (ext4_has_feature_metadata_csum(inode->i_sb))
2080 		csum_size = sizeof(struct ext4_dir_entry_tail);
2081 
2082 	blocksize =  dir->i_sb->s_blocksize;
2083 	dxtrace(printk(KERN_DEBUG "Creating index: inode %llu\n", dir->i_ino));
2084 	BUFFER_TRACE(bh, "get_write_access");
2085 	retval = ext4_journal_get_write_access(handle, dir->i_sb, bh,
2086 					       EXT4_JTR_NONE);
2087 	if (retval) {
2088 		ext4_std_error(dir->i_sb, retval);
2089 		brelse(bh);
2090 		return retval;
2091 	}
2092 
2093 	root = (struct dx_root *) bh->b_data;
2094 	if (!ext4_check_dx_root(dir, root)) {
2095 		brelse(bh);
2096 		return -EFSCORRUPTED;
2097 	}
2098 
2099 	/* The 0th block becomes the root, move the dirents out */
2100 	fde = &root->dotdot;
2101 	de = (struct ext4_dir_entry_2 *)((char *)fde +
2102 		ext4_rec_len_from_disk(fde->rec_len, blocksize));
2103 	len = ((char *) root) + (blocksize - csum_size) - (char *) de;
2104 
2105 	/* Allocate new block for the 0th block's dirents */
2106 	bh2 = ext4_append(handle, dir, &block);
2107 	if (IS_ERR(bh2)) {
2108 		brelse(bh);
2109 		return PTR_ERR(bh2);
2110 	}
2111 	ext4_set_inode_flag(dir, EXT4_INODE_INDEX);
2112 	data2 = bh2->b_data;
2113 
2114 	memcpy(data2, de, len);
2115 	memset(de, 0, len); /* wipe old data */
2116 	de = (struct ext4_dir_entry_2 *) data2;
2117 	top = data2 + len;
2118 	while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top) {
2119 		if (ext4_check_dir_entry(dir, NULL, de, bh2, data2, len,
2120 					(char *)de - data2)) {
2121 			brelse(bh2);
2122 			brelse(bh);
2123 			return -EFSCORRUPTED;
2124 		}
2125 		de = de2;
2126 	}
2127 	de->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
2128 					   (char *) de, blocksize);
2129 
2130 	if (csum_size)
2131 		ext4_initialize_dirent_tail(bh2, blocksize);
2132 
2133 	/* Initialize the root; the dot dirents already exist */
2134 	de = (struct ext4_dir_entry_2 *) (&root->dotdot);
2135 	de->rec_len = ext4_rec_len_to_disk(
2136 			blocksize - ext4_dir_rec_len(2, NULL), blocksize);
2137 	memset (&root->info, 0, sizeof(root->info));
2138 	root->info.info_length = sizeof(root->info);
2139 	if (ext4_hash_in_dirent(dir))
2140 		root->info.hash_version = DX_HASH_SIPHASH;
2141 	else
2142 		root->info.hash_version =
2143 				EXT4_SB(dir->i_sb)->s_def_hash_version;
2144 
2145 	entries = root->entries;
2146 	dx_set_block(entries, 1);
2147 	dx_set_count(entries, 1);
2148 	dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
2149 
2150 	/* Initialize as for dx_probe */
2151 	fname->hinfo.hash_version = root->info.hash_version;
2152 	if (fname->hinfo.hash_version <= DX_HASH_TEA)
2153 		fname->hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
2154 	fname->hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
2155 
2156 	/* casefolded encrypted hashes are computed on fname setup */
2157 	if (!ext4_hash_in_dirent(dir)) {
2158 		int err = ext4fs_dirhash(dir, fname_name(fname),
2159 					 fname_len(fname), &fname->hinfo);
2160 		if (err < 0) {
2161 			brelse(bh2);
2162 			brelse(bh);
2163 			return err;
2164 		}
2165 	}
2166 	memset(frames, 0, sizeof(frames));
2167 	frame = frames;
2168 	frame->entries = entries;
2169 	frame->at = entries;
2170 	frame->bh = bh;
2171 
2172 	retval = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
2173 	if (retval)
2174 		goto out_frames;
2175 	retval = ext4_handle_dirty_dirblock(handle, dir, bh2);
2176 	if (retval)
2177 		goto out_frames;
2178 
2179 	de = do_split(handle,dir, &bh2, frame, &fname->hinfo);
2180 	if (IS_ERR(de)) {
2181 		retval = PTR_ERR(de);
2182 		goto out_frames;
2183 	}
2184 
2185 	retval = add_dirent_to_buf(handle, fname, dir, inode, de, bh2);
2186 out_frames:
2187 	/*
2188 	 * Even if the block split failed, we have to properly write
2189 	 * out all the changes we did so far. Otherwise we can end up
2190 	 * with corrupted filesystem.
2191 	 */
2192 	if (retval)
2193 		ext4_mark_inode_dirty(handle, dir);
2194 	dx_release(frames);
2195 	brelse(bh2);
2196 	return retval;
2197 }
2198 
2199 /*
2200  *	ext4_add_entry()
2201  *
2202  * adds a file entry to the specified directory, using the same
2203  * semantics as ext4_find_entry(). It returns NULL if it failed.
2204  *
2205  * NOTE!! The inode part of 'de' is left at 0 - which means you
2206  * may not sleep between calling this and putting something into
2207  * the entry, as someone else might have used it while you slept.
2208  */
2209 static int __ext4_add_entry(handle_t *handle, struct inode *dir,
2210 			  const struct qstr *d_name,
2211 			  struct inode *inode)
2212 {
2213 	struct buffer_head *bh = NULL;
2214 	struct ext4_dir_entry_2 *de;
2215 	struct super_block *sb;
2216 	struct ext4_filename fname;
2217 	int	retval;
2218 	int	dx_fallback=0;
2219 	unsigned blocksize;
2220 	ext4_lblk_t block, blocks;
2221 	int	csum_size = 0;
2222 
2223 	if (ext4_has_feature_metadata_csum(inode->i_sb))
2224 		csum_size = sizeof(struct ext4_dir_entry_tail);
2225 
2226 	sb = dir->i_sb;
2227 	blocksize = sb->s_blocksize;
2228 
2229 	if (!generic_ci_validate_strict_name(dir, d_name))
2230 		return -EINVAL;
2231 
2232 	retval = ext4_fname_setup_filename(dir, d_name, 0, &fname);
2233 	if (retval)
2234 		return retval;
2235 
2236 	if (ext4_has_inline_data(dir)) {
2237 		retval = ext4_try_add_inline_entry(handle, &fname, dir, inode);
2238 		if (retval < 0)
2239 			goto out;
2240 		if (retval == 1) {
2241 			retval = 0;
2242 			goto out;
2243 		}
2244 	}
2245 
2246 	if (is_dx(dir)) {
2247 		retval = ext4_dx_add_entry(handle, &fname, dir, inode);
2248 		if (!retval || (retval != ERR_BAD_DX_DIR))
2249 			goto out;
2250 		/* Can we just ignore htree data? */
2251 		if (ext4_has_feature_metadata_csum(sb)) {
2252 			EXT4_ERROR_INODE(dir,
2253 				"Directory has corrupted htree index.");
2254 			retval = -EFSCORRUPTED;
2255 			goto out;
2256 		}
2257 		ext4_clear_inode_flag(dir, EXT4_INODE_INDEX);
2258 		dx_fallback++;
2259 		retval = ext4_mark_inode_dirty(handle, dir);
2260 		if (unlikely(retval))
2261 			goto out;
2262 	}
2263 	blocks = dir->i_size >> sb->s_blocksize_bits;
2264 	for (block = 0; block < blocks; block++) {
2265 		bh = ext4_read_dirblock(dir, block, DIRENT);
2266 		if (bh == NULL) {
2267 			bh = ext4_bread(handle, dir, block,
2268 					EXT4_GET_BLOCKS_CREATE);
2269 			goto add_to_new_block;
2270 		}
2271 		if (IS_ERR(bh)) {
2272 			retval = PTR_ERR(bh);
2273 			bh = NULL;
2274 			goto out;
2275 		}
2276 		retval = add_dirent_to_buf(handle, &fname, dir, inode,
2277 					   NULL, bh);
2278 		if (retval != -ENOSPC)
2279 			goto out;
2280 
2281 		if (blocks == 1 && !dx_fallback &&
2282 		    ext4_has_feature_dir_index(sb)) {
2283 			retval = make_indexed_dir(handle, &fname, dir,
2284 						  inode, bh);
2285 			bh = NULL; /* make_indexed_dir releases bh */
2286 			goto out;
2287 		}
2288 		brelse(bh);
2289 	}
2290 	bh = ext4_append(handle, dir, &block);
2291 add_to_new_block:
2292 	if (IS_ERR(bh)) {
2293 		retval = PTR_ERR(bh);
2294 		bh = NULL;
2295 		goto out;
2296 	}
2297 	de = (struct ext4_dir_entry_2 *) bh->b_data;
2298 	de->inode = 0;
2299 	de->rec_len = ext4_rec_len_to_disk(blocksize - csum_size, blocksize);
2300 
2301 	if (csum_size)
2302 		ext4_initialize_dirent_tail(bh, blocksize);
2303 
2304 	retval = add_dirent_to_buf(handle, &fname, dir, inode, de, bh);
2305 out:
2306 	ext4_fname_free_filename(&fname);
2307 	brelse(bh);
2308 	if (retval == 0)
2309 		ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
2310 	return retval;
2311 }
2312 
2313 static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
2314 			  struct inode *inode)
2315 {
2316 	struct inode *dir = d_inode(dentry->d_parent);
2317 
2318 	if (fscrypt_is_nokey_name(dentry))
2319 		return -ENOKEY;
2320 	return __ext4_add_entry(handle, dir, &dentry->d_name, inode);
2321 }
2322 
2323 /*
2324  * Returns 0 for success, or a negative error value
2325  */
2326 static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
2327 			     struct inode *dir, struct inode *inode)
2328 {
2329 	struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
2330 	struct dx_entry *entries, *at;
2331 	struct buffer_head *bh;
2332 	struct super_block *sb = dir->i_sb;
2333 	struct ext4_dir_entry_2 *de;
2334 	int restart;
2335 	int err;
2336 
2337 again:
2338 	restart = 0;
2339 	frame = dx_probe(fname, dir, NULL, frames);
2340 	if (IS_ERR(frame))
2341 		return PTR_ERR(frame);
2342 	entries = frame->entries;
2343 	at = frame->at;
2344 	bh = ext4_read_dirblock(dir, dx_get_block(frame->at), DIRENT_HTREE);
2345 	if (IS_ERR(bh)) {
2346 		err = PTR_ERR(bh);
2347 		bh = NULL;
2348 		goto cleanup;
2349 	}
2350 
2351 	BUFFER_TRACE(bh, "get_write_access");
2352 	err = ext4_journal_get_write_access(handle, sb, bh, EXT4_JTR_NONE);
2353 	if (err)
2354 		goto journal_error;
2355 
2356 	err = add_dirent_to_buf(handle, fname, dir, inode, NULL, bh);
2357 	if (err != -ENOSPC)
2358 		goto cleanup;
2359 
2360 	err = 0;
2361 	/* Block full, should compress but for now just split */
2362 	dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
2363 		       dx_get_count(entries), dx_get_limit(entries)));
2364 	/* Need to split index? */
2365 	if (dx_get_count(entries) == dx_get_limit(entries)) {
2366 		ext4_lblk_t newblock;
2367 		int levels = frame - frames + 1;
2368 		unsigned int icount;
2369 		int add_level = 1;
2370 		struct dx_entry *entries2;
2371 		struct dx_node *node2;
2372 		struct buffer_head *bh2;
2373 
2374 		while (frame > frames) {
2375 			if (dx_get_count((frame - 1)->entries) <
2376 			    dx_get_limit((frame - 1)->entries)) {
2377 				add_level = 0;
2378 				break;
2379 			}
2380 			frame--; /* split higher index block */
2381 			at = frame->at;
2382 			entries = frame->entries;
2383 			restart = 1;
2384 		}
2385 		if (add_level && levels == ext4_dir_htree_level(sb)) {
2386 			ext4_warning(sb, "Directory (ino: %llu) index full, "
2387 					 "reach max htree level :%d",
2388 					 dir->i_ino, levels);
2389 			if (ext4_dir_htree_level(sb) < EXT4_HTREE_LEVEL) {
2390 				ext4_warning(sb, "Large directory feature is "
2391 						 "not enabled on this "
2392 						 "filesystem");
2393 			}
2394 			err = -ENOSPC;
2395 			goto cleanup;
2396 		}
2397 		icount = dx_get_count(entries);
2398 		bh2 = ext4_append(handle, dir, &newblock);
2399 		if (IS_ERR(bh2)) {
2400 			err = PTR_ERR(bh2);
2401 			goto cleanup;
2402 		}
2403 		node2 = (struct dx_node *)(bh2->b_data);
2404 		entries2 = node2->entries;
2405 		memset(&node2->fake, 0, sizeof(struct fake_dirent));
2406 		node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize,
2407 							   sb->s_blocksize);
2408 		BUFFER_TRACE(frame->bh, "get_write_access");
2409 		err = ext4_journal_get_write_access(handle, sb, frame->bh,
2410 						    EXT4_JTR_NONE);
2411 		if (err) {
2412 			brelse(bh2);
2413 			goto journal_error;
2414 		}
2415 		if (!add_level) {
2416 			unsigned icount1 = icount/2, icount2 = icount - icount1;
2417 			unsigned hash2 = dx_get_hash(entries + icount1);
2418 			dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
2419 				       icount1, icount2));
2420 
2421 			BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
2422 			err = ext4_journal_get_write_access(handle, sb,
2423 							    (frame - 1)->bh,
2424 							    EXT4_JTR_NONE);
2425 			if (err) {
2426 				brelse(bh2);
2427 				goto journal_error;
2428 			}
2429 
2430 			memcpy((char *) entries2, (char *) (entries + icount1),
2431 			       icount2 * sizeof(struct dx_entry));
2432 			dx_set_count(entries, icount1);
2433 			dx_set_count(entries2, icount2);
2434 			dx_set_limit(entries2, dx_node_limit(dir));
2435 
2436 			/* Which index block gets the new entry? */
2437 			if (at - entries >= icount1) {
2438 				frame->at = at - entries - icount1 + entries2;
2439 				frame->entries = entries = entries2;
2440 				swap(frame->bh, bh2);
2441 			}
2442 			dx_insert_block((frame - 1), hash2, newblock);
2443 			dxtrace(dx_show_index("node", frame->entries));
2444 			dxtrace(dx_show_index("node",
2445 			       ((struct dx_node *) bh2->b_data)->entries));
2446 			err = ext4_handle_dirty_dx_node(handle, dir, bh2);
2447 			if (err) {
2448 				brelse(bh2);
2449 				goto journal_error;
2450 			}
2451 			brelse (bh2);
2452 			err = ext4_handle_dirty_dx_node(handle, dir,
2453 						   (frame - 1)->bh);
2454 			if (err)
2455 				goto journal_error;
2456 			err = ext4_handle_dirty_dx_node(handle, dir,
2457 							frame->bh);
2458 			if (restart || err)
2459 				goto journal_error;
2460 		} else {
2461 			struct dx_root *dxroot;
2462 			memcpy((char *) entries2, (char *) entries,
2463 			       icount * sizeof(struct dx_entry));
2464 			dx_set_limit(entries2, dx_node_limit(dir));
2465 
2466 			/* Set up root */
2467 			dx_set_count(entries, 1);
2468 			dx_set_block(entries + 0, newblock);
2469 			dxroot = (struct dx_root *)frames[0].bh->b_data;
2470 			dxroot->info.indirect_levels += 1;
2471 			dxtrace(printk(KERN_DEBUG
2472 				       "Creating %d level index...\n",
2473 				       dxroot->info.indirect_levels));
2474 			err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
2475 			if (err) {
2476 				brelse(bh2);
2477 				goto journal_error;
2478 			}
2479 			err = ext4_handle_dirty_dx_node(handle, dir, bh2);
2480 			brelse(bh2);
2481 			restart = 1;
2482 			goto journal_error;
2483 		}
2484 	}
2485 	de = do_split(handle, dir, &bh, frame, &fname->hinfo);
2486 	if (IS_ERR(de)) {
2487 		err = PTR_ERR(de);
2488 		goto cleanup;
2489 	}
2490 	err = add_dirent_to_buf(handle, fname, dir, inode, de, bh);
2491 	goto cleanup;
2492 
2493 journal_error:
2494 	ext4_std_error(dir->i_sb, err); /* this is a no-op if err == 0 */
2495 cleanup:
2496 	brelse(bh);
2497 	dx_release(frames);
2498 	/* @restart is true means htree-path has been changed, we need to
2499 	 * repeat dx_probe() to find out valid htree-path
2500 	 */
2501 	if (restart && err == 0)
2502 		goto again;
2503 	return err;
2504 }
2505 
2506 /*
2507  * ext4_generic_delete_entry deletes a directory entry by merging it
2508  * with the previous entry
2509  */
2510 int ext4_generic_delete_entry(struct inode *dir,
2511 			      struct ext4_dir_entry_2 *de_del,
2512 			      struct buffer_head *bh,
2513 			      void *entry_buf,
2514 			      int buf_size,
2515 			      int csum_size)
2516 {
2517 	struct ext4_dir_entry_2 *de, *pde;
2518 	unsigned int blocksize = dir->i_sb->s_blocksize;
2519 	int i;
2520 
2521 	i = 0;
2522 	pde = NULL;
2523 	de = entry_buf;
2524 	while (i < buf_size - csum_size) {
2525 		if (ext4_check_dir_entry(dir, NULL, de, bh,
2526 					 entry_buf, buf_size, i))
2527 			return -EFSCORRUPTED;
2528 		if (de == de_del)  {
2529 			if (pde) {
2530 				pde->rec_len = ext4_rec_len_to_disk(
2531 					ext4_rec_len_from_disk(pde->rec_len,
2532 							       blocksize) +
2533 					ext4_rec_len_from_disk(de->rec_len,
2534 							       blocksize),
2535 					blocksize);
2536 
2537 				/* wipe entire dir_entry */
2538 				memset(de, 0, ext4_rec_len_from_disk(de->rec_len,
2539 								blocksize));
2540 			} else {
2541 				/* wipe dir_entry excluding the rec_len field */
2542 				de->inode = 0;
2543 				memset(&de->name_len, 0,
2544 					ext4_rec_len_from_disk(de->rec_len,
2545 								blocksize) -
2546 					offsetof(struct ext4_dir_entry_2,
2547 								name_len));
2548 			}
2549 
2550 			inode_inc_iversion(dir);
2551 			return 0;
2552 		}
2553 		i += ext4_rec_len_from_disk(de->rec_len, blocksize);
2554 		pde = de;
2555 		de = ext4_next_entry(de, blocksize);
2556 	}
2557 	return -ENOENT;
2558 }
2559 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_generic_delete_entry);
2560 
2561 static int ext4_delete_entry(handle_t *handle,
2562 			     struct inode *dir,
2563 			     struct ext4_dir_entry_2 *de_del,
2564 			     struct buffer_head *bh)
2565 {
2566 	int err, csum_size = 0;
2567 
2568 	if (ext4_has_inline_data(dir)) {
2569 		int has_inline_data = 1;
2570 		err = ext4_delete_inline_entry(handle, dir, de_del, bh,
2571 					       &has_inline_data);
2572 		if (has_inline_data)
2573 			return err;
2574 	}
2575 
2576 	if (ext4_has_feature_metadata_csum(dir->i_sb))
2577 		csum_size = sizeof(struct ext4_dir_entry_tail);
2578 
2579 	BUFFER_TRACE(bh, "get_write_access");
2580 	err = ext4_journal_get_write_access(handle, dir->i_sb, bh,
2581 					    EXT4_JTR_NONE);
2582 	if (unlikely(err))
2583 		goto out;
2584 
2585 	err = ext4_generic_delete_entry(dir, de_del, bh, bh->b_data,
2586 					dir->i_sb->s_blocksize, csum_size);
2587 	if (err)
2588 		goto out;
2589 
2590 	BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
2591 	err = ext4_handle_dirty_dirblock(handle, dir, bh);
2592 	if (unlikely(err))
2593 		goto out;
2594 
2595 	return 0;
2596 out:
2597 	if (err != -ENOENT)
2598 		ext4_std_error(dir->i_sb, err);
2599 	return err;
2600 }
2601 
2602 /*
2603  * Set directory link count to 1 if nlinks > EXT4_LINK_MAX, or if nlinks == 2
2604  * since this indicates that nlinks count was previously 1 to avoid overflowing
2605  * the 16-bit i_links_count field on disk.  Directories with i_nlink == 1 mean
2606  * that subdirectory link counts are not being maintained accurately.
2607  *
2608  * The caller has already checked for i_nlink overflow in case the DIR_LINK
2609  * feature is not enabled and returned -EMLINK.  The is_dx() check is a proxy
2610  * for checking S_ISDIR(inode) (since the INODE_INDEX feature will not be set
2611  * on regular files) and to avoid creating huge/slow non-HTREE directories.
2612  */
2613 static void ext4_inc_count(struct inode *inode)
2614 {
2615 	inc_nlink(inode);
2616 	if (is_dx(inode) &&
2617 	    (inode->i_nlink > EXT4_LINK_MAX || inode->i_nlink == 2))
2618 		set_nlink(inode, 1);
2619 }
2620 
2621 /*
2622  * If a directory had nlink == 1, then we should let it be 1. This indicates
2623  * directory has >EXT4_LINK_MAX subdirs.
2624  */
2625 static void ext4_dec_count(struct inode *inode)
2626 {
2627 	if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
2628 		drop_nlink(inode);
2629 }
2630 
2631 
2632 /*
2633  * Add non-directory inode to a directory. On success, the inode reference is
2634  * consumed by dentry is instantiation. This is also indicated by clearing of
2635  * *inodep pointer. On failure, the caller is responsible for dropping the
2636  * inode reference in the safe context.
2637  */
2638 static int ext4_add_nondir(handle_t *handle,
2639 		struct dentry *dentry, struct inode **inodep)
2640 {
2641 	struct inode *dir = d_inode(dentry->d_parent);
2642 	struct inode *inode = *inodep;
2643 	int err = ext4_add_entry(handle, dentry, inode);
2644 	if (!err) {
2645 		err = ext4_mark_inode_dirty(handle, inode);
2646 		if (IS_DIRSYNC(dir))
2647 			ext4_handle_sync(handle);
2648 		d_instantiate_new(dentry, inode);
2649 		*inodep = NULL;
2650 		return err;
2651 	}
2652 	drop_nlink(inode);
2653 	ext4_mark_inode_dirty(handle, inode);
2654 	ext4_orphan_add(handle, inode);
2655 	unlock_new_inode(inode);
2656 	return err;
2657 }
2658 
2659 /*
2660  * By the time this is called, we already have created
2661  * the directory cache entry for the new file, but it
2662  * is so far negative - it has no inode.
2663  *
2664  * If the create succeeds, we fill in the inode information
2665  * with d_instantiate().
2666  */
2667 static int ext4_create(const struct mnt_idmap *idmap, struct inode *dir,
2668 		       struct dentry *dentry, umode_t mode)
2669 {
2670 	handle_t *handle;
2671 	struct inode *inode;
2672 	int err, credits, retries = 0;
2673 
2674 	err = dquot_initialize(dir);
2675 	if (err)
2676 		return err;
2677 
2678 	credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2679 		   EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
2680 retry:
2681 	inode = ext4_new_inode_start_handle(idmap, dir, mode, &dentry->d_name,
2682 					    0, NULL, EXT4_HT_DIR, credits);
2683 	handle = ext4_journal_current_handle();
2684 	err = PTR_ERR(inode);
2685 	if (!IS_ERR(inode)) {
2686 		inode->i_op = &ext4_file_inode_operations;
2687 		inode->i_fop = &ext4_file_operations;
2688 		ext4_set_aops(inode);
2689 		err = ext4_add_nondir(handle, dentry, &inode);
2690 		if (!err)
2691 			ext4_fc_track_create(handle, dentry);
2692 	}
2693 	if (handle)
2694 		ext4_journal_stop(handle);
2695 	if (!IS_ERR_OR_NULL(inode))
2696 		iput(inode);
2697 	if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2698 		goto retry;
2699 	return err;
2700 }
2701 
2702 static int ext4_mknod(const struct mnt_idmap *idmap, struct inode *dir,
2703 		      struct dentry *dentry, umode_t mode, dev_t rdev)
2704 {
2705 	handle_t *handle;
2706 	struct inode *inode;
2707 	int err, credits, retries = 0;
2708 
2709 	err = dquot_initialize(dir);
2710 	if (err)
2711 		return err;
2712 
2713 	credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2714 		   EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
2715 retry:
2716 	inode = ext4_new_inode_start_handle(idmap, dir, mode, &dentry->d_name,
2717 					    0, NULL, EXT4_HT_DIR, credits);
2718 	handle = ext4_journal_current_handle();
2719 	err = PTR_ERR(inode);
2720 	if (!IS_ERR(inode)) {
2721 		init_special_inode(inode, inode->i_mode, rdev);
2722 		inode->i_op = &ext4_special_inode_operations;
2723 		err = ext4_add_nondir(handle, dentry, &inode);
2724 		if (!err)
2725 			ext4_fc_track_create(handle, dentry);
2726 	}
2727 	if (handle)
2728 		ext4_journal_stop(handle);
2729 	if (!IS_ERR_OR_NULL(inode))
2730 		iput(inode);
2731 	if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2732 		goto retry;
2733 	return err;
2734 }
2735 
2736 static int ext4_tmpfile(const struct mnt_idmap *idmap, struct inode *dir,
2737 			struct file *file, umode_t mode)
2738 {
2739 	handle_t *handle;
2740 	struct inode *inode;
2741 	int err, retries = 0;
2742 
2743 	err = dquot_initialize(dir);
2744 	if (err)
2745 		return err;
2746 
2747 retry:
2748 	inode = ext4_new_inode_start_handle(idmap, dir, mode,
2749 					    NULL, 0, NULL,
2750 					    EXT4_HT_DIR,
2751 			EXT4_MAXQUOTAS_TRANS_BLOCKS(dir->i_sb) +
2752 			  4 + EXT4_XATTR_TRANS_BLOCKS);
2753 	handle = ext4_journal_current_handle();
2754 	err = PTR_ERR(inode);
2755 	if (!IS_ERR(inode)) {
2756 		inode->i_op = &ext4_file_inode_operations;
2757 		inode->i_fop = &ext4_file_operations;
2758 		ext4_set_aops(inode);
2759 		d_tmpfile(file, inode);
2760 		err = ext4_orphan_add(handle, inode);
2761 		if (err)
2762 			goto err_unlock_inode;
2763 		mark_inode_dirty(inode);
2764 		unlock_new_inode(inode);
2765 	}
2766 	if (handle)
2767 		ext4_journal_stop(handle);
2768 	if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2769 		goto retry;
2770 	return finish_open_simple(file, err);
2771 err_unlock_inode:
2772 	ext4_journal_stop(handle);
2773 	unlock_new_inode(inode);
2774 	return err;
2775 }
2776 
2777 int ext4_init_dirblock(handle_t *handle, struct inode *inode,
2778 		       struct buffer_head *bh, unsigned int parent_ino,
2779 		       void *inline_buf, int inline_size)
2780 {
2781 	struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *) bh->b_data;
2782 	size_t			blocksize = bh->b_size;
2783 	int			csum_size = 0, header_size;
2784 
2785 	if (ext4_has_feature_metadata_csum(inode->i_sb))
2786 		csum_size = sizeof(struct ext4_dir_entry_tail);
2787 
2788 	de->inode = cpu_to_le32(inode->i_ino);
2789 	de->name_len = 1;
2790 	de->rec_len = ext4_rec_len_to_disk(ext4_dir_rec_len(de->name_len, NULL),
2791 					   blocksize);
2792 	memcpy(de->name, ".", 2);
2793 	ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2794 
2795 	de = ext4_next_entry(de, blocksize);
2796 	de->inode = cpu_to_le32(parent_ino);
2797 	de->name_len = 2;
2798 	memcpy(de->name, "..", 3);
2799 	ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2800 	if (inline_buf) {
2801 		de->rec_len = ext4_rec_len_to_disk(
2802 					ext4_dir_rec_len(de->name_len, NULL),
2803 					blocksize);
2804 		de = ext4_next_entry(de, blocksize);
2805 		header_size = (char *)de - bh->b_data;
2806 		memcpy((void *)de, inline_buf, inline_size);
2807 		ext4_update_final_de(bh->b_data, inline_size + header_size,
2808 			blocksize - csum_size);
2809 	} else {
2810 		de->rec_len = ext4_rec_len_to_disk(blocksize -
2811 					(csum_size + ext4_dir_rec_len(1, NULL)),
2812 					blocksize);
2813 	}
2814 
2815 	if (csum_size)
2816 		ext4_initialize_dirent_tail(bh, blocksize);
2817 	BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata");
2818 	set_buffer_uptodate(bh);
2819 	set_buffer_verified(bh);
2820 	return ext4_handle_dirty_dirblock(handle, inode, bh);
2821 }
2822 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_init_dirblock);
2823 
2824 int ext4_init_new_dir(handle_t *handle, struct inode *dir,
2825 			     struct inode *inode)
2826 {
2827 	struct buffer_head *dir_block = NULL;
2828 	ext4_lblk_t block = 0;
2829 	int err;
2830 
2831 	if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2832 		err = ext4_try_create_inline_dir(handle, dir, inode);
2833 		if (err < 0 && err != -ENOSPC)
2834 			goto out;
2835 		if (!err)
2836 			goto out;
2837 	}
2838 
2839 	set_nlink(inode, 2);
2840 	inode->i_size = 0;
2841 	dir_block = ext4_append(handle, inode, &block);
2842 	if (IS_ERR(dir_block))
2843 		return PTR_ERR(dir_block);
2844 	err = ext4_init_dirblock(handle, inode, dir_block, dir->i_ino, NULL, 0);
2845 out:
2846 	brelse(dir_block);
2847 	return err;
2848 }
2849 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_init_new_dir);
2850 
2851 static struct dentry *ext4_mkdir(const struct mnt_idmap *idmap, struct inode *dir,
2852 				 struct dentry *dentry, umode_t mode)
2853 {
2854 	handle_t *handle;
2855 	struct inode *inode;
2856 	int err, err2 = 0, credits, retries = 0;
2857 
2858 	if (EXT4_DIR_LINK_MAX(dir))
2859 		return ERR_PTR(-EMLINK);
2860 
2861 	err = dquot_initialize(dir);
2862 	if (err)
2863 		return ERR_PTR(err);
2864 
2865 	credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2866 		   EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
2867 retry:
2868 	inode = ext4_new_inode_start_handle(idmap, dir, mode,
2869 					    &dentry->d_name,
2870 					    0, NULL, EXT4_HT_DIR, credits);
2871 	handle = ext4_journal_current_handle();
2872 	err = PTR_ERR(inode);
2873 	if (IS_ERR(inode))
2874 		goto out_stop;
2875 
2876 	inode->i_op = &ext4_dir_inode_operations;
2877 	inode->i_fop = &ext4_dir_operations;
2878 	err = ext4_init_new_dir(handle, dir, inode);
2879 	if (err)
2880 		goto out_clear_inode;
2881 	err = ext4_mark_inode_dirty(handle, inode);
2882 	if (!err)
2883 		err = ext4_add_entry(handle, dentry, inode);
2884 	if (err) {
2885 out_clear_inode:
2886 		clear_nlink(inode);
2887 		ext4_orphan_add(handle, inode);
2888 		unlock_new_inode(inode);
2889 		err2 = ext4_mark_inode_dirty(handle, inode);
2890 		if (unlikely(err2))
2891 			err = err2;
2892 		ext4_journal_stop(handle);
2893 		iput(inode);
2894 		goto out_retry;
2895 	}
2896 	ext4_inc_count(dir);
2897 
2898 	ext4_update_dx_flag(dir);
2899 	err = ext4_mark_inode_dirty(handle, dir);
2900 	if (err)
2901 		goto out_clear_inode;
2902 	d_instantiate_new(dentry, inode);
2903 	ext4_fc_track_create(handle, dentry);
2904 	if (IS_DIRSYNC(dir))
2905 		ext4_handle_sync(handle);
2906 
2907 out_stop:
2908 	if (handle)
2909 		ext4_journal_stop(handle);
2910 out_retry:
2911 	if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2912 		goto retry;
2913 	return err ? ERR_PTR(err) : NULL;
2914 }
2915 
2916 /*
2917  * routine to check that the specified directory is empty (for rmdir)
2918  */
2919 bool ext4_empty_dir(struct inode *inode)
2920 {
2921 	unsigned int offset;
2922 	struct buffer_head *bh;
2923 	struct ext4_dir_entry_2 *de;
2924 	struct super_block *sb;
2925 
2926 	if (ext4_has_inline_data(inode)) {
2927 		int has_inline_data = 1;
2928 		int ret;
2929 
2930 		ret = empty_inline_dir(inode, &has_inline_data);
2931 		if (has_inline_data)
2932 			return ret;
2933 	}
2934 
2935 	sb = inode->i_sb;
2936 	if (inode->i_size < ext4_dir_rec_len(1, NULL) +
2937 					ext4_dir_rec_len(2, NULL)) {
2938 		EXT4_ERROR_INODE(inode, "invalid size");
2939 		return false;
2940 	}
2941 	bh = ext4_read_dirblock(inode, 0, EITHER);
2942 	if (IS_ERR(bh))
2943 		return false;
2944 
2945 	de = (struct ext4_dir_entry_2 *) bh->b_data;
2946 	if (ext4_check_dir_entry(inode, NULL, de, bh, bh->b_data, bh->b_size,
2947 				 0) ||
2948 	    le32_to_cpu(de->inode) != inode->i_ino || de->name_len != 1 ||
2949 	    de->name[0] != '.') {
2950 		ext4_warning_inode(inode, "directory missing '.'");
2951 		brelse(bh);
2952 		return false;
2953 	}
2954 	offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
2955 	de = ext4_next_entry(de, sb->s_blocksize);
2956 	if (ext4_check_dir_entry(inode, NULL, de, bh, bh->b_data, bh->b_size,
2957 				 offset) ||
2958 	    le32_to_cpu(de->inode) == 0 || de->name_len != 2 ||
2959 	    de->name[0] != '.' || de->name[1] != '.') {
2960 		ext4_warning_inode(inode, "directory missing '..'");
2961 		brelse(bh);
2962 		return false;
2963 	}
2964 	offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
2965 	while (offset < inode->i_size) {
2966 		if (!(offset & (sb->s_blocksize - 1))) {
2967 			unsigned int lblock;
2968 			brelse(bh);
2969 			lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb);
2970 			bh = ext4_read_dirblock(inode, lblock, EITHER);
2971 			if (bh == NULL) {
2972 				offset += sb->s_blocksize;
2973 				continue;
2974 			}
2975 			if (IS_ERR(bh))
2976 				return false;
2977 		}
2978 		de = (struct ext4_dir_entry_2 *) (bh->b_data +
2979 					(offset & (sb->s_blocksize - 1)));
2980 		if (ext4_check_dir_entry(inode, NULL, de, bh,
2981 					 bh->b_data, bh->b_size, offset) ||
2982 		    le32_to_cpu(de->inode)) {
2983 			brelse(bh);
2984 			return false;
2985 		}
2986 		offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
2987 	}
2988 	brelse(bh);
2989 	return true;
2990 }
2991 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_empty_dir);
2992 
2993 static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
2994 {
2995 	int retval;
2996 	struct inode *inode;
2997 	struct buffer_head *bh;
2998 	struct ext4_dir_entry_2 *de = NULL;
2999 	handle_t *handle = NULL;
3000 
3001 	retval = ext4_emergency_state(dir->i_sb);
3002 	if (unlikely(retval))
3003 		return retval;
3004 
3005 	/* Initialize quotas before so that eventual writes go in
3006 	 * separate transaction */
3007 	retval = dquot_initialize(dir);
3008 	if (retval)
3009 		return retval;
3010 	retval = dquot_initialize(d_inode(dentry));
3011 	if (retval)
3012 		return retval;
3013 
3014 	retval = -ENOENT;
3015 	bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
3016 	if (IS_ERR(bh))
3017 		return PTR_ERR(bh);
3018 	if (!bh)
3019 		goto end_rmdir;
3020 
3021 	inode = d_inode(dentry);
3022 
3023 	retval = -EFSCORRUPTED;
3024 	if (le32_to_cpu(de->inode) != inode->i_ino)
3025 		goto end_rmdir;
3026 
3027 	retval = -ENOTEMPTY;
3028 	if (!ext4_empty_dir(inode))
3029 		goto end_rmdir;
3030 
3031 	handle = ext4_journal_start(dir, EXT4_HT_DIR,
3032 				    EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
3033 	if (IS_ERR(handle)) {
3034 		retval = PTR_ERR(handle);
3035 		handle = NULL;
3036 		goto end_rmdir;
3037 	}
3038 
3039 	if (IS_DIRSYNC(dir))
3040 		ext4_handle_sync(handle);
3041 
3042 	retval = ext4_delete_entry(handle, dir, de, bh);
3043 	if (retval)
3044 		goto end_rmdir;
3045 	if (!EXT4_DIR_LINK_EMPTY(inode))
3046 		ext4_warning_inode(inode,
3047 			     "empty directory '%.*s' has too many links (%u)",
3048 			     dentry->d_name.len, dentry->d_name.name,
3049 			     inode->i_nlink);
3050 	inode_inc_iversion(inode);
3051 	clear_nlink(inode);
3052 	/* There's no need to set i_disksize: the fact that i_nlink is
3053 	 * zero will ensure that the right thing happens during any
3054 	 * recovery. */
3055 	inode->i_size = 0;
3056 	ext4_orphan_add(handle, inode);
3057 	inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
3058 	inode_set_ctime_current(inode);
3059 	retval = ext4_mark_inode_dirty(handle, inode);
3060 	if (retval)
3061 		goto end_rmdir;
3062 	ext4_dec_count(dir);
3063 	ext4_update_dx_flag(dir);
3064 	ext4_fc_track_unlink(handle, dentry);
3065 	retval = ext4_mark_inode_dirty(handle, dir);
3066 
3067 end_rmdir:
3068 	brelse(bh);
3069 	if (handle)
3070 		ext4_journal_stop(handle);
3071 
3072 	/* VFS negative dentries are incompatible with Encoding and
3073 	 * Case-insensitiveness. Eventually we'll want avoid
3074 	 * invalidating the dentries here, alongside with returning the
3075 	 * negative dentries at ext4_lookup(), when it is better
3076 	 * supported by the VFS for the CI case.
3077 	 */
3078 	if (!retval && IS_ENABLED(CONFIG_UNICODE) && IS_CASEFOLDED(dir))
3079 		d_invalidate(dentry);
3080 
3081 	return retval;
3082 }
3083 
3084 int __ext4_unlink(struct inode *dir, const struct qstr *d_name,
3085 		  struct inode *inode,
3086 		  struct dentry *dentry /* NULL during fast_commit recovery */)
3087 {
3088 	int retval = -ENOENT;
3089 	struct buffer_head *bh;
3090 	struct ext4_dir_entry_2 *de = NULL;
3091 	handle_t *handle;
3092 	int skip_remove_dentry = 0;
3093 
3094 	/*
3095 	 * Keep this outside the transaction; it may have to set up the
3096 	 * directory's encryption key, which isn't GFP_NOFS-safe.
3097 	 */
3098 	bh = ext4_find_entry(dir, d_name, &de, NULL);
3099 	if (IS_ERR(bh))
3100 		return PTR_ERR(bh);
3101 
3102 	if (!bh)
3103 		return -ENOENT;
3104 
3105 	if (le32_to_cpu(de->inode) != inode->i_ino) {
3106 		/*
3107 		 * It's okay if we find dont find dentry which matches
3108 		 * the inode. That's because it might have gotten
3109 		 * renamed to a different inode number
3110 		 */
3111 		if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
3112 			skip_remove_dentry = 1;
3113 		else
3114 			goto out_bh;
3115 	}
3116 
3117 	handle = ext4_journal_start(dir, EXT4_HT_DIR,
3118 				    EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
3119 	if (IS_ERR(handle)) {
3120 		retval = PTR_ERR(handle);
3121 		goto out_bh;
3122 	}
3123 
3124 	if (IS_DIRSYNC(dir))
3125 		ext4_handle_sync(handle);
3126 
3127 	if (!skip_remove_dentry) {
3128 		retval = ext4_delete_entry(handle, dir, de, bh);
3129 		if (retval)
3130 			goto out_handle;
3131 		inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
3132 		ext4_update_dx_flag(dir);
3133 		retval = ext4_mark_inode_dirty(handle, dir);
3134 		if (retval)
3135 			goto out_handle;
3136 	} else {
3137 		retval = 0;
3138 	}
3139 	if (inode->i_nlink == 0)
3140 		ext4_warning_inode(inode, "Deleting file '%.*s' with no links",
3141 				   d_name->len, d_name->name);
3142 	else
3143 		drop_nlink(inode);
3144 	if (!inode->i_nlink)
3145 		ext4_orphan_add(handle, inode);
3146 	inode_set_ctime_current(inode);
3147 	retval = ext4_mark_inode_dirty(handle, inode);
3148 	if (dentry && !retval)
3149 		ext4_fc_track_unlink(handle, dentry);
3150 out_handle:
3151 	ext4_journal_stop(handle);
3152 out_bh:
3153 	brelse(bh);
3154 	return retval;
3155 }
3156 EXPORT_SYMBOL_FOR_EXT4_TEST(__ext4_unlink);
3157 
3158 static int ext4_unlink(struct inode *dir, struct dentry *dentry)
3159 {
3160 	int retval;
3161 
3162 	retval = ext4_emergency_state(dir->i_sb);
3163 	if (unlikely(retval))
3164 		return retval;
3165 
3166 	trace_ext4_unlink_enter(dir, dentry);
3167 	/*
3168 	 * Initialize quotas before so that eventual writes go
3169 	 * in separate transaction
3170 	 */
3171 	retval = dquot_initialize(dir);
3172 	if (retval)
3173 		goto out_trace;
3174 	retval = dquot_initialize(d_inode(dentry));
3175 	if (retval)
3176 		goto out_trace;
3177 
3178 	retval = __ext4_unlink(dir, &dentry->d_name, d_inode(dentry), dentry);
3179 
3180 	/* VFS negative dentries are incompatible with Encoding and
3181 	 * Case-insensitiveness. Eventually we'll want avoid
3182 	 * invalidating the dentries here, alongside with returning the
3183 	 * negative dentries at ext4_lookup(), when it is  better
3184 	 * supported by the VFS for the CI case.
3185 	 */
3186 	if (IS_ENABLED(CONFIG_UNICODE) && IS_CASEFOLDED(dir))
3187 		d_invalidate(dentry);
3188 
3189 out_trace:
3190 	trace_ext4_unlink_exit(dentry, retval);
3191 	return retval;
3192 }
3193 
3194 static int ext4_init_symlink_block(handle_t *handle, struct inode *inode,
3195 				   struct fscrypt_str *disk_link)
3196 {
3197 	struct buffer_head *bh;
3198 	char *kaddr;
3199 	int err = 0;
3200 
3201 	bh = ext4_bread(handle, inode, 0, EXT4_GET_BLOCKS_CREATE);
3202 	if (IS_ERR(bh))
3203 		return PTR_ERR(bh);
3204 
3205 	BUFFER_TRACE(bh, "get_write_access");
3206 	err = ext4_journal_get_write_access(handle, inode->i_sb, bh, EXT4_JTR_NONE);
3207 	if (err)
3208 		goto out;
3209 
3210 	kaddr = (char *)bh->b_data;
3211 	memcpy(kaddr, disk_link->name, disk_link->len);
3212 	inode->i_size = disk_link->len - 1;
3213 	EXT4_I(inode)->i_disksize = inode->i_size;
3214 	err = ext4_handle_dirty_metadata(handle, inode, bh);
3215 out:
3216 	brelse(bh);
3217 	return err;
3218 }
3219 
3220 static int ext4_symlink(const struct mnt_idmap *idmap, struct inode *dir,
3221 			struct dentry *dentry, const char *symname)
3222 {
3223 	handle_t *handle;
3224 	struct inode *inode;
3225 	int err, len = strlen(symname);
3226 	int credits;
3227 	struct fscrypt_str disk_link;
3228 	int retries = 0;
3229 
3230 	err = ext4_emergency_state(dir->i_sb);
3231 	if (unlikely(err))
3232 		return err;
3233 
3234 	err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize,
3235 				      &disk_link);
3236 	if (err)
3237 		return err;
3238 
3239 	err = dquot_initialize(dir);
3240 	if (err)
3241 		return err;
3242 
3243 	/*
3244 	 * EXT4_INDEX_EXTRA_TRANS_BLOCKS for addition of entry into the
3245 	 * directory. +3 for inode, inode bitmap, group descriptor allocation.
3246 	 * EXT4_DATA_TRANS_BLOCKS for the data block allocation and
3247 	 * modification.
3248 	 */
3249 	credits = EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
3250 		  EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3;
3251 retry:
3252 	inode = ext4_new_inode_start_handle(idmap, dir, S_IFLNK|S_IRWXUGO,
3253 					    &dentry->d_name, 0, NULL,
3254 					    EXT4_HT_DIR, credits);
3255 	handle = ext4_journal_current_handle();
3256 	if (IS_ERR(inode)) {
3257 		if (handle)
3258 			ext4_journal_stop(handle);
3259 		err = PTR_ERR(inode);
3260 		goto out_retry;
3261 	}
3262 
3263 	if (IS_ENCRYPTED(inode)) {
3264 		err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);
3265 		if (err)
3266 			goto err_drop_inode;
3267 		inode->i_op = &ext4_encrypted_symlink_inode_operations;
3268 	} else {
3269 		if ((disk_link.len > EXT4_N_BLOCKS * 4)) {
3270 			inode->i_op = &ext4_symlink_inode_operations;
3271 		} else {
3272 			inode->i_op = &ext4_fast_symlink_inode_operations;
3273 		}
3274 	}
3275 
3276 	if ((disk_link.len > EXT4_N_BLOCKS * 4)) {
3277 		/* alloc symlink block and fill it */
3278 		err = ext4_init_symlink_block(handle, inode, &disk_link);
3279 		if (err)
3280 			goto err_drop_inode;
3281 	} else {
3282 		/* clear the extent format for fast symlink */
3283 		ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
3284 		memcpy((char *)&EXT4_I(inode)->i_data, disk_link.name,
3285 		       disk_link.len);
3286 		inode->i_size = disk_link.len - 1;
3287 		EXT4_I(inode)->i_disksize = inode->i_size;
3288 		if (!IS_ENCRYPTED(inode))
3289 			inode_set_cached_link(inode, (char *)&EXT4_I(inode)->i_data,
3290 					      inode->i_size);
3291 	}
3292 	err = ext4_add_nondir(handle, dentry, &inode);
3293 	if (handle)
3294 		ext4_journal_stop(handle);
3295 	iput(inode);
3296 	goto out_retry;
3297 
3298 err_drop_inode:
3299 	clear_nlink(inode);
3300 	ext4_mark_inode_dirty(handle, inode);
3301 	ext4_orphan_add(handle, inode);
3302 	unlock_new_inode(inode);
3303 	if (handle)
3304 		ext4_journal_stop(handle);
3305 	iput(inode);
3306 out_retry:
3307 	if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
3308 		goto retry;
3309 	if (disk_link.name != (unsigned char *)symname)
3310 		kfree(disk_link.name);
3311 	return err;
3312 }
3313 
3314 int __ext4_link(struct inode *dir, struct inode *inode,
3315 		const struct qstr *d_name, struct dentry *dentry)
3316 {
3317 	handle_t *handle;
3318 	int err, retries = 0;
3319 retry:
3320 	handle = ext4_journal_start(dir, EXT4_HT_DIR,
3321 		(EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
3322 		 EXT4_INDEX_EXTRA_TRANS_BLOCKS) + 1);
3323 	if (IS_ERR(handle))
3324 		return PTR_ERR(handle);
3325 
3326 	if (IS_DIRSYNC(dir))
3327 		ext4_handle_sync(handle);
3328 
3329 	inode_set_ctime_current(inode);
3330 	ext4_inc_count(inode);
3331 
3332 	err = __ext4_add_entry(handle, dir, d_name, inode);
3333 	if (!err) {
3334 		err = ext4_mark_inode_dirty(handle, inode);
3335 		/* this can happen only for tmpfile being
3336 		 * linked the first time
3337 		 */
3338 		if (inode->i_nlink == 1)
3339 			ext4_orphan_del(handle, inode);
3340 		if (dentry)
3341 			ext4_fc_track_link(handle, inode, dentry);
3342 	} else {
3343 		drop_nlink(inode);
3344 	}
3345 	ext4_journal_stop(handle);
3346 	if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
3347 		goto retry;
3348 	return err;
3349 }
3350 EXPORT_SYMBOL_FOR_EXT4_TEST(__ext4_link);
3351 
3352 static int ext4_link(struct dentry *old_dentry,
3353 		     struct inode *dir, struct dentry *dentry)
3354 {
3355 	struct inode *inode = d_inode(old_dentry);
3356 	int err;
3357 
3358 	if (inode->i_nlink >= EXT4_LINK_MAX)
3359 		return -EMLINK;
3360 
3361 	err = fscrypt_prepare_link(old_dentry, dir, dentry);
3362 	if (err)
3363 		return err;
3364 
3365 	if ((ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT)) &&
3366 	    (!projid_eq(EXT4_I(dir)->i_projid,
3367 			EXT4_I(old_dentry->d_inode)->i_projid)))
3368 		return -EXDEV;
3369 
3370 	err = dquot_initialize(dir);
3371 	if (err)
3372 		return err;
3373 	err = __ext4_link(dir, inode, &dentry->d_name, dentry);
3374 	if (!err) {
3375 		ihold(inode);
3376 		d_instantiate(dentry, inode);
3377 	}
3378 	return err;
3379 }
3380 /*
3381  * Try to find buffer head where contains the parent block.
3382  * It should be the inode block if it is inlined or the 1st block
3383  * if it is a normal dir.
3384  */
3385 static struct buffer_head *ext4_get_first_dir_block(handle_t *handle,
3386 					struct inode *inode,
3387 					int *retval,
3388 					struct ext4_dir_entry_2 **parent_de,
3389 					int *inlined)
3390 {
3391 	struct buffer_head *bh;
3392 
3393 	if (!ext4_has_inline_data(inode)) {
3394 		struct ext4_dir_entry_2 *de;
3395 		unsigned int offset;
3396 
3397 		bh = ext4_read_dirblock(inode, 0, EITHER);
3398 		if (IS_ERR(bh)) {
3399 			*retval = PTR_ERR(bh);
3400 			return NULL;
3401 		}
3402 
3403 		de = (struct ext4_dir_entry_2 *) bh->b_data;
3404 		if (ext4_check_dir_entry(inode, NULL, de, bh, bh->b_data,
3405 					 bh->b_size, 0) ||
3406 		    le32_to_cpu(de->inode) != inode->i_ino ||
3407 		    de->name_len != 1 || de->name[0] != '.') {
3408 			EXT4_ERROR_INODE(inode, "directory missing '.'");
3409 			brelse(bh);
3410 			*retval = -EFSCORRUPTED;
3411 			return NULL;
3412 		}
3413 		offset = ext4_rec_len_from_disk(de->rec_len,
3414 						inode->i_sb->s_blocksize);
3415 		de = ext4_next_entry(de, inode->i_sb->s_blocksize);
3416 		if (ext4_check_dir_entry(inode, NULL, de, bh, bh->b_data,
3417 					 bh->b_size, offset) ||
3418 		    le32_to_cpu(de->inode) == 0 || de->name_len != 2 ||
3419 		    de->name[0] != '.' || de->name[1] != '.') {
3420 			EXT4_ERROR_INODE(inode, "directory missing '..'");
3421 			brelse(bh);
3422 			*retval = -EFSCORRUPTED;
3423 			return NULL;
3424 		}
3425 		*parent_de = de;
3426 
3427 		return bh;
3428 	}
3429 
3430 	*inlined = 1;
3431 	return ext4_get_first_inline_block(inode, parent_de, retval);
3432 }
3433 
3434 struct ext4_renament {
3435 	struct inode *dir;
3436 	struct dentry *dentry;
3437 	struct inode *inode;
3438 	bool is_dir;
3439 	int dir_nlink_delta;
3440 
3441 	/* entry for "dentry" */
3442 	struct buffer_head *bh;
3443 	struct ext4_dir_entry_2 *de;
3444 	int inlined;
3445 
3446 	/* entry for ".." in inode if it's a directory */
3447 	struct buffer_head *dir_bh;
3448 	struct ext4_dir_entry_2 *parent_de;
3449 	int dir_inlined;
3450 };
3451 
3452 static int ext4_rename_dir_prepare(handle_t *handle, struct ext4_renament *ent, bool is_cross)
3453 {
3454 	int retval;
3455 
3456 	ent->is_dir = true;
3457 	if (!is_cross)
3458 		return 0;
3459 
3460 	ent->dir_bh = ext4_get_first_dir_block(handle, ent->inode,
3461 					      &retval, &ent->parent_de,
3462 					      &ent->dir_inlined);
3463 	if (!ent->dir_bh)
3464 		return retval;
3465 	if (le32_to_cpu(ent->parent_de->inode) != ent->dir->i_ino)
3466 		return -EFSCORRUPTED;
3467 	BUFFER_TRACE(ent->dir_bh, "get_write_access");
3468 	return ext4_journal_get_write_access(handle, ent->dir->i_sb,
3469 					     ent->dir_bh, EXT4_JTR_NONE);
3470 }
3471 
3472 static int ext4_rename_dir_finish(handle_t *handle, struct ext4_renament *ent,
3473 				  unsigned dir_ino)
3474 {
3475 	int retval;
3476 
3477 	if (!ent->dir_bh)
3478 		return 0;
3479 
3480 	ent->parent_de->inode = cpu_to_le32(dir_ino);
3481 	BUFFER_TRACE(ent->dir_bh, "call ext4_handle_dirty_metadata");
3482 	if (!ent->dir_inlined) {
3483 		if (is_dx(ent->inode)) {
3484 			retval = ext4_handle_dirty_dx_node(handle,
3485 							   ent->inode,
3486 							   ent->dir_bh);
3487 		} else {
3488 			retval = ext4_handle_dirty_dirblock(handle, ent->inode,
3489 							    ent->dir_bh);
3490 		}
3491 	} else {
3492 		retval = ext4_mark_inode_dirty(handle, ent->inode);
3493 	}
3494 	if (retval) {
3495 		ext4_std_error(ent->dir->i_sb, retval);
3496 		return retval;
3497 	}
3498 	return 0;
3499 }
3500 
3501 static int ext4_setent(handle_t *handle, struct ext4_renament *ent,
3502 		       unsigned ino, unsigned file_type)
3503 {
3504 	int retval, retval2;
3505 
3506 	BUFFER_TRACE(ent->bh, "get write access");
3507 	retval = ext4_journal_get_write_access(handle, ent->dir->i_sb, ent->bh,
3508 					       EXT4_JTR_NONE);
3509 	if (retval)
3510 		return retval;
3511 	ent->de->inode = cpu_to_le32(ino);
3512 	if (ext4_has_feature_filetype(ent->dir->i_sb))
3513 		ent->de->file_type = file_type;
3514 	inode_inc_iversion(ent->dir);
3515 	inode_set_mtime_to_ts(ent->dir, inode_set_ctime_current(ent->dir));
3516 	retval = ext4_mark_inode_dirty(handle, ent->dir);
3517 	BUFFER_TRACE(ent->bh, "call ext4_handle_dirty_metadata");
3518 	if (!ent->inlined) {
3519 		retval2 = ext4_handle_dirty_dirblock(handle, ent->dir, ent->bh);
3520 		if (unlikely(retval2)) {
3521 			ext4_std_error(ent->dir->i_sb, retval2);
3522 			return retval2;
3523 		}
3524 	}
3525 	return retval;
3526 }
3527 
3528 static void ext4_resetent(handle_t *handle, struct ext4_renament *ent,
3529 			  unsigned ino, unsigned file_type)
3530 {
3531 	struct ext4_renament old = *ent;
3532 	int retval = 0;
3533 
3534 	/*
3535 	 * old->de could have moved from under us during make indexed dir,
3536 	 * so the old->de may no longer valid and need to find it again
3537 	 * before reset old inode info.
3538 	 */
3539 	old.bh = ext4_find_entry(old.dir, &old.dentry->d_name, &old.de,
3540 				 &old.inlined);
3541 	if (IS_ERR(old.bh))
3542 		retval = PTR_ERR(old.bh);
3543 	if (!old.bh)
3544 		retval = -ENOENT;
3545 	if (retval) {
3546 		ext4_std_error(old.dir->i_sb, retval);
3547 		return;
3548 	}
3549 
3550 	ext4_setent(handle, &old, ino, file_type);
3551 	brelse(old.bh);
3552 }
3553 
3554 static int ext4_find_delete_entry(handle_t *handle, struct inode *dir,
3555 				  const struct qstr *d_name)
3556 {
3557 	int retval = -ENOENT;
3558 	struct buffer_head *bh;
3559 	struct ext4_dir_entry_2 *de = NULL;
3560 
3561 	bh = ext4_find_entry(dir, d_name, &de, NULL);
3562 	if (IS_ERR(bh))
3563 		return PTR_ERR(bh);
3564 	if (bh) {
3565 		retval = ext4_delete_entry(handle, dir, de, bh);
3566 		brelse(bh);
3567 	}
3568 	return retval;
3569 }
3570 
3571 static void ext4_rename_delete(handle_t *handle, struct ext4_renament *ent,
3572 			       int force_reread)
3573 {
3574 	int retval;
3575 	/*
3576 	 * ent->de could have moved from under us during htree split, so make
3577 	 * sure that we are deleting the right entry.  We might also be pointing
3578 	 * to a stale entry in the unused part of ent->bh so just checking inum
3579 	 * and the name isn't enough.
3580 	 */
3581 	if (le32_to_cpu(ent->de->inode) != ent->inode->i_ino ||
3582 	    ent->de->name_len != ent->dentry->d_name.len ||
3583 	    strncmp(ent->de->name, ent->dentry->d_name.name,
3584 		    ent->de->name_len) ||
3585 	    force_reread) {
3586 		retval = ext4_find_delete_entry(handle, ent->dir,
3587 						&ent->dentry->d_name);
3588 	} else {
3589 		retval = ext4_delete_entry(handle, ent->dir, ent->de, ent->bh);
3590 		if (retval == -ENOENT) {
3591 			retval = ext4_find_delete_entry(handle, ent->dir,
3592 							&ent->dentry->d_name);
3593 		}
3594 	}
3595 
3596 	if (retval) {
3597 		ext4_warning_inode(ent->dir,
3598 				   "Deleting old file: nlink %d, error=%d",
3599 				   ent->dir->i_nlink, retval);
3600 	}
3601 }
3602 
3603 static void ext4_update_dir_count(handle_t *handle, struct ext4_renament *ent)
3604 {
3605 	if (ent->dir_nlink_delta) {
3606 		if (ent->dir_nlink_delta == -1)
3607 			ext4_dec_count(ent->dir);
3608 		else
3609 			ext4_inc_count(ent->dir);
3610 		ext4_mark_inode_dirty(handle, ent->dir);
3611 	}
3612 }
3613 
3614 static struct inode *ext4_whiteout_for_rename(const struct mnt_idmap *idmap,
3615 					      struct ext4_renament *ent,
3616 					      int credits, handle_t **h)
3617 {
3618 	struct inode *wh;
3619 	handle_t *handle;
3620 	int retries = 0;
3621 
3622 	/*
3623 	 * for inode block, sb block, group summaries,
3624 	 * and inode bitmap
3625 	 */
3626 	credits += (EXT4_MAXQUOTAS_TRANS_BLOCKS(ent->dir->i_sb) +
3627 		    EXT4_XATTR_TRANS_BLOCKS + 4);
3628 retry:
3629 	wh = ext4_new_inode_start_handle(idmap, ent->dir,
3630 					 S_IFCHR | WHITEOUT_MODE,
3631 					 &ent->dentry->d_name, 0, NULL,
3632 					 EXT4_HT_DIR, credits);
3633 
3634 	handle = ext4_journal_current_handle();
3635 	if (IS_ERR(wh)) {
3636 		if (handle)
3637 			ext4_journal_stop(handle);
3638 		if (PTR_ERR(wh) == -ENOSPC &&
3639 		    ext4_should_retry_alloc(ent->dir->i_sb, &retries))
3640 			goto retry;
3641 	} else {
3642 		*h = handle;
3643 		init_special_inode(wh, wh->i_mode, WHITEOUT_DEV);
3644 		wh->i_op = &ext4_special_inode_operations;
3645 	}
3646 	return wh;
3647 }
3648 
3649 /*
3650  * Anybody can rename anything with this: the permission checks are left to the
3651  * higher-level routines.
3652  *
3653  * n.b.  old_{dentry,inode) refers to the source dentry/inode
3654  * while new_{dentry,inode) refers to the destination dentry/inode
3655  * This comes from rename(const char *oldpath, const char *newpath)
3656  */
3657 static int ext4_rename(const struct mnt_idmap *idmap, struct inode *old_dir,
3658 		       struct dentry *old_dentry, struct inode *new_dir,
3659 		       struct dentry *new_dentry, unsigned int flags)
3660 {
3661 	handle_t *handle = NULL;
3662 	struct ext4_renament old = {
3663 		.dir = old_dir,
3664 		.dentry = old_dentry,
3665 		.inode = d_inode(old_dentry),
3666 	};
3667 	struct ext4_renament new = {
3668 		.dir = new_dir,
3669 		.dentry = new_dentry,
3670 		.inode = d_inode(new_dentry),
3671 	};
3672 	int force_reread;
3673 	int retval;
3674 	struct inode *whiteout = NULL;
3675 	int credits;
3676 	u8 old_file_type;
3677 
3678 	if (new.inode && new.inode->i_nlink == 0) {
3679 		EXT4_ERROR_INODE(new.inode,
3680 				 "target of rename is already freed");
3681 		return -EFSCORRUPTED;
3682 	}
3683 
3684 	if ((ext4_test_inode_flag(new_dir, EXT4_INODE_PROJINHERIT)) &&
3685 	    (!projid_eq(EXT4_I(new_dir)->i_projid,
3686 			EXT4_I(old_dentry->d_inode)->i_projid)))
3687 		return -EXDEV;
3688 
3689 	retval = dquot_initialize(old.dir);
3690 	if (retval)
3691 		return retval;
3692 	retval = dquot_initialize(old.inode);
3693 	if (retval)
3694 		return retval;
3695 	retval = dquot_initialize(new.dir);
3696 	if (retval)
3697 		return retval;
3698 
3699 	/* Initialize quotas before so that eventual writes go
3700 	 * in separate transaction */
3701 	if (new.inode) {
3702 		retval = dquot_initialize(new.inode);
3703 		if (retval)
3704 			return retval;
3705 	}
3706 
3707 	old.bh = ext4_find_entry(old.dir, &old.dentry->d_name, &old.de,
3708 				 &old.inlined);
3709 	if (IS_ERR(old.bh))
3710 		return PTR_ERR(old.bh);
3711 
3712 	/*
3713 	 *  Check for inode number is _not_ due to possible IO errors.
3714 	 *  We might rmdir the source, keep it as pwd of some process
3715 	 *  and merrily kill the link to whatever was created under the
3716 	 *  same name. Goodbye sticky bit ;-<
3717 	 */
3718 	retval = -ENOENT;
3719 	if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
3720 		goto release_bh;
3721 
3722 	new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
3723 				 &new.de, &new.inlined);
3724 	if (IS_ERR(new.bh)) {
3725 		retval = PTR_ERR(new.bh);
3726 		new.bh = NULL;
3727 		goto release_bh;
3728 	}
3729 	if (new.bh) {
3730 		if (!new.inode) {
3731 			brelse(new.bh);
3732 			new.bh = NULL;
3733 		}
3734 	}
3735 	if (new.inode && !test_opt(new.dir->i_sb, NO_AUTO_DA_ALLOC))
3736 		ext4_alloc_da_blocks(old.inode);
3737 
3738 	credits = (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
3739 		   EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
3740 	if (!(flags & RENAME_WHITEOUT)) {
3741 		handle = ext4_journal_start(old.dir, EXT4_HT_DIR, credits);
3742 		if (IS_ERR(handle)) {
3743 			retval = PTR_ERR(handle);
3744 			goto release_bh;
3745 		}
3746 	} else {
3747 		whiteout = ext4_whiteout_for_rename(idmap, &old, credits, &handle);
3748 		if (IS_ERR(whiteout)) {
3749 			retval = PTR_ERR(whiteout);
3750 			goto release_bh;
3751 		}
3752 	}
3753 
3754 	old_file_type = old.de->file_type;
3755 	if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
3756 		ext4_handle_sync(handle);
3757 
3758 	if (S_ISDIR(old.inode->i_mode)) {
3759 		if (new.inode) {
3760 			retval = -ENOTEMPTY;
3761 			if (!ext4_empty_dir(new.inode))
3762 				goto end_rename;
3763 		} else {
3764 			retval = -EMLINK;
3765 			if (new.dir != old.dir && EXT4_DIR_LINK_MAX(new.dir))
3766 				goto end_rename;
3767 		}
3768 		retval = ext4_rename_dir_prepare(handle, &old, new.dir != old.dir);
3769 		if (retval)
3770 			goto end_rename;
3771 	}
3772 	/*
3773 	 * If we're renaming a file within an inline_data dir and adding or
3774 	 * setting the new dirent causes a conversion from inline_data to
3775 	 * extents/blockmap, we need to force the dirent delete code to
3776 	 * re-read the directory, or else we end up trying to delete a dirent
3777 	 * from what is now the extent tree root (or a block map).
3778 	 */
3779 	force_reread = (new.dir->i_ino == old.dir->i_ino &&
3780 			ext4_test_inode_flag(new.dir, EXT4_INODE_INLINE_DATA));
3781 
3782 	if (whiteout) {
3783 		/*
3784 		 * Do this before adding a new entry, so the old entry is sure
3785 		 * to be still pointing to the valid old entry.
3786 		 */
3787 		retval = ext4_setent(handle, &old, whiteout->i_ino,
3788 				     EXT4_FT_CHRDEV);
3789 		if (retval)
3790 			goto end_rename;
3791 		retval = ext4_mark_inode_dirty(handle, whiteout);
3792 		if (unlikely(retval))
3793 			goto end_rename;
3794 
3795 	}
3796 	if (!new.bh) {
3797 		retval = ext4_add_entry(handle, new.dentry, old.inode);
3798 		if (retval)
3799 			goto end_rename;
3800 	} else {
3801 		retval = ext4_setent(handle, &new,
3802 				     old.inode->i_ino, old_file_type);
3803 		if (retval)
3804 			goto end_rename;
3805 	}
3806 	if (force_reread)
3807 		force_reread = !ext4_test_inode_flag(new.dir,
3808 						     EXT4_INODE_INLINE_DATA);
3809 
3810 	/*
3811 	 * Like most other Unix systems, set the ctime for inodes on a
3812 	 * rename.
3813 	 */
3814 	inode_set_ctime_current(old.inode);
3815 	retval = ext4_mark_inode_dirty(handle, old.inode);
3816 	if (unlikely(retval))
3817 		goto end_rename;
3818 
3819 	if (!whiteout) {
3820 		/*
3821 		 * ok, that's it
3822 		 */
3823 		ext4_rename_delete(handle, &old, force_reread);
3824 	}
3825 
3826 	if (new.inode) {
3827 		ext4_dec_count(new.inode);
3828 		inode_set_ctime_current(new.inode);
3829 	}
3830 	inode_set_mtime_to_ts(old.dir, inode_set_ctime_current(old.dir));
3831 	ext4_update_dx_flag(old.dir);
3832 	if (old.is_dir) {
3833 		retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
3834 		if (retval)
3835 			goto end_rename;
3836 
3837 		ext4_dec_count(old.dir);
3838 		if (new.inode) {
3839 			/* checked ext4_empty_dir above, can't have another
3840 			 * parent, ext4_dec_count() won't work for many-linked
3841 			 * dirs */
3842 			clear_nlink(new.inode);
3843 		} else {
3844 			ext4_inc_count(new.dir);
3845 			ext4_update_dx_flag(new.dir);
3846 			retval = ext4_mark_inode_dirty(handle, new.dir);
3847 			if (unlikely(retval))
3848 				goto end_rename;
3849 		}
3850 	}
3851 	retval = ext4_mark_inode_dirty(handle, old.dir);
3852 	if (unlikely(retval))
3853 		goto end_rename;
3854 
3855 	if (old.is_dir) {
3856 		/*
3857 		 * We disable fast commits here that's because the
3858 		 * replay code is not yet capable of changing dot dot
3859 		 * dirents in directories.
3860 		 */
3861 		ext4_fc_mark_ineligible(old.inode->i_sb,
3862 			EXT4_FC_REASON_RENAME_DIR, handle);
3863 	} else {
3864 		struct super_block *sb = old.inode->i_sb;
3865 
3866 		if (new.inode)
3867 			ext4_fc_track_unlink(handle, new.dentry);
3868 		if (test_opt2(sb, JOURNAL_FAST_COMMIT) &&
3869 		    !(EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY) &&
3870 		    !(ext4_test_mount_flag(sb, EXT4_MF_FC_INELIGIBLE))) {
3871 			__ext4_fc_track_link(handle, old.inode, new.dentry);
3872 			__ext4_fc_track_unlink(handle, old.inode, old.dentry);
3873 			if (whiteout)
3874 				__ext4_fc_track_create(handle, whiteout,
3875 						       old.dentry);
3876 		}
3877 	}
3878 
3879 	if (new.inode) {
3880 		retval = ext4_mark_inode_dirty(handle, new.inode);
3881 		if (unlikely(retval))
3882 			goto end_rename;
3883 		if (!new.inode->i_nlink)
3884 			ext4_orphan_add(handle, new.inode);
3885 	}
3886 	retval = 0;
3887 
3888 end_rename:
3889 	if (whiteout) {
3890 		if (retval) {
3891 			ext4_resetent(handle, &old,
3892 				      old.inode->i_ino, old_file_type);
3893 			drop_nlink(whiteout);
3894 			ext4_mark_inode_dirty(handle, whiteout);
3895 			ext4_orphan_add(handle, whiteout);
3896 		}
3897 		unlock_new_inode(whiteout);
3898 		ext4_journal_stop(handle);
3899 		iput(whiteout);
3900 	} else {
3901 		ext4_journal_stop(handle);
3902 	}
3903 release_bh:
3904 	brelse(old.dir_bh);
3905 	brelse(old.bh);
3906 	brelse(new.bh);
3907 
3908 	return retval;
3909 }
3910 
3911 static int ext4_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
3912 			     struct inode *new_dir, struct dentry *new_dentry)
3913 {
3914 	handle_t *handle = NULL;
3915 	struct ext4_renament old = {
3916 		.dir = old_dir,
3917 		.dentry = old_dentry,
3918 		.inode = d_inode(old_dentry),
3919 	};
3920 	struct ext4_renament new = {
3921 		.dir = new_dir,
3922 		.dentry = new_dentry,
3923 		.inode = d_inode(new_dentry),
3924 	};
3925 	u8 new_file_type;
3926 	int retval;
3927 
3928 	if ((ext4_test_inode_flag(new_dir, EXT4_INODE_PROJINHERIT) &&
3929 	     !projid_eq(EXT4_I(new_dir)->i_projid,
3930 			EXT4_I(old_dentry->d_inode)->i_projid)) ||
3931 	    (ext4_test_inode_flag(old_dir, EXT4_INODE_PROJINHERIT) &&
3932 	     !projid_eq(EXT4_I(old_dir)->i_projid,
3933 			EXT4_I(new_dentry->d_inode)->i_projid)))
3934 		return -EXDEV;
3935 
3936 	retval = dquot_initialize(old.dir);
3937 	if (retval)
3938 		return retval;
3939 	retval = dquot_initialize(new.dir);
3940 	if (retval)
3941 		return retval;
3942 
3943 	old.bh = ext4_find_entry(old.dir, &old.dentry->d_name,
3944 				 &old.de, &old.inlined);
3945 	if (IS_ERR(old.bh))
3946 		return PTR_ERR(old.bh);
3947 	/*
3948 	 *  Check for inode number is _not_ due to possible IO errors.
3949 	 *  We might rmdir the source, keep it as pwd of some process
3950 	 *  and merrily kill the link to whatever was created under the
3951 	 *  same name. Goodbye sticky bit ;-<
3952 	 */
3953 	retval = -ENOENT;
3954 	if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
3955 		goto end_rename;
3956 
3957 	new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
3958 				 &new.de, &new.inlined);
3959 	if (IS_ERR(new.bh)) {
3960 		retval = PTR_ERR(new.bh);
3961 		new.bh = NULL;
3962 		goto end_rename;
3963 	}
3964 
3965 	/* RENAME_EXCHANGE case: old *and* new must both exist */
3966 	if (!new.bh || le32_to_cpu(new.de->inode) != new.inode->i_ino)
3967 		goto end_rename;
3968 
3969 	handle = ext4_journal_start(old.dir, EXT4_HT_DIR,
3970 		(2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
3971 		 2 * EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2));
3972 	if (IS_ERR(handle)) {
3973 		retval = PTR_ERR(handle);
3974 		handle = NULL;
3975 		goto end_rename;
3976 	}
3977 
3978 	if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
3979 		ext4_handle_sync(handle);
3980 
3981 	if (S_ISDIR(old.inode->i_mode)) {
3982 		retval = ext4_rename_dir_prepare(handle, &old, new.dir != old.dir);
3983 		if (retval)
3984 			goto end_rename;
3985 	}
3986 	if (S_ISDIR(new.inode->i_mode)) {
3987 		retval = ext4_rename_dir_prepare(handle, &new, new.dir != old.dir);
3988 		if (retval)
3989 			goto end_rename;
3990 	}
3991 
3992 	/*
3993 	 * Other than the special case of overwriting a directory, parents'
3994 	 * nlink only needs to be modified if this is a cross directory rename.
3995 	 */
3996 	if (old.dir != new.dir && old.is_dir != new.is_dir) {
3997 		old.dir_nlink_delta = old.is_dir ? -1 : 1;
3998 		new.dir_nlink_delta = -old.dir_nlink_delta;
3999 		retval = -EMLINK;
4000 		if ((old.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(old.dir)) ||
4001 		    (new.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(new.dir)))
4002 			goto end_rename;
4003 	}
4004 
4005 	new_file_type = new.de->file_type;
4006 	retval = ext4_setent(handle, &new, old.inode->i_ino, old.de->file_type);
4007 	if (retval)
4008 		goto end_rename;
4009 
4010 	retval = ext4_setent(handle, &old, new.inode->i_ino, new_file_type);
4011 	if (retval)
4012 		goto end_rename;
4013 
4014 	/*
4015 	 * Like most other Unix systems, set the ctime for inodes on a
4016 	 * rename.
4017 	 */
4018 	inode_set_ctime_current(old.inode);
4019 	inode_set_ctime_current(new.inode);
4020 	retval = ext4_mark_inode_dirty(handle, old.inode);
4021 	if (unlikely(retval))
4022 		goto end_rename;
4023 	retval = ext4_mark_inode_dirty(handle, new.inode);
4024 	if (unlikely(retval))
4025 		goto end_rename;
4026 	ext4_fc_mark_ineligible(new.inode->i_sb,
4027 				EXT4_FC_REASON_CROSS_RENAME, handle);
4028 	if (old.dir_bh) {
4029 		retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
4030 		if (retval)
4031 			goto end_rename;
4032 	}
4033 	if (new.dir_bh) {
4034 		retval = ext4_rename_dir_finish(handle, &new, old.dir->i_ino);
4035 		if (retval)
4036 			goto end_rename;
4037 	}
4038 	ext4_update_dir_count(handle, &old);
4039 	ext4_update_dir_count(handle, &new);
4040 	retval = 0;
4041 
4042 end_rename:
4043 	brelse(old.dir_bh);
4044 	brelse(new.dir_bh);
4045 	brelse(old.bh);
4046 	brelse(new.bh);
4047 	if (handle)
4048 		ext4_journal_stop(handle);
4049 	return retval;
4050 }
4051 
4052 static int ext4_rename2(const struct mnt_idmap *idmap,
4053 			struct inode *old_dir, struct dentry *old_dentry,
4054 			struct inode *new_dir, struct dentry *new_dentry,
4055 			unsigned int flags)
4056 {
4057 	int err;
4058 
4059 	err = ext4_emergency_state(old_dir->i_sb);
4060 	if (unlikely(err))
4061 		return err;
4062 
4063 	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
4064 		return -EINVAL;
4065 
4066 	err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
4067 				     flags);
4068 	if (err)
4069 		return err;
4070 
4071 	if (flags & RENAME_EXCHANGE) {
4072 		return ext4_cross_rename(old_dir, old_dentry,
4073 					 new_dir, new_dentry);
4074 	}
4075 
4076 	return ext4_rename(idmap, old_dir, old_dentry, new_dir, new_dentry, flags);
4077 }
4078 
4079 /*
4080  * directories can handle most operations...
4081  */
4082 const struct inode_operations ext4_dir_inode_operations = {
4083 	.create		= ext4_create,
4084 	.lookup		= ext4_lookup,
4085 	.link		= ext4_link,
4086 	.unlink		= ext4_unlink,
4087 	.symlink	= ext4_symlink,
4088 	.mkdir		= ext4_mkdir,
4089 	.rmdir		= ext4_rmdir,
4090 	.mknod		= ext4_mknod,
4091 	.tmpfile	= ext4_tmpfile,
4092 	.rename		= ext4_rename2,
4093 	.setattr	= ext4_setattr,
4094 	.getattr	= ext4_getattr,
4095 	.listxattr	= ext4_listxattr,
4096 	.get_inode_acl	= ext4_get_acl,
4097 	.set_acl	= ext4_set_acl,
4098 	.fiemap         = ext4_fiemap,
4099 	.fileattr_get	= ext4_fileattr_get,
4100 	.fileattr_set	= ext4_fileattr_set,
4101 };
4102 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_dir_inode_operations);
4103 
4104 const struct inode_operations ext4_special_inode_operations = {
4105 	.setattr	= ext4_setattr,
4106 	.getattr	= ext4_getattr,
4107 	.listxattr	= ext4_listxattr,
4108 	.get_inode_acl	= ext4_get_acl,
4109 	.set_acl	= ext4_set_acl,
4110 };
4111 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_special_inode_operations);
4112 
4113 #if IS_ENABLED(CONFIG_EXT4_KUNIT_TESTS)
4114 struct buffer_head *ext4_append_test(handle_t *handle, struct inode *inode,
4115 				     ext4_lblk_t *block)
4116 {
4117 	return ext4_append(handle, inode, block);
4118 }
4119 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_append_test);
4120 
4121 struct buffer_head *ext4_read_dirblock_test(struct inode *inode,
4122 					    ext4_lblk_t block,
4123 					    enum dirblock_type type)
4124 {
4125 	return ext4_read_dirblock(inode, block, type);
4126 }
4127 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_read_dirblock_test);
4128 
4129 void ext4_dx_csum_set_test(struct inode *inode, struct ext4_dir_entry *dirent)
4130 {
4131 	ext4_dx_csum_set(inode, dirent);
4132 }
4133 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_dx_csum_set_test);
4134 
4135 int ext4_dx_csum_verify_test(struct inode *inode,
4136 			     struct ext4_dir_entry *dirent)
4137 {
4138 	return ext4_dx_csum_verify(inode, dirent);
4139 }
4140 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_dx_csum_verify_test);
4141 
4142 struct dx_frame *dx_probe_test(struct ext4_filename *fname, struct inode *dir,
4143 			       struct dx_hash_info *hinfo,
4144 			       struct dx_frame *frame_in)
4145 {
4146 	return dx_probe(fname, dir, hinfo, frame_in);
4147 }
4148 EXPORT_SYMBOL_FOR_EXT4_TEST(dx_probe_test);
4149 
4150 void dx_release_test(struct dx_frame *frames)
4151 {
4152 	dx_release(frames);
4153 }
4154 EXPORT_SYMBOL_FOR_EXT4_TEST(dx_release_test);
4155 
4156 int ext4_htree_next_block_test(struct inode *dir, __u32 hash,
4157 			       struct dx_frame *frame,
4158 			       struct dx_frame *frames, __u32 *start_hash)
4159 {
4160 	return ext4_htree_next_block(dir, hash, frame, frames, start_hash);
4161 }
4162 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_htree_next_block_test);
4163 
4164 int dx_make_map_test(struct inode *dir, struct buffer_head *bh,
4165 		     struct dx_hash_info *hinfo,
4166 		     struct dx_map_entry *map_tail)
4167 {
4168 	return dx_make_map(dir, bh, hinfo, map_tail);
4169 }
4170 EXPORT_SYMBOL_FOR_EXT4_TEST(dx_make_map_test);
4171 
4172 void dx_sort_map_test(struct dx_map_entry *map, unsigned int count)
4173 {
4174 	dx_sort_map(map, count);
4175 }
4176 EXPORT_SYMBOL_FOR_EXT4_TEST(dx_sort_map_test);
4177 
4178 void dx_insert_block_test(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
4179 {
4180 	dx_insert_block(frame, hash, block);
4181 }
4182 EXPORT_SYMBOL_FOR_EXT4_TEST(dx_insert_block_test);
4183 
4184 struct ext4_dir_entry_2 *dx_move_dirents_test(struct inode *dir, char *from,
4185 					      char *to,
4186 					      struct dx_map_entry *map,
4187 					      int count,
4188 					      unsigned int blocksize)
4189 {
4190 	return dx_move_dirents(dir, from, to, map, count, blocksize);
4191 }
4192 EXPORT_SYMBOL_FOR_EXT4_TEST(dx_move_dirents_test);
4193 
4194 struct ext4_dir_entry_2 *dx_pack_dirents_test(struct inode *dir, char *base,
4195 					      unsigned int blocksize)
4196 {
4197 	return dx_pack_dirents(dir, base, blocksize);
4198 }
4199 EXPORT_SYMBOL_FOR_EXT4_TEST(dx_pack_dirents_test);
4200 
4201 struct ext4_dir_entry_2 *do_split_test(handle_t *handle, struct inode *dir,
4202 				       struct buffer_head **bh,
4203 				       struct dx_frame *frame,
4204 				       struct dx_hash_info *hinfo)
4205 {
4206 	return do_split(handle, dir, bh, frame, hinfo);
4207 }
4208 EXPORT_SYMBOL_FOR_EXT4_TEST(do_split_test);
4209 
4210 bool ext4_check_dx_root_test(struct inode *dir, struct dx_root *root)
4211 {
4212 	return ext4_check_dx_root(dir, root);
4213 }
4214 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_check_dx_root_test);
4215 
4216 struct buffer_head *ext4_find_entry_test(struct inode *dir,
4217 					 const struct qstr *d_name,
4218 					 struct ext4_dir_entry_2 **res_dir,
4219 					 int *inlined)
4220 {
4221 	return ext4_find_entry(dir, d_name, res_dir, inlined);
4222 }
4223 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_find_entry_test);
4224 
4225 struct buffer_head *ext4_lookup_entry_test(struct inode *dir,
4226 					   struct dentry *dentry,
4227 					   struct ext4_dir_entry_2 **res_dir)
4228 {
4229 	return ext4_lookup_entry(dir, dentry, res_dir);
4230 }
4231 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_lookup_entry_test);
4232 
4233 int ext4_add_entry_test(handle_t *handle, struct dentry *dentry,
4234 			struct inode *inode)
4235 {
4236 	return ext4_add_entry(handle, dentry, inode);
4237 }
4238 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_add_entry_test);
4239 
4240 int ext4_delete_entry_test(handle_t *handle, struct inode *dir,
4241 			   struct ext4_dir_entry_2 *de_del,
4242 			   struct buffer_head *bh)
4243 {
4244 	return ext4_delete_entry(handle, dir, de_del, bh);
4245 }
4246 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_delete_entry_test);
4247 
4248 void ext4_inc_count_test(struct inode *inode)
4249 {
4250 	ext4_inc_count(inode);
4251 }
4252 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_inc_count_test);
4253 
4254 void ext4_dec_count_test(struct inode *inode)
4255 {
4256 	ext4_dec_count(inode);
4257 }
4258 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_dec_count_test);
4259 
4260 int ext4_init_symlink_block_test(handle_t *handle, struct inode *inode,
4261 				 struct fscrypt_str *disk_link)
4262 {
4263 	return ext4_init_symlink_block(handle, inode, disk_link);
4264 }
4265 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_init_symlink_block_test);
4266 #endif
4267