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 int ext4_dirent_csum_verify(struct inode
*inode
, struct ext4_dir_entry
*dirent
)
266 struct ext4_dir_entry_tail
*t
;
268 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode
->i_sb
,
269 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
272 t
= get_dirent_tail(inode
, dirent
);
274 EXT4_ERROR_INODE(inode
, "metadata_csum set but no space in dir "
275 "leaf for checksum. Please run e2fsck -D.");
279 if (t
->det_checksum
!= ext4_dirent_csum(inode
, dirent
,
280 (void *)t
- (void *)dirent
))
286 static void ext4_dirent_csum_set(struct inode
*inode
,
287 struct ext4_dir_entry
*dirent
)
289 struct ext4_dir_entry_tail
*t
;
291 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode
->i_sb
,
292 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
295 t
= get_dirent_tail(inode
, dirent
);
297 EXT4_ERROR_INODE(inode
, "metadata_csum set but no space in dir "
298 "leaf for checksum. Please run e2fsck -D.");
302 t
->det_checksum
= ext4_dirent_csum(inode
, dirent
,
303 (void *)t
- (void *)dirent
);
306 static inline int ext4_handle_dirty_dirent_node(handle_t
*handle
,
308 struct buffer_head
*bh
)
310 ext4_dirent_csum_set(inode
, (struct ext4_dir_entry
*)bh
->b_data
);
311 return ext4_handle_dirty_metadata(handle
, inode
, bh
);
314 static struct dx_countlimit
*get_dx_countlimit(struct inode
*inode
,
315 struct ext4_dir_entry
*dirent
,
318 struct ext4_dir_entry
*dp
;
319 struct dx_root_info
*root
;
322 if (le16_to_cpu(dirent
->rec_len
) == EXT4_BLOCK_SIZE(inode
->i_sb
))
324 else if (le16_to_cpu(dirent
->rec_len
) == 12) {
325 dp
= (struct ext4_dir_entry
*)(((void *)dirent
) + 12);
326 if (le16_to_cpu(dp
->rec_len
) !=
327 EXT4_BLOCK_SIZE(inode
->i_sb
) - 12)
329 root
= (struct dx_root_info
*)(((void *)dp
+ 12));
330 if (root
->reserved_zero
||
331 root
->info_length
!= sizeof(struct dx_root_info
))
338 *offset
= count_offset
;
339 return (struct dx_countlimit
*)(((void *)dirent
) + count_offset
);
342 static __le32
ext4_dx_csum(struct inode
*inode
, struct ext4_dir_entry
*dirent
,
343 int count_offset
, int count
, struct dx_tail
*t
)
345 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
346 struct ext4_inode_info
*ei
= EXT4_I(inode
);
347 __u32 csum
, old_csum
;
350 size
= count_offset
+ (count
* sizeof(struct dx_entry
));
351 old_csum
= t
->dt_checksum
;
353 csum
= ext4_chksum(sbi
, ei
->i_csum_seed
, (__u8
*)dirent
, size
);
354 csum
= ext4_chksum(sbi
, csum
, (__u8
*)t
, sizeof(struct dx_tail
));
355 t
->dt_checksum
= old_csum
;
357 return cpu_to_le32(csum
);
360 static int ext4_dx_csum_verify(struct inode
*inode
,
361 struct ext4_dir_entry
*dirent
)
363 struct dx_countlimit
*c
;
365 int count_offset
, limit
, count
;
367 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode
->i_sb
,
368 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
371 c
= get_dx_countlimit(inode
, dirent
, &count_offset
);
373 EXT4_ERROR_INODE(inode
, "dir seems corrupt? Run e2fsck -D.");
376 limit
= le16_to_cpu(c
->limit
);
377 count
= le16_to_cpu(c
->count
);
378 if (count_offset
+ (limit
* sizeof(struct dx_entry
)) >
379 EXT4_BLOCK_SIZE(inode
->i_sb
) - sizeof(struct dx_tail
)) {
380 EXT4_ERROR_INODE(inode
, "metadata_csum set but no space for "
381 "tree checksum found. Run e2fsck -D.");
384 t
= (struct dx_tail
*)(((struct dx_entry
*)c
) + limit
);
386 if (t
->dt_checksum
!= ext4_dx_csum(inode
, dirent
, count_offset
,
392 static void ext4_dx_csum_set(struct inode
*inode
, struct ext4_dir_entry
*dirent
)
394 struct dx_countlimit
*c
;
396 int count_offset
, limit
, count
;
398 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode
->i_sb
,
399 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
402 c
= get_dx_countlimit(inode
, dirent
, &count_offset
);
404 EXT4_ERROR_INODE(inode
, "dir seems corrupt? Run e2fsck -D.");
407 limit
= le16_to_cpu(c
->limit
);
408 count
= le16_to_cpu(c
->count
);
409 if (count_offset
+ (limit
* sizeof(struct dx_entry
)) >
410 EXT4_BLOCK_SIZE(inode
->i_sb
) - sizeof(struct dx_tail
)) {
411 EXT4_ERROR_INODE(inode
, "metadata_csum set but no space for "
412 "tree checksum. Run e2fsck -D.");
415 t
= (struct dx_tail
*)(((struct dx_entry
*)c
) + limit
);
417 t
->dt_checksum
= ext4_dx_csum(inode
, dirent
, count_offset
, count
, t
);
420 static inline int ext4_handle_dirty_dx_node(handle_t
*handle
,
422 struct buffer_head
*bh
)
424 ext4_dx_csum_set(inode
, (struct ext4_dir_entry
*)bh
->b_data
);
425 return ext4_handle_dirty_metadata(handle
, inode
, bh
);
429 * p is at least 6 bytes before the end of page
431 static inline struct ext4_dir_entry_2
*
432 ext4_next_entry(struct ext4_dir_entry_2
*p
, unsigned long blocksize
)
434 return (struct ext4_dir_entry_2
*)((char *)p
+
435 ext4_rec_len_from_disk(p
->rec_len
, blocksize
));
439 * Future: use high four bits of block for coalesce-on-delete flags
440 * Mask them off for now.
443 static inline ext4_lblk_t
dx_get_block(struct dx_entry
*entry
)
445 return le32_to_cpu(entry
->block
) & 0x00ffffff;
448 static inline void dx_set_block(struct dx_entry
*entry
, ext4_lblk_t value
)
450 entry
->block
= cpu_to_le32(value
);
453 static inline unsigned dx_get_hash(struct dx_entry
*entry
)
455 return le32_to_cpu(entry
->hash
);
458 static inline void dx_set_hash(struct dx_entry
*entry
, unsigned value
)
460 entry
->hash
= cpu_to_le32(value
);
463 static inline unsigned dx_get_count(struct dx_entry
*entries
)
465 return le16_to_cpu(((struct dx_countlimit
*) entries
)->count
);
468 static inline unsigned dx_get_limit(struct dx_entry
*entries
)
470 return le16_to_cpu(((struct dx_countlimit
*) entries
)->limit
);
473 static inline void dx_set_count(struct dx_entry
*entries
, unsigned value
)
475 ((struct dx_countlimit
*) entries
)->count
= cpu_to_le16(value
);
478 static inline void dx_set_limit(struct dx_entry
*entries
, unsigned value
)
480 ((struct dx_countlimit
*) entries
)->limit
= cpu_to_le16(value
);
483 static inline unsigned dx_root_limit(struct inode
*dir
, unsigned infosize
)
485 unsigned entry_space
= dir
->i_sb
->s_blocksize
- EXT4_DIR_REC_LEN(1) -
486 EXT4_DIR_REC_LEN(2) - infosize
;
488 if (EXT4_HAS_RO_COMPAT_FEATURE(dir
->i_sb
,
489 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
490 entry_space
-= sizeof(struct dx_tail
);
491 return entry_space
/ sizeof(struct dx_entry
);
494 static inline unsigned dx_node_limit(struct inode
*dir
)
496 unsigned entry_space
= dir
->i_sb
->s_blocksize
- EXT4_DIR_REC_LEN(0);
498 if (EXT4_HAS_RO_COMPAT_FEATURE(dir
->i_sb
,
499 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
500 entry_space
-= sizeof(struct dx_tail
);
501 return entry_space
/ sizeof(struct dx_entry
);
508 static void dx_show_index(char * label
, struct dx_entry
*entries
)
510 int i
, n
= dx_get_count (entries
);
511 printk(KERN_DEBUG
"%s index ", label
);
512 for (i
= 0; i
< n
; i
++) {
513 printk("%x->%lu ", i
? dx_get_hash(entries
+ i
) :
514 0, (unsigned long)dx_get_block(entries
+ i
));
526 static struct stats
dx_show_leaf(struct dx_hash_info
*hinfo
, struct ext4_dir_entry_2
*de
,
527 int size
, int show_names
)
529 unsigned names
= 0, space
= 0;
530 char *base
= (char *) de
;
531 struct dx_hash_info h
= *hinfo
;
534 while ((char *) de
< base
+ size
)
540 int len
= de
->name_len
;
541 char *name
= de
->name
;
542 while (len
--) printk("%c", *name
++);
543 ext4fs_dirhash(de
->name
, de
->name_len
, &h
);
544 printk(":%x.%u ", h
.hash
,
545 (unsigned) ((char *) de
- base
));
547 space
+= EXT4_DIR_REC_LEN(de
->name_len
);
550 de
= ext4_next_entry(de
, size
);
552 printk("(%i)\n", names
);
553 return (struct stats
) { names
, space
, 1 };
556 struct stats
dx_show_entries(struct dx_hash_info
*hinfo
, struct inode
*dir
,
557 struct dx_entry
*entries
, int levels
)
559 unsigned blocksize
= dir
->i_sb
->s_blocksize
;
560 unsigned count
= dx_get_count(entries
), names
= 0, space
= 0, i
;
562 struct buffer_head
*bh
;
564 printk("%i indexed blocks...\n", count
);
565 for (i
= 0; i
< count
; i
++, entries
++)
567 ext4_lblk_t block
= dx_get_block(entries
);
568 ext4_lblk_t hash
= i
? dx_get_hash(entries
): 0;
569 u32 range
= i
< count
- 1? (dx_get_hash(entries
+ 1) - hash
): ~hash
;
571 printk("%s%3u:%03u hash %8x/%8x ",levels
?"":" ", i
, block
, hash
, range
);
572 if (!(bh
= ext4_bread (NULL
,dir
, block
, 0,&err
))) continue;
574 dx_show_entries(hinfo
, dir
, ((struct dx_node
*) bh
->b_data
)->entries
, levels
- 1):
575 dx_show_leaf(hinfo
, (struct ext4_dir_entry_2
*) bh
->b_data
, blocksize
, 0);
576 names
+= stats
.names
;
577 space
+= stats
.space
;
578 bcount
+= stats
.bcount
;
582 printk(KERN_DEBUG
"%snames %u, fullness %u (%u%%)\n",
583 levels
? "" : " ", names
, space
/bcount
,
584 (space
/bcount
)*100/blocksize
);
585 return (struct stats
) { names
, space
, bcount
};
587 #endif /* DX_DEBUG */
590 * Probe for a directory leaf block to search.
592 * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
593 * error in the directory index, and the caller should fall back to
594 * searching the directory normally. The callers of dx_probe **MUST**
595 * check for this error code, and make sure it never gets reflected
598 static struct dx_frame
*
599 dx_probe(const struct qstr
*d_name
, struct inode
*dir
,
600 struct dx_hash_info
*hinfo
, struct dx_frame
*frame_in
, int *err
)
602 unsigned count
, indirect
;
603 struct dx_entry
*at
, *entries
, *p
, *q
, *m
;
604 struct dx_root
*root
;
605 struct buffer_head
*bh
;
606 struct dx_frame
*frame
= frame_in
;
610 if (!(bh
= ext4_bread(NULL
, dir
, 0, 0, err
))) {
612 *err
= ERR_BAD_DX_DIR
;
615 root
= (struct dx_root
*) bh
->b_data
;
616 if (root
->info
.hash_version
!= DX_HASH_TEA
&&
617 root
->info
.hash_version
!= DX_HASH_HALF_MD4
&&
618 root
->info
.hash_version
!= DX_HASH_LEGACY
) {
619 ext4_warning(dir
->i_sb
, "Unrecognised inode hash code %d",
620 root
->info
.hash_version
);
622 *err
= ERR_BAD_DX_DIR
;
625 hinfo
->hash_version
= root
->info
.hash_version
;
626 if (hinfo
->hash_version
<= DX_HASH_TEA
)
627 hinfo
->hash_version
+= EXT4_SB(dir
->i_sb
)->s_hash_unsigned
;
628 hinfo
->seed
= EXT4_SB(dir
->i_sb
)->s_hash_seed
;
630 ext4fs_dirhash(d_name
->name
, d_name
->len
, hinfo
);
633 if (root
->info
.unused_flags
& 1) {
634 ext4_warning(dir
->i_sb
, "Unimplemented inode hash flags: %#06x",
635 root
->info
.unused_flags
);
637 *err
= ERR_BAD_DX_DIR
;
641 if ((indirect
= root
->info
.indirect_levels
) > 1) {
642 ext4_warning(dir
->i_sb
, "Unimplemented inode hash depth: %#06x",
643 root
->info
.indirect_levels
);
645 *err
= ERR_BAD_DX_DIR
;
649 if (!buffer_verified(bh
) &&
650 !ext4_dx_csum_verify(dir
, (struct ext4_dir_entry
*)bh
->b_data
)) {
651 ext4_warning(dir
->i_sb
, "Root failed checksum");
653 *err
= ERR_BAD_DX_DIR
;
656 set_buffer_verified(bh
);
658 entries
= (struct dx_entry
*) (((char *)&root
->info
) +
659 root
->info
.info_length
);
661 if (dx_get_limit(entries
) != dx_root_limit(dir
,
662 root
->info
.info_length
)) {
663 ext4_warning(dir
->i_sb
, "dx entry: limit != root limit");
665 *err
= ERR_BAD_DX_DIR
;
669 dxtrace(printk("Look up %x", hash
));
672 count
= dx_get_count(entries
);
673 if (!count
|| count
> dx_get_limit(entries
)) {
674 ext4_warning(dir
->i_sb
,
675 "dx entry: no count or count > limit");
677 *err
= ERR_BAD_DX_DIR
;
682 q
= entries
+ count
- 1;
686 dxtrace(printk("."));
687 if (dx_get_hash(m
) > hash
)
693 if (0) // linear search cross check
695 unsigned n
= count
- 1;
699 dxtrace(printk(","));
700 if (dx_get_hash(++at
) > hash
)
706 assert (at
== p
- 1);
710 dxtrace(printk(" %x->%u\n", at
== entries
? 0: dx_get_hash(at
), dx_get_block(at
)));
712 frame
->entries
= entries
;
714 if (!indirect
--) return frame
;
715 if (!(bh
= ext4_bread(NULL
, dir
, dx_get_block(at
), 0, err
))) {
717 *err
= ERR_BAD_DX_DIR
;
720 at
= entries
= ((struct dx_node
*) bh
->b_data
)->entries
;
722 if (!buffer_verified(bh
) &&
723 !ext4_dx_csum_verify(dir
,
724 (struct ext4_dir_entry
*)bh
->b_data
)) {
725 ext4_warning(dir
->i_sb
, "Node failed checksum");
727 *err
= ERR_BAD_DX_DIR
;
730 set_buffer_verified(bh
);
732 if (dx_get_limit(entries
) != dx_node_limit (dir
)) {
733 ext4_warning(dir
->i_sb
,
734 "dx entry: limit != node limit");
736 *err
= ERR_BAD_DX_DIR
;
743 while (frame
>= frame_in
) {
748 if (*err
== ERR_BAD_DX_DIR
)
749 ext4_warning(dir
->i_sb
,
750 "Corrupt dir inode %lu, running e2fsck is "
751 "recommended.", dir
->i_ino
);
755 static void dx_release (struct dx_frame
*frames
)
757 if (frames
[0].bh
== NULL
)
760 if (((struct dx_root
*) frames
[0].bh
->b_data
)->info
.indirect_levels
)
761 brelse(frames
[1].bh
);
762 brelse(frames
[0].bh
);
766 * This function increments the frame pointer to search the next leaf
767 * block, and reads in the necessary intervening nodes if the search
768 * should be necessary. Whether or not the search is necessary is
769 * controlled by the hash parameter. If the hash value is even, then
770 * the search is only continued if the next block starts with that
771 * hash value. This is used if we are searching for a specific file.
773 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
775 * This function returns 1 if the caller should continue to search,
776 * or 0 if it should not. If there is an error reading one of the
777 * index blocks, it will a negative error code.
779 * If start_hash is non-null, it will be filled in with the starting
780 * hash of the next page.
782 static int ext4_htree_next_block(struct inode
*dir
, __u32 hash
,
783 struct dx_frame
*frame
,
784 struct dx_frame
*frames
,
788 struct buffer_head
*bh
;
789 int err
, num_frames
= 0;
794 * Find the next leaf page by incrementing the frame pointer.
795 * If we run out of entries in the interior node, loop around and
796 * increment pointer in the parent node. When we break out of
797 * this loop, num_frames indicates the number of interior
798 * nodes need to be read.
801 if (++(p
->at
) < p
->entries
+ dx_get_count(p
->entries
))
810 * If the hash is 1, then continue only if the next page has a
811 * continuation hash of any value. This is used for readdir
812 * handling. Otherwise, check to see if the hash matches the
813 * desired contiuation hash. If it doesn't, return since
814 * there's no point to read in the successive index pages.
816 bhash
= dx_get_hash(p
->at
);
819 if ((hash
& 1) == 0) {
820 if ((bhash
& ~1) != hash
)
824 * If the hash is HASH_NB_ALWAYS, we always go to the next
825 * block so no check is necessary
827 while (num_frames
--) {
828 if (!(bh
= ext4_bread(NULL
, dir
, dx_get_block(p
->at
),
831 ext4_error(dir
->i_sb
,
832 "Directory hole detected on inode %lu\n",
836 return err
; /* Failure */
839 if (!buffer_verified(bh
) &&
840 !ext4_dx_csum_verify(dir
,
841 (struct ext4_dir_entry
*)bh
->b_data
)) {
842 ext4_warning(dir
->i_sb
, "Node failed checksum");
845 set_buffer_verified(bh
);
850 p
->at
= p
->entries
= ((struct dx_node
*) bh
->b_data
)->entries
;
857 * This function fills a red-black tree with information from a
858 * directory block. It returns the number directory entries loaded
859 * into the tree. If there is an error it is returned in err.
861 static int htree_dirblock_to_tree(struct file
*dir_file
,
862 struct inode
*dir
, ext4_lblk_t block
,
863 struct dx_hash_info
*hinfo
,
864 __u32 start_hash
, __u32 start_minor_hash
)
866 struct buffer_head
*bh
;
867 struct ext4_dir_entry_2
*de
, *top
;
868 int err
= 0, count
= 0;
870 dxtrace(printk(KERN_INFO
"In htree dirblock_to_tree: block %lu\n",
871 (unsigned long)block
));
872 if (!(bh
= ext4_bread(NULL
, dir
, block
, 0, &err
))) {
875 ext4_error(dir
->i_sb
,
876 "Directory hole detected on inode %lu\n",
882 if (!buffer_verified(bh
) &&
883 !ext4_dirent_csum_verify(dir
, (struct ext4_dir_entry
*)bh
->b_data
))
885 set_buffer_verified(bh
);
887 de
= (struct ext4_dir_entry_2
*) bh
->b_data
;
888 top
= (struct ext4_dir_entry_2
*) ((char *) de
+
889 dir
->i_sb
->s_blocksize
-
890 EXT4_DIR_REC_LEN(0));
891 for (; de
< top
; de
= ext4_next_entry(de
, dir
->i_sb
->s_blocksize
)) {
892 if (ext4_check_dir_entry(dir
, NULL
, de
, bh
,
893 (block
<<EXT4_BLOCK_SIZE_BITS(dir
->i_sb
))
894 + ((char *)de
- bh
->b_data
))) {
895 /* On error, skip the f_pos to the next block. */
896 dir_file
->f_pos
= (dir_file
->f_pos
|
897 (dir
->i_sb
->s_blocksize
- 1)) + 1;
901 ext4fs_dirhash(de
->name
, de
->name_len
, hinfo
);
902 if ((hinfo
->hash
< start_hash
) ||
903 ((hinfo
->hash
== start_hash
) &&
904 (hinfo
->minor_hash
< start_minor_hash
)))
908 if ((err
= ext4_htree_store_dirent(dir_file
,
909 hinfo
->hash
, hinfo
->minor_hash
, de
)) != 0) {
921 * This function fills a red-black tree with information from a
922 * directory. We start scanning the directory in hash order, starting
923 * at start_hash and start_minor_hash.
925 * This function returns the number of entries inserted into the tree,
926 * or a negative error code.
928 int ext4_htree_fill_tree(struct file
*dir_file
, __u32 start_hash
,
929 __u32 start_minor_hash
, __u32
*next_hash
)
931 struct dx_hash_info hinfo
;
932 struct ext4_dir_entry_2
*de
;
933 struct dx_frame frames
[2], *frame
;
940 dxtrace(printk(KERN_DEBUG
"In htree_fill_tree, start hash: %x:%x\n",
941 start_hash
, start_minor_hash
));
942 dir
= dir_file
->f_path
.dentry
->d_inode
;
943 if (!(ext4_test_inode_flag(dir
, EXT4_INODE_INDEX
))) {
944 hinfo
.hash_version
= EXT4_SB(dir
->i_sb
)->s_def_hash_version
;
945 if (hinfo
.hash_version
<= DX_HASH_TEA
)
946 hinfo
.hash_version
+=
947 EXT4_SB(dir
->i_sb
)->s_hash_unsigned
;
948 hinfo
.seed
= EXT4_SB(dir
->i_sb
)->s_hash_seed
;
949 count
= htree_dirblock_to_tree(dir_file
, dir
, 0, &hinfo
,
950 start_hash
, start_minor_hash
);
954 hinfo
.hash
= start_hash
;
955 hinfo
.minor_hash
= 0;
956 frame
= dx_probe(NULL
, dir
, &hinfo
, frames
, &err
);
960 /* Add '.' and '..' from the htree header */
961 if (!start_hash
&& !start_minor_hash
) {
962 de
= (struct ext4_dir_entry_2
*) frames
[0].bh
->b_data
;
963 if ((err
= ext4_htree_store_dirent(dir_file
, 0, 0, de
)) != 0)
967 if (start_hash
< 2 || (start_hash
==2 && start_minor_hash
==0)) {
968 de
= (struct ext4_dir_entry_2
*) frames
[0].bh
->b_data
;
969 de
= ext4_next_entry(de
, dir
->i_sb
->s_blocksize
);
970 if ((err
= ext4_htree_store_dirent(dir_file
, 2, 0, de
)) != 0)
976 block
= dx_get_block(frame
->at
);
977 ret
= htree_dirblock_to_tree(dir_file
, dir
, block
, &hinfo
,
978 start_hash
, start_minor_hash
);
985 ret
= ext4_htree_next_block(dir
, HASH_NB_ALWAYS
,
986 frame
, frames
, &hashval
);
987 *next_hash
= hashval
;
993 * Stop if: (a) there are no more entries, or
994 * (b) we have inserted at least one entry and the
995 * next hash value is not a continuation
998 (count
&& ((hashval
& 1) == 0)))
1002 dxtrace(printk(KERN_DEBUG
"Fill tree: returned %d entries, "
1003 "next hash: %x\n", count
, *next_hash
));
1012 * Directory block splitting, compacting
1016 * Create map of hash values, offsets, and sizes, stored at end of block.
1017 * Returns number of entries mapped.
1019 static int dx_make_map(struct ext4_dir_entry_2
*de
, unsigned blocksize
,
1020 struct dx_hash_info
*hinfo
,
1021 struct dx_map_entry
*map_tail
)
1024 char *base
= (char *) de
;
1025 struct dx_hash_info h
= *hinfo
;
1027 while ((char *) de
< base
+ blocksize
) {
1028 if (de
->name_len
&& de
->inode
) {
1029 ext4fs_dirhash(de
->name
, de
->name_len
, &h
);
1031 map_tail
->hash
= h
.hash
;
1032 map_tail
->offs
= ((char *) de
- base
)>>2;
1033 map_tail
->size
= le16_to_cpu(de
->rec_len
);
1037 /* XXX: do we need to check rec_len == 0 case? -Chris */
1038 de
= ext4_next_entry(de
, blocksize
);
1043 /* Sort map by hash value */
1044 static void dx_sort_map (struct dx_map_entry
*map
, unsigned count
)
1046 struct dx_map_entry
*p
, *q
, *top
= map
+ count
- 1;
1048 /* Combsort until bubble sort doesn't suck */
1050 count
= count
*10/13;
1051 if (count
- 9 < 2) /* 9, 10 -> 11 */
1053 for (p
= top
, q
= p
- count
; q
>= map
; p
--, q
--)
1054 if (p
->hash
< q
->hash
)
1057 /* Garden variety bubble sort */
1062 if (q
[1].hash
>= q
[0].hash
)
1070 static void dx_insert_block(struct dx_frame
*frame
, u32 hash
, ext4_lblk_t block
)
1072 struct dx_entry
*entries
= frame
->entries
;
1073 struct dx_entry
*old
= frame
->at
, *new = old
+ 1;
1074 int count
= dx_get_count(entries
);
1076 assert(count
< dx_get_limit(entries
));
1077 assert(old
< entries
+ count
);
1078 memmove(new + 1, new, (char *)(entries
+ count
) - (char *)(new));
1079 dx_set_hash(new, hash
);
1080 dx_set_block(new, block
);
1081 dx_set_count(entries
, count
+ 1);
1084 static void ext4_update_dx_flag(struct inode
*inode
)
1086 if (!EXT4_HAS_COMPAT_FEATURE(inode
->i_sb
,
1087 EXT4_FEATURE_COMPAT_DIR_INDEX
))
1088 ext4_clear_inode_flag(inode
, EXT4_INODE_INDEX
);
1092 * NOTE! unlike strncmp, ext4_match returns 1 for success, 0 for failure.
1094 * `len <= EXT4_NAME_LEN' is guaranteed by caller.
1095 * `de != NULL' is guaranteed by caller.
1097 static inline int ext4_match (int len
, const char * const name
,
1098 struct ext4_dir_entry_2
* de
)
1100 if (len
!= de
->name_len
)
1104 return !memcmp(name
, de
->name
, len
);
1108 * Returns 0 if not found, -1 on failure, and 1 on success
1110 static inline int search_dirblock(struct buffer_head
*bh
,
1112 const struct qstr
*d_name
,
1113 unsigned int offset
,
1114 struct ext4_dir_entry_2
** res_dir
)
1116 struct ext4_dir_entry_2
* de
;
1119 const char *name
= d_name
->name
;
1120 int namelen
= d_name
->len
;
1122 de
= (struct ext4_dir_entry_2
*) bh
->b_data
;
1123 dlimit
= bh
->b_data
+ dir
->i_sb
->s_blocksize
;
1124 while ((char *) de
< dlimit
) {
1125 /* this code is executed quadratically often */
1126 /* do minimal checking `by hand' */
1128 if ((char *) de
+ namelen
<= dlimit
&&
1129 ext4_match (namelen
, name
, de
)) {
1130 /* found a match - just to be sure, do a full check */
1131 if (ext4_check_dir_entry(dir
, NULL
, de
, bh
, offset
))
1136 /* prevent looping on a bad block */
1137 de_len
= ext4_rec_len_from_disk(de
->rec_len
,
1138 dir
->i_sb
->s_blocksize
);
1142 de
= (struct ext4_dir_entry_2
*) ((char *) de
+ de_len
);
1151 * finds an entry in the specified directory with the wanted name. It
1152 * returns the cache buffer in which the entry was found, and the entry
1153 * itself (as a parameter - res_dir). It does NOT read the inode of the
1154 * entry - you'll have to do that yourself if you want to.
1156 * The returned buffer_head has ->b_count elevated. The caller is expected
1157 * to brelse() it when appropriate.
1159 static struct buffer_head
* ext4_find_entry (struct inode
*dir
,
1160 const struct qstr
*d_name
,
1161 struct ext4_dir_entry_2
** res_dir
)
1163 struct super_block
*sb
;
1164 struct buffer_head
*bh_use
[NAMEI_RA_SIZE
];
1165 struct buffer_head
*bh
, *ret
= NULL
;
1166 ext4_lblk_t start
, block
, b
;
1167 const u8
*name
= d_name
->name
;
1168 int ra_max
= 0; /* Number of bh's in the readahead
1170 int ra_ptr
= 0; /* Current index into readahead
1173 ext4_lblk_t nblocks
;
1179 namelen
= d_name
->len
;
1180 if (namelen
> EXT4_NAME_LEN
)
1182 if ((namelen
<= 2) && (name
[0] == '.') &&
1183 (name
[1] == '.' || name
[1] == '\0')) {
1185 * "." or ".." will only be in the first block
1186 * NFS may look up ".."; "." should be handled by the VFS
1193 bh
= ext4_dx_find_entry(dir
, d_name
, res_dir
, &err
);
1195 * On success, or if the error was file not found,
1196 * return. Otherwise, fall back to doing a search the
1197 * old fashioned way.
1199 if (bh
|| (err
!= ERR_BAD_DX_DIR
))
1201 dxtrace(printk(KERN_DEBUG
"ext4_find_entry: dx failed, "
1204 nblocks
= dir
->i_size
>> EXT4_BLOCK_SIZE_BITS(sb
);
1205 start
= EXT4_I(dir
)->i_dir_start_lookup
;
1206 if (start
>= nblocks
)
1212 * We deal with the read-ahead logic here.
1214 if (ra_ptr
>= ra_max
) {
1215 /* Refill the readahead buffer */
1218 for (ra_max
= 0; ra_max
< NAMEI_RA_SIZE
; ra_max
++) {
1220 * Terminate if we reach the end of the
1221 * directory and must wrap, or if our
1222 * search has finished at this block.
1224 if (b
>= nblocks
|| (num
&& block
== start
)) {
1225 bh_use
[ra_max
] = NULL
;
1229 bh
= ext4_getblk(NULL
, dir
, b
++, 0, &err
);
1230 bh_use
[ra_max
] = bh
;
1232 ll_rw_block(READ
| REQ_META
| REQ_PRIO
,
1236 if ((bh
= bh_use
[ra_ptr
++]) == NULL
)
1239 if (!buffer_uptodate(bh
)) {
1240 /* read error, skip block & hope for the best */
1241 EXT4_ERROR_INODE(dir
, "reading directory lblock %lu",
1242 (unsigned long) block
);
1246 if (!buffer_verified(bh
) &&
1247 !ext4_dirent_csum_verify(dir
,
1248 (struct ext4_dir_entry
*)bh
->b_data
)) {
1249 EXT4_ERROR_INODE(dir
, "checksumming directory "
1250 "block %lu", (unsigned long)block
);
1254 set_buffer_verified(bh
);
1255 i
= search_dirblock(bh
, dir
, d_name
,
1256 block
<< EXT4_BLOCK_SIZE_BITS(sb
), res_dir
);
1258 EXT4_I(dir
)->i_dir_start_lookup
= block
;
1260 goto cleanup_and_exit
;
1264 goto cleanup_and_exit
;
1267 if (++block
>= nblocks
)
1269 } while (block
!= start
);
1272 * If the directory has grown while we were searching, then
1273 * search the last part of the directory before giving up.
1276 nblocks
= dir
->i_size
>> EXT4_BLOCK_SIZE_BITS(sb
);
1277 if (block
< nblocks
) {
1283 /* Clean up the read-ahead blocks */
1284 for (; ra_ptr
< ra_max
; ra_ptr
++)
1285 brelse(bh_use
[ra_ptr
]);
1289 static struct buffer_head
* ext4_dx_find_entry(struct inode
*dir
, const struct qstr
*d_name
,
1290 struct ext4_dir_entry_2
**res_dir
, int *err
)
1292 struct super_block
* sb
= dir
->i_sb
;
1293 struct dx_hash_info hinfo
;
1294 struct dx_frame frames
[2], *frame
;
1295 struct buffer_head
*bh
;
1299 if (!(frame
= dx_probe(d_name
, dir
, &hinfo
, frames
, err
)))
1302 block
= dx_get_block(frame
->at
);
1303 if (!(bh
= ext4_bread(NULL
, dir
, block
, 0, err
))) {
1306 ext4_error(dir
->i_sb
,
1307 "Directory hole detected on inode %lu\n",
1313 if (!buffer_verified(bh
) &&
1314 !ext4_dirent_csum_verify(dir
,
1315 (struct ext4_dir_entry
*)bh
->b_data
)) {
1316 EXT4_ERROR_INODE(dir
, "checksumming directory "
1317 "block %lu", (unsigned long)block
);
1322 set_buffer_verified(bh
);
1323 retval
= search_dirblock(bh
, dir
, d_name
,
1324 block
<< EXT4_BLOCK_SIZE_BITS(sb
),
1326 if (retval
== 1) { /* Success! */
1332 *err
= ERR_BAD_DX_DIR
;
1336 /* Check to see if we should continue to search */
1337 retval
= ext4_htree_next_block(dir
, hinfo
.hash
, frame
,
1341 "error reading index page in directory #%lu",
1346 } while (retval
== 1);
1350 dxtrace(printk(KERN_DEBUG
"%s not found\n", d_name
->name
));
1351 dx_release (frames
);
1355 static struct dentry
*ext4_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
1357 struct inode
*inode
;
1358 struct ext4_dir_entry_2
*de
;
1359 struct buffer_head
*bh
;
1361 if (dentry
->d_name
.len
> EXT4_NAME_LEN
)
1362 return ERR_PTR(-ENAMETOOLONG
);
1364 bh
= ext4_find_entry(dir
, &dentry
->d_name
, &de
);
1367 __u32 ino
= le32_to_cpu(de
->inode
);
1369 if (!ext4_valid_inum(dir
->i_sb
, ino
)) {
1370 EXT4_ERROR_INODE(dir
, "bad inode number: %u", ino
);
1371 return ERR_PTR(-EIO
);
1373 if (unlikely(ino
== dir
->i_ino
)) {
1374 EXT4_ERROR_INODE(dir
, "'%.*s' linked to parent dir",
1376 dentry
->d_name
.name
);
1377 return ERR_PTR(-EIO
);
1379 inode
= ext4_iget(dir
->i_sb
, ino
);
1380 if (inode
== ERR_PTR(-ESTALE
)) {
1381 EXT4_ERROR_INODE(dir
,
1382 "deleted inode referenced: %u",
1384 return ERR_PTR(-EIO
);
1387 return d_splice_alias(inode
, dentry
);
1391 struct dentry
*ext4_get_parent(struct dentry
*child
)
1394 static const struct qstr dotdot
= QSTR_INIT("..", 2);
1395 struct ext4_dir_entry_2
* de
;
1396 struct buffer_head
*bh
;
1398 bh
= ext4_find_entry(child
->d_inode
, &dotdot
, &de
);
1400 return ERR_PTR(-ENOENT
);
1401 ino
= le32_to_cpu(de
->inode
);
1404 if (!ext4_valid_inum(child
->d_inode
->i_sb
, ino
)) {
1405 EXT4_ERROR_INODE(child
->d_inode
,
1406 "bad parent inode number: %u", ino
);
1407 return ERR_PTR(-EIO
);
1410 return d_obtain_alias(ext4_iget(child
->d_inode
->i_sb
, ino
));
1414 static unsigned char ext4_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
1415 [S_IFREG
>> S_SHIFT
] = EXT4_FT_REG_FILE
,
1416 [S_IFDIR
>> S_SHIFT
] = EXT4_FT_DIR
,
1417 [S_IFCHR
>> S_SHIFT
] = EXT4_FT_CHRDEV
,
1418 [S_IFBLK
>> S_SHIFT
] = EXT4_FT_BLKDEV
,
1419 [S_IFIFO
>> S_SHIFT
] = EXT4_FT_FIFO
,
1420 [S_IFSOCK
>> S_SHIFT
] = EXT4_FT_SOCK
,
1421 [S_IFLNK
>> S_SHIFT
] = EXT4_FT_SYMLINK
,
1424 static inline void ext4_set_de_type(struct super_block
*sb
,
1425 struct ext4_dir_entry_2
*de
,
1427 if (EXT4_HAS_INCOMPAT_FEATURE(sb
, EXT4_FEATURE_INCOMPAT_FILETYPE
))
1428 de
->file_type
= ext4_type_by_mode
[(mode
& S_IFMT
)>>S_SHIFT
];
1432 * Move count entries from end of map between two memory locations.
1433 * Returns pointer to last entry moved.
1435 static struct ext4_dir_entry_2
*
1436 dx_move_dirents(char *from
, char *to
, struct dx_map_entry
*map
, int count
,
1439 unsigned rec_len
= 0;
1442 struct ext4_dir_entry_2
*de
= (struct ext4_dir_entry_2
*)
1443 (from
+ (map
->offs
<<2));
1444 rec_len
= EXT4_DIR_REC_LEN(de
->name_len
);
1445 memcpy (to
, de
, rec_len
);
1446 ((struct ext4_dir_entry_2
*) to
)->rec_len
=
1447 ext4_rec_len_to_disk(rec_len
, blocksize
);
1452 return (struct ext4_dir_entry_2
*) (to
- rec_len
);
1456 * Compact each dir entry in the range to the minimal rec_len.
1457 * Returns pointer to last entry in range.
1459 static struct ext4_dir_entry_2
* dx_pack_dirents(char *base
, unsigned blocksize
)
1461 struct ext4_dir_entry_2
*next
, *to
, *prev
, *de
= (struct ext4_dir_entry_2
*) base
;
1462 unsigned rec_len
= 0;
1465 while ((char*)de
< base
+ blocksize
) {
1466 next
= ext4_next_entry(de
, blocksize
);
1467 if (de
->inode
&& de
->name_len
) {
1468 rec_len
= EXT4_DIR_REC_LEN(de
->name_len
);
1470 memmove(to
, de
, rec_len
);
1471 to
->rec_len
= ext4_rec_len_to_disk(rec_len
, blocksize
);
1473 to
= (struct ext4_dir_entry_2
*) (((char *) to
) + rec_len
);
1481 * Split a full leaf block to make room for a new dir entry.
1482 * Allocate a new block, and move entries so that they are approx. equally full.
1483 * Returns pointer to de in block into which the new entry will be inserted.
1485 static struct ext4_dir_entry_2
*do_split(handle_t
*handle
, struct inode
*dir
,
1486 struct buffer_head
**bh
,struct dx_frame
*frame
,
1487 struct dx_hash_info
*hinfo
, int *error
)
1489 unsigned blocksize
= dir
->i_sb
->s_blocksize
;
1490 unsigned count
, continued
;
1491 struct buffer_head
*bh2
;
1492 ext4_lblk_t newblock
;
1494 struct dx_map_entry
*map
;
1495 char *data1
= (*bh
)->b_data
, *data2
;
1496 unsigned split
, move
, size
;
1497 struct ext4_dir_entry_2
*de
= NULL
, *de2
;
1498 struct ext4_dir_entry_tail
*t
;
1502 if (EXT4_HAS_RO_COMPAT_FEATURE(dir
->i_sb
,
1503 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
1504 csum_size
= sizeof(struct ext4_dir_entry_tail
);
1506 bh2
= ext4_append (handle
, dir
, &newblock
, &err
);
1513 BUFFER_TRACE(*bh
, "get_write_access");
1514 err
= ext4_journal_get_write_access(handle
, *bh
);
1518 BUFFER_TRACE(frame
->bh
, "get_write_access");
1519 err
= ext4_journal_get_write_access(handle
, frame
->bh
);
1523 data2
= bh2
->b_data
;
1525 /* create map in the end of data2 block */
1526 map
= (struct dx_map_entry
*) (data2
+ blocksize
);
1527 count
= dx_make_map((struct ext4_dir_entry_2
*) data1
,
1528 blocksize
, hinfo
, map
);
1530 dx_sort_map(map
, count
);
1531 /* Split the existing block in the middle, size-wise */
1534 for (i
= count
-1; i
>= 0; i
--) {
1535 /* is more than half of this entry in 2nd half of the block? */
1536 if (size
+ map
[i
].size
/2 > blocksize
/2)
1538 size
+= map
[i
].size
;
1541 /* map index at which we will split */
1542 split
= count
- move
;
1543 hash2
= map
[split
].hash
;
1544 continued
= hash2
== map
[split
- 1].hash
;
1545 dxtrace(printk(KERN_INFO
"Split block %lu at %x, %i/%i\n",
1546 (unsigned long)dx_get_block(frame
->at
),
1547 hash2
, split
, count
-split
));
1549 /* Fancy dance to stay within two buffers */
1550 de2
= dx_move_dirents(data1
, data2
, map
+ split
, count
- split
, blocksize
);
1551 de
= dx_pack_dirents(data1
, blocksize
);
1552 de
->rec_len
= ext4_rec_len_to_disk(data1
+ (blocksize
- csum_size
) -
1555 de2
->rec_len
= ext4_rec_len_to_disk(data2
+ (blocksize
- csum_size
) -
1559 t
= EXT4_DIRENT_TAIL(data2
, blocksize
);
1560 initialize_dirent_tail(t
, blocksize
);
1562 t
= EXT4_DIRENT_TAIL(data1
, blocksize
);
1563 initialize_dirent_tail(t
, blocksize
);
1566 dxtrace(dx_show_leaf (hinfo
, (struct ext4_dir_entry_2
*) data1
, blocksize
, 1));
1567 dxtrace(dx_show_leaf (hinfo
, (struct ext4_dir_entry_2
*) data2
, blocksize
, 1));
1569 /* Which block gets the new entry? */
1570 if (hinfo
->hash
>= hash2
)
1575 dx_insert_block(frame
, hash2
+ continued
, newblock
);
1576 err
= ext4_handle_dirty_dirent_node(handle
, dir
, bh2
);
1579 err
= ext4_handle_dirty_dx_node(handle
, dir
, frame
->bh
);
1583 dxtrace(dx_show_index("frame", frame
->entries
));
1590 ext4_std_error(dir
->i_sb
, err
);
1597 * Add a new entry into a directory (leaf) block. If de is non-NULL,
1598 * it points to a directory entry which is guaranteed to be large
1599 * enough for new directory entry. If de is NULL, then
1600 * add_dirent_to_buf will attempt search the directory block for
1601 * space. It will return -ENOSPC if no space is available, and -EIO
1602 * and -EEXIST if directory entry already exists.
1604 static int add_dirent_to_buf(handle_t
*handle
, struct dentry
*dentry
,
1605 struct inode
*inode
, struct ext4_dir_entry_2
*de
,
1606 struct buffer_head
*bh
)
1608 struct inode
*dir
= dentry
->d_parent
->d_inode
;
1609 const char *name
= dentry
->d_name
.name
;
1610 int namelen
= dentry
->d_name
.len
;
1611 unsigned int offset
= 0;
1612 unsigned int blocksize
= dir
->i_sb
->s_blocksize
;
1613 unsigned short reclen
;
1614 int nlen
, rlen
, err
;
1618 if (EXT4_HAS_RO_COMPAT_FEATURE(inode
->i_sb
,
1619 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
1620 csum_size
= sizeof(struct ext4_dir_entry_tail
);
1622 reclen
= EXT4_DIR_REC_LEN(namelen
);
1624 de
= (struct ext4_dir_entry_2
*)bh
->b_data
;
1625 top
= bh
->b_data
+ (blocksize
- csum_size
) - reclen
;
1626 while ((char *) de
<= top
) {
1627 if (ext4_check_dir_entry(dir
, NULL
, de
, bh
, offset
))
1629 if (ext4_match(namelen
, name
, de
))
1631 nlen
= EXT4_DIR_REC_LEN(de
->name_len
);
1632 rlen
= ext4_rec_len_from_disk(de
->rec_len
, blocksize
);
1633 if ((de
->inode
? rlen
- nlen
: rlen
) >= reclen
)
1635 de
= (struct ext4_dir_entry_2
*)((char *)de
+ rlen
);
1638 if ((char *) de
> top
)
1641 BUFFER_TRACE(bh
, "get_write_access");
1642 err
= ext4_journal_get_write_access(handle
, bh
);
1644 ext4_std_error(dir
->i_sb
, err
);
1648 /* By now the buffer is marked for journaling */
1649 nlen
= EXT4_DIR_REC_LEN(de
->name_len
);
1650 rlen
= ext4_rec_len_from_disk(de
->rec_len
, blocksize
);
1652 struct ext4_dir_entry_2
*de1
= (struct ext4_dir_entry_2
*)((char *)de
+ nlen
);
1653 de1
->rec_len
= ext4_rec_len_to_disk(rlen
- nlen
, blocksize
);
1654 de
->rec_len
= ext4_rec_len_to_disk(nlen
, blocksize
);
1657 de
->file_type
= EXT4_FT_UNKNOWN
;
1658 de
->inode
= cpu_to_le32(inode
->i_ino
);
1659 ext4_set_de_type(dir
->i_sb
, de
, inode
->i_mode
);
1660 de
->name_len
= namelen
;
1661 memcpy(de
->name
, name
, namelen
);
1663 * XXX shouldn't update any times until successful
1664 * completion of syscall, but too many callers depend
1667 * XXX similarly, too many callers depend on
1668 * ext4_new_inode() setting the times, but error
1669 * recovery deletes the inode, so the worst that can
1670 * happen is that the times are slightly out of date
1671 * and/or different from the directory change time.
1673 dir
->i_mtime
= dir
->i_ctime
= ext4_current_time(dir
);
1674 ext4_update_dx_flag(dir
);
1676 ext4_mark_inode_dirty(handle
, dir
);
1677 BUFFER_TRACE(bh
, "call ext4_handle_dirty_metadata");
1678 err
= ext4_handle_dirty_dirent_node(handle
, dir
, bh
);
1680 ext4_std_error(dir
->i_sb
, err
);
1685 * This converts a one block unindexed directory to a 3 block indexed
1686 * directory, and adds the dentry to the indexed directory.
1688 static int make_indexed_dir(handle_t
*handle
, struct dentry
*dentry
,
1689 struct inode
*inode
, struct buffer_head
*bh
)
1691 struct inode
*dir
= dentry
->d_parent
->d_inode
;
1692 const char *name
= dentry
->d_name
.name
;
1693 int namelen
= dentry
->d_name
.len
;
1694 struct buffer_head
*bh2
;
1695 struct dx_root
*root
;
1696 struct dx_frame frames
[2], *frame
;
1697 struct dx_entry
*entries
;
1698 struct ext4_dir_entry_2
*de
, *de2
;
1699 struct ext4_dir_entry_tail
*t
;
1704 struct dx_hash_info hinfo
;
1706 struct fake_dirent
*fde
;
1709 if (EXT4_HAS_RO_COMPAT_FEATURE(inode
->i_sb
,
1710 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
1711 csum_size
= sizeof(struct ext4_dir_entry_tail
);
1713 blocksize
= dir
->i_sb
->s_blocksize
;
1714 dxtrace(printk(KERN_DEBUG
"Creating index: inode %lu\n", dir
->i_ino
));
1715 retval
= ext4_journal_get_write_access(handle
, bh
);
1717 ext4_std_error(dir
->i_sb
, retval
);
1721 root
= (struct dx_root
*) bh
->b_data
;
1723 /* The 0th block becomes the root, move the dirents out */
1724 fde
= &root
->dotdot
;
1725 de
= (struct ext4_dir_entry_2
*)((char *)fde
+
1726 ext4_rec_len_from_disk(fde
->rec_len
, blocksize
));
1727 if ((char *) de
>= (((char *) root
) + blocksize
)) {
1728 EXT4_ERROR_INODE(dir
, "invalid rec_len for '..'");
1732 len
= ((char *) root
) + (blocksize
- csum_size
) - (char *) de
;
1734 /* Allocate new block for the 0th block's dirents */
1735 bh2
= ext4_append(handle
, dir
, &block
, &retval
);
1740 ext4_set_inode_flag(dir
, EXT4_INODE_INDEX
);
1741 data1
= bh2
->b_data
;
1743 memcpy (data1
, de
, len
);
1744 de
= (struct ext4_dir_entry_2
*) data1
;
1746 while ((char *)(de2
= ext4_next_entry(de
, blocksize
)) < top
)
1748 de
->rec_len
= ext4_rec_len_to_disk(data1
+ (blocksize
- csum_size
) -
1753 t
= EXT4_DIRENT_TAIL(data1
, blocksize
);
1754 initialize_dirent_tail(t
, blocksize
);
1757 /* Initialize the root; the dot dirents already exist */
1758 de
= (struct ext4_dir_entry_2
*) (&root
->dotdot
);
1759 de
->rec_len
= ext4_rec_len_to_disk(blocksize
- EXT4_DIR_REC_LEN(2),
1761 memset (&root
->info
, 0, sizeof(root
->info
));
1762 root
->info
.info_length
= sizeof(root
->info
);
1763 root
->info
.hash_version
= EXT4_SB(dir
->i_sb
)->s_def_hash_version
;
1764 entries
= root
->entries
;
1765 dx_set_block(entries
, 1);
1766 dx_set_count(entries
, 1);
1767 dx_set_limit(entries
, dx_root_limit(dir
, sizeof(root
->info
)));
1769 /* Initialize as for dx_probe */
1770 hinfo
.hash_version
= root
->info
.hash_version
;
1771 if (hinfo
.hash_version
<= DX_HASH_TEA
)
1772 hinfo
.hash_version
+= EXT4_SB(dir
->i_sb
)->s_hash_unsigned
;
1773 hinfo
.seed
= EXT4_SB(dir
->i_sb
)->s_hash_seed
;
1774 ext4fs_dirhash(name
, namelen
, &hinfo
);
1776 frame
->entries
= entries
;
1777 frame
->at
= entries
;
1781 ext4_handle_dirty_dx_node(handle
, dir
, frame
->bh
);
1782 ext4_handle_dirty_dirent_node(handle
, dir
, bh
);
1784 de
= do_split(handle
,dir
, &bh
, frame
, &hinfo
, &retval
);
1787 * Even if the block split failed, we have to properly write
1788 * out all the changes we did so far. Otherwise we can end up
1789 * with corrupted filesystem.
1791 ext4_mark_inode_dirty(handle
, dir
);
1797 retval
= add_dirent_to_buf(handle
, dentry
, inode
, de
, bh
);
1805 * adds a file entry to the specified directory, using the same
1806 * semantics as ext4_find_entry(). It returns NULL if it failed.
1808 * NOTE!! The inode part of 'de' is left at 0 - which means you
1809 * may not sleep between calling this and putting something into
1810 * the entry, as someone else might have used it while you slept.
1812 static int ext4_add_entry(handle_t
*handle
, struct dentry
*dentry
,
1813 struct inode
*inode
)
1815 struct inode
*dir
= dentry
->d_parent
->d_inode
;
1816 struct buffer_head
*bh
;
1817 struct ext4_dir_entry_2
*de
;
1818 struct ext4_dir_entry_tail
*t
;
1819 struct super_block
*sb
;
1823 ext4_lblk_t block
, blocks
;
1826 if (EXT4_HAS_RO_COMPAT_FEATURE(inode
->i_sb
,
1827 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
1828 csum_size
= sizeof(struct ext4_dir_entry_tail
);
1831 blocksize
= sb
->s_blocksize
;
1832 if (!dentry
->d_name
.len
)
1835 retval
= ext4_dx_add_entry(handle
, dentry
, inode
);
1836 if (!retval
|| (retval
!= ERR_BAD_DX_DIR
))
1838 ext4_clear_inode_flag(dir
, EXT4_INODE_INDEX
);
1840 ext4_mark_inode_dirty(handle
, dir
);
1842 blocks
= dir
->i_size
>> sb
->s_blocksize_bits
;
1843 for (block
= 0; block
< blocks
; block
++) {
1844 if (!(bh
= ext4_bread(handle
, dir
, block
, 0, &retval
))) {
1847 ext4_error(inode
->i_sb
,
1848 "Directory hole detected on inode %lu\n",
1853 if (!buffer_verified(bh
) &&
1854 !ext4_dirent_csum_verify(dir
,
1855 (struct ext4_dir_entry
*)bh
->b_data
))
1857 set_buffer_verified(bh
);
1858 retval
= add_dirent_to_buf(handle
, dentry
, inode
, NULL
, bh
);
1859 if (retval
!= -ENOSPC
) {
1864 if (blocks
== 1 && !dx_fallback
&&
1865 EXT4_HAS_COMPAT_FEATURE(sb
, EXT4_FEATURE_COMPAT_DIR_INDEX
))
1866 return make_indexed_dir(handle
, dentry
, inode
, bh
);
1869 bh
= ext4_append(handle
, dir
, &block
, &retval
);
1872 de
= (struct ext4_dir_entry_2
*) bh
->b_data
;
1874 de
->rec_len
= ext4_rec_len_to_disk(blocksize
- csum_size
, blocksize
);
1877 t
= EXT4_DIRENT_TAIL(bh
->b_data
, blocksize
);
1878 initialize_dirent_tail(t
, blocksize
);
1881 retval
= add_dirent_to_buf(handle
, dentry
, inode
, de
, bh
);
1884 ext4_set_inode_state(inode
, EXT4_STATE_NEWENTRY
);
1889 * Returns 0 for success, or a negative error value
1891 static int ext4_dx_add_entry(handle_t
*handle
, struct dentry
*dentry
,
1892 struct inode
*inode
)
1894 struct dx_frame frames
[2], *frame
;
1895 struct dx_entry
*entries
, *at
;
1896 struct dx_hash_info hinfo
;
1897 struct buffer_head
*bh
;
1898 struct inode
*dir
= dentry
->d_parent
->d_inode
;
1899 struct super_block
*sb
= dir
->i_sb
;
1900 struct ext4_dir_entry_2
*de
;
1903 frame
= dx_probe(&dentry
->d_name
, dir
, &hinfo
, frames
, &err
);
1906 entries
= frame
->entries
;
1909 if (!(bh
= ext4_bread(handle
, dir
, dx_get_block(frame
->at
), 0, &err
))) {
1912 ext4_error(dir
->i_sb
,
1913 "Directory hole detected on inode %lu\n",
1919 if (!buffer_verified(bh
) &&
1920 !ext4_dirent_csum_verify(dir
, (struct ext4_dir_entry
*)bh
->b_data
))
1922 set_buffer_verified(bh
);
1924 BUFFER_TRACE(bh
, "get_write_access");
1925 err
= ext4_journal_get_write_access(handle
, bh
);
1929 err
= add_dirent_to_buf(handle
, dentry
, inode
, NULL
, bh
);
1933 /* Block full, should compress but for now just split */
1934 dxtrace(printk(KERN_DEBUG
"using %u of %u node entries\n",
1935 dx_get_count(entries
), dx_get_limit(entries
)));
1936 /* Need to split index? */
1937 if (dx_get_count(entries
) == dx_get_limit(entries
)) {
1938 ext4_lblk_t newblock
;
1939 unsigned icount
= dx_get_count(entries
);
1940 int levels
= frame
- frames
;
1941 struct dx_entry
*entries2
;
1942 struct dx_node
*node2
;
1943 struct buffer_head
*bh2
;
1945 if (levels
&& (dx_get_count(frames
->entries
) ==
1946 dx_get_limit(frames
->entries
))) {
1947 ext4_warning(sb
, "Directory index full!");
1951 bh2
= ext4_append (handle
, dir
, &newblock
, &err
);
1954 node2
= (struct dx_node
*)(bh2
->b_data
);
1955 entries2
= node2
->entries
;
1956 memset(&node2
->fake
, 0, sizeof(struct fake_dirent
));
1957 node2
->fake
.rec_len
= ext4_rec_len_to_disk(sb
->s_blocksize
,
1959 BUFFER_TRACE(frame
->bh
, "get_write_access");
1960 err
= ext4_journal_get_write_access(handle
, frame
->bh
);
1964 unsigned icount1
= icount
/2, icount2
= icount
- icount1
;
1965 unsigned hash2
= dx_get_hash(entries
+ icount1
);
1966 dxtrace(printk(KERN_DEBUG
"Split index %i/%i\n",
1969 BUFFER_TRACE(frame
->bh
, "get_write_access"); /* index root */
1970 err
= ext4_journal_get_write_access(handle
,
1975 memcpy((char *) entries2
, (char *) (entries
+ icount1
),
1976 icount2
* sizeof(struct dx_entry
));
1977 dx_set_count(entries
, icount1
);
1978 dx_set_count(entries2
, icount2
);
1979 dx_set_limit(entries2
, dx_node_limit(dir
));
1981 /* Which index block gets the new entry? */
1982 if (at
- entries
>= icount1
) {
1983 frame
->at
= at
= at
- entries
- icount1
+ entries2
;
1984 frame
->entries
= entries
= entries2
;
1985 swap(frame
->bh
, bh2
);
1987 dx_insert_block(frames
+ 0, hash2
, newblock
);
1988 dxtrace(dx_show_index("node", frames
[1].entries
));
1989 dxtrace(dx_show_index("node",
1990 ((struct dx_node
*) bh2
->b_data
)->entries
));
1991 err
= ext4_handle_dirty_dx_node(handle
, dir
, bh2
);
1996 dxtrace(printk(KERN_DEBUG
1997 "Creating second level index...\n"));
1998 memcpy((char *) entries2
, (char *) entries
,
1999 icount
* sizeof(struct dx_entry
));
2000 dx_set_limit(entries2
, dx_node_limit(dir
));
2003 dx_set_count(entries
, 1);
2004 dx_set_block(entries
+ 0, newblock
);
2005 ((struct dx_root
*) frames
[0].bh
->b_data
)->info
.indirect_levels
= 1;
2007 /* Add new access path frame */
2009 frame
->at
= at
= at
- entries
+ entries2
;
2010 frame
->entries
= entries
= entries2
;
2012 err
= ext4_journal_get_write_access(handle
,
2017 err
= ext4_handle_dirty_dx_node(handle
, dir
, frames
[0].bh
);
2019 ext4_std_error(inode
->i_sb
, err
);
2023 de
= do_split(handle
, dir
, &bh
, frame
, &hinfo
, &err
);
2026 err
= add_dirent_to_buf(handle
, dentry
, inode
, de
, bh
);
2030 ext4_std_error(dir
->i_sb
, err
);
2039 * ext4_delete_entry deletes a directory entry by merging it with the
2042 static int ext4_delete_entry(handle_t
*handle
,
2044 struct ext4_dir_entry_2
*de_del
,
2045 struct buffer_head
*bh
)
2047 struct ext4_dir_entry_2
*de
, *pde
;
2048 unsigned int blocksize
= dir
->i_sb
->s_blocksize
;
2052 if (EXT4_HAS_RO_COMPAT_FEATURE(dir
->i_sb
,
2053 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
2054 csum_size
= sizeof(struct ext4_dir_entry_tail
);
2058 de
= (struct ext4_dir_entry_2
*) bh
->b_data
;
2059 while (i
< bh
->b_size
- csum_size
) {
2060 if (ext4_check_dir_entry(dir
, NULL
, de
, bh
, i
))
2063 BUFFER_TRACE(bh
, "get_write_access");
2064 err
= ext4_journal_get_write_access(handle
, bh
);
2065 if (unlikely(err
)) {
2066 ext4_std_error(dir
->i_sb
, err
);
2070 pde
->rec_len
= ext4_rec_len_to_disk(
2071 ext4_rec_len_from_disk(pde
->rec_len
,
2073 ext4_rec_len_from_disk(de
->rec_len
,
2079 BUFFER_TRACE(bh
, "call ext4_handle_dirty_metadata");
2080 err
= ext4_handle_dirty_dirent_node(handle
, dir
, bh
);
2081 if (unlikely(err
)) {
2082 ext4_std_error(dir
->i_sb
, err
);
2087 i
+= ext4_rec_len_from_disk(de
->rec_len
, blocksize
);
2089 de
= ext4_next_entry(de
, blocksize
);
2095 * DIR_NLINK feature is set if 1) nlinks > EXT4_LINK_MAX or 2) nlinks == 2,
2096 * since this indicates that nlinks count was previously 1.
2098 static void ext4_inc_count(handle_t
*handle
, struct inode
*inode
)
2101 if (is_dx(inode
) && inode
->i_nlink
> 1) {
2102 /* limit is 16-bit i_links_count */
2103 if (inode
->i_nlink
>= EXT4_LINK_MAX
|| inode
->i_nlink
== 2) {
2104 set_nlink(inode
, 1);
2105 EXT4_SET_RO_COMPAT_FEATURE(inode
->i_sb
,
2106 EXT4_FEATURE_RO_COMPAT_DIR_NLINK
);
2112 * If a directory had nlink == 1, then we should let it be 1. This indicates
2113 * directory has >EXT4_LINK_MAX subdirs.
2115 static void ext4_dec_count(handle_t
*handle
, struct inode
*inode
)
2117 if (!S_ISDIR(inode
->i_mode
) || inode
->i_nlink
> 2)
2122 static int ext4_add_nondir(handle_t
*handle
,
2123 struct dentry
*dentry
, struct inode
*inode
)
2125 int err
= ext4_add_entry(handle
, dentry
, inode
);
2127 ext4_mark_inode_dirty(handle
, inode
);
2128 unlock_new_inode(inode
);
2129 d_instantiate(dentry
, inode
);
2133 unlock_new_inode(inode
);
2139 * By the time this is called, we already have created
2140 * the directory cache entry for the new file, but it
2141 * is so far negative - it has no inode.
2143 * If the create succeeds, we fill in the inode information
2144 * with d_instantiate().
2146 static int ext4_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
2150 struct inode
*inode
;
2151 int err
, retries
= 0;
2153 dquot_initialize(dir
);
2156 handle
= ext4_journal_start(dir
, EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
2157 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 3 +
2158 EXT4_MAXQUOTAS_INIT_BLOCKS(dir
->i_sb
));
2160 return PTR_ERR(handle
);
2162 if (IS_DIRSYNC(dir
))
2163 ext4_handle_sync(handle
);
2165 inode
= ext4_new_inode(handle
, dir
, mode
, &dentry
->d_name
, 0, NULL
);
2166 err
= PTR_ERR(inode
);
2167 if (!IS_ERR(inode
)) {
2168 inode
->i_op
= &ext4_file_inode_operations
;
2169 inode
->i_fop
= &ext4_file_operations
;
2170 ext4_set_aops(inode
);
2171 err
= ext4_add_nondir(handle
, dentry
, inode
);
2173 ext4_journal_stop(handle
);
2174 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
2179 static int ext4_mknod(struct inode
*dir
, struct dentry
*dentry
,
2180 umode_t mode
, dev_t rdev
)
2183 struct inode
*inode
;
2184 int err
, retries
= 0;
2186 if (!new_valid_dev(rdev
))
2189 dquot_initialize(dir
);
2192 handle
= ext4_journal_start(dir
, EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
2193 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 3 +
2194 EXT4_MAXQUOTAS_INIT_BLOCKS(dir
->i_sb
));
2196 return PTR_ERR(handle
);
2198 if (IS_DIRSYNC(dir
))
2199 ext4_handle_sync(handle
);
2201 inode
= ext4_new_inode(handle
, dir
, mode
, &dentry
->d_name
, 0, NULL
);
2202 err
= PTR_ERR(inode
);
2203 if (!IS_ERR(inode
)) {
2204 init_special_inode(inode
, inode
->i_mode
, rdev
);
2205 inode
->i_op
= &ext4_special_inode_operations
;
2206 err
= ext4_add_nondir(handle
, dentry
, inode
);
2208 ext4_journal_stop(handle
);
2209 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
2214 static int ext4_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
2217 struct inode
*inode
;
2218 struct buffer_head
*dir_block
= NULL
;
2219 struct ext4_dir_entry_2
*de
;
2220 struct ext4_dir_entry_tail
*t
;
2221 unsigned int blocksize
= dir
->i_sb
->s_blocksize
;
2223 int err
, retries
= 0;
2225 if (EXT4_HAS_RO_COMPAT_FEATURE(dir
->i_sb
,
2226 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
2227 csum_size
= sizeof(struct ext4_dir_entry_tail
);
2229 if (EXT4_DIR_LINK_MAX(dir
))
2232 dquot_initialize(dir
);
2235 handle
= ext4_journal_start(dir
, EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
2236 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 3 +
2237 EXT4_MAXQUOTAS_INIT_BLOCKS(dir
->i_sb
));
2239 return PTR_ERR(handle
);
2241 if (IS_DIRSYNC(dir
))
2242 ext4_handle_sync(handle
);
2244 inode
= ext4_new_inode(handle
, dir
, S_IFDIR
| mode
,
2245 &dentry
->d_name
, 0, NULL
);
2246 err
= PTR_ERR(inode
);
2250 inode
->i_op
= &ext4_dir_inode_operations
;
2251 inode
->i_fop
= &ext4_dir_operations
;
2252 inode
->i_size
= EXT4_I(inode
)->i_disksize
= inode
->i_sb
->s_blocksize
;
2253 if (!(dir_block
= ext4_bread(handle
, inode
, 0, 1, &err
))) {
2256 ext4_error(inode
->i_sb
,
2257 "Directory hole detected on inode %lu\n",
2260 goto out_clear_inode
;
2262 BUFFER_TRACE(dir_block
, "get_write_access");
2263 err
= ext4_journal_get_write_access(handle
, dir_block
);
2265 goto out_clear_inode
;
2266 de
= (struct ext4_dir_entry_2
*) dir_block
->b_data
;
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(dir
->i_sb
, de
, S_IFDIR
);
2273 de
= ext4_next_entry(de
, blocksize
);
2274 de
->inode
= cpu_to_le32(dir
->i_ino
);
2275 de
->rec_len
= ext4_rec_len_to_disk(blocksize
-
2276 (csum_size
+ EXT4_DIR_REC_LEN(1)),
2279 strcpy(de
->name
, "..");
2280 ext4_set_de_type(dir
->i_sb
, de
, S_IFDIR
);
2281 set_nlink(inode
, 2);
2284 t
= EXT4_DIRENT_TAIL(dir_block
->b_data
, blocksize
);
2285 initialize_dirent_tail(t
, blocksize
);
2288 BUFFER_TRACE(dir_block
, "call ext4_handle_dirty_metadata");
2289 err
= ext4_handle_dirty_dirent_node(handle
, inode
, dir_block
);
2291 goto out_clear_inode
;
2292 set_buffer_verified(dir_block
);
2293 err
= ext4_mark_inode_dirty(handle
, inode
);
2295 err
= ext4_add_entry(handle
, dentry
, inode
);
2299 unlock_new_inode(inode
);
2300 ext4_mark_inode_dirty(handle
, inode
);
2304 ext4_inc_count(handle
, dir
);
2305 ext4_update_dx_flag(dir
);
2306 err
= ext4_mark_inode_dirty(handle
, dir
);
2308 goto out_clear_inode
;
2309 unlock_new_inode(inode
);
2310 d_instantiate(dentry
, inode
);
2313 ext4_journal_stop(handle
);
2314 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
2320 * routine to check that the specified directory is empty (for rmdir)
2322 static int empty_dir(struct inode
*inode
)
2324 unsigned int offset
;
2325 struct buffer_head
*bh
;
2326 struct ext4_dir_entry_2
*de
, *de1
;
2327 struct super_block
*sb
;
2331 if (inode
->i_size
< EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2) ||
2332 !(bh
= ext4_bread(NULL
, inode
, 0, 0, &err
))) {
2334 EXT4_ERROR_INODE(inode
,
2335 "error %d reading directory lblock 0", err
);
2337 ext4_warning(inode
->i_sb
,
2338 "bad directory (dir #%lu) - no data block",
2342 if (!buffer_verified(bh
) &&
2343 !ext4_dirent_csum_verify(inode
,
2344 (struct ext4_dir_entry
*)bh
->b_data
)) {
2345 EXT4_ERROR_INODE(inode
, "checksum error reading directory "
2349 set_buffer_verified(bh
);
2350 de
= (struct ext4_dir_entry_2
*) bh
->b_data
;
2351 de1
= ext4_next_entry(de
, sb
->s_blocksize
);
2352 if (le32_to_cpu(de
->inode
) != inode
->i_ino
||
2353 !le32_to_cpu(de1
->inode
) ||
2354 strcmp(".", de
->name
) ||
2355 strcmp("..", de1
->name
)) {
2356 ext4_warning(inode
->i_sb
,
2357 "bad directory (dir #%lu) - no `.' or `..'",
2362 offset
= ext4_rec_len_from_disk(de
->rec_len
, sb
->s_blocksize
) +
2363 ext4_rec_len_from_disk(de1
->rec_len
, sb
->s_blocksize
);
2364 de
= ext4_next_entry(de1
, sb
->s_blocksize
);
2365 while (offset
< inode
->i_size
) {
2367 (void *) de
>= (void *) (bh
->b_data
+sb
->s_blocksize
)) {
2368 unsigned int lblock
;
2371 lblock
= offset
>> EXT4_BLOCK_SIZE_BITS(sb
);
2372 bh
= ext4_bread(NULL
, inode
, lblock
, 0, &err
);
2375 EXT4_ERROR_INODE(inode
,
2376 "error %d reading directory "
2377 "lblock %u", err
, lblock
);
2379 ext4_warning(inode
->i_sb
,
2380 "bad directory (dir #%lu) - no data block",
2383 offset
+= sb
->s_blocksize
;
2386 if (!buffer_verified(bh
) &&
2387 !ext4_dirent_csum_verify(inode
,
2388 (struct ext4_dir_entry
*)bh
->b_data
)) {
2389 EXT4_ERROR_INODE(inode
, "checksum error "
2390 "reading directory lblock 0");
2393 set_buffer_verified(bh
);
2394 de
= (struct ext4_dir_entry_2
*) bh
->b_data
;
2396 if (ext4_check_dir_entry(inode
, NULL
, de
, bh
, offset
)) {
2397 de
= (struct ext4_dir_entry_2
*)(bh
->b_data
+
2399 offset
= (offset
| (sb
->s_blocksize
- 1)) + 1;
2402 if (le32_to_cpu(de
->inode
)) {
2406 offset
+= ext4_rec_len_from_disk(de
->rec_len
, sb
->s_blocksize
);
2407 de
= ext4_next_entry(de
, sb
->s_blocksize
);
2413 /* ext4_orphan_add() links an unlinked or truncated inode into a list of
2414 * such inodes, starting at the superblock, in case we crash before the
2415 * file is closed/deleted, or in case the inode truncate spans multiple
2416 * transactions and the last transaction is not recovered after a crash.
2418 * At filesystem recovery time, we walk this list deleting unlinked
2419 * inodes and truncating linked inodes in ext4_orphan_cleanup().
2421 int ext4_orphan_add(handle_t
*handle
, struct inode
*inode
)
2423 struct super_block
*sb
= inode
->i_sb
;
2424 struct ext4_iloc iloc
;
2427 if (!EXT4_SB(sb
)->s_journal
)
2430 mutex_lock(&EXT4_SB(sb
)->s_orphan_lock
);
2431 if (!list_empty(&EXT4_I(inode
)->i_orphan
))
2435 * Orphan handling is only valid for files with data blocks
2436 * being truncated, or files being unlinked. Note that we either
2437 * hold i_mutex, or the inode can not be referenced from outside,
2438 * so i_nlink should not be bumped due to race
2440 J_ASSERT((S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
2441 S_ISLNK(inode
->i_mode
)) || inode
->i_nlink
== 0);
2443 BUFFER_TRACE(EXT4_SB(sb
)->s_sbh
, "get_write_access");
2444 err
= ext4_journal_get_write_access(handle
, EXT4_SB(sb
)->s_sbh
);
2448 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
2452 * Due to previous errors inode may be already a part of on-disk
2453 * orphan list. If so skip on-disk list modification.
2455 if (NEXT_ORPHAN(inode
) && NEXT_ORPHAN(inode
) <=
2456 (le32_to_cpu(EXT4_SB(sb
)->s_es
->s_inodes_count
)))
2459 /* Insert this inode at the head of the on-disk orphan list... */
2460 NEXT_ORPHAN(inode
) = le32_to_cpu(EXT4_SB(sb
)->s_es
->s_last_orphan
);
2461 EXT4_SB(sb
)->s_es
->s_last_orphan
= cpu_to_le32(inode
->i_ino
);
2462 err
= ext4_handle_dirty_super(handle
, sb
);
2463 rc
= ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
2467 /* Only add to the head of the in-memory list if all the
2468 * previous operations succeeded. If the orphan_add is going to
2469 * fail (possibly taking the journal offline), we can't risk
2470 * leaving the inode on the orphan list: stray orphan-list
2471 * entries can cause panics at unmount time.
2473 * This is safe: on error we're going to ignore the orphan list
2474 * anyway on the next recovery. */
2477 list_add(&EXT4_I(inode
)->i_orphan
, &EXT4_SB(sb
)->s_orphan
);
2479 jbd_debug(4, "superblock will point to %lu\n", inode
->i_ino
);
2480 jbd_debug(4, "orphan inode %lu will point to %d\n",
2481 inode
->i_ino
, NEXT_ORPHAN(inode
));
2483 mutex_unlock(&EXT4_SB(sb
)->s_orphan_lock
);
2484 ext4_std_error(inode
->i_sb
, err
);
2489 * ext4_orphan_del() removes an unlinked or truncated inode from the list
2490 * of such inodes stored on disk, because it is finally being cleaned up.
2492 int ext4_orphan_del(handle_t
*handle
, struct inode
*inode
)
2494 struct list_head
*prev
;
2495 struct ext4_inode_info
*ei
= EXT4_I(inode
);
2496 struct ext4_sb_info
*sbi
;
2498 struct ext4_iloc iloc
;
2501 if (!EXT4_SB(inode
->i_sb
)->s_journal
)
2504 mutex_lock(&EXT4_SB(inode
->i_sb
)->s_orphan_lock
);
2505 if (list_empty(&ei
->i_orphan
))
2508 ino_next
= NEXT_ORPHAN(inode
);
2509 prev
= ei
->i_orphan
.prev
;
2510 sbi
= EXT4_SB(inode
->i_sb
);
2512 jbd_debug(4, "remove inode %lu from orphan list\n", inode
->i_ino
);
2514 list_del_init(&ei
->i_orphan
);
2516 /* If we're on an error path, we may not have a valid
2517 * transaction handle with which to update the orphan list on
2518 * disk, but we still need to remove the inode from the linked
2519 * list in memory. */
2523 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
2527 if (prev
== &sbi
->s_orphan
) {
2528 jbd_debug(4, "superblock will point to %u\n", ino_next
);
2529 BUFFER_TRACE(sbi
->s_sbh
, "get_write_access");
2530 err
= ext4_journal_get_write_access(handle
, sbi
->s_sbh
);
2533 sbi
->s_es
->s_last_orphan
= cpu_to_le32(ino_next
);
2534 err
= ext4_handle_dirty_super(handle
, inode
->i_sb
);
2536 struct ext4_iloc iloc2
;
2537 struct inode
*i_prev
=
2538 &list_entry(prev
, struct ext4_inode_info
, i_orphan
)->vfs_inode
;
2540 jbd_debug(4, "orphan inode %lu will point to %u\n",
2541 i_prev
->i_ino
, ino_next
);
2542 err
= ext4_reserve_inode_write(handle
, i_prev
, &iloc2
);
2545 NEXT_ORPHAN(i_prev
) = ino_next
;
2546 err
= ext4_mark_iloc_dirty(handle
, i_prev
, &iloc2
);
2550 NEXT_ORPHAN(inode
) = 0;
2551 err
= ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
2554 ext4_std_error(inode
->i_sb
, err
);
2556 mutex_unlock(&EXT4_SB(inode
->i_sb
)->s_orphan_lock
);
2564 static int ext4_rmdir(struct inode
*dir
, struct dentry
*dentry
)
2567 struct inode
*inode
;
2568 struct buffer_head
*bh
;
2569 struct ext4_dir_entry_2
*de
;
2572 /* Initialize quotas before so that eventual writes go in
2573 * separate transaction */
2574 dquot_initialize(dir
);
2575 dquot_initialize(dentry
->d_inode
);
2577 handle
= ext4_journal_start(dir
, EXT4_DELETE_TRANS_BLOCKS(dir
->i_sb
));
2579 return PTR_ERR(handle
);
2582 bh
= ext4_find_entry(dir
, &dentry
->d_name
, &de
);
2586 if (IS_DIRSYNC(dir
))
2587 ext4_handle_sync(handle
);
2589 inode
= dentry
->d_inode
;
2592 if (le32_to_cpu(de
->inode
) != inode
->i_ino
)
2595 retval
= -ENOTEMPTY
;
2596 if (!empty_dir(inode
))
2599 retval
= ext4_delete_entry(handle
, dir
, de
, bh
);
2602 if (!EXT4_DIR_LINK_EMPTY(inode
))
2603 ext4_warning(inode
->i_sb
,
2604 "empty directory has too many links (%d)",
2608 /* There's no need to set i_disksize: the fact that i_nlink is
2609 * zero will ensure that the right thing happens during any
2612 ext4_orphan_add(handle
, inode
);
2613 inode
->i_ctime
= dir
->i_ctime
= dir
->i_mtime
= ext4_current_time(inode
);
2614 ext4_mark_inode_dirty(handle
, inode
);
2615 ext4_dec_count(handle
, dir
);
2616 ext4_update_dx_flag(dir
);
2617 ext4_mark_inode_dirty(handle
, dir
);
2620 ext4_journal_stop(handle
);
2625 static int ext4_unlink(struct inode
*dir
, struct dentry
*dentry
)
2628 struct inode
*inode
;
2629 struct buffer_head
*bh
;
2630 struct ext4_dir_entry_2
*de
;
2633 trace_ext4_unlink_enter(dir
, dentry
);
2634 /* Initialize quotas before so that eventual writes go
2635 * in separate transaction */
2636 dquot_initialize(dir
);
2637 dquot_initialize(dentry
->d_inode
);
2639 handle
= ext4_journal_start(dir
, EXT4_DELETE_TRANS_BLOCKS(dir
->i_sb
));
2641 return PTR_ERR(handle
);
2643 if (IS_DIRSYNC(dir
))
2644 ext4_handle_sync(handle
);
2647 bh
= ext4_find_entry(dir
, &dentry
->d_name
, &de
);
2651 inode
= dentry
->d_inode
;
2654 if (le32_to_cpu(de
->inode
) != inode
->i_ino
)
2657 if (!inode
->i_nlink
) {
2658 ext4_warning(inode
->i_sb
,
2659 "Deleting nonexistent file (%lu), %d",
2660 inode
->i_ino
, inode
->i_nlink
);
2661 set_nlink(inode
, 1);
2663 retval
= ext4_delete_entry(handle
, dir
, de
, bh
);
2666 dir
->i_ctime
= dir
->i_mtime
= ext4_current_time(dir
);
2667 ext4_update_dx_flag(dir
);
2668 ext4_mark_inode_dirty(handle
, dir
);
2670 if (!inode
->i_nlink
)
2671 ext4_orphan_add(handle
, inode
);
2672 inode
->i_ctime
= ext4_current_time(inode
);
2673 ext4_mark_inode_dirty(handle
, inode
);
2677 ext4_journal_stop(handle
);
2679 trace_ext4_unlink_exit(dentry
, retval
);
2683 static int ext4_symlink(struct inode
*dir
,
2684 struct dentry
*dentry
, const char *symname
)
2687 struct inode
*inode
;
2688 int l
, err
, retries
= 0;
2691 l
= strlen(symname
)+1;
2692 if (l
> dir
->i_sb
->s_blocksize
)
2693 return -ENAMETOOLONG
;
2695 dquot_initialize(dir
);
2697 if (l
> EXT4_N_BLOCKS
* 4) {
2699 * For non-fast symlinks, we just allocate inode and put it on
2700 * orphan list in the first transaction => we need bitmap,
2701 * group descriptor, sb, inode block, quota blocks, and
2702 * possibly selinux xattr blocks.
2704 credits
= 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir
->i_sb
) +
2705 EXT4_XATTR_TRANS_BLOCKS
;
2708 * Fast symlink. We have to add entry to directory
2709 * (EXT4_DATA_TRANS_BLOCKS + EXT4_INDEX_EXTRA_TRANS_BLOCKS),
2710 * allocate new inode (bitmap, group descriptor, inode block,
2711 * quota blocks, sb is already counted in previous macros).
2713 credits
= EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
2714 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 3 +
2715 EXT4_MAXQUOTAS_INIT_BLOCKS(dir
->i_sb
);
2718 handle
= ext4_journal_start(dir
, credits
);
2720 return PTR_ERR(handle
);
2722 if (IS_DIRSYNC(dir
))
2723 ext4_handle_sync(handle
);
2725 inode
= ext4_new_inode(handle
, dir
, S_IFLNK
|S_IRWXUGO
,
2726 &dentry
->d_name
, 0, NULL
);
2727 err
= PTR_ERR(inode
);
2731 if (l
> EXT4_N_BLOCKS
* 4) {
2732 inode
->i_op
= &ext4_symlink_inode_operations
;
2733 ext4_set_aops(inode
);
2735 * We cannot call page_symlink() with transaction started
2736 * because it calls into ext4_write_begin() which can wait
2737 * for transaction commit if we are running out of space
2738 * and thus we deadlock. So we have to stop transaction now
2739 * and restart it when symlink contents is written.
2741 * To keep fs consistent in case of crash, we have to put inode
2742 * to orphan list in the mean time.
2745 err
= ext4_orphan_add(handle
, inode
);
2746 ext4_journal_stop(handle
);
2748 goto err_drop_inode
;
2749 err
= __page_symlink(inode
, symname
, l
, 1);
2751 goto err_drop_inode
;
2753 * Now inode is being linked into dir (EXT4_DATA_TRANS_BLOCKS
2754 * + EXT4_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
2756 handle
= ext4_journal_start(dir
,
2757 EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
2758 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 1);
2759 if (IS_ERR(handle
)) {
2760 err
= PTR_ERR(handle
);
2761 goto err_drop_inode
;
2763 set_nlink(inode
, 1);
2764 err
= ext4_orphan_del(handle
, inode
);
2766 ext4_journal_stop(handle
);
2768 goto err_drop_inode
;
2771 /* clear the extent format for fast symlink */
2772 ext4_clear_inode_flag(inode
, EXT4_INODE_EXTENTS
);
2773 inode
->i_op
= &ext4_fast_symlink_inode_operations
;
2774 memcpy((char *)&EXT4_I(inode
)->i_data
, symname
, l
);
2775 inode
->i_size
= l
-1;
2777 EXT4_I(inode
)->i_disksize
= inode
->i_size
;
2778 err
= ext4_add_nondir(handle
, dentry
, inode
);
2780 ext4_journal_stop(handle
);
2781 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
2785 unlock_new_inode(inode
);
2790 static int ext4_link(struct dentry
*old_dentry
,
2791 struct inode
*dir
, struct dentry
*dentry
)
2794 struct inode
*inode
= old_dentry
->d_inode
;
2795 int err
, retries
= 0;
2797 if (inode
->i_nlink
>= EXT4_LINK_MAX
)
2800 dquot_initialize(dir
);
2803 handle
= ext4_journal_start(dir
, EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
2804 EXT4_INDEX_EXTRA_TRANS_BLOCKS
);
2806 return PTR_ERR(handle
);
2808 if (IS_DIRSYNC(dir
))
2809 ext4_handle_sync(handle
);
2811 inode
->i_ctime
= ext4_current_time(inode
);
2812 ext4_inc_count(handle
, inode
);
2815 err
= ext4_add_entry(handle
, dentry
, inode
);
2817 ext4_mark_inode_dirty(handle
, inode
);
2818 d_instantiate(dentry
, inode
);
2823 ext4_journal_stop(handle
);
2824 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
2829 #define PARENT_INO(buffer, size) \
2830 (ext4_next_entry((struct ext4_dir_entry_2 *)(buffer), size)->inode)
2833 * Anybody can rename anything with this: the permission checks are left to the
2834 * higher-level routines.
2836 static int ext4_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
2837 struct inode
*new_dir
, struct dentry
*new_dentry
)
2840 struct inode
*old_inode
, *new_inode
;
2841 struct buffer_head
*old_bh
, *new_bh
, *dir_bh
;
2842 struct ext4_dir_entry_2
*old_de
, *new_de
;
2843 int retval
, force_da_alloc
= 0;
2845 dquot_initialize(old_dir
);
2846 dquot_initialize(new_dir
);
2848 old_bh
= new_bh
= dir_bh
= NULL
;
2850 /* Initialize quotas before so that eventual writes go
2851 * in separate transaction */
2852 if (new_dentry
->d_inode
)
2853 dquot_initialize(new_dentry
->d_inode
);
2854 handle
= ext4_journal_start(old_dir
, 2 *
2855 EXT4_DATA_TRANS_BLOCKS(old_dir
->i_sb
) +
2856 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 2);
2858 return PTR_ERR(handle
);
2860 if (IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
))
2861 ext4_handle_sync(handle
);
2863 old_bh
= ext4_find_entry(old_dir
, &old_dentry
->d_name
, &old_de
);
2865 * Check for inode number is _not_ due to possible IO errors.
2866 * We might rmdir the source, keep it as pwd of some process
2867 * and merrily kill the link to whatever was created under the
2868 * same name. Goodbye sticky bit ;-<
2870 old_inode
= old_dentry
->d_inode
;
2872 if (!old_bh
|| le32_to_cpu(old_de
->inode
) != old_inode
->i_ino
)
2875 new_inode
= new_dentry
->d_inode
;
2876 new_bh
= ext4_find_entry(new_dir
, &new_dentry
->d_name
, &new_de
);
2883 if (S_ISDIR(old_inode
->i_mode
)) {
2885 retval
= -ENOTEMPTY
;
2886 if (!empty_dir(new_inode
))
2890 if (!(dir_bh
= ext4_bread(handle
, old_inode
, 0, 0, &retval
))) {
2893 ext4_error(old_inode
->i_sb
,
2894 "Directory hole detected on inode %lu\n",
2899 if (!buffer_verified(dir_bh
) &&
2900 !ext4_dirent_csum_verify(old_inode
,
2901 (struct ext4_dir_entry
*)dir_bh
->b_data
))
2903 set_buffer_verified(dir_bh
);
2904 if (le32_to_cpu(PARENT_INO(dir_bh
->b_data
,
2905 old_dir
->i_sb
->s_blocksize
)) != old_dir
->i_ino
)
2908 if (!new_inode
&& new_dir
!= old_dir
&&
2909 EXT4_DIR_LINK_MAX(new_dir
))
2911 BUFFER_TRACE(dir_bh
, "get_write_access");
2912 retval
= ext4_journal_get_write_access(handle
, dir_bh
);
2917 retval
= ext4_add_entry(handle
, new_dentry
, old_inode
);
2921 BUFFER_TRACE(new_bh
, "get write access");
2922 retval
= ext4_journal_get_write_access(handle
, new_bh
);
2925 new_de
->inode
= cpu_to_le32(old_inode
->i_ino
);
2926 if (EXT4_HAS_INCOMPAT_FEATURE(new_dir
->i_sb
,
2927 EXT4_FEATURE_INCOMPAT_FILETYPE
))
2928 new_de
->file_type
= old_de
->file_type
;
2929 new_dir
->i_version
++;
2930 new_dir
->i_ctime
= new_dir
->i_mtime
=
2931 ext4_current_time(new_dir
);
2932 ext4_mark_inode_dirty(handle
, new_dir
);
2933 BUFFER_TRACE(new_bh
, "call ext4_handle_dirty_metadata");
2934 retval
= ext4_handle_dirty_dirent_node(handle
, new_dir
, new_bh
);
2935 if (unlikely(retval
)) {
2936 ext4_std_error(new_dir
->i_sb
, retval
);
2944 * Like most other Unix systems, set the ctime for inodes on a
2947 old_inode
->i_ctime
= ext4_current_time(old_inode
);
2948 ext4_mark_inode_dirty(handle
, old_inode
);
2953 if (le32_to_cpu(old_de
->inode
) != old_inode
->i_ino
||
2954 old_de
->name_len
!= old_dentry
->d_name
.len
||
2955 strncmp(old_de
->name
, old_dentry
->d_name
.name
, old_de
->name_len
) ||
2956 (retval
= ext4_delete_entry(handle
, old_dir
,
2957 old_de
, old_bh
)) == -ENOENT
) {
2958 /* old_de could have moved from under us during htree split, so
2959 * make sure that we are deleting the right entry. We might
2960 * also be pointing to a stale entry in the unused part of
2961 * old_bh so just checking inum and the name isn't enough. */
2962 struct buffer_head
*old_bh2
;
2963 struct ext4_dir_entry_2
*old_de2
;
2965 old_bh2
= ext4_find_entry(old_dir
, &old_dentry
->d_name
, &old_de2
);
2967 retval
= ext4_delete_entry(handle
, old_dir
,
2973 ext4_warning(old_dir
->i_sb
,
2974 "Deleting old file (%lu), %d, error=%d",
2975 old_dir
->i_ino
, old_dir
->i_nlink
, retval
);
2979 ext4_dec_count(handle
, new_inode
);
2980 new_inode
->i_ctime
= ext4_current_time(new_inode
);
2982 old_dir
->i_ctime
= old_dir
->i_mtime
= ext4_current_time(old_dir
);
2983 ext4_update_dx_flag(old_dir
);
2985 PARENT_INO(dir_bh
->b_data
, new_dir
->i_sb
->s_blocksize
) =
2986 cpu_to_le32(new_dir
->i_ino
);
2987 BUFFER_TRACE(dir_bh
, "call ext4_handle_dirty_metadata");
2988 if (is_dx(old_inode
)) {
2989 retval
= ext4_handle_dirty_dx_node(handle
,
2993 retval
= ext4_handle_dirty_dirent_node(handle
,
2998 ext4_std_error(old_dir
->i_sb
, retval
);
3001 ext4_dec_count(handle
, old_dir
);
3003 /* checked empty_dir above, can't have another parent,
3004 * ext4_dec_count() won't work for many-linked dirs */
3005 clear_nlink(new_inode
);
3007 ext4_inc_count(handle
, new_dir
);
3008 ext4_update_dx_flag(new_dir
);
3009 ext4_mark_inode_dirty(handle
, new_dir
);
3012 ext4_mark_inode_dirty(handle
, old_dir
);
3014 ext4_mark_inode_dirty(handle
, new_inode
);
3015 if (!new_inode
->i_nlink
)
3016 ext4_orphan_add(handle
, new_inode
);
3017 if (!test_opt(new_dir
->i_sb
, NO_AUTO_DA_ALLOC
))
3026 ext4_journal_stop(handle
);
3027 if (retval
== 0 && force_da_alloc
)
3028 ext4_alloc_da_blocks(old_inode
);
3033 * directories can handle most operations...
3035 const struct inode_operations ext4_dir_inode_operations
= {
3036 .create
= ext4_create
,
3037 .lookup
= ext4_lookup
,
3039 .unlink
= ext4_unlink
,
3040 .symlink
= ext4_symlink
,
3041 .mkdir
= ext4_mkdir
,
3042 .rmdir
= ext4_rmdir
,
3043 .mknod
= ext4_mknod
,
3044 .rename
= ext4_rename
,
3045 .setattr
= ext4_setattr
,
3046 #ifdef CONFIG_EXT4_FS_XATTR
3047 .setxattr
= generic_setxattr
,
3048 .getxattr
= generic_getxattr
,
3049 .listxattr
= ext4_listxattr
,
3050 .removexattr
= generic_removexattr
,
3052 .get_acl
= ext4_get_acl
,
3053 .fiemap
= ext4_fiemap
,
3056 const struct inode_operations ext4_special_inode_operations
= {
3057 .setattr
= ext4_setattr
,
3058 #ifdef CONFIG_EXT4_FS_XATTR
3059 .setxattr
= generic_setxattr
,
3060 .getxattr
= generic_getxattr
,
3061 .listxattr
= ext4_listxattr
,
3062 .removexattr
= generic_removexattr
,
3064 .get_acl
= ext4_get_acl
,