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
28 #include <linux/pagemap.h>
29 #include <linux/jbd.h>
30 #include <linux/time.h>
31 #include <linux/ext3_fs.h>
32 #include <linux/ext3_jbd.h>
33 #include <linux/fcntl.h>
34 #include <linux/stat.h>
35 #include <linux/string.h>
36 #include <linux/quotaops.h>
37 #include <linux/buffer_head.h>
38 #include <linux/smp_lock.h>
43 * define how far ahead to read directories while searching them.
45 #define NAMEI_RA_CHUNKS 2
46 #define NAMEI_RA_BLOCKS 4
47 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
48 #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
50 static struct buffer_head
*ext3_append(handle_t
*handle
,
54 struct buffer_head
*bh
;
56 *block
= inode
->i_size
>> inode
->i_sb
->s_blocksize_bits
;
58 if ((bh
= ext3_bread(handle
, inode
, *block
, 1, err
))) {
59 inode
->i_size
+= inode
->i_sb
->s_blocksize
;
60 EXT3_I(inode
)->i_disksize
= inode
->i_size
;
61 ext3_journal_get_write_access(handle
,bh
);
67 #define assert(test) J_ASSERT(test)
71 #define swap(x, y) do { typeof(x) z = x; x = y; y = z; } while (0)
74 typedef struct { u32 v
; } le_u32
;
75 typedef struct { u16 v
; } le_u16
;
78 #define dxtrace(command) command
80 #define dxtrace(command)
104 * dx_root_info is laid out so that if it should somehow get overlaid by a
105 * dirent the two low bits of the hash version will be zero. Therefore, the
106 * hash version mod 4 should never be 0. Sincerely, the paranoia department.
111 struct fake_dirent dot
;
113 struct fake_dirent dotdot
;
117 le_u32 reserved_zero
;
119 u8 info_length
; /* 8 */
124 struct dx_entry entries
[0];
129 struct fake_dirent fake
;
130 struct dx_entry entries
[0];
136 struct buffer_head
*bh
;
137 struct dx_entry
*entries
;
147 #ifdef CONFIG_EXT3_INDEX
148 static inline unsigned dx_get_block (struct dx_entry
*entry
);
149 static void dx_set_block (struct dx_entry
*entry
, unsigned value
);
150 static inline unsigned dx_get_hash (struct dx_entry
*entry
);
151 static void dx_set_hash (struct dx_entry
*entry
, unsigned value
);
152 static unsigned dx_get_count (struct dx_entry
*entries
);
153 static unsigned dx_get_limit (struct dx_entry
*entries
);
154 static void dx_set_count (struct dx_entry
*entries
, unsigned value
);
155 static void dx_set_limit (struct dx_entry
*entries
, unsigned value
);
156 static unsigned dx_root_limit (struct inode
*dir
, unsigned infosize
);
157 static unsigned dx_node_limit (struct inode
*dir
);
158 static struct dx_frame
*dx_probe(struct dentry
*dentry
,
160 struct dx_hash_info
*hinfo
,
161 struct dx_frame
*frame
,
163 static void dx_release (struct dx_frame
*frames
);
164 static int dx_make_map (struct ext3_dir_entry_2
*de
, int size
,
165 struct dx_hash_info
*hinfo
, struct dx_map_entry map
[]);
166 static void dx_sort_map(struct dx_map_entry
*map
, unsigned count
);
167 static struct ext3_dir_entry_2
*dx_move_dirents (char *from
, char *to
,
168 struct dx_map_entry
*offsets
, int count
);
169 static struct ext3_dir_entry_2
* dx_pack_dirents (char *base
, int size
);
170 static void dx_insert_block (struct dx_frame
*frame
, u32 hash
, u32 block
);
171 static int ext3_htree_next_block(struct inode
*dir
, __u32 hash
,
172 struct dx_frame
*frame
,
173 struct dx_frame
*frames
, int *err
,
175 static struct buffer_head
* ext3_dx_find_entry(struct dentry
*dentry
,
176 struct ext3_dir_entry_2
**res_dir
, int *err
);
177 static int ext3_dx_add_entry(handle_t
*handle
, struct dentry
*dentry
,
178 struct inode
*inode
);
181 * Future: use high four bits of block for coalesce-on-delete flags
182 * Mask them off for now.
185 static inline unsigned dx_get_block (struct dx_entry
*entry
)
187 return le32_to_cpu(entry
->block
.v
) & 0x00ffffff;
190 static inline void dx_set_block (struct dx_entry
*entry
, unsigned value
)
192 entry
->block
.v
= cpu_to_le32(value
);
195 static inline unsigned dx_get_hash (struct dx_entry
*entry
)
197 return le32_to_cpu(entry
->hash
.v
);
200 static inline void dx_set_hash (struct dx_entry
*entry
, unsigned value
)
202 entry
->hash
.v
= 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
.v
);
210 static inline unsigned dx_get_limit (struct dx_entry
*entries
)
212 return le16_to_cpu(((struct dx_countlimit
*) entries
)->limit
.v
);
215 static inline void dx_set_count (struct dx_entry
*entries
, unsigned value
)
217 ((struct dx_countlimit
*) entries
)->count
.v
= cpu_to_le16(value
);
220 static inline void dx_set_limit (struct dx_entry
*entries
, unsigned value
)
222 ((struct dx_countlimit
*) entries
)->limit
.v
= 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 0? 20: entry_space
/ sizeof(struct dx_entry
);
232 static inline unsigned dx_node_limit (struct inode
*dir
)
234 unsigned entry_space
= dir
->i_sb
->s_blocksize
- EXT3_DIR_REC_LEN(0);
235 return 0? 22: entry_space
/ sizeof(struct dx_entry
);
249 static struct stats
dx_show_leaf(struct dx_hash_info
*hinfo
, struct ext3_dir_entry_2
*de
,
250 int size
, int show_names
)
252 unsigned names
= 0, space
= 0;
253 char *base
= (char *) de
;
254 struct dx_hash_info h
= *hinfo
;
257 while ((char *) de
< base
+ size
)
263 int len
= de
->name_len
;
264 char *name
= de
->name
;
265 while (len
--) printk("%c", *name
++);
266 ext3fs_dirhash(de
->name
, de
->name_len
, &h
);
267 printk(":%x.%u ", h
.hash
,
268 ((char *) de
- base
));
270 space
+= EXT3_DIR_REC_LEN(de
->name_len
);
273 de
= (struct ext3_dir_entry_2
*) ((char *) de
+ le16_to_cpu(de
->rec_len
));
275 printk("(%i)\n", names
);
276 return (struct stats
) { names
, space
, 1 };
279 struct stats
dx_show_entries(struct dx_hash_info
*hinfo
, struct inode
*dir
,
280 struct dx_entry
*entries
, int levels
)
282 unsigned blocksize
= dir
->i_sb
->s_blocksize
;
283 unsigned count
= dx_get_count (entries
), names
= 0, space
= 0, i
;
285 struct buffer_head
*bh
;
287 printk("%i indexed blocks...\n", count
);
288 for (i
= 0; i
< count
; i
++, entries
++)
290 u32 block
= dx_get_block(entries
), hash
= i
? dx_get_hash(entries
): 0;
291 u32 range
= i
< count
- 1? (dx_get_hash(entries
+ 1) - hash
): ~hash
;
293 printk("%s%3u:%03u hash %8x/%8x ",levels
?"":" ", i
, block
, hash
, range
);
294 if (!(bh
= ext3_bread (NULL
,dir
, block
, 0,&err
))) continue;
296 dx_show_entries(hinfo
, dir
, ((struct dx_node
*) bh
->b_data
)->entries
, levels
- 1):
297 dx_show_leaf(hinfo
, (struct ext3_dir_entry_2
*) bh
->b_data
, blocksize
, 0);
298 names
+= stats
.names
;
299 space
+= stats
.space
;
300 bcount
+= stats
.bcount
;
304 printk("%snames %u, fullness %u (%u%%)\n", levels
?"":" ",
305 names
, space
/bcount
,(space
/bcount
)*100/blocksize
);
306 return (struct stats
) { names
, space
, bcount
};
308 #endif /* DX_DEBUG */
311 * Probe for a directory leaf block to search.
313 * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
314 * error in the directory index, and the caller should fall back to
315 * searching the directory normally. The callers of dx_probe **MUST**
316 * check for this error code, and make sure it never gets reflected
319 static struct dx_frame
*
320 dx_probe(struct dentry
*dentry
, struct inode
*dir
,
321 struct dx_hash_info
*hinfo
, struct dx_frame
*frame_in
, int *err
)
323 unsigned count
, indirect
;
324 struct dx_entry
*at
, *entries
, *p
, *q
, *m
;
325 struct dx_root
*root
;
326 struct buffer_head
*bh
;
327 struct dx_frame
*frame
= frame_in
;
332 dir
= dentry
->d_parent
->d_inode
;
333 if (!(bh
= ext3_bread (NULL
,dir
, 0, 0, err
)))
335 root
= (struct dx_root
*) bh
->b_data
;
336 if (root
->info
.hash_version
!= DX_HASH_TEA
&&
337 root
->info
.hash_version
!= DX_HASH_HALF_MD4
&&
338 root
->info
.hash_version
!= DX_HASH_LEGACY
) {
339 ext3_warning(dir
->i_sb
, __FUNCTION__
,
340 "Unrecognised inode hash code %d",
341 root
->info
.hash_version
);
343 *err
= ERR_BAD_DX_DIR
;
346 hinfo
->hash_version
= root
->info
.hash_version
;
347 hinfo
->seed
= EXT3_SB(dir
->i_sb
)->s_hash_seed
;
349 ext3fs_dirhash(dentry
->d_name
.name
, dentry
->d_name
.len
, hinfo
);
352 if (root
->info
.unused_flags
& 1) {
353 ext3_warning(dir
->i_sb
, __FUNCTION__
,
354 "Unimplemented inode hash flags: %#06x",
355 root
->info
.unused_flags
);
357 *err
= ERR_BAD_DX_DIR
;
361 if ((indirect
= root
->info
.indirect_levels
) > 1) {
362 ext3_warning(dir
->i_sb
, __FUNCTION__
,
363 "Unimplemented inode hash depth: %#06x",
364 root
->info
.indirect_levels
);
366 *err
= ERR_BAD_DX_DIR
;
370 entries
= (struct dx_entry
*) (((char *)&root
->info
) +
371 root
->info
.info_length
);
372 assert(dx_get_limit(entries
) == dx_root_limit(dir
,
373 root
->info
.info_length
));
374 dxtrace (printk("Look up %x", hash
));
377 count
= dx_get_count(entries
);
378 assert (count
&& count
<= dx_get_limit(entries
));
380 q
= entries
+ count
- 1;
384 dxtrace(printk("."));
385 if (dx_get_hash(m
) > hash
)
391 if (0) // linear search cross check
393 unsigned n
= count
- 1;
397 dxtrace(printk(","));
398 if (dx_get_hash(++at
) > hash
)
404 assert (at
== p
- 1);
408 dxtrace(printk(" %x->%u\n", at
== entries
? 0: dx_get_hash(at
), dx_get_block(at
)));
410 frame
->entries
= entries
;
412 if (!indirect
--) return frame
;
413 if (!(bh
= ext3_bread (NULL
,dir
, dx_get_block(at
), 0, err
)))
415 at
= entries
= ((struct dx_node
*) bh
->b_data
)->entries
;
416 assert (dx_get_limit(entries
) == dx_node_limit (dir
));
420 while (frame
>= frame_in
) {
428 static void dx_release (struct dx_frame
*frames
)
430 if (frames
[0].bh
== NULL
)
433 if (((struct dx_root
*) frames
[0].bh
->b_data
)->info
.indirect_levels
)
434 brelse(frames
[1].bh
);
435 brelse(frames
[0].bh
);
439 * This function increments the frame pointer to search the next leaf
440 * block, and reads in the necessary intervening nodes if the search
441 * should be necessary. Whether or not the search is necessary is
442 * controlled by the hash parameter. If the hash value is even, then
443 * the search is only continued if the next block starts with that
444 * hash value. This is used if we are searching for a specific file.
446 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
448 * This function returns 1 if the caller should continue to search,
449 * or 0 if it should not. If there is an error reading one of the
450 * index blocks, it will return -1.
452 * If start_hash is non-null, it will be filled in with the starting
453 * hash of the next page.
455 static int ext3_htree_next_block(struct inode
*dir
, __u32 hash
,
456 struct dx_frame
*frame
,
457 struct dx_frame
*frames
, int *err
,
461 struct buffer_head
*bh
;
468 * Find the next leaf page by incrementing the frame pointer.
469 * If we run out of entries in the interior node, loop around and
470 * increment pointer in the parent node. When we break out of
471 * this loop, num_frames indicates the number of interior
472 * nodes need to be read.
475 if (++(p
->at
) < p
->entries
+ dx_get_count(p
->entries
))
484 * If the hash is 1, then continue only if the next page has a
485 * continuation hash of any value. This is used for readdir
486 * handling. Otherwise, check to see if the hash matches the
487 * desired contiuation hash. If it doesn't, return since
488 * there's no point to read in the successive index pages.
490 bhash
= dx_get_hash(p
->at
);
493 if ((hash
& 1) == 0) {
494 if ((bhash
& ~1) != hash
)
498 * If the hash is HASH_NB_ALWAYS, we always go to the next
499 * block so no check is necessary
501 while (num_frames
--) {
502 if (!(bh
= ext3_bread(NULL
, dir
, dx_get_block(p
->at
),
504 return -1; /* Failure */
508 p
->at
= p
->entries
= ((struct dx_node
*) bh
->b_data
)->entries
;
515 * p is at least 6 bytes before the end of page
517 static inline struct ext3_dir_entry_2
*ext3_next_entry(struct ext3_dir_entry_2
*p
)
519 return (struct ext3_dir_entry_2
*)((char*)p
+ le16_to_cpu(p
->rec_len
));
523 * This function fills a red-black tree with information from a
524 * directory. We start scanning the directory in hash order, starting
525 * at start_hash and start_minor_hash.
527 * This function returns the number of entries inserted into the tree,
528 * or a negative error code.
530 int ext3_htree_fill_tree(struct file
*dir_file
, __u32 start_hash
,
531 __u32 start_minor_hash
, __u32
*next_hash
)
533 struct dx_hash_info hinfo
;
534 struct buffer_head
*bh
;
535 struct ext3_dir_entry_2
*de
, *top
;
536 static struct dx_frame frames
[2], *frame
;
543 dxtrace(printk("In htree_fill_tree, start hash: %x:%x\n", start_hash
,
545 dir
= dir_file
->f_dentry
->d_inode
;
546 hinfo
.hash
= start_hash
;
547 hinfo
.minor_hash
= 0;
548 frame
= dx_probe(0, dir_file
->f_dentry
->d_inode
, &hinfo
, frames
, &err
);
552 /* Add '.' and '..' from the htree header */
553 if (!start_hash
&& !start_minor_hash
) {
554 de
= (struct ext3_dir_entry_2
*) frames
[0].bh
->b_data
;
555 if ((err
= ext3_htree_store_dirent(dir_file
, 0, 0, de
)) != 0)
557 de
= ext3_next_entry(de
);
558 if ((err
= ext3_htree_store_dirent(dir_file
, 0, 0, de
)) != 0)
564 block
= dx_get_block(frame
->at
);
565 dxtrace(printk("Reading block %d\n", block
));
566 if (!(bh
= ext3_bread (NULL
, dir
, block
, 0, &err
)))
569 de
= (struct ext3_dir_entry_2
*) bh
->b_data
;
570 top
= (struct ext3_dir_entry_2
*) ((char *) de
+ dir
->i_sb
->s_blocksize
-
571 EXT3_DIR_REC_LEN(0));
572 for (; de
< top
; de
= ext3_next_entry(de
)) {
573 ext3fs_dirhash(de
->name
, de
->name_len
, &hinfo
);
574 if ((hinfo
.hash
< start_hash
) ||
575 ((hinfo
.hash
== start_hash
) &&
576 (hinfo
.minor_hash
< start_minor_hash
)))
578 if ((err
= ext3_htree_store_dirent(dir_file
,
579 hinfo
.hash
, hinfo
.minor_hash
, de
)) != 0) {
587 ret
= ext3_htree_next_block(dir
, HASH_NB_ALWAYS
,
588 frame
, frames
, &err
, &hashval
);
590 *next_hash
= hashval
;
594 * Stop if: (a) there are no more entries, or
595 * (b) we have inserted at least one entry and the
596 * next hash value is not a continuation
599 (count
&& ((hashval
& 1) == 0)))
603 dxtrace(printk("Fill tree: returned %d entries\n", count
));
612 * Directory block splitting, compacting
615 static int dx_make_map (struct ext3_dir_entry_2
*de
, int size
,
616 struct dx_hash_info
*hinfo
, struct dx_map_entry
*map_tail
)
619 char *base
= (char *) de
;
620 struct dx_hash_info h
= *hinfo
;
622 while ((char *) de
< base
+ size
)
624 if (de
->name_len
&& de
->inode
) {
625 ext3fs_dirhash(de
->name
, de
->name_len
, &h
);
627 map_tail
->hash
= h
.hash
;
628 map_tail
->offs
= (u32
) ((char *) de
- base
);
631 /* XXX: do we need to check rec_len == 0 case? -Chris */
632 de
= (struct ext3_dir_entry_2
*) ((char *) de
+ le16_to_cpu(de
->rec_len
));
637 static void dx_sort_map (struct dx_map_entry
*map
, unsigned count
)
639 struct dx_map_entry
*p
, *q
, *top
= map
+ count
- 1;
641 /* Combsort until bubble sort doesn't suck */
645 if (count
- 9 < 2) /* 9, 10 -> 11 */
647 for (p
= top
, q
= p
- count
; q
>= map
; p
--, q
--)
648 if (p
->hash
< q
->hash
)
651 /* Garden variety bubble sort */
657 if (q
[1].hash
>= q
[0].hash
)
665 static void dx_insert_block(struct dx_frame
*frame
, u32 hash
, u32 block
)
667 struct dx_entry
*entries
= frame
->entries
;
668 struct dx_entry
*old
= frame
->at
, *new = old
+ 1;
669 int count
= dx_get_count(entries
);
671 assert(count
< dx_get_limit(entries
));
672 assert(old
< entries
+ count
);
673 memmove(new + 1, new, (char *)(entries
+ count
) - (char *)(new));
674 dx_set_hash(new, hash
);
675 dx_set_block(new, block
);
676 dx_set_count(entries
, count
+ 1);
681 static void ext3_update_dx_flag(struct inode
*inode
)
683 if (!EXT3_HAS_COMPAT_FEATURE(inode
->i_sb
,
684 EXT3_FEATURE_COMPAT_DIR_INDEX
))
685 EXT3_I(inode
)->i_flags
&= ~EXT3_INDEX_FL
;
689 * NOTE! unlike strncmp, ext3_match returns 1 for success, 0 for failure.
691 * `len <= EXT3_NAME_LEN' is guaranteed by caller.
692 * `de != NULL' is guaranteed by caller.
694 static inline int ext3_match (int len
, const char * const name
,
695 struct ext3_dir_entry_2
* de
)
697 if (len
!= de
->name_len
)
701 return !memcmp(name
, de
->name
, len
);
705 * Returns 0 if not found, -1 on failure, and 1 on success
707 static inline int search_dirblock(struct buffer_head
* bh
,
709 struct dentry
*dentry
,
710 unsigned long offset
,
711 struct ext3_dir_entry_2
** res_dir
)
713 struct ext3_dir_entry_2
* de
;
716 const char *name
= dentry
->d_name
.name
;
717 int namelen
= dentry
->d_name
.len
;
719 de
= (struct ext3_dir_entry_2
*) bh
->b_data
;
720 dlimit
= bh
->b_data
+ dir
->i_sb
->s_blocksize
;
721 while ((char *) de
< dlimit
) {
722 /* this code is executed quadratically often */
723 /* do minimal checking `by hand' */
725 if ((char *) de
+ namelen
<= dlimit
&&
726 ext3_match (namelen
, name
, de
)) {
727 /* found a match - just to be sure, do a full check */
728 if (!ext3_check_dir_entry("ext3_find_entry",
729 dir
, de
, bh
, offset
))
734 /* prevent looping on a bad block */
735 de_len
= le16_to_cpu(de
->rec_len
);
739 de
= (struct ext3_dir_entry_2
*) ((char *) de
+ de_len
);
748 * finds an entry in the specified directory with the wanted name. It
749 * returns the cache buffer in which the entry was found, and the entry
750 * itself (as a parameter - res_dir). It does NOT read the inode of the
751 * entry - you'll have to do that yourself if you want to.
753 * The returned buffer_head has ->b_count elevated. The caller is expected
754 * to brelse() it when appropriate.
758 static struct buffer_head
* ext3_find_entry (struct dentry
*dentry
,
759 struct ext3_dir_entry_2
** res_dir
)
761 struct super_block
* sb
;
762 struct buffer_head
* bh_use
[NAMEI_RA_SIZE
];
763 struct buffer_head
* bh
, *ret
= NULL
;
764 unsigned long start
, block
, b
;
765 int ra_max
= 0; /* Number of bh's in the readahead
767 int ra_ptr
= 0; /* Current index into readahead
771 struct inode
*dir
= dentry
->d_parent
->d_inode
;
778 blocksize
= sb
->s_blocksize
;
779 namelen
= dentry
->d_name
.len
;
780 name
= dentry
->d_name
.name
;
781 if (namelen
> EXT3_NAME_LEN
)
783 #ifdef CONFIG_EXT3_INDEX
785 bh
= ext3_dx_find_entry(dentry
, res_dir
, &err
);
787 * On success, or if the error was file not found,
788 * return. Otherwise, fall back to doing a search the
791 if (bh
|| (err
!= ERR_BAD_DX_DIR
))
793 dxtrace(printk("ext3_find_entry: dx failed, falling back\n"));
796 nblocks
= dir
->i_size
>> EXT3_BLOCK_SIZE_BITS(sb
);
797 start
= EXT3_I(dir
)->i_dir_start_lookup
;
798 if (start
>= nblocks
)
804 * We deal with the read-ahead logic here.
806 if (ra_ptr
>= ra_max
) {
807 /* Refill the readahead buffer */
810 for (ra_max
= 0; ra_max
< NAMEI_RA_SIZE
; ra_max
++) {
812 * Terminate if we reach the end of the
813 * directory and must wrap, or if our
814 * search has finished at this block.
816 if (b
>= nblocks
|| (num
&& block
== start
)) {
817 bh_use
[ra_max
] = NULL
;
821 bh
= ext3_getblk(NULL
, dir
, b
++, 0, &err
);
824 ll_rw_block(READ
, 1, &bh
);
827 if ((bh
= bh_use
[ra_ptr
++]) == NULL
)
830 if (!buffer_uptodate(bh
)) {
831 /* read error, skip block & hope for the best */
835 i
= search_dirblock(bh
, dir
, dentry
,
836 block
<< EXT3_BLOCK_SIZE_BITS(sb
), res_dir
);
838 EXT3_I(dir
)->i_dir_start_lookup
= block
;
840 goto cleanup_and_exit
;
844 goto cleanup_and_exit
;
847 if (++block
>= nblocks
)
849 } while (block
!= start
);
852 * If the directory has grown while we were searching, then
853 * search the last part of the directory before giving up.
856 nblocks
= dir
->i_size
>> EXT3_BLOCK_SIZE_BITS(sb
);
857 if (block
< nblocks
) {
863 /* Clean up the read-ahead blocks */
864 for (; ra_ptr
< ra_max
; ra_ptr
++)
865 brelse (bh_use
[ra_ptr
]);
869 #ifdef CONFIG_EXT3_INDEX
870 static struct buffer_head
* ext3_dx_find_entry(struct dentry
*dentry
,
871 struct ext3_dir_entry_2
**res_dir
, int *err
)
873 struct super_block
* sb
;
874 struct dx_hash_info hinfo
;
876 struct dx_frame frames
[2], *frame
;
877 struct ext3_dir_entry_2
*de
, *top
;
878 struct buffer_head
*bh
;
881 int namelen
= dentry
->d_name
.len
;
882 const u8
*name
= dentry
->d_name
.name
;
883 struct inode
*dir
= dentry
->d_parent
->d_inode
;
886 if (!(frame
= dx_probe (dentry
, 0, &hinfo
, frames
, err
)))
890 block
= dx_get_block(frame
->at
);
891 if (!(bh
= ext3_bread (NULL
,dir
, block
, 0, err
)))
893 de
= (struct ext3_dir_entry_2
*) bh
->b_data
;
894 top
= (struct ext3_dir_entry_2
*) ((char *) de
+ sb
->s_blocksize
-
895 EXT3_DIR_REC_LEN(0));
896 for (; de
< top
; de
= ext3_next_entry(de
))
897 if (ext3_match (namelen
, name
, de
)) {
898 if (!ext3_check_dir_entry("ext3_find_entry",
900 (block
<<EXT3_BLOCK_SIZE_BITS(sb
))
901 +((char *)de
- bh
->b_data
))) {
910 /* Check to see if we should continue to search */
911 retval
= ext3_htree_next_block(dir
, hash
, frame
,
914 ext3_warning(sb
, __FUNCTION__
,
915 "error reading index page in directory #%lu",
919 } while (retval
== 1);
923 dxtrace(printk("%s not found\n", name
));
929 static struct dentry
*ext3_lookup(struct inode
* dir
, struct dentry
*dentry
)
931 struct inode
* inode
;
932 struct ext3_dir_entry_2
* de
;
933 struct buffer_head
* bh
;
935 if (dentry
->d_name
.len
> EXT3_NAME_LEN
)
936 return ERR_PTR(-ENAMETOOLONG
);
939 bh
= ext3_find_entry(dentry
, &de
);
942 unsigned long ino
= le32_to_cpu(de
->inode
);
944 inode
= iget(dir
->i_sb
, ino
);
948 return ERR_PTR(-EACCES
);
953 return d_splice_alias(inode
, dentry
);
954 d_add(dentry
, inode
);
959 struct dentry
*ext3_get_parent(struct dentry
*child
)
962 struct dentry
*parent
;
964 struct dentry dotdot
;
965 struct ext3_dir_entry_2
* de
;
966 struct buffer_head
*bh
;
968 dotdot
.d_name
.name
= "..";
969 dotdot
.d_name
.len
= 2;
970 dotdot
.d_parent
= child
; /* confusing, isn't it! */
973 bh
= ext3_find_entry(&dotdot
, &de
);
977 return ERR_PTR(-ENOENT
);
979 ino
= le32_to_cpu(de
->inode
);
981 inode
= iget(child
->d_inode
->i_sb
, ino
);
985 return ERR_PTR(-EACCES
);
987 parent
= d_alloc_anon(inode
);
990 parent
= ERR_PTR(-ENOMEM
);
996 static unsigned char ext3_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
997 [S_IFREG
>> S_SHIFT
] = EXT3_FT_REG_FILE
,
998 [S_IFDIR
>> S_SHIFT
] = EXT3_FT_DIR
,
999 [S_IFCHR
>> S_SHIFT
] = EXT3_FT_CHRDEV
,
1000 [S_IFBLK
>> S_SHIFT
] = EXT3_FT_BLKDEV
,
1001 [S_IFIFO
>> S_SHIFT
] = EXT3_FT_FIFO
,
1002 [S_IFSOCK
>> S_SHIFT
] = EXT3_FT_SOCK
,
1003 [S_IFLNK
>> S_SHIFT
] = EXT3_FT_SYMLINK
,
1006 static inline void ext3_set_de_type(struct super_block
*sb
,
1007 struct ext3_dir_entry_2
*de
,
1009 if (EXT3_HAS_INCOMPAT_FEATURE(sb
, EXT3_FEATURE_INCOMPAT_FILETYPE
))
1010 de
->file_type
= ext3_type_by_mode
[(mode
& S_IFMT
)>>S_SHIFT
];
1013 #ifdef CONFIG_EXT3_INDEX
1014 static struct ext3_dir_entry_2
*
1015 dx_move_dirents(char *from
, char *to
, struct dx_map_entry
*map
, int count
)
1017 unsigned rec_len
= 0;
1020 struct ext3_dir_entry_2
*de
= (struct ext3_dir_entry_2
*) (from
+ map
->offs
);
1021 rec_len
= EXT3_DIR_REC_LEN(de
->name_len
);
1022 memcpy (to
, de
, rec_len
);
1023 ((struct ext3_dir_entry_2
*) to
)->rec_len
=
1024 cpu_to_le16(rec_len
);
1029 return (struct ext3_dir_entry_2
*) (to
- rec_len
);
1032 static struct ext3_dir_entry_2
* dx_pack_dirents(char *base
, int size
)
1034 struct ext3_dir_entry_2
*next
, *to
, *prev
, *de
= (struct ext3_dir_entry_2
*) base
;
1035 unsigned rec_len
= 0;
1038 while ((char*)de
< base
+ size
) {
1039 next
= (struct ext3_dir_entry_2
*) ((char *) de
+
1040 le16_to_cpu(de
->rec_len
));
1041 if (de
->inode
&& de
->name_len
) {
1042 rec_len
= EXT3_DIR_REC_LEN(de
->name_len
);
1044 memmove(to
, de
, rec_len
);
1045 to
->rec_len
= cpu_to_le16(rec_len
);
1047 to
= (struct ext3_dir_entry_2
*) (((char *) to
) + rec_len
);
1054 static struct ext3_dir_entry_2
*do_split(handle_t
*handle
, struct inode
*dir
,
1055 struct buffer_head
**bh
,struct dx_frame
*frame
,
1056 struct dx_hash_info
*hinfo
, int *error
)
1058 unsigned blocksize
= dir
->i_sb
->s_blocksize
;
1059 unsigned count
, continued
;
1060 struct buffer_head
*bh2
;
1063 struct dx_map_entry
*map
;
1064 char *data1
= (*bh
)->b_data
, *data2
;
1066 struct ext3_dir_entry_2
*de
= NULL
, *de2
;
1069 bh2
= ext3_append (handle
, dir
, &newblock
, error
);
1076 BUFFER_TRACE(*bh
, "get_write_access");
1077 err
= ext3_journal_get_write_access(handle
, *bh
);
1083 ext3_std_error(dir
->i_sb
, err
);
1086 BUFFER_TRACE(frame
->bh
, "get_write_access");
1087 err
= ext3_journal_get_write_access(handle
, frame
->bh
);
1091 data2
= bh2
->b_data
;
1093 /* create map in the end of data2 block */
1094 map
= (struct dx_map_entry
*) (data2
+ blocksize
);
1095 count
= dx_make_map ((struct ext3_dir_entry_2
*) data1
,
1096 blocksize
, hinfo
, map
);
1098 split
= count
/2; // need to adjust to actual middle
1099 dx_sort_map (map
, count
);
1100 hash2
= map
[split
].hash
;
1101 continued
= hash2
== map
[split
- 1].hash
;
1102 dxtrace(printk("Split block %i at %x, %i/%i\n",
1103 dx_get_block(frame
->at
), hash2
, split
, count
-split
));
1105 /* Fancy dance to stay within two buffers */
1106 de2
= dx_move_dirents(data1
, data2
, map
+ split
, count
- split
);
1107 de
= dx_pack_dirents(data1
,blocksize
);
1108 de
->rec_len
= cpu_to_le16(data1
+ blocksize
- (char *) de
);
1109 de2
->rec_len
= cpu_to_le16(data2
+ blocksize
- (char *) de2
);
1110 dxtrace(dx_show_leaf (hinfo
, (struct ext3_dir_entry_2
*) data1
, blocksize
, 1));
1111 dxtrace(dx_show_leaf (hinfo
, (struct ext3_dir_entry_2
*) data2
, blocksize
, 1));
1113 /* Which block gets the new entry? */
1114 if (hinfo
->hash
>= hash2
)
1119 dx_insert_block (frame
, hash2
+ continued
, newblock
);
1120 err
= ext3_journal_dirty_metadata (handle
, bh2
);
1123 err
= ext3_journal_dirty_metadata (handle
, frame
->bh
);
1127 dxtrace(dx_show_index ("frame", frame
->entries
));
1135 * Add a new entry into a directory (leaf) block. If de is non-NULL,
1136 * it points to a directory entry which is guaranteed to be large
1137 * enough for new directory entry. If de is NULL, then
1138 * add_dirent_to_buf will attempt search the directory block for
1139 * space. It will return -ENOSPC if no space is available, and -EIO
1140 * and -EEXIST if directory entry already exists.
1142 * NOTE! bh is NOT released in the case where ENOSPC is returned. In
1143 * all other cases bh is released.
1145 static int add_dirent_to_buf(handle_t
*handle
, struct dentry
*dentry
,
1146 struct inode
*inode
, struct ext3_dir_entry_2
*de
,
1147 struct buffer_head
* bh
)
1149 struct inode
*dir
= dentry
->d_parent
->d_inode
;
1150 const char *name
= dentry
->d_name
.name
;
1151 int namelen
= dentry
->d_name
.len
;
1152 unsigned long offset
= 0;
1153 unsigned short reclen
;
1154 int nlen
, rlen
, err
;
1157 reclen
= EXT3_DIR_REC_LEN(namelen
);
1159 de
= (struct ext3_dir_entry_2
*)bh
->b_data
;
1160 top
= bh
->b_data
+ dir
->i_sb
->s_blocksize
- reclen
;
1161 while ((char *) de
<= top
) {
1162 if (!ext3_check_dir_entry("ext3_add_entry", dir
, de
,
1167 if (ext3_match (namelen
, name
, de
)) {
1171 nlen
= EXT3_DIR_REC_LEN(de
->name_len
);
1172 rlen
= le16_to_cpu(de
->rec_len
);
1173 if ((de
->inode
? rlen
- nlen
: rlen
) >= reclen
)
1175 de
= (struct ext3_dir_entry_2
*)((char *)de
+ rlen
);
1178 if ((char *) de
> top
)
1181 BUFFER_TRACE(bh
, "get_write_access");
1182 err
= ext3_journal_get_write_access(handle
, bh
);
1184 ext3_std_error(dir
->i_sb
, err
);
1189 /* By now the buffer is marked for journaling */
1190 nlen
= EXT3_DIR_REC_LEN(de
->name_len
);
1191 rlen
= le16_to_cpu(de
->rec_len
);
1193 struct ext3_dir_entry_2
*de1
= (struct ext3_dir_entry_2
*)((char *)de
+ nlen
);
1194 de1
->rec_len
= cpu_to_le16(rlen
- nlen
);
1195 de
->rec_len
= cpu_to_le16(nlen
);
1198 de
->file_type
= EXT3_FT_UNKNOWN
;
1200 de
->inode
= cpu_to_le32(inode
->i_ino
);
1201 ext3_set_de_type(dir
->i_sb
, de
, inode
->i_mode
);
1204 de
->name_len
= namelen
;
1205 memcpy (de
->name
, name
, namelen
);
1207 * XXX shouldn't update any times until successful
1208 * completion of syscall, but too many callers depend
1211 * XXX similarly, too many callers depend on
1212 * ext3_new_inode() setting the times, but error
1213 * recovery deletes the inode, so the worst that can
1214 * happen is that the times are slightly out of date
1215 * and/or different from the directory change time.
1217 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
1218 ext3_update_dx_flag(dir
);
1220 ext3_mark_inode_dirty(handle
, dir
);
1221 BUFFER_TRACE(bh
, "call ext3_journal_dirty_metadata");
1222 err
= ext3_journal_dirty_metadata(handle
, bh
);
1224 ext3_std_error(dir
->i_sb
, err
);
1229 #ifdef CONFIG_EXT3_INDEX
1231 * This converts a one block unindexed directory to a 3 block indexed
1232 * directory, and adds the dentry to the indexed directory.
1234 static int make_indexed_dir(handle_t
*handle
, struct dentry
*dentry
,
1235 struct inode
*inode
, struct buffer_head
*bh
)
1237 struct inode
*dir
= dentry
->d_parent
->d_inode
;
1238 const char *name
= dentry
->d_name
.name
;
1239 int namelen
= dentry
->d_name
.len
;
1240 struct buffer_head
*bh2
;
1241 struct dx_root
*root
;
1242 struct dx_frame frames
[2], *frame
;
1243 struct dx_entry
*entries
;
1244 struct ext3_dir_entry_2
*de
, *de2
;
1249 struct dx_hash_info hinfo
;
1252 blocksize
= dir
->i_sb
->s_blocksize
;
1253 dxtrace(printk("Creating index\n"));
1254 retval
= ext3_journal_get_write_access(handle
, bh
);
1256 ext3_std_error(dir
->i_sb
, retval
);
1260 root
= (struct dx_root
*) bh
->b_data
;
1262 EXT3_I(dir
)->i_flags
|= EXT3_INDEX_FL
;
1263 bh2
= ext3_append (handle
, dir
, &block
, &retval
);
1268 data1
= bh2
->b_data
;
1270 /* The 0th block becomes the root, move the dirents out */
1271 de
= (struct ext3_dir_entry_2
*) &root
->info
;
1272 len
= ((char *) root
) + blocksize
- (char *) de
;
1273 memcpy (data1
, de
, len
);
1274 de
= (struct ext3_dir_entry_2
*) data1
;
1276 while (((char *) de2
=(char*)de
+le16_to_cpu(de
->rec_len
)) < top
)
1278 de
->rec_len
= cpu_to_le16(data1
+ blocksize
- (char *) de
);
1279 /* Initialize the root; the dot dirents already exist */
1280 de
= (struct ext3_dir_entry_2
*) (&root
->dotdot
);
1281 de
->rec_len
= cpu_to_le16(blocksize
- EXT3_DIR_REC_LEN(2));
1282 memset (&root
->info
, 0, sizeof(root
->info
));
1283 root
->info
.info_length
= sizeof(root
->info
);
1284 root
->info
.hash_version
= EXT3_SB(dir
->i_sb
)->s_def_hash_version
;
1285 entries
= root
->entries
;
1286 dx_set_block (entries
, 1);
1287 dx_set_count (entries
, 1);
1288 dx_set_limit (entries
, dx_root_limit(dir
, sizeof(root
->info
)));
1290 /* Initialize as for dx_probe */
1291 hinfo
.hash_version
= root
->info
.hash_version
;
1292 hinfo
.seed
= EXT3_SB(dir
->i_sb
)->s_hash_seed
;
1293 ext3fs_dirhash(name
, namelen
, &hinfo
);
1295 frame
->entries
= entries
;
1296 frame
->at
= entries
;
1299 de
= do_split(handle
,dir
, &bh
, frame
, &hinfo
, &retval
);
1300 dx_release (frames
);
1304 return add_dirent_to_buf(handle
, dentry
, inode
, de
, bh
);
1311 * adds a file entry to the specified directory, using the same
1312 * semantics as ext3_find_entry(). It returns NULL if it failed.
1314 * NOTE!! The inode part of 'de' is left at 0 - which means you
1315 * may not sleep between calling this and putting something into
1316 * the entry, as someone else might have used it while you slept.
1318 static int ext3_add_entry (handle_t
*handle
, struct dentry
*dentry
,
1319 struct inode
*inode
)
1321 struct inode
*dir
= dentry
->d_parent
->d_inode
;
1322 unsigned long offset
;
1323 struct buffer_head
* bh
;
1324 struct ext3_dir_entry_2
*de
;
1325 struct super_block
* sb
;
1327 #ifdef CONFIG_EXT3_INDEX
1331 unsigned nlen
, rlen
;
1335 blocksize
= sb
->s_blocksize
;
1336 if (!dentry
->d_name
.len
)
1338 #ifdef CONFIG_EXT3_INDEX
1340 retval
= ext3_dx_add_entry(handle
, dentry
, inode
);
1341 if (!retval
|| (retval
!= ERR_BAD_DX_DIR
))
1343 EXT3_I(dir
)->i_flags
&= ~EXT3_INDEX_FL
;
1345 ext3_mark_inode_dirty(handle
, dir
);
1348 blocks
= dir
->i_size
>> sb
->s_blocksize_bits
;
1349 for (block
= 0, offset
= 0; block
< blocks
; block
++) {
1350 bh
= ext3_bread(handle
, dir
, block
, 0, &retval
);
1353 retval
= add_dirent_to_buf(handle
, dentry
, inode
, 0, bh
);
1354 if (retval
!= -ENOSPC
)
1357 #ifdef CONFIG_EXT3_INDEX
1358 if (blocks
== 1 && !dx_fallback
&&
1359 EXT3_HAS_COMPAT_FEATURE(sb
, EXT3_FEATURE_COMPAT_DIR_INDEX
))
1360 return make_indexed_dir(handle
, dentry
, inode
, bh
);
1364 bh
= ext3_append(handle
, dir
, &block
, &retval
);
1367 de
= (struct ext3_dir_entry_2
*) bh
->b_data
;
1369 de
->rec_len
= cpu_to_le16(rlen
= blocksize
);
1371 return add_dirent_to_buf(handle
, dentry
, inode
, de
, bh
);
1374 #ifdef CONFIG_EXT3_INDEX
1376 * Returns 0 for success, or a negative error value
1378 static int ext3_dx_add_entry(handle_t
*handle
, struct dentry
*dentry
,
1379 struct inode
*inode
)
1381 struct dx_frame frames
[2], *frame
;
1382 struct dx_entry
*entries
, *at
;
1383 struct dx_hash_info hinfo
;
1384 struct buffer_head
* bh
;
1385 struct inode
*dir
= dentry
->d_parent
->d_inode
;
1386 struct super_block
* sb
= dir
->i_sb
;
1387 struct ext3_dir_entry_2
*de
;
1390 frame
= dx_probe(dentry
, 0, &hinfo
, frames
, &err
);
1393 entries
= frame
->entries
;
1396 if (!(bh
= ext3_bread(handle
,dir
, dx_get_block(frame
->at
), 0, &err
)))
1399 BUFFER_TRACE(bh
, "get_write_access");
1400 err
= ext3_journal_get_write_access(handle
, bh
);
1404 err
= add_dirent_to_buf(handle
, dentry
, inode
, 0, bh
);
1405 if (err
!= -ENOSPC
) {
1410 /* Block full, should compress but for now just split */
1411 dxtrace(printk("using %u of %u node entries\n",
1412 dx_get_count(entries
), dx_get_limit(entries
)));
1413 /* Need to split index? */
1414 if (dx_get_count(entries
) == dx_get_limit(entries
)) {
1416 unsigned icount
= dx_get_count(entries
);
1417 int levels
= frame
- frames
;
1418 struct dx_entry
*entries2
;
1419 struct dx_node
*node2
;
1420 struct buffer_head
*bh2
;
1422 if (levels
&& (dx_get_count(frames
->entries
) ==
1423 dx_get_limit(frames
->entries
))) {
1424 ext3_warning(sb
, __FUNCTION__
,
1425 "Directory index full!\n");
1429 bh2
= ext3_append (handle
, dir
, &newblock
, &err
);
1432 node2
= (struct dx_node
*)(bh2
->b_data
);
1433 entries2
= node2
->entries
;
1434 node2
->fake
.rec_len
= cpu_to_le16(sb
->s_blocksize
);
1435 node2
->fake
.inode
= 0;
1436 BUFFER_TRACE(frame
->bh
, "get_write_access");
1437 err
= ext3_journal_get_write_access(handle
, frame
->bh
);
1441 unsigned icount1
= icount
/2, icount2
= icount
- icount1
;
1442 unsigned hash2
= dx_get_hash(entries
+ icount1
);
1443 dxtrace(printk("Split index %i/%i\n", icount1
, icount2
));
1445 BUFFER_TRACE(frame
->bh
, "get_write_access"); /* index root */
1446 err
= ext3_journal_get_write_access(handle
,
1451 memcpy ((char *) entries2
, (char *) (entries
+ icount1
),
1452 icount2
* sizeof(struct dx_entry
));
1453 dx_set_count (entries
, icount1
);
1454 dx_set_count (entries2
, icount2
);
1455 dx_set_limit (entries2
, dx_node_limit(dir
));
1457 /* Which index block gets the new entry? */
1458 if (at
- entries
>= icount1
) {
1459 frame
->at
= at
= at
- entries
- icount1
+ entries2
;
1460 frame
->entries
= entries
= entries2
;
1461 swap(frame
->bh
, bh2
);
1463 dx_insert_block (frames
+ 0, hash2
, newblock
);
1464 dxtrace(dx_show_index ("node", frames
[1].entries
));
1465 dxtrace(dx_show_index ("node",
1466 ((struct dx_node
*) bh2
->b_data
)->entries
));
1467 err
= ext3_journal_dirty_metadata(handle
, bh2
);
1472 dxtrace(printk("Creating second level index...\n"));
1473 memcpy((char *) entries2
, (char *) entries
,
1474 icount
* sizeof(struct dx_entry
));
1475 dx_set_limit(entries2
, dx_node_limit(dir
));
1478 dx_set_count(entries
, 1);
1479 dx_set_block(entries
+ 0, newblock
);
1480 ((struct dx_root
*) frames
[0].bh
->b_data
)->info
.indirect_levels
= 1;
1482 /* Add new access path frame */
1484 frame
->at
= at
= at
- entries
+ entries2
;
1485 frame
->entries
= entries
= entries2
;
1487 err
= ext3_journal_get_write_access(handle
,
1492 ext3_journal_dirty_metadata(handle
, frames
[0].bh
);
1494 de
= do_split(handle
, dir
, &bh
, frame
, &hinfo
, &err
);
1497 err
= add_dirent_to_buf(handle
, dentry
, inode
, de
, bh
);
1502 ext3_std_error(dir
->i_sb
, err
);
1512 * ext3_delete_entry deletes a directory entry by merging it with the
1515 static int ext3_delete_entry (handle_t
*handle
,
1517 struct ext3_dir_entry_2
* de_del
,
1518 struct buffer_head
* bh
)
1520 struct ext3_dir_entry_2
* de
, * pde
;
1525 de
= (struct ext3_dir_entry_2
*) bh
->b_data
;
1526 while (i
< bh
->b_size
) {
1527 if (!ext3_check_dir_entry("ext3_delete_entry", dir
, de
, bh
, i
))
1530 BUFFER_TRACE(bh
, "get_write_access");
1531 ext3_journal_get_write_access(handle
, bh
);
1534 cpu_to_le16(le16_to_cpu(pde
->rec_len
) +
1535 le16_to_cpu(de
->rec_len
));
1539 BUFFER_TRACE(bh
, "call ext3_journal_dirty_metadata");
1540 ext3_journal_dirty_metadata(handle
, bh
);
1543 i
+= le16_to_cpu(de
->rec_len
);
1545 de
= (struct ext3_dir_entry_2
*)
1546 ((char *) de
+ le16_to_cpu(de
->rec_len
));
1552 * ext3_mark_inode_dirty is somewhat expensive, so unlike ext2 we
1553 * do not perform it in these functions. We perform it at the call site,
1556 static inline void ext3_inc_count(handle_t
*handle
, struct inode
*inode
)
1561 static inline void ext3_dec_count(handle_t
*handle
, struct inode
*inode
)
1566 static int ext3_add_nondir(handle_t
*handle
,
1567 struct dentry
*dentry
, struct inode
*inode
)
1569 int err
= ext3_add_entry(handle
, dentry
, inode
);
1571 err
= ext3_mark_inode_dirty(handle
, inode
);
1573 d_instantiate(dentry
, inode
);
1577 ext3_dec_count(handle
, inode
);
1583 * By the time this is called, we already have created
1584 * the directory cache entry for the new file, but it
1585 * is so far negative - it has no inode.
1587 * If the create succeeds, we fill in the inode information
1588 * with d_instantiate().
1590 static int ext3_create (struct inode
* dir
, struct dentry
* dentry
, int mode
)
1593 struct inode
* inode
;
1597 handle
= ext3_journal_start(dir
, EXT3_DATA_TRANS_BLOCKS
+
1598 EXT3_INDEX_EXTRA_TRANS_BLOCKS
+ 3);
1599 if (IS_ERR(handle
)) {
1601 return PTR_ERR(handle
);
1604 if (IS_DIRSYNC(dir
))
1607 inode
= ext3_new_inode (handle
, dir
, mode
);
1608 err
= PTR_ERR(inode
);
1609 if (!IS_ERR(inode
)) {
1610 inode
->i_op
= &ext3_file_inode_operations
;
1611 inode
->i_fop
= &ext3_file_operations
;
1612 if (ext3_should_writeback_data(inode
))
1613 inode
->i_mapping
->a_ops
= &ext3_writeback_aops
;
1615 inode
->i_mapping
->a_ops
= &ext3_aops
;
1616 err
= ext3_add_nondir(handle
, dentry
, inode
);
1618 ext3_journal_stop(handle
, dir
);
1623 static int ext3_mknod (struct inode
* dir
, struct dentry
*dentry
,
1624 int mode
, dev_t rdev
)
1627 struct inode
*inode
;
1631 handle
= ext3_journal_start(dir
, EXT3_DATA_TRANS_BLOCKS
+
1632 EXT3_INDEX_EXTRA_TRANS_BLOCKS
+ 3);
1633 if (IS_ERR(handle
)) {
1635 return PTR_ERR(handle
);
1638 if (IS_DIRSYNC(dir
))
1641 inode
= ext3_new_inode (handle
, dir
, mode
);
1642 err
= PTR_ERR(inode
);
1643 if (!IS_ERR(inode
)) {
1644 init_special_inode(inode
, inode
->i_mode
, rdev
);
1645 #ifdef CONFIG_EXT3_FS_XATTR
1646 inode
->i_op
= &ext3_special_inode_operations
;
1648 err
= ext3_add_nondir(handle
, dentry
, inode
);
1650 ext3_journal_stop(handle
, dir
);
1655 static int ext3_mkdir(struct inode
* dir
, struct dentry
* dentry
, int mode
)
1658 struct inode
* inode
;
1659 struct buffer_head
* dir_block
;
1660 struct ext3_dir_entry_2
* de
;
1663 if (dir
->i_nlink
>= EXT3_LINK_MAX
)
1667 handle
= ext3_journal_start(dir
, EXT3_DATA_TRANS_BLOCKS
+
1668 EXT3_INDEX_EXTRA_TRANS_BLOCKS
+ 3);
1669 if (IS_ERR(handle
)) {
1671 return PTR_ERR(handle
);
1674 if (IS_DIRSYNC(dir
))
1677 inode
= ext3_new_inode (handle
, dir
, S_IFDIR
| mode
);
1678 err
= PTR_ERR(inode
);
1682 inode
->i_op
= &ext3_dir_inode_operations
;
1683 inode
->i_fop
= &ext3_dir_operations
;
1684 inode
->i_size
= EXT3_I(inode
)->i_disksize
= inode
->i_sb
->s_blocksize
;
1685 dir_block
= ext3_bread (handle
, inode
, 0, 1, &err
);
1687 inode
->i_nlink
--; /* is this nlink == 0? */
1688 ext3_mark_inode_dirty(handle
, inode
);
1692 BUFFER_TRACE(dir_block
, "get_write_access");
1693 ext3_journal_get_write_access(handle
, dir_block
);
1694 de
= (struct ext3_dir_entry_2
*) dir_block
->b_data
;
1695 de
->inode
= cpu_to_le32(inode
->i_ino
);
1697 de
->rec_len
= cpu_to_le16(EXT3_DIR_REC_LEN(de
->name_len
));
1698 strcpy (de
->name
, ".");
1699 ext3_set_de_type(dir
->i_sb
, de
, S_IFDIR
);
1700 de
= (struct ext3_dir_entry_2
*)
1701 ((char *) de
+ le16_to_cpu(de
->rec_len
));
1702 de
->inode
= cpu_to_le32(dir
->i_ino
);
1703 de
->rec_len
= cpu_to_le16(inode
->i_sb
->s_blocksize
-EXT3_DIR_REC_LEN(1));
1705 strcpy (de
->name
, "..");
1706 ext3_set_de_type(dir
->i_sb
, de
, S_IFDIR
);
1708 BUFFER_TRACE(dir_block
, "call ext3_journal_dirty_metadata");
1709 ext3_journal_dirty_metadata(handle
, dir_block
);
1711 ext3_mark_inode_dirty(handle
, inode
);
1712 err
= ext3_add_entry (handle
, dentry
, inode
);
1715 ext3_mark_inode_dirty(handle
, inode
);
1720 ext3_update_dx_flag(dir
);
1721 ext3_mark_inode_dirty(handle
, dir
);
1722 d_instantiate(dentry
, inode
);
1724 ext3_journal_stop(handle
, dir
);
1730 * routine to check that the specified directory is empty (for rmdir)
1732 static int empty_dir (struct inode
* inode
)
1734 unsigned long offset
;
1735 struct buffer_head
* bh
;
1736 struct ext3_dir_entry_2
* de
, * de1
;
1737 struct super_block
* sb
;
1741 if (inode
->i_size
< EXT3_DIR_REC_LEN(1) + EXT3_DIR_REC_LEN(2) ||
1742 !(bh
= ext3_bread (NULL
, inode
, 0, 0, &err
))) {
1743 ext3_warning (inode
->i_sb
, "empty_dir",
1744 "bad directory (dir #%lu) - no data block",
1748 de
= (struct ext3_dir_entry_2
*) bh
->b_data
;
1749 de1
= (struct ext3_dir_entry_2
*)
1750 ((char *) de
+ le16_to_cpu(de
->rec_len
));
1751 if (le32_to_cpu(de
->inode
) != inode
->i_ino
||
1752 !le32_to_cpu(de1
->inode
) ||
1753 strcmp (".", de
->name
) ||
1754 strcmp ("..", de1
->name
)) {
1755 ext3_warning (inode
->i_sb
, "empty_dir",
1756 "bad directory (dir #%lu) - no `.' or `..'",
1761 offset
= le16_to_cpu(de
->rec_len
) + le16_to_cpu(de1
->rec_len
);
1762 de
= (struct ext3_dir_entry_2
*)
1763 ((char *) de1
+ le16_to_cpu(de1
->rec_len
));
1764 while (offset
< inode
->i_size
) {
1766 (void *) de
>= (void *) (bh
->b_data
+sb
->s_blocksize
)) {
1768 bh
= ext3_bread (NULL
, inode
,
1769 offset
>> EXT3_BLOCK_SIZE_BITS(sb
), 0, &err
);
1772 ext3_error (sb
, "empty_dir",
1773 "directory #%lu contains a hole at offset %lu",
1774 inode
->i_ino
, offset
);
1776 offset
+= sb
->s_blocksize
;
1779 de
= (struct ext3_dir_entry_2
*) bh
->b_data
;
1781 if (!ext3_check_dir_entry ("empty_dir", inode
, de
, bh
,
1786 if (le32_to_cpu(de
->inode
)) {
1790 offset
+= le16_to_cpu(de
->rec_len
);
1791 de
= (struct ext3_dir_entry_2
*)
1792 ((char *) de
+ le16_to_cpu(de
->rec_len
));
1798 /* ext3_orphan_add() links an unlinked or truncated inode into a list of
1799 * such inodes, starting at the superblock, in case we crash before the
1800 * file is closed/deleted, or in case the inode truncate spans multiple
1801 * transactions and the last transaction is not recovered after a crash.
1803 * At filesystem recovery time, we walk this list deleting unlinked
1804 * inodes and truncating linked inodes in ext3_orphan_cleanup().
1806 int ext3_orphan_add(handle_t
*handle
, struct inode
*inode
)
1808 struct super_block
*sb
= inode
->i_sb
;
1809 struct ext3_iloc iloc
;
1813 if (!list_empty(&EXT3_I(inode
)->i_orphan
))
1816 /* Orphan handling is only valid for files with data blocks
1817 * being truncated, or files being unlinked. */
1819 /* @@@ FIXME: Observation from aviro:
1820 * I think I can trigger J_ASSERT in ext3_orphan_add(). We block
1821 * here (on lock_super()), so race with ext3_link() which might bump
1822 * ->i_nlink. For, say it, character device. Not a regular file,
1823 * not a directory, not a symlink and ->i_nlink > 0.
1825 J_ASSERT ((S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
1826 S_ISLNK(inode
->i_mode
)) || inode
->i_nlink
== 0);
1828 BUFFER_TRACE(EXT3_SB(sb
)->s_sbh
, "get_write_access");
1829 err
= ext3_journal_get_write_access(handle
, EXT3_SB(sb
)->s_sbh
);
1833 err
= ext3_reserve_inode_write(handle
, inode
, &iloc
);
1837 /* Insert this inode at the head of the on-disk orphan list... */
1838 NEXT_ORPHAN(inode
) = le32_to_cpu(EXT3_SB(sb
)->s_es
->s_last_orphan
);
1839 EXT3_SB(sb
)->s_es
->s_last_orphan
= cpu_to_le32(inode
->i_ino
);
1840 err
= ext3_journal_dirty_metadata(handle
, EXT3_SB(sb
)->s_sbh
);
1841 rc
= ext3_mark_iloc_dirty(handle
, inode
, &iloc
);
1845 /* Only add to the head of the in-memory list if all the
1846 * previous operations succeeded. If the orphan_add is going to
1847 * fail (possibly taking the journal offline), we can't risk
1848 * leaving the inode on the orphan list: stray orphan-list
1849 * entries can cause panics at unmount time.
1851 * This is safe: on error we're going to ignore the orphan list
1852 * anyway on the next recovery. */
1854 list_add(&EXT3_I(inode
)->i_orphan
, &EXT3_SB(sb
)->s_orphan
);
1856 jbd_debug(4, "superblock will point to %ld\n", inode
->i_ino
);
1857 jbd_debug(4, "orphan inode %ld will point to %d\n",
1858 inode
->i_ino
, NEXT_ORPHAN(inode
));
1861 ext3_std_error(inode
->i_sb
, err
);
1866 * ext3_orphan_del() removes an unlinked or truncated inode from the list
1867 * of such inodes stored on disk, because it is finally being cleaned up.
1869 int ext3_orphan_del(handle_t
*handle
, struct inode
*inode
)
1871 struct list_head
*prev
;
1872 struct ext3_inode_info
*ei
= EXT3_I(inode
);
1873 struct ext3_sb_info
*sbi
;
1874 unsigned long ino_next
;
1875 struct ext3_iloc iloc
;
1878 lock_super(inode
->i_sb
);
1879 if (list_empty(&ei
->i_orphan
)) {
1880 unlock_super(inode
->i_sb
);
1884 ino_next
= NEXT_ORPHAN(inode
);
1885 prev
= ei
->i_orphan
.prev
;
1886 sbi
= EXT3_SB(inode
->i_sb
);
1888 jbd_debug(4, "remove inode %lu from orphan list\n", inode
->i_ino
);
1890 list_del_init(&ei
->i_orphan
);
1892 /* If we're on an error path, we may not have a valid
1893 * transaction handle with which to update the orphan list on
1894 * disk, but we still need to remove the inode from the linked
1895 * list in memory. */
1899 err
= ext3_reserve_inode_write(handle
, inode
, &iloc
);
1903 if (prev
== &sbi
->s_orphan
) {
1904 jbd_debug(4, "superblock will point to %lu\n", ino_next
);
1905 BUFFER_TRACE(sbi
->s_sbh
, "get_write_access");
1906 err
= ext3_journal_get_write_access(handle
, sbi
->s_sbh
);
1909 sbi
->s_es
->s_last_orphan
= cpu_to_le32(ino_next
);
1910 err
= ext3_journal_dirty_metadata(handle
, sbi
->s_sbh
);
1912 struct ext3_iloc iloc2
;
1913 struct inode
*i_prev
=
1914 &list_entry(prev
, struct ext3_inode_info
, i_orphan
)->vfs_inode
;
1916 jbd_debug(4, "orphan inode %lu will point to %lu\n",
1917 i_prev
->i_ino
, ino_next
);
1918 err
= ext3_reserve_inode_write(handle
, i_prev
, &iloc2
);
1921 NEXT_ORPHAN(i_prev
) = ino_next
;
1922 err
= ext3_mark_iloc_dirty(handle
, i_prev
, &iloc2
);
1926 NEXT_ORPHAN(inode
) = 0;
1927 err
= ext3_mark_iloc_dirty(handle
, inode
, &iloc
);
1932 ext3_std_error(inode
->i_sb
, err
);
1934 unlock_super(inode
->i_sb
);
1942 static int ext3_rmdir (struct inode
* dir
, struct dentry
*dentry
)
1945 struct inode
* inode
;
1946 struct buffer_head
* bh
;
1947 struct ext3_dir_entry_2
* de
;
1951 handle
= ext3_journal_start(dir
, EXT3_DELETE_TRANS_BLOCKS
);
1952 if (IS_ERR(handle
)) {
1954 return PTR_ERR(handle
);
1958 bh
= ext3_find_entry (dentry
, &de
);
1962 if (IS_DIRSYNC(dir
))
1965 inode
= dentry
->d_inode
;
1969 if (le32_to_cpu(de
->inode
) != inode
->i_ino
)
1972 retval
= -ENOTEMPTY
;
1973 if (!empty_dir (inode
))
1976 retval
= ext3_delete_entry(handle
, dir
, de
, bh
);
1979 if (inode
->i_nlink
!= 2)
1980 ext3_warning (inode
->i_sb
, "ext3_rmdir",
1981 "empty directory has nlink!=2 (%d)",
1985 /* There's no need to set i_disksize: the fact that i_nlink is
1986 * zero will ensure that the right thing happens during any
1989 ext3_orphan_add(handle
, inode
);
1990 ext3_mark_inode_dirty(handle
, inode
);
1992 inode
->i_ctime
= dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
1993 ext3_update_dx_flag(dir
);
1994 ext3_mark_inode_dirty(handle
, dir
);
1997 ext3_journal_stop(handle
, dir
);
2003 static int ext3_unlink(struct inode
* dir
, struct dentry
*dentry
)
2006 struct inode
* inode
;
2007 struct buffer_head
* bh
;
2008 struct ext3_dir_entry_2
* de
;
2012 handle
= ext3_journal_start(dir
, EXT3_DELETE_TRANS_BLOCKS
);
2013 if (IS_ERR(handle
)) {
2015 return PTR_ERR(handle
);
2018 if (IS_DIRSYNC(dir
))
2022 bh
= ext3_find_entry (dentry
, &de
);
2026 inode
= dentry
->d_inode
;
2030 if (le32_to_cpu(de
->inode
) != inode
->i_ino
)
2033 if (!inode
->i_nlink
) {
2034 ext3_warning (inode
->i_sb
, "ext3_unlink",
2035 "Deleting nonexistent file (%lu), %d",
2036 inode
->i_ino
, inode
->i_nlink
);
2039 retval
= ext3_delete_entry(handle
, dir
, de
, bh
);
2042 dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
2043 ext3_update_dx_flag(dir
);
2044 ext3_mark_inode_dirty(handle
, dir
);
2046 if (!inode
->i_nlink
)
2047 ext3_orphan_add(handle
, inode
);
2048 ext3_mark_inode_dirty(handle
, inode
);
2049 inode
->i_ctime
= dir
->i_ctime
;
2053 ext3_journal_stop(handle
, dir
);
2059 static int ext3_symlink (struct inode
* dir
,
2060 struct dentry
*dentry
, const char * symname
)
2063 struct inode
* inode
;
2066 l
= strlen(symname
)+1;
2067 if (l
> dir
->i_sb
->s_blocksize
)
2068 return -ENAMETOOLONG
;
2071 handle
= ext3_journal_start(dir
, EXT3_DATA_TRANS_BLOCKS
+
2072 EXT3_INDEX_EXTRA_TRANS_BLOCKS
+ 5);
2073 if (IS_ERR(handle
)) {
2075 return PTR_ERR(handle
);
2078 if (IS_DIRSYNC(dir
))
2081 inode
= ext3_new_inode (handle
, dir
, S_IFLNK
|S_IRWXUGO
);
2082 err
= PTR_ERR(inode
);
2086 if (l
> sizeof (EXT3_I(inode
)->i_data
)) {
2087 inode
->i_op
= &ext3_symlink_inode_operations
;
2088 if (ext3_should_writeback_data(inode
))
2089 inode
->i_mapping
->a_ops
= &ext3_writeback_aops
;
2091 inode
->i_mapping
->a_ops
= &ext3_aops
;
2093 * page_symlink() calls into ext3_prepare/commit_write.
2094 * We have a transaction open. All is sweetness. It also sets
2095 * i_size in generic_commit_write().
2097 err
= page_symlink(inode
, symname
, l
);
2099 ext3_dec_count(handle
, inode
);
2100 ext3_mark_inode_dirty(handle
, inode
);
2105 inode
->i_op
= &ext3_fast_symlink_inode_operations
;
2106 memcpy((char*)&EXT3_I(inode
)->i_data
,symname
,l
);
2107 inode
->i_size
= l
-1;
2109 EXT3_I(inode
)->i_disksize
= inode
->i_size
;
2110 err
= ext3_add_nondir(handle
, dentry
, inode
);
2112 ext3_journal_stop(handle
, dir
);
2117 static int ext3_link (struct dentry
* old_dentry
,
2118 struct inode
* dir
, struct dentry
*dentry
)
2121 struct inode
*inode
= old_dentry
->d_inode
;
2125 if (inode
->i_nlink
>= EXT3_LINK_MAX
) {
2130 handle
= ext3_journal_start(dir
, EXT3_DATA_TRANS_BLOCKS
+
2131 EXT3_INDEX_EXTRA_TRANS_BLOCKS
);
2132 if (IS_ERR(handle
)) {
2134 return PTR_ERR(handle
);
2137 if (IS_DIRSYNC(dir
))
2140 inode
->i_ctime
= CURRENT_TIME
;
2141 ext3_inc_count(handle
, inode
);
2142 atomic_inc(&inode
->i_count
);
2144 err
= ext3_add_nondir(handle
, dentry
, inode
);
2145 ext3_journal_stop(handle
, dir
);
2150 #define PARENT_INO(buffer) \
2151 ((struct ext3_dir_entry_2 *) ((char *) buffer + \
2152 le16_to_cpu(((struct ext3_dir_entry_2 *) buffer)->rec_len)))->inode
2155 * Anybody can rename anything with this: the permission checks are left to the
2156 * higher-level routines.
2158 static int ext3_rename (struct inode
* old_dir
, struct dentry
*old_dentry
,
2159 struct inode
* new_dir
,struct dentry
*new_dentry
)
2162 struct inode
* old_inode
, * new_inode
;
2163 struct buffer_head
* old_bh
, * new_bh
, * dir_bh
;
2164 struct ext3_dir_entry_2
* old_de
, * new_de
;
2167 old_bh
= new_bh
= dir_bh
= NULL
;
2170 handle
= ext3_journal_start(old_dir
, 2 * EXT3_DATA_TRANS_BLOCKS
+
2171 EXT3_INDEX_EXTRA_TRANS_BLOCKS
+ 2);
2172 if (IS_ERR(handle
)) {
2174 return PTR_ERR(handle
);
2177 if (IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
))
2180 old_bh
= ext3_find_entry (old_dentry
, &old_de
);
2182 * Check for inode number is _not_ due to possible IO errors.
2183 * We might rmdir the source, keep it as pwd of some process
2184 * and merrily kill the link to whatever was created under the
2185 * same name. Goodbye sticky bit ;-<
2187 old_inode
= old_dentry
->d_inode
;
2189 if (!old_bh
|| le32_to_cpu(old_de
->inode
) != old_inode
->i_ino
)
2192 new_inode
= new_dentry
->d_inode
;
2193 new_bh
= ext3_find_entry (new_dentry
, &new_de
);
2199 DQUOT_INIT(new_inode
);
2202 if (S_ISDIR(old_inode
->i_mode
)) {
2204 retval
= -ENOTEMPTY
;
2205 if (!empty_dir (new_inode
))
2209 dir_bh
= ext3_bread (handle
, old_inode
, 0, 0, &retval
);
2212 if (le32_to_cpu(PARENT_INO(dir_bh
->b_data
)) != old_dir
->i_ino
)
2215 if (!new_inode
&& new_dir
!=old_dir
&&
2216 new_dir
->i_nlink
>= EXT3_LINK_MAX
)
2220 retval
= ext3_add_entry (handle
, new_dentry
, old_inode
);
2224 BUFFER_TRACE(new_bh
, "get write access");
2225 BUFFER_TRACE(new_bh
, "get_write_access");
2226 ext3_journal_get_write_access(handle
, new_bh
);
2227 new_de
->inode
= le32_to_cpu(old_inode
->i_ino
);
2228 if (EXT3_HAS_INCOMPAT_FEATURE(new_dir
->i_sb
,
2229 EXT3_FEATURE_INCOMPAT_FILETYPE
))
2230 new_de
->file_type
= old_de
->file_type
;
2231 new_dir
->i_version
++;
2232 BUFFER_TRACE(new_bh
, "call ext3_journal_dirty_metadata");
2233 ext3_journal_dirty_metadata(handle
, new_bh
);
2239 * Like most other Unix systems, set the ctime for inodes on a
2242 old_inode
->i_ctime
= CURRENT_TIME
;
2243 ext3_mark_inode_dirty(handle
, old_inode
);
2248 retval
= ext3_delete_entry(handle
, old_dir
, old_de
, old_bh
);
2249 if (retval
== -ENOENT
) {
2251 * old_de could have moved out from under us.
2253 struct buffer_head
*old_bh2
;
2254 struct ext3_dir_entry_2
*old_de2
;
2256 old_bh2
= ext3_find_entry(old_dentry
, &old_de2
);
2258 retval
= ext3_delete_entry(handle
, old_dir
,
2264 ext3_warning(old_dir
->i_sb
, "ext3_rename",
2265 "Deleting old file (%lu), %d, error=%d",
2266 old_dir
->i_ino
, old_dir
->i_nlink
, retval
);
2270 new_inode
->i_nlink
--;
2271 new_inode
->i_ctime
= CURRENT_TIME
;
2273 old_dir
->i_ctime
= old_dir
->i_mtime
= CURRENT_TIME
;
2274 ext3_update_dx_flag(old_dir
);
2276 BUFFER_TRACE(dir_bh
, "get_write_access");
2277 ext3_journal_get_write_access(handle
, dir_bh
);
2278 PARENT_INO(dir_bh
->b_data
) = le32_to_cpu(new_dir
->i_ino
);
2279 BUFFER_TRACE(dir_bh
, "call ext3_journal_dirty_metadata");
2280 ext3_journal_dirty_metadata(handle
, dir_bh
);
2283 new_inode
->i_nlink
--;
2286 ext3_update_dx_flag(new_dir
);
2287 ext3_mark_inode_dirty(handle
, new_dir
);
2290 ext3_mark_inode_dirty(handle
, old_dir
);
2292 ext3_mark_inode_dirty(handle
, new_inode
);
2293 if (!new_inode
->i_nlink
)
2294 ext3_orphan_add(handle
, new_inode
);
2302 ext3_journal_stop(handle
, old_dir
);
2308 * directories can handle most operations...
2310 struct inode_operations ext3_dir_inode_operations
= {
2311 .create
= ext3_create
,
2312 .lookup
= ext3_lookup
,
2314 .unlink
= ext3_unlink
,
2315 .symlink
= ext3_symlink
,
2316 .mkdir
= ext3_mkdir
,
2317 .rmdir
= ext3_rmdir
,
2318 .mknod
= ext3_mknod
,
2319 .rename
= ext3_rename
,
2320 .setattr
= ext3_setattr
,
2321 .setxattr
= ext3_setxattr
,
2322 .getxattr
= ext3_getxattr
,
2323 .listxattr
= ext3_listxattr
,
2324 .removexattr
= ext3_removexattr
,
2325 .permission
= ext3_permission
,
2328 struct inode_operations ext3_special_inode_operations
= {
2329 .setattr
= ext3_setattr
,
2330 .setxattr
= ext3_setxattr
,
2331 .getxattr
= ext3_getxattr
,
2332 .listxattr
= ext3_listxattr
,
2333 .removexattr
= ext3_removexattr
,
2334 .permission
= ext3_permission
,