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",
86 static int ext4_dx_csum_verify(struct inode
*inode
,
87 struct ext4_dir_entry
*dirent
);
93 #define ext4_read_dirblock(inode, block, type) \
94 __ext4_read_dirblock((inode), (block), (type), __LINE__)
96 static struct buffer_head
*__ext4_read_dirblock(struct inode
*inode
,
101 struct buffer_head
*bh
;
102 struct ext4_dir_entry
*dirent
;
103 int err
= 0, is_dx_block
= 0;
105 bh
= ext4_bread(NULL
, inode
, block
, 0, &err
);
108 ext4_error_inode(inode
, __func__
, line
, block
,
109 "Directory hole found");
110 return ERR_PTR(-EIO
);
112 __ext4_warning(inode
->i_sb
, __func__
, line
,
113 "error reading directory block "
114 "(ino %lu, block %lu)", inode
->i_ino
,
115 (unsigned long) block
);
118 dirent
= (struct ext4_dir_entry
*) bh
->b_data
;
119 /* Determine whether or not we have an index block */
123 else if (ext4_rec_len_from_disk(dirent
->rec_len
,
124 inode
->i_sb
->s_blocksize
) ==
125 inode
->i_sb
->s_blocksize
)
128 if (!is_dx_block
&& type
== INDEX
) {
129 ext4_error_inode(inode
, __func__
, line
, block
,
130 "directory leaf block found instead of index block");
131 return ERR_PTR(-EIO
);
133 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode
->i_sb
,
134 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
) ||
139 * An empty leaf block can get mistaken for a index block; for
140 * this reason, we can only check the index checksum when the
141 * caller is sure it should be an index block.
143 if (is_dx_block
&& type
== INDEX
) {
144 if (ext4_dx_csum_verify(inode
, dirent
))
145 set_buffer_verified(bh
);
147 ext4_error_inode(inode
, __func__
, line
, block
,
148 "Directory index failed checksum");
150 return ERR_PTR(-EIO
);
154 if (ext4_dirent_csum_verify(inode
, dirent
))
155 set_buffer_verified(bh
);
157 ext4_error_inode(inode
, __func__
, line
, block
,
158 "Directory block failed checksum");
160 return ERR_PTR(-EIO
);
167 #define assert(test) J_ASSERT(test)
171 #define dxtrace(command) command
173 #define dxtrace(command)
197 * dx_root_info is laid out so that if it should somehow get overlaid by a
198 * dirent the two low bits of the hash version will be zero. Therefore, the
199 * hash version mod 4 should never be 0. Sincerely, the paranoia department.
204 struct fake_dirent dot
;
206 struct fake_dirent dotdot
;
210 __le32 reserved_zero
;
212 u8 info_length
; /* 8 */
217 struct dx_entry entries
[0];
222 struct fake_dirent fake
;
223 struct dx_entry entries
[0];
229 struct buffer_head
*bh
;
230 struct dx_entry
*entries
;
242 * This goes at the end of each htree block.
246 __le32 dt_checksum
; /* crc32c(uuid+inum+dirblock) */
249 static inline ext4_lblk_t
dx_get_block(struct dx_entry
*entry
);
250 static void dx_set_block(struct dx_entry
*entry
, ext4_lblk_t value
);
251 static inline unsigned dx_get_hash(struct dx_entry
*entry
);
252 static void dx_set_hash(struct dx_entry
*entry
, unsigned value
);
253 static unsigned dx_get_count(struct dx_entry
*entries
);
254 static unsigned dx_get_limit(struct dx_entry
*entries
);
255 static void dx_set_count(struct dx_entry
*entries
, unsigned value
);
256 static void dx_set_limit(struct dx_entry
*entries
, unsigned value
);
257 static unsigned dx_root_limit(struct inode
*dir
, unsigned infosize
);
258 static unsigned dx_node_limit(struct inode
*dir
);
259 static struct dx_frame
*dx_probe(const struct qstr
*d_name
,
261 struct dx_hash_info
*hinfo
,
262 struct dx_frame
*frame
,
264 static void dx_release(struct dx_frame
*frames
);
265 static int dx_make_map(struct ext4_dir_entry_2
*de
, unsigned blocksize
,
266 struct dx_hash_info
*hinfo
, struct dx_map_entry map
[]);
267 static void dx_sort_map(struct dx_map_entry
*map
, unsigned count
);
268 static struct ext4_dir_entry_2
*dx_move_dirents(char *from
, char *to
,
269 struct dx_map_entry
*offsets
, int count
, unsigned blocksize
);
270 static struct ext4_dir_entry_2
* dx_pack_dirents(char *base
, unsigned blocksize
);
271 static void dx_insert_block(struct dx_frame
*frame
,
272 u32 hash
, ext4_lblk_t block
);
273 static int ext4_htree_next_block(struct inode
*dir
, __u32 hash
,
274 struct dx_frame
*frame
,
275 struct dx_frame
*frames
,
277 static struct buffer_head
* ext4_dx_find_entry(struct inode
*dir
,
278 const struct qstr
*d_name
,
279 struct ext4_dir_entry_2
**res_dir
,
281 static int ext4_dx_add_entry(handle_t
*handle
, struct dentry
*dentry
,
282 struct inode
*inode
);
284 /* checksumming functions */
285 void initialize_dirent_tail(struct ext4_dir_entry_tail
*t
,
286 unsigned int blocksize
)
288 memset(t
, 0, sizeof(struct ext4_dir_entry_tail
));
289 t
->det_rec_len
= ext4_rec_len_to_disk(
290 sizeof(struct ext4_dir_entry_tail
), blocksize
);
291 t
->det_reserved_ft
= EXT4_FT_DIR_CSUM
;
294 /* Walk through a dirent block to find a checksum "dirent" at the tail */
295 static struct ext4_dir_entry_tail
*get_dirent_tail(struct inode
*inode
,
296 struct ext4_dir_entry
*de
)
298 struct ext4_dir_entry_tail
*t
;
301 struct ext4_dir_entry
*d
, *top
;
304 top
= (struct ext4_dir_entry
*)(((void *)de
) +
305 (EXT4_BLOCK_SIZE(inode
->i_sb
) -
306 sizeof(struct ext4_dir_entry_tail
)));
307 while (d
< top
&& d
->rec_len
)
308 d
= (struct ext4_dir_entry
*)(((void *)d
) +
309 le16_to_cpu(d
->rec_len
));
314 t
= (struct ext4_dir_entry_tail
*)d
;
316 t
= EXT4_DIRENT_TAIL(de
, EXT4_BLOCK_SIZE(inode
->i_sb
));
319 if (t
->det_reserved_zero1
||
320 le16_to_cpu(t
->det_rec_len
) != sizeof(struct ext4_dir_entry_tail
) ||
321 t
->det_reserved_zero2
||
322 t
->det_reserved_ft
!= EXT4_FT_DIR_CSUM
)
328 static __le32
ext4_dirent_csum(struct inode
*inode
,
329 struct ext4_dir_entry
*dirent
, int size
)
331 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
332 struct ext4_inode_info
*ei
= EXT4_I(inode
);
335 csum
= ext4_chksum(sbi
, ei
->i_csum_seed
, (__u8
*)dirent
, size
);
336 return cpu_to_le32(csum
);
339 static void warn_no_space_for_csum(struct inode
*inode
)
341 ext4_warning(inode
->i_sb
, "no space in directory inode %lu leaf for "
342 "checksum. Please run e2fsck -D.", inode
->i_ino
);
345 int ext4_dirent_csum_verify(struct inode
*inode
, struct ext4_dir_entry
*dirent
)
347 struct ext4_dir_entry_tail
*t
;
349 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode
->i_sb
,
350 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
353 t
= get_dirent_tail(inode
, dirent
);
355 warn_no_space_for_csum(inode
);
359 if (t
->det_checksum
!= ext4_dirent_csum(inode
, dirent
,
360 (void *)t
- (void *)dirent
))
366 static void ext4_dirent_csum_set(struct inode
*inode
,
367 struct ext4_dir_entry
*dirent
)
369 struct ext4_dir_entry_tail
*t
;
371 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode
->i_sb
,
372 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
375 t
= get_dirent_tail(inode
, dirent
);
377 warn_no_space_for_csum(inode
);
381 t
->det_checksum
= ext4_dirent_csum(inode
, dirent
,
382 (void *)t
- (void *)dirent
);
385 int ext4_handle_dirty_dirent_node(handle_t
*handle
,
387 struct buffer_head
*bh
)
389 ext4_dirent_csum_set(inode
, (struct ext4_dir_entry
*)bh
->b_data
);
390 return ext4_handle_dirty_metadata(handle
, inode
, bh
);
393 static struct dx_countlimit
*get_dx_countlimit(struct inode
*inode
,
394 struct ext4_dir_entry
*dirent
,
397 struct ext4_dir_entry
*dp
;
398 struct dx_root_info
*root
;
401 if (le16_to_cpu(dirent
->rec_len
) == EXT4_BLOCK_SIZE(inode
->i_sb
))
403 else if (le16_to_cpu(dirent
->rec_len
) == 12) {
404 dp
= (struct ext4_dir_entry
*)(((void *)dirent
) + 12);
405 if (le16_to_cpu(dp
->rec_len
) !=
406 EXT4_BLOCK_SIZE(inode
->i_sb
) - 12)
408 root
= (struct dx_root_info
*)(((void *)dp
+ 12));
409 if (root
->reserved_zero
||
410 root
->info_length
!= sizeof(struct dx_root_info
))
417 *offset
= count_offset
;
418 return (struct dx_countlimit
*)(((void *)dirent
) + count_offset
);
421 static __le32
ext4_dx_csum(struct inode
*inode
, struct ext4_dir_entry
*dirent
,
422 int count_offset
, int count
, struct dx_tail
*t
)
424 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
425 struct ext4_inode_info
*ei
= EXT4_I(inode
);
426 __u32 csum
, old_csum
;
429 size
= count_offset
+ (count
* sizeof(struct dx_entry
));
430 old_csum
= t
->dt_checksum
;
432 csum
= ext4_chksum(sbi
, ei
->i_csum_seed
, (__u8
*)dirent
, size
);
433 csum
= ext4_chksum(sbi
, csum
, (__u8
*)t
, sizeof(struct dx_tail
));
434 t
->dt_checksum
= old_csum
;
436 return cpu_to_le32(csum
);
439 static int ext4_dx_csum_verify(struct inode
*inode
,
440 struct ext4_dir_entry
*dirent
)
442 struct dx_countlimit
*c
;
444 int count_offset
, limit
, count
;
446 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode
->i_sb
,
447 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
450 c
= get_dx_countlimit(inode
, dirent
, &count_offset
);
452 EXT4_ERROR_INODE(inode
, "dir seems corrupt? Run e2fsck -D.");
455 limit
= le16_to_cpu(c
->limit
);
456 count
= le16_to_cpu(c
->count
);
457 if (count_offset
+ (limit
* sizeof(struct dx_entry
)) >
458 EXT4_BLOCK_SIZE(inode
->i_sb
) - sizeof(struct dx_tail
)) {
459 warn_no_space_for_csum(inode
);
462 t
= (struct dx_tail
*)(((struct dx_entry
*)c
) + limit
);
464 if (t
->dt_checksum
!= ext4_dx_csum(inode
, dirent
, count_offset
,
470 static void ext4_dx_csum_set(struct inode
*inode
, struct ext4_dir_entry
*dirent
)
472 struct dx_countlimit
*c
;
474 int count_offset
, limit
, count
;
476 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode
->i_sb
,
477 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
480 c
= get_dx_countlimit(inode
, dirent
, &count_offset
);
482 EXT4_ERROR_INODE(inode
, "dir seems corrupt? Run e2fsck -D.");
485 limit
= le16_to_cpu(c
->limit
);
486 count
= le16_to_cpu(c
->count
);
487 if (count_offset
+ (limit
* sizeof(struct dx_entry
)) >
488 EXT4_BLOCK_SIZE(inode
->i_sb
) - sizeof(struct dx_tail
)) {
489 warn_no_space_for_csum(inode
);
492 t
= (struct dx_tail
*)(((struct dx_entry
*)c
) + limit
);
494 t
->dt_checksum
= ext4_dx_csum(inode
, dirent
, count_offset
, count
, t
);
497 static inline int ext4_handle_dirty_dx_node(handle_t
*handle
,
499 struct buffer_head
*bh
)
501 ext4_dx_csum_set(inode
, (struct ext4_dir_entry
*)bh
->b_data
);
502 return ext4_handle_dirty_metadata(handle
, inode
, bh
);
506 * p is at least 6 bytes before the end of page
508 static inline struct ext4_dir_entry_2
*
509 ext4_next_entry(struct ext4_dir_entry_2
*p
, unsigned long blocksize
)
511 return (struct ext4_dir_entry_2
*)((char *)p
+
512 ext4_rec_len_from_disk(p
->rec_len
, blocksize
));
516 * Future: use high four bits of block for coalesce-on-delete flags
517 * Mask them off for now.
520 static inline ext4_lblk_t
dx_get_block(struct dx_entry
*entry
)
522 return le32_to_cpu(entry
->block
) & 0x00ffffff;
525 static inline void dx_set_block(struct dx_entry
*entry
, ext4_lblk_t value
)
527 entry
->block
= cpu_to_le32(value
);
530 static inline unsigned dx_get_hash(struct dx_entry
*entry
)
532 return le32_to_cpu(entry
->hash
);
535 static inline void dx_set_hash(struct dx_entry
*entry
, unsigned value
)
537 entry
->hash
= cpu_to_le32(value
);
540 static inline unsigned dx_get_count(struct dx_entry
*entries
)
542 return le16_to_cpu(((struct dx_countlimit
*) entries
)->count
);
545 static inline unsigned dx_get_limit(struct dx_entry
*entries
)
547 return le16_to_cpu(((struct dx_countlimit
*) entries
)->limit
);
550 static inline void dx_set_count(struct dx_entry
*entries
, unsigned value
)
552 ((struct dx_countlimit
*) entries
)->count
= cpu_to_le16(value
);
555 static inline void dx_set_limit(struct dx_entry
*entries
, unsigned value
)
557 ((struct dx_countlimit
*) entries
)->limit
= cpu_to_le16(value
);
560 static inline unsigned dx_root_limit(struct inode
*dir
, unsigned infosize
)
562 unsigned entry_space
= dir
->i_sb
->s_blocksize
- EXT4_DIR_REC_LEN(1) -
563 EXT4_DIR_REC_LEN(2) - infosize
;
565 if (EXT4_HAS_RO_COMPAT_FEATURE(dir
->i_sb
,
566 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
567 entry_space
-= sizeof(struct dx_tail
);
568 return entry_space
/ sizeof(struct dx_entry
);
571 static inline unsigned dx_node_limit(struct inode
*dir
)
573 unsigned entry_space
= dir
->i_sb
->s_blocksize
- EXT4_DIR_REC_LEN(0);
575 if (EXT4_HAS_RO_COMPAT_FEATURE(dir
->i_sb
,
576 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
577 entry_space
-= sizeof(struct dx_tail
);
578 return entry_space
/ sizeof(struct dx_entry
);
585 static void dx_show_index(char * label
, struct dx_entry
*entries
)
587 int i
, n
= dx_get_count (entries
);
588 printk(KERN_DEBUG
"%s index ", label
);
589 for (i
= 0; i
< n
; i
++) {
590 printk("%x->%lu ", i
? dx_get_hash(entries
+ i
) :
591 0, (unsigned long)dx_get_block(entries
+ i
));
603 static struct stats
dx_show_leaf(struct dx_hash_info
*hinfo
, struct ext4_dir_entry_2
*de
,
604 int size
, int show_names
)
606 unsigned names
= 0, space
= 0;
607 char *base
= (char *) de
;
608 struct dx_hash_info h
= *hinfo
;
611 while ((char *) de
< base
+ size
)
617 int len
= de
->name_len
;
618 char *name
= de
->name
;
619 while (len
--) printk("%c", *name
++);
620 ext4fs_dirhash(de
->name
, de
->name_len
, &h
);
621 printk(":%x.%u ", h
.hash
,
622 (unsigned) ((char *) de
- base
));
624 space
+= EXT4_DIR_REC_LEN(de
->name_len
);
627 de
= ext4_next_entry(de
, size
);
629 printk("(%i)\n", names
);
630 return (struct stats
) { names
, space
, 1 };
633 struct stats
dx_show_entries(struct dx_hash_info
*hinfo
, struct inode
*dir
,
634 struct dx_entry
*entries
, int levels
)
636 unsigned blocksize
= dir
->i_sb
->s_blocksize
;
637 unsigned count
= dx_get_count(entries
), names
= 0, space
= 0, i
;
639 struct buffer_head
*bh
;
641 printk("%i indexed blocks...\n", count
);
642 for (i
= 0; i
< count
; i
++, entries
++)
644 ext4_lblk_t block
= dx_get_block(entries
);
645 ext4_lblk_t hash
= i
? dx_get_hash(entries
): 0;
646 u32 range
= i
< count
- 1? (dx_get_hash(entries
+ 1) - hash
): ~hash
;
648 printk("%s%3u:%03u hash %8x/%8x ",levels
?"":" ", i
, block
, hash
, range
);
649 if (!(bh
= ext4_bread (NULL
,dir
, block
, 0,&err
))) continue;
651 dx_show_entries(hinfo
, dir
, ((struct dx_node
*) bh
->b_data
)->entries
, levels
- 1):
652 dx_show_leaf(hinfo
, (struct ext4_dir_entry_2
*) bh
->b_data
, blocksize
, 0);
653 names
+= stats
.names
;
654 space
+= stats
.space
;
655 bcount
+= stats
.bcount
;
659 printk(KERN_DEBUG
"%snames %u, fullness %u (%u%%)\n",
660 levels
? "" : " ", names
, space
/bcount
,
661 (space
/bcount
)*100/blocksize
);
662 return (struct stats
) { names
, space
, bcount
};
664 #endif /* DX_DEBUG */
667 * Probe for a directory leaf block to search.
669 * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
670 * error in the directory index, and the caller should fall back to
671 * searching the directory normally. The callers of dx_probe **MUST**
672 * check for this error code, and make sure it never gets reflected
675 static struct dx_frame
*
676 dx_probe(const struct qstr
*d_name
, struct inode
*dir
,
677 struct dx_hash_info
*hinfo
, struct dx_frame
*frame_in
, int *err
)
679 unsigned count
, indirect
;
680 struct dx_entry
*at
, *entries
, *p
, *q
, *m
;
681 struct dx_root
*root
;
682 struct buffer_head
*bh
;
683 struct dx_frame
*frame
= frame_in
;
687 bh
= ext4_read_dirblock(dir
, 0, INDEX
);
692 root
= (struct dx_root
*) bh
->b_data
;
693 if (root
->info
.hash_version
!= DX_HASH_TEA
&&
694 root
->info
.hash_version
!= DX_HASH_HALF_MD4
&&
695 root
->info
.hash_version
!= DX_HASH_LEGACY
) {
696 ext4_warning(dir
->i_sb
, "Unrecognised inode hash code %d",
697 root
->info
.hash_version
);
699 *err
= ERR_BAD_DX_DIR
;
702 hinfo
->hash_version
= root
->info
.hash_version
;
703 if (hinfo
->hash_version
<= DX_HASH_TEA
)
704 hinfo
->hash_version
+= EXT4_SB(dir
->i_sb
)->s_hash_unsigned
;
705 hinfo
->seed
= EXT4_SB(dir
->i_sb
)->s_hash_seed
;
707 ext4fs_dirhash(d_name
->name
, d_name
->len
, hinfo
);
710 if (root
->info
.unused_flags
& 1) {
711 ext4_warning(dir
->i_sb
, "Unimplemented inode hash flags: %#06x",
712 root
->info
.unused_flags
);
714 *err
= ERR_BAD_DX_DIR
;
718 if ((indirect
= root
->info
.indirect_levels
) > 1) {
719 ext4_warning(dir
->i_sb
, "Unimplemented inode hash depth: %#06x",
720 root
->info
.indirect_levels
);
722 *err
= ERR_BAD_DX_DIR
;
726 entries
= (struct dx_entry
*) (((char *)&root
->info
) +
727 root
->info
.info_length
);
729 if (dx_get_limit(entries
) != dx_root_limit(dir
,
730 root
->info
.info_length
)) {
731 ext4_warning(dir
->i_sb
, "dx entry: limit != root limit");
733 *err
= ERR_BAD_DX_DIR
;
737 dxtrace(printk("Look up %x", hash
));
740 count
= dx_get_count(entries
);
741 if (!count
|| count
> dx_get_limit(entries
)) {
742 ext4_warning(dir
->i_sb
,
743 "dx entry: no count or count > limit");
745 *err
= ERR_BAD_DX_DIR
;
750 q
= entries
+ count
- 1;
754 dxtrace(printk("."));
755 if (dx_get_hash(m
) > hash
)
761 if (0) // linear search cross check
763 unsigned n
= count
- 1;
767 dxtrace(printk(","));
768 if (dx_get_hash(++at
) > hash
)
774 assert (at
== p
- 1);
778 dxtrace(printk(" %x->%u\n", at
== entries
? 0: dx_get_hash(at
), dx_get_block(at
)));
780 frame
->entries
= entries
;
782 if (!indirect
--) return frame
;
783 bh
= ext4_read_dirblock(dir
, dx_get_block(at
), INDEX
);
788 entries
= ((struct dx_node
*) bh
->b_data
)->entries
;
790 if (dx_get_limit(entries
) != dx_node_limit (dir
)) {
791 ext4_warning(dir
->i_sb
,
792 "dx entry: limit != node limit");
794 *err
= ERR_BAD_DX_DIR
;
801 while (frame
>= frame_in
) {
806 if (*err
== ERR_BAD_DX_DIR
)
807 ext4_warning(dir
->i_sb
,
808 "Corrupt dir inode %lu, running e2fsck is "
809 "recommended.", dir
->i_ino
);
813 static void dx_release (struct dx_frame
*frames
)
815 if (frames
[0].bh
== NULL
)
818 if (((struct dx_root
*) frames
[0].bh
->b_data
)->info
.indirect_levels
)
819 brelse(frames
[1].bh
);
820 brelse(frames
[0].bh
);
824 * This function increments the frame pointer to search the next leaf
825 * block, and reads in the necessary intervening nodes if the search
826 * should be necessary. Whether or not the search is necessary is
827 * controlled by the hash parameter. If the hash value is even, then
828 * the search is only continued if the next block starts with that
829 * hash value. This is used if we are searching for a specific file.
831 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
833 * This function returns 1 if the caller should continue to search,
834 * or 0 if it should not. If there is an error reading one of the
835 * index blocks, it will a negative error code.
837 * If start_hash is non-null, it will be filled in with the starting
838 * hash of the next page.
840 static int ext4_htree_next_block(struct inode
*dir
, __u32 hash
,
841 struct dx_frame
*frame
,
842 struct dx_frame
*frames
,
846 struct buffer_head
*bh
;
852 * Find the next leaf page by incrementing the frame pointer.
853 * If we run out of entries in the interior node, loop around and
854 * increment pointer in the parent node. When we break out of
855 * this loop, num_frames indicates the number of interior
856 * nodes need to be read.
859 if (++(p
->at
) < p
->entries
+ dx_get_count(p
->entries
))
868 * If the hash is 1, then continue only if the next page has a
869 * continuation hash of any value. This is used for readdir
870 * handling. Otherwise, check to see if the hash matches the
871 * desired contiuation hash. If it doesn't, return since
872 * there's no point to read in the successive index pages.
874 bhash
= dx_get_hash(p
->at
);
877 if ((hash
& 1) == 0) {
878 if ((bhash
& ~1) != hash
)
882 * If the hash is HASH_NB_ALWAYS, we always go to the next
883 * block so no check is necessary
885 while (num_frames
--) {
886 bh
= ext4_read_dirblock(dir
, dx_get_block(p
->at
), INDEX
);
892 p
->at
= p
->entries
= ((struct dx_node
*) bh
->b_data
)->entries
;
899 * This function fills a red-black tree with information from a
900 * directory block. It returns the number directory entries loaded
901 * into the tree. If there is an error it is returned in err.
903 static int htree_dirblock_to_tree(struct file
*dir_file
,
904 struct inode
*dir
, ext4_lblk_t block
,
905 struct dx_hash_info
*hinfo
,
906 __u32 start_hash
, __u32 start_minor_hash
)
908 struct buffer_head
*bh
;
909 struct ext4_dir_entry_2
*de
, *top
;
910 int err
= 0, count
= 0;
912 dxtrace(printk(KERN_INFO
"In htree dirblock_to_tree: block %lu\n",
913 (unsigned long)block
));
914 bh
= ext4_read_dirblock(dir
, block
, DIRENT
);
918 de
= (struct ext4_dir_entry_2
*) bh
->b_data
;
919 top
= (struct ext4_dir_entry_2
*) ((char *) de
+
920 dir
->i_sb
->s_blocksize
-
921 EXT4_DIR_REC_LEN(0));
922 for (; de
< top
; de
= ext4_next_entry(de
, dir
->i_sb
->s_blocksize
)) {
923 if (ext4_check_dir_entry(dir
, NULL
, de
, bh
,
924 bh
->b_data
, bh
->b_size
,
925 (block
<<EXT4_BLOCK_SIZE_BITS(dir
->i_sb
))
926 + ((char *)de
- bh
->b_data
))) {
927 /* On error, skip the f_pos to the next block. */
928 dir_file
->f_pos
= (dir_file
->f_pos
|
929 (dir
->i_sb
->s_blocksize
- 1)) + 1;
933 ext4fs_dirhash(de
->name
, de
->name_len
, hinfo
);
934 if ((hinfo
->hash
< start_hash
) ||
935 ((hinfo
->hash
== start_hash
) &&
936 (hinfo
->minor_hash
< start_minor_hash
)))
940 if ((err
= ext4_htree_store_dirent(dir_file
,
941 hinfo
->hash
, hinfo
->minor_hash
, de
)) != 0) {
953 * This function fills a red-black tree with information from a
954 * directory. We start scanning the directory in hash order, starting
955 * at start_hash and start_minor_hash.
957 * This function returns the number of entries inserted into the tree,
958 * or a negative error code.
960 int ext4_htree_fill_tree(struct file
*dir_file
, __u32 start_hash
,
961 __u32 start_minor_hash
, __u32
*next_hash
)
963 struct dx_hash_info hinfo
;
964 struct ext4_dir_entry_2
*de
;
965 struct dx_frame frames
[2], *frame
;
972 dxtrace(printk(KERN_DEBUG
"In htree_fill_tree, start hash: %x:%x\n",
973 start_hash
, start_minor_hash
));
974 dir
= dir_file
->f_path
.dentry
->d_inode
;
975 if (!(ext4_test_inode_flag(dir
, EXT4_INODE_INDEX
))) {
976 hinfo
.hash_version
= EXT4_SB(dir
->i_sb
)->s_def_hash_version
;
977 if (hinfo
.hash_version
<= DX_HASH_TEA
)
978 hinfo
.hash_version
+=
979 EXT4_SB(dir
->i_sb
)->s_hash_unsigned
;
980 hinfo
.seed
= EXT4_SB(dir
->i_sb
)->s_hash_seed
;
981 count
= htree_dirblock_to_tree(dir_file
, dir
, 0, &hinfo
,
982 start_hash
, start_minor_hash
);
986 hinfo
.hash
= start_hash
;
987 hinfo
.minor_hash
= 0;
988 frame
= dx_probe(NULL
, dir
, &hinfo
, frames
, &err
);
992 /* Add '.' and '..' from the htree header */
993 if (!start_hash
&& !start_minor_hash
) {
994 de
= (struct ext4_dir_entry_2
*) frames
[0].bh
->b_data
;
995 if ((err
= ext4_htree_store_dirent(dir_file
, 0, 0, de
)) != 0)
999 if (start_hash
< 2 || (start_hash
==2 && start_minor_hash
==0)) {
1000 de
= (struct ext4_dir_entry_2
*) frames
[0].bh
->b_data
;
1001 de
= ext4_next_entry(de
, dir
->i_sb
->s_blocksize
);
1002 if ((err
= ext4_htree_store_dirent(dir_file
, 2, 0, de
)) != 0)
1008 block
= dx_get_block(frame
->at
);
1009 ret
= htree_dirblock_to_tree(dir_file
, dir
, block
, &hinfo
,
1010 start_hash
, start_minor_hash
);
1017 ret
= ext4_htree_next_block(dir
, HASH_NB_ALWAYS
,
1018 frame
, frames
, &hashval
);
1019 *next_hash
= hashval
;
1025 * Stop if: (a) there are no more entries, or
1026 * (b) we have inserted at least one entry and the
1027 * next hash value is not a continuation
1030 (count
&& ((hashval
& 1) == 0)))
1034 dxtrace(printk(KERN_DEBUG
"Fill tree: returned %d entries, "
1035 "next hash: %x\n", count
, *next_hash
));
1042 static inline int search_dirblock(struct buffer_head
*bh
,
1044 const struct qstr
*d_name
,
1045 unsigned int offset
,
1046 struct ext4_dir_entry_2
**res_dir
)
1048 return search_dir(bh
, bh
->b_data
, dir
->i_sb
->s_blocksize
, dir
,
1049 d_name
, offset
, res_dir
);
1053 * Directory block splitting, compacting
1057 * Create map of hash values, offsets, and sizes, stored at end of block.
1058 * Returns number of entries mapped.
1060 static int dx_make_map(struct ext4_dir_entry_2
*de
, unsigned blocksize
,
1061 struct dx_hash_info
*hinfo
,
1062 struct dx_map_entry
*map_tail
)
1065 char *base
= (char *) de
;
1066 struct dx_hash_info h
= *hinfo
;
1068 while ((char *) de
< base
+ blocksize
) {
1069 if (de
->name_len
&& de
->inode
) {
1070 ext4fs_dirhash(de
->name
, de
->name_len
, &h
);
1072 map_tail
->hash
= h
.hash
;
1073 map_tail
->offs
= ((char *) de
- base
)>>2;
1074 map_tail
->size
= le16_to_cpu(de
->rec_len
);
1078 /* XXX: do we need to check rec_len == 0 case? -Chris */
1079 de
= ext4_next_entry(de
, blocksize
);
1084 /* Sort map by hash value */
1085 static void dx_sort_map (struct dx_map_entry
*map
, unsigned count
)
1087 struct dx_map_entry
*p
, *q
, *top
= map
+ count
- 1;
1089 /* Combsort until bubble sort doesn't suck */
1091 count
= count
*10/13;
1092 if (count
- 9 < 2) /* 9, 10 -> 11 */
1094 for (p
= top
, q
= p
- count
; q
>= map
; p
--, q
--)
1095 if (p
->hash
< q
->hash
)
1098 /* Garden variety bubble sort */
1103 if (q
[1].hash
>= q
[0].hash
)
1111 static void dx_insert_block(struct dx_frame
*frame
, u32 hash
, ext4_lblk_t block
)
1113 struct dx_entry
*entries
= frame
->entries
;
1114 struct dx_entry
*old
= frame
->at
, *new = old
+ 1;
1115 int count
= dx_get_count(entries
);
1117 assert(count
< dx_get_limit(entries
));
1118 assert(old
< entries
+ count
);
1119 memmove(new + 1, new, (char *)(entries
+ count
) - (char *)(new));
1120 dx_set_hash(new, hash
);
1121 dx_set_block(new, block
);
1122 dx_set_count(entries
, count
+ 1);
1126 * NOTE! unlike strncmp, ext4_match returns 1 for success, 0 for failure.
1128 * `len <= EXT4_NAME_LEN' is guaranteed by caller.
1129 * `de != NULL' is guaranteed by caller.
1131 static inline int ext4_match (int len
, const char * const name
,
1132 struct ext4_dir_entry_2
* de
)
1134 if (len
!= de
->name_len
)
1138 return !memcmp(name
, de
->name
, len
);
1142 * Returns 0 if not found, -1 on failure, and 1 on success
1144 int search_dir(struct buffer_head
*bh
,
1148 const struct qstr
*d_name
,
1149 unsigned int offset
,
1150 struct ext4_dir_entry_2
**res_dir
)
1152 struct ext4_dir_entry_2
* de
;
1155 const char *name
= d_name
->name
;
1156 int namelen
= d_name
->len
;
1158 de
= (struct ext4_dir_entry_2
*)search_buf
;
1159 dlimit
= search_buf
+ buf_size
;
1160 while ((char *) de
< dlimit
) {
1161 /* this code is executed quadratically often */
1162 /* do minimal checking `by hand' */
1164 if ((char *) de
+ namelen
<= dlimit
&&
1165 ext4_match (namelen
, name
, de
)) {
1166 /* found a match - just to be sure, do a full check */
1167 if (ext4_check_dir_entry(dir
, NULL
, de
, bh
, bh
->b_data
,
1168 bh
->b_size
, offset
))
1173 /* prevent looping on a bad block */
1174 de_len
= ext4_rec_len_from_disk(de
->rec_len
,
1175 dir
->i_sb
->s_blocksize
);
1179 de
= (struct ext4_dir_entry_2
*) ((char *) de
+ de_len
);
1184 static int is_dx_internal_node(struct inode
*dir
, ext4_lblk_t block
,
1185 struct ext4_dir_entry
*de
)
1187 struct super_block
*sb
= dir
->i_sb
;
1193 if (de
->inode
== 0 &&
1194 ext4_rec_len_from_disk(de
->rec_len
, sb
->s_blocksize
) ==
1203 * finds an entry in the specified directory with the wanted name. It
1204 * returns the cache buffer in which the entry was found, and the entry
1205 * itself (as a parameter - res_dir). It does NOT read the inode of the
1206 * entry - you'll have to do that yourself if you want to.
1208 * The returned buffer_head has ->b_count elevated. The caller is expected
1209 * to brelse() it when appropriate.
1211 static struct buffer_head
* ext4_find_entry (struct inode
*dir
,
1212 const struct qstr
*d_name
,
1213 struct ext4_dir_entry_2
**res_dir
,
1216 struct super_block
*sb
;
1217 struct buffer_head
*bh_use
[NAMEI_RA_SIZE
];
1218 struct buffer_head
*bh
, *ret
= NULL
;
1219 ext4_lblk_t start
, block
, b
;
1220 const u8
*name
= d_name
->name
;
1221 int ra_max
= 0; /* Number of bh's in the readahead
1223 int ra_ptr
= 0; /* Current index into readahead
1226 ext4_lblk_t nblocks
;
1232 namelen
= d_name
->len
;
1233 if (namelen
> EXT4_NAME_LEN
)
1236 if (ext4_has_inline_data(dir
)) {
1237 int has_inline_data
= 1;
1238 ret
= ext4_find_inline_entry(dir
, d_name
, res_dir
,
1240 if (has_inline_data
) {
1247 if ((namelen
<= 2) && (name
[0] == '.') &&
1248 (name
[1] == '.' || name
[1] == '\0')) {
1250 * "." or ".." will only be in the first block
1251 * NFS may look up ".."; "." should be handled by the VFS
1258 bh
= ext4_dx_find_entry(dir
, d_name
, res_dir
, &err
);
1260 * On success, or if the error was file not found,
1261 * return. Otherwise, fall back to doing a search the
1262 * old fashioned way.
1264 if (bh
|| (err
!= ERR_BAD_DX_DIR
))
1266 dxtrace(printk(KERN_DEBUG
"ext4_find_entry: dx failed, "
1269 nblocks
= dir
->i_size
>> EXT4_BLOCK_SIZE_BITS(sb
);
1270 start
= EXT4_I(dir
)->i_dir_start_lookup
;
1271 if (start
>= nblocks
)
1277 * We deal with the read-ahead logic here.
1279 if (ra_ptr
>= ra_max
) {
1280 /* Refill the readahead buffer */
1283 for (ra_max
= 0; ra_max
< NAMEI_RA_SIZE
; ra_max
++) {
1285 * Terminate if we reach the end of the
1286 * directory and must wrap, or if our
1287 * search has finished at this block.
1289 if (b
>= nblocks
|| (num
&& block
== start
)) {
1290 bh_use
[ra_max
] = NULL
;
1294 bh
= ext4_getblk(NULL
, dir
, b
++, 0, &err
);
1295 bh_use
[ra_max
] = bh
;
1297 ll_rw_block(READ
| REQ_META
| REQ_PRIO
,
1301 if ((bh
= bh_use
[ra_ptr
++]) == NULL
)
1304 if (!buffer_uptodate(bh
)) {
1305 /* read error, skip block & hope for the best */
1306 EXT4_ERROR_INODE(dir
, "reading directory lblock %lu",
1307 (unsigned long) block
);
1311 if (!buffer_verified(bh
) &&
1312 !is_dx_internal_node(dir
, block
,
1313 (struct ext4_dir_entry
*)bh
->b_data
) &&
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
);
1321 set_buffer_verified(bh
);
1322 i
= search_dirblock(bh
, dir
, d_name
,
1323 block
<< EXT4_BLOCK_SIZE_BITS(sb
), res_dir
);
1325 EXT4_I(dir
)->i_dir_start_lookup
= block
;
1327 goto cleanup_and_exit
;
1331 goto cleanup_and_exit
;
1334 if (++block
>= nblocks
)
1336 } while (block
!= start
);
1339 * If the directory has grown while we were searching, then
1340 * search the last part of the directory before giving up.
1343 nblocks
= dir
->i_size
>> EXT4_BLOCK_SIZE_BITS(sb
);
1344 if (block
< nblocks
) {
1350 /* Clean up the read-ahead blocks */
1351 for (; ra_ptr
< ra_max
; ra_ptr
++)
1352 brelse(bh_use
[ra_ptr
]);
1356 static struct buffer_head
* ext4_dx_find_entry(struct inode
*dir
, const struct qstr
*d_name
,
1357 struct ext4_dir_entry_2
**res_dir
, int *err
)
1359 struct super_block
* sb
= dir
->i_sb
;
1360 struct dx_hash_info hinfo
;
1361 struct dx_frame frames
[2], *frame
;
1362 struct buffer_head
*bh
;
1366 if (!(frame
= dx_probe(d_name
, dir
, &hinfo
, frames
, err
)))
1369 block
= dx_get_block(frame
->at
);
1370 bh
= ext4_read_dirblock(dir
, block
, DIRENT
);
1375 retval
= search_dirblock(bh
, dir
, d_name
,
1376 block
<< EXT4_BLOCK_SIZE_BITS(sb
),
1378 if (retval
== 1) { /* Success! */
1384 *err
= ERR_BAD_DX_DIR
;
1388 /* Check to see if we should continue to search */
1389 retval
= ext4_htree_next_block(dir
, hinfo
.hash
, frame
,
1393 "error reading index page in directory #%lu",
1398 } while (retval
== 1);
1402 dxtrace(printk(KERN_DEBUG
"%s not found\n", d_name
->name
));
1403 dx_release (frames
);
1407 static struct dentry
*ext4_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
1409 struct inode
*inode
;
1410 struct ext4_dir_entry_2
*de
;
1411 struct buffer_head
*bh
;
1413 if (dentry
->d_name
.len
> EXT4_NAME_LEN
)
1414 return ERR_PTR(-ENAMETOOLONG
);
1416 bh
= ext4_find_entry(dir
, &dentry
->d_name
, &de
, NULL
);
1419 __u32 ino
= le32_to_cpu(de
->inode
);
1421 if (!ext4_valid_inum(dir
->i_sb
, ino
)) {
1422 EXT4_ERROR_INODE(dir
, "bad inode number: %u", ino
);
1423 return ERR_PTR(-EIO
);
1425 if (unlikely(ino
== dir
->i_ino
)) {
1426 EXT4_ERROR_INODE(dir
, "'%.*s' linked to parent dir",
1428 dentry
->d_name
.name
);
1429 return ERR_PTR(-EIO
);
1431 inode
= ext4_iget(dir
->i_sb
, ino
);
1432 if (inode
== ERR_PTR(-ESTALE
)) {
1433 EXT4_ERROR_INODE(dir
,
1434 "deleted inode referenced: %u",
1436 return ERR_PTR(-EIO
);
1439 return d_splice_alias(inode
, dentry
);
1443 struct dentry
*ext4_get_parent(struct dentry
*child
)
1446 static const struct qstr dotdot
= QSTR_INIT("..", 2);
1447 struct ext4_dir_entry_2
* de
;
1448 struct buffer_head
*bh
;
1450 bh
= ext4_find_entry(child
->d_inode
, &dotdot
, &de
, NULL
);
1452 return ERR_PTR(-ENOENT
);
1453 ino
= le32_to_cpu(de
->inode
);
1456 if (!ext4_valid_inum(child
->d_inode
->i_sb
, ino
)) {
1457 EXT4_ERROR_INODE(child
->d_inode
,
1458 "bad parent inode number: %u", ino
);
1459 return ERR_PTR(-EIO
);
1462 return d_obtain_alias(ext4_iget(child
->d_inode
->i_sb
, ino
));
1466 static unsigned char ext4_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
1467 [S_IFREG
>> S_SHIFT
] = EXT4_FT_REG_FILE
,
1468 [S_IFDIR
>> S_SHIFT
] = EXT4_FT_DIR
,
1469 [S_IFCHR
>> S_SHIFT
] = EXT4_FT_CHRDEV
,
1470 [S_IFBLK
>> S_SHIFT
] = EXT4_FT_BLKDEV
,
1471 [S_IFIFO
>> S_SHIFT
] = EXT4_FT_FIFO
,
1472 [S_IFSOCK
>> S_SHIFT
] = EXT4_FT_SOCK
,
1473 [S_IFLNK
>> S_SHIFT
] = EXT4_FT_SYMLINK
,
1476 static inline void ext4_set_de_type(struct super_block
*sb
,
1477 struct ext4_dir_entry_2
*de
,
1479 if (EXT4_HAS_INCOMPAT_FEATURE(sb
, EXT4_FEATURE_INCOMPAT_FILETYPE
))
1480 de
->file_type
= ext4_type_by_mode
[(mode
& S_IFMT
)>>S_SHIFT
];
1484 * Move count entries from end of map between two memory locations.
1485 * Returns pointer to last entry moved.
1487 static struct ext4_dir_entry_2
*
1488 dx_move_dirents(char *from
, char *to
, struct dx_map_entry
*map
, int count
,
1491 unsigned rec_len
= 0;
1494 struct ext4_dir_entry_2
*de
= (struct ext4_dir_entry_2
*)
1495 (from
+ (map
->offs
<<2));
1496 rec_len
= EXT4_DIR_REC_LEN(de
->name_len
);
1497 memcpy (to
, de
, rec_len
);
1498 ((struct ext4_dir_entry_2
*) to
)->rec_len
=
1499 ext4_rec_len_to_disk(rec_len
, blocksize
);
1504 return (struct ext4_dir_entry_2
*) (to
- rec_len
);
1508 * Compact each dir entry in the range to the minimal rec_len.
1509 * Returns pointer to last entry in range.
1511 static struct ext4_dir_entry_2
* dx_pack_dirents(char *base
, unsigned blocksize
)
1513 struct ext4_dir_entry_2
*next
, *to
, *prev
, *de
= (struct ext4_dir_entry_2
*) base
;
1514 unsigned rec_len
= 0;
1517 while ((char*)de
< base
+ blocksize
) {
1518 next
= ext4_next_entry(de
, blocksize
);
1519 if (de
->inode
&& de
->name_len
) {
1520 rec_len
= EXT4_DIR_REC_LEN(de
->name_len
);
1522 memmove(to
, de
, rec_len
);
1523 to
->rec_len
= ext4_rec_len_to_disk(rec_len
, blocksize
);
1525 to
= (struct ext4_dir_entry_2
*) (((char *) to
) + rec_len
);
1533 * Split a full leaf block to make room for a new dir entry.
1534 * Allocate a new block, and move entries so that they are approx. equally full.
1535 * Returns pointer to de in block into which the new entry will be inserted.
1537 static struct ext4_dir_entry_2
*do_split(handle_t
*handle
, struct inode
*dir
,
1538 struct buffer_head
**bh
,struct dx_frame
*frame
,
1539 struct dx_hash_info
*hinfo
, int *error
)
1541 unsigned blocksize
= dir
->i_sb
->s_blocksize
;
1542 unsigned count
, continued
;
1543 struct buffer_head
*bh2
;
1544 ext4_lblk_t newblock
;
1546 struct dx_map_entry
*map
;
1547 char *data1
= (*bh
)->b_data
, *data2
;
1548 unsigned split
, move
, size
;
1549 struct ext4_dir_entry_2
*de
= NULL
, *de2
;
1550 struct ext4_dir_entry_tail
*t
;
1554 if (EXT4_HAS_RO_COMPAT_FEATURE(dir
->i_sb
,
1555 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
1556 csum_size
= sizeof(struct ext4_dir_entry_tail
);
1558 bh2
= ext4_append (handle
, dir
, &newblock
, &err
);
1565 BUFFER_TRACE(*bh
, "get_write_access");
1566 err
= ext4_journal_get_write_access(handle
, *bh
);
1570 BUFFER_TRACE(frame
->bh
, "get_write_access");
1571 err
= ext4_journal_get_write_access(handle
, frame
->bh
);
1575 data2
= bh2
->b_data
;
1577 /* create map in the end of data2 block */
1578 map
= (struct dx_map_entry
*) (data2
+ blocksize
);
1579 count
= dx_make_map((struct ext4_dir_entry_2
*) data1
,
1580 blocksize
, hinfo
, map
);
1582 dx_sort_map(map
, count
);
1583 /* Split the existing block in the middle, size-wise */
1586 for (i
= count
-1; i
>= 0; i
--) {
1587 /* is more than half of this entry in 2nd half of the block? */
1588 if (size
+ map
[i
].size
/2 > blocksize
/2)
1590 size
+= map
[i
].size
;
1593 /* map index at which we will split */
1594 split
= count
- move
;
1595 hash2
= map
[split
].hash
;
1596 continued
= hash2
== map
[split
- 1].hash
;
1597 dxtrace(printk(KERN_INFO
"Split block %lu at %x, %i/%i\n",
1598 (unsigned long)dx_get_block(frame
->at
),
1599 hash2
, split
, count
-split
));
1601 /* Fancy dance to stay within two buffers */
1602 de2
= dx_move_dirents(data1
, data2
, map
+ split
, count
- split
, blocksize
);
1603 de
= dx_pack_dirents(data1
, blocksize
);
1604 de
->rec_len
= ext4_rec_len_to_disk(data1
+ (blocksize
- csum_size
) -
1607 de2
->rec_len
= ext4_rec_len_to_disk(data2
+ (blocksize
- csum_size
) -
1611 t
= EXT4_DIRENT_TAIL(data2
, blocksize
);
1612 initialize_dirent_tail(t
, blocksize
);
1614 t
= EXT4_DIRENT_TAIL(data1
, blocksize
);
1615 initialize_dirent_tail(t
, blocksize
);
1618 dxtrace(dx_show_leaf (hinfo
, (struct ext4_dir_entry_2
*) data1
, blocksize
, 1));
1619 dxtrace(dx_show_leaf (hinfo
, (struct ext4_dir_entry_2
*) data2
, blocksize
, 1));
1621 /* Which block gets the new entry? */
1622 if (hinfo
->hash
>= hash2
)
1627 dx_insert_block(frame
, hash2
+ continued
, newblock
);
1628 err
= ext4_handle_dirty_dirent_node(handle
, dir
, bh2
);
1631 err
= ext4_handle_dirty_dx_node(handle
, dir
, frame
->bh
);
1635 dxtrace(dx_show_index("frame", frame
->entries
));
1642 ext4_std_error(dir
->i_sb
, err
);
1648 int ext4_find_dest_de(struct inode
*dir
, struct inode
*inode
,
1649 struct buffer_head
*bh
,
1650 void *buf
, int buf_size
,
1651 const char *name
, int namelen
,
1652 struct ext4_dir_entry_2
**dest_de
)
1654 struct ext4_dir_entry_2
*de
;
1655 unsigned short reclen
= EXT4_DIR_REC_LEN(namelen
);
1657 unsigned int offset
= 0;
1660 de
= (struct ext4_dir_entry_2
*)buf
;
1661 top
= buf
+ buf_size
- reclen
;
1662 while ((char *) de
<= top
) {
1663 if (ext4_check_dir_entry(dir
, NULL
, de
, bh
,
1664 buf
, buf_size
, offset
))
1666 if (ext4_match(namelen
, name
, de
))
1668 nlen
= EXT4_DIR_REC_LEN(de
->name_len
);
1669 rlen
= ext4_rec_len_from_disk(de
->rec_len
, buf_size
);
1670 if ((de
->inode
? rlen
- nlen
: rlen
) >= reclen
)
1672 de
= (struct ext4_dir_entry_2
*)((char *)de
+ rlen
);
1675 if ((char *) de
> top
)
1682 void ext4_insert_dentry(struct inode
*inode
,
1683 struct ext4_dir_entry_2
*de
,
1685 const char *name
, int namelen
)
1690 nlen
= EXT4_DIR_REC_LEN(de
->name_len
);
1691 rlen
= ext4_rec_len_from_disk(de
->rec_len
, buf_size
);
1693 struct ext4_dir_entry_2
*de1
=
1694 (struct ext4_dir_entry_2
*)((char *)de
+ nlen
);
1695 de1
->rec_len
= ext4_rec_len_to_disk(rlen
- nlen
, buf_size
);
1696 de
->rec_len
= ext4_rec_len_to_disk(nlen
, buf_size
);
1699 de
->file_type
= EXT4_FT_UNKNOWN
;
1700 de
->inode
= cpu_to_le32(inode
->i_ino
);
1701 ext4_set_de_type(inode
->i_sb
, de
, inode
->i_mode
);
1702 de
->name_len
= namelen
;
1703 memcpy(de
->name
, name
, namelen
);
1706 * Add a new entry into a directory (leaf) block. If de is non-NULL,
1707 * it points to a directory entry which is guaranteed to be large
1708 * enough for new directory entry. If de is NULL, then
1709 * add_dirent_to_buf will attempt search the directory block for
1710 * space. It will return -ENOSPC if no space is available, and -EIO
1711 * and -EEXIST if directory entry already exists.
1713 static int add_dirent_to_buf(handle_t
*handle
, struct dentry
*dentry
,
1714 struct inode
*inode
, struct ext4_dir_entry_2
*de
,
1715 struct buffer_head
*bh
)
1717 struct inode
*dir
= dentry
->d_parent
->d_inode
;
1718 const char *name
= dentry
->d_name
.name
;
1719 int namelen
= dentry
->d_name
.len
;
1720 unsigned int blocksize
= dir
->i_sb
->s_blocksize
;
1724 if (EXT4_HAS_RO_COMPAT_FEATURE(inode
->i_sb
,
1725 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
1726 csum_size
= sizeof(struct ext4_dir_entry_tail
);
1729 err
= ext4_find_dest_de(dir
, inode
,
1730 bh
, bh
->b_data
, blocksize
- csum_size
,
1731 name
, namelen
, &de
);
1735 BUFFER_TRACE(bh
, "get_write_access");
1736 err
= ext4_journal_get_write_access(handle
, bh
);
1738 ext4_std_error(dir
->i_sb
, err
);
1742 /* By now the buffer is marked for journaling */
1743 ext4_insert_dentry(inode
, de
, blocksize
, name
, namelen
);
1746 * XXX shouldn't update any times until successful
1747 * completion of syscall, but too many callers depend
1750 * XXX similarly, too many callers depend on
1751 * ext4_new_inode() setting the times, but error
1752 * recovery deletes the inode, so the worst that can
1753 * happen is that the times are slightly out of date
1754 * and/or different from the directory change time.
1756 dir
->i_mtime
= dir
->i_ctime
= ext4_current_time(dir
);
1757 ext4_update_dx_flag(dir
);
1759 ext4_mark_inode_dirty(handle
, dir
);
1760 BUFFER_TRACE(bh
, "call ext4_handle_dirty_metadata");
1761 err
= ext4_handle_dirty_dirent_node(handle
, dir
, bh
);
1763 ext4_std_error(dir
->i_sb
, err
);
1768 * This converts a one block unindexed directory to a 3 block indexed
1769 * directory, and adds the dentry to the indexed directory.
1771 static int make_indexed_dir(handle_t
*handle
, struct dentry
*dentry
,
1772 struct inode
*inode
, struct buffer_head
*bh
)
1774 struct inode
*dir
= dentry
->d_parent
->d_inode
;
1775 const char *name
= dentry
->d_name
.name
;
1776 int namelen
= dentry
->d_name
.len
;
1777 struct buffer_head
*bh2
;
1778 struct dx_root
*root
;
1779 struct dx_frame frames
[2], *frame
;
1780 struct dx_entry
*entries
;
1781 struct ext4_dir_entry_2
*de
, *de2
;
1782 struct ext4_dir_entry_tail
*t
;
1787 struct dx_hash_info hinfo
;
1789 struct fake_dirent
*fde
;
1792 if (EXT4_HAS_RO_COMPAT_FEATURE(inode
->i_sb
,
1793 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
1794 csum_size
= sizeof(struct ext4_dir_entry_tail
);
1796 blocksize
= dir
->i_sb
->s_blocksize
;
1797 dxtrace(printk(KERN_DEBUG
"Creating index: inode %lu\n", dir
->i_ino
));
1798 retval
= ext4_journal_get_write_access(handle
, bh
);
1800 ext4_std_error(dir
->i_sb
, retval
);
1804 root
= (struct dx_root
*) bh
->b_data
;
1806 /* The 0th block becomes the root, move the dirents out */
1807 fde
= &root
->dotdot
;
1808 de
= (struct ext4_dir_entry_2
*)((char *)fde
+
1809 ext4_rec_len_from_disk(fde
->rec_len
, blocksize
));
1810 if ((char *) de
>= (((char *) root
) + blocksize
)) {
1811 EXT4_ERROR_INODE(dir
, "invalid rec_len for '..'");
1815 len
= ((char *) root
) + (blocksize
- csum_size
) - (char *) de
;
1817 /* Allocate new block for the 0th block's dirents */
1818 bh2
= ext4_append(handle
, dir
, &block
, &retval
);
1823 ext4_set_inode_flag(dir
, EXT4_INODE_INDEX
);
1824 data1
= bh2
->b_data
;
1826 memcpy (data1
, de
, len
);
1827 de
= (struct ext4_dir_entry_2
*) data1
;
1829 while ((char *)(de2
= ext4_next_entry(de
, blocksize
)) < top
)
1831 de
->rec_len
= ext4_rec_len_to_disk(data1
+ (blocksize
- csum_size
) -
1836 t
= EXT4_DIRENT_TAIL(data1
, blocksize
);
1837 initialize_dirent_tail(t
, blocksize
);
1840 /* Initialize the root; the dot dirents already exist */
1841 de
= (struct ext4_dir_entry_2
*) (&root
->dotdot
);
1842 de
->rec_len
= ext4_rec_len_to_disk(blocksize
- EXT4_DIR_REC_LEN(2),
1844 memset (&root
->info
, 0, sizeof(root
->info
));
1845 root
->info
.info_length
= sizeof(root
->info
);
1846 root
->info
.hash_version
= EXT4_SB(dir
->i_sb
)->s_def_hash_version
;
1847 entries
= root
->entries
;
1848 dx_set_block(entries
, 1);
1849 dx_set_count(entries
, 1);
1850 dx_set_limit(entries
, dx_root_limit(dir
, sizeof(root
->info
)));
1852 /* Initialize as for dx_probe */
1853 hinfo
.hash_version
= root
->info
.hash_version
;
1854 if (hinfo
.hash_version
<= DX_HASH_TEA
)
1855 hinfo
.hash_version
+= EXT4_SB(dir
->i_sb
)->s_hash_unsigned
;
1856 hinfo
.seed
= EXT4_SB(dir
->i_sb
)->s_hash_seed
;
1857 ext4fs_dirhash(name
, namelen
, &hinfo
);
1859 frame
->entries
= entries
;
1860 frame
->at
= entries
;
1864 ext4_handle_dirty_dx_node(handle
, dir
, frame
->bh
);
1865 ext4_handle_dirty_dirent_node(handle
, dir
, bh
);
1867 de
= do_split(handle
,dir
, &bh
, frame
, &hinfo
, &retval
);
1870 * Even if the block split failed, we have to properly write
1871 * out all the changes we did so far. Otherwise we can end up
1872 * with corrupted filesystem.
1874 ext4_mark_inode_dirty(handle
, dir
);
1880 retval
= add_dirent_to_buf(handle
, dentry
, inode
, de
, bh
);
1888 * adds a file entry to the specified directory, using the same
1889 * semantics as ext4_find_entry(). It returns NULL if it failed.
1891 * NOTE!! The inode part of 'de' is left at 0 - which means you
1892 * may not sleep between calling this and putting something into
1893 * the entry, as someone else might have used it while you slept.
1895 static int ext4_add_entry(handle_t
*handle
, struct dentry
*dentry
,
1896 struct inode
*inode
)
1898 struct inode
*dir
= dentry
->d_parent
->d_inode
;
1899 struct buffer_head
*bh
;
1900 struct ext4_dir_entry_2
*de
;
1901 struct ext4_dir_entry_tail
*t
;
1902 struct super_block
*sb
;
1906 ext4_lblk_t block
, blocks
;
1909 if (EXT4_HAS_RO_COMPAT_FEATURE(inode
->i_sb
,
1910 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
1911 csum_size
= sizeof(struct ext4_dir_entry_tail
);
1914 blocksize
= sb
->s_blocksize
;
1915 if (!dentry
->d_name
.len
)
1918 if (ext4_has_inline_data(dir
)) {
1919 retval
= ext4_try_add_inline_entry(handle
, dentry
, inode
);
1929 retval
= ext4_dx_add_entry(handle
, dentry
, inode
);
1930 if (!retval
|| (retval
!= ERR_BAD_DX_DIR
))
1932 ext4_clear_inode_flag(dir
, EXT4_INODE_INDEX
);
1934 ext4_mark_inode_dirty(handle
, dir
);
1936 blocks
= dir
->i_size
>> sb
->s_blocksize_bits
;
1937 for (block
= 0; block
< blocks
; block
++) {
1938 bh
= ext4_read_dirblock(dir
, block
, DIRENT
);
1942 retval
= add_dirent_to_buf(handle
, dentry
, inode
, NULL
, bh
);
1943 if (retval
!= -ENOSPC
) {
1948 if (blocks
== 1 && !dx_fallback
&&
1949 EXT4_HAS_COMPAT_FEATURE(sb
, EXT4_FEATURE_COMPAT_DIR_INDEX
))
1950 return make_indexed_dir(handle
, dentry
, inode
, bh
);
1953 bh
= ext4_append(handle
, dir
, &block
, &retval
);
1956 de
= (struct ext4_dir_entry_2
*) bh
->b_data
;
1958 de
->rec_len
= ext4_rec_len_to_disk(blocksize
- csum_size
, blocksize
);
1961 t
= EXT4_DIRENT_TAIL(bh
->b_data
, blocksize
);
1962 initialize_dirent_tail(t
, blocksize
);
1965 retval
= add_dirent_to_buf(handle
, dentry
, inode
, de
, bh
);
1968 ext4_set_inode_state(inode
, EXT4_STATE_NEWENTRY
);
1973 * Returns 0 for success, or a negative error value
1975 static int ext4_dx_add_entry(handle_t
*handle
, struct dentry
*dentry
,
1976 struct inode
*inode
)
1978 struct dx_frame frames
[2], *frame
;
1979 struct dx_entry
*entries
, *at
;
1980 struct dx_hash_info hinfo
;
1981 struct buffer_head
*bh
;
1982 struct inode
*dir
= dentry
->d_parent
->d_inode
;
1983 struct super_block
*sb
= dir
->i_sb
;
1984 struct ext4_dir_entry_2
*de
;
1987 frame
= dx_probe(&dentry
->d_name
, dir
, &hinfo
, frames
, &err
);
1990 entries
= frame
->entries
;
1992 bh
= ext4_read_dirblock(dir
, dx_get_block(frame
->at
), DIRENT
);
1999 BUFFER_TRACE(bh
, "get_write_access");
2000 err
= ext4_journal_get_write_access(handle
, bh
);
2004 err
= add_dirent_to_buf(handle
, dentry
, inode
, NULL
, bh
);
2008 /* Block full, should compress but for now just split */
2009 dxtrace(printk(KERN_DEBUG
"using %u of %u node entries\n",
2010 dx_get_count(entries
), dx_get_limit(entries
)));
2011 /* Need to split index? */
2012 if (dx_get_count(entries
) == dx_get_limit(entries
)) {
2013 ext4_lblk_t newblock
;
2014 unsigned icount
= dx_get_count(entries
);
2015 int levels
= frame
- frames
;
2016 struct dx_entry
*entries2
;
2017 struct dx_node
*node2
;
2018 struct buffer_head
*bh2
;
2020 if (levels
&& (dx_get_count(frames
->entries
) ==
2021 dx_get_limit(frames
->entries
))) {
2022 ext4_warning(sb
, "Directory index full!");
2026 bh2
= ext4_append (handle
, dir
, &newblock
, &err
);
2029 node2
= (struct dx_node
*)(bh2
->b_data
);
2030 entries2
= node2
->entries
;
2031 memset(&node2
->fake
, 0, sizeof(struct fake_dirent
));
2032 node2
->fake
.rec_len
= ext4_rec_len_to_disk(sb
->s_blocksize
,
2034 BUFFER_TRACE(frame
->bh
, "get_write_access");
2035 err
= ext4_journal_get_write_access(handle
, frame
->bh
);
2039 unsigned icount1
= icount
/2, icount2
= icount
- icount1
;
2040 unsigned hash2
= dx_get_hash(entries
+ icount1
);
2041 dxtrace(printk(KERN_DEBUG
"Split index %i/%i\n",
2044 BUFFER_TRACE(frame
->bh
, "get_write_access"); /* index root */
2045 err
= ext4_journal_get_write_access(handle
,
2050 memcpy((char *) entries2
, (char *) (entries
+ icount1
),
2051 icount2
* sizeof(struct dx_entry
));
2052 dx_set_count(entries
, icount1
);
2053 dx_set_count(entries2
, icount2
);
2054 dx_set_limit(entries2
, dx_node_limit(dir
));
2056 /* Which index block gets the new entry? */
2057 if (at
- entries
>= icount1
) {
2058 frame
->at
= at
= at
- entries
- icount1
+ entries2
;
2059 frame
->entries
= entries
= entries2
;
2060 swap(frame
->bh
, bh2
);
2062 dx_insert_block(frames
+ 0, hash2
, newblock
);
2063 dxtrace(dx_show_index("node", frames
[1].entries
));
2064 dxtrace(dx_show_index("node",
2065 ((struct dx_node
*) bh2
->b_data
)->entries
));
2066 err
= ext4_handle_dirty_dx_node(handle
, dir
, bh2
);
2071 dxtrace(printk(KERN_DEBUG
2072 "Creating second level index...\n"));
2073 memcpy((char *) entries2
, (char *) entries
,
2074 icount
* sizeof(struct dx_entry
));
2075 dx_set_limit(entries2
, dx_node_limit(dir
));
2078 dx_set_count(entries
, 1);
2079 dx_set_block(entries
+ 0, newblock
);
2080 ((struct dx_root
*) frames
[0].bh
->b_data
)->info
.indirect_levels
= 1;
2082 /* Add new access path frame */
2084 frame
->at
= at
= at
- entries
+ entries2
;
2085 frame
->entries
= entries
= entries2
;
2087 err
= ext4_journal_get_write_access(handle
,
2092 err
= ext4_handle_dirty_dx_node(handle
, dir
, frames
[0].bh
);
2094 ext4_std_error(inode
->i_sb
, err
);
2098 de
= do_split(handle
, dir
, &bh
, frame
, &hinfo
, &err
);
2101 err
= add_dirent_to_buf(handle
, dentry
, inode
, de
, bh
);
2105 ext4_std_error(dir
->i_sb
, err
);
2113 * ext4_generic_delete_entry deletes a directory entry by merging it
2114 * with the previous entry
2116 int ext4_generic_delete_entry(handle_t
*handle
,
2118 struct ext4_dir_entry_2
*de_del
,
2119 struct buffer_head
*bh
,
2124 struct ext4_dir_entry_2
*de
, *pde
;
2125 unsigned int blocksize
= dir
->i_sb
->s_blocksize
;
2130 de
= (struct ext4_dir_entry_2
*)entry_buf
;
2131 while (i
< buf_size
- csum_size
) {
2132 if (ext4_check_dir_entry(dir
, NULL
, de
, bh
,
2133 bh
->b_data
, bh
->b_size
, i
))
2137 pde
->rec_len
= ext4_rec_len_to_disk(
2138 ext4_rec_len_from_disk(pde
->rec_len
,
2140 ext4_rec_len_from_disk(de
->rec_len
,
2148 i
+= ext4_rec_len_from_disk(de
->rec_len
, blocksize
);
2150 de
= ext4_next_entry(de
, blocksize
);
2155 static int ext4_delete_entry(handle_t
*handle
,
2157 struct ext4_dir_entry_2
*de_del
,
2158 struct buffer_head
*bh
)
2160 int err
, csum_size
= 0;
2162 if (ext4_has_inline_data(dir
)) {
2163 int has_inline_data
= 1;
2164 err
= ext4_delete_inline_entry(handle
, dir
, de_del
, bh
,
2166 if (has_inline_data
)
2170 if (EXT4_HAS_RO_COMPAT_FEATURE(dir
->i_sb
,
2171 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
2172 csum_size
= sizeof(struct ext4_dir_entry_tail
);
2174 BUFFER_TRACE(bh
, "get_write_access");
2175 err
= ext4_journal_get_write_access(handle
, bh
);
2179 err
= ext4_generic_delete_entry(handle
, dir
, de_del
,
2181 dir
->i_sb
->s_blocksize
, csum_size
);
2185 BUFFER_TRACE(bh
, "call ext4_handle_dirty_metadata");
2186 err
= ext4_handle_dirty_dirent_node(handle
, dir
, bh
);
2193 ext4_std_error(dir
->i_sb
, err
);
2198 * DIR_NLINK feature is set if 1) nlinks > EXT4_LINK_MAX or 2) nlinks == 2,
2199 * since this indicates that nlinks count was previously 1.
2201 static void ext4_inc_count(handle_t
*handle
, struct inode
*inode
)
2204 if (is_dx(inode
) && inode
->i_nlink
> 1) {
2205 /* limit is 16-bit i_links_count */
2206 if (inode
->i_nlink
>= EXT4_LINK_MAX
|| inode
->i_nlink
== 2) {
2207 set_nlink(inode
, 1);
2208 EXT4_SET_RO_COMPAT_FEATURE(inode
->i_sb
,
2209 EXT4_FEATURE_RO_COMPAT_DIR_NLINK
);
2215 * If a directory had nlink == 1, then we should let it be 1. This indicates
2216 * directory has >EXT4_LINK_MAX subdirs.
2218 static void ext4_dec_count(handle_t
*handle
, struct inode
*inode
)
2220 if (!S_ISDIR(inode
->i_mode
) || inode
->i_nlink
> 2)
2225 static int ext4_add_nondir(handle_t
*handle
,
2226 struct dentry
*dentry
, struct inode
*inode
)
2228 int err
= ext4_add_entry(handle
, dentry
, inode
);
2230 ext4_mark_inode_dirty(handle
, inode
);
2231 unlock_new_inode(inode
);
2232 d_instantiate(dentry
, inode
);
2236 unlock_new_inode(inode
);
2242 * By the time this is called, we already have created
2243 * the directory cache entry for the new file, but it
2244 * is so far negative - it has no inode.
2246 * If the create succeeds, we fill in the inode information
2247 * with d_instantiate().
2249 static int ext4_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
2253 struct inode
*inode
;
2254 int err
, credits
, retries
= 0;
2256 dquot_initialize(dir
);
2258 credits
= (EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
2259 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 3 +
2260 EXT4_MAXQUOTAS_INIT_BLOCKS(dir
->i_sb
));
2262 inode
= ext4_new_inode_start_handle(dir
, mode
, &dentry
->d_name
, 0,
2263 NULL
, EXT4_HT_DIR
, credits
);
2264 handle
= ext4_journal_current_handle();
2265 err
= PTR_ERR(inode
);
2266 if (!IS_ERR(inode
)) {
2267 inode
->i_op
= &ext4_file_inode_operations
;
2268 inode
->i_fop
= &ext4_file_operations
;
2269 ext4_set_aops(inode
);
2270 err
= ext4_add_nondir(handle
, dentry
, inode
);
2271 if (!err
&& IS_DIRSYNC(dir
))
2272 ext4_handle_sync(handle
);
2275 ext4_journal_stop(handle
);
2276 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
2281 static int ext4_mknod(struct inode
*dir
, struct dentry
*dentry
,
2282 umode_t mode
, dev_t rdev
)
2285 struct inode
*inode
;
2286 int err
, credits
, retries
= 0;
2288 if (!new_valid_dev(rdev
))
2291 dquot_initialize(dir
);
2293 credits
= (EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
2294 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 3 +
2295 EXT4_MAXQUOTAS_INIT_BLOCKS(dir
->i_sb
));
2297 inode
= ext4_new_inode_start_handle(dir
, mode
, &dentry
->d_name
, 0,
2298 NULL
, EXT4_HT_DIR
, credits
);
2299 handle
= ext4_journal_current_handle();
2300 err
= PTR_ERR(inode
);
2301 if (!IS_ERR(inode
)) {
2302 init_special_inode(inode
, inode
->i_mode
, rdev
);
2303 inode
->i_op
= &ext4_special_inode_operations
;
2304 err
= ext4_add_nondir(handle
, dentry
, inode
);
2305 if (!err
&& IS_DIRSYNC(dir
))
2306 ext4_handle_sync(handle
);
2309 ext4_journal_stop(handle
);
2310 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
2315 struct ext4_dir_entry_2
*ext4_init_dot_dotdot(struct inode
*inode
,
2316 struct ext4_dir_entry_2
*de
,
2317 int blocksize
, int csum_size
,
2318 unsigned int parent_ino
, int dotdot_real_len
)
2320 de
->inode
= cpu_to_le32(inode
->i_ino
);
2322 de
->rec_len
= ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de
->name_len
),
2324 strcpy(de
->name
, ".");
2325 ext4_set_de_type(inode
->i_sb
, de
, S_IFDIR
);
2327 de
= ext4_next_entry(de
, blocksize
);
2328 de
->inode
= cpu_to_le32(parent_ino
);
2330 if (!dotdot_real_len
)
2331 de
->rec_len
= ext4_rec_len_to_disk(blocksize
-
2332 (csum_size
+ EXT4_DIR_REC_LEN(1)),
2335 de
->rec_len
= ext4_rec_len_to_disk(
2336 EXT4_DIR_REC_LEN(de
->name_len
), blocksize
);
2337 strcpy(de
->name
, "..");
2338 ext4_set_de_type(inode
->i_sb
, de
, S_IFDIR
);
2340 return ext4_next_entry(de
, blocksize
);
2343 static int ext4_init_new_dir(handle_t
*handle
, struct inode
*dir
,
2344 struct inode
*inode
)
2346 struct buffer_head
*dir_block
= NULL
;
2347 struct ext4_dir_entry_2
*de
;
2348 struct ext4_dir_entry_tail
*t
;
2349 ext4_lblk_t block
= 0;
2350 unsigned int blocksize
= dir
->i_sb
->s_blocksize
;
2354 if (EXT4_HAS_RO_COMPAT_FEATURE(dir
->i_sb
,
2355 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
2356 csum_size
= sizeof(struct ext4_dir_entry_tail
);
2358 if (ext4_test_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
)) {
2359 err
= ext4_try_create_inline_dir(handle
, dir
, inode
);
2360 if (err
< 0 && err
!= -ENOSPC
)
2367 if (!(dir_block
= ext4_append(handle
, inode
, &block
, &err
)))
2369 BUFFER_TRACE(dir_block
, "get_write_access");
2370 err
= ext4_journal_get_write_access(handle
, dir_block
);
2373 de
= (struct ext4_dir_entry_2
*)dir_block
->b_data
;
2374 ext4_init_dot_dotdot(inode
, de
, blocksize
, csum_size
, dir
->i_ino
, 0);
2375 set_nlink(inode
, 2);
2377 t
= EXT4_DIRENT_TAIL(dir_block
->b_data
, blocksize
);
2378 initialize_dirent_tail(t
, blocksize
);
2381 BUFFER_TRACE(dir_block
, "call ext4_handle_dirty_metadata");
2382 err
= ext4_handle_dirty_dirent_node(handle
, inode
, dir_block
);
2385 set_buffer_verified(dir_block
);
2391 static int ext4_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
2394 struct inode
*inode
;
2395 int err
, credits
, retries
= 0;
2397 if (EXT4_DIR_LINK_MAX(dir
))
2400 dquot_initialize(dir
);
2402 credits
= (EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
2403 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 3 +
2404 EXT4_MAXQUOTAS_INIT_BLOCKS(dir
->i_sb
));
2406 inode
= ext4_new_inode_start_handle(dir
, S_IFDIR
| mode
,
2408 0, NULL
, EXT4_HT_DIR
, credits
);
2409 handle
= ext4_journal_current_handle();
2410 err
= PTR_ERR(inode
);
2414 inode
->i_op
= &ext4_dir_inode_operations
;
2415 inode
->i_fop
= &ext4_dir_operations
;
2416 err
= ext4_init_new_dir(handle
, dir
, inode
);
2418 goto out_clear_inode
;
2419 err
= ext4_mark_inode_dirty(handle
, inode
);
2421 err
= ext4_add_entry(handle
, dentry
, inode
);
2425 unlock_new_inode(inode
);
2426 ext4_mark_inode_dirty(handle
, inode
);
2430 ext4_inc_count(handle
, dir
);
2431 ext4_update_dx_flag(dir
);
2432 err
= ext4_mark_inode_dirty(handle
, dir
);
2434 goto out_clear_inode
;
2435 unlock_new_inode(inode
);
2436 d_instantiate(dentry
, inode
);
2437 if (IS_DIRSYNC(dir
))
2438 ext4_handle_sync(handle
);
2442 ext4_journal_stop(handle
);
2443 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
2449 * routine to check that the specified directory is empty (for rmdir)
2451 static int empty_dir(struct inode
*inode
)
2453 unsigned int offset
;
2454 struct buffer_head
*bh
;
2455 struct ext4_dir_entry_2
*de
, *de1
;
2456 struct super_block
*sb
;
2459 if (ext4_has_inline_data(inode
)) {
2460 int has_inline_data
= 1;
2462 err
= empty_inline_dir(inode
, &has_inline_data
);
2463 if (has_inline_data
)
2468 if (inode
->i_size
< EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2)) {
2469 EXT4_ERROR_INODE(inode
, "invalid size");
2472 bh
= ext4_read_dirblock(inode
, 0, EITHER
);
2476 de
= (struct ext4_dir_entry_2
*) bh
->b_data
;
2477 de1
= ext4_next_entry(de
, sb
->s_blocksize
);
2478 if (le32_to_cpu(de
->inode
) != inode
->i_ino
||
2479 !le32_to_cpu(de1
->inode
) ||
2480 strcmp(".", de
->name
) ||
2481 strcmp("..", de1
->name
)) {
2482 ext4_warning(inode
->i_sb
,
2483 "bad directory (dir #%lu) - no `.' or `..'",
2488 offset
= ext4_rec_len_from_disk(de
->rec_len
, sb
->s_blocksize
) +
2489 ext4_rec_len_from_disk(de1
->rec_len
, sb
->s_blocksize
);
2490 de
= ext4_next_entry(de1
, sb
->s_blocksize
);
2491 while (offset
< inode
->i_size
) {
2493 (void *) de
>= (void *) (bh
->b_data
+sb
->s_blocksize
)) {
2494 unsigned int lblock
;
2497 lblock
= offset
>> EXT4_BLOCK_SIZE_BITS(sb
);
2498 bh
= ext4_read_dirblock(inode
, lblock
, EITHER
);
2501 de
= (struct ext4_dir_entry_2
*) bh
->b_data
;
2503 if (ext4_check_dir_entry(inode
, NULL
, de
, bh
,
2504 bh
->b_data
, bh
->b_size
, offset
)) {
2505 de
= (struct ext4_dir_entry_2
*)(bh
->b_data
+
2507 offset
= (offset
| (sb
->s_blocksize
- 1)) + 1;
2510 if (le32_to_cpu(de
->inode
)) {
2514 offset
+= ext4_rec_len_from_disk(de
->rec_len
, sb
->s_blocksize
);
2515 de
= ext4_next_entry(de
, sb
->s_blocksize
);
2521 /* ext4_orphan_add() links an unlinked or truncated inode into a list of
2522 * such inodes, starting at the superblock, in case we crash before the
2523 * file is closed/deleted, or in case the inode truncate spans multiple
2524 * transactions and the last transaction is not recovered after a crash.
2526 * At filesystem recovery time, we walk this list deleting unlinked
2527 * inodes and truncating linked inodes in ext4_orphan_cleanup().
2529 int ext4_orphan_add(handle_t
*handle
, struct inode
*inode
)
2531 struct super_block
*sb
= inode
->i_sb
;
2532 struct ext4_iloc iloc
;
2535 if (!EXT4_SB(sb
)->s_journal
)
2538 mutex_lock(&EXT4_SB(sb
)->s_orphan_lock
);
2539 if (!list_empty(&EXT4_I(inode
)->i_orphan
))
2543 * Orphan handling is only valid for files with data blocks
2544 * being truncated, or files being unlinked. Note that we either
2545 * hold i_mutex, or the inode can not be referenced from outside,
2546 * so i_nlink should not be bumped due to race
2548 J_ASSERT((S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
2549 S_ISLNK(inode
->i_mode
)) || inode
->i_nlink
== 0);
2551 BUFFER_TRACE(EXT4_SB(sb
)->s_sbh
, "get_write_access");
2552 err
= ext4_journal_get_write_access(handle
, EXT4_SB(sb
)->s_sbh
);
2556 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
2560 * Due to previous errors inode may be already a part of on-disk
2561 * orphan list. If so skip on-disk list modification.
2563 if (NEXT_ORPHAN(inode
) && NEXT_ORPHAN(inode
) <=
2564 (le32_to_cpu(EXT4_SB(sb
)->s_es
->s_inodes_count
)))
2567 /* Insert this inode at the head of the on-disk orphan list... */
2568 NEXT_ORPHAN(inode
) = le32_to_cpu(EXT4_SB(sb
)->s_es
->s_last_orphan
);
2569 EXT4_SB(sb
)->s_es
->s_last_orphan
= cpu_to_le32(inode
->i_ino
);
2570 err
= ext4_handle_dirty_super(handle
, sb
);
2571 rc
= ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
2575 /* Only add to the head of the in-memory list if all the
2576 * previous operations succeeded. If the orphan_add is going to
2577 * fail (possibly taking the journal offline), we can't risk
2578 * leaving the inode on the orphan list: stray orphan-list
2579 * entries can cause panics at unmount time.
2581 * This is safe: on error we're going to ignore the orphan list
2582 * anyway on the next recovery. */
2585 list_add(&EXT4_I(inode
)->i_orphan
, &EXT4_SB(sb
)->s_orphan
);
2587 jbd_debug(4, "superblock will point to %lu\n", inode
->i_ino
);
2588 jbd_debug(4, "orphan inode %lu will point to %d\n",
2589 inode
->i_ino
, NEXT_ORPHAN(inode
));
2591 mutex_unlock(&EXT4_SB(sb
)->s_orphan_lock
);
2592 ext4_std_error(inode
->i_sb
, err
);
2597 * ext4_orphan_del() removes an unlinked or truncated inode from the list
2598 * of such inodes stored on disk, because it is finally being cleaned up.
2600 int ext4_orphan_del(handle_t
*handle
, struct inode
*inode
)
2602 struct list_head
*prev
;
2603 struct ext4_inode_info
*ei
= EXT4_I(inode
);
2604 struct ext4_sb_info
*sbi
;
2606 struct ext4_iloc iloc
;
2609 if ((!EXT4_SB(inode
->i_sb
)->s_journal
) &&
2610 !(EXT4_SB(inode
->i_sb
)->s_mount_state
& EXT4_ORPHAN_FS
))
2613 mutex_lock(&EXT4_SB(inode
->i_sb
)->s_orphan_lock
);
2614 if (list_empty(&ei
->i_orphan
))
2617 ino_next
= NEXT_ORPHAN(inode
);
2618 prev
= ei
->i_orphan
.prev
;
2619 sbi
= EXT4_SB(inode
->i_sb
);
2621 jbd_debug(4, "remove inode %lu from orphan list\n", inode
->i_ino
);
2623 list_del_init(&ei
->i_orphan
);
2625 /* If we're on an error path, we may not have a valid
2626 * transaction handle with which to update the orphan list on
2627 * disk, but we still need to remove the inode from the linked
2628 * list in memory. */
2632 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
2636 if (prev
== &sbi
->s_orphan
) {
2637 jbd_debug(4, "superblock will point to %u\n", ino_next
);
2638 BUFFER_TRACE(sbi
->s_sbh
, "get_write_access");
2639 err
= ext4_journal_get_write_access(handle
, sbi
->s_sbh
);
2642 sbi
->s_es
->s_last_orphan
= cpu_to_le32(ino_next
);
2643 err
= ext4_handle_dirty_super(handle
, inode
->i_sb
);
2645 struct ext4_iloc iloc2
;
2646 struct inode
*i_prev
=
2647 &list_entry(prev
, struct ext4_inode_info
, i_orphan
)->vfs_inode
;
2649 jbd_debug(4, "orphan inode %lu will point to %u\n",
2650 i_prev
->i_ino
, ino_next
);
2651 err
= ext4_reserve_inode_write(handle
, i_prev
, &iloc2
);
2654 NEXT_ORPHAN(i_prev
) = ino_next
;
2655 err
= ext4_mark_iloc_dirty(handle
, i_prev
, &iloc2
);
2659 NEXT_ORPHAN(inode
) = 0;
2660 err
= ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
2663 ext4_std_error(inode
->i_sb
, err
);
2665 mutex_unlock(&EXT4_SB(inode
->i_sb
)->s_orphan_lock
);
2673 static int ext4_rmdir(struct inode
*dir
, struct dentry
*dentry
)
2676 struct inode
*inode
;
2677 struct buffer_head
*bh
;
2678 struct ext4_dir_entry_2
*de
;
2679 handle_t
*handle
= NULL
;
2681 /* Initialize quotas before so that eventual writes go in
2682 * separate transaction */
2683 dquot_initialize(dir
);
2684 dquot_initialize(dentry
->d_inode
);
2687 bh
= ext4_find_entry(dir
, &dentry
->d_name
, &de
, NULL
);
2691 inode
= dentry
->d_inode
;
2694 if (le32_to_cpu(de
->inode
) != inode
->i_ino
)
2697 retval
= -ENOTEMPTY
;
2698 if (!empty_dir(inode
))
2701 handle
= ext4_journal_start(dir
, EXT4_HT_DIR
,
2702 EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
));
2703 if (IS_ERR(handle
)) {
2704 retval
= PTR_ERR(handle
);
2709 if (IS_DIRSYNC(dir
))
2710 ext4_handle_sync(handle
);
2712 retval
= ext4_delete_entry(handle
, dir
, de
, bh
);
2715 if (!EXT4_DIR_LINK_EMPTY(inode
))
2716 ext4_warning(inode
->i_sb
,
2717 "empty directory has too many links (%d)",
2721 /* There's no need to set i_disksize: the fact that i_nlink is
2722 * zero will ensure that the right thing happens during any
2725 ext4_orphan_add(handle
, inode
);
2726 inode
->i_ctime
= dir
->i_ctime
= dir
->i_mtime
= ext4_current_time(inode
);
2727 ext4_mark_inode_dirty(handle
, inode
);
2728 ext4_dec_count(handle
, dir
);
2729 ext4_update_dx_flag(dir
);
2730 ext4_mark_inode_dirty(handle
, dir
);
2735 ext4_journal_stop(handle
);
2739 static int ext4_unlink(struct inode
*dir
, struct dentry
*dentry
)
2742 struct inode
*inode
;
2743 struct buffer_head
*bh
;
2744 struct ext4_dir_entry_2
*de
;
2745 handle_t
*handle
= NULL
;
2747 trace_ext4_unlink_enter(dir
, dentry
);
2748 /* Initialize quotas before so that eventual writes go
2749 * in separate transaction */
2750 dquot_initialize(dir
);
2751 dquot_initialize(dentry
->d_inode
);
2754 bh
= ext4_find_entry(dir
, &dentry
->d_name
, &de
, NULL
);
2758 inode
= dentry
->d_inode
;
2761 if (le32_to_cpu(de
->inode
) != inode
->i_ino
)
2764 handle
= ext4_journal_start(dir
, EXT4_HT_DIR
,
2765 EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
));
2766 if (IS_ERR(handle
)) {
2767 retval
= PTR_ERR(handle
);
2772 if (IS_DIRSYNC(dir
))
2773 ext4_handle_sync(handle
);
2775 if (!inode
->i_nlink
) {
2776 ext4_warning(inode
->i_sb
,
2777 "Deleting nonexistent file (%lu), %d",
2778 inode
->i_ino
, inode
->i_nlink
);
2779 set_nlink(inode
, 1);
2781 retval
= ext4_delete_entry(handle
, dir
, de
, bh
);
2784 dir
->i_ctime
= dir
->i_mtime
= ext4_current_time(dir
);
2785 ext4_update_dx_flag(dir
);
2786 ext4_mark_inode_dirty(handle
, dir
);
2788 if (!inode
->i_nlink
)
2789 ext4_orphan_add(handle
, inode
);
2790 inode
->i_ctime
= ext4_current_time(inode
);
2791 ext4_mark_inode_dirty(handle
, inode
);
2797 ext4_journal_stop(handle
);
2798 trace_ext4_unlink_exit(dentry
, retval
);
2802 static int ext4_symlink(struct inode
*dir
,
2803 struct dentry
*dentry
, const char *symname
)
2806 struct inode
*inode
;
2807 int l
, err
, retries
= 0;
2810 l
= strlen(symname
)+1;
2811 if (l
> dir
->i_sb
->s_blocksize
)
2812 return -ENAMETOOLONG
;
2814 dquot_initialize(dir
);
2816 if (l
> EXT4_N_BLOCKS
* 4) {
2818 * For non-fast symlinks, we just allocate inode and put it on
2819 * orphan list in the first transaction => we need bitmap,
2820 * group descriptor, sb, inode block, quota blocks, and
2821 * possibly selinux xattr blocks.
2823 credits
= 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir
->i_sb
) +
2824 EXT4_XATTR_TRANS_BLOCKS
;
2827 * Fast symlink. We have to add entry to directory
2828 * (EXT4_DATA_TRANS_BLOCKS + EXT4_INDEX_EXTRA_TRANS_BLOCKS),
2829 * allocate new inode (bitmap, group descriptor, inode block,
2830 * quota blocks, sb is already counted in previous macros).
2832 credits
= EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
2833 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 3 +
2834 EXT4_MAXQUOTAS_INIT_BLOCKS(dir
->i_sb
);
2837 inode
= ext4_new_inode_start_handle(dir
, S_IFLNK
|S_IRWXUGO
,
2838 &dentry
->d_name
, 0, NULL
,
2839 EXT4_HT_DIR
, credits
);
2840 handle
= ext4_journal_current_handle();
2841 err
= PTR_ERR(inode
);
2845 if (l
> EXT4_N_BLOCKS
* 4) {
2846 inode
->i_op
= &ext4_symlink_inode_operations
;
2847 ext4_set_aops(inode
);
2849 * We cannot call page_symlink() with transaction started
2850 * because it calls into ext4_write_begin() which can wait
2851 * for transaction commit if we are running out of space
2852 * and thus we deadlock. So we have to stop transaction now
2853 * and restart it when symlink contents is written.
2855 * To keep fs consistent in case of crash, we have to put inode
2856 * to orphan list in the mean time.
2859 err
= ext4_orphan_add(handle
, inode
);
2860 ext4_journal_stop(handle
);
2862 goto err_drop_inode
;
2863 err
= __page_symlink(inode
, symname
, l
, 1);
2865 goto err_drop_inode
;
2867 * Now inode is being linked into dir (EXT4_DATA_TRANS_BLOCKS
2868 * + EXT4_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
2870 handle
= ext4_journal_start(dir
, EXT4_HT_DIR
,
2871 EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
2872 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 1);
2873 if (IS_ERR(handle
)) {
2874 err
= PTR_ERR(handle
);
2875 goto err_drop_inode
;
2877 set_nlink(inode
, 1);
2878 err
= ext4_orphan_del(handle
, inode
);
2880 ext4_journal_stop(handle
);
2882 goto err_drop_inode
;
2885 /* clear the extent format for fast symlink */
2886 ext4_clear_inode_flag(inode
, EXT4_INODE_EXTENTS
);
2887 inode
->i_op
= &ext4_fast_symlink_inode_operations
;
2888 memcpy((char *)&EXT4_I(inode
)->i_data
, symname
, l
);
2889 inode
->i_size
= l
-1;
2891 EXT4_I(inode
)->i_disksize
= inode
->i_size
;
2892 err
= ext4_add_nondir(handle
, dentry
, inode
);
2893 if (!err
&& IS_DIRSYNC(dir
))
2894 ext4_handle_sync(handle
);
2898 ext4_journal_stop(handle
);
2899 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
2903 unlock_new_inode(inode
);
2908 static int ext4_link(struct dentry
*old_dentry
,
2909 struct inode
*dir
, struct dentry
*dentry
)
2912 struct inode
*inode
= old_dentry
->d_inode
;
2913 int err
, retries
= 0;
2915 if (inode
->i_nlink
>= EXT4_LINK_MAX
)
2918 dquot_initialize(dir
);
2921 handle
= ext4_journal_start(dir
, EXT4_HT_DIR
,
2922 (EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
2923 EXT4_INDEX_EXTRA_TRANS_BLOCKS
));
2925 return PTR_ERR(handle
);
2927 if (IS_DIRSYNC(dir
))
2928 ext4_handle_sync(handle
);
2930 inode
->i_ctime
= ext4_current_time(inode
);
2931 ext4_inc_count(handle
, inode
);
2934 err
= ext4_add_entry(handle
, dentry
, inode
);
2936 ext4_mark_inode_dirty(handle
, inode
);
2937 d_instantiate(dentry
, inode
);
2942 ext4_journal_stop(handle
);
2943 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
2950 * Try to find buffer head where contains the parent block.
2951 * It should be the inode block if it is inlined or the 1st block
2952 * if it is a normal dir.
2954 static struct buffer_head
*ext4_get_first_dir_block(handle_t
*handle
,
2955 struct inode
*inode
,
2957 struct ext4_dir_entry_2
**parent_de
,
2960 struct buffer_head
*bh
;
2962 if (!ext4_has_inline_data(inode
)) {
2963 bh
= ext4_read_dirblock(inode
, 0, EITHER
);
2965 *retval
= PTR_ERR(bh
);
2968 *parent_de
= ext4_next_entry(
2969 (struct ext4_dir_entry_2
*)bh
->b_data
,
2970 inode
->i_sb
->s_blocksize
);
2975 return ext4_get_first_inline_block(inode
, parent_de
, retval
);
2979 * Anybody can rename anything with this: the permission checks are left to the
2980 * higher-level routines.
2982 static int ext4_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
2983 struct inode
*new_dir
, struct dentry
*new_dentry
)
2986 struct inode
*old_inode
, *new_inode
;
2987 struct buffer_head
*old_bh
, *new_bh
, *dir_bh
;
2988 struct ext4_dir_entry_2
*old_de
, *new_de
;
2989 int retval
, force_da_alloc
= 0;
2990 int inlined
= 0, new_inlined
= 0;
2991 struct ext4_dir_entry_2
*parent_de
;
2993 dquot_initialize(old_dir
);
2994 dquot_initialize(new_dir
);
2996 old_bh
= new_bh
= dir_bh
= NULL
;
2998 /* Initialize quotas before so that eventual writes go
2999 * in separate transaction */
3000 if (new_dentry
->d_inode
)
3001 dquot_initialize(new_dentry
->d_inode
);
3002 handle
= ext4_journal_start(old_dir
, EXT4_HT_DIR
,
3003 (2 * EXT4_DATA_TRANS_BLOCKS(old_dir
->i_sb
) +
3004 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 2));
3006 return PTR_ERR(handle
);
3008 if (IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
))
3009 ext4_handle_sync(handle
);
3011 old_bh
= ext4_find_entry(old_dir
, &old_dentry
->d_name
, &old_de
, NULL
);
3013 * Check for inode number is _not_ due to possible IO errors.
3014 * We might rmdir the source, keep it as pwd of some process
3015 * and merrily kill the link to whatever was created under the
3016 * same name. Goodbye sticky bit ;-<
3018 old_inode
= old_dentry
->d_inode
;
3020 if (!old_bh
|| le32_to_cpu(old_de
->inode
) != old_inode
->i_ino
)
3023 new_inode
= new_dentry
->d_inode
;
3024 new_bh
= ext4_find_entry(new_dir
, &new_dentry
->d_name
,
3025 &new_de
, &new_inlined
);
3032 if (S_ISDIR(old_inode
->i_mode
)) {
3034 retval
= -ENOTEMPTY
;
3035 if (!empty_dir(new_inode
))
3039 dir_bh
= ext4_get_first_dir_block(handle
, old_inode
,
3040 &retval
, &parent_de
,
3044 if (le32_to_cpu(parent_de
->inode
) != old_dir
->i_ino
)
3047 if (!new_inode
&& new_dir
!= old_dir
&&
3048 EXT4_DIR_LINK_MAX(new_dir
))
3050 BUFFER_TRACE(dir_bh
, "get_write_access");
3051 retval
= ext4_journal_get_write_access(handle
, dir_bh
);
3056 retval
= ext4_add_entry(handle
, new_dentry
, old_inode
);
3060 BUFFER_TRACE(new_bh
, "get write access");
3061 retval
= ext4_journal_get_write_access(handle
, new_bh
);
3064 new_de
->inode
= cpu_to_le32(old_inode
->i_ino
);
3065 if (EXT4_HAS_INCOMPAT_FEATURE(new_dir
->i_sb
,
3066 EXT4_FEATURE_INCOMPAT_FILETYPE
))
3067 new_de
->file_type
= old_de
->file_type
;
3068 new_dir
->i_version
++;
3069 new_dir
->i_ctime
= new_dir
->i_mtime
=
3070 ext4_current_time(new_dir
);
3071 ext4_mark_inode_dirty(handle
, new_dir
);
3072 BUFFER_TRACE(new_bh
, "call ext4_handle_dirty_metadata");
3074 retval
= ext4_handle_dirty_dirent_node(handle
,
3076 if (unlikely(retval
)) {
3077 ext4_std_error(new_dir
->i_sb
, retval
);
3086 * Like most other Unix systems, set the ctime for inodes on a
3089 old_inode
->i_ctime
= ext4_current_time(old_inode
);
3090 ext4_mark_inode_dirty(handle
, old_inode
);
3095 if (le32_to_cpu(old_de
->inode
) != old_inode
->i_ino
||
3096 old_de
->name_len
!= old_dentry
->d_name
.len
||
3097 strncmp(old_de
->name
, old_dentry
->d_name
.name
, old_de
->name_len
) ||
3098 (retval
= ext4_delete_entry(handle
, old_dir
,
3099 old_de
, old_bh
)) == -ENOENT
) {
3100 /* old_de could have moved from under us during htree split, so
3101 * make sure that we are deleting the right entry. We might
3102 * also be pointing to a stale entry in the unused part of
3103 * old_bh so just checking inum and the name isn't enough. */
3104 struct buffer_head
*old_bh2
;
3105 struct ext4_dir_entry_2
*old_de2
;
3107 old_bh2
= ext4_find_entry(old_dir
, &old_dentry
->d_name
,
3110 retval
= ext4_delete_entry(handle
, old_dir
,
3116 ext4_warning(old_dir
->i_sb
,
3117 "Deleting old file (%lu), %d, error=%d",
3118 old_dir
->i_ino
, old_dir
->i_nlink
, retval
);
3122 ext4_dec_count(handle
, new_inode
);
3123 new_inode
->i_ctime
= ext4_current_time(new_inode
);
3125 old_dir
->i_ctime
= old_dir
->i_mtime
= ext4_current_time(old_dir
);
3126 ext4_update_dx_flag(old_dir
);
3128 parent_de
->inode
= cpu_to_le32(new_dir
->i_ino
);
3129 BUFFER_TRACE(dir_bh
, "call ext4_handle_dirty_metadata");
3131 if (is_dx(old_inode
)) {
3132 retval
= ext4_handle_dirty_dx_node(handle
,
3136 retval
= ext4_handle_dirty_dirent_node(handle
,
3140 retval
= ext4_mark_inode_dirty(handle
, old_inode
);
3143 ext4_std_error(old_dir
->i_sb
, retval
);
3146 ext4_dec_count(handle
, old_dir
);
3148 /* checked empty_dir above, can't have another parent,
3149 * ext4_dec_count() won't work for many-linked dirs */
3150 clear_nlink(new_inode
);
3152 ext4_inc_count(handle
, new_dir
);
3153 ext4_update_dx_flag(new_dir
);
3154 ext4_mark_inode_dirty(handle
, new_dir
);
3157 ext4_mark_inode_dirty(handle
, old_dir
);
3159 ext4_mark_inode_dirty(handle
, new_inode
);
3160 if (!new_inode
->i_nlink
)
3161 ext4_orphan_add(handle
, new_inode
);
3162 if (!test_opt(new_dir
->i_sb
, NO_AUTO_DA_ALLOC
))
3171 ext4_journal_stop(handle
);
3172 if (retval
== 0 && force_da_alloc
)
3173 ext4_alloc_da_blocks(old_inode
);
3178 * directories can handle most operations...
3180 const struct inode_operations ext4_dir_inode_operations
= {
3181 .create
= ext4_create
,
3182 .lookup
= ext4_lookup
,
3184 .unlink
= ext4_unlink
,
3185 .symlink
= ext4_symlink
,
3186 .mkdir
= ext4_mkdir
,
3187 .rmdir
= ext4_rmdir
,
3188 .mknod
= ext4_mknod
,
3189 .rename
= ext4_rename
,
3190 .setattr
= ext4_setattr
,
3191 .setxattr
= generic_setxattr
,
3192 .getxattr
= generic_getxattr
,
3193 .listxattr
= ext4_listxattr
,
3194 .removexattr
= generic_removexattr
,
3195 .get_acl
= ext4_get_acl
,
3196 .fiemap
= ext4_fiemap
,
3199 const struct inode_operations ext4_special_inode_operations
= {
3200 .setattr
= ext4_setattr
,
3201 .setxattr
= generic_setxattr
,
3202 .getxattr
= generic_getxattr
,
3203 .listxattr
= ext4_listxattr
,
3204 .removexattr
= generic_removexattr
,
3205 .get_acl
= ext4_get_acl
,