3 * @author Lunaixsky (zelong56@gmail.com)
4 * @brief Lunaix virtual file system - an abstraction layer for all file system.
8 * @copyright Copyright (c) 2022
12 #include <klibc/string.h>
13 #include <lunaix/dirent.h>
14 #include <lunaix/foptions.h>
15 #include <lunaix/fs.h>
16 #include <lunaix/mm/cake.h>
17 #include <lunaix/mm/page.h>
18 #include <lunaix/mm/valloc.h>
19 #include <lunaix/process.h>
20 #include <lunaix/spike.h>
21 #include <lunaix/syscall.h>
23 #define PATH_DELIM '/'
24 #define DNODE_HASHTABLE_BITS 10
25 #define DNODE_HASHTABLE_SIZE (1 << DNODE_HASHTABLE_BITS)
26 #define DNODE_HASH_MASK (DNODE_HASHTABLE_SIZE - 1)
27 #define DNODE_HASHBITS (32 - DNODE_HASHTABLE_BITS)
29 static struct cake_pile* dnode_pile;
30 static struct cake_pile* inode_pile;
31 static struct cake_pile* file_pile;
32 static struct cake_pile* superblock_pile;
33 static struct cake_pile* fd_pile;
35 static struct v_superblock* root_sb;
36 static struct hbucket* dnode_cache;
44 vfs_d_free(struct v_dnode* dnode);
50 vfs_sb_free(struct v_superblock* sb);
55 // 为他们专门创建一个蛋糕堆,而不使用valloc,这样我们可以最小化内碎片的产生
56 dnode_pile = cake_new_pile("dnode_cache", sizeof(struct v_dnode), 1, 0);
57 inode_pile = cake_new_pile("inode_cache", sizeof(struct v_inode), 1, 0);
58 file_pile = cake_new_pile("file_cache", sizeof(struct v_file), 1, 0);
59 fd_pile = cake_new_pile("fd_cache", sizeof(struct v_fd), 1, 0);
61 cake_new_pile("sb_cache", sizeof(struct v_superblock), 1, 0);
63 dnode_cache = vzalloc(DNODE_HASHTABLE_SIZE * sizeof(struct hbucket));
65 // 创建一个根superblock,用来蕴含我们的根目录。
66 root_sb = vfs_sb_alloc();
67 root_sb->root = vfs_d_alloc();
70 inline struct hbucket*
71 __dcache_get_bucket(struct v_dnode* parent, unsigned int hash)
73 // 与parent的指针值做加法,来减小碰撞的可能性。
74 hash += (uint32_t)parent;
76 hash = hash ^ (hash >> DNODE_HASHBITS);
77 return &dnode_cache[hash & DNODE_HASH_MASK];
81 vfs_dcache_lookup(struct v_dnode* parent, struct hstr* str)
86 struct hbucket* slot = __dcache_get_bucket(parent, str->hash);
88 struct v_dnode *pos, *n;
89 hashtable_bucket_foreach(slot, pos, n, hash_list)
91 if (pos->name.hash == str->hash) {
99 vfs_dcache_add(struct v_dnode* parent, struct v_dnode* dnode)
101 struct hbucket* bucket = __dcache_get_bucket(parent, dnode->name.hash);
102 hlist_add(&bucket->head, &dnode->hash_list);
106 vfs_walk(struct v_dnode* start,
108 struct v_dnode** dentry,
109 struct hstr* component,
115 if (path[0] == PATH_DELIM || !start) {
116 if ((walk_options & VFS_WALK_FSRELATIVE) && start) {
117 start = start->super_block->root;
119 start = root_sb->root;
124 struct v_dnode* dnode;
125 struct v_dnode* current_level = start;
127 char name_content[VFS_NAME_MAXLEN];
128 struct hstr name = HSTR(name_content, 0);
130 char current = path[i++], lookahead;
132 lookahead = path[i++];
133 if (current != PATH_DELIM) {
134 if (j >= VFS_NAME_MAXLEN - 1) {
137 if (!VFS_VALID_CHAR(current)) {
140 name_content[j++] = current;
146 // handling cases like /^.*(\/+).*$/
147 if (lookahead == PATH_DELIM) {
153 hstr_rehash(&name, HSTR_FULL_HASH);
155 if (!lookahead && (walk_options & VFS_WALK_PARENT)) {
157 component->hash = name.hash;
159 strcpy(component->value, name_content);
164 dnode = vfs_dcache_lookup(current_level, &name);
167 dnode = vfs_d_alloc();
168 dnode->name = HSTR(valloc(VFS_NAME_MAXLEN), j);
169 dnode->name.hash = name.hash;
171 strcpy(dnode->name.value, name_content);
174 current_level->inode->ops.dir_lookup(current_level->inode, dnode);
176 if (errno == ENOENT && (walk_options & VFS_WALK_MKPARENT)) {
177 if (!current_level->inode->ops.mkdir) {
180 errno = current_level->inode->ops.mkdir(
181 current_level->inode, dnode);
189 vfs_dcache_add(current_level, dnode);
191 dnode->parent = current_level;
192 llist_append(¤t_level->children, &dnode->siblings);
196 current_level = dnode;
201 *dentry = current_level;
205 vfree(dnode->name.value);
211 vfs_mount(const char* target, const char* fs_name, bdev_t device)
216 if (!(errno = vfs_walk(NULL, target, &mnt, NULL, 0))) {
217 errno = vfs_mount_at(fs_name, device, mnt);
224 vfs_unmount(const char* target)
229 if (!(errno = vfs_walk(NULL, target, &mnt, NULL, 0))) {
230 errno = vfs_unmount_at(mnt);
237 vfs_mount_at(const char* fs_name, bdev_t device, struct v_dnode* mnt_point)
239 struct filesystem* fs = fsm_get(fs_name);
242 struct v_superblock* sb = vfs_sb_alloc();
247 if (!(errno = fs->mount(sb, mnt_point))) {
249 sb->root = mnt_point;
250 mnt_point->super_block = sb;
251 llist_append(&root_sb->sb_list, &sb->sb_list);
258 vfs_unmount_at(struct v_dnode* mnt_point)
261 struct v_superblock* sb = mnt_point->super_block;
265 if (!(errno = sb->fs->unmount(sb))) {
266 struct v_dnode* fs_root = sb->root;
267 llist_delete(&fs_root->siblings);
268 llist_delete(&sb->sb_list);
275 vfs_open(struct v_dnode* dnode, struct v_file** file)
277 if (!dnode->inode || !dnode->inode->ops.open) {
281 struct v_file* vfile = cake_grab(file_pile);
282 memset(vfile, 0, sizeof(*vfile));
284 int errno = dnode->inode->ops.open(dnode->inode, vfile);
286 cake_release(file_pile, vfile);
294 vfs_close(struct v_file* file)
296 if (!file->ops.close) {
300 int errno = file->ops.close(file);
302 cake_release(file_pile, file);
308 vfs_fsync(struct v_file* file)
311 if (file->ops.sync) {
312 errno = file->ops.sync(file);
314 if (!errno && file->inode->ops.sync) {
315 return file->inode->ops.sync(file->inode);
321 vfs_alloc_fdslot(int* fd)
323 for (size_t i = 0; i < VFS_MAX_FD; i++) {
324 if (!__current->fdtable->fds[i]) {
335 struct v_superblock* sb = cake_grab(superblock_pile);
336 memset(sb, 0, sizeof(*sb));
337 llist_init_head(&sb->sb_list);
342 vfs_sb_free(struct v_superblock* sb)
344 cake_release(superblock_pile, sb);
350 struct v_dnode* dnode = cake_grab(dnode_pile);
351 llist_init_head(&dnode->children);
355 vfs_d_free(struct v_dnode* dnode)
357 if (dnode->ops.destruct) {
358 dnode->ops.destruct(dnode);
360 cake_release(dnode_pile, dnode);
366 struct v_inode* inode = cake_grab(inode_pile);
367 memset(inode, 0, sizeof(*inode));
373 vfs_i_free(struct v_inode* inode)
375 cake_release(inode_pile, inode);
378 __DEFINE_LXSYSCALL2(int, open, const char*, path, int, options)
380 char name_str[VFS_NAME_MAXLEN];
381 struct hstr name = HSTR(name_str, 0);
382 struct v_dnode *dentry, *file;
384 if ((errno = vfs_walk(NULL, path, &dentry, &name, VFS_WALK_PARENT))) {
388 vfs_walk(dentry, name.value, &file, NULL, 0);
390 struct v_file* opened_file = 0;
392 if ((options & FO_CREATE)) {
393 errno = dentry->inode->ops.create(dentry->inode, opened_file);
398 errno = vfs_open(file, &opened_file);
401 __current->k_status = errno;
403 if (!errno && !(errno = vfs_alloc_fdslot(&fd))) {
404 struct v_fd* fd_s = vzalloc(sizeof(*fd_s));
405 fd_s->file = opened_file;
406 fd_s->pos = file->inode->fsize & -((options & FO_APPEND) == 0);
407 __current->fdtable->fds[fd] = fd_s;
410 return SYSCALL_ESTATUS(errno);
413 __DEFINE_LXSYSCALL1(int, close, int, fd)
417 if (fd < 0 || fd >= VFS_MAX_FD || !(fd_s = __current->fdtable->fds[fd])) {
419 } else if (!(errno = vfs_close(fd_s->file))) {
421 __current->fdtable->fds[fd] = 0;
424 __current->k_status = errno;
426 return SYSCALL_ESTATUS(errno);
430 __vfs_readdir_callback(struct dir_context* dctx,
435 struct dirent* dent = (struct dirent*)dctx->cb_data;
436 strncpy(dent->d_name, name, DIRENT_NAME_MAX_LEN);
438 dent->d_type = dtype;
441 __DEFINE_LXSYSCALL2(int, readdir, int, fd, struct dirent*, dent)
445 if (fd < 0 || fd >= VFS_MAX_FD || !(fd_s = __current->fdtable->fds[fd])) {
447 } else if (!(fd_s->file->inode->itype & VFS_INODE_TYPE_DIR)) {
450 struct dir_context dctx =
451 (struct dir_context){ .cb_data = dent,
452 .index = dent->d_offset,
453 .read_complete_callback =
454 __vfs_readdir_callback };
455 if (!(errno = fd_s->file->ops.readdir(fd_s->file, &dctx))) {
460 __current->k_status = errno;
461 return SYSCALL_ESTATUS(errno);
464 __DEFINE_LXSYSCALL1(int, mkdir, const char*, path)
466 struct v_dnode *parent, *dir;
467 struct hstr component = HSTR(valloc(VFS_NAME_MAXLEN), 0);
468 int errno = vfs_walk(NULL, path, &parent, &component, VFS_WALK_PARENT);
473 if (!parent->inode->ops.mkdir) {
475 } else if (!(parent->inode->itype & VFS_INODE_TYPE_DIR)) {
479 dir->name = component;
480 if (!(errno = parent->inode->ops.mkdir(parent->inode, dir))) {
481 llist_append(&parent->children, &dir->siblings);
484 vfree(component.value);
489 __current->k_status = errno;
490 return SYSCALL_ESTATUS(errno);
493 __DEFINE_LXSYSCALL3(size_t, read, int, fd, void*, buf, size_t, count)
498 __DEFINE_LXSYSCALL3(size_t, write, int, fd, void*, buf, size_t, count)