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/fcntl.h>
32 #include <linux/stat.h>
33 #include <linux/string.h>
34 #include <linux/quotaops.h>
35 #include <linux/buffer_head.h>
36 #include <linux/bio.h>
38 #include "ext4_jbd2.h"
43 #include <trace/events/ext4.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
,
54 ext4_lblk_t
*block
, int *err
)
56 struct buffer_head
*bh
;
58 if (unlikely(EXT4_SB(inode
->i_sb
)->s_max_dir_size_kb
&&
59 ((inode
->i_size
>> 10) >=
60 EXT4_SB(inode
->i_sb
)->s_max_dir_size_kb
))) {
65 *block
= inode
->i_size
>> inode
->i_sb
->s_blocksize_bits
;
67 bh
= ext4_bread(handle
, inode
, *block
, 1, err
);
69 inode
->i_size
+= inode
->i_sb
->s_blocksize
;
70 EXT4_I(inode
)->i_disksize
= inode
->i_size
;
71 *err
= ext4_journal_get_write_access(handle
, bh
);
79 ext4_error(inode
->i_sb
,
80 "Directory hole detected on inode %lu\n",
87 #define assert(test) J_ASSERT(test)
91 #define dxtrace(command) command
93 #define dxtrace(command)
117 * dx_root_info is laid out so that if it should somehow get overlaid by a
118 * dirent the two low bits of the hash version will be zero. Therefore, the
119 * hash version mod 4 should never be 0. Sincerely, the paranoia department.
124 struct fake_dirent dot
;
126 struct fake_dirent dotdot
;
130 __le32 reserved_zero
;
132 u8 info_length
; /* 8 */
137 struct dx_entry entries
[0];
142 struct fake_dirent fake
;
143 struct dx_entry entries
[0];
149 struct buffer_head
*bh
;
150 struct dx_entry
*entries
;
162 * This goes at the end of each htree block.
166 __le32 dt_checksum
; /* crc32c(uuid+inum+dirblock) */
169 static inline ext4_lblk_t
dx_get_block(struct dx_entry
*entry
);
170 static void dx_set_block(struct dx_entry
*entry
, ext4_lblk_t value
);
171 static inline unsigned dx_get_hash(struct dx_entry
*entry
);
172 static void dx_set_hash(struct dx_entry
*entry
, unsigned value
);
173 static unsigned dx_get_count(struct dx_entry
*entries
);
174 static unsigned dx_get_limit(struct dx_entry
*entries
);
175 static void dx_set_count(struct dx_entry
*entries
, unsigned value
);
176 static void dx_set_limit(struct dx_entry
*entries
, unsigned value
);
177 static unsigned dx_root_limit(struct inode
*dir
, unsigned infosize
);
178 static unsigned dx_node_limit(struct inode
*dir
);
179 static struct dx_frame
*dx_probe(const struct qstr
*d_name
,
181 struct dx_hash_info
*hinfo
,
182 struct dx_frame
*frame
,
184 static void dx_release(struct dx_frame
*frames
);
185 static int dx_make_map(struct ext4_dir_entry_2
*de
, unsigned blocksize
,
186 struct dx_hash_info
*hinfo
, struct dx_map_entry map
[]);
187 static void dx_sort_map(struct dx_map_entry
*map
, unsigned count
);
188 static struct ext4_dir_entry_2
*dx_move_dirents(char *from
, char *to
,
189 struct dx_map_entry
*offsets
, int count
, unsigned blocksize
);
190 static struct ext4_dir_entry_2
* dx_pack_dirents(char *base
, unsigned blocksize
);
191 static void dx_insert_block(struct dx_frame
*frame
,
192 u32 hash
, ext4_lblk_t block
);
193 static int ext4_htree_next_block(struct inode
*dir
, __u32 hash
,
194 struct dx_frame
*frame
,
195 struct dx_frame
*frames
,
197 static struct buffer_head
* ext4_dx_find_entry(struct inode
*dir
,
198 const struct qstr
*d_name
,
199 struct ext4_dir_entry_2
**res_dir
,
201 static int ext4_dx_add_entry(handle_t
*handle
, struct dentry
*dentry
,
202 struct inode
*inode
);
204 /* checksumming functions */
205 #define EXT4_DIRENT_TAIL(block, blocksize) \
206 ((struct ext4_dir_entry_tail *)(((void *)(block)) + \
208 sizeof(struct ext4_dir_entry_tail))))
210 static void initialize_dirent_tail(struct ext4_dir_entry_tail
*t
,
211 unsigned int blocksize
)
213 memset(t
, 0, sizeof(struct ext4_dir_entry_tail
));
214 t
->det_rec_len
= ext4_rec_len_to_disk(
215 sizeof(struct ext4_dir_entry_tail
), blocksize
);
216 t
->det_reserved_ft
= EXT4_FT_DIR_CSUM
;
219 /* Walk through a dirent block to find a checksum "dirent" at the tail */
220 static struct ext4_dir_entry_tail
*get_dirent_tail(struct inode
*inode
,
221 struct ext4_dir_entry
*de
)
223 struct ext4_dir_entry_tail
*t
;
226 struct ext4_dir_entry
*d
, *top
;
229 top
= (struct ext4_dir_entry
*)(((void *)de
) +
230 (EXT4_BLOCK_SIZE(inode
->i_sb
) -
231 sizeof(struct ext4_dir_entry_tail
)));
232 while (d
< top
&& d
->rec_len
)
233 d
= (struct ext4_dir_entry
*)(((void *)d
) +
234 le16_to_cpu(d
->rec_len
));
239 t
= (struct ext4_dir_entry_tail
*)d
;
241 t
= EXT4_DIRENT_TAIL(de
, EXT4_BLOCK_SIZE(inode
->i_sb
));
244 if (t
->det_reserved_zero1
||
245 le16_to_cpu(t
->det_rec_len
) != sizeof(struct ext4_dir_entry_tail
) ||
246 t
->det_reserved_zero2
||
247 t
->det_reserved_ft
!= EXT4_FT_DIR_CSUM
)
253 static __le32
ext4_dirent_csum(struct inode
*inode
,
254 struct ext4_dir_entry
*dirent
, int size
)
256 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
257 struct ext4_inode_info
*ei
= EXT4_I(inode
);
260 csum
= ext4_chksum(sbi
, ei
->i_csum_seed
, (__u8
*)dirent
, size
);
261 return cpu_to_le32(csum
);
264 static void warn_no_space_for_csum(struct inode
*inode
)
266 ext4_warning(inode
->i_sb
, "no space in directory inode %lu leaf for "
267 "checksum. Please run e2fsck -D.", inode
->i_ino
);
270 int ext4_dirent_csum_verify(struct inode
*inode
, struct ext4_dir_entry
*dirent
)
272 struct ext4_dir_entry_tail
*t
;
274 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode
->i_sb
,
275 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
278 t
= get_dirent_tail(inode
, dirent
);
280 warn_no_space_for_csum(inode
);
284 if (t
->det_checksum
!= ext4_dirent_csum(inode
, dirent
,
285 (void *)t
- (void *)dirent
))
291 static void ext4_dirent_csum_set(struct inode
*inode
,
292 struct ext4_dir_entry
*dirent
)
294 struct ext4_dir_entry_tail
*t
;
296 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode
->i_sb
,
297 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
300 t
= get_dirent_tail(inode
, dirent
);
302 warn_no_space_for_csum(inode
);
306 t
->det_checksum
= ext4_dirent_csum(inode
, dirent
,
307 (void *)t
- (void *)dirent
);
310 static inline int ext4_handle_dirty_dirent_node(handle_t
*handle
,
312 struct buffer_head
*bh
)
314 ext4_dirent_csum_set(inode
, (struct ext4_dir_entry
*)bh
->b_data
);
315 return ext4_handle_dirty_metadata(handle
, inode
, bh
);
318 static struct dx_countlimit
*get_dx_countlimit(struct inode
*inode
,
319 struct ext4_dir_entry
*dirent
,
322 struct ext4_dir_entry
*dp
;
323 struct dx_root_info
*root
;
326 if (le16_to_cpu(dirent
->rec_len
) == EXT4_BLOCK_SIZE(inode
->i_sb
))
328 else if (le16_to_cpu(dirent
->rec_len
) == 12) {
329 dp
= (struct ext4_dir_entry
*)(((void *)dirent
) + 12);
330 if (le16_to_cpu(dp
->rec_len
) !=
331 EXT4_BLOCK_SIZE(inode
->i_sb
) - 12)
333 root
= (struct dx_root_info
*)(((void *)dp
+ 12));
334 if (root
->reserved_zero
||
335 root
->info_length
!= sizeof(struct dx_root_info
))
342 *offset
= count_offset
;
343 return (struct dx_countlimit
*)(((void *)dirent
) + count_offset
);
346 static __le32
ext4_dx_csum(struct inode
*inode
, struct ext4_dir_entry
*dirent
,
347 int count_offset
, int count
, struct dx_tail
*t
)
349 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
350 struct ext4_inode_info
*ei
= EXT4_I(inode
);
351 __u32 csum
, old_csum
;
354 size
= count_offset
+ (count
* sizeof(struct dx_entry
));
355 old_csum
= t
->dt_checksum
;
357 csum
= ext4_chksum(sbi
, ei
->i_csum_seed
, (__u8
*)dirent
, size
);
358 csum
= ext4_chksum(sbi
, csum
, (__u8
*)t
, sizeof(struct dx_tail
));
359 t
->dt_checksum
= old_csum
;
361 return cpu_to_le32(csum
);
364 static int ext4_dx_csum_verify(struct inode
*inode
,
365 struct ext4_dir_entry
*dirent
)
367 struct dx_countlimit
*c
;
369 int count_offset
, limit
, count
;
371 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode
->i_sb
,
372 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
375 c
= get_dx_countlimit(inode
, dirent
, &count_offset
);
377 EXT4_ERROR_INODE(inode
, "dir seems corrupt? Run e2fsck -D.");
380 limit
= le16_to_cpu(c
->limit
);
381 count
= le16_to_cpu(c
->count
);
382 if (count_offset
+ (limit
* sizeof(struct dx_entry
)) >
383 EXT4_BLOCK_SIZE(inode
->i_sb
) - sizeof(struct dx_tail
)) {
384 warn_no_space_for_csum(inode
);
387 t
= (struct dx_tail
*)(((struct dx_entry
*)c
) + limit
);
389 if (t
->dt_checksum
!= ext4_dx_csum(inode
, dirent
, count_offset
,
395 static void ext4_dx_csum_set(struct inode
*inode
, struct ext4_dir_entry
*dirent
)
397 struct dx_countlimit
*c
;
399 int count_offset
, limit
, count
;
401 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode
->i_sb
,
402 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
405 c
= get_dx_countlimit(inode
, dirent
, &count_offset
);
407 EXT4_ERROR_INODE(inode
, "dir seems corrupt? Run e2fsck -D.");
410 limit
= le16_to_cpu(c
->limit
);
411 count
= le16_to_cpu(c
->count
);
412 if (count_offset
+ (limit
* sizeof(struct dx_entry
)) >
413 EXT4_BLOCK_SIZE(inode
->i_sb
) - sizeof(struct dx_tail
)) {
414 warn_no_space_for_csum(inode
);
417 t
= (struct dx_tail
*)(((struct dx_entry
*)c
) + limit
);
419 t
->dt_checksum
= ext4_dx_csum(inode
, dirent
, count_offset
, count
, t
);
422 static inline int ext4_handle_dirty_dx_node(handle_t
*handle
,
424 struct buffer_head
*bh
)
426 ext4_dx_csum_set(inode
, (struct ext4_dir_entry
*)bh
->b_data
);
427 return ext4_handle_dirty_metadata(handle
, inode
, bh
);
431 * p is at least 6 bytes before the end of page
433 static inline struct ext4_dir_entry_2
*
434 ext4_next_entry(struct ext4_dir_entry_2
*p
, unsigned long blocksize
)
436 return (struct ext4_dir_entry_2
*)((char *)p
+
437 ext4_rec_len_from_disk(p
->rec_len
, blocksize
));
441 * Future: use high four bits of block for coalesce-on-delete flags
442 * Mask them off for now.
445 static inline ext4_lblk_t
dx_get_block(struct dx_entry
*entry
)
447 return le32_to_cpu(entry
->block
) & 0x00ffffff;
450 static inline void dx_set_block(struct dx_entry
*entry
, ext4_lblk_t value
)
452 entry
->block
= cpu_to_le32(value
);
455 static inline unsigned dx_get_hash(struct dx_entry
*entry
)
457 return le32_to_cpu(entry
->hash
);
460 static inline void dx_set_hash(struct dx_entry
*entry
, unsigned value
)
462 entry
->hash
= cpu_to_le32(value
);
465 static inline unsigned dx_get_count(struct dx_entry
*entries
)
467 return le16_to_cpu(((struct dx_countlimit
*) entries
)->count
);
470 static inline unsigned dx_get_limit(struct dx_entry
*entries
)
472 return le16_to_cpu(((struct dx_countlimit
*) entries
)->limit
);
475 static inline void dx_set_count(struct dx_entry
*entries
, unsigned value
)
477 ((struct dx_countlimit
*) entries
)->count
= cpu_to_le16(value
);
480 static inline void dx_set_limit(struct dx_entry
*entries
, unsigned value
)
482 ((struct dx_countlimit
*) entries
)->limit
= cpu_to_le16(value
);
485 static inline unsigned dx_root_limit(struct inode
*dir
, unsigned infosize
)
487 unsigned entry_space
= dir
->i_sb
->s_blocksize
- EXT4_DIR_REC_LEN(1) -
488 EXT4_DIR_REC_LEN(2) - infosize
;
490 if (EXT4_HAS_RO_COMPAT_FEATURE(dir
->i_sb
,
491 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
492 entry_space
-= sizeof(struct dx_tail
);
493 return entry_space
/ sizeof(struct dx_entry
);
496 static inline unsigned dx_node_limit(struct inode
*dir
)
498 unsigned entry_space
= dir
->i_sb
->s_blocksize
- EXT4_DIR_REC_LEN(0);
500 if (EXT4_HAS_RO_COMPAT_FEATURE(dir
->i_sb
,
501 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
502 entry_space
-= sizeof(struct dx_tail
);
503 return entry_space
/ sizeof(struct dx_entry
);
510 static void dx_show_index(char * label
, struct dx_entry
*entries
)
512 int i
, n
= dx_get_count (entries
);
513 printk(KERN_DEBUG
"%s index ", label
);
514 for (i
= 0; i
< n
; i
++) {
515 printk("%x->%lu ", i
? dx_get_hash(entries
+ i
) :
516 0, (unsigned long)dx_get_block(entries
+ i
));
528 static struct stats
dx_show_leaf(struct dx_hash_info
*hinfo
, struct ext4_dir_entry_2
*de
,
529 int size
, int show_names
)
531 unsigned names
= 0, space
= 0;
532 char *base
= (char *) de
;
533 struct dx_hash_info h
= *hinfo
;
536 while ((char *) de
< base
+ size
)
542 int len
= de
->name_len
;
543 char *name
= de
->name
;
544 while (len
--) printk("%c", *name
++);
545 ext4fs_dirhash(de
->name
, de
->name_len
, &h
);
546 printk(":%x.%u ", h
.hash
,
547 (unsigned) ((char *) de
- base
));
549 space
+= EXT4_DIR_REC_LEN(de
->name_len
);
552 de
= ext4_next_entry(de
, size
);
554 printk("(%i)\n", names
);
555 return (struct stats
) { names
, space
, 1 };
558 struct stats
dx_show_entries(struct dx_hash_info
*hinfo
, struct inode
*dir
,
559 struct dx_entry
*entries
, int levels
)
561 unsigned blocksize
= dir
->i_sb
->s_blocksize
;
562 unsigned count
= dx_get_count(entries
), names
= 0, space
= 0, i
;
564 struct buffer_head
*bh
;
566 printk("%i indexed blocks...\n", count
);
567 for (i
= 0; i
< count
; i
++, entries
++)
569 ext4_lblk_t block
= dx_get_block(entries
);
570 ext4_lblk_t hash
= i
? dx_get_hash(entries
): 0;
571 u32 range
= i
< count
- 1? (dx_get_hash(entries
+ 1) - hash
): ~hash
;
573 printk("%s%3u:%03u hash %8x/%8x ",levels
?"":" ", i
, block
, hash
, range
);
574 if (!(bh
= ext4_bread (NULL
,dir
, block
, 0,&err
))) continue;
576 dx_show_entries(hinfo
, dir
, ((struct dx_node
*) bh
->b_data
)->entries
, levels
- 1):
577 dx_show_leaf(hinfo
, (struct ext4_dir_entry_2
*) bh
->b_data
, blocksize
, 0);
578 names
+= stats
.names
;
579 space
+= stats
.space
;
580 bcount
+= stats
.bcount
;
584 printk(KERN_DEBUG
"%snames %u, fullness %u (%u%%)\n",
585 levels
? "" : " ", names
, space
/bcount
,
586 (space
/bcount
)*100/blocksize
);
587 return (struct stats
) { names
, space
, bcount
};
589 #endif /* DX_DEBUG */
592 * Probe for a directory leaf block to search.
594 * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
595 * error in the directory index, and the caller should fall back to
596 * searching the directory normally. The callers of dx_probe **MUST**
597 * check for this error code, and make sure it never gets reflected
600 static struct dx_frame
*
601 dx_probe(const struct qstr
*d_name
, struct inode
*dir
,
602 struct dx_hash_info
*hinfo
, struct dx_frame
*frame_in
, int *err
)
604 unsigned count
, indirect
;
605 struct dx_entry
*at
, *entries
, *p
, *q
, *m
;
606 struct dx_root
*root
;
607 struct buffer_head
*bh
;
608 struct dx_frame
*frame
= frame_in
;
612 if (!(bh
= ext4_bread(NULL
, dir
, 0, 0, err
))) {
614 *err
= ERR_BAD_DX_DIR
;
617 root
= (struct dx_root
*) bh
->b_data
;
618 if (root
->info
.hash_version
!= DX_HASH_TEA
&&
619 root
->info
.hash_version
!= DX_HASH_HALF_MD4
&&
620 root
->info
.hash_version
!= DX_HASH_LEGACY
) {
621 ext4_warning(dir
->i_sb
, "Unrecognised inode hash code %d",
622 root
->info
.hash_version
);
624 *err
= ERR_BAD_DX_DIR
;
627 hinfo
->hash_version
= root
->info
.hash_version
;
628 if (hinfo
->hash_version
<= DX_HASH_TEA
)
629 hinfo
->hash_version
+= EXT4_SB(dir
->i_sb
)->s_hash_unsigned
;
630 hinfo
->seed
= EXT4_SB(dir
->i_sb
)->s_hash_seed
;
632 ext4fs_dirhash(d_name
->name
, d_name
->len
, hinfo
);
635 if (root
->info
.unused_flags
& 1) {
636 ext4_warning(dir
->i_sb
, "Unimplemented inode hash flags: %#06x",
637 root
->info
.unused_flags
);
639 *err
= ERR_BAD_DX_DIR
;
643 if ((indirect
= root
->info
.indirect_levels
) > 1) {
644 ext4_warning(dir
->i_sb
, "Unimplemented inode hash depth: %#06x",
645 root
->info
.indirect_levels
);
647 *err
= ERR_BAD_DX_DIR
;
651 if (!buffer_verified(bh
) &&
652 !ext4_dx_csum_verify(dir
, (struct ext4_dir_entry
*)bh
->b_data
)) {
653 ext4_warning(dir
->i_sb
, "Root failed checksum");
655 *err
= ERR_BAD_DX_DIR
;
658 set_buffer_verified(bh
);
660 entries
= (struct dx_entry
*) (((char *)&root
->info
) +
661 root
->info
.info_length
);
663 if (dx_get_limit(entries
) != dx_root_limit(dir
,
664 root
->info
.info_length
)) {
665 ext4_warning(dir
->i_sb
, "dx entry: limit != root limit");
667 *err
= ERR_BAD_DX_DIR
;
671 dxtrace(printk("Look up %x", hash
));
674 count
= dx_get_count(entries
);
675 if (!count
|| count
> dx_get_limit(entries
)) {
676 ext4_warning(dir
->i_sb
,
677 "dx entry: no count or count > limit");
679 *err
= ERR_BAD_DX_DIR
;
684 q
= entries
+ count
- 1;
688 dxtrace(printk("."));
689 if (dx_get_hash(m
) > hash
)
695 if (0) // linear search cross check
697 unsigned n
= count
- 1;
701 dxtrace(printk(","));
702 if (dx_get_hash(++at
) > hash
)
708 assert (at
== p
- 1);
712 dxtrace(printk(" %x->%u\n", at
== entries
? 0: dx_get_hash(at
), dx_get_block(at
)));
714 frame
->entries
= entries
;
716 if (!indirect
--) return frame
;
717 if (!(bh
= ext4_bread(NULL
, dir
, dx_get_block(at
), 0, err
))) {
719 *err
= ERR_BAD_DX_DIR
;
722 at
= entries
= ((struct dx_node
*) bh
->b_data
)->entries
;
724 if (!buffer_verified(bh
) &&
725 !ext4_dx_csum_verify(dir
,
726 (struct ext4_dir_entry
*)bh
->b_data
)) {
727 ext4_warning(dir
->i_sb
, "Node failed checksum");
729 *err
= ERR_BAD_DX_DIR
;
732 set_buffer_verified(bh
);
734 if (dx_get_limit(entries
) != dx_node_limit (dir
)) {
735 ext4_warning(dir
->i_sb
,
736 "dx entry: limit != node limit");
738 *err
= ERR_BAD_DX_DIR
;
745 while (frame
>= frame_in
) {
750 if (*err
== ERR_BAD_DX_DIR
)
751 ext4_warning(dir
->i_sb
,
752 "Corrupt dir inode %lu, running e2fsck is "
753 "recommended.", dir
->i_ino
);
757 static void dx_release (struct dx_frame
*frames
)
759 if (frames
[0].bh
== NULL
)
762 if (((struct dx_root
*) frames
[0].bh
->b_data
)->info
.indirect_levels
)
763 brelse(frames
[1].bh
);
764 brelse(frames
[0].bh
);
768 * This function increments the frame pointer to search the next leaf
769 * block, and reads in the necessary intervening nodes if the search
770 * should be necessary. Whether or not the search is necessary is
771 * controlled by the hash parameter. If the hash value is even, then
772 * the search is only continued if the next block starts with that
773 * hash value. This is used if we are searching for a specific file.
775 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
777 * This function returns 1 if the caller should continue to search,
778 * or 0 if it should not. If there is an error reading one of the
779 * index blocks, it will a negative error code.
781 * If start_hash is non-null, it will be filled in with the starting
782 * hash of the next page.
784 static int ext4_htree_next_block(struct inode
*dir
, __u32 hash
,
785 struct dx_frame
*frame
,
786 struct dx_frame
*frames
,
790 struct buffer_head
*bh
;
791 int err
, num_frames
= 0;
796 * Find the next leaf page by incrementing the frame pointer.
797 * If we run out of entries in the interior node, loop around and
798 * increment pointer in the parent node. When we break out of
799 * this loop, num_frames indicates the number of interior
800 * nodes need to be read.
803 if (++(p
->at
) < p
->entries
+ dx_get_count(p
->entries
))
812 * If the hash is 1, then continue only if the next page has a
813 * continuation hash of any value. This is used for readdir
814 * handling. Otherwise, check to see if the hash matches the
815 * desired contiuation hash. If it doesn't, return since
816 * there's no point to read in the successive index pages.
818 bhash
= dx_get_hash(p
->at
);
821 if ((hash
& 1) == 0) {
822 if ((bhash
& ~1) != hash
)
826 * If the hash is HASH_NB_ALWAYS, we always go to the next
827 * block so no check is necessary
829 while (num_frames
--) {
830 if (!(bh
= ext4_bread(NULL
, dir
, dx_get_block(p
->at
),
833 ext4_error(dir
->i_sb
,
834 "Directory hole detected on inode %lu\n",
838 return err
; /* Failure */
841 if (!buffer_verified(bh
) &&
842 !ext4_dx_csum_verify(dir
,
843 (struct ext4_dir_entry
*)bh
->b_data
)) {
844 ext4_warning(dir
->i_sb
, "Node failed checksum");
847 set_buffer_verified(bh
);
852 p
->at
= p
->entries
= ((struct dx_node
*) bh
->b_data
)->entries
;
859 * This function fills a red-black tree with information from a
860 * directory block. It returns the number directory entries loaded
861 * into the tree. If there is an error it is returned in err.
863 static int htree_dirblock_to_tree(struct file
*dir_file
,
864 struct inode
*dir
, ext4_lblk_t block
,
865 struct dx_hash_info
*hinfo
,
866 __u32 start_hash
, __u32 start_minor_hash
)
868 struct buffer_head
*bh
;
869 struct ext4_dir_entry_2
*de
, *top
;
870 int err
= 0, count
= 0;
872 dxtrace(printk(KERN_INFO
"In htree dirblock_to_tree: block %lu\n",
873 (unsigned long)block
));
874 if (!(bh
= ext4_bread(NULL
, dir
, block
, 0, &err
))) {
877 ext4_error(dir
->i_sb
,
878 "Directory hole detected on inode %lu\n",
884 if (!buffer_verified(bh
) &&
885 !ext4_dirent_csum_verify(dir
, (struct ext4_dir_entry
*)bh
->b_data
))
887 set_buffer_verified(bh
);
889 de
= (struct ext4_dir_entry_2
*) bh
->b_data
;
890 top
= (struct ext4_dir_entry_2
*) ((char *) de
+
891 dir
->i_sb
->s_blocksize
-
892 EXT4_DIR_REC_LEN(0));
893 for (; de
< top
; de
= ext4_next_entry(de
, dir
->i_sb
->s_blocksize
)) {
894 if (ext4_check_dir_entry(dir
, NULL
, de
, bh
,
895 bh
->b_data
, bh
->b_size
,
896 (block
<<EXT4_BLOCK_SIZE_BITS(dir
->i_sb
))
897 + ((char *)de
- bh
->b_data
))) {
898 /* On error, skip the f_pos to the next block. */
899 dir_file
->f_pos
= (dir_file
->f_pos
|
900 (dir
->i_sb
->s_blocksize
- 1)) + 1;
904 ext4fs_dirhash(de
->name
, de
->name_len
, hinfo
);
905 if ((hinfo
->hash
< start_hash
) ||
906 ((hinfo
->hash
== start_hash
) &&
907 (hinfo
->minor_hash
< start_minor_hash
)))
911 if ((err
= ext4_htree_store_dirent(dir_file
,
912 hinfo
->hash
, hinfo
->minor_hash
, de
)) != 0) {
924 * This function fills a red-black tree with information from a
925 * directory. We start scanning the directory in hash order, starting
926 * at start_hash and start_minor_hash.
928 * This function returns the number of entries inserted into the tree,
929 * or a negative error code.
931 int ext4_htree_fill_tree(struct file
*dir_file
, __u32 start_hash
,
932 __u32 start_minor_hash
, __u32
*next_hash
)
934 struct dx_hash_info hinfo
;
935 struct ext4_dir_entry_2
*de
;
936 struct dx_frame frames
[2], *frame
;
943 dxtrace(printk(KERN_DEBUG
"In htree_fill_tree, start hash: %x:%x\n",
944 start_hash
, start_minor_hash
));
945 dir
= dir_file
->f_path
.dentry
->d_inode
;
946 if (!(ext4_test_inode_flag(dir
, EXT4_INODE_INDEX
))) {
947 hinfo
.hash_version
= EXT4_SB(dir
->i_sb
)->s_def_hash_version
;
948 if (hinfo
.hash_version
<= DX_HASH_TEA
)
949 hinfo
.hash_version
+=
950 EXT4_SB(dir
->i_sb
)->s_hash_unsigned
;
951 hinfo
.seed
= EXT4_SB(dir
->i_sb
)->s_hash_seed
;
952 count
= htree_dirblock_to_tree(dir_file
, dir
, 0, &hinfo
,
953 start_hash
, start_minor_hash
);
957 hinfo
.hash
= start_hash
;
958 hinfo
.minor_hash
= 0;
959 frame
= dx_probe(NULL
, dir
, &hinfo
, frames
, &err
);
963 /* Add '.' and '..' from the htree header */
964 if (!start_hash
&& !start_minor_hash
) {
965 de
= (struct ext4_dir_entry_2
*) frames
[0].bh
->b_data
;
966 if ((err
= ext4_htree_store_dirent(dir_file
, 0, 0, de
)) != 0)
970 if (start_hash
< 2 || (start_hash
==2 && start_minor_hash
==0)) {
971 de
= (struct ext4_dir_entry_2
*) frames
[0].bh
->b_data
;
972 de
= ext4_next_entry(de
, dir
->i_sb
->s_blocksize
);
973 if ((err
= ext4_htree_store_dirent(dir_file
, 2, 0, de
)) != 0)
979 block
= dx_get_block(frame
->at
);
980 ret
= htree_dirblock_to_tree(dir_file
, dir
, block
, &hinfo
,
981 start_hash
, start_minor_hash
);
988 ret
= ext4_htree_next_block(dir
, HASH_NB_ALWAYS
,
989 frame
, frames
, &hashval
);
990 *next_hash
= hashval
;
996 * Stop if: (a) there are no more entries, or
997 * (b) we have inserted at least one entry and the
998 * next hash value is not a continuation
1001 (count
&& ((hashval
& 1) == 0)))
1005 dxtrace(printk(KERN_DEBUG
"Fill tree: returned %d entries, "
1006 "next hash: %x\n", count
, *next_hash
));
1015 * Directory block splitting, compacting
1019 * Create map of hash values, offsets, and sizes, stored at end of block.
1020 * Returns number of entries mapped.
1022 static int dx_make_map(struct ext4_dir_entry_2
*de
, unsigned blocksize
,
1023 struct dx_hash_info
*hinfo
,
1024 struct dx_map_entry
*map_tail
)
1027 char *base
= (char *) de
;
1028 struct dx_hash_info h
= *hinfo
;
1030 while ((char *) de
< base
+ blocksize
) {
1031 if (de
->name_len
&& de
->inode
) {
1032 ext4fs_dirhash(de
->name
, de
->name_len
, &h
);
1034 map_tail
->hash
= h
.hash
;
1035 map_tail
->offs
= ((char *) de
- base
)>>2;
1036 map_tail
->size
= le16_to_cpu(de
->rec_len
);
1040 /* XXX: do we need to check rec_len == 0 case? -Chris */
1041 de
= ext4_next_entry(de
, blocksize
);
1046 /* Sort map by hash value */
1047 static void dx_sort_map (struct dx_map_entry
*map
, unsigned count
)
1049 struct dx_map_entry
*p
, *q
, *top
= map
+ count
- 1;
1051 /* Combsort until bubble sort doesn't suck */
1053 count
= count
*10/13;
1054 if (count
- 9 < 2) /* 9, 10 -> 11 */
1056 for (p
= top
, q
= p
- count
; q
>= map
; p
--, q
--)
1057 if (p
->hash
< q
->hash
)
1060 /* Garden variety bubble sort */
1065 if (q
[1].hash
>= q
[0].hash
)
1073 static void dx_insert_block(struct dx_frame
*frame
, u32 hash
, ext4_lblk_t block
)
1075 struct dx_entry
*entries
= frame
->entries
;
1076 struct dx_entry
*old
= frame
->at
, *new = old
+ 1;
1077 int count
= dx_get_count(entries
);
1079 assert(count
< dx_get_limit(entries
));
1080 assert(old
< entries
+ count
);
1081 memmove(new + 1, new, (char *)(entries
+ count
) - (char *)(new));
1082 dx_set_hash(new, hash
);
1083 dx_set_block(new, block
);
1084 dx_set_count(entries
, count
+ 1);
1088 * NOTE! unlike strncmp, ext4_match returns 1 for success, 0 for failure.
1090 * `len <= EXT4_NAME_LEN' is guaranteed by caller.
1091 * `de != NULL' is guaranteed by caller.
1093 static inline int ext4_match (int len
, const char * const name
,
1094 struct ext4_dir_entry_2
* de
)
1096 if (len
!= de
->name_len
)
1100 return !memcmp(name
, de
->name
, len
);
1104 * Returns 0 if not found, -1 on failure, and 1 on success
1106 static inline int search_dirblock(struct buffer_head
*bh
,
1108 const struct qstr
*d_name
,
1109 unsigned int offset
,
1110 struct ext4_dir_entry_2
** res_dir
)
1112 struct ext4_dir_entry_2
* de
;
1115 const char *name
= d_name
->name
;
1116 int namelen
= d_name
->len
;
1118 de
= (struct ext4_dir_entry_2
*) bh
->b_data
;
1119 dlimit
= bh
->b_data
+ dir
->i_sb
->s_blocksize
;
1120 while ((char *) de
< dlimit
) {
1121 /* this code is executed quadratically often */
1122 /* do minimal checking `by hand' */
1124 if ((char *) de
+ namelen
<= dlimit
&&
1125 ext4_match (namelen
, name
, de
)) {
1126 /* found a match - just to be sure, do a full check */
1127 if (ext4_check_dir_entry(dir
, NULL
, de
, bh
, bh
->b_data
,
1128 bh
->b_size
, offset
))
1133 /* prevent looping on a bad block */
1134 de_len
= ext4_rec_len_from_disk(de
->rec_len
,
1135 dir
->i_sb
->s_blocksize
);
1139 de
= (struct ext4_dir_entry_2
*) ((char *) de
+ de_len
);
1144 static int is_dx_internal_node(struct inode
*dir
, ext4_lblk_t block
,
1145 struct ext4_dir_entry
*de
)
1147 struct super_block
*sb
= dir
->i_sb
;
1153 if (de
->inode
== 0 &&
1154 ext4_rec_len_from_disk(de
->rec_len
, sb
->s_blocksize
) ==
1163 * finds an entry in the specified directory with the wanted name. It
1164 * returns the cache buffer in which the entry was found, and the entry
1165 * itself (as a parameter - res_dir). It does NOT read the inode of the
1166 * entry - you'll have to do that yourself if you want to.
1168 * The returned buffer_head has ->b_count elevated. The caller is expected
1169 * to brelse() it when appropriate.
1171 static struct buffer_head
* ext4_find_entry (struct inode
*dir
,
1172 const struct qstr
*d_name
,
1173 struct ext4_dir_entry_2
** res_dir
)
1175 struct super_block
*sb
;
1176 struct buffer_head
*bh_use
[NAMEI_RA_SIZE
];
1177 struct buffer_head
*bh
, *ret
= NULL
;
1178 ext4_lblk_t start
, block
, b
;
1179 const u8
*name
= d_name
->name
;
1180 int ra_max
= 0; /* Number of bh's in the readahead
1182 int ra_ptr
= 0; /* Current index into readahead
1185 ext4_lblk_t nblocks
;
1191 namelen
= d_name
->len
;
1192 if (namelen
> EXT4_NAME_LEN
)
1194 if ((namelen
<= 2) && (name
[0] == '.') &&
1195 (name
[1] == '.' || name
[1] == '\0')) {
1197 * "." or ".." will only be in the first block
1198 * NFS may look up ".."; "." should be handled by the VFS
1205 bh
= ext4_dx_find_entry(dir
, d_name
, res_dir
, &err
);
1207 * On success, or if the error was file not found,
1208 * return. Otherwise, fall back to doing a search the
1209 * old fashioned way.
1211 if (bh
|| (err
!= ERR_BAD_DX_DIR
))
1213 dxtrace(printk(KERN_DEBUG
"ext4_find_entry: dx failed, "
1216 nblocks
= dir
->i_size
>> EXT4_BLOCK_SIZE_BITS(sb
);
1217 start
= EXT4_I(dir
)->i_dir_start_lookup
;
1218 if (start
>= nblocks
)
1224 * We deal with the read-ahead logic here.
1226 if (ra_ptr
>= ra_max
) {
1227 /* Refill the readahead buffer */
1230 for (ra_max
= 0; ra_max
< NAMEI_RA_SIZE
; ra_max
++) {
1232 * Terminate if we reach the end of the
1233 * directory and must wrap, or if our
1234 * search has finished at this block.
1236 if (b
>= nblocks
|| (num
&& block
== start
)) {
1237 bh_use
[ra_max
] = NULL
;
1241 bh
= ext4_getblk(NULL
, dir
, b
++, 0, &err
);
1242 bh_use
[ra_max
] = bh
;
1244 ll_rw_block(READ
| REQ_META
| REQ_PRIO
,
1248 if ((bh
= bh_use
[ra_ptr
++]) == NULL
)
1251 if (!buffer_uptodate(bh
)) {
1252 /* read error, skip block & hope for the best */
1253 EXT4_ERROR_INODE(dir
, "reading directory lblock %lu",
1254 (unsigned long) block
);
1258 if (!buffer_verified(bh
) &&
1259 !is_dx_internal_node(dir
, block
,
1260 (struct ext4_dir_entry
*)bh
->b_data
) &&
1261 !ext4_dirent_csum_verify(dir
,
1262 (struct ext4_dir_entry
*)bh
->b_data
)) {
1263 EXT4_ERROR_INODE(dir
, "checksumming directory "
1264 "block %lu", (unsigned long)block
);
1268 set_buffer_verified(bh
);
1269 i
= search_dirblock(bh
, dir
, d_name
,
1270 block
<< EXT4_BLOCK_SIZE_BITS(sb
), res_dir
);
1272 EXT4_I(dir
)->i_dir_start_lookup
= block
;
1274 goto cleanup_and_exit
;
1278 goto cleanup_and_exit
;
1281 if (++block
>= nblocks
)
1283 } while (block
!= start
);
1286 * If the directory has grown while we were searching, then
1287 * search the last part of the directory before giving up.
1290 nblocks
= dir
->i_size
>> EXT4_BLOCK_SIZE_BITS(sb
);
1291 if (block
< nblocks
) {
1297 /* Clean up the read-ahead blocks */
1298 for (; ra_ptr
< ra_max
; ra_ptr
++)
1299 brelse(bh_use
[ra_ptr
]);
1303 static struct buffer_head
* ext4_dx_find_entry(struct inode
*dir
, const struct qstr
*d_name
,
1304 struct ext4_dir_entry_2
**res_dir
, int *err
)
1306 struct super_block
* sb
= dir
->i_sb
;
1307 struct dx_hash_info hinfo
;
1308 struct dx_frame frames
[2], *frame
;
1309 struct buffer_head
*bh
;
1313 if (!(frame
= dx_probe(d_name
, dir
, &hinfo
, frames
, err
)))
1316 block
= dx_get_block(frame
->at
);
1317 if (!(bh
= ext4_bread(NULL
, dir
, block
, 0, err
))) {
1320 ext4_error(dir
->i_sb
,
1321 "Directory hole detected on inode %lu\n",
1327 if (!buffer_verified(bh
) &&
1328 !ext4_dirent_csum_verify(dir
,
1329 (struct ext4_dir_entry
*)bh
->b_data
)) {
1330 EXT4_ERROR_INODE(dir
, "checksumming directory "
1331 "block %lu", (unsigned long)block
);
1336 set_buffer_verified(bh
);
1337 retval
= search_dirblock(bh
, dir
, d_name
,
1338 block
<< EXT4_BLOCK_SIZE_BITS(sb
),
1340 if (retval
== 1) { /* Success! */
1346 *err
= ERR_BAD_DX_DIR
;
1350 /* Check to see if we should continue to search */
1351 retval
= ext4_htree_next_block(dir
, hinfo
.hash
, frame
,
1355 "error reading index page in directory #%lu",
1360 } while (retval
== 1);
1364 dxtrace(printk(KERN_DEBUG
"%s not found\n", d_name
->name
));
1365 dx_release (frames
);
1369 static struct dentry
*ext4_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
1371 struct inode
*inode
;
1372 struct ext4_dir_entry_2
*de
;
1373 struct buffer_head
*bh
;
1375 if (dentry
->d_name
.len
> EXT4_NAME_LEN
)
1376 return ERR_PTR(-ENAMETOOLONG
);
1378 bh
= ext4_find_entry(dir
, &dentry
->d_name
, &de
);
1381 __u32 ino
= le32_to_cpu(de
->inode
);
1383 if (!ext4_valid_inum(dir
->i_sb
, ino
)) {
1384 EXT4_ERROR_INODE(dir
, "bad inode number: %u", ino
);
1385 return ERR_PTR(-EIO
);
1387 if (unlikely(ino
== dir
->i_ino
)) {
1388 EXT4_ERROR_INODE(dir
, "'%.*s' linked to parent dir",
1390 dentry
->d_name
.name
);
1391 return ERR_PTR(-EIO
);
1393 inode
= ext4_iget(dir
->i_sb
, ino
);
1394 if (inode
== ERR_PTR(-ESTALE
)) {
1395 EXT4_ERROR_INODE(dir
,
1396 "deleted inode referenced: %u",
1398 return ERR_PTR(-EIO
);
1401 return d_splice_alias(inode
, dentry
);
1405 struct dentry
*ext4_get_parent(struct dentry
*child
)
1408 static const struct qstr dotdot
= QSTR_INIT("..", 2);
1409 struct ext4_dir_entry_2
* de
;
1410 struct buffer_head
*bh
;
1412 bh
= ext4_find_entry(child
->d_inode
, &dotdot
, &de
);
1414 return ERR_PTR(-ENOENT
);
1415 ino
= le32_to_cpu(de
->inode
);
1418 if (!ext4_valid_inum(child
->d_inode
->i_sb
, ino
)) {
1419 EXT4_ERROR_INODE(child
->d_inode
,
1420 "bad parent inode number: %u", ino
);
1421 return ERR_PTR(-EIO
);
1424 return d_obtain_alias(ext4_iget(child
->d_inode
->i_sb
, ino
));
1428 static unsigned char ext4_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
1429 [S_IFREG
>> S_SHIFT
] = EXT4_FT_REG_FILE
,
1430 [S_IFDIR
>> S_SHIFT
] = EXT4_FT_DIR
,
1431 [S_IFCHR
>> S_SHIFT
] = EXT4_FT_CHRDEV
,
1432 [S_IFBLK
>> S_SHIFT
] = EXT4_FT_BLKDEV
,
1433 [S_IFIFO
>> S_SHIFT
] = EXT4_FT_FIFO
,
1434 [S_IFSOCK
>> S_SHIFT
] = EXT4_FT_SOCK
,
1435 [S_IFLNK
>> S_SHIFT
] = EXT4_FT_SYMLINK
,
1438 static inline void ext4_set_de_type(struct super_block
*sb
,
1439 struct ext4_dir_entry_2
*de
,
1441 if (EXT4_HAS_INCOMPAT_FEATURE(sb
, EXT4_FEATURE_INCOMPAT_FILETYPE
))
1442 de
->file_type
= ext4_type_by_mode
[(mode
& S_IFMT
)>>S_SHIFT
];
1446 * Move count entries from end of map between two memory locations.
1447 * Returns pointer to last entry moved.
1449 static struct ext4_dir_entry_2
*
1450 dx_move_dirents(char *from
, char *to
, struct dx_map_entry
*map
, int count
,
1453 unsigned rec_len
= 0;
1456 struct ext4_dir_entry_2
*de
= (struct ext4_dir_entry_2
*)
1457 (from
+ (map
->offs
<<2));
1458 rec_len
= EXT4_DIR_REC_LEN(de
->name_len
);
1459 memcpy (to
, de
, rec_len
);
1460 ((struct ext4_dir_entry_2
*) to
)->rec_len
=
1461 ext4_rec_len_to_disk(rec_len
, blocksize
);
1466 return (struct ext4_dir_entry_2
*) (to
- rec_len
);
1470 * Compact each dir entry in the range to the minimal rec_len.
1471 * Returns pointer to last entry in range.
1473 static struct ext4_dir_entry_2
* dx_pack_dirents(char *base
, unsigned blocksize
)
1475 struct ext4_dir_entry_2
*next
, *to
, *prev
, *de
= (struct ext4_dir_entry_2
*) base
;
1476 unsigned rec_len
= 0;
1479 while ((char*)de
< base
+ blocksize
) {
1480 next
= ext4_next_entry(de
, blocksize
);
1481 if (de
->inode
&& de
->name_len
) {
1482 rec_len
= EXT4_DIR_REC_LEN(de
->name_len
);
1484 memmove(to
, de
, rec_len
);
1485 to
->rec_len
= ext4_rec_len_to_disk(rec_len
, blocksize
);
1487 to
= (struct ext4_dir_entry_2
*) (((char *) to
) + rec_len
);
1495 * Split a full leaf block to make room for a new dir entry.
1496 * Allocate a new block, and move entries so that they are approx. equally full.
1497 * Returns pointer to de in block into which the new entry will be inserted.
1499 static struct ext4_dir_entry_2
*do_split(handle_t
*handle
, struct inode
*dir
,
1500 struct buffer_head
**bh
,struct dx_frame
*frame
,
1501 struct dx_hash_info
*hinfo
, int *error
)
1503 unsigned blocksize
= dir
->i_sb
->s_blocksize
;
1504 unsigned count
, continued
;
1505 struct buffer_head
*bh2
;
1506 ext4_lblk_t newblock
;
1508 struct dx_map_entry
*map
;
1509 char *data1
= (*bh
)->b_data
, *data2
;
1510 unsigned split
, move
, size
;
1511 struct ext4_dir_entry_2
*de
= NULL
, *de2
;
1512 struct ext4_dir_entry_tail
*t
;
1516 if (EXT4_HAS_RO_COMPAT_FEATURE(dir
->i_sb
,
1517 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
1518 csum_size
= sizeof(struct ext4_dir_entry_tail
);
1520 bh2
= ext4_append (handle
, dir
, &newblock
, &err
);
1527 BUFFER_TRACE(*bh
, "get_write_access");
1528 err
= ext4_journal_get_write_access(handle
, *bh
);
1532 BUFFER_TRACE(frame
->bh
, "get_write_access");
1533 err
= ext4_journal_get_write_access(handle
, frame
->bh
);
1537 data2
= bh2
->b_data
;
1539 /* create map in the end of data2 block */
1540 map
= (struct dx_map_entry
*) (data2
+ blocksize
);
1541 count
= dx_make_map((struct ext4_dir_entry_2
*) data1
,
1542 blocksize
, hinfo
, map
);
1544 dx_sort_map(map
, count
);
1545 /* Split the existing block in the middle, size-wise */
1548 for (i
= count
-1; i
>= 0; i
--) {
1549 /* is more than half of this entry in 2nd half of the block? */
1550 if (size
+ map
[i
].size
/2 > blocksize
/2)
1552 size
+= map
[i
].size
;
1555 /* map index at which we will split */
1556 split
= count
- move
;
1557 hash2
= map
[split
].hash
;
1558 continued
= hash2
== map
[split
- 1].hash
;
1559 dxtrace(printk(KERN_INFO
"Split block %lu at %x, %i/%i\n",
1560 (unsigned long)dx_get_block(frame
->at
),
1561 hash2
, split
, count
-split
));
1563 /* Fancy dance to stay within two buffers */
1564 de2
= dx_move_dirents(data1
, data2
, map
+ split
, count
- split
, blocksize
);
1565 de
= dx_pack_dirents(data1
, blocksize
);
1566 de
->rec_len
= ext4_rec_len_to_disk(data1
+ (blocksize
- csum_size
) -
1569 de2
->rec_len
= ext4_rec_len_to_disk(data2
+ (blocksize
- csum_size
) -
1573 t
= EXT4_DIRENT_TAIL(data2
, blocksize
);
1574 initialize_dirent_tail(t
, blocksize
);
1576 t
= EXT4_DIRENT_TAIL(data1
, blocksize
);
1577 initialize_dirent_tail(t
, blocksize
);
1580 dxtrace(dx_show_leaf (hinfo
, (struct ext4_dir_entry_2
*) data1
, blocksize
, 1));
1581 dxtrace(dx_show_leaf (hinfo
, (struct ext4_dir_entry_2
*) data2
, blocksize
, 1));
1583 /* Which block gets the new entry? */
1584 if (hinfo
->hash
>= hash2
)
1589 dx_insert_block(frame
, hash2
+ continued
, newblock
);
1590 err
= ext4_handle_dirty_dirent_node(handle
, dir
, bh2
);
1593 err
= ext4_handle_dirty_dx_node(handle
, dir
, frame
->bh
);
1597 dxtrace(dx_show_index("frame", frame
->entries
));
1604 ext4_std_error(dir
->i_sb
, err
);
1610 int ext4_find_dest_de(struct inode
*dir
, struct inode
*inode
,
1611 struct buffer_head
*bh
,
1612 void *buf
, int buf_size
,
1613 const char *name
, int namelen
,
1614 struct ext4_dir_entry_2
**dest_de
)
1616 struct ext4_dir_entry_2
*de
;
1617 unsigned short reclen
= EXT4_DIR_REC_LEN(namelen
);
1619 unsigned int offset
= 0;
1622 de
= (struct ext4_dir_entry_2
*)buf
;
1623 top
= buf
+ buf_size
- reclen
;
1624 while ((char *) de
<= top
) {
1625 if (ext4_check_dir_entry(dir
, NULL
, de
, bh
,
1626 buf
, buf_size
, offset
))
1628 if (ext4_match(namelen
, name
, de
))
1630 nlen
= EXT4_DIR_REC_LEN(de
->name_len
);
1631 rlen
= ext4_rec_len_from_disk(de
->rec_len
, buf_size
);
1632 if ((de
->inode
? rlen
- nlen
: rlen
) >= reclen
)
1634 de
= (struct ext4_dir_entry_2
*)((char *)de
+ rlen
);
1637 if ((char *) de
> top
)
1644 void ext4_insert_dentry(struct inode
*inode
,
1645 struct ext4_dir_entry_2
*de
,
1647 const char *name
, int namelen
)
1652 nlen
= EXT4_DIR_REC_LEN(de
->name_len
);
1653 rlen
= ext4_rec_len_from_disk(de
->rec_len
, buf_size
);
1655 struct ext4_dir_entry_2
*de1
=
1656 (struct ext4_dir_entry_2
*)((char *)de
+ nlen
);
1657 de1
->rec_len
= ext4_rec_len_to_disk(rlen
- nlen
, buf_size
);
1658 de
->rec_len
= ext4_rec_len_to_disk(nlen
, buf_size
);
1661 de
->file_type
= EXT4_FT_UNKNOWN
;
1662 de
->inode
= cpu_to_le32(inode
->i_ino
);
1663 ext4_set_de_type(inode
->i_sb
, de
, inode
->i_mode
);
1664 de
->name_len
= namelen
;
1665 memcpy(de
->name
, name
, namelen
);
1668 * Add a new entry into a directory (leaf) block. If de is non-NULL,
1669 * it points to a directory entry which is guaranteed to be large
1670 * enough for new directory entry. If de is NULL, then
1671 * add_dirent_to_buf will attempt search the directory block for
1672 * space. It will return -ENOSPC if no space is available, and -EIO
1673 * and -EEXIST if directory entry already exists.
1675 static int add_dirent_to_buf(handle_t
*handle
, struct dentry
*dentry
,
1676 struct inode
*inode
, struct ext4_dir_entry_2
*de
,
1677 struct buffer_head
*bh
)
1679 struct inode
*dir
= dentry
->d_parent
->d_inode
;
1680 const char *name
= dentry
->d_name
.name
;
1681 int namelen
= dentry
->d_name
.len
;
1682 unsigned int blocksize
= dir
->i_sb
->s_blocksize
;
1683 unsigned short reclen
;
1687 if (EXT4_HAS_RO_COMPAT_FEATURE(inode
->i_sb
,
1688 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
1689 csum_size
= sizeof(struct ext4_dir_entry_tail
);
1691 reclen
= EXT4_DIR_REC_LEN(namelen
);
1693 err
= ext4_find_dest_de(dir
, inode
,
1694 bh
, bh
->b_data
, blocksize
- csum_size
,
1695 name
, namelen
, &de
);
1699 BUFFER_TRACE(bh
, "get_write_access");
1700 err
= ext4_journal_get_write_access(handle
, bh
);
1702 ext4_std_error(dir
->i_sb
, err
);
1706 /* By now the buffer is marked for journaling */
1707 ext4_insert_dentry(inode
, de
, blocksize
, name
, namelen
);
1710 * XXX shouldn't update any times until successful
1711 * completion of syscall, but too many callers depend
1714 * XXX similarly, too many callers depend on
1715 * ext4_new_inode() setting the times, but error
1716 * recovery deletes the inode, so the worst that can
1717 * happen is that the times are slightly out of date
1718 * and/or different from the directory change time.
1720 dir
->i_mtime
= dir
->i_ctime
= ext4_current_time(dir
);
1721 ext4_update_dx_flag(dir
);
1723 ext4_mark_inode_dirty(handle
, dir
);
1724 BUFFER_TRACE(bh
, "call ext4_handle_dirty_metadata");
1725 err
= ext4_handle_dirty_dirent_node(handle
, dir
, bh
);
1727 ext4_std_error(dir
->i_sb
, err
);
1732 * This converts a one block unindexed directory to a 3 block indexed
1733 * directory, and adds the dentry to the indexed directory.
1735 static int make_indexed_dir(handle_t
*handle
, struct dentry
*dentry
,
1736 struct inode
*inode
, struct buffer_head
*bh
)
1738 struct inode
*dir
= dentry
->d_parent
->d_inode
;
1739 const char *name
= dentry
->d_name
.name
;
1740 int namelen
= dentry
->d_name
.len
;
1741 struct buffer_head
*bh2
;
1742 struct dx_root
*root
;
1743 struct dx_frame frames
[2], *frame
;
1744 struct dx_entry
*entries
;
1745 struct ext4_dir_entry_2
*de
, *de2
;
1746 struct ext4_dir_entry_tail
*t
;
1751 struct dx_hash_info hinfo
;
1753 struct fake_dirent
*fde
;
1756 if (EXT4_HAS_RO_COMPAT_FEATURE(inode
->i_sb
,
1757 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
1758 csum_size
= sizeof(struct ext4_dir_entry_tail
);
1760 blocksize
= dir
->i_sb
->s_blocksize
;
1761 dxtrace(printk(KERN_DEBUG
"Creating index: inode %lu\n", dir
->i_ino
));
1762 retval
= ext4_journal_get_write_access(handle
, bh
);
1764 ext4_std_error(dir
->i_sb
, retval
);
1768 root
= (struct dx_root
*) bh
->b_data
;
1770 /* The 0th block becomes the root, move the dirents out */
1771 fde
= &root
->dotdot
;
1772 de
= (struct ext4_dir_entry_2
*)((char *)fde
+
1773 ext4_rec_len_from_disk(fde
->rec_len
, blocksize
));
1774 if ((char *) de
>= (((char *) root
) + blocksize
)) {
1775 EXT4_ERROR_INODE(dir
, "invalid rec_len for '..'");
1779 len
= ((char *) root
) + (blocksize
- csum_size
) - (char *) de
;
1781 /* Allocate new block for the 0th block's dirents */
1782 bh2
= ext4_append(handle
, dir
, &block
, &retval
);
1787 ext4_set_inode_flag(dir
, EXT4_INODE_INDEX
);
1788 data1
= bh2
->b_data
;
1790 memcpy (data1
, de
, len
);
1791 de
= (struct ext4_dir_entry_2
*) data1
;
1793 while ((char *)(de2
= ext4_next_entry(de
, blocksize
)) < top
)
1795 de
->rec_len
= ext4_rec_len_to_disk(data1
+ (blocksize
- csum_size
) -
1800 t
= EXT4_DIRENT_TAIL(data1
, blocksize
);
1801 initialize_dirent_tail(t
, blocksize
);
1804 /* Initialize the root; the dot dirents already exist */
1805 de
= (struct ext4_dir_entry_2
*) (&root
->dotdot
);
1806 de
->rec_len
= ext4_rec_len_to_disk(blocksize
- EXT4_DIR_REC_LEN(2),
1808 memset (&root
->info
, 0, sizeof(root
->info
));
1809 root
->info
.info_length
= sizeof(root
->info
);
1810 root
->info
.hash_version
= EXT4_SB(dir
->i_sb
)->s_def_hash_version
;
1811 entries
= root
->entries
;
1812 dx_set_block(entries
, 1);
1813 dx_set_count(entries
, 1);
1814 dx_set_limit(entries
, dx_root_limit(dir
, sizeof(root
->info
)));
1816 /* Initialize as for dx_probe */
1817 hinfo
.hash_version
= root
->info
.hash_version
;
1818 if (hinfo
.hash_version
<= DX_HASH_TEA
)
1819 hinfo
.hash_version
+= EXT4_SB(dir
->i_sb
)->s_hash_unsigned
;
1820 hinfo
.seed
= EXT4_SB(dir
->i_sb
)->s_hash_seed
;
1821 ext4fs_dirhash(name
, namelen
, &hinfo
);
1823 frame
->entries
= entries
;
1824 frame
->at
= entries
;
1828 ext4_handle_dirty_dx_node(handle
, dir
, frame
->bh
);
1829 ext4_handle_dirty_dirent_node(handle
, dir
, bh
);
1831 de
= do_split(handle
,dir
, &bh
, frame
, &hinfo
, &retval
);
1834 * Even if the block split failed, we have to properly write
1835 * out all the changes we did so far. Otherwise we can end up
1836 * with corrupted filesystem.
1838 ext4_mark_inode_dirty(handle
, dir
);
1844 retval
= add_dirent_to_buf(handle
, dentry
, inode
, de
, bh
);
1852 * adds a file entry to the specified directory, using the same
1853 * semantics as ext4_find_entry(). It returns NULL if it failed.
1855 * NOTE!! The inode part of 'de' is left at 0 - which means you
1856 * may not sleep between calling this and putting something into
1857 * the entry, as someone else might have used it while you slept.
1859 static int ext4_add_entry(handle_t
*handle
, struct dentry
*dentry
,
1860 struct inode
*inode
)
1862 struct inode
*dir
= dentry
->d_parent
->d_inode
;
1863 struct buffer_head
*bh
;
1864 struct ext4_dir_entry_2
*de
;
1865 struct ext4_dir_entry_tail
*t
;
1866 struct super_block
*sb
;
1870 ext4_lblk_t block
, blocks
;
1873 if (EXT4_HAS_RO_COMPAT_FEATURE(inode
->i_sb
,
1874 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
1875 csum_size
= sizeof(struct ext4_dir_entry_tail
);
1878 blocksize
= sb
->s_blocksize
;
1879 if (!dentry
->d_name
.len
)
1882 retval
= ext4_dx_add_entry(handle
, dentry
, inode
);
1883 if (!retval
|| (retval
!= ERR_BAD_DX_DIR
))
1885 ext4_clear_inode_flag(dir
, EXT4_INODE_INDEX
);
1887 ext4_mark_inode_dirty(handle
, dir
);
1889 blocks
= dir
->i_size
>> sb
->s_blocksize_bits
;
1890 for (block
= 0; block
< blocks
; block
++) {
1891 if (!(bh
= ext4_bread(handle
, dir
, block
, 0, &retval
))) {
1894 ext4_error(inode
->i_sb
,
1895 "Directory hole detected on inode %lu\n",
1900 if (!buffer_verified(bh
) &&
1901 !ext4_dirent_csum_verify(dir
,
1902 (struct ext4_dir_entry
*)bh
->b_data
))
1904 set_buffer_verified(bh
);
1905 retval
= add_dirent_to_buf(handle
, dentry
, inode
, NULL
, bh
);
1906 if (retval
!= -ENOSPC
) {
1911 if (blocks
== 1 && !dx_fallback
&&
1912 EXT4_HAS_COMPAT_FEATURE(sb
, EXT4_FEATURE_COMPAT_DIR_INDEX
))
1913 return make_indexed_dir(handle
, dentry
, inode
, bh
);
1916 bh
= ext4_append(handle
, dir
, &block
, &retval
);
1919 de
= (struct ext4_dir_entry_2
*) bh
->b_data
;
1921 de
->rec_len
= ext4_rec_len_to_disk(blocksize
- csum_size
, blocksize
);
1924 t
= EXT4_DIRENT_TAIL(bh
->b_data
, blocksize
);
1925 initialize_dirent_tail(t
, blocksize
);
1928 retval
= add_dirent_to_buf(handle
, dentry
, inode
, de
, bh
);
1931 ext4_set_inode_state(inode
, EXT4_STATE_NEWENTRY
);
1936 * Returns 0 for success, or a negative error value
1938 static int ext4_dx_add_entry(handle_t
*handle
, struct dentry
*dentry
,
1939 struct inode
*inode
)
1941 struct dx_frame frames
[2], *frame
;
1942 struct dx_entry
*entries
, *at
;
1943 struct dx_hash_info hinfo
;
1944 struct buffer_head
*bh
;
1945 struct inode
*dir
= dentry
->d_parent
->d_inode
;
1946 struct super_block
*sb
= dir
->i_sb
;
1947 struct ext4_dir_entry_2
*de
;
1950 frame
= dx_probe(&dentry
->d_name
, dir
, &hinfo
, frames
, &err
);
1953 entries
= frame
->entries
;
1956 if (!(bh
= ext4_bread(handle
, dir
, dx_get_block(frame
->at
), 0, &err
))) {
1959 ext4_error(dir
->i_sb
,
1960 "Directory hole detected on inode %lu\n",
1966 if (!buffer_verified(bh
) &&
1967 !ext4_dirent_csum_verify(dir
, (struct ext4_dir_entry
*)bh
->b_data
))
1969 set_buffer_verified(bh
);
1971 BUFFER_TRACE(bh
, "get_write_access");
1972 err
= ext4_journal_get_write_access(handle
, bh
);
1976 err
= add_dirent_to_buf(handle
, dentry
, inode
, NULL
, bh
);
1980 /* Block full, should compress but for now just split */
1981 dxtrace(printk(KERN_DEBUG
"using %u of %u node entries\n",
1982 dx_get_count(entries
), dx_get_limit(entries
)));
1983 /* Need to split index? */
1984 if (dx_get_count(entries
) == dx_get_limit(entries
)) {
1985 ext4_lblk_t newblock
;
1986 unsigned icount
= dx_get_count(entries
);
1987 int levels
= frame
- frames
;
1988 struct dx_entry
*entries2
;
1989 struct dx_node
*node2
;
1990 struct buffer_head
*bh2
;
1992 if (levels
&& (dx_get_count(frames
->entries
) ==
1993 dx_get_limit(frames
->entries
))) {
1994 ext4_warning(sb
, "Directory index full!");
1998 bh2
= ext4_append (handle
, dir
, &newblock
, &err
);
2001 node2
= (struct dx_node
*)(bh2
->b_data
);
2002 entries2
= node2
->entries
;
2003 memset(&node2
->fake
, 0, sizeof(struct fake_dirent
));
2004 node2
->fake
.rec_len
= ext4_rec_len_to_disk(sb
->s_blocksize
,
2006 BUFFER_TRACE(frame
->bh
, "get_write_access");
2007 err
= ext4_journal_get_write_access(handle
, frame
->bh
);
2011 unsigned icount1
= icount
/2, icount2
= icount
- icount1
;
2012 unsigned hash2
= dx_get_hash(entries
+ icount1
);
2013 dxtrace(printk(KERN_DEBUG
"Split index %i/%i\n",
2016 BUFFER_TRACE(frame
->bh
, "get_write_access"); /* index root */
2017 err
= ext4_journal_get_write_access(handle
,
2022 memcpy((char *) entries2
, (char *) (entries
+ icount1
),
2023 icount2
* sizeof(struct dx_entry
));
2024 dx_set_count(entries
, icount1
);
2025 dx_set_count(entries2
, icount2
);
2026 dx_set_limit(entries2
, dx_node_limit(dir
));
2028 /* Which index block gets the new entry? */
2029 if (at
- entries
>= icount1
) {
2030 frame
->at
= at
= at
- entries
- icount1
+ entries2
;
2031 frame
->entries
= entries
= entries2
;
2032 swap(frame
->bh
, bh2
);
2034 dx_insert_block(frames
+ 0, hash2
, newblock
);
2035 dxtrace(dx_show_index("node", frames
[1].entries
));
2036 dxtrace(dx_show_index("node",
2037 ((struct dx_node
*) bh2
->b_data
)->entries
));
2038 err
= ext4_handle_dirty_dx_node(handle
, dir
, bh2
);
2043 dxtrace(printk(KERN_DEBUG
2044 "Creating second level index...\n"));
2045 memcpy((char *) entries2
, (char *) entries
,
2046 icount
* sizeof(struct dx_entry
));
2047 dx_set_limit(entries2
, dx_node_limit(dir
));
2050 dx_set_count(entries
, 1);
2051 dx_set_block(entries
+ 0, newblock
);
2052 ((struct dx_root
*) frames
[0].bh
->b_data
)->info
.indirect_levels
= 1;
2054 /* Add new access path frame */
2056 frame
->at
= at
= at
- entries
+ entries2
;
2057 frame
->entries
= entries
= entries2
;
2059 err
= ext4_journal_get_write_access(handle
,
2064 err
= ext4_handle_dirty_dx_node(handle
, dir
, frames
[0].bh
);
2066 ext4_std_error(inode
->i_sb
, err
);
2070 de
= do_split(handle
, dir
, &bh
, frame
, &hinfo
, &err
);
2073 err
= add_dirent_to_buf(handle
, dentry
, inode
, de
, bh
);
2077 ext4_std_error(dir
->i_sb
, err
);
2086 * ext4_delete_entry deletes a directory entry by merging it with the
2089 static int ext4_delete_entry(handle_t
*handle
,
2091 struct ext4_dir_entry_2
*de_del
,
2092 struct buffer_head
*bh
)
2094 struct ext4_dir_entry_2
*de
, *pde
;
2095 unsigned int blocksize
= dir
->i_sb
->s_blocksize
;
2099 if (EXT4_HAS_RO_COMPAT_FEATURE(dir
->i_sb
,
2100 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
2101 csum_size
= sizeof(struct ext4_dir_entry_tail
);
2105 de
= (struct ext4_dir_entry_2
*) bh
->b_data
;
2106 while (i
< bh
->b_size
- csum_size
) {
2107 if (ext4_check_dir_entry(dir
, NULL
, de
, bh
,
2108 bh
->b_data
, bh
->b_size
, i
))
2111 BUFFER_TRACE(bh
, "get_write_access");
2112 err
= ext4_journal_get_write_access(handle
, bh
);
2113 if (unlikely(err
)) {
2114 ext4_std_error(dir
->i_sb
, err
);
2118 pde
->rec_len
= ext4_rec_len_to_disk(
2119 ext4_rec_len_from_disk(pde
->rec_len
,
2121 ext4_rec_len_from_disk(de
->rec_len
,
2127 BUFFER_TRACE(bh
, "call ext4_handle_dirty_metadata");
2128 err
= ext4_handle_dirty_dirent_node(handle
, dir
, bh
);
2129 if (unlikely(err
)) {
2130 ext4_std_error(dir
->i_sb
, err
);
2135 i
+= ext4_rec_len_from_disk(de
->rec_len
, blocksize
);
2137 de
= ext4_next_entry(de
, blocksize
);
2143 * DIR_NLINK feature is set if 1) nlinks > EXT4_LINK_MAX or 2) nlinks == 2,
2144 * since this indicates that nlinks count was previously 1.
2146 static void ext4_inc_count(handle_t
*handle
, struct inode
*inode
)
2149 if (is_dx(inode
) && inode
->i_nlink
> 1) {
2150 /* limit is 16-bit i_links_count */
2151 if (inode
->i_nlink
>= EXT4_LINK_MAX
|| inode
->i_nlink
== 2) {
2152 set_nlink(inode
, 1);
2153 EXT4_SET_RO_COMPAT_FEATURE(inode
->i_sb
,
2154 EXT4_FEATURE_RO_COMPAT_DIR_NLINK
);
2160 * If a directory had nlink == 1, then we should let it be 1. This indicates
2161 * directory has >EXT4_LINK_MAX subdirs.
2163 static void ext4_dec_count(handle_t
*handle
, struct inode
*inode
)
2165 if (!S_ISDIR(inode
->i_mode
) || inode
->i_nlink
> 2)
2170 static int ext4_add_nondir(handle_t
*handle
,
2171 struct dentry
*dentry
, struct inode
*inode
)
2173 int err
= ext4_add_entry(handle
, dentry
, inode
);
2175 ext4_mark_inode_dirty(handle
, inode
);
2176 unlock_new_inode(inode
);
2177 d_instantiate(dentry
, inode
);
2181 unlock_new_inode(inode
);
2187 * By the time this is called, we already have created
2188 * the directory cache entry for the new file, but it
2189 * is so far negative - it has no inode.
2191 * If the create succeeds, we fill in the inode information
2192 * with d_instantiate().
2194 static int ext4_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
2198 struct inode
*inode
;
2199 int err
, retries
= 0;
2201 dquot_initialize(dir
);
2204 handle
= ext4_journal_start(dir
, EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
2205 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 3 +
2206 EXT4_MAXQUOTAS_INIT_BLOCKS(dir
->i_sb
));
2208 return PTR_ERR(handle
);
2210 if (IS_DIRSYNC(dir
))
2211 ext4_handle_sync(handle
);
2213 inode
= ext4_new_inode(handle
, dir
, mode
, &dentry
->d_name
, 0, NULL
);
2214 err
= PTR_ERR(inode
);
2215 if (!IS_ERR(inode
)) {
2216 inode
->i_op
= &ext4_file_inode_operations
;
2217 inode
->i_fop
= &ext4_file_operations
;
2218 ext4_set_aops(inode
);
2219 err
= ext4_add_nondir(handle
, dentry
, inode
);
2221 ext4_journal_stop(handle
);
2222 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
2227 static int ext4_mknod(struct inode
*dir
, struct dentry
*dentry
,
2228 umode_t mode
, dev_t rdev
)
2231 struct inode
*inode
;
2232 int err
, retries
= 0;
2234 if (!new_valid_dev(rdev
))
2237 dquot_initialize(dir
);
2240 handle
= ext4_journal_start(dir
, EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
2241 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 3 +
2242 EXT4_MAXQUOTAS_INIT_BLOCKS(dir
->i_sb
));
2244 return PTR_ERR(handle
);
2246 if (IS_DIRSYNC(dir
))
2247 ext4_handle_sync(handle
);
2249 inode
= ext4_new_inode(handle
, dir
, mode
, &dentry
->d_name
, 0, NULL
);
2250 err
= PTR_ERR(inode
);
2251 if (!IS_ERR(inode
)) {
2252 init_special_inode(inode
, inode
->i_mode
, rdev
);
2253 inode
->i_op
= &ext4_special_inode_operations
;
2254 err
= ext4_add_nondir(handle
, dentry
, inode
);
2256 ext4_journal_stop(handle
);
2257 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
2262 struct ext4_dir_entry_2
*ext4_init_dot_dotdot(struct inode
*inode
,
2263 struct ext4_dir_entry_2
*de
,
2264 int blocksize
, int csum_size
,
2265 unsigned int parent_ino
, int dotdot_real_len
)
2267 de
->inode
= cpu_to_le32(inode
->i_ino
);
2269 de
->rec_len
= ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de
->name_len
),
2271 strcpy(de
->name
, ".");
2272 ext4_set_de_type(inode
->i_sb
, de
, S_IFDIR
);
2274 de
= ext4_next_entry(de
, blocksize
);
2275 de
->inode
= cpu_to_le32(parent_ino
);
2277 if (!dotdot_real_len
)
2278 de
->rec_len
= ext4_rec_len_to_disk(blocksize
-
2279 (csum_size
+ EXT4_DIR_REC_LEN(1)),
2282 de
->rec_len
= ext4_rec_len_to_disk(
2283 EXT4_DIR_REC_LEN(de
->name_len
), blocksize
);
2284 strcpy(de
->name
, "..");
2285 ext4_set_de_type(inode
->i_sb
, de
, S_IFDIR
);
2287 return ext4_next_entry(de
, blocksize
);
2290 static int ext4_init_new_dir(handle_t
*handle
, struct inode
*dir
,
2291 struct inode
*inode
)
2293 struct buffer_head
*dir_block
= NULL
;
2294 struct ext4_dir_entry_2
*de
;
2295 struct ext4_dir_entry_tail
*t
;
2296 unsigned int blocksize
= dir
->i_sb
->s_blocksize
;
2300 if (EXT4_HAS_RO_COMPAT_FEATURE(dir
->i_sb
,
2301 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
2302 csum_size
= sizeof(struct ext4_dir_entry_tail
);
2304 inode
->i_size
= EXT4_I(inode
)->i_disksize
= blocksize
;
2305 dir_block
= ext4_bread(handle
, inode
, 0, 1, &err
);
2306 if (!(dir_block
= ext4_bread(handle
, inode
, 0, 1, &err
))) {
2309 ext4_error(inode
->i_sb
,
2310 "Directory hole detected on inode %lu\n",
2315 BUFFER_TRACE(dir_block
, "get_write_access");
2316 err
= ext4_journal_get_write_access(handle
, dir_block
);
2319 de
= (struct ext4_dir_entry_2
*)dir_block
->b_data
;
2320 ext4_init_dot_dotdot(inode
, de
, blocksize
, csum_size
, dir
->i_ino
, 0);
2321 set_nlink(inode
, 2);
2323 t
= EXT4_DIRENT_TAIL(dir_block
->b_data
, blocksize
);
2324 initialize_dirent_tail(t
, blocksize
);
2327 BUFFER_TRACE(dir_block
, "call ext4_handle_dirty_metadata");
2328 err
= ext4_handle_dirty_dirent_node(handle
, inode
, dir_block
);
2331 set_buffer_verified(dir_block
);
2337 static int ext4_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
2340 struct inode
*inode
;
2341 int err
, retries
= 0;
2343 if (EXT4_DIR_LINK_MAX(dir
))
2346 dquot_initialize(dir
);
2349 handle
= ext4_journal_start(dir
, EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
2350 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 3 +
2351 EXT4_MAXQUOTAS_INIT_BLOCKS(dir
->i_sb
));
2353 return PTR_ERR(handle
);
2355 if (IS_DIRSYNC(dir
))
2356 ext4_handle_sync(handle
);
2358 inode
= ext4_new_inode(handle
, dir
, S_IFDIR
| mode
,
2359 &dentry
->d_name
, 0, NULL
);
2360 err
= PTR_ERR(inode
);
2364 inode
->i_op
= &ext4_dir_inode_operations
;
2365 inode
->i_fop
= &ext4_dir_operations
;
2366 err
= ext4_init_new_dir(handle
, dir
, inode
);
2368 goto out_clear_inode
;
2369 err
= ext4_mark_inode_dirty(handle
, inode
);
2371 err
= ext4_add_entry(handle
, dentry
, inode
);
2375 unlock_new_inode(inode
);
2376 ext4_mark_inode_dirty(handle
, inode
);
2380 ext4_inc_count(handle
, dir
);
2381 ext4_update_dx_flag(dir
);
2382 err
= ext4_mark_inode_dirty(handle
, dir
);
2384 goto out_clear_inode
;
2385 unlock_new_inode(inode
);
2386 d_instantiate(dentry
, inode
);
2388 ext4_journal_stop(handle
);
2389 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
2395 * routine to check that the specified directory is empty (for rmdir)
2397 static int empty_dir(struct inode
*inode
)
2399 unsigned int offset
;
2400 struct buffer_head
*bh
;
2401 struct ext4_dir_entry_2
*de
, *de1
;
2402 struct super_block
*sb
;
2406 if (inode
->i_size
< EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2) ||
2407 !(bh
= ext4_bread(NULL
, inode
, 0, 0, &err
))) {
2409 EXT4_ERROR_INODE(inode
,
2410 "error %d reading directory lblock 0", err
);
2412 ext4_warning(inode
->i_sb
,
2413 "bad directory (dir #%lu) - no data block",
2417 if (!buffer_verified(bh
) &&
2418 !ext4_dirent_csum_verify(inode
,
2419 (struct ext4_dir_entry
*)bh
->b_data
)) {
2420 EXT4_ERROR_INODE(inode
, "checksum error reading directory "
2424 set_buffer_verified(bh
);
2425 de
= (struct ext4_dir_entry_2
*) bh
->b_data
;
2426 de1
= ext4_next_entry(de
, sb
->s_blocksize
);
2427 if (le32_to_cpu(de
->inode
) != inode
->i_ino
||
2428 !le32_to_cpu(de1
->inode
) ||
2429 strcmp(".", de
->name
) ||
2430 strcmp("..", de1
->name
)) {
2431 ext4_warning(inode
->i_sb
,
2432 "bad directory (dir #%lu) - no `.' or `..'",
2437 offset
= ext4_rec_len_from_disk(de
->rec_len
, sb
->s_blocksize
) +
2438 ext4_rec_len_from_disk(de1
->rec_len
, sb
->s_blocksize
);
2439 de
= ext4_next_entry(de1
, sb
->s_blocksize
);
2440 while (offset
< inode
->i_size
) {
2442 (void *) de
>= (void *) (bh
->b_data
+sb
->s_blocksize
)) {
2443 unsigned int lblock
;
2446 lblock
= offset
>> EXT4_BLOCK_SIZE_BITS(sb
);
2447 bh
= ext4_bread(NULL
, inode
, lblock
, 0, &err
);
2450 EXT4_ERROR_INODE(inode
,
2451 "error %d reading directory "
2452 "lblock %u", err
, lblock
);
2454 ext4_warning(inode
->i_sb
,
2455 "bad directory (dir #%lu) - no data block",
2458 offset
+= sb
->s_blocksize
;
2461 if (!buffer_verified(bh
) &&
2462 !ext4_dirent_csum_verify(inode
,
2463 (struct ext4_dir_entry
*)bh
->b_data
)) {
2464 EXT4_ERROR_INODE(inode
, "checksum error "
2465 "reading directory lblock 0");
2468 set_buffer_verified(bh
);
2469 de
= (struct ext4_dir_entry_2
*) bh
->b_data
;
2471 if (ext4_check_dir_entry(inode
, NULL
, de
, bh
,
2472 bh
->b_data
, bh
->b_size
, offset
)) {
2473 de
= (struct ext4_dir_entry_2
*)(bh
->b_data
+
2475 offset
= (offset
| (sb
->s_blocksize
- 1)) + 1;
2478 if (le32_to_cpu(de
->inode
)) {
2482 offset
+= ext4_rec_len_from_disk(de
->rec_len
, sb
->s_blocksize
);
2483 de
= ext4_next_entry(de
, sb
->s_blocksize
);
2489 /* ext4_orphan_add() links an unlinked or truncated inode into a list of
2490 * such inodes, starting at the superblock, in case we crash before the
2491 * file is closed/deleted, or in case the inode truncate spans multiple
2492 * transactions and the last transaction is not recovered after a crash.
2494 * At filesystem recovery time, we walk this list deleting unlinked
2495 * inodes and truncating linked inodes in ext4_orphan_cleanup().
2497 int ext4_orphan_add(handle_t
*handle
, struct inode
*inode
)
2499 struct super_block
*sb
= inode
->i_sb
;
2500 struct ext4_iloc iloc
;
2503 if (!EXT4_SB(sb
)->s_journal
)
2506 mutex_lock(&EXT4_SB(sb
)->s_orphan_lock
);
2507 if (!list_empty(&EXT4_I(inode
)->i_orphan
))
2511 * Orphan handling is only valid for files with data blocks
2512 * being truncated, or files being unlinked. Note that we either
2513 * hold i_mutex, or the inode can not be referenced from outside,
2514 * so i_nlink should not be bumped due to race
2516 J_ASSERT((S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
2517 S_ISLNK(inode
->i_mode
)) || inode
->i_nlink
== 0);
2519 BUFFER_TRACE(EXT4_SB(sb
)->s_sbh
, "get_write_access");
2520 err
= ext4_journal_get_write_access(handle
, EXT4_SB(sb
)->s_sbh
);
2524 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
2528 * Due to previous errors inode may be already a part of on-disk
2529 * orphan list. If so skip on-disk list modification.
2531 if (NEXT_ORPHAN(inode
) && NEXT_ORPHAN(inode
) <=
2532 (le32_to_cpu(EXT4_SB(sb
)->s_es
->s_inodes_count
)))
2535 /* Insert this inode at the head of the on-disk orphan list... */
2536 NEXT_ORPHAN(inode
) = le32_to_cpu(EXT4_SB(sb
)->s_es
->s_last_orphan
);
2537 EXT4_SB(sb
)->s_es
->s_last_orphan
= cpu_to_le32(inode
->i_ino
);
2538 err
= ext4_handle_dirty_super(handle
, sb
);
2539 rc
= ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
2543 /* Only add to the head of the in-memory list if all the
2544 * previous operations succeeded. If the orphan_add is going to
2545 * fail (possibly taking the journal offline), we can't risk
2546 * leaving the inode on the orphan list: stray orphan-list
2547 * entries can cause panics at unmount time.
2549 * This is safe: on error we're going to ignore the orphan list
2550 * anyway on the next recovery. */
2553 list_add(&EXT4_I(inode
)->i_orphan
, &EXT4_SB(sb
)->s_orphan
);
2555 jbd_debug(4, "superblock will point to %lu\n", inode
->i_ino
);
2556 jbd_debug(4, "orphan inode %lu will point to %d\n",
2557 inode
->i_ino
, NEXT_ORPHAN(inode
));
2559 mutex_unlock(&EXT4_SB(sb
)->s_orphan_lock
);
2560 ext4_std_error(inode
->i_sb
, err
);
2565 * ext4_orphan_del() removes an unlinked or truncated inode from the list
2566 * of such inodes stored on disk, because it is finally being cleaned up.
2568 int ext4_orphan_del(handle_t
*handle
, struct inode
*inode
)
2570 struct list_head
*prev
;
2571 struct ext4_inode_info
*ei
= EXT4_I(inode
);
2572 struct ext4_sb_info
*sbi
;
2574 struct ext4_iloc iloc
;
2577 if (!EXT4_SB(inode
->i_sb
)->s_journal
)
2580 mutex_lock(&EXT4_SB(inode
->i_sb
)->s_orphan_lock
);
2581 if (list_empty(&ei
->i_orphan
))
2584 ino_next
= NEXT_ORPHAN(inode
);
2585 prev
= ei
->i_orphan
.prev
;
2586 sbi
= EXT4_SB(inode
->i_sb
);
2588 jbd_debug(4, "remove inode %lu from orphan list\n", inode
->i_ino
);
2590 list_del_init(&ei
->i_orphan
);
2592 /* If we're on an error path, we may not have a valid
2593 * transaction handle with which to update the orphan list on
2594 * disk, but we still need to remove the inode from the linked
2595 * list in memory. */
2599 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
2603 if (prev
== &sbi
->s_orphan
) {
2604 jbd_debug(4, "superblock will point to %u\n", ino_next
);
2605 BUFFER_TRACE(sbi
->s_sbh
, "get_write_access");
2606 err
= ext4_journal_get_write_access(handle
, sbi
->s_sbh
);
2609 sbi
->s_es
->s_last_orphan
= cpu_to_le32(ino_next
);
2610 err
= ext4_handle_dirty_super(handle
, inode
->i_sb
);
2612 struct ext4_iloc iloc2
;
2613 struct inode
*i_prev
=
2614 &list_entry(prev
, struct ext4_inode_info
, i_orphan
)->vfs_inode
;
2616 jbd_debug(4, "orphan inode %lu will point to %u\n",
2617 i_prev
->i_ino
, ino_next
);
2618 err
= ext4_reserve_inode_write(handle
, i_prev
, &iloc2
);
2621 NEXT_ORPHAN(i_prev
) = ino_next
;
2622 err
= ext4_mark_iloc_dirty(handle
, i_prev
, &iloc2
);
2626 NEXT_ORPHAN(inode
) = 0;
2627 err
= ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
2630 ext4_std_error(inode
->i_sb
, err
);
2632 mutex_unlock(&EXT4_SB(inode
->i_sb
)->s_orphan_lock
);
2640 static int ext4_rmdir(struct inode
*dir
, struct dentry
*dentry
)
2643 struct inode
*inode
;
2644 struct buffer_head
*bh
;
2645 struct ext4_dir_entry_2
*de
;
2648 /* Initialize quotas before so that eventual writes go in
2649 * separate transaction */
2650 dquot_initialize(dir
);
2651 dquot_initialize(dentry
->d_inode
);
2653 handle
= ext4_journal_start(dir
, EXT4_DELETE_TRANS_BLOCKS(dir
->i_sb
));
2655 return PTR_ERR(handle
);
2658 bh
= ext4_find_entry(dir
, &dentry
->d_name
, &de
);
2662 if (IS_DIRSYNC(dir
))
2663 ext4_handle_sync(handle
);
2665 inode
= dentry
->d_inode
;
2668 if (le32_to_cpu(de
->inode
) != inode
->i_ino
)
2671 retval
= -ENOTEMPTY
;
2672 if (!empty_dir(inode
))
2675 retval
= ext4_delete_entry(handle
, dir
, de
, bh
);
2678 if (!EXT4_DIR_LINK_EMPTY(inode
))
2679 ext4_warning(inode
->i_sb
,
2680 "empty directory has too many links (%d)",
2684 /* There's no need to set i_disksize: the fact that i_nlink is
2685 * zero will ensure that the right thing happens during any
2688 ext4_orphan_add(handle
, inode
);
2689 inode
->i_ctime
= dir
->i_ctime
= dir
->i_mtime
= ext4_current_time(inode
);
2690 ext4_mark_inode_dirty(handle
, inode
);
2691 ext4_dec_count(handle
, dir
);
2692 ext4_update_dx_flag(dir
);
2693 ext4_mark_inode_dirty(handle
, dir
);
2696 ext4_journal_stop(handle
);
2701 static int ext4_unlink(struct inode
*dir
, struct dentry
*dentry
)
2704 struct inode
*inode
;
2705 struct buffer_head
*bh
;
2706 struct ext4_dir_entry_2
*de
;
2709 trace_ext4_unlink_enter(dir
, dentry
);
2710 /* Initialize quotas before so that eventual writes go
2711 * in separate transaction */
2712 dquot_initialize(dir
);
2713 dquot_initialize(dentry
->d_inode
);
2715 handle
= ext4_journal_start(dir
, EXT4_DELETE_TRANS_BLOCKS(dir
->i_sb
));
2717 return PTR_ERR(handle
);
2719 if (IS_DIRSYNC(dir
))
2720 ext4_handle_sync(handle
);
2723 bh
= ext4_find_entry(dir
, &dentry
->d_name
, &de
);
2727 inode
= dentry
->d_inode
;
2730 if (le32_to_cpu(de
->inode
) != inode
->i_ino
)
2733 if (!inode
->i_nlink
) {
2734 ext4_warning(inode
->i_sb
,
2735 "Deleting nonexistent file (%lu), %d",
2736 inode
->i_ino
, inode
->i_nlink
);
2737 set_nlink(inode
, 1);
2739 retval
= ext4_delete_entry(handle
, dir
, de
, bh
);
2742 dir
->i_ctime
= dir
->i_mtime
= ext4_current_time(dir
);
2743 ext4_update_dx_flag(dir
);
2744 ext4_mark_inode_dirty(handle
, dir
);
2746 if (!inode
->i_nlink
)
2747 ext4_orphan_add(handle
, inode
);
2748 inode
->i_ctime
= ext4_current_time(inode
);
2749 ext4_mark_inode_dirty(handle
, inode
);
2753 ext4_journal_stop(handle
);
2755 trace_ext4_unlink_exit(dentry
, retval
);
2759 static int ext4_symlink(struct inode
*dir
,
2760 struct dentry
*dentry
, const char *symname
)
2763 struct inode
*inode
;
2764 int l
, err
, retries
= 0;
2767 l
= strlen(symname
)+1;
2768 if (l
> dir
->i_sb
->s_blocksize
)
2769 return -ENAMETOOLONG
;
2771 dquot_initialize(dir
);
2773 if (l
> EXT4_N_BLOCKS
* 4) {
2775 * For non-fast symlinks, we just allocate inode and put it on
2776 * orphan list in the first transaction => we need bitmap,
2777 * group descriptor, sb, inode block, quota blocks, and
2778 * possibly selinux xattr blocks.
2780 credits
= 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir
->i_sb
) +
2781 EXT4_XATTR_TRANS_BLOCKS
;
2784 * Fast symlink. We have to add entry to directory
2785 * (EXT4_DATA_TRANS_BLOCKS + EXT4_INDEX_EXTRA_TRANS_BLOCKS),
2786 * allocate new inode (bitmap, group descriptor, inode block,
2787 * quota blocks, sb is already counted in previous macros).
2789 credits
= EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
2790 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 3 +
2791 EXT4_MAXQUOTAS_INIT_BLOCKS(dir
->i_sb
);
2794 handle
= ext4_journal_start(dir
, credits
);
2796 return PTR_ERR(handle
);
2798 if (IS_DIRSYNC(dir
))
2799 ext4_handle_sync(handle
);
2801 inode
= ext4_new_inode(handle
, dir
, S_IFLNK
|S_IRWXUGO
,
2802 &dentry
->d_name
, 0, NULL
);
2803 err
= PTR_ERR(inode
);
2807 if (l
> EXT4_N_BLOCKS
* 4) {
2808 inode
->i_op
= &ext4_symlink_inode_operations
;
2809 ext4_set_aops(inode
);
2811 * We cannot call page_symlink() with transaction started
2812 * because it calls into ext4_write_begin() which can wait
2813 * for transaction commit if we are running out of space
2814 * and thus we deadlock. So we have to stop transaction now
2815 * and restart it when symlink contents is written.
2817 * To keep fs consistent in case of crash, we have to put inode
2818 * to orphan list in the mean time.
2821 err
= ext4_orphan_add(handle
, inode
);
2822 ext4_journal_stop(handle
);
2824 goto err_drop_inode
;
2825 err
= __page_symlink(inode
, symname
, l
, 1);
2827 goto err_drop_inode
;
2829 * Now inode is being linked into dir (EXT4_DATA_TRANS_BLOCKS
2830 * + EXT4_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
2832 handle
= ext4_journal_start(dir
,
2833 EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
2834 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 1);
2835 if (IS_ERR(handle
)) {
2836 err
= PTR_ERR(handle
);
2837 goto err_drop_inode
;
2839 set_nlink(inode
, 1);
2840 err
= ext4_orphan_del(handle
, inode
);
2842 ext4_journal_stop(handle
);
2844 goto err_drop_inode
;
2847 /* clear the extent format for fast symlink */
2848 ext4_clear_inode_flag(inode
, EXT4_INODE_EXTENTS
);
2849 inode
->i_op
= &ext4_fast_symlink_inode_operations
;
2850 memcpy((char *)&EXT4_I(inode
)->i_data
, symname
, l
);
2851 inode
->i_size
= l
-1;
2853 EXT4_I(inode
)->i_disksize
= inode
->i_size
;
2854 err
= ext4_add_nondir(handle
, dentry
, inode
);
2856 ext4_journal_stop(handle
);
2857 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
2861 unlock_new_inode(inode
);
2866 static int ext4_link(struct dentry
*old_dentry
,
2867 struct inode
*dir
, struct dentry
*dentry
)
2870 struct inode
*inode
= old_dentry
->d_inode
;
2871 int err
, retries
= 0;
2873 if (inode
->i_nlink
>= EXT4_LINK_MAX
)
2876 dquot_initialize(dir
);
2879 handle
= ext4_journal_start(dir
, EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
2880 EXT4_INDEX_EXTRA_TRANS_BLOCKS
);
2882 return PTR_ERR(handle
);
2884 if (IS_DIRSYNC(dir
))
2885 ext4_handle_sync(handle
);
2887 inode
->i_ctime
= ext4_current_time(inode
);
2888 ext4_inc_count(handle
, inode
);
2891 err
= ext4_add_entry(handle
, dentry
, inode
);
2893 ext4_mark_inode_dirty(handle
, inode
);
2894 d_instantiate(dentry
, inode
);
2899 ext4_journal_stop(handle
);
2900 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
2905 #define PARENT_INO(buffer, size) \
2906 (ext4_next_entry((struct ext4_dir_entry_2 *)(buffer), size)->inode)
2909 * Anybody can rename anything with this: the permission checks are left to the
2910 * higher-level routines.
2912 static int ext4_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
2913 struct inode
*new_dir
, struct dentry
*new_dentry
)
2916 struct inode
*old_inode
, *new_inode
;
2917 struct buffer_head
*old_bh
, *new_bh
, *dir_bh
;
2918 struct ext4_dir_entry_2
*old_de
, *new_de
;
2919 int retval
, force_da_alloc
= 0;
2921 dquot_initialize(old_dir
);
2922 dquot_initialize(new_dir
);
2924 old_bh
= new_bh
= dir_bh
= NULL
;
2926 /* Initialize quotas before so that eventual writes go
2927 * in separate transaction */
2928 if (new_dentry
->d_inode
)
2929 dquot_initialize(new_dentry
->d_inode
);
2930 handle
= ext4_journal_start(old_dir
, 2 *
2931 EXT4_DATA_TRANS_BLOCKS(old_dir
->i_sb
) +
2932 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 2);
2934 return PTR_ERR(handle
);
2936 if (IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
))
2937 ext4_handle_sync(handle
);
2939 old_bh
= ext4_find_entry(old_dir
, &old_dentry
->d_name
, &old_de
);
2941 * Check for inode number is _not_ due to possible IO errors.
2942 * We might rmdir the source, keep it as pwd of some process
2943 * and merrily kill the link to whatever was created under the
2944 * same name. Goodbye sticky bit ;-<
2946 old_inode
= old_dentry
->d_inode
;
2948 if (!old_bh
|| le32_to_cpu(old_de
->inode
) != old_inode
->i_ino
)
2951 new_inode
= new_dentry
->d_inode
;
2952 new_bh
= ext4_find_entry(new_dir
, &new_dentry
->d_name
, &new_de
);
2959 if (S_ISDIR(old_inode
->i_mode
)) {
2961 retval
= -ENOTEMPTY
;
2962 if (!empty_dir(new_inode
))
2966 if (!(dir_bh
= ext4_bread(handle
, old_inode
, 0, 0, &retval
))) {
2969 ext4_error(old_inode
->i_sb
,
2970 "Directory hole detected on inode %lu\n",
2975 if (!buffer_verified(dir_bh
) &&
2976 !ext4_dirent_csum_verify(old_inode
,
2977 (struct ext4_dir_entry
*)dir_bh
->b_data
))
2979 set_buffer_verified(dir_bh
);
2980 if (le32_to_cpu(PARENT_INO(dir_bh
->b_data
,
2981 old_dir
->i_sb
->s_blocksize
)) != old_dir
->i_ino
)
2984 if (!new_inode
&& new_dir
!= old_dir
&&
2985 EXT4_DIR_LINK_MAX(new_dir
))
2987 BUFFER_TRACE(dir_bh
, "get_write_access");
2988 retval
= ext4_journal_get_write_access(handle
, dir_bh
);
2993 retval
= ext4_add_entry(handle
, new_dentry
, old_inode
);
2997 BUFFER_TRACE(new_bh
, "get write access");
2998 retval
= ext4_journal_get_write_access(handle
, new_bh
);
3001 new_de
->inode
= cpu_to_le32(old_inode
->i_ino
);
3002 if (EXT4_HAS_INCOMPAT_FEATURE(new_dir
->i_sb
,
3003 EXT4_FEATURE_INCOMPAT_FILETYPE
))
3004 new_de
->file_type
= old_de
->file_type
;
3005 new_dir
->i_version
++;
3006 new_dir
->i_ctime
= new_dir
->i_mtime
=
3007 ext4_current_time(new_dir
);
3008 ext4_mark_inode_dirty(handle
, new_dir
);
3009 BUFFER_TRACE(new_bh
, "call ext4_handle_dirty_metadata");
3010 retval
= ext4_handle_dirty_dirent_node(handle
, new_dir
, new_bh
);
3011 if (unlikely(retval
)) {
3012 ext4_std_error(new_dir
->i_sb
, retval
);
3020 * Like most other Unix systems, set the ctime for inodes on a
3023 old_inode
->i_ctime
= ext4_current_time(old_inode
);
3024 ext4_mark_inode_dirty(handle
, old_inode
);
3029 if (le32_to_cpu(old_de
->inode
) != old_inode
->i_ino
||
3030 old_de
->name_len
!= old_dentry
->d_name
.len
||
3031 strncmp(old_de
->name
, old_dentry
->d_name
.name
, old_de
->name_len
) ||
3032 (retval
= ext4_delete_entry(handle
, old_dir
,
3033 old_de
, old_bh
)) == -ENOENT
) {
3034 /* old_de could have moved from under us during htree split, so
3035 * make sure that we are deleting the right entry. We might
3036 * also be pointing to a stale entry in the unused part of
3037 * old_bh so just checking inum and the name isn't enough. */
3038 struct buffer_head
*old_bh2
;
3039 struct ext4_dir_entry_2
*old_de2
;
3041 old_bh2
= ext4_find_entry(old_dir
, &old_dentry
->d_name
, &old_de2
);
3043 retval
= ext4_delete_entry(handle
, old_dir
,
3049 ext4_warning(old_dir
->i_sb
,
3050 "Deleting old file (%lu), %d, error=%d",
3051 old_dir
->i_ino
, old_dir
->i_nlink
, retval
);
3055 ext4_dec_count(handle
, new_inode
);
3056 new_inode
->i_ctime
= ext4_current_time(new_inode
);
3058 old_dir
->i_ctime
= old_dir
->i_mtime
= ext4_current_time(old_dir
);
3059 ext4_update_dx_flag(old_dir
);
3061 PARENT_INO(dir_bh
->b_data
, new_dir
->i_sb
->s_blocksize
) =
3062 cpu_to_le32(new_dir
->i_ino
);
3063 BUFFER_TRACE(dir_bh
, "call ext4_handle_dirty_metadata");
3064 if (is_dx(old_inode
)) {
3065 retval
= ext4_handle_dirty_dx_node(handle
,
3069 retval
= ext4_handle_dirty_dirent_node(handle
,
3074 ext4_std_error(old_dir
->i_sb
, retval
);
3077 ext4_dec_count(handle
, old_dir
);
3079 /* checked empty_dir above, can't have another parent,
3080 * ext4_dec_count() won't work for many-linked dirs */
3081 clear_nlink(new_inode
);
3083 ext4_inc_count(handle
, new_dir
);
3084 ext4_update_dx_flag(new_dir
);
3085 ext4_mark_inode_dirty(handle
, new_dir
);
3088 ext4_mark_inode_dirty(handle
, old_dir
);
3090 ext4_mark_inode_dirty(handle
, new_inode
);
3091 if (!new_inode
->i_nlink
)
3092 ext4_orphan_add(handle
, new_inode
);
3093 if (!test_opt(new_dir
->i_sb
, NO_AUTO_DA_ALLOC
))
3102 ext4_journal_stop(handle
);
3103 if (retval
== 0 && force_da_alloc
)
3104 ext4_alloc_da_blocks(old_inode
);
3109 * directories can handle most operations...
3111 const struct inode_operations ext4_dir_inode_operations
= {
3112 .create
= ext4_create
,
3113 .lookup
= ext4_lookup
,
3115 .unlink
= ext4_unlink
,
3116 .symlink
= ext4_symlink
,
3117 .mkdir
= ext4_mkdir
,
3118 .rmdir
= ext4_rmdir
,
3119 .mknod
= ext4_mknod
,
3120 .rename
= ext4_rename
,
3121 .setattr
= ext4_setattr
,
3122 #ifdef CONFIG_EXT4_FS_XATTR
3123 .setxattr
= generic_setxattr
,
3124 .getxattr
= generic_getxattr
,
3125 .listxattr
= ext4_listxattr
,
3126 .removexattr
= generic_removexattr
,
3128 .get_acl
= ext4_get_acl
,
3129 .fiemap
= ext4_fiemap
,
3132 const struct inode_operations ext4_special_inode_operations
= {
3133 .setattr
= ext4_setattr
,
3134 #ifdef CONFIG_EXT4_FS_XATTR
3135 .setxattr
= generic_setxattr
,
3136 .getxattr
= generic_getxattr
,
3137 .listxattr
= ext4_listxattr
,
3138 .removexattr
= generic_removexattr
,
3140 .get_acl
= ext4_get_acl
,