VSF Documented
vsf_linux_fs.h
Go to the documentation of this file.
1/*****************************************************************************
2 * Copyright(C)2009-2022 by VSF Team *
3 * *
4 * Licensed under the Apache License, Version 2.0 (the "License"); *
5 * you may not use this file except in compliance with the License. *
6 * You may obtain a copy of the License at *
7 * *
8 * http://www.apache.org/licenses/LICENSE-2.0 *
9 * *
10 * Unless required by applicable law or agreed to in writing, software *
11 * distributed under the License is distributed on an "AS IS" BASIS, *
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13 * See the License for the specific language governing permissions and *
14 * limitations under the License. *
15 * *
16 ****************************************************************************/
17
18#ifndef __VSF_LINUX_FS_INTERNAL_H__
19#define __VSF_LINUX_FS_INTERNAL_H__
20
21/*============================ INCLUDES ======================================*/
22
23// avoid to include vsf.h, include necessary headers only
24// #include "vsf.h"
25
27#include "hal/arch/vsf_arch.h"
28#include "kernel/vsf_kernel.h"
29
30#if VSF_USE_LINUX == ENABLED
31
32#if VSF_LINUX_CFG_RELATIVE_PATH == ENABLED
35#else
36# include <termios.h>
37# include <sys/stat.h>
38#endif
39
40#if defined(__VSF_LINUX_FS_CLASS_IMPLEMENT)
41# define __VSF_CLASS_IMPLEMENT__
42#elif defined(__VSF_LINUX_FS_CLASS_INHERIT__)
43# define __VSF_CLASS_INHERIT__
44#endif
45
46#include "utilities/ooc_class.h"
47
48#ifdef __cplusplus
49extern "C" {
50#endif
51
52/*============================ MACROS ========================================*/
53
54#if VSF_USE_FS != ENABLED
55# error VSF_USE_FS MUST be enabled to use fs in linux
56#endif
57
58/*============================ MACROFIED FUNCTIONS ===========================*/
59/*============================ TYPES =========================================*/
60
65
66enum {
68};
69
70typedef struct vsf_linux_fd_op_t {
73 void (*fn_init)(vsf_linux_fd_t *sfd);
74 void (*fn_fini)(vsf_linux_fd_t *sfd);
75 int (*fn_fcntl)(vsf_linux_fd_t *sfd, int cmd, uintptr_t arg);
76 ssize_t (*fn_read)(vsf_linux_fd_t *sfd, void *buf, size_t count);
77 ssize_t (*fn_write)(vsf_linux_fd_t *sfd, const void *buf, size_t count);
79 int (*fn_eof)(vsf_linux_fd_t *sfd);
81 int (*fn_stat)(vsf_linux_fd_t *sfd, struct stat *buf);
82
83 void * (*fn_mmap)(vsf_linux_fd_t *sfd, off64_t offset, size_t len, uint_fast32_t feature);
84 int (*fn_munmap)(vsf_linux_fd_t *sfd, void *buffer);
85 int (*fn_msync)(vsf_linux_fd_t *sfd, void *buffer);
87
94
96 protected_member(
97 // target is used in socket_unix to indicate message for sendmsg/recvmsg,
98 // ofc it can be used in other fd types
99 void *target;
100 int flags;
101
102 short status;
103 short events;
104 // sticky_events will not be cleared
105 // use for eg pipe, pipe_tx is closed, pipe_rx will always POLLIN
106 short sticky_events;
107 short user_data;
108
109 vsf_linux_fd_priv_callback_t events_callback[2];
111 private_member(
112 int ref;
113 )
114};
115
117 protected_member(
118 int fd;
119 int fd_flags;
120 int cur_rdflags;
121 int cur_wrflags;
122 const vsf_linux_fd_op_t *op;
124
125 private_member(
126 vsf_dlist_node_t fd_node;
127 void *mmapped_buffer;
128 bool is_close_pending_on_munmap;
129 )
130
131 protected_member(
132 int unget_buff;
134
135 // used while binding fd to something
136 // eg: in popen, fd is binded with the target pid
137 union {
138 pid_t pid;
139 } binding;
141};
142
143#if defined(__VSF_LINUX_FS_CLASS_IMPLEMENT) || defined(__VSF_LINUX_FS_CLASS_INHERIT__)
144# if VSF_LINUX_CFG_FS_CACHE_SIZE > 0
145VSF_STATIC_ASSERT(VSF_LINUX_CFG_FS_CACHE_SIZE <= ((1 << (sizeof(uint16_t) << 3)) - 1));
146VSF_STATIC_ASSERT(VSF_LINUX_CFG_FS_CACHE_SIZE > VSF_LINUX_CFG_FS_CACHE_THRESHOLD);
147# endif
148typedef struct vk_file_t vk_file_t;
149typedef struct vsf_linux_fs_priv_t {
150 implement(vsf_linux_fd_priv_t)
151 vk_file_t *file;
152
153 union {
154 struct dirent dir;
155 struct dirent64 dir64;
156 };
157# if VSF_LINUX_CFG_FS_CACHE_SIZE > 0
158 uint8_t *cache_buffer;
159 uint16_t cache_size;
160 uint16_t cache_offset;
161 uint64_t cache_pos;
162# endif
163} vsf_linux_fs_priv_t;
164
165vsf_dcl_class(vsf_linux_stream_priv_t)
166typedef void (*vsf_linux_stream_on_evt_t)(vsf_linux_stream_priv_t *priv, vsf_protect_t orig, short event, bool is_ready);
167
168vsf_class(vsf_linux_stream_priv_t) {
169 public_member(
170 implement(vsf_linux_fs_priv_t)
171 )
172 protected_member(
173 vsf_stream_t *stream_rx;
174 vsf_stream_t *stream_tx;
175 vsf_linux_stream_on_evt_t on_evt;
176 void *term_priv;
177 bool stream_rx_allocated;
178 bool stream_tx_allocated;
179 )
180};
181
182vsf_class(vsf_linux_pipe_priv_t) {
183 protected_member(
184 implement(vsf_linux_stream_priv_t)
185 vsf_linux_pipe_priv_t *pipe_remote;
186 )
187};
188
189typedef struct vsf_linux_term_priv_t {
190 implement(vsf_linux_pipe_priv_t)
191 const vsf_linux_fd_op_t *subop;
192 struct termios termios;
193 pid_t foreground_pgid;
194 volatile uint8_t sig_pending;
195 char esc_type;
196 bool line_start;
197 bool subop_inited;
198 bool icrnl_pending;
199} vsf_linux_term_priv_t;
200
201typedef struct vsf_linux_key_t vsf_linux_key_t;
202vsf_class(vsf_linux_key_priv_t) {
203 public_member(
204 implement(vsf_linux_fd_priv_t)
205 )
206 protected_member(
207 union {
208 struct {
209 unsigned short *semadj_arr;
210 } sem;
211 } u;
212 vsf_linux_key_t *key;
213 void (*fn_fini)(vsf_linux_fd_t *sfd);
214 int (*fn_close)(vsf_linux_fd_t *sfd);
215 )
216};
217#endif
218
219/*============================ GLOBAL VARIABLES ==============================*/
220
221#if defined(__VSF_LINUX_FS_CLASS_IMPLEMENT) || defined(__VSF_LINUX_FS_CLASS_INHERIT__)
227#endif
228
229/*============================ LOCAL VARIABLES ===============================*/
230/*============================ PROTOTYPES ====================================*/
231
232#if defined(__VSF_LINUX_FS_CLASS_IMPLEMENT) || defined(__VSF_LINUX_FS_CLASS_INHERIT__)
233extern int vsf_linux_fd_bind_target_ex(int fd,
234 void *target, const vsf_linux_fd_op_t *op,
237 uint_fast32_t feature, uint64_t size);
238extern int vsf_linux_fd_bind_target(int fd, void *target,
240 vsf_param_eda_evthandler_t peda_write);
241// compatible with file and directory
242extern int vsf_linux_fd_get_target(int fd, void **target);
243extern int vsf_linux_fs_bind_target(const char *pathname, void *target,
245 vsf_param_eda_evthandler_t peda_write);
246extern int vsf_linux_fs_bind_dir_target(const char *pathname, void *target);
247int vsf_linux_fs_bind_target_ex(const char *pathname,
248 void *target, const vsf_linux_fd_op_t *op,
251 uint_fast32_t feature, uint64_t size);
252extern int vsf_linux_fs_get_target(const char *pathname, void **target);
253typedef struct vk_vfs_file_t vk_vfs_file_t;
254extern int vsf_linux_fs_bind_target_relative(vk_vfs_file_t *dir, const char *pathname,
255 void *target, const vsf_linux_fd_op_t *op,
256 uint_fast32_t feature, uint64_t size);
257extern int vsf_linux_fs_bind_pipe(const char *pathname1, const char *pathname2, bool exclusive);
258extern int vsf_linux_create_pty(int num);
259
260extern int vsf_linux_fd_create(vsf_linux_fd_t **sfd, const vsf_linux_fd_op_t *op);
261extern vsf_linux_fd_t * vsf_linux_fd_get(int fd);
262extern void vsf_linux_fd_delete(int fd);
263extern bool vsf_linux_fd_is_block(vsf_linux_fd_t *sfd);
264
265extern int vsf_linux_fd_get_feature(int fd, uint_fast32_t *feature);
266extern int vsf_linux_fd_set_feature(int fd, uint_fast32_t feature);
267extern int vsf_linux_fd_add_feature(int fd, uint_fast32_t feature);
268
269extern int vsf_linux_fd_set_size(int fd, uint64_t size);
270
271// vsf_linux_fd_xx_trigger/vsf_linux_fd_xx_pend MUST be called scheduler protected
278extern short vsf_linux_fd_get_status(vsf_linux_fd_priv_t *priv, short status);
279
280// stream
281// IMPORTANT: priority of stream MUST be within scheduler priorities
282extern vsf_linux_fd_t * vsf_linux_stream(vsf_stream_t *stream_rx, vsf_stream_t *stream_tx);
285
286// pipe
288extern vsf_linux_fd_t * vsf_linux_tx_pipe(vsf_linux_pipe_priv_t *priv_rx);
289#endif
290
291extern int vsf_linux_fs_bind_buffer(const char *pathname, void *buffer,
292 uint_fast32_t feature, uint64_t size);
293extern int vsf_linux_fs_bind_stream(const char *pathname, vsf_stream_t *stream_rx, vsf_stream_t *stream_tx);
294
295#ifdef __cplusplus
296}
297#endif
298
299// DO NOT undefine MACROs below, because lower header will check the same MACROs to include necessary codes
300//#undef __VSF_LINUX_FS_CLASS_IMPLEMENT
301//#undef __VSF_LINUX_FS_CLASS_INHERIT__
302
303/*============================ INCLUDES ======================================*/
304
305#include "./vfs/vsf_linux_vfs.h"
306
307#endif // VSF_USE_LINUX
308#endif // __VSF_LINUX_FS_INTERNAL_H__
309/* EOF */
#define VSF_STATIC_ASSERT(__COND,...)
Definition __type.h:199
Definition vsf_fs.h:114
Definition vsf_linux_fs.h:95
Definition vsf_linux_fs.h:116
Definition vsf_linux.h:283
Definition vsf_linux.h:160
Definition vsf_queue_stream.h:60
Definition vsf_simple_stream.h:254
union @1235::@1246::@1247 u
int pid_t
Definition types.h:113
long long off64_t
Definition types.h:125
int ssize_t
Definition types.h:91
#define vsf_dcl_class
Definition ooc_class.h:50
#define vsf_class(__name)
Definition ooc_class.h:52
uint32_t uintptr_t
Definition stdint.h:38
unsigned short uint16_t
Definition stdint.h:7
unsigned int uint_fast32_t
Definition stdint.h:27
unsigned long long uint64_t
Definition stdint.h:11
unsigned char uint8_t
Definition stdint.h:5
Definition dirent.h:42
Definition dirent.h:35
Definition stat.h:107
Definition termios.h:205
Definition vsf_list.h:888
Definition vsf_linux_fs.h:70
int(* fn_setsize)(vsf_linux_fd_t *sfd, off64_t size)
Definition vsf_linux_fs.h:80
ssize_t(* fn_write)(vsf_linux_fd_t *sfd, const void *buf, size_t count)
Definition vsf_linux_fs.h:77
int feature
Definition vsf_linux_fs.h:72
int(* fn_msync)(vsf_linux_fd_t *sfd, void *buffer)
Definition vsf_linux_fs.h:85
int priv_size
Definition vsf_linux_fs.h:71
int(* fn_fcntl)(vsf_linux_fd_t *sfd, int cmd, uintptr_t arg)
Definition vsf_linux_fs.h:75
int(* fn_stat)(vsf_linux_fd_t *sfd, struct stat *buf)
Definition vsf_linux_fs.h:81
int(* fn_close)(vsf_linux_fd_t *sfd)
Definition vsf_linux_fs.h:78
int(* fn_eof)(vsf_linux_fd_t *sfd)
Definition vsf_linux_fs.h:79
ssize_t(* fn_read)(vsf_linux_fd_t *sfd, void *buf, size_t count)
Definition vsf_linux_fs.h:76
int(* fn_munmap)(vsf_linux_fd_t *sfd, void *buffer)
Definition vsf_linux_fs.h:84
void(* fn_init)(vsf_linux_fd_t *sfd)
Definition vsf_linux_fs.h:73
void(* fn_fini)(vsf_linux_fd_t *sfd)
Definition vsf_linux_fs.h:74
Definition vsf_linux_fs.h:88
short pendind_events
Definition vsf_linux_fs.h:89
void(* cb)(vsf_linux_fd_priv_t *priv, void *param, short events, vsf_protect_t orig)
Definition vsf_linux_fs.h:92
vsf_linux_process_t * owner
Definition vsf_linux_fs.h:90
void * param
Definition vsf_linux_fs.h:91
Definition vsf_linux.h:130
uintalu_t vsf_protect_t
Definition vsf_arch_abstraction.h:60
void(* vsf_param_eda_evthandler_t)(uintptr_t target, vsf_evt_t evt)
Parameterized event handler; invoked with the user parameter (target) and the event (evt)
Definition vsf_eda.h:1681
#define VSF_LINUX_CFG_FS_CACHE_SIZE
Definition vsf_linux_cfg.h:42
int vsf_linux_fs_bind_target_relative(vk_vfs_file_t *dir, const char *pathname, void *target, const vsf_linux_fd_op_t *op, uint_fast32_t feature, uint64_t size)
Definition vsf_linux_fs.c:2890
int vsf_linux_fs_get_target(const char *pathname, void **target)
Definition vsf_linux_fs.c:2878
int vsf_linux_fs_bind_dir_target(const char *pathname, void *target)
Definition vsf_linux_fs.c:2943
vsf_linux_fd_t * vsf_linux_tx_pipe(vsf_linux_pipe_priv_t *priv_rx)
Definition vsf_linux_fs.c:3583
int vsf_linux_fd_bind_target_ex(int fd, void *target, const vsf_linux_fd_op_t *op, vsf_param_eda_evthandler_t peda_read, vsf_param_eda_evthandler_t peda_write, uint_fast32_t feature, uint64_t size)
Definition vsf_linux_fs.c:2851
void vsf_linux_fd_set_events(vsf_linux_fd_priv_t *priv, short events, vsf_protect_t orig)
Definition vsf_linux_fs.c:1271
int vsf_linux_fd_set_feature(int fd, uint_fast32_t feature)
Definition vsf_linux_fs.c:996
int vsf_linux_fs_bind_target(const char *pathname, void *target, vsf_param_eda_evthandler_t peda_read, vsf_param_eda_evthandler_t peda_write)
Definition vsf_linux_fs.c:2930
bool vsf_linux_fd_is_block(vsf_linux_fd_t *sfd)
Definition vsf_linux_fs.c:1192
const vsf_linux_fd_op_t __vsf_linux_stream_fdop
Definition vsf_linux_fs.c:242
int vsf_linux_fd_get_feature(int fd, uint_fast32_t *feature)
Definition vsf_linux_fs.c:984
int vsf_linux_create_pty(int num)
Definition vsf_linux_fs.c:3640
vsf_linux_fd_priv_callback_t * vsf_linux_fd_claim_calback(vsf_linux_fd_priv_t *priv)
Definition vsf_linux_fs.c:1217
int vsf_linux_fd_create(vsf_linux_fd_t **sfd, const vsf_linux_fd_op_t *op)
Definition vsf_linux_fs.c:1138
vsf_linux_fd_t * vsf_linux_rx_stream(vsf_stream_t *stream)
Definition vsf_linux_fs.c:3446
int vsf_linux_fs_bind_pipe(const char *pathname1, const char *pathname2, bool exclusive)
Definition vsf_linux_fs.c:3611
vsf_linux_fd_t * vsf_linux_fd_get(int fd)
Definition vsf_linux_fs.c:960
int vsf_linux_fd_bind_target(int fd, void *target, vsf_param_eda_evthandler_t peda_read, vsf_param_eda_evthandler_t peda_write)
Definition vsf_linux_fs.c:2871
const vsf_linux_fd_op_t vsf_linux_term_fdop
Definition vsf_linux_fs.c:264
int vsf_linux_fd_get_target(int fd, void **target)
Definition vsf_linux_fs.c:2838
void vsf_linux_fd_delete(int fd)
Definition vsf_linux_fs.c:1166
const vsf_linux_fd_op_t vsf_linux_pipe_fdop
Definition vsf_linux_fs.c:252
void vsf_linux_fd_clear_status(vsf_linux_fd_priv_t *priv, short status, vsf_protect_t orig)
Definition vsf_linux_fs.c:1297
int vsf_linux_fd_set_size(int fd, uint64_t size)
Definition vsf_linux_fs.c:1015
void vsf_linux_fd_release_calback(vsf_linux_fd_priv_t *priv, vsf_linux_fd_priv_callback_t *callback)
Definition vsf_linux_fs.c:1232
int vsf_linux_fs_bind_target_ex(const char *pathname, void *target, const vsf_linux_fd_op_t *op, vsf_param_eda_evthandler_t peda_read, vsf_param_eda_evthandler_t peda_write, uint_fast32_t feature, uint64_t size)
Definition vsf_linux_fs.c:2912
short vsf_linux_fd_get_status(vsf_linux_fd_priv_t *priv, short status)
Definition vsf_linux_fs.c:1304
short vsf_linux_fd_pend_events(vsf_linux_fd_priv_t *priv, short events, vsf_linux_trigger_t *trig, vsf_protect_t orig)
Definition vsf_linux_fs.c:1238
void vsf_linux_fd_set_status(vsf_linux_fd_priv_t *priv, short status, vsf_protect_t orig)
Definition vsf_linux_fs.c:1286
vsf_linux_fd_t * vsf_linux_stream(vsf_stream_t *stream_rx, vsf_stream_t *stream_tx)
Definition vsf_linux_fs.c:3432
const vsf_linux_fd_op_t vsf_linux_key_fdop
Definition vsf_linux_fs.c:276
vsf_linux_fd_t * vsf_linux_rx_pipe(vsf_queue_stream_t *queue_stream)
Definition vsf_linux_fs.c:3554
vsf_linux_fd_t * vsf_linux_tx_stream(vsf_stream_t *stream)
Definition vsf_linux_fs.c:3451
const vsf_linux_fd_op_t __vsf_linux_fs_fdop
Definition vsf_linux_fs.c:210
int vsf_linux_fd_add_feature(int fd, uint_fast32_t feature)
Definition vsf_linux_fs.c:1006
@ VSF_LINUX_FDOP_FEATURE_FS
Definition vsf_linux_fs.h:67
int vsf_linux_fs_bind_buffer(const char *pathname, void *buffer, uint_fast32_t feature, uint64_t size)
Definition vsf_linux_fs.c:3009
struct vsf_linux_fd_priv_callback_t vsf_linux_fd_priv_callback_t
struct vsf_linux_fd_op_t vsf_linux_fd_op_t
int vsf_linux_fs_bind_stream(const char *pathname, vsf_stream_t *stream_rx, vsf_stream_t *stream_tx)
Definition vsf_linux_fs.c:3456
uint64_t offset
Definition vsf_memfs.h:49
uint32_t size
Definition vsf_memfs.h:50
uint8_t status
Definition vsf_tgui.h:144
struct event_t events[32]
Definition vsf_xboot.c:36
Generated from commit: vsfteam/vsf@2a137b0