Linux 6.13-rc4
[linux.git] / fs / exfat / namei.c
blob97d2774760fe3dead731bc01c02d1764014ec0cc
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
4 */
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"
13 #include "exfat_fs.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,
32 * drop it.
34 static int exfat_d_revalidate(struct dentry *dentry, unsigned int flags)
36 int ret;
38 if (flags & LOOKUP_RCU)
39 return -ECHILD;
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))
52 return 1;
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))
59 return 0;
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);
65 return ret;
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,
70 bool keep_last_dots)
72 if (!keep_last_dots) {
73 while (len && name[len - 1] == '.')
74 len--;
76 return len;
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);
92 int i, charlen;
93 wchar_t c;
95 for (i = 0; i < len; i += charlen) {
96 charlen = t->char2uni(&name[i], len - i, &c);
97 if (charlen < 0)
98 return charlen;
99 hash = partial_name_hash(exfat_toupper(sb, c), hash);
102 qstr->hash = end_name_hash(hash);
103 return 0;
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);
115 wchar_t c1, c2;
116 int charlen, i;
118 if (alen != blen)
119 return 1;
121 for (i = 0; i < len; i += charlen) {
122 charlen = t->char2uni(&name->name[i], alen - i, &c1);
123 if (charlen < 0)
124 return 1;
125 if (charlen != t->char2uni(&str[i], blen - i, &c2))
126 return 1;
128 if (exfat_toupper(sb, c1) != exfat_toupper(sb, c2))
129 return 1;
132 return 0;
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);
148 int i, charlen;
149 unicode_t u;
151 for (i = 0; i < len; i += charlen) {
152 charlen = utf8_to_utf32(&name[i], len - i, &u);
153 if (charlen < 0)
154 return charlen;
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,
160 hash);
163 qstr->hash = end_name_hash(hash);
164 return 0;
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);
176 unicode_t u_a, u_b;
177 int charlen, i;
179 if (alen != blen)
180 return 1;
182 for (i = 0; i < alen; i += charlen) {
183 charlen = utf8_to_utf32(&name->name[i], alen - i, &u_a);
184 if (charlen < 0)
185 return 1;
186 if (charlen != utf8_to_utf32(&str[i], blen - i, &u_b))
187 return 1;
189 if (u_a <= 0xFFFF && u_b <= 0xFFFF) {
190 if (exfat_toupper(sb, u_a) != exfat_toupper(sb, u_b))
191 return 1;
192 } else {
193 if (u_a != u_b)
194 return 1;
198 return 0;
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)
212 int i, dentry, ret;
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)
231 return -ENOSPC;
233 hint_femp->eidx = EXFAT_HINT_NONE;
234 exfat_chain_dup(&clu, &hint_femp->cur);
235 } else {
236 exfat_chain_dup(&clu, p_dir);
237 dentry = 0;
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);
245 if (ret < 0)
246 return ret;
247 else if (ret == 0)
248 return dentry;
250 dentry += ret;
251 i += ret;
253 while (i >= dentries_per_clu) {
254 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
255 if (--clu.size > 0)
256 clu.dir++;
257 else
258 clu.dir = EXFAT_EOF_CLUSTER;
259 } else {
260 if (exfat_get_next_cluster(sb, &clu.dir))
261 return -EIO;
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,
272 clu.flags);
273 else
274 hint_femp->cur = clu;
276 return -ENOSPC;
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)
284 * dentries
286 return -ENOSPC;
288 return 0;
292 * Find an empty directory entry set.
294 * If there isn't any empty slot, expand cluster chain.
296 * in:
297 * inode: inode of the parent directory
298 * num_entries: specifies how many dentries in the empty directory entry set
300 * out:
301 * p_dir: the cluster where the empty directory entry set is located
302 * es: The found empty directory entry set
304 * return:
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)
312 int dentry;
313 unsigned int ret, last_clu;
314 loff_t size = 0;
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) {
333 if (dentry == -EIO)
334 break;
336 if (exfat_check_max_dentries(inode))
337 return -ENOSPC;
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))
345 return -EIO;
347 exfat_chain_set(&clu, last_clu + 1, 0, p_dir->flags);
348 } else {
349 /* This directory is empty */
350 exfat_chain_set(&clu, EXFAT_EOF_CLUSTER, 0,
351 ALLOC_NO_FAT_CHAIN);
354 /* allocate a cluster */
355 ret = exfat_alloc_cluster(inode, 1, &clu, IS_DIRSYNC(inode));
356 if (ret)
357 return ret;
359 if (exfat_zeroed_cluster(inode, clu.dir))
360 return -EIO;
362 if (ei->start_clu == EXFAT_EOF_CLUSTER) {
363 ei->start_clu = clu.dir;
364 p_dir->dir = clu.dir;
365 hint_femp.eidx = 0;
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))
380 return -EIO;
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++;
388 p_dir->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)
411 int namelen;
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))
427 return -EINVAL;
428 namelen = pathlen;
430 if (!namelen)
431 return -ENOENT;
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,
445 &lossy);
446 if (namelen < 0)
447 return namelen; /* return error value */
449 if ((lossy && !lookup) || !namelen)
450 return (lossy & NLS_NAME_OVERLEN) ? -ENAMETOOLONG : -EINVAL;
452 return 0;
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;
482 int clu_size = 0;
483 unsigned int start_clu = EXFAT_FREE_CLUSTER;
485 ret = exfat_resolve_path(inode, path, &uniname);
486 if (ret)
487 goto out;
489 num_entries = exfat_calc_num_entries(&uniname);
490 if (num_entries < 0) {
491 ret = num_entries;
492 goto out;
495 /* exfat_find_empty_entry must be called before alloc_cluster() */
496 dentry = exfat_find_empty_entry(inode, &info->dir, num_entries, &es);
497 if (dentry < 0) {
498 ret = dentry; /* -EIO or -ENOSPC */
499 goto out;
502 if (type == TYPE_DIR && !sbi->options.zero_size_dir) {
503 ret = exfat_alloc_new_dir(inode, &clu);
504 if (ret) {
505 exfat_put_dentry_set(&es, false);
506 goto out;
508 start_clu = clu.dir;
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));
520 if (ret)
521 goto out;
523 info->entry = dentry;
524 info->flags = ALLOC_NO_FAT_CHAIN;
525 info->type = type;
527 if (type == TYPE_FILE) {
528 info->attr = EXFAT_ATTR_ARCHIVE;
529 info->start_clu = EXFAT_EOF_CLUSTER;
530 info->size = 0;
531 info->num_subdirs = 0;
532 } else {
533 info->attr = EXFAT_ATTR_SUBDIR;
534 if (sbi->options.zero_size_dir)
535 info->start_clu = EXFAT_EOF_CLUSTER;
536 else
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));
546 out:
547 return ret;
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;
554 struct inode *inode;
555 struct exfat_dir_entry info;
556 loff_t i_pos;
557 int err;
558 loff_t size = i_size_read(dir);
560 if (unlikely(exfat_forced_shutdown(sb)))
561 return -EIO;
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);
566 if (err)
567 goto unlock;
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);
573 else
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);
579 if (err)
580 goto unlock;
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);
589 unlock:
590 mutex_unlock(&EXFAT_SB(sb)->s_lock);
591 return err;
594 /* lookup a file */
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;
609 if (qname->len == 0)
610 return -ENOENT;
612 /* check the validity of directory name in the given pathname */
613 ret = exfat_resolve_path_for_lookup(dir, qname->name, &uni_name);
614 if (ret)
615 return ret;
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);
630 if (dentry < 0)
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;
639 info->dir = cdir;
640 info->entry = dentry;
641 info->num_subdirs = 0;
643 if (exfat_get_dentry_set(&es, sb, &cdir, dentry, ES_2_ENTRIES))
644 return -EIO;
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)",
657 info->start_clu);
658 info->size = 0;
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;
671 } else
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) {
692 exfat_fs_error(sb,
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);
695 return -EIO;
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);
702 if (count < 0)
703 return -EIO;
705 info->num_subdirs = count + EXFAT_MIN_SUBDIR;
707 return 0;
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,
716 unsigned int flags)
718 struct super_block *sb = dir->i_sb;
719 struct inode *inode;
720 struct dentry *alias;
721 struct exfat_dir_entry info;
722 int err;
723 loff_t i_pos;
724 mode_t i_mode;
726 mutex_lock(&EXFAT_SB(sb)->s_lock);
727 err = exfat_find(dir, &dentry->d_name, &info);
728 if (err) {
729 if (err == -ENOENT) {
730 inode = NULL;
731 goto out;
733 goto unlock;
736 i_pos = exfat_make_i_pos(&info);
737 inode = exfat_build_inode(sb, &info, i_pos);
738 err = PTR_ERR_OR_ZERO(inode);
739 if (err)
740 goto unlock;
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",
762 alias);
763 d_drop(dentry);
764 d_rehash(alias);
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);
775 iput(inode);
776 mutex_unlock(&EXFAT_SB(sb)->s_lock);
777 return alias;
779 dput(alias);
780 out:
781 mutex_unlock(&EXFAT_SB(sb)->s_lock);
782 if (!inode)
783 exfat_d_version_set(dentry, inode_query_iversion(dir));
785 return d_splice_alias(inode, dentry);
786 unlock:
787 mutex_unlock(&EXFAT_SB(sb)->s_lock);
788 return ERR_PTR(err);
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;
798 int err = 0;
800 if (unlikely(exfat_forced_shutdown(sb)))
801 return -EIO;
803 mutex_lock(&EXFAT_SB(sb)->s_lock);
804 if (ei->dir.dir == DIR_DELETED) {
805 exfat_err(sb, "abnormal access to deleted dentry");
806 err = -ENOENT;
807 goto unlock;
810 err = exfat_get_dentry_set_by_ei(&es, sb, ei);
811 if (err) {
812 err = -EIO;
813 goto unlock;
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));
822 if (err)
823 goto unlock;
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);
833 clear_nlink(inode);
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));
838 unlock:
839 mutex_unlock(&EXFAT_SB(sb)->s_lock);
840 return err;
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;
847 struct inode *inode;
848 struct exfat_dir_entry info;
849 loff_t i_pos;
850 int err;
851 loff_t size = i_size_read(dir);
853 if (unlikely(exfat_forced_shutdown(sb)))
854 return -EIO;
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);
859 if (err)
860 goto unlock;
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);
866 else
867 mark_inode_dirty(dir);
868 inc_nlink(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);
873 if (err)
874 goto unlock;
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);
883 unlock:
884 mutex_unlock(&EXFAT_SB(sb)->s_lock);
885 return err;
888 static int exfat_check_dir_empty(struct super_block *sb,
889 struct exfat_chain *p_dir)
891 int i, dentries_per_clu;
892 unsigned int type;
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)
901 return 0;
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);
908 if (!ep)
909 return -EIO;
910 type = exfat_get_entry_type(ep);
911 brelse(bh);
912 if (type == TYPE_UNUSED)
913 return 0;
915 if (type != TYPE_FILE && type != TYPE_DIR)
916 continue;
918 return -ENOTEMPTY;
921 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
922 if (--clu.size > 0)
923 clu.dir++;
924 else
925 clu.dir = EXFAT_EOF_CLUSTER;
926 } else {
927 if (exfat_get_next_cluster(sb, &(clu.dir)))
928 return -EIO;
932 return 0;
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;
943 int err;
945 if (unlikely(exfat_forced_shutdown(sb)))
946 return -EIO;
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");
952 err = -ENOENT;
953 goto unlock;
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);
960 if (err) {
961 if (err == -EIO)
962 exfat_err(sb, "failed to exfat_check_dir_empty : err(%d)",
963 err);
964 goto unlock;
967 err = exfat_get_dentry_set_by_ei(&es, sb, ei);
968 if (err) {
969 err = -EIO;
970 goto unlock;
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));
978 if (err)
979 goto unlock;
981 ei->dir.dir = DIR_DELETED;
983 inode_inc_iversion(dir);
984 simple_inode_init_ts(dir);
985 exfat_truncate_inode_atime(dir);
986 if (IS_DIRSYNC(dir))
987 exfat_sync_inode(dir);
988 else
989 mark_inode_dirty(dir);
990 drop_nlink(dir);
992 clear_nlink(inode);
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));
997 unlock:
998 mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
999 return err;
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)))
1012 return -EIO;
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);
1019 if (ret) {
1020 ret = -EIO;
1021 return ret;
1024 epold = exfat_get_dentry_cached(&old_es, ES_IDX_FILE);
1026 if (old_es.num_entries < num_new_entries) {
1027 int newentry;
1028 struct exfat_chain dir;
1030 newentry = exfat_find_empty_entry(parent_inode, &dir,
1031 num_new_entries, &new_es);
1032 if (newentry < 0) {
1033 ret = newentry; /* -EIO or -ENOSPC */
1034 goto put_old_es;
1037 epnew = exfat_get_dentry_cached(&new_es, ES_IDX_FILE);
1038 *epnew = *epold;
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);
1046 *epnew = *epold;
1048 exfat_init_ext_entry(&new_es, num_new_entries, p_uniname);
1050 ret = exfat_put_dentry_set(&new_es, sync);
1051 if (ret)
1052 goto put_old_es;
1054 exfat_remove_entries(parent_inode, &old_es, ES_IDX_FILE);
1055 ei->dir = dir;
1056 ei->entry = newentry;
1057 } else {
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);
1068 put_old_es:
1069 exfat_put_dentry_set(&old_es, false);
1070 return ret;
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);
1086 if (ret)
1087 return -EIO;
1089 newentry = exfat_find_empty_entry(parent_inode, &newdir,
1090 num_new_entries, &new_es);
1091 if (newentry < 0) {
1092 ret = newentry; /* -EIO or -ENOSPC */
1093 goto put_mov_es;
1096 epmov = exfat_get_dentry_cached(&mov_es, ES_IDX_FILE);
1097 epnew = exfat_get_dentry_cached(&new_es, ES_IDX_FILE);
1098 *epnew = *epmov;
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);
1106 *epnew = *epmov;
1108 exfat_init_ext_entry(&new_es, num_new_entries, p_uniname);
1109 exfat_remove_entries(parent_inode, &mov_es, ES_IDX_FILE);
1111 ei->dir = newdir;
1112 ei->entry = newentry;
1114 ret = exfat_put_dentry_set(&new_es, IS_DIRSYNC(parent_inode));
1115 if (ret)
1116 goto put_mov_es;
1118 return exfat_put_dentry_set(&mov_es, IS_DIRSYNC(parent_inode));
1120 put_mov_es:
1121 exfat_put_dentry_set(&mov_es, false);
1123 return ret;
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)
1131 int ret;
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)
1141 return -EINVAL;
1143 if (ei->dir.dir == DIR_DELETED) {
1144 exfat_err(sb, "abnormal access to deleted source dentry");
1145 return -ENOENT;
1148 /* check whether new dir is existing directory and empty */
1149 if (new_inode) {
1150 ret = -EIO;
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");
1155 goto out;
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;
1163 new_clu.size =
1164 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode),
1165 sbi);
1166 new_clu.flags = new_ei->flags;
1168 ret = exfat_check_dir_empty(sb, &new_clu);
1169 if (ret)
1170 goto out;
1174 /* check the validity of directory name in the given new pathname */
1175 ret = exfat_resolve_path(new_parent_inode, new_path, &uni_name);
1176 if (ret)
1177 goto out;
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);
1183 else
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);
1191 if (ret) {
1192 ret = -EIO;
1193 goto del_out;
1196 exfat_remove_entries(new_inode, &es, ES_IDX_FILE);
1198 ret = exfat_put_dentry_set(&es, IS_DIRSYNC(new_inode));
1199 if (ret)
1200 goto del_out;
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 */
1214 ret = -EIO;
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;
1222 del_out:
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;
1229 out:
1230 return ret;
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,
1236 unsigned int flags)
1238 struct inode *old_inode, *new_inode;
1239 struct super_block *sb = old_dir->i_sb;
1240 loff_t i_pos;
1241 int err;
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)
1250 return -EINVAL;
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);
1257 if (err)
1258 goto unlock;
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);
1266 else
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);
1275 else
1276 mark_inode_dirty(old_inode);
1278 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
1279 drop_nlink(old_dir);
1280 if (!new_inode)
1281 inc_nlink(new_dir);
1284 inode_inc_iversion(old_dir);
1285 if (new_dir != old_dir)
1286 mark_inode_dirty(old_dir);
1288 if (new_inode) {
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);
1296 } else {
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);
1303 unlock:
1304 mutex_unlock(&EXFAT_SB(sb)->s_lock);
1305 return err;
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,