1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
6 #include <linux/iversion.h>
7 #include <linux/namei.h>
8 #include <linux/slab.h>
9 #include <linux/buffer_head.h>
10 #include <linux/nls.h>
12 #include "exfat_raw.h"
15 static inline unsigned long exfat_d_version(struct dentry *dentry)
17 return (unsigned long) dentry->d_fsdata;
20 static inline void exfat_d_version_set(struct dentry *dentry,
21 unsigned long version)
23 dentry->d_fsdata = (void *) version;
27 * If new entry was created in the parent, it could create the 8.3 alias (the
28 * shortname of logname). So, the parent may have the negative-dentry which
29 * matches the created 8.3 alias.
31 * If it happened, the negative dentry isn't actually negative anymore. So,
34 static int exfat_d_revalidate(struct dentry *dentry, unsigned int flags)
38 if (flags & LOOKUP_RCU)
42 * This is not negative dentry. Always valid.
44 * Note, rename() to existing directory entry will have ->d_inode, and
45 * will use existing name which isn't specified name by user.
47 * We may be able to drop this positive dentry here. But dropping
48 * positive dentry isn't good idea. So it's unsupported like
49 * rename("filename", "FILENAME") for now.
51 if (d_really_is_positive(dentry))
55 * Drop the negative dentry, in order to make sure to use the case
56 * sensitive name which is specified by user if this is for creation.
58 if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
61 spin_lock(&dentry->d_lock);
62 ret = inode_eq_iversion(d_inode(dentry->d_parent),
63 exfat_d_version(dentry));
64 spin_unlock(&dentry->d_lock);
68 /* returns the length of a struct qstr, ignoring trailing dots */
69 static unsigned int exfat_striptail_len(unsigned int len, const char *name)
71 while (len && name[len - 1] == '.')
77 * Compute the hash for the exfat name corresponding to the dentry. If the name
78 * is invalid, we leave the hash code unchanged so that the existing dentry can
79 * be used. The exfat fs routines will return ENOENT or EINVAL as appropriate.
81 static int exfat_d_hash(const struct dentry *dentry, struct qstr *qstr)
83 struct super_block *sb = dentry->d_sb;
84 struct nls_table *t = EXFAT_SB(sb)->nls_io;
85 const unsigned char *name = qstr->name;
86 unsigned int len = exfat_striptail_len(qstr->len, qstr->name);
87 unsigned long hash = init_name_hash(dentry);
91 for (i = 0; i < len; i += charlen) {
92 charlen = t->char2uni(&name[i], len - i, &c);
95 hash = partial_name_hash(exfat_toupper(sb, c), hash);
98 qstr->hash = end_name_hash(hash);
102 static int exfat_d_cmp(const struct dentry *dentry, unsigned int len,
103 const char *str, const struct qstr *name)
105 struct super_block *sb = dentry->d_sb;
106 struct nls_table *t = EXFAT_SB(sb)->nls_io;
107 unsigned int alen = exfat_striptail_len(name->len, name->name);
108 unsigned int blen = exfat_striptail_len(len, str);
115 for (i = 0; i < len; i += charlen) {
116 charlen = t->char2uni(&name->name[i], alen - i, &c1);
119 if (charlen != t->char2uni(&str[i], blen - i, &c2))
122 if (exfat_toupper(sb, c1) != exfat_toupper(sb, c2))
129 const struct dentry_operations exfat_dentry_ops = {
130 .d_revalidate = exfat_d_revalidate,
131 .d_hash = exfat_d_hash,
132 .d_compare = exfat_d_cmp,
135 static int exfat_utf8_d_hash(const struct dentry *dentry, struct qstr *qstr)
137 struct super_block *sb = dentry->d_sb;
138 const unsigned char *name = qstr->name;
139 unsigned int len = exfat_striptail_len(qstr->len, qstr->name);
140 unsigned long hash = init_name_hash(dentry);
144 for (i = 0; i < len; i += charlen) {
145 charlen = utf8_to_utf32(&name[i], len - i, &u);
150 * exfat_toupper() works only for code points up to the U+FFFF.
152 hash = partial_name_hash(u <= 0xFFFF ? exfat_toupper(sb, u) : u,
156 qstr->hash = end_name_hash(hash);
160 static int exfat_utf8_d_cmp(const struct dentry *dentry, unsigned int len,
161 const char *str, const struct qstr *name)
163 struct super_block *sb = dentry->d_sb;
164 unsigned int alen = exfat_striptail_len(name->len, name->name);
165 unsigned int blen = exfat_striptail_len(len, str);
172 for (i = 0; i < alen; i += charlen) {
173 charlen = utf8_to_utf32(&name->name[i], alen - i, &u_a);
176 if (charlen != utf8_to_utf32(&str[i], blen - i, &u_b))
179 if (u_a <= 0xFFFF && u_b <= 0xFFFF) {
180 if (exfat_toupper(sb, u_a) != exfat_toupper(sb, u_b))
191 const struct dentry_operations exfat_utf8_dentry_ops = {
192 .d_revalidate = exfat_d_revalidate,
193 .d_hash = exfat_utf8_d_hash,
194 .d_compare = exfat_utf8_d_cmp,
197 /* used only in search empty_slot() */
198 #define CNT_UNUSED_NOHIT (-1)
199 #define CNT_UNUSED_HIT (-2)
200 /* search EMPTY CONTINUOUS "num_entries" entries */
201 static int exfat_search_empty_slot(struct super_block *sb,
202 struct exfat_hint_femp *hint_femp, struct exfat_chain *p_dir,
205 int i, dentry, num_empty = 0;
206 int dentries_per_clu;
208 struct exfat_chain clu;
209 struct exfat_dentry *ep;
210 struct exfat_sb_info *sbi = EXFAT_SB(sb);
211 struct buffer_head *bh;
213 dentries_per_clu = sbi->dentries_per_clu;
215 if (hint_femp->eidx != EXFAT_HINT_NONE) {
216 dentry = hint_femp->eidx;
217 if (num_entries <= hint_femp->count) {
218 hint_femp->eidx = EXFAT_HINT_NONE;
222 exfat_chain_dup(&clu, &hint_femp->cur);
224 exfat_chain_dup(&clu, p_dir);
228 while (clu.dir != EXFAT_EOF_CLUSTER) {
229 i = dentry & (dentries_per_clu - 1);
231 for (; i < dentries_per_clu; i++, dentry++) {
232 ep = exfat_get_dentry(sb, &clu, i, &bh, NULL);
235 type = exfat_get_entry_type(ep);
238 if (type == TYPE_UNUSED || type == TYPE_DELETED) {
240 if (hint_femp->eidx == EXFAT_HINT_NONE) {
241 hint_femp->eidx = dentry;
242 hint_femp->count = CNT_UNUSED_NOHIT;
243 exfat_chain_set(&hint_femp->cur,
244 clu.dir, clu.size, clu.flags);
247 if (type == TYPE_UNUSED &&
248 hint_femp->count != CNT_UNUSED_HIT)
249 hint_femp->count = CNT_UNUSED_HIT;
251 if (hint_femp->eidx != EXFAT_HINT_NONE &&
252 hint_femp->count == CNT_UNUSED_HIT) {
253 /* unused empty group means
254 * an empty group which includes
258 "found bogus dentry(%d) beyond unused empty group(%d) (start_clu : %u, cur_clu : %u)",
259 dentry, hint_femp->eidx,
260 p_dir->dir, clu.dir);
265 hint_femp->eidx = EXFAT_HINT_NONE;
268 if (num_empty >= num_entries) {
269 /* found and invalidate hint_femp */
270 hint_femp->eidx = EXFAT_HINT_NONE;
271 return (dentry - (num_entries - 1));
275 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
279 clu.dir = EXFAT_EOF_CLUSTER;
281 if (exfat_get_next_cluster(sb, &clu.dir))
289 static int exfat_check_max_dentries(struct inode *inode)
291 if (EXFAT_B_TO_DEN(i_size_read(inode)) >= MAX_EXFAT_DENTRIES) {
293 * exFAT spec allows a dir to grow up to 8388608(256MB)
301 /* find empty directory entry.
302 * if there isn't any empty slot, expand cluster chain.
304 static int exfat_find_empty_entry(struct inode *inode,
305 struct exfat_chain *p_dir, int num_entries)
308 unsigned int ret, last_clu;
311 struct exfat_chain clu;
312 struct exfat_dentry *ep = NULL;
313 struct super_block *sb = inode->i_sb;
314 struct exfat_sb_info *sbi = EXFAT_SB(sb);
315 struct exfat_inode_info *ei = EXFAT_I(inode);
316 struct exfat_hint_femp hint_femp;
318 hint_femp.eidx = EXFAT_HINT_NONE;
320 if (ei->hint_femp.eidx != EXFAT_HINT_NONE) {
321 hint_femp = ei->hint_femp;
322 ei->hint_femp.eidx = EXFAT_HINT_NONE;
325 while ((dentry = exfat_search_empty_slot(sb, &hint_femp, p_dir,
330 if (exfat_check_max_dentries(inode))
333 /* we trust p_dir->size regardless of FAT type */
334 if (exfat_find_last_cluster(sb, p_dir, &last_clu))
338 * Allocate new cluster to this directory
340 exfat_chain_set(&clu, last_clu + 1, 0, p_dir->flags);
342 /* allocate a cluster */
343 ret = exfat_alloc_cluster(inode, 1, &clu, IS_DIRSYNC(inode));
347 if (exfat_zeroed_cluster(inode, clu.dir))
350 /* append to the FAT chain */
351 if (clu.flags != p_dir->flags) {
352 /* no-fat-chain bit is disabled,
353 * so fat-chain should be synced with alloc-bitmap
355 exfat_chain_cont_cluster(sb, p_dir->dir, p_dir->size);
356 p_dir->flags = ALLOC_FAT_CHAIN;
357 hint_femp.cur.flags = ALLOC_FAT_CHAIN;
360 if (clu.flags == ALLOC_FAT_CHAIN)
361 if (exfat_ent_set(sb, last_clu, clu.dir))
364 if (hint_femp.eidx == EXFAT_HINT_NONE) {
365 /* the special case that new dentry
366 * should be allocated from the start of new cluster
368 hint_femp.eidx = EXFAT_B_TO_DEN_IDX(p_dir->size, sbi);
369 hint_femp.count = sbi->dentries_per_clu;
371 exfat_chain_set(&hint_femp.cur, clu.dir, 0, clu.flags);
373 hint_femp.cur.size++;
375 size = EXFAT_CLU_TO_B(p_dir->size, sbi);
377 /* update the directory entry */
378 if (p_dir->dir != sbi->root_dir) {
379 struct buffer_head *bh;
381 ep = exfat_get_dentry(sb,
382 &(ei->dir), ei->entry + 1, &bh, §or);
386 ep->dentry.stream.valid_size = cpu_to_le64(size);
387 ep->dentry.stream.size = ep->dentry.stream.valid_size;
388 ep->dentry.stream.flags = p_dir->flags;
389 exfat_update_bh(bh, IS_DIRSYNC(inode));
391 if (exfat_update_dir_chksum(inode, &(ei->dir),
396 /* directory inode should be updated in here */
397 i_size_write(inode, size);
398 EXFAT_I(inode)->i_size_ondisk += sbi->cluster_size;
399 EXFAT_I(inode)->i_size_aligned += sbi->cluster_size;
400 EXFAT_I(inode)->flags = p_dir->flags;
401 inode->i_blocks += 1 << sbi->sect_per_clus_bits;
408 * Name Resolution Functions :
409 * Zero if it was successful; otherwise nonzero.
411 static int __exfat_resolve_path(struct inode *inode, const unsigned char *path,
412 struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
416 int lossy = NLS_NAME_NO_LOSSY;
417 struct super_block *sb = inode->i_sb;
418 struct exfat_sb_info *sbi = EXFAT_SB(sb);
419 struct exfat_inode_info *ei = EXFAT_I(inode);
421 /* strip all trailing periods */
422 namelen = exfat_striptail_len(strlen(path), path);
426 if (strlen(path) > (MAX_NAME_LENGTH * MAX_CHARSET_SIZE))
427 return -ENAMETOOLONG;
430 * strip all leading spaces :
431 * "MS windows 7" supports leading spaces.
432 * So we should skip this preprocessing for compatibility.
435 /* file name conversion :
436 * If lookup case, we allow bad-name for compatibility.
438 namelen = exfat_nls_to_utf16(sb, path, namelen, p_uniname,
441 return namelen; /* return error value */
443 if ((lossy && !lookup) || !namelen)
446 exfat_chain_set(p_dir, ei->start_clu,
447 EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags);
452 static inline int exfat_resolve_path(struct inode *inode,
453 const unsigned char *path, struct exfat_chain *dir,
454 struct exfat_uni_name *uni)
456 return __exfat_resolve_path(inode, path, dir, uni, 0);
459 static inline int exfat_resolve_path_for_lookup(struct inode *inode,
460 const unsigned char *path, struct exfat_chain *dir,
461 struct exfat_uni_name *uni)
463 return __exfat_resolve_path(inode, path, dir, uni, 1);
466 static inline loff_t exfat_make_i_pos(struct exfat_dir_entry *info)
468 return ((loff_t) info->dir.dir << 32) | (info->entry & 0xffffffff);
471 static int exfat_add_entry(struct inode *inode, const char *path,
472 struct exfat_chain *p_dir, unsigned int type,
473 struct exfat_dir_entry *info)
475 int ret, dentry, num_entries;
476 struct super_block *sb = inode->i_sb;
477 struct exfat_sb_info *sbi = EXFAT_SB(sb);
478 struct exfat_uni_name uniname;
479 struct exfat_chain clu;
481 unsigned int start_clu = EXFAT_FREE_CLUSTER;
483 ret = exfat_resolve_path(inode, path, p_dir, &uniname);
487 num_entries = exfat_calc_num_entries(&uniname);
488 if (num_entries < 0) {
493 /* exfat_find_empty_entry must be called before alloc_cluster() */
494 dentry = exfat_find_empty_entry(inode, p_dir, num_entries);
496 ret = dentry; /* -EIO or -ENOSPC */
500 if (type == TYPE_DIR) {
501 ret = exfat_alloc_new_dir(inode, &clu);
505 clu_size = sbi->cluster_size;
508 /* update the directory entry */
509 /* fill the dos name directory entry information of the created file.
510 * the first cluster is not determined yet. (0)
512 ret = exfat_init_dir_entry(inode, p_dir, dentry, type,
513 start_clu, clu_size);
517 ret = exfat_init_ext_entry(inode, p_dir, dentry, num_entries, &uniname);
522 info->entry = dentry;
523 info->flags = ALLOC_NO_FAT_CHAIN;
526 if (type == TYPE_FILE) {
527 info->attr = ATTR_ARCHIVE;
528 info->start_clu = EXFAT_EOF_CLUSTER;
530 info->num_subdirs = 0;
532 info->attr = ATTR_SUBDIR;
533 info->start_clu = start_clu;
534 info->size = clu_size;
535 info->num_subdirs = EXFAT_MIN_SUBDIR;
537 memset(&info->crtime, 0, sizeof(info->crtime));
538 memset(&info->mtime, 0, sizeof(info->mtime));
539 memset(&info->atime, 0, sizeof(info->atime));
544 static int exfat_create(struct user_namespace *mnt_userns, struct inode *dir,
545 struct dentry *dentry, umode_t mode, bool excl)
547 struct super_block *sb = dir->i_sb;
549 struct exfat_chain cdir;
550 struct exfat_dir_entry info;
554 mutex_lock(&EXFAT_SB(sb)->s_lock);
555 exfat_set_volume_dirty(sb);
556 err = exfat_add_entry(dir, dentry->d_name.name, &cdir, TYPE_FILE,
558 exfat_clear_volume_dirty(sb);
562 inode_inc_iversion(dir);
563 dir->i_ctime = dir->i_mtime = current_time(dir);
565 exfat_sync_inode(dir);
567 mark_inode_dirty(dir);
569 i_pos = exfat_make_i_pos(&info);
570 inode = exfat_build_inode(sb, &info, i_pos);
571 err = PTR_ERR_OR_ZERO(inode);
575 inode_inc_iversion(inode);
576 inode->i_mtime = inode->i_atime = inode->i_ctime =
577 EXFAT_I(inode)->i_crtime = current_time(inode);
578 exfat_truncate_atime(&inode->i_atime);
579 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
581 d_instantiate(dentry, inode);
583 mutex_unlock(&EXFAT_SB(sb)->s_lock);
588 static int exfat_find(struct inode *dir, struct qstr *qname,
589 struct exfat_dir_entry *info)
591 int ret, dentry, num_entries, count;
592 struct exfat_chain cdir;
593 struct exfat_uni_name uni_name;
594 struct super_block *sb = dir->i_sb;
595 struct exfat_sb_info *sbi = EXFAT_SB(sb);
596 struct exfat_inode_info *ei = EXFAT_I(dir);
597 struct exfat_dentry *ep, *ep2;
598 struct exfat_entry_set_cache *es;
599 /* for optimized dir & entry to prevent long traverse of cluster chain */
600 struct exfat_hint hint_opt;
605 /* check the validity of directory name in the given pathname */
606 ret = exfat_resolve_path_for_lookup(dir, qname->name, &cdir, &uni_name);
610 num_entries = exfat_calc_num_entries(&uni_name);
614 /* check the validation of hint_stat and initialize it if required */
615 if (ei->version != (inode_peek_iversion_raw(dir) & 0xffffffff)) {
616 ei->hint_stat.clu = cdir.dir;
617 ei->hint_stat.eidx = 0;
618 ei->version = (inode_peek_iversion_raw(dir) & 0xffffffff);
619 ei->hint_femp.eidx = EXFAT_HINT_NONE;
622 /* search the file name for directories */
623 dentry = exfat_find_dir_entry(sb, ei, &cdir, &uni_name,
624 num_entries, TYPE_ALL, &hint_opt);
627 return dentry; /* -error value */
630 info->entry = dentry;
631 info->num_subdirs = 0;
633 /* adjust cdir to the optimized value */
634 cdir.dir = hint_opt.clu;
635 if (cdir.flags & ALLOC_NO_FAT_CHAIN)
636 cdir.size -= dentry / sbi->dentries_per_clu;
637 dentry = hint_opt.eidx;
638 es = exfat_get_dentry_set(sb, &cdir, dentry, ES_2_ENTRIES);
641 ep = exfat_get_dentry_cached(es, 0);
642 ep2 = exfat_get_dentry_cached(es, 1);
644 info->type = exfat_get_entry_type(ep);
645 info->attr = le16_to_cpu(ep->dentry.file.attr);
646 info->size = le64_to_cpu(ep2->dentry.stream.valid_size);
647 if ((info->type == TYPE_FILE) && (info->size == 0)) {
648 info->flags = ALLOC_NO_FAT_CHAIN;
649 info->start_clu = EXFAT_EOF_CLUSTER;
651 info->flags = ep2->dentry.stream.flags;
653 le32_to_cpu(ep2->dentry.stream.start_clu);
656 exfat_get_entry_time(sbi, &info->crtime,
657 ep->dentry.file.create_tz,
658 ep->dentry.file.create_time,
659 ep->dentry.file.create_date,
660 ep->dentry.file.create_time_cs);
661 exfat_get_entry_time(sbi, &info->mtime,
662 ep->dentry.file.modify_tz,
663 ep->dentry.file.modify_time,
664 ep->dentry.file.modify_date,
665 ep->dentry.file.modify_time_cs);
666 exfat_get_entry_time(sbi, &info->atime,
667 ep->dentry.file.access_tz,
668 ep->dentry.file.access_time,
669 ep->dentry.file.access_date,
671 exfat_free_dentry_set(es, false);
673 if (ei->start_clu == EXFAT_FREE_CLUSTER) {
675 "non-zero size file starts with zero cluster (size : %llu, p_dir : %u, entry : 0x%08x)",
676 i_size_read(dir), ei->dir.dir, ei->entry);
680 if (info->type == TYPE_DIR) {
681 exfat_chain_set(&cdir, info->start_clu,
682 EXFAT_B_TO_CLU(info->size, sbi), info->flags);
683 count = exfat_count_dir_entries(sb, &cdir);
687 info->num_subdirs = count + EXFAT_MIN_SUBDIR;
692 static int exfat_d_anon_disconn(struct dentry *dentry)
694 return IS_ROOT(dentry) && (dentry->d_flags & DCACHE_DISCONNECTED);
697 static struct dentry *exfat_lookup(struct inode *dir, struct dentry *dentry,
700 struct super_block *sb = dir->i_sb;
702 struct dentry *alias;
703 struct exfat_dir_entry info;
708 mutex_lock(&EXFAT_SB(sb)->s_lock);
709 err = exfat_find(dir, &dentry->d_name, &info);
711 if (err == -ENOENT) {
718 i_pos = exfat_make_i_pos(&info);
719 inode = exfat_build_inode(sb, &info, i_pos);
720 err = PTR_ERR_OR_ZERO(inode);
724 i_mode = inode->i_mode;
725 alias = d_find_alias(inode);
728 * Checking "alias->d_parent == dentry->d_parent" to make sure
729 * FS is not corrupted (especially double linked dir).
731 if (alias && alias->d_parent == dentry->d_parent &&
732 !exfat_d_anon_disconn(alias)) {
735 * Unhashed alias is able to exist because of revalidate()
736 * called by lookup_fast. You can easily make this status
737 * by calling create and lookup concurrently
738 * In such case, we reuse an alias instead of new dentry
740 if (d_unhashed(alias)) {
741 WARN_ON(alias->d_name.hash_len !=
742 dentry->d_name.hash_len);
743 exfat_info(sb, "rehashed a dentry(%p) in read lookup",
747 } else if (!S_ISDIR(i_mode)) {
749 * This inode has non anonymous-DCACHE_DISCONNECTED
750 * dentry. This means, the user did ->lookup() by an
751 * another name (longname vs 8.3 alias of it) in past.
753 * Switch to new one for reason of locality if possible.
755 d_move(alias, dentry);
758 mutex_unlock(&EXFAT_SB(sb)->s_lock);
763 mutex_unlock(&EXFAT_SB(sb)->s_lock);
765 exfat_d_version_set(dentry, inode_query_iversion(dir));
767 return d_splice_alias(inode, dentry);
769 mutex_unlock(&EXFAT_SB(sb)->s_lock);
773 /* remove an entry, BUT don't truncate */
774 static int exfat_unlink(struct inode *dir, struct dentry *dentry)
776 struct exfat_chain cdir;
777 struct exfat_dentry *ep;
778 struct super_block *sb = dir->i_sb;
779 struct inode *inode = dentry->d_inode;
780 struct exfat_inode_info *ei = EXFAT_I(inode);
781 struct buffer_head *bh;
783 int num_entries, entry, err = 0;
785 mutex_lock(&EXFAT_SB(sb)->s_lock);
786 exfat_chain_dup(&cdir, &ei->dir);
788 if (ei->dir.dir == DIR_DELETED) {
789 exfat_err(sb, "abnormal access to deleted dentry");
794 ep = exfat_get_dentry(sb, &cdir, entry, &bh, §or);
799 num_entries = exfat_count_ext_entries(sb, &cdir, entry, ep);
800 if (num_entries < 0) {
808 exfat_set_volume_dirty(sb);
809 /* update the directory entry */
810 if (exfat_remove_entries(dir, &cdir, entry, 0, num_entries)) {
815 /* This doesn't modify ei */
816 ei->dir.dir = DIR_DELETED;
817 exfat_clear_volume_dirty(sb);
819 inode_inc_iversion(dir);
820 dir->i_mtime = dir->i_atime = current_time(dir);
821 exfat_truncate_atime(&dir->i_atime);
823 exfat_sync_inode(dir);
825 mark_inode_dirty(dir);
828 inode->i_mtime = inode->i_atime = current_time(inode);
829 exfat_truncate_atime(&inode->i_atime);
830 exfat_unhash_inode(inode);
831 exfat_d_version_set(dentry, inode_query_iversion(dir));
833 mutex_unlock(&EXFAT_SB(sb)->s_lock);
837 static int exfat_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
838 struct dentry *dentry, umode_t mode)
840 struct super_block *sb = dir->i_sb;
842 struct exfat_dir_entry info;
843 struct exfat_chain cdir;
847 mutex_lock(&EXFAT_SB(sb)->s_lock);
848 exfat_set_volume_dirty(sb);
849 err = exfat_add_entry(dir, dentry->d_name.name, &cdir, TYPE_DIR,
851 exfat_clear_volume_dirty(sb);
855 inode_inc_iversion(dir);
856 dir->i_ctime = dir->i_mtime = current_time(dir);
858 exfat_sync_inode(dir);
860 mark_inode_dirty(dir);
863 i_pos = exfat_make_i_pos(&info);
864 inode = exfat_build_inode(sb, &info, i_pos);
865 err = PTR_ERR_OR_ZERO(inode);
869 inode_inc_iversion(inode);
870 inode->i_mtime = inode->i_atime = inode->i_ctime =
871 EXFAT_I(inode)->i_crtime = current_time(inode);
872 exfat_truncate_atime(&inode->i_atime);
873 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
875 d_instantiate(dentry, inode);
878 mutex_unlock(&EXFAT_SB(sb)->s_lock);
882 static int exfat_check_dir_empty(struct super_block *sb,
883 struct exfat_chain *p_dir)
885 int i, dentries_per_clu;
887 struct exfat_chain clu;
888 struct exfat_dentry *ep;
889 struct exfat_sb_info *sbi = EXFAT_SB(sb);
890 struct buffer_head *bh;
892 dentries_per_clu = sbi->dentries_per_clu;
894 exfat_chain_dup(&clu, p_dir);
896 while (clu.dir != EXFAT_EOF_CLUSTER) {
897 for (i = 0; i < dentries_per_clu; i++) {
898 ep = exfat_get_dentry(sb, &clu, i, &bh, NULL);
901 type = exfat_get_entry_type(ep);
903 if (type == TYPE_UNUSED)
906 if (type != TYPE_FILE && type != TYPE_DIR)
912 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
916 clu.dir = EXFAT_EOF_CLUSTER;
918 if (exfat_get_next_cluster(sb, &(clu.dir)))
926 static int exfat_rmdir(struct inode *dir, struct dentry *dentry)
928 struct inode *inode = dentry->d_inode;
929 struct exfat_dentry *ep;
930 struct exfat_chain cdir, clu_to_free;
931 struct super_block *sb = inode->i_sb;
932 struct exfat_sb_info *sbi = EXFAT_SB(sb);
933 struct exfat_inode_info *ei = EXFAT_I(inode);
934 struct buffer_head *bh;
936 int num_entries, entry, err;
938 mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
940 exfat_chain_dup(&cdir, &ei->dir);
943 if (ei->dir.dir == DIR_DELETED) {
944 exfat_err(sb, "abnormal access to deleted dentry");
949 exfat_chain_set(&clu_to_free, ei->start_clu,
950 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi), ei->flags);
952 err = exfat_check_dir_empty(sb, &clu_to_free);
955 exfat_err(sb, "failed to exfat_check_dir_empty : err(%d)",
960 ep = exfat_get_dentry(sb, &cdir, entry, &bh, §or);
966 num_entries = exfat_count_ext_entries(sb, &cdir, entry, ep);
967 if (num_entries < 0) {
975 exfat_set_volume_dirty(sb);
976 err = exfat_remove_entries(dir, &cdir, entry, 0, num_entries);
978 exfat_err(sb, "failed to exfat_remove_entries : err(%d)", err);
981 ei->dir.dir = DIR_DELETED;
982 exfat_clear_volume_dirty(sb);
984 inode_inc_iversion(dir);
985 dir->i_mtime = dir->i_atime = current_time(dir);
986 exfat_truncate_atime(&dir->i_atime);
988 exfat_sync_inode(dir);
990 mark_inode_dirty(dir);
994 inode->i_mtime = inode->i_atime = current_time(inode);
995 exfat_truncate_atime(&inode->i_atime);
996 exfat_unhash_inode(inode);
997 exfat_d_version_set(dentry, inode_query_iversion(dir));
999 mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
1003 static int exfat_rename_file(struct inode *inode, struct exfat_chain *p_dir,
1004 int oldentry, struct exfat_uni_name *p_uniname,
1005 struct exfat_inode_info *ei)
1007 int ret, num_old_entries, num_new_entries;
1008 sector_t sector_old, sector_new;
1009 struct exfat_dentry *epold, *epnew;
1010 struct super_block *sb = inode->i_sb;
1011 struct buffer_head *new_bh, *old_bh;
1012 int sync = IS_DIRSYNC(inode);
1014 epold = exfat_get_dentry(sb, p_dir, oldentry, &old_bh, §or_old);
1018 num_old_entries = exfat_count_ext_entries(sb, p_dir, oldentry, epold);
1019 if (num_old_entries < 0)
1023 num_new_entries = exfat_calc_num_entries(p_uniname);
1024 if (num_new_entries < 0)
1025 return num_new_entries;
1027 if (num_old_entries < num_new_entries) {
1031 exfat_find_empty_entry(inode, p_dir, num_new_entries);
1033 return newentry; /* -EIO or -ENOSPC */
1035 epnew = exfat_get_dentry(sb, p_dir, newentry, &new_bh,
1041 if (exfat_get_entry_type(epnew) == TYPE_FILE) {
1042 epnew->dentry.file.attr |= cpu_to_le16(ATTR_ARCHIVE);
1043 ei->attr |= ATTR_ARCHIVE;
1045 exfat_update_bh(new_bh, sync);
1049 epold = exfat_get_dentry(sb, p_dir, oldentry + 1, &old_bh,
1053 epnew = exfat_get_dentry(sb, p_dir, newentry + 1, &new_bh,
1061 exfat_update_bh(new_bh, sync);
1065 ret = exfat_init_ext_entry(inode, p_dir, newentry,
1066 num_new_entries, p_uniname);
1070 exfat_remove_entries(inode, p_dir, oldentry, 0,
1072 ei->entry = newentry;
1074 if (exfat_get_entry_type(epold) == TYPE_FILE) {
1075 epold->dentry.file.attr |= cpu_to_le16(ATTR_ARCHIVE);
1076 ei->attr |= ATTR_ARCHIVE;
1078 exfat_update_bh(old_bh, sync);
1080 ret = exfat_init_ext_entry(inode, p_dir, oldentry,
1081 num_new_entries, p_uniname);
1085 exfat_remove_entries(inode, p_dir, oldentry, num_new_entries,
1091 static int exfat_move_file(struct inode *inode, struct exfat_chain *p_olddir,
1092 int oldentry, struct exfat_chain *p_newdir,
1093 struct exfat_uni_name *p_uniname, struct exfat_inode_info *ei)
1095 int ret, newentry, num_new_entries, num_old_entries;
1096 sector_t sector_mov, sector_new;
1097 struct exfat_dentry *epmov, *epnew;
1098 struct super_block *sb = inode->i_sb;
1099 struct buffer_head *mov_bh, *new_bh;
1101 epmov = exfat_get_dentry(sb, p_olddir, oldentry, &mov_bh, §or_mov);
1105 num_old_entries = exfat_count_ext_entries(sb, p_olddir, oldentry,
1107 if (num_old_entries < 0)
1111 num_new_entries = exfat_calc_num_entries(p_uniname);
1112 if (num_new_entries < 0)
1113 return num_new_entries;
1115 newentry = exfat_find_empty_entry(inode, p_newdir, num_new_entries);
1117 return newentry; /* -EIO or -ENOSPC */
1119 epnew = exfat_get_dentry(sb, p_newdir, newentry, &new_bh, §or_new);
1124 if (exfat_get_entry_type(epnew) == TYPE_FILE) {
1125 epnew->dentry.file.attr |= cpu_to_le16(ATTR_ARCHIVE);
1126 ei->attr |= ATTR_ARCHIVE;
1128 exfat_update_bh(new_bh, IS_DIRSYNC(inode));
1132 epmov = exfat_get_dentry(sb, p_olddir, oldentry + 1, &mov_bh,
1136 epnew = exfat_get_dentry(sb, p_newdir, newentry + 1, &new_bh,
1144 exfat_update_bh(new_bh, IS_DIRSYNC(inode));
1148 ret = exfat_init_ext_entry(inode, p_newdir, newentry, num_new_entries,
1153 exfat_remove_entries(inode, p_olddir, oldentry, 0, num_old_entries);
1155 exfat_chain_set(&ei->dir, p_newdir->dir, p_newdir->size,
1158 ei->entry = newentry;
1162 static void exfat_update_parent_info(struct exfat_inode_info *ei,
1163 struct inode *parent_inode)
1165 struct exfat_sb_info *sbi = EXFAT_SB(parent_inode->i_sb);
1166 struct exfat_inode_info *parent_ei = EXFAT_I(parent_inode);
1167 loff_t parent_isize = i_size_read(parent_inode);
1170 * the problem that struct exfat_inode_info caches wrong parent info.
1172 * because of flag-mismatch of ei->dir,
1173 * there is abnormal traversing cluster chain.
1175 if (unlikely(parent_ei->flags != ei->dir.flags ||
1176 parent_isize != EXFAT_CLU_TO_B(ei->dir.size, sbi) ||
1177 parent_ei->start_clu != ei->dir.dir)) {
1178 exfat_chain_set(&ei->dir, parent_ei->start_clu,
1179 EXFAT_B_TO_CLU_ROUND_UP(parent_isize, sbi),
1184 /* rename or move a old file into a new file */
1185 static int __exfat_rename(struct inode *old_parent_inode,
1186 struct exfat_inode_info *ei, struct inode *new_parent_inode,
1187 struct dentry *new_dentry)
1191 struct exfat_chain olddir, newdir;
1192 struct exfat_chain *p_dir = NULL;
1193 struct exfat_uni_name uni_name;
1194 struct exfat_dentry *ep;
1195 struct super_block *sb = old_parent_inode->i_sb;
1196 struct exfat_sb_info *sbi = EXFAT_SB(sb);
1197 const unsigned char *new_path = new_dentry->d_name.name;
1198 struct inode *new_inode = new_dentry->d_inode;
1200 struct exfat_inode_info *new_ei = NULL;
1201 unsigned int new_entry_type = TYPE_UNUSED;
1203 struct buffer_head *old_bh, *new_bh = NULL;
1205 /* check the validity of pointer parameters */
1206 if (new_path == NULL || strlen(new_path) == 0)
1209 if (ei->dir.dir == DIR_DELETED) {
1210 exfat_err(sb, "abnormal access to deleted source dentry");
1214 exfat_update_parent_info(ei, old_parent_inode);
1216 exfat_chain_dup(&olddir, &ei->dir);
1219 ep = exfat_get_dentry(sb, &olddir, dentry, &old_bh, NULL);
1226 /* check whether new dir is existing directory and empty */
1229 new_ei = EXFAT_I(new_inode);
1231 if (new_ei->dir.dir == DIR_DELETED) {
1232 exfat_err(sb, "abnormal access to deleted target dentry");
1236 exfat_update_parent_info(new_ei, new_parent_inode);
1238 p_dir = &(new_ei->dir);
1239 new_entry = new_ei->entry;
1240 ep = exfat_get_dentry(sb, p_dir, new_entry, &new_bh, NULL);
1244 new_entry_type = exfat_get_entry_type(ep);
1247 /* if new_inode exists, update ei */
1248 if (new_entry_type == TYPE_DIR) {
1249 struct exfat_chain new_clu;
1251 new_clu.dir = new_ei->start_clu;
1253 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode),
1255 new_clu.flags = new_ei->flags;
1257 ret = exfat_check_dir_empty(sb, &new_clu);
1263 /* check the validity of directory name in the given new pathname */
1264 ret = exfat_resolve_path(new_parent_inode, new_path, &newdir,
1269 exfat_set_volume_dirty(sb);
1271 if (olddir.dir == newdir.dir)
1272 ret = exfat_rename_file(new_parent_inode, &olddir, dentry,
1275 ret = exfat_move_file(new_parent_inode, &olddir, dentry,
1276 &newdir, &uni_name, ei);
1278 if (!ret && new_inode) {
1279 /* delete entries of new_dir */
1280 ep = exfat_get_dentry(sb, p_dir, new_entry, &new_bh, NULL);
1286 num_entries = exfat_count_ext_entries(sb, p_dir, new_entry, ep);
1287 if (num_entries < 0) {
1293 if (exfat_remove_entries(new_inode, p_dir, new_entry, 0,
1299 /* Free the clusters if new_inode is a dir(as if exfat_rmdir) */
1300 if (new_entry_type == TYPE_DIR) {
1301 /* new_ei, new_clu_to_free */
1302 struct exfat_chain new_clu_to_free;
1304 exfat_chain_set(&new_clu_to_free, new_ei->start_clu,
1305 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode),
1306 sbi), new_ei->flags);
1308 if (exfat_free_cluster(new_inode, &new_clu_to_free)) {
1309 /* just set I/O error only */
1313 i_size_write(new_inode, 0);
1314 new_ei->start_clu = EXFAT_EOF_CLUSTER;
1315 new_ei->flags = ALLOC_NO_FAT_CHAIN;
1318 /* Update new_inode ei
1319 * Prevent syncing removed new_inode
1320 * (new_ei is already initialized above code ("if (new_inode)")
1322 new_ei->dir.dir = DIR_DELETED;
1324 exfat_clear_volume_dirty(sb);
1329 static int exfat_rename(struct user_namespace *mnt_userns,
1330 struct inode *old_dir, struct dentry *old_dentry,
1331 struct inode *new_dir, struct dentry *new_dentry,
1334 struct inode *old_inode, *new_inode;
1335 struct super_block *sb = old_dir->i_sb;
1340 * The VFS already checks for existence, so for local filesystems
1341 * the RENAME_NOREPLACE implementation is equivalent to plain rename.
1342 * Don't support any other flags
1344 if (flags & ~RENAME_NOREPLACE)
1347 mutex_lock(&EXFAT_SB(sb)->s_lock);
1348 old_inode = old_dentry->d_inode;
1349 new_inode = new_dentry->d_inode;
1351 err = __exfat_rename(old_dir, EXFAT_I(old_inode), new_dir, new_dentry);
1355 inode_inc_iversion(new_dir);
1356 new_dir->i_ctime = new_dir->i_mtime = new_dir->i_atime =
1357 EXFAT_I(new_dir)->i_crtime = current_time(new_dir);
1358 exfat_truncate_atime(&new_dir->i_atime);
1359 if (IS_DIRSYNC(new_dir))
1360 exfat_sync_inode(new_dir);
1362 mark_inode_dirty(new_dir);
1364 i_pos = ((loff_t)EXFAT_I(old_inode)->dir.dir << 32) |
1365 (EXFAT_I(old_inode)->entry & 0xffffffff);
1366 exfat_unhash_inode(old_inode);
1367 exfat_hash_inode(old_inode, i_pos);
1368 if (IS_DIRSYNC(new_dir))
1369 exfat_sync_inode(old_inode);
1371 mark_inode_dirty(old_inode);
1373 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
1374 drop_nlink(old_dir);
1379 inode_inc_iversion(old_dir);
1380 old_dir->i_ctime = old_dir->i_mtime = current_time(old_dir);
1381 if (IS_DIRSYNC(old_dir))
1382 exfat_sync_inode(old_dir);
1384 mark_inode_dirty(old_dir);
1387 exfat_unhash_inode(new_inode);
1389 /* skip drop_nlink if new_inode already has been dropped */
1390 if (new_inode->i_nlink) {
1391 drop_nlink(new_inode);
1392 if (S_ISDIR(new_inode->i_mode))
1393 drop_nlink(new_inode);
1395 exfat_warn(sb, "abnormal access to an inode dropped");
1396 WARN_ON(new_inode->i_nlink == 0);
1398 new_inode->i_ctime = EXFAT_I(new_inode)->i_crtime =
1399 current_time(new_inode);
1403 mutex_unlock(&EXFAT_SB(sb)->s_lock);
1407 const struct inode_operations exfat_dir_inode_operations = {
1408 .create = exfat_create,
1409 .lookup = exfat_lookup,
1410 .unlink = exfat_unlink,
1411 .mkdir = exfat_mkdir,
1412 .rmdir = exfat_rmdir,
1413 .rename = exfat_rename,
1414 .setattr = exfat_setattr,
1415 .getattr = exfat_getattr,