2 * linux/fs/ext4/namei.c
4 * Copyright (C) 1992, 1993, 1994, 1995
5 * Remy Card (card@masi.ibp.fr)
6 * Laboratoire MASI - Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
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/jbd2.h>
30 #include <linux/time.h>
31 #include <linux/ext4_fs.h>
32 #include <linux/ext4_jbd2.h>
33 #include <linux/fcntl.h>
34 #include <linux/stat.h>
35 #include <linux/string.h>
36 #include <linux/quotaops.h>
37 #include <linux/buffer_head.h>
38 #include <linux/bio.h>
45 * define how far ahead to read directories while searching them.
47 #define NAMEI_RA_CHUNKS 2
48 #define NAMEI_RA_BLOCKS 4
49 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
50 #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
52 static struct buffer_head
*ext4_append(handle_t
*handle
,
56 struct buffer_head
*bh
;
58 *block
= inode
->i_size
>> inode
->i_sb
->s_blocksize_bits
;
60 if ((bh
= ext4_bread(handle
, inode
, *block
, 1, err
))) {
61 inode
->i_size
+= inode
->i_sb
->s_blocksize
;
62 EXT4_I(inode
)->i_disksize
= inode
->i_size
;
63 ext4_journal_get_write_access(handle
,bh
);
69 #define assert(test) J_ASSERT(test)
73 #define swap(x, y) do { typeof(x) z = x; x = y; y = z; } while (0)
77 #define dxtrace(command) command
79 #define dxtrace(command)
103 * dx_root_info is laid out so that if it should somehow get overlaid by a
104 * dirent the two low bits of the hash version will be zero. Therefore, the
105 * hash version mod 4 should never be 0. Sincerely, the paranoia department.
110 struct fake_dirent dot
;
112 struct fake_dirent dotdot
;
116 __le32 reserved_zero
;
118 u8 info_length
; /* 8 */
123 struct dx_entry entries
[0];
128 struct fake_dirent fake
;
129 struct dx_entry entries
[0];
135 struct buffer_head
*bh
;
136 struct dx_entry
*entries
;
147 #ifdef CONFIG_EXT4_INDEX
148 static inline unsigned dx_get_block (struct dx_entry
*entry
);
149 static void dx_set_block (struct dx_entry
*entry
, unsigned value
);
150 static inline unsigned dx_get_hash (struct dx_entry
*entry
);
151 static void dx_set_hash (struct dx_entry
*entry
, unsigned value
);
152 static unsigned dx_get_count (struct dx_entry
*entries
);
153 static unsigned dx_get_limit (struct dx_entry
*entries
);
154 static void dx_set_count (struct dx_entry
*entries
, unsigned value
);
155 static void dx_set_limit (struct dx_entry
*entries
, unsigned value
);
156 static unsigned dx_root_limit (struct inode
*dir
, unsigned infosize
);
157 static unsigned dx_node_limit (struct inode
*dir
);
158 static struct dx_frame
*dx_probe(struct dentry
*dentry
,
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 ext4_dir_entry_2
*de
, int size
,
165 struct dx_hash_info
*hinfo
, struct dx_map_entry map
[]);
166 static void dx_sort_map(struct dx_map_entry
*map
, unsigned count
);
167 static struct ext4_dir_entry_2
*dx_move_dirents (char *from
, char *to
,
168 struct dx_map_entry
*offsets
, int count
);
169 static struct ext4_dir_entry_2
* dx_pack_dirents (char *base
, int size
);
170 static void dx_insert_block (struct dx_frame
*frame
, u32 hash
, u32 block
);
171 static int ext4_htree_next_block(struct inode
*dir
, __u32 hash
,
172 struct dx_frame
*frame
,
173 struct dx_frame
*frames
,
175 static struct buffer_head
* ext4_dx_find_entry(struct dentry
*dentry
,
176 struct ext4_dir_entry_2
**res_dir
, int *err
);
177 static int ext4_dx_add_entry(handle_t
*handle
, struct dentry
*dentry
,
178 struct inode
*inode
);
181 * Future: use high four bits of block for coalesce-on-delete flags
182 * Mask them off for now.
185 static inline unsigned dx_get_block (struct dx_entry
*entry
)
187 return le32_to_cpu(entry
->block
) & 0x00ffffff;
190 static inline void dx_set_block (struct dx_entry
*entry
, unsigned value
)
192 entry
->block
= cpu_to_le32(value
);
195 static inline unsigned dx_get_hash (struct dx_entry
*entry
)
197 return le32_to_cpu(entry
->hash
);
200 static inline void dx_set_hash (struct dx_entry
*entry
, unsigned value
)
202 entry
->hash
= cpu_to_le32(value
);
205 static inline unsigned dx_get_count (struct dx_entry
*entries
)
207 return le16_to_cpu(((struct dx_countlimit
*) entries
)->count
);
210 static inline unsigned dx_get_limit (struct dx_entry
*entries
)
212 return le16_to_cpu(((struct dx_countlimit
*) entries
)->limit
);
215 static inline void dx_set_count (struct dx_entry
*entries
, unsigned value
)
217 ((struct dx_countlimit
*) entries
)->count
= cpu_to_le16(value
);
220 static inline void dx_set_limit (struct dx_entry
*entries
, unsigned value
)
222 ((struct dx_countlimit
*) entries
)->limit
= cpu_to_le16(value
);
225 static inline unsigned dx_root_limit (struct inode
*dir
, unsigned infosize
)
227 unsigned entry_space
= dir
->i_sb
->s_blocksize
- EXT4_DIR_REC_LEN(1) -
228 EXT4_DIR_REC_LEN(2) - infosize
;
229 return 0? 20: entry_space
/ sizeof(struct dx_entry
);
232 static inline unsigned dx_node_limit (struct inode
*dir
)
234 unsigned entry_space
= dir
->i_sb
->s_blocksize
- EXT4_DIR_REC_LEN(0);
235 return 0? 22: entry_space
/ sizeof(struct dx_entry
);
242 static void dx_show_index (char * label
, struct dx_entry
*entries
)
244 int i
, n
= dx_get_count (entries
);
245 printk("%s index ", label
);
246 for (i
= 0; i
< n
; i
++) {
247 printk("%x->%u ", i
? dx_get_hash(entries
+ i
) :
248 0, dx_get_block(entries
+ i
));
260 static struct stats
dx_show_leaf(struct dx_hash_info
*hinfo
, struct ext4_dir_entry_2
*de
,
261 int size
, int show_names
)
263 unsigned names
= 0, space
= 0;
264 char *base
= (char *) de
;
265 struct dx_hash_info h
= *hinfo
;
268 while ((char *) de
< base
+ size
)
274 int len
= de
->name_len
;
275 char *name
= de
->name
;
276 while (len
--) printk("%c", *name
++);
277 ext4fs_dirhash(de
->name
, de
->name_len
, &h
);
278 printk(":%x.%u ", h
.hash
,
279 ((char *) de
- base
));
281 space
+= EXT4_DIR_REC_LEN(de
->name_len
);
284 de
= (struct ext4_dir_entry_2
*) ((char *) de
+ le16_to_cpu(de
->rec_len
));
286 printk("(%i)\n", names
);
287 return (struct stats
) { names
, space
, 1 };
290 struct stats
dx_show_entries(struct dx_hash_info
*hinfo
, struct inode
*dir
,
291 struct dx_entry
*entries
, int levels
)
293 unsigned blocksize
= dir
->i_sb
->s_blocksize
;
294 unsigned count
= dx_get_count (entries
), names
= 0, space
= 0, i
;
296 struct buffer_head
*bh
;
298 printk("%i indexed blocks...\n", count
);
299 for (i
= 0; i
< count
; i
++, entries
++)
301 u32 block
= dx_get_block(entries
), hash
= i
? dx_get_hash(entries
): 0;
302 u32 range
= i
< count
- 1? (dx_get_hash(entries
+ 1) - hash
): ~hash
;
304 printk("%s%3u:%03u hash %8x/%8x ",levels
?"":" ", i
, block
, hash
, range
);
305 if (!(bh
= ext4_bread (NULL
,dir
, block
, 0,&err
))) continue;
307 dx_show_entries(hinfo
, dir
, ((struct dx_node
*) bh
->b_data
)->entries
, levels
- 1):
308 dx_show_leaf(hinfo
, (struct ext4_dir_entry_2
*) bh
->b_data
, blocksize
, 0);
309 names
+= stats
.names
;
310 space
+= stats
.space
;
311 bcount
+= stats
.bcount
;
315 printk("%snames %u, fullness %u (%u%%)\n", levels
?"":" ",
316 names
, space
/bcount
,(space
/bcount
)*100/blocksize
);
317 return (struct stats
) { names
, space
, bcount
};
319 #endif /* DX_DEBUG */
322 * Probe for a directory leaf block to search.
324 * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
325 * error in the directory index, and the caller should fall back to
326 * searching the directory normally. The callers of dx_probe **MUST**
327 * check for this error code, and make sure it never gets reflected
330 static struct dx_frame
*
331 dx_probe(struct dentry
*dentry
, struct inode
*dir
,
332 struct dx_hash_info
*hinfo
, struct dx_frame
*frame_in
, int *err
)
334 unsigned count
, indirect
;
335 struct dx_entry
*at
, *entries
, *p
, *q
, *m
;
336 struct dx_root
*root
;
337 struct buffer_head
*bh
;
338 struct dx_frame
*frame
= frame_in
;
343 dir
= dentry
->d_parent
->d_inode
;
344 if (!(bh
= ext4_bread (NULL
,dir
, 0, 0, err
)))
346 root
= (struct dx_root
*) bh
->b_data
;
347 if (root
->info
.hash_version
!= DX_HASH_TEA
&&
348 root
->info
.hash_version
!= DX_HASH_HALF_MD4
&&
349 root
->info
.hash_version
!= DX_HASH_LEGACY
) {
350 ext4_warning(dir
->i_sb
, __FUNCTION__
,
351 "Unrecognised inode hash code %d",
352 root
->info
.hash_version
);
354 *err
= ERR_BAD_DX_DIR
;
357 hinfo
->hash_version
= root
->info
.hash_version
;
358 hinfo
->seed
= EXT4_SB(dir
->i_sb
)->s_hash_seed
;
360 ext4fs_dirhash(dentry
->d_name
.name
, dentry
->d_name
.len
, hinfo
);
363 if (root
->info
.unused_flags
& 1) {
364 ext4_warning(dir
->i_sb
, __FUNCTION__
,
365 "Unimplemented inode hash flags: %#06x",
366 root
->info
.unused_flags
);
368 *err
= ERR_BAD_DX_DIR
;
372 if ((indirect
= root
->info
.indirect_levels
) > 1) {
373 ext4_warning(dir
->i_sb
, __FUNCTION__
,
374 "Unimplemented inode hash depth: %#06x",
375 root
->info
.indirect_levels
);
377 *err
= ERR_BAD_DX_DIR
;
381 entries
= (struct dx_entry
*) (((char *)&root
->info
) +
382 root
->info
.info_length
);
384 if (dx_get_limit(entries
) != dx_root_limit(dir
,
385 root
->info
.info_length
)) {
386 ext4_warning(dir
->i_sb
, __FUNCTION__
,
387 "dx entry: limit != root limit");
389 *err
= ERR_BAD_DX_DIR
;
393 dxtrace (printk("Look up %x", hash
));
396 count
= dx_get_count(entries
);
397 if (!count
|| count
> dx_get_limit(entries
)) {
398 ext4_warning(dir
->i_sb
, __FUNCTION__
,
399 "dx entry: no count or count > limit");
401 *err
= ERR_BAD_DX_DIR
;
406 q
= entries
+ count
- 1;
410 dxtrace(printk("."));
411 if (dx_get_hash(m
) > hash
)
417 if (0) // linear search cross check
419 unsigned n
= count
- 1;
423 dxtrace(printk(","));
424 if (dx_get_hash(++at
) > hash
)
430 assert (at
== p
- 1);
434 dxtrace(printk(" %x->%u\n", at
== entries
? 0: dx_get_hash(at
), dx_get_block(at
)));
436 frame
->entries
= entries
;
438 if (!indirect
--) return frame
;
439 if (!(bh
= ext4_bread (NULL
,dir
, dx_get_block(at
), 0, err
)))
441 at
= entries
= ((struct dx_node
*) bh
->b_data
)->entries
;
442 if (dx_get_limit(entries
) != dx_node_limit (dir
)) {
443 ext4_warning(dir
->i_sb
, __FUNCTION__
,
444 "dx entry: limit != node limit");
446 *err
= ERR_BAD_DX_DIR
;
453 while (frame
>= frame_in
) {
458 if (*err
== ERR_BAD_DX_DIR
)
459 ext4_warning(dir
->i_sb
, __FUNCTION__
,
460 "Corrupt dir inode %ld, running e2fsck is "
461 "recommended.", dir
->i_ino
);
465 static void dx_release (struct dx_frame
*frames
)
467 if (frames
[0].bh
== NULL
)
470 if (((struct dx_root
*) frames
[0].bh
->b_data
)->info
.indirect_levels
)
471 brelse(frames
[1].bh
);
472 brelse(frames
[0].bh
);
476 * This function increments the frame pointer to search the next leaf
477 * block, and reads in the necessary intervening nodes if the search
478 * should be necessary. Whether or not the search is necessary is
479 * controlled by the hash parameter. If the hash value is even, then
480 * the search is only continued if the next block starts with that
481 * hash value. This is used if we are searching for a specific file.
483 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
485 * This function returns 1 if the caller should continue to search,
486 * or 0 if it should not. If there is an error reading one of the
487 * index blocks, it will a negative error code.
489 * If start_hash is non-null, it will be filled in with the starting
490 * hash of the next page.
492 static int ext4_htree_next_block(struct inode
*dir
, __u32 hash
,
493 struct dx_frame
*frame
,
494 struct dx_frame
*frames
,
498 struct buffer_head
*bh
;
499 int err
, num_frames
= 0;
504 * Find the next leaf page by incrementing the frame pointer.
505 * If we run out of entries in the interior node, loop around and
506 * increment pointer in the parent node. When we break out of
507 * this loop, num_frames indicates the number of interior
508 * nodes need to be read.
511 if (++(p
->at
) < p
->entries
+ dx_get_count(p
->entries
))
520 * If the hash is 1, then continue only if the next page has a
521 * continuation hash of any value. This is used for readdir
522 * handling. Otherwise, check to see if the hash matches the
523 * desired contiuation hash. If it doesn't, return since
524 * there's no point to read in the successive index pages.
526 bhash
= dx_get_hash(p
->at
);
529 if ((hash
& 1) == 0) {
530 if ((bhash
& ~1) != hash
)
534 * If the hash is HASH_NB_ALWAYS, we always go to the next
535 * block so no check is necessary
537 while (num_frames
--) {
538 if (!(bh
= ext4_bread(NULL
, dir
, dx_get_block(p
->at
),
540 return err
; /* Failure */
544 p
->at
= p
->entries
= ((struct dx_node
*) bh
->b_data
)->entries
;
551 * p is at least 6 bytes before the end of page
553 static inline struct ext4_dir_entry_2
*ext4_next_entry(struct ext4_dir_entry_2
*p
)
555 return (struct ext4_dir_entry_2
*)((char*)p
+ le16_to_cpu(p
->rec_len
));
559 * This function fills a red-black tree with information from a
560 * directory block. It returns the number directory entries loaded
561 * into the tree. If there is an error it is returned in err.
563 static int htree_dirblock_to_tree(struct file
*dir_file
,
564 struct inode
*dir
, int block
,
565 struct dx_hash_info
*hinfo
,
566 __u32 start_hash
, __u32 start_minor_hash
)
568 struct buffer_head
*bh
;
569 struct ext4_dir_entry_2
*de
, *top
;
572 dxtrace(printk("In htree dirblock_to_tree: block %d\n", block
));
573 if (!(bh
= ext4_bread (NULL
, dir
, block
, 0, &err
)))
576 de
= (struct ext4_dir_entry_2
*) bh
->b_data
;
577 top
= (struct ext4_dir_entry_2
*) ((char *) de
+
578 dir
->i_sb
->s_blocksize
-
579 EXT4_DIR_REC_LEN(0));
580 for (; de
< top
; de
= ext4_next_entry(de
)) {
581 if (!ext4_check_dir_entry("htree_dirblock_to_tree", dir
, de
, bh
,
582 (block
<<EXT4_BLOCK_SIZE_BITS(dir
->i_sb
))
583 +((char *)de
- bh
->b_data
))) {
584 /* On error, skip the f_pos to the next block. */
585 dir_file
->f_pos
= (dir_file
->f_pos
|
586 (dir
->i_sb
->s_blocksize
- 1)) + 1;
590 ext4fs_dirhash(de
->name
, de
->name_len
, hinfo
);
591 if ((hinfo
->hash
< start_hash
) ||
592 ((hinfo
->hash
== start_hash
) &&
593 (hinfo
->minor_hash
< start_minor_hash
)))
597 if ((err
= ext4_htree_store_dirent(dir_file
,
598 hinfo
->hash
, hinfo
->minor_hash
, de
)) != 0) {
610 * This function fills a red-black tree with information from a
611 * directory. We start scanning the directory in hash order, starting
612 * at start_hash and start_minor_hash.
614 * This function returns the number of entries inserted into the tree,
615 * or a negative error code.
617 int ext4_htree_fill_tree(struct file
*dir_file
, __u32 start_hash
,
618 __u32 start_minor_hash
, __u32
*next_hash
)
620 struct dx_hash_info hinfo
;
621 struct ext4_dir_entry_2
*de
;
622 struct dx_frame frames
[2], *frame
;
629 dxtrace(printk("In htree_fill_tree, start hash: %x:%x\n", start_hash
,
631 dir
= dir_file
->f_path
.dentry
->d_inode
;
632 if (!(EXT4_I(dir
)->i_flags
& EXT4_INDEX_FL
)) {
633 hinfo
.hash_version
= EXT4_SB(dir
->i_sb
)->s_def_hash_version
;
634 hinfo
.seed
= EXT4_SB(dir
->i_sb
)->s_hash_seed
;
635 count
= htree_dirblock_to_tree(dir_file
, dir
, 0, &hinfo
,
636 start_hash
, start_minor_hash
);
640 hinfo
.hash
= start_hash
;
641 hinfo
.minor_hash
= 0;
642 frame
= dx_probe(NULL
, dir_file
->f_path
.dentry
->d_inode
, &hinfo
, frames
, &err
);
646 /* Add '.' and '..' from the htree header */
647 if (!start_hash
&& !start_minor_hash
) {
648 de
= (struct ext4_dir_entry_2
*) frames
[0].bh
->b_data
;
649 if ((err
= ext4_htree_store_dirent(dir_file
, 0, 0, de
)) != 0)
653 if (start_hash
< 2 || (start_hash
==2 && start_minor_hash
==0)) {
654 de
= (struct ext4_dir_entry_2
*) frames
[0].bh
->b_data
;
655 de
= ext4_next_entry(de
);
656 if ((err
= ext4_htree_store_dirent(dir_file
, 2, 0, de
)) != 0)
662 block
= dx_get_block(frame
->at
);
663 ret
= htree_dirblock_to_tree(dir_file
, dir
, block
, &hinfo
,
664 start_hash
, start_minor_hash
);
671 ret
= ext4_htree_next_block(dir
, HASH_NB_ALWAYS
,
672 frame
, frames
, &hashval
);
673 *next_hash
= hashval
;
679 * Stop if: (a) there are no more entries, or
680 * (b) we have inserted at least one entry and the
681 * next hash value is not a continuation
684 (count
&& ((hashval
& 1) == 0)))
688 dxtrace(printk("Fill tree: returned %d entries, next hash: %x\n",
698 * Directory block splitting, compacting
702 * Create map of hash values, offsets, and sizes, stored at end of block.
703 * Returns number of entries mapped.
705 static int dx_make_map (struct ext4_dir_entry_2
*de
, int size
,
706 struct dx_hash_info
*hinfo
, struct dx_map_entry
*map_tail
)
709 char *base
= (char *) de
;
710 struct dx_hash_info h
= *hinfo
;
712 while ((char *) de
< base
+ size
)
714 if (de
->name_len
&& de
->inode
) {
715 ext4fs_dirhash(de
->name
, de
->name_len
, &h
);
717 map_tail
->hash
= h
.hash
;
718 map_tail
->offs
= (u16
) ((char *) de
- base
);
719 map_tail
->size
= le16_to_cpu(de
->rec_len
);
723 /* XXX: do we need to check rec_len == 0 case? -Chris */
724 de
= (struct ext4_dir_entry_2
*) ((char *) de
+ le16_to_cpu(de
->rec_len
));
729 /* Sort map by hash value */
730 static void dx_sort_map (struct dx_map_entry
*map
, unsigned count
)
732 struct dx_map_entry
*p
, *q
, *top
= map
+ count
- 1;
734 /* 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 */
748 if (q
[1].hash
>= q
[0].hash
)
756 static void dx_insert_block(struct dx_frame
*frame
, u32 hash
, u32 block
)
758 struct dx_entry
*entries
= frame
->entries
;
759 struct dx_entry
*old
= frame
->at
, *new = old
+ 1;
760 int count
= dx_get_count(entries
);
762 assert(count
< dx_get_limit(entries
));
763 assert(old
< entries
+ count
);
764 memmove(new + 1, new, (char *)(entries
+ count
) - (char *)(new));
765 dx_set_hash(new, hash
);
766 dx_set_block(new, block
);
767 dx_set_count(entries
, count
+ 1);
772 static void ext4_update_dx_flag(struct inode
*inode
)
774 if (!EXT4_HAS_COMPAT_FEATURE(inode
->i_sb
,
775 EXT4_FEATURE_COMPAT_DIR_INDEX
))
776 EXT4_I(inode
)->i_flags
&= ~EXT4_INDEX_FL
;
780 * NOTE! unlike strncmp, ext4_match returns 1 for success, 0 for failure.
782 * `len <= EXT4_NAME_LEN' is guaranteed by caller.
783 * `de != NULL' is guaranteed by caller.
785 static inline int ext4_match (int len
, const char * const name
,
786 struct ext4_dir_entry_2
* de
)
788 if (len
!= de
->name_len
)
792 return !memcmp(name
, de
->name
, len
);
796 * Returns 0 if not found, -1 on failure, and 1 on success
798 static inline int search_dirblock(struct buffer_head
* bh
,
800 struct dentry
*dentry
,
801 unsigned long offset
,
802 struct ext4_dir_entry_2
** res_dir
)
804 struct ext4_dir_entry_2
* de
;
807 const char *name
= dentry
->d_name
.name
;
808 int namelen
= dentry
->d_name
.len
;
810 de
= (struct ext4_dir_entry_2
*) bh
->b_data
;
811 dlimit
= bh
->b_data
+ dir
->i_sb
->s_blocksize
;
812 while ((char *) de
< dlimit
) {
813 /* this code is executed quadratically often */
814 /* do minimal checking `by hand' */
816 if ((char *) de
+ namelen
<= dlimit
&&
817 ext4_match (namelen
, name
, de
)) {
818 /* found a match - just to be sure, do a full check */
819 if (!ext4_check_dir_entry("ext4_find_entry",
820 dir
, de
, bh
, offset
))
825 /* prevent looping on a bad block */
826 de_len
= le16_to_cpu(de
->rec_len
);
830 de
= (struct ext4_dir_entry_2
*) ((char *) de
+ de_len
);
839 * finds an entry in the specified directory with the wanted name. It
840 * returns the cache buffer in which the entry was found, and the entry
841 * itself (as a parameter - res_dir). It does NOT read the inode of the
842 * entry - you'll have to do that yourself if you want to.
844 * The returned buffer_head has ->b_count elevated. The caller is expected
845 * to brelse() it when appropriate.
847 static struct buffer_head
* ext4_find_entry (struct dentry
*dentry
,
848 struct ext4_dir_entry_2
** res_dir
)
850 struct super_block
* sb
;
851 struct buffer_head
* bh_use
[NAMEI_RA_SIZE
];
852 struct buffer_head
* bh
, *ret
= NULL
;
853 unsigned long start
, block
, b
;
854 int ra_max
= 0; /* Number of bh's in the readahead
856 int ra_ptr
= 0; /* Current index into readahead
860 struct inode
*dir
= dentry
->d_parent
->d_inode
;
867 blocksize
= sb
->s_blocksize
;
868 namelen
= dentry
->d_name
.len
;
869 name
= dentry
->d_name
.name
;
870 if (namelen
> EXT4_NAME_LEN
)
872 #ifdef CONFIG_EXT4_INDEX
874 bh
= ext4_dx_find_entry(dentry
, res_dir
, &err
);
876 * On success, or if the error was file not found,
877 * return. Otherwise, fall back to doing a search the
880 if (bh
|| (err
!= ERR_BAD_DX_DIR
))
882 dxtrace(printk("ext4_find_entry: dx failed, falling back\n"));
885 nblocks
= dir
->i_size
>> EXT4_BLOCK_SIZE_BITS(sb
);
886 start
= EXT4_I(dir
)->i_dir_start_lookup
;
887 if (start
>= nblocks
)
893 * We deal with the read-ahead logic here.
895 if (ra_ptr
>= ra_max
) {
896 /* Refill the readahead buffer */
899 for (ra_max
= 0; ra_max
< NAMEI_RA_SIZE
; ra_max
++) {
901 * Terminate if we reach the end of the
902 * directory and must wrap, or if our
903 * search has finished at this block.
905 if (b
>= nblocks
|| (num
&& block
== start
)) {
906 bh_use
[ra_max
] = NULL
;
910 bh
= ext4_getblk(NULL
, dir
, b
++, 0, &err
);
913 ll_rw_block(READ_META
, 1, &bh
);
916 if ((bh
= bh_use
[ra_ptr
++]) == NULL
)
919 if (!buffer_uptodate(bh
)) {
920 /* read error, skip block & hope for the best */
921 ext4_error(sb
, __FUNCTION__
, "reading directory #%lu "
922 "offset %lu", dir
->i_ino
, block
);
926 i
= search_dirblock(bh
, dir
, dentry
,
927 block
<< EXT4_BLOCK_SIZE_BITS(sb
), res_dir
);
929 EXT4_I(dir
)->i_dir_start_lookup
= block
;
931 goto cleanup_and_exit
;
935 goto cleanup_and_exit
;
938 if (++block
>= nblocks
)
940 } while (block
!= start
);
943 * If the directory has grown while we were searching, then
944 * search the last part of the directory before giving up.
947 nblocks
= dir
->i_size
>> EXT4_BLOCK_SIZE_BITS(sb
);
948 if (block
< nblocks
) {
954 /* Clean up the read-ahead blocks */
955 for (; ra_ptr
< ra_max
; ra_ptr
++)
956 brelse (bh_use
[ra_ptr
]);
960 #ifdef CONFIG_EXT4_INDEX
961 static struct buffer_head
* ext4_dx_find_entry(struct dentry
*dentry
,
962 struct ext4_dir_entry_2
**res_dir
, int *err
)
964 struct super_block
* sb
;
965 struct dx_hash_info hinfo
;
967 struct dx_frame frames
[2], *frame
;
968 struct ext4_dir_entry_2
*de
, *top
;
969 struct buffer_head
*bh
;
972 int namelen
= dentry
->d_name
.len
;
973 const u8
*name
= dentry
->d_name
.name
;
974 struct inode
*dir
= dentry
->d_parent
->d_inode
;
977 /* NFS may look up ".." - look at dx_root directory block */
978 if (namelen
> 2 || name
[0] != '.'||(name
[1] != '.' && name
[1] != '\0')){
979 if (!(frame
= dx_probe(dentry
, NULL
, &hinfo
, frames
, err
)))
983 frame
->bh
= NULL
; /* for dx_release() */
984 frame
->at
= (struct dx_entry
*)frames
; /* hack for zero entry*/
985 dx_set_block(frame
->at
, 0); /* dx_root block is 0 */
989 block
= dx_get_block(frame
->at
);
990 if (!(bh
= ext4_bread (NULL
,dir
, block
, 0, err
)))
992 de
= (struct ext4_dir_entry_2
*) bh
->b_data
;
993 top
= (struct ext4_dir_entry_2
*) ((char *) de
+ sb
->s_blocksize
-
994 EXT4_DIR_REC_LEN(0));
995 for (; de
< top
; de
= ext4_next_entry(de
))
996 if (ext4_match (namelen
, name
, de
)) {
997 if (!ext4_check_dir_entry("ext4_find_entry",
999 (block
<<EXT4_BLOCK_SIZE_BITS(sb
))
1000 +((char *)de
- bh
->b_data
))) {
1002 *err
= ERR_BAD_DX_DIR
;
1006 dx_release (frames
);
1010 /* Check to see if we should continue to search */
1011 retval
= ext4_htree_next_block(dir
, hash
, frame
,
1014 ext4_warning(sb
, __FUNCTION__
,
1015 "error reading index page in directory #%lu",
1020 } while (retval
== 1);
1024 dxtrace(printk("%s not found\n", name
));
1025 dx_release (frames
);
1030 static struct dentry
*ext4_lookup(struct inode
* dir
, struct dentry
*dentry
, struct nameidata
*nd
)
1032 struct inode
* inode
;
1033 struct ext4_dir_entry_2
* de
;
1034 struct buffer_head
* bh
;
1036 if (dentry
->d_name
.len
> EXT4_NAME_LEN
)
1037 return ERR_PTR(-ENAMETOOLONG
);
1039 bh
= ext4_find_entry(dentry
, &de
);
1042 unsigned long ino
= le32_to_cpu(de
->inode
);
1044 if (!ext4_valid_inum(dir
->i_sb
, ino
)) {
1045 ext4_error(dir
->i_sb
, "ext4_lookup",
1046 "bad inode number: %lu", ino
);
1049 inode
= iget(dir
->i_sb
, ino
);
1052 return ERR_PTR(-EACCES
);
1054 if (is_bad_inode(inode
)) {
1056 return ERR_PTR(-ENOENT
);
1059 return d_splice_alias(inode
, dentry
);
1063 struct dentry
*ext4_get_parent(struct dentry
*child
)
1066 struct dentry
*parent
;
1067 struct inode
*inode
;
1068 struct dentry dotdot
;
1069 struct ext4_dir_entry_2
* de
;
1070 struct buffer_head
*bh
;
1072 dotdot
.d_name
.name
= "..";
1073 dotdot
.d_name
.len
= 2;
1074 dotdot
.d_parent
= child
; /* confusing, isn't it! */
1076 bh
= ext4_find_entry(&dotdot
, &de
);
1079 return ERR_PTR(-ENOENT
);
1080 ino
= le32_to_cpu(de
->inode
);
1083 if (!ext4_valid_inum(child
->d_inode
->i_sb
, ino
)) {
1084 ext4_error(child
->d_inode
->i_sb
, "ext4_get_parent",
1085 "bad inode number: %lu", ino
);
1088 inode
= iget(child
->d_inode
->i_sb
, ino
);
1091 return ERR_PTR(-EACCES
);
1093 if (is_bad_inode(inode
)) {
1095 return ERR_PTR(-ENOENT
);
1098 parent
= d_alloc_anon(inode
);
1101 parent
= ERR_PTR(-ENOMEM
);
1107 static unsigned char ext4_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
1108 [S_IFREG
>> S_SHIFT
] = EXT4_FT_REG_FILE
,
1109 [S_IFDIR
>> S_SHIFT
] = EXT4_FT_DIR
,
1110 [S_IFCHR
>> S_SHIFT
] = EXT4_FT_CHRDEV
,
1111 [S_IFBLK
>> S_SHIFT
] = EXT4_FT_BLKDEV
,
1112 [S_IFIFO
>> S_SHIFT
] = EXT4_FT_FIFO
,
1113 [S_IFSOCK
>> S_SHIFT
] = EXT4_FT_SOCK
,
1114 [S_IFLNK
>> S_SHIFT
] = EXT4_FT_SYMLINK
,
1117 static inline void ext4_set_de_type(struct super_block
*sb
,
1118 struct ext4_dir_entry_2
*de
,
1120 if (EXT4_HAS_INCOMPAT_FEATURE(sb
, EXT4_FEATURE_INCOMPAT_FILETYPE
))
1121 de
->file_type
= ext4_type_by_mode
[(mode
& S_IFMT
)>>S_SHIFT
];
1124 #ifdef CONFIG_EXT4_INDEX
1126 * Move count entries from end of map between two memory locations.
1127 * Returns pointer to last entry moved.
1129 static struct ext4_dir_entry_2
*
1130 dx_move_dirents(char *from
, char *to
, struct dx_map_entry
*map
, int count
)
1132 unsigned rec_len
= 0;
1135 struct ext4_dir_entry_2
*de
= (struct ext4_dir_entry_2
*) (from
+ map
->offs
);
1136 rec_len
= EXT4_DIR_REC_LEN(de
->name_len
);
1137 memcpy (to
, de
, rec_len
);
1138 ((struct ext4_dir_entry_2
*) to
)->rec_len
=
1139 cpu_to_le16(rec_len
);
1144 return (struct ext4_dir_entry_2
*) (to
- rec_len
);
1148 * Compact each dir entry in the range to the minimal rec_len.
1149 * Returns pointer to last entry in range.
1151 static struct ext4_dir_entry_2
* dx_pack_dirents(char *base
, int size
)
1153 struct ext4_dir_entry_2
*next
, *to
, *prev
, *de
= (struct ext4_dir_entry_2
*) base
;
1154 unsigned rec_len
= 0;
1157 while ((char*)de
< base
+ size
) {
1158 next
= (struct ext4_dir_entry_2
*) ((char *) de
+
1159 le16_to_cpu(de
->rec_len
));
1160 if (de
->inode
&& de
->name_len
) {
1161 rec_len
= EXT4_DIR_REC_LEN(de
->name_len
);
1163 memmove(to
, de
, rec_len
);
1164 to
->rec_len
= cpu_to_le16(rec_len
);
1166 to
= (struct ext4_dir_entry_2
*) (((char *) to
) + rec_len
);
1174 * Split a full leaf block to make room for a new dir entry.
1175 * Allocate a new block, and move entries so that they are approx. equally full.
1176 * Returns pointer to de in block into which the new entry will be inserted.
1178 static struct ext4_dir_entry_2
*do_split(handle_t
*handle
, struct inode
*dir
,
1179 struct buffer_head
**bh
,struct dx_frame
*frame
,
1180 struct dx_hash_info
*hinfo
, int *error
)
1182 unsigned blocksize
= dir
->i_sb
->s_blocksize
;
1183 unsigned count
, continued
;
1184 struct buffer_head
*bh2
;
1187 struct dx_map_entry
*map
;
1188 char *data1
= (*bh
)->b_data
, *data2
;
1189 unsigned split
, move
, size
, i
;
1190 struct ext4_dir_entry_2
*de
= NULL
, *de2
;
1193 bh2
= ext4_append (handle
, dir
, &newblock
, &err
);
1200 BUFFER_TRACE(*bh
, "get_write_access");
1201 err
= ext4_journal_get_write_access(handle
, *bh
);
1205 BUFFER_TRACE(frame
->bh
, "get_write_access");
1206 err
= ext4_journal_get_write_access(handle
, frame
->bh
);
1210 data2
= bh2
->b_data
;
1212 /* create map in the end of data2 block */
1213 map
= (struct dx_map_entry
*) (data2
+ blocksize
);
1214 count
= dx_make_map ((struct ext4_dir_entry_2
*) data1
,
1215 blocksize
, hinfo
, map
);
1217 dx_sort_map (map
, count
);
1218 /* Split the existing block in the middle, size-wise */
1221 for (i
= count
-1; i
>= 0; i
--) {
1222 /* is more than half of this entry in 2nd half of the block? */
1223 if (size
+ map
[i
].size
/2 > blocksize
/2)
1225 size
+= map
[i
].size
;
1228 /* map index at which we will split */
1229 split
= count
- move
;
1230 hash2
= map
[split
].hash
;
1231 continued
= hash2
== map
[split
- 1].hash
;
1232 dxtrace(printk("Split block %i at %x, %i/%i\n",
1233 dx_get_block(frame
->at
), hash2
, split
, count
-split
));
1235 /* Fancy dance to stay within two buffers */
1236 de2
= dx_move_dirents(data1
, data2
, map
+ split
, count
- split
);
1237 de
= dx_pack_dirents(data1
,blocksize
);
1238 de
->rec_len
= cpu_to_le16(data1
+ blocksize
- (char *) de
);
1239 de2
->rec_len
= cpu_to_le16(data2
+ blocksize
- (char *) de2
);
1240 dxtrace(dx_show_leaf (hinfo
, (struct ext4_dir_entry_2
*) data1
, blocksize
, 1));
1241 dxtrace(dx_show_leaf (hinfo
, (struct ext4_dir_entry_2
*) data2
, blocksize
, 1));
1243 /* Which block gets the new entry? */
1244 if (hinfo
->hash
>= hash2
)
1249 dx_insert_block (frame
, hash2
+ continued
, newblock
);
1250 err
= ext4_journal_dirty_metadata (handle
, bh2
);
1253 err
= ext4_journal_dirty_metadata (handle
, frame
->bh
);
1257 dxtrace(dx_show_index ("frame", frame
->entries
));
1264 ext4_std_error(dir
->i_sb
, err
);
1273 * Add a new entry into a directory (leaf) block. If de is non-NULL,
1274 * it points to a directory entry which is guaranteed to be large
1275 * enough for new directory entry. If de is NULL, then
1276 * add_dirent_to_buf will attempt search the directory block for
1277 * space. It will return -ENOSPC if no space is available, and -EIO
1278 * and -EEXIST if directory entry already exists.
1280 * NOTE! bh is NOT released in the case where ENOSPC is returned. In
1281 * all other cases bh is released.
1283 static int add_dirent_to_buf(handle_t
*handle
, struct dentry
*dentry
,
1284 struct inode
*inode
, struct ext4_dir_entry_2
*de
,
1285 struct buffer_head
* bh
)
1287 struct inode
*dir
= dentry
->d_parent
->d_inode
;
1288 const char *name
= dentry
->d_name
.name
;
1289 int namelen
= dentry
->d_name
.len
;
1290 unsigned long offset
= 0;
1291 unsigned short reclen
;
1292 int nlen
, rlen
, err
;
1295 reclen
= EXT4_DIR_REC_LEN(namelen
);
1297 de
= (struct ext4_dir_entry_2
*)bh
->b_data
;
1298 top
= bh
->b_data
+ dir
->i_sb
->s_blocksize
- reclen
;
1299 while ((char *) de
<= top
) {
1300 if (!ext4_check_dir_entry("ext4_add_entry", dir
, de
,
1305 if (ext4_match (namelen
, name
, de
)) {
1309 nlen
= EXT4_DIR_REC_LEN(de
->name_len
);
1310 rlen
= le16_to_cpu(de
->rec_len
);
1311 if ((de
->inode
? rlen
- nlen
: rlen
) >= reclen
)
1313 de
= (struct ext4_dir_entry_2
*)((char *)de
+ rlen
);
1316 if ((char *) de
> top
)
1319 BUFFER_TRACE(bh
, "get_write_access");
1320 err
= ext4_journal_get_write_access(handle
, bh
);
1322 ext4_std_error(dir
->i_sb
, err
);
1327 /* By now the buffer is marked for journaling */
1328 nlen
= EXT4_DIR_REC_LEN(de
->name_len
);
1329 rlen
= le16_to_cpu(de
->rec_len
);
1331 struct ext4_dir_entry_2
*de1
= (struct ext4_dir_entry_2
*)((char *)de
+ nlen
);
1332 de1
->rec_len
= cpu_to_le16(rlen
- nlen
);
1333 de
->rec_len
= cpu_to_le16(nlen
);
1336 de
->file_type
= EXT4_FT_UNKNOWN
;
1338 de
->inode
= cpu_to_le32(inode
->i_ino
);
1339 ext4_set_de_type(dir
->i_sb
, de
, inode
->i_mode
);
1342 de
->name_len
= namelen
;
1343 memcpy (de
->name
, name
, namelen
);
1345 * XXX shouldn't update any times until successful
1346 * completion of syscall, but too many callers depend
1349 * XXX similarly, too many callers depend on
1350 * ext4_new_inode() setting the times, but error
1351 * recovery deletes the inode, so the worst that can
1352 * happen is that the times are slightly out of date
1353 * and/or different from the directory change time.
1355 dir
->i_mtime
= dir
->i_ctime
= ext4_current_time(dir
);
1356 ext4_update_dx_flag(dir
);
1358 ext4_mark_inode_dirty(handle
, dir
);
1359 BUFFER_TRACE(bh
, "call ext4_journal_dirty_metadata");
1360 err
= ext4_journal_dirty_metadata(handle
, bh
);
1362 ext4_std_error(dir
->i_sb
, err
);
1367 #ifdef CONFIG_EXT4_INDEX
1369 * This converts a one block unindexed directory to a 3 block indexed
1370 * directory, and adds the dentry to the indexed directory.
1372 static int make_indexed_dir(handle_t
*handle
, struct dentry
*dentry
,
1373 struct inode
*inode
, struct buffer_head
*bh
)
1375 struct inode
*dir
= dentry
->d_parent
->d_inode
;
1376 const char *name
= dentry
->d_name
.name
;
1377 int namelen
= dentry
->d_name
.len
;
1378 struct buffer_head
*bh2
;
1379 struct dx_root
*root
;
1380 struct dx_frame frames
[2], *frame
;
1381 struct dx_entry
*entries
;
1382 struct ext4_dir_entry_2
*de
, *de2
;
1387 struct dx_hash_info hinfo
;
1389 struct fake_dirent
*fde
;
1391 blocksize
= dir
->i_sb
->s_blocksize
;
1392 dxtrace(printk("Creating index\n"));
1393 retval
= ext4_journal_get_write_access(handle
, bh
);
1395 ext4_std_error(dir
->i_sb
, retval
);
1399 root
= (struct dx_root
*) bh
->b_data
;
1401 bh2
= ext4_append (handle
, dir
, &block
, &retval
);
1406 EXT4_I(dir
)->i_flags
|= EXT4_INDEX_FL
;
1407 data1
= bh2
->b_data
;
1409 /* The 0th block becomes the root, move the dirents out */
1410 fde
= &root
->dotdot
;
1411 de
= (struct ext4_dir_entry_2
*)((char *)fde
+ le16_to_cpu(fde
->rec_len
));
1412 len
= ((char *) root
) + blocksize
- (char *) de
;
1413 memcpy (data1
, de
, len
);
1414 de
= (struct ext4_dir_entry_2
*) data1
;
1416 while ((char *)(de2
=(void*)de
+le16_to_cpu(de
->rec_len
)) < top
)
1418 de
->rec_len
= cpu_to_le16(data1
+ blocksize
- (char *) de
);
1419 /* Initialize the root; the dot dirents already exist */
1420 de
= (struct ext4_dir_entry_2
*) (&root
->dotdot
);
1421 de
->rec_len
= cpu_to_le16(blocksize
- EXT4_DIR_REC_LEN(2));
1422 memset (&root
->info
, 0, sizeof(root
->info
));
1423 root
->info
.info_length
= sizeof(root
->info
);
1424 root
->info
.hash_version
= EXT4_SB(dir
->i_sb
)->s_def_hash_version
;
1425 entries
= root
->entries
;
1426 dx_set_block (entries
, 1);
1427 dx_set_count (entries
, 1);
1428 dx_set_limit (entries
, dx_root_limit(dir
, sizeof(root
->info
)));
1430 /* Initialize as for dx_probe */
1431 hinfo
.hash_version
= root
->info
.hash_version
;
1432 hinfo
.seed
= EXT4_SB(dir
->i_sb
)->s_hash_seed
;
1433 ext4fs_dirhash(name
, namelen
, &hinfo
);
1435 frame
->entries
= entries
;
1436 frame
->at
= entries
;
1439 de
= do_split(handle
,dir
, &bh
, frame
, &hinfo
, &retval
);
1440 dx_release (frames
);
1444 return add_dirent_to_buf(handle
, dentry
, inode
, de
, bh
);
1451 * adds a file entry to the specified directory, using the same
1452 * semantics as ext4_find_entry(). It returns NULL if it failed.
1454 * NOTE!! The inode part of 'de' is left at 0 - which means you
1455 * may not sleep between calling this and putting something into
1456 * the entry, as someone else might have used it while you slept.
1458 static int ext4_add_entry (handle_t
*handle
, struct dentry
*dentry
,
1459 struct inode
*inode
)
1461 struct inode
*dir
= dentry
->d_parent
->d_inode
;
1462 unsigned long offset
;
1463 struct buffer_head
* bh
;
1464 struct ext4_dir_entry_2
*de
;
1465 struct super_block
* sb
;
1467 #ifdef CONFIG_EXT4_INDEX
1474 blocksize
= sb
->s_blocksize
;
1475 if (!dentry
->d_name
.len
)
1477 #ifdef CONFIG_EXT4_INDEX
1479 retval
= ext4_dx_add_entry(handle
, dentry
, inode
);
1480 if (!retval
|| (retval
!= ERR_BAD_DX_DIR
))
1482 EXT4_I(dir
)->i_flags
&= ~EXT4_INDEX_FL
;
1484 ext4_mark_inode_dirty(handle
, dir
);
1487 blocks
= dir
->i_size
>> sb
->s_blocksize_bits
;
1488 for (block
= 0, offset
= 0; block
< blocks
; block
++) {
1489 bh
= ext4_bread(handle
, dir
, block
, 0, &retval
);
1492 retval
= add_dirent_to_buf(handle
, dentry
, inode
, NULL
, bh
);
1493 if (retval
!= -ENOSPC
)
1496 #ifdef CONFIG_EXT4_INDEX
1497 if (blocks
== 1 && !dx_fallback
&&
1498 EXT4_HAS_COMPAT_FEATURE(sb
, EXT4_FEATURE_COMPAT_DIR_INDEX
))
1499 return make_indexed_dir(handle
, dentry
, inode
, bh
);
1503 bh
= ext4_append(handle
, dir
, &block
, &retval
);
1506 de
= (struct ext4_dir_entry_2
*) bh
->b_data
;
1508 de
->rec_len
= cpu_to_le16(blocksize
);
1509 return add_dirent_to_buf(handle
, dentry
, inode
, de
, bh
);
1512 #ifdef CONFIG_EXT4_INDEX
1514 * Returns 0 for success, or a negative error value
1516 static int ext4_dx_add_entry(handle_t
*handle
, struct dentry
*dentry
,
1517 struct inode
*inode
)
1519 struct dx_frame frames
[2], *frame
;
1520 struct dx_entry
*entries
, *at
;
1521 struct dx_hash_info hinfo
;
1522 struct buffer_head
* bh
;
1523 struct inode
*dir
= dentry
->d_parent
->d_inode
;
1524 struct super_block
* sb
= dir
->i_sb
;
1525 struct ext4_dir_entry_2
*de
;
1528 frame
= dx_probe(dentry
, NULL
, &hinfo
, frames
, &err
);
1531 entries
= frame
->entries
;
1534 if (!(bh
= ext4_bread(handle
,dir
, dx_get_block(frame
->at
), 0, &err
)))
1537 BUFFER_TRACE(bh
, "get_write_access");
1538 err
= ext4_journal_get_write_access(handle
, bh
);
1542 err
= add_dirent_to_buf(handle
, dentry
, inode
, NULL
, bh
);
1543 if (err
!= -ENOSPC
) {
1548 /* Block full, should compress but for now just split */
1549 dxtrace(printk("using %u of %u node entries\n",
1550 dx_get_count(entries
), dx_get_limit(entries
)));
1551 /* Need to split index? */
1552 if (dx_get_count(entries
) == dx_get_limit(entries
)) {
1554 unsigned icount
= dx_get_count(entries
);
1555 int levels
= frame
- frames
;
1556 struct dx_entry
*entries2
;
1557 struct dx_node
*node2
;
1558 struct buffer_head
*bh2
;
1560 if (levels
&& (dx_get_count(frames
->entries
) ==
1561 dx_get_limit(frames
->entries
))) {
1562 ext4_warning(sb
, __FUNCTION__
,
1563 "Directory index full!");
1567 bh2
= ext4_append (handle
, dir
, &newblock
, &err
);
1570 node2
= (struct dx_node
*)(bh2
->b_data
);
1571 entries2
= node2
->entries
;
1572 node2
->fake
.rec_len
= cpu_to_le16(sb
->s_blocksize
);
1573 node2
->fake
.inode
= 0;
1574 BUFFER_TRACE(frame
->bh
, "get_write_access");
1575 err
= ext4_journal_get_write_access(handle
, frame
->bh
);
1579 unsigned icount1
= icount
/2, icount2
= icount
- icount1
;
1580 unsigned hash2
= dx_get_hash(entries
+ icount1
);
1581 dxtrace(printk("Split index %i/%i\n", icount1
, icount2
));
1583 BUFFER_TRACE(frame
->bh
, "get_write_access"); /* index root */
1584 err
= ext4_journal_get_write_access(handle
,
1589 memcpy ((char *) entries2
, (char *) (entries
+ icount1
),
1590 icount2
* sizeof(struct dx_entry
));
1591 dx_set_count (entries
, icount1
);
1592 dx_set_count (entries2
, icount2
);
1593 dx_set_limit (entries2
, dx_node_limit(dir
));
1595 /* Which index block gets the new entry? */
1596 if (at
- entries
>= icount1
) {
1597 frame
->at
= at
= at
- entries
- icount1
+ entries2
;
1598 frame
->entries
= entries
= entries2
;
1599 swap(frame
->bh
, bh2
);
1601 dx_insert_block (frames
+ 0, hash2
, newblock
);
1602 dxtrace(dx_show_index ("node", frames
[1].entries
));
1603 dxtrace(dx_show_index ("node",
1604 ((struct dx_node
*) bh2
->b_data
)->entries
));
1605 err
= ext4_journal_dirty_metadata(handle
, bh2
);
1610 dxtrace(printk("Creating second level index...\n"));
1611 memcpy((char *) entries2
, (char *) entries
,
1612 icount
* sizeof(struct dx_entry
));
1613 dx_set_limit(entries2
, dx_node_limit(dir
));
1616 dx_set_count(entries
, 1);
1617 dx_set_block(entries
+ 0, newblock
);
1618 ((struct dx_root
*) frames
[0].bh
->b_data
)->info
.indirect_levels
= 1;
1620 /* Add new access path frame */
1622 frame
->at
= at
= at
- entries
+ entries2
;
1623 frame
->entries
= entries
= entries2
;
1625 err
= ext4_journal_get_write_access(handle
,
1630 ext4_journal_dirty_metadata(handle
, frames
[0].bh
);
1632 de
= do_split(handle
, dir
, &bh
, frame
, &hinfo
, &err
);
1635 err
= add_dirent_to_buf(handle
, dentry
, inode
, de
, bh
);
1640 ext4_std_error(dir
->i_sb
, err
);
1650 * ext4_delete_entry deletes a directory entry by merging it with the
1653 static int ext4_delete_entry (handle_t
*handle
,
1655 struct ext4_dir_entry_2
* de_del
,
1656 struct buffer_head
* bh
)
1658 struct ext4_dir_entry_2
* de
, * pde
;
1663 de
= (struct ext4_dir_entry_2
*) bh
->b_data
;
1664 while (i
< bh
->b_size
) {
1665 if (!ext4_check_dir_entry("ext4_delete_entry", dir
, de
, bh
, i
))
1668 BUFFER_TRACE(bh
, "get_write_access");
1669 ext4_journal_get_write_access(handle
, bh
);
1672 cpu_to_le16(le16_to_cpu(pde
->rec_len
) +
1673 le16_to_cpu(de
->rec_len
));
1677 BUFFER_TRACE(bh
, "call ext4_journal_dirty_metadata");
1678 ext4_journal_dirty_metadata(handle
, bh
);
1681 i
+= le16_to_cpu(de
->rec_len
);
1683 de
= (struct ext4_dir_entry_2
*)
1684 ((char *) de
+ le16_to_cpu(de
->rec_len
));
1690 * DIR_NLINK feature is set if 1) nlinks > EXT4_LINK_MAX or 2) nlinks == 2,
1691 * since this indicates that nlinks count was previously 1.
1693 static void ext4_inc_count(handle_t
*handle
, struct inode
*inode
)
1696 if (is_dx(inode
) && inode
->i_nlink
> 1) {
1697 /* limit is 16-bit i_links_count */
1698 if (inode
->i_nlink
>= EXT4_LINK_MAX
|| inode
->i_nlink
== 2) {
1700 EXT4_SET_RO_COMPAT_FEATURE(inode
->i_sb
,
1701 EXT4_FEATURE_RO_COMPAT_DIR_NLINK
);
1707 * If a directory had nlink == 1, then we should let it be 1. This indicates
1708 * directory has >EXT4_LINK_MAX subdirs.
1710 static void ext4_dec_count(handle_t
*handle
, struct inode
*inode
)
1713 if (S_ISDIR(inode
->i_mode
) && inode
->i_nlink
== 0)
1718 static int ext4_add_nondir(handle_t
*handle
,
1719 struct dentry
*dentry
, struct inode
*inode
)
1721 int err
= ext4_add_entry(handle
, dentry
, inode
);
1723 ext4_mark_inode_dirty(handle
, inode
);
1724 d_instantiate(dentry
, inode
);
1733 * By the time this is called, we already have created
1734 * the directory cache entry for the new file, but it
1735 * is so far negative - it has no inode.
1737 * If the create succeeds, we fill in the inode information
1738 * with d_instantiate().
1740 static int ext4_create (struct inode
* dir
, struct dentry
* dentry
, int mode
,
1741 struct nameidata
*nd
)
1744 struct inode
* inode
;
1745 int err
, retries
= 0;
1748 handle
= ext4_journal_start(dir
, EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
1749 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 3 +
1750 2*EXT4_QUOTA_INIT_BLOCKS(dir
->i_sb
));
1752 return PTR_ERR(handle
);
1754 if (IS_DIRSYNC(dir
))
1757 inode
= ext4_new_inode (handle
, dir
, mode
);
1758 err
= PTR_ERR(inode
);
1759 if (!IS_ERR(inode
)) {
1760 inode
->i_op
= &ext4_file_inode_operations
;
1761 inode
->i_fop
= &ext4_file_operations
;
1762 ext4_set_aops(inode
);
1763 err
= ext4_add_nondir(handle
, dentry
, inode
);
1765 ext4_journal_stop(handle
);
1766 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
1771 static int ext4_mknod (struct inode
* dir
, struct dentry
*dentry
,
1772 int mode
, dev_t rdev
)
1775 struct inode
*inode
;
1776 int err
, retries
= 0;
1778 if (!new_valid_dev(rdev
))
1782 handle
= ext4_journal_start(dir
, EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
1783 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 3 +
1784 2*EXT4_QUOTA_INIT_BLOCKS(dir
->i_sb
));
1786 return PTR_ERR(handle
);
1788 if (IS_DIRSYNC(dir
))
1791 inode
= ext4_new_inode (handle
, dir
, mode
);
1792 err
= PTR_ERR(inode
);
1793 if (!IS_ERR(inode
)) {
1794 init_special_inode(inode
, inode
->i_mode
, rdev
);
1795 #ifdef CONFIG_EXT4DEV_FS_XATTR
1796 inode
->i_op
= &ext4_special_inode_operations
;
1798 err
= ext4_add_nondir(handle
, dentry
, inode
);
1800 ext4_journal_stop(handle
);
1801 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
1806 static int ext4_mkdir(struct inode
* dir
, struct dentry
* dentry
, int mode
)
1809 struct inode
* inode
;
1810 struct buffer_head
* dir_block
;
1811 struct ext4_dir_entry_2
* de
;
1812 int err
, retries
= 0;
1814 if (EXT4_DIR_LINK_MAX(dir
))
1818 handle
= ext4_journal_start(dir
, EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
1819 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 3 +
1820 2*EXT4_QUOTA_INIT_BLOCKS(dir
->i_sb
));
1822 return PTR_ERR(handle
);
1824 if (IS_DIRSYNC(dir
))
1827 inode
= ext4_new_inode (handle
, dir
, S_IFDIR
| mode
);
1828 err
= PTR_ERR(inode
);
1832 inode
->i_op
= &ext4_dir_inode_operations
;
1833 inode
->i_fop
= &ext4_dir_operations
;
1834 inode
->i_size
= EXT4_I(inode
)->i_disksize
= inode
->i_sb
->s_blocksize
;
1835 dir_block
= ext4_bread (handle
, inode
, 0, 1, &err
);
1837 ext4_dec_count(handle
, inode
); /* is this nlink == 0? */
1838 ext4_mark_inode_dirty(handle
, inode
);
1842 BUFFER_TRACE(dir_block
, "get_write_access");
1843 ext4_journal_get_write_access(handle
, dir_block
);
1844 de
= (struct ext4_dir_entry_2
*) dir_block
->b_data
;
1845 de
->inode
= cpu_to_le32(inode
->i_ino
);
1847 de
->rec_len
= cpu_to_le16(EXT4_DIR_REC_LEN(de
->name_len
));
1848 strcpy (de
->name
, ".");
1849 ext4_set_de_type(dir
->i_sb
, de
, S_IFDIR
);
1850 de
= (struct ext4_dir_entry_2
*)
1851 ((char *) de
+ le16_to_cpu(de
->rec_len
));
1852 de
->inode
= cpu_to_le32(dir
->i_ino
);
1853 de
->rec_len
= cpu_to_le16(inode
->i_sb
->s_blocksize
-EXT4_DIR_REC_LEN(1));
1855 strcpy (de
->name
, "..");
1856 ext4_set_de_type(dir
->i_sb
, de
, S_IFDIR
);
1858 BUFFER_TRACE(dir_block
, "call ext4_journal_dirty_metadata");
1859 ext4_journal_dirty_metadata(handle
, dir_block
);
1861 ext4_mark_inode_dirty(handle
, inode
);
1862 err
= ext4_add_entry (handle
, dentry
, inode
);
1865 ext4_mark_inode_dirty(handle
, inode
);
1869 ext4_inc_count(handle
, dir
);
1870 ext4_update_dx_flag(dir
);
1871 ext4_mark_inode_dirty(handle
, dir
);
1872 d_instantiate(dentry
, inode
);
1874 ext4_journal_stop(handle
);
1875 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
1881 * routine to check that the specified directory is empty (for rmdir)
1883 static int empty_dir (struct inode
* inode
)
1885 unsigned long offset
;
1886 struct buffer_head
* bh
;
1887 struct ext4_dir_entry_2
* de
, * de1
;
1888 struct super_block
* sb
;
1892 if (inode
->i_size
< EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2) ||
1893 !(bh
= ext4_bread (NULL
, inode
, 0, 0, &err
))) {
1895 ext4_error(inode
->i_sb
, __FUNCTION__
,
1896 "error %d reading directory #%lu offset 0",
1899 ext4_warning(inode
->i_sb
, __FUNCTION__
,
1900 "bad directory (dir #%lu) - no data block",
1904 de
= (struct ext4_dir_entry_2
*) bh
->b_data
;
1905 de1
= (struct ext4_dir_entry_2
*)
1906 ((char *) de
+ le16_to_cpu(de
->rec_len
));
1907 if (le32_to_cpu(de
->inode
) != inode
->i_ino
||
1908 !le32_to_cpu(de1
->inode
) ||
1909 strcmp (".", de
->name
) ||
1910 strcmp ("..", de1
->name
)) {
1911 ext4_warning (inode
->i_sb
, "empty_dir",
1912 "bad directory (dir #%lu) - no `.' or `..'",
1917 offset
= le16_to_cpu(de
->rec_len
) + le16_to_cpu(de1
->rec_len
);
1918 de
= (struct ext4_dir_entry_2
*)
1919 ((char *) de1
+ le16_to_cpu(de1
->rec_len
));
1920 while (offset
< inode
->i_size
) {
1922 (void *) de
>= (void *) (bh
->b_data
+sb
->s_blocksize
)) {
1925 bh
= ext4_bread (NULL
, inode
,
1926 offset
>> EXT4_BLOCK_SIZE_BITS(sb
), 0, &err
);
1929 ext4_error(sb
, __FUNCTION__
,
1930 "error %d reading directory"
1932 err
, inode
->i_ino
, offset
);
1933 offset
+= sb
->s_blocksize
;
1936 de
= (struct ext4_dir_entry_2
*) bh
->b_data
;
1938 if (!ext4_check_dir_entry("empty_dir", inode
, de
, bh
, offset
)) {
1939 de
= (struct ext4_dir_entry_2
*)(bh
->b_data
+
1941 offset
= (offset
| (sb
->s_blocksize
- 1)) + 1;
1944 if (le32_to_cpu(de
->inode
)) {
1948 offset
+= le16_to_cpu(de
->rec_len
);
1949 de
= (struct ext4_dir_entry_2
*)
1950 ((char *) de
+ le16_to_cpu(de
->rec_len
));
1956 /* ext4_orphan_add() links an unlinked or truncated inode into a list of
1957 * such inodes, starting at the superblock, in case we crash before the
1958 * file is closed/deleted, or in case the inode truncate spans multiple
1959 * transactions and the last transaction is not recovered after a crash.
1961 * At filesystem recovery time, we walk this list deleting unlinked
1962 * inodes and truncating linked inodes in ext4_orphan_cleanup().
1964 int ext4_orphan_add(handle_t
*handle
, struct inode
*inode
)
1966 struct super_block
*sb
= inode
->i_sb
;
1967 struct ext4_iloc iloc
;
1971 if (!list_empty(&EXT4_I(inode
)->i_orphan
))
1974 /* Orphan handling is only valid for files with data blocks
1975 * being truncated, or files being unlinked. */
1977 /* @@@ FIXME: Observation from aviro:
1978 * I think I can trigger J_ASSERT in ext4_orphan_add(). We block
1979 * here (on lock_super()), so race with ext4_link() which might bump
1980 * ->i_nlink. For, say it, character device. Not a regular file,
1981 * not a directory, not a symlink and ->i_nlink > 0.
1983 J_ASSERT ((S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
1984 S_ISLNK(inode
->i_mode
)) || inode
->i_nlink
== 0);
1986 BUFFER_TRACE(EXT4_SB(sb
)->s_sbh
, "get_write_access");
1987 err
= ext4_journal_get_write_access(handle
, EXT4_SB(sb
)->s_sbh
);
1991 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
1995 /* Insert this inode at the head of the on-disk orphan list... */
1996 NEXT_ORPHAN(inode
) = le32_to_cpu(EXT4_SB(sb
)->s_es
->s_last_orphan
);
1997 EXT4_SB(sb
)->s_es
->s_last_orphan
= cpu_to_le32(inode
->i_ino
);
1998 err
= ext4_journal_dirty_metadata(handle
, EXT4_SB(sb
)->s_sbh
);
1999 rc
= ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
2003 /* Only add to the head of the in-memory list if all the
2004 * previous operations succeeded. If the orphan_add is going to
2005 * fail (possibly taking the journal offline), we can't risk
2006 * leaving the inode on the orphan list: stray orphan-list
2007 * entries can cause panics at unmount time.
2009 * This is safe: on error we're going to ignore the orphan list
2010 * anyway on the next recovery. */
2012 list_add(&EXT4_I(inode
)->i_orphan
, &EXT4_SB(sb
)->s_orphan
);
2014 jbd_debug(4, "superblock will point to %lu\n", inode
->i_ino
);
2015 jbd_debug(4, "orphan inode %lu will point to %d\n",
2016 inode
->i_ino
, NEXT_ORPHAN(inode
));
2019 ext4_std_error(inode
->i_sb
, err
);
2024 * ext4_orphan_del() removes an unlinked or truncated inode from the list
2025 * of such inodes stored on disk, because it is finally being cleaned up.
2027 int ext4_orphan_del(handle_t
*handle
, struct inode
*inode
)
2029 struct list_head
*prev
;
2030 struct ext4_inode_info
*ei
= EXT4_I(inode
);
2031 struct ext4_sb_info
*sbi
;
2032 unsigned long ino_next
;
2033 struct ext4_iloc iloc
;
2036 lock_super(inode
->i_sb
);
2037 if (list_empty(&ei
->i_orphan
)) {
2038 unlock_super(inode
->i_sb
);
2042 ino_next
= NEXT_ORPHAN(inode
);
2043 prev
= ei
->i_orphan
.prev
;
2044 sbi
= EXT4_SB(inode
->i_sb
);
2046 jbd_debug(4, "remove inode %lu from orphan list\n", inode
->i_ino
);
2048 list_del_init(&ei
->i_orphan
);
2050 /* If we're on an error path, we may not have a valid
2051 * transaction handle with which to update the orphan list on
2052 * disk, but we still need to remove the inode from the linked
2053 * list in memory. */
2057 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
2061 if (prev
== &sbi
->s_orphan
) {
2062 jbd_debug(4, "superblock will point to %lu\n", ino_next
);
2063 BUFFER_TRACE(sbi
->s_sbh
, "get_write_access");
2064 err
= ext4_journal_get_write_access(handle
, sbi
->s_sbh
);
2067 sbi
->s_es
->s_last_orphan
= cpu_to_le32(ino_next
);
2068 err
= ext4_journal_dirty_metadata(handle
, sbi
->s_sbh
);
2070 struct ext4_iloc iloc2
;
2071 struct inode
*i_prev
=
2072 &list_entry(prev
, struct ext4_inode_info
, i_orphan
)->vfs_inode
;
2074 jbd_debug(4, "orphan inode %lu will point to %lu\n",
2075 i_prev
->i_ino
, ino_next
);
2076 err
= ext4_reserve_inode_write(handle
, i_prev
, &iloc2
);
2079 NEXT_ORPHAN(i_prev
) = ino_next
;
2080 err
= ext4_mark_iloc_dirty(handle
, i_prev
, &iloc2
);
2084 NEXT_ORPHAN(inode
) = 0;
2085 err
= ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
2088 ext4_std_error(inode
->i_sb
, err
);
2090 unlock_super(inode
->i_sb
);
2098 static int ext4_rmdir (struct inode
* dir
, struct dentry
*dentry
)
2101 struct inode
* inode
;
2102 struct buffer_head
* bh
;
2103 struct ext4_dir_entry_2
* de
;
2106 /* Initialize quotas before so that eventual writes go in
2107 * separate transaction */
2108 DQUOT_INIT(dentry
->d_inode
);
2109 handle
= ext4_journal_start(dir
, EXT4_DELETE_TRANS_BLOCKS(dir
->i_sb
));
2111 return PTR_ERR(handle
);
2114 bh
= ext4_find_entry (dentry
, &de
);
2118 if (IS_DIRSYNC(dir
))
2121 inode
= dentry
->d_inode
;
2124 if (le32_to_cpu(de
->inode
) != inode
->i_ino
)
2127 retval
= -ENOTEMPTY
;
2128 if (!empty_dir (inode
))
2131 retval
= ext4_delete_entry(handle
, dir
, de
, bh
);
2134 if (!EXT4_DIR_LINK_EMPTY(inode
))
2135 ext4_warning (inode
->i_sb
, "ext4_rmdir",
2136 "empty directory has too many links (%d)",
2140 /* There's no need to set i_disksize: the fact that i_nlink is
2141 * zero will ensure that the right thing happens during any
2144 ext4_orphan_add(handle
, inode
);
2145 inode
->i_ctime
= dir
->i_ctime
= dir
->i_mtime
= ext4_current_time(inode
);
2146 ext4_mark_inode_dirty(handle
, inode
);
2147 ext4_dec_count(handle
, dir
);
2148 ext4_update_dx_flag(dir
);
2149 ext4_mark_inode_dirty(handle
, dir
);
2152 ext4_journal_stop(handle
);
2157 static int ext4_unlink(struct inode
* dir
, struct dentry
*dentry
)
2160 struct inode
* inode
;
2161 struct buffer_head
* bh
;
2162 struct ext4_dir_entry_2
* de
;
2165 /* Initialize quotas before so that eventual writes go
2166 * in separate transaction */
2167 DQUOT_INIT(dentry
->d_inode
);
2168 handle
= ext4_journal_start(dir
, EXT4_DELETE_TRANS_BLOCKS(dir
->i_sb
));
2170 return PTR_ERR(handle
);
2172 if (IS_DIRSYNC(dir
))
2176 bh
= ext4_find_entry (dentry
, &de
);
2180 inode
= dentry
->d_inode
;
2183 if (le32_to_cpu(de
->inode
) != inode
->i_ino
)
2186 if (!inode
->i_nlink
) {
2187 ext4_warning (inode
->i_sb
, "ext4_unlink",
2188 "Deleting nonexistent file (%lu), %d",
2189 inode
->i_ino
, inode
->i_nlink
);
2192 retval
= ext4_delete_entry(handle
, dir
, de
, bh
);
2195 dir
->i_ctime
= dir
->i_mtime
= ext4_current_time(dir
);
2196 ext4_update_dx_flag(dir
);
2197 ext4_mark_inode_dirty(handle
, dir
);
2198 ext4_dec_count(handle
, inode
);
2199 if (!inode
->i_nlink
)
2200 ext4_orphan_add(handle
, inode
);
2201 inode
->i_ctime
= ext4_current_time(inode
);
2202 ext4_mark_inode_dirty(handle
, inode
);
2206 ext4_journal_stop(handle
);
2211 static int ext4_symlink (struct inode
* dir
,
2212 struct dentry
*dentry
, const char * symname
)
2215 struct inode
* inode
;
2216 int l
, err
, retries
= 0;
2218 l
= strlen(symname
)+1;
2219 if (l
> dir
->i_sb
->s_blocksize
)
2220 return -ENAMETOOLONG
;
2223 handle
= ext4_journal_start(dir
, EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
2224 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 5 +
2225 2*EXT4_QUOTA_INIT_BLOCKS(dir
->i_sb
));
2227 return PTR_ERR(handle
);
2229 if (IS_DIRSYNC(dir
))
2232 inode
= ext4_new_inode (handle
, dir
, S_IFLNK
|S_IRWXUGO
);
2233 err
= PTR_ERR(inode
);
2237 if (l
> sizeof (EXT4_I(inode
)->i_data
)) {
2238 inode
->i_op
= &ext4_symlink_inode_operations
;
2239 ext4_set_aops(inode
);
2241 * page_symlink() calls into ext4_prepare/commit_write.
2242 * We have a transaction open. All is sweetness. It also sets
2243 * i_size in generic_commit_write().
2245 err
= __page_symlink(inode
, symname
, l
,
2246 mapping_gfp_mask(inode
->i_mapping
) & ~__GFP_FS
);
2248 ext4_dec_count(handle
, inode
);
2249 ext4_mark_inode_dirty(handle
, inode
);
2254 inode
->i_op
= &ext4_fast_symlink_inode_operations
;
2255 memcpy((char*)&EXT4_I(inode
)->i_data
,symname
,l
);
2256 inode
->i_size
= l
-1;
2258 EXT4_I(inode
)->i_disksize
= inode
->i_size
;
2259 err
= ext4_add_nondir(handle
, dentry
, inode
);
2261 ext4_journal_stop(handle
);
2262 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
2267 static int ext4_link (struct dentry
* old_dentry
,
2268 struct inode
* dir
, struct dentry
*dentry
)
2271 struct inode
*inode
= old_dentry
->d_inode
;
2272 int err
, retries
= 0;
2274 if (EXT4_DIR_LINK_MAX(inode
))
2278 * Return -ENOENT if we've raced with unlink and i_nlink is 0. Doing
2279 * otherwise has the potential to corrupt the orphan inode list.
2281 if (inode
->i_nlink
== 0)
2285 handle
= ext4_journal_start(dir
, EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
2286 EXT4_INDEX_EXTRA_TRANS_BLOCKS
);
2288 return PTR_ERR(handle
);
2290 if (IS_DIRSYNC(dir
))
2293 inode
->i_ctime
= ext4_current_time(inode
);
2294 ext4_inc_count(handle
, inode
);
2295 atomic_inc(&inode
->i_count
);
2297 err
= ext4_add_nondir(handle
, dentry
, inode
);
2298 ext4_journal_stop(handle
);
2299 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
2304 #define PARENT_INO(buffer) \
2305 ((struct ext4_dir_entry_2 *) ((char *) buffer + \
2306 le16_to_cpu(((struct ext4_dir_entry_2 *) buffer)->rec_len)))->inode
2309 * Anybody can rename anything with this: the permission checks are left to the
2310 * higher-level routines.
2312 static int ext4_rename (struct inode
* old_dir
, struct dentry
*old_dentry
,
2313 struct inode
* new_dir
,struct dentry
*new_dentry
)
2316 struct inode
* old_inode
, * new_inode
;
2317 struct buffer_head
* old_bh
, * new_bh
, * dir_bh
;
2318 struct ext4_dir_entry_2
* old_de
, * new_de
;
2321 old_bh
= new_bh
= dir_bh
= NULL
;
2323 /* Initialize quotas before so that eventual writes go
2324 * in separate transaction */
2325 if (new_dentry
->d_inode
)
2326 DQUOT_INIT(new_dentry
->d_inode
);
2327 handle
= ext4_journal_start(old_dir
, 2 *
2328 EXT4_DATA_TRANS_BLOCKS(old_dir
->i_sb
) +
2329 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 2);
2331 return PTR_ERR(handle
);
2333 if (IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
))
2336 old_bh
= ext4_find_entry (old_dentry
, &old_de
);
2338 * Check for inode number is _not_ due to possible IO errors.
2339 * We might rmdir the source, keep it as pwd of some process
2340 * and merrily kill the link to whatever was created under the
2341 * same name. Goodbye sticky bit ;-<
2343 old_inode
= old_dentry
->d_inode
;
2345 if (!old_bh
|| le32_to_cpu(old_de
->inode
) != old_inode
->i_ino
)
2348 new_inode
= new_dentry
->d_inode
;
2349 new_bh
= ext4_find_entry (new_dentry
, &new_de
);
2356 if (S_ISDIR(old_inode
->i_mode
)) {
2358 retval
= -ENOTEMPTY
;
2359 if (!empty_dir (new_inode
))
2363 dir_bh
= ext4_bread (handle
, old_inode
, 0, 0, &retval
);
2366 if (le32_to_cpu(PARENT_INO(dir_bh
->b_data
)) != old_dir
->i_ino
)
2369 if (!new_inode
&& new_dir
!=old_dir
&&
2370 new_dir
->i_nlink
>= EXT4_LINK_MAX
)
2374 retval
= ext4_add_entry (handle
, new_dentry
, old_inode
);
2378 BUFFER_TRACE(new_bh
, "get write access");
2379 ext4_journal_get_write_access(handle
, new_bh
);
2380 new_de
->inode
= cpu_to_le32(old_inode
->i_ino
);
2381 if (EXT4_HAS_INCOMPAT_FEATURE(new_dir
->i_sb
,
2382 EXT4_FEATURE_INCOMPAT_FILETYPE
))
2383 new_de
->file_type
= old_de
->file_type
;
2384 new_dir
->i_version
++;
2385 BUFFER_TRACE(new_bh
, "call ext4_journal_dirty_metadata");
2386 ext4_journal_dirty_metadata(handle
, new_bh
);
2392 * Like most other Unix systems, set the ctime for inodes on a
2395 old_inode
->i_ctime
= ext4_current_time(old_inode
);
2396 ext4_mark_inode_dirty(handle
, old_inode
);
2401 if (le32_to_cpu(old_de
->inode
) != old_inode
->i_ino
||
2402 old_de
->name_len
!= old_dentry
->d_name
.len
||
2403 strncmp(old_de
->name
, old_dentry
->d_name
.name
, old_de
->name_len
) ||
2404 (retval
= ext4_delete_entry(handle
, old_dir
,
2405 old_de
, old_bh
)) == -ENOENT
) {
2406 /* old_de could have moved from under us during htree split, so
2407 * make sure that we are deleting the right entry. We might
2408 * also be pointing to a stale entry in the unused part of
2409 * old_bh so just checking inum and the name isn't enough. */
2410 struct buffer_head
*old_bh2
;
2411 struct ext4_dir_entry_2
*old_de2
;
2413 old_bh2
= ext4_find_entry(old_dentry
, &old_de2
);
2415 retval
= ext4_delete_entry(handle
, old_dir
,
2421 ext4_warning(old_dir
->i_sb
, "ext4_rename",
2422 "Deleting old file (%lu), %d, error=%d",
2423 old_dir
->i_ino
, old_dir
->i_nlink
, retval
);
2427 ext4_dec_count(handle
, new_inode
);
2428 new_inode
->i_ctime
= ext4_current_time(new_inode
);
2430 old_dir
->i_ctime
= old_dir
->i_mtime
= ext4_current_time(old_dir
);
2431 ext4_update_dx_flag(old_dir
);
2433 BUFFER_TRACE(dir_bh
, "get_write_access");
2434 ext4_journal_get_write_access(handle
, dir_bh
);
2435 PARENT_INO(dir_bh
->b_data
) = cpu_to_le32(new_dir
->i_ino
);
2436 BUFFER_TRACE(dir_bh
, "call ext4_journal_dirty_metadata");
2437 ext4_journal_dirty_metadata(handle
, dir_bh
);
2438 ext4_dec_count(handle
, old_dir
);
2440 /* checked empty_dir above, can't have another parent,
2441 * ext3_dec_count() won't work for many-linked dirs */
2442 new_inode
->i_nlink
= 0;
2444 ext4_inc_count(handle
, new_dir
);
2445 ext4_update_dx_flag(new_dir
);
2446 ext4_mark_inode_dirty(handle
, new_dir
);
2449 ext4_mark_inode_dirty(handle
, old_dir
);
2451 ext4_mark_inode_dirty(handle
, new_inode
);
2452 if (!new_inode
->i_nlink
)
2453 ext4_orphan_add(handle
, new_inode
);
2461 ext4_journal_stop(handle
);
2466 * directories can handle most operations...
2468 const struct inode_operations ext4_dir_inode_operations
= {
2469 .create
= ext4_create
,
2470 .lookup
= ext4_lookup
,
2472 .unlink
= ext4_unlink
,
2473 .symlink
= ext4_symlink
,
2474 .mkdir
= ext4_mkdir
,
2475 .rmdir
= ext4_rmdir
,
2476 .mknod
= ext4_mknod
,
2477 .rename
= ext4_rename
,
2478 .setattr
= ext4_setattr
,
2479 #ifdef CONFIG_EXT4DEV_FS_XATTR
2480 .setxattr
= generic_setxattr
,
2481 .getxattr
= generic_getxattr
,
2482 .listxattr
= ext4_listxattr
,
2483 .removexattr
= generic_removexattr
,
2485 .permission
= ext4_permission
,
2488 const struct inode_operations ext4_special_inode_operations
= {
2489 .setattr
= ext4_setattr
,
2490 #ifdef CONFIG_EXT4DEV_FS_XATTR
2491 .setxattr
= generic_setxattr
,
2492 .getxattr
= generic_getxattr
,
2493 .listxattr
= ext4_listxattr
,
2494 .removexattr
= generic_removexattr
,
2496 .permission
= ext4_permission
,