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 if necessary */
69 static unsigned int exfat_striptail_len(unsigned int len, const char *name,
72 if (!keep_last_dots) {
73 while (len && name[len - 1] == '.')
80 * Compute the hash for the exfat name corresponding to the dentry. If the name
81 * is invalid, we leave the hash code unchanged so that the existing dentry can
82 * be used. The exfat fs routines will return ENOENT or EINVAL as appropriate.
84 static int exfat_d_hash(const struct dentry *dentry, struct qstr *qstr)
86 struct super_block *sb = dentry->d_sb;
87 struct nls_table *t = EXFAT_SB(sb)->nls_io;
88 const unsigned char *name = qstr->name;
89 unsigned int len = exfat_striptail_len(qstr->len, qstr->name,
90 EXFAT_SB(sb)->options.keep_last_dots);
91 unsigned long hash = init_name_hash(dentry);
95 for (i = 0; i < len; i += charlen) {
96 charlen = t->char2uni(&name[i], len - i, &c);
99 hash = partial_name_hash(exfat_toupper(sb, c), hash);
102 qstr->hash = end_name_hash(hash);
106 static int exfat_d_cmp(const struct dentry *dentry, unsigned int len,
107 const char *str, const struct qstr *name)
109 struct super_block *sb = dentry->d_sb;
110 struct nls_table *t = EXFAT_SB(sb)->nls_io;
111 unsigned int alen = exfat_striptail_len(name->len, name->name,
112 EXFAT_SB(sb)->options.keep_last_dots);
113 unsigned int blen = exfat_striptail_len(len, str,
114 EXFAT_SB(sb)->options.keep_last_dots);
121 for (i = 0; i < len; i += charlen) {
122 charlen = t->char2uni(&name->name[i], alen - i, &c1);
125 if (charlen != t->char2uni(&str[i], blen - i, &c2))
128 if (exfat_toupper(sb, c1) != exfat_toupper(sb, c2))
135 const struct dentry_operations exfat_dentry_ops = {
136 .d_revalidate = exfat_d_revalidate,
137 .d_hash = exfat_d_hash,
138 .d_compare = exfat_d_cmp,
141 static int exfat_utf8_d_hash(const struct dentry *dentry, struct qstr *qstr)
143 struct super_block *sb = dentry->d_sb;
144 const unsigned char *name = qstr->name;
145 unsigned int len = exfat_striptail_len(qstr->len, qstr->name,
146 EXFAT_SB(sb)->options.keep_last_dots);
147 unsigned long hash = init_name_hash(dentry);
151 for (i = 0; i < len; i += charlen) {
152 charlen = utf8_to_utf32(&name[i], len - i, &u);
157 * exfat_toupper() works only for code points up to the U+FFFF.
159 hash = partial_name_hash(u <= 0xFFFF ? exfat_toupper(sb, u) : u,
163 qstr->hash = end_name_hash(hash);
167 static int exfat_utf8_d_cmp(const struct dentry *dentry, unsigned int len,
168 const char *str, const struct qstr *name)
170 struct super_block *sb = dentry->d_sb;
171 unsigned int alen = exfat_striptail_len(name->len, name->name,
172 EXFAT_SB(sb)->options.keep_last_dots);
173 unsigned int blen = exfat_striptail_len(len, str,
174 EXFAT_SB(sb)->options.keep_last_dots);
182 for (i = 0; i < alen; i += charlen) {
183 charlen = utf8_to_utf32(&name->name[i], alen - i, &u_a);
186 if (charlen != utf8_to_utf32(&str[i], blen - i, &u_b))
189 if (u_a <= 0xFFFF && u_b <= 0xFFFF) {
190 if (exfat_toupper(sb, u_a) != exfat_toupper(sb, u_b))
201 const struct dentry_operations exfat_utf8_dentry_ops = {
202 .d_revalidate = exfat_d_revalidate,
203 .d_hash = exfat_utf8_d_hash,
204 .d_compare = exfat_utf8_d_cmp,
207 /* used only in search empty_slot() */
208 #define CNT_UNUSED_NOHIT (-1)
209 #define CNT_UNUSED_HIT (-2)
210 /* search EMPTY CONTINUOUS "num_entries" entries */
211 static int exfat_search_empty_slot(struct super_block *sb,
212 struct exfat_hint_femp *hint_femp, struct exfat_chain *p_dir,
215 int i, dentry, num_empty = 0;
216 int dentries_per_clu;
218 struct exfat_chain clu;
219 struct exfat_dentry *ep;
220 struct exfat_sb_info *sbi = EXFAT_SB(sb);
221 struct buffer_head *bh;
223 dentries_per_clu = sbi->dentries_per_clu;
225 if (hint_femp->eidx != EXFAT_HINT_NONE) {
226 dentry = hint_femp->eidx;
229 * If hint_femp->count is enough, it is needed to check if
230 * there are actual empty entries.
231 * Otherwise, and if "dentry + hint_famp->count" is also equal
232 * to "p_dir->size * dentries_per_clu", it means ENOSPC.
234 if (dentry + hint_femp->count == p_dir->size * dentries_per_clu &&
235 num_entries > hint_femp->count)
238 hint_femp->eidx = EXFAT_HINT_NONE;
239 exfat_chain_dup(&clu, &hint_femp->cur);
241 exfat_chain_dup(&clu, p_dir);
245 while (clu.dir != EXFAT_EOF_CLUSTER) {
246 i = dentry & (dentries_per_clu - 1);
248 for (; i < dentries_per_clu; i++, dentry++) {
249 ep = exfat_get_dentry(sb, &clu, i, &bh);
252 type = exfat_get_entry_type(ep);
255 if (type == TYPE_UNUSED || type == TYPE_DELETED) {
257 if (hint_femp->eidx == EXFAT_HINT_NONE) {
258 hint_femp->eidx = dentry;
259 hint_femp->count = CNT_UNUSED_NOHIT;
260 exfat_chain_set(&hint_femp->cur,
261 clu.dir, clu.size, clu.flags);
264 if (type == TYPE_UNUSED &&
265 hint_femp->count != CNT_UNUSED_HIT)
266 hint_femp->count = CNT_UNUSED_HIT;
268 if (hint_femp->eidx != EXFAT_HINT_NONE &&
269 hint_femp->count == CNT_UNUSED_HIT) {
270 /* unused empty group means
271 * an empty group which includes
275 "found bogus dentry(%d) beyond unused empty group(%d) (start_clu : %u, cur_clu : %u)",
276 dentry, hint_femp->eidx,
277 p_dir->dir, clu.dir);
282 hint_femp->eidx = EXFAT_HINT_NONE;
285 if (num_empty >= num_entries) {
286 /* found and invalidate hint_femp */
287 hint_femp->eidx = EXFAT_HINT_NONE;
288 return (dentry - (num_entries - 1));
292 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
296 clu.dir = EXFAT_EOF_CLUSTER;
298 if (exfat_get_next_cluster(sb, &clu.dir))
303 hint_femp->eidx = p_dir->size * dentries_per_clu - num_empty;
304 hint_femp->count = num_empty;
306 exfat_chain_set(&hint_femp->cur, EXFAT_EOF_CLUSTER, 0,
312 static int exfat_check_max_dentries(struct inode *inode)
314 if (EXFAT_B_TO_DEN(i_size_read(inode)) >= MAX_EXFAT_DENTRIES) {
316 * exFAT spec allows a dir to grow up to 8388608(256MB)
324 /* find empty directory entry.
325 * if there isn't any empty slot, expand cluster chain.
327 static int exfat_find_empty_entry(struct inode *inode,
328 struct exfat_chain *p_dir, int num_entries)
331 unsigned int ret, last_clu;
333 struct exfat_chain clu;
334 struct super_block *sb = inode->i_sb;
335 struct exfat_sb_info *sbi = EXFAT_SB(sb);
336 struct exfat_inode_info *ei = EXFAT_I(inode);
337 struct exfat_hint_femp hint_femp;
339 hint_femp.eidx = EXFAT_HINT_NONE;
341 if (ei->hint_femp.eidx != EXFAT_HINT_NONE) {
342 hint_femp = ei->hint_femp;
343 ei->hint_femp.eidx = EXFAT_HINT_NONE;
346 while ((dentry = exfat_search_empty_slot(sb, &hint_femp, p_dir,
351 if (exfat_check_max_dentries(inode))
355 * Allocate new cluster to this directory
357 if (ei->start_clu != EXFAT_EOF_CLUSTER) {
358 /* we trust p_dir->size regardless of FAT type */
359 if (exfat_find_last_cluster(sb, p_dir, &last_clu))
362 exfat_chain_set(&clu, last_clu + 1, 0, p_dir->flags);
364 /* This directory is empty */
365 exfat_chain_set(&clu, EXFAT_EOF_CLUSTER, 0,
369 /* allocate a cluster */
370 ret = exfat_alloc_cluster(inode, 1, &clu, IS_DIRSYNC(inode));
374 if (exfat_zeroed_cluster(inode, clu.dir))
377 if (ei->start_clu == EXFAT_EOF_CLUSTER) {
378 ei->start_clu = clu.dir;
379 p_dir->dir = clu.dir;
382 /* append to the FAT chain */
383 if (clu.flags != p_dir->flags) {
384 /* no-fat-chain bit is disabled,
385 * so fat-chain should be synced with alloc-bitmap
387 exfat_chain_cont_cluster(sb, p_dir->dir, p_dir->size);
388 p_dir->flags = ALLOC_FAT_CHAIN;
389 hint_femp.cur.flags = ALLOC_FAT_CHAIN;
392 if (clu.flags == ALLOC_FAT_CHAIN)
393 if (exfat_ent_set(sb, last_clu, clu.dir))
396 if (hint_femp.cur.dir == EXFAT_EOF_CLUSTER)
397 exfat_chain_set(&hint_femp.cur, clu.dir, 0, clu.flags);
399 hint_femp.count += sbi->dentries_per_clu;
401 hint_femp.cur.size++;
403 size = EXFAT_CLU_TO_B(p_dir->size, sbi);
405 /* directory inode should be updated in here */
406 i_size_write(inode, size);
407 ei->i_size_ondisk += sbi->cluster_size;
408 ei->i_size_aligned += sbi->cluster_size;
409 ei->flags = p_dir->flags;
410 inode->i_blocks += sbi->cluster_size >> 9;
417 * Name Resolution Functions :
418 * Zero if it was successful; otherwise nonzero.
420 static int __exfat_resolve_path(struct inode *inode, const unsigned char *path,
421 struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
425 int lossy = NLS_NAME_NO_LOSSY;
426 struct super_block *sb = inode->i_sb;
427 struct exfat_sb_info *sbi = EXFAT_SB(sb);
428 struct exfat_inode_info *ei = EXFAT_I(inode);
429 int pathlen = strlen(path);
432 * get the length of the pathname excluding
433 * trailing periods, if any.
435 namelen = exfat_striptail_len(pathlen, path, false);
436 if (EXFAT_SB(sb)->options.keep_last_dots) {
438 * Do not allow the creation of files with names
439 * ending with period(s).
441 if (!lookup && (namelen < pathlen))
447 if (pathlen > (MAX_NAME_LENGTH * MAX_CHARSET_SIZE))
448 return -ENAMETOOLONG;
451 * strip all leading spaces :
452 * "MS windows 7" supports leading spaces.
453 * So we should skip this preprocessing for compatibility.
456 /* file name conversion :
457 * If lookup case, we allow bad-name for compatibility.
459 namelen = exfat_nls_to_utf16(sb, path, namelen, p_uniname,
462 return namelen; /* return error value */
464 if ((lossy && !lookup) || !namelen)
465 return (lossy & NLS_NAME_OVERLEN) ? -ENAMETOOLONG : -EINVAL;
467 exfat_chain_set(p_dir, ei->start_clu,
468 EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags);
473 static inline int exfat_resolve_path(struct inode *inode,
474 const unsigned char *path, struct exfat_chain *dir,
475 struct exfat_uni_name *uni)
477 return __exfat_resolve_path(inode, path, dir, uni, 0);
480 static inline int exfat_resolve_path_for_lookup(struct inode *inode,
481 const unsigned char *path, struct exfat_chain *dir,
482 struct exfat_uni_name *uni)
484 return __exfat_resolve_path(inode, path, dir, uni, 1);
487 static inline loff_t exfat_make_i_pos(struct exfat_dir_entry *info)
489 return ((loff_t) info->dir.dir << 32) | (info->entry & 0xffffffff);
492 static int exfat_add_entry(struct inode *inode, const char *path,
493 struct exfat_chain *p_dir, unsigned int type,
494 struct exfat_dir_entry *info)
496 int ret, dentry, num_entries;
497 struct super_block *sb = inode->i_sb;
498 struct exfat_sb_info *sbi = EXFAT_SB(sb);
499 struct exfat_uni_name uniname;
500 struct exfat_chain clu;
502 unsigned int start_clu = EXFAT_FREE_CLUSTER;
504 ret = exfat_resolve_path(inode, path, p_dir, &uniname);
508 num_entries = exfat_calc_num_entries(&uniname);
509 if (num_entries < 0) {
514 /* exfat_find_empty_entry must be called before alloc_cluster() */
515 dentry = exfat_find_empty_entry(inode, p_dir, num_entries);
517 ret = dentry; /* -EIO or -ENOSPC */
521 if (type == TYPE_DIR) {
522 ret = exfat_alloc_new_dir(inode, &clu);
526 clu_size = sbi->cluster_size;
529 /* update the directory entry */
530 /* fill the dos name directory entry information of the created file.
531 * the first cluster is not determined yet. (0)
533 ret = exfat_init_dir_entry(inode, p_dir, dentry, type,
534 start_clu, clu_size);
538 ret = exfat_init_ext_entry(inode, p_dir, dentry, num_entries, &uniname);
543 info->entry = dentry;
544 info->flags = ALLOC_NO_FAT_CHAIN;
547 if (type == TYPE_FILE) {
548 info->attr = ATTR_ARCHIVE;
549 info->start_clu = EXFAT_EOF_CLUSTER;
551 info->num_subdirs = 0;
553 info->attr = ATTR_SUBDIR;
554 info->start_clu = start_clu;
555 info->size = clu_size;
556 info->num_subdirs = EXFAT_MIN_SUBDIR;
558 memset(&info->crtime, 0, sizeof(info->crtime));
559 memset(&info->mtime, 0, sizeof(info->mtime));
560 memset(&info->atime, 0, sizeof(info->atime));
565 static int exfat_create(struct mnt_idmap *idmap, struct inode *dir,
566 struct dentry *dentry, umode_t mode, bool excl)
568 struct super_block *sb = dir->i_sb;
570 struct exfat_chain cdir;
571 struct exfat_dir_entry info;
575 mutex_lock(&EXFAT_SB(sb)->s_lock);
576 exfat_set_volume_dirty(sb);
577 err = exfat_add_entry(dir, dentry->d_name.name, &cdir, TYPE_FILE,
582 inode_inc_iversion(dir);
583 dir->i_mtime = inode_set_ctime_current(dir);
585 exfat_sync_inode(dir);
587 mark_inode_dirty(dir);
589 i_pos = exfat_make_i_pos(&info);
590 inode = exfat_build_inode(sb, &info, i_pos);
591 err = PTR_ERR_OR_ZERO(inode);
595 inode_inc_iversion(inode);
596 inode->i_mtime = inode->i_atime = EXFAT_I(inode)->i_crtime = inode_set_ctime_current(inode);
597 exfat_truncate_atime(&inode->i_atime);
598 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
600 d_instantiate(dentry, inode);
602 mutex_unlock(&EXFAT_SB(sb)->s_lock);
607 static int exfat_find(struct inode *dir, struct qstr *qname,
608 struct exfat_dir_entry *info)
610 int ret, dentry, count;
611 struct exfat_chain cdir;
612 struct exfat_uni_name uni_name;
613 struct super_block *sb = dir->i_sb;
614 struct exfat_sb_info *sbi = EXFAT_SB(sb);
615 struct exfat_inode_info *ei = EXFAT_I(dir);
616 struct exfat_dentry *ep, *ep2;
617 struct exfat_entry_set_cache es;
618 /* for optimized dir & entry to prevent long traverse of cluster chain */
619 struct exfat_hint hint_opt;
624 /* check the validity of directory name in the given pathname */
625 ret = exfat_resolve_path_for_lookup(dir, qname->name, &cdir, &uni_name);
629 /* check the validation of hint_stat and initialize it if required */
630 if (ei->version != (inode_peek_iversion_raw(dir) & 0xffffffff)) {
631 ei->hint_stat.clu = cdir.dir;
632 ei->hint_stat.eidx = 0;
633 ei->version = (inode_peek_iversion_raw(dir) & 0xffffffff);
634 ei->hint_femp.eidx = EXFAT_HINT_NONE;
637 /* search the file name for directories */
638 dentry = exfat_find_dir_entry(sb, ei, &cdir, &uni_name, &hint_opt);
640 return dentry; /* -error value */
643 info->entry = dentry;
644 info->num_subdirs = 0;
646 /* adjust cdir to the optimized value */
647 cdir.dir = hint_opt.clu;
648 if (cdir.flags & ALLOC_NO_FAT_CHAIN)
649 cdir.size -= dentry / sbi->dentries_per_clu;
650 dentry = hint_opt.eidx;
651 if (exfat_get_dentry_set(&es, sb, &cdir, dentry, ES_2_ENTRIES))
653 ep = exfat_get_dentry_cached(&es, ES_IDX_FILE);
654 ep2 = exfat_get_dentry_cached(&es, ES_IDX_STREAM);
656 info->type = exfat_get_entry_type(ep);
657 info->attr = le16_to_cpu(ep->dentry.file.attr);
658 info->size = le64_to_cpu(ep2->dentry.stream.valid_size);
659 if (info->size == 0) {
660 info->flags = ALLOC_NO_FAT_CHAIN;
661 info->start_clu = EXFAT_EOF_CLUSTER;
663 info->flags = ep2->dentry.stream.flags;
665 le32_to_cpu(ep2->dentry.stream.start_clu);
668 exfat_get_entry_time(sbi, &info->crtime,
669 ep->dentry.file.create_tz,
670 ep->dentry.file.create_time,
671 ep->dentry.file.create_date,
672 ep->dentry.file.create_time_cs);
673 exfat_get_entry_time(sbi, &info->mtime,
674 ep->dentry.file.modify_tz,
675 ep->dentry.file.modify_time,
676 ep->dentry.file.modify_date,
677 ep->dentry.file.modify_time_cs);
678 exfat_get_entry_time(sbi, &info->atime,
679 ep->dentry.file.access_tz,
680 ep->dentry.file.access_time,
681 ep->dentry.file.access_date,
683 exfat_put_dentry_set(&es, false);
685 if (ei->start_clu == EXFAT_FREE_CLUSTER) {
687 "non-zero size file starts with zero cluster (size : %llu, p_dir : %u, entry : 0x%08x)",
688 i_size_read(dir), ei->dir.dir, ei->entry);
692 if (info->type == TYPE_DIR) {
693 exfat_chain_set(&cdir, info->start_clu,
694 EXFAT_B_TO_CLU(info->size, sbi), info->flags);
695 count = exfat_count_dir_entries(sb, &cdir);
699 info->num_subdirs = count + EXFAT_MIN_SUBDIR;
704 static int exfat_d_anon_disconn(struct dentry *dentry)
706 return IS_ROOT(dentry) && (dentry->d_flags & DCACHE_DISCONNECTED);
709 static struct dentry *exfat_lookup(struct inode *dir, struct dentry *dentry,
712 struct super_block *sb = dir->i_sb;
714 struct dentry *alias;
715 struct exfat_dir_entry info;
720 mutex_lock(&EXFAT_SB(sb)->s_lock);
721 err = exfat_find(dir, &dentry->d_name, &info);
723 if (err == -ENOENT) {
730 i_pos = exfat_make_i_pos(&info);
731 inode = exfat_build_inode(sb, &info, i_pos);
732 err = PTR_ERR_OR_ZERO(inode);
736 i_mode = inode->i_mode;
737 alias = d_find_alias(inode);
740 * Checking "alias->d_parent == dentry->d_parent" to make sure
741 * FS is not corrupted (especially double linked dir).
743 if (alias && alias->d_parent == dentry->d_parent &&
744 !exfat_d_anon_disconn(alias)) {
747 * Unhashed alias is able to exist because of revalidate()
748 * called by lookup_fast. You can easily make this status
749 * by calling create and lookup concurrently
750 * In such case, we reuse an alias instead of new dentry
752 if (d_unhashed(alias)) {
753 WARN_ON(alias->d_name.hash_len !=
754 dentry->d_name.hash_len);
755 exfat_info(sb, "rehashed a dentry(%p) in read lookup",
759 } else if (!S_ISDIR(i_mode)) {
761 * This inode has non anonymous-DCACHE_DISCONNECTED
762 * dentry. This means, the user did ->lookup() by an
763 * another name (longname vs 8.3 alias of it) in past.
765 * Switch to new one for reason of locality if possible.
767 d_move(alias, dentry);
770 mutex_unlock(&EXFAT_SB(sb)->s_lock);
775 mutex_unlock(&EXFAT_SB(sb)->s_lock);
777 exfat_d_version_set(dentry, inode_query_iversion(dir));
779 return d_splice_alias(inode, dentry);
781 mutex_unlock(&EXFAT_SB(sb)->s_lock);
785 /* remove an entry, BUT don't truncate */
786 static int exfat_unlink(struct inode *dir, struct dentry *dentry)
788 struct exfat_chain cdir;
789 struct exfat_dentry *ep;
790 struct super_block *sb = dir->i_sb;
791 struct inode *inode = dentry->d_inode;
792 struct exfat_inode_info *ei = EXFAT_I(inode);
793 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);
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;
829 inode_inc_iversion(dir);
830 dir->i_mtime = dir->i_atime = inode_set_ctime_current(dir);
831 exfat_truncate_atime(&dir->i_atime);
833 exfat_sync_inode(dir);
835 mark_inode_dirty(dir);
838 inode->i_mtime = inode->i_atime = inode_set_ctime_current(inode);
839 exfat_truncate_atime(&inode->i_atime);
840 exfat_unhash_inode(inode);
841 exfat_d_version_set(dentry, inode_query_iversion(dir));
843 mutex_unlock(&EXFAT_SB(sb)->s_lock);
847 static int exfat_mkdir(struct mnt_idmap *idmap, struct inode *dir,
848 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,
864 inode_inc_iversion(dir);
865 dir->i_mtime = inode_set_ctime_current(dir);
867 exfat_sync_inode(dir);
869 mark_inode_dirty(dir);
872 i_pos = exfat_make_i_pos(&info);
873 inode = exfat_build_inode(sb, &info, i_pos);
874 err = PTR_ERR_OR_ZERO(inode);
878 inode_inc_iversion(inode);
879 inode->i_mtime = inode->i_atime = EXFAT_I(inode)->i_crtime = inode_set_ctime_current(inode);
880 exfat_truncate_atime(&inode->i_atime);
881 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
883 d_instantiate(dentry, inode);
886 mutex_unlock(&EXFAT_SB(sb)->s_lock);
890 static int exfat_check_dir_empty(struct super_block *sb,
891 struct exfat_chain *p_dir)
893 int i, dentries_per_clu;
895 struct exfat_chain clu;
896 struct exfat_dentry *ep;
897 struct exfat_sb_info *sbi = EXFAT_SB(sb);
898 struct buffer_head *bh;
900 dentries_per_clu = sbi->dentries_per_clu;
902 if (p_dir->dir == EXFAT_EOF_CLUSTER)
905 exfat_chain_dup(&clu, p_dir);
907 while (clu.dir != EXFAT_EOF_CLUSTER) {
908 for (i = 0; i < dentries_per_clu; i++) {
909 ep = exfat_get_dentry(sb, &clu, i, &bh);
912 type = exfat_get_entry_type(ep);
914 if (type == TYPE_UNUSED)
917 if (type != TYPE_FILE && type != TYPE_DIR)
923 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
927 clu.dir = EXFAT_EOF_CLUSTER;
929 if (exfat_get_next_cluster(sb, &(clu.dir)))
937 static int exfat_rmdir(struct inode *dir, struct dentry *dentry)
939 struct inode *inode = dentry->d_inode;
940 struct exfat_dentry *ep;
941 struct exfat_chain cdir, clu_to_free;
942 struct super_block *sb = inode->i_sb;
943 struct exfat_sb_info *sbi = EXFAT_SB(sb);
944 struct exfat_inode_info *ei = EXFAT_I(inode);
945 struct buffer_head *bh;
946 int num_entries, entry, err;
948 mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
950 exfat_chain_dup(&cdir, &ei->dir);
953 if (ei->dir.dir == DIR_DELETED) {
954 exfat_err(sb, "abnormal access to deleted dentry");
959 exfat_chain_set(&clu_to_free, ei->start_clu,
960 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi), ei->flags);
962 err = exfat_check_dir_empty(sb, &clu_to_free);
965 exfat_err(sb, "failed to exfat_check_dir_empty : err(%d)",
970 ep = exfat_get_dentry(sb, &cdir, entry, &bh);
976 num_entries = exfat_count_ext_entries(sb, &cdir, entry, ep);
977 if (num_entries < 0) {
985 exfat_set_volume_dirty(sb);
986 err = exfat_remove_entries(dir, &cdir, entry, 0, num_entries);
988 exfat_err(sb, "failed to exfat_remove_entries : err(%d)", err);
991 ei->dir.dir = DIR_DELETED;
993 inode_inc_iversion(dir);
994 dir->i_mtime = dir->i_atime = inode_set_ctime_current(dir);
995 exfat_truncate_atime(&dir->i_atime);
997 exfat_sync_inode(dir);
999 mark_inode_dirty(dir);
1003 inode->i_mtime = inode->i_atime = inode_set_ctime_current(inode);
1004 exfat_truncate_atime(&inode->i_atime);
1005 exfat_unhash_inode(inode);
1006 exfat_d_version_set(dentry, inode_query_iversion(dir));
1008 mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
1012 static int exfat_rename_file(struct inode *inode, struct exfat_chain *p_dir,
1013 int oldentry, struct exfat_uni_name *p_uniname,
1014 struct exfat_inode_info *ei)
1016 int ret, num_old_entries, num_new_entries;
1017 struct exfat_dentry *epold, *epnew;
1018 struct super_block *sb = inode->i_sb;
1019 struct buffer_head *new_bh, *old_bh;
1020 int sync = IS_DIRSYNC(inode);
1022 epold = exfat_get_dentry(sb, p_dir, oldentry, &old_bh);
1026 num_old_entries = exfat_count_ext_entries(sb, p_dir, oldentry, epold);
1027 if (num_old_entries < 0)
1031 num_new_entries = exfat_calc_num_entries(p_uniname);
1032 if (num_new_entries < 0)
1033 return num_new_entries;
1035 if (num_old_entries < num_new_entries) {
1039 exfat_find_empty_entry(inode, p_dir, num_new_entries);
1041 return newentry; /* -EIO or -ENOSPC */
1043 epnew = exfat_get_dentry(sb, p_dir, newentry, &new_bh);
1048 if (exfat_get_entry_type(epnew) == TYPE_FILE) {
1049 epnew->dentry.file.attr |= cpu_to_le16(ATTR_ARCHIVE);
1050 ei->attr |= ATTR_ARCHIVE;
1052 exfat_update_bh(new_bh, sync);
1056 epold = exfat_get_dentry(sb, p_dir, oldentry + 1, &old_bh);
1059 epnew = exfat_get_dentry(sb, p_dir, newentry + 1, &new_bh);
1066 exfat_update_bh(new_bh, sync);
1070 ret = exfat_init_ext_entry(inode, p_dir, newentry,
1071 num_new_entries, p_uniname);
1075 exfat_remove_entries(inode, p_dir, oldentry, 0,
1078 ei->entry = newentry;
1080 if (exfat_get_entry_type(epold) == TYPE_FILE) {
1081 epold->dentry.file.attr |= cpu_to_le16(ATTR_ARCHIVE);
1082 ei->attr |= ATTR_ARCHIVE;
1084 exfat_update_bh(old_bh, sync);
1086 ret = exfat_init_ext_entry(inode, p_dir, oldentry,
1087 num_new_entries, p_uniname);
1091 exfat_remove_entries(inode, p_dir, oldentry, num_new_entries,
1097 static int exfat_move_file(struct inode *inode, struct exfat_chain *p_olddir,
1098 int oldentry, struct exfat_chain *p_newdir,
1099 struct exfat_uni_name *p_uniname, struct exfat_inode_info *ei)
1101 int ret, newentry, num_new_entries, num_old_entries;
1102 struct exfat_dentry *epmov, *epnew;
1103 struct super_block *sb = inode->i_sb;
1104 struct buffer_head *mov_bh, *new_bh;
1106 epmov = exfat_get_dentry(sb, p_olddir, oldentry, &mov_bh);
1110 num_old_entries = exfat_count_ext_entries(sb, p_olddir, oldentry,
1112 if (num_old_entries < 0)
1116 num_new_entries = exfat_calc_num_entries(p_uniname);
1117 if (num_new_entries < 0)
1118 return num_new_entries;
1120 newentry = exfat_find_empty_entry(inode, p_newdir, num_new_entries);
1122 return newentry; /* -EIO or -ENOSPC */
1124 epnew = exfat_get_dentry(sb, p_newdir, newentry, &new_bh);
1129 if (exfat_get_entry_type(epnew) == TYPE_FILE) {
1130 epnew->dentry.file.attr |= cpu_to_le16(ATTR_ARCHIVE);
1131 ei->attr |= ATTR_ARCHIVE;
1133 exfat_update_bh(new_bh, IS_DIRSYNC(inode));
1137 epmov = exfat_get_dentry(sb, p_olddir, oldentry + 1, &mov_bh);
1140 epnew = exfat_get_dentry(sb, p_newdir, newentry + 1, &new_bh);
1147 exfat_update_bh(new_bh, IS_DIRSYNC(inode));
1151 ret = exfat_init_ext_entry(inode, p_newdir, newentry, num_new_entries,
1156 exfat_remove_entries(inode, p_olddir, oldentry, 0, num_old_entries);
1158 exfat_chain_set(&ei->dir, p_newdir->dir, p_newdir->size,
1161 ei->entry = newentry;
1165 /* rename or move a old file into a new file */
1166 static int __exfat_rename(struct inode *old_parent_inode,
1167 struct exfat_inode_info *ei, struct inode *new_parent_inode,
1168 struct dentry *new_dentry)
1172 struct exfat_chain olddir, newdir;
1173 struct exfat_chain *p_dir = NULL;
1174 struct exfat_uni_name uni_name;
1175 struct exfat_dentry *ep;
1176 struct super_block *sb = old_parent_inode->i_sb;
1177 struct exfat_sb_info *sbi = EXFAT_SB(sb);
1178 const unsigned char *new_path = new_dentry->d_name.name;
1179 struct inode *new_inode = new_dentry->d_inode;
1181 struct exfat_inode_info *new_ei = NULL;
1182 unsigned int new_entry_type = TYPE_UNUSED;
1184 struct buffer_head *new_bh = NULL;
1186 /* check the validity of pointer parameters */
1187 if (new_path == NULL || strlen(new_path) == 0)
1190 if (ei->dir.dir == DIR_DELETED) {
1191 exfat_err(sb, "abnormal access to deleted source dentry");
1195 exfat_chain_set(&olddir, EXFAT_I(old_parent_inode)->start_clu,
1196 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(old_parent_inode), sbi),
1197 EXFAT_I(old_parent_inode)->flags);
1200 /* check whether new dir is existing directory and empty */
1203 new_ei = EXFAT_I(new_inode);
1205 if (new_ei->dir.dir == DIR_DELETED) {
1206 exfat_err(sb, "abnormal access to deleted target dentry");
1210 p_dir = &(new_ei->dir);
1211 new_entry = new_ei->entry;
1212 ep = exfat_get_dentry(sb, p_dir, new_entry, &new_bh);
1216 new_entry_type = exfat_get_entry_type(ep);
1219 /* if new_inode exists, update ei */
1220 if (new_entry_type == TYPE_DIR) {
1221 struct exfat_chain new_clu;
1223 new_clu.dir = new_ei->start_clu;
1225 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode),
1227 new_clu.flags = new_ei->flags;
1229 ret = exfat_check_dir_empty(sb, &new_clu);
1235 /* check the validity of directory name in the given new pathname */
1236 ret = exfat_resolve_path(new_parent_inode, new_path, &newdir,
1241 exfat_set_volume_dirty(sb);
1243 if (olddir.dir == newdir.dir)
1244 ret = exfat_rename_file(new_parent_inode, &olddir, dentry,
1247 ret = exfat_move_file(new_parent_inode, &olddir, dentry,
1248 &newdir, &uni_name, ei);
1250 if (!ret && new_inode) {
1251 /* delete entries of new_dir */
1252 ep = exfat_get_dentry(sb, p_dir, new_entry, &new_bh);
1258 num_entries = exfat_count_ext_entries(sb, p_dir, new_entry, ep);
1259 if (num_entries < 0) {
1265 if (exfat_remove_entries(new_inode, p_dir, new_entry, 0,
1271 /* Free the clusters if new_inode is a dir(as if exfat_rmdir) */
1272 if (new_entry_type == TYPE_DIR &&
1273 new_ei->start_clu != EXFAT_EOF_CLUSTER) {
1274 /* new_ei, new_clu_to_free */
1275 struct exfat_chain new_clu_to_free;
1277 exfat_chain_set(&new_clu_to_free, new_ei->start_clu,
1278 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode),
1279 sbi), new_ei->flags);
1281 if (exfat_free_cluster(new_inode, &new_clu_to_free)) {
1282 /* just set I/O error only */
1286 i_size_write(new_inode, 0);
1287 new_ei->start_clu = EXFAT_EOF_CLUSTER;
1288 new_ei->flags = ALLOC_NO_FAT_CHAIN;
1291 /* Update new_inode ei
1292 * Prevent syncing removed new_inode
1293 * (new_ei is already initialized above code ("if (new_inode)")
1295 new_ei->dir.dir = DIR_DELETED;
1301 static int exfat_rename(struct mnt_idmap *idmap,
1302 struct inode *old_dir, struct dentry *old_dentry,
1303 struct inode *new_dir, struct dentry *new_dentry,
1306 struct inode *old_inode, *new_inode;
1307 struct super_block *sb = old_dir->i_sb;
1312 * The VFS already checks for existence, so for local filesystems
1313 * the RENAME_NOREPLACE implementation is equivalent to plain rename.
1314 * Don't support any other flags
1316 if (flags & ~RENAME_NOREPLACE)
1319 mutex_lock(&EXFAT_SB(sb)->s_lock);
1320 old_inode = old_dentry->d_inode;
1321 new_inode = new_dentry->d_inode;
1323 err = __exfat_rename(old_dir, EXFAT_I(old_inode), new_dir, new_dentry);
1327 inode_inc_iversion(new_dir);
1328 simple_rename_timestamp(old_dir, old_dentry, new_dir, new_dentry);
1329 EXFAT_I(new_dir)->i_crtime = current_time(new_dir);
1330 exfat_truncate_atime(&new_dir->i_atime);
1331 if (IS_DIRSYNC(new_dir))
1332 exfat_sync_inode(new_dir);
1334 mark_inode_dirty(new_dir);
1336 i_pos = ((loff_t)EXFAT_I(old_inode)->dir.dir << 32) |
1337 (EXFAT_I(old_inode)->entry & 0xffffffff);
1338 exfat_unhash_inode(old_inode);
1339 exfat_hash_inode(old_inode, i_pos);
1340 if (IS_DIRSYNC(new_dir))
1341 exfat_sync_inode(old_inode);
1343 mark_inode_dirty(old_inode);
1345 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
1346 drop_nlink(old_dir);
1351 inode_inc_iversion(old_dir);
1352 if (IS_DIRSYNC(old_dir))
1353 exfat_sync_inode(old_dir);
1355 mark_inode_dirty(old_dir);
1358 exfat_unhash_inode(new_inode);
1360 /* skip drop_nlink if new_inode already has been dropped */
1361 if (new_inode->i_nlink) {
1362 drop_nlink(new_inode);
1363 if (S_ISDIR(new_inode->i_mode))
1364 drop_nlink(new_inode);
1366 exfat_warn(sb, "abnormal access to an inode dropped");
1367 WARN_ON(new_inode->i_nlink == 0);
1369 EXFAT_I(new_inode)->i_crtime = current_time(new_inode);
1373 mutex_unlock(&EXFAT_SB(sb)->s_lock);
1377 const struct inode_operations exfat_dir_inode_operations = {
1378 .create = exfat_create,
1379 .lookup = exfat_lookup,
1380 .unlink = exfat_unlink,
1381 .mkdir = exfat_mkdir,
1382 .rmdir = exfat_rmdir,
1383 .rename = exfat_rename,
1384 .setattr = exfat_setattr,
1385 .getattr = exfat_getattr,