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_NAME_MAX
123 
130 #define VFS_NAME_MAX (31)
131 #endif
132 
136 #define VFS_ANY_FD (-1)
137 
138 /* Forward declarations */
143 
147 typedef struct vfs_dir_ops vfs_dir_ops_t;
148 
153 
157 /* not struct vfs_mount because of name collision with the function */
159 
163 typedef struct {
168 
175  const char *mount_point;
177  atomic_int open_files;
178  void *private_data;
179 };
180 
186 typedef struct {
189  int flags;
190  off_t pos;
192  union {
193  void *ptr;
194  int value;
195  } private_data;
196 } vfs_file_t;
197 
206 typedef struct {
209  union {
210  void *ptr;
211  int value;
212  uint8_t buffer[VFS_DIR_BUFFER_SIZE];
213  } private_data;
214 } vfs_DIR;
215 
224 typedef struct {
225  ino_t d_ino;
226  char d_name[VFS_NAME_MAX + 1];
227 } vfs_dirent_t;
228 
234 struct vfs_file_ops {
253  int (*close) (vfs_file_t *filp);
254 
265  int (*fcntl) (vfs_file_t *filp, int cmd, int arg);
266 
276  int (*fstat) (vfs_file_t *filp, struct stat *buf);
277 
295  off_t (*lseek) (vfs_file_t *filp, off_t off, int whence);
296 
324  int (*open) (vfs_file_t *filp, const char *name, int flags, mode_t mode, const char *abs_path);
325 
336  ssize_t (*read) (vfs_file_t *filp, void *dest, size_t nbytes);
337 
348  ssize_t (*write) (vfs_file_t *filp, const void *src, size_t nbytes);
349 };
350 
354 struct vfs_dir_ops {
365  int (*opendir) (vfs_DIR *dirp, const char *dirname, const char *abs_path);
366 
386  int (*readdir) (vfs_DIR *dirp, vfs_dirent_t *entry);
387 
396  int (*closedir) (vfs_DIR *dirp);
397 };
398 
419  int (*mount) (vfs_mount_t *mountp);
420 
429  int (*umount) (vfs_mount_t *mountp);
430 
445  int (*rename) (vfs_mount_t *mountp, const char *from_path, const char *to_path);
446 
456  int (*unlink) (vfs_mount_t *mountp, const char *name);
457 
468  int (*mkdir) (vfs_mount_t *mountp, const char *name, mode_t mode);
469 
481  int (*rmdir) (vfs_mount_t *mountp, const char *name);
482 
493  int (*stat) (vfs_mount_t *mountp, const char *restrict path, struct stat *restrict buf);
494 
510  int (*statvfs) (vfs_mount_t *mountp, const char *restrict path, struct statvfs *restrict buf);
511 
527  int (*fstatvfs) (vfs_mount_t *mountp, vfs_file_t *filp, struct statvfs *buf);
528 };
529 
538 int vfs_close(int fd);
539 
550 int vfs_fcntl(int fd, int cmd, int arg);
551 
561 int vfs_fstat(int fd, struct stat *buf);
562 
572 int vfs_fstatvfs(int fd, struct statvfs *buf);
573 
591 off_t vfs_lseek(int fd, off_t off, int whence);
592 
603 int vfs_open(const char *name, int flags, mode_t mode);
604 
615 ssize_t vfs_read(int fd, void *dest, size_t count);
616 
627 ssize_t vfs_write(int fd, const void *src, size_t count);
628 
640 int vfs_opendir(vfs_DIR *dirp, const char *dirname);
641 
659 int vfs_readdir(vfs_DIR *dirp, vfs_dirent_t *entry);
660 
672 int vfs_closedir(vfs_DIR *dirp);
673 
685 int vfs_mount(vfs_mount_t *mountp);
686 
700 int vfs_rename(const char *from_path, const char *to_path);
701 
712 int vfs_umount(vfs_mount_t *mountp);
713 
722 int vfs_unlink(const char *name);
723 
733 int vfs_mkdir(const char *name, mode_t mode);
734 
745 int vfs_rmdir(const char *name);
746 
756 int vfs_stat(const char *restrict path, struct stat *restrict buf);
757 
770 int vfs_statvfs(const char *restrict path, struct statvfs *restrict buf);
771 
791 int vfs_bind(int fd, int flags, const vfs_file_ops_t *f_op, void *private_data);
792 
810 int vfs_normalize_path(char *buf, const char *path, size_t buflen);
811 
827 const vfs_mount_t *vfs_iterate_mounts(const vfs_mount_t *cur);
828 
829 #ifdef __cplusplus
830 }
831 #endif
832 
833 #endif /* VFS_H */
834 
Types used by the kernel.
off_t vfs_lseek(int fd, off_t off, int whence)
Seek to position in file.
int(* stat)(vfs_mount_t *mountp, const char *restrict path, struct stat *restrict buf)
Get file status.
Definition: vfs.h:493
int value
alternatively, you can use private_data as an int
Definition: vfs.h:194
int vfs_rename(const char *from_path, const char *to_path)
Rename a file.
int(* fcntl)(vfs_file_t *filp, int cmd, int arg)
Query/set options on an open file.
Definition: vfs.h:265
POSIX compatible sys/statvfs.h definitions.
off_t pos
Current position in the file.
Definition: vfs.h:190
void * private_data
File system driver private data, implementation defined.
Definition: vfs.h:178
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(* mkdir)(vfs_mount_t *mountp, const char *name, mode_t mode)
Create a directory on the file system.
Definition: vfs.h:468
int flags
File flags.
Definition: vfs.h:189
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.
const char * mount_point
Mount point, e.g.
Definition: vfs.h:175
int(* closedir)(vfs_DIR *dirp)
Close an open directory.
Definition: vfs.h:396
const vfs_file_ops_t * f_op
File operations table.
Definition: vfs.h:187
#define VFS_DIR_BUFFER_SIZE
Size of buffer space in vfs_DIR.
Definition: vfs.h:119
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:324
void * ptr
pointer to private data
Definition: vfs.h:210
User facing directory entry.
Definition: vfs.h:224
off_t(* lseek)(vfs_file_t *filp, off_t off, int whence)
Seek to position in file.
Definition: vfs.h:295
ssize_t vfs_write(int fd, const void *src, size_t count)
Write bytes to an open file.
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:386
int(* fstat)(vfs_file_t *filp, struct stat *buf)
Get status of an open file.
Definition: vfs.h:276
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:164
int vfs_unlink(const char *name)
Unlink (delete) a file from a mounted file system.
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:207
const vfs_file_system_t * fs
The file system driver for the mount point.
Definition: vfs.h:174
int(* rename)(vfs_mount_t *mountp, const char *from_path, const char *to_path)
Rename a file.
Definition: vfs.h:445
int vfs_statvfs(const char *restrict path, struct statvfs *restrict buf)
Get file system status.
int(* unlink)(vfs_mount_t *mountp, const char *name)
Unlink (delete) a file from the file system.
Definition: vfs.h:456
vfs_mount_t * mp
Pointer to mount table entry.
Definition: vfs.h:188
ino_t d_ino
file serial number, unique for the file system ("inode" in Linux)
Definition: vfs.h:225
int(* mount)(vfs_mount_t *mountp)
Perform any extra processing needed after mounting a file system.
Definition: vfs.h:419
int vfs_close(int fd)
Close an open file.
vfs_mount_t * mp
Pointer to mount table entry.
Definition: vfs.h:208
Internal representation of a file system directory entry.
Definition: vfs.h:206
Operations on mounted file systems.
Definition: vfs.h:404
ssize_t(* read)(vfs_file_t *filp, void *dest, size_t nbytes)
Read bytes from an open file.
Definition: vfs.h:336
Information about an open file.
Definition: vfs.h:186
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:165
int(* opendir)(vfs_DIR *dirp, const char *dirname, const char *abs_path)
Open a directory for reading with readdir.
Definition: vfs.h:365
A mounted file system.
Definition: vfs.h:172
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.
ssize_t(* write)(vfs_file_t *filp, const void *src, size_t nbytes)
Write bytes to an open file.
Definition: vfs.h:348
List node structure.
Definition: list.h:40
void * ptr
pointer to private data
Definition: vfs.h:193
Operations on open files.
Definition: vfs.h:234
kernel_pid_t pid
PID of the process that opened the file.
Definition: vfs.h:191
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:211
#define VFS_NAME_MAX
Maximum length of the name in a vfs_dirent_t (not including terminating null)
Definition: vfs.h:130
ssize_t vfs_read(int fd, void *dest, size_t count)
Read bytes from an open file.
Circular linked list.
int(* rmdir)(vfs_mount_t *mountp, const char *name)
Remove a directory from the file system.
Definition: vfs.h:481
A file system driver.
Definition: vfs.h:163
const vfs_file_system_ops_t * fs_op
File system operations table.
Definition: vfs.h:166
Operations on open directories.
Definition: vfs.h:354
File system information.
Definition: statvfs.h:47
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:177
int(* umount)(vfs_mount_t *mountp)
Perform the necessary clean up for unmounting a file system.
Definition: vfs.h:429
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:173
int(* fstatvfs)(vfs_mount_t *mountp, vfs_file_t *filp, struct statvfs *buf)
Get file system status of an open file.
Definition: vfs.h:527
int(* close)(vfs_file_t *filp)
Close an open file.
Definition: vfs.h:253
size_t mount_point_len
Length of mount_point string (set by vfs_mount)
Definition: vfs.h:176