• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * The little filesystem
3  *
4  * Copyright (c) 2022, The littlefs authors.
5  * Copyright (c) 2017, Arm Limited. All rights reserved.
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 #ifndef LFS_H
9 #define LFS_H
10 
11 #include "lfs_util.h"
12 
13 #ifdef __cplusplus
14 extern "C"
15 {
16 #endif
17 
18 
19 /// Version info ///
20 
21 // Software library version
22 // Major (top-nibble), incremented on backwards incompatible changes
23 // Minor (bottom-nibble), incremented on feature additions
24 #define LFS_VERSION 0x00020009
25 #define LFS_VERSION_MAJOR (0xffff & (LFS_VERSION >> 16))
26 #define LFS_VERSION_MINOR (0xffff & (LFS_VERSION >>  0))
27 
28 // Version of On-disk data structures
29 // Major (top-nibble), incremented on backwards incompatible changes
30 // Minor (bottom-nibble), incremented on feature additions
31 #define LFS_DISK_VERSION 0x00020001
32 #define LFS_DISK_VERSION_MAJOR (0xffff & (LFS_DISK_VERSION >> 16))
33 #define LFS_DISK_VERSION_MINOR (0xffff & (LFS_DISK_VERSION >>  0))
34 
35 
36 /// Definitions ///
37 
38 // Type definitions
39 typedef uint32_t lfs_size_t;
40 typedef uint32_t lfs_off_t;
41 
42 typedef int32_t  lfs_ssize_t;
43 typedef int32_t  lfs_soff_t;
44 
45 typedef uint32_t lfs_block_t;
46 
47 // Maximum name size in bytes, may be redefined to reduce the size of the
48 // info struct. Limited to <= 1022. Stored in superblock and must be
49 // respected by other littlefs drivers.
50 #ifndef LFS_NAME_MAX
51 #define LFS_NAME_MAX 255
52 #endif
53 
54 // Maximum size of a file in bytes, may be redefined to limit to support other
55 // drivers. Limited on disk to <= 2147483647. Stored in superblock and must be
56 // respected by other littlefs drivers.
57 #ifndef LFS_FILE_MAX
58 #define LFS_FILE_MAX 2147483647
59 #endif
60 
61 // Maximum size of custom attributes in bytes, may be redefined, but there is
62 // no real benefit to using a smaller LFS_ATTR_MAX. Limited to <= 1022.
63 #ifndef LFS_ATTR_MAX
64 #define LFS_ATTR_MAX 1022
65 #endif
66 
67 // Possible error codes, these are negative to allow
68 // valid positive return values
69 enum lfs_error {
70     LFS_ERR_OK          = 0,    // No error
71     LFS_ERR_IO          = -5,   // Error during device operation
72     LFS_ERR_CORRUPT     = -84,  // Corrupted
73     LFS_ERR_NOENT       = -2,   // No directory entry
74     LFS_ERR_EXIST       = -17,  // Entry already exists
75     LFS_ERR_NOTDIR      = -20,  // Entry is not a dir
76     LFS_ERR_ISDIR       = -21,  // Entry is a dir
77     LFS_ERR_NOTEMPTY    = -39,  // Dir is not empty
78     LFS_ERR_BADF        = -9,   // Bad file number
79     LFS_ERR_FBIG        = -27,  // File too large
80     LFS_ERR_INVAL       = -22,  // Invalid parameter
81     LFS_ERR_NOSPC       = -28,  // No space left on device
82     LFS_ERR_NOMEM       = -12,  // No more memory available
83     LFS_ERR_NOATTR      = -61,  // No data/attr available
84     LFS_ERR_NAMETOOLONG = -36,  // File name too long
85 };
86 
87 // File types
88 enum lfs_type {
89     // file types
90     LFS_TYPE_REG            = 0x001,
91     LFS_TYPE_DIR            = 0x002,
92 
93     // internally used types
94     LFS_TYPE_SPLICE         = 0x400,
95     LFS_TYPE_NAME           = 0x000,
96     LFS_TYPE_STRUCT         = 0x200,
97     LFS_TYPE_USERATTR       = 0x300,
98     LFS_TYPE_FROM           = 0x100,
99     LFS_TYPE_TAIL           = 0x600,
100     LFS_TYPE_GLOBALS        = 0x700,
101     LFS_TYPE_CRC            = 0x500,
102 
103     // internally used type specializations
104     LFS_TYPE_CREATE         = 0x401,
105     LFS_TYPE_DELETE         = 0x4ff,
106     LFS_TYPE_SUPERBLOCK     = 0x0ff,
107     LFS_TYPE_DIRSTRUCT      = 0x200,
108     LFS_TYPE_CTZSTRUCT      = 0x202,
109     LFS_TYPE_INLINESTRUCT   = 0x201,
110     LFS_TYPE_SOFTTAIL       = 0x600,
111     LFS_TYPE_HARDTAIL       = 0x601,
112     LFS_TYPE_MOVESTATE      = 0x7ff,
113     LFS_TYPE_CCRC           = 0x500,
114     LFS_TYPE_FCRC           = 0x5ff,
115 
116     // internal chip sources
117     LFS_FROM_NOOP           = 0x000,
118     LFS_FROM_MOVE           = 0x101,
119     LFS_FROM_USERATTRS      = 0x102,
120 };
121 
122 // File open flags
123 enum lfs_open_flags {
124     // open flags
125     LFS_O_RDONLY = 1,         // Open a file as read only
126 #ifndef LFS_READONLY
127     LFS_O_WRONLY = 2,         // Open a file as write only
128     LFS_O_RDWR   = 3,         // Open a file as read and write
129     LFS_O_CREAT  = 0x0100,    // Create a file if it does not exist
130     LFS_O_EXCL   = 0x0200,    // Fail if a file already exists
131     LFS_O_TRUNC  = 0x0400,    // Truncate the existing file to zero size
132     LFS_O_APPEND = 0x0800,    // Move to end of file on every write
133 #endif
134 
135     // internally used flags
136 #ifndef LFS_READONLY
137     LFS_F_DIRTY   = 0x010000, // File does not match storage
138     LFS_F_WRITING = 0x020000, // File has been written since last flush
139 #endif
140     LFS_F_READING = 0x040000, // File has been read since last flush
141 #ifndef LFS_READONLY
142     LFS_F_ERRED   = 0x080000, // An error occurred during write
143 #endif
144     LFS_F_INLINE  = 0x100000, // Currently inlined in directory entry
145 };
146 
147 // File seek flags
148 enum lfs_whence_flags {
149     LFS_SEEK_SET = 0,   // Seek relative to an absolute position
150     LFS_SEEK_CUR = 1,   // Seek relative to the current file position
151     LFS_SEEK_END = 2,   // Seek relative to the end of the file
152 };
153 
154 
155 // Configuration provided during initialization of the littlefs
156 struct lfs_config {
157     // Opaque user provided context that can be used to pass
158     // information to the block device operations
159     void *context;
160 
161     // Read a region in a block. Negative error codes are propagated
162     // to the user.
163     int (*read)(const struct lfs_config *c, lfs_block_t block,
164             lfs_off_t off, void *buffer, lfs_size_t size);
165 
166     // Program a region in a block. The block must have previously
167     // been erased. Negative error codes are propagated to the user.
168     // May return LFS_ERR_CORRUPT if the block should be considered bad.
169     int (*prog)(const struct lfs_config *c, lfs_block_t block,
170             lfs_off_t off, const void *buffer, lfs_size_t size);
171 
172     // Erase a block. A block must be erased before being programmed.
173     // The state of an erased block is undefined. Negative error codes
174     // are propagated to the user.
175     // May return LFS_ERR_CORRUPT if the block should be considered bad.
176     int (*erase)(const struct lfs_config *c, lfs_block_t block);
177 
178     // Sync the state of the underlying block device. Negative error codes
179     // are propagated to the user.
180     int (*sync)(const struct lfs_config *c);
181 
182 #ifdef LFS_THREADSAFE
183     // Lock the underlying block device. Negative error codes
184     // are propagated to the user.
185     int (*lock)(const struct lfs_config *c);
186 
187     // Unlock the underlying block device. Negative error codes
188     // are propagated to the user.
189     int (*unlock)(const struct lfs_config *c);
190 #endif
191 
192     // Minimum size of a block read in bytes. All read operations will be a
193     // multiple of this value.
194     lfs_size_t read_size;
195 
196     // Minimum size of a block program in bytes. All program operations will be
197     // a multiple of this value.
198     lfs_size_t prog_size;
199 
200     // Size of an erasable block in bytes. This does not impact ram consumption
201     // and may be larger than the physical erase size. However, non-inlined
202     // files take up at minimum one block. Must be a multiple of the read and
203     // program sizes.
204     lfs_size_t block_size;
205 
206     // Number of erasable blocks on the device.
207     lfs_size_t block_count;
208 
209     // Number of erase cycles before littlefs evicts metadata logs and moves
210     // the metadata to another block. Suggested values are in the
211     // range 100-1000, with large values having better performance at the cost
212     // of less consistent wear distribution.
213     //
214     // Set to -1 to disable block-level wear-leveling.
215     int32_t block_cycles;
216 
217     // Size of block caches in bytes. Each cache buffers a portion of a block in
218     // RAM. The littlefs needs a read cache, a program cache, and one additional
219     // cache per file. Larger caches can improve performance by storing more
220     // data and reducing the number of disk accesses. Must be a multiple of the
221     // read and program sizes, and a factor of the block size.
222     lfs_size_t cache_size;
223 
224     // Size of the lookahead buffer in bytes. A larger lookahead buffer
225     // increases the number of blocks found during an allocation pass. The
226     // lookahead buffer is stored as a compact bitmap, so each byte of RAM
227     // can track 8 blocks.
228     lfs_size_t lookahead_size;
229 
230     // Threshold for metadata compaction during lfs_fs_gc in bytes. Metadata
231     // pairs that exceed this threshold will be compacted during lfs_fs_gc.
232     // Defaults to ~88% block_size when zero, though the default may change
233     // in the future.
234     //
235     // Note this only affects lfs_fs_gc. Normal compactions still only occur
236     // when full.
237     //
238     // Set to -1 to disable metadata compaction during lfs_fs_gc.
239     lfs_size_t compact_thresh;
240 
241     // Optional statically allocated read buffer. Must be cache_size.
242     // By default lfs_malloc is used to allocate this buffer.
243     void *read_buffer;
244 
245     // Optional statically allocated program buffer. Must be cache_size.
246     // By default lfs_malloc is used to allocate this buffer.
247     void *prog_buffer;
248 
249     // Optional statically allocated lookahead buffer. Must be lookahead_size.
250     // By default lfs_malloc is used to allocate this buffer.
251     void *lookahead_buffer;
252 
253     // Optional upper limit on length of file names in bytes. No downside for
254     // larger names except the size of the info struct which is controlled by
255     // the LFS_NAME_MAX define. Defaults to LFS_NAME_MAX when zero. Stored in
256     // superblock and must be respected by other littlefs drivers.
257     lfs_size_t name_max;
258 
259     // Optional upper limit on files in bytes. No downside for larger files
260     // but must be <= LFS_FILE_MAX. Defaults to LFS_FILE_MAX when zero. Stored
261     // in superblock and must be respected by other littlefs drivers.
262     lfs_size_t file_max;
263 
264     // Optional upper limit on custom attributes in bytes. No downside for
265     // larger attributes size but must be <= LFS_ATTR_MAX. Defaults to
266     // LFS_ATTR_MAX when zero.
267     lfs_size_t attr_max;
268 
269     // Optional upper limit on total space given to metadata pairs in bytes. On
270     // devices with large blocks (e.g. 128kB) setting this to a low size (2-8kB)
271     // can help bound the metadata compaction time. Must be <= block_size.
272     // Defaults to block_size when zero.
273     lfs_size_t metadata_max;
274 
275     // Optional upper limit on inlined files in bytes. Inlined files live in
276     // metadata and decrease storage requirements, but may be limited to
277     // improve metadata-related performance. Must be <= cache_size, <=
278     // attr_max, and <= block_size/8. Defaults to the largest possible
279     // inline_max when zero.
280     //
281     // Set to -1 to disable inlined files.
282     lfs_size_t inline_max;
283 
284 #ifdef LFS_MULTIVERSION
285     // On-disk version to use when writing in the form of 16-bit major version
286     // + 16-bit minor version. This limiting metadata to what is supported by
287     // older minor versions. Note that some features will be lost. Defaults to
288     // to the most recent minor version when zero.
289     uint32_t disk_version;
290 #endif
291 };
292 
293 // File info structure
294 struct lfs_info {
295     // Type of the file, either LFS_TYPE_REG or LFS_TYPE_DIR
296     uint8_t type;
297 
298     // Size of the file, only valid for REG files. Limited to 32-bits.
299     lfs_size_t size;
300 
301     // Name of the file stored as a null-terminated string. Limited to
302     // LFS_NAME_MAX+1, which can be changed by redefining LFS_NAME_MAX to
303     // reduce RAM. LFS_NAME_MAX is stored in superblock and must be
304     // respected by other littlefs drivers.
305     char name[LFS_NAME_MAX+1];
306 };
307 
308 // Filesystem info structure
309 struct lfs_fsinfo {
310     // On-disk version.
311     uint32_t disk_version;
312 
313     // Size of a logical block in bytes.
314     lfs_size_t block_size;
315 
316     // Number of logical blocks in filesystem.
317     lfs_size_t block_count;
318 
319     // Upper limit on the length of file names in bytes.
320     lfs_size_t name_max;
321 
322     // Upper limit on the size of files in bytes.
323     lfs_size_t file_max;
324 
325     // Upper limit on the size of custom attributes in bytes.
326     lfs_size_t attr_max;
327 };
328 
329 // Custom attribute structure, used to describe custom attributes
330 // committed atomically during file writes.
331 struct lfs_attr {
332     // 8-bit type of attribute, provided by user and used to
333     // identify the attribute
334     uint8_t type;
335 
336     // Pointer to buffer containing the attribute
337     void *buffer;
338 
339     // Size of attribute in bytes, limited to LFS_ATTR_MAX
340     lfs_size_t size;
341 };
342 
343 // Optional configuration provided during lfs_file_opencfg
344 struct lfs_file_config {
345     // Optional statically allocated file buffer. Must be cache_size.
346     // By default lfs_malloc is used to allocate this buffer.
347     void *buffer;
348 
349     // Optional list of custom attributes related to the file. If the file
350     // is opened with read access, these attributes will be read from disk
351     // during the open call. If the file is opened with write access, the
352     // attributes will be written to disk every file sync or close. This
353     // write occurs atomically with update to the file's contents.
354     //
355     // Custom attributes are uniquely identified by an 8-bit type and limited
356     // to LFS_ATTR_MAX bytes. When read, if the stored attribute is smaller
357     // than the buffer, it will be padded with zeros. If the stored attribute
358     // is larger, then it will be silently truncated. If the attribute is not
359     // found, it will be created implicitly.
360     struct lfs_attr *attrs;
361 
362     // Number of custom attributes in the list
363     lfs_size_t attr_count;
364 };
365 
366 
367 /// internal littlefs data structures ///
368 typedef struct lfs_cache {
369     lfs_block_t block;
370     lfs_off_t off;
371     lfs_size_t size;
372     uint8_t *buffer;
373 } lfs_cache_t;
374 
375 typedef struct lfs_mdir {
376     lfs_block_t pair[2];
377     uint32_t rev;
378     lfs_off_t off;
379     uint32_t etag;
380     uint16_t count;
381     bool erased;
382     bool split;
383     lfs_block_t tail[2];
384 } lfs_mdir_t;
385 
386 // littlefs directory type
387 typedef struct lfs_dir {
388     struct lfs_dir *next;
389     uint16_t id;
390     uint8_t type;
391     lfs_mdir_t m;
392 
393     lfs_off_t pos;
394     lfs_block_t head[2];
395 } lfs_dir_t;
396 
397 // littlefs file type
398 typedef struct lfs_file {
399     struct lfs_file *next;
400     uint16_t id;
401     uint8_t type;
402     lfs_mdir_t m;
403 
404     struct lfs_ctz {
405         lfs_block_t head;
406         lfs_size_t size;
407     } ctz;
408 
409     uint32_t flags;
410     lfs_off_t pos;
411     lfs_block_t block;
412     lfs_off_t off;
413     lfs_cache_t cache;
414 
415     const struct lfs_file_config *cfg;
416 } lfs_file_t;
417 
418 typedef struct lfs_superblock {
419     uint32_t version;
420     lfs_size_t block_size;
421     lfs_size_t block_count;
422     lfs_size_t name_max;
423     lfs_size_t file_max;
424     lfs_size_t attr_max;
425 } lfs_superblock_t;
426 
427 typedef struct lfs_gstate {
428     uint32_t tag;
429     lfs_block_t pair[2];
430 } lfs_gstate_t;
431 
432 // The littlefs filesystem type
433 typedef struct lfs {
434     lfs_cache_t rcache;
435     lfs_cache_t pcache;
436 
437     lfs_block_t root[2];
438     struct lfs_mlist {
439         struct lfs_mlist *next;
440         uint16_t id;
441         uint8_t type;
442         lfs_mdir_t m;
443     } *mlist;
444     uint32_t seed;
445 
446     lfs_gstate_t gstate;
447     lfs_gstate_t gdisk;
448     lfs_gstate_t gdelta;
449 
450     struct lfs_lookahead {
451         lfs_block_t start;
452         lfs_block_t size;
453         lfs_block_t next;
454         lfs_block_t ckpoint;
455         uint8_t *buffer;
456     } lookahead;
457 
458     const struct lfs_config *cfg;
459     lfs_size_t block_count;
460     lfs_size_t name_max;
461     lfs_size_t file_max;
462     lfs_size_t attr_max;
463     lfs_size_t inline_max;
464 
465 #ifdef LFS_MIGRATE
466     struct lfs1 *lfs1;
467 #endif
468 } lfs_t;
469 
470 
471 /// Filesystem functions ///
472 
473 #ifndef LFS_READONLY
474 // Format a block device with the littlefs
475 //
476 // Requires a littlefs object and config struct. This clobbers the littlefs
477 // object, and does not leave the filesystem mounted. The config struct must
478 // be zeroed for defaults and backwards compatibility.
479 //
480 // Returns a negative error code on failure.
481 int lfs_format(lfs_t *lfs, const struct lfs_config *config);
482 #endif
483 
484 // Mounts a littlefs
485 //
486 // Requires a littlefs object and config struct. Multiple filesystems
487 // may be mounted simultaneously with multiple littlefs objects. Both
488 // lfs and config must be allocated while mounted. The config struct must
489 // be zeroed for defaults and backwards compatibility.
490 //
491 // Returns a negative error code on failure.
492 int lfs_mount(lfs_t *lfs, const struct lfs_config *config);
493 
494 // Unmounts a littlefs
495 //
496 // Does nothing besides releasing any allocated resources.
497 // Returns a negative error code on failure.
498 int lfs_unmount(lfs_t *lfs);
499 
500 /// General operations ///
501 
502 #ifndef LFS_READONLY
503 // Removes a file or directory
504 //
505 // If removing a directory, the directory must be empty.
506 // Returns a negative error code on failure.
507 int lfs_remove(lfs_t *lfs, const char *path);
508 #endif
509 
510 #ifndef LFS_READONLY
511 // Rename or move a file or directory
512 //
513 // If the destination exists, it must match the source in type.
514 // If the destination is a directory, the directory must be empty.
515 //
516 // Returns a negative error code on failure.
517 int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath);
518 #endif
519 
520 // Find info about a file or directory
521 //
522 // Fills out the info structure, based on the specified file or directory.
523 // Returns a negative error code on failure.
524 int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info);
525 
526 // Get a custom attribute
527 //
528 // Custom attributes are uniquely identified by an 8-bit type and limited
529 // to LFS_ATTR_MAX bytes. When read, if the stored attribute is smaller than
530 // the buffer, it will be padded with zeros. If the stored attribute is larger,
531 // then it will be silently truncated. If no attribute is found, the error
532 // LFS_ERR_NOATTR is returned and the buffer is filled with zeros.
533 //
534 // Returns the size of the attribute, or a negative error code on failure.
535 // Note, the returned size is the size of the attribute on disk, irrespective
536 // of the size of the buffer. This can be used to dynamically allocate a buffer
537 // or check for existence.
538 lfs_ssize_t lfs_getattr(lfs_t *lfs, const char *path,
539         uint8_t type, void *buffer, lfs_size_t size);
540 
541 #ifndef LFS_READONLY
542 // Set custom attributes
543 //
544 // Custom attributes are uniquely identified by an 8-bit type and limited
545 // to LFS_ATTR_MAX bytes. If an attribute is not found, it will be
546 // implicitly created.
547 //
548 // Returns a negative error code on failure.
549 int lfs_setattr(lfs_t *lfs, const char *path,
550         uint8_t type, const void *buffer, lfs_size_t size);
551 #endif
552 
553 #ifndef LFS_READONLY
554 // Removes a custom attribute
555 //
556 // If an attribute is not found, nothing happens.
557 //
558 // Returns a negative error code on failure.
559 int lfs_removeattr(lfs_t *lfs, const char *path, uint8_t type);
560 #endif
561 
562 
563 /// File operations ///
564 
565 #ifndef LFS_NO_MALLOC
566 // Open a file
567 //
568 // The mode that the file is opened in is determined by the flags, which
569 // are values from the enum lfs_open_flags that are bitwise-ored together.
570 //
571 // Returns a negative error code on failure.
572 int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
573         const char *path, int flags);
574 
575 // if LFS_NO_MALLOC is defined, lfs_file_open() will fail with LFS_ERR_NOMEM
576 // thus use lfs_file_opencfg() with config.buffer set.
577 #endif
578 
579 // Open a file with extra configuration
580 //
581 // The mode that the file is opened in is determined by the flags, which
582 // are values from the enum lfs_open_flags that are bitwise-ored together.
583 //
584 // The config struct provides additional config options per file as described
585 // above. The config struct must remain allocated while the file is open, and
586 // the config struct must be zeroed for defaults and backwards compatibility.
587 //
588 // Returns a negative error code on failure.
589 int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file,
590         const char *path, int flags,
591         const struct lfs_file_config *config);
592 
593 // Close a file
594 //
595 // Any pending writes are written out to storage as though
596 // sync had been called and releases any allocated resources.
597 //
598 // Returns a negative error code on failure.
599 int lfs_file_close(lfs_t *lfs, lfs_file_t *file);
600 
601 // Synchronize a file on storage
602 //
603 // Any pending writes are written out to storage.
604 // Returns a negative error code on failure.
605 int lfs_file_sync(lfs_t *lfs, lfs_file_t *file);
606 
607 // Read data from file
608 //
609 // Takes a buffer and size indicating where to store the read data.
610 // Returns the number of bytes read, or a negative error code on failure.
611 lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file,
612         void *buffer, lfs_size_t size);
613 
614 #ifndef LFS_READONLY
615 // Write data to file
616 //
617 // Takes a buffer and size indicating the data to write. The file will not
618 // actually be updated on the storage until either sync or close is called.
619 //
620 // Returns the number of bytes written, or a negative error code on failure.
621 lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file,
622         const void *buffer, lfs_size_t size);
623 #endif
624 
625 // Change the position of the file
626 //
627 // The change in position is determined by the offset and whence flag.
628 // Returns the new position of the file, or a negative error code on failure.
629 lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file,
630         lfs_soff_t off, int whence);
631 
632 #ifndef LFS_READONLY
633 // Truncates the size of the file to the specified size
634 //
635 // Returns a negative error code on failure.
636 int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size);
637 #endif
638 
639 // Return the position of the file
640 //
641 // Equivalent to lfs_file_seek(lfs, file, 0, LFS_SEEK_CUR)
642 // Returns the position of the file, or a negative error code on failure.
643 lfs_soff_t lfs_file_tell(lfs_t *lfs, lfs_file_t *file);
644 
645 // Change the position of the file to the beginning of the file
646 //
647 // Equivalent to lfs_file_seek(lfs, file, 0, LFS_SEEK_SET)
648 // Returns a negative error code on failure.
649 int lfs_file_rewind(lfs_t *lfs, lfs_file_t *file);
650 
651 // Return the size of the file
652 //
653 // Similar to lfs_file_seek(lfs, file, 0, LFS_SEEK_END)
654 // Returns the size of the file, or a negative error code on failure.
655 lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file);
656 
657 
658 /// Directory operations ///
659 
660 #ifndef LFS_READONLY
661 // Create a directory
662 //
663 // Returns a negative error code on failure.
664 int lfs_mkdir(lfs_t *lfs, const char *path);
665 #endif
666 
667 // Open a directory
668 //
669 // Once open a directory can be used with read to iterate over files.
670 // Returns a negative error code on failure.
671 int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path);
672 
673 // Close a directory
674 //
675 // Releases any allocated resources.
676 // Returns a negative error code on failure.
677 int lfs_dir_close(lfs_t *lfs, lfs_dir_t *dir);
678 
679 // Read an entry in the directory
680 //
681 // Fills out the info structure, based on the specified file or directory.
682 // Returns a positive value on success, 0 at the end of directory,
683 // or a negative error code on failure.
684 int lfs_dir_read(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info);
685 
686 // Change the position of the directory
687 //
688 // The new off must be a value previous returned from tell and specifies
689 // an absolute offset in the directory seek.
690 //
691 // Returns a negative error code on failure.
692 int lfs_dir_seek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off);
693 
694 // Return the position of the directory
695 //
696 // The returned offset is only meant to be consumed by seek and may not make
697 // sense, but does indicate the current position in the directory iteration.
698 //
699 // Returns the position of the directory, or a negative error code on failure.
700 lfs_soff_t lfs_dir_tell(lfs_t *lfs, lfs_dir_t *dir);
701 
702 // Change the position of the directory to the beginning of the directory
703 //
704 // Returns a negative error code on failure.
705 int lfs_dir_rewind(lfs_t *lfs, lfs_dir_t *dir);
706 
707 
708 /// Filesystem-level filesystem operations
709 
710 // Find on-disk info about the filesystem
711 //
712 // Fills out the fsinfo structure based on the filesystem found on-disk.
713 // Returns a negative error code on failure.
714 int lfs_fs_stat(lfs_t *lfs, struct lfs_fsinfo *fsinfo);
715 
716 // Finds the current size of the filesystem
717 //
718 // Note: Result is best effort. If files share COW structures, the returned
719 // size may be larger than the filesystem actually is.
720 //
721 // Returns the number of allocated blocks, or a negative error code on failure.
722 lfs_ssize_t lfs_fs_size(lfs_t *lfs);
723 
724 // Traverse through all blocks in use by the filesystem
725 //
726 // The provided callback will be called with each block address that is
727 // currently in use by the filesystem. This can be used to determine which
728 // blocks are in use or how much of the storage is available.
729 //
730 // Returns a negative error code on failure.
731 int lfs_fs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data);
732 
733 #ifndef LFS_READONLY
734 // Attempt to make the filesystem consistent and ready for writing
735 //
736 // Calling this function is not required, consistency will be implicitly
737 // enforced on the first operation that writes to the filesystem, but this
738 // function allows the work to be performed earlier and without other
739 // filesystem changes.
740 //
741 // Returns a negative error code on failure.
742 int lfs_fs_mkconsistent(lfs_t *lfs);
743 #endif
744 
745 #ifndef LFS_READONLY
746 // Attempt any janitorial work
747 //
748 // This currently:
749 // 1. Calls mkconsistent if not already consistent
750 // 2. Compacts metadata > compact_thresh
751 // 3. Populates the block allocator
752 //
753 // Though additional janitorial work may be added in the future.
754 //
755 // Calling this function is not required, but may allow the offloading of
756 // expensive janitorial work to a less time-critical code path.
757 //
758 // Returns a negative error code on failure. Accomplishing nothing is not
759 // an error.
760 int lfs_fs_gc(lfs_t *lfs);
761 #endif
762 
763 #ifndef LFS_READONLY
764 // Grows the filesystem to a new size, updating the superblock with the new
765 // block count.
766 //
767 // Note: This is irreversible.
768 //
769 // Returns a negative error code on failure.
770 int lfs_fs_grow(lfs_t *lfs, lfs_size_t block_count);
771 #endif
772 
773 #ifndef LFS_READONLY
774 #ifdef LFS_MIGRATE
775 // Attempts to migrate a previous version of littlefs
776 //
777 // Behaves similarly to the lfs_format function. Attempts to mount
778 // the previous version of littlefs and update the filesystem so it can be
779 // mounted with the current version of littlefs.
780 //
781 // Requires a littlefs object and config struct. This clobbers the littlefs
782 // object, and does not leave the filesystem mounted. The config struct must
783 // be zeroed for defaults and backwards compatibility.
784 //
785 // Returns a negative error code on failure.
786 int lfs_migrate(lfs_t *lfs, const struct lfs_config *cfg);
787 #endif
788 #endif
789 
790 
791 #ifdef __cplusplus
792 } /* extern "C" */
793 #endif
794 
795 #endif
796