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 upto 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 memcpy(&hint_femp, &ei->hint_femp,
322 sizeof(struct exfat_hint_femp));
323 ei->hint_femp.eidx = EXFAT_HINT_NONE;
326 while ((dentry = exfat_search_empty_slot(sb, &hint_femp, p_dir,
331 if (exfat_check_max_dentries(inode))
334 /* we trust p_dir->size regardless of FAT type */
335 if (exfat_find_last_cluster(sb, p_dir, &last_clu))
339 * Allocate new cluster to this directory
341 exfat_chain_set(&clu, last_clu + 1, 0, p_dir->flags);
343 /* allocate a cluster */
344 ret = exfat_alloc_cluster(inode, 1, &clu);
348 if (exfat_zeroed_cluster(inode, clu.dir))
351 /* append to the FAT chain */
352 if (clu.flags != p_dir->flags) {
353 /* no-fat-chain bit is disabled,
354 * so fat-chain should be synced with alloc-bitmap
356 exfat_chain_cont_cluster(sb, p_dir->dir, p_dir->size);
357 p_dir->flags = ALLOC_FAT_CHAIN;
358 hint_femp.cur.flags = ALLOC_FAT_CHAIN;
361 if (clu.flags == ALLOC_FAT_CHAIN)
362 if (exfat_ent_set(sb, last_clu, clu.dir))
365 if (hint_femp.eidx == EXFAT_HINT_NONE) {
366 /* the special case that new dentry
367 * should be allocated from the start of new cluster
369 hint_femp.eidx = EXFAT_B_TO_DEN_IDX(p_dir->size, sbi);
370 hint_femp.count = sbi->dentries_per_clu;
372 exfat_chain_set(&hint_femp.cur, clu.dir, 0, clu.flags);
374 hint_femp.cur.size++;
376 size = EXFAT_CLU_TO_B(p_dir->size, sbi);
378 /* update the directory entry */
379 if (p_dir->dir != sbi->root_dir) {
380 struct buffer_head *bh;
382 ep = exfat_get_dentry(sb,
383 &(ei->dir), ei->entry + 1, &bh, §or);
387 ep->dentry.stream.valid_size = cpu_to_le64(size);
388 ep->dentry.stream.size = ep->dentry.stream.valid_size;
389 ep->dentry.stream.flags = p_dir->flags;
390 exfat_update_bh(sb, bh, IS_DIRSYNC(inode));
392 if (exfat_update_dir_chksum(inode, &(ei->dir),
397 /* directory inode should be updated in here */
398 i_size_write(inode, size);
399 EXFAT_I(inode)->i_size_ondisk += sbi->cluster_size;
400 EXFAT_I(inode)->i_size_aligned += sbi->cluster_size;
401 EXFAT_I(inode)->flags = p_dir->flags;
402 inode->i_blocks += 1 << sbi->sect_per_clus_bits;
409 * Name Resolution Functions :
410 * Zero if it was successful; otherwise nonzero.
412 static int __exfat_resolve_path(struct inode *inode, const unsigned char *path,
413 struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
417 int lossy = NLS_NAME_NO_LOSSY;
418 struct super_block *sb = inode->i_sb;
419 struct exfat_sb_info *sbi = EXFAT_SB(sb);
420 struct exfat_inode_info *ei = EXFAT_I(inode);
422 /* strip all trailing periods */
423 namelen = exfat_striptail_len(strlen(path), path);
427 if (strlen(path) > (MAX_NAME_LENGTH * MAX_CHARSET_SIZE))
428 return -ENAMETOOLONG;
431 * strip all leading spaces :
432 * "MS windows 7" supports leading spaces.
433 * So we should skip this preprocessing for compatibility.
436 /* file name conversion :
437 * If lookup case, we allow bad-name for compatibility.
439 namelen = exfat_nls_to_utf16(sb, path, namelen, p_uniname,
442 return namelen; /* return error value */
444 if ((lossy && !lookup) || !namelen)
447 exfat_chain_set(p_dir, ei->start_clu,
448 EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags);
453 static inline int exfat_resolve_path(struct inode *inode,
454 const unsigned char *path, struct exfat_chain *dir,
455 struct exfat_uni_name *uni)
457 return __exfat_resolve_path(inode, path, dir, uni, 0);
460 static inline int exfat_resolve_path_for_lookup(struct inode *inode,
461 const unsigned char *path, struct exfat_chain *dir,
462 struct exfat_uni_name *uni)
464 return __exfat_resolve_path(inode, path, dir, uni, 1);
467 static inline loff_t exfat_make_i_pos(struct exfat_dir_entry *info)
469 return ((loff_t) info->dir.dir << 32) | (info->entry & 0xffffffff);
472 static int exfat_add_entry(struct inode *inode, const char *path,
473 struct exfat_chain *p_dir, unsigned int type,
474 struct exfat_dir_entry *info)
476 int ret, dentry, num_entries;
477 struct super_block *sb = inode->i_sb;
478 struct exfat_sb_info *sbi = EXFAT_SB(sb);
479 struct exfat_uni_name uniname;
480 struct exfat_chain clu;
482 unsigned int start_clu = EXFAT_FREE_CLUSTER;
484 ret = exfat_resolve_path(inode, path, p_dir, &uniname);
488 num_entries = exfat_calc_num_entries(&uniname);
489 if (num_entries < 0) {
494 /* exfat_find_empty_entry must be called before alloc_cluster() */
495 dentry = exfat_find_empty_entry(inode, p_dir, num_entries);
497 ret = dentry; /* -EIO or -ENOSPC */
501 if (type == TYPE_DIR) {
502 ret = exfat_alloc_new_dir(inode, &clu);
506 clu_size = sbi->cluster_size;
509 /* update the directory entry */
510 /* fill the dos name directory entry information of the created file.
511 * the first cluster is not determined yet. (0)
513 ret = exfat_init_dir_entry(inode, p_dir, dentry, type,
514 start_clu, clu_size);
518 ret = exfat_init_ext_entry(inode, p_dir, dentry, num_entries, &uniname);
522 memcpy(&info->dir, p_dir, sizeof(struct exfat_chain));
523 info->entry = dentry;
524 info->flags = ALLOC_NO_FAT_CHAIN;
527 if (type == TYPE_FILE) {
528 info->attr = ATTR_ARCHIVE;
529 info->start_clu = EXFAT_EOF_CLUSTER;
531 info->num_subdirs = 0;
534 struct exfat_chain cdir;
536 info->attr = ATTR_SUBDIR;
537 info->start_clu = start_clu;
538 info->size = clu_size;
540 exfat_chain_set(&cdir, info->start_clu,
541 EXFAT_B_TO_CLU(info->size, sbi), info->flags);
542 count = exfat_count_dir_entries(sb, &cdir);
545 info->num_subdirs = count + EXFAT_MIN_SUBDIR;
547 memset(&info->crtime, 0, sizeof(info->crtime));
548 memset(&info->mtime, 0, sizeof(info->mtime));
549 memset(&info->atime, 0, sizeof(info->atime));
554 static int exfat_create(struct inode *dir, struct dentry *dentry, umode_t mode,
557 struct super_block *sb = dir->i_sb;
559 struct exfat_chain cdir;
560 struct exfat_dir_entry info;
564 mutex_lock(&EXFAT_SB(sb)->s_lock);
565 exfat_set_vol_flags(sb, VOL_DIRTY);
566 err = exfat_add_entry(dir, dentry->d_name.name, &cdir, TYPE_FILE,
568 exfat_set_vol_flags(sb, VOL_CLEAN);
572 inode_inc_iversion(dir);
573 dir->i_ctime = dir->i_mtime = current_time(dir);
575 exfat_sync_inode(dir);
577 mark_inode_dirty(dir);
579 i_pos = exfat_make_i_pos(&info);
580 inode = exfat_build_inode(sb, &info, i_pos);
584 inode_inc_iversion(inode);
585 inode->i_mtime = inode->i_atime = inode->i_ctime =
586 EXFAT_I(inode)->i_crtime = current_time(inode);
587 exfat_truncate_atime(&inode->i_atime);
588 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
590 d_instantiate(dentry, inode);
592 mutex_unlock(&EXFAT_SB(sb)->s_lock);
597 static int exfat_find(struct inode *dir, struct qstr *qname,
598 struct exfat_dir_entry *info)
600 int ret, dentry, num_entries, count;
601 struct exfat_chain cdir;
602 struct exfat_uni_name uni_name;
603 struct super_block *sb = dir->i_sb;
604 struct exfat_sb_info *sbi = EXFAT_SB(sb);
605 struct exfat_inode_info *ei = EXFAT_I(dir);
610 /* check the validity of directory name in the given pathname */
611 ret = exfat_resolve_path_for_lookup(dir, qname->name, &cdir, &uni_name);
615 num_entries = exfat_calc_num_entries(&uni_name);
619 /* check the validation of hint_stat and initialize it if required */
620 if (ei->version != (inode_peek_iversion_raw(dir) & 0xffffffff)) {
621 ei->hint_stat.clu = cdir.dir;
622 ei->hint_stat.eidx = 0;
623 ei->version = (inode_peek_iversion_raw(dir) & 0xffffffff);
624 ei->hint_femp.eidx = EXFAT_HINT_NONE;
627 /* search the file name for directories */
628 dentry = exfat_find_dir_entry(sb, ei, &cdir, &uni_name,
629 num_entries, TYPE_ALL);
631 if ((dentry < 0) && (dentry != -EEXIST))
632 return dentry; /* -error value */
634 memcpy(&info->dir, &cdir.dir, sizeof(struct exfat_chain));
635 info->entry = dentry;
636 info->num_subdirs = 0;
638 /* root directory itself */
639 if (unlikely(dentry == -EEXIST)) {
642 info->type = TYPE_DIR;
643 info->attr = ATTR_SUBDIR;
644 info->flags = ALLOC_FAT_CHAIN;
645 info->start_clu = sbi->root_dir;
646 memset(&info->crtime, 0, sizeof(info->crtime));
647 memset(&info->mtime, 0, sizeof(info->mtime));
648 memset(&info->atime, 0, sizeof(info->atime));
650 exfat_chain_set(&cdir, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
651 if (exfat_count_num_clusters(sb, &cdir, &num_clu))
653 info->size = num_clu << sbi->cluster_size_bits;
655 count = exfat_count_dir_entries(sb, &cdir);
659 info->num_subdirs = count;
661 struct exfat_dentry *ep, *ep2;
662 struct exfat_entry_set_cache *es;
664 es = exfat_get_dentry_set(sb, &cdir, dentry, ES_2_ENTRIES);
667 ep = exfat_get_dentry_cached(es, 0);
668 ep2 = exfat_get_dentry_cached(es, 1);
670 info->type = exfat_get_entry_type(ep);
671 info->attr = le16_to_cpu(ep->dentry.file.attr);
672 info->size = le64_to_cpu(ep2->dentry.stream.valid_size);
673 if ((info->type == TYPE_FILE) && (info->size == 0)) {
674 info->flags = ALLOC_NO_FAT_CHAIN;
675 info->start_clu = EXFAT_EOF_CLUSTER;
677 info->flags = ep2->dentry.stream.flags;
679 le32_to_cpu(ep2->dentry.stream.start_clu);
682 if (ei->start_clu == EXFAT_FREE_CLUSTER) {
684 "non-zero size file starts with zero cluster (size : %llu, p_dir : %u, entry : 0x%08x)",
685 i_size_read(dir), ei->dir.dir, ei->entry);
686 exfat_free_dentry_set(es, false);
690 exfat_get_entry_time(sbi, &info->crtime,
691 ep->dentry.file.create_tz,
692 ep->dentry.file.create_time,
693 ep->dentry.file.create_date,
694 ep->dentry.file.create_time_cs);
695 exfat_get_entry_time(sbi, &info->mtime,
696 ep->dentry.file.modify_tz,
697 ep->dentry.file.modify_time,
698 ep->dentry.file.modify_date,
699 ep->dentry.file.modify_time_cs);
700 exfat_get_entry_time(sbi, &info->atime,
701 ep->dentry.file.access_tz,
702 ep->dentry.file.access_time,
703 ep->dentry.file.access_date,
705 exfat_free_dentry_set(es, false);
707 if (info->type == TYPE_DIR) {
708 exfat_chain_set(&cdir, info->start_clu,
709 EXFAT_B_TO_CLU(info->size, sbi), info->flags);
710 count = exfat_count_dir_entries(sb, &cdir);
714 info->num_subdirs = count + EXFAT_MIN_SUBDIR;
720 static int exfat_d_anon_disconn(struct dentry *dentry)
722 return IS_ROOT(dentry) && (dentry->d_flags & DCACHE_DISCONNECTED);
725 static struct dentry *exfat_lookup(struct inode *dir, struct dentry *dentry,
728 struct super_block *sb = dir->i_sb;
730 struct dentry *alias;
731 struct exfat_dir_entry info;
736 mutex_lock(&EXFAT_SB(sb)->s_lock);
737 err = exfat_find(dir, &dentry->d_name, &info);
739 if (err == -ENOENT) {
746 i_pos = exfat_make_i_pos(&info);
747 inode = exfat_build_inode(sb, &info, i_pos);
749 err = PTR_ERR(inode);
753 i_mode = inode->i_mode;
754 alias = d_find_alias(inode);
757 * Checking "alias->d_parent == dentry->d_parent" to make sure
758 * FS is not corrupted (especially double linked dir).
760 if (alias && alias->d_parent == dentry->d_parent &&
761 !exfat_d_anon_disconn(alias)) {
764 * Unhashed alias is able to exist because of revalidate()
765 * called by lookup_fast. You can easily make this status
766 * by calling create and lookup concurrently
767 * In such case, we reuse an alias instead of new dentry
769 if (d_unhashed(alias)) {
770 WARN_ON(alias->d_name.hash_len !=
771 dentry->d_name.hash_len);
772 exfat_info(sb, "rehashed a dentry(%p) in read lookup",
776 } else if (!S_ISDIR(i_mode)) {
778 * This inode has non anonymous-DCACHE_DISCONNECTED
779 * dentry. This means, the user did ->lookup() by an
780 * another name (longname vs 8.3 alias of it) in past.
782 * Switch to new one for reason of locality if possible.
784 d_move(alias, dentry);
787 mutex_unlock(&EXFAT_SB(sb)->s_lock);
792 mutex_unlock(&EXFAT_SB(sb)->s_lock);
794 exfat_d_version_set(dentry, inode_query_iversion(dir));
796 return d_splice_alias(inode, dentry);
798 mutex_unlock(&EXFAT_SB(sb)->s_lock);
802 /* remove an entry, BUT don't truncate */
803 static int exfat_unlink(struct inode *dir, struct dentry *dentry)
805 struct exfat_chain cdir;
806 struct exfat_dentry *ep;
807 struct super_block *sb = dir->i_sb;
808 struct inode *inode = dentry->d_inode;
809 struct exfat_inode_info *ei = EXFAT_I(inode);
810 struct buffer_head *bh;
812 int num_entries, entry, err = 0;
814 mutex_lock(&EXFAT_SB(sb)->s_lock);
815 exfat_chain_dup(&cdir, &ei->dir);
817 if (ei->dir.dir == DIR_DELETED) {
818 exfat_err(sb, "abnormal access to deleted dentry");
823 ep = exfat_get_dentry(sb, &cdir, entry, &bh, §or);
828 num_entries = exfat_count_ext_entries(sb, &cdir, entry, ep);
829 if (num_entries < 0) {
837 exfat_set_vol_flags(sb, VOL_DIRTY);
838 /* update the directory entry */
839 if (exfat_remove_entries(dir, &cdir, entry, 0, num_entries)) {
844 /* This doesn't modify ei */
845 ei->dir.dir = DIR_DELETED;
846 exfat_set_vol_flags(sb, VOL_CLEAN);
848 inode_inc_iversion(dir);
849 dir->i_mtime = dir->i_atime = current_time(dir);
850 exfat_truncate_atime(&dir->i_atime);
852 exfat_sync_inode(dir);
854 mark_inode_dirty(dir);
857 inode->i_mtime = inode->i_atime = current_time(inode);
858 exfat_truncate_atime(&inode->i_atime);
859 exfat_unhash_inode(inode);
860 exfat_d_version_set(dentry, inode_query_iversion(dir));
862 mutex_unlock(&EXFAT_SB(sb)->s_lock);
866 static int exfat_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
868 struct super_block *sb = dir->i_sb;
870 struct exfat_dir_entry info;
871 struct exfat_chain cdir;
875 mutex_lock(&EXFAT_SB(sb)->s_lock);
876 exfat_set_vol_flags(sb, VOL_DIRTY);
877 err = exfat_add_entry(dir, dentry->d_name.name, &cdir, TYPE_DIR,
879 exfat_set_vol_flags(sb, VOL_CLEAN);
883 inode_inc_iversion(dir);
884 dir->i_ctime = dir->i_mtime = current_time(dir);
886 exfat_sync_inode(dir);
888 mark_inode_dirty(dir);
891 i_pos = exfat_make_i_pos(&info);
892 inode = exfat_build_inode(sb, &info, i_pos);
894 err = PTR_ERR(inode);
898 inode_inc_iversion(inode);
899 inode->i_mtime = inode->i_atime = inode->i_ctime =
900 EXFAT_I(inode)->i_crtime = current_time(inode);
901 exfat_truncate_atime(&inode->i_atime);
902 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
904 d_instantiate(dentry, inode);
907 mutex_unlock(&EXFAT_SB(sb)->s_lock);
911 static int exfat_check_dir_empty(struct super_block *sb,
912 struct exfat_chain *p_dir)
914 int i, dentries_per_clu;
916 struct exfat_chain clu;
917 struct exfat_dentry *ep;
918 struct exfat_sb_info *sbi = EXFAT_SB(sb);
919 struct buffer_head *bh;
921 dentries_per_clu = sbi->dentries_per_clu;
923 exfat_chain_dup(&clu, p_dir);
925 while (clu.dir != EXFAT_EOF_CLUSTER) {
926 for (i = 0; i < dentries_per_clu; i++) {
927 ep = exfat_get_dentry(sb, &clu, i, &bh, NULL);
930 type = exfat_get_entry_type(ep);
932 if (type == TYPE_UNUSED)
935 if (type != TYPE_FILE && type != TYPE_DIR)
941 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
945 clu.dir = EXFAT_EOF_CLUSTER;
947 if (exfat_get_next_cluster(sb, &(clu.dir)))
955 static int exfat_rmdir(struct inode *dir, struct dentry *dentry)
957 struct inode *inode = dentry->d_inode;
958 struct exfat_dentry *ep;
959 struct exfat_chain cdir, clu_to_free;
960 struct super_block *sb = inode->i_sb;
961 struct exfat_sb_info *sbi = EXFAT_SB(sb);
962 struct exfat_inode_info *ei = EXFAT_I(inode);
963 struct buffer_head *bh;
965 int num_entries, entry, err;
967 mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
969 exfat_chain_dup(&cdir, &ei->dir);
972 if (ei->dir.dir == DIR_DELETED) {
973 exfat_err(sb, "abnormal access to deleted dentry");
978 exfat_set_vol_flags(sb, VOL_DIRTY);
979 exfat_chain_set(&clu_to_free, ei->start_clu,
980 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi), ei->flags);
982 err = exfat_check_dir_empty(sb, &clu_to_free);
985 exfat_err(sb, "failed to exfat_check_dir_empty : err(%d)",
990 ep = exfat_get_dentry(sb, &cdir, entry, &bh, §or);
996 num_entries = exfat_count_ext_entries(sb, &cdir, entry, ep);
997 if (num_entries < 0) {
1005 err = exfat_remove_entries(dir, &cdir, entry, 0, num_entries);
1007 exfat_err(sb, "failed to exfat_remove_entries : err(%d)", err);
1010 ei->dir.dir = DIR_DELETED;
1011 exfat_set_vol_flags(sb, VOL_CLEAN);
1013 inode_inc_iversion(dir);
1014 dir->i_mtime = dir->i_atime = current_time(dir);
1015 exfat_truncate_atime(&dir->i_atime);
1016 if (IS_DIRSYNC(dir))
1017 exfat_sync_inode(dir);
1019 mark_inode_dirty(dir);
1023 inode->i_mtime = inode->i_atime = current_time(inode);
1024 exfat_truncate_atime(&inode->i_atime);
1025 exfat_unhash_inode(inode);
1026 exfat_d_version_set(dentry, inode_query_iversion(dir));
1028 mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
1032 static int exfat_rename_file(struct inode *inode, struct exfat_chain *p_dir,
1033 int oldentry, struct exfat_uni_name *p_uniname,
1034 struct exfat_inode_info *ei)
1036 int ret, num_old_entries, num_new_entries;
1037 sector_t sector_old, sector_new;
1038 struct exfat_dentry *epold, *epnew;
1039 struct super_block *sb = inode->i_sb;
1040 struct buffer_head *new_bh, *old_bh;
1041 int sync = IS_DIRSYNC(inode);
1043 epold = exfat_get_dentry(sb, p_dir, oldentry, &old_bh, §or_old);
1047 num_old_entries = exfat_count_ext_entries(sb, p_dir, oldentry, epold);
1048 if (num_old_entries < 0)
1052 num_new_entries = exfat_calc_num_entries(p_uniname);
1053 if (num_new_entries < 0)
1054 return num_new_entries;
1056 if (num_old_entries < num_new_entries) {
1060 exfat_find_empty_entry(inode, p_dir, num_new_entries);
1062 return newentry; /* -EIO or -ENOSPC */
1064 epnew = exfat_get_dentry(sb, p_dir, newentry, &new_bh,
1069 memcpy(epnew, epold, DENTRY_SIZE);
1070 if (exfat_get_entry_type(epnew) == TYPE_FILE) {
1071 epnew->dentry.file.attr |= cpu_to_le16(ATTR_ARCHIVE);
1072 ei->attr |= ATTR_ARCHIVE;
1074 exfat_update_bh(sb, new_bh, sync);
1078 epold = exfat_get_dentry(sb, p_dir, oldentry + 1, &old_bh,
1080 epnew = exfat_get_dentry(sb, p_dir, newentry + 1, &new_bh,
1082 if (!epold || !epnew)
1085 memcpy(epnew, epold, DENTRY_SIZE);
1086 exfat_update_bh(sb, new_bh, sync);
1090 ret = exfat_init_ext_entry(inode, p_dir, newentry,
1091 num_new_entries, p_uniname);
1095 exfat_remove_entries(inode, p_dir, oldentry, 0,
1097 ei->entry = newentry;
1099 if (exfat_get_entry_type(epold) == TYPE_FILE) {
1100 epold->dentry.file.attr |= cpu_to_le16(ATTR_ARCHIVE);
1101 ei->attr |= ATTR_ARCHIVE;
1103 exfat_update_bh(sb, old_bh, sync);
1105 ret = exfat_init_ext_entry(inode, p_dir, oldentry,
1106 num_new_entries, p_uniname);
1110 exfat_remove_entries(inode, p_dir, oldentry, num_new_entries,
1116 static int exfat_move_file(struct inode *inode, struct exfat_chain *p_olddir,
1117 int oldentry, struct exfat_chain *p_newdir,
1118 struct exfat_uni_name *p_uniname, struct exfat_inode_info *ei)
1120 int ret, newentry, num_new_entries, num_old_entries;
1121 sector_t sector_mov, sector_new;
1122 struct exfat_dentry *epmov, *epnew;
1123 struct super_block *sb = inode->i_sb;
1124 struct buffer_head *mov_bh, *new_bh;
1126 epmov = exfat_get_dentry(sb, p_olddir, oldentry, &mov_bh, §or_mov);
1130 /* check if the source and target directory is the same */
1131 if (exfat_get_entry_type(epmov) == TYPE_DIR &&
1132 le32_to_cpu(epmov->dentry.stream.start_clu) == p_newdir->dir)
1135 num_old_entries = exfat_count_ext_entries(sb, p_olddir, oldentry,
1137 if (num_old_entries < 0)
1141 num_new_entries = exfat_calc_num_entries(p_uniname);
1142 if (num_new_entries < 0)
1143 return num_new_entries;
1145 newentry = exfat_find_empty_entry(inode, p_newdir, num_new_entries);
1147 return newentry; /* -EIO or -ENOSPC */
1149 epnew = exfat_get_dentry(sb, p_newdir, newentry, &new_bh, §or_new);
1153 memcpy(epnew, epmov, DENTRY_SIZE);
1154 if (exfat_get_entry_type(epnew) == TYPE_FILE) {
1155 epnew->dentry.file.attr |= cpu_to_le16(ATTR_ARCHIVE);
1156 ei->attr |= ATTR_ARCHIVE;
1158 exfat_update_bh(sb, new_bh, IS_DIRSYNC(inode));
1162 epmov = exfat_get_dentry(sb, p_olddir, oldentry + 1, &mov_bh,
1164 epnew = exfat_get_dentry(sb, p_newdir, newentry + 1, &new_bh,
1166 if (!epmov || !epnew)
1169 memcpy(epnew, epmov, DENTRY_SIZE);
1170 exfat_update_bh(sb, new_bh, IS_DIRSYNC(inode));
1174 ret = exfat_init_ext_entry(inode, p_newdir, newentry, num_new_entries,
1179 exfat_remove_entries(inode, p_olddir, oldentry, 0, num_old_entries);
1181 exfat_chain_set(&ei->dir, p_newdir->dir, p_newdir->size,
1184 ei->entry = newentry;
1188 static void exfat_update_parent_info(struct exfat_inode_info *ei,
1189 struct inode *parent_inode)
1191 struct exfat_sb_info *sbi = EXFAT_SB(parent_inode->i_sb);
1192 struct exfat_inode_info *parent_ei = EXFAT_I(parent_inode);
1193 loff_t parent_isize = i_size_read(parent_inode);
1196 * the problem that struct exfat_inode_info caches wrong parent info.
1198 * because of flag-mismatch of ei->dir,
1199 * there is abnormal traversing cluster chain.
1201 if (unlikely(parent_ei->flags != ei->dir.flags ||
1202 parent_isize != EXFAT_CLU_TO_B(ei->dir.size, sbi) ||
1203 parent_ei->start_clu != ei->dir.dir)) {
1204 exfat_chain_set(&ei->dir, parent_ei->start_clu,
1205 EXFAT_B_TO_CLU_ROUND_UP(parent_isize, sbi),
1210 /* rename or move a old file into a new file */
1211 static int __exfat_rename(struct inode *old_parent_inode,
1212 struct exfat_inode_info *ei, struct inode *new_parent_inode,
1213 struct dentry *new_dentry)
1217 struct exfat_chain olddir, newdir;
1218 struct exfat_chain *p_dir = NULL;
1219 struct exfat_uni_name uni_name;
1220 struct exfat_dentry *ep;
1221 struct super_block *sb = old_parent_inode->i_sb;
1222 struct exfat_sb_info *sbi = EXFAT_SB(sb);
1223 const unsigned char *new_path = new_dentry->d_name.name;
1224 struct inode *new_inode = new_dentry->d_inode;
1226 struct exfat_inode_info *new_ei = NULL;
1227 unsigned int new_entry_type = TYPE_UNUSED;
1229 struct buffer_head *old_bh, *new_bh = NULL;
1231 /* check the validity of pointer parameters */
1232 if (new_path == NULL || strlen(new_path) == 0)
1235 if (ei->dir.dir == DIR_DELETED) {
1236 exfat_err(sb, "abnormal access to deleted source dentry");
1240 exfat_update_parent_info(ei, old_parent_inode);
1242 exfat_chain_dup(&olddir, &ei->dir);
1245 ep = exfat_get_dentry(sb, &olddir, dentry, &old_bh, NULL);
1252 /* check whether new dir is existing directory and empty */
1255 new_ei = EXFAT_I(new_inode);
1257 if (new_ei->dir.dir == DIR_DELETED) {
1258 exfat_err(sb, "abnormal access to deleted target dentry");
1262 exfat_update_parent_info(new_ei, new_parent_inode);
1264 p_dir = &(new_ei->dir);
1265 new_entry = new_ei->entry;
1266 ep = exfat_get_dentry(sb, p_dir, new_entry, &new_bh, NULL);
1270 new_entry_type = exfat_get_entry_type(ep);
1273 /* if new_inode exists, update ei */
1274 if (new_entry_type == TYPE_DIR) {
1275 struct exfat_chain new_clu;
1277 new_clu.dir = new_ei->start_clu;
1279 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode),
1281 new_clu.flags = new_ei->flags;
1283 ret = exfat_check_dir_empty(sb, &new_clu);
1289 /* check the validity of directory name in the given new pathname */
1290 ret = exfat_resolve_path(new_parent_inode, new_path, &newdir,
1295 exfat_set_vol_flags(sb, VOL_DIRTY);
1297 if (olddir.dir == newdir.dir)
1298 ret = exfat_rename_file(new_parent_inode, &olddir, dentry,
1301 ret = exfat_move_file(new_parent_inode, &olddir, dentry,
1302 &newdir, &uni_name, ei);
1304 if (!ret && new_inode) {
1305 /* delete entries of new_dir */
1306 ep = exfat_get_dentry(sb, p_dir, new_entry, &new_bh, NULL);
1312 num_entries = exfat_count_ext_entries(sb, p_dir, new_entry, ep);
1313 if (num_entries < 0) {
1319 if (exfat_remove_entries(new_inode, p_dir, new_entry, 0,
1325 /* Free the clusters if new_inode is a dir(as if exfat_rmdir) */
1326 if (new_entry_type == TYPE_DIR) {
1327 /* new_ei, new_clu_to_free */
1328 struct exfat_chain new_clu_to_free;
1330 exfat_chain_set(&new_clu_to_free, new_ei->start_clu,
1331 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode),
1332 sbi), new_ei->flags);
1334 if (exfat_free_cluster(new_inode, &new_clu_to_free)) {
1335 /* just set I/O error only */
1339 i_size_write(new_inode, 0);
1340 new_ei->start_clu = EXFAT_EOF_CLUSTER;
1341 new_ei->flags = ALLOC_NO_FAT_CHAIN;
1344 /* Update new_inode ei
1345 * Prevent syncing removed new_inode
1346 * (new_ei is already initialized above code ("if (new_inode)")
1348 new_ei->dir.dir = DIR_DELETED;
1350 exfat_set_vol_flags(sb, VOL_CLEAN);
1355 static int exfat_rename(struct inode *old_dir, struct dentry *old_dentry,
1356 struct inode *new_dir, struct dentry *new_dentry,
1359 struct inode *old_inode, *new_inode;
1360 struct super_block *sb = old_dir->i_sb;
1365 * The VFS already checks for existence, so for local filesystems
1366 * the RENAME_NOREPLACE implementation is equivalent to plain rename.
1367 * Don't support any other flags
1369 if (flags & ~RENAME_NOREPLACE)
1372 mutex_lock(&EXFAT_SB(sb)->s_lock);
1373 old_inode = old_dentry->d_inode;
1374 new_inode = new_dentry->d_inode;
1376 err = __exfat_rename(old_dir, EXFAT_I(old_inode), new_dir, new_dentry);
1380 inode_inc_iversion(new_dir);
1381 new_dir->i_ctime = new_dir->i_mtime = new_dir->i_atime =
1382 EXFAT_I(new_dir)->i_crtime = current_time(new_dir);
1383 exfat_truncate_atime(&new_dir->i_atime);
1384 if (IS_DIRSYNC(new_dir))
1385 exfat_sync_inode(new_dir);
1387 mark_inode_dirty(new_dir);
1389 i_pos = ((loff_t)EXFAT_I(old_inode)->dir.dir << 32) |
1390 (EXFAT_I(old_inode)->entry & 0xffffffff);
1391 exfat_unhash_inode(old_inode);
1392 exfat_hash_inode(old_inode, i_pos);
1393 if (IS_DIRSYNC(new_dir))
1394 exfat_sync_inode(old_inode);
1396 mark_inode_dirty(old_inode);
1398 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
1399 drop_nlink(old_dir);
1404 inode_inc_iversion(old_dir);
1405 old_dir->i_ctime = old_dir->i_mtime = current_time(old_dir);
1406 if (IS_DIRSYNC(old_dir))
1407 exfat_sync_inode(old_dir);
1409 mark_inode_dirty(old_dir);
1412 exfat_unhash_inode(new_inode);
1414 /* skip drop_nlink if new_inode already has been dropped */
1415 if (new_inode->i_nlink) {
1416 drop_nlink(new_inode);
1417 if (S_ISDIR(new_inode->i_mode))
1418 drop_nlink(new_inode);
1420 exfat_warn(sb, "abnormal access to an inode dropped");
1421 WARN_ON(new_inode->i_nlink == 0);
1423 new_inode->i_ctime = EXFAT_I(new_inode)->i_crtime =
1424 current_time(new_inode);
1428 mutex_unlock(&EXFAT_SB(sb)->s_lock);
1432 const struct inode_operations exfat_dir_inode_operations = {
1433 .create = exfat_create,
1434 .lookup = exfat_lookup,
1435 .unlink = exfat_unlink,
1436 .mkdir = exfat_mkdir,
1437 .rmdir = exfat_rmdir,
1438 .rename = exfat_rename,
1439 .setattr = exfat_setattr,
1440 .getattr = exfat_getattr,