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