-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello.rs
More file actions
224 lines (204 loc) · 5.97 KB
/
hello.rs
File metadata and controls
224 lines (204 loc) · 5.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
mod common;
use std::cell::Cell;
use std::ffi::OsStr;
use std::sync::atomic::AtomicU64;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use std::time::Duration;
use std::time::UNIX_EPOCH;
use clap::Parser;
use fuser::Errno;
use fuser::FileAttr;
use fuser::FileHandle;
use fuser::FileType;
use fuser::Filesystem;
use fuser::INodeNo;
use fuser::LockOwner;
use fuser::MountOption;
use fuser::OpenFlags;
use fuser::ReplyAttr;
use fuser::ReplyData;
use fuser::ReplyDirectory;
use fuser::ReplyEntry;
use fuser::Request;
use crate::common::args::CommonArgs;
thread_local! {
static THREAD_INDEX: Cell<Option<usize>> = const { Cell::new(None) };
}
#[derive(Parser)]
#[command(version, author = "Christopher Berner")]
struct Args {
#[clap(flatten)]
common_args: CommonArgs,
}
const TTL: Duration = Duration::from_secs(1); // 1 second
const HELLO_DIR_ATTR: FileAttr = FileAttr {
ino: INodeNo::ROOT,
size: 0,
blocks: 0,
atime: UNIX_EPOCH, // 1970-01-01 00:00:00
mtime: UNIX_EPOCH,
ctime: UNIX_EPOCH,
crtime: UNIX_EPOCH,
kind: FileType::Directory,
perm: 0o755,
nlink: 2,
uid: 501,
gid: 20,
rdev: 0,
flags: 0,
blksize: 512,
};
const HELLO_TXT_CONTENT: &str = "Hello World!\n";
const HELLO_TXT_ATTR: FileAttr = FileAttr {
ino: INodeNo(2),
size: 13,
blocks: 1,
atime: UNIX_EPOCH, // 1970-01-01 00:00:00
mtime: UNIX_EPOCH,
ctime: UNIX_EPOCH,
crtime: UNIX_EPOCH,
kind: FileType::RegularFile,
perm: 0o644,
nlink: 1,
uid: 501,
gid: 20,
rdev: 0,
flags: 0,
blksize: 512,
};
const STATS_PER_THREAD_ATTR: FileAttr = FileAttr {
ino: INodeNo(3),
size: 0, // Dynamic content, size will be determined at read time
blocks: 0,
atime: UNIX_EPOCH,
mtime: UNIX_EPOCH,
ctime: UNIX_EPOCH,
crtime: UNIX_EPOCH,
kind: FileType::RegularFile,
perm: 0o444,
nlink: 1,
uid: 501,
gid: 20,
rdev: 0,
flags: 0,
blksize: 512,
};
struct HelloFS {
reads_per_thread: Vec<AtomicU64>,
next_thread_index: AtomicUsize,
}
impl HelloFS {
fn stats_content(&self) -> String {
let mut content = String::new();
for count in &self.reads_per_thread {
content.push_str(&format!("{}\n", count.load(Ordering::Relaxed)));
}
content
}
}
impl Filesystem for HelloFS {
fn lookup(&self, _req: &Request, parent: INodeNo, name: &OsStr, reply: ReplyEntry) {
if u64::from(parent) == 1 && name.to_str() == Some("hello.txt") {
reply.entry(&TTL, &HELLO_TXT_ATTR, fuser::Generation(0));
} else if u64::from(parent) == 1 && name.to_str() == Some("stats-per-thread") {
let content = self.stats_content();
let mut attr = STATS_PER_THREAD_ATTR;
attr.size = content.len() as u64;
// Must use zero TTL, otherwise previous size is cached.
reply.entry(&Duration::ZERO, &attr, fuser::Generation(0));
} else {
reply.error(Errno::ENOENT);
}
}
fn getattr(&self, _req: &Request, ino: INodeNo, _fh: Option<FileHandle>, reply: ReplyAttr) {
match u64::from(ino) {
1 => reply.attr(&TTL, &HELLO_DIR_ATTR),
2 => reply.attr(&TTL, &HELLO_TXT_ATTR),
3 => {
let content = self.stats_content();
let mut attr = STATS_PER_THREAD_ATTR;
attr.size = content.len() as u64;
// Must use zero TTL, otherwise previous size is cached.
reply.attr(&Duration::ZERO, &attr);
}
_ => reply.error(Errno::ENOENT),
}
}
fn read(
&self,
_req: &Request,
ino: INodeNo,
_fh: FileHandle,
offset: u64,
_size: u32,
_flags: OpenFlags,
_lock_owner: Option<LockOwner>,
reply: ReplyData,
) {
let thread_idx = THREAD_INDEX.with(|idx| match idx.get() {
Some(i) => i,
None => {
let new_idx = self.next_thread_index.fetch_add(1, Ordering::SeqCst);
idx.set(Some(new_idx));
new_idx
}
});
if thread_idx < self.reads_per_thread.len() {
self.reads_per_thread[thread_idx].fetch_add(1, Ordering::Relaxed);
}
if u64::from(ino) == 2 {
reply.data(&HELLO_TXT_CONTENT.as_bytes()[offset as usize..]);
} else if u64::from(ino) == 3 {
let content = self.stats_content();
let bytes = content.as_bytes();
if offset as usize >= bytes.len() {
reply.data(&[]);
} else {
reply.data(&bytes[offset as usize..]);
}
} else {
reply.error(Errno::ENOENT);
}
}
fn readdir(
&self,
_req: &Request,
ino: INodeNo,
_fh: FileHandle,
offset: u64,
mut reply: ReplyDirectory,
) {
if u64::from(ino) != 1 {
reply.error(Errno::ENOENT);
return;
}
let entries = vec![
(1, FileType::Directory, "."),
(1, FileType::Directory, ".."),
(2, FileType::RegularFile, "hello.txt"),
(3, FileType::RegularFile, "stats-per-thread"),
];
for (i, entry) in entries.into_iter().enumerate().skip(offset as usize) {
// i + 1 means the index of the next entry
if reply.add(INodeNo(entry.0), (i + 1) as u64, entry.1, entry.2) {
break;
}
}
reply.ok();
}
}
fn main() {
let args = Args::parse();
env_logger::init();
let mut cfg = args.common_args.config();
cfg.mount_options
.extend([MountOption::RO, MountOption::FSName("hello".to_string())]);
let fs = HelloFS {
reads_per_thread: (0..args.common_args.n_threads)
.map(|_| AtomicU64::new(0))
.collect(),
next_thread_index: AtomicUsize::new(0),
};
fuser::mount(fs, &args.common_args.mount_point, &cfg).unwrap();
}