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 /* The stdatomic.h in GCC gives compilation errors with C++
58  * see: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60932
59  */
60 #ifdef __cplusplus
61 #include "c11_atomics_compat.hpp"
62 #else
63 #include <stdatomic.h> /* for atomic_int */
64 #endif
65 #include <sys/stat.h> /* for struct stat */
66 #include <sys/types.h> /* for off_t etc. */
67 #include <sys/statvfs.h> /* for struct statvfs */
68 
69 #include "sched.h"
70 #include "clist.h"
71 
72 #ifdef __cplusplus
73 extern "C" {
74 /* restrict is a C99 keyword, not valid in C++, but GCC and Clang have the
75  * __restrict__ extension keyword which can be used instead */
76 #define restrict __restrict__
77 /* If the above is not supported by the compiler, you can replace it with an
78  * empty definition instead: */
79 /* #define restrict */
80 #endif
81 
86 #ifndef _MAX
87 #define _MAX(a, b) ((a) > (b) ? (a) : (b))
88 #endif
89 #ifndef MAX4
90 #define MAX4(a, b, c, d) _MAX(_MAX((a), (b)), _MAX((c),(d)))
91 #endif
92 
98 #ifdef MODULE_FATFS_VFS
99 #define FATFS_VFS_DIR_BUFFER_SIZE (44)
100 #define FATFS_VFS_FILE_BUFFER_SIZE (72)
101 #else
102 #define FATFS_VFS_DIR_BUFFER_SIZE (1)
103 #define FATFS_VFS_FILE_BUFFER_SIZE (1)
104 #endif
105 
111 #ifdef MODULE_LITTLEFS
112 #define LITTLEFS_VFS_DIR_BUFFER_SIZE (44)
113 #define LITTLEFS_VFS_FILE_BUFFER_SIZE (56)
114 #else
115 #define LITTLEFS_VFS_DIR_BUFFER_SIZE (1)
116 #define LITTLEFS_VFS_FILE_BUFFER_SIZE (1)
117 #endif
118 
124 #ifdef MODULE_LITTLEFS2
125 #define LITTLEFS2_VFS_DIR_BUFFER_SIZE (52)
126 #define LITTLEFS2_VFS_FILE_BUFFER_SIZE (84)
127 #else
128 #define LITTLEFS2_VFS_DIR_BUFFER_SIZE (1)
129 #define LITTLEFS2_VFS_FILE_BUFFER_SIZE (1)
130 #endif
131 
137 #ifdef MODULE_SPIFFS
138 #define SPIFFS_VFS_DIR_BUFFER_SIZE (12)
139 #define SPIFFS_VFS_FILE_BUFFER_SIZE (1)
140 #else
141 #define SPIFFS_VFS_DIR_BUFFER_SIZE (1)
142 #define SPIFFS_VFS_FILE_BUFFER_SIZE (1)
143 #endif
144 
146 #ifndef VFS_MAX_OPEN_FILES
147 
150 #define VFS_MAX_OPEN_FILES (16)
151 #endif
152 
153 #ifndef VFS_DIR_BUFFER_SIZE
154 
181 #define VFS_DIR_BUFFER_SIZE MAX4(FATFS_VFS_DIR_BUFFER_SIZE, \
182  LITTLEFS_VFS_DIR_BUFFER_SIZE, \
183  LITTLEFS2_VFS_DIR_BUFFER_SIZE, \
184  SPIFFS_VFS_DIR_BUFFER_SIZE \
185  )
186 #endif
187 
188 #ifndef VFS_FILE_BUFFER_SIZE
189 
208 #define VFS_FILE_BUFFER_SIZE MAX4(FATFS_VFS_FILE_BUFFER_SIZE, \
209  LITTLEFS_VFS_FILE_BUFFER_SIZE, \
210  LITTLEFS2_VFS_FILE_BUFFER_SIZE,\
211  SPIFFS_VFS_FILE_BUFFER_SIZE \
212  )
213 #endif
214 
215 #ifndef VFS_NAME_MAX
216 
223 #define VFS_NAME_MAX (31)
224 #endif
225 
229 #define VFS_ANY_FD (-1)
230 
231 /* Forward declarations */
236 
240 typedef struct vfs_dir_ops vfs_dir_ops_t;
241 
246 
250 /* not struct vfs_mount because of name collision with the function */
252 
256 typedef struct {
261 
268  const char *mount_point;
271  void *private_data;
272 };
273 
279 typedef struct {
282  int flags;
283  off_t pos;
285  union {
286  void *ptr;
287  int value;
288  uint8_t buffer[VFS_FILE_BUFFER_SIZE];
289  } private_data;
290 } vfs_file_t;
291 
300 typedef struct {
303  union {
304  void *ptr;
305  int value;
306  uint8_t buffer[VFS_DIR_BUFFER_SIZE];
307  } private_data;
308 } vfs_DIR;
309 
318 typedef struct {
319  ino_t d_ino;
320  char d_name[VFS_NAME_MAX + 1];
321 } vfs_dirent_t;
322 
328 struct vfs_file_ops {
347  int (*close) (vfs_file_t *filp);
348 
359  int (*fcntl) (vfs_file_t *filp, int cmd, int arg);
360 
370  int (*fstat) (vfs_file_t *filp, struct stat *buf);
371 
389  off_t (*lseek) (vfs_file_t *filp, off_t off, int whence);
390 
418  int (*open) (vfs_file_t *filp, const char *name, int flags, mode_t mode, const char *abs_path);
419 
430  ssize_t (*read) (vfs_file_t *filp, void *dest, size_t nbytes);
431 
442  ssize_t (*write) (vfs_file_t *filp, const void *src, size_t nbytes);
443 };
444 
448 struct vfs_dir_ops {
459  int (*opendir) (vfs_DIR *dirp, const char *dirname, const char *abs_path);
460 
480  int (*readdir) (vfs_DIR *dirp, vfs_dirent_t *entry);
481 
490  int (*closedir) (vfs_DIR *dirp);
491 };
492 
507  int (*format) (vfs_mount_t *mountp);
508 
523  int (*mount) (vfs_mount_t *mountp);
524 
533  int (*umount) (vfs_mount_t *mountp);
534 
549  int (*rename) (vfs_mount_t *mountp, const char *from_path, const char *to_path);
550 
560  int (*unlink) (vfs_mount_t *mountp, const char *name);
561 
572  int (*mkdir) (vfs_mount_t *mountp, const char *name, mode_t mode);
573 
585  int (*rmdir) (vfs_mount_t *mountp, const char *name);
586 
597  int (*stat) (vfs_mount_t *mountp, const char *restrict path, struct stat *restrict buf);
598 
614  int (*statvfs) (vfs_mount_t *mountp, const char *restrict path, struct statvfs *restrict buf);
615 
631  int (*fstatvfs) (vfs_mount_t *mountp, vfs_file_t *filp, struct statvfs *buf);
632 };
633 
641 void vfs_bind_stdio(void);
642 
651 int vfs_close(int fd);
652 
663 int vfs_fcntl(int fd, int cmd, int arg);
664 
674 int vfs_fstat(int fd, struct stat *buf);
675 
685 int vfs_fstatvfs(int fd, struct statvfs *buf);
686 
704 off_t vfs_lseek(int fd, off_t off, int whence);
705 
716 int vfs_open(const char *name, int flags, mode_t mode);
717 
728 ssize_t vfs_read(int fd, void *dest, size_t count);
729 
740 ssize_t vfs_write(int fd, const void *src, size_t count);
741 
753 int vfs_opendir(vfs_DIR *dirp, const char *dirname);
754 
772 int vfs_readdir(vfs_DIR *dirp, vfs_dirent_t *entry);
773 
785 int vfs_closedir(vfs_DIR *dirp);
786 
800 int vfs_format(vfs_mount_t *mountp);
801 
813 int vfs_mount(vfs_mount_t *mountp);
814 
828 int vfs_rename(const char *from_path, const char *to_path);
829 
840 int vfs_umount(vfs_mount_t *mountp);
841 
850 int vfs_unlink(const char *name);
851 
861 int vfs_mkdir(const char *name, mode_t mode);
862 
873 int vfs_rmdir(const char *name);
874 
884 int vfs_stat(const char *restrict path, struct stat *restrict buf);
885 
898 int vfs_statvfs(const char *restrict path, struct statvfs *restrict buf);
899 
919 int vfs_bind(int fd, int flags, const vfs_file_ops_t *f_op, void *private_data);
920 
938 int vfs_normalize_path(char *buf, const char *path, size_t buflen);
939 
955 const vfs_mount_t *vfs_iterate_mounts(const vfs_mount_t *cur);
956 
970 const vfs_file_t *vfs_file_get(int fd);
971 
983  const char *restrict path,
984  struct stat *restrict buf);
985 
986 #ifdef __cplusplus
987 }
988 #endif
989 
990 #endif /* VFS_H */
991 
kernel_pid_t
int16_t kernel_pid_t
Unique process identifier.
Definition: sched.h:125
vfs_dirent_t::d_ino
ino_t d_ino
file serial number, unique for the file system ("inode" in Linux)
Definition: vfs.h:319
vfs_fstat
int vfs_fstat(int fd, struct stat *buf)
Get status of an open file.
vfs_file_t::mp
vfs_mount_t * mp
Pointer to mount table entry.
Definition: vfs.h:281
vfs_file_system_t::d_op
const vfs_dir_ops_t * d_op
Directory operations table.
Definition: vfs.h:258
atomic_int
Type with the same alignment and size as atomic_int
Definition: c11_atomics_compat.hpp:118
vfs_file_ops::write
ssize_t(* write)(vfs_file_t *filp, const void *src, size_t nbytes)
Write bytes to an open file.
Definition: vfs.h:442
vfs_rename
int vfs_rename(const char *from_path, const char *to_path)
Rename a file.
vfs_iterate_mounts
const vfs_mount_t * vfs_iterate_mounts(const vfs_mount_t *cur)
Iterate through all mounted file systems.
vfs_bind
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.
vfs_file_system_ops::stat
int(* stat)(vfs_mount_t *mountp, const char *restrict path, struct stat *restrict buf)
Get file status.
Definition: vfs.h:597
vfs_fstatvfs
int vfs_fstatvfs(int fd, struct statvfs *buf)
Get file system status of the file system containing an open file.
vfs_sysop_stat_from_fstat
int vfs_sysop_stat_from_fstat(vfs_mount_t *mountp, const char *restrict path, struct stat *restrict buf)
Implementation of stat using fstat
vfs_file_t::pid
kernel_pid_t pid
PID of the process that opened the file.
Definition: vfs.h:284
vfs_mount_struct::list_entry
clist_node_t list_entry
List entry for the _vfs_mount_list list.
Definition: vfs.h:266
vfs_file_system_ops::mkdir
int(* mkdir)(vfs_mount_t *mountp, const char *name, mode_t mode)
Create a directory on the file system.
Definition: vfs.h:572
vfs_write
ssize_t vfs_write(int fd, const void *src, size_t count)
Write bytes to an open file.
vfs_dirent_t
User facing directory entry.
Definition: vfs.h:318
vfs_mount_struct::private_data
void * private_data
File system driver private data, implementation defined.
Definition: vfs.h:271
vfs_file_t::f_op
const vfs_file_ops_t * f_op
File operations table.
Definition: vfs.h:280
vfs_file_t::flags
int flags
File flags.
Definition: vfs.h:282
sched.h
Scheduler API definition.
vfs_normalize_path
int vfs_normalize_path(char *buf, const char *path, size_t buflen)
Normalize a path.
vfs_mkdir
int vfs_mkdir(const char *name, mode_t mode)
Create a directory on the file system.
vfs_readdir
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.
VFS_DIR_BUFFER_SIZE
#define VFS_DIR_BUFFER_SIZE
Size of buffer space in vfs_DIR.
Definition: vfs.h:181
statvfs.h
POSIX compatible sys/statvfs.h definitions.
vfs_bind_stdio
void vfs_bind_stdio(void)
Allocate and bind file descriptors for STDIN, STDERR, and STDOUT.
vfs_dir_ops::opendir
int(* opendir)(vfs_DIR *dirp, const char *dirname, const char *abs_path)
Open a directory for reading with readdir.
Definition: vfs.h:459
vfs_unlink
int vfs_unlink(const char *name)
Unlink (delete) a file from a mounted file system.
vfs_close
int vfs_close(int fd)
Close an open file.
vfs_file_system_ops::rename
int(* rename)(vfs_mount_t *mountp, const char *from_path, const char *to_path)
Rename a file.
Definition: vfs.h:549
vfs_file_t
Information about an open file.
Definition: vfs.h:279
vfs_file_system_ops::unlink
int(* unlink)(vfs_mount_t *mountp, const char *name)
Unlink (delete) a file from the file system.
Definition: vfs.h:560
vfs_statvfs
int vfs_statvfs(const char *restrict path, struct statvfs *restrict buf)
Get file system status.
vfs_file_ops::fstat
int(* fstat)(vfs_file_t *filp, struct stat *buf)
Get status of an open file.
Definition: vfs.h:370
vfs_mount_struct::mount_point
const char * mount_point
Mount point, e.g.
Definition: vfs.h:268
vfs_dir_ops::readdir
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:480
vfs_DIR
Internal representation of a file system directory entry.
Definition: vfs.h:300
vfs_file_system_ops::mount
int(* mount)(vfs_mount_t *mountp)
Perform any extra processing needed after mounting a file system.
Definition: vfs.h:523
vfs_rmdir
int vfs_rmdir(const char *name)
Remove a directory from the file system.
vfs_file_get
const vfs_file_t * vfs_file_get(int fd)
Get information about the file for internal purposes.
vfs_DIR::ptr
void * ptr
pointer to private data
Definition: vfs.h:304
vfs_DIR::mp
vfs_mount_t * mp
Pointer to mount table entry.
Definition: vfs.h:302
vfs_file_system_t::fs_op
const vfs_file_system_ops_t * fs_op
File system operations table.
Definition: vfs.h:259
vfs_opendir
int vfs_opendir(vfs_DIR *dirp, const char *dirname)
Open a directory for reading with readdir.
clist.h
Circular linked list.
vfs_file_ops::read
ssize_t(* read)(vfs_file_t *filp, void *dest, size_t nbytes)
Read bytes from an open file.
Definition: vfs.h:430
vfs_dir_ops::closedir
int(* closedir)(vfs_DIR *dirp)
Close an open directory.
Definition: vfs.h:490
c11_atomics_compat.hpp
C++ compatibility of default C11 atomics types.
vfs_mount_struct::fs
const vfs_file_system_t * fs
The file system driver for the mount point.
Definition: vfs.h:267
vfs_file_t::value
int value
alternatively, you can use private_data as an int
Definition: vfs.h:287
vfs_file_system_t::f_op
const vfs_file_ops_t * f_op
File operations table.
Definition: vfs.h:257
vfs_stat
int vfs_stat(const char *restrict path, struct stat *restrict buf)
Get file status.
vfs_file_system_ops::rmdir
int(* rmdir)(vfs_mount_t *mountp, const char *name)
Remove a directory from the file system.
Definition: vfs.h:585
vfs_file_system_t
A file system driver.
Definition: vfs.h:256
vfs_file_ops::open
int(* open)(vfs_file_t *filp, const char *name, int flags, mode_t mode, const char *abs_path)
Attempt to open a file in the file system at rel_path.
Definition: vfs.h:418
vfs_mount_struct::open_files
atomic_int open_files
Number of currently open files.
Definition: vfs.h:270
vfs_DIR::d_op
const vfs_dir_ops_t * d_op
Directory operations table.
Definition: vfs.h:301
vfs_fcntl
int vfs_fcntl(int fd, int cmd, int arg)
Query/set options on an open file.
vfs_file_ops::fcntl
int(* fcntl)(vfs_file_t *filp, int cmd, int arg)
Query/set options on an open file.
Definition: vfs.h:359
list_node
List node structure.
Definition: list.h:40
vfs_file_t::ptr
void * ptr
pointer to private data
Definition: vfs.h:286
vfs_file_system_ops::format
int(* format)(vfs_mount_t *mountp)
Format the file system on the given mount point.
Definition: vfs.h:507
vfs_file_ops::close
int(* close)(vfs_file_t *filp)
Close an open file.
Definition: vfs.h:347
vfs_open
int vfs_open(const char *name, int flags, mode_t mode)
Open a file.
vfs_file_t::pos
off_t pos
Current position in the file.
Definition: vfs.h:283
VFS_NAME_MAX
#define VFS_NAME_MAX
Maximum length of the name in a vfs_dirent_t (not including terminating null)
Definition: vfs.h:223
vfs_file_system_ops::fstatvfs
int(* fstatvfs)(vfs_mount_t *mountp, vfs_file_t *filp, struct statvfs *buf)
Get file system status of an open file.
Definition: vfs.h:631
vfs_read
ssize_t vfs_read(int fd, void *dest, size_t count)
Read bytes from an open file.
vfs_file_ops
Operations on open files.
Definition: vfs.h:328
vfs_closedir
int vfs_closedir(vfs_DIR *dirp)
Close an open directory.
vfs_DIR::value
int value
alternatively, you can use private_data as an int
Definition: vfs.h:305
vfs_mount_struct::mount_point_len
size_t mount_point_len
Length of mount_point string (set by vfs_mount)
Definition: vfs.h:269
vfs_mount
int vfs_mount(vfs_mount_t *mountp)
Mount a file system.
vfs_file_ops::lseek
off_t(* lseek)(vfs_file_t *filp, off_t off, int whence)
Seek to position in file.
Definition: vfs.h:389
vfs_file_system_ops::umount
int(* umount)(vfs_mount_t *mountp)
Perform the necessary clean up for unmounting a file system.
Definition: vfs.h:533
vfs_format
int vfs_format(vfs_mount_t *mountp)
Format a file system.
vfs_lseek
off_t vfs_lseek(int fd, off_t off, int whence)
Seek to position in file.
VFS_FILE_BUFFER_SIZE
#define VFS_FILE_BUFFER_SIZE
Size of buffer space in vfs_file_t.
Definition: vfs.h:208
statvfs
File system information.
Definition: statvfs.h:45
vfs_dir_ops
Operations on open directories.
Definition: vfs.h:448
vfs_file_system_ops
Operations on mounted file systems.
Definition: vfs.h:498
vfs_mount_struct
A mounted file system.
Definition: vfs.h:265
vfs_umount
int vfs_umount(vfs_mount_t *mountp)
Unmount a mounted file system.