comparison src/cs/drivers/drv_app/ffs/board/core.h @ 0:4e78acac3d88

src/{condat,cs,gpf,nucleus}: import from Selenite
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 16 Oct 2020 06:23:26 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4e78acac3d88
1 /******************************************************************************
2 * Flash File System (ffs)
3 * Idea, design and coding by Mads Meisner-Jensen, mmj@ti.com
4 *
5 * ffs core functions
6 *
7 * $Id: core.h 1.80.1.15.1.36 Thu, 08 Jan 2004 15:05:23 +0100 tsj $
8 *
9 ******************************************************************************/
10
11 #if (TARGET == 1)
12 #include "rv/rv_defined_swe.h"
13 #endif
14
15 /******************************************************************************
16 * Compile option switches
17 ******************************************************************************/
18
19 // FFS compiled with extra test functionality
20 #define FFS_TEST 1
21
22 // Default max number of simultaneous open files
23 #ifdef RVM_MSFE_SWE
24 #define FFS_FD_MAX 20
25 #else
26 #define FFS_FD_MAX 4
27 #endif
28
29 #define FFS_RECLAIM_NEW 1
30
31 /******************************************************************************
32 * Compile constants
33 ******************************************************************************/
34
35 // FFS API version (in four-digit BCD format)
36 #define FFS_API_VERSION ((uint16) 0x0642)
37
38 // FFS_DRV_VERSION is in drv.h
39
40 // TMFFS protocol version is in tmffs.h
41
42 // Magic for determining (formatted) file system version. First two digits
43 // represent major version, bottom two digits represent minor version. An
44 // ffs code compiled for one major version X is compatible with any other
45 // format version with major = X. Minor version is incremented when adding
46 // new features that does not break compatibility.
47 #define FFS_FORMAT_VERSION (0x0210)
48 #define BLOCK_MAGIC_LOW ('f'<<8|'F') // "Ffs#"
49 #define BLOCK_MAGIC_HIGH ('#'<<8|'s')
50 #define BLOCK_MAGIC ((BLOCK_MAGIC_HIGH << 8)|(BLOCK_MAGIC_LOW))
51
52 // Absolute maximum number of inodes allowed
53 #define FFS_INODES_MAX 2048
54
55 // Default maximum number of inodes allowed
56 #define FFS_INODES_MAX_DEFAULT 1024
57
58 // Default number of path components (limit due to recursiveness of
59 // inodes_reclaim())
60 #define FFS_PATH_DEPTH_MAX 6
61
62 // Maximum number of blocks (flash sectors) in a ffs system. FFS_BLOCKS_MAX
63 // must be >= the number of blocks in the largest flash device memory
64 // map. It is used to allocate the number of entries in the static bstat
65 // array.
66 #define FFS_BLOCKS_MAX 128
67 // Default size of journal file (represented as 256'ths of the blocksize)
68 #define FFS_JOURNAL_SIZE_IN256THS 16 // one 16'ths of the block size.
69
70 // Without the min size will the maximum of files (fs.blocks_files_max) in
71 // one block be 32 files if the blocksize is 8kB!
72 #define FFS_JOURNAL_SIZE_MIN 1024
73
74 #define FFS_JOURNAL_NAME ".journal"
75
76 // Default max size of file name (excluding null terminator)
77 #define FFS_FILENAME_MAX 20
78
79 // Maximum distance in age between youngest and oldest blocks
80 #define FFS_DAGE_MAX 256
81 #define FFS_DAGE_GAIN_MIN (FFS_DAGE_MAX / 4)
82 #define FFS_DAGE_EARLY_WIDTH 64
83
84 // Offset on file descriptors
85 #define FFS_FD_OFFSET '1'
86
87 // Macros to set flags and test bits in flash memory words (negative logic)
88 #define BIT_SET(value, bits) ((value) & (~bits))
89 #define IS_BIT_SET(value, bits) (~(value) & (bits))
90
91 // Number of free inodes and journal entries to keep for "emergencies"
92 #define FFS_INODES_MARGIN 4
93 #define FFS_JOURNAL_MARGIN 4
94
95
96 /******************************************************************************
97 * Macros used in both drv.c and core.c
98 ******************************************************************************/
99
100 // Convert a offset_t value to a block index
101 #define offset2block(offset) (((uint32) offset) >> dev.binfo[0].size_ld)
102
103 // Convert between offset and address
104 #define offset2addr(offset) (dev.base + (offset))
105
106 // Size of a block
107 #define blocksize(block) (1 << dev.binfo[block].size_ld)
108
109 // Test if flag is set
110 #define is_open_option(options, flags) ((options & flags) == flags)
111
112 // Amount of reserved space.
113 #define RESERVED_LOW 2 * fs.journal_size
114 #define RESERVED_NONE 0
115
116 // We have to saturate because a recently reclaimed inodes block could
117 // theoretically possess a high age
118 #define saturate_dage(dage) (dage > (2*FFS_DAGE_MAX) ? (2*FFS_DAGE_MAX) : dage)
119
120 /******************************************************************************
121 * External declarations
122 ******************************************************************************/
123
124 extern struct fs_s fs;
125
126 extern struct ffs_stats_s stats;
127
128 extern const struct block_info_s *binfo;
129
130
131 /******************************************************************************
132 * Block Types
133 ******************************************************************************/
134
135 // Block age, ie. number of times block has been erased
136 typedef uint16 age_t;
137
138 // Maximum age a block can have
139 #define BLOCK_AGE_MAX 0xFFFF
140
141 // ffs block status flags. These are stored in the first 2 bytes of
142 // the ffs block in the flash sector.
143 enum BLOCK_FLAGS {
144 BF_LOST = 0x80, // block is lost and will soon be erased
145 BF_FREE = 0x40, // free (preformatted and with block magic)
146 BF_DATA = 0x02, // data
147 BF_CLEANING = 0x01, // block is being cleaned
148 BF_INODES = 0x10, // block contains inodes
149 BF_COPYING = 0x04 // block is a coming inodes block
150 };
151 enum BLOCK_STATES {
152 BF_IS_EMPTY = ~(0),
153 BF_IS_FREE = ~(BF_FREE),
154 BF_IS_DATA = ~(BF_FREE | BF_DATA),
155 BF_IS_CLEANING = ~(BF_FREE | BF_DATA | BF_CLEANING),
156 BF_IS_COPYING = ~(BF_FREE | BF_COPYING),
157 BF_IS_INODES = ~(BF_FREE | BF_COPYING | BF_INODES),
158 BF_IS_INODES_LOST = ~(BF_FREE | BF_COPYING | BF_INODES | BF_LOST)
159 };
160
161 // Header of each FFS block
162 struct block_header_s {
163 uint16 magic_low; // 32-bit magic number
164 uint16 magic_high;
165 uint16 version; // FFS_FORMAT_VERSION used for formatting
166 age_t age; // number of times this block has been erased
167 uint16 flags; // status flags of this block (BLOCK_FLAGS)
168 uint16 reserved0;
169 uint16 reserved1;
170 uint16 reserved2;
171 };
172
173 // Important the below define MUST fit to the size of the header that is written
174 #define BHEADER_SIZE sizeof(struct block_header_s)
175
176 #define OLD_BLOCK_MAGIC_LOW ('S'<<8|'F') // "FS"
177 #define OLD_FFS_FORMAT_VERSION (0x0100) // 1.00 (in four-digit BCD format)
178
179 // Old header of each FFS block. From old/previous FFS format version
180 struct block_header_old_s {
181 uint8 flags;
182 uint8 copied;
183 uint8 magicflags;
184 uint8 reserved0;
185 uint16 magic_low;
186 uint16 magic_high;
187 uint16 reserved1;
188 uint16 reserved2;
189 };
190
191 // Block status. This struct holds the status of one ffs block This relation
192 // is always valid: <block size> = <used> + <lost> + <free>. The block size
193 // is obtained from the corresponding block_info structure. <used> and
194 // <lost> variables always holds a value which is a multiple of
195 // FFS_GRANULARITY. For inodes, <used> is number of inodes in active use,
196 // <lost> is number of deleted/lost inodes, <numfiles> is the index of the
197 // first free inode.
198 struct block_stat_s {
199 blocksize_t used; // number of used bytes
200 blocksize_t lost; // number of lost bytes
201 uint16 flags; // flash block flags (first 16 bits of each block)
202 uint16 objects; // number of valid objects
203 };
204
205 extern struct block_stat_s bstat[FFS_BLOCKS_MAX];
206
207
208 /******************************************************************************
209 * Object Types
210 ******************************************************************************/
211
212 // This enum MUST be in sync with the one in ffs.h.
213 enum OBJECT_TYPE_E {
214 // remaining filetypes are in ffs.h
215 OT_ERASED = 0,
216 OT_NULL = 7,
217 OT_MASK = 7,
218 OT_MAX = 4
219 };
220
221 // This enum MUST be in sync with the one in ffs.h.
222 enum OBJECT_FLAGS_E {
223 // remaining object flags are in ffs.h
224 OF_UNDEF0 = 1<<5,
225 OF_UNDEF1 = 1<<6,
226 OF_EXACT = 1<<7, // used by control()/update_commit() interaction. This
227 // is *not* an object flag!
228 OF_ALL = OF_READONLY, // all flags allowed to be changed by user
229 OF_MASK = 0xF0
230 };
231
232 struct inode_s {
233 uint16 size;
234 uint8 reserved; // size extension?
235 objflags_t flags;
236 iref_t child; // link to first inode in dir (this inode is a dir)
237 iref_t sibling; // link to next inode in same directory
238 location_t location; // location of object
239 uint16 sequence; //
240 uint16 updates; // times this object has been updated
241 };
242
243 struct file_descriptor_s {
244 char *buf; // Write buffer
245 iref_t seghead; // First chunk. Contain file name and optional data
246 iref_t wch; // Inode of work chunk (if chunk is read to buf)
247 int fp; // File pointer
248 int wfp; // Work file pointer always points to start of wch
249 int size; // Size of object (all chunks and data from buf)
250 int8 options; // Open options
251 int dirty; // Indicate if buf contain valid data or not
252 };
253
254
255 /******************************************************************************
256 * Journal types and global fs structure
257 ******************************************************************************/
258
259 enum JOURNAL_FLAGS {
260 JOURNAL_WRITING = 0x02, // journal is being written to journal file
261 JOURNAL_READY = 0x04, // journal has been written to journal file
262 JOURNAL_DONE = 0x08 // journal has been written to ffs
263 };
264
265 enum JOURNAL_STATES {
266 JOURNAL_IS_EMPTY = ~(0),
267 JOURNAL_IS_WRITING = ~(JOURNAL_WRITING),
268 JOURNAL_IS_READY = ~(JOURNAL_WRITING | JOURNAL_READY),
269 JOURNAL_IS_DONE = ~(JOURNAL_WRITING | JOURNAL_READY | JOURNAL_DONE)
270 };
271
272 // Journal entry structure. Note that the state byte *MUST* be the first
273 // byte of the structure!
274 struct journal_s {
275 uint8 state; // state of journal entry.
276 objflags_t flags; // type of object
277 iref_t i; // iref of object
278 iref_t diri; // iref of object that is this object's parent/sibling
279 iref_t oldi; // iref of object being replaced (only for updates)
280 location_t location; // object's location
281 uint16 size; // object's size
282 iref_t repli; // inode which is replaced
283 };
284
285 // Main ffs info struct (initialised by ffs_initialize())
286 struct fs_s {
287 struct inode_s *inodes_addr; // base address of inodes
288 iref_t root; // iref of root directory
289 bref_t inodes; // index into bstat containing inode block
290 bref_t newinodes; // index into bstat containing new inode block
291 bref_t blocks_free_min; // Number of spare blocks (0 or 1)
292 int filesize_max; // Max size of object data
293 int reserved_space; // Byte size of space reserved for journal relocation
294 iref_t inodes_max; // Max number of inodes possible
295 iref_t inodes_high; // number of inodes triggering an inodes_reclaim()
296 iref_t objects_max; // Max number of objects (valid inodes) allowed
297 age_t age_max; // Max block age found by blocks_fsck()
298 iref_t block_files_max; // max number of files in a block
299 iref_t block_files_reserved; // Reserved for journals
300 uint16 format; // FFS version as formatted in flash blocks
301 uint16 sequence; // Object sequence number (for debug only)
302 effs_t initerror; // ffs_initialize() return code
303 uint8 lost_threshold; // Threshold percentage for data block reclaim
304 uint8 flags; // Global FFS options/flags
305 uint8 filename_max; // Max length of a filename
306 uint8 path_depth_max; // Max path componenents allowed
307 uint8 numfds; // Mumber of available file descriptors
308 uint8 testflags;
309 int8 journal_depth; // Current journal nesting depth (0 or 1)
310 iref_t ijournal; // iref of journal file
311 uint32 journal_size; // Byte size of journal file
312 uint32 journal_pos; // Byte offset to first free entry in journal file
313 struct journal_s journal;
314 uint8 fd_max; // number of max available file descriptors
315 int fd_buf_size; // size of stream buffer
316 struct file_descriptor_s fd[FFS_FD_MAX];
317 struct journal_s ojournal; // "Old" journal
318 int link_child; // Link child in journal or not
319 iref_t i_backup; // Used by ffs_file_write()
320 int chunk_size_max; // Max size of one chunk
321 int chunk_size_min; // Min size of one chunk
322 uint32 debug[4];
323 };
324
325 // This is the layout of the FFS performance statistics file. The file is
326 // created with the name ".statistics" in the root directory at format. It
327 // is updated after each data and inodes reclaim (after writing the file
328 // that provoked the reclaim). The file is only updated if it exists, so if
329 // the user does not want the file, she can erase it after the initial
330 // format. FIXME: The use of the .statistics file is not yet implemented
331 struct ffs_stats_s {
332 uint32 data_allocated; // implemented
333
334 struct { // Not yet implemented
335 uint32 created;
336 uint32 updated;
337 uint32 read;
338 } files;
339 struct { // Not yet implemented
340 uint32 written[2];
341 uint32 read[2];
342 } bytes;
343 struct {
344 uint32 most_lost; // Block candidate
345 uint32 most_unused; // Block candidate
346 uint32 youngest; // Block candidate
347 uint32 valid[2]; // Amount of valid reclaimed data
348 uint32 lost[2]; // Amount of lost reclaimed data
349 } drec;
350 struct {
351 uint32 num; // Number of inode reclaims
352 uint32 valid; // Number of valid reclaimed inodes
353 uint32 lost; // Number of lost reclaimed inodes
354 } irec;
355 };
356 extern struct ffs_stats_s stats;
357
358
359 /******************************************************************************
360 * Miscellaneous types
361 ******************************************************************************/
362
363 // only used with (FFS_TEST == 1)
364 enum TEST_RECOVERY {
365 JOURNAL_TEST_BASE = 0x10,
366 JOURNAL_TEST_EMPTY,
367 JOURNAL_TEST_WRITING,
368 JOURNAL_TEST_READY,
369 JOURNAL_TEST_COMMITTING,
370 JOURNAL_TEST_COMMITTED,
371 JOURNAL_TEST_DONE,
372 BLOCK_COMMIT_BASE = 0x20,
373 BLOCK_COMMIT_BEFORE,
374 BLOCK_COMMIT_NO_VALID,
375 BLOCK_COMMIT_OLD_FREE,
376 BLOCK_COMMIT_AFTER,
377 BLOCK_RECLAIM_BASE = 0x40,
378 BLOCK_RECLAIM_ALLOC,
379 BLOCK_RECLAIM_CLEANING,
380 BLOCK_RECLAIM_NO_CLEAN,
381 BLOCK_RECOVER_OBJECTS
382 };
383
384 enum FLASH_DATA {
385 FLASH_NULL8 = 0xFF,
386 FLASH_NULL16 = 0xFFFF,
387 FLASH_NULL32 = 0xFFFFFFFFL,
388 IREF_NULL = FLASH_NULL16
389 };
390
391
392 // This enum MUST be in sync with the one in ffs.h.
393 enum OBJECT_CONTROL {
394 // remaining object control codes are in ffs.h
395 OC_FS_FLAGS = 80,
396 OC_TRACE_INIT = 82,
397 OC_DEV_MANUFACT = 88,
398 OC_DEV_DEVICE = 89,
399
400 OC_DEBUG_FIRST = 120,
401 OC_DEBUG_0 = 120,
402 OC_DEBUG_1 = 121,
403 OC_DEBUG_2 = 122,
404 OC_DEBUG_3 = 123,
405 OC_DEBUG_LAST = 123,
406
407 OC_FS_TESTFLAGS = 127
408 };
409
410 enum FS_FLAGS {
411 FS_DIR_DATA = 0x01 // allow directory objects to contain data.
412 };
413
414 enum RECLAIM_CANDIDATE {
415 MOST_LOST,
416 MOST_UNUSED,
417 YOUNGEST
418 };
419
420 /******************************************************************************
421 * Macros
422 ******************************************************************************/
423
424 // Convert between location and offset
425 #define location2offset(location) ((location) << dev.atomlog2)
426 #define offset2location(offset) (((uint32) offset) >> dev.atomlog2)
427
428 // test if object is of a specific type
429 #define is_object(objp, type) (((objp)->flags & OT_MASK) == (type))
430
431 // test if object is valid (directory, file or symlink)
432 #define is_object_valid(ip) ((ip->flags & OT_MASK) <= OT_MAX && (ip->flags & OT_MASK) != OT_ERASED)
433
434 // test if block is in a specific state
435 #define is_block(block, state) (bstat[block].flags == (uint16) (state))
436
437 // test if block has certain flags set
438 #define is_block_flag(block, bits) (IS_BIT_SET(bstat[block].flags, (bits)))
439
440 // convert an object's data address to the address of the object's name
441 #define addr2name(addr) (addr)
442
443 // Convert a size to an aligned size
444 #define atomalign(size) (((size) + dev.atomsize-1) & ~dev.atomnotmask)
445 #define wordalign(size) (((size) + 3) & ~3)
446 #define halfwordalign(size) (((size) + 1) & ~1)
447
448 #define inode_addr(i) (fs.inodes_addr + i)
449
450 #define JOURNAL_POS_INITIAL (wordalign(2 + sizeof(FFS_JOURNAL_NAME) + 1))
451
452
453 /******************************************************************************
454 * Function prototypes
455 ******************************************************************************/
456
457 // Helper functions
458
459 effs_t is_filename(const char *s);
460 int ffs_strlen(const char *s);
461 int ffs_strcmp(const char *s, const char *p);
462 char *addr2data(const char *addr, const struct inode_s *ip);
463
464 int object_datasize(iref_t i);
465 iref_t is_readonly(iref_t i, const char *name);
466 iref_t dir_traverse(iref_t i, iref_t *entries);
467
468 bref_t block_alloc(bref_t n, uint16 flags);
469 bref_t block_alloc_try(bref_t *n);
470 void block_flags_write(uint8 block, uint8 flags);
471
472 offset_t data_alloc(int size);
473 offset_t data_alloc_try(int size);
474 offset_t data_reserved_alloc(int size);
475
476 iref_t inode_alloc(void);
477
478 effs_t is_fd_valid(fd_t fdi);
479 effs_t is_offset_in_buf(int offset, fd_t fdi);
480
481 iref_t chunk_alloc(int realsize, int is_journal, offset_t *offset);
482
483 iref_t inode_alloc_try(void);
484 fd_t get_fdi(iref_t i);
485
486 offset_t data_prealloc(int realsize);
487
488 // Functions used by API
489
490 effs_t object_update(iref_t oldi);
491 iref_t object_create(const char *name, const char *buf, int size,
492 iref_t dir);
493 int file_read(const char *name, void *addr, int size);
494 int stream_read(fd_t fdi, void *src, int size);
495 int object_read(const char *name, char *buf, int size, int linkflag);
496
497 iref_t object_stat(const char *name, struct xstat_s *stat,
498 int linkflag, int fdi, int extended);
499 effs_t object_remove(iref_t i);
500 iref_t object_rename(iref_t oldi, const char *newname, iref_t newdir);
501 effs_t object_control(iref_t i, int8 action, int value);
502 int object_truncate(const char *pathname, fd_t fdi, offset_t length);
503 iref_t object_lookup(const char *path, char **leaf, iref_t *dir);
504 iref_t object_lookup_once(const char *path, char **leaf, iref_t *dir);
505 iref_t dir_open(const char *name);
506 iref_t dir_next (iref_t dir, iref_t i, char *name, int8 size);
507
508
509 // Journalling
510
511 void journal_begin(iref_t oldi);
512 void journal_end(uint8 type);
513 void journal_commit(uint8 type);
514 int journal_push(void);
515 int journal_pop(void);
516 iref_t journal_create(iref_t oldi);
517 effs_t journal_init(iref_t i);
518
519
520 // Format, Init and Reclaim
521
522 void block_preformat(bref_t b, age_t age);
523 effs_t fs_preformat(void);
524 effs_t is_formattable(int8 flag);
525 effs_t fs_format(const char *fsname_and_options);
526
527 effs_t ffs_initialize(void);
528 void fs_params_init(const char *p);
529 blocksize_t block_used(bref_t b);
530
531 effs_t ffs_begin(void);
532 int ffs_end(int error);
533
534 int block_reclaim(bref_t b);
535 int blocks_reclaim(void);
536 void block_commit(void);
537
538 iref_t data_reclaim(int space);
539 int data_reclaim_try(int space);
540 iref_t data_block_reclaim(bref_t b, int reclaim_candidate);
541 iref_t object_relocate(iref_t oldi);
542 iref_t block_clean(bref_t b);
543
544 void block_free(bref_t block);
545
546 void inodes_set(iref_t i);
547 effs_t inodes_reclaim(void);
548
549 int reclaim(void);
550
551 // Internally used functions
552
553 effs_t file_read_int(const char *path, void *src, int size);
554 effs_t file_update(const char *path, void *src, int size);
555
556 int statistics_file_create(void);
557 int statistics_write(void);
558 void statistics_init(void);
559 void statistics_update_drec(int valid, int lost, int candidate);
560 void statistics_update_irec(int valid, int lost);
561
562 // Chunk Operations
563 iref_t segment_create(const char *buf, int size, iref_t dir);
564 int segment_datasize(const struct inode_s *ip);
565 int segment_read(iref_t i, char *buf, int size, int offset);
566 iref_t segment_next(iref_t i);
567 iref_t segment_traverse(iref_t i, iref_t *entries);
568 int segfile_seek(iref_t in_i, int in_pos,
569 iref_t *out_i, int *out_pos_i);
570 iref_t chunk_traverse(iref_t i);
571 effs_t datasync(fd_t fdi);
572 // debug/test functions
573
574 void tr_bstat(void);
575 void tr_fd(fd_t fdi);
576
577 // These prototypes really belong in ffs.h but as they have not been
578 // implemented, we will not show these prototypes to application
579 // programmers.
580 effs_t fcntl(fd_t fd, int8 action, uint32 *param);