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