88e9a2c7e328e74d3518c2e9b698353f46080f3b
[firefly-linux-kernel-4.4.55.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/jbd2.h>
30 #include <linux/time.h>
31 #include <linux/fcntl.h>
32 #include <linux/stat.h>
33 #include <linux/string.h>
34 #include <linux/quotaops.h>
35 #include <linux/buffer_head.h>
36 #include <linux/bio.h>
37 #include "ext4.h"
38 #include "ext4_jbd2.h"
39
40 #include "xattr.h"
41 #include "acl.h"
42
43 #include <trace/events/ext4.h>
44 /*
45  * define how far ahead to read directories while searching them.
46  */
47 #define NAMEI_RA_CHUNKS  2
48 #define NAMEI_RA_BLOCKS  4
49 #define NAMEI_RA_SIZE        (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
50 #define NAMEI_RA_INDEX(c,b)  (((c) * NAMEI_RA_BLOCKS) + (b))
51
52 static struct buffer_head *ext4_append(handle_t *handle,
53                                         struct inode *inode,
54                                         ext4_lblk_t *block, int *err)
55 {
56         struct buffer_head *bh;
57
58         if (unlikely(EXT4_SB(inode->i_sb)->s_max_dir_size_kb &&
59                      ((inode->i_size >> 10) >=
60                       EXT4_SB(inode->i_sb)->s_max_dir_size_kb))) {
61                 *err = -ENOSPC;
62                 return NULL;
63         }
64
65         *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
66
67         bh = ext4_bread(handle, inode, *block, 1, err);
68         if (bh) {
69                 inode->i_size += inode->i_sb->s_blocksize;
70                 EXT4_I(inode)->i_disksize = inode->i_size;
71                 *err = ext4_journal_get_write_access(handle, bh);
72                 if (*err) {
73                         brelse(bh);
74                         bh = NULL;
75                 }
76         }
77         if (!bh && !(*err)) {
78                 *err = -EIO;
79                 ext4_error(inode->i_sb,
80                            "Directory hole detected on inode %lu\n",
81                            inode->i_ino);
82         }
83         return bh;
84 }
85
86 #ifndef assert
87 #define assert(test) J_ASSERT(test)
88 #endif
89
90 #ifdef DX_DEBUG
91 #define dxtrace(command) command
92 #else
93 #define dxtrace(command)
94 #endif
95
96 struct fake_dirent
97 {
98         __le32 inode;
99         __le16 rec_len;
100         u8 name_len;
101         u8 file_type;
102 };
103
104 struct dx_countlimit
105 {
106         __le16 limit;
107         __le16 count;
108 };
109
110 struct dx_entry
111 {
112         __le32 hash;
113         __le32 block;
114 };
115
116 /*
117  * dx_root_info is laid out so that if it should somehow get overlaid by a
118  * dirent the two low bits of the hash version will be zero.  Therefore, the
119  * hash version mod 4 should never be 0.  Sincerely, the paranoia department.
120  */
121
122 struct dx_root
123 {
124         struct fake_dirent dot;
125         char dot_name[4];
126         struct fake_dirent dotdot;
127         char dotdot_name[4];
128         struct dx_root_info
129         {
130                 __le32 reserved_zero;
131                 u8 hash_version;
132                 u8 info_length; /* 8 */
133                 u8 indirect_levels;
134                 u8 unused_flags;
135         }
136         info;
137         struct dx_entry entries[0];
138 };
139
140 struct dx_node
141 {
142         struct fake_dirent fake;
143         struct dx_entry entries[0];
144 };
145
146
147 struct dx_frame
148 {
149         struct buffer_head *bh;
150         struct dx_entry *entries;
151         struct dx_entry *at;
152 };
153
154 struct dx_map_entry
155 {
156         u32 hash;
157         u16 offs;
158         u16 size;
159 };
160
161 /*
162  * This goes at the end of each htree block.
163  */
164 struct dx_tail {
165         u32 dt_reserved;
166         __le32 dt_checksum;     /* crc32c(uuid+inum+dirblock) */
167 };
168
169 static inline ext4_lblk_t dx_get_block(struct dx_entry *entry);
170 static void dx_set_block(struct dx_entry *entry, ext4_lblk_t value);
171 static inline unsigned dx_get_hash(struct dx_entry *entry);
172 static void dx_set_hash(struct dx_entry *entry, unsigned value);
173 static unsigned dx_get_count(struct dx_entry *entries);
174 static unsigned dx_get_limit(struct dx_entry *entries);
175 static void dx_set_count(struct dx_entry *entries, unsigned value);
176 static void dx_set_limit(struct dx_entry *entries, unsigned value);
177 static unsigned dx_root_limit(struct inode *dir, unsigned infosize);
178 static unsigned dx_node_limit(struct inode *dir);
179 static struct dx_frame *dx_probe(const struct qstr *d_name,
180                                  struct inode *dir,
181                                  struct dx_hash_info *hinfo,
182                                  struct dx_frame *frame,
183                                  int *err);
184 static void dx_release(struct dx_frame *frames);
185 static int dx_make_map(struct ext4_dir_entry_2 *de, unsigned blocksize,
186                        struct dx_hash_info *hinfo, struct dx_map_entry map[]);
187 static void dx_sort_map(struct dx_map_entry *map, unsigned count);
188 static struct ext4_dir_entry_2 *dx_move_dirents(char *from, char *to,
189                 struct dx_map_entry *offsets, int count, unsigned blocksize);
190 static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize);
191 static void dx_insert_block(struct dx_frame *frame,
192                                         u32 hash, ext4_lblk_t block);
193 static int ext4_htree_next_block(struct inode *dir, __u32 hash,
194                                  struct dx_frame *frame,
195                                  struct dx_frame *frames,
196                                  __u32 *start_hash);
197 static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
198                 const struct qstr *d_name,
199                 struct ext4_dir_entry_2 **res_dir,
200                 int *err);
201 static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
202                              struct inode *inode);
203
204 /* checksumming functions */
205 #define EXT4_DIRENT_TAIL(block, blocksize) \
206         ((struct ext4_dir_entry_tail *)(((void *)(block)) + \
207                                         ((blocksize) - \
208                                          sizeof(struct ext4_dir_entry_tail))))
209
210 static void initialize_dirent_tail(struct ext4_dir_entry_tail *t,
211                                    unsigned int blocksize)
212 {
213         memset(t, 0, sizeof(struct ext4_dir_entry_tail));
214         t->det_rec_len = ext4_rec_len_to_disk(
215                         sizeof(struct ext4_dir_entry_tail), blocksize);
216         t->det_reserved_ft = EXT4_FT_DIR_CSUM;
217 }
218
219 /* Walk through a dirent block to find a checksum "dirent" at the tail */
220 static struct ext4_dir_entry_tail *get_dirent_tail(struct inode *inode,
221                                                    struct ext4_dir_entry *de)
222 {
223         struct ext4_dir_entry_tail *t;
224
225 #ifdef PARANOID
226         struct ext4_dir_entry *d, *top;
227
228         d = de;
229         top = (struct ext4_dir_entry *)(((void *)de) +
230                 (EXT4_BLOCK_SIZE(inode->i_sb) -
231                 sizeof(struct ext4_dir_entry_tail)));
232         while (d < top && d->rec_len)
233                 d = (struct ext4_dir_entry *)(((void *)d) +
234                     le16_to_cpu(d->rec_len));
235
236         if (d != top)
237                 return NULL;
238
239         t = (struct ext4_dir_entry_tail *)d;
240 #else
241         t = EXT4_DIRENT_TAIL(de, EXT4_BLOCK_SIZE(inode->i_sb));
242 #endif
243
244         if (t->det_reserved_zero1 ||
245             le16_to_cpu(t->det_rec_len) != sizeof(struct ext4_dir_entry_tail) ||
246             t->det_reserved_zero2 ||
247             t->det_reserved_ft != EXT4_FT_DIR_CSUM)
248                 return NULL;
249
250         return t;
251 }
252
253 static __le32 ext4_dirent_csum(struct inode *inode,
254                                struct ext4_dir_entry *dirent, int size)
255 {
256         struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
257         struct ext4_inode_info *ei = EXT4_I(inode);
258         __u32 csum;
259
260         csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
261         return cpu_to_le32(csum);
262 }
263
264 static void warn_no_space_for_csum(struct inode *inode)
265 {
266         ext4_warning(inode->i_sb, "no space in directory inode %lu leaf for "
267                      "checksum.  Please run e2fsck -D.", inode->i_ino);
268 }
269
270 int ext4_dirent_csum_verify(struct inode *inode, struct ext4_dir_entry *dirent)
271 {
272         struct ext4_dir_entry_tail *t;
273
274         if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
275                                         EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
276                 return 1;
277
278         t = get_dirent_tail(inode, dirent);
279         if (!t) {
280                 warn_no_space_for_csum(inode);
281                 return 0;
282         }
283
284         if (t->det_checksum != ext4_dirent_csum(inode, dirent,
285                                                 (void *)t - (void *)dirent))
286                 return 0;
287
288         return 1;
289 }
290
291 static void ext4_dirent_csum_set(struct inode *inode,
292                                  struct ext4_dir_entry *dirent)
293 {
294         struct ext4_dir_entry_tail *t;
295
296         if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
297                                         EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
298                 return;
299
300         t = get_dirent_tail(inode, dirent);
301         if (!t) {
302                 warn_no_space_for_csum(inode);
303                 return;
304         }
305
306         t->det_checksum = ext4_dirent_csum(inode, dirent,
307                                            (void *)t - (void *)dirent);
308 }
309
310 static inline int ext4_handle_dirty_dirent_node(handle_t *handle,
311                                                 struct inode *inode,
312                                                 struct buffer_head *bh)
313 {
314         ext4_dirent_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
315         return ext4_handle_dirty_metadata(handle, inode, bh);
316 }
317
318 static struct dx_countlimit *get_dx_countlimit(struct inode *inode,
319                                                struct ext4_dir_entry *dirent,
320                                                int *offset)
321 {
322         struct ext4_dir_entry *dp;
323         struct dx_root_info *root;
324         int count_offset;
325
326         if (le16_to_cpu(dirent->rec_len) == EXT4_BLOCK_SIZE(inode->i_sb))
327                 count_offset = 8;
328         else if (le16_to_cpu(dirent->rec_len) == 12) {
329                 dp = (struct ext4_dir_entry *)(((void *)dirent) + 12);
330                 if (le16_to_cpu(dp->rec_len) !=
331                     EXT4_BLOCK_SIZE(inode->i_sb) - 12)
332                         return NULL;
333                 root = (struct dx_root_info *)(((void *)dp + 12));
334                 if (root->reserved_zero ||
335                     root->info_length != sizeof(struct dx_root_info))
336                         return NULL;
337                 count_offset = 32;
338         } else
339                 return NULL;
340
341         if (offset)
342                 *offset = count_offset;
343         return (struct dx_countlimit *)(((void *)dirent) + count_offset);
344 }
345
346 static __le32 ext4_dx_csum(struct inode *inode, struct ext4_dir_entry *dirent,
347                            int count_offset, int count, struct dx_tail *t)
348 {
349         struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
350         struct ext4_inode_info *ei = EXT4_I(inode);
351         __u32 csum, old_csum;
352         int size;
353
354         size = count_offset + (count * sizeof(struct dx_entry));
355         old_csum = t->dt_checksum;
356         t->dt_checksum = 0;
357         csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
358         csum = ext4_chksum(sbi, csum, (__u8 *)t, sizeof(struct dx_tail));
359         t->dt_checksum = old_csum;
360
361         return cpu_to_le32(csum);
362 }
363
364 static int ext4_dx_csum_verify(struct inode *inode,
365                                struct ext4_dir_entry *dirent)
366 {
367         struct dx_countlimit *c;
368         struct dx_tail *t;
369         int count_offset, limit, count;
370
371         if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
372                                         EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
373                 return 1;
374
375         c = get_dx_countlimit(inode, dirent, &count_offset);
376         if (!c) {
377                 EXT4_ERROR_INODE(inode, "dir seems corrupt?  Run e2fsck -D.");
378                 return 1;
379         }
380         limit = le16_to_cpu(c->limit);
381         count = le16_to_cpu(c->count);
382         if (count_offset + (limit * sizeof(struct dx_entry)) >
383             EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
384                 warn_no_space_for_csum(inode);
385                 return 1;
386         }
387         t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
388
389         if (t->dt_checksum != ext4_dx_csum(inode, dirent, count_offset,
390                                             count, t))
391                 return 0;
392         return 1;
393 }
394
395 static void ext4_dx_csum_set(struct inode *inode, struct ext4_dir_entry *dirent)
396 {
397         struct dx_countlimit *c;
398         struct dx_tail *t;
399         int count_offset, limit, count;
400
401         if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
402                                         EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
403                 return;
404
405         c = get_dx_countlimit(inode, dirent, &count_offset);
406         if (!c) {
407                 EXT4_ERROR_INODE(inode, "dir seems corrupt?  Run e2fsck -D.");
408                 return;
409         }
410         limit = le16_to_cpu(c->limit);
411         count = le16_to_cpu(c->count);
412         if (count_offset + (limit * sizeof(struct dx_entry)) >
413             EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
414                 warn_no_space_for_csum(inode);
415                 return;
416         }
417         t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
418
419         t->dt_checksum = ext4_dx_csum(inode, dirent, count_offset, count, t);
420 }
421
422 static inline int ext4_handle_dirty_dx_node(handle_t *handle,
423                                             struct inode *inode,
424                                             struct buffer_head *bh)
425 {
426         ext4_dx_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
427         return ext4_handle_dirty_metadata(handle, inode, bh);
428 }
429
430 /*
431  * p is at least 6 bytes before the end of page
432  */
433 static inline struct ext4_dir_entry_2 *
434 ext4_next_entry(struct ext4_dir_entry_2 *p, unsigned long blocksize)
435 {
436         return (struct ext4_dir_entry_2 *)((char *)p +
437                 ext4_rec_len_from_disk(p->rec_len, blocksize));
438 }
439
440 /*
441  * Future: use high four bits of block for coalesce-on-delete flags
442  * Mask them off for now.
443  */
444
445 static inline ext4_lblk_t dx_get_block(struct dx_entry *entry)
446 {
447         return le32_to_cpu(entry->block) & 0x00ffffff;
448 }
449
450 static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value)
451 {
452         entry->block = cpu_to_le32(value);
453 }
454
455 static inline unsigned dx_get_hash(struct dx_entry *entry)
456 {
457         return le32_to_cpu(entry->hash);
458 }
459
460 static inline void dx_set_hash(struct dx_entry *entry, unsigned value)
461 {
462         entry->hash = cpu_to_le32(value);
463 }
464
465 static inline unsigned dx_get_count(struct dx_entry *entries)
466 {
467         return le16_to_cpu(((struct dx_countlimit *) entries)->count);
468 }
469
470 static inline unsigned dx_get_limit(struct dx_entry *entries)
471 {
472         return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
473 }
474
475 static inline void dx_set_count(struct dx_entry *entries, unsigned value)
476 {
477         ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
478 }
479
480 static inline void dx_set_limit(struct dx_entry *entries, unsigned value)
481 {
482         ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
483 }
484
485 static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize)
486 {
487         unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(1) -
488                 EXT4_DIR_REC_LEN(2) - infosize;
489
490         if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
491                                        EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
492                 entry_space -= sizeof(struct dx_tail);
493         return entry_space / sizeof(struct dx_entry);
494 }
495
496 static inline unsigned dx_node_limit(struct inode *dir)
497 {
498         unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0);
499
500         if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
501                                        EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
502                 entry_space -= sizeof(struct dx_tail);
503         return entry_space / sizeof(struct dx_entry);
504 }
505
506 /*
507  * Debug
508  */
509 #ifdef DX_DEBUG
510 static void dx_show_index(char * label, struct dx_entry *entries)
511 {
512         int i, n = dx_get_count (entries);
513         printk(KERN_DEBUG "%s index ", label);
514         for (i = 0; i < n; i++) {
515                 printk("%x->%lu ", i ? dx_get_hash(entries + i) :
516                                 0, (unsigned long)dx_get_block(entries + i));
517         }
518         printk("\n");
519 }
520
521 struct stats
522 {
523         unsigned names;
524         unsigned space;
525         unsigned bcount;
526 };
527
528 static struct stats dx_show_leaf(struct dx_hash_info *hinfo, struct ext4_dir_entry_2 *de,
529                                  int size, int show_names)
530 {
531         unsigned names = 0, space = 0;
532         char *base = (char *) de;
533         struct dx_hash_info h = *hinfo;
534
535         printk("names: ");
536         while ((char *) de < base + size)
537         {
538                 if (de->inode)
539                 {
540                         if (show_names)
541                         {
542                                 int len = de->name_len;
543                                 char *name = de->name;
544                                 while (len--) printk("%c", *name++);
545                                 ext4fs_dirhash(de->name, de->name_len, &h);
546                                 printk(":%x.%u ", h.hash,
547                                        (unsigned) ((char *) de - base));
548                         }
549                         space += EXT4_DIR_REC_LEN(de->name_len);
550                         names++;
551                 }
552                 de = ext4_next_entry(de, size);
553         }
554         printk("(%i)\n", names);
555         return (struct stats) { names, space, 1 };
556 }
557
558 struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
559                              struct dx_entry *entries, int levels)
560 {
561         unsigned blocksize = dir->i_sb->s_blocksize;
562         unsigned count = dx_get_count(entries), names = 0, space = 0, i;
563         unsigned bcount = 0;
564         struct buffer_head *bh;
565         int err;
566         printk("%i indexed blocks...\n", count);
567         for (i = 0; i < count; i++, entries++)
568         {
569                 ext4_lblk_t block = dx_get_block(entries);
570                 ext4_lblk_t hash  = i ? dx_get_hash(entries): 0;
571                 u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
572                 struct stats stats;
573                 printk("%s%3u:%03u hash %8x/%8x ",levels?"":"   ", i, block, hash, range);
574                 if (!(bh = ext4_bread (NULL,dir, block, 0,&err))) continue;
575                 stats = levels?
576                    dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
577                    dx_show_leaf(hinfo, (struct ext4_dir_entry_2 *) bh->b_data, blocksize, 0);
578                 names += stats.names;
579                 space += stats.space;
580                 bcount += stats.bcount;
581                 brelse(bh);
582         }
583         if (bcount)
584                 printk(KERN_DEBUG "%snames %u, fullness %u (%u%%)\n",
585                        levels ? "" : "   ", names, space/bcount,
586                        (space/bcount)*100/blocksize);
587         return (struct stats) { names, space, bcount};
588 }
589 #endif /* DX_DEBUG */
590
591 /*
592  * Probe for a directory leaf block to search.
593  *
594  * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
595  * error in the directory index, and the caller should fall back to
596  * searching the directory normally.  The callers of dx_probe **MUST**
597  * check for this error code, and make sure it never gets reflected
598  * back to userspace.
599  */
600 static struct dx_frame *
601 dx_probe(const struct qstr *d_name, struct inode *dir,
602          struct dx_hash_info *hinfo, struct dx_frame *frame_in, int *err)
603 {
604         unsigned count, indirect;
605         struct dx_entry *at, *entries, *p, *q, *m;
606         struct dx_root *root;
607         struct buffer_head *bh;
608         struct dx_frame *frame = frame_in;
609         u32 hash;
610
611         frame->bh = NULL;
612         if (!(bh = ext4_bread(NULL, dir, 0, 0, err))) {
613                 if (*err == 0)
614                         *err = ERR_BAD_DX_DIR;
615                 goto fail;
616         }
617         root = (struct dx_root *) bh->b_data;
618         if (root->info.hash_version != DX_HASH_TEA &&
619             root->info.hash_version != DX_HASH_HALF_MD4 &&
620             root->info.hash_version != DX_HASH_LEGACY) {
621                 ext4_warning(dir->i_sb, "Unrecognised inode hash code %d",
622                              root->info.hash_version);
623                 brelse(bh);
624                 *err = ERR_BAD_DX_DIR;
625                 goto fail;
626         }
627         hinfo->hash_version = root->info.hash_version;
628         if (hinfo->hash_version <= DX_HASH_TEA)
629                 hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
630         hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
631         if (d_name)
632                 ext4fs_dirhash(d_name->name, d_name->len, hinfo);
633         hash = hinfo->hash;
634
635         if (root->info.unused_flags & 1) {
636                 ext4_warning(dir->i_sb, "Unimplemented inode hash flags: %#06x",
637                              root->info.unused_flags);
638                 brelse(bh);
639                 *err = ERR_BAD_DX_DIR;
640                 goto fail;
641         }
642
643         if ((indirect = root->info.indirect_levels) > 1) {
644                 ext4_warning(dir->i_sb, "Unimplemented inode hash depth: %#06x",
645                              root->info.indirect_levels);
646                 brelse(bh);
647                 *err = ERR_BAD_DX_DIR;
648                 goto fail;
649         }
650
651         if (!buffer_verified(bh) &&
652             !ext4_dx_csum_verify(dir, (struct ext4_dir_entry *)bh->b_data)) {
653                 ext4_warning(dir->i_sb, "Root failed checksum");
654                 brelse(bh);
655                 *err = ERR_BAD_DX_DIR;
656                 goto fail;
657         }
658         set_buffer_verified(bh);
659
660         entries = (struct dx_entry *) (((char *)&root->info) +
661                                        root->info.info_length);
662
663         if (dx_get_limit(entries) != dx_root_limit(dir,
664                                                    root->info.info_length)) {
665                 ext4_warning(dir->i_sb, "dx entry: limit != root limit");
666                 brelse(bh);
667                 *err = ERR_BAD_DX_DIR;
668                 goto fail;
669         }
670
671         dxtrace(printk("Look up %x", hash));
672         while (1)
673         {
674                 count = dx_get_count(entries);
675                 if (!count || count > dx_get_limit(entries)) {
676                         ext4_warning(dir->i_sb,
677                                      "dx entry: no count or count > limit");
678                         brelse(bh);
679                         *err = ERR_BAD_DX_DIR;
680                         goto fail2;
681                 }
682
683                 p = entries + 1;
684                 q = entries + count - 1;
685                 while (p <= q)
686                 {
687                         m = p + (q - p)/2;
688                         dxtrace(printk("."));
689                         if (dx_get_hash(m) > hash)
690                                 q = m - 1;
691                         else
692                                 p = m + 1;
693                 }
694
695                 if (0) // linear search cross check
696                 {
697                         unsigned n = count - 1;
698                         at = entries;
699                         while (n--)
700                         {
701                                 dxtrace(printk(","));
702                                 if (dx_get_hash(++at) > hash)
703                                 {
704                                         at--;
705                                         break;
706                                 }
707                         }
708                         assert (at == p - 1);
709                 }
710
711                 at = p - 1;
712                 dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
713                 frame->bh = bh;
714                 frame->entries = entries;
715                 frame->at = at;
716                 if (!indirect--) return frame;
717                 if (!(bh = ext4_bread(NULL, dir, dx_get_block(at), 0, err))) {
718                         if (!(*err))
719                                 *err = ERR_BAD_DX_DIR;
720                         goto fail2;
721                 }
722                 at = entries = ((struct dx_node *) bh->b_data)->entries;
723
724                 if (!buffer_verified(bh) &&
725                     !ext4_dx_csum_verify(dir,
726                                          (struct ext4_dir_entry *)bh->b_data)) {
727                         ext4_warning(dir->i_sb, "Node failed checksum");
728                         brelse(bh);
729                         *err = ERR_BAD_DX_DIR;
730                         goto fail;
731                 }
732                 set_buffer_verified(bh);
733
734                 if (dx_get_limit(entries) != dx_node_limit (dir)) {
735                         ext4_warning(dir->i_sb,
736                                      "dx entry: limit != node limit");
737                         brelse(bh);
738                         *err = ERR_BAD_DX_DIR;
739                         goto fail2;
740                 }
741                 frame++;
742                 frame->bh = NULL;
743         }
744 fail2:
745         while (frame >= frame_in) {
746                 brelse(frame->bh);
747                 frame--;
748         }
749 fail:
750         if (*err == ERR_BAD_DX_DIR)
751                 ext4_warning(dir->i_sb,
752                              "Corrupt dir inode %lu, running e2fsck is "
753                              "recommended.", dir->i_ino);
754         return NULL;
755 }
756
757 static void dx_release (struct dx_frame *frames)
758 {
759         if (frames[0].bh == NULL)
760                 return;
761
762         if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
763                 brelse(frames[1].bh);
764         brelse(frames[0].bh);
765 }
766
767 /*
768  * This function increments the frame pointer to search the next leaf
769  * block, and reads in the necessary intervening nodes if the search
770  * should be necessary.  Whether or not the search is necessary is
771  * controlled by the hash parameter.  If the hash value is even, then
772  * the search is only continued if the next block starts with that
773  * hash value.  This is used if we are searching for a specific file.
774  *
775  * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
776  *
777  * This function returns 1 if the caller should continue to search,
778  * or 0 if it should not.  If there is an error reading one of the
779  * index blocks, it will a negative error code.
780  *
781  * If start_hash is non-null, it will be filled in with the starting
782  * hash of the next page.
783  */
784 static int ext4_htree_next_block(struct inode *dir, __u32 hash,
785                                  struct dx_frame *frame,
786                                  struct dx_frame *frames,
787                                  __u32 *start_hash)
788 {
789         struct dx_frame *p;
790         struct buffer_head *bh;
791         int err, num_frames = 0;
792         __u32 bhash;
793
794         p = frame;
795         /*
796          * Find the next leaf page by incrementing the frame pointer.
797          * If we run out of entries in the interior node, loop around and
798          * increment pointer in the parent node.  When we break out of
799          * this loop, num_frames indicates the number of interior
800          * nodes need to be read.
801          */
802         while (1) {
803                 if (++(p->at) < p->entries + dx_get_count(p->entries))
804                         break;
805                 if (p == frames)
806                         return 0;
807                 num_frames++;
808                 p--;
809         }
810
811         /*
812          * If the hash is 1, then continue only if the next page has a
813          * continuation hash of any value.  This is used for readdir
814          * handling.  Otherwise, check to see if the hash matches the
815          * desired contiuation hash.  If it doesn't, return since
816          * there's no point to read in the successive index pages.
817          */
818         bhash = dx_get_hash(p->at);
819         if (start_hash)
820                 *start_hash = bhash;
821         if ((hash & 1) == 0) {
822                 if ((bhash & ~1) != hash)
823                         return 0;
824         }
825         /*
826          * If the hash is HASH_NB_ALWAYS, we always go to the next
827          * block so no check is necessary
828          */
829         while (num_frames--) {
830                 if (!(bh = ext4_bread(NULL, dir, dx_get_block(p->at),
831                                       0, &err))) {
832                         if (!err) {
833                                 ext4_error(dir->i_sb,
834                                            "Directory hole detected on inode %lu\n",
835                                            dir->i_ino);
836                                 return -EIO;
837                         }
838                         return err; /* Failure */
839                 }
840
841                 if (!buffer_verified(bh) &&
842                     !ext4_dx_csum_verify(dir,
843                                          (struct ext4_dir_entry *)bh->b_data)) {
844                         ext4_warning(dir->i_sb, "Node failed checksum");
845                         return -EIO;
846                 }
847                 set_buffer_verified(bh);
848
849                 p++;
850                 brelse(p->bh);
851                 p->bh = bh;
852                 p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
853         }
854         return 1;
855 }
856
857
858 /*
859  * This function fills a red-black tree with information from a
860  * directory block.  It returns the number directory entries loaded
861  * into the tree.  If there is an error it is returned in err.
862  */
863 static int htree_dirblock_to_tree(struct file *dir_file,
864                                   struct inode *dir, ext4_lblk_t block,
865                                   struct dx_hash_info *hinfo,
866                                   __u32 start_hash, __u32 start_minor_hash)
867 {
868         struct buffer_head *bh;
869         struct ext4_dir_entry_2 *de, *top;
870         int err = 0, count = 0;
871
872         dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
873                                                         (unsigned long)block));
874         if (!(bh = ext4_bread(NULL, dir, block, 0, &err))) {
875                 if (!err) {
876                         err = -EIO;
877                         ext4_error(dir->i_sb,
878                                    "Directory hole detected on inode %lu\n",
879                                    dir->i_ino);
880                 }
881                 return err;
882         }
883
884         if (!buffer_verified(bh) &&
885             !ext4_dirent_csum_verify(dir, (struct ext4_dir_entry *)bh->b_data))
886                 return -EIO;
887         set_buffer_verified(bh);
888
889         de = (struct ext4_dir_entry_2 *) bh->b_data;
890         top = (struct ext4_dir_entry_2 *) ((char *) de +
891                                            dir->i_sb->s_blocksize -
892                                            EXT4_DIR_REC_LEN(0));
893         for (; de < top; de = ext4_next_entry(de, dir->i_sb->s_blocksize)) {
894                 if (ext4_check_dir_entry(dir, NULL, de, bh,
895                                 (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
896                                          + ((char *)de - bh->b_data))) {
897                         /* On error, skip the f_pos to the next block. */
898                         dir_file->f_pos = (dir_file->f_pos |
899                                         (dir->i_sb->s_blocksize - 1)) + 1;
900                         brelse(bh);
901                         return count;
902                 }
903                 ext4fs_dirhash(de->name, de->name_len, hinfo);
904                 if ((hinfo->hash < start_hash) ||
905                     ((hinfo->hash == start_hash) &&
906                      (hinfo->minor_hash < start_minor_hash)))
907                         continue;
908                 if (de->inode == 0)
909                         continue;
910                 if ((err = ext4_htree_store_dirent(dir_file,
911                                    hinfo->hash, hinfo->minor_hash, de)) != 0) {
912                         brelse(bh);
913                         return err;
914                 }
915                 count++;
916         }
917         brelse(bh);
918         return count;
919 }
920
921
922 /*
923  * This function fills a red-black tree with information from a
924  * directory.  We start scanning the directory in hash order, starting
925  * at start_hash and start_minor_hash.
926  *
927  * This function returns the number of entries inserted into the tree,
928  * or a negative error code.
929  */
930 int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
931                          __u32 start_minor_hash, __u32 *next_hash)
932 {
933         struct dx_hash_info hinfo;
934         struct ext4_dir_entry_2 *de;
935         struct dx_frame frames[2], *frame;
936         struct inode *dir;
937         ext4_lblk_t block;
938         int count = 0;
939         int ret, err;
940         __u32 hashval;
941
942         dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n",
943                        start_hash, start_minor_hash));
944         dir = dir_file->f_path.dentry->d_inode;
945         if (!(ext4_test_inode_flag(dir, EXT4_INODE_INDEX))) {
946                 hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
947                 if (hinfo.hash_version <= DX_HASH_TEA)
948                         hinfo.hash_version +=
949                                 EXT4_SB(dir->i_sb)->s_hash_unsigned;
950                 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
951                 count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
952                                                start_hash, start_minor_hash);
953                 *next_hash = ~0;
954                 return count;
955         }
956         hinfo.hash = start_hash;
957         hinfo.minor_hash = 0;
958         frame = dx_probe(NULL, dir, &hinfo, frames, &err);
959         if (!frame)
960                 return err;
961
962         /* Add '.' and '..' from the htree header */
963         if (!start_hash && !start_minor_hash) {
964                 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
965                 if ((err = ext4_htree_store_dirent(dir_file, 0, 0, de)) != 0)
966                         goto errout;
967                 count++;
968         }
969         if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
970                 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
971                 de = ext4_next_entry(de, dir->i_sb->s_blocksize);
972                 if ((err = ext4_htree_store_dirent(dir_file, 2, 0, de)) != 0)
973                         goto errout;
974                 count++;
975         }
976
977         while (1) {
978                 block = dx_get_block(frame->at);
979                 ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
980                                              start_hash, start_minor_hash);
981                 if (ret < 0) {
982                         err = ret;
983                         goto errout;
984                 }
985                 count += ret;
986                 hashval = ~0;
987                 ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
988                                             frame, frames, &hashval);
989                 *next_hash = hashval;
990                 if (ret < 0) {
991                         err = ret;
992                         goto errout;
993                 }
994                 /*
995                  * Stop if:  (a) there are no more entries, or
996                  * (b) we have inserted at least one entry and the
997                  * next hash value is not a continuation
998                  */
999                 if ((ret == 0) ||
1000                     (count && ((hashval & 1) == 0)))
1001                         break;
1002         }
1003         dx_release(frames);
1004         dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, "
1005                        "next hash: %x\n", count, *next_hash));
1006         return count;
1007 errout:
1008         dx_release(frames);
1009         return (err);
1010 }
1011
1012
1013 /*
1014  * Directory block splitting, compacting
1015  */
1016
1017 /*
1018  * Create map of hash values, offsets, and sizes, stored at end of block.
1019  * Returns number of entries mapped.
1020  */
1021 static int dx_make_map(struct ext4_dir_entry_2 *de, unsigned blocksize,
1022                        struct dx_hash_info *hinfo,
1023                        struct dx_map_entry *map_tail)
1024 {
1025         int count = 0;
1026         char *base = (char *) de;
1027         struct dx_hash_info h = *hinfo;
1028
1029         while ((char *) de < base + blocksize) {
1030                 if (de->name_len && de->inode) {
1031                         ext4fs_dirhash(de->name, de->name_len, &h);
1032                         map_tail--;
1033                         map_tail->hash = h.hash;
1034                         map_tail->offs = ((char *) de - base)>>2;
1035                         map_tail->size = le16_to_cpu(de->rec_len);
1036                         count++;
1037                         cond_resched();
1038                 }
1039                 /* XXX: do we need to check rec_len == 0 case? -Chris */
1040                 de = ext4_next_entry(de, blocksize);
1041         }
1042         return count;
1043 }
1044
1045 /* Sort map by hash value */
1046 static void dx_sort_map (struct dx_map_entry *map, unsigned count)
1047 {
1048         struct dx_map_entry *p, *q, *top = map + count - 1;
1049         int more;
1050         /* Combsort until bubble sort doesn't suck */
1051         while (count > 2) {
1052                 count = count*10/13;
1053                 if (count - 9 < 2) /* 9, 10 -> 11 */
1054                         count = 11;
1055                 for (p = top, q = p - count; q >= map; p--, q--)
1056                         if (p->hash < q->hash)
1057                                 swap(*p, *q);
1058         }
1059         /* Garden variety bubble sort */
1060         do {
1061                 more = 0;
1062                 q = top;
1063                 while (q-- > map) {
1064                         if (q[1].hash >= q[0].hash)
1065                                 continue;
1066                         swap(*(q+1), *q);
1067                         more = 1;
1068                 }
1069         } while(more);
1070 }
1071
1072 static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
1073 {
1074         struct dx_entry *entries = frame->entries;
1075         struct dx_entry *old = frame->at, *new = old + 1;
1076         int count = dx_get_count(entries);
1077
1078         assert(count < dx_get_limit(entries));
1079         assert(old < entries + count);
1080         memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
1081         dx_set_hash(new, hash);
1082         dx_set_block(new, block);
1083         dx_set_count(entries, count + 1);
1084 }
1085
1086 static void ext4_update_dx_flag(struct inode *inode)
1087 {
1088         if (!EXT4_HAS_COMPAT_FEATURE(inode->i_sb,
1089                                      EXT4_FEATURE_COMPAT_DIR_INDEX))
1090                 ext4_clear_inode_flag(inode, EXT4_INODE_INDEX);
1091 }
1092
1093 /*
1094  * NOTE! unlike strncmp, ext4_match returns 1 for success, 0 for failure.
1095  *
1096  * `len <= EXT4_NAME_LEN' is guaranteed by caller.
1097  * `de != NULL' is guaranteed by caller.
1098  */
1099 static inline int ext4_match (int len, const char * const name,
1100                               struct ext4_dir_entry_2 * de)
1101 {
1102         if (len != de->name_len)
1103                 return 0;
1104         if (!de->inode)
1105                 return 0;
1106         return !memcmp(name, de->name, len);
1107 }
1108
1109 /*
1110  * Returns 0 if not found, -1 on failure, and 1 on success
1111  */
1112 static inline int search_dirblock(struct buffer_head *bh,
1113                                   struct inode *dir,
1114                                   const struct qstr *d_name,
1115                                   unsigned int offset,
1116                                   struct ext4_dir_entry_2 ** res_dir)
1117 {
1118         struct ext4_dir_entry_2 * de;
1119         char * dlimit;
1120         int de_len;
1121         const char *name = d_name->name;
1122         int namelen = d_name->len;
1123
1124         de = (struct ext4_dir_entry_2 *) bh->b_data;
1125         dlimit = bh->b_data + dir->i_sb->s_blocksize;
1126         while ((char *) de < dlimit) {
1127                 /* this code is executed quadratically often */
1128                 /* do minimal checking `by hand' */
1129
1130                 if ((char *) de + namelen <= dlimit &&
1131                     ext4_match (namelen, name, de)) {
1132                         /* found a match - just to be sure, do a full check */
1133                         if (ext4_check_dir_entry(dir, NULL, de, bh, offset))
1134                                 return -1;
1135                         *res_dir = de;
1136                         return 1;
1137                 }
1138                 /* prevent looping on a bad block */
1139                 de_len = ext4_rec_len_from_disk(de->rec_len,
1140                                                 dir->i_sb->s_blocksize);
1141                 if (de_len <= 0)
1142                         return -1;
1143                 offset += de_len;
1144                 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
1145         }
1146         return 0;
1147 }
1148
1149 static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block,
1150                                struct ext4_dir_entry *de)
1151 {
1152         struct super_block *sb = dir->i_sb;
1153
1154         if (!is_dx(dir))
1155                 return 0;
1156         if (block == 0)
1157                 return 1;
1158         if (de->inode == 0 &&
1159             ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) ==
1160                         sb->s_blocksize)
1161                 return 1;
1162         return 0;
1163 }
1164
1165 /*
1166  *      ext4_find_entry()
1167  *
1168  * finds an entry in the specified directory with the wanted name. It
1169  * returns the cache buffer in which the entry was found, and the entry
1170  * itself (as a parameter - res_dir). It does NOT read the inode of the
1171  * entry - you'll have to do that yourself if you want to.
1172  *
1173  * The returned buffer_head has ->b_count elevated.  The caller is expected
1174  * to brelse() it when appropriate.
1175  */
1176 static struct buffer_head * ext4_find_entry (struct inode *dir,
1177                                         const struct qstr *d_name,
1178                                         struct ext4_dir_entry_2 ** res_dir)
1179 {
1180         struct super_block *sb;
1181         struct buffer_head *bh_use[NAMEI_RA_SIZE];
1182         struct buffer_head *bh, *ret = NULL;
1183         ext4_lblk_t start, block, b;
1184         const u8 *name = d_name->name;
1185         int ra_max = 0;         /* Number of bh's in the readahead
1186                                    buffer, bh_use[] */
1187         int ra_ptr = 0;         /* Current index into readahead
1188                                    buffer */
1189         int num = 0;
1190         ext4_lblk_t  nblocks;
1191         int i, err;
1192         int namelen;
1193
1194         *res_dir = NULL;
1195         sb = dir->i_sb;
1196         namelen = d_name->len;
1197         if (namelen > EXT4_NAME_LEN)
1198                 return NULL;
1199         if ((namelen <= 2) && (name[0] == '.') &&
1200             (name[1] == '.' || name[1] == '\0')) {
1201                 /*
1202                  * "." or ".." will only be in the first block
1203                  * NFS may look up ".."; "." should be handled by the VFS
1204                  */
1205                 block = start = 0;
1206                 nblocks = 1;
1207                 goto restart;
1208         }
1209         if (is_dx(dir)) {
1210                 bh = ext4_dx_find_entry(dir, d_name, res_dir, &err);
1211                 /*
1212                  * On success, or if the error was file not found,
1213                  * return.  Otherwise, fall back to doing a search the
1214                  * old fashioned way.
1215                  */
1216                 if (bh || (err != ERR_BAD_DX_DIR))
1217                         return bh;
1218                 dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
1219                                "falling back\n"));
1220         }
1221         nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
1222         start = EXT4_I(dir)->i_dir_start_lookup;
1223         if (start >= nblocks)
1224                 start = 0;
1225         block = start;
1226 restart:
1227         do {
1228                 /*
1229                  * We deal with the read-ahead logic here.
1230                  */
1231                 if (ra_ptr >= ra_max) {
1232                         /* Refill the readahead buffer */
1233                         ra_ptr = 0;
1234                         b = block;
1235                         for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
1236                                 /*
1237                                  * Terminate if we reach the end of the
1238                                  * directory and must wrap, or if our
1239                                  * search has finished at this block.
1240                                  */
1241                                 if (b >= nblocks || (num && block == start)) {
1242                                         bh_use[ra_max] = NULL;
1243                                         break;
1244                                 }
1245                                 num++;
1246                                 bh = ext4_getblk(NULL, dir, b++, 0, &err);
1247                                 bh_use[ra_max] = bh;
1248                                 if (bh)
1249                                         ll_rw_block(READ | REQ_META | REQ_PRIO,
1250                                                     1, &bh);
1251                         }
1252                 }
1253                 if ((bh = bh_use[ra_ptr++]) == NULL)
1254                         goto next;
1255                 wait_on_buffer(bh);
1256                 if (!buffer_uptodate(bh)) {
1257                         /* read error, skip block & hope for the best */
1258                         EXT4_ERROR_INODE(dir, "reading directory lblock %lu",
1259                                          (unsigned long) block);
1260                         brelse(bh);
1261                         goto next;
1262                 }
1263                 if (!buffer_verified(bh) &&
1264                     !is_dx_internal_node(dir, block,
1265                                          (struct ext4_dir_entry *)bh->b_data) &&
1266                     !ext4_dirent_csum_verify(dir,
1267                                 (struct ext4_dir_entry *)bh->b_data)) {
1268                         EXT4_ERROR_INODE(dir, "checksumming directory "
1269                                          "block %lu", (unsigned long)block);
1270                         brelse(bh);
1271                         goto next;
1272                 }
1273                 set_buffer_verified(bh);
1274                 i = search_dirblock(bh, dir, d_name,
1275                             block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
1276                 if (i == 1) {
1277                         EXT4_I(dir)->i_dir_start_lookup = block;
1278                         ret = bh;
1279                         goto cleanup_and_exit;
1280                 } else {
1281                         brelse(bh);
1282                         if (i < 0)
1283                                 goto cleanup_and_exit;
1284                 }
1285         next:
1286                 if (++block >= nblocks)
1287                         block = 0;
1288         } while (block != start);
1289
1290         /*
1291          * If the directory has grown while we were searching, then
1292          * search the last part of the directory before giving up.
1293          */
1294         block = nblocks;
1295         nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
1296         if (block < nblocks) {
1297                 start = 0;
1298                 goto restart;
1299         }
1300
1301 cleanup_and_exit:
1302         /* Clean up the read-ahead blocks */
1303         for (; ra_ptr < ra_max; ra_ptr++)
1304                 brelse(bh_use[ra_ptr]);
1305         return ret;
1306 }
1307
1308 static struct buffer_head * ext4_dx_find_entry(struct inode *dir, const struct qstr *d_name,
1309                        struct ext4_dir_entry_2 **res_dir, int *err)
1310 {
1311         struct super_block * sb = dir->i_sb;
1312         struct dx_hash_info     hinfo;
1313         struct dx_frame frames[2], *frame;
1314         struct buffer_head *bh;
1315         ext4_lblk_t block;
1316         int retval;
1317
1318         if (!(frame = dx_probe(d_name, dir, &hinfo, frames, err)))
1319                 return NULL;
1320         do {
1321                 block = dx_get_block(frame->at);
1322                 if (!(bh = ext4_bread(NULL, dir, block, 0, err))) {
1323                         if (!(*err)) {
1324                                 *err = -EIO;
1325                                 ext4_error(dir->i_sb,
1326                                            "Directory hole detected on inode %lu\n",
1327                                            dir->i_ino);
1328                         }
1329                         goto errout;
1330                 }
1331
1332                 if (!buffer_verified(bh) &&
1333                     !ext4_dirent_csum_verify(dir,
1334                                 (struct ext4_dir_entry *)bh->b_data)) {
1335                         EXT4_ERROR_INODE(dir, "checksumming directory "
1336                                          "block %lu", (unsigned long)block);
1337                         brelse(bh);
1338                         *err = -EIO;
1339                         goto errout;
1340                 }
1341                 set_buffer_verified(bh);
1342                 retval = search_dirblock(bh, dir, d_name,
1343                                          block << EXT4_BLOCK_SIZE_BITS(sb),
1344                                          res_dir);
1345                 if (retval == 1) {      /* Success! */
1346                         dx_release(frames);
1347                         return bh;
1348                 }
1349                 brelse(bh);
1350                 if (retval == -1) {
1351                         *err = ERR_BAD_DX_DIR;
1352                         goto errout;
1353                 }
1354
1355                 /* Check to see if we should continue to search */
1356                 retval = ext4_htree_next_block(dir, hinfo.hash, frame,
1357                                                frames, NULL);
1358                 if (retval < 0) {
1359                         ext4_warning(sb,
1360                              "error reading index page in directory #%lu",
1361                              dir->i_ino);
1362                         *err = retval;
1363                         goto errout;
1364                 }
1365         } while (retval == 1);
1366
1367         *err = -ENOENT;
1368 errout:
1369         dxtrace(printk(KERN_DEBUG "%s not found\n", d_name->name));
1370         dx_release (frames);
1371         return NULL;
1372 }
1373
1374 static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
1375 {
1376         struct inode *inode;
1377         struct ext4_dir_entry_2 *de;
1378         struct buffer_head *bh;
1379
1380         if (dentry->d_name.len > EXT4_NAME_LEN)
1381                 return ERR_PTR(-ENAMETOOLONG);
1382
1383         bh = ext4_find_entry(dir, &dentry->d_name, &de);
1384         inode = NULL;
1385         if (bh) {
1386                 __u32 ino = le32_to_cpu(de->inode);
1387                 brelse(bh);
1388                 if (!ext4_valid_inum(dir->i_sb, ino)) {
1389                         EXT4_ERROR_INODE(dir, "bad inode number: %u", ino);
1390                         return ERR_PTR(-EIO);
1391                 }
1392                 if (unlikely(ino == dir->i_ino)) {
1393                         EXT4_ERROR_INODE(dir, "'%.*s' linked to parent dir",
1394                                          dentry->d_name.len,
1395                                          dentry->d_name.name);
1396                         return ERR_PTR(-EIO);
1397                 }
1398                 inode = ext4_iget(dir->i_sb, ino);
1399                 if (inode == ERR_PTR(-ESTALE)) {
1400                         EXT4_ERROR_INODE(dir,
1401                                          "deleted inode referenced: %u",
1402                                          ino);
1403                         return ERR_PTR(-EIO);
1404                 }
1405         }
1406         return d_splice_alias(inode, dentry);
1407 }
1408
1409
1410 struct dentry *ext4_get_parent(struct dentry *child)
1411 {
1412         __u32 ino;
1413         static const struct qstr dotdot = QSTR_INIT("..", 2);
1414         struct ext4_dir_entry_2 * de;
1415         struct buffer_head *bh;
1416
1417         bh = ext4_find_entry(child->d_inode, &dotdot, &de);
1418         if (!bh)
1419                 return ERR_PTR(-ENOENT);
1420         ino = le32_to_cpu(de->inode);
1421         brelse(bh);
1422
1423         if (!ext4_valid_inum(child->d_inode->i_sb, ino)) {
1424                 EXT4_ERROR_INODE(child->d_inode,
1425                                  "bad parent inode number: %u", ino);
1426                 return ERR_PTR(-EIO);
1427         }
1428
1429         return d_obtain_alias(ext4_iget(child->d_inode->i_sb, ino));
1430 }
1431
1432 #define S_SHIFT 12
1433 static unsigned char ext4_type_by_mode[S_IFMT >> S_SHIFT] = {
1434         [S_IFREG >> S_SHIFT]    = EXT4_FT_REG_FILE,
1435         [S_IFDIR >> S_SHIFT]    = EXT4_FT_DIR,
1436         [S_IFCHR >> S_SHIFT]    = EXT4_FT_CHRDEV,
1437         [S_IFBLK >> S_SHIFT]    = EXT4_FT_BLKDEV,
1438         [S_IFIFO >> S_SHIFT]    = EXT4_FT_FIFO,
1439         [S_IFSOCK >> S_SHIFT]   = EXT4_FT_SOCK,
1440         [S_IFLNK >> S_SHIFT]    = EXT4_FT_SYMLINK,
1441 };
1442
1443 static inline void ext4_set_de_type(struct super_block *sb,
1444                                 struct ext4_dir_entry_2 *de,
1445                                 umode_t mode) {
1446         if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FILETYPE))
1447                 de->file_type = ext4_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
1448 }
1449
1450 /*
1451  * Move count entries from end of map between two memory locations.
1452  * Returns pointer to last entry moved.
1453  */
1454 static struct ext4_dir_entry_2 *
1455 dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count,
1456                 unsigned blocksize)
1457 {
1458         unsigned rec_len = 0;
1459
1460         while (count--) {
1461                 struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)
1462                                                 (from + (map->offs<<2));
1463                 rec_len = EXT4_DIR_REC_LEN(de->name_len);
1464                 memcpy (to, de, rec_len);
1465                 ((struct ext4_dir_entry_2 *) to)->rec_len =
1466                                 ext4_rec_len_to_disk(rec_len, blocksize);
1467                 de->inode = 0;
1468                 map++;
1469                 to += rec_len;
1470         }
1471         return (struct ext4_dir_entry_2 *) (to - rec_len);
1472 }
1473
1474 /*
1475  * Compact each dir entry in the range to the minimal rec_len.
1476  * Returns pointer to last entry in range.
1477  */
1478 static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize)
1479 {
1480         struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
1481         unsigned rec_len = 0;
1482
1483         prev = to = de;
1484         while ((char*)de < base + blocksize) {
1485                 next = ext4_next_entry(de, blocksize);
1486                 if (de->inode && de->name_len) {
1487                         rec_len = EXT4_DIR_REC_LEN(de->name_len);
1488                         if (de > to)
1489                                 memmove(to, de, rec_len);
1490                         to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
1491                         prev = to;
1492                         to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
1493                 }
1494                 de = next;
1495         }
1496         return prev;
1497 }
1498
1499 /*
1500  * Split a full leaf block to make room for a new dir entry.
1501  * Allocate a new block, and move entries so that they are approx. equally full.
1502  * Returns pointer to de in block into which the new entry will be inserted.
1503  */
1504 static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
1505                         struct buffer_head **bh,struct dx_frame *frame,
1506                         struct dx_hash_info *hinfo, int *error)
1507 {
1508         unsigned blocksize = dir->i_sb->s_blocksize;
1509         unsigned count, continued;
1510         struct buffer_head *bh2;
1511         ext4_lblk_t newblock;
1512         u32 hash2;
1513         struct dx_map_entry *map;
1514         char *data1 = (*bh)->b_data, *data2;
1515         unsigned split, move, size;
1516         struct ext4_dir_entry_2 *de = NULL, *de2;
1517         struct ext4_dir_entry_tail *t;
1518         int     csum_size = 0;
1519         int     err = 0, i;
1520
1521         if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
1522                                        EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
1523                 csum_size = sizeof(struct ext4_dir_entry_tail);
1524
1525         bh2 = ext4_append (handle, dir, &newblock, &err);
1526         if (!(bh2)) {
1527                 brelse(*bh);
1528                 *bh = NULL;
1529                 goto errout;
1530         }
1531
1532         BUFFER_TRACE(*bh, "get_write_access");
1533         err = ext4_journal_get_write_access(handle, *bh);
1534         if (err)
1535                 goto journal_error;
1536
1537         BUFFER_TRACE(frame->bh, "get_write_access");
1538         err = ext4_journal_get_write_access(handle, frame->bh);
1539         if (err)
1540                 goto journal_error;
1541
1542         data2 = bh2->b_data;
1543
1544         /* create map in the end of data2 block */
1545         map = (struct dx_map_entry *) (data2 + blocksize);
1546         count = dx_make_map((struct ext4_dir_entry_2 *) data1,
1547                              blocksize, hinfo, map);
1548         map -= count;
1549         dx_sort_map(map, count);
1550         /* Split the existing block in the middle, size-wise */
1551         size = 0;
1552         move = 0;
1553         for (i = count-1; i >= 0; i--) {
1554                 /* is more than half of this entry in 2nd half of the block? */
1555                 if (size + map[i].size/2 > blocksize/2)
1556                         break;
1557                 size += map[i].size;
1558                 move++;
1559         }
1560         /* map index at which we will split */
1561         split = count - move;
1562         hash2 = map[split].hash;
1563         continued = hash2 == map[split - 1].hash;
1564         dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
1565                         (unsigned long)dx_get_block(frame->at),
1566                                         hash2, split, count-split));
1567
1568         /* Fancy dance to stay within two buffers */
1569         de2 = dx_move_dirents(data1, data2, map + split, count - split, blocksize);
1570         de = dx_pack_dirents(data1, blocksize);
1571         de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
1572                                            (char *) de,
1573                                            blocksize);
1574         de2->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
1575                                             (char *) de2,
1576                                             blocksize);
1577         if (csum_size) {
1578                 t = EXT4_DIRENT_TAIL(data2, blocksize);
1579                 initialize_dirent_tail(t, blocksize);
1580
1581                 t = EXT4_DIRENT_TAIL(data1, blocksize);
1582                 initialize_dirent_tail(t, blocksize);
1583         }
1584
1585         dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data1, blocksize, 1));
1586         dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data2, blocksize, 1));
1587
1588         /* Which block gets the new entry? */
1589         if (hinfo->hash >= hash2)
1590         {
1591                 swap(*bh, bh2);
1592                 de = de2;
1593         }
1594         dx_insert_block(frame, hash2 + continued, newblock);
1595         err = ext4_handle_dirty_dirent_node(handle, dir, bh2);
1596         if (err)
1597                 goto journal_error;
1598         err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
1599         if (err)
1600                 goto journal_error;
1601         brelse(bh2);
1602         dxtrace(dx_show_index("frame", frame->entries));
1603         return de;
1604
1605 journal_error:
1606         brelse(*bh);
1607         brelse(bh2);
1608         *bh = NULL;
1609         ext4_std_error(dir->i_sb, err);
1610 errout:
1611         *error = err;
1612         return NULL;
1613 }
1614
1615 /*
1616  * Add a new entry into a directory (leaf) block.  If de is non-NULL,
1617  * it points to a directory entry which is guaranteed to be large
1618  * enough for new directory entry.  If de is NULL, then
1619  * add_dirent_to_buf will attempt search the directory block for
1620  * space.  It will return -ENOSPC if no space is available, and -EIO
1621  * and -EEXIST if directory entry already exists.
1622  */
1623 static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
1624                              struct inode *inode, struct ext4_dir_entry_2 *de,
1625                              struct buffer_head *bh)
1626 {
1627         struct inode    *dir = dentry->d_parent->d_inode;
1628         const char      *name = dentry->d_name.name;
1629         int             namelen = dentry->d_name.len;
1630         unsigned int    offset = 0;
1631         unsigned int    blocksize = dir->i_sb->s_blocksize;
1632         unsigned short  reclen;
1633         int             nlen, rlen, err;
1634         char            *top;
1635         int             csum_size = 0;
1636
1637         if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
1638                                        EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
1639                 csum_size = sizeof(struct ext4_dir_entry_tail);
1640
1641         reclen = EXT4_DIR_REC_LEN(namelen);
1642         if (!de) {
1643                 de = (struct ext4_dir_entry_2 *)bh->b_data;
1644                 top = bh->b_data + (blocksize - csum_size) - reclen;
1645                 while ((char *) de <= top) {
1646                         if (ext4_check_dir_entry(dir, NULL, de, bh, offset))
1647                                 return -EIO;
1648                         if (ext4_match(namelen, name, de))
1649                                 return -EEXIST;
1650                         nlen = EXT4_DIR_REC_LEN(de->name_len);
1651                         rlen = ext4_rec_len_from_disk(de->rec_len, blocksize);
1652                         if ((de->inode? rlen - nlen: rlen) >= reclen)
1653                                 break;
1654                         de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
1655                         offset += rlen;
1656                 }
1657                 if ((char *) de > top)
1658                         return -ENOSPC;
1659         }
1660         BUFFER_TRACE(bh, "get_write_access");
1661         err = ext4_journal_get_write_access(handle, bh);
1662         if (err) {
1663                 ext4_std_error(dir->i_sb, err);
1664                 return err;
1665         }
1666
1667         /* By now the buffer is marked for journaling */
1668         nlen = EXT4_DIR_REC_LEN(de->name_len);
1669         rlen = ext4_rec_len_from_disk(de->rec_len, blocksize);
1670         if (de->inode) {
1671                 struct ext4_dir_entry_2 *de1 = (struct ext4_dir_entry_2 *)((char *)de + nlen);
1672                 de1->rec_len = ext4_rec_len_to_disk(rlen - nlen, blocksize);
1673                 de->rec_len = ext4_rec_len_to_disk(nlen, blocksize);
1674                 de = de1;
1675         }
1676         de->file_type = EXT4_FT_UNKNOWN;
1677         de->inode = cpu_to_le32(inode->i_ino);
1678         ext4_set_de_type(dir->i_sb, de, inode->i_mode);
1679         de->name_len = namelen;
1680         memcpy(de->name, name, namelen);
1681         /*
1682          * XXX shouldn't update any times until successful
1683          * completion of syscall, but too many callers depend
1684          * on this.
1685          *
1686          * XXX similarly, too many callers depend on
1687          * ext4_new_inode() setting the times, but error
1688          * recovery deletes the inode, so the worst that can
1689          * happen is that the times are slightly out of date
1690          * and/or different from the directory change time.
1691          */
1692         dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
1693         ext4_update_dx_flag(dir);
1694         dir->i_version++;
1695         ext4_mark_inode_dirty(handle, dir);
1696         BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
1697         err = ext4_handle_dirty_dirent_node(handle, dir, bh);
1698         if (err)
1699                 ext4_std_error(dir->i_sb, err);
1700         return 0;
1701 }
1702
1703 /*
1704  * This converts a one block unindexed directory to a 3 block indexed
1705  * directory, and adds the dentry to the indexed directory.
1706  */
1707 static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
1708                             struct inode *inode, struct buffer_head *bh)
1709 {
1710         struct inode    *dir = dentry->d_parent->d_inode;
1711         const char      *name = dentry->d_name.name;
1712         int             namelen = dentry->d_name.len;
1713         struct buffer_head *bh2;
1714         struct dx_root  *root;
1715         struct dx_frame frames[2], *frame;
1716         struct dx_entry *entries;
1717         struct ext4_dir_entry_2 *de, *de2;
1718         struct ext4_dir_entry_tail *t;
1719         char            *data1, *top;
1720         unsigned        len;
1721         int             retval;
1722         unsigned        blocksize;
1723         struct dx_hash_info hinfo;
1724         ext4_lblk_t  block;
1725         struct fake_dirent *fde;
1726         int             csum_size = 0;
1727
1728         if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
1729                                        EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
1730                 csum_size = sizeof(struct ext4_dir_entry_tail);
1731
1732         blocksize =  dir->i_sb->s_blocksize;
1733         dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino));
1734         retval = ext4_journal_get_write_access(handle, bh);
1735         if (retval) {
1736                 ext4_std_error(dir->i_sb, retval);
1737                 brelse(bh);
1738                 return retval;
1739         }
1740         root = (struct dx_root *) bh->b_data;
1741
1742         /* The 0th block becomes the root, move the dirents out */
1743         fde = &root->dotdot;
1744         de = (struct ext4_dir_entry_2 *)((char *)fde +
1745                 ext4_rec_len_from_disk(fde->rec_len, blocksize));
1746         if ((char *) de >= (((char *) root) + blocksize)) {
1747                 EXT4_ERROR_INODE(dir, "invalid rec_len for '..'");
1748                 brelse(bh);
1749                 return -EIO;
1750         }
1751         len = ((char *) root) + (blocksize - csum_size) - (char *) de;
1752
1753         /* Allocate new block for the 0th block's dirents */
1754         bh2 = ext4_append(handle, dir, &block, &retval);
1755         if (!(bh2)) {
1756                 brelse(bh);
1757                 return retval;
1758         }
1759         ext4_set_inode_flag(dir, EXT4_INODE_INDEX);
1760         data1 = bh2->b_data;
1761
1762         memcpy (data1, de, len);
1763         de = (struct ext4_dir_entry_2 *) data1;
1764         top = data1 + len;
1765         while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top)
1766                 de = de2;
1767         de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
1768                                            (char *) de,
1769                                            blocksize);
1770
1771         if (csum_size) {
1772                 t = EXT4_DIRENT_TAIL(data1, blocksize);
1773                 initialize_dirent_tail(t, blocksize);
1774         }
1775
1776         /* Initialize the root; the dot dirents already exist */
1777         de = (struct ext4_dir_entry_2 *) (&root->dotdot);
1778         de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2),
1779                                            blocksize);
1780         memset (&root->info, 0, sizeof(root->info));
1781         root->info.info_length = sizeof(root->info);
1782         root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
1783         entries = root->entries;
1784         dx_set_block(entries, 1);
1785         dx_set_count(entries, 1);
1786         dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
1787
1788         /* Initialize as for dx_probe */
1789         hinfo.hash_version = root->info.hash_version;
1790         if (hinfo.hash_version <= DX_HASH_TEA)
1791                 hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
1792         hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
1793         ext4fs_dirhash(name, namelen, &hinfo);
1794         frame = frames;
1795         frame->entries = entries;
1796         frame->at = entries;
1797         frame->bh = bh;
1798         bh = bh2;
1799
1800         ext4_handle_dirty_dx_node(handle, dir, frame->bh);
1801         ext4_handle_dirty_dirent_node(handle, dir, bh);
1802
1803         de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
1804         if (!de) {
1805                 /*
1806                  * Even if the block split failed, we have to properly write
1807                  * out all the changes we did so far. Otherwise we can end up
1808                  * with corrupted filesystem.
1809                  */
1810                 ext4_mark_inode_dirty(handle, dir);
1811                 dx_release(frames);
1812                 return retval;
1813         }
1814         dx_release(frames);
1815
1816         retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
1817         brelse(bh);
1818         return retval;
1819 }
1820
1821 /*
1822  *      ext4_add_entry()
1823  *
1824  * adds a file entry to the specified directory, using the same
1825  * semantics as ext4_find_entry(). It returns NULL if it failed.
1826  *
1827  * NOTE!! The inode part of 'de' is left at 0 - which means you
1828  * may not sleep between calling this and putting something into
1829  * the entry, as someone else might have used it while you slept.
1830  */
1831 static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
1832                           struct inode *inode)
1833 {
1834         struct inode *dir = dentry->d_parent->d_inode;
1835         struct buffer_head *bh;
1836         struct ext4_dir_entry_2 *de;
1837         struct ext4_dir_entry_tail *t;
1838         struct super_block *sb;
1839         int     retval;
1840         int     dx_fallback=0;
1841         unsigned blocksize;
1842         ext4_lblk_t block, blocks;
1843         int     csum_size = 0;
1844
1845         if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
1846                                        EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
1847                 csum_size = sizeof(struct ext4_dir_entry_tail);
1848
1849         sb = dir->i_sb;
1850         blocksize = sb->s_blocksize;
1851         if (!dentry->d_name.len)
1852                 return -EINVAL;
1853         if (is_dx(dir)) {
1854                 retval = ext4_dx_add_entry(handle, dentry, inode);
1855                 if (!retval || (retval != ERR_BAD_DX_DIR))
1856                         return retval;
1857                 ext4_clear_inode_flag(dir, EXT4_INODE_INDEX);
1858                 dx_fallback++;
1859                 ext4_mark_inode_dirty(handle, dir);
1860         }
1861         blocks = dir->i_size >> sb->s_blocksize_bits;
1862         for (block = 0; block < blocks; block++) {
1863                 if (!(bh = ext4_bread(handle, dir, block, 0, &retval))) {
1864                         if (!retval) {
1865                                 retval = -EIO;
1866                                 ext4_error(inode->i_sb,
1867                                            "Directory hole detected on inode %lu\n",
1868                                            inode->i_ino);
1869                         }
1870                         return retval;
1871                 }
1872                 if (!buffer_verified(bh) &&
1873                     !ext4_dirent_csum_verify(dir,
1874                                 (struct ext4_dir_entry *)bh->b_data))
1875                         return -EIO;
1876                 set_buffer_verified(bh);
1877                 retval = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
1878                 if (retval != -ENOSPC) {
1879                         brelse(bh);
1880                         return retval;
1881                 }
1882
1883                 if (blocks == 1 && !dx_fallback &&
1884                     EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_DIR_INDEX))
1885                         return make_indexed_dir(handle, dentry, inode, bh);
1886                 brelse(bh);
1887         }
1888         bh = ext4_append(handle, dir, &block, &retval);
1889         if (!bh)
1890                 return retval;
1891         de = (struct ext4_dir_entry_2 *) bh->b_data;
1892         de->inode = 0;
1893         de->rec_len = ext4_rec_len_to_disk(blocksize - csum_size, blocksize);
1894
1895         if (csum_size) {
1896                 t = EXT4_DIRENT_TAIL(bh->b_data, blocksize);
1897                 initialize_dirent_tail(t, blocksize);
1898         }
1899
1900         retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
1901         brelse(bh);
1902         if (retval == 0)
1903                 ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
1904         return retval;
1905 }
1906
1907 /*
1908  * Returns 0 for success, or a negative error value
1909  */
1910 static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
1911                              struct inode *inode)
1912 {
1913         struct dx_frame frames[2], *frame;
1914         struct dx_entry *entries, *at;
1915         struct dx_hash_info hinfo;
1916         struct buffer_head *bh;
1917         struct inode *dir = dentry->d_parent->d_inode;
1918         struct super_block *sb = dir->i_sb;
1919         struct ext4_dir_entry_2 *de;
1920         int err;
1921
1922         frame = dx_probe(&dentry->d_name, dir, &hinfo, frames, &err);
1923         if (!frame)
1924                 return err;
1925         entries = frame->entries;
1926         at = frame->at;
1927
1928         if (!(bh = ext4_bread(handle, dir, dx_get_block(frame->at), 0, &err))) {
1929                 if (!err) {
1930                         err = -EIO;
1931                         ext4_error(dir->i_sb,
1932                                    "Directory hole detected on inode %lu\n",
1933                                    dir->i_ino);
1934                 }
1935                 goto cleanup;
1936         }
1937
1938         if (!buffer_verified(bh) &&
1939             !ext4_dirent_csum_verify(dir, (struct ext4_dir_entry *)bh->b_data))
1940                 goto journal_error;
1941         set_buffer_verified(bh);
1942
1943         BUFFER_TRACE(bh, "get_write_access");
1944         err = ext4_journal_get_write_access(handle, bh);
1945         if (err)
1946                 goto journal_error;
1947
1948         err = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
1949         if (err != -ENOSPC)
1950                 goto cleanup;
1951
1952         /* Block full, should compress but for now just split */
1953         dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
1954                        dx_get_count(entries), dx_get_limit(entries)));
1955         /* Need to split index? */
1956         if (dx_get_count(entries) == dx_get_limit(entries)) {
1957                 ext4_lblk_t newblock;
1958                 unsigned icount = dx_get_count(entries);
1959                 int levels = frame - frames;
1960                 struct dx_entry *entries2;
1961                 struct dx_node *node2;
1962                 struct buffer_head *bh2;
1963
1964                 if (levels && (dx_get_count(frames->entries) ==
1965                                dx_get_limit(frames->entries))) {
1966                         ext4_warning(sb, "Directory index full!");
1967                         err = -ENOSPC;
1968                         goto cleanup;
1969                 }
1970                 bh2 = ext4_append (handle, dir, &newblock, &err);
1971                 if (!(bh2))
1972                         goto cleanup;
1973                 node2 = (struct dx_node *)(bh2->b_data);
1974                 entries2 = node2->entries;
1975                 memset(&node2->fake, 0, sizeof(struct fake_dirent));
1976                 node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize,
1977                                                            sb->s_blocksize);
1978                 BUFFER_TRACE(frame->bh, "get_write_access");
1979                 err = ext4_journal_get_write_access(handle, frame->bh);
1980                 if (err)
1981                         goto journal_error;
1982                 if (levels) {
1983                         unsigned icount1 = icount/2, icount2 = icount - icount1;
1984                         unsigned hash2 = dx_get_hash(entries + icount1);
1985                         dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
1986                                        icount1, icount2));
1987
1988                         BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
1989                         err = ext4_journal_get_write_access(handle,
1990                                                              frames[0].bh);
1991                         if (err)
1992                                 goto journal_error;
1993
1994                         memcpy((char *) entries2, (char *) (entries + icount1),
1995                                icount2 * sizeof(struct dx_entry));
1996                         dx_set_count(entries, icount1);
1997                         dx_set_count(entries2, icount2);
1998                         dx_set_limit(entries2, dx_node_limit(dir));
1999
2000                         /* Which index block gets the new entry? */
2001                         if (at - entries >= icount1) {
2002                                 frame->at = at = at - entries - icount1 + entries2;
2003                                 frame->entries = entries = entries2;
2004                                 swap(frame->bh, bh2);
2005                         }
2006                         dx_insert_block(frames + 0, hash2, newblock);
2007                         dxtrace(dx_show_index("node", frames[1].entries));
2008                         dxtrace(dx_show_index("node",
2009                                ((struct dx_node *) bh2->b_data)->entries));
2010                         err = ext4_handle_dirty_dx_node(handle, dir, bh2);
2011                         if (err)
2012                                 goto journal_error;
2013                         brelse (bh2);
2014                 } else {
2015                         dxtrace(printk(KERN_DEBUG
2016                                        "Creating second level index...\n"));
2017                         memcpy((char *) entries2, (char *) entries,
2018                                icount * sizeof(struct dx_entry));
2019                         dx_set_limit(entries2, dx_node_limit(dir));
2020
2021                         /* Set up root */
2022                         dx_set_count(entries, 1);
2023                         dx_set_block(entries + 0, newblock);
2024                         ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
2025
2026                         /* Add new access path frame */
2027                         frame = frames + 1;
2028                         frame->at = at = at - entries + entries2;
2029                         frame->entries = entries = entries2;
2030                         frame->bh = bh2;
2031                         err = ext4_journal_get_write_access(handle,
2032                                                              frame->bh);
2033                         if (err)
2034                                 goto journal_error;
2035                 }
2036                 err = ext4_handle_dirty_dx_node(handle, dir, frames[0].bh);
2037                 if (err) {
2038                         ext4_std_error(inode->i_sb, err);
2039                         goto cleanup;
2040                 }
2041         }
2042         de = do_split(handle, dir, &bh, frame, &hinfo, &err);
2043         if (!de)
2044                 goto cleanup;
2045         err = add_dirent_to_buf(handle, dentry, inode, de, bh);
2046         goto cleanup;
2047
2048 journal_error:
2049         ext4_std_error(dir->i_sb, err);
2050 cleanup:
2051         if (bh)
2052                 brelse(bh);
2053         dx_release(frames);
2054         return err;
2055 }
2056
2057 /*
2058  * ext4_delete_entry deletes a directory entry by merging it with the
2059  * previous entry
2060  */
2061 static int ext4_delete_entry(handle_t *handle,
2062                              struct inode *dir,
2063                              struct ext4_dir_entry_2 *de_del,
2064                              struct buffer_head *bh)
2065 {
2066         struct ext4_dir_entry_2 *de, *pde;
2067         unsigned int blocksize = dir->i_sb->s_blocksize;
2068         int csum_size = 0;
2069         int i, err;
2070
2071         if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
2072                                        EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
2073                 csum_size = sizeof(struct ext4_dir_entry_tail);
2074
2075         i = 0;
2076         pde = NULL;
2077         de = (struct ext4_dir_entry_2 *) bh->b_data;
2078         while (i < bh->b_size - csum_size) {
2079                 if (ext4_check_dir_entry(dir, NULL, de, bh, i))
2080                         return -EIO;
2081                 if (de == de_del)  {
2082                         BUFFER_TRACE(bh, "get_write_access");
2083                         err = ext4_journal_get_write_access(handle, bh);
2084                         if (unlikely(err)) {
2085                                 ext4_std_error(dir->i_sb, err);
2086                                 return err;
2087                         }
2088                         if (pde)
2089                                 pde->rec_len = ext4_rec_len_to_disk(
2090                                         ext4_rec_len_from_disk(pde->rec_len,
2091                                                                blocksize) +
2092                                         ext4_rec_len_from_disk(de->rec_len,
2093                                                                blocksize),
2094                                         blocksize);
2095                         else
2096                                 de->inode = 0;
2097                         dir->i_version++;
2098                         BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
2099                         err = ext4_handle_dirty_dirent_node(handle, dir, bh);
2100                         if (unlikely(err)) {
2101                                 ext4_std_error(dir->i_sb, err);
2102                                 return err;
2103                         }
2104                         return 0;
2105                 }
2106                 i += ext4_rec_len_from_disk(de->rec_len, blocksize);
2107                 pde = de;
2108                 de = ext4_next_entry(de, blocksize);
2109         }
2110         return -ENOENT;
2111 }
2112
2113 /*
2114  * DIR_NLINK feature is set if 1) nlinks > EXT4_LINK_MAX or 2) nlinks == 2,
2115  * since this indicates that nlinks count was previously 1.
2116  */
2117 static void ext4_inc_count(handle_t *handle, struct inode *inode)
2118 {
2119         inc_nlink(inode);
2120         if (is_dx(inode) && inode->i_nlink > 1) {
2121                 /* limit is 16-bit i_links_count */
2122                 if (inode->i_nlink >= EXT4_LINK_MAX || inode->i_nlink == 2) {
2123                         set_nlink(inode, 1);
2124                         EXT4_SET_RO_COMPAT_FEATURE(inode->i_sb,
2125                                               EXT4_FEATURE_RO_COMPAT_DIR_NLINK);
2126                 }
2127         }
2128 }
2129
2130 /*
2131  * If a directory had nlink == 1, then we should let it be 1. This indicates
2132  * directory has >EXT4_LINK_MAX subdirs.
2133  */
2134 static void ext4_dec_count(handle_t *handle, struct inode *inode)
2135 {
2136         if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
2137                 drop_nlink(inode);
2138 }
2139
2140
2141 static int ext4_add_nondir(handle_t *handle,
2142                 struct dentry *dentry, struct inode *inode)
2143 {
2144         int err = ext4_add_entry(handle, dentry, inode);
2145         if (!err) {
2146                 ext4_mark_inode_dirty(handle, inode);
2147                 unlock_new_inode(inode);
2148                 d_instantiate(dentry, inode);
2149                 return 0;
2150         }
2151         drop_nlink(inode);
2152         unlock_new_inode(inode);
2153         iput(inode);
2154         return err;
2155 }
2156
2157 /*
2158  * By the time this is called, we already have created
2159  * the directory cache entry for the new file, but it
2160  * is so far negative - it has no inode.
2161  *
2162  * If the create succeeds, we fill in the inode information
2163  * with d_instantiate().
2164  */
2165 static int ext4_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2166                        bool excl)
2167 {
2168         handle_t *handle;
2169         struct inode *inode;
2170         int err, retries = 0;
2171
2172         dquot_initialize(dir);
2173
2174 retry:
2175         handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2176                                         EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
2177                                         EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
2178         if (IS_ERR(handle))
2179                 return PTR_ERR(handle);
2180
2181         if (IS_DIRSYNC(dir))
2182                 ext4_handle_sync(handle);
2183
2184         inode = ext4_new_inode(handle, dir, mode, &dentry->d_name, 0, NULL);
2185         err = PTR_ERR(inode);
2186         if (!IS_ERR(inode)) {
2187                 inode->i_op = &ext4_file_inode_operations;
2188                 inode->i_fop = &ext4_file_operations;
2189                 ext4_set_aops(inode);
2190                 err = ext4_add_nondir(handle, dentry, inode);
2191         }
2192         ext4_journal_stop(handle);
2193         if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2194                 goto retry;
2195         return err;
2196 }
2197
2198 static int ext4_mknod(struct inode *dir, struct dentry *dentry,
2199                       umode_t mode, dev_t rdev)
2200 {
2201         handle_t *handle;
2202         struct inode *inode;
2203         int err, retries = 0;
2204
2205         if (!new_valid_dev(rdev))
2206                 return -EINVAL;
2207
2208         dquot_initialize(dir);
2209
2210 retry:
2211         handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2212                                         EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
2213                                         EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
2214         if (IS_ERR(handle))
2215                 return PTR_ERR(handle);
2216
2217         if (IS_DIRSYNC(dir))
2218                 ext4_handle_sync(handle);
2219
2220         inode = ext4_new_inode(handle, dir, mode, &dentry->d_name, 0, NULL);
2221         err = PTR_ERR(inode);
2222         if (!IS_ERR(inode)) {
2223                 init_special_inode(inode, inode->i_mode, rdev);
2224                 inode->i_op = &ext4_special_inode_operations;
2225                 err = ext4_add_nondir(handle, dentry, inode);
2226         }
2227         ext4_journal_stop(handle);
2228         if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2229                 goto retry;
2230         return err;
2231 }
2232
2233 static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
2234 {
2235         handle_t *handle;
2236         struct inode *inode;
2237         struct buffer_head *dir_block = NULL;
2238         struct ext4_dir_entry_2 *de;
2239         struct ext4_dir_entry_tail *t;
2240         unsigned int blocksize = dir->i_sb->s_blocksize;
2241         int csum_size = 0;
2242         int err, retries = 0;
2243
2244         if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
2245                                        EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
2246                 csum_size = sizeof(struct ext4_dir_entry_tail);
2247
2248         if (EXT4_DIR_LINK_MAX(dir))
2249                 return -EMLINK;
2250
2251         dquot_initialize(dir);
2252
2253 retry:
2254         handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2255                                         EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
2256                                         EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
2257         if (IS_ERR(handle))
2258                 return PTR_ERR(handle);
2259
2260         if (IS_DIRSYNC(dir))
2261                 ext4_handle_sync(handle);
2262
2263         inode = ext4_new_inode(handle, dir, S_IFDIR | mode,
2264                                &dentry->d_name, 0, NULL);
2265         err = PTR_ERR(inode);
2266         if (IS_ERR(inode))
2267                 goto out_stop;
2268
2269         inode->i_op = &ext4_dir_inode_operations;
2270         inode->i_fop = &ext4_dir_operations;
2271         inode->i_size = EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
2272         if (!(dir_block = ext4_bread(handle, inode, 0, 1, &err))) {
2273                 if (!err) {
2274                         err = -EIO;
2275                         ext4_error(inode->i_sb,
2276                                    "Directory hole detected on inode %lu\n",
2277                                    inode->i_ino);
2278                 }
2279                 goto out_clear_inode;
2280         }
2281         BUFFER_TRACE(dir_block, "get_write_access");
2282         err = ext4_journal_get_write_access(handle, dir_block);
2283         if (err)
2284                 goto out_clear_inode;
2285         de = (struct ext4_dir_entry_2 *) dir_block->b_data;
2286         de->inode = cpu_to_le32(inode->i_ino);
2287         de->name_len = 1;
2288         de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len),
2289                                            blocksize);
2290         strcpy(de->name, ".");
2291         ext4_set_de_type(dir->i_sb, de, S_IFDIR);
2292         de = ext4_next_entry(de, blocksize);
2293         de->inode = cpu_to_le32(dir->i_ino);
2294         de->rec_len = ext4_rec_len_to_disk(blocksize -
2295                                            (csum_size + EXT4_DIR_REC_LEN(1)),
2296                                            blocksize);
2297         de->name_len = 2;
2298         strcpy(de->name, "..");
2299         ext4_set_de_type(dir->i_sb, de, S_IFDIR);
2300         set_nlink(inode, 2);
2301
2302         if (csum_size) {
2303                 t = EXT4_DIRENT_TAIL(dir_block->b_data, blocksize);
2304                 initialize_dirent_tail(t, blocksize);
2305         }
2306
2307         BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata");
2308         err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
2309         if (err)
2310                 goto out_clear_inode;
2311         set_buffer_verified(dir_block);
2312         err = ext4_mark_inode_dirty(handle, inode);
2313         if (!err)
2314                 err = ext4_add_entry(handle, dentry, inode);
2315         if (err) {
2316 out_clear_inode:
2317                 clear_nlink(inode);
2318                 unlock_new_inode(inode);
2319                 ext4_mark_inode_dirty(handle, inode);
2320                 iput(inode);
2321                 goto out_stop;
2322         }
2323         ext4_inc_count(handle, dir);
2324         ext4_update_dx_flag(dir);
2325         err = ext4_mark_inode_dirty(handle, dir);
2326         if (err)
2327                 goto out_clear_inode;
2328         unlock_new_inode(inode);
2329         d_instantiate(dentry, inode);
2330 out_stop:
2331         brelse(dir_block);
2332         ext4_journal_stop(handle);
2333         if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2334                 goto retry;
2335         return err;
2336 }
2337
2338 /*
2339  * routine to check that the specified directory is empty (for rmdir)
2340  */
2341 static int empty_dir(struct inode *inode)
2342 {
2343         unsigned int offset;
2344         struct buffer_head *bh;
2345         struct ext4_dir_entry_2 *de, *de1;
2346         struct super_block *sb;
2347         int err = 0;
2348
2349         sb = inode->i_sb;
2350         if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2) ||
2351             !(bh = ext4_bread(NULL, inode, 0, 0, &err))) {
2352                 if (err)
2353                         EXT4_ERROR_INODE(inode,
2354                                 "error %d reading directory lblock 0", err);
2355                 else
2356                         ext4_warning(inode->i_sb,
2357                                      "bad directory (dir #%lu) - no data block",
2358                                      inode->i_ino);
2359                 return 1;
2360         }
2361         if (!buffer_verified(bh) &&
2362             !ext4_dirent_csum_verify(inode,
2363                         (struct ext4_dir_entry *)bh->b_data)) {
2364                 EXT4_ERROR_INODE(inode, "checksum error reading directory "
2365                                  "lblock 0");
2366                 return -EIO;
2367         }
2368         set_buffer_verified(bh);
2369         de = (struct ext4_dir_entry_2 *) bh->b_data;
2370         de1 = ext4_next_entry(de, sb->s_blocksize);
2371         if (le32_to_cpu(de->inode) != inode->i_ino ||
2372                         !le32_to_cpu(de1->inode) ||
2373                         strcmp(".", de->name) ||
2374                         strcmp("..", de1->name)) {
2375                 ext4_warning(inode->i_sb,
2376                              "bad directory (dir #%lu) - no `.' or `..'",
2377                              inode->i_ino);
2378                 brelse(bh);
2379                 return 1;
2380         }
2381         offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) +
2382                  ext4_rec_len_from_disk(de1->rec_len, sb->s_blocksize);
2383         de = ext4_next_entry(de1, sb->s_blocksize);
2384         while (offset < inode->i_size) {
2385                 if (!bh ||
2386                     (void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
2387                         unsigned int lblock;
2388                         err = 0;
2389                         brelse(bh);
2390                         lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb);
2391                         bh = ext4_bread(NULL, inode, lblock, 0, &err);
2392                         if (!bh) {
2393                                 if (err)
2394                                         EXT4_ERROR_INODE(inode,
2395                                                 "error %d reading directory "
2396                                                 "lblock %u", err, lblock);
2397                                 else
2398                                         ext4_warning(inode->i_sb,
2399                                                 "bad directory (dir #%lu) - no data block",
2400                                                 inode->i_ino);
2401
2402                                 offset += sb->s_blocksize;
2403                                 continue;
2404                         }
2405                         if (!buffer_verified(bh) &&
2406                             !ext4_dirent_csum_verify(inode,
2407                                         (struct ext4_dir_entry *)bh->b_data)) {
2408                                 EXT4_ERROR_INODE(inode, "checksum error "
2409                                                  "reading directory lblock 0");
2410                                 return -EIO;
2411                         }
2412                         set_buffer_verified(bh);
2413                         de = (struct ext4_dir_entry_2 *) bh->b_data;
2414                 }
2415                 if (ext4_check_dir_entry(inode, NULL, de, bh, offset)) {
2416                         de = (struct ext4_dir_entry_2 *)(bh->b_data +
2417                                                          sb->s_blocksize);
2418                         offset = (offset | (sb->s_blocksize - 1)) + 1;
2419                         continue;
2420                 }
2421                 if (le32_to_cpu(de->inode)) {
2422                         brelse(bh);
2423                         return 0;
2424                 }
2425                 offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
2426                 de = ext4_next_entry(de, sb->s_blocksize);
2427         }
2428         brelse(bh);
2429         return 1;
2430 }
2431
2432 /* ext4_orphan_add() links an unlinked or truncated inode into a list of
2433  * such inodes, starting at the superblock, in case we crash before the
2434  * file is closed/deleted, or in case the inode truncate spans multiple
2435  * transactions and the last transaction is not recovered after a crash.
2436  *
2437  * At filesystem recovery time, we walk this list deleting unlinked
2438  * inodes and truncating linked inodes in ext4_orphan_cleanup().
2439  */
2440 int ext4_orphan_add(handle_t *handle, struct inode *inode)
2441 {
2442         struct super_block *sb = inode->i_sb;
2443         struct ext4_iloc iloc;
2444         int err = 0, rc;
2445
2446         if (!EXT4_SB(sb)->s_journal)
2447                 return 0;
2448
2449         mutex_lock(&EXT4_SB(sb)->s_orphan_lock);
2450         if (!list_empty(&EXT4_I(inode)->i_orphan))
2451                 goto out_unlock;
2452
2453         /*
2454          * Orphan handling is only valid for files with data blocks
2455          * being truncated, or files being unlinked. Note that we either
2456          * hold i_mutex, or the inode can not be referenced from outside,
2457          * so i_nlink should not be bumped due to race
2458          */
2459         J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
2460                   S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
2461
2462         BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
2463         err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
2464         if (err)
2465                 goto out_unlock;
2466
2467         err = ext4_reserve_inode_write(handle, inode, &iloc);
2468         if (err)
2469                 goto out_unlock;
2470         /*
2471          * Due to previous errors inode may be already a part of on-disk
2472          * orphan list. If so skip on-disk list modification.
2473          */
2474         if (NEXT_ORPHAN(inode) && NEXT_ORPHAN(inode) <=
2475                 (le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count)))
2476                         goto mem_insert;
2477
2478         /* Insert this inode at the head of the on-disk orphan list... */
2479         NEXT_ORPHAN(inode) = le32_to_cpu(EXT4_SB(sb)->s_es->s_last_orphan);
2480         EXT4_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
2481         err = ext4_handle_dirty_super(handle, sb);
2482         rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
2483         if (!err)
2484                 err = rc;
2485
2486         /* Only add to the head of the in-memory list if all the
2487          * previous operations succeeded.  If the orphan_add is going to
2488          * fail (possibly taking the journal offline), we can't risk
2489          * leaving the inode on the orphan list: stray orphan-list
2490          * entries can cause panics at unmount time.
2491          *
2492          * This is safe: on error we're going to ignore the orphan list
2493          * anyway on the next recovery. */
2494 mem_insert:
2495         if (!err)
2496                 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
2497
2498         jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
2499         jbd_debug(4, "orphan inode %lu will point to %d\n",
2500                         inode->i_ino, NEXT_ORPHAN(inode));
2501 out_unlock:
2502         mutex_unlock(&EXT4_SB(sb)->s_orphan_lock);
2503         ext4_std_error(inode->i_sb, err);
2504         return err;
2505 }
2506
2507 /*
2508  * ext4_orphan_del() removes an unlinked or truncated inode from the list
2509  * of such inodes stored on disk, because it is finally being cleaned up.
2510  */
2511 int ext4_orphan_del(handle_t *handle, struct inode *inode)
2512 {
2513         struct list_head *prev;
2514         struct ext4_inode_info *ei = EXT4_I(inode);
2515         struct ext4_sb_info *sbi;
2516         __u32 ino_next;
2517         struct ext4_iloc iloc;
2518         int err = 0;
2519
2520         if (!EXT4_SB(inode->i_sb)->s_journal)
2521                 return 0;
2522
2523         mutex_lock(&EXT4_SB(inode->i_sb)->s_orphan_lock);
2524         if (list_empty(&ei->i_orphan))
2525                 goto out;
2526
2527         ino_next = NEXT_ORPHAN(inode);
2528         prev = ei->i_orphan.prev;
2529         sbi = EXT4_SB(inode->i_sb);
2530
2531         jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
2532
2533         list_del_init(&ei->i_orphan);
2534
2535         /* If we're on an error path, we may not have a valid
2536          * transaction handle with which to update the orphan list on
2537          * disk, but we still need to remove the inode from the linked
2538          * list in memory. */
2539         if (!handle)
2540                 goto out;
2541
2542         err = ext4_reserve_inode_write(handle, inode, &iloc);
2543         if (err)
2544                 goto out_err;
2545
2546         if (prev == &sbi->s_orphan) {
2547                 jbd_debug(4, "superblock will point to %u\n", ino_next);
2548                 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
2549                 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
2550                 if (err)
2551                         goto out_brelse;
2552                 sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
2553                 err = ext4_handle_dirty_super(handle, inode->i_sb);
2554         } else {
2555                 struct ext4_iloc iloc2;
2556                 struct inode *i_prev =
2557                         &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
2558
2559                 jbd_debug(4, "orphan inode %lu will point to %u\n",
2560                           i_prev->i_ino, ino_next);
2561                 err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
2562                 if (err)
2563                         goto out_brelse;
2564                 NEXT_ORPHAN(i_prev) = ino_next;
2565                 err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
2566         }
2567         if (err)
2568                 goto out_brelse;
2569         NEXT_ORPHAN(inode) = 0;
2570         err = ext4_mark_iloc_dirty(handle, inode, &iloc);
2571
2572 out_err:
2573         ext4_std_error(inode->i_sb, err);
2574 out:
2575         mutex_unlock(&EXT4_SB(inode->i_sb)->s_orphan_lock);
2576         return err;
2577
2578 out_brelse:
2579         brelse(iloc.bh);
2580         goto out_err;
2581 }
2582
2583 static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
2584 {
2585         int retval;
2586         struct inode *inode;
2587         struct buffer_head *bh;
2588         struct ext4_dir_entry_2 *de;
2589         handle_t *handle;
2590
2591         /* Initialize quotas before so that eventual writes go in
2592          * separate transaction */
2593         dquot_initialize(dir);
2594         dquot_initialize(dentry->d_inode);
2595
2596         handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
2597         if (IS_ERR(handle))
2598                 return PTR_ERR(handle);
2599
2600         retval = -ENOENT;
2601         bh = ext4_find_entry(dir, &dentry->d_name, &de);
2602         if (!bh)
2603                 goto end_rmdir;
2604
2605         if (IS_DIRSYNC(dir))
2606                 ext4_handle_sync(handle);
2607
2608         inode = dentry->d_inode;
2609
2610         retval = -EIO;
2611         if (le32_to_cpu(de->inode) != inode->i_ino)
2612                 goto end_rmdir;
2613
2614         retval = -ENOTEMPTY;
2615         if (!empty_dir(inode))
2616                 goto end_rmdir;
2617
2618         retval = ext4_delete_entry(handle, dir, de, bh);
2619         if (retval)
2620                 goto end_rmdir;
2621         if (!EXT4_DIR_LINK_EMPTY(inode))
2622                 ext4_warning(inode->i_sb,
2623                              "empty directory has too many links (%d)",
2624                              inode->i_nlink);
2625         inode->i_version++;
2626         clear_nlink(inode);
2627         /* There's no need to set i_disksize: the fact that i_nlink is
2628          * zero will ensure that the right thing happens during any
2629          * recovery. */
2630         inode->i_size = 0;
2631         ext4_orphan_add(handle, inode);
2632         inode->i_ctime = dir->i_ctime = dir->i_mtime = ext4_current_time(inode);
2633         ext4_mark_inode_dirty(handle, inode);
2634         ext4_dec_count(handle, dir);
2635         ext4_update_dx_flag(dir);
2636         ext4_mark_inode_dirty(handle, dir);
2637
2638 end_rmdir:
2639         ext4_journal_stop(handle);
2640         brelse(bh);
2641         return retval;
2642 }
2643
2644 static int ext4_unlink(struct inode *dir, struct dentry *dentry)
2645 {
2646         int retval;
2647         struct inode *inode;
2648         struct buffer_head *bh;
2649         struct ext4_dir_entry_2 *de;
2650         handle_t *handle;
2651
2652         trace_ext4_unlink_enter(dir, dentry);
2653         /* Initialize quotas before so that eventual writes go
2654          * in separate transaction */
2655         dquot_initialize(dir);
2656         dquot_initialize(dentry->d_inode);
2657
2658         handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
2659         if (IS_ERR(handle))
2660                 return PTR_ERR(handle);
2661
2662         if (IS_DIRSYNC(dir))
2663                 ext4_handle_sync(handle);
2664
2665         retval = -ENOENT;
2666         bh = ext4_find_entry(dir, &dentry->d_name, &de);
2667         if (!bh)
2668                 goto end_unlink;
2669
2670         inode = dentry->d_inode;
2671
2672         retval = -EIO;
2673         if (le32_to_cpu(de->inode) != inode->i_ino)
2674                 goto end_unlink;
2675
2676         if (!inode->i_nlink) {
2677                 ext4_warning(inode->i_sb,
2678                              "Deleting nonexistent file (%lu), %d",
2679                              inode->i_ino, inode->i_nlink);
2680                 set_nlink(inode, 1);
2681         }
2682         retval = ext4_delete_entry(handle, dir, de, bh);
2683         if (retval)
2684                 goto end_unlink;
2685         dir->i_ctime = dir->i_mtime = ext4_current_time(dir);
2686         ext4_update_dx_flag(dir);
2687         ext4_mark_inode_dirty(handle, dir);
2688         drop_nlink(inode);
2689         if (!inode->i_nlink)
2690                 ext4_orphan_add(handle, inode);
2691         inode->i_ctime = ext4_current_time(inode);
2692         ext4_mark_inode_dirty(handle, inode);
2693         retval = 0;
2694
2695 end_unlink:
2696         ext4_journal_stop(handle);
2697         brelse(bh);
2698         trace_ext4_unlink_exit(dentry, retval);
2699         return retval;
2700 }
2701
2702 static int ext4_symlink(struct inode *dir,
2703                         struct dentry *dentry, const char *symname)
2704 {
2705         handle_t *handle;
2706         struct inode *inode;
2707         int l, err, retries = 0;
2708         int credits;
2709
2710         l = strlen(symname)+1;
2711         if (l > dir->i_sb->s_blocksize)
2712                 return -ENAMETOOLONG;
2713
2714         dquot_initialize(dir);
2715
2716         if (l > EXT4_N_BLOCKS * 4) {
2717                 /*
2718                  * For non-fast symlinks, we just allocate inode and put it on
2719                  * orphan list in the first transaction => we need bitmap,
2720                  * group descriptor, sb, inode block, quota blocks, and
2721                  * possibly selinux xattr blocks.
2722                  */
2723                 credits = 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
2724                           EXT4_XATTR_TRANS_BLOCKS;
2725         } else {
2726                 /*
2727                  * Fast symlink. We have to add entry to directory
2728                  * (EXT4_DATA_TRANS_BLOCKS + EXT4_INDEX_EXTRA_TRANS_BLOCKS),
2729                  * allocate new inode (bitmap, group descriptor, inode block,
2730                  * quota blocks, sb is already counted in previous macros).
2731                  */
2732                 credits = EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2733                           EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
2734                           EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb);
2735         }
2736 retry:
2737         handle = ext4_journal_start(dir, credits);
2738         if (IS_ERR(handle))
2739                 return PTR_ERR(handle);
2740
2741         if (IS_DIRSYNC(dir))
2742                 ext4_handle_sync(handle);
2743
2744         inode = ext4_new_inode(handle, dir, S_IFLNK|S_IRWXUGO,
2745                                &dentry->d_name, 0, NULL);
2746         err = PTR_ERR(inode);
2747         if (IS_ERR(inode))
2748                 goto out_stop;
2749
2750         if (l > EXT4_N_BLOCKS * 4) {
2751                 inode->i_op = &ext4_symlink_inode_operations;
2752                 ext4_set_aops(inode);
2753                 /*
2754                  * We cannot call page_symlink() with transaction started
2755                  * because it calls into ext4_write_begin() which can wait
2756                  * for transaction commit if we are running out of space
2757                  * and thus we deadlock. So we have to stop transaction now
2758                  * and restart it when symlink contents is written.
2759                  * 
2760                  * To keep fs consistent in case of crash, we have to put inode
2761                  * to orphan list in the mean time.
2762                  */
2763                 drop_nlink(inode);
2764                 err = ext4_orphan_add(handle, inode);
2765                 ext4_journal_stop(handle);
2766                 if (err)
2767                         goto err_drop_inode;
2768                 err = __page_symlink(inode, symname, l, 1);
2769                 if (err)
2770                         goto err_drop_inode;
2771                 /*
2772                  * Now inode is being linked into dir (EXT4_DATA_TRANS_BLOCKS
2773                  * + EXT4_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
2774                  */
2775                 handle = ext4_journal_start(dir,
2776                                 EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2777                                 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 1);
2778                 if (IS_ERR(handle)) {
2779                         err = PTR_ERR(handle);
2780                         goto err_drop_inode;
2781                 }
2782                 set_nlink(inode, 1);
2783                 err = ext4_orphan_del(handle, inode);
2784                 if (err) {
2785                         ext4_journal_stop(handle);
2786                         clear_nlink(inode);
2787                         goto err_drop_inode;
2788                 }
2789         } else {
2790                 /* clear the extent format for fast symlink */
2791                 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
2792                 inode->i_op = &ext4_fast_symlink_inode_operations;
2793                 memcpy((char *)&EXT4_I(inode)->i_data, symname, l);
2794                 inode->i_size = l-1;
2795         }
2796         EXT4_I(inode)->i_disksize = inode->i_size;
2797         err = ext4_add_nondir(handle, dentry, inode);
2798 out_stop:
2799         ext4_journal_stop(handle);
2800         if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2801                 goto retry;
2802         return err;
2803 err_drop_inode:
2804         unlock_new_inode(inode);
2805         iput(inode);
2806         return err;
2807 }
2808
2809 static int ext4_link(struct dentry *old_dentry,
2810                      struct inode *dir, struct dentry *dentry)
2811 {
2812         handle_t *handle;
2813         struct inode *inode = old_dentry->d_inode;
2814         int err, retries = 0;
2815
2816         if (inode->i_nlink >= EXT4_LINK_MAX)
2817                 return -EMLINK;
2818
2819         dquot_initialize(dir);
2820
2821 retry:
2822         handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2823                                         EXT4_INDEX_EXTRA_TRANS_BLOCKS);
2824         if (IS_ERR(handle))
2825                 return PTR_ERR(handle);
2826
2827         if (IS_DIRSYNC(dir))
2828                 ext4_handle_sync(handle);
2829
2830         inode->i_ctime = ext4_current_time(inode);
2831         ext4_inc_count(handle, inode);
2832         ihold(inode);
2833
2834         err = ext4_add_entry(handle, dentry, inode);
2835         if (!err) {
2836                 ext4_mark_inode_dirty(handle, inode);
2837                 d_instantiate(dentry, inode);
2838         } else {
2839                 drop_nlink(inode);
2840                 iput(inode);
2841         }
2842         ext4_journal_stop(handle);
2843         if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2844                 goto retry;
2845         return err;
2846 }
2847
2848 #define PARENT_INO(buffer, size) \
2849         (ext4_next_entry((struct ext4_dir_entry_2 *)(buffer), size)->inode)
2850
2851 /*
2852  * Anybody can rename anything with this: the permission checks are left to the
2853  * higher-level routines.
2854  */
2855 static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
2856                        struct inode *new_dir, struct dentry *new_dentry)
2857 {
2858         handle_t *handle;
2859         struct inode *old_inode, *new_inode;
2860         struct buffer_head *old_bh, *new_bh, *dir_bh;
2861         struct ext4_dir_entry_2 *old_de, *new_de;
2862         int retval, force_da_alloc = 0;
2863
2864         dquot_initialize(old_dir);
2865         dquot_initialize(new_dir);
2866
2867         old_bh = new_bh = dir_bh = NULL;
2868
2869         /* Initialize quotas before so that eventual writes go
2870          * in separate transaction */
2871         if (new_dentry->d_inode)
2872                 dquot_initialize(new_dentry->d_inode);
2873         handle = ext4_journal_start(old_dir, 2 *
2874                                         EXT4_DATA_TRANS_BLOCKS(old_dir->i_sb) +
2875                                         EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
2876         if (IS_ERR(handle))
2877                 return PTR_ERR(handle);
2878
2879         if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
2880                 ext4_handle_sync(handle);
2881
2882         old_bh = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de);
2883         /*
2884          *  Check for inode number is _not_ due to possible IO errors.
2885          *  We might rmdir the source, keep it as pwd of some process
2886          *  and merrily kill the link to whatever was created under the
2887          *  same name. Goodbye sticky bit ;-<
2888          */
2889         old_inode = old_dentry->d_inode;
2890         retval = -ENOENT;
2891         if (!old_bh || le32_to_cpu(old_de->inode) != old_inode->i_ino)
2892                 goto end_rename;
2893
2894         new_inode = new_dentry->d_inode;
2895         new_bh = ext4_find_entry(new_dir, &new_dentry->d_name, &new_de);
2896         if (new_bh) {
2897                 if (!new_inode) {
2898                         brelse(new_bh);
2899                         new_bh = NULL;
2900                 }
2901         }
2902         if (S_ISDIR(old_inode->i_mode)) {
2903                 if (new_inode) {
2904                         retval = -ENOTEMPTY;
2905                         if (!empty_dir(new_inode))
2906                                 goto end_rename;
2907                 }
2908                 retval = -EIO;
2909                 if (!(dir_bh = ext4_bread(handle, old_inode, 0, 0, &retval))) {
2910                         if (!retval) {
2911                                 retval = -EIO;
2912                                 ext4_error(old_inode->i_sb,
2913                                            "Directory hole detected on inode %lu\n",
2914                                            old_inode->i_ino);
2915                         }
2916                         goto end_rename;
2917                 }
2918                 if (!buffer_verified(dir_bh) &&
2919                     !ext4_dirent_csum_verify(old_inode,
2920                                 (struct ext4_dir_entry *)dir_bh->b_data))
2921                         goto end_rename;
2922                 set_buffer_verified(dir_bh);
2923                 if (le32_to_cpu(PARENT_INO(dir_bh->b_data,
2924                                 old_dir->i_sb->s_blocksize)) != old_dir->i_ino)
2925                         goto end_rename;
2926                 retval = -EMLINK;
2927                 if (!new_inode && new_dir != old_dir &&
2928                     EXT4_DIR_LINK_MAX(new_dir))
2929                         goto end_rename;
2930                 BUFFER_TRACE(dir_bh, "get_write_access");
2931                 retval = ext4_journal_get_write_access(handle, dir_bh);
2932                 if (retval)
2933                         goto end_rename;
2934         }
2935         if (!new_bh) {
2936                 retval = ext4_add_entry(handle, new_dentry, old_inode);
2937                 if (retval)
2938                         goto end_rename;
2939         } else {
2940                 BUFFER_TRACE(new_bh, "get write access");
2941                 retval = ext4_journal_get_write_access(handle, new_bh);
2942                 if (retval)
2943                         goto end_rename;
2944                 new_de->inode = cpu_to_le32(old_inode->i_ino);
2945                 if (EXT4_HAS_INCOMPAT_FEATURE(new_dir->i_sb,
2946                                               EXT4_FEATURE_INCOMPAT_FILETYPE))
2947                         new_de->file_type = old_de->file_type;
2948                 new_dir->i_version++;
2949                 new_dir->i_ctime = new_dir->i_mtime =
2950                                         ext4_current_time(new_dir);
2951                 ext4_mark_inode_dirty(handle, new_dir);
2952                 BUFFER_TRACE(new_bh, "call ext4_handle_dirty_metadata");
2953                 retval = ext4_handle_dirty_dirent_node(handle, new_dir, new_bh);
2954                 if (unlikely(retval)) {
2955                         ext4_std_error(new_dir->i_sb, retval);
2956                         goto end_rename;
2957                 }
2958                 brelse(new_bh);
2959                 new_bh = NULL;
2960         }
2961
2962         /*
2963          * Like most other Unix systems, set the ctime for inodes on a
2964          * rename.
2965          */
2966         old_inode->i_ctime = ext4_current_time(old_inode);
2967         ext4_mark_inode_dirty(handle, old_inode);
2968
2969         /*
2970          * ok, that's it
2971          */
2972         if (le32_to_cpu(old_de->inode) != old_inode->i_ino ||
2973             old_de->name_len != old_dentry->d_name.len ||
2974             strncmp(old_de->name, old_dentry->d_name.name, old_de->name_len) ||
2975             (retval = ext4_delete_entry(handle, old_dir,
2976                                         old_de, old_bh)) == -ENOENT) {
2977                 /* old_de could have moved from under us during htree split, so
2978                  * make sure that we are deleting the right entry.  We might
2979                  * also be pointing to a stale entry in the unused part of
2980                  * old_bh so just checking inum and the name isn't enough. */
2981                 struct buffer_head *old_bh2;
2982                 struct ext4_dir_entry_2 *old_de2;
2983
2984                 old_bh2 = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de2);
2985                 if (old_bh2) {
2986                         retval = ext4_delete_entry(handle, old_dir,
2987                                                    old_de2, old_bh2);
2988                         brelse(old_bh2);
2989                 }
2990         }
2991         if (retval) {
2992                 ext4_warning(old_dir->i_sb,
2993                                 "Deleting old file (%lu), %d, error=%d",
2994                                 old_dir->i_ino, old_dir->i_nlink, retval);
2995         }
2996
2997         if (new_inode) {
2998                 ext4_dec_count(handle, new_inode);
2999                 new_inode->i_ctime = ext4_current_time(new_inode);
3000         }
3001         old_dir->i_ctime = old_dir->i_mtime = ext4_current_time(old_dir);
3002         ext4_update_dx_flag(old_dir);
3003         if (dir_bh) {
3004                 PARENT_INO(dir_bh->b_data, new_dir->i_sb->s_blocksize) =
3005                                                 cpu_to_le32(new_dir->i_ino);
3006                 BUFFER_TRACE(dir_bh, "call ext4_handle_dirty_metadata");
3007                 if (is_dx(old_inode)) {
3008                         retval = ext4_handle_dirty_dx_node(handle,
3009                                                            old_inode,
3010                                                            dir_bh);
3011                 } else {
3012                         retval = ext4_handle_dirty_dirent_node(handle,
3013                                                                old_inode,
3014                                                                dir_bh);
3015                 }
3016                 if (retval) {
3017                         ext4_std_error(old_dir->i_sb, retval);
3018                         goto end_rename;
3019                 }
3020                 ext4_dec_count(handle, old_dir);
3021                 if (new_inode) {
3022                         /* checked empty_dir above, can't have another parent,
3023                          * ext4_dec_count() won't work for many-linked dirs */
3024                         clear_nlink(new_inode);
3025                 } else {
3026                         ext4_inc_count(handle, new_dir);
3027                         ext4_update_dx_flag(new_dir);
3028                         ext4_mark_inode_dirty(handle, new_dir);
3029                 }
3030         }
3031         ext4_mark_inode_dirty(handle, old_dir);
3032         if (new_inode) {
3033                 ext4_mark_inode_dirty(handle, new_inode);
3034                 if (!new_inode->i_nlink)
3035                         ext4_orphan_add(handle, new_inode);
3036                 if (!test_opt(new_dir->i_sb, NO_AUTO_DA_ALLOC))
3037                         force_da_alloc = 1;
3038         }
3039         retval = 0;
3040
3041 end_rename:
3042         brelse(dir_bh);
3043         brelse(old_bh);
3044         brelse(new_bh);
3045         ext4_journal_stop(handle);
3046         if (retval == 0 && force_da_alloc)
3047                 ext4_alloc_da_blocks(old_inode);
3048         return retval;
3049 }
3050
3051 /*
3052  * directories can handle most operations...
3053  */
3054 const struct inode_operations ext4_dir_inode_operations = {
3055         .create         = ext4_create,
3056         .lookup         = ext4_lookup,
3057         .link           = ext4_link,
3058         .unlink         = ext4_unlink,
3059         .symlink        = ext4_symlink,
3060         .mkdir          = ext4_mkdir,
3061         .rmdir          = ext4_rmdir,
3062         .mknod          = ext4_mknod,
3063         .rename         = ext4_rename,
3064         .setattr        = ext4_setattr,
3065 #ifdef CONFIG_EXT4_FS_XATTR
3066         .setxattr       = generic_setxattr,
3067         .getxattr       = generic_getxattr,
3068         .listxattr      = ext4_listxattr,
3069         .removexattr    = generic_removexattr,
3070 #endif
3071         .get_acl        = ext4_get_acl,
3072         .fiemap         = ext4_fiemap,
3073 };
3074
3075 const struct inode_operations ext4_special_inode_operations = {
3076         .setattr        = ext4_setattr,
3077 #ifdef CONFIG_EXT4_FS_XATTR
3078         .setxattr       = generic_setxattr,
3079         .getxattr       = generic_getxattr,
3080         .listxattr      = ext4_listxattr,
3081         .removexattr    = generic_removexattr,
3082 #endif
3083         .get_acl        = ext4_get_acl,
3084 };