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