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