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