TARFS 0.1.5
Read-only TAR filesystem for ESP32
Loading...
Searching...
No Matches
fs.h
Go to the documentation of this file.
1/*
2 * TARFS - Immutable (read-only) filesystem for embedded systems.
3 *
4 * Copyright (c) 2026 Viacheslav Logunov
5 * SPDX-License-Identifier: MMIT
6 *
7 * Author:
8 * Viacheslav Logunov <vvb333007@gmail.com>
9 *
10 * Project:
11 * https://github.com/vvb333007/tarfs
12 *
13 * @file fs.h
14 * @brief Public file system API
15 */
16
17
18#pragma once
19
20#include <stdlib.h>
21#include <stdatomic.h>
22#include <stddef.h>
23#include <errno.h>
24#include <time.h>
25
26#include "config.h"
27#include "os.h"
28
29#define TARFS_MAX_FS CONFIG_TARFS_MAX_FS
30#define TARFS_MAX_FDS CONFIG_TARFS_MAX_FDS
31
32#include "refc.h"
33#include "file.h"
34#include "inode.h"
35
36
44#define TARFS_IOCTL_BASE 0x54415200 /* "TAR\0" */
45
46#ifndef FIOGETFD
47# define FIOGETFD (TARFS_IOCTL_BASE + 0)
48#endif
49#ifndef FIONREAD
50# define FIONREAD (TARFS_IOCTL_BASE + 1)
51#endif
52#ifndef FIONBIO
53# define FIONBIO (TARFS_IOCTL_BASE + 2)
54#endif
55
59struct ioctl_req {
60 int fs_idx;
61 int fd;
62};
63
64/* Useful runtime stats on mounted filesystem. This one is required by statvfs() API
65 * unlike counters, this structure is populated once at mount and never changed
66 */
68
69 unsigned int badblocks;
70 unsigned int files;
71 unsigned int links;
72 unsigned int dirs;
73 unsigned int ram;
74#if CONFIG_TARFS_INTEGRITY
75 unsigned int badcrc;
76#endif
77};
78
79#if CONFIG_TARFS_LOG
80extern bool g_tarfs_log;
81#endif
82
83
94struct tarfs_fs {
95
96 refc_t fs_ref;
97 uintptr_t fs_handle;
98 void const *fs_vaddr;
99 size_t fs_dsize;
100 size_t fs_size;
101 uint32_t fs_nino;
102 tarfs_inode_t const * const * fs_ino;
104 _Atomic(uint32_t) fs_usedfd;
107 time_t fs_mtime;
108 uint16_t fs_opencrc:1;
109 uint16_t fs_reserved:15;
111#if CONFIG_TARFS_COUNTERS
112 uint64_t fs_bmmap;
113 uint64_t fs_bread;
114 uint32_t fs_nfail;
115#endif
117};
118
119
120#if CONFIG_TARFS_HAVE_STATVFS_H
121# include <sys/statvfs.h>
122#else
126enum {
127
136
137};
138
142struct statvfs {
143
144/* TODO: review types below. Right now it is a mess */
145
146 size_t f_bsize; /* Filesystem block size */
147 size_t f_frsize; /* Fragment size */
148 size_t f_blocks; /* Size of fs in f_frsize units */
149 size_t f_bfree; /* Number of free blocks */
150 size_t f_bavail; /* Number of free blocks for unprivileged users */
151 size_t f_files; /* Number of files */
152 size_t f_ffree; /* Number of free inodes */
153 size_t f_favail; /* Number of free inodes for unprivileged users */
154 size_t f_fsid; /* Filesystem ID */
155 int f_flag; /* Mount flags */
156 size_t f_namemax; /* Maximum filename length */
157
158/* TARFS extensions: */
159
160 uint64_t f_bread; /* Total bytes read()+pread() */
161 uint64_t f_bmmap; /* Total bytes mmap() */
162 uint32_t f_nfail; /* Total number of failures (e.g. out-of-memory, inode with NULL address etc) */
163 size_t f_badblocks;
164 size_t f_links;
165 size_t f_dirs;
166 size_t f_ram;
167 size_t f_badcrc;
168};
169#endif
170
171
172#ifdef __cplusplus
173extern "C" {
174#endif
175
207int tarfs_mount(const char *label, const char *mountpoint, const char *link_rebase, const char *path_rebase);
208
209
244int tarfs_mount_memory(const void *addr, size_t length,
245 const char *mountpoint,
246 const char *link_rebase,
247 const char *path_rebase);
253int tarfs_unmount(const char *mountpoint);
254
266unsigned int tarfs_fsck(const char *label);
267
268
285int tarfs_integrity(int en);
286
287
304
305int tarfs_integrity_on_open(int fs_idx, int en);
306
326int tarfs_fsindex(const char *path);
327
328
348int tarfs_info(const char *mp, size_t *raw_size, size_t *data_size);
349
350
363int tarfs_statvfs(void *ctx, struct statvfs *st);
364
365
366
374int tarfs_dump(int fs_idx);
375
376
377
378/******************************************************************
379 * Internal API, not to be used by user program
380 *
381 *******************************************************************/
382
383
384
392int tarfs_addref(struct tarfs_fs *fs);
393int tarfs_unref(struct tarfs_fs *fs);
394
402struct tarfs_fs *tarfs_getfs(int i);
403
408struct tarfs_fs *tarfs_getfs_addref(int i);
409
410/* Implementation of a calloc() and a strdup() via memory backend
411 *
412 */
413void *tarfs_calloc(size_t count, size_t size);
414char *tarfs_strdup(char const *str);
421static inline void tarfs_lock() { tarfs_os_acquire_mutex(); }
422static inline void tarfs_unlock() { tarfs_os_release_mutex(); }
423static inline void tarfs_init() { tarfs_os_init(); }
424
425/* Logging, used internally by the tarfs library; Must be enabled in config.h (see CONFIG_TARFS_LOG macro)
426 *
427 * log() macro does all output (printf-like)
428 * tarfs_logging() controls output of log() macro.
429 *
430 */
431#if CONFIG_TARFS_LOG
432
433#define log( Format_, ... ) do { if (g_tarfs_log) printf( "%s(): " Format_, __func__, ##__VA_ARGS__ ); } while(0)
434
435static inline void tarfs_logging(bool en) {
436
437 g_tarfs_log = en;
438 log("tarfs core logging enabled\r\n"); /* if it is disabled we don't see it */
439
440}
441#else
442
443/* No-ops when logging is not compiled in
444 */
445#define log( Format_, ... ) do {} while(0)
446#define tarfs_logging( X_ ) do {} while(0)
447
448#endif /* CONFIG_TARFS_LOG */
449
450
451#ifdef __cplusplus
452};
453#endif
int tarfs_statvfs(void *ctx, struct statvfs *st)
Obtain filesystem statistics.
Definition fs.c:686
int tarfs_fsindex(const char *path)
Find the filesystem responsible for a given path.
Definition fs.c:147
int tarfs_unref(struct tarfs_fs *fs)
Definition fs.c:260
#define log(Format_,...)
Definition fs.h:445
int tarfs_integrity_on_open(int fs_idx, int en)
Enable, disable, or query per-open filesystem integrity checking.
Definition fs.c:770
unsigned int tarfs_fsck(const char *label)
Perform a deep filesystem integrity check.
Definition fs.c:569
int tarfs_unmount(const char *mountpoint)
Unmount tar file system.
Definition fs.c:267
void * tarfs_calloc(size_t count, size_t size)
calloc() based on a memory backend; Memory backend must set errno if there were errors
Definition fs.c:492
char * tarfs_strdup(char const *str)
Definition fs.c:505
int tarfs_mount_memory(const void *addr, size_t length, const char *mountpoint, const char *link_rebase, const char *path_rebase)
Mount a TARFS filesystem from an already mapped memory buffer.
Definition fs.c:310
struct tarfs_fs * tarfs_getfs(int i)
Obtain raw pointer to the filesystem descriptor by filesystem slot (value returned by tarfs_mount()) ...
Definition fs.c:54
int tarfs_mount(const char *label, const char *mountpoint, const char *link_rebase, const char *path_rebase)
Mount a TARFS filesystem from an OS-specific resource.
Definition fs.c:449
int tarfs_addref(struct tarfs_fs *fs)
Filesystem reference counting.
Definition fs.c:253
int tarfs_dump(int fs_idx)
Dump internal filesystem information for debugging.
Definition fs.c:832
@ ST_RELATIME
Definition fs.h:134
@ ST_NODEV
Definition fs.h:129
@ ST_NOEXEC
Definition fs.h:131
@ ST_NOATIME
Definition fs.h:128
@ ST_RDONLY
Definition fs.h:133
@ ST_NOSUID
Definition fs.h:132
@ ST_NODIRATIME
Definition fs.h:130
@ ST_SYNCHRONOUS
Definition fs.h:135
int tarfs_integrity(int en)
Enables or disables CRC64 integrity verification for TARFS archives.
Definition fs.c:803
struct tarfs_fs * tarfs_getfs_addref(int i)
Obtain raw pointer to the filesystem descriptor by filesystem slot Increment FS refcounter,...
Definition fs.c:70
#define tarfs_logging(X_)
Definition fs.h:446
#define TARFS_MAX_FDS
Definition fs.h:30
int tarfs_info(const char *mp, size_t *raw_size, size_t *data_size)
Get filesystem size information.
Definition fs.c:533
void tarfs_os_release_mutex()
Unlock access to s_tarfs[] table and to the s_numfs counter only.
Definition os_esp32.c:166
void tarfs_os_init()
Create a recursive sync object.
Definition os_esp32.c:147
void tarfs_os_acquire_mutex()
Lock access to s_tarfs[] table and to the s_numfs counter only.
Definition os_esp32.c:156
Return/Request argument for FIOGETFD ioctl.
Definition fs.h:59
int fs_idx
Definition fs.h:60
int fd
Definition fs.h:61
The statvfs structure for systems without it (e.g.
Definition fs.h:142
size_t f_badcrc
Definition fs.h:167
size_t f_frsize
Definition fs.h:147
size_t f_ram
Definition fs.h:166
size_t f_dirs
Definition fs.h:165
size_t f_ffree
Definition fs.h:152
size_t f_bavail
Definition fs.h:150
uint64_t f_bmmap
Definition fs.h:161
size_t f_bsize
Definition fs.h:146
size_t f_links
Definition fs.h:164
int f_flag
Definition fs.h:155
size_t f_files
Definition fs.h:151
size_t f_namemax
Definition fs.h:156
uint32_t f_nfail
Definition fs.h:162
size_t f_badblocks
Definition fs.h:163
size_t f_fsid
Definition fs.h:154
uint64_t f_bread
Definition fs.h:160
size_t f_favail
Definition fs.h:153
size_t f_bfree
Definition fs.h:149
size_t f_blocks
Definition fs.h:148
TARFS File API: tarf_open(), tarf_close(), tarf_read(), tarf_pread(), tarf_lseek(),...
Definition file.h:63
This descriptor holds all file descriptors opened.
Definition fs.h:94
refc_t fs_ref
Definition fs.h:96
_Atomic(uint32_t) fs_usedfd
struct tarfs_fp fs_fd[16]
Definition fs.h:106
uint16_t fs_reserved
Definition fs.h:109
uintptr_t fs_handle
Definition fs.h:97
uint32_t fs_nfail
Definition fs.h:114
tarfs_inode_t const * fs_root
Definition fs.h:103
struct tarfs_stats fs_stats
Definition fs.h:110
uint64_t fs_bread
Definition fs.h:113
tarfs_inode_t const *const * fs_ino
Definition fs.h:102
uint64_t fs_bmmap
Definition fs.h:112
uint16_t fs_opencrc
Definition fs.h:108
uint32_t fs_nino
Definition fs.h:101
size_t fs_dsize
Definition fs.h:99
time_t fs_mtime
Definition fs.h:107
void const * fs_vaddr
Definition fs.h:98
char fs_mountpoint[]
Definition fs.h:116
size_t fs_size
Definition fs.h:100
TARFS inode structure, 12..20 bytes per inode; 1000 files require ~16KiB of RAM to store the filesyst...
Definition inode.h:63
unsigned int ram
Definition fs.h:73
unsigned int links
Definition fs.h:71
unsigned int dirs
Definition fs.h:72
unsigned int badblocks
Definition fs.h:69
unsigned int files
Definition fs.h:70
const char size[12]
Definition tar.h:5