xref: /linux/fs/exfat/dir.c (revision a62fe21079978e5134ad863f8a9835eb24c06d43) !
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
4  */
5 
6 #include <linux/slab.h>
7 #include <linux/compat.h>
8 #include <linux/bio.h>
9 #include <linux/buffer_head.h>
10 #include <linux/filelock.h>
11 
12 #include "exfat_raw.h"
13 #include "exfat_fs.h"
14 
15 static int exfat_extract_uni_name(struct exfat_dentry *ep,
16 		unsigned short *uniname)
17 {
18 	int i, len = 0;
19 
20 	for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) {
21 		*uniname = le16_to_cpu(ep->dentry.name.unicode_0_14[i]);
22 		if (*uniname == 0x0)
23 			return len;
24 		uniname++;
25 		len++;
26 	}
27 
28 	*uniname = 0x0;
29 	return len;
30 
31 }
32 
33 static int exfat_get_uniname_from_ext_entry(struct super_block *sb,
34 		struct exfat_chain *p_dir, int entry, unsigned short *uniname)
35 {
36 	int i, err;
37 	struct exfat_entry_set_cache es;
38 	unsigned int uni_len = 0, len;
39 
40 	err = exfat_get_dentry_set(&es, sb, p_dir, entry, ES_ALL_ENTRIES);
41 	if (err)
42 		return err;
43 
44 	/*
45 	 * First entry  : file entry
46 	 * Second entry : stream-extension entry
47 	 * Third entry  : first file-name entry
48 	 * So, the index of first file-name dentry should start from 2.
49 	 */
50 	for (i = ES_IDX_FIRST_FILENAME; i < es.num_entries; i++) {
51 		struct exfat_dentry *ep = exfat_get_dentry_cached(&es, i);
52 
53 		/* end of name entry */
54 		if (exfat_get_entry_type(ep) != TYPE_EXTEND)
55 			break;
56 
57 		len = exfat_extract_uni_name(ep, uniname);
58 		uni_len += len;
59 		if (len != EXFAT_FILE_NAME_LEN || uni_len >= MAX_NAME_LENGTH)
60 			break;
61 		uniname += EXFAT_FILE_NAME_LEN;
62 	}
63 
64 	exfat_put_dentry_set(&es, false);
65 	return 0;
66 }
67 
68 /* read a directory entry from the opened directory */
69 static int exfat_readdir(struct inode *inode, loff_t *cpos, struct exfat_dir_entry *dir_entry)
70 {
71 	int i, dentries_per_clu, num_ext, err;
72 	unsigned int type, clu_offset, max_dentries;
73 	struct exfat_chain dir, clu;
74 	struct exfat_uni_name uni_name;
75 	struct exfat_dentry *ep;
76 	struct super_block *sb = inode->i_sb;
77 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
78 	struct exfat_inode_info *ei = EXFAT_I(inode);
79 	unsigned int dentry = EXFAT_B_TO_DEN(*cpos) & 0xFFFFFFFF;
80 	struct buffer_head *bh;
81 
82 	/* check if the given file ID is opened */
83 	if (ei->type != TYPE_DIR)
84 		return -EPERM;
85 
86 	exfat_chain_set(&dir, ei->start_clu,
87 		EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags);
88 
89 	dentries_per_clu = sbi->dentries_per_clu;
90 	max_dentries = (unsigned int)min_t(u64, MAX_EXFAT_DENTRIES,
91 				(u64)EXFAT_CLU_TO_DEN(sbi->num_clusters, sbi));
92 
93 	clu_offset = EXFAT_DEN_TO_CLU(dentry, sbi);
94 	exfat_chain_dup(&clu, &dir);
95 
96 	if (clu.flags == ALLOC_FAT_CHAIN) {
97 		/* hint_information */
98 		if (clu_offset > 0 && ei->hint_bmap.off != EXFAT_EOF_CLUSTER &&
99 		    ei->hint_bmap.off > 0 && clu_offset >= ei->hint_bmap.off) {
100 			clu_offset -= ei->hint_bmap.off;
101 			clu.dir = ei->hint_bmap.clu;
102 			clu.size -= ei->hint_bmap.off;
103 		}
104 	}
105 
106 	if (exfat_chain_advance(sb, &clu, clu_offset))
107 		return -EIO;
108 
109 	while (clu.dir != EXFAT_EOF_CLUSTER && dentry < max_dentries) {
110 		i = dentry & (dentries_per_clu - 1);
111 
112 		for ( ; i < dentries_per_clu; i++, dentry++) {
113 			ep = exfat_get_dentry(sb, &clu, i, &bh);
114 			if (!ep)
115 				return -EIO;
116 
117 			type = exfat_get_entry_type(ep);
118 			if (type == TYPE_UNUSED) {
119 				brelse(bh);
120 				goto out;
121 			}
122 
123 			if (type != TYPE_FILE && type != TYPE_DIR) {
124 				brelse(bh);
125 				continue;
126 			}
127 
128 			num_ext = ep->dentry.file.num_ext;
129 			dir_entry->attr = le16_to_cpu(ep->dentry.file.attr);
130 
131 			*uni_name.name = 0x0;
132 			err = exfat_get_uniname_from_ext_entry(sb, &clu, i,
133 				uni_name.name);
134 			if (err) {
135 				brelse(bh);
136 				continue;
137 			}
138 			exfat_utf16_to_nls(sb, &uni_name,
139 				dir_entry->namebuf.lfn,
140 				dir_entry->namebuf.lfnbuf_len);
141 			brelse(bh);
142 
143 			ep = exfat_get_dentry(sb, &clu, i + 1, &bh);
144 			if (!ep)
145 				return -EIO;
146 			dir_entry->entry = i;
147 			dir_entry->dir = clu;
148 			brelse(bh);
149 
150 			ei->hint_bmap.off = EXFAT_DEN_TO_CLU(dentry, sbi);
151 			ei->hint_bmap.clu = clu.dir;
152 
153 			*cpos = EXFAT_DEN_TO_B(dentry + 1 + num_ext);
154 			return 0;
155 		}
156 
157 		if (exfat_chain_advance(sb, &clu, 1))
158 			return -EIO;
159 	}
160 
161 out:
162 	dir_entry->namebuf.lfn[0] = '\0';
163 	*cpos = EXFAT_DEN_TO_B(dentry);
164 	return 0;
165 }
166 
167 static void exfat_init_namebuf(struct exfat_dentry_namebuf *nb)
168 {
169 	nb->lfn = NULL;
170 	nb->lfnbuf_len = 0;
171 }
172 
173 static int exfat_alloc_namebuf(struct exfat_dentry_namebuf *nb)
174 {
175 	nb->lfn = __getname();
176 	if (!nb->lfn)
177 		return -ENOMEM;
178 	nb->lfnbuf_len = MAX_VFSNAME_BUF_SIZE;
179 	return 0;
180 }
181 
182 static void exfat_free_namebuf(struct exfat_dentry_namebuf *nb)
183 {
184 	if (!nb->lfn)
185 		return;
186 
187 	__putname(nb->lfn);
188 	exfat_init_namebuf(nb);
189 }
190 
191 /*
192  * Before calling dir_emit*(), sbi->s_lock should be released
193  * because page fault can occur in dir_emit*().
194  */
195 #define ITER_POS_FILLED_DOTS    (2)
196 static int exfat_iterate(struct file *file, struct dir_context *ctx)
197 {
198 	struct inode *inode = file_inode(file);
199 	struct super_block *sb = inode->i_sb;
200 	struct inode *tmp;
201 	struct exfat_dir_entry de;
202 	struct exfat_dentry_namebuf *nb = &(de.namebuf);
203 	struct exfat_inode_info *ei = EXFAT_I(inode);
204 	unsigned long inum;
205 	loff_t cpos, i_pos;
206 	int err = 0, fake_offset = 0;
207 
208 	exfat_init_namebuf(nb);
209 
210 	cpos = ctx->pos;
211 	if (!dir_emit_dots(file, ctx))
212 		goto out;
213 
214 	if (ctx->pos == ITER_POS_FILLED_DOTS) {
215 		cpos = 0;
216 		fake_offset = 1;
217 	}
218 
219 	cpos = round_up(cpos, DENTRY_SIZE);
220 
221 	/* name buffer should be allocated before use */
222 	err = exfat_alloc_namebuf(nb);
223 	if (err)
224 		goto out;
225 get_new:
226 	mutex_lock(&EXFAT_SB(sb)->s_lock);
227 
228 	if (ei->flags == ALLOC_NO_FAT_CHAIN && cpos >= i_size_read(inode))
229 		goto end_of_dir;
230 
231 	err = exfat_readdir(inode, &cpos, &de);
232 	if (err) {
233 		/*
234 		 * At least we tried to read a sector.
235 		 * Move cpos to next sector position (should be aligned).
236 		 */
237 		if (err == -EIO) {
238 			cpos += 1 << (sb->s_blocksize_bits);
239 			cpos &= ~(loff_t)(sb->s_blocksize - 1);
240 		}
241 
242 		err = -EIO;
243 		goto end_of_dir;
244 	}
245 
246 	if (!nb->lfn[0])
247 		goto end_of_dir;
248 
249 	i_pos = ((loff_t)de.dir.dir << 32) | (de.entry & 0xffffffff);
250 	tmp = exfat_iget(sb, i_pos);
251 	if (tmp) {
252 		inum = tmp->i_ino;
253 		iput(tmp);
254 	} else {
255 		inum = iunique(sb, EXFAT_ROOT_INO);
256 	}
257 
258 	mutex_unlock(&EXFAT_SB(sb)->s_lock);
259 	if (!dir_emit(ctx, nb->lfn, strlen(nb->lfn), inum,
260 			(de.attr & EXFAT_ATTR_SUBDIR) ? DT_DIR : DT_REG))
261 		goto out;
262 	ctx->pos = cpos;
263 	goto get_new;
264 
265 end_of_dir:
266 	if (!cpos && fake_offset)
267 		cpos = ITER_POS_FILLED_DOTS;
268 	ctx->pos = cpos;
269 	mutex_unlock(&EXFAT_SB(sb)->s_lock);
270 out:
271 	/*
272 	 * To improve performance, free namebuf after unlock sb_lock.
273 	 * If namebuf is not allocated, this function do nothing
274 	 */
275 	exfat_free_namebuf(nb);
276 	return err;
277 }
278 
279 WRAP_DIR_ITER(exfat_iterate) // FIXME!
280 const struct file_operations exfat_dir_operations = {
281 	.llseek		= generic_file_llseek,
282 	.read		= generic_read_dir,
283 	.iterate_shared	= shared_exfat_iterate,
284 	.unlocked_ioctl = exfat_ioctl,
285 #ifdef CONFIG_COMPAT
286 	.compat_ioctl = exfat_compat_ioctl,
287 #endif
288 	.fsync		= exfat_file_fsync,
289 	.setlease	= generic_setlease,
290 };
291 
292 int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu)
293 {
294 	int ret;
295 
296 	exfat_chain_set(clu, EXFAT_EOF_CLUSTER, 0, ALLOC_NO_FAT_CHAIN);
297 
298 	ret = exfat_alloc_cluster(inode, 1, clu, IS_DIRSYNC(inode));
299 	if (ret)
300 		return ret;
301 
302 	return exfat_zeroed_cluster(inode, clu->dir);
303 }
304 
305 int exfat_calc_num_entries(struct exfat_uni_name *p_uniname)
306 {
307 	int len;
308 
309 	len = p_uniname->name_len;
310 	if (len == 0)
311 		return -EINVAL;
312 
313 	/* 1 file entry + 1 stream entry + name entries */
314 	return ES_ENTRY_NUM(len);
315 }
316 
317 unsigned int exfat_get_entry_type(struct exfat_dentry *ep)
318 {
319 	if (ep->type == EXFAT_UNUSED)
320 		return TYPE_UNUSED;
321 	if (IS_EXFAT_DELETED(ep->type))
322 		return TYPE_DELETED;
323 	if (ep->type == EXFAT_INVAL)
324 		return TYPE_INVALID;
325 	if (IS_EXFAT_CRITICAL_PRI(ep->type)) {
326 		if (ep->type == EXFAT_BITMAP)
327 			return TYPE_BITMAP;
328 		if (ep->type == EXFAT_UPCASE)
329 			return TYPE_UPCASE;
330 		if (ep->type == EXFAT_VOLUME)
331 			return TYPE_VOLUME;
332 		if (ep->type == EXFAT_FILE) {
333 			if (le16_to_cpu(ep->dentry.file.attr) & EXFAT_ATTR_SUBDIR)
334 				return TYPE_DIR;
335 			return TYPE_FILE;
336 		}
337 		return TYPE_CRITICAL_PRI;
338 	}
339 	if (IS_EXFAT_BENIGN_PRI(ep->type)) {
340 		if (ep->type == EXFAT_GUID)
341 			return TYPE_GUID;
342 		if (ep->type == EXFAT_PADDING)
343 			return TYPE_PADDING;
344 		if (ep->type == EXFAT_ACLTAB)
345 			return TYPE_ACLTAB;
346 		return TYPE_BENIGN_PRI;
347 	}
348 	if (IS_EXFAT_CRITICAL_SEC(ep->type)) {
349 		if (ep->type == EXFAT_STREAM)
350 			return TYPE_STREAM;
351 		if (ep->type == EXFAT_NAME)
352 			return TYPE_EXTEND;
353 		if (ep->type == EXFAT_ACL)
354 			return TYPE_ACL;
355 		return TYPE_CRITICAL_SEC;
356 	}
357 
358 	if (ep->type == EXFAT_VENDOR_EXT)
359 		return TYPE_VENDOR_EXT;
360 	if (ep->type == EXFAT_VENDOR_ALLOC)
361 		return TYPE_VENDOR_ALLOC;
362 
363 	return TYPE_BENIGN_SEC;
364 }
365 
366 static void exfat_set_entry_type(struct exfat_dentry *ep, unsigned int type)
367 {
368 	if (type == TYPE_UNUSED) {
369 		ep->type = EXFAT_UNUSED;
370 	} else if (type == TYPE_DELETED) {
371 		ep->type &= EXFAT_DELETE;
372 	} else if (type == TYPE_STREAM) {
373 		ep->type = EXFAT_STREAM;
374 	} else if (type == TYPE_EXTEND) {
375 		ep->type = EXFAT_NAME;
376 	} else if (type == TYPE_BITMAP) {
377 		ep->type = EXFAT_BITMAP;
378 	} else if (type == TYPE_UPCASE) {
379 		ep->type = EXFAT_UPCASE;
380 	} else if (type == TYPE_VOLUME) {
381 		ep->type = EXFAT_VOLUME;
382 	} else if (type == TYPE_DIR) {
383 		ep->type = EXFAT_FILE;
384 		ep->dentry.file.attr = cpu_to_le16(EXFAT_ATTR_SUBDIR);
385 	} else if (type == TYPE_FILE) {
386 		ep->type = EXFAT_FILE;
387 		ep->dentry.file.attr = cpu_to_le16(EXFAT_ATTR_ARCHIVE);
388 	}
389 }
390 
391 static void exfat_init_stream_entry(struct exfat_dentry *ep,
392 		unsigned int start_clu, unsigned long long size)
393 {
394 	memset(ep, 0, sizeof(*ep));
395 	exfat_set_entry_type(ep, TYPE_STREAM);
396 	if (size == 0)
397 		ep->dentry.stream.flags = ALLOC_FAT_CHAIN;
398 	else
399 		ep->dentry.stream.flags = ALLOC_NO_FAT_CHAIN;
400 	ep->dentry.stream.start_clu = cpu_to_le32(start_clu);
401 	ep->dentry.stream.valid_size = cpu_to_le64(size);
402 	ep->dentry.stream.size = cpu_to_le64(size);
403 }
404 
405 static void exfat_init_name_entry(struct exfat_dentry *ep,
406 		unsigned short *uniname)
407 {
408 	int i;
409 
410 	exfat_set_entry_type(ep, TYPE_EXTEND);
411 	ep->dentry.name.flags = 0x0;
412 
413 	for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) {
414 		if (*uniname != 0x0) {
415 			ep->dentry.name.unicode_0_14[i] = cpu_to_le16(*uniname);
416 			uniname++;
417 		} else {
418 			ep->dentry.name.unicode_0_14[i] = 0x0;
419 		}
420 	}
421 }
422 
423 void exfat_init_dir_entry(struct exfat_entry_set_cache *es,
424 		unsigned int type, unsigned int start_clu,
425 		unsigned long long size, struct timespec64 *ts)
426 {
427 	struct super_block *sb = es->sb;
428 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
429 	struct exfat_dentry *ep;
430 
431 	ep = exfat_get_dentry_cached(es, ES_IDX_FILE);
432 	memset(ep, 0, sizeof(*ep));
433 	exfat_set_entry_type(ep, type);
434 	exfat_set_entry_time(sbi, ts,
435 			&ep->dentry.file.create_tz,
436 			&ep->dentry.file.create_time,
437 			&ep->dentry.file.create_date,
438 			&ep->dentry.file.create_time_cs);
439 	exfat_set_entry_time(sbi, ts,
440 			&ep->dentry.file.modify_tz,
441 			&ep->dentry.file.modify_time,
442 			&ep->dentry.file.modify_date,
443 			&ep->dentry.file.modify_time_cs);
444 	exfat_set_entry_time(sbi, ts,
445 			&ep->dentry.file.access_tz,
446 			&ep->dentry.file.access_time,
447 			&ep->dentry.file.access_date,
448 			NULL);
449 
450 	ep = exfat_get_dentry_cached(es, ES_IDX_STREAM);
451 	exfat_init_stream_entry(ep, start_clu, size);
452 }
453 
454 static void exfat_free_benign_secondary_clusters(struct inode *inode,
455 		struct exfat_dentry *ep)
456 {
457 	struct super_block *sb = inode->i_sb;
458 	struct exfat_chain dir;
459 	unsigned int start_clu =
460 		le32_to_cpu(ep->dentry.generic_secondary.start_clu);
461 	u64 size = le64_to_cpu(ep->dentry.generic_secondary.size);
462 	unsigned char flags = ep->dentry.generic_secondary.flags;
463 
464 	if (!(flags & ALLOC_POSSIBLE) || !start_clu || !size)
465 		return;
466 
467 	exfat_chain_set(&dir, start_clu,
468 			EXFAT_B_TO_CLU_ROUND_UP(size, EXFAT_SB(sb)),
469 			flags);
470 	exfat_free_cluster(inode, &dir);
471 }
472 
473 void exfat_init_ext_entry(struct exfat_entry_set_cache *es, int num_entries,
474 		struct exfat_uni_name *p_uniname)
475 {
476 	int i;
477 	unsigned short *uniname = p_uniname->name;
478 	struct exfat_dentry *ep;
479 
480 	es->num_entries = num_entries;
481 	ep = exfat_get_dentry_cached(es, ES_IDX_FILE);
482 	ep->dentry.file.num_ext = (unsigned char)(num_entries - 1);
483 
484 	ep = exfat_get_dentry_cached(es, ES_IDX_STREAM);
485 	ep->dentry.stream.name_len = p_uniname->name_len;
486 	ep->dentry.stream.name_hash = cpu_to_le16(p_uniname->name_hash);
487 
488 	for (i = ES_IDX_FIRST_FILENAME; i < num_entries; i++) {
489 		ep = exfat_get_dentry_cached(es, i);
490 		exfat_init_name_entry(ep, uniname);
491 		uniname += EXFAT_FILE_NAME_LEN;
492 	}
493 
494 	exfat_update_dir_chksum(es);
495 }
496 
497 void exfat_remove_entries(struct inode *inode, struct exfat_entry_set_cache *es,
498 		int order)
499 {
500 	int i;
501 	struct exfat_dentry *ep;
502 
503 	for (i = order; i < es->num_entries; i++) {
504 		ep = exfat_get_dentry_cached(es, i);
505 
506 		if (exfat_get_entry_type(ep) & TYPE_BENIGN_SEC)
507 			exfat_free_benign_secondary_clusters(inode, ep);
508 
509 		exfat_set_entry_type(ep, TYPE_DELETED);
510 	}
511 
512 	if (order < es->num_entries)
513 		es->modified = true;
514 }
515 
516 void exfat_update_dir_chksum(struct exfat_entry_set_cache *es)
517 {
518 	int chksum_type = CS_DIR_ENTRY, i;
519 	unsigned short chksum = 0;
520 	struct exfat_dentry *ep;
521 
522 	for (i = ES_IDX_FILE; i < es->num_entries; i++) {
523 		ep = exfat_get_dentry_cached(es, i);
524 		chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
525 					     chksum_type);
526 		chksum_type = CS_DEFAULT;
527 	}
528 	ep = exfat_get_dentry_cached(es, ES_IDX_FILE);
529 	ep->dentry.file.checksum = cpu_to_le16(chksum);
530 	es->modified = true;
531 }
532 
533 int exfat_put_dentry_set(struct exfat_entry_set_cache *es, int sync)
534 {
535 	int i, err = 0;
536 
537 	if (es->modified)
538 		err = exfat_update_bhs(es->bh, es->num_bh, sync);
539 
540 	for (i = 0; i < es->num_bh; i++)
541 		if (err)
542 			bforget(es->bh[i]);
543 		else
544 			brelse(es->bh[i]);
545 
546 	if (IS_DYNAMIC_ES(es))
547 		kfree(es->bh);
548 
549 	return err;
550 }
551 
552 static int exfat_find_location(struct super_block *sb, struct exfat_chain *p_dir,
553 			       int entry, sector_t *sector, int *offset)
554 {
555 	int ret;
556 	unsigned int off, clu = 0;
557 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
558 
559 	off = EXFAT_DEN_TO_B(entry);
560 
561 	clu = p_dir->dir;
562 	ret = exfat_cluster_walk(sb, &clu, EXFAT_B_TO_CLU(off, sbi), p_dir->flags);
563 	if (ret)
564 		return ret;
565 
566 	if (clu == EXFAT_EOF_CLUSTER) {
567 		exfat_fs_error(sb,
568 			"unexpected early break in cluster chain (clu : %u, len : %d)",
569 			p_dir->dir,
570 			EXFAT_B_TO_CLU(off, sbi));
571 		return -EIO;
572 	}
573 
574 	if (!exfat_test_bitmap(sb, clu)) {
575 		exfat_err(sb, "failed to test cluster bit(%u)", clu);
576 		return -EIO;
577 	}
578 
579 	/* byte offset in cluster */
580 	off = EXFAT_CLU_OFFSET(off, sbi);
581 
582 	/* byte offset in sector    */
583 	*offset = EXFAT_BLK_OFFSET(off, sb);
584 
585 	/* sector offset in cluster */
586 	*sector = EXFAT_B_TO_BLK(off, sb);
587 	*sector += exfat_cluster_to_sector(sbi, clu);
588 	return 0;
589 }
590 
591 struct exfat_dentry *exfat_get_dentry(struct super_block *sb,
592 		struct exfat_chain *p_dir, int entry, struct buffer_head **bh)
593 {
594 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
595 	unsigned int sect_per_clus = sbi->sect_per_clus;
596 	unsigned int dentries_per_page = EXFAT_B_TO_DEN(PAGE_SIZE);
597 	int off;
598 	sector_t sec;
599 
600 	if (p_dir->dir == DIR_DELETED) {
601 		exfat_err(sb, "abnormal access to deleted dentry");
602 		return NULL;
603 	}
604 
605 	if (exfat_find_location(sb, p_dir, entry, &sec, &off))
606 		return NULL;
607 
608 	if (sect_per_clus > 1 &&
609 	    (entry & (dentries_per_page - 1)) == 0) {
610 		sector_t ra = sec;
611 		blkcnt_t cnt = 0;
612 		unsigned int ra_count = sect_per_clus;
613 
614 		/* Not sector aligned with ra_count, resize ra_count to page size */
615 		if ((sec - sbi->data_start_sector) & (ra_count - 1))
616 			ra_count = PAGE_SIZE >> sb->s_blocksize_bits;
617 
618 		exfat_blk_readahead(sb, sec, &ra, &cnt, sec + ra_count - 1);
619 	}
620 
621 	*bh = sb_bread(sb, sec);
622 	if (!*bh)
623 		return NULL;
624 
625 	return (struct exfat_dentry *)((*bh)->b_data + off);
626 }
627 
628 enum exfat_validate_dentry_mode {
629 	ES_MODE_GET_FILE_ENTRY,
630 	ES_MODE_GET_STRM_ENTRY,
631 	ES_MODE_GET_NAME_ENTRY,
632 	ES_MODE_GET_CRITICAL_SEC_ENTRY,
633 	ES_MODE_GET_BENIGN_SEC_ENTRY,
634 };
635 
636 static bool exfat_validate_entry(unsigned int type,
637 		enum exfat_validate_dentry_mode *mode)
638 {
639 	if (type == TYPE_UNUSED || type == TYPE_DELETED)
640 		return false;
641 
642 	switch (*mode) {
643 	case ES_MODE_GET_FILE_ENTRY:
644 		if (type != TYPE_STREAM)
645 			return false;
646 		*mode = ES_MODE_GET_STRM_ENTRY;
647 		break;
648 	case ES_MODE_GET_STRM_ENTRY:
649 		if (type != TYPE_EXTEND)
650 			return false;
651 		*mode = ES_MODE_GET_NAME_ENTRY;
652 		break;
653 	case ES_MODE_GET_NAME_ENTRY:
654 		if (type & TYPE_BENIGN_SEC)
655 			*mode = ES_MODE_GET_BENIGN_SEC_ENTRY;
656 		else if (type != TYPE_EXTEND)
657 			return false;
658 		break;
659 	case ES_MODE_GET_BENIGN_SEC_ENTRY:
660 		/* Assume unreconized benign secondary entry */
661 		if (!(type & TYPE_BENIGN_SEC))
662 			return false;
663 		break;
664 	default:
665 		return false;
666 	}
667 
668 	return true;
669 }
670 
671 struct exfat_dentry *exfat_get_dentry_cached(
672 	struct exfat_entry_set_cache *es, int num)
673 {
674 	int off = es->start_off + num * DENTRY_SIZE;
675 	struct buffer_head *bh = es->bh[EXFAT_B_TO_BLK(off, es->sb)];
676 	char *p = bh->b_data + EXFAT_BLK_OFFSET(off, es->sb);
677 
678 	return (struct exfat_dentry *)p;
679 }
680 
681 /*
682  * Returns a set of dentries.
683  *
684  * Note It provides a direct pointer to bh->data via exfat_get_dentry_cached().
685  * User should call exfat_get_dentry_set() after setting 'modified' to apply
686  * changes made in this entry set to the real device.
687  *
688  * in:
689  *   sb+p_dir+entry: indicates a file/dir
690  *   num_entries: specifies how many dentries should be included.
691  *                It will be set to es->num_entries if it is not 0.
692  *                If num_entries is 0, es->num_entries will be obtained
693  *                from the first dentry.
694  * out:
695  *   es: pointer of entry set on success.
696  * return:
697  *   0 on success
698  *   -error code on failure
699  */
700 static int __exfat_get_dentry_set(struct exfat_entry_set_cache *es,
701 		struct super_block *sb, struct exfat_chain *p_dir, int entry,
702 		unsigned int num_entries)
703 {
704 	int ret, i, num_bh;
705 	unsigned int off;
706 	sector_t sec;
707 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
708 	struct buffer_head *bh;
709 
710 	if (p_dir->dir == DIR_DELETED) {
711 		exfat_err(sb, "access to deleted dentry");
712 		return -EIO;
713 	}
714 
715 	ret = exfat_find_location(sb, p_dir, entry, &sec, &off);
716 	if (ret)
717 		return ret;
718 
719 	memset(es, 0, sizeof(*es));
720 	es->sb = sb;
721 	es->modified = false;
722 	es->start_off = off;
723 	es->bh = es->__bh;
724 
725 	bh = sb_bread(sb, sec);
726 	if (!bh)
727 		return -EIO;
728 	es->bh[es->num_bh++] = bh;
729 
730 	if (num_entries == ES_ALL_ENTRIES) {
731 		struct exfat_dentry *ep;
732 
733 		ep = exfat_get_dentry_cached(es, ES_IDX_FILE);
734 		if (ep->type != EXFAT_FILE) {
735 			brelse(bh);
736 			return -EIO;
737 		}
738 
739 		num_entries = ep->dentry.file.num_ext + 1;
740 	}
741 
742 	es->num_entries = num_entries;
743 
744 	num_bh = EXFAT_B_TO_BLK_ROUND_UP(off + num_entries * DENTRY_SIZE, sb);
745 	if (num_bh > ARRAY_SIZE(es->__bh)) {
746 		es->bh = kmalloc_objs(*es->bh, num_bh, GFP_NOFS);
747 		if (!es->bh) {
748 			brelse(bh);
749 			return -ENOMEM;
750 		}
751 		es->bh[0] = bh;
752 	}
753 
754 	for (i = 1; i < num_bh; i++) {
755 		/* get the next sector */
756 		if (exfat_is_last_sector_in_cluster(sbi, sec)) {
757 			unsigned int clu = exfat_sector_to_cluster(sbi, sec);
758 
759 			if (exfat_cluster_walk(sb, &clu, 1, p_dir->flags))
760 				goto put_es;
761 			sec = exfat_cluster_to_sector(sbi, clu);
762 		} else {
763 			sec++;
764 		}
765 
766 		bh = sb_bread(sb, sec);
767 		if (!bh)
768 			goto put_es;
769 		es->bh[es->num_bh++] = bh;
770 	}
771 
772 	return 0;
773 
774 put_es:
775 	exfat_put_dentry_set(es, false);
776 	return -EIO;
777 }
778 
779 int exfat_get_dentry_set(struct exfat_entry_set_cache *es,
780 		struct super_block *sb, struct exfat_chain *p_dir,
781 		int entry, unsigned int num_entries)
782 {
783 	int ret, i;
784 	struct exfat_dentry *ep;
785 	enum exfat_validate_dentry_mode mode = ES_MODE_GET_FILE_ENTRY;
786 
787 	ret = __exfat_get_dentry_set(es, sb, p_dir, entry, num_entries);
788 	if (ret < 0)
789 		return ret;
790 
791 	/* validate cached dentries */
792 	for (i = ES_IDX_STREAM; i < es->num_entries; i++) {
793 		ep = exfat_get_dentry_cached(es, i);
794 		if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
795 			goto put_es;
796 	}
797 	return 0;
798 
799 put_es:
800 	exfat_put_dentry_set(es, false);
801 	return -EIO;
802 }
803 
804 static int exfat_validate_empty_dentry_set(struct exfat_entry_set_cache *es)
805 {
806 	struct exfat_dentry *ep;
807 	struct buffer_head *bh;
808 	int i, off;
809 	bool unused_hit = false;
810 
811 	/*
812 	 * ONLY UNUSED OR DELETED DENTRIES ARE ALLOWED:
813 	 * Although it violates the specification for a deleted entry to
814 	 * follow an unused entry, some exFAT implementations could work
815 	 * like this. Therefore, to improve compatibility, let's allow it.
816 	 */
817 	for (i = 0; i < es->num_entries; i++) {
818 		ep = exfat_get_dentry_cached(es, i);
819 		if (ep->type == EXFAT_UNUSED) {
820 			unused_hit = true;
821 		} else if (!IS_EXFAT_DELETED(ep->type)) {
822 			if (unused_hit)
823 				goto err_used_follow_unused;
824 			i++;
825 			goto count_skip_entries;
826 		}
827 	}
828 
829 	return 0;
830 
831 err_used_follow_unused:
832 	off = es->start_off + (i << DENTRY_SIZE_BITS);
833 	bh = es->bh[EXFAT_B_TO_BLK(off, es->sb)];
834 
835 	exfat_fs_error(es->sb,
836 		"in sector %lld, dentry %d should be unused, but 0x%x",
837 		bh->b_blocknr, off >> DENTRY_SIZE_BITS, ep->type);
838 
839 	return -EIO;
840 
841 count_skip_entries:
842 	es->num_entries = EXFAT_B_TO_DEN(EXFAT_BLK_TO_B(es->num_bh, es->sb) - es->start_off);
843 	for (; i < es->num_entries; i++) {
844 		ep = exfat_get_dentry_cached(es, i);
845 		if (IS_EXFAT_DELETED(ep->type))
846 			break;
847 	}
848 
849 	return i;
850 }
851 
852 /*
853  * Get an empty dentry set.
854  *
855  * in:
856  *   sb+p_dir+entry: indicates the empty dentry location
857  *   num_entries: specifies how many empty dentries should be included.
858  * out:
859  *   es: pointer of empty dentry set on success.
860  * return:
861  *   0  : on success
862  *   >0 : the dentries are not empty, the return value is the number of
863  *        dentries to be skipped for the next lookup.
864  *   <0 : on failure
865  */
866 int exfat_get_empty_dentry_set(struct exfat_entry_set_cache *es,
867 		struct super_block *sb, struct exfat_chain *p_dir,
868 		int entry, unsigned int num_entries)
869 {
870 	int ret;
871 
872 	ret = __exfat_get_dentry_set(es, sb, p_dir, entry, num_entries);
873 	if (ret < 0)
874 		return ret;
875 
876 	ret = exfat_validate_empty_dentry_set(es);
877 	if (ret)
878 		exfat_put_dentry_set(es, false);
879 
880 	return ret;
881 }
882 
883 static inline void exfat_reset_empty_hint(struct exfat_hint_femp *hint_femp)
884 {
885 	hint_femp->eidx = EXFAT_HINT_NONE;
886 	hint_femp->count = 0;
887 }
888 
889 static inline void exfat_set_empty_hint(struct exfat_inode_info *ei,
890 		struct exfat_hint_femp *candi_empty, struct exfat_chain *clu,
891 		int dentry, int num_entries, int entry_type)
892 {
893 	if (ei->hint_femp.eidx == EXFAT_HINT_NONE ||
894 	    ei->hint_femp.eidx > dentry) {
895 		int total_entries = EXFAT_B_TO_DEN(i_size_read(&ei->vfs_inode));
896 
897 		if (candi_empty->count == 0) {
898 			candi_empty->cur = *clu;
899 			candi_empty->eidx = dentry;
900 		}
901 
902 		if (entry_type == TYPE_UNUSED)
903 			candi_empty->count += total_entries - dentry;
904 		else
905 			candi_empty->count++;
906 
907 		if (candi_empty->count == num_entries ||
908 		    candi_empty->count + candi_empty->eidx == total_entries)
909 			ei->hint_femp = *candi_empty;
910 	}
911 }
912 
913 enum {
914 	DIRENT_STEP_FILE,
915 	DIRENT_STEP_STRM,
916 	DIRENT_STEP_NAME,
917 	DIRENT_STEP_SECD,
918 };
919 
920 /*
921  * @ei:         inode info of parent directory
922  * @p_dir:      directory structure of parent directory
923  * @num_entries:entry size of p_uniname
924  * @hint_opt:   If p_uniname is found, filled with optimized dir/entry
925  *              for traversing cluster chain.
926  * @return:
927  *   >= 0:      file directory entry position where the name exists
928  *   -ENOENT:   entry with the name does not exist
929  *   -EIO:      I/O error
930  */
931 int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
932 		struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
933 		struct exfat_hint *hint_opt)
934 {
935 	int i, rewind = 0, dentry = 0, end_eidx = 0, num_ext = 0, len;
936 	int order, step, name_len = 0;
937 	int dentries_per_clu;
938 	unsigned int entry_type;
939 	unsigned short *uniname = NULL;
940 	struct exfat_chain clu;
941 	struct exfat_hint *hint_stat = &ei->hint_stat;
942 	struct exfat_hint_femp candi_empty;
943 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
944 	int num_entries = exfat_calc_num_entries(p_uniname);
945 	unsigned int clu_count = 0;
946 
947 	if (num_entries < 0)
948 		return num_entries;
949 
950 	dentries_per_clu = sbi->dentries_per_clu;
951 
952 	exfat_chain_dup(&clu, p_dir);
953 
954 	if (hint_stat->eidx) {
955 		clu.dir = hint_stat->clu;
956 		dentry = hint_stat->eidx;
957 		end_eidx = dentry;
958 	}
959 
960 	exfat_reset_empty_hint(&ei->hint_femp);
961 
962 rewind:
963 	order = 0;
964 	step = DIRENT_STEP_FILE;
965 	exfat_reset_empty_hint(&candi_empty);
966 
967 	while (clu.dir != EXFAT_EOF_CLUSTER) {
968 		i = dentry & (dentries_per_clu - 1);
969 		for (; i < dentries_per_clu; i++, dentry++) {
970 			struct exfat_dentry *ep;
971 			struct buffer_head *bh;
972 
973 			if (rewind && dentry == end_eidx)
974 				goto not_found;
975 
976 			ep = exfat_get_dentry(sb, &clu, i, &bh);
977 			if (!ep)
978 				return -EIO;
979 
980 			entry_type = exfat_get_entry_type(ep);
981 
982 			if (entry_type == TYPE_UNUSED ||
983 			    entry_type == TYPE_DELETED) {
984 				step = DIRENT_STEP_FILE;
985 
986 				exfat_set_empty_hint(ei, &candi_empty, &clu,
987 						dentry, num_entries,
988 						entry_type);
989 
990 				brelse(bh);
991 				if (entry_type == TYPE_UNUSED)
992 					goto not_found;
993 				continue;
994 			}
995 
996 			exfat_reset_empty_hint(&candi_empty);
997 
998 			if (entry_type == TYPE_FILE || entry_type == TYPE_DIR) {
999 				step = DIRENT_STEP_FILE;
1000 				hint_opt->clu = clu.dir;
1001 				hint_opt->eidx = i;
1002 				num_ext = ep->dentry.file.num_ext;
1003 				step = DIRENT_STEP_STRM;
1004 				brelse(bh);
1005 				continue;
1006 			}
1007 
1008 			if (entry_type == TYPE_STREAM) {
1009 				u16 name_hash;
1010 
1011 				if (step != DIRENT_STEP_STRM) {
1012 					step = DIRENT_STEP_FILE;
1013 					brelse(bh);
1014 					continue;
1015 				}
1016 				step = DIRENT_STEP_FILE;
1017 				name_hash = le16_to_cpu(
1018 						ep->dentry.stream.name_hash);
1019 				if (p_uniname->name_hash == name_hash &&
1020 				    p_uniname->name_len ==
1021 						ep->dentry.stream.name_len) {
1022 					step = DIRENT_STEP_NAME;
1023 					order = 1;
1024 					name_len = 0;
1025 				}
1026 				brelse(bh);
1027 				continue;
1028 			}
1029 
1030 			brelse(bh);
1031 			if (entry_type == TYPE_EXTEND) {
1032 				unsigned short entry_uniname[16], unichar;
1033 
1034 				if (step != DIRENT_STEP_NAME ||
1035 				    name_len >= MAX_NAME_LENGTH) {
1036 					step = DIRENT_STEP_FILE;
1037 					continue;
1038 				}
1039 
1040 				if (++order == 2)
1041 					uniname = p_uniname->name;
1042 				else
1043 					uniname += EXFAT_FILE_NAME_LEN;
1044 
1045 				len = exfat_extract_uni_name(ep, entry_uniname);
1046 				name_len += len;
1047 
1048 				unichar = *(uniname+len);
1049 				*(uniname+len) = 0x0;
1050 
1051 				if (exfat_uniname_ncmp(sb, uniname,
1052 					entry_uniname, len)) {
1053 					step = DIRENT_STEP_FILE;
1054 				} else if (p_uniname->name_len == name_len) {
1055 					if (order == num_ext)
1056 						goto found;
1057 					step = DIRENT_STEP_SECD;
1058 				}
1059 
1060 				*(uniname+len) = unichar;
1061 				continue;
1062 			}
1063 
1064 			if (entry_type &
1065 					(TYPE_CRITICAL_SEC | TYPE_BENIGN_SEC)) {
1066 				if (step == DIRENT_STEP_SECD) {
1067 					if (++order == num_ext)
1068 						goto found;
1069 					continue;
1070 				}
1071 			}
1072 			step = DIRENT_STEP_FILE;
1073 		}
1074 
1075 		if (exfat_chain_advance(sb, &clu, 1))
1076 			return -EIO;
1077 
1078 		/* break if the cluster chain includes a loop */
1079 		if (unlikely(++clu_count > EXFAT_DATA_CLUSTER_COUNT(sbi)))
1080 			goto not_found;
1081 	}
1082 
1083 not_found:
1084 	/*
1085 	 * We started at not 0 index,so we should try to find target
1086 	 * from 0 index to the index we started at.
1087 	 */
1088 	if (!rewind && end_eidx) {
1089 		rewind = 1;
1090 		dentry = 0;
1091 		clu.dir = p_dir->dir;
1092 		goto rewind;
1093 	}
1094 
1095 	/*
1096 	 * set the EXFAT_EOF_CLUSTER flag to avoid search
1097 	 * from the beginning again when allocated a new cluster
1098 	 */
1099 	if (ei->hint_femp.eidx == EXFAT_HINT_NONE) {
1100 		ei->hint_femp.cur.dir = EXFAT_EOF_CLUSTER;
1101 		ei->hint_femp.eidx = p_dir->size * dentries_per_clu;
1102 		ei->hint_femp.count = 0;
1103 	}
1104 
1105 	/* initialized hint_stat */
1106 	hint_stat->clu = p_dir->dir;
1107 	hint_stat->eidx = 0;
1108 	return -ENOENT;
1109 
1110 found:
1111 	/* next dentry we'll find is out of this cluster */
1112 	if (!((dentry + 1) & (dentries_per_clu - 1))) {
1113 		int ret = 0;
1114 
1115 		ret = exfat_chain_advance(sb, &clu, 1);
1116 
1117 		if (ret || clu.dir == EXFAT_EOF_CLUSTER) {
1118 			/* just initialized hint_stat */
1119 			hint_stat->clu = p_dir->dir;
1120 			hint_stat->eidx = 0;
1121 			return (dentry - num_ext);
1122 		}
1123 	}
1124 
1125 	hint_stat->clu = clu.dir;
1126 	hint_stat->eidx = dentry + 1;
1127 	return dentry - num_ext;
1128 }
1129 
1130 int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
1131 {
1132 	int i, count = 0;
1133 	int dentries_per_clu;
1134 	unsigned int entry_type;
1135 	unsigned int clu_count = 0;
1136 	struct exfat_chain clu;
1137 	struct exfat_dentry *ep;
1138 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
1139 	struct buffer_head *bh;
1140 
1141 	dentries_per_clu = sbi->dentries_per_clu;
1142 
1143 	exfat_chain_dup(&clu, p_dir);
1144 
1145 	while (clu.dir != EXFAT_EOF_CLUSTER) {
1146 		for (i = 0; i < dentries_per_clu; i++) {
1147 			ep = exfat_get_dentry(sb, &clu, i, &bh);
1148 			if (!ep)
1149 				return -EIO;
1150 			entry_type = exfat_get_entry_type(ep);
1151 			brelse(bh);
1152 
1153 			if (entry_type == TYPE_UNUSED)
1154 				return count;
1155 			if (entry_type != TYPE_DIR)
1156 				continue;
1157 			count++;
1158 		}
1159 
1160 		if (exfat_chain_advance(sb, &clu, 1))
1161 			return -EIO;
1162 
1163 		if (unlikely(++clu_count > sbi->used_clusters)) {
1164 			exfat_fs_error(sb, "FAT or bitmap is corrupted");
1165 			return -EIO;
1166 		}
1167 	}
1168 
1169 	return count;
1170 }
1171 
1172 static int exfat_get_volume_label_dentry(struct super_block *sb,
1173 		struct exfat_entry_set_cache *es)
1174 {
1175 	int i;
1176 	int dentry = 0;
1177 	unsigned int type;
1178 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
1179 	struct exfat_hint_femp hint_femp;
1180 	struct exfat_inode_info *ei = EXFAT_I(sb->s_root->d_inode);
1181 	struct exfat_chain clu;
1182 	struct exfat_dentry *ep;
1183 	struct buffer_head *bh;
1184 
1185 	hint_femp.eidx = EXFAT_HINT_NONE;
1186 	exfat_chain_set(&clu, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
1187 
1188 	while (clu.dir != EXFAT_EOF_CLUSTER) {
1189 		for (i = 0; i < sbi->dentries_per_clu; i++, dentry++) {
1190 			ep = exfat_get_dentry(sb, &clu, i, &bh);
1191 			if (!ep)
1192 				return -EIO;
1193 
1194 			type = exfat_get_entry_type(ep);
1195 			if (hint_femp.eidx == EXFAT_HINT_NONE) {
1196 				if (type == TYPE_DELETED || type == TYPE_UNUSED) {
1197 					hint_femp.cur = clu;
1198 					hint_femp.eidx = dentry;
1199 					hint_femp.count = 1;
1200 				}
1201 			}
1202 
1203 			if (type == TYPE_UNUSED) {
1204 				brelse(bh);
1205 				goto not_found;
1206 			}
1207 
1208 			if (type != TYPE_VOLUME) {
1209 				brelse(bh);
1210 				continue;
1211 			}
1212 
1213 			memset(es, 0, sizeof(*es));
1214 			es->sb = sb;
1215 			es->bh = es->__bh;
1216 			es->bh[0] = bh;
1217 			es->num_bh = 1;
1218 			es->start_off = EXFAT_DEN_TO_B(i) % sb->s_blocksize;
1219 
1220 			return 0;
1221 		}
1222 
1223 		if (exfat_get_next_cluster(sb, &(clu.dir)))
1224 			return -EIO;
1225 	}
1226 
1227 not_found:
1228 	if (hint_femp.eidx == EXFAT_HINT_NONE) {
1229 		hint_femp.cur.dir = EXFAT_EOF_CLUSTER;
1230 		hint_femp.eidx = dentry;
1231 		hint_femp.count = 0;
1232 	}
1233 
1234 	ei->hint_femp = hint_femp;
1235 
1236 	return -ENOENT;
1237 }
1238 
1239 int exfat_read_volume_label(struct super_block *sb, struct exfat_uni_name *label_out)
1240 {
1241 	int ret, i;
1242 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
1243 	struct exfat_entry_set_cache es;
1244 	struct exfat_dentry *ep;
1245 
1246 	mutex_lock(&sbi->s_lock);
1247 
1248 	memset(label_out, 0, sizeof(*label_out));
1249 	ret = exfat_get_volume_label_dentry(sb, &es);
1250 	if (ret < 0) {
1251 		/*
1252 		 * ENOENT signifies that a volume label dentry doesn't exist
1253 		 * We will treat this as an empty volume label and not fail.
1254 		 */
1255 		if (ret == -ENOENT)
1256 			ret = 0;
1257 
1258 		goto unlock;
1259 	}
1260 
1261 	ep = exfat_get_dentry_cached(&es, 0);
1262 	label_out->name_len = ep->dentry.volume_label.char_count;
1263 	if (label_out->name_len > EXFAT_VOLUME_LABEL_LEN) {
1264 		ret = -EIO;
1265 		exfat_put_dentry_set(&es, false);
1266 		goto unlock;
1267 	}
1268 
1269 	for (i = 0; i < label_out->name_len; i++)
1270 		label_out->name[i] = le16_to_cpu(ep->dentry.volume_label.volume_label[i]);
1271 
1272 	exfat_put_dentry_set(&es, false);
1273 unlock:
1274 	mutex_unlock(&sbi->s_lock);
1275 	return ret;
1276 }
1277 
1278 int exfat_write_volume_label(struct super_block *sb,
1279 			     struct exfat_uni_name *label)
1280 {
1281 	int ret, i;
1282 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
1283 	struct inode *root_inode = sb->s_root->d_inode;
1284 	struct exfat_entry_set_cache es;
1285 	struct exfat_chain clu;
1286 	struct exfat_dentry *ep;
1287 
1288 	if (label->name_len > EXFAT_VOLUME_LABEL_LEN)
1289 		return -EINVAL;
1290 
1291 	mutex_lock(&sbi->s_lock);
1292 
1293 	ret = exfat_get_volume_label_dentry(sb, &es);
1294 	if (ret == -ENOENT) {
1295 		if (label->name_len == 0) {
1296 			/* No volume label dentry, no need to clear */
1297 			ret = 0;
1298 			goto unlock;
1299 		}
1300 
1301 		ret = exfat_find_empty_entry(root_inode, &clu, 1, &es);
1302 	}
1303 
1304 	if (ret < 0)
1305 		goto unlock;
1306 
1307 	ep = exfat_get_dentry_cached(&es, 0);
1308 
1309 	if (label->name_len == 0 && ep->dentry.volume_label.char_count == 0) {
1310 		/* volume label had been cleared */
1311 		exfat_put_dentry_set(&es, 0);
1312 		goto unlock;
1313 	}
1314 
1315 	memset(ep, 0, sizeof(*ep));
1316 	ep->type = EXFAT_VOLUME;
1317 
1318 	for (i = 0; i < label->name_len; i++)
1319 		ep->dentry.volume_label.volume_label[i] =
1320 			cpu_to_le16(label->name[i]);
1321 
1322 	ep->dentry.volume_label.char_count = label->name_len;
1323 	es.modified = true;
1324 
1325 	ret = exfat_put_dentry_set(&es, IS_DIRSYNC(root_inode));
1326 
1327 unlock:
1328 	mutex_unlock(&sbi->s_lock);
1329 	return ret;
1330 }
1331