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