1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/ext4/namei.c
5 * Copyright (C) 1992, 1993, 1994, 1995
6 * Remy Card (card@masi.ibp.fr)
7 * Laboratoire MASI - Institut Blaise Pascal
8 * Universite Pierre et Marie Curie (Paris VI)
12 * linux/fs/minix/namei.c
14 * Copyright (C) 1991, 1992 Linus Torvalds
16 * Big-endian to little-endian byte-swapping/bitmaps by
17 * David S. Miller (davem@caip.rutgers.edu), 1995
18 * Directory entry file type support and forward compatibility hooks
19 * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
20 * Hash Tree Directory indexing (c)
21 * Daniel Phillips, 2001
22 * Hash Tree Directory indexing porting
23 * Christopher Li, 2002
24 * Hash Tree Directory indexing cleanup
29 #include <linux/pagemap.h>
30 #include <linux/time.h>
31 #include <linux/fcntl.h>
32 #include <linux/stat.h>
33 #include <linux/string.h>
34 #include <linux/quotaops.h>
35 #include <linux/buffer_head.h>
36 #include <linux/bio.h>
37 #include <linux/iversion.h>
38 #include <linux/unicode.h>
40 #include "ext4_jbd2.h"
45 #include <trace/events/ext4.h>
47 * define how far ahead to read directories while searching them.
49 #define NAMEI_RA_CHUNKS 2
50 #define NAMEI_RA_BLOCKS 4
51 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
53 static struct buffer_head *ext4_append(handle_t *handle,
57 struct ext4_map_blocks map;
58 struct buffer_head *bh;
61 if (unlikely(EXT4_SB(inode->i_sb)->s_max_dir_size_kb &&
62 ((inode->i_size >> 10) >=
63 EXT4_SB(inode->i_sb)->s_max_dir_size_kb)))
64 return ERR_PTR(-ENOSPC);
66 *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
71 * We're appending new directory block. Make sure the block is not
72 * allocated yet, otherwise we will end up corrupting the
75 err = ext4_map_blocks(NULL, inode, &map, 0);
79 EXT4_ERROR_INODE(inode, "Logical block already allocated");
80 return ERR_PTR(-EFSCORRUPTED);
83 bh = ext4_bread(handle, inode, *block, EXT4_GET_BLOCKS_CREATE);
86 inode->i_size += inode->i_sb->s_blocksize;
87 EXT4_I(inode)->i_disksize = inode->i_size;
88 BUFFER_TRACE(bh, "get_write_access");
89 err = ext4_journal_get_write_access(handle, bh);
92 ext4_std_error(inode->i_sb, err);
98 static int ext4_dx_csum_verify(struct inode *inode,
99 struct ext4_dir_entry *dirent);
102 * Hints to ext4_read_dirblock regarding whether we expect a directory
103 * block being read to be an index block, or a block containing
104 * directory entries (and if the latter, whether it was found via a
105 * logical block in an htree index block). This is used to control
106 * what sort of sanity checkinig ext4_read_dirblock() will do on the
107 * directory block read from the storage device. EITHER will means
108 * the caller doesn't know what kind of directory block will be read,
109 * so no specific verification will be done.
112 EITHER, INDEX, DIRENT, DIRENT_HTREE
115 #define ext4_read_dirblock(inode, block, type) \
116 __ext4_read_dirblock((inode), (block), (type), __func__, __LINE__)
118 static struct buffer_head *__ext4_read_dirblock(struct inode *inode,
120 dirblock_type_t type,
124 struct buffer_head *bh;
125 struct ext4_dir_entry *dirent;
128 if (block >= inode->i_size >> inode->i_blkbits) {
129 ext4_error_inode(inode, func, line, block,
130 "Attempting to read directory block (%u) that is past i_size (%llu)",
131 block, inode->i_size);
132 return ERR_PTR(-EFSCORRUPTED);
135 if (ext4_simulate_fail(inode->i_sb, EXT4_SIM_DIRBLOCK_EIO))
138 bh = ext4_bread(NULL, inode, block, 0);
140 __ext4_warning(inode->i_sb, func, line,
141 "inode #%lu: lblock %lu: comm %s: "
142 "error %ld reading directory block",
143 inode->i_ino, (unsigned long)block,
144 current->comm, PTR_ERR(bh));
148 if (!bh && (type == INDEX || type == DIRENT_HTREE)) {
149 ext4_error_inode(inode, func, line, block,
150 "Directory hole found for htree %s block",
151 (type == INDEX) ? "index" : "leaf");
152 return ERR_PTR(-EFSCORRUPTED);
156 dirent = (struct ext4_dir_entry *) bh->b_data;
157 /* Determine whether or not we have an index block */
161 else if (ext4_rec_len_from_disk(dirent->rec_len,
162 inode->i_sb->s_blocksize) ==
163 inode->i_sb->s_blocksize)
166 if (!is_dx_block && type == INDEX) {
167 ext4_error_inode(inode, func, line, block,
168 "directory leaf block found instead of index block");
170 return ERR_PTR(-EFSCORRUPTED);
172 if (!ext4_has_metadata_csum(inode->i_sb) ||
177 * An empty leaf block can get mistaken for a index block; for
178 * this reason, we can only check the index checksum when the
179 * caller is sure it should be an index block.
181 if (is_dx_block && type == INDEX) {
182 if (ext4_dx_csum_verify(inode, dirent) &&
183 !ext4_simulate_fail(inode->i_sb, EXT4_SIM_DIRBLOCK_CRC))
184 set_buffer_verified(bh);
186 ext4_error_inode_err(inode, func, line, block,
188 "Directory index failed checksum");
190 return ERR_PTR(-EFSBADCRC);
194 if (ext4_dirblock_csum_verify(inode, bh) &&
195 !ext4_simulate_fail(inode->i_sb, EXT4_SIM_DIRBLOCK_CRC))
196 set_buffer_verified(bh);
198 ext4_error_inode_err(inode, func, line, block,
200 "Directory block failed checksum");
202 return ERR_PTR(-EFSBADCRC);
209 #define assert(test) J_ASSERT(test)
213 #define dxtrace(command) command
215 #define dxtrace(command)
239 * dx_root_info is laid out so that if it should somehow get overlaid by a
240 * dirent the two low bits of the hash version will be zero. Therefore, the
241 * hash version mod 4 should never be 0. Sincerely, the paranoia department.
246 struct fake_dirent dot;
248 struct fake_dirent dotdot;
252 __le32 reserved_zero;
254 u8 info_length; /* 8 */
259 struct dx_entry entries[];
264 struct fake_dirent fake;
265 struct dx_entry entries[];
271 struct buffer_head *bh;
272 struct dx_entry *entries;
284 * This goes at the end of each htree block.
288 __le32 dt_checksum; /* crc32c(uuid+inum+dirblock) */
291 static inline ext4_lblk_t dx_get_block(struct dx_entry *entry);
292 static void dx_set_block(struct dx_entry *entry, ext4_lblk_t value);
293 static inline unsigned dx_get_hash(struct dx_entry *entry);
294 static void dx_set_hash(struct dx_entry *entry, unsigned value);
295 static unsigned dx_get_count(struct dx_entry *entries);
296 static unsigned dx_get_limit(struct dx_entry *entries);
297 static void dx_set_count(struct dx_entry *entries, unsigned value);
298 static void dx_set_limit(struct dx_entry *entries, unsigned value);
299 static unsigned dx_root_limit(struct inode *dir, unsigned infosize);
300 static unsigned dx_node_limit(struct inode *dir);
301 static struct dx_frame *dx_probe(struct ext4_filename *fname,
303 struct dx_hash_info *hinfo,
304 struct dx_frame *frame);
305 static void dx_release(struct dx_frame *frames);
306 static int dx_make_map(struct inode *dir, struct buffer_head *bh,
307 struct dx_hash_info *hinfo,
308 struct dx_map_entry *map_tail);
309 static void dx_sort_map(struct dx_map_entry *map, unsigned count);
310 static struct ext4_dir_entry_2 *dx_move_dirents(char *from, char *to,
311 struct dx_map_entry *offsets, int count, unsigned blocksize);
312 static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize);
313 static void dx_insert_block(struct dx_frame *frame,
314 u32 hash, ext4_lblk_t block);
315 static int ext4_htree_next_block(struct inode *dir, __u32 hash,
316 struct dx_frame *frame,
317 struct dx_frame *frames,
319 static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
320 struct ext4_filename *fname,
321 struct ext4_dir_entry_2 **res_dir);
322 static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
323 struct inode *dir, struct inode *inode);
325 /* checksumming functions */
326 void ext4_initialize_dirent_tail(struct buffer_head *bh,
327 unsigned int blocksize)
329 struct ext4_dir_entry_tail *t = EXT4_DIRENT_TAIL(bh->b_data, blocksize);
331 memset(t, 0, sizeof(struct ext4_dir_entry_tail));
332 t->det_rec_len = ext4_rec_len_to_disk(
333 sizeof(struct ext4_dir_entry_tail), blocksize);
334 t->det_reserved_ft = EXT4_FT_DIR_CSUM;
337 /* Walk through a dirent block to find a checksum "dirent" at the tail */
338 static struct ext4_dir_entry_tail *get_dirent_tail(struct inode *inode,
339 struct buffer_head *bh)
341 struct ext4_dir_entry_tail *t;
344 struct ext4_dir_entry *d, *top;
346 d = (struct ext4_dir_entry *)bh->b_data;
347 top = (struct ext4_dir_entry *)(bh->b_data +
348 (EXT4_BLOCK_SIZE(inode->i_sb) -
349 sizeof(struct ext4_dir_entry_tail)));
350 while (d < top && d->rec_len)
351 d = (struct ext4_dir_entry *)(((void *)d) +
352 le16_to_cpu(d->rec_len));
357 t = (struct ext4_dir_entry_tail *)d;
359 t = EXT4_DIRENT_TAIL(bh->b_data, EXT4_BLOCK_SIZE(inode->i_sb));
362 if (t->det_reserved_zero1 ||
363 le16_to_cpu(t->det_rec_len) != sizeof(struct ext4_dir_entry_tail) ||
364 t->det_reserved_zero2 ||
365 t->det_reserved_ft != EXT4_FT_DIR_CSUM)
371 static __le32 ext4_dirblock_csum(struct inode *inode, void *dirent, int size)
373 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
374 struct ext4_inode_info *ei = EXT4_I(inode);
377 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
378 return cpu_to_le32(csum);
381 #define warn_no_space_for_csum(inode) \
382 __warn_no_space_for_csum((inode), __func__, __LINE__)
384 static void __warn_no_space_for_csum(struct inode *inode, const char *func,
387 __ext4_warning_inode(inode, func, line,
388 "No space for directory leaf checksum. Please run e2fsck -D.");
391 int ext4_dirblock_csum_verify(struct inode *inode, struct buffer_head *bh)
393 struct ext4_dir_entry_tail *t;
395 if (!ext4_has_metadata_csum(inode->i_sb))
398 t = get_dirent_tail(inode, bh);
400 warn_no_space_for_csum(inode);
404 if (t->det_checksum != ext4_dirblock_csum(inode, bh->b_data,
405 (char *)t - bh->b_data))
411 static void ext4_dirblock_csum_set(struct inode *inode,
412 struct buffer_head *bh)
414 struct ext4_dir_entry_tail *t;
416 if (!ext4_has_metadata_csum(inode->i_sb))
419 t = get_dirent_tail(inode, bh);
421 warn_no_space_for_csum(inode);
425 t->det_checksum = ext4_dirblock_csum(inode, bh->b_data,
426 (char *)t - bh->b_data);
429 int ext4_handle_dirty_dirblock(handle_t *handle,
431 struct buffer_head *bh)
433 ext4_dirblock_csum_set(inode, bh);
434 return ext4_handle_dirty_metadata(handle, inode, bh);
437 static struct dx_countlimit *get_dx_countlimit(struct inode *inode,
438 struct ext4_dir_entry *dirent,
441 struct ext4_dir_entry *dp;
442 struct dx_root_info *root;
445 if (le16_to_cpu(dirent->rec_len) == EXT4_BLOCK_SIZE(inode->i_sb))
447 else if (le16_to_cpu(dirent->rec_len) == 12) {
448 dp = (struct ext4_dir_entry *)(((void *)dirent) + 12);
449 if (le16_to_cpu(dp->rec_len) !=
450 EXT4_BLOCK_SIZE(inode->i_sb) - 12)
452 root = (struct dx_root_info *)(((void *)dp + 12));
453 if (root->reserved_zero ||
454 root->info_length != sizeof(struct dx_root_info))
461 *offset = count_offset;
462 return (struct dx_countlimit *)(((void *)dirent) + count_offset);
465 static __le32 ext4_dx_csum(struct inode *inode, struct ext4_dir_entry *dirent,
466 int count_offset, int count, struct dx_tail *t)
468 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
469 struct ext4_inode_info *ei = EXT4_I(inode);
472 __u32 dummy_csum = 0;
473 int offset = offsetof(struct dx_tail, dt_checksum);
475 size = count_offset + (count * sizeof(struct dx_entry));
476 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
477 csum = ext4_chksum(sbi, csum, (__u8 *)t, offset);
478 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
480 return cpu_to_le32(csum);
483 static int ext4_dx_csum_verify(struct inode *inode,
484 struct ext4_dir_entry *dirent)
486 struct dx_countlimit *c;
488 int count_offset, limit, count;
490 if (!ext4_has_metadata_csum(inode->i_sb))
493 c = get_dx_countlimit(inode, dirent, &count_offset);
495 EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
498 limit = le16_to_cpu(c->limit);
499 count = le16_to_cpu(c->count);
500 if (count_offset + (limit * sizeof(struct dx_entry)) >
501 EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
502 warn_no_space_for_csum(inode);
505 t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
507 if (t->dt_checksum != ext4_dx_csum(inode, dirent, count_offset,
513 static void ext4_dx_csum_set(struct inode *inode, struct ext4_dir_entry *dirent)
515 struct dx_countlimit *c;
517 int count_offset, limit, count;
519 if (!ext4_has_metadata_csum(inode->i_sb))
522 c = get_dx_countlimit(inode, dirent, &count_offset);
524 EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
527 limit = le16_to_cpu(c->limit);
528 count = le16_to_cpu(c->count);
529 if (count_offset + (limit * sizeof(struct dx_entry)) >
530 EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
531 warn_no_space_for_csum(inode);
534 t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
536 t->dt_checksum = ext4_dx_csum(inode, dirent, count_offset, count, t);
539 static inline int ext4_handle_dirty_dx_node(handle_t *handle,
541 struct buffer_head *bh)
543 ext4_dx_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
544 return ext4_handle_dirty_metadata(handle, inode, bh);
548 * p is at least 6 bytes before the end of page
550 static inline struct ext4_dir_entry_2 *
551 ext4_next_entry(struct ext4_dir_entry_2 *p, unsigned long blocksize)
553 return (struct ext4_dir_entry_2 *)((char *)p +
554 ext4_rec_len_from_disk(p->rec_len, blocksize));
558 * Future: use high four bits of block for coalesce-on-delete flags
559 * Mask them off for now.
562 static inline ext4_lblk_t dx_get_block(struct dx_entry *entry)
564 return le32_to_cpu(entry->block) & 0x0fffffff;
567 static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value)
569 entry->block = cpu_to_le32(value);
572 static inline unsigned dx_get_hash(struct dx_entry *entry)
574 return le32_to_cpu(entry->hash);
577 static inline void dx_set_hash(struct dx_entry *entry, unsigned value)
579 entry->hash = cpu_to_le32(value);
582 static inline unsigned dx_get_count(struct dx_entry *entries)
584 return le16_to_cpu(((struct dx_countlimit *) entries)->count);
587 static inline unsigned dx_get_limit(struct dx_entry *entries)
589 return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
592 static inline void dx_set_count(struct dx_entry *entries, unsigned value)
594 ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
597 static inline void dx_set_limit(struct dx_entry *entries, unsigned value)
599 ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
602 static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize)
604 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(1) -
605 EXT4_DIR_REC_LEN(2) - infosize;
607 if (ext4_has_metadata_csum(dir->i_sb))
608 entry_space -= sizeof(struct dx_tail);
609 return entry_space / sizeof(struct dx_entry);
612 static inline unsigned dx_node_limit(struct inode *dir)
614 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0);
616 if (ext4_has_metadata_csum(dir->i_sb))
617 entry_space -= sizeof(struct dx_tail);
618 return entry_space / sizeof(struct dx_entry);
625 static void dx_show_index(char * label, struct dx_entry *entries)
627 int i, n = dx_get_count (entries);
628 printk(KERN_DEBUG "%s index", label);
629 for (i = 0; i < n; i++) {
630 printk(KERN_CONT " %x->%lu",
631 i ? dx_get_hash(entries + i) : 0,
632 (unsigned long)dx_get_block(entries + i));
634 printk(KERN_CONT "\n");
644 static struct stats dx_show_leaf(struct inode *dir,
645 struct dx_hash_info *hinfo,
646 struct ext4_dir_entry_2 *de,
647 int size, int show_names)
649 unsigned names = 0, space = 0;
650 char *base = (char *) de;
651 struct dx_hash_info h = *hinfo;
654 while ((char *) de < base + size)
660 #ifdef CONFIG_FS_ENCRYPTION
663 struct fscrypt_str fname_crypto_str =
669 if (IS_ENCRYPTED(dir))
670 res = fscrypt_get_encryption_info(dir);
672 printk(KERN_WARNING "Error setting up"
673 " fname crypto: %d\n", res);
675 if (!fscrypt_has_encryption_key(dir)) {
676 /* Directory is not encrypted */
677 ext4fs_dirhash(dir, de->name,
679 printk("%*.s:(U)%x.%u ", len,
681 (unsigned) ((char *) de
684 struct fscrypt_str de_name =
685 FSTR_INIT(name, len);
687 /* Directory is encrypted */
688 res = fscrypt_fname_alloc_buffer(
689 len, &fname_crypto_str);
691 printk(KERN_WARNING "Error "
695 res = fscrypt_fname_disk_to_usr(dir,
699 printk(KERN_WARNING "Error "
700 "converting filename "
706 name = fname_crypto_str.name;
707 len = fname_crypto_str.len;
709 ext4fs_dirhash(dir, de->name,
711 printk("%*.s:(E)%x.%u ", len, name,
712 h.hash, (unsigned) ((char *) de
714 fscrypt_fname_free_buffer(
718 int len = de->name_len;
719 char *name = de->name;
720 ext4fs_dirhash(dir, de->name, de->name_len, &h);
721 printk("%*.s:%x.%u ", len, name, h.hash,
722 (unsigned) ((char *) de - base));
725 space += EXT4_DIR_REC_LEN(de->name_len);
728 de = ext4_next_entry(de, size);
730 printk(KERN_CONT "(%i)\n", names);
731 return (struct stats) { names, space, 1 };
734 struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
735 struct dx_entry *entries, int levels)
737 unsigned blocksize = dir->i_sb->s_blocksize;
738 unsigned count = dx_get_count(entries), names = 0, space = 0, i;
740 struct buffer_head *bh;
741 printk("%i indexed blocks...\n", count);
742 for (i = 0; i < count; i++, entries++)
744 ext4_lblk_t block = dx_get_block(entries);
745 ext4_lblk_t hash = i ? dx_get_hash(entries): 0;
746 u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
748 printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
749 bh = ext4_bread(NULL,dir, block, 0);
750 if (!bh || IS_ERR(bh))
753 dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
754 dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *)
755 bh->b_data, blocksize, 0);
756 names += stats.names;
757 space += stats.space;
758 bcount += stats.bcount;
762 printk(KERN_DEBUG "%snames %u, fullness %u (%u%%)\n",
763 levels ? "" : " ", names, space/bcount,
764 (space/bcount)*100/blocksize);
765 return (struct stats) { names, space, bcount};
767 #endif /* DX_DEBUG */
770 * Probe for a directory leaf block to search.
772 * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
773 * error in the directory index, and the caller should fall back to
774 * searching the directory normally. The callers of dx_probe **MUST**
775 * check for this error code, and make sure it never gets reflected
778 static struct dx_frame *
779 dx_probe(struct ext4_filename *fname, struct inode *dir,
780 struct dx_hash_info *hinfo, struct dx_frame *frame_in)
782 unsigned count, indirect, level, i;
783 struct dx_entry *at, *entries, *p, *q, *m;
784 struct dx_root *root;
785 struct dx_frame *frame = frame_in;
786 struct dx_frame *ret_err = ERR_PTR(ERR_BAD_DX_DIR);
789 ext4_lblk_t blocks[EXT4_HTREE_LEVEL];
791 memset(frame_in, 0, EXT4_HTREE_LEVEL * sizeof(frame_in[0]));
792 frame->bh = ext4_read_dirblock(dir, 0, INDEX);
793 if (IS_ERR(frame->bh))
794 return (struct dx_frame *) frame->bh;
796 root = (struct dx_root *) frame->bh->b_data;
797 if (root->info.hash_version != DX_HASH_TEA &&
798 root->info.hash_version != DX_HASH_HALF_MD4 &&
799 root->info.hash_version != DX_HASH_LEGACY) {
800 ext4_warning_inode(dir, "Unrecognised inode hash code %u",
801 root->info.hash_version);
805 hinfo = &fname->hinfo;
806 hinfo->hash_version = root->info.hash_version;
807 if (hinfo->hash_version <= DX_HASH_TEA)
808 hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
809 hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
810 if (fname && fname_name(fname))
811 ext4fs_dirhash(dir, fname_name(fname), fname_len(fname), hinfo);
814 if (root->info.unused_flags & 1) {
815 ext4_warning_inode(dir, "Unimplemented hash flags: %#06x",
816 root->info.unused_flags);
820 indirect = root->info.indirect_levels;
821 if (indirect >= ext4_dir_htree_level(dir->i_sb)) {
822 ext4_warning(dir->i_sb,
823 "Directory (ino: %lu) htree depth %#06x exceed"
824 "supported value", dir->i_ino,
825 ext4_dir_htree_level(dir->i_sb));
826 if (ext4_dir_htree_level(dir->i_sb) < EXT4_HTREE_LEVEL) {
827 ext4_warning(dir->i_sb, "Enable large directory "
828 "feature to access it");
833 entries = (struct dx_entry *)(((char *)&root->info) +
834 root->info.info_length);
836 if (dx_get_limit(entries) != dx_root_limit(dir,
837 root->info.info_length)) {
838 ext4_warning_inode(dir, "dx entry: limit %u != root limit %u",
839 dx_get_limit(entries),
840 dx_root_limit(dir, root->info.info_length));
844 dxtrace(printk("Look up %x", hash));
848 count = dx_get_count(entries);
849 if (!count || count > dx_get_limit(entries)) {
850 ext4_warning_inode(dir,
851 "dx entry: count %u beyond limit %u",
852 count, dx_get_limit(entries));
857 q = entries + count - 1;
860 dxtrace(printk(KERN_CONT "."));
861 if (dx_get_hash(m) > hash)
867 if (0) { // linear search cross check
868 unsigned n = count - 1;
872 dxtrace(printk(KERN_CONT ","));
873 if (dx_get_hash(++at) > hash)
879 assert (at == p - 1);
883 dxtrace(printk(KERN_CONT " %x->%u\n",
884 at == entries ? 0 : dx_get_hash(at),
886 frame->entries = entries;
889 block = dx_get_block(at);
890 for (i = 0; i <= level; i++) {
891 if (blocks[i] == block) {
892 ext4_warning_inode(dir,
893 "dx entry: tree cycle block %u points back to block %u",
894 blocks[level], block);
898 if (++level > indirect)
900 blocks[level] = block;
902 frame->bh = ext4_read_dirblock(dir, block, INDEX);
903 if (IS_ERR(frame->bh)) {
904 ret_err = (struct dx_frame *) frame->bh;
909 entries = ((struct dx_node *) frame->bh->b_data)->entries;
911 if (dx_get_limit(entries) != dx_node_limit(dir)) {
912 ext4_warning_inode(dir,
913 "dx entry: limit %u != node limit %u",
914 dx_get_limit(entries), dx_node_limit(dir));
919 while (frame >= frame_in) {
924 if (ret_err == ERR_PTR(ERR_BAD_DX_DIR))
925 ext4_warning_inode(dir,
926 "Corrupt directory, running e2fsck is recommended");
930 static void dx_release(struct dx_frame *frames)
932 struct dx_root_info *info;
934 unsigned int indirect_levels;
936 if (frames[0].bh == NULL)
939 info = &((struct dx_root *)frames[0].bh->b_data)->info;
940 /* save local copy, "info" may be freed after brelse() */
941 indirect_levels = info->indirect_levels;
942 for (i = 0; i <= indirect_levels; i++) {
943 if (frames[i].bh == NULL)
945 brelse(frames[i].bh);
951 * This function increments the frame pointer to search the next leaf
952 * block, and reads in the necessary intervening nodes if the search
953 * should be necessary. Whether or not the search is necessary is
954 * controlled by the hash parameter. If the hash value is even, then
955 * the search is only continued if the next block starts with that
956 * hash value. This is used if we are searching for a specific file.
958 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
960 * This function returns 1 if the caller should continue to search,
961 * or 0 if it should not. If there is an error reading one of the
962 * index blocks, it will a negative error code.
964 * If start_hash is non-null, it will be filled in with the starting
965 * hash of the next page.
967 static int ext4_htree_next_block(struct inode *dir, __u32 hash,
968 struct dx_frame *frame,
969 struct dx_frame *frames,
973 struct buffer_head *bh;
979 * Find the next leaf page by incrementing the frame pointer.
980 * If we run out of entries in the interior node, loop around and
981 * increment pointer in the parent node. When we break out of
982 * this loop, num_frames indicates the number of interior
983 * nodes need to be read.
986 if (++(p->at) < p->entries + dx_get_count(p->entries))
995 * If the hash is 1, then continue only if the next page has a
996 * continuation hash of any value. This is used for readdir
997 * handling. Otherwise, check to see if the hash matches the
998 * desired contiuation hash. If it doesn't, return since
999 * there's no point to read in the successive index pages.
1001 bhash = dx_get_hash(p->at);
1003 *start_hash = bhash;
1004 if ((hash & 1) == 0) {
1005 if ((bhash & ~1) != hash)
1009 * If the hash is HASH_NB_ALWAYS, we always go to the next
1010 * block so no check is necessary
1012 while (num_frames--) {
1013 bh = ext4_read_dirblock(dir, dx_get_block(p->at), INDEX);
1019 p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
1026 * This function fills a red-black tree with information from a
1027 * directory block. It returns the number directory entries loaded
1028 * into the tree. If there is an error it is returned in err.
1030 static int htree_dirblock_to_tree(struct file *dir_file,
1031 struct inode *dir, ext4_lblk_t block,
1032 struct dx_hash_info *hinfo,
1033 __u32 start_hash, __u32 start_minor_hash)
1035 struct buffer_head *bh;
1036 struct ext4_dir_entry_2 *de, *top;
1037 int err = 0, count = 0;
1038 struct fscrypt_str fname_crypto_str = FSTR_INIT(NULL, 0), tmp_str;
1040 dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
1041 (unsigned long)block));
1042 bh = ext4_read_dirblock(dir, block, DIRENT_HTREE);
1046 de = (struct ext4_dir_entry_2 *) bh->b_data;
1047 top = (struct ext4_dir_entry_2 *) ((char *) de +
1048 dir->i_sb->s_blocksize -
1049 EXT4_DIR_REC_LEN(0));
1050 /* Check if the directory is encrypted */
1051 if (IS_ENCRYPTED(dir)) {
1052 err = fscrypt_get_encryption_info(dir);
1057 err = fscrypt_fname_alloc_buffer(EXT4_NAME_LEN,
1065 for (; de < top; de = ext4_next_entry(de, dir->i_sb->s_blocksize)) {
1066 if (ext4_check_dir_entry(dir, NULL, de, bh,
1067 bh->b_data, bh->b_size,
1068 (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
1069 + ((char *)de - bh->b_data))) {
1070 /* silently ignore the rest of the block */
1073 ext4fs_dirhash(dir, de->name, de->name_len, hinfo);
1074 if ((hinfo->hash < start_hash) ||
1075 ((hinfo->hash == start_hash) &&
1076 (hinfo->minor_hash < start_minor_hash)))
1080 if (!IS_ENCRYPTED(dir)) {
1081 tmp_str.name = de->name;
1082 tmp_str.len = de->name_len;
1083 err = ext4_htree_store_dirent(dir_file,
1084 hinfo->hash, hinfo->minor_hash, de,
1087 int save_len = fname_crypto_str.len;
1088 struct fscrypt_str de_name = FSTR_INIT(de->name,
1091 /* Directory is encrypted */
1092 err = fscrypt_fname_disk_to_usr(dir, hinfo->hash,
1093 hinfo->minor_hash, &de_name,
1099 err = ext4_htree_store_dirent(dir_file,
1100 hinfo->hash, hinfo->minor_hash, de,
1102 fname_crypto_str.len = save_len;
1112 fscrypt_fname_free_buffer(&fname_crypto_str);
1118 * This function fills a red-black tree with information from a
1119 * directory. We start scanning the directory in hash order, starting
1120 * at start_hash and start_minor_hash.
1122 * This function returns the number of entries inserted into the tree,
1123 * or a negative error code.
1125 int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
1126 __u32 start_minor_hash, __u32 *next_hash)
1128 struct dx_hash_info hinfo;
1129 struct ext4_dir_entry_2 *de;
1130 struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
1136 struct fscrypt_str tmp_str;
1138 dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n",
1139 start_hash, start_minor_hash));
1140 dir = file_inode(dir_file);
1141 if (!(ext4_test_inode_flag(dir, EXT4_INODE_INDEX))) {
1142 hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
1143 if (hinfo.hash_version <= DX_HASH_TEA)
1144 hinfo.hash_version +=
1145 EXT4_SB(dir->i_sb)->s_hash_unsigned;
1146 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
1147 if (ext4_has_inline_data(dir)) {
1148 int has_inline_data = 1;
1149 count = ext4_inlinedir_to_tree(dir_file, dir, 0,
1153 if (has_inline_data) {
1158 count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
1159 start_hash, start_minor_hash);
1163 hinfo.hash = start_hash;
1164 hinfo.minor_hash = 0;
1165 frame = dx_probe(NULL, dir, &hinfo, frames);
1167 return PTR_ERR(frame);
1169 /* Add '.' and '..' from the htree header */
1170 if (!start_hash && !start_minor_hash) {
1171 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
1172 tmp_str.name = de->name;
1173 tmp_str.len = de->name_len;
1174 err = ext4_htree_store_dirent(dir_file, 0, 0,
1180 if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
1181 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
1182 de = ext4_next_entry(de, dir->i_sb->s_blocksize);
1183 tmp_str.name = de->name;
1184 tmp_str.len = de->name_len;
1185 err = ext4_htree_store_dirent(dir_file, 2, 0,
1193 if (fatal_signal_pending(current)) {
1198 block = dx_get_block(frame->at);
1199 ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
1200 start_hash, start_minor_hash);
1207 ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
1208 frame, frames, &hashval);
1209 *next_hash = hashval;
1215 * Stop if: (a) there are no more entries, or
1216 * (b) we have inserted at least one entry and the
1217 * next hash value is not a continuation
1220 (count && ((hashval & 1) == 0)))
1224 dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, "
1225 "next hash: %x\n", count, *next_hash));
1232 static inline int search_dirblock(struct buffer_head *bh,
1234 struct ext4_filename *fname,
1235 unsigned int offset,
1236 struct ext4_dir_entry_2 **res_dir)
1238 return ext4_search_dir(bh, bh->b_data, dir->i_sb->s_blocksize, dir,
1239 fname, offset, res_dir);
1243 * Directory block splitting, compacting
1247 * Create map of hash values, offsets, and sizes, stored at end of block.
1248 * Returns number of entries mapped.
1250 static int dx_make_map(struct inode *dir, struct buffer_head *bh,
1251 struct dx_hash_info *hinfo,
1252 struct dx_map_entry *map_tail)
1255 struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)bh->b_data;
1256 unsigned int buflen = bh->b_size;
1257 char *base = bh->b_data;
1258 struct dx_hash_info h = *hinfo;
1260 if (ext4_has_metadata_csum(dir->i_sb))
1261 buflen -= sizeof(struct ext4_dir_entry_tail);
1263 while ((char *) de < base + buflen) {
1264 if (ext4_check_dir_entry(dir, NULL, de, bh, base, buflen,
1265 ((char *)de) - base))
1266 return -EFSCORRUPTED;
1267 if (de->name_len && de->inode) {
1268 ext4fs_dirhash(dir, de->name, de->name_len, &h);
1270 map_tail->hash = h.hash;
1271 map_tail->offs = ((char *) de - base)>>2;
1272 map_tail->size = le16_to_cpu(de->rec_len);
1276 de = ext4_next_entry(de, dir->i_sb->s_blocksize);
1281 /* Sort map by hash value */
1282 static void dx_sort_map (struct dx_map_entry *map, unsigned count)
1284 struct dx_map_entry *p, *q, *top = map + count - 1;
1286 /* Combsort until bubble sort doesn't suck */
1288 count = count*10/13;
1289 if (count - 9 < 2) /* 9, 10 -> 11 */
1291 for (p = top, q = p - count; q >= map; p--, q--)
1292 if (p->hash < q->hash)
1295 /* Garden variety bubble sort */
1300 if (q[1].hash >= q[0].hash)
1308 static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
1310 struct dx_entry *entries = frame->entries;
1311 struct dx_entry *old = frame->at, *new = old + 1;
1312 int count = dx_get_count(entries);
1314 assert(count < dx_get_limit(entries));
1315 assert(old < entries + count);
1316 memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
1317 dx_set_hash(new, hash);
1318 dx_set_block(new, block);
1319 dx_set_count(entries, count + 1);
1322 #ifdef CONFIG_UNICODE
1324 * Test whether a case-insensitive directory entry matches the filename
1325 * being searched for. If quick is set, assume the name being looked up
1326 * is already in the casefolded form.
1328 * Returns: 0 if the directory entry matches, more than 0 if it
1329 * doesn't match or less than zero on error.
1331 int ext4_ci_compare(const struct inode *parent, const struct qstr *name,
1332 const struct qstr *entry, bool quick)
1334 const struct super_block *sb = parent->i_sb;
1335 const struct unicode_map *um = sb->s_encoding;
1339 ret = utf8_strncasecmp_folded(um, name, entry);
1341 ret = utf8_strncasecmp(um, name, entry);
1344 /* Handle invalid character sequence as either an error
1345 * or as an opaque byte sequence.
1347 if (sb_has_strict_encoding(sb))
1350 if (name->len != entry->len)
1353 return !!memcmp(name->name, entry->name, name->len);
1359 void ext4_fname_setup_ci_filename(struct inode *dir, const struct qstr *iname,
1360 struct fscrypt_str *cf_name)
1364 if (!IS_CASEFOLDED(dir) || !dir->i_sb->s_encoding) {
1365 cf_name->name = NULL;
1369 cf_name->name = kmalloc(EXT4_NAME_LEN, GFP_NOFS);
1373 len = utf8_casefold(dir->i_sb->s_encoding,
1374 iname, cf_name->name,
1377 kfree(cf_name->name);
1378 cf_name->name = NULL;
1381 cf_name->len = (unsigned) len;
1387 * Test whether a directory entry matches the filename being searched for.
1389 * Return: %true if the directory entry matches, otherwise %false.
1391 static inline bool ext4_match(const struct inode *parent,
1392 const struct ext4_filename *fname,
1393 const struct ext4_dir_entry_2 *de)
1395 struct fscrypt_name f;
1396 #ifdef CONFIG_UNICODE
1397 const struct qstr entry = {.name = de->name, .len = de->name_len};
1403 f.usr_fname = fname->usr_fname;
1404 f.disk_name = fname->disk_name;
1405 #ifdef CONFIG_FS_ENCRYPTION
1406 f.crypto_buf = fname->crypto_buf;
1409 #ifdef CONFIG_UNICODE
1410 if (parent->i_sb->s_encoding && IS_CASEFOLDED(parent)) {
1411 if (fname->cf_name.name) {
1412 struct qstr cf = {.name = fname->cf_name.name,
1413 .len = fname->cf_name.len};
1414 return !ext4_ci_compare(parent, &cf, &entry, true);
1416 return !ext4_ci_compare(parent, fname->usr_fname, &entry,
1421 return fscrypt_match_name(&f, de->name, de->name_len);
1425 * Returns 0 if not found, -1 on failure, and 1 on success
1427 int ext4_search_dir(struct buffer_head *bh, char *search_buf, int buf_size,
1428 struct inode *dir, struct ext4_filename *fname,
1429 unsigned int offset, struct ext4_dir_entry_2 **res_dir)
1431 struct ext4_dir_entry_2 * de;
1435 de = (struct ext4_dir_entry_2 *)search_buf;
1436 dlimit = search_buf + buf_size;
1437 while ((char *) de < dlimit - EXT4_BASE_DIR_LEN) {
1438 /* this code is executed quadratically often */
1439 /* do minimal checking `by hand' */
1440 if (de->name + de->name_len <= dlimit &&
1441 ext4_match(dir, fname, de)) {
1442 /* found a match - just to be sure, do
1444 if (ext4_check_dir_entry(dir, NULL, de, bh, search_buf,
1450 /* prevent looping on a bad block */
1451 de_len = ext4_rec_len_from_disk(de->rec_len,
1452 dir->i_sb->s_blocksize);
1456 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
1461 static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block,
1462 struct ext4_dir_entry *de)
1464 struct super_block *sb = dir->i_sb;
1470 if (de->inode == 0 &&
1471 ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) ==
1478 * __ext4_find_entry()
1480 * finds an entry in the specified directory with the wanted name. It
1481 * returns the cache buffer in which the entry was found, and the entry
1482 * itself (as a parameter - res_dir). It does NOT read the inode of the
1483 * entry - you'll have to do that yourself if you want to.
1485 * The returned buffer_head has ->b_count elevated. The caller is expected
1486 * to brelse() it when appropriate.
1488 static struct buffer_head *__ext4_find_entry(struct inode *dir,
1489 struct ext4_filename *fname,
1490 struct ext4_dir_entry_2 **res_dir,
1493 struct super_block *sb;
1494 struct buffer_head *bh_use[NAMEI_RA_SIZE];
1495 struct buffer_head *bh, *ret = NULL;
1496 ext4_lblk_t start, block;
1497 const u8 *name = fname->usr_fname->name;
1498 size_t ra_max = 0; /* Number of bh's in the readahead
1500 size_t ra_ptr = 0; /* Current index into readahead
1502 ext4_lblk_t nblocks;
1503 int i, namelen, retval;
1507 namelen = fname->usr_fname->len;
1508 if (namelen > EXT4_NAME_LEN)
1511 if (ext4_has_inline_data(dir)) {
1512 int has_inline_data = 1;
1513 ret = ext4_find_inline_entry(dir, fname, res_dir,
1515 if (has_inline_data) {
1518 goto cleanup_and_exit;
1522 if ((namelen <= 2) && (name[0] == '.') &&
1523 (name[1] == '.' || name[1] == '\0')) {
1525 * "." or ".." will only be in the first block
1526 * NFS may look up ".."; "." should be handled by the VFS
1533 ret = ext4_dx_find_entry(dir, fname, res_dir);
1535 * On success, or if the error was file not found,
1536 * return. Otherwise, fall back to doing a search the
1537 * old fashioned way.
1539 if (!IS_ERR(ret) || PTR_ERR(ret) != ERR_BAD_DX_DIR)
1540 goto cleanup_and_exit;
1541 dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
1545 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
1548 goto cleanup_and_exit;
1550 start = EXT4_I(dir)->i_dir_start_lookup;
1551 if (start >= nblocks)
1557 * We deal with the read-ahead logic here.
1560 if (ra_ptr >= ra_max) {
1561 /* Refill the readahead buffer */
1564 ra_max = start - block;
1566 ra_max = nblocks - block;
1567 ra_max = min(ra_max, ARRAY_SIZE(bh_use));
1568 retval = ext4_bread_batch(dir, block, ra_max,
1569 false /* wait */, bh_use);
1571 ret = ERR_PTR(retval);
1573 goto cleanup_and_exit;
1576 if ((bh = bh_use[ra_ptr++]) == NULL)
1579 if (!buffer_uptodate(bh)) {
1580 EXT4_ERROR_INODE_ERR(dir, EIO,
1581 "reading directory lblock %lu",
1582 (unsigned long) block);
1584 ret = ERR_PTR(-EIO);
1585 goto cleanup_and_exit;
1587 if (!buffer_verified(bh) &&
1588 !is_dx_internal_node(dir, block,
1589 (struct ext4_dir_entry *)bh->b_data) &&
1590 !ext4_dirblock_csum_verify(dir, bh)) {
1591 EXT4_ERROR_INODE_ERR(dir, EFSBADCRC,
1592 "checksumming directory "
1593 "block %lu", (unsigned long)block);
1595 ret = ERR_PTR(-EFSBADCRC);
1596 goto cleanup_and_exit;
1598 set_buffer_verified(bh);
1599 i = search_dirblock(bh, dir, fname,
1600 block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
1602 EXT4_I(dir)->i_dir_start_lookup = block;
1604 goto cleanup_and_exit;
1608 goto cleanup_and_exit;
1611 if (++block >= nblocks)
1613 } while (block != start);
1616 * If the directory has grown while we were searching, then
1617 * search the last part of the directory before giving up.
1620 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
1621 if (block < nblocks) {
1627 /* Clean up the read-ahead blocks */
1628 for (; ra_ptr < ra_max; ra_ptr++)
1629 brelse(bh_use[ra_ptr]);
1633 static struct buffer_head *ext4_find_entry(struct inode *dir,
1634 const struct qstr *d_name,
1635 struct ext4_dir_entry_2 **res_dir,
1639 struct ext4_filename fname;
1640 struct buffer_head *bh;
1642 err = ext4_fname_setup_filename(dir, d_name, 1, &fname);
1646 return ERR_PTR(err);
1648 bh = __ext4_find_entry(dir, &fname, res_dir, inlined);
1650 ext4_fname_free_filename(&fname);
1654 static struct buffer_head *ext4_lookup_entry(struct inode *dir,
1655 struct dentry *dentry,
1656 struct ext4_dir_entry_2 **res_dir)
1659 struct ext4_filename fname;
1660 struct buffer_head *bh;
1662 err = ext4_fname_prepare_lookup(dir, dentry, &fname);
1666 return ERR_PTR(err);
1668 bh = __ext4_find_entry(dir, &fname, res_dir, NULL);
1670 ext4_fname_free_filename(&fname);
1674 static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
1675 struct ext4_filename *fname,
1676 struct ext4_dir_entry_2 **res_dir)
1678 struct super_block * sb = dir->i_sb;
1679 struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
1680 struct buffer_head *bh;
1684 #ifdef CONFIG_FS_ENCRYPTION
1687 frame = dx_probe(fname, dir, NULL, frames);
1689 return (struct buffer_head *) frame;
1691 block = dx_get_block(frame->at);
1692 bh = ext4_read_dirblock(dir, block, DIRENT_HTREE);
1696 retval = search_dirblock(bh, dir, fname,
1697 block << EXT4_BLOCK_SIZE_BITS(sb),
1703 bh = ERR_PTR(ERR_BAD_DX_DIR);
1707 /* Check to see if we should continue to search */
1708 retval = ext4_htree_next_block(dir, fname->hinfo.hash, frame,
1711 ext4_warning_inode(dir,
1712 "error %d reading directory index block",
1714 bh = ERR_PTR(retval);
1717 } while (retval == 1);
1721 dxtrace(printk(KERN_DEBUG "%s not found\n", fname->usr_fname->name));
1727 static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
1729 struct inode *inode;
1730 struct ext4_dir_entry_2 *de;
1731 struct buffer_head *bh;
1733 if (dentry->d_name.len > EXT4_NAME_LEN)
1734 return ERR_PTR(-ENAMETOOLONG);
1736 bh = ext4_lookup_entry(dir, dentry, &de);
1738 return ERR_CAST(bh);
1741 __u32 ino = le32_to_cpu(de->inode);
1743 if (!ext4_valid_inum(dir->i_sb, ino)) {
1744 EXT4_ERROR_INODE(dir, "bad inode number: %u", ino);
1745 return ERR_PTR(-EFSCORRUPTED);
1747 if (unlikely(ino == dir->i_ino)) {
1748 EXT4_ERROR_INODE(dir, "'%pd' linked to parent dir",
1750 return ERR_PTR(-EFSCORRUPTED);
1752 inode = ext4_iget(dir->i_sb, ino, EXT4_IGET_NORMAL);
1753 if (inode == ERR_PTR(-ESTALE)) {
1754 EXT4_ERROR_INODE(dir,
1755 "deleted inode referenced: %u",
1757 return ERR_PTR(-EFSCORRUPTED);
1759 if (!IS_ERR(inode) && IS_ENCRYPTED(dir) &&
1760 (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
1761 !fscrypt_has_permitted_context(dir, inode)) {
1762 ext4_warning(inode->i_sb,
1763 "Inconsistent encryption contexts: %lu/%lu",
1764 dir->i_ino, inode->i_ino);
1766 return ERR_PTR(-EPERM);
1770 #ifdef CONFIG_UNICODE
1771 if (!inode && IS_CASEFOLDED(dir)) {
1772 /* Eventually we want to call d_add_ci(dentry, NULL)
1773 * for negative dentries in the encoding case as
1774 * well. For now, prevent the negative dentry
1775 * from being cached.
1780 return d_splice_alias(inode, dentry);
1784 struct dentry *ext4_get_parent(struct dentry *child)
1787 static const struct qstr dotdot = QSTR_INIT("..", 2);
1788 struct ext4_dir_entry_2 * de;
1789 struct buffer_head *bh;
1791 bh = ext4_find_entry(d_inode(child), &dotdot, &de, NULL);
1793 return ERR_CAST(bh);
1795 return ERR_PTR(-ENOENT);
1796 ino = le32_to_cpu(de->inode);
1799 if (!ext4_valid_inum(child->d_sb, ino)) {
1800 EXT4_ERROR_INODE(d_inode(child),
1801 "bad parent inode number: %u", ino);
1802 return ERR_PTR(-EFSCORRUPTED);
1805 return d_obtain_alias(ext4_iget(child->d_sb, ino, EXT4_IGET_NORMAL));
1809 * Move count entries from end of map between two memory locations.
1810 * Returns pointer to last entry moved.
1812 static struct ext4_dir_entry_2 *
1813 dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count,
1816 unsigned rec_len = 0;
1819 struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)
1820 (from + (map->offs<<2));
1821 rec_len = EXT4_DIR_REC_LEN(de->name_len);
1822 memcpy (to, de, rec_len);
1823 ((struct ext4_dir_entry_2 *) to)->rec_len =
1824 ext4_rec_len_to_disk(rec_len, blocksize);
1829 return (struct ext4_dir_entry_2 *) (to - rec_len);
1833 * Compact each dir entry in the range to the minimal rec_len.
1834 * Returns pointer to last entry in range.
1836 static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize)
1838 struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
1839 unsigned rec_len = 0;
1842 while ((char*)de < base + blocksize) {
1843 next = ext4_next_entry(de, blocksize);
1844 if (de->inode && de->name_len) {
1845 rec_len = EXT4_DIR_REC_LEN(de->name_len);
1847 memmove(to, de, rec_len);
1848 to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
1850 to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
1858 * Split a full leaf block to make room for a new dir entry.
1859 * Allocate a new block, and move entries so that they are approx. equally full.
1860 * Returns pointer to de in block into which the new entry will be inserted.
1862 static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
1863 struct buffer_head **bh,struct dx_frame *frame,
1864 struct dx_hash_info *hinfo)
1866 unsigned blocksize = dir->i_sb->s_blocksize;
1869 struct buffer_head *bh2;
1870 ext4_lblk_t newblock;
1872 struct dx_map_entry *map;
1873 char *data1 = (*bh)->b_data, *data2;
1874 unsigned split, move, size;
1875 struct ext4_dir_entry_2 *de = NULL, *de2;
1879 if (ext4_has_metadata_csum(dir->i_sb))
1880 csum_size = sizeof(struct ext4_dir_entry_tail);
1882 bh2 = ext4_append(handle, dir, &newblock);
1886 return (struct ext4_dir_entry_2 *) bh2;
1889 BUFFER_TRACE(*bh, "get_write_access");
1890 err = ext4_journal_get_write_access(handle, *bh);
1894 BUFFER_TRACE(frame->bh, "get_write_access");
1895 err = ext4_journal_get_write_access(handle, frame->bh);
1899 data2 = bh2->b_data;
1901 /* create map in the end of data2 block */
1902 map = (struct dx_map_entry *) (data2 + blocksize);
1903 count = dx_make_map(dir, *bh, hinfo, map);
1909 dx_sort_map(map, count);
1910 /* Ensure that neither split block is over half full */
1913 for (i = count-1; i >= 0; i--) {
1914 /* is more than half of this entry in 2nd half of the block? */
1915 if (size + map[i].size/2 > blocksize/2)
1917 size += map[i].size;
1921 * map index at which we will split
1923 * If the sum of active entries didn't exceed half the block size, just
1924 * split it in half by count; each resulting block will have at least
1925 * half the space free.
1928 split = count - move;
1932 hash2 = map[split].hash;
1933 continued = hash2 == map[split - 1].hash;
1934 dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
1935 (unsigned long)dx_get_block(frame->at),
1936 hash2, split, count-split));
1938 /* Fancy dance to stay within two buffers */
1939 de2 = dx_move_dirents(data1, data2, map + split, count - split,
1941 de = dx_pack_dirents(data1, blocksize);
1942 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
1945 de2->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
1949 ext4_initialize_dirent_tail(*bh, blocksize);
1950 ext4_initialize_dirent_tail(bh2, blocksize);
1953 dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data1,
1955 dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data2,
1958 /* Which block gets the new entry? */
1959 if (hinfo->hash >= hash2) {
1963 dx_insert_block(frame, hash2 + continued, newblock);
1964 err = ext4_handle_dirty_dirblock(handle, dir, bh2);
1967 err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
1971 dxtrace(dx_show_index("frame", frame->entries));
1978 ext4_std_error(dir->i_sb, err);
1979 return ERR_PTR(err);
1982 int ext4_find_dest_de(struct inode *dir, struct inode *inode,
1983 struct buffer_head *bh,
1984 void *buf, int buf_size,
1985 struct ext4_filename *fname,
1986 struct ext4_dir_entry_2 **dest_de)
1988 struct ext4_dir_entry_2 *de;
1989 unsigned short reclen = EXT4_DIR_REC_LEN(fname_len(fname));
1991 unsigned int offset = 0;
1994 de = (struct ext4_dir_entry_2 *)buf;
1995 top = buf + buf_size - reclen;
1996 while ((char *) de <= top) {
1997 if (ext4_check_dir_entry(dir, NULL, de, bh,
1998 buf, buf_size, offset))
1999 return -EFSCORRUPTED;
2000 if (ext4_match(dir, fname, de))
2002 nlen = EXT4_DIR_REC_LEN(de->name_len);
2003 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
2004 if ((de->inode ? rlen - nlen : rlen) >= reclen)
2006 de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
2009 if ((char *) de > top)
2016 void ext4_insert_dentry(struct inode *inode,
2017 struct ext4_dir_entry_2 *de,
2019 struct ext4_filename *fname)
2024 nlen = EXT4_DIR_REC_LEN(de->name_len);
2025 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
2027 struct ext4_dir_entry_2 *de1 =
2028 (struct ext4_dir_entry_2 *)((char *)de + nlen);
2029 de1->rec_len = ext4_rec_len_to_disk(rlen - nlen, buf_size);
2030 de->rec_len = ext4_rec_len_to_disk(nlen, buf_size);
2033 de->file_type = EXT4_FT_UNKNOWN;
2034 de->inode = cpu_to_le32(inode->i_ino);
2035 ext4_set_de_type(inode->i_sb, de, inode->i_mode);
2036 de->name_len = fname_len(fname);
2037 memcpy(de->name, fname_name(fname), fname_len(fname));
2041 * Add a new entry into a directory (leaf) block. If de is non-NULL,
2042 * it points to a directory entry which is guaranteed to be large
2043 * enough for new directory entry. If de is NULL, then
2044 * add_dirent_to_buf will attempt search the directory block for
2045 * space. It will return -ENOSPC if no space is available, and -EIO
2046 * and -EEXIST if directory entry already exists.
2048 static int add_dirent_to_buf(handle_t *handle, struct ext4_filename *fname,
2050 struct inode *inode, struct ext4_dir_entry_2 *de,
2051 struct buffer_head *bh)
2053 unsigned int blocksize = dir->i_sb->s_blocksize;
2057 if (ext4_has_metadata_csum(inode->i_sb))
2058 csum_size = sizeof(struct ext4_dir_entry_tail);
2061 err = ext4_find_dest_de(dir, inode, bh, bh->b_data,
2062 blocksize - csum_size, fname, &de);
2066 BUFFER_TRACE(bh, "get_write_access");
2067 err = ext4_journal_get_write_access(handle, bh);
2069 ext4_std_error(dir->i_sb, err);
2073 /* By now the buffer is marked for journaling */
2074 ext4_insert_dentry(inode, de, blocksize, fname);
2077 * XXX shouldn't update any times until successful
2078 * completion of syscall, but too many callers depend
2081 * XXX similarly, too many callers depend on
2082 * ext4_new_inode() setting the times, but error
2083 * recovery deletes the inode, so the worst that can
2084 * happen is that the times are slightly out of date
2085 * and/or different from the directory change time.
2087 dir->i_mtime = dir->i_ctime = current_time(dir);
2088 ext4_update_dx_flag(dir);
2089 inode_inc_iversion(dir);
2090 err2 = ext4_mark_inode_dirty(handle, dir);
2091 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
2092 err = ext4_handle_dirty_dirblock(handle, dir, bh);
2094 ext4_std_error(dir->i_sb, err);
2095 return err ? err : err2;
2099 * This converts a one block unindexed directory to a 3 block indexed
2100 * directory, and adds the dentry to the indexed directory.
2102 static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname,
2104 struct inode *inode, struct buffer_head *bh)
2106 struct buffer_head *bh2;
2107 struct dx_root *root;
2108 struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
2109 struct dx_entry *entries;
2110 struct ext4_dir_entry_2 *de, *de2;
2116 struct fake_dirent *fde;
2119 if (ext4_has_metadata_csum(inode->i_sb))
2120 csum_size = sizeof(struct ext4_dir_entry_tail);
2122 blocksize = dir->i_sb->s_blocksize;
2123 dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino));
2124 BUFFER_TRACE(bh, "get_write_access");
2125 retval = ext4_journal_get_write_access(handle, bh);
2127 ext4_std_error(dir->i_sb, retval);
2131 root = (struct dx_root *) bh->b_data;
2133 /* The 0th block becomes the root, move the dirents out */
2134 fde = &root->dotdot;
2135 de = (struct ext4_dir_entry_2 *)((char *)fde +
2136 ext4_rec_len_from_disk(fde->rec_len, blocksize));
2137 if ((char *) de >= (((char *) root) + blocksize)) {
2138 EXT4_ERROR_INODE(dir, "invalid rec_len for '..'");
2140 return -EFSCORRUPTED;
2142 len = ((char *) root) + (blocksize - csum_size) - (char *) de;
2144 /* Allocate new block for the 0th block's dirents */
2145 bh2 = ext4_append(handle, dir, &block);
2148 return PTR_ERR(bh2);
2150 ext4_set_inode_flag(dir, EXT4_INODE_INDEX);
2151 data2 = bh2->b_data;
2153 memcpy(data2, de, len);
2154 de = (struct ext4_dir_entry_2 *) data2;
2156 while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top)
2158 de->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
2159 (char *) de, blocksize);
2162 ext4_initialize_dirent_tail(bh2, blocksize);
2164 /* Initialize the root; the dot dirents already exist */
2165 de = (struct ext4_dir_entry_2 *) (&root->dotdot);
2166 de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2),
2168 memset (&root->info, 0, sizeof(root->info));
2169 root->info.info_length = sizeof(root->info);
2170 root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
2171 entries = root->entries;
2172 dx_set_block(entries, 1);
2173 dx_set_count(entries, 1);
2174 dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
2176 /* Initialize as for dx_probe */
2177 fname->hinfo.hash_version = root->info.hash_version;
2178 if (fname->hinfo.hash_version <= DX_HASH_TEA)
2179 fname->hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
2180 fname->hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
2181 ext4fs_dirhash(dir, fname_name(fname), fname_len(fname), &fname->hinfo);
2183 memset(frames, 0, sizeof(frames));
2185 frame->entries = entries;
2186 frame->at = entries;
2189 retval = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
2192 retval = ext4_handle_dirty_dirblock(handle, dir, bh2);
2196 de = do_split(handle,dir, &bh2, frame, &fname->hinfo);
2198 retval = PTR_ERR(de);
2202 retval = add_dirent_to_buf(handle, fname, dir, inode, de, bh2);
2205 * Even if the block split failed, we have to properly write
2206 * out all the changes we did so far. Otherwise we can end up
2207 * with corrupted filesystem.
2210 ext4_mark_inode_dirty(handle, dir);
2219 * adds a file entry to the specified directory, using the same
2220 * semantics as ext4_find_entry(). It returns NULL if it failed.
2222 * NOTE!! The inode part of 'de' is left at 0 - which means you
2223 * may not sleep between calling this and putting something into
2224 * the entry, as someone else might have used it while you slept.
2226 static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
2227 struct inode *inode)
2229 struct inode *dir = d_inode(dentry->d_parent);
2230 struct buffer_head *bh = NULL;
2231 struct ext4_dir_entry_2 *de;
2232 struct super_block *sb;
2233 struct ext4_filename fname;
2237 ext4_lblk_t block, blocks;
2240 if (ext4_has_metadata_csum(inode->i_sb))
2241 csum_size = sizeof(struct ext4_dir_entry_tail);
2244 blocksize = sb->s_blocksize;
2245 if (!dentry->d_name.len)
2248 if (fscrypt_is_nokey_name(dentry))
2251 #ifdef CONFIG_UNICODE
2252 if (sb_has_strict_encoding(sb) && IS_CASEFOLDED(dir) &&
2253 sb->s_encoding && utf8_validate(sb->s_encoding, &dentry->d_name))
2257 retval = ext4_fname_setup_filename(dir, &dentry->d_name, 0, &fname);
2261 if (ext4_has_inline_data(dir)) {
2262 retval = ext4_try_add_inline_entry(handle, &fname, dir, inode);
2272 retval = ext4_dx_add_entry(handle, &fname, dir, inode);
2273 if (!retval || (retval != ERR_BAD_DX_DIR))
2275 /* Can we just ignore htree data? */
2276 if (ext4_has_metadata_csum(sb)) {
2277 EXT4_ERROR_INODE(dir,
2278 "Directory has corrupted htree index.");
2279 retval = -EFSCORRUPTED;
2282 ext4_clear_inode_flag(dir, EXT4_INODE_INDEX);
2284 retval = ext4_mark_inode_dirty(handle, dir);
2285 if (unlikely(retval))
2288 blocks = dir->i_size >> sb->s_blocksize_bits;
2289 for (block = 0; block < blocks; block++) {
2290 bh = ext4_read_dirblock(dir, block, DIRENT);
2292 bh = ext4_bread(handle, dir, block,
2293 EXT4_GET_BLOCKS_CREATE);
2294 goto add_to_new_block;
2297 retval = PTR_ERR(bh);
2301 retval = add_dirent_to_buf(handle, &fname, dir, inode,
2303 if (retval != -ENOSPC)
2306 if (blocks == 1 && !dx_fallback &&
2307 ext4_has_feature_dir_index(sb)) {
2308 retval = make_indexed_dir(handle, &fname, dir,
2310 bh = NULL; /* make_indexed_dir releases bh */
2315 bh = ext4_append(handle, dir, &block);
2318 retval = PTR_ERR(bh);
2322 de = (struct ext4_dir_entry_2 *) bh->b_data;
2324 de->rec_len = ext4_rec_len_to_disk(blocksize - csum_size, blocksize);
2327 ext4_initialize_dirent_tail(bh, blocksize);
2329 retval = add_dirent_to_buf(handle, &fname, dir, inode, de, bh);
2331 ext4_fname_free_filename(&fname);
2334 ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
2339 * Returns 0 for success, or a negative error value
2341 static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
2342 struct inode *dir, struct inode *inode)
2344 struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
2345 struct dx_entry *entries, *at;
2346 struct buffer_head *bh;
2347 struct super_block *sb = dir->i_sb;
2348 struct ext4_dir_entry_2 *de;
2354 frame = dx_probe(fname, dir, NULL, frames);
2356 return PTR_ERR(frame);
2357 entries = frame->entries;
2359 bh = ext4_read_dirblock(dir, dx_get_block(frame->at), DIRENT_HTREE);
2366 BUFFER_TRACE(bh, "get_write_access");
2367 err = ext4_journal_get_write_access(handle, bh);
2371 err = add_dirent_to_buf(handle, fname, dir, inode, NULL, bh);
2376 /* Block full, should compress but for now just split */
2377 dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
2378 dx_get_count(entries), dx_get_limit(entries)));
2379 /* Need to split index? */
2380 if (dx_get_count(entries) == dx_get_limit(entries)) {
2381 ext4_lblk_t newblock;
2382 int levels = frame - frames + 1;
2383 unsigned int icount;
2385 struct dx_entry *entries2;
2386 struct dx_node *node2;
2387 struct buffer_head *bh2;
2389 while (frame > frames) {
2390 if (dx_get_count((frame - 1)->entries) <
2391 dx_get_limit((frame - 1)->entries)) {
2395 frame--; /* split higher index block */
2397 entries = frame->entries;
2400 if (add_level && levels == ext4_dir_htree_level(sb)) {
2401 ext4_warning(sb, "Directory (ino: %lu) index full, "
2402 "reach max htree level :%d",
2403 dir->i_ino, levels);
2404 if (ext4_dir_htree_level(sb) < EXT4_HTREE_LEVEL) {
2405 ext4_warning(sb, "Large directory feature is "
2406 "not enabled on this "
2412 icount = dx_get_count(entries);
2413 bh2 = ext4_append(handle, dir, &newblock);
2418 node2 = (struct dx_node *)(bh2->b_data);
2419 entries2 = node2->entries;
2420 memset(&node2->fake, 0, sizeof(struct fake_dirent));
2421 node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize,
2423 BUFFER_TRACE(frame->bh, "get_write_access");
2424 err = ext4_journal_get_write_access(handle, frame->bh);
2428 unsigned icount1 = icount/2, icount2 = icount - icount1;
2429 unsigned hash2 = dx_get_hash(entries + icount1);
2430 dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
2433 BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
2434 err = ext4_journal_get_write_access(handle,
2439 memcpy((char *) entries2, (char *) (entries + icount1),
2440 icount2 * sizeof(struct dx_entry));
2441 dx_set_count(entries, icount1);
2442 dx_set_count(entries2, icount2);
2443 dx_set_limit(entries2, dx_node_limit(dir));
2445 /* Which index block gets the new entry? */
2446 if (at - entries >= icount1) {
2447 frame->at = at = at - entries - icount1 + entries2;
2448 frame->entries = entries = entries2;
2449 swap(frame->bh, bh2);
2451 dx_insert_block((frame - 1), hash2, newblock);
2452 dxtrace(dx_show_index("node", frame->entries));
2453 dxtrace(dx_show_index("node",
2454 ((struct dx_node *) bh2->b_data)->entries));
2455 err = ext4_handle_dirty_dx_node(handle, dir, bh2);
2459 err = ext4_handle_dirty_dx_node(handle, dir,
2463 err = ext4_handle_dirty_dx_node(handle, dir,
2468 struct dx_root *dxroot;
2469 memcpy((char *) entries2, (char *) entries,
2470 icount * sizeof(struct dx_entry));
2471 dx_set_limit(entries2, dx_node_limit(dir));
2474 dx_set_count(entries, 1);
2475 dx_set_block(entries + 0, newblock);
2476 dxroot = (struct dx_root *)frames[0].bh->b_data;
2477 dxroot->info.indirect_levels += 1;
2478 dxtrace(printk(KERN_DEBUG
2479 "Creating %d level index...\n",
2480 dxroot->info.indirect_levels));
2481 err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
2484 err = ext4_handle_dirty_dx_node(handle, dir, bh2);
2490 de = do_split(handle, dir, &bh, frame, &fname->hinfo);
2495 err = add_dirent_to_buf(handle, fname, dir, inode, de, bh);
2499 ext4_std_error(dir->i_sb, err); /* this is a no-op if err == 0 */
2503 /* @restart is true means htree-path has been changed, we need to
2504 * repeat dx_probe() to find out valid htree-path
2506 if (restart && err == 0)
2512 * ext4_generic_delete_entry deletes a directory entry by merging it
2513 * with the previous entry
2515 int ext4_generic_delete_entry(struct inode *dir,
2516 struct ext4_dir_entry_2 *de_del,
2517 struct buffer_head *bh,
2522 struct ext4_dir_entry_2 *de, *pde;
2523 unsigned int blocksize = dir->i_sb->s_blocksize;
2528 de = (struct ext4_dir_entry_2 *)entry_buf;
2529 while (i < buf_size - csum_size) {
2530 if (ext4_check_dir_entry(dir, NULL, de, bh,
2531 entry_buf, buf_size, i))
2532 return -EFSCORRUPTED;
2535 pde->rec_len = ext4_rec_len_to_disk(
2536 ext4_rec_len_from_disk(pde->rec_len,
2538 ext4_rec_len_from_disk(de->rec_len,
2543 inode_inc_iversion(dir);
2546 i += ext4_rec_len_from_disk(de->rec_len, blocksize);
2548 de = ext4_next_entry(de, blocksize);
2553 static int ext4_delete_entry(handle_t *handle,
2555 struct ext4_dir_entry_2 *de_del,
2556 struct buffer_head *bh)
2558 int err, csum_size = 0;
2560 if (ext4_has_inline_data(dir)) {
2561 int has_inline_data = 1;
2562 err = ext4_delete_inline_entry(handle, dir, de_del, bh,
2564 if (has_inline_data)
2568 if (ext4_has_metadata_csum(dir->i_sb))
2569 csum_size = sizeof(struct ext4_dir_entry_tail);
2571 BUFFER_TRACE(bh, "get_write_access");
2572 err = ext4_journal_get_write_access(handle, bh);
2576 err = ext4_generic_delete_entry(dir, de_del, bh, bh->b_data,
2577 dir->i_sb->s_blocksize, csum_size);
2581 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
2582 err = ext4_handle_dirty_dirblock(handle, dir, bh);
2589 ext4_std_error(dir->i_sb, err);
2594 * Set directory link count to 1 if nlinks > EXT4_LINK_MAX, or if nlinks == 2
2595 * since this indicates that nlinks count was previously 1 to avoid overflowing
2596 * the 16-bit i_links_count field on disk. Directories with i_nlink == 1 mean
2597 * that subdirectory link counts are not being maintained accurately.
2599 * The caller has already checked for i_nlink overflow in case the DIR_LINK
2600 * feature is not enabled and returned -EMLINK. The is_dx() check is a proxy
2601 * for checking S_ISDIR(inode) (since the INODE_INDEX feature will not be set
2602 * on regular files) and to avoid creating huge/slow non-HTREE directories.
2604 static void ext4_inc_count(struct inode *inode)
2608 (inode->i_nlink > EXT4_LINK_MAX || inode->i_nlink == 2))
2609 set_nlink(inode, 1);
2613 * If a directory had nlink == 1, then we should let it be 1. This indicates
2614 * directory has >EXT4_LINK_MAX subdirs.
2616 static void ext4_dec_count(struct inode *inode)
2618 if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
2624 * Add non-directory inode to a directory. On success, the inode reference is
2625 * consumed by dentry is instantiation. This is also indicated by clearing of
2626 * *inodep pointer. On failure, the caller is responsible for dropping the
2627 * inode reference in the safe context.
2629 static int ext4_add_nondir(handle_t *handle,
2630 struct dentry *dentry, struct inode **inodep)
2632 struct inode *dir = d_inode(dentry->d_parent);
2633 struct inode *inode = *inodep;
2634 int err = ext4_add_entry(handle, dentry, inode);
2636 err = ext4_mark_inode_dirty(handle, inode);
2637 if (IS_DIRSYNC(dir))
2638 ext4_handle_sync(handle);
2639 d_instantiate_new(dentry, inode);
2644 ext4_orphan_add(handle, inode);
2645 unlock_new_inode(inode);
2650 * By the time this is called, we already have created
2651 * the directory cache entry for the new file, but it
2652 * is so far negative - it has no inode.
2654 * If the create succeeds, we fill in the inode information
2655 * with d_instantiate().
2657 static int ext4_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2661 struct inode *inode;
2662 int err, credits, retries = 0;
2664 err = dquot_initialize(dir);
2668 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2669 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
2671 inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0,
2672 NULL, EXT4_HT_DIR, credits);
2673 handle = ext4_journal_current_handle();
2674 err = PTR_ERR(inode);
2675 if (!IS_ERR(inode)) {
2676 inode->i_op = &ext4_file_inode_operations;
2677 inode->i_fop = &ext4_file_operations;
2678 ext4_set_aops(inode);
2679 err = ext4_add_nondir(handle, dentry, &inode);
2681 ext4_fc_track_create(handle, dentry);
2684 ext4_journal_stop(handle);
2685 if (!IS_ERR_OR_NULL(inode))
2687 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2692 static int ext4_mknod(struct inode *dir, struct dentry *dentry,
2693 umode_t mode, dev_t rdev)
2696 struct inode *inode;
2697 int err, credits, retries = 0;
2699 err = dquot_initialize(dir);
2703 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2704 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
2706 inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0,
2707 NULL, EXT4_HT_DIR, credits);
2708 handle = ext4_journal_current_handle();
2709 err = PTR_ERR(inode);
2710 if (!IS_ERR(inode)) {
2711 init_special_inode(inode, inode->i_mode, rdev);
2712 inode->i_op = &ext4_special_inode_operations;
2713 err = ext4_add_nondir(handle, dentry, &inode);
2715 ext4_fc_track_create(handle, dentry);
2718 ext4_journal_stop(handle);
2719 if (!IS_ERR_OR_NULL(inode))
2721 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2726 static int ext4_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
2729 struct inode *inode;
2730 int err, retries = 0;
2732 err = dquot_initialize(dir);
2737 inode = ext4_new_inode_start_handle(dir, mode,
2740 EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
2741 4 + EXT4_XATTR_TRANS_BLOCKS);
2742 handle = ext4_journal_current_handle();
2743 err = PTR_ERR(inode);
2744 if (!IS_ERR(inode)) {
2745 inode->i_op = &ext4_file_inode_operations;
2746 inode->i_fop = &ext4_file_operations;
2747 ext4_set_aops(inode);
2748 d_tmpfile(dentry, inode);
2749 err = ext4_orphan_add(handle, inode);
2751 goto err_unlock_inode;
2752 mark_inode_dirty(inode);
2753 unlock_new_inode(inode);
2756 ext4_journal_stop(handle);
2757 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2761 ext4_journal_stop(handle);
2762 unlock_new_inode(inode);
2766 struct ext4_dir_entry_2 *ext4_init_dot_dotdot(struct inode *inode,
2767 struct ext4_dir_entry_2 *de,
2768 int blocksize, int csum_size,
2769 unsigned int parent_ino, int dotdot_real_len)
2771 de->inode = cpu_to_le32(inode->i_ino);
2773 de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len),
2775 strcpy(de->name, ".");
2776 ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2778 de = ext4_next_entry(de, blocksize);
2779 de->inode = cpu_to_le32(parent_ino);
2781 if (!dotdot_real_len)
2782 de->rec_len = ext4_rec_len_to_disk(blocksize -
2783 (csum_size + EXT4_DIR_REC_LEN(1)),
2786 de->rec_len = ext4_rec_len_to_disk(
2787 EXT4_DIR_REC_LEN(de->name_len), blocksize);
2788 strcpy(de->name, "..");
2789 ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2791 return ext4_next_entry(de, blocksize);
2794 int ext4_init_new_dir(handle_t *handle, struct inode *dir,
2795 struct inode *inode)
2797 struct buffer_head *dir_block = NULL;
2798 struct ext4_dir_entry_2 *de;
2799 ext4_lblk_t block = 0;
2800 unsigned int blocksize = dir->i_sb->s_blocksize;
2804 if (ext4_has_metadata_csum(dir->i_sb))
2805 csum_size = sizeof(struct ext4_dir_entry_tail);
2807 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2808 err = ext4_try_create_inline_dir(handle, dir, inode);
2809 if (err < 0 && err != -ENOSPC)
2816 dir_block = ext4_append(handle, inode, &block);
2817 if (IS_ERR(dir_block))
2818 return PTR_ERR(dir_block);
2819 de = (struct ext4_dir_entry_2 *)dir_block->b_data;
2820 ext4_init_dot_dotdot(inode, de, blocksize, csum_size, dir->i_ino, 0);
2821 set_nlink(inode, 2);
2823 ext4_initialize_dirent_tail(dir_block, blocksize);
2825 BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata");
2826 err = ext4_handle_dirty_dirblock(handle, inode, dir_block);
2829 set_buffer_verified(dir_block);
2835 static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
2838 struct inode *inode;
2839 int err, err2 = 0, credits, retries = 0;
2841 if (EXT4_DIR_LINK_MAX(dir))
2844 err = dquot_initialize(dir);
2848 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2849 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
2851 inode = ext4_new_inode_start_handle(dir, S_IFDIR | mode,
2853 0, NULL, EXT4_HT_DIR, credits);
2854 handle = ext4_journal_current_handle();
2855 err = PTR_ERR(inode);
2859 inode->i_op = &ext4_dir_inode_operations;
2860 inode->i_fop = &ext4_dir_operations;
2861 err = ext4_init_new_dir(handle, dir, inode);
2863 goto out_clear_inode;
2864 err = ext4_mark_inode_dirty(handle, inode);
2866 err = ext4_add_entry(handle, dentry, inode);
2870 ext4_orphan_add(handle, inode);
2871 unlock_new_inode(inode);
2872 err2 = ext4_mark_inode_dirty(handle, inode);
2875 ext4_journal_stop(handle);
2879 ext4_inc_count(dir);
2881 ext4_update_dx_flag(dir);
2882 err = ext4_mark_inode_dirty(handle, dir);
2884 goto out_clear_inode;
2885 d_instantiate_new(dentry, inode);
2886 ext4_fc_track_create(handle, dentry);
2887 if (IS_DIRSYNC(dir))
2888 ext4_handle_sync(handle);
2892 ext4_journal_stop(handle);
2894 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2900 * routine to check that the specified directory is empty (for rmdir)
2902 bool ext4_empty_dir(struct inode *inode)
2904 unsigned int offset;
2905 struct buffer_head *bh;
2906 struct ext4_dir_entry_2 *de;
2907 struct super_block *sb;
2909 if (ext4_has_inline_data(inode)) {
2910 int has_inline_data = 1;
2913 ret = empty_inline_dir(inode, &has_inline_data);
2914 if (has_inline_data)
2919 if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2)) {
2920 EXT4_ERROR_INODE(inode, "invalid size");
2923 /* The first directory block must not be a hole,
2924 * so treat it as DIRENT_HTREE
2926 bh = ext4_read_dirblock(inode, 0, DIRENT_HTREE);
2930 de = (struct ext4_dir_entry_2 *) bh->b_data;
2931 if (ext4_check_dir_entry(inode, NULL, de, bh, bh->b_data, bh->b_size,
2933 le32_to_cpu(de->inode) != inode->i_ino || strcmp(".", de->name)) {
2934 ext4_warning_inode(inode, "directory missing '.'");
2938 offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
2939 de = ext4_next_entry(de, sb->s_blocksize);
2940 if (ext4_check_dir_entry(inode, NULL, de, bh, bh->b_data, bh->b_size,
2942 le32_to_cpu(de->inode) == 0 || strcmp("..", de->name)) {
2943 ext4_warning_inode(inode, "directory missing '..'");
2947 offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
2948 while (offset < inode->i_size) {
2949 if (!(offset & (sb->s_blocksize - 1))) {
2950 unsigned int lblock;
2952 lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb);
2953 bh = ext4_read_dirblock(inode, lblock, EITHER);
2955 offset += sb->s_blocksize;
2961 de = (struct ext4_dir_entry_2 *) (bh->b_data +
2962 (offset & (sb->s_blocksize - 1)));
2963 if (ext4_check_dir_entry(inode, NULL, de, bh,
2964 bh->b_data, bh->b_size, offset) ||
2965 le32_to_cpu(de->inode)) {
2969 offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
2976 * ext4_orphan_add() links an unlinked or truncated inode into a list of
2977 * such inodes, starting at the superblock, in case we crash before the
2978 * file is closed/deleted, or in case the inode truncate spans multiple
2979 * transactions and the last transaction is not recovered after a crash.
2981 * At filesystem recovery time, we walk this list deleting unlinked
2982 * inodes and truncating linked inodes in ext4_orphan_cleanup().
2984 * Orphan list manipulation functions must be called under i_mutex unless
2985 * we are just creating the inode or deleting it.
2987 int ext4_orphan_add(handle_t *handle, struct inode *inode)
2989 struct super_block *sb = inode->i_sb;
2990 struct ext4_sb_info *sbi = EXT4_SB(sb);
2991 struct ext4_iloc iloc;
2995 if (!sbi->s_journal || is_bad_inode(inode))
2998 WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
2999 !inode_is_locked(inode));
3001 * Exit early if inode already is on orphan list. This is a big speedup
3002 * since we don't have to contend on the global s_orphan_lock.
3004 if (!list_empty(&EXT4_I(inode)->i_orphan))
3008 * Orphan handling is only valid for files with data blocks
3009 * being truncated, or files being unlinked. Note that we either
3010 * hold i_mutex, or the inode can not be referenced from outside,
3011 * so i_nlink should not be bumped due to race
3013 J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
3014 S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
3016 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
3017 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
3021 err = ext4_reserve_inode_write(handle, inode, &iloc);
3025 mutex_lock(&sbi->s_orphan_lock);
3027 * Due to previous errors inode may be already a part of on-disk
3028 * orphan list. If so skip on-disk list modification.
3030 if (!NEXT_ORPHAN(inode) || NEXT_ORPHAN(inode) >
3031 (le32_to_cpu(sbi->s_es->s_inodes_count))) {
3032 /* Insert this inode at the head of the on-disk orphan list */
3033 NEXT_ORPHAN(inode) = le32_to_cpu(sbi->s_es->s_last_orphan);
3034 sbi->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
3037 list_add(&EXT4_I(inode)->i_orphan, &sbi->s_orphan);
3038 mutex_unlock(&sbi->s_orphan_lock);
3041 err = ext4_handle_dirty_super(handle, sb);
3042 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
3047 * We have to remove inode from in-memory list if
3048 * addition to on disk orphan list failed. Stray orphan
3049 * list entries can cause panics at unmount time.
3051 mutex_lock(&sbi->s_orphan_lock);
3052 list_del_init(&EXT4_I(inode)->i_orphan);
3053 mutex_unlock(&sbi->s_orphan_lock);
3058 jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
3059 jbd_debug(4, "orphan inode %lu will point to %d\n",
3060 inode->i_ino, NEXT_ORPHAN(inode));
3062 ext4_std_error(sb, err);
3067 * ext4_orphan_del() removes an unlinked or truncated inode from the list
3068 * of such inodes stored on disk, because it is finally being cleaned up.
3070 int ext4_orphan_del(handle_t *handle, struct inode *inode)
3072 struct list_head *prev;
3073 struct ext4_inode_info *ei = EXT4_I(inode);
3074 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3076 struct ext4_iloc iloc;
3079 if (!sbi->s_journal && !(sbi->s_mount_state & EXT4_ORPHAN_FS))
3082 WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
3083 !inode_is_locked(inode));
3084 /* Do this quick check before taking global s_orphan_lock. */
3085 if (list_empty(&ei->i_orphan))
3089 /* Grab inode buffer early before taking global s_orphan_lock */
3090 err = ext4_reserve_inode_write(handle, inode, &iloc);
3093 mutex_lock(&sbi->s_orphan_lock);
3094 jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
3096 prev = ei->i_orphan.prev;
3097 list_del_init(&ei->i_orphan);
3099 /* If we're on an error path, we may not have a valid
3100 * transaction handle with which to update the orphan list on
3101 * disk, but we still need to remove the inode from the linked
3102 * list in memory. */
3103 if (!handle || err) {
3104 mutex_unlock(&sbi->s_orphan_lock);
3108 ino_next = NEXT_ORPHAN(inode);
3109 if (prev == &sbi->s_orphan) {
3110 jbd_debug(4, "superblock will point to %u\n", ino_next);
3111 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
3112 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
3114 mutex_unlock(&sbi->s_orphan_lock);
3117 sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
3118 mutex_unlock(&sbi->s_orphan_lock);
3119 err = ext4_handle_dirty_super(handle, inode->i_sb);
3121 struct ext4_iloc iloc2;
3122 struct inode *i_prev =
3123 &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
3125 jbd_debug(4, "orphan inode %lu will point to %u\n",
3126 i_prev->i_ino, ino_next);
3127 err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
3129 mutex_unlock(&sbi->s_orphan_lock);
3132 NEXT_ORPHAN(i_prev) = ino_next;
3133 err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
3134 mutex_unlock(&sbi->s_orphan_lock);
3138 NEXT_ORPHAN(inode) = 0;
3139 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
3141 ext4_std_error(inode->i_sb, err);
3149 static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
3152 struct inode *inode;
3153 struct buffer_head *bh;
3154 struct ext4_dir_entry_2 *de;
3155 handle_t *handle = NULL;
3157 if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb))))
3160 /* Initialize quotas before so that eventual writes go in
3161 * separate transaction */
3162 retval = dquot_initialize(dir);
3165 retval = dquot_initialize(d_inode(dentry));
3170 bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
3176 inode = d_inode(dentry);
3178 retval = -EFSCORRUPTED;
3179 if (le32_to_cpu(de->inode) != inode->i_ino)
3182 retval = -ENOTEMPTY;
3183 if (!ext4_empty_dir(inode))
3186 handle = ext4_journal_start(dir, EXT4_HT_DIR,
3187 EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
3188 if (IS_ERR(handle)) {
3189 retval = PTR_ERR(handle);
3194 if (IS_DIRSYNC(dir))
3195 ext4_handle_sync(handle);
3197 retval = ext4_delete_entry(handle, dir, de, bh);
3200 if (!EXT4_DIR_LINK_EMPTY(inode))
3201 ext4_warning_inode(inode,
3202 "empty directory '%.*s' has too many links (%u)",
3203 dentry->d_name.len, dentry->d_name.name,
3205 inode_inc_iversion(inode);
3207 /* There's no need to set i_disksize: the fact that i_nlink is
3208 * zero will ensure that the right thing happens during any
3211 ext4_orphan_add(handle, inode);
3212 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
3213 retval = ext4_mark_inode_dirty(handle, inode);
3216 ext4_dec_count(dir);
3217 ext4_update_dx_flag(dir);
3218 ext4_fc_track_unlink(handle, dentry);
3219 retval = ext4_mark_inode_dirty(handle, dir);
3221 #ifdef CONFIG_UNICODE
3222 /* VFS negative dentries are incompatible with Encoding and
3223 * Case-insensitiveness. Eventually we'll want avoid
3224 * invalidating the dentries here, alongside with returning the
3225 * negative dentries at ext4_lookup(), when it is better
3226 * supported by the VFS for the CI case.
3228 if (IS_CASEFOLDED(dir))
3229 d_invalidate(dentry);
3235 ext4_journal_stop(handle);
3239 int __ext4_unlink(handle_t *handle, struct inode *dir, const struct qstr *d_name,
3240 struct inode *inode)
3242 int retval = -ENOENT;
3243 struct buffer_head *bh;
3244 struct ext4_dir_entry_2 *de;
3245 int skip_remove_dentry = 0;
3247 bh = ext4_find_entry(dir, d_name, &de, NULL);
3254 if (le32_to_cpu(de->inode) != inode->i_ino) {
3256 * It's okay if we find dont find dentry which matches
3257 * the inode. That's because it might have gotten
3258 * renamed to a different inode number
3260 if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
3261 skip_remove_dentry = 1;
3266 if (IS_DIRSYNC(dir))
3267 ext4_handle_sync(handle);
3269 if (!skip_remove_dentry) {
3270 retval = ext4_delete_entry(handle, dir, de, bh);
3273 dir->i_ctime = dir->i_mtime = current_time(dir);
3274 ext4_update_dx_flag(dir);
3275 retval = ext4_mark_inode_dirty(handle, dir);
3281 if (inode->i_nlink == 0)
3282 ext4_warning_inode(inode, "Deleting file '%.*s' with no links",
3283 d_name->len, d_name->name);
3286 if (!inode->i_nlink)
3287 ext4_orphan_add(handle, inode);
3288 inode->i_ctime = current_time(inode);
3289 retval = ext4_mark_inode_dirty(handle, inode);
3296 static int ext4_unlink(struct inode *dir, struct dentry *dentry)
3301 if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb))))
3304 trace_ext4_unlink_enter(dir, dentry);
3306 * Initialize quotas before so that eventual writes go
3307 * in separate transaction
3309 retval = dquot_initialize(dir);
3312 retval = dquot_initialize(d_inode(dentry));
3316 handle = ext4_journal_start(dir, EXT4_HT_DIR,
3317 EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
3318 if (IS_ERR(handle)) {
3319 retval = PTR_ERR(handle);
3323 retval = __ext4_unlink(handle, dir, &dentry->d_name, d_inode(dentry));
3325 ext4_fc_track_unlink(handle, dentry);
3326 #ifdef CONFIG_UNICODE
3327 /* VFS negative dentries are incompatible with Encoding and
3328 * Case-insensitiveness. Eventually we'll want avoid
3329 * invalidating the dentries here, alongside with returning the
3330 * negative dentries at ext4_lookup(), when it is better
3331 * supported by the VFS for the CI case.
3333 if (IS_CASEFOLDED(dir))
3334 d_invalidate(dentry);
3337 ext4_journal_stop(handle);
3340 trace_ext4_unlink_exit(dentry, retval);
3344 static int ext4_symlink(struct inode *dir,
3345 struct dentry *dentry, const char *symname)
3348 struct inode *inode;
3349 int err, len = strlen(symname);
3351 struct fscrypt_str disk_link;
3353 if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb))))
3356 err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize,
3361 err = dquot_initialize(dir);
3365 if ((disk_link.len > EXT4_N_BLOCKS * 4)) {
3367 * For non-fast symlinks, we just allocate inode and put it on
3368 * orphan list in the first transaction => we need bitmap,
3369 * group descriptor, sb, inode block, quota blocks, and
3370 * possibly selinux xattr blocks.
3372 credits = 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
3373 EXT4_XATTR_TRANS_BLOCKS;
3376 * Fast symlink. We have to add entry to directory
3377 * (EXT4_DATA_TRANS_BLOCKS + EXT4_INDEX_EXTRA_TRANS_BLOCKS),
3378 * allocate new inode (bitmap, group descriptor, inode block,
3379 * quota blocks, sb is already counted in previous macros).
3381 credits = EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
3382 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3;
3385 inode = ext4_new_inode_start_handle(dir, S_IFLNK|S_IRWXUGO,
3386 &dentry->d_name, 0, NULL,
3387 EXT4_HT_DIR, credits);
3388 handle = ext4_journal_current_handle();
3389 if (IS_ERR(inode)) {
3391 ext4_journal_stop(handle);
3392 return PTR_ERR(inode);
3395 if (IS_ENCRYPTED(inode)) {
3396 err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);
3398 goto err_drop_inode;
3399 inode->i_op = &ext4_encrypted_symlink_inode_operations;
3402 if ((disk_link.len > EXT4_N_BLOCKS * 4)) {
3403 if (!IS_ENCRYPTED(inode))
3404 inode->i_op = &ext4_symlink_inode_operations;
3405 inode_nohighmem(inode);
3406 ext4_set_aops(inode);
3408 * We cannot call page_symlink() with transaction started
3409 * because it calls into ext4_write_begin() which can wait
3410 * for transaction commit if we are running out of space
3411 * and thus we deadlock. So we have to stop transaction now
3412 * and restart it when symlink contents is written.
3414 * To keep fs consistent in case of crash, we have to put inode
3415 * to orphan list in the mean time.
3418 err = ext4_orphan_add(handle, inode);
3420 ext4_journal_stop(handle);
3423 goto err_drop_inode;
3424 err = __page_symlink(inode, disk_link.name, disk_link.len, 1);
3426 goto err_drop_inode;
3428 * Now inode is being linked into dir (EXT4_DATA_TRANS_BLOCKS
3429 * + EXT4_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
3431 handle = ext4_journal_start(dir, EXT4_HT_DIR,
3432 EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
3433 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 1);
3434 if (IS_ERR(handle)) {
3435 err = PTR_ERR(handle);
3437 goto err_drop_inode;
3439 set_nlink(inode, 1);
3440 err = ext4_orphan_del(handle, inode);
3442 goto err_drop_inode;
3444 /* clear the extent format for fast symlink */
3445 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
3446 if (!IS_ENCRYPTED(inode)) {
3447 inode->i_op = &ext4_fast_symlink_inode_operations;
3448 inode->i_link = (char *)&EXT4_I(inode)->i_data;
3450 memcpy((char *)&EXT4_I(inode)->i_data, disk_link.name,
3452 inode->i_size = disk_link.len - 1;
3454 EXT4_I(inode)->i_disksize = inode->i_size;
3455 err = ext4_add_nondir(handle, dentry, &inode);
3457 ext4_journal_stop(handle);
3460 goto out_free_encrypted_link;
3464 ext4_journal_stop(handle);
3466 unlock_new_inode(inode);
3468 out_free_encrypted_link:
3469 if (disk_link.name != (unsigned char *)symname)
3470 kfree(disk_link.name);
3474 int __ext4_link(struct inode *dir, struct inode *inode, struct dentry *dentry)
3477 int err, retries = 0;
3479 handle = ext4_journal_start(dir, EXT4_HT_DIR,
3480 (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
3481 EXT4_INDEX_EXTRA_TRANS_BLOCKS) + 1);
3483 return PTR_ERR(handle);
3485 if (IS_DIRSYNC(dir))
3486 ext4_handle_sync(handle);
3488 inode->i_ctime = current_time(inode);
3489 ext4_inc_count(inode);
3492 err = ext4_add_entry(handle, dentry, inode);
3494 err = ext4_mark_inode_dirty(handle, inode);
3495 /* this can happen only for tmpfile being
3496 * linked the first time
3498 if (inode->i_nlink == 1)
3499 ext4_orphan_del(handle, inode);
3500 d_instantiate(dentry, inode);
3501 ext4_fc_track_link(handle, dentry);
3506 ext4_journal_stop(handle);
3507 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
3512 static int ext4_link(struct dentry *old_dentry,
3513 struct inode *dir, struct dentry *dentry)
3515 struct inode *inode = d_inode(old_dentry);
3518 if (inode->i_nlink >= EXT4_LINK_MAX)
3521 err = fscrypt_prepare_link(old_dentry, dir, dentry);
3525 if ((ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT)) &&
3526 (!projid_eq(EXT4_I(dir)->i_projid,
3527 EXT4_I(old_dentry->d_inode)->i_projid)))
3530 err = dquot_initialize(dir);
3533 return __ext4_link(dir, inode, dentry);
3537 * Try to find buffer head where contains the parent block.
3538 * It should be the inode block if it is inlined or the 1st block
3539 * if it is a normal dir.
3541 static struct buffer_head *ext4_get_first_dir_block(handle_t *handle,
3542 struct inode *inode,
3544 struct ext4_dir_entry_2 **parent_de,
3547 struct buffer_head *bh;
3549 if (!ext4_has_inline_data(inode)) {
3550 struct ext4_dir_entry_2 *de;
3551 unsigned int offset;
3553 /* The first directory block must not be a hole, so
3554 * treat it as DIRENT_HTREE
3556 bh = ext4_read_dirblock(inode, 0, DIRENT_HTREE);
3558 *retval = PTR_ERR(bh);
3562 de = (struct ext4_dir_entry_2 *) bh->b_data;
3563 if (ext4_check_dir_entry(inode, NULL, de, bh, bh->b_data,
3565 le32_to_cpu(de->inode) != inode->i_ino ||
3566 strcmp(".", de->name)) {
3567 EXT4_ERROR_INODE(inode, "directory missing '.'");
3569 *retval = -EFSCORRUPTED;
3572 offset = ext4_rec_len_from_disk(de->rec_len,
3573 inode->i_sb->s_blocksize);
3574 de = ext4_next_entry(de, inode->i_sb->s_blocksize);
3575 if (ext4_check_dir_entry(inode, NULL, de, bh, bh->b_data,
3576 bh->b_size, offset) ||
3577 le32_to_cpu(de->inode) == 0 || strcmp("..", de->name)) {
3578 EXT4_ERROR_INODE(inode, "directory missing '..'");
3580 *retval = -EFSCORRUPTED;
3589 return ext4_get_first_inline_block(inode, parent_de, retval);
3592 struct ext4_renament {
3594 struct dentry *dentry;
3595 struct inode *inode;
3597 int dir_nlink_delta;
3599 /* entry for "dentry" */
3600 struct buffer_head *bh;
3601 struct ext4_dir_entry_2 *de;
3604 /* entry for ".." in inode if it's a directory */
3605 struct buffer_head *dir_bh;
3606 struct ext4_dir_entry_2 *parent_de;
3610 static int ext4_rename_dir_prepare(handle_t *handle, struct ext4_renament *ent)
3614 ent->dir_bh = ext4_get_first_dir_block(handle, ent->inode,
3615 &retval, &ent->parent_de,
3619 if (le32_to_cpu(ent->parent_de->inode) != ent->dir->i_ino)
3620 return -EFSCORRUPTED;
3621 BUFFER_TRACE(ent->dir_bh, "get_write_access");
3622 return ext4_journal_get_write_access(handle, ent->dir_bh);
3625 static int ext4_rename_dir_finish(handle_t *handle, struct ext4_renament *ent,
3630 ent->parent_de->inode = cpu_to_le32(dir_ino);
3631 BUFFER_TRACE(ent->dir_bh, "call ext4_handle_dirty_metadata");
3632 if (!ent->dir_inlined) {
3633 if (is_dx(ent->inode)) {
3634 retval = ext4_handle_dirty_dx_node(handle,
3638 retval = ext4_handle_dirty_dirblock(handle, ent->inode,
3642 retval = ext4_mark_inode_dirty(handle, ent->inode);
3645 ext4_std_error(ent->dir->i_sb, retval);
3651 static int ext4_setent(handle_t *handle, struct ext4_renament *ent,
3652 unsigned ino, unsigned file_type)
3654 int retval, retval2;
3656 BUFFER_TRACE(ent->bh, "get write access");
3657 retval = ext4_journal_get_write_access(handle, ent->bh);
3660 ent->de->inode = cpu_to_le32(ino);
3661 if (ext4_has_feature_filetype(ent->dir->i_sb))
3662 ent->de->file_type = file_type;
3663 inode_inc_iversion(ent->dir);
3664 ent->dir->i_ctime = ent->dir->i_mtime =
3665 current_time(ent->dir);
3666 retval = ext4_mark_inode_dirty(handle, ent->dir);
3667 BUFFER_TRACE(ent->bh, "call ext4_handle_dirty_metadata");
3668 if (!ent->inlined) {
3669 retval2 = ext4_handle_dirty_dirblock(handle, ent->dir, ent->bh);
3670 if (unlikely(retval2)) {
3671 ext4_std_error(ent->dir->i_sb, retval2);
3678 static void ext4_resetent(handle_t *handle, struct ext4_renament *ent,
3679 unsigned ino, unsigned file_type)
3681 struct ext4_renament old = *ent;
3685 * old->de could have moved from under us during make indexed dir,
3686 * so the old->de may no longer valid and need to find it again
3687 * before reset old inode info.
3689 old.bh = ext4_find_entry(old.dir, &old.dentry->d_name, &old.de, NULL);
3691 retval = PTR_ERR(old.bh);
3695 ext4_std_error(old.dir->i_sb, retval);
3699 ext4_setent(handle, &old, ino, file_type);
3703 static int ext4_find_delete_entry(handle_t *handle, struct inode *dir,
3704 const struct qstr *d_name)
3706 int retval = -ENOENT;
3707 struct buffer_head *bh;
3708 struct ext4_dir_entry_2 *de;
3710 bh = ext4_find_entry(dir, d_name, &de, NULL);
3714 retval = ext4_delete_entry(handle, dir, de, bh);
3720 static void ext4_rename_delete(handle_t *handle, struct ext4_renament *ent,
3725 * ent->de could have moved from under us during htree split, so make
3726 * sure that we are deleting the right entry. We might also be pointing
3727 * to a stale entry in the unused part of ent->bh so just checking inum
3728 * and the name isn't enough.
3730 if (le32_to_cpu(ent->de->inode) != ent->inode->i_ino ||
3731 ent->de->name_len != ent->dentry->d_name.len ||
3732 strncmp(ent->de->name, ent->dentry->d_name.name,
3733 ent->de->name_len) ||
3735 retval = ext4_find_delete_entry(handle, ent->dir,
3736 &ent->dentry->d_name);
3738 retval = ext4_delete_entry(handle, ent->dir, ent->de, ent->bh);
3739 if (retval == -ENOENT) {
3740 retval = ext4_find_delete_entry(handle, ent->dir,
3741 &ent->dentry->d_name);
3746 ext4_warning_inode(ent->dir,
3747 "Deleting old file: nlink %d, error=%d",
3748 ent->dir->i_nlink, retval);
3752 static void ext4_update_dir_count(handle_t *handle, struct ext4_renament *ent)
3754 if (ent->dir_nlink_delta) {
3755 if (ent->dir_nlink_delta == -1)
3756 ext4_dec_count(ent->dir);
3758 ext4_inc_count(ent->dir);
3759 ext4_mark_inode_dirty(handle, ent->dir);
3763 static struct inode *ext4_whiteout_for_rename(struct ext4_renament *ent,
3764 int credits, handle_t **h)
3771 * for inode block, sb block, group summaries,
3774 credits += (EXT4_MAXQUOTAS_TRANS_BLOCKS(ent->dir->i_sb) +
3775 EXT4_XATTR_TRANS_BLOCKS + 4);
3777 wh = ext4_new_inode_start_handle(ent->dir, S_IFCHR | WHITEOUT_MODE,
3778 &ent->dentry->d_name, 0, NULL,
3779 EXT4_HT_DIR, credits);
3781 handle = ext4_journal_current_handle();
3784 ext4_journal_stop(handle);
3785 if (PTR_ERR(wh) == -ENOSPC &&
3786 ext4_should_retry_alloc(ent->dir->i_sb, &retries))
3790 init_special_inode(wh, wh->i_mode, WHITEOUT_DEV);
3791 wh->i_op = &ext4_special_inode_operations;
3797 * Anybody can rename anything with this: the permission checks are left to the
3798 * higher-level routines.
3800 * n.b. old_{dentry,inode) refers to the source dentry/inode
3801 * while new_{dentry,inode) refers to the destination dentry/inode
3802 * This comes from rename(const char *oldpath, const char *newpath)
3804 static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
3805 struct inode *new_dir, struct dentry *new_dentry,
3808 handle_t *handle = NULL;
3809 struct ext4_renament old = {
3811 .dentry = old_dentry,
3812 .inode = d_inode(old_dentry),
3814 struct ext4_renament new = {
3816 .dentry = new_dentry,
3817 .inode = d_inode(new_dentry),
3821 struct inode *whiteout = NULL;
3825 if (new.inode && new.inode->i_nlink == 0) {
3826 EXT4_ERROR_INODE(new.inode,
3827 "target of rename is already freed");
3828 return -EFSCORRUPTED;
3831 if ((ext4_test_inode_flag(new_dir, EXT4_INODE_PROJINHERIT)) &&
3832 (!projid_eq(EXT4_I(new_dir)->i_projid,
3833 EXT4_I(old_dentry->d_inode)->i_projid)))
3836 retval = dquot_initialize(old.dir);
3839 retval = dquot_initialize(new.dir);
3843 /* Initialize quotas before so that eventual writes go
3844 * in separate transaction */
3846 retval = dquot_initialize(new.inode);
3851 old.bh = ext4_find_entry(old.dir, &old.dentry->d_name, &old.de, NULL);
3853 return PTR_ERR(old.bh);
3855 * Check for inode number is _not_ due to possible IO errors.
3856 * We might rmdir the source, keep it as pwd of some process
3857 * and merrily kill the link to whatever was created under the
3858 * same name. Goodbye sticky bit ;-<
3861 if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
3864 new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
3865 &new.de, &new.inlined);
3866 if (IS_ERR(new.bh)) {
3867 retval = PTR_ERR(new.bh);
3877 if (new.inode && !test_opt(new.dir->i_sb, NO_AUTO_DA_ALLOC))
3878 ext4_alloc_da_blocks(old.inode);
3880 credits = (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
3881 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
3882 if (!(flags & RENAME_WHITEOUT)) {
3883 handle = ext4_journal_start(old.dir, EXT4_HT_DIR, credits);
3884 if (IS_ERR(handle)) {
3885 retval = PTR_ERR(handle);
3889 whiteout = ext4_whiteout_for_rename(&old, credits, &handle);
3890 if (IS_ERR(whiteout)) {
3891 retval = PTR_ERR(whiteout);
3896 old_file_type = old.de->file_type;
3897 if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
3898 ext4_handle_sync(handle);
3900 if (S_ISDIR(old.inode->i_mode)) {
3902 retval = -ENOTEMPTY;
3903 if (!ext4_empty_dir(new.inode))
3907 if (new.dir != old.dir && EXT4_DIR_LINK_MAX(new.dir))
3910 retval = ext4_rename_dir_prepare(handle, &old);
3915 * If we're renaming a file within an inline_data dir and adding or
3916 * setting the new dirent causes a conversion from inline_data to
3917 * extents/blockmap, we need to force the dirent delete code to
3918 * re-read the directory, or else we end up trying to delete a dirent
3919 * from what is now the extent tree root (or a block map).
3921 force_reread = (new.dir->i_ino == old.dir->i_ino &&
3922 ext4_test_inode_flag(new.dir, EXT4_INODE_INLINE_DATA));
3926 * Do this before adding a new entry, so the old entry is sure
3927 * to be still pointing to the valid old entry.
3929 retval = ext4_setent(handle, &old, whiteout->i_ino,
3933 retval = ext4_mark_inode_dirty(handle, whiteout);
3934 if (unlikely(retval))
3939 retval = ext4_add_entry(handle, new.dentry, old.inode);
3943 retval = ext4_setent(handle, &new,
3944 old.inode->i_ino, old_file_type);
3949 force_reread = !ext4_test_inode_flag(new.dir,
3950 EXT4_INODE_INLINE_DATA);
3953 * Like most other Unix systems, set the ctime for inodes on a
3956 old.inode->i_ctime = current_time(old.inode);
3957 retval = ext4_mark_inode_dirty(handle, old.inode);
3958 if (unlikely(retval))
3965 ext4_rename_delete(handle, &old, force_reread);
3969 ext4_dec_count(new.inode);
3970 new.inode->i_ctime = current_time(new.inode);
3972 old.dir->i_ctime = old.dir->i_mtime = current_time(old.dir);
3973 ext4_update_dx_flag(old.dir);
3975 retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
3979 ext4_dec_count(old.dir);
3981 /* checked ext4_empty_dir above, can't have another
3982 * parent, ext4_dec_count() won't work for many-linked
3984 clear_nlink(new.inode);
3986 ext4_inc_count(new.dir);
3987 ext4_update_dx_flag(new.dir);
3988 retval = ext4_mark_inode_dirty(handle, new.dir);
3989 if (unlikely(retval))
3993 retval = ext4_mark_inode_dirty(handle, old.dir);
3994 if (unlikely(retval))
3997 if (S_ISDIR(old.inode->i_mode)) {
3999 * We disable fast commits here that's because the
4000 * replay code is not yet capable of changing dot dot
4001 * dirents in directories.
4003 ext4_fc_mark_ineligible(old.inode->i_sb,
4004 EXT4_FC_REASON_RENAME_DIR);
4007 ext4_fc_track_unlink(handle, new.dentry);
4008 __ext4_fc_track_link(handle, old.inode, new.dentry);
4009 __ext4_fc_track_unlink(handle, old.inode, old.dentry);
4011 __ext4_fc_track_create(handle, whiteout, old.dentry);
4015 retval = ext4_mark_inode_dirty(handle, new.inode);
4016 if (unlikely(retval))
4018 if (!new.inode->i_nlink)
4019 ext4_orphan_add(handle, new.inode);
4026 ext4_resetent(handle, &old,
4027 old.inode->i_ino, old_file_type);
4028 drop_nlink(whiteout);
4029 ext4_orphan_add(handle, whiteout);
4031 unlock_new_inode(whiteout);
4032 ext4_journal_stop(handle);
4035 ext4_journal_stop(handle);
4044 static int ext4_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
4045 struct inode *new_dir, struct dentry *new_dentry)
4047 handle_t *handle = NULL;
4048 struct ext4_renament old = {
4050 .dentry = old_dentry,
4051 .inode = d_inode(old_dentry),
4053 struct ext4_renament new = {
4055 .dentry = new_dentry,
4056 .inode = d_inode(new_dentry),
4060 struct timespec64 ctime;
4062 if ((ext4_test_inode_flag(new_dir, EXT4_INODE_PROJINHERIT) &&
4063 !projid_eq(EXT4_I(new_dir)->i_projid,
4064 EXT4_I(old_dentry->d_inode)->i_projid)) ||
4065 (ext4_test_inode_flag(old_dir, EXT4_INODE_PROJINHERIT) &&
4066 !projid_eq(EXT4_I(old_dir)->i_projid,
4067 EXT4_I(new_dentry->d_inode)->i_projid)))
4070 retval = dquot_initialize(old.dir);
4073 retval = dquot_initialize(new.dir);
4077 old.bh = ext4_find_entry(old.dir, &old.dentry->d_name,
4078 &old.de, &old.inlined);
4080 return PTR_ERR(old.bh);
4082 * Check for inode number is _not_ due to possible IO errors.
4083 * We might rmdir the source, keep it as pwd of some process
4084 * and merrily kill the link to whatever was created under the
4085 * same name. Goodbye sticky bit ;-<
4088 if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
4091 new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
4092 &new.de, &new.inlined);
4093 if (IS_ERR(new.bh)) {
4094 retval = PTR_ERR(new.bh);
4099 /* RENAME_EXCHANGE case: old *and* new must both exist */
4100 if (!new.bh || le32_to_cpu(new.de->inode) != new.inode->i_ino)
4103 handle = ext4_journal_start(old.dir, EXT4_HT_DIR,
4104 (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
4105 2 * EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2));
4106 if (IS_ERR(handle)) {
4107 retval = PTR_ERR(handle);
4112 if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
4113 ext4_handle_sync(handle);
4115 if (S_ISDIR(old.inode->i_mode)) {
4117 retval = ext4_rename_dir_prepare(handle, &old);
4121 if (S_ISDIR(new.inode->i_mode)) {
4123 retval = ext4_rename_dir_prepare(handle, &new);
4129 * Other than the special case of overwriting a directory, parents'
4130 * nlink only needs to be modified if this is a cross directory rename.
4132 if (old.dir != new.dir && old.is_dir != new.is_dir) {
4133 old.dir_nlink_delta = old.is_dir ? -1 : 1;
4134 new.dir_nlink_delta = -old.dir_nlink_delta;
4136 if ((old.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(old.dir)) ||
4137 (new.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(new.dir)))
4141 new_file_type = new.de->file_type;
4142 retval = ext4_setent(handle, &new, old.inode->i_ino, old.de->file_type);
4146 retval = ext4_setent(handle, &old, new.inode->i_ino, new_file_type);
4151 * Like most other Unix systems, set the ctime for inodes on a
4154 ctime = current_time(old.inode);
4155 old.inode->i_ctime = ctime;
4156 new.inode->i_ctime = ctime;
4157 retval = ext4_mark_inode_dirty(handle, old.inode);
4158 if (unlikely(retval))
4160 retval = ext4_mark_inode_dirty(handle, new.inode);
4161 if (unlikely(retval))
4163 ext4_fc_mark_ineligible(new.inode->i_sb,
4164 EXT4_FC_REASON_CROSS_RENAME);
4166 retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
4171 retval = ext4_rename_dir_finish(handle, &new, old.dir->i_ino);
4175 ext4_update_dir_count(handle, &old);
4176 ext4_update_dir_count(handle, &new);
4185 ext4_journal_stop(handle);
4189 static int ext4_rename2(struct inode *old_dir, struct dentry *old_dentry,
4190 struct inode *new_dir, struct dentry *new_dentry,
4195 if (unlikely(ext4_forced_shutdown(EXT4_SB(old_dir->i_sb))))
4198 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
4201 err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
4206 if (flags & RENAME_EXCHANGE) {
4207 return ext4_cross_rename(old_dir, old_dentry,
4208 new_dir, new_dentry);
4211 return ext4_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
4215 * directories can handle most operations...
4217 const struct inode_operations ext4_dir_inode_operations = {
4218 .create = ext4_create,
4219 .lookup = ext4_lookup,
4221 .unlink = ext4_unlink,
4222 .symlink = ext4_symlink,
4223 .mkdir = ext4_mkdir,
4224 .rmdir = ext4_rmdir,
4225 .mknod = ext4_mknod,
4226 .tmpfile = ext4_tmpfile,
4227 .rename = ext4_rename2,
4228 .setattr = ext4_setattr,
4229 .getattr = ext4_getattr,
4230 .listxattr = ext4_listxattr,
4231 .get_acl = ext4_get_acl,
4232 .set_acl = ext4_set_acl,
4233 .fiemap = ext4_fiemap,
4236 const struct inode_operations ext4_special_inode_operations = {
4237 .setattr = ext4_setattr,
4238 .getattr = ext4_getattr,
4239 .listxattr = ext4_listxattr,
4240 .get_acl = ext4_get_acl,
4241 .set_acl = ext4_set_acl,