-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.rs
416 lines (386 loc) · 13.3 KB
/
main.rs
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// vim: tw=80
use std::{
fs::Permissions,
os::unix::{fs::PermissionsExt, io::RawFd},
path::{Path, PathBuf},
process::exit,
sync::Arc,
};
use bfffs_core::{
controller::Controller,
device_manager::DevManager,
property::PropertyName,
rpc,
Error,
Result,
};
use cfg_if::cfg_if;
use clap::{crate_version, Parser};
use fuse3::{
raw::{MountHandle, Session},
MountOptions,
};
use futures::{stream::FuturesUnordered, TryFutureExt, TryStreamExt};
use nix::{
fcntl::{open, OFlag},
sys::stat::Mode,
unistd,
};
use tokio_seqpacket::{UCred, UnixSeqpacket, UnixSeqpacketListener};
use tracing::{error, warn};
use tracing_subscriber::EnvFilter;
mod fs;
use crate::fs::FuseFs;
#[derive(Parser, Clone, Debug)]
#[clap(version = crate_version!())]
struct Cli {
// TODO: configurable log level
/// Mount options, comma delimited. Apply to all BFFFS mounts
#[clap(
short = 'o',
long,
require_value_delimiter(true),
value_delimiter(',')
)]
options: Vec<String>,
#[clap(long, default_value = "/var/run/bfffsd.sock")]
sock: PathBuf,
/// Pool name
pool_name: String,
#[clap(required(true))]
devices: Vec<String>,
}
/// bfffsd's communications socket
struct Socket {
sockpath: PathBuf,
listener: UnixSeqpacketListener,
_lockfd: RawFd,
}
impl Drop for Socket {
fn drop(&mut self) {
if !std::thread::panicking() {
let _ignore = std::fs::remove_file(&self.sockpath);
self.sockpath.set_extension("lock");
let _ignore = std::fs::remove_file(&self.sockpath);
}
}
}
impl Socket {
fn new(path: &Path) -> Self {
let sockpath = path.to_owned();
let mut lockaddr = path.to_owned();
lockaddr.set_extension("lock");
let _lockfd = open(
&lockaddr,
OFlag::O_EXLOCK | OFlag::O_RDWR | OFlag::O_CREAT,
Mode::from_bits(0o600).unwrap(),
)
.unwrap_or_else(|_| {
eprintln!("Could not obtain lockfile");
std::process::exit(1);
});
let _ignore_result = std::fs::remove_file(path);
let listener = UnixSeqpacketListener::bind(path).unwrap();
std::fs::set_permissions(path, Permissions::from_mode(0o666)).unwrap();
Socket {
sockpath,
listener,
_lockfd,
}
}
}
struct Bfffsd {
controller: Controller,
_dev_manager: DevManager,
mount_opts: MountOptions,
}
impl Bfffsd {
async fn handle_client(self: Arc<Self>, peer: UnixSeqpacket) {
const BUFSIZ: usize = 4096;
let mut buf = vec![0u8; BUFSIZ];
loop {
let nread = peer.recv(&mut buf).await.unwrap();
if nread == 0 {
// Client disconnected normally
break;
} else if nread >= BUFSIZ {
warn!("Client sent unexpectedly large request");
break;
} else {
buf.truncate(nread);
let req: rpc::Request = bincode::deserialize(&buf[..]).unwrap();
let creds = peer.peer_cred().unwrap();
let resp = self.process_rpc(req, creds).await;
let encoded: Vec<u8> = bincode::serialize(&resp).unwrap();
let nwrite = peer.send(&encoded).await;
if nwrite.is_err() || nwrite.unwrap() != encoded.len() {
warn!("Client disconnected before reading response");
break;
}
}
// XXX The resize operation can be eliminated after
// tokio-seqpacket-rs gains support for Rust's read_buf feature.
buf.resize(BUFSIZ, 0);
}
}
async fn new(cli: Cli) -> Self {
let mut cache_size: Option<usize> = None;
let mut writeback_size: Option<usize> = None;
let mut mount_opts = MountOptions::default();
mount_opts.fs_name("bfffs");
if nix::unistd::getuid().is_root() {
mount_opts.allow_other(true);
mount_opts.default_permissions(true);
}
mount_opts.no_open_support(true);
mount_opts.no_open_dir_support(true);
// Unconditionally disable the kernel's buffer cache; BFFFS has its own
mount_opts.custom_options("direct_io");
for o in cli.options.iter() {
if let Some((name, value)) = o.split_once('=') {
if name == "cache_size" {
let v = value.parse().unwrap_or_else(|_| {
eprintln!("cache_size must be numeric");
exit(2);
});
cache_size = Some(v);
continue;
} else if name == "writeback_size" {
let v = value.parse().unwrap_or_else(|_| {
eprintln!("writeback_size must be numeric");
exit(2);
});
writeback_size = Some(v);
continue;
}
// else, must be a mount_fusefs option
}
// Must be a mount_fusefs option
mount_opts.custom_options(o);
}
let mut dev_manager = DevManager::default();
if let Some(cs) = cache_size {
dev_manager.cache_size(cs);
}
if let Some(wbs) = writeback_size {
dev_manager.writeback_size(wbs);
}
for dev in cli.devices.iter() {
// TODO: taste devices in parallel
dev_manager.taste(dev).await.unwrap();
}
let uuid = dev_manager
.importable_pools()
.iter()
.find(|(name, _uuid)| **name == cli.pool_name)
.unwrap_or_else(|| {
eprintln!("error: pool {} not found", cli.pool_name);
std::process::exit(1);
})
.1;
let db = dev_manager.import_by_uuid(uuid).await.unwrap();
let controller = Controller::new(db);
Bfffsd {
controller,
_dev_manager: dev_manager,
mount_opts,
}
}
#[tracing::instrument(skip(self))]
#[cfg_attr(test, allow(unused_variables))]
async fn mount(&self, name: String) -> Result<MountHandle> {
let mo2 = self.mount_opts.clone();
let mp = self
.controller
.get_prop(&name, PropertyName::Mountpoint)
.map_ok(|(prop, _source)| PathBuf::from(prop.as_str()))
.await?;
tracing::debug!("mounting {:?}", mp);
cfg_if! {
if #[cfg(test)] {
Session::new(mo2).mount(FuseFs::default(), mp)
.map_err(Error::from)
.await
} else {
self.controller.new_fs(&name)
.and_then(|fs| {
let fusefs = FuseFs::new(fs);
Session::new(mo2).mount(fusefs, mp)
.map_err(Error::from)
})
.await
}
}
}
async fn process_rpc(
&self,
req: rpc::Request,
creds: UCred,
) -> rpc::Response {
match req {
rpc::Request::DebugDropCache => {
if creds.uid() != unistd::geteuid().as_raw() {
rpc::Response::FsMount(Err(Error::EPERM))
} else {
self.controller.drop_cache();
rpc::Response::DebugDropCache(Ok(()))
}
}
rpc::Request::FsCreate(req) => {
if creds.uid() != unistd::geteuid().as_raw() {
rpc::Response::FsMount(Err(Error::EPERM))
} else {
let r = self
.controller
.create_fs(&req.name)
.and_then(|tree_id| {
req.props
.into_iter()
.map(|prop| {
self.controller.set_prop(&req.name, prop)
})
.collect::<FuturesUnordered<_>>()
.try_collect::<Vec<_>>()
.map_ok(move |_| tree_id)
})
.await;
rpc::Response::FsCreate(r)
}
}
rpc::Request::FsDestroy(req) => {
if creds.uid() != unistd::geteuid().as_raw() {
rpc::Response::FsMount(Err(Error::EPERM))
} else {
let r = self.controller.destroy_fs(&req.name).await;
rpc::Response::FsDestroy(r)
}
}
rpc::Request::FsList(req) => {
// this value of chunkqty is a guess, not well-calculated
const CHUNKQTY: usize = 64;
let mut rstream = self
.controller
.list_fs(&req.name, req.offset)
.try_chunks(CHUNKQTY);
let r = rstream
.try_next()
.map_ok(|ov| {
match ov {
Some(v) => {
v.into_iter()
.map(|de| {
rpc::fs::DsInfo {
name: de.name,
props: vec![], // TODO
offset: de.offs,
}
})
.collect::<Vec<_>>()
}
None => vec![],
}
})
.map_err(|tce| tce.1)
.await;
rpc::Response::FsList(r)
}
rpc::Request::FsMount(req) => {
if creds.uid() != unistd::geteuid().as_raw() {
rpc::Response::FsMount(Err(Error::EPERM))
} else {
match self.mount(req.name).await {
Ok(_) => rpc::Response::FsMount(Ok(())),
Err(e) => {
error!("mount: {:?}", e);
rpc::Response::FsMount(Err(e))
}
}
}
}
rpc::Request::FsUnmount(req) => {
if creds.uid() != unistd::geteuid().as_raw() {
rpc::Response::FsUnmount(Err(Error::EPERM))
} else {
match self.unmount(&req.name, req.force).await {
Ok(_) => rpc::Response::FsUnmount(Ok(())),
Err(e) => {
error!("unmount: {:?}", e);
rpc::Response::FsUnmount(Err(e))
}
}
}
}
rpc::Request::PoolClean(req) => {
if creds.uid() != unistd::geteuid().as_raw() {
rpc::Response::PoolClean(Err(Error::EPERM))
} else {
let r = self.controller.clean(&req.pool).map(drop);
rpc::Response::PoolClean(r)
}
}
}
}
async fn run(self: Arc<Self>, mut sock: Socket) {
loop {
let peer = sock.listener.accept().await.unwrap();
tokio::spawn(self.clone().handle_client(peer));
}
}
async fn unmount(&self, name: &str, force: bool) -> Result<()> {
self.controller.unmount(name, force).await
}
}
#[tokio::main(flavor = "current_thread")]
async fn main() {
tracing_subscriber::fmt()
.pretty()
.with_env_filter(EnvFilter::from_default_env())
.init();
let cli: Cli = Cli::parse();
let sock = Socket::new(&cli.sock);
let bfffsd = Arc::new(Bfffsd::new(cli).await);
bfffsd.run(sock).await;
}
#[cfg(test)]
mod t {
use clap::ErrorKind::*;
use rstest::rstest;
use super::*;
#[rstest]
#[case(Vec::new())]
#[case(vec!["bfffsd"])]
#[case(vec!["bfffsd", "testpool"])]
fn missing_arg(#[case] args: Vec<&str>) {
let e = Cli::try_parse_from(args).unwrap_err();
assert!(
e.kind() == MissingRequiredArgument ||
e.kind() == DisplayHelpOnMissingArgumentOrSubcommand
);
}
#[test]
fn options() {
let args = vec![
"bfffsd",
"-o",
"allow_other,default_permissions",
"testpool",
"--sock",
"/tmp/bfffs.sock",
"/dev/da0",
];
let cli = Cli::try_parse_from(args).unwrap();
assert_eq!(cli.pool_name, "testpool");
assert_eq!(cli.sock, Path::new("/tmp/bfffs.sock"));
assert_eq!(cli.options, vec!["allow_other", "default_permissions"]);
assert_eq!(cli.devices[0], "/dev/da0");
}
#[test]
fn plain() {
let args = vec!["bfffsd", "testpool", "/dev/da0"];
let cli = Cli::try_parse_from(args).unwrap();
assert_eq!(cli.pool_name, "testpool");
assert_eq!(cli.sock, Path::new("/var/run/bfffsd.sock"));
assert!(cli.options.is_empty());
assert_eq!(cli.devices[0], "/dev/da0");
}
}