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 if ((bh
= ext3_dir_bread(handle
, inode
, *block
, 1, err
))) {
50 inode
->i_size
+= inode
->i_sb
->s_blocksize
;
51 EXT3_I(inode
)->i_disksize
= inode
->i_size
;
52 *err
= ext3_journal_get_write_access(handle
, bh
);
62 #define assert(test) J_ASSERT(test)
66 #define dxtrace(command) command
68 #define dxtrace(command)
92 * dx_root_info is laid out so that if it should somehow get overlaid by a
93 * dirent the two low bits of the hash version will be zero. Therefore, the
94 * hash version mod 4 should never be 0. Sincerely, the paranoia department.
99 struct fake_dirent dot
;
101 struct fake_dirent dotdot
;
105 __le32 reserved_zero
;
107 u8 info_length
; /* 8 */
112 struct dx_entry entries
[0];
117 struct fake_dirent fake
;
118 struct dx_entry entries
[0];
124 struct buffer_head
*bh
;
125 struct dx_entry
*entries
;
136 static inline unsigned dx_get_block (struct dx_entry
*entry
);
137 static void dx_set_block (struct dx_entry
*entry
, unsigned value
);
138 static inline unsigned dx_get_hash (struct dx_entry
*entry
);
139 static void dx_set_hash (struct dx_entry
*entry
, unsigned value
);
140 static unsigned dx_get_count (struct dx_entry
*entries
);
141 static unsigned dx_get_limit (struct dx_entry
*entries
);
142 static void dx_set_count (struct dx_entry
*entries
, unsigned value
);
143 static void dx_set_limit (struct dx_entry
*entries
, unsigned value
);
144 static unsigned dx_root_limit (struct inode
*dir
, unsigned infosize
);
145 static unsigned dx_node_limit (struct inode
*dir
);
146 static struct dx_frame
*dx_probe(struct qstr
*entry
,
148 struct dx_hash_info
*hinfo
,
149 struct dx_frame
*frame
,
151 static void dx_release (struct dx_frame
*frames
);
152 static int dx_make_map(struct ext3_dir_entry_2
*de
, unsigned blocksize
,
153 struct dx_hash_info
*hinfo
, struct dx_map_entry map
[]);
154 static void dx_sort_map(struct dx_map_entry
*map
, unsigned count
);
155 static struct ext3_dir_entry_2
*dx_move_dirents (char *from
, char *to
,
156 struct dx_map_entry
*offsets
, int count
);
157 static struct ext3_dir_entry_2
*dx_pack_dirents(char *base
, unsigned blocksize
);
158 static void dx_insert_block (struct dx_frame
*frame
, u32 hash
, u32 block
);
159 static int ext3_htree_next_block(struct inode
*dir
, __u32 hash
,
160 struct dx_frame
*frame
,
161 struct dx_frame
*frames
,
163 static struct buffer_head
* ext3_dx_find_entry(struct inode
*dir
,
164 struct qstr
*entry
, struct ext3_dir_entry_2
**res_dir
,
166 static int ext3_dx_add_entry(handle_t
*handle
, struct dentry
*dentry
,
167 struct inode
*inode
);
170 * p is at least 6 bytes before the end of page
172 static inline struct ext3_dir_entry_2
*
173 ext3_next_entry(struct ext3_dir_entry_2
*p
)
175 return (struct ext3_dir_entry_2
*)((char *)p
+
176 ext3_rec_len_from_disk(p
->rec_len
));
180 * Future: use high four bits of block for coalesce-on-delete flags
181 * Mask them off for now.
184 static inline unsigned dx_get_block (struct dx_entry
*entry
)
186 return le32_to_cpu(entry
->block
) & 0x00ffffff;
189 static inline void dx_set_block (struct dx_entry
*entry
, unsigned value
)
191 entry
->block
= cpu_to_le32(value
);
194 static inline unsigned dx_get_hash (struct dx_entry
*entry
)
196 return le32_to_cpu(entry
->hash
);
199 static inline void dx_set_hash (struct dx_entry
*entry
, unsigned value
)
201 entry
->hash
= cpu_to_le32(value
);
204 static inline unsigned dx_get_count (struct dx_entry
*entries
)
206 return le16_to_cpu(((struct dx_countlimit
*) entries
)->count
);
209 static inline unsigned dx_get_limit (struct dx_entry
*entries
)
211 return le16_to_cpu(((struct dx_countlimit
*) entries
)->limit
);
214 static inline void dx_set_count (struct dx_entry
*entries
, unsigned value
)
216 ((struct dx_countlimit
*) entries
)->count
= cpu_to_le16(value
);
219 static inline void dx_set_limit (struct dx_entry
*entries
, unsigned value
)
221 ((struct dx_countlimit
*) entries
)->limit
= cpu_to_le16(value
);
224 static inline unsigned dx_root_limit (struct inode
*dir
, unsigned infosize
)
226 unsigned entry_space
= dir
->i_sb
->s_blocksize
- EXT3_DIR_REC_LEN(1) -
227 EXT3_DIR_REC_LEN(2) - infosize
;
228 return entry_space
/ sizeof(struct dx_entry
);
231 static inline unsigned dx_node_limit (struct inode
*dir
)
233 unsigned entry_space
= dir
->i_sb
->s_blocksize
- EXT3_DIR_REC_LEN(0);
234 return entry_space
/ sizeof(struct dx_entry
);
241 static void dx_show_index (char * label
, struct dx_entry
*entries
)
243 int i
, n
= dx_get_count (entries
);
244 printk("%s index ", label
);
245 for (i
= 0; i
< n
; i
++)
247 printk("%x->%u ", i
? dx_get_hash(entries
+ i
): 0, dx_get_block(entries
+ i
));
259 static struct stats
dx_show_leaf(struct dx_hash_info
*hinfo
, struct ext3_dir_entry_2
*de
,
260 int size
, int show_names
)
262 unsigned names
= 0, space
= 0;
263 char *base
= (char *) de
;
264 struct dx_hash_info h
= *hinfo
;
267 while ((char *) de
< base
+ size
)
273 int len
= de
->name_len
;
274 char *name
= de
->name
;
275 while (len
--) printk("%c", *name
++);
276 ext3fs_dirhash(de
->name
, de
->name_len
, &h
);
277 printk(":%x.%u ", h
.hash
,
278 (unsigned) ((char *) de
- base
));
280 space
+= EXT3_DIR_REC_LEN(de
->name_len
);
283 de
= ext3_next_entry(de
);
285 printk("(%i)\n", names
);
286 return (struct stats
) { names
, space
, 1 };
289 struct stats
dx_show_entries(struct dx_hash_info
*hinfo
, struct inode
*dir
,
290 struct dx_entry
*entries
, int levels
)
292 unsigned blocksize
= dir
->i_sb
->s_blocksize
;
293 unsigned count
= dx_get_count (entries
), names
= 0, space
= 0, i
;
295 struct buffer_head
*bh
;
297 printk("%i indexed blocks...\n", count
);
298 for (i
= 0; i
< count
; i
++, entries
++)
300 u32 block
= dx_get_block(entries
), hash
= i
? dx_get_hash(entries
): 0;
301 u32 range
= i
< count
- 1? (dx_get_hash(entries
+ 1) - hash
): ~hash
;
303 printk("%s%3u:%03u hash %8x/%8x ",levels
?"":" ", i
, block
, hash
, range
);
304 if (!(bh
= ext3_bread (NULL
,dir
, block
, 0,&err
))) continue;
306 dx_show_entries(hinfo
, dir
, ((struct dx_node
*) bh
->b_data
)->entries
, levels
- 1):
307 dx_show_leaf(hinfo
, (struct ext3_dir_entry_2
*) bh
->b_data
, blocksize
, 0);
308 names
+= stats
.names
;
309 space
+= stats
.space
;
310 bcount
+= stats
.bcount
;
314 printk("%snames %u, fullness %u (%u%%)\n", levels
?"":" ",
315 names
, space
/bcount
,(space
/bcount
)*100/blocksize
);
316 return (struct stats
) { names
, space
, bcount
};
318 #endif /* DX_DEBUG */
321 * Probe for a directory leaf block to search.
323 * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
324 * error in the directory index, and the caller should fall back to
325 * searching the directory normally. The callers of dx_probe **MUST**
326 * check for this error code, and make sure it never gets reflected
329 static struct dx_frame
*
330 dx_probe(struct qstr
*entry
, struct inode
*dir
,
331 struct dx_hash_info
*hinfo
, struct dx_frame
*frame_in
, int *err
)
333 unsigned count
, indirect
;
334 struct dx_entry
*at
, *entries
, *p
, *q
, *m
;
335 struct dx_root
*root
;
336 struct buffer_head
*bh
;
337 struct dx_frame
*frame
= frame_in
;
341 if (!(bh
= ext3_dir_bread(NULL
, dir
, 0, 0, err
))) {
342 *err
= ERR_BAD_DX_DIR
;
345 root
= (struct dx_root
*) bh
->b_data
;
346 if (root
->info
.hash_version
!= DX_HASH_TEA
&&
347 root
->info
.hash_version
!= DX_HASH_HALF_MD4
&&
348 root
->info
.hash_version
!= DX_HASH_LEGACY
) {
349 ext3_warning(dir
->i_sb
, __func__
,
350 "Unrecognised inode hash code %d",
351 root
->info
.hash_version
);
353 *err
= ERR_BAD_DX_DIR
;
356 hinfo
->hash_version
= root
->info
.hash_version
;
357 if (hinfo
->hash_version
<= DX_HASH_TEA
)
358 hinfo
->hash_version
+= EXT3_SB(dir
->i_sb
)->s_hash_unsigned
;
359 hinfo
->seed
= EXT3_SB(dir
->i_sb
)->s_hash_seed
;
361 ext3fs_dirhash(entry
->name
, entry
->len
, hinfo
);
364 if (root
->info
.unused_flags
& 1) {
365 ext3_warning(dir
->i_sb
, __func__
,
366 "Unimplemented inode hash flags: %#06x",
367 root
->info
.unused_flags
);
369 *err
= ERR_BAD_DX_DIR
;
373 if ((indirect
= root
->info
.indirect_levels
) > 1) {
374 ext3_warning(dir
->i_sb
, __func__
,
375 "Unimplemented inode hash depth: %#06x",
376 root
->info
.indirect_levels
);
378 *err
= ERR_BAD_DX_DIR
;
382 entries
= (struct dx_entry
*) (((char *)&root
->info
) +
383 root
->info
.info_length
);
385 if (dx_get_limit(entries
) != dx_root_limit(dir
,
386 root
->info
.info_length
)) {
387 ext3_warning(dir
->i_sb
, __func__
,
388 "dx entry: limit != root limit");
390 *err
= ERR_BAD_DX_DIR
;
394 dxtrace (printk("Look up %x", hash
));
397 count
= dx_get_count(entries
);
398 if (!count
|| count
> dx_get_limit(entries
)) {
399 ext3_warning(dir
->i_sb
, __func__
,
400 "dx entry: no count or count > limit");
402 *err
= ERR_BAD_DX_DIR
;
407 q
= entries
+ count
- 1;
411 dxtrace(printk("."));
412 if (dx_get_hash(m
) > hash
)
418 if (0) // linear search cross check
420 unsigned n
= count
- 1;
424 dxtrace(printk(","));
425 if (dx_get_hash(++at
) > hash
)
431 assert (at
== p
- 1);
435 dxtrace(printk(" %x->%u\n", at
== entries
? 0: dx_get_hash(at
), dx_get_block(at
)));
437 frame
->entries
= entries
;
439 if (!indirect
--) return frame
;
440 if (!(bh
= ext3_dir_bread(NULL
, dir
, dx_get_block(at
), 0, err
))) {
441 *err
= ERR_BAD_DX_DIR
;
444 at
= entries
= ((struct dx_node
*) bh
->b_data
)->entries
;
445 if (dx_get_limit(entries
) != dx_node_limit (dir
)) {
446 ext3_warning(dir
->i_sb
, __func__
,
447 "dx entry: limit != node limit");
449 *err
= ERR_BAD_DX_DIR
;
456 while (frame
>= frame_in
) {
461 if (*err
== ERR_BAD_DX_DIR
)
462 ext3_warning(dir
->i_sb
, __func__
,
463 "Corrupt dir inode %ld, running e2fsck is "
464 "recommended.", dir
->i_ino
);
468 static void dx_release (struct dx_frame
*frames
)
470 if (frames
[0].bh
== NULL
)
473 if (((struct dx_root
*) frames
[0].bh
->b_data
)->info
.indirect_levels
)
474 brelse(frames
[1].bh
);
475 brelse(frames
[0].bh
);
479 * This function increments the frame pointer to search the next leaf
480 * block, and reads in the necessary intervening nodes if the search
481 * should be necessary. Whether or not the search is necessary is
482 * controlled by the hash parameter. If the hash value is even, then
483 * the search is only continued if the next block starts with that
484 * hash value. This is used if we are searching for a specific file.
486 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
488 * This function returns 1 if the caller should continue to search,
489 * or 0 if it should not. If there is an error reading one of the
490 * index blocks, it will a negative error code.
492 * If start_hash is non-null, it will be filled in with the starting
493 * hash of the next page.
495 static int ext3_htree_next_block(struct inode
*dir
, __u32 hash
,
496 struct dx_frame
*frame
,
497 struct dx_frame
*frames
,
501 struct buffer_head
*bh
;
502 int err
, num_frames
= 0;
507 * Find the next leaf page by incrementing the frame pointer.
508 * If we run out of entries in the interior node, loop around and
509 * increment pointer in the parent node. When we break out of
510 * this loop, num_frames indicates the number of interior
511 * nodes need to be read.
514 if (++(p
->at
) < p
->entries
+ dx_get_count(p
->entries
))
523 * If the hash is 1, then continue only if the next page has a
524 * continuation hash of any value. This is used for readdir
525 * handling. Otherwise, check to see if the hash matches the
526 * desired contiuation hash. If it doesn't, return since
527 * there's no point to read in the successive index pages.
529 bhash
= dx_get_hash(p
->at
);
532 if ((hash
& 1) == 0) {
533 if ((bhash
& ~1) != hash
)
537 * If the hash is HASH_NB_ALWAYS, we always go to the next
538 * block so no check is necessary
540 while (num_frames
--) {
541 if (!(bh
= ext3_dir_bread(NULL
, dir
, dx_get_block(p
->at
),
543 return err
; /* Failure */
547 p
->at
= p
->entries
= ((struct dx_node
*) bh
->b_data
)->entries
;
554 * This function fills a red-black tree with information from a
555 * directory block. It returns the number directory entries loaded
556 * into the tree. If there is an error it is returned in err.
558 static int htree_dirblock_to_tree(struct file
*dir_file
,
559 struct inode
*dir
, int block
,
560 struct dx_hash_info
*hinfo
,
561 __u32 start_hash
, __u32 start_minor_hash
)
563 struct buffer_head
*bh
;
564 struct ext3_dir_entry_2
*de
, *top
;
565 int err
= 0, count
= 0;
567 dxtrace(printk("In htree dirblock_to_tree: block %d\n", block
));
569 if (!(bh
= ext3_dir_bread(NULL
, dir
, block
, 0, &err
)))
572 de
= (struct ext3_dir_entry_2
*) bh
->b_data
;
573 top
= (struct ext3_dir_entry_2
*) ((char *) de
+
574 dir
->i_sb
->s_blocksize
-
575 EXT3_DIR_REC_LEN(0));
576 for (; de
< top
; de
= ext3_next_entry(de
)) {
577 if (!ext3_check_dir_entry("htree_dirblock_to_tree", dir
, de
, bh
,
578 (block
<<EXT3_BLOCK_SIZE_BITS(dir
->i_sb
))
579 +((char *)de
- bh
->b_data
))) {
580 /* On error, skip the f_pos to the next block. */
581 dir_file
->f_pos
= (dir_file
->f_pos
|
582 (dir
->i_sb
->s_blocksize
- 1)) + 1;
586 ext3fs_dirhash(de
->name
, de
->name_len
, hinfo
);
587 if ((hinfo
->hash
< start_hash
) ||
588 ((hinfo
->hash
== start_hash
) &&
589 (hinfo
->minor_hash
< start_minor_hash
)))
593 if ((err
= ext3_htree_store_dirent(dir_file
,
594 hinfo
->hash
, hinfo
->minor_hash
, de
)) != 0) {
606 * This function fills a red-black tree with information from a
607 * directory. We start scanning the directory in hash order, starting
608 * at start_hash and start_minor_hash.
610 * This function returns the number of entries inserted into the tree,
611 * or a negative error code.
613 int ext3_htree_fill_tree(struct file
*dir_file
, __u32 start_hash
,
614 __u32 start_minor_hash
, __u32
*next_hash
)
616 struct dx_hash_info hinfo
;
617 struct ext3_dir_entry_2
*de
;
618 struct dx_frame frames
[2], *frame
;
625 dxtrace(printk("In htree_fill_tree, start hash: %x:%x\n", start_hash
,
627 dir
= file_inode(dir_file
);
628 if (!(EXT3_I(dir
)->i_flags
& EXT3_INDEX_FL
)) {
629 hinfo
.hash_version
= EXT3_SB(dir
->i_sb
)->s_def_hash_version
;
630 if (hinfo
.hash_version
<= DX_HASH_TEA
)
631 hinfo
.hash_version
+=
632 EXT3_SB(dir
->i_sb
)->s_hash_unsigned
;
633 hinfo
.seed
= EXT3_SB(dir
->i_sb
)->s_hash_seed
;
634 count
= htree_dirblock_to_tree(dir_file
, dir
, 0, &hinfo
,
635 start_hash
, start_minor_hash
);
639 hinfo
.hash
= start_hash
;
640 hinfo
.minor_hash
= 0;
641 frame
= dx_probe(NULL
, file_inode(dir_file
), &hinfo
, frames
, &err
);
645 /* Add '.' and '..' from the htree header */
646 if (!start_hash
&& !start_minor_hash
) {
647 de
= (struct ext3_dir_entry_2
*) frames
[0].bh
->b_data
;
648 if ((err
= ext3_htree_store_dirent(dir_file
, 0, 0, de
)) != 0)
652 if (start_hash
< 2 || (start_hash
==2 && start_minor_hash
==0)) {
653 de
= (struct ext3_dir_entry_2
*) frames
[0].bh
->b_data
;
654 de
= ext3_next_entry(de
);
655 if ((err
= ext3_htree_store_dirent(dir_file
, 2, 0, de
)) != 0)
661 block
= dx_get_block(frame
->at
);
662 ret
= htree_dirblock_to_tree(dir_file
, dir
, block
, &hinfo
,
663 start_hash
, start_minor_hash
);
670 ret
= ext3_htree_next_block(dir
, HASH_NB_ALWAYS
,
671 frame
, frames
, &hashval
);
672 *next_hash
= hashval
;
678 * Stop if: (a) there are no more entries, or
679 * (b) we have inserted at least one entry and the
680 * next hash value is not a continuation
683 (count
&& ((hashval
& 1) == 0)))
687 dxtrace(printk("Fill tree: returned %d entries, next hash: %x\n",
697 * Directory block splitting, compacting
701 * Create map of hash values, offsets, and sizes, stored at end of block.
702 * Returns number of entries mapped.
704 static int dx_make_map(struct ext3_dir_entry_2
*de
, unsigned blocksize
,
705 struct dx_hash_info
*hinfo
, struct dx_map_entry
*map_tail
)
708 char *base
= (char *) de
;
709 struct dx_hash_info h
= *hinfo
;
711 while ((char *) de
< base
+ blocksize
)
713 if (de
->name_len
&& de
->inode
) {
714 ext3fs_dirhash(de
->name
, de
->name_len
, &h
);
716 map_tail
->hash
= h
.hash
;
717 map_tail
->offs
= (u16
) ((char *) de
- base
);
718 map_tail
->size
= le16_to_cpu(de
->rec_len
);
722 /* XXX: do we need to check rec_len == 0 case? -Chris */
723 de
= ext3_next_entry(de
);
728 /* Sort map by hash value */
729 static void dx_sort_map (struct dx_map_entry
*map
, unsigned count
)
731 struct dx_map_entry
*p
, *q
, *top
= map
+ count
- 1;
733 /* Combsort until bubble sort doesn't suck */
737 if (count
- 9 < 2) /* 9, 10 -> 11 */
739 for (p
= top
, q
= p
- count
; q
>= map
; p
--, q
--)
740 if (p
->hash
< q
->hash
)
743 /* Garden variety bubble sort */
749 if (q
[1].hash
>= q
[0].hash
)
757 static void dx_insert_block(struct dx_frame
*frame
, u32 hash
, u32 block
)
759 struct dx_entry
*entries
= frame
->entries
;
760 struct dx_entry
*old
= frame
->at
, *new = old
+ 1;
761 int count
= dx_get_count(entries
);
763 assert(count
< dx_get_limit(entries
));
764 assert(old
< entries
+ count
);
765 memmove(new + 1, new, (char *)(entries
+ count
) - (char *)(new));
766 dx_set_hash(new, hash
);
767 dx_set_block(new, block
);
768 dx_set_count(entries
, count
+ 1);
771 static void ext3_update_dx_flag(struct inode
*inode
)
773 if (!EXT3_HAS_COMPAT_FEATURE(inode
->i_sb
,
774 EXT3_FEATURE_COMPAT_DIR_INDEX
))
775 EXT3_I(inode
)->i_flags
&= ~EXT3_INDEX_FL
;
779 * NOTE! unlike strncmp, ext3_match returns 1 for success, 0 for failure.
781 * `len <= EXT3_NAME_LEN' is guaranteed by caller.
782 * `de != NULL' is guaranteed by caller.
784 static inline int ext3_match (int len
, const char * const name
,
785 struct ext3_dir_entry_2
* de
)
787 if (len
!= de
->name_len
)
791 return !memcmp(name
, de
->name
, len
);
795 * Returns 0 if not found, -1 on failure, and 1 on success
797 static inline int search_dirblock(struct buffer_head
* bh
,
800 unsigned long offset
,
801 struct ext3_dir_entry_2
** res_dir
)
803 struct ext3_dir_entry_2
* de
;
806 const char *name
= child
->name
;
807 int namelen
= child
->len
;
809 de
= (struct ext3_dir_entry_2
*) bh
->b_data
;
810 dlimit
= bh
->b_data
+ dir
->i_sb
->s_blocksize
;
811 while ((char *) de
< dlimit
) {
812 /* this code is executed quadratically often */
813 /* do minimal checking `by hand' */
815 if ((char *) de
+ namelen
<= dlimit
&&
816 ext3_match (namelen
, name
, de
)) {
817 /* found a match - just to be sure, do a full check */
818 if (!ext3_check_dir_entry("ext3_find_entry",
819 dir
, de
, bh
, offset
))
824 /* prevent looping on a bad block */
825 de_len
= ext3_rec_len_from_disk(de
->rec_len
);
829 de
= (struct ext3_dir_entry_2
*) ((char *) de
+ de_len
);
838 * finds an entry in the specified directory with the wanted name. It
839 * returns the cache buffer in which the entry was found, and the entry
840 * itself (as a parameter - res_dir). It does NOT read the inode of the
841 * entry - you'll have to do that yourself if you want to.
843 * The returned buffer_head has ->b_count elevated. The caller is expected
844 * to brelse() it when appropriate.
846 static struct buffer_head
*ext3_find_entry(struct inode
*dir
,
848 struct ext3_dir_entry_2
**res_dir
)
850 struct super_block
* sb
;
851 struct buffer_head
* bh_use
[NAMEI_RA_SIZE
];
852 struct buffer_head
* bh
, *ret
= NULL
;
853 unsigned long start
, block
, b
;
854 const u8
*name
= entry
->name
;
855 int ra_max
= 0; /* Number of bh's in the readahead
857 int ra_ptr
= 0; /* Current index into readahead
865 namelen
= entry
->len
;
866 if (namelen
> EXT3_NAME_LEN
)
868 if ((namelen
<= 2) && (name
[0] == '.') &&
869 (name
[1] == '.' || name
[1] == 0)) {
871 * "." or ".." will only be in the first block
872 * NFS may look up ".."; "." should be handled by the VFS
879 bh
= ext3_dx_find_entry(dir
, entry
, res_dir
, &err
);
881 * On success, or if the error was file not found,
882 * return. Otherwise, fall back to doing a search the
885 if (bh
|| (err
!= ERR_BAD_DX_DIR
))
887 dxtrace(printk("ext3_find_entry: dx failed, falling back\n"));
889 nblocks
= dir
->i_size
>> EXT3_BLOCK_SIZE_BITS(sb
);
890 start
= EXT3_I(dir
)->i_dir_start_lookup
;
891 if (start
>= nblocks
)
897 * We deal with the read-ahead logic here.
899 if (ra_ptr
>= ra_max
) {
900 /* Refill the readahead buffer */
903 for (ra_max
= 0; ra_max
< NAMEI_RA_SIZE
; ra_max
++) {
905 * Terminate if we reach the end of the
906 * directory and must wrap, or if our
907 * search has finished at this block.
909 if (b
>= nblocks
|| (num
&& block
== start
)) {
910 bh_use
[ra_max
] = NULL
;
914 bh
= ext3_getblk(NULL
, dir
, b
++, 0, &err
);
916 if (bh
&& !bh_uptodate_or_lock(bh
)) {
918 bh
->b_end_io
= end_buffer_read_sync
;
919 submit_bh(READ
| REQ_META
| REQ_PRIO
,
924 if ((bh
= bh_use
[ra_ptr
++]) == NULL
)
927 if (!buffer_uptodate(bh
)) {
928 /* read error, skip block & hope for the best */
929 ext3_error(sb
, __func__
, "reading directory #%lu "
930 "offset %lu", dir
->i_ino
, block
);
934 i
= search_dirblock(bh
, dir
, entry
,
935 block
<< EXT3_BLOCK_SIZE_BITS(sb
), res_dir
);
937 EXT3_I(dir
)->i_dir_start_lookup
= block
;
939 goto cleanup_and_exit
;
943 goto cleanup_and_exit
;
946 if (++block
>= nblocks
)
948 } while (block
!= start
);
951 * If the directory has grown while we were searching, then
952 * search the last part of the directory before giving up.
955 nblocks
= dir
->i_size
>> EXT3_BLOCK_SIZE_BITS(sb
);
956 if (block
< nblocks
) {
962 /* Clean up the read-ahead blocks */
963 for (; ra_ptr
< ra_max
; ra_ptr
++)
964 brelse (bh_use
[ra_ptr
]);
968 static struct buffer_head
* ext3_dx_find_entry(struct inode
*dir
,
969 struct qstr
*entry
, struct ext3_dir_entry_2
**res_dir
,
972 struct super_block
*sb
= dir
->i_sb
;
973 struct dx_hash_info hinfo
;
974 struct dx_frame frames
[2], *frame
;
975 struct buffer_head
*bh
;
979 if (!(frame
= dx_probe(entry
, dir
, &hinfo
, frames
, err
)))
982 block
= dx_get_block(frame
->at
);
983 if (!(bh
= ext3_dir_bread (NULL
, dir
, block
, 0, err
)))
986 retval
= search_dirblock(bh
, dir
, entry
,
987 block
<< EXT3_BLOCK_SIZE_BITS(sb
),
995 *err
= ERR_BAD_DX_DIR
;
999 /* Check to see if we should continue to search */
1000 retval
= ext3_htree_next_block(dir
, hinfo
.hash
, frame
,
1003 ext3_warning(sb
, __func__
,
1004 "error reading index page in directory #%lu",
1009 } while (retval
== 1);
1013 dxtrace(printk("%s not found\n", entry
->name
));
1014 dx_release (frames
);
1018 static struct dentry
*ext3_lookup(struct inode
* dir
, struct dentry
*dentry
, unsigned int flags
)
1020 struct inode
* inode
;
1021 struct ext3_dir_entry_2
* de
;
1022 struct buffer_head
* bh
;
1024 if (dentry
->d_name
.len
> EXT3_NAME_LEN
)
1025 return ERR_PTR(-ENAMETOOLONG
);
1027 bh
= ext3_find_entry(dir
, &dentry
->d_name
, &de
);
1030 unsigned long ino
= le32_to_cpu(de
->inode
);
1032 if (!ext3_valid_inum(dir
->i_sb
, ino
)) {
1033 ext3_error(dir
->i_sb
, "ext3_lookup",
1034 "bad inode number: %lu", ino
);
1035 return ERR_PTR(-EIO
);
1037 inode
= ext3_iget(dir
->i_sb
, ino
);
1038 if (inode
== ERR_PTR(-ESTALE
)) {
1039 ext3_error(dir
->i_sb
, __func__
,
1040 "deleted inode referenced: %lu",
1042 return ERR_PTR(-EIO
);
1045 return d_splice_alias(inode
, dentry
);
1049 struct dentry
*ext3_get_parent(struct dentry
*child
)
1052 struct qstr dotdot
= QSTR_INIT("..", 2);
1053 struct ext3_dir_entry_2
* de
;
1054 struct buffer_head
*bh
;
1056 bh
= ext3_find_entry(child
->d_inode
, &dotdot
, &de
);
1058 return ERR_PTR(-ENOENT
);
1059 ino
= le32_to_cpu(de
->inode
);
1062 if (!ext3_valid_inum(child
->d_inode
->i_sb
, ino
)) {
1063 ext3_error(child
->d_inode
->i_sb
, "ext3_get_parent",
1064 "bad inode number: %lu", ino
);
1065 return ERR_PTR(-EIO
);
1068 return d_obtain_alias(ext3_iget(child
->d_inode
->i_sb
, ino
));
1072 static unsigned char ext3_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
1073 [S_IFREG
>> S_SHIFT
] = EXT3_FT_REG_FILE
,
1074 [S_IFDIR
>> S_SHIFT
] = EXT3_FT_DIR
,
1075 [S_IFCHR
>> S_SHIFT
] = EXT3_FT_CHRDEV
,
1076 [S_IFBLK
>> S_SHIFT
] = EXT3_FT_BLKDEV
,
1077 [S_IFIFO
>> S_SHIFT
] = EXT3_FT_FIFO
,
1078 [S_IFSOCK
>> S_SHIFT
] = EXT3_FT_SOCK
,
1079 [S_IFLNK
>> S_SHIFT
] = EXT3_FT_SYMLINK
,
1082 static inline void ext3_set_de_type(struct super_block
*sb
,
1083 struct ext3_dir_entry_2
*de
,
1085 if (EXT3_HAS_INCOMPAT_FEATURE(sb
, EXT3_FEATURE_INCOMPAT_FILETYPE
))
1086 de
->file_type
= ext3_type_by_mode
[(mode
& S_IFMT
)>>S_SHIFT
];
1090 * Move count entries from end of map between two memory locations.
1091 * Returns pointer to last entry moved.
1093 static struct ext3_dir_entry_2
*
1094 dx_move_dirents(char *from
, char *to
, struct dx_map_entry
*map
, int count
)
1096 unsigned rec_len
= 0;
1099 struct ext3_dir_entry_2
*de
= (struct ext3_dir_entry_2
*) (from
+ map
->offs
);
1100 rec_len
= EXT3_DIR_REC_LEN(de
->name_len
);
1101 memcpy (to
, de
, rec_len
);
1102 ((struct ext3_dir_entry_2
*) to
)->rec_len
=
1103 ext3_rec_len_to_disk(rec_len
);
1108 return (struct ext3_dir_entry_2
*) (to
- rec_len
);
1112 * Compact each dir entry in the range to the minimal rec_len.
1113 * Returns pointer to last entry in range.
1115 static struct ext3_dir_entry_2
*dx_pack_dirents(char *base
, unsigned blocksize
)
1117 struct ext3_dir_entry_2
*next
, *to
, *prev
;
1118 struct ext3_dir_entry_2
*de
= (struct ext3_dir_entry_2
*)base
;
1119 unsigned rec_len
= 0;
1122 while ((char *)de
< base
+ blocksize
) {
1123 next
= ext3_next_entry(de
);
1124 if (de
->inode
&& de
->name_len
) {
1125 rec_len
= EXT3_DIR_REC_LEN(de
->name_len
);
1127 memmove(to
, de
, rec_len
);
1128 to
->rec_len
= ext3_rec_len_to_disk(rec_len
);
1130 to
= (struct ext3_dir_entry_2
*) (((char *) to
) + rec_len
);
1138 * Split a full leaf block to make room for a new dir entry.
1139 * Allocate a new block, and move entries so that they are approx. equally full.
1140 * Returns pointer to de in block into which the new entry will be inserted.
1142 static struct ext3_dir_entry_2
*do_split(handle_t
*handle
, struct inode
*dir
,
1143 struct buffer_head
**bh
,struct dx_frame
*frame
,
1144 struct dx_hash_info
*hinfo
, int *error
)
1146 unsigned blocksize
= dir
->i_sb
->s_blocksize
;
1147 unsigned count
, continued
;
1148 struct buffer_head
*bh2
;
1151 struct dx_map_entry
*map
;
1152 char *data1
= (*bh
)->b_data
, *data2
;
1153 unsigned split
, move
, size
;
1154 struct ext3_dir_entry_2
*de
= NULL
, *de2
;
1157 bh2
= ext3_append (handle
, dir
, &newblock
, &err
);
1164 BUFFER_TRACE(*bh
, "get_write_access");
1165 err
= ext3_journal_get_write_access(handle
, *bh
);
1169 BUFFER_TRACE(frame
->bh
, "get_write_access");
1170 err
= ext3_journal_get_write_access(handle
, frame
->bh
);
1174 data2
= bh2
->b_data
;
1176 /* create map in the end of data2 block */
1177 map
= (struct dx_map_entry
*) (data2
+ blocksize
);
1178 count
= dx_make_map ((struct ext3_dir_entry_2
*) data1
,
1179 blocksize
, hinfo
, map
);
1181 dx_sort_map (map
, count
);
1182 /* Split the existing block in the middle, size-wise */
1185 for (i
= count
-1; i
>= 0; i
--) {
1186 /* is more than half of this entry in 2nd half of the block? */
1187 if (size
+ map
[i
].size
/2 > blocksize
/2)
1189 size
+= map
[i
].size
;
1192 /* map index at which we will split */
1193 split
= count
- move
;
1194 hash2
= map
[split
].hash
;
1195 continued
= hash2
== map
[split
- 1].hash
;
1196 dxtrace(printk("Split block %i at %x, %i/%i\n",
1197 dx_get_block(frame
->at
), hash2
, split
, count
-split
));
1199 /* Fancy dance to stay within two buffers */
1200 de2
= dx_move_dirents(data1
, data2
, map
+ split
, count
- split
);
1201 de
= dx_pack_dirents(data1
,blocksize
);
1202 de
->rec_len
= ext3_rec_len_to_disk(data1
+ blocksize
- (char *) de
);
1203 de2
->rec_len
= ext3_rec_len_to_disk(data2
+ blocksize
- (char *) de2
);
1204 dxtrace(dx_show_leaf (hinfo
, (struct ext3_dir_entry_2
*) data1
, blocksize
, 1));
1205 dxtrace(dx_show_leaf (hinfo
, (struct ext3_dir_entry_2
*) data2
, blocksize
, 1));
1207 /* Which block gets the new entry? */
1208 if (hinfo
->hash
>= hash2
)
1213 dx_insert_block (frame
, hash2
+ continued
, newblock
);
1214 err
= ext3_journal_dirty_metadata (handle
, bh2
);
1217 err
= ext3_journal_dirty_metadata (handle
, frame
->bh
);
1221 dxtrace(dx_show_index ("frame", frame
->entries
));
1228 ext3_std_error(dir
->i_sb
, err
);
1236 * Add a new entry into a directory (leaf) block. If de is non-NULL,
1237 * it points to a directory entry which is guaranteed to be large
1238 * enough for new directory entry. If de is NULL, then
1239 * add_dirent_to_buf will attempt search the directory block for
1240 * space. It will return -ENOSPC if no space is available, and -EIO
1241 * and -EEXIST if directory entry already exists.
1243 * NOTE! bh is NOT released in the case where ENOSPC is returned. In
1244 * all other cases bh is released.
1246 static int add_dirent_to_buf(handle_t
*handle
, struct dentry
*dentry
,
1247 struct inode
*inode
, struct ext3_dir_entry_2
*de
,
1248 struct buffer_head
* bh
)
1250 struct inode
*dir
= dentry
->d_parent
->d_inode
;
1251 const char *name
= dentry
->d_name
.name
;
1252 int namelen
= dentry
->d_name
.len
;
1253 unsigned long offset
= 0;
1254 unsigned short reclen
;
1255 int nlen
, rlen
, err
;
1258 reclen
= EXT3_DIR_REC_LEN(namelen
);
1260 de
= (struct ext3_dir_entry_2
*)bh
->b_data
;
1261 top
= bh
->b_data
+ dir
->i_sb
->s_blocksize
- reclen
;
1262 while ((char *) de
<= top
) {
1263 if (!ext3_check_dir_entry("ext3_add_entry", dir
, de
,
1268 if (ext3_match (namelen
, name
, de
)) {
1272 nlen
= EXT3_DIR_REC_LEN(de
->name_len
);
1273 rlen
= ext3_rec_len_from_disk(de
->rec_len
);
1274 if ((de
->inode
? rlen
- nlen
: rlen
) >= reclen
)
1276 de
= (struct ext3_dir_entry_2
*)((char *)de
+ rlen
);
1279 if ((char *) de
> top
)
1282 BUFFER_TRACE(bh
, "get_write_access");
1283 err
= ext3_journal_get_write_access(handle
, bh
);
1285 ext3_std_error(dir
->i_sb
, err
);
1290 /* By now the buffer is marked for journaling */
1291 nlen
= EXT3_DIR_REC_LEN(de
->name_len
);
1292 rlen
= ext3_rec_len_from_disk(de
->rec_len
);
1294 struct ext3_dir_entry_2
*de1
= (struct ext3_dir_entry_2
*)((char *)de
+ nlen
);
1295 de1
->rec_len
= ext3_rec_len_to_disk(rlen
- nlen
);
1296 de
->rec_len
= ext3_rec_len_to_disk(nlen
);
1299 de
->file_type
= EXT3_FT_UNKNOWN
;
1301 de
->inode
= cpu_to_le32(inode
->i_ino
);
1302 ext3_set_de_type(dir
->i_sb
, de
, inode
->i_mode
);
1305 de
->name_len
= namelen
;
1306 memcpy (de
->name
, name
, namelen
);
1308 * XXX shouldn't update any times until successful
1309 * completion of syscall, but too many callers depend
1312 * XXX similarly, too many callers depend on
1313 * ext3_new_inode() setting the times, but error
1314 * recovery deletes the inode, so the worst that can
1315 * happen is that the times are slightly out of date
1316 * and/or different from the directory change time.
1318 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME_SEC
;
1319 ext3_update_dx_flag(dir
);
1321 ext3_mark_inode_dirty(handle
, dir
);
1322 BUFFER_TRACE(bh
, "call ext3_journal_dirty_metadata");
1323 err
= ext3_journal_dirty_metadata(handle
, bh
);
1325 ext3_std_error(dir
->i_sb
, err
);
1331 * This converts a one block unindexed directory to a 3 block indexed
1332 * directory, and adds the dentry to the indexed directory.
1334 static int make_indexed_dir(handle_t
*handle
, struct dentry
*dentry
,
1335 struct inode
*inode
, struct buffer_head
*bh
)
1337 struct inode
*dir
= dentry
->d_parent
->d_inode
;
1338 const char *name
= dentry
->d_name
.name
;
1339 int namelen
= dentry
->d_name
.len
;
1340 struct buffer_head
*bh2
;
1341 struct dx_root
*root
;
1342 struct dx_frame frames
[2], *frame
;
1343 struct dx_entry
*entries
;
1344 struct ext3_dir_entry_2
*de
, *de2
;
1349 struct dx_hash_info hinfo
;
1351 struct fake_dirent
*fde
;
1353 blocksize
= dir
->i_sb
->s_blocksize
;
1354 dxtrace(printk(KERN_DEBUG
"Creating index: inode %lu\n", dir
->i_ino
));
1355 retval
= ext3_journal_get_write_access(handle
, bh
);
1357 ext3_std_error(dir
->i_sb
, retval
);
1361 root
= (struct dx_root
*) bh
->b_data
;
1363 /* The 0th block becomes the root, move the dirents out */
1364 fde
= &root
->dotdot
;
1365 de
= (struct ext3_dir_entry_2
*)((char *)fde
+
1366 ext3_rec_len_from_disk(fde
->rec_len
));
1367 if ((char *) de
>= (((char *) root
) + blocksize
)) {
1368 ext3_error(dir
->i_sb
, __func__
,
1369 "invalid rec_len for '..' in inode %lu",
1374 len
= ((char *) root
) + blocksize
- (char *) de
;
1376 bh2
= ext3_append (handle
, dir
, &block
, &retval
);
1381 EXT3_I(dir
)->i_flags
|= EXT3_INDEX_FL
;
1382 data1
= bh2
->b_data
;
1384 memcpy (data1
, de
, len
);
1385 de
= (struct ext3_dir_entry_2
*) data1
;
1387 while ((char *)(de2
= ext3_next_entry(de
)) < top
)
1389 de
->rec_len
= ext3_rec_len_to_disk(data1
+ blocksize
- (char *) de
);
1390 /* Initialize the root; the dot dirents already exist */
1391 de
= (struct ext3_dir_entry_2
*) (&root
->dotdot
);
1392 de
->rec_len
= ext3_rec_len_to_disk(blocksize
- EXT3_DIR_REC_LEN(2));
1393 memset (&root
->info
, 0, sizeof(root
->info
));
1394 root
->info
.info_length
= sizeof(root
->info
);
1395 root
->info
.hash_version
= EXT3_SB(dir
->i_sb
)->s_def_hash_version
;
1396 entries
= root
->entries
;
1397 dx_set_block (entries
, 1);
1398 dx_set_count (entries
, 1);
1399 dx_set_limit (entries
, dx_root_limit(dir
, sizeof(root
->info
)));
1401 /* Initialize as for dx_probe */
1402 hinfo
.hash_version
= root
->info
.hash_version
;
1403 if (hinfo
.hash_version
<= DX_HASH_TEA
)
1404 hinfo
.hash_version
+= EXT3_SB(dir
->i_sb
)->s_hash_unsigned
;
1405 hinfo
.seed
= EXT3_SB(dir
->i_sb
)->s_hash_seed
;
1406 ext3fs_dirhash(name
, namelen
, &hinfo
);
1408 frame
->entries
= entries
;
1409 frame
->at
= entries
;
1413 * Mark buffers dirty here so that if do_split() fails we write a
1414 * consistent set of buffers to disk.
1416 ext3_journal_dirty_metadata(handle
, frame
->bh
);
1417 ext3_journal_dirty_metadata(handle
, bh
);
1418 de
= do_split(handle
,dir
, &bh
, frame
, &hinfo
, &retval
);
1420 ext3_mark_inode_dirty(handle
, dir
);
1426 return add_dirent_to_buf(handle
, dentry
, inode
, de
, bh
);
1432 * adds a file entry to the specified directory, using the same
1433 * semantics as ext3_find_entry(). It returns NULL if it failed.
1435 * NOTE!! The inode part of 'de' is left at 0 - which means you
1436 * may not sleep between calling this and putting something into
1437 * the entry, as someone else might have used it while you slept.
1439 static int ext3_add_entry (handle_t
*handle
, struct dentry
*dentry
,
1440 struct inode
*inode
)
1442 struct inode
*dir
= dentry
->d_parent
->d_inode
;
1443 struct buffer_head
* bh
;
1444 struct ext3_dir_entry_2
*de
;
1445 struct super_block
* sb
;
1452 blocksize
= sb
->s_blocksize
;
1453 if (!dentry
->d_name
.len
)
1456 retval
= ext3_dx_add_entry(handle
, dentry
, inode
);
1457 if (!retval
|| (retval
!= ERR_BAD_DX_DIR
))
1459 EXT3_I(dir
)->i_flags
&= ~EXT3_INDEX_FL
;
1461 ext3_mark_inode_dirty(handle
, dir
);
1463 blocks
= dir
->i_size
>> sb
->s_blocksize_bits
;
1464 for (block
= 0; block
< blocks
; block
++) {
1465 if (!(bh
= ext3_dir_bread(handle
, dir
, block
, 0, &retval
)))
1468 retval
= add_dirent_to_buf(handle
, dentry
, inode
, NULL
, bh
);
1469 if (retval
!= -ENOSPC
)
1472 if (blocks
== 1 && !dx_fallback
&&
1473 EXT3_HAS_COMPAT_FEATURE(sb
, EXT3_FEATURE_COMPAT_DIR_INDEX
))
1474 return make_indexed_dir(handle
, dentry
, inode
, bh
);
1477 bh
= ext3_append(handle
, dir
, &block
, &retval
);
1480 de
= (struct ext3_dir_entry_2
*) bh
->b_data
;
1482 de
->rec_len
= ext3_rec_len_to_disk(blocksize
);
1483 return add_dirent_to_buf(handle
, dentry
, inode
, de
, bh
);
1487 * Returns 0 for success, or a negative error value
1489 static int ext3_dx_add_entry(handle_t
*handle
, struct dentry
*dentry
,
1490 struct inode
*inode
)
1492 struct dx_frame frames
[2], *frame
;
1493 struct dx_entry
*entries
, *at
;
1494 struct dx_hash_info hinfo
;
1495 struct buffer_head
* bh
;
1496 struct inode
*dir
= dentry
->d_parent
->d_inode
;
1497 struct super_block
* sb
= dir
->i_sb
;
1498 struct ext3_dir_entry_2
*de
;
1501 frame
= dx_probe(&dentry
->d_name
, dir
, &hinfo
, frames
, &err
);
1504 entries
= frame
->entries
;
1507 if (!(bh
= ext3_dir_bread(handle
, dir
, dx_get_block(frame
->at
), 0, &err
)))
1510 BUFFER_TRACE(bh
, "get_write_access");
1511 err
= ext3_journal_get_write_access(handle
, bh
);
1515 err
= add_dirent_to_buf(handle
, dentry
, inode
, NULL
, bh
);
1516 if (err
!= -ENOSPC
) {
1521 /* Block full, should compress but for now just split */
1522 dxtrace(printk("using %u of %u node entries\n",
1523 dx_get_count(entries
), dx_get_limit(entries
)));
1524 /* Need to split index? */
1525 if (dx_get_count(entries
) == dx_get_limit(entries
)) {
1527 unsigned icount
= dx_get_count(entries
);
1528 int levels
= frame
- frames
;
1529 struct dx_entry
*entries2
;
1530 struct dx_node
*node2
;
1531 struct buffer_head
*bh2
;
1533 if (levels
&& (dx_get_count(frames
->entries
) ==
1534 dx_get_limit(frames
->entries
))) {
1535 ext3_warning(sb
, __func__
,
1536 "Directory index full!");
1540 bh2
= ext3_append (handle
, dir
, &newblock
, &err
);
1543 node2
= (struct dx_node
*)(bh2
->b_data
);
1544 entries2
= node2
->entries
;
1545 memset(&node2
->fake
, 0, sizeof(struct fake_dirent
));
1546 node2
->fake
.rec_len
= ext3_rec_len_to_disk(sb
->s_blocksize
);
1547 BUFFER_TRACE(frame
->bh
, "get_write_access");
1548 err
= ext3_journal_get_write_access(handle
, frame
->bh
);
1552 unsigned icount1
= icount
/2, icount2
= icount
- icount1
;
1553 unsigned hash2
= dx_get_hash(entries
+ icount1
);
1554 dxtrace(printk("Split index %i/%i\n", icount1
, icount2
));
1556 BUFFER_TRACE(frame
->bh
, "get_write_access"); /* index root */
1557 err
= ext3_journal_get_write_access(handle
,
1562 memcpy ((char *) entries2
, (char *) (entries
+ icount1
),
1563 icount2
* sizeof(struct dx_entry
));
1564 dx_set_count (entries
, icount1
);
1565 dx_set_count (entries2
, icount2
);
1566 dx_set_limit (entries2
, dx_node_limit(dir
));
1568 /* Which index block gets the new entry? */
1569 if (at
- entries
>= icount1
) {
1570 frame
->at
= at
= at
- entries
- icount1
+ entries2
;
1571 frame
->entries
= entries
= entries2
;
1572 swap(frame
->bh
, bh2
);
1574 dx_insert_block (frames
+ 0, hash2
, newblock
);
1575 dxtrace(dx_show_index ("node", frames
[1].entries
));
1576 dxtrace(dx_show_index ("node",
1577 ((struct dx_node
*) bh2
->b_data
)->entries
));
1578 err
= ext3_journal_dirty_metadata(handle
, bh2
);
1583 dxtrace(printk("Creating second level index...\n"));
1584 memcpy((char *) entries2
, (char *) entries
,
1585 icount
* sizeof(struct dx_entry
));
1586 dx_set_limit(entries2
, dx_node_limit(dir
));
1589 dx_set_count(entries
, 1);
1590 dx_set_block(entries
+ 0, newblock
);
1591 ((struct dx_root
*) frames
[0].bh
->b_data
)->info
.indirect_levels
= 1;
1593 /* Add new access path frame */
1595 frame
->at
= at
= at
- entries
+ entries2
;
1596 frame
->entries
= entries
= entries2
;
1598 err
= ext3_journal_get_write_access(handle
,
1603 err
= ext3_journal_dirty_metadata(handle
, frames
[0].bh
);
1607 de
= do_split(handle
, dir
, &bh
, frame
, &hinfo
, &err
);
1610 err
= add_dirent_to_buf(handle
, dentry
, inode
, de
, bh
);
1615 ext3_std_error(dir
->i_sb
, err
);
1624 * ext3_delete_entry deletes a directory entry by merging it with the
1627 static int ext3_delete_entry (handle_t
*handle
,
1629 struct ext3_dir_entry_2
* de_del
,
1630 struct buffer_head
* bh
)
1632 struct ext3_dir_entry_2
* de
, * pde
;
1637 de
= (struct ext3_dir_entry_2
*) bh
->b_data
;
1638 while (i
< bh
->b_size
) {
1639 if (!ext3_check_dir_entry("ext3_delete_entry", dir
, de
, bh
, i
))
1644 BUFFER_TRACE(bh
, "get_write_access");
1645 err
= ext3_journal_get_write_access(handle
, bh
);
1650 pde
->rec_len
= ext3_rec_len_to_disk(
1651 ext3_rec_len_from_disk(pde
->rec_len
) +
1652 ext3_rec_len_from_disk(de
->rec_len
));
1656 BUFFER_TRACE(bh
, "call ext3_journal_dirty_metadata");
1657 err
= ext3_journal_dirty_metadata(handle
, bh
);
1660 ext3_std_error(dir
->i_sb
, err
);
1665 i
+= ext3_rec_len_from_disk(de
->rec_len
);
1667 de
= ext3_next_entry(de
);
1672 static int ext3_add_nondir(handle_t
*handle
,
1673 struct dentry
*dentry
, struct inode
*inode
)
1675 int err
= ext3_add_entry(handle
, dentry
, inode
);
1677 ext3_mark_inode_dirty(handle
, inode
);
1678 unlock_new_inode(inode
);
1679 d_instantiate(dentry
, inode
);
1683 unlock_new_inode(inode
);
1689 * By the time this is called, we already have created
1690 * the directory cache entry for the new file, but it
1691 * is so far negative - it has no inode.
1693 * If the create succeeds, we fill in the inode information
1694 * with d_instantiate().
1696 static int ext3_create (struct inode
* dir
, struct dentry
* dentry
, umode_t mode
,
1700 struct inode
* inode
;
1701 int err
, retries
= 0;
1703 dquot_initialize(dir
);
1706 handle
= ext3_journal_start(dir
, EXT3_DATA_TRANS_BLOCKS(dir
->i_sb
) +
1707 EXT3_INDEX_EXTRA_TRANS_BLOCKS
+ 3 +
1708 EXT3_MAXQUOTAS_INIT_BLOCKS(dir
->i_sb
));
1710 return PTR_ERR(handle
);
1712 if (IS_DIRSYNC(dir
))
1715 inode
= ext3_new_inode (handle
, dir
, &dentry
->d_name
, mode
);
1716 err
= PTR_ERR(inode
);
1717 if (!IS_ERR(inode
)) {
1718 inode
->i_op
= &ext3_file_inode_operations
;
1719 inode
->i_fop
= &ext3_file_operations
;
1720 ext3_set_aops(inode
);
1721 err
= ext3_add_nondir(handle
, dentry
, inode
);
1723 ext3_journal_stop(handle
);
1724 if (err
== -ENOSPC
&& ext3_should_retry_alloc(dir
->i_sb
, &retries
))
1729 static int ext3_mknod (struct inode
* dir
, struct dentry
*dentry
,
1730 umode_t mode
, dev_t rdev
)
1733 struct inode
*inode
;
1734 int err
, retries
= 0;
1736 if (!new_valid_dev(rdev
))
1739 dquot_initialize(dir
);
1742 handle
= ext3_journal_start(dir
, EXT3_DATA_TRANS_BLOCKS(dir
->i_sb
) +
1743 EXT3_INDEX_EXTRA_TRANS_BLOCKS
+ 3 +
1744 EXT3_MAXQUOTAS_INIT_BLOCKS(dir
->i_sb
));
1746 return PTR_ERR(handle
);
1748 if (IS_DIRSYNC(dir
))
1751 inode
= ext3_new_inode (handle
, dir
, &dentry
->d_name
, mode
);
1752 err
= PTR_ERR(inode
);
1753 if (!IS_ERR(inode
)) {
1754 init_special_inode(inode
, inode
->i_mode
, rdev
);
1755 #ifdef CONFIG_EXT3_FS_XATTR
1756 inode
->i_op
= &ext3_special_inode_operations
;
1758 err
= ext3_add_nondir(handle
, dentry
, inode
);
1760 ext3_journal_stop(handle
);
1761 if (err
== -ENOSPC
&& ext3_should_retry_alloc(dir
->i_sb
, &retries
))
1766 static int ext3_mkdir(struct inode
* dir
, struct dentry
* dentry
, umode_t mode
)
1769 struct inode
* inode
;
1770 struct buffer_head
* dir_block
= NULL
;
1771 struct ext3_dir_entry_2
* de
;
1772 int err
, retries
= 0;
1774 if (dir
->i_nlink
>= EXT3_LINK_MAX
)
1777 dquot_initialize(dir
);
1780 handle
= ext3_journal_start(dir
, EXT3_DATA_TRANS_BLOCKS(dir
->i_sb
) +
1781 EXT3_INDEX_EXTRA_TRANS_BLOCKS
+ 3 +
1782 EXT3_MAXQUOTAS_INIT_BLOCKS(dir
->i_sb
));
1784 return PTR_ERR(handle
);
1786 if (IS_DIRSYNC(dir
))
1789 inode
= ext3_new_inode (handle
, dir
, &dentry
->d_name
, S_IFDIR
| mode
);
1790 err
= PTR_ERR(inode
);
1794 inode
->i_op
= &ext3_dir_inode_operations
;
1795 inode
->i_fop
= &ext3_dir_operations
;
1796 inode
->i_size
= EXT3_I(inode
)->i_disksize
= inode
->i_sb
->s_blocksize
;
1797 if (!(dir_block
= ext3_dir_bread(handle
, inode
, 0, 1, &err
)))
1798 goto out_clear_inode
;
1800 BUFFER_TRACE(dir_block
, "get_write_access");
1801 err
= ext3_journal_get_write_access(handle
, dir_block
);
1803 goto out_clear_inode
;
1805 de
= (struct ext3_dir_entry_2
*) dir_block
->b_data
;
1806 de
->inode
= cpu_to_le32(inode
->i_ino
);
1808 de
->rec_len
= ext3_rec_len_to_disk(EXT3_DIR_REC_LEN(de
->name_len
));
1809 strcpy (de
->name
, ".");
1810 ext3_set_de_type(dir
->i_sb
, de
, S_IFDIR
);
1811 de
= ext3_next_entry(de
);
1812 de
->inode
= cpu_to_le32(dir
->i_ino
);
1813 de
->rec_len
= ext3_rec_len_to_disk(inode
->i_sb
->s_blocksize
-
1814 EXT3_DIR_REC_LEN(1));
1816 strcpy (de
->name
, "..");
1817 ext3_set_de_type(dir
->i_sb
, de
, S_IFDIR
);
1818 set_nlink(inode
, 2);
1819 BUFFER_TRACE(dir_block
, "call ext3_journal_dirty_metadata");
1820 err
= ext3_journal_dirty_metadata(handle
, dir_block
);
1822 goto out_clear_inode
;
1824 err
= ext3_mark_inode_dirty(handle
, inode
);
1826 err
= ext3_add_entry (handle
, dentry
, inode
);
1831 unlock_new_inode(inode
);
1832 ext3_mark_inode_dirty(handle
, inode
);
1837 ext3_update_dx_flag(dir
);
1838 err
= ext3_mark_inode_dirty(handle
, dir
);
1840 goto out_clear_inode
;
1842 unlock_new_inode(inode
);
1843 d_instantiate(dentry
, inode
);
1846 ext3_journal_stop(handle
);
1847 if (err
== -ENOSPC
&& ext3_should_retry_alloc(dir
->i_sb
, &retries
))
1853 * routine to check that the specified directory is empty (for rmdir)
1855 static int empty_dir (struct inode
* inode
)
1857 unsigned long offset
;
1858 struct buffer_head
* bh
;
1859 struct ext3_dir_entry_2
* de
, * de1
;
1860 struct super_block
* sb
;
1864 if (inode
->i_size
< EXT3_DIR_REC_LEN(1) + EXT3_DIR_REC_LEN(2) ||
1865 !(bh
= ext3_dir_bread(NULL
, inode
, 0, 0, &err
))) {
1867 ext3_error(inode
->i_sb
, __func__
,
1868 "error %d reading directory #%lu offset 0",
1871 ext3_warning(inode
->i_sb
, __func__
,
1872 "bad directory (dir #%lu) - no data block",
1876 de
= (struct ext3_dir_entry_2
*) bh
->b_data
;
1877 de1
= ext3_next_entry(de
);
1878 if (le32_to_cpu(de
->inode
) != inode
->i_ino
||
1879 !le32_to_cpu(de1
->inode
) ||
1880 strcmp (".", de
->name
) ||
1881 strcmp ("..", de1
->name
)) {
1882 ext3_warning (inode
->i_sb
, "empty_dir",
1883 "bad directory (dir #%lu) - no `.' or `..'",
1888 offset
= ext3_rec_len_from_disk(de
->rec_len
) +
1889 ext3_rec_len_from_disk(de1
->rec_len
);
1890 de
= ext3_next_entry(de1
);
1891 while (offset
< inode
->i_size
) {
1893 (void *) de
>= (void *) (bh
->b_data
+sb
->s_blocksize
)) {
1896 if (!(bh
= ext3_dir_bread (NULL
, inode
,
1897 offset
>> EXT3_BLOCK_SIZE_BITS(sb
), 0, &err
))) {
1899 ext3_error(sb
, __func__
,
1900 "error %d reading directory"
1902 err
, inode
->i_ino
, offset
);
1903 offset
+= sb
->s_blocksize
;
1906 de
= (struct ext3_dir_entry_2
*) bh
->b_data
;
1908 if (!ext3_check_dir_entry("empty_dir", inode
, de
, bh
, offset
)) {
1909 de
= (struct ext3_dir_entry_2
*)(bh
->b_data
+
1911 offset
= (offset
| (sb
->s_blocksize
- 1)) + 1;
1914 if (le32_to_cpu(de
->inode
)) {
1918 offset
+= ext3_rec_len_from_disk(de
->rec_len
);
1919 de
= ext3_next_entry(de
);
1925 /* ext3_orphan_add() links an unlinked or truncated inode into a list of
1926 * such inodes, starting at the superblock, in case we crash before the
1927 * file is closed/deleted, or in case the inode truncate spans multiple
1928 * transactions and the last transaction is not recovered after a crash.
1930 * At filesystem recovery time, we walk this list deleting unlinked
1931 * inodes and truncating linked inodes in ext3_orphan_cleanup().
1933 int ext3_orphan_add(handle_t
*handle
, struct inode
*inode
)
1935 struct super_block
*sb
= inode
->i_sb
;
1936 struct ext3_iloc iloc
;
1939 mutex_lock(&EXT3_SB(sb
)->s_orphan_lock
);
1940 if (!list_empty(&EXT3_I(inode
)->i_orphan
))
1943 /* Orphan handling is only valid for files with data blocks
1944 * being truncated, or files being unlinked. */
1946 /* @@@ FIXME: Observation from aviro:
1947 * I think I can trigger J_ASSERT in ext3_orphan_add(). We block
1948 * here (on s_orphan_lock), so race with ext3_link() which might bump
1949 * ->i_nlink. For, say it, character device. Not a regular file,
1950 * not a directory, not a symlink and ->i_nlink > 0.
1952 * tytso, 4/25/2009: I'm not sure how that could happen;
1953 * shouldn't the fs core protect us from these sort of
1954 * unlink()/link() races?
1956 J_ASSERT ((S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
1957 S_ISLNK(inode
->i_mode
)) || inode
->i_nlink
== 0);
1959 BUFFER_TRACE(EXT3_SB(sb
)->s_sbh
, "get_write_access");
1960 err
= ext3_journal_get_write_access(handle
, EXT3_SB(sb
)->s_sbh
);
1964 err
= ext3_reserve_inode_write(handle
, inode
, &iloc
);
1968 /* Insert this inode at the head of the on-disk orphan list... */
1969 NEXT_ORPHAN(inode
) = le32_to_cpu(EXT3_SB(sb
)->s_es
->s_last_orphan
);
1970 EXT3_SB(sb
)->s_es
->s_last_orphan
= cpu_to_le32(inode
->i_ino
);
1971 err
= ext3_journal_dirty_metadata(handle
, EXT3_SB(sb
)->s_sbh
);
1972 rc
= ext3_mark_iloc_dirty(handle
, inode
, &iloc
);
1976 /* Only add to the head of the in-memory list if all the
1977 * previous operations succeeded. If the orphan_add is going to
1978 * fail (possibly taking the journal offline), we can't risk
1979 * leaving the inode on the orphan list: stray orphan-list
1980 * entries can cause panics at unmount time.
1982 * This is safe: on error we're going to ignore the orphan list
1983 * anyway on the next recovery. */
1985 list_add(&EXT3_I(inode
)->i_orphan
, &EXT3_SB(sb
)->s_orphan
);
1987 jbd_debug(4, "superblock will point to %lu\n", inode
->i_ino
);
1988 jbd_debug(4, "orphan inode %lu will point to %d\n",
1989 inode
->i_ino
, NEXT_ORPHAN(inode
));
1991 mutex_unlock(&EXT3_SB(sb
)->s_orphan_lock
);
1992 ext3_std_error(inode
->i_sb
, err
);
1997 * ext3_orphan_del() removes an unlinked or truncated inode from the list
1998 * of such inodes stored on disk, because it is finally being cleaned up.
2000 int ext3_orphan_del(handle_t
*handle
, struct inode
*inode
)
2002 struct list_head
*prev
;
2003 struct ext3_inode_info
*ei
= EXT3_I(inode
);
2004 struct ext3_sb_info
*sbi
;
2005 unsigned long ino_next
;
2006 struct ext3_iloc iloc
;
2009 mutex_lock(&EXT3_SB(inode
->i_sb
)->s_orphan_lock
);
2010 if (list_empty(&ei
->i_orphan
))
2013 ino_next
= NEXT_ORPHAN(inode
);
2014 prev
= ei
->i_orphan
.prev
;
2015 sbi
= EXT3_SB(inode
->i_sb
);
2017 jbd_debug(4, "remove inode %lu from orphan list\n", inode
->i_ino
);
2019 list_del_init(&ei
->i_orphan
);
2021 /* If we're on an error path, we may not have a valid
2022 * transaction handle with which to update the orphan list on
2023 * disk, but we still need to remove the inode from the linked
2024 * list in memory. */
2028 err
= ext3_reserve_inode_write(handle
, inode
, &iloc
);
2032 if (prev
== &sbi
->s_orphan
) {
2033 jbd_debug(4, "superblock will point to %lu\n", ino_next
);
2034 BUFFER_TRACE(sbi
->s_sbh
, "get_write_access");
2035 err
= ext3_journal_get_write_access(handle
, sbi
->s_sbh
);
2038 sbi
->s_es
->s_last_orphan
= cpu_to_le32(ino_next
);
2039 err
= ext3_journal_dirty_metadata(handle
, sbi
->s_sbh
);
2041 struct ext3_iloc iloc2
;
2042 struct inode
*i_prev
=
2043 &list_entry(prev
, struct ext3_inode_info
, i_orphan
)->vfs_inode
;
2045 jbd_debug(4, "orphan inode %lu will point to %lu\n",
2046 i_prev
->i_ino
, ino_next
);
2047 err
= ext3_reserve_inode_write(handle
, i_prev
, &iloc2
);
2050 NEXT_ORPHAN(i_prev
) = ino_next
;
2051 err
= ext3_mark_iloc_dirty(handle
, i_prev
, &iloc2
);
2055 NEXT_ORPHAN(inode
) = 0;
2056 err
= ext3_mark_iloc_dirty(handle
, inode
, &iloc
);
2059 ext3_std_error(inode
->i_sb
, err
);
2061 mutex_unlock(&EXT3_SB(inode
->i_sb
)->s_orphan_lock
);
2069 static int ext3_rmdir (struct inode
* dir
, struct dentry
*dentry
)
2072 struct inode
* inode
;
2073 struct buffer_head
* bh
;
2074 struct ext3_dir_entry_2
* de
;
2077 /* Initialize quotas before so that eventual writes go in
2078 * separate transaction */
2079 dquot_initialize(dir
);
2080 dquot_initialize(dentry
->d_inode
);
2082 handle
= ext3_journal_start(dir
, EXT3_DELETE_TRANS_BLOCKS(dir
->i_sb
));
2084 return PTR_ERR(handle
);
2087 bh
= ext3_find_entry(dir
, &dentry
->d_name
, &de
);
2091 if (IS_DIRSYNC(dir
))
2094 inode
= dentry
->d_inode
;
2097 if (le32_to_cpu(de
->inode
) != inode
->i_ino
)
2100 retval
= -ENOTEMPTY
;
2101 if (!empty_dir (inode
))
2104 retval
= ext3_delete_entry(handle
, dir
, de
, bh
);
2107 if (inode
->i_nlink
!= 2)
2108 ext3_warning (inode
->i_sb
, "ext3_rmdir",
2109 "empty directory has nlink!=2 (%d)",
2113 /* There's no need to set i_disksize: the fact that i_nlink is
2114 * zero will ensure that the right thing happens during any
2117 ext3_orphan_add(handle
, inode
);
2118 inode
->i_ctime
= dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME_SEC
;
2119 ext3_mark_inode_dirty(handle
, inode
);
2121 ext3_update_dx_flag(dir
);
2122 ext3_mark_inode_dirty(handle
, dir
);
2125 ext3_journal_stop(handle
);
2130 static int ext3_unlink(struct inode
* dir
, struct dentry
*dentry
)
2133 struct inode
* inode
;
2134 struct buffer_head
* bh
;
2135 struct ext3_dir_entry_2
* de
;
2138 trace_ext3_unlink_enter(dir
, dentry
);
2139 /* Initialize quotas before so that eventual writes go
2140 * in separate transaction */
2141 dquot_initialize(dir
);
2142 dquot_initialize(dentry
->d_inode
);
2144 handle
= ext3_journal_start(dir
, EXT3_DELETE_TRANS_BLOCKS(dir
->i_sb
));
2146 return PTR_ERR(handle
);
2148 if (IS_DIRSYNC(dir
))
2152 bh
= ext3_find_entry(dir
, &dentry
->d_name
, &de
);
2156 inode
= dentry
->d_inode
;
2159 if (le32_to_cpu(de
->inode
) != inode
->i_ino
)
2162 if (!inode
->i_nlink
) {
2163 ext3_warning (inode
->i_sb
, "ext3_unlink",
2164 "Deleting nonexistent file (%lu), %d",
2165 inode
->i_ino
, inode
->i_nlink
);
2166 set_nlink(inode
, 1);
2168 retval
= ext3_delete_entry(handle
, dir
, de
, bh
);
2171 dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME_SEC
;
2172 ext3_update_dx_flag(dir
);
2173 ext3_mark_inode_dirty(handle
, dir
);
2175 if (!inode
->i_nlink
)
2176 ext3_orphan_add(handle
, inode
);
2177 inode
->i_ctime
= dir
->i_ctime
;
2178 ext3_mark_inode_dirty(handle
, inode
);
2182 ext3_journal_stop(handle
);
2184 trace_ext3_unlink_exit(dentry
, retval
);
2188 static int ext3_symlink (struct inode
* dir
,
2189 struct dentry
*dentry
, const char * symname
)
2192 struct inode
* inode
;
2193 int l
, err
, retries
= 0;
2196 l
= strlen(symname
)+1;
2197 if (l
> dir
->i_sb
->s_blocksize
)
2198 return -ENAMETOOLONG
;
2200 dquot_initialize(dir
);
2202 if (l
> EXT3_N_BLOCKS
* 4) {
2204 * For non-fast symlinks, we just allocate inode and put it on
2205 * orphan list in the first transaction => we need bitmap,
2206 * group descriptor, sb, inode block, quota blocks, and
2207 * possibly selinux xattr blocks.
2209 credits
= 4 + EXT3_MAXQUOTAS_INIT_BLOCKS(dir
->i_sb
) +
2210 EXT3_XATTR_TRANS_BLOCKS
;
2213 * Fast symlink. We have to add entry to directory
2214 * (EXT3_DATA_TRANS_BLOCKS + EXT3_INDEX_EXTRA_TRANS_BLOCKS),
2215 * allocate new inode (bitmap, group descriptor, inode block,
2216 * quota blocks, sb is already counted in previous macros).
2218 credits
= EXT3_DATA_TRANS_BLOCKS(dir
->i_sb
) +
2219 EXT3_INDEX_EXTRA_TRANS_BLOCKS
+ 3 +
2220 EXT3_MAXQUOTAS_INIT_BLOCKS(dir
->i_sb
);
2223 handle
= ext3_journal_start(dir
, credits
);
2225 return PTR_ERR(handle
);
2227 if (IS_DIRSYNC(dir
))
2230 inode
= ext3_new_inode (handle
, dir
, &dentry
->d_name
, S_IFLNK
|S_IRWXUGO
);
2231 err
= PTR_ERR(inode
);
2235 if (l
> EXT3_N_BLOCKS
* 4) {
2236 inode
->i_op
= &ext3_symlink_inode_operations
;
2237 ext3_set_aops(inode
);
2239 * We cannot call page_symlink() with transaction started
2240 * because it calls into ext3_write_begin() which acquires page
2241 * lock which ranks below transaction start (and it can also
2242 * wait for journal commit if we are running out of space). So
2243 * we have to stop transaction now and restart it when symlink
2244 * contents is written.
2246 * To keep fs consistent in case of crash, we have to put inode
2247 * to orphan list in the mean time.
2250 err
= ext3_orphan_add(handle
, inode
);
2251 ext3_journal_stop(handle
);
2253 goto err_drop_inode
;
2254 err
= __page_symlink(inode
, symname
, l
, 1);
2256 goto err_drop_inode
;
2258 * Now inode is being linked into dir (EXT3_DATA_TRANS_BLOCKS
2259 * + EXT3_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
2261 handle
= ext3_journal_start(dir
,
2262 EXT3_DATA_TRANS_BLOCKS(dir
->i_sb
) +
2263 EXT3_INDEX_EXTRA_TRANS_BLOCKS
+ 1);
2264 if (IS_ERR(handle
)) {
2265 err
= PTR_ERR(handle
);
2266 goto err_drop_inode
;
2268 set_nlink(inode
, 1);
2269 err
= ext3_orphan_del(handle
, inode
);
2271 ext3_journal_stop(handle
);
2273 goto err_drop_inode
;
2276 inode
->i_op
= &ext3_fast_symlink_inode_operations
;
2277 memcpy((char*)&EXT3_I(inode
)->i_data
,symname
,l
);
2278 inode
->i_size
= l
-1;
2280 EXT3_I(inode
)->i_disksize
= inode
->i_size
;
2281 err
= ext3_add_nondir(handle
, dentry
, inode
);
2283 ext3_journal_stop(handle
);
2284 if (err
== -ENOSPC
&& ext3_should_retry_alloc(dir
->i_sb
, &retries
))
2288 unlock_new_inode(inode
);
2293 static int ext3_link (struct dentry
* old_dentry
,
2294 struct inode
* dir
, struct dentry
*dentry
)
2297 struct inode
*inode
= old_dentry
->d_inode
;
2298 int err
, retries
= 0;
2300 if (inode
->i_nlink
>= EXT3_LINK_MAX
)
2303 dquot_initialize(dir
);
2306 handle
= ext3_journal_start(dir
, EXT3_DATA_TRANS_BLOCKS(dir
->i_sb
) +
2307 EXT3_INDEX_EXTRA_TRANS_BLOCKS
);
2309 return PTR_ERR(handle
);
2311 if (IS_DIRSYNC(dir
))
2314 inode
->i_ctime
= CURRENT_TIME_SEC
;
2318 err
= ext3_add_entry(handle
, dentry
, inode
);
2320 ext3_mark_inode_dirty(handle
, inode
);
2321 d_instantiate(dentry
, inode
);
2326 ext3_journal_stop(handle
);
2327 if (err
== -ENOSPC
&& ext3_should_retry_alloc(dir
->i_sb
, &retries
))
2332 #define PARENT_INO(buffer) \
2333 (ext3_next_entry((struct ext3_dir_entry_2 *)(buffer))->inode)
2336 * Anybody can rename anything with this: the permission checks are left to the
2337 * higher-level routines.
2339 static int ext3_rename (struct inode
* old_dir
, struct dentry
*old_dentry
,
2340 struct inode
* new_dir
,struct dentry
*new_dentry
)
2343 struct inode
* old_inode
, * new_inode
;
2344 struct buffer_head
* old_bh
, * new_bh
, * dir_bh
;
2345 struct ext3_dir_entry_2
* old_de
, * new_de
;
2346 int retval
, flush_file
= 0;
2348 dquot_initialize(old_dir
);
2349 dquot_initialize(new_dir
);
2351 old_bh
= new_bh
= dir_bh
= NULL
;
2353 /* Initialize quotas before so that eventual writes go
2354 * in separate transaction */
2355 if (new_dentry
->d_inode
)
2356 dquot_initialize(new_dentry
->d_inode
);
2357 handle
= ext3_journal_start(old_dir
, 2 *
2358 EXT3_DATA_TRANS_BLOCKS(old_dir
->i_sb
) +
2359 EXT3_INDEX_EXTRA_TRANS_BLOCKS
+ 2);
2361 return PTR_ERR(handle
);
2363 if (IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
))
2366 old_bh
= ext3_find_entry(old_dir
, &old_dentry
->d_name
, &old_de
);
2368 * Check for inode number is _not_ due to possible IO errors.
2369 * We might rmdir the source, keep it as pwd of some process
2370 * and merrily kill the link to whatever was created under the
2371 * same name. Goodbye sticky bit ;-<
2373 old_inode
= old_dentry
->d_inode
;
2375 if (!old_bh
|| le32_to_cpu(old_de
->inode
) != old_inode
->i_ino
)
2378 new_inode
= new_dentry
->d_inode
;
2379 new_bh
= ext3_find_entry(new_dir
, &new_dentry
->d_name
, &new_de
);
2386 if (S_ISDIR(old_inode
->i_mode
)) {
2388 retval
= -ENOTEMPTY
;
2389 if (!empty_dir (new_inode
))
2393 dir_bh
= ext3_dir_bread(handle
, old_inode
, 0, 0, &retval
);
2396 if (le32_to_cpu(PARENT_INO(dir_bh
->b_data
)) != old_dir
->i_ino
)
2399 if (!new_inode
&& new_dir
!=old_dir
&&
2400 new_dir
->i_nlink
>= EXT3_LINK_MAX
)
2404 retval
= ext3_add_entry (handle
, new_dentry
, old_inode
);
2408 BUFFER_TRACE(new_bh
, "get write access");
2409 retval
= ext3_journal_get_write_access(handle
, new_bh
);
2412 new_de
->inode
= cpu_to_le32(old_inode
->i_ino
);
2413 if (EXT3_HAS_INCOMPAT_FEATURE(new_dir
->i_sb
,
2414 EXT3_FEATURE_INCOMPAT_FILETYPE
))
2415 new_de
->file_type
= old_de
->file_type
;
2416 new_dir
->i_version
++;
2417 new_dir
->i_ctime
= new_dir
->i_mtime
= CURRENT_TIME_SEC
;
2418 ext3_mark_inode_dirty(handle
, new_dir
);
2419 BUFFER_TRACE(new_bh
, "call ext3_journal_dirty_metadata");
2420 retval
= ext3_journal_dirty_metadata(handle
, new_bh
);
2428 * Like most other Unix systems, set the ctime for inodes on a
2431 old_inode
->i_ctime
= CURRENT_TIME_SEC
;
2432 ext3_mark_inode_dirty(handle
, old_inode
);
2437 if (le32_to_cpu(old_de
->inode
) != old_inode
->i_ino
||
2438 old_de
->name_len
!= old_dentry
->d_name
.len
||
2439 strncmp(old_de
->name
, old_dentry
->d_name
.name
, old_de
->name_len
) ||
2440 (retval
= ext3_delete_entry(handle
, old_dir
,
2441 old_de
, old_bh
)) == -ENOENT
) {
2442 /* old_de could have moved from under us during htree split, so
2443 * make sure that we are deleting the right entry. We might
2444 * also be pointing to a stale entry in the unused part of
2445 * old_bh so just checking inum and the name isn't enough. */
2446 struct buffer_head
*old_bh2
;
2447 struct ext3_dir_entry_2
*old_de2
;
2449 old_bh2
= ext3_find_entry(old_dir
, &old_dentry
->d_name
,
2452 retval
= ext3_delete_entry(handle
, old_dir
,
2458 ext3_warning(old_dir
->i_sb
, "ext3_rename",
2459 "Deleting old file (%lu), %d, error=%d",
2460 old_dir
->i_ino
, old_dir
->i_nlink
, retval
);
2464 drop_nlink(new_inode
);
2465 new_inode
->i_ctime
= CURRENT_TIME_SEC
;
2467 old_dir
->i_ctime
= old_dir
->i_mtime
= CURRENT_TIME_SEC
;
2468 ext3_update_dx_flag(old_dir
);
2470 BUFFER_TRACE(dir_bh
, "get_write_access");
2471 retval
= ext3_journal_get_write_access(handle
, dir_bh
);
2474 PARENT_INO(dir_bh
->b_data
) = cpu_to_le32(new_dir
->i_ino
);
2475 BUFFER_TRACE(dir_bh
, "call ext3_journal_dirty_metadata");
2476 retval
= ext3_journal_dirty_metadata(handle
, dir_bh
);
2479 ext3_std_error(new_dir
->i_sb
, retval
);
2482 drop_nlink(old_dir
);
2484 drop_nlink(new_inode
);
2487 ext3_update_dx_flag(new_dir
);
2488 ext3_mark_inode_dirty(handle
, new_dir
);
2491 ext3_mark_inode_dirty(handle
, old_dir
);
2493 ext3_mark_inode_dirty(handle
, new_inode
);
2494 if (!new_inode
->i_nlink
)
2495 ext3_orphan_add(handle
, new_inode
);
2496 if (ext3_should_writeback_data(new_inode
))
2505 ext3_journal_stop(handle
);
2506 if (retval
== 0 && flush_file
)
2507 filemap_flush(old_inode
->i_mapping
);
2512 * directories can handle most operations...
2514 const struct inode_operations ext3_dir_inode_operations
= {
2515 .create
= ext3_create
,
2516 .lookup
= ext3_lookup
,
2518 .unlink
= ext3_unlink
,
2519 .symlink
= ext3_symlink
,
2520 .mkdir
= ext3_mkdir
,
2521 .rmdir
= ext3_rmdir
,
2522 .mknod
= ext3_mknod
,
2523 .rename
= ext3_rename
,
2524 .setattr
= ext3_setattr
,
2525 #ifdef CONFIG_EXT3_FS_XATTR
2526 .setxattr
= generic_setxattr
,
2527 .getxattr
= generic_getxattr
,
2528 .listxattr
= ext3_listxattr
,
2529 .removexattr
= generic_removexattr
,
2531 .get_acl
= ext3_get_acl
,
2534 const struct inode_operations ext3_special_inode_operations
= {
2535 .setattr
= ext3_setattr
,
2536 #ifdef CONFIG_EXT3_FS_XATTR
2537 .setxattr
= generic_setxattr
,
2538 .getxattr
= generic_getxattr
,
2539 .listxattr
= ext3_listxattr
,
2540 .removexattr
= generic_removexattr
,
2542 .get_acl
= ext3_get_acl
,