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