exfat: convert exfat_init_ext_entry() to use dentry cache
[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);
d97e0606 542 exfat_init_ext_entry(&es, num_entries, &uniname);
cf8663fa
YM
543
544 ret = exfat_put_dentry_set(&es, IS_DIRSYNC(inode));
5f2aa075
NJ
545 if (ret)
546 goto out;
547
a7a24168 548 info->dir = *p_dir;
5f2aa075
NJ
549 info->entry = dentry;
550 info->flags = ALLOC_NO_FAT_CHAIN;
551 info->type = type;
552
553 if (type == TYPE_FILE) {
0ab8ba71 554 info->attr = EXFAT_ATTR_ARCHIVE;
5f2aa075
NJ
555 info->start_clu = EXFAT_EOF_CLUSTER;
556 info->size = 0;
557 info->num_subdirs = 0;
558 } else {
0ab8ba71 559 info->attr = EXFAT_ATTR_SUBDIR;
ee785c15
YM
560 if (sbi->options.zero_size_dir)
561 info->start_clu = EXFAT_EOF_CLUSTER;
562 else
563 info->start_clu = start_clu;
5f2aa075 564 info->size = clu_size;
6c958a09 565 info->num_subdirs = EXFAT_MIN_SUBDIR;
5f2aa075 566 }
11a347fb
YM
567 info->valid_size = info->size;
568
5f2aa075
NJ
569 memset(&info->crtime, 0, sizeof(info->crtime));
570 memset(&info->mtime, 0, sizeof(info->mtime));
571 memset(&info->atime, 0, sizeof(info->atime));
572out:
573 return ret;
574}
575
6c960e68 576static int exfat_create(struct mnt_idmap *idmap, struct inode *dir,
549c7297 577 struct dentry *dentry, umode_t mode, bool excl)
5f2aa075
NJ
578{
579 struct super_block *sb = dir->i_sb;
580 struct inode *inode;
581 struct exfat_chain cdir;
582 struct exfat_dir_entry info;
583 loff_t i_pos;
584 int err;
585
586 mutex_lock(&EXFAT_SB(sb)->s_lock);
7018ec68 587 exfat_set_volume_dirty(sb);
5f2aa075
NJ
588 err = exfat_add_entry(dir, dentry->d_name.name, &cdir, TYPE_FILE,
589 &info);
5f2aa075
NJ
590 if (err)
591 goto unlock;
592
593 inode_inc_iversion(dir);
4c72a36e 594 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
5f2aa075
NJ
595 if (IS_DIRSYNC(dir))
596 exfat_sync_inode(dir);
597 else
598 mark_inode_dirty(dir);
599
600 i_pos = exfat_make_i_pos(&info);
601 inode = exfat_build_inode(sb, &info, i_pos);
d6c9efd9
TK
602 err = PTR_ERR_OR_ZERO(inode);
603 if (err)
5f2aa075
NJ
604 goto unlock;
605
606 inode_inc_iversion(inode);
4c72a36e
JL
607 EXFAT_I(inode)->i_crtime = simple_inode_init_ts(inode);
608 exfat_truncate_inode_atime(inode);
609
5f2aa075
NJ
610 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
611
612 d_instantiate(dentry, inode);
613unlock:
614 mutex_unlock(&EXFAT_SB(sb)->s_lock);
615 return err;
616}
617
618/* lookup a file */
619static int exfat_find(struct inode *dir, struct qstr *qname,
620 struct exfat_dir_entry *info)
621{
72880cb5 622 int ret, dentry, count;
5f2aa075
NJ
623 struct exfat_chain cdir;
624 struct exfat_uni_name uni_name;
5f2aa075
NJ
625 struct super_block *sb = dir->i_sb;
626 struct exfat_sb_info *sbi = EXFAT_SB(sb);
627 struct exfat_inode_info *ei = EXFAT_I(dir);
188df41f 628 struct exfat_dentry *ep, *ep2;
20914ff6 629 struct exfat_entry_set_cache es;
c6e2f52e
HK
630 /* for optimized dir & entry to prevent long traverse of cluster chain */
631 struct exfat_hint hint_opt;
5f2aa075
NJ
632
633 if (qname->len == 0)
634 return -ENOENT;
635
636 /* check the validity of directory name in the given pathname */
637 ret = exfat_resolve_path_for_lookup(dir, qname->name, &cdir, &uni_name);
638 if (ret)
639 return ret;
640
5f2aa075
NJ
641 /* check the validation of hint_stat and initialize it if required */
642 if (ei->version != (inode_peek_iversion_raw(dir) & 0xffffffff)) {
643 ei->hint_stat.clu = cdir.dir;
644 ei->hint_stat.eidx = 0;
645 ei->version = (inode_peek_iversion_raw(dir) & 0xffffffff);
646 ei->hint_femp.eidx = EXFAT_HINT_NONE;
647 }
648
649 /* search the file name for directories */
72880cb5 650 dentry = exfat_find_dir_entry(sb, ei, &cdir, &uni_name, &hint_opt);
188df41f 651 if (dentry < 0)
5f2aa075
NJ
652 return dentry; /* -error value */
653
a7a24168 654 info->dir = cdir;
5f2aa075
NJ
655 info->entry = dentry;
656 info->num_subdirs = 0;
657
c6e2f52e
HK
658 /* adjust cdir to the optimized value */
659 cdir.dir = hint_opt.clu;
660 if (cdir.flags & ALLOC_NO_FAT_CHAIN)
661 cdir.size -= dentry / sbi->dentries_per_clu;
662 dentry = hint_opt.eidx;
20914ff6 663 if (exfat_get_dentry_set(&es, sb, &cdir, dentry, ES_2_ENTRIES))
188df41f 664 return -EIO;
f3fe3954
YM
665 ep = exfat_get_dentry_cached(&es, ES_IDX_FILE);
666 ep2 = exfat_get_dentry_cached(&es, ES_IDX_STREAM);
188df41f
TK
667
668 info->type = exfat_get_entry_type(ep);
669 info->attr = le16_to_cpu(ep->dentry.file.attr);
670 info->size = le64_to_cpu(ep2->dentry.stream.valid_size);
11a347fb
YM
671 info->valid_size = le64_to_cpu(ep2->dentry.stream.valid_size);
672 info->size = le64_to_cpu(ep2->dentry.stream.size);
dab48b8f 673 if (info->size == 0) {
188df41f
TK
674 info->flags = ALLOC_NO_FAT_CHAIN;
675 info->start_clu = EXFAT_EOF_CLUSTER;
676 } else {
677 info->flags = ep2->dentry.stream.flags;
678 info->start_clu =
679 le32_to_cpu(ep2->dentry.stream.start_clu);
680 }
5f2aa075 681
188df41f
TK
682 exfat_get_entry_time(sbi, &info->crtime,
683 ep->dentry.file.create_tz,
684 ep->dentry.file.create_time,
685 ep->dentry.file.create_date,
686 ep->dentry.file.create_time_cs);
687 exfat_get_entry_time(sbi, &info->mtime,
688 ep->dentry.file.modify_tz,
689 ep->dentry.file.modify_time,
690 ep->dentry.file.modify_date,
691 ep->dentry.file.modify_time_cs);
692 exfat_get_entry_time(sbi, &info->atime,
693 ep->dentry.file.access_tz,
694 ep->dentry.file.access_time,
695 ep->dentry.file.access_date,
696 0);
3b9681ac 697 exfat_put_dentry_set(&es, false);
188df41f
TK
698
699 if (ei->start_clu == EXFAT_FREE_CLUSTER) {
700 exfat_fs_error(sb,
701 "non-zero size file starts with zero cluster (size : %llu, p_dir : %u, entry : 0x%08x)",
702 i_size_read(dir), ei->dir.dir, ei->entry);
703 return -EIO;
704 }
5f2aa075 705
188df41f
TK
706 if (info->type == TYPE_DIR) {
707 exfat_chain_set(&cdir, info->start_clu,
708 EXFAT_B_TO_CLU(info->size, sbi), info->flags);
5f2aa075
NJ
709 count = exfat_count_dir_entries(sb, &cdir);
710 if (count < 0)
711 return -EIO;
712
188df41f 713 info->num_subdirs = count + EXFAT_MIN_SUBDIR;
5f2aa075
NJ
714 }
715 return 0;
716}
717
718static int exfat_d_anon_disconn(struct dentry *dentry)
719{
720 return IS_ROOT(dentry) && (dentry->d_flags & DCACHE_DISCONNECTED);
721}
722
723static struct dentry *exfat_lookup(struct inode *dir, struct dentry *dentry,
724 unsigned int flags)
725{
726 struct super_block *sb = dir->i_sb;
727 struct inode *inode;
728 struct dentry *alias;
729 struct exfat_dir_entry info;
730 int err;
731 loff_t i_pos;
732 mode_t i_mode;
733
734 mutex_lock(&EXFAT_SB(sb)->s_lock);
735 err = exfat_find(dir, &dentry->d_name, &info);
736 if (err) {
737 if (err == -ENOENT) {
738 inode = NULL;
739 goto out;
740 }
741 goto unlock;
742 }
743
744 i_pos = exfat_make_i_pos(&info);
745 inode = exfat_build_inode(sb, &info, i_pos);
d6c9efd9
TK
746 err = PTR_ERR_OR_ZERO(inode);
747 if (err)
5f2aa075 748 goto unlock;
5f2aa075
NJ
749
750 i_mode = inode->i_mode;
751 alias = d_find_alias(inode);
752
753 /*
754 * Checking "alias->d_parent == dentry->d_parent" to make sure
755 * FS is not corrupted (especially double linked dir).
756 */
757 if (alias && alias->d_parent == dentry->d_parent &&
758 !exfat_d_anon_disconn(alias)) {
759
760 /*
761 * Unhashed alias is able to exist because of revalidate()
762 * called by lookup_fast. You can easily make this status
763 * by calling create and lookup concurrently
764 * In such case, we reuse an alias instead of new dentry
765 */
766 if (d_unhashed(alias)) {
767 WARN_ON(alias->d_name.hash_len !=
768 dentry->d_name.hash_len);
d1727d55
JP
769 exfat_info(sb, "rehashed a dentry(%p) in read lookup",
770 alias);
5f2aa075
NJ
771 d_drop(dentry);
772 d_rehash(alias);
773 } else if (!S_ISDIR(i_mode)) {
774 /*
775 * This inode has non anonymous-DCACHE_DISCONNECTED
776 * dentry. This means, the user did ->lookup() by an
777 * another name (longname vs 8.3 alias of it) in past.
778 *
779 * Switch to new one for reason of locality if possible.
780 */
781 d_move(alias, dentry);
782 }
783 iput(inode);
784 mutex_unlock(&EXFAT_SB(sb)->s_lock);
785 return alias;
786 }
787 dput(alias);
788out:
789 mutex_unlock(&EXFAT_SB(sb)->s_lock);
790 if (!inode)
791 exfat_d_version_set(dentry, inode_query_iversion(dir));
792
793 return d_splice_alias(inode, dentry);
794unlock:
795 mutex_unlock(&EXFAT_SB(sb)->s_lock);
796 return ERR_PTR(err);
797}
798
799/* remove an entry, BUT don't truncate */
800static int exfat_unlink(struct inode *dir, struct dentry *dentry)
801{
802 struct exfat_chain cdir;
5f2aa075
NJ
803 struct super_block *sb = dir->i_sb;
804 struct inode *inode = dentry->d_inode;
805 struct exfat_inode_info *ei = EXFAT_I(inode);
ff4343da
YM
806 struct exfat_entry_set_cache es;
807 int entry, err = 0;
5f2aa075
NJ
808
809 mutex_lock(&EXFAT_SB(sb)->s_lock);
810 exfat_chain_dup(&cdir, &ei->dir);
811 entry = ei->entry;
812 if (ei->dir.dir == DIR_DELETED) {
d1727d55 813 exfat_err(sb, "abnormal access to deleted dentry");
5f2aa075
NJ
814 err = -ENOENT;
815 goto unlock;
816 }
817
ff4343da
YM
818 err = exfat_get_dentry_set(&es, sb, &cdir, entry, ES_ALL_ENTRIES);
819 if (err) {
5f2aa075 820 err = -EIO;
5f2aa075
NJ
821 goto unlock;
822 }
5f2aa075 823
7018ec68 824 exfat_set_volume_dirty(sb);
ff4343da 825
5f2aa075 826 /* update the directory entry */
ff4343da
YM
827 exfat_remove_entries(inode, &es, ES_IDX_FILE);
828
829 err = exfat_put_dentry_set(&es, IS_DIRSYNC(inode));
830 if (err)
5f2aa075 831 goto unlock;
5f2aa075
NJ
832
833 /* This doesn't modify ei */
834 ei->dir.dir = DIR_DELETED;
5f2aa075
NJ
835
836 inode_inc_iversion(dir);
4c72a36e
JL
837 simple_inode_init_ts(dir);
838 exfat_truncate_inode_atime(dir);
5f2aa075
NJ
839 if (IS_DIRSYNC(dir))
840 exfat_sync_inode(dir);
841 else
842 mark_inode_dirty(dir);
843
844 clear_nlink(inode);
4c72a36e
JL
845 simple_inode_init_ts(inode);
846 exfat_truncate_inode_atime(inode);
5f2aa075
NJ
847 exfat_unhash_inode(inode);
848 exfat_d_version_set(dentry, inode_query_iversion(dir));
849unlock:
850 mutex_unlock(&EXFAT_SB(sb)->s_lock);
851 return err;
852}
853
c54bd91e 854static int exfat_mkdir(struct mnt_idmap *idmap, struct inode *dir,
549c7297 855 struct dentry *dentry, umode_t mode)
5f2aa075
NJ
856{
857 struct super_block *sb = dir->i_sb;
858 struct inode *inode;
859 struct exfat_dir_entry info;
860 struct exfat_chain cdir;
861 loff_t i_pos;
862 int err;
863
864 mutex_lock(&EXFAT_SB(sb)->s_lock);
7018ec68 865 exfat_set_volume_dirty(sb);
5f2aa075
NJ
866 err = exfat_add_entry(dir, dentry->d_name.name, &cdir, TYPE_DIR,
867 &info);
5f2aa075
NJ
868 if (err)
869 goto unlock;
870
871 inode_inc_iversion(dir);
4c72a36e 872 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
5f2aa075
NJ
873 if (IS_DIRSYNC(dir))
874 exfat_sync_inode(dir);
875 else
876 mark_inode_dirty(dir);
877 inc_nlink(dir);
878
879 i_pos = exfat_make_i_pos(&info);
880 inode = exfat_build_inode(sb, &info, i_pos);
d6c9efd9
TK
881 err = PTR_ERR_OR_ZERO(inode);
882 if (err)
5f2aa075 883 goto unlock;
5f2aa075
NJ
884
885 inode_inc_iversion(inode);
4c72a36e
JL
886 EXFAT_I(inode)->i_crtime = simple_inode_init_ts(inode);
887 exfat_truncate_inode_atime(inode);
5f2aa075
NJ
888 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
889
890 d_instantiate(dentry, inode);
891
892unlock:
893 mutex_unlock(&EXFAT_SB(sb)->s_lock);
894 return err;
895}
896
897static int exfat_check_dir_empty(struct super_block *sb,
898 struct exfat_chain *p_dir)
899{
900 int i, dentries_per_clu;
901 unsigned int type;
902 struct exfat_chain clu;
903 struct exfat_dentry *ep;
904 struct exfat_sb_info *sbi = EXFAT_SB(sb);
905 struct buffer_head *bh;
906
907 dentries_per_clu = sbi->dentries_per_clu;
908
dab48b8f
YM
909 if (p_dir->dir == EXFAT_EOF_CLUSTER)
910 return 0;
911
5f2aa075
NJ
912 exfat_chain_dup(&clu, p_dir);
913
914 while (clu.dir != EXFAT_EOF_CLUSTER) {
915 for (i = 0; i < dentries_per_clu; i++) {
c71510b3 916 ep = exfat_get_dentry(sb, &clu, i, &bh);
5f2aa075
NJ
917 if (!ep)
918 return -EIO;
919 type = exfat_get_entry_type(ep);
920 brelse(bh);
921 if (type == TYPE_UNUSED)
922 return 0;
923
924 if (type != TYPE_FILE && type != TYPE_DIR)
925 continue;
926
927 return -ENOTEMPTY;
928 }
929
930 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
931 if (--clu.size > 0)
932 clu.dir++;
933 else
934 clu.dir = EXFAT_EOF_CLUSTER;
935 } else {
936 if (exfat_get_next_cluster(sb, &(clu.dir)))
937 return -EIO;
938 }
939 }
940
941 return 0;
942}
943
944static int exfat_rmdir(struct inode *dir, struct dentry *dentry)
945{
946 struct inode *inode = dentry->d_inode;
5f2aa075
NJ
947 struct exfat_chain cdir, clu_to_free;
948 struct super_block *sb = inode->i_sb;
949 struct exfat_sb_info *sbi = EXFAT_SB(sb);
950 struct exfat_inode_info *ei = EXFAT_I(inode);
ff4343da
YM
951 struct exfat_entry_set_cache es;
952 int entry, err;
5f2aa075
NJ
953
954 mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
955
956 exfat_chain_dup(&cdir, &ei->dir);
957 entry = ei->entry;
958
959 if (ei->dir.dir == DIR_DELETED) {
d1727d55 960 exfat_err(sb, "abnormal access to deleted dentry");
5f2aa075
NJ
961 err = -ENOENT;
962 goto unlock;
963 }
964
5f2aa075
NJ
965 exfat_chain_set(&clu_to_free, ei->start_clu,
966 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi), ei->flags);
967
968 err = exfat_check_dir_empty(sb, &clu_to_free);
969 if (err) {
970 if (err == -EIO)
d1727d55
JP
971 exfat_err(sb, "failed to exfat_check_dir_empty : err(%d)",
972 err);
5f2aa075
NJ
973 goto unlock;
974 }
975
ff4343da
YM
976 err = exfat_get_dentry_set(&es, sb, &cdir, entry, ES_ALL_ENTRIES);
977 if (err) {
5f2aa075 978 err = -EIO;
5f2aa075
NJ
979 goto unlock;
980 }
5f2aa075 981
7018ec68 982 exfat_set_volume_dirty(sb);
ff4343da
YM
983
984 exfat_remove_entries(inode, &es, ES_IDX_FILE);
985
986 err = exfat_put_dentry_set(&es, IS_DIRSYNC(dir));
987 if (err)
5f2aa075 988 goto unlock;
ff4343da 989
5f2aa075 990 ei->dir.dir = DIR_DELETED;
5f2aa075
NJ
991
992 inode_inc_iversion(dir);
4c72a36e
JL
993 simple_inode_init_ts(dir);
994 exfat_truncate_inode_atime(dir);
5f2aa075
NJ
995 if (IS_DIRSYNC(dir))
996 exfat_sync_inode(dir);
997 else
998 mark_inode_dirty(dir);
999 drop_nlink(dir);
1000
1001 clear_nlink(inode);
4c72a36e
JL
1002 simple_inode_init_ts(inode);
1003 exfat_truncate_inode_atime(inode);
5f2aa075
NJ
1004 exfat_unhash_inode(inode);
1005 exfat_d_version_set(dentry, inode_query_iversion(dir));
1006unlock:
1007 mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
1008 return err;
1009}
1010
1011static int exfat_rename_file(struct inode *inode, struct exfat_chain *p_dir,
1012 int oldentry, struct exfat_uni_name *p_uniname,
1013 struct exfat_inode_info *ei)
1014{
ff4343da 1015 int ret, num_new_entries;
5f2aa075
NJ
1016 struct exfat_dentry *epold, *epnew;
1017 struct super_block *sb = inode->i_sb;
d97e0606 1018 struct exfat_entry_set_cache old_es, new_es;
5f2aa075
NJ
1019 int sync = IS_DIRSYNC(inode);
1020
5f2aa075
NJ
1021 num_new_entries = exfat_calc_num_entries(p_uniname);
1022 if (num_new_entries < 0)
1023 return num_new_entries;
1024
ff4343da
YM
1025 ret = exfat_get_dentry_set(&old_es, sb, p_dir, oldentry, ES_ALL_ENTRIES);
1026 if (ret) {
1027 ret = -EIO;
1028 return ret;
1029 }
1030
1031 epold = exfat_get_dentry_cached(&old_es, ES_IDX_FILE);
1032
1033 if (old_es.num_entries < num_new_entries) {
5f2aa075
NJ
1034 int newentry;
1035
1036 newentry =
1037 exfat_find_empty_entry(inode, p_dir, num_new_entries);
ff4343da
YM
1038 if (newentry < 0) {
1039 ret = newentry; /* -EIO or -ENOSPC */
1040 goto put_old_es;
1041 }
5f2aa075 1042
d97e0606
YM
1043 ret = exfat_get_empty_dentry_set(&new_es, sb, p_dir, newentry,
1044 num_new_entries);
1045 if (ret)
ff4343da 1046 goto put_old_es;
5f2aa075 1047
d97e0606 1048 epnew = exfat_get_dentry_cached(&new_es, ES_IDX_FILE);
a7a24168 1049 *epnew = *epold;
5f2aa075 1050 if (exfat_get_entry_type(epnew) == TYPE_FILE) {
0ab8ba71
JC
1051 epnew->dentry.file.attr |= cpu_to_le16(EXFAT_ATTR_ARCHIVE);
1052 ei->attr |= EXFAT_ATTR_ARCHIVE;
5f2aa075 1053 }
5f2aa075 1054
ff4343da 1055 epold = exfat_get_dentry_cached(&old_es, ES_IDX_STREAM);
d97e0606 1056 epnew = exfat_get_dentry_cached(&new_es, ES_IDX_STREAM);
a7a24168 1057 *epnew = *epold;
5f2aa075 1058
d97e0606
YM
1059 exfat_init_ext_entry(&new_es, num_new_entries, p_uniname);
1060
1061 ret = exfat_put_dentry_set(&new_es, sync);
5f2aa075 1062 if (ret)
ff4343da 1063 goto put_old_es;
5f2aa075 1064
ff4343da 1065 exfat_remove_entries(inode, &old_es, ES_IDX_FILE);
d8dad258 1066 ei->dir = *p_dir;
5f2aa075
NJ
1067 ei->entry = newentry;
1068 } else {
1069 if (exfat_get_entry_type(epold) == TYPE_FILE) {
0ab8ba71
JC
1070 epold->dentry.file.attr |= cpu_to_le16(EXFAT_ATTR_ARCHIVE);
1071 ei->attr |= EXFAT_ATTR_ARCHIVE;
5f2aa075 1072 }
4e1aa22f
YM
1073
1074 exfat_remove_entries(inode, &old_es, ES_IDX_FIRST_FILENAME + 1);
d97e0606 1075 exfat_init_ext_entry(&old_es, num_new_entries, p_uniname);
5f2aa075 1076 }
ff4343da
YM
1077 return exfat_put_dentry_set(&old_es, sync);
1078
1079put_old_es:
1080 exfat_put_dentry_set(&old_es, false);
1081 return ret;
5f2aa075
NJ
1082}
1083
1084static int exfat_move_file(struct inode *inode, struct exfat_chain *p_olddir,
1085 int oldentry, struct exfat_chain *p_newdir,
1086 struct exfat_uni_name *p_uniname, struct exfat_inode_info *ei)
1087{
ff4343da 1088 int ret, newentry, num_new_entries;
5f2aa075
NJ
1089 struct exfat_dentry *epmov, *epnew;
1090 struct super_block *sb = inode->i_sb;
d97e0606 1091 struct exfat_entry_set_cache mov_es, new_es;
5f2aa075
NJ
1092
1093 num_new_entries = exfat_calc_num_entries(p_uniname);
1094 if (num_new_entries < 0)
1095 return num_new_entries;
1096
1097 newentry = exfat_find_empty_entry(inode, p_newdir, num_new_entries);
1098 if (newentry < 0)
1099 return newentry; /* -EIO or -ENOSPC */
1100
ff4343da
YM
1101 ret = exfat_get_dentry_set(&mov_es, sb, p_olddir, oldentry,
1102 ES_ALL_ENTRIES);
1103 if (ret)
5f2aa075
NJ
1104 return -EIO;
1105
d97e0606
YM
1106 ret = exfat_get_empty_dentry_set(&new_es, sb, p_newdir, newentry,
1107 num_new_entries);
1108 if (ret)
ff4343da 1109 goto put_mov_es;
ff4343da 1110
d97e0606
YM
1111 epmov = exfat_get_dentry_cached(&mov_es, ES_IDX_FILE);
1112 epnew = exfat_get_dentry_cached(&new_es, ES_IDX_FILE);
a7a24168 1113 *epnew = *epmov;
5f2aa075 1114 if (exfat_get_entry_type(epnew) == TYPE_FILE) {
0ab8ba71
JC
1115 epnew->dentry.file.attr |= cpu_to_le16(EXFAT_ATTR_ARCHIVE);
1116 ei->attr |= EXFAT_ATTR_ARCHIVE;
5f2aa075 1117 }
5f2aa075 1118
ff4343da 1119 epmov = exfat_get_dentry_cached(&mov_es, ES_IDX_STREAM);
d97e0606 1120 epnew = exfat_get_dentry_cached(&new_es, ES_IDX_STREAM);
a7a24168 1121 *epnew = *epmov;
5f2aa075 1122
d97e0606 1123 exfat_init_ext_entry(&new_es, num_new_entries, p_uniname);
ff4343da 1124 exfat_remove_entries(inode, &mov_es, ES_IDX_FILE);
5f2aa075
NJ
1125
1126 exfat_chain_set(&ei->dir, p_newdir->dir, p_newdir->size,
1127 p_newdir->flags);
1128
1129 ei->entry = newentry;
d97e0606
YM
1130
1131 ret = exfat_put_dentry_set(&new_es, IS_DIRSYNC(inode));
1132 if (ret)
1133 goto put_mov_es;
1134
ff4343da
YM
1135 return exfat_put_dentry_set(&mov_es, IS_DIRSYNC(inode));
1136
1137put_mov_es:
1138 exfat_put_dentry_set(&mov_es, false);
1139
1140 return ret;
5f2aa075
NJ
1141}
1142
5f2aa075
NJ
1143/* rename or move a old file into a new file */
1144static int __exfat_rename(struct inode *old_parent_inode,
1145 struct exfat_inode_info *ei, struct inode *new_parent_inode,
1146 struct dentry *new_dentry)
1147{
1148 int ret;
1149 int dentry;
1150 struct exfat_chain olddir, newdir;
1151 struct exfat_chain *p_dir = NULL;
1152 struct exfat_uni_name uni_name;
1153 struct exfat_dentry *ep;
1154 struct super_block *sb = old_parent_inode->i_sb;
1155 struct exfat_sb_info *sbi = EXFAT_SB(sb);
1156 const unsigned char *new_path = new_dentry->d_name.name;
1157 struct inode *new_inode = new_dentry->d_inode;
5f2aa075
NJ
1158 struct exfat_inode_info *new_ei = NULL;
1159 unsigned int new_entry_type = TYPE_UNUSED;
1160 int new_entry = 0;
015c0d4f 1161 struct buffer_head *new_bh = NULL;
5f2aa075
NJ
1162
1163 /* check the validity of pointer parameters */
1164 if (new_path == NULL || strlen(new_path) == 0)
1165 return -EINVAL;
1166
1167 if (ei->dir.dir == DIR_DELETED) {
d1727d55 1168 exfat_err(sb, "abnormal access to deleted source dentry");
5f2aa075
NJ
1169 return -ENOENT;
1170 }
1171
204e6cea
SS
1172 exfat_chain_set(&olddir, EXFAT_I(old_parent_inode)->start_clu,
1173 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(old_parent_inode), sbi),
1174 EXFAT_I(old_parent_inode)->flags);
5f2aa075
NJ
1175 dentry = ei->entry;
1176
5f2aa075
NJ
1177 /* check whether new dir is existing directory and empty */
1178 if (new_inode) {
1179 ret = -EIO;
1180 new_ei = EXFAT_I(new_inode);
1181
1182 if (new_ei->dir.dir == DIR_DELETED) {
d1727d55 1183 exfat_err(sb, "abnormal access to deleted target dentry");
5f2aa075
NJ
1184 goto out;
1185 }
1186
5f2aa075
NJ
1187 p_dir = &(new_ei->dir);
1188 new_entry = new_ei->entry;
c71510b3 1189 ep = exfat_get_dentry(sb, p_dir, new_entry, &new_bh);
5f2aa075
NJ
1190 if (!ep)
1191 goto out;
1192
1193 new_entry_type = exfat_get_entry_type(ep);
1194 brelse(new_bh);
1195
1196 /* if new_inode exists, update ei */
1197 if (new_entry_type == TYPE_DIR) {
1198 struct exfat_chain new_clu;
1199
1200 new_clu.dir = new_ei->start_clu;
1201 new_clu.size =
1202 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode),
1203 sbi);
1204 new_clu.flags = new_ei->flags;
1205
1206 ret = exfat_check_dir_empty(sb, &new_clu);
1207 if (ret)
1208 goto out;
1209 }
1210 }
1211
1212 /* check the validity of directory name in the given new pathname */
1213 ret = exfat_resolve_path(new_parent_inode, new_path, &newdir,
1214 &uni_name);
1215 if (ret)
1216 goto out;
1217
7018ec68 1218 exfat_set_volume_dirty(sb);
5f2aa075
NJ
1219
1220 if (olddir.dir == newdir.dir)
1221 ret = exfat_rename_file(new_parent_inode, &olddir, dentry,
1222 &uni_name, ei);
1223 else
1224 ret = exfat_move_file(new_parent_inode, &olddir, dentry,
1225 &newdir, &uni_name, ei);
1226
1227 if (!ret && new_inode) {
ff4343da
YM
1228 struct exfat_entry_set_cache es;
1229
5f2aa075 1230 /* delete entries of new_dir */
ff4343da
YM
1231 ret = exfat_get_dentry_set(&es, sb, p_dir, new_entry,
1232 ES_ALL_ENTRIES);
1233 if (ret) {
5f2aa075
NJ
1234 ret = -EIO;
1235 goto del_out;
1236 }
1237
ff4343da 1238 exfat_remove_entries(new_inode, &es, ES_IDX_FILE);
5f2aa075 1239
ff4343da
YM
1240 ret = exfat_put_dentry_set(&es, IS_DIRSYNC(new_inode));
1241 if (ret)
5f2aa075 1242 goto del_out;
5f2aa075
NJ
1243
1244 /* Free the clusters if new_inode is a dir(as if exfat_rmdir) */
dab48b8f
YM
1245 if (new_entry_type == TYPE_DIR &&
1246 new_ei->start_clu != EXFAT_EOF_CLUSTER) {
5f2aa075
NJ
1247 /* new_ei, new_clu_to_free */
1248 struct exfat_chain new_clu_to_free;
1249
1250 exfat_chain_set(&new_clu_to_free, new_ei->start_clu,
1251 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode),
1252 sbi), new_ei->flags);
1253
1254 if (exfat_free_cluster(new_inode, &new_clu_to_free)) {
1255 /* just set I/O error only */
1256 ret = -EIO;
1257 }
1258
1259 i_size_write(new_inode, 0);
11a347fb 1260 new_ei->valid_size = 0;
5f2aa075
NJ
1261 new_ei->start_clu = EXFAT_EOF_CLUSTER;
1262 new_ei->flags = ALLOC_NO_FAT_CHAIN;
1263 }
1264del_out:
1265 /* Update new_inode ei
1266 * Prevent syncing removed new_inode
1267 * (new_ei is already initialized above code ("if (new_inode)")
1268 */
1269 new_ei->dir.dir = DIR_DELETED;
1270 }
5f2aa075
NJ
1271out:
1272 return ret;
1273}
1274
e18275ae 1275static int exfat_rename(struct mnt_idmap *idmap,
549c7297
CB
1276 struct inode *old_dir, struct dentry *old_dentry,
1277 struct inode *new_dir, struct dentry *new_dentry,
1278 unsigned int flags)
5f2aa075
NJ
1279{
1280 struct inode *old_inode, *new_inode;
1281 struct super_block *sb = old_dir->i_sb;
1282 loff_t i_pos;
1283 int err;
1284
1285 /*
1286 * The VFS already checks for existence, so for local filesystems
1287 * the RENAME_NOREPLACE implementation is equivalent to plain rename.
1288 * Don't support any other flags
1289 */
1290 if (flags & ~RENAME_NOREPLACE)
1291 return -EINVAL;
1292
1293 mutex_lock(&EXFAT_SB(sb)->s_lock);
1294 old_inode = old_dentry->d_inode;
1295 new_inode = new_dentry->d_inode;
1296
1297 err = __exfat_rename(old_dir, EXFAT_I(old_inode), new_dir, new_dentry);
1298 if (err)
1299 goto unlock;
1300
1301 inode_inc_iversion(new_dir);
d06cdfdd
JL
1302 simple_rename_timestamp(old_dir, old_dentry, new_dir, new_dentry);
1303 EXFAT_I(new_dir)->i_crtime = current_time(new_dir);
4c72a36e 1304 exfat_truncate_inode_atime(new_dir);
5f2aa075
NJ
1305 if (IS_DIRSYNC(new_dir))
1306 exfat_sync_inode(new_dir);
1307 else
1308 mark_inode_dirty(new_dir);
1309
1310 i_pos = ((loff_t)EXFAT_I(old_inode)->dir.dir << 32) |
1311 (EXFAT_I(old_inode)->entry & 0xffffffff);
1312 exfat_unhash_inode(old_inode);
1313 exfat_hash_inode(old_inode, i_pos);
1314 if (IS_DIRSYNC(new_dir))
1315 exfat_sync_inode(old_inode);
1316 else
1317 mark_inode_dirty(old_inode);
1318
1319 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
1320 drop_nlink(old_dir);
1321 if (!new_inode)
1322 inc_nlink(new_dir);
1323 }
1324
1325 inode_inc_iversion(old_dir);
5f2aa075
NJ
1326 if (IS_DIRSYNC(old_dir))
1327 exfat_sync_inode(old_dir);
1328 else
1329 mark_inode_dirty(old_dir);
1330
1331 if (new_inode) {
1332 exfat_unhash_inode(new_inode);
1333
1334 /* skip drop_nlink if new_inode already has been dropped */
1335 if (new_inode->i_nlink) {
1336 drop_nlink(new_inode);
1337 if (S_ISDIR(new_inode->i_mode))
1338 drop_nlink(new_inode);
1339 } else {
d1727d55 1340 exfat_warn(sb, "abnormal access to an inode dropped");
5f2aa075
NJ
1341 WARN_ON(new_inode->i_nlink == 0);
1342 }
f29f1908 1343 EXFAT_I(new_inode)->i_crtime = current_time(new_inode);
5f2aa075
NJ
1344 }
1345
1346unlock:
1347 mutex_unlock(&EXFAT_SB(sb)->s_lock);
1348 return err;
1349}
1350
1351const struct inode_operations exfat_dir_inode_operations = {
1352 .create = exfat_create,
1353 .lookup = exfat_lookup,
1354 .unlink = exfat_unlink,
1355 .mkdir = exfat_mkdir,
1356 .rmdir = exfat_rmdir,
1357 .rename = exfat_rename,
1358 .setattr = exfat_setattr,
1359 .getattr = exfat_getattr,
1360};