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