xref: /openbmc/linux/fs/exfat/dir.c (revision 305c8388)
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		= generic_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 		ep->dentry.name.unicode_0_14[i] = cpu_to_le16(*uniname);
429 		if (*uniname == 0x0)
430 			break;
431 		uniname++;
432 	}
433 }
434 
435 int exfat_init_dir_entry(struct inode *inode, struct exfat_chain *p_dir,
436 		int entry, unsigned int type, unsigned int start_clu,
437 		unsigned long long size)
438 {
439 	struct super_block *sb = inode->i_sb;
440 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
441 	struct timespec64 ts = current_time(inode);
442 	sector_t sector;
443 	struct exfat_dentry *ep;
444 	struct buffer_head *bh;
445 
446 	/*
447 	 * We cannot use exfat_get_dentry_set here because file ep is not
448 	 * initialized yet.
449 	 */
450 	ep = exfat_get_dentry(sb, p_dir, entry, &bh, &sector);
451 	if (!ep)
452 		return -EIO;
453 
454 	exfat_set_entry_type(ep, type);
455 	exfat_set_entry_time(sbi, &ts,
456 			&ep->dentry.file.create_tz,
457 			&ep->dentry.file.create_time,
458 			&ep->dentry.file.create_date,
459 			&ep->dentry.file.create_time_cs);
460 	exfat_set_entry_time(sbi, &ts,
461 			&ep->dentry.file.modify_tz,
462 			&ep->dentry.file.modify_time,
463 			&ep->dentry.file.modify_date,
464 			&ep->dentry.file.modify_time_cs);
465 	exfat_set_entry_time(sbi, &ts,
466 			&ep->dentry.file.access_tz,
467 			&ep->dentry.file.access_time,
468 			&ep->dentry.file.access_date,
469 			NULL);
470 
471 	exfat_update_bh(sb, bh, IS_DIRSYNC(inode));
472 	brelse(bh);
473 
474 	ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh, &sector);
475 	if (!ep)
476 		return -EIO;
477 
478 	exfat_init_stream_entry(ep,
479 		(type == TYPE_FILE) ? ALLOC_FAT_CHAIN : ALLOC_NO_FAT_CHAIN,
480 		start_clu, size);
481 	exfat_update_bh(sb, bh, IS_DIRSYNC(inode));
482 	brelse(bh);
483 
484 	return 0;
485 }
486 
487 int exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir,
488 		int entry)
489 {
490 	struct super_block *sb = inode->i_sb;
491 	int ret = 0;
492 	int i, num_entries;
493 	sector_t sector;
494 	u16 chksum;
495 	struct exfat_dentry *ep, *fep;
496 	struct buffer_head *fbh, *bh;
497 
498 	fep = exfat_get_dentry(sb, p_dir, entry, &fbh, &sector);
499 	if (!fep)
500 		return -EIO;
501 
502 	num_entries = fep->dentry.file.num_ext + 1;
503 	chksum = exfat_calc_chksum16(fep, DENTRY_SIZE, 0, CS_DIR_ENTRY);
504 
505 	for (i = 1; i < num_entries; i++) {
506 		ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, NULL);
507 		if (!ep) {
508 			ret = -EIO;
509 			goto release_fbh;
510 		}
511 		chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
512 				CS_DEFAULT);
513 		brelse(bh);
514 	}
515 
516 	fep->dentry.file.checksum = cpu_to_le16(chksum);
517 	exfat_update_bh(sb, fbh, IS_DIRSYNC(inode));
518 release_fbh:
519 	brelse(fbh);
520 	return ret;
521 }
522 
523 int exfat_init_ext_entry(struct inode *inode, struct exfat_chain *p_dir,
524 		int entry, int num_entries, struct exfat_uni_name *p_uniname)
525 {
526 	struct super_block *sb = inode->i_sb;
527 	int i;
528 	sector_t sector;
529 	unsigned short *uniname = p_uniname->name;
530 	struct exfat_dentry *ep;
531 	struct buffer_head *bh;
532 	int sync = IS_DIRSYNC(inode);
533 
534 	ep = exfat_get_dentry(sb, p_dir, entry, &bh, &sector);
535 	if (!ep)
536 		return -EIO;
537 
538 	ep->dentry.file.num_ext = (unsigned char)(num_entries - 1);
539 	exfat_update_bh(sb, bh, sync);
540 	brelse(bh);
541 
542 	ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh, &sector);
543 	if (!ep)
544 		return -EIO;
545 
546 	ep->dentry.stream.name_len = p_uniname->name_len;
547 	ep->dentry.stream.name_hash = cpu_to_le16(p_uniname->name_hash);
548 	exfat_update_bh(sb, bh, sync);
549 	brelse(bh);
550 
551 	for (i = EXFAT_FIRST_CLUSTER; i < num_entries; i++) {
552 		ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, &sector);
553 		if (!ep)
554 			return -EIO;
555 
556 		exfat_init_name_entry(ep, uniname);
557 		exfat_update_bh(sb, bh, sync);
558 		brelse(bh);
559 		uniname += EXFAT_FILE_NAME_LEN;
560 	}
561 
562 	exfat_update_dir_chksum(inode, p_dir, entry);
563 	return 0;
564 }
565 
566 int exfat_remove_entries(struct inode *inode, struct exfat_chain *p_dir,
567 		int entry, int order, int num_entries)
568 {
569 	struct super_block *sb = inode->i_sb;
570 	int i;
571 	sector_t sector;
572 	struct exfat_dentry *ep;
573 	struct buffer_head *bh;
574 
575 	for (i = order; i < num_entries; i++) {
576 		ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, &sector);
577 		if (!ep)
578 			return -EIO;
579 
580 		exfat_set_entry_type(ep, TYPE_DELETED);
581 		exfat_update_bh(sb, bh, IS_DIRSYNC(inode));
582 		brelse(bh);
583 	}
584 
585 	return 0;
586 }
587 
588 void exfat_update_dir_chksum_with_entry_set(struct exfat_entry_set_cache *es)
589 {
590 	int chksum_type = CS_DIR_ENTRY, i;
591 	unsigned short chksum = 0;
592 	struct exfat_dentry *ep;
593 
594 	for (i = 0; i < es->num_entries; i++) {
595 		ep = exfat_get_dentry_cached(es, i);
596 		chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
597 					     chksum_type);
598 		chksum_type = CS_DEFAULT;
599 	}
600 	ep = exfat_get_dentry_cached(es, 0);
601 	ep->dentry.file.checksum = cpu_to_le16(chksum);
602 	es->modified = true;
603 }
604 
605 void exfat_free_dentry_set(struct exfat_entry_set_cache *es, int sync)
606 {
607 	int i;
608 
609 	for (i = 0; i < es->num_bh; i++) {
610 		if (es->modified)
611 			exfat_update_bh(es->sb, es->bh[i], sync);
612 		brelse(es->bh[i]);
613 	}
614 	kfree(es);
615 }
616 
617 static int exfat_walk_fat_chain(struct super_block *sb,
618 		struct exfat_chain *p_dir, unsigned int byte_offset,
619 		unsigned int *clu)
620 {
621 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
622 	unsigned int clu_offset;
623 	unsigned int cur_clu;
624 
625 	clu_offset = EXFAT_B_TO_CLU(byte_offset, sbi);
626 	cur_clu = p_dir->dir;
627 
628 	if (p_dir->flags == ALLOC_NO_FAT_CHAIN) {
629 		cur_clu += clu_offset;
630 	} else {
631 		while (clu_offset > 0) {
632 			if (exfat_get_next_cluster(sb, &cur_clu))
633 				return -EIO;
634 			if (cur_clu == EXFAT_EOF_CLUSTER) {
635 				exfat_fs_error(sb,
636 					"invalid dentry access beyond EOF (clu : %u, eidx : %d)",
637 					p_dir->dir,
638 					EXFAT_B_TO_DEN(byte_offset));
639 				return -EIO;
640 			}
641 			clu_offset--;
642 		}
643 	}
644 
645 	*clu = cur_clu;
646 	return 0;
647 }
648 
649 int exfat_find_location(struct super_block *sb, struct exfat_chain *p_dir,
650 		int entry, sector_t *sector, int *offset)
651 {
652 	int ret;
653 	unsigned int off, clu = 0;
654 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
655 
656 	off = EXFAT_DEN_TO_B(entry);
657 
658 	ret = exfat_walk_fat_chain(sb, p_dir, off, &clu);
659 	if (ret)
660 		return ret;
661 
662 	/* byte offset in cluster */
663 	off = EXFAT_CLU_OFFSET(off, sbi);
664 
665 	/* byte offset in sector    */
666 	*offset = EXFAT_BLK_OFFSET(off, sb);
667 
668 	/* sector offset in cluster */
669 	*sector = EXFAT_B_TO_BLK(off, sb);
670 	*sector += exfat_cluster_to_sector(sbi, clu);
671 	return 0;
672 }
673 
674 #define EXFAT_MAX_RA_SIZE     (128*1024)
675 static int exfat_dir_readahead(struct super_block *sb, sector_t sec)
676 {
677 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
678 	struct buffer_head *bh;
679 	unsigned int max_ra_count = EXFAT_MAX_RA_SIZE >> sb->s_blocksize_bits;
680 	unsigned int page_ra_count = PAGE_SIZE >> sb->s_blocksize_bits;
681 	unsigned int adj_ra_count = max(sbi->sect_per_clus, page_ra_count);
682 	unsigned int ra_count = min(adj_ra_count, max_ra_count);
683 
684 	/* Read-ahead is not required */
685 	if (sbi->sect_per_clus == 1)
686 		return 0;
687 
688 	if (sec < sbi->data_start_sector) {
689 		exfat_err(sb, "requested sector is invalid(sect:%llu, root:%llu)",
690 			  (unsigned long long)sec, sbi->data_start_sector);
691 		return -EIO;
692 	}
693 
694 	/* Not sector aligned with ra_count, resize ra_count to page size */
695 	if ((sec - sbi->data_start_sector) & (ra_count - 1))
696 		ra_count = page_ra_count;
697 
698 	bh = sb_find_get_block(sb, sec);
699 	if (!bh || !buffer_uptodate(bh)) {
700 		unsigned int i;
701 
702 		for (i = 0; i < ra_count; i++)
703 			sb_breadahead(sb, (sector_t)(sec + i));
704 	}
705 	brelse(bh);
706 	return 0;
707 }
708 
709 struct exfat_dentry *exfat_get_dentry(struct super_block *sb,
710 		struct exfat_chain *p_dir, int entry, struct buffer_head **bh,
711 		sector_t *sector)
712 {
713 	unsigned int dentries_per_page = EXFAT_B_TO_DEN(PAGE_SIZE);
714 	int off;
715 	sector_t sec;
716 
717 	if (p_dir->dir == DIR_DELETED) {
718 		exfat_err(sb, "abnormal access to deleted dentry");
719 		return NULL;
720 	}
721 
722 	if (exfat_find_location(sb, p_dir, entry, &sec, &off))
723 		return NULL;
724 
725 	if (p_dir->dir != EXFAT_FREE_CLUSTER &&
726 			!(entry & (dentries_per_page - 1)))
727 		exfat_dir_readahead(sb, sec);
728 
729 	*bh = sb_bread(sb, sec);
730 	if (!*bh)
731 		return NULL;
732 
733 	if (sector)
734 		*sector = sec;
735 	return (struct exfat_dentry *)((*bh)->b_data + off);
736 }
737 
738 enum exfat_validate_dentry_mode {
739 	ES_MODE_STARTED,
740 	ES_MODE_GET_FILE_ENTRY,
741 	ES_MODE_GET_STRM_ENTRY,
742 	ES_MODE_GET_NAME_ENTRY,
743 	ES_MODE_GET_CRITICAL_SEC_ENTRY,
744 };
745 
746 static bool exfat_validate_entry(unsigned int type,
747 		enum exfat_validate_dentry_mode *mode)
748 {
749 	if (type == TYPE_UNUSED || type == TYPE_DELETED)
750 		return false;
751 
752 	switch (*mode) {
753 	case ES_MODE_STARTED:
754 		if  (type != TYPE_FILE && type != TYPE_DIR)
755 			return false;
756 		*mode = ES_MODE_GET_FILE_ENTRY;
757 		return true;
758 	case ES_MODE_GET_FILE_ENTRY:
759 		if (type != TYPE_STREAM)
760 			return false;
761 		*mode = ES_MODE_GET_STRM_ENTRY;
762 		return true;
763 	case ES_MODE_GET_STRM_ENTRY:
764 		if (type != TYPE_EXTEND)
765 			return false;
766 		*mode = ES_MODE_GET_NAME_ENTRY;
767 		return true;
768 	case ES_MODE_GET_NAME_ENTRY:
769 		if (type == TYPE_STREAM)
770 			return false;
771 		if (type != TYPE_EXTEND) {
772 			if (!(type & TYPE_CRITICAL_SEC))
773 				return false;
774 			*mode = ES_MODE_GET_CRITICAL_SEC_ENTRY;
775 		}
776 		return true;
777 	case ES_MODE_GET_CRITICAL_SEC_ENTRY:
778 		if (type == TYPE_EXTEND || type == TYPE_STREAM)
779 			return false;
780 		if ((type & TYPE_CRITICAL_SEC) != TYPE_CRITICAL_SEC)
781 			return false;
782 		return true;
783 	default:
784 		WARN_ON_ONCE(1);
785 		return false;
786 	}
787 }
788 
789 struct exfat_dentry *exfat_get_dentry_cached(
790 	struct exfat_entry_set_cache *es, int num)
791 {
792 	int off = es->start_off + num * DENTRY_SIZE;
793 	struct buffer_head *bh = es->bh[EXFAT_B_TO_BLK(off, es->sb)];
794 	char *p = bh->b_data + EXFAT_BLK_OFFSET(off, es->sb);
795 
796 	return (struct exfat_dentry *)p;
797 }
798 
799 /*
800  * Returns a set of dentries for a file or dir.
801  *
802  * Note It provides a direct pointer to bh->data via exfat_get_dentry_cached().
803  * User should call exfat_get_dentry_set() after setting 'modified' to apply
804  * changes made in this entry set to the real device.
805  *
806  * in:
807  *   sb+p_dir+entry: indicates a file/dir
808  *   type:  specifies how many dentries should be included.
809  * return:
810  *   pointer of entry set on success,
811  *   NULL on failure.
812  */
813 struct exfat_entry_set_cache *exfat_get_dentry_set(struct super_block *sb,
814 		struct exfat_chain *p_dir, int entry, unsigned int type)
815 {
816 	int ret, i, num_bh;
817 	unsigned int off, byte_offset, clu = 0;
818 	sector_t sec;
819 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
820 	struct exfat_entry_set_cache *es;
821 	struct exfat_dentry *ep;
822 	int num_entries;
823 	enum exfat_validate_dentry_mode mode = ES_MODE_STARTED;
824 	struct buffer_head *bh;
825 
826 	if (p_dir->dir == DIR_DELETED) {
827 		exfat_err(sb, "access to deleted dentry");
828 		return NULL;
829 	}
830 
831 	byte_offset = EXFAT_DEN_TO_B(entry);
832 	ret = exfat_walk_fat_chain(sb, p_dir, byte_offset, &clu);
833 	if (ret)
834 		return NULL;
835 
836 	es = kzalloc(sizeof(*es), GFP_KERNEL);
837 	if (!es)
838 		return NULL;
839 	es->sb = sb;
840 	es->modified = false;
841 
842 	/* byte offset in cluster */
843 	byte_offset = EXFAT_CLU_OFFSET(byte_offset, sbi);
844 
845 	/* byte offset in sector */
846 	off = EXFAT_BLK_OFFSET(byte_offset, sb);
847 	es->start_off = off;
848 
849 	/* sector offset in cluster */
850 	sec = EXFAT_B_TO_BLK(byte_offset, sb);
851 	sec += exfat_cluster_to_sector(sbi, clu);
852 
853 	bh = sb_bread(sb, sec);
854 	if (!bh)
855 		goto free_es;
856 	es->bh[es->num_bh++] = bh;
857 
858 	ep = exfat_get_dentry_cached(es, 0);
859 	if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
860 		goto free_es;
861 
862 	num_entries = type == ES_ALL_ENTRIES ?
863 		ep->dentry.file.num_ext + 1 : type;
864 	es->num_entries = num_entries;
865 
866 	num_bh = EXFAT_B_TO_BLK_ROUND_UP(off + num_entries * DENTRY_SIZE, sb);
867 	for (i = 1; i < num_bh; i++) {
868 		/* get the next sector */
869 		if (exfat_is_last_sector_in_cluster(sbi, sec)) {
870 			if (p_dir->flags == ALLOC_NO_FAT_CHAIN)
871 				clu++;
872 			else if (exfat_get_next_cluster(sb, &clu))
873 				goto free_es;
874 			sec = exfat_cluster_to_sector(sbi, clu);
875 		} else {
876 			sec++;
877 		}
878 
879 		bh = sb_bread(sb, sec);
880 		if (!bh)
881 			goto free_es;
882 		es->bh[es->num_bh++] = bh;
883 	}
884 
885 	/* validiate cached dentries */
886 	for (i = 1; i < num_entries; i++) {
887 		ep = exfat_get_dentry_cached(es, i);
888 		if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
889 			goto free_es;
890 	}
891 	return es;
892 
893 free_es:
894 	exfat_free_dentry_set(es, false);
895 	return NULL;
896 }
897 
898 enum {
899 	DIRENT_STEP_FILE,
900 	DIRENT_STEP_STRM,
901 	DIRENT_STEP_NAME,
902 	DIRENT_STEP_SECD,
903 };
904 
905 /*
906  * return values:
907  *   >= 0	: return dir entiry position with the name in dir
908  *   -EEXIST	: (root dir, ".") it is the root dir itself
909  *   -ENOENT	: entry with the name does not exist
910  *   -EIO	: I/O error
911  */
912 int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
913 		struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
914 		int num_entries, unsigned int type)
915 {
916 	int i, rewind = 0, dentry = 0, end_eidx = 0, num_ext = 0, len;
917 	int order, step, name_len = 0;
918 	int dentries_per_clu, num_empty = 0;
919 	unsigned int entry_type;
920 	unsigned short *uniname = NULL;
921 	struct exfat_chain clu;
922 	struct exfat_hint *hint_stat = &ei->hint_stat;
923 	struct exfat_hint_femp candi_empty;
924 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
925 
926 	dentries_per_clu = sbi->dentries_per_clu;
927 
928 	exfat_chain_dup(&clu, p_dir);
929 
930 	if (hint_stat->eidx) {
931 		clu.dir = hint_stat->clu;
932 		dentry = hint_stat->eidx;
933 		end_eidx = dentry;
934 	}
935 
936 	candi_empty.eidx = EXFAT_HINT_NONE;
937 rewind:
938 	order = 0;
939 	step = DIRENT_STEP_FILE;
940 	while (clu.dir != EXFAT_EOF_CLUSTER) {
941 		i = dentry & (dentries_per_clu - 1);
942 		for (; i < dentries_per_clu; i++, dentry++) {
943 			struct exfat_dentry *ep;
944 			struct buffer_head *bh;
945 
946 			if (rewind && dentry == end_eidx)
947 				goto not_found;
948 
949 			ep = exfat_get_dentry(sb, &clu, i, &bh, NULL);
950 			if (!ep)
951 				return -EIO;
952 
953 			entry_type = exfat_get_entry_type(ep);
954 
955 			if (entry_type == TYPE_UNUSED ||
956 			    entry_type == TYPE_DELETED) {
957 				step = DIRENT_STEP_FILE;
958 
959 				num_empty++;
960 				if (candi_empty.eidx == EXFAT_HINT_NONE &&
961 						num_empty == 1) {
962 					exfat_chain_set(&candi_empty.cur,
963 						clu.dir, clu.size, clu.flags);
964 				}
965 
966 				if (candi_empty.eidx == EXFAT_HINT_NONE &&
967 						num_empty >= num_entries) {
968 					candi_empty.eidx =
969 						dentry - (num_empty - 1);
970 					WARN_ON(candi_empty.eidx < 0);
971 					candi_empty.count = num_empty;
972 
973 					if (ei->hint_femp.eidx ==
974 							EXFAT_HINT_NONE ||
975 						candi_empty.eidx <=
976 							 ei->hint_femp.eidx) {
977 						memcpy(&ei->hint_femp,
978 							&candi_empty,
979 							sizeof(candi_empty));
980 					}
981 				}
982 
983 				brelse(bh);
984 				if (entry_type == TYPE_UNUSED)
985 					goto not_found;
986 				continue;
987 			}
988 
989 			num_empty = 0;
990 			candi_empty.eidx = EXFAT_HINT_NONE;
991 
992 			if (entry_type == TYPE_FILE || entry_type == TYPE_DIR) {
993 				step = DIRENT_STEP_FILE;
994 				if (type == TYPE_ALL || type == entry_type) {
995 					num_ext = ep->dentry.file.num_ext;
996 					step = DIRENT_STEP_STRM;
997 				}
998 				brelse(bh);
999 				continue;
1000 			}
1001 
1002 			if (entry_type == TYPE_STREAM) {
1003 				u16 name_hash;
1004 
1005 				if (step != DIRENT_STEP_STRM) {
1006 					step = DIRENT_STEP_FILE;
1007 					brelse(bh);
1008 					continue;
1009 				}
1010 				step = DIRENT_STEP_FILE;
1011 				name_hash = le16_to_cpu(
1012 						ep->dentry.stream.name_hash);
1013 				if (p_uniname->name_hash == name_hash &&
1014 				    p_uniname->name_len ==
1015 						ep->dentry.stream.name_len) {
1016 					step = DIRENT_STEP_NAME;
1017 					order = 1;
1018 					name_len = 0;
1019 				}
1020 				brelse(bh);
1021 				continue;
1022 			}
1023 
1024 			brelse(bh);
1025 			if (entry_type == TYPE_EXTEND) {
1026 				unsigned short entry_uniname[16], unichar;
1027 
1028 				if (step != DIRENT_STEP_NAME) {
1029 					step = DIRENT_STEP_FILE;
1030 					continue;
1031 				}
1032 
1033 				if (++order == 2)
1034 					uniname = p_uniname->name;
1035 				else
1036 					uniname += EXFAT_FILE_NAME_LEN;
1037 
1038 				len = exfat_extract_uni_name(ep, entry_uniname);
1039 				name_len += len;
1040 
1041 				unichar = *(uniname+len);
1042 				*(uniname+len) = 0x0;
1043 
1044 				if (exfat_uniname_ncmp(sb, uniname,
1045 					entry_uniname, len)) {
1046 					step = DIRENT_STEP_FILE;
1047 				} else if (p_uniname->name_len == name_len) {
1048 					if (order == num_ext)
1049 						goto found;
1050 					step = DIRENT_STEP_SECD;
1051 				}
1052 
1053 				*(uniname+len) = unichar;
1054 				continue;
1055 			}
1056 
1057 			if (entry_type &
1058 					(TYPE_CRITICAL_SEC | TYPE_BENIGN_SEC)) {
1059 				if (step == DIRENT_STEP_SECD) {
1060 					if (++order == num_ext)
1061 						goto found;
1062 					continue;
1063 				}
1064 			}
1065 			step = DIRENT_STEP_FILE;
1066 		}
1067 
1068 		if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1069 			if (--clu.size > 0)
1070 				clu.dir++;
1071 			else
1072 				clu.dir = EXFAT_EOF_CLUSTER;
1073 		} else {
1074 			if (exfat_get_next_cluster(sb, &clu.dir))
1075 				return -EIO;
1076 		}
1077 	}
1078 
1079 not_found:
1080 	/*
1081 	 * We started at not 0 index,so we should try to find target
1082 	 * from 0 index to the index we started at.
1083 	 */
1084 	if (!rewind && end_eidx) {
1085 		rewind = 1;
1086 		dentry = 0;
1087 		clu.dir = p_dir->dir;
1088 		/* reset empty hint */
1089 		num_empty = 0;
1090 		candi_empty.eidx = EXFAT_HINT_NONE;
1091 		goto rewind;
1092 	}
1093 
1094 	/* initialized hint_stat */
1095 	hint_stat->clu = p_dir->dir;
1096 	hint_stat->eidx = 0;
1097 	return -ENOENT;
1098 
1099 found:
1100 	/* next dentry we'll find is out of this cluster */
1101 	if (!((dentry + 1) & (dentries_per_clu - 1))) {
1102 		int ret = 0;
1103 
1104 		if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1105 			if (--clu.size > 0)
1106 				clu.dir++;
1107 			else
1108 				clu.dir = EXFAT_EOF_CLUSTER;
1109 		} else {
1110 			ret = exfat_get_next_cluster(sb, &clu.dir);
1111 		}
1112 
1113 		if (ret || clu.dir != EXFAT_EOF_CLUSTER) {
1114 			/* just initialized hint_stat */
1115 			hint_stat->clu = p_dir->dir;
1116 			hint_stat->eidx = 0;
1117 			return (dentry - num_ext);
1118 		}
1119 	}
1120 
1121 	hint_stat->clu = clu.dir;
1122 	hint_stat->eidx = dentry + 1;
1123 	return dentry - num_ext;
1124 }
1125 
1126 int exfat_count_ext_entries(struct super_block *sb, struct exfat_chain *p_dir,
1127 		int entry, struct exfat_dentry *ep)
1128 {
1129 	int i, count = 0;
1130 	unsigned int type;
1131 	struct exfat_dentry *ext_ep;
1132 	struct buffer_head *bh;
1133 
1134 	for (i = 0, entry++; i < ep->dentry.file.num_ext; i++, entry++) {
1135 		ext_ep = exfat_get_dentry(sb, p_dir, entry, &bh, NULL);
1136 		if (!ext_ep)
1137 			return -EIO;
1138 
1139 		type = exfat_get_entry_type(ext_ep);
1140 		brelse(bh);
1141 		if (type == TYPE_EXTEND || type == TYPE_STREAM)
1142 			count++;
1143 		else
1144 			break;
1145 	}
1146 	return count;
1147 }
1148 
1149 int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
1150 {
1151 	int i, count = 0;
1152 	int dentries_per_clu;
1153 	unsigned int entry_type;
1154 	struct exfat_chain clu;
1155 	struct exfat_dentry *ep;
1156 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
1157 	struct buffer_head *bh;
1158 
1159 	dentries_per_clu = sbi->dentries_per_clu;
1160 
1161 	exfat_chain_dup(&clu, p_dir);
1162 
1163 	while (clu.dir != EXFAT_EOF_CLUSTER) {
1164 		for (i = 0; i < dentries_per_clu; i++) {
1165 			ep = exfat_get_dentry(sb, &clu, i, &bh, NULL);
1166 			if (!ep)
1167 				return -EIO;
1168 			entry_type = exfat_get_entry_type(ep);
1169 			brelse(bh);
1170 
1171 			if (entry_type == TYPE_UNUSED)
1172 				return count;
1173 			if (entry_type != TYPE_DIR)
1174 				continue;
1175 			count++;
1176 		}
1177 
1178 		if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1179 			if (--clu.size > 0)
1180 				clu.dir++;
1181 			else
1182 				clu.dir = EXFAT_EOF_CLUSTER;
1183 		} else {
1184 			if (exfat_get_next_cluster(sb, &(clu.dir)))
1185 				return -EIO;
1186 		}
1187 	}
1188 
1189 	return count;
1190 }
1191