blob: 7f8bac1a9b0e5fbcd65d96f27fb38f698309036b [file] [log] [blame]
Victor Hsieh79f296b2021-12-02 15:38:08 -08001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use fuse::mount::MountOption;
18use std::fs::OpenOptions;
Victor Hsieh963d5132022-03-09 21:58:17 +000019use std::num::NonZeroU8;
Victor Hsieh79f296b2021-12-02 15:38:08 -080020use std::os::unix::io::AsRawFd;
21use std::path::Path;
22
23use super::AuthFs;
24
Victor Hsieh58a5e9b2022-03-09 21:57:26 +000025/// Maximum bytes (excluding the FUSE header) `AuthFs` will receive from the kernel for write
26/// operations by another process.
Victor Hsieh79f296b2021-12-02 15:38:08 -080027pub const MAX_WRITE_BYTES: u32 = 65536;
28
Victor Hsieh58a5e9b2022-03-09 21:57:26 +000029/// Maximum bytes (excluding the FUSE header) `AuthFs` will receive from the kernel for read
30/// operations by another process.
Victor Hsieh79f296b2021-12-02 15:38:08 -080031/// TODO(victorhsieh): This option is deprecated by FUSE. Figure out if we can remove this.
32const MAX_READ_BYTES: u32 = 65536;
33
34/// Mount and start the FUSE instance to handle messages. This requires CAP_SYS_ADMIN.
35pub fn mount_and_enter_message_loop(
36 authfs: AuthFs,
37 mountpoint: &Path,
38 extra_options: &Option<String>,
Victor Hsieh963d5132022-03-09 21:58:17 +000039 threads: Option<NonZeroU8>,
Victor Hsieh79f296b2021-12-02 15:38:08 -080040) -> Result<(), fuse::Error> {
41 let dev_fuse = OpenOptions::new()
42 .read(true)
43 .write(true)
44 .open("/dev/fuse")
45 .expect("Failed to open /dev/fuse");
46
47 let mut mount_options = vec![
48 MountOption::FD(dev_fuse.as_raw_fd()),
49 MountOption::RootMode(libc::S_IFDIR | libc::S_IXUSR | libc::S_IXGRP | libc::S_IXOTH),
50 MountOption::AllowOther,
51 MountOption::UserId(0),
52 MountOption::GroupId(0),
53 MountOption::MaxRead(MAX_READ_BYTES),
54 ];
55 if let Some(value) = extra_options {
56 mount_options.push(MountOption::Extra(value));
57 }
58
Victor Hsiehbbac5192022-02-22 23:54:32 +000059 fuse::mount(
60 mountpoint,
61 "authfs",
62 libc::MS_NOSUID | libc::MS_NODEV | libc::MS_NOEXEC,
63 &mount_options,
64 )
65 .expect("Failed to mount fuse");
Victor Hsieh79f296b2021-12-02 15:38:08 -080066
Victor Hsieh58a5e9b2022-03-09 21:57:26 +000067 let mut config = fuse::FuseConfig::new();
68 config.dev_fuse(dev_fuse).max_write(MAX_WRITE_BYTES).max_read(MAX_READ_BYTES);
Victor Hsieh963d5132022-03-09 21:58:17 +000069 if let Some(num) = threads {
70 config.num_threads(u8::from(num).into());
71 }
Victor Hsieh58a5e9b2022-03-09 21:57:26 +000072 config.enter_message_loop(authfs)
Victor Hsieh79f296b2021-12-02 15:38:08 -080073}