xref: /linux/fs/exfat/namei.c (revision 231eb762bbe8fa232e672e5a399eed16a0c0c45a)
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 /* search EMPTY CONTINUOUS "num_entries" entries */
208 static int exfat_search_empty_slot(struct super_block *sb,
209 		struct exfat_hint_femp *hint_femp, struct exfat_chain *p_dir,
210 		int num_entries, struct exfat_entry_set_cache *es)
211 {
212 	int i, dentry, ret;
213 	int dentries_per_clu;
214 	struct exfat_chain clu;
215 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
216 	int total_entries = EXFAT_CLU_TO_DEN(p_dir->size, sbi);
217 
218 	dentries_per_clu = sbi->dentries_per_clu;
219 
220 	if (hint_femp->eidx != EXFAT_HINT_NONE) {
221 		dentry = hint_femp->eidx;
222 
223 		/*
224 		 * If hint_femp->count is enough, it is needed to check if
225 		 * there are actual empty entries.
226 		 * Otherwise, and if "dentry + hint_famp->count" is also equal
227 		 * to "p_dir->size * dentries_per_clu", it means ENOSPC.
228 		 */
229 		if (dentry + hint_femp->count == total_entries &&
230 		    num_entries > hint_femp->count)
231 			return -ENOSPC;
232 
233 		hint_femp->eidx = EXFAT_HINT_NONE;
234 		exfat_chain_dup(&clu, &hint_femp->cur);
235 	} else {
236 		exfat_chain_dup(&clu, p_dir);
237 		dentry = 0;
238 	}
239 
240 	while (dentry + num_entries < total_entries &&
241 	       clu.dir != EXFAT_EOF_CLUSTER) {
242 		i = dentry & (dentries_per_clu - 1);
243 
244 		ret = exfat_get_empty_dentry_set(es, sb, &clu, i, num_entries);
245 		if (ret < 0)
246 			return ret;
247 		else if (ret == 0)
248 			return dentry;
249 
250 		dentry += ret;
251 		i += ret;
252 
253 		while (i >= dentries_per_clu) {
254 			if (clu.flags == ALLOC_NO_FAT_CHAIN) {
255 				if (--clu.size > 0)
256 					clu.dir++;
257 				else
258 					clu.dir = EXFAT_EOF_CLUSTER;
259 			} else {
260 				if (exfat_get_next_cluster(sb, &clu.dir))
261 					return -EIO;
262 			}
263 
264 			i -= dentries_per_clu;
265 		}
266 	}
267 
268 	hint_femp->eidx = dentry;
269 	hint_femp->count = 0;
270 	if (dentry == total_entries || clu.dir == EXFAT_EOF_CLUSTER)
271 		exfat_chain_set(&hint_femp->cur, EXFAT_EOF_CLUSTER, 0,
272 				clu.flags);
273 	else
274 		hint_femp->cur = clu;
275 
276 	return -ENOSPC;
277 }
278 
279 static int exfat_check_max_dentries(struct inode *inode)
280 {
281 	if (EXFAT_B_TO_DEN(i_size_read(inode)) >= MAX_EXFAT_DENTRIES) {
282 		/*
283 		 * exFAT spec allows a dir to grow up to 8388608(256MB)
284 		 * dentries
285 		 */
286 		return -ENOSPC;
287 	}
288 	return 0;
289 }
290 
291 /* find empty directory entry.
292  * if there isn't any empty slot, expand cluster chain.
293  */
294 static int exfat_find_empty_entry(struct inode *inode,
295 		struct exfat_chain *p_dir, int num_entries,
296 		struct exfat_entry_set_cache *es)
297 {
298 	int dentry;
299 	unsigned int ret, last_clu;
300 	loff_t size = 0;
301 	struct exfat_chain clu;
302 	struct super_block *sb = inode->i_sb;
303 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
304 	struct exfat_inode_info *ei = EXFAT_I(inode);
305 	struct exfat_hint_femp hint_femp;
306 
307 	hint_femp.eidx = EXFAT_HINT_NONE;
308 
309 	if (ei->hint_femp.eidx != EXFAT_HINT_NONE) {
310 		hint_femp = ei->hint_femp;
311 		ei->hint_femp.eidx = EXFAT_HINT_NONE;
312 	}
313 
314 	while ((dentry = exfat_search_empty_slot(sb, &hint_femp, p_dir,
315 					num_entries, es)) < 0) {
316 		if (dentry == -EIO)
317 			break;
318 
319 		if (exfat_check_max_dentries(inode))
320 			return -ENOSPC;
321 
322 		/*
323 		 * Allocate new cluster to this directory
324 		 */
325 		if (ei->start_clu != EXFAT_EOF_CLUSTER) {
326 			/* we trust p_dir->size regardless of FAT type */
327 			if (exfat_find_last_cluster(sb, p_dir, &last_clu))
328 				return -EIO;
329 
330 			exfat_chain_set(&clu, last_clu + 1, 0, p_dir->flags);
331 		} else {
332 			/* This directory is empty */
333 			exfat_chain_set(&clu, EXFAT_EOF_CLUSTER, 0,
334 					ALLOC_NO_FAT_CHAIN);
335 		}
336 
337 		/* allocate a cluster */
338 		ret = exfat_alloc_cluster(inode, 1, &clu, IS_DIRSYNC(inode));
339 		if (ret)
340 			return ret;
341 
342 		if (exfat_zeroed_cluster(inode, clu.dir))
343 			return -EIO;
344 
345 		if (ei->start_clu == EXFAT_EOF_CLUSTER) {
346 			ei->start_clu = clu.dir;
347 			p_dir->dir = clu.dir;
348 		}
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.cur.dir == EXFAT_EOF_CLUSTER)
365 			exfat_chain_set(&hint_femp.cur, clu.dir, 0, clu.flags);
366 
367 		hint_femp.count += sbi->dentries_per_clu;
368 
369 		hint_femp.cur.size++;
370 		p_dir->size++;
371 		size = EXFAT_CLU_TO_B(p_dir->size, sbi);
372 
373 		/* directory inode should be updated in here */
374 		i_size_write(inode, size);
375 		ei->valid_size += sbi->cluster_size;
376 		ei->flags = p_dir->flags;
377 		inode->i_blocks += sbi->cluster_size >> 9;
378 	}
379 
380 	return dentry;
381 }
382 
383 /*
384  * Name Resolution Functions :
385  * Zero if it was successful; otherwise nonzero.
386  */
387 static int __exfat_resolve_path(struct inode *inode, const unsigned char *path,
388 		struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
389 		int lookup)
390 {
391 	int namelen;
392 	int lossy = NLS_NAME_NO_LOSSY;
393 	struct super_block *sb = inode->i_sb;
394 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
395 	struct exfat_inode_info *ei = EXFAT_I(inode);
396 	int pathlen = strlen(path);
397 
398 	/*
399 	 * get the length of the pathname excluding
400 	 * trailing periods, if any.
401 	 */
402 	namelen = exfat_striptail_len(pathlen, path, false);
403 	if (EXFAT_SB(sb)->options.keep_last_dots) {
404 		/*
405 		 * Do not allow the creation of files with names
406 		 * ending with period(s).
407 		 */
408 		if (!lookup && (namelen < pathlen))
409 			return -EINVAL;
410 		namelen = pathlen;
411 	}
412 	if (!namelen)
413 		return -ENOENT;
414 	if (pathlen > (MAX_NAME_LENGTH * MAX_CHARSET_SIZE))
415 		return -ENAMETOOLONG;
416 
417 	/*
418 	 * strip all leading spaces :
419 	 * "MS windows 7" supports leading spaces.
420 	 * So we should skip this preprocessing for compatibility.
421 	 */
422 
423 	/* file name conversion :
424 	 * If lookup case, we allow bad-name for compatibility.
425 	 */
426 	namelen = exfat_nls_to_utf16(sb, path, namelen, p_uniname,
427 			&lossy);
428 	if (namelen < 0)
429 		return namelen; /* return error value */
430 
431 	if ((lossy && !lookup) || !namelen)
432 		return (lossy & NLS_NAME_OVERLEN) ? -ENAMETOOLONG : -EINVAL;
433 
434 	exfat_chain_set(p_dir, ei->start_clu,
435 		EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags);
436 
437 	return 0;
438 }
439 
440 static inline int exfat_resolve_path(struct inode *inode,
441 		const unsigned char *path, struct exfat_chain *dir,
442 		struct exfat_uni_name *uni)
443 {
444 	return __exfat_resolve_path(inode, path, dir, uni, 0);
445 }
446 
447 static inline int exfat_resolve_path_for_lookup(struct inode *inode,
448 		const unsigned char *path, struct exfat_chain *dir,
449 		struct exfat_uni_name *uni)
450 {
451 	return __exfat_resolve_path(inode, path, dir, uni, 1);
452 }
453 
454 static inline loff_t exfat_make_i_pos(struct exfat_dir_entry *info)
455 {
456 	return ((loff_t) info->dir.dir << 32) | (info->entry & 0xffffffff);
457 }
458 
459 static int exfat_add_entry(struct inode *inode, const char *path,
460 		struct exfat_chain *p_dir, unsigned int type,
461 		struct exfat_dir_entry *info)
462 {
463 	int ret, dentry, num_entries;
464 	struct super_block *sb = inode->i_sb;
465 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
466 	struct exfat_uni_name uniname;
467 	struct exfat_chain clu;
468 	struct timespec64 ts = current_time(inode);
469 	struct exfat_entry_set_cache es;
470 	int clu_size = 0;
471 	unsigned int start_clu = EXFAT_FREE_CLUSTER;
472 
473 	ret = exfat_resolve_path(inode, path, p_dir, &uniname);
474 	if (ret)
475 		goto out;
476 
477 	num_entries = exfat_calc_num_entries(&uniname);
478 	if (num_entries < 0) {
479 		ret = num_entries;
480 		goto out;
481 	}
482 
483 	/* exfat_find_empty_entry must be called before alloc_cluster() */
484 	dentry = exfat_find_empty_entry(inode, p_dir, num_entries, &es);
485 	if (dentry < 0) {
486 		ret = dentry; /* -EIO or -ENOSPC */
487 		goto out;
488 	}
489 
490 	if (type == TYPE_DIR && !sbi->options.zero_size_dir) {
491 		ret = exfat_alloc_new_dir(inode, &clu);
492 		if (ret) {
493 			exfat_put_dentry_set(&es, false);
494 			goto out;
495 		}
496 		start_clu = clu.dir;
497 		clu_size = sbi->cluster_size;
498 	}
499 
500 	/* update the directory entry */
501 	/* fill the dos name directory entry information of the created file.
502 	 * the first cluster is not determined yet. (0)
503 	 */
504 	exfat_init_dir_entry(&es, type, start_clu, clu_size, &ts);
505 	exfat_init_ext_entry(&es, num_entries, &uniname);
506 
507 	ret = exfat_put_dentry_set(&es, IS_DIRSYNC(inode));
508 	if (ret)
509 		goto out;
510 
511 	info->dir = *p_dir;
512 	info->entry = dentry;
513 	info->flags = ALLOC_NO_FAT_CHAIN;
514 	info->type = type;
515 
516 	if (type == TYPE_FILE) {
517 		info->attr = EXFAT_ATTR_ARCHIVE;
518 		info->start_clu = EXFAT_EOF_CLUSTER;
519 		info->size = 0;
520 		info->num_subdirs = 0;
521 	} else {
522 		info->attr = EXFAT_ATTR_SUBDIR;
523 		if (sbi->options.zero_size_dir)
524 			info->start_clu = EXFAT_EOF_CLUSTER;
525 		else
526 			info->start_clu = start_clu;
527 		info->size = clu_size;
528 		info->num_subdirs = EXFAT_MIN_SUBDIR;
529 	}
530 	info->valid_size = info->size;
531 
532 	memset(&info->crtime, 0, sizeof(info->crtime));
533 	memset(&info->mtime, 0, sizeof(info->mtime));
534 	memset(&info->atime, 0, sizeof(info->atime));
535 out:
536 	return ret;
537 }
538 
539 static int exfat_create(struct mnt_idmap *idmap, struct inode *dir,
540 			struct dentry *dentry, umode_t mode, bool excl)
541 {
542 	struct super_block *sb = dir->i_sb;
543 	struct inode *inode;
544 	struct exfat_chain cdir;
545 	struct exfat_dir_entry info;
546 	loff_t i_pos;
547 	int err;
548 	loff_t size = i_size_read(dir);
549 
550 	mutex_lock(&EXFAT_SB(sb)->s_lock);
551 	exfat_set_volume_dirty(sb);
552 	err = exfat_add_entry(dir, dentry->d_name.name, &cdir, TYPE_FILE,
553 		&info);
554 	if (err)
555 		goto unlock;
556 
557 	inode_inc_iversion(dir);
558 	inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
559 	if (IS_DIRSYNC(dir) && size != i_size_read(dir))
560 		exfat_sync_inode(dir);
561 	else
562 		mark_inode_dirty(dir);
563 
564 	i_pos = exfat_make_i_pos(&info);
565 	inode = exfat_build_inode(sb, &info, i_pos);
566 	err = PTR_ERR_OR_ZERO(inode);
567 	if (err)
568 		goto unlock;
569 
570 	inode_inc_iversion(inode);
571 	EXFAT_I(inode)->i_crtime = simple_inode_init_ts(inode);
572 	exfat_truncate_inode_atime(inode);
573 
574 	/* timestamp is already written, so mark_inode_dirty() is unneeded. */
575 
576 	d_instantiate(dentry, inode);
577 unlock:
578 	mutex_unlock(&EXFAT_SB(sb)->s_lock);
579 	return err;
580 }
581 
582 /* lookup a file */
583 static int exfat_find(struct inode *dir, struct qstr *qname,
584 		struct exfat_dir_entry *info)
585 {
586 	int ret, dentry, count;
587 	struct exfat_chain cdir;
588 	struct exfat_uni_name uni_name;
589 	struct super_block *sb = dir->i_sb;
590 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
591 	struct exfat_inode_info *ei = EXFAT_I(dir);
592 	struct exfat_dentry *ep, *ep2;
593 	struct exfat_entry_set_cache es;
594 	/* for optimized dir & entry to prevent long traverse of cluster chain */
595 	struct exfat_hint hint_opt;
596 
597 	if (qname->len == 0)
598 		return -ENOENT;
599 
600 	/* check the validity of directory name in the given pathname */
601 	ret = exfat_resolve_path_for_lookup(dir, qname->name, &cdir, &uni_name);
602 	if (ret)
603 		return ret;
604 
605 	/* check the validation of hint_stat and initialize it if required */
606 	if (ei->version != (inode_peek_iversion_raw(dir) & 0xffffffff)) {
607 		ei->hint_stat.clu = cdir.dir;
608 		ei->hint_stat.eidx = 0;
609 		ei->version = (inode_peek_iversion_raw(dir) & 0xffffffff);
610 		ei->hint_femp.eidx = EXFAT_HINT_NONE;
611 	}
612 
613 	/* search the file name for directories */
614 	dentry = exfat_find_dir_entry(sb, ei, &cdir, &uni_name, &hint_opt);
615 	if (dentry < 0)
616 		return dentry; /* -error value */
617 
618 	info->dir = cdir;
619 	info->entry = dentry;
620 	info->num_subdirs = 0;
621 
622 	/* adjust cdir to the optimized value */
623 	cdir.dir = hint_opt.clu;
624 	if (cdir.flags & ALLOC_NO_FAT_CHAIN)
625 		cdir.size -= dentry / sbi->dentries_per_clu;
626 	dentry = hint_opt.eidx;
627 	if (exfat_get_dentry_set(&es, sb, &cdir, dentry, ES_2_ENTRIES))
628 		return -EIO;
629 	ep = exfat_get_dentry_cached(&es, ES_IDX_FILE);
630 	ep2 = exfat_get_dentry_cached(&es, ES_IDX_STREAM);
631 
632 	info->type = exfat_get_entry_type(ep);
633 	info->attr = le16_to_cpu(ep->dentry.file.attr);
634 	info->size = le64_to_cpu(ep2->dentry.stream.valid_size);
635 	info->valid_size = le64_to_cpu(ep2->dentry.stream.valid_size);
636 	info->size = le64_to_cpu(ep2->dentry.stream.size);
637 	if (info->size == 0) {
638 		info->flags = ALLOC_NO_FAT_CHAIN;
639 		info->start_clu = EXFAT_EOF_CLUSTER;
640 	} else {
641 		info->flags = ep2->dentry.stream.flags;
642 		info->start_clu =
643 			le32_to_cpu(ep2->dentry.stream.start_clu);
644 	}
645 
646 	exfat_get_entry_time(sbi, &info->crtime,
647 			     ep->dentry.file.create_tz,
648 			     ep->dentry.file.create_time,
649 			     ep->dentry.file.create_date,
650 			     ep->dentry.file.create_time_cs);
651 	exfat_get_entry_time(sbi, &info->mtime,
652 			     ep->dentry.file.modify_tz,
653 			     ep->dentry.file.modify_time,
654 			     ep->dentry.file.modify_date,
655 			     ep->dentry.file.modify_time_cs);
656 	exfat_get_entry_time(sbi, &info->atime,
657 			     ep->dentry.file.access_tz,
658 			     ep->dentry.file.access_time,
659 			     ep->dentry.file.access_date,
660 			     0);
661 	exfat_put_dentry_set(&es, false);
662 
663 	if (ei->start_clu == EXFAT_FREE_CLUSTER) {
664 		exfat_fs_error(sb,
665 			       "non-zero size file starts with zero cluster (size : %llu, p_dir : %u, entry : 0x%08x)",
666 			       i_size_read(dir), ei->dir.dir, ei->entry);
667 		return -EIO;
668 	}
669 
670 	if (info->type == TYPE_DIR) {
671 		exfat_chain_set(&cdir, info->start_clu,
672 				EXFAT_B_TO_CLU(info->size, sbi), info->flags);
673 		count = exfat_count_dir_entries(sb, &cdir);
674 		if (count < 0)
675 			return -EIO;
676 
677 		info->num_subdirs = count + EXFAT_MIN_SUBDIR;
678 	}
679 	return 0;
680 }
681 
682 static int exfat_d_anon_disconn(struct dentry *dentry)
683 {
684 	return IS_ROOT(dentry) && (dentry->d_flags & DCACHE_DISCONNECTED);
685 }
686 
687 static struct dentry *exfat_lookup(struct inode *dir, struct dentry *dentry,
688 		unsigned int flags)
689 {
690 	struct super_block *sb = dir->i_sb;
691 	struct inode *inode;
692 	struct dentry *alias;
693 	struct exfat_dir_entry info;
694 	int err;
695 	loff_t i_pos;
696 	mode_t i_mode;
697 
698 	mutex_lock(&EXFAT_SB(sb)->s_lock);
699 	err = exfat_find(dir, &dentry->d_name, &info);
700 	if (err) {
701 		if (err == -ENOENT) {
702 			inode = NULL;
703 			goto out;
704 		}
705 		goto unlock;
706 	}
707 
708 	i_pos = exfat_make_i_pos(&info);
709 	inode = exfat_build_inode(sb, &info, i_pos);
710 	err = PTR_ERR_OR_ZERO(inode);
711 	if (err)
712 		goto unlock;
713 
714 	i_mode = inode->i_mode;
715 	alias = d_find_alias(inode);
716 
717 	/*
718 	 * Checking "alias->d_parent == dentry->d_parent" to make sure
719 	 * FS is not corrupted (especially double linked dir).
720 	 */
721 	if (alias && alias->d_parent == dentry->d_parent &&
722 			!exfat_d_anon_disconn(alias)) {
723 
724 		/*
725 		 * Unhashed alias is able to exist because of revalidate()
726 		 * called by lookup_fast. You can easily make this status
727 		 * by calling create and lookup concurrently
728 		 * In such case, we reuse an alias instead of new dentry
729 		 */
730 		if (d_unhashed(alias)) {
731 			WARN_ON(alias->d_name.hash_len !=
732 				dentry->d_name.hash_len);
733 			exfat_info(sb, "rehashed a dentry(%p) in read lookup",
734 				   alias);
735 			d_drop(dentry);
736 			d_rehash(alias);
737 		} else if (!S_ISDIR(i_mode)) {
738 			/*
739 			 * This inode has non anonymous-DCACHE_DISCONNECTED
740 			 * dentry. This means, the user did ->lookup() by an
741 			 * another name (longname vs 8.3 alias of it) in past.
742 			 *
743 			 * Switch to new one for reason of locality if possible.
744 			 */
745 			d_move(alias, dentry);
746 		}
747 		iput(inode);
748 		mutex_unlock(&EXFAT_SB(sb)->s_lock);
749 		return alias;
750 	}
751 	dput(alias);
752 out:
753 	mutex_unlock(&EXFAT_SB(sb)->s_lock);
754 	if (!inode)
755 		exfat_d_version_set(dentry, inode_query_iversion(dir));
756 
757 	return d_splice_alias(inode, dentry);
758 unlock:
759 	mutex_unlock(&EXFAT_SB(sb)->s_lock);
760 	return ERR_PTR(err);
761 }
762 
763 /* remove an entry, BUT don't truncate */
764 static int exfat_unlink(struct inode *dir, struct dentry *dentry)
765 {
766 	struct exfat_chain cdir;
767 	struct super_block *sb = dir->i_sb;
768 	struct inode *inode = dentry->d_inode;
769 	struct exfat_inode_info *ei = EXFAT_I(inode);
770 	struct exfat_entry_set_cache es;
771 	int entry, err = 0;
772 
773 	mutex_lock(&EXFAT_SB(sb)->s_lock);
774 	exfat_chain_dup(&cdir, &ei->dir);
775 	entry = ei->entry;
776 	if (ei->dir.dir == DIR_DELETED) {
777 		exfat_err(sb, "abnormal access to deleted dentry");
778 		err = -ENOENT;
779 		goto unlock;
780 	}
781 
782 	err = exfat_get_dentry_set(&es, sb, &cdir, entry, ES_ALL_ENTRIES);
783 	if (err) {
784 		err = -EIO;
785 		goto unlock;
786 	}
787 
788 	exfat_set_volume_dirty(sb);
789 
790 	/* update the directory entry */
791 	exfat_remove_entries(inode, &es, ES_IDX_FILE);
792 
793 	err = exfat_put_dentry_set(&es, IS_DIRSYNC(inode));
794 	if (err)
795 		goto unlock;
796 
797 	/* This doesn't modify ei */
798 	ei->dir.dir = DIR_DELETED;
799 
800 	inode_inc_iversion(dir);
801 	simple_inode_init_ts(dir);
802 	exfat_truncate_inode_atime(dir);
803 	mark_inode_dirty(dir);
804 
805 	clear_nlink(inode);
806 	simple_inode_init_ts(inode);
807 	exfat_truncate_inode_atime(inode);
808 	exfat_unhash_inode(inode);
809 	exfat_d_version_set(dentry, inode_query_iversion(dir));
810 unlock:
811 	mutex_unlock(&EXFAT_SB(sb)->s_lock);
812 	return err;
813 }
814 
815 static int exfat_mkdir(struct mnt_idmap *idmap, struct inode *dir,
816 		       struct dentry *dentry, umode_t mode)
817 {
818 	struct super_block *sb = dir->i_sb;
819 	struct inode *inode;
820 	struct exfat_dir_entry info;
821 	struct exfat_chain cdir;
822 	loff_t i_pos;
823 	int err;
824 	loff_t size = i_size_read(dir);
825 
826 	mutex_lock(&EXFAT_SB(sb)->s_lock);
827 	exfat_set_volume_dirty(sb);
828 	err = exfat_add_entry(dir, dentry->d_name.name, &cdir, TYPE_DIR,
829 		&info);
830 	if (err)
831 		goto unlock;
832 
833 	inode_inc_iversion(dir);
834 	inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
835 	if (IS_DIRSYNC(dir) && size != i_size_read(dir))
836 		exfat_sync_inode(dir);
837 	else
838 		mark_inode_dirty(dir);
839 	inc_nlink(dir);
840 
841 	i_pos = exfat_make_i_pos(&info);
842 	inode = exfat_build_inode(sb, &info, i_pos);
843 	err = PTR_ERR_OR_ZERO(inode);
844 	if (err)
845 		goto unlock;
846 
847 	inode_inc_iversion(inode);
848 	EXFAT_I(inode)->i_crtime = simple_inode_init_ts(inode);
849 	exfat_truncate_inode_atime(inode);
850 	/* timestamp is already written, so mark_inode_dirty() is unneeded. */
851 
852 	d_instantiate(dentry, inode);
853 
854 unlock:
855 	mutex_unlock(&EXFAT_SB(sb)->s_lock);
856 	return err;
857 }
858 
859 static int exfat_check_dir_empty(struct super_block *sb,
860 		struct exfat_chain *p_dir)
861 {
862 	int i, dentries_per_clu;
863 	unsigned int type;
864 	struct exfat_chain clu;
865 	struct exfat_dentry *ep;
866 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
867 	struct buffer_head *bh;
868 
869 	dentries_per_clu = sbi->dentries_per_clu;
870 
871 	if (p_dir->dir == EXFAT_EOF_CLUSTER)
872 		return 0;
873 
874 	exfat_chain_dup(&clu, p_dir);
875 
876 	while (clu.dir != EXFAT_EOF_CLUSTER) {
877 		for (i = 0; i < dentries_per_clu; i++) {
878 			ep = exfat_get_dentry(sb, &clu, i, &bh);
879 			if (!ep)
880 				return -EIO;
881 			type = exfat_get_entry_type(ep);
882 			brelse(bh);
883 			if (type == TYPE_UNUSED)
884 				return 0;
885 
886 			if (type != TYPE_FILE && type != TYPE_DIR)
887 				continue;
888 
889 			return -ENOTEMPTY;
890 		}
891 
892 		if (clu.flags == ALLOC_NO_FAT_CHAIN) {
893 			if (--clu.size > 0)
894 				clu.dir++;
895 			else
896 				clu.dir = EXFAT_EOF_CLUSTER;
897 		} else {
898 			if (exfat_get_next_cluster(sb, &(clu.dir)))
899 				return -EIO;
900 		}
901 	}
902 
903 	return 0;
904 }
905 
906 static int exfat_rmdir(struct inode *dir, struct dentry *dentry)
907 {
908 	struct inode *inode = dentry->d_inode;
909 	struct exfat_chain cdir, clu_to_free;
910 	struct super_block *sb = inode->i_sb;
911 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
912 	struct exfat_inode_info *ei = EXFAT_I(inode);
913 	struct exfat_entry_set_cache es;
914 	int entry, err;
915 
916 	mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
917 
918 	exfat_chain_dup(&cdir, &ei->dir);
919 	entry = ei->entry;
920 
921 	if (ei->dir.dir == DIR_DELETED) {
922 		exfat_err(sb, "abnormal access to deleted dentry");
923 		err = -ENOENT;
924 		goto unlock;
925 	}
926 
927 	exfat_chain_set(&clu_to_free, ei->start_clu,
928 		EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi), ei->flags);
929 
930 	err = exfat_check_dir_empty(sb, &clu_to_free);
931 	if (err) {
932 		if (err == -EIO)
933 			exfat_err(sb, "failed to exfat_check_dir_empty : err(%d)",
934 				  err);
935 		goto unlock;
936 	}
937 
938 	err = exfat_get_dentry_set(&es, sb, &cdir, entry, ES_ALL_ENTRIES);
939 	if (err) {
940 		err = -EIO;
941 		goto unlock;
942 	}
943 
944 	exfat_set_volume_dirty(sb);
945 
946 	exfat_remove_entries(inode, &es, ES_IDX_FILE);
947 
948 	err = exfat_put_dentry_set(&es, IS_DIRSYNC(dir));
949 	if (err)
950 		goto unlock;
951 
952 	ei->dir.dir = DIR_DELETED;
953 
954 	inode_inc_iversion(dir);
955 	simple_inode_init_ts(dir);
956 	exfat_truncate_inode_atime(dir);
957 	if (IS_DIRSYNC(dir))
958 		exfat_sync_inode(dir);
959 	else
960 		mark_inode_dirty(dir);
961 	drop_nlink(dir);
962 
963 	clear_nlink(inode);
964 	simple_inode_init_ts(inode);
965 	exfat_truncate_inode_atime(inode);
966 	exfat_unhash_inode(inode);
967 	exfat_d_version_set(dentry, inode_query_iversion(dir));
968 unlock:
969 	mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
970 	return err;
971 }
972 
973 static int exfat_rename_file(struct inode *inode, struct exfat_chain *p_dir,
974 		int oldentry, struct exfat_uni_name *p_uniname,
975 		struct exfat_inode_info *ei)
976 {
977 	int ret, num_new_entries;
978 	struct exfat_dentry *epold, *epnew;
979 	struct super_block *sb = inode->i_sb;
980 	struct exfat_entry_set_cache old_es, new_es;
981 	int sync = IS_DIRSYNC(inode);
982 
983 	num_new_entries = exfat_calc_num_entries(p_uniname);
984 	if (num_new_entries < 0)
985 		return num_new_entries;
986 
987 	ret = exfat_get_dentry_set(&old_es, sb, p_dir, oldentry, ES_ALL_ENTRIES);
988 	if (ret) {
989 		ret = -EIO;
990 		return ret;
991 	}
992 
993 	epold = exfat_get_dentry_cached(&old_es, ES_IDX_FILE);
994 
995 	if (old_es.num_entries < num_new_entries) {
996 		int newentry;
997 
998 		newentry = exfat_find_empty_entry(inode, p_dir, num_new_entries,
999 				&new_es);
1000 		if (newentry < 0) {
1001 			ret = newentry; /* -EIO or -ENOSPC */
1002 			goto put_old_es;
1003 		}
1004 
1005 		epnew = exfat_get_dentry_cached(&new_es, ES_IDX_FILE);
1006 		*epnew = *epold;
1007 		if (exfat_get_entry_type(epnew) == TYPE_FILE) {
1008 			epnew->dentry.file.attr |= cpu_to_le16(EXFAT_ATTR_ARCHIVE);
1009 			ei->attr |= EXFAT_ATTR_ARCHIVE;
1010 		}
1011 
1012 		epold = exfat_get_dentry_cached(&old_es, ES_IDX_STREAM);
1013 		epnew = exfat_get_dentry_cached(&new_es, ES_IDX_STREAM);
1014 		*epnew = *epold;
1015 
1016 		exfat_init_ext_entry(&new_es, num_new_entries, p_uniname);
1017 
1018 		ret = exfat_put_dentry_set(&new_es, sync);
1019 		if (ret)
1020 			goto put_old_es;
1021 
1022 		exfat_remove_entries(inode, &old_es, ES_IDX_FILE);
1023 		ei->dir = *p_dir;
1024 		ei->entry = newentry;
1025 	} else {
1026 		if (exfat_get_entry_type(epold) == TYPE_FILE) {
1027 			epold->dentry.file.attr |= cpu_to_le16(EXFAT_ATTR_ARCHIVE);
1028 			ei->attr |= EXFAT_ATTR_ARCHIVE;
1029 		}
1030 
1031 		exfat_remove_entries(inode, &old_es, ES_IDX_FIRST_FILENAME + 1);
1032 		exfat_init_ext_entry(&old_es, num_new_entries, p_uniname);
1033 	}
1034 	return exfat_put_dentry_set(&old_es, sync);
1035 
1036 put_old_es:
1037 	exfat_put_dentry_set(&old_es, false);
1038 	return ret;
1039 }
1040 
1041 static int exfat_move_file(struct inode *inode, struct exfat_chain *p_olddir,
1042 		int oldentry, struct exfat_chain *p_newdir,
1043 		struct exfat_uni_name *p_uniname, struct exfat_inode_info *ei)
1044 {
1045 	int ret, newentry, num_new_entries;
1046 	struct exfat_dentry *epmov, *epnew;
1047 	struct super_block *sb = inode->i_sb;
1048 	struct exfat_entry_set_cache mov_es, new_es;
1049 
1050 	num_new_entries = exfat_calc_num_entries(p_uniname);
1051 	if (num_new_entries < 0)
1052 		return num_new_entries;
1053 
1054 	ret = exfat_get_dentry_set(&mov_es, sb, p_olddir, oldentry,
1055 			ES_ALL_ENTRIES);
1056 	if (ret)
1057 		return -EIO;
1058 
1059 	newentry = exfat_find_empty_entry(inode, p_newdir, num_new_entries,
1060 			&new_es);
1061 	if (newentry < 0) {
1062 		ret = newentry; /* -EIO or -ENOSPC */
1063 		goto put_mov_es;
1064 	}
1065 
1066 	epmov = exfat_get_dentry_cached(&mov_es, ES_IDX_FILE);
1067 	epnew = exfat_get_dentry_cached(&new_es, ES_IDX_FILE);
1068 	*epnew = *epmov;
1069 	if (exfat_get_entry_type(epnew) == TYPE_FILE) {
1070 		epnew->dentry.file.attr |= cpu_to_le16(EXFAT_ATTR_ARCHIVE);
1071 		ei->attr |= EXFAT_ATTR_ARCHIVE;
1072 	}
1073 
1074 	epmov = exfat_get_dentry_cached(&mov_es, ES_IDX_STREAM);
1075 	epnew = exfat_get_dentry_cached(&new_es, ES_IDX_STREAM);
1076 	*epnew = *epmov;
1077 
1078 	exfat_init_ext_entry(&new_es, num_new_entries, p_uniname);
1079 	exfat_remove_entries(inode, &mov_es, ES_IDX_FILE);
1080 
1081 	exfat_chain_set(&ei->dir, p_newdir->dir, p_newdir->size,
1082 		p_newdir->flags);
1083 
1084 	ei->entry = newentry;
1085 
1086 	ret = exfat_put_dentry_set(&new_es, IS_DIRSYNC(inode));
1087 	if (ret)
1088 		goto put_mov_es;
1089 
1090 	return exfat_put_dentry_set(&mov_es, IS_DIRSYNC(inode));
1091 
1092 put_mov_es:
1093 	exfat_put_dentry_set(&mov_es, false);
1094 
1095 	return ret;
1096 }
1097 
1098 /* rename or move a old file into a new file */
1099 static int __exfat_rename(struct inode *old_parent_inode,
1100 		struct exfat_inode_info *ei, struct inode *new_parent_inode,
1101 		struct dentry *new_dentry)
1102 {
1103 	int ret;
1104 	int dentry;
1105 	struct exfat_chain olddir, newdir;
1106 	struct exfat_chain *p_dir = NULL;
1107 	struct exfat_uni_name uni_name;
1108 	struct exfat_dentry *ep;
1109 	struct super_block *sb = old_parent_inode->i_sb;
1110 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
1111 	const unsigned char *new_path = new_dentry->d_name.name;
1112 	struct inode *new_inode = new_dentry->d_inode;
1113 	struct exfat_inode_info *new_ei = NULL;
1114 	unsigned int new_entry_type = TYPE_UNUSED;
1115 	int new_entry = 0;
1116 	struct buffer_head *new_bh = NULL;
1117 
1118 	/* check the validity of pointer parameters */
1119 	if (new_path == NULL || strlen(new_path) == 0)
1120 		return -EINVAL;
1121 
1122 	if (ei->dir.dir == DIR_DELETED) {
1123 		exfat_err(sb, "abnormal access to deleted source dentry");
1124 		return -ENOENT;
1125 	}
1126 
1127 	exfat_chain_set(&olddir, EXFAT_I(old_parent_inode)->start_clu,
1128 		EXFAT_B_TO_CLU_ROUND_UP(i_size_read(old_parent_inode), sbi),
1129 		EXFAT_I(old_parent_inode)->flags);
1130 	dentry = ei->entry;
1131 
1132 	/* check whether new dir is existing directory and empty */
1133 	if (new_inode) {
1134 		ret = -EIO;
1135 		new_ei = EXFAT_I(new_inode);
1136 
1137 		if (new_ei->dir.dir == DIR_DELETED) {
1138 			exfat_err(sb, "abnormal access to deleted target dentry");
1139 			goto out;
1140 		}
1141 
1142 		p_dir = &(new_ei->dir);
1143 		new_entry = new_ei->entry;
1144 		ep = exfat_get_dentry(sb, p_dir, new_entry, &new_bh);
1145 		if (!ep)
1146 			goto out;
1147 
1148 		new_entry_type = exfat_get_entry_type(ep);
1149 		brelse(new_bh);
1150 
1151 		/* if new_inode exists, update ei */
1152 		if (new_entry_type == TYPE_DIR) {
1153 			struct exfat_chain new_clu;
1154 
1155 			new_clu.dir = new_ei->start_clu;
1156 			new_clu.size =
1157 				EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode),
1158 				sbi);
1159 			new_clu.flags = new_ei->flags;
1160 
1161 			ret = exfat_check_dir_empty(sb, &new_clu);
1162 			if (ret)
1163 				goto out;
1164 		}
1165 	}
1166 
1167 	/* check the validity of directory name in the given new pathname */
1168 	ret = exfat_resolve_path(new_parent_inode, new_path, &newdir,
1169 			&uni_name);
1170 	if (ret)
1171 		goto out;
1172 
1173 	exfat_set_volume_dirty(sb);
1174 
1175 	if (olddir.dir == newdir.dir)
1176 		ret = exfat_rename_file(new_parent_inode, &olddir, dentry,
1177 				&uni_name, ei);
1178 	else
1179 		ret = exfat_move_file(new_parent_inode, &olddir, dentry,
1180 				&newdir, &uni_name, ei);
1181 
1182 	if (!ret && new_inode) {
1183 		struct exfat_entry_set_cache es;
1184 
1185 		/* delete entries of new_dir */
1186 		ret = exfat_get_dentry_set(&es, sb, p_dir, new_entry,
1187 				ES_ALL_ENTRIES);
1188 		if (ret) {
1189 			ret = -EIO;
1190 			goto del_out;
1191 		}
1192 
1193 		exfat_remove_entries(new_inode, &es, ES_IDX_FILE);
1194 
1195 		ret = exfat_put_dentry_set(&es, IS_DIRSYNC(new_inode));
1196 		if (ret)
1197 			goto del_out;
1198 
1199 		/* Free the clusters if new_inode is a dir(as if exfat_rmdir) */
1200 		if (new_entry_type == TYPE_DIR &&
1201 		    new_ei->start_clu != EXFAT_EOF_CLUSTER) {
1202 			/* new_ei, new_clu_to_free */
1203 			struct exfat_chain new_clu_to_free;
1204 
1205 			exfat_chain_set(&new_clu_to_free, new_ei->start_clu,
1206 				EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode),
1207 				sbi), new_ei->flags);
1208 
1209 			if (exfat_free_cluster(new_inode, &new_clu_to_free)) {
1210 				/* just set I/O error only */
1211 				ret = -EIO;
1212 			}
1213 
1214 			i_size_write(new_inode, 0);
1215 			new_ei->valid_size = 0;
1216 			new_ei->start_clu = EXFAT_EOF_CLUSTER;
1217 			new_ei->flags = ALLOC_NO_FAT_CHAIN;
1218 		}
1219 del_out:
1220 		/* Update new_inode ei
1221 		 * Prevent syncing removed new_inode
1222 		 * (new_ei is already initialized above code ("if (new_inode)")
1223 		 */
1224 		new_ei->dir.dir = DIR_DELETED;
1225 	}
1226 out:
1227 	return ret;
1228 }
1229 
1230 static int exfat_rename(struct mnt_idmap *idmap,
1231 			struct inode *old_dir, struct dentry *old_dentry,
1232 			struct inode *new_dir, struct dentry *new_dentry,
1233 			unsigned int flags)
1234 {
1235 	struct inode *old_inode, *new_inode;
1236 	struct super_block *sb = old_dir->i_sb;
1237 	loff_t i_pos;
1238 	int err;
1239 	loff_t size = i_size_read(new_dir);
1240 
1241 	/*
1242 	 * The VFS already checks for existence, so for local filesystems
1243 	 * the RENAME_NOREPLACE implementation is equivalent to plain rename.
1244 	 * Don't support any other flags
1245 	 */
1246 	if (flags & ~RENAME_NOREPLACE)
1247 		return -EINVAL;
1248 
1249 	mutex_lock(&EXFAT_SB(sb)->s_lock);
1250 	old_inode = old_dentry->d_inode;
1251 	new_inode = new_dentry->d_inode;
1252 
1253 	err = __exfat_rename(old_dir, EXFAT_I(old_inode), new_dir, new_dentry);
1254 	if (err)
1255 		goto unlock;
1256 
1257 	inode_inc_iversion(new_dir);
1258 	simple_rename_timestamp(old_dir, old_dentry, new_dir, new_dentry);
1259 	EXFAT_I(new_dir)->i_crtime = current_time(new_dir);
1260 	exfat_truncate_inode_atime(new_dir);
1261 	if (IS_DIRSYNC(new_dir) && size != i_size_read(new_dir))
1262 		exfat_sync_inode(new_dir);
1263 	else
1264 		mark_inode_dirty(new_dir);
1265 
1266 	i_pos = ((loff_t)EXFAT_I(old_inode)->dir.dir << 32) |
1267 		(EXFAT_I(old_inode)->entry & 0xffffffff);
1268 	exfat_unhash_inode(old_inode);
1269 	exfat_hash_inode(old_inode, i_pos);
1270 	if (IS_DIRSYNC(new_dir))
1271 		exfat_sync_inode(old_inode);
1272 	else
1273 		mark_inode_dirty(old_inode);
1274 
1275 	if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
1276 		drop_nlink(old_dir);
1277 		if (!new_inode)
1278 			inc_nlink(new_dir);
1279 	}
1280 
1281 	inode_inc_iversion(old_dir);
1282 	if (new_dir != old_dir)
1283 		mark_inode_dirty(old_dir);
1284 
1285 	if (new_inode) {
1286 		exfat_unhash_inode(new_inode);
1287 
1288 		/* skip drop_nlink if new_inode already has been dropped */
1289 		if (new_inode->i_nlink) {
1290 			drop_nlink(new_inode);
1291 			if (S_ISDIR(new_inode->i_mode))
1292 				drop_nlink(new_inode);
1293 		} else {
1294 			exfat_warn(sb, "abnormal access to an inode dropped");
1295 			WARN_ON(new_inode->i_nlink == 0);
1296 		}
1297 		EXFAT_I(new_inode)->i_crtime = current_time(new_inode);
1298 	}
1299 
1300 unlock:
1301 	mutex_unlock(&EXFAT_SB(sb)->s_lock);
1302 	return err;
1303 }
1304 
1305 const struct inode_operations exfat_dir_inode_operations = {
1306 	.create		= exfat_create,
1307 	.lookup		= exfat_lookup,
1308 	.unlink		= exfat_unlink,
1309 	.mkdir		= exfat_mkdir,
1310 	.rmdir		= exfat_rmdir,
1311 	.rename		= exfat_rename,
1312 	.setattr	= exfat_setattr,
1313 	.getattr	= exfat_getattr,
1314 };
1315