MOS Source Code
Loading...
Searching...
No Matches
inode.c
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
4
8#include "mos/syslog/printk.h"
9
11#include <mos_stdlib.h>
12
15
17{
18 MOS_UNUSED(inode);
19 kfree(inode);
20 return true;
21}
22
23static bool inode_try_drop(inode_t *inode)
24{
25 if (inode->refcount == 0 && inode->nlinks == 0)
26 {
27 pr_dinfo2(vfs, "inode %p has 0 refcount and 0 nlinks, dropping", (void *) inode);
28
29 // drop the inode
30 mutex_acquire(&inode->cache.lock);
31 pagecache_flush_or_drop_all(&inode->cache, true);
32 mutex_release(&inode->cache.lock);
33
34 bool dropped = false;
35 if (inode->superblock->ops && inode->superblock->ops->drop_inode)
36 dropped = inode->superblock->ops->drop_inode(inode);
37 else
38 dropped = vfs_generic_inode_drop(inode);
39
40 if (!dropped)
41 pr_warn("inode %p has 0 refcount and 0 nlinks, but failed to be dropped", (void *) inode);
42
43 return dropped;
44 }
45
46 return false;
47}
48
49void inode_init(inode_t *inode, superblock_t *sb, u64 ino, file_type_t type)
50{
51 inode->superblock = sb;
52 inode->ino = ino;
53 inode->type = type;
54 inode->file_ops = NULL;
55 inode->nlinks = 1;
56 inode->perm = 0;
57 inode->private_data = NULL;
58 inode->refcount = 0;
59
61 inode->cache.owner = inode;
62 inode->cache.lock = 0;
63}
64
66{
67 inode_t *inode = kmalloc(inode_cache);
68 inode_init(inode, sb, ino, type);
69 return inode;
70}
71
72void inode_ref(inode_t *inode)
73{
74 MOS_ASSERT(inode);
75 inode->refcount++;
76}
77
78bool inode_unref(inode_t *inode)
79{
80 MOS_ASSERT(inode);
81 MOS_ASSERT(inode->refcount > 0);
82 inode->refcount--;
83 return inode_try_drop(inode);
84}
85
86bool inode_unlink(inode_t *dir, dentry_t *dentry)
87{
88 inode_t *inode = dentry->inode;
89 MOS_ASSERT(dir && inode);
90 MOS_ASSERT(inode->nlinks > 0);
91
92 inode->nlinks--;
93 bool ok = true;
94 if (dir->ops->unlink)
95 ok = dir->ops->unlink(dir, dentry);
96
97 if (!ok)
98 {
99 inode->nlinks++;
100 return false;
101 }
102
103 const bool dropped = inode_try_drop(dentry->inode);
104 MOS_ASSERT_X(!dropped, "inode %p was dropped accidentally, where dentry %p should be holding a reference", (void *) inode, (void *) dentry);
105
106 return true;
107}
#define MOS_ASSERT_X(cond, msg,...)
Definition assert.h:15
#define MOS_ASSERT(cond)
Definition assert.h:14
#define MOS_INODE_CACHE_HASHMAP_SIZE
Definition autoconf.h:20
file_type_t
Definition fs_types.h:14
MOSAPI int __pure hashmap_simple_key_compare(uintn key1, uintn key2)
MOSAPI hash_t __pure hashmap_identity_hash(uintn key)
MOSAPI void hashmap_init(hashmap_t *map, size_t capacity, hashmap_hash_t hash_func, hashmap_key_compare_t compare_func)
Definition hashmap.c:24
static bool inode_try_drop(inode_t *inode)
Definition inode.c:23
inode_t * inode_create(superblock_t *sb, u64 ino, file_type_t type)
Definition inode.c:65
static bool vfs_generic_inode_drop(inode_t *inode)
Definition inode.c:16
void inode_ref(inode_t *inode)
Definition inode.c:72
void inode_init(inode_t *inode, superblock_t *sb, u64 ino, file_type_t type)
Definition inode.c:49
bool inode_unlink(inode_t *dir, dentry_t *dentry)
Unlink a dentry from its parent inode.
Definition inode.c:86
bool inode_unref(inode_t *inode)
Definition inode.c:78
slab_t * inode_cache
Definition inode.c:13
#define MOS_UNUSED(x)
Definition mos_global.h:64
long pagecache_flush_or_drop_all(inode_cache_t *icache, bool drop_page)
Flush or drop all pages in the page cache.
Definition page_cache.c:78
#define NULL
Definition pb_syshdr.h:46
#define pr_warn(fmt,...)
Definition printk.h:38
#define pr_dinfo2(feat, fmt,...)
Definition printk.h:27
#define mutex_acquire(mutex)
Definition rpc_client.c:41
#define mutex_release(mutex)
Definition rpc_client.c:42
#define SLAB_AUTOINIT(name, var, type)
inode_t * inode
Definition vfs_types.h:117
inode_t * owner
Definition vfs_types.h:150
hashmap_t pages
Definition vfs_types.h:151
mutex_t lock
Definition vfs_types.h:149
bool(* unlink)(inode_t *dir, dentry_t *dentry)
remove a file name, this is called after nlinks is decremented
Definition vfs_types.h:85
superblock_t * superblock
Definition vfs_types.h:171
file_perm_t perm
Definition vfs_types.h:159
const inode_ops_t * ops
Definition vfs_types.h:172
file_type_t type
Definition vfs_types.h:158
const file_ops_t * file_ops
Definition vfs_types.h:173
atomic_t refcount
number of references to this inode
Definition vfs_types.h:177
inode_cache_t cache
Definition vfs_types.h:175
u64 ino
Definition vfs_types.h:157
void * private_data
Definition vfs_types.h:174
ssize_t nlinks
Definition vfs_types.h:166
Definition slab.h:45
unsigned long long u64
Definition types.h:23