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