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