feat: simple read/write lock implementation
[lunaix-os.git] / lunaix-os / kernel / fs / vfs.c
index eaf35e03087e963dea8c5509dc543f8c43b40798..c9431bac944992c215230aa31861699e5916ee91 100644 (file)
@@ -254,12 +254,35 @@ vfs_link(struct v_dnode* to_link, struct v_dnode* name)
 }
 
 int
 }
 
 int
-vfs_close(struct v_file* file)
+vfs_pclose(struct v_file* file, pid_t pid)
 {
     int errno = 0;
 {
     int errno = 0;
-    if (!(errno = file->ops->close(file))) {
+    if (file->ref_count > 1) {
+        atomic_fetch_sub(&file->ref_count, 1);
+    } else if (!(errno = file->ops->close(file))) {
         atomic_fetch_sub(&file->dnode->ref_count, 1);
         file->inode->open_count--;
         atomic_fetch_sub(&file->dnode->ref_count, 1);
         file->inode->open_count--;
+
+        // Prevent dead lock.
+        // This happened when process is terminated while blocking on read.
+        // In that case, the process is still holding the inode lock and it will
+        // never get released.
+        /*
+         * The unlocking should also include ownership check.
+         *
+         * To see why, consider two process both open the same file both with
+         * fd=x.
+         *      Process A: busy on reading x
+         *      Process B: do nothing with x
+         * Assuming that, after a very short time, process B get terminated
+         * while process A is still busy in it's reading business. By this
+         * design, the inode lock of this file x is get released by B rather
+         * than A. And this will cause a probable race condition on A if other
+         * process is writing to this file later after B exit.
+         */
+        if (mutex_on_hold(&file->inode->lock)) {
+            mutex_unlock_for(&file->inode->lock, pid);
+        }
         mnt_chillax(file->dnode->mnt);
 
         pcache_commit_all(file->inode);
         mnt_chillax(file->dnode->mnt);
 
         pcache_commit_all(file->inode);
@@ -268,6 +291,12 @@ vfs_close(struct v_file* file)
     return errno;
 }
 
     return errno;
 }
 
+int
+vfs_close(struct v_file* file)
+{
+    return vfs_pclose(file, __current->pid);
+}
+
 int
 vfs_fsync(struct v_file* file)
 {
 int
 vfs_fsync(struct v_file* file)
 {
@@ -561,9 +590,7 @@ __DEFINE_LXSYSCALL1(int, close, int, fd)
         goto done_err;
     }
 
         goto done_err;
     }
 
-    if (fd_s->file->ref_count > 1) {
-        fd_s->file->ref_count--;
-    } else if ((errno = vfs_close(fd_s->file))) {
+    if ((errno = vfs_close(fd_s->file))) {
         goto done_err;
     }
 
         goto done_err;
     }
 
@@ -609,9 +636,9 @@ __DEFINE_LXSYSCALL2(int, readdir, int, fd, struct dirent*, dent)
                                   __vfs_readdir_callback };
         errno = 1;
         if (dent->d_offset == 0) {
                                   __vfs_readdir_callback };
         errno = 1;
         if (dent->d_offset == 0) {
-            __vfs_readdir_callback(&dctx, vfs_dot.value, vfs_dot.len, 0);
+            __vfs_readdir_callback(&dctx, vfs_dot.value, vfs_dot.len, DT_DIR);
         } else if (dent->d_offset == 1) {
         } else if (dent->d_offset == 1) {
-            __vfs_readdir_callback(&dctx, vfs_ddot.value, vfs_ddot.len, 0);
+            __vfs_readdir_callback(&dctx, vfs_ddot.value, vfs_ddot.len, DT_DIR);
         } else {
             dctx.index -= 2;
             if ((errno = fd_s->file->ops->readdir(fd_s->file, &dctx)) != 1) {
         } else {
             dctx.index -= 2;
             if ((errno = fd_s->file->ops->readdir(fd_s->file, &dctx)) != 1) {
@@ -687,13 +714,11 @@ __DEFINE_LXSYSCALL3(int, write, int, fd, void*, buf, size_t, count)
 
     file->inode->mtime = clock_unixtime();
 
 
     file->inode->mtime = clock_unixtime();
 
-    __SYSCALL_INTERRUPTIBLE({
-        if ((file->inode->itype & VFS_IFSEQDEV) || (fd_s->flags & FO_DIRECT)) {
-            errno = file->ops->write(file->inode, buf, count, file->f_pos);
-        } else {
-            errno = pcache_write(file->inode, buf, count, file->f_pos);
-        }
-    })
+    if ((file->inode->itype & VFS_IFSEQDEV) || (fd_s->flags & FO_DIRECT)) {
+        errno = file->ops->write(file->inode, buf, count, file->f_pos);
+    } else {
+        errno = pcache_write(file->inode, buf, count, file->f_pos);
+    }
 
     if (errno > 0) {
         file->f_pos += errno;
 
     if (errno > 0) {
         file->f_pos += errno;
@@ -753,7 +778,7 @@ done:
 int
 vfs_get_path(struct v_dnode* dnode, char* buf, size_t size, int depth)
 {
 int
 vfs_get_path(struct v_dnode* dnode, char* buf, size_t size, int depth)
 {
-    if (!dnode || dnode->parent == dnode) {
+    if (!dnode) {
         return 0;
     }
 
         return 0;
     }
 
@@ -761,13 +786,19 @@ vfs_get_path(struct v_dnode* dnode, char* buf, size_t size, int depth)
         return ENAMETOOLONG;
     }
 
         return ENAMETOOLONG;
     }
 
-    size_t len = vfs_get_path(dnode->parent, buf, size, depth + 1);
+    size_t len = 0;
+
+    if (dnode->parent != dnode) {
+        len = vfs_get_path(dnode->parent, buf, size, depth + 1);
+    }
 
     if (len >= size) {
         return len;
     }
 
 
     if (len >= size) {
         return len;
     }
 
-    buf[len++] = VFS_PATH_DELIM;
+    if (!len || buf[len - 1] != VFS_PATH_DELIM) {
+        buf[len++] = VFS_PATH_DELIM;
+    }
 
     size_t cpy_size = MIN(dnode->name.len, size - len);
     strncpy(buf + len, dnode->name.value, cpy_size);
 
     size_t cpy_size = MIN(dnode->name.len, size - len);
     strncpy(buf + len, dnode->name.value, cpy_size);
@@ -793,6 +824,19 @@ vfs_readlink(struct v_dnode* dnode, char* buf, size_t size)
     return 0;
 }
 
     return 0;
 }
 
+int
+vfs_get_dtype(int itype)
+{
+    switch (itype) {
+        case VFS_IFDIR:
+            return DT_DIR;
+        case VFS_IFSYMLINK:
+            return DT_SYMLINK;
+        default:
+            return DT_PIPE;
+    }
+}
+
 __DEFINE_LXSYSCALL3(int, realpathat, int, fd, char*, buf, size_t, size)
 {
     int errno;
 __DEFINE_LXSYSCALL3(int, realpathat, int, fd, char*, buf, size_t, size)
 {
     int errno;
@@ -1162,8 +1206,22 @@ done:
     return DO_STATUS(errno);
 }
 
     return DO_STATUS(errno);
 }
 
+void
+vfs_ref_dnode(struct v_dnode* dnode)
+{
+    atomic_fetch_add(&dnode->ref_count, 1);
+    mnt_mkbusy(dnode->mnt);
+}
+
+void
+vfs_unref_dnode(struct v_dnode* dnode)
+{
+    atomic_fetch_sub(&dnode->ref_count, 1);
+    mnt_chillax(dnode->mnt);
+}
+
 int
 int
-__vfs_do_chdir(struct v_dnode* dnode)
+vfs_do_chdir(struct proc_info* proc, struct v_dnode* dnode)
 {
     int errno = 0;
 
 {
     int errno = 0;
 
@@ -1174,14 +1232,12 @@ __vfs_do_chdir(struct v_dnode* dnode)
         goto done;
     }
 
         goto done;
     }
 
-    if (__current->cwd) {
-        atomic_fetch_sub(&__current->cwd->ref_count, 1);
-        mnt_chillax(__current->cwd->mnt);
+    if (proc->cwd) {
+        vfs_unref_dnode(proc->cwd);
     }
 
     }
 
-    atomic_fetch_add(&dnode->ref_count, 1);
-    mnt_mkbusy(dnode->mnt);
-    __current->cwd = dnode;
+    vfs_ref_dnode(dnode);
+    proc->cwd = dnode;
 
     unlock_dnode(dnode);
 
 
     unlock_dnode(dnode);
 
@@ -1198,7 +1254,7 @@ __DEFINE_LXSYSCALL1(int, chdir, const char*, path)
         goto done;
     }
 
         goto done;
     }
 
-    errno = __vfs_do_chdir(dnode);
+    errno = vfs_do_chdir(__current, dnode);
 
 done:
     return DO_STATUS(errno);
 
 done:
     return DO_STATUS(errno);
@@ -1213,7 +1269,7 @@ __DEFINE_LXSYSCALL1(int, fchdir, int, fd)
         goto done;
     }
 
         goto done;
     }
 
-    errno = __vfs_do_chdir(fd_s->file->dnode);
+    errno = vfs_do_chdir(__current, fd_s->file->dnode);
 
 done:
     return DO_STATUS(errno);
 
 done:
     return DO_STATUS(errno);