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