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