Fix file system racing and ext2 directory insertion (#58)
[lunaix-os.git] / lunaix-os / includes / lunaix / fs.h
index a33b1d0fed3c9f0c0123d2723d1dcb2b228b9ccc..003f41058248e490a6db4cf1a33f7ee5dd93870b 100644 (file)
@@ -1,8 +1,6 @@
 #ifndef __LUNAIX_VFS_H
 #define __LUNAIX_VFS_H
 
-#include <lunaix/clock.h>
-#include <lunaix/device.h>
 #include <lunaix/ds/btrie.h>
 #include <lunaix/ds/hashtable.h>
 #include <lunaix/ds/hstr.h>
 #include <lunaix/ds/llist.h>
 #include <lunaix/ds/lru.h>
 #include <lunaix/ds/mutex.h>
+#include <lunaix/ds/rwlock.h>
+
+#include <lunaix/clock.h>
+#include <lunaix/device.h>
 #include <lunaix/status.h>
 #include <lunaix/spike.h>
 #include <lunaix/bcache.h>
-
-#include <stdatomic.h>
+#include <lunaix/fs_acl.h>
 
 #include <usr/lunaix/fstypes.h>
 
      ('0' <= (chr) && (chr) <= '9') || (chr) == '.' || (chr) == '_' ||         \
      (chr) == '-' || (chr) == ':')
 
-#define unlock_inode(inode) mutex_unlock(&inode->lock)
+#define unlock_inode(inode) mutex_unlock_nested(&inode->lock)
 #define lock_inode(inode)                                                      \
     ({                                                                         \
-        mutex_lock(&inode->lock);                                              \
+        mutex_lock_nested(&inode->lock);                                       \
         lru_use_one(inode_lru, &inode->lru);                                   \
     })
 
-#define unlock_dnode(dnode) mutex_unlock(&dnode->lock)
+#define unlock_dnode(dnode) mutex_unlock_nested(&dnode->lock)
 #define lock_dnode(dnode)                                                      \
     ({                                                                         \
-        mutex_lock(&dnode->lock);                                              \
+        mutex_lock_nested(&dnode->lock);                                       \
         lru_use_one(dnode_lru, &dnode->lru);                                   \
     })
 
+#define dnode_atomic(dnode, ops)    \
+    do { lock_dnode(dnode); ops; unlock_dnode(dnode); } while(0)
+
+#define locked_node(node) mutex_on_hold(&(node)->lock)
+
 #define assert_fs(cond) assert_p(cond, "FS")
 #define fail_fs(msg) fail_p(msg, "FS")
 
@@ -118,6 +124,28 @@ struct fs_iter
     struct filesystem* fs;
 };
 
+struct vncache
+{
+    struct hbucket* pool;
+    rwlock_t lock;
+};
+#define cache_atomic_read(cache, ops)           \
+    do {                                        \
+        rwlock_begin_read(&(cache)->lock);      \
+        ops;                                    \
+        rwlock_end_read(&(cache)->lock);        \
+    } while (0)
+
+#define cache_atomic_write(cache, ops)          \
+    do {                                        \
+        rwlock_begin_write(&(cache)->lock);     \
+        ops;                                    \
+        rwlock_end_write(&(cache)->lock);       \
+    } while (0)
+
+#define dnode_cache(dnode)   (&(dnode)->super_block->d_cache)
+#define inode_cache(inode)   (&(inode)->sb->i_cache)
+
 struct v_superblock
 {
     struct llist_header sb_list;
@@ -125,7 +153,9 @@ struct v_superblock
     struct v_dnode* root;
     struct filesystem* fs;
     struct blkbuf_cache* blks;
-    struct hbucket* i_cache;
+    struct vncache i_cache;
+    struct vncache d_cache;
+    
     void* data;
     unsigned int ref_count;
     size_t blksize;
@@ -211,7 +241,7 @@ struct v_file
     struct v_dnode* dnode;
     struct llist_header* f_list;
     u32_t f_pos;
-    atomic_ulong ref_count;
+    unsigned long ref_count;
     void* data;
     struct v_file_ops* ops; // for caching
 };
@@ -248,6 +278,11 @@ struct v_inode
     u32_t link_count;
     u32_t lb_usage;
     u32_t fsize;
+
+    u32_t acl;
+    uid_t uid;
+    gid_t gid;
+
     void* data; // 允许底层FS绑定他的一些专有数据
     struct llist_header aka_dnodes;
     struct llist_header xattrs;
@@ -285,9 +320,11 @@ struct v_dnode
     struct llist_header aka_list;
     struct llist_header children;
     struct llist_header siblings;
+    
     struct v_superblock* super_block;
     struct v_mount* mnt;
-    atomic_ulong ref_count;
+    
+    unsigned long ref_count;
 
     void* data;
 
@@ -297,7 +334,10 @@ struct v_dnode
 struct v_fdtable
 {
     struct v_fd* fds[VFS_MAX_FD];
+    mutex_t lock;   // inter-threads contention
 };
+#define lock_fdtable(fdtab)     mutex_lock(&(fdtab)->lock)
+#define unlock_fdtable(fdtab)   mutex_unlock(&(fdtab)->lock)
 
 struct pcache
 {
@@ -356,6 +396,21 @@ fsm_itend(struct fs_iter* iterator)
     iterator->fs = NULL;
 }
 
+void
+vfs_vncache_init(struct vncache* cache);
+
+void
+vfs_vncache_free(struct vncache* cache);
+
+void
+vfs_vncache_add(struct vncache* cache, size_t key, struct hlist_node* node);
+
+#define vncache_lock_read(cache)    rwlock_begin_read(&(cache)->lock);
+#define vncache_unlock_read(cache)  rwlock_end_read(&(cache)->lock);
+
+#define vncache_lock_write(cache)    rwlock_begin_write(&(cache)->lock);
+#define vncache_unlock_write(cache)  rwlock_end_write(&(cache)->lock);
+
 void
 vfs_init();
 
@@ -387,6 +442,10 @@ vfs_walk_proc(const char* path,
               struct hstr* component,
               int options);
 
+int
+vfs_walkat(int fd, const char* path, int at_opts, 
+           struct v_dnode** dnode_out);
+
 int
 vfs_mount(const char* target,
           const char* fs_name,
@@ -430,15 +489,15 @@ struct v_superblock*
 vfs_sb_alloc();
 
 void
-vfs_sb_free(struct v_superblock* sb);
+vfs_sb_unref(struct v_superblock* sb);
 
 void
 vfs_sb_ref(struct v_superblock* sb);
 
 #define vfs_assign_sb(sb_accessor, sb)      \
     ({                                      \
-        if (sb_accessor) {                \
-            vfs_sb_free(sb_accessor);       \
+        if (likely(sb_accessor)) {          \
+            vfs_sb_unref(sb_accessor);       \
         }                                   \
         vfs_sb_ref(((sb_accessor) = (sb))); \
     })
@@ -455,6 +514,20 @@ vfs_d_assign_sb(struct v_dnode* dnode, struct v_superblock* sb)
     vfs_assign_sb(dnode->super_block, sb);
 }
 
+static inline void
+vfs_d_assign_vmnt(struct v_dnode* dnode, struct v_mount* vmnt)
+{
+    if (dnode->mnt) {
+        assert_msg(dnode->mnt->mnt_point != dnode, 
+                    "vmnt must be detached first");
+    }
+
+    dnode->mnt = vmnt;
+
+    if (likely(vmnt))
+        vfs_d_assign_sb(dnode, vmnt->super_block);
+}
+
 static inline void
 vfs_vmnt_assign_sb(struct v_mount* vmnt, struct v_superblock* sb)
 {
@@ -462,7 +535,7 @@ vfs_vmnt_assign_sb(struct v_mount* vmnt, struct v_superblock* sb)
 }
 
 struct v_dnode*
-vfs_d_alloc();
+vfs_d_alloc(struct v_dnode* parent, struct hstr* name);
 
 void
 vfs_d_free(struct v_dnode* dnode);
@@ -488,15 +561,6 @@ vfs_getfd(int fd, struct v_fd** fd_s);
 int
 vfs_get_dtype(int itype);
 
-void
-vfs_ref_dnode(struct v_dnode* dnode);
-
-void
-vfs_ref_file(struct v_file* file);
-
-void
-vfs_unref_dnode(struct v_dnode* dnode);
-
 int
 vfs_get_path(struct v_dnode* dnode, char* buf, size_t size, int depth);
 
@@ -540,8 +604,49 @@ mnt_chillax(struct v_mount* mnt);
 int
 vfs_mount_root(const char* fs_name, struct device* device);
 
-struct v_mount*
-vfs_create_mount(struct v_mount* parent, struct v_dnode* mnt_point);
+static inline bool
+mnt_check_busy(struct v_mount* mnt)
+{
+    return mnt->busy_counter > 1;
+}
+
+static inline void
+vfs_ref_dnode(struct v_dnode* dnode)
+{
+    dnode->ref_count++;
+    
+    if (likely(dnode->mnt)) {
+        mnt_mkbusy(dnode->mnt);
+    }
+}
+
+static inline void
+vfs_unref_dnode(struct v_dnode* dnode)
+{
+    dnode->ref_count--;
+
+    if (likely(dnode->mnt)) {
+        mnt_chillax(dnode->mnt);
+    }
+}
+
+static inline void
+vfs_ref_file(struct v_file* file)
+{
+    file->ref_count++;
+}
+
+static inline void
+vfs_unref_file(struct v_file* file)
+{
+    file->ref_count--;
+}
+
+static inline bool
+vfs_check_duped_file(struct v_file* file)
+{
+    return file->ref_count > 1;
+}
 
 int
 vfs_check_writable(struct v_dnode* dnode);
@@ -597,6 +702,18 @@ void
 xattr_addcache(struct v_inode* inode, struct v_xattr_entry* xattr);
 
 
+/* --- fdtable --- */
+
+struct v_fdtable*
+fdtable_create();
+
+void
+fdtable_copy(struct v_fdtable* dest, struct v_fdtable* src);
+
+void
+fdtable_free(struct v_fdtable* table);
+
+
 /* --- misc stuff --- */
 
 #define check_itype(to_check, itype)    \
@@ -659,4 +776,28 @@ check_symlink_node(struct v_inode* inode)
     return check_itype(inode->itype, VFS_IFSYMLINK);
 }
 
+static inline bool
+check_allow_ops(struct v_inode* inode, unsigned int perm)
+{
+    return fsacl_allow_ops(perm, inode->acl, inode->uid, inode->gid);
+}
+
+static inline bool
+check_allow_read(struct v_inode* inode)
+{
+    return check_allow_ops(inode, FSACL_aR);
+}
+
+static inline bool
+check_allow_write(struct v_inode* inode)
+{
+    return check_allow_ops(inode, FSACL_aW);
+}
+
+static inline bool
+check_allow_execute(struct v_inode* inode)
+{
+    return check_allow_ops(inode, FSACL_aX);
+}
+
 #endif /* __LUNAIX_VFS_H */