forked from lorabasics/basicstation
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfs.c
More file actions
1425 lines (1303 loc) · 43 KB
/
fs.c
File metadata and controls
1425 lines (1303 loc) · 43 KB
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
/*
* --- Revised 3-Clause BSD License ---
* Copyright Semtech Corporation 2022. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of the Semtech corporation nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL SEMTECH CORPORATION. BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <fcntl.h>
#if defined(CFG_linux) || defined(CFG_flashsim)
#include <sys/stat.h>
#include <errno.h>
#include <stdarg.h>
#include <unistd.h>
#endif // defined(CFG_linux)
#include "s2conf.h"
#include "rt.h"
#include "kwcrc.h"
#include "uj.h"
#include "fs.h"
// Flash is organized in 32bit words
// Whole/part is split up into two sections.
// If one section is full, a GC collection copies over
// only live data to the other section.
// A section consists of an magic + GC seq counter
// Each GC increments the sequence counter.
// Given two section magics is is clear which one is older.
//
// After the section magic follow records:
//
// [begtag] ... [endtag]
//
// They can be travered forward and backward.
// begtag/endtag both contain a length.
// begtag carries an ino number.
// ino numbers are increasing and are reset/relabeled during GC.
// endtag has a CRC.
//
// [begtag] [fncrc] [ctim] [filename '\0'{1,4}] [endtag] FILE/DELETE
// [begtag] [fncrc] [fncrc2] [filename1 '\0' filename2 '\0'{1,4}] [endtag] RENAME
// [begtag] [data0] [data1] ... [dataN '\0'{0,3}] [endtag] DATA
//
// For DATA records the number pad bytes is indicated in endtag.
// pad(begtag) is always 0 for all records.
// pad(endtag) is zero for FILE/DELETE/RENAME.
//
// End of GC is marked with a FILE record and a filename word 002f2f00 and
// fncrc=0 ctime=0.
//
// Make our file handles different from system ones (just safety)
#define OFF_FD 0x10000
#define MAX_INO 0x3FFF
#define CRC_INI 0x1234
#define FLASH_MAGIC 0xA4B5
#define FLASH_BEG_A (FLASH_ADDR + FLASH_PAGE_SIZE * FS_PAGE_START)
#define FLASH_BEG_B (FLASH_BEG_A + FLASH_PAGE_SIZE * (FS_PAGE_CNT/2))
#define FLASH_END_A (FLASH_BEG_B)
#define FLASH_END_B (FLASH_BEG_B + FLASH_PAGE_SIZE * (FS_PAGE_CNT/2))
static inline u1_t FSTAG_cmd(u4_t v) { return (v >> 30) & 3; }
static inline u2_t FSTAG_ino(u4_t v) { return ((v >> 16) & MAX_INO); }
static inline u2_t FSTAG_crc(u4_t v) { return ((v >> 16) & 0xFFFF); }
static inline u2_t FSTAG_len(u4_t v) { return v & 0xFFFC; }
static inline u1_t FSTAG_pad(u4_t v) { return v & 3; }
static inline u4_t FSTAG_mkBeg(u1_t cmd, u2_t ino, u2_t len, u1_t pad) {
return (cmd<<30) | ((ino&MAX_INO)<<16) | (len&0xFFFC) | (pad&3);
}
static inline u4_t FSTAG_mkEnd(u2_t crc, u2_t len, u1_t pad) {
return ((crc&0xFFFF)<<16) | (len&0xFFFC)|(pad&3);
}
#define FSCMD_FILE 0
#define FSCMD_DATA 1
#define FSCMD_RENAME 2
#define FSCMD_DELETE 3
typedef struct fctx {
u4_t faddr;
u4_t begtag;
u4_t endtag;
} fctx_t;
typedef struct fh {
u2_t ino;
u2_t droff; // offset inside data record
u4_t faddr;
u4_t foff; // file read offset
} fh_t;
struct ino_cache {
u4_t faddrFile; // creating FILE record
u4_t faddrRename; // last rename
u4_t fncrc;
};
#define AUXBUF_SZW (2*((FS_MAX_FNSIZE+3)/4))
#define AUXBUF_SZ4 (4*AUXBUF_SZW)
static union auxbuf {
u4_t u4[AUXBUF_SZW];
u1_t u1[AUXBUF_SZ4];
} auxbuf;
static fctx_t fctxCache;
static u4_t flashKey[4];
static u4_t flashWP;
static u2_t nextIno;
static s1_t fsSection = -1; // 0|1, -1 no fs_ini called yet
static const char DEFAULT_CWD[] = "/s2/";
static str_t cwd = DEFAULT_CWD;
static fh_t fhTable[FS_MAX_FD];
static inline u4_t flashFsBeg() {
return fsSection ? FLASH_BEG_B+4 : FLASH_BEG_A+4;
}
static inline u4_t flashFsMax() {
return fsSection ? FLASH_END_B : FLASH_END_A;
}
static u4_t encrypt1 (u4_t faddr, u4_t data) {
return data ^ flashKey[(faddr>>2) & 3];
}
static u4_t decrypt1 (u4_t faddr, u4_t data) {
return encrypt1(faddr, data);
}
static void encryptN (u4_t faddr, u4_t* data, uint u4cnt) {
for( uint u=0; u<u4cnt; u++ ) {
data[u] = encrypt1(faddr+u*4, data[u]);
}
}
static void decryptN (u4_t faddr, u4_t* data, uint u4cnt) {
for( uint u=0; u<u4cnt; u++ ) {
data[u] = decrypt1(faddr+u*4, data[u]);
}
}
void wrFlash1 (u4_t faddr, u4_t data) {
assert(faddr < (faddr >= FLASH_BEG_B ? FLASH_END_B : FLASH_END_A));
data = encrypt1(faddr, data);
sys_writeFlash(faddr, &data, 1);
}
static void wrFlash1wp (u4_t data) {
u4_t faddr = flashWP;
wrFlash1(faddr, data);
flashWP = faddr + 4;
}
u4_t rdFlash1 (u4_t faddr) {
u4_t data;
assert(faddr < flashFsMax());
sys_readFlash(faddr, &data, 1);
return decrypt1(faddr, data);
}
void wrFlashN (u4_t faddr, u4_t* daddr, uint u4cnt, int keepData) {
assert(faddr + u4cnt*4 <= (faddr >= FLASH_BEG_B ? FLASH_END_B : FLASH_END_A));
encryptN(faddr, daddr, u4cnt);
sys_writeFlash(faddr, daddr, u4cnt);
if( keepData )
decryptN(faddr, daddr, u4cnt);
}
static void wrFlashNwp (u4_t* daddr, uint u4cnt, int keepData) {
u4_t faddr = flashWP;
wrFlashN (faddr, daddr, u4cnt, keepData);
flashWP = faddr + u4cnt*4;
}
void rdFlashN(u4_t faddr, u4_t* daddr, uint u4cnt) {
assert(faddr + u4cnt*4 <= flashFsMax());
sys_readFlash(faddr, daddr, u4cnt);
decryptN(faddr, daddr, u4cnt);
}
static u4_t fctx_begtag (fctx_t* fctx) {
u4_t begtag = fctx->begtag;
if( begtag == 0 )
begtag = fctx->begtag = rdFlash1(fctx->faddr);
return begtag;
}
static u4_t fctx_endtag (fctx_t* fctx) {
u4_t endtag = fctx->endtag;
if( endtag == 0 ) {
u4_t begtag = fctx_begtag(fctx);
u4_t faddr = fctx->faddr + 4 + FSTAG_len(begtag);
endtag = fctx->endtag = rdFlash1(faddr);
}
return endtag;
}
static u2_t dataCrc (u2_t crc, const u1_t* data, uint len) {
u1_t a=crc>>8, b=crc&0xFF;
for( int i=0; i<len; i++ ) {
b += (a += data[i]);
}
while( -len & 3 ) {
b += a;
len++;
}
return (a<<8)|b;
}
static u4_t fnCrc (const char* fn) {
u4_t crc = 0;
while( *fn++ )
crc = UJ_UPDATE_CRC(crc,*fn);
return UJ_FINISH_CRC(crc);
}
static int isFlashFull (u4_t reqbytes) {
int emergency = 0;
reqbytes = (reqbytes + 3) & ~3;
while( flashWP + reqbytes > flashFsMax() || nextIno >= MAX_INO-2 ) {
if( emergency == 2 ) {
// No space even after an emergency clean up
errno = ENOSPC;
return -1;
}
fs_gc(emergency);
emergency++;
}
return 0;
}
// Return len filename + zero byte
int fs_fnNormalize (const char* fn, char* wb, int maxsz) {
int ri = 0, wi = 0;
wb[0] = 0;
if( maxsz <= 2 ) {
errno = ENAMETOOLONG;
return 0;
}
if( fn[0] != '/' ) {
wi = strlen(cwd);
if( wi+2 >= maxsz ) {
errno = ENAMETOOLONG;
return 0;
}
strcpy(wb, cwd);
} else {
ri = wi = 1;
wb[0] = '/';
}
int c;
while( 1 ) {
// Start of path syllable - previous char is /
c = fn[ri];
if( c=='/' ) {
ri++; // ignore double slash
continue;
}
if( c=='.' && (fn[ri+1] == '/' || fn[ri+1] == 0) ) {
ri += 2 - !fn[ri+1]; // ignore ./ or .\0
continue;
}
if( c=='.' && fn[ri+1] == '.' && (fn[ri+2] == '/' || fn[ri+2] == 0) ) {
ri += 3 - !fn[ri+2]; // skip ../ and move back one syllable
if( wi == 1 )
continue; // root slash
do {
wi -= 1;
} while( wb[wi-1] != '/');
continue;
}
if( c == 0 ) {
if( wi > 1 )
wi -= 1; // remove trailing /
wb[wi] = 0;
return wi+1;
}
while( 1 ) {
c = fn[ri];
if( c==0 ) {
wb[wi] = 0;
return wi+1;
}
wb[wi++] = c;
if( wi+2 >= maxsz ) {
wb[wi] = 0;
errno = ENAMETOOLONG;
return 0;
}
ri++;
if( c=='/' )
break;
}
}
}
static void fctx_setTo (fctx_t* fctx, u4_t faddr) {
memset(fctx, 0, sizeof(*fctx));
fctx->faddr = faddr;
}
static int checkFilename (const char* fn) {
if( fn == NULL ) {
errno = EFAULT;
return 0;
}
char* wb = (char*)&auxbuf.u4[3];
int fnlen = auxbuf.u4[0] = fs_fnNormalize(fn, wb, FS_MAX_FNSIZE);
#if defined(CFG_linux)
if( strncmp(wb, "/s2/", 3) != 0 || (wb[3] != 0 && wb[3] != '/') ) {
// branch out into linux FS
return -1;
}
#endif // defined(CFG_linux)
return fnlen;
}
static int fs_findFile (fctx_t* fctx, const char* fn) {
int fnlen;
if( fn != NULL ) {
fnlen = checkFilename(fn);
if( fnlen == 0 )
return -1;
} else {
// Caller already did checkFilename!
fnlen = auxbuf.u4[0];
}
char* wb = (char*)&auxbuf.u1[12];
u4_t seekcrc = auxbuf.u4[1] = fnCrc(wb);
u4_t faddr = flashWP; // end of last record
while( faddr > flashFsBeg() ) {
u4_t endtag = rdFlash1(faddr-4);
u4_t len = FSTAG_len(endtag);
faddr -= len+8;
u4_t begtag = rdFlash1(faddr);
u1_t cmd = FSTAG_cmd(begtag);
if( cmd == FSCMD_DATA )
continue;
u4_t fncrc = rdFlash1(faddr+4);
if( seekcrc == fncrc ) {
if( cmd == FSCMD_RENAME || cmd == FSCMD_DELETE )
break;
assert(cmd == FSCMD_FILE);
fctx_setTo(fctx, faddr);
fctx->begtag = begtag;
fctx->endtag = endtag;
return 0;
}
if( cmd == FSCMD_RENAME && seekcrc == rdFlash1(faddr+8) )
seekcrc = fncrc;
}
errno = ENOENT;
return -1;
}
static int fs_handleFile (const char* fn, const char* fn2, u1_t cmd, u2_t ino) {
char* wb = (char*)&auxbuf.u1[12];
int fnlen;
if( fn == NULL ) {
// Some previous operation already put normalized filename into auxbuf
fnlen = auxbuf.u4[0];
} else {
fnlen = fs_fnNormalize(fn, wb, FS_MAX_FNSIZE);
if( fnlen == 0 )
return -1;
}
auxbuf.u4[1] = fnCrc(wb);
if( fn2 != NULL ) {
int fnlen2 = fs_fnNormalize(fn2, wb+fnlen, FS_MAX_FNSIZE);
if( fnlen2 == 0 )
return -1;
auxbuf.u4[2] = fnCrc(wb+fnlen);
fnlen += fnlen2;
} else {
auxbuf.u4[2] = rt_getUTC()/rt_seconds(1);
}
while( (fnlen & 3) != 0 )
wb[fnlen++] = 0;
fnlen += 8;
auxbuf.u4[0] = FSTAG_mkBeg(cmd, ino, fnlen, 0);
u4_t dlen4 = fnlen/4+2;
auxbuf.u4[dlen4-1] = FSTAG_mkEnd(dataCrc(CRC_INI, &auxbuf.u1[4], fnlen), fnlen, 0);
wrFlashNwp(auxbuf.u4, dlen4, 1);
return 0;
}
static int fs_createFile (fh_t* fh, const char* fn) {
u4_t faddr = flashWP;
if( fs_handleFile(fn, NULL, FSCMD_FILE, nextIno++) == -1 )
return -1;
u4_t begtag = auxbuf.u4[0];
fh->faddr = faddr;
fh->ino = FSTAG_ino(begtag);
fh->droff = FSTAG_len(begtag); // full - read moves on to next
fh->foff = 0;
return 0;
}
static fh_t* fd2fh (int fd) {
if( fd < OFF_FD || fd >= OFF_FD+FS_MAX_FD ) {
errno = EINVAL;
return NULL;
}
if( fhTable[fd-OFF_FD].ino == 0 || fhTable[fd-OFF_FD].ino > MAX_INO ) {
errno = EBADF;
return NULL;
}
return &fhTable[fd-OFF_FD];
}
static int fs_findNextDataRecord (fctx_t* fctx, u2_t ino) {
u4_t faddr = fctx->faddr;
if( faddr >= flashWP )
return 0;
u4_t begtag = fctx_begtag(fctx);
if( ino == 0 )
ino = FSTAG_ino(begtag);
while(1) {
faddr += FSTAG_len(begtag) + 8;
if( faddr >= flashWP )
return 0;
begtag = rdFlash1(faddr);
if( FSTAG_ino(begtag) == ino && FSTAG_cmd(begtag) == FSCMD_DATA )
break;
}
fctx_setTo(fctx, faddr);
fctx->begtag = begtag;
return 1;
}
int fs_read (int fd, void* dp, int dlen) {
u1_t* data = (u1_t*)dp;
fh_t* fh = fd2fh(fd);
if( fh == NULL ) {
#if defined(CFG_linux)
if( errno == EINVAL ) {
return read(fd, dp, dlen);
}
#endif
return -1;
}
if( dlen == 0 )
return 0;
if( fh->faddr == 0 ) { // opened for writing?
errno = EBADF;
return -1;
}
fctx_t* fctx = &fctxCache;
fctx_setTo(fctx, fh->faddr);
int rlen = 0;
int droff = fh->droff;
while(1) {
int begtag = fctx_begtag(fctx);
int drend = FSTAG_len(begtag) - FSTAG_pad(fctx_endtag(fctx));
while( droff < drend ) {
u4_t cpylen = drend-droff;
if( cpylen > dlen )
cpylen = dlen;
u4_t fb = fctx->faddr + 4 + droff;
u4_t fb4 = fb & ~3;
u4_t fl4 = ((fb+cpylen+3) & ~3) - fb4;
if( fl4 > AUXBUF_SZ4 ) {
fl4 = AUXBUF_SZ4;
cpylen = AUXBUF_SZ4 - (fb-fb4);
}
rdFlashN(fb4, auxbuf.u4, fl4/4);
memcpy(data, &auxbuf.u1[fb-fb4], cpylen);
droff += cpylen;
rlen += cpylen;
dlen -= cpylen;
if( dlen == 0 ) {
goto done;
}
data += cpylen;
}
if( !fs_findNextDataRecord(fctx, 0) ) {
// Keep data record - droff indicates no more
// data in this one. Next read wil check again if
// data blocks have been appended
goto done;
}
droff = 0;
}
done:
fh->faddr = fctx->faddr;
fh->droff = droff;
fh->foff += rlen;
return rlen;
}
int fs_write (int fd, const void* dp, int dlen) {
const u1_t* data = (const u1_t*)dp;
fh_t* fh = fd2fh(fd);
if( fh == NULL ) {
#if defined(CFG_linux)
if( errno == EINVAL ) {
return write(fd, dp, dlen);
}
#endif
return -1;
}
if( fh->faddr != 0 ) { // opened for reading?
errno = EBADF;
return -1;
}
if( dlen == 0 )
return 0;
if( isFlashFull(dlen+8) == -1 )
return -1;
auxbuf.u4[0] = 0;
u2_t dlenCeil = (dlen+3) & ~3;
//u2_t dcrc = dataCrc(dataCrc(CRC_INI, data, dlen), auxbuf.u1, dlenCeil-dlen);
u2_t dcrc = dataCrc(CRC_INI, data, dlen);
int doff = 0;
u1_t tbeg=0, tend=0;
u1_t* tb = &auxbuf.u1[4];
int tblen = sizeof(auxbuf.u1)-8;
auxbuf.u4[0] = FSTAG_mkBeg(FSCMD_DATA, fh->ino, dlenCeil, 0);
while( !tend ) {
int cpylen = dlen-doff;
if( cpylen > tblen )
cpylen = tblen;
doff += cpylen;
int cpylen4 = (cpylen+3)/4;
if( doff == dlen ) {
auxbuf.u4[0+cpylen4] = 0; // proactively padding
auxbuf.u4[1+cpylen4] = FSTAG_mkEnd(dcrc, dlenCeil, dlenCeil-dlen);
tend = 1;
}
memcpy(tb, data+doff-cpylen, cpylen);
wrFlashNwp(&auxbuf.u4[tbeg], (1-tbeg)+cpylen4+tend, 0);
tbeg = 1;
}
return dlen;
}
int fs_chdir (str_t dir) {
// Normalize dir
str_t ndir = dir;
if( dir != NULL ) {
ndir = (str_t)auxbuf.u1;
int sz = fs_fnNormalize(dir, (char*)auxbuf.u1, FS_MAX_FNSIZE);
if( sz == 0 )
return -1;
auxbuf.u1[sz-1] = '/';
auxbuf.u1[sz] = 0;
}
if( cwd != DEFAULT_CWD )
free((void*)cwd);
if( ndir == NULL || strcmp(ndir, DEFAULT_CWD) == 0 ) {
cwd = DEFAULT_CWD;
} else {
cwd = rt_strdup(ndir);
}
return 0;
}
int fs_unlink (const char* fn) {
int fnlen = checkFilename(fn);
#if defined(CFG_linux)
if( fnlen == -1 ) {
return unlink(fn);
}
#endif // defined(CFG_linux)
if( fnlen <= 0 )
return -1;
if( fs_findFile(&fctxCache, NULL) == -1 )
return -1;
return fs_handleFile(NULL, NULL, FSCMD_DELETE, FSTAG_ino(fctx_begtag(&fctxCache)));
}
int fs_rename (const char* from, const char* to) {
int fnlen2 = checkFilename(to);
int fnlen = checkFilename(from);
if( fnlen == 0 || fnlen2 == 0 )
return -1;
#if defined(CFG_linux)
if( fnlen == -1 && fnlen2 == -1 ) {
return rename(from, to);
}
#endif // defined(CFG_linux)
if( fnlen == -1 || fnlen2 == -1 ) {
errno = EXDEV;
return -1;
}
if( isFlashFull(fnlen+fnlen2+16) == -1 )
return -1;
if( fs_findFile(&fctxCache, NULL) == -1 )
return -1;
return fs_handleFile(NULL, to, FSCMD_RENAME, FSTAG_ino(fctx_begtag(&fctxCache)));
}
int fs_access (str_t fn, int mode) {
int fnlen = checkFilename(fn);
#if defined(CFG_linux)
if( fnlen == -1 ) {
return access(fn, mode);
}
#endif // defined(CFG_linux)
if( fnlen <= 0 )
return -1;
return fs_findFile(&fctxCache, NULL);
}
int fs_open (str_t fn, int mode, ...) {
int fnlen = checkFilename(fn);
#if defined(CFG_linux)
if( fnlen == -1 ) {
va_list ap;
va_start(ap, mode);
int flags = va_arg(ap, int);
va_end(ap);
return open(fn, mode, flags);
}
#endif // defined(CFG_linux)
if( fnlen <= 0 )
return -1;
if( isFlashFull(fnlen+16) == -1 )
return -1;
fh_t* fh = NULL;
for( int i=0; i< FS_MAX_FD; i++ ) {
if( fhTable[i].ino == 0 ) {
fh = &fhTable[i];
break;
}
}
if( fh == NULL ) {
errno = ENFILE;
return -1;
}
if( mode == (O_CREAT|O_WRONLY|O_TRUNC) ) {
if( fs_createFile(fh, NULL) == -1 )
return -1;
fh->faddr = 0; // WRONLY
fh->droff = 0; // not used during write
fh->foff = 0; // not used during write
}
else if( mode == (O_CREAT|O_APPEND|O_WRONLY) ) {
fctx_t* fctx = &fctxCache;
if( fs_findFile(fctx, NULL) == -1 ) {
if( fs_createFile(fh, NULL) == -1 )
return -1;
fh->faddr = 0; // WRONLY
fh->droff = 0; // not used during write
fh->foff = 0; // not used during write
} else {
u4_t begtag = fctx_begtag(fctx);
fh->ino = FSTAG_ino(begtag);
fh->droff = 0; // not used during write
fh->foff = 0; // not used during write
fh->faddr = 0; // WRONLY
}
}
else if( mode == O_RDONLY ) {
fctx_t* fctx = &fctxCache;
if( fs_findFile(fctx, NULL) == -1 )
return -1;
u4_t begtag = fctx_begtag(fctx);
fh->ino = FSTAG_ino(begtag);
fh->droff = FSTAG_len(begtag); // full - read moves on to next
fh->foff = 0;
fh->faddr = fctx->faddr;
}
else {
errno = EINVAL;
return -1;
}
return fh - fhTable + OFF_FD;
}
int fs_close(int fd) {
fh_t* fh = fd2fh(fd);
if( fh == NULL ) {
#if defined(CFG_linux)
if( errno == EINVAL ) {
return close(fd);
}
#endif
return -1;
}
memset(fh, 0, sizeof(*fh));
return 0;
}
int fs_stat (str_t fn, struct stat* st) {
int fnlen = checkFilename(fn);
#if defined(CFG_linux)
if( fnlen == -1 ) {
return stat(fn, st);
}
#endif // defined(CFG_linux)
if( fnlen <= 0 )
return -1;
if( fs_findFile(&fctxCache, NULL) == -1 )
return -1;
u2_t ino = FSTAG_ino(fctx_begtag(&fctxCache));
u4_t ctim = rdFlash1(fctxCache.faddr+8);
uint sz = 0;
while( fs_findNextDataRecord(&fctxCache, ino) ) {
u4_t endtag = fctx_endtag(&fctxCache);
sz += FSTAG_len(endtag) - FSTAG_pad(endtag);
}
memset(st, 0, sizeof(*st));
st->st_mode = 0006;
st->st_ino = ino;
st->st_size = sz;
st->st_ctim.tv_sec = ctim;
return 0;
}
int fs_lseek (int fd, int offset, int whence) {
fh_t* fh = fd2fh(fd);
if( fh == NULL )
return -1;
if( fh->faddr == 0 ) {
// no seek on writable files - fs can do only append
errno = EINVAL;
return -1;
}
if( whence != SEEK_SET || offset < 0 ) {
// not supported right now - because not used
errno = EINVAL;
return -1;
}
u2_t ino = fh->ino;
fctx_setTo(&fctxCache, flashFsBeg());
int droff=0, foff=0;
while( fs_findNextDataRecord(&fctxCache, ino) ) {
u4_t endtag = fctx_endtag(&fctxCache);
droff = FSTAG_len(endtag) - FSTAG_pad(endtag);
foff += droff;
if( foff >= offset ) {
fh->faddr = fctxCache.faddr;
fh->droff = droff - (foff-offset);
fh->foff = offset;
return 0;
}
}
fh->faddr = fctxCache.faddr;
fh->droff = droff;
fh->foff = foff;
return 0;
}
void fs_sync () {
#if defined(CFG_linux)
sync();
#endif // defined(CFG_linux)
}
static int fs_validateRecord (fctx_t* fctx) {
u4_t begtag = fctx_begtag(fctx);
u2_t ino = FSTAG_ino(begtag);
u4_t len = FSTAG_len(begtag);
u4_t pad = FSTAG_pad(begtag);
u4_t faddr = fctx->faddr;
if( faddr + 8 + len > flashFsMax() ||
// right we don't have anything that requires initial padding
len == 0 || pad )
return -1;
u4_t endtag = fctx_endtag(fctx);
u4_t endpad = FSTAG_pad(endtag);
u4_t endlen = FSTAG_len(endtag);
u2_t dcrc = FSTAG_crc(endtag);
if( len != endlen || pad+endpad > len )
return -1;
u4_t off=0, cpycnt=0;
u2_t xcrc = CRC_INI;
while( off < len ) {
cpycnt = len - off;
if( cpycnt > AUXBUF_SZ4 )
cpycnt = AUXBUF_SZ4;
rdFlashN(faddr + off + 4, auxbuf.u4, cpycnt/4);
xcrc = dataCrc(xcrc, auxbuf.u1, cpycnt);
off += cpycnt;
}
if( xcrc != dcrc )
return -1;
fctx_setTo(fctx, faddr + len + 8);
return ino;
}
static void fs_smartErase (u4_t pgaddr, u4_t pagecnt) {
while( pagecnt > 0 ) {
u4_t off=0, len=AUXBUF_SZ4;
while( off < FLASH_PAGE_SIZE ) {
if( off + AUXBUF_SZ4 > FLASH_PAGE_SIZE )
len = FLASH_PAGE_SIZE - off;
u4_t lenw = len/4;
sys_readFlash(pgaddr+off, auxbuf.u4, lenw);
for( int wi=0; wi<lenw; wi++ ) {
if( auxbuf.u4[wi] != FLASH_ERASED ) {
sys_eraseFlash(pgaddr, 1);
goto nextpage;
}
}
off += len;
}
nextpage:
pagecnt--;
pgaddr += FLASH_PAGE_SIZE;
}
}
// return:
// 0 - pristine flash
// 1 - section recovered as is
// 2 - GC was required
//
int fs_ck () {
u4_t magic[2];
fsSection = 1;
magic[1] = rdFlash1(FLASH_BEG_B);
fsSection = 0;
magic[0] = rdFlash1(FLASH_BEG_A);
if( (magic[0] >> 16) != FLASH_MAGIC && (magic[1] >> 16) != FLASH_MAGIC ) {
// Looks pristine - never seen any transactions
fs_smartErase(FLASH_BEG_A, FS_PAGE_CNT);
fsSection = 0;
flashWP = flashFsBeg()-4;
wrFlash1wp(FLASH_MAGIC<<16);
nextIno = 1;
LOG(MOD_SYS|INFO, "FSCK initializing pristine flash");
return 0;
}
if( (magic[0] >> 16) == FLASH_MAGIC && (magic[1] >> 16) == FLASH_MAGIC ) {
// Both sections with magics - probably aborted GC
// Rerun GC on older section.
int d = magic[0] - magic[1];
if( d != 1 && d != -1 ) {
LOG(MOD_SYS|ERROR, "FSCK discovered strange magics: A=%08X B=%08X", magic[0], magic[1]);
}
fsSection = d < 0 ? 0 : 1;
LOG(MOD_SYS|INFO, "FSCK found two section markers: %c%d -> %c",
fsSection+'A', magic[fsSection] & 0xFFFF, (1^fsSection)+'A');
} else {
// Only one section has a magic marker - make it current.
assert( ((magic[0] >> 16) == FLASH_MAGIC) == !((magic[1] >> 16) == FLASH_MAGIC) );
fsSection = (magic[0] >> 16) == FLASH_MAGIC ? 0 : 1;
LOG(MOD_SYS|INFO, "FSCK found section marker %c%d",
fsSection+'A', magic[fsSection] & 0xFFFF);
}
// Validate current section
uint rcnt=0, maxino=0; int ino;
fctx_setTo(&fctxCache, flashFsBeg());
while( (ino = fs_validateRecord(&fctxCache)) >= 0 ) {
if( ino > maxino ) maxino = ino;
rcnt++;
}
nextIno = maxino+1; // unlikely ino rollover! -> emergency gc
flashWP = fctxCache.faddr;
LOG(MOD_SYS|INFO, "FSCK section %c: %d records, %d bytes used, %d bytes free",
fsSection+'A', rcnt, flashWP - (flashFsBeg()-4), flashFsMax()-flashWP);
u4_t fend = flashFsMax();
u4_t faddr = fctxCache.faddr;
while( faddr < fend ) {
u4_t len = fend - faddr;
if( len > AUXBUF_SZ4 )
len = AUXBUF_SZ4;
u4_t lenw = len/4;
sys_readFlash(faddr, auxbuf.u4, lenw);
for( int wi=0; wi<lenw; wi++ ) {
if( auxbuf.u4[wi] != FLASH_ERASED ) {
LOG(MOD_SYS|INFO, "FSCK section %c followed by dirty flash - GC required.", fsSection+'A');
fs_gc(0);
return 2;
}
}
faddr += len;
}
// We found a set of sane records followed by
// erased flash until section end.
// Do a smart erase of the other section
fs_smartErase(fsSection ? FLASH_BEG_A : FLASH_BEG_B, FS_PAGE_CNT/2);
LOG(MOD_SYS|INFO, "FSCK section %c followed by erased flash - all clear.", fsSection+'A');
return 1;
}
void fs_info(fsinfo_t* infop) {
infop->fbasep = sys_ptrFlash();
infop->fbase = FLASH_BEG_A;
infop->pagecnt = FS_PAGE_CNT & ~1;
infop->pagesize = FLASH_PAGE_SIZE;
infop->activeSection = fsSection;
infop->gcCycles = rdFlash1(flashFsBeg()-4) & 0xFFFF;
infop->used = flashWP - flashFsBeg() + 4;
infop->free = flashFsMax() - flashWP;
u4_t rcnt = 0;
u4_t faddr = flashFsBeg();
while( faddr < flashWP ) {
faddr += FSTAG_len(rdFlash1(faddr)) + 8;
rcnt++;
}
infop->records = rcnt;
memcpy(infop->key, flashKey, sizeof(infop->key));
}
void fs_gc (int emergency) {
// Invalidate all open files
// If any one of them survises GC it'll be reinstated
for( int fdi=0; fdi < FS_MAX_FD; fdi++ ) {
if( fhTable[fdi].ino != 0 )
fhTable[fdi].ino |= MAX_INO+1; // invalidate
}
u4_t faddrCont = flashFsBeg();
u4_t faddrEnd = flashWP;
fsSection ^= 1;
flashWP = flashFsBeg() - 4;
fsSection ^= 1;
wrFlash1wp(rdFlash1(flashFsBeg()-4) + 1);
nextIno = 1;
while( faddrCont < faddrEnd ) {
// Start a new collect phase - gather a set inodes
// and follow them til the end of the FS log.
// Cache is all zeros
struct ino_cache inocache[16] = {{ 0 }};
u1_t ucache = 0;
u1_t overflow = 0;
u4_t faddr = faddrCont;
u4_t begtag;
faddrCont = faddrEnd;
for(; faddr < faddrEnd; faddr += 8 + FSTAG_len(begtag) ) {
begtag = rdFlash1(faddr);
u1_t cmd = FSTAG_cmd(begtag);
if( cmd == FSCMD_DATA )
continue;
u4_t fncrc = rdFlash1(faddr+4);
s1_t match = -1;
// Is file in cache?
for( u1_t ui=0; ui<ucache; ui++ ) {
if( fncrc == inocache[ui].fncrc ) {