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