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