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 /* search EMPTY CONTINUOUS "num_entries" entries */
208 static int exfat_search_empty_slot(struct super_block
*sb
,
209 struct exfat_hint_femp
*hint_femp
, struct exfat_chain
*p_dir
,
210 int num_entries
, struct exfat_entry_set_cache
*es
)
213 int dentries_per_clu
;
214 struct exfat_chain clu
;
215 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
216 int total_entries
= EXFAT_CLU_TO_DEN(p_dir
->size
, sbi
);
218 dentries_per_clu
= sbi
->dentries_per_clu
;
220 if (hint_femp
->eidx
!= EXFAT_HINT_NONE
) {
221 dentry
= hint_femp
->eidx
;
224 * If hint_femp->count is enough, it is needed to check if
225 * there are actual empty entries.
226 * Otherwise, and if "dentry + hint_famp->count" is also equal
227 * to "p_dir->size * dentries_per_clu", it means ENOSPC.
229 if (dentry
+ hint_femp
->count
== total_entries
&&
230 num_entries
> hint_femp
->count
)
233 hint_femp
->eidx
= EXFAT_HINT_NONE
;
234 exfat_chain_dup(&clu
, &hint_femp
->cur
);
236 exfat_chain_dup(&clu
, p_dir
);
240 while (dentry
+ num_entries
< total_entries
&&
241 clu
.dir
!= EXFAT_EOF_CLUSTER
) {
242 i
= dentry
& (dentries_per_clu
- 1);
244 ret
= exfat_get_empty_dentry_set(es
, sb
, &clu
, i
, num_entries
);
253 while (i
>= dentries_per_clu
) {
254 if (clu
.flags
== ALLOC_NO_FAT_CHAIN
) {
258 clu
.dir
= EXFAT_EOF_CLUSTER
;
260 if (exfat_get_next_cluster(sb
, &clu
.dir
))
264 i
-= dentries_per_clu
;
268 hint_femp
->eidx
= dentry
;
269 hint_femp
->count
= 0;
270 if (dentry
== total_entries
|| clu
.dir
== EXFAT_EOF_CLUSTER
)
271 exfat_chain_set(&hint_femp
->cur
, EXFAT_EOF_CLUSTER
, 0,
274 hint_femp
->cur
= clu
;
279 static int exfat_check_max_dentries(struct inode
*inode
)
281 if (EXFAT_B_TO_DEN(i_size_read(inode
)) >= MAX_EXFAT_DENTRIES
) {
283 * exFAT spec allows a dir to grow up to 8388608(256MB)
291 /* find empty directory entry.
292 * if there isn't any empty slot, expand cluster chain.
294 static int exfat_find_empty_entry(struct inode
*inode
,
295 struct exfat_chain
*p_dir
, int num_entries
,
296 struct exfat_entry_set_cache
*es
)
299 unsigned int ret
, last_clu
;
301 struct exfat_chain clu
;
302 struct super_block
*sb
= inode
->i_sb
;
303 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
304 struct exfat_inode_info
*ei
= EXFAT_I(inode
);
305 struct exfat_hint_femp hint_femp
;
307 hint_femp
.eidx
= EXFAT_HINT_NONE
;
309 if (ei
->hint_femp
.eidx
!= EXFAT_HINT_NONE
) {
310 hint_femp
= ei
->hint_femp
;
311 ei
->hint_femp
.eidx
= EXFAT_HINT_NONE
;
314 while ((dentry
= exfat_search_empty_slot(sb
, &hint_femp
, p_dir
,
315 num_entries
, es
)) < 0) {
319 if (exfat_check_max_dentries(inode
))
323 * Allocate new cluster to this directory
325 if (ei
->start_clu
!= EXFAT_EOF_CLUSTER
) {
326 /* we trust p_dir->size regardless of FAT type */
327 if (exfat_find_last_cluster(sb
, p_dir
, &last_clu
))
330 exfat_chain_set(&clu
, last_clu
+ 1, 0, p_dir
->flags
);
332 /* This directory is empty */
333 exfat_chain_set(&clu
, EXFAT_EOF_CLUSTER
, 0,
337 /* allocate a cluster */
338 ret
= exfat_alloc_cluster(inode
, 1, &clu
, IS_DIRSYNC(inode
));
342 if (exfat_zeroed_cluster(inode
, clu
.dir
))
345 if (ei
->start_clu
== EXFAT_EOF_CLUSTER
) {
346 ei
->start_clu
= clu
.dir
;
347 p_dir
->dir
= clu
.dir
;
350 /* append to the FAT chain */
351 if (clu
.flags
!= p_dir
->flags
) {
352 /* no-fat-chain bit is disabled,
353 * so fat-chain should be synced with alloc-bitmap
355 exfat_chain_cont_cluster(sb
, p_dir
->dir
, p_dir
->size
);
356 p_dir
->flags
= ALLOC_FAT_CHAIN
;
357 hint_femp
.cur
.flags
= ALLOC_FAT_CHAIN
;
360 if (clu
.flags
== ALLOC_FAT_CHAIN
)
361 if (exfat_ent_set(sb
, last_clu
, clu
.dir
))
364 if (hint_femp
.cur
.dir
== EXFAT_EOF_CLUSTER
)
365 exfat_chain_set(&hint_femp
.cur
, clu
.dir
, 0, clu
.flags
);
367 hint_femp
.count
+= sbi
->dentries_per_clu
;
369 hint_femp
.cur
.size
++;
371 size
= EXFAT_CLU_TO_B(p_dir
->size
, sbi
);
373 /* directory inode should be updated in here */
374 i_size_write(inode
, size
);
375 ei
->valid_size
+= sbi
->cluster_size
;
376 ei
->flags
= p_dir
->flags
;
377 inode
->i_blocks
+= sbi
->cluster_size
>> 9;
384 * Name Resolution Functions :
385 * Zero if it was successful; otherwise nonzero.
387 static int __exfat_resolve_path(struct inode
*inode
, const unsigned char *path
,
388 struct exfat_chain
*p_dir
, struct exfat_uni_name
*p_uniname
,
392 int lossy
= NLS_NAME_NO_LOSSY
;
393 struct super_block
*sb
= inode
->i_sb
;
394 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
395 struct exfat_inode_info
*ei
= EXFAT_I(inode
);
396 int pathlen
= strlen(path
);
399 * get the length of the pathname excluding
400 * trailing periods, if any.
402 namelen
= exfat_striptail_len(pathlen
, path
, false);
403 if (EXFAT_SB(sb
)->options
.keep_last_dots
) {
405 * Do not allow the creation of files with names
406 * ending with period(s).
408 if (!lookup
&& (namelen
< pathlen
))
414 if (pathlen
> (MAX_NAME_LENGTH
* MAX_CHARSET_SIZE
))
415 return -ENAMETOOLONG
;
418 * strip all leading spaces :
419 * "MS windows 7" supports leading spaces.
420 * So we should skip this preprocessing for compatibility.
423 /* file name conversion :
424 * If lookup case, we allow bad-name for compatibility.
426 namelen
= exfat_nls_to_utf16(sb
, path
, namelen
, p_uniname
,
429 return namelen
; /* return error value */
431 if ((lossy
&& !lookup
) || !namelen
)
432 return (lossy
& NLS_NAME_OVERLEN
) ? -ENAMETOOLONG
: -EINVAL
;
434 exfat_chain_set(p_dir
, ei
->start_clu
,
435 EXFAT_B_TO_CLU(i_size_read(inode
), sbi
), ei
->flags
);
440 static inline int exfat_resolve_path(struct inode
*inode
,
441 const unsigned char *path
, struct exfat_chain
*dir
,
442 struct exfat_uni_name
*uni
)
444 return __exfat_resolve_path(inode
, path
, dir
, uni
, 0);
447 static inline int exfat_resolve_path_for_lookup(struct inode
*inode
,
448 const unsigned char *path
, struct exfat_chain
*dir
,
449 struct exfat_uni_name
*uni
)
451 return __exfat_resolve_path(inode
, path
, dir
, uni
, 1);
454 static inline loff_t
exfat_make_i_pos(struct exfat_dir_entry
*info
)
456 return ((loff_t
) info
->dir
.dir
<< 32) | (info
->entry
& 0xffffffff);
459 static int exfat_add_entry(struct inode
*inode
, const char *path
,
460 struct exfat_chain
*p_dir
, unsigned int type
,
461 struct exfat_dir_entry
*info
)
463 int ret
, dentry
, num_entries
;
464 struct super_block
*sb
= inode
->i_sb
;
465 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
466 struct exfat_uni_name uniname
;
467 struct exfat_chain clu
;
468 struct timespec64 ts
= current_time(inode
);
469 struct exfat_entry_set_cache es
;
471 unsigned int start_clu
= EXFAT_FREE_CLUSTER
;
473 ret
= exfat_resolve_path(inode
, path
, p_dir
, &uniname
);
477 num_entries
= exfat_calc_num_entries(&uniname
);
478 if (num_entries
< 0) {
483 /* exfat_find_empty_entry must be called before alloc_cluster() */
484 dentry
= exfat_find_empty_entry(inode
, p_dir
, num_entries
, &es
);
486 ret
= dentry
; /* -EIO or -ENOSPC */
490 if (type
== TYPE_DIR
&& !sbi
->options
.zero_size_dir
) {
491 ret
= exfat_alloc_new_dir(inode
, &clu
);
493 exfat_put_dentry_set(&es
, false);
497 clu_size
= sbi
->cluster_size
;
500 /* update the directory entry */
501 /* fill the dos name directory entry information of the created file.
502 * the first cluster is not determined yet. (0)
504 exfat_init_dir_entry(&es
, type
, start_clu
, clu_size
, &ts
);
505 exfat_init_ext_entry(&es
, num_entries
, &uniname
);
507 ret
= exfat_put_dentry_set(&es
, IS_DIRSYNC(inode
));
512 info
->entry
= dentry
;
513 info
->flags
= ALLOC_NO_FAT_CHAIN
;
516 if (type
== TYPE_FILE
) {
517 info
->attr
= EXFAT_ATTR_ARCHIVE
;
518 info
->start_clu
= EXFAT_EOF_CLUSTER
;
520 info
->num_subdirs
= 0;
522 info
->attr
= EXFAT_ATTR_SUBDIR
;
523 if (sbi
->options
.zero_size_dir
)
524 info
->start_clu
= EXFAT_EOF_CLUSTER
;
526 info
->start_clu
= start_clu
;
527 info
->size
= clu_size
;
528 info
->num_subdirs
= EXFAT_MIN_SUBDIR
;
530 info
->valid_size
= info
->size
;
532 memset(&info
->crtime
, 0, sizeof(info
->crtime
));
533 memset(&info
->mtime
, 0, sizeof(info
->mtime
));
534 memset(&info
->atime
, 0, sizeof(info
->atime
));
539 static int exfat_create(struct mnt_idmap
*idmap
, struct inode
*dir
,
540 struct dentry
*dentry
, umode_t mode
, bool excl
)
542 struct super_block
*sb
= dir
->i_sb
;
544 struct exfat_chain cdir
;
545 struct exfat_dir_entry info
;
548 loff_t size
= i_size_read(dir
);
550 if (unlikely(exfat_forced_shutdown(sb
)))
553 mutex_lock(&EXFAT_SB(sb
)->s_lock
);
554 exfat_set_volume_dirty(sb
);
555 err
= exfat_add_entry(dir
, dentry
->d_name
.name
, &cdir
, TYPE_FILE
,
560 inode_inc_iversion(dir
);
561 inode_set_mtime_to_ts(dir
, inode_set_ctime_current(dir
));
562 if (IS_DIRSYNC(dir
) && size
!= i_size_read(dir
))
563 exfat_sync_inode(dir
);
565 mark_inode_dirty(dir
);
567 i_pos
= exfat_make_i_pos(&info
);
568 inode
= exfat_build_inode(sb
, &info
, i_pos
);
569 err
= PTR_ERR_OR_ZERO(inode
);
573 inode_inc_iversion(inode
);
574 EXFAT_I(inode
)->i_crtime
= simple_inode_init_ts(inode
);
575 exfat_truncate_inode_atime(inode
);
577 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
579 d_instantiate(dentry
, inode
);
581 mutex_unlock(&EXFAT_SB(sb
)->s_lock
);
586 static int exfat_find(struct inode
*dir
, struct qstr
*qname
,
587 struct exfat_dir_entry
*info
)
589 int ret
, dentry
, count
;
590 struct exfat_chain cdir
;
591 struct exfat_uni_name uni_name
;
592 struct super_block
*sb
= dir
->i_sb
;
593 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
594 struct exfat_inode_info
*ei
= EXFAT_I(dir
);
595 struct exfat_dentry
*ep
, *ep2
;
596 struct exfat_entry_set_cache es
;
597 /* for optimized dir & entry to prevent long traverse of cluster chain */
598 struct exfat_hint hint_opt
;
603 /* check the validity of directory name in the given pathname */
604 ret
= exfat_resolve_path_for_lookup(dir
, qname
->name
, &cdir
, &uni_name
);
608 /* check the validation of hint_stat and initialize it if required */
609 if (ei
->version
!= (inode_peek_iversion_raw(dir
) & 0xffffffff)) {
610 ei
->hint_stat
.clu
= cdir
.dir
;
611 ei
->hint_stat
.eidx
= 0;
612 ei
->version
= (inode_peek_iversion_raw(dir
) & 0xffffffff);
613 ei
->hint_femp
.eidx
= EXFAT_HINT_NONE
;
616 /* search the file name for directories */
617 dentry
= exfat_find_dir_entry(sb
, ei
, &cdir
, &uni_name
, &hint_opt
);
619 return dentry
; /* -error value */
622 info
->entry
= dentry
;
623 info
->num_subdirs
= 0;
625 /* adjust cdir to the optimized value */
626 cdir
.dir
= hint_opt
.clu
;
627 if (cdir
.flags
& ALLOC_NO_FAT_CHAIN
)
628 cdir
.size
-= dentry
/ sbi
->dentries_per_clu
;
629 dentry
= hint_opt
.eidx
;
630 if (exfat_get_dentry_set(&es
, sb
, &cdir
, dentry
, ES_2_ENTRIES
))
632 ep
= exfat_get_dentry_cached(&es
, ES_IDX_FILE
);
633 ep2
= exfat_get_dentry_cached(&es
, ES_IDX_STREAM
);
635 info
->type
= exfat_get_entry_type(ep
);
636 info
->attr
= le16_to_cpu(ep
->dentry
.file
.attr
);
637 info
->size
= le64_to_cpu(ep2
->dentry
.stream
.valid_size
);
638 info
->valid_size
= le64_to_cpu(ep2
->dentry
.stream
.valid_size
);
639 info
->size
= le64_to_cpu(ep2
->dentry
.stream
.size
);
640 if (info
->size
== 0) {
641 info
->flags
= ALLOC_NO_FAT_CHAIN
;
642 info
->start_clu
= EXFAT_EOF_CLUSTER
;
644 info
->flags
= ep2
->dentry
.stream
.flags
;
646 le32_to_cpu(ep2
->dentry
.stream
.start_clu
);
649 exfat_get_entry_time(sbi
, &info
->crtime
,
650 ep
->dentry
.file
.create_tz
,
651 ep
->dentry
.file
.create_time
,
652 ep
->dentry
.file
.create_date
,
653 ep
->dentry
.file
.create_time_cs
);
654 exfat_get_entry_time(sbi
, &info
->mtime
,
655 ep
->dentry
.file
.modify_tz
,
656 ep
->dentry
.file
.modify_time
,
657 ep
->dentry
.file
.modify_date
,
658 ep
->dentry
.file
.modify_time_cs
);
659 exfat_get_entry_time(sbi
, &info
->atime
,
660 ep
->dentry
.file
.access_tz
,
661 ep
->dentry
.file
.access_time
,
662 ep
->dentry
.file
.access_date
,
664 exfat_put_dentry_set(&es
, false);
666 if (ei
->start_clu
== EXFAT_FREE_CLUSTER
) {
668 "non-zero size file starts with zero cluster (size : %llu, p_dir : %u, entry : 0x%08x)",
669 i_size_read(dir
), ei
->dir
.dir
, ei
->entry
);
673 if (info
->type
== TYPE_DIR
) {
674 exfat_chain_set(&cdir
, info
->start_clu
,
675 EXFAT_B_TO_CLU(info
->size
, sbi
), info
->flags
);
676 count
= exfat_count_dir_entries(sb
, &cdir
);
680 info
->num_subdirs
= count
+ EXFAT_MIN_SUBDIR
;
685 static int exfat_d_anon_disconn(struct dentry
*dentry
)
687 return IS_ROOT(dentry
) && (dentry
->d_flags
& DCACHE_DISCONNECTED
);
690 static struct dentry
*exfat_lookup(struct inode
*dir
, struct dentry
*dentry
,
693 struct super_block
*sb
= dir
->i_sb
;
695 struct dentry
*alias
;
696 struct exfat_dir_entry info
;
701 mutex_lock(&EXFAT_SB(sb
)->s_lock
);
702 err
= exfat_find(dir
, &dentry
->d_name
, &info
);
704 if (err
== -ENOENT
) {
711 i_pos
= exfat_make_i_pos(&info
);
712 inode
= exfat_build_inode(sb
, &info
, i_pos
);
713 err
= PTR_ERR_OR_ZERO(inode
);
717 i_mode
= inode
->i_mode
;
718 alias
= d_find_alias(inode
);
721 * Checking "alias->d_parent == dentry->d_parent" to make sure
722 * FS is not corrupted (especially double linked dir).
724 if (alias
&& alias
->d_parent
== dentry
->d_parent
&&
725 !exfat_d_anon_disconn(alias
)) {
728 * Unhashed alias is able to exist because of revalidate()
729 * called by lookup_fast. You can easily make this status
730 * by calling create and lookup concurrently
731 * In such case, we reuse an alias instead of new dentry
733 if (d_unhashed(alias
)) {
734 WARN_ON(alias
->d_name
.hash_len
!=
735 dentry
->d_name
.hash_len
);
736 exfat_info(sb
, "rehashed a dentry(%p) in read lookup",
740 } else if (!S_ISDIR(i_mode
)) {
742 * This inode has non anonymous-DCACHE_DISCONNECTED
743 * dentry. This means, the user did ->lookup() by an
744 * another name (longname vs 8.3 alias of it) in past.
746 * Switch to new one for reason of locality if possible.
748 d_move(alias
, dentry
);
751 mutex_unlock(&EXFAT_SB(sb
)->s_lock
);
756 mutex_unlock(&EXFAT_SB(sb
)->s_lock
);
758 exfat_d_version_set(dentry
, inode_query_iversion(dir
));
760 return d_splice_alias(inode
, dentry
);
762 mutex_unlock(&EXFAT_SB(sb
)->s_lock
);
766 /* remove an entry, BUT don't truncate */
767 static int exfat_unlink(struct inode
*dir
, struct dentry
*dentry
)
769 struct exfat_chain cdir
;
770 struct super_block
*sb
= dir
->i_sb
;
771 struct inode
*inode
= dentry
->d_inode
;
772 struct exfat_inode_info
*ei
= EXFAT_I(inode
);
773 struct exfat_entry_set_cache es
;
776 if (unlikely(exfat_forced_shutdown(sb
)))
779 mutex_lock(&EXFAT_SB(sb
)->s_lock
);
780 exfat_chain_dup(&cdir
, &ei
->dir
);
782 if (ei
->dir
.dir
== DIR_DELETED
) {
783 exfat_err(sb
, "abnormal access to deleted dentry");
788 err
= exfat_get_dentry_set(&es
, sb
, &cdir
, entry
, ES_ALL_ENTRIES
);
794 exfat_set_volume_dirty(sb
);
796 /* update the directory entry */
797 exfat_remove_entries(inode
, &es
, ES_IDX_FILE
);
799 err
= exfat_put_dentry_set(&es
, IS_DIRSYNC(inode
));
803 /* This doesn't modify ei */
804 ei
->dir
.dir
= DIR_DELETED
;
806 inode_inc_iversion(dir
);
807 simple_inode_init_ts(dir
);
808 exfat_truncate_inode_atime(dir
);
809 mark_inode_dirty(dir
);
812 simple_inode_init_ts(inode
);
813 exfat_truncate_inode_atime(inode
);
814 exfat_unhash_inode(inode
);
815 exfat_d_version_set(dentry
, inode_query_iversion(dir
));
817 mutex_unlock(&EXFAT_SB(sb
)->s_lock
);
821 static int exfat_mkdir(struct mnt_idmap
*idmap
, struct inode
*dir
,
822 struct dentry
*dentry
, umode_t mode
)
824 struct super_block
*sb
= dir
->i_sb
;
826 struct exfat_dir_entry info
;
827 struct exfat_chain cdir
;
830 loff_t size
= i_size_read(dir
);
832 if (unlikely(exfat_forced_shutdown(sb
)))
835 mutex_lock(&EXFAT_SB(sb
)->s_lock
);
836 exfat_set_volume_dirty(sb
);
837 err
= exfat_add_entry(dir
, dentry
->d_name
.name
, &cdir
, TYPE_DIR
,
842 inode_inc_iversion(dir
);
843 inode_set_mtime_to_ts(dir
, inode_set_ctime_current(dir
));
844 if (IS_DIRSYNC(dir
) && size
!= i_size_read(dir
))
845 exfat_sync_inode(dir
);
847 mark_inode_dirty(dir
);
850 i_pos
= exfat_make_i_pos(&info
);
851 inode
= exfat_build_inode(sb
, &info
, i_pos
);
852 err
= PTR_ERR_OR_ZERO(inode
);
856 inode_inc_iversion(inode
);
857 EXFAT_I(inode
)->i_crtime
= simple_inode_init_ts(inode
);
858 exfat_truncate_inode_atime(inode
);
859 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
861 d_instantiate(dentry
, inode
);
864 mutex_unlock(&EXFAT_SB(sb
)->s_lock
);
868 static int exfat_check_dir_empty(struct super_block
*sb
,
869 struct exfat_chain
*p_dir
)
871 int i
, dentries_per_clu
;
873 struct exfat_chain clu
;
874 struct exfat_dentry
*ep
;
875 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
876 struct buffer_head
*bh
;
878 dentries_per_clu
= sbi
->dentries_per_clu
;
880 if (p_dir
->dir
== EXFAT_EOF_CLUSTER
)
883 exfat_chain_dup(&clu
, p_dir
);
885 while (clu
.dir
!= EXFAT_EOF_CLUSTER
) {
886 for (i
= 0; i
< dentries_per_clu
; i
++) {
887 ep
= exfat_get_dentry(sb
, &clu
, i
, &bh
);
890 type
= exfat_get_entry_type(ep
);
892 if (type
== TYPE_UNUSED
)
895 if (type
!= TYPE_FILE
&& type
!= TYPE_DIR
)
901 if (clu
.flags
== ALLOC_NO_FAT_CHAIN
) {
905 clu
.dir
= EXFAT_EOF_CLUSTER
;
907 if (exfat_get_next_cluster(sb
, &(clu
.dir
)))
915 static int exfat_rmdir(struct inode
*dir
, struct dentry
*dentry
)
917 struct inode
*inode
= dentry
->d_inode
;
918 struct exfat_chain cdir
, clu_to_free
;
919 struct super_block
*sb
= inode
->i_sb
;
920 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
921 struct exfat_inode_info
*ei
= EXFAT_I(inode
);
922 struct exfat_entry_set_cache es
;
925 if (unlikely(exfat_forced_shutdown(sb
)))
928 mutex_lock(&EXFAT_SB(inode
->i_sb
)->s_lock
);
930 exfat_chain_dup(&cdir
, &ei
->dir
);
933 if (ei
->dir
.dir
== DIR_DELETED
) {
934 exfat_err(sb
, "abnormal access to deleted dentry");
939 exfat_chain_set(&clu_to_free
, ei
->start_clu
,
940 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode
), sbi
), ei
->flags
);
942 err
= exfat_check_dir_empty(sb
, &clu_to_free
);
945 exfat_err(sb
, "failed to exfat_check_dir_empty : err(%d)",
950 err
= exfat_get_dentry_set(&es
, sb
, &cdir
, entry
, ES_ALL_ENTRIES
);
956 exfat_set_volume_dirty(sb
);
958 exfat_remove_entries(inode
, &es
, ES_IDX_FILE
);
960 err
= exfat_put_dentry_set(&es
, IS_DIRSYNC(dir
));
964 ei
->dir
.dir
= DIR_DELETED
;
966 inode_inc_iversion(dir
);
967 simple_inode_init_ts(dir
);
968 exfat_truncate_inode_atime(dir
);
970 exfat_sync_inode(dir
);
972 mark_inode_dirty(dir
);
976 simple_inode_init_ts(inode
);
977 exfat_truncate_inode_atime(inode
);
978 exfat_unhash_inode(inode
);
979 exfat_d_version_set(dentry
, inode_query_iversion(dir
));
981 mutex_unlock(&EXFAT_SB(inode
->i_sb
)->s_lock
);
985 static int exfat_rename_file(struct inode
*inode
, struct exfat_chain
*p_dir
,
986 int oldentry
, struct exfat_uni_name
*p_uniname
,
987 struct exfat_inode_info
*ei
)
989 int ret
, num_new_entries
;
990 struct exfat_dentry
*epold
, *epnew
;
991 struct super_block
*sb
= inode
->i_sb
;
992 struct exfat_entry_set_cache old_es
, new_es
;
993 int sync
= IS_DIRSYNC(inode
);
995 if (unlikely(exfat_forced_shutdown(sb
)))
998 num_new_entries
= exfat_calc_num_entries(p_uniname
);
999 if (num_new_entries
< 0)
1000 return num_new_entries
;
1002 ret
= exfat_get_dentry_set(&old_es
, sb
, p_dir
, oldentry
, ES_ALL_ENTRIES
);
1008 epold
= exfat_get_dentry_cached(&old_es
, ES_IDX_FILE
);
1010 if (old_es
.num_entries
< num_new_entries
) {
1013 newentry
= exfat_find_empty_entry(inode
, p_dir
, num_new_entries
,
1016 ret
= newentry
; /* -EIO or -ENOSPC */
1020 epnew
= exfat_get_dentry_cached(&new_es
, ES_IDX_FILE
);
1022 if (exfat_get_entry_type(epnew
) == TYPE_FILE
) {
1023 epnew
->dentry
.file
.attr
|= cpu_to_le16(EXFAT_ATTR_ARCHIVE
);
1024 ei
->attr
|= EXFAT_ATTR_ARCHIVE
;
1027 epold
= exfat_get_dentry_cached(&old_es
, ES_IDX_STREAM
);
1028 epnew
= exfat_get_dentry_cached(&new_es
, ES_IDX_STREAM
);
1031 exfat_init_ext_entry(&new_es
, num_new_entries
, p_uniname
);
1033 ret
= exfat_put_dentry_set(&new_es
, sync
);
1037 exfat_remove_entries(inode
, &old_es
, ES_IDX_FILE
);
1039 ei
->entry
= newentry
;
1041 if (exfat_get_entry_type(epold
) == TYPE_FILE
) {
1042 epold
->dentry
.file
.attr
|= cpu_to_le16(EXFAT_ATTR_ARCHIVE
);
1043 ei
->attr
|= EXFAT_ATTR_ARCHIVE
;
1046 exfat_remove_entries(inode
, &old_es
, ES_IDX_FIRST_FILENAME
+ 1);
1047 exfat_init_ext_entry(&old_es
, num_new_entries
, p_uniname
);
1049 return exfat_put_dentry_set(&old_es
, sync
);
1052 exfat_put_dentry_set(&old_es
, false);
1056 static int exfat_move_file(struct inode
*inode
, struct exfat_chain
*p_olddir
,
1057 int oldentry
, struct exfat_chain
*p_newdir
,
1058 struct exfat_uni_name
*p_uniname
, struct exfat_inode_info
*ei
)
1060 int ret
, newentry
, num_new_entries
;
1061 struct exfat_dentry
*epmov
, *epnew
;
1062 struct super_block
*sb
= inode
->i_sb
;
1063 struct exfat_entry_set_cache mov_es
, new_es
;
1065 num_new_entries
= exfat_calc_num_entries(p_uniname
);
1066 if (num_new_entries
< 0)
1067 return num_new_entries
;
1069 ret
= exfat_get_dentry_set(&mov_es
, sb
, p_olddir
, oldentry
,
1074 newentry
= exfat_find_empty_entry(inode
, p_newdir
, num_new_entries
,
1077 ret
= newentry
; /* -EIO or -ENOSPC */
1081 epmov
= exfat_get_dentry_cached(&mov_es
, ES_IDX_FILE
);
1082 epnew
= exfat_get_dentry_cached(&new_es
, ES_IDX_FILE
);
1084 if (exfat_get_entry_type(epnew
) == TYPE_FILE
) {
1085 epnew
->dentry
.file
.attr
|= cpu_to_le16(EXFAT_ATTR_ARCHIVE
);
1086 ei
->attr
|= EXFAT_ATTR_ARCHIVE
;
1089 epmov
= exfat_get_dentry_cached(&mov_es
, ES_IDX_STREAM
);
1090 epnew
= exfat_get_dentry_cached(&new_es
, ES_IDX_STREAM
);
1093 exfat_init_ext_entry(&new_es
, num_new_entries
, p_uniname
);
1094 exfat_remove_entries(inode
, &mov_es
, ES_IDX_FILE
);
1096 exfat_chain_set(&ei
->dir
, p_newdir
->dir
, p_newdir
->size
,
1099 ei
->entry
= newentry
;
1101 ret
= exfat_put_dentry_set(&new_es
, IS_DIRSYNC(inode
));
1105 return exfat_put_dentry_set(&mov_es
, IS_DIRSYNC(inode
));
1108 exfat_put_dentry_set(&mov_es
, false);
1113 /* rename or move a old file into a new file */
1114 static int __exfat_rename(struct inode
*old_parent_inode
,
1115 struct exfat_inode_info
*ei
, struct inode
*new_parent_inode
,
1116 struct dentry
*new_dentry
)
1120 struct exfat_chain olddir
, newdir
;
1121 struct exfat_chain
*p_dir
= NULL
;
1122 struct exfat_uni_name uni_name
;
1123 struct exfat_dentry
*ep
;
1124 struct super_block
*sb
= old_parent_inode
->i_sb
;
1125 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
1126 const unsigned char *new_path
= new_dentry
->d_name
.name
;
1127 struct inode
*new_inode
= new_dentry
->d_inode
;
1128 struct exfat_inode_info
*new_ei
= NULL
;
1129 unsigned int new_entry_type
= TYPE_UNUSED
;
1131 struct buffer_head
*new_bh
= NULL
;
1133 /* check the validity of pointer parameters */
1134 if (new_path
== NULL
|| strlen(new_path
) == 0)
1137 if (ei
->dir
.dir
== DIR_DELETED
) {
1138 exfat_err(sb
, "abnormal access to deleted source dentry");
1142 exfat_chain_set(&olddir
, EXFAT_I(old_parent_inode
)->start_clu
,
1143 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(old_parent_inode
), sbi
),
1144 EXFAT_I(old_parent_inode
)->flags
);
1147 /* check whether new dir is existing directory and empty */
1150 new_ei
= EXFAT_I(new_inode
);
1152 if (new_ei
->dir
.dir
== DIR_DELETED
) {
1153 exfat_err(sb
, "abnormal access to deleted target dentry");
1157 p_dir
= &(new_ei
->dir
);
1158 new_entry
= new_ei
->entry
;
1159 ep
= exfat_get_dentry(sb
, p_dir
, new_entry
, &new_bh
);
1163 new_entry_type
= exfat_get_entry_type(ep
);
1166 /* if new_inode exists, update ei */
1167 if (new_entry_type
== TYPE_DIR
) {
1168 struct exfat_chain new_clu
;
1170 new_clu
.dir
= new_ei
->start_clu
;
1172 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode
),
1174 new_clu
.flags
= new_ei
->flags
;
1176 ret
= exfat_check_dir_empty(sb
, &new_clu
);
1182 /* check the validity of directory name in the given new pathname */
1183 ret
= exfat_resolve_path(new_parent_inode
, new_path
, &newdir
,
1188 exfat_set_volume_dirty(sb
);
1190 if (olddir
.dir
== newdir
.dir
)
1191 ret
= exfat_rename_file(new_parent_inode
, &olddir
, dentry
,
1194 ret
= exfat_move_file(new_parent_inode
, &olddir
, dentry
,
1195 &newdir
, &uni_name
, ei
);
1197 if (!ret
&& new_inode
) {
1198 struct exfat_entry_set_cache es
;
1200 /* delete entries of new_dir */
1201 ret
= exfat_get_dentry_set(&es
, sb
, p_dir
, new_entry
,
1208 exfat_remove_entries(new_inode
, &es
, ES_IDX_FILE
);
1210 ret
= exfat_put_dentry_set(&es
, IS_DIRSYNC(new_inode
));
1214 /* Free the clusters if new_inode is a dir(as if exfat_rmdir) */
1215 if (new_entry_type
== TYPE_DIR
&&
1216 new_ei
->start_clu
!= EXFAT_EOF_CLUSTER
) {
1217 /* new_ei, new_clu_to_free */
1218 struct exfat_chain new_clu_to_free
;
1220 exfat_chain_set(&new_clu_to_free
, new_ei
->start_clu
,
1221 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode
),
1222 sbi
), new_ei
->flags
);
1224 if (exfat_free_cluster(new_inode
, &new_clu_to_free
)) {
1225 /* just set I/O error only */
1229 i_size_write(new_inode
, 0);
1230 new_ei
->valid_size
= 0;
1231 new_ei
->start_clu
= EXFAT_EOF_CLUSTER
;
1232 new_ei
->flags
= ALLOC_NO_FAT_CHAIN
;
1235 /* Update new_inode ei
1236 * Prevent syncing removed new_inode
1237 * (new_ei is already initialized above code ("if (new_inode)")
1239 new_ei
->dir
.dir
= DIR_DELETED
;
1245 static int exfat_rename(struct mnt_idmap
*idmap
,
1246 struct inode
*old_dir
, struct dentry
*old_dentry
,
1247 struct inode
*new_dir
, struct dentry
*new_dentry
,
1250 struct inode
*old_inode
, *new_inode
;
1251 struct super_block
*sb
= old_dir
->i_sb
;
1254 loff_t size
= i_size_read(new_dir
);
1257 * The VFS already checks for existence, so for local filesystems
1258 * the RENAME_NOREPLACE implementation is equivalent to plain rename.
1259 * Don't support any other flags
1261 if (flags
& ~RENAME_NOREPLACE
)
1264 mutex_lock(&EXFAT_SB(sb
)->s_lock
);
1265 old_inode
= old_dentry
->d_inode
;
1266 new_inode
= new_dentry
->d_inode
;
1268 err
= __exfat_rename(old_dir
, EXFAT_I(old_inode
), new_dir
, new_dentry
);
1272 inode_inc_iversion(new_dir
);
1273 simple_rename_timestamp(old_dir
, old_dentry
, new_dir
, new_dentry
);
1274 EXFAT_I(new_dir
)->i_crtime
= current_time(new_dir
);
1275 exfat_truncate_inode_atime(new_dir
);
1276 if (IS_DIRSYNC(new_dir
) && size
!= i_size_read(new_dir
))
1277 exfat_sync_inode(new_dir
);
1279 mark_inode_dirty(new_dir
);
1281 i_pos
= ((loff_t
)EXFAT_I(old_inode
)->dir
.dir
<< 32) |
1282 (EXFAT_I(old_inode
)->entry
& 0xffffffff);
1283 exfat_unhash_inode(old_inode
);
1284 exfat_hash_inode(old_inode
, i_pos
);
1285 if (IS_DIRSYNC(new_dir
))
1286 exfat_sync_inode(old_inode
);
1288 mark_inode_dirty(old_inode
);
1290 if (S_ISDIR(old_inode
->i_mode
) && old_dir
!= new_dir
) {
1291 drop_nlink(old_dir
);
1296 inode_inc_iversion(old_dir
);
1297 if (new_dir
!= old_dir
)
1298 mark_inode_dirty(old_dir
);
1301 exfat_unhash_inode(new_inode
);
1303 /* skip drop_nlink if new_inode already has been dropped */
1304 if (new_inode
->i_nlink
) {
1305 drop_nlink(new_inode
);
1306 if (S_ISDIR(new_inode
->i_mode
))
1307 drop_nlink(new_inode
);
1309 exfat_warn(sb
, "abnormal access to an inode dropped");
1310 WARN_ON(new_inode
->i_nlink
== 0);
1312 EXFAT_I(new_inode
)->i_crtime
= current_time(new_inode
);
1316 mutex_unlock(&EXFAT_SB(sb
)->s_lock
);
1320 const struct inode_operations exfat_dir_inode_operations
= {
1321 .create
= exfat_create
,
1322 .lookup
= exfat_lookup
,
1323 .unlink
= exfat_unlink
,
1324 .mkdir
= exfat_mkdir
,
1325 .rmdir
= exfat_rmdir
,
1326 .rename
= exfat_rename
,
1327 .setattr
= exfat_setattr
,
1328 .getattr
= exfat_getattr
,