Loading...
Searching...
No Matches
vfs.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016 Eistec AB
3 *
4 * This file is subject to the terms and conditions of the GNU Lesser
5 * General Public License v2.1. See the file LICENSE in the top level
6 * directory for more details.
7 */
8
53#ifndef VFS_H
54#define VFS_H
55
56#include <stdint.h>
57#include <sys/stat.h> /* for struct stat */
58#include <sys/types.h> /* for off_t etc. */
59#include <sys/statvfs.h> /* for struct statvfs */
60
61#include "sched.h"
62#include "clist.h"
63#include "iolist.h"
64#include "mtd.h"
65#include "xfa.h"
66
67#ifdef __cplusplus
68extern "C" {
69/* restrict is a C99 keyword, not valid in C++, but GCC and Clang have the
70 * __restrict__ extension keyword which can be used instead */
71#define restrict __restrict__
72/* If the above is not supported by the compiler, you can replace it with an
73 * empty definition instead: */
74/* #define restrict */
75#endif
76
81#ifndef _MAX
82#define _MAX(a, b) ((a) > (b) ? (a) : (b))
83#endif
84
85#ifndef MAX5
89#define MAX5(a, b, c, d, e) _MAX(_MAX(_MAX((a), (b)), _MAX((c),(d))), (e))
90#endif
97#ifdef MODULE_FATFS_VFS
98#include "ffconf.h"
99
100# if FF_FS_TINY
101# define _FATFS_FILE_CACHE (0)
102# else
103# define _FATFS_FILE_CACHE FF_MAX_SS
104# endif
105
106# if FF_USE_FASTSEEK
107# if (__SIZEOF_POINTER__ == 8)
108# define _FATFS_FILE_SEEK_PTR (8)
109# else
110# define _FATFS_FILE_SEEK_PTR (4)
111# endif
112# else
113# define _FATFS_FILE_SEEK_PTR (0)
114# endif
115
116# if FF_FS_EXFAT
117# define _FATFS_FILE_EXFAT (48)
118# define _FATFS_DIR_EXFAT (32)
119# else
120# define _FATFS_FILE_EXFAT (0)
121# define _FATFS_DIR_EXFAT (0)
122# endif
123
124# if FF_USE_LFN
125# if (__SIZEOF_POINTER__ == 8)
126# define _FATFS_DIR_LFN (8)
127# else
128# define _FATFS_DIR_LFN (4)
129# endif
130# else
131# define _FATFS_DIR_LFN (0)
132# endif
133
134# if (__SIZEOF_POINTER__ == 8)
135# define FATFS_VFS_DIR_BUFFER_SIZE (64 + _FATFS_DIR_LFN + _FATFS_DIR_EXFAT)
136# define FATFS_VFS_FILE_BUFFER_SIZE (64 + VFS_NAME_MAX + _FATFS_FILE_CACHE + \
137 _FATFS_FILE_SEEK_PTR + _FATFS_FILE_EXFAT)
138# else
139# define FATFS_VFS_DIR_BUFFER_SIZE (44 + _FATFS_DIR_LFN + _FATFS_DIR_EXFAT)
140# define FATFS_VFS_FILE_BUFFER_SIZE (44 + VFS_NAME_MAX + _FATFS_FILE_CACHE + \
141 _FATFS_FILE_SEEK_PTR + _FATFS_FILE_EXFAT)
142# endif
143#else
144# define FATFS_VFS_DIR_BUFFER_SIZE (1)
145# define FATFS_VFS_FILE_BUFFER_SIZE (1)
146#endif
153#ifdef MODULE_LITTLEFS
154# if (__SIZEOF_POINTER__ == 8)
155# define LITTLEFS_VFS_DIR_BUFFER_SIZE (48)
156# define LITTLEFS_VFS_FILE_BUFFER_SIZE (72)
157# else
158# define LITTLEFS_VFS_DIR_BUFFER_SIZE (44)
159# define LITTLEFS_VFS_FILE_BUFFER_SIZE (56)
160# endif
161#else
162# define LITTLEFS_VFS_DIR_BUFFER_SIZE (1)
163# define LITTLEFS_VFS_FILE_BUFFER_SIZE (1)
164#endif
171#ifdef MODULE_LITTLEFS2
172# if (__SIZEOF_POINTER__ == 8)
173# define LITTLEFS2_VFS_DIR_BUFFER_SIZE (56)
174# define LITTLEFS2_VFS_FILE_BUFFER_SIZE (104)
175# else
176# define LITTLEFS2_VFS_DIR_BUFFER_SIZE (52)
177# define LITTLEFS2_VFS_FILE_BUFFER_SIZE (84)
178# endif
179#else
180# define LITTLEFS2_VFS_DIR_BUFFER_SIZE (1)
181# define LITTLEFS2_VFS_FILE_BUFFER_SIZE (1)
182#endif
189#ifdef MODULE_SPIFFS
190# define SPIFFS_VFS_DIR_BUFFER_SIZE (12)
191# define SPIFFS_VFS_FILE_BUFFER_SIZE (1)
192#else
193# define SPIFFS_VFS_DIR_BUFFER_SIZE (1)
194# define SPIFFS_VFS_FILE_BUFFER_SIZE (1)
195#endif
202#if defined(MODULE_LWEXT4) || DOXYGEN
203# define LWEXT4_VFS_DIR_BUFFER_SIZE (308)
204# define LWEXT4_VFS_FILE_BUFFER_SIZE (32)
205#else
206# define LWEXT4_VFS_DIR_BUFFER_SIZE (1)
207# define LWEXT4_VFS_FILE_BUFFER_SIZE (1)
208#endif
211#ifndef VFS_MAX_OPEN_FILES
215#define VFS_MAX_OPEN_FILES (16)
216#endif
217
218#ifndef VFS_DIR_BUFFER_SIZE
246#define VFS_DIR_BUFFER_SIZE MAX5(FATFS_VFS_DIR_BUFFER_SIZE, \
247 LITTLEFS_VFS_DIR_BUFFER_SIZE, \
248 LITTLEFS2_VFS_DIR_BUFFER_SIZE, \
249 SPIFFS_VFS_DIR_BUFFER_SIZE, \
250 LWEXT4_VFS_DIR_BUFFER_SIZE \
251 )
252#endif
253
254#ifndef VFS_FILE_BUFFER_SIZE
274#define VFS_FILE_BUFFER_SIZE MAX5(FATFS_VFS_FILE_BUFFER_SIZE, \
275 LITTLEFS_VFS_FILE_BUFFER_SIZE, \
276 LITTLEFS2_VFS_FILE_BUFFER_SIZE,\
277 SPIFFS_VFS_FILE_BUFFER_SIZE, \
278 LWEXT4_VFS_FILE_BUFFER_SIZE \
279 )
280#endif
281
282#ifndef VFS_NAME_MAX
290#define VFS_NAME_MAX (31)
291#endif
292
296#define VFS_ANY_FD (-1)
297
303#define VFS_MTD(mtd) { .dev = &mtd.base }
304
318#define VFS_AUTO_MOUNT(type, mtd, path, idx) \
319 static type ## _desc_t fs_desc_ ## idx = mtd; \
320 \
321 XFA(vfs_mountpoints_xfa, 0) \
322 vfs_mount_t _mount_mtd_ ## idx = { \
323 .fs = &type ## _file_system, \
324 .mount_point = path, \
325 .private_data = &fs_desc_ ## idx, \
326 }
327
328/* Forward declarations */
333
338
343
347/* not struct vfs_mount because of name collision with the function */
349
353extern const vfs_file_ops_t mtd_vfs_ops;
354
358#define VFS_FS_FLAG_WANT_ABS_PATH (1 << 0)
359
363typedef struct {
367 const uint32_t flags;
369
381
387typedef struct {
390 int flags;
391 off_t pos;
393 union {
394 void *ptr;
395 int value;
396 uint8_t buffer[VFS_FILE_BUFFER_SIZE];
397 } private_data;
398} vfs_file_t;
399
408typedef struct {
411 union {
412 void *ptr;
413 int value;
414 uint8_t buffer[VFS_DIR_BUFFER_SIZE];
415 } private_data;
416} vfs_DIR;
417
426typedef struct {
427 ino_t d_ino;
428 char d_name[VFS_NAME_MAX + 1];
430
455 int (*close) (vfs_file_t *filp);
456
467 int (*fcntl) (vfs_file_t *filp, int cmd, int arg);
468
478 int (*fstat) (vfs_file_t *filp, struct stat *buf);
479
497 off_t (*lseek) (vfs_file_t *filp, off_t off, int whence);
498
525 int (*open) (vfs_file_t *filp, const char *name, int flags, mode_t mode);
526
537 ssize_t (*read) (vfs_file_t *filp, void *dest, size_t nbytes);
538
549 ssize_t (*write) (vfs_file_t *filp, const void *src, size_t nbytes);
550
560 int (*fsync) (vfs_file_t *filp);
561};
562
576 int (*opendir) (vfs_DIR *dirp, const char *dirname);
577
597 int (*readdir) (vfs_DIR *dirp, vfs_dirent_t *entry);
598
607 int (*closedir) (vfs_DIR *dirp);
608};
609
624 int (*format) (vfs_mount_t *mountp);
625
640 int (*mount) (vfs_mount_t *mountp);
641
650 int (*umount) (vfs_mount_t *mountp);
651
666 int (*rename) (vfs_mount_t *mountp, const char *from_path, const char *to_path);
667
677 int (*unlink) (vfs_mount_t *mountp, const char *name);
678
689 int (*mkdir) (vfs_mount_t *mountp, const char *name, mode_t mode);
690
702 int (*rmdir) (vfs_mount_t *mountp, const char *name);
703
714 int (*stat) (vfs_mount_t *mountp, const char *restrict path, struct stat *restrict buf);
715
731 int (*statvfs) (vfs_mount_t *mountp, const char *restrict path, struct statvfs *restrict buf);
732};
733
741void vfs_bind_stdio(void);
742
751int vfs_close(int fd);
752
763int vfs_fcntl(int fd, int cmd, int arg);
764
774int vfs_fstat(int fd, struct stat *buf);
775
785int vfs_fstatvfs(int fd, struct statvfs *buf);
786
796int vfs_dstatvfs(vfs_DIR *dirp, struct statvfs *buf);
797
815off_t vfs_lseek(int fd, off_t off, int whence);
816
827int vfs_open(const char *name, int flags, mode_t mode);
828
842ssize_t vfs_read(int fd, void *dest, size_t count);
843
857ssize_t vfs_write(int fd, const void *src, size_t count);
858
868ssize_t vfs_write_iol(int fd, const iolist_t *iolist);
869
879int vfs_fsync(int fd);
880
892int vfs_opendir(vfs_DIR *dirp, const char *dirname);
893
912
925
940
954int vfs_format_by_path(const char *path);
955
968
982int vfs_mount_by_path(const char *path);
983
995int vfs_unmount_by_path(const char *path, bool force);
996
1010int vfs_rename(const char *from_path, const char *to_path);
1011
1023int vfs_umount(vfs_mount_t *mountp, bool force);
1024
1033int vfs_unlink(const char *name);
1034
1044int vfs_mkdir(const char *name, mode_t mode);
1045
1056int vfs_rmdir(const char *name);
1057
1067int vfs_stat(const char *restrict path, struct stat *restrict buf);
1068
1081int vfs_statvfs(const char *restrict path, struct statvfs *restrict buf);
1082
1102int vfs_bind(int fd, int flags, const vfs_file_ops_t *f_op, void *private_data);
1103
1121int vfs_normalize_path(char *buf, const char *path, size_t buflen);
1122
1140
1171
1186
1198 const char *restrict path,
1199 struct stat *restrict buf);
1200
1201#ifdef __cplusplus
1202}
1203#endif
1204
1205#endif /* VFS_H */
1206
Circular linked list.
int16_t kernel_pid_t
Unique process identifier.
Definition sched.h:139
#define VFS_DIR_BUFFER_SIZE
Size of buffer space in vfs_DIR.
Definition vfs.h:246
int vfs_mount(vfs_mount_t *mountp)
Mount a file system.
int vfs_normalize_path(char *buf, const char *path, size_t buflen)
Normalize a path.
int vfs_open(const char *name, int flags, mode_t mode)
Open a file.
int vfs_statvfs(const char *restrict path, struct statvfs *restrict buf)
Get file system status.
ssize_t vfs_write(int fd, const void *src, size_t count)
Write bytes to an open file.
int vfs_mkdir(const char *name, mode_t mode)
Create a directory on the file system.
int vfs_umount(vfs_mount_t *mountp, bool force)
Unmount a mounted file system.
int vfs_close(int fd)
Close an open file.
#define VFS_FILE_BUFFER_SIZE
Size of buffer space in vfs_file_t.
Definition vfs.h:274
int vfs_sysop_stat_from_fstat(vfs_mount_t *mountp, const char *restrict path, struct stat *restrict buf)
Implementation of stat using fstat
const vfs_mount_t * vfs_iterate_mounts(const vfs_mount_t *cur)
Iterate through all mounted file systems.
#define VFS_NAME_MAX
Maximum length of the name in a vfs_dirent_t (not including terminating null)
Definition vfs.h:290
int vfs_readdir(vfs_DIR *dirp, vfs_dirent_t *entry)
Read a single entry from the open directory dirp and advance the read position by one.
off_t vfs_lseek(int fd, off_t off, int whence)
Seek to position in file.
int vfs_format(vfs_mount_t *mountp)
Format a file system.
int vfs_bind(int fd, int flags, const vfs_file_ops_t *f_op, void *private_data)
Allocate a new file descriptor and give it file operations.
int vfs_format_by_path(const char *path)
Format a file system.
int vfs_rename(const char *from_path, const char *to_path)
Rename a file.
ssize_t vfs_write_iol(int fd, const iolist_t *iolist)
Write bytes from an iolist to an open file.
int vfs_stat(const char *restrict path, struct stat *restrict buf)
Get file status.
void vfs_bind_stdio(void)
Allocate and bind file descriptors for STDIN, STDERR, and STDOUT.
const vfs_file_t * vfs_file_get(int fd)
Get information about the file for internal purposes.
int vfs_unlink(const char *name)
Unlink (delete) a file from a mounted file system.
int vfs_fsync(int fd)
Synchronize a file on storage Any pending writes are written out to storage.
int vfs_opendir(vfs_DIR *dirp, const char *dirname)
Open a directory for reading with readdir.
const vfs_file_ops_t mtd_vfs_ops
MTD driver for VFS.
int vfs_dstatvfs(vfs_DIR *dirp, struct statvfs *buf)
Get file system status of the file system containing an open directory.
int vfs_rmdir(const char *name)
Remove a directory from the file system.
int vfs_fcntl(int fd, int cmd, int arg)
Query/set options on an open file.
int vfs_fstat(int fd, struct stat *buf)
Get status of an open file.
ssize_t vfs_read(int fd, void *dest, size_t count)
Read bytes from an open file.
bool vfs_iterate_mount_dirs(vfs_DIR *dir)
Iterate through all mounted file systems by their root directories.
int vfs_closedir(vfs_DIR *dirp)
Close an open directory.
int vfs_fstatvfs(int fd, struct statvfs *buf)
Get file system status of the file system containing an open file.
int vfs_mount_by_path(const char *path)
Mount a file system with a pre-configured mount path.
int vfs_unmount_by_path(const char *path, bool force)
Unmount a file system with a pre-configured mount path.
iolist scatter / gather IO
Scheduler API definition.
POSIX compatible sys/statvfs.h definitions.
iolist structure definition
Definition iolist.h:39
List node structure.
Definition list.h:40
File system information.
Definition statvfs.h:46
Internal representation of a file system directory entry.
Definition vfs.h:408
const vfs_dir_ops_t * d_op
Directory operations table.
Definition vfs.h:409
int value
alternatively, you can use private_data as an int
Definition vfs.h:413
vfs_mount_t * mp
Pointer to mount table entry.
Definition vfs.h:410
void * ptr
pointer to private data
Definition vfs.h:412
Operations on open directories.
Definition vfs.h:566
int(* readdir)(vfs_DIR *dirp, vfs_dirent_t *entry)
Read a single entry from the open directory dirp and advance the read position by one.
Definition vfs.h:597
int(* opendir)(vfs_DIR *dirp, const char *dirname)
Open a directory for reading with readdir.
Definition vfs.h:576
int(* closedir)(vfs_DIR *dirp)
Close an open directory.
Definition vfs.h:607
User facing directory entry.
Definition vfs.h:426
ino_t d_ino
file serial number, unique for the file system ("inode" in Linux)
Definition vfs.h:427
Operations on open files.
Definition vfs.h:436
int(* fcntl)(vfs_file_t *filp, int cmd, int arg)
Query/set options on an open file.
Definition vfs.h:467
int(* close)(vfs_file_t *filp)
Close an open file.
Definition vfs.h:455
int(* fstat)(vfs_file_t *filp, struct stat *buf)
Get status of an open file.
Definition vfs.h:478
int(* fsync)(vfs_file_t *filp)
Synchronize a file on storage Any pending writes are written out to storage.
Definition vfs.h:560
off_t(* lseek)(vfs_file_t *filp, off_t off, int whence)
Seek to position in file.
Definition vfs.h:497
int(* open)(vfs_file_t *filp, const char *name, int flags, mode_t mode)
Attempt to open a file in the file system at rel_path.
Definition vfs.h:525
ssize_t(* read)(vfs_file_t *filp, void *dest, size_t nbytes)
Read bytes from an open file.
Definition vfs.h:537
ssize_t(* write)(vfs_file_t *filp, const void *src, size_t nbytes)
Write bytes to an open file.
Definition vfs.h:549
Operations on mounted file systems.
Definition vfs.h:615
int(* mount)(vfs_mount_t *mountp)
Perform any extra processing needed after mounting a file system.
Definition vfs.h:640
int(* unlink)(vfs_mount_t *mountp, const char *name)
Unlink (delete) a file from the file system.
Definition vfs.h:677
int(* rename)(vfs_mount_t *mountp, const char *from_path, const char *to_path)
Rename a file.
Definition vfs.h:666
int(* stat)(vfs_mount_t *mountp, const char *restrict path, struct stat *restrict buf)
Get file status.
Definition vfs.h:714
int(* mkdir)(vfs_mount_t *mountp, const char *name, mode_t mode)
Create a directory on the file system.
Definition vfs.h:689
int(* format)(vfs_mount_t *mountp)
Format the file system on the given mount point.
Definition vfs.h:624
int(* rmdir)(vfs_mount_t *mountp, const char *name)
Remove a directory from the file system.
Definition vfs.h:702
int(* umount)(vfs_mount_t *mountp)
Perform the necessary clean up for unmounting a file system.
Definition vfs.h:650
A file system driver.
Definition vfs.h:363
const vfs_file_system_ops_t * fs_op
File system operations table.
Definition vfs.h:366
const vfs_dir_ops_t * d_op
Directory operations table.
Definition vfs.h:365
const vfs_file_ops_t * f_op
File operations table.
Definition vfs.h:364
const uint32_t flags
File system flags.
Definition vfs.h:367
Information about an open file.
Definition vfs.h:387
void * ptr
pointer to private data
Definition vfs.h:394
vfs_mount_t * mp
Pointer to mount table entry.
Definition vfs.h:389
kernel_pid_t pid
PID of the process that opened the file.
Definition vfs.h:392
int value
alternatively, you can use private_data as an int
Definition vfs.h:395
int flags
File flags.
Definition vfs.h:390
const vfs_file_ops_t * f_op
File operations table.
Definition vfs.h:388
off_t pos
Current position in the file.
Definition vfs.h:391
A mounted file system.
Definition vfs.h:373
const vfs_file_system_t * fs
The file system driver for the mount point.
Definition vfs.h:375
clist_node_t list_entry
List entry for the _vfs_mount_list list.
Definition vfs.h:374
size_t mount_point_len
Length of mount_point string (set by vfs_mount)
Definition vfs.h:377
uint16_t open_files
Number of currently open files and directories.
Definition vfs.h:378
void * private_data
File system driver private data, implementation defined.
Definition vfs.h:379
const char * mount_point
Mount point, e.g.
Definition vfs.h:376
Cross File Arrays.