xref: /linux/fs/exfat/namei.c (revision c4dde411bc366f568dbe33366253bbfea049e8ea)
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 inode *dir, const struct qstr *name,
35 			      struct dentry *dentry, unsigned int flags)
36 {
37 	if (flags & LOOKUP_RCU)
38 		return -ECHILD;
39 
40 	/*
41 	 * This is not negative dentry. Always valid.
42 	 *
43 	 * Note, rename() to existing directory entry will have ->d_inode, and
44 	 * will use existing name which isn't specified name by user.
45 	 *
46 	 * We may be able to drop this positive dentry here. But dropping
47 	 * positive dentry isn't good idea. So it's unsupported like
48 	 * rename("filename", "FILENAME") for now.
49 	 */
50 	if (d_really_is_positive(dentry))
51 		return 1;
52 
53 	/*
54 	 * Drop the negative dentry, in order to make sure to use the case
55 	 * sensitive name which is specified by user if this is for creation.
56 	 */
57 	if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
58 		return 0;
59 
60 	return inode_eq_iversion(dir, exfat_d_version(dentry));
61 }
62 
63 /* returns the length of a struct qstr, ignoring trailing dots if necessary */
64 static unsigned int exfat_striptail_len(unsigned int len, const char *name,
65 					bool keep_last_dots)
66 {
67 	if (!keep_last_dots) {
68 		while (len && name[len - 1] == '.')
69 			len--;
70 	}
71 	return len;
72 }
73 
74 /*
75  * Compute the hash for the exfat name corresponding to the dentry.  If the name
76  * is invalid, we leave the hash code unchanged so that the existing dentry can
77  * be used. The exfat fs routines will return ENOENT or EINVAL as appropriate.
78  */
79 static int exfat_d_hash(const struct dentry *dentry, struct qstr *qstr)
80 {
81 	struct super_block *sb = dentry->d_sb;
82 	struct nls_table *t = EXFAT_SB(sb)->nls_io;
83 	const unsigned char *name = qstr->name;
84 	unsigned int len = exfat_striptail_len(qstr->len, qstr->name,
85 			   EXFAT_SB(sb)->options.keep_last_dots);
86 	unsigned long hash = init_name_hash(dentry);
87 	int i, charlen;
88 	wchar_t c;
89 
90 	for (i = 0; i < len; i += charlen) {
91 		charlen = t->char2uni(&name[i], len - i, &c);
92 		if (charlen < 0)
93 			return charlen;
94 		hash = partial_name_hash(exfat_toupper(sb, c), hash);
95 	}
96 
97 	qstr->hash = end_name_hash(hash);
98 	return 0;
99 }
100 
101 static int exfat_d_cmp(const struct dentry *dentry, unsigned int len,
102 		const char *str, const struct qstr *name)
103 {
104 	struct super_block *sb = dentry->d_sb;
105 	struct nls_table *t = EXFAT_SB(sb)->nls_io;
106 	unsigned int alen = exfat_striptail_len(name->len, name->name,
107 				EXFAT_SB(sb)->options.keep_last_dots);
108 	unsigned int blen = exfat_striptail_len(len, str,
109 				EXFAT_SB(sb)->options.keep_last_dots);
110 	wchar_t c1, c2;
111 	int charlen, i;
112 
113 	if (alen != blen)
114 		return 1;
115 
116 	for (i = 0; i < len; i += charlen) {
117 		charlen = t->char2uni(&name->name[i], alen - i, &c1);
118 		if (charlen < 0)
119 			return 1;
120 		if (charlen != t->char2uni(&str[i], blen - i, &c2))
121 			return 1;
122 
123 		if (exfat_toupper(sb, c1) != exfat_toupper(sb, c2))
124 			return 1;
125 	}
126 
127 	return 0;
128 }
129 
130 const struct dentry_operations exfat_dentry_ops = {
131 	.d_revalidate	= exfat_d_revalidate,
132 	.d_hash		= exfat_d_hash,
133 	.d_compare	= exfat_d_cmp,
134 };
135 
136 static int exfat_utf8_d_hash(const struct dentry *dentry, struct qstr *qstr)
137 {
138 	struct super_block *sb = dentry->d_sb;
139 	const unsigned char *name = qstr->name;
140 	unsigned int len = exfat_striptail_len(qstr->len, qstr->name,
141 			       EXFAT_SB(sb)->options.keep_last_dots);
142 	unsigned long hash = init_name_hash(dentry);
143 	int i, charlen;
144 	unicode_t u;
145 
146 	for (i = 0; i < len; i += charlen) {
147 		charlen = utf8_to_utf32(&name[i], len - i, &u);
148 		if (charlen < 0)
149 			return charlen;
150 
151 		/*
152 		 * exfat_toupper() works only for code points up to the U+FFFF.
153 		 */
154 		hash = partial_name_hash(u <= 0xFFFF ? exfat_toupper(sb, u) : u,
155 					 hash);
156 	}
157 
158 	qstr->hash = end_name_hash(hash);
159 	return 0;
160 }
161 
162 static int exfat_utf8_d_cmp(const struct dentry *dentry, unsigned int len,
163 		const char *str, const struct qstr *name)
164 {
165 	struct super_block *sb = dentry->d_sb;
166 	unsigned int alen = exfat_striptail_len(name->len, name->name,
167 				EXFAT_SB(sb)->options.keep_last_dots);
168 	unsigned int blen = exfat_striptail_len(len, str,
169 				EXFAT_SB(sb)->options.keep_last_dots);
170 
171 	unicode_t u_a, u_b;
172 	int charlen, i;
173 
174 	if (alen != blen)
175 		return 1;
176 
177 	for (i = 0; i < alen; i += charlen) {
178 		charlen = utf8_to_utf32(&name->name[i], alen - i, &u_a);
179 		if (charlen < 0)
180 			return 1;
181 		if (charlen != utf8_to_utf32(&str[i], blen - i, &u_b))
182 			return 1;
183 
184 		if (u_a <= 0xFFFF && u_b <= 0xFFFF) {
185 			if (exfat_toupper(sb, u_a) != exfat_toupper(sb, u_b))
186 				return 1;
187 		} else {
188 			if (u_a != u_b)
189 				return 1;
190 		}
191 	}
192 
193 	return 0;
194 }
195 
196 const struct dentry_operations exfat_utf8_dentry_ops = {
197 	.d_revalidate	= exfat_d_revalidate,
198 	.d_hash		= exfat_utf8_d_hash,
199 	.d_compare	= exfat_utf8_d_cmp,
200 };
201 
202 /* search EMPTY CONTINUOUS "num_entries" entries */
203 static int exfat_search_empty_slot(struct super_block *sb,
204 		struct exfat_hint_femp *hint_femp, struct exfat_chain *p_dir,
205 		int num_entries, struct exfat_entry_set_cache *es)
206 {
207 	int i, dentry, ret;
208 	int dentries_per_clu;
209 	struct exfat_chain clu;
210 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
211 	int total_entries = EXFAT_CLU_TO_DEN(p_dir->size, sbi);
212 
213 	dentries_per_clu = sbi->dentries_per_clu;
214 
215 	if (hint_femp->eidx != EXFAT_HINT_NONE) {
216 		dentry = hint_femp->eidx;
217 
218 		/*
219 		 * If hint_femp->count is enough, it is needed to check if
220 		 * there are actual empty entries.
221 		 * Otherwise, and if "dentry + hint_famp->count" is also equal
222 		 * to "p_dir->size * dentries_per_clu", it means ENOSPC.
223 		 */
224 		if (dentry + hint_femp->count == total_entries &&
225 		    num_entries > hint_femp->count)
226 			return -ENOSPC;
227 
228 		hint_femp->eidx = EXFAT_HINT_NONE;
229 		exfat_chain_dup(&clu, &hint_femp->cur);
230 	} else {
231 		exfat_chain_dup(&clu, p_dir);
232 		dentry = 0;
233 	}
234 
235 	while (dentry + num_entries <= total_entries &&
236 	       clu.dir != EXFAT_EOF_CLUSTER) {
237 		i = dentry & (dentries_per_clu - 1);
238 
239 		ret = exfat_get_empty_dentry_set(es, sb, &clu, i, num_entries);
240 		if (ret < 0)
241 			return ret;
242 		else if (ret == 0)
243 			return dentry;
244 
245 		dentry += ret;
246 		i += ret;
247 
248 		while (i >= dentries_per_clu) {
249 			if (exfat_chain_advance(sb, &clu, 1))
250 				return -EIO;
251 
252 			i -= dentries_per_clu;
253 		}
254 	}
255 
256 	hint_femp->eidx = dentry;
257 	hint_femp->count = 0;
258 	if (dentry == total_entries || clu.dir == EXFAT_EOF_CLUSTER)
259 		exfat_chain_set(&hint_femp->cur, EXFAT_EOF_CLUSTER, 0,
260 				clu.flags);
261 	else
262 		hint_femp->cur = clu;
263 
264 	return -ENOSPC;
265 }
266 
267 static int exfat_check_max_dentries(struct inode *inode)
268 {
269 	if (EXFAT_B_TO_DEN(i_size_read(inode)) >= MAX_EXFAT_DENTRIES) {
270 		/*
271 		 * exFAT spec allows a dir to grow up to 8388608(256MB)
272 		 * dentries
273 		 */
274 		return -ENOSPC;
275 	}
276 	return 0;
277 }
278 
279 /*
280  * Find an empty directory entry set.
281  *
282  * If there isn't any empty slot, expand cluster chain.
283  *
284  * in:
285  *   inode: inode of the parent directory
286  *   num_entries: specifies how many dentries in the empty directory entry set
287  *
288  * out:
289  *   p_dir: the cluster where the empty directory entry set is located
290  *   es: The found empty directory entry set
291  *
292  * return:
293  *   the directory entry index in p_dir is returned on succeeds
294  *   -error code is returned on failure
295  */
296 int exfat_find_empty_entry(struct inode *inode,
297 		struct exfat_chain *p_dir, int num_entries,
298 		struct exfat_entry_set_cache *es)
299 {
300 	int dentry, ret;
301 	unsigned int last_clu;
302 	loff_t size = 0;
303 	struct exfat_chain clu;
304 	struct super_block *sb = inode->i_sb;
305 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
306 	struct exfat_inode_info *ei = EXFAT_I(inode);
307 	struct exfat_hint_femp hint_femp;
308 
309 	hint_femp.eidx = EXFAT_HINT_NONE;
310 
311 	if (ei->hint_femp.eidx != EXFAT_HINT_NONE) {
312 		hint_femp = ei->hint_femp;
313 		ei->hint_femp.eidx = EXFAT_HINT_NONE;
314 	}
315 
316 	exfat_chain_set(p_dir, ei->start_clu,
317 			EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags);
318 
319 	while ((dentry = exfat_search_empty_slot(sb, &hint_femp, p_dir,
320 					num_entries, es)) < 0) {
321 		if (dentry != -ENOSPC)
322 			return dentry;
323 
324 		if (exfat_check_max_dentries(inode))
325 			return -ENOSPC;
326 
327 		/*
328 		 * Allocate new cluster to this directory
329 		 */
330 		if (ei->start_clu != EXFAT_EOF_CLUSTER) {
331 			/* we trust p_dir->size regardless of FAT type */
332 			if (exfat_find_last_cluster(sb, p_dir, &last_clu))
333 				return -EIO;
334 
335 			exfat_chain_set(&clu, last_clu + 1, 0, p_dir->flags);
336 		} else {
337 			/* This directory is empty */
338 			exfat_chain_set(&clu, EXFAT_EOF_CLUSTER, 0,
339 					ALLOC_NO_FAT_CHAIN);
340 		}
341 
342 		/* allocate a cluster */
343 		ret = exfat_alloc_cluster(inode, 1, &clu, IS_DIRSYNC(inode));
344 		if (ret)
345 			return ret;
346 
347 		if (exfat_zeroed_cluster(inode, clu.dir))
348 			return -EIO;
349 
350 		if (ei->start_clu == EXFAT_EOF_CLUSTER) {
351 			ei->start_clu = clu.dir;
352 			p_dir->dir = clu.dir;
353 			hint_femp.eidx = 0;
354 		}
355 
356 		/* append to the FAT chain */
357 		if (clu.flags != p_dir->flags) {
358 			/* no-fat-chain bit is disabled,
359 			 * so fat-chain should be synced with alloc-bitmap
360 			 */
361 			if (exfat_chain_cont_cluster(sb, p_dir->dir, p_dir->size))
362 				return -EIO;
363 			p_dir->flags = ALLOC_FAT_CHAIN;
364 			hint_femp.cur.flags = ALLOC_FAT_CHAIN;
365 		}
366 
367 		if (clu.flags == ALLOC_FAT_CHAIN)
368 			if (exfat_ent_set(sb, last_clu, clu.dir))
369 				return -EIO;
370 
371 		if (hint_femp.cur.dir == EXFAT_EOF_CLUSTER)
372 			exfat_chain_set(&hint_femp.cur, clu.dir, 0, clu.flags);
373 
374 		hint_femp.count += sbi->dentries_per_clu;
375 
376 		hint_femp.cur.size++;
377 		p_dir->size++;
378 		size = EXFAT_CLU_TO_B(p_dir->size, sbi);
379 
380 		/* directory inode should be updated in here */
381 		i_size_write(inode, size);
382 		ei->valid_size += sbi->cluster_size;
383 		ei->flags = p_dir->flags;
384 		inode->i_blocks += sbi->cluster_size >> 9;
385 	}
386 
387 	p_dir->dir = exfat_sector_to_cluster(sbi, es->bh[0]->b_blocknr);
388 	p_dir->size -= dentry / sbi->dentries_per_clu;
389 
390 	return dentry & (sbi->dentries_per_clu - 1);
391 }
392 
393 /*
394  * Name Resolution Functions :
395  * Zero if it was successful; otherwise nonzero.
396  */
397 static int __exfat_resolve_path(struct inode *inode, const unsigned char *path,
398 		struct exfat_uni_name *p_uniname, int lookup)
399 {
400 	int namelen;
401 	int lossy = NLS_NAME_NO_LOSSY;
402 	struct super_block *sb = inode->i_sb;
403 	int pathlen = strlen(path);
404 
405 	/*
406 	 * get the length of the pathname excluding
407 	 * trailing periods, if any.
408 	 */
409 	namelen = exfat_striptail_len(pathlen, path, false);
410 	if (EXFAT_SB(sb)->options.keep_last_dots) {
411 		/*
412 		 * Do not allow the creation of files with names
413 		 * ending with period(s).
414 		 */
415 		if (!lookup && (namelen < pathlen))
416 			return -EINVAL;
417 		namelen = pathlen;
418 	}
419 	if (!namelen)
420 		return -ENOENT;
421 	if (pathlen > (MAX_NAME_LENGTH * MAX_CHARSET_SIZE))
422 		return -ENAMETOOLONG;
423 
424 	/*
425 	 * strip all leading spaces :
426 	 * "MS windows 7" supports leading spaces.
427 	 * So we should skip this preprocessing for compatibility.
428 	 */
429 
430 	/* file name conversion :
431 	 * If lookup case, we allow bad-name for compatibility.
432 	 */
433 	namelen = exfat_nls_to_utf16(sb, path, namelen, p_uniname,
434 			&lossy);
435 	if (namelen < 0)
436 		return namelen; /* return error value */
437 
438 	if ((lossy && !lookup) || !namelen)
439 		return -EINVAL;
440 
441 	return 0;
442 }
443 
444 static inline int exfat_resolve_path(struct inode *inode,
445 		const unsigned char *path, struct exfat_uni_name *uni)
446 {
447 	return __exfat_resolve_path(inode, path, uni, 0);
448 }
449 
450 static inline int exfat_resolve_path_for_lookup(struct inode *inode,
451 		const unsigned char *path, struct exfat_uni_name *uni)
452 {
453 	return __exfat_resolve_path(inode, path, uni, 1);
454 }
455 
456 static inline loff_t exfat_make_i_pos(struct exfat_dir_entry *info)
457 {
458 	return ((loff_t) info->dir.dir << 32) | (info->entry & 0xffffffff);
459 }
460 
461 static int exfat_add_entry(struct inode *inode, const char *path,
462 		unsigned int type, struct exfat_dir_entry *info)
463 {
464 	int ret, dentry, num_entries;
465 	struct super_block *sb = inode->i_sb;
466 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
467 	struct exfat_uni_name uniname;
468 	struct exfat_chain clu;
469 	struct timespec64 ts = current_time(inode);
470 	struct exfat_entry_set_cache es;
471 	int clu_size = 0;
472 	unsigned int start_clu = EXFAT_FREE_CLUSTER;
473 
474 	ret = exfat_resolve_path(inode, path, &uniname);
475 	if (ret)
476 		goto out;
477 
478 	num_entries = exfat_calc_num_entries(&uniname);
479 	if (num_entries < 0) {
480 		ret = num_entries;
481 		goto out;
482 	}
483 
484 	/* exfat_find_empty_entry must be called before alloc_cluster() */
485 	dentry = exfat_find_empty_entry(inode, &info->dir, num_entries, &es);
486 	if (dentry < 0) {
487 		ret = dentry; /* -EIO or -ENOSPC */
488 		goto out;
489 	}
490 
491 	if (type == TYPE_DIR && !sbi->options.zero_size_dir) {
492 		ret = exfat_alloc_new_dir(inode, &clu);
493 		if (ret) {
494 			exfat_put_dentry_set(&es, false);
495 			goto out;
496 		}
497 		start_clu = clu.dir;
498 		clu_size = sbi->cluster_size;
499 	}
500 
501 	/* update the directory entry */
502 	/* fill the dos name directory entry information of the created file.
503 	 * the first cluster is not determined yet. (0)
504 	 */
505 	exfat_init_dir_entry(&es, type, start_clu, clu_size, &ts);
506 	exfat_init_ext_entry(&es, num_entries, &uniname);
507 
508 	ret = exfat_put_dentry_set(&es, IS_DIRSYNC(inode));
509 	if (ret)
510 		goto out;
511 
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_dir_entry info;
545 	loff_t i_pos;
546 	int err;
547 	loff_t size = i_size_read(dir);
548 
549 	if (unlikely(exfat_forced_shutdown(sb)))
550 		return -EIO;
551 
552 	mutex_lock(&EXFAT_SB(sb)->s_lock);
553 	exfat_set_volume_dirty(sb);
554 	err = exfat_add_entry(dir, dentry->d_name.name, TYPE_FILE, &info);
555 	if (err)
556 		goto unlock;
557 
558 	inode_inc_iversion(dir);
559 	inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
560 	if (IS_DIRSYNC(dir) && size != i_size_read(dir))
561 		exfat_sync_inode(dir);
562 	else
563 		mark_inode_dirty(dir);
564 
565 	i_pos = exfat_make_i_pos(&info);
566 	inode = exfat_build_inode(sb, &info, i_pos);
567 	err = PTR_ERR_OR_ZERO(inode);
568 	if (err)
569 		goto unlock;
570 
571 	inode_inc_iversion(inode);
572 	EXFAT_I(inode)->i_crtime = simple_inode_init_ts(inode);
573 	exfat_truncate_inode_atime(inode);
574 
575 	/* timestamp is already written, so mark_inode_dirty() is unneeded. */
576 
577 	d_instantiate(dentry, inode);
578 unlock:
579 	mutex_unlock(&EXFAT_SB(sb)->s_lock);
580 	return err;
581 }
582 
583 /* lookup a file */
584 static int exfat_find(struct inode *dir, const struct qstr *qname,
585 		struct exfat_dir_entry *info)
586 {
587 	int ret, dentry, count;
588 	struct exfat_chain cdir;
589 	struct exfat_uni_name uni_name;
590 	struct super_block *sb = dir->i_sb;
591 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
592 	struct exfat_inode_info *ei = EXFAT_I(dir);
593 	struct exfat_dentry *ep, *ep2;
594 	struct exfat_entry_set_cache es;
595 	/* for optimized dir & entry to prevent long traverse of cluster chain */
596 	struct exfat_hint hint_opt;
597 
598 	if (qname->len == 0)
599 		return -ENOENT;
600 
601 	/* check the validity of directory name in the given pathname */
602 	ret = exfat_resolve_path_for_lookup(dir, qname->name, &uni_name);
603 	if (ret)
604 		return ret;
605 
606 	exfat_chain_set(&cdir, ei->start_clu,
607 		EXFAT_B_TO_CLU(i_size_read(dir), sbi), ei->flags);
608 
609 	/* check the validation of hint_stat and initialize it if required */
610 	if (ei->version != (inode_peek_iversion_raw(dir) & 0xffffffff)) {
611 		ei->hint_stat.clu = cdir.dir;
612 		ei->hint_stat.eidx = 0;
613 		ei->version = (inode_peek_iversion_raw(dir) & 0xffffffff);
614 		ei->hint_femp.eidx = EXFAT_HINT_NONE;
615 	}
616 
617 	/* search the file name for directories */
618 	dentry = exfat_find_dir_entry(sb, ei, &cdir, &uni_name, &hint_opt);
619 	if (dentry < 0)
620 		return dentry; /* -error value */
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 
628 	info->dir = cdir;
629 	info->entry = dentry;
630 	info->num_subdirs = 0;
631 
632 	if (exfat_get_dentry_set(&es, sb, &cdir, dentry, ES_2_ENTRIES))
633 		return -EIO;
634 	ep = exfat_get_dentry_cached(&es, ES_IDX_FILE);
635 	ep2 = exfat_get_dentry_cached(&es, ES_IDX_STREAM);
636 
637 	info->type = exfat_get_entry_type(ep);
638 	info->attr = le16_to_cpu(ep->dentry.file.attr);
639 	info->valid_size = le64_to_cpu(ep2->dentry.stream.valid_size);
640 	info->size = le64_to_cpu(ep2->dentry.stream.size);
641 
642 	info->start_clu = le32_to_cpu(ep2->dentry.stream.start_clu);
643 	if (!is_valid_cluster(sbi, info->start_clu) && info->size) {
644 		exfat_warn(sb, "start_clu is invalid cluster(0x%x)",
645 				info->start_clu);
646 		info->size = 0;
647 		info->valid_size = 0;
648 	}
649 
650 	if (info->valid_size > info->size) {
651 		exfat_warn(sb, "valid_size(%lld) is greater than size(%lld)",
652 				info->valid_size, info->size);
653 		info->valid_size = info->size;
654 	}
655 
656 	if (info->size == 0) {
657 		info->flags = ALLOC_NO_FAT_CHAIN;
658 		info->start_clu = EXFAT_EOF_CLUSTER;
659 	} else
660 		info->flags = ep2->dentry.stream.flags;
661 
662 	exfat_get_entry_time(sbi, &info->crtime,
663 			     ep->dentry.file.create_tz,
664 			     ep->dentry.file.create_time,
665 			     ep->dentry.file.create_date,
666 			     ep->dentry.file.create_time_cs);
667 	exfat_get_entry_time(sbi, &info->mtime,
668 			     ep->dentry.file.modify_tz,
669 			     ep->dentry.file.modify_time,
670 			     ep->dentry.file.modify_date,
671 			     ep->dentry.file.modify_time_cs);
672 	exfat_get_entry_time(sbi, &info->atime,
673 			     ep->dentry.file.access_tz,
674 			     ep->dentry.file.access_time,
675 			     ep->dentry.file.access_date,
676 			     0);
677 	exfat_put_dentry_set(&es, false);
678 
679 	if (info->valid_size < 0) {
680 		exfat_fs_error(sb, "data valid size is invalid(%lld)", info->valid_size);
681 		return -EIO;
682 	}
683 
684 	if (unlikely(EXFAT_B_TO_CLU_ROUND_UP(info->size, sbi) > sbi->used_clusters)) {
685 		exfat_fs_error(sb, "data size is invalid(%lld)", info->size);
686 		return -EIO;
687 	}
688 
689 	if (ei->start_clu == EXFAT_FREE_CLUSTER) {
690 		exfat_fs_error(sb,
691 			       "non-zero size file starts with zero cluster (size : %llu, p_dir : %u, entry : 0x%08x)",
692 			       i_size_read(dir), ei->dir.dir, ei->entry);
693 		return -EIO;
694 	}
695 
696 	if (info->type == TYPE_DIR) {
697 		exfat_chain_set(&cdir, info->start_clu,
698 				EXFAT_B_TO_CLU(info->size, sbi), info->flags);
699 		count = exfat_count_dir_entries(sb, &cdir);
700 		if (count < 0)
701 			return -EIO;
702 
703 		info->num_subdirs = count + EXFAT_MIN_SUBDIR;
704 	}
705 	return 0;
706 }
707 
708 static int exfat_d_anon_disconn(struct dentry *dentry)
709 {
710 	return IS_ROOT(dentry) && (dentry->d_flags & DCACHE_DISCONNECTED);
711 }
712 
713 static struct dentry *exfat_lookup(struct inode *dir, struct dentry *dentry,
714 		unsigned int flags)
715 {
716 	struct super_block *sb = dir->i_sb;
717 	struct inode *inode;
718 	struct dentry *alias;
719 	struct exfat_dir_entry info;
720 	int err;
721 	loff_t i_pos;
722 	mode_t i_mode;
723 
724 	mutex_lock(&EXFAT_SB(sb)->s_lock);
725 	err = exfat_find(dir, &dentry->d_name, &info);
726 	if (err) {
727 		if (err == -ENOENT) {
728 			inode = NULL;
729 			goto out;
730 		}
731 		goto unlock;
732 	}
733 
734 	i_pos = exfat_make_i_pos(&info);
735 	inode = exfat_build_inode(sb, &info, i_pos);
736 	err = PTR_ERR_OR_ZERO(inode);
737 	if (err)
738 		goto unlock;
739 
740 	i_mode = inode->i_mode;
741 	alias = d_find_alias(inode);
742 
743 	/*
744 	 * Checking "alias->d_parent == dentry->d_parent" to make sure
745 	 * FS is not corrupted (especially double linked dir).
746 	 */
747 	if (alias && alias->d_parent == dentry->d_parent &&
748 			!exfat_d_anon_disconn(alias)) {
749 
750 		/*
751 		 * Unhashed alias is able to exist because of revalidate()
752 		 * called by lookup_fast. You can easily make this status
753 		 * by calling create and lookup concurrently
754 		 * In such case, we reuse an alias instead of new dentry
755 		 */
756 		if (d_unhashed(alias)) {
757 			WARN_ON(alias->d_name.hash_len !=
758 				dentry->d_name.hash_len);
759 			exfat_info(sb, "rehashed a dentry(%p) in read lookup",
760 				   alias);
761 			d_drop(dentry);
762 			d_rehash(alias);
763 		} else if (!S_ISDIR(i_mode)) {
764 			/*
765 			 * This inode has non anonymous-DCACHE_DISCONNECTED
766 			 * dentry. This means, the user did ->lookup() by an
767 			 * another name (longname vs 8.3 alias of it) in past.
768 			 *
769 			 * Switch to new one for reason of locality if possible.
770 			 */
771 			d_move(alias, dentry);
772 		}
773 		iput(inode);
774 		mutex_unlock(&EXFAT_SB(sb)->s_lock);
775 		return alias;
776 	}
777 	dput(alias);
778 out:
779 	mutex_unlock(&EXFAT_SB(sb)->s_lock);
780 	if (!inode)
781 		exfat_d_version_set(dentry, inode_query_iversion(dir));
782 
783 	return d_splice_alias(inode, dentry);
784 unlock:
785 	mutex_unlock(&EXFAT_SB(sb)->s_lock);
786 	return ERR_PTR(err);
787 }
788 
789 /* remove an entry, BUT don't truncate */
790 static int exfat_unlink(struct inode *dir, struct dentry *dentry)
791 {
792 	struct super_block *sb = dir->i_sb;
793 	struct inode *inode = dentry->d_inode;
794 	struct exfat_inode_info *ei = EXFAT_I(inode);
795 	struct exfat_entry_set_cache es;
796 	int err = 0;
797 
798 	if (unlikely(exfat_forced_shutdown(sb)))
799 		return -EIO;
800 
801 	mutex_lock(&EXFAT_SB(sb)->s_lock);
802 	if (ei->dir.dir == DIR_DELETED) {
803 		exfat_err(sb, "abnormal access to deleted dentry");
804 		err = -ENOENT;
805 		goto unlock;
806 	}
807 
808 	err = exfat_get_dentry_set_by_ei(&es, sb, ei);
809 	if (err) {
810 		err = -EIO;
811 		goto unlock;
812 	}
813 
814 	exfat_set_volume_dirty(sb);
815 
816 	/* update the directory entry */
817 	exfat_remove_entries(inode, &es, ES_IDX_FILE);
818 
819 	err = exfat_put_dentry_set(&es, IS_DIRSYNC(inode));
820 	if (err)
821 		goto unlock;
822 
823 	/* This doesn't modify ei */
824 	ei->dir.dir = DIR_DELETED;
825 
826 	inode_inc_iversion(dir);
827 	simple_inode_init_ts(dir);
828 	exfat_truncate_inode_atime(dir);
829 	mark_inode_dirty(dir);
830 
831 	clear_nlink(inode);
832 	simple_inode_init_ts(inode);
833 	exfat_truncate_inode_atime(inode);
834 	exfat_unhash_inode(inode);
835 	exfat_d_version_set(dentry, inode_query_iversion(dir));
836 unlock:
837 	mutex_unlock(&EXFAT_SB(sb)->s_lock);
838 	return err;
839 }
840 
841 static struct dentry *exfat_mkdir(struct mnt_idmap *idmap, struct inode *dir,
842 				  struct dentry *dentry, umode_t mode)
843 {
844 	struct super_block *sb = dir->i_sb;
845 	struct inode *inode;
846 	struct exfat_dir_entry info;
847 	loff_t i_pos;
848 	int err;
849 	loff_t size = i_size_read(dir);
850 
851 	if (unlikely(exfat_forced_shutdown(sb)))
852 		return ERR_PTR(-EIO);
853 
854 	mutex_lock(&EXFAT_SB(sb)->s_lock);
855 	exfat_set_volume_dirty(sb);
856 	err = exfat_add_entry(dir, dentry->d_name.name, TYPE_DIR, &info);
857 	if (err)
858 		goto unlock;
859 
860 	inode_inc_iversion(dir);
861 	inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
862 	if (IS_DIRSYNC(dir) && size != i_size_read(dir))
863 		exfat_sync_inode(dir);
864 	else
865 		mark_inode_dirty(dir);
866 	inc_nlink(dir);
867 
868 	i_pos = exfat_make_i_pos(&info);
869 	inode = exfat_build_inode(sb, &info, i_pos);
870 	if (IS_ERR(inode)) {
871 		err = PTR_ERR(inode);
872 		goto unlock;
873 	}
874 
875 	inode_inc_iversion(inode);
876 	EXFAT_I(inode)->i_crtime = simple_inode_init_ts(inode);
877 	exfat_truncate_inode_atime(inode);
878 	/* timestamp is already written, so mark_inode_dirty() is unneeded. */
879 
880 	d_instantiate(dentry, inode);
881 
882 unlock:
883 	mutex_unlock(&EXFAT_SB(sb)->s_lock);
884 	return err ? ERR_PTR(err) : NULL;
885 }
886 
887 static int exfat_check_dir_empty(struct super_block *sb,
888 		struct exfat_chain *p_dir)
889 {
890 	int i, dentries_per_clu;
891 	unsigned int type;
892 	unsigned int clu_count = 0;
893 	struct exfat_chain clu;
894 	struct exfat_dentry *ep;
895 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
896 	struct buffer_head *bh;
897 
898 	dentries_per_clu = sbi->dentries_per_clu;
899 
900 	if (p_dir->dir == EXFAT_EOF_CLUSTER)
901 		return 0;
902 
903 	exfat_chain_dup(&clu, p_dir);
904 
905 	while (clu.dir != EXFAT_EOF_CLUSTER) {
906 		for (i = 0; i < dentries_per_clu; i++) {
907 			ep = exfat_get_dentry(sb, &clu, i, &bh);
908 			if (!ep)
909 				return -EIO;
910 			type = exfat_get_entry_type(ep);
911 			brelse(bh);
912 			if (type == TYPE_UNUSED)
913 				return 0;
914 
915 			if (type != TYPE_FILE && type != TYPE_DIR)
916 				continue;
917 
918 			return -ENOTEMPTY;
919 		}
920 
921 		if (exfat_chain_advance(sb, &clu, 1))
922 			return -EIO;
923 
924 		/* break if the cluster chain includes a loop */
925 		if (unlikely(++clu_count > EXFAT_DATA_CLUSTER_COUNT(sbi)))
926 			break;
927 	}
928 
929 	return 0;
930 }
931 
932 static int exfat_rmdir(struct inode *dir, struct dentry *dentry)
933 {
934 	struct inode *inode = dentry->d_inode;
935 	struct exfat_chain clu_to_free;
936 	struct super_block *sb = inode->i_sb;
937 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
938 	struct exfat_inode_info *ei = EXFAT_I(inode);
939 	struct exfat_entry_set_cache es;
940 	int err;
941 
942 	if (unlikely(exfat_forced_shutdown(sb)))
943 		return -EIO;
944 
945 	mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
946 
947 	if (ei->dir.dir == DIR_DELETED) {
948 		exfat_err(sb, "abnormal access to deleted dentry");
949 		err = -ENOENT;
950 		goto unlock;
951 	}
952 
953 	exfat_chain_set(&clu_to_free, ei->start_clu,
954 		EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi), ei->flags);
955 
956 	err = exfat_check_dir_empty(sb, &clu_to_free);
957 	if (err) {
958 		if (err == -EIO)
959 			exfat_err(sb, "failed to exfat_check_dir_empty : err(%d)",
960 				  err);
961 		goto unlock;
962 	}
963 
964 	err = exfat_get_dentry_set_by_ei(&es, sb, ei);
965 	if (err) {
966 		err = -EIO;
967 		goto unlock;
968 	}
969 
970 	exfat_set_volume_dirty(sb);
971 
972 	exfat_remove_entries(inode, &es, ES_IDX_FILE);
973 
974 	err = exfat_put_dentry_set(&es, IS_DIRSYNC(dir));
975 	if (err)
976 		goto unlock;
977 
978 	ei->dir.dir = DIR_DELETED;
979 
980 	inode_inc_iversion(dir);
981 	simple_inode_init_ts(dir);
982 	exfat_truncate_inode_atime(dir);
983 	if (IS_DIRSYNC(dir))
984 		exfat_sync_inode(dir);
985 	else
986 		mark_inode_dirty(dir);
987 	drop_nlink(dir);
988 
989 	clear_nlink(inode);
990 	simple_inode_init_ts(inode);
991 	exfat_truncate_inode_atime(inode);
992 	exfat_unhash_inode(inode);
993 	exfat_d_version_set(dentry, inode_query_iversion(dir));
994 unlock:
995 	mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
996 	return err;
997 }
998 
999 static int exfat_rename_file(struct inode *parent_inode,
1000 		struct exfat_uni_name *p_uniname, struct exfat_inode_info *ei)
1001 {
1002 	int ret, num_new_entries;
1003 	struct exfat_dentry *epold, *epnew;
1004 	struct super_block *sb = parent_inode->i_sb;
1005 	struct exfat_entry_set_cache old_es, new_es;
1006 	int sync = IS_DIRSYNC(parent_inode);
1007 
1008 	if (unlikely(exfat_forced_shutdown(sb)))
1009 		return -EIO;
1010 
1011 	num_new_entries = exfat_calc_num_entries(p_uniname);
1012 	if (num_new_entries < 0)
1013 		return num_new_entries;
1014 
1015 	ret = exfat_get_dentry_set_by_ei(&old_es, sb, ei);
1016 	if (ret) {
1017 		ret = -EIO;
1018 		return ret;
1019 	}
1020 
1021 	epold = exfat_get_dentry_cached(&old_es, ES_IDX_FILE);
1022 
1023 	if (old_es.num_entries < num_new_entries) {
1024 		int newentry;
1025 		struct exfat_chain dir;
1026 
1027 		newentry = exfat_find_empty_entry(parent_inode, &dir,
1028 				num_new_entries, &new_es);
1029 		if (newentry < 0) {
1030 			ret = newentry; /* -EIO or -ENOSPC */
1031 			goto put_old_es;
1032 		}
1033 
1034 		epnew = exfat_get_dentry_cached(&new_es, ES_IDX_FILE);
1035 		*epnew = *epold;
1036 		if (exfat_get_entry_type(epnew) == TYPE_FILE) {
1037 			epnew->dentry.file.attr |= cpu_to_le16(EXFAT_ATTR_ARCHIVE);
1038 			ei->attr |= EXFAT_ATTR_ARCHIVE;
1039 		}
1040 
1041 		epold = exfat_get_dentry_cached(&old_es, ES_IDX_STREAM);
1042 		epnew = exfat_get_dentry_cached(&new_es, ES_IDX_STREAM);
1043 		*epnew = *epold;
1044 
1045 		exfat_init_ext_entry(&new_es, num_new_entries, p_uniname);
1046 
1047 		ret = exfat_put_dentry_set(&new_es, sync);
1048 		if (ret)
1049 			goto put_old_es;
1050 
1051 		exfat_remove_entries(parent_inode, &old_es, ES_IDX_FILE);
1052 		ei->dir = dir;
1053 		ei->entry = newentry;
1054 	} else {
1055 		if (exfat_get_entry_type(epold) == TYPE_FILE) {
1056 			epold->dentry.file.attr |= cpu_to_le16(EXFAT_ATTR_ARCHIVE);
1057 			ei->attr |= EXFAT_ATTR_ARCHIVE;
1058 		}
1059 
1060 		exfat_remove_entries(parent_inode, &old_es, ES_IDX_FIRST_FILENAME + 1);
1061 		exfat_init_ext_entry(&old_es, num_new_entries, p_uniname);
1062 	}
1063 	return exfat_put_dentry_set(&old_es, sync);
1064 
1065 put_old_es:
1066 	exfat_put_dentry_set(&old_es, false);
1067 	return ret;
1068 }
1069 
1070 static int exfat_move_file(struct inode *parent_inode,
1071 		struct exfat_uni_name *p_uniname, struct exfat_inode_info *ei)
1072 {
1073 	int ret, newentry, num_new_entries;
1074 	struct exfat_dentry *epmov, *epnew;
1075 	struct exfat_entry_set_cache mov_es, new_es;
1076 	struct exfat_chain newdir;
1077 
1078 	num_new_entries = exfat_calc_num_entries(p_uniname);
1079 	if (num_new_entries < 0)
1080 		return num_new_entries;
1081 
1082 	ret = exfat_get_dentry_set_by_ei(&mov_es, parent_inode->i_sb, ei);
1083 	if (ret)
1084 		return -EIO;
1085 
1086 	newentry = exfat_find_empty_entry(parent_inode, &newdir,
1087 			num_new_entries, &new_es);
1088 	if (newentry < 0) {
1089 		ret = newentry; /* -EIO or -ENOSPC */
1090 		goto put_mov_es;
1091 	}
1092 
1093 	epmov = exfat_get_dentry_cached(&mov_es, ES_IDX_FILE);
1094 	epnew = exfat_get_dentry_cached(&new_es, ES_IDX_FILE);
1095 	*epnew = *epmov;
1096 	if (exfat_get_entry_type(epnew) == TYPE_FILE) {
1097 		epnew->dentry.file.attr |= cpu_to_le16(EXFAT_ATTR_ARCHIVE);
1098 		ei->attr |= EXFAT_ATTR_ARCHIVE;
1099 	}
1100 
1101 	epmov = exfat_get_dentry_cached(&mov_es, ES_IDX_STREAM);
1102 	epnew = exfat_get_dentry_cached(&new_es, ES_IDX_STREAM);
1103 	*epnew = *epmov;
1104 
1105 	exfat_init_ext_entry(&new_es, num_new_entries, p_uniname);
1106 	exfat_remove_entries(parent_inode, &mov_es, ES_IDX_FILE);
1107 
1108 	ei->dir = newdir;
1109 	ei->entry = newentry;
1110 
1111 	ret = exfat_put_dentry_set(&new_es, IS_DIRSYNC(parent_inode));
1112 	if (ret)
1113 		goto put_mov_es;
1114 
1115 	return exfat_put_dentry_set(&mov_es, IS_DIRSYNC(parent_inode));
1116 
1117 put_mov_es:
1118 	exfat_put_dentry_set(&mov_es, false);
1119 
1120 	return ret;
1121 }
1122 
1123 /* rename or move a old file into a new file */
1124 static int __exfat_rename(struct inode *old_parent_inode,
1125 		struct exfat_inode_info *ei, struct inode *new_parent_inode,
1126 		struct dentry *new_dentry)
1127 {
1128 	int ret;
1129 	struct exfat_uni_name uni_name;
1130 	struct super_block *sb = old_parent_inode->i_sb;
1131 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
1132 	const unsigned char *new_path = new_dentry->d_name.name;
1133 	struct inode *new_inode = new_dentry->d_inode;
1134 	struct exfat_inode_info *new_ei = NULL;
1135 
1136 	/* check the validity of pointer parameters */
1137 	if (new_path == NULL || strlen(new_path) == 0)
1138 		return -EINVAL;
1139 
1140 	if (ei->dir.dir == DIR_DELETED) {
1141 		exfat_err(sb, "abnormal access to deleted source dentry");
1142 		return -ENOENT;
1143 	}
1144 
1145 	/* check whether new dir is existing directory and empty */
1146 	if (new_inode) {
1147 		ret = -EIO;
1148 		new_ei = EXFAT_I(new_inode);
1149 
1150 		if (new_ei->dir.dir == DIR_DELETED) {
1151 			exfat_err(sb, "abnormal access to deleted target dentry");
1152 			goto out;
1153 		}
1154 
1155 		/* if new_inode exists, update ei */
1156 		if (S_ISDIR(new_inode->i_mode)) {
1157 			struct exfat_chain new_clu;
1158 
1159 			new_clu.dir = new_ei->start_clu;
1160 			new_clu.size =
1161 				EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode),
1162 				sbi);
1163 			new_clu.flags = new_ei->flags;
1164 
1165 			ret = exfat_check_dir_empty(sb, &new_clu);
1166 			if (ret)
1167 				goto out;
1168 		}
1169 	}
1170 
1171 	/* check the validity of directory name in the given new pathname */
1172 	ret = exfat_resolve_path(new_parent_inode, new_path, &uni_name);
1173 	if (ret)
1174 		goto out;
1175 
1176 	exfat_set_volume_dirty(sb);
1177 
1178 	if (new_parent_inode == old_parent_inode)
1179 		ret = exfat_rename_file(new_parent_inode, &uni_name, ei);
1180 	else
1181 		ret = exfat_move_file(new_parent_inode, &uni_name, ei);
1182 
1183 	if (!ret && new_inode) {
1184 		struct exfat_entry_set_cache es;
1185 
1186 		/* delete entries of new_dir */
1187 		ret = exfat_get_dentry_set_by_ei(&es, sb, new_ei);
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 (S_ISDIR(new_inode->i_mode) &&
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