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