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