summaryrefslogtreecommitdiff
path: root/examples/ext2.rs
blob: f82b34aaf0c8bdb808333bbd069b7e21e51f5aae (plain)
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/* Read-only ext2 (rev 1.0) implementation.
 *
 * This is not really async, since the whole backing storage
 * is mmap()ed for simplicity, and then treated as a static
 * slice (likely unsound, I don't care). Some yields are
 * springled in a few places in order to emulate true async
 * operations.
 *
 * Reference: <https://www.nongnu.org/ext2-doc/ext2.html>
 */

#[cfg(target_endian = "big")]
compile_error!("This example assumes a little-endian system");

use std::{
    ffi::{CStr, OsStr},
    fs::File,
    mem::size_of,
    ops::ControlFlow,
    os::unix::{ffi::OsStrExt, io::AsRawFd},
    path::{Path, PathBuf},
    time::{Duration, UNIX_EPOCH},
};

use nix::{
    dir::Type,
    errno::Errno,
    sys::mman::{mmap, MapFlags, ProtFlags},
    sys::stat::Mode,
    unistd::{Gid, Uid},
};

use blown_fuse::{
    io::{Attrs, Entry, FsInfo, Known, Stat},
    mount::{mount_sync, Options},
    ops::{Getattr, Init, Lookup, Readdir, Readlink, Statfs},
    session::{Dispatch, Start},
    Done, FuseResult, Ino, Op, Ttl,
};

use bytemuck::{cast_slice, from_bytes, try_from_bytes};
use bytemuck_derive::{Pod, Zeroable};
use clap::{App, Arg};
use futures_util::stream::{self, Stream, StreamExt, TryStreamExt};
use smallvec::SmallVec;
use tokio::{self, runtime::Runtime};
use uuid::Uuid;

const EXT2_ROOT: Ino = Ino(2);

struct Ext2 {
    backing: &'static [u8],
    superblock: &'static Superblock,
}

struct Resolved {
    ino: Ino,
    inode: &'static Inode,
}

#[derive(Pod, Zeroable, Copy, Clone)]
#[repr(C)]
struct Superblock {
    s_inodes_count: u32,
    s_blocks_count: u32,
    s_r_blocks_count: u32,
    s_free_blocks_count: u32,
    s_free_inodes_count: u32,
    s_first_data_block: u32,
    s_log_block_size: u32,
    s_log_frag_size: i32,
    s_blocks_per_group: u32,
    s_frags_per_group: u32,
    s_inodes_per_group: u32,
    s_mtime: u32,
    s_wtime: u32,
    s_mnt_count: u16,
    s_max_mnt_count: u16,
    s_magic: u16,
    s_state: u16,
    s_errors: u16,
    s_minor_rev_level: u16,
    s_lastcheck: u32,
    s_checkinterval: u32,
    s_creator_os: u32,
    s_rev_level: u32,
    s_def_resuid: u16,
    s_def_resgid: u16,
    s_first_ino: u32,
    s_inode_size: u16,
    s_block_group_nr: u16,
    s_feature_compat: u32,
    s_feature_incompat: u32,
    s_feature_ro_compat: u32,
    s_uuid: [u8; 16],
    s_volume_name: [u8; 16],
    s_last_mounted: [u8; 64],
}

#[derive(Pod, Zeroable, Copy, Clone)]
#[repr(C)]
struct GroupDescriptor {
    bg_block_bitmap: u32,
    bg_inode_bitmap: u32,
    bg_inode_table: u32,
    bg_free_blocks_count: u16,
    bg_free_inodes_count: u16,
    bg_used_dirs_count: u16,
    bg_pad: u16,
    bg_reserved: [u32; 3],
}

#[derive(Pod, Zeroable, Copy, Clone)]
#[repr(C)]
struct Inode {
    i_mode: u16,
    i_uid: u16,
    i_size: u32,
    i_atime: u32,
    i_ctime: u32,
    i_mtime: u32,
    i_dtime: u32,
    i_gid: u16,
    i_links_count: u16,
    i_blocks: u32,
    i_flags: u32,
    i_osd1: u32,
    i_block: [u32; 15],
    i_generation: u32,
    i_file_acl: u32,
    i_dir_acl: u32,
    i_faddr: u32,
    i_osd2: [u32; 3],
}

#[derive(Pod, Zeroable, Copy, Clone)]
#[repr(C)]
struct LinkedEntry {
    inode: u32,
    rec_len: u16,
    name_len: u8,
    file_type: u8,
}

impl Ext2 {
    fn directory_stream(
        &self,
        inode: &'static Inode,
        start: u64,
    ) -> impl Stream<Item = Result<Entry<'static, Resolved>, Errno>> + '_ {
        stream::try_unfold(start, move |mut position| async move {
            loop {
                if position == inode.i_size as u64 {
                    break Ok(None); // End of stream
                }

                let bytes = self.seek_contiguous(inode, position)?;
                let (header, bytes) = bytes.split_at(size_of::<LinkedEntry>());
                let header: &LinkedEntry = from_bytes(header);

                position += header.rec_len as u64;
                if header.inode == 0 {
                    continue; // Unused entry
                }

                let ino = Ino(header.inode as u64);
                let name = OsStr::from_bytes(&bytes[..header.name_len as usize]);

                let inode = Resolved {
                    ino,
                    inode: self.inode(ino)?,
                };

                let entry = Entry {
                    inode,
                    name,
                    offset: position,
                    ttl: Ttl::MAX,
                };

                break Ok(Some((entry, position)));
            }
        })
    }

    fn inode(&self, ino: Ino) -> Result<&'static Inode, Errno> {
        let Ino(ino) = match ino {
            Ino::ROOT => EXT2_ROOT,
            EXT2_ROOT => Ino::ROOT,
            ino => ino,
        };

        if ino == 0 {
            log::error!("Attempted to access the null (0) inode");
            return Err(Errno::EIO);
        }

        let index = (ino - 1) as usize;
        let inodes_per_group = self.superblock.s_inodes_per_group as usize;
        let (block, index) = (index / inodes_per_group, index % inodes_per_group);

        let table_base = self.group_descriptors()?[block].bg_inode_table as usize;
        let inode_size = self.superblock.s_inode_size as usize;

        let inodes_per_block = self.block_size() / inode_size;
        let block = table_base + index / inodes_per_block;

        let start = index % inodes_per_block * inode_size;
        let end = start + size_of::<Inode>();

        Ok(from_bytes(&self.block(block)?[start..end]))
    }

    fn seek_contiguous(
        &self,
        inode: &'static Inode,
        position: u64,
    ) -> Result<&'static [u8], Errno> {
        let block_size = self.block_size();
        let position = position as usize;
        let (direct, offset) = (position / block_size, position % block_size);

        let out_of_bounds = || log::error!("Offset {} out of bounds", position);
        let chase = |indices: &[usize]| {
            let root: &[u8] = cast_slice(&inode.i_block);
            indices
                .iter()
                .try_fold(root, |ptrs, index| {
                    let ptrs: &[u32] = cast_slice(ptrs);
                    let block = ptrs[*index];

                    if block > 0 {
                        self.block(ptrs[*index] as usize)
                    } else {
                        out_of_bounds();
                        Err(Errno::EIO)
                    }
                })
                .map(|block| &block[offset..])
        };

        const DIRECT_PTRS: usize = 12;

        if direct < DIRECT_PTRS {
            return chase(&[direct]);
        }

        let ptrs_per_block = block_size / size_of::<u32>();
        let (level1, level1_index) = {
            let indirect = direct - DIRECT_PTRS;
            (indirect / ptrs_per_block, indirect % ptrs_per_block)
        };

        if level1 == 0 {
            return chase(&[DIRECT_PTRS, level1_index]);
        }

        let (level2, level2_index) = (level1 / ptrs_per_block, level1 % ptrs_per_block);
        if level2 == 0 {
            return chase(&[DIRECT_PTRS + 1, level2_index, level1_index]);
        }

        let (level3, level3_index) = (level2 / ptrs_per_block, level2 % ptrs_per_block);
        if level3 == 0 {
            chase(&[DIRECT_PTRS + 2, level3_index, level2_index, level1_index])
        } else {
            out_of_bounds();
            Err(Errno::EIO)
        }
    }

    fn group_descriptors(&self) -> Result<&'static [GroupDescriptor], Errno> {
        let start = (self.superblock.s_first_data_block + 1) as usize;
        let groups = (self.superblock.s_blocks_count / self.superblock.s_blocks_per_group) as usize;
        let descriptors_per_block = self.block_size() / size_of::<GroupDescriptor>();
        let table_blocks = (groups + descriptors_per_block - 1) / descriptors_per_block;

        self.blocks(start..start + table_blocks)
            .map(|blocks| &cast_slice(blocks)[..groups])
    }

    fn block(&self, n: usize) -> Result<&'static [u8], Errno> {
        self.blocks(n..n + 1)
    }

    fn blocks(&self, range: std::ops::Range<usize>) -> Result<&'static [u8], Errno> {
        let block_size = self.block_size();
        let (start, end) = (range.start * block_size, range.end * block_size);

        if self.backing.len() >= end {
            Ok(&self.backing[start..end])
        } else {
            log::error!("Bad block range: ({}..{})", range.start, range.end);
            Err(Errno::EIO)
        }
    }

    fn block_size(&self) -> usize {
        1024usize << self.superblock.s_log_block_size
    }
}

impl Ext2 {
    fn init<'o>(&self, (_, reply): Op<'o, Init>) -> Done<'o> {
        let label = &self.superblock.s_volume_name;
        let label = &label[..=label.iter().position(|byte| *byte == b'\0').unwrap_or(0)];
        let label = CStr::from_bytes_with_nul(label)
            .ok()
            .map(|label| {
                let label = label.to_string_lossy();
                if !label.is_empty() {
                    label
                } else {
                    "(empty)".into()
                }
            })
            .unwrap_or_else(|| "(bad)".into());

        log::info!("UUID: {}", Uuid::from_bytes(self.superblock.s_uuid));
        log::info!("Label: {}", label.escape_debug());

        log::info!("Mounted successfully");
        reply.ok()
    }

    async fn statfs<'o>(&self, (_, reply): Op<'o, Statfs>) -> Done<'o> {
        let total_blocks = self.superblock.s_blocks_count as u64;
        let free_blocks = self.superblock.s_free_blocks_count as u64;
        let available_blocks = free_blocks - self.superblock.s_r_blocks_count as u64;
        let total_inodes = self.superblock.s_inodes_count as u64;
        let free_inodes = self.superblock.s_free_inodes_count as u64;

        let info = FsInfo::default()
            .blocks(
                self.block_size() as u32,
                total_blocks,
                free_blocks,
                available_blocks,
            )
            .inodes(total_inodes, free_inodes)
            .max_filename(255);

        reply.info(&info)
    }

    async fn getattr<'o>(&self, (request, reply): Op<'o, Getattr>) -> Done<'o> {
        let ino = request.ino();
        let (reply, inode) = reply.and_then(self.inode(ino))?;

        reply.known(&Resolved { ino, inode })
    }

    async fn lookup<'o>(&self, (request, reply): Op<'o, Lookup>) -> Done<'o> {
        let name = request.name();
        let (mut reply, parent) = reply.and_then(self.inode(request.ino()))?;

        //TODO: Indexed directories
        let stream = self.directory_stream(parent, 0);
        tokio::pin!(stream);

        let inode = loop {
            let (next_reply, entry) = reply.and_then(stream.try_next().await)?;
            reply = next_reply;

            match entry {
                Some(entry) if entry.name == name => break Some(entry.inode),
                Some(_) => continue,
                None => break None,
            }
        };

        if let Some(inode) = inode {
            reply.found(inode, Ttl::MAX)
        } else {
            reply.not_found(Ttl::MAX)
        }
    }

    async fn readlink<'o>(&self, (request, reply): Op<'o, Readlink>) -> Done<'o> {
        let ino = request.ino();
        let (mut reply, inode) = reply.and_then(self.inode(ino))?;

        let resolved = Resolved { ino, inode };
        if resolved.inode_type() != Type::Symlink {
            return reply.invalid_argument();
        }

        let size = inode.i_size as usize;
        if size < size_of::<[u32; 15]>() {
            return reply.target(OsStr::from_bytes(&cast_slice(&inode.i_block)[..size]));
        }

        /* This is unlikely to ever spill, and is guaranteed not to
         * do so for valid symlinks on any fs where block_size >= 4096.
         */
        let mut segments = SmallVec::<[&[u8]; 1]>::new();
        let (mut size, mut offset) = (size, 0);

        while size > 0 {
            let (next_reply, segment) = reply.and_then(self.seek_contiguous(inode, offset))?;
            reply = next_reply;

            let segment = &segment[..segment.len().min(size)];
            segments.push(segment);

            size -= segment.len();
            offset += segment.len() as u64;
        }

        reply.gather_target(&segments)
    }

    async fn readdir<'o>(&self, (request, reply): Op<'o, Readdir>) -> Done<'o> {
        let (reply, inode) = reply.and_then(self.inode(request.ino()))?;
        let mut reply = reply.buffered(Vec::new());

        let stream = self.directory_stream(inode, request.offset());
        tokio::pin!(stream);

        while let Some(entry) = stream.next().await {
            let (next_reply, entry) = reply.and_then(entry)?;
            let (next_reply, ()) = next_reply.entry(entry)?;
            reply = next_reply;
        }

        reply.end()
    }
}

impl Stat for Resolved {
    fn ino(&self) -> Ino {
        self.ino
    }

    fn inode_type(&self) -> Type {
        let inode_type = self.inode.i_mode >> 12;
        match inode_type {
            0x01 => Type::Fifo,
            0x02 => Type::CharacterDevice,
            0x04 => Type::Directory,
            0x06 => Type::BlockDevice,
            0x08 => Type::File,
            0x0A => Type::Symlink,
            0x0C => Type::Socket,

            _ => {
                log::error!("Inode {} has invalid type {:x}", self.ino, inode_type);
                Type::File
            }
        }
    }

    fn attrs(&self) -> (Attrs, Ttl) {
        let inode = self.inode;
        let (access, modify, change) = {
            let time = |seconds: u32| (UNIX_EPOCH + Duration::from_secs(seconds.into())).into();
            let (atime, mtime, ctime) = (inode.i_atime, inode.i_mtime, inode.i_ctime);

            (time(atime), time(mtime), time(ctime))
        };

        let attrs = Attrs::default()
            .size((inode.i_dir_acl as u64) << 32 | inode.i_size as u64)
            .owner(
                Uid::from_raw(inode.i_uid.into()),
                Gid::from_raw(inode.i_gid.into()),
            )
            .mode(Mode::from_bits_truncate(inode.i_mode.into()))
            .blocks(inode.i_blocks.into())
            .block_size(512)
            .times(access, modify, change)
            .links(inode.i_links_count.into());

        (attrs, Ttl::MAX)
    }
}

impl Known for Resolved {
    type Inode = Resolved;

    fn inode(&self) -> &Self::Inode {
        self
    }

    fn unveil(self) {}
}

async fn main_loop(session: Start, fs: Ext2) -> FuseResult<()> {
    let session = session.start(|op| fs.init(op)).await?;
    let mut endpoint = session.endpoint();

    loop {
        let result = endpoint.receive(|dispatch| async {
            use Dispatch::*;

            match dispatch {
                Statfs(statfs) => fs.statfs(statfs.op()?).await,
                Getattr(getattr) => fs.getattr(getattr.op()?).await,
                Lookup(lookup) => fs.lookup(lookup.op()?).await,
                Readlink(readlink) => fs.readlink(readlink.op()?).await,
                Readdir(readdir) => fs.readdir(readdir.op()?).await,

                dispatch => {
                    let (_, reply) = dispatch.op();
                    reply.not_implemented()
                }
            }
        });

        match result.await? {
            ControlFlow::Break(()) => break Ok(()),
            ControlFlow::Continue(()) => continue,
        }
    }
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let matches = App::new("ext2")
        .about("read-only ext2 FUSE driver")
        .arg(Arg::from_usage("[mount_options] -o <options>... 'See fuse(8)'").number_of_values(1))
        .arg(Arg::from_usage("<image> 'Filesystem image file'"))
        .arg(Arg::from_usage("<mountpoint> 'Filesystem mountpoint'"))
        .get_matches();

    env_logger::builder()
        .filter_level(log::LevelFilter::Info)
        .init();

    let (image, session) = {
        let (image, mountpoint) = {
            let required_path = |key| Path::new(matches.value_of(key).unwrap());
            (required_path("image"), required_path("mountpoint"))
        };

        let canonical = image.canonicalize();
        let canonical = canonical.as_ref().map(PathBuf::as_path).unwrap_or(image);

        let mut options = Options::default();
        options
            .fs_name(canonical)
            .read_only()
            .extend(matches.values_of_os("mount_options").into_iter().flatten());

        (image, mount_sync(mountpoint, &options)?)
    };

    let file = File::open(image)?;
    let backing = unsafe {
        let length = file.metadata().unwrap().len() as usize;

        let base = mmap(
            std::ptr::null_mut(),
            length,
            ProtFlags::PROT_READ,
            MapFlags::MAP_PRIVATE,
            file.as_raw_fd(),
            0,
        );

        std::slice::from_raw_parts(base.unwrap() as *const u8, length)
    };

    let superblock = if backing.len() >= 1024 + size_of::<Superblock>() {
        Some(&backing[1024..1024 + size_of::<Superblock>()])
    } else {
        None
    };

    let superblock = superblock.and_then(|superblock| try_from_bytes(superblock).ok());
    let superblock: &'static Superblock = match superblock {
        Some(superblock) => superblock,
        None => {
            log::error!("Bad superblock");
            return Err(Errno::EINVAL.into());
        }
    };

    if superblock.s_magic != 0xef53 {
        log::error!("Bad magic");
        return Err(Errno::EINVAL.into());
    }

    let (major, minor) = (superblock.s_rev_level, superblock.s_minor_rev_level);
    if (major, minor) != (1, 0) {
        log::error!("Unsupported revision: {}.{}", major, minor);
        return Err(Errno::EINVAL.into());
    }

    let fs = Ext2 {
        backing,
        superblock,
    };

    Ok(Runtime::new()?.block_on(main_loop(session, fs))?)
}