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