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