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