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
exfat_d_version(struct dentry * dentry)15 static inline unsigned long exfat_d_version(struct dentry *dentry)
16 {
17 return (unsigned long) dentry->d_fsdata;
18 }
19
exfat_d_version_set(struct dentry * dentry,unsigned long version)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 */
exfat_d_revalidate(struct dentry * dentry,unsigned int flags)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 if necessary */
exfat_striptail_len(unsigned int len,const char * name,bool keep_last_dots)69 static unsigned int exfat_striptail_len(unsigned int len, const char *name,
70 bool keep_last_dots)
71 {
72 if (!keep_last_dots) {
73 while (len && name[len - 1] == '.')
74 len--;
75 }
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 */
exfat_d_hash(const struct dentry * dentry,struct qstr * qstr)84 static 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;
89 unsigned int len = exfat_striptail_len(qstr->len, qstr->name,
90 EXFAT_SB(sb)->options.keep_last_dots);
91 unsigned long hash = init_name_hash(dentry);
92 int i, charlen;
93 wchar_t c;
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
exfat_d_cmp(const struct dentry * dentry,unsigned int len,const char * str,const struct qstr * name)106 static 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;
111 unsigned int alen = exfat_striptail_len(name->len, name->name,
112 EXFAT_SB(sb)->options.keep_last_dots);
113 unsigned int blen = exfat_striptail_len(len, str,
114 EXFAT_SB(sb)->options.keep_last_dots);
115 wchar_t c1, c2;
116 int charlen, i;
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
135 const struct dentry_operations exfat_dentry_ops = {
136 .d_revalidate = exfat_d_revalidate,
137 .d_hash = exfat_d_hash,
138 .d_compare = exfat_d_cmp,
139 };
140
exfat_utf8_d_hash(const struct dentry * dentry,struct qstr * qstr)141 static 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;
145 unsigned int len = exfat_striptail_len(qstr->len, qstr->name,
146 EXFAT_SB(sb)->options.keep_last_dots);
147 unsigned long hash = init_name_hash(dentry);
148 int i, charlen;
149 unicode_t u;
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 /*
157 * exfat_toupper() works only for code points up to the U+FFFF.
158 */
159 hash = partial_name_hash(u <= 0xFFFF ? exfat_toupper(sb, u) : u,
160 hash);
161 }
162
163 qstr->hash = end_name_hash(hash);
164 return 0;
165 }
166
exfat_utf8_d_cmp(const struct dentry * dentry,unsigned int len,const char * str,const struct qstr * name)167 static 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;
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
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;
192 } else {
193 if (u_a != u_b)
194 return 1;
195 }
196 }
197
198 return 0;
199 }
200
201 const struct dentry_operations exfat_utf8_dentry_ops = {
202 .d_revalidate = exfat_d_revalidate,
203 .d_hash = exfat_utf8_d_hash,
204 .d_compare = exfat_utf8_d_cmp,
205 };
206
207 /* search EMPTY CONTINUOUS "num_entries" entries */
exfat_search_empty_slot(struct super_block * sb,struct exfat_hint_femp * hint_femp,struct exfat_chain * p_dir,int num_entries,struct exfat_entry_set_cache * es)208 static int exfat_search_empty_slot(struct super_block *sb,
209 struct exfat_hint_femp *hint_femp, struct exfat_chain *p_dir,
210 int num_entries, struct exfat_entry_set_cache *es)
211 {
212 int i, dentry, ret;
213 int dentries_per_clu;
214 struct exfat_chain clu;
215 struct exfat_sb_info *sbi = EXFAT_SB(sb);
216 int total_entries = EXFAT_CLU_TO_DEN(p_dir->size, sbi);
217
218 dentries_per_clu = sbi->dentries_per_clu;
219
220 if (hint_femp->eidx != EXFAT_HINT_NONE) {
221 dentry = hint_femp->eidx;
222
223 /*
224 * If hint_femp->count is enough, it is needed to check if
225 * there are actual empty entries.
226 * Otherwise, and if "dentry + hint_famp->count" is also equal
227 * to "p_dir->size * dentries_per_clu", it means ENOSPC.
228 */
229 if (dentry + hint_femp->count == total_entries &&
230 num_entries > hint_femp->count)
231 return -ENOSPC;
232
233 hint_femp->eidx = EXFAT_HINT_NONE;
234 exfat_chain_dup(&clu, &hint_femp->cur);
235 } else {
236 exfat_chain_dup(&clu, p_dir);
237 dentry = 0;
238 }
239
240 while (dentry + num_entries <= total_entries &&
241 clu.dir != EXFAT_EOF_CLUSTER) {
242 i = dentry & (dentries_per_clu - 1);
243
244 ret = exfat_get_empty_dentry_set(es, sb, &clu, i, num_entries);
245 if (ret < 0)
246 return ret;
247 else if (ret == 0)
248 return dentry;
249
250 dentry += ret;
251 i += ret;
252
253 while (i >= dentries_per_clu) {
254 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
255 if (--clu.size > 0)
256 clu.dir++;
257 else
258 clu.dir = EXFAT_EOF_CLUSTER;
259 } else {
260 if (exfat_get_next_cluster(sb, &clu.dir))
261 return -EIO;
262 }
263
264 i -= dentries_per_clu;
265 }
266 }
267
268 hint_femp->eidx = dentry;
269 hint_femp->count = 0;
270 if (dentry == total_entries || clu.dir == EXFAT_EOF_CLUSTER)
271 exfat_chain_set(&hint_femp->cur, EXFAT_EOF_CLUSTER, 0,
272 clu.flags);
273 else
274 hint_femp->cur = clu;
275
276 return -ENOSPC;
277 }
278
exfat_check_max_dentries(struct inode * inode)279 static int exfat_check_max_dentries(struct inode *inode)
280 {
281 if (EXFAT_B_TO_DEN(i_size_read(inode)) >= MAX_EXFAT_DENTRIES) {
282 /*
283 * exFAT spec allows a dir to grow up to 8388608(256MB)
284 * dentries
285 */
286 return -ENOSPC;
287 }
288 return 0;
289 }
290
291 /* find empty directory entry.
292 * if there isn't any empty slot, expand cluster chain.
293 */
exfat_find_empty_entry(struct inode * inode,struct exfat_chain * p_dir,int num_entries,struct exfat_entry_set_cache * es)294 static int exfat_find_empty_entry(struct inode *inode,
295 struct exfat_chain *p_dir, int num_entries,
296 struct exfat_entry_set_cache *es)
297 {
298 int dentry;
299 unsigned int ret, last_clu;
300 loff_t size = 0;
301 struct exfat_chain clu;
302 struct super_block *sb = inode->i_sb;
303 struct exfat_sb_info *sbi = EXFAT_SB(sb);
304 struct exfat_inode_info *ei = EXFAT_I(inode);
305 struct exfat_hint_femp hint_femp;
306
307 hint_femp.eidx = EXFAT_HINT_NONE;
308
309 if (ei->hint_femp.eidx != EXFAT_HINT_NONE) {
310 hint_femp = ei->hint_femp;
311 ei->hint_femp.eidx = EXFAT_HINT_NONE;
312 }
313
314 while ((dentry = exfat_search_empty_slot(sb, &hint_femp, p_dir,
315 num_entries, es)) < 0) {
316 if (dentry == -EIO)
317 break;
318
319 if (exfat_check_max_dentries(inode))
320 return -ENOSPC;
321
322 /*
323 * Allocate new cluster to this directory
324 */
325 if (ei->start_clu != EXFAT_EOF_CLUSTER) {
326 /* we trust p_dir->size regardless of FAT type */
327 if (exfat_find_last_cluster(sb, p_dir, &last_clu))
328 return -EIO;
329
330 exfat_chain_set(&clu, last_clu + 1, 0, p_dir->flags);
331 } else {
332 /* This directory is empty */
333 exfat_chain_set(&clu, EXFAT_EOF_CLUSTER, 0,
334 ALLOC_NO_FAT_CHAIN);
335 }
336
337 /* allocate a cluster */
338 ret = exfat_alloc_cluster(inode, 1, &clu, IS_DIRSYNC(inode));
339 if (ret)
340 return ret;
341
342 if (exfat_zeroed_cluster(inode, clu.dir))
343 return -EIO;
344
345 if (ei->start_clu == EXFAT_EOF_CLUSTER) {
346 ei->start_clu = clu.dir;
347 p_dir->dir = clu.dir;
348 hint_femp.eidx = 0;
349 }
350
351 /* append to the FAT chain */
352 if (clu.flags != p_dir->flags) {
353 /* no-fat-chain bit is disabled,
354 * so fat-chain should be synced with alloc-bitmap
355 */
356 exfat_chain_cont_cluster(sb, p_dir->dir, p_dir->size);
357 p_dir->flags = ALLOC_FAT_CHAIN;
358 hint_femp.cur.flags = ALLOC_FAT_CHAIN;
359 }
360
361 if (clu.flags == ALLOC_FAT_CHAIN)
362 if (exfat_ent_set(sb, last_clu, clu.dir))
363 return -EIO;
364
365 if (hint_femp.cur.dir == EXFAT_EOF_CLUSTER)
366 exfat_chain_set(&hint_femp.cur, clu.dir, 0, clu.flags);
367
368 hint_femp.count += sbi->dentries_per_clu;
369
370 hint_femp.cur.size++;
371 p_dir->size++;
372 size = EXFAT_CLU_TO_B(p_dir->size, sbi);
373
374 /* directory inode should be updated in here */
375 i_size_write(inode, size);
376 ei->valid_size += sbi->cluster_size;
377 ei->flags = p_dir->flags;
378 inode->i_blocks += sbi->cluster_size >> 9;
379 }
380
381 return dentry;
382 }
383
384 /*
385 * Name Resolution Functions :
386 * Zero if it was successful; otherwise nonzero.
387 */
__exfat_resolve_path(struct inode * inode,const unsigned char * path,struct exfat_chain * p_dir,struct exfat_uni_name * p_uniname,int lookup)388 static int __exfat_resolve_path(struct inode *inode, const unsigned char *path,
389 struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
390 int lookup)
391 {
392 int namelen;
393 int lossy = NLS_NAME_NO_LOSSY;
394 struct super_block *sb = inode->i_sb;
395 struct exfat_sb_info *sbi = EXFAT_SB(sb);
396 struct exfat_inode_info *ei = EXFAT_I(inode);
397 int pathlen = strlen(path);
398
399 /*
400 * get the length of the pathname excluding
401 * trailing periods, if any.
402 */
403 namelen = exfat_striptail_len(pathlen, path, false);
404 if (EXFAT_SB(sb)->options.keep_last_dots) {
405 /*
406 * Do not allow the creation of files with names
407 * ending with period(s).
408 */
409 if (!lookup && (namelen < pathlen))
410 return -EINVAL;
411 namelen = pathlen;
412 }
413 if (!namelen)
414 return -ENOENT;
415 if (pathlen > (MAX_NAME_LENGTH * MAX_CHARSET_SIZE))
416 return -ENAMETOOLONG;
417
418 /*
419 * strip all leading spaces :
420 * "MS windows 7" supports leading spaces.
421 * So we should skip this preprocessing for compatibility.
422 */
423
424 /* file name conversion :
425 * If lookup case, we allow bad-name for compatibility.
426 */
427 namelen = exfat_nls_to_utf16(sb, path, namelen, p_uniname,
428 &lossy);
429 if (namelen < 0)
430 return namelen; /* return error value */
431
432 if ((lossy && !lookup) || !namelen)
433 return (lossy & NLS_NAME_OVERLEN) ? -ENAMETOOLONG : -EINVAL;
434
435 exfat_chain_set(p_dir, ei->start_clu,
436 EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags);
437
438 return 0;
439 }
440
exfat_resolve_path(struct inode * inode,const unsigned char * path,struct exfat_chain * dir,struct exfat_uni_name * uni)441 static inline int exfat_resolve_path(struct inode *inode,
442 const unsigned char *path, struct exfat_chain *dir,
443 struct exfat_uni_name *uni)
444 {
445 return __exfat_resolve_path(inode, path, dir, uni, 0);
446 }
447
exfat_resolve_path_for_lookup(struct inode * inode,const unsigned char * path,struct exfat_chain * dir,struct exfat_uni_name * uni)448 static inline int exfat_resolve_path_for_lookup(struct inode *inode,
449 const unsigned char *path, struct exfat_chain *dir,
450 struct exfat_uni_name *uni)
451 {
452 return __exfat_resolve_path(inode, path, dir, uni, 1);
453 }
454
exfat_make_i_pos(struct exfat_dir_entry * info)455 static inline loff_t exfat_make_i_pos(struct exfat_dir_entry *info)
456 {
457 return ((loff_t) info->dir.dir << 32) | (info->entry & 0xffffffff);
458 }
459
exfat_add_entry(struct inode * inode,const char * path,struct exfat_chain * p_dir,unsigned int type,struct exfat_dir_entry * info)460 static int exfat_add_entry(struct inode *inode, const char *path,
461 struct exfat_chain *p_dir, unsigned int type,
462 struct exfat_dir_entry *info)
463 {
464 int ret, dentry, num_entries;
465 struct super_block *sb = inode->i_sb;
466 struct exfat_sb_info *sbi = EXFAT_SB(sb);
467 struct exfat_uni_name uniname;
468 struct exfat_chain clu;
469 struct timespec64 ts = current_time(inode);
470 struct exfat_entry_set_cache es;
471 int clu_size = 0;
472 unsigned int start_clu = EXFAT_FREE_CLUSTER;
473
474 ret = exfat_resolve_path(inode, path, p_dir, &uniname);
475 if (ret)
476 goto out;
477
478 num_entries = exfat_calc_num_entries(&uniname);
479 if (num_entries < 0) {
480 ret = num_entries;
481 goto out;
482 }
483
484 /* exfat_find_empty_entry must be called before alloc_cluster() */
485 dentry = exfat_find_empty_entry(inode, p_dir, num_entries, &es);
486 if (dentry < 0) {
487 ret = dentry; /* -EIO or -ENOSPC */
488 goto out;
489 }
490
491 if (type == TYPE_DIR && !sbi->options.zero_size_dir) {
492 ret = exfat_alloc_new_dir(inode, &clu);
493 if (ret) {
494 exfat_put_dentry_set(&es, false);
495 goto out;
496 }
497 start_clu = clu.dir;
498 clu_size = sbi->cluster_size;
499 }
500
501 /* update the directory entry */
502 /* fill the dos name directory entry information of the created file.
503 * the first cluster is not determined yet. (0)
504 */
505 exfat_init_dir_entry(&es, type, start_clu, clu_size, &ts);
506 exfat_init_ext_entry(&es, num_entries, &uniname);
507
508 ret = exfat_put_dentry_set(&es, IS_DIRSYNC(inode));
509 if (ret)
510 goto out;
511
512 info->dir = *p_dir;
513 info->entry = dentry;
514 info->flags = ALLOC_NO_FAT_CHAIN;
515 info->type = type;
516
517 if (type == TYPE_FILE) {
518 info->attr = EXFAT_ATTR_ARCHIVE;
519 info->start_clu = EXFAT_EOF_CLUSTER;
520 info->size = 0;
521 info->num_subdirs = 0;
522 } else {
523 info->attr = EXFAT_ATTR_SUBDIR;
524 if (sbi->options.zero_size_dir)
525 info->start_clu = EXFAT_EOF_CLUSTER;
526 else
527 info->start_clu = start_clu;
528 info->size = clu_size;
529 info->num_subdirs = EXFAT_MIN_SUBDIR;
530 }
531 info->valid_size = info->size;
532
533 memset(&info->crtime, 0, sizeof(info->crtime));
534 memset(&info->mtime, 0, sizeof(info->mtime));
535 memset(&info->atime, 0, sizeof(info->atime));
536 out:
537 return ret;
538 }
539
exfat_create(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,bool excl)540 static int exfat_create(struct mnt_idmap *idmap, struct inode *dir,
541 struct dentry *dentry, umode_t mode, bool excl)
542 {
543 struct super_block *sb = dir->i_sb;
544 struct inode *inode;
545 struct exfat_chain cdir;
546 struct exfat_dir_entry info;
547 loff_t i_pos;
548 int err;
549 loff_t size = i_size_read(dir);
550
551 if (unlikely(exfat_forced_shutdown(sb)))
552 return -EIO;
553
554 mutex_lock(&EXFAT_SB(sb)->s_lock);
555 exfat_set_volume_dirty(sb);
556 err = exfat_add_entry(dir, dentry->d_name.name, &cdir, TYPE_FILE,
557 &info);
558 if (err)
559 goto unlock;
560
561 inode_inc_iversion(dir);
562 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
563 if (IS_DIRSYNC(dir) && size != i_size_read(dir))
564 exfat_sync_inode(dir);
565 else
566 mark_inode_dirty(dir);
567
568 i_pos = exfat_make_i_pos(&info);
569 inode = exfat_build_inode(sb, &info, i_pos);
570 err = PTR_ERR_OR_ZERO(inode);
571 if (err)
572 goto unlock;
573
574 inode_inc_iversion(inode);
575 EXFAT_I(inode)->i_crtime = simple_inode_init_ts(inode);
576 exfat_truncate_inode_atime(inode);
577
578 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
579
580 d_instantiate(dentry, inode);
581 unlock:
582 mutex_unlock(&EXFAT_SB(sb)->s_lock);
583 return err;
584 }
585
586 /* lookup a file */
exfat_find(struct inode * dir,struct qstr * qname,struct exfat_dir_entry * info)587 static int exfat_find(struct inode *dir, struct qstr *qname,
588 struct exfat_dir_entry *info)
589 {
590 int ret, dentry, count;
591 struct exfat_chain cdir;
592 struct exfat_uni_name uni_name;
593 struct super_block *sb = dir->i_sb;
594 struct exfat_sb_info *sbi = EXFAT_SB(sb);
595 struct exfat_inode_info *ei = EXFAT_I(dir);
596 struct exfat_dentry *ep, *ep2;
597 struct exfat_entry_set_cache es;
598 /* for optimized dir & entry to prevent long traverse of cluster chain */
599 struct exfat_hint hint_opt;
600
601 if (qname->len == 0)
602 return -ENOENT;
603
604 /* check the validity of directory name in the given pathname */
605 ret = exfat_resolve_path_for_lookup(dir, qname->name, &cdir, &uni_name);
606 if (ret)
607 return ret;
608
609 /* check the validation of hint_stat and initialize it if required */
610 if (ei->version != (inode_peek_iversion_raw(dir) & 0xffffffff)) {
611 ei->hint_stat.clu = cdir.dir;
612 ei->hint_stat.eidx = 0;
613 ei->version = (inode_peek_iversion_raw(dir) & 0xffffffff);
614 ei->hint_femp.eidx = EXFAT_HINT_NONE;
615 }
616
617 /* search the file name for directories */
618 dentry = exfat_find_dir_entry(sb, ei, &cdir, &uni_name, &hint_opt);
619 if (dentry < 0)
620 return dentry; /* -error value */
621
622 info->dir = cdir;
623 info->entry = dentry;
624 info->num_subdirs = 0;
625
626 /* adjust cdir to the optimized value */
627 cdir.dir = hint_opt.clu;
628 if (cdir.flags & ALLOC_NO_FAT_CHAIN)
629 cdir.size -= dentry / sbi->dentries_per_clu;
630 dentry = hint_opt.eidx;
631 if (exfat_get_dentry_set(&es, sb, &cdir, dentry, ES_2_ENTRIES))
632 return -EIO;
633 ep = exfat_get_dentry_cached(&es, ES_IDX_FILE);
634 ep2 = exfat_get_dentry_cached(&es, ES_IDX_STREAM);
635
636 info->type = exfat_get_entry_type(ep);
637 info->attr = le16_to_cpu(ep->dentry.file.attr);
638 info->size = le64_to_cpu(ep2->dentry.stream.valid_size);
639 info->valid_size = le64_to_cpu(ep2->dentry.stream.valid_size);
640 info->size = le64_to_cpu(ep2->dentry.stream.size);
641
642 if (unlikely(EXFAT_B_TO_CLU_ROUND_UP(info->size, sbi) > sbi->used_clusters)) {
643 exfat_fs_error(sb, "data size is invalid(%lld)", info->size);
644 return -EIO;
645 }
646
647 info->start_clu = le32_to_cpu(ep2->dentry.stream.start_clu);
648 if (!is_valid_cluster(sbi, info->start_clu) && info->size) {
649 exfat_warn(sb, "start_clu is invalid cluster(0x%x)",
650 info->start_clu);
651 info->size = 0;
652 info->valid_size = 0;
653 }
654
655 if (info->valid_size > info->size) {
656 exfat_warn(sb, "valid_size(%lld) is greater than size(%lld)",
657 info->valid_size, info->size);
658 info->valid_size = info->size;
659 }
660
661 if (info->size == 0) {
662 info->flags = ALLOC_NO_FAT_CHAIN;
663 info->start_clu = EXFAT_EOF_CLUSTER;
664 } else
665 info->flags = ep2->dentry.stream.flags;
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_put_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
exfat_d_anon_disconn(struct dentry * dentry)703 static int exfat_d_anon_disconn(struct dentry *dentry)
704 {
705 return IS_ROOT(dentry) && (dentry->d_flags & DCACHE_DISCONNECTED);
706 }
707
exfat_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)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 */
exfat_unlink(struct inode * dir,struct dentry * dentry)785 static int exfat_unlink(struct inode *dir, struct dentry *dentry)
786 {
787 struct exfat_chain cdir;
788 struct super_block *sb = dir->i_sb;
789 struct inode *inode = dentry->d_inode;
790 struct exfat_inode_info *ei = EXFAT_I(inode);
791 struct exfat_entry_set_cache es;
792 int entry, err = 0;
793
794 if (unlikely(exfat_forced_shutdown(sb)))
795 return -EIO;
796
797 mutex_lock(&EXFAT_SB(sb)->s_lock);
798 exfat_chain_dup(&cdir, &ei->dir);
799 entry = ei->entry;
800 if (ei->dir.dir == DIR_DELETED) {
801 exfat_err(sb, "abnormal access to deleted dentry");
802 err = -ENOENT;
803 goto unlock;
804 }
805
806 err = exfat_get_dentry_set(&es, sb, &cdir, entry, ES_ALL_ENTRIES);
807 if (err) {
808 err = -EIO;
809 goto unlock;
810 }
811
812 exfat_set_volume_dirty(sb);
813
814 /* update the directory entry */
815 exfat_remove_entries(inode, &es, ES_IDX_FILE);
816
817 err = exfat_put_dentry_set(&es, IS_DIRSYNC(inode));
818 if (err)
819 goto unlock;
820
821 /* This doesn't modify ei */
822 ei->dir.dir = DIR_DELETED;
823
824 inode_inc_iversion(dir);
825 simple_inode_init_ts(dir);
826 exfat_truncate_inode_atime(dir);
827 mark_inode_dirty(dir);
828
829 clear_nlink(inode);
830 simple_inode_init_ts(inode);
831 exfat_truncate_inode_atime(inode);
832 exfat_unhash_inode(inode);
833 exfat_d_version_set(dentry, inode_query_iversion(dir));
834 unlock:
835 mutex_unlock(&EXFAT_SB(sb)->s_lock);
836 return err;
837 }
838
exfat_mkdir(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode)839 static int exfat_mkdir(struct mnt_idmap *idmap, struct inode *dir,
840 struct dentry *dentry, umode_t mode)
841 {
842 struct super_block *sb = dir->i_sb;
843 struct inode *inode;
844 struct exfat_dir_entry info;
845 struct exfat_chain cdir;
846 loff_t i_pos;
847 int err;
848 loff_t size = i_size_read(dir);
849
850 if (unlikely(exfat_forced_shutdown(sb)))
851 return -EIO;
852
853 mutex_lock(&EXFAT_SB(sb)->s_lock);
854 exfat_set_volume_dirty(sb);
855 err = exfat_add_entry(dir, dentry->d_name.name, &cdir, TYPE_DIR,
856 &info);
857 if (err)
858 goto unlock;
859
860 inode_inc_iversion(dir);
861 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
862 if (IS_DIRSYNC(dir) && size != i_size_read(dir))
863 exfat_sync_inode(dir);
864 else
865 mark_inode_dirty(dir);
866 inc_nlink(dir);
867
868 i_pos = exfat_make_i_pos(&info);
869 inode = exfat_build_inode(sb, &info, i_pos);
870 err = PTR_ERR_OR_ZERO(inode);
871 if (err)
872 goto unlock;
873
874 inode_inc_iversion(inode);
875 EXFAT_I(inode)->i_crtime = simple_inode_init_ts(inode);
876 exfat_truncate_inode_atime(inode);
877 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
878
879 d_instantiate(dentry, inode);
880
881 unlock:
882 mutex_unlock(&EXFAT_SB(sb)->s_lock);
883 return err;
884 }
885
exfat_check_dir_empty(struct super_block * sb,struct exfat_chain * p_dir)886 static int exfat_check_dir_empty(struct super_block *sb,
887 struct exfat_chain *p_dir)
888 {
889 int i, dentries_per_clu;
890 unsigned int type;
891 unsigned int clu_count = 0;
892 struct exfat_chain clu;
893 struct exfat_dentry *ep;
894 struct exfat_sb_info *sbi = EXFAT_SB(sb);
895 struct buffer_head *bh;
896
897 dentries_per_clu = sbi->dentries_per_clu;
898
899 if (p_dir->dir == EXFAT_EOF_CLUSTER)
900 return 0;
901
902 exfat_chain_dup(&clu, p_dir);
903
904 while (clu.dir != EXFAT_EOF_CLUSTER) {
905 for (i = 0; i < dentries_per_clu; i++) {
906 ep = exfat_get_dentry(sb, &clu, i, &bh);
907 if (!ep)
908 return -EIO;
909 type = exfat_get_entry_type(ep);
910 brelse(bh);
911 if (type == TYPE_UNUSED)
912 return 0;
913
914 if (type != TYPE_FILE && type != TYPE_DIR)
915 continue;
916
917 return -ENOTEMPTY;
918 }
919
920 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
921 if (--clu.size > 0)
922 clu.dir++;
923 else
924 clu.dir = EXFAT_EOF_CLUSTER;
925 } else {
926 if (exfat_get_next_cluster(sb, &(clu.dir)))
927 return -EIO;
928
929 /* break if the cluster chain includes a loop */
930 if (unlikely(++clu_count > EXFAT_DATA_CLUSTER_COUNT(sbi)))
931 break;
932 }
933 }
934
935 return 0;
936 }
937
exfat_rmdir(struct inode * dir,struct dentry * dentry)938 static int exfat_rmdir(struct inode *dir, struct dentry *dentry)
939 {
940 struct inode *inode = dentry->d_inode;
941 struct exfat_chain cdir, clu_to_free;
942 struct super_block *sb = inode->i_sb;
943 struct exfat_sb_info *sbi = EXFAT_SB(sb);
944 struct exfat_inode_info *ei = EXFAT_I(inode);
945 struct exfat_entry_set_cache es;
946 int entry, err;
947
948 if (unlikely(exfat_forced_shutdown(sb)))
949 return -EIO;
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 err = exfat_get_dentry_set(&es, sb, &cdir, entry, ES_ALL_ENTRIES);
974 if (err) {
975 err = -EIO;
976 goto unlock;
977 }
978
979 exfat_set_volume_dirty(sb);
980
981 exfat_remove_entries(inode, &es, ES_IDX_FILE);
982
983 err = exfat_put_dentry_set(&es, IS_DIRSYNC(dir));
984 if (err)
985 goto unlock;
986
987 ei->dir.dir = DIR_DELETED;
988
989 inode_inc_iversion(dir);
990 simple_inode_init_ts(dir);
991 exfat_truncate_inode_atime(dir);
992 if (IS_DIRSYNC(dir))
993 exfat_sync_inode(dir);
994 else
995 mark_inode_dirty(dir);
996 drop_nlink(dir);
997
998 clear_nlink(inode);
999 simple_inode_init_ts(inode);
1000 exfat_truncate_inode_atime(inode);
1001 exfat_unhash_inode(inode);
1002 exfat_d_version_set(dentry, inode_query_iversion(dir));
1003 unlock:
1004 mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
1005 return err;
1006 }
1007
exfat_rename_file(struct inode * inode,struct exfat_chain * p_dir,int oldentry,struct exfat_uni_name * p_uniname,struct exfat_inode_info * ei)1008 static int exfat_rename_file(struct inode *inode, struct exfat_chain *p_dir,
1009 int oldentry, struct exfat_uni_name *p_uniname,
1010 struct exfat_inode_info *ei)
1011 {
1012 int ret, num_new_entries;
1013 struct exfat_dentry *epold, *epnew;
1014 struct super_block *sb = inode->i_sb;
1015 struct exfat_entry_set_cache old_es, new_es;
1016 int sync = IS_DIRSYNC(inode);
1017
1018 if (unlikely(exfat_forced_shutdown(sb)))
1019 return -EIO;
1020
1021 num_new_entries = exfat_calc_num_entries(p_uniname);
1022 if (num_new_entries < 0)
1023 return num_new_entries;
1024
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) {
1034 int newentry;
1035
1036 newentry = exfat_find_empty_entry(inode, p_dir, num_new_entries,
1037 &new_es);
1038 if (newentry < 0) {
1039 ret = newentry; /* -EIO or -ENOSPC */
1040 goto put_old_es;
1041 }
1042
1043 epnew = exfat_get_dentry_cached(&new_es, ES_IDX_FILE);
1044 *epnew = *epold;
1045 if (exfat_get_entry_type(epnew) == TYPE_FILE) {
1046 epnew->dentry.file.attr |= cpu_to_le16(EXFAT_ATTR_ARCHIVE);
1047 ei->attr |= EXFAT_ATTR_ARCHIVE;
1048 }
1049
1050 epold = exfat_get_dentry_cached(&old_es, ES_IDX_STREAM);
1051 epnew = exfat_get_dentry_cached(&new_es, ES_IDX_STREAM);
1052 *epnew = *epold;
1053
1054 exfat_init_ext_entry(&new_es, num_new_entries, p_uniname);
1055
1056 ret = exfat_put_dentry_set(&new_es, sync);
1057 if (ret)
1058 goto put_old_es;
1059
1060 exfat_remove_entries(inode, &old_es, ES_IDX_FILE);
1061 ei->dir = *p_dir;
1062 ei->entry = newentry;
1063 } else {
1064 if (exfat_get_entry_type(epold) == TYPE_FILE) {
1065 epold->dentry.file.attr |= cpu_to_le16(EXFAT_ATTR_ARCHIVE);
1066 ei->attr |= EXFAT_ATTR_ARCHIVE;
1067 }
1068
1069 exfat_remove_entries(inode, &old_es, ES_IDX_FIRST_FILENAME + 1);
1070 exfat_init_ext_entry(&old_es, num_new_entries, p_uniname);
1071 }
1072 return exfat_put_dentry_set(&old_es, sync);
1073
1074 put_old_es:
1075 exfat_put_dentry_set(&old_es, false);
1076 return ret;
1077 }
1078
exfat_move_file(struct inode * inode,struct exfat_chain * p_olddir,int oldentry,struct exfat_chain * p_newdir,struct exfat_uni_name * p_uniname,struct exfat_inode_info * ei)1079 static int exfat_move_file(struct inode *inode, struct exfat_chain *p_olddir,
1080 int oldentry, struct exfat_chain *p_newdir,
1081 struct exfat_uni_name *p_uniname, struct exfat_inode_info *ei)
1082 {
1083 int ret, newentry, num_new_entries;
1084 struct exfat_dentry *epmov, *epnew;
1085 struct super_block *sb = inode->i_sb;
1086 struct exfat_entry_set_cache mov_es, new_es;
1087
1088 num_new_entries = exfat_calc_num_entries(p_uniname);
1089 if (num_new_entries < 0)
1090 return num_new_entries;
1091
1092 ret = exfat_get_dentry_set(&mov_es, sb, p_olddir, oldentry,
1093 ES_ALL_ENTRIES);
1094 if (ret)
1095 return -EIO;
1096
1097 newentry = exfat_find_empty_entry(inode, p_newdir, num_new_entries,
1098 &new_es);
1099 if (newentry < 0) {
1100 ret = newentry; /* -EIO or -ENOSPC */
1101 goto put_mov_es;
1102 }
1103
1104 epmov = exfat_get_dentry_cached(&mov_es, ES_IDX_FILE);
1105 epnew = exfat_get_dentry_cached(&new_es, ES_IDX_FILE);
1106 *epnew = *epmov;
1107 if (exfat_get_entry_type(epnew) == TYPE_FILE) {
1108 epnew->dentry.file.attr |= cpu_to_le16(EXFAT_ATTR_ARCHIVE);
1109 ei->attr |= EXFAT_ATTR_ARCHIVE;
1110 }
1111
1112 epmov = exfat_get_dentry_cached(&mov_es, ES_IDX_STREAM);
1113 epnew = exfat_get_dentry_cached(&new_es, ES_IDX_STREAM);
1114 *epnew = *epmov;
1115
1116 exfat_init_ext_entry(&new_es, num_new_entries, p_uniname);
1117 exfat_remove_entries(inode, &mov_es, ES_IDX_FILE);
1118
1119 exfat_chain_set(&ei->dir, p_newdir->dir, p_newdir->size,
1120 p_newdir->flags);
1121
1122 ei->entry = newentry;
1123
1124 ret = exfat_put_dentry_set(&new_es, IS_DIRSYNC(inode));
1125 if (ret)
1126 goto put_mov_es;
1127
1128 return exfat_put_dentry_set(&mov_es, IS_DIRSYNC(inode));
1129
1130 put_mov_es:
1131 exfat_put_dentry_set(&mov_es, false);
1132
1133 return ret;
1134 }
1135
1136 /* rename or move a old file into a new file */
__exfat_rename(struct inode * old_parent_inode,struct exfat_inode_info * ei,struct inode * new_parent_inode,struct dentry * new_dentry)1137 static int __exfat_rename(struct inode *old_parent_inode,
1138 struct exfat_inode_info *ei, struct inode *new_parent_inode,
1139 struct dentry *new_dentry)
1140 {
1141 int ret;
1142 int dentry;
1143 struct exfat_chain olddir, newdir;
1144 struct exfat_chain *p_dir = NULL;
1145 struct exfat_uni_name uni_name;
1146 struct exfat_dentry *ep;
1147 struct super_block *sb = old_parent_inode->i_sb;
1148 struct exfat_sb_info *sbi = EXFAT_SB(sb);
1149 const unsigned char *new_path = new_dentry->d_name.name;
1150 struct inode *new_inode = new_dentry->d_inode;
1151 struct exfat_inode_info *new_ei = NULL;
1152 unsigned int new_entry_type = TYPE_UNUSED;
1153 int new_entry = 0;
1154 struct buffer_head *new_bh = NULL;
1155
1156 /* check the validity of pointer parameters */
1157 if (new_path == NULL || strlen(new_path) == 0)
1158 return -EINVAL;
1159
1160 if (ei->dir.dir == DIR_DELETED) {
1161 exfat_err(sb, "abnormal access to deleted source dentry");
1162 return -ENOENT;
1163 }
1164
1165 exfat_chain_set(&olddir, EXFAT_I(old_parent_inode)->start_clu,
1166 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(old_parent_inode), sbi),
1167 EXFAT_I(old_parent_inode)->flags);
1168 dentry = ei->entry;
1169
1170 /* check whether new dir is existing directory and empty */
1171 if (new_inode) {
1172 ret = -EIO;
1173 new_ei = EXFAT_I(new_inode);
1174
1175 if (new_ei->dir.dir == DIR_DELETED) {
1176 exfat_err(sb, "abnormal access to deleted target dentry");
1177 goto out;
1178 }
1179
1180 p_dir = &(new_ei->dir);
1181 new_entry = new_ei->entry;
1182 ep = exfat_get_dentry(sb, p_dir, new_entry, &new_bh);
1183 if (!ep)
1184 goto out;
1185
1186 new_entry_type = exfat_get_entry_type(ep);
1187 brelse(new_bh);
1188
1189 /* if new_inode exists, update ei */
1190 if (new_entry_type == TYPE_DIR) {
1191 struct exfat_chain new_clu;
1192
1193 new_clu.dir = new_ei->start_clu;
1194 new_clu.size =
1195 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode),
1196 sbi);
1197 new_clu.flags = new_ei->flags;
1198
1199 ret = exfat_check_dir_empty(sb, &new_clu);
1200 if (ret)
1201 goto out;
1202 }
1203 }
1204
1205 /* check the validity of directory name in the given new pathname */
1206 ret = exfat_resolve_path(new_parent_inode, new_path, &newdir,
1207 &uni_name);
1208 if (ret)
1209 goto out;
1210
1211 exfat_set_volume_dirty(sb);
1212
1213 if (olddir.dir == newdir.dir)
1214 ret = exfat_rename_file(new_parent_inode, &olddir, dentry,
1215 &uni_name, ei);
1216 else
1217 ret = exfat_move_file(new_parent_inode, &olddir, dentry,
1218 &newdir, &uni_name, ei);
1219
1220 if (!ret && new_inode) {
1221 struct exfat_entry_set_cache es;
1222
1223 /* delete entries of new_dir */
1224 ret = exfat_get_dentry_set(&es, sb, p_dir, new_entry,
1225 ES_ALL_ENTRIES);
1226 if (ret) {
1227 ret = -EIO;
1228 goto del_out;
1229 }
1230
1231 exfat_remove_entries(new_inode, &es, ES_IDX_FILE);
1232
1233 ret = exfat_put_dentry_set(&es, IS_DIRSYNC(new_inode));
1234 if (ret)
1235 goto del_out;
1236
1237 /* Free the clusters if new_inode is a dir(as if exfat_rmdir) */
1238 if (new_entry_type == TYPE_DIR &&
1239 new_ei->start_clu != EXFAT_EOF_CLUSTER) {
1240 /* new_ei, new_clu_to_free */
1241 struct exfat_chain new_clu_to_free;
1242
1243 exfat_chain_set(&new_clu_to_free, new_ei->start_clu,
1244 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode),
1245 sbi), new_ei->flags);
1246
1247 if (exfat_free_cluster(new_inode, &new_clu_to_free)) {
1248 /* just set I/O error only */
1249 ret = -EIO;
1250 }
1251
1252 i_size_write(new_inode, 0);
1253 new_ei->valid_size = 0;
1254 new_ei->start_clu = EXFAT_EOF_CLUSTER;
1255 new_ei->flags = ALLOC_NO_FAT_CHAIN;
1256 }
1257 del_out:
1258 /* Update new_inode ei
1259 * Prevent syncing removed new_inode
1260 * (new_ei is already initialized above code ("if (new_inode)")
1261 */
1262 new_ei->dir.dir = DIR_DELETED;
1263 }
1264 out:
1265 return ret;
1266 }
1267
exfat_rename(struct mnt_idmap * idmap,struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)1268 static int exfat_rename(struct mnt_idmap *idmap,
1269 struct inode *old_dir, struct dentry *old_dentry,
1270 struct inode *new_dir, struct dentry *new_dentry,
1271 unsigned int flags)
1272 {
1273 struct inode *old_inode, *new_inode;
1274 struct super_block *sb = old_dir->i_sb;
1275 loff_t i_pos;
1276 int err;
1277 loff_t size = i_size_read(new_dir);
1278
1279 /*
1280 * The VFS already checks for existence, so for local filesystems
1281 * the RENAME_NOREPLACE implementation is equivalent to plain rename.
1282 * Don't support any other flags
1283 */
1284 if (flags & ~RENAME_NOREPLACE)
1285 return -EINVAL;
1286
1287 mutex_lock(&EXFAT_SB(sb)->s_lock);
1288 old_inode = old_dentry->d_inode;
1289 new_inode = new_dentry->d_inode;
1290
1291 err = __exfat_rename(old_dir, EXFAT_I(old_inode), new_dir, new_dentry);
1292 if (err)
1293 goto unlock;
1294
1295 inode_inc_iversion(new_dir);
1296 simple_rename_timestamp(old_dir, old_dentry, new_dir, new_dentry);
1297 EXFAT_I(new_dir)->i_crtime = current_time(new_dir);
1298 exfat_truncate_inode_atime(new_dir);
1299 if (IS_DIRSYNC(new_dir) && size != i_size_read(new_dir))
1300 exfat_sync_inode(new_dir);
1301 else
1302 mark_inode_dirty(new_dir);
1303
1304 i_pos = ((loff_t)EXFAT_I(old_inode)->dir.dir << 32) |
1305 (EXFAT_I(old_inode)->entry & 0xffffffff);
1306 exfat_unhash_inode(old_inode);
1307 exfat_hash_inode(old_inode, i_pos);
1308 if (IS_DIRSYNC(new_dir))
1309 exfat_sync_inode(old_inode);
1310 else
1311 mark_inode_dirty(old_inode);
1312
1313 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
1314 drop_nlink(old_dir);
1315 if (!new_inode)
1316 inc_nlink(new_dir);
1317 }
1318
1319 inode_inc_iversion(old_dir);
1320 if (new_dir != old_dir)
1321 mark_inode_dirty(old_dir);
1322
1323 if (new_inode) {
1324 exfat_unhash_inode(new_inode);
1325
1326 /* skip drop_nlink if new_inode already has been dropped */
1327 if (new_inode->i_nlink) {
1328 drop_nlink(new_inode);
1329 if (S_ISDIR(new_inode->i_mode))
1330 drop_nlink(new_inode);
1331 } else {
1332 exfat_warn(sb, "abnormal access to an inode dropped");
1333 WARN_ON(new_inode->i_nlink == 0);
1334 }
1335 EXFAT_I(new_inode)->i_crtime = current_time(new_inode);
1336 }
1337
1338 unlock:
1339 mutex_unlock(&EXFAT_SB(sb)->s_lock);
1340 return err;
1341 }
1342
1343 const struct inode_operations exfat_dir_inode_operations = {
1344 .create = exfat_create,
1345 .lookup = exfat_lookup,
1346 .unlink = exfat_unlink,
1347 .mkdir = exfat_mkdir,
1348 .rmdir = exfat_rmdir,
1349 .rename = exfat_rename,
1350 .setattr = exfat_setattr,
1351 .getattr = exfat_getattr,
1352 };
1353