exfat: move free cluster out of exfat_init_ext_entry()
[linux-block.git] / fs / exfat / namei.c
CommitLineData
5f2aa075
NJ
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
4 */
5
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>
11
12#include "exfat_raw.h"
13#include "exfat_fs.h"
14
15static inline unsigned long exfat_d_version(struct dentry *dentry)
16{
17 return (unsigned long) dentry->d_fsdata;
18}
19
20static inline void exfat_d_version_set(struct dentry *dentry,
21 unsigned long version)
22{
23 dentry->d_fsdata = (void *) version;
24}
25
26/*
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.
30 *
31 * If it happened, the negative dentry isn't actually negative anymore. So,
32 * drop it.
33 */
34static int exfat_d_revalidate(struct dentry *dentry, unsigned int flags)
35{
36 int ret;
37
38 if (flags & LOOKUP_RCU)
39 return -ECHILD;
40
41 /*
42 * This is not negative dentry. Always valid.
43 *
44 * Note, rename() to existing directory entry will have ->d_inode, and
45 * will use existing name which isn't specified name by user.
46 *
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.
50 */
51 if (d_really_is_positive(dentry))
52 return 1;
53
54 /*
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.
57 */
58 if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
59 return 0;
60
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;
66}
67
9ec784bf
VK
68/* returns the length of a struct qstr, ignoring trailing dots if necessary */
69static unsigned int exfat_striptail_len(unsigned int len, const char *name,
70 bool keep_last_dots)
5f2aa075 71{
9ec784bf
VK
72 if (!keep_last_dots) {
73 while (len && name[len - 1] == '.')
74 len--;
75 }
5f2aa075
NJ
76 return len;
77}
78
79/*
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.
83 */
84static int exfat_d_hash(const struct dentry *dentry, struct qstr *qstr)
85{
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;
9ec784bf
VK
89 unsigned int len = exfat_striptail_len(qstr->len, qstr->name,
90 EXFAT_SB(sb)->options.keep_last_dots);
5f2aa075
NJ
91 unsigned long hash = init_name_hash(dentry);
92 int i, charlen;
93 wchar_t c;
94
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);
100 }
101
102 qstr->hash = end_name_hash(hash);
103 return 0;
104}
105
106static int exfat_d_cmp(const struct dentry *dentry, unsigned int len,
107 const char *str, const struct qstr *name)
108{
109 struct super_block *sb = dentry->d_sb;
110 struct nls_table *t = EXFAT_SB(sb)->nls_io;
9ec784bf
VK
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);
5f2aa075
NJ
115 wchar_t c1, c2;
116 int charlen, i;
117
118 if (alen != blen)
119 return 1;
120
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;
127
128 if (exfat_toupper(sb, c1) != exfat_toupper(sb, c2))
129 return 1;
130 }
131
132 return 0;
133}
134
135const struct dentry_operations exfat_dentry_ops = {
136 .d_revalidate = exfat_d_revalidate,
137 .d_hash = exfat_d_hash,
138 .d_compare = exfat_d_cmp,
139};
140
141static int exfat_utf8_d_hash(const struct dentry *dentry, struct qstr *qstr)
142{
143 struct super_block *sb = dentry->d_sb;
144 const unsigned char *name = qstr->name;
9ec784bf
VK
145 unsigned int len = exfat_striptail_len(qstr->len, qstr->name,
146 EXFAT_SB(sb)->options.keep_last_dots);
5f2aa075
NJ
147 unsigned long hash = init_name_hash(dentry);
148 int i, charlen;
149 unicode_t u;
150
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;
155
156 /*
5f2aa075
NJ
157 * exfat_toupper() works only for code points up to the U+FFFF.
158 */
dddf7da3
PR
159 hash = partial_name_hash(u <= 0xFFFF ? exfat_toupper(sb, u) : u,
160 hash);
5f2aa075
NJ
161 }
162
163 qstr->hash = end_name_hash(hash);
164 return 0;
165}
166
167static int exfat_utf8_d_cmp(const struct dentry *dentry, unsigned int len,
168 const char *str, const struct qstr *name)
169{
170 struct super_block *sb = dentry->d_sb;
9ec784bf
VK
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);
175
5f2aa075
NJ
176 unicode_t u_a, u_b;
177 int charlen, i;
178
179 if (alen != blen)
180 return 1;
181
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;
188
189 if (u_a <= 0xFFFF && u_b <= 0xFFFF) {
190 if (exfat_toupper(sb, u_a) != exfat_toupper(sb, u_b))
191 return 1;
5f2aa075 192 } else {
197298a6
PR
193 if (u_a != u_b)
194 return 1;
5f2aa075
NJ
195 }
196 }
197
198 return 0;
199}
200
201const 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,
205};
206
207/* used only in search empty_slot() */
208#define CNT_UNUSED_NOHIT (-1)
209#define CNT_UNUSED_HIT (-2)
210/* search EMPTY CONTINUOUS "num_entries" entries */
211static int exfat_search_empty_slot(struct super_block *sb,
212 struct exfat_hint_femp *hint_femp, struct exfat_chain *p_dir,
213 int num_entries)
214{
215 int i, dentry, num_empty = 0;
216 int dentries_per_clu;
217 unsigned int type;
218 struct exfat_chain clu;
219 struct exfat_dentry *ep;
220 struct exfat_sb_info *sbi = EXFAT_SB(sb);
221 struct buffer_head *bh;
222
223 dentries_per_clu = sbi->dentries_per_clu;
224
225 if (hint_femp->eidx != EXFAT_HINT_NONE) {
226 dentry = hint_femp->eidx;
5f2aa075 227
e298c8a8
YM
228 /*
229 * If hint_femp->count is enough, it is needed to check if
230 * there are actual empty entries.
231 * Otherwise, and if "dentry + hint_famp->count" is also equal
232 * to "p_dir->size * dentries_per_clu", it means ENOSPC.
233 */
234 if (dentry + hint_femp->count == p_dir->size * dentries_per_clu &&
235 num_entries > hint_femp->count)
236 return -ENOSPC;
237
238 hint_femp->eidx = EXFAT_HINT_NONE;
5f2aa075
NJ
239 exfat_chain_dup(&clu, &hint_femp->cur);
240 } else {
241 exfat_chain_dup(&clu, p_dir);
242 dentry = 0;
243 }
244
245 while (clu.dir != EXFAT_EOF_CLUSTER) {
246 i = dentry & (dentries_per_clu - 1);
247
248 for (; i < dentries_per_clu; i++, dentry++) {
c71510b3 249 ep = exfat_get_dentry(sb, &clu, i, &bh);
5f2aa075
NJ
250 if (!ep)
251 return -EIO;
252 type = exfat_get_entry_type(ep);
253 brelse(bh);
254
255 if (type == TYPE_UNUSED || type == TYPE_DELETED) {
256 num_empty++;
257 if (hint_femp->eidx == EXFAT_HINT_NONE) {
258 hint_femp->eidx = dentry;
259 hint_femp->count = CNT_UNUSED_NOHIT;
260 exfat_chain_set(&hint_femp->cur,
261 clu.dir, clu.size, clu.flags);
262 }
263
264 if (type == TYPE_UNUSED &&
265 hint_femp->count != CNT_UNUSED_HIT)
266 hint_femp->count = CNT_UNUSED_HIT;
267 } else {
268 if (hint_femp->eidx != EXFAT_HINT_NONE &&
269 hint_femp->count == CNT_UNUSED_HIT) {
270 /* unused empty group means
271 * an empty group which includes
272 * unused dentry
273 */
274 exfat_fs_error(sb,
275 "found bogus dentry(%d) beyond unused empty group(%d) (start_clu : %u, cur_clu : %u)",
276 dentry, hint_femp->eidx,
277 p_dir->dir, clu.dir);
278 return -EIO;
279 }
280
281 num_empty = 0;
282 hint_femp->eidx = EXFAT_HINT_NONE;
283 }
284
285 if (num_empty >= num_entries) {
286 /* found and invalidate hint_femp */
287 hint_femp->eidx = EXFAT_HINT_NONE;
288 return (dentry - (num_entries - 1));
289 }
290 }
291
292 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
293 if (--clu.size > 0)
294 clu.dir++;
295 else
296 clu.dir = EXFAT_EOF_CLUSTER;
297 } else {
298 if (exfat_get_next_cluster(sb, &clu.dir))
299 return -EIO;
300 }
301 }
302
e298c8a8
YM
303 hint_femp->eidx = p_dir->size * dentries_per_clu - num_empty;
304 hint_femp->count = num_empty;
305 if (num_empty == 0)
306 exfat_chain_set(&hint_femp->cur, EXFAT_EOF_CLUSTER, 0,
307 clu.flags);
308
5f2aa075
NJ
309 return -ENOSPC;
310}
311
312static int exfat_check_max_dentries(struct inode *inode)
313{
314 if (EXFAT_B_TO_DEN(i_size_read(inode)) >= MAX_EXFAT_DENTRIES) {
315 /*
9e456aea 316 * exFAT spec allows a dir to grow up to 8388608(256MB)
5f2aa075
NJ
317 * dentries
318 */
319 return -ENOSPC;
320 }
321 return 0;
322}
323
324/* find empty directory entry.
325 * if there isn't any empty slot, expand cluster chain.
326 */
327static int exfat_find_empty_entry(struct inode *inode,
328 struct exfat_chain *p_dir, int num_entries)
329{
330 int dentry;
331 unsigned int ret, last_clu;
5f2aa075
NJ
332 loff_t size = 0;
333 struct exfat_chain clu;
5f2aa075
NJ
334 struct super_block *sb = inode->i_sb;
335 struct exfat_sb_info *sbi = EXFAT_SB(sb);
336 struct exfat_inode_info *ei = EXFAT_I(inode);
337 struct exfat_hint_femp hint_femp;
338
339 hint_femp.eidx = EXFAT_HINT_NONE;
340
341 if (ei->hint_femp.eidx != EXFAT_HINT_NONE) {
a7a24168 342 hint_femp = ei->hint_femp;
5f2aa075
NJ
343 ei->hint_femp.eidx = EXFAT_HINT_NONE;
344 }
345
346 while ((dentry = exfat_search_empty_slot(sb, &hint_femp, p_dir,
347 num_entries)) < 0) {
348 if (dentry == -EIO)
349 break;
350
351 if (exfat_check_max_dentries(inode))
352 return -ENOSPC;
353
5f2aa075
NJ
354 /*
355 * Allocate new cluster to this directory
356 */
dab48b8f
YM
357 if (ei->start_clu != EXFAT_EOF_CLUSTER) {
358 /* we trust p_dir->size regardless of FAT type */
359 if (exfat_find_last_cluster(sb, p_dir, &last_clu))
360 return -EIO;
361
362 exfat_chain_set(&clu, last_clu + 1, 0, p_dir->flags);
363 } else {
364 /* This directory is empty */
365 exfat_chain_set(&clu, EXFAT_EOF_CLUSTER, 0,
366 ALLOC_NO_FAT_CHAIN);
367 }
5f2aa075
NJ
368
369 /* allocate a cluster */
23befe49 370 ret = exfat_alloc_cluster(inode, 1, &clu, IS_DIRSYNC(inode));
5f2aa075
NJ
371 if (ret)
372 return ret;
373
374 if (exfat_zeroed_cluster(inode, clu.dir))
375 return -EIO;
376
dab48b8f
YM
377 if (ei->start_clu == EXFAT_EOF_CLUSTER) {
378 ei->start_clu = clu.dir;
379 p_dir->dir = clu.dir;
380 }
381
5f2aa075
NJ
382 /* append to the FAT chain */
383 if (clu.flags != p_dir->flags) {
384 /* no-fat-chain bit is disabled,
385 * so fat-chain should be synced with alloc-bitmap
386 */
387 exfat_chain_cont_cluster(sb, p_dir->dir, p_dir->size);
388 p_dir->flags = ALLOC_FAT_CHAIN;
389 hint_femp.cur.flags = ALLOC_FAT_CHAIN;
390 }
391
392 if (clu.flags == ALLOC_FAT_CHAIN)
393 if (exfat_ent_set(sb, last_clu, clu.dir))
394 return -EIO;
395
e298c8a8 396 if (hint_femp.cur.dir == EXFAT_EOF_CLUSTER)
5f2aa075 397 exfat_chain_set(&hint_femp.cur, clu.dir, 0, clu.flags);
e298c8a8
YM
398
399 hint_femp.count += sbi->dentries_per_clu;
400
5f2aa075
NJ
401 hint_femp.cur.size++;
402 p_dir->size++;
403 size = EXFAT_CLU_TO_B(p_dir->size, sbi);
404
5f2aa075
NJ
405 /* directory inode should be updated in here */
406 i_size_write(inode, size);
7dee6f57
CVB
407 ei->i_size_ondisk += sbi->cluster_size;
408 ei->i_size_aligned += sbi->cluster_size;
11a347fb 409 ei->valid_size += sbi->cluster_size;
7dee6f57 410 ei->flags = p_dir->flags;
39c1ce8e 411 inode->i_blocks += sbi->cluster_size >> 9;
5f2aa075
NJ
412 }
413
414 return dentry;
415}
416
417/*
418 * Name Resolution Functions :
419 * Zero if it was successful; otherwise nonzero.
420 */
421static int __exfat_resolve_path(struct inode *inode, const unsigned char *path,
422 struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
423 int lookup)
424{
425 int namelen;
426 int lossy = NLS_NAME_NO_LOSSY;
427 struct super_block *sb = inode->i_sb;
428 struct exfat_sb_info *sbi = EXFAT_SB(sb);
429 struct exfat_inode_info *ei = EXFAT_I(inode);
9ec784bf 430 int pathlen = strlen(path);
5f2aa075 431
9ec784bf
VK
432 /*
433 * get the length of the pathname excluding
434 * trailing periods, if any.
435 */
436 namelen = exfat_striptail_len(pathlen, path, false);
437 if (EXFAT_SB(sb)->options.keep_last_dots) {
438 /*
439 * Do not allow the creation of files with names
440 * ending with period(s).
441 */
442 if (!lookup && (namelen < pathlen))
443 return -EINVAL;
444 namelen = pathlen;
445 }
5f2aa075
NJ
446 if (!namelen)
447 return -ENOENT;
9ec784bf 448 if (pathlen > (MAX_NAME_LENGTH * MAX_CHARSET_SIZE))
5f2aa075
NJ
449 return -ENAMETOOLONG;
450
451 /*
452 * strip all leading spaces :
453 * "MS windows 7" supports leading spaces.
454 * So we should skip this preprocessing for compatibility.
455 */
456
457 /* file name conversion :
458 * If lookup case, we allow bad-name for compatibility.
459 */
460 namelen = exfat_nls_to_utf16(sb, path, namelen, p_uniname,
461 &lossy);
462 if (namelen < 0)
463 return namelen; /* return error value */
464
465 if ((lossy && !lookup) || !namelen)
86da53e8 466 return (lossy & NLS_NAME_OVERLEN) ? -ENAMETOOLONG : -EINVAL;
5f2aa075
NJ
467
468 exfat_chain_set(p_dir, ei->start_clu,
469 EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags);
470
471 return 0;
472}
473
474static inline int exfat_resolve_path(struct inode *inode,
475 const unsigned char *path, struct exfat_chain *dir,
476 struct exfat_uni_name *uni)
477{
478 return __exfat_resolve_path(inode, path, dir, uni, 0);
479}
480
481static inline int exfat_resolve_path_for_lookup(struct inode *inode,
482 const unsigned char *path, struct exfat_chain *dir,
483 struct exfat_uni_name *uni)
484{
485 return __exfat_resolve_path(inode, path, dir, uni, 1);
486}
487
488static inline loff_t exfat_make_i_pos(struct exfat_dir_entry *info)
489{
490 return ((loff_t) info->dir.dir << 32) | (info->entry & 0xffffffff);
491}
492
493static int exfat_add_entry(struct inode *inode, const char *path,
494 struct exfat_chain *p_dir, unsigned int type,
495 struct exfat_dir_entry *info)
496{
497 int ret, dentry, num_entries;
498 struct super_block *sb = inode->i_sb;
499 struct exfat_sb_info *sbi = EXFAT_SB(sb);
500 struct exfat_uni_name uniname;
501 struct exfat_chain clu;
cf8663fa
YM
502 struct timespec64 ts = current_time(inode);
503 struct exfat_entry_set_cache es;
5f2aa075
NJ
504 int clu_size = 0;
505 unsigned int start_clu = EXFAT_FREE_CLUSTER;
506
507 ret = exfat_resolve_path(inode, path, p_dir, &uniname);
508 if (ret)
509 goto out;
510
511 num_entries = exfat_calc_num_entries(&uniname);
512 if (num_entries < 0) {
513 ret = num_entries;
514 goto out;
515 }
516
517 /* exfat_find_empty_entry must be called before alloc_cluster() */
518 dentry = exfat_find_empty_entry(inode, p_dir, num_entries);
519 if (dentry < 0) {
520 ret = dentry; /* -EIO or -ENOSPC */
521 goto out;
522 }
523
ee785c15 524 if (type == TYPE_DIR && !sbi->options.zero_size_dir) {
5f2aa075
NJ
525 ret = exfat_alloc_new_dir(inode, &clu);
526 if (ret)
527 goto out;
528 start_clu = clu.dir;
529 clu_size = sbi->cluster_size;
530 }
531
532 /* update the directory entry */
533 /* fill the dos name directory entry information of the created file.
534 * the first cluster is not determined yet. (0)
535 */
cf8663fa
YM
536
537 ret = exfat_get_empty_dentry_set(&es, sb, p_dir, dentry, num_entries);
538 if (ret)
539 goto out;
540
541 exfat_init_dir_entry(&es, type, start_clu, clu_size, &ts);
542
543 ret = exfat_put_dentry_set(&es, IS_DIRSYNC(inode));
5f2aa075
NJ
544 if (ret)
545 goto out;
546
547 ret = exfat_init_ext_entry(inode, p_dir, dentry, num_entries, &uniname);
548 if (ret)
549 goto out;
550
a7a24168 551 info->dir = *p_dir;
5f2aa075
NJ
552 info->entry = dentry;
553 info->flags = ALLOC_NO_FAT_CHAIN;
554 info->type = type;
555
556 if (type == TYPE_FILE) {
0ab8ba71 557 info->attr = EXFAT_ATTR_ARCHIVE;
5f2aa075
NJ
558 info->start_clu = EXFAT_EOF_CLUSTER;
559 info->size = 0;
560 info->num_subdirs = 0;
561 } else {
0ab8ba71 562 info->attr = EXFAT_ATTR_SUBDIR;
ee785c15
YM
563 if (sbi->options.zero_size_dir)
564 info->start_clu = EXFAT_EOF_CLUSTER;
565 else
566 info->start_clu = start_clu;
5f2aa075 567 info->size = clu_size;
6c958a09 568 info->num_subdirs = EXFAT_MIN_SUBDIR;
5f2aa075 569 }
11a347fb
YM
570 info->valid_size = info->size;
571
5f2aa075
NJ
572 memset(&info->crtime, 0, sizeof(info->crtime));
573 memset(&info->mtime, 0, sizeof(info->mtime));
574 memset(&info->atime, 0, sizeof(info->atime));
575out:
576 return ret;
577}
578
6c960e68 579static int exfat_create(struct mnt_idmap *idmap, struct inode *dir,
549c7297 580 struct dentry *dentry, umode_t mode, bool excl)
5f2aa075
NJ
581{
582 struct super_block *sb = dir->i_sb;
583 struct inode *inode;
584 struct exfat_chain cdir;
585 struct exfat_dir_entry info;
586 loff_t i_pos;
587 int err;
588
589 mutex_lock(&EXFAT_SB(sb)->s_lock);
7018ec68 590 exfat_set_volume_dirty(sb);
5f2aa075
NJ
591 err = exfat_add_entry(dir, dentry->d_name.name, &cdir, TYPE_FILE,
592 &info);
5f2aa075
NJ
593 if (err)
594 goto unlock;
595
596 inode_inc_iversion(dir);
4c72a36e 597 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
5f2aa075
NJ
598 if (IS_DIRSYNC(dir))
599 exfat_sync_inode(dir);
600 else
601 mark_inode_dirty(dir);
602
603 i_pos = exfat_make_i_pos(&info);
604 inode = exfat_build_inode(sb, &info, i_pos);
d6c9efd9
TK
605 err = PTR_ERR_OR_ZERO(inode);
606 if (err)
5f2aa075
NJ
607 goto unlock;
608
609 inode_inc_iversion(inode);
4c72a36e
JL
610 EXFAT_I(inode)->i_crtime = simple_inode_init_ts(inode);
611 exfat_truncate_inode_atime(inode);
612
5f2aa075
NJ
613 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
614
615 d_instantiate(dentry, inode);
616unlock:
617 mutex_unlock(&EXFAT_SB(sb)->s_lock);
618 return err;
619}
620
621/* lookup a file */
622static int exfat_find(struct inode *dir, struct qstr *qname,
623 struct exfat_dir_entry *info)
624{
72880cb5 625 int ret, dentry, count;
5f2aa075
NJ
626 struct exfat_chain cdir;
627 struct exfat_uni_name uni_name;
5f2aa075
NJ
628 struct super_block *sb = dir->i_sb;
629 struct exfat_sb_info *sbi = EXFAT_SB(sb);
630 struct exfat_inode_info *ei = EXFAT_I(dir);
188df41f 631 struct exfat_dentry *ep, *ep2;
20914ff6 632 struct exfat_entry_set_cache es;
c6e2f52e
HK
633 /* for optimized dir & entry to prevent long traverse of cluster chain */
634 struct exfat_hint hint_opt;
5f2aa075
NJ
635
636 if (qname->len == 0)
637 return -ENOENT;
638
639 /* check the validity of directory name in the given pathname */
640 ret = exfat_resolve_path_for_lookup(dir, qname->name, &cdir, &uni_name);
641 if (ret)
642 return ret;
643
5f2aa075
NJ
644 /* check the validation of hint_stat and initialize it if required */
645 if (ei->version != (inode_peek_iversion_raw(dir) & 0xffffffff)) {
646 ei->hint_stat.clu = cdir.dir;
647 ei->hint_stat.eidx = 0;
648 ei->version = (inode_peek_iversion_raw(dir) & 0xffffffff);
649 ei->hint_femp.eidx = EXFAT_HINT_NONE;
650 }
651
652 /* search the file name for directories */
72880cb5 653 dentry = exfat_find_dir_entry(sb, ei, &cdir, &uni_name, &hint_opt);
188df41f 654 if (dentry < 0)
5f2aa075
NJ
655 return dentry; /* -error value */
656
a7a24168 657 info->dir = cdir;
5f2aa075
NJ
658 info->entry = dentry;
659 info->num_subdirs = 0;
660
c6e2f52e
HK
661 /* adjust cdir to the optimized value */
662 cdir.dir = hint_opt.clu;
663 if (cdir.flags & ALLOC_NO_FAT_CHAIN)
664 cdir.size -= dentry / sbi->dentries_per_clu;
665 dentry = hint_opt.eidx;
20914ff6 666 if (exfat_get_dentry_set(&es, sb, &cdir, dentry, ES_2_ENTRIES))
188df41f 667 return -EIO;
f3fe3954
YM
668 ep = exfat_get_dentry_cached(&es, ES_IDX_FILE);
669 ep2 = exfat_get_dentry_cached(&es, ES_IDX_STREAM);
188df41f
TK
670
671 info->type = exfat_get_entry_type(ep);
672 info->attr = le16_to_cpu(ep->dentry.file.attr);
673 info->size = le64_to_cpu(ep2->dentry.stream.valid_size);
11a347fb
YM
674 info->valid_size = le64_to_cpu(ep2->dentry.stream.valid_size);
675 info->size = le64_to_cpu(ep2->dentry.stream.size);
dab48b8f 676 if (info->size == 0) {
188df41f
TK
677 info->flags = ALLOC_NO_FAT_CHAIN;
678 info->start_clu = EXFAT_EOF_CLUSTER;
679 } else {
680 info->flags = ep2->dentry.stream.flags;
681 info->start_clu =
682 le32_to_cpu(ep2->dentry.stream.start_clu);
683 }
5f2aa075 684
188df41f
TK
685 exfat_get_entry_time(sbi, &info->crtime,
686 ep->dentry.file.create_tz,
687 ep->dentry.file.create_time,
688 ep->dentry.file.create_date,
689 ep->dentry.file.create_time_cs);
690 exfat_get_entry_time(sbi, &info->mtime,
691 ep->dentry.file.modify_tz,
692 ep->dentry.file.modify_time,
693 ep->dentry.file.modify_date,
694 ep->dentry.file.modify_time_cs);
695 exfat_get_entry_time(sbi, &info->atime,
696 ep->dentry.file.access_tz,
697 ep->dentry.file.access_time,
698 ep->dentry.file.access_date,
699 0);
3b9681ac 700 exfat_put_dentry_set(&es, false);
188df41f
TK
701
702 if (ei->start_clu == EXFAT_FREE_CLUSTER) {
703 exfat_fs_error(sb,
704 "non-zero size file starts with zero cluster (size : %llu, p_dir : %u, entry : 0x%08x)",
705 i_size_read(dir), ei->dir.dir, ei->entry);
706 return -EIO;
707 }
5f2aa075 708
188df41f
TK
709 if (info->type == TYPE_DIR) {
710 exfat_chain_set(&cdir, info->start_clu,
711 EXFAT_B_TO_CLU(info->size, sbi), info->flags);
5f2aa075
NJ
712 count = exfat_count_dir_entries(sb, &cdir);
713 if (count < 0)
714 return -EIO;
715
188df41f 716 info->num_subdirs = count + EXFAT_MIN_SUBDIR;
5f2aa075
NJ
717 }
718 return 0;
719}
720
721static int exfat_d_anon_disconn(struct dentry *dentry)
722{
723 return IS_ROOT(dentry) && (dentry->d_flags & DCACHE_DISCONNECTED);
724}
725
726static struct dentry *exfat_lookup(struct inode *dir, struct dentry *dentry,
727 unsigned int flags)
728{
729 struct super_block *sb = dir->i_sb;
730 struct inode *inode;
731 struct dentry *alias;
732 struct exfat_dir_entry info;
733 int err;
734 loff_t i_pos;
735 mode_t i_mode;
736
737 mutex_lock(&EXFAT_SB(sb)->s_lock);
738 err = exfat_find(dir, &dentry->d_name, &info);
739 if (err) {
740 if (err == -ENOENT) {
741 inode = NULL;
742 goto out;
743 }
744 goto unlock;
745 }
746
747 i_pos = exfat_make_i_pos(&info);
748 inode = exfat_build_inode(sb, &info, i_pos);
d6c9efd9
TK
749 err = PTR_ERR_OR_ZERO(inode);
750 if (err)
5f2aa075 751 goto unlock;
5f2aa075
NJ
752
753 i_mode = inode->i_mode;
754 alias = d_find_alias(inode);
755
756 /*
757 * Checking "alias->d_parent == dentry->d_parent" to make sure
758 * FS is not corrupted (especially double linked dir).
759 */
760 if (alias && alias->d_parent == dentry->d_parent &&
761 !exfat_d_anon_disconn(alias)) {
762
763 /*
764 * Unhashed alias is able to exist because of revalidate()
765 * called by lookup_fast. You can easily make this status
766 * by calling create and lookup concurrently
767 * In such case, we reuse an alias instead of new dentry
768 */
769 if (d_unhashed(alias)) {
770 WARN_ON(alias->d_name.hash_len !=
771 dentry->d_name.hash_len);
d1727d55
JP
772 exfat_info(sb, "rehashed a dentry(%p) in read lookup",
773 alias);
5f2aa075
NJ
774 d_drop(dentry);
775 d_rehash(alias);
776 } else if (!S_ISDIR(i_mode)) {
777 /*
778 * This inode has non anonymous-DCACHE_DISCONNECTED
779 * dentry. This means, the user did ->lookup() by an
780 * another name (longname vs 8.3 alias of it) in past.
781 *
782 * Switch to new one for reason of locality if possible.
783 */
784 d_move(alias, dentry);
785 }
786 iput(inode);
787 mutex_unlock(&EXFAT_SB(sb)->s_lock);
788 return alias;
789 }
790 dput(alias);
791out:
792 mutex_unlock(&EXFAT_SB(sb)->s_lock);
793 if (!inode)
794 exfat_d_version_set(dentry, inode_query_iversion(dir));
795
796 return d_splice_alias(inode, dentry);
797unlock:
798 mutex_unlock(&EXFAT_SB(sb)->s_lock);
799 return ERR_PTR(err);
800}
801
802/* remove an entry, BUT don't truncate */
803static int exfat_unlink(struct inode *dir, struct dentry *dentry)
804{
805 struct exfat_chain cdir;
5f2aa075
NJ
806 struct super_block *sb = dir->i_sb;
807 struct inode *inode = dentry->d_inode;
808 struct exfat_inode_info *ei = EXFAT_I(inode);
ff4343da
YM
809 struct exfat_entry_set_cache es;
810 int entry, err = 0;
5f2aa075
NJ
811
812 mutex_lock(&EXFAT_SB(sb)->s_lock);
813 exfat_chain_dup(&cdir, &ei->dir);
814 entry = ei->entry;
815 if (ei->dir.dir == DIR_DELETED) {
d1727d55 816 exfat_err(sb, "abnormal access to deleted dentry");
5f2aa075
NJ
817 err = -ENOENT;
818 goto unlock;
819 }
820
ff4343da
YM
821 err = exfat_get_dentry_set(&es, sb, &cdir, entry, ES_ALL_ENTRIES);
822 if (err) {
5f2aa075 823 err = -EIO;
5f2aa075
NJ
824 goto unlock;
825 }
5f2aa075 826
7018ec68 827 exfat_set_volume_dirty(sb);
ff4343da 828
5f2aa075 829 /* update the directory entry */
ff4343da
YM
830 exfat_remove_entries(inode, &es, ES_IDX_FILE);
831
832 err = exfat_put_dentry_set(&es, IS_DIRSYNC(inode));
833 if (err)
5f2aa075 834 goto unlock;
5f2aa075
NJ
835
836 /* This doesn't modify ei */
837 ei->dir.dir = DIR_DELETED;
5f2aa075
NJ
838
839 inode_inc_iversion(dir);
4c72a36e
JL
840 simple_inode_init_ts(dir);
841 exfat_truncate_inode_atime(dir);
5f2aa075
NJ
842 if (IS_DIRSYNC(dir))
843 exfat_sync_inode(dir);
844 else
845 mark_inode_dirty(dir);
846
847 clear_nlink(inode);
4c72a36e
JL
848 simple_inode_init_ts(inode);
849 exfat_truncate_inode_atime(inode);
5f2aa075
NJ
850 exfat_unhash_inode(inode);
851 exfat_d_version_set(dentry, inode_query_iversion(dir));
852unlock:
853 mutex_unlock(&EXFAT_SB(sb)->s_lock);
854 return err;
855}
856
c54bd91e 857static int exfat_mkdir(struct mnt_idmap *idmap, struct inode *dir,
549c7297 858 struct dentry *dentry, umode_t mode)
5f2aa075
NJ
859{
860 struct super_block *sb = dir->i_sb;
861 struct inode *inode;
862 struct exfat_dir_entry info;
863 struct exfat_chain cdir;
864 loff_t i_pos;
865 int err;
866
867 mutex_lock(&EXFAT_SB(sb)->s_lock);
7018ec68 868 exfat_set_volume_dirty(sb);
5f2aa075
NJ
869 err = exfat_add_entry(dir, dentry->d_name.name, &cdir, TYPE_DIR,
870 &info);
5f2aa075
NJ
871 if (err)
872 goto unlock;
873
874 inode_inc_iversion(dir);
4c72a36e 875 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
5f2aa075
NJ
876 if (IS_DIRSYNC(dir))
877 exfat_sync_inode(dir);
878 else
879 mark_inode_dirty(dir);
880 inc_nlink(dir);
881
882 i_pos = exfat_make_i_pos(&info);
883 inode = exfat_build_inode(sb, &info, i_pos);
d6c9efd9
TK
884 err = PTR_ERR_OR_ZERO(inode);
885 if (err)
5f2aa075 886 goto unlock;
5f2aa075
NJ
887
888 inode_inc_iversion(inode);
4c72a36e
JL
889 EXFAT_I(inode)->i_crtime = simple_inode_init_ts(inode);
890 exfat_truncate_inode_atime(inode);
5f2aa075
NJ
891 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
892
893 d_instantiate(dentry, inode);
894
895unlock:
896 mutex_unlock(&EXFAT_SB(sb)->s_lock);
897 return err;
898}
899
900static int exfat_check_dir_empty(struct super_block *sb,
901 struct exfat_chain *p_dir)
902{
903 int i, dentries_per_clu;
904 unsigned int type;
905 struct exfat_chain clu;
906 struct exfat_dentry *ep;
907 struct exfat_sb_info *sbi = EXFAT_SB(sb);
908 struct buffer_head *bh;
909
910 dentries_per_clu = sbi->dentries_per_clu;
911
dab48b8f
YM
912 if (p_dir->dir == EXFAT_EOF_CLUSTER)
913 return 0;
914
5f2aa075
NJ
915 exfat_chain_dup(&clu, p_dir);
916
917 while (clu.dir != EXFAT_EOF_CLUSTER) {
918 for (i = 0; i < dentries_per_clu; i++) {
c71510b3 919 ep = exfat_get_dentry(sb, &clu, i, &bh);
5f2aa075
NJ
920 if (!ep)
921 return -EIO;
922 type = exfat_get_entry_type(ep);
923 brelse(bh);
924 if (type == TYPE_UNUSED)
925 return 0;
926
927 if (type != TYPE_FILE && type != TYPE_DIR)
928 continue;
929
930 return -ENOTEMPTY;
931 }
932
933 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
934 if (--clu.size > 0)
935 clu.dir++;
936 else
937 clu.dir = EXFAT_EOF_CLUSTER;
938 } else {
939 if (exfat_get_next_cluster(sb, &(clu.dir)))
940 return -EIO;
941 }
942 }
943
944 return 0;
945}
946
947static int exfat_rmdir(struct inode *dir, struct dentry *dentry)
948{
949 struct inode *inode = dentry->d_inode;
5f2aa075
NJ
950 struct exfat_chain cdir, clu_to_free;
951 struct super_block *sb = inode->i_sb;
952 struct exfat_sb_info *sbi = EXFAT_SB(sb);
953 struct exfat_inode_info *ei = EXFAT_I(inode);
ff4343da
YM
954 struct exfat_entry_set_cache es;
955 int entry, err;
5f2aa075
NJ
956
957 mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
958
959 exfat_chain_dup(&cdir, &ei->dir);
960 entry = ei->entry;
961
962 if (ei->dir.dir == DIR_DELETED) {
d1727d55 963 exfat_err(sb, "abnormal access to deleted dentry");
5f2aa075
NJ
964 err = -ENOENT;
965 goto unlock;
966 }
967
5f2aa075
NJ
968 exfat_chain_set(&clu_to_free, ei->start_clu,
969 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi), ei->flags);
970
971 err = exfat_check_dir_empty(sb, &clu_to_free);
972 if (err) {
973 if (err == -EIO)
d1727d55
JP
974 exfat_err(sb, "failed to exfat_check_dir_empty : err(%d)",
975 err);
5f2aa075
NJ
976 goto unlock;
977 }
978
ff4343da
YM
979 err = exfat_get_dentry_set(&es, sb, &cdir, entry, ES_ALL_ENTRIES);
980 if (err) {
5f2aa075 981 err = -EIO;
5f2aa075
NJ
982 goto unlock;
983 }
5f2aa075 984
7018ec68 985 exfat_set_volume_dirty(sb);
ff4343da
YM
986
987 exfat_remove_entries(inode, &es, ES_IDX_FILE);
988
989 err = exfat_put_dentry_set(&es, IS_DIRSYNC(dir));
990 if (err)
5f2aa075 991 goto unlock;
ff4343da 992
5f2aa075 993 ei->dir.dir = DIR_DELETED;
5f2aa075
NJ
994
995 inode_inc_iversion(dir);
4c72a36e
JL
996 simple_inode_init_ts(dir);
997 exfat_truncate_inode_atime(dir);
5f2aa075
NJ
998 if (IS_DIRSYNC(dir))
999 exfat_sync_inode(dir);
1000 else
1001 mark_inode_dirty(dir);
1002 drop_nlink(dir);
1003
1004 clear_nlink(inode);
4c72a36e
JL
1005 simple_inode_init_ts(inode);
1006 exfat_truncate_inode_atime(inode);
5f2aa075
NJ
1007 exfat_unhash_inode(inode);
1008 exfat_d_version_set(dentry, inode_query_iversion(dir));
1009unlock:
1010 mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
1011 return err;
1012}
1013
1014static int exfat_rename_file(struct inode *inode, struct exfat_chain *p_dir,
1015 int oldentry, struct exfat_uni_name *p_uniname,
1016 struct exfat_inode_info *ei)
1017{
ff4343da 1018 int ret, num_new_entries;
5f2aa075
NJ
1019 struct exfat_dentry *epold, *epnew;
1020 struct super_block *sb = inode->i_sb;
ff4343da
YM
1021 struct buffer_head *new_bh;
1022 struct exfat_entry_set_cache old_es;
5f2aa075
NJ
1023 int sync = IS_DIRSYNC(inode);
1024
5f2aa075
NJ
1025 num_new_entries = exfat_calc_num_entries(p_uniname);
1026 if (num_new_entries < 0)
1027 return num_new_entries;
1028
ff4343da
YM
1029 ret = exfat_get_dentry_set(&old_es, sb, p_dir, oldentry, ES_ALL_ENTRIES);
1030 if (ret) {
1031 ret = -EIO;
1032 return ret;
1033 }
1034
1035 epold = exfat_get_dentry_cached(&old_es, ES_IDX_FILE);
1036
1037 if (old_es.num_entries < num_new_entries) {
5f2aa075
NJ
1038 int newentry;
1039
1040 newentry =
1041 exfat_find_empty_entry(inode, p_dir, num_new_entries);
ff4343da
YM
1042 if (newentry < 0) {
1043 ret = newentry; /* -EIO or -ENOSPC */
1044 goto put_old_es;
1045 }
5f2aa075 1046
c71510b3 1047 epnew = exfat_get_dentry(sb, p_dir, newentry, &new_bh);
ff4343da
YM
1048 if (!epnew) {
1049 ret = -EIO;
1050 goto put_old_es;
1051 }
5f2aa075 1052
a7a24168 1053 *epnew = *epold;
5f2aa075 1054 if (exfat_get_entry_type(epnew) == TYPE_FILE) {
0ab8ba71
JC
1055 epnew->dentry.file.attr |= cpu_to_le16(EXFAT_ATTR_ARCHIVE);
1056 ei->attr |= EXFAT_ATTR_ARCHIVE;
5f2aa075 1057 }
2c7f8937 1058 exfat_update_bh(new_bh, sync);
5f2aa075
NJ
1059 brelse(new_bh);
1060
ff4343da 1061 epold = exfat_get_dentry_cached(&old_es, ES_IDX_STREAM);
c71510b3 1062 epnew = exfat_get_dentry(sb, p_dir, newentry + 1, &new_bh);
e8dd3cda 1063 if (!epnew) {
ff4343da
YM
1064 ret = -EIO;
1065 goto put_old_es;
e8dd3cda 1066 }
5f2aa075 1067
a7a24168 1068 *epnew = *epold;
2c7f8937 1069 exfat_update_bh(new_bh, sync);
5f2aa075
NJ
1070 brelse(new_bh);
1071
1072 ret = exfat_init_ext_entry(inode, p_dir, newentry,
1073 num_new_entries, p_uniname);
1074 if (ret)
ff4343da 1075 goto put_old_es;
5f2aa075 1076
ff4343da 1077 exfat_remove_entries(inode, &old_es, ES_IDX_FILE);
d8dad258 1078 ei->dir = *p_dir;
5f2aa075
NJ
1079 ei->entry = newentry;
1080 } else {
1081 if (exfat_get_entry_type(epold) == TYPE_FILE) {
0ab8ba71
JC
1082 epold->dentry.file.attr |= cpu_to_le16(EXFAT_ATTR_ARCHIVE);
1083 ei->attr |= EXFAT_ATTR_ARCHIVE;
5f2aa075 1084 }
4e1aa22f
YM
1085
1086 exfat_remove_entries(inode, &old_es, ES_IDX_FIRST_FILENAME + 1);
1087
5f2aa075
NJ
1088 ret = exfat_init_ext_entry(inode, p_dir, oldentry,
1089 num_new_entries, p_uniname);
1090 if (ret)
ff4343da 1091 goto put_old_es;
5f2aa075 1092 }
ff4343da
YM
1093 return exfat_put_dentry_set(&old_es, sync);
1094
1095put_old_es:
1096 exfat_put_dentry_set(&old_es, false);
1097 return ret;
5f2aa075
NJ
1098}
1099
1100static int exfat_move_file(struct inode *inode, struct exfat_chain *p_olddir,
1101 int oldentry, struct exfat_chain *p_newdir,
1102 struct exfat_uni_name *p_uniname, struct exfat_inode_info *ei)
1103{
ff4343da 1104 int ret, newentry, num_new_entries;
5f2aa075
NJ
1105 struct exfat_dentry *epmov, *epnew;
1106 struct super_block *sb = inode->i_sb;
ff4343da
YM
1107 struct buffer_head *new_bh;
1108 struct exfat_entry_set_cache mov_es;
5f2aa075
NJ
1109
1110 num_new_entries = exfat_calc_num_entries(p_uniname);
1111 if (num_new_entries < 0)
1112 return num_new_entries;
1113
1114 newentry = exfat_find_empty_entry(inode, p_newdir, num_new_entries);
1115 if (newentry < 0)
1116 return newentry; /* -EIO or -ENOSPC */
1117
ff4343da
YM
1118 ret = exfat_get_dentry_set(&mov_es, sb, p_olddir, oldentry,
1119 ES_ALL_ENTRIES);
1120 if (ret)
5f2aa075
NJ
1121 return -EIO;
1122
ff4343da
YM
1123 epmov = exfat_get_dentry_cached(&mov_es, ES_IDX_FILE);
1124 epnew = exfat_get_dentry(sb, p_newdir, newentry, &new_bh);
1125 if (!epnew) {
1126 ret = -EIO;
1127 goto put_mov_es;
1128 }
1129
a7a24168 1130 *epnew = *epmov;
5f2aa075 1131 if (exfat_get_entry_type(epnew) == TYPE_FILE) {
0ab8ba71
JC
1132 epnew->dentry.file.attr |= cpu_to_le16(EXFAT_ATTR_ARCHIVE);
1133 ei->attr |= EXFAT_ATTR_ARCHIVE;
5f2aa075 1134 }
2c7f8937 1135 exfat_update_bh(new_bh, IS_DIRSYNC(inode));
5f2aa075
NJ
1136 brelse(new_bh);
1137
ff4343da 1138 epmov = exfat_get_dentry_cached(&mov_es, ES_IDX_STREAM);
c71510b3 1139 epnew = exfat_get_dentry(sb, p_newdir, newentry + 1, &new_bh);
e8dd3cda 1140 if (!epnew) {
ff4343da
YM
1141 ret = -EIO;
1142 goto put_mov_es;
e8dd3cda 1143 }
5f2aa075 1144
a7a24168 1145 *epnew = *epmov;
2c7f8937 1146 exfat_update_bh(new_bh, IS_DIRSYNC(inode));
5f2aa075
NJ
1147 brelse(new_bh);
1148
1149 ret = exfat_init_ext_entry(inode, p_newdir, newentry, num_new_entries,
1150 p_uniname);
1151 if (ret)
1152 return ret;
1153
ff4343da 1154 exfat_remove_entries(inode, &mov_es, ES_IDX_FILE);
5f2aa075
NJ
1155
1156 exfat_chain_set(&ei->dir, p_newdir->dir, p_newdir->size,
1157 p_newdir->flags);
1158
1159 ei->entry = newentry;
ff4343da
YM
1160 return exfat_put_dentry_set(&mov_es, IS_DIRSYNC(inode));
1161
1162put_mov_es:
1163 exfat_put_dentry_set(&mov_es, false);
1164
1165 return ret;
5f2aa075
NJ
1166}
1167
5f2aa075
NJ
1168/* rename or move a old file into a new file */
1169static int __exfat_rename(struct inode *old_parent_inode,
1170 struct exfat_inode_info *ei, struct inode *new_parent_inode,
1171 struct dentry *new_dentry)
1172{
1173 int ret;
1174 int dentry;
1175 struct exfat_chain olddir, newdir;
1176 struct exfat_chain *p_dir = NULL;
1177 struct exfat_uni_name uni_name;
1178 struct exfat_dentry *ep;
1179 struct super_block *sb = old_parent_inode->i_sb;
1180 struct exfat_sb_info *sbi = EXFAT_SB(sb);
1181 const unsigned char *new_path = new_dentry->d_name.name;
1182 struct inode *new_inode = new_dentry->d_inode;
5f2aa075
NJ
1183 struct exfat_inode_info *new_ei = NULL;
1184 unsigned int new_entry_type = TYPE_UNUSED;
1185 int new_entry = 0;
015c0d4f 1186 struct buffer_head *new_bh = NULL;
5f2aa075
NJ
1187
1188 /* check the validity of pointer parameters */
1189 if (new_path == NULL || strlen(new_path) == 0)
1190 return -EINVAL;
1191
1192 if (ei->dir.dir == DIR_DELETED) {
d1727d55 1193 exfat_err(sb, "abnormal access to deleted source dentry");
5f2aa075
NJ
1194 return -ENOENT;
1195 }
1196
204e6cea
SS
1197 exfat_chain_set(&olddir, EXFAT_I(old_parent_inode)->start_clu,
1198 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(old_parent_inode), sbi),
1199 EXFAT_I(old_parent_inode)->flags);
5f2aa075
NJ
1200 dentry = ei->entry;
1201
5f2aa075
NJ
1202 /* check whether new dir is existing directory and empty */
1203 if (new_inode) {
1204 ret = -EIO;
1205 new_ei = EXFAT_I(new_inode);
1206
1207 if (new_ei->dir.dir == DIR_DELETED) {
d1727d55 1208 exfat_err(sb, "abnormal access to deleted target dentry");
5f2aa075
NJ
1209 goto out;
1210 }
1211
5f2aa075
NJ
1212 p_dir = &(new_ei->dir);
1213 new_entry = new_ei->entry;
c71510b3 1214 ep = exfat_get_dentry(sb, p_dir, new_entry, &new_bh);
5f2aa075
NJ
1215 if (!ep)
1216 goto out;
1217
1218 new_entry_type = exfat_get_entry_type(ep);
1219 brelse(new_bh);
1220
1221 /* if new_inode exists, update ei */
1222 if (new_entry_type == TYPE_DIR) {
1223 struct exfat_chain new_clu;
1224
1225 new_clu.dir = new_ei->start_clu;
1226 new_clu.size =
1227 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode),
1228 sbi);
1229 new_clu.flags = new_ei->flags;
1230
1231 ret = exfat_check_dir_empty(sb, &new_clu);
1232 if (ret)
1233 goto out;
1234 }
1235 }
1236
1237 /* check the validity of directory name in the given new pathname */
1238 ret = exfat_resolve_path(new_parent_inode, new_path, &newdir,
1239 &uni_name);
1240 if (ret)
1241 goto out;
1242
7018ec68 1243 exfat_set_volume_dirty(sb);
5f2aa075
NJ
1244
1245 if (olddir.dir == newdir.dir)
1246 ret = exfat_rename_file(new_parent_inode, &olddir, dentry,
1247 &uni_name, ei);
1248 else
1249 ret = exfat_move_file(new_parent_inode, &olddir, dentry,
1250 &newdir, &uni_name, ei);
1251
1252 if (!ret && new_inode) {
ff4343da
YM
1253 struct exfat_entry_set_cache es;
1254
5f2aa075 1255 /* delete entries of new_dir */
ff4343da
YM
1256 ret = exfat_get_dentry_set(&es, sb, p_dir, new_entry,
1257 ES_ALL_ENTRIES);
1258 if (ret) {
5f2aa075
NJ
1259 ret = -EIO;
1260 goto del_out;
1261 }
1262
ff4343da 1263 exfat_remove_entries(new_inode, &es, ES_IDX_FILE);
5f2aa075 1264
ff4343da
YM
1265 ret = exfat_put_dentry_set(&es, IS_DIRSYNC(new_inode));
1266 if (ret)
5f2aa075 1267 goto del_out;
5f2aa075
NJ
1268
1269 /* Free the clusters if new_inode is a dir(as if exfat_rmdir) */
dab48b8f
YM
1270 if (new_entry_type == TYPE_DIR &&
1271 new_ei->start_clu != EXFAT_EOF_CLUSTER) {
5f2aa075
NJ
1272 /* new_ei, new_clu_to_free */
1273 struct exfat_chain new_clu_to_free;
1274
1275 exfat_chain_set(&new_clu_to_free, new_ei->start_clu,
1276 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode),
1277 sbi), new_ei->flags);
1278
1279 if (exfat_free_cluster(new_inode, &new_clu_to_free)) {
1280 /* just set I/O error only */
1281 ret = -EIO;
1282 }
1283
1284 i_size_write(new_inode, 0);
11a347fb 1285 new_ei->valid_size = 0;
5f2aa075
NJ
1286 new_ei->start_clu = EXFAT_EOF_CLUSTER;
1287 new_ei->flags = ALLOC_NO_FAT_CHAIN;
1288 }
1289del_out:
1290 /* Update new_inode ei
1291 * Prevent syncing removed new_inode
1292 * (new_ei is already initialized above code ("if (new_inode)")
1293 */
1294 new_ei->dir.dir = DIR_DELETED;
1295 }
5f2aa075
NJ
1296out:
1297 return ret;
1298}
1299
e18275ae 1300static int exfat_rename(struct mnt_idmap *idmap,
549c7297
CB
1301 struct inode *old_dir, struct dentry *old_dentry,
1302 struct inode *new_dir, struct dentry *new_dentry,
1303 unsigned int flags)
5f2aa075
NJ
1304{
1305 struct inode *old_inode, *new_inode;
1306 struct super_block *sb = old_dir->i_sb;
1307 loff_t i_pos;
1308 int err;
1309
1310 /*
1311 * The VFS already checks for existence, so for local filesystems
1312 * the RENAME_NOREPLACE implementation is equivalent to plain rename.
1313 * Don't support any other flags
1314 */
1315 if (flags & ~RENAME_NOREPLACE)
1316 return -EINVAL;
1317
1318 mutex_lock(&EXFAT_SB(sb)->s_lock);
1319 old_inode = old_dentry->d_inode;
1320 new_inode = new_dentry->d_inode;
1321
1322 err = __exfat_rename(old_dir, EXFAT_I(old_inode), new_dir, new_dentry);
1323 if (err)
1324 goto unlock;
1325
1326 inode_inc_iversion(new_dir);
d06cdfdd
JL
1327 simple_rename_timestamp(old_dir, old_dentry, new_dir, new_dentry);
1328 EXFAT_I(new_dir)->i_crtime = current_time(new_dir);
4c72a36e 1329 exfat_truncate_inode_atime(new_dir);
5f2aa075
NJ
1330 if (IS_DIRSYNC(new_dir))
1331 exfat_sync_inode(new_dir);
1332 else
1333 mark_inode_dirty(new_dir);
1334
1335 i_pos = ((loff_t)EXFAT_I(old_inode)->dir.dir << 32) |
1336 (EXFAT_I(old_inode)->entry & 0xffffffff);
1337 exfat_unhash_inode(old_inode);
1338 exfat_hash_inode(old_inode, i_pos);
1339 if (IS_DIRSYNC(new_dir))
1340 exfat_sync_inode(old_inode);
1341 else
1342 mark_inode_dirty(old_inode);
1343
1344 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
1345 drop_nlink(old_dir);
1346 if (!new_inode)
1347 inc_nlink(new_dir);
1348 }
1349
1350 inode_inc_iversion(old_dir);
5f2aa075
NJ
1351 if (IS_DIRSYNC(old_dir))
1352 exfat_sync_inode(old_dir);
1353 else
1354 mark_inode_dirty(old_dir);
1355
1356 if (new_inode) {
1357 exfat_unhash_inode(new_inode);
1358
1359 /* skip drop_nlink if new_inode already has been dropped */
1360 if (new_inode->i_nlink) {
1361 drop_nlink(new_inode);
1362 if (S_ISDIR(new_inode->i_mode))
1363 drop_nlink(new_inode);
1364 } else {
d1727d55 1365 exfat_warn(sb, "abnormal access to an inode dropped");
5f2aa075
NJ
1366 WARN_ON(new_inode->i_nlink == 0);
1367 }
f29f1908 1368 EXFAT_I(new_inode)->i_crtime = current_time(new_inode);
5f2aa075
NJ
1369 }
1370
1371unlock:
1372 mutex_unlock(&EXFAT_SB(sb)->s_lock);
1373 return err;
1374}
1375
1376const struct inode_operations exfat_dir_inode_operations = {
1377 .create = exfat_create,
1378 .lookup = exfat_lookup,
1379 .unlink = exfat_unlink,
1380 .mkdir = exfat_mkdir,
1381 .rmdir = exfat_rmdir,
1382 .rename = exfat_rename,
1383 .setattr = exfat_setattr,
1384 .getattr = exfat_getattr,
1385};