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