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