allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / fs / ext4 / namei.c
blobc3f9a4b4a3207063a14b0ce3e0f004208e66917b
1 /*
2 * linux/fs/ext4/namei.c
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)
9 * from
11 * linux/fs/minix/namei.c
13 * Copyright (C) 1991, 1992 Linus Torvalds
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
27 #include <linux/fs.h>
28 #include <linux/pagemap.h>
29 #include <linux/jbd2.h>
30 #include <linux/time.h>
31 #include <linux/ext4_fs.h>
32 #include <linux/ext4_jbd2.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>
40 #include "namei.h"
41 #include "xattr.h"
42 #include "acl.h"
45 * define how far ahead to read directories while searching them.
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))
52 static struct buffer_head *ext4_append(handle_t *handle,
53 struct inode *inode,
54 u32 *block, int *err)
56 struct buffer_head *bh;
58 *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
60 if ((bh = ext4_bread(handle, inode, *block, 1, err))) {
61 inode->i_size += inode->i_sb->s_blocksize;
62 EXT4_I(inode)->i_disksize = inode->i_size;
63 ext4_journal_get_write_access(handle,bh);
65 return bh;
68 #ifndef assert
69 #define assert(test) J_ASSERT(test)
70 #endif
72 #ifndef swap
73 #define swap(x, y) do { typeof(x) z = x; x = y; y = z; } while (0)
74 #endif
76 #ifdef DX_DEBUG
77 #define dxtrace(command) command
78 #else
79 #define dxtrace(command)
80 #endif
82 struct fake_dirent
84 __le32 inode;
85 __le16 rec_len;
86 u8 name_len;
87 u8 file_type;
90 struct dx_countlimit
92 __le16 limit;
93 __le16 count;
96 struct dx_entry
98 __le32 hash;
99 __le32 block;
103 * dx_root_info is laid out so that if it should somehow get overlaid by a
104 * dirent the two low bits of the hash version will be zero. Therefore, the
105 * hash version mod 4 should never be 0. Sincerely, the paranoia department.
108 struct dx_root
110 struct fake_dirent dot;
111 char dot_name[4];
112 struct fake_dirent dotdot;
113 char dotdot_name[4];
114 struct dx_root_info
116 __le32 reserved_zero;
117 u8 hash_version;
118 u8 info_length; /* 8 */
119 u8 indirect_levels;
120 u8 unused_flags;
122 info;
123 struct dx_entry entries[0];
126 struct dx_node
128 struct fake_dirent fake;
129 struct dx_entry entries[0];
133 struct dx_frame
135 struct buffer_head *bh;
136 struct dx_entry *entries;
137 struct dx_entry *at;
140 struct dx_map_entry
142 u32 hash;
143 u16 offs;
144 u16 size;
147 #ifdef CONFIG_EXT4_INDEX
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 dentry *dentry,
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 ext4_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 ext4_dir_entry_2 *dx_move_dirents (char *from, char *to,
168 struct dx_map_entry *offsets, int count);
169 static struct ext4_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 ext4_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 * ext4_dx_find_entry(struct dentry *dentry,
176 struct ext4_dir_entry_2 **res_dir, int *err);
177 static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
178 struct inode *inode);
181 * Future: use high four bits of block for coalesce-on-delete flags
182 * Mask them off for now.
185 static inline unsigned dx_get_block (struct dx_entry *entry)
187 return le32_to_cpu(entry->block) & 0x00ffffff;
190 static inline void dx_set_block (struct dx_entry *entry, unsigned value)
192 entry->block = cpu_to_le32(value);
195 static inline unsigned dx_get_hash (struct dx_entry *entry)
197 return le32_to_cpu(entry->hash);
200 static inline void dx_set_hash (struct dx_entry *entry, unsigned value)
202 entry->hash = cpu_to_le32(value);
205 static inline unsigned dx_get_count (struct dx_entry *entries)
207 return le16_to_cpu(((struct dx_countlimit *) entries)->count);
210 static inline unsigned dx_get_limit (struct dx_entry *entries)
212 return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
215 static inline void dx_set_count (struct dx_entry *entries, unsigned value)
217 ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
220 static inline void dx_set_limit (struct dx_entry *entries, unsigned value)
222 ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
225 static inline unsigned dx_root_limit (struct inode *dir, unsigned infosize)
227 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(1) -
228 EXT4_DIR_REC_LEN(2) - infosize;
229 return 0? 20: entry_space / sizeof(struct dx_entry);
232 static inline unsigned dx_node_limit (struct inode *dir)
234 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0);
235 return 0? 22: entry_space / sizeof(struct dx_entry);
239 * Debug
241 #ifdef DX_DEBUG
242 static void dx_show_index (char * label, struct dx_entry *entries)
244 int i, n = dx_get_count (entries);
245 printk("%s index ", label);
246 for (i = 0; i < n; i++) {
247 printk("%x->%u ", i? dx_get_hash(entries + i) :
248 0, dx_get_block(entries + i));
250 printk("\n");
253 struct stats
255 unsigned names;
256 unsigned space;
257 unsigned bcount;
260 static struct stats dx_show_leaf(struct dx_hash_info *hinfo, struct ext4_dir_entry_2 *de,
261 int size, int show_names)
263 unsigned names = 0, space = 0;
264 char *base = (char *) de;
265 struct dx_hash_info h = *hinfo;
267 printk("names: ");
268 while ((char *) de < base + size)
270 if (de->inode)
272 if (show_names)
274 int len = de->name_len;
275 char *name = de->name;
276 while (len--) printk("%c", *name++);
277 ext4fs_dirhash(de->name, de->name_len, &h);
278 printk(":%x.%u ", h.hash,
279 ((char *) de - base));
281 space += EXT4_DIR_REC_LEN(de->name_len);
282 names++;
284 de = (struct ext4_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
286 printk("(%i)\n", names);
287 return (struct stats) { names, space, 1 };
290 struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
291 struct dx_entry *entries, int levels)
293 unsigned blocksize = dir->i_sb->s_blocksize;
294 unsigned count = dx_get_count (entries), names = 0, space = 0, i;
295 unsigned bcount = 0;
296 struct buffer_head *bh;
297 int err;
298 printk("%i indexed blocks...\n", count);
299 for (i = 0; i < count; i++, entries++)
301 u32 block = dx_get_block(entries), hash = i? dx_get_hash(entries): 0;
302 u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
303 struct stats stats;
304 printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
305 if (!(bh = ext4_bread (NULL,dir, block, 0,&err))) continue;
306 stats = levels?
307 dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
308 dx_show_leaf(hinfo, (struct ext4_dir_entry_2 *) bh->b_data, blocksize, 0);
309 names += stats.names;
310 space += stats.space;
311 bcount += stats.bcount;
312 brelse (bh);
314 if (bcount)
315 printk("%snames %u, fullness %u (%u%%)\n", levels?"":" ",
316 names, space/bcount,(space/bcount)*100/blocksize);
317 return (struct stats) { names, space, bcount};
319 #endif /* DX_DEBUG */
322 * Probe for a directory leaf block to search.
324 * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
325 * error in the directory index, and the caller should fall back to
326 * searching the directory normally. The callers of dx_probe **MUST**
327 * check for this error code, and make sure it never gets reflected
328 * back to userspace.
330 static struct dx_frame *
331 dx_probe(struct dentry *dentry, struct inode *dir,
332 struct dx_hash_info *hinfo, struct dx_frame *frame_in, int *err)
334 unsigned count, indirect;
335 struct dx_entry *at, *entries, *p, *q, *m;
336 struct dx_root *root;
337 struct buffer_head *bh;
338 struct dx_frame *frame = frame_in;
339 u32 hash;
341 frame->bh = NULL;
342 if (dentry)
343 dir = dentry->d_parent->d_inode;
344 if (!(bh = ext4_bread (NULL,dir, 0, 0, err)))
345 goto fail;
346 root = (struct dx_root *) bh->b_data;
347 if (root->info.hash_version != DX_HASH_TEA &&
348 root->info.hash_version != DX_HASH_HALF_MD4 &&
349 root->info.hash_version != DX_HASH_LEGACY) {
350 ext4_warning(dir->i_sb, __FUNCTION__,
351 "Unrecognised inode hash code %d",
352 root->info.hash_version);
353 brelse(bh);
354 *err = ERR_BAD_DX_DIR;
355 goto fail;
357 hinfo->hash_version = root->info.hash_version;
358 hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
359 if (dentry)
360 ext4fs_dirhash(dentry->d_name.name, dentry->d_name.len, hinfo);
361 hash = hinfo->hash;
363 if (root->info.unused_flags & 1) {
364 ext4_warning(dir->i_sb, __FUNCTION__,
365 "Unimplemented inode hash flags: %#06x",
366 root->info.unused_flags);
367 brelse(bh);
368 *err = ERR_BAD_DX_DIR;
369 goto fail;
372 if ((indirect = root->info.indirect_levels) > 1) {
373 ext4_warning(dir->i_sb, __FUNCTION__,
374 "Unimplemented inode hash depth: %#06x",
375 root->info.indirect_levels);
376 brelse(bh);
377 *err = ERR_BAD_DX_DIR;
378 goto fail;
381 entries = (struct dx_entry *) (((char *)&root->info) +
382 root->info.info_length);
384 if (dx_get_limit(entries) != dx_root_limit(dir,
385 root->info.info_length)) {
386 ext4_warning(dir->i_sb, __FUNCTION__,
387 "dx entry: limit != root limit");
388 brelse(bh);
389 *err = ERR_BAD_DX_DIR;
390 goto fail;
393 dxtrace (printk("Look up %x", hash));
394 while (1)
396 count = dx_get_count(entries);
397 if (!count || count > dx_get_limit(entries)) {
398 ext4_warning(dir->i_sb, __FUNCTION__,
399 "dx entry: no count or count > limit");
400 brelse(bh);
401 *err = ERR_BAD_DX_DIR;
402 goto fail2;
405 p = entries + 1;
406 q = entries + count - 1;
407 while (p <= q)
409 m = p + (q - p)/2;
410 dxtrace(printk("."));
411 if (dx_get_hash(m) > hash)
412 q = m - 1;
413 else
414 p = m + 1;
417 if (0) // linear search cross check
419 unsigned n = count - 1;
420 at = entries;
421 while (n--)
423 dxtrace(printk(","));
424 if (dx_get_hash(++at) > hash)
426 at--;
427 break;
430 assert (at == p - 1);
433 at = p - 1;
434 dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
435 frame->bh = bh;
436 frame->entries = entries;
437 frame->at = at;
438 if (!indirect--) return frame;
439 if (!(bh = ext4_bread (NULL,dir, dx_get_block(at), 0, err)))
440 goto fail2;
441 at = entries = ((struct dx_node *) bh->b_data)->entries;
442 if (dx_get_limit(entries) != dx_node_limit (dir)) {
443 ext4_warning(dir->i_sb, __FUNCTION__,
444 "dx entry: limit != node limit");
445 brelse(bh);
446 *err = ERR_BAD_DX_DIR;
447 goto fail2;
449 frame++;
450 frame->bh = NULL;
452 fail2:
453 while (frame >= frame_in) {
454 brelse(frame->bh);
455 frame--;
457 fail:
458 if (*err == ERR_BAD_DX_DIR)
459 ext4_warning(dir->i_sb, __FUNCTION__,
460 "Corrupt dir inode %ld, running e2fsck is "
461 "recommended.", dir->i_ino);
462 return NULL;
465 static void dx_release (struct dx_frame *frames)
467 if (frames[0].bh == NULL)
468 return;
470 if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
471 brelse(frames[1].bh);
472 brelse(frames[0].bh);
476 * This function increments the frame pointer to search the next leaf
477 * block, and reads in the necessary intervening nodes if the search
478 * should be necessary. Whether or not the search is necessary is
479 * controlled by the hash parameter. If the hash value is even, then
480 * the search is only continued if the next block starts with that
481 * hash value. This is used if we are searching for a specific file.
483 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
485 * This function returns 1 if the caller should continue to search,
486 * or 0 if it should not. If there is an error reading one of the
487 * index blocks, it will a negative error code.
489 * If start_hash is non-null, it will be filled in with the starting
490 * hash of the next page.
492 static int ext4_htree_next_block(struct inode *dir, __u32 hash,
493 struct dx_frame *frame,
494 struct dx_frame *frames,
495 __u32 *start_hash)
497 struct dx_frame *p;
498 struct buffer_head *bh;
499 int err, num_frames = 0;
500 __u32 bhash;
502 p = frame;
504 * Find the next leaf page by incrementing the frame pointer.
505 * If we run out of entries in the interior node, loop around and
506 * increment pointer in the parent node. When we break out of
507 * this loop, num_frames indicates the number of interior
508 * nodes need to be read.
510 while (1) {
511 if (++(p->at) < p->entries + dx_get_count(p->entries))
512 break;
513 if (p == frames)
514 return 0;
515 num_frames++;
516 p--;
520 * If the hash is 1, then continue only if the next page has a
521 * continuation hash of any value. This is used for readdir
522 * handling. Otherwise, check to see if the hash matches the
523 * desired contiuation hash. If it doesn't, return since
524 * there's no point to read in the successive index pages.
526 bhash = dx_get_hash(p->at);
527 if (start_hash)
528 *start_hash = bhash;
529 if ((hash & 1) == 0) {
530 if ((bhash & ~1) != hash)
531 return 0;
534 * If the hash is HASH_NB_ALWAYS, we always go to the next
535 * block so no check is necessary
537 while (num_frames--) {
538 if (!(bh = ext4_bread(NULL, dir, dx_get_block(p->at),
539 0, &err)))
540 return err; /* Failure */
541 p++;
542 brelse (p->bh);
543 p->bh = bh;
544 p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
546 return 1;
551 * p is at least 6 bytes before the end of page
553 static inline struct ext4_dir_entry_2 *ext4_next_entry(struct ext4_dir_entry_2 *p)
555 return (struct ext4_dir_entry_2 *)((char*)p + le16_to_cpu(p->rec_len));
559 * This function fills a red-black tree with information from a
560 * directory block. It returns the number directory entries loaded
561 * into the tree. If there is an error it is returned in err.
563 static int htree_dirblock_to_tree(struct file *dir_file,
564 struct inode *dir, int block,
565 struct dx_hash_info *hinfo,
566 __u32 start_hash, __u32 start_minor_hash)
568 struct buffer_head *bh;
569 struct ext4_dir_entry_2 *de, *top;
570 int err, count = 0;
572 dxtrace(printk("In htree dirblock_to_tree: block %d\n", block));
573 if (!(bh = ext4_bread (NULL, dir, block, 0, &err)))
574 return err;
576 de = (struct ext4_dir_entry_2 *) bh->b_data;
577 top = (struct ext4_dir_entry_2 *) ((char *) de +
578 dir->i_sb->s_blocksize -
579 EXT4_DIR_REC_LEN(0));
580 for (; de < top; de = ext4_next_entry(de)) {
581 if (!ext4_check_dir_entry("htree_dirblock_to_tree", dir, de, bh,
582 (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
583 +((char *)de - bh->b_data))) {
584 /* On error, skip the f_pos to the next block. */
585 dir_file->f_pos = (dir_file->f_pos |
586 (dir->i_sb->s_blocksize - 1)) + 1;
587 brelse (bh);
588 return count;
590 ext4fs_dirhash(de->name, de->name_len, hinfo);
591 if ((hinfo->hash < start_hash) ||
592 ((hinfo->hash == start_hash) &&
593 (hinfo->minor_hash < start_minor_hash)))
594 continue;
595 if (de->inode == 0)
596 continue;
597 if ((err = ext4_htree_store_dirent(dir_file,
598 hinfo->hash, hinfo->minor_hash, de)) != 0) {
599 brelse(bh);
600 return err;
602 count++;
604 brelse(bh);
605 return count;
610 * This function fills a red-black tree with information from a
611 * directory. We start scanning the directory in hash order, starting
612 * at start_hash and start_minor_hash.
614 * This function returns the number of entries inserted into the tree,
615 * or a negative error code.
617 int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
618 __u32 start_minor_hash, __u32 *next_hash)
620 struct dx_hash_info hinfo;
621 struct ext4_dir_entry_2 *de;
622 struct dx_frame frames[2], *frame;
623 struct inode *dir;
624 int block, err;
625 int count = 0;
626 int ret;
627 __u32 hashval;
629 dxtrace(printk("In htree_fill_tree, start hash: %x:%x\n", start_hash,
630 start_minor_hash));
631 dir = dir_file->f_path.dentry->d_inode;
632 if (!(EXT4_I(dir)->i_flags & EXT4_INDEX_FL)) {
633 hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
634 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
635 count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
636 start_hash, start_minor_hash);
637 *next_hash = ~0;
638 return count;
640 hinfo.hash = start_hash;
641 hinfo.minor_hash = 0;
642 frame = dx_probe(NULL, dir_file->f_path.dentry->d_inode, &hinfo, frames, &err);
643 if (!frame)
644 return err;
646 /* Add '.' and '..' from the htree header */
647 if (!start_hash && !start_minor_hash) {
648 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
649 if ((err = ext4_htree_store_dirent(dir_file, 0, 0, de)) != 0)
650 goto errout;
651 count++;
653 if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
654 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
655 de = ext4_next_entry(de);
656 if ((err = ext4_htree_store_dirent(dir_file, 2, 0, de)) != 0)
657 goto errout;
658 count++;
661 while (1) {
662 block = dx_get_block(frame->at);
663 ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
664 start_hash, start_minor_hash);
665 if (ret < 0) {
666 err = ret;
667 goto errout;
669 count += ret;
670 hashval = ~0;
671 ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
672 frame, frames, &hashval);
673 *next_hash = hashval;
674 if (ret < 0) {
675 err = ret;
676 goto errout;
679 * Stop if: (a) there are no more entries, or
680 * (b) we have inserted at least one entry and the
681 * next hash value is not a continuation
683 if ((ret == 0) ||
684 (count && ((hashval & 1) == 0)))
685 break;
687 dx_release(frames);
688 dxtrace(printk("Fill tree: returned %d entries, next hash: %x\n",
689 count, *next_hash));
690 return count;
691 errout:
692 dx_release(frames);
693 return (err);
698 * Directory block splitting, compacting
702 * Create map of hash values, offsets, and sizes, stored at end of block.
703 * Returns number of entries mapped.
705 static int dx_make_map (struct ext4_dir_entry_2 *de, int size,
706 struct dx_hash_info *hinfo, struct dx_map_entry *map_tail)
708 int count = 0;
709 char *base = (char *) de;
710 struct dx_hash_info h = *hinfo;
712 while ((char *) de < base + size)
714 if (de->name_len && de->inode) {
715 ext4fs_dirhash(de->name, de->name_len, &h);
716 map_tail--;
717 map_tail->hash = h.hash;
718 map_tail->offs = (u16) ((char *) de - base);
719 map_tail->size = le16_to_cpu(de->rec_len);
720 count++;
721 cond_resched();
723 /* XXX: do we need to check rec_len == 0 case? -Chris */
724 de = (struct ext4_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
726 return count;
729 /* Sort map by hash value */
730 static void dx_sort_map (struct dx_map_entry *map, unsigned count)
732 struct dx_map_entry *p, *q, *top = map + count - 1;
733 int more;
734 /* Combsort until bubble sort doesn't suck */
735 while (count > 2) {
736 count = count*10/13;
737 if (count - 9 < 2) /* 9, 10 -> 11 */
738 count = 11;
739 for (p = top, q = p - count; q >= map; p--, q--)
740 if (p->hash < q->hash)
741 swap(*p, *q);
743 /* Garden variety bubble sort */
744 do {
745 more = 0;
746 q = top;
747 while (q-- > map) {
748 if (q[1].hash >= q[0].hash)
749 continue;
750 swap(*(q+1), *q);
751 more = 1;
753 } while(more);
756 static void dx_insert_block(struct dx_frame *frame, u32 hash, u32 block)
758 struct dx_entry *entries = frame->entries;
759 struct dx_entry *old = frame->at, *new = old + 1;
760 int count = dx_get_count(entries);
762 assert(count < dx_get_limit(entries));
763 assert(old < entries + count);
764 memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
765 dx_set_hash(new, hash);
766 dx_set_block(new, block);
767 dx_set_count(entries, count + 1);
769 #endif
772 static void ext4_update_dx_flag(struct inode *inode)
774 if (!EXT4_HAS_COMPAT_FEATURE(inode->i_sb,
775 EXT4_FEATURE_COMPAT_DIR_INDEX))
776 EXT4_I(inode)->i_flags &= ~EXT4_INDEX_FL;
780 * NOTE! unlike strncmp, ext4_match returns 1 for success, 0 for failure.
782 * `len <= EXT4_NAME_LEN' is guaranteed by caller.
783 * `de != NULL' is guaranteed by caller.
785 static inline int ext4_match (int len, const char * const name,
786 struct ext4_dir_entry_2 * de)
788 if (len != de->name_len)
789 return 0;
790 if (!de->inode)
791 return 0;
792 return !memcmp(name, de->name, len);
796 * Returns 0 if not found, -1 on failure, and 1 on success
798 static inline int search_dirblock(struct buffer_head * bh,
799 struct inode *dir,
800 struct dentry *dentry,
801 unsigned long offset,
802 struct ext4_dir_entry_2 ** res_dir)
804 struct ext4_dir_entry_2 * de;
805 char * dlimit;
806 int de_len;
807 const char *name = dentry->d_name.name;
808 int namelen = dentry->d_name.len;
810 de = (struct ext4_dir_entry_2 *) bh->b_data;
811 dlimit = bh->b_data + dir->i_sb->s_blocksize;
812 while ((char *) de < dlimit) {
813 /* this code is executed quadratically often */
814 /* do minimal checking `by hand' */
816 if ((char *) de + namelen <= dlimit &&
817 ext4_match (namelen, name, de)) {
818 /* found a match - just to be sure, do a full check */
819 if (!ext4_check_dir_entry("ext4_find_entry",
820 dir, de, bh, offset))
821 return -1;
822 *res_dir = de;
823 return 1;
825 /* prevent looping on a bad block */
826 de_len = le16_to_cpu(de->rec_len);
827 if (de_len <= 0)
828 return -1;
829 offset += de_len;
830 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
832 return 0;
837 * ext4_find_entry()
839 * finds an entry in the specified directory with the wanted name. It
840 * returns the cache buffer in which the entry was found, and the entry
841 * itself (as a parameter - res_dir). It does NOT read the inode of the
842 * entry - you'll have to do that yourself if you want to.
844 * The returned buffer_head has ->b_count elevated. The caller is expected
845 * to brelse() it when appropriate.
847 static struct buffer_head * ext4_find_entry (struct dentry *dentry,
848 struct ext4_dir_entry_2 ** res_dir)
850 struct super_block * sb;
851 struct buffer_head * bh_use[NAMEI_RA_SIZE];
852 struct buffer_head * bh, *ret = NULL;
853 unsigned long start, block, b;
854 int ra_max = 0; /* Number of bh's in the readahead
855 buffer, bh_use[] */
856 int ra_ptr = 0; /* Current index into readahead
857 buffer */
858 int num = 0;
859 int nblocks, i, err;
860 struct inode *dir = dentry->d_parent->d_inode;
861 int namelen;
862 const u8 *name;
863 unsigned blocksize;
865 *res_dir = NULL;
866 sb = dir->i_sb;
867 blocksize = sb->s_blocksize;
868 namelen = dentry->d_name.len;
869 name = dentry->d_name.name;
870 if (namelen > EXT4_NAME_LEN)
871 return NULL;
872 #ifdef CONFIG_EXT4_INDEX
873 if (is_dx(dir)) {
874 bh = ext4_dx_find_entry(dentry, res_dir, &err);
876 * On success, or if the error was file not found,
877 * return. Otherwise, fall back to doing a search the
878 * old fashioned way.
880 if (bh || (err != ERR_BAD_DX_DIR))
881 return bh;
882 dxtrace(printk("ext4_find_entry: dx failed, falling back\n"));
884 #endif
885 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
886 start = EXT4_I(dir)->i_dir_start_lookup;
887 if (start >= nblocks)
888 start = 0;
889 block = start;
890 restart:
891 do {
893 * We deal with the read-ahead logic here.
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++) {
901 * Terminate if we reach the end of the
902 * directory and must wrap, or if our
903 * search has finished at this block.
905 if (b >= nblocks || (num && block == start)) {
906 bh_use[ra_max] = NULL;
907 break;
909 num++;
910 bh = ext4_getblk(NULL, dir, b++, 0, &err);
911 bh_use[ra_max] = bh;
912 if (bh)
913 ll_rw_block(READ_META, 1, &bh);
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 ext4_error(sb, __FUNCTION__, "reading directory #%lu "
922 "offset %lu", dir->i_ino, block);
923 brelse(bh);
924 goto next;
926 i = search_dirblock(bh, dir, dentry,
927 block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
928 if (i == 1) {
929 EXT4_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;
937 next:
938 if (++block >= nblocks)
939 block = 0;
940 } while (block != start);
943 * If the directory has grown while we were searching, then
944 * search the last part of the directory before giving up.
946 block = nblocks;
947 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
948 if (block < nblocks) {
949 start = 0;
950 goto restart;
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;
960 #ifdef CONFIG_EXT4_INDEX
961 static struct buffer_head * ext4_dx_find_entry(struct dentry *dentry,
962 struct ext4_dir_entry_2 **res_dir, int *err)
964 struct super_block * sb;
965 struct dx_hash_info hinfo;
966 u32 hash;
967 struct dx_frame frames[2], *frame;
968 struct ext4_dir_entry_2 *de, *top;
969 struct buffer_head *bh;
970 unsigned long block;
971 int retval;
972 int namelen = dentry->d_name.len;
973 const u8 *name = dentry->d_name.name;
974 struct inode *dir = dentry->d_parent->d_inode;
976 sb = dir->i_sb;
977 /* NFS may look up ".." - look at dx_root directory block */
978 if (namelen > 2 || name[0] != '.'||(name[1] != '.' && name[1] != '\0')){
979 if (!(frame = dx_probe(dentry, NULL, &hinfo, frames, err)))
980 return NULL;
981 } else {
982 frame = frames;
983 frame->bh = NULL; /* for dx_release() */
984 frame->at = (struct dx_entry *)frames; /* hack for zero entry*/
985 dx_set_block(frame->at, 0); /* dx_root block is 0 */
987 hash = hinfo.hash;
988 do {
989 block = dx_get_block(frame->at);
990 if (!(bh = ext4_bread (NULL,dir, block, 0, err)))
991 goto errout;
992 de = (struct ext4_dir_entry_2 *) bh->b_data;
993 top = (struct ext4_dir_entry_2 *) ((char *) de + sb->s_blocksize -
994 EXT4_DIR_REC_LEN(0));
995 for (; de < top; de = ext4_next_entry(de))
996 if (ext4_match (namelen, name, de)) {
997 if (!ext4_check_dir_entry("ext4_find_entry",
998 dir, de, bh,
999 (block<<EXT4_BLOCK_SIZE_BITS(sb))
1000 +((char *)de - bh->b_data))) {
1001 brelse (bh);
1002 *err = ERR_BAD_DX_DIR;
1003 goto errout;
1005 *res_dir = de;
1006 dx_release (frames);
1007 return bh;
1009 brelse (bh);
1010 /* Check to see if we should continue to search */
1011 retval = ext4_htree_next_block(dir, hash, frame,
1012 frames, NULL);
1013 if (retval < 0) {
1014 ext4_warning(sb, __FUNCTION__,
1015 "error reading index page in directory #%lu",
1016 dir->i_ino);
1017 *err = retval;
1018 goto errout;
1020 } while (retval == 1);
1022 *err = -ENOENT;
1023 errout:
1024 dxtrace(printk("%s not found\n", name));
1025 dx_release (frames);
1026 return NULL;
1028 #endif
1030 static struct dentry *ext4_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
1032 struct inode * inode;
1033 struct ext4_dir_entry_2 * de;
1034 struct buffer_head * bh;
1036 if (dentry->d_name.len > EXT4_NAME_LEN)
1037 return ERR_PTR(-ENAMETOOLONG);
1039 bh = ext4_find_entry(dentry, &de);
1040 inode = NULL;
1041 if (bh) {
1042 unsigned long ino = le32_to_cpu(de->inode);
1043 brelse (bh);
1044 if (!ext4_valid_inum(dir->i_sb, ino)) {
1045 ext4_error(dir->i_sb, "ext4_lookup",
1046 "bad inode number: %lu", ino);
1047 inode = NULL;
1048 } else
1049 inode = iget(dir->i_sb, ino);
1051 if (!inode)
1052 return ERR_PTR(-EACCES);
1054 if (is_bad_inode(inode)) {
1055 iput(inode);
1056 return ERR_PTR(-ENOENT);
1059 return d_splice_alias(inode, dentry);
1063 struct dentry *ext4_get_parent(struct dentry *child)
1065 unsigned long ino;
1066 struct dentry *parent;
1067 struct inode *inode;
1068 struct dentry dotdot;
1069 struct ext4_dir_entry_2 * de;
1070 struct buffer_head *bh;
1072 dotdot.d_name.name = "..";
1073 dotdot.d_name.len = 2;
1074 dotdot.d_parent = child; /* confusing, isn't it! */
1076 bh = ext4_find_entry(&dotdot, &de);
1077 inode = NULL;
1078 if (!bh)
1079 return ERR_PTR(-ENOENT);
1080 ino = le32_to_cpu(de->inode);
1081 brelse(bh);
1083 if (!ext4_valid_inum(child->d_inode->i_sb, ino)) {
1084 ext4_error(child->d_inode->i_sb, "ext4_get_parent",
1085 "bad inode number: %lu", ino);
1086 inode = NULL;
1087 } else
1088 inode = iget(child->d_inode->i_sb, ino);
1090 if (!inode)
1091 return ERR_PTR(-EACCES);
1093 if (is_bad_inode(inode)) {
1094 iput(inode);
1095 return ERR_PTR(-ENOENT);
1098 parent = d_alloc_anon(inode);
1099 if (!parent) {
1100 iput(inode);
1101 parent = ERR_PTR(-ENOMEM);
1103 return parent;
1106 #define S_SHIFT 12
1107 static unsigned char ext4_type_by_mode[S_IFMT >> S_SHIFT] = {
1108 [S_IFREG >> S_SHIFT] = EXT4_FT_REG_FILE,
1109 [S_IFDIR >> S_SHIFT] = EXT4_FT_DIR,
1110 [S_IFCHR >> S_SHIFT] = EXT4_FT_CHRDEV,
1111 [S_IFBLK >> S_SHIFT] = EXT4_FT_BLKDEV,
1112 [S_IFIFO >> S_SHIFT] = EXT4_FT_FIFO,
1113 [S_IFSOCK >> S_SHIFT] = EXT4_FT_SOCK,
1114 [S_IFLNK >> S_SHIFT] = EXT4_FT_SYMLINK,
1117 static inline void ext4_set_de_type(struct super_block *sb,
1118 struct ext4_dir_entry_2 *de,
1119 umode_t mode) {
1120 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FILETYPE))
1121 de->file_type = ext4_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
1124 #ifdef CONFIG_EXT4_INDEX
1126 * Move count entries from end of map between two memory locations.
1127 * Returns pointer to last entry moved.
1129 static struct ext4_dir_entry_2 *
1130 dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count)
1132 unsigned rec_len = 0;
1134 while (count--) {
1135 struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *) (from + map->offs);
1136 rec_len = EXT4_DIR_REC_LEN(de->name_len);
1137 memcpy (to, de, rec_len);
1138 ((struct ext4_dir_entry_2 *) to)->rec_len =
1139 cpu_to_le16(rec_len);
1140 de->inode = 0;
1141 map++;
1142 to += rec_len;
1144 return (struct ext4_dir_entry_2 *) (to - rec_len);
1148 * Compact each dir entry in the range to the minimal rec_len.
1149 * Returns pointer to last entry in range.
1151 static struct ext4_dir_entry_2* dx_pack_dirents(char *base, int size)
1153 struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
1154 unsigned rec_len = 0;
1156 prev = to = de;
1157 while ((char*)de < base + size) {
1158 next = (struct ext4_dir_entry_2 *) ((char *) de +
1159 le16_to_cpu(de->rec_len));
1160 if (de->inode && de->name_len) {
1161 rec_len = EXT4_DIR_REC_LEN(de->name_len);
1162 if (de > to)
1163 memmove(to, de, rec_len);
1164 to->rec_len = cpu_to_le16(rec_len);
1165 prev = to;
1166 to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
1168 de = next;
1170 return prev;
1174 * Split a full leaf block to make room for a new dir entry.
1175 * Allocate a new block, and move entries so that they are approx. equally full.
1176 * Returns pointer to de in block into which the new entry will be inserted.
1178 static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
1179 struct buffer_head **bh,struct dx_frame *frame,
1180 struct dx_hash_info *hinfo, int *error)
1182 unsigned blocksize = dir->i_sb->s_blocksize;
1183 unsigned count, continued;
1184 struct buffer_head *bh2;
1185 u32 newblock;
1186 u32 hash2;
1187 struct dx_map_entry *map;
1188 char *data1 = (*bh)->b_data, *data2;
1189 unsigned split, move, size, i;
1190 struct ext4_dir_entry_2 *de = NULL, *de2;
1191 int err = 0;
1193 bh2 = ext4_append (handle, dir, &newblock, &err);
1194 if (!(bh2)) {
1195 brelse(*bh);
1196 *bh = NULL;
1197 goto errout;
1200 BUFFER_TRACE(*bh, "get_write_access");
1201 err = ext4_journal_get_write_access(handle, *bh);
1202 if (err)
1203 goto journal_error;
1205 BUFFER_TRACE(frame->bh, "get_write_access");
1206 err = ext4_journal_get_write_access(handle, frame->bh);
1207 if (err)
1208 goto journal_error;
1210 data2 = bh2->b_data;
1212 /* create map in the end of data2 block */
1213 map = (struct dx_map_entry *) (data2 + blocksize);
1214 count = dx_make_map ((struct ext4_dir_entry_2 *) data1,
1215 blocksize, hinfo, map);
1216 map -= count;
1217 dx_sort_map (map, count);
1218 /* Split the existing block in the middle, size-wise */
1219 size = 0;
1220 move = 0;
1221 for (i = count-1; i >= 0; i--) {
1222 /* is more than half of this entry in 2nd half of the block? */
1223 if (size + map[i].size/2 > blocksize/2)
1224 break;
1225 size += map[i].size;
1226 move++;
1228 /* map index at which we will split */
1229 split = count - move;
1230 hash2 = map[split].hash;
1231 continued = hash2 == map[split - 1].hash;
1232 dxtrace(printk("Split block %i at %x, %i/%i\n",
1233 dx_get_block(frame->at), hash2, split, count-split));
1235 /* Fancy dance to stay within two buffers */
1236 de2 = dx_move_dirents(data1, data2, map + split, count - split);
1237 de = dx_pack_dirents(data1,blocksize);
1238 de->rec_len = cpu_to_le16(data1 + blocksize - (char *) de);
1239 de2->rec_len = cpu_to_le16(data2 + blocksize - (char *) de2);
1240 dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data1, blocksize, 1));
1241 dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data2, blocksize, 1));
1243 /* Which block gets the new entry? */
1244 if (hinfo->hash >= hash2)
1246 swap(*bh, bh2);
1247 de = de2;
1249 dx_insert_block (frame, hash2 + continued, newblock);
1250 err = ext4_journal_dirty_metadata (handle, bh2);
1251 if (err)
1252 goto journal_error;
1253 err = ext4_journal_dirty_metadata (handle, frame->bh);
1254 if (err)
1255 goto journal_error;
1256 brelse (bh2);
1257 dxtrace(dx_show_index ("frame", frame->entries));
1258 return de;
1260 journal_error:
1261 brelse(*bh);
1262 brelse(bh2);
1263 *bh = NULL;
1264 ext4_std_error(dir->i_sb, err);
1265 errout:
1266 *error = err;
1267 return NULL;
1269 #endif
1273 * Add a new entry into a directory (leaf) block. If de is non-NULL,
1274 * it points to a directory entry which is guaranteed to be large
1275 * enough for new directory entry. If de is NULL, then
1276 * add_dirent_to_buf will attempt search the directory block for
1277 * space. It will return -ENOSPC if no space is available, and -EIO
1278 * and -EEXIST if directory entry already exists.
1280 * NOTE! bh is NOT released in the case where ENOSPC is returned. In
1281 * all other cases bh is released.
1283 static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
1284 struct inode *inode, struct ext4_dir_entry_2 *de,
1285 struct buffer_head * bh)
1287 struct inode *dir = dentry->d_parent->d_inode;
1288 const char *name = dentry->d_name.name;
1289 int namelen = dentry->d_name.len;
1290 unsigned long offset = 0;
1291 unsigned short reclen;
1292 int nlen, rlen, err;
1293 char *top;
1295 reclen = EXT4_DIR_REC_LEN(namelen);
1296 if (!de) {
1297 de = (struct ext4_dir_entry_2 *)bh->b_data;
1298 top = bh->b_data + dir->i_sb->s_blocksize - reclen;
1299 while ((char *) de <= top) {
1300 if (!ext4_check_dir_entry("ext4_add_entry", dir, de,
1301 bh, offset)) {
1302 brelse (bh);
1303 return -EIO;
1305 if (ext4_match (namelen, name, de)) {
1306 brelse (bh);
1307 return -EEXIST;
1309 nlen = EXT4_DIR_REC_LEN(de->name_len);
1310 rlen = le16_to_cpu(de->rec_len);
1311 if ((de->inode? rlen - nlen: rlen) >= reclen)
1312 break;
1313 de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
1314 offset += rlen;
1316 if ((char *) de > top)
1317 return -ENOSPC;
1319 BUFFER_TRACE(bh, "get_write_access");
1320 err = ext4_journal_get_write_access(handle, bh);
1321 if (err) {
1322 ext4_std_error(dir->i_sb, err);
1323 brelse(bh);
1324 return err;
1327 /* By now the buffer is marked for journaling */
1328 nlen = EXT4_DIR_REC_LEN(de->name_len);
1329 rlen = le16_to_cpu(de->rec_len);
1330 if (de->inode) {
1331 struct ext4_dir_entry_2 *de1 = (struct ext4_dir_entry_2 *)((char *)de + nlen);
1332 de1->rec_len = cpu_to_le16(rlen - nlen);
1333 de->rec_len = cpu_to_le16(nlen);
1334 de = de1;
1336 de->file_type = EXT4_FT_UNKNOWN;
1337 if (inode) {
1338 de->inode = cpu_to_le32(inode->i_ino);
1339 ext4_set_de_type(dir->i_sb, de, inode->i_mode);
1340 } else
1341 de->inode = 0;
1342 de->name_len = namelen;
1343 memcpy (de->name, name, namelen);
1345 * XXX shouldn't update any times until successful
1346 * completion of syscall, but too many callers depend
1347 * on this.
1349 * XXX similarly, too many callers depend on
1350 * ext4_new_inode() setting the times, but error
1351 * recovery deletes the inode, so the worst that can
1352 * happen is that the times are slightly out of date
1353 * and/or different from the directory change time.
1355 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
1356 ext4_update_dx_flag(dir);
1357 dir->i_version++;
1358 ext4_mark_inode_dirty(handle, dir);
1359 BUFFER_TRACE(bh, "call ext4_journal_dirty_metadata");
1360 err = ext4_journal_dirty_metadata(handle, bh);
1361 if (err)
1362 ext4_std_error(dir->i_sb, err);
1363 brelse(bh);
1364 return 0;
1367 #ifdef CONFIG_EXT4_INDEX
1369 * This converts a one block unindexed directory to a 3 block indexed
1370 * directory, and adds the dentry to the indexed directory.
1372 static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
1373 struct inode *inode, struct buffer_head *bh)
1375 struct inode *dir = dentry->d_parent->d_inode;
1376 const char *name = dentry->d_name.name;
1377 int namelen = dentry->d_name.len;
1378 struct buffer_head *bh2;
1379 struct dx_root *root;
1380 struct dx_frame frames[2], *frame;
1381 struct dx_entry *entries;
1382 struct ext4_dir_entry_2 *de, *de2;
1383 char *data1, *top;
1384 unsigned len;
1385 int retval;
1386 unsigned blocksize;
1387 struct dx_hash_info hinfo;
1388 u32 block;
1389 struct fake_dirent *fde;
1391 blocksize = dir->i_sb->s_blocksize;
1392 dxtrace(printk("Creating index\n"));
1393 retval = ext4_journal_get_write_access(handle, bh);
1394 if (retval) {
1395 ext4_std_error(dir->i_sb, retval);
1396 brelse(bh);
1397 return retval;
1399 root = (struct dx_root *) bh->b_data;
1401 bh2 = ext4_append (handle, dir, &block, &retval);
1402 if (!(bh2)) {
1403 brelse(bh);
1404 return retval;
1406 EXT4_I(dir)->i_flags |= EXT4_INDEX_FL;
1407 data1 = bh2->b_data;
1409 /* The 0th block becomes the root, move the dirents out */
1410 fde = &root->dotdot;
1411 de = (struct ext4_dir_entry_2 *)((char *)fde + le16_to_cpu(fde->rec_len));
1412 len = ((char *) root) + blocksize - (char *) de;
1413 memcpy (data1, de, len);
1414 de = (struct ext4_dir_entry_2 *) data1;
1415 top = data1 + len;
1416 while ((char *)(de2=(void*)de+le16_to_cpu(de->rec_len)) < top)
1417 de = de2;
1418 de->rec_len = cpu_to_le16(data1 + blocksize - (char *) de);
1419 /* Initialize the root; the dot dirents already exist */
1420 de = (struct ext4_dir_entry_2 *) (&root->dotdot);
1421 de->rec_len = cpu_to_le16(blocksize - EXT4_DIR_REC_LEN(2));
1422 memset (&root->info, 0, sizeof(root->info));
1423 root->info.info_length = sizeof(root->info);
1424 root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
1425 entries = root->entries;
1426 dx_set_block (entries, 1);
1427 dx_set_count (entries, 1);
1428 dx_set_limit (entries, dx_root_limit(dir, sizeof(root->info)));
1430 /* Initialize as for dx_probe */
1431 hinfo.hash_version = root->info.hash_version;
1432 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
1433 ext4fs_dirhash(name, namelen, &hinfo);
1434 frame = frames;
1435 frame->entries = entries;
1436 frame->at = entries;
1437 frame->bh = bh;
1438 bh = bh2;
1439 de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
1440 dx_release (frames);
1441 if (!(de))
1442 return retval;
1444 return add_dirent_to_buf(handle, dentry, inode, de, bh);
1446 #endif
1449 * ext4_add_entry()
1451 * adds a file entry to the specified directory, using the same
1452 * semantics as ext4_find_entry(). It returns NULL if it failed.
1454 * NOTE!! The inode part of 'de' is left at 0 - which means you
1455 * may not sleep between calling this and putting something into
1456 * the entry, as someone else might have used it while you slept.
1458 static int ext4_add_entry (handle_t *handle, struct dentry *dentry,
1459 struct inode *inode)
1461 struct inode *dir = dentry->d_parent->d_inode;
1462 unsigned long offset;
1463 struct buffer_head * bh;
1464 struct ext4_dir_entry_2 *de;
1465 struct super_block * sb;
1466 int retval;
1467 #ifdef CONFIG_EXT4_INDEX
1468 int dx_fallback=0;
1469 #endif
1470 unsigned blocksize;
1471 u32 block, blocks;
1473 sb = dir->i_sb;
1474 blocksize = sb->s_blocksize;
1475 if (!dentry->d_name.len)
1476 return -EINVAL;
1477 #ifdef CONFIG_EXT4_INDEX
1478 if (is_dx(dir)) {
1479 retval = ext4_dx_add_entry(handle, dentry, inode);
1480 if (!retval || (retval != ERR_BAD_DX_DIR))
1481 return retval;
1482 EXT4_I(dir)->i_flags &= ~EXT4_INDEX_FL;
1483 dx_fallback++;
1484 ext4_mark_inode_dirty(handle, dir);
1486 #endif
1487 blocks = dir->i_size >> sb->s_blocksize_bits;
1488 for (block = 0, offset = 0; block < blocks; block++) {
1489 bh = ext4_bread(handle, dir, block, 0, &retval);
1490 if(!bh)
1491 return retval;
1492 retval = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
1493 if (retval != -ENOSPC)
1494 return retval;
1496 #ifdef CONFIG_EXT4_INDEX
1497 if (blocks == 1 && !dx_fallback &&
1498 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_DIR_INDEX))
1499 return make_indexed_dir(handle, dentry, inode, bh);
1500 #endif
1501 brelse(bh);
1503 bh = ext4_append(handle, dir, &block, &retval);
1504 if (!bh)
1505 return retval;
1506 de = (struct ext4_dir_entry_2 *) bh->b_data;
1507 de->inode = 0;
1508 de->rec_len = cpu_to_le16(blocksize);
1509 return add_dirent_to_buf(handle, dentry, inode, de, bh);
1512 #ifdef CONFIG_EXT4_INDEX
1514 * Returns 0 for success, or a negative error value
1516 static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
1517 struct inode *inode)
1519 struct dx_frame frames[2], *frame;
1520 struct dx_entry *entries, *at;
1521 struct dx_hash_info hinfo;
1522 struct buffer_head * bh;
1523 struct inode *dir = dentry->d_parent->d_inode;
1524 struct super_block * sb = dir->i_sb;
1525 struct ext4_dir_entry_2 *de;
1526 int err;
1528 frame = dx_probe(dentry, NULL, &hinfo, frames, &err);
1529 if (!frame)
1530 return err;
1531 entries = frame->entries;
1532 at = frame->at;
1534 if (!(bh = ext4_bread(handle,dir, dx_get_block(frame->at), 0, &err)))
1535 goto cleanup;
1537 BUFFER_TRACE(bh, "get_write_access");
1538 err = ext4_journal_get_write_access(handle, bh);
1539 if (err)
1540 goto journal_error;
1542 err = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
1543 if (err != -ENOSPC) {
1544 bh = NULL;
1545 goto cleanup;
1548 /* Block full, should compress but for now just split */
1549 dxtrace(printk("using %u of %u node entries\n",
1550 dx_get_count(entries), dx_get_limit(entries)));
1551 /* Need to split index? */
1552 if (dx_get_count(entries) == dx_get_limit(entries)) {
1553 u32 newblock;
1554 unsigned icount = dx_get_count(entries);
1555 int levels = frame - frames;
1556 struct dx_entry *entries2;
1557 struct dx_node *node2;
1558 struct buffer_head *bh2;
1560 if (levels && (dx_get_count(frames->entries) ==
1561 dx_get_limit(frames->entries))) {
1562 ext4_warning(sb, __FUNCTION__,
1563 "Directory index full!");
1564 err = -ENOSPC;
1565 goto cleanup;
1567 bh2 = ext4_append (handle, dir, &newblock, &err);
1568 if (!(bh2))
1569 goto cleanup;
1570 node2 = (struct dx_node *)(bh2->b_data);
1571 entries2 = node2->entries;
1572 node2->fake.rec_len = cpu_to_le16(sb->s_blocksize);
1573 node2->fake.inode = 0;
1574 BUFFER_TRACE(frame->bh, "get_write_access");
1575 err = ext4_journal_get_write_access(handle, frame->bh);
1576 if (err)
1577 goto journal_error;
1578 if (levels) {
1579 unsigned icount1 = icount/2, icount2 = icount - icount1;
1580 unsigned hash2 = dx_get_hash(entries + icount1);
1581 dxtrace(printk("Split index %i/%i\n", icount1, icount2));
1583 BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
1584 err = ext4_journal_get_write_access(handle,
1585 frames[0].bh);
1586 if (err)
1587 goto journal_error;
1589 memcpy ((char *) entries2, (char *) (entries + icount1),
1590 icount2 * sizeof(struct dx_entry));
1591 dx_set_count (entries, icount1);
1592 dx_set_count (entries2, icount2);
1593 dx_set_limit (entries2, dx_node_limit(dir));
1595 /* Which index block gets the new entry? */
1596 if (at - entries >= icount1) {
1597 frame->at = at = at - entries - icount1 + entries2;
1598 frame->entries = entries = entries2;
1599 swap(frame->bh, bh2);
1601 dx_insert_block (frames + 0, hash2, newblock);
1602 dxtrace(dx_show_index ("node", frames[1].entries));
1603 dxtrace(dx_show_index ("node",
1604 ((struct dx_node *) bh2->b_data)->entries));
1605 err = ext4_journal_dirty_metadata(handle, bh2);
1606 if (err)
1607 goto journal_error;
1608 brelse (bh2);
1609 } else {
1610 dxtrace(printk("Creating second level index...\n"));
1611 memcpy((char *) entries2, (char *) entries,
1612 icount * sizeof(struct dx_entry));
1613 dx_set_limit(entries2, dx_node_limit(dir));
1615 /* Set up root */
1616 dx_set_count(entries, 1);
1617 dx_set_block(entries + 0, newblock);
1618 ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
1620 /* Add new access path frame */
1621 frame = frames + 1;
1622 frame->at = at = at - entries + entries2;
1623 frame->entries = entries = entries2;
1624 frame->bh = bh2;
1625 err = ext4_journal_get_write_access(handle,
1626 frame->bh);
1627 if (err)
1628 goto journal_error;
1630 ext4_journal_dirty_metadata(handle, frames[0].bh);
1632 de = do_split(handle, dir, &bh, frame, &hinfo, &err);
1633 if (!de)
1634 goto cleanup;
1635 err = add_dirent_to_buf(handle, dentry, inode, de, bh);
1636 bh = NULL;
1637 goto cleanup;
1639 journal_error:
1640 ext4_std_error(dir->i_sb, err);
1641 cleanup:
1642 if (bh)
1643 brelse(bh);
1644 dx_release(frames);
1645 return err;
1647 #endif
1650 * ext4_delete_entry deletes a directory entry by merging it with the
1651 * previous entry
1653 static int ext4_delete_entry (handle_t *handle,
1654 struct inode * dir,
1655 struct ext4_dir_entry_2 * de_del,
1656 struct buffer_head * bh)
1658 struct ext4_dir_entry_2 * de, * pde;
1659 int i;
1661 i = 0;
1662 pde = NULL;
1663 de = (struct ext4_dir_entry_2 *) bh->b_data;
1664 while (i < bh->b_size) {
1665 if (!ext4_check_dir_entry("ext4_delete_entry", dir, de, bh, i))
1666 return -EIO;
1667 if (de == de_del) {
1668 BUFFER_TRACE(bh, "get_write_access");
1669 ext4_journal_get_write_access(handle, bh);
1670 if (pde)
1671 pde->rec_len =
1672 cpu_to_le16(le16_to_cpu(pde->rec_len) +
1673 le16_to_cpu(de->rec_len));
1674 else
1675 de->inode = 0;
1676 dir->i_version++;
1677 BUFFER_TRACE(bh, "call ext4_journal_dirty_metadata");
1678 ext4_journal_dirty_metadata(handle, bh);
1679 return 0;
1681 i += le16_to_cpu(de->rec_len);
1682 pde = de;
1683 de = (struct ext4_dir_entry_2 *)
1684 ((char *) de + le16_to_cpu(de->rec_len));
1686 return -ENOENT;
1689 static int ext4_add_nondir(handle_t *handle,
1690 struct dentry *dentry, struct inode *inode)
1692 int err = ext4_add_entry(handle, dentry, inode);
1693 if (!err) {
1694 ext4_mark_inode_dirty(handle, inode);
1695 d_instantiate(dentry, inode);
1696 return 0;
1698 drop_nlink(inode);
1699 iput(inode);
1700 return err;
1704 * By the time this is called, we already have created
1705 * the directory cache entry for the new file, but it
1706 * is so far negative - it has no inode.
1708 * If the create succeeds, we fill in the inode information
1709 * with d_instantiate().
1711 static int ext4_create (struct inode * dir, struct dentry * dentry, int mode,
1712 struct nameidata *nd)
1714 handle_t *handle;
1715 struct inode * inode;
1716 int err, retries = 0;
1718 retry:
1719 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
1720 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
1721 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
1722 if (IS_ERR(handle))
1723 return PTR_ERR(handle);
1725 if (IS_DIRSYNC(dir))
1726 handle->h_sync = 1;
1728 inode = ext4_new_inode (handle, dir, mode);
1729 err = PTR_ERR(inode);
1730 if (!IS_ERR(inode)) {
1731 inode->i_op = &ext4_file_inode_operations;
1732 inode->i_fop = &ext4_file_operations;
1733 ext4_set_aops(inode);
1734 err = ext4_add_nondir(handle, dentry, inode);
1736 ext4_journal_stop(handle);
1737 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
1738 goto retry;
1739 return err;
1742 static int ext4_mknod (struct inode * dir, struct dentry *dentry,
1743 int mode, dev_t rdev)
1745 handle_t *handle;
1746 struct inode *inode;
1747 int err, retries = 0;
1749 if (!new_valid_dev(rdev))
1750 return -EINVAL;
1752 retry:
1753 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
1754 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
1755 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
1756 if (IS_ERR(handle))
1757 return PTR_ERR(handle);
1759 if (IS_DIRSYNC(dir))
1760 handle->h_sync = 1;
1762 inode = ext4_new_inode (handle, dir, mode);
1763 err = PTR_ERR(inode);
1764 if (!IS_ERR(inode)) {
1765 init_special_inode(inode, inode->i_mode, rdev);
1766 #ifdef CONFIG_EXT4DEV_FS_XATTR
1767 inode->i_op = &ext4_special_inode_operations;
1768 #endif
1769 err = ext4_add_nondir(handle, dentry, inode);
1771 ext4_journal_stop(handle);
1772 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
1773 goto retry;
1774 return err;
1777 static int ext4_mkdir(struct inode * dir, struct dentry * dentry, int mode)
1779 handle_t *handle;
1780 struct inode * inode;
1781 struct buffer_head * dir_block;
1782 struct ext4_dir_entry_2 * de;
1783 int err, retries = 0;
1785 if (dir->i_nlink >= EXT4_LINK_MAX)
1786 return -EMLINK;
1788 retry:
1789 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
1790 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
1791 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
1792 if (IS_ERR(handle))
1793 return PTR_ERR(handle);
1795 if (IS_DIRSYNC(dir))
1796 handle->h_sync = 1;
1798 inode = ext4_new_inode (handle, dir, S_IFDIR | mode);
1799 err = PTR_ERR(inode);
1800 if (IS_ERR(inode))
1801 goto out_stop;
1803 inode->i_op = &ext4_dir_inode_operations;
1804 inode->i_fop = &ext4_dir_operations;
1805 inode->i_size = EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
1806 dir_block = ext4_bread (handle, inode, 0, 1, &err);
1807 if (!dir_block) {
1808 drop_nlink(inode); /* is this nlink == 0? */
1809 ext4_mark_inode_dirty(handle, inode);
1810 iput (inode);
1811 goto out_stop;
1813 BUFFER_TRACE(dir_block, "get_write_access");
1814 ext4_journal_get_write_access(handle, dir_block);
1815 de = (struct ext4_dir_entry_2 *) dir_block->b_data;
1816 de->inode = cpu_to_le32(inode->i_ino);
1817 de->name_len = 1;
1818 de->rec_len = cpu_to_le16(EXT4_DIR_REC_LEN(de->name_len));
1819 strcpy (de->name, ".");
1820 ext4_set_de_type(dir->i_sb, de, S_IFDIR);
1821 de = (struct ext4_dir_entry_2 *)
1822 ((char *) de + le16_to_cpu(de->rec_len));
1823 de->inode = cpu_to_le32(dir->i_ino);
1824 de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize-EXT4_DIR_REC_LEN(1));
1825 de->name_len = 2;
1826 strcpy (de->name, "..");
1827 ext4_set_de_type(dir->i_sb, de, S_IFDIR);
1828 inode->i_nlink = 2;
1829 BUFFER_TRACE(dir_block, "call ext4_journal_dirty_metadata");
1830 ext4_journal_dirty_metadata(handle, dir_block);
1831 brelse (dir_block);
1832 ext4_mark_inode_dirty(handle, inode);
1833 err = ext4_add_entry (handle, dentry, inode);
1834 if (err) {
1835 inode->i_nlink = 0;
1836 ext4_mark_inode_dirty(handle, inode);
1837 iput (inode);
1838 goto out_stop;
1840 inc_nlink(dir);
1841 ext4_update_dx_flag(dir);
1842 ext4_mark_inode_dirty(handle, dir);
1843 d_instantiate(dentry, inode);
1844 out_stop:
1845 ext4_journal_stop(handle);
1846 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
1847 goto retry;
1848 return err;
1852 * routine to check that the specified directory is empty (for rmdir)
1854 static int empty_dir (struct inode * inode)
1856 unsigned long offset;
1857 struct buffer_head * bh;
1858 struct ext4_dir_entry_2 * de, * de1;
1859 struct super_block * sb;
1860 int err = 0;
1862 sb = inode->i_sb;
1863 if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2) ||
1864 !(bh = ext4_bread (NULL, inode, 0, 0, &err))) {
1865 if (err)
1866 ext4_error(inode->i_sb, __FUNCTION__,
1867 "error %d reading directory #%lu offset 0",
1868 err, inode->i_ino);
1869 else
1870 ext4_warning(inode->i_sb, __FUNCTION__,
1871 "bad directory (dir #%lu) - no data block",
1872 inode->i_ino);
1873 return 1;
1875 de = (struct ext4_dir_entry_2 *) bh->b_data;
1876 de1 = (struct ext4_dir_entry_2 *)
1877 ((char *) de + le16_to_cpu(de->rec_len));
1878 if (le32_to_cpu(de->inode) != inode->i_ino ||
1879 !le32_to_cpu(de1->inode) ||
1880 strcmp (".", de->name) ||
1881 strcmp ("..", de1->name)) {
1882 ext4_warning (inode->i_sb, "empty_dir",
1883 "bad directory (dir #%lu) - no `.' or `..'",
1884 inode->i_ino);
1885 brelse (bh);
1886 return 1;
1888 offset = le16_to_cpu(de->rec_len) + le16_to_cpu(de1->rec_len);
1889 de = (struct ext4_dir_entry_2 *)
1890 ((char *) de1 + le16_to_cpu(de1->rec_len));
1891 while (offset < inode->i_size ) {
1892 if (!bh ||
1893 (void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
1894 err = 0;
1895 brelse (bh);
1896 bh = ext4_bread (NULL, inode,
1897 offset >> EXT4_BLOCK_SIZE_BITS(sb), 0, &err);
1898 if (!bh) {
1899 if (err)
1900 ext4_error(sb, __FUNCTION__,
1901 "error %d reading directory"
1902 " #%lu offset %lu",
1903 err, inode->i_ino, offset);
1904 offset += sb->s_blocksize;
1905 continue;
1907 de = (struct ext4_dir_entry_2 *) bh->b_data;
1909 if (!ext4_check_dir_entry("empty_dir", inode, de, bh, offset)) {
1910 de = (struct ext4_dir_entry_2 *)(bh->b_data +
1911 sb->s_blocksize);
1912 offset = (offset | (sb->s_blocksize - 1)) + 1;
1913 continue;
1915 if (le32_to_cpu(de->inode)) {
1916 brelse (bh);
1917 return 0;
1919 offset += le16_to_cpu(de->rec_len);
1920 de = (struct ext4_dir_entry_2 *)
1921 ((char *) de + le16_to_cpu(de->rec_len));
1923 brelse (bh);
1924 return 1;
1927 /* ext4_orphan_add() links an unlinked or truncated inode into a list of
1928 * such inodes, starting at the superblock, in case we crash before the
1929 * file is closed/deleted, or in case the inode truncate spans multiple
1930 * transactions and the last transaction is not recovered after a crash.
1932 * At filesystem recovery time, we walk this list deleting unlinked
1933 * inodes and truncating linked inodes in ext4_orphan_cleanup().
1935 int ext4_orphan_add(handle_t *handle, struct inode *inode)
1937 struct super_block *sb = inode->i_sb;
1938 struct ext4_iloc iloc;
1939 int err = 0, rc;
1941 lock_super(sb);
1942 if (!list_empty(&EXT4_I(inode)->i_orphan))
1943 goto out_unlock;
1945 /* Orphan handling is only valid for files with data blocks
1946 * being truncated, or files being unlinked. */
1948 /* @@@ FIXME: Observation from aviro:
1949 * I think I can trigger J_ASSERT in ext4_orphan_add(). We block
1950 * here (on lock_super()), so race with ext4_link() which might bump
1951 * ->i_nlink. For, say it, character device. Not a regular file,
1952 * not a directory, not a symlink and ->i_nlink > 0.
1954 J_ASSERT ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1955 S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
1957 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
1958 err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
1959 if (err)
1960 goto out_unlock;
1962 err = ext4_reserve_inode_write(handle, inode, &iloc);
1963 if (err)
1964 goto out_unlock;
1966 /* Insert this inode at the head of the on-disk orphan list... */
1967 NEXT_ORPHAN(inode) = le32_to_cpu(EXT4_SB(sb)->s_es->s_last_orphan);
1968 EXT4_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
1969 err = ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh);
1970 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
1971 if (!err)
1972 err = rc;
1974 /* Only add to the head of the in-memory list if all the
1975 * previous operations succeeded. If the orphan_add is going to
1976 * fail (possibly taking the journal offline), we can't risk
1977 * leaving the inode on the orphan list: stray orphan-list
1978 * entries can cause panics at unmount time.
1980 * This is safe: on error we're going to ignore the orphan list
1981 * anyway on the next recovery. */
1982 if (!err)
1983 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
1985 jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
1986 jbd_debug(4, "orphan inode %lu will point to %d\n",
1987 inode->i_ino, NEXT_ORPHAN(inode));
1988 out_unlock:
1989 unlock_super(sb);
1990 ext4_std_error(inode->i_sb, err);
1991 return err;
1995 * ext4_orphan_del() removes an unlinked or truncated inode from the list
1996 * of such inodes stored on disk, because it is finally being cleaned up.
1998 int ext4_orphan_del(handle_t *handle, struct inode *inode)
2000 struct list_head *prev;
2001 struct ext4_inode_info *ei = EXT4_I(inode);
2002 struct ext4_sb_info *sbi;
2003 unsigned long ino_next;
2004 struct ext4_iloc iloc;
2005 int err = 0;
2007 lock_super(inode->i_sb);
2008 if (list_empty(&ei->i_orphan)) {
2009 unlock_super(inode->i_sb);
2010 return 0;
2013 ino_next = NEXT_ORPHAN(inode);
2014 prev = ei->i_orphan.prev;
2015 sbi = EXT4_SB(inode->i_sb);
2017 jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
2019 list_del_init(&ei->i_orphan);
2021 /* If we're on an error path, we may not have a valid
2022 * transaction handle with which to update the orphan list on
2023 * disk, but we still need to remove the inode from the linked
2024 * list in memory. */
2025 if (!handle)
2026 goto out;
2028 err = ext4_reserve_inode_write(handle, inode, &iloc);
2029 if (err)
2030 goto out_err;
2032 if (prev == &sbi->s_orphan) {
2033 jbd_debug(4, "superblock will point to %lu\n", ino_next);
2034 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
2035 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
2036 if (err)
2037 goto out_brelse;
2038 sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
2039 err = ext4_journal_dirty_metadata(handle, sbi->s_sbh);
2040 } else {
2041 struct ext4_iloc iloc2;
2042 struct inode *i_prev =
2043 &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
2045 jbd_debug(4, "orphan inode %lu will point to %lu\n",
2046 i_prev->i_ino, ino_next);
2047 err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
2048 if (err)
2049 goto out_brelse;
2050 NEXT_ORPHAN(i_prev) = ino_next;
2051 err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
2053 if (err)
2054 goto out_brelse;
2055 NEXT_ORPHAN(inode) = 0;
2056 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
2058 out_err:
2059 ext4_std_error(inode->i_sb, err);
2060 out:
2061 unlock_super(inode->i_sb);
2062 return err;
2064 out_brelse:
2065 brelse(iloc.bh);
2066 goto out_err;
2069 static int ext4_rmdir (struct inode * dir, struct dentry *dentry)
2071 int retval;
2072 struct inode * inode;
2073 struct buffer_head * bh;
2074 struct ext4_dir_entry_2 * de;
2075 handle_t *handle;
2077 /* Initialize quotas before so that eventual writes go in
2078 * separate transaction */
2079 DQUOT_INIT(dentry->d_inode);
2080 handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
2081 if (IS_ERR(handle))
2082 return PTR_ERR(handle);
2084 retval = -ENOENT;
2085 bh = ext4_find_entry (dentry, &de);
2086 if (!bh)
2087 goto end_rmdir;
2089 if (IS_DIRSYNC(dir))
2090 handle->h_sync = 1;
2092 inode = dentry->d_inode;
2094 retval = -EIO;
2095 if (le32_to_cpu(de->inode) != inode->i_ino)
2096 goto end_rmdir;
2098 retval = -ENOTEMPTY;
2099 if (!empty_dir (inode))
2100 goto end_rmdir;
2102 retval = ext4_delete_entry(handle, dir, de, bh);
2103 if (retval)
2104 goto end_rmdir;
2105 if (inode->i_nlink != 2)
2106 ext4_warning (inode->i_sb, "ext4_rmdir",
2107 "empty directory has nlink!=2 (%d)",
2108 inode->i_nlink);
2109 inode->i_version++;
2110 clear_nlink(inode);
2111 /* There's no need to set i_disksize: the fact that i_nlink is
2112 * zero will ensure that the right thing happens during any
2113 * recovery. */
2114 inode->i_size = 0;
2115 ext4_orphan_add(handle, inode);
2116 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
2117 ext4_mark_inode_dirty(handle, inode);
2118 drop_nlink(dir);
2119 ext4_update_dx_flag(dir);
2120 ext4_mark_inode_dirty(handle, dir);
2122 end_rmdir:
2123 ext4_journal_stop(handle);
2124 brelse (bh);
2125 return retval;
2128 static int ext4_unlink(struct inode * dir, struct dentry *dentry)
2130 int retval;
2131 struct inode * inode;
2132 struct buffer_head * bh;
2133 struct ext4_dir_entry_2 * de;
2134 handle_t *handle;
2136 /* Initialize quotas before so that eventual writes go
2137 * in separate transaction */
2138 DQUOT_INIT(dentry->d_inode);
2139 handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
2140 if (IS_ERR(handle))
2141 return PTR_ERR(handle);
2143 if (IS_DIRSYNC(dir))
2144 handle->h_sync = 1;
2146 retval = -ENOENT;
2147 bh = ext4_find_entry (dentry, &de);
2148 if (!bh)
2149 goto end_unlink;
2151 inode = dentry->d_inode;
2153 retval = -EIO;
2154 if (le32_to_cpu(de->inode) != inode->i_ino)
2155 goto end_unlink;
2157 if (!inode->i_nlink) {
2158 ext4_warning (inode->i_sb, "ext4_unlink",
2159 "Deleting nonexistent file (%lu), %d",
2160 inode->i_ino, inode->i_nlink);
2161 inode->i_nlink = 1;
2163 retval = ext4_delete_entry(handle, dir, de, bh);
2164 if (retval)
2165 goto end_unlink;
2166 dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
2167 ext4_update_dx_flag(dir);
2168 ext4_mark_inode_dirty(handle, dir);
2169 drop_nlink(inode);
2170 if (!inode->i_nlink)
2171 ext4_orphan_add(handle, inode);
2172 inode->i_ctime = dir->i_ctime;
2173 ext4_mark_inode_dirty(handle, inode);
2174 retval = 0;
2176 end_unlink:
2177 ext4_journal_stop(handle);
2178 brelse (bh);
2179 return retval;
2182 static int ext4_symlink (struct inode * dir,
2183 struct dentry *dentry, const char * symname)
2185 handle_t *handle;
2186 struct inode * inode;
2187 int l, err, retries = 0;
2189 l = strlen(symname)+1;
2190 if (l > dir->i_sb->s_blocksize)
2191 return -ENAMETOOLONG;
2193 retry:
2194 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2195 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 5 +
2196 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
2197 if (IS_ERR(handle))
2198 return PTR_ERR(handle);
2200 if (IS_DIRSYNC(dir))
2201 handle->h_sync = 1;
2203 inode = ext4_new_inode (handle, dir, S_IFLNK|S_IRWXUGO);
2204 err = PTR_ERR(inode);
2205 if (IS_ERR(inode))
2206 goto out_stop;
2208 if (l > sizeof (EXT4_I(inode)->i_data)) {
2209 inode->i_op = &ext4_symlink_inode_operations;
2210 ext4_set_aops(inode);
2212 * page_symlink() calls into ext4_prepare/commit_write.
2213 * We have a transaction open. All is sweetness. It also sets
2214 * i_size in generic_commit_write().
2216 err = __page_symlink(inode, symname, l,
2217 mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
2218 if (err) {
2219 drop_nlink(inode);
2220 ext4_mark_inode_dirty(handle, inode);
2221 iput (inode);
2222 goto out_stop;
2224 } else {
2225 inode->i_op = &ext4_fast_symlink_inode_operations;
2226 memcpy((char*)&EXT4_I(inode)->i_data,symname,l);
2227 inode->i_size = l-1;
2229 EXT4_I(inode)->i_disksize = inode->i_size;
2230 err = ext4_add_nondir(handle, dentry, inode);
2231 out_stop:
2232 ext4_journal_stop(handle);
2233 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2234 goto retry;
2235 return err;
2238 static int ext4_link (struct dentry * old_dentry,
2239 struct inode * dir, struct dentry *dentry)
2241 handle_t *handle;
2242 struct inode *inode = old_dentry->d_inode;
2243 int err, retries = 0;
2245 if (inode->i_nlink >= EXT4_LINK_MAX)
2246 return -EMLINK;
2248 * Return -ENOENT if we've raced with unlink and i_nlink is 0. Doing
2249 * otherwise has the potential to corrupt the orphan inode list.
2251 if (inode->i_nlink == 0)
2252 return -ENOENT;
2254 retry:
2255 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2256 EXT4_INDEX_EXTRA_TRANS_BLOCKS);
2257 if (IS_ERR(handle))
2258 return PTR_ERR(handle);
2260 if (IS_DIRSYNC(dir))
2261 handle->h_sync = 1;
2263 inode->i_ctime = CURRENT_TIME_SEC;
2264 inc_nlink(inode);
2265 atomic_inc(&inode->i_count);
2267 err = ext4_add_nondir(handle, dentry, inode);
2268 ext4_journal_stop(handle);
2269 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2270 goto retry;
2271 return err;
2274 #define PARENT_INO(buffer) \
2275 ((struct ext4_dir_entry_2 *) ((char *) buffer + \
2276 le16_to_cpu(((struct ext4_dir_entry_2 *) buffer)->rec_len)))->inode
2279 * Anybody can rename anything with this: the permission checks are left to the
2280 * higher-level routines.
2282 static int ext4_rename (struct inode * old_dir, struct dentry *old_dentry,
2283 struct inode * new_dir,struct dentry *new_dentry)
2285 handle_t *handle;
2286 struct inode * old_inode, * new_inode;
2287 struct buffer_head * old_bh, * new_bh, * dir_bh;
2288 struct ext4_dir_entry_2 * old_de, * new_de;
2289 int retval;
2291 old_bh = new_bh = dir_bh = NULL;
2293 /* Initialize quotas before so that eventual writes go
2294 * in separate transaction */
2295 if (new_dentry->d_inode)
2296 DQUOT_INIT(new_dentry->d_inode);
2297 handle = ext4_journal_start(old_dir, 2 *
2298 EXT4_DATA_TRANS_BLOCKS(old_dir->i_sb) +
2299 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
2300 if (IS_ERR(handle))
2301 return PTR_ERR(handle);
2303 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
2304 handle->h_sync = 1;
2306 old_bh = ext4_find_entry (old_dentry, &old_de);
2308 * Check for inode number is _not_ due to possible IO errors.
2309 * We might rmdir the source, keep it as pwd of some process
2310 * and merrily kill the link to whatever was created under the
2311 * same name. Goodbye sticky bit ;-<
2313 old_inode = old_dentry->d_inode;
2314 retval = -ENOENT;
2315 if (!old_bh || le32_to_cpu(old_de->inode) != old_inode->i_ino)
2316 goto end_rename;
2318 new_inode = new_dentry->d_inode;
2319 new_bh = ext4_find_entry (new_dentry, &new_de);
2320 if (new_bh) {
2321 if (!new_inode) {
2322 brelse (new_bh);
2323 new_bh = NULL;
2326 if (S_ISDIR(old_inode->i_mode)) {
2327 if (new_inode) {
2328 retval = -ENOTEMPTY;
2329 if (!empty_dir (new_inode))
2330 goto end_rename;
2332 retval = -EIO;
2333 dir_bh = ext4_bread (handle, old_inode, 0, 0, &retval);
2334 if (!dir_bh)
2335 goto end_rename;
2336 if (le32_to_cpu(PARENT_INO(dir_bh->b_data)) != old_dir->i_ino)
2337 goto end_rename;
2338 retval = -EMLINK;
2339 if (!new_inode && new_dir!=old_dir &&
2340 new_dir->i_nlink >= EXT4_LINK_MAX)
2341 goto end_rename;
2343 if (!new_bh) {
2344 retval = ext4_add_entry (handle, new_dentry, old_inode);
2345 if (retval)
2346 goto end_rename;
2347 } else {
2348 BUFFER_TRACE(new_bh, "get write access");
2349 ext4_journal_get_write_access(handle, new_bh);
2350 new_de->inode = cpu_to_le32(old_inode->i_ino);
2351 if (EXT4_HAS_INCOMPAT_FEATURE(new_dir->i_sb,
2352 EXT4_FEATURE_INCOMPAT_FILETYPE))
2353 new_de->file_type = old_de->file_type;
2354 new_dir->i_version++;
2355 BUFFER_TRACE(new_bh, "call ext4_journal_dirty_metadata");
2356 ext4_journal_dirty_metadata(handle, new_bh);
2357 brelse(new_bh);
2358 new_bh = NULL;
2362 * Like most other Unix systems, set the ctime for inodes on a
2363 * rename.
2365 old_inode->i_ctime = CURRENT_TIME_SEC;
2366 ext4_mark_inode_dirty(handle, old_inode);
2369 * ok, that's it
2371 if (le32_to_cpu(old_de->inode) != old_inode->i_ino ||
2372 old_de->name_len != old_dentry->d_name.len ||
2373 strncmp(old_de->name, old_dentry->d_name.name, old_de->name_len) ||
2374 (retval = ext4_delete_entry(handle, old_dir,
2375 old_de, old_bh)) == -ENOENT) {
2376 /* old_de could have moved from under us during htree split, so
2377 * make sure that we are deleting the right entry. We might
2378 * also be pointing to a stale entry in the unused part of
2379 * old_bh so just checking inum and the name isn't enough. */
2380 struct buffer_head *old_bh2;
2381 struct ext4_dir_entry_2 *old_de2;
2383 old_bh2 = ext4_find_entry(old_dentry, &old_de2);
2384 if (old_bh2) {
2385 retval = ext4_delete_entry(handle, old_dir,
2386 old_de2, old_bh2);
2387 brelse(old_bh2);
2390 if (retval) {
2391 ext4_warning(old_dir->i_sb, "ext4_rename",
2392 "Deleting old file (%lu), %d, error=%d",
2393 old_dir->i_ino, old_dir->i_nlink, retval);
2396 if (new_inode) {
2397 drop_nlink(new_inode);
2398 new_inode->i_ctime = CURRENT_TIME_SEC;
2400 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME_SEC;
2401 ext4_update_dx_flag(old_dir);
2402 if (dir_bh) {
2403 BUFFER_TRACE(dir_bh, "get_write_access");
2404 ext4_journal_get_write_access(handle, dir_bh);
2405 PARENT_INO(dir_bh->b_data) = cpu_to_le32(new_dir->i_ino);
2406 BUFFER_TRACE(dir_bh, "call ext4_journal_dirty_metadata");
2407 ext4_journal_dirty_metadata(handle, dir_bh);
2408 drop_nlink(old_dir);
2409 if (new_inode) {
2410 drop_nlink(new_inode);
2411 } else {
2412 inc_nlink(new_dir);
2413 ext4_update_dx_flag(new_dir);
2414 ext4_mark_inode_dirty(handle, new_dir);
2417 ext4_mark_inode_dirty(handle, old_dir);
2418 if (new_inode) {
2419 ext4_mark_inode_dirty(handle, new_inode);
2420 if (!new_inode->i_nlink)
2421 ext4_orphan_add(handle, new_inode);
2423 retval = 0;
2425 end_rename:
2426 brelse (dir_bh);
2427 brelse (old_bh);
2428 brelse (new_bh);
2429 ext4_journal_stop(handle);
2430 return retval;
2434 * directories can handle most operations...
2436 const struct inode_operations ext4_dir_inode_operations = {
2437 .create = ext4_create,
2438 .lookup = ext4_lookup,
2439 .link = ext4_link,
2440 .unlink = ext4_unlink,
2441 .symlink = ext4_symlink,
2442 .mkdir = ext4_mkdir,
2443 .rmdir = ext4_rmdir,
2444 .mknod = ext4_mknod,
2445 .rename = ext4_rename,
2446 .setattr = ext4_setattr,
2447 #ifdef CONFIG_EXT4DEV_FS_XATTR
2448 .setxattr = generic_setxattr,
2449 .getxattr = generic_getxattr,
2450 .listxattr = ext4_listxattr,
2451 .removexattr = generic_removexattr,
2452 #endif
2453 .permission = ext4_permission,
2456 const struct inode_operations ext4_special_inode_operations = {
2457 .setattr = ext4_setattr,
2458 #ifdef CONFIG_EXT4DEV_FS_XATTR
2459 .setxattr = generic_setxattr,
2460 .getxattr = generic_getxattr,
2461 .listxattr = ext4_listxattr,
2462 .removexattr = generic_removexattr,
2463 #endif
2464 .permission = ext4_permission,