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