2 * linux/fs/ext3/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)
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
27 #include <linux/quotaops.h>
34 * define how far ahead to read directories while searching them.
36 #define NAMEI_RA_CHUNKS 2
37 #define NAMEI_RA_BLOCKS 4
38 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
39 #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
41 static struct buffer_head *ext3_append(handle_t *handle,
45 struct buffer_head *bh;
47 *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
49 bh = ext3_bread(handle, inode, *block, 1, err);
51 inode->i_size += inode->i_sb->s_blocksize;
52 EXT3_I(inode)->i_disksize = inode->i_size;
53 *err = ext3_journal_get_write_access(handle, bh);
63 #define assert(test) J_ASSERT(test)
67 #define dxtrace(command) command
69 #define dxtrace(command)
93 * dx_root_info is laid out so that if it should somehow get overlaid by a
94 * dirent the two low bits of the hash version will be zero. Therefore, the
95 * hash version mod 4 should never be 0. Sincerely, the paranoia department.
100 struct fake_dirent dot;
102 struct fake_dirent dotdot;
106 __le32 reserved_zero;
108 u8 info_length; /* 8 */
113 struct dx_entry entries[0];
118 struct fake_dirent fake;
119 struct dx_entry entries[0];
125 struct buffer_head *bh;
126 struct dx_entry *entries;
137 static inline unsigned dx_get_block (struct dx_entry *entry);
138 static void dx_set_block (struct dx_entry *entry, unsigned value);
139 static inline unsigned dx_get_hash (struct dx_entry *entry);
140 static void dx_set_hash (struct dx_entry *entry, unsigned value);
141 static unsigned dx_get_count (struct dx_entry *entries);
142 static unsigned dx_get_limit (struct dx_entry *entries);
143 static void dx_set_count (struct dx_entry *entries, unsigned value);
144 static void dx_set_limit (struct dx_entry *entries, unsigned value);
145 static unsigned dx_root_limit (struct inode *dir, unsigned infosize);
146 static unsigned dx_node_limit (struct inode *dir);
147 static struct dx_frame *dx_probe(struct qstr *entry,
149 struct dx_hash_info *hinfo,
150 struct dx_frame *frame,
152 static void dx_release (struct dx_frame *frames);
153 static int dx_make_map(struct ext3_dir_entry_2 *de, unsigned blocksize,
154 struct dx_hash_info *hinfo, struct dx_map_entry map[]);
155 static void dx_sort_map(struct dx_map_entry *map, unsigned count);
156 static struct ext3_dir_entry_2 *dx_move_dirents (char *from, char *to,
157 struct dx_map_entry *offsets, int count);
158 static struct ext3_dir_entry_2 *dx_pack_dirents(char *base, unsigned blocksize);
159 static void dx_insert_block (struct dx_frame *frame, u32 hash, u32 block);
160 static int ext3_htree_next_block(struct inode *dir, __u32 hash,
161 struct dx_frame *frame,
162 struct dx_frame *frames,
164 static struct buffer_head * ext3_dx_find_entry(struct inode *dir,
165 struct qstr *entry, struct ext3_dir_entry_2 **res_dir,
167 static int ext3_dx_add_entry(handle_t *handle, struct dentry *dentry,
168 struct inode *inode);
171 * p is at least 6 bytes before the end of page
173 static inline struct ext3_dir_entry_2 *
174 ext3_next_entry(struct ext3_dir_entry_2 *p)
176 return (struct ext3_dir_entry_2 *)((char *)p +
177 ext3_rec_len_from_disk(p->rec_len));
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 - EXT3_DIR_REC_LEN(1) -
228 EXT3_DIR_REC_LEN(2) - infosize;
229 return 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 - EXT3_DIR_REC_LEN(0);
235 return entry_space / sizeof(struct dx_entry);
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++)
248 printk("%x->%u ", i? dx_get_hash(entries + i): 0, dx_get_block(entries + i));
260 static struct stats dx_show_leaf(struct dx_hash_info *hinfo, struct ext3_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;
268 while ((char *) de < base + size)
274 int len = de->name_len;
275 char *name = de->name;
276 while (len--) printk("%c", *name++);
277 ext3fs_dirhash(de->name, de->name_len, &h);
278 printk(":%x.%u ", h.hash,
279 (unsigned) ((char *) de - base));
281 space += EXT3_DIR_REC_LEN(de->name_len);
284 de = ext3_next_entry(de);
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;
296 struct buffer_head *bh;
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;
304 printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
305 if (!(bh = ext3_bread (NULL,dir, block, 0,&err))) continue;
307 dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
308 dx_show_leaf(hinfo, (struct ext3_dir_entry_2 *) bh->b_data, blocksize, 0);
309 names += stats.names;
310 space += stats.space;
311 bcount += stats.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
330 static struct dx_frame *
331 dx_probe(struct qstr *entry, 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;
342 if (!(bh = ext3_bread (NULL,dir, 0, 0, err)))
344 root = (struct dx_root *) bh->b_data;
345 if (root->info.hash_version != DX_HASH_TEA &&
346 root->info.hash_version != DX_HASH_HALF_MD4 &&
347 root->info.hash_version != DX_HASH_LEGACY) {
348 ext3_warning(dir->i_sb, __func__,
349 "Unrecognised inode hash code %d",
350 root->info.hash_version);
352 *err = ERR_BAD_DX_DIR;
355 hinfo->hash_version = root->info.hash_version;
356 if (hinfo->hash_version <= DX_HASH_TEA)
357 hinfo->hash_version += EXT3_SB(dir->i_sb)->s_hash_unsigned;
358 hinfo->seed = EXT3_SB(dir->i_sb)->s_hash_seed;
360 ext3fs_dirhash(entry->name, entry->len, hinfo);
363 if (root->info.unused_flags & 1) {
364 ext3_warning(dir->i_sb, __func__,
365 "Unimplemented inode hash flags: %#06x",
366 root->info.unused_flags);
368 *err = ERR_BAD_DX_DIR;
372 if ((indirect = root->info.indirect_levels) > 1) {
373 ext3_warning(dir->i_sb, __func__,
374 "Unimplemented inode hash depth: %#06x",
375 root->info.indirect_levels);
377 *err = ERR_BAD_DX_DIR;
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 ext3_warning(dir->i_sb, __func__,
387 "dx entry: limit != root limit");
389 *err = ERR_BAD_DX_DIR;
393 dxtrace (printk("Look up %x", hash));
396 count = dx_get_count(entries);
397 if (!count || count > dx_get_limit(entries)) {
398 ext3_warning(dir->i_sb, __func__,
399 "dx entry: no count or count > limit");
401 *err = ERR_BAD_DX_DIR;
406 q = entries + count - 1;
410 dxtrace(printk("."));
411 if (dx_get_hash(m) > hash)
417 if (0) // linear search cross check
419 unsigned n = count - 1;
423 dxtrace(printk(","));
424 if (dx_get_hash(++at) > hash)
430 assert (at == p - 1);
434 dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
436 frame->entries = entries;
438 if (!indirect--) return frame;
439 if (!(bh = ext3_bread (NULL,dir, dx_get_block(at), 0, err)))
441 at = entries = ((struct dx_node *) bh->b_data)->entries;
442 if (dx_get_limit(entries) != dx_node_limit (dir)) {
443 ext3_warning(dir->i_sb, __func__,
444 "dx entry: limit != node limit");
446 *err = ERR_BAD_DX_DIR;
453 while (frame >= frame_in) {
458 if (*err == ERR_BAD_DX_DIR)
459 ext3_warning(dir->i_sb, __func__,
460 "Corrupt dir inode %ld, running e2fsck is "
461 "recommended.", dir->i_ino);
465 static void dx_release (struct dx_frame *frames)
467 if (frames[0].bh == NULL)
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 ext3_htree_next_block(struct inode *dir, __u32 hash,
493 struct dx_frame *frame,
494 struct dx_frame *frames,
498 struct buffer_head *bh;
499 int err, num_frames = 0;
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.
511 if (++(p->at) < p->entries + dx_get_count(p->entries))
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);
529 if ((hash & 1) == 0) {
530 if ((bhash & ~1) != hash)
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 = ext3_bread(NULL, dir, dx_get_block(p->at),
540 return err; /* Failure */
544 p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
551 * This function fills a red-black tree with information from a
552 * directory block. It returns the number directory entries loaded
553 * into the tree. If there is an error it is returned in err.
555 static int htree_dirblock_to_tree(struct file *dir_file,
556 struct inode *dir, int block,
557 struct dx_hash_info *hinfo,
558 __u32 start_hash, __u32 start_minor_hash)
560 struct buffer_head *bh;
561 struct ext3_dir_entry_2 *de, *top;
564 dxtrace(printk("In htree dirblock_to_tree: block %d\n", block));
565 if (!(bh = ext3_bread (NULL, dir, block, 0, &err)))
568 de = (struct ext3_dir_entry_2 *) bh->b_data;
569 top = (struct ext3_dir_entry_2 *) ((char *) de +
570 dir->i_sb->s_blocksize -
571 EXT3_DIR_REC_LEN(0));
572 for (; de < top; de = ext3_next_entry(de)) {
573 if (!ext3_check_dir_entry("htree_dirblock_to_tree", dir, de, bh,
574 (block<<EXT3_BLOCK_SIZE_BITS(dir->i_sb))
575 +((char *)de - bh->b_data))) {
576 /* On error, skip the f_pos to the next block. */
577 dir_file->f_pos = (dir_file->f_pos |
578 (dir->i_sb->s_blocksize - 1)) + 1;
582 ext3fs_dirhash(de->name, de->name_len, hinfo);
583 if ((hinfo->hash < start_hash) ||
584 ((hinfo->hash == start_hash) &&
585 (hinfo->minor_hash < start_minor_hash)))
589 if ((err = ext3_htree_store_dirent(dir_file,
590 hinfo->hash, hinfo->minor_hash, de)) != 0) {
602 * This function fills a red-black tree with information from a
603 * directory. We start scanning the directory in hash order, starting
604 * at start_hash and start_minor_hash.
606 * This function returns the number of entries inserted into the tree,
607 * or a negative error code.
609 int ext3_htree_fill_tree(struct file *dir_file, __u32 start_hash,
610 __u32 start_minor_hash, __u32 *next_hash)
612 struct dx_hash_info hinfo;
613 struct ext3_dir_entry_2 *de;
614 struct dx_frame frames[2], *frame;
621 dxtrace(printk("In htree_fill_tree, start hash: %x:%x\n", start_hash,
623 dir = dir_file->f_path.dentry->d_inode;
624 if (!(EXT3_I(dir)->i_flags & EXT3_INDEX_FL)) {
625 hinfo.hash_version = EXT3_SB(dir->i_sb)->s_def_hash_version;
626 if (hinfo.hash_version <= DX_HASH_TEA)
627 hinfo.hash_version +=
628 EXT3_SB(dir->i_sb)->s_hash_unsigned;
629 hinfo.seed = EXT3_SB(dir->i_sb)->s_hash_seed;
630 count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
631 start_hash, start_minor_hash);
635 hinfo.hash = start_hash;
636 hinfo.minor_hash = 0;
637 frame = dx_probe(NULL, dir_file->f_path.dentry->d_inode, &hinfo, frames, &err);
641 /* Add '.' and '..' from the htree header */
642 if (!start_hash && !start_minor_hash) {
643 de = (struct ext3_dir_entry_2 *) frames[0].bh->b_data;
644 if ((err = ext3_htree_store_dirent(dir_file, 0, 0, de)) != 0)
648 if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
649 de = (struct ext3_dir_entry_2 *) frames[0].bh->b_data;
650 de = ext3_next_entry(de);
651 if ((err = ext3_htree_store_dirent(dir_file, 2, 0, de)) != 0)
657 block = dx_get_block(frame->at);
658 ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
659 start_hash, start_minor_hash);
666 ret = ext3_htree_next_block(dir, HASH_NB_ALWAYS,
667 frame, frames, &hashval);
668 *next_hash = hashval;
674 * Stop if: (a) there are no more entries, or
675 * (b) we have inserted at least one entry and the
676 * next hash value is not a continuation
679 (count && ((hashval & 1) == 0)))
683 dxtrace(printk("Fill tree: returned %d entries, next hash: %x\n",
693 * Directory block splitting, compacting
697 * Create map of hash values, offsets, and sizes, stored at end of block.
698 * Returns number of entries mapped.
700 static int dx_make_map(struct ext3_dir_entry_2 *de, unsigned blocksize,
701 struct dx_hash_info *hinfo, struct dx_map_entry *map_tail)
704 char *base = (char *) de;
705 struct dx_hash_info h = *hinfo;
707 while ((char *) de < base + blocksize)
709 if (de->name_len && de->inode) {
710 ext3fs_dirhash(de->name, de->name_len, &h);
712 map_tail->hash = h.hash;
713 map_tail->offs = (u16) ((char *) de - base);
714 map_tail->size = le16_to_cpu(de->rec_len);
718 /* XXX: do we need to check rec_len == 0 case? -Chris */
719 de = ext3_next_entry(de);
724 /* Sort map by hash value */
725 static void dx_sort_map (struct dx_map_entry *map, unsigned count)
727 struct dx_map_entry *p, *q, *top = map + count - 1;
729 /* Combsort until bubble sort doesn't suck */
733 if (count - 9 < 2) /* 9, 10 -> 11 */
735 for (p = top, q = p - count; q >= map; p--, q--)
736 if (p->hash < q->hash)
739 /* Garden variety bubble sort */
745 if (q[1].hash >= q[0].hash)
753 static void dx_insert_block(struct dx_frame *frame, u32 hash, u32 block)
755 struct dx_entry *entries = frame->entries;
756 struct dx_entry *old = frame->at, *new = old + 1;
757 int count = dx_get_count(entries);
759 assert(count < dx_get_limit(entries));
760 assert(old < entries + count);
761 memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
762 dx_set_hash(new, hash);
763 dx_set_block(new, block);
764 dx_set_count(entries, count + 1);
767 static void ext3_update_dx_flag(struct inode *inode)
769 if (!EXT3_HAS_COMPAT_FEATURE(inode->i_sb,
770 EXT3_FEATURE_COMPAT_DIR_INDEX))
771 EXT3_I(inode)->i_flags &= ~EXT3_INDEX_FL;
775 * NOTE! unlike strncmp, ext3_match returns 1 for success, 0 for failure.
777 * `len <= EXT3_NAME_LEN' is guaranteed by caller.
778 * `de != NULL' is guaranteed by caller.
780 static inline int ext3_match (int len, const char * const name,
781 struct ext3_dir_entry_2 * de)
783 if (len != de->name_len)
787 return !memcmp(name, de->name, len);
791 * Returns 0 if not found, -1 on failure, and 1 on success
793 static inline int search_dirblock(struct buffer_head * bh,
796 unsigned long offset,
797 struct ext3_dir_entry_2 ** res_dir)
799 struct ext3_dir_entry_2 * de;
802 const char *name = child->name;
803 int namelen = child->len;
805 de = (struct ext3_dir_entry_2 *) bh->b_data;
806 dlimit = bh->b_data + dir->i_sb->s_blocksize;
807 while ((char *) de < dlimit) {
808 /* this code is executed quadratically often */
809 /* do minimal checking `by hand' */
811 if ((char *) de + namelen <= dlimit &&
812 ext3_match (namelen, name, de)) {
813 /* found a match - just to be sure, do a full check */
814 if (!ext3_check_dir_entry("ext3_find_entry",
815 dir, de, bh, offset))
820 /* prevent looping on a bad block */
821 de_len = ext3_rec_len_from_disk(de->rec_len);
825 de = (struct ext3_dir_entry_2 *) ((char *) de + de_len);
834 * finds an entry in the specified directory with the wanted name. It
835 * returns the cache buffer in which the entry was found, and the entry
836 * itself (as a parameter - res_dir). It does NOT read the inode of the
837 * entry - you'll have to do that yourself if you want to.
839 * The returned buffer_head has ->b_count elevated. The caller is expected
840 * to brelse() it when appropriate.
842 static struct buffer_head *ext3_find_entry(struct inode *dir,
844 struct ext3_dir_entry_2 **res_dir)
846 struct super_block * sb;
847 struct buffer_head * bh_use[NAMEI_RA_SIZE];
848 struct buffer_head * bh, *ret = NULL;
849 unsigned long start, block, b;
850 const u8 *name = entry->name;
851 int ra_max = 0; /* Number of bh's in the readahead
853 int ra_ptr = 0; /* Current index into readahead
861 namelen = entry->len;
862 if (namelen > EXT3_NAME_LEN)
864 if ((namelen <= 2) && (name[0] == '.') &&
865 (name[1] == '.' || name[1] == 0)) {
867 * "." or ".." will only be in the first block
868 * NFS may look up ".."; "." should be handled by the VFS
875 bh = ext3_dx_find_entry(dir, entry, res_dir, &err);
877 * On success, or if the error was file not found,
878 * return. Otherwise, fall back to doing a search the
881 if (bh || (err != ERR_BAD_DX_DIR))
883 dxtrace(printk("ext3_find_entry: dx failed, falling back\n"));
885 nblocks = dir->i_size >> EXT3_BLOCK_SIZE_BITS(sb);
886 start = EXT3_I(dir)->i_dir_start_lookup;
887 if (start >= nblocks)
893 * We deal with the read-ahead logic here.
895 if (ra_ptr >= ra_max) {
896 /* Refill the readahead buffer */
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;
910 bh = ext3_getblk(NULL, dir, b++, 0, &err);
912 if (bh && !bh_uptodate_or_lock(bh)) {
914 bh->b_end_io = end_buffer_read_sync;
915 submit_bh(READ | REQ_META | REQ_PRIO,
920 if ((bh = bh_use[ra_ptr++]) == NULL)
923 if (!buffer_uptodate(bh)) {
924 /* read error, skip block & hope for the best */
925 ext3_error(sb, __func__, "reading directory #%lu "
926 "offset %lu", dir->i_ino, block);
930 i = search_dirblock(bh, dir, entry,
931 block << EXT3_BLOCK_SIZE_BITS(sb), res_dir);
933 EXT3_I(dir)->i_dir_start_lookup = block;
935 goto cleanup_and_exit;
939 goto cleanup_and_exit;
942 if (++block >= nblocks)
944 } while (block != start);
947 * If the directory has grown while we were searching, then
948 * search the last part of the directory before giving up.
951 nblocks = dir->i_size >> EXT3_BLOCK_SIZE_BITS(sb);
952 if (block < nblocks) {
958 /* Clean up the read-ahead blocks */
959 for (; ra_ptr < ra_max; ra_ptr++)
960 brelse (bh_use[ra_ptr]);
964 static struct buffer_head * ext3_dx_find_entry(struct inode *dir,
965 struct qstr *entry, struct ext3_dir_entry_2 **res_dir,
968 struct super_block *sb = dir->i_sb;
969 struct dx_hash_info hinfo;
970 struct dx_frame frames[2], *frame;
971 struct buffer_head *bh;
975 if (!(frame = dx_probe(entry, dir, &hinfo, frames, err)))
978 block = dx_get_block(frame->at);
979 if (!(bh = ext3_bread (NULL,dir, block, 0, err)))
982 retval = search_dirblock(bh, dir, entry,
983 block << EXT3_BLOCK_SIZE_BITS(sb),
991 *err = ERR_BAD_DX_DIR;
995 /* Check to see if we should continue to search */
996 retval = ext3_htree_next_block(dir, hinfo.hash, frame,
999 ext3_warning(sb, __func__,
1000 "error reading index page in directory #%lu",
1005 } while (retval == 1);
1009 dxtrace(printk("%s not found\n", entry->name));
1010 dx_release (frames);
1014 static struct dentry *ext3_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
1016 struct inode * inode;
1017 struct ext3_dir_entry_2 * de;
1018 struct buffer_head * bh;
1020 if (dentry->d_name.len > EXT3_NAME_LEN)
1021 return ERR_PTR(-ENAMETOOLONG);
1023 bh = ext3_find_entry(dir, &dentry->d_name, &de);
1026 unsigned long ino = le32_to_cpu(de->inode);
1028 if (!ext3_valid_inum(dir->i_sb, ino)) {
1029 ext3_error(dir->i_sb, "ext3_lookup",
1030 "bad inode number: %lu", ino);
1031 return ERR_PTR(-EIO);
1033 inode = ext3_iget(dir->i_sb, ino);
1034 if (inode == ERR_PTR(-ESTALE)) {
1035 ext3_error(dir->i_sb, __func__,
1036 "deleted inode referenced: %lu",
1038 return ERR_PTR(-EIO);
1041 return d_splice_alias(inode, dentry);
1045 struct dentry *ext3_get_parent(struct dentry *child)
1048 struct qstr dotdot = QSTR_INIT("..", 2);
1049 struct ext3_dir_entry_2 * de;
1050 struct buffer_head *bh;
1052 bh = ext3_find_entry(child->d_inode, &dotdot, &de);
1054 return ERR_PTR(-ENOENT);
1055 ino = le32_to_cpu(de->inode);
1058 if (!ext3_valid_inum(child->d_inode->i_sb, ino)) {
1059 ext3_error(child->d_inode->i_sb, "ext3_get_parent",
1060 "bad inode number: %lu", ino);
1061 return ERR_PTR(-EIO);
1064 return d_obtain_alias(ext3_iget(child->d_inode->i_sb, ino));
1068 static unsigned char ext3_type_by_mode[S_IFMT >> S_SHIFT] = {
1069 [S_IFREG >> S_SHIFT] = EXT3_FT_REG_FILE,
1070 [S_IFDIR >> S_SHIFT] = EXT3_FT_DIR,
1071 [S_IFCHR >> S_SHIFT] = EXT3_FT_CHRDEV,
1072 [S_IFBLK >> S_SHIFT] = EXT3_FT_BLKDEV,
1073 [S_IFIFO >> S_SHIFT] = EXT3_FT_FIFO,
1074 [S_IFSOCK >> S_SHIFT] = EXT3_FT_SOCK,
1075 [S_IFLNK >> S_SHIFT] = EXT3_FT_SYMLINK,
1078 static inline void ext3_set_de_type(struct super_block *sb,
1079 struct ext3_dir_entry_2 *de,
1081 if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_FILETYPE))
1082 de->file_type = ext3_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
1086 * Move count entries from end of map between two memory locations.
1087 * Returns pointer to last entry moved.
1089 static struct ext3_dir_entry_2 *
1090 dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count)
1092 unsigned rec_len = 0;
1095 struct ext3_dir_entry_2 *de = (struct ext3_dir_entry_2 *) (from + map->offs);
1096 rec_len = EXT3_DIR_REC_LEN(de->name_len);
1097 memcpy (to, de, rec_len);
1098 ((struct ext3_dir_entry_2 *) to)->rec_len =
1099 ext3_rec_len_to_disk(rec_len);
1104 return (struct ext3_dir_entry_2 *) (to - rec_len);
1108 * Compact each dir entry in the range to the minimal rec_len.
1109 * Returns pointer to last entry in range.
1111 static struct ext3_dir_entry_2 *dx_pack_dirents(char *base, unsigned blocksize)
1113 struct ext3_dir_entry_2 *next, *to, *prev;
1114 struct ext3_dir_entry_2 *de = (struct ext3_dir_entry_2 *)base;
1115 unsigned rec_len = 0;
1118 while ((char *)de < base + blocksize) {
1119 next = ext3_next_entry(de);
1120 if (de->inode && de->name_len) {
1121 rec_len = EXT3_DIR_REC_LEN(de->name_len);
1123 memmove(to, de, rec_len);
1124 to->rec_len = ext3_rec_len_to_disk(rec_len);
1126 to = (struct ext3_dir_entry_2 *) (((char *) to) + rec_len);
1134 * Split a full leaf block to make room for a new dir entry.
1135 * Allocate a new block, and move entries so that they are approx. equally full.
1136 * Returns pointer to de in block into which the new entry will be inserted.
1138 static struct ext3_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
1139 struct buffer_head **bh,struct dx_frame *frame,
1140 struct dx_hash_info *hinfo, int *error)
1142 unsigned blocksize = dir->i_sb->s_blocksize;
1143 unsigned count, continued;
1144 struct buffer_head *bh2;
1147 struct dx_map_entry *map;
1148 char *data1 = (*bh)->b_data, *data2;
1149 unsigned split, move, size;
1150 struct ext3_dir_entry_2 *de = NULL, *de2;
1153 bh2 = ext3_append (handle, dir, &newblock, &err);
1160 BUFFER_TRACE(*bh, "get_write_access");
1161 err = ext3_journal_get_write_access(handle, *bh);
1165 BUFFER_TRACE(frame->bh, "get_write_access");
1166 err = ext3_journal_get_write_access(handle, frame->bh);
1170 data2 = bh2->b_data;
1172 /* create map in the end of data2 block */
1173 map = (struct dx_map_entry *) (data2 + blocksize);
1174 count = dx_make_map ((struct ext3_dir_entry_2 *) data1,
1175 blocksize, hinfo, map);
1177 dx_sort_map (map, count);
1178 /* Split the existing block in the middle, size-wise */
1181 for (i = count-1; i >= 0; i--) {
1182 /* is more than half of this entry in 2nd half of the block? */
1183 if (size + map[i].size/2 > blocksize/2)
1185 size += map[i].size;
1188 /* map index at which we will split */
1189 split = count - move;
1190 hash2 = map[split].hash;
1191 continued = hash2 == map[split - 1].hash;
1192 dxtrace(printk("Split block %i at %x, %i/%i\n",
1193 dx_get_block(frame->at), hash2, split, count-split));
1195 /* Fancy dance to stay within two buffers */
1196 de2 = dx_move_dirents(data1, data2, map + split, count - split);
1197 de = dx_pack_dirents(data1,blocksize);
1198 de->rec_len = ext3_rec_len_to_disk(data1 + blocksize - (char *) de);
1199 de2->rec_len = ext3_rec_len_to_disk(data2 + blocksize - (char *) de2);
1200 dxtrace(dx_show_leaf (hinfo, (struct ext3_dir_entry_2 *) data1, blocksize, 1));
1201 dxtrace(dx_show_leaf (hinfo, (struct ext3_dir_entry_2 *) data2, blocksize, 1));
1203 /* Which block gets the new entry? */
1204 if (hinfo->hash >= hash2)
1209 dx_insert_block (frame, hash2 + continued, newblock);
1210 err = ext3_journal_dirty_metadata (handle, bh2);
1213 err = ext3_journal_dirty_metadata (handle, frame->bh);
1217 dxtrace(dx_show_index ("frame", frame->entries));
1224 ext3_std_error(dir->i_sb, err);
1232 * Add a new entry into a directory (leaf) block. If de is non-NULL,
1233 * it points to a directory entry which is guaranteed to be large
1234 * enough for new directory entry. If de is NULL, then
1235 * add_dirent_to_buf will attempt search the directory block for
1236 * space. It will return -ENOSPC if no space is available, and -EIO
1237 * and -EEXIST if directory entry already exists.
1239 * NOTE! bh is NOT released in the case where ENOSPC is returned. In
1240 * all other cases bh is released.
1242 static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
1243 struct inode *inode, struct ext3_dir_entry_2 *de,
1244 struct buffer_head * bh)
1246 struct inode *dir = dentry->d_parent->d_inode;
1247 const char *name = dentry->d_name.name;
1248 int namelen = dentry->d_name.len;
1249 unsigned long offset = 0;
1250 unsigned short reclen;
1251 int nlen, rlen, err;
1254 reclen = EXT3_DIR_REC_LEN(namelen);
1256 de = (struct ext3_dir_entry_2 *)bh->b_data;
1257 top = bh->b_data + dir->i_sb->s_blocksize - reclen;
1258 while ((char *) de <= top) {
1259 if (!ext3_check_dir_entry("ext3_add_entry", dir, de,
1264 if (ext3_match (namelen, name, de)) {
1268 nlen = EXT3_DIR_REC_LEN(de->name_len);
1269 rlen = ext3_rec_len_from_disk(de->rec_len);
1270 if ((de->inode? rlen - nlen: rlen) >= reclen)
1272 de = (struct ext3_dir_entry_2 *)((char *)de + rlen);
1275 if ((char *) de > top)
1278 BUFFER_TRACE(bh, "get_write_access");
1279 err = ext3_journal_get_write_access(handle, bh);
1281 ext3_std_error(dir->i_sb, err);
1286 /* By now the buffer is marked for journaling */
1287 nlen = EXT3_DIR_REC_LEN(de->name_len);
1288 rlen = ext3_rec_len_from_disk(de->rec_len);
1290 struct ext3_dir_entry_2 *de1 = (struct ext3_dir_entry_2 *)((char *)de + nlen);
1291 de1->rec_len = ext3_rec_len_to_disk(rlen - nlen);
1292 de->rec_len = ext3_rec_len_to_disk(nlen);
1295 de->file_type = EXT3_FT_UNKNOWN;
1297 de->inode = cpu_to_le32(inode->i_ino);
1298 ext3_set_de_type(dir->i_sb, de, inode->i_mode);
1301 de->name_len = namelen;
1302 memcpy (de->name, name, namelen);
1304 * XXX shouldn't update any times until successful
1305 * completion of syscall, but too many callers depend
1308 * XXX similarly, too many callers depend on
1309 * ext3_new_inode() setting the times, but error
1310 * recovery deletes the inode, so the worst that can
1311 * happen is that the times are slightly out of date
1312 * and/or different from the directory change time.
1314 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
1315 ext3_update_dx_flag(dir);
1317 ext3_mark_inode_dirty(handle, dir);
1318 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
1319 err = ext3_journal_dirty_metadata(handle, bh);
1321 ext3_std_error(dir->i_sb, err);
1327 * This converts a one block unindexed directory to a 3 block indexed
1328 * directory, and adds the dentry to the indexed directory.
1330 static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
1331 struct inode *inode, struct buffer_head *bh)
1333 struct inode *dir = dentry->d_parent->d_inode;
1334 const char *name = dentry->d_name.name;
1335 int namelen = dentry->d_name.len;
1336 struct buffer_head *bh2;
1337 struct dx_root *root;
1338 struct dx_frame frames[2], *frame;
1339 struct dx_entry *entries;
1340 struct ext3_dir_entry_2 *de, *de2;
1345 struct dx_hash_info hinfo;
1347 struct fake_dirent *fde;
1349 blocksize = dir->i_sb->s_blocksize;
1350 dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino));
1351 retval = ext3_journal_get_write_access(handle, bh);
1353 ext3_std_error(dir->i_sb, retval);
1357 root = (struct dx_root *) bh->b_data;
1359 /* The 0th block becomes the root, move the dirents out */
1360 fde = &root->dotdot;
1361 de = (struct ext3_dir_entry_2 *)((char *)fde +
1362 ext3_rec_len_from_disk(fde->rec_len));
1363 if ((char *) de >= (((char *) root) + blocksize)) {
1364 ext3_error(dir->i_sb, __func__,
1365 "invalid rec_len for '..' in inode %lu",
1370 len = ((char *) root) + blocksize - (char *) de;
1372 bh2 = ext3_append (handle, dir, &block, &retval);
1377 EXT3_I(dir)->i_flags |= EXT3_INDEX_FL;
1378 data1 = bh2->b_data;
1380 memcpy (data1, de, len);
1381 de = (struct ext3_dir_entry_2 *) data1;
1383 while ((char *)(de2 = ext3_next_entry(de)) < top)
1385 de->rec_len = ext3_rec_len_to_disk(data1 + blocksize - (char *) de);
1386 /* Initialize the root; the dot dirents already exist */
1387 de = (struct ext3_dir_entry_2 *) (&root->dotdot);
1388 de->rec_len = ext3_rec_len_to_disk(blocksize - EXT3_DIR_REC_LEN(2));
1389 memset (&root->info, 0, sizeof(root->info));
1390 root->info.info_length = sizeof(root->info);
1391 root->info.hash_version = EXT3_SB(dir->i_sb)->s_def_hash_version;
1392 entries = root->entries;
1393 dx_set_block (entries, 1);
1394 dx_set_count (entries, 1);
1395 dx_set_limit (entries, dx_root_limit(dir, sizeof(root->info)));
1397 /* Initialize as for dx_probe */
1398 hinfo.hash_version = root->info.hash_version;
1399 if (hinfo.hash_version <= DX_HASH_TEA)
1400 hinfo.hash_version += EXT3_SB(dir->i_sb)->s_hash_unsigned;
1401 hinfo.seed = EXT3_SB(dir->i_sb)->s_hash_seed;
1402 ext3fs_dirhash(name, namelen, &hinfo);
1404 frame->entries = entries;
1405 frame->at = entries;
1409 * Mark buffers dirty here so that if do_split() fails we write a
1410 * consistent set of buffers to disk.
1412 ext3_journal_dirty_metadata(handle, frame->bh);
1413 ext3_journal_dirty_metadata(handle, bh);
1414 de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
1416 ext3_mark_inode_dirty(handle, dir);
1422 return add_dirent_to_buf(handle, dentry, inode, de, bh);
1428 * adds a file entry to the specified directory, using the same
1429 * semantics as ext3_find_entry(). It returns NULL if it failed.
1431 * NOTE!! The inode part of 'de' is left at 0 - which means you
1432 * may not sleep between calling this and putting something into
1433 * the entry, as someone else might have used it while you slept.
1435 static int ext3_add_entry (handle_t *handle, struct dentry *dentry,
1436 struct inode *inode)
1438 struct inode *dir = dentry->d_parent->d_inode;
1439 struct buffer_head * bh;
1440 struct ext3_dir_entry_2 *de;
1441 struct super_block * sb;
1448 blocksize = sb->s_blocksize;
1449 if (!dentry->d_name.len)
1452 retval = ext3_dx_add_entry(handle, dentry, inode);
1453 if (!retval || (retval != ERR_BAD_DX_DIR))
1455 EXT3_I(dir)->i_flags &= ~EXT3_INDEX_FL;
1457 ext3_mark_inode_dirty(handle, dir);
1459 blocks = dir->i_size >> sb->s_blocksize_bits;
1460 for (block = 0; block < blocks; block++) {
1461 bh = ext3_bread(handle, dir, block, 0, &retval);
1464 retval = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
1465 if (retval != -ENOSPC)
1468 if (blocks == 1 && !dx_fallback &&
1469 EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_DIR_INDEX))
1470 return make_indexed_dir(handle, dentry, inode, bh);
1473 bh = ext3_append(handle, dir, &block, &retval);
1476 de = (struct ext3_dir_entry_2 *) bh->b_data;
1478 de->rec_len = ext3_rec_len_to_disk(blocksize);
1479 return add_dirent_to_buf(handle, dentry, inode, de, bh);
1483 * Returns 0 for success, or a negative error value
1485 static int ext3_dx_add_entry(handle_t *handle, struct dentry *dentry,
1486 struct inode *inode)
1488 struct dx_frame frames[2], *frame;
1489 struct dx_entry *entries, *at;
1490 struct dx_hash_info hinfo;
1491 struct buffer_head * bh;
1492 struct inode *dir = dentry->d_parent->d_inode;
1493 struct super_block * sb = dir->i_sb;
1494 struct ext3_dir_entry_2 *de;
1497 frame = dx_probe(&dentry->d_name, dir, &hinfo, frames, &err);
1500 entries = frame->entries;
1503 if (!(bh = ext3_bread(handle,dir, dx_get_block(frame->at), 0, &err)))
1506 BUFFER_TRACE(bh, "get_write_access");
1507 err = ext3_journal_get_write_access(handle, bh);
1511 err = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
1512 if (err != -ENOSPC) {
1517 /* Block full, should compress but for now just split */
1518 dxtrace(printk("using %u of %u node entries\n",
1519 dx_get_count(entries), dx_get_limit(entries)));
1520 /* Need to split index? */
1521 if (dx_get_count(entries) == dx_get_limit(entries)) {
1523 unsigned icount = dx_get_count(entries);
1524 int levels = frame - frames;
1525 struct dx_entry *entries2;
1526 struct dx_node *node2;
1527 struct buffer_head *bh2;
1529 if (levels && (dx_get_count(frames->entries) ==
1530 dx_get_limit(frames->entries))) {
1531 ext3_warning(sb, __func__,
1532 "Directory index full!");
1536 bh2 = ext3_append (handle, dir, &newblock, &err);
1539 node2 = (struct dx_node *)(bh2->b_data);
1540 entries2 = node2->entries;
1541 memset(&node2->fake, 0, sizeof(struct fake_dirent));
1542 node2->fake.rec_len = ext3_rec_len_to_disk(sb->s_blocksize);
1543 BUFFER_TRACE(frame->bh, "get_write_access");
1544 err = ext3_journal_get_write_access(handle, frame->bh);
1548 unsigned icount1 = icount/2, icount2 = icount - icount1;
1549 unsigned hash2 = dx_get_hash(entries + icount1);
1550 dxtrace(printk("Split index %i/%i\n", icount1, icount2));
1552 BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
1553 err = ext3_journal_get_write_access(handle,
1558 memcpy ((char *) entries2, (char *) (entries + icount1),
1559 icount2 * sizeof(struct dx_entry));
1560 dx_set_count (entries, icount1);
1561 dx_set_count (entries2, icount2);
1562 dx_set_limit (entries2, dx_node_limit(dir));
1564 /* Which index block gets the new entry? */
1565 if (at - entries >= icount1) {
1566 frame->at = at = at - entries - icount1 + entries2;
1567 frame->entries = entries = entries2;
1568 swap(frame->bh, bh2);
1570 dx_insert_block (frames + 0, hash2, newblock);
1571 dxtrace(dx_show_index ("node", frames[1].entries));
1572 dxtrace(dx_show_index ("node",
1573 ((struct dx_node *) bh2->b_data)->entries));
1574 err = ext3_journal_dirty_metadata(handle, bh2);
1579 dxtrace(printk("Creating second level index...\n"));
1580 memcpy((char *) entries2, (char *) entries,
1581 icount * sizeof(struct dx_entry));
1582 dx_set_limit(entries2, dx_node_limit(dir));
1585 dx_set_count(entries, 1);
1586 dx_set_block(entries + 0, newblock);
1587 ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
1589 /* Add new access path frame */
1591 frame->at = at = at - entries + entries2;
1592 frame->entries = entries = entries2;
1594 err = ext3_journal_get_write_access(handle,
1599 err = ext3_journal_dirty_metadata(handle, frames[0].bh);
1603 de = do_split(handle, dir, &bh, frame, &hinfo, &err);
1606 err = add_dirent_to_buf(handle, dentry, inode, de, bh);
1611 ext3_std_error(dir->i_sb, err);
1620 * ext3_delete_entry deletes a directory entry by merging it with the
1623 static int ext3_delete_entry (handle_t *handle,
1625 struct ext3_dir_entry_2 * de_del,
1626 struct buffer_head * bh)
1628 struct ext3_dir_entry_2 * de, * pde;
1633 de = (struct ext3_dir_entry_2 *) bh->b_data;
1634 while (i < bh->b_size) {
1635 if (!ext3_check_dir_entry("ext3_delete_entry", dir, de, bh, i))
1640 BUFFER_TRACE(bh, "get_write_access");
1641 err = ext3_journal_get_write_access(handle, bh);
1646 pde->rec_len = ext3_rec_len_to_disk(
1647 ext3_rec_len_from_disk(pde->rec_len) +
1648 ext3_rec_len_from_disk(de->rec_len));
1652 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
1653 err = ext3_journal_dirty_metadata(handle, bh);
1656 ext3_std_error(dir->i_sb, err);
1661 i += ext3_rec_len_from_disk(de->rec_len);
1663 de = ext3_next_entry(de);
1668 static int ext3_add_nondir(handle_t *handle,
1669 struct dentry *dentry, struct inode *inode)
1671 int err = ext3_add_entry(handle, dentry, inode);
1673 ext3_mark_inode_dirty(handle, inode);
1674 d_instantiate(dentry, inode);
1675 unlock_new_inode(inode);
1679 unlock_new_inode(inode);
1685 * By the time this is called, we already have created
1686 * the directory cache entry for the new file, but it
1687 * is so far negative - it has no inode.
1689 * If the create succeeds, we fill in the inode information
1690 * with d_instantiate().
1692 static int ext3_create (struct inode * dir, struct dentry * dentry, umode_t mode,
1693 struct nameidata *nd)
1696 struct inode * inode;
1697 int err, retries = 0;
1699 dquot_initialize(dir);
1702 handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS(dir->i_sb) +
1703 EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3 +
1704 EXT3_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
1706 return PTR_ERR(handle);
1708 if (IS_DIRSYNC(dir))
1711 inode = ext3_new_inode (handle, dir, &dentry->d_name, mode);
1712 err = PTR_ERR(inode);
1713 if (!IS_ERR(inode)) {
1714 inode->i_op = &ext3_file_inode_operations;
1715 inode->i_fop = &ext3_file_operations;
1716 ext3_set_aops(inode);
1717 err = ext3_add_nondir(handle, dentry, inode);
1719 ext3_journal_stop(handle);
1720 if (err == -ENOSPC && ext3_should_retry_alloc(dir->i_sb, &retries))
1725 static int ext3_mknod (struct inode * dir, struct dentry *dentry,
1726 umode_t mode, dev_t rdev)
1729 struct inode *inode;
1730 int err, retries = 0;
1732 if (!new_valid_dev(rdev))
1735 dquot_initialize(dir);
1738 handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS(dir->i_sb) +
1739 EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3 +
1740 EXT3_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
1742 return PTR_ERR(handle);
1744 if (IS_DIRSYNC(dir))
1747 inode = ext3_new_inode (handle, dir, &dentry->d_name, mode);
1748 err = PTR_ERR(inode);
1749 if (!IS_ERR(inode)) {
1750 init_special_inode(inode, inode->i_mode, rdev);
1751 #ifdef CONFIG_EXT3_FS_XATTR
1752 inode->i_op = &ext3_special_inode_operations;
1754 err = ext3_add_nondir(handle, dentry, inode);
1756 ext3_journal_stop(handle);
1757 if (err == -ENOSPC && ext3_should_retry_alloc(dir->i_sb, &retries))
1762 static int ext3_mkdir(struct inode * dir, struct dentry * dentry, umode_t mode)
1765 struct inode * inode;
1766 struct buffer_head * dir_block = NULL;
1767 struct ext3_dir_entry_2 * de;
1768 int err, retries = 0;
1770 if (dir->i_nlink >= EXT3_LINK_MAX)
1773 dquot_initialize(dir);
1776 handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS(dir->i_sb) +
1777 EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3 +
1778 EXT3_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
1780 return PTR_ERR(handle);
1782 if (IS_DIRSYNC(dir))
1785 inode = ext3_new_inode (handle, dir, &dentry->d_name, S_IFDIR | mode);
1786 err = PTR_ERR(inode);
1790 inode->i_op = &ext3_dir_inode_operations;
1791 inode->i_fop = &ext3_dir_operations;
1792 inode->i_size = EXT3_I(inode)->i_disksize = inode->i_sb->s_blocksize;
1793 dir_block = ext3_bread (handle, inode, 0, 1, &err);
1795 goto out_clear_inode;
1797 BUFFER_TRACE(dir_block, "get_write_access");
1798 err = ext3_journal_get_write_access(handle, dir_block);
1800 goto out_clear_inode;
1802 de = (struct ext3_dir_entry_2 *) dir_block->b_data;
1803 de->inode = cpu_to_le32(inode->i_ino);
1805 de->rec_len = ext3_rec_len_to_disk(EXT3_DIR_REC_LEN(de->name_len));
1806 strcpy (de->name, ".");
1807 ext3_set_de_type(dir->i_sb, de, S_IFDIR);
1808 de = ext3_next_entry(de);
1809 de->inode = cpu_to_le32(dir->i_ino);
1810 de->rec_len = ext3_rec_len_to_disk(inode->i_sb->s_blocksize -
1811 EXT3_DIR_REC_LEN(1));
1813 strcpy (de->name, "..");
1814 ext3_set_de_type(dir->i_sb, de, S_IFDIR);
1815 set_nlink(inode, 2);
1816 BUFFER_TRACE(dir_block, "call ext3_journal_dirty_metadata");
1817 err = ext3_journal_dirty_metadata(handle, dir_block);
1819 goto out_clear_inode;
1821 err = ext3_mark_inode_dirty(handle, inode);
1823 err = ext3_add_entry (handle, dentry, inode);
1828 unlock_new_inode(inode);
1829 ext3_mark_inode_dirty(handle, inode);
1834 ext3_update_dx_flag(dir);
1835 err = ext3_mark_inode_dirty(handle, dir);
1837 goto out_clear_inode;
1839 d_instantiate(dentry, inode);
1840 unlock_new_inode(inode);
1843 ext3_journal_stop(handle);
1844 if (err == -ENOSPC && ext3_should_retry_alloc(dir->i_sb, &retries))
1850 * routine to check that the specified directory is empty (for rmdir)
1852 static int empty_dir (struct inode * inode)
1854 unsigned long offset;
1855 struct buffer_head * bh;
1856 struct ext3_dir_entry_2 * de, * de1;
1857 struct super_block * sb;
1861 if (inode->i_size < EXT3_DIR_REC_LEN(1) + EXT3_DIR_REC_LEN(2) ||
1862 !(bh = ext3_bread (NULL, inode, 0, 0, &err))) {
1864 ext3_error(inode->i_sb, __func__,
1865 "error %d reading directory #%lu offset 0",
1868 ext3_warning(inode->i_sb, __func__,
1869 "bad directory (dir #%lu) - no data block",
1873 de = (struct ext3_dir_entry_2 *) bh->b_data;
1874 de1 = ext3_next_entry(de);
1875 if (le32_to_cpu(de->inode) != inode->i_ino ||
1876 !le32_to_cpu(de1->inode) ||
1877 strcmp (".", de->name) ||
1878 strcmp ("..", de1->name)) {
1879 ext3_warning (inode->i_sb, "empty_dir",
1880 "bad directory (dir #%lu) - no `.' or `..'",
1885 offset = ext3_rec_len_from_disk(de->rec_len) +
1886 ext3_rec_len_from_disk(de1->rec_len);
1887 de = ext3_next_entry(de1);
1888 while (offset < inode->i_size ) {
1890 (void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
1893 bh = ext3_bread (NULL, inode,
1894 offset >> EXT3_BLOCK_SIZE_BITS(sb), 0, &err);
1897 ext3_error(sb, __func__,
1898 "error %d reading directory"
1900 err, inode->i_ino, offset);
1901 offset += sb->s_blocksize;
1904 de = (struct ext3_dir_entry_2 *) bh->b_data;
1906 if (!ext3_check_dir_entry("empty_dir", inode, de, bh, offset)) {
1907 de = (struct ext3_dir_entry_2 *)(bh->b_data +
1909 offset = (offset | (sb->s_blocksize - 1)) + 1;
1912 if (le32_to_cpu(de->inode)) {
1916 offset += ext3_rec_len_from_disk(de->rec_len);
1917 de = ext3_next_entry(de);
1923 /* ext3_orphan_add() links an unlinked or truncated inode into a list of
1924 * such inodes, starting at the superblock, in case we crash before the
1925 * file is closed/deleted, or in case the inode truncate spans multiple
1926 * transactions and the last transaction is not recovered after a crash.
1928 * At filesystem recovery time, we walk this list deleting unlinked
1929 * inodes and truncating linked inodes in ext3_orphan_cleanup().
1931 int ext3_orphan_add(handle_t *handle, struct inode *inode)
1933 struct super_block *sb = inode->i_sb;
1934 struct ext3_iloc iloc;
1937 mutex_lock(&EXT3_SB(sb)->s_orphan_lock);
1938 if (!list_empty(&EXT3_I(inode)->i_orphan))
1941 /* Orphan handling is only valid for files with data blocks
1942 * being truncated, or files being unlinked. */
1944 /* @@@ FIXME: Observation from aviro:
1945 * I think I can trigger J_ASSERT in ext3_orphan_add(). We block
1946 * here (on s_orphan_lock), so race with ext3_link() which might bump
1947 * ->i_nlink. For, say it, character device. Not a regular file,
1948 * not a directory, not a symlink and ->i_nlink > 0.
1950 * tytso, 4/25/2009: I'm not sure how that could happen;
1951 * shouldn't the fs core protect us from these sort of
1952 * unlink()/link() races?
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(EXT3_SB(sb)->s_sbh, "get_write_access");
1958 err = ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh);
1962 err = ext3_reserve_inode_write(handle, inode, &iloc);
1966 /* Insert this inode at the head of the on-disk orphan list... */
1967 NEXT_ORPHAN(inode) = le32_to_cpu(EXT3_SB(sb)->s_es->s_last_orphan);
1968 EXT3_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
1969 err = ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
1970 rc = ext3_mark_iloc_dirty(handle, inode, &iloc);
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. */
1983 list_add(&EXT3_I(inode)->i_orphan, &EXT3_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));
1989 mutex_unlock(&EXT3_SB(sb)->s_orphan_lock);
1990 ext3_std_error(inode->i_sb, err);
1995 * ext3_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 ext3_orphan_del(handle_t *handle, struct inode *inode)
2000 struct list_head *prev;
2001 struct ext3_inode_info *ei = EXT3_I(inode);
2002 struct ext3_sb_info *sbi;
2003 unsigned long ino_next;
2004 struct ext3_iloc iloc;
2007 mutex_lock(&EXT3_SB(inode->i_sb)->s_orphan_lock);
2008 if (list_empty(&ei->i_orphan))
2011 ino_next = NEXT_ORPHAN(inode);
2012 prev = ei->i_orphan.prev;
2013 sbi = EXT3_SB(inode->i_sb);
2015 jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
2017 list_del_init(&ei->i_orphan);
2019 /* If we're on an error path, we may not have a valid
2020 * transaction handle with which to update the orphan list on
2021 * disk, but we still need to remove the inode from the linked
2022 * list in memory. */
2026 err = ext3_reserve_inode_write(handle, inode, &iloc);
2030 if (prev == &sbi->s_orphan) {
2031 jbd_debug(4, "superblock will point to %lu\n", ino_next);
2032 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
2033 err = ext3_journal_get_write_access(handle, sbi->s_sbh);
2036 sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
2037 err = ext3_journal_dirty_metadata(handle, sbi->s_sbh);
2039 struct ext3_iloc iloc2;
2040 struct inode *i_prev =
2041 &list_entry(prev, struct ext3_inode_info, i_orphan)->vfs_inode;
2043 jbd_debug(4, "orphan inode %lu will point to %lu\n",
2044 i_prev->i_ino, ino_next);
2045 err = ext3_reserve_inode_write(handle, i_prev, &iloc2);
2048 NEXT_ORPHAN(i_prev) = ino_next;
2049 err = ext3_mark_iloc_dirty(handle, i_prev, &iloc2);
2053 NEXT_ORPHAN(inode) = 0;
2054 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
2057 ext3_std_error(inode->i_sb, err);
2059 mutex_unlock(&EXT3_SB(inode->i_sb)->s_orphan_lock);
2067 static int ext3_rmdir (struct inode * dir, struct dentry *dentry)
2070 struct inode * inode;
2071 struct buffer_head * bh;
2072 struct ext3_dir_entry_2 * de;
2075 /* Initialize quotas before so that eventual writes go in
2076 * separate transaction */
2077 dquot_initialize(dir);
2078 dquot_initialize(dentry->d_inode);
2080 handle = ext3_journal_start(dir, EXT3_DELETE_TRANS_BLOCKS(dir->i_sb));
2082 return PTR_ERR(handle);
2085 bh = ext3_find_entry(dir, &dentry->d_name, &de);
2089 if (IS_DIRSYNC(dir))
2092 inode = dentry->d_inode;
2095 if (le32_to_cpu(de->inode) != inode->i_ino)
2098 retval = -ENOTEMPTY;
2099 if (!empty_dir (inode))
2102 retval = ext3_delete_entry(handle, dir, de, bh);
2105 if (inode->i_nlink != 2)
2106 ext3_warning (inode->i_sb, "ext3_rmdir",
2107 "empty directory has nlink!=2 (%d)",
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
2115 ext3_orphan_add(handle, inode);
2116 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
2117 ext3_mark_inode_dirty(handle, inode);
2119 ext3_update_dx_flag(dir);
2120 ext3_mark_inode_dirty(handle, dir);
2123 ext3_journal_stop(handle);
2128 static int ext3_unlink(struct inode * dir, struct dentry *dentry)
2131 struct inode * inode;
2132 struct buffer_head * bh;
2133 struct ext3_dir_entry_2 * de;
2136 trace_ext3_unlink_enter(dir, dentry);
2137 /* Initialize quotas before so that eventual writes go
2138 * in separate transaction */
2139 dquot_initialize(dir);
2140 dquot_initialize(dentry->d_inode);
2142 handle = ext3_journal_start(dir, EXT3_DELETE_TRANS_BLOCKS(dir->i_sb));
2144 return PTR_ERR(handle);
2146 if (IS_DIRSYNC(dir))
2150 bh = ext3_find_entry(dir, &dentry->d_name, &de);
2154 inode = dentry->d_inode;
2157 if (le32_to_cpu(de->inode) != inode->i_ino)
2160 if (!inode->i_nlink) {
2161 ext3_warning (inode->i_sb, "ext3_unlink",
2162 "Deleting nonexistent file (%lu), %d",
2163 inode->i_ino, inode->i_nlink);
2164 set_nlink(inode, 1);
2166 retval = ext3_delete_entry(handle, dir, de, bh);
2169 dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
2170 ext3_update_dx_flag(dir);
2171 ext3_mark_inode_dirty(handle, dir);
2173 if (!inode->i_nlink)
2174 ext3_orphan_add(handle, inode);
2175 inode->i_ctime = dir->i_ctime;
2176 ext3_mark_inode_dirty(handle, inode);
2180 ext3_journal_stop(handle);
2182 trace_ext3_unlink_exit(dentry, retval);
2186 static int ext3_symlink (struct inode * dir,
2187 struct dentry *dentry, const char * symname)
2190 struct inode * inode;
2191 int l, err, retries = 0;
2194 l = strlen(symname)+1;
2195 if (l > dir->i_sb->s_blocksize)
2196 return -ENAMETOOLONG;
2198 dquot_initialize(dir);
2200 if (l > EXT3_N_BLOCKS * 4) {
2202 * For non-fast symlinks, we just allocate inode and put it on
2203 * orphan list in the first transaction => we need bitmap,
2204 * group descriptor, sb, inode block, quota blocks, and
2205 * possibly selinux xattr blocks.
2207 credits = 4 + EXT3_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
2208 EXT3_XATTR_TRANS_BLOCKS;
2211 * Fast symlink. We have to add entry to directory
2212 * (EXT3_DATA_TRANS_BLOCKS + EXT3_INDEX_EXTRA_TRANS_BLOCKS),
2213 * allocate new inode (bitmap, group descriptor, inode block,
2214 * quota blocks, sb is already counted in previous macros).
2216 credits = EXT3_DATA_TRANS_BLOCKS(dir->i_sb) +
2217 EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3 +
2218 EXT3_MAXQUOTAS_INIT_BLOCKS(dir->i_sb);
2221 handle = ext3_journal_start(dir, credits);
2223 return PTR_ERR(handle);
2225 if (IS_DIRSYNC(dir))
2228 inode = ext3_new_inode (handle, dir, &dentry->d_name, S_IFLNK|S_IRWXUGO);
2229 err = PTR_ERR(inode);
2233 if (l > EXT3_N_BLOCKS * 4) {
2234 inode->i_op = &ext3_symlink_inode_operations;
2235 ext3_set_aops(inode);
2237 * We cannot call page_symlink() with transaction started
2238 * because it calls into ext3_write_begin() which acquires page
2239 * lock which ranks below transaction start (and it can also
2240 * wait for journal commit if we are running out of space). So
2241 * we have to stop transaction now and restart it when symlink
2242 * contents is written.
2244 * To keep fs consistent in case of crash, we have to put inode
2245 * to orphan list in the mean time.
2248 err = ext3_orphan_add(handle, inode);
2249 ext3_journal_stop(handle);
2251 goto err_drop_inode;
2252 err = __page_symlink(inode, symname, l, 1);
2254 goto err_drop_inode;
2256 * Now inode is being linked into dir (EXT3_DATA_TRANS_BLOCKS
2257 * + EXT3_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
2259 handle = ext3_journal_start(dir,
2260 EXT3_DATA_TRANS_BLOCKS(dir->i_sb) +
2261 EXT3_INDEX_EXTRA_TRANS_BLOCKS + 1);
2262 if (IS_ERR(handle)) {
2263 err = PTR_ERR(handle);
2264 goto err_drop_inode;
2266 set_nlink(inode, 1);
2267 err = ext3_orphan_del(handle, inode);
2269 ext3_journal_stop(handle);
2271 goto err_drop_inode;
2274 inode->i_op = &ext3_fast_symlink_inode_operations;
2275 memcpy((char*)&EXT3_I(inode)->i_data,symname,l);
2276 inode->i_size = l-1;
2278 EXT3_I(inode)->i_disksize = inode->i_size;
2279 err = ext3_add_nondir(handle, dentry, inode);
2281 ext3_journal_stop(handle);
2282 if (err == -ENOSPC && ext3_should_retry_alloc(dir->i_sb, &retries))
2286 unlock_new_inode(inode);
2291 static int ext3_link (struct dentry * old_dentry,
2292 struct inode * dir, struct dentry *dentry)
2295 struct inode *inode = old_dentry->d_inode;
2296 int err, retries = 0;
2298 if (inode->i_nlink >= EXT3_LINK_MAX)
2301 dquot_initialize(dir);
2304 handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS(dir->i_sb) +
2305 EXT3_INDEX_EXTRA_TRANS_BLOCKS);
2307 return PTR_ERR(handle);
2309 if (IS_DIRSYNC(dir))
2312 inode->i_ctime = CURRENT_TIME_SEC;
2316 err = ext3_add_entry(handle, dentry, inode);
2318 ext3_mark_inode_dirty(handle, inode);
2319 d_instantiate(dentry, inode);
2324 ext3_journal_stop(handle);
2325 if (err == -ENOSPC && ext3_should_retry_alloc(dir->i_sb, &retries))
2330 #define PARENT_INO(buffer) \
2331 (ext3_next_entry((struct ext3_dir_entry_2 *)(buffer))->inode)
2334 * Anybody can rename anything with this: the permission checks are left to the
2335 * higher-level routines.
2337 static int ext3_rename (struct inode * old_dir, struct dentry *old_dentry,
2338 struct inode * new_dir,struct dentry *new_dentry)
2341 struct inode * old_inode, * new_inode;
2342 struct buffer_head * old_bh, * new_bh, * dir_bh;
2343 struct ext3_dir_entry_2 * old_de, * new_de;
2344 int retval, flush_file = 0;
2346 dquot_initialize(old_dir);
2347 dquot_initialize(new_dir);
2349 old_bh = new_bh = dir_bh = NULL;
2351 /* Initialize quotas before so that eventual writes go
2352 * in separate transaction */
2353 if (new_dentry->d_inode)
2354 dquot_initialize(new_dentry->d_inode);
2355 handle = ext3_journal_start(old_dir, 2 *
2356 EXT3_DATA_TRANS_BLOCKS(old_dir->i_sb) +
2357 EXT3_INDEX_EXTRA_TRANS_BLOCKS + 2);
2359 return PTR_ERR(handle);
2361 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
2364 old_bh = ext3_find_entry(old_dir, &old_dentry->d_name, &old_de);
2366 * Check for inode number is _not_ due to possible IO errors.
2367 * We might rmdir the source, keep it as pwd of some process
2368 * and merrily kill the link to whatever was created under the
2369 * same name. Goodbye sticky bit ;-<
2371 old_inode = old_dentry->d_inode;
2373 if (!old_bh || le32_to_cpu(old_de->inode) != old_inode->i_ino)
2376 new_inode = new_dentry->d_inode;
2377 new_bh = ext3_find_entry(new_dir, &new_dentry->d_name, &new_de);
2384 if (S_ISDIR(old_inode->i_mode)) {
2386 retval = -ENOTEMPTY;
2387 if (!empty_dir (new_inode))
2391 dir_bh = ext3_bread (handle, old_inode, 0, 0, &retval);
2394 if (le32_to_cpu(PARENT_INO(dir_bh->b_data)) != old_dir->i_ino)
2397 if (!new_inode && new_dir!=old_dir &&
2398 new_dir->i_nlink >= EXT3_LINK_MAX)
2402 retval = ext3_add_entry (handle, new_dentry, old_inode);
2406 BUFFER_TRACE(new_bh, "get write access");
2407 retval = ext3_journal_get_write_access(handle, new_bh);
2410 new_de->inode = cpu_to_le32(old_inode->i_ino);
2411 if (EXT3_HAS_INCOMPAT_FEATURE(new_dir->i_sb,
2412 EXT3_FEATURE_INCOMPAT_FILETYPE))
2413 new_de->file_type = old_de->file_type;
2414 new_dir->i_version++;
2415 new_dir->i_ctime = new_dir->i_mtime = CURRENT_TIME_SEC;
2416 ext3_mark_inode_dirty(handle, new_dir);
2417 BUFFER_TRACE(new_bh, "call ext3_journal_dirty_metadata");
2418 retval = ext3_journal_dirty_metadata(handle, new_bh);
2426 * Like most other Unix systems, set the ctime for inodes on a
2429 old_inode->i_ctime = CURRENT_TIME_SEC;
2430 ext3_mark_inode_dirty(handle, old_inode);
2435 if (le32_to_cpu(old_de->inode) != old_inode->i_ino ||
2436 old_de->name_len != old_dentry->d_name.len ||
2437 strncmp(old_de->name, old_dentry->d_name.name, old_de->name_len) ||
2438 (retval = ext3_delete_entry(handle, old_dir,
2439 old_de, old_bh)) == -ENOENT) {
2440 /* old_de could have moved from under us during htree split, so
2441 * make sure that we are deleting the right entry. We might
2442 * also be pointing to a stale entry in the unused part of
2443 * old_bh so just checking inum and the name isn't enough. */
2444 struct buffer_head *old_bh2;
2445 struct ext3_dir_entry_2 *old_de2;
2447 old_bh2 = ext3_find_entry(old_dir, &old_dentry->d_name,
2450 retval = ext3_delete_entry(handle, old_dir,
2456 ext3_warning(old_dir->i_sb, "ext3_rename",
2457 "Deleting old file (%lu), %d, error=%d",
2458 old_dir->i_ino, old_dir->i_nlink, retval);
2462 drop_nlink(new_inode);
2463 new_inode->i_ctime = CURRENT_TIME_SEC;
2465 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME_SEC;
2466 ext3_update_dx_flag(old_dir);
2468 BUFFER_TRACE(dir_bh, "get_write_access");
2469 retval = ext3_journal_get_write_access(handle, dir_bh);
2472 PARENT_INO(dir_bh->b_data) = cpu_to_le32(new_dir->i_ino);
2473 BUFFER_TRACE(dir_bh, "call ext3_journal_dirty_metadata");
2474 retval = ext3_journal_dirty_metadata(handle, dir_bh);
2477 ext3_std_error(new_dir->i_sb, retval);
2480 drop_nlink(old_dir);
2482 drop_nlink(new_inode);
2485 ext3_update_dx_flag(new_dir);
2486 ext3_mark_inode_dirty(handle, new_dir);
2489 ext3_mark_inode_dirty(handle, old_dir);
2491 ext3_mark_inode_dirty(handle, new_inode);
2492 if (!new_inode->i_nlink)
2493 ext3_orphan_add(handle, new_inode);
2494 if (ext3_should_writeback_data(new_inode))
2503 ext3_journal_stop(handle);
2504 if (retval == 0 && flush_file)
2505 filemap_flush(old_inode->i_mapping);
2510 * directories can handle most operations...
2512 const struct inode_operations ext3_dir_inode_operations = {
2513 .create = ext3_create,
2514 .lookup = ext3_lookup,
2516 .unlink = ext3_unlink,
2517 .symlink = ext3_symlink,
2518 .mkdir = ext3_mkdir,
2519 .rmdir = ext3_rmdir,
2520 .mknod = ext3_mknod,
2521 .rename = ext3_rename,
2522 .setattr = ext3_setattr,
2523 #ifdef CONFIG_EXT3_FS_XATTR
2524 .setxattr = generic_setxattr,
2525 .getxattr = generic_getxattr,
2526 .listxattr = ext3_listxattr,
2527 .removexattr = generic_removexattr,
2529 .get_acl = ext3_get_acl,
2532 const struct inode_operations ext3_special_inode_operations = {
2533 .setattr = ext3_setattr,
2534 #ifdef CONFIG_EXT3_FS_XATTR
2535 .setxattr = generic_setxattr,
2536 .getxattr = generic_getxattr,
2537 .listxattr = ext3_listxattr,
2538 .removexattr = generic_removexattr,
2540 .get_acl = ext3_get_acl,