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))
334 * Allocate new cluster to this directory
336 if (ei->start_clu != EXFAT_EOF_CLUSTER) {
337 /* we trust p_dir->size regardless of FAT type */
338 if (exfat_find_last_cluster(sb, p_dir, &last_clu))
341 exfat_chain_set(&clu, last_clu + 1, 0, p_dir->flags);
343 /* This directory is empty */
344 exfat_chain_set(&clu, EXFAT_EOF_CLUSTER, 0,
348 /* allocate a cluster */
349 ret = exfat_alloc_cluster(inode, 1, &clu);
353 if (exfat_zeroed_cluster(inode, clu.dir))
356 if (ei->start_clu == EXFAT_EOF_CLUSTER) {
357 ei->start_clu = clu.dir;
358 p_dir->dir = clu.dir;
361 /* append to the FAT chain */
362 if (clu.flags != p_dir->flags) {
363 /* no-fat-chain bit is disabled,
364 * so fat-chain should be synced with alloc-bitmap
366 exfat_chain_cont_cluster(sb, p_dir->dir, p_dir->size);
367 p_dir->flags = ALLOC_FAT_CHAIN;
368 hint_femp.cur.flags = ALLOC_FAT_CHAIN;
371 if (clu.flags == ALLOC_FAT_CHAIN)
372 if (exfat_ent_set(sb, last_clu, clu.dir))
375 if (hint_femp.eidx == EXFAT_HINT_NONE) {
376 /* the special case that new dentry
377 * should be allocated from the start of new cluster
379 hint_femp.eidx = EXFAT_B_TO_DEN_IDX(p_dir->size, sbi);
380 hint_femp.count = sbi->dentries_per_clu;
382 exfat_chain_set(&hint_femp.cur, clu.dir, 0, clu.flags);
384 hint_femp.cur.size++;
386 size = EXFAT_CLU_TO_B(p_dir->size, sbi);
388 /* update the directory entry */
389 if (p_dir->dir != sbi->root_dir) {
390 struct buffer_head *bh;
392 ep = exfat_get_dentry(sb,
393 &(ei->dir), ei->entry + 1, &bh, §or);
397 ep->dentry.stream.valid_size = cpu_to_le64(size);
398 ep->dentry.stream.size = ep->dentry.stream.valid_size;
399 ep->dentry.stream.flags = p_dir->flags;
400 exfat_update_bh(bh, IS_DIRSYNC(inode));
402 if (exfat_update_dir_chksum(inode, &(ei->dir),
407 /* directory inode should be updated in here */
408 i_size_write(inode, size);
409 ei->i_size_ondisk += sbi->cluster_size;
410 ei->i_size_aligned += sbi->cluster_size;
411 ei->flags = p_dir->flags;
412 inode->i_blocks += sbi->cluster_size >> 9;
419 * Name Resolution Functions :
420 * Zero if it was successful; otherwise nonzero.
422 static int __exfat_resolve_path(struct inode *inode, const unsigned char *path,
423 struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
427 int lossy = NLS_NAME_NO_LOSSY;
428 struct super_block *sb = inode->i_sb;
429 struct exfat_sb_info *sbi = EXFAT_SB(sb);
430 struct exfat_inode_info *ei = EXFAT_I(inode);
432 /* strip all trailing periods */
433 namelen = exfat_striptail_len(strlen(path), path);
437 if (strlen(path) > (MAX_NAME_LENGTH * MAX_CHARSET_SIZE))
438 return -ENAMETOOLONG;
441 * strip all leading spaces :
442 * "MS windows 7" supports leading spaces.
443 * So we should skip this preprocessing for compatibility.
446 /* file name conversion :
447 * If lookup case, we allow bad-name for compatibility.
449 namelen = exfat_nls_to_utf16(sb, path, namelen, p_uniname,
452 return namelen; /* return error value */
454 if ((lossy && !lookup) || !namelen)
457 exfat_chain_set(p_dir, ei->start_clu,
458 EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags);
463 static inline int exfat_resolve_path(struct inode *inode,
464 const unsigned char *path, struct exfat_chain *dir,
465 struct exfat_uni_name *uni)
467 return __exfat_resolve_path(inode, path, dir, uni, 0);
470 static inline int exfat_resolve_path_for_lookup(struct inode *inode,
471 const unsigned char *path, struct exfat_chain *dir,
472 struct exfat_uni_name *uni)
474 return __exfat_resolve_path(inode, path, dir, uni, 1);
477 static inline loff_t exfat_make_i_pos(struct exfat_dir_entry *info)
479 return ((loff_t) info->dir.dir << 32) | (info->entry & 0xffffffff);
482 static int exfat_add_entry(struct inode *inode, const char *path,
483 struct exfat_chain *p_dir, unsigned int type,
484 struct exfat_dir_entry *info)
486 int ret, dentry, num_entries;
487 struct super_block *sb = inode->i_sb;
488 struct exfat_sb_info *sbi = EXFAT_SB(sb);
489 struct exfat_uni_name uniname;
490 struct exfat_chain clu;
492 unsigned int start_clu = EXFAT_FREE_CLUSTER;
494 ret = exfat_resolve_path(inode, path, p_dir, &uniname);
498 num_entries = exfat_calc_num_entries(&uniname);
499 if (num_entries < 0) {
504 /* exfat_find_empty_entry must be called before alloc_cluster() */
505 dentry = exfat_find_empty_entry(inode, p_dir, num_entries);
507 ret = dentry; /* -EIO or -ENOSPC */
511 if (type == TYPE_DIR) {
512 ret = exfat_alloc_new_dir(inode, &clu);
516 clu_size = sbi->cluster_size;
519 /* update the directory entry */
520 /* fill the dos name directory entry information of the created file.
521 * the first cluster is not determined yet. (0)
523 ret = exfat_init_dir_entry(inode, p_dir, dentry, type,
524 start_clu, clu_size);
528 ret = exfat_init_ext_entry(inode, p_dir, dentry, num_entries, &uniname);
533 info->entry = dentry;
534 info->flags = ALLOC_NO_FAT_CHAIN;
537 if (type == TYPE_FILE) {
538 info->attr = ATTR_ARCHIVE;
539 info->start_clu = EXFAT_EOF_CLUSTER;
541 info->num_subdirs = 0;
543 info->attr = ATTR_SUBDIR;
544 info->start_clu = start_clu;
545 info->size = clu_size;
546 info->num_subdirs = EXFAT_MIN_SUBDIR;
548 memset(&info->crtime, 0, sizeof(info->crtime));
549 memset(&info->mtime, 0, sizeof(info->mtime));
550 memset(&info->atime, 0, sizeof(info->atime));
555 static int exfat_create(struct inode *dir, struct dentry *dentry, umode_t mode,
558 struct super_block *sb = dir->i_sb;
560 struct exfat_chain cdir;
561 struct exfat_dir_entry info;
565 mutex_lock(&EXFAT_SB(sb)->s_lock);
566 exfat_set_volume_dirty(sb);
567 err = exfat_add_entry(dir, dentry->d_name.name, &cdir, TYPE_FILE,
569 exfat_clear_volume_dirty(sb);
573 inode_inc_iversion(dir);
574 dir->i_ctime = dir->i_mtime = current_time(dir);
576 exfat_sync_inode(dir);
578 mark_inode_dirty(dir);
580 i_pos = exfat_make_i_pos(&info);
581 inode = exfat_build_inode(sb, &info, i_pos);
582 err = PTR_ERR_OR_ZERO(inode);
586 inode_inc_iversion(inode);
587 inode->i_mtime = inode->i_atime = inode->i_ctime =
588 EXFAT_I(inode)->i_crtime = current_time(inode);
589 exfat_truncate_atime(&inode->i_atime);
590 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
592 d_instantiate(dentry, inode);
594 mutex_unlock(&EXFAT_SB(sb)->s_lock);
599 static int exfat_find(struct inode *dir, struct qstr *qname,
600 struct exfat_dir_entry *info)
602 int ret, dentry, num_entries, count;
603 struct exfat_chain cdir;
604 struct exfat_uni_name uni_name;
605 struct super_block *sb = dir->i_sb;
606 struct exfat_sb_info *sbi = EXFAT_SB(sb);
607 struct exfat_inode_info *ei = EXFAT_I(dir);
608 struct exfat_dentry *ep, *ep2;
609 struct exfat_entry_set_cache *es;
610 /* for optimized dir & entry to prevent long traverse of cluster chain */
611 struct exfat_hint hint_opt;
616 /* check the validity of directory name in the given pathname */
617 ret = exfat_resolve_path_for_lookup(dir, qname->name, &cdir, &uni_name);
621 num_entries = exfat_calc_num_entries(&uni_name);
625 /* check the validation of hint_stat and initialize it if required */
626 if (ei->version != (inode_peek_iversion_raw(dir) & 0xffffffff)) {
627 ei->hint_stat.clu = cdir.dir;
628 ei->hint_stat.eidx = 0;
629 ei->version = (inode_peek_iversion_raw(dir) & 0xffffffff);
630 ei->hint_femp.eidx = EXFAT_HINT_NONE;
633 /* search the file name for directories */
634 dentry = exfat_find_dir_entry(sb, ei, &cdir, &uni_name,
635 num_entries, TYPE_ALL, &hint_opt);
638 return dentry; /* -error value */
641 info->entry = dentry;
642 info->num_subdirs = 0;
644 /* adjust cdir to the optimized value */
645 cdir.dir = hint_opt.clu;
646 if (cdir.flags & ALLOC_NO_FAT_CHAIN)
647 cdir.size -= dentry / sbi->dentries_per_clu;
648 dentry = hint_opt.eidx;
649 es = exfat_get_dentry_set(sb, &cdir, dentry, ES_2_ENTRIES);
652 ep = exfat_get_dentry_cached(es, 0);
653 ep2 = exfat_get_dentry_cached(es, 1);
655 info->type = exfat_get_entry_type(ep);
656 info->attr = le16_to_cpu(ep->dentry.file.attr);
657 info->size = le64_to_cpu(ep2->dentry.stream.valid_size);
658 if (info->size == 0) {
659 info->flags = ALLOC_NO_FAT_CHAIN;
660 info->start_clu = EXFAT_EOF_CLUSTER;
662 info->flags = ep2->dentry.stream.flags;
664 le32_to_cpu(ep2->dentry.stream.start_clu);
667 exfat_get_entry_time(sbi, &info->crtime,
668 ep->dentry.file.create_tz,
669 ep->dentry.file.create_time,
670 ep->dentry.file.create_date,
671 ep->dentry.file.create_time_cs);
672 exfat_get_entry_time(sbi, &info->mtime,
673 ep->dentry.file.modify_tz,
674 ep->dentry.file.modify_time,
675 ep->dentry.file.modify_date,
676 ep->dentry.file.modify_time_cs);
677 exfat_get_entry_time(sbi, &info->atime,
678 ep->dentry.file.access_tz,
679 ep->dentry.file.access_time,
680 ep->dentry.file.access_date,
682 exfat_free_dentry_set(es, false);
684 if (ei->start_clu == EXFAT_FREE_CLUSTER) {
686 "non-zero size file starts with zero cluster (size : %llu, p_dir : %u, entry : 0x%08x)",
687 i_size_read(dir), ei->dir.dir, ei->entry);
691 if (info->type == TYPE_DIR) {
692 exfat_chain_set(&cdir, info->start_clu,
693 EXFAT_B_TO_CLU(info->size, sbi), info->flags);
694 count = exfat_count_dir_entries(sb, &cdir);
698 info->num_subdirs = count + EXFAT_MIN_SUBDIR;
703 static int exfat_d_anon_disconn(struct dentry *dentry)
705 return IS_ROOT(dentry) && (dentry->d_flags & DCACHE_DISCONNECTED);
708 static struct dentry *exfat_lookup(struct inode *dir, struct dentry *dentry,
711 struct super_block *sb = dir->i_sb;
713 struct dentry *alias;
714 struct exfat_dir_entry info;
719 mutex_lock(&EXFAT_SB(sb)->s_lock);
720 err = exfat_find(dir, &dentry->d_name, &info);
722 if (err == -ENOENT) {
729 i_pos = exfat_make_i_pos(&info);
730 inode = exfat_build_inode(sb, &info, i_pos);
731 err = PTR_ERR_OR_ZERO(inode);
735 i_mode = inode->i_mode;
736 alias = d_find_alias(inode);
739 * Checking "alias->d_parent == dentry->d_parent" to make sure
740 * FS is not corrupted (especially double linked dir).
742 if (alias && alias->d_parent == dentry->d_parent &&
743 !exfat_d_anon_disconn(alias)) {
746 * Unhashed alias is able to exist because of revalidate()
747 * called by lookup_fast. You can easily make this status
748 * by calling create and lookup concurrently
749 * In such case, we reuse an alias instead of new dentry
751 if (d_unhashed(alias)) {
752 WARN_ON(alias->d_name.hash_len !=
753 dentry->d_name.hash_len);
754 exfat_info(sb, "rehashed a dentry(%p) in read lookup",
758 } else if (!S_ISDIR(i_mode)) {
760 * This inode has non anonymous-DCACHE_DISCONNECTED
761 * dentry. This means, the user did ->lookup() by an
762 * another name (longname vs 8.3 alias of it) in past.
764 * Switch to new one for reason of locality if possible.
766 d_move(alias, dentry);
769 mutex_unlock(&EXFAT_SB(sb)->s_lock);
774 mutex_unlock(&EXFAT_SB(sb)->s_lock);
776 exfat_d_version_set(dentry, inode_query_iversion(dir));
778 return d_splice_alias(inode, dentry);
780 mutex_unlock(&EXFAT_SB(sb)->s_lock);
784 /* remove an entry, BUT don't truncate */
785 static int exfat_unlink(struct inode *dir, struct dentry *dentry)
787 struct exfat_chain cdir;
788 struct exfat_dentry *ep;
789 struct super_block *sb = dir->i_sb;
790 struct inode *inode = dentry->d_inode;
791 struct exfat_inode_info *ei = EXFAT_I(inode);
792 struct buffer_head *bh;
794 int num_entries, entry, err = 0;
796 mutex_lock(&EXFAT_SB(sb)->s_lock);
797 exfat_chain_dup(&cdir, &ei->dir);
799 if (ei->dir.dir == DIR_DELETED) {
800 exfat_err(sb, "abnormal access to deleted dentry");
805 ep = exfat_get_dentry(sb, &cdir, entry, &bh, §or);
810 num_entries = exfat_count_ext_entries(sb, &cdir, entry, ep);
811 if (num_entries < 0) {
819 exfat_set_volume_dirty(sb);
820 /* update the directory entry */
821 if (exfat_remove_entries(dir, &cdir, entry, 0, num_entries)) {
826 /* This doesn't modify ei */
827 ei->dir.dir = DIR_DELETED;
828 exfat_clear_volume_dirty(sb);
830 inode_inc_iversion(dir);
831 dir->i_mtime = dir->i_atime = current_time(dir);
832 exfat_truncate_atime(&dir->i_atime);
834 exfat_sync_inode(dir);
836 mark_inode_dirty(dir);
839 inode->i_mtime = inode->i_atime = current_time(inode);
840 exfat_truncate_atime(&inode->i_atime);
841 exfat_unhash_inode(inode);
842 exfat_d_version_set(dentry, inode_query_iversion(dir));
844 mutex_unlock(&EXFAT_SB(sb)->s_lock);
848 static int exfat_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
850 struct super_block *sb = dir->i_sb;
852 struct exfat_dir_entry info;
853 struct exfat_chain cdir;
857 mutex_lock(&EXFAT_SB(sb)->s_lock);
858 exfat_set_volume_dirty(sb);
859 err = exfat_add_entry(dir, dentry->d_name.name, &cdir, TYPE_DIR,
861 exfat_clear_volume_dirty(sb);
865 inode_inc_iversion(dir);
866 dir->i_ctime = dir->i_mtime = current_time(dir);
868 exfat_sync_inode(dir);
870 mark_inode_dirty(dir);
873 i_pos = exfat_make_i_pos(&info);
874 inode = exfat_build_inode(sb, &info, i_pos);
875 err = PTR_ERR_OR_ZERO(inode);
879 inode_inc_iversion(inode);
880 inode->i_mtime = inode->i_atime = inode->i_ctime =
881 EXFAT_I(inode)->i_crtime = current_time(inode);
882 exfat_truncate_atime(&inode->i_atime);
883 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
885 d_instantiate(dentry, inode);
888 mutex_unlock(&EXFAT_SB(sb)->s_lock);
892 static int exfat_check_dir_empty(struct super_block *sb,
893 struct exfat_chain *p_dir)
895 int i, dentries_per_clu;
897 struct exfat_chain clu;
898 struct exfat_dentry *ep;
899 struct exfat_sb_info *sbi = EXFAT_SB(sb);
900 struct buffer_head *bh;
902 dentries_per_clu = sbi->dentries_per_clu;
904 if (p_dir->dir == EXFAT_EOF_CLUSTER)
907 exfat_chain_dup(&clu, p_dir);
909 while (clu.dir != EXFAT_EOF_CLUSTER) {
910 for (i = 0; i < dentries_per_clu; i++) {
911 ep = exfat_get_dentry(sb, &clu, i, &bh, NULL);
914 type = exfat_get_entry_type(ep);
916 if (type == TYPE_UNUSED)
919 if (type != TYPE_FILE && type != TYPE_DIR)
925 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
929 clu.dir = EXFAT_EOF_CLUSTER;
931 if (exfat_get_next_cluster(sb, &(clu.dir)))
939 static int exfat_rmdir(struct inode *dir, struct dentry *dentry)
941 struct inode *inode = dentry->d_inode;
942 struct exfat_dentry *ep;
943 struct exfat_chain cdir, clu_to_free;
944 struct super_block *sb = inode->i_sb;
945 struct exfat_sb_info *sbi = EXFAT_SB(sb);
946 struct exfat_inode_info *ei = EXFAT_I(inode);
947 struct buffer_head *bh;
949 int num_entries, entry, err;
951 mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
953 exfat_chain_dup(&cdir, &ei->dir);
956 if (ei->dir.dir == DIR_DELETED) {
957 exfat_err(sb, "abnormal access to deleted dentry");
962 exfat_chain_set(&clu_to_free, ei->start_clu,
963 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi), ei->flags);
965 err = exfat_check_dir_empty(sb, &clu_to_free);
968 exfat_err(sb, "failed to exfat_check_dir_empty : err(%d)",
973 ep = exfat_get_dentry(sb, &cdir, entry, &bh, §or);
979 num_entries = exfat_count_ext_entries(sb, &cdir, entry, ep);
980 if (num_entries < 0) {
988 exfat_set_volume_dirty(sb);
989 err = exfat_remove_entries(dir, &cdir, entry, 0, num_entries);
991 exfat_err(sb, "failed to exfat_remove_entries : err(%d)", err);
994 ei->dir.dir = DIR_DELETED;
995 exfat_clear_volume_dirty(sb);
997 inode_inc_iversion(dir);
998 dir->i_mtime = dir->i_atime = current_time(dir);
999 exfat_truncate_atime(&dir->i_atime);
1000 if (IS_DIRSYNC(dir))
1001 exfat_sync_inode(dir);
1003 mark_inode_dirty(dir);
1007 inode->i_mtime = inode->i_atime = current_time(inode);
1008 exfat_truncate_atime(&inode->i_atime);
1009 exfat_unhash_inode(inode);
1010 exfat_d_version_set(dentry, inode_query_iversion(dir));
1012 mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
1016 static int exfat_rename_file(struct inode *inode, struct exfat_chain *p_dir,
1017 int oldentry, struct exfat_uni_name *p_uniname,
1018 struct exfat_inode_info *ei)
1020 int ret, num_old_entries, num_new_entries;
1021 sector_t sector_old, sector_new;
1022 struct exfat_dentry *epold, *epnew;
1023 struct super_block *sb = inode->i_sb;
1024 struct buffer_head *new_bh, *old_bh;
1025 int sync = IS_DIRSYNC(inode);
1027 epold = exfat_get_dentry(sb, p_dir, oldentry, &old_bh, §or_old);
1031 num_old_entries = exfat_count_ext_entries(sb, p_dir, oldentry, epold);
1032 if (num_old_entries < 0)
1036 num_new_entries = exfat_calc_num_entries(p_uniname);
1037 if (num_new_entries < 0)
1038 return num_new_entries;
1040 if (num_old_entries < num_new_entries) {
1044 exfat_find_empty_entry(inode, p_dir, num_new_entries);
1046 return newentry; /* -EIO or -ENOSPC */
1048 epnew = exfat_get_dentry(sb, p_dir, newentry, &new_bh,
1054 if (exfat_get_entry_type(epnew) == TYPE_FILE) {
1055 epnew->dentry.file.attr |= cpu_to_le16(ATTR_ARCHIVE);
1056 ei->attr |= ATTR_ARCHIVE;
1058 exfat_update_bh(new_bh, sync);
1062 epold = exfat_get_dentry(sb, p_dir, oldentry + 1, &old_bh,
1066 epnew = exfat_get_dentry(sb, p_dir, newentry + 1, &new_bh,
1074 exfat_update_bh(new_bh, sync);
1078 ret = exfat_init_ext_entry(inode, p_dir, newentry,
1079 num_new_entries, p_uniname);
1083 exfat_remove_entries(inode, p_dir, oldentry, 0,
1085 ei->entry = newentry;
1087 if (exfat_get_entry_type(epold) == TYPE_FILE) {
1088 epold->dentry.file.attr |= cpu_to_le16(ATTR_ARCHIVE);
1089 ei->attr |= ATTR_ARCHIVE;
1091 exfat_update_bh(old_bh, sync);
1093 ret = exfat_init_ext_entry(inode, p_dir, oldentry,
1094 num_new_entries, p_uniname);
1098 exfat_remove_entries(inode, p_dir, oldentry, num_new_entries,
1104 static int exfat_move_file(struct inode *inode, struct exfat_chain *p_olddir,
1105 int oldentry, struct exfat_chain *p_newdir,
1106 struct exfat_uni_name *p_uniname, struct exfat_inode_info *ei)
1108 int ret, newentry, num_new_entries, num_old_entries;
1109 sector_t sector_mov, sector_new;
1110 struct exfat_dentry *epmov, *epnew;
1111 struct super_block *sb = inode->i_sb;
1112 struct buffer_head *mov_bh, *new_bh;
1114 epmov = exfat_get_dentry(sb, p_olddir, oldentry, &mov_bh, §or_mov);
1118 num_old_entries = exfat_count_ext_entries(sb, p_olddir, oldentry,
1120 if (num_old_entries < 0)
1124 num_new_entries = exfat_calc_num_entries(p_uniname);
1125 if (num_new_entries < 0)
1126 return num_new_entries;
1128 newentry = exfat_find_empty_entry(inode, p_newdir, num_new_entries);
1130 return newentry; /* -EIO or -ENOSPC */
1132 epnew = exfat_get_dentry(sb, p_newdir, newentry, &new_bh, §or_new);
1137 if (exfat_get_entry_type(epnew) == TYPE_FILE) {
1138 epnew->dentry.file.attr |= cpu_to_le16(ATTR_ARCHIVE);
1139 ei->attr |= ATTR_ARCHIVE;
1141 exfat_update_bh(new_bh, IS_DIRSYNC(inode));
1145 epmov = exfat_get_dentry(sb, p_olddir, oldentry + 1, &mov_bh,
1149 epnew = exfat_get_dentry(sb, p_newdir, newentry + 1, &new_bh,
1157 exfat_update_bh(new_bh, IS_DIRSYNC(inode));
1161 ret = exfat_init_ext_entry(inode, p_newdir, newentry, num_new_entries,
1166 exfat_remove_entries(inode, p_olddir, oldentry, 0, num_old_entries);
1168 exfat_chain_set(&ei->dir, p_newdir->dir, p_newdir->size,
1171 ei->entry = newentry;
1175 static void exfat_update_parent_info(struct exfat_inode_info *ei,
1176 struct inode *parent_inode)
1178 struct exfat_sb_info *sbi = EXFAT_SB(parent_inode->i_sb);
1179 struct exfat_inode_info *parent_ei = EXFAT_I(parent_inode);
1180 loff_t parent_isize = i_size_read(parent_inode);
1183 * the problem that struct exfat_inode_info caches wrong parent info.
1185 * because of flag-mismatch of ei->dir,
1186 * there is abnormal traversing cluster chain.
1188 if (unlikely(parent_ei->flags != ei->dir.flags ||
1189 parent_isize != EXFAT_CLU_TO_B(ei->dir.size, sbi) ||
1190 parent_ei->start_clu != ei->dir.dir)) {
1191 exfat_chain_set(&ei->dir, parent_ei->start_clu,
1192 EXFAT_B_TO_CLU_ROUND_UP(parent_isize, sbi),
1197 /* rename or move a old file into a new file */
1198 static int __exfat_rename(struct inode *old_parent_inode,
1199 struct exfat_inode_info *ei, struct inode *new_parent_inode,
1200 struct dentry *new_dentry)
1204 struct exfat_chain olddir, newdir;
1205 struct exfat_chain *p_dir = NULL;
1206 struct exfat_uni_name uni_name;
1207 struct exfat_dentry *ep;
1208 struct super_block *sb = old_parent_inode->i_sb;
1209 struct exfat_sb_info *sbi = EXFAT_SB(sb);
1210 const unsigned char *new_path = new_dentry->d_name.name;
1211 struct inode *new_inode = new_dentry->d_inode;
1213 struct exfat_inode_info *new_ei = NULL;
1214 unsigned int new_entry_type = TYPE_UNUSED;
1216 struct buffer_head *old_bh, *new_bh = NULL;
1218 /* check the validity of pointer parameters */
1219 if (new_path == NULL || strlen(new_path) == 0)
1222 if (ei->dir.dir == DIR_DELETED) {
1223 exfat_err(sb, "abnormal access to deleted source dentry");
1227 exfat_update_parent_info(ei, old_parent_inode);
1229 exfat_chain_dup(&olddir, &ei->dir);
1232 ep = exfat_get_dentry(sb, &olddir, dentry, &old_bh, NULL);
1239 /* check whether new dir is existing directory and empty */
1242 new_ei = EXFAT_I(new_inode);
1244 if (new_ei->dir.dir == DIR_DELETED) {
1245 exfat_err(sb, "abnormal access to deleted target dentry");
1249 exfat_update_parent_info(new_ei, new_parent_inode);
1251 p_dir = &(new_ei->dir);
1252 new_entry = new_ei->entry;
1253 ep = exfat_get_dentry(sb, p_dir, new_entry, &new_bh, NULL);
1257 new_entry_type = exfat_get_entry_type(ep);
1260 /* if new_inode exists, update ei */
1261 if (new_entry_type == TYPE_DIR) {
1262 struct exfat_chain new_clu;
1264 new_clu.dir = new_ei->start_clu;
1266 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode),
1268 new_clu.flags = new_ei->flags;
1270 ret = exfat_check_dir_empty(sb, &new_clu);
1276 /* check the validity of directory name in the given new pathname */
1277 ret = exfat_resolve_path(new_parent_inode, new_path, &newdir,
1282 exfat_set_volume_dirty(sb);
1284 if (olddir.dir == newdir.dir)
1285 ret = exfat_rename_file(new_parent_inode, &olddir, dentry,
1288 ret = exfat_move_file(new_parent_inode, &olddir, dentry,
1289 &newdir, &uni_name, ei);
1291 if (!ret && new_inode) {
1292 /* delete entries of new_dir */
1293 ep = exfat_get_dentry(sb, p_dir, new_entry, &new_bh, NULL);
1299 num_entries = exfat_count_ext_entries(sb, p_dir, new_entry, ep);
1300 if (num_entries < 0) {
1306 if (exfat_remove_entries(new_inode, p_dir, new_entry, 0,
1312 /* Free the clusters if new_inode is a dir(as if exfat_rmdir) */
1313 if (new_entry_type == TYPE_DIR &&
1314 new_ei->start_clu != EXFAT_EOF_CLUSTER) {
1315 /* new_ei, new_clu_to_free */
1316 struct exfat_chain new_clu_to_free;
1318 exfat_chain_set(&new_clu_to_free, new_ei->start_clu,
1319 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode),
1320 sbi), new_ei->flags);
1322 if (exfat_free_cluster(new_inode, &new_clu_to_free)) {
1323 /* just set I/O error only */
1327 i_size_write(new_inode, 0);
1328 new_ei->start_clu = EXFAT_EOF_CLUSTER;
1329 new_ei->flags = ALLOC_NO_FAT_CHAIN;
1332 /* Update new_inode ei
1333 * Prevent syncing removed new_inode
1334 * (new_ei is already initialized above code ("if (new_inode)")
1336 new_ei->dir.dir = DIR_DELETED;
1338 exfat_clear_volume_dirty(sb);
1343 static int exfat_rename(struct inode *old_dir, struct dentry *old_dentry,
1344 struct inode *new_dir, struct dentry *new_dentry,
1347 struct inode *old_inode, *new_inode;
1348 struct super_block *sb = old_dir->i_sb;
1353 * The VFS already checks for existence, so for local filesystems
1354 * the RENAME_NOREPLACE implementation is equivalent to plain rename.
1355 * Don't support any other flags
1357 if (flags & ~RENAME_NOREPLACE)
1360 mutex_lock(&EXFAT_SB(sb)->s_lock);
1361 old_inode = old_dentry->d_inode;
1362 new_inode = new_dentry->d_inode;
1364 err = __exfat_rename(old_dir, EXFAT_I(old_inode), new_dir, new_dentry);
1368 inode_inc_iversion(new_dir);
1369 new_dir->i_ctime = new_dir->i_mtime = new_dir->i_atime =
1370 EXFAT_I(new_dir)->i_crtime = current_time(new_dir);
1371 exfat_truncate_atime(&new_dir->i_atime);
1372 if (IS_DIRSYNC(new_dir))
1373 exfat_sync_inode(new_dir);
1375 mark_inode_dirty(new_dir);
1377 i_pos = ((loff_t)EXFAT_I(old_inode)->dir.dir << 32) |
1378 (EXFAT_I(old_inode)->entry & 0xffffffff);
1379 exfat_unhash_inode(old_inode);
1380 exfat_hash_inode(old_inode, i_pos);
1381 if (IS_DIRSYNC(new_dir))
1382 exfat_sync_inode(old_inode);
1384 mark_inode_dirty(old_inode);
1386 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
1387 drop_nlink(old_dir);
1392 inode_inc_iversion(old_dir);
1393 old_dir->i_ctime = old_dir->i_mtime = current_time(old_dir);
1394 if (IS_DIRSYNC(old_dir))
1395 exfat_sync_inode(old_dir);
1397 mark_inode_dirty(old_dir);
1400 exfat_unhash_inode(new_inode);
1402 /* skip drop_nlink if new_inode already has been dropped */
1403 if (new_inode->i_nlink) {
1404 drop_nlink(new_inode);
1405 if (S_ISDIR(new_inode->i_mode))
1406 drop_nlink(new_inode);
1408 exfat_warn(sb, "abnormal access to an inode dropped");
1409 WARN_ON(new_inode->i_nlink == 0);
1411 new_inode->i_ctime = EXFAT_I(new_inode)->i_crtime =
1412 current_time(new_inode);
1416 mutex_unlock(&EXFAT_SB(sb)->s_lock);
1420 const struct inode_operations exfat_dir_inode_operations = {
1421 .create = exfat_create,
1422 .lookup = exfat_lookup,
1423 .unlink = exfat_unlink,
1424 .mkdir = exfat_mkdir,
1425 .rmdir = exfat_rmdir,
1426 .rename = exfat_rename,
1427 .setattr = exfat_setattr,
1428 .getattr = exfat_getattr,