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 <atomic>
62 /* Make atomic_int available without namespace specifier */
63 using std::atomic_int;
64 #else
65 #include <stdatomic.h> /* for atomic_int */
66 #endif
67 #include <sys/stat.h> /* for struct stat */
68 #include <sys/types.h> /* for off_t etc. */
69 #include <sys/statvfs.h> /* for struct statvfs */
70 
71 #include "kernel_types.h"
72 #include "clist.h"
73 
74 #ifdef __cplusplus
75 extern "C" {
76 /* restrict is a C99 keyword, not valid in C++, but GCC and Clang have the
77  * __restrict__ extension keyword which can be used instead */
78 #define restrict __restrict__
79 /* If the above is not supported by the compiler, you can replace it with an
80  * empty definition instead: */
81 /* #define restrict */
82 #endif
83 
84 #ifndef VFS_MAX_OPEN_FILES
85 
88 #define VFS_MAX_OPEN_FILES (16)
89 #endif
90 
91 #ifndef VFS_DIR_BUFFER_SIZE
92 
119 #define VFS_DIR_BUFFER_SIZE (12)
120 #endif
121 
122 #ifndef VFS_FILE_BUFFER_SIZE
123 
142 #define VFS_FILE_BUFFER_SIZE (1)
143 #endif
144 
145 #ifndef VFS_NAME_MAX
146 
153 #define VFS_NAME_MAX (31)
154 #endif
155 
159 #define VFS_ANY_FD (-1)
160 
161 /* Forward declarations */
166 
170 typedef struct vfs_dir_ops vfs_dir_ops_t;
171 
176 
180 /* not struct vfs_mount because of name collision with the function */
182 
186 typedef struct {
191 
198  const char *mount_point;
200  atomic_int open_files;
201  void *private_data;
202 };
203 
209 typedef struct {
212  int flags;
215  union {
216  void *ptr;
217  int value;
218  uint8_t buffer[VFS_FILE_BUFFER_SIZE];
219  } private_data;
220 } vfs_file_t;
221 
230 typedef struct {
233  union {
234  void *ptr;
235  int value;
236  uint8_t buffer[VFS_DIR_BUFFER_SIZE];
237  } private_data;
238 } vfs_DIR;
239 
248 typedef struct {
250  char d_name[VFS_NAME_MAX + 1];
251 } vfs_dirent_t;
252 
258 struct vfs_file_ops {
277  int (*close) (vfs_file_t *filp);
278 
289  int (*fcntl) (vfs_file_t *filp, int cmd, int arg);
290 
300  int (*fstat) (vfs_file_t *filp, struct stat *buf);
301 
319  off_t (*lseek) (vfs_file_t *filp, off_t off, int whence);
320 
348  int (*open) (vfs_file_t *filp, const char *name, int flags, mode_t mode, const char *abs_path);
349 
360  ssize_t (*read) (vfs_file_t *filp, void *dest, size_t nbytes);
361 
372  ssize_t (*write) (vfs_file_t *filp, const void *src, size_t nbytes);
373 };
374 
378 struct vfs_dir_ops {
389  int (*opendir) (vfs_DIR *dirp, const char *dirname, const char *abs_path);
390 
410  int (*readdir) (vfs_DIR *dirp, vfs_dirent_t *entry);
411 
420  int (*closedir) (vfs_DIR *dirp);
421 };
422 
437  int (*format) (vfs_mount_t *mountp);
438 
453  int (*mount) (vfs_mount_t *mountp);
454 
463  int (*umount) (vfs_mount_t *mountp);
464 
479  int (*rename) (vfs_mount_t *mountp, const char *from_path, const char *to_path);
480 
490  int (*unlink) (vfs_mount_t *mountp, const char *name);
491 
502  int (*mkdir) (vfs_mount_t *mountp, const char *name, mode_t mode);
503 
515  int (*rmdir) (vfs_mount_t *mountp, const char *name);
516 
527  int (*stat) (vfs_mount_t *mountp, const char *restrict path, struct stat *restrict buf);
528 
544  int (*statvfs) (vfs_mount_t *mountp, const char *restrict path, struct statvfs *restrict buf);
545 
561  int (*fstatvfs) (vfs_mount_t *mountp, vfs_file_t *filp, struct statvfs *buf);
562 };
563 
571 void vfs_bind_stdio(void);
572 
581 int vfs_close(int fd);
582 
593 int vfs_fcntl(int fd, int cmd, int arg);
594 
604 int vfs_fstat(int fd, struct stat *buf);
605 
615 int vfs_fstatvfs(int fd, struct statvfs *buf);
616 
634 off_t vfs_lseek(int fd, off_t off, int whence);
635 
646 int vfs_open(const char *name, int flags, mode_t mode);
647 
658 ssize_t vfs_read(int fd, void *dest, size_t count);
659 
670 ssize_t vfs_write(int fd, const void *src, size_t count);
671 
683 int vfs_opendir(vfs_DIR *dirp, const char *dirname);
684 
702 int vfs_readdir(vfs_DIR *dirp, vfs_dirent_t *entry);
703 
715 int vfs_closedir(vfs_DIR *dirp);
716 
730 int vfs_format(vfs_mount_t *mountp);
731 
743 int vfs_mount(vfs_mount_t *mountp);
744 
758 int vfs_rename(const char *from_path, const char *to_path);
759 
770 int vfs_umount(vfs_mount_t *mountp);
771 
780 int vfs_unlink(const char *name);
781 
791 int vfs_mkdir(const char *name, mode_t mode);
792 
803 int vfs_rmdir(const char *name);
804 
814 int vfs_stat(const char *restrict path, struct stat *restrict buf);
815 
828 int vfs_statvfs(const char *restrict path, struct statvfs *restrict buf);
829 
849 int vfs_bind(int fd, int flags, const vfs_file_ops_t *f_op, void *private_data);
850 
868 int vfs_normalize_path(char *buf, const char *path, size_t buflen);
869 
885 const vfs_mount_t *vfs_iterate_mounts(const vfs_mount_t *cur);
886 
887 #ifdef __cplusplus
888 }
889 #endif
890 
891 #endif /* VFS_H */
892 
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:217
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:213
void * private_data
File system driver private data, implementation defined.
Definition: vfs.h:201
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:212
int16_t kernel_pid_t
Unique process identifier.
Definition: kernel_types.h:83
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:198
const vfs_file_ops_t * f_op
File operations table.
Definition: vfs.h:210
#define VFS_DIR_BUFFER_SIZE
Size of buffer space in vfs_DIR.
Definition: vfs.h:119
void vfs_bind_stdio(void)
Allocate and bind file descriptors for STDIN, STDERR, and STDOUT.
void * ptr
pointer to private data
Definition: vfs.h:234
User facing directory entry.
Definition: vfs.h:248
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.
const vfs_file_ops_t * f_op
File operations table.
Definition: vfs.h:187
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:231
const vfs_file_system_t * fs
The file system driver for the mount point.
Definition: vfs.h:197
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:211
ino_t d_ino
file serial number, unique for the file system ("inode" in Linux)
Definition: vfs.h:249
int vfs_close(int fd)
Close an open file.
vfs_mount_t * mp
Pointer to mount table entry.
Definition: vfs.h:232
Internal representation of a file system directory entry.
Definition: vfs.h:230
Operations on mounted file systems.
Definition: vfs.h:428
Information about an open file.
Definition: vfs.h:209
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:188
A mounted file system.
Definition: vfs.h:195
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:216
This file is a modification of original sys/types.h.
Operations on open files.
Definition: vfs.h:258
kernel_pid_t pid
PID of the process that opened the file.
Definition: vfs.h:214
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:235
#define VFS_NAME_MAX
Maximum length of the name in a vfs_dirent_t (not including terminating null)
Definition: vfs.h:153
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:186
const vfs_file_system_ops_t * fs_op
File system operations table.
Definition: vfs.h:189
Operations on open directories.
Definition: vfs.h:378
File system information.
Definition: statvfs.h:45
int vfs_open(const char *name, int flags, mode_t mode)
Open a file.
uint32_t ino_t
Used for file serial numbers.
Definition: msp430_types.h:82
atomic_int open_files
Number of currently open files.
Definition: vfs.h:200
#define VFS_FILE_BUFFER_SIZE
Size of buffer space in vfs_file_t.
Definition: vfs.h:142
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:196
uint32_t mode_t
Used for some file attributes.
Definition: msp430_types.h:84
size_t mount_point_len
Length of mount_point string (set by vfs_mount)
Definition: vfs.h:199
int vfs_format(vfs_mount_t *mountp)
Format a file system.