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