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>
38 #include "ext4_jbd2.h"
43 #include <trace/events/ext4.h>
45 * define how far ahead to read directories while searching them.
47 #define NAMEI_RA_CHUNKS 2
48 #define NAMEI_RA_BLOCKS 4
49 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
51 static struct buffer_head
*ext4_append(handle_t
*handle
,
55 struct buffer_head
*bh
;
58 if (unlikely(EXT4_SB(inode
->i_sb
)->s_max_dir_size_kb
&&
59 ((inode
->i_size
>> 10) >=
60 EXT4_SB(inode
->i_sb
)->s_max_dir_size_kb
)))
61 return ERR_PTR(-ENOSPC
);
63 *block
= inode
->i_size
>> inode
->i_sb
->s_blocksize_bits
;
65 bh
= ext4_bread(handle
, inode
, *block
, EXT4_GET_BLOCKS_CREATE
);
68 inode
->i_size
+= inode
->i_sb
->s_blocksize
;
69 EXT4_I(inode
)->i_disksize
= inode
->i_size
;
70 BUFFER_TRACE(bh
, "get_write_access");
71 err
= ext4_journal_get_write_access(handle
, bh
);
74 ext4_std_error(inode
->i_sb
, err
);
80 static int ext4_dx_csum_verify(struct inode
*inode
,
81 struct ext4_dir_entry
*dirent
);
87 #define ext4_read_dirblock(inode, block, type) \
88 __ext4_read_dirblock((inode), (block), (type), __func__, __LINE__)
90 static struct buffer_head
*__ext4_read_dirblock(struct inode
*inode
,
96 struct buffer_head
*bh
;
97 struct ext4_dir_entry
*dirent
;
100 bh
= ext4_bread(NULL
, inode
, block
, 0);
102 __ext4_warning(inode
->i_sb
, func
, line
,
103 "inode #%lu: lblock %lu: comm %s: "
104 "error %ld reading directory block",
105 inode
->i_ino
, (unsigned long)block
,
106 current
->comm
, PTR_ERR(bh
));
111 ext4_error_inode(inode
, func
, line
, block
,
112 "Directory hole found");
113 return ERR_PTR(-EFSCORRUPTED
);
115 dirent
= (struct ext4_dir_entry
*) bh
->b_data
;
116 /* Determine whether or not we have an index block */
120 else if (ext4_rec_len_from_disk(dirent
->rec_len
,
121 inode
->i_sb
->s_blocksize
) ==
122 inode
->i_sb
->s_blocksize
)
125 if (!is_dx_block
&& type
== INDEX
) {
126 ext4_error_inode(inode
, func
, line
, block
,
127 "directory leaf block found instead of index block");
128 return ERR_PTR(-EFSCORRUPTED
);
130 if (!ext4_has_metadata_csum(inode
->i_sb
) ||
135 * An empty leaf block can get mistaken for a index block; for
136 * this reason, we can only check the index checksum when the
137 * caller is sure it should be an index block.
139 if (is_dx_block
&& type
== INDEX
) {
140 if (ext4_dx_csum_verify(inode
, dirent
))
141 set_buffer_verified(bh
);
143 ext4_error_inode(inode
, func
, line
, block
,
144 "Directory index failed checksum");
146 return ERR_PTR(-EFSBADCRC
);
150 if (ext4_dirent_csum_verify(inode
, dirent
))
151 set_buffer_verified(bh
);
153 ext4_error_inode(inode
, func
, line
, block
,
154 "Directory block failed checksum");
156 return ERR_PTR(-EFSBADCRC
);
163 #define assert(test) J_ASSERT(test)
167 #define dxtrace(command) command
169 #define dxtrace(command)
193 * dx_root_info is laid out so that if it should somehow get overlaid by a
194 * dirent the two low bits of the hash version will be zero. Therefore, the
195 * hash version mod 4 should never be 0. Sincerely, the paranoia department.
200 struct fake_dirent dot
;
202 struct fake_dirent dotdot
;
206 __le32 reserved_zero
;
208 u8 info_length
; /* 8 */
213 struct dx_entry entries
[0];
218 struct fake_dirent fake
;
219 struct dx_entry entries
[0];
225 struct buffer_head
*bh
;
226 struct dx_entry
*entries
;
238 * This goes at the end of each htree block.
242 __le32 dt_checksum
; /* crc32c(uuid+inum+dirblock) */
245 static inline ext4_lblk_t
dx_get_block(struct dx_entry
*entry
);
246 static void dx_set_block(struct dx_entry
*entry
, ext4_lblk_t value
);
247 static inline unsigned dx_get_hash(struct dx_entry
*entry
);
248 static void dx_set_hash(struct dx_entry
*entry
, unsigned value
);
249 static unsigned dx_get_count(struct dx_entry
*entries
);
250 static unsigned dx_get_limit(struct dx_entry
*entries
);
251 static void dx_set_count(struct dx_entry
*entries
, unsigned value
);
252 static void dx_set_limit(struct dx_entry
*entries
, unsigned value
);
253 static unsigned dx_root_limit(struct inode
*dir
, unsigned infosize
);
254 static unsigned dx_node_limit(struct inode
*dir
);
255 static struct dx_frame
*dx_probe(struct ext4_filename
*fname
,
257 struct dx_hash_info
*hinfo
,
258 struct dx_frame
*frame
);
259 static void dx_release(struct dx_frame
*frames
);
260 static int dx_make_map(struct inode
*dir
, struct ext4_dir_entry_2
*de
,
261 unsigned blocksize
, struct dx_hash_info
*hinfo
,
262 struct dx_map_entry map
[]);
263 static void dx_sort_map(struct dx_map_entry
*map
, unsigned count
);
264 static struct ext4_dir_entry_2
*dx_move_dirents(char *from
, char *to
,
265 struct dx_map_entry
*offsets
, int count
, unsigned blocksize
);
266 static struct ext4_dir_entry_2
* dx_pack_dirents(char *base
, unsigned blocksize
);
267 static void dx_insert_block(struct dx_frame
*frame
,
268 u32 hash
, ext4_lblk_t block
);
269 static int ext4_htree_next_block(struct inode
*dir
, __u32 hash
,
270 struct dx_frame
*frame
,
271 struct dx_frame
*frames
,
273 static struct buffer_head
* ext4_dx_find_entry(struct inode
*dir
,
274 struct ext4_filename
*fname
,
275 struct ext4_dir_entry_2
**res_dir
);
276 static int ext4_dx_add_entry(handle_t
*handle
, struct ext4_filename
*fname
,
277 struct inode
*dir
, struct inode
*inode
);
279 /* checksumming functions */
280 void initialize_dirent_tail(struct ext4_dir_entry_tail
*t
,
281 unsigned int blocksize
)
283 memset(t
, 0, sizeof(struct ext4_dir_entry_tail
));
284 t
->det_rec_len
= ext4_rec_len_to_disk(
285 sizeof(struct ext4_dir_entry_tail
), blocksize
);
286 t
->det_reserved_ft
= EXT4_FT_DIR_CSUM
;
289 /* Walk through a dirent block to find a checksum "dirent" at the tail */
290 static struct ext4_dir_entry_tail
*get_dirent_tail(struct inode
*inode
,
291 struct ext4_dir_entry
*de
)
293 struct ext4_dir_entry_tail
*t
;
296 struct ext4_dir_entry
*d
, *top
;
299 top
= (struct ext4_dir_entry
*)(((void *)de
) +
300 (EXT4_BLOCK_SIZE(inode
->i_sb
) -
301 sizeof(struct ext4_dir_entry_tail
)));
302 while (d
< top
&& d
->rec_len
)
303 d
= (struct ext4_dir_entry
*)(((void *)d
) +
304 le16_to_cpu(d
->rec_len
));
309 t
= (struct ext4_dir_entry_tail
*)d
;
311 t
= EXT4_DIRENT_TAIL(de
, EXT4_BLOCK_SIZE(inode
->i_sb
));
314 if (t
->det_reserved_zero1
||
315 le16_to_cpu(t
->det_rec_len
) != sizeof(struct ext4_dir_entry_tail
) ||
316 t
->det_reserved_zero2
||
317 t
->det_reserved_ft
!= EXT4_FT_DIR_CSUM
)
323 static __le32
ext4_dirent_csum(struct inode
*inode
,
324 struct ext4_dir_entry
*dirent
, int size
)
326 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
327 struct ext4_inode_info
*ei
= EXT4_I(inode
);
330 csum
= ext4_chksum(sbi
, ei
->i_csum_seed
, (__u8
*)dirent
, size
);
331 return cpu_to_le32(csum
);
334 #define warn_no_space_for_csum(inode) \
335 __warn_no_space_for_csum((inode), __func__, __LINE__)
337 static void __warn_no_space_for_csum(struct inode
*inode
, const char *func
,
340 __ext4_warning_inode(inode
, func
, line
,
341 "No space for directory leaf checksum. Please run e2fsck -D.");
344 int ext4_dirent_csum_verify(struct inode
*inode
, struct ext4_dir_entry
*dirent
)
346 struct ext4_dir_entry_tail
*t
;
348 if (!ext4_has_metadata_csum(inode
->i_sb
))
351 t
= get_dirent_tail(inode
, dirent
);
353 warn_no_space_for_csum(inode
);
357 if (t
->det_checksum
!= ext4_dirent_csum(inode
, dirent
,
358 (void *)t
- (void *)dirent
))
364 static void ext4_dirent_csum_set(struct inode
*inode
,
365 struct ext4_dir_entry
*dirent
)
367 struct ext4_dir_entry_tail
*t
;
369 if (!ext4_has_metadata_csum(inode
->i_sb
))
372 t
= get_dirent_tail(inode
, dirent
);
374 warn_no_space_for_csum(inode
);
378 t
->det_checksum
= ext4_dirent_csum(inode
, dirent
,
379 (void *)t
- (void *)dirent
);
382 int ext4_handle_dirty_dirent_node(handle_t
*handle
,
384 struct buffer_head
*bh
)
386 ext4_dirent_csum_set(inode
, (struct ext4_dir_entry
*)bh
->b_data
);
387 return ext4_handle_dirty_metadata(handle
, inode
, bh
);
390 static struct dx_countlimit
*get_dx_countlimit(struct inode
*inode
,
391 struct ext4_dir_entry
*dirent
,
394 struct ext4_dir_entry
*dp
;
395 struct dx_root_info
*root
;
398 if (le16_to_cpu(dirent
->rec_len
) == EXT4_BLOCK_SIZE(inode
->i_sb
))
400 else if (le16_to_cpu(dirent
->rec_len
) == 12) {
401 dp
= (struct ext4_dir_entry
*)(((void *)dirent
) + 12);
402 if (le16_to_cpu(dp
->rec_len
) !=
403 EXT4_BLOCK_SIZE(inode
->i_sb
) - 12)
405 root
= (struct dx_root_info
*)(((void *)dp
+ 12));
406 if (root
->reserved_zero
||
407 root
->info_length
!= sizeof(struct dx_root_info
))
414 *offset
= count_offset
;
415 return (struct dx_countlimit
*)(((void *)dirent
) + count_offset
);
418 static __le32
ext4_dx_csum(struct inode
*inode
, struct ext4_dir_entry
*dirent
,
419 int count_offset
, int count
, struct dx_tail
*t
)
421 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
422 struct ext4_inode_info
*ei
= EXT4_I(inode
);
425 __u32 dummy_csum
= 0;
426 int offset
= offsetof(struct dx_tail
, dt_checksum
);
428 size
= count_offset
+ (count
* sizeof(struct dx_entry
));
429 csum
= ext4_chksum(sbi
, ei
->i_csum_seed
, (__u8
*)dirent
, size
);
430 csum
= ext4_chksum(sbi
, csum
, (__u8
*)t
, offset
);
431 csum
= ext4_chksum(sbi
, csum
, (__u8
*)&dummy_csum
, sizeof(dummy_csum
));
433 return cpu_to_le32(csum
);
436 static int ext4_dx_csum_verify(struct inode
*inode
,
437 struct ext4_dir_entry
*dirent
)
439 struct dx_countlimit
*c
;
441 int count_offset
, limit
, count
;
443 if (!ext4_has_metadata_csum(inode
->i_sb
))
446 c
= get_dx_countlimit(inode
, dirent
, &count_offset
);
448 EXT4_ERROR_INODE(inode
, "dir seems corrupt? Run e2fsck -D.");
451 limit
= le16_to_cpu(c
->limit
);
452 count
= le16_to_cpu(c
->count
);
453 if (count_offset
+ (limit
* sizeof(struct dx_entry
)) >
454 EXT4_BLOCK_SIZE(inode
->i_sb
) - sizeof(struct dx_tail
)) {
455 warn_no_space_for_csum(inode
);
458 t
= (struct dx_tail
*)(((struct dx_entry
*)c
) + limit
);
460 if (t
->dt_checksum
!= ext4_dx_csum(inode
, dirent
, count_offset
,
466 static void ext4_dx_csum_set(struct inode
*inode
, struct ext4_dir_entry
*dirent
)
468 struct dx_countlimit
*c
;
470 int count_offset
, limit
, count
;
472 if (!ext4_has_metadata_csum(inode
->i_sb
))
475 c
= get_dx_countlimit(inode
, dirent
, &count_offset
);
477 EXT4_ERROR_INODE(inode
, "dir seems corrupt? Run e2fsck -D.");
480 limit
= le16_to_cpu(c
->limit
);
481 count
= le16_to_cpu(c
->count
);
482 if (count_offset
+ (limit
* sizeof(struct dx_entry
)) >
483 EXT4_BLOCK_SIZE(inode
->i_sb
) - sizeof(struct dx_tail
)) {
484 warn_no_space_for_csum(inode
);
487 t
= (struct dx_tail
*)(((struct dx_entry
*)c
) + limit
);
489 t
->dt_checksum
= ext4_dx_csum(inode
, dirent
, count_offset
, count
, t
);
492 static inline int ext4_handle_dirty_dx_node(handle_t
*handle
,
494 struct buffer_head
*bh
)
496 ext4_dx_csum_set(inode
, (struct ext4_dir_entry
*)bh
->b_data
);
497 return ext4_handle_dirty_metadata(handle
, inode
, bh
);
501 * p is at least 6 bytes before the end of page
503 static inline struct ext4_dir_entry_2
*
504 ext4_next_entry(struct ext4_dir_entry_2
*p
, unsigned long blocksize
)
506 return (struct ext4_dir_entry_2
*)((char *)p
+
507 ext4_rec_len_from_disk(p
->rec_len
, blocksize
));
511 * Future: use high four bits of block for coalesce-on-delete flags
512 * Mask them off for now.
515 static inline ext4_lblk_t
dx_get_block(struct dx_entry
*entry
)
517 return le32_to_cpu(entry
->block
) & 0x0fffffff;
520 static inline void dx_set_block(struct dx_entry
*entry
, ext4_lblk_t value
)
522 entry
->block
= cpu_to_le32(value
);
525 static inline unsigned dx_get_hash(struct dx_entry
*entry
)
527 return le32_to_cpu(entry
->hash
);
530 static inline void dx_set_hash(struct dx_entry
*entry
, unsigned value
)
532 entry
->hash
= cpu_to_le32(value
);
535 static inline unsigned dx_get_count(struct dx_entry
*entries
)
537 return le16_to_cpu(((struct dx_countlimit
*) entries
)->count
);
540 static inline unsigned dx_get_limit(struct dx_entry
*entries
)
542 return le16_to_cpu(((struct dx_countlimit
*) entries
)->limit
);
545 static inline void dx_set_count(struct dx_entry
*entries
, unsigned value
)
547 ((struct dx_countlimit
*) entries
)->count
= cpu_to_le16(value
);
550 static inline void dx_set_limit(struct dx_entry
*entries
, unsigned value
)
552 ((struct dx_countlimit
*) entries
)->limit
= cpu_to_le16(value
);
555 static inline unsigned dx_root_limit(struct inode
*dir
, unsigned infosize
)
557 unsigned entry_space
= dir
->i_sb
->s_blocksize
- EXT4_DIR_REC_LEN(1) -
558 EXT4_DIR_REC_LEN(2) - infosize
;
560 if (ext4_has_metadata_csum(dir
->i_sb
))
561 entry_space
-= sizeof(struct dx_tail
);
562 return entry_space
/ sizeof(struct dx_entry
);
565 static inline unsigned dx_node_limit(struct inode
*dir
)
567 unsigned entry_space
= dir
->i_sb
->s_blocksize
- EXT4_DIR_REC_LEN(0);
569 if (ext4_has_metadata_csum(dir
->i_sb
))
570 entry_space
-= sizeof(struct dx_tail
);
571 return entry_space
/ sizeof(struct dx_entry
);
578 static void dx_show_index(char * label
, struct dx_entry
*entries
)
580 int i
, n
= dx_get_count (entries
);
581 printk(KERN_DEBUG
"%s index", label
);
582 for (i
= 0; i
< n
; i
++) {
583 printk(KERN_CONT
" %x->%lu",
584 i
? dx_get_hash(entries
+ i
) : 0,
585 (unsigned long)dx_get_block(entries
+ i
));
587 printk(KERN_CONT
"\n");
597 static struct stats
dx_show_leaf(struct inode
*dir
,
598 struct dx_hash_info
*hinfo
,
599 struct ext4_dir_entry_2
*de
,
600 int size
, int show_names
)
602 unsigned names
= 0, space
= 0;
603 char *base
= (char *) de
;
604 struct dx_hash_info h
= *hinfo
;
607 while ((char *) de
< base
+ size
)
613 #ifdef CONFIG_EXT4_FS_ENCRYPTION
616 struct fscrypt_str fname_crypto_str
=
622 if (ext4_encrypted_inode(dir
))
623 res
= fscrypt_get_encryption_info(dir
);
625 printk(KERN_WARNING
"Error setting up"
626 " fname crypto: %d\n", res
);
628 if (!fscrypt_has_encryption_key(dir
)) {
629 /* Directory is not encrypted */
630 ext4fs_dirhash(de
->name
,
632 printk("%*.s:(U)%x.%u ", len
,
634 (unsigned) ((char *) de
637 struct fscrypt_str de_name
=
638 FSTR_INIT(name
, len
);
640 /* Directory is encrypted */
641 res
= fscrypt_fname_alloc_buffer(
645 printk(KERN_WARNING
"Error "
649 res
= fscrypt_fname_disk_to_usr(dir
,
653 printk(KERN_WARNING
"Error "
654 "converting filename "
660 name
= fname_crypto_str
.name
;
661 len
= fname_crypto_str
.len
;
663 ext4fs_dirhash(de
->name
, de
->name_len
,
665 printk("%*.s:(E)%x.%u ", len
, name
,
666 h
.hash
, (unsigned) ((char *) de
668 fscrypt_fname_free_buffer(
672 int len
= de
->name_len
;
673 char *name
= de
->name
;
674 ext4fs_dirhash(de
->name
, de
->name_len
, &h
);
675 printk("%*.s:%x.%u ", len
, name
, h
.hash
,
676 (unsigned) ((char *) de
- base
));
679 space
+= EXT4_DIR_REC_LEN(de
->name_len
);
682 de
= ext4_next_entry(de
, size
);
684 printk(KERN_CONT
"(%i)\n", names
);
685 return (struct stats
) { names
, space
, 1 };
688 struct stats
dx_show_entries(struct dx_hash_info
*hinfo
, struct inode
*dir
,
689 struct dx_entry
*entries
, int levels
)
691 unsigned blocksize
= dir
->i_sb
->s_blocksize
;
692 unsigned count
= dx_get_count(entries
), names
= 0, space
= 0, i
;
694 struct buffer_head
*bh
;
695 printk("%i indexed blocks...\n", count
);
696 for (i
= 0; i
< count
; i
++, entries
++)
698 ext4_lblk_t block
= dx_get_block(entries
);
699 ext4_lblk_t hash
= i
? dx_get_hash(entries
): 0;
700 u32 range
= i
< count
- 1? (dx_get_hash(entries
+ 1) - hash
): ~hash
;
702 printk("%s%3u:%03u hash %8x/%8x ",levels
?"":" ", i
, block
, hash
, range
);
703 bh
= ext4_bread(NULL
,dir
, block
, 0);
704 if (!bh
|| IS_ERR(bh
))
707 dx_show_entries(hinfo
, dir
, ((struct dx_node
*) bh
->b_data
)->entries
, levels
- 1):
708 dx_show_leaf(dir
, hinfo
, (struct ext4_dir_entry_2
*)
709 bh
->b_data
, blocksize
, 0);
710 names
+= stats
.names
;
711 space
+= stats
.space
;
712 bcount
+= stats
.bcount
;
716 printk(KERN_DEBUG
"%snames %u, fullness %u (%u%%)\n",
717 levels
? "" : " ", names
, space
/bcount
,
718 (space
/bcount
)*100/blocksize
);
719 return (struct stats
) { names
, space
, bcount
};
721 #endif /* DX_DEBUG */
724 * Probe for a directory leaf block to search.
726 * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
727 * error in the directory index, and the caller should fall back to
728 * searching the directory normally. The callers of dx_probe **MUST**
729 * check for this error code, and make sure it never gets reflected
732 static struct dx_frame
*
733 dx_probe(struct ext4_filename
*fname
, struct inode
*dir
,
734 struct dx_hash_info
*hinfo
, struct dx_frame
*frame_in
)
736 unsigned count
, indirect
;
737 struct dx_entry
*at
, *entries
, *p
, *q
, *m
;
738 struct dx_root
*root
;
739 struct dx_frame
*frame
= frame_in
;
740 struct dx_frame
*ret_err
= ERR_PTR(ERR_BAD_DX_DIR
);
743 memset(frame_in
, 0, EXT4_HTREE_LEVEL
* sizeof(frame_in
[0]));
744 frame
->bh
= ext4_read_dirblock(dir
, 0, INDEX
);
745 if (IS_ERR(frame
->bh
))
746 return (struct dx_frame
*) frame
->bh
;
748 root
= (struct dx_root
*) frame
->bh
->b_data
;
749 if (root
->info
.hash_version
!= DX_HASH_TEA
&&
750 root
->info
.hash_version
!= DX_HASH_HALF_MD4
&&
751 root
->info
.hash_version
!= DX_HASH_LEGACY
) {
752 ext4_warning_inode(dir
, "Unrecognised inode hash code %u",
753 root
->info
.hash_version
);
757 hinfo
= &fname
->hinfo
;
758 hinfo
->hash_version
= root
->info
.hash_version
;
759 if (hinfo
->hash_version
<= DX_HASH_TEA
)
760 hinfo
->hash_version
+= EXT4_SB(dir
->i_sb
)->s_hash_unsigned
;
761 hinfo
->seed
= EXT4_SB(dir
->i_sb
)->s_hash_seed
;
762 if (fname
&& fname_name(fname
))
763 ext4fs_dirhash(fname_name(fname
), fname_len(fname
), hinfo
);
766 if (root
->info
.unused_flags
& 1) {
767 ext4_warning_inode(dir
, "Unimplemented hash flags: %#06x",
768 root
->info
.unused_flags
);
772 indirect
= root
->info
.indirect_levels
;
773 if (indirect
>= ext4_dir_htree_level(dir
->i_sb
)) {
774 ext4_warning(dir
->i_sb
,
775 "Directory (ino: %lu) htree depth %#06x exceed"
776 "supported value", dir
->i_ino
,
777 ext4_dir_htree_level(dir
->i_sb
));
778 if (ext4_dir_htree_level(dir
->i_sb
) < EXT4_HTREE_LEVEL
) {
779 ext4_warning(dir
->i_sb
, "Enable large directory "
780 "feature to access it");
785 entries
= (struct dx_entry
*)(((char *)&root
->info
) +
786 root
->info
.info_length
);
788 if (dx_get_limit(entries
) != dx_root_limit(dir
,
789 root
->info
.info_length
)) {
790 ext4_warning_inode(dir
, "dx entry: limit %u != root limit %u",
791 dx_get_limit(entries
),
792 dx_root_limit(dir
, root
->info
.info_length
));
796 dxtrace(printk("Look up %x", hash
));
798 count
= dx_get_count(entries
);
799 if (!count
|| count
> dx_get_limit(entries
)) {
800 ext4_warning_inode(dir
,
801 "dx entry: count %u beyond limit %u",
802 count
, dx_get_limit(entries
));
807 q
= entries
+ count
- 1;
810 dxtrace(printk(KERN_CONT
"."));
811 if (dx_get_hash(m
) > hash
)
817 if (0) { // linear search cross check
818 unsigned n
= count
- 1;
822 dxtrace(printk(KERN_CONT
","));
823 if (dx_get_hash(++at
) > hash
)
829 assert (at
== p
- 1);
833 dxtrace(printk(KERN_CONT
" %x->%u\n",
834 at
== entries
? 0 : dx_get_hash(at
),
836 frame
->entries
= entries
;
841 frame
->bh
= ext4_read_dirblock(dir
, dx_get_block(at
), INDEX
);
842 if (IS_ERR(frame
->bh
)) {
843 ret_err
= (struct dx_frame
*) frame
->bh
;
847 entries
= ((struct dx_node
*) frame
->bh
->b_data
)->entries
;
849 if (dx_get_limit(entries
) != dx_node_limit(dir
)) {
850 ext4_warning_inode(dir
,
851 "dx entry: limit %u != node limit %u",
852 dx_get_limit(entries
), dx_node_limit(dir
));
857 while (frame
>= frame_in
) {
862 if (ret_err
== ERR_PTR(ERR_BAD_DX_DIR
))
863 ext4_warning_inode(dir
,
864 "Corrupt directory, running e2fsck is recommended");
868 static void dx_release(struct dx_frame
*frames
)
870 struct dx_root_info
*info
;
873 if (frames
[0].bh
== NULL
)
876 info
= &((struct dx_root
*)frames
[0].bh
->b_data
)->info
;
877 for (i
= 0; i
<= info
->indirect_levels
; i
++) {
878 if (frames
[i
].bh
== NULL
)
880 brelse(frames
[i
].bh
);
886 * This function increments the frame pointer to search the next leaf
887 * block, and reads in the necessary intervening nodes if the search
888 * should be necessary. Whether or not the search is necessary is
889 * controlled by the hash parameter. If the hash value is even, then
890 * the search is only continued if the next block starts with that
891 * hash value. This is used if we are searching for a specific file.
893 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
895 * This function returns 1 if the caller should continue to search,
896 * or 0 if it should not. If there is an error reading one of the
897 * index blocks, it will a negative error code.
899 * If start_hash is non-null, it will be filled in with the starting
900 * hash of the next page.
902 static int ext4_htree_next_block(struct inode
*dir
, __u32 hash
,
903 struct dx_frame
*frame
,
904 struct dx_frame
*frames
,
908 struct buffer_head
*bh
;
914 * Find the next leaf page by incrementing the frame pointer.
915 * If we run out of entries in the interior node, loop around and
916 * increment pointer in the parent node. When we break out of
917 * this loop, num_frames indicates the number of interior
918 * nodes need to be read.
921 if (++(p
->at
) < p
->entries
+ dx_get_count(p
->entries
))
930 * If the hash is 1, then continue only if the next page has a
931 * continuation hash of any value. This is used for readdir
932 * handling. Otherwise, check to see if the hash matches the
933 * desired contiuation hash. If it doesn't, return since
934 * there's no point to read in the successive index pages.
936 bhash
= dx_get_hash(p
->at
);
939 if ((hash
& 1) == 0) {
940 if ((bhash
& ~1) != hash
)
944 * If the hash is HASH_NB_ALWAYS, we always go to the next
945 * block so no check is necessary
947 while (num_frames
--) {
948 bh
= ext4_read_dirblock(dir
, dx_get_block(p
->at
), INDEX
);
954 p
->at
= p
->entries
= ((struct dx_node
*) bh
->b_data
)->entries
;
961 * This function fills a red-black tree with information from a
962 * directory block. It returns the number directory entries loaded
963 * into the tree. If there is an error it is returned in err.
965 static int htree_dirblock_to_tree(struct file
*dir_file
,
966 struct inode
*dir
, ext4_lblk_t block
,
967 struct dx_hash_info
*hinfo
,
968 __u32 start_hash
, __u32 start_minor_hash
)
970 struct buffer_head
*bh
;
971 struct ext4_dir_entry_2
*de
, *top
;
972 int err
= 0, count
= 0;
973 struct fscrypt_str fname_crypto_str
= FSTR_INIT(NULL
, 0), tmp_str
;
975 dxtrace(printk(KERN_INFO
"In htree dirblock_to_tree: block %lu\n",
976 (unsigned long)block
));
977 bh
= ext4_read_dirblock(dir
, block
, DIRENT
);
981 de
= (struct ext4_dir_entry_2
*) bh
->b_data
;
982 top
= (struct ext4_dir_entry_2
*) ((char *) de
+
983 dir
->i_sb
->s_blocksize
-
984 EXT4_DIR_REC_LEN(0));
985 #ifdef CONFIG_EXT4_FS_ENCRYPTION
986 /* Check if the directory is encrypted */
987 if (ext4_encrypted_inode(dir
)) {
988 err
= fscrypt_get_encryption_info(dir
);
993 err
= fscrypt_fname_alloc_buffer(dir
, EXT4_NAME_LEN
,
1001 for (; de
< top
; de
= ext4_next_entry(de
, dir
->i_sb
->s_blocksize
)) {
1002 if (ext4_check_dir_entry(dir
, NULL
, de
, bh
,
1003 bh
->b_data
, bh
->b_size
,
1004 (block
<<EXT4_BLOCK_SIZE_BITS(dir
->i_sb
))
1005 + ((char *)de
- bh
->b_data
))) {
1006 /* silently ignore the rest of the block */
1009 ext4fs_dirhash(de
->name
, de
->name_len
, hinfo
);
1010 if ((hinfo
->hash
< start_hash
) ||
1011 ((hinfo
->hash
== start_hash
) &&
1012 (hinfo
->minor_hash
< start_minor_hash
)))
1016 if (!ext4_encrypted_inode(dir
)) {
1017 tmp_str
.name
= de
->name
;
1018 tmp_str
.len
= de
->name_len
;
1019 err
= ext4_htree_store_dirent(dir_file
,
1020 hinfo
->hash
, hinfo
->minor_hash
, de
,
1023 int save_len
= fname_crypto_str
.len
;
1024 struct fscrypt_str de_name
= FSTR_INIT(de
->name
,
1027 /* Directory is encrypted */
1028 err
= fscrypt_fname_disk_to_usr(dir
, hinfo
->hash
,
1029 hinfo
->minor_hash
, &de_name
,
1035 err
= ext4_htree_store_dirent(dir_file
,
1036 hinfo
->hash
, hinfo
->minor_hash
, de
,
1038 fname_crypto_str
.len
= save_len
;
1048 #ifdef CONFIG_EXT4_FS_ENCRYPTION
1049 fscrypt_fname_free_buffer(&fname_crypto_str
);
1056 * This function fills a red-black tree with information from a
1057 * directory. We start scanning the directory in hash order, starting
1058 * at start_hash and start_minor_hash.
1060 * This function returns the number of entries inserted into the tree,
1061 * or a negative error code.
1063 int ext4_htree_fill_tree(struct file
*dir_file
, __u32 start_hash
,
1064 __u32 start_minor_hash
, __u32
*next_hash
)
1066 struct dx_hash_info hinfo
;
1067 struct ext4_dir_entry_2
*de
;
1068 struct dx_frame frames
[EXT4_HTREE_LEVEL
], *frame
;
1074 struct fscrypt_str tmp_str
;
1076 dxtrace(printk(KERN_DEBUG
"In htree_fill_tree, start hash: %x:%x\n",
1077 start_hash
, start_minor_hash
));
1078 dir
= file_inode(dir_file
);
1079 if (!(ext4_test_inode_flag(dir
, EXT4_INODE_INDEX
))) {
1080 hinfo
.hash_version
= EXT4_SB(dir
->i_sb
)->s_def_hash_version
;
1081 if (hinfo
.hash_version
<= DX_HASH_TEA
)
1082 hinfo
.hash_version
+=
1083 EXT4_SB(dir
->i_sb
)->s_hash_unsigned
;
1084 hinfo
.seed
= EXT4_SB(dir
->i_sb
)->s_hash_seed
;
1085 if (ext4_has_inline_data(dir
)) {
1086 int has_inline_data
= 1;
1087 count
= htree_inlinedir_to_tree(dir_file
, dir
, 0,
1091 if (has_inline_data
) {
1096 count
= htree_dirblock_to_tree(dir_file
, dir
, 0, &hinfo
,
1097 start_hash
, start_minor_hash
);
1101 hinfo
.hash
= start_hash
;
1102 hinfo
.minor_hash
= 0;
1103 frame
= dx_probe(NULL
, dir
, &hinfo
, frames
);
1105 return PTR_ERR(frame
);
1107 /* Add '.' and '..' from the htree header */
1108 if (!start_hash
&& !start_minor_hash
) {
1109 de
= (struct ext4_dir_entry_2
*) frames
[0].bh
->b_data
;
1110 tmp_str
.name
= de
->name
;
1111 tmp_str
.len
= de
->name_len
;
1112 err
= ext4_htree_store_dirent(dir_file
, 0, 0,
1118 if (start_hash
< 2 || (start_hash
==2 && start_minor_hash
==0)) {
1119 de
= (struct ext4_dir_entry_2
*) frames
[0].bh
->b_data
;
1120 de
= ext4_next_entry(de
, dir
->i_sb
->s_blocksize
);
1121 tmp_str
.name
= de
->name
;
1122 tmp_str
.len
= de
->name_len
;
1123 err
= ext4_htree_store_dirent(dir_file
, 2, 0,
1131 if (fatal_signal_pending(current
)) {
1136 block
= dx_get_block(frame
->at
);
1137 ret
= htree_dirblock_to_tree(dir_file
, dir
, block
, &hinfo
,
1138 start_hash
, start_minor_hash
);
1145 ret
= ext4_htree_next_block(dir
, HASH_NB_ALWAYS
,
1146 frame
, frames
, &hashval
);
1147 *next_hash
= hashval
;
1153 * Stop if: (a) there are no more entries, or
1154 * (b) we have inserted at least one entry and the
1155 * next hash value is not a continuation
1158 (count
&& ((hashval
& 1) == 0)))
1162 dxtrace(printk(KERN_DEBUG
"Fill tree: returned %d entries, "
1163 "next hash: %x\n", count
, *next_hash
));
1170 static inline int search_dirblock(struct buffer_head
*bh
,
1172 struct ext4_filename
*fname
,
1173 unsigned int offset
,
1174 struct ext4_dir_entry_2
**res_dir
)
1176 return ext4_search_dir(bh
, bh
->b_data
, dir
->i_sb
->s_blocksize
, dir
,
1177 fname
, offset
, res_dir
);
1181 * Directory block splitting, compacting
1185 * Create map of hash values, offsets, and sizes, stored at end of block.
1186 * Returns number of entries mapped.
1188 static int dx_make_map(struct inode
*dir
, struct ext4_dir_entry_2
*de
,
1189 unsigned blocksize
, struct dx_hash_info
*hinfo
,
1190 struct dx_map_entry
*map_tail
)
1193 char *base
= (char *) de
;
1194 struct dx_hash_info h
= *hinfo
;
1196 while ((char *) de
< base
+ blocksize
) {
1197 if (de
->name_len
&& de
->inode
) {
1198 ext4fs_dirhash(de
->name
, de
->name_len
, &h
);
1200 map_tail
->hash
= h
.hash
;
1201 map_tail
->offs
= ((char *) de
- base
)>>2;
1202 map_tail
->size
= le16_to_cpu(de
->rec_len
);
1206 /* XXX: do we need to check rec_len == 0 case? -Chris */
1207 de
= ext4_next_entry(de
, blocksize
);
1212 /* Sort map by hash value */
1213 static void dx_sort_map (struct dx_map_entry
*map
, unsigned count
)
1215 struct dx_map_entry
*p
, *q
, *top
= map
+ count
- 1;
1217 /* Combsort until bubble sort doesn't suck */
1219 count
= count
*10/13;
1220 if (count
- 9 < 2) /* 9, 10 -> 11 */
1222 for (p
= top
, q
= p
- count
; q
>= map
; p
--, q
--)
1223 if (p
->hash
< q
->hash
)
1226 /* Garden variety bubble sort */
1231 if (q
[1].hash
>= q
[0].hash
)
1239 static void dx_insert_block(struct dx_frame
*frame
, u32 hash
, ext4_lblk_t block
)
1241 struct dx_entry
*entries
= frame
->entries
;
1242 struct dx_entry
*old
= frame
->at
, *new = old
+ 1;
1243 int count
= dx_get_count(entries
);
1245 assert(count
< dx_get_limit(entries
));
1246 assert(old
< entries
+ count
);
1247 memmove(new + 1, new, (char *)(entries
+ count
) - (char *)(new));
1248 dx_set_hash(new, hash
);
1249 dx_set_block(new, block
);
1250 dx_set_count(entries
, count
+ 1);
1254 * Test whether a directory entry matches the filename being searched for.
1256 * Return: %true if the directory entry matches, otherwise %false.
1258 static inline bool ext4_match(const struct ext4_filename
*fname
,
1259 const struct ext4_dir_entry_2
*de
)
1261 struct fscrypt_name f
;
1266 f
.usr_fname
= fname
->usr_fname
;
1267 f
.disk_name
= fname
->disk_name
;
1268 #ifdef CONFIG_EXT4_FS_ENCRYPTION
1269 f
.crypto_buf
= fname
->crypto_buf
;
1271 return fscrypt_match_name(&f
, de
->name
, de
->name_len
);
1275 * Returns 0 if not found, -1 on failure, and 1 on success
1277 int ext4_search_dir(struct buffer_head
*bh
, char *search_buf
, int buf_size
,
1278 struct inode
*dir
, struct ext4_filename
*fname
,
1279 unsigned int offset
, struct ext4_dir_entry_2
**res_dir
)
1281 struct ext4_dir_entry_2
* de
;
1285 de
= (struct ext4_dir_entry_2
*)search_buf
;
1286 dlimit
= search_buf
+ buf_size
;
1287 while ((char *) de
< dlimit
) {
1288 /* this code is executed quadratically often */
1289 /* do minimal checking `by hand' */
1290 if ((char *) de
+ de
->name_len
<= dlimit
&&
1291 ext4_match(fname
, de
)) {
1292 /* found a match - just to be sure, do
1294 if (ext4_check_dir_entry(dir
, NULL
, de
, bh
, bh
->b_data
,
1295 bh
->b_size
, offset
))
1300 /* prevent looping on a bad block */
1301 de_len
= ext4_rec_len_from_disk(de
->rec_len
,
1302 dir
->i_sb
->s_blocksize
);
1306 de
= (struct ext4_dir_entry_2
*) ((char *) de
+ de_len
);
1311 static int is_dx_internal_node(struct inode
*dir
, ext4_lblk_t block
,
1312 struct ext4_dir_entry
*de
)
1314 struct super_block
*sb
= dir
->i_sb
;
1320 if (de
->inode
== 0 &&
1321 ext4_rec_len_from_disk(de
->rec_len
, sb
->s_blocksize
) ==
1330 * finds an entry in the specified directory with the wanted name. It
1331 * returns the cache buffer in which the entry was found, and the entry
1332 * itself (as a parameter - res_dir). It does NOT read the inode of the
1333 * entry - you'll have to do that yourself if you want to.
1335 * The returned buffer_head has ->b_count elevated. The caller is expected
1336 * to brelse() it when appropriate.
1338 static struct buffer_head
* ext4_find_entry (struct inode
*dir
,
1339 const struct qstr
*d_name
,
1340 struct ext4_dir_entry_2
**res_dir
,
1343 struct super_block
*sb
;
1344 struct buffer_head
*bh_use
[NAMEI_RA_SIZE
];
1345 struct buffer_head
*bh
, *ret
= NULL
;
1346 ext4_lblk_t start
, block
;
1347 const u8
*name
= d_name
->name
;
1348 size_t ra_max
= 0; /* Number of bh's in the readahead
1350 size_t ra_ptr
= 0; /* Current index into readahead
1352 ext4_lblk_t nblocks
;
1353 int i
, namelen
, retval
;
1354 struct ext4_filename fname
;
1358 namelen
= d_name
->len
;
1359 if (namelen
> EXT4_NAME_LEN
)
1362 retval
= ext4_fname_setup_filename(dir
, d_name
, 1, &fname
);
1363 if (retval
== -ENOENT
)
1366 return ERR_PTR(retval
);
1368 if (ext4_has_inline_data(dir
)) {
1369 int has_inline_data
= 1;
1370 ret
= ext4_find_inline_entry(dir
, &fname
, res_dir
,
1372 if (has_inline_data
) {
1375 goto cleanup_and_exit
;
1379 if ((namelen
<= 2) && (name
[0] == '.') &&
1380 (name
[1] == '.' || name
[1] == '\0')) {
1382 * "." or ".." will only be in the first block
1383 * NFS may look up ".."; "." should be handled by the VFS
1390 ret
= ext4_dx_find_entry(dir
, &fname
, res_dir
);
1392 * On success, or if the error was file not found,
1393 * return. Otherwise, fall back to doing a search the
1394 * old fashioned way.
1396 if (!IS_ERR(ret
) || PTR_ERR(ret
) != ERR_BAD_DX_DIR
)
1397 goto cleanup_and_exit
;
1398 dxtrace(printk(KERN_DEBUG
"ext4_find_entry: dx failed, "
1402 nblocks
= dir
->i_size
>> EXT4_BLOCK_SIZE_BITS(sb
);
1405 goto cleanup_and_exit
;
1407 start
= EXT4_I(dir
)->i_dir_start_lookup
;
1408 if (start
>= nblocks
)
1414 * We deal with the read-ahead logic here.
1416 if (ra_ptr
>= ra_max
) {
1417 /* Refill the readahead buffer */
1420 ra_max
= start
- block
;
1422 ra_max
= nblocks
- block
;
1423 ra_max
= min(ra_max
, ARRAY_SIZE(bh_use
));
1424 retval
= ext4_bread_batch(dir
, block
, ra_max
,
1425 false /* wait */, bh_use
);
1427 ret
= ERR_PTR(retval
);
1429 goto cleanup_and_exit
;
1432 if ((bh
= bh_use
[ra_ptr
++]) == NULL
)
1435 if (!buffer_uptodate(bh
)) {
1436 EXT4_ERROR_INODE(dir
, "reading directory lblock %lu",
1437 (unsigned long) block
);
1439 ret
= ERR_PTR(-EIO
);
1440 goto cleanup_and_exit
;
1442 if (!buffer_verified(bh
) &&
1443 !is_dx_internal_node(dir
, block
,
1444 (struct ext4_dir_entry
*)bh
->b_data
) &&
1445 !ext4_dirent_csum_verify(dir
,
1446 (struct ext4_dir_entry
*)bh
->b_data
)) {
1447 EXT4_ERROR_INODE(dir
, "checksumming directory "
1448 "block %lu", (unsigned long)block
);
1450 ret
= ERR_PTR(-EFSBADCRC
);
1451 goto cleanup_and_exit
;
1453 set_buffer_verified(bh
);
1454 i
= search_dirblock(bh
, dir
, &fname
,
1455 block
<< EXT4_BLOCK_SIZE_BITS(sb
), res_dir
);
1457 EXT4_I(dir
)->i_dir_start_lookup
= block
;
1459 goto cleanup_and_exit
;
1463 goto cleanup_and_exit
;
1466 if (++block
>= nblocks
)
1468 } while (block
!= start
);
1471 * If the directory has grown while we were searching, then
1472 * search the last part of the directory before giving up.
1475 nblocks
= dir
->i_size
>> EXT4_BLOCK_SIZE_BITS(sb
);
1476 if (block
< nblocks
) {
1482 /* Clean up the read-ahead blocks */
1483 for (; ra_ptr
< ra_max
; ra_ptr
++)
1484 brelse(bh_use
[ra_ptr
]);
1485 ext4_fname_free_filename(&fname
);
1489 static struct buffer_head
* ext4_dx_find_entry(struct inode
*dir
,
1490 struct ext4_filename
*fname
,
1491 struct ext4_dir_entry_2
**res_dir
)
1493 struct super_block
* sb
= dir
->i_sb
;
1494 struct dx_frame frames
[EXT4_HTREE_LEVEL
], *frame
;
1495 struct buffer_head
*bh
;
1499 #ifdef CONFIG_EXT4_FS_ENCRYPTION
1502 frame
= dx_probe(fname
, dir
, NULL
, frames
);
1504 return (struct buffer_head
*) frame
;
1506 block
= dx_get_block(frame
->at
);
1507 bh
= ext4_read_dirblock(dir
, block
, DIRENT
);
1511 retval
= search_dirblock(bh
, dir
, fname
,
1512 block
<< EXT4_BLOCK_SIZE_BITS(sb
),
1518 bh
= ERR_PTR(ERR_BAD_DX_DIR
);
1522 /* Check to see if we should continue to search */
1523 retval
= ext4_htree_next_block(dir
, fname
->hinfo
.hash
, frame
,
1526 ext4_warning_inode(dir
,
1527 "error %d reading directory index block",
1529 bh
= ERR_PTR(retval
);
1532 } while (retval
== 1);
1536 dxtrace(printk(KERN_DEBUG
"%s not found\n", fname
->usr_fname
->name
));
1542 static struct dentry
*ext4_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
1544 struct inode
*inode
;
1545 struct ext4_dir_entry_2
*de
;
1546 struct buffer_head
*bh
;
1548 if (ext4_encrypted_inode(dir
)) {
1549 int res
= fscrypt_get_encryption_info(dir
);
1552 * DCACHE_ENCRYPTED_WITH_KEY is set if the dentry is
1553 * created while the directory was encrypted and we
1554 * have access to the key.
1556 if (fscrypt_has_encryption_key(dir
))
1557 fscrypt_set_encrypted_dentry(dentry
);
1558 fscrypt_set_d_op(dentry
);
1559 if (res
&& res
!= -ENOKEY
)
1560 return ERR_PTR(res
);
1563 if (dentry
->d_name
.len
> EXT4_NAME_LEN
)
1564 return ERR_PTR(-ENAMETOOLONG
);
1566 bh
= ext4_find_entry(dir
, &dentry
->d_name
, &de
, NULL
);
1568 return (struct dentry
*) bh
;
1571 __u32 ino
= le32_to_cpu(de
->inode
);
1573 if (!ext4_valid_inum(dir
->i_sb
, ino
)) {
1574 EXT4_ERROR_INODE(dir
, "bad inode number: %u", ino
);
1575 return ERR_PTR(-EFSCORRUPTED
);
1577 if (unlikely(ino
== dir
->i_ino
)) {
1578 EXT4_ERROR_INODE(dir
, "'%pd' linked to parent dir",
1580 return ERR_PTR(-EFSCORRUPTED
);
1582 inode
= ext4_iget_normal(dir
->i_sb
, ino
);
1583 if (inode
== ERR_PTR(-ESTALE
)) {
1584 EXT4_ERROR_INODE(dir
,
1585 "deleted inode referenced: %u",
1587 return ERR_PTR(-EFSCORRUPTED
);
1589 if (!IS_ERR(inode
) && ext4_encrypted_inode(dir
) &&
1590 (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
)) &&
1591 !fscrypt_has_permitted_context(dir
, inode
)) {
1592 ext4_warning(inode
->i_sb
,
1593 "Inconsistent encryption contexts: %lu/%lu",
1594 dir
->i_ino
, inode
->i_ino
);
1596 return ERR_PTR(-EPERM
);
1599 return d_splice_alias(inode
, dentry
);
1603 struct dentry
*ext4_get_parent(struct dentry
*child
)
1606 static const struct qstr dotdot
= QSTR_INIT("..", 2);
1607 struct ext4_dir_entry_2
* de
;
1608 struct buffer_head
*bh
;
1610 bh
= ext4_find_entry(d_inode(child
), &dotdot
, &de
, NULL
);
1612 return (struct dentry
*) bh
;
1614 return ERR_PTR(-ENOENT
);
1615 ino
= le32_to_cpu(de
->inode
);
1618 if (!ext4_valid_inum(child
->d_sb
, ino
)) {
1619 EXT4_ERROR_INODE(d_inode(child
),
1620 "bad parent inode number: %u", ino
);
1621 return ERR_PTR(-EFSCORRUPTED
);
1624 return d_obtain_alias(ext4_iget_normal(child
->d_sb
, ino
));
1628 * Move count entries from end of map between two memory locations.
1629 * Returns pointer to last entry moved.
1631 static struct ext4_dir_entry_2
*
1632 dx_move_dirents(char *from
, char *to
, struct dx_map_entry
*map
, int count
,
1635 unsigned rec_len
= 0;
1638 struct ext4_dir_entry_2
*de
= (struct ext4_dir_entry_2
*)
1639 (from
+ (map
->offs
<<2));
1640 rec_len
= EXT4_DIR_REC_LEN(de
->name_len
);
1641 memcpy (to
, de
, rec_len
);
1642 ((struct ext4_dir_entry_2
*) to
)->rec_len
=
1643 ext4_rec_len_to_disk(rec_len
, blocksize
);
1648 return (struct ext4_dir_entry_2
*) (to
- rec_len
);
1652 * Compact each dir entry in the range to the minimal rec_len.
1653 * Returns pointer to last entry in range.
1655 static struct ext4_dir_entry_2
* dx_pack_dirents(char *base
, unsigned blocksize
)
1657 struct ext4_dir_entry_2
*next
, *to
, *prev
, *de
= (struct ext4_dir_entry_2
*) base
;
1658 unsigned rec_len
= 0;
1661 while ((char*)de
< base
+ blocksize
) {
1662 next
= ext4_next_entry(de
, blocksize
);
1663 if (de
->inode
&& de
->name_len
) {
1664 rec_len
= EXT4_DIR_REC_LEN(de
->name_len
);
1666 memmove(to
, de
, rec_len
);
1667 to
->rec_len
= ext4_rec_len_to_disk(rec_len
, blocksize
);
1669 to
= (struct ext4_dir_entry_2
*) (((char *) to
) + rec_len
);
1677 * Split a full leaf block to make room for a new dir entry.
1678 * Allocate a new block, and move entries so that they are approx. equally full.
1679 * Returns pointer to de in block into which the new entry will be inserted.
1681 static struct ext4_dir_entry_2
*do_split(handle_t
*handle
, struct inode
*dir
,
1682 struct buffer_head
**bh
,struct dx_frame
*frame
,
1683 struct dx_hash_info
*hinfo
)
1685 unsigned blocksize
= dir
->i_sb
->s_blocksize
;
1686 unsigned count
, continued
;
1687 struct buffer_head
*bh2
;
1688 ext4_lblk_t newblock
;
1690 struct dx_map_entry
*map
;
1691 char *data1
= (*bh
)->b_data
, *data2
;
1692 unsigned split
, move
, size
;
1693 struct ext4_dir_entry_2
*de
= NULL
, *de2
;
1694 struct ext4_dir_entry_tail
*t
;
1698 if (ext4_has_metadata_csum(dir
->i_sb
))
1699 csum_size
= sizeof(struct ext4_dir_entry_tail
);
1701 bh2
= ext4_append(handle
, dir
, &newblock
);
1705 return (struct ext4_dir_entry_2
*) bh2
;
1708 BUFFER_TRACE(*bh
, "get_write_access");
1709 err
= ext4_journal_get_write_access(handle
, *bh
);
1713 BUFFER_TRACE(frame
->bh
, "get_write_access");
1714 err
= ext4_journal_get_write_access(handle
, frame
->bh
);
1718 data2
= bh2
->b_data
;
1720 /* create map in the end of data2 block */
1721 map
= (struct dx_map_entry
*) (data2
+ blocksize
);
1722 count
= dx_make_map(dir
, (struct ext4_dir_entry_2
*) data1
,
1723 blocksize
, hinfo
, map
);
1725 dx_sort_map(map
, count
);
1726 /* Split the existing block in the middle, size-wise */
1729 for (i
= count
-1; i
>= 0; i
--) {
1730 /* is more than half of this entry in 2nd half of the block? */
1731 if (size
+ map
[i
].size
/2 > blocksize
/2)
1733 size
+= map
[i
].size
;
1736 /* map index at which we will split */
1737 split
= count
- move
;
1738 hash2
= map
[split
].hash
;
1739 continued
= hash2
== map
[split
- 1].hash
;
1740 dxtrace(printk(KERN_INFO
"Split block %lu at %x, %i/%i\n",
1741 (unsigned long)dx_get_block(frame
->at
),
1742 hash2
, split
, count
-split
));
1744 /* Fancy dance to stay within two buffers */
1745 de2
= dx_move_dirents(data1
, data2
, map
+ split
, count
- split
,
1747 de
= dx_pack_dirents(data1
, blocksize
);
1748 de
->rec_len
= ext4_rec_len_to_disk(data1
+ (blocksize
- csum_size
) -
1751 de2
->rec_len
= ext4_rec_len_to_disk(data2
+ (blocksize
- csum_size
) -
1755 t
= EXT4_DIRENT_TAIL(data2
, blocksize
);
1756 initialize_dirent_tail(t
, blocksize
);
1758 t
= EXT4_DIRENT_TAIL(data1
, blocksize
);
1759 initialize_dirent_tail(t
, blocksize
);
1762 dxtrace(dx_show_leaf(dir
, hinfo
, (struct ext4_dir_entry_2
*) data1
,
1764 dxtrace(dx_show_leaf(dir
, hinfo
, (struct ext4_dir_entry_2
*) data2
,
1767 /* Which block gets the new entry? */
1768 if (hinfo
->hash
>= hash2
) {
1772 dx_insert_block(frame
, hash2
+ continued
, newblock
);
1773 err
= ext4_handle_dirty_dirent_node(handle
, dir
, bh2
);
1776 err
= ext4_handle_dirty_dx_node(handle
, dir
, frame
->bh
);
1780 dxtrace(dx_show_index("frame", frame
->entries
));
1787 ext4_std_error(dir
->i_sb
, err
);
1788 return ERR_PTR(err
);
1791 int ext4_find_dest_de(struct inode
*dir
, struct inode
*inode
,
1792 struct buffer_head
*bh
,
1793 void *buf
, int buf_size
,
1794 struct ext4_filename
*fname
,
1795 struct ext4_dir_entry_2
**dest_de
)
1797 struct ext4_dir_entry_2
*de
;
1798 unsigned short reclen
= EXT4_DIR_REC_LEN(fname_len(fname
));
1800 unsigned int offset
= 0;
1803 de
= (struct ext4_dir_entry_2
*)buf
;
1804 top
= buf
+ buf_size
- reclen
;
1805 while ((char *) de
<= top
) {
1806 if (ext4_check_dir_entry(dir
, NULL
, de
, bh
,
1807 buf
, buf_size
, offset
))
1808 return -EFSCORRUPTED
;
1809 if (ext4_match(fname
, de
))
1811 nlen
= EXT4_DIR_REC_LEN(de
->name_len
);
1812 rlen
= ext4_rec_len_from_disk(de
->rec_len
, buf_size
);
1813 if ((de
->inode
? rlen
- nlen
: rlen
) >= reclen
)
1815 de
= (struct ext4_dir_entry_2
*)((char *)de
+ rlen
);
1818 if ((char *) de
> top
)
1825 void ext4_insert_dentry(struct inode
*inode
,
1826 struct ext4_dir_entry_2
*de
,
1828 struct ext4_filename
*fname
)
1833 nlen
= EXT4_DIR_REC_LEN(de
->name_len
);
1834 rlen
= ext4_rec_len_from_disk(de
->rec_len
, buf_size
);
1836 struct ext4_dir_entry_2
*de1
=
1837 (struct ext4_dir_entry_2
*)((char *)de
+ nlen
);
1838 de1
->rec_len
= ext4_rec_len_to_disk(rlen
- nlen
, buf_size
);
1839 de
->rec_len
= ext4_rec_len_to_disk(nlen
, buf_size
);
1842 de
->file_type
= EXT4_FT_UNKNOWN
;
1843 de
->inode
= cpu_to_le32(inode
->i_ino
);
1844 ext4_set_de_type(inode
->i_sb
, de
, inode
->i_mode
);
1845 de
->name_len
= fname_len(fname
);
1846 memcpy(de
->name
, fname_name(fname
), fname_len(fname
));
1850 * Add a new entry into a directory (leaf) block. If de is non-NULL,
1851 * it points to a directory entry which is guaranteed to be large
1852 * enough for new directory entry. If de is NULL, then
1853 * add_dirent_to_buf will attempt search the directory block for
1854 * space. It will return -ENOSPC if no space is available, and -EIO
1855 * and -EEXIST if directory entry already exists.
1857 static int add_dirent_to_buf(handle_t
*handle
, struct ext4_filename
*fname
,
1859 struct inode
*inode
, struct ext4_dir_entry_2
*de
,
1860 struct buffer_head
*bh
)
1862 unsigned int blocksize
= dir
->i_sb
->s_blocksize
;
1866 if (ext4_has_metadata_csum(inode
->i_sb
))
1867 csum_size
= sizeof(struct ext4_dir_entry_tail
);
1870 err
= ext4_find_dest_de(dir
, inode
, bh
, bh
->b_data
,
1871 blocksize
- csum_size
, fname
, &de
);
1875 BUFFER_TRACE(bh
, "get_write_access");
1876 err
= ext4_journal_get_write_access(handle
, bh
);
1878 ext4_std_error(dir
->i_sb
, err
);
1882 /* By now the buffer is marked for journaling */
1883 ext4_insert_dentry(inode
, de
, blocksize
, fname
);
1886 * XXX shouldn't update any times until successful
1887 * completion of syscall, but too many callers depend
1890 * XXX similarly, too many callers depend on
1891 * ext4_new_inode() setting the times, but error
1892 * recovery deletes the inode, so the worst that can
1893 * happen is that the times are slightly out of date
1894 * and/or different from the directory change time.
1896 dir
->i_mtime
= dir
->i_ctime
= current_time(dir
);
1897 ext4_update_dx_flag(dir
);
1898 inode_inc_iversion(dir
);
1899 ext4_mark_inode_dirty(handle
, dir
);
1900 BUFFER_TRACE(bh
, "call ext4_handle_dirty_metadata");
1901 err
= ext4_handle_dirty_dirent_node(handle
, dir
, bh
);
1903 ext4_std_error(dir
->i_sb
, err
);
1908 * This converts a one block unindexed directory to a 3 block indexed
1909 * directory, and adds the dentry to the indexed directory.
1911 static int make_indexed_dir(handle_t
*handle
, struct ext4_filename
*fname
,
1913 struct inode
*inode
, struct buffer_head
*bh
)
1915 struct buffer_head
*bh2
;
1916 struct dx_root
*root
;
1917 struct dx_frame frames
[EXT4_HTREE_LEVEL
], *frame
;
1918 struct dx_entry
*entries
;
1919 struct ext4_dir_entry_2
*de
, *de2
;
1920 struct ext4_dir_entry_tail
*t
;
1926 struct fake_dirent
*fde
;
1929 if (ext4_has_metadata_csum(inode
->i_sb
))
1930 csum_size
= sizeof(struct ext4_dir_entry_tail
);
1932 blocksize
= dir
->i_sb
->s_blocksize
;
1933 dxtrace(printk(KERN_DEBUG
"Creating index: inode %lu\n", dir
->i_ino
));
1934 BUFFER_TRACE(bh
, "get_write_access");
1935 retval
= ext4_journal_get_write_access(handle
, bh
);
1937 ext4_std_error(dir
->i_sb
, retval
);
1941 root
= (struct dx_root
*) bh
->b_data
;
1943 /* The 0th block becomes the root, move the dirents out */
1944 fde
= &root
->dotdot
;
1945 de
= (struct ext4_dir_entry_2
*)((char *)fde
+
1946 ext4_rec_len_from_disk(fde
->rec_len
, blocksize
));
1947 if ((char *) de
>= (((char *) root
) + blocksize
)) {
1948 EXT4_ERROR_INODE(dir
, "invalid rec_len for '..'");
1950 return -EFSCORRUPTED
;
1952 len
= ((char *) root
) + (blocksize
- csum_size
) - (char *) de
;
1954 /* Allocate new block for the 0th block's dirents */
1955 bh2
= ext4_append(handle
, dir
, &block
);
1958 return PTR_ERR(bh2
);
1960 ext4_set_inode_flag(dir
, EXT4_INODE_INDEX
);
1961 data1
= bh2
->b_data
;
1963 memcpy (data1
, de
, len
);
1964 de
= (struct ext4_dir_entry_2
*) data1
;
1966 while ((char *)(de2
= ext4_next_entry(de
, blocksize
)) < top
)
1968 de
->rec_len
= ext4_rec_len_to_disk(data1
+ (blocksize
- csum_size
) -
1973 t
= EXT4_DIRENT_TAIL(data1
, blocksize
);
1974 initialize_dirent_tail(t
, blocksize
);
1977 /* Initialize the root; the dot dirents already exist */
1978 de
= (struct ext4_dir_entry_2
*) (&root
->dotdot
);
1979 de
->rec_len
= ext4_rec_len_to_disk(blocksize
- EXT4_DIR_REC_LEN(2),
1981 memset (&root
->info
, 0, sizeof(root
->info
));
1982 root
->info
.info_length
= sizeof(root
->info
);
1983 root
->info
.hash_version
= EXT4_SB(dir
->i_sb
)->s_def_hash_version
;
1984 entries
= root
->entries
;
1985 dx_set_block(entries
, 1);
1986 dx_set_count(entries
, 1);
1987 dx_set_limit(entries
, dx_root_limit(dir
, sizeof(root
->info
)));
1989 /* Initialize as for dx_probe */
1990 fname
->hinfo
.hash_version
= root
->info
.hash_version
;
1991 if (fname
->hinfo
.hash_version
<= DX_HASH_TEA
)
1992 fname
->hinfo
.hash_version
+= EXT4_SB(dir
->i_sb
)->s_hash_unsigned
;
1993 fname
->hinfo
.seed
= EXT4_SB(dir
->i_sb
)->s_hash_seed
;
1994 ext4fs_dirhash(fname_name(fname
), fname_len(fname
), &fname
->hinfo
);
1996 memset(frames
, 0, sizeof(frames
));
1998 frame
->entries
= entries
;
1999 frame
->at
= entries
;
2002 retval
= ext4_handle_dirty_dx_node(handle
, dir
, frame
->bh
);
2005 retval
= ext4_handle_dirty_dirent_node(handle
, dir
, bh2
);
2009 de
= do_split(handle
,dir
, &bh2
, frame
, &fname
->hinfo
);
2011 retval
= PTR_ERR(de
);
2015 retval
= add_dirent_to_buf(handle
, fname
, dir
, inode
, de
, bh2
);
2018 * Even if the block split failed, we have to properly write
2019 * out all the changes we did so far. Otherwise we can end up
2020 * with corrupted filesystem.
2023 ext4_mark_inode_dirty(handle
, dir
);
2032 * adds a file entry to the specified directory, using the same
2033 * semantics as ext4_find_entry(). It returns NULL if it failed.
2035 * NOTE!! The inode part of 'de' is left at 0 - which means you
2036 * may not sleep between calling this and putting something into
2037 * the entry, as someone else might have used it while you slept.
2039 static int ext4_add_entry(handle_t
*handle
, struct dentry
*dentry
,
2040 struct inode
*inode
)
2042 struct inode
*dir
= d_inode(dentry
->d_parent
);
2043 struct buffer_head
*bh
= NULL
;
2044 struct ext4_dir_entry_2
*de
;
2045 struct ext4_dir_entry_tail
*t
;
2046 struct super_block
*sb
;
2047 struct ext4_filename fname
;
2051 ext4_lblk_t block
, blocks
;
2054 if (ext4_has_metadata_csum(inode
->i_sb
))
2055 csum_size
= sizeof(struct ext4_dir_entry_tail
);
2058 blocksize
= sb
->s_blocksize
;
2059 if (!dentry
->d_name
.len
)
2062 retval
= ext4_fname_setup_filename(dir
, &dentry
->d_name
, 0, &fname
);
2066 if (ext4_has_inline_data(dir
)) {
2067 retval
= ext4_try_add_inline_entry(handle
, &fname
, dir
, inode
);
2077 retval
= ext4_dx_add_entry(handle
, &fname
, dir
, inode
);
2078 if (!retval
|| (retval
!= ERR_BAD_DX_DIR
))
2080 ext4_clear_inode_flag(dir
, EXT4_INODE_INDEX
);
2082 ext4_mark_inode_dirty(handle
, dir
);
2084 blocks
= dir
->i_size
>> sb
->s_blocksize_bits
;
2085 for (block
= 0; block
< blocks
; block
++) {
2086 bh
= ext4_read_dirblock(dir
, block
, DIRENT
);
2088 retval
= PTR_ERR(bh
);
2092 retval
= add_dirent_to_buf(handle
, &fname
, dir
, inode
,
2094 if (retval
!= -ENOSPC
)
2097 if (blocks
== 1 && !dx_fallback
&&
2098 ext4_has_feature_dir_index(sb
)) {
2099 retval
= make_indexed_dir(handle
, &fname
, dir
,
2101 bh
= NULL
; /* make_indexed_dir releases bh */
2106 bh
= ext4_append(handle
, dir
, &block
);
2108 retval
= PTR_ERR(bh
);
2112 de
= (struct ext4_dir_entry_2
*) bh
->b_data
;
2114 de
->rec_len
= ext4_rec_len_to_disk(blocksize
- csum_size
, blocksize
);
2117 t
= EXT4_DIRENT_TAIL(bh
->b_data
, blocksize
);
2118 initialize_dirent_tail(t
, blocksize
);
2121 retval
= add_dirent_to_buf(handle
, &fname
, dir
, inode
, de
, bh
);
2123 ext4_fname_free_filename(&fname
);
2126 ext4_set_inode_state(inode
, EXT4_STATE_NEWENTRY
);
2131 * Returns 0 for success, or a negative error value
2133 static int ext4_dx_add_entry(handle_t
*handle
, struct ext4_filename
*fname
,
2134 struct inode
*dir
, struct inode
*inode
)
2136 struct dx_frame frames
[EXT4_HTREE_LEVEL
], *frame
;
2137 struct dx_entry
*entries
, *at
;
2138 struct buffer_head
*bh
;
2139 struct super_block
*sb
= dir
->i_sb
;
2140 struct ext4_dir_entry_2
*de
;
2146 frame
= dx_probe(fname
, dir
, NULL
, frames
);
2148 return PTR_ERR(frame
);
2149 entries
= frame
->entries
;
2151 bh
= ext4_read_dirblock(dir
, dx_get_block(frame
->at
), DIRENT
);
2158 BUFFER_TRACE(bh
, "get_write_access");
2159 err
= ext4_journal_get_write_access(handle
, bh
);
2163 err
= add_dirent_to_buf(handle
, fname
, dir
, inode
, NULL
, bh
);
2168 /* Block full, should compress but for now just split */
2169 dxtrace(printk(KERN_DEBUG
"using %u of %u node entries\n",
2170 dx_get_count(entries
), dx_get_limit(entries
)));
2171 /* Need to split index? */
2172 if (dx_get_count(entries
) == dx_get_limit(entries
)) {
2173 ext4_lblk_t newblock
;
2174 int levels
= frame
- frames
+ 1;
2175 unsigned int icount
;
2177 struct dx_entry
*entries2
;
2178 struct dx_node
*node2
;
2179 struct buffer_head
*bh2
;
2181 while (frame
> frames
) {
2182 if (dx_get_count((frame
- 1)->entries
) <
2183 dx_get_limit((frame
- 1)->entries
)) {
2187 frame
--; /* split higher index block */
2189 entries
= frame
->entries
;
2192 if (add_level
&& levels
== ext4_dir_htree_level(sb
)) {
2193 ext4_warning(sb
, "Directory (ino: %lu) index full, "
2194 "reach max htree level :%d",
2195 dir
->i_ino
, levels
);
2196 if (ext4_dir_htree_level(sb
) < EXT4_HTREE_LEVEL
) {
2197 ext4_warning(sb
, "Large directory feature is "
2198 "not enabled on this "
2204 icount
= dx_get_count(entries
);
2205 bh2
= ext4_append(handle
, dir
, &newblock
);
2210 node2
= (struct dx_node
*)(bh2
->b_data
);
2211 entries2
= node2
->entries
;
2212 memset(&node2
->fake
, 0, sizeof(struct fake_dirent
));
2213 node2
->fake
.rec_len
= ext4_rec_len_to_disk(sb
->s_blocksize
,
2215 BUFFER_TRACE(frame
->bh
, "get_write_access");
2216 err
= ext4_journal_get_write_access(handle
, frame
->bh
);
2220 unsigned icount1
= icount
/2, icount2
= icount
- icount1
;
2221 unsigned hash2
= dx_get_hash(entries
+ icount1
);
2222 dxtrace(printk(KERN_DEBUG
"Split index %i/%i\n",
2225 BUFFER_TRACE(frame
->bh
, "get_write_access"); /* index root */
2226 err
= ext4_journal_get_write_access(handle
,
2231 memcpy((char *) entries2
, (char *) (entries
+ icount1
),
2232 icount2
* sizeof(struct dx_entry
));
2233 dx_set_count(entries
, icount1
);
2234 dx_set_count(entries2
, icount2
);
2235 dx_set_limit(entries2
, dx_node_limit(dir
));
2237 /* Which index block gets the new entry? */
2238 if (at
- entries
>= icount1
) {
2239 frame
->at
= at
= at
- entries
- icount1
+ entries2
;
2240 frame
->entries
= entries
= entries2
;
2241 swap(frame
->bh
, bh2
);
2243 dx_insert_block((frame
- 1), hash2
, newblock
);
2244 dxtrace(dx_show_index("node", frame
->entries
));
2245 dxtrace(dx_show_index("node",
2246 ((struct dx_node
*) bh2
->b_data
)->entries
));
2247 err
= ext4_handle_dirty_dx_node(handle
, dir
, bh2
);
2251 err
= ext4_handle_dirty_dx_node(handle
, dir
,
2256 err
= ext4_handle_dirty_dx_node(handle
, dir
,
2261 struct dx_root
*dxroot
;
2262 memcpy((char *) entries2
, (char *) entries
,
2263 icount
* sizeof(struct dx_entry
));
2264 dx_set_limit(entries2
, dx_node_limit(dir
));
2267 dx_set_count(entries
, 1);
2268 dx_set_block(entries
+ 0, newblock
);
2269 dxroot
= (struct dx_root
*)frames
[0].bh
->b_data
;
2270 dxroot
->info
.indirect_levels
+= 1;
2271 dxtrace(printk(KERN_DEBUG
2272 "Creating %d level index...\n",
2273 info
->indirect_levels
));
2274 err
= ext4_handle_dirty_dx_node(handle
, dir
, frame
->bh
);
2277 err
= ext4_handle_dirty_dx_node(handle
, dir
, bh2
);
2283 de
= do_split(handle
, dir
, &bh
, frame
, &fname
->hinfo
);
2288 err
= add_dirent_to_buf(handle
, fname
, dir
, inode
, de
, bh
);
2292 ext4_std_error(dir
->i_sb
, err
); /* this is a no-op if err == 0 */
2296 /* @restart is true means htree-path has been changed, we need to
2297 * repeat dx_probe() to find out valid htree-path
2299 if (restart
&& err
== 0)
2305 * ext4_generic_delete_entry deletes a directory entry by merging it
2306 * with the previous entry
2308 int ext4_generic_delete_entry(handle_t
*handle
,
2310 struct ext4_dir_entry_2
*de_del
,
2311 struct buffer_head
*bh
,
2316 struct ext4_dir_entry_2
*de
, *pde
;
2317 unsigned int blocksize
= dir
->i_sb
->s_blocksize
;
2322 de
= (struct ext4_dir_entry_2
*)entry_buf
;
2323 while (i
< buf_size
- csum_size
) {
2324 if (ext4_check_dir_entry(dir
, NULL
, de
, bh
,
2325 bh
->b_data
, bh
->b_size
, i
))
2326 return -EFSCORRUPTED
;
2329 pde
->rec_len
= ext4_rec_len_to_disk(
2330 ext4_rec_len_from_disk(pde
->rec_len
,
2332 ext4_rec_len_from_disk(de
->rec_len
,
2337 inode_inc_iversion(dir
);
2340 i
+= ext4_rec_len_from_disk(de
->rec_len
, blocksize
);
2342 de
= ext4_next_entry(de
, blocksize
);
2347 static int ext4_delete_entry(handle_t
*handle
,
2349 struct ext4_dir_entry_2
*de_del
,
2350 struct buffer_head
*bh
)
2352 int err
, csum_size
= 0;
2354 if (ext4_has_inline_data(dir
)) {
2355 int has_inline_data
= 1;
2356 err
= ext4_delete_inline_entry(handle
, dir
, de_del
, bh
,
2358 if (has_inline_data
)
2362 if (ext4_has_metadata_csum(dir
->i_sb
))
2363 csum_size
= sizeof(struct ext4_dir_entry_tail
);
2365 BUFFER_TRACE(bh
, "get_write_access");
2366 err
= ext4_journal_get_write_access(handle
, bh
);
2370 err
= ext4_generic_delete_entry(handle
, dir
, de_del
,
2372 dir
->i_sb
->s_blocksize
, csum_size
);
2376 BUFFER_TRACE(bh
, "call ext4_handle_dirty_metadata");
2377 err
= ext4_handle_dirty_dirent_node(handle
, dir
, bh
);
2384 ext4_std_error(dir
->i_sb
, err
);
2389 * Set directory link count to 1 if nlinks > EXT4_LINK_MAX, or if nlinks == 2
2390 * since this indicates that nlinks count was previously 1 to avoid overflowing
2391 * the 16-bit i_links_count field on disk. Directories with i_nlink == 1 mean
2392 * that subdirectory link counts are not being maintained accurately.
2394 * The caller has already checked for i_nlink overflow in case the DIR_LINK
2395 * feature is not enabled and returned -EMLINK. The is_dx() check is a proxy
2396 * for checking S_ISDIR(inode) (since the INODE_INDEX feature will not be set
2397 * on regular files) and to avoid creating huge/slow non-HTREE directories.
2399 static void ext4_inc_count(handle_t
*handle
, struct inode
*inode
)
2403 (inode
->i_nlink
> EXT4_LINK_MAX
|| inode
->i_nlink
== 2))
2404 set_nlink(inode
, 1);
2408 * If a directory had nlink == 1, then we should let it be 1. This indicates
2409 * directory has >EXT4_LINK_MAX subdirs.
2411 static void ext4_dec_count(handle_t
*handle
, struct inode
*inode
)
2413 if (!S_ISDIR(inode
->i_mode
) || inode
->i_nlink
> 2)
2418 static int ext4_add_nondir(handle_t
*handle
,
2419 struct dentry
*dentry
, struct inode
*inode
)
2421 int err
= ext4_add_entry(handle
, dentry
, inode
);
2423 ext4_mark_inode_dirty(handle
, inode
);
2424 d_instantiate_new(dentry
, inode
);
2428 unlock_new_inode(inode
);
2434 * By the time this is called, we already have created
2435 * the directory cache entry for the new file, but it
2436 * is so far negative - it has no inode.
2438 * If the create succeeds, we fill in the inode information
2439 * with d_instantiate().
2441 static int ext4_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
2445 struct inode
*inode
;
2446 int err
, credits
, retries
= 0;
2448 err
= dquot_initialize(dir
);
2452 credits
= (EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
2453 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 3);
2455 inode
= ext4_new_inode_start_handle(dir
, mode
, &dentry
->d_name
, 0,
2456 NULL
, EXT4_HT_DIR
, credits
);
2457 handle
= ext4_journal_current_handle();
2458 err
= PTR_ERR(inode
);
2459 if (!IS_ERR(inode
)) {
2460 inode
->i_op
= &ext4_file_inode_operations
;
2461 inode
->i_fop
= &ext4_file_operations
;
2462 ext4_set_aops(inode
);
2463 err
= ext4_add_nondir(handle
, dentry
, inode
);
2464 if (!err
&& IS_DIRSYNC(dir
))
2465 ext4_handle_sync(handle
);
2468 ext4_journal_stop(handle
);
2469 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
2474 static int ext4_mknod(struct inode
*dir
, struct dentry
*dentry
,
2475 umode_t mode
, dev_t rdev
)
2478 struct inode
*inode
;
2479 int err
, credits
, retries
= 0;
2481 err
= dquot_initialize(dir
);
2485 credits
= (EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
2486 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 3);
2488 inode
= ext4_new_inode_start_handle(dir
, mode
, &dentry
->d_name
, 0,
2489 NULL
, EXT4_HT_DIR
, credits
);
2490 handle
= ext4_journal_current_handle();
2491 err
= PTR_ERR(inode
);
2492 if (!IS_ERR(inode
)) {
2493 init_special_inode(inode
, inode
->i_mode
, rdev
);
2494 inode
->i_op
= &ext4_special_inode_operations
;
2495 err
= ext4_add_nondir(handle
, dentry
, inode
);
2496 if (!err
&& IS_DIRSYNC(dir
))
2497 ext4_handle_sync(handle
);
2500 ext4_journal_stop(handle
);
2501 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
2506 static int ext4_tmpfile(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
2509 struct inode
*inode
;
2510 int err
, retries
= 0;
2512 err
= dquot_initialize(dir
);
2517 inode
= ext4_new_inode_start_handle(dir
, mode
,
2520 EXT4_MAXQUOTAS_INIT_BLOCKS(dir
->i_sb
) +
2521 4 + EXT4_XATTR_TRANS_BLOCKS
);
2522 handle
= ext4_journal_current_handle();
2523 err
= PTR_ERR(inode
);
2524 if (!IS_ERR(inode
)) {
2525 inode
->i_op
= &ext4_file_inode_operations
;
2526 inode
->i_fop
= &ext4_file_operations
;
2527 ext4_set_aops(inode
);
2528 d_tmpfile(dentry
, inode
);
2529 err
= ext4_orphan_add(handle
, inode
);
2531 goto err_unlock_inode
;
2532 mark_inode_dirty(inode
);
2533 unlock_new_inode(inode
);
2536 ext4_journal_stop(handle
);
2537 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
2541 ext4_journal_stop(handle
);
2542 unlock_new_inode(inode
);
2546 struct ext4_dir_entry_2
*ext4_init_dot_dotdot(struct inode
*inode
,
2547 struct ext4_dir_entry_2
*de
,
2548 int blocksize
, int csum_size
,
2549 unsigned int parent_ino
, int dotdot_real_len
)
2551 de
->inode
= cpu_to_le32(inode
->i_ino
);
2553 de
->rec_len
= ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de
->name_len
),
2555 strcpy(de
->name
, ".");
2556 ext4_set_de_type(inode
->i_sb
, de
, S_IFDIR
);
2558 de
= ext4_next_entry(de
, blocksize
);
2559 de
->inode
= cpu_to_le32(parent_ino
);
2561 if (!dotdot_real_len
)
2562 de
->rec_len
= ext4_rec_len_to_disk(blocksize
-
2563 (csum_size
+ EXT4_DIR_REC_LEN(1)),
2566 de
->rec_len
= ext4_rec_len_to_disk(
2567 EXT4_DIR_REC_LEN(de
->name_len
), blocksize
);
2568 strcpy(de
->name
, "..");
2569 ext4_set_de_type(inode
->i_sb
, de
, S_IFDIR
);
2571 return ext4_next_entry(de
, blocksize
);
2574 static int ext4_init_new_dir(handle_t
*handle
, struct inode
*dir
,
2575 struct inode
*inode
)
2577 struct buffer_head
*dir_block
= NULL
;
2578 struct ext4_dir_entry_2
*de
;
2579 struct ext4_dir_entry_tail
*t
;
2580 ext4_lblk_t block
= 0;
2581 unsigned int blocksize
= dir
->i_sb
->s_blocksize
;
2585 if (ext4_has_metadata_csum(dir
->i_sb
))
2586 csum_size
= sizeof(struct ext4_dir_entry_tail
);
2588 if (ext4_test_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
)) {
2589 err
= ext4_try_create_inline_dir(handle
, dir
, inode
);
2590 if (err
< 0 && err
!= -ENOSPC
)
2597 dir_block
= ext4_append(handle
, inode
, &block
);
2598 if (IS_ERR(dir_block
))
2599 return PTR_ERR(dir_block
);
2600 de
= (struct ext4_dir_entry_2
*)dir_block
->b_data
;
2601 ext4_init_dot_dotdot(inode
, de
, blocksize
, csum_size
, dir
->i_ino
, 0);
2602 set_nlink(inode
, 2);
2604 t
= EXT4_DIRENT_TAIL(dir_block
->b_data
, blocksize
);
2605 initialize_dirent_tail(t
, blocksize
);
2608 BUFFER_TRACE(dir_block
, "call ext4_handle_dirty_metadata");
2609 err
= ext4_handle_dirty_dirent_node(handle
, inode
, dir_block
);
2612 set_buffer_verified(dir_block
);
2618 static int ext4_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
2621 struct inode
*inode
;
2622 int err
, credits
, retries
= 0;
2624 if (EXT4_DIR_LINK_MAX(dir
))
2627 err
= dquot_initialize(dir
);
2631 credits
= (EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
2632 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 3);
2634 inode
= ext4_new_inode_start_handle(dir
, S_IFDIR
| mode
,
2636 0, NULL
, EXT4_HT_DIR
, credits
);
2637 handle
= ext4_journal_current_handle();
2638 err
= PTR_ERR(inode
);
2642 inode
->i_op
= &ext4_dir_inode_operations
;
2643 inode
->i_fop
= &ext4_dir_operations
;
2644 err
= ext4_init_new_dir(handle
, dir
, inode
);
2646 goto out_clear_inode
;
2647 err
= ext4_mark_inode_dirty(handle
, inode
);
2649 err
= ext4_add_entry(handle
, dentry
, inode
);
2653 unlock_new_inode(inode
);
2654 ext4_mark_inode_dirty(handle
, inode
);
2658 ext4_inc_count(handle
, dir
);
2659 ext4_update_dx_flag(dir
);
2660 err
= ext4_mark_inode_dirty(handle
, dir
);
2662 goto out_clear_inode
;
2663 d_instantiate_new(dentry
, inode
);
2664 if (IS_DIRSYNC(dir
))
2665 ext4_handle_sync(handle
);
2669 ext4_journal_stop(handle
);
2670 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
2676 * routine to check that the specified directory is empty (for rmdir)
2678 bool ext4_empty_dir(struct inode
*inode
)
2680 unsigned int offset
;
2681 struct buffer_head
*bh
;
2682 struct ext4_dir_entry_2
*de
, *de1
;
2683 struct super_block
*sb
;
2685 if (ext4_has_inline_data(inode
)) {
2686 int has_inline_data
= 1;
2689 ret
= empty_inline_dir(inode
, &has_inline_data
);
2690 if (has_inline_data
)
2695 if (inode
->i_size
< EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2)) {
2696 EXT4_ERROR_INODE(inode
, "invalid size");
2699 bh
= ext4_read_dirblock(inode
, 0, EITHER
);
2703 de
= (struct ext4_dir_entry_2
*) bh
->b_data
;
2704 de1
= ext4_next_entry(de
, sb
->s_blocksize
);
2705 if (le32_to_cpu(de
->inode
) != inode
->i_ino
||
2706 le32_to_cpu(de1
->inode
) == 0 ||
2707 strcmp(".", de
->name
) || strcmp("..", de1
->name
)) {
2708 ext4_warning_inode(inode
, "directory missing '.' and/or '..'");
2712 offset
= ext4_rec_len_from_disk(de
->rec_len
, sb
->s_blocksize
) +
2713 ext4_rec_len_from_disk(de1
->rec_len
, sb
->s_blocksize
);
2714 de
= ext4_next_entry(de1
, sb
->s_blocksize
);
2715 while (offset
< inode
->i_size
) {
2716 if ((void *) de
>= (void *) (bh
->b_data
+sb
->s_blocksize
)) {
2717 unsigned int lblock
;
2719 lblock
= offset
>> EXT4_BLOCK_SIZE_BITS(sb
);
2720 bh
= ext4_read_dirblock(inode
, lblock
, EITHER
);
2723 de
= (struct ext4_dir_entry_2
*) bh
->b_data
;
2725 if (ext4_check_dir_entry(inode
, NULL
, de
, bh
,
2726 bh
->b_data
, bh
->b_size
, offset
)) {
2727 de
= (struct ext4_dir_entry_2
*)(bh
->b_data
+
2729 offset
= (offset
| (sb
->s_blocksize
- 1)) + 1;
2732 if (le32_to_cpu(de
->inode
)) {
2736 offset
+= ext4_rec_len_from_disk(de
->rec_len
, sb
->s_blocksize
);
2737 de
= ext4_next_entry(de
, sb
->s_blocksize
);
2744 * ext4_orphan_add() links an unlinked or truncated inode into a list of
2745 * such inodes, starting at the superblock, in case we crash before the
2746 * file is closed/deleted, or in case the inode truncate spans multiple
2747 * transactions and the last transaction is not recovered after a crash.
2749 * At filesystem recovery time, we walk this list deleting unlinked
2750 * inodes and truncating linked inodes in ext4_orphan_cleanup().
2752 * Orphan list manipulation functions must be called under i_mutex unless
2753 * we are just creating the inode or deleting it.
2755 int ext4_orphan_add(handle_t
*handle
, struct inode
*inode
)
2757 struct super_block
*sb
= inode
->i_sb
;
2758 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2759 struct ext4_iloc iloc
;
2763 if (!sbi
->s_journal
|| is_bad_inode(inode
))
2766 WARN_ON_ONCE(!(inode
->i_state
& (I_NEW
| I_FREEING
)) &&
2767 !inode_is_locked(inode
));
2769 * Exit early if inode already is on orphan list. This is a big speedup
2770 * since we don't have to contend on the global s_orphan_lock.
2772 if (!list_empty(&EXT4_I(inode
)->i_orphan
))
2776 * Orphan handling is only valid for files with data blocks
2777 * being truncated, or files being unlinked. Note that we either
2778 * hold i_mutex, or the inode can not be referenced from outside,
2779 * so i_nlink should not be bumped due to race
2781 J_ASSERT((S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
2782 S_ISLNK(inode
->i_mode
)) || inode
->i_nlink
== 0);
2784 BUFFER_TRACE(sbi
->s_sbh
, "get_write_access");
2785 err
= ext4_journal_get_write_access(handle
, sbi
->s_sbh
);
2789 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
2793 mutex_lock(&sbi
->s_orphan_lock
);
2795 * Due to previous errors inode may be already a part of on-disk
2796 * orphan list. If so skip on-disk list modification.
2798 if (!NEXT_ORPHAN(inode
) || NEXT_ORPHAN(inode
) >
2799 (le32_to_cpu(sbi
->s_es
->s_inodes_count
))) {
2800 /* Insert this inode at the head of the on-disk orphan list */
2801 NEXT_ORPHAN(inode
) = le32_to_cpu(sbi
->s_es
->s_last_orphan
);
2802 sbi
->s_es
->s_last_orphan
= cpu_to_le32(inode
->i_ino
);
2805 list_add(&EXT4_I(inode
)->i_orphan
, &sbi
->s_orphan
);
2806 mutex_unlock(&sbi
->s_orphan_lock
);
2809 err
= ext4_handle_dirty_super(handle
, sb
);
2810 rc
= ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
2815 * We have to remove inode from in-memory list if
2816 * addition to on disk orphan list failed. Stray orphan
2817 * list entries can cause panics at unmount time.
2819 mutex_lock(&sbi
->s_orphan_lock
);
2820 list_del_init(&EXT4_I(inode
)->i_orphan
);
2821 mutex_unlock(&sbi
->s_orphan_lock
);
2824 jbd_debug(4, "superblock will point to %lu\n", inode
->i_ino
);
2825 jbd_debug(4, "orphan inode %lu will point to %d\n",
2826 inode
->i_ino
, NEXT_ORPHAN(inode
));
2828 ext4_std_error(sb
, err
);
2833 * ext4_orphan_del() removes an unlinked or truncated inode from the list
2834 * of such inodes stored on disk, because it is finally being cleaned up.
2836 int ext4_orphan_del(handle_t
*handle
, struct inode
*inode
)
2838 struct list_head
*prev
;
2839 struct ext4_inode_info
*ei
= EXT4_I(inode
);
2840 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
2842 struct ext4_iloc iloc
;
2845 if (!sbi
->s_journal
&& !(sbi
->s_mount_state
& EXT4_ORPHAN_FS
))
2848 WARN_ON_ONCE(!(inode
->i_state
& (I_NEW
| I_FREEING
)) &&
2849 !inode_is_locked(inode
));
2850 /* Do this quick check before taking global s_orphan_lock. */
2851 if (list_empty(&ei
->i_orphan
))
2855 /* Grab inode buffer early before taking global s_orphan_lock */
2856 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
2859 mutex_lock(&sbi
->s_orphan_lock
);
2860 jbd_debug(4, "remove inode %lu from orphan list\n", inode
->i_ino
);
2862 prev
= ei
->i_orphan
.prev
;
2863 list_del_init(&ei
->i_orphan
);
2865 /* If we're on an error path, we may not have a valid
2866 * transaction handle with which to update the orphan list on
2867 * disk, but we still need to remove the inode from the linked
2868 * list in memory. */
2869 if (!handle
|| err
) {
2870 mutex_unlock(&sbi
->s_orphan_lock
);
2874 ino_next
= NEXT_ORPHAN(inode
);
2875 if (prev
== &sbi
->s_orphan
) {
2876 jbd_debug(4, "superblock will point to %u\n", ino_next
);
2877 BUFFER_TRACE(sbi
->s_sbh
, "get_write_access");
2878 err
= ext4_journal_get_write_access(handle
, sbi
->s_sbh
);
2880 mutex_unlock(&sbi
->s_orphan_lock
);
2883 sbi
->s_es
->s_last_orphan
= cpu_to_le32(ino_next
);
2884 mutex_unlock(&sbi
->s_orphan_lock
);
2885 err
= ext4_handle_dirty_super(handle
, inode
->i_sb
);
2887 struct ext4_iloc iloc2
;
2888 struct inode
*i_prev
=
2889 &list_entry(prev
, struct ext4_inode_info
, i_orphan
)->vfs_inode
;
2891 jbd_debug(4, "orphan inode %lu will point to %u\n",
2892 i_prev
->i_ino
, ino_next
);
2893 err
= ext4_reserve_inode_write(handle
, i_prev
, &iloc2
);
2895 mutex_unlock(&sbi
->s_orphan_lock
);
2898 NEXT_ORPHAN(i_prev
) = ino_next
;
2899 err
= ext4_mark_iloc_dirty(handle
, i_prev
, &iloc2
);
2900 mutex_unlock(&sbi
->s_orphan_lock
);
2904 NEXT_ORPHAN(inode
) = 0;
2905 err
= ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
2907 ext4_std_error(inode
->i_sb
, err
);
2915 static int ext4_rmdir(struct inode
*dir
, struct dentry
*dentry
)
2918 struct inode
*inode
;
2919 struct buffer_head
*bh
;
2920 struct ext4_dir_entry_2
*de
;
2921 handle_t
*handle
= NULL
;
2923 if (unlikely(ext4_forced_shutdown(EXT4_SB(dir
->i_sb
))))
2926 /* Initialize quotas before so that eventual writes go in
2927 * separate transaction */
2928 retval
= dquot_initialize(dir
);
2931 retval
= dquot_initialize(d_inode(dentry
));
2936 bh
= ext4_find_entry(dir
, &dentry
->d_name
, &de
, NULL
);
2942 inode
= d_inode(dentry
);
2944 retval
= -EFSCORRUPTED
;
2945 if (le32_to_cpu(de
->inode
) != inode
->i_ino
)
2948 retval
= -ENOTEMPTY
;
2949 if (!ext4_empty_dir(inode
))
2952 handle
= ext4_journal_start(dir
, EXT4_HT_DIR
,
2953 EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
));
2954 if (IS_ERR(handle
)) {
2955 retval
= PTR_ERR(handle
);
2960 if (IS_DIRSYNC(dir
))
2961 ext4_handle_sync(handle
);
2963 retval
= ext4_delete_entry(handle
, dir
, de
, bh
);
2966 if (!EXT4_DIR_LINK_EMPTY(inode
))
2967 ext4_warning_inode(inode
,
2968 "empty directory '%.*s' has too many links (%u)",
2969 dentry
->d_name
.len
, dentry
->d_name
.name
,
2973 /* There's no need to set i_disksize: the fact that i_nlink is
2974 * zero will ensure that the right thing happens during any
2977 ext4_orphan_add(handle
, inode
);
2978 inode
->i_ctime
= dir
->i_ctime
= dir
->i_mtime
= current_time(inode
);
2979 ext4_mark_inode_dirty(handle
, inode
);
2980 ext4_dec_count(handle
, dir
);
2981 ext4_update_dx_flag(dir
);
2982 ext4_mark_inode_dirty(handle
, dir
);
2987 ext4_journal_stop(handle
);
2991 static int ext4_unlink(struct inode
*dir
, struct dentry
*dentry
)
2994 struct inode
*inode
;
2995 struct buffer_head
*bh
;
2996 struct ext4_dir_entry_2
*de
;
2997 handle_t
*handle
= NULL
;
2999 if (unlikely(ext4_forced_shutdown(EXT4_SB(dir
->i_sb
))))
3002 trace_ext4_unlink_enter(dir
, dentry
);
3003 /* Initialize quotas before so that eventual writes go
3004 * in separate transaction */
3005 retval
= dquot_initialize(dir
);
3008 retval
= dquot_initialize(d_inode(dentry
));
3013 bh
= ext4_find_entry(dir
, &dentry
->d_name
, &de
, NULL
);
3019 inode
= d_inode(dentry
);
3021 retval
= -EFSCORRUPTED
;
3022 if (le32_to_cpu(de
->inode
) != inode
->i_ino
)
3025 handle
= ext4_journal_start(dir
, EXT4_HT_DIR
,
3026 EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
));
3027 if (IS_ERR(handle
)) {
3028 retval
= PTR_ERR(handle
);
3033 if (IS_DIRSYNC(dir
))
3034 ext4_handle_sync(handle
);
3036 if (inode
->i_nlink
== 0) {
3037 ext4_warning_inode(inode
, "Deleting file '%.*s' with no links",
3038 dentry
->d_name
.len
, dentry
->d_name
.name
);
3039 set_nlink(inode
, 1);
3041 retval
= ext4_delete_entry(handle
, dir
, de
, bh
);
3044 dir
->i_ctime
= dir
->i_mtime
= current_time(dir
);
3045 ext4_update_dx_flag(dir
);
3046 ext4_mark_inode_dirty(handle
, dir
);
3048 if (!inode
->i_nlink
)
3049 ext4_orphan_add(handle
, inode
);
3050 inode
->i_ctime
= current_time(inode
);
3051 ext4_mark_inode_dirty(handle
, inode
);
3056 ext4_journal_stop(handle
);
3057 trace_ext4_unlink_exit(dentry
, retval
);
3061 static int ext4_symlink(struct inode
*dir
,
3062 struct dentry
*dentry
, const char *symname
)
3065 struct inode
*inode
;
3066 int err
, len
= strlen(symname
);
3068 struct fscrypt_str disk_link
;
3070 if (unlikely(ext4_forced_shutdown(EXT4_SB(dir
->i_sb
))))
3073 err
= fscrypt_prepare_symlink(dir
, symname
, len
, dir
->i_sb
->s_blocksize
,
3078 err
= dquot_initialize(dir
);
3082 if ((disk_link
.len
> EXT4_N_BLOCKS
* 4)) {
3084 * For non-fast symlinks, we just allocate inode and put it on
3085 * orphan list in the first transaction => we need bitmap,
3086 * group descriptor, sb, inode block, quota blocks, and
3087 * possibly selinux xattr blocks.
3089 credits
= 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir
->i_sb
) +
3090 EXT4_XATTR_TRANS_BLOCKS
;
3093 * Fast symlink. We have to add entry to directory
3094 * (EXT4_DATA_TRANS_BLOCKS + EXT4_INDEX_EXTRA_TRANS_BLOCKS),
3095 * allocate new inode (bitmap, group descriptor, inode block,
3096 * quota blocks, sb is already counted in previous macros).
3098 credits
= EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
3099 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 3;
3102 inode
= ext4_new_inode_start_handle(dir
, S_IFLNK
|S_IRWXUGO
,
3103 &dentry
->d_name
, 0, NULL
,
3104 EXT4_HT_DIR
, credits
);
3105 handle
= ext4_journal_current_handle();
3106 if (IS_ERR(inode
)) {
3108 ext4_journal_stop(handle
);
3109 return PTR_ERR(inode
);
3112 if (IS_ENCRYPTED(inode
)) {
3113 err
= fscrypt_encrypt_symlink(inode
, symname
, len
, &disk_link
);
3115 goto err_drop_inode
;
3116 inode
->i_op
= &ext4_encrypted_symlink_inode_operations
;
3119 if ((disk_link
.len
> EXT4_N_BLOCKS
* 4)) {
3120 if (!IS_ENCRYPTED(inode
))
3121 inode
->i_op
= &ext4_symlink_inode_operations
;
3122 inode_nohighmem(inode
);
3123 ext4_set_aops(inode
);
3125 * We cannot call page_symlink() with transaction started
3126 * because it calls into ext4_write_begin() which can wait
3127 * for transaction commit if we are running out of space
3128 * and thus we deadlock. So we have to stop transaction now
3129 * and restart it when symlink contents is written.
3131 * To keep fs consistent in case of crash, we have to put inode
3132 * to orphan list in the mean time.
3135 err
= ext4_orphan_add(handle
, inode
);
3136 ext4_journal_stop(handle
);
3139 goto err_drop_inode
;
3140 err
= __page_symlink(inode
, disk_link
.name
, disk_link
.len
, 1);
3142 goto err_drop_inode
;
3144 * Now inode is being linked into dir (EXT4_DATA_TRANS_BLOCKS
3145 * + EXT4_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
3147 handle
= ext4_journal_start(dir
, EXT4_HT_DIR
,
3148 EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
3149 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 1);
3150 if (IS_ERR(handle
)) {
3151 err
= PTR_ERR(handle
);
3153 goto err_drop_inode
;
3155 set_nlink(inode
, 1);
3156 err
= ext4_orphan_del(handle
, inode
);
3158 goto err_drop_inode
;
3160 /* clear the extent format for fast symlink */
3161 ext4_clear_inode_flag(inode
, EXT4_INODE_EXTENTS
);
3162 if (!IS_ENCRYPTED(inode
)) {
3163 inode
->i_op
= &ext4_fast_symlink_inode_operations
;
3164 inode
->i_link
= (char *)&EXT4_I(inode
)->i_data
;
3166 memcpy((char *)&EXT4_I(inode
)->i_data
, disk_link
.name
,
3168 inode
->i_size
= disk_link
.len
- 1;
3170 EXT4_I(inode
)->i_disksize
= inode
->i_size
;
3171 err
= ext4_add_nondir(handle
, dentry
, inode
);
3172 if (!err
&& IS_DIRSYNC(dir
))
3173 ext4_handle_sync(handle
);
3176 ext4_journal_stop(handle
);
3177 goto out_free_encrypted_link
;
3181 ext4_journal_stop(handle
);
3183 unlock_new_inode(inode
);
3185 out_free_encrypted_link
:
3186 if (disk_link
.name
!= (unsigned char *)symname
)
3187 kfree(disk_link
.name
);
3191 static int ext4_link(struct dentry
*old_dentry
,
3192 struct inode
*dir
, struct dentry
*dentry
)
3195 struct inode
*inode
= d_inode(old_dentry
);
3196 int err
, retries
= 0;
3198 if (inode
->i_nlink
>= EXT4_LINK_MAX
)
3200 if (ext4_encrypted_inode(dir
) &&
3201 !fscrypt_has_permitted_context(dir
, inode
))
3204 if ((ext4_test_inode_flag(dir
, EXT4_INODE_PROJINHERIT
)) &&
3205 (!projid_eq(EXT4_I(dir
)->i_projid
,
3206 EXT4_I(old_dentry
->d_inode
)->i_projid
)))
3209 err
= dquot_initialize(dir
);
3214 handle
= ext4_journal_start(dir
, EXT4_HT_DIR
,
3215 (EXT4_DATA_TRANS_BLOCKS(dir
->i_sb
) +
3216 EXT4_INDEX_EXTRA_TRANS_BLOCKS
) + 1);
3218 return PTR_ERR(handle
);
3220 if (IS_DIRSYNC(dir
))
3221 ext4_handle_sync(handle
);
3223 inode
->i_ctime
= current_time(inode
);
3224 ext4_inc_count(handle
, inode
);
3227 err
= ext4_add_entry(handle
, dentry
, inode
);
3229 ext4_mark_inode_dirty(handle
, inode
);
3230 /* this can happen only for tmpfile being
3231 * linked the first time
3233 if (inode
->i_nlink
== 1)
3234 ext4_orphan_del(handle
, inode
);
3235 d_instantiate(dentry
, inode
);
3240 ext4_journal_stop(handle
);
3241 if (err
== -ENOSPC
&& ext4_should_retry_alloc(dir
->i_sb
, &retries
))
3248 * Try to find buffer head where contains the parent block.
3249 * It should be the inode block if it is inlined or the 1st block
3250 * if it is a normal dir.
3252 static struct buffer_head
*ext4_get_first_dir_block(handle_t
*handle
,
3253 struct inode
*inode
,
3255 struct ext4_dir_entry_2
**parent_de
,
3258 struct buffer_head
*bh
;
3260 if (!ext4_has_inline_data(inode
)) {
3261 bh
= ext4_read_dirblock(inode
, 0, EITHER
);
3263 *retval
= PTR_ERR(bh
);
3266 *parent_de
= ext4_next_entry(
3267 (struct ext4_dir_entry_2
*)bh
->b_data
,
3268 inode
->i_sb
->s_blocksize
);
3273 return ext4_get_first_inline_block(inode
, parent_de
, retval
);
3276 struct ext4_renament
{
3278 struct dentry
*dentry
;
3279 struct inode
*inode
;
3281 int dir_nlink_delta
;
3283 /* entry for "dentry" */
3284 struct buffer_head
*bh
;
3285 struct ext4_dir_entry_2
*de
;
3288 /* entry for ".." in inode if it's a directory */
3289 struct buffer_head
*dir_bh
;
3290 struct ext4_dir_entry_2
*parent_de
;
3294 static int ext4_rename_dir_prepare(handle_t
*handle
, struct ext4_renament
*ent
)
3298 ent
->dir_bh
= ext4_get_first_dir_block(handle
, ent
->inode
,
3299 &retval
, &ent
->parent_de
,
3303 if (le32_to_cpu(ent
->parent_de
->inode
) != ent
->dir
->i_ino
)
3304 return -EFSCORRUPTED
;
3305 BUFFER_TRACE(ent
->dir_bh
, "get_write_access");
3306 return ext4_journal_get_write_access(handle
, ent
->dir_bh
);
3309 static int ext4_rename_dir_finish(handle_t
*handle
, struct ext4_renament
*ent
,
3314 ent
->parent_de
->inode
= cpu_to_le32(dir_ino
);
3315 BUFFER_TRACE(ent
->dir_bh
, "call ext4_handle_dirty_metadata");
3316 if (!ent
->dir_inlined
) {
3317 if (is_dx(ent
->inode
)) {
3318 retval
= ext4_handle_dirty_dx_node(handle
,
3322 retval
= ext4_handle_dirty_dirent_node(handle
,
3327 retval
= ext4_mark_inode_dirty(handle
, ent
->inode
);
3330 ext4_std_error(ent
->dir
->i_sb
, retval
);
3336 static int ext4_setent(handle_t
*handle
, struct ext4_renament
*ent
,
3337 unsigned ino
, unsigned file_type
)
3341 BUFFER_TRACE(ent
->bh
, "get write access");
3342 retval
= ext4_journal_get_write_access(handle
, ent
->bh
);
3345 ent
->de
->inode
= cpu_to_le32(ino
);
3346 if (ext4_has_feature_filetype(ent
->dir
->i_sb
))
3347 ent
->de
->file_type
= file_type
;
3348 ent
->dir
->i_version
++;
3349 ent
->dir
->i_ctime
= ent
->dir
->i_mtime
=
3350 current_time(ent
->dir
);
3351 ext4_mark_inode_dirty(handle
, ent
->dir
);
3352 BUFFER_TRACE(ent
->bh
, "call ext4_handle_dirty_metadata");
3353 if (!ent
->inlined
) {
3354 retval
= ext4_handle_dirty_dirent_node(handle
,
3356 if (unlikely(retval
)) {
3357 ext4_std_error(ent
->dir
->i_sb
, retval
);
3367 static int ext4_find_delete_entry(handle_t
*handle
, struct inode
*dir
,
3368 const struct qstr
*d_name
)
3370 int retval
= -ENOENT
;
3371 struct buffer_head
*bh
;
3372 struct ext4_dir_entry_2
*de
;
3374 bh
= ext4_find_entry(dir
, d_name
, &de
, NULL
);
3378 retval
= ext4_delete_entry(handle
, dir
, de
, bh
);
3384 static void ext4_rename_delete(handle_t
*handle
, struct ext4_renament
*ent
,
3389 * ent->de could have moved from under us during htree split, so make
3390 * sure that we are deleting the right entry. We might also be pointing
3391 * to a stale entry in the unused part of ent->bh so just checking inum
3392 * and the name isn't enough.
3394 if (le32_to_cpu(ent
->de
->inode
) != ent
->inode
->i_ino
||
3395 ent
->de
->name_len
!= ent
->dentry
->d_name
.len
||
3396 strncmp(ent
->de
->name
, ent
->dentry
->d_name
.name
,
3397 ent
->de
->name_len
) ||
3399 retval
= ext4_find_delete_entry(handle
, ent
->dir
,
3400 &ent
->dentry
->d_name
);
3402 retval
= ext4_delete_entry(handle
, ent
->dir
, ent
->de
, ent
->bh
);
3403 if (retval
== -ENOENT
) {
3404 retval
= ext4_find_delete_entry(handle
, ent
->dir
,
3405 &ent
->dentry
->d_name
);
3410 ext4_warning_inode(ent
->dir
,
3411 "Deleting old file: nlink %d, error=%d",
3412 ent
->dir
->i_nlink
, retval
);
3416 static void ext4_update_dir_count(handle_t
*handle
, struct ext4_renament
*ent
)
3418 if (ent
->dir_nlink_delta
) {
3419 if (ent
->dir_nlink_delta
== -1)
3420 ext4_dec_count(handle
, ent
->dir
);
3422 ext4_inc_count(handle
, ent
->dir
);
3423 ext4_mark_inode_dirty(handle
, ent
->dir
);
3427 static struct inode
*ext4_whiteout_for_rename(struct ext4_renament
*ent
,
3428 int credits
, handle_t
**h
)
3435 * for inode block, sb block, group summaries,
3438 credits
+= (EXT4_MAXQUOTAS_TRANS_BLOCKS(ent
->dir
->i_sb
) +
3439 EXT4_XATTR_TRANS_BLOCKS
+ 4);
3441 wh
= ext4_new_inode_start_handle(ent
->dir
, S_IFCHR
| WHITEOUT_MODE
,
3442 &ent
->dentry
->d_name
, 0, NULL
,
3443 EXT4_HT_DIR
, credits
);
3445 handle
= ext4_journal_current_handle();
3448 ext4_journal_stop(handle
);
3449 if (PTR_ERR(wh
) == -ENOSPC
&&
3450 ext4_should_retry_alloc(ent
->dir
->i_sb
, &retries
))
3454 init_special_inode(wh
, wh
->i_mode
, WHITEOUT_DEV
);
3455 wh
->i_op
= &ext4_special_inode_operations
;
3461 * Anybody can rename anything with this: the permission checks are left to the
3462 * higher-level routines.
3464 * n.b. old_{dentry,inode) refers to the source dentry/inode
3465 * while new_{dentry,inode) refers to the destination dentry/inode
3466 * This comes from rename(const char *oldpath, const char *newpath)
3468 static int ext4_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
3469 struct inode
*new_dir
, struct dentry
*new_dentry
,
3472 handle_t
*handle
= NULL
;
3473 struct ext4_renament old
= {
3475 .dentry
= old_dentry
,
3476 .inode
= d_inode(old_dentry
),
3478 struct ext4_renament
new = {
3480 .dentry
= new_dentry
,
3481 .inode
= d_inode(new_dentry
),
3485 struct inode
*whiteout
= NULL
;
3489 if (new.inode
&& new.inode
->i_nlink
== 0) {
3490 EXT4_ERROR_INODE(new.inode
,
3491 "target of rename is already freed");
3492 return -EFSCORRUPTED
;
3495 if ((ext4_test_inode_flag(new_dir
, EXT4_INODE_PROJINHERIT
)) &&
3496 (!projid_eq(EXT4_I(new_dir
)->i_projid
,
3497 EXT4_I(old_dentry
->d_inode
)->i_projid
)))
3500 if ((ext4_encrypted_inode(old_dir
) &&
3501 !fscrypt_has_encryption_key(old_dir
)) ||
3502 (ext4_encrypted_inode(new_dir
) &&
3503 !fscrypt_has_encryption_key(new_dir
)))
3506 retval
= dquot_initialize(old
.dir
);
3509 retval
= dquot_initialize(new.dir
);
3513 /* Initialize quotas before so that eventual writes go
3514 * in separate transaction */
3516 retval
= dquot_initialize(new.inode
);
3521 old
.bh
= ext4_find_entry(old
.dir
, &old
.dentry
->d_name
, &old
.de
, NULL
);
3523 return PTR_ERR(old
.bh
);
3525 * Check for inode number is _not_ due to possible IO errors.
3526 * We might rmdir the source, keep it as pwd of some process
3527 * and merrily kill the link to whatever was created under the
3528 * same name. Goodbye sticky bit ;-<
3531 if (!old
.bh
|| le32_to_cpu(old
.de
->inode
) != old
.inode
->i_ino
)
3534 if ((old
.dir
!= new.dir
) &&
3535 ext4_encrypted_inode(new.dir
) &&
3536 !fscrypt_has_permitted_context(new.dir
, old
.inode
)) {
3541 new.bh
= ext4_find_entry(new.dir
, &new.dentry
->d_name
,
3542 &new.de
, &new.inlined
);
3543 if (IS_ERR(new.bh
)) {
3544 retval
= PTR_ERR(new.bh
);
3554 if (new.inode
&& !test_opt(new.dir
->i_sb
, NO_AUTO_DA_ALLOC
))
3555 ext4_alloc_da_blocks(old
.inode
);
3557 credits
= (2 * EXT4_DATA_TRANS_BLOCKS(old
.dir
->i_sb
) +
3558 EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 2);
3559 if (!(flags
& RENAME_WHITEOUT
)) {
3560 handle
= ext4_journal_start(old
.dir
, EXT4_HT_DIR
, credits
);
3561 if (IS_ERR(handle
)) {
3562 retval
= PTR_ERR(handle
);
3567 whiteout
= ext4_whiteout_for_rename(&old
, credits
, &handle
);
3568 if (IS_ERR(whiteout
)) {
3569 retval
= PTR_ERR(whiteout
);
3575 if (IS_DIRSYNC(old
.dir
) || IS_DIRSYNC(new.dir
))
3576 ext4_handle_sync(handle
);
3578 if (S_ISDIR(old
.inode
->i_mode
)) {
3580 retval
= -ENOTEMPTY
;
3581 if (!ext4_empty_dir(new.inode
))
3585 if (new.dir
!= old
.dir
&& EXT4_DIR_LINK_MAX(new.dir
))
3588 retval
= ext4_rename_dir_prepare(handle
, &old
);
3593 * If we're renaming a file within an inline_data dir and adding or
3594 * setting the new dirent causes a conversion from inline_data to
3595 * extents/blockmap, we need to force the dirent delete code to
3596 * re-read the directory, or else we end up trying to delete a dirent
3597 * from what is now the extent tree root (or a block map).
3599 force_reread
= (new.dir
->i_ino
== old
.dir
->i_ino
&&
3600 ext4_test_inode_flag(new.dir
, EXT4_INODE_INLINE_DATA
));
3602 old_file_type
= old
.de
->file_type
;
3605 * Do this before adding a new entry, so the old entry is sure
3606 * to be still pointing to the valid old entry.
3608 retval
= ext4_setent(handle
, &old
, whiteout
->i_ino
,
3612 ext4_mark_inode_dirty(handle
, whiteout
);
3615 retval
= ext4_add_entry(handle
, new.dentry
, old
.inode
);
3619 retval
= ext4_setent(handle
, &new,
3620 old
.inode
->i_ino
, old_file_type
);
3625 force_reread
= !ext4_test_inode_flag(new.dir
,
3626 EXT4_INODE_INLINE_DATA
);
3629 * Like most other Unix systems, set the ctime for inodes on a
3632 old
.inode
->i_ctime
= current_time(old
.inode
);
3633 ext4_mark_inode_dirty(handle
, old
.inode
);
3639 ext4_rename_delete(handle
, &old
, force_reread
);
3643 ext4_dec_count(handle
, new.inode
);
3644 new.inode
->i_ctime
= current_time(new.inode
);
3646 old
.dir
->i_ctime
= old
.dir
->i_mtime
= current_time(old
.dir
);
3647 ext4_update_dx_flag(old
.dir
);
3649 retval
= ext4_rename_dir_finish(handle
, &old
, new.dir
->i_ino
);
3653 ext4_dec_count(handle
, old
.dir
);
3655 /* checked ext4_empty_dir above, can't have another
3656 * parent, ext4_dec_count() won't work for many-linked
3658 clear_nlink(new.inode
);
3660 ext4_inc_count(handle
, new.dir
);
3661 ext4_update_dx_flag(new.dir
);
3662 ext4_mark_inode_dirty(handle
, new.dir
);
3665 ext4_mark_inode_dirty(handle
, old
.dir
);
3667 ext4_mark_inode_dirty(handle
, new.inode
);
3668 if (!new.inode
->i_nlink
)
3669 ext4_orphan_add(handle
, new.inode
);
3679 drop_nlink(whiteout
);
3680 unlock_new_inode(whiteout
);
3684 ext4_journal_stop(handle
);
3688 static int ext4_cross_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
3689 struct inode
*new_dir
, struct dentry
*new_dentry
)
3691 handle_t
*handle
= NULL
;
3692 struct ext4_renament old
= {
3694 .dentry
= old_dentry
,
3695 .inode
= d_inode(old_dentry
),
3697 struct ext4_renament
new = {
3699 .dentry
= new_dentry
,
3700 .inode
= d_inode(new_dentry
),
3704 struct timespec ctime
;
3706 if ((ext4_encrypted_inode(old_dir
) &&
3707 !fscrypt_has_encryption_key(old_dir
)) ||
3708 (ext4_encrypted_inode(new_dir
) &&
3709 !fscrypt_has_encryption_key(new_dir
)))
3712 if ((ext4_encrypted_inode(old_dir
) ||
3713 ext4_encrypted_inode(new_dir
)) &&
3714 (old_dir
!= new_dir
) &&
3715 (!fscrypt_has_permitted_context(new_dir
, old
.inode
) ||
3716 !fscrypt_has_permitted_context(old_dir
, new.inode
)))
3719 if ((ext4_test_inode_flag(new_dir
, EXT4_INODE_PROJINHERIT
) &&
3720 !projid_eq(EXT4_I(new_dir
)->i_projid
,
3721 EXT4_I(old_dentry
->d_inode
)->i_projid
)) ||
3722 (ext4_test_inode_flag(old_dir
, EXT4_INODE_PROJINHERIT
) &&
3723 !projid_eq(EXT4_I(old_dir
)->i_projid
,
3724 EXT4_I(new_dentry
->d_inode
)->i_projid
)))
3727 retval
= dquot_initialize(old
.dir
);
3730 retval
= dquot_initialize(new.dir
);
3734 old
.bh
= ext4_find_entry(old
.dir
, &old
.dentry
->d_name
,
3735 &old
.de
, &old
.inlined
);
3737 return PTR_ERR(old
.bh
);
3739 * Check for inode number is _not_ due to possible IO errors.
3740 * We might rmdir the source, keep it as pwd of some process
3741 * and merrily kill the link to whatever was created under the
3742 * same name. Goodbye sticky bit ;-<
3745 if (!old
.bh
|| le32_to_cpu(old
.de
->inode
) != old
.inode
->i_ino
)
3748 new.bh
= ext4_find_entry(new.dir
, &new.dentry
->d_name
,
3749 &new.de
, &new.inlined
);
3750 if (IS_ERR(new.bh
)) {
3751 retval
= PTR_ERR(new.bh
);
3756 /* RENAME_EXCHANGE case: old *and* new must both exist */
3757 if (!new.bh
|| le32_to_cpu(new.de
->inode
) != new.inode
->i_ino
)
3760 handle
= ext4_journal_start(old
.dir
, EXT4_HT_DIR
,
3761 (2 * EXT4_DATA_TRANS_BLOCKS(old
.dir
->i_sb
) +
3762 2 * EXT4_INDEX_EXTRA_TRANS_BLOCKS
+ 2));
3763 if (IS_ERR(handle
)) {
3764 retval
= PTR_ERR(handle
);
3769 if (IS_DIRSYNC(old
.dir
) || IS_DIRSYNC(new.dir
))
3770 ext4_handle_sync(handle
);
3772 if (S_ISDIR(old
.inode
->i_mode
)) {
3774 retval
= ext4_rename_dir_prepare(handle
, &old
);
3778 if (S_ISDIR(new.inode
->i_mode
)) {
3780 retval
= ext4_rename_dir_prepare(handle
, &new);
3786 * Other than the special case of overwriting a directory, parents'
3787 * nlink only needs to be modified if this is a cross directory rename.
3789 if (old
.dir
!= new.dir
&& old
.is_dir
!= new.is_dir
) {
3790 old
.dir_nlink_delta
= old
.is_dir
? -1 : 1;
3791 new.dir_nlink_delta
= -old
.dir_nlink_delta
;
3793 if ((old
.dir_nlink_delta
> 0 && EXT4_DIR_LINK_MAX(old
.dir
)) ||
3794 (new.dir_nlink_delta
> 0 && EXT4_DIR_LINK_MAX(new.dir
)))
3798 new_file_type
= new.de
->file_type
;
3799 retval
= ext4_setent(handle
, &new, old
.inode
->i_ino
, old
.de
->file_type
);
3803 retval
= ext4_setent(handle
, &old
, new.inode
->i_ino
, new_file_type
);
3808 * Like most other Unix systems, set the ctime for inodes on a
3811 ctime
= current_time(old
.inode
);
3812 old
.inode
->i_ctime
= ctime
;
3813 new.inode
->i_ctime
= ctime
;
3814 ext4_mark_inode_dirty(handle
, old
.inode
);
3815 ext4_mark_inode_dirty(handle
, new.inode
);
3818 retval
= ext4_rename_dir_finish(handle
, &old
, new.dir
->i_ino
);
3823 retval
= ext4_rename_dir_finish(handle
, &new, old
.dir
->i_ino
);
3827 ext4_update_dir_count(handle
, &old
);
3828 ext4_update_dir_count(handle
, &new);
3837 ext4_journal_stop(handle
);
3841 static int ext4_rename2(struct inode
*old_dir
, struct dentry
*old_dentry
,
3842 struct inode
*new_dir
, struct dentry
*new_dentry
,
3845 if (unlikely(ext4_forced_shutdown(EXT4_SB(old_dir
->i_sb
))))
3848 if (flags
& ~(RENAME_NOREPLACE
| RENAME_EXCHANGE
| RENAME_WHITEOUT
))
3851 if (flags
& RENAME_EXCHANGE
) {
3852 return ext4_cross_rename(old_dir
, old_dentry
,
3853 new_dir
, new_dentry
);
3856 return ext4_rename(old_dir
, old_dentry
, new_dir
, new_dentry
, flags
);
3860 * directories can handle most operations...
3862 const struct inode_operations ext4_dir_inode_operations
= {
3863 .create
= ext4_create
,
3864 .lookup
= ext4_lookup
,
3866 .unlink
= ext4_unlink
,
3867 .symlink
= ext4_symlink
,
3868 .mkdir
= ext4_mkdir
,
3869 .rmdir
= ext4_rmdir
,
3870 .mknod
= ext4_mknod
,
3871 .tmpfile
= ext4_tmpfile
,
3872 .rename
= ext4_rename2
,
3873 .setattr
= ext4_setattr
,
3874 .getattr
= ext4_getattr
,
3875 .listxattr
= ext4_listxattr
,
3876 .get_acl
= ext4_get_acl
,
3877 .set_acl
= ext4_set_acl
,
3878 .fiemap
= ext4_fiemap
,
3881 const struct inode_operations ext4_special_inode_operations
= {
3882 .setattr
= ext4_setattr
,
3883 .getattr
= ext4_getattr
,
3884 .listxattr
= ext4_listxattr
,
3885 .get_acl
= ext4_get_acl
,
3886 .set_acl
= ext4_set_acl
,