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