]> git.karo-electronics.de Git - karo-tx-linux.git/blob - fs/ext4/namei.c
edb9f10c14559cbc667c42ef3b1460d5991a3888
[karo-tx-linux.git] / fs / ext4 / namei.c
1 /*
2  *  linux/fs/ext4/namei.c
3  *
4  * Copyright (C) 1992, 1993, 1994, 1995
5  * Remy Card (card@masi.ibp.fr)
6  * Laboratoire MASI - Institut Blaise Pascal
7  * Universite Pierre et Marie Curie (Paris VI)
8  *
9  *  from
10  *
11  *  linux/fs/minix/namei.c
12  *
13  *  Copyright (C) 1991, 1992  Linus Torvalds
14  *
15  *  Big-endian to little-endian byte-swapping/bitmaps by
16  *        David S. Miller (davem@caip.rutgers.edu), 1995
17  *  Directory entry file type support and forward compatibility hooks
18  *      for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
19  *  Hash Tree Directory indexing (c)
20  *      Daniel Phillips, 2001
21  *  Hash Tree Directory indexing porting
22  *      Christopher Li, 2002
23  *  Hash Tree Directory indexing cleanup
24  *      Theodore Ts'o, 2002
25  */
26
27 #include <linux/fs.h>
28 #include <linux/pagemap.h>
29 #include <linux/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 struct ext4_dir_entry_2 *ext4_init_dot_dotdot(struct inode *inode,
2234                           struct ext4_dir_entry_2 *de,
2235                           int blocksize, int csum_size,
2236                           unsigned int parent_ino, int dotdot_real_len)
2237 {
2238         de->inode = cpu_to_le32(inode->i_ino);
2239         de->name_len = 1;
2240         de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len),
2241                                            blocksize);
2242         strcpy(de->name, ".");
2243         ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2244
2245         de = ext4_next_entry(de, blocksize);
2246         de->inode = cpu_to_le32(parent_ino);
2247         de->name_len = 2;
2248         if (!dotdot_real_len)
2249                 de->rec_len = ext4_rec_len_to_disk(blocksize -
2250                                         (csum_size + EXT4_DIR_REC_LEN(1)),
2251                                         blocksize);
2252         else
2253                 de->rec_len = ext4_rec_len_to_disk(
2254                                 EXT4_DIR_REC_LEN(de->name_len), blocksize);
2255         strcpy(de->name, "..");
2256         ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2257
2258         return ext4_next_entry(de, blocksize);
2259 }
2260
2261 static int ext4_init_new_dir(handle_t *handle, struct inode *dir,
2262                              struct inode *inode)
2263 {
2264         struct buffer_head *dir_block = NULL;
2265         struct ext4_dir_entry_2 *de;
2266         struct ext4_dir_entry_tail *t;
2267         unsigned int blocksize = dir->i_sb->s_blocksize;
2268         int csum_size = 0;
2269         int err;
2270
2271         if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
2272                                        EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
2273                 csum_size = sizeof(struct ext4_dir_entry_tail);
2274
2275         inode->i_size = EXT4_I(inode)->i_disksize = blocksize;
2276         dir_block = ext4_bread(handle, inode, 0, 1, &err);
2277         if (!(dir_block = ext4_bread(handle, inode, 0, 1, &err))) {
2278                 if (!err) {
2279                         err = -EIO;
2280                         ext4_error(inode->i_sb,
2281                                    "Directory hole detected on inode %lu\n",
2282                                    inode->i_ino);
2283                 }
2284                 goto out;
2285         }
2286         BUFFER_TRACE(dir_block, "get_write_access");
2287         err = ext4_journal_get_write_access(handle, dir_block);
2288         if (err)
2289                 goto out;
2290         de = (struct ext4_dir_entry_2 *)dir_block->b_data;
2291         ext4_init_dot_dotdot(inode, de, blocksize, csum_size, dir->i_ino, 0);
2292         set_nlink(inode, 2);
2293         if (csum_size) {
2294                 t = EXT4_DIRENT_TAIL(dir_block->b_data, blocksize);
2295                 initialize_dirent_tail(t, blocksize);
2296         }
2297
2298         BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata");
2299         err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
2300         if (err)
2301                 goto out;
2302         set_buffer_verified(dir_block);
2303 out:
2304         brelse(dir_block);
2305         return err;
2306 }
2307
2308 static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
2309 {
2310         handle_t *handle;
2311         struct inode *inode;
2312         int err, retries = 0;
2313
2314         if (EXT4_DIR_LINK_MAX(dir))
2315                 return -EMLINK;
2316
2317         dquot_initialize(dir);
2318
2319 retry:
2320         handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2321                                         EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
2322                                         EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
2323         if (IS_ERR(handle))
2324                 return PTR_ERR(handle);
2325
2326         if (IS_DIRSYNC(dir))
2327                 ext4_handle_sync(handle);
2328
2329         inode = ext4_new_inode(handle, dir, S_IFDIR | mode,
2330                                &dentry->d_name, 0, NULL);
2331         err = PTR_ERR(inode);
2332         if (IS_ERR(inode))
2333                 goto out_stop;
2334
2335         inode->i_op = &ext4_dir_inode_operations;
2336         inode->i_fop = &ext4_dir_operations;
2337         err = ext4_init_new_dir(handle, dir, inode);
2338         if (err)
2339                 goto out_clear_inode;
2340         err = ext4_mark_inode_dirty(handle, inode);
2341         if (!err)
2342                 err = ext4_add_entry(handle, dentry, inode);
2343         if (err) {
2344 out_clear_inode:
2345                 clear_nlink(inode);
2346                 unlock_new_inode(inode);
2347                 ext4_mark_inode_dirty(handle, inode);
2348                 iput(inode);
2349                 goto out_stop;
2350         }
2351         ext4_inc_count(handle, dir);
2352         ext4_update_dx_flag(dir);
2353         err = ext4_mark_inode_dirty(handle, dir);
2354         if (err)
2355                 goto out_clear_inode;
2356         unlock_new_inode(inode);
2357         d_instantiate(dentry, inode);
2358 out_stop:
2359         ext4_journal_stop(handle);
2360         if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2361                 goto retry;
2362         return err;
2363 }
2364
2365 /*
2366  * routine to check that the specified directory is empty (for rmdir)
2367  */
2368 static int empty_dir(struct inode *inode)
2369 {
2370         unsigned int offset;
2371         struct buffer_head *bh;
2372         struct ext4_dir_entry_2 *de, *de1;
2373         struct super_block *sb;
2374         int err = 0;
2375
2376         sb = inode->i_sb;
2377         if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2) ||
2378             !(bh = ext4_bread(NULL, inode, 0, 0, &err))) {
2379                 if (err)
2380                         EXT4_ERROR_INODE(inode,
2381                                 "error %d reading directory lblock 0", err);
2382                 else
2383                         ext4_warning(inode->i_sb,
2384                                      "bad directory (dir #%lu) - no data block",
2385                                      inode->i_ino);
2386                 return 1;
2387         }
2388         if (!buffer_verified(bh) &&
2389             !ext4_dirent_csum_verify(inode,
2390                         (struct ext4_dir_entry *)bh->b_data)) {
2391                 EXT4_ERROR_INODE(inode, "checksum error reading directory "
2392                                  "lblock 0");
2393                 return -EIO;
2394         }
2395         set_buffer_verified(bh);
2396         de = (struct ext4_dir_entry_2 *) bh->b_data;
2397         de1 = ext4_next_entry(de, sb->s_blocksize);
2398         if (le32_to_cpu(de->inode) != inode->i_ino ||
2399                         !le32_to_cpu(de1->inode) ||
2400                         strcmp(".", de->name) ||
2401                         strcmp("..", de1->name)) {
2402                 ext4_warning(inode->i_sb,
2403                              "bad directory (dir #%lu) - no `.' or `..'",
2404                              inode->i_ino);
2405                 brelse(bh);
2406                 return 1;
2407         }
2408         offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) +
2409                  ext4_rec_len_from_disk(de1->rec_len, sb->s_blocksize);
2410         de = ext4_next_entry(de1, sb->s_blocksize);
2411         while (offset < inode->i_size) {
2412                 if (!bh ||
2413                     (void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
2414                         unsigned int lblock;
2415                         err = 0;
2416                         brelse(bh);
2417                         lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb);
2418                         bh = ext4_bread(NULL, inode, lblock, 0, &err);
2419                         if (!bh) {
2420                                 if (err)
2421                                         EXT4_ERROR_INODE(inode,
2422                                                 "error %d reading directory "
2423                                                 "lblock %u", err, lblock);
2424                                 else
2425                                         ext4_warning(inode->i_sb,
2426                                                 "bad directory (dir #%lu) - no data block",
2427                                                 inode->i_ino);
2428
2429                                 offset += sb->s_blocksize;
2430                                 continue;
2431                         }
2432                         if (!buffer_verified(bh) &&
2433                             !ext4_dirent_csum_verify(inode,
2434                                         (struct ext4_dir_entry *)bh->b_data)) {
2435                                 EXT4_ERROR_INODE(inode, "checksum error "
2436                                                  "reading directory lblock 0");
2437                                 return -EIO;
2438                         }
2439                         set_buffer_verified(bh);
2440                         de = (struct ext4_dir_entry_2 *) bh->b_data;
2441                 }
2442                 if (ext4_check_dir_entry(inode, NULL, de, bh, offset)) {
2443                         de = (struct ext4_dir_entry_2 *)(bh->b_data +
2444                                                          sb->s_blocksize);
2445                         offset = (offset | (sb->s_blocksize - 1)) + 1;
2446                         continue;
2447                 }
2448                 if (le32_to_cpu(de->inode)) {
2449                         brelse(bh);
2450                         return 0;
2451                 }
2452                 offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
2453                 de = ext4_next_entry(de, sb->s_blocksize);
2454         }
2455         brelse(bh);
2456         return 1;
2457 }
2458
2459 /* ext4_orphan_add() links an unlinked or truncated inode into a list of
2460  * such inodes, starting at the superblock, in case we crash before the
2461  * file is closed/deleted, or in case the inode truncate spans multiple
2462  * transactions and the last transaction is not recovered after a crash.
2463  *
2464  * At filesystem recovery time, we walk this list deleting unlinked
2465  * inodes and truncating linked inodes in ext4_orphan_cleanup().
2466  */
2467 int ext4_orphan_add(handle_t *handle, struct inode *inode)
2468 {
2469         struct super_block *sb = inode->i_sb;
2470         struct ext4_iloc iloc;
2471         int err = 0, rc;
2472
2473         if (!EXT4_SB(sb)->s_journal)
2474                 return 0;
2475
2476         mutex_lock(&EXT4_SB(sb)->s_orphan_lock);
2477         if (!list_empty(&EXT4_I(inode)->i_orphan))
2478                 goto out_unlock;
2479
2480         /*
2481          * Orphan handling is only valid for files with data blocks
2482          * being truncated, or files being unlinked. Note that we either
2483          * hold i_mutex, or the inode can not be referenced from outside,
2484          * so i_nlink should not be bumped due to race
2485          */
2486         J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
2487                   S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
2488
2489         BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
2490         err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
2491         if (err)
2492                 goto out_unlock;
2493
2494         err = ext4_reserve_inode_write(handle, inode, &iloc);
2495         if (err)
2496                 goto out_unlock;
2497         /*
2498          * Due to previous errors inode may be already a part of on-disk
2499          * orphan list. If so skip on-disk list modification.
2500          */
2501         if (NEXT_ORPHAN(inode) && NEXT_ORPHAN(inode) <=
2502                 (le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count)))
2503                         goto mem_insert;
2504
2505         /* Insert this inode at the head of the on-disk orphan list... */
2506         NEXT_ORPHAN(inode) = le32_to_cpu(EXT4_SB(sb)->s_es->s_last_orphan);
2507         EXT4_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
2508         err = ext4_handle_dirty_super(handle, sb);
2509         rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
2510         if (!err)
2511                 err = rc;
2512
2513         /* Only add to the head of the in-memory list if all the
2514          * previous operations succeeded.  If the orphan_add is going to
2515          * fail (possibly taking the journal offline), we can't risk
2516          * leaving the inode on the orphan list: stray orphan-list
2517          * entries can cause panics at unmount time.
2518          *
2519          * This is safe: on error we're going to ignore the orphan list
2520          * anyway on the next recovery. */
2521 mem_insert:
2522         if (!err)
2523                 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
2524
2525         jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
2526         jbd_debug(4, "orphan inode %lu will point to %d\n",
2527                         inode->i_ino, NEXT_ORPHAN(inode));
2528 out_unlock:
2529         mutex_unlock(&EXT4_SB(sb)->s_orphan_lock);
2530         ext4_std_error(inode->i_sb, err);
2531         return err;
2532 }
2533
2534 /*
2535  * ext4_orphan_del() removes an unlinked or truncated inode from the list
2536  * of such inodes stored on disk, because it is finally being cleaned up.
2537  */
2538 int ext4_orphan_del(handle_t *handle, struct inode *inode)
2539 {
2540         struct list_head *prev;
2541         struct ext4_inode_info *ei = EXT4_I(inode);
2542         struct ext4_sb_info *sbi;
2543         __u32 ino_next;
2544         struct ext4_iloc iloc;
2545         int err = 0;
2546
2547         if (!EXT4_SB(inode->i_sb)->s_journal)
2548                 return 0;
2549
2550         mutex_lock(&EXT4_SB(inode->i_sb)->s_orphan_lock);
2551         if (list_empty(&ei->i_orphan))
2552                 goto out;
2553
2554         ino_next = NEXT_ORPHAN(inode);
2555         prev = ei->i_orphan.prev;
2556         sbi = EXT4_SB(inode->i_sb);
2557
2558         jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
2559
2560         list_del_init(&ei->i_orphan);
2561
2562         /* If we're on an error path, we may not have a valid
2563          * transaction handle with which to update the orphan list on
2564          * disk, but we still need to remove the inode from the linked
2565          * list in memory. */
2566         if (!handle)
2567                 goto out;
2568
2569         err = ext4_reserve_inode_write(handle, inode, &iloc);
2570         if (err)
2571                 goto out_err;
2572
2573         if (prev == &sbi->s_orphan) {
2574                 jbd_debug(4, "superblock will point to %u\n", ino_next);
2575                 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
2576                 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
2577                 if (err)
2578                         goto out_brelse;
2579                 sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
2580                 err = ext4_handle_dirty_super(handle, inode->i_sb);
2581         } else {
2582                 struct ext4_iloc iloc2;
2583                 struct inode *i_prev =
2584                         &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
2585
2586                 jbd_debug(4, "orphan inode %lu will point to %u\n",
2587                           i_prev->i_ino, ino_next);
2588                 err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
2589                 if (err)
2590                         goto out_brelse;
2591                 NEXT_ORPHAN(i_prev) = ino_next;
2592                 err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
2593         }
2594         if (err)
2595                 goto out_brelse;
2596         NEXT_ORPHAN(inode) = 0;
2597         err = ext4_mark_iloc_dirty(handle, inode, &iloc);
2598
2599 out_err:
2600         ext4_std_error(inode->i_sb, err);
2601 out:
2602         mutex_unlock(&EXT4_SB(inode->i_sb)->s_orphan_lock);
2603         return err;
2604
2605 out_brelse:
2606         brelse(iloc.bh);
2607         goto out_err;
2608 }
2609
2610 static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
2611 {
2612         int retval;
2613         struct inode *inode;
2614         struct buffer_head *bh;
2615         struct ext4_dir_entry_2 *de;
2616         handle_t *handle;
2617
2618         /* Initialize quotas before so that eventual writes go in
2619          * separate transaction */
2620         dquot_initialize(dir);
2621         dquot_initialize(dentry->d_inode);
2622
2623         handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
2624         if (IS_ERR(handle))
2625                 return PTR_ERR(handle);
2626
2627         retval = -ENOENT;
2628         bh = ext4_find_entry(dir, &dentry->d_name, &de);
2629         if (!bh)
2630                 goto end_rmdir;
2631
2632         if (IS_DIRSYNC(dir))
2633                 ext4_handle_sync(handle);
2634
2635         inode = dentry->d_inode;
2636
2637         retval = -EIO;
2638         if (le32_to_cpu(de->inode) != inode->i_ino)
2639                 goto end_rmdir;
2640
2641         retval = -ENOTEMPTY;
2642         if (!empty_dir(inode))
2643                 goto end_rmdir;
2644
2645         retval = ext4_delete_entry(handle, dir, de, bh);
2646         if (retval)
2647                 goto end_rmdir;
2648         if (!EXT4_DIR_LINK_EMPTY(inode))
2649                 ext4_warning(inode->i_sb,
2650                              "empty directory has too many links (%d)",
2651                              inode->i_nlink);
2652         inode->i_version++;
2653         clear_nlink(inode);
2654         /* There's no need to set i_disksize: the fact that i_nlink is
2655          * zero will ensure that the right thing happens during any
2656          * recovery. */
2657         inode->i_size = 0;
2658         ext4_orphan_add(handle, inode);
2659         inode->i_ctime = dir->i_ctime = dir->i_mtime = ext4_current_time(inode);
2660         ext4_mark_inode_dirty(handle, inode);
2661         ext4_dec_count(handle, dir);
2662         ext4_update_dx_flag(dir);
2663         ext4_mark_inode_dirty(handle, dir);
2664
2665 end_rmdir:
2666         ext4_journal_stop(handle);
2667         brelse(bh);
2668         return retval;
2669 }
2670
2671 static int ext4_unlink(struct inode *dir, struct dentry *dentry)
2672 {
2673         int retval;
2674         struct inode *inode;
2675         struct buffer_head *bh;
2676         struct ext4_dir_entry_2 *de;
2677         handle_t *handle;
2678
2679         trace_ext4_unlink_enter(dir, dentry);
2680         /* Initialize quotas before so that eventual writes go
2681          * in separate transaction */
2682         dquot_initialize(dir);
2683         dquot_initialize(dentry->d_inode);
2684
2685         handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
2686         if (IS_ERR(handle))
2687                 return PTR_ERR(handle);
2688
2689         if (IS_DIRSYNC(dir))
2690                 ext4_handle_sync(handle);
2691
2692         retval = -ENOENT;
2693         bh = ext4_find_entry(dir, &dentry->d_name, &de);
2694         if (!bh)
2695                 goto end_unlink;
2696
2697         inode = dentry->d_inode;
2698
2699         retval = -EIO;
2700         if (le32_to_cpu(de->inode) != inode->i_ino)
2701                 goto end_unlink;
2702
2703         if (!inode->i_nlink) {
2704                 ext4_warning(inode->i_sb,
2705                              "Deleting nonexistent file (%lu), %d",
2706                              inode->i_ino, inode->i_nlink);
2707                 set_nlink(inode, 1);
2708         }
2709         retval = ext4_delete_entry(handle, dir, de, bh);
2710         if (retval)
2711                 goto end_unlink;
2712         dir->i_ctime = dir->i_mtime = ext4_current_time(dir);
2713         ext4_update_dx_flag(dir);
2714         ext4_mark_inode_dirty(handle, dir);
2715         drop_nlink(inode);
2716         if (!inode->i_nlink)
2717                 ext4_orphan_add(handle, inode);
2718         inode->i_ctime = ext4_current_time(inode);
2719         ext4_mark_inode_dirty(handle, inode);
2720         retval = 0;
2721
2722 end_unlink:
2723         ext4_journal_stop(handle);
2724         brelse(bh);
2725         trace_ext4_unlink_exit(dentry, retval);
2726         return retval;
2727 }
2728
2729 static int ext4_symlink(struct inode *dir,
2730                         struct dentry *dentry, const char *symname)
2731 {
2732         handle_t *handle;
2733         struct inode *inode;
2734         int l, err, retries = 0;
2735         int credits;
2736
2737         l = strlen(symname)+1;
2738         if (l > dir->i_sb->s_blocksize)
2739                 return -ENAMETOOLONG;
2740
2741         dquot_initialize(dir);
2742
2743         if (l > EXT4_N_BLOCKS * 4) {
2744                 /*
2745                  * For non-fast symlinks, we just allocate inode and put it on
2746                  * orphan list in the first transaction => we need bitmap,
2747                  * group descriptor, sb, inode block, quota blocks, and
2748                  * possibly selinux xattr blocks.
2749                  */
2750                 credits = 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
2751                           EXT4_XATTR_TRANS_BLOCKS;
2752         } else {
2753                 /*
2754                  * Fast symlink. We have to add entry to directory
2755                  * (EXT4_DATA_TRANS_BLOCKS + EXT4_INDEX_EXTRA_TRANS_BLOCKS),
2756                  * allocate new inode (bitmap, group descriptor, inode block,
2757                  * quota blocks, sb is already counted in previous macros).
2758                  */
2759                 credits = EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2760                           EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
2761                           EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb);
2762         }
2763 retry:
2764         handle = ext4_journal_start(dir, credits);
2765         if (IS_ERR(handle))
2766                 return PTR_ERR(handle);
2767
2768         if (IS_DIRSYNC(dir))
2769                 ext4_handle_sync(handle);
2770
2771         inode = ext4_new_inode(handle, dir, S_IFLNK|S_IRWXUGO,
2772                                &dentry->d_name, 0, NULL);
2773         err = PTR_ERR(inode);
2774         if (IS_ERR(inode))
2775                 goto out_stop;
2776
2777         if (l > EXT4_N_BLOCKS * 4) {
2778                 inode->i_op = &ext4_symlink_inode_operations;
2779                 ext4_set_aops(inode);
2780                 /*
2781                  * We cannot call page_symlink() with transaction started
2782                  * because it calls into ext4_write_begin() which can wait
2783                  * for transaction commit if we are running out of space
2784                  * and thus we deadlock. So we have to stop transaction now
2785                  * and restart it when symlink contents is written.
2786                  * 
2787                  * To keep fs consistent in case of crash, we have to put inode
2788                  * to orphan list in the mean time.
2789                  */
2790                 drop_nlink(inode);
2791                 err = ext4_orphan_add(handle, inode);
2792                 ext4_journal_stop(handle);
2793                 if (err)
2794                         goto err_drop_inode;
2795                 err = __page_symlink(inode, symname, l, 1);
2796                 if (err)
2797                         goto err_drop_inode;
2798                 /*
2799                  * Now inode is being linked into dir (EXT4_DATA_TRANS_BLOCKS
2800                  * + EXT4_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
2801                  */
2802                 handle = ext4_journal_start(dir,
2803                                 EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2804                                 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 1);
2805                 if (IS_ERR(handle)) {
2806                         err = PTR_ERR(handle);
2807                         goto err_drop_inode;
2808                 }
2809                 set_nlink(inode, 1);
2810                 err = ext4_orphan_del(handle, inode);
2811                 if (err) {
2812                         ext4_journal_stop(handle);
2813                         clear_nlink(inode);
2814                         goto err_drop_inode;
2815                 }
2816         } else {
2817                 /* clear the extent format for fast symlink */
2818                 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
2819                 inode->i_op = &ext4_fast_symlink_inode_operations;
2820                 memcpy((char *)&EXT4_I(inode)->i_data, symname, l);
2821                 inode->i_size = l-1;
2822         }
2823         EXT4_I(inode)->i_disksize = inode->i_size;
2824         err = ext4_add_nondir(handle, dentry, inode);
2825 out_stop:
2826         ext4_journal_stop(handle);
2827         if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2828                 goto retry;
2829         return err;
2830 err_drop_inode:
2831         unlock_new_inode(inode);
2832         iput(inode);
2833         return err;
2834 }
2835
2836 static int ext4_link(struct dentry *old_dentry,
2837                      struct inode *dir, struct dentry *dentry)
2838 {
2839         handle_t *handle;
2840         struct inode *inode = old_dentry->d_inode;
2841         int err, retries = 0;
2842
2843         if (inode->i_nlink >= EXT4_LINK_MAX)
2844                 return -EMLINK;
2845
2846         dquot_initialize(dir);
2847
2848 retry:
2849         handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2850                                         EXT4_INDEX_EXTRA_TRANS_BLOCKS);
2851         if (IS_ERR(handle))
2852                 return PTR_ERR(handle);
2853
2854         if (IS_DIRSYNC(dir))
2855                 ext4_handle_sync(handle);
2856
2857         inode->i_ctime = ext4_current_time(inode);
2858         ext4_inc_count(handle, inode);
2859         ihold(inode);
2860
2861         err = ext4_add_entry(handle, dentry, inode);
2862         if (!err) {
2863                 ext4_mark_inode_dirty(handle, inode);
2864                 d_instantiate(dentry, inode);
2865         } else {
2866                 drop_nlink(inode);
2867                 iput(inode);
2868         }
2869         ext4_journal_stop(handle);
2870         if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2871                 goto retry;
2872         return err;
2873 }
2874
2875 #define PARENT_INO(buffer, size) \
2876         (ext4_next_entry((struct ext4_dir_entry_2 *)(buffer), size)->inode)
2877
2878 /*
2879  * Anybody can rename anything with this: the permission checks are left to the
2880  * higher-level routines.
2881  */
2882 static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
2883                        struct inode *new_dir, struct dentry *new_dentry)
2884 {
2885         handle_t *handle;
2886         struct inode *old_inode, *new_inode;
2887         struct buffer_head *old_bh, *new_bh, *dir_bh;
2888         struct ext4_dir_entry_2 *old_de, *new_de;
2889         int retval, force_da_alloc = 0;
2890
2891         dquot_initialize(old_dir);
2892         dquot_initialize(new_dir);
2893
2894         old_bh = new_bh = dir_bh = NULL;
2895
2896         /* Initialize quotas before so that eventual writes go
2897          * in separate transaction */
2898         if (new_dentry->d_inode)
2899                 dquot_initialize(new_dentry->d_inode);
2900         handle = ext4_journal_start(old_dir, 2 *
2901                                         EXT4_DATA_TRANS_BLOCKS(old_dir->i_sb) +
2902                                         EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
2903         if (IS_ERR(handle))
2904                 return PTR_ERR(handle);
2905
2906         if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
2907                 ext4_handle_sync(handle);
2908
2909         old_bh = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de);
2910         /*
2911          *  Check for inode number is _not_ due to possible IO errors.
2912          *  We might rmdir the source, keep it as pwd of some process
2913          *  and merrily kill the link to whatever was created under the
2914          *  same name. Goodbye sticky bit ;-<
2915          */
2916         old_inode = old_dentry->d_inode;
2917         retval = -ENOENT;
2918         if (!old_bh || le32_to_cpu(old_de->inode) != old_inode->i_ino)
2919                 goto end_rename;
2920
2921         new_inode = new_dentry->d_inode;
2922         new_bh = ext4_find_entry(new_dir, &new_dentry->d_name, &new_de);
2923         if (new_bh) {
2924                 if (!new_inode) {
2925                         brelse(new_bh);
2926                         new_bh = NULL;
2927                 }
2928         }
2929         if (S_ISDIR(old_inode->i_mode)) {
2930                 if (new_inode) {
2931                         retval = -ENOTEMPTY;
2932                         if (!empty_dir(new_inode))
2933                                 goto end_rename;
2934                 }
2935                 retval = -EIO;
2936                 if (!(dir_bh = ext4_bread(handle, old_inode, 0, 0, &retval))) {
2937                         if (!retval) {
2938                                 retval = -EIO;
2939                                 ext4_error(old_inode->i_sb,
2940                                            "Directory hole detected on inode %lu\n",
2941                                            old_inode->i_ino);
2942                         }
2943                         goto end_rename;
2944                 }
2945                 if (!buffer_verified(dir_bh) &&
2946                     !ext4_dirent_csum_verify(old_inode,
2947                                 (struct ext4_dir_entry *)dir_bh->b_data))
2948                         goto end_rename;
2949                 set_buffer_verified(dir_bh);
2950                 if (le32_to_cpu(PARENT_INO(dir_bh->b_data,
2951                                 old_dir->i_sb->s_blocksize)) != old_dir->i_ino)
2952                         goto end_rename;
2953                 retval = -EMLINK;
2954                 if (!new_inode && new_dir != old_dir &&
2955                     EXT4_DIR_LINK_MAX(new_dir))
2956                         goto end_rename;
2957                 BUFFER_TRACE(dir_bh, "get_write_access");
2958                 retval = ext4_journal_get_write_access(handle, dir_bh);
2959                 if (retval)
2960                         goto end_rename;
2961         }
2962         if (!new_bh) {
2963                 retval = ext4_add_entry(handle, new_dentry, old_inode);
2964                 if (retval)
2965                         goto end_rename;
2966         } else {
2967                 BUFFER_TRACE(new_bh, "get write access");
2968                 retval = ext4_journal_get_write_access(handle, new_bh);
2969                 if (retval)
2970                         goto end_rename;
2971                 new_de->inode = cpu_to_le32(old_inode->i_ino);
2972                 if (EXT4_HAS_INCOMPAT_FEATURE(new_dir->i_sb,
2973                                               EXT4_FEATURE_INCOMPAT_FILETYPE))
2974                         new_de->file_type = old_de->file_type;
2975                 new_dir->i_version++;
2976                 new_dir->i_ctime = new_dir->i_mtime =
2977                                         ext4_current_time(new_dir);
2978                 ext4_mark_inode_dirty(handle, new_dir);
2979                 BUFFER_TRACE(new_bh, "call ext4_handle_dirty_metadata");
2980                 retval = ext4_handle_dirty_dirent_node(handle, new_dir, new_bh);
2981                 if (unlikely(retval)) {
2982                         ext4_std_error(new_dir->i_sb, retval);
2983                         goto end_rename;
2984                 }
2985                 brelse(new_bh);
2986                 new_bh = NULL;
2987         }
2988
2989         /*
2990          * Like most other Unix systems, set the ctime for inodes on a
2991          * rename.
2992          */
2993         old_inode->i_ctime = ext4_current_time(old_inode);
2994         ext4_mark_inode_dirty(handle, old_inode);
2995
2996         /*
2997          * ok, that's it
2998          */
2999         if (le32_to_cpu(old_de->inode) != old_inode->i_ino ||
3000             old_de->name_len != old_dentry->d_name.len ||
3001             strncmp(old_de->name, old_dentry->d_name.name, old_de->name_len) ||
3002             (retval = ext4_delete_entry(handle, old_dir,
3003                                         old_de, old_bh)) == -ENOENT) {
3004                 /* old_de could have moved from under us during htree split, so
3005                  * make sure that we are deleting the right entry.  We might
3006                  * also be pointing to a stale entry in the unused part of
3007                  * old_bh so just checking inum and the name isn't enough. */
3008                 struct buffer_head *old_bh2;
3009                 struct ext4_dir_entry_2 *old_de2;
3010
3011                 old_bh2 = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de2);
3012                 if (old_bh2) {
3013                         retval = ext4_delete_entry(handle, old_dir,
3014                                                    old_de2, old_bh2);
3015                         brelse(old_bh2);
3016                 }
3017         }
3018         if (retval) {
3019                 ext4_warning(old_dir->i_sb,
3020                                 "Deleting old file (%lu), %d, error=%d",
3021                                 old_dir->i_ino, old_dir->i_nlink, retval);
3022         }
3023
3024         if (new_inode) {
3025                 ext4_dec_count(handle, new_inode);
3026                 new_inode->i_ctime = ext4_current_time(new_inode);
3027         }
3028         old_dir->i_ctime = old_dir->i_mtime = ext4_current_time(old_dir);
3029         ext4_update_dx_flag(old_dir);
3030         if (dir_bh) {
3031                 PARENT_INO(dir_bh->b_data, new_dir->i_sb->s_blocksize) =
3032                                                 cpu_to_le32(new_dir->i_ino);
3033                 BUFFER_TRACE(dir_bh, "call ext4_handle_dirty_metadata");
3034                 if (is_dx(old_inode)) {
3035                         retval = ext4_handle_dirty_dx_node(handle,
3036                                                            old_inode,
3037                                                            dir_bh);
3038                 } else {
3039                         retval = ext4_handle_dirty_dirent_node(handle,
3040                                                                old_inode,
3041                                                                dir_bh);
3042                 }
3043                 if (retval) {
3044                         ext4_std_error(old_dir->i_sb, retval);
3045                         goto end_rename;
3046                 }
3047                 ext4_dec_count(handle, old_dir);
3048                 if (new_inode) {
3049                         /* checked empty_dir above, can't have another parent,
3050                          * ext4_dec_count() won't work for many-linked dirs */
3051                         clear_nlink(new_inode);
3052                 } else {
3053                         ext4_inc_count(handle, new_dir);
3054                         ext4_update_dx_flag(new_dir);
3055                         ext4_mark_inode_dirty(handle, new_dir);
3056                 }
3057         }
3058         ext4_mark_inode_dirty(handle, old_dir);
3059         if (new_inode) {
3060                 ext4_mark_inode_dirty(handle, new_inode);
3061                 if (!new_inode->i_nlink)
3062                         ext4_orphan_add(handle, new_inode);
3063                 if (!test_opt(new_dir->i_sb, NO_AUTO_DA_ALLOC))
3064                         force_da_alloc = 1;
3065         }
3066         retval = 0;
3067
3068 end_rename:
3069         brelse(dir_bh);
3070         brelse(old_bh);
3071         brelse(new_bh);
3072         ext4_journal_stop(handle);
3073         if (retval == 0 && force_da_alloc)
3074                 ext4_alloc_da_blocks(old_inode);
3075         return retval;
3076 }
3077
3078 /*
3079  * directories can handle most operations...
3080  */
3081 const struct inode_operations ext4_dir_inode_operations = {
3082         .create         = ext4_create,
3083         .lookup         = ext4_lookup,
3084         .link           = ext4_link,
3085         .unlink         = ext4_unlink,
3086         .symlink        = ext4_symlink,
3087         .mkdir          = ext4_mkdir,
3088         .rmdir          = ext4_rmdir,
3089         .mknod          = ext4_mknod,
3090         .rename         = ext4_rename,
3091         .setattr        = ext4_setattr,
3092 #ifdef CONFIG_EXT4_FS_XATTR
3093         .setxattr       = generic_setxattr,
3094         .getxattr       = generic_getxattr,
3095         .listxattr      = ext4_listxattr,
3096         .removexattr    = generic_removexattr,
3097 #endif
3098         .get_acl        = ext4_get_acl,
3099         .fiemap         = ext4_fiemap,
3100 };
3101
3102 const struct inode_operations ext4_special_inode_operations = {
3103         .setattr        = ext4_setattr,
3104 #ifdef CONFIG_EXT4_FS_XATTR
3105         .setxattr       = generic_setxattr,
3106         .getxattr       = generic_getxattr,
3107         .listxattr      = ext4_listxattr,
3108         .removexattr    = generic_removexattr,
3109 #endif
3110         .get_acl        = ext4_get_acl,
3111 };