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