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