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 * Convert to UTF-16: code points above U+FFFF are encoded as
152 * exfat_toupper() works only for code points up to the U+FFFF.
155 hash = partial_name_hash(exfat_high_surrogate(u), hash);
156 hash = partial_name_hash(exfat_low_surrogate(u), hash);
158 hash = partial_name_hash(exfat_toupper(sb, u), hash);
162 qstr->hash = end_name_hash(hash);
166 static int exfat_utf8_d_cmp(const struct dentry *dentry, unsigned int len,
167 const char *str, const struct qstr *name)
169 struct super_block *sb = dentry->d_sb;
170 unsigned int alen = exfat_striptail_len(name->len, name->name);
171 unsigned int blen = exfat_striptail_len(len, str);
178 for (i = 0; i < alen; i += charlen) {
179 charlen = utf8_to_utf32(&name->name[i], alen - i, &u_a);
182 if (charlen != utf8_to_utf32(&str[i], blen - i, &u_b))
185 if (u_a <= 0xFFFF && u_b <= 0xFFFF) {
186 if (exfat_toupper(sb, u_a) != exfat_toupper(sb, u_b))
188 } else if (u_a > 0xFFFF && u_b > 0xFFFF) {
189 if (exfat_low_surrogate(u_a) !=
190 exfat_low_surrogate(u_b) ||
191 exfat_high_surrogate(u_a) !=
192 exfat_high_surrogate(u_b))
202 const struct dentry_operations exfat_utf8_dentry_ops = {
203 .d_revalidate = exfat_d_revalidate,
204 .d_hash = exfat_utf8_d_hash,
205 .d_compare = exfat_utf8_d_cmp,
208 /* used only in search empty_slot() */
209 #define CNT_UNUSED_NOHIT (-1)
210 #define CNT_UNUSED_HIT (-2)
211 /* search EMPTY CONTINUOUS "num_entries" entries */
212 static int exfat_search_empty_slot(struct super_block *sb,
213 struct exfat_hint_femp *hint_femp, struct exfat_chain *p_dir,
216 int i, dentry, num_empty = 0;
217 int dentries_per_clu;
219 struct exfat_chain clu;
220 struct exfat_dentry *ep;
221 struct exfat_sb_info *sbi = EXFAT_SB(sb);
222 struct buffer_head *bh;
224 dentries_per_clu = sbi->dentries_per_clu;
226 if (hint_femp->eidx != EXFAT_HINT_NONE) {
227 dentry = hint_femp->eidx;
228 if (num_entries <= hint_femp->count) {
229 hint_femp->eidx = EXFAT_HINT_NONE;
233 exfat_chain_dup(&clu, &hint_femp->cur);
235 exfat_chain_dup(&clu, p_dir);
239 while (clu.dir != EXFAT_EOF_CLUSTER) {
240 i = dentry & (dentries_per_clu - 1);
242 for (; i < dentries_per_clu; i++, dentry++) {
243 ep = exfat_get_dentry(sb, &clu, i, &bh, NULL);
246 type = exfat_get_entry_type(ep);
249 if (type == TYPE_UNUSED || type == TYPE_DELETED) {
251 if (hint_femp->eidx == EXFAT_HINT_NONE) {
252 hint_femp->eidx = dentry;
253 hint_femp->count = CNT_UNUSED_NOHIT;
254 exfat_chain_set(&hint_femp->cur,
255 clu.dir, clu.size, clu.flags);
258 if (type == TYPE_UNUSED &&
259 hint_femp->count != CNT_UNUSED_HIT)
260 hint_femp->count = CNT_UNUSED_HIT;
262 if (hint_femp->eidx != EXFAT_HINT_NONE &&
263 hint_femp->count == CNT_UNUSED_HIT) {
264 /* unused empty group means
265 * an empty group which includes
269 "found bogus dentry(%d) beyond unused empty group(%d) (start_clu : %u, cur_clu : %u)",
270 dentry, hint_femp->eidx,
271 p_dir->dir, clu.dir);
276 hint_femp->eidx = EXFAT_HINT_NONE;
279 if (num_empty >= num_entries) {
280 /* found and invalidate hint_femp */
281 hint_femp->eidx = EXFAT_HINT_NONE;
282 return (dentry - (num_entries - 1));
286 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
290 clu.dir = EXFAT_EOF_CLUSTER;
292 if (exfat_get_next_cluster(sb, &clu.dir))
300 static int exfat_check_max_dentries(struct inode *inode)
302 if (EXFAT_B_TO_DEN(i_size_read(inode)) >= MAX_EXFAT_DENTRIES) {
304 * exFAT spec allows a dir to grow upto 8388608(256MB)
312 /* find empty directory entry.
313 * if there isn't any empty slot, expand cluster chain.
315 static int exfat_find_empty_entry(struct inode *inode,
316 struct exfat_chain *p_dir, int num_entries)
319 unsigned int ret, last_clu;
322 struct exfat_chain clu;
323 struct exfat_dentry *ep = NULL;
324 struct super_block *sb = inode->i_sb;
325 struct exfat_sb_info *sbi = EXFAT_SB(sb);
326 struct exfat_inode_info *ei = EXFAT_I(inode);
327 struct exfat_hint_femp hint_femp;
329 hint_femp.eidx = EXFAT_HINT_NONE;
331 if (ei->hint_femp.eidx != EXFAT_HINT_NONE) {
332 memcpy(&hint_femp, &ei->hint_femp,
333 sizeof(struct exfat_hint_femp));
334 ei->hint_femp.eidx = EXFAT_HINT_NONE;
337 while ((dentry = exfat_search_empty_slot(sb, &hint_femp, p_dir,
342 if (exfat_check_max_dentries(inode))
345 /* we trust p_dir->size regardless of FAT type */
346 if (exfat_find_last_cluster(sb, p_dir, &last_clu))
350 * Allocate new cluster to this directory
352 exfat_chain_set(&clu, last_clu + 1, 0, p_dir->flags);
354 /* allocate a cluster */
355 ret = exfat_alloc_cluster(inode, 1, &clu);
359 if (exfat_zeroed_cluster(inode, clu.dir))
362 /* append to the FAT chain */
363 if (clu.flags != p_dir->flags) {
364 /* no-fat-chain bit is disabled,
365 * so fat-chain should be synced with alloc-bitmap
367 exfat_chain_cont_cluster(sb, p_dir->dir, p_dir->size);
368 p_dir->flags = ALLOC_FAT_CHAIN;
369 hint_femp.cur.flags = ALLOC_FAT_CHAIN;
372 if (clu.flags == ALLOC_FAT_CHAIN)
373 if (exfat_ent_set(sb, last_clu, clu.dir))
376 if (hint_femp.eidx == EXFAT_HINT_NONE) {
377 /* the special case that new dentry
378 * should be allocated from the start of new cluster
380 hint_femp.eidx = EXFAT_B_TO_DEN_IDX(p_dir->size, sbi);
381 hint_femp.count = sbi->dentries_per_clu;
383 exfat_chain_set(&hint_femp.cur, clu.dir, 0, clu.flags);
385 hint_femp.cur.size++;
387 size = EXFAT_CLU_TO_B(p_dir->size, sbi);
389 /* update the directory entry */
390 if (p_dir->dir != sbi->root_dir) {
391 struct buffer_head *bh;
393 ep = exfat_get_dentry(sb,
394 &(ei->dir), ei->entry + 1, &bh, §or);
398 ep->dentry.stream.valid_size = cpu_to_le64(size);
399 ep->dentry.stream.size = ep->dentry.stream.valid_size;
400 ep->dentry.stream.flags = p_dir->flags;
401 exfat_update_bh(sb, bh, IS_DIRSYNC(inode));
403 if (exfat_update_dir_chksum(inode, &(ei->dir),
408 /* directory inode should be updated in here */
409 i_size_write(inode, size);
410 EXFAT_I(inode)->i_size_ondisk += sbi->cluster_size;
411 EXFAT_I(inode)->i_size_aligned += sbi->cluster_size;
412 EXFAT_I(inode)->flags = p_dir->flags;
413 inode->i_blocks += 1 << sbi->sect_per_clus_bits;
420 * Name Resolution Functions :
421 * Zero if it was successful; otherwise nonzero.
423 static int __exfat_resolve_path(struct inode *inode, const unsigned char *path,
424 struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
428 int lossy = NLS_NAME_NO_LOSSY;
429 struct super_block *sb = inode->i_sb;
430 struct exfat_sb_info *sbi = EXFAT_SB(sb);
431 struct exfat_inode_info *ei = EXFAT_I(inode);
433 /* strip all trailing periods */
434 namelen = exfat_striptail_len(strlen(path), path);
438 if (strlen(path) > (MAX_NAME_LENGTH * MAX_CHARSET_SIZE))
439 return -ENAMETOOLONG;
442 * strip all leading spaces :
443 * "MS windows 7" supports leading spaces.
444 * So we should skip this preprocessing for compatibility.
447 /* file name conversion :
448 * If lookup case, we allow bad-name for compatibility.
450 namelen = exfat_nls_to_utf16(sb, path, namelen, p_uniname,
453 return namelen; /* return error value */
455 if ((lossy && !lookup) || !namelen)
458 exfat_chain_set(p_dir, ei->start_clu,
459 EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags);
464 static inline int exfat_resolve_path(struct inode *inode,
465 const unsigned char *path, struct exfat_chain *dir,
466 struct exfat_uni_name *uni)
468 return __exfat_resolve_path(inode, path, dir, uni, 0);
471 static inline int exfat_resolve_path_for_lookup(struct inode *inode,
472 const unsigned char *path, struct exfat_chain *dir,
473 struct exfat_uni_name *uni)
475 return __exfat_resolve_path(inode, path, dir, uni, 1);
478 static inline loff_t exfat_make_i_pos(struct exfat_dir_entry *info)
480 return ((loff_t) info->dir.dir << 32) | (info->entry & 0xffffffff);
483 static int exfat_add_entry(struct inode *inode, const char *path,
484 struct exfat_chain *p_dir, unsigned int type,
485 struct exfat_dir_entry *info)
487 int ret, dentry, num_entries;
488 struct super_block *sb = inode->i_sb;
489 struct exfat_sb_info *sbi = EXFAT_SB(sb);
490 struct exfat_uni_name uniname;
491 struct exfat_chain clu;
493 unsigned int start_clu = EXFAT_FREE_CLUSTER;
495 ret = exfat_resolve_path(inode, path, p_dir, &uniname);
499 num_entries = exfat_calc_num_entries(&uniname);
500 if (num_entries < 0) {
505 /* exfat_find_empty_entry must be called before alloc_cluster() */
506 dentry = exfat_find_empty_entry(inode, p_dir, num_entries);
508 ret = dentry; /* -EIO or -ENOSPC */
512 if (type == TYPE_DIR) {
513 ret = exfat_alloc_new_dir(inode, &clu);
517 clu_size = sbi->cluster_size;
520 /* update the directory entry */
521 /* fill the dos name directory entry information of the created file.
522 * the first cluster is not determined yet. (0)
524 ret = exfat_init_dir_entry(inode, p_dir, dentry, type,
525 start_clu, clu_size);
529 ret = exfat_init_ext_entry(inode, p_dir, dentry, num_entries, &uniname);
533 memcpy(&info->dir, p_dir, sizeof(struct exfat_chain));
534 info->entry = dentry;
535 info->flags = ALLOC_NO_FAT_CHAIN;
538 if (type == TYPE_FILE) {
539 info->attr = ATTR_ARCHIVE;
540 info->start_clu = EXFAT_EOF_CLUSTER;
542 info->num_subdirs = 0;
545 struct exfat_chain cdir;
547 info->attr = ATTR_SUBDIR;
548 info->start_clu = start_clu;
549 info->size = clu_size;
551 exfat_chain_set(&cdir, info->start_clu,
552 EXFAT_B_TO_CLU(info->size, sbi), info->flags);
553 count = exfat_count_dir_entries(sb, &cdir);
556 info->num_subdirs = count + 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 inode *dir, struct dentry *dentry, umode_t mode,
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_vol_flags(sb, VOL_DIRTY);
577 err = exfat_add_entry(dir, dentry->d_name.name, &cdir, TYPE_FILE,
579 exfat_set_vol_flags(sb, VOL_CLEAN);
583 inode_inc_iversion(dir);
584 dir->i_ctime = dir->i_mtime = current_time(dir);
586 exfat_sync_inode(dir);
588 mark_inode_dirty(dir);
590 i_pos = exfat_make_i_pos(&info);
591 inode = exfat_build_inode(sb, &info, i_pos);
595 inode_inc_iversion(inode);
596 inode->i_mtime = inode->i_atime = inode->i_ctime =
597 EXFAT_I(inode)->i_crtime = current_time(inode);
598 exfat_truncate_atime(&inode->i_atime);
599 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
601 d_instantiate(dentry, inode);
603 mutex_unlock(&EXFAT_SB(sb)->s_lock);
608 static int exfat_find(struct inode *dir, struct qstr *qname,
609 struct exfat_dir_entry *info)
611 int ret, dentry, num_entries, count;
612 struct exfat_chain cdir;
613 struct exfat_uni_name uni_name;
614 struct exfat_dentry *ep, *ep2;
615 struct exfat_entry_set_cache *es = NULL;
616 struct super_block *sb = dir->i_sb;
617 struct exfat_sb_info *sbi = EXFAT_SB(sb);
618 struct exfat_inode_info *ei = EXFAT_I(dir);
623 /* check the validity of directory name in the given pathname */
624 ret = exfat_resolve_path_for_lookup(dir, qname->name, &cdir, &uni_name);
628 num_entries = exfat_calc_num_entries(&uni_name);
632 /* check the validation of hint_stat and initialize it if required */
633 if (ei->version != (inode_peek_iversion_raw(dir) & 0xffffffff)) {
634 ei->hint_stat.clu = cdir.dir;
635 ei->hint_stat.eidx = 0;
636 ei->version = (inode_peek_iversion_raw(dir) & 0xffffffff);
637 ei->hint_femp.eidx = EXFAT_HINT_NONE;
640 /* search the file name for directories */
641 dentry = exfat_find_dir_entry(sb, ei, &cdir, &uni_name,
642 num_entries, TYPE_ALL);
644 if ((dentry < 0) && (dentry != -EEXIST))
645 return dentry; /* -error value */
647 memcpy(&info->dir, &cdir.dir, sizeof(struct exfat_chain));
648 info->entry = dentry;
649 info->num_subdirs = 0;
651 /* root directory itself */
652 if (unlikely(dentry == -EEXIST)) {
655 info->type = TYPE_DIR;
656 info->attr = ATTR_SUBDIR;
657 info->flags = ALLOC_FAT_CHAIN;
658 info->start_clu = sbi->root_dir;
659 memset(&info->crtime, 0, sizeof(info->crtime));
660 memset(&info->mtime, 0, sizeof(info->mtime));
661 memset(&info->atime, 0, sizeof(info->atime));
663 exfat_chain_set(&cdir, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
664 if (exfat_count_num_clusters(sb, &cdir, &num_clu))
666 info->size = num_clu << sbi->cluster_size_bits;
668 count = exfat_count_dir_entries(sb, &cdir);
672 info->num_subdirs = count;
674 es = exfat_get_dentry_set(sb, &cdir, dentry, ES_2_ENTRIES, &ep);
679 info->type = exfat_get_entry_type(ep);
680 info->attr = le16_to_cpu(ep->dentry.file.attr);
681 info->size = le64_to_cpu(ep2->dentry.stream.valid_size);
682 if ((info->type == TYPE_FILE) && (info->size == 0)) {
683 info->flags = ALLOC_NO_FAT_CHAIN;
684 info->start_clu = EXFAT_EOF_CLUSTER;
686 info->flags = ep2->dentry.stream.flags;
688 le32_to_cpu(ep2->dentry.stream.start_clu);
691 if (ei->start_clu == EXFAT_FREE_CLUSTER) {
693 "non-zero size file starts with zero cluster (size : %llu, p_dir : %u, entry : 0x%08x)",
694 i_size_read(dir), ei->dir.dir, ei->entry);
699 exfat_get_entry_time(sbi, &info->crtime,
700 ep->dentry.file.create_tz,
701 ep->dentry.file.create_time,
702 ep->dentry.file.create_date,
703 ep->dentry.file.create_time_ms);
704 exfat_get_entry_time(sbi, &info->mtime,
705 ep->dentry.file.modify_tz,
706 ep->dentry.file.modify_time,
707 ep->dentry.file.modify_date,
708 ep->dentry.file.modify_time_ms);
709 exfat_get_entry_time(sbi, &info->atime,
710 ep->dentry.file.access_tz,
711 ep->dentry.file.access_time,
712 ep->dentry.file.access_date,
716 if (info->type == TYPE_DIR) {
717 exfat_chain_set(&cdir, info->start_clu,
718 EXFAT_B_TO_CLU(info->size, sbi), info->flags);
719 count = exfat_count_dir_entries(sb, &cdir);
723 info->num_subdirs = count + EXFAT_MIN_SUBDIR;
729 static int exfat_d_anon_disconn(struct dentry *dentry)
731 return IS_ROOT(dentry) && (dentry->d_flags & DCACHE_DISCONNECTED);
734 static struct dentry *exfat_lookup(struct inode *dir, struct dentry *dentry,
737 struct super_block *sb = dir->i_sb;
739 struct dentry *alias;
740 struct exfat_dir_entry info;
745 mutex_lock(&EXFAT_SB(sb)->s_lock);
746 err = exfat_find(dir, &dentry->d_name, &info);
748 if (err == -ENOENT) {
755 i_pos = exfat_make_i_pos(&info);
756 inode = exfat_build_inode(sb, &info, i_pos);
758 err = PTR_ERR(inode);
762 i_mode = inode->i_mode;
763 alias = d_find_alias(inode);
766 * Checking "alias->d_parent == dentry->d_parent" to make sure
767 * FS is not corrupted (especially double linked dir).
769 if (alias && alias->d_parent == dentry->d_parent &&
770 !exfat_d_anon_disconn(alias)) {
773 * Unhashed alias is able to exist because of revalidate()
774 * called by lookup_fast. You can easily make this status
775 * by calling create and lookup concurrently
776 * In such case, we reuse an alias instead of new dentry
778 if (d_unhashed(alias)) {
779 WARN_ON(alias->d_name.hash_len !=
780 dentry->d_name.hash_len);
781 exfat_msg(sb, KERN_INFO,
782 "rehashed a dentry(%p) in read lookup", alias);
785 } else if (!S_ISDIR(i_mode)) {
787 * This inode has non anonymous-DCACHE_DISCONNECTED
788 * dentry. This means, the user did ->lookup() by an
789 * another name (longname vs 8.3 alias of it) in past.
791 * Switch to new one for reason of locality if possible.
793 d_move(alias, dentry);
796 mutex_unlock(&EXFAT_SB(sb)->s_lock);
801 mutex_unlock(&EXFAT_SB(sb)->s_lock);
803 exfat_d_version_set(dentry, inode_query_iversion(dir));
805 return d_splice_alias(inode, dentry);
807 mutex_unlock(&EXFAT_SB(sb)->s_lock);
811 /* remove an entry, BUT don't truncate */
812 static int exfat_unlink(struct inode *dir, struct dentry *dentry)
814 struct exfat_chain cdir;
815 struct exfat_dentry *ep;
816 struct super_block *sb = dir->i_sb;
817 struct inode *inode = dentry->d_inode;
818 struct exfat_inode_info *ei = EXFAT_I(inode);
819 struct buffer_head *bh;
821 int num_entries, entry, err = 0;
823 mutex_lock(&EXFAT_SB(sb)->s_lock);
824 exfat_chain_dup(&cdir, &ei->dir);
826 if (ei->dir.dir == DIR_DELETED) {
827 exfat_msg(sb, KERN_ERR, "abnormal access to deleted dentry");
832 ep = exfat_get_dentry(sb, &cdir, entry, &bh, §or);
837 num_entries = exfat_count_ext_entries(sb, &cdir, entry, ep);
838 if (num_entries < 0) {
846 exfat_set_vol_flags(sb, VOL_DIRTY);
847 /* update the directory entry */
848 if (exfat_remove_entries(dir, &cdir, entry, 0, num_entries)) {
853 /* This doesn't modify ei */
854 ei->dir.dir = DIR_DELETED;
855 exfat_set_vol_flags(sb, VOL_CLEAN);
857 inode_inc_iversion(dir);
858 dir->i_mtime = dir->i_atime = current_time(dir);
859 exfat_truncate_atime(&dir->i_atime);
861 exfat_sync_inode(dir);
863 mark_inode_dirty(dir);
866 inode->i_mtime = inode->i_atime = current_time(inode);
867 exfat_truncate_atime(&inode->i_atime);
868 exfat_unhash_inode(inode);
869 exfat_d_version_set(dentry, inode_query_iversion(dir));
871 mutex_unlock(&EXFAT_SB(sb)->s_lock);
875 static int exfat_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
877 struct super_block *sb = dir->i_sb;
879 struct exfat_dir_entry info;
880 struct exfat_chain cdir;
884 mutex_lock(&EXFAT_SB(sb)->s_lock);
885 exfat_set_vol_flags(sb, VOL_DIRTY);
886 err = exfat_add_entry(dir, dentry->d_name.name, &cdir, TYPE_DIR,
888 exfat_set_vol_flags(sb, VOL_CLEAN);
892 inode_inc_iversion(dir);
893 dir->i_ctime = dir->i_mtime = current_time(dir);
895 exfat_sync_inode(dir);
897 mark_inode_dirty(dir);
900 i_pos = exfat_make_i_pos(&info);
901 inode = exfat_build_inode(sb, &info, i_pos);
903 err = PTR_ERR(inode);
907 inode_inc_iversion(inode);
908 inode->i_mtime = inode->i_atime = inode->i_ctime =
909 EXFAT_I(inode)->i_crtime = current_time(inode);
910 exfat_truncate_atime(&inode->i_atime);
911 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
913 d_instantiate(dentry, inode);
916 mutex_unlock(&EXFAT_SB(sb)->s_lock);
920 static int exfat_check_dir_empty(struct super_block *sb,
921 struct exfat_chain *p_dir)
923 int i, dentries_per_clu;
925 struct exfat_chain clu;
926 struct exfat_dentry *ep;
927 struct exfat_sb_info *sbi = EXFAT_SB(sb);
928 struct buffer_head *bh;
930 dentries_per_clu = sbi->dentries_per_clu;
932 exfat_chain_dup(&clu, p_dir);
934 while (clu.dir != EXFAT_EOF_CLUSTER) {
935 for (i = 0; i < dentries_per_clu; i++) {
936 ep = exfat_get_dentry(sb, &clu, i, &bh, NULL);
939 type = exfat_get_entry_type(ep);
941 if (type == TYPE_UNUSED)
944 if (type != TYPE_FILE && type != TYPE_DIR)
950 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
954 clu.dir = EXFAT_EOF_CLUSTER;
956 if (exfat_get_next_cluster(sb, &(clu.dir)))
964 static int exfat_rmdir(struct inode *dir, struct dentry *dentry)
966 struct inode *inode = dentry->d_inode;
967 struct exfat_dentry *ep;
968 struct exfat_chain cdir, clu_to_free;
969 struct super_block *sb = inode->i_sb;
970 struct exfat_sb_info *sbi = EXFAT_SB(sb);
971 struct exfat_inode_info *ei = EXFAT_I(inode);
972 struct buffer_head *bh;
974 int num_entries, entry, err;
976 mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
978 exfat_chain_dup(&cdir, &ei->dir);
981 if (ei->dir.dir == DIR_DELETED) {
982 exfat_msg(sb, KERN_ERR, "abnormal access to deleted dentry");
987 exfat_set_vol_flags(sb, VOL_DIRTY);
988 exfat_chain_set(&clu_to_free, ei->start_clu,
989 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi), ei->flags);
991 err = exfat_check_dir_empty(sb, &clu_to_free);
994 exfat_msg(sb, KERN_ERR,
995 "failed to exfat_check_dir_empty : err(%d)",
1000 ep = exfat_get_dentry(sb, &cdir, entry, &bh, §or);
1006 num_entries = exfat_count_ext_entries(sb, &cdir, entry, ep);
1007 if (num_entries < 0) {
1015 err = exfat_remove_entries(dir, &cdir, entry, 0, num_entries);
1017 exfat_msg(sb, KERN_ERR,
1018 "failed to exfat_remove_entries : err(%d)",
1022 ei->dir.dir = DIR_DELETED;
1023 exfat_set_vol_flags(sb, VOL_CLEAN);
1025 inode_inc_iversion(dir);
1026 dir->i_mtime = dir->i_atime = current_time(dir);
1027 exfat_truncate_atime(&dir->i_atime);
1028 if (IS_DIRSYNC(dir))
1029 exfat_sync_inode(dir);
1031 mark_inode_dirty(dir);
1035 inode->i_mtime = inode->i_atime = current_time(inode);
1036 exfat_truncate_atime(&inode->i_atime);
1037 exfat_unhash_inode(inode);
1038 exfat_d_version_set(dentry, inode_query_iversion(dir));
1040 mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
1044 static int exfat_rename_file(struct inode *inode, struct exfat_chain *p_dir,
1045 int oldentry, struct exfat_uni_name *p_uniname,
1046 struct exfat_inode_info *ei)
1048 int ret, num_old_entries, num_new_entries;
1049 sector_t sector_old, sector_new;
1050 struct exfat_dentry *epold, *epnew;
1051 struct super_block *sb = inode->i_sb;
1052 struct buffer_head *new_bh, *old_bh;
1053 int sync = IS_DIRSYNC(inode);
1055 epold = exfat_get_dentry(sb, p_dir, oldentry, &old_bh, §or_old);
1059 num_old_entries = exfat_count_ext_entries(sb, p_dir, oldentry, epold);
1060 if (num_old_entries < 0)
1064 num_new_entries = exfat_calc_num_entries(p_uniname);
1065 if (num_new_entries < 0)
1066 return num_new_entries;
1068 if (num_old_entries < num_new_entries) {
1072 exfat_find_empty_entry(inode, p_dir, num_new_entries);
1074 return newentry; /* -EIO or -ENOSPC */
1076 epnew = exfat_get_dentry(sb, p_dir, newentry, &new_bh,
1081 memcpy(epnew, epold, DENTRY_SIZE);
1082 if (exfat_get_entry_type(epnew) == TYPE_FILE) {
1083 epnew->dentry.file.attr |= cpu_to_le16(ATTR_ARCHIVE);
1084 ei->attr |= ATTR_ARCHIVE;
1086 exfat_update_bh(sb, new_bh, sync);
1090 epold = exfat_get_dentry(sb, p_dir, oldentry + 1, &old_bh,
1092 epnew = exfat_get_dentry(sb, p_dir, newentry + 1, &new_bh,
1094 if (!epold || !epnew)
1097 memcpy(epnew, epold, DENTRY_SIZE);
1098 exfat_update_bh(sb, new_bh, sync);
1102 ret = exfat_init_ext_entry(inode, p_dir, newentry,
1103 num_new_entries, p_uniname);
1107 exfat_remove_entries(inode, p_dir, oldentry, 0,
1109 ei->entry = newentry;
1111 if (exfat_get_entry_type(epold) == TYPE_FILE) {
1112 epold->dentry.file.attr |= cpu_to_le16(ATTR_ARCHIVE);
1113 ei->attr |= ATTR_ARCHIVE;
1115 exfat_update_bh(sb, old_bh, sync);
1117 ret = exfat_init_ext_entry(inode, p_dir, oldentry,
1118 num_new_entries, p_uniname);
1122 exfat_remove_entries(inode, p_dir, oldentry, num_new_entries,
1128 static int exfat_move_file(struct inode *inode, struct exfat_chain *p_olddir,
1129 int oldentry, struct exfat_chain *p_newdir,
1130 struct exfat_uni_name *p_uniname, struct exfat_inode_info *ei)
1132 int ret, newentry, num_new_entries, num_old_entries;
1133 sector_t sector_mov, sector_new;
1134 struct exfat_dentry *epmov, *epnew;
1135 struct super_block *sb = inode->i_sb;
1136 struct buffer_head *mov_bh, *new_bh;
1138 epmov = exfat_get_dentry(sb, p_olddir, oldentry, &mov_bh, §or_mov);
1142 /* check if the source and target directory is the same */
1143 if (exfat_get_entry_type(epmov) == TYPE_DIR &&
1144 le32_to_cpu(epmov->dentry.stream.start_clu) == p_newdir->dir)
1147 num_old_entries = exfat_count_ext_entries(sb, p_olddir, oldentry,
1149 if (num_old_entries < 0)
1153 num_new_entries = exfat_calc_num_entries(p_uniname);
1154 if (num_new_entries < 0)
1155 return num_new_entries;
1157 newentry = exfat_find_empty_entry(inode, p_newdir, num_new_entries);
1159 return newentry; /* -EIO or -ENOSPC */
1161 epnew = exfat_get_dentry(sb, p_newdir, newentry, &new_bh, §or_new);
1165 memcpy(epnew, epmov, DENTRY_SIZE);
1166 if (exfat_get_entry_type(epnew) == TYPE_FILE) {
1167 epnew->dentry.file.attr |= cpu_to_le16(ATTR_ARCHIVE);
1168 ei->attr |= ATTR_ARCHIVE;
1170 exfat_update_bh(sb, new_bh, IS_DIRSYNC(inode));
1174 epmov = exfat_get_dentry(sb, p_olddir, oldentry + 1, &mov_bh,
1176 epnew = exfat_get_dentry(sb, p_newdir, newentry + 1, &new_bh,
1178 if (!epmov || !epnew)
1181 memcpy(epnew, epmov, DENTRY_SIZE);
1182 exfat_update_bh(sb, new_bh, IS_DIRSYNC(inode));
1186 ret = exfat_init_ext_entry(inode, p_newdir, newentry, num_new_entries,
1191 exfat_remove_entries(inode, p_olddir, oldentry, 0, num_old_entries);
1193 exfat_chain_set(&ei->dir, p_newdir->dir, p_newdir->size,
1196 ei->entry = newentry;
1200 static void exfat_update_parent_info(struct exfat_inode_info *ei,
1201 struct inode *parent_inode)
1203 struct exfat_sb_info *sbi = EXFAT_SB(parent_inode->i_sb);
1204 struct exfat_inode_info *parent_ei = EXFAT_I(parent_inode);
1205 loff_t parent_isize = i_size_read(parent_inode);
1208 * the problem that struct exfat_inode_info caches wrong parent info.
1210 * because of flag-mismatch of ei->dir,
1211 * there is abnormal traversing cluster chain.
1213 if (unlikely(parent_ei->flags != ei->dir.flags ||
1214 parent_isize != EXFAT_CLU_TO_B(ei->dir.size, sbi) ||
1215 parent_ei->start_clu != ei->dir.dir)) {
1216 exfat_chain_set(&ei->dir, parent_ei->start_clu,
1217 EXFAT_B_TO_CLU_ROUND_UP(parent_isize, sbi),
1222 /* rename or move a old file into a new file */
1223 static int __exfat_rename(struct inode *old_parent_inode,
1224 struct exfat_inode_info *ei, struct inode *new_parent_inode,
1225 struct dentry *new_dentry)
1229 struct exfat_chain olddir, newdir;
1230 struct exfat_chain *p_dir = NULL;
1231 struct exfat_uni_name uni_name;
1232 struct exfat_dentry *ep;
1233 struct super_block *sb = old_parent_inode->i_sb;
1234 struct exfat_sb_info *sbi = EXFAT_SB(sb);
1235 const unsigned char *new_path = new_dentry->d_name.name;
1236 struct inode *new_inode = new_dentry->d_inode;
1238 struct exfat_inode_info *new_ei = NULL;
1239 unsigned int new_entry_type = TYPE_UNUSED;
1241 struct buffer_head *old_bh, *new_bh = NULL;
1243 /* check the validity of pointer parameters */
1244 if (new_path == NULL || strlen(new_path) == 0)
1247 if (ei->dir.dir == DIR_DELETED) {
1248 exfat_msg(sb, KERN_ERR,
1249 "abnormal access to deleted source dentry");
1253 exfat_update_parent_info(ei, old_parent_inode);
1255 exfat_chain_dup(&olddir, &ei->dir);
1258 ep = exfat_get_dentry(sb, &olddir, dentry, &old_bh, NULL);
1265 /* check whether new dir is existing directory and empty */
1268 new_ei = EXFAT_I(new_inode);
1270 if (new_ei->dir.dir == DIR_DELETED) {
1271 exfat_msg(sb, KERN_ERR,
1272 "abnormal access to deleted target dentry");
1276 exfat_update_parent_info(new_ei, new_parent_inode);
1278 p_dir = &(new_ei->dir);
1279 new_entry = new_ei->entry;
1280 ep = exfat_get_dentry(sb, p_dir, new_entry, &new_bh, NULL);
1284 new_entry_type = exfat_get_entry_type(ep);
1287 /* if new_inode exists, update ei */
1288 if (new_entry_type == TYPE_DIR) {
1289 struct exfat_chain new_clu;
1291 new_clu.dir = new_ei->start_clu;
1293 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode),
1295 new_clu.flags = new_ei->flags;
1297 ret = exfat_check_dir_empty(sb, &new_clu);
1303 /* check the validity of directory name in the given new pathname */
1304 ret = exfat_resolve_path(new_parent_inode, new_path, &newdir,
1309 exfat_set_vol_flags(sb, VOL_DIRTY);
1311 if (olddir.dir == newdir.dir)
1312 ret = exfat_rename_file(new_parent_inode, &olddir, dentry,
1315 ret = exfat_move_file(new_parent_inode, &olddir, dentry,
1316 &newdir, &uni_name, ei);
1318 if (!ret && new_inode) {
1319 /* delete entries of new_dir */
1320 ep = exfat_get_dentry(sb, p_dir, new_entry, &new_bh, NULL);
1326 num_entries = exfat_count_ext_entries(sb, p_dir, new_entry, ep);
1327 if (num_entries < 0) {
1333 if (exfat_remove_entries(new_inode, p_dir, new_entry, 0,
1339 /* Free the clusters if new_inode is a dir(as if exfat_rmdir) */
1340 if (new_entry_type == TYPE_DIR) {
1341 /* new_ei, new_clu_to_free */
1342 struct exfat_chain new_clu_to_free;
1344 exfat_chain_set(&new_clu_to_free, new_ei->start_clu,
1345 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode),
1346 sbi), new_ei->flags);
1348 if (exfat_free_cluster(new_inode, &new_clu_to_free)) {
1349 /* just set I/O error only */
1353 i_size_write(new_inode, 0);
1354 new_ei->start_clu = EXFAT_EOF_CLUSTER;
1355 new_ei->flags = ALLOC_NO_FAT_CHAIN;
1358 /* Update new_inode ei
1359 * Prevent syncing removed new_inode
1360 * (new_ei is already initialized above code ("if (new_inode)")
1362 new_ei->dir.dir = DIR_DELETED;
1364 exfat_set_vol_flags(sb, VOL_CLEAN);
1369 static int exfat_rename(struct inode *old_dir, struct dentry *old_dentry,
1370 struct inode *new_dir, struct dentry *new_dentry,
1373 struct inode *old_inode, *new_inode;
1374 struct super_block *sb = old_dir->i_sb;
1379 * The VFS already checks for existence, so for local filesystems
1380 * the RENAME_NOREPLACE implementation is equivalent to plain rename.
1381 * Don't support any other flags
1383 if (flags & ~RENAME_NOREPLACE)
1386 mutex_lock(&EXFAT_SB(sb)->s_lock);
1387 old_inode = old_dentry->d_inode;
1388 new_inode = new_dentry->d_inode;
1390 err = __exfat_rename(old_dir, EXFAT_I(old_inode), new_dir, new_dentry);
1394 inode_inc_iversion(new_dir);
1395 new_dir->i_ctime = new_dir->i_mtime = new_dir->i_atime =
1396 EXFAT_I(new_dir)->i_crtime = current_time(new_dir);
1397 exfat_truncate_atime(&new_dir->i_atime);
1398 if (IS_DIRSYNC(new_dir))
1399 exfat_sync_inode(new_dir);
1401 mark_inode_dirty(new_dir);
1403 i_pos = ((loff_t)EXFAT_I(old_inode)->dir.dir << 32) |
1404 (EXFAT_I(old_inode)->entry & 0xffffffff);
1405 exfat_unhash_inode(old_inode);
1406 exfat_hash_inode(old_inode, i_pos);
1407 if (IS_DIRSYNC(new_dir))
1408 exfat_sync_inode(old_inode);
1410 mark_inode_dirty(old_inode);
1412 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
1413 drop_nlink(old_dir);
1418 inode_inc_iversion(old_dir);
1419 old_dir->i_ctime = old_dir->i_mtime = current_time(old_dir);
1420 if (IS_DIRSYNC(old_dir))
1421 exfat_sync_inode(old_dir);
1423 mark_inode_dirty(old_dir);
1426 exfat_unhash_inode(new_inode);
1428 /* skip drop_nlink if new_inode already has been dropped */
1429 if (new_inode->i_nlink) {
1430 drop_nlink(new_inode);
1431 if (S_ISDIR(new_inode->i_mode))
1432 drop_nlink(new_inode);
1434 exfat_msg(sb, KERN_WARNING,
1435 "abnormal access to an inode dropped");
1436 WARN_ON(new_inode->i_nlink == 0);
1438 new_inode->i_ctime = EXFAT_I(new_inode)->i_crtime =
1439 current_time(new_inode);
1443 mutex_unlock(&EXFAT_SB(sb)->s_lock);
1447 const struct inode_operations exfat_dir_inode_operations = {
1448 .create = exfat_create,
1449 .lookup = exfat_lookup,
1450 .unlink = exfat_unlink,
1451 .mkdir = exfat_mkdir,
1452 .rmdir = exfat_rmdir,
1453 .rename = exfat_rename,
1454 .setattr = exfat_setattr,
1455 .getattr = exfat_getattr,