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