-
Notifications
You must be signed in to change notification settings - Fork 20
/
core_exfat.c
1581 lines (1320 loc) · 40.4 KB
/
core_exfat.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_exfat.c */
/* PURPOSE : exFAT-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 */
/*----------------------------------------------------------------------*/
/*----------------------------------------------------------------------*/
/* Global Variable Definitions */
/*----------------------------------------------------------------------*/
/*----------------------------------------------------------------------*/
/* Local Variable Definitions */
/*----------------------------------------------------------------------*/
static u8 free_bit[] = {
0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2,/* 0 ~ 19*/
0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3,/* 20 ~ 39*/
0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2,/* 40 ~ 59*/
0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,/* 60 ~ 79*/
0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2,/* 80 ~ 99*/
0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3,/*100 ~ 119*/
0, 1, 0, 2, 0, 1, 0, 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2,/*120 ~ 139*/
0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5,/*140 ~ 159*/
0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2,/*160 ~ 179*/
0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3,/*180 ~ 199*/
0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2,/*200 ~ 219*/
0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,/*220 ~ 239*/
0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 /*240 ~ 254*/
};
static u8 used_bit[] = {
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3,/* 0 ~ 19*/
2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4,/* 20 ~ 39*/
2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5,/* 40 ~ 59*/
4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,/* 60 ~ 79*/
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4,/* 80 ~ 99*/
3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6,/*100 ~ 119*/
4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4,/*120 ~ 139*/
3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,/*140 ~ 159*/
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5,/*160 ~ 179*/
4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5,/*180 ~ 199*/
3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6,/*200 ~ 219*/
5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,/*220 ~ 239*/
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 /*240 ~ 255*/
};
/*======================================================================*/
/* Local Function Definitions */
/*======================================================================*/
/*
* Directory Entry Management Functions
*/
static u32 exfat_get_entry_type(DENTRY_T *p_entry)
{
FILE_DENTRY_T *ep = (FILE_DENTRY_T *) p_entry;
if (ep->type == EXFAT_UNUSED)
return TYPE_UNUSED;
if (ep->type < 0x80)
return TYPE_DELETED;
if (ep->type == 0x80)
return TYPE_INVALID;
if (ep->type < 0xA0) {
if (ep->type == 0x81)
return TYPE_BITMAP;
if (ep->type == 0x82)
return TYPE_UPCASE;
if (ep->type == 0x83)
return TYPE_VOLUME;
if (ep->type == 0x85) {
if (le16_to_cpu(ep->attr) & ATTR_SUBDIR)
return TYPE_DIR;
return TYPE_FILE;
}
return TYPE_CRITICAL_PRI;
}
if (ep->type < 0xC0) {
if (ep->type == 0xA0)
return TYPE_GUID;
if (ep->type == 0xA1)
return TYPE_PADDING;
if (ep->type == 0xA2)
return TYPE_ACLTAB;
return TYPE_BENIGN_PRI;
}
if (ep->type < 0xE0) {
if (ep->type == 0xC0)
return TYPE_STREAM;
if (ep->type == 0xC1)
return TYPE_EXTEND;
if (ep->type == 0xC2)
return TYPE_ACL;
return TYPE_CRITICAL_SEC;
}
return TYPE_BENIGN_SEC;
} /* end of exfat_get_entry_type */
static void exfat_set_entry_type(DENTRY_T *p_entry, u32 type)
{
FILE_DENTRY_T *ep = (FILE_DENTRY_T *) p_entry;
if (type == TYPE_UNUSED) {
ep->type = 0x0;
} else if (type == TYPE_DELETED) {
ep->type &= ~0x80;
} else if (type == TYPE_STREAM) {
ep->type = 0xC0;
} else if (type == TYPE_EXTEND) {
ep->type = 0xC1;
} else if (type == TYPE_BITMAP) {
ep->type = 0x81;
} else if (type == TYPE_UPCASE) {
ep->type = 0x82;
} else if (type == TYPE_VOLUME) {
ep->type = 0x83;
} else if (type == TYPE_DIR) {
ep->type = 0x85;
ep->attr = cpu_to_le16(ATTR_SUBDIR);
} else if (type == TYPE_FILE) {
ep->type = 0x85;
ep->attr = cpu_to_le16(ATTR_ARCHIVE);
} else if (type == TYPE_SYMLINK) {
ep->type = 0x85;
ep->attr = cpu_to_le16(ATTR_ARCHIVE | ATTR_SYMLINK);
}
} /* end of exfat_set_entry_type */
static u32 exfat_get_entry_attr(DENTRY_T *p_entry)
{
FILE_DENTRY_T *ep = (FILE_DENTRY_T *)p_entry;
return (u32)le16_to_cpu(ep->attr);
} /* end of exfat_get_entry_attr */
static void exfat_set_entry_attr(DENTRY_T *p_entry, u32 attr)
{
FILE_DENTRY_T *ep = (FILE_DENTRY_T *)p_entry;
ep->attr = cpu_to_le16((u16) attr);
} /* end of exfat_set_entry_attr */
static u8 exfat_get_entry_flag(DENTRY_T *p_entry)
{
STRM_DENTRY_T *ep = (STRM_DENTRY_T *)p_entry;
return ep->flags;
} /* end of exfat_get_entry_flag */
static void exfat_set_entry_flag(DENTRY_T *p_entry, u8 flags)
{
STRM_DENTRY_T *ep = (STRM_DENTRY_T *)p_entry;
ep->flags = flags;
} /* end of exfat_set_entry_flag */
static u32 exfat_get_entry_clu0(DENTRY_T *p_entry)
{
STRM_DENTRY_T *ep = (STRM_DENTRY_T *)p_entry;
return (u32)le32_to_cpu(ep->start_clu);
} /* end of exfat_get_entry_clu0 */
static void exfat_set_entry_clu0(DENTRY_T *p_entry, u32 start_clu)
{
STRM_DENTRY_T *ep = (STRM_DENTRY_T *)p_entry;
ep->start_clu = cpu_to_le32(start_clu);
} /* end of exfat_set_entry_clu0 */
static u64 exfat_get_entry_size(DENTRY_T *p_entry)
{
STRM_DENTRY_T *ep = (STRM_DENTRY_T *)p_entry;
return le64_to_cpu(ep->valid_size);
} /* end of exfat_get_entry_size */
static void exfat_set_entry_size(DENTRY_T *p_entry, u64 size)
{
STRM_DENTRY_T *ep = (STRM_DENTRY_T *)p_entry;
ep->valid_size = cpu_to_le64(size);
ep->size = cpu_to_le64(size);
} /* end of exfat_set_entry_size */
#define TENS_MS_PER_SEC (100)
#define SEC_TO_TENS_MS(sec) (((sec) & 0x01) ? TENS_MS_PER_SEC : 0)
#define TENS_MS_TO_SEC(tens_ms) (((tens_ms) / TENS_MS_PER_SEC) ? 1 : 0)
static void exfat_get_entry_time(DENTRY_T *p_entry, TIMESTAMP_T *tp, u8 mode)
{
u16 t = 0x00, d = 0x21, tz = 0x00, s = 0x00;
FILE_DENTRY_T *ep = (FILE_DENTRY_T *)p_entry;
switch (mode) {
case TM_CREATE:
t = le16_to_cpu(ep->create_time);
d = le16_to_cpu(ep->create_date);
s = TENS_MS_TO_SEC(ep->create_time_ms);
tz = ep->create_tz;
break;
case TM_MODIFY:
t = le16_to_cpu(ep->modify_time);
d = le16_to_cpu(ep->modify_date);
s = TENS_MS_TO_SEC(ep->modify_time_ms);
tz = ep->modify_tz;
break;
case TM_ACCESS:
t = le16_to_cpu(ep->access_time);
d = le16_to_cpu(ep->access_date);
tz = ep->access_tz;
break;
}
tp->tz.value = tz;
tp->sec = ((t & 0x001F) << 1) + s;
tp->min = (t >> 5) & 0x003F;
tp->hour = (t >> 11);
tp->day = (d & 0x001F);
tp->mon = (d >> 5) & 0x000F;
tp->year = (d >> 9);
} /* end of exfat_get_entry_time */
static void exfat_set_entry_time(DENTRY_T *p_entry, TIMESTAMP_T *tp, u8 mode)
{
u16 t, d;
FILE_DENTRY_T *ep = (FILE_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_time_ms = SEC_TO_TENS_MS(tp->sec);
ep->create_date = cpu_to_le16(d);
ep->create_tz = tp->tz.value;
break;
case TM_MODIFY:
ep->modify_time = cpu_to_le16(t);
ep->modify_date = cpu_to_le16(d);
ep->modify_time_ms = (tp->sec & 0x1) ? TENS_MS_PER_SEC : 0;
ep->modify_tz = tp->tz.value;
break;
case TM_ACCESS:
ep->access_time = cpu_to_le16(t);
ep->access_date = cpu_to_le16(d);
ep->access_tz = tp->tz.value;
break;
}
} /* end of exfat_set_entry_time */
static void __init_file_entry(struct super_block *sb, FILE_DENTRY_T *ep, u32 type)
{
TIMESTAMP_T tm, *tp;
exfat_set_entry_type((DENTRY_T *) ep, type);
tp = tm_now_sb(sb, &tm);
exfat_set_entry_time((DENTRY_T *) ep, tp, TM_CREATE);
exfat_set_entry_time((DENTRY_T *) ep, tp, TM_MODIFY);
exfat_set_entry_time((DENTRY_T *) ep, tp, TM_ACCESS);
} /* end of __init_file_entry */
static void __init_strm_entry(STRM_DENTRY_T *ep, u8 flags, u32 start_clu, u64 size)
{
exfat_set_entry_type((DENTRY_T *) ep, TYPE_STREAM);
ep->flags = flags;
ep->start_clu = cpu_to_le32(start_clu);
ep->valid_size = cpu_to_le64(size);
ep->size = cpu_to_le64(size);
} /* end of __init_strm_entry */
static void __init_name_entry(NAME_DENTRY_T *ep, u16 *uniname)
{
s32 i;
exfat_set_entry_type((DENTRY_T *) ep, TYPE_EXTEND);
ep->flags = 0x0;
for (i = 0; i < 15; i++) {
ep->unicode_0_14[i] = cpu_to_le16(*uniname);
if (*uniname == 0x0)
break;
uniname++;
}
} /* end of __init_name_entry */
static s32 exfat_init_dir_entry(struct super_block *sb, CHAIN_T *p_dir, s32 entry, u32 type, u32 start_clu, u64 size)
{
u64 sector;
u8 flags;
FILE_DENTRY_T *file_ep;
STRM_DENTRY_T *strm_ep;
flags = (type == TYPE_FILE) ? 0x01 : 0x03;
/* we cannot use get_dentry_set_in_dir here because file ep is not initialized yet */
file_ep = (FILE_DENTRY_T *)get_dentry_in_dir(sb, p_dir, entry, §or);
if (!file_ep)
return -EIO;
strm_ep = (STRM_DENTRY_T *)get_dentry_in_dir(sb, p_dir, entry+1, §or);
if (!strm_ep)
return -EIO;
__init_file_entry(sb, file_ep, type);
if (dcache_modify(sb, sector))
return -EIO;
__init_strm_entry(strm_ep, flags, start_clu, size);
if (dcache_modify(sb, sector))
return -EIO;
return 0;
} /* end of exfat_init_dir_entry */
s32 update_dir_chksum(struct super_block *sb, CHAIN_T *p_dir, s32 entry)
{
s32 ret = -EIO;
s32 i, num_entries;
u64 sector;
u16 chksum;
FILE_DENTRY_T *file_ep;
DENTRY_T *ep;
file_ep = (FILE_DENTRY_T *)get_dentry_in_dir(sb, p_dir, entry, §or);
if (!file_ep)
return -EIO;
dcache_lock(sb, sector);
num_entries = (s32) file_ep->num_ext + 1;
chksum = calc_chksum_2byte((void *) file_ep, DENTRY_SIZE, 0, CS_DIR_ENTRY);
for (i = 1; i < num_entries; i++) {
ep = get_dentry_in_dir(sb, p_dir, entry+i, NULL);
if (!ep)
goto out_unlock;
chksum = calc_chksum_2byte((void *) ep, DENTRY_SIZE, chksum, CS_DEFAULT);
}
file_ep->checksum = cpu_to_le16(chksum);
ret = dcache_modify(sb, sector);
out_unlock:
dcache_unlock(sb, sector);
return ret;
} /* end of update_dir_chksum */
static s32 exfat_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;
u16 *uniname = p_uniname->name;
FILE_DENTRY_T *file_ep;
STRM_DENTRY_T *strm_ep;
NAME_DENTRY_T *name_ep;
file_ep = (FILE_DENTRY_T *)get_dentry_in_dir(sb, p_dir, entry, §or);
if (!file_ep)
return -EIO;
file_ep->num_ext = (u8)(num_entries - 1);
dcache_modify(sb, sector);
strm_ep = (STRM_DENTRY_T *)get_dentry_in_dir(sb, p_dir, entry+1, §or);
if (!strm_ep)
return -EIO;
strm_ep->name_len = p_uniname->name_len;
strm_ep->name_hash = cpu_to_le16(p_uniname->name_hash);
dcache_modify(sb, sector);
for (i = 2; i < num_entries; i++) {
name_ep = (NAME_DENTRY_T *)get_dentry_in_dir(sb, p_dir, entry+i, §or);
if (!name_ep)
return -EIO;
__init_name_entry(name_ep, uniname);
dcache_modify(sb, sector);
uniname += 15;
}
update_dir_chksum(sb, p_dir, entry);
return 0;
} /* end of exfat_init_ext_entry */
static s32 exfat_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 = order; i < num_entries; i++) {
ep = get_dentry_in_dir(sb, p_dir, entry+i, §or);
if (!ep)
return -EIO;
exfat_set_entry_type(ep, TYPE_DELETED);
if (dcache_modify(sb, sector))
return -EIO;
}
return 0;
}
static s32 __write_partial_entries_in_entry_set(struct super_block *sb,
ENTRY_SET_CACHE_T *es, u64 sec, u32 off, u32 count)
{
s32 num_entries;
u32 buf_off = (off - es->offset);
u32 remaining_byte_in_sector, copy_entries;
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
u32 clu;
u8 *buf, *esbuf = (u8 *)&(es->__buf);
TMSG("%s entered\n", __func__);
MMSG("%s: es %p sec %llu off %u cnt %d\n", __func__, es, sec, off, count);
num_entries = count;
while (num_entries) {
/* write per sector base */
remaining_byte_in_sector = (1 << sb->s_blocksize_bits) - off;
copy_entries = min((s32)(remaining_byte_in_sector >> DENTRY_SIZE_BITS), num_entries);
buf = dcache_getblk(sb, sec);
if (!buf)
goto err_out;
MMSG("es->buf %p buf_off %u\n", esbuf, buf_off);
MMSG("copying %d entries from %p to sector %llu\n", copy_entries, (esbuf + buf_off), sec);
memcpy(buf + off, esbuf + buf_off, copy_entries << DENTRY_SIZE_BITS);
dcache_modify(sb, sec);
num_entries -= copy_entries;
if (num_entries) {
// get next sector
if (IS_LAST_SECT_IN_CLUS(fsi, sec)) {
clu = SECT_TO_CLUS(fsi, sec);
if (es->alloc_flag == 0x03)
clu++;
else if (get_next_clus_safe(sb, &clu))
goto err_out;
sec = CLUS_TO_SECT(fsi, clu);
} else {
sec++;
}
off = 0;
buf_off += copy_entries << DENTRY_SIZE_BITS;
}
}
TMSG("%s exited successfully\n", __func__);
return 0;
err_out:
TMSG("%s failed\n", __func__);
return -EIO;
}
/* write back all entries in entry set */
static s32 __write_whole_entry_set(struct super_block *sb, ENTRY_SET_CACHE_T *es)
{
return __write_partial_entries_in_entry_set(sb, es, es->sector, es->offset, es->num_entries);
}
s32 update_dir_chksum_with_entry_set(struct super_block *sb, ENTRY_SET_CACHE_T *es)
{
DENTRY_T *ep;
u16 chksum = 0;
s32 chksum_type = CS_DIR_ENTRY, i;
ep = (DENTRY_T *)&(es->__buf);
for (i = 0; i < es->num_entries; i++) {
MMSG("%s %p\n", __func__, ep);
chksum = calc_chksum_2byte((void *) ep, DENTRY_SIZE, chksum, chksum_type);
ep++;
chksum_type = CS_DEFAULT;
}
ep = (DENTRY_T *)&(es->__buf);
((FILE_DENTRY_T *)ep)->checksum = cpu_to_le16(chksum);
return __write_whole_entry_set(sb, es);
}
/* returns a set of dentries for a file or dir.
* Note that this is a copy (dump) of dentries so that user should call write_entry_set()
* to apply changes made in this entry set to the real device.
* in:
* sb+p_dir+entry: indicates a file/dir
* type: specifies how many dentries should be included.
* out:
* file_ep: will point the first dentry(= file dentry) on success
* return:
* pointer of entry set on success,
* NULL on failure.
*/
#define ES_MODE_STARTED 0
#define ES_MODE_GET_FILE_ENTRY 1
#define ES_MODE_GET_STRM_ENTRY 2
#define ES_MODE_GET_NAME_ENTRY 3
#define ES_MODE_GET_CRITICAL_SEC_ENTRY 4
ENTRY_SET_CACHE_T *get_dentry_set_in_dir(struct super_block *sb,
CHAIN_T *p_dir, s32 entry, u32 type, DENTRY_T **file_ep)
{
s32 ret;
u32 off, byte_offset, clu = 0;
u32 entry_type;
u64 sec;
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
ENTRY_SET_CACHE_T *es = NULL;
DENTRY_T *ep, *pos;
u8 *buf;
u8 num_entries;
s32 mode = ES_MODE_STARTED;
/* FIXME : is available in error case? */
if (p_dir->dir == DIR_DELETED) {
EMSG("%s : access to deleted dentry\n", __func__);
BUG_ON(!fsi->prev_eio);
return NULL;
}
TMSG("%s entered\n", __func__);
MMSG("p_dir dir %u flags %x size %d\n", p_dir->dir, p_dir->flags, p_dir->size);
MMSG("entry %d type %d\n", entry, type);
byte_offset = entry << DENTRY_SIZE_BITS;
ret = walk_fat_chain(sb, p_dir, byte_offset, &clu);
if (ret)
return NULL;
/* byte offset in cluster */
byte_offset &= fsi->cluster_size - 1;
/* byte offset in sector */
off = byte_offset & (u32)(sb->s_blocksize - 1);
/* sector offset in cluster */
sec = byte_offset >> (sb->s_blocksize_bits);
sec += CLUS_TO_SECT(fsi, clu);
buf = dcache_getblk(sb, sec);
if (!buf)
goto err_out;
ep = (DENTRY_T *)(buf + off);
entry_type = exfat_get_entry_type(ep);
if ((entry_type != TYPE_FILE)
&& (entry_type != TYPE_DIR))
goto err_out;
if (type == ES_ALL_ENTRIES)
num_entries = ((FILE_DENTRY_T *)ep)->num_ext+1;
else
num_entries = type;
MMSG("trying to malloc %lx bytes for %d entries\n",
(unsigned long)(offsetof(ENTRY_SET_CACHE_T, __buf) + (num_entries) * sizeof(DENTRY_T)), num_entries);
es = kmalloc((offsetof(ENTRY_SET_CACHE_T, __buf) + (num_entries) * sizeof(DENTRY_T)), GFP_KERNEL);
if (!es) {
EMSG("%s: failed to alloc entryset\n", __func__);
goto err_out;
}
es->num_entries = num_entries;
es->sector = sec;
es->offset = off;
es->alloc_flag = p_dir->flags;
pos = (DENTRY_T *) &(es->__buf);
while (num_entries) {
// instead of copying whole sector, we will check every entry.
// this will provide minimum stablity and consistency.
entry_type = exfat_get_entry_type(ep);
if ((entry_type == TYPE_UNUSED) || (entry_type == TYPE_DELETED))
goto err_out;
switch (mode) {
case ES_MODE_STARTED:
if ((entry_type == TYPE_FILE) || (entry_type == TYPE_DIR))
mode = ES_MODE_GET_FILE_ENTRY;
else
goto err_out;
break;
case ES_MODE_GET_FILE_ENTRY:
if (entry_type == TYPE_STREAM)
mode = ES_MODE_GET_STRM_ENTRY;
else
goto err_out;
break;
case ES_MODE_GET_STRM_ENTRY:
if (entry_type == TYPE_EXTEND)
mode = ES_MODE_GET_NAME_ENTRY;
else
goto err_out;
break;
case ES_MODE_GET_NAME_ENTRY:
if (entry_type == TYPE_EXTEND)
break;
else if (entry_type == TYPE_STREAM)
goto err_out;
else if (entry_type & TYPE_CRITICAL_SEC)
mode = ES_MODE_GET_CRITICAL_SEC_ENTRY;
else
goto err_out;
break;
case ES_MODE_GET_CRITICAL_SEC_ENTRY:
if ((entry_type == TYPE_EXTEND) || (entry_type == TYPE_STREAM))
goto err_out;
else if ((entry_type & TYPE_CRITICAL_SEC) != TYPE_CRITICAL_SEC)
goto err_out;
break;
}
/* copy dentry */
memcpy(pos, ep, sizeof(DENTRY_T));
if (--num_entries == 0)
break;
if (((off + DENTRY_SIZE) & (u32)(sb->s_blocksize - 1)) <
(off & (u32)(sb->s_blocksize - 1))) {
// get the next sector
if (IS_LAST_SECT_IN_CLUS(fsi, sec)) {
if (es->alloc_flag == 0x03)
clu++;
else if (get_next_clus_safe(sb, &clu))
goto err_out;
sec = CLUS_TO_SECT(fsi, clu);
} else {
sec++;
}
buf = dcache_getblk(sb, sec);
if (!buf)
goto err_out;
off = 0;
ep = (DENTRY_T *)(buf);
} else {
ep++;
off += DENTRY_SIZE;
}
pos++;
}
if (file_ep)
*file_ep = (DENTRY_T *)&(es->__buf);
MMSG("es sec %llu offset %u flags %d, num_entries %u buf ptr %p\n",
es->sector, es->offset, es->alloc_flag, es->num_entries, &(es->__buf));
TMSG("%s exited %p\n", __func__, es);
return es;
err_out:
TMSG("%s exited (return NULL) (es %p)\n", __func__, es);
/* kfree(NULL) is safe */
kfree(es);
es = NULL;
return NULL;
}
void release_dentry_set(ENTRY_SET_CACHE_T *es)
{
TMSG("%s %p\n", __func__, es);
/* kfree(NULL) is safe */
kfree(es);
es = NULL;
}
static s32 __extract_uni_name_from_name_entry(NAME_DENTRY_T *ep, u16 *uniname, s32 order)
{
s32 i, len = 0;
for (i = 0; i < 15; i++) {
/* FIXME : unaligned? */
*uniname = le16_to_cpu(ep->unicode_0_14[i]);
if (*uniname == 0x0)
return len;
uniname++;
len++;
}
*uniname = 0x0;
return len;
} /* end of __extract_uni_name_from_name_entry */
#define DIRENT_STEP_FILE (0)
#define DIRENT_STEP_STRM (1)
#define DIRENT_STEP_NAME (2)
#define DIRENT_STEP_SECD (3)
/* return values of exfat_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 s32 exfat_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 *unused, u32 type)
{
s32 i, rewind = 0, dentry = 0, end_eidx = 0, num_ext = 0, len;
s32 order, step, name_len;
s32 dentries_per_clu, num_empty = 0;
u32 entry_type;
u16 entry_uniname[16], *uniname = NULL, unichar;
CHAIN_T clu;
DENTRY_T *ep;
HINT_T *hint_stat = &fid->hint_stat;
HINT_FEMP_T candi_empty;
FILE_DENTRY_T *file_ep;
STRM_DENTRY_T *strm_ep;
NAME_DENTRY_T *name_ep;
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
/*
* REMARK:
* DOT and DOTDOT are handled by VFS layer
*/
if (IS_CLUS_FREE(p_dir->dir))
return -EIO;
dentries_per_clu = fsi->dentries_per_clu;
clu.dir = p_dir->dir;
clu.size = p_dir->size;
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;
rewind:
order = 0;
step = DIRENT_STEP_FILE;
while (!IS_CLUS_EOF(clu.dir)) {
i = dentry & (dentries_per_clu - 1);
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 = exfat_get_entry_type(ep);
if ((entry_type == TYPE_UNUSED) || (entry_type == TYPE_DELETED)) {
step = DIRENT_STEP_FILE;
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;
continue;
}
num_empty = 0;
candi_empty.eidx = -1;
if ((entry_type == TYPE_FILE) || (entry_type == TYPE_DIR)) {
step = DIRENT_STEP_FILE;
if ((type == TYPE_ALL) || (type == entry_type)) {
file_ep = (FILE_DENTRY_T *) ep;
num_ext = file_ep->num_ext;
step = DIRENT_STEP_STRM;
}
continue;
}
if (entry_type == TYPE_STREAM) {
if (step != DIRENT_STEP_STRM) {
step = DIRENT_STEP_FILE;
continue;
}
step = DIRENT_STEP_FILE;
strm_ep = (STRM_DENTRY_T *) ep;
if ((p_uniname->name_hash == le16_to_cpu(strm_ep->name_hash)) &&
(p_uniname->name_len == strm_ep->name_len)) {
step = DIRENT_STEP_NAME;
order = 1;
name_len = 0;
}
continue;
}
if (entry_type == TYPE_EXTEND) {
if (step != DIRENT_STEP_NAME) {
step = DIRENT_STEP_FILE;
continue;
}
name_ep = (NAME_DENTRY_T *) ep;
if ((++order) == 2)
uniname = p_uniname->name;
else
uniname += 15;
len = __extract_uni_name_from_name_entry(name_ep, entry_uniname, order);
name_len += len;
unichar = *(uniname+len);
*(uniname+len) = 0x0;
if (nls_cmp_uniname(sb, uniname, entry_uniname)) {
step = DIRENT_STEP_FILE;
} else if (name_len == p_uniname->name_len) {
if (order == num_ext) {
//fid->hint_femp.eidx = -1;
goto found;
}
step = DIRENT_STEP_SECD;
}
*(uniname+len) = unichar;
continue;
}
if (entry_type & (TYPE_CRITICAL_SEC | TYPE_BENIGN_SEC)) {
if (step == DIRENT_STEP_SECD) {
if (++order == num_ext)
goto found;
continue;
}
}
step = DIRENT_STEP_FILE;
}
if (clu.flags == 0x03) {
if ((--clu.size) > 0)
clu.dir++;
else
clu.dir = CLUS_EOF;
} else {
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 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-1))) {
int ret = 0;
if (clu.flags == 0x03) {
if ((--clu.size) > 0)
clu.dir++;
else
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 - num_ext);
}
}
hint_stat->clu = clu.dir;
hint_stat->eidx = dentry + 1;
return (dentry - num_ext);
} /* end of exfat_find_dir_entry */
/* returns -EIO on error */
static s32 exfat_count_ext_entries(struct super_block *sb, CHAIN_T *p_dir, s32 entry, DENTRY_T *p_entry)
{
s32 i, count = 0;
u32 type;
FILE_DENTRY_T *file_ep = (FILE_DENTRY_T *) p_entry;
DENTRY_T *ext_ep;
for (i = 0, entry++; i < file_ep->num_ext; i++, entry++) {
ext_ep = get_dentry_in_dir(sb, p_dir, entry, NULL);
if (!ext_ep)
return -EIO;
type = exfat_get_entry_type(ext_ep);
if ((type == TYPE_EXTEND) || (type == TYPE_STREAM))
count++;
else
return count;
}
return count;
} /* end of exfat_count_ext_entries */
/*
* Name Conversion Functions
*/
static void exfat_get_uniname_from_ext_entry(struct super_block *sb, CHAIN_T *p_dir, s32 entry, u16 *uniname)
{
s32 i;
DENTRY_T *ep;
ENTRY_SET_CACHE_T *es;
es = get_dentry_set_in_dir(sb, p_dir, entry, ES_ALL_ENTRIES, &ep);
if (!es)
return;
if (es->num_entries < 3)
goto out;
ep += 2;
/*
* First entry : file entry