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