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