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 /* silently ignore the rest of the block */
579 ext3fs_dirhash(de
->name
, de
->name_len
, hinfo
);
580 if ((hinfo
->hash
< start_hash
) ||
581 ((hinfo
->hash
== start_hash
) &&
582 (hinfo
->minor_hash
< start_minor_hash
)))
586 if ((err
= ext3_htree_store_dirent(dir_file
,
587 hinfo
->hash
, hinfo
->minor_hash
, de
)) != 0) {
599 * This function fills a red-black tree with information from a
600 * directory. We start scanning the directory in hash order, starting
601 * at start_hash and start_minor_hash.
603 * This function returns the number of entries inserted into the tree,
604 * or a negative error code.
606 int ext3_htree_fill_tree(struct file
*dir_file
, __u32 start_hash
,
607 __u32 start_minor_hash
, __u32
*next_hash
)
609 struct dx_hash_info hinfo
;
610 struct ext3_dir_entry_2
*de
;
611 struct dx_frame frames
[2], *frame
;
618 dxtrace(printk("In htree_fill_tree, start hash: %x:%x\n", start_hash
,
620 dir
= dir_file
->f_path
.dentry
->d_inode
;
621 if (!(EXT3_I(dir
)->i_flags
& EXT3_INDEX_FL
)) {
622 hinfo
.hash_version
= EXT3_SB(dir
->i_sb
)->s_def_hash_version
;
623 if (hinfo
.hash_version
<= DX_HASH_TEA
)
624 hinfo
.hash_version
+=
625 EXT3_SB(dir
->i_sb
)->s_hash_unsigned
;
626 hinfo
.seed
= EXT3_SB(dir
->i_sb
)->s_hash_seed
;
627 count
= htree_dirblock_to_tree(dir_file
, dir
, 0, &hinfo
,
628 start_hash
, start_minor_hash
);
632 hinfo
.hash
= start_hash
;
633 hinfo
.minor_hash
= 0;
634 frame
= dx_probe(NULL
, dir_file
->f_path
.dentry
->d_inode
, &hinfo
, frames
, &err
);
638 /* Add '.' and '..' from the htree header */
639 if (!start_hash
&& !start_minor_hash
) {
640 de
= (struct ext3_dir_entry_2
*) frames
[0].bh
->b_data
;
641 if ((err
= ext3_htree_store_dirent(dir_file
, 0, 0, de
)) != 0)
645 if (start_hash
< 2 || (start_hash
==2 && start_minor_hash
==0)) {
646 de
= (struct ext3_dir_entry_2
*) frames
[0].bh
->b_data
;
647 de
= ext3_next_entry(de
);
648 if ((err
= ext3_htree_store_dirent(dir_file
, 2, 0, de
)) != 0)
654 block
= dx_get_block(frame
->at
);
655 ret
= htree_dirblock_to_tree(dir_file
, dir
, block
, &hinfo
,
656 start_hash
, start_minor_hash
);
663 ret
= ext3_htree_next_block(dir
, HASH_NB_ALWAYS
,
664 frame
, frames
, &hashval
);
665 *next_hash
= hashval
;
671 * Stop if: (a) there are no more entries, or
672 * (b) we have inserted at least one entry and the
673 * next hash value is not a continuation
676 (count
&& ((hashval
& 1) == 0)))
680 dxtrace(printk("Fill tree: returned %d entries, next hash: %x\n",
690 * Directory block splitting, compacting
694 * Create map of hash values, offsets, and sizes, stored at end of block.
695 * Returns number of entries mapped.
697 static int dx_make_map(struct ext3_dir_entry_2
*de
, unsigned blocksize
,
698 struct dx_hash_info
*hinfo
, struct dx_map_entry
*map_tail
)
701 char *base
= (char *) de
;
702 struct dx_hash_info h
= *hinfo
;
704 while ((char *) de
< base
+ blocksize
)
706 if (de
->name_len
&& de
->inode
) {
707 ext3fs_dirhash(de
->name
, de
->name_len
, &h
);
709 map_tail
->hash
= h
.hash
;
710 map_tail
->offs
= (u16
) ((char *) de
- base
);
711 map_tail
->size
= le16_to_cpu(de
->rec_len
);
715 /* XXX: do we need to check rec_len == 0 case? -Chris */
716 de
= ext3_next_entry(de
);
721 /* Sort map by hash value */
722 static void dx_sort_map (struct dx_map_entry
*map
, unsigned count
)
724 struct dx_map_entry
*p
, *q
, *top
= map
+ count
- 1;
726 /* Combsort until bubble sort doesn't suck */
730 if (count
- 9 < 2) /* 9, 10 -> 11 */
732 for (p
= top
, q
= p
- count
; q
>= map
; p
--, q
--)
733 if (p
->hash
< q
->hash
)
736 /* Garden variety bubble sort */
742 if (q
[1].hash
>= q
[0].hash
)
750 static void dx_insert_block(struct dx_frame
*frame
, u32 hash
, u32 block
)
752 struct dx_entry
*entries
= frame
->entries
;
753 struct dx_entry
*old
= frame
->at
, *new = old
+ 1;
754 int count
= dx_get_count(entries
);
756 assert(count
< dx_get_limit(entries
));
757 assert(old
< entries
+ count
);
758 memmove(new + 1, new, (char *)(entries
+ count
) - (char *)(new));
759 dx_set_hash(new, hash
);
760 dx_set_block(new, block
);
761 dx_set_count(entries
, count
+ 1);
764 static void ext3_update_dx_flag(struct inode
*inode
)
766 if (!EXT3_HAS_COMPAT_FEATURE(inode
->i_sb
,
767 EXT3_FEATURE_COMPAT_DIR_INDEX
))
768 EXT3_I(inode
)->i_flags
&= ~EXT3_INDEX_FL
;
772 * NOTE! unlike strncmp, ext3_match returns 1 for success, 0 for failure.
774 * `len <= EXT3_NAME_LEN' is guaranteed by caller.
775 * `de != NULL' is guaranteed by caller.
777 static inline int ext3_match (int len
, const char * const name
,
778 struct ext3_dir_entry_2
* de
)
780 if (len
!= de
->name_len
)
784 return !memcmp(name
, de
->name
, len
);
788 * Returns 0 if not found, -1 on failure, and 1 on success
790 static inline int search_dirblock(struct buffer_head
* bh
,
793 unsigned long offset
,
794 struct ext3_dir_entry_2
** res_dir
)
796 struct ext3_dir_entry_2
* de
;
799 const char *name
= child
->name
;
800 int namelen
= child
->len
;
802 de
= (struct ext3_dir_entry_2
*) bh
->b_data
;
803 dlimit
= bh
->b_data
+ dir
->i_sb
->s_blocksize
;
804 while ((char *) de
< dlimit
) {
805 /* this code is executed quadratically often */
806 /* do minimal checking `by hand' */
808 if ((char *) de
+ namelen
<= dlimit
&&
809 ext3_match (namelen
, name
, de
)) {
810 /* found a match - just to be sure, do a full check */
811 if (!ext3_check_dir_entry("ext3_find_entry",
812 dir
, de
, bh
, offset
))
817 /* prevent looping on a bad block */
818 de_len
= ext3_rec_len_from_disk(de
->rec_len
);
822 de
= (struct ext3_dir_entry_2
*) ((char *) de
+ de_len
);
831 * finds an entry in the specified directory with the wanted name. It
832 * returns the cache buffer in which the entry was found, and the entry
833 * itself (as a parameter - res_dir). It does NOT read the inode of the
834 * entry - you'll have to do that yourself if you want to.
836 * The returned buffer_head has ->b_count elevated. The caller is expected
837 * to brelse() it when appropriate.
839 static struct buffer_head
*ext3_find_entry(struct inode
*dir
,
841 struct ext3_dir_entry_2
**res_dir
)
843 struct super_block
* sb
;
844 struct buffer_head
* bh_use
[NAMEI_RA_SIZE
];
845 struct buffer_head
* bh
, *ret
= NULL
;
846 unsigned long start
, block
, b
;
847 const u8
*name
= entry
->name
;
848 int ra_max
= 0; /* Number of bh's in the readahead
850 int ra_ptr
= 0; /* Current index into readahead
858 namelen
= entry
->len
;
859 if (namelen
> EXT3_NAME_LEN
)
861 if ((namelen
<= 2) && (name
[0] == '.') &&
862 (name
[1] == '.' || name
[1] == 0)) {
864 * "." or ".." will only be in the first block
865 * NFS may look up ".."; "." should be handled by the VFS
872 bh
= ext3_dx_find_entry(dir
, entry
, res_dir
, &err
);
874 * On success, or if the error was file not found,
875 * return. Otherwise, fall back to doing a search the
878 if (bh
|| (err
!= ERR_BAD_DX_DIR
))
880 dxtrace(printk("ext3_find_entry: dx failed, falling back\n"));
882 nblocks
= dir
->i_size
>> EXT3_BLOCK_SIZE_BITS(sb
);
883 start
= EXT3_I(dir
)->i_dir_start_lookup
;
884 if (start
>= nblocks
)
890 * We deal with the read-ahead logic here.
892 if (ra_ptr
>= ra_max
) {
893 /* Refill the readahead buffer */
896 for (ra_max
= 0; ra_max
< NAMEI_RA_SIZE
; ra_max
++) {
898 * Terminate if we reach the end of the
899 * directory and must wrap, or if our
900 * search has finished at this block.
902 if (b
>= nblocks
|| (num
&& block
== start
)) {
903 bh_use
[ra_max
] = NULL
;
907 bh
= ext3_getblk(NULL
, dir
, b
++, 0, &err
);
909 if (bh
&& !bh_uptodate_or_lock(bh
)) {
911 bh
->b_end_io
= end_buffer_read_sync
;
912 submit_bh(READ
| REQ_META
| REQ_PRIO
,
917 if ((bh
= bh_use
[ra_ptr
++]) == NULL
)
920 if (!buffer_uptodate(bh
)) {
921 /* read error, skip block & hope for the best */
922 ext3_error(sb
, __func__
, "reading directory #%lu "
923 "offset %lu", dir
->i_ino
, block
);
927 i
= search_dirblock(bh
, dir
, entry
,
928 block
<< EXT3_BLOCK_SIZE_BITS(sb
), res_dir
);
930 EXT3_I(dir
)->i_dir_start_lookup
= block
;
932 goto cleanup_and_exit
;
936 goto cleanup_and_exit
;
939 if (++block
>= nblocks
)
941 } while (block
!= start
);
944 * If the directory has grown while we were searching, then
945 * search the last part of the directory before giving up.
948 nblocks
= dir
->i_size
>> EXT3_BLOCK_SIZE_BITS(sb
);
949 if (block
< nblocks
) {
955 /* Clean up the read-ahead blocks */
956 for (; ra_ptr
< ra_max
; ra_ptr
++)
957 brelse (bh_use
[ra_ptr
]);
961 static struct buffer_head
* ext3_dx_find_entry(struct inode
*dir
,
962 struct qstr
*entry
, struct ext3_dir_entry_2
**res_dir
,
965 struct super_block
*sb
= dir
->i_sb
;
966 struct dx_hash_info hinfo
;
967 struct dx_frame frames
[2], *frame
;
968 struct buffer_head
*bh
;
972 if (!(frame
= dx_probe(entry
, dir
, &hinfo
, frames
, err
)))
975 block
= dx_get_block(frame
->at
);
976 if (!(bh
= ext3_bread (NULL
,dir
, block
, 0, err
)))
979 retval
= search_dirblock(bh
, dir
, entry
,
980 block
<< EXT3_BLOCK_SIZE_BITS(sb
),
988 *err
= ERR_BAD_DX_DIR
;
992 /* Check to see if we should continue to search */
993 retval
= ext3_htree_next_block(dir
, hinfo
.hash
, frame
,
996 ext3_warning(sb
, __func__
,
997 "error reading index page in directory #%lu",
1002 } while (retval
== 1);
1006 dxtrace(printk("%s not found\n", entry
->name
));
1007 dx_release (frames
);
1011 static struct dentry
*ext3_lookup(struct inode
* dir
, struct dentry
*dentry
, struct nameidata
*nd
)
1013 struct inode
* inode
;
1014 struct ext3_dir_entry_2
* de
;
1015 struct buffer_head
* bh
;
1017 if (dentry
->d_name
.len
> EXT3_NAME_LEN
)
1018 return ERR_PTR(-ENAMETOOLONG
);
1020 bh
= ext3_find_entry(dir
, &dentry
->d_name
, &de
);
1023 unsigned long ino
= le32_to_cpu(de
->inode
);
1025 if (!ext3_valid_inum(dir
->i_sb
, ino
)) {
1026 ext3_error(dir
->i_sb
, "ext3_lookup",
1027 "bad inode number: %lu", ino
);
1028 return ERR_PTR(-EIO
);
1030 inode
= ext3_iget(dir
->i_sb
, ino
);
1031 if (inode
== ERR_PTR(-ESTALE
)) {
1032 ext3_error(dir
->i_sb
, __func__
,
1033 "deleted inode referenced: %lu",
1035 return ERR_PTR(-EIO
);
1038 return d_splice_alias(inode
, dentry
);
1042 struct dentry
*ext3_get_parent(struct dentry
*child
)
1045 struct qstr dotdot
= {.name
= "..", .len
= 2};
1046 struct ext3_dir_entry_2
* de
;
1047 struct buffer_head
*bh
;
1049 bh
= ext3_find_entry(child
->d_inode
, &dotdot
, &de
);
1051 return ERR_PTR(-ENOENT
);
1052 ino
= le32_to_cpu(de
->inode
);
1055 if (!ext3_valid_inum(child
->d_inode
->i_sb
, ino
)) {
1056 ext3_error(child
->d_inode
->i_sb
, "ext3_get_parent",
1057 "bad inode number: %lu", ino
);
1058 return ERR_PTR(-EIO
);
1061 return d_obtain_alias(ext3_iget(child
->d_inode
->i_sb
, ino
));
1065 static unsigned char ext3_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
1066 [S_IFREG
>> S_SHIFT
] = EXT3_FT_REG_FILE
,
1067 [S_IFDIR
>> S_SHIFT
] = EXT3_FT_DIR
,
1068 [S_IFCHR
>> S_SHIFT
] = EXT3_FT_CHRDEV
,
1069 [S_IFBLK
>> S_SHIFT
] = EXT3_FT_BLKDEV
,
1070 [S_IFIFO
>> S_SHIFT
] = EXT3_FT_FIFO
,
1071 [S_IFSOCK
>> S_SHIFT
] = EXT3_FT_SOCK
,
1072 [S_IFLNK
>> S_SHIFT
] = EXT3_FT_SYMLINK
,
1075 static inline void ext3_set_de_type(struct super_block
*sb
,
1076 struct ext3_dir_entry_2
*de
,
1078 if (EXT3_HAS_INCOMPAT_FEATURE(sb
, EXT3_FEATURE_INCOMPAT_FILETYPE
))
1079 de
->file_type
= ext3_type_by_mode
[(mode
& S_IFMT
)>>S_SHIFT
];
1083 * Move count entries from end of map between two memory locations.
1084 * Returns pointer to last entry moved.
1086 static struct ext3_dir_entry_2
*
1087 dx_move_dirents(char *from
, char *to
, struct dx_map_entry
*map
, int count
)
1089 unsigned rec_len
= 0;
1092 struct ext3_dir_entry_2
*de
= (struct ext3_dir_entry_2
*) (from
+ map
->offs
);
1093 rec_len
= EXT3_DIR_REC_LEN(de
->name_len
);
1094 memcpy (to
, de
, rec_len
);
1095 ((struct ext3_dir_entry_2
*) to
)->rec_len
=
1096 ext3_rec_len_to_disk(rec_len
);
1101 return (struct ext3_dir_entry_2
*) (to
- rec_len
);
1105 * Compact each dir entry in the range to the minimal rec_len.
1106 * Returns pointer to last entry in range.
1108 static struct ext3_dir_entry_2
*dx_pack_dirents(char *base
, unsigned blocksize
)
1110 struct ext3_dir_entry_2
*next
, *to
, *prev
;
1111 struct ext3_dir_entry_2
*de
= (struct ext3_dir_entry_2
*)base
;
1112 unsigned rec_len
= 0;
1115 while ((char *)de
< base
+ blocksize
) {
1116 next
= ext3_next_entry(de
);
1117 if (de
->inode
&& de
->name_len
) {
1118 rec_len
= EXT3_DIR_REC_LEN(de
->name_len
);
1120 memmove(to
, de
, rec_len
);
1121 to
->rec_len
= ext3_rec_len_to_disk(rec_len
);
1123 to
= (struct ext3_dir_entry_2
*) (((char *) to
) + rec_len
);
1131 * Split a full leaf block to make room for a new dir entry.
1132 * Allocate a new block, and move entries so that they are approx. equally full.
1133 * Returns pointer to de in block into which the new entry will be inserted.
1135 static struct ext3_dir_entry_2
*do_split(handle_t
*handle
, struct inode
*dir
,
1136 struct buffer_head
**bh
,struct dx_frame
*frame
,
1137 struct dx_hash_info
*hinfo
, int *error
)
1139 unsigned blocksize
= dir
->i_sb
->s_blocksize
;
1140 unsigned count
, continued
;
1141 struct buffer_head
*bh2
;
1144 struct dx_map_entry
*map
;
1145 char *data1
= (*bh
)->b_data
, *data2
;
1146 unsigned split
, move
, size
;
1147 struct ext3_dir_entry_2
*de
= NULL
, *de2
;
1150 bh2
= ext3_append (handle
, dir
, &newblock
, &err
);
1157 BUFFER_TRACE(*bh
, "get_write_access");
1158 err
= ext3_journal_get_write_access(handle
, *bh
);
1162 BUFFER_TRACE(frame
->bh
, "get_write_access");
1163 err
= ext3_journal_get_write_access(handle
, frame
->bh
);
1167 data2
= bh2
->b_data
;
1169 /* create map in the end of data2 block */
1170 map
= (struct dx_map_entry
*) (data2
+ blocksize
);
1171 count
= dx_make_map ((struct ext3_dir_entry_2
*) data1
,
1172 blocksize
, hinfo
, map
);
1174 dx_sort_map (map
, count
);
1175 /* Split the existing block in the middle, size-wise */
1178 for (i
= count
-1; i
>= 0; i
--) {
1179 /* is more than half of this entry in 2nd half of the block? */
1180 if (size
+ map
[i
].size
/2 > blocksize
/2)
1182 size
+= map
[i
].size
;
1185 /* map index at which we will split */
1186 split
= count
- move
;
1187 hash2
= map
[split
].hash
;
1188 continued
= hash2
== map
[split
- 1].hash
;
1189 dxtrace(printk("Split block %i at %x, %i/%i\n",
1190 dx_get_block(frame
->at
), hash2
, split
, count
-split
));
1192 /* Fancy dance to stay within two buffers */
1193 de2
= dx_move_dirents(data1
, data2
, map
+ split
, count
- split
);
1194 de
= dx_pack_dirents(data1
,blocksize
);
1195 de
->rec_len
= ext3_rec_len_to_disk(data1
+ blocksize
- (char *) de
);
1196 de2
->rec_len
= ext3_rec_len_to_disk(data2
+ blocksize
- (char *) de2
);
1197 dxtrace(dx_show_leaf (hinfo
, (struct ext3_dir_entry_2
*) data1
, blocksize
, 1));
1198 dxtrace(dx_show_leaf (hinfo
, (struct ext3_dir_entry_2
*) data2
, blocksize
, 1));
1200 /* Which block gets the new entry? */
1201 if (hinfo
->hash
>= hash2
)
1206 dx_insert_block (frame
, hash2
+ continued
, newblock
);
1207 err
= ext3_journal_dirty_metadata (handle
, bh2
);
1210 err
= ext3_journal_dirty_metadata (handle
, frame
->bh
);
1214 dxtrace(dx_show_index ("frame", frame
->entries
));
1221 ext3_std_error(dir
->i_sb
, err
);
1229 * Add a new entry into a directory (leaf) block. If de is non-NULL,
1230 * it points to a directory entry which is guaranteed to be large
1231 * enough for new directory entry. If de is NULL, then
1232 * add_dirent_to_buf will attempt search the directory block for
1233 * space. It will return -ENOSPC if no space is available, and -EIO
1234 * and -EEXIST if directory entry already exists.
1236 * NOTE! bh is NOT released in the case where ENOSPC is returned. In
1237 * all other cases bh is released.
1239 static int add_dirent_to_buf(handle_t
*handle
, struct dentry
*dentry
,
1240 struct inode
*inode
, struct ext3_dir_entry_2
*de
,
1241 struct buffer_head
* bh
)
1243 struct inode
*dir
= dentry
->d_parent
->d_inode
;
1244 const char *name
= dentry
->d_name
.name
;
1245 int namelen
= dentry
->d_name
.len
;
1246 unsigned long offset
= 0;
1247 unsigned short reclen
;
1248 int nlen
, rlen
, err
;
1251 reclen
= EXT3_DIR_REC_LEN(namelen
);
1253 de
= (struct ext3_dir_entry_2
*)bh
->b_data
;
1254 top
= bh
->b_data
+ dir
->i_sb
->s_blocksize
- reclen
;
1255 while ((char *) de
<= top
) {
1256 if (!ext3_check_dir_entry("ext3_add_entry", dir
, de
,
1261 if (ext3_match (namelen
, name
, de
)) {
1265 nlen
= EXT3_DIR_REC_LEN(de
->name_len
);
1266 rlen
= ext3_rec_len_from_disk(de
->rec_len
);
1267 if ((de
->inode
? rlen
- nlen
: rlen
) >= reclen
)
1269 de
= (struct ext3_dir_entry_2
*)((char *)de
+ rlen
);
1272 if ((char *) de
> top
)
1275 BUFFER_TRACE(bh
, "get_write_access");
1276 err
= ext3_journal_get_write_access(handle
, bh
);
1278 ext3_std_error(dir
->i_sb
, err
);
1283 /* By now the buffer is marked for journaling */
1284 nlen
= EXT3_DIR_REC_LEN(de
->name_len
);
1285 rlen
= ext3_rec_len_from_disk(de
->rec_len
);
1287 struct ext3_dir_entry_2
*de1
= (struct ext3_dir_entry_2
*)((char *)de
+ nlen
);
1288 de1
->rec_len
= ext3_rec_len_to_disk(rlen
- nlen
);
1289 de
->rec_len
= ext3_rec_len_to_disk(nlen
);
1292 de
->file_type
= EXT3_FT_UNKNOWN
;
1294 de
->inode
= cpu_to_le32(inode
->i_ino
);
1295 ext3_set_de_type(dir
->i_sb
, de
, inode
->i_mode
);
1298 de
->name_len
= namelen
;
1299 memcpy (de
->name
, name
, namelen
);
1301 * XXX shouldn't update any times until successful
1302 * completion of syscall, but too many callers depend
1305 * XXX similarly, too many callers depend on
1306 * ext3_new_inode() setting the times, but error
1307 * recovery deletes the inode, so the worst that can
1308 * happen is that the times are slightly out of date
1309 * and/or different from the directory change time.
1311 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME_SEC
;
1312 ext3_update_dx_flag(dir
);
1314 ext3_mark_inode_dirty(handle
, dir
);
1315 BUFFER_TRACE(bh
, "call ext3_journal_dirty_metadata");
1316 err
= ext3_journal_dirty_metadata(handle
, bh
);
1318 ext3_std_error(dir
->i_sb
, err
);
1324 * This converts a one block unindexed directory to a 3 block indexed
1325 * directory, and adds the dentry to the indexed directory.
1327 static int make_indexed_dir(handle_t
*handle
, struct dentry
*dentry
,
1328 struct inode
*inode
, struct buffer_head
*bh
)
1330 struct inode
*dir
= dentry
->d_parent
->d_inode
;
1331 const char *name
= dentry
->d_name
.name
;
1332 int namelen
= dentry
->d_name
.len
;
1333 struct buffer_head
*bh2
;
1334 struct dx_root
*root
;
1335 struct dx_frame frames
[2], *frame
;
1336 struct dx_entry
*entries
;
1337 struct ext3_dir_entry_2
*de
, *de2
;
1342 struct dx_hash_info hinfo
;
1344 struct fake_dirent
*fde
;
1346 blocksize
= dir
->i_sb
->s_blocksize
;
1347 dxtrace(printk(KERN_DEBUG
"Creating index: inode %lu\n", dir
->i_ino
));
1348 retval
= ext3_journal_get_write_access(handle
, bh
);
1350 ext3_std_error(dir
->i_sb
, retval
);
1354 root
= (struct dx_root
*) bh
->b_data
;
1356 /* The 0th block becomes the root, move the dirents out */
1357 fde
= &root
->dotdot
;
1358 de
= (struct ext3_dir_entry_2
*)((char *)fde
+
1359 ext3_rec_len_from_disk(fde
->rec_len
));
1360 if ((char *) de
>= (((char *) root
) + blocksize
)) {
1361 ext3_error(dir
->i_sb
, __func__
,
1362 "invalid rec_len for '..' in inode %lu",
1367 len
= ((char *) root
) + blocksize
- (char *) de
;
1369 bh2
= ext3_append (handle
, dir
, &block
, &retval
);
1374 EXT3_I(dir
)->i_flags
|= EXT3_INDEX_FL
;
1375 data1
= bh2
->b_data
;
1377 memcpy (data1
, de
, len
);
1378 de
= (struct ext3_dir_entry_2
*) data1
;
1380 while ((char *)(de2
= ext3_next_entry(de
)) < top
)
1382 de
->rec_len
= ext3_rec_len_to_disk(data1
+ blocksize
- (char *) de
);
1383 /* Initialize the root; the dot dirents already exist */
1384 de
= (struct ext3_dir_entry_2
*) (&root
->dotdot
);
1385 de
->rec_len
= ext3_rec_len_to_disk(blocksize
- EXT3_DIR_REC_LEN(2));
1386 memset (&root
->info
, 0, sizeof(root
->info
));
1387 root
->info
.info_length
= sizeof(root
->info
);
1388 root
->info
.hash_version
= EXT3_SB(dir
->i_sb
)->s_def_hash_version
;
1389 entries
= root
->entries
;
1390 dx_set_block (entries
, 1);
1391 dx_set_count (entries
, 1);
1392 dx_set_limit (entries
, dx_root_limit(dir
, sizeof(root
->info
)));
1394 /* Initialize as for dx_probe */
1395 hinfo
.hash_version
= root
->info
.hash_version
;
1396 if (hinfo
.hash_version
<= DX_HASH_TEA
)
1397 hinfo
.hash_version
+= EXT3_SB(dir
->i_sb
)->s_hash_unsigned
;
1398 hinfo
.seed
= EXT3_SB(dir
->i_sb
)->s_hash_seed
;
1399 ext3fs_dirhash(name
, namelen
, &hinfo
);
1401 frame
->entries
= entries
;
1402 frame
->at
= entries
;
1406 * Mark buffers dirty here so that if do_split() fails we write a
1407 * consistent set of buffers to disk.
1409 ext3_journal_dirty_metadata(handle
, frame
->bh
);
1410 ext3_journal_dirty_metadata(handle
, bh
);
1411 de
= do_split(handle
,dir
, &bh
, frame
, &hinfo
, &retval
);
1413 ext3_mark_inode_dirty(handle
, dir
);
1419 return add_dirent_to_buf(handle
, dentry
, inode
, de
, bh
);
1425 * adds a file entry to the specified directory, using the same
1426 * semantics as ext3_find_entry(). It returns NULL if it failed.
1428 * NOTE!! The inode part of 'de' is left at 0 - which means you
1429 * may not sleep between calling this and putting something into
1430 * the entry, as someone else might have used it while you slept.
1432 static int ext3_add_entry (handle_t
*handle
, struct dentry
*dentry
,
1433 struct inode
*inode
)
1435 struct inode
*dir
= dentry
->d_parent
->d_inode
;
1436 struct buffer_head
* bh
;
1437 struct ext3_dir_entry_2
*de
;
1438 struct super_block
* sb
;
1445 blocksize
= sb
->s_blocksize
;
1446 if (!dentry
->d_name
.len
)
1449 retval
= ext3_dx_add_entry(handle
, dentry
, inode
);
1450 if (!retval
|| (retval
!= ERR_BAD_DX_DIR
))
1452 EXT3_I(dir
)->i_flags
&= ~EXT3_INDEX_FL
;
1454 ext3_mark_inode_dirty(handle
, dir
);
1456 blocks
= dir
->i_size
>> sb
->s_blocksize_bits
;
1457 for (block
= 0; block
< blocks
; block
++) {
1458 bh
= ext3_bread(handle
, dir
, block
, 0, &retval
);
1461 retval
= add_dirent_to_buf(handle
, dentry
, inode
, NULL
, bh
);
1462 if (retval
!= -ENOSPC
)
1465 if (blocks
== 1 && !dx_fallback
&&
1466 EXT3_HAS_COMPAT_FEATURE(sb
, EXT3_FEATURE_COMPAT_DIR_INDEX
))
1467 return make_indexed_dir(handle
, dentry
, inode
, bh
);
1470 bh
= ext3_append(handle
, dir
, &block
, &retval
);
1473 de
= (struct ext3_dir_entry_2
*) bh
->b_data
;
1475 de
->rec_len
= ext3_rec_len_to_disk(blocksize
);
1476 return add_dirent_to_buf(handle
, dentry
, inode
, de
, bh
);
1480 * Returns 0 for success, or a negative error value
1482 static int ext3_dx_add_entry(handle_t
*handle
, struct dentry
*dentry
,
1483 struct inode
*inode
)
1485 struct dx_frame frames
[2], *frame
;
1486 struct dx_entry
*entries
, *at
;
1487 struct dx_hash_info hinfo
;
1488 struct buffer_head
* bh
;
1489 struct inode
*dir
= dentry
->d_parent
->d_inode
;
1490 struct super_block
* sb
= dir
->i_sb
;
1491 struct ext3_dir_entry_2
*de
;
1494 frame
= dx_probe(&dentry
->d_name
, dir
, &hinfo
, frames
, &err
);
1497 entries
= frame
->entries
;
1500 if (!(bh
= ext3_bread(handle
,dir
, dx_get_block(frame
->at
), 0, &err
)))
1503 BUFFER_TRACE(bh
, "get_write_access");
1504 err
= ext3_journal_get_write_access(handle
, bh
);
1508 err
= add_dirent_to_buf(handle
, dentry
, inode
, NULL
, bh
);
1509 if (err
!= -ENOSPC
) {
1514 /* Block full, should compress but for now just split */
1515 dxtrace(printk("using %u of %u node entries\n",
1516 dx_get_count(entries
), dx_get_limit(entries
)));
1517 /* Need to split index? */
1518 if (dx_get_count(entries
) == dx_get_limit(entries
)) {
1520 unsigned icount
= dx_get_count(entries
);
1521 int levels
= frame
- frames
;
1522 struct dx_entry
*entries2
;
1523 struct dx_node
*node2
;
1524 struct buffer_head
*bh2
;
1526 if (levels
&& (dx_get_count(frames
->entries
) ==
1527 dx_get_limit(frames
->entries
))) {
1528 ext3_warning(sb
, __func__
,
1529 "Directory index full!");
1533 bh2
= ext3_append (handle
, dir
, &newblock
, &err
);
1536 node2
= (struct dx_node
*)(bh2
->b_data
);
1537 entries2
= node2
->entries
;
1538 memset(&node2
->fake
, 0, sizeof(struct fake_dirent
));
1539 node2
->fake
.rec_len
= ext3_rec_len_to_disk(sb
->s_blocksize
);
1540 BUFFER_TRACE(frame
->bh
, "get_write_access");
1541 err
= ext3_journal_get_write_access(handle
, frame
->bh
);
1545 unsigned icount1
= icount
/2, icount2
= icount
- icount1
;
1546 unsigned hash2
= dx_get_hash(entries
+ icount1
);
1547 dxtrace(printk("Split index %i/%i\n", icount1
, icount2
));
1549 BUFFER_TRACE(frame
->bh
, "get_write_access"); /* index root */
1550 err
= ext3_journal_get_write_access(handle
,
1555 memcpy ((char *) entries2
, (char *) (entries
+ icount1
),
1556 icount2
* sizeof(struct dx_entry
));
1557 dx_set_count (entries
, icount1
);
1558 dx_set_count (entries2
, icount2
);
1559 dx_set_limit (entries2
, dx_node_limit(dir
));
1561 /* Which index block gets the new entry? */
1562 if (at
- entries
>= icount1
) {
1563 frame
->at
= at
= at
- entries
- icount1
+ entries2
;
1564 frame
->entries
= entries
= entries2
;
1565 swap(frame
->bh
, bh2
);
1567 dx_insert_block (frames
+ 0, hash2
, newblock
);
1568 dxtrace(dx_show_index ("node", frames
[1].entries
));
1569 dxtrace(dx_show_index ("node",
1570 ((struct dx_node
*) bh2
->b_data
)->entries
));
1571 err
= ext3_journal_dirty_metadata(handle
, bh2
);
1576 dxtrace(printk("Creating second level index...\n"));
1577 memcpy((char *) entries2
, (char *) entries
,
1578 icount
* sizeof(struct dx_entry
));
1579 dx_set_limit(entries2
, dx_node_limit(dir
));
1582 dx_set_count(entries
, 1);
1583 dx_set_block(entries
+ 0, newblock
);
1584 ((struct dx_root
*) frames
[0].bh
->b_data
)->info
.indirect_levels
= 1;
1586 /* Add new access path frame */
1588 frame
->at
= at
= at
- entries
+ entries2
;
1589 frame
->entries
= entries
= entries2
;
1591 err
= ext3_journal_get_write_access(handle
,
1596 err
= ext3_journal_dirty_metadata(handle
, frames
[0].bh
);
1600 de
= do_split(handle
, dir
, &bh
, frame
, &hinfo
, &err
);
1603 err
= add_dirent_to_buf(handle
, dentry
, inode
, de
, bh
);
1608 ext3_std_error(dir
->i_sb
, err
);
1617 * ext3_delete_entry deletes a directory entry by merging it with the
1620 static int ext3_delete_entry (handle_t
*handle
,
1622 struct ext3_dir_entry_2
* de_del
,
1623 struct buffer_head
* bh
)
1625 struct ext3_dir_entry_2
* de
, * pde
;
1630 de
= (struct ext3_dir_entry_2
*) bh
->b_data
;
1631 while (i
< bh
->b_size
) {
1632 if (!ext3_check_dir_entry("ext3_delete_entry", dir
, de
, bh
, i
))
1637 BUFFER_TRACE(bh
, "get_write_access");
1638 err
= ext3_journal_get_write_access(handle
, bh
);
1643 pde
->rec_len
= ext3_rec_len_to_disk(
1644 ext3_rec_len_from_disk(pde
->rec_len
) +
1645 ext3_rec_len_from_disk(de
->rec_len
));
1649 BUFFER_TRACE(bh
, "call ext3_journal_dirty_metadata");
1650 err
= ext3_journal_dirty_metadata(handle
, bh
);
1653 ext3_std_error(dir
->i_sb
, err
);
1658 i
+= ext3_rec_len_from_disk(de
->rec_len
);
1660 de
= ext3_next_entry(de
);
1665 static int ext3_add_nondir(handle_t
*handle
,
1666 struct dentry
*dentry
, struct inode
*inode
)
1668 int err
= ext3_add_entry(handle
, dentry
, inode
);
1670 ext3_mark_inode_dirty(handle
, inode
);
1671 d_instantiate(dentry
, inode
);
1672 unlock_new_inode(inode
);
1676 unlock_new_inode(inode
);
1682 * By the time this is called, we already have created
1683 * the directory cache entry for the new file, but it
1684 * is so far negative - it has no inode.
1686 * If the create succeeds, we fill in the inode information
1687 * with d_instantiate().
1689 static int ext3_create (struct inode
* dir
, struct dentry
* dentry
, umode_t mode
,
1690 struct nameidata
*nd
)
1693 struct inode
* inode
;
1694 int err
, retries
= 0;
1696 dquot_initialize(dir
);
1699 handle
= ext3_journal_start(dir
, EXT3_DATA_TRANS_BLOCKS(dir
->i_sb
) +
1700 EXT3_INDEX_EXTRA_TRANS_BLOCKS
+ 3 +
1701 EXT3_MAXQUOTAS_INIT_BLOCKS(dir
->i_sb
));
1703 return PTR_ERR(handle
);
1705 if (IS_DIRSYNC(dir
))
1708 inode
= ext3_new_inode (handle
, dir
, &dentry
->d_name
, mode
);
1709 err
= PTR_ERR(inode
);
1710 if (!IS_ERR(inode
)) {
1711 inode
->i_op
= &ext3_file_inode_operations
;
1712 inode
->i_fop
= &ext3_file_operations
;
1713 ext3_set_aops(inode
);
1714 err
= ext3_add_nondir(handle
, dentry
, inode
);
1716 ext3_journal_stop(handle
);
1717 if (err
== -ENOSPC
&& ext3_should_retry_alloc(dir
->i_sb
, &retries
))
1722 static int ext3_mknod (struct inode
* dir
, struct dentry
*dentry
,
1723 umode_t mode
, dev_t rdev
)
1726 struct inode
*inode
;
1727 int err
, retries
= 0;
1729 if (!new_valid_dev(rdev
))
1732 dquot_initialize(dir
);
1735 handle
= ext3_journal_start(dir
, EXT3_DATA_TRANS_BLOCKS(dir
->i_sb
) +
1736 EXT3_INDEX_EXTRA_TRANS_BLOCKS
+ 3 +
1737 EXT3_MAXQUOTAS_INIT_BLOCKS(dir
->i_sb
));
1739 return PTR_ERR(handle
);
1741 if (IS_DIRSYNC(dir
))
1744 inode
= ext3_new_inode (handle
, dir
, &dentry
->d_name
, mode
);
1745 err
= PTR_ERR(inode
);
1746 if (!IS_ERR(inode
)) {
1747 init_special_inode(inode
, inode
->i_mode
, rdev
);
1748 #ifdef CONFIG_EXT3_FS_XATTR
1749 inode
->i_op
= &ext3_special_inode_operations
;
1751 err
= ext3_add_nondir(handle
, dentry
, inode
);
1753 ext3_journal_stop(handle
);
1754 if (err
== -ENOSPC
&& ext3_should_retry_alloc(dir
->i_sb
, &retries
))
1759 static int ext3_mkdir(struct inode
* dir
, struct dentry
* dentry
, umode_t mode
)
1762 struct inode
* inode
;
1763 struct buffer_head
* dir_block
= NULL
;
1764 struct ext3_dir_entry_2
* de
;
1765 int err
, retries
= 0;
1767 if (dir
->i_nlink
>= EXT3_LINK_MAX
)
1770 dquot_initialize(dir
);
1773 handle
= ext3_journal_start(dir
, EXT3_DATA_TRANS_BLOCKS(dir
->i_sb
) +
1774 EXT3_INDEX_EXTRA_TRANS_BLOCKS
+ 3 +
1775 EXT3_MAXQUOTAS_INIT_BLOCKS(dir
->i_sb
));
1777 return PTR_ERR(handle
);
1779 if (IS_DIRSYNC(dir
))
1782 inode
= ext3_new_inode (handle
, dir
, &dentry
->d_name
, S_IFDIR
| mode
);
1783 err
= PTR_ERR(inode
);
1787 inode
->i_op
= &ext3_dir_inode_operations
;
1788 inode
->i_fop
= &ext3_dir_operations
;
1789 inode
->i_size
= EXT3_I(inode
)->i_disksize
= inode
->i_sb
->s_blocksize
;
1790 dir_block
= ext3_bread (handle
, inode
, 0, 1, &err
);
1792 goto out_clear_inode
;
1794 BUFFER_TRACE(dir_block
, "get_write_access");
1795 err
= ext3_journal_get_write_access(handle
, dir_block
);
1797 goto out_clear_inode
;
1799 de
= (struct ext3_dir_entry_2
*) dir_block
->b_data
;
1800 de
->inode
= cpu_to_le32(inode
->i_ino
);
1802 de
->rec_len
= ext3_rec_len_to_disk(EXT3_DIR_REC_LEN(de
->name_len
));
1803 strcpy (de
->name
, ".");
1804 ext3_set_de_type(dir
->i_sb
, de
, S_IFDIR
);
1805 de
= ext3_next_entry(de
);
1806 de
->inode
= cpu_to_le32(dir
->i_ino
);
1807 de
->rec_len
= ext3_rec_len_to_disk(inode
->i_sb
->s_blocksize
-
1808 EXT3_DIR_REC_LEN(1));
1810 strcpy (de
->name
, "..");
1811 ext3_set_de_type(dir
->i_sb
, de
, S_IFDIR
);
1812 set_nlink(inode
, 2);
1813 BUFFER_TRACE(dir_block
, "call ext3_journal_dirty_metadata");
1814 err
= ext3_journal_dirty_metadata(handle
, dir_block
);
1816 goto out_clear_inode
;
1818 err
= ext3_mark_inode_dirty(handle
, inode
);
1820 err
= ext3_add_entry (handle
, dentry
, inode
);
1825 unlock_new_inode(inode
);
1826 ext3_mark_inode_dirty(handle
, inode
);
1831 ext3_update_dx_flag(dir
);
1832 err
= ext3_mark_inode_dirty(handle
, dir
);
1834 goto out_clear_inode
;
1836 d_instantiate(dentry
, inode
);
1837 unlock_new_inode(inode
);
1840 ext3_journal_stop(handle
);
1841 if (err
== -ENOSPC
&& ext3_should_retry_alloc(dir
->i_sb
, &retries
))
1847 * routine to check that the specified directory is empty (for rmdir)
1849 static int empty_dir (struct inode
* inode
)
1851 unsigned long offset
;
1852 struct buffer_head
* bh
;
1853 struct ext3_dir_entry_2
* de
, * de1
;
1854 struct super_block
* sb
;
1858 if (inode
->i_size
< EXT3_DIR_REC_LEN(1) + EXT3_DIR_REC_LEN(2) ||
1859 !(bh
= ext3_bread (NULL
, inode
, 0, 0, &err
))) {
1861 ext3_error(inode
->i_sb
, __func__
,
1862 "error %d reading directory #%lu offset 0",
1865 ext3_warning(inode
->i_sb
, __func__
,
1866 "bad directory (dir #%lu) - no data block",
1870 de
= (struct ext3_dir_entry_2
*) bh
->b_data
;
1871 de1
= ext3_next_entry(de
);
1872 if (le32_to_cpu(de
->inode
) != inode
->i_ino
||
1873 !le32_to_cpu(de1
->inode
) ||
1874 strcmp (".", de
->name
) ||
1875 strcmp ("..", de1
->name
)) {
1876 ext3_warning (inode
->i_sb
, "empty_dir",
1877 "bad directory (dir #%lu) - no `.' or `..'",
1882 offset
= ext3_rec_len_from_disk(de
->rec_len
) +
1883 ext3_rec_len_from_disk(de1
->rec_len
);
1884 de
= ext3_next_entry(de1
);
1885 while (offset
< inode
->i_size
) {
1887 (void *) de
>= (void *) (bh
->b_data
+sb
->s_blocksize
)) {
1890 bh
= ext3_bread (NULL
, inode
,
1891 offset
>> EXT3_BLOCK_SIZE_BITS(sb
), 0, &err
);
1894 ext3_error(sb
, __func__
,
1895 "error %d reading directory"
1897 err
, inode
->i_ino
, offset
);
1898 offset
+= sb
->s_blocksize
;
1901 de
= (struct ext3_dir_entry_2
*) bh
->b_data
;
1903 if (!ext3_check_dir_entry("empty_dir", inode
, de
, bh
, offset
)) {
1904 de
= (struct ext3_dir_entry_2
*)(bh
->b_data
+
1906 offset
= (offset
| (sb
->s_blocksize
- 1)) + 1;
1909 if (le32_to_cpu(de
->inode
)) {
1913 offset
+= ext3_rec_len_from_disk(de
->rec_len
);
1914 de
= ext3_next_entry(de
);
1920 /* ext3_orphan_add() links an unlinked or truncated inode into a list of
1921 * such inodes, starting at the superblock, in case we crash before the
1922 * file is closed/deleted, or in case the inode truncate spans multiple
1923 * transactions and the last transaction is not recovered after a crash.
1925 * At filesystem recovery time, we walk this list deleting unlinked
1926 * inodes and truncating linked inodes in ext3_orphan_cleanup().
1928 int ext3_orphan_add(handle_t
*handle
, struct inode
*inode
)
1930 struct super_block
*sb
= inode
->i_sb
;
1931 struct ext3_iloc iloc
;
1934 mutex_lock(&EXT3_SB(sb
)->s_orphan_lock
);
1935 if (!list_empty(&EXT3_I(inode
)->i_orphan
))
1938 /* Orphan handling is only valid for files with data blocks
1939 * being truncated, or files being unlinked. */
1941 /* @@@ FIXME: Observation from aviro:
1942 * I think I can trigger J_ASSERT in ext3_orphan_add(). We block
1943 * here (on s_orphan_lock), so race with ext3_link() which might bump
1944 * ->i_nlink. For, say it, character device. Not a regular file,
1945 * not a directory, not a symlink and ->i_nlink > 0.
1947 * tytso, 4/25/2009: I'm not sure how that could happen;
1948 * shouldn't the fs core protect us from these sort of
1949 * unlink()/link() races?
1951 J_ASSERT ((S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
1952 S_ISLNK(inode
->i_mode
)) || inode
->i_nlink
== 0);
1954 BUFFER_TRACE(EXT3_SB(sb
)->s_sbh
, "get_write_access");
1955 err
= ext3_journal_get_write_access(handle
, EXT3_SB(sb
)->s_sbh
);
1959 err
= ext3_reserve_inode_write(handle
, inode
, &iloc
);
1963 /* Insert this inode at the head of the on-disk orphan list... */
1964 NEXT_ORPHAN(inode
) = le32_to_cpu(EXT3_SB(sb
)->s_es
->s_last_orphan
);
1965 EXT3_SB(sb
)->s_es
->s_last_orphan
= cpu_to_le32(inode
->i_ino
);
1966 err
= ext3_journal_dirty_metadata(handle
, EXT3_SB(sb
)->s_sbh
);
1967 rc
= ext3_mark_iloc_dirty(handle
, inode
, &iloc
);
1971 /* Only add to the head of the in-memory list if all the
1972 * previous operations succeeded. If the orphan_add is going to
1973 * fail (possibly taking the journal offline), we can't risk
1974 * leaving the inode on the orphan list: stray orphan-list
1975 * entries can cause panics at unmount time.
1977 * This is safe: on error we're going to ignore the orphan list
1978 * anyway on the next recovery. */
1980 list_add(&EXT3_I(inode
)->i_orphan
, &EXT3_SB(sb
)->s_orphan
);
1982 jbd_debug(4, "superblock will point to %lu\n", inode
->i_ino
);
1983 jbd_debug(4, "orphan inode %lu will point to %d\n",
1984 inode
->i_ino
, NEXT_ORPHAN(inode
));
1986 mutex_unlock(&EXT3_SB(sb
)->s_orphan_lock
);
1987 ext3_std_error(inode
->i_sb
, err
);
1992 * ext3_orphan_del() removes an unlinked or truncated inode from the list
1993 * of such inodes stored on disk, because it is finally being cleaned up.
1995 int ext3_orphan_del(handle_t
*handle
, struct inode
*inode
)
1997 struct list_head
*prev
;
1998 struct ext3_inode_info
*ei
= EXT3_I(inode
);
1999 struct ext3_sb_info
*sbi
;
2000 unsigned long ino_next
;
2001 struct ext3_iloc iloc
;
2004 mutex_lock(&EXT3_SB(inode
->i_sb
)->s_orphan_lock
);
2005 if (list_empty(&ei
->i_orphan
))
2008 ino_next
= NEXT_ORPHAN(inode
);
2009 prev
= ei
->i_orphan
.prev
;
2010 sbi
= EXT3_SB(inode
->i_sb
);
2012 jbd_debug(4, "remove inode %lu from orphan list\n", inode
->i_ino
);
2014 list_del_init(&ei
->i_orphan
);
2016 /* If we're on an error path, we may not have a valid
2017 * transaction handle with which to update the orphan list on
2018 * disk, but we still need to remove the inode from the linked
2019 * list in memory. */
2023 err
= ext3_reserve_inode_write(handle
, inode
, &iloc
);
2027 if (prev
== &sbi
->s_orphan
) {
2028 jbd_debug(4, "superblock will point to %lu\n", ino_next
);
2029 BUFFER_TRACE(sbi
->s_sbh
, "get_write_access");
2030 err
= ext3_journal_get_write_access(handle
, sbi
->s_sbh
);
2033 sbi
->s_es
->s_last_orphan
= cpu_to_le32(ino_next
);
2034 err
= ext3_journal_dirty_metadata(handle
, sbi
->s_sbh
);
2036 struct ext3_iloc iloc2
;
2037 struct inode
*i_prev
=
2038 &list_entry(prev
, struct ext3_inode_info
, i_orphan
)->vfs_inode
;
2040 jbd_debug(4, "orphan inode %lu will point to %lu\n",
2041 i_prev
->i_ino
, ino_next
);
2042 err
= ext3_reserve_inode_write(handle
, i_prev
, &iloc2
);
2045 NEXT_ORPHAN(i_prev
) = ino_next
;
2046 err
= ext3_mark_iloc_dirty(handle
, i_prev
, &iloc2
);
2050 NEXT_ORPHAN(inode
) = 0;
2051 err
= ext3_mark_iloc_dirty(handle
, inode
, &iloc
);
2054 ext3_std_error(inode
->i_sb
, err
);
2056 mutex_unlock(&EXT3_SB(inode
->i_sb
)->s_orphan_lock
);
2064 static int ext3_rmdir (struct inode
* dir
, struct dentry
*dentry
)
2067 struct inode
* inode
;
2068 struct buffer_head
* bh
;
2069 struct ext3_dir_entry_2
* de
;
2072 /* Initialize quotas before so that eventual writes go in
2073 * separate transaction */
2074 dquot_initialize(dir
);
2075 dquot_initialize(dentry
->d_inode
);
2077 handle
= ext3_journal_start(dir
, EXT3_DELETE_TRANS_BLOCKS(dir
->i_sb
));
2079 return PTR_ERR(handle
);
2082 bh
= ext3_find_entry(dir
, &dentry
->d_name
, &de
);
2086 if (IS_DIRSYNC(dir
))
2089 inode
= dentry
->d_inode
;
2092 if (le32_to_cpu(de
->inode
) != inode
->i_ino
)
2095 retval
= -ENOTEMPTY
;
2096 if (!empty_dir (inode
))
2099 retval
= ext3_delete_entry(handle
, dir
, de
, bh
);
2102 if (inode
->i_nlink
!= 2)
2103 ext3_warning (inode
->i_sb
, "ext3_rmdir",
2104 "empty directory has nlink!=2 (%d)",
2108 /* There's no need to set i_disksize: the fact that i_nlink is
2109 * zero will ensure that the right thing happens during any
2112 ext3_orphan_add(handle
, inode
);
2113 inode
->i_ctime
= dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME_SEC
;
2114 ext3_mark_inode_dirty(handle
, inode
);
2116 ext3_update_dx_flag(dir
);
2117 ext3_mark_inode_dirty(handle
, dir
);
2120 ext3_journal_stop(handle
);
2125 static int ext3_unlink(struct inode
* dir
, struct dentry
*dentry
)
2128 struct inode
* inode
;
2129 struct buffer_head
* bh
;
2130 struct ext3_dir_entry_2
* de
;
2133 trace_ext3_unlink_enter(dir
, dentry
);
2134 /* Initialize quotas before so that eventual writes go
2135 * in separate transaction */
2136 dquot_initialize(dir
);
2137 dquot_initialize(dentry
->d_inode
);
2139 handle
= ext3_journal_start(dir
, EXT3_DELETE_TRANS_BLOCKS(dir
->i_sb
));
2141 return PTR_ERR(handle
);
2143 if (IS_DIRSYNC(dir
))
2147 bh
= ext3_find_entry(dir
, &dentry
->d_name
, &de
);
2151 inode
= dentry
->d_inode
;
2154 if (le32_to_cpu(de
->inode
) != inode
->i_ino
)
2157 if (!inode
->i_nlink
) {
2158 ext3_warning (inode
->i_sb
, "ext3_unlink",
2159 "Deleting nonexistent file (%lu), %d",
2160 inode
->i_ino
, inode
->i_nlink
);
2161 set_nlink(inode
, 1);
2163 retval
= ext3_delete_entry(handle
, dir
, de
, bh
);
2166 dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME_SEC
;
2167 ext3_update_dx_flag(dir
);
2168 ext3_mark_inode_dirty(handle
, dir
);
2170 if (!inode
->i_nlink
)
2171 ext3_orphan_add(handle
, inode
);
2172 inode
->i_ctime
= dir
->i_ctime
;
2173 ext3_mark_inode_dirty(handle
, inode
);
2177 ext3_journal_stop(handle
);
2179 trace_ext3_unlink_exit(dentry
, retval
);
2183 static int ext3_symlink (struct inode
* dir
,
2184 struct dentry
*dentry
, const char * symname
)
2187 struct inode
* inode
;
2188 int l
, err
, retries
= 0;
2191 l
= strlen(symname
)+1;
2192 if (l
> dir
->i_sb
->s_blocksize
)
2193 return -ENAMETOOLONG
;
2195 dquot_initialize(dir
);
2197 if (l
> EXT3_N_BLOCKS
* 4) {
2199 * For non-fast symlinks, we just allocate inode and put it on
2200 * orphan list in the first transaction => we need bitmap,
2201 * group descriptor, sb, inode block, quota blocks, and
2202 * possibly selinux xattr blocks.
2204 credits
= 4 + EXT3_MAXQUOTAS_INIT_BLOCKS(dir
->i_sb
) +
2205 EXT3_XATTR_TRANS_BLOCKS
;
2208 * Fast symlink. We have to add entry to directory
2209 * (EXT3_DATA_TRANS_BLOCKS + EXT3_INDEX_EXTRA_TRANS_BLOCKS),
2210 * allocate new inode (bitmap, group descriptor, inode block,
2211 * quota blocks, sb is already counted in previous macros).
2213 credits
= EXT3_DATA_TRANS_BLOCKS(dir
->i_sb
) +
2214 EXT3_INDEX_EXTRA_TRANS_BLOCKS
+ 3 +
2215 EXT3_MAXQUOTAS_INIT_BLOCKS(dir
->i_sb
);
2218 handle
= ext3_journal_start(dir
, credits
);
2220 return PTR_ERR(handle
);
2222 if (IS_DIRSYNC(dir
))
2225 inode
= ext3_new_inode (handle
, dir
, &dentry
->d_name
, S_IFLNK
|S_IRWXUGO
);
2226 err
= PTR_ERR(inode
);
2230 if (l
> EXT3_N_BLOCKS
* 4) {
2231 inode
->i_op
= &ext3_symlink_inode_operations
;
2232 ext3_set_aops(inode
);
2234 * We cannot call page_symlink() with transaction started
2235 * because it calls into ext3_write_begin() which acquires page
2236 * lock which ranks below transaction start (and it can also
2237 * wait for journal commit if we are running out of space). So
2238 * we have to stop transaction now and restart it when symlink
2239 * contents is written.
2241 * To keep fs consistent in case of crash, we have to put inode
2242 * to orphan list in the mean time.
2245 err
= ext3_orphan_add(handle
, inode
);
2246 ext3_journal_stop(handle
);
2248 goto err_drop_inode
;
2249 err
= __page_symlink(inode
, symname
, l
, 1);
2251 goto err_drop_inode
;
2253 * Now inode is being linked into dir (EXT3_DATA_TRANS_BLOCKS
2254 * + EXT3_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
2256 handle
= ext3_journal_start(dir
,
2257 EXT3_DATA_TRANS_BLOCKS(dir
->i_sb
) +
2258 EXT3_INDEX_EXTRA_TRANS_BLOCKS
+ 1);
2259 if (IS_ERR(handle
)) {
2260 err
= PTR_ERR(handle
);
2261 goto err_drop_inode
;
2263 set_nlink(inode
, 1);
2264 err
= ext3_orphan_del(handle
, inode
);
2266 ext3_journal_stop(handle
);
2268 goto err_drop_inode
;
2271 inode
->i_op
= &ext3_fast_symlink_inode_operations
;
2272 memcpy((char*)&EXT3_I(inode
)->i_data
,symname
,l
);
2273 inode
->i_size
= l
-1;
2275 EXT3_I(inode
)->i_disksize
= inode
->i_size
;
2276 err
= ext3_add_nondir(handle
, dentry
, inode
);
2278 ext3_journal_stop(handle
);
2279 if (err
== -ENOSPC
&& ext3_should_retry_alloc(dir
->i_sb
, &retries
))
2283 unlock_new_inode(inode
);
2288 static int ext3_link (struct dentry
* old_dentry
,
2289 struct inode
* dir
, struct dentry
*dentry
)
2292 struct inode
*inode
= old_dentry
->d_inode
;
2293 int err
, retries
= 0;
2295 if (inode
->i_nlink
>= EXT3_LINK_MAX
)
2298 dquot_initialize(dir
);
2301 handle
= ext3_journal_start(dir
, EXT3_DATA_TRANS_BLOCKS(dir
->i_sb
) +
2302 EXT3_INDEX_EXTRA_TRANS_BLOCKS
);
2304 return PTR_ERR(handle
);
2306 if (IS_DIRSYNC(dir
))
2309 inode
->i_ctime
= CURRENT_TIME_SEC
;
2313 err
= ext3_add_entry(handle
, dentry
, inode
);
2315 ext3_mark_inode_dirty(handle
, inode
);
2316 d_instantiate(dentry
, inode
);
2321 ext3_journal_stop(handle
);
2322 if (err
== -ENOSPC
&& ext3_should_retry_alloc(dir
->i_sb
, &retries
))
2327 #define PARENT_INO(buffer) \
2328 (ext3_next_entry((struct ext3_dir_entry_2 *)(buffer))->inode)
2331 * Anybody can rename anything with this: the permission checks are left to the
2332 * higher-level routines.
2334 static int ext3_rename (struct inode
* old_dir
, struct dentry
*old_dentry
,
2335 struct inode
* new_dir
,struct dentry
*new_dentry
)
2338 struct inode
* old_inode
, * new_inode
;
2339 struct buffer_head
* old_bh
, * new_bh
, * dir_bh
;
2340 struct ext3_dir_entry_2
* old_de
, * new_de
;
2341 int retval
, flush_file
= 0;
2343 dquot_initialize(old_dir
);
2344 dquot_initialize(new_dir
);
2346 old_bh
= new_bh
= dir_bh
= NULL
;
2348 /* Initialize quotas before so that eventual writes go
2349 * in separate transaction */
2350 if (new_dentry
->d_inode
)
2351 dquot_initialize(new_dentry
->d_inode
);
2352 handle
= ext3_journal_start(old_dir
, 2 *
2353 EXT3_DATA_TRANS_BLOCKS(old_dir
->i_sb
) +
2354 EXT3_INDEX_EXTRA_TRANS_BLOCKS
+ 2);
2356 return PTR_ERR(handle
);
2358 if (IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
))
2361 old_bh
= ext3_find_entry(old_dir
, &old_dentry
->d_name
, &old_de
);
2363 * Check for inode number is _not_ due to possible IO errors.
2364 * We might rmdir the source, keep it as pwd of some process
2365 * and merrily kill the link to whatever was created under the
2366 * same name. Goodbye sticky bit ;-<
2368 old_inode
= old_dentry
->d_inode
;
2370 if (!old_bh
|| le32_to_cpu(old_de
->inode
) != old_inode
->i_ino
)
2373 new_inode
= new_dentry
->d_inode
;
2374 new_bh
= ext3_find_entry(new_dir
, &new_dentry
->d_name
, &new_de
);
2381 if (S_ISDIR(old_inode
->i_mode
)) {
2383 retval
= -ENOTEMPTY
;
2384 if (!empty_dir (new_inode
))
2388 dir_bh
= ext3_bread (handle
, old_inode
, 0, 0, &retval
);
2391 if (le32_to_cpu(PARENT_INO(dir_bh
->b_data
)) != old_dir
->i_ino
)
2394 if (!new_inode
&& new_dir
!=old_dir
&&
2395 new_dir
->i_nlink
>= EXT3_LINK_MAX
)
2399 retval
= ext3_add_entry (handle
, new_dentry
, old_inode
);
2403 BUFFER_TRACE(new_bh
, "get write access");
2404 retval
= ext3_journal_get_write_access(handle
, new_bh
);
2407 new_de
->inode
= cpu_to_le32(old_inode
->i_ino
);
2408 if (EXT3_HAS_INCOMPAT_FEATURE(new_dir
->i_sb
,
2409 EXT3_FEATURE_INCOMPAT_FILETYPE
))
2410 new_de
->file_type
= old_de
->file_type
;
2411 new_dir
->i_version
++;
2412 new_dir
->i_ctime
= new_dir
->i_mtime
= CURRENT_TIME_SEC
;
2413 ext3_mark_inode_dirty(handle
, new_dir
);
2414 BUFFER_TRACE(new_bh
, "call ext3_journal_dirty_metadata");
2415 retval
= ext3_journal_dirty_metadata(handle
, new_bh
);
2423 * Like most other Unix systems, set the ctime for inodes on a
2426 old_inode
->i_ctime
= CURRENT_TIME_SEC
;
2427 ext3_mark_inode_dirty(handle
, old_inode
);
2432 if (le32_to_cpu(old_de
->inode
) != old_inode
->i_ino
||
2433 old_de
->name_len
!= old_dentry
->d_name
.len
||
2434 strncmp(old_de
->name
, old_dentry
->d_name
.name
, old_de
->name_len
) ||
2435 (retval
= ext3_delete_entry(handle
, old_dir
,
2436 old_de
, old_bh
)) == -ENOENT
) {
2437 /* old_de could have moved from under us during htree split, so
2438 * make sure that we are deleting the right entry. We might
2439 * also be pointing to a stale entry in the unused part of
2440 * old_bh so just checking inum and the name isn't enough. */
2441 struct buffer_head
*old_bh2
;
2442 struct ext3_dir_entry_2
*old_de2
;
2444 old_bh2
= ext3_find_entry(old_dir
, &old_dentry
->d_name
,
2447 retval
= ext3_delete_entry(handle
, old_dir
,
2453 ext3_warning(old_dir
->i_sb
, "ext3_rename",
2454 "Deleting old file (%lu), %d, error=%d",
2455 old_dir
->i_ino
, old_dir
->i_nlink
, retval
);
2459 drop_nlink(new_inode
);
2460 new_inode
->i_ctime
= CURRENT_TIME_SEC
;
2462 old_dir
->i_ctime
= old_dir
->i_mtime
= CURRENT_TIME_SEC
;
2463 ext3_update_dx_flag(old_dir
);
2465 BUFFER_TRACE(dir_bh
, "get_write_access");
2466 retval
= ext3_journal_get_write_access(handle
, dir_bh
);
2469 PARENT_INO(dir_bh
->b_data
) = cpu_to_le32(new_dir
->i_ino
);
2470 BUFFER_TRACE(dir_bh
, "call ext3_journal_dirty_metadata");
2471 retval
= ext3_journal_dirty_metadata(handle
, dir_bh
);
2474 ext3_std_error(new_dir
->i_sb
, retval
);
2477 drop_nlink(old_dir
);
2479 drop_nlink(new_inode
);
2482 ext3_update_dx_flag(new_dir
);
2483 ext3_mark_inode_dirty(handle
, new_dir
);
2486 ext3_mark_inode_dirty(handle
, old_dir
);
2488 ext3_mark_inode_dirty(handle
, new_inode
);
2489 if (!new_inode
->i_nlink
)
2490 ext3_orphan_add(handle
, new_inode
);
2491 if (ext3_should_writeback_data(new_inode
))
2500 ext3_journal_stop(handle
);
2501 if (retval
== 0 && flush_file
)
2502 filemap_flush(old_inode
->i_mapping
);
2507 * directories can handle most operations...
2509 const struct inode_operations ext3_dir_inode_operations
= {
2510 .create
= ext3_create
,
2511 .lookup
= ext3_lookup
,
2513 .unlink
= ext3_unlink
,
2514 .symlink
= ext3_symlink
,
2515 .mkdir
= ext3_mkdir
,
2516 .rmdir
= ext3_rmdir
,
2517 .mknod
= ext3_mknod
,
2518 .rename
= ext3_rename
,
2519 .setattr
= ext3_setattr
,
2520 #ifdef CONFIG_EXT3_FS_XATTR
2521 .setxattr
= generic_setxattr
,
2522 .getxattr
= generic_getxattr
,
2523 .listxattr
= ext3_listxattr
,
2524 .removexattr
= generic_removexattr
,
2526 .get_acl
= ext3_get_acl
,
2529 const struct inode_operations ext3_special_inode_operations
= {
2530 .setattr
= ext3_setattr
,
2531 #ifdef CONFIG_EXT3_FS_XATTR
2532 .setxattr
= generic_setxattr
,
2533 .getxattr
= generic_getxattr
,
2534 .listxattr
= ext3_listxattr
,
2535 .removexattr
= generic_removexattr
,
2537 .get_acl
= ext3_get_acl
,