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