xref: /openbmc/linux/fs/exfat/dir.c (revision ce746d43)
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(sb, 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(sb, 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(sb, 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(sb, 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(sb, 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(sb, 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(sb, 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 void exfat_free_dentry_set(struct exfat_entry_set_cache *es, int sync)
608 {
609 	int i;
610 
611 	for (i = 0; i < es->num_bh; i++) {
612 		if (es->modified)
613 			exfat_update_bh(es->sb, es->bh[i], sync);
614 		brelse(es->bh[i]);
615 	}
616 	kfree(es);
617 }
618 
619 static int exfat_walk_fat_chain(struct super_block *sb,
620 		struct exfat_chain *p_dir, unsigned int byte_offset,
621 		unsigned int *clu)
622 {
623 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
624 	unsigned int clu_offset;
625 	unsigned int cur_clu;
626 
627 	clu_offset = EXFAT_B_TO_CLU(byte_offset, sbi);
628 	cur_clu = p_dir->dir;
629 
630 	if (p_dir->flags == ALLOC_NO_FAT_CHAIN) {
631 		cur_clu += clu_offset;
632 	} else {
633 		while (clu_offset > 0) {
634 			if (exfat_get_next_cluster(sb, &cur_clu))
635 				return -EIO;
636 			if (cur_clu == EXFAT_EOF_CLUSTER) {
637 				exfat_fs_error(sb,
638 					"invalid dentry access beyond EOF (clu : %u, eidx : %d)",
639 					p_dir->dir,
640 					EXFAT_B_TO_DEN(byte_offset));
641 				return -EIO;
642 			}
643 			clu_offset--;
644 		}
645 	}
646 
647 	*clu = cur_clu;
648 	return 0;
649 }
650 
651 int exfat_find_location(struct super_block *sb, struct exfat_chain *p_dir,
652 		int entry, sector_t *sector, int *offset)
653 {
654 	int ret;
655 	unsigned int off, clu = 0;
656 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
657 
658 	off = EXFAT_DEN_TO_B(entry);
659 
660 	ret = exfat_walk_fat_chain(sb, p_dir, off, &clu);
661 	if (ret)
662 		return ret;
663 
664 	/* byte offset in cluster */
665 	off = EXFAT_CLU_OFFSET(off, sbi);
666 
667 	/* byte offset in sector    */
668 	*offset = EXFAT_BLK_OFFSET(off, sb);
669 
670 	/* sector offset in cluster */
671 	*sector = EXFAT_B_TO_BLK(off, sb);
672 	*sector += exfat_cluster_to_sector(sbi, clu);
673 	return 0;
674 }
675 
676 #define EXFAT_MAX_RA_SIZE     (128*1024)
677 static int exfat_dir_readahead(struct super_block *sb, sector_t sec)
678 {
679 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
680 	struct buffer_head *bh;
681 	unsigned int max_ra_count = EXFAT_MAX_RA_SIZE >> sb->s_blocksize_bits;
682 	unsigned int page_ra_count = PAGE_SIZE >> sb->s_blocksize_bits;
683 	unsigned int adj_ra_count = max(sbi->sect_per_clus, page_ra_count);
684 	unsigned int ra_count = min(adj_ra_count, max_ra_count);
685 
686 	/* Read-ahead is not required */
687 	if (sbi->sect_per_clus == 1)
688 		return 0;
689 
690 	if (sec < sbi->data_start_sector) {
691 		exfat_err(sb, "requested sector is invalid(sect:%llu, root:%llu)",
692 			  (unsigned long long)sec, sbi->data_start_sector);
693 		return -EIO;
694 	}
695 
696 	/* Not sector aligned with ra_count, resize ra_count to page size */
697 	if ((sec - sbi->data_start_sector) & (ra_count - 1))
698 		ra_count = page_ra_count;
699 
700 	bh = sb_find_get_block(sb, sec);
701 	if (!bh || !buffer_uptodate(bh)) {
702 		unsigned int i;
703 
704 		for (i = 0; i < ra_count; i++)
705 			sb_breadahead(sb, (sector_t)(sec + i));
706 	}
707 	brelse(bh);
708 	return 0;
709 }
710 
711 struct exfat_dentry *exfat_get_dentry(struct super_block *sb,
712 		struct exfat_chain *p_dir, int entry, struct buffer_head **bh,
713 		sector_t *sector)
714 {
715 	unsigned int dentries_per_page = EXFAT_B_TO_DEN(PAGE_SIZE);
716 	int off;
717 	sector_t sec;
718 
719 	if (p_dir->dir == DIR_DELETED) {
720 		exfat_err(sb, "abnormal access to deleted dentry");
721 		return NULL;
722 	}
723 
724 	if (exfat_find_location(sb, p_dir, entry, &sec, &off))
725 		return NULL;
726 
727 	if (p_dir->dir != EXFAT_FREE_CLUSTER &&
728 			!(entry & (dentries_per_page - 1)))
729 		exfat_dir_readahead(sb, sec);
730 
731 	*bh = sb_bread(sb, sec);
732 	if (!*bh)
733 		return NULL;
734 
735 	if (sector)
736 		*sector = sec;
737 	return (struct exfat_dentry *)((*bh)->b_data + off);
738 }
739 
740 enum exfat_validate_dentry_mode {
741 	ES_MODE_STARTED,
742 	ES_MODE_GET_FILE_ENTRY,
743 	ES_MODE_GET_STRM_ENTRY,
744 	ES_MODE_GET_NAME_ENTRY,
745 	ES_MODE_GET_CRITICAL_SEC_ENTRY,
746 };
747 
748 static bool exfat_validate_entry(unsigned int type,
749 		enum exfat_validate_dentry_mode *mode)
750 {
751 	if (type == TYPE_UNUSED || type == TYPE_DELETED)
752 		return false;
753 
754 	switch (*mode) {
755 	case ES_MODE_STARTED:
756 		if  (type != TYPE_FILE && type != TYPE_DIR)
757 			return false;
758 		*mode = ES_MODE_GET_FILE_ENTRY;
759 		return true;
760 	case ES_MODE_GET_FILE_ENTRY:
761 		if (type != TYPE_STREAM)
762 			return false;
763 		*mode = ES_MODE_GET_STRM_ENTRY;
764 		return true;
765 	case ES_MODE_GET_STRM_ENTRY:
766 		if (type != TYPE_EXTEND)
767 			return false;
768 		*mode = ES_MODE_GET_NAME_ENTRY;
769 		return true;
770 	case ES_MODE_GET_NAME_ENTRY:
771 		if (type == TYPE_STREAM)
772 			return false;
773 		if (type != TYPE_EXTEND) {
774 			if (!(type & TYPE_CRITICAL_SEC))
775 				return false;
776 			*mode = ES_MODE_GET_CRITICAL_SEC_ENTRY;
777 		}
778 		return true;
779 	case ES_MODE_GET_CRITICAL_SEC_ENTRY:
780 		if (type == TYPE_EXTEND || type == TYPE_STREAM)
781 			return false;
782 		if ((type & TYPE_CRITICAL_SEC) != TYPE_CRITICAL_SEC)
783 			return false;
784 		return true;
785 	default:
786 		WARN_ON_ONCE(1);
787 		return false;
788 	}
789 }
790 
791 struct exfat_dentry *exfat_get_dentry_cached(
792 	struct exfat_entry_set_cache *es, int num)
793 {
794 	int off = es->start_off + num * DENTRY_SIZE;
795 	struct buffer_head *bh = es->bh[EXFAT_B_TO_BLK(off, es->sb)];
796 	char *p = bh->b_data + EXFAT_BLK_OFFSET(off, es->sb);
797 
798 	return (struct exfat_dentry *)p;
799 }
800 
801 /*
802  * Returns a set of dentries for a file or dir.
803  *
804  * Note It provides a direct pointer to bh->data via exfat_get_dentry_cached().
805  * User should call exfat_get_dentry_set() after setting 'modified' to apply
806  * changes made in this entry set to the real device.
807  *
808  * in:
809  *   sb+p_dir+entry: indicates a file/dir
810  *   type:  specifies how many dentries should be included.
811  * return:
812  *   pointer of entry set on success,
813  *   NULL on failure.
814  */
815 struct exfat_entry_set_cache *exfat_get_dentry_set(struct super_block *sb,
816 		struct exfat_chain *p_dir, int entry, unsigned int type)
817 {
818 	int ret, i, num_bh;
819 	unsigned int off, byte_offset, clu = 0;
820 	sector_t sec;
821 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
822 	struct exfat_entry_set_cache *es;
823 	struct exfat_dentry *ep;
824 	int num_entries;
825 	enum exfat_validate_dentry_mode mode = ES_MODE_STARTED;
826 	struct buffer_head *bh;
827 
828 	if (p_dir->dir == DIR_DELETED) {
829 		exfat_err(sb, "access to deleted dentry");
830 		return NULL;
831 	}
832 
833 	byte_offset = EXFAT_DEN_TO_B(entry);
834 	ret = exfat_walk_fat_chain(sb, p_dir, byte_offset, &clu);
835 	if (ret)
836 		return NULL;
837 
838 	es = kzalloc(sizeof(*es), GFP_KERNEL);
839 	if (!es)
840 		return NULL;
841 	es->sb = sb;
842 	es->modified = false;
843 
844 	/* byte offset in cluster */
845 	byte_offset = EXFAT_CLU_OFFSET(byte_offset, sbi);
846 
847 	/* byte offset in sector */
848 	off = EXFAT_BLK_OFFSET(byte_offset, sb);
849 	es->start_off = off;
850 
851 	/* sector offset in cluster */
852 	sec = EXFAT_B_TO_BLK(byte_offset, sb);
853 	sec += exfat_cluster_to_sector(sbi, clu);
854 
855 	bh = sb_bread(sb, sec);
856 	if (!bh)
857 		goto free_es;
858 	es->bh[es->num_bh++] = bh;
859 
860 	ep = exfat_get_dentry_cached(es, 0);
861 	if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
862 		goto free_es;
863 
864 	num_entries = type == ES_ALL_ENTRIES ?
865 		ep->dentry.file.num_ext + 1 : type;
866 	es->num_entries = num_entries;
867 
868 	num_bh = EXFAT_B_TO_BLK_ROUND_UP(off + num_entries * DENTRY_SIZE, sb);
869 	for (i = 1; i < num_bh; i++) {
870 		/* get the next sector */
871 		if (exfat_is_last_sector_in_cluster(sbi, sec)) {
872 			if (p_dir->flags == ALLOC_NO_FAT_CHAIN)
873 				clu++;
874 			else if (exfat_get_next_cluster(sb, &clu))
875 				goto free_es;
876 			sec = exfat_cluster_to_sector(sbi, clu);
877 		} else {
878 			sec++;
879 		}
880 
881 		bh = sb_bread(sb, sec);
882 		if (!bh)
883 			goto free_es;
884 		es->bh[es->num_bh++] = bh;
885 	}
886 
887 	/* validiate cached dentries */
888 	for (i = 1; i < num_entries; i++) {
889 		ep = exfat_get_dentry_cached(es, i);
890 		if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
891 			goto free_es;
892 	}
893 	return es;
894 
895 free_es:
896 	exfat_free_dentry_set(es, false);
897 	return NULL;
898 }
899 
900 enum {
901 	DIRENT_STEP_FILE,
902 	DIRENT_STEP_STRM,
903 	DIRENT_STEP_NAME,
904 	DIRENT_STEP_SECD,
905 };
906 
907 /*
908  * return values:
909  *   >= 0	: return dir entiry position with the name in dir
910  *   -EEXIST	: (root dir, ".") it is the root dir itself
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 						memcpy(&ei->hint_femp,
980 							&candi_empty,
981 							sizeof(candi_empty));
982 					}
983 				}
984 
985 				brelse(bh);
986 				if (entry_type == TYPE_UNUSED)
987 					goto not_found;
988 				continue;
989 			}
990 
991 			num_empty = 0;
992 			candi_empty.eidx = EXFAT_HINT_NONE;
993 
994 			if (entry_type == TYPE_FILE || entry_type == TYPE_DIR) {
995 				step = DIRENT_STEP_FILE;
996 				if (type == TYPE_ALL || type == entry_type) {
997 					num_ext = ep->dentry.file.num_ext;
998 					step = DIRENT_STEP_STRM;
999 				}
1000 				brelse(bh);
1001 				continue;
1002 			}
1003 
1004 			if (entry_type == TYPE_STREAM) {
1005 				u16 name_hash;
1006 
1007 				if (step != DIRENT_STEP_STRM) {
1008 					step = DIRENT_STEP_FILE;
1009 					brelse(bh);
1010 					continue;
1011 				}
1012 				step = DIRENT_STEP_FILE;
1013 				name_hash = le16_to_cpu(
1014 						ep->dentry.stream.name_hash);
1015 				if (p_uniname->name_hash == name_hash &&
1016 				    p_uniname->name_len ==
1017 						ep->dentry.stream.name_len) {
1018 					step = DIRENT_STEP_NAME;
1019 					order = 1;
1020 					name_len = 0;
1021 				}
1022 				brelse(bh);
1023 				continue;
1024 			}
1025 
1026 			brelse(bh);
1027 			if (entry_type == TYPE_EXTEND) {
1028 				unsigned short entry_uniname[16], unichar;
1029 
1030 				if (step != DIRENT_STEP_NAME) {
1031 					step = DIRENT_STEP_FILE;
1032 					continue;
1033 				}
1034 
1035 				if (++order == 2)
1036 					uniname = p_uniname->name;
1037 				else
1038 					uniname += EXFAT_FILE_NAME_LEN;
1039 
1040 				len = exfat_extract_uni_name(ep, entry_uniname);
1041 				name_len += len;
1042 
1043 				unichar = *(uniname+len);
1044 				*(uniname+len) = 0x0;
1045 
1046 				if (exfat_uniname_ncmp(sb, uniname,
1047 					entry_uniname, len)) {
1048 					step = DIRENT_STEP_FILE;
1049 				} else if (p_uniname->name_len == name_len) {
1050 					if (order == num_ext)
1051 						goto found;
1052 					step = DIRENT_STEP_SECD;
1053 				}
1054 
1055 				*(uniname+len) = unichar;
1056 				continue;
1057 			}
1058 
1059 			if (entry_type &
1060 					(TYPE_CRITICAL_SEC | TYPE_BENIGN_SEC)) {
1061 				if (step == DIRENT_STEP_SECD) {
1062 					if (++order == num_ext)
1063 						goto found;
1064 					continue;
1065 				}
1066 			}
1067 			step = DIRENT_STEP_FILE;
1068 		}
1069 
1070 		if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1071 			if (--clu.size > 0)
1072 				clu.dir++;
1073 			else
1074 				clu.dir = EXFAT_EOF_CLUSTER;
1075 		} else {
1076 			if (exfat_get_next_cluster(sb, &clu.dir))
1077 				return -EIO;
1078 		}
1079 	}
1080 
1081 not_found:
1082 	/*
1083 	 * We started at not 0 index,so we should try to find target
1084 	 * from 0 index to the index we started at.
1085 	 */
1086 	if (!rewind && end_eidx) {
1087 		rewind = 1;
1088 		dentry = 0;
1089 		clu.dir = p_dir->dir;
1090 		/* reset empty hint */
1091 		num_empty = 0;
1092 		candi_empty.eidx = EXFAT_HINT_NONE;
1093 		goto rewind;
1094 	}
1095 
1096 	/* initialized hint_stat */
1097 	hint_stat->clu = p_dir->dir;
1098 	hint_stat->eidx = 0;
1099 	return -ENOENT;
1100 
1101 found:
1102 	/* next dentry we'll find is out of this cluster */
1103 	if (!((dentry + 1) & (dentries_per_clu - 1))) {
1104 		int ret = 0;
1105 
1106 		if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1107 			if (--clu.size > 0)
1108 				clu.dir++;
1109 			else
1110 				clu.dir = EXFAT_EOF_CLUSTER;
1111 		} else {
1112 			ret = exfat_get_next_cluster(sb, &clu.dir);
1113 		}
1114 
1115 		if (ret || clu.dir == EXFAT_EOF_CLUSTER) {
1116 			/* just initialized hint_stat */
1117 			hint_stat->clu = p_dir->dir;
1118 			hint_stat->eidx = 0;
1119 			return (dentry - num_ext);
1120 		}
1121 	}
1122 
1123 	hint_stat->clu = clu.dir;
1124 	hint_stat->eidx = dentry + 1;
1125 	return dentry - num_ext;
1126 }
1127 
1128 int exfat_count_ext_entries(struct super_block *sb, struct exfat_chain *p_dir,
1129 		int entry, struct exfat_dentry *ep)
1130 {
1131 	int i, count = 0;
1132 	unsigned int type;
1133 	struct exfat_dentry *ext_ep;
1134 	struct buffer_head *bh;
1135 
1136 	for (i = 0, entry++; i < ep->dentry.file.num_ext; i++, entry++) {
1137 		ext_ep = exfat_get_dentry(sb, p_dir, entry, &bh, NULL);
1138 		if (!ext_ep)
1139 			return -EIO;
1140 
1141 		type = exfat_get_entry_type(ext_ep);
1142 		brelse(bh);
1143 		if (type == TYPE_EXTEND || type == TYPE_STREAM)
1144 			count++;
1145 		else
1146 			break;
1147 	}
1148 	return count;
1149 }
1150 
1151 int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
1152 {
1153 	int i, count = 0;
1154 	int dentries_per_clu;
1155 	unsigned int entry_type;
1156 	struct exfat_chain clu;
1157 	struct exfat_dentry *ep;
1158 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
1159 	struct buffer_head *bh;
1160 
1161 	dentries_per_clu = sbi->dentries_per_clu;
1162 
1163 	exfat_chain_dup(&clu, p_dir);
1164 
1165 	while (clu.dir != EXFAT_EOF_CLUSTER) {
1166 		for (i = 0; i < dentries_per_clu; i++) {
1167 			ep = exfat_get_dentry(sb, &clu, i, &bh, NULL);
1168 			if (!ep)
1169 				return -EIO;
1170 			entry_type = exfat_get_entry_type(ep);
1171 			brelse(bh);
1172 
1173 			if (entry_type == TYPE_UNUSED)
1174 				return count;
1175 			if (entry_type != TYPE_DIR)
1176 				continue;
1177 			count++;
1178 		}
1179 
1180 		if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1181 			if (--clu.size > 0)
1182 				clu.dir++;
1183 			else
1184 				clu.dir = EXFAT_EOF_CLUSTER;
1185 		} else {
1186 			if (exfat_get_next_cluster(sb, &(clu.dir)))
1187 				return -EIO;
1188 		}
1189 	}
1190 
1191 	return count;
1192 }
1193