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