xref: /linux/fs/exfat/dir.c (revision bba2c3615bd6cfee7456d1130f2e6b01b3f4e9ba)
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_bytes_to_dentries(*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_bytes_to_cluster(sbi, i_size_read(inode)), ei->flags);
88 
89 	dentries_per_clu = sbi->dentries_per_clu;
90 	max_dentries = min(MAX_EXFAT_DENTRIES,
91 			exfat_cluster_to_dentries(sbi, sbi->num_clusters));
92 
93 	clu_offset = exfat_dentries_to_cluster(sbi, dentry);
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_dentries_to_cluster(sbi, dentry);
151 			ei->hint_bmap.clu = clu.dir;
152 
153 			*cpos = exfat_dentries_to_bytes(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_dentries_to_bytes(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), false);
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_bytes_to_cluster_round_up(EXFAT_SB(sb), size),
469 			flags);
470 	exfat_free_cluster(inode, &dir);
471 }
472 
473 /*
474  * exfat_init_ext_entry - initialize extension entries in a directory entry set
475  * @es:          target entry set
476  * @num_entries: number of entries excluding benign secondary entries
477  * @p_uniname:   filename to store
478  * @old_es:      optional source entry set with benign secondary entries, or NULL
479  * @num_extra:   number of benign secondary entries to copy from @old_es
480  *
481  * Set up the file, stream extension, and filename entries in @es, optionally
482  * preserving @num_extra benign secondary entries from @old_es.  @es and @old_es
483  * may refer to the same entry set; excess entries are marked as deleted.
484  */
485 void exfat_init_ext_entry(struct exfat_entry_set_cache *es, int num_entries,
486 		struct exfat_uni_name *p_uniname,
487 		struct exfat_entry_set_cache *old_es, int num_extra)
488 {
489 	int i, src_start = 0, old_num;
490 	unsigned short *uniname = p_uniname->name;
491 	struct exfat_dentry *ep;
492 
493 	if (WARN_ON(num_extra < 0 || (num_extra && (!old_es ||
494 		    old_es->num_entries < ES_IDX_FIRST_FILENAME + num_extra))))
495 		num_extra = 0;
496 
497 	/*
498 	 * Save old entry count and source position before modifying
499 	 * es->num_entries, since old_es and es may point to the same
500 	 * entry set.
501 	 */
502 	old_num = es->num_entries;
503 	if (old_es && num_extra > 0)
504 		src_start = old_es->num_entries - num_extra;
505 
506 	es->num_entries = num_entries + num_extra;
507 	ep = exfat_get_dentry_cached(es, ES_IDX_FILE);
508 	ep->dentry.file.num_ext = (unsigned char)(num_entries - 1 + num_extra);
509 
510 	ep = exfat_get_dentry_cached(es, ES_IDX_STREAM);
511 	ep->dentry.stream.name_len = p_uniname->name_len;
512 	ep->dentry.stream.name_hash = cpu_to_le16(p_uniname->name_hash);
513 
514 	if (old_es && num_extra > 0) {
515 		for (i = 0; i < num_extra; i++)
516 			*exfat_get_dentry_cached(es, num_entries + i) =
517 				*exfat_get_dentry_cached(old_es, src_start + i);
518 	}
519 
520 	for (i = ES_IDX_FIRST_FILENAME; i < num_entries; i++) {
521 		ep = exfat_get_dentry_cached(es, i);
522 		exfat_init_name_entry(ep, uniname);
523 		uniname += EXFAT_FILE_NAME_LEN;
524 	}
525 
526 	/* Mark excess old entries as deleted (in-place shrink) */
527 	for (i = num_entries + num_extra; i < old_num; i++) {
528 		ep = exfat_get_dentry_cached(es, i);
529 		exfat_set_entry_type(ep, TYPE_DELETED);
530 	}
531 
532 	exfat_update_dir_chksum(es);
533 }
534 
535 void exfat_remove_entries(struct inode *inode, struct exfat_entry_set_cache *es,
536 		int order, bool free_benign)
537 {
538 	int i;
539 	struct exfat_dentry *ep;
540 
541 	for (i = order; i < es->num_entries; i++) {
542 		ep = exfat_get_dentry_cached(es, i);
543 
544 		if (free_benign && (exfat_get_entry_type(ep) & TYPE_BENIGN_SEC))
545 			exfat_free_benign_secondary_clusters(inode, ep);
546 
547 		exfat_set_entry_type(ep, TYPE_DELETED);
548 	}
549 
550 	if (order < es->num_entries)
551 		es->modified = true;
552 }
553 
554 void exfat_update_dir_chksum(struct exfat_entry_set_cache *es)
555 {
556 	int chksum_type = CS_DIR_ENTRY, i;
557 	unsigned short chksum = 0;
558 	struct exfat_dentry *ep;
559 
560 	for (i = ES_IDX_FILE; i < es->num_entries; i++) {
561 		ep = exfat_get_dentry_cached(es, i);
562 		chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
563 					     chksum_type);
564 		chksum_type = CS_DEFAULT;
565 	}
566 	ep = exfat_get_dentry_cached(es, ES_IDX_FILE);
567 	ep->dentry.file.checksum = cpu_to_le16(chksum);
568 	es->modified = true;
569 }
570 
571 int exfat_put_dentry_set(struct exfat_entry_set_cache *es, int sync)
572 {
573 	int i, err = 0;
574 
575 	if (es->modified)
576 		err = exfat_update_bhs(es->bh, es->num_bh, sync);
577 
578 	for (i = 0; i < es->num_bh; i++)
579 		if (err)
580 			bforget(es->bh[i]);
581 		else
582 			brelse(es->bh[i]);
583 
584 	if (IS_DYNAMIC_ES(es))
585 		kfree(es->bh);
586 
587 	return err;
588 }
589 
590 static int exfat_find_location(struct super_block *sb, struct exfat_chain *p_dir,
591 			       int entry, sector_t *sector, int *offset)
592 {
593 	int ret;
594 	unsigned int off, clu = 0;
595 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
596 
597 	off = exfat_dentries_to_bytes(entry);
598 
599 	clu = p_dir->dir;
600 	ret = exfat_cluster_walk(sb, &clu, exfat_bytes_to_cluster(sbi, off),
601 			p_dir->flags);
602 	if (ret)
603 		return ret;
604 
605 	if (clu == EXFAT_EOF_CLUSTER) {
606 		exfat_fs_error(sb,
607 			"unexpected early break in cluster chain (clu : %u, len : %d)",
608 			p_dir->dir,
609 			exfat_bytes_to_cluster(sbi, off));
610 		return -EIO;
611 	}
612 
613 	if (!exfat_test_bitmap(sb, clu)) {
614 		exfat_err(sb, "failed to test cluster bit(%u)", clu);
615 		return -EIO;
616 	}
617 
618 	/* byte offset in cluster */
619 	off = exfat_cluster_offset(sbi, off);
620 
621 	/* byte offset in sector    */
622 	*offset = exfat_block_offset(sb, off);
623 
624 	/* sector offset in cluster */
625 	*sector = exfat_bytes_to_block(sb, off);
626 	*sector += exfat_cluster_to_sector(sbi, clu);
627 	return 0;
628 }
629 
630 struct exfat_dentry *exfat_get_dentry(struct super_block *sb,
631 		struct exfat_chain *p_dir, int entry, struct buffer_head **bh)
632 {
633 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
634 	unsigned int sect_per_clus = sbi->sect_per_clus;
635 	unsigned int dentries_per_page = exfat_bytes_to_dentries(PAGE_SIZE);
636 	int off;
637 	sector_t sec;
638 
639 	if (p_dir->dir == DIR_DELETED) {
640 		exfat_err(sb, "abnormal access to deleted dentry");
641 		return NULL;
642 	}
643 
644 	if (exfat_find_location(sb, p_dir, entry, &sec, &off))
645 		return NULL;
646 
647 	if (sect_per_clus > 1 &&
648 	    (entry & (dentries_per_page - 1)) == 0) {
649 		sector_t ra = sec;
650 		blkcnt_t cnt = 0;
651 		unsigned int ra_count = sect_per_clus;
652 
653 		/* Not sector aligned with ra_count, resize ra_count to page size */
654 		if ((sec - sbi->data_start_sector) & (ra_count - 1))
655 			ra_count = PAGE_SIZE >> sb->s_blocksize_bits;
656 
657 		exfat_blk_readahead(sb, sec, &ra, &cnt, sec + ra_count - 1);
658 	}
659 
660 	*bh = sb_bread(sb, sec);
661 	if (!*bh)
662 		return NULL;
663 
664 	return (struct exfat_dentry *)((*bh)->b_data + off);
665 }
666 
667 enum exfat_validate_dentry_mode {
668 	ES_MODE_GET_FILE_ENTRY,
669 	ES_MODE_GET_STRM_ENTRY,
670 	ES_MODE_GET_NAME_ENTRY,
671 	ES_MODE_GET_CRITICAL_SEC_ENTRY,
672 	ES_MODE_GET_BENIGN_SEC_ENTRY,
673 };
674 
675 static bool exfat_validate_entry(unsigned int type,
676 		enum exfat_validate_dentry_mode *mode)
677 {
678 	if (type == TYPE_UNUSED || type == TYPE_DELETED)
679 		return false;
680 
681 	switch (*mode) {
682 	case ES_MODE_GET_FILE_ENTRY:
683 		if (type != TYPE_STREAM)
684 			return false;
685 		*mode = ES_MODE_GET_STRM_ENTRY;
686 		break;
687 	case ES_MODE_GET_STRM_ENTRY:
688 		if (type != TYPE_EXTEND)
689 			return false;
690 		*mode = ES_MODE_GET_NAME_ENTRY;
691 		break;
692 	case ES_MODE_GET_NAME_ENTRY:
693 		if (type & TYPE_BENIGN_SEC)
694 			*mode = ES_MODE_GET_BENIGN_SEC_ENTRY;
695 		else if (type != TYPE_EXTEND)
696 			return false;
697 		break;
698 	case ES_MODE_GET_BENIGN_SEC_ENTRY:
699 		/* Assume unreconized benign secondary entry */
700 		if (!(type & TYPE_BENIGN_SEC))
701 			return false;
702 		break;
703 	default:
704 		return false;
705 	}
706 
707 	return true;
708 }
709 
710 struct exfat_dentry *exfat_get_dentry_cached(
711 	struct exfat_entry_set_cache *es, int num)
712 {
713 	int off = es->start_off + num * DENTRY_SIZE;
714 	struct buffer_head *bh = es->bh[exfat_bytes_to_block(es->sb, off)];
715 	char *p = bh->b_data + exfat_block_offset(es->sb, off);
716 
717 	return (struct exfat_dentry *)p;
718 }
719 
720 /*
721  * Returns a set of dentries.
722  *
723  * Note It provides a direct pointer to bh->data via exfat_get_dentry_cached().
724  * User should call exfat_get_dentry_set() after setting 'modified' to apply
725  * changes made in this entry set to the real device.
726  *
727  * in:
728  *   sb+p_dir+entry: indicates a file/dir
729  *   num_entries: specifies how many dentries should be included.
730  *                It will be set to es->num_entries if it is not 0.
731  *                If num_entries is 0, es->num_entries will be obtained
732  *                from the first dentry.
733  * out:
734  *   es: pointer of entry set on success.
735  * return:
736  *   0 on success
737  *   -error code on failure
738  */
739 static int __exfat_get_dentry_set(struct exfat_entry_set_cache *es,
740 		struct super_block *sb, struct exfat_chain *p_dir, int entry,
741 		unsigned int num_entries)
742 {
743 	int ret, i, num_bh;
744 	unsigned int off;
745 	sector_t sec;
746 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
747 	struct buffer_head *bh;
748 
749 	if (p_dir->dir == DIR_DELETED) {
750 		exfat_err(sb, "access to deleted dentry");
751 		return -EIO;
752 	}
753 
754 	ret = exfat_find_location(sb, p_dir, entry, &sec, &off);
755 	if (ret)
756 		return ret;
757 
758 	memset(es, 0, sizeof(*es));
759 	es->sb = sb;
760 	es->modified = false;
761 	es->start_off = off;
762 	es->bh = es->__bh;
763 
764 	bh = sb_bread(sb, sec);
765 	if (!bh)
766 		return -EIO;
767 	es->bh[es->num_bh++] = bh;
768 
769 	if (num_entries == ES_ALL_ENTRIES) {
770 		struct exfat_dentry *ep;
771 
772 		ep = exfat_get_dentry_cached(es, ES_IDX_FILE);
773 		if (ep->type != EXFAT_FILE) {
774 			brelse(bh);
775 			return -EIO;
776 		}
777 
778 		num_entries = ep->dentry.file.num_ext + 1;
779 	}
780 
781 	es->num_entries = num_entries;
782 
783 	num_bh = exfat_bytes_to_block_round_up(sb, off + num_entries * DENTRY_SIZE);
784 	if (num_bh > ARRAY_SIZE(es->__bh)) {
785 		es->bh = kmalloc_objs(*es->bh, num_bh, GFP_NOFS);
786 		if (!es->bh) {
787 			brelse(bh);
788 			return -ENOMEM;
789 		}
790 		es->bh[0] = bh;
791 	}
792 
793 	for (i = 1; i < num_bh; i++) {
794 		/* get the next sector */
795 		if (exfat_is_last_sector_in_cluster(sbi, sec)) {
796 			unsigned int clu = exfat_sector_to_cluster(sbi, sec);
797 
798 			if (exfat_cluster_walk(sb, &clu, 1, p_dir->flags))
799 				goto put_es;
800 			sec = exfat_cluster_to_sector(sbi, clu);
801 		} else {
802 			sec++;
803 		}
804 
805 		bh = sb_bread(sb, sec);
806 		if (!bh)
807 			goto put_es;
808 		es->bh[es->num_bh++] = bh;
809 	}
810 
811 	return 0;
812 
813 put_es:
814 	exfat_put_dentry_set(es, false);
815 	return -EIO;
816 }
817 
818 int exfat_get_dentry_set(struct exfat_entry_set_cache *es,
819 		struct super_block *sb, struct exfat_chain *p_dir,
820 		int entry, unsigned int num_entries)
821 {
822 	int ret, i;
823 	struct exfat_dentry *ep;
824 	enum exfat_validate_dentry_mode mode = ES_MODE_GET_FILE_ENTRY;
825 
826 	ret = __exfat_get_dentry_set(es, sb, p_dir, entry, num_entries);
827 	if (ret < 0)
828 		return ret;
829 
830 	/* validate cached dentries */
831 	for (i = ES_IDX_STREAM; i < es->num_entries; i++) {
832 		ep = exfat_get_dentry_cached(es, i);
833 		if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
834 			goto put_es;
835 	}
836 	return 0;
837 
838 put_es:
839 	exfat_put_dentry_set(es, false);
840 	return -EIO;
841 }
842 
843 static int exfat_validate_empty_dentry_set(struct exfat_entry_set_cache *es)
844 {
845 	struct exfat_dentry *ep;
846 	struct buffer_head *bh;
847 	int i, off;
848 	bool unused_hit = false;
849 
850 	/*
851 	 * ONLY UNUSED OR DELETED DENTRIES ARE ALLOWED:
852 	 * Although it violates the specification for a deleted entry to
853 	 * follow an unused entry, some exFAT implementations could work
854 	 * like this. Therefore, to improve compatibility, let's allow it.
855 	 */
856 	for (i = 0; i < es->num_entries; i++) {
857 		ep = exfat_get_dentry_cached(es, i);
858 		if (ep->type == EXFAT_UNUSED) {
859 			unused_hit = true;
860 		} else if (!IS_EXFAT_DELETED(ep->type)) {
861 			if (unused_hit)
862 				goto err_used_follow_unused;
863 			i++;
864 			goto count_skip_entries;
865 		}
866 	}
867 
868 	return 0;
869 
870 err_used_follow_unused:
871 	off = es->start_off + (i << DENTRY_SIZE_BITS);
872 	bh = es->bh[exfat_bytes_to_block(es->sb, off)];
873 
874 	exfat_fs_error(es->sb,
875 		"in sector %lld, dentry %d should be unused, but 0x%x",
876 		bh->b_blocknr, off >> DENTRY_SIZE_BITS, ep->type);
877 
878 	return -EIO;
879 
880 count_skip_entries:
881 	es->num_entries =
882 		exfat_bytes_to_dentries(exfat_block_to_bytes(es->sb, es->num_bh) - es->start_off);
883 	for (; i < es->num_entries; i++) {
884 		ep = exfat_get_dentry_cached(es, i);
885 		if (IS_EXFAT_DELETED(ep->type))
886 			break;
887 	}
888 
889 	return i;
890 }
891 
892 /*
893  * Get an empty dentry set.
894  *
895  * in:
896  *   sb+p_dir+entry: indicates the empty dentry location
897  *   num_entries: specifies how many empty dentries should be included.
898  * out:
899  *   es: pointer of empty dentry set on success.
900  * return:
901  *   0  : on success
902  *   >0 : the dentries are not empty, the return value is the number of
903  *        dentries to be skipped for the next lookup.
904  *   <0 : on failure
905  */
906 int exfat_get_empty_dentry_set(struct exfat_entry_set_cache *es,
907 		struct super_block *sb, struct exfat_chain *p_dir,
908 		int entry, unsigned int num_entries)
909 {
910 	int ret;
911 
912 	ret = __exfat_get_dentry_set(es, sb, p_dir, entry, num_entries);
913 	if (ret < 0)
914 		return ret;
915 
916 	ret = exfat_validate_empty_dentry_set(es);
917 	if (ret)
918 		exfat_put_dentry_set(es, false);
919 
920 	return ret;
921 }
922 
923 static inline void exfat_reset_empty_hint(struct exfat_hint_femp *hint_femp)
924 {
925 	hint_femp->eidx = EXFAT_HINT_NONE;
926 	hint_femp->count = 0;
927 }
928 
929 static inline void exfat_set_empty_hint(struct exfat_inode_info *ei,
930 		struct exfat_hint_femp *candi_empty, struct exfat_chain *clu,
931 		int dentry, int num_entries, int entry_type)
932 {
933 	if (ei->hint_femp.eidx == EXFAT_HINT_NONE ||
934 	    ei->hint_femp.eidx > dentry) {
935 		int total_entries = exfat_bytes_to_dentries(i_size_read(&ei->vfs_inode));
936 
937 		if (candi_empty->count == 0) {
938 			candi_empty->cur = *clu;
939 			candi_empty->eidx = dentry;
940 		}
941 
942 		if (entry_type == TYPE_UNUSED)
943 			candi_empty->count += total_entries - dentry;
944 		else
945 			candi_empty->count++;
946 
947 		if (candi_empty->count == num_entries ||
948 		    candi_empty->count + candi_empty->eidx == total_entries)
949 			ei->hint_femp = *candi_empty;
950 	}
951 }
952 
953 enum {
954 	DIRENT_STEP_FILE,
955 	DIRENT_STEP_STRM,
956 	DIRENT_STEP_NAME,
957 	DIRENT_STEP_SECD,
958 };
959 
960 /*
961  * @ei:         inode info of parent directory
962  * @p_dir:      directory structure of parent directory
963  * @num_entries:entry size of p_uniname
964  * @hint_opt:   If p_uniname is found, filled with optimized dir/entry
965  *              for traversing cluster chain.
966  * @return:
967  *   >= 0:      file directory entry position where the name exists
968  *   -ENOENT:   entry with the name does not exist
969  *   -EIO:      I/O error
970  */
971 int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
972 		struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
973 		struct exfat_hint *hint_opt)
974 {
975 	int i, rewind = 0, dentry = 0, end_eidx = 0, num_ext = 0, len;
976 	int order, step, name_len = 0;
977 	int dentries_per_clu;
978 	unsigned int entry_type;
979 	unsigned short *uniname = NULL;
980 	struct exfat_chain clu;
981 	struct exfat_hint *hint_stat = &ei->hint_stat;
982 	struct exfat_hint_femp candi_empty;
983 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
984 	int num_entries = exfat_calc_num_entries(p_uniname);
985 	unsigned int clu_count = 0;
986 
987 	if (num_entries < 0)
988 		return num_entries;
989 
990 	dentries_per_clu = sbi->dentries_per_clu;
991 
992 	exfat_chain_dup(&clu, p_dir);
993 
994 	if (hint_stat->eidx) {
995 		clu.dir = hint_stat->clu;
996 		dentry = hint_stat->eidx;
997 		end_eidx = dentry;
998 	}
999 
1000 	exfat_reset_empty_hint(&ei->hint_femp);
1001 
1002 rewind:
1003 	order = 0;
1004 	step = DIRENT_STEP_FILE;
1005 	exfat_reset_empty_hint(&candi_empty);
1006 
1007 	while (clu.dir != EXFAT_EOF_CLUSTER) {
1008 		i = dentry & (dentries_per_clu - 1);
1009 		for (; i < dentries_per_clu; i++, dentry++) {
1010 			struct exfat_dentry *ep;
1011 			struct buffer_head *bh;
1012 
1013 			if (rewind && dentry == end_eidx)
1014 				goto not_found;
1015 
1016 			ep = exfat_get_dentry(sb, &clu, i, &bh);
1017 			if (!ep)
1018 				return -EIO;
1019 
1020 			entry_type = exfat_get_entry_type(ep);
1021 
1022 			if (entry_type == TYPE_UNUSED ||
1023 			    entry_type == TYPE_DELETED) {
1024 				step = DIRENT_STEP_FILE;
1025 
1026 				exfat_set_empty_hint(ei, &candi_empty, &clu,
1027 						dentry, num_entries,
1028 						entry_type);
1029 
1030 				brelse(bh);
1031 				if (entry_type == TYPE_UNUSED)
1032 					goto not_found;
1033 				continue;
1034 			}
1035 
1036 			exfat_reset_empty_hint(&candi_empty);
1037 
1038 			if (entry_type == TYPE_FILE || entry_type == TYPE_DIR) {
1039 				step = DIRENT_STEP_FILE;
1040 				hint_opt->clu = clu.dir;
1041 				hint_opt->eidx = i;
1042 				num_ext = ep->dentry.file.num_ext;
1043 				step = DIRENT_STEP_STRM;
1044 				brelse(bh);
1045 				continue;
1046 			}
1047 
1048 			if (entry_type == TYPE_STREAM) {
1049 				u16 name_hash;
1050 
1051 				if (step != DIRENT_STEP_STRM) {
1052 					step = DIRENT_STEP_FILE;
1053 					brelse(bh);
1054 					continue;
1055 				}
1056 				step = DIRENT_STEP_FILE;
1057 				name_hash = le16_to_cpu(
1058 						ep->dentry.stream.name_hash);
1059 				if (p_uniname->name_hash == name_hash &&
1060 				    p_uniname->name_len ==
1061 						ep->dentry.stream.name_len) {
1062 					step = DIRENT_STEP_NAME;
1063 					order = 1;
1064 					name_len = 0;
1065 				}
1066 				brelse(bh);
1067 				continue;
1068 			}
1069 
1070 			if (entry_type == TYPE_EXTEND) {
1071 				unsigned short entry_uniname[16], unichar;
1072 				unsigned int offset;
1073 
1074 				if (step != DIRENT_STEP_NAME ||
1075 				    name_len >= MAX_NAME_LENGTH) {
1076 					brelse(bh);
1077 					step = DIRENT_STEP_FILE;
1078 					continue;
1079 				}
1080 
1081 				offset = (++order - 2) * EXFAT_FILE_NAME_LEN;
1082 				len = exfat_extract_uni_name(ep, entry_uniname);
1083 				brelse(bh);
1084 				if (offset > MAX_NAME_LENGTH ||
1085 				    len > MAX_NAME_LENGTH - offset) {
1086 					step = DIRENT_STEP_FILE;
1087 					continue;
1088 				}
1089 				uniname = p_uniname->name + offset;
1090 				name_len += len;
1091 
1092 				unichar = *(uniname+len);
1093 				*(uniname+len) = 0x0;
1094 
1095 				if (exfat_uniname_ncmp(sb, uniname,
1096 					entry_uniname, len)) {
1097 					step = DIRENT_STEP_FILE;
1098 				} else if (p_uniname->name_len == name_len) {
1099 					if (order == num_ext)
1100 						goto found;
1101 					step = DIRENT_STEP_SECD;
1102 				}
1103 
1104 				*(uniname+len) = unichar;
1105 				continue;
1106 			}
1107 
1108 			brelse(bh);
1109 			if (entry_type &
1110 					(TYPE_CRITICAL_SEC | TYPE_BENIGN_SEC)) {
1111 				if (step == DIRENT_STEP_SECD) {
1112 					if (++order == num_ext)
1113 						goto found;
1114 					continue;
1115 				}
1116 			}
1117 			step = DIRENT_STEP_FILE;
1118 		}
1119 
1120 		if (exfat_chain_advance(sb, &clu, 1))
1121 			return -EIO;
1122 
1123 		/* break if the cluster chain includes a loop */
1124 		if (unlikely(++clu_count > EXFAT_DATA_CLUSTER_COUNT(sbi)))
1125 			goto not_found;
1126 	}
1127 
1128 not_found:
1129 	/*
1130 	 * We started at not 0 index,so we should try to find target
1131 	 * from 0 index to the index we started at.
1132 	 */
1133 	if (!rewind && end_eidx) {
1134 		rewind = 1;
1135 		dentry = 0;
1136 		clu.dir = p_dir->dir;
1137 		goto rewind;
1138 	}
1139 
1140 	/*
1141 	 * set the EXFAT_EOF_CLUSTER flag to avoid search
1142 	 * from the beginning again when allocated a new cluster
1143 	 */
1144 	if (ei->hint_femp.eidx == EXFAT_HINT_NONE) {
1145 		ei->hint_femp.cur.dir = EXFAT_EOF_CLUSTER;
1146 		ei->hint_femp.eidx = p_dir->size * dentries_per_clu;
1147 		ei->hint_femp.count = 0;
1148 	}
1149 
1150 	/* initialized hint_stat */
1151 	hint_stat->clu = p_dir->dir;
1152 	hint_stat->eidx = 0;
1153 	return -ENOENT;
1154 
1155 found:
1156 	/* next dentry we'll find is out of this cluster */
1157 	if (!((dentry + 1) & (dentries_per_clu - 1))) {
1158 		int ret = 0;
1159 
1160 		ret = exfat_chain_advance(sb, &clu, 1);
1161 
1162 		if (ret || clu.dir == EXFAT_EOF_CLUSTER) {
1163 			/* just initialized hint_stat */
1164 			hint_stat->clu = p_dir->dir;
1165 			hint_stat->eidx = 0;
1166 			return (dentry - num_ext);
1167 		}
1168 	}
1169 
1170 	hint_stat->clu = clu.dir;
1171 	hint_stat->eidx = dentry + 1;
1172 	return dentry - num_ext;
1173 }
1174 
1175 int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
1176 {
1177 	int i, count = 0;
1178 	int dentries_per_clu;
1179 	unsigned int entry_type;
1180 	unsigned int clu_count = 0;
1181 	struct exfat_chain clu;
1182 	struct exfat_dentry *ep;
1183 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
1184 	struct buffer_head *bh;
1185 
1186 	dentries_per_clu = sbi->dentries_per_clu;
1187 
1188 	exfat_chain_dup(&clu, p_dir);
1189 
1190 	while (clu.dir != EXFAT_EOF_CLUSTER) {
1191 		for (i = 0; i < dentries_per_clu; i++) {
1192 			ep = exfat_get_dentry(sb, &clu, i, &bh);
1193 			if (!ep)
1194 				return -EIO;
1195 			entry_type = exfat_get_entry_type(ep);
1196 			brelse(bh);
1197 
1198 			if (entry_type == TYPE_UNUSED)
1199 				return count;
1200 			if (entry_type != TYPE_DIR)
1201 				continue;
1202 			count++;
1203 		}
1204 
1205 		if (exfat_chain_advance(sb, &clu, 1))
1206 			return -EIO;
1207 
1208 		if (unlikely(++clu_count > sbi->used_clusters)) {
1209 			exfat_fs_error(sb, "FAT or bitmap is corrupted");
1210 			return -EIO;
1211 		}
1212 	}
1213 
1214 	return count;
1215 }
1216 
1217 static int exfat_get_volume_label_dentry(struct super_block *sb,
1218 		struct exfat_entry_set_cache *es)
1219 {
1220 	int i;
1221 	int dentry = 0;
1222 	unsigned int type;
1223 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
1224 	struct exfat_hint_femp hint_femp;
1225 	struct exfat_inode_info *ei = EXFAT_I(sb->s_root->d_inode);
1226 	struct exfat_chain clu;
1227 	struct exfat_dentry *ep;
1228 	struct buffer_head *bh;
1229 
1230 	hint_femp.eidx = EXFAT_HINT_NONE;
1231 	exfat_chain_set(&clu, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
1232 
1233 	while (clu.dir != EXFAT_EOF_CLUSTER) {
1234 		for (i = 0; i < sbi->dentries_per_clu; i++, dentry++) {
1235 			ep = exfat_get_dentry(sb, &clu, i, &bh);
1236 			if (!ep)
1237 				return -EIO;
1238 
1239 			type = exfat_get_entry_type(ep);
1240 			if (hint_femp.eidx == EXFAT_HINT_NONE) {
1241 				if (type == TYPE_DELETED || type == TYPE_UNUSED) {
1242 					hint_femp.cur = clu;
1243 					hint_femp.eidx = dentry;
1244 					hint_femp.count = 1;
1245 				}
1246 			}
1247 
1248 			if (type == TYPE_UNUSED) {
1249 				brelse(bh);
1250 				goto not_found;
1251 			}
1252 
1253 			if (type != TYPE_VOLUME) {
1254 				brelse(bh);
1255 				continue;
1256 			}
1257 
1258 			memset(es, 0, sizeof(*es));
1259 			es->sb = sb;
1260 			es->bh = es->__bh;
1261 			es->bh[0] = bh;
1262 			es->num_bh = 1;
1263 			es->start_off = exfat_dentries_to_bytes(i) % sb->s_blocksize;
1264 
1265 			return 0;
1266 		}
1267 
1268 		if (exfat_get_next_cluster(sb, &(clu.dir)))
1269 			return -EIO;
1270 	}
1271 
1272 not_found:
1273 	if (hint_femp.eidx == EXFAT_HINT_NONE) {
1274 		hint_femp.cur.dir = EXFAT_EOF_CLUSTER;
1275 		hint_femp.eidx = dentry;
1276 		hint_femp.count = 0;
1277 	}
1278 
1279 	ei->hint_femp = hint_femp;
1280 
1281 	return -ENOENT;
1282 }
1283 
1284 int exfat_read_volume_label(struct super_block *sb, struct exfat_uni_name *label_out)
1285 {
1286 	int ret, i;
1287 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
1288 	struct exfat_entry_set_cache es;
1289 	struct exfat_dentry *ep;
1290 
1291 	mutex_lock(&sbi->s_lock);
1292 
1293 	memset(label_out, 0, sizeof(*label_out));
1294 	ret = exfat_get_volume_label_dentry(sb, &es);
1295 	if (ret < 0) {
1296 		/*
1297 		 * ENOENT signifies that a volume label dentry doesn't exist
1298 		 * We will treat this as an empty volume label and not fail.
1299 		 */
1300 		if (ret == -ENOENT)
1301 			ret = 0;
1302 
1303 		goto unlock;
1304 	}
1305 
1306 	ep = exfat_get_dentry_cached(&es, 0);
1307 	label_out->name_len = ep->dentry.volume_label.char_count;
1308 	if (label_out->name_len > EXFAT_VOLUME_LABEL_LEN) {
1309 		ret = -EIO;
1310 		exfat_put_dentry_set(&es, false);
1311 		goto unlock;
1312 	}
1313 
1314 	for (i = 0; i < label_out->name_len; i++)
1315 		label_out->name[i] = le16_to_cpu(ep->dentry.volume_label.volume_label[i]);
1316 
1317 	exfat_put_dentry_set(&es, false);
1318 unlock:
1319 	mutex_unlock(&sbi->s_lock);
1320 	return ret;
1321 }
1322 
1323 int exfat_write_volume_label(struct super_block *sb,
1324 			     struct exfat_uni_name *label)
1325 {
1326 	int ret, i;
1327 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
1328 	struct inode *root_inode = sb->s_root->d_inode;
1329 	struct exfat_entry_set_cache es;
1330 	struct exfat_chain clu;
1331 	struct exfat_dentry *ep;
1332 
1333 	if (label->name_len > EXFAT_VOLUME_LABEL_LEN)
1334 		return -EINVAL;
1335 
1336 	mutex_lock(&sbi->s_lock);
1337 
1338 	ret = exfat_get_volume_label_dentry(sb, &es);
1339 	if (ret == -ENOENT) {
1340 		if (label->name_len == 0) {
1341 			/* No volume label dentry, no need to clear */
1342 			ret = 0;
1343 			goto unlock;
1344 		}
1345 
1346 		ret = exfat_find_empty_entry(root_inode, &clu, 1, &es);
1347 	}
1348 
1349 	if (ret < 0)
1350 		goto unlock;
1351 
1352 	ep = exfat_get_dentry_cached(&es, 0);
1353 
1354 	if (label->name_len == 0 && ep->dentry.volume_label.char_count == 0) {
1355 		/* volume label had been cleared */
1356 		exfat_put_dentry_set(&es, 0);
1357 		goto unlock;
1358 	}
1359 
1360 	memset(ep, 0, sizeof(*ep));
1361 	ep->type = EXFAT_VOLUME;
1362 
1363 	for (i = 0; i < label->name_len; i++)
1364 		ep->dentry.volume_label.volume_label[i] =
1365 			cpu_to_le16(label->name[i]);
1366 
1367 	ep->dentry.volume_label.char_count = label->name_len;
1368 	es.modified = true;
1369 
1370 	ret = exfat_put_dentry_set(&es, IS_DIRSYNC(root_inode));
1371 
1372 unlock:
1373 	mutex_unlock(&sbi->s_lock);
1374 	return ret;
1375 }
1376