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