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 "kernel_types.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 
972 #ifdef __cplusplus
973 }
974 #endif
975 
976 #endif /* VFS_H */
977 
C++ compatibility of default C11 atomics types.
Types used by the kernel.
off_t vfs_lseek(int fd, off_t off, int whence)
Seek to position in file.
int value
alternatively, you can use private_data as an int
Definition: vfs.h:287
int vfs_rename(const char *from_path, const char *to_path)
Rename a file.
POSIX compatible sys/statvfs.h definitions.
off_t pos
Current position in the file.
Definition: vfs.h:283
void * private_data
File system driver private data, implementation defined.
Definition: vfs.h:271
int vfs_umount(vfs_mount_t *mountp)
Unmount a mounted file system.
const vfs_mount_t * vfs_iterate_mounts(const vfs_mount_t *cur)
Iterate through all mounted file systems.
int flags
File flags.
Definition: vfs.h:282
int vfs_fstat(int fd, struct stat *buf)
Get status of an open file.
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_fstatvfs(int fd, struct statvfs *buf)
Get file system status of the file system containing an open file.
int32_t off_t
Used for file sizes and offsets.
Definition: msp430_types.h:86
const char * mount_point
Mount point, e.g.
Definition: vfs.h:268
const vfs_file_ops_t * f_op
File operations table.
Definition: vfs.h:280
#define VFS_DIR_BUFFER_SIZE
Size of buffer space in vfs_DIR.
Definition: vfs.h:181
void vfs_bind_stdio(void)
Allocate and bind file descriptors for STDIN, STDERR, and STDOUT.
void * ptr
pointer to private data
Definition: vfs.h:304
User facing directory entry.
Definition: vfs.h:318
ssize_t vfs_write(int fd, const void *src, size_t count)
Write bytes to an open file.
int16_t kernel_pid_t
Unique process identifier.
Definition: kernel_types.h:83
int vfs_mkdir(const char *name, mode_t mode)
Create a directory on the file system.
const vfs_file_ops_t * f_op
File operations table.
Definition: vfs.h:257
int vfs_unlink(const char *name)
Unlink (delete) a file from a mounted file system.
signed int ssize_t
Used for a count of bytes or an error indication.
Definition: msp430_types.h:89
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...
int vfs_normalize_path(char *buf, const char *path, size_t buflen)
Normalize a path.
const vfs_dir_ops_t * d_op
Directory operations table.
Definition: vfs.h:301
const vfs_file_system_t * fs
The file system driver for the mount point.
Definition: vfs.h:267
int vfs_statvfs(const char *restrict path, struct statvfs *restrict buf)
Get file system status.
vfs_mount_t * mp
Pointer to mount table entry.
Definition: vfs.h:281
ino_t d_ino
file serial number, unique for the file system ("inode" in Linux)
Definition: vfs.h:319
int vfs_close(int fd)
Close an open file.
vfs_mount_t * mp
Pointer to mount table entry.
Definition: vfs.h:302
Internal representation of a file system directory entry.
Definition: vfs.h:300
Operations on mounted file systems.
Definition: vfs.h:498
Information about an open file.
Definition: vfs.h:279
int vfs_opendir(vfs_DIR *dirp, const char *dirname)
Open a directory for reading with readdir.
const vfs_dir_ops_t * d_op
Directory operations table.
Definition: vfs.h:258
const vfs_file_t * vfs_file_get(int fd)
Get information about the file for internal purposes.
Type with the same alignment and size as atomic_int
A mounted file system.
Definition: vfs.h:265
int vfs_stat(const char *restrict path, struct stat *restrict buf)
Get file status.
int vfs_rmdir(const char *name)
Remove a directory from the file system.
List node structure.
Definition: list.h:40
void * ptr
pointer to private data
Definition: vfs.h:286
Operations on open files.
Definition: vfs.h:328
kernel_pid_t pid
PID of the process that opened the file.
Definition: vfs.h:284
int vfs_fcntl(int fd, int cmd, int arg)
Query/set options on an open file.
int value
alternatively, you can use private_data as an int
Definition: vfs.h:305
#define VFS_NAME_MAX
Maximum length of the name in a vfs_dirent_t (not including terminating null)
Definition: vfs.h:223
ssize_t vfs_read(int fd, void *dest, size_t count)
Read bytes from an open file.
Circular linked list.
A file system driver.
Definition: vfs.h:256
const vfs_file_system_ops_t * fs_op
File system operations table.
Definition: vfs.h:259
Operations on open directories.
Definition: vfs.h:448
File system information.
Definition: statvfs.h:45
int vfs_open(const char *name, int flags, mode_t mode)
Open a file.
atomic_int open_files
Number of currently open files.
Definition: vfs.h:270
#define VFS_FILE_BUFFER_SIZE
Size of buffer space in vfs_file_t.
Definition: vfs.h:208
int vfs_closedir(vfs_DIR *dirp)
Close an open directory.
int vfs_mount(vfs_mount_t *mountp)
Mount a file system.
clist_node_t list_entry
List entry for the _vfs_mount_list list.
Definition: vfs.h:266
size_t mount_point_len
Length of mount_point string (set by vfs_mount)
Definition: vfs.h:269
int vfs_format(vfs_mount_t *mountp)
Format a file system.