xref: /linux/fs/exfat/dir.c (revision ff39899be80b9d90d5e13775eb9fd150338b6e15)
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 	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 	max_dentries = (unsigned int)min_t(u64, MAX_EXFAT_DENTRIES,
89 					   (u64)sbi->num_clusters << dentries_per_clu_bits);
90 
91 	clu_offset = dentry >> dentries_per_clu_bits;
92 	exfat_chain_dup(&clu, &dir);
93 
94 	if (clu.flags == ALLOC_NO_FAT_CHAIN) {
95 		clu.dir += clu_offset;
96 		clu.size -= clu_offset;
97 	} else {
98 		/* hint_information */
99 		if (clu_offset > 0 && ei->hint_bmap.off != EXFAT_EOF_CLUSTER &&
100 		    ei->hint_bmap.off > 0 && clu_offset >= ei->hint_bmap.off) {
101 			clu_offset -= ei->hint_bmap.off;
102 			clu.dir = ei->hint_bmap.clu;
103 		}
104 
105 		while (clu_offset > 0) {
106 			if (exfat_get_next_cluster(sb, &(clu.dir)))
107 				return -EIO;
108 
109 			clu_offset--;
110 		}
111 	}
112 
113 	while (clu.dir != EXFAT_EOF_CLUSTER && dentry < max_dentries) {
114 		i = dentry & (dentries_per_clu - 1);
115 
116 		for ( ; i < dentries_per_clu; i++, dentry++) {
117 			ep = exfat_get_dentry(sb, &clu, i, &bh);
118 			if (!ep)
119 				return -EIO;
120 
121 			type = exfat_get_entry_type(ep);
122 			if (type == TYPE_UNUSED) {
123 				brelse(bh);
124 				break;
125 			}
126 
127 			if (type != TYPE_FILE && type != TYPE_DIR) {
128 				brelse(bh);
129 				continue;
130 			}
131 
132 			num_ext = ep->dentry.file.num_ext;
133 			dir_entry->attr = le16_to_cpu(ep->dentry.file.attr);
134 			exfat_get_entry_time(sbi, &dir_entry->crtime,
135 					ep->dentry.file.create_tz,
136 					ep->dentry.file.create_time,
137 					ep->dentry.file.create_date,
138 					ep->dentry.file.create_time_cs);
139 			exfat_get_entry_time(sbi, &dir_entry->mtime,
140 					ep->dentry.file.modify_tz,
141 					ep->dentry.file.modify_time,
142 					ep->dentry.file.modify_date,
143 					ep->dentry.file.modify_time_cs);
144 			exfat_get_entry_time(sbi, &dir_entry->atime,
145 					ep->dentry.file.access_tz,
146 					ep->dentry.file.access_time,
147 					ep->dentry.file.access_date,
148 					0);
149 
150 			*uni_name.name = 0x0;
151 			exfat_get_uniname_from_ext_entry(sb, &clu, i,
152 				uni_name.name);
153 			exfat_utf16_to_nls(sb, &uni_name,
154 				dir_entry->namebuf.lfn,
155 				dir_entry->namebuf.lfnbuf_len);
156 			brelse(bh);
157 
158 			ep = exfat_get_dentry(sb, &clu, i + 1, &bh);
159 			if (!ep)
160 				return -EIO;
161 			dir_entry->size =
162 				le64_to_cpu(ep->dentry.stream.valid_size);
163 			dir_entry->entry = dentry;
164 			brelse(bh);
165 
166 			ei->hint_bmap.off = dentry >> dentries_per_clu_bits;
167 			ei->hint_bmap.clu = clu.dir;
168 
169 			*cpos = EXFAT_DEN_TO_B(dentry + 1 + num_ext);
170 			return 0;
171 		}
172 
173 		if (clu.flags == ALLOC_NO_FAT_CHAIN) {
174 			if (--clu.size > 0)
175 				clu.dir++;
176 			else
177 				clu.dir = EXFAT_EOF_CLUSTER;
178 		} else {
179 			if (exfat_get_next_cluster(sb, &(clu.dir)))
180 				return -EIO;
181 		}
182 	}
183 
184 	dir_entry->namebuf.lfn[0] = '\0';
185 	*cpos = EXFAT_DEN_TO_B(dentry);
186 	return 0;
187 }
188 
189 static void exfat_init_namebuf(struct exfat_dentry_namebuf *nb)
190 {
191 	nb->lfn = NULL;
192 	nb->lfnbuf_len = 0;
193 }
194 
195 static int exfat_alloc_namebuf(struct exfat_dentry_namebuf *nb)
196 {
197 	nb->lfn = __getname();
198 	if (!nb->lfn)
199 		return -ENOMEM;
200 	nb->lfnbuf_len = MAX_VFSNAME_BUF_SIZE;
201 	return 0;
202 }
203 
204 static void exfat_free_namebuf(struct exfat_dentry_namebuf *nb)
205 {
206 	if (!nb->lfn)
207 		return;
208 
209 	__putname(nb->lfn);
210 	exfat_init_namebuf(nb);
211 }
212 
213 /* skip iterating emit_dots when dir is empty */
214 #define ITER_POS_FILLED_DOTS    (2)
215 static int exfat_iterate(struct file *file, struct dir_context *ctx)
216 {
217 	struct inode *inode = file_inode(file);
218 	struct super_block *sb = inode->i_sb;
219 	struct inode *tmp;
220 	struct exfat_dir_entry de;
221 	struct exfat_dentry_namebuf *nb = &(de.namebuf);
222 	struct exfat_inode_info *ei = EXFAT_I(inode);
223 	unsigned long inum;
224 	loff_t cpos, i_pos;
225 	int err = 0, fake_offset = 0;
226 
227 	exfat_init_namebuf(nb);
228 	mutex_lock(&EXFAT_SB(sb)->s_lock);
229 
230 	cpos = ctx->pos;
231 	if (!dir_emit_dots(file, ctx))
232 		goto unlock;
233 
234 	if (ctx->pos == ITER_POS_FILLED_DOTS) {
235 		cpos = 0;
236 		fake_offset = 1;
237 	}
238 
239 	if (cpos & (DENTRY_SIZE - 1)) {
240 		err = -ENOENT;
241 		goto unlock;
242 	}
243 
244 	/* name buffer should be allocated before use */
245 	err = exfat_alloc_namebuf(nb);
246 	if (err)
247 		goto unlock;
248 get_new:
249 	if (ei->flags == ALLOC_NO_FAT_CHAIN && cpos >= i_size_read(inode))
250 		goto end_of_dir;
251 
252 	err = exfat_readdir(inode, &cpos, &de);
253 	if (err) {
254 		/*
255 		 * At least we tried to read a sector.  Move cpos to next sector
256 		 * position (should be aligned).
257 		 */
258 		if (err == -EIO) {
259 			cpos += 1 << (sb->s_blocksize_bits);
260 			cpos &= ~(sb->s_blocksize - 1);
261 		}
262 
263 		err = -EIO;
264 		goto end_of_dir;
265 	}
266 
267 	if (!nb->lfn[0])
268 		goto end_of_dir;
269 
270 	i_pos = ((loff_t)ei->start_clu << 32) |	(de.entry & 0xffffffff);
271 	tmp = exfat_iget(sb, i_pos);
272 	if (tmp) {
273 		inum = tmp->i_ino;
274 		iput(tmp);
275 	} else {
276 		inum = iunique(sb, EXFAT_ROOT_INO);
277 	}
278 
279 	/*
280 	 * Before calling dir_emit(), sb_lock should be released.
281 	 * Because page fault can occur in dir_emit() when the size
282 	 * of buffer given from user is larger than one page size.
283 	 */
284 	mutex_unlock(&EXFAT_SB(sb)->s_lock);
285 	if (!dir_emit(ctx, nb->lfn, strlen(nb->lfn), inum,
286 			(de.attr & ATTR_SUBDIR) ? DT_DIR : DT_REG))
287 		goto out_unlocked;
288 	mutex_lock(&EXFAT_SB(sb)->s_lock);
289 	ctx->pos = cpos;
290 	goto get_new;
291 
292 end_of_dir:
293 	if (!cpos && fake_offset)
294 		cpos = ITER_POS_FILLED_DOTS;
295 	ctx->pos = cpos;
296 unlock:
297 	mutex_unlock(&EXFAT_SB(sb)->s_lock);
298 out_unlocked:
299 	/*
300 	 * To improve performance, free namebuf after unlock sb_lock.
301 	 * If namebuf is not allocated, this function do nothing
302 	 */
303 	exfat_free_namebuf(nb);
304 	return err;
305 }
306 
307 const struct file_operations exfat_dir_operations = {
308 	.llseek		= generic_file_llseek,
309 	.read		= generic_read_dir,
310 	.iterate	= exfat_iterate,
311 	.unlocked_ioctl = exfat_ioctl,
312 #ifdef CONFIG_COMPAT
313 	.compat_ioctl = exfat_compat_ioctl,
314 #endif
315 	.fsync		= exfat_file_fsync,
316 };
317 
318 int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu)
319 {
320 	int ret;
321 
322 	exfat_chain_set(clu, EXFAT_EOF_CLUSTER, 0, ALLOC_NO_FAT_CHAIN);
323 
324 	ret = exfat_alloc_cluster(inode, 1, clu, IS_DIRSYNC(inode));
325 	if (ret)
326 		return ret;
327 
328 	return exfat_zeroed_cluster(inode, clu->dir);
329 }
330 
331 int exfat_calc_num_entries(struct exfat_uni_name *p_uniname)
332 {
333 	int len;
334 
335 	len = p_uniname->name_len;
336 	if (len == 0)
337 		return -EINVAL;
338 
339 	/* 1 file entry + 1 stream entry + name entries */
340 	return ((len - 1) / EXFAT_FILE_NAME_LEN + 3);
341 }
342 
343 unsigned int exfat_get_entry_type(struct exfat_dentry *ep)
344 {
345 	if (ep->type == EXFAT_UNUSED)
346 		return TYPE_UNUSED;
347 	if (IS_EXFAT_DELETED(ep->type))
348 		return TYPE_DELETED;
349 	if (ep->type == EXFAT_INVAL)
350 		return TYPE_INVALID;
351 	if (IS_EXFAT_CRITICAL_PRI(ep->type)) {
352 		if (ep->type == EXFAT_BITMAP)
353 			return TYPE_BITMAP;
354 		if (ep->type == EXFAT_UPCASE)
355 			return TYPE_UPCASE;
356 		if (ep->type == EXFAT_VOLUME)
357 			return TYPE_VOLUME;
358 		if (ep->type == EXFAT_FILE) {
359 			if (le16_to_cpu(ep->dentry.file.attr) & ATTR_SUBDIR)
360 				return TYPE_DIR;
361 			return TYPE_FILE;
362 		}
363 		return TYPE_CRITICAL_PRI;
364 	}
365 	if (IS_EXFAT_BENIGN_PRI(ep->type)) {
366 		if (ep->type == EXFAT_GUID)
367 			return TYPE_GUID;
368 		if (ep->type == EXFAT_PADDING)
369 			return TYPE_PADDING;
370 		if (ep->type == EXFAT_ACLTAB)
371 			return TYPE_ACLTAB;
372 		return TYPE_BENIGN_PRI;
373 	}
374 	if (IS_EXFAT_CRITICAL_SEC(ep->type)) {
375 		if (ep->type == EXFAT_STREAM)
376 			return TYPE_STREAM;
377 		if (ep->type == EXFAT_NAME)
378 			return TYPE_EXTEND;
379 		if (ep->type == EXFAT_ACL)
380 			return TYPE_ACL;
381 		return TYPE_CRITICAL_SEC;
382 	}
383 	return TYPE_BENIGN_SEC;
384 }
385 
386 static void exfat_set_entry_type(struct exfat_dentry *ep, unsigned int type)
387 {
388 	if (type == TYPE_UNUSED) {
389 		ep->type = EXFAT_UNUSED;
390 	} else if (type == TYPE_DELETED) {
391 		ep->type &= EXFAT_DELETE;
392 	} else if (type == TYPE_STREAM) {
393 		ep->type = EXFAT_STREAM;
394 	} else if (type == TYPE_EXTEND) {
395 		ep->type = EXFAT_NAME;
396 	} else if (type == TYPE_BITMAP) {
397 		ep->type = EXFAT_BITMAP;
398 	} else if (type == TYPE_UPCASE) {
399 		ep->type = EXFAT_UPCASE;
400 	} else if (type == TYPE_VOLUME) {
401 		ep->type = EXFAT_VOLUME;
402 	} else if (type == TYPE_DIR) {
403 		ep->type = EXFAT_FILE;
404 		ep->dentry.file.attr = cpu_to_le16(ATTR_SUBDIR);
405 	} else if (type == TYPE_FILE) {
406 		ep->type = EXFAT_FILE;
407 		ep->dentry.file.attr = cpu_to_le16(ATTR_ARCHIVE);
408 	}
409 }
410 
411 static void exfat_init_stream_entry(struct exfat_dentry *ep,
412 		unsigned char flags, unsigned int start_clu,
413 		unsigned long long size)
414 {
415 	exfat_set_entry_type(ep, TYPE_STREAM);
416 	ep->dentry.stream.flags = flags;
417 	ep->dentry.stream.start_clu = cpu_to_le32(start_clu);
418 	ep->dentry.stream.valid_size = cpu_to_le64(size);
419 	ep->dentry.stream.size = cpu_to_le64(size);
420 }
421 
422 static void exfat_init_name_entry(struct exfat_dentry *ep,
423 		unsigned short *uniname)
424 {
425 	int i;
426 
427 	exfat_set_entry_type(ep, TYPE_EXTEND);
428 	ep->dentry.name.flags = 0x0;
429 
430 	for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) {
431 		if (*uniname != 0x0) {
432 			ep->dentry.name.unicode_0_14[i] = cpu_to_le16(*uniname);
433 			uniname++;
434 		} else {
435 			ep->dentry.name.unicode_0_14[i] = 0x0;
436 		}
437 	}
438 }
439 
440 int exfat_init_dir_entry(struct inode *inode, struct exfat_chain *p_dir,
441 		int entry, unsigned int type, unsigned int start_clu,
442 		unsigned long long size)
443 {
444 	struct super_block *sb = inode->i_sb;
445 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
446 	struct timespec64 ts = current_time(inode);
447 	struct exfat_dentry *ep;
448 	struct buffer_head *bh;
449 
450 	/*
451 	 * We cannot use exfat_get_dentry_set here because file ep is not
452 	 * initialized yet.
453 	 */
454 	ep = exfat_get_dentry(sb, p_dir, entry, &bh);
455 	if (!ep)
456 		return -EIO;
457 
458 	exfat_set_entry_type(ep, type);
459 	exfat_set_entry_time(sbi, &ts,
460 			&ep->dentry.file.create_tz,
461 			&ep->dentry.file.create_time,
462 			&ep->dentry.file.create_date,
463 			&ep->dentry.file.create_time_cs);
464 	exfat_set_entry_time(sbi, &ts,
465 			&ep->dentry.file.modify_tz,
466 			&ep->dentry.file.modify_time,
467 			&ep->dentry.file.modify_date,
468 			&ep->dentry.file.modify_time_cs);
469 	exfat_set_entry_time(sbi, &ts,
470 			&ep->dentry.file.access_tz,
471 			&ep->dentry.file.access_time,
472 			&ep->dentry.file.access_date,
473 			NULL);
474 
475 	exfat_update_bh(bh, IS_DIRSYNC(inode));
476 	brelse(bh);
477 
478 	ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh);
479 	if (!ep)
480 		return -EIO;
481 
482 	exfat_init_stream_entry(ep,
483 		(type == TYPE_FILE) ? ALLOC_FAT_CHAIN : ALLOC_NO_FAT_CHAIN,
484 		start_clu, size);
485 	exfat_update_bh(bh, IS_DIRSYNC(inode));
486 	brelse(bh);
487 
488 	return 0;
489 }
490 
491 int exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir,
492 		int entry)
493 {
494 	struct super_block *sb = inode->i_sb;
495 	int ret = 0;
496 	int i, num_entries;
497 	u16 chksum;
498 	struct exfat_dentry *ep, *fep;
499 	struct buffer_head *fbh, *bh;
500 
501 	fep = exfat_get_dentry(sb, p_dir, entry, &fbh);
502 	if (!fep)
503 		return -EIO;
504 
505 	num_entries = fep->dentry.file.num_ext + 1;
506 	chksum = exfat_calc_chksum16(fep, DENTRY_SIZE, 0, CS_DIR_ENTRY);
507 
508 	for (i = 1; i < num_entries; i++) {
509 		ep = exfat_get_dentry(sb, p_dir, entry + i, &bh);
510 		if (!ep) {
511 			ret = -EIO;
512 			goto release_fbh;
513 		}
514 		chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
515 				CS_DEFAULT);
516 		brelse(bh);
517 	}
518 
519 	fep->dentry.file.checksum = cpu_to_le16(chksum);
520 	exfat_update_bh(fbh, IS_DIRSYNC(inode));
521 release_fbh:
522 	brelse(fbh);
523 	return ret;
524 }
525 
526 int exfat_init_ext_entry(struct inode *inode, struct exfat_chain *p_dir,
527 		int entry, int num_entries, struct exfat_uni_name *p_uniname)
528 {
529 	struct super_block *sb = inode->i_sb;
530 	int i;
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);
537 	if (!ep)
538 		return -EIO;
539 
540 	ep->dentry.file.num_ext = (unsigned char)(num_entries - 1);
541 	exfat_update_bh(bh, sync);
542 	brelse(bh);
543 
544 	ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh);
545 	if (!ep)
546 		return -EIO;
547 
548 	ep->dentry.stream.name_len = p_uniname->name_len;
549 	ep->dentry.stream.name_hash = cpu_to_le16(p_uniname->name_hash);
550 	exfat_update_bh(bh, sync);
551 	brelse(bh);
552 
553 	for (i = EXFAT_FIRST_CLUSTER; i < num_entries; i++) {
554 		ep = exfat_get_dentry(sb, p_dir, entry + i, &bh);
555 		if (!ep)
556 			return -EIO;
557 
558 		exfat_init_name_entry(ep, uniname);
559 		exfat_update_bh(bh, sync);
560 		brelse(bh);
561 		uniname += EXFAT_FILE_NAME_LEN;
562 	}
563 
564 	exfat_update_dir_chksum(inode, p_dir, entry);
565 	return 0;
566 }
567 
568 int exfat_remove_entries(struct inode *inode, struct exfat_chain *p_dir,
569 		int entry, int order, int num_entries)
570 {
571 	struct super_block *sb = inode->i_sb;
572 	int i;
573 	struct exfat_dentry *ep;
574 	struct buffer_head *bh;
575 
576 	for (i = order; i < num_entries; i++) {
577 		ep = exfat_get_dentry(sb, p_dir, entry + i, &bh);
578 		if (!ep)
579 			return -EIO;
580 
581 		exfat_set_entry_type(ep, TYPE_DELETED);
582 		exfat_update_bh(bh, IS_DIRSYNC(inode));
583 		brelse(bh);
584 	}
585 
586 	return 0;
587 }
588 
589 void exfat_update_dir_chksum_with_entry_set(struct exfat_entry_set_cache *es)
590 {
591 	int chksum_type = CS_DIR_ENTRY, i;
592 	unsigned short chksum = 0;
593 	struct exfat_dentry *ep;
594 
595 	for (i = 0; i < es->num_entries; i++) {
596 		ep = exfat_get_dentry_cached(es, i);
597 		chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
598 					     chksum_type);
599 		chksum_type = CS_DEFAULT;
600 	}
601 	ep = exfat_get_dentry_cached(es, 0);
602 	ep->dentry.file.checksum = cpu_to_le16(chksum);
603 	es->modified = true;
604 }
605 
606 int exfat_free_dentry_set(struct exfat_entry_set_cache *es, int sync)
607 {
608 	int i, err = 0;
609 
610 	if (es->modified)
611 		err = exfat_update_bhs(es->bh, es->num_bh, sync);
612 
613 	for (i = 0; i < es->num_bh; i++)
614 		if (err)
615 			bforget(es->bh[i]);
616 		else
617 			brelse(es->bh[i]);
618 	kfree(es);
619 	return err;
620 }
621 
622 static int exfat_walk_fat_chain(struct super_block *sb,
623 		struct exfat_chain *p_dir, unsigned int byte_offset,
624 		unsigned int *clu)
625 {
626 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
627 	unsigned int clu_offset;
628 	unsigned int cur_clu;
629 
630 	clu_offset = EXFAT_B_TO_CLU(byte_offset, sbi);
631 	cur_clu = p_dir->dir;
632 
633 	if (p_dir->flags == ALLOC_NO_FAT_CHAIN) {
634 		cur_clu += clu_offset;
635 	} else {
636 		while (clu_offset > 0) {
637 			if (exfat_get_next_cluster(sb, &cur_clu))
638 				return -EIO;
639 			if (cur_clu == EXFAT_EOF_CLUSTER) {
640 				exfat_fs_error(sb,
641 					"invalid dentry access beyond EOF (clu : %u, eidx : %d)",
642 					p_dir->dir,
643 					EXFAT_B_TO_DEN(byte_offset));
644 				return -EIO;
645 			}
646 			clu_offset--;
647 		}
648 	}
649 
650 	*clu = cur_clu;
651 	return 0;
652 }
653 
654 static int exfat_find_location(struct super_block *sb, struct exfat_chain *p_dir,
655 			       int entry, sector_t *sector, int *offset)
656 {
657 	int ret;
658 	unsigned int off, clu = 0;
659 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
660 
661 	off = EXFAT_DEN_TO_B(entry);
662 
663 	ret = exfat_walk_fat_chain(sb, p_dir, off, &clu);
664 	if (ret)
665 		return ret;
666 
667 	/* byte offset in cluster */
668 	off = EXFAT_CLU_OFFSET(off, sbi);
669 
670 	/* byte offset in sector    */
671 	*offset = EXFAT_BLK_OFFSET(off, sb);
672 
673 	/* sector offset in cluster */
674 	*sector = EXFAT_B_TO_BLK(off, sb);
675 	*sector += exfat_cluster_to_sector(sbi, clu);
676 	return 0;
677 }
678 
679 #define EXFAT_MAX_RA_SIZE     (128*1024)
680 static int exfat_dir_readahead(struct super_block *sb, sector_t sec)
681 {
682 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
683 	struct buffer_head *bh;
684 	unsigned int max_ra_count = EXFAT_MAX_RA_SIZE >> sb->s_blocksize_bits;
685 	unsigned int page_ra_count = PAGE_SIZE >> sb->s_blocksize_bits;
686 	unsigned int adj_ra_count = max(sbi->sect_per_clus, page_ra_count);
687 	unsigned int ra_count = min(adj_ra_count, max_ra_count);
688 
689 	/* Read-ahead is not required */
690 	if (sbi->sect_per_clus == 1)
691 		return 0;
692 
693 	if (sec < sbi->data_start_sector) {
694 		exfat_err(sb, "requested sector is invalid(sect:%llu, root:%llu)",
695 			  (unsigned long long)sec, sbi->data_start_sector);
696 		return -EIO;
697 	}
698 
699 	/* Not sector aligned with ra_count, resize ra_count to page size */
700 	if ((sec - sbi->data_start_sector) & (ra_count - 1))
701 		ra_count = page_ra_count;
702 
703 	bh = sb_find_get_block(sb, sec);
704 	if (!bh || !buffer_uptodate(bh)) {
705 		unsigned int i;
706 
707 		for (i = 0; i < ra_count; i++)
708 			sb_breadahead(sb, (sector_t)(sec + i));
709 	}
710 	brelse(bh);
711 	return 0;
712 }
713 
714 struct exfat_dentry *exfat_get_dentry(struct super_block *sb,
715 		struct exfat_chain *p_dir, int entry, struct buffer_head **bh)
716 {
717 	unsigned int dentries_per_page = EXFAT_B_TO_DEN(PAGE_SIZE);
718 	int off;
719 	sector_t sec;
720 
721 	if (p_dir->dir == DIR_DELETED) {
722 		exfat_err(sb, "abnormal access to deleted dentry");
723 		return NULL;
724 	}
725 
726 	if (exfat_find_location(sb, p_dir, entry, &sec, &off))
727 		return NULL;
728 
729 	if (p_dir->dir != EXFAT_FREE_CLUSTER &&
730 			!(entry & (dentries_per_page - 1)))
731 		exfat_dir_readahead(sb, sec);
732 
733 	*bh = sb_bread(sb, sec);
734 	if (!*bh)
735 		return NULL;
736 
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 	/* validate 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 static inline void exfat_reset_empty_hint(struct exfat_hint_femp *hint_femp)
901 {
902 	hint_femp->eidx = EXFAT_HINT_NONE;
903 	hint_femp->count = 0;
904 }
905 
906 static inline void exfat_set_empty_hint(struct exfat_inode_info *ei,
907 		struct exfat_hint_femp *candi_empty, struct exfat_chain *clu,
908 		int dentry, int num_entries)
909 {
910 	if (ei->hint_femp.eidx == EXFAT_HINT_NONE ||
911 	    ei->hint_femp.eidx > dentry) {
912 		if (candi_empty->count == 0) {
913 			candi_empty->cur = *clu;
914 			candi_empty->eidx = dentry;
915 		}
916 
917 		candi_empty->count++;
918 		if (candi_empty->count == num_entries)
919 			ei->hint_femp = *candi_empty;
920 	}
921 }
922 
923 enum {
924 	DIRENT_STEP_FILE,
925 	DIRENT_STEP_STRM,
926 	DIRENT_STEP_NAME,
927 	DIRENT_STEP_SECD,
928 };
929 
930 /*
931  * @ei:         inode info of parent directory
932  * @p_dir:      directory structure of parent directory
933  * @num_entries:entry size of p_uniname
934  * @hint_opt:   If p_uniname is found, filled with optimized dir/entry
935  *              for traversing cluster chain.
936  * @return:
937  *   >= 0:      file directory entry position where the name exists
938  *   -ENOENT:   entry with the name does not exist
939  *   -EIO:      I/O error
940  */
941 int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
942 		struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
943 		int num_entries, unsigned int type, struct exfat_hint *hint_opt)
944 {
945 	int i, rewind = 0, dentry = 0, end_eidx = 0, num_ext = 0, len;
946 	int order, step, name_len = 0;
947 	int dentries_per_clu;
948 	unsigned int entry_type;
949 	unsigned short *uniname = NULL;
950 	struct exfat_chain clu;
951 	struct exfat_hint *hint_stat = &ei->hint_stat;
952 	struct exfat_hint_femp candi_empty;
953 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
954 
955 	dentries_per_clu = sbi->dentries_per_clu;
956 
957 	exfat_chain_dup(&clu, p_dir);
958 
959 	if (hint_stat->eidx) {
960 		clu.dir = hint_stat->clu;
961 		dentry = hint_stat->eidx;
962 		end_eidx = dentry;
963 	}
964 
965 	exfat_reset_empty_hint(&ei->hint_femp);
966 
967 rewind:
968 	order = 0;
969 	step = DIRENT_STEP_FILE;
970 	exfat_reset_empty_hint(&candi_empty);
971 
972 	while (clu.dir != EXFAT_EOF_CLUSTER) {
973 		i = dentry & (dentries_per_clu - 1);
974 		for (; i < dentries_per_clu; i++, dentry++) {
975 			struct exfat_dentry *ep;
976 			struct buffer_head *bh;
977 
978 			if (rewind && dentry == end_eidx)
979 				goto not_found;
980 
981 			ep = exfat_get_dentry(sb, &clu, i, &bh);
982 			if (!ep)
983 				return -EIO;
984 
985 			entry_type = exfat_get_entry_type(ep);
986 
987 			if (entry_type == TYPE_UNUSED ||
988 			    entry_type == TYPE_DELETED) {
989 				step = DIRENT_STEP_FILE;
990 
991 				exfat_set_empty_hint(ei, &candi_empty, &clu,
992 						dentry, num_entries);
993 
994 				brelse(bh);
995 				if (entry_type == TYPE_UNUSED)
996 					goto not_found;
997 				continue;
998 			}
999 
1000 			exfat_reset_empty_hint(&candi_empty);
1001 
1002 			if (entry_type == TYPE_FILE || entry_type == TYPE_DIR) {
1003 				step = DIRENT_STEP_FILE;
1004 				hint_opt->clu = clu.dir;
1005 				hint_opt->eidx = i;
1006 				if (type == TYPE_ALL || type == entry_type) {
1007 					num_ext = ep->dentry.file.num_ext;
1008 					step = DIRENT_STEP_STRM;
1009 				}
1010 				brelse(bh);
1011 				continue;
1012 			}
1013 
1014 			if (entry_type == TYPE_STREAM) {
1015 				u16 name_hash;
1016 
1017 				if (step != DIRENT_STEP_STRM) {
1018 					step = DIRENT_STEP_FILE;
1019 					brelse(bh);
1020 					continue;
1021 				}
1022 				step = DIRENT_STEP_FILE;
1023 				name_hash = le16_to_cpu(
1024 						ep->dentry.stream.name_hash);
1025 				if (p_uniname->name_hash == name_hash &&
1026 				    p_uniname->name_len ==
1027 						ep->dentry.stream.name_len) {
1028 					step = DIRENT_STEP_NAME;
1029 					order = 1;
1030 					name_len = 0;
1031 				}
1032 				brelse(bh);
1033 				continue;
1034 			}
1035 
1036 			brelse(bh);
1037 			if (entry_type == TYPE_EXTEND) {
1038 				unsigned short entry_uniname[16], unichar;
1039 
1040 				if (step != DIRENT_STEP_NAME) {
1041 					step = DIRENT_STEP_FILE;
1042 					continue;
1043 				}
1044 
1045 				if (++order == 2)
1046 					uniname = p_uniname->name;
1047 				else
1048 					uniname += EXFAT_FILE_NAME_LEN;
1049 
1050 				len = exfat_extract_uni_name(ep, entry_uniname);
1051 				name_len += len;
1052 
1053 				unichar = *(uniname+len);
1054 				*(uniname+len) = 0x0;
1055 
1056 				if (exfat_uniname_ncmp(sb, uniname,
1057 					entry_uniname, len)) {
1058 					step = DIRENT_STEP_FILE;
1059 				} else if (p_uniname->name_len == name_len) {
1060 					if (order == num_ext)
1061 						goto found;
1062 					step = DIRENT_STEP_SECD;
1063 				}
1064 
1065 				*(uniname+len) = unichar;
1066 				continue;
1067 			}
1068 
1069 			if (entry_type &
1070 					(TYPE_CRITICAL_SEC | TYPE_BENIGN_SEC)) {
1071 				if (step == DIRENT_STEP_SECD) {
1072 					if (++order == num_ext)
1073 						goto found;
1074 					continue;
1075 				}
1076 			}
1077 			step = DIRENT_STEP_FILE;
1078 		}
1079 
1080 		if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1081 			if (--clu.size > 0)
1082 				clu.dir++;
1083 			else
1084 				clu.dir = EXFAT_EOF_CLUSTER;
1085 		} else {
1086 			if (exfat_get_next_cluster(sb, &clu.dir))
1087 				return -EIO;
1088 		}
1089 	}
1090 
1091 not_found:
1092 	/*
1093 	 * We started at not 0 index,so we should try to find target
1094 	 * from 0 index to the index we started at.
1095 	 */
1096 	if (!rewind && end_eidx) {
1097 		rewind = 1;
1098 		dentry = 0;
1099 		clu.dir = p_dir->dir;
1100 		goto rewind;
1101 	}
1102 
1103 	/* initialized hint_stat */
1104 	hint_stat->clu = p_dir->dir;
1105 	hint_stat->eidx = 0;
1106 	return -ENOENT;
1107 
1108 found:
1109 	/* next dentry we'll find is out of this cluster */
1110 	if (!((dentry + 1) & (dentries_per_clu - 1))) {
1111 		int ret = 0;
1112 
1113 		if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1114 			if (--clu.size > 0)
1115 				clu.dir++;
1116 			else
1117 				clu.dir = EXFAT_EOF_CLUSTER;
1118 		} else {
1119 			ret = exfat_get_next_cluster(sb, &clu.dir);
1120 		}
1121 
1122 		if (ret || clu.dir == EXFAT_EOF_CLUSTER) {
1123 			/* just initialized hint_stat */
1124 			hint_stat->clu = p_dir->dir;
1125 			hint_stat->eidx = 0;
1126 			return (dentry - num_ext);
1127 		}
1128 	}
1129 
1130 	hint_stat->clu = clu.dir;
1131 	hint_stat->eidx = dentry + 1;
1132 	return dentry - num_ext;
1133 }
1134 
1135 int exfat_count_ext_entries(struct super_block *sb, struct exfat_chain *p_dir,
1136 		int entry, struct exfat_dentry *ep)
1137 {
1138 	int i, count = 0;
1139 	unsigned int type;
1140 	struct exfat_dentry *ext_ep;
1141 	struct buffer_head *bh;
1142 
1143 	for (i = 0, entry++; i < ep->dentry.file.num_ext; i++, entry++) {
1144 		ext_ep = exfat_get_dentry(sb, p_dir, entry, &bh);
1145 		if (!ext_ep)
1146 			return -EIO;
1147 
1148 		type = exfat_get_entry_type(ext_ep);
1149 		brelse(bh);
1150 		if (type == TYPE_EXTEND || type == TYPE_STREAM)
1151 			count++;
1152 		else
1153 			break;
1154 	}
1155 	return count;
1156 }
1157 
1158 int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
1159 {
1160 	int i, count = 0;
1161 	int dentries_per_clu;
1162 	unsigned int entry_type;
1163 	struct exfat_chain clu;
1164 	struct exfat_dentry *ep;
1165 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
1166 	struct buffer_head *bh;
1167 
1168 	dentries_per_clu = sbi->dentries_per_clu;
1169 
1170 	exfat_chain_dup(&clu, p_dir);
1171 
1172 	while (clu.dir != EXFAT_EOF_CLUSTER) {
1173 		for (i = 0; i < dentries_per_clu; i++) {
1174 			ep = exfat_get_dentry(sb, &clu, i, &bh);
1175 			if (!ep)
1176 				return -EIO;
1177 			entry_type = exfat_get_entry_type(ep);
1178 			brelse(bh);
1179 
1180 			if (entry_type == TYPE_UNUSED)
1181 				return count;
1182 			if (entry_type != TYPE_DIR)
1183 				continue;
1184 			count++;
1185 		}
1186 
1187 		if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1188 			if (--clu.size > 0)
1189 				clu.dir++;
1190 			else
1191 				clu.dir = EXFAT_EOF_CLUSTER;
1192 		} else {
1193 			if (exfat_get_next_cluster(sb, &(clu.dir)))
1194 				return -EIO;
1195 		}
1196 	}
1197 
1198 	return count;
1199 }
1200