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