Skip to main content

starnix_modules_ext4/
lib.rs

1// Copyright 2021 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
5#![recursion_limit = "256"]
6
7use ext4_lib::parser::{Parser as ExtParser, XattrMap as ExtXattrMap};
8use ext4_lib::readers::VmoReader;
9use ext4_lib::structs::{EntryType, INode, ROOT_INODE_NUM};
10use once_cell::sync::OnceCell;
11use starnix_core::mm::ProtectionFlags;
12use starnix_core::mm::memory::MemoryObject;
13use starnix_core::task::CurrentTask;
14use starnix_core::vfs::{
15    CacheMode, DEFAULT_BYTES_PER_BLOCK, DirectoryEntryType, DirentSink, FileObject, FileOps,
16    FileSystem, FileSystemHandle, FileSystemOps, FileSystemOptions, FsNode, FsNodeHandle,
17    FsNodeInfo, FsNodeOps, FsStr, FsString, MemoryRegularFile, SeekTarget, SymlinkTarget, XattrOp,
18    XattrStorage, default_seek, fileops_impl_directory, fileops_impl_noop_sync,
19    fs_node_impl_dir_readonly, fs_node_impl_not_dir, fs_node_impl_symlink,
20    fs_node_impl_xattr_delegate,
21};
22use starnix_logging::{impossible_error, track_stub};
23use starnix_sync::{FileOpsCore, Locked, Unlocked};
24use starnix_types::vfs::default_statfs;
25use starnix_uapi::auth::FsCred;
26use starnix_uapi::errors::Errno;
27use starnix_uapi::file_mode::FileMode;
28use starnix_uapi::mount_flags::FileSystemFlags;
29use starnix_uapi::open_flags::OpenFlags;
30use starnix_uapi::{EXT4_SUPER_MAGIC, errno, error, ino_t, off_t, statfs};
31use std::sync::Arc;
32use zx::HandleBased;
33
34mod pager;
35
36use pager::{Pager, PagerExtent};
37
38pub struct ExtFilesystem {
39    parser: ExtParser,
40    pager: Arc<Pager>,
41}
42
43impl FileSystemOps for ExtFilesystem {
44    fn name(&self) -> &'static FsStr {
45        "ext4".into()
46    }
47
48    fn statfs(
49        &self,
50        _locked: &mut Locked<FileOpsCore>,
51        _fs: &FileSystem,
52        _current_task: &CurrentTask,
53    ) -> Result<statfs, Errno> {
54        Ok(default_statfs(EXT4_SUPER_MAGIC))
55    }
56}
57
58struct ExtNode {
59    inode_num: u32,
60    inode: INode,
61    xattrs: ExtXattrMap,
62}
63
64impl ExtFilesystem {
65    pub fn new_fs(
66        locked: &mut Locked<Unlocked>,
67        current_task: &CurrentTask,
68        options: FileSystemOptions,
69    ) -> Result<FileSystemHandle, Errno> {
70        let mut open_flags = OpenFlags::RDWR;
71        let mut prot_flags = ProtectionFlags::READ | ProtectionFlags::WRITE | ProtectionFlags::EXEC;
72        if options.flags.contains(FileSystemFlags::RDONLY) {
73            open_flags = OpenFlags::RDONLY;
74            prot_flags ^= ProtectionFlags::WRITE;
75        }
76
77        let source_device = current_task.open_file(locked, options.source.as_ref(), open_flags)?;
78
79        // Note that we *require* get_memory to work here for performance reasons.  Fallback to
80        // FIDL-based read/write API is not an option.
81        let memory = source_device.get_memory(locked, current_task, None, prot_flags)?;
82        let pager_vmo = memory
83            .as_vmo()
84            .ok_or_else(|| errno!(EINVAL))?
85            .duplicate_handle(zx::Rights::SAME_RIGHTS)
86            .map_err(impossible_error)?;
87        let parser_vmo = Arc::new(
88            memory
89                .as_vmo()
90                .ok_or_else(|| errno!(EINVAL))?
91                .duplicate_handle(zx::Rights::SAME_RIGHTS)
92                .map_err(impossible_error)?,
93        );
94        let parser = ExtParser::new(Box::new(VmoReader::new(parser_vmo)));
95        let pager =
96            Arc::new(Pager::new(pager_vmo, parser.block_size().map_err(|e| errno!(EIO, e))?)?);
97        let fs = Self { parser, pager };
98        let ops = ExtDirectory { inner: Arc::new(ExtNode::new(&fs, ROOT_INODE_NUM)?) };
99        let fs = FileSystem::new(
100            locked,
101            current_task.kernel(),
102            CacheMode::Cached(current_task.kernel().fs_cache_config()),
103            fs,
104            options,
105        )?;
106        fs.create_root(ROOT_INODE_NUM as ino_t, ops);
107        Ok(fs)
108    }
109}
110
111impl ExtNode {
112    fn new(fs: &ExtFilesystem, inode_num: u32) -> Result<ExtNode, Errno> {
113        let inode = fs.parser.inode(inode_num).map_err(|e| errno!(EIO, e))?;
114        let xattrs = fs.parser.inode_xattrs(inode_num).unwrap_or_default();
115        Ok(ExtNode { inode_num, inode, xattrs })
116    }
117}
118
119impl XattrStorage for ExtNode {
120    fn list_xattrs(&self, _locked: &mut Locked<FileOpsCore>) -> Result<Vec<FsString>, Errno> {
121        Ok(self.xattrs.keys().map(|k| k.clone().into()).collect())
122    }
123
124    fn get_xattr(
125        &self,
126        _locked: &mut Locked<FileOpsCore>,
127        name: &FsStr,
128    ) -> Result<FsString, Errno> {
129        self.xattrs.get(&**name).map(|a| a.clone().into()).ok_or_else(|| errno!(ENODATA))
130    }
131
132    fn set_xattr(
133        &self,
134        _locked: &mut Locked<FileOpsCore>,
135        _name: &FsStr,
136        _value: &FsStr,
137        _op: XattrOp,
138    ) -> Result<(), Errno> {
139        error!(ENOSYS)
140    }
141    fn remove_xattr(&self, _locked: &mut Locked<FileOpsCore>, _name: &FsStr) -> Result<(), Errno> {
142        error!(ENOSYS)
143    }
144}
145
146struct ExtDirectory {
147    inner: Arc<ExtNode>,
148}
149
150impl FsNodeOps for ExtDirectory {
151    fs_node_impl_dir_readonly!();
152    fs_node_impl_xattr_delegate!(self, self.inner);
153
154    fn create_file_ops(
155        &self,
156        _locked: &mut Locked<FileOpsCore>,
157        _node: &FsNode,
158        _current_task: &CurrentTask,
159        _flags: OpenFlags,
160    ) -> Result<Box<dyn FileOps>, Errno> {
161        Ok(Box::new(ExtDirFileObject { inner: self.inner.clone() }))
162    }
163
164    fn lookup(
165        &self,
166        _locked: &mut Locked<FileOpsCore>,
167        node: &FsNode,
168        _current_task: &CurrentTask,
169        name: &FsStr,
170    ) -> Result<FsNodeHandle, Errno> {
171        let fs = node.fs();
172        let fs_ops = fs.downcast_ops::<ExtFilesystem>().unwrap();
173        let dir_entries =
174            fs_ops.parser.entries_from_inode(&self.inner.inode).map_err(|e| errno!(EIO, e))?;
175        let entry = dir_entries
176            .iter()
177            .find(|e| e.name_bytes() == name)
178            .ok_or_else(|| errno!(ENOENT, name))?;
179        let ext_node = ExtNode::new(fs_ops, entry.e2d_ino.into())?;
180        let inode_num = ext_node.inode_num as ino_t;
181        fs.get_or_create_node(inode_num, || {
182            let entry_type = EntryType::from_u8(entry.e2d_type).map_err(|e| errno!(EIO, e))?;
183            let mode = FileMode::from_bits(ext_node.inode.e2di_mode.into());
184
185            let uid = get_uid_from_node(&ext_node);
186            let gid = get_gid_from_node(&ext_node);
187            let owner = FsCred { uid, gid };
188
189            let size = get_size_from_node(&ext_node, &mode);
190            let blocks = get_blocks_from_node(&ext_node);
191            let nlink = ext_node.inode.e2di_nlink.into();
192
193            let ops: Box<dyn FsNodeOps> = match entry_type {
194                EntryType::RegularFile => Box::new(ExtFile::new(ext_node, name.to_owned())),
195                EntryType::Directory => Box::new(ExtDirectory { inner: Arc::new(ext_node) }),
196                EntryType::SymLink => Box::new(ExtSymlink { inner: ext_node }),
197                EntryType::Unknown => {
198                    track_stub!(TODO("https://fxbug.dev/322873719"), "ext4 unknown entry type");
199                    Box::new(ExtFile::new(ext_node, name.to_owned()))
200                }
201                EntryType::CharacterDevice => {
202                    track_stub!(TODO("https://fxbug.dev/322874445"), "ext4 character device");
203                    Box::new(ExtFile::new(ext_node, name.to_owned()))
204                }
205                EntryType::BlockDevice => {
206                    track_stub!(TODO("https://fxbug.dev/322874062"), "ext4 block device");
207                    Box::new(ExtFile::new(ext_node, name.to_owned()))
208                }
209                EntryType::FIFO => {
210                    track_stub!(TODO("https://fxbug.dev/322874249"), "ext4 fifo");
211                    Box::new(ExtFile::new(ext_node, name.to_owned()))
212                }
213                EntryType::Socket => {
214                    track_stub!(TODO("https://fxbug.dev/322874081"), "ext4 socket");
215                    Box::new(ExtFile::new(ext_node, name.to_owned()))
216                }
217            };
218
219            let child = FsNode::new_uncached(
220                inode_num,
221                ops,
222                &fs,
223                FsNodeInfo { mode, uid: owner.uid, gid: owner.gid, ..Default::default() },
224            );
225            child.update_info(|info| {
226                info.size = size as usize;
227                info.link_count = nlink;
228                info.blksize = DEFAULT_BYTES_PER_BLOCK;
229                info.blocks = blocks as usize;
230            });
231            Ok(child)
232        })
233    }
234}
235
236fn merge_low_high_16(low: u32, high: u32) -> u32 {
237    low | (high << 16)
238}
239
240fn merge_low_high_32(low: u64, high: u64) -> u64 {
241    low | (high << 32)
242}
243
244fn get_uid_from_node(ext_node: &ExtNode) -> u32 {
245    let uid_lower: u32 = ext_node.inode.e2di_uid.into();
246    let uid_upper: u32 = ext_node.inode.e2di_uid_high.into();
247    merge_low_high_16(uid_lower, uid_upper)
248}
249
250fn get_gid_from_node(ext_node: &ExtNode) -> u32 {
251    let gid_lower: u32 = ext_node.inode.e2di_gid.into();
252    let gid_upper: u32 = ext_node.inode.e2di_gid_high.into();
253    merge_low_high_16(gid_lower, gid_upper)
254}
255
256fn get_size_from_node(ext_node: &ExtNode, mode: &FileMode) -> u64 {
257    if mode.is_reg() {
258        let size_lower: u64 = ext_node.inode.e2di_size.into();
259        let size_upper: u64 = ext_node.inode.e2di_size_high.into();
260        merge_low_high_32(size_lower, size_upper)
261    } else {
262        ext_node.inode.e2di_size.into()
263    }
264}
265
266fn get_blocks_from_node(ext_node: &ExtNode) -> u64 {
267    let blocks_lower: u64 = ext_node.inode.e2di_nblock.into();
268    let blocks_upper: u64 = ext_node.inode.e2di_nblock_high.into();
269    merge_low_high_32(blocks_lower, blocks_upper)
270}
271
272struct ExtFile {
273    inner: ExtNode,
274    name: FsString,
275
276    // The VMO here will be a child of the main VMO that the pager holds.  We want to keep it here
277    // so that whilst ExtFile remains resident, we hold a child reference to the main VMO which
278    // will prevent the pager from dropping the VMO (and any data we might have paged-in).
279    memory: OnceCell<Arc<MemoryObject>>,
280}
281
282impl ExtFile {
283    fn new(inner: ExtNode, name: FsString) -> Self {
284        ExtFile { inner, name, memory: OnceCell::new() }
285    }
286}
287
288impl FsNodeOps for ExtFile {
289    fs_node_impl_not_dir!();
290    fs_node_impl_xattr_delegate!(self, self.inner);
291
292    fn create_file_ops(
293        &self,
294        _locked: &mut Locked<FileOpsCore>,
295        node: &FsNode,
296        _current_task: &CurrentTask,
297        _flags: OpenFlags,
298    ) -> Result<Box<dyn FileOps>, Errno> {
299        let fs = node.fs();
300        let fs_ops = fs.downcast_ops::<ExtFilesystem>().unwrap();
301        let inode_num = self.inner.inode_num;
302        let memory = self.memory.get_or_try_init(|| {
303            let (file_size, extents) = fs_ops
304                .parser
305                .read_extents(self.inner.inode_num)
306                .map_err(|e| errno!(EINVAL, format!("failed to read extents: {e}")))?;
307            // The extents should be sorted which we rely on later.
308            let mut pager_extents = Vec::with_capacity(extents.len());
309            let mut last_block = 0;
310            for e in extents {
311                let pager_extent = PagerExtent::from(e);
312                if pager_extent.logical.start < last_block {
313                    return error!(EIO, "Bad extent");
314                }
315                last_block = pager_extent.logical.end;
316                pager_extents.push(pager_extent);
317            }
318            Ok(Arc::new(MemoryObject::from(
319                fs_ops
320                    .pager
321                    .register(self.name.as_ref(), inode_num, file_size, &pager_extents)
322                    .map_err(|e| errno!(EINVAL, e))?,
323            )))
324        })?;
325
326        // TODO(https://fxbug.dev/42080696) returned memory shouldn't be writeable
327        Ok(Box::new(MemoryRegularFile::new(memory.clone())))
328    }
329}
330
331impl From<ext4_lib::structs::Extent> for PagerExtent {
332    fn from(e: ext4_lib::structs::Extent) -> Self {
333        let block_count: u16 = e.e_len.into();
334        let start = e.e_blk.into();
335        Self { logical: start..start + block_count as u32, physical_block: e.target_block_num() }
336    }
337}
338
339struct ExtSymlink {
340    inner: ExtNode,
341}
342
343impl FsNodeOps for ExtSymlink {
344    fs_node_impl_symlink!();
345    fs_node_impl_xattr_delegate!(self, self.inner);
346
347    fn readlink(
348        &self,
349        _locked: &mut Locked<FileOpsCore>,
350        node: &FsNode,
351        _current_task: &CurrentTask,
352    ) -> Result<SymlinkTarget, Errno> {
353        let fs = node.fs();
354        let fs_ops = fs.downcast_ops::<ExtFilesystem>().unwrap();
355        let data = fs_ops.parser.read_data(self.inner.inode_num).map_err(|e| errno!(EIO, e))?;
356        Ok(SymlinkTarget::Path(data.into()))
357    }
358}
359
360struct ExtDirFileObject {
361    inner: Arc<ExtNode>,
362}
363
364impl FileOps for ExtDirFileObject {
365    fileops_impl_directory!();
366    fileops_impl_noop_sync!();
367
368    fn seek(
369        &self,
370        _locked: &mut Locked<FileOpsCore>,
371        _file: &FileObject,
372        _current_task: &CurrentTask,
373        current_offset: off_t,
374        target: SeekTarget,
375    ) -> Result<off_t, Errno> {
376        Ok(default_seek(current_offset, target, || error!(EINVAL))?)
377    }
378
379    fn readdir(
380        &self,
381        _locked: &mut Locked<FileOpsCore>,
382        file: &FileObject,
383        _current_task: &CurrentTask,
384        sink: &mut dyn DirentSink,
385    ) -> Result<(), Errno> {
386        let fs = file.node().fs();
387        let fs_ops = fs.downcast_ops::<ExtFilesystem>().unwrap();
388        let dir_entries =
389            fs_ops.parser.entries_from_inode(&self.inner.inode).map_err(|e| errno!(EIO, e))?;
390
391        if sink.offset() as usize >= dir_entries.len() {
392            return Ok(());
393        }
394
395        for entry in dir_entries[(sink.offset() as usize)..].iter() {
396            let inode_num = entry.e2d_ino.into();
397            let entry_type = directory_entry_type(
398                EntryType::from_u8(entry.e2d_type).map_err(|e| errno!(EIO, e))?,
399            );
400            sink.add(inode_num, sink.offset() + 1, entry_type, entry.name_bytes().into())?;
401        }
402        Ok(())
403    }
404}
405
406fn directory_entry_type(entry_type: EntryType) -> DirectoryEntryType {
407    match entry_type {
408        EntryType::Unknown => DirectoryEntryType::UNKNOWN,
409        EntryType::RegularFile => DirectoryEntryType::REG,
410        EntryType::Directory => DirectoryEntryType::DIR,
411        EntryType::CharacterDevice => DirectoryEntryType::CHR,
412        EntryType::BlockDevice => DirectoryEntryType::BLK,
413        EntryType::FIFO => DirectoryEntryType::FIFO,
414        EntryType::Socket => DirectoryEntryType::SOCK,
415        EntryType::SymLink => DirectoryEntryType::LNK,
416    }
417}
418
419#[cfg(test)]
420mod tests {
421    use super::*;
422    use ext4_lib::structs::INode;
423    use starnix_uapi::file_mode::mode;
424    use zerocopy::FromBytes;
425    use zerocopy::byteorder::little_endian::{U16 as LE16, U32 as LE32};
426
427    fn default_inode() -> INode {
428        let zero = vec![0; 160];
429        INode::read_from_bytes(&zero).expect("failed to read from bytes")
430    }
431
432    fn create_test_ext_node(inode: INode) -> ExtNode {
433        ExtNode { inode_num: 1, inode, xattrs: ExtXattrMap::default() }
434    }
435
436    #[test]
437    fn test_get_uid_from_node() {
438        let mut inode = default_inode();
439        inode.e2di_uid = LE16::new(1001);
440        inode.e2di_uid_high = LE16::new(1);
441        let node = create_test_ext_node(inode);
442        assert_eq!(get_uid_from_node(&node), (1 << 16) | 1001);
443    }
444
445    #[test]
446    fn test_get_gid_from_node() {
447        let mut inode = default_inode();
448        inode.e2di_gid = LE16::new(1002);
449        inode.e2di_gid_high = LE16::new(2);
450        let node = create_test_ext_node(inode);
451        assert_eq!(get_gid_from_node(&node), (2 << 16) | 1002);
452    }
453
454    #[test]
455    fn test_get_size_from_node() {
456        // Test with a regular file.
457        let mut inode = default_inode();
458        inode.e2di_size = LE32::new(0x12345678);
459        inode.e2di_size_high = LE32::new(0x9);
460        let node = create_test_ext_node(inode);
461        let mode = mode!(IFREG, 0o777);
462        assert_eq!(get_size_from_node(&node, &mode), (0x9 << 32) | 0x12345678);
463
464        // Test with a directory, where size_high should be ignored.
465        let mode = mode!(IFDIR, 0o777);
466        assert_eq!(get_size_from_node(&node, &mode), 0x12345678);
467    }
468
469    #[test]
470    fn test_get_blocks_from_node() {
471        let mut inode = default_inode();
472        inode.e2di_nblock = LE32::new(0xABCDE);
473        inode.e2di_nblock_high = LE16::new(0x3);
474        let node = create_test_ext_node(inode);
475        assert_eq!(get_blocks_from_node(&node), (0x3 << 32) | 0xABCDE);
476    }
477}