Skip to main content

starnix_uapi/
mount_flags.rs

1// Copyright 2023 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::uapi;
6use atomic_bitflags::atomic_bitflags;
7
8atomic_bitflags! {
9    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
10    pub struct MountFlags: u32 {
11        // per-mountpoint flags
12        const RDONLY = uapi::MS_RDONLY;
13        const NOEXEC = uapi::MS_NOEXEC;
14        const NOSUID = uapi::MS_NOSUID;
15        const NODEV = uapi::MS_NODEV;
16        const NOATIME = uapi::MS_NOATIME;
17        const NODIRATIME = uapi::MS_NODIRATIME;
18        const RELATIME = uapi::MS_RELATIME;
19        const STRICTATIME = uapi::MS_STRICTATIME;
20
21        // per-superblock flags
22        const SILENT = uapi::MS_SILENT;
23        const LAZYTIME = uapi::MS_LAZYTIME;
24        const SYNCHRONOUS = uapi::MS_SYNCHRONOUS;
25        const DIRSYNC = uapi::MS_DIRSYNC;
26        const MANDLOCK = uapi::MS_MANDLOCK;
27
28        // mount() control flags
29        const REMOUNT = uapi::MS_REMOUNT;
30        const BIND = uapi::MS_BIND;
31        const REC = uapi::MS_REC;
32        const DOWNSTREAM = uapi::MS_SLAVE;
33        const SHARED = uapi::MS_SHARED;
34        const PRIVATE = uapi::MS_PRIVATE;
35
36        /// Flags that change be changed with REMOUNT.
37        ///
38        /// MS_DIRSYNC and MS_SILENT cannot be changed with REMOUNT.
39        const CHANGEABLE_WITH_REMOUNT = MountpointFlags::all().bits() |
40            Self::MANDLOCK.bits() | Self::LAZYTIME.bits() | Self::SYNCHRONOUS.bits();
41    }
42}
43
44atomic_bitflags! {
45    /// Subset of `MountFlags` that allow the behaviours of different mountpoints to the same
46    /// underlying `FileSystem` to be independently configured.
47    /// Note that
48    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
49    pub struct MountpointFlags: u32 {
50        // Flags stored for each mountpoint to configure its behaviour.
51        const RDONLY = MountFlags::RDONLY.bits();
52        const NOEXEC = MountFlags::NOEXEC.bits();
53        const NOSUID = MountFlags::NOSUID.bits();
54        const NODEV = MountFlags::NODEV.bits();
55        const NOATIME = MountFlags::NOATIME.bits();
56        const NODIRATIME = MountFlags::NODIRATIME.bits();
57        const RELATIME = MountFlags::RELATIME.bits();
58
59        const STORED_ON_MOUNT = Self::RDONLY.bits() | Self::NOEXEC.bits() | Self::NOSUID.bits() |
60            Self::NODEV.bits() | Self::NOATIME.bits() | Self::NODIRATIME.bits() | Self::RELATIME.bits();
61
62        // Flags affecting the behaviour of a single operation on a mountpoint.
63        const STRICTATIME = MountFlags::STRICTATIME.bits();
64        const REC = MountFlags::REC.bits();
65
66        /// Flags used to control how file access times are managed. Note that STRICTATIME is only
67        /// used by callers to specify that all other bits should be cleared; it is never stored.
68        const ATIME_MODE_FLAGS = Self::NOATIME.bits() | Self::RELATIME.bits() |Self::STRICTATIME.bits();
69        const ATIME_FLAGS = Self::ATIME_MODE_FLAGS.bits() | Self::NODIRATIME.bits();
70    }
71}
72
73impl MountpointFlags {
74    /// Ensures that `self` has an access-time flag set, copying the flag value from `existing` if
75    /// no flag is currently set.
76    /// This is used both to apply the kernel default `MS_RELATIME` if no other access-time flag is
77    /// passed to `mount()`, and to preserve existing access-time flags when re-mounting, unless the
78    /// `MS_STRICTATIME` flag is explicitly passed.
79    pub fn default_atime_from(&mut self, existing: MountpointFlags) {
80        if !self.intersects(Self::ATIME_FLAGS) {
81            // If no `ATIME_FLAGS` are set at all, preserve the `existing` flags.
82            *self |= existing & Self::ATIME_FLAGS;
83        } else if !self.intersects(Self::ATIME_MODE_FLAGS) {
84            // If the caller did not set any `ATIME_MODE_FLAGS` then default to the `RELATIME` mode.
85            *self |= Self::RELATIME;
86        }
87    }
88}
89
90impl From<MountpointFlags> for MountFlags {
91    fn from(flags: MountpointFlags) -> Self {
92        // MountpointFlags is defined using only bits that are valid for MountFlags.
93        Self::from_bits_retain(flags.bits())
94    }
95}
96
97atomic_bitflags! {
98    /// Subset of `MountFlags` that affect `FileSystem` behaviour.
99    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
100    pub struct FileSystemFlags: u32 {
101        const RDONLY = MountFlags::RDONLY.bits();
102        const DIRSYNC = MountFlags::DIRSYNC.bits();
103        const LAZYTIME = MountFlags::LAZYTIME.bits();
104        const MANDLOCK = MountFlags::MANDLOCK.bits();
105        const SILENT = MountFlags::SILENT.bits();
106        const SYNCHRONOUS = MountFlags::SYNCHRONOUS.bits();
107    }
108}
109
110impl From<FileSystemFlags> for MountFlags {
111    fn from(flags: FileSystemFlags) -> Self {
112        // FileSystemFlags is defined using only bits that are valid for MountFlags.
113        Self::from_bits_retain(flags.bits())
114    }
115}
116
117impl MountFlags {
118    pub fn mountpoint_flags(&self) -> MountpointFlags {
119        MountpointFlags::from_bits_truncate(self.bits())
120    }
121
122    pub fn file_system_flags(&self) -> FileSystemFlags {
123        FileSystemFlags::from_bits_truncate(self.bits())
124    }
125}
126
127impl std::fmt::Display for MountpointFlags {
128    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
129        MountFlags::from(*self).fmt(f)
130    }
131}
132
133impl std::fmt::Display for FileSystemFlags {
134    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
135        MountFlags::from(*self).fmt(f)
136    }
137}
138
139/// Display trait implementation for the subset of flags that are stored with mounts or superblocks.
140impl std::fmt::Display for MountFlags {
141    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
142        // `RDONLY` is stored both with the mount and the superblock flags.
143        write!(f, "{}", if self.contains(Self::RDONLY) { "ro" } else { "rw" })?;
144
145        // Stored only in the mount flags.
146        if self.contains(Self::NOEXEC) {
147            write!(f, ",noexec")?;
148        }
149        if self.contains(Self::NOSUID) {
150            write!(f, ",nosuid")?;
151        }
152        if self.contains(Self::NODEV) {
153            write!(f, ",nodev")?
154        }
155        if self.contains(Self::NOATIME) {
156            write!(f, ",noatime")?;
157        }
158        if self.contains(Self::NODIRATIME) {
159            write!(f, ",nodiratime")?;
160        }
161        if self.contains(Self::RELATIME) {
162            write!(f, ",relatime")?;
163        }
164
165        // Stored only on the superblock.
166        if self.contains(Self::DIRSYNC) {
167            write!(f, ",dirsync")?;
168        }
169        if self.contains(Self::LAZYTIME) {
170            write!(f, ",lazytime")?;
171        }
172        if self.contains(Self::MANDLOCK) {
173            write!(f, ",mand")?;
174        }
175        // `SILENT` is not reported in proc-mounts nor proc-pid-mountinfo.
176        if self.contains(Self::SYNCHRONOUS) {
177            write!(f, ",sync")?;
178        }
179
180        Ok(())
181    }
182}