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