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