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