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