1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/ext4/namei.c
5 * Copyright (C) 1992, 1993, 1994, 1995
6 * Remy Card (card@masi.ibp.fr)
7 * Laboratoire MASI - Institut Blaise Pascal
8 * Universite Pierre et Marie Curie (Paris VI)
12 * linux/fs/minix/namei.c
14 * Copyright (C) 1991, 1992 Linus Torvalds
16 * Big-endian to little-endian byte-swapping/bitmaps by
17 * David S. Miller (davem@caip.rutgers.edu), 1995
18 * Directory entry file type support and forward compatibility hooks
19 * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
20 * Hash Tree Directory indexing (c)
21 * Daniel Phillips, 2001
22 * Hash Tree Directory indexing porting
23 * Christopher Li, 2002
24 * Hash Tree Directory indexing cleanup
29 #include <linux/pagemap.h>
30 #include <linux/time.h>
31 #include <linux/fcntl.h>
32 #include <linux/stat.h>
33 #include <linux/string.h>
34 #include <linux/quotaops.h>
35 #include <linux/buffer_head.h>
36 #include <linux/bio.h>
37 #include <linux/iversion.h>
38 #include <linux/unicode.h>
40 #include "ext4_jbd2.h"
45 #include <trace/events/ext4.h>
47 * define how far ahead to read directories while searching them.
49 #define NAMEI_RA_CHUNKS 2
50 #define NAMEI_RA_BLOCKS 4
51 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
53 static struct buffer_head
*ext4_append(handle_t
*handle
,
57 struct buffer_head
*bh
;
60 if (unlikely(EXT4_SB(inode
->i_sb
)->s_max_dir_size_kb
&&
61 ((inode
->i_size
>> 10) >=
62 EXT4_SB(inode
->i_sb
)->s_max_dir_size_kb
)))
63 return ERR_PTR(-ENOSPC
);
65 *block
= inode
->i_size
>> inode
->i_sb
->s_blocksize_bits
;
67 bh
= ext4_bread(handle
, inode
, *block
, EXT4_GET_BLOCKS_CREATE
);
70 inode
->i_size
+= inode
->i_sb
->s_blocksize
;
71 EXT4_I(inode
)->i_disksize
= inode
->i_size
;
72 BUFFER_TRACE(bh
, "get_write_access");
73 err
= ext4_journal_get_write_access(handle
, bh
);
76 ext4_std_error(inode
->i_sb
, err
);
82 static int ext4_dx_csum_verify(struct inode
*inode
,
83 struct ext4_dir_entry
*dirent
);
89 #define ext4_read_dirblock(inode, block, type) \
90 __ext4_read_dirblock((inode), (block), (type), __func__, __LINE__)
92 static struct buffer_head
*__ext4_read_dirblock(struct inode
*inode
,
98 struct buffer_head
*bh
;
99 struct ext4_dir_entry
*dirent
;
102 bh
= ext4_bread(NULL
, inode
, block
, 0);
104 __ext4_warning(inode
->i_sb
, func
, line
,
105 "inode #%lu: lblock %lu: comm %s: "
106 "error %ld reading directory block",
107 inode
->i_ino
, (unsigned long)block
,
108 current
->comm
, PTR_ERR(bh
));
113 ext4_error_inode(inode
, func
, line
, block
,
114 "Directory hole found");
115 return ERR_PTR(-EFSCORRUPTED
);
117 dirent
= (struct ext4_dir_entry
*) bh
->b_data
;
118 /* Determine whether or not we have an index block */
122 else if (ext4_rec_len_from_disk(dirent
->rec_len
,
123 inode
->i_sb
->s_blocksize
) ==
124 inode
->i_sb
->s_blocksize
)
127 if (!is_dx_block
&& type
== INDEX
) {
128 ext4_error_inode(inode
, func
, line
, block
,
129 "directory leaf block found instead of index block");
131 return ERR_PTR(-EFSCORRUPTED
);
133 if (!ext4_has_metadata_csum(inode
->i_sb
) ||
138 * An empty leaf block can get mistaken for a index block; for
139 * this reason, we can only check the index checksum when the
140 * caller is sure it should be an index block.
142 if (is_dx_block
&& type
== INDEX
) {
143 if (ext4_dx_csum_verify(inode
, dirent
))
144 set_buffer_verified(bh
);
146 ext4_error_inode(inode
, func
, line
, block
,
147 "Directory index failed checksum");
149 return ERR_PTR(-EFSBADCRC
);
153 if (ext4_dirent_csum_verify(inode
, dirent
))
154 set_buffer_verified(bh
);
156 ext4_error_inode(inode
, func
, line
, block
,
157 "Directory block failed checksum");
159 return ERR_PTR(-EFSBADCRC
);
166 #define assert(test) J_ASSERT(test)
170 #define dxtrace(command) command
172 #define dxtrace(command)
196 * dx_root_info is laid out so that if it should somehow get overlaid by a
197 * dirent the two low bits of the hash version will be zero. Therefore, the
198 * hash version mod 4 should never be 0. Sincerely, the paranoia department.
203 struct fake_dirent dot
;
205 struct fake_dirent dotdot
;
209 __le32 reserved_zero
;
211 u8 info_length
; /* 8 */
216 struct dx_entry entries
[0];
221 struct fake_dirent fake
;
222 struct dx_entry entries
[0];
228 struct buffer_head
*bh
;
229 struct dx_entry
*entries
;
241 * This goes at the end of each htree block.
245 __le32 dt_checksum
; /* crc32c(uuid+inum+dirblock) */
248 static inline ext4_lblk_t
dx_get_block(struct dx_entry
*entry
);
249 static void dx_set_block(struct dx_entry
*entry
, ext4_lblk_t value
);
250 static inline unsigned dx_get_hash(struct dx_entry
*entry
);
251 static void dx_set_hash(struct dx_entry
*entry
, unsigned value
);
252 static unsigned dx_get_count(struct dx_entry
*entries
);
253 static unsigned dx_get_limit(struct dx_entry
*entries
);
254 static void dx_set_count(struct dx_entry
*entries
, unsigned value
);
255 static void dx_set_limit(struct dx_entry
*entries
, unsigned value
);
256 static unsigned dx_root_limit(struct inode
*dir
, unsigned infosize
);
257 static unsigned dx_node_limit(struct inode
*dir
);
258 static struct dx_frame
*dx_probe(struct ext4_filename
*fname
,
260 struct dx_hash_info
*hinfo
,
261 struct dx_frame
*frame
);
262 static void dx_release(struct dx_frame
*frames
);
263 static int dx_make_map(struct inode
*dir
, struct ext4_dir_entry_2
*de
,
264 unsigned blocksize
, struct dx_hash_info
*hinfo
,
265 struct dx_map_entry map
[]);
266 static void dx_sort_map(struct dx_map_entry
*map
, unsigned count
);
267 static struct ext4_dir_entry_2
*dx_move_dirents(char *from
, char *to
,
268 struct dx_map_entry
*offsets
, int count
, unsigned blocksize
);
269 static struct ext4_dir_entry_2
* dx_pack_dirents(char *base
, unsigned blocksize
);
270 static void dx_insert_block(struct dx_frame
*frame
,
271 u32 hash
, ext4_lblk_t block
);
272 static int ext4_htree_next_block(struct inode
*dir
, __u32 hash
,
273 struct dx_frame
*frame
,
274 struct dx_frame
*frames
,
276 static struct buffer_head
* ext4_dx_find_entry(struct inode
*dir
,
277 struct ext4_filename
*fname
,
278 struct ext4_dir_entry_2
**res_dir
);
279 static int ext4_dx_add_entry(handle_t
*handle
, struct ext4_filename
*fname
,
280 struct inode
*dir
, struct inode
*inode
);
282 /* checksumming functions */
283 void initialize_dirent_tail(struct ext4_dir_entry_tail
*t
,
284 unsigned int blocksize
)
286 memset(t
, 0, sizeof(struct ext4_dir_entry_tail
));
287 t
->det_rec_len
= ext4_rec_len_to_disk(
288 sizeof(struct ext4_dir_entry_tail
), blocksize
);
289 t
->det_reserved_ft
= EXT4_FT_DIR_CSUM
;
292 /* Walk through a dirent block to find a checksum "dirent" at the tail */
293 static struct ext4_dir_entry_tail
*get_dirent_tail(struct inode
*inode
,
294 struct ext4_dir_entry
*de
)
296 struct ext4_dir_entry_tail
*t
;
299 struct ext4_dir_entry
*d
, *top
;
302 top
= (struct ext4_dir_entry
*)(((void *)de
) +
303 (EXT4_BLOCK_SIZE(inode
->i_sb
) -
304 sizeof(struct ext4_dir_entry_tail
)));
305 while (d
< top
&& d
->rec_len
)
306 d
= (struct ext4_dir_entry
*)(((void *)d
) +
307 le16_to_cpu(d
->rec_len
));
312 t
= (struct ext4_dir_entry_tail
*)d
;
314 t
= EXT4_DIRENT_TAIL(de
, EXT4_BLOCK_SIZE(inode
->i_sb
));
317 if (t
->det_reserved_zero1
||
318 le16_to_cpu(t
->det_rec_len
) != sizeof(struct ext4_dir_entry_tail
) ||
319 t
->det_reserved_zero2
||
320 t
->det_reserved_ft
!= EXT4_FT_DIR_CSUM
)
326 static __le32
ext4_dirent_csum(struct inode
*inode
,
327 struct ext4_dir_entry
*dirent
, int size
)
329 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
330 struct ext4_inode_info
*ei
= EXT4_I(inode
);
333 csum
= ext4_chksum(sbi
, ei
->i_csum_seed
, (__u8
*)dirent
, size
);
334 return cpu_to_le32(csum
);
337 #define warn_no_space_for_csum(inode) \
338 __warn_no_space_for_csum((inode), __func__, __LINE__)
340 static void __warn_no_space_for_csum(struct inode
*inode
, const char *func
,
343 __ext4_warning_inode(inode
, func
, line
,
344 "No space for directory leaf checksum. Please run e2fsck -D.");
347 int ext4_dirent_csum_verify(struct inode
*inode
, struct ext4_dir_entry
*dirent
)
349 struct ext4_dir_entry_tail
*t
;
351 if (!ext4_has_metadata_csum(inode
->i_sb
))
354 t
= get_dirent_tail(inode
, dirent
);
356 warn_no_space_for_csum(inode
);
360 if (t
->det_checksum
!= ext4_dirent_csum(inode
, dirent
,
361 (void *)t
- (void *)dirent
))
367 static void ext4_dirent_csum_set(struct inode
*inode
,
368 struct ext4_dir_entry
*dirent
)
370 struct ext4_dir_entry_tail
*t
;
372 if (!ext4_has_metadata_csum(inode
->i_sb
))
375 t
= get_dirent_tail(inode
, dirent
);
377 warn_no_space_for_csum(inode
);
381 t
->det_checksum
= ext4_dirent_csum(inode
, dirent
,
382 (void *)t
- (void *)dirent
);
385 int ext4_handle_dirty_dirent_node(handle_t
*handle
,
387 struct buffer_head
*bh
)
389 ext4_dirent_csum_set(inode
, (struct ext4_dir_entry
*)bh
->b_data
);
390 return ext4_handle_dirty_metadata(handle
, inode
, bh
);
393 static struct dx_countlimit
*get_dx_countlimit(struct inode
*inode
,
394 struct ext4_dir_entry
*dirent
,
397 struct ext4_dir_entry
*dp
;
398 struct dx_root_info
*root
;
401 if (le16_to_cpu(dirent
->rec_len
) == EXT4_BLOCK_SIZE(inode
->i_sb
))
403 else if (le16_to_cpu(dirent
->rec_len
) == 12) {
404 dp
= (struct ext4_dir_entry
*)(((void *)dirent
) + 12);
405 if (le16_to_cpu(dp
->rec_len
) !=
406 EXT4_BLOCK_SIZE(inode
->i_sb
) - 12)
408 root
= (struct dx_root_info
*)(((void *)dp
+ 12));
409 if (root
->reserved_zero
||
410 root
->info_length
!= sizeof(struct dx_root_info
))
417 *offset
= count_offset
;
418 return (struct dx_countlimit
*)(((void *)dirent
) + count_offset
);
421 static __le32
ext4_dx_csum(struct inode
*inode
, struct ext4_dir_entry
*dirent
,
422 int count_offset
, int count
, struct dx_tail
*t
)
424 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
425 struct ext4_inode_info
*ei
= EXT4_I(inode
);
428 __u32 dummy_csum
= 0;
429 int offset
= offsetof(struct dx_tail
, dt_checksum
);
431 size
= count_offset
+ (count
* sizeof(struct dx_entry
));
432 csum
= ext4_chksum(sbi
, ei
->i_csum_seed
, (__u8
*)dirent
, size
);
433 csum
= ext4_chksum(sbi
, csum
, (__u8
*)t
, offset
);
434 csum
= ext4_chksum(sbi
, csum
, (__u8
*)&dummy_csum
, sizeof(dummy_csum
));
436 return cpu_to_le32(csum
);
439 static int ext4_dx_csum_verify(struct inode
*inode
,
440 struct ext4_dir_entry
*dirent
)
442 struct dx_countlimit
*c
;
444 int count_offset
, limit
, count
;
446 if (!ext4_has_metadata_csum(inode
->i_sb
))
449 c
= get_dx_countlimit(inode
, dirent
, &count_offset
);
451 EXT4_ERROR_INODE(inode
, "dir seems corrupt? Run e2fsck -D.");
454 limit
= le16_to_cpu(c
->limit
);
455 count
= le16_to_cpu(c
->count
);
456 if (count_offset
+ (limit
* sizeof(struct dx_entry
)) >
457 EXT4_BLOCK_SIZE(inode
->i_sb
) - sizeof(struct dx_tail
)) {
458 warn_no_space_for_csum(inode
);
461 t
= (struct dx_tail
*)(((struct dx_entry
*)c
) + limit
);
463 if (t
->dt_checksum
!= ext4_dx_csum(inode
, dirent
, count_offset
,
469 static void ext4_dx_csum_set(struct inode
*inode
, struct ext4_dir_entry
*dirent
)
471 struct dx_countlimit
*c
;
473 int count_offset
, limit
, count
;
475 if (!ext4_has_metadata_csum(inode
->i_sb
))
478 c
= get_dx_countlimit(inode
, dirent
, &count_offset
);
480 EXT4_ERROR_INODE(inode
, "dir seems corrupt? Run e2fsck -D.");
483 limit
= le16_to_cpu(c
->limit
);
484 count
= le16_to_cpu(c
->count
);
485 if (count_offset
+ (limit
* sizeof(struct dx_entry
)) >
486 EXT4_BLOCK_SIZE(inode
->i_sb
) - sizeof(struct dx_tail
)) {
487 warn_no_space_for_csum(inode
);
490 t
= (struct dx_tail
*)(((struct dx_entry
*)c
) + limit
);
492 t
->dt_checksum
= ext4_dx_csum(inode
, dirent
, count_offset
, count
, t
);
495 static inline int ext4_handle_dirty_dx_node(handle_t
*handle
,
497 struct buffer_head
*bh
)
499 ext4_dx_csum_set(inode
, (struct ext4_dir_entry
*)bh
->b_data
);
500 return ext4_handle_dirty_metadata(handle
, inode
, bh
);
504 * p is at least 6 bytes before the end of page
506 static inline struct ext4_dir_entry_2
*
507 ext4_next_entry(struct ext4_dir_entry_2
*p
, unsigned long blocksize
)
509 return (struct ext4_dir_entry_2
*)((char *)p
+
510 ext4_rec_len_from_disk(p
->rec_len
, blocksize
));
514 * Future: use high four bits of block for coalesce-on-delete flags
515 * Mask them off for now.
518 static inline ext4_lblk_t
dx_get_block(struct dx_entry
*entry
)
520 return le32_to_cpu(entry
->block
) & 0x0fffffff;
523 static inline void dx_set_block(struct dx_entry
*entry
, ext4_lblk_t value
)
525 entry
->block
= cpu_to_le32(value
);
528 static inline unsigned dx_get_hash(struct dx_entry
*entry
)
530 return le32_to_cpu(entry
->hash
);
533 static inline void dx_set_hash(struct dx_entry
*entry
, unsigned value
)
535 entry
->hash
= cpu_to_le32(value
);
538 static inline unsigned dx_get_count(struct dx_entry
*entries
)
540 return le16_to_cpu(((struct dx_countlimit
*) entries
)->count
);
543 static inline unsigned dx_get_limit(struct dx_entry
*entries
)
545 return le16_to_cpu(((struct dx_countlimit
*) entries
)->limit
);
548 static inline void dx_set_count(struct dx_entry
*entries
, unsigned value
)
550 ((struct dx_countlimit
*) entries
)->count
= cpu_to_le16(value
);
553 static inline void dx_set_limit(struct dx_entry
*entries
, unsigned value
)
555 ((struct dx_countlimit
*) entries
)->limit
= cpu_to_le16(value
);
558 static inline unsigned dx_root_limit(struct inode
*dir
, unsigned infosize
)
560 unsigned entry_space
= dir
->i_sb
->s_blocksize
- EXT4_DIR_REC_LEN(1) -
561 EXT4_DIR_REC_LEN(2) - infosize
;
563 if (ext4_has_metadata_csum(dir
->i_sb
))
564 entry_space
-= sizeof(struct dx_tail
);
565 return entry_space
/ sizeof(struct dx_entry
);
568 static inline unsigned dx_node_limit(struct inode
*dir
)
570 unsigned entry_space
= dir
->i_sb
->s_blocksize
- EXT4_DIR_REC_LEN(0);
572 if (ext4_has_metadata_csum(dir
->i_sb
))
573 entry_space
-= sizeof(struct dx_tail
);
574 return entry_space
/ sizeof(struct dx_entry
);
581 static void dx_show_index(char * label
, struct dx_entry
*entries
)
583 int i
, n
= dx_get_count (entries
);
584 printk(KERN_DEBUG
"%s index", label
);
585 for (i
= 0; i
< n
; i
++) {
586 printk(KERN_CONT
" %x->%lu",
587 i
? dx_get_hash(entries
+ i
) : 0,
588 (unsigned long)dx_get_block(entries
+ i
));
590 printk(KERN_CONT
"\n");
600 static struct stats
dx_show_leaf(struct inode
*dir
,
601 struct dx_hash_info
*hinfo
,
602 struct ext4_dir_entry_2
*de
,
603 int size
, int show_names
)
605 unsigned names
= 0, space
= 0;
606 char *base
= (char *) de
;
607 struct dx_hash_info h
= *hinfo
;
610 while ((char *) de
< base
+ size
)
616 #ifdef CONFIG_FS_ENCRYPTION
619 struct fscrypt_str fname_crypto_str
=
625 if (IS_ENCRYPTED(dir
))
626 res
= fscrypt_get_encryption_info(dir
);
628 printk(KERN_WARNING
"Error setting up"
629 " fname crypto: %d\n", res
);
631 if (!fscrypt_has_encryption_key(dir
)) {
632 /* Directory is not encrypted */
633 ext4fs_dirhash(dir
, de
->name
,
635 printk("%*.s:(U)%x.%u ", len
,
637 (unsigned) ((char *) de
640 struct fscrypt_str de_name
=
641 FSTR_INIT(name
, len
);
643 /* Directory is encrypted */
644 res
= fscrypt_fname_alloc_buffer(
648 printk(KERN_WARNING
"Error "
652 res
= fscrypt_fname_disk_to_usr(dir
,
656 printk(KERN_WARNING
"Error "
657 "converting filename "
663 name
= fname_crypto_str
.name
;
664 len
= fname_crypto_str
.len
;
666 ext4fs_dirhash(dir
, de
->name
,
668 printk("%*.s:(E)%x.%u ", len
, name
,
669 h
.hash
, (unsigned) ((char *) de
671 fscrypt_fname_free_buffer(
675 int len
= de
->name_len
;
676 char *name
= de
->name
;
677 ext4fs_dirhash(dir
, de
->name
, de
->name_len
, &h
);
678 printk("%*.s:%x.%u ", len
, name
, h
.hash
,
679 (unsigned) ((char *) de
- base
));
682 space
+= EXT4_DIR_REC_LEN(de
->name_len
);
685 de
= ext4_next_entry(de
, size
);
687 printk(KERN_CONT
"(%i)\n", names
);
688 return (struct stats
) { names
, space
, 1 };
691 struct stats
dx_show_entries(struct dx_hash_info
*hinfo
, struct inode
*dir
,
692 struct dx_entry
*entries
, int levels
)
694 unsigned blocksize
= dir
->i_sb
->s_blocksize
;
695 unsigned count
= dx_get_count(entries
), names
= 0, space
= 0, i
;
697 struct buffer_head
*bh
;
698 printk("%i indexed blocks...\n", count
);
699 for (i
= 0; i
< count
; i
++, entries
++)
701 ext4_lblk_t block
= dx_get_block(entries
);
702 ext4_lblk_t hash
= i
? dx_get_hash(entries
): 0;
703 u32 range
= i
< count
- 1? (dx_get_hash(entries
+ 1) - hash
): ~hash
;
705 printk("%s%3u:%03u hash %8x/%8x ",levels
?"":" ", i
, block
, hash
, range
);
706 bh
= ext4_bread(NULL
,dir
, block
, 0);
707 if (!bh
|| IS_ERR(bh
))
710 dx_show_entries(hinfo
, dir
, ((struct dx_node
*) bh
->b_data
)->entries
, levels
- 1):
711 dx_show_leaf(dir
, hinfo
, (struct ext4_dir_entry_2
*)
712 bh
->b_data
, blocksize
, 0);
713 names
+= stats
.names
;
714 space
+= stats
.space
;
715 bcount
+= stats
.bcount
;
719 printk(KERN_DEBUG
"%snames %u, fullness %u (%u%%)\n",
720 levels
? "" : " ", names
, space
/bcount
,
721 (space
/bcount
)*100/blocksize
);
722 return (struct stats
) { names
, space
, bcount
};
724 #endif /* DX_DEBUG */
727 * Probe for a directory leaf block to search.
729 * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
730 * error in the directory index, and the caller should fall back to
731 * searching the directory normally. The callers of dx_probe **MUST**
732 * check for this error code, and make sure it never gets reflected
735 static struct dx_frame
*
736 dx_probe(struct ext4_filename
*fname
, struct inode
*dir
,
737 struct dx_hash_info
*hinfo
, struct dx_frame
*frame_in
)
739 unsigned count
, indirect
;
740 struct dx_entry
*at
, *entries
, *p
, *q
, *m
;
741 struct dx_root
*root
;
742 struct dx_frame
*frame
= frame_in
;
743 struct dx_frame
*ret_err
= ERR_PTR(ERR_BAD_DX_DIR
);
746 memset(frame_in
, 0, EXT4_HTREE_LEVEL
* sizeof(frame_in
[0]));
747 frame
->bh
= ext4_read_dirblock(dir
, 0, INDEX
);
748 if (IS_ERR(frame
->bh
))
749 return (struct dx_frame
*) frame
->bh
;
751 root
= (struct dx_root
*) frame
->bh
->b_data
;
752 if (root
->info
.hash_version
!= DX_HASH_TEA
&&
753 root
->info
.hash_version
!= DX_HASH_HALF_MD4
&&
754 root
->info
.hash_version
!= DX_HASH_LEGACY
) {
755 ext4_warning_inode(dir
, "Unrecognised inode hash code %u",
756 root
->info
.hash_version
);
760 hinfo
= &fname
->hinfo
;
761 hinfo
->hash_version
= root
->info
.hash_version
;
762 if (hinfo
->hash_version
<= DX_HASH_TEA
)
763 hinfo
->hash_version
+= EXT4_SB(dir
->i_sb
)->s_hash_unsigned
;
764 hinfo
->seed
= EXT4_SB(dir
->i_sb
)->s_hash_seed
;
765 if (fname
&& fname_name(fname
))
766 ext4fs_dirhash(dir
, fname_name(fname
), fname_len(fname
), hinfo
);
769 if (root
->info
.unused_flags
& 1) {
770 ext4_warning_inode(dir
, "Unimplemented hash flags: %#06x",
771 root
->info
.unused_flags
);
775 indirect
= root
->info
.indirect_levels
;
776 if (indirect
>= ext4_dir_htree_level(dir
->i_sb
)) {
777 ext4_warning(dir
->i_sb
,
778 "Directory (ino: %lu) htree depth %#06x exceed"
779 "supported value", dir
->i_ino
,
780 ext4_dir_htree_level(dir
->i_sb
));
781 if (ext4_dir_htree_level(dir
->i_sb
) < EXT4_HTREE_LEVEL
) {
782 ext4_warning(dir
->i_sb
, "Enable large directory "
783 "feature to access it");
788 entries
= (struct dx_entry
*)(((char *)&root
->info
) +
789 root
->info
.info_length
);
791 if (dx_get_limit(entries
) != dx_root_limit(dir
,
792 root
->info
.info_length
)) {
793 ext4_warning_inode(dir
, "dx entry: limit %u != root limit %u",
794 dx_get_limit(entries
),
795 dx_root_limit(dir
, root
->info
.info_length
));
799 dxtrace(printk("Look up %x", hash
));
801 count
= dx_get_count(entries
);
802 if (!count
|| count
> dx_get_limit(entries
)) {
803 ext4_warning_inode(dir
,
804 "dx entry: count %u beyond limit %u",
805 count
, dx_get_limit(entries
));
810 q
= entries
+ count
- 1;
813 dxtrace(printk(KERN_CONT
"."));
814 if (dx_get_hash(m
) > hash
)
820 if (0) { // linear search cross check
821 unsigned n
= count
- 1;
825 dxtrace(printk(KERN_CONT
","));
826 if (dx_get_hash(++at
) > hash
)
832 assert (at
== p
- 1);
836 dxtrace(printk(KERN_CONT
" %x->%u\n",
837 at
== entries
? 0 : dx_get_hash(at
),
839 frame
->entries
= entries
;
844 frame
->bh
= ext4_read_dirblock(dir
, dx_get_block(at
), INDEX
);
845 if (IS_ERR(frame
->bh
)) {
846 ret_err
= (struct dx_frame
*) frame
->bh
;
850 entries
= ((struct dx_node
*) frame
->bh
->b_data
)->entries
;
852 if (dx_get_limit(entries
) != dx_node_limit(dir
)) {
853 ext4_warning_inode(dir
,
854 "dx entry: limit %u != node limit %u",
855 dx_get_limit(entries
), dx_node_limit(dir
));
860 while (frame
>= frame_in
) {
865 if (ret_err
== ERR_PTR(ERR_BAD_DX_DIR
))
866 ext4_warning_inode(dir
,
867 "Corrupt directory, running e2fsck is recommended");
871 static void dx_release(struct dx_frame
*frames
)
873 struct dx_root_info
*info
;
875 unsigned int indirect_levels
;
877 if (frames
[0].bh
== NULL
)
880 info
= &((struct dx_root
*)frames
[0].bh
->b_data
)->info
;
881 /* save local copy, "info" may be freed after brelse() */
882 indirect_levels
= info
->indirect_levels
;
883 for (i
= 0; i
<= indirect_levels
; i
++) {
884 if (frames
[i
].bh
== NULL
)
886 brelse(frames
[i
].bh
);
892 * This function increments the frame pointer to search the next leaf
893 * block, and reads in the necessary intervening nodes if the search
894 * should be necessary. Whether or not the search is necessary is
895 * controlled by the hash parameter. If the hash value is even, then
896 * the search is only continued if the next block starts with that
897 * hash value. This is used if we are searching for a specific file.
899 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
901 * This function returns 1 if the caller should continue to search,
902 * or 0 if it should not. If there is an error reading one of the
903 * index blocks, it will a negative error code.
905 * If start_hash is non-null, it will be filled in with the starting
906 * hash of the next page.
908 static int ext4_htree_next_block(struct inode
*dir
, __u32 hash
,
909 struct dx_frame
*frame
,
910 struct dx_frame
*frames
,
914 struct buffer_head
*bh
;
920 * Find the next leaf page by incrementing the frame pointer.
921 * If we run out of entries in the interior node, loop around and
922 * increment pointer in the parent node. When we break out of
923 * this loop, num_frames indicates the number of interior
924 * nodes need to be read.
927 if (++(p
->at
) < p
->entries
+ dx_get_count(p
->entries
))
936 * If the hash is 1, then continue only if the next page has a
937 * continuation hash of any value. This is used for readdir
938 * handling. Otherwise, check to see if the hash matches the
939 * desired contiuation hash. If it doesn't, return since
940 * there's no point to read in the successive index pages.
942 bhash
= dx_get_hash(p
->at
);
945 if ((hash
& 1) == 0) {
946 if ((bhash
& ~1) != hash
)
950 * If the hash is HASH_NB_ALWAYS, we always go to the next
951 * block so no check is necessary
953 while (num_frames
--) {
954 bh
= ext4_read_dirblock(dir
, dx_get_block(p
->at
), INDEX
);
960 p
->at
= p
->entries
= ((struct dx_node
*) bh
->b_data
)->entries
;
967 * This function fills a red-black tree with information from a
968 * directory block. It returns the number directory entries loaded
969 * into the tree. If there is an error it is returned in err.
971 static int htree_dirblock_to_tree(struct file
*dir_file
,
972 struct inode
*dir
, ext4_lblk_t block
,
973 struct dx_hash_info
*hinfo
,
974 __u32 start_hash
, __u32 start_minor_hash
)
976 struct buffer_head
*bh
;
977 struct ext4_dir_entry_2
*de
, *top
;
978 int err
= 0, count
= 0;
979 struct fscrypt_str fname_crypto_str
= FSTR_INIT(NULL
, 0), tmp_str
;
981 dxtrace(printk(KERN_INFO
"In htree dirblock_to_tree: block %lu\n",
982 (unsigned long)block
));
983 bh
= ext4_read_dirblock(dir
, block
, DIRENT
);
987 de
= (struct ext4_dir_entry_2
*) bh
->b_data
;
988 top
= (struct ext4_dir_entry_2
*) ((char *) de
+
989 dir
->i_sb
->s_blocksize
-
990 EXT4_DIR_REC_LEN(0));
991 #ifdef CONFIG_FS_ENCRYPTION
992 /* Check if the directory is encrypted */
993 if (IS_ENCRYPTED(dir
)) {
994 err
= fscrypt_get_encryption_info(dir
);
999 err
= fscrypt_fname_alloc_buffer(dir
, EXT4_NAME_LEN
,
1007 for (; de
< top
; de
= ext4_next_entry(de
, dir
->i_sb
->s_blocksize
)) {
1008 if (ext4_check_dir_entry(dir
, NULL
, de
, bh
,
1009 bh
->b_data
, bh
->b_size
,
1010 (block
<<EXT4_BLOCK_SIZE_BITS(dir
->i_sb
))
1011 + ((char *)de
- bh
->b_data
))) {
1012 /* silently ignore the rest of the block */
1015 ext4fs_dirhash(dir
, de
->name
, de
->name_len
, hinfo
);
1016 if ((hinfo
->hash
< start_hash
) ||
1017 ((hinfo
->hash
== start_hash
) &&
1018 (hinfo
->minor_hash
< start_minor_hash
)))
1022 if (!IS_ENCRYPTED(dir
)) {
1023 tmp_str
.name
= de
->name
;
1024 tmp_str
.len
= de
->name_len
;
1025 err
= ext4_htree_store_dirent(dir_file
,
1026 hinfo
->hash
, hinfo
->minor_hash
, de
,
1029 int save_len
= fname_crypto_str
.len
;
1030 struct fscrypt_str de_name
= FSTR_INIT(de
->name
,
1033 /* Directory is encrypted */
1034 err
= fscrypt_fname_disk_to_usr(dir
, hinfo
->hash
,
1035 hinfo
->minor_hash
, &de_name
,
1041 err
= ext4_htree_store_dirent(dir_file
,
1042 hinfo
->hash
, hinfo
->minor_hash
, de
,
1044 fname_crypto_str
.len
= save_len
;
1054 #ifdef CONFIG_FS_ENCRYPTION
1055 fscrypt_fname_free_buffer(&fname_crypto_str
);
1062 * This function fills a red-black tree with information from a
1063 * directory. We start scanning the directory in hash order, starting
1064 * at start_hash and start_minor_hash.
1066 * This function returns the number of entries inserted into the tree,
1067 * or a negative error code.
1069 int ext4_htree_fill_tree(struct file
*dir_file
, __u32 start_hash
,
1070 __u32 start_minor_hash
, __u32
*next_hash
)
1072 struct dx_hash_info hinfo
;
1073 struct ext4_dir_entry_2
*de
;
1074 struct dx_frame frames
[EXT4_HTREE_LEVEL
], *frame
;
1080 struct fscrypt_str tmp_str
;
1082 dxtrace(printk(KERN_DEBUG
"In htree_fill_tree, start hash: %x:%x\n",
1083 start_hash
, start_minor_hash
));
1084 dir
= file_inode(dir_file
);
1085 if (!(ext4_test_inode_flag(dir
, EXT4_INODE_INDEX
))) {
1086 hinfo
.hash_version
= EXT4_SB(dir
->i_sb
)->s_def_hash_version
;
1087 if (hinfo
.hash_version
<= DX_HASH_TEA
)
1088 hinfo
.hash_version
+=
1089 EXT4_SB(dir
->i_sb
)->s_hash_unsigned
;
1090 hinfo
.seed
= EXT4_SB(dir
->i_sb
)->s_hash_seed
;
1091 if (ext4_has_inline_data(dir
)) {
1092 int has_inline_data
= 1;
1093 count
= htree_inlinedir_to_tree(dir_file
, dir
, 0,
1097 if (has_inline_data
) {
1102 count
= htree_dirblock_to_tree(dir_file
, dir
, 0, &hinfo
,
1103 start_hash
, start_minor_hash
);
1107 hinfo
.hash
= start_hash
;
1108 hinfo
.minor_hash
= 0;
1109 frame
= dx_probe(NULL
, dir
, &hinfo
, frames
);
1111 return PTR_ERR(frame
);
1113 /* Add '.' and '..' from the htree header */
1114 if (!start_hash
&& !start_minor_hash
) {
1115 de
= (struct ext4_dir_entry_2
*) frames
[0].bh
->b_data
;
1116 tmp_str
.name
= de
->name
;
1117 tmp_str
.len
= de
->name_len
;
1118 err
= ext4_htree_store_dirent(dir_file
, 0, 0,
1124 if (start_hash
< 2 || (start_hash
==2 && start_minor_hash
==0)) {
1125 de
= (struct ext4_dir_entry_2
*) frames
[0].bh
->b_data
;
1126 de
= ext4_next_entry(de
, dir
->i_sb
->s_blocksize
);
1127 tmp_str
.name
= de
->name
;
1128 tmp_str
.len
= de
->name_len
;
1129 err
= ext4_htree_store_dirent(dir_file
, 2, 0,
1137 if (fatal_signal_pending(current
)) {
1142 block
= dx_get_block(frame
->at
);
1143 ret
= htree_dirblock_to_tree(dir_file
, dir
, block
, &hinfo
,
1144 start_hash
, start_minor_hash
);
1151 ret
= ext4_htree_next_block(dir
, HASH_NB_ALWAYS
,
1152 frame
, frames
, &hashval
);
1153 *next_hash
= hashval
;
1159 * Stop if: (a) there are no more entries, or
1160 * (b) we have inserted at least one entry and the
1161 * next hash value is not a continuation
1164 (count
&& ((hashval
& 1) == 0)))
1168 dxtrace(printk(KERN_DEBUG
"Fill tree: returned %d entries, "
1169 "next hash: %x\n", count
, *next_hash
));
1176 static inline int search_dirblock(struct buffer_head
*bh
,
1178 struct ext4_filename
*fname
,
1179 unsigned int offset
,
1180 struct ext4_dir_entry_2
**res_dir
)
1182 return ext4_search_dir(bh
, bh
->b_data
, dir
->i_sb
->s_blocksize
, dir
,
1183 fname
, offset
, res_dir
);
1187 * Directory block splitting, compacting
1191 * Create map of hash values, offsets, and sizes, stored at end of block.
1192 * Returns number of entries mapped.
1194 static int dx_make_map(struct inode
*dir
, struct ext4_dir_entry_2
*de
,
1195 unsigned blocksize
, struct dx_hash_info
*hinfo
,
1196 struct dx_map_entry
*map_tail
)
1199 char *base
= (char *) de
;
1200 struct dx_hash_info h
= *hinfo
;
1202 while ((char *) de
< base
+ blocksize
) {
1203 if (de
->name_len
&& de
->inode
) {
1204 ext4fs_dirhash(dir
, de
->name
, de
->name_len
, &h
);
1206 map_tail
->hash
= h
.hash
;
1207 map_tail
->offs
= ((char *) de
- base
)>>2;
1208 map_tail
->size
= le16_to_cpu(de
->rec_len
);
1212 /* XXX: do we need to check rec_len == 0 case? -Chris */
1213 de
= ext4_next_entry(de
, blocksize
);
1218 /* Sort map by hash value */
1219 static void dx_sort_map (struct dx_map_entry
*map
, unsigned count
)
1221 struct dx_map_entry
*p
, *q
, *top
= map
+ count
- 1;
1223 /* Combsort until bubble sort doesn't suck */
1225 count
= count
*10/13;
1226 if (count
- 9 < 2) /* 9, 10 -> 11 */
1228 for (p
= top
, q
= p
- count
; q
>= map
; p
--, q
--)
1229 if (p
->hash
< q
->hash
)
1232 /* Garden variety bubble sort */
1237 if (q
[1].hash
>= q
[0].hash
)
1245 static void dx_insert_block(struct dx_frame
*frame
, u32 hash
, ext4_lblk_t block
)
1247 struct dx_entry
*entries
= frame
->entries
;
1248 struct dx_entry
*old
= frame
->at
, *new = old
+ 1;
1249 int count
= dx_get_count(entries
);
1251 assert(count
< dx_get_limit(entries
));
1252 assert(old
< entries
+ count
);
1253 memmove(new + 1, new, (char *)(entries
+ count
) - (char *)(new));
1254 dx_set_hash(new, hash
);
1255 dx_set_block(new, block
);
1256 dx_set_count(entries
, count
+ 1);
1259 #ifdef CONFIG_UNICODE
1261 * Test whether a case-insensitive directory entry matches the filename
1262 * being searched for.
1264 * Returns: 0 if the directory entry matches, more than 0 if it
1265 * doesn't match or less than zero on error.
1267 int ext4_ci_compare(const struct inode
*parent
, const struct qstr
*name
,
1268 const struct qstr
*entry
)
1270 const struct ext4_sb_info
*sbi
= EXT4_SB(parent
->i_sb
);
1271 const struct unicode_map
*um
= sbi
->s_encoding
;
1274 ret
= utf8_strncasecmp(um
, name
, entry
);
1276 /* Handle invalid character sequence as either an error
1277 * or as an opaque byte sequence.
1279 if (ext4_has_strict_mode(sbi
))
1282 if (name
->len
!= entry
->len
)
1285 return !!memcmp(name
->name
, entry
->name
, name
->len
);
1293 * Test whether a directory entry matches the filename being searched for.
1295 * Return: %true if the directory entry matches, otherwise %false.
1297 static inline bool ext4_match(const struct inode
*parent
,
1298 const struct ext4_filename
*fname
,
1299 const struct ext4_dir_entry_2
*de
)
1301 struct fscrypt_name f
;
1302 #ifdef CONFIG_UNICODE
1303 const struct qstr entry
= {.name
= de
->name
, .len
= de
->name_len
};
1309 f
.usr_fname
= fname
->usr_fname
;
1310 f
.disk_name
= fname
->disk_name
;
1311 #ifdef CONFIG_FS_ENCRYPTION
1312 f
.crypto_buf
= fname
->crypto_buf
;
1315 #ifdef CONFIG_UNICODE
1316 if (EXT4_SB(parent
->i_sb
)->s_encoding
&& IS_CASEFOLDED(parent
))
1317 return (ext4_ci_compare(parent
, fname
->usr_fname
, &entry
) == 0);
1320 return fscrypt_match_name(&f
, de
->name
, de
->name_len
);
1324 * Returns 0 if not found, -1 on failure, and 1 on success
1326 int ext4_search_dir(struct buffer_head
*bh
, char *search_buf
, int buf_size
,
1327 struct inode
*dir
, struct ext4_filename
*fname
,
1328 unsigned int offset
, struct ext4_dir_entry_2
**res_dir
)
1330 struct ext4_dir_entry_2
* de
;
1334 de
= (struct ext4_dir_entry_2
*)search_buf
;
1335 dlimit
= search_buf
+ buf_size
;
1336 while ((char *) de
< dlimit
) {
1337 /* this code is executed quadratically often */
1338 /* do minimal checking `by hand' */
1339 if ((char *) de
+ de
->name_len
<= dlimit
&&
1340 ext4_match(dir
, fname
, de
)) {
1341 /* found a match - just to be sure, do
1343 if (ext4_check_dir_entry(dir
, NULL
, de
, bh
, bh
->b_data
,
1344 bh
->b_size
, offset
))
1349 /* prevent looping on a bad block */
1350 de_len
= ext4_rec_len_from_disk(de
->rec_len
,
1351 dir
->i_sb
->s_blocksize
);
1355 de
= (struct ext4_dir_entry_2
*) ((char *) de
+ de_len
);
1360 static int is_dx_internal_node(struct inode
*dir
, ext4_lblk_t block
,
1361 struct ext4_dir_entry
*de
)
1363 struct super_block
*sb
= dir
->i_sb
;
1369 if (de
->inode
== 0 &&
1370 ext4_rec_len_from_disk(de
->rec_len
, sb
->s_blocksize
) ==
1377 * __ext4_find_entry()
1379 * finds an entry in the specified directory with the wanted name. It
1380 * returns the cache buffer in which the entry was found, and the entry
1381 * itself (as a parameter - res_dir). It does NOT read the inode of the
1382 * entry - you'll have to do that yourself if you want to.
1384 * The returned buffer_head has ->b_count elevated. The caller is expected
1385 * to brelse() it when appropriate.
1387 static struct buffer_head
*__ext4_find_entry(struct inode
*dir
,
1388 struct ext4_filename
*fname
,
1389 struct ext4_dir_entry_2
**res_dir
,
1392 struct super_block
*sb
;
1393 struct buffer_head
*bh_use
[NAMEI_RA_SIZE
];
1394 struct buffer_head
*bh
, *ret
= NULL
;
1395 ext4_lblk_t start
, block
;
1396 const u8
*name
= fname
->usr_fname
->name
;
1397 size_t ra_max
= 0; /* Number of bh's in the readahead
1399 size_t ra_ptr
= 0; /* Current index into readahead
1401 ext4_lblk_t nblocks
;
1402 int i
, namelen
, retval
;
1406 namelen
= fname
->usr_fname
->len
;
1407 if (namelen
> EXT4_NAME_LEN
)
1410 if (ext4_has_inline_data(dir
)) {
1411 int has_inline_data
= 1;
1412 ret
= ext4_find_inline_entry(dir
, fname
, res_dir
,
1414 if (has_inline_data
) {
1417 goto cleanup_and_exit
;
1421 if ((namelen
<= 2) && (name
[0] == '.') &&
1422 (name
[1] == '.' || name
[1] == '\0')) {
1424 * "." or ".." will only be in the first block
1425 * NFS may look up ".."; "." should be handled by the VFS
1432 ret
= ext4_dx_find_entry(dir
, fname
, res_dir
);
1434 * On success, or if the error was file not found,
1435 * return. Otherwise, fall back to doing a search the
1436 * old fashioned way.
1438 if (!IS_ERR(ret
) || PTR_ERR(ret
) != ERR_BAD_DX_DIR
)
1439 goto cleanup_and_exit
;
1440 dxtrace(printk(KERN_DEBUG
"ext4_find_entry: dx failed, "
1444 nblocks
= dir
->i_size
>> EXT4_BLOCK_SIZE_BITS(sb
);
1447 goto cleanup_and_exit
;
1449 start
= EXT4_I(dir
)->i_dir_start_lookup
;
1450 if (start
>= nblocks
)
1456 * We deal with the read-ahead logic here.
1458 if (ra_ptr
>= ra_max
) {
1459 /* Refill the readahead buffer */
1462 ra_max
= start
- block
;
1464 ra_max
= nblocks
- block
;
1465 ra_max
= min(ra_max
, ARRAY_SIZE(bh_use
));
1466 retval
= ext4_bread_batch(dir
, block
, ra_max
,
1467 false /* wait */, bh_use
);
1469 ret
= ERR_PTR(retval
);
1471 goto cleanup_and_exit
;
1474 if ((bh
= bh_use
[ra_ptr
++]) == NULL
)
1477 if (!buffer_uptodate(bh
)) {
1478 EXT4_ERROR_INODE(dir
, "reading directory lblock %lu",
1479 (unsigned long) block
);
1481 ret
= ERR_PTR(-EIO
);
1482 goto cleanup_and_exit
;
1484 if (!buffer_verified(bh
) &&
1485 !is_dx_internal_node(dir
, block
,
1486 (struct ext4_dir_entry
*)bh
->b_data
) &&
1487 !ext4_dirent_csum_verify(dir
,
1488 (struct ext4_dir_entry
*)bh
->b_data
)) {
1489 EXT4_ERROR_INODE(dir
, "checksumming directory "
1490 "block %lu", (unsigned long)block
);
1492 ret
= ERR_PTR(-EFSBADCRC
);
1493 goto cleanup_and_exit
;
1495 set_buffer_verified(bh
);
1496 i
= search_dirblock(bh
, dir
, fname
,
1497 block
<< EXT4_BLOCK_SIZE_BITS(sb
), res_dir
);
1499 EXT4_I(dir
)->i_dir_start_lookup
= block
;
1501 goto cleanup_and_exit
;
1505 goto cleanup_and_exit
;
1508 if (++block
>= nblocks
)
1510 } while (block
!= start
);
1513 * If the directory has grown while we were searching, then
1514 * search the last part of the directory before giving up.
1517 nblocks
= dir
->i_size
>> EXT4_BLOCK_SIZE_BITS(sb
);
1518 if (block
< nblocks
) {
1524 /* Clean up the read-ahead blocks */
1525 for (; ra_ptr
< ra_max
; ra_ptr
++)
1526 brelse(bh_use
[ra_ptr
]);
1530 static struct buffer_head
*ext4_find_entry(struct inode
*dir
,
1531 const struct qstr
*d_name
,
1532 struct ext4_dir_entry_2
**res_dir
,
1536 struct ext4_filename fname
;
1537 struct buffer_head
*bh
;
1539 err
= ext4_fname_setup_filename(dir
, d_name
, 1, &fname
);
1543 return ERR_PTR(err
);
1545 bh
= __ext4_find_entry(dir
, &fname
, res_dir
, inlined
);
1547 ext4_fname_free_filename(&fname
);
1551 static struct buffer_head
*ext4_lookup_entry(struct inode
*dir
,
1552 struct dentry
*dentry
,
1553 struct ext4_dir_entry_2
**res_dir
)
1556 struct ext4_filename fname
;
1557 struct buffer_head
*bh
;
1559 err
= ext4_fname_prepare_lookup(dir
, dentry
, &fname
);
1563 return ERR_PTR(err
);
1565 bh
= __ext4_find_entry(dir
, &fname
, res_dir
, NULL
);
1567 ext4_fname_free_filename(&fname
);
1571 static struct buffer_head
* ext4_dx_find_entry(struct inode
*dir
,
1572 struct ext4_filename
*fname
,
1573 struct ext4_dir_entry_2
**res_dir
)
1575 struct super_block
* sb
= dir
->i_sb
;
1576 struct dx_frame frames
[EXT4_HTREE_LEVEL
], *frame
;
1577 struct buffer_head
*bh
;
1581 #ifdef CONFIG_FS_ENCRYPTION
1584 frame
= dx_probe(fname
, dir
, NULL
, frames
);
1586 return (struct buffer_head
*) frame
;
1588 block
= dx_get_block(frame
->at
);
1589 bh
= ext4_read_dirblock(dir
, block
, DIRENT
);
1593 retval
= search_dirblock(bh
, dir
, fname
,
1594 block
<< EXT4_BLOCK_SIZE_BITS(sb
),
1600 bh
= ERR_PTR(ERR_BAD_DX_DIR
);
1604 /* Check to see if we should continue to search */
1605 retval
= ext4_htree_next_block(dir
, fname
->hinfo
.hash
, frame
,
1608 ext4_warning_inode(dir
,
1609 "error %d reading directory index block",
1611 bh
= ERR_PTR(retval
);
1614 } while (retval
== 1);
1618 dxtrace(printk(KERN_DEBUG
"%s not found\n", fname
->usr_fname
->name
));
1624 static struct dentry
*ext4_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
1626 struct inode
*inode
;
1627 struct ext4_dir_entry_2
*de
;
1628 struct buffer_head
*bh
;
1630 if (dentry
->d_name
.len
> EXT4_NAME_LEN
)
1631 return ERR_PTR(-ENAMETOOLONG
);
1633 bh
= ext4_lookup_entry(dir
, dentry
, &de
);
1635 return ERR_CAST(bh
);
1638 __u32 ino
= le32_to_cpu(de
->inode
);
1640 if (!ext4_valid_inum(dir
->i_sb
, ino
)) {
1641 EXT4_ERROR_INODE(dir
, "bad inode number: %u", ino
);
1642 return ERR_PTR(-EFSCORRUPTED
);
1644 if (unlikely(ino
== dir
->i_ino
)) {
1645 EXT4_ERROR_INODE(dir
, "'%pd' linked to parent dir",
1647 return ERR_PTR(-EFSCORRUPTED
);
1649 inode
= ext4_iget(dir
->i_sb
, ino
, EXT4_IGET_NORMAL
);
1650 if (inode
== ERR_PTR(-ESTALE
)) {
1651 EXT4_ERROR_INODE(dir
,
1652 "deleted inode referenced: %u",
1654 return ERR_PTR(-EFSCORRUPTED
);
1656 if (!IS_ERR(inode
) && IS_ENCRYPTED(dir
) &&
1657 (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
)) &&
1658 !fscrypt_has_permitted_context(dir
, inode
)) {
1659 ext4_warning(inode
->i_sb
,
1660 "Inconsistent encryption contexts: %lu/%lu",
1661 dir
->i_ino
, inode
->i_ino
);
1663 return ERR_PTR(-EPERM
);
1667 #ifdef CONFIG_UNICODE
1668 if (!inode
&& IS_CASEFOLDED(dir
)) {
1669 /* Eventually we want to call d_add_ci(dentry, NULL)
1670 * for negative dentries in the encoding case as
1671 * well. For now, prevent the negative dentry
1672 * from being cached.
1677 return d_splice_alias(inode
, dentry
);
1681 struct dentry
*ext4_get_parent(struct dentry
*child
)
1684 static const struct qstr dotdot
= QSTR_INIT("..", 2);
1685 struct ext4_dir_entry_2
* de
;
1686 struct buffer_head
*bh
;
1688 bh
= ext4_find_entry(d_inode(child
), &dotdot
, &de
, NULL
);
1690 return ERR_CAST(bh
);
1692 return ERR_PTR(-ENOENT
);
1693 ino
= le32_to_cpu(de
->inode
);
1696 if (!ext4_valid_inum(child
->d_sb
, ino
)) {
1697 EXT4_ERROR_INODE(d_inode(child
),
1698 "bad parent inode number: %u", ino
);
1699 return ERR_PTR(-EFSCORRUPTED
);
1702 return d_obtain_alias(ext4_iget(child
->d_sb
, ino
, EXT4_IGET_NORMAL
));
1706 * Move count entries from end of map between two memory locations.
1707 * Returns pointer to last entry moved.
1709 static struct ext4_dir_entry_2
*
1710 dx_move_dirents(char *from
, char *to
, struct dx_map_entry
*map
, int count
,
1713 unsigned rec_len
= 0;
1716 struct ext4_dir_entry_2
*de
= (struct ext4_dir_entry_2
*)
1717 (from
+ (map
->offs
<<2));
1718 rec_len
= EXT4_DIR_REC_LEN(de
->name_len
);
1719 memcpy (to
, de
, rec_len
);
1720 ((struct ext4_dir_entry_2
*) to
)->rec_len
=
1721 ext4_rec_len_to_disk(rec_len
, blocksize
);
1726 return (struct ext4_dir_entry_2
*) (to
- rec_len
);
1730 * Compact each dir entry in the range to the minimal rec_len.
1731 * Returns pointer to last entry in range.
1733 static struct ext4_dir_entry_2
* dx_pack_dirents(char *base
, unsigned blocksize
)
1735 struct ext4_dir_entry_2
*next
, *to
, *prev
, *de
= (struct ext4_dir_entry_2
*) base
;
1736 unsigned rec_len
= 0;
1739 while ((char*)de
< base
+ blocksize
) {
1740 next
= ext4_next_entry(de
, blocksize
);
1741 if (de
->inode
&& de
->name_len
) {
1742 rec_len
= EXT4_DIR_REC_LEN(de
->name_len
);
1744 memmove(to
, de
, rec_len
);
1745 to
->rec_len
= ext4_rec_len_to_disk(rec_len
, blocksize
);
1747 to
= (struct ext4_dir_entry_2
*) (((char *) to
) + rec_len
);
1755 * Split a full leaf block to make room for a new dir entry.
1756 * Allocate a new block, and move entries so that they are approx. equally full.
1757 * Returns pointer to de in block into which the new entry will be inserted.
1759 static struct ext4_dir_entry_2
*do_split(handle_t
*handle
, struct inode
*dir
,
1760 struct buffer_head
**bh
,struct dx_frame
*frame
,
1761 struct dx_hash_info
*hinfo
)
1763 unsigned blocksize
= dir
->i_sb
->s_blocksize
;
1764 unsigned count
, continued
;
1765 struct buffer_head
*bh2
;
1766 ext4_lblk_t newblock
;
1768 struct dx_map_entry
*map
;
1769 char *data1
= (*bh
)->b_data
, *data2
;
1770 unsigned split
, move
, size
;
1771 struct ext4_dir_entry_2
*de
= NULL
, *de2
;
1772 struct ext4_dir_entry_tail
*t
;
1776 if (ext4_has_metadata_csum(dir
->i_sb
))
1777 csum_size
= sizeof(struct ext4_dir_entry_tail
);
1779 bh2
= ext4_append(handle
, dir
, &newblock
);
1783 return (struct ext4_dir_entry_2
*) bh2
;
1786 BUFFER_TRACE(*bh
, "get_write_access");
1787 err
= ext4_journal_get_write_access(handle
, *bh
);
1791 BUFFER_TRACE(frame
->bh
, "get_write_access");
1792 err
= ext4_journal_get_write_access(handle
, frame
->bh
);
1796 data2
= bh2
->b_data
;
1798 /* create map in the end of data2 block */
1799 map
= (struct dx_map_entry
*) (data2
+ blocksize
);
1800 count
= dx_make_map(dir
, (struct ext4_dir_entry_2
*) data1
,
1801 blocksize
, hinfo
, map
);
1803 dx_sort_map(map
, count
);
1804 /* Split the existing block in the middle, size-wise */
1807 for (i
= count
-1; i
>= 0; i
--) {
1808 /* is more than half of this entry in 2nd half of the block? */
1809 if (size
+ map
[i
].size
/2 > blocksize
/2)
1811 size
+= map
[i
].size
;
1814 /* map index at which we will split */
1815 split
= count
- move
;
1816 hash2
= map
[split
].hash
;
1817 continued
= hash2
== map
[split
- 1].hash
;
1818 dxtrace(printk(KERN_INFO
"Split block %lu at %x, %i/%i\n",
1819 (unsigned long)dx_get_block(frame
->at
),
1820 hash2
, split
, count
-split
));
1822 /* Fancy dance to stay within two buffers */
1823 de2
= dx_move_dirents(data1
, data2
, map
+ split
, count
- split
,
1825 de
= dx_pack_dirents(data1
, blocksize
);
1826 de
->rec_len
= ext4_rec_len_to_disk(data1
+ (blocksize
- csum_size
) -
1829 de2
->rec_len
= ext4_rec_len_to_disk(data2
+ (blocksize
- csum_size
) -
1833 t
= EXT4_DIRENT_TAIL(data2
, blocksize
);
1834 initialize_dirent_tail(t
, blocksize
);
1836 t
= EXT4_DIRENT_TAIL(data1
, blocksize
);
1837 initialize_dirent_tail(t
, blocksize
);
1840 dxtrace(dx_show_leaf(dir
, hinfo
, (struct ext4_dir_entry_2
*) data1
,
1842 dxtrace(dx_show_leaf(dir
, hinfo
, (struct ext4_dir_entry_2
*) data2
,
1845 /* Which block gets the new entry? */
1846 if (hinfo
->hash
>= hash2
) {
1850 dx_insert_block(frame
, hash2
+ continued
, newblock
);
1851 err
= ext4_handle_dirty_dirent_node(handle
, dir
, bh2
);
1854 err
= ext4_handle_dirty_dx_node(handle
, dir
, frame
->bh
);
1858 dxtrace(dx_show_index("frame", frame
->entries
));
1865 ext4_std_error(dir
->i_sb
, err
);
1866 return ERR_PTR(err
);
1869 int ext4_find_dest_de(struct inode
*dir
, struct inode
*inode
,
1870 struct buffer_head
*bh
,
1871 void *buf
, int buf_size
,
1872 struct ext4_filename
*fname
,
1873 struct ext4_dir_entry_2
**dest_de
)
1875 struct ext4_dir_entry_2
*de
;
1876 unsigned short reclen
= EXT4_DIR_REC_LEN(fname_len(fname
));
1878 unsigned int offset
= 0;
1881 de
= (struct ext4_dir_entry_2
*)buf
;
1882 top
= buf
+ buf_size
- reclen
;
1883 while ((char *) de
<= top
) {
1884 if (ext4_check_dir_entry(dir
, NULL
, de
, bh
,
1885 buf
, buf_size
, offset
))
1886 return -EFSCORRUPTED
;
1887 if (ext4_match(dir
, fname
, de
))
1889 nlen
= EXT4_DIR_REC_LEN(de
->name_len
);
1890 rlen
= ext4_rec_len_from_disk(de
->rec_len
, buf_size
);
1891 if ((de
->inode
? rlen
- nlen
: rlen
) >= reclen
)
1893 de
= (struct ext4_dir_entry_2
*)((char *)de
+ rlen
);
1896 if ((char *) de
> top
)
1903 void ext4_insert_dentry(struct inode
*inode
,
1904 struct ext4_dir_entry_2
*de
,
1906 struct ext4_filename
*fname
)
1911 nlen
= EXT4_DIR_REC_LEN(de
->name_len
);
1912 rlen
= ext4_rec_len_from_disk(de
->rec_len
, buf_size
);
1914 struct ext4_dir_entry_2
*de1
=
1915 (struct ext4_dir_entry_2
*)((char *)de
+ nlen
);
1916 de1
->rec_len
= ext4_rec_len_to_disk(rlen
- nlen
, buf_size
);
1917 de
->rec_len
= ext4_rec_len_to_disk(nlen
, buf_size
);
1920 de
->file_type
= EXT4_FT_UNKNOWN
;
1921 de
->inode
= cpu_to_le32(inode
->i_ino
);
1922 ext4_set_de_type(inode
->i_sb
, de
, inode
->i_mode
);
1923 de
->name_len
= fname_len(fname
);
1924 memcpy(de
->name
, fname_name(fname
), fname_len(fname
));
1928 * Add a new entry into a directory (leaf) block. If de is non-NULL,
1929 * it points to a directory entry which is guaranteed to be large
1930 * enough for new directory entry. If de is NULL, then
1931 * add_dirent_to_buf will attempt search the directory block for
1932 * space. It will return -ENOSPC if no space is available, and -EIO
1933 * and -EEXIST if directory entry already exists.
1935 static int add_dirent_to_buf(handle_t
*handle
, struct ext4_filename
*fname
,
1937 struct inode
*inode
, struct ext4_dir_entry_2
*de
,
1938 struct buffer_head
*bh
)
1940 unsigned int blocksize
= dir
->i_sb
->s_blocksize
;
1944 if (ext4_has_metadata_csum(inode
->i_sb
))
1945 csum_size
= sizeof(struct ext4_dir_entry_tail
);
1948 err
= ext4_find_dest_de(dir
, inode
, bh
, bh
->b_data
,
1949 blocksize
- csum_size
, fname
, &de
);
1953 BUFFER_TRACE(bh
, "get_write_access");
1954 err
= ext4_journal_get_write_access(handle
, bh
);
1956 ext4_std_error(dir
->i_sb
, err
);
1960 /* By now the buffer is marked for journaling */
1961 ext4_insert_dentry(inode
, de
, blocksize
, fname
);
1964 * XXX shouldn't update any times until successful
1965 * completion of syscall, but too many callers depend
1968 * XXX similarly, too many callers depend on
1969 * ext4_new_inode() setting the times, but error
1970 * recovery deletes the inode, so the worst that can
1971 * happen is that the times are slightly out of date
1972 * and/or different from the directory change time.
1974 dir
->i_mtime
= dir
->i_ctime
= current_time(dir
);
1975 ext4_update_dx_flag(dir
);
1976 inode_inc_iversion(dir
);
1977 ext4_mark_inode_dirty(handle
, dir
);
1978 BUFFER_TRACE(bh
, "call ext4_handle_dirty_metadata");
1979 err
= ext4_handle_dirty_dirent_node(handle
, dir
, bh
);
1981 ext4_std_error(dir
->i_sb
, err
);
1986 * This converts a one block unindexed directory to a 3 block indexed
1987 * directory, and adds the dentry to the indexed directory.
1989 static int make_indexed_dir(handle_t
*handle
, struct ext4_filename
*fname
,
1991 struct inode
*inode
, struct buffer_head
*bh
)
1993 struct buffer_head
*bh2
;
1994 struct dx_root
*root
;
1995 struct dx_frame frames
[EXT4_HTREE_LEVEL
], *frame
;
1996 struct dx_entry
*entries
;
1997 struct ext4_dir_entry_2
*de
, *de2
;
1998 struct ext4_dir_entry_tail
*t
;
2004 struct fake_dirent
*fde
;
2007 if (ext4_has_metadata_csum(inode
->i_sb
))
2008 csum_size
= sizeof(struct ext4_dir_entry_tail
);
2010 blocksize
= dir
->i_sb
->s_blocksize
;
2011 dxtrace(printk(KERN_DEBUG
"Creating index: inode %lu\n", dir
->i_ino
));
2012 BUFFER_TRACE(bh
, "get_write_access");
2013 retval
= ext4_journal_get_write_access(handle
, bh
);
2015 ext4_std_error(dir
->i_sb
, retval
);
2019 root
= (struct dx_root
*) bh
->b_data
;
2021 /* The 0th block becomes the root, move the dirents out */
2022 fde
= &root
->dotdot
;
2023 de
= (struct ext4_dir_entry_2
*)((char *)fde
+
2024 ext4_rec_len_from_disk(fde
->rec_len
, blocksize
));
2025 if ((char *) de
>= (((char *) root
) + blocksize
)) {
2026 EXT4_ERROR_INODE(dir
, "invalid rec_len for '..'");
2028 return -EFSCORRUPTED
;
2030 len
= ((char *) root
) + (blocksize
- csum_size
) - (char *) de
;
2032 /* Allocate new block for the 0th block's dirents */
2033 bh2
= ext4_append(handle
, dir
, &block
);
2036 return PTR_ERR(bh2
);
2038 ext4_set_inode_flag(dir
, EXT4_INODE_INDEX
);
2039 data1
= bh2
->b_data
;
2041 memcpy (data1
, de
, len
);
2042 de
= (struct ext4_dir_entry_2
*) data1
;
2044 while ((char *)(de2
= ext4_next_entry(de
, blocksize
)) < top
)
2046 de
->rec_len
= ext4_rec_len_to_disk(data1
+ (blocksize
- csum_size
) -
2051 t
= EXT4_DIRENT_TAIL(data1
, blocksize
);
2052 initialize_dirent_tail(t
, blocksize
);
2055 /* Initialize the root; the dot dirents already exist */
2056 de
= (struct ext4_dir_entry_2
*) (&root
->dotdot
);
2057 de
->rec_len
= ext4_rec_len_to_disk(blocksize
- EXT4_DIR_REC_LEN(2),
2059 memset (&root
->info
, 0, sizeof(root
->info
));
2060 root
->info
.info_length
= sizeof(root
->info
);
2061 root
->info
.hash_version
= EXT4_SB(dir
->i_sb
)->s_def_hash_version
;
2062 entries
= root
->entries
;
2063 dx_set_block(entries
, 1);
2064 dx_set_count(entries
, 1);
2065 dx_set_limit(entries
, dx_root_limit(dir
, sizeof(root
->info
)));
2067 /* Initialize as for dx_probe */
2068 fname
->hinfo
.hash_version
= root
->info
.hash_version
;
2069 if (fname
->hinfo
.hash_version
<= DX_HASH_TEA
)
2070 fname
->hinfo
.hash_version
+= EXT4_SB(dir
->i_sb
)->s_hash_unsigned
;
2071 fname
->hinfo
.seed
= EXT4_SB(dir
->i_sb
)->s_hash_seed
;
2072 ext4fs_dirhash(dir
, fname_name(fname
), fname_len(fname
), &fname
->hinfo
);
2074 memset(frames
, 0, sizeof(frames
));
2076 frame
->entries
= entries
;
2077 frame
->at
= entries
;
2080 retval
= ext4_handle_dirty_dx_node(handle
, dir
, frame
->bh
);
2083 retval
= ext4_handle_dirty_dirent_node(handle
, dir
, bh2
);
2087 de
= do_split(handle
,dir
, &bh2
, frame
, &fname
->hinfo
);
2089 retval
= PTR_ERR(de
);
2093 retval
= add_dirent_to_buf(handle
, fname
, dir
, inode
, de
, bh2
);
2096 * Even if the block split failed, we have to properly write
2097 * out all the changes we did so far. Otherwise we can end up
2098 * with corrupted filesystem.
2101 ext4_mark_inode_dirty(handle
, dir
);
2110 * adds a file entry to the specified directory, using the same
2111 * semantics as ext4_find_entry(). It returns NULL if it failed.
2113 * NOTE!! The inode part of 'de' is left at 0 - which means you
2114 * may not sleep between calling this and putting something into
2115 * the entry, as someone else might have used it while you slept.
2117 static int ext4_add_entry(handle_t
*handle
, struct dentry
*dentry
,
2118 struct inode
*inode
)
2120 struct inode
*dir
= d_inode(dentry
->d_parent
);
2121 struct buffer_head
*bh
= NULL
;
2122 struct ext4_dir_entry_2
*de
;
2123 struct ext4_dir_entry_tail
*t
;
2124 struct super_block
*sb
;
2125 struct ext4_sb_info
*sbi
;
2126 struct ext4_filename fname
;
2130 ext4_lblk_t block
, blocks
;
2133 if (ext4_has_metadata_csum(inode
->i_sb
))
2134 csum_size
= sizeof(struct ext4_dir_entry_tail
);
2138 blocksize
= sb
->s_blocksize
;
2139 if (!dentry
->d_name
.len
)
2142 #ifdef CONFIG_UNICODE
2143 if (ext4_has_strict_mode(sbi
) && IS_CASEFOLDED(dir
) &&
2144 utf8_validate(sbi
->s_encoding
, &dentry
->d_name
))
2148 retval
= ext4_fname_setup_filename(dir
, &dentry
->d_name
, 0, &fname
);
2152 if (ext4_has_inline_data(dir
)) {
2153 retval
= ext4_try_add_inline_entry(handle
, &fname
, dir
, inode
);
2163 retval
= ext4_dx_add_entry(handle
, &fname
, dir
, inode
);
2164 if (!retval
|| (retval
!= ERR_BAD_DX_DIR
))
2166 ext4_clear_inode_flag(dir
, EXT4_INODE_INDEX
);
2168 ext4_mark_inode_dirty(handle
, dir
);
2170 blocks
= dir
->i_size
>> sb
->s_blocksize_bits
;
2171 for (block
= 0; block
< blocks
; block
++) {
2172 bh
= ext4_read_dirblock(dir
, block
, DIRENT
);
2174 retval
= PTR_ERR(bh
);
2178 retval
= add_dirent_to_buf(handle
, &fname
, dir
, inode
,
2180 if (retval
!= -ENOSPC
)
2183 if (blocks
== 1 && !dx_fallback
&&
2184 ext4_has_feature_dir_index(sb
)) {
2185 retval
= make_indexed_dir(handle
, &fname
, dir
,
2187 bh
= NULL
; /* make_indexed_dir releases bh */
2192 bh
= ext4_append(handle
, dir
, &block
);
2194 retval
= PTR_ERR(bh
);
2198 de
= (struct ext4_dir_entry_2
*) bh
->b_data
;
2200 de
->rec_len
= ext4_rec_len_to_disk(blocksize
- csum_size
, blocksize
);
2203 t
= EXT4_DIRENT_TAIL(bh
->b_data
, blocksize
);
2204 initialize_dirent_tail(t
, blocksize
);
2207 retval
= add_dirent_to_buf(handle
, &fname
, dir
, inode
, de
, bh
);
2209 ext4_fname_free_filename(&fname
);
2212 ext4_set_inode_state(inode
, EXT4_STATE_NEWENTRY
);
2217 * Returns 0 for success, or a negative error value
2219 static int ext4_dx_add_entry(handle_t
*handle
, struct ext4_filename
*fname
,
2220 struct inode
*dir
, struct inode
*inode
)
2222 struct dx_frame frames
[EXT4_HTREE_LEVEL
], *frame
;
2223 struct dx_entry
*entries
, *at
;
2224 struct buffer_head
*bh
;
2225 struct super_block
*sb
= dir
->i_sb
;
2226 struct ext4_dir_entry_2
*de
;
2232 frame
= dx_probe(fname
, dir
, NULL
, frames
);
2234 return PTR_ERR(frame
);
2235 entries
= frame
->entries
;
2237 bh
= ext4_read_dirblock(dir
, dx_get_block(frame
->at
), DIRENT
);
2244 BUFFER_TRACE(bh
, "get_write_access");
2245 err
= ext4_journal_get_write_access(handle
, bh
);
2249 err
= add_dirent_to_buf(handle
, fname
, dir
, inode
, NULL
, bh
);
2254 /* Block full, should compress but for now just split */
2255 dxtrace(printk(KERN_DEBUG
"using %u of %u node entries\n",
2256 dx_get_count(entries
), dx_get_limit(entries
)));
2257 /* Need to split index? */
2258 if (dx_get_count(entries
) == dx_get_limit(entries
)) {
2259 ext4_lblk_t newblock
;
2260 int levels
= frame
- frames
+ 1;
2261 unsigned int icount
;
2263 struct dx_entry
*entries2
;
2264 struct dx_node
*node2
;
2265 struct buffer_head
*bh2
;
2267 while (frame
> frames
) {
2268 if (dx_get_count((frame
- 1)->entries
) <
2269 dx_get_limit((frame
- 1)->entries
)) {
2273 frame
--; /* split higher index block */
2275 entries
= frame
->entries
;
2278 if (add_level
&& levels
== ext4_dir_htree_level(sb
)) {
2279 ext4_warning(sb
, "Directory (ino: %lu) index full, "
2280 "reach max htree level :%d",
2281 dir
->i_ino
, levels
);
2282 if (ext4_dir_htree_level(sb
) < EXT4_HTREE_LEVEL
) {
2283 ext4_warning(sb
, "Large directory feature is "
2284 "not enabled on this "
2290 icount
= dx_get_count(entries
);
2291 bh2
= ext4_append(handle
, dir
, &newblock
);
2296 node2
= (struct dx_node
*)(bh2
->b_data
);
2297 entries2
= node2
->entries
;
2298 memset(&node2
->fake
, 0, sizeof(struct fake_dirent
));
2299 node2
->fake
.rec_len
= ext4_rec_len_to_disk(sb
->s_blocksize
,
2301 BUFFER_TRACE(frame
->bh
, "get_write_access");
2302 err
= ext4_journal_get_write_access(handle
, frame
->bh
);
2306 unsigned icount1
= icount
/2, icount2
= icount
- icount1
;
2307 unsigned hash2
= dx_get_hash(entries
+ icount1
);
2308 dxtrace(printk(KERN_DEBUG
"Split index %i/%i\n",
2311 BUFFER_TRACE(frame
->bh
, "get_write_access"); /* index root */
2312 err
= ext4_journal_get_write_access(handle
,
2317 memcpy((char *) entries2
, (char *) (entries
+ icount1
),
2318 icount2
* sizeof(struct dx_entry
));
2319 dx_set_count(entries
, icount1
);
2320 dx_set_count(entries2
, icount2
);
2321 dx_set_limit(entries2
, dx_node_limit(dir
));
2323 /* Which index block gets the new entry? */
2324 if (at
- entries
>= icount1
) {
2325 frame
->at
= at
= at
- entries
- icount1
+ entries2
;
2326 frame
->entries
= entries
= entries2
;
2327 swap(frame
->bh
, bh2
);
2329 dx_insert_block((frame
- 1), hash2
, newblock
);
2330 dxtrace(dx_show_index("node", frame
->entries
));
2331 dxtrace(dx_show_index("node",
2332 ((struct dx_node
*) bh2
->b_data
)->entries
));
2333 err
= ext4_handle_dirty_dx_node(handle
, dir
, bh2
);
2337 err
= ext4_handle_dirty_dx_node(handle
, dir
,
2342 err
= ext4_handle_dirty_dx_node(handle
, dir
,
2347 struct dx_root
*dxroot
;
2348 memcpy((char *) entries2
, (char *) entries
,
2349 icount
* sizeof(struct dx_entry
));
2350 dx_set_limit(entries2
, dx_node_limit(dir
));
2353 dx_set_count(entries
, 1);
2354 dx_set_block(entries
+ 0, newblock
);
2355 dxroot
= (struct dx_root
*)frames
[0].bh
->b_data
;
2356 dxroot
->info
.indirect_levels
+= 1;
2357 dxtrace(printk(KERN_DEBUG
2358 "Creating %d level index...\n",
2359 dxroot
->info
.indirect_levels
));
2360 err
= ext4_handle_dirty_dx_node(handle
, dir
, frame
->bh
);
2363 err
= ext4_handle_dirty_dx_node(handle
, dir
, bh2
);
2369 de
= do_split(handle
, dir
, &bh
, frame
, &fname
->hinfo
);
2374 err
= add_dirent_to_buf(handle
, fname
, dir
, inode
, de
, bh
);
2378 ext4_std_error(dir
->i_sb
, err
); /* this is a no-op if err == 0 */
2382 /* @restart is true means htree-path has been changed, we need to
2383 * repeat dx_probe() to find out valid htree-path
2385 if (restart
&& err
== 0)
2391 * ext4_generic_delete_entry deletes a directory entry by merging it
2392 * with the previous entry
2394 int ext4_generic_delete_entry(handle_t
*handle
,
2396 struct ext4_dir_entry_2
*de_del
,
2397 struct buffer_head
*bh
,
2402 struct ext4_dir_entry_2
*de
, *pde
;
2403 unsigned int blocksize
= dir
->i_sb
->s_blocksize
;
2408 de
= (struct ext4_dir_entry_2
*)entry_buf
;
2409 while (i
< buf_size
- csum_size
) {
2410 if (ext4_check_dir_entry(dir
, NULL
, de
, bh
,
2411 bh
->b_data
, bh
->b_size
, i
))
2412 return -EFSCORRUPTED
;
2415 pde
->rec_len
= ext4_rec_len_to_disk(
2416 ext4_rec_len_from_disk(pde
->rec_len
,
2418 ext4_rec_len_from_disk(de
->rec_len
,
2423 inode_inc_iversion(dir
);
2426 i
+= ext4_rec_len_from_disk(de
->rec_len
, blocksize
);
2428 de
= ext4_next_entry(de
, blocksize
);
2433 static int ext4_delete_entry(handle_t
*handle
,
2435 struct ext4_dir_entry_2
*de_del
,
2436 struct buffer_head
*bh
)
2438 int err
, csum_size
= 0;
2440 if (ext4_has_inline_data(dir
)) {
2441 int has_inline_data
= 1;
2442 err
= ext4_delete_inline_entry(handle
, dir
, de_del
, bh
,
2444 if (has_inline_data
)
2448 if (ext4_has_metadata_csum(dir
->i_sb
))
2449 csum_size
= sizeof(struct ext4_dir_entry_tail
);
2451 BUFFER_TRACE(bh
, "get_write_access");
2452 err
= ext4_journal_get_write_access(handle
, bh
);
2456 err
= ext4_generic_delete_entry(handle
, dir
, de_del
,
2458 dir
->i_sb
->s_blocksize
, csum_size
);
2462 BUFFER_TRACE(bh
, "call ext4_handle_dirty_metadata");
2463 err
= ext4_handle_dirty_dirent_node(handle
, dir
, bh
);
2470 ext4_std_error(dir
->i_sb
, err
);
2475 * Set directory link count to 1 if nlinks > EXT4_LINK_MAX, or if nlinks == 2
2476 * since this indicates that nlinks count was previously 1 to avoid overflowing
2477 * the 16-bit i_links_count field on disk. Directories with i_nlink == 1 mean
2478 * that subdirectory link counts are not being maintained accurately.
2480 * The caller has already checked for i_nlink overflow in case the DIR_LINK
2481 * feature is not enabled and returned -EMLINK. The is_dx() check is a proxy
2482 * for checking S_ISDIR(inode) (since the INODE_INDEX feature will not be set
2483 * on regular files) and to avoid creating huge/slow non-HTREE directories.
2485 static void ext4_inc_count(handle_t
*handle
, struct inode
*inode
)
2489 (inode
->i_nlink
> EXT4_LINK_MAX
|| inode
->i_nlink
== 2))
2490 set_nlink(inode
, 1);
2494 * If a directory had nlink == 1, then we should let it be 1. This indicates
2495 * directory has >EXT4_LINK_MAX subdirs.
2497 static void ext4_dec_count(handle_t
*handle
, struct inode
*inode
)
2499 if (!S_ISDIR(inode
->i_mode
) || inode
->i_nlink
> 2)
2504 static int ext4_add_nondir(handle_t
*handle
,
2505 struct dentry
*dentry
, struct inode
*inode
)
2507 int err
= ext4_add_entry(handle
, dentry
, inode
);
2509 ext4_mark_inode_dirty(handle
, inode
);
2510 d_instantiate_new(dentry
, inode
);
2514 unlock_new_inode(inode
);
2520 * By the time this is called, we already have created
2521 * the directory cache entry for the new file, but it
2522 * is so far negative - it has no inode.
2524 * If the create succeeds, we fill in the inode information
2525 * with d_instantiate().
2527 static int ext4_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
2531 struct inode
*inode
;
2532 int err
, credits
, retries
= 0;
2534 err
= dquot_initialize(dir
);
2538 credits
= (EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
2539 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 3);
2541 inode
= ext4_new_inode_start_handle(dir
, mode
, &dentry
->d_name
, 0,
2542 NULL
, EXT4_HT_DIR
, credits
);
2543 handle
= ext4_journal_current_handle();
2544 err
= PTR_ERR(inode
);
2545 if (!IS_ERR(inode
)) {
2546 inode
->i_op
= &ext4_file_inode_operations
;
2547 inode
->i_fop
= &ext4_file_operations
;
2548 ext4_set_aops(inode
);
2549 err
= ext4_add_nondir(handle
, dentry
, inode
);
2550 if (!err
&& IS_DIRSYNC(dir
))
2551 ext4_handle_sync(handle
);
2554 ext4_journal_stop(handle
);
2555 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
2560 static int ext4_mknod(struct inode
*dir
, struct dentry
*dentry
,
2561 umode_t mode
, dev_t rdev
)
2564 struct inode
*inode
;
2565 int err
, credits
, retries
= 0;
2567 err
= dquot_initialize(dir
);
2571 credits
= (EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
2572 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 3);
2574 inode
= ext4_new_inode_start_handle(dir
, mode
, &dentry
->d_name
, 0,
2575 NULL
, EXT4_HT_DIR
, credits
);
2576 handle
= ext4_journal_current_handle();
2577 err
= PTR_ERR(inode
);
2578 if (!IS_ERR(inode
)) {
2579 init_special_inode(inode
, inode
->i_mode
, rdev
);
2580 inode
->i_op
= &ext4_special_inode_operations
;
2581 err
= ext4_add_nondir(handle
, dentry
, inode
);
2582 if (!err
&& IS_DIRSYNC(dir
))
2583 ext4_handle_sync(handle
);
2586 ext4_journal_stop(handle
);
2587 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
2592 static int ext4_tmpfile(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
2595 struct inode
*inode
;
2596 int err
, retries
= 0;
2598 err
= dquot_initialize(dir
);
2603 inode
= ext4_new_inode_start_handle(dir
, mode
,
2606 EXT4_MAXQUOTAS_INIT_BLOCKS(dir
->i_sb
) +
2607 4 + EXT4_XATTR_TRANS_BLOCKS
);
2608 handle
= ext4_journal_current_handle();
2609 err
= PTR_ERR(inode
);
2610 if (!IS_ERR(inode
)) {
2611 inode
->i_op
= &ext4_file_inode_operations
;
2612 inode
->i_fop
= &ext4_file_operations
;
2613 ext4_set_aops(inode
);
2614 d_tmpfile(dentry
, inode
);
2615 err
= ext4_orphan_add(handle
, inode
);
2617 goto err_unlock_inode
;
2618 mark_inode_dirty(inode
);
2619 unlock_new_inode(inode
);
2622 ext4_journal_stop(handle
);
2623 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
2627 ext4_journal_stop(handle
);
2628 unlock_new_inode(inode
);
2632 struct ext4_dir_entry_2
*ext4_init_dot_dotdot(struct inode
*inode
,
2633 struct ext4_dir_entry_2
*de
,
2634 int blocksize
, int csum_size
,
2635 unsigned int parent_ino
, int dotdot_real_len
)
2637 de
->inode
= cpu_to_le32(inode
->i_ino
);
2639 de
->rec_len
= ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de
->name_len
),
2641 strcpy(de
->name
, ".");
2642 ext4_set_de_type(inode
->i_sb
, de
, S_IFDIR
);
2644 de
= ext4_next_entry(de
, blocksize
);
2645 de
->inode
= cpu_to_le32(parent_ino
);
2647 if (!dotdot_real_len
)
2648 de
->rec_len
= ext4_rec_len_to_disk(blocksize
-
2649 (csum_size
+ EXT4_DIR_REC_LEN(1)),
2652 de
->rec_len
= ext4_rec_len_to_disk(
2653 EXT4_DIR_REC_LEN(de
->name_len
), blocksize
);
2654 strcpy(de
->name
, "..");
2655 ext4_set_de_type(inode
->i_sb
, de
, S_IFDIR
);
2657 return ext4_next_entry(de
, blocksize
);
2660 static int ext4_init_new_dir(handle_t
*handle
, struct inode
*dir
,
2661 struct inode
*inode
)
2663 struct buffer_head
*dir_block
= NULL
;
2664 struct ext4_dir_entry_2
*de
;
2665 struct ext4_dir_entry_tail
*t
;
2666 ext4_lblk_t block
= 0;
2667 unsigned int blocksize
= dir
->i_sb
->s_blocksize
;
2671 if (ext4_has_metadata_csum(dir
->i_sb
))
2672 csum_size
= sizeof(struct ext4_dir_entry_tail
);
2674 if (ext4_test_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
)) {
2675 err
= ext4_try_create_inline_dir(handle
, dir
, inode
);
2676 if (err
< 0 && err
!= -ENOSPC
)
2683 dir_block
= ext4_append(handle
, inode
, &block
);
2684 if (IS_ERR(dir_block
))
2685 return PTR_ERR(dir_block
);
2686 de
= (struct ext4_dir_entry_2
*)dir_block
->b_data
;
2687 ext4_init_dot_dotdot(inode
, de
, blocksize
, csum_size
, dir
->i_ino
, 0);
2688 set_nlink(inode
, 2);
2690 t
= EXT4_DIRENT_TAIL(dir_block
->b_data
, blocksize
);
2691 initialize_dirent_tail(t
, blocksize
);
2694 BUFFER_TRACE(dir_block
, "call ext4_handle_dirty_metadata");
2695 err
= ext4_handle_dirty_dirent_node(handle
, inode
, dir_block
);
2698 set_buffer_verified(dir_block
);
2704 static int ext4_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
2707 struct inode
*inode
;
2708 int err
, credits
, retries
= 0;
2710 if (EXT4_DIR_LINK_MAX(dir
))
2713 err
= dquot_initialize(dir
);
2717 credits
= (EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
2718 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 3);
2720 inode
= ext4_new_inode_start_handle(dir
, S_IFDIR
| mode
,
2722 0, NULL
, EXT4_HT_DIR
, credits
);
2723 handle
= ext4_journal_current_handle();
2724 err
= PTR_ERR(inode
);
2728 inode
->i_op
= &ext4_dir_inode_operations
;
2729 inode
->i_fop
= &ext4_dir_operations
;
2730 err
= ext4_init_new_dir(handle
, dir
, inode
);
2732 goto out_clear_inode
;
2733 err
= ext4_mark_inode_dirty(handle
, inode
);
2735 err
= ext4_add_entry(handle
, dentry
, inode
);
2739 unlock_new_inode(inode
);
2740 ext4_mark_inode_dirty(handle
, inode
);
2744 ext4_inc_count(handle
, dir
);
2745 ext4_update_dx_flag(dir
);
2746 err
= ext4_mark_inode_dirty(handle
, dir
);
2748 goto out_clear_inode
;
2749 d_instantiate_new(dentry
, inode
);
2750 if (IS_DIRSYNC(dir
))
2751 ext4_handle_sync(handle
);
2755 ext4_journal_stop(handle
);
2756 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
2762 * routine to check that the specified directory is empty (for rmdir)
2764 bool ext4_empty_dir(struct inode
*inode
)
2766 unsigned int offset
;
2767 struct buffer_head
*bh
;
2768 struct ext4_dir_entry_2
*de
, *de1
;
2769 struct super_block
*sb
;
2771 if (ext4_has_inline_data(inode
)) {
2772 int has_inline_data
= 1;
2775 ret
= empty_inline_dir(inode
, &has_inline_data
);
2776 if (has_inline_data
)
2781 if (inode
->i_size
< EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2)) {
2782 EXT4_ERROR_INODE(inode
, "invalid size");
2785 bh
= ext4_read_dirblock(inode
, 0, EITHER
);
2789 de
= (struct ext4_dir_entry_2
*) bh
->b_data
;
2790 de1
= ext4_next_entry(de
, sb
->s_blocksize
);
2791 if (le32_to_cpu(de
->inode
) != inode
->i_ino
||
2792 le32_to_cpu(de1
->inode
) == 0 ||
2793 strcmp(".", de
->name
) || strcmp("..", de1
->name
)) {
2794 ext4_warning_inode(inode
, "directory missing '.' and/or '..'");
2798 offset
= ext4_rec_len_from_disk(de
->rec_len
, sb
->s_blocksize
) +
2799 ext4_rec_len_from_disk(de1
->rec_len
, sb
->s_blocksize
);
2800 de
= ext4_next_entry(de1
, sb
->s_blocksize
);
2801 while (offset
< inode
->i_size
) {
2802 if ((void *) de
>= (void *) (bh
->b_data
+sb
->s_blocksize
)) {
2803 unsigned int lblock
;
2805 lblock
= offset
>> EXT4_BLOCK_SIZE_BITS(sb
);
2806 bh
= ext4_read_dirblock(inode
, lblock
, EITHER
);
2809 de
= (struct ext4_dir_entry_2
*) bh
->b_data
;
2811 if (ext4_check_dir_entry(inode
, NULL
, de
, bh
,
2812 bh
->b_data
, bh
->b_size
, offset
)) {
2813 de
= (struct ext4_dir_entry_2
*)(bh
->b_data
+
2815 offset
= (offset
| (sb
->s_blocksize
- 1)) + 1;
2818 if (le32_to_cpu(de
->inode
)) {
2822 offset
+= ext4_rec_len_from_disk(de
->rec_len
, sb
->s_blocksize
);
2823 de
= ext4_next_entry(de
, sb
->s_blocksize
);
2830 * ext4_orphan_add() links an unlinked or truncated inode into a list of
2831 * such inodes, starting at the superblock, in case we crash before the
2832 * file is closed/deleted, or in case the inode truncate spans multiple
2833 * transactions and the last transaction is not recovered after a crash.
2835 * At filesystem recovery time, we walk this list deleting unlinked
2836 * inodes and truncating linked inodes in ext4_orphan_cleanup().
2838 * Orphan list manipulation functions must be called under i_mutex unless
2839 * we are just creating the inode or deleting it.
2841 int ext4_orphan_add(handle_t
*handle
, struct inode
*inode
)
2843 struct super_block
*sb
= inode
->i_sb
;
2844 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2845 struct ext4_iloc iloc
;
2849 if (!sbi
->s_journal
|| is_bad_inode(inode
))
2852 WARN_ON_ONCE(!(inode
->i_state
& (I_NEW
| I_FREEING
)) &&
2853 !inode_is_locked(inode
));
2855 * Exit early if inode already is on orphan list. This is a big speedup
2856 * since we don't have to contend on the global s_orphan_lock.
2858 if (!list_empty(&EXT4_I(inode
)->i_orphan
))
2862 * Orphan handling is only valid for files with data blocks
2863 * being truncated, or files being unlinked. Note that we either
2864 * hold i_mutex, or the inode can not be referenced from outside,
2865 * so i_nlink should not be bumped due to race
2867 J_ASSERT((S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
2868 S_ISLNK(inode
->i_mode
)) || inode
->i_nlink
== 0);
2870 BUFFER_TRACE(sbi
->s_sbh
, "get_write_access");
2871 err
= ext4_journal_get_write_access(handle
, sbi
->s_sbh
);
2875 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
2879 mutex_lock(&sbi
->s_orphan_lock
);
2881 * Due to previous errors inode may be already a part of on-disk
2882 * orphan list. If so skip on-disk list modification.
2884 if (!NEXT_ORPHAN(inode
) || NEXT_ORPHAN(inode
) >
2885 (le32_to_cpu(sbi
->s_es
->s_inodes_count
))) {
2886 /* Insert this inode at the head of the on-disk orphan list */
2887 NEXT_ORPHAN(inode
) = le32_to_cpu(sbi
->s_es
->s_last_orphan
);
2888 sbi
->s_es
->s_last_orphan
= cpu_to_le32(inode
->i_ino
);
2891 list_add(&EXT4_I(inode
)->i_orphan
, &sbi
->s_orphan
);
2892 mutex_unlock(&sbi
->s_orphan_lock
);
2895 err
= ext4_handle_dirty_super(handle
, sb
);
2896 rc
= ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
2901 * We have to remove inode from in-memory list if
2902 * addition to on disk orphan list failed. Stray orphan
2903 * list entries can cause panics at unmount time.
2905 mutex_lock(&sbi
->s_orphan_lock
);
2906 list_del_init(&EXT4_I(inode
)->i_orphan
);
2907 mutex_unlock(&sbi
->s_orphan_lock
);
2912 jbd_debug(4, "superblock will point to %lu\n", inode
->i_ino
);
2913 jbd_debug(4, "orphan inode %lu will point to %d\n",
2914 inode
->i_ino
, NEXT_ORPHAN(inode
));
2916 ext4_std_error(sb
, err
);
2921 * ext4_orphan_del() removes an unlinked or truncated inode from the list
2922 * of such inodes stored on disk, because it is finally being cleaned up.
2924 int ext4_orphan_del(handle_t
*handle
, struct inode
*inode
)
2926 struct list_head
*prev
;
2927 struct ext4_inode_info
*ei
= EXT4_I(inode
);
2928 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
2930 struct ext4_iloc iloc
;
2933 if (!sbi
->s_journal
&& !(sbi
->s_mount_state
& EXT4_ORPHAN_FS
))
2936 WARN_ON_ONCE(!(inode
->i_state
& (I_NEW
| I_FREEING
)) &&
2937 !inode_is_locked(inode
));
2938 /* Do this quick check before taking global s_orphan_lock. */
2939 if (list_empty(&ei
->i_orphan
))
2943 /* Grab inode buffer early before taking global s_orphan_lock */
2944 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
2947 mutex_lock(&sbi
->s_orphan_lock
);
2948 jbd_debug(4, "remove inode %lu from orphan list\n", inode
->i_ino
);
2950 prev
= ei
->i_orphan
.prev
;
2951 list_del_init(&ei
->i_orphan
);
2953 /* If we're on an error path, we may not have a valid
2954 * transaction handle with which to update the orphan list on
2955 * disk, but we still need to remove the inode from the linked
2956 * list in memory. */
2957 if (!handle
|| err
) {
2958 mutex_unlock(&sbi
->s_orphan_lock
);
2962 ino_next
= NEXT_ORPHAN(inode
);
2963 if (prev
== &sbi
->s_orphan
) {
2964 jbd_debug(4, "superblock will point to %u\n", ino_next
);
2965 BUFFER_TRACE(sbi
->s_sbh
, "get_write_access");
2966 err
= ext4_journal_get_write_access(handle
, sbi
->s_sbh
);
2968 mutex_unlock(&sbi
->s_orphan_lock
);
2971 sbi
->s_es
->s_last_orphan
= cpu_to_le32(ino_next
);
2972 mutex_unlock(&sbi
->s_orphan_lock
);
2973 err
= ext4_handle_dirty_super(handle
, inode
->i_sb
);
2975 struct ext4_iloc iloc2
;
2976 struct inode
*i_prev
=
2977 &list_entry(prev
, struct ext4_inode_info
, i_orphan
)->vfs_inode
;
2979 jbd_debug(4, "orphan inode %lu will point to %u\n",
2980 i_prev
->i_ino
, ino_next
);
2981 err
= ext4_reserve_inode_write(handle
, i_prev
, &iloc2
);
2983 mutex_unlock(&sbi
->s_orphan_lock
);
2986 NEXT_ORPHAN(i_prev
) = ino_next
;
2987 err
= ext4_mark_iloc_dirty(handle
, i_prev
, &iloc2
);
2988 mutex_unlock(&sbi
->s_orphan_lock
);
2992 NEXT_ORPHAN(inode
) = 0;
2993 err
= ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
2995 ext4_std_error(inode
->i_sb
, err
);
3003 static int ext4_rmdir(struct inode
*dir
, struct dentry
*dentry
)
3006 struct inode
*inode
;
3007 struct buffer_head
*bh
;
3008 struct ext4_dir_entry_2
*de
;
3009 handle_t
*handle
= NULL
;
3011 if (unlikely(ext4_forced_shutdown(EXT4_SB(dir
->i_sb
))))
3014 /* Initialize quotas before so that eventual writes go in
3015 * separate transaction */
3016 retval
= dquot_initialize(dir
);
3019 retval
= dquot_initialize(d_inode(dentry
));
3024 bh
= ext4_find_entry(dir
, &dentry
->d_name
, &de
, NULL
);
3030 inode
= d_inode(dentry
);
3032 retval
= -EFSCORRUPTED
;
3033 if (le32_to_cpu(de
->inode
) != inode
->i_ino
)
3036 retval
= -ENOTEMPTY
;
3037 if (!ext4_empty_dir(inode
))
3040 handle
= ext4_journal_start(dir
, EXT4_HT_DIR
,
3041 EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
));
3042 if (IS_ERR(handle
)) {
3043 retval
= PTR_ERR(handle
);
3048 if (IS_DIRSYNC(dir
))
3049 ext4_handle_sync(handle
);
3051 retval
= ext4_delete_entry(handle
, dir
, de
, bh
);
3054 if (!EXT4_DIR_LINK_EMPTY(inode
))
3055 ext4_warning_inode(inode
,
3056 "empty directory '%.*s' has too many links (%u)",
3057 dentry
->d_name
.len
, dentry
->d_name
.name
,
3059 inode_inc_iversion(inode
);
3061 /* There's no need to set i_disksize: the fact that i_nlink is
3062 * zero will ensure that the right thing happens during any
3065 ext4_orphan_add(handle
, inode
);
3066 inode
->i_ctime
= dir
->i_ctime
= dir
->i_mtime
= current_time(inode
);
3067 ext4_mark_inode_dirty(handle
, inode
);
3068 ext4_dec_count(handle
, dir
);
3069 ext4_update_dx_flag(dir
);
3070 ext4_mark_inode_dirty(handle
, dir
);
3072 #ifdef CONFIG_UNICODE
3073 /* VFS negative dentries are incompatible with Encoding and
3074 * Case-insensitiveness. Eventually we'll want avoid
3075 * invalidating the dentries here, alongside with returning the
3076 * negative dentries at ext4_lookup(), when it is better
3077 * supported by the VFS for the CI case.
3079 if (IS_CASEFOLDED(dir
))
3080 d_invalidate(dentry
);
3086 ext4_journal_stop(handle
);
3090 static int ext4_unlink(struct inode
*dir
, struct dentry
*dentry
)
3093 struct inode
*inode
;
3094 struct buffer_head
*bh
;
3095 struct ext4_dir_entry_2
*de
;
3096 handle_t
*handle
= NULL
;
3098 if (unlikely(ext4_forced_shutdown(EXT4_SB(dir
->i_sb
))))
3101 trace_ext4_unlink_enter(dir
, dentry
);
3102 /* Initialize quotas before so that eventual writes go
3103 * in separate transaction */
3104 retval
= dquot_initialize(dir
);
3107 retval
= dquot_initialize(d_inode(dentry
));
3112 bh
= ext4_find_entry(dir
, &dentry
->d_name
, &de
, NULL
);
3118 inode
= d_inode(dentry
);
3120 retval
= -EFSCORRUPTED
;
3121 if (le32_to_cpu(de
->inode
) != inode
->i_ino
)
3124 handle
= ext4_journal_start(dir
, EXT4_HT_DIR
,
3125 EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
));
3126 if (IS_ERR(handle
)) {
3127 retval
= PTR_ERR(handle
);
3132 if (IS_DIRSYNC(dir
))
3133 ext4_handle_sync(handle
);
3135 if (inode
->i_nlink
== 0) {
3136 ext4_warning_inode(inode
, "Deleting file '%.*s' with no links",
3137 dentry
->d_name
.len
, dentry
->d_name
.name
);
3138 set_nlink(inode
, 1);
3140 retval
= ext4_delete_entry(handle
, dir
, de
, bh
);
3143 dir
->i_ctime
= dir
->i_mtime
= current_time(dir
);
3144 ext4_update_dx_flag(dir
);
3145 ext4_mark_inode_dirty(handle
, dir
);
3147 if (!inode
->i_nlink
)
3148 ext4_orphan_add(handle
, inode
);
3149 inode
->i_ctime
= current_time(inode
);
3150 ext4_mark_inode_dirty(handle
, inode
);
3152 #ifdef CONFIG_UNICODE
3153 /* VFS negative dentries are incompatible with Encoding and
3154 * Case-insensitiveness. Eventually we'll want avoid
3155 * invalidating the dentries here, alongside with returning the
3156 * negative dentries at ext4_lookup(), when it is better
3157 * supported by the VFS for the CI case.
3159 if (IS_CASEFOLDED(dir
))
3160 d_invalidate(dentry
);
3166 ext4_journal_stop(handle
);
3167 trace_ext4_unlink_exit(dentry
, retval
);
3171 static int ext4_symlink(struct inode
*dir
,
3172 struct dentry
*dentry
, const char *symname
)
3175 struct inode
*inode
;
3176 int err
, len
= strlen(symname
);
3178 struct fscrypt_str disk_link
;
3180 if (unlikely(ext4_forced_shutdown(EXT4_SB(dir
->i_sb
))))
3183 err
= fscrypt_prepare_symlink(dir
, symname
, len
, dir
->i_sb
->s_blocksize
,
3188 err
= dquot_initialize(dir
);
3192 if ((disk_link
.len
> EXT4_N_BLOCKS
* 4)) {
3194 * For non-fast symlinks, we just allocate inode and put it on
3195 * orphan list in the first transaction => we need bitmap,
3196 * group descriptor, sb, inode block, quota blocks, and
3197 * possibly selinux xattr blocks.
3199 credits
= 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir
->i_sb
) +
3200 EXT4_XATTR_TRANS_BLOCKS
;
3203 * Fast symlink. We have to add entry to directory
3204 * (EXT4_DATA_TRANS_BLOCKS + EXT4_INDEX_EXTRA_TRANS_BLOCKS),
3205 * allocate new inode (bitmap, group descriptor, inode block,
3206 * quota blocks, sb is already counted in previous macros).
3208 credits
= EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
3209 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 3;
3212 inode
= ext4_new_inode_start_handle(dir
, S_IFLNK
|S_IRWXUGO
,
3213 &dentry
->d_name
, 0, NULL
,
3214 EXT4_HT_DIR
, credits
);
3215 handle
= ext4_journal_current_handle();
3216 if (IS_ERR(inode
)) {
3218 ext4_journal_stop(handle
);
3219 return PTR_ERR(inode
);
3222 if (IS_ENCRYPTED(inode
)) {
3223 err
= fscrypt_encrypt_symlink(inode
, symname
, len
, &disk_link
);
3225 goto err_drop_inode
;
3226 inode
->i_op
= &ext4_encrypted_symlink_inode_operations
;
3229 if ((disk_link
.len
> EXT4_N_BLOCKS
* 4)) {
3230 if (!IS_ENCRYPTED(inode
))
3231 inode
->i_op
= &ext4_symlink_inode_operations
;
3232 inode_nohighmem(inode
);
3233 ext4_set_aops(inode
);
3235 * We cannot call page_symlink() with transaction started
3236 * because it calls into ext4_write_begin() which can wait
3237 * for transaction commit if we are running out of space
3238 * and thus we deadlock. So we have to stop transaction now
3239 * and restart it when symlink contents is written.
3241 * To keep fs consistent in case of crash, we have to put inode
3242 * to orphan list in the mean time.
3245 err
= ext4_orphan_add(handle
, inode
);
3246 ext4_journal_stop(handle
);
3249 goto err_drop_inode
;
3250 err
= __page_symlink(inode
, disk_link
.name
, disk_link
.len
, 1);
3252 goto err_drop_inode
;
3254 * Now inode is being linked into dir (EXT4_DATA_TRANS_BLOCKS
3255 * + EXT4_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
3257 handle
= ext4_journal_start(dir
, EXT4_HT_DIR
,
3258 EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
3259 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 1);
3260 if (IS_ERR(handle
)) {
3261 err
= PTR_ERR(handle
);
3263 goto err_drop_inode
;
3265 set_nlink(inode
, 1);
3266 err
= ext4_orphan_del(handle
, inode
);
3268 goto err_drop_inode
;
3270 /* clear the extent format for fast symlink */
3271 ext4_clear_inode_flag(inode
, EXT4_INODE_EXTENTS
);
3272 if (!IS_ENCRYPTED(inode
)) {
3273 inode
->i_op
= &ext4_fast_symlink_inode_operations
;
3274 inode
->i_link
= (char *)&EXT4_I(inode
)->i_data
;
3276 memcpy((char *)&EXT4_I(inode
)->i_data
, disk_link
.name
,
3278 inode
->i_size
= disk_link
.len
- 1;
3280 EXT4_I(inode
)->i_disksize
= inode
->i_size
;
3281 err
= ext4_add_nondir(handle
, dentry
, inode
);
3282 if (!err
&& IS_DIRSYNC(dir
))
3283 ext4_handle_sync(handle
);
3286 ext4_journal_stop(handle
);
3287 goto out_free_encrypted_link
;
3291 ext4_journal_stop(handle
);
3293 unlock_new_inode(inode
);
3295 out_free_encrypted_link
:
3296 if (disk_link
.name
!= (unsigned char *)symname
)
3297 kfree(disk_link
.name
);
3301 static int ext4_link(struct dentry
*old_dentry
,
3302 struct inode
*dir
, struct dentry
*dentry
)
3305 struct inode
*inode
= d_inode(old_dentry
);
3306 int err
, retries
= 0;
3308 if (inode
->i_nlink
>= EXT4_LINK_MAX
)
3311 err
= fscrypt_prepare_link(old_dentry
, dir
, dentry
);
3315 if ((ext4_test_inode_flag(dir
, EXT4_INODE_PROJINHERIT
)) &&
3316 (!projid_eq(EXT4_I(dir
)->i_projid
,
3317 EXT4_I(old_dentry
->d_inode
)->i_projid
)))
3320 err
= dquot_initialize(dir
);
3325 handle
= ext4_journal_start(dir
, EXT4_HT_DIR
,
3326 (EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
3327 EXT4_INDEX_EXTRA_TRANS_BLOCKS
) + 1);
3329 return PTR_ERR(handle
);
3331 if (IS_DIRSYNC(dir
))
3332 ext4_handle_sync(handle
);
3334 inode
->i_ctime
= current_time(inode
);
3335 ext4_inc_count(handle
, inode
);
3338 err
= ext4_add_entry(handle
, dentry
, inode
);
3340 ext4_mark_inode_dirty(handle
, inode
);
3341 /* this can happen only for tmpfile being
3342 * linked the first time
3344 if (inode
->i_nlink
== 1)
3345 ext4_orphan_del(handle
, inode
);
3346 d_instantiate(dentry
, inode
);
3351 ext4_journal_stop(handle
);
3352 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
3359 * Try to find buffer head where contains the parent block.
3360 * It should be the inode block if it is inlined or the 1st block
3361 * if it is a normal dir.
3363 static struct buffer_head
*ext4_get_first_dir_block(handle_t
*handle
,
3364 struct inode
*inode
,
3366 struct ext4_dir_entry_2
**parent_de
,
3369 struct buffer_head
*bh
;
3371 if (!ext4_has_inline_data(inode
)) {
3372 bh
= ext4_read_dirblock(inode
, 0, EITHER
);
3374 *retval
= PTR_ERR(bh
);
3377 *parent_de
= ext4_next_entry(
3378 (struct ext4_dir_entry_2
*)bh
->b_data
,
3379 inode
->i_sb
->s_blocksize
);
3384 return ext4_get_first_inline_block(inode
, parent_de
, retval
);
3387 struct ext4_renament
{
3389 struct dentry
*dentry
;
3390 struct inode
*inode
;
3392 int dir_nlink_delta
;
3394 /* entry for "dentry" */
3395 struct buffer_head
*bh
;
3396 struct ext4_dir_entry_2
*de
;
3399 /* entry for ".." in inode if it's a directory */
3400 struct buffer_head
*dir_bh
;
3401 struct ext4_dir_entry_2
*parent_de
;
3405 static int ext4_rename_dir_prepare(handle_t
*handle
, struct ext4_renament
*ent
)
3409 ent
->dir_bh
= ext4_get_first_dir_block(handle
, ent
->inode
,
3410 &retval
, &ent
->parent_de
,
3414 if (le32_to_cpu(ent
->parent_de
->inode
) != ent
->dir
->i_ino
)
3415 return -EFSCORRUPTED
;
3416 BUFFER_TRACE(ent
->dir_bh
, "get_write_access");
3417 return ext4_journal_get_write_access(handle
, ent
->dir_bh
);
3420 static int ext4_rename_dir_finish(handle_t
*handle
, struct ext4_renament
*ent
,
3425 ent
->parent_de
->inode
= cpu_to_le32(dir_ino
);
3426 BUFFER_TRACE(ent
->dir_bh
, "call ext4_handle_dirty_metadata");
3427 if (!ent
->dir_inlined
) {
3428 if (is_dx(ent
->inode
)) {
3429 retval
= ext4_handle_dirty_dx_node(handle
,
3433 retval
= ext4_handle_dirty_dirent_node(handle
,
3438 retval
= ext4_mark_inode_dirty(handle
, ent
->inode
);
3441 ext4_std_error(ent
->dir
->i_sb
, retval
);
3447 static int ext4_setent(handle_t
*handle
, struct ext4_renament
*ent
,
3448 unsigned ino
, unsigned file_type
)
3452 BUFFER_TRACE(ent
->bh
, "get write access");
3453 retval
= ext4_journal_get_write_access(handle
, ent
->bh
);
3456 ent
->de
->inode
= cpu_to_le32(ino
);
3457 if (ext4_has_feature_filetype(ent
->dir
->i_sb
))
3458 ent
->de
->file_type
= file_type
;
3459 inode_inc_iversion(ent
->dir
);
3460 ent
->dir
->i_ctime
= ent
->dir
->i_mtime
=
3461 current_time(ent
->dir
);
3462 ext4_mark_inode_dirty(handle
, ent
->dir
);
3463 BUFFER_TRACE(ent
->bh
, "call ext4_handle_dirty_metadata");
3464 if (!ent
->inlined
) {
3465 retval
= ext4_handle_dirty_dirent_node(handle
,
3467 if (unlikely(retval
)) {
3468 ext4_std_error(ent
->dir
->i_sb
, retval
);
3478 static int ext4_find_delete_entry(handle_t
*handle
, struct inode
*dir
,
3479 const struct qstr
*d_name
)
3481 int retval
= -ENOENT
;
3482 struct buffer_head
*bh
;
3483 struct ext4_dir_entry_2
*de
;
3485 bh
= ext4_find_entry(dir
, d_name
, &de
, NULL
);
3489 retval
= ext4_delete_entry(handle
, dir
, de
, bh
);
3495 static void ext4_rename_delete(handle_t
*handle
, struct ext4_renament
*ent
,
3500 * ent->de could have moved from under us during htree split, so make
3501 * sure that we are deleting the right entry. We might also be pointing
3502 * to a stale entry in the unused part of ent->bh so just checking inum
3503 * and the name isn't enough.
3505 if (le32_to_cpu(ent
->de
->inode
) != ent
->inode
->i_ino
||
3506 ent
->de
->name_len
!= ent
->dentry
->d_name
.len
||
3507 strncmp(ent
->de
->name
, ent
->dentry
->d_name
.name
,
3508 ent
->de
->name_len
) ||
3510 retval
= ext4_find_delete_entry(handle
, ent
->dir
,
3511 &ent
->dentry
->d_name
);
3513 retval
= ext4_delete_entry(handle
, ent
->dir
, ent
->de
, ent
->bh
);
3514 if (retval
== -ENOENT
) {
3515 retval
= ext4_find_delete_entry(handle
, ent
->dir
,
3516 &ent
->dentry
->d_name
);
3521 ext4_warning_inode(ent
->dir
,
3522 "Deleting old file: nlink %d, error=%d",
3523 ent
->dir
->i_nlink
, retval
);
3527 static void ext4_update_dir_count(handle_t
*handle
, struct ext4_renament
*ent
)
3529 if (ent
->dir_nlink_delta
) {
3530 if (ent
->dir_nlink_delta
== -1)
3531 ext4_dec_count(handle
, ent
->dir
);
3533 ext4_inc_count(handle
, ent
->dir
);
3534 ext4_mark_inode_dirty(handle
, ent
->dir
);
3538 static struct inode
*ext4_whiteout_for_rename(struct ext4_renament
*ent
,
3539 int credits
, handle_t
**h
)
3546 * for inode block, sb block, group summaries,
3549 credits
+= (EXT4_MAXQUOTAS_TRANS_BLOCKS(ent
->dir
->i_sb
) +
3550 EXT4_XATTR_TRANS_BLOCKS
+ 4);
3552 wh
= ext4_new_inode_start_handle(ent
->dir
, S_IFCHR
| WHITEOUT_MODE
,
3553 &ent
->dentry
->d_name
, 0, NULL
,
3554 EXT4_HT_DIR
, credits
);
3556 handle
= ext4_journal_current_handle();
3559 ext4_journal_stop(handle
);
3560 if (PTR_ERR(wh
) == -ENOSPC
&&
3561 ext4_should_retry_alloc(ent
->dir
->i_sb
, &retries
))
3565 init_special_inode(wh
, wh
->i_mode
, WHITEOUT_DEV
);
3566 wh
->i_op
= &ext4_special_inode_operations
;
3572 * Anybody can rename anything with this: the permission checks are left to the
3573 * higher-level routines.
3575 * n.b. old_{dentry,inode) refers to the source dentry/inode
3576 * while new_{dentry,inode) refers to the destination dentry/inode
3577 * This comes from rename(const char *oldpath, const char *newpath)
3579 static int ext4_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
3580 struct inode
*new_dir
, struct dentry
*new_dentry
,
3583 handle_t
*handle
= NULL
;
3584 struct ext4_renament old
= {
3586 .dentry
= old_dentry
,
3587 .inode
= d_inode(old_dentry
),
3589 struct ext4_renament
new = {
3591 .dentry
= new_dentry
,
3592 .inode
= d_inode(new_dentry
),
3596 struct inode
*whiteout
= NULL
;
3600 if (new.inode
&& new.inode
->i_nlink
== 0) {
3601 EXT4_ERROR_INODE(new.inode
,
3602 "target of rename is already freed");
3603 return -EFSCORRUPTED
;
3606 if ((ext4_test_inode_flag(new_dir
, EXT4_INODE_PROJINHERIT
)) &&
3607 (!projid_eq(EXT4_I(new_dir
)->i_projid
,
3608 EXT4_I(old_dentry
->d_inode
)->i_projid
)))
3611 retval
= dquot_initialize(old
.dir
);
3614 retval
= dquot_initialize(new.dir
);
3618 /* Initialize quotas before so that eventual writes go
3619 * in separate transaction */
3621 retval
= dquot_initialize(new.inode
);
3626 old
.bh
= ext4_find_entry(old
.dir
, &old
.dentry
->d_name
, &old
.de
, NULL
);
3628 return PTR_ERR(old
.bh
);
3630 * Check for inode number is _not_ due to possible IO errors.
3631 * We might rmdir the source, keep it as pwd of some process
3632 * and merrily kill the link to whatever was created under the
3633 * same name. Goodbye sticky bit ;-<
3636 if (!old
.bh
|| le32_to_cpu(old
.de
->inode
) != old
.inode
->i_ino
)
3639 new.bh
= ext4_find_entry(new.dir
, &new.dentry
->d_name
,
3640 &new.de
, &new.inlined
);
3641 if (IS_ERR(new.bh
)) {
3642 retval
= PTR_ERR(new.bh
);
3652 if (new.inode
&& !test_opt(new.dir
->i_sb
, NO_AUTO_DA_ALLOC
))
3653 ext4_alloc_da_blocks(old
.inode
);
3655 credits
= (2 * EXT4_DATA_TRANS_BLOCKS(old
.dir
->i_sb
) +
3656 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 2);
3657 if (!(flags
& RENAME_WHITEOUT
)) {
3658 handle
= ext4_journal_start(old
.dir
, EXT4_HT_DIR
, credits
);
3659 if (IS_ERR(handle
)) {
3660 retval
= PTR_ERR(handle
);
3665 whiteout
= ext4_whiteout_for_rename(&old
, credits
, &handle
);
3666 if (IS_ERR(whiteout
)) {
3667 retval
= PTR_ERR(whiteout
);
3673 if (IS_DIRSYNC(old
.dir
) || IS_DIRSYNC(new.dir
))
3674 ext4_handle_sync(handle
);
3676 if (S_ISDIR(old
.inode
->i_mode
)) {
3678 retval
= -ENOTEMPTY
;
3679 if (!ext4_empty_dir(new.inode
))
3683 if (new.dir
!= old
.dir
&& EXT4_DIR_LINK_MAX(new.dir
))
3686 retval
= ext4_rename_dir_prepare(handle
, &old
);
3691 * If we're renaming a file within an inline_data dir and adding or
3692 * setting the new dirent causes a conversion from inline_data to
3693 * extents/blockmap, we need to force the dirent delete code to
3694 * re-read the directory, or else we end up trying to delete a dirent
3695 * from what is now the extent tree root (or a block map).
3697 force_reread
= (new.dir
->i_ino
== old
.dir
->i_ino
&&
3698 ext4_test_inode_flag(new.dir
, EXT4_INODE_INLINE_DATA
));
3700 old_file_type
= old
.de
->file_type
;
3703 * Do this before adding a new entry, so the old entry is sure
3704 * to be still pointing to the valid old entry.
3706 retval
= ext4_setent(handle
, &old
, whiteout
->i_ino
,
3710 ext4_mark_inode_dirty(handle
, whiteout
);
3713 retval
= ext4_add_entry(handle
, new.dentry
, old
.inode
);
3717 retval
= ext4_setent(handle
, &new,
3718 old
.inode
->i_ino
, old_file_type
);
3723 force_reread
= !ext4_test_inode_flag(new.dir
,
3724 EXT4_INODE_INLINE_DATA
);
3727 * Like most other Unix systems, set the ctime for inodes on a
3730 old
.inode
->i_ctime
= current_time(old
.inode
);
3731 ext4_mark_inode_dirty(handle
, old
.inode
);
3737 ext4_rename_delete(handle
, &old
, force_reread
);
3741 ext4_dec_count(handle
, new.inode
);
3742 new.inode
->i_ctime
= current_time(new.inode
);
3744 old
.dir
->i_ctime
= old
.dir
->i_mtime
= current_time(old
.dir
);
3745 ext4_update_dx_flag(old
.dir
);
3747 retval
= ext4_rename_dir_finish(handle
, &old
, new.dir
->i_ino
);
3751 ext4_dec_count(handle
, old
.dir
);
3753 /* checked ext4_empty_dir above, can't have another
3754 * parent, ext4_dec_count() won't work for many-linked
3756 clear_nlink(new.inode
);
3758 ext4_inc_count(handle
, new.dir
);
3759 ext4_update_dx_flag(new.dir
);
3760 ext4_mark_inode_dirty(handle
, new.dir
);
3763 ext4_mark_inode_dirty(handle
, old
.dir
);
3765 ext4_mark_inode_dirty(handle
, new.inode
);
3766 if (!new.inode
->i_nlink
)
3767 ext4_orphan_add(handle
, new.inode
);
3777 drop_nlink(whiteout
);
3778 unlock_new_inode(whiteout
);
3782 ext4_journal_stop(handle
);
3786 static int ext4_cross_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
3787 struct inode
*new_dir
, struct dentry
*new_dentry
)
3789 handle_t
*handle
= NULL
;
3790 struct ext4_renament old
= {
3792 .dentry
= old_dentry
,
3793 .inode
= d_inode(old_dentry
),
3795 struct ext4_renament
new = {
3797 .dentry
= new_dentry
,
3798 .inode
= d_inode(new_dentry
),
3802 struct timespec64 ctime
;
3804 if ((ext4_test_inode_flag(new_dir
, EXT4_INODE_PROJINHERIT
) &&
3805 !projid_eq(EXT4_I(new_dir
)->i_projid
,
3806 EXT4_I(old_dentry
->d_inode
)->i_projid
)) ||
3807 (ext4_test_inode_flag(old_dir
, EXT4_INODE_PROJINHERIT
) &&
3808 !projid_eq(EXT4_I(old_dir
)->i_projid
,
3809 EXT4_I(new_dentry
->d_inode
)->i_projid
)))
3812 retval
= dquot_initialize(old
.dir
);
3815 retval
= dquot_initialize(new.dir
);
3819 old
.bh
= ext4_find_entry(old
.dir
, &old
.dentry
->d_name
,
3820 &old
.de
, &old
.inlined
);
3822 return PTR_ERR(old
.bh
);
3824 * Check for inode number is _not_ due to possible IO errors.
3825 * We might rmdir the source, keep it as pwd of some process
3826 * and merrily kill the link to whatever was created under the
3827 * same name. Goodbye sticky bit ;-<
3830 if (!old
.bh
|| le32_to_cpu(old
.de
->inode
) != old
.inode
->i_ino
)
3833 new.bh
= ext4_find_entry(new.dir
, &new.dentry
->d_name
,
3834 &new.de
, &new.inlined
);
3835 if (IS_ERR(new.bh
)) {
3836 retval
= PTR_ERR(new.bh
);
3841 /* RENAME_EXCHANGE case: old *and* new must both exist */
3842 if (!new.bh
|| le32_to_cpu(new.de
->inode
) != new.inode
->i_ino
)
3845 handle
= ext4_journal_start(old
.dir
, EXT4_HT_DIR
,
3846 (2 * EXT4_DATA_TRANS_BLOCKS(old
.dir
->i_sb
) +
3847 2 * EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 2));
3848 if (IS_ERR(handle
)) {
3849 retval
= PTR_ERR(handle
);
3854 if (IS_DIRSYNC(old
.dir
) || IS_DIRSYNC(new.dir
))
3855 ext4_handle_sync(handle
);
3857 if (S_ISDIR(old
.inode
->i_mode
)) {
3859 retval
= ext4_rename_dir_prepare(handle
, &old
);
3863 if (S_ISDIR(new.inode
->i_mode
)) {
3865 retval
= ext4_rename_dir_prepare(handle
, &new);
3871 * Other than the special case of overwriting a directory, parents'
3872 * nlink only needs to be modified if this is a cross directory rename.
3874 if (old
.dir
!= new.dir
&& old
.is_dir
!= new.is_dir
) {
3875 old
.dir_nlink_delta
= old
.is_dir
? -1 : 1;
3876 new.dir_nlink_delta
= -old
.dir_nlink_delta
;
3878 if ((old
.dir_nlink_delta
> 0 && EXT4_DIR_LINK_MAX(old
.dir
)) ||
3879 (new.dir_nlink_delta
> 0 && EXT4_DIR_LINK_MAX(new.dir
)))
3883 new_file_type
= new.de
->file_type
;
3884 retval
= ext4_setent(handle
, &new, old
.inode
->i_ino
, old
.de
->file_type
);
3888 retval
= ext4_setent(handle
, &old
, new.inode
->i_ino
, new_file_type
);
3893 * Like most other Unix systems, set the ctime for inodes on a
3896 ctime
= current_time(old
.inode
);
3897 old
.inode
->i_ctime
= ctime
;
3898 new.inode
->i_ctime
= ctime
;
3899 ext4_mark_inode_dirty(handle
, old
.inode
);
3900 ext4_mark_inode_dirty(handle
, new.inode
);
3903 retval
= ext4_rename_dir_finish(handle
, &old
, new.dir
->i_ino
);
3908 retval
= ext4_rename_dir_finish(handle
, &new, old
.dir
->i_ino
);
3912 ext4_update_dir_count(handle
, &old
);
3913 ext4_update_dir_count(handle
, &new);
3922 ext4_journal_stop(handle
);
3926 static int ext4_rename2(struct inode
*old_dir
, struct dentry
*old_dentry
,
3927 struct inode
*new_dir
, struct dentry
*new_dentry
,
3932 if (unlikely(ext4_forced_shutdown(EXT4_SB(old_dir
->i_sb
))))
3935 if (flags
& ~(RENAME_NOREPLACE
| RENAME_EXCHANGE
| RENAME_WHITEOUT
))
3938 err
= fscrypt_prepare_rename(old_dir
, old_dentry
, new_dir
, new_dentry
,
3943 if (flags
& RENAME_EXCHANGE
) {
3944 return ext4_cross_rename(old_dir
, old_dentry
,
3945 new_dir
, new_dentry
);
3948 return ext4_rename(old_dir
, old_dentry
, new_dir
, new_dentry
, flags
);
3952 * directories can handle most operations...
3954 const struct inode_operations ext4_dir_inode_operations
= {
3955 .create
= ext4_create
,
3956 .lookup
= ext4_lookup
,
3958 .unlink
= ext4_unlink
,
3959 .symlink
= ext4_symlink
,
3960 .mkdir
= ext4_mkdir
,
3961 .rmdir
= ext4_rmdir
,
3962 .mknod
= ext4_mknod
,
3963 .tmpfile
= ext4_tmpfile
,
3964 .rename
= ext4_rename2
,
3965 .setattr
= ext4_setattr
,
3966 .getattr
= ext4_getattr
,
3967 .listxattr
= ext4_listxattr
,
3968 .get_acl
= ext4_get_acl
,
3969 .set_acl
= ext4_set_acl
,
3970 .fiemap
= ext4_fiemap
,
3973 const struct inode_operations ext4_special_inode_operations
= {
3974 .setattr
= ext4_setattr
,
3975 .getattr
= ext4_getattr
,
3976 .listxattr
= ext4_listxattr
,
3977 .get_acl
= ext4_get_acl
,
3978 .set_acl
= ext4_set_acl
,