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/time.h>
30 #include <linux/fcntl.h>
31 #include <linux/stat.h>
32 #include <linux/string.h>
33 #include <linux/quotaops.h>
34 #include <linux/buffer_head.h>
35 #include <linux/bio.h>
37 #include "ext4_jbd2.h"
42 #include <trace/events/ext4.h>
44 * define how far ahead to read directories while searching them.
46 #define NAMEI_RA_CHUNKS 2
47 #define NAMEI_RA_BLOCKS 4
48 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
50 static struct buffer_head
*ext4_append(handle_t
*handle
,
54 struct buffer_head
*bh
;
57 if (unlikely(EXT4_SB(inode
->i_sb
)->s_max_dir_size_kb
&&
58 ((inode
->i_size
>> 10) >=
59 EXT4_SB(inode
->i_sb
)->s_max_dir_size_kb
)))
60 return ERR_PTR(-ENOSPC
);
62 *block
= inode
->i_size
>> inode
->i_sb
->s_blocksize_bits
;
64 bh
= ext4_bread(handle
, inode
, *block
, 1);
67 inode
->i_size
+= inode
->i_sb
->s_blocksize
;
68 EXT4_I(inode
)->i_disksize
= inode
->i_size
;
69 BUFFER_TRACE(bh
, "get_write_access");
70 err
= ext4_journal_get_write_access(handle
, bh
);
73 ext4_std_error(inode
->i_sb
, err
);
79 static int ext4_dx_csum_verify(struct inode
*inode
,
80 struct ext4_dir_entry
*dirent
);
86 #define ext4_read_dirblock(inode, block, type) \
87 __ext4_read_dirblock((inode), (block), (type), __LINE__)
89 static struct buffer_head
*__ext4_read_dirblock(struct inode
*inode
,
94 struct buffer_head
*bh
;
95 struct ext4_dir_entry
*dirent
;
98 bh
= ext4_bread(NULL
, inode
, block
, 0);
100 __ext4_warning(inode
->i_sb
, __func__
, line
,
101 "error %ld reading directory block "
102 "(ino %lu, block %lu)", PTR_ERR(bh
), inode
->i_ino
,
103 (unsigned long) block
);
108 ext4_error_inode(inode
, __func__
, line
, block
, "Directory hole found");
109 return ERR_PTR(-EIO
);
111 dirent
= (struct ext4_dir_entry
*) bh
->b_data
;
112 /* Determine whether or not we have an index block */
116 else if (ext4_rec_len_from_disk(dirent
->rec_len
,
117 inode
->i_sb
->s_blocksize
) ==
118 inode
->i_sb
->s_blocksize
)
121 if (!is_dx_block
&& type
== INDEX
) {
122 ext4_error_inode(inode
, __func__
, line
, block
,
123 "directory leaf block found instead of index block");
124 return ERR_PTR(-EIO
);
126 if (!ext4_has_metadata_csum(inode
->i_sb
) ||
131 * An empty leaf block can get mistaken for a index block; for
132 * this reason, we can only check the index checksum when the
133 * caller is sure it should be an index block.
135 if (is_dx_block
&& type
== INDEX
) {
136 if (ext4_dx_csum_verify(inode
, dirent
))
137 set_buffer_verified(bh
);
139 ext4_error_inode(inode
, __func__
, line
, block
,
140 "Directory index failed checksum");
142 return ERR_PTR(-EIO
);
146 if (ext4_dirent_csum_verify(inode
, dirent
))
147 set_buffer_verified(bh
);
149 ext4_error_inode(inode
, __func__
, line
, block
,
150 "Directory block failed checksum");
152 return ERR_PTR(-EIO
);
159 #define assert(test) J_ASSERT(test)
163 #define dxtrace(command) command
165 #define dxtrace(command)
189 * dx_root_info is laid out so that if it should somehow get overlaid by a
190 * dirent the two low bits of the hash version will be zero. Therefore, the
191 * hash version mod 4 should never be 0. Sincerely, the paranoia department.
196 struct fake_dirent dot
;
198 struct fake_dirent dotdot
;
202 __le32 reserved_zero
;
204 u8 info_length
; /* 8 */
209 struct dx_entry entries
[0];
214 struct fake_dirent fake
;
215 struct dx_entry entries
[0];
221 struct buffer_head
*bh
;
222 struct dx_entry
*entries
;
234 * This goes at the end of each htree block.
238 __le32 dt_checksum
; /* crc32c(uuid+inum+dirblock) */
241 static inline ext4_lblk_t
dx_get_block(struct dx_entry
*entry
);
242 static void dx_set_block(struct dx_entry
*entry
, ext4_lblk_t value
);
243 static inline unsigned dx_get_hash(struct dx_entry
*entry
);
244 static void dx_set_hash(struct dx_entry
*entry
, unsigned value
);
245 static unsigned dx_get_count(struct dx_entry
*entries
);
246 static unsigned dx_get_limit(struct dx_entry
*entries
);
247 static void dx_set_count(struct dx_entry
*entries
, unsigned value
);
248 static void dx_set_limit(struct dx_entry
*entries
, unsigned value
);
249 static unsigned dx_root_limit(struct inode
*dir
, unsigned infosize
);
250 static unsigned dx_node_limit(struct inode
*dir
);
251 static struct dx_frame
*dx_probe(const struct qstr
*d_name
,
253 struct dx_hash_info
*hinfo
,
254 struct dx_frame
*frame
);
255 static void dx_release(struct dx_frame
*frames
);
256 static int dx_make_map(struct inode
*dir
, struct ext4_dir_entry_2
*de
,
257 unsigned blocksize
, struct dx_hash_info
*hinfo
,
258 struct dx_map_entry map
[]);
259 static void dx_sort_map(struct dx_map_entry
*map
, unsigned count
);
260 static struct ext4_dir_entry_2
*dx_move_dirents(char *from
, char *to
,
261 struct dx_map_entry
*offsets
, int count
, unsigned blocksize
);
262 static struct ext4_dir_entry_2
* dx_pack_dirents(char *base
, unsigned blocksize
);
263 static void dx_insert_block(struct dx_frame
*frame
,
264 u32 hash
, ext4_lblk_t block
);
265 static int ext4_htree_next_block(struct inode
*dir
, __u32 hash
,
266 struct dx_frame
*frame
,
267 struct dx_frame
*frames
,
269 static struct buffer_head
* ext4_dx_find_entry(struct inode
*dir
,
270 const struct qstr
*d_name
,
271 struct ext4_dir_entry_2
**res_dir
);
272 static int ext4_dx_add_entry(handle_t
*handle
, struct dentry
*dentry
,
273 struct inode
*inode
);
275 /* checksumming functions */
276 void initialize_dirent_tail(struct ext4_dir_entry_tail
*t
,
277 unsigned int blocksize
)
279 memset(t
, 0, sizeof(struct ext4_dir_entry_tail
));
280 t
->det_rec_len
= ext4_rec_len_to_disk(
281 sizeof(struct ext4_dir_entry_tail
), blocksize
);
282 t
->det_reserved_ft
= EXT4_FT_DIR_CSUM
;
285 /* Walk through a dirent block to find a checksum "dirent" at the tail */
286 static struct ext4_dir_entry_tail
*get_dirent_tail(struct inode
*inode
,
287 struct ext4_dir_entry
*de
)
289 struct ext4_dir_entry_tail
*t
;
292 struct ext4_dir_entry
*d
, *top
;
295 top
= (struct ext4_dir_entry
*)(((void *)de
) +
296 (EXT4_BLOCK_SIZE(inode
->i_sb
) -
297 sizeof(struct ext4_dir_entry_tail
)));
298 while (d
< top
&& d
->rec_len
)
299 d
= (struct ext4_dir_entry
*)(((void *)d
) +
300 le16_to_cpu(d
->rec_len
));
305 t
= (struct ext4_dir_entry_tail
*)d
;
307 t
= EXT4_DIRENT_TAIL(de
, EXT4_BLOCK_SIZE(inode
->i_sb
));
310 if (t
->det_reserved_zero1
||
311 le16_to_cpu(t
->det_rec_len
) != sizeof(struct ext4_dir_entry_tail
) ||
312 t
->det_reserved_zero2
||
313 t
->det_reserved_ft
!= EXT4_FT_DIR_CSUM
)
319 static __le32
ext4_dirent_csum(struct inode
*inode
,
320 struct ext4_dir_entry
*dirent
, int size
)
322 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
323 struct ext4_inode_info
*ei
= EXT4_I(inode
);
326 csum
= ext4_chksum(sbi
, ei
->i_csum_seed
, (__u8
*)dirent
, size
);
327 return cpu_to_le32(csum
);
330 static void warn_no_space_for_csum(struct inode
*inode
)
332 ext4_warning(inode
->i_sb
, "no space in directory inode %lu leaf for "
333 "checksum. Please run e2fsck -D.", inode
->i_ino
);
336 int ext4_dirent_csum_verify(struct inode
*inode
, struct ext4_dir_entry
*dirent
)
338 struct ext4_dir_entry_tail
*t
;
340 if (!ext4_has_metadata_csum(inode
->i_sb
))
343 t
= get_dirent_tail(inode
, dirent
);
345 warn_no_space_for_csum(inode
);
349 if (t
->det_checksum
!= ext4_dirent_csum(inode
, dirent
,
350 (void *)t
- (void *)dirent
))
356 static void ext4_dirent_csum_set(struct inode
*inode
,
357 struct ext4_dir_entry
*dirent
)
359 struct ext4_dir_entry_tail
*t
;
361 if (!ext4_has_metadata_csum(inode
->i_sb
))
364 t
= get_dirent_tail(inode
, dirent
);
366 warn_no_space_for_csum(inode
);
370 t
->det_checksum
= ext4_dirent_csum(inode
, dirent
,
371 (void *)t
- (void *)dirent
);
374 int ext4_handle_dirty_dirent_node(handle_t
*handle
,
376 struct buffer_head
*bh
)
378 ext4_dirent_csum_set(inode
, (struct ext4_dir_entry
*)bh
->b_data
);
379 return ext4_handle_dirty_metadata(handle
, inode
, bh
);
382 static struct dx_countlimit
*get_dx_countlimit(struct inode
*inode
,
383 struct ext4_dir_entry
*dirent
,
386 struct ext4_dir_entry
*dp
;
387 struct dx_root_info
*root
;
390 if (le16_to_cpu(dirent
->rec_len
) == EXT4_BLOCK_SIZE(inode
->i_sb
))
392 else if (le16_to_cpu(dirent
->rec_len
) == 12) {
393 dp
= (struct ext4_dir_entry
*)(((void *)dirent
) + 12);
394 if (le16_to_cpu(dp
->rec_len
) !=
395 EXT4_BLOCK_SIZE(inode
->i_sb
) - 12)
397 root
= (struct dx_root_info
*)(((void *)dp
+ 12));
398 if (root
->reserved_zero
||
399 root
->info_length
!= sizeof(struct dx_root_info
))
406 *offset
= count_offset
;
407 return (struct dx_countlimit
*)(((void *)dirent
) + count_offset
);
410 static __le32
ext4_dx_csum(struct inode
*inode
, struct ext4_dir_entry
*dirent
,
411 int count_offset
, int count
, struct dx_tail
*t
)
413 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
414 struct ext4_inode_info
*ei
= EXT4_I(inode
);
419 size
= count_offset
+ (count
* sizeof(struct dx_entry
));
420 save_csum
= t
->dt_checksum
;
422 csum
= ext4_chksum(sbi
, ei
->i_csum_seed
, (__u8
*)dirent
, size
);
423 csum
= ext4_chksum(sbi
, csum
, (__u8
*)t
, sizeof(struct dx_tail
));
424 t
->dt_checksum
= save_csum
;
426 return cpu_to_le32(csum
);
429 static int ext4_dx_csum_verify(struct inode
*inode
,
430 struct ext4_dir_entry
*dirent
)
432 struct dx_countlimit
*c
;
434 int count_offset
, limit
, count
;
436 if (!ext4_has_metadata_csum(inode
->i_sb
))
439 c
= get_dx_countlimit(inode
, dirent
, &count_offset
);
441 EXT4_ERROR_INODE(inode
, "dir seems corrupt? Run e2fsck -D.");
444 limit
= le16_to_cpu(c
->limit
);
445 count
= le16_to_cpu(c
->count
);
446 if (count_offset
+ (limit
* sizeof(struct dx_entry
)) >
447 EXT4_BLOCK_SIZE(inode
->i_sb
) - sizeof(struct dx_tail
)) {
448 warn_no_space_for_csum(inode
);
451 t
= (struct dx_tail
*)(((struct dx_entry
*)c
) + limit
);
453 if (t
->dt_checksum
!= ext4_dx_csum(inode
, dirent
, count_offset
,
459 static void ext4_dx_csum_set(struct inode
*inode
, struct ext4_dir_entry
*dirent
)
461 struct dx_countlimit
*c
;
463 int count_offset
, limit
, count
;
465 if (!ext4_has_metadata_csum(inode
->i_sb
))
468 c
= get_dx_countlimit(inode
, dirent
, &count_offset
);
470 EXT4_ERROR_INODE(inode
, "dir seems corrupt? Run e2fsck -D.");
473 limit
= le16_to_cpu(c
->limit
);
474 count
= le16_to_cpu(c
->count
);
475 if (count_offset
+ (limit
* sizeof(struct dx_entry
)) >
476 EXT4_BLOCK_SIZE(inode
->i_sb
) - sizeof(struct dx_tail
)) {
477 warn_no_space_for_csum(inode
);
480 t
= (struct dx_tail
*)(((struct dx_entry
*)c
) + limit
);
482 t
->dt_checksum
= ext4_dx_csum(inode
, dirent
, count_offset
, count
, t
);
485 static inline int ext4_handle_dirty_dx_node(handle_t
*handle
,
487 struct buffer_head
*bh
)
489 ext4_dx_csum_set(inode
, (struct ext4_dir_entry
*)bh
->b_data
);
490 return ext4_handle_dirty_metadata(handle
, inode
, bh
);
494 * p is at least 6 bytes before the end of page
496 static inline struct ext4_dir_entry_2
*
497 ext4_next_entry(struct ext4_dir_entry_2
*p
, unsigned long blocksize
)
499 return (struct ext4_dir_entry_2
*)((char *)p
+
500 ext4_rec_len_from_disk(p
->rec_len
, blocksize
));
504 * Future: use high four bits of block for coalesce-on-delete flags
505 * Mask them off for now.
508 static inline ext4_lblk_t
dx_get_block(struct dx_entry
*entry
)
510 return le32_to_cpu(entry
->block
) & 0x00ffffff;
513 static inline void dx_set_block(struct dx_entry
*entry
, ext4_lblk_t value
)
515 entry
->block
= cpu_to_le32(value
);
518 static inline unsigned dx_get_hash(struct dx_entry
*entry
)
520 return le32_to_cpu(entry
->hash
);
523 static inline void dx_set_hash(struct dx_entry
*entry
, unsigned value
)
525 entry
->hash
= cpu_to_le32(value
);
528 static inline unsigned dx_get_count(struct dx_entry
*entries
)
530 return le16_to_cpu(((struct dx_countlimit
*) entries
)->count
);
533 static inline unsigned dx_get_limit(struct dx_entry
*entries
)
535 return le16_to_cpu(((struct dx_countlimit
*) entries
)->limit
);
538 static inline void dx_set_count(struct dx_entry
*entries
, unsigned value
)
540 ((struct dx_countlimit
*) entries
)->count
= cpu_to_le16(value
);
543 static inline void dx_set_limit(struct dx_entry
*entries
, unsigned value
)
545 ((struct dx_countlimit
*) entries
)->limit
= cpu_to_le16(value
);
548 static inline unsigned dx_root_limit(struct inode
*dir
, unsigned infosize
)
550 unsigned entry_space
= dir
->i_sb
->s_blocksize
- EXT4_DIR_REC_LEN(1) -
551 EXT4_DIR_REC_LEN(2) - infosize
;
553 if (ext4_has_metadata_csum(dir
->i_sb
))
554 entry_space
-= sizeof(struct dx_tail
);
555 return entry_space
/ sizeof(struct dx_entry
);
558 static inline unsigned dx_node_limit(struct inode
*dir
)
560 unsigned entry_space
= dir
->i_sb
->s_blocksize
- EXT4_DIR_REC_LEN(0);
562 if (ext4_has_metadata_csum(dir
->i_sb
))
563 entry_space
-= sizeof(struct dx_tail
);
564 return entry_space
/ sizeof(struct dx_entry
);
571 static void dx_show_index(char * label
, struct dx_entry
*entries
)
573 int i
, n
= dx_get_count (entries
);
574 printk(KERN_DEBUG
"%s index ", label
);
575 for (i
= 0; i
< n
; i
++) {
576 printk("%x->%lu ", i
? dx_get_hash(entries
+ i
) :
577 0, (unsigned long)dx_get_block(entries
+ i
));
589 static struct stats
dx_show_leaf(struct inode
*dir
,
590 struct dx_hash_info
*hinfo
,
591 struct ext4_dir_entry_2
*de
,
592 int size
, int show_names
)
594 unsigned names
= 0, space
= 0;
595 char *base
= (char *) de
;
596 struct dx_hash_info h
= *hinfo
;
599 while ((char *) de
< base
+ size
)
605 #ifdef CONFIG_EXT4_FS_ENCRYPTION
608 struct ext4_str fname_crypto_str
609 = {.name
= NULL
, .len
= 0};
610 struct ext4_fname_crypto_ctx
*ctx
= NULL
;
615 ctx
= ext4_get_fname_crypto_ctx(dir
,
618 printk(KERN_WARNING
"Error acquiring"
619 " crypto ctxt--skipping crypto\n");
623 /* Directory is not encrypted */
624 ext4fs_dirhash(de
->name
,
626 printk("%*.s:(U)%x.%u ", len
,
628 (unsigned) ((char *) de
631 /* Directory is encrypted */
632 res
= ext4_fname_crypto_alloc_buffer(
636 printk(KERN_WARNING
"Error "
640 ext4_put_fname_crypto_ctx(&ctx
);
643 res
= ext4_fname_disk_to_usr(ctx
, NULL
, de
,
646 printk(KERN_WARNING
"Error "
647 "converting filename "
653 name
= fname_crypto_str
.name
;
654 len
= fname_crypto_str
.len
;
656 ext4fs_dirhash(de
->name
, de
->name_len
,
658 printk("%*.s:(E)%x.%u ", len
, name
,
659 h
.hash
, (unsigned) ((char *) de
661 ext4_put_fname_crypto_ctx(&ctx
);
662 ext4_fname_crypto_free_buffer(
666 int len
= de
->name_len
;
667 char *name
= de
->name
;
668 ext4fs_dirhash(de
->name
, de
->name_len
, &h
);
669 printk("%*.s:%x.%u ", len
, name
, h
.hash
,
670 (unsigned) ((char *) de
- base
));
673 space
+= EXT4_DIR_REC_LEN(de
->name_len
);
676 de
= ext4_next_entry(de
, size
);
678 printk("(%i)\n", names
);
679 return (struct stats
) { names
, space
, 1 };
682 struct stats
dx_show_entries(struct dx_hash_info
*hinfo
, struct inode
*dir
,
683 struct dx_entry
*entries
, int levels
)
685 unsigned blocksize
= dir
->i_sb
->s_blocksize
;
686 unsigned count
= dx_get_count(entries
), names
= 0, space
= 0, i
;
688 struct buffer_head
*bh
;
689 printk("%i indexed blocks...\n", count
);
690 for (i
= 0; i
< count
; i
++, entries
++)
692 ext4_lblk_t block
= dx_get_block(entries
);
693 ext4_lblk_t hash
= i
? dx_get_hash(entries
): 0;
694 u32 range
= i
< count
- 1? (dx_get_hash(entries
+ 1) - hash
): ~hash
;
696 printk("%s%3u:%03u hash %8x/%8x ",levels
?"":" ", i
, block
, hash
, range
);
697 bh
= ext4_bread(NULL
,dir
, block
, 0);
698 if (!bh
|| IS_ERR(bh
))
701 dx_show_entries(hinfo
, dir
, ((struct dx_node
*) bh
->b_data
)->entries
, levels
- 1):
702 dx_show_leaf(dir
, hinfo
, (struct ext4_dir_entry_2
*)
703 bh
->b_data
, blocksize
, 0);
704 names
+= stats
.names
;
705 space
+= stats
.space
;
706 bcount
+= stats
.bcount
;
710 printk(KERN_DEBUG
"%snames %u, fullness %u (%u%%)\n",
711 levels
? "" : " ", names
, space
/bcount
,
712 (space
/bcount
)*100/blocksize
);
713 return (struct stats
) { names
, space
, bcount
};
715 #endif /* DX_DEBUG */
718 * Probe for a directory leaf block to search.
720 * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
721 * error in the directory index, and the caller should fall back to
722 * searching the directory normally. The callers of dx_probe **MUST**
723 * check for this error code, and make sure it never gets reflected
726 static struct dx_frame
*
727 dx_probe(const struct qstr
*d_name
, struct inode
*dir
,
728 struct dx_hash_info
*hinfo
, struct dx_frame
*frame_in
)
730 unsigned count
, indirect
;
731 struct dx_entry
*at
, *entries
, *p
, *q
, *m
;
732 struct dx_root
*root
;
733 struct dx_frame
*frame
= frame_in
;
734 struct dx_frame
*ret_err
= ERR_PTR(ERR_BAD_DX_DIR
);
737 frame
->bh
= ext4_read_dirblock(dir
, 0, INDEX
);
738 if (IS_ERR(frame
->bh
))
739 return (struct dx_frame
*) frame
->bh
;
741 root
= (struct dx_root
*) frame
->bh
->b_data
;
742 if (root
->info
.hash_version
!= DX_HASH_TEA
&&
743 root
->info
.hash_version
!= DX_HASH_HALF_MD4
&&
744 root
->info
.hash_version
!= DX_HASH_LEGACY
) {
745 ext4_warning(dir
->i_sb
, "Unrecognised inode hash code %d",
746 root
->info
.hash_version
);
749 hinfo
->hash_version
= root
->info
.hash_version
;
750 if (hinfo
->hash_version
<= DX_HASH_TEA
)
751 hinfo
->hash_version
+= EXT4_SB(dir
->i_sb
)->s_hash_unsigned
;
752 hinfo
->seed
= EXT4_SB(dir
->i_sb
)->s_hash_seed
;
753 #ifdef CONFIG_EXT4_FS_ENCRYPTION
755 struct ext4_fname_crypto_ctx
*ctx
= NULL
;
758 /* Check if the directory is encrypted */
759 ctx
= ext4_get_fname_crypto_ctx(dir
, EXT4_NAME_LEN
);
761 ret_err
= ERR_PTR(PTR_ERR(ctx
));
764 res
= ext4_fname_usr_to_hash(ctx
, d_name
, hinfo
);
766 ret_err
= ERR_PTR(res
);
769 ext4_put_fname_crypto_ctx(&ctx
);
773 ext4fs_dirhash(d_name
->name
, d_name
->len
, hinfo
);
777 if (root
->info
.unused_flags
& 1) {
778 ext4_warning(dir
->i_sb
, "Unimplemented inode hash flags: %#06x",
779 root
->info
.unused_flags
);
783 if ((indirect
= root
->info
.indirect_levels
) > 1) {
784 ext4_warning(dir
->i_sb
, "Unimplemented inode hash depth: %#06x",
785 root
->info
.indirect_levels
);
789 entries
= (struct dx_entry
*) (((char *)&root
->info
) +
790 root
->info
.info_length
);
792 if (dx_get_limit(entries
) != dx_root_limit(dir
,
793 root
->info
.info_length
)) {
794 ext4_warning(dir
->i_sb
, "dx entry: limit != root limit");
798 dxtrace(printk("Look up %x", hash
));
800 count
= dx_get_count(entries
);
801 if (!count
|| count
> dx_get_limit(entries
)) {
802 ext4_warning(dir
->i_sb
,
803 "dx entry: no count or count > limit");
808 q
= entries
+ count
- 1;
811 dxtrace(printk("."));
812 if (dx_get_hash(m
) > hash
)
818 if (0) { // linear search cross check
819 unsigned n
= count
- 1;
823 dxtrace(printk(","));
824 if (dx_get_hash(++at
) > hash
)
830 assert (at
== p
- 1);
834 dxtrace(printk(" %x->%u\n", at
== entries
? 0: dx_get_hash(at
), dx_get_block(at
)));
835 frame
->entries
= entries
;
840 frame
->bh
= ext4_read_dirblock(dir
, dx_get_block(at
), INDEX
);
841 if (IS_ERR(frame
->bh
)) {
842 ret_err
= (struct dx_frame
*) frame
->bh
;
846 entries
= ((struct dx_node
*) frame
->bh
->b_data
)->entries
;
848 if (dx_get_limit(entries
) != dx_node_limit (dir
)) {
849 ext4_warning(dir
->i_sb
,
850 "dx entry: limit != node limit");
855 while (frame
>= frame_in
) {
860 if (ret_err
== ERR_PTR(ERR_BAD_DX_DIR
))
861 ext4_warning(dir
->i_sb
,
862 "Corrupt dir inode %lu, running e2fsck is "
863 "recommended.", dir
->i_ino
);
867 static void dx_release (struct dx_frame
*frames
)
869 if (frames
[0].bh
== NULL
)
872 if (((struct dx_root
*) frames
[0].bh
->b_data
)->info
.indirect_levels
)
873 brelse(frames
[1].bh
);
874 brelse(frames
[0].bh
);
878 * This function increments the frame pointer to search the next leaf
879 * block, and reads in the necessary intervening nodes if the search
880 * should be necessary. Whether or not the search is necessary is
881 * controlled by the hash parameter. If the hash value is even, then
882 * the search is only continued if the next block starts with that
883 * hash value. This is used if we are searching for a specific file.
885 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
887 * This function returns 1 if the caller should continue to search,
888 * or 0 if it should not. If there is an error reading one of the
889 * index blocks, it will a negative error code.
891 * If start_hash is non-null, it will be filled in with the starting
892 * hash of the next page.
894 static int ext4_htree_next_block(struct inode
*dir
, __u32 hash
,
895 struct dx_frame
*frame
,
896 struct dx_frame
*frames
,
900 struct buffer_head
*bh
;
906 * Find the next leaf page by incrementing the frame pointer.
907 * If we run out of entries in the interior node, loop around and
908 * increment pointer in the parent node. When we break out of
909 * this loop, num_frames indicates the number of interior
910 * nodes need to be read.
913 if (++(p
->at
) < p
->entries
+ dx_get_count(p
->entries
))
922 * If the hash is 1, then continue only if the next page has a
923 * continuation hash of any value. This is used for readdir
924 * handling. Otherwise, check to see if the hash matches the
925 * desired contiuation hash. If it doesn't, return since
926 * there's no point to read in the successive index pages.
928 bhash
= dx_get_hash(p
->at
);
931 if ((hash
& 1) == 0) {
932 if ((bhash
& ~1) != hash
)
936 * If the hash is HASH_NB_ALWAYS, we always go to the next
937 * block so no check is necessary
939 while (num_frames
--) {
940 bh
= ext4_read_dirblock(dir
, dx_get_block(p
->at
), INDEX
);
946 p
->at
= p
->entries
= ((struct dx_node
*) bh
->b_data
)->entries
;
953 * This function fills a red-black tree with information from a
954 * directory block. It returns the number directory entries loaded
955 * into the tree. If there is an error it is returned in err.
957 static int htree_dirblock_to_tree(struct file
*dir_file
,
958 struct inode
*dir
, ext4_lblk_t block
,
959 struct dx_hash_info
*hinfo
,
960 __u32 start_hash
, __u32 start_minor_hash
)
962 struct buffer_head
*bh
;
963 struct ext4_dir_entry_2
*de
, *top
;
964 int err
= 0, count
= 0;
965 struct ext4_fname_crypto_ctx
*ctx
= NULL
;
966 struct ext4_str fname_crypto_str
= {.name
= NULL
, .len
= 0}, tmp_str
;
968 dxtrace(printk(KERN_INFO
"In htree dirblock_to_tree: block %lu\n",
969 (unsigned long)block
));
970 bh
= ext4_read_dirblock(dir
, block
, DIRENT
);
974 de
= (struct ext4_dir_entry_2
*) bh
->b_data
;
975 top
= (struct ext4_dir_entry_2
*) ((char *) de
+
976 dir
->i_sb
->s_blocksize
-
977 EXT4_DIR_REC_LEN(0));
978 #ifdef CONFIG_EXT4_FS_ENCRYPTION
979 /* Check if the directory is encrypted */
980 ctx
= ext4_get_fname_crypto_ctx(dir
, EXT4_NAME_LEN
);
987 err
= ext4_fname_crypto_alloc_buffer(ctx
, EXT4_NAME_LEN
,
990 ext4_put_fname_crypto_ctx(&ctx
);
996 for (; de
< top
; de
= ext4_next_entry(de
, dir
->i_sb
->s_blocksize
)) {
997 if (ext4_check_dir_entry(dir
, NULL
, de
, bh
,
998 bh
->b_data
, bh
->b_size
,
999 (block
<<EXT4_BLOCK_SIZE_BITS(dir
->i_sb
))
1000 + ((char *)de
- bh
->b_data
))) {
1001 /* silently ignore the rest of the block */
1004 ext4fs_dirhash(de
->name
, de
->name_len
, hinfo
);
1005 if ((hinfo
->hash
< start_hash
) ||
1006 ((hinfo
->hash
== start_hash
) &&
1007 (hinfo
->minor_hash
< start_minor_hash
)))
1012 /* Directory is not encrypted */
1013 tmp_str
.name
= de
->name
;
1014 tmp_str
.len
= de
->name_len
;
1015 err
= ext4_htree_store_dirent(dir_file
,
1016 hinfo
->hash
, hinfo
->minor_hash
, de
,
1019 /* Directory is encrypted */
1020 err
= ext4_fname_disk_to_usr(ctx
, hinfo
, de
,
1026 err
= ext4_htree_store_dirent(dir_file
,
1027 hinfo
->hash
, hinfo
->minor_hash
, de
,
1038 #ifdef CONFIG_EXT4_FS_ENCRYPTION
1039 ext4_put_fname_crypto_ctx(&ctx
);
1040 ext4_fname_crypto_free_buffer(&fname_crypto_str
);
1047 * This function fills a red-black tree with information from a
1048 * directory. We start scanning the directory in hash order, starting
1049 * at start_hash and start_minor_hash.
1051 * This function returns the number of entries inserted into the tree,
1052 * or a negative error code.
1054 int ext4_htree_fill_tree(struct file
*dir_file
, __u32 start_hash
,
1055 __u32 start_minor_hash
, __u32
*next_hash
)
1057 struct dx_hash_info hinfo
;
1058 struct ext4_dir_entry_2
*de
;
1059 struct dx_frame frames
[2], *frame
;
1065 struct ext4_str tmp_str
;
1067 dxtrace(printk(KERN_DEBUG
"In htree_fill_tree, start hash: %x:%x\n",
1068 start_hash
, start_minor_hash
));
1069 dir
= file_inode(dir_file
);
1070 if (!(ext4_test_inode_flag(dir
, EXT4_INODE_INDEX
))) {
1071 hinfo
.hash_version
= EXT4_SB(dir
->i_sb
)->s_def_hash_version
;
1072 if (hinfo
.hash_version
<= DX_HASH_TEA
)
1073 hinfo
.hash_version
+=
1074 EXT4_SB(dir
->i_sb
)->s_hash_unsigned
;
1075 hinfo
.seed
= EXT4_SB(dir
->i_sb
)->s_hash_seed
;
1076 if (ext4_has_inline_data(dir
)) {
1077 int has_inline_data
= 1;
1078 count
= htree_inlinedir_to_tree(dir_file
, dir
, 0,
1082 if (has_inline_data
) {
1087 count
= htree_dirblock_to_tree(dir_file
, dir
, 0, &hinfo
,
1088 start_hash
, start_minor_hash
);
1092 hinfo
.hash
= start_hash
;
1093 hinfo
.minor_hash
= 0;
1094 frame
= dx_probe(NULL
, dir
, &hinfo
, frames
);
1096 return PTR_ERR(frame
);
1098 /* Add '.' and '..' from the htree header */
1099 if (!start_hash
&& !start_minor_hash
) {
1100 de
= (struct ext4_dir_entry_2
*) frames
[0].bh
->b_data
;
1101 tmp_str
.name
= de
->name
;
1102 tmp_str
.len
= de
->name_len
;
1103 err
= ext4_htree_store_dirent(dir_file
, 0, 0,
1109 if (start_hash
< 2 || (start_hash
==2 && start_minor_hash
==0)) {
1110 de
= (struct ext4_dir_entry_2
*) frames
[0].bh
->b_data
;
1111 de
= ext4_next_entry(de
, dir
->i_sb
->s_blocksize
);
1112 tmp_str
.name
= de
->name
;
1113 tmp_str
.len
= de
->name_len
;
1114 err
= ext4_htree_store_dirent(dir_file
, 2, 0,
1122 block
= dx_get_block(frame
->at
);
1123 ret
= htree_dirblock_to_tree(dir_file
, dir
, block
, &hinfo
,
1124 start_hash
, start_minor_hash
);
1131 ret
= ext4_htree_next_block(dir
, HASH_NB_ALWAYS
,
1132 frame
, frames
, &hashval
);
1133 *next_hash
= hashval
;
1139 * Stop if: (a) there are no more entries, or
1140 * (b) we have inserted at least one entry and the
1141 * next hash value is not a continuation
1144 (count
&& ((hashval
& 1) == 0)))
1148 dxtrace(printk(KERN_DEBUG
"Fill tree: returned %d entries, "
1149 "next hash: %x\n", count
, *next_hash
));
1156 static inline int search_dirblock(struct buffer_head
*bh
,
1158 const struct qstr
*d_name
,
1159 unsigned int offset
,
1160 struct ext4_dir_entry_2
**res_dir
)
1162 return search_dir(bh
, bh
->b_data
, dir
->i_sb
->s_blocksize
, dir
,
1163 d_name
, offset
, res_dir
);
1167 * Directory block splitting, compacting
1171 * Create map of hash values, offsets, and sizes, stored at end of block.
1172 * Returns number of entries mapped.
1174 static int dx_make_map(struct inode
*dir
, struct ext4_dir_entry_2
*de
,
1175 unsigned blocksize
, struct dx_hash_info
*hinfo
,
1176 struct dx_map_entry
*map_tail
)
1179 char *base
= (char *) de
;
1180 struct dx_hash_info h
= *hinfo
;
1182 while ((char *) de
< base
+ blocksize
) {
1183 if (de
->name_len
&& de
->inode
) {
1184 ext4fs_dirhash(de
->name
, de
->name_len
, &h
);
1186 map_tail
->hash
= h
.hash
;
1187 map_tail
->offs
= ((char *) de
- base
)>>2;
1188 map_tail
->size
= le16_to_cpu(de
->rec_len
);
1192 /* XXX: do we need to check rec_len == 0 case? -Chris */
1193 de
= ext4_next_entry(de
, blocksize
);
1198 /* Sort map by hash value */
1199 static void dx_sort_map (struct dx_map_entry
*map
, unsigned count
)
1201 struct dx_map_entry
*p
, *q
, *top
= map
+ count
- 1;
1203 /* Combsort until bubble sort doesn't suck */
1205 count
= count
*10/13;
1206 if (count
- 9 < 2) /* 9, 10 -> 11 */
1208 for (p
= top
, q
= p
- count
; q
>= map
; p
--, q
--)
1209 if (p
->hash
< q
->hash
)
1212 /* Garden variety bubble sort */
1217 if (q
[1].hash
>= q
[0].hash
)
1225 static void dx_insert_block(struct dx_frame
*frame
, u32 hash
, ext4_lblk_t block
)
1227 struct dx_entry
*entries
= frame
->entries
;
1228 struct dx_entry
*old
= frame
->at
, *new = old
+ 1;
1229 int count
= dx_get_count(entries
);
1231 assert(count
< dx_get_limit(entries
));
1232 assert(old
< entries
+ count
);
1233 memmove(new + 1, new, (char *)(entries
+ count
) - (char *)(new));
1234 dx_set_hash(new, hash
);
1235 dx_set_block(new, block
);
1236 dx_set_count(entries
, count
+ 1);
1240 * NOTE! unlike strncmp, ext4_match returns 1 for success, 0 for failure.
1242 * `len <= EXT4_NAME_LEN' is guaranteed by caller.
1243 * `de != NULL' is guaranteed by caller.
1245 static inline int ext4_match(struct ext4_fname_crypto_ctx
*ctx
,
1246 struct ext4_str
*fname_crypto_str
,
1247 int len
, const char * const name
,
1248 struct ext4_dir_entry_2
*de
)
1255 #ifdef CONFIG_EXT4_FS_ENCRYPTION
1257 return ext4_fname_match(ctx
, fname_crypto_str
, len
, name
, de
);
1259 if (len
!= de
->name_len
)
1261 res
= memcmp(name
, de
->name
, len
);
1262 return (res
== 0) ? 1 : 0;
1266 * Returns 0 if not found, -1 on failure, and 1 on success
1268 int search_dir(struct buffer_head
*bh
, char *search_buf
, int buf_size
,
1269 struct inode
*dir
, const struct qstr
*d_name
,
1270 unsigned int offset
, struct ext4_dir_entry_2
**res_dir
)
1272 struct ext4_dir_entry_2
* de
;
1275 const char *name
= d_name
->name
;
1276 int namelen
= d_name
->len
;
1277 struct ext4_fname_crypto_ctx
*ctx
= NULL
;
1278 struct ext4_str fname_crypto_str
= {.name
= NULL
, .len
= 0};
1281 ctx
= ext4_get_fname_crypto_ctx(dir
, EXT4_NAME_LEN
);
1285 de
= (struct ext4_dir_entry_2
*)search_buf
;
1286 dlimit
= search_buf
+ buf_size
;
1287 while ((char *) de
< dlimit
) {
1288 /* this code is executed quadratically often */
1289 /* do minimal checking `by hand' */
1290 if ((char *) de
+ de
->name_len
<= dlimit
) {
1291 res
= ext4_match(ctx
, &fname_crypto_str
, namelen
,
1298 /* found a match - just to be sure, do
1300 if (ext4_check_dir_entry(dir
, NULL
, de
, bh
,
1302 bh
->b_size
, offset
)) {
1312 /* prevent looping on a bad block */
1313 de_len
= ext4_rec_len_from_disk(de
->rec_len
,
1314 dir
->i_sb
->s_blocksize
);
1320 de
= (struct ext4_dir_entry_2
*) ((char *) de
+ de_len
);
1325 ext4_put_fname_crypto_ctx(&ctx
);
1326 ext4_fname_crypto_free_buffer(&fname_crypto_str
);
1330 static int is_dx_internal_node(struct inode
*dir
, ext4_lblk_t block
,
1331 struct ext4_dir_entry
*de
)
1333 struct super_block
*sb
= dir
->i_sb
;
1339 if (de
->inode
== 0 &&
1340 ext4_rec_len_from_disk(de
->rec_len
, sb
->s_blocksize
) ==
1349 * finds an entry in the specified directory with the wanted name. It
1350 * returns the cache buffer in which the entry was found, and the entry
1351 * itself (as a parameter - res_dir). It does NOT read the inode of the
1352 * entry - you'll have to do that yourself if you want to.
1354 * The returned buffer_head has ->b_count elevated. The caller is expected
1355 * to brelse() it when appropriate.
1357 static struct buffer_head
* ext4_find_entry (struct inode
*dir
,
1358 const struct qstr
*d_name
,
1359 struct ext4_dir_entry_2
**res_dir
,
1362 struct super_block
*sb
;
1363 struct buffer_head
*bh_use
[NAMEI_RA_SIZE
];
1364 struct buffer_head
*bh
, *ret
= NULL
;
1365 ext4_lblk_t start
, block
, b
;
1366 const u8
*name
= d_name
->name
;
1367 int ra_max
= 0; /* Number of bh's in the readahead
1369 int ra_ptr
= 0; /* Current index into readahead
1372 ext4_lblk_t nblocks
;
1377 namelen
= d_name
->len
;
1378 if (namelen
> EXT4_NAME_LEN
)
1381 if (ext4_has_inline_data(dir
)) {
1382 int has_inline_data
= 1;
1383 ret
= ext4_find_inline_entry(dir
, d_name
, res_dir
,
1385 if (has_inline_data
) {
1392 if ((namelen
<= 2) && (name
[0] == '.') &&
1393 (name
[1] == '.' || name
[1] == '\0')) {
1395 * "." or ".." will only be in the first block
1396 * NFS may look up ".."; "." should be handled by the VFS
1403 bh
= ext4_dx_find_entry(dir
, d_name
, res_dir
);
1405 * On success, or if the error was file not found,
1406 * return. Otherwise, fall back to doing a search the
1407 * old fashioned way.
1409 if (!IS_ERR(bh
) || PTR_ERR(bh
) != ERR_BAD_DX_DIR
)
1411 dxtrace(printk(KERN_DEBUG
"ext4_find_entry: dx failed, "
1414 nblocks
= dir
->i_size
>> EXT4_BLOCK_SIZE_BITS(sb
);
1415 start
= EXT4_I(dir
)->i_dir_start_lookup
;
1416 if (start
>= nblocks
)
1422 * We deal with the read-ahead logic here.
1424 if (ra_ptr
>= ra_max
) {
1425 /* Refill the readahead buffer */
1428 for (ra_max
= 0; ra_max
< NAMEI_RA_SIZE
; ra_max
++) {
1430 * Terminate if we reach the end of the
1431 * directory and must wrap, or if our
1432 * search has finished at this block.
1434 if (b
>= nblocks
|| (num
&& block
== start
)) {
1435 bh_use
[ra_max
] = NULL
;
1439 bh
= ext4_getblk(NULL
, dir
, b
++, 0);
1440 if (unlikely(IS_ERR(bh
))) {
1445 bh_use
[ra_max
] = bh
;
1447 ll_rw_block(READ
| REQ_META
| REQ_PRIO
,
1451 if ((bh
= bh_use
[ra_ptr
++]) == NULL
)
1454 if (!buffer_uptodate(bh
)) {
1455 /* read error, skip block & hope for the best */
1456 EXT4_ERROR_INODE(dir
, "reading directory lblock %lu",
1457 (unsigned long) block
);
1461 if (!buffer_verified(bh
) &&
1462 !is_dx_internal_node(dir
, block
,
1463 (struct ext4_dir_entry
*)bh
->b_data
) &&
1464 !ext4_dirent_csum_verify(dir
,
1465 (struct ext4_dir_entry
*)bh
->b_data
)) {
1466 EXT4_ERROR_INODE(dir
, "checksumming directory "
1467 "block %lu", (unsigned long)block
);
1471 set_buffer_verified(bh
);
1472 i
= search_dirblock(bh
, dir
, d_name
,
1473 block
<< EXT4_BLOCK_SIZE_BITS(sb
), res_dir
);
1475 EXT4_I(dir
)->i_dir_start_lookup
= block
;
1477 goto cleanup_and_exit
;
1481 goto cleanup_and_exit
;
1484 if (++block
>= nblocks
)
1486 } while (block
!= start
);
1489 * If the directory has grown while we were searching, then
1490 * search the last part of the directory before giving up.
1493 nblocks
= dir
->i_size
>> EXT4_BLOCK_SIZE_BITS(sb
);
1494 if (block
< nblocks
) {
1500 /* Clean up the read-ahead blocks */
1501 for (; ra_ptr
< ra_max
; ra_ptr
++)
1502 brelse(bh_use
[ra_ptr
]);
1506 static struct buffer_head
* ext4_dx_find_entry(struct inode
*dir
, const struct qstr
*d_name
,
1507 struct ext4_dir_entry_2
**res_dir
)
1509 struct super_block
* sb
= dir
->i_sb
;
1510 struct dx_hash_info hinfo
;
1511 struct dx_frame frames
[2], *frame
;
1512 struct buffer_head
*bh
;
1516 #ifdef CONFIG_EXT4_FS_ENCRYPTION
1519 frame
= dx_probe(d_name
, dir
, &hinfo
, frames
);
1521 return (struct buffer_head
*) frame
;
1523 block
= dx_get_block(frame
->at
);
1524 bh
= ext4_read_dirblock(dir
, block
, DIRENT
);
1528 retval
= search_dirblock(bh
, dir
, d_name
,
1529 block
<< EXT4_BLOCK_SIZE_BITS(sb
),
1535 bh
= ERR_PTR(ERR_BAD_DX_DIR
);
1539 /* Check to see if we should continue to search */
1540 retval
= ext4_htree_next_block(dir
, hinfo
.hash
, frame
,
1544 "error %d reading index page in directory #%lu",
1545 retval
, dir
->i_ino
);
1546 bh
= ERR_PTR(retval
);
1549 } while (retval
== 1);
1553 dxtrace(printk(KERN_DEBUG
"%s not found\n", d_name
->name
));
1559 static struct dentry
*ext4_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
1561 struct inode
*inode
;
1562 struct ext4_dir_entry_2
*de
;
1563 struct buffer_head
*bh
;
1565 if (dentry
->d_name
.len
> EXT4_NAME_LEN
)
1566 return ERR_PTR(-ENAMETOOLONG
);
1568 bh
= ext4_find_entry(dir
, &dentry
->d_name
, &de
, NULL
);
1570 return (struct dentry
*) bh
;
1573 __u32 ino
= le32_to_cpu(de
->inode
);
1575 if (!ext4_valid_inum(dir
->i_sb
, ino
)) {
1576 EXT4_ERROR_INODE(dir
, "bad inode number: %u", ino
);
1577 return ERR_PTR(-EIO
);
1579 if (unlikely(ino
== dir
->i_ino
)) {
1580 EXT4_ERROR_INODE(dir
, "'%pd' linked to parent dir",
1582 return ERR_PTR(-EIO
);
1584 inode
= ext4_iget_normal(dir
->i_sb
, ino
);
1585 if (inode
== ERR_PTR(-ESTALE
)) {
1586 EXT4_ERROR_INODE(dir
,
1587 "deleted inode referenced: %u",
1589 return ERR_PTR(-EIO
);
1591 if (!IS_ERR(inode
) && ext4_encrypted_inode(dir
) &&
1592 (S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
1593 S_ISLNK(inode
->i_mode
)) &&
1594 !ext4_is_child_context_consistent_with_parent(dir
,
1597 ext4_warning(inode
->i_sb
,
1598 "Inconsistent encryption contexts: %lu/%lu\n",
1599 (unsigned long) dir
->i_ino
,
1600 (unsigned long) inode
->i_ino
);
1601 return ERR_PTR(-EPERM
);
1604 return d_splice_alias(inode
, dentry
);
1608 struct dentry
*ext4_get_parent(struct dentry
*child
)
1611 static const struct qstr dotdot
= QSTR_INIT("..", 2);
1612 struct ext4_dir_entry_2
* de
;
1613 struct buffer_head
*bh
;
1615 bh
= ext4_find_entry(d_inode(child
), &dotdot
, &de
, NULL
);
1617 return (struct dentry
*) bh
;
1619 return ERR_PTR(-ENOENT
);
1620 ino
= le32_to_cpu(de
->inode
);
1623 if (!ext4_valid_inum(d_inode(child
)->i_sb
, ino
)) {
1624 EXT4_ERROR_INODE(d_inode(child
),
1625 "bad parent inode number: %u", ino
);
1626 return ERR_PTR(-EIO
);
1629 return d_obtain_alias(ext4_iget_normal(d_inode(child
)->i_sb
, ino
));
1633 * Move count entries from end of map between two memory locations.
1634 * Returns pointer to last entry moved.
1636 static struct ext4_dir_entry_2
*
1637 dx_move_dirents(char *from
, char *to
, struct dx_map_entry
*map
, int count
,
1640 unsigned rec_len
= 0;
1643 struct ext4_dir_entry_2
*de
= (struct ext4_dir_entry_2
*)
1644 (from
+ (map
->offs
<<2));
1645 rec_len
= EXT4_DIR_REC_LEN(de
->name_len
);
1646 memcpy (to
, de
, rec_len
);
1647 ((struct ext4_dir_entry_2
*) to
)->rec_len
=
1648 ext4_rec_len_to_disk(rec_len
, blocksize
);
1653 return (struct ext4_dir_entry_2
*) (to
- rec_len
);
1657 * Compact each dir entry in the range to the minimal rec_len.
1658 * Returns pointer to last entry in range.
1660 static struct ext4_dir_entry_2
* dx_pack_dirents(char *base
, unsigned blocksize
)
1662 struct ext4_dir_entry_2
*next
, *to
, *prev
, *de
= (struct ext4_dir_entry_2
*) base
;
1663 unsigned rec_len
= 0;
1666 while ((char*)de
< base
+ blocksize
) {
1667 next
= ext4_next_entry(de
, blocksize
);
1668 if (de
->inode
&& de
->name_len
) {
1669 rec_len
= EXT4_DIR_REC_LEN(de
->name_len
);
1671 memmove(to
, de
, rec_len
);
1672 to
->rec_len
= ext4_rec_len_to_disk(rec_len
, blocksize
);
1674 to
= (struct ext4_dir_entry_2
*) (((char *) to
) + rec_len
);
1682 * Split a full leaf block to make room for a new dir entry.
1683 * Allocate a new block, and move entries so that they are approx. equally full.
1684 * Returns pointer to de in block into which the new entry will be inserted.
1686 static struct ext4_dir_entry_2
*do_split(handle_t
*handle
, struct inode
*dir
,
1687 struct buffer_head
**bh
,struct dx_frame
*frame
,
1688 struct dx_hash_info
*hinfo
)
1690 unsigned blocksize
= dir
->i_sb
->s_blocksize
;
1691 unsigned count
, continued
;
1692 struct buffer_head
*bh2
;
1693 ext4_lblk_t newblock
;
1695 struct dx_map_entry
*map
;
1696 char *data1
= (*bh
)->b_data
, *data2
;
1697 unsigned split
, move
, size
;
1698 struct ext4_dir_entry_2
*de
= NULL
, *de2
;
1699 struct ext4_dir_entry_tail
*t
;
1703 if (ext4_has_metadata_csum(dir
->i_sb
))
1704 csum_size
= sizeof(struct ext4_dir_entry_tail
);
1706 bh2
= ext4_append(handle
, dir
, &newblock
);
1710 return (struct ext4_dir_entry_2
*) bh2
;
1713 BUFFER_TRACE(*bh
, "get_write_access");
1714 err
= ext4_journal_get_write_access(handle
, *bh
);
1718 BUFFER_TRACE(frame
->bh
, "get_write_access");
1719 err
= ext4_journal_get_write_access(handle
, frame
->bh
);
1723 data2
= bh2
->b_data
;
1725 /* create map in the end of data2 block */
1726 map
= (struct dx_map_entry
*) (data2
+ blocksize
);
1727 count
= dx_make_map(dir
, (struct ext4_dir_entry_2
*) data1
,
1728 blocksize
, hinfo
, map
);
1730 dx_sort_map(map
, count
);
1731 /* Split the existing block in the middle, size-wise */
1734 for (i
= count
-1; i
>= 0; i
--) {
1735 /* is more than half of this entry in 2nd half of the block? */
1736 if (size
+ map
[i
].size
/2 > blocksize
/2)
1738 size
+= map
[i
].size
;
1741 /* map index at which we will split */
1742 split
= count
- move
;
1743 hash2
= map
[split
].hash
;
1744 continued
= hash2
== map
[split
- 1].hash
;
1745 dxtrace(printk(KERN_INFO
"Split block %lu at %x, %i/%i\n",
1746 (unsigned long)dx_get_block(frame
->at
),
1747 hash2
, split
, count
-split
));
1749 /* Fancy dance to stay within two buffers */
1750 de2
= dx_move_dirents(data1
, data2
, map
+ split
, count
- split
,
1752 de
= dx_pack_dirents(data1
, blocksize
);
1753 de
->rec_len
= ext4_rec_len_to_disk(data1
+ (blocksize
- csum_size
) -
1756 de2
->rec_len
= ext4_rec_len_to_disk(data2
+ (blocksize
- csum_size
) -
1760 t
= EXT4_DIRENT_TAIL(data2
, blocksize
);
1761 initialize_dirent_tail(t
, blocksize
);
1763 t
= EXT4_DIRENT_TAIL(data1
, blocksize
);
1764 initialize_dirent_tail(t
, blocksize
);
1767 dxtrace(dx_show_leaf(dir
, hinfo
, (struct ext4_dir_entry_2
*) data1
,
1769 dxtrace(dx_show_leaf(dir
, hinfo
, (struct ext4_dir_entry_2
*) data2
,
1772 /* Which block gets the new entry? */
1773 if (hinfo
->hash
>= hash2
) {
1777 dx_insert_block(frame
, hash2
+ continued
, newblock
);
1778 err
= ext4_handle_dirty_dirent_node(handle
, dir
, bh2
);
1781 err
= ext4_handle_dirty_dx_node(handle
, dir
, frame
->bh
);
1785 dxtrace(dx_show_index("frame", frame
->entries
));
1792 ext4_std_error(dir
->i_sb
, err
);
1793 return ERR_PTR(err
);
1796 int ext4_find_dest_de(struct inode
*dir
, struct inode
*inode
,
1797 struct buffer_head
*bh
,
1798 void *buf
, int buf_size
,
1799 const char *name
, int namelen
,
1800 struct ext4_dir_entry_2
**dest_de
)
1802 struct ext4_dir_entry_2
*de
;
1803 unsigned short reclen
= EXT4_DIR_REC_LEN(namelen
);
1805 unsigned int offset
= 0;
1807 struct ext4_fname_crypto_ctx
*ctx
= NULL
;
1808 struct ext4_str fname_crypto_str
= {.name
= NULL
, .len
= 0};
1811 ctx
= ext4_get_fname_crypto_ctx(dir
, EXT4_NAME_LEN
);
1816 /* Calculate record length needed to store the entry */
1817 res
= ext4_fname_crypto_namelen_on_disk(ctx
, namelen
);
1819 ext4_put_fname_crypto_ctx(&ctx
);
1822 reclen
= EXT4_DIR_REC_LEN(res
);
1825 de
= (struct ext4_dir_entry_2
*)buf
;
1826 top
= buf
+ buf_size
- reclen
;
1827 while ((char *) de
<= top
) {
1828 if (ext4_check_dir_entry(dir
, NULL
, de
, bh
,
1829 buf
, buf_size
, offset
)) {
1833 /* Provide crypto context and crypto buffer to ext4 match */
1834 res
= ext4_match(ctx
, &fname_crypto_str
, namelen
, name
, de
);
1841 nlen
= EXT4_DIR_REC_LEN(de
->name_len
);
1842 rlen
= ext4_rec_len_from_disk(de
->rec_len
, buf_size
);
1843 if ((de
->inode
? rlen
- nlen
: rlen
) >= reclen
)
1845 de
= (struct ext4_dir_entry_2
*)((char *)de
+ rlen
);
1849 if ((char *) de
> top
)
1856 ext4_put_fname_crypto_ctx(&ctx
);
1857 ext4_fname_crypto_free_buffer(&fname_crypto_str
);
1861 int ext4_insert_dentry(struct inode
*dir
,
1862 struct inode
*inode
,
1863 struct ext4_dir_entry_2
*de
,
1865 const struct qstr
*iname
,
1866 const char *name
, int namelen
)
1870 struct ext4_fname_crypto_ctx
*ctx
= NULL
;
1871 struct ext4_str fname_crypto_str
= {.name
= NULL
, .len
= 0};
1872 struct ext4_str tmp_str
;
1875 ctx
= ext4_get_fname_crypto_ctx(dir
, EXT4_NAME_LEN
);
1878 /* By default, the input name would be written to the disk */
1879 tmp_str
.name
= (unsigned char *)name
;
1880 tmp_str
.len
= namelen
;
1882 /* Directory is encrypted */
1883 res
= ext4_fname_crypto_alloc_buffer(ctx
, EXT4_NAME_LEN
,
1886 ext4_put_fname_crypto_ctx(&ctx
);
1889 res
= ext4_fname_usr_to_disk(ctx
, iname
, &fname_crypto_str
);
1891 ext4_put_fname_crypto_ctx(&ctx
);
1892 ext4_fname_crypto_free_buffer(&fname_crypto_str
);
1895 tmp_str
.name
= fname_crypto_str
.name
;
1896 tmp_str
.len
= fname_crypto_str
.len
;
1899 nlen
= EXT4_DIR_REC_LEN(de
->name_len
);
1900 rlen
= ext4_rec_len_from_disk(de
->rec_len
, buf_size
);
1902 struct ext4_dir_entry_2
*de1
=
1903 (struct ext4_dir_entry_2
*)((char *)de
+ nlen
);
1904 de1
->rec_len
= ext4_rec_len_to_disk(rlen
- nlen
, buf_size
);
1905 de
->rec_len
= ext4_rec_len_to_disk(nlen
, buf_size
);
1908 de
->file_type
= EXT4_FT_UNKNOWN
;
1909 de
->inode
= cpu_to_le32(inode
->i_ino
);
1910 ext4_set_de_type(inode
->i_sb
, de
, inode
->i_mode
);
1911 de
->name_len
= tmp_str
.len
;
1913 memcpy(de
->name
, tmp_str
.name
, tmp_str
.len
);
1914 ext4_put_fname_crypto_ctx(&ctx
);
1915 ext4_fname_crypto_free_buffer(&fname_crypto_str
);
1920 * Add a new entry into a directory (leaf) block. If de is non-NULL,
1921 * it points to a directory entry which is guaranteed to be large
1922 * enough for new directory entry. If de is NULL, then
1923 * add_dirent_to_buf will attempt search the directory block for
1924 * space. It will return -ENOSPC if no space is available, and -EIO
1925 * and -EEXIST if directory entry already exists.
1927 static int add_dirent_to_buf(handle_t
*handle
, struct dentry
*dentry
,
1928 struct inode
*inode
, struct ext4_dir_entry_2
*de
,
1929 struct buffer_head
*bh
)
1931 struct inode
*dir
= d_inode(dentry
->d_parent
);
1932 const char *name
= dentry
->d_name
.name
;
1933 int namelen
= dentry
->d_name
.len
;
1934 unsigned int blocksize
= dir
->i_sb
->s_blocksize
;
1938 if (ext4_has_metadata_csum(inode
->i_sb
))
1939 csum_size
= sizeof(struct ext4_dir_entry_tail
);
1942 err
= ext4_find_dest_de(dir
, inode
,
1943 bh
, bh
->b_data
, blocksize
- csum_size
,
1944 name
, namelen
, &de
);
1948 BUFFER_TRACE(bh
, "get_write_access");
1949 err
= ext4_journal_get_write_access(handle
, bh
);
1951 ext4_std_error(dir
->i_sb
, err
);
1955 /* By now the buffer is marked for journaling. Due to crypto operations,
1956 * the following function call may fail */
1957 err
= ext4_insert_dentry(dir
, inode
, de
, blocksize
, &dentry
->d_name
,
1963 * XXX shouldn't update any times until successful
1964 * completion of syscall, but too many callers depend
1967 * XXX similarly, too many callers depend on
1968 * ext4_new_inode() setting the times, but error
1969 * recovery deletes the inode, so the worst that can
1970 * happen is that the times are slightly out of date
1971 * and/or different from the directory change time.
1973 dir
->i_mtime
= dir
->i_ctime
= ext4_current_time(dir
);
1974 ext4_update_dx_flag(dir
);
1976 ext4_mark_inode_dirty(handle
, dir
);
1977 BUFFER_TRACE(bh
, "call ext4_handle_dirty_metadata");
1978 err
= ext4_handle_dirty_dirent_node(handle
, dir
, bh
);
1980 ext4_std_error(dir
->i_sb
, err
);
1985 * This converts a one block unindexed directory to a 3 block indexed
1986 * directory, and adds the dentry to the indexed directory.
1988 static int make_indexed_dir(handle_t
*handle
, struct dentry
*dentry
,
1989 struct inode
*inode
, struct buffer_head
*bh
)
1991 struct inode
*dir
= d_inode(dentry
->d_parent
);
1992 #ifdef CONFIG_EXT4_FS_ENCRYPTION
1993 struct ext4_fname_crypto_ctx
*ctx
= NULL
;
1996 const char *name
= dentry
->d_name
.name
;
1997 int namelen
= dentry
->d_name
.len
;
1999 struct buffer_head
*bh2
;
2000 struct dx_root
*root
;
2001 struct dx_frame frames
[2], *frame
;
2002 struct dx_entry
*entries
;
2003 struct ext4_dir_entry_2
*de
, *de2
;
2004 struct ext4_dir_entry_tail
*t
;
2009 struct dx_hash_info hinfo
;
2011 struct fake_dirent
*fde
;
2014 #ifdef CONFIG_EXT4_FS_ENCRYPTION
2015 ctx
= ext4_get_fname_crypto_ctx(dir
, EXT4_NAME_LEN
);
2017 return PTR_ERR(ctx
);
2020 if (ext4_has_metadata_csum(inode
->i_sb
))
2021 csum_size
= sizeof(struct ext4_dir_entry_tail
);
2023 blocksize
= dir
->i_sb
->s_blocksize
;
2024 dxtrace(printk(KERN_DEBUG
"Creating index: inode %lu\n", dir
->i_ino
));
2025 BUFFER_TRACE(bh
, "get_write_access");
2026 retval
= ext4_journal_get_write_access(handle
, bh
);
2028 ext4_std_error(dir
->i_sb
, retval
);
2032 root
= (struct dx_root
*) bh
->b_data
;
2034 /* The 0th block becomes the root, move the dirents out */
2035 fde
= &root
->dotdot
;
2036 de
= (struct ext4_dir_entry_2
*)((char *)fde
+
2037 ext4_rec_len_from_disk(fde
->rec_len
, blocksize
));
2038 if ((char *) de
>= (((char *) root
) + blocksize
)) {
2039 EXT4_ERROR_INODE(dir
, "invalid rec_len for '..'");
2043 len
= ((char *) root
) + (blocksize
- csum_size
) - (char *) de
;
2045 /* Allocate new block for the 0th block's dirents */
2046 bh2
= ext4_append(handle
, dir
, &block
);
2049 return PTR_ERR(bh2
);
2051 ext4_set_inode_flag(dir
, EXT4_INODE_INDEX
);
2052 data1
= bh2
->b_data
;
2054 memcpy (data1
, de
, len
);
2055 de
= (struct ext4_dir_entry_2
*) data1
;
2057 while ((char *)(de2
= ext4_next_entry(de
, blocksize
)) < top
)
2059 de
->rec_len
= ext4_rec_len_to_disk(data1
+ (blocksize
- csum_size
) -
2064 t
= EXT4_DIRENT_TAIL(data1
, blocksize
);
2065 initialize_dirent_tail(t
, blocksize
);
2068 /* Initialize the root; the dot dirents already exist */
2069 de
= (struct ext4_dir_entry_2
*) (&root
->dotdot
);
2070 de
->rec_len
= ext4_rec_len_to_disk(blocksize
- EXT4_DIR_REC_LEN(2),
2072 memset (&root
->info
, 0, sizeof(root
->info
));
2073 root
->info
.info_length
= sizeof(root
->info
);
2074 root
->info
.hash_version
= EXT4_SB(dir
->i_sb
)->s_def_hash_version
;
2075 entries
= root
->entries
;
2076 dx_set_block(entries
, 1);
2077 dx_set_count(entries
, 1);
2078 dx_set_limit(entries
, dx_root_limit(dir
, sizeof(root
->info
)));
2080 /* Initialize as for dx_probe */
2081 hinfo
.hash_version
= root
->info
.hash_version
;
2082 if (hinfo
.hash_version
<= DX_HASH_TEA
)
2083 hinfo
.hash_version
+= EXT4_SB(dir
->i_sb
)->s_hash_unsigned
;
2084 hinfo
.seed
= EXT4_SB(dir
->i_sb
)->s_hash_seed
;
2085 #ifdef CONFIG_EXT4_FS_ENCRYPTION
2086 res
= ext4_fname_usr_to_hash(ctx
, &dentry
->d_name
, &hinfo
);
2088 ext4_put_fname_crypto_ctx(&ctx
);
2089 ext4_mark_inode_dirty(handle
, dir
);
2093 ext4_put_fname_crypto_ctx(&ctx
);
2095 ext4fs_dirhash(name
, namelen
, &hinfo
);
2097 memset(frames
, 0, sizeof(frames
));
2099 frame
->entries
= entries
;
2100 frame
->at
= entries
;
2104 retval
= ext4_handle_dirty_dx_node(handle
, dir
, frame
->bh
);
2107 retval
= ext4_handle_dirty_dirent_node(handle
, dir
, bh
);
2111 de
= do_split(handle
,dir
, &bh
, frame
, &hinfo
);
2113 retval
= PTR_ERR(de
);
2118 retval
= add_dirent_to_buf(handle
, dentry
, inode
, de
, bh
);
2123 * Even if the block split failed, we have to properly write
2124 * out all the changes we did so far. Otherwise we can end up
2125 * with corrupted filesystem.
2127 ext4_mark_inode_dirty(handle
, dir
);
2135 * adds a file entry to the specified directory, using the same
2136 * semantics as ext4_find_entry(). It returns NULL if it failed.
2138 * NOTE!! The inode part of 'de' is left at 0 - which means you
2139 * may not sleep between calling this and putting something into
2140 * the entry, as someone else might have used it while you slept.
2142 static int ext4_add_entry(handle_t
*handle
, struct dentry
*dentry
,
2143 struct inode
*inode
)
2145 struct inode
*dir
= d_inode(dentry
->d_parent
);
2146 struct buffer_head
*bh
= NULL
;
2147 struct ext4_dir_entry_2
*de
;
2148 struct ext4_dir_entry_tail
*t
;
2149 struct super_block
*sb
;
2153 ext4_lblk_t block
, blocks
;
2156 if (ext4_has_metadata_csum(inode
->i_sb
))
2157 csum_size
= sizeof(struct ext4_dir_entry_tail
);
2160 blocksize
= sb
->s_blocksize
;
2161 if (!dentry
->d_name
.len
)
2164 if (ext4_has_inline_data(dir
)) {
2165 retval
= ext4_try_add_inline_entry(handle
, dentry
, inode
);
2175 retval
= ext4_dx_add_entry(handle
, dentry
, inode
);
2176 if (!retval
|| (retval
!= ERR_BAD_DX_DIR
))
2178 ext4_clear_inode_flag(dir
, EXT4_INODE_INDEX
);
2180 ext4_mark_inode_dirty(handle
, dir
);
2182 blocks
= dir
->i_size
>> sb
->s_blocksize_bits
;
2183 for (block
= 0; block
< blocks
; block
++) {
2184 bh
= ext4_read_dirblock(dir
, block
, DIRENT
);
2188 retval
= add_dirent_to_buf(handle
, dentry
, inode
, NULL
, bh
);
2189 if (retval
!= -ENOSPC
)
2192 if (blocks
== 1 && !dx_fallback
&&
2193 EXT4_HAS_COMPAT_FEATURE(sb
, EXT4_FEATURE_COMPAT_DIR_INDEX
)) {
2194 retval
= make_indexed_dir(handle
, dentry
, inode
, bh
);
2195 bh
= NULL
; /* make_indexed_dir releases bh */
2200 bh
= ext4_append(handle
, dir
, &block
);
2203 de
= (struct ext4_dir_entry_2
*) bh
->b_data
;
2205 de
->rec_len
= ext4_rec_len_to_disk(blocksize
- csum_size
, blocksize
);
2208 t
= EXT4_DIRENT_TAIL(bh
->b_data
, blocksize
);
2209 initialize_dirent_tail(t
, blocksize
);
2212 retval
= add_dirent_to_buf(handle
, dentry
, inode
, de
, bh
);
2216 ext4_set_inode_state(inode
, EXT4_STATE_NEWENTRY
);
2221 * Returns 0 for success, or a negative error value
2223 static int ext4_dx_add_entry(handle_t
*handle
, struct dentry
*dentry
,
2224 struct inode
*inode
)
2226 struct dx_frame frames
[2], *frame
;
2227 struct dx_entry
*entries
, *at
;
2228 struct dx_hash_info hinfo
;
2229 struct buffer_head
*bh
;
2230 struct inode
*dir
= d_inode(dentry
->d_parent
);
2231 struct super_block
*sb
= dir
->i_sb
;
2232 struct ext4_dir_entry_2
*de
;
2235 frame
= dx_probe(&dentry
->d_name
, dir
, &hinfo
, frames
);
2237 return PTR_ERR(frame
);
2238 entries
= frame
->entries
;
2240 bh
= ext4_read_dirblock(dir
, dx_get_block(frame
->at
), DIRENT
);
2247 BUFFER_TRACE(bh
, "get_write_access");
2248 err
= ext4_journal_get_write_access(handle
, bh
);
2252 err
= add_dirent_to_buf(handle
, dentry
, inode
, NULL
, bh
);
2256 /* Block full, should compress but for now just split */
2257 dxtrace(printk(KERN_DEBUG
"using %u of %u node entries\n",
2258 dx_get_count(entries
), dx_get_limit(entries
)));
2259 /* Need to split index? */
2260 if (dx_get_count(entries
) == dx_get_limit(entries
)) {
2261 ext4_lblk_t newblock
;
2262 unsigned icount
= dx_get_count(entries
);
2263 int levels
= frame
- frames
;
2264 struct dx_entry
*entries2
;
2265 struct dx_node
*node2
;
2266 struct buffer_head
*bh2
;
2268 if (levels
&& (dx_get_count(frames
->entries
) ==
2269 dx_get_limit(frames
->entries
))) {
2270 ext4_warning(sb
, "Directory index full!");
2274 bh2
= ext4_append(handle
, dir
, &newblock
);
2279 node2
= (struct dx_node
*)(bh2
->b_data
);
2280 entries2
= node2
->entries
;
2281 memset(&node2
->fake
, 0, sizeof(struct fake_dirent
));
2282 node2
->fake
.rec_len
= ext4_rec_len_to_disk(sb
->s_blocksize
,
2284 BUFFER_TRACE(frame
->bh
, "get_write_access");
2285 err
= ext4_journal_get_write_access(handle
, frame
->bh
);
2289 unsigned icount1
= icount
/2, icount2
= icount
- icount1
;
2290 unsigned hash2
= dx_get_hash(entries
+ icount1
);
2291 dxtrace(printk(KERN_DEBUG
"Split index %i/%i\n",
2294 BUFFER_TRACE(frame
->bh
, "get_write_access"); /* index root */
2295 err
= ext4_journal_get_write_access(handle
,
2300 memcpy((char *) entries2
, (char *) (entries
+ icount1
),
2301 icount2
* sizeof(struct dx_entry
));
2302 dx_set_count(entries
, icount1
);
2303 dx_set_count(entries2
, icount2
);
2304 dx_set_limit(entries2
, dx_node_limit(dir
));
2306 /* Which index block gets the new entry? */
2307 if (at
- entries
>= icount1
) {
2308 frame
->at
= at
= at
- entries
- icount1
+ entries2
;
2309 frame
->entries
= entries
= entries2
;
2310 swap(frame
->bh
, bh2
);
2312 dx_insert_block(frames
+ 0, hash2
, newblock
);
2313 dxtrace(dx_show_index("node", frames
[1].entries
));
2314 dxtrace(dx_show_index("node",
2315 ((struct dx_node
*) bh2
->b_data
)->entries
));
2316 err
= ext4_handle_dirty_dx_node(handle
, dir
, bh2
);
2321 dxtrace(printk(KERN_DEBUG
2322 "Creating second level index...\n"));
2323 memcpy((char *) entries2
, (char *) entries
,
2324 icount
* sizeof(struct dx_entry
));
2325 dx_set_limit(entries2
, dx_node_limit(dir
));
2328 dx_set_count(entries
, 1);
2329 dx_set_block(entries
+ 0, newblock
);
2330 ((struct dx_root
*) frames
[0].bh
->b_data
)->info
.indirect_levels
= 1;
2332 /* Add new access path frame */
2334 frame
->at
= at
= at
- entries
+ entries2
;
2335 frame
->entries
= entries
= entries2
;
2337 err
= ext4_journal_get_write_access(handle
,
2342 err
= ext4_handle_dirty_dx_node(handle
, dir
, frames
[0].bh
);
2344 ext4_std_error(inode
->i_sb
, err
);
2348 de
= do_split(handle
, dir
, &bh
, frame
, &hinfo
);
2353 err
= add_dirent_to_buf(handle
, dentry
, inode
, de
, bh
);
2357 ext4_std_error(dir
->i_sb
, err
);
2365 * ext4_generic_delete_entry deletes a directory entry by merging it
2366 * with the previous entry
2368 int ext4_generic_delete_entry(handle_t
*handle
,
2370 struct ext4_dir_entry_2
*de_del
,
2371 struct buffer_head
*bh
,
2376 struct ext4_dir_entry_2
*de
, *pde
;
2377 unsigned int blocksize
= dir
->i_sb
->s_blocksize
;
2382 de
= (struct ext4_dir_entry_2
*)entry_buf
;
2383 while (i
< buf_size
- csum_size
) {
2384 if (ext4_check_dir_entry(dir
, NULL
, de
, bh
,
2385 bh
->b_data
, bh
->b_size
, i
))
2389 pde
->rec_len
= ext4_rec_len_to_disk(
2390 ext4_rec_len_from_disk(pde
->rec_len
,
2392 ext4_rec_len_from_disk(de
->rec_len
,
2400 i
+= ext4_rec_len_from_disk(de
->rec_len
, blocksize
);
2402 de
= ext4_next_entry(de
, blocksize
);
2407 static int ext4_delete_entry(handle_t
*handle
,
2409 struct ext4_dir_entry_2
*de_del
,
2410 struct buffer_head
*bh
)
2412 int err
, csum_size
= 0;
2414 if (ext4_has_inline_data(dir
)) {
2415 int has_inline_data
= 1;
2416 err
= ext4_delete_inline_entry(handle
, dir
, de_del
, bh
,
2418 if (has_inline_data
)
2422 if (ext4_has_metadata_csum(dir
->i_sb
))
2423 csum_size
= sizeof(struct ext4_dir_entry_tail
);
2425 BUFFER_TRACE(bh
, "get_write_access");
2426 err
= ext4_journal_get_write_access(handle
, bh
);
2430 err
= ext4_generic_delete_entry(handle
, dir
, de_del
,
2432 dir
->i_sb
->s_blocksize
, csum_size
);
2436 BUFFER_TRACE(bh
, "call ext4_handle_dirty_metadata");
2437 err
= ext4_handle_dirty_dirent_node(handle
, dir
, bh
);
2444 ext4_std_error(dir
->i_sb
, err
);
2449 * DIR_NLINK feature is set if 1) nlinks > EXT4_LINK_MAX or 2) nlinks == 2,
2450 * since this indicates that nlinks count was previously 1.
2452 static void ext4_inc_count(handle_t
*handle
, struct inode
*inode
)
2455 if (is_dx(inode
) && inode
->i_nlink
> 1) {
2456 /* limit is 16-bit i_links_count */
2457 if (inode
->i_nlink
>= EXT4_LINK_MAX
|| inode
->i_nlink
== 2) {
2458 set_nlink(inode
, 1);
2459 EXT4_SET_RO_COMPAT_FEATURE(inode
->i_sb
,
2460 EXT4_FEATURE_RO_COMPAT_DIR_NLINK
);
2466 * If a directory had nlink == 1, then we should let it be 1. This indicates
2467 * directory has >EXT4_LINK_MAX subdirs.
2469 static void ext4_dec_count(handle_t
*handle
, struct inode
*inode
)
2471 if (!S_ISDIR(inode
->i_mode
) || inode
->i_nlink
> 2)
2476 static int ext4_add_nondir(handle_t
*handle
,
2477 struct dentry
*dentry
, struct inode
*inode
)
2479 int err
= ext4_add_entry(handle
, dentry
, inode
);
2481 ext4_mark_inode_dirty(handle
, inode
);
2482 unlock_new_inode(inode
);
2483 d_instantiate(dentry
, inode
);
2487 unlock_new_inode(inode
);
2493 * By the time this is called, we already have created
2494 * the directory cache entry for the new file, but it
2495 * is so far negative - it has no inode.
2497 * If the create succeeds, we fill in the inode information
2498 * with d_instantiate().
2500 static int ext4_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
2504 struct inode
*inode
;
2505 int err
, credits
, retries
= 0;
2507 dquot_initialize(dir
);
2509 credits
= (EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
2510 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 3);
2512 inode
= ext4_new_inode_start_handle(dir
, mode
, &dentry
->d_name
, 0,
2513 NULL
, EXT4_HT_DIR
, credits
);
2514 handle
= ext4_journal_current_handle();
2515 err
= PTR_ERR(inode
);
2516 if (!IS_ERR(inode
)) {
2517 inode
->i_op
= &ext4_file_inode_operations
;
2518 inode
->i_fop
= &ext4_file_operations
;
2519 ext4_set_aops(inode
);
2521 #ifdef CONFIG_EXT4_FS_ENCRYPTION
2522 if (!err
&& (ext4_encrypted_inode(dir
) ||
2523 DUMMY_ENCRYPTION_ENABLED(EXT4_SB(dir
->i_sb
)))) {
2524 err
= ext4_inherit_context(dir
, inode
);
2527 unlock_new_inode(inode
);
2533 err
= ext4_add_nondir(handle
, dentry
, inode
);
2534 if (!err
&& IS_DIRSYNC(dir
))
2535 ext4_handle_sync(handle
);
2538 ext4_journal_stop(handle
);
2539 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
2544 static int ext4_mknod(struct inode
*dir
, struct dentry
*dentry
,
2545 umode_t mode
, dev_t rdev
)
2548 struct inode
*inode
;
2549 int err
, credits
, retries
= 0;
2551 if (!new_valid_dev(rdev
))
2554 dquot_initialize(dir
);
2556 credits
= (EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
2557 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 3);
2559 inode
= ext4_new_inode_start_handle(dir
, mode
, &dentry
->d_name
, 0,
2560 NULL
, EXT4_HT_DIR
, credits
);
2561 handle
= ext4_journal_current_handle();
2562 err
= PTR_ERR(inode
);
2563 if (!IS_ERR(inode
)) {
2564 init_special_inode(inode
, inode
->i_mode
, rdev
);
2565 inode
->i_op
= &ext4_special_inode_operations
;
2566 err
= ext4_add_nondir(handle
, dentry
, inode
);
2567 if (!err
&& IS_DIRSYNC(dir
))
2568 ext4_handle_sync(handle
);
2571 ext4_journal_stop(handle
);
2572 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
2577 static int ext4_tmpfile(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
2580 struct inode
*inode
;
2581 int err
, retries
= 0;
2583 dquot_initialize(dir
);
2586 inode
= ext4_new_inode_start_handle(dir
, mode
,
2589 EXT4_MAXQUOTAS_INIT_BLOCKS(dir
->i_sb
) +
2590 4 + EXT4_XATTR_TRANS_BLOCKS
);
2591 handle
= ext4_journal_current_handle();
2592 err
= PTR_ERR(inode
);
2593 if (!IS_ERR(inode
)) {
2594 inode
->i_op
= &ext4_file_inode_operations
;
2595 inode
->i_fop
= &ext4_file_operations
;
2596 ext4_set_aops(inode
);
2597 d_tmpfile(dentry
, inode
);
2598 err
= ext4_orphan_add(handle
, inode
);
2600 goto err_unlock_inode
;
2601 mark_inode_dirty(inode
);
2602 unlock_new_inode(inode
);
2605 ext4_journal_stop(handle
);
2606 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
2610 ext4_journal_stop(handle
);
2611 unlock_new_inode(inode
);
2615 struct ext4_dir_entry_2
*ext4_init_dot_dotdot(struct inode
*inode
,
2616 struct ext4_dir_entry_2
*de
,
2617 int blocksize
, int csum_size
,
2618 unsigned int parent_ino
, int dotdot_real_len
)
2620 de
->inode
= cpu_to_le32(inode
->i_ino
);
2622 de
->rec_len
= ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de
->name_len
),
2624 strcpy(de
->name
, ".");
2625 ext4_set_de_type(inode
->i_sb
, de
, S_IFDIR
);
2627 de
= ext4_next_entry(de
, blocksize
);
2628 de
->inode
= cpu_to_le32(parent_ino
);
2630 if (!dotdot_real_len
)
2631 de
->rec_len
= ext4_rec_len_to_disk(blocksize
-
2632 (csum_size
+ EXT4_DIR_REC_LEN(1)),
2635 de
->rec_len
= ext4_rec_len_to_disk(
2636 EXT4_DIR_REC_LEN(de
->name_len
), blocksize
);
2637 strcpy(de
->name
, "..");
2638 ext4_set_de_type(inode
->i_sb
, de
, S_IFDIR
);
2640 return ext4_next_entry(de
, blocksize
);
2643 static int ext4_init_new_dir(handle_t
*handle
, struct inode
*dir
,
2644 struct inode
*inode
)
2646 struct buffer_head
*dir_block
= NULL
;
2647 struct ext4_dir_entry_2
*de
;
2648 struct ext4_dir_entry_tail
*t
;
2649 ext4_lblk_t block
= 0;
2650 unsigned int blocksize
= dir
->i_sb
->s_blocksize
;
2654 if (ext4_has_metadata_csum(dir
->i_sb
))
2655 csum_size
= sizeof(struct ext4_dir_entry_tail
);
2657 if (ext4_test_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
)) {
2658 err
= ext4_try_create_inline_dir(handle
, dir
, inode
);
2659 if (err
< 0 && err
!= -ENOSPC
)
2666 dir_block
= ext4_append(handle
, inode
, &block
);
2667 if (IS_ERR(dir_block
))
2668 return PTR_ERR(dir_block
);
2669 de
= (struct ext4_dir_entry_2
*)dir_block
->b_data
;
2670 ext4_init_dot_dotdot(inode
, de
, blocksize
, csum_size
, dir
->i_ino
, 0);
2671 set_nlink(inode
, 2);
2673 t
= EXT4_DIRENT_TAIL(dir_block
->b_data
, blocksize
);
2674 initialize_dirent_tail(t
, blocksize
);
2677 BUFFER_TRACE(dir_block
, "call ext4_handle_dirty_metadata");
2678 err
= ext4_handle_dirty_dirent_node(handle
, inode
, dir_block
);
2681 set_buffer_verified(dir_block
);
2687 static int ext4_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
2690 struct inode
*inode
;
2691 int err
, credits
, retries
= 0;
2693 if (EXT4_DIR_LINK_MAX(dir
))
2696 dquot_initialize(dir
);
2698 credits
= (EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
2699 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 3);
2701 inode
= ext4_new_inode_start_handle(dir
, S_IFDIR
| mode
,
2703 0, NULL
, EXT4_HT_DIR
, credits
);
2704 handle
= ext4_journal_current_handle();
2705 err
= PTR_ERR(inode
);
2709 inode
->i_op
= &ext4_dir_inode_operations
;
2710 inode
->i_fop
= &ext4_dir_operations
;
2711 err
= ext4_init_new_dir(handle
, dir
, inode
);
2713 goto out_clear_inode
;
2714 #ifdef CONFIG_EXT4_FS_ENCRYPTION
2715 if (ext4_encrypted_inode(dir
) ||
2716 DUMMY_ENCRYPTION_ENABLED(EXT4_SB(dir
->i_sb
))) {
2717 err
= ext4_inherit_context(dir
, inode
);
2719 goto out_clear_inode
;
2722 err
= ext4_mark_inode_dirty(handle
, inode
);
2724 err
= ext4_add_entry(handle
, dentry
, inode
);
2728 unlock_new_inode(inode
);
2729 ext4_mark_inode_dirty(handle
, inode
);
2733 ext4_inc_count(handle
, dir
);
2734 ext4_update_dx_flag(dir
);
2735 err
= ext4_mark_inode_dirty(handle
, dir
);
2737 goto out_clear_inode
;
2738 unlock_new_inode(inode
);
2739 d_instantiate(dentry
, inode
);
2740 if (IS_DIRSYNC(dir
))
2741 ext4_handle_sync(handle
);
2745 ext4_journal_stop(handle
);
2746 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
2752 * routine to check that the specified directory is empty (for rmdir)
2754 int ext4_empty_dir(struct inode
*inode
)
2756 unsigned int offset
;
2757 struct buffer_head
*bh
;
2758 struct ext4_dir_entry_2
*de
, *de1
;
2759 struct super_block
*sb
;
2762 if (ext4_has_inline_data(inode
)) {
2763 int has_inline_data
= 1;
2765 err
= empty_inline_dir(inode
, &has_inline_data
);
2766 if (has_inline_data
)
2771 if (inode
->i_size
< EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2)) {
2772 EXT4_ERROR_INODE(inode
, "invalid size");
2775 bh
= ext4_read_dirblock(inode
, 0, EITHER
);
2779 de
= (struct ext4_dir_entry_2
*) bh
->b_data
;
2780 de1
= ext4_next_entry(de
, sb
->s_blocksize
);
2781 if (le32_to_cpu(de
->inode
) != inode
->i_ino
||
2782 !le32_to_cpu(de1
->inode
) ||
2783 strcmp(".", de
->name
) ||
2784 strcmp("..", de1
->name
)) {
2785 ext4_warning(inode
->i_sb
,
2786 "bad directory (dir #%lu) - no `.' or `..'",
2791 offset
= ext4_rec_len_from_disk(de
->rec_len
, sb
->s_blocksize
) +
2792 ext4_rec_len_from_disk(de1
->rec_len
, sb
->s_blocksize
);
2793 de
= ext4_next_entry(de1
, sb
->s_blocksize
);
2794 while (offset
< inode
->i_size
) {
2795 if ((void *) de
>= (void *) (bh
->b_data
+sb
->s_blocksize
)) {
2796 unsigned int lblock
;
2799 lblock
= offset
>> EXT4_BLOCK_SIZE_BITS(sb
);
2800 bh
= ext4_read_dirblock(inode
, lblock
, EITHER
);
2803 de
= (struct ext4_dir_entry_2
*) bh
->b_data
;
2805 if (ext4_check_dir_entry(inode
, NULL
, de
, bh
,
2806 bh
->b_data
, bh
->b_size
, offset
)) {
2807 de
= (struct ext4_dir_entry_2
*)(bh
->b_data
+
2809 offset
= (offset
| (sb
->s_blocksize
- 1)) + 1;
2812 if (le32_to_cpu(de
->inode
)) {
2816 offset
+= ext4_rec_len_from_disk(de
->rec_len
, sb
->s_blocksize
);
2817 de
= ext4_next_entry(de
, sb
->s_blocksize
);
2824 * ext4_orphan_add() links an unlinked or truncated inode into a list of
2825 * such inodes, starting at the superblock, in case we crash before the
2826 * file is closed/deleted, or in case the inode truncate spans multiple
2827 * transactions and the last transaction is not recovered after a crash.
2829 * At filesystem recovery time, we walk this list deleting unlinked
2830 * inodes and truncating linked inodes in ext4_orphan_cleanup().
2832 * Orphan list manipulation functions must be called under i_mutex unless
2833 * we are just creating the inode or deleting it.
2835 int ext4_orphan_add(handle_t
*handle
, struct inode
*inode
)
2837 struct super_block
*sb
= inode
->i_sb
;
2838 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2839 struct ext4_iloc iloc
;
2843 if (!sbi
->s_journal
|| is_bad_inode(inode
))
2846 WARN_ON_ONCE(!(inode
->i_state
& (I_NEW
| I_FREEING
)) &&
2847 !mutex_is_locked(&inode
->i_mutex
));
2849 * Exit early if inode already is on orphan list. This is a big speedup
2850 * since we don't have to contend on the global s_orphan_lock.
2852 if (!list_empty(&EXT4_I(inode
)->i_orphan
))
2856 * Orphan handling is only valid for files with data blocks
2857 * being truncated, or files being unlinked. Note that we either
2858 * hold i_mutex, or the inode can not be referenced from outside,
2859 * so i_nlink should not be bumped due to race
2861 J_ASSERT((S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
2862 S_ISLNK(inode
->i_mode
)) || inode
->i_nlink
== 0);
2864 BUFFER_TRACE(sbi
->s_sbh
, "get_write_access");
2865 err
= ext4_journal_get_write_access(handle
, sbi
->s_sbh
);
2869 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
2873 mutex_lock(&sbi
->s_orphan_lock
);
2875 * Due to previous errors inode may be already a part of on-disk
2876 * orphan list. If so skip on-disk list modification.
2878 if (!NEXT_ORPHAN(inode
) || NEXT_ORPHAN(inode
) >
2879 (le32_to_cpu(sbi
->s_es
->s_inodes_count
))) {
2880 /* Insert this inode at the head of the on-disk orphan list */
2881 NEXT_ORPHAN(inode
) = le32_to_cpu(sbi
->s_es
->s_last_orphan
);
2882 sbi
->s_es
->s_last_orphan
= cpu_to_le32(inode
->i_ino
);
2885 list_add(&EXT4_I(inode
)->i_orphan
, &sbi
->s_orphan
);
2886 mutex_unlock(&sbi
->s_orphan_lock
);
2889 err
= ext4_handle_dirty_super(handle
, sb
);
2890 rc
= ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
2895 * We have to remove inode from in-memory list if
2896 * addition to on disk orphan list failed. Stray orphan
2897 * list entries can cause panics at unmount time.
2899 mutex_lock(&sbi
->s_orphan_lock
);
2900 list_del(&EXT4_I(inode
)->i_orphan
);
2901 mutex_unlock(&sbi
->s_orphan_lock
);
2904 jbd_debug(4, "superblock will point to %lu\n", inode
->i_ino
);
2905 jbd_debug(4, "orphan inode %lu will point to %d\n",
2906 inode
->i_ino
, NEXT_ORPHAN(inode
));
2908 ext4_std_error(sb
, err
);
2913 * ext4_orphan_del() removes an unlinked or truncated inode from the list
2914 * of such inodes stored on disk, because it is finally being cleaned up.
2916 int ext4_orphan_del(handle_t
*handle
, struct inode
*inode
)
2918 struct list_head
*prev
;
2919 struct ext4_inode_info
*ei
= EXT4_I(inode
);
2920 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
2922 struct ext4_iloc iloc
;
2925 if (!sbi
->s_journal
&& !(sbi
->s_mount_state
& EXT4_ORPHAN_FS
))
2928 WARN_ON_ONCE(!(inode
->i_state
& (I_NEW
| I_FREEING
)) &&
2929 !mutex_is_locked(&inode
->i_mutex
));
2930 /* Do this quick check before taking global s_orphan_lock. */
2931 if (list_empty(&ei
->i_orphan
))
2935 /* Grab inode buffer early before taking global s_orphan_lock */
2936 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
2939 mutex_lock(&sbi
->s_orphan_lock
);
2940 jbd_debug(4, "remove inode %lu from orphan list\n", inode
->i_ino
);
2942 prev
= ei
->i_orphan
.prev
;
2943 list_del_init(&ei
->i_orphan
);
2945 /* If we're on an error path, we may not have a valid
2946 * transaction handle with which to update the orphan list on
2947 * disk, but we still need to remove the inode from the linked
2948 * list in memory. */
2949 if (!handle
|| err
) {
2950 mutex_unlock(&sbi
->s_orphan_lock
);
2954 ino_next
= NEXT_ORPHAN(inode
);
2955 if (prev
== &sbi
->s_orphan
) {
2956 jbd_debug(4, "superblock will point to %u\n", ino_next
);
2957 BUFFER_TRACE(sbi
->s_sbh
, "get_write_access");
2958 err
= ext4_journal_get_write_access(handle
, sbi
->s_sbh
);
2960 mutex_unlock(&sbi
->s_orphan_lock
);
2963 sbi
->s_es
->s_last_orphan
= cpu_to_le32(ino_next
);
2964 mutex_unlock(&sbi
->s_orphan_lock
);
2965 err
= ext4_handle_dirty_super(handle
, inode
->i_sb
);
2967 struct ext4_iloc iloc2
;
2968 struct inode
*i_prev
=
2969 &list_entry(prev
, struct ext4_inode_info
, i_orphan
)->vfs_inode
;
2971 jbd_debug(4, "orphan inode %lu will point to %u\n",
2972 i_prev
->i_ino
, ino_next
);
2973 err
= ext4_reserve_inode_write(handle
, i_prev
, &iloc2
);
2975 mutex_unlock(&sbi
->s_orphan_lock
);
2978 NEXT_ORPHAN(i_prev
) = ino_next
;
2979 err
= ext4_mark_iloc_dirty(handle
, i_prev
, &iloc2
);
2980 mutex_unlock(&sbi
->s_orphan_lock
);
2984 NEXT_ORPHAN(inode
) = 0;
2985 err
= ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
2987 ext4_std_error(inode
->i_sb
, err
);
2995 static int ext4_rmdir(struct inode
*dir
, struct dentry
*dentry
)
2998 struct inode
*inode
;
2999 struct buffer_head
*bh
;
3000 struct ext4_dir_entry_2
*de
;
3001 handle_t
*handle
= NULL
;
3003 /* Initialize quotas before so that eventual writes go in
3004 * separate transaction */
3005 dquot_initialize(dir
);
3006 dquot_initialize(d_inode(dentry
));
3009 bh
= ext4_find_entry(dir
, &dentry
->d_name
, &de
, NULL
);
3015 inode
= d_inode(dentry
);
3018 if (le32_to_cpu(de
->inode
) != inode
->i_ino
)
3021 retval
= -ENOTEMPTY
;
3022 if (!ext4_empty_dir(inode
))
3025 handle
= ext4_journal_start(dir
, EXT4_HT_DIR
,
3026 EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
));
3027 if (IS_ERR(handle
)) {
3028 retval
= PTR_ERR(handle
);
3033 if (IS_DIRSYNC(dir
))
3034 ext4_handle_sync(handle
);
3036 retval
= ext4_delete_entry(handle
, dir
, de
, bh
);
3039 if (!EXT4_DIR_LINK_EMPTY(inode
))
3040 ext4_warning(inode
->i_sb
,
3041 "empty directory has too many links (%d)",
3045 /* There's no need to set i_disksize: the fact that i_nlink is
3046 * zero will ensure that the right thing happens during any
3049 ext4_orphan_add(handle
, inode
);
3050 inode
->i_ctime
= dir
->i_ctime
= dir
->i_mtime
= ext4_current_time(inode
);
3051 ext4_mark_inode_dirty(handle
, inode
);
3052 ext4_dec_count(handle
, dir
);
3053 ext4_update_dx_flag(dir
);
3054 ext4_mark_inode_dirty(handle
, dir
);
3059 ext4_journal_stop(handle
);
3063 static int ext4_unlink(struct inode
*dir
, struct dentry
*dentry
)
3066 struct inode
*inode
;
3067 struct buffer_head
*bh
;
3068 struct ext4_dir_entry_2
*de
;
3069 handle_t
*handle
= NULL
;
3071 trace_ext4_unlink_enter(dir
, dentry
);
3072 /* Initialize quotas before so that eventual writes go
3073 * in separate transaction */
3074 dquot_initialize(dir
);
3075 dquot_initialize(d_inode(dentry
));
3078 bh
= ext4_find_entry(dir
, &dentry
->d_name
, &de
, NULL
);
3084 inode
= d_inode(dentry
);
3087 if (le32_to_cpu(de
->inode
) != inode
->i_ino
)
3090 handle
= ext4_journal_start(dir
, EXT4_HT_DIR
,
3091 EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
));
3092 if (IS_ERR(handle
)) {
3093 retval
= PTR_ERR(handle
);
3098 if (IS_DIRSYNC(dir
))
3099 ext4_handle_sync(handle
);
3101 if (!inode
->i_nlink
) {
3102 ext4_warning(inode
->i_sb
,
3103 "Deleting nonexistent file (%lu), %d",
3104 inode
->i_ino
, inode
->i_nlink
);
3105 set_nlink(inode
, 1);
3107 retval
= ext4_delete_entry(handle
, dir
, de
, bh
);
3110 dir
->i_ctime
= dir
->i_mtime
= ext4_current_time(dir
);
3111 ext4_update_dx_flag(dir
);
3112 ext4_mark_inode_dirty(handle
, dir
);
3114 if (!inode
->i_nlink
)
3115 ext4_orphan_add(handle
, inode
);
3116 inode
->i_ctime
= ext4_current_time(inode
);
3117 ext4_mark_inode_dirty(handle
, inode
);
3122 ext4_journal_stop(handle
);
3123 trace_ext4_unlink_exit(dentry
, retval
);
3127 static int ext4_symlink(struct inode
*dir
,
3128 struct dentry
*dentry
, const char *symname
)
3131 struct inode
*inode
;
3132 int err
, len
= strlen(symname
);
3134 bool encryption_required
;
3135 struct ext4_str disk_link
;
3136 struct ext4_encrypted_symlink_data
*sd
= NULL
;
3138 disk_link
.len
= len
+ 1;
3139 disk_link
.name
= (char *) symname
;
3141 encryption_required
= (ext4_encrypted_inode(dir
) ||
3142 DUMMY_ENCRYPTION_ENABLED(EXT4_SB(dir
->i_sb
)));
3143 if (encryption_required
)
3144 disk_link
.len
= encrypted_symlink_data_len(len
) + 1;
3145 if (disk_link
.len
> dir
->i_sb
->s_blocksize
)
3146 return -ENAMETOOLONG
;
3148 dquot_initialize(dir
);
3150 if ((disk_link
.len
> EXT4_N_BLOCKS
* 4)) {
3152 * For non-fast symlinks, we just allocate inode and put it on
3153 * orphan list in the first transaction => we need bitmap,
3154 * group descriptor, sb, inode block, quota blocks, and
3155 * possibly selinux xattr blocks.
3157 credits
= 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir
->i_sb
) +
3158 EXT4_XATTR_TRANS_BLOCKS
;
3161 * Fast symlink. We have to add entry to directory
3162 * (EXT4_DATA_TRANS_BLOCKS + EXT4_INDEX_EXTRA_TRANS_BLOCKS),
3163 * allocate new inode (bitmap, group descriptor, inode block,
3164 * quota blocks, sb is already counted in previous macros).
3166 credits
= EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
3167 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 3;
3170 inode
= ext4_new_inode_start_handle(dir
, S_IFLNK
|S_IRWXUGO
,
3171 &dentry
->d_name
, 0, NULL
,
3172 EXT4_HT_DIR
, credits
);
3173 handle
= ext4_journal_current_handle();
3174 if (IS_ERR(inode
)) {
3176 ext4_journal_stop(handle
);
3177 return PTR_ERR(inode
);
3180 if (encryption_required
) {
3181 struct ext4_fname_crypto_ctx
*ctx
= NULL
;
3183 struct ext4_str ostr
;
3185 sd
= kzalloc(disk_link
.len
, GFP_NOFS
);
3188 goto err_drop_inode
;
3190 err
= ext4_inherit_context(dir
, inode
);
3192 goto err_drop_inode
;
3193 ctx
= ext4_get_fname_crypto_ctx(inode
,
3194 inode
->i_sb
->s_blocksize
);
3195 if (IS_ERR_OR_NULL(ctx
)) {
3196 /* We just set the policy, so ctx should not be NULL */
3197 err
= (ctx
== NULL
) ? -EIO
: PTR_ERR(ctx
);
3198 goto err_drop_inode
;
3200 istr
.name
= (const unsigned char *) symname
;
3202 ostr
.name
= sd
->encrypted_path
;
3203 err
= ext4_fname_usr_to_disk(ctx
, &istr
, &ostr
);
3204 ext4_put_fname_crypto_ctx(&ctx
);
3206 goto err_drop_inode
;
3207 sd
->len
= cpu_to_le16(ostr
.len
);
3208 disk_link
.name
= (char *) sd
;
3209 inode
->i_op
= &ext4_encrypted_symlink_inode_operations
;
3212 if ((disk_link
.len
> EXT4_N_BLOCKS
* 4)) {
3213 if (!encryption_required
)
3214 inode
->i_op
= &ext4_symlink_inode_operations
;
3215 ext4_set_aops(inode
);
3217 * We cannot call page_symlink() with transaction started
3218 * because it calls into ext4_write_begin() which can wait
3219 * for transaction commit if we are running out of space
3220 * and thus we deadlock. So we have to stop transaction now
3221 * and restart it when symlink contents is written.
3223 * To keep fs consistent in case of crash, we have to put inode
3224 * to orphan list in the mean time.
3227 err
= ext4_orphan_add(handle
, inode
);
3228 ext4_journal_stop(handle
);
3231 goto err_drop_inode
;
3232 err
= __page_symlink(inode
, disk_link
.name
, disk_link
.len
, 1);
3234 goto err_drop_inode
;
3236 * Now inode is being linked into dir (EXT4_DATA_TRANS_BLOCKS
3237 * + EXT4_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
3239 handle
= ext4_journal_start(dir
, EXT4_HT_DIR
,
3240 EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
3241 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 1);
3242 if (IS_ERR(handle
)) {
3243 err
= PTR_ERR(handle
);
3245 goto err_drop_inode
;
3247 set_nlink(inode
, 1);
3248 err
= ext4_orphan_del(handle
, inode
);
3250 goto err_drop_inode
;
3252 /* clear the extent format for fast symlink */
3253 ext4_clear_inode_flag(inode
, EXT4_INODE_EXTENTS
);
3254 if (!encryption_required
) {
3255 inode
->i_op
= &ext4_fast_symlink_inode_operations
;
3256 inode
->i_link
= (char *)&EXT4_I(inode
)->i_data
;
3258 memcpy((char *)&EXT4_I(inode
)->i_data
, disk_link
.name
,
3260 inode
->i_size
= disk_link
.len
- 1;
3262 EXT4_I(inode
)->i_disksize
= inode
->i_size
;
3263 err
= ext4_add_nondir(handle
, dentry
, inode
);
3264 if (!err
&& IS_DIRSYNC(dir
))
3265 ext4_handle_sync(handle
);
3268 ext4_journal_stop(handle
);
3273 ext4_journal_stop(handle
);
3276 unlock_new_inode(inode
);
3281 static int ext4_link(struct dentry
*old_dentry
,
3282 struct inode
*dir
, struct dentry
*dentry
)
3285 struct inode
*inode
= d_inode(old_dentry
);
3286 int err
, retries
= 0;
3288 if (inode
->i_nlink
>= EXT4_LINK_MAX
)
3290 if (ext4_encrypted_inode(dir
) &&
3291 !ext4_is_child_context_consistent_with_parent(dir
, inode
))
3293 dquot_initialize(dir
);
3296 handle
= ext4_journal_start(dir
, EXT4_HT_DIR
,
3297 (EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
3298 EXT4_INDEX_EXTRA_TRANS_BLOCKS
) + 1);
3300 return PTR_ERR(handle
);
3302 if (IS_DIRSYNC(dir
))
3303 ext4_handle_sync(handle
);
3305 inode
->i_ctime
= ext4_current_time(inode
);
3306 ext4_inc_count(handle
, inode
);
3309 err
= ext4_add_entry(handle
, dentry
, inode
);
3311 ext4_mark_inode_dirty(handle
, inode
);
3312 /* this can happen only for tmpfile being
3313 * linked the first time
3315 if (inode
->i_nlink
== 1)
3316 ext4_orphan_del(handle
, inode
);
3317 d_instantiate(dentry
, inode
);
3322 ext4_journal_stop(handle
);
3323 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
3330 * Try to find buffer head where contains the parent block.
3331 * It should be the inode block if it is inlined or the 1st block
3332 * if it is a normal dir.
3334 static struct buffer_head
*ext4_get_first_dir_block(handle_t
*handle
,
3335 struct inode
*inode
,
3337 struct ext4_dir_entry_2
**parent_de
,
3340 struct buffer_head
*bh
;
3342 if (!ext4_has_inline_data(inode
)) {
3343 bh
= ext4_read_dirblock(inode
, 0, EITHER
);
3345 *retval
= PTR_ERR(bh
);
3348 *parent_de
= ext4_next_entry(
3349 (struct ext4_dir_entry_2
*)bh
->b_data
,
3350 inode
->i_sb
->s_blocksize
);
3355 return ext4_get_first_inline_block(inode
, parent_de
, retval
);
3358 struct ext4_renament
{
3360 struct dentry
*dentry
;
3361 struct inode
*inode
;
3363 int dir_nlink_delta
;
3365 /* entry for "dentry" */
3366 struct buffer_head
*bh
;
3367 struct ext4_dir_entry_2
*de
;
3370 /* entry for ".." in inode if it's a directory */
3371 struct buffer_head
*dir_bh
;
3372 struct ext4_dir_entry_2
*parent_de
;
3376 static int ext4_rename_dir_prepare(handle_t
*handle
, struct ext4_renament
*ent
)
3380 ent
->dir_bh
= ext4_get_first_dir_block(handle
, ent
->inode
,
3381 &retval
, &ent
->parent_de
,
3385 if (le32_to_cpu(ent
->parent_de
->inode
) != ent
->dir
->i_ino
)
3387 BUFFER_TRACE(ent
->dir_bh
, "get_write_access");
3388 return ext4_journal_get_write_access(handle
, ent
->dir_bh
);
3391 static int ext4_rename_dir_finish(handle_t
*handle
, struct ext4_renament
*ent
,
3396 ent
->parent_de
->inode
= cpu_to_le32(dir_ino
);
3397 BUFFER_TRACE(ent
->dir_bh
, "call ext4_handle_dirty_metadata");
3398 if (!ent
->dir_inlined
) {
3399 if (is_dx(ent
->inode
)) {
3400 retval
= ext4_handle_dirty_dx_node(handle
,
3404 retval
= ext4_handle_dirty_dirent_node(handle
,
3409 retval
= ext4_mark_inode_dirty(handle
, ent
->inode
);
3412 ext4_std_error(ent
->dir
->i_sb
, retval
);
3418 static int ext4_setent(handle_t
*handle
, struct ext4_renament
*ent
,
3419 unsigned ino
, unsigned file_type
)
3423 BUFFER_TRACE(ent
->bh
, "get write access");
3424 retval
= ext4_journal_get_write_access(handle
, ent
->bh
);
3427 ent
->de
->inode
= cpu_to_le32(ino
);
3428 if (EXT4_HAS_INCOMPAT_FEATURE(ent
->dir
->i_sb
,
3429 EXT4_FEATURE_INCOMPAT_FILETYPE
))
3430 ent
->de
->file_type
= file_type
;
3431 ent
->dir
->i_version
++;
3432 ent
->dir
->i_ctime
= ent
->dir
->i_mtime
=
3433 ext4_current_time(ent
->dir
);
3434 ext4_mark_inode_dirty(handle
, ent
->dir
);
3435 BUFFER_TRACE(ent
->bh
, "call ext4_handle_dirty_metadata");
3436 if (!ent
->inlined
) {
3437 retval
= ext4_handle_dirty_dirent_node(handle
,
3439 if (unlikely(retval
)) {
3440 ext4_std_error(ent
->dir
->i_sb
, retval
);
3450 static int ext4_find_delete_entry(handle_t
*handle
, struct inode
*dir
,
3451 const struct qstr
*d_name
)
3453 int retval
= -ENOENT
;
3454 struct buffer_head
*bh
;
3455 struct ext4_dir_entry_2
*de
;
3457 bh
= ext4_find_entry(dir
, d_name
, &de
, NULL
);
3461 retval
= ext4_delete_entry(handle
, dir
, de
, bh
);
3467 static void ext4_rename_delete(handle_t
*handle
, struct ext4_renament
*ent
,
3472 * ent->de could have moved from under us during htree split, so make
3473 * sure that we are deleting the right entry. We might also be pointing
3474 * to a stale entry in the unused part of ent->bh so just checking inum
3475 * and the name isn't enough.
3477 if (le32_to_cpu(ent
->de
->inode
) != ent
->inode
->i_ino
||
3478 ent
->de
->name_len
!= ent
->dentry
->d_name
.len
||
3479 strncmp(ent
->de
->name
, ent
->dentry
->d_name
.name
,
3480 ent
->de
->name_len
) ||
3482 retval
= ext4_find_delete_entry(handle
, ent
->dir
,
3483 &ent
->dentry
->d_name
);
3485 retval
= ext4_delete_entry(handle
, ent
->dir
, ent
->de
, ent
->bh
);
3486 if (retval
== -ENOENT
) {
3487 retval
= ext4_find_delete_entry(handle
, ent
->dir
,
3488 &ent
->dentry
->d_name
);
3493 ext4_warning(ent
->dir
->i_sb
,
3494 "Deleting old file (%lu), %d, error=%d",
3495 ent
->dir
->i_ino
, ent
->dir
->i_nlink
, retval
);
3499 static void ext4_update_dir_count(handle_t
*handle
, struct ext4_renament
*ent
)
3501 if (ent
->dir_nlink_delta
) {
3502 if (ent
->dir_nlink_delta
== -1)
3503 ext4_dec_count(handle
, ent
->dir
);
3505 ext4_inc_count(handle
, ent
->dir
);
3506 ext4_mark_inode_dirty(handle
, ent
->dir
);
3510 static struct inode
*ext4_whiteout_for_rename(struct ext4_renament
*ent
,
3511 int credits
, handle_t
**h
)
3518 * for inode block, sb block, group summaries,
3521 credits
+= (EXT4_MAXQUOTAS_TRANS_BLOCKS(ent
->dir
->i_sb
) +
3522 EXT4_XATTR_TRANS_BLOCKS
+ 4);
3524 wh
= ext4_new_inode_start_handle(ent
->dir
, S_IFCHR
| WHITEOUT_MODE
,
3525 &ent
->dentry
->d_name
, 0, NULL
,
3526 EXT4_HT_DIR
, credits
);
3528 handle
= ext4_journal_current_handle();
3531 ext4_journal_stop(handle
);
3532 if (PTR_ERR(wh
) == -ENOSPC
&&
3533 ext4_should_retry_alloc(ent
->dir
->i_sb
, &retries
))
3537 init_special_inode(wh
, wh
->i_mode
, WHITEOUT_DEV
);
3538 wh
->i_op
= &ext4_special_inode_operations
;
3544 * Anybody can rename anything with this: the permission checks are left to the
3545 * higher-level routines.
3547 * n.b. old_{dentry,inode) refers to the source dentry/inode
3548 * while new_{dentry,inode) refers to the destination dentry/inode
3549 * This comes from rename(const char *oldpath, const char *newpath)
3551 static int ext4_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
3552 struct inode
*new_dir
, struct dentry
*new_dentry
,
3555 handle_t
*handle
= NULL
;
3556 struct ext4_renament old
= {
3558 .dentry
= old_dentry
,
3559 .inode
= d_inode(old_dentry
),
3561 struct ext4_renament
new = {
3563 .dentry
= new_dentry
,
3564 .inode
= d_inode(new_dentry
),
3568 struct inode
*whiteout
= NULL
;
3572 dquot_initialize(old
.dir
);
3573 dquot_initialize(new.dir
);
3575 /* Initialize quotas before so that eventual writes go
3576 * in separate transaction */
3578 dquot_initialize(new.inode
);
3580 old
.bh
= ext4_find_entry(old
.dir
, &old
.dentry
->d_name
, &old
.de
, NULL
);
3582 return PTR_ERR(old
.bh
);
3584 * Check for inode number is _not_ due to possible IO errors.
3585 * We might rmdir the source, keep it as pwd of some process
3586 * and merrily kill the link to whatever was created under the
3587 * same name. Goodbye sticky bit ;-<
3590 if (!old
.bh
|| le32_to_cpu(old
.de
->inode
) != old
.inode
->i_ino
)
3593 if ((old
.dir
!= new.dir
) &&
3594 ext4_encrypted_inode(new.dir
) &&
3595 !ext4_is_child_context_consistent_with_parent(new.dir
,
3601 new.bh
= ext4_find_entry(new.dir
, &new.dentry
->d_name
,
3602 &new.de
, &new.inlined
);
3603 if (IS_ERR(new.bh
)) {
3604 retval
= PTR_ERR(new.bh
);
3614 if (new.inode
&& !test_opt(new.dir
->i_sb
, NO_AUTO_DA_ALLOC
))
3615 ext4_alloc_da_blocks(old
.inode
);
3617 credits
= (2 * EXT4_DATA_TRANS_BLOCKS(old
.dir
->i_sb
) +
3618 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 2);
3619 if (!(flags
& RENAME_WHITEOUT
)) {
3620 handle
= ext4_journal_start(old
.dir
, EXT4_HT_DIR
, credits
);
3621 if (IS_ERR(handle
)) {
3622 retval
= PTR_ERR(handle
);
3627 whiteout
= ext4_whiteout_for_rename(&old
, credits
, &handle
);
3628 if (IS_ERR(whiteout
)) {
3629 retval
= PTR_ERR(whiteout
);
3635 if (IS_DIRSYNC(old
.dir
) || IS_DIRSYNC(new.dir
))
3636 ext4_handle_sync(handle
);
3638 if (S_ISDIR(old
.inode
->i_mode
)) {
3640 retval
= -ENOTEMPTY
;
3641 if (!ext4_empty_dir(new.inode
))
3645 if (new.dir
!= old
.dir
&& EXT4_DIR_LINK_MAX(new.dir
))
3648 retval
= ext4_rename_dir_prepare(handle
, &old
);
3653 * If we're renaming a file within an inline_data dir and adding or
3654 * setting the new dirent causes a conversion from inline_data to
3655 * extents/blockmap, we need to force the dirent delete code to
3656 * re-read the directory, or else we end up trying to delete a dirent
3657 * from what is now the extent tree root (or a block map).
3659 force_reread
= (new.dir
->i_ino
== old
.dir
->i_ino
&&
3660 ext4_test_inode_flag(new.dir
, EXT4_INODE_INLINE_DATA
));
3662 old_file_type
= old
.de
->file_type
;
3665 * Do this before adding a new entry, so the old entry is sure
3666 * to be still pointing to the valid old entry.
3668 retval
= ext4_setent(handle
, &old
, whiteout
->i_ino
,
3672 ext4_mark_inode_dirty(handle
, whiteout
);
3675 retval
= ext4_add_entry(handle
, new.dentry
, old
.inode
);
3679 retval
= ext4_setent(handle
, &new,
3680 old
.inode
->i_ino
, old_file_type
);
3685 force_reread
= !ext4_test_inode_flag(new.dir
,
3686 EXT4_INODE_INLINE_DATA
);
3689 * Like most other Unix systems, set the ctime for inodes on a
3692 old
.inode
->i_ctime
= ext4_current_time(old
.inode
);
3693 ext4_mark_inode_dirty(handle
, old
.inode
);
3699 ext4_rename_delete(handle
, &old
, force_reread
);
3703 ext4_dec_count(handle
, new.inode
);
3704 new.inode
->i_ctime
= ext4_current_time(new.inode
);
3706 old
.dir
->i_ctime
= old
.dir
->i_mtime
= ext4_current_time(old
.dir
);
3707 ext4_update_dx_flag(old
.dir
);
3709 retval
= ext4_rename_dir_finish(handle
, &old
, new.dir
->i_ino
);
3713 ext4_dec_count(handle
, old
.dir
);
3715 /* checked ext4_empty_dir above, can't have another
3716 * parent, ext4_dec_count() won't work for many-linked
3718 clear_nlink(new.inode
);
3720 ext4_inc_count(handle
, new.dir
);
3721 ext4_update_dx_flag(new.dir
);
3722 ext4_mark_inode_dirty(handle
, new.dir
);
3725 ext4_mark_inode_dirty(handle
, old
.dir
);
3727 ext4_mark_inode_dirty(handle
, new.inode
);
3728 if (!new.inode
->i_nlink
)
3729 ext4_orphan_add(handle
, new.inode
);
3739 drop_nlink(whiteout
);
3740 unlock_new_inode(whiteout
);
3744 ext4_journal_stop(handle
);
3748 static int ext4_cross_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
3749 struct inode
*new_dir
, struct dentry
*new_dentry
)
3751 handle_t
*handle
= NULL
;
3752 struct ext4_renament old
= {
3754 .dentry
= old_dentry
,
3755 .inode
= d_inode(old_dentry
),
3757 struct ext4_renament
new = {
3759 .dentry
= new_dentry
,
3760 .inode
= d_inode(new_dentry
),
3765 dquot_initialize(old
.dir
);
3766 dquot_initialize(new.dir
);
3768 old
.bh
= ext4_find_entry(old
.dir
, &old
.dentry
->d_name
,
3769 &old
.de
, &old
.inlined
);
3771 return PTR_ERR(old
.bh
);
3773 * Check for inode number is _not_ due to possible IO errors.
3774 * We might rmdir the source, keep it as pwd of some process
3775 * and merrily kill the link to whatever was created under the
3776 * same name. Goodbye sticky bit ;-<
3779 if (!old
.bh
|| le32_to_cpu(old
.de
->inode
) != old
.inode
->i_ino
)
3782 new.bh
= ext4_find_entry(new.dir
, &new.dentry
->d_name
,
3783 &new.de
, &new.inlined
);
3784 if (IS_ERR(new.bh
)) {
3785 retval
= PTR_ERR(new.bh
);
3790 /* RENAME_EXCHANGE case: old *and* new must both exist */
3791 if (!new.bh
|| le32_to_cpu(new.de
->inode
) != new.inode
->i_ino
)
3794 handle
= ext4_journal_start(old
.dir
, EXT4_HT_DIR
,
3795 (2 * EXT4_DATA_TRANS_BLOCKS(old
.dir
->i_sb
) +
3796 2 * EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 2));
3797 if (IS_ERR(handle
)) {
3798 retval
= PTR_ERR(handle
);
3803 if (IS_DIRSYNC(old
.dir
) || IS_DIRSYNC(new.dir
))
3804 ext4_handle_sync(handle
);
3806 if (S_ISDIR(old
.inode
->i_mode
)) {
3808 retval
= ext4_rename_dir_prepare(handle
, &old
);
3812 if (S_ISDIR(new.inode
->i_mode
)) {
3814 retval
= ext4_rename_dir_prepare(handle
, &new);
3820 * Other than the special case of overwriting a directory, parents'
3821 * nlink only needs to be modified if this is a cross directory rename.
3823 if (old
.dir
!= new.dir
&& old
.is_dir
!= new.is_dir
) {
3824 old
.dir_nlink_delta
= old
.is_dir
? -1 : 1;
3825 new.dir_nlink_delta
= -old
.dir_nlink_delta
;
3827 if ((old
.dir_nlink_delta
> 0 && EXT4_DIR_LINK_MAX(old
.dir
)) ||
3828 (new.dir_nlink_delta
> 0 && EXT4_DIR_LINK_MAX(new.dir
)))
3832 new_file_type
= new.de
->file_type
;
3833 retval
= ext4_setent(handle
, &new, old
.inode
->i_ino
, old
.de
->file_type
);
3837 retval
= ext4_setent(handle
, &old
, new.inode
->i_ino
, new_file_type
);
3842 * Like most other Unix systems, set the ctime for inodes on a
3845 old
.inode
->i_ctime
= ext4_current_time(old
.inode
);
3846 new.inode
->i_ctime
= ext4_current_time(new.inode
);
3847 ext4_mark_inode_dirty(handle
, old
.inode
);
3848 ext4_mark_inode_dirty(handle
, new.inode
);
3851 retval
= ext4_rename_dir_finish(handle
, &old
, new.dir
->i_ino
);
3856 retval
= ext4_rename_dir_finish(handle
, &new, old
.dir
->i_ino
);
3860 ext4_update_dir_count(handle
, &old
);
3861 ext4_update_dir_count(handle
, &new);
3870 ext4_journal_stop(handle
);
3874 static int ext4_rename2(struct inode
*old_dir
, struct dentry
*old_dentry
,
3875 struct inode
*new_dir
, struct dentry
*new_dentry
,
3878 if (flags
& ~(RENAME_NOREPLACE
| RENAME_EXCHANGE
| RENAME_WHITEOUT
))
3881 if (flags
& RENAME_EXCHANGE
) {
3882 return ext4_cross_rename(old_dir
, old_dentry
,
3883 new_dir
, new_dentry
);
3886 return ext4_rename(old_dir
, old_dentry
, new_dir
, new_dentry
, flags
);
3890 * directories can handle most operations...
3892 const struct inode_operations ext4_dir_inode_operations
= {
3893 .create
= ext4_create
,
3894 .lookup
= ext4_lookup
,
3896 .unlink
= ext4_unlink
,
3897 .symlink
= ext4_symlink
,
3898 .mkdir
= ext4_mkdir
,
3899 .rmdir
= ext4_rmdir
,
3900 .mknod
= ext4_mknod
,
3901 .tmpfile
= ext4_tmpfile
,
3902 .rename2
= ext4_rename2
,
3903 .setattr
= ext4_setattr
,
3904 .setxattr
= generic_setxattr
,
3905 .getxattr
= generic_getxattr
,
3906 .listxattr
= ext4_listxattr
,
3907 .removexattr
= generic_removexattr
,
3908 .get_acl
= ext4_get_acl
,
3909 .set_acl
= ext4_set_acl
,
3910 .fiemap
= ext4_fiemap
,
3913 const struct inode_operations ext4_special_inode_operations
= {
3914 .setattr
= ext4_setattr
,
3915 .setxattr
= generic_setxattr
,
3916 .getxattr
= generic_getxattr
,
3917 .listxattr
= ext4_listxattr
,
3918 .removexattr
= generic_removexattr
,
3919 .get_acl
= ext4_get_acl
,
3920 .set_acl
= ext4_set_acl
,