xref: /haiku/headers/private/kernel/vfs.h (revision 62f5ba006a08b0df30631375878effaf67ae5dbc)
1 /*
2  * Copyright 2002-2009, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  *
5  * Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
6  * Distributed under the terms of the NewOS License.
7  */
8 #ifndef _KERNEL_VFS_H
9 #define _KERNEL_VFS_H
10 
11 
12 #include <kernel.h>
13 #include <lock.h>
14 #include <util/list.h>
15 
16 #include <fs_interface.h>
17 
18 #include <dirent.h>
19 #include <signal.h>
20 #include <sys/socket.h>
21 #include <sys/stat.h>
22 #include <sys/select.h>
23 
24 #include <vfs_defs.h>
25 
26 
27 #define DEFAULT_FD_TABLE_SIZE	256
28 #define MAX_FD_TABLE_SIZE		8192
29 #define DEFAULT_NODE_MONITORS	4096
30 #define MAX_NODE_MONITORS		65536
31 
32 #define B_UNMOUNT_BUSY_PARTITION	0x80000000
33 
34 struct attr_info;
35 struct file_descriptor;
36 struct kernel_args;
37 struct net_stat;
38 struct pollfd;
39 struct rlimit;
40 struct selectsync;
41 struct select_info;
42 struct VMCache;
43 struct vnode;
44 
45 
46 /** The I/O context of a process/team, holds the fd array among others */
47 typedef struct io_context {
48 	struct vnode *root;
49 	struct vnode *cwd;
50 	mutex		io_mutex;
51 	int32		ref_count;
52 	uint32		table_size;
53 	uint32		num_used_fds;
54 	struct file_descriptor **fds;
55 	struct select_info **select_infos;
56 	uint8		*fds_close_on_exec;
57 	struct list node_monitors;
58 	uint32		num_monitors;
59 	uint32		max_monitors;
60 } io_context;
61 
62 /* macro to allocate a iovec array on the stack */
63 #define IOVECS(name, size) \
64 	uint8 _##name[sizeof(iovecs) + (size)*sizeof(iovec)]; \
65 	iovecs *name = (iovecs *)_##name
66 
67 
68 #ifdef __cplusplus
69 extern "C" {
70 #endif
71 
72 status_t	vfs_init(struct kernel_args *args);
73 status_t	vfs_bootstrap_file_systems(void);
74 void		vfs_mount_boot_file_system(struct kernel_args *args);
75 void		vfs_exec_io_context(io_context *context);
76 io_context*	vfs_new_io_context(io_context* parentContext,
77 				bool purgeCloseOnExec);
78 void		vfs_get_io_context(io_context *context);
79 void		vfs_put_io_context(io_context *context);
80 
81 int			vfs_getrlimit(int resource, struct rlimit *rlp);
82 int			vfs_setrlimit(int resource, const struct rlimit *rlp);
83 
84 /* calls needed by the VM for paging and by the file cache */
85 status_t	vfs_get_vnode_from_fd(int fd, bool kernel, struct vnode **_vnode);
86 status_t	vfs_get_vnode_from_path(const char *path, bool kernel,
87 				struct vnode **_vnode);
88 status_t	vfs_get_vnode(dev_t mountID, ino_t vnodeID, bool canWait,
89 				struct vnode **_vnode);
90 status_t	vfs_entry_ref_to_vnode(dev_t mountID, ino_t directoryID,
91 				const char *name, struct vnode **_vnode);
92 void		vfs_vnode_to_node_ref(struct vnode *vnode, dev_t *_mountID,
93 				ino_t *_vnodeID);
94 
95 int			vfs_open_vnode(struct vnode* vnode, int openMode, bool kernel);
96 status_t	vfs_lookup_vnode(dev_t mountID, ino_t vnodeID,
97 				struct vnode **_vnode);
98 void		vfs_put_vnode(struct vnode *vnode);
99 void		vfs_acquire_vnode(struct vnode *vnode);
100 status_t	vfs_get_cookie_from_fd(int fd, void **_cookie);
101 bool		vfs_can_page(struct vnode *vnode, void *cookie);
102 status_t	vfs_read_pages(struct vnode *vnode, void *cookie, off_t pos,
103 				const iovec *vecs, size_t count, uint32 flags,
104 				size_t *_numBytes);
105 status_t	vfs_write_pages(struct vnode *vnode, void *cookie, off_t pos,
106 				const iovec *vecs, size_t count, uint32 flags,
107 				size_t *_numBytes);
108 status_t	vfs_vnode_io(struct vnode* vnode, void* cookie,
109 				io_request* request);
110 status_t	vfs_synchronous_io(io_request* request,
111 				status_t (*doIO)(void* cookie, off_t offset, void* buffer,
112 					size_t* length),
113 				void* cookie);
114 status_t	vfs_get_vnode_cache(struct vnode *vnode, struct VMCache **_cache,
115 				bool allocate);
116 status_t	vfs_get_file_map(struct vnode *vnode, off_t offset, size_t size,
117 				struct file_io_vec *vecs, size_t *_count);
118 status_t	vfs_get_fs_node_from_path(fs_volume *volume, const char *path,
119 				bool traverseLeafLink, bool kernel, void **_node);
120 status_t	vfs_stat_vnode(struct vnode *vnode, struct stat *stat);
121 status_t	vfs_stat_node_ref(dev_t device, ino_t inode, struct stat *stat);
122 status_t	vfs_get_vnode_name(struct vnode *vnode, char *name,
123 				size_t nameSize);
124 status_t	vfs_entry_ref_to_path(dev_t device, ino_t inode, const char *leaf,
125 				char *path, size_t pathLength);
126 status_t	vfs_get_cwd(dev_t *_mountID, ino_t *_vnodeID);
127 void		vfs_unlock_vnode_if_locked(struct file_descriptor *descriptor);
128 status_t	vfs_unmount(dev_t mountID, uint32 flags);
129 status_t	vfs_disconnect_vnode(dev_t mountID, ino_t vnodeID);
130 void		vfs_free_unused_vnodes(int32 level);
131 
132 status_t	vfs_read_stat(int fd, const char *path, bool traverseLeafLink,
133 				struct stat *stat, bool kernel);
134 
135 /* special module convenience call */
136 status_t	vfs_get_module_path(const char *basePath, const char *moduleName,
137 				char *pathBuffer, size_t bufferSize);
138 
139 /* service call for whoever needs a normalized path */
140 status_t	vfs_normalize_path(const char *path, char *buffer,
141 				size_t bufferSize, bool traverseLink, bool kernel);
142 
143 /* service call for whoever wants to create a special node */
144 status_t	vfs_create_special_node(const char *path, fs_vnode *subVnode,
145 				mode_t mode, uint32 flags, bool kernel, fs_vnode *_superVnode,
146 				struct vnode **_createdVnode);
147 
148 /* service call for the node monitor */
149 status_t	resolve_mount_point_to_volume_root(dev_t mountID, ino_t nodeID,
150 				dev_t *resolvedMountID, ino_t *resolvedNodeID);
151 
152 /* calls the syscall dispatcher should use for user file I/O */
153 dev_t		_user_mount(const char *path, const char *device,
154 				const char *fs_name, uint32 flags, const char *args,
155 				size_t argsLength);
156 status_t	_user_unmount(const char *path, uint32 flags);
157 status_t	_user_read_fs_info(dev_t device, struct fs_info *info);
158 status_t	_user_write_fs_info(dev_t device, const struct fs_info *info,
159 				int mask);
160 dev_t		_user_next_device(int32 *_cookie);
161 status_t	_user_sync(void);
162 status_t	_user_get_next_fd_info(team_id team, uint32 *cookie,
163 				struct fd_info *info, size_t infoSize);
164 status_t	_user_entry_ref_to_path(dev_t device, ino_t inode, const char *leaf,
165 				char *userPath, size_t pathLength);
166 status_t	_user_normalize_path(const char* userPath, bool traverseLink,
167 				char* buffer);
168 int			_user_open_entry_ref(dev_t device, ino_t inode, const char *name,
169 				int openMode, int perms);
170 int			_user_open(int fd, const char *path, int openMode, int perms);
171 int			_user_open_dir_node_ref(dev_t device, ino_t inode);
172 int			_user_open_dir_entry_ref(dev_t device, ino_t inode,
173 				const char *name);
174 int			_user_open_dir(int fd, const char *path);
175 int			_user_open_parent_dir(int fd, char *name, size_t nameLength);
176 status_t	_user_fcntl(int fd, int op, uint32 argument);
177 status_t	_user_fsync(int fd);
178 status_t	_user_flock(int fd, int op);
179 status_t	_user_read_stat(int fd, const char *path, bool traverseLink,
180 				struct stat *stat, size_t statSize);
181 status_t	_user_write_stat(int fd, const char *path, bool traverseLink,
182 				const struct stat *stat, size_t statSize, int statMask);
183 off_t		_user_seek(int fd, off_t pos, int seekType);
184 status_t	_user_create_dir_entry_ref(dev_t device, ino_t inode,
185 				const char *name, int perms);
186 status_t	_user_create_dir(int fd, const char *path, int perms);
187 status_t	_user_remove_dir(int fd, const char *path);
188 status_t	_user_read_link(int fd, const char *path, char *buffer,
189 				size_t *_bufferSize);
190 status_t	_user_write_link(const char *path, const char *toPath);
191 status_t	_user_create_symlink(int fd, const char *path, const char *toPath,
192 				int mode);
193 status_t	_user_create_link(int pathFD, const char *path, int toFD,
194 				const char *toPath, bool traverseLeafLink);
195 status_t	_user_unlink(int fd, const char *path);
196 status_t	_user_rename(int oldFD, const char *oldpath, int newFD,
197 				const char *newpath);
198 status_t	_user_create_fifo(int fd, const char *path, mode_t perms);
199 status_t	_user_create_pipe(int *fds);
200 status_t	_user_access(int fd, const char *path, int mode,
201 				bool effectiveUserGroup);
202 ssize_t		_user_select(int numfds, fd_set *readSet, fd_set *writeSet,
203 				fd_set *errorSet, bigtime_t timeout, const sigset_t *sigMask);
204 ssize_t		_user_poll(struct pollfd *fds, int numfds, bigtime_t timeout);
205 int			_user_open_attr_dir(int fd, const char *path);
206 ssize_t		_user_read_attr(int fd, const char *attribute, off_t pos,
207 				void *buffer, size_t readBytes);
208 ssize_t		_user_write_attr(int fd, const char *attribute, uint32 type,
209 				off_t pos, const void *buffer, size_t readBytes);
210 status_t	_user_stat_attr(int fd, const char *attribute,
211 				struct attr_info *attrInfo);
212 int			_user_open_attr(int fd, const char* path, const char *name,
213 				uint32 type, int openMode);
214 status_t	_user_remove_attr(int fd, const char *name);
215 status_t	_user_rename_attr(int fromFile, const char *fromName, int toFile,
216 				const char *toName);
217 int			_user_open_index_dir(dev_t device);
218 status_t	_user_create_index(dev_t device, const char *name, uint32 type,
219 				uint32 flags);
220 status_t	_user_read_index_stat(dev_t device, const char *name,
221 				struct stat *stat);
222 status_t	_user_remove_index(dev_t device, const char *name);
223 status_t	_user_getcwd(char *buffer, size_t size);
224 status_t	_user_setcwd(int fd, const char *path);
225 status_t	_user_change_root(const char *path);
226 int			_user_open_query(dev_t device, const char *query,
227 				size_t queryLength, uint32 flags, port_id port, int32 token);
228 
229 /* fd user prototypes (implementation located in fd.cpp)  */
230 ssize_t		_user_read(int fd, off_t pos, void *buffer, size_t bufferSize);
231 ssize_t		_user_readv(int fd, off_t pos, const iovec *vecs, size_t count);
232 ssize_t		_user_write(int fd, off_t pos, const void *buffer,
233 				size_t bufferSize);
234 ssize_t		_user_writev(int fd, off_t pos, const iovec *vecs, size_t count);
235 status_t	_user_ioctl(int fd, ulong cmd, void *data, size_t length);
236 ssize_t		_user_read_dir(int fd, struct dirent *buffer, size_t bufferSize,
237 				uint32 maxCount);
238 status_t	_user_rewind_dir(int fd);
239 status_t	_user_close(int fd);
240 int			_user_dup(int fd);
241 int			_user_dup2(int ofd, int nfd);
242 status_t	_user_lock_node(int fd);
243 status_t	_user_unlock_node(int fd);
244 
245 /* socket user prototypes (implementation in socket.cpp) */
246 int			_user_socket(int family, int type, int protocol);
247 status_t	_user_bind(int socket, const struct sockaddr *address,
248 				socklen_t addressLength);
249 status_t	_user_shutdown_socket(int socket, int how);
250 status_t	_user_connect(int socket, const struct sockaddr *address,
251 				socklen_t addressLength);
252 status_t	_user_listen(int socket, int backlog);
253 int			_user_accept(int socket, struct sockaddr *address,
254 				socklen_t *_addressLength);
255 ssize_t		_user_recv(int socket, void *data, size_t length, int flags);
256 ssize_t		_user_recvfrom(int socket, void *data, size_t length, int flags,
257 				struct sockaddr *address, socklen_t *_addressLength);
258 ssize_t		_user_recvmsg(int socket, struct msghdr *message, int flags);
259 ssize_t		_user_send(int socket, const void *data, size_t length, int flags);
260 ssize_t		_user_sendto(int socket, const void *data, size_t length, int flags,
261 				const struct sockaddr *address, socklen_t addressLength);
262 ssize_t		_user_sendmsg(int socket, const struct msghdr *message, int flags);
263 status_t	_user_getsockopt(int socket, int level, int option, void *value,
264 				socklen_t *_length);
265 status_t	_user_setsockopt(int socket, int level, int option,
266 				const void *value, socklen_t length);
267 status_t	_user_getpeername(int socket, struct sockaddr *address,
268 				socklen_t *_addressLength);
269 status_t	_user_getsockname(int socket, struct sockaddr *address,
270 				socklen_t *_addressLength);
271 int			_user_sockatmark(int socket);
272 status_t	_user_socketpair(int family, int type, int protocol,
273 				int *socketVector);
274 status_t	_user_get_next_socket_stat(int family, uint32 *cookie,
275 				struct net_stat *stat);
276 
277 #ifdef __cplusplus
278 }
279 #endif
280 
281 
282 #ifdef __cplusplus
283 
284 class AsyncIOCallback {
285 public:
286 	virtual						~AsyncIOCallback();
287 
288 	virtual	void				IOFinished(status_t status,
289 									bool partialTransfer,
290 									size_t bytesTransferred) = 0;
291 
292 	static	status_t 			IORequestCallback(void* data,
293 									io_request* request, status_t status,
294 									bool partialTransfer,
295 									size_t transferEndOffset);
296 };
297 
298 
299 class StackableAsyncIOCallback : public AsyncIOCallback {
300 public:
301 								StackableAsyncIOCallback(AsyncIOCallback* next);
302 
303 protected:
304 			AsyncIOCallback*	fNextCallback;
305 };
306 
307 
308 status_t	vfs_asynchronous_read_pages(struct vnode* vnode, void* cookie,
309 				off_t pos, const iovec* vecs, size_t count, size_t numBytes,
310 				uint32 flags, AsyncIOCallback* callback);
311 
312 status_t	vfs_asynchronous_write_pages(struct vnode* vnode, void* cookie,
313 				off_t pos, const iovec* vecs, size_t count, size_t numBytes,
314 				uint32 flags, AsyncIOCallback* callback);
315 
316 #endif	// __cplusplus
317 
318 #endif	/* _KERNEL_VFS_H */
319