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