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