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 & EXFAT_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) & EXFAT_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(EXFAT_ATTR_SUBDIR);
415 } else if (type == TYPE_FILE) {
416 ep->type = EXFAT_FILE;
417 ep->dentry.file.attr = cpu_to_le16(EXFAT_ATTR_ARCHIVE);
418 }
419 }
420
exfat_init_stream_entry(struct exfat_dentry * ep,unsigned int start_clu,unsigned long long size)421 static void exfat_init_stream_entry(struct exfat_dentry *ep,
422 unsigned int start_clu, unsigned long long size)
423 {
424 memset(ep, 0, sizeof(*ep));
425 exfat_set_entry_type(ep, TYPE_STREAM);
426 if (size == 0)
427 ep->dentry.stream.flags = ALLOC_FAT_CHAIN;
428 else
429 ep->dentry.stream.flags = ALLOC_NO_FAT_CHAIN;
430 ep->dentry.stream.start_clu = cpu_to_le32(start_clu);
431 ep->dentry.stream.valid_size = cpu_to_le64(size);
432 ep->dentry.stream.size = cpu_to_le64(size);
433 }
434
exfat_init_name_entry(struct exfat_dentry * ep,unsigned short * uniname)435 static void exfat_init_name_entry(struct exfat_dentry *ep,
436 unsigned short *uniname)
437 {
438 int i;
439
440 exfat_set_entry_type(ep, TYPE_EXTEND);
441 ep->dentry.name.flags = 0x0;
442
443 for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) {
444 if (*uniname != 0x0) {
445 ep->dentry.name.unicode_0_14[i] = cpu_to_le16(*uniname);
446 uniname++;
447 } else {
448 ep->dentry.name.unicode_0_14[i] = 0x0;
449 }
450 }
451 }
452
exfat_init_dir_entry(struct exfat_entry_set_cache * es,unsigned int type,unsigned int start_clu,unsigned long long size,struct timespec64 * ts)453 void exfat_init_dir_entry(struct exfat_entry_set_cache *es,
454 unsigned int type, unsigned int start_clu,
455 unsigned long long size, struct timespec64 *ts)
456 {
457 struct super_block *sb = es->sb;
458 struct exfat_sb_info *sbi = EXFAT_SB(sb);
459 struct exfat_dentry *ep;
460
461 ep = exfat_get_dentry_cached(es, ES_IDX_FILE);
462 memset(ep, 0, sizeof(*ep));
463 exfat_set_entry_type(ep, type);
464 exfat_set_entry_time(sbi, ts,
465 &ep->dentry.file.create_tz,
466 &ep->dentry.file.create_time,
467 &ep->dentry.file.create_date,
468 &ep->dentry.file.create_time_cs);
469 exfat_set_entry_time(sbi, ts,
470 &ep->dentry.file.modify_tz,
471 &ep->dentry.file.modify_time,
472 &ep->dentry.file.modify_date,
473 &ep->dentry.file.modify_time_cs);
474 exfat_set_entry_time(sbi, ts,
475 &ep->dentry.file.access_tz,
476 &ep->dentry.file.access_time,
477 &ep->dentry.file.access_date,
478 NULL);
479
480 ep = exfat_get_dentry_cached(es, ES_IDX_STREAM);
481 exfat_init_stream_entry(ep, start_clu, size);
482 }
483
exfat_free_benign_secondary_clusters(struct inode * inode,struct exfat_dentry * ep)484 static void exfat_free_benign_secondary_clusters(struct inode *inode,
485 struct exfat_dentry *ep)
486 {
487 struct super_block *sb = inode->i_sb;
488 struct exfat_chain dir;
489 unsigned int start_clu =
490 le32_to_cpu(ep->dentry.generic_secondary.start_clu);
491 u64 size = le64_to_cpu(ep->dentry.generic_secondary.size);
492 unsigned char flags = ep->dentry.generic_secondary.flags;
493
494 if (!(flags & ALLOC_POSSIBLE) || !start_clu || !size)
495 return;
496
497 exfat_chain_set(&dir, start_clu,
498 EXFAT_B_TO_CLU_ROUND_UP(size, EXFAT_SB(sb)),
499 flags);
500 exfat_free_cluster(inode, &dir);
501 }
502
exfat_init_ext_entry(struct exfat_entry_set_cache * es,int num_entries,struct exfat_uni_name * p_uniname)503 void exfat_init_ext_entry(struct exfat_entry_set_cache *es, int num_entries,
504 struct exfat_uni_name *p_uniname)
505 {
506 int i;
507 unsigned short *uniname = p_uniname->name;
508 struct exfat_dentry *ep;
509
510 ep = exfat_get_dentry_cached(es, ES_IDX_FILE);
511 ep->dentry.file.num_ext = (unsigned char)(num_entries - 1);
512
513 ep = exfat_get_dentry_cached(es, ES_IDX_STREAM);
514 ep->dentry.stream.name_len = p_uniname->name_len;
515 ep->dentry.stream.name_hash = cpu_to_le16(p_uniname->name_hash);
516
517 for (i = ES_IDX_FIRST_FILENAME; i < num_entries; i++) {
518 ep = exfat_get_dentry_cached(es, i);
519 exfat_init_name_entry(ep, uniname);
520 uniname += EXFAT_FILE_NAME_LEN;
521 }
522
523 exfat_update_dir_chksum(es);
524 }
525
exfat_remove_entries(struct inode * inode,struct exfat_entry_set_cache * es,int order)526 void exfat_remove_entries(struct inode *inode, struct exfat_entry_set_cache *es,
527 int order)
528 {
529 int i;
530 struct exfat_dentry *ep;
531
532 for (i = order; i < es->num_entries; i++) {
533 ep = exfat_get_dentry_cached(es, i);
534
535 if (exfat_get_entry_type(ep) & TYPE_BENIGN_SEC)
536 exfat_free_benign_secondary_clusters(inode, ep);
537
538 exfat_set_entry_type(ep, TYPE_DELETED);
539 }
540
541 if (order < es->num_entries)
542 es->modified = true;
543 }
544
exfat_update_dir_chksum(struct exfat_entry_set_cache * es)545 void exfat_update_dir_chksum(struct exfat_entry_set_cache *es)
546 {
547 int chksum_type = CS_DIR_ENTRY, i;
548 unsigned short chksum = 0;
549 struct exfat_dentry *ep;
550
551 for (i = ES_IDX_FILE; i < es->num_entries; i++) {
552 ep = exfat_get_dentry_cached(es, i);
553 chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
554 chksum_type);
555 chksum_type = CS_DEFAULT;
556 }
557 ep = exfat_get_dentry_cached(es, ES_IDX_FILE);
558 ep->dentry.file.checksum = cpu_to_le16(chksum);
559 es->modified = true;
560 }
561
exfat_put_dentry_set(struct exfat_entry_set_cache * es,int sync)562 int exfat_put_dentry_set(struct exfat_entry_set_cache *es, int sync)
563 {
564 int i, err = 0;
565
566 if (es->modified)
567 err = exfat_update_bhs(es->bh, es->num_bh, sync);
568
569 for (i = 0; i < es->num_bh; i++)
570 if (err)
571 bforget(es->bh[i]);
572 else
573 brelse(es->bh[i]);
574
575 if (IS_DYNAMIC_ES(es))
576 kfree(es->bh);
577
578 return err;
579 }
580
exfat_walk_fat_chain(struct super_block * sb,struct exfat_chain * p_dir,unsigned int byte_offset,unsigned int * clu)581 static int exfat_walk_fat_chain(struct super_block *sb,
582 struct exfat_chain *p_dir, unsigned int byte_offset,
583 unsigned int *clu)
584 {
585 struct exfat_sb_info *sbi = EXFAT_SB(sb);
586 unsigned int clu_offset;
587 unsigned int cur_clu;
588
589 clu_offset = EXFAT_B_TO_CLU(byte_offset, sbi);
590 cur_clu = p_dir->dir;
591
592 if (p_dir->flags == ALLOC_NO_FAT_CHAIN) {
593 cur_clu += clu_offset;
594 } else {
595 while (clu_offset > 0) {
596 if (exfat_get_next_cluster(sb, &cur_clu))
597 return -EIO;
598 if (cur_clu == EXFAT_EOF_CLUSTER) {
599 exfat_fs_error(sb,
600 "invalid dentry access beyond EOF (clu : %u, eidx : %d)",
601 p_dir->dir,
602 EXFAT_B_TO_DEN(byte_offset));
603 return -EIO;
604 }
605 clu_offset--;
606 }
607 }
608
609 *clu = cur_clu;
610 return 0;
611 }
612
exfat_find_location(struct super_block * sb,struct exfat_chain * p_dir,int entry,sector_t * sector,int * offset)613 static int exfat_find_location(struct super_block *sb, struct exfat_chain *p_dir,
614 int entry, sector_t *sector, int *offset)
615 {
616 int ret;
617 unsigned int off, clu = 0;
618 struct exfat_sb_info *sbi = EXFAT_SB(sb);
619
620 off = EXFAT_DEN_TO_B(entry);
621
622 ret = exfat_walk_fat_chain(sb, p_dir, off, &clu);
623 if (ret)
624 return ret;
625
626 /* byte offset in cluster */
627 off = EXFAT_CLU_OFFSET(off, sbi);
628
629 /* byte offset in sector */
630 *offset = EXFAT_BLK_OFFSET(off, sb);
631
632 /* sector offset in cluster */
633 *sector = EXFAT_B_TO_BLK(off, sb);
634 *sector += exfat_cluster_to_sector(sbi, clu);
635 return 0;
636 }
637
638 #define EXFAT_MAX_RA_SIZE (128*1024)
exfat_dir_readahead(struct super_block * sb,sector_t sec)639 static int exfat_dir_readahead(struct super_block *sb, sector_t sec)
640 {
641 struct exfat_sb_info *sbi = EXFAT_SB(sb);
642 struct buffer_head *bh;
643 unsigned int max_ra_count = EXFAT_MAX_RA_SIZE >> sb->s_blocksize_bits;
644 unsigned int page_ra_count = PAGE_SIZE >> sb->s_blocksize_bits;
645 unsigned int adj_ra_count = max(sbi->sect_per_clus, page_ra_count);
646 unsigned int ra_count = min(adj_ra_count, max_ra_count);
647
648 /* Read-ahead is not required */
649 if (sbi->sect_per_clus == 1)
650 return 0;
651
652 if (sec < sbi->data_start_sector) {
653 exfat_err(sb, "requested sector is invalid(sect:%llu, root:%llu)",
654 (unsigned long long)sec, sbi->data_start_sector);
655 return -EIO;
656 }
657
658 /* Not sector aligned with ra_count, resize ra_count to page size */
659 if ((sec - sbi->data_start_sector) & (ra_count - 1))
660 ra_count = page_ra_count;
661
662 bh = sb_find_get_block(sb, sec);
663 if (!bh || !buffer_uptodate(bh)) {
664 unsigned int i;
665
666 for (i = 0; i < ra_count; i++)
667 sb_breadahead(sb, (sector_t)(sec + i));
668 }
669 brelse(bh);
670 return 0;
671 }
672
exfat_get_dentry(struct super_block * sb,struct exfat_chain * p_dir,int entry,struct buffer_head ** bh)673 struct exfat_dentry *exfat_get_dentry(struct super_block *sb,
674 struct exfat_chain *p_dir, int entry, struct buffer_head **bh)
675 {
676 unsigned int dentries_per_page = EXFAT_B_TO_DEN(PAGE_SIZE);
677 int off;
678 sector_t sec;
679
680 if (p_dir->dir == DIR_DELETED) {
681 exfat_err(sb, "abnormal access to deleted dentry");
682 return NULL;
683 }
684
685 if (exfat_find_location(sb, p_dir, entry, &sec, &off))
686 return NULL;
687
688 if (p_dir->dir != EXFAT_FREE_CLUSTER &&
689 !(entry & (dentries_per_page - 1)))
690 exfat_dir_readahead(sb, sec);
691
692 *bh = sb_bread(sb, sec);
693 if (!*bh)
694 return NULL;
695
696 return (struct exfat_dentry *)((*bh)->b_data + off);
697 }
698
699 enum exfat_validate_dentry_mode {
700 ES_MODE_GET_FILE_ENTRY,
701 ES_MODE_GET_STRM_ENTRY,
702 ES_MODE_GET_NAME_ENTRY,
703 ES_MODE_GET_CRITICAL_SEC_ENTRY,
704 ES_MODE_GET_BENIGN_SEC_ENTRY,
705 };
706
exfat_validate_entry(unsigned int type,enum exfat_validate_dentry_mode * mode)707 static bool exfat_validate_entry(unsigned int type,
708 enum exfat_validate_dentry_mode *mode)
709 {
710 if (type == TYPE_UNUSED || type == TYPE_DELETED)
711 return false;
712
713 switch (*mode) {
714 case ES_MODE_GET_FILE_ENTRY:
715 if (type != TYPE_STREAM)
716 return false;
717 *mode = ES_MODE_GET_STRM_ENTRY;
718 break;
719 case ES_MODE_GET_STRM_ENTRY:
720 if (type != TYPE_EXTEND)
721 return false;
722 *mode = ES_MODE_GET_NAME_ENTRY;
723 break;
724 case ES_MODE_GET_NAME_ENTRY:
725 if (type & TYPE_BENIGN_SEC)
726 *mode = ES_MODE_GET_BENIGN_SEC_ENTRY;
727 else if (type != TYPE_EXTEND)
728 return false;
729 break;
730 case ES_MODE_GET_BENIGN_SEC_ENTRY:
731 /* Assume unreconized benign secondary entry */
732 if (!(type & TYPE_BENIGN_SEC))
733 return false;
734 break;
735 default:
736 return false;
737 }
738
739 return true;
740 }
741
exfat_get_dentry_cached(struct exfat_entry_set_cache * es,int num)742 struct exfat_dentry *exfat_get_dentry_cached(
743 struct exfat_entry_set_cache *es, int num)
744 {
745 int off = es->start_off + num * DENTRY_SIZE;
746 struct buffer_head *bh = es->bh[EXFAT_B_TO_BLK(off, es->sb)];
747 char *p = bh->b_data + EXFAT_BLK_OFFSET(off, es->sb);
748
749 return (struct exfat_dentry *)p;
750 }
751
752 /*
753 * Returns a set of dentries.
754 *
755 * Note It provides a direct pointer to bh->data via exfat_get_dentry_cached().
756 * User should call exfat_get_dentry_set() after setting 'modified' to apply
757 * changes made in this entry set to the real device.
758 *
759 * in:
760 * sb+p_dir+entry: indicates a file/dir
761 * num_entries: specifies how many dentries should be included.
762 * It will be set to es->num_entries if it is not 0.
763 * If num_entries is 0, es->num_entries will be obtained
764 * from the first dentry.
765 * out:
766 * es: pointer of entry set on success.
767 * return:
768 * 0 on success
769 * -error code on failure
770 */
__exfat_get_dentry_set(struct exfat_entry_set_cache * es,struct super_block * sb,struct exfat_chain * p_dir,int entry,unsigned int num_entries)771 static int __exfat_get_dentry_set(struct exfat_entry_set_cache *es,
772 struct super_block *sb, struct exfat_chain *p_dir, int entry,
773 unsigned int num_entries)
774 {
775 int ret, i, num_bh;
776 unsigned int off;
777 sector_t sec;
778 struct exfat_sb_info *sbi = EXFAT_SB(sb);
779 struct buffer_head *bh;
780
781 if (p_dir->dir == DIR_DELETED) {
782 exfat_err(sb, "access to deleted dentry");
783 return -EIO;
784 }
785
786 ret = exfat_find_location(sb, p_dir, entry, &sec, &off);
787 if (ret)
788 return ret;
789
790 memset(es, 0, sizeof(*es));
791 es->sb = sb;
792 es->modified = false;
793 es->start_off = off;
794 es->bh = es->__bh;
795
796 bh = sb_bread(sb, sec);
797 if (!bh)
798 return -EIO;
799 es->bh[es->num_bh++] = bh;
800
801 if (num_entries == ES_ALL_ENTRIES) {
802 struct exfat_dentry *ep;
803
804 ep = exfat_get_dentry_cached(es, ES_IDX_FILE);
805 if (ep->type != EXFAT_FILE) {
806 brelse(bh);
807 return -EIO;
808 }
809
810 num_entries = ep->dentry.file.num_ext + 1;
811 }
812
813 es->num_entries = num_entries;
814
815 num_bh = EXFAT_B_TO_BLK_ROUND_UP(off + num_entries * DENTRY_SIZE, sb);
816 if (num_bh > ARRAY_SIZE(es->__bh)) {
817 es->bh = kmalloc_array(num_bh, sizeof(*es->bh), GFP_NOFS);
818 if (!es->bh) {
819 brelse(bh);
820 return -ENOMEM;
821 }
822 es->bh[0] = bh;
823 }
824
825 for (i = 1; i < num_bh; i++) {
826 /* get the next sector */
827 if (exfat_is_last_sector_in_cluster(sbi, sec)) {
828 unsigned int clu = exfat_sector_to_cluster(sbi, sec);
829
830 if (p_dir->flags == ALLOC_NO_FAT_CHAIN)
831 clu++;
832 else if (exfat_get_next_cluster(sb, &clu))
833 goto put_es;
834 sec = exfat_cluster_to_sector(sbi, clu);
835 } else {
836 sec++;
837 }
838
839 bh = sb_bread(sb, sec);
840 if (!bh)
841 goto put_es;
842 es->bh[es->num_bh++] = bh;
843 }
844
845 return 0;
846
847 put_es:
848 exfat_put_dentry_set(es, false);
849 return -EIO;
850 }
851
exfat_get_dentry_set(struct exfat_entry_set_cache * es,struct super_block * sb,struct exfat_chain * p_dir,int entry,unsigned int num_entries)852 int exfat_get_dentry_set(struct exfat_entry_set_cache *es,
853 struct super_block *sb, struct exfat_chain *p_dir,
854 int entry, unsigned int num_entries)
855 {
856 int ret, i;
857 struct exfat_dentry *ep;
858 enum exfat_validate_dentry_mode mode = ES_MODE_GET_FILE_ENTRY;
859
860 ret = __exfat_get_dentry_set(es, sb, p_dir, entry, num_entries);
861 if (ret < 0)
862 return ret;
863
864 /* validate cached dentries */
865 for (i = ES_IDX_STREAM; i < es->num_entries; i++) {
866 ep = exfat_get_dentry_cached(es, i);
867 if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
868 goto put_es;
869 }
870 return 0;
871
872 put_es:
873 exfat_put_dentry_set(es, false);
874 return -EIO;
875 }
876
exfat_validate_empty_dentry_set(struct exfat_entry_set_cache * es)877 static int exfat_validate_empty_dentry_set(struct exfat_entry_set_cache *es)
878 {
879 struct exfat_dentry *ep;
880 struct buffer_head *bh;
881 int i, off;
882 bool unused_hit = false;
883
884 /*
885 * ONLY UNUSED OR DELETED DENTRIES ARE ALLOWED:
886 * Although it violates the specification for a deleted entry to
887 * follow an unused entry, some exFAT implementations could work
888 * like this. Therefore, to improve compatibility, let's allow it.
889 */
890 for (i = 0; i < es->num_entries; i++) {
891 ep = exfat_get_dentry_cached(es, i);
892 if (ep->type == EXFAT_UNUSED) {
893 unused_hit = true;
894 } else if (!IS_EXFAT_DELETED(ep->type)) {
895 if (unused_hit)
896 goto err_used_follow_unused;
897 i++;
898 goto count_skip_entries;
899 }
900 }
901
902 return 0;
903
904 err_used_follow_unused:
905 off = es->start_off + (i << DENTRY_SIZE_BITS);
906 bh = es->bh[EXFAT_B_TO_BLK(off, es->sb)];
907
908 exfat_fs_error(es->sb,
909 "in sector %lld, dentry %d should be unused, but 0x%x",
910 bh->b_blocknr, off >> DENTRY_SIZE_BITS, ep->type);
911
912 return -EIO;
913
914 count_skip_entries:
915 es->num_entries = EXFAT_B_TO_DEN(EXFAT_BLK_TO_B(es->num_bh, es->sb) - es->start_off);
916 for (; i < es->num_entries; i++) {
917 ep = exfat_get_dentry_cached(es, i);
918 if (IS_EXFAT_DELETED(ep->type))
919 break;
920 }
921
922 return i;
923 }
924
925 /*
926 * Get an empty dentry set.
927 *
928 * in:
929 * sb+p_dir+entry: indicates the empty dentry location
930 * num_entries: specifies how many empty dentries should be included.
931 * out:
932 * es: pointer of empty dentry set on success.
933 * return:
934 * 0 : on success
935 * >0 : the dentries are not empty, the return value is the number of
936 * dentries to be skipped for the next lookup.
937 * <0 : on failure
938 */
exfat_get_empty_dentry_set(struct exfat_entry_set_cache * es,struct super_block * sb,struct exfat_chain * p_dir,int entry,unsigned int num_entries)939 int exfat_get_empty_dentry_set(struct exfat_entry_set_cache *es,
940 struct super_block *sb, struct exfat_chain *p_dir,
941 int entry, unsigned int num_entries)
942 {
943 int ret;
944
945 ret = __exfat_get_dentry_set(es, sb, p_dir, entry, num_entries);
946 if (ret < 0)
947 return ret;
948
949 ret = exfat_validate_empty_dentry_set(es);
950 if (ret)
951 exfat_put_dentry_set(es, false);
952
953 return ret;
954 }
955
exfat_reset_empty_hint(struct exfat_hint_femp * hint_femp)956 static inline void exfat_reset_empty_hint(struct exfat_hint_femp *hint_femp)
957 {
958 hint_femp->eidx = EXFAT_HINT_NONE;
959 hint_femp->count = 0;
960 }
961
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)962 static inline void exfat_set_empty_hint(struct exfat_inode_info *ei,
963 struct exfat_hint_femp *candi_empty, struct exfat_chain *clu,
964 int dentry, int num_entries, int entry_type)
965 {
966 if (ei->hint_femp.eidx == EXFAT_HINT_NONE ||
967 ei->hint_femp.eidx > dentry) {
968 int total_entries = EXFAT_B_TO_DEN(i_size_read(&ei->vfs_inode));
969
970 if (candi_empty->count == 0) {
971 candi_empty->cur = *clu;
972 candi_empty->eidx = dentry;
973 }
974
975 if (entry_type == TYPE_UNUSED)
976 candi_empty->count += total_entries - dentry;
977 else
978 candi_empty->count++;
979
980 if (candi_empty->count == num_entries ||
981 candi_empty->count + candi_empty->eidx == total_entries)
982 ei->hint_femp = *candi_empty;
983 }
984 }
985
986 enum {
987 DIRENT_STEP_FILE,
988 DIRENT_STEP_STRM,
989 DIRENT_STEP_NAME,
990 DIRENT_STEP_SECD,
991 };
992
993 /*
994 * @ei: inode info of parent directory
995 * @p_dir: directory structure of parent directory
996 * @num_entries:entry size of p_uniname
997 * @hint_opt: If p_uniname is found, filled with optimized dir/entry
998 * for traversing cluster chain.
999 * @return:
1000 * >= 0: file directory entry position where the name exists
1001 * -ENOENT: entry with the name does not exist
1002 * -EIO: I/O error
1003 */
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)1004 int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
1005 struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
1006 struct exfat_hint *hint_opt)
1007 {
1008 int i, rewind = 0, dentry = 0, end_eidx = 0, num_ext = 0, len;
1009 int order, step, name_len = 0;
1010 int dentries_per_clu;
1011 unsigned int entry_type;
1012 unsigned short *uniname = NULL;
1013 struct exfat_chain clu;
1014 struct exfat_hint *hint_stat = &ei->hint_stat;
1015 struct exfat_hint_femp candi_empty;
1016 struct exfat_sb_info *sbi = EXFAT_SB(sb);
1017 int num_entries = exfat_calc_num_entries(p_uniname);
1018 unsigned int clu_count = 0;
1019
1020 if (num_entries < 0)
1021 return num_entries;
1022
1023 dentries_per_clu = sbi->dentries_per_clu;
1024
1025 exfat_chain_dup(&clu, p_dir);
1026
1027 if (hint_stat->eidx) {
1028 clu.dir = hint_stat->clu;
1029 dentry = hint_stat->eidx;
1030 end_eidx = dentry;
1031 }
1032
1033 exfat_reset_empty_hint(&ei->hint_femp);
1034
1035 rewind:
1036 order = 0;
1037 step = DIRENT_STEP_FILE;
1038 exfat_reset_empty_hint(&candi_empty);
1039
1040 while (clu.dir != EXFAT_EOF_CLUSTER) {
1041 i = dentry & (dentries_per_clu - 1);
1042 for (; i < dentries_per_clu; i++, dentry++) {
1043 struct exfat_dentry *ep;
1044 struct buffer_head *bh;
1045
1046 if (rewind && dentry == end_eidx)
1047 goto not_found;
1048
1049 ep = exfat_get_dentry(sb, &clu, i, &bh);
1050 if (!ep)
1051 return -EIO;
1052
1053 entry_type = exfat_get_entry_type(ep);
1054
1055 if (entry_type == TYPE_UNUSED ||
1056 entry_type == TYPE_DELETED) {
1057 step = DIRENT_STEP_FILE;
1058
1059 exfat_set_empty_hint(ei, &candi_empty, &clu,
1060 dentry, num_entries,
1061 entry_type);
1062
1063 brelse(bh);
1064 if (entry_type == TYPE_UNUSED)
1065 goto not_found;
1066 continue;
1067 }
1068
1069 exfat_reset_empty_hint(&candi_empty);
1070
1071 if (entry_type == TYPE_FILE || entry_type == TYPE_DIR) {
1072 step = DIRENT_STEP_FILE;
1073 hint_opt->clu = clu.dir;
1074 hint_opt->eidx = i;
1075 num_ext = ep->dentry.file.num_ext;
1076 step = DIRENT_STEP_STRM;
1077 brelse(bh);
1078 continue;
1079 }
1080
1081 if (entry_type == TYPE_STREAM) {
1082 u16 name_hash;
1083
1084 if (step != DIRENT_STEP_STRM) {
1085 step = DIRENT_STEP_FILE;
1086 brelse(bh);
1087 continue;
1088 }
1089 step = DIRENT_STEP_FILE;
1090 name_hash = le16_to_cpu(
1091 ep->dentry.stream.name_hash);
1092 if (p_uniname->name_hash == name_hash &&
1093 p_uniname->name_len ==
1094 ep->dentry.stream.name_len) {
1095 step = DIRENT_STEP_NAME;
1096 order = 1;
1097 name_len = 0;
1098 }
1099 brelse(bh);
1100 continue;
1101 }
1102
1103 brelse(bh);
1104 if (entry_type == TYPE_EXTEND) {
1105 unsigned short entry_uniname[16], unichar;
1106
1107 if (step != DIRENT_STEP_NAME ||
1108 name_len >= MAX_NAME_LENGTH) {
1109 step = DIRENT_STEP_FILE;
1110 continue;
1111 }
1112
1113 if (++order == 2)
1114 uniname = p_uniname->name;
1115 else
1116 uniname += EXFAT_FILE_NAME_LEN;
1117
1118 len = exfat_extract_uni_name(ep, entry_uniname);
1119 name_len += len;
1120
1121 unichar = *(uniname+len);
1122 *(uniname+len) = 0x0;
1123
1124 if (exfat_uniname_ncmp(sb, uniname,
1125 entry_uniname, len)) {
1126 step = DIRENT_STEP_FILE;
1127 } else if (p_uniname->name_len == name_len) {
1128 if (order == num_ext)
1129 goto found;
1130 step = DIRENT_STEP_SECD;
1131 }
1132
1133 *(uniname+len) = unichar;
1134 continue;
1135 }
1136
1137 if (entry_type &
1138 (TYPE_CRITICAL_SEC | TYPE_BENIGN_SEC)) {
1139 if (step == DIRENT_STEP_SECD) {
1140 if (++order == num_ext)
1141 goto found;
1142 continue;
1143 }
1144 }
1145 step = DIRENT_STEP_FILE;
1146 }
1147
1148 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1149 if (--clu.size > 0)
1150 clu.dir++;
1151 else
1152 clu.dir = EXFAT_EOF_CLUSTER;
1153 } else {
1154 if (exfat_get_next_cluster(sb, &clu.dir))
1155 return -EIO;
1156
1157 /* break if the cluster chain includes a loop */
1158 if (unlikely(++clu_count > EXFAT_DATA_CLUSTER_COUNT(sbi)))
1159 goto not_found;
1160 }
1161 }
1162
1163 not_found:
1164 /*
1165 * We started at not 0 index,so we should try to find target
1166 * from 0 index to the index we started at.
1167 */
1168 if (!rewind && end_eidx) {
1169 rewind = 1;
1170 dentry = 0;
1171 clu.dir = p_dir->dir;
1172 goto rewind;
1173 }
1174
1175 /*
1176 * set the EXFAT_EOF_CLUSTER flag to avoid search
1177 * from the beginning again when allocated a new cluster
1178 */
1179 if (ei->hint_femp.eidx == EXFAT_HINT_NONE) {
1180 ei->hint_femp.cur.dir = EXFAT_EOF_CLUSTER;
1181 ei->hint_femp.eidx = p_dir->size * dentries_per_clu;
1182 ei->hint_femp.count = 0;
1183 }
1184
1185 /* initialized hint_stat */
1186 hint_stat->clu = p_dir->dir;
1187 hint_stat->eidx = 0;
1188 return -ENOENT;
1189
1190 found:
1191 /* next dentry we'll find is out of this cluster */
1192 if (!((dentry + 1) & (dentries_per_clu - 1))) {
1193 int ret = 0;
1194
1195 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1196 if (--clu.size > 0)
1197 clu.dir++;
1198 else
1199 clu.dir = EXFAT_EOF_CLUSTER;
1200 } else {
1201 ret = exfat_get_next_cluster(sb, &clu.dir);
1202 }
1203
1204 if (ret || clu.dir == EXFAT_EOF_CLUSTER) {
1205 /* just initialized hint_stat */
1206 hint_stat->clu = p_dir->dir;
1207 hint_stat->eidx = 0;
1208 return (dentry - num_ext);
1209 }
1210 }
1211
1212 hint_stat->clu = clu.dir;
1213 hint_stat->eidx = dentry + 1;
1214 return dentry - num_ext;
1215 }
1216
exfat_count_dir_entries(struct super_block * sb,struct exfat_chain * p_dir)1217 int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
1218 {
1219 int i, count = 0;
1220 int dentries_per_clu;
1221 unsigned int entry_type;
1222 unsigned int clu_count = 0;
1223 struct exfat_chain clu;
1224 struct exfat_dentry *ep;
1225 struct exfat_sb_info *sbi = EXFAT_SB(sb);
1226 struct buffer_head *bh;
1227
1228 dentries_per_clu = sbi->dentries_per_clu;
1229
1230 exfat_chain_dup(&clu, p_dir);
1231
1232 while (clu.dir != EXFAT_EOF_CLUSTER) {
1233 for (i = 0; i < dentries_per_clu; i++) {
1234 ep = exfat_get_dentry(sb, &clu, i, &bh);
1235 if (!ep)
1236 return -EIO;
1237 entry_type = exfat_get_entry_type(ep);
1238 brelse(bh);
1239
1240 if (entry_type == TYPE_UNUSED)
1241 return count;
1242 if (entry_type != TYPE_DIR)
1243 continue;
1244 count++;
1245 }
1246
1247 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1248 if (--clu.size > 0)
1249 clu.dir++;
1250 else
1251 clu.dir = EXFAT_EOF_CLUSTER;
1252 } else {
1253 if (exfat_get_next_cluster(sb, &(clu.dir)))
1254 return -EIO;
1255
1256 if (unlikely(++clu_count > sbi->used_clusters)) {
1257 exfat_fs_error(sb, "FAT or bitmap is corrupted");
1258 return -EIO;
1259 }
1260
1261 }
1262 }
1263
1264 return count;
1265 }
1266