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)
292 * Find an empty directory entry set.
294 * If there isn't any empty slot, expand cluster chain.
297 * inode: inode of the parent directory
298 * num_entries: specifies how many dentries in the empty directory entry set
301 * p_dir: the cluster where the empty directory entry set is located
302 * es: The found empty directory entry set
305 * the directory entry index in p_dir is returned on succeeds
306 * -error code is returned on failure
308 static int exfat_find_empty_entry(struct inode
*inode
,
309 struct exfat_chain
*p_dir
, int num_entries
,
310 struct exfat_entry_set_cache
*es
)
313 unsigned int ret
, last_clu
;
315 struct exfat_chain clu
;
316 struct super_block
*sb
= inode
->i_sb
;
317 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
318 struct exfat_inode_info
*ei
= EXFAT_I(inode
);
319 struct exfat_hint_femp hint_femp
;
321 hint_femp
.eidx
= EXFAT_HINT_NONE
;
323 if (ei
->hint_femp
.eidx
!= EXFAT_HINT_NONE
) {
324 hint_femp
= ei
->hint_femp
;
325 ei
->hint_femp
.eidx
= EXFAT_HINT_NONE
;
328 exfat_chain_set(p_dir
, ei
->start_clu
,
329 EXFAT_B_TO_CLU(i_size_read(inode
), sbi
), ei
->flags
);
331 while ((dentry
= exfat_search_empty_slot(sb
, &hint_femp
, p_dir
,
332 num_entries
, es
)) < 0) {
336 if (exfat_check_max_dentries(inode
))
340 * Allocate new cluster to this directory
342 if (ei
->start_clu
!= EXFAT_EOF_CLUSTER
) {
343 /* we trust p_dir->size regardless of FAT type */
344 if (exfat_find_last_cluster(sb
, p_dir
, &last_clu
))
347 exfat_chain_set(&clu
, last_clu
+ 1, 0, p_dir
->flags
);
349 /* This directory is empty */
350 exfat_chain_set(&clu
, EXFAT_EOF_CLUSTER
, 0,
354 /* allocate a cluster */
355 ret
= exfat_alloc_cluster(inode
, 1, &clu
, IS_DIRSYNC(inode
));
359 if (exfat_zeroed_cluster(inode
, clu
.dir
))
362 if (ei
->start_clu
== EXFAT_EOF_CLUSTER
) {
363 ei
->start_clu
= clu
.dir
;
364 p_dir
->dir
= clu
.dir
;
368 /* append to the FAT chain */
369 if (clu
.flags
!= p_dir
->flags
) {
370 /* no-fat-chain bit is disabled,
371 * so fat-chain should be synced with alloc-bitmap
373 exfat_chain_cont_cluster(sb
, p_dir
->dir
, p_dir
->size
);
374 p_dir
->flags
= ALLOC_FAT_CHAIN
;
375 hint_femp
.cur
.flags
= ALLOC_FAT_CHAIN
;
378 if (clu
.flags
== ALLOC_FAT_CHAIN
)
379 if (exfat_ent_set(sb
, last_clu
, clu
.dir
))
382 if (hint_femp
.cur
.dir
== EXFAT_EOF_CLUSTER
)
383 exfat_chain_set(&hint_femp
.cur
, clu
.dir
, 0, clu
.flags
);
385 hint_femp
.count
+= sbi
->dentries_per_clu
;
387 hint_femp
.cur
.size
++;
389 size
= EXFAT_CLU_TO_B(p_dir
->size
, sbi
);
391 /* directory inode should be updated in here */
392 i_size_write(inode
, size
);
393 ei
->valid_size
+= sbi
->cluster_size
;
394 ei
->flags
= p_dir
->flags
;
395 inode
->i_blocks
+= sbi
->cluster_size
>> 9;
398 p_dir
->dir
= exfat_sector_to_cluster(sbi
, es
->bh
[0]->b_blocknr
);
399 p_dir
->size
-= dentry
/ sbi
->dentries_per_clu
;
401 return dentry
& (sbi
->dentries_per_clu
- 1);
405 * Name Resolution Functions :
406 * Zero if it was successful; otherwise nonzero.
408 static int __exfat_resolve_path(struct inode
*inode
, const unsigned char *path
,
409 struct exfat_uni_name
*p_uniname
, int lookup
)
412 int lossy
= NLS_NAME_NO_LOSSY
;
413 struct super_block
*sb
= inode
->i_sb
;
414 int pathlen
= strlen(path
);
417 * get the length of the pathname excluding
418 * trailing periods, if any.
420 namelen
= exfat_striptail_len(pathlen
, path
, false);
421 if (EXFAT_SB(sb
)->options
.keep_last_dots
) {
423 * Do not allow the creation of files with names
424 * ending with period(s).
426 if (!lookup
&& (namelen
< pathlen
))
432 if (pathlen
> (MAX_NAME_LENGTH
* MAX_CHARSET_SIZE
))
433 return -ENAMETOOLONG
;
436 * strip all leading spaces :
437 * "MS windows 7" supports leading spaces.
438 * So we should skip this preprocessing for compatibility.
441 /* file name conversion :
442 * If lookup case, we allow bad-name for compatibility.
444 namelen
= exfat_nls_to_utf16(sb
, path
, namelen
, p_uniname
,
447 return namelen
; /* return error value */
449 if ((lossy
&& !lookup
) || !namelen
)
450 return (lossy
& NLS_NAME_OVERLEN
) ? -ENAMETOOLONG
: -EINVAL
;
455 static inline int exfat_resolve_path(struct inode
*inode
,
456 const unsigned char *path
, struct exfat_uni_name
*uni
)
458 return __exfat_resolve_path(inode
, path
, uni
, 0);
461 static inline int exfat_resolve_path_for_lookup(struct inode
*inode
,
462 const unsigned char *path
, struct exfat_uni_name
*uni
)
464 return __exfat_resolve_path(inode
, path
, 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 unsigned int type
, struct exfat_dir_entry
*info
)
475 int ret
, dentry
, num_entries
;
476 struct super_block
*sb
= inode
->i_sb
;
477 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
478 struct exfat_uni_name uniname
;
479 struct exfat_chain clu
;
480 struct timespec64 ts
= current_time(inode
);
481 struct exfat_entry_set_cache es
;
483 unsigned int start_clu
= EXFAT_FREE_CLUSTER
;
485 ret
= exfat_resolve_path(inode
, path
, &uniname
);
489 num_entries
= exfat_calc_num_entries(&uniname
);
490 if (num_entries
< 0) {
495 /* exfat_find_empty_entry must be called before alloc_cluster() */
496 dentry
= exfat_find_empty_entry(inode
, &info
->dir
, num_entries
, &es
);
498 ret
= dentry
; /* -EIO or -ENOSPC */
502 if (type
== TYPE_DIR
&& !sbi
->options
.zero_size_dir
) {
503 ret
= exfat_alloc_new_dir(inode
, &clu
);
505 exfat_put_dentry_set(&es
, false);
509 clu_size
= sbi
->cluster_size
;
512 /* update the directory entry */
513 /* fill the dos name directory entry information of the created file.
514 * the first cluster is not determined yet. (0)
516 exfat_init_dir_entry(&es
, type
, start_clu
, clu_size
, &ts
);
517 exfat_init_ext_entry(&es
, num_entries
, &uniname
);
519 ret
= exfat_put_dentry_set(&es
, IS_DIRSYNC(inode
));
523 info
->entry
= dentry
;
524 info
->flags
= ALLOC_NO_FAT_CHAIN
;
527 if (type
== TYPE_FILE
) {
528 info
->attr
= EXFAT_ATTR_ARCHIVE
;
529 info
->start_clu
= EXFAT_EOF_CLUSTER
;
531 info
->num_subdirs
= 0;
533 info
->attr
= EXFAT_ATTR_SUBDIR
;
534 if (sbi
->options
.zero_size_dir
)
535 info
->start_clu
= EXFAT_EOF_CLUSTER
;
537 info
->start_clu
= start_clu
;
538 info
->size
= clu_size
;
539 info
->num_subdirs
= EXFAT_MIN_SUBDIR
;
541 info
->valid_size
= info
->size
;
543 memset(&info
->crtime
, 0, sizeof(info
->crtime
));
544 memset(&info
->mtime
, 0, sizeof(info
->mtime
));
545 memset(&info
->atime
, 0, sizeof(info
->atime
));
550 static int exfat_create(struct mnt_idmap
*idmap
, struct inode
*dir
,
551 struct dentry
*dentry
, umode_t mode
, bool excl
)
553 struct super_block
*sb
= dir
->i_sb
;
555 struct exfat_dir_entry info
;
558 loff_t size
= i_size_read(dir
);
560 if (unlikely(exfat_forced_shutdown(sb
)))
563 mutex_lock(&EXFAT_SB(sb
)->s_lock
);
564 exfat_set_volume_dirty(sb
);
565 err
= exfat_add_entry(dir
, dentry
->d_name
.name
, TYPE_FILE
, &info
);
569 inode_inc_iversion(dir
);
570 inode_set_mtime_to_ts(dir
, inode_set_ctime_current(dir
));
571 if (IS_DIRSYNC(dir
) && size
!= i_size_read(dir
))
572 exfat_sync_inode(dir
);
574 mark_inode_dirty(dir
);
576 i_pos
= exfat_make_i_pos(&info
);
577 inode
= exfat_build_inode(sb
, &info
, i_pos
);
578 err
= PTR_ERR_OR_ZERO(inode
);
582 inode_inc_iversion(inode
);
583 EXFAT_I(inode
)->i_crtime
= simple_inode_init_ts(inode
);
584 exfat_truncate_inode_atime(inode
);
586 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
588 d_instantiate(dentry
, inode
);
590 mutex_unlock(&EXFAT_SB(sb
)->s_lock
);
595 static int exfat_find(struct inode
*dir
, struct qstr
*qname
,
596 struct exfat_dir_entry
*info
)
598 int ret
, dentry
, count
;
599 struct exfat_chain cdir
;
600 struct exfat_uni_name uni_name
;
601 struct super_block
*sb
= dir
->i_sb
;
602 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
603 struct exfat_inode_info
*ei
= EXFAT_I(dir
);
604 struct exfat_dentry
*ep
, *ep2
;
605 struct exfat_entry_set_cache es
;
606 /* for optimized dir & entry to prevent long traverse of cluster chain */
607 struct exfat_hint hint_opt
;
612 /* check the validity of directory name in the given pathname */
613 ret
= exfat_resolve_path_for_lookup(dir
, qname
->name
, &uni_name
);
617 exfat_chain_set(&cdir
, ei
->start_clu
,
618 EXFAT_B_TO_CLU(i_size_read(dir
), sbi
), ei
->flags
);
620 /* check the validation of hint_stat and initialize it if required */
621 if (ei
->version
!= (inode_peek_iversion_raw(dir
) & 0xffffffff)) {
622 ei
->hint_stat
.clu
= cdir
.dir
;
623 ei
->hint_stat
.eidx
= 0;
624 ei
->version
= (inode_peek_iversion_raw(dir
) & 0xffffffff);
625 ei
->hint_femp
.eidx
= EXFAT_HINT_NONE
;
628 /* search the file name for directories */
629 dentry
= exfat_find_dir_entry(sb
, ei
, &cdir
, &uni_name
, &hint_opt
);
631 return dentry
; /* -error value */
633 /* adjust cdir to the optimized value */
634 cdir
.dir
= hint_opt
.clu
;
635 if (cdir
.flags
& ALLOC_NO_FAT_CHAIN
)
636 cdir
.size
-= dentry
/ sbi
->dentries_per_clu
;
637 dentry
= hint_opt
.eidx
;
640 info
->entry
= dentry
;
641 info
->num_subdirs
= 0;
643 if (exfat_get_dentry_set(&es
, sb
, &cdir
, dentry
, ES_2_ENTRIES
))
645 ep
= exfat_get_dentry_cached(&es
, ES_IDX_FILE
);
646 ep2
= exfat_get_dentry_cached(&es
, ES_IDX_STREAM
);
648 info
->type
= exfat_get_entry_type(ep
);
649 info
->attr
= le16_to_cpu(ep
->dentry
.file
.attr
);
650 info
->size
= le64_to_cpu(ep2
->dentry
.stream
.valid_size
);
651 info
->valid_size
= le64_to_cpu(ep2
->dentry
.stream
.valid_size
);
652 info
->size
= le64_to_cpu(ep2
->dentry
.stream
.size
);
654 info
->start_clu
= le32_to_cpu(ep2
->dentry
.stream
.start_clu
);
655 if (!is_valid_cluster(sbi
, info
->start_clu
) && info
->size
) {
656 exfat_warn(sb
, "start_clu is invalid cluster(0x%x)",
659 info
->valid_size
= 0;
662 if (info
->valid_size
> info
->size
) {
663 exfat_warn(sb
, "valid_size(%lld) is greater than size(%lld)",
664 info
->valid_size
, info
->size
);
665 info
->valid_size
= info
->size
;
668 if (info
->size
== 0) {
669 info
->flags
= ALLOC_NO_FAT_CHAIN
;
670 info
->start_clu
= EXFAT_EOF_CLUSTER
;
672 info
->flags
= ep2
->dentry
.stream
.flags
;
674 exfat_get_entry_time(sbi
, &info
->crtime
,
675 ep
->dentry
.file
.create_tz
,
676 ep
->dentry
.file
.create_time
,
677 ep
->dentry
.file
.create_date
,
678 ep
->dentry
.file
.create_time_cs
);
679 exfat_get_entry_time(sbi
, &info
->mtime
,
680 ep
->dentry
.file
.modify_tz
,
681 ep
->dentry
.file
.modify_time
,
682 ep
->dentry
.file
.modify_date
,
683 ep
->dentry
.file
.modify_time_cs
);
684 exfat_get_entry_time(sbi
, &info
->atime
,
685 ep
->dentry
.file
.access_tz
,
686 ep
->dentry
.file
.access_time
,
687 ep
->dentry
.file
.access_date
,
689 exfat_put_dentry_set(&es
, false);
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
);
698 if (info
->type
== TYPE_DIR
) {
699 exfat_chain_set(&cdir
, info
->start_clu
,
700 EXFAT_B_TO_CLU(info
->size
, sbi
), info
->flags
);
701 count
= exfat_count_dir_entries(sb
, &cdir
);
705 info
->num_subdirs
= count
+ EXFAT_MIN_SUBDIR
;
710 static int exfat_d_anon_disconn(struct dentry
*dentry
)
712 return IS_ROOT(dentry
) && (dentry
->d_flags
& DCACHE_DISCONNECTED
);
715 static struct dentry
*exfat_lookup(struct inode
*dir
, struct dentry
*dentry
,
718 struct super_block
*sb
= dir
->i_sb
;
720 struct dentry
*alias
;
721 struct exfat_dir_entry info
;
726 mutex_lock(&EXFAT_SB(sb
)->s_lock
);
727 err
= exfat_find(dir
, &dentry
->d_name
, &info
);
729 if (err
== -ENOENT
) {
736 i_pos
= exfat_make_i_pos(&info
);
737 inode
= exfat_build_inode(sb
, &info
, i_pos
);
738 err
= PTR_ERR_OR_ZERO(inode
);
742 i_mode
= inode
->i_mode
;
743 alias
= d_find_alias(inode
);
746 * Checking "alias->d_parent == dentry->d_parent" to make sure
747 * FS is not corrupted (especially double linked dir).
749 if (alias
&& alias
->d_parent
== dentry
->d_parent
&&
750 !exfat_d_anon_disconn(alias
)) {
753 * Unhashed alias is able to exist because of revalidate()
754 * called by lookup_fast. You can easily make this status
755 * by calling create and lookup concurrently
756 * In such case, we reuse an alias instead of new dentry
758 if (d_unhashed(alias
)) {
759 WARN_ON(alias
->d_name
.hash_len
!=
760 dentry
->d_name
.hash_len
);
761 exfat_info(sb
, "rehashed a dentry(%p) in read lookup",
765 } else if (!S_ISDIR(i_mode
)) {
767 * This inode has non anonymous-DCACHE_DISCONNECTED
768 * dentry. This means, the user did ->lookup() by an
769 * another name (longname vs 8.3 alias of it) in past.
771 * Switch to new one for reason of locality if possible.
773 d_move(alias
, dentry
);
776 mutex_unlock(&EXFAT_SB(sb
)->s_lock
);
781 mutex_unlock(&EXFAT_SB(sb
)->s_lock
);
783 exfat_d_version_set(dentry
, inode_query_iversion(dir
));
785 return d_splice_alias(inode
, dentry
);
787 mutex_unlock(&EXFAT_SB(sb
)->s_lock
);
791 /* remove an entry, BUT don't truncate */
792 static int exfat_unlink(struct inode
*dir
, struct dentry
*dentry
)
794 struct super_block
*sb
= dir
->i_sb
;
795 struct inode
*inode
= dentry
->d_inode
;
796 struct exfat_inode_info
*ei
= EXFAT_I(inode
);
797 struct exfat_entry_set_cache es
;
800 if (unlikely(exfat_forced_shutdown(sb
)))
803 mutex_lock(&EXFAT_SB(sb
)->s_lock
);
804 if (ei
->dir
.dir
== DIR_DELETED
) {
805 exfat_err(sb
, "abnormal access to deleted dentry");
810 err
= exfat_get_dentry_set_by_ei(&es
, sb
, ei
);
816 exfat_set_volume_dirty(sb
);
818 /* update the directory entry */
819 exfat_remove_entries(inode
, &es
, ES_IDX_FILE
);
821 err
= exfat_put_dentry_set(&es
, IS_DIRSYNC(inode
));
825 /* This doesn't modify ei */
826 ei
->dir
.dir
= DIR_DELETED
;
828 inode_inc_iversion(dir
);
829 simple_inode_init_ts(dir
);
830 exfat_truncate_inode_atime(dir
);
831 mark_inode_dirty(dir
);
834 simple_inode_init_ts(inode
);
835 exfat_truncate_inode_atime(inode
);
836 exfat_unhash_inode(inode
);
837 exfat_d_version_set(dentry
, inode_query_iversion(dir
));
839 mutex_unlock(&EXFAT_SB(sb
)->s_lock
);
843 static int exfat_mkdir(struct mnt_idmap
*idmap
, struct inode
*dir
,
844 struct dentry
*dentry
, umode_t mode
)
846 struct super_block
*sb
= dir
->i_sb
;
848 struct exfat_dir_entry info
;
851 loff_t size
= i_size_read(dir
);
853 if (unlikely(exfat_forced_shutdown(sb
)))
856 mutex_lock(&EXFAT_SB(sb
)->s_lock
);
857 exfat_set_volume_dirty(sb
);
858 err
= exfat_add_entry(dir
, dentry
->d_name
.name
, TYPE_DIR
, &info
);
862 inode_inc_iversion(dir
);
863 inode_set_mtime_to_ts(dir
, inode_set_ctime_current(dir
));
864 if (IS_DIRSYNC(dir
) && size
!= i_size_read(dir
))
865 exfat_sync_inode(dir
);
867 mark_inode_dirty(dir
);
870 i_pos
= exfat_make_i_pos(&info
);
871 inode
= exfat_build_inode(sb
, &info
, i_pos
);
872 err
= PTR_ERR_OR_ZERO(inode
);
876 inode_inc_iversion(inode
);
877 EXFAT_I(inode
)->i_crtime
= simple_inode_init_ts(inode
);
878 exfat_truncate_inode_atime(inode
);
879 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
881 d_instantiate(dentry
, inode
);
884 mutex_unlock(&EXFAT_SB(sb
)->s_lock
);
888 static int exfat_check_dir_empty(struct super_block
*sb
,
889 struct exfat_chain
*p_dir
)
891 int i
, dentries_per_clu
;
893 struct exfat_chain clu
;
894 struct exfat_dentry
*ep
;
895 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
896 struct buffer_head
*bh
;
898 dentries_per_clu
= sbi
->dentries_per_clu
;
900 if (p_dir
->dir
== EXFAT_EOF_CLUSTER
)
903 exfat_chain_dup(&clu
, p_dir
);
905 while (clu
.dir
!= EXFAT_EOF_CLUSTER
) {
906 for (i
= 0; i
< dentries_per_clu
; i
++) {
907 ep
= exfat_get_dentry(sb
, &clu
, i
, &bh
);
910 type
= exfat_get_entry_type(ep
);
912 if (type
== TYPE_UNUSED
)
915 if (type
!= TYPE_FILE
&& type
!= TYPE_DIR
)
921 if (clu
.flags
== ALLOC_NO_FAT_CHAIN
) {
925 clu
.dir
= EXFAT_EOF_CLUSTER
;
927 if (exfat_get_next_cluster(sb
, &(clu
.dir
)))
935 static int exfat_rmdir(struct inode
*dir
, struct dentry
*dentry
)
937 struct inode
*inode
= dentry
->d_inode
;
938 struct exfat_chain clu_to_free
;
939 struct super_block
*sb
= inode
->i_sb
;
940 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
941 struct exfat_inode_info
*ei
= EXFAT_I(inode
);
942 struct exfat_entry_set_cache es
;
945 if (unlikely(exfat_forced_shutdown(sb
)))
948 mutex_lock(&EXFAT_SB(inode
->i_sb
)->s_lock
);
950 if (ei
->dir
.dir
== DIR_DELETED
) {
951 exfat_err(sb
, "abnormal access to deleted dentry");
956 exfat_chain_set(&clu_to_free
, ei
->start_clu
,
957 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode
), sbi
), ei
->flags
);
959 err
= exfat_check_dir_empty(sb
, &clu_to_free
);
962 exfat_err(sb
, "failed to exfat_check_dir_empty : err(%d)",
967 err
= exfat_get_dentry_set_by_ei(&es
, sb
, ei
);
973 exfat_set_volume_dirty(sb
);
975 exfat_remove_entries(inode
, &es
, ES_IDX_FILE
);
977 err
= exfat_put_dentry_set(&es
, IS_DIRSYNC(dir
));
981 ei
->dir
.dir
= DIR_DELETED
;
983 inode_inc_iversion(dir
);
984 simple_inode_init_ts(dir
);
985 exfat_truncate_inode_atime(dir
);
987 exfat_sync_inode(dir
);
989 mark_inode_dirty(dir
);
993 simple_inode_init_ts(inode
);
994 exfat_truncate_inode_atime(inode
);
995 exfat_unhash_inode(inode
);
996 exfat_d_version_set(dentry
, inode_query_iversion(dir
));
998 mutex_unlock(&EXFAT_SB(inode
->i_sb
)->s_lock
);
1002 static int exfat_rename_file(struct inode
*parent_inode
,
1003 struct exfat_uni_name
*p_uniname
, struct exfat_inode_info
*ei
)
1005 int ret
, num_new_entries
;
1006 struct exfat_dentry
*epold
, *epnew
;
1007 struct super_block
*sb
= parent_inode
->i_sb
;
1008 struct exfat_entry_set_cache old_es
, new_es
;
1009 int sync
= IS_DIRSYNC(parent_inode
);
1011 if (unlikely(exfat_forced_shutdown(sb
)))
1014 num_new_entries
= exfat_calc_num_entries(p_uniname
);
1015 if (num_new_entries
< 0)
1016 return num_new_entries
;
1018 ret
= exfat_get_dentry_set_by_ei(&old_es
, sb
, ei
);
1024 epold
= exfat_get_dentry_cached(&old_es
, ES_IDX_FILE
);
1026 if (old_es
.num_entries
< num_new_entries
) {
1028 struct exfat_chain dir
;
1030 newentry
= exfat_find_empty_entry(parent_inode
, &dir
,
1031 num_new_entries
, &new_es
);
1033 ret
= newentry
; /* -EIO or -ENOSPC */
1037 epnew
= exfat_get_dentry_cached(&new_es
, ES_IDX_FILE
);
1039 if (exfat_get_entry_type(epnew
) == TYPE_FILE
) {
1040 epnew
->dentry
.file
.attr
|= cpu_to_le16(EXFAT_ATTR_ARCHIVE
);
1041 ei
->attr
|= EXFAT_ATTR_ARCHIVE
;
1044 epold
= exfat_get_dentry_cached(&old_es
, ES_IDX_STREAM
);
1045 epnew
= exfat_get_dentry_cached(&new_es
, ES_IDX_STREAM
);
1048 exfat_init_ext_entry(&new_es
, num_new_entries
, p_uniname
);
1050 ret
= exfat_put_dentry_set(&new_es
, sync
);
1054 exfat_remove_entries(parent_inode
, &old_es
, ES_IDX_FILE
);
1056 ei
->entry
= newentry
;
1058 if (exfat_get_entry_type(epold
) == TYPE_FILE
) {
1059 epold
->dentry
.file
.attr
|= cpu_to_le16(EXFAT_ATTR_ARCHIVE
);
1060 ei
->attr
|= EXFAT_ATTR_ARCHIVE
;
1063 exfat_remove_entries(parent_inode
, &old_es
, ES_IDX_FIRST_FILENAME
+ 1);
1064 exfat_init_ext_entry(&old_es
, num_new_entries
, p_uniname
);
1066 return exfat_put_dentry_set(&old_es
, sync
);
1069 exfat_put_dentry_set(&old_es
, false);
1073 static int exfat_move_file(struct inode
*parent_inode
,
1074 struct exfat_uni_name
*p_uniname
, struct exfat_inode_info
*ei
)
1076 int ret
, newentry
, num_new_entries
;
1077 struct exfat_dentry
*epmov
, *epnew
;
1078 struct exfat_entry_set_cache mov_es
, new_es
;
1079 struct exfat_chain newdir
;
1081 num_new_entries
= exfat_calc_num_entries(p_uniname
);
1082 if (num_new_entries
< 0)
1083 return num_new_entries
;
1085 ret
= exfat_get_dentry_set_by_ei(&mov_es
, parent_inode
->i_sb
, ei
);
1089 newentry
= exfat_find_empty_entry(parent_inode
, &newdir
,
1090 num_new_entries
, &new_es
);
1092 ret
= newentry
; /* -EIO or -ENOSPC */
1096 epmov
= exfat_get_dentry_cached(&mov_es
, ES_IDX_FILE
);
1097 epnew
= exfat_get_dentry_cached(&new_es
, ES_IDX_FILE
);
1099 if (exfat_get_entry_type(epnew
) == TYPE_FILE
) {
1100 epnew
->dentry
.file
.attr
|= cpu_to_le16(EXFAT_ATTR_ARCHIVE
);
1101 ei
->attr
|= EXFAT_ATTR_ARCHIVE
;
1104 epmov
= exfat_get_dentry_cached(&mov_es
, ES_IDX_STREAM
);
1105 epnew
= exfat_get_dentry_cached(&new_es
, ES_IDX_STREAM
);
1108 exfat_init_ext_entry(&new_es
, num_new_entries
, p_uniname
);
1109 exfat_remove_entries(parent_inode
, &mov_es
, ES_IDX_FILE
);
1112 ei
->entry
= newentry
;
1114 ret
= exfat_put_dentry_set(&new_es
, IS_DIRSYNC(parent_inode
));
1118 return exfat_put_dentry_set(&mov_es
, IS_DIRSYNC(parent_inode
));
1121 exfat_put_dentry_set(&mov_es
, false);
1126 /* rename or move a old file into a new file */
1127 static int __exfat_rename(struct inode
*old_parent_inode
,
1128 struct exfat_inode_info
*ei
, struct inode
*new_parent_inode
,
1129 struct dentry
*new_dentry
)
1132 struct exfat_uni_name uni_name
;
1133 struct super_block
*sb
= old_parent_inode
->i_sb
;
1134 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
1135 const unsigned char *new_path
= new_dentry
->d_name
.name
;
1136 struct inode
*new_inode
= new_dentry
->d_inode
;
1137 struct exfat_inode_info
*new_ei
= NULL
;
1139 /* check the validity of pointer parameters */
1140 if (new_path
== NULL
|| strlen(new_path
) == 0)
1143 if (ei
->dir
.dir
== DIR_DELETED
) {
1144 exfat_err(sb
, "abnormal access to deleted source dentry");
1148 /* check whether new dir is existing directory and empty */
1151 new_ei
= EXFAT_I(new_inode
);
1153 if (new_ei
->dir
.dir
== DIR_DELETED
) {
1154 exfat_err(sb
, "abnormal access to deleted target dentry");
1158 /* if new_inode exists, update ei */
1159 if (S_ISDIR(new_inode
->i_mode
)) {
1160 struct exfat_chain new_clu
;
1162 new_clu
.dir
= new_ei
->start_clu
;
1164 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode
),
1166 new_clu
.flags
= new_ei
->flags
;
1168 ret
= exfat_check_dir_empty(sb
, &new_clu
);
1174 /* check the validity of directory name in the given new pathname */
1175 ret
= exfat_resolve_path(new_parent_inode
, new_path
, &uni_name
);
1179 exfat_set_volume_dirty(sb
);
1181 if (new_parent_inode
== old_parent_inode
)
1182 ret
= exfat_rename_file(new_parent_inode
, &uni_name
, ei
);
1184 ret
= exfat_move_file(new_parent_inode
, &uni_name
, ei
);
1186 if (!ret
&& new_inode
) {
1187 struct exfat_entry_set_cache es
;
1189 /* delete entries of new_dir */
1190 ret
= exfat_get_dentry_set_by_ei(&es
, sb
, new_ei
);
1196 exfat_remove_entries(new_inode
, &es
, ES_IDX_FILE
);
1198 ret
= exfat_put_dentry_set(&es
, IS_DIRSYNC(new_inode
));
1202 /* Free the clusters if new_inode is a dir(as if exfat_rmdir) */
1203 if (S_ISDIR(new_inode
->i_mode
) &&
1204 new_ei
->start_clu
!= EXFAT_EOF_CLUSTER
) {
1205 /* new_ei, new_clu_to_free */
1206 struct exfat_chain new_clu_to_free
;
1208 exfat_chain_set(&new_clu_to_free
, new_ei
->start_clu
,
1209 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode
),
1210 sbi
), new_ei
->flags
);
1212 if (exfat_free_cluster(new_inode
, &new_clu_to_free
)) {
1213 /* just set I/O error only */
1217 i_size_write(new_inode
, 0);
1218 new_ei
->valid_size
= 0;
1219 new_ei
->start_clu
= EXFAT_EOF_CLUSTER
;
1220 new_ei
->flags
= ALLOC_NO_FAT_CHAIN
;
1223 /* Update new_inode ei
1224 * Prevent syncing removed new_inode
1225 * (new_ei is already initialized above code ("if (new_inode)")
1227 new_ei
->dir
.dir
= DIR_DELETED
;
1233 static int exfat_rename(struct mnt_idmap
*idmap
,
1234 struct inode
*old_dir
, struct dentry
*old_dentry
,
1235 struct inode
*new_dir
, struct dentry
*new_dentry
,
1238 struct inode
*old_inode
, *new_inode
;
1239 struct super_block
*sb
= old_dir
->i_sb
;
1242 loff_t size
= i_size_read(new_dir
);
1245 * The VFS already checks for existence, so for local filesystems
1246 * the RENAME_NOREPLACE implementation is equivalent to plain rename.
1247 * Don't support any other flags
1249 if (flags
& ~RENAME_NOREPLACE
)
1252 mutex_lock(&EXFAT_SB(sb
)->s_lock
);
1253 old_inode
= old_dentry
->d_inode
;
1254 new_inode
= new_dentry
->d_inode
;
1256 err
= __exfat_rename(old_dir
, EXFAT_I(old_inode
), new_dir
, new_dentry
);
1260 inode_inc_iversion(new_dir
);
1261 simple_rename_timestamp(old_dir
, old_dentry
, new_dir
, new_dentry
);
1262 EXFAT_I(new_dir
)->i_crtime
= current_time(new_dir
);
1263 exfat_truncate_inode_atime(new_dir
);
1264 if (IS_DIRSYNC(new_dir
) && size
!= i_size_read(new_dir
))
1265 exfat_sync_inode(new_dir
);
1267 mark_inode_dirty(new_dir
);
1269 i_pos
= ((loff_t
)EXFAT_I(old_inode
)->dir
.dir
<< 32) |
1270 (EXFAT_I(old_inode
)->entry
& 0xffffffff);
1271 exfat_unhash_inode(old_inode
);
1272 exfat_hash_inode(old_inode
, i_pos
);
1273 if (IS_DIRSYNC(new_dir
))
1274 exfat_sync_inode(old_inode
);
1276 mark_inode_dirty(old_inode
);
1278 if (S_ISDIR(old_inode
->i_mode
) && old_dir
!= new_dir
) {
1279 drop_nlink(old_dir
);
1284 inode_inc_iversion(old_dir
);
1285 if (new_dir
!= old_dir
)
1286 mark_inode_dirty(old_dir
);
1289 exfat_unhash_inode(new_inode
);
1291 /* skip drop_nlink if new_inode already has been dropped */
1292 if (new_inode
->i_nlink
) {
1293 drop_nlink(new_inode
);
1294 if (S_ISDIR(new_inode
->i_mode
))
1295 drop_nlink(new_inode
);
1297 exfat_warn(sb
, "abnormal access to an inode dropped");
1298 WARN_ON(new_inode
->i_nlink
== 0);
1300 EXFAT_I(new_inode
)->i_crtime
= current_time(new_inode
);
1304 mutex_unlock(&EXFAT_SB(sb
)->s_lock
);
1308 const struct inode_operations exfat_dir_inode_operations
= {
1309 .create
= exfat_create
,
1310 .lookup
= exfat_lookup
,
1311 .unlink
= exfat_unlink
,
1312 .mkdir
= exfat_mkdir
,
1313 .rmdir
= exfat_rmdir
,
1314 .rename
= exfat_rename
,
1315 .setattr
= exfat_setattr
,
1316 .getattr
= exfat_getattr
,