MOS Source Code
Loading...
Searching...
No Matches
inode.cpp
Go to the documentation of this file.
1
// SPDX-License-Identifier: GPL-3.0-or-later
2
3
#include "
mos/filesystem/inode.hpp
"
4
5
#include "
mos/filesystem/page_cache.hpp
"
6
#include "
mos/filesystem/vfs_types.hpp
"
7
#include "
mos/syslog/printk.hpp
"
8
9
#include <
mos/lib/structures/hashmap_common.hpp
>
10
#include <
mos_stdlib.hpp
>
11
12
static
bool
vfs_generic_inode_drop
(
inode_t
*inode)
13
{
14
MOS_UNUSED
(inode);
15
delete
inode;
16
return
true
;
17
}
18
19
static
bool
inode_try_drop
(
inode_t
*inode)
20
{
21
if
(inode->
refcount
== 0 && inode->
nlinks
== 0)
22
{
23
pr_dinfo2
(vfs,
"inode %p has 0 refcount and 0 nlinks, dropping"
, (
void
*) inode);
24
25
// drop the inode
26
mutex_acquire
(&inode->cache.lock);
27
pagecache_flush_or_drop_all
(&inode->cache,
true
);
28
mutex_release
(&inode->cache.lock);
29
30
bool
dropped =
false
;
31
if
(inode->superblock->ops && inode->superblock->ops->drop_inode)
32
dropped = inode->superblock->ops->drop_inode(inode);
33
else
34
dropped =
vfs_generic_inode_drop
(inode);
35
36
if
(!dropped)
37
pr_warn
(
"inode %p has 0 refcount and 0 nlinks, but failed to be dropped"
, (
void
*) inode);
38
39
return
dropped;
40
}
41
42
return
false
;
43
}
44
45
void
inode_init
(
inode_t
*inode,
superblock_t
*sb,
u64
ino,
file_type_t
type)
46
{
47
inode->
superblock
= sb;
48
inode->
ino
= ino;
49
inode->
type
= type;
50
inode->
file_ops
=
NULL
;
51
inode->
nlinks
= 1;
52
inode->
perm
= 0;
53
inode->
private_data
=
NULL
;
54
inode->
refcount
= 0;
55
inode->
cache
.
owner
= inode;
56
inode->
cache
.
lock
= 0;
57
}
58
59
inode_t
*
inode_create
(
superblock_t
*sb,
u64
ino,
file_type_t
type)
60
{
61
inode_t
*inode =
mos::create<inode_t>
();
62
inode_init
(inode, sb, ino, type);
63
return
inode;
64
}
65
66
void
inode_ref
(
inode_t
*inode)
67
{
68
MOS_ASSERT
(inode);
69
inode->
refcount
++;
70
}
71
72
bool
inode_unref
(
inode_t
*inode)
73
{
74
MOS_ASSERT
(inode);
75
MOS_ASSERT
(inode->
refcount
> 0);
76
inode->
refcount
--;
77
return
inode_try_drop
(inode);
78
}
79
80
bool
inode_unlink
(
inode_t
*dir,
dentry_t
*dentry)
81
{
82
inode_t
*inode = dentry->
inode
;
83
MOS_ASSERT
(dir && inode);
84
MOS_ASSERT
(inode->
nlinks
> 0);
85
86
inode->
nlinks
--;
87
bool
ok =
true
;
88
if
(dir->
ops
->
unlink
)
89
ok = dir->
ops
->
unlink
(dir, dentry);
90
91
if
(!ok)
92
{
93
inode->
nlinks
++;
94
return
false
;
95
}
96
97
const
bool
dropped =
inode_try_drop
(dentry->
inode
);
98
MOS_ASSERT_X
(!dropped,
"inode %p was dropped accidentally, where dentry %p should be holding a reference"
, (
void
*) inode, (
void
*) dentry);
99
100
return
true
;
101
}
MOS_ASSERT_X
#define MOS_ASSERT_X(cond, msg,...)
Definition
assert.hpp:15
MOS_ASSERT
#define MOS_ASSERT(cond)
Definition
assert.hpp:14
file_type_t
file_type_t
Definition
fs_types.h:14
hashmap_common.hpp
inode_try_drop
static bool inode_try_drop(inode_t *inode)
Definition
inode.cpp:19
inode_create
inode_t * inode_create(superblock_t *sb, u64 ino, file_type_t type)
Definition
inode.cpp:59
vfs_generic_inode_drop
static bool vfs_generic_inode_drop(inode_t *inode)
Definition
inode.cpp:12
inode_ref
void inode_ref(inode_t *inode)
Definition
inode.cpp:66
inode_init
void inode_init(inode_t *inode, superblock_t *sb, u64 ino, file_type_t type)
Definition
inode.cpp:45
inode_unlink
bool inode_unlink(inode_t *dir, dentry_t *dentry)
Unlink a dentry from its parent inode.
Definition
inode.cpp:80
inode_unref
bool inode_unref(inode_t *inode)
Definition
inode.cpp:72
inode.hpp
MOS_UNUSED
#define MOS_UNUSED(x)
Definition
mos_global.h:65
mos_stdlib.hpp
mos::create
T * create(Args &&...args)
Definition
allocator.hpp:10
pagecache_flush_or_drop_all
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.cpp:76
page_cache.hpp
NULL
#define NULL
Definition
pb_syshdr.h:46
printk.hpp
pr_warn
#define pr_warn(fmt,...)
Definition
printk.hpp:38
pr_dinfo2
#define pr_dinfo2(feat, fmt,...)
Definition
printk.hpp:27
mutex_acquire
#define mutex_acquire(mutex)
Definition
rpc_client.cpp:42
mutex_release
#define mutex_release(mutex)
Definition
rpc_client.cpp:43
dentry_t
Definition
vfs_types.hpp:114
dentry_t::inode
inode_t * inode
Definition
vfs_types.hpp:118
inode_cache_t::owner
inode_t * owner
Definition
vfs_types.hpp:151
inode_cache_t::lock
mutex_t lock
Definition
vfs_types.hpp:150
inode_ops_t::unlink
bool(* unlink)(inode_t *dir, dentry_t *dentry)
remove a file name, this is called after nlinks is decremented
Definition
vfs_types.hpp:86
inode_t
Definition
vfs_types.hpp:157
inode_t::superblock
superblock_t * superblock
Definition
vfs_types.hpp:172
inode_t::perm
file_perm_t perm
Definition
vfs_types.hpp:160
inode_t::ops
const inode_ops_t * ops
Definition
vfs_types.hpp:173
inode_t::type
file_type_t type
Definition
vfs_types.hpp:159
inode_t::file_ops
const file_ops_t * file_ops
Definition
vfs_types.hpp:174
inode_t::refcount
atomic_t refcount
number of references to this inode
Definition
vfs_types.hpp:178
inode_t::cache
inode_cache_t cache
Definition
vfs_types.hpp:176
inode_t::ino
u64 ino
Definition
vfs_types.hpp:158
inode_t::private_data
void * private_data
Definition
vfs_types.hpp:175
inode_t::nlinks
ssize_t nlinks
Definition
vfs_types.hpp:167
superblock_t
Definition
vfs_types.hpp:107
u64
unsigned long long u64
Definition
types.h:19
vfs_types.hpp
kernel
filesystem
inode.cpp
Generated on Tue Feb 18 2025 16:41:40 for MOS Source Code by
1.13.2