This repository has been archived by the owner on Apr 15, 2020. It is now read-only.
forked from cryptomilk/kernel-sdfat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcore_fat.c
1464 lines (1200 loc) · 36.9 KB
/
core_fat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
/************************************************************************/
/* */
/* PROJECT : exFAT & FAT12/16/32 File System */
/* FILE : core_fat.c */
/* PURPOSE : FAT-fs core code for sdFAT */
/* */
/*----------------------------------------------------------------------*/
/* NOTES */
/* */
/* */
/************************************************************************/
#include <linux/version.h>
#include <linux/blkdev.h>
#include <linux/workqueue.h>
#include <linux/kernel.h>
#include <linux/log2.h>
#include "sdfat.h"
#include "core.h"
#include <asm/byteorder.h>
#include <asm/unaligned.h>
/*----------------------------------------------------------------------*/
/* Constant & Macro Definitions */
/*----------------------------------------------------------------------*/
#define MAX_LFN_ORDER (20)
/*
* MAX_EST_AU_SECT should be changed according to 32/64bits.
* On 32bit, 4KB page supports 512 clusters per AU.
* But, on 64bit, 4KB page can handle a half of total list_head of 32bit's.
* Bcause the size of list_head structure on 64bit increases twofold over 32bit.
*/
#if (BITS_PER_LONG == 64)
//#define MAX_EST_AU_SECT (16384) /* upto 8MB */
#define MAX_EST_AU_SECT (32768) /* upto 16MB, used more page for list_head */
#else
#define MAX_EST_AU_SECT (32768) /* upto 16MB */
#endif
/*======================================================================*/
/* Local Function Declarations */
/*======================================================================*/
static s32 __extract_uni_name_from_ext_entry(EXT_DENTRY_T *, u16 *, s32);
/*----------------------------------------------------------------------*/
/* Global Variable Definitions */
/*----------------------------------------------------------------------*/
/*----------------------------------------------------------------------*/
/* Local Variable Definitions */
/*----------------------------------------------------------------------*/
/*======================================================================*/
/* Local Function Definitions */
/*======================================================================*/
static u32 __calc_default_au_size(struct super_block *sb)
{
struct block_device *bdev = sb->s_bdev;
struct gendisk *disk;
struct request_queue *queue;
struct queue_limits *limit;
unsigned int est_au_sect = MAX_EST_AU_SECT;
unsigned int est_au_size = 0;
unsigned int queue_au_size = 0;
sector_t total_sect = 0;
/* we assumed that sector size is 512 bytes */
disk = bdev->bd_disk;
if (!disk)
goto out;
queue = disk->queue;
if (!queue)
goto out;
limit = &queue->limits;
queue_au_size = limit->discard_granularity;
/* estimate function(x) =
* (total_sect / 2) * 512 / 1024
* => (total_sect >> 1) >> 1)
* => (total_sect >> 2)
* => estimated bytes size
*
* ex1) <= 8GB -> 4MB
* ex2) 16GB -> 8MB
* ex3) >= 32GB -> 16MB
*/
total_sect = disk->part0.nr_sects;
est_au_size = total_sect >> 2;
/* au_size assumed that bytes per sector is 512 */
est_au_sect = est_au_size >> 9;
MMSG("DBG1: total_sect(%llu) est_au_size(%u) est_au_sect(%u)\n",
(u64)total_sect, est_au_size, est_au_sect);
if (est_au_sect <= 8192) {
/* 4MB */
est_au_sect = 8192;
} else if (est_au_sect <= 16384) {
/* 8MB */
est_au_sect = 16384;
} else {
/* 8MB or 16MB */
est_au_sect = MAX_EST_AU_SECT;
}
MMSG("DBG2: total_sect(%llu) est_au_size(%u) est_au_sect(%u)\n",
(u64)total_sect, est_au_size, est_au_sect);
if (est_au_size < queue_au_size &&
queue_au_size <= (MAX_EST_AU_SECT << 9)) {
DMSG("use queue_au_size(%u) instead of est_au_size(%u)\n",
queue_au_size, est_au_size);
est_au_sect = queue_au_size >> 9;
}
out:
if (sb->s_blocksize != 512) {
ASSERT(sb->s_blocksize_bits > 9);
sdfat_log_msg(sb, KERN_INFO,
"adjustment est_au_size by logical block size(%lu)",
sb->s_blocksize);
est_au_sect >>= (sb->s_blocksize_bits - 9);
}
sdfat_log_msg(sb, KERN_INFO, "set default AU sectors : %u "
"(queue_au_size : %u KB, disk_size : %llu MB)",
est_au_sect, queue_au_size >> 10, (u64)(total_sect >> 11));
return est_au_sect;
}
/*
* Cluster Management Functions
*/
static s32 fat_free_cluster(struct super_block *sb, CHAIN_T *p_chain, s32 do_relse)
{
s32 ret = -EIO;
s32 num_clusters = 0;
u32 clu, prev;
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
s32 i;
u64 sector;
/* invalid cluster number */
if (IS_CLUS_FREE(p_chain->dir) || IS_CLUS_EOF(p_chain->dir))
return 0;
/* no cluster to truncate */
if (!p_chain->size) {
DMSG("%s: cluster(%u) truncation is not required.",
__func__, p_chain->dir);
return 0;
}
/* check cluster validation */
if ((p_chain->dir < 2) && (p_chain->dir >= fsi->num_clusters)) {
EMSG("%s: invalid start cluster (%u)\n", __func__, p_chain->dir);
sdfat_debug_bug_on(1);
return -EIO;
}
set_sb_dirty(sb);
clu = p_chain->dir;
do {
if (do_relse) {
sector = CLUS_TO_SECT(fsi, clu);
for (i = 0; i < fsi->sect_per_clus; i++) {
if (dcache_release(sb, sector+i) == -EIO)
goto out;
}
}
prev = clu;
if (get_next_clus_safe(sb, &clu)) {
/* print more helpful log */
if (IS_CLUS_BAD(clu)) {
sdfat_log_msg(sb, KERN_ERR, "%s : "
"deleting bad cluster (clu[%u]->BAD)",
__func__, prev);
} else if (IS_CLUS_FREE(clu)) {
sdfat_log_msg(sb, KERN_ERR, "%s : "
"deleting free cluster (clu[%u]->FREE)",
__func__, prev);
}
goto out;
}
/* Free FAT chain */
if (fat_ent_set(sb, prev, CLUS_FREE))
goto out;
/* Update AMAP if needed */
if (fsi->amap) {
if (amap_release_cluster(sb, prev))
return -EIO;
}
num_clusters++;
} while (!IS_CLUS_EOF(clu));
/* success */
ret = 0;
out:
fsi->used_clusters -= num_clusters;
return ret;
} /* end of fat_free_cluster */
static s32 fat_alloc_cluster(struct super_block *sb, u32 num_alloc, CHAIN_T *p_chain, s32 dest)
{
s32 ret = -ENOSPC;
u32 i, num_clusters = 0, total_cnt;
u32 new_clu, last_clu = CLUS_EOF, read_clu;
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
total_cnt = fsi->num_clusters - CLUS_BASE;
if (unlikely(total_cnt < fsi->used_clusters)) {
sdfat_fs_error_ratelimit(sb,
"%s : invalid used clusters(t:%u,u:%u)\n",
__func__, total_cnt, fsi->used_clusters);
return -EIO;
}
if (num_alloc > total_cnt - fsi->used_clusters)
return -ENOSPC;
new_clu = p_chain->dir;
if (IS_CLUS_EOF(new_clu))
new_clu = fsi->clu_srch_ptr;
else if (new_clu >= fsi->num_clusters)
new_clu = CLUS_BASE;
set_sb_dirty(sb);
p_chain->dir = CLUS_EOF;
for (i = CLUS_BASE; i < fsi->num_clusters; i++) {
if (fat_ent_get(sb, new_clu, &read_clu)) {
ret = -EIO;
goto error;
}
if (IS_CLUS_FREE(read_clu)) {
if (fat_ent_set(sb, new_clu, CLUS_EOF)) {
ret = -EIO;
goto error;
}
num_clusters++;
if (IS_CLUS_EOF(p_chain->dir)) {
p_chain->dir = new_clu;
} else {
if (fat_ent_set(sb, last_clu, new_clu)) {
ret = -EIO;
goto error;
}
}
last_clu = new_clu;
if ((--num_alloc) == 0) {
fsi->clu_srch_ptr = new_clu;
fsi->used_clusters += num_clusters;
return 0;
}
}
if ((++new_clu) >= fsi->num_clusters)
new_clu = CLUS_BASE;
}
error:
if (num_clusters)
fat_free_cluster(sb, p_chain, 0);
return ret;
} /* end of fat_alloc_cluster */
static s32 fat_count_used_clusters(struct super_block *sb, u32 *ret_count)
{
s32 i;
u32 clu, count = 0;
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
for (i = CLUS_BASE; i < fsi->num_clusters; i++) {
if (fat_ent_get(sb, i, &clu))
return -EIO;
if (!IS_CLUS_FREE(clu))
count++;
}
*ret_count = count;
return 0;
} /* end of fat_count_used_clusters */
/*
* Directory Entry Management Functions
*/
static u32 fat_get_entry_type(DENTRY_T *p_entry)
{
DOS_DENTRY_T *ep = (DOS_DENTRY_T *)p_entry;
/* first byte of 32bytes dummy */
if (*(ep->name) == MSDOS_UNUSED)
return TYPE_UNUSED;
/* 0xE5 of Kanji Japanese is replaced to 0x05 */
else if (*(ep->name) == MSDOS_DELETED)
return TYPE_DELETED;
/* 11th byte of 32bytes dummy */
else if ((ep->attr & ATTR_EXTEND_MASK) == ATTR_EXTEND)
return TYPE_EXTEND;
else if (!(ep->attr & (ATTR_SUBDIR | ATTR_VOLUME)))
return TYPE_FILE;
else if ((ep->attr & (ATTR_SUBDIR | ATTR_VOLUME)) == ATTR_SUBDIR)
return TYPE_DIR;
else if ((ep->attr & (ATTR_SUBDIR | ATTR_VOLUME)) == ATTR_VOLUME)
return TYPE_VOLUME;
return TYPE_INVALID;
} /* end of fat_get_entry_type */
static void fat_set_entry_type(DENTRY_T *p_entry, u32 type)
{
DOS_DENTRY_T *ep = (DOS_DENTRY_T *)p_entry;
if (type == TYPE_UNUSED)
*(ep->name) = MSDOS_UNUSED; /* 0x0 */
else if (type == TYPE_DELETED)
*(ep->name) = MSDOS_DELETED; /* 0xE5 */
else if (type == TYPE_EXTEND)
ep->attr = ATTR_EXTEND;
else if (type == TYPE_DIR)
ep->attr = ATTR_SUBDIR;
else if (type == TYPE_FILE)
ep->attr = ATTR_ARCHIVE;
else if (type == TYPE_SYMLINK)
ep->attr = ATTR_ARCHIVE | ATTR_SYMLINK;
} /* end of fat_set_entry_type */
static u32 fat_get_entry_attr(DENTRY_T *p_entry)
{
DOS_DENTRY_T *ep = (DOS_DENTRY_T *)p_entry;
return (u32)ep->attr;
} /* end of fat_get_entry_attr */
static void fat_set_entry_attr(DENTRY_T *p_entry, u32 attr)
{
DOS_DENTRY_T *ep = (DOS_DENTRY_T *)p_entry;
ep->attr = (u8)attr;
} /* end of fat_set_entry_attr */
static u8 fat_get_entry_flag(DENTRY_T *p_entry)
{
return 0x01;
} /* end of fat_get_entry_flag */
static void fat_set_entry_flag(DENTRY_T *p_entry, u8 flags)
{
} /* end of fat_set_entry_flag */
static u32 fat_get_entry_clu0(DENTRY_T *p_entry)
{
DOS_DENTRY_T *ep = (DOS_DENTRY_T *)p_entry;
/* FIXME : is ok? */
return(((u32)(le16_to_cpu(ep->start_clu_hi)) << 16) | le16_to_cpu(ep->start_clu_lo));
} /* end of fat_get_entry_clu0 */
static void fat_set_entry_clu0(DENTRY_T *p_entry, u32 start_clu)
{
DOS_DENTRY_T *ep = (DOS_DENTRY_T *)p_entry;
ep->start_clu_lo = cpu_to_le16(CLUSTER_16(start_clu));
ep->start_clu_hi = cpu_to_le16(CLUSTER_16(start_clu >> 16));
} /* end of fat_set_entry_clu0 */
static u64 fat_get_entry_size(DENTRY_T *p_entry)
{
DOS_DENTRY_T *ep = (DOS_DENTRY_T *)p_entry;
return (u64)le32_to_cpu(ep->size);
} /* end of fat_get_entry_size */
static void fat_set_entry_size(DENTRY_T *p_entry, u64 size)
{
DOS_DENTRY_T *ep = (DOS_DENTRY_T *)p_entry;
ep->size = cpu_to_le32((u32)size);
} /* end of fat_set_entry_size */
static void fat_get_entry_time(DENTRY_T *p_entry, TIMESTAMP_T *tp, u8 mode)
{
u16 t = 0x00, d = 0x21;
DOS_DENTRY_T *ep = (DOS_DENTRY_T *) p_entry;
switch (mode) {
case TM_CREATE:
t = le16_to_cpu(ep->create_time);
d = le16_to_cpu(ep->create_date);
break;
case TM_MODIFY:
t = le16_to_cpu(ep->modify_time);
d = le16_to_cpu(ep->modify_date);
break;
}
tp->sec = (t & 0x001F) << 1;
tp->min = (t >> 5) & 0x003F;
tp->hour = (t >> 11);
tp->day = (d & 0x001F);
tp->mon = (d >> 5) & 0x000F;
tp->year = (d >> 9);
} /* end of fat_get_entry_time */
static void fat_set_entry_time(DENTRY_T *p_entry, TIMESTAMP_T *tp, u8 mode)
{
u16 t, d;
DOS_DENTRY_T *ep = (DOS_DENTRY_T *) p_entry;
t = (tp->hour << 11) | (tp->min << 5) | (tp->sec >> 1);
d = (tp->year << 9) | (tp->mon << 5) | tp->day;
switch (mode) {
case TM_CREATE:
ep->create_time = cpu_to_le16(t);
ep->create_date = cpu_to_le16(d);
break;
case TM_MODIFY:
ep->modify_time = cpu_to_le16(t);
ep->modify_date = cpu_to_le16(d);
break;
}
} /* end of fat_set_entry_time */
static void __init_dos_entry(struct super_block *sb, DOS_DENTRY_T *ep, u32 type, u32 start_clu)
{
TIMESTAMP_T tm, *tp;
fat_set_entry_type((DENTRY_T *) ep, type);
ep->start_clu_lo = cpu_to_le16(CLUSTER_16(start_clu));
ep->start_clu_hi = cpu_to_le16(CLUSTER_16(start_clu >> 16));
ep->size = 0;
tp = tm_now(SDFAT_SB(sb), &tm);
fat_set_entry_time((DENTRY_T *) ep, tp, TM_CREATE);
fat_set_entry_time((DENTRY_T *) ep, tp, TM_MODIFY);
ep->access_date = 0;
ep->create_time_ms = 0;
} /* end of __init_dos_entry */
static void __init_ext_entry(EXT_DENTRY_T *ep, s32 order, u8 chksum, u16 *uniname)
{
s32 i;
u8 end = false;
fat_set_entry_type((DENTRY_T *) ep, TYPE_EXTEND);
ep->order = (u8) order;
ep->sysid = 0;
ep->checksum = chksum;
ep->start_clu = 0;
/* unaligned name */
for (i = 0; i < 5; i++) {
if (!end) {
put_unaligned_le16(*uniname, &(ep->unicode_0_4[i<<1]));
if (*uniname == 0x0)
end = true;
else
uniname++;
} else {
put_unaligned_le16(0xFFFF, &(ep->unicode_0_4[i<<1]));
}
}
/* aligned name */
for (i = 0; i < 6; i++) {
if (!end) {
ep->unicode_5_10[i] = cpu_to_le16(*uniname);
if (*uniname == 0x0)
end = true;
else
uniname++;
} else {
ep->unicode_5_10[i] = cpu_to_le16(0xFFFF);
}
}
/* aligned name */
for (i = 0; i < 2; i++) {
if (!end) {
ep->unicode_11_12[i] = cpu_to_le16(*uniname);
if (*uniname == 0x0)
end = true;
else
uniname++;
} else {
ep->unicode_11_12[i] = cpu_to_le16(0xFFFF);
}
}
} /* end of __init_ext_entry */
static s32 fat_init_dir_entry(struct super_block *sb, CHAIN_T *p_dir, s32 entry, u32 type,
u32 start_clu, u64 size)
{
u64 sector;
DOS_DENTRY_T *dos_ep;
dos_ep = (DOS_DENTRY_T *) get_dentry_in_dir(sb, p_dir, entry, §or);
if (!dos_ep)
return -EIO;
__init_dos_entry(sb, dos_ep, type, start_clu);
dcache_modify(sb, sector);
return 0;
} /* end of fat_init_dir_entry */
static s32 fat_init_ext_entry(struct super_block *sb, CHAIN_T *p_dir, s32 entry, s32 num_entries,
UNI_NAME_T *p_uniname, DOS_NAME_T *p_dosname)
{
s32 i;
u64 sector;
u8 chksum;
u16 *uniname = p_uniname->name;
DOS_DENTRY_T *dos_ep;
EXT_DENTRY_T *ext_ep;
dos_ep = (DOS_DENTRY_T *) get_dentry_in_dir(sb, p_dir, entry, §or);
if (!dos_ep)
return -EIO;
dos_ep->lcase = p_dosname->name_case;
memcpy(dos_ep->name, p_dosname->name, DOS_NAME_LENGTH);
if (dcache_modify(sb, sector))
return -EIO;
if ((--num_entries) > 0) {
chksum = calc_chksum_1byte((void *) dos_ep->name, DOS_NAME_LENGTH, 0);
for (i = 1; i < num_entries; i++) {
ext_ep = (EXT_DENTRY_T *) get_dentry_in_dir(sb, p_dir, entry-i, §or);
if (!ext_ep)
return -EIO;
__init_ext_entry(ext_ep, i, chksum, uniname);
if (dcache_modify(sb, sector))
return -EIO;
uniname += 13;
}
ext_ep = (EXT_DENTRY_T *) get_dentry_in_dir(sb, p_dir, entry-i, §or);
if (!ext_ep)
return -EIO;
__init_ext_entry(ext_ep, i+MSDOS_LAST_LFN, chksum, uniname);
if (dcache_modify(sb, sector))
return -EIO;
}
return 0;
} /* end of fat_init_ext_entry */
static s32 fat_delete_dir_entry(struct super_block *sb, CHAIN_T *p_dir, s32 entry, s32 order, s32 num_entries)
{
s32 i;
u64 sector;
DENTRY_T *ep;
for (i = num_entries-1; i >= order; i--) {
ep = get_dentry_in_dir(sb, p_dir, entry-i, §or);
if (!ep)
return -EIO;
fat_set_entry_type(ep, TYPE_DELETED);
if (dcache_modify(sb, sector))
return -EIO;
}
return 0;
}
/* return values of fat_find_dir_entry()
* >= 0 : return dir entiry position with the name in dir
* -EEXIST : (root dir, ".") it is the root dir itself
* -ENOENT : entry with the name does not exist
* -EIO : I/O error
*/
static inline s32 __get_dentries_per_clu(FS_INFO_T *fsi, s32 clu)
{
if (IS_CLUS_FREE(clu)) /* FAT16 root_dir */
return fsi->dentries_in_root;
return fsi->dentries_per_clu;
}
static s32 fat_find_dir_entry(struct super_block *sb, FILE_ID_T *fid,
CHAIN_T *p_dir, UNI_NAME_T *p_uniname, s32 num_entries, DOS_NAME_T *p_dosname, u32 type)
{
s32 i, rewind = 0, dentry = 0, end_eidx = 0;
s32 chksum = 0, lfn_ord = 0, lfn_len = 0;
s32 dentries_per_clu, num_empty = 0;
u32 entry_type;
u16 entry_uniname[14], *uniname = NULL;
CHAIN_T clu;
DENTRY_T *ep;
HINT_T *hint_stat = &fid->hint_stat;
HINT_FEMP_T candi_empty;
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
/*
* REMARK:
* DOT and DOTDOT are handled by VFS layer
*/
dentries_per_clu = __get_dentries_per_clu(fsi, p_dir->dir);
clu.dir = p_dir->dir;
clu.flags = p_dir->flags;
if (hint_stat->eidx) {
clu.dir = hint_stat->clu;
dentry = hint_stat->eidx;
end_eidx = dentry;
}
candi_empty.eidx = -1;
MMSG("lookup dir= %s\n", p_dosname->name);
rewind:
while (!IS_CLUS_EOF(clu.dir)) {
i = dentry % dentries_per_clu;
for (; i < dentries_per_clu; i++, dentry++) {
if (rewind && (dentry == end_eidx))
goto not_found;
ep = get_dentry_in_dir(sb, &clu, i, NULL);
if (!ep)
return -EIO;
entry_type = fat_get_entry_type(ep);
/*
* Most directory entries have long name,
* So, we check extend directory entry first.
*/
if (entry_type == TYPE_EXTEND) {
EXT_DENTRY_T *ext_ep = (EXT_DENTRY_T *)ep;
u32 cur_ord = (u32)ext_ep->order;
u32 cur_chksum = (s32)ext_ep->checksum;
s32 len = 13;
u16 unichar;
num_empty = 0;
candi_empty.eidx = -1;
/* check whether new lfn or not */
if (cur_ord & MSDOS_LAST_LFN) {
cur_ord &= ~(MSDOS_LAST_LFN);
chksum = cur_chksum;
len = (13 * (cur_ord-1));
uniname = (p_uniname->name + len);
lfn_ord = cur_ord + 1;
lfn_len = 0;
/* check minimum name length */
if (cur_ord &&
(len > p_uniname->name_len)) {
/* MISMATCHED NAME LENGTH */
lfn_len = -1;
}
len = 0;
}
/* invalid lfn order */
if (!cur_ord || (cur_ord > MAX_LFN_ORDER) ||
((cur_ord + 1) != lfn_ord))
goto reset_dentry_set;
/* check checksum of directory entry set */
if (cur_chksum != chksum)
goto reset_dentry_set;
/* update order for next dentry */
lfn_ord = cur_ord;
/* check whether mismatched lfn or not */
if (lfn_len == -1) {
/* MISMATCHED LFN DENTRY SET */
continue;
}
if (!uniname) {
sdfat_fs_error(sb,
"%s : abnormal dentry "
"(start_clu[%u], "
"idx[%u])", __func__,
p_dir->dir, dentry);
sdfat_debug_bug_on(1);
return -EIO;
}
/* update position of name buffer */
uniname -= len;
/* get utf16 characters saved on this entry */
len = __extract_uni_name_from_ext_entry(ext_ep, entry_uniname, lfn_ord);
/* replace last char to null */
unichar = *(uniname+len);
*(uniname+len) = (u16)0x0;
/* uniname ext_dentry unit compare repeatdly */
if (nls_cmp_uniname(sb, uniname, entry_uniname)) {
/* DO HANDLE WRONG NAME */
lfn_len = -1;
} else {
/* add matched chars length */
lfn_len += len;
}
/* restore previous character */
*(uniname+len) = unichar;
/* jump to check next dentry */
continue;
} else if ((entry_type == TYPE_FILE) || (entry_type == TYPE_DIR)) {
DOS_DENTRY_T *dos_ep = (DOS_DENTRY_T *)ep;
u32 cur_chksum = (s32)calc_chksum_1byte(
(void *) dos_ep->name,
DOS_NAME_LENGTH, 0);
num_empty = 0;
candi_empty.eidx = -1;
MMSG("checking dir= %c%c%c%c%c%c%c%c%c%c%c\n",
dos_ep->name[0], dos_ep->name[1],
dos_ep->name[2], dos_ep->name[3],
dos_ep->name[4], dos_ep->name[5],
dos_ep->name[6], dos_ep->name[7],
dos_ep->name[8], dos_ep->name[9],
dos_ep->name[10]);
/*
* if there is no valid long filename,
* we should check short filename.
*/
if (!lfn_len || (cur_chksum != chksum)) {
/* check shortname */
if ((p_dosname->name[0] != '\0') &&
!nls_cmp_sfn(sb,
p_dosname->name,
dos_ep->name)) {
goto found;
}
/* check name length */
} else if ((lfn_len > 0) &&
((s32)p_uniname->name_len ==
lfn_len)) {
goto found;
}
/* DO HANDLE MISMATCHED SFN, FALL THROUGH */
} else if ((entry_type == TYPE_UNUSED) || (entry_type == TYPE_DELETED)) {
num_empty++;
if (candi_empty.eidx == -1) {
if (num_empty == 1) {
candi_empty.cur.dir = clu.dir;
candi_empty.cur.size = clu.size;
candi_empty.cur.flags = clu.flags;
}
if (num_empty >= num_entries) {
candi_empty.eidx = dentry - (num_empty - 1);
ASSERT(0 <= candi_empty.eidx);
candi_empty.count = num_empty;
if ((fid->hint_femp.eidx == -1) ||
(candi_empty.eidx <= fid->hint_femp.eidx)) {
memcpy(&fid->hint_femp,
&candi_empty,
sizeof(HINT_FEMP_T));
}
}
}
if (entry_type == TYPE_UNUSED)
goto not_found;
/* FALL THROUGH */
}
reset_dentry_set:
/* TYPE_DELETED, TYPE_VOLUME OR MISMATCHED SFN */
lfn_ord = 0;
lfn_len = 0;
chksum = 0;
}
if (IS_CLUS_FREE(p_dir->dir))
break; /* FAT16 root_dir */
if (get_next_clus_safe(sb, &clu.dir))
return -EIO;
}
not_found:
/* we started at not 0 index,so we should try to find target
* from 0 index to the index we started at.
*/
if (!rewind && end_eidx) {
rewind = 1;
dentry = 0;
clu.dir = p_dir->dir;
/* reset dentry set */
lfn_ord = 0;
lfn_len = 0;
chksum = 0;
/* reset empty hint_*/
num_empty = 0;
candi_empty.eidx = -1;
goto rewind;
}
/* initialized hint_stat */
hint_stat->clu = p_dir->dir;
hint_stat->eidx = 0;
return -ENOENT;
found:
/* next dentry we'll find is out of this cluster */
if (!((dentry + 1) % dentries_per_clu)) {
int ret = 0;
/* FAT16 root_dir */
if (IS_CLUS_FREE(p_dir->dir))
clu.dir = CLUS_EOF;
else
ret = get_next_clus_safe(sb, &clu.dir);
if (ret || IS_CLUS_EOF(clu.dir)) {
/* just initialized hint_stat */
hint_stat->clu = p_dir->dir;
hint_stat->eidx = 0;
return dentry;
}
}
hint_stat->clu = clu.dir;
hint_stat->eidx = dentry + 1;
return dentry;
} /* end of fat_find_dir_entry */
/* returns -EIO on error */
static s32 fat_count_ext_entries(struct super_block *sb, CHAIN_T *p_dir, s32 entry, DENTRY_T *p_entry)
{
s32 count = 0;
u8 chksum;
DOS_DENTRY_T *dos_ep = (DOS_DENTRY_T *) p_entry;
EXT_DENTRY_T *ext_ep;
chksum = calc_chksum_1byte((void *) dos_ep->name, DOS_NAME_LENGTH, 0);
for (entry--; entry >= 0; entry--) {
ext_ep = (EXT_DENTRY_T *)get_dentry_in_dir(sb, p_dir, entry, NULL);
if (!ext_ep)
return -EIO;
if ((fat_get_entry_type((DENTRY_T *)ext_ep) == TYPE_EXTEND) &&
(ext_ep->checksum == chksum)) {
count++;
if (ext_ep->order > MSDOS_LAST_LFN)
return count;
} else {
return count;
}
}
return count;
}
/*
* Name Conversion Functions
*/
static s32 __extract_uni_name_from_ext_entry(EXT_DENTRY_T *ep, u16 *uniname, s32 order)
{
s32 i, len = 0;
for (i = 0; i < 5; i++) {
*uniname = get_unaligned_le16(&(ep->unicode_0_4[i<<1]));
if (*uniname == 0x0)
return len;
uniname++;
len++;
}
if (order < 20) {
for (i = 0; i < 6; i++) {
/* FIXME : unaligned? */
*uniname = le16_to_cpu(ep->unicode_5_10[i]);
if (*uniname == 0x0)
return len;
uniname++;
len++;
}
} else {
for (i = 0; i < 4; i++) {
/* FIXME : unaligned? */
*uniname = le16_to_cpu(ep->unicode_5_10[i]);
if (*uniname == 0x0)
return len;
uniname++;
len++;
}
*uniname = 0x0; /* uniname[MAX_NAME_LENGTH] */
return len;
}
for (i = 0; i < 2; i++) {
/* FIXME : unaligned? */
*uniname = le16_to_cpu(ep->unicode_11_12[i]);
if (*uniname == 0x0)
return len;
uniname++;
len++;
}
*uniname = 0x0;
return len;
} /* end of __extract_uni_name_from_ext_entry */
static void fat_get_uniname_from_ext_entry(struct super_block *sb, CHAIN_T *p_dir, s32 entry, u16 *uniname)
{
u32 i;
u16 *name = uniname;
u32 chksum;
DOS_DENTRY_T *dos_ep =
(DOS_DENTRY_T *)get_dentry_in_dir(sb, p_dir, entry, NULL);
if (unlikely(!dos_ep))
goto invalid_lfn;
chksum = (u32)calc_chksum_1byte(
(void *) dos_ep->name,
DOS_NAME_LENGTH, 0);
for (entry--, i = 1; entry >= 0; entry--, i++) {
EXT_DENTRY_T *ep;
ep = (EXT_DENTRY_T *)get_dentry_in_dir(sb, p_dir, entry, NULL);
if (!ep)
goto invalid_lfn;
if (fat_get_entry_type((DENTRY_T *) ep) != TYPE_EXTEND)
goto invalid_lfn;
if (chksum != (u32)ep->checksum)
goto invalid_lfn;
if (i != (u32)(ep->order & ~(MSDOS_LAST_LFN)))
goto invalid_lfn;
__extract_uni_name_from_ext_entry(ep, name, (s32)i);
if (ep->order & MSDOS_LAST_LFN)