Tenok
A Linux-like Real-Time Operating System for Robotics and Internet of Things
fs.h
Go to the documentation of this file.
1 
4 #ifndef __FS_H__
5 #define __FS_H__
6 
7 #include <dirent.h>
8 #include <stdbool.h>
9 #include <stddef.h>
10 #include <stdint.h>
11 #include <sys/types.h>
12 
13 #include <common/list.h>
14 #include <kernel/wait.h>
15 
16 #include "kconfig.h"
17 
18 #define RDEV_ROOTFS 0
19 
20 #define FILE_RESERVED_NUM (THREAD_MAX + 3)
21 #define THREAD_PIPE_FD(thread_id) (thread_id + 3)
22 
23 /* +---------------------------+
24  * | File table layout |
25  * +-----------+---------------+
26  * | 0 | stdin |
27  * +-----------+---------------+
28  * | 1 | stdout |
29  * +-----------+---------------+
30  * | 2 | stderr |
31  * +-----------+---------------+
32  * | 3 | Thread pipe 1 |
33  * +-----------+---------------+
34  * | ... | ... |
35  * +-----------+---------------+
36  * | N + 2 | Thread pipe N |
37  * +-----------+---------------+
38  * | N + 3 | File 1 |
39  * +-----------+---------------+
40  * | ... | ... |
41  * +-----------+---------------+
42  * | N + M + 3 | File M |
43  * +-----------+---------------+
44  *
45  * N = THREAD_MAX
46  * M = OPEN_MAX
47  */
48 
49 typedef void (*drv_init_func_t)(void);
50 
51 enum {
52  FS_CREATE_FILE = 1,
53  FS_OPEN_FILE = 2,
54  FS_OPEN_DIR = 3,
55  FS_MOUNT = 4,
56  FS_GET_CWD = 5,
57  FS_CHANGE_DIR = 6,
58 } FS_SERVER_CMDS;
59 
60 struct super_block {
61  bool s_rd_only; /* Read-only flag */
62  uint32_t s_blk_cnt; /* number of the used blocks */
63  uint32_t s_inode_cnt; /* number of the used inodes */
64  uint64_t s_sb_addr; /* Start address of the super block */
65  uint64_t s_ino_addr; /* Start address of the inode table */
66  uint64_t s_blk_addr; /* Start address of the blocks region */
67 };
68 
69 /* Block header will be placed at the top of every blocks of regular files */
70 struct block_header {
71  /* Virtual address of the next block */
72  uint32_t b_next;
73  uint32_t reserved;
74 };
75 
76 struct mount {
77  struct file *dev_file; /* Driver file of the mounted device */
78  struct super_block super_blk; /* Super block of the mounted device */
79 };
80 
81 /* index node */
82 struct inode {
83  /* File type: S_IFIFO, S_IFCHR, etc. */
84  uint8_t i_mode;
85  /* The device on which the file is mounted */
86  uint8_t i_rdev;
87  /* The mounted file is loaded into the rootfs or not */
88  bool i_sync;
89  /* inode number */
90  uint32_t i_ino;
91  /* inode number of the parent directory */
92  uint32_t i_parent;
93  /* File descriptor number */
94  uint32_t i_fd;
95  /* File size (bytes) */
96  uint32_t i_size;
97  /* block_numbers = file_size / (block_size - block_header_size) */
98  uint32_t i_blocks;
99  /* Virtual address for accessing the storage */
100  uint32_t i_data;
101  uint32_t reserved1;
102  /* List head of the dentry table */
103  struct list_head i_dentry;
104  uint32_t reserved2[2];
105 };
106 
107 /* Directory entry */
108 struct dentry {
109  /* File name */
110  char d_name[NAME_MAX];
111  /* The inode of the file */
112  uint32_t d_inode;
113  /* The inode of the parent directory */
114  uint32_t d_parent;
115  /* List head of the dentry */
116  struct list_head d_list;
117  uint32_t reserved[2];
118 };
119 
120 struct file {
121  struct inode *f_inode;
122  struct file_operations *f_op;
123  uint32_t f_events;
124  int f_flags;
125  struct list_head list;
126 };
127 
129  off_t (*lseek)(struct file *filp, off_t offset, int whence);
130  ssize_t (*read)(struct file *filp, char *buf, size_t size, off_t offset);
131  ssize_t (*write)(struct file *filp,
132  const char *buf,
133  size_t size,
134  off_t offset);
135  int (*ioctl)(struct file *, unsigned int cmd, unsigned long arg);
136  int (*open)(struct inode *inode, struct file *file);
137 };
138 
139 struct fdtable {
140  int flags;
141  struct file *file;
142 };
143 
144 void rootfs_init(void);
145 
146 void link_stdin_dev(char *path);
147 void link_stdout_dev(char *path);
148 void link_stderr_dev(char *path);
149 
150 int register_chrdev(char *name, struct file_operations *fops);
151 int register_blkdev(char *name, struct file_operations *fops);
152 
153 int fs_read_dir(DIR *dirp, struct dirent *dirent);
154 uint32_t fs_get_block_addr(struct inode *inode, int blk_index);
155 uint32_t fs_file_append_block(struct inode *inode);
156 
157 void request_create_file(int thread_id, const char *path, uint8_t file_type);
158 void request_open_file(int thread_id, const char *path);
159 void request_open_directory(int reply_fd, const char *path);
160 void request_mount(int thread_id, const char *source, const char *target);
161 void request_getcwd(int thread_id, char *buf, size_t len);
162 void request_chdir(int thread_id, const char *path);
163 
164 void filesysd(void);
165 
166 void fs_print_inode_bitmap(void);
167 void fs_print_block_bitmap(void);
168 
169 #endif
int open(const char *pathname, int flags)
Open the file specified by the pathname.
Definition: file.c:15
int ioctl(int fd, unsigned int request, unsigned long arg)
Perform device-specific control.
Definition: file.c:60
Definition: fs.h:70
Definition: fs.h:108
Definition: dirent.h:19
Definition: dirent.h:13
Definition: fs.h:139
Definition: fs.h:128
Definition: fs.h:120
Definition: fs.h:82
Definition: list.h:111
Definition: fs.h:76
Definition: fs.h:60
ssize_t write(int fd, const void *buf, size_t count)
Write up to count bytes from the buffer starting at buf to the file referred to by the file descripto...
Definition: file.c:55
ssize_t read(int fd, void *buf, size_t count)
Attempt to read up to count bytes from file descriptor fd into the buffer starting at buf.
Definition: file.c:45