vfs.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2016 Eistec AB
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
52 #include <stdint.h>
53 #include <sys/stat.h> /* for struct stat */
54 #include <sys/types.h> /* for off_t etc. */
55 #include <sys/statvfs.h> /* for struct statvfs */
56 
57 #include "sched.h"
58 #include "clist.h"
59 #include "iolist.h"
60 #include "macros/utils.h"
61 #include "mtd.h"
62 #ifdef MODULE_NANOCOAP_FS
63 #include "net/sock/config.h"
64 #endif
65 #include "xfa.h"
66 
67 #ifdef __cplusplus
68 extern "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 
80 #ifndef MAX7
81 #define MAX7(a, b, c, d, e, f, g) MAX(MAX(MAX(MAX(MAX((a), (b)), MAX((c), (d))), (e)), (f)), (g))
82 #endif
83 
88 #ifdef MODULE_FATFS_VFS
89 #include "ffconf.h"
90 
91 # if FF_FS_TINY
92 # define _FATFS_FILE_CACHE (0)
93 # else
94 # define _FATFS_FILE_CACHE FF_MAX_SS
95 # endif
96 
97 # if FF_USE_FASTSEEK
98 # if (__SIZEOF_POINTER__ == 8)
99 # define _FATFS_FILE_SEEK_PTR (8)
100 # else
101 # define _FATFS_FILE_SEEK_PTR (4)
102 # endif
103 # else
104 # define _FATFS_FILE_SEEK_PTR (0)
105 # endif
106 
107 # if FF_FS_EXFAT
108 # define _FATFS_FILE_EXFAT (48)
109 # define _FATFS_DIR_EXFAT (32)
110 # else
111 # define _FATFS_FILE_EXFAT (0)
112 # define _FATFS_DIR_EXFAT (0)
113 # endif
114 
115 # if FF_USE_LFN
116 # if (__SIZEOF_POINTER__ == 8)
117 # define _FATFS_DIR_LFN (8)
118 # else
119 # define _FATFS_DIR_LFN (4)
120 # endif
121 # else
122 # define _FATFS_DIR_LFN (0)
123 # endif
124 
125 # if (__SIZEOF_POINTER__ == 8)
126 # define FATFS_VFS_DIR_BUFFER_SIZE (64 + _FATFS_DIR_LFN + _FATFS_DIR_EXFAT)
127 # define FATFS_VFS_FILE_BUFFER_SIZE (64 + VFS_NAME_MAX + _FATFS_FILE_CACHE + \
128  _FATFS_FILE_SEEK_PTR + _FATFS_FILE_EXFAT)
129 # else
130 # define FATFS_VFS_DIR_BUFFER_SIZE (44 + _FATFS_DIR_LFN + _FATFS_DIR_EXFAT)
131 # define FATFS_VFS_FILE_BUFFER_SIZE (44 + VFS_NAME_MAX + _FATFS_FILE_CACHE + \
132  _FATFS_FILE_SEEK_PTR + _FATFS_FILE_EXFAT)
133 # endif
134 #else
135 # define FATFS_VFS_DIR_BUFFER_SIZE (1)
136 # define FATFS_VFS_FILE_BUFFER_SIZE (1)
137 #endif
144 #ifdef MODULE_LITTLEFS
145 # if (__SIZEOF_POINTER__ == 8)
146 # define LITTLEFS_VFS_DIR_BUFFER_SIZE (48)
147 # define LITTLEFS_VFS_FILE_BUFFER_SIZE (72)
148 # else
149 # define LITTLEFS_VFS_DIR_BUFFER_SIZE (44)
150 # define LITTLEFS_VFS_FILE_BUFFER_SIZE (56)
151 # endif
152 #else
153 # define LITTLEFS_VFS_DIR_BUFFER_SIZE (1)
154 # define LITTLEFS_VFS_FILE_BUFFER_SIZE (1)
155 #endif
162 #ifdef MODULE_LITTLEFS2
163 # if (__SIZEOF_POINTER__ == 8)
164 # define LITTLEFS2_VFS_DIR_BUFFER_SIZE (56)
165 # define LITTLEFS2_VFS_FILE_BUFFER_SIZE (104)
166 # else
167 # define LITTLEFS2_VFS_DIR_BUFFER_SIZE (52)
168 # define LITTLEFS2_VFS_FILE_BUFFER_SIZE (84)
169 # endif
170 #else
171 # define LITTLEFS2_VFS_DIR_BUFFER_SIZE (1)
172 # define LITTLEFS2_VFS_FILE_BUFFER_SIZE (1)
173 #endif
180 #ifdef MODULE_SPIFFS
181 # define SPIFFS_VFS_DIR_BUFFER_SIZE (12)
182 # define SPIFFS_VFS_FILE_BUFFER_SIZE (1)
183 #else
184 # define SPIFFS_VFS_DIR_BUFFER_SIZE (1)
185 # define SPIFFS_VFS_FILE_BUFFER_SIZE (1)
186 #endif
193 #if defined(MODULE_LWEXT4) || DOXYGEN
194 # define LWEXT4_VFS_DIR_BUFFER_SIZE (308)
195 # define LWEXT4_VFS_FILE_BUFFER_SIZE (32)
196 #else
197 # define LWEXT4_VFS_DIR_BUFFER_SIZE (1)
198 # define LWEXT4_VFS_FILE_BUFFER_SIZE (1)
199 #endif
206 #if defined(MODULE_NANOCOAP_FS) || DOXYGEN
207 # define NANOCOAP_FS_VFS_DIR_BUFFER_SIZE \
208  (4 + CONFIG_SOCK_URLPATH_MAXLEN)
209 # define NANOCOAP_FS_VFS_FILE_BUFFER_SIZE \
210  (4 + CONFIG_SOCK_URLPATH_MAXLEN)
211 #else
212 # define NANOCOAP_FS_VFS_DIR_BUFFER_SIZE (1)
213 # define NANOCOAP_FS_VFS_FILE_BUFFER_SIZE (1)
214 #endif
221 #if defined(MODULE_XIPFS) || DOXYGEN
222 # define XIPFS_VFS_DIR_BUFFER_SIZE (68)
223 # define XIPFS_VFS_FILE_BUFFER_SIZE (12)
224 #else
225 # define XIPFS_VFS_DIR_BUFFER_SIZE (1)
226 # define XIPFS_VFS_FILE_BUFFER_SIZE (1)
227 #endif
230 #ifndef VFS_MAX_OPEN_FILES
234 #define VFS_MAX_OPEN_FILES (16)
235 #endif
236 
237 #ifndef VFS_DIR_BUFFER_SIZE
265 #define VFS_DIR_BUFFER_SIZE MAX7(FATFS_VFS_DIR_BUFFER_SIZE, \
266  LITTLEFS_VFS_DIR_BUFFER_SIZE, \
267  LITTLEFS2_VFS_DIR_BUFFER_SIZE, \
268  SPIFFS_VFS_DIR_BUFFER_SIZE, \
269  LWEXT4_VFS_DIR_BUFFER_SIZE, \
270  NANOCOAP_FS_VFS_DIR_BUFFER_SIZE, \
271  XIPFS_VFS_DIR_BUFFER_SIZE \
272  )
273 #endif
274 
275 #ifndef VFS_FILE_BUFFER_SIZE
295 #define VFS_FILE_BUFFER_SIZE MAX7(FATFS_VFS_FILE_BUFFER_SIZE, \
296  LITTLEFS_VFS_FILE_BUFFER_SIZE, \
297  LITTLEFS2_VFS_FILE_BUFFER_SIZE, \
298  SPIFFS_VFS_FILE_BUFFER_SIZE, \
299  LWEXT4_VFS_FILE_BUFFER_SIZE, \
300  NANOCOAP_FS_VFS_FILE_BUFFER_SIZE, \
301  XIPFS_VFS_FILE_BUFFER_SIZE \
302  )
303 #endif
304 
305 #ifndef VFS_NAME_MAX
313 #define VFS_NAME_MAX (31)
314 #endif
315 
319 #define VFS_ANY_FD (-1)
320 
326 #define VFS_MTD(mtd) { .dev = &mtd.base }
327 
341 #define VFS_AUTO_MOUNT(type, mtd, path, idx) \
342  static type ## _desc_t fs_desc_ ## idx = mtd; \
343  \
344  XFA(vfs_mount_t, vfs_mountpoints_xfa, 0) \
345  _mount_mtd_ ## idx = { \
346  .fs = &type ## _file_system, \
347  .mount_point = path, \
348  .private_data = &fs_desc_ ## idx, \
349  }
350 
351 /* Forward declarations */
355 typedef struct vfs_file_ops vfs_file_ops_t;
356 
360 typedef struct vfs_dir_ops vfs_dir_ops_t;
361 
366 
370 /* not struct vfs_mount because of name collision with the function */
371 typedef struct vfs_mount_struct vfs_mount_t;
372 
376 extern const vfs_file_ops_t mtd_vfs_ops;
377 
381 #define VFS_FS_FLAG_WANT_ABS_PATH (1 << 0)
382 
386 typedef struct {
390  const uint32_t flags;
392 
399  const char *mount_point;
401  uint16_t open_files;
402  void *private_data;
403 };
404 
410 typedef struct {
413  int flags;
414  off_t pos;
416  union {
417  void *ptr;
418  int value;
419  uint8_t buffer[VFS_FILE_BUFFER_SIZE];
420  } private_data;
421 } vfs_file_t;
422 
431 typedef struct {
434  union {
435  void *ptr;
436  int value;
437  uint8_t buffer[VFS_DIR_BUFFER_SIZE];
438  } private_data;
439 } vfs_DIR;
440 
449 typedef struct {
450  ino_t d_ino;
451  char d_name[VFS_NAME_MAX + 1];
452 } vfs_dirent_t;
453 
459 struct vfs_file_ops {
478  int (*close) (vfs_file_t *filp);
479 
490  int (*fcntl) (vfs_file_t *filp, int cmd, int arg);
491 
501  int (*fstat) (vfs_file_t *filp, struct stat *buf);
502 
520  off_t (*lseek) (vfs_file_t *filp, off_t off, int whence);
521 
548  int (*open) (vfs_file_t *filp, const char *name, int flags, mode_t mode);
549 
560  ssize_t (*read) (vfs_file_t *filp, void *dest, size_t nbytes);
561 
572  ssize_t (*write) (vfs_file_t *filp, const void *src, size_t nbytes);
573 
583  int (*fsync) (vfs_file_t *filp);
584 };
585 
589 struct vfs_dir_ops {
599  int (*opendir) (vfs_DIR *dirp, const char *dirname);
600 
620  int (*readdir) (vfs_DIR *dirp, vfs_dirent_t *entry);
621 
630  int (*closedir) (vfs_DIR *dirp);
631 };
632 
647  int (*format) (vfs_mount_t *mountp);
648 
663  int (*mount) (vfs_mount_t *mountp);
664 
673  int (*umount) (vfs_mount_t *mountp);
674 
689  int (*rename) (vfs_mount_t *mountp, const char *from_path, const char *to_path);
690 
700  int (*unlink) (vfs_mount_t *mountp, const char *name);
701 
712  int (*mkdir) (vfs_mount_t *mountp, const char *name, mode_t mode);
713 
725  int (*rmdir) (vfs_mount_t *mountp, const char *name);
726 
737  int (*stat) (vfs_mount_t *mountp, const char *restrict path, struct stat *restrict buf);
738 
754  int (*statvfs) (vfs_mount_t *mountp, const char *restrict path, struct statvfs *restrict buf);
755 };
756 
764 void vfs_bind_stdio(void);
765 
774 int vfs_close(int fd);
775 
786 int vfs_fcntl(int fd, int cmd, int arg);
787 
797 int vfs_fstat(int fd, struct stat *buf);
798 
808 int vfs_fstatvfs(int fd, struct statvfs *buf);
809 
819 int vfs_dstatvfs(vfs_DIR *dirp, struct statvfs *buf);
820 
838 off_t vfs_lseek(int fd, off_t off, int whence);
839 
850 int vfs_open(const char *name, int flags, mode_t mode);
851 
865 ssize_t vfs_read(int fd, void *dest, size_t count);
866 
879 ssize_t vfs_readline(int fd, char *dest, size_t count);
880 
894 ssize_t vfs_write(int fd, const void *src, size_t count);
895 
905 ssize_t vfs_write_iol(int fd, const iolist_t *iolist);
906 
916 int vfs_fsync(int fd);
917 
929 int vfs_opendir(vfs_DIR *dirp, const char *dirname);
930 
948 int vfs_readdir(vfs_DIR *dirp, vfs_dirent_t *entry);
949 
962 
976 int vfs_format(vfs_mount_t *mountp);
977 
991 int vfs_format_by_path(const char *path);
992 
1004 int vfs_mount(vfs_mount_t *mountp);
1005 
1019 int vfs_mount_by_path(const char *path);
1020 
1032 int vfs_unmount_by_path(const char *path, bool force);
1033 
1047 int vfs_rename(const char *from_path, const char *to_path);
1048 
1060 int vfs_umount(vfs_mount_t *mountp, bool force);
1061 
1070 int vfs_unlink(const char *name);
1071 
1081 int vfs_mkdir(const char *name, mode_t mode);
1082 
1093 int vfs_rmdir(const char *name);
1094 
1104 int vfs_stat(const char *restrict path, struct stat *restrict buf);
1105 
1118 int vfs_statvfs(const char *restrict path, struct statvfs *restrict buf);
1119 
1139 int vfs_bind(int fd, int flags, const vfs_file_ops_t *f_op, void *private_data);
1140 
1158 int vfs_normalize_path(char *buf, const char *path, size_t buflen);
1159 
1190 
1204 const vfs_file_t *vfs_file_get(int fd);
1205 
1217  const char *restrict path,
1218  struct stat *restrict buf);
1219 
1220 #ifdef __cplusplus
1221 }
1222 #endif
1223 
Circular linked list.
Various helper macros.
int16_t kernel_pid_t
Unique process identifier.
Definition: sched.h:135
#define VFS_DIR_BUFFER_SIZE
Size of buffer space in vfs_DIR.
Definition: vfs.h:265
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:295
int vfs_sysop_stat_from_fstat(vfs_mount_t *mountp, const char *restrict path, struct stat *restrict buf)
Implementation of stat using fstat
#define VFS_NAME_MAX
Maximum length of the name in a vfs_dirent_t (not including terminating null)
Definition: vfs.h:313
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.
ssize_t vfs_readline(int fd, char *dest, size_t count)
Read a line from an open text file.
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:35
List node structure.
Definition: list.h:36
File system information.
Definition: statvfs.h:46
Internal representation of a file system directory entry.
Definition: vfs.h:431
const vfs_dir_ops_t * d_op
Directory operations table.
Definition: vfs.h:432
int value
alternatively, you can use private_data as an int
Definition: vfs.h:436
vfs_mount_t * mp
Pointer to mount table entry.
Definition: vfs.h:433
void * ptr
pointer to private data
Definition: vfs.h:435
Operations on open directories.
Definition: vfs.h:589
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:620
int(* opendir)(vfs_DIR *dirp, const char *dirname)
Open a directory for reading with readdir.
Definition: vfs.h:599
int(* closedir)(vfs_DIR *dirp)
Close an open directory.
Definition: vfs.h:630
User facing directory entry.
Definition: vfs.h:449
ino_t d_ino
file serial number, unique for the file system ("inode" in Linux)
Definition: vfs.h:450
Operations on open files.
Definition: vfs.h:459
int(* fcntl)(vfs_file_t *filp, int cmd, int arg)
Query/set options on an open file.
Definition: vfs.h:490
int(* close)(vfs_file_t *filp)
Close an open file.
Definition: vfs.h:478
int(* fstat)(vfs_file_t *filp, struct stat *buf)
Get status of an open file.
Definition: vfs.h:501
int(* fsync)(vfs_file_t *filp)
Synchronize a file on storage Any pending writes are written out to storage.
Definition: vfs.h:583
off_t(* lseek)(vfs_file_t *filp, off_t off, int whence)
Seek to position in file.
Definition: vfs.h:520
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:548
ssize_t(* read)(vfs_file_t *filp, void *dest, size_t nbytes)
Read bytes from an open file.
Definition: vfs.h:560
ssize_t(* write)(vfs_file_t *filp, const void *src, size_t nbytes)
Write bytes to an open file.
Definition: vfs.h:572
Operations on mounted file systems.
Definition: vfs.h:638
int(* mount)(vfs_mount_t *mountp)
Perform any extra processing needed after mounting a file system.
Definition: vfs.h:663
int(* unlink)(vfs_mount_t *mountp, const char *name)
Unlink (delete) a file from the file system.
Definition: vfs.h:700
int(* rename)(vfs_mount_t *mountp, const char *from_path, const char *to_path)
Rename a file.
Definition: vfs.h:689
int(* stat)(vfs_mount_t *mountp, const char *restrict path, struct stat *restrict buf)
Get file status.
Definition: vfs.h:737
int(* mkdir)(vfs_mount_t *mountp, const char *name, mode_t mode)
Create a directory on the file system.
Definition: vfs.h:712
int(* format)(vfs_mount_t *mountp)
Format the file system on the given mount point.
Definition: vfs.h:647
int(* rmdir)(vfs_mount_t *mountp, const char *name)
Remove a directory from the file system.
Definition: vfs.h:725
int(* umount)(vfs_mount_t *mountp)
Perform the necessary clean up for unmounting a file system.
Definition: vfs.h:673
A file system driver.
Definition: vfs.h:386
const vfs_file_system_ops_t * fs_op
File system operations table.
Definition: vfs.h:389
const vfs_dir_ops_t * d_op
Directory operations table.
Definition: vfs.h:388
const vfs_file_ops_t * f_op
File operations table.
Definition: vfs.h:387
const uint32_t flags
File system flags.
Definition: vfs.h:390
Information about an open file.
Definition: vfs.h:410
void * ptr
pointer to private data
Definition: vfs.h:417
vfs_mount_t * mp
Pointer to mount table entry.
Definition: vfs.h:412
kernel_pid_t pid
PID of the process that opened the file.
Definition: vfs.h:415
int value
alternatively, you can use private_data as an int
Definition: vfs.h:418
int flags
File flags.
Definition: vfs.h:413
const vfs_file_ops_t * f_op
File operations table.
Definition: vfs.h:411
off_t pos
Current position in the file.
Definition: vfs.h:414
A mounted file system.
Definition: vfs.h:396
const vfs_file_system_t * fs
The file system driver for the mount point.
Definition: vfs.h:398
clist_node_t list_entry
List entry for the _vfs_mount_list list.
Definition: vfs.h:397
size_t mount_point_len
Length of mount_point string (set by vfs_mount)
Definition: vfs.h:400
uint16_t open_files
Number of currently open files and directories.
Definition: vfs.h:401
void * private_data
File system driver private data, implementation defined.
Definition: vfs.h:402
const char * mount_point
Mount point, e.g.
Definition: vfs.h:399
Cross File Arrays.