1use crate::ll::{fuse_abi as abi, Errno, Response};
9use log::{debug, error, warn};
10use std::convert::TryFrom;
11#[cfg(feature = "abi-7-28")]
12use std::convert::TryInto;
13use std::path::Path;
14
15use crate::channel::ChannelSender;
16use crate::ll::Request as _;
17#[cfg(feature = "abi-7-21")]
18use crate::reply::ReplyDirectoryPlus;
19use crate::reply::{Reply, ReplyDirectory, ReplySender};
20use crate::session::{Session, SessionACL};
21use crate::Filesystem;
22use crate::{ll, KernelConfig};
23
24#[derive(Debug)]
26pub struct Request<'a> {
27 ch: ChannelSender,
29 #[allow(unused)]
31 data: &'a [u8],
32 request: ll::AnyRequest<'a>,
34}
35
36impl<'a> Request<'a> {
37 pub(crate) fn new(ch: ChannelSender, data: &'a [u8]) -> Option<Request<'a>> {
39 let request = match ll::AnyRequest::try_from(data) {
40 Ok(request) => request,
41 Err(err) => {
42 error!("{}", err);
43 return None;
44 }
45 };
46
47 Some(Self { ch, data, request })
48 }
49
50 pub(crate) fn dispatch<FS: Filesystem>(&self, se: &mut Session<FS>) {
54 debug!("{}", self.request);
55 let unique = self.request.unique();
56
57 let res = match self.dispatch_req(se) {
58 Ok(Some(resp)) => resp,
59 Ok(None) => return,
60 Err(errno) => self.request.reply_err(errno),
61 }
62 .with_iovec(unique, |iov| self.ch.send(iov));
63
64 if let Err(err) = res {
65 warn!("Request {:?}: Failed to send reply: {}", unique, err)
66 }
67 }
68
69 fn dispatch_req<FS: Filesystem>(
70 &self,
71 se: &mut Session<FS>,
72 ) -> Result<Option<Response<'_>>, Errno> {
73 let op = self.request.operation().map_err(|_| Errno::ENOSYS)?;
74 if (se.allowed == SessionACL::RootAndOwner
76 && self.request.uid() != se.session_owner
77 && self.request.uid() != 0)
78 || (se.allowed == SessionACL::Owner && self.request.uid() != se.session_owner)
79 {
80 #[cfg(feature = "abi-7-21")]
81 {
82 match op {
83 ll::Operation::Init(_)
85 | ll::Operation::Destroy(_)
86 | ll::Operation::Read(_)
87 | ll::Operation::ReadDir(_)
88 | ll::Operation::ReadDirPlus(_)
89 | ll::Operation::BatchForget(_)
90 | ll::Operation::Forget(_)
91 | ll::Operation::Write(_)
92 | ll::Operation::FSync(_)
93 | ll::Operation::FSyncDir(_)
94 | ll::Operation::Release(_)
95 | ll::Operation::ReleaseDir(_) => {}
96 _ => {
97 return Err(Errno::EACCES);
98 }
99 }
100 }
101 #[cfg(all(feature = "abi-7-16", not(feature = "abi-7-21")))]
102 {
103 match op {
104 ll::Operation::Init(_)
106 | ll::Operation::Destroy(_)
107 | ll::Operation::Read(_)
108 | ll::Operation::ReadDir(_)
109 | ll::Operation::BatchForget(_)
110 | ll::Operation::Forget(_)
111 | ll::Operation::Write(_)
112 | ll::Operation::FSync(_)
113 | ll::Operation::FSyncDir(_)
114 | ll::Operation::Release(_)
115 | ll::Operation::ReleaseDir(_) => {}
116 _ => {
117 return Err(Errno::EACCES);
118 }
119 }
120 }
121 #[cfg(not(feature = "abi-7-16"))]
122 {
123 match op {
124 ll::Operation::Init(_)
126 | ll::Operation::Destroy(_)
127 | ll::Operation::Read(_)
128 | ll::Operation::ReadDir(_)
129 | ll::Operation::Forget(_)
130 | ll::Operation::Write(_)
131 | ll::Operation::FSync(_)
132 | ll::Operation::FSyncDir(_)
133 | ll::Operation::Release(_)
134 | ll::Operation::ReleaseDir(_) => {}
135 _ => {
136 return Err(Errno::EACCES);
137 }
138 }
139 }
140 }
141 match op {
142 ll::Operation::Init(x) => {
144 let v = x.version();
146 if v < ll::Version(7, 6) {
147 error!("Unsupported FUSE ABI version {}", v);
148 return Err(Errno::EPROTO);
149 }
150 se.proto_major = v.major();
152 se.proto_minor = v.minor();
153
154 let mut config = KernelConfig::new(x.capabilities(), x.max_readahead());
155 se.filesystem
157 .init(self, &mut config)
158 .map_err(Errno::from_i32)?;
159
160 debug!(
164 "INIT response: ABI {}.{}, flags {:#x}, max readahead {}, max write {}",
165 abi::FUSE_KERNEL_VERSION,
166 abi::FUSE_KERNEL_MINOR_VERSION,
167 x.capabilities() & config.requested,
168 config.max_readahead,
169 config.max_write
170 );
171 se.initialized = true;
172 return Ok(Some(x.reply(&config)));
173 }
174 _ if !se.initialized => {
176 warn!("Ignoring FUSE operation before init: {}", self.request);
177 return Err(Errno::EIO);
178 }
179 ll::Operation::Destroy(x) => {
181 se.filesystem.destroy();
182 se.destroyed = true;
183 return Ok(Some(x.reply()));
184 }
185 _ if se.destroyed => {
187 warn!("Ignoring FUSE operation after destroy: {}", self.request);
188 return Err(Errno::EIO);
189 }
190
191 ll::Operation::Interrupt(_) => {
192 return Err(Errno::ENOSYS);
194 }
195
196 ll::Operation::Lookup(x) => {
197 se.filesystem.lookup(
198 self,
199 self.request.nodeid().into(),
200 x.name().as_ref(),
201 self.reply(),
202 );
203 }
204 ll::Operation::Forget(x) => {
205 se.filesystem
206 .forget(self, self.request.nodeid().into(), x.nlookup()); }
208 ll::Operation::GetAttr(_) => {
209 se.filesystem
210 .getattr(self, self.request.nodeid().into(), self.reply());
211 }
212 ll::Operation::SetAttr(x) => {
213 se.filesystem.setattr(
214 self,
215 self.request.nodeid().into(),
216 x.mode(),
217 x.uid(),
218 x.gid(),
219 x.size(),
220 x.atime(),
221 x.mtime(),
222 x.ctime(),
223 x.file_handle().map(|fh| fh.into()),
224 x.crtime(),
225 x.chgtime(),
226 x.bkuptime(),
227 x.flags(),
228 self.reply(),
229 );
230 }
231 ll::Operation::ReadLink(_) => {
232 se.filesystem
233 .readlink(self, self.request.nodeid().into(), self.reply());
234 }
235 ll::Operation::MkNod(x) => {
236 se.filesystem.mknod(
237 self,
238 self.request.nodeid().into(),
239 x.name().as_ref(),
240 x.mode(),
241 x.umask(),
242 x.rdev(),
243 self.reply(),
244 );
245 }
246 ll::Operation::MkDir(x) => {
247 se.filesystem.mkdir(
248 self,
249 self.request.nodeid().into(),
250 x.name().as_ref(),
251 x.mode(),
252 x.umask(),
253 self.reply(),
254 );
255 }
256 ll::Operation::Unlink(x) => {
257 se.filesystem.unlink(
258 self,
259 self.request.nodeid().into(),
260 x.name().as_ref(),
261 self.reply(),
262 );
263 }
264 ll::Operation::RmDir(x) => {
265 se.filesystem.rmdir(
266 self,
267 self.request.nodeid().into(),
268 x.name().as_ref(),
269 self.reply(),
270 );
271 }
272 ll::Operation::SymLink(x) => {
273 se.filesystem.symlink(
274 self,
275 self.request.nodeid().into(),
276 x.link_name().as_ref(),
277 Path::new(x.target()),
278 self.reply(),
279 );
280 }
281 ll::Operation::Rename(x) => {
282 se.filesystem.rename(
283 self,
284 self.request.nodeid().into(),
285 x.src().name.as_ref(),
286 x.dest().dir.into(),
287 x.dest().name.as_ref(),
288 0,
289 self.reply(),
290 );
291 }
292 ll::Operation::Link(x) => {
293 se.filesystem.link(
294 self,
295 x.inode_no().into(),
296 self.request.nodeid().into(),
297 x.dest().name.as_ref(),
298 self.reply(),
299 );
300 }
301 ll::Operation::Open(x) => {
302 se.filesystem
303 .open(self, self.request.nodeid().into(), x.flags(), self.reply());
304 }
305 ll::Operation::Read(x) => {
306 se.filesystem.read(
307 self,
308 self.request.nodeid().into(),
309 x.file_handle().into(),
310 x.offset(),
311 x.size(),
312 x.flags(),
313 x.lock_owner().map(|l| l.into()),
314 self.reply(),
315 );
316 }
317 ll::Operation::Write(x) => {
318 se.filesystem.write(
319 self,
320 self.request.nodeid().into(),
321 x.file_handle().into(),
322 x.offset(),
323 x.data(),
324 x.write_flags(),
325 x.flags(),
326 x.lock_owner().map(|l| l.into()),
327 self.reply(),
328 );
329 }
330 ll::Operation::Flush(x) => {
331 se.filesystem.flush(
332 self,
333 self.request.nodeid().into(),
334 x.file_handle().into(),
335 x.lock_owner().into(),
336 self.reply(),
337 );
338 }
339 ll::Operation::Release(x) => {
340 se.filesystem.release(
341 self,
342 self.request.nodeid().into(),
343 x.file_handle().into(),
344 x.flags(),
345 x.lock_owner().map(|x| x.into()),
346 x.flush(),
347 self.reply(),
348 );
349 }
350 ll::Operation::FSync(x) => {
351 se.filesystem.fsync(
352 self,
353 self.request.nodeid().into(),
354 x.file_handle().into(),
355 x.fdatasync(),
356 self.reply(),
357 );
358 }
359 ll::Operation::OpenDir(x) => {
360 se.filesystem
361 .opendir(self, self.request.nodeid().into(), x.flags(), self.reply());
362 }
363 ll::Operation::ReadDir(x) => {
364 se.filesystem.readdir(
365 self,
366 self.request.nodeid().into(),
367 x.file_handle().into(),
368 x.offset(),
369 ReplyDirectory::new(
370 self.request.unique().into(),
371 self.ch.clone(),
372 x.size() as usize,
373 ),
374 );
375 }
376 ll::Operation::ReleaseDir(x) => {
377 se.filesystem.releasedir(
378 self,
379 self.request.nodeid().into(),
380 x.file_handle().into(),
381 x.flags(),
382 self.reply(),
383 );
384 }
385 ll::Operation::FSyncDir(x) => {
386 se.filesystem.fsyncdir(
387 self,
388 self.request.nodeid().into(),
389 x.file_handle().into(),
390 x.fdatasync(),
391 self.reply(),
392 );
393 }
394 ll::Operation::StatFs(_) => {
395 se.filesystem
396 .statfs(self, self.request.nodeid().into(), self.reply());
397 }
398 ll::Operation::SetXAttr(x) => {
399 se.filesystem.setxattr(
400 self,
401 self.request.nodeid().into(),
402 x.name(),
403 x.value(),
404 x.flags(),
405 x.position(),
406 self.reply(),
407 );
408 }
409 ll::Operation::GetXAttr(x) => {
410 se.filesystem.getxattr(
411 self,
412 self.request.nodeid().into(),
413 x.name(),
414 x.size_u32(),
415 self.reply(),
416 );
417 }
418 ll::Operation::ListXAttr(x) => {
419 se.filesystem
420 .listxattr(self, self.request.nodeid().into(), x.size(), self.reply());
421 }
422 ll::Operation::RemoveXAttr(x) => {
423 se.filesystem.removexattr(
424 self,
425 self.request.nodeid().into(),
426 x.name(),
427 self.reply(),
428 );
429 }
430 ll::Operation::Access(x) => {
431 se.filesystem
432 .access(self, self.request.nodeid().into(), x.mask(), self.reply());
433 }
434 ll::Operation::Create(x) => {
435 se.filesystem.create(
436 self,
437 self.request.nodeid().into(),
438 x.name().as_ref(),
439 x.mode(),
440 x.umask(),
441 x.flags(),
442 self.reply(),
443 );
444 }
445 ll::Operation::GetLk(x) => {
446 se.filesystem.getlk(
447 self,
448 self.request.nodeid().into(),
449 x.file_handle().into(),
450 x.lock_owner().into(),
451 x.lock().range.0,
452 x.lock().range.1,
453 x.lock().typ,
454 x.lock().pid,
455 self.reply(),
456 );
457 }
458 ll::Operation::SetLk(x) => {
459 se.filesystem.setlk(
460 self,
461 self.request.nodeid().into(),
462 x.file_handle().into(),
463 x.lock_owner().into(),
464 x.lock().range.0,
465 x.lock().range.1,
466 x.lock().typ,
467 x.lock().pid,
468 false,
469 self.reply(),
470 );
471 }
472 ll::Operation::SetLkW(x) => {
473 se.filesystem.setlk(
474 self,
475 self.request.nodeid().into(),
476 x.file_handle().into(),
477 x.lock_owner().into(),
478 x.lock().range.0,
479 x.lock().range.1,
480 x.lock().typ,
481 x.lock().pid,
482 true,
483 self.reply(),
484 );
485 }
486 ll::Operation::BMap(x) => {
487 se.filesystem.bmap(
488 self,
489 self.request.nodeid().into(),
490 x.block_size(),
491 x.block(),
492 self.reply(),
493 );
494 }
495
496 #[cfg(feature = "abi-7-11")]
497 ll::Operation::IoCtl(x) => {
498 if x.unrestricted() {
499 return Err(Errno::ENOSYS);
500 } else {
501 se.filesystem.ioctl(
502 self,
503 self.request.nodeid().into(),
504 x.file_handle().into(),
505 x.flags(),
506 x.command(),
507 x.in_data(),
508 x.out_size(),
509 self.reply(),
510 );
511 }
512 }
513 #[cfg(feature = "abi-7-11")]
514 ll::Operation::Poll(x) => {
515 se.filesystem.poll(
516 self,
517 self.request.nodeid().into(),
518 x.file_handle().into(),
519 x.kernel_handle(),
520 x.events(),
521 x.flags(),
522 self.reply(),
523 );
524 }
525 #[cfg(feature = "abi-7-15")]
526 ll::Operation::NotifyReply(_) => {
527 return Err(Errno::ENOSYS);
529 }
530 #[cfg(feature = "abi-7-16")]
531 ll::Operation::BatchForget(x) => {
532 se.filesystem.batch_forget(self, x.nodes()); }
534 #[cfg(feature = "abi-7-19")]
535 ll::Operation::FAllocate(x) => {
536 se.filesystem.fallocate(
537 self,
538 self.request.nodeid().into(),
539 x.file_handle().into(),
540 x.offset(),
541 x.len(),
542 x.mode(),
543 self.reply(),
544 );
545 }
546 #[cfg(feature = "abi-7-21")]
547 ll::Operation::ReadDirPlus(x) => {
548 se.filesystem.readdirplus(
549 self,
550 self.request.nodeid().into(),
551 x.file_handle().into(),
552 x.offset(),
553 ReplyDirectoryPlus::new(
554 self.request.unique().into(),
555 self.ch.clone(),
556 x.size() as usize,
557 ),
558 );
559 }
560 #[cfg(feature = "abi-7-23")]
561 ll::Operation::Rename2(x) => {
562 se.filesystem.rename(
563 self,
564 x.from().dir.into(),
565 x.from().name.as_ref(),
566 x.to().dir.into(),
567 x.to().name.as_ref(),
568 x.flags(),
569 self.reply(),
570 );
571 }
572 #[cfg(feature = "abi-7-24")]
573 ll::Operation::Lseek(x) => {
574 se.filesystem.lseek(
575 self,
576 self.request.nodeid().into(),
577 x.file_handle().into(),
578 x.offset(),
579 x.whence(),
580 self.reply(),
581 );
582 }
583 #[cfg(feature = "abi-7-28")]
584 ll::Operation::CopyFileRange(x) => {
585 let (i, o) = (x.src(), x.dest());
586 se.filesystem.copy_file_range(
587 self,
588 i.inode.into(),
589 i.file_handle.into(),
590 i.offset,
591 o.inode.into(),
592 o.file_handle.into(),
593 o.offset,
594 x.len(),
595 x.flags().try_into().unwrap(),
596 self.reply(),
597 );
598 }
599 #[cfg(target_os = "macos")]
600 ll::Operation::SetVolName(x) => {
601 se.filesystem.setvolname(self, x.name(), self.reply());
602 }
603 #[cfg(target_os = "macos")]
604 ll::Operation::GetXTimes(_) => {
605 se.filesystem
606 .getxtimes(self, self.request.nodeid().into(), self.reply());
607 }
608 #[cfg(target_os = "macos")]
609 ll::Operation::Exchange(x) => {
610 se.filesystem.exchange(
611 self,
612 x.from().dir.into(),
613 x.from().name.as_ref(),
614 x.to().dir.into(),
615 x.to().name.as_ref(),
616 x.options(),
617 self.reply(),
618 );
619 }
620
621 #[cfg(feature = "abi-7-12")]
622 ll::Operation::CuseInit(_) => {
623 return Err(Errno::ENOSYS);
625 }
626 }
627 Ok(None)
628 }
629
630 fn reply<T: Reply>(&self) -> T {
633 Reply::new(self.request.unique().into(), self.ch.clone())
634 }
635
636 #[inline]
638 pub fn unique(&self) -> u64 {
639 self.request.unique().into()
640 }
641
642 #[inline]
644 pub fn uid(&self) -> u32 {
645 self.request.uid()
646 }
647
648 #[inline]
650 pub fn gid(&self) -> u32 {
651 self.request.gid()
652 }
653
654 #[inline]
656 pub fn pid(&self) -> u32 {
657 self.request.pid()
658 }
659}