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
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! Library for filesystem management in rust.
//!
//! This library is analogous to the fs-management library in zircon. It provides support for
//! formatting, mounting, unmounting, and fsck-ing. It is implemented in a similar way to the C++
//! version.  For components v2, add `/svc/fuchsia.process.Launcher` to `use` and add the
//! binaries as dependencies to your component.

mod error;
pub mod filesystem;
pub mod format;
pub mod partition;

use {
    cstr::cstr,
    fdio::{spawn_etc, SpawnAction, SpawnOptions},
    fidl_fuchsia_fs_startup::{
        CompressionAlgorithm, EvictionPolicyOverride, FormatOptions, StartOptions,
    },
    fuchsia_zircon as zx,
    std::{ffi::CStr, sync::Arc},
};

// Re-export errors as public.
pub use error::{CommandError, KillError, LaunchProcessError, QueryError, ShutdownError};

pub const BLOBFS_TYPE_GUID: [u8; 16] = [
    0x0e, 0x38, 0x67, 0x29, 0x4c, 0x13, 0xbb, 0x4c, 0xb6, 0xda, 0x17, 0xe7, 0xce, 0x1c, 0xa4, 0x5d,
];
pub const DATA_TYPE_GUID: [u8; 16] = [
    0x0c, 0x5f, 0x18, 0x08, 0x2d, 0x89, 0x8a, 0x42, 0xa7, 0x89, 0xdb, 0xee, 0xc8, 0xf5, 0x5e, 0x6a,
];
pub const FVM_TYPE_GUID: [u8; 16] = [
    0xb8, 0x7c, 0xfd, 0x49, 0x15, 0xdf, 0x73, 0x4e, 0xb9, 0xd9, 0x99, 0x20, 0x70, 0x12, 0x7f, 0x0f,
];

pub const FVM_TYPE_GUID_STR: &str = "49fd7cb8-df15-4e73-b9d9-992070127f0f";

pub const FS_COLLECTION_NAME: &'static str = "fs-collection";

fn launch_process(
    args: &[&CStr],
    mut actions: Vec<SpawnAction<'_>>,
) -> Result<zx::Process, LaunchProcessError> {
    match spawn_etc(
        &zx::Handle::invalid().into(),
        SpawnOptions::CLONE_ALL,
        args[0],
        args,
        None,
        &mut actions,
    ) {
        Ok(process) => Ok(process),
        Err((status, message)) => Err(LaunchProcessError {
            args: args.iter().map(|&a| a.to_owned()).collect(),
            status,
            message,
        }),
    }
}

#[derive(Clone)]
pub enum ComponentType {
    /// Launch the filesystem as a static child, using the configured name in the Mode as the child
    /// name. If the child doesn't exist, this will fail.
    StaticChild,

    /// Launch the filesystem as a dynamic child, in the configured collection. By default, the
    /// collection is "fs-collection".
    DynamicChild { collection_name: String },
}

impl Default for ComponentType {
    fn default() -> Self {
        ComponentType::DynamicChild { collection_name: "fs-collection".to_string() }
    }
}

pub enum Mode<'a> {
    /// Run the filesystem as a legacy binary.
    /// TODO(fxbug.dev/117437): remove legacy filesystem launching support.
    Legacy(LegacyConfig<'a>),

    /// Run the filesystem as a component.
    Component {
        /// For static children, the name specifies the name of the child.  For dynamic children,
        /// the component URL is "#meta/{component-name}.cm".  The library will attempt to connect
        /// to a static child first, and if that fails, it will launch the filesystem within a
        /// collection.
        name: &'a str,

        /// It should be possible to reuse components after serving them, but it's not universally
        /// supported.
        reuse_component_after_serving: bool,

        /// Format options as defined by the startup protocol
        format_options: FormatOptions,

        /// Start options as defined by the startup protocol
        start_options: StartOptions,

        /// Whether to launch this filesystem as a dynamic or static child.
        component_type: ComponentType,
    },
}

impl<'a> Mode<'a> {
    // TODO(fxbug.dev/117437): remove legacy run type
    fn into_legacy_config(self) -> Option<LegacyConfig<'a>> {
        match self {
            Mode::Legacy(config) => Some(config),
            _ => None,
        }
    }

    // TODO(fxbug.dev/117437): remove Option when legacy run type is removed
    fn component_name(&self) -> Option<&str> {
        match self {
            Mode::Component { name, .. } => Some(name),
            _ => None,
        }
    }

    // TODO(fxbug.dev/117437): remove Option when legacy run type is removed
    fn component_type(&self) -> Option<ComponentType> {
        match self {
            Mode::Component { component_type, .. } => Some(component_type.clone()),
            _ => None,
        }
    }
}

// TODO(fxbug.dev/117437): remove legacy run type
#[derive(Default)]
pub struct LegacyConfig<'a> {
    /// Path to the binary.
    pub binary_path: &'a CStr,

    /// Arguments passed to the binary for all subcommands
    pub generic_args: Vec<&'a CStr>,

    /// Arguments passed to the binary for formatting
    pub format_args: Vec<&'a CStr>,

    /// Arguments passed to the binary for mounting
    pub mount_args: Vec<&'a CStr>,
}

/// Describes the configuration for a particular filesystem.
pub trait FSConfig: Send + Sync + 'static {
    /// Returns the mode in which to run this filesystem.
    fn mode(&self) -> Mode<'_>;

    /// Returns a handle for the crypt service (if any).
    fn crypt_client(&self) -> Option<zx::Channel> {
        // By default, filesystems don't need a crypt service.
        None
    }

    /// Whether the filesystem supports multiple volumes.
    fn is_multi_volume(&self) -> bool {
        false
    }

    fn disk_format(&self) -> format::DiskFormat {
        format::DiskFormat::Unknown
    }
}

///
/// FILESYSTEMS
///

/// Layout of blobs in blobfs
#[derive(Clone)]
pub enum BlobLayout {
    /// Merkle tree is stored in a separate block. This is deprecated and used only on Astro
    /// devices (it takes more space).
    DeprecatedPadded,

    /// Merkle tree is appended to the last block of data
    Compact,
}

/// Compression used for blobs in blobfs
#[derive(Clone)]
pub enum BlobCompression {
    ZSTDChunked,
    Uncompressed,
}

/// Eviction policy used for blobs in blobfs
#[derive(Clone)]
pub enum BlobEvictionPolicy {
    NeverEvict,
    EvictImmediately,
}

/// Blobfs Filesystem Configuration
/// If fields are None or false, they will not be set in arguments.
#[derive(Clone, Default)]
pub struct Blobfs {
    // Format options
    pub verbose: bool,
    pub deprecated_padded_blobfs_format: bool,
    pub num_inodes: u64,
    // Start Options
    pub readonly: bool,
    pub write_compression_algorithm: Option<BlobCompression>,
    pub write_compression_level: Option<i32>,
    pub cache_eviction_policy_override: Option<BlobEvictionPolicy>,
    pub component_type: ComponentType,
}

impl Blobfs {
    /// Manages a block device using the default configuration.
    pub fn new(block_device: fidl_fuchsia_device::ControllerProxy) -> filesystem::Filesystem {
        filesystem::Filesystem::new(block_device, Self::default())
    }

    /// Launch blobfs, with the default configuration, as a dynamic child in the fs-collection.
    pub fn dynamic_child() -> Self {
        Self {
            component_type: ComponentType::DynamicChild {
                collection_name: FS_COLLECTION_NAME.to_string(),
            },
            ..Default::default()
        }
    }
}

impl FSConfig for Blobfs {
    fn mode(&self) -> Mode<'_> {
        Mode::Component {
            name: "blobfs",
            reuse_component_after_serving: false,
            format_options: FormatOptions {
                verbose: self.verbose,
                deprecated_padded_blobfs_format: self.deprecated_padded_blobfs_format,
                num_inodes: self.num_inodes,
                fvm_data_slices: 0,
            },
            start_options: {
                let mut start_options = StartOptions {
                    read_only: self.readonly,
                    verbose: self.verbose,
                    fsck_after_every_transaction: false,
                    write_compression_level: self.write_compression_level.unwrap_or(-1),
                    write_compression_algorithm: CompressionAlgorithm::ZstdChunked,
                    cache_eviction_policy_override: EvictionPolicyOverride::None,
                };
                if let Some(compression) = &self.write_compression_algorithm {
                    start_options.write_compression_algorithm = match compression {
                        BlobCompression::ZSTDChunked => CompressionAlgorithm::ZstdChunked,
                        BlobCompression::Uncompressed => CompressionAlgorithm::Uncompressed,
                    }
                }
                if let Some(eviction) = &self.cache_eviction_policy_override {
                    start_options.cache_eviction_policy_override = match eviction {
                        BlobEvictionPolicy::NeverEvict => EvictionPolicyOverride::NeverEvict,
                        BlobEvictionPolicy::EvictImmediately => {
                            EvictionPolicyOverride::EvictImmediately
                        }
                    }
                }
                start_options
            },
            component_type: self.component_type.clone(),
        }
    }

    fn disk_format(&self) -> format::DiskFormat {
        format::DiskFormat::Blobfs
    }
}

/// Minfs Filesystem Configuration
/// If fields are None or false, they will not be set in arguments.
#[derive(Clone, Default)]
pub struct Minfs {
    // TODO(xbhatnag): Add support for fvm_data_slices
    // Format options
    pub verbose: bool,
    pub fvm_data_slices: u32,
    // Start Options
    pub readonly: bool,
    pub fsck_after_every_transaction: bool,
    pub component_type: ComponentType,
}

impl Minfs {
    /// Manages a block device using the default configuration.
    pub fn new(block_device: fidl_fuchsia_device::ControllerProxy) -> filesystem::Filesystem {
        filesystem::Filesystem::new(block_device, Self::default())
    }

    /// Launch minfs, with the default configuration, as a dynamic child in the fs-collection.
    pub fn dynamic_child() -> Self {
        Self {
            component_type: ComponentType::DynamicChild {
                collection_name: FS_COLLECTION_NAME.to_string(),
            },
            ..Default::default()
        }
    }
}

impl FSConfig for Minfs {
    fn mode(&self) -> Mode<'_> {
        Mode::Component {
            name: "minfs",
            reuse_component_after_serving: false,
            format_options: FormatOptions {
                verbose: self.verbose,
                deprecated_padded_blobfs_format: false,
                num_inodes: 0,
                fvm_data_slices: self.fvm_data_slices,
            },
            start_options: StartOptions {
                read_only: self.readonly,
                verbose: self.verbose,
                fsck_after_every_transaction: self.fsck_after_every_transaction,
                write_compression_level: -1,
                write_compression_algorithm: CompressionAlgorithm::ZstdChunked,
                cache_eviction_policy_override: EvictionPolicyOverride::None,
            },
            component_type: self.component_type.clone(),
        }
    }

    fn disk_format(&self) -> format::DiskFormat {
        format::DiskFormat::Minfs
    }
}

/// MinfsLegacy Filesystem Configuration
/// MinfsLegacy allows us to launch minfs as a legacy binary
/// DON'T USE!! This is only for //src/identity (fxbug.dev/114443)
/// If fields are None or false, they will not be set in arguments.
#[derive(Clone, Default)]
pub struct MinfsLegacy {
    // TODO(xbhatnag): Add support for fvm_data_slices
    pub verbose: bool,
    pub readonly: bool,
    pub fsck_after_every_transaction: bool,
}

impl MinfsLegacy {
    /// Manages a block device using the default configuration.
    pub fn new(block_device: fidl_fuchsia_device::ControllerProxy) -> filesystem::Filesystem {
        filesystem::Filesystem::new(block_device, Self::default())
    }
}

impl FSConfig for MinfsLegacy {
    fn mode(&self) -> Mode<'_> {
        Mode::Legacy(LegacyConfig {
            binary_path: cstr!("/pkg/bin/minfs"),
            generic_args: {
                let mut args = vec![];
                if self.verbose {
                    args.push(cstr!("--verbose"));
                }
                args
            },
            mount_args: {
                let mut args = vec![];
                if self.readonly {
                    args.push(cstr!("--readonly"));
                }
                if self.fsck_after_every_transaction {
                    args.push(cstr!("--fsck_after_every_transaction"));
                }
                args
            },
            ..Default::default()
        })
    }

    fn disk_format(&self) -> format::DiskFormat {
        format::DiskFormat::Minfs
    }
}

pub type CryptClientFn = Arc<dyn Fn() -> zx::Channel + Send + Sync>;

/// Fxfs Filesystem Configuration
/// If fields are None or false, they will not be set in arguments.
#[derive(Clone, Default)]
pub struct Fxfs {
    // This is only used by fsck.
    pub crypt_client_fn: Option<CryptClientFn>,
    // Start Options
    pub readonly: bool,
    pub fsck_after_every_transaction: bool,
    pub component_type: ComponentType,
}

impl Fxfs {
    pub fn with_crypt_client(crypt_client_fn: CryptClientFn) -> Self {
        Fxfs { crypt_client_fn: Some(crypt_client_fn), ..Default::default() }
    }

    /// Manages a block device using the default configuration.
    pub fn new(block_device: fidl_fuchsia_device::ControllerProxy) -> filesystem::Filesystem {
        filesystem::Filesystem::new(block_device, Self::default())
    }

    /// Launch Fxfs, with the default configuration, as a dynamic child in the fs-collection.
    pub fn dynamic_child() -> Self {
        Self {
            component_type: ComponentType::DynamicChild {
                collection_name: FS_COLLECTION_NAME.to_string(),
            },
            ..Default::default()
        }
    }
}

impl FSConfig for Fxfs {
    fn mode(&self) -> Mode<'_> {
        Mode::Component {
            name: "fxfs",
            reuse_component_after_serving: true,
            format_options: FormatOptions {
                verbose: false,
                deprecated_padded_blobfs_format: false,
                num_inodes: 0,
                fvm_data_slices: 0,
            },
            start_options: StartOptions {
                read_only: self.readonly,
                verbose: false,
                fsck_after_every_transaction: self.fsck_after_every_transaction,
                write_compression_level: -1,
                write_compression_algorithm: CompressionAlgorithm::ZstdChunked,
                cache_eviction_policy_override: EvictionPolicyOverride::None,
            },
            component_type: self.component_type.clone(),
        }
    }

    fn crypt_client(&self) -> Option<zx::Channel> {
        self.crypt_client_fn.as_ref().map(|f| f())
    }

    fn is_multi_volume(&self) -> bool {
        true
    }

    fn disk_format(&self) -> format::DiskFormat {
        format::DiskFormat::Fxfs
    }
}

/// F2fs Filesystem Configuration
/// If fields are None or false, they will not be set in arguments.
#[derive(Clone, Default)]
pub struct F2fs {
    pub component_type: ComponentType,
}

impl F2fs {
    /// Manages a block device using the default configuration.
    pub fn new(block_device: fidl_fuchsia_device::ControllerProxy) -> filesystem::Filesystem {
        filesystem::Filesystem::new(block_device, Self::default())
    }

    /// Launch f2fs, with the default configuration, as a dynamic child in the fs-collection.
    pub fn dynamic_child() -> Self {
        Self {
            component_type: ComponentType::DynamicChild {
                collection_name: FS_COLLECTION_NAME.to_string(),
            },
            ..Default::default()
        }
    }
}

impl FSConfig for F2fs {
    fn mode(&self) -> Mode<'_> {
        Mode::Component {
            name: "f2fs",
            reuse_component_after_serving: false,
            format_options: FormatOptions {
                verbose: false,
                deprecated_padded_blobfs_format: false,
                num_inodes: 0,
                fvm_data_slices: 0,
            },
            start_options: StartOptions {
                read_only: false,
                verbose: false,
                fsck_after_every_transaction: false,
                write_compression_level: -1,
                write_compression_algorithm: CompressionAlgorithm::ZstdChunked,
                cache_eviction_policy_override: EvictionPolicyOverride::None,
            },
            component_type: self.component_type.clone(),
        }
    }
    fn is_multi_volume(&self) -> bool {
        false
    }

    fn disk_format(&self) -> format::DiskFormat {
        format::DiskFormat::F2fs
    }
}

/// Factoryfs Filesystem Configuration
/// If fields are None or false, they will not be set in arguments.
#[derive(Clone, Default)]
pub struct Factoryfs {
    pub verbose: bool,
}

impl Factoryfs {
    /// Manages a block device using the default configuration.
    pub fn new(block_device: fidl_fuchsia_device::ControllerProxy) -> filesystem::Filesystem {
        filesystem::Filesystem::new(block_device, Self::default())
    }
}

impl FSConfig for Factoryfs {
    // TODO(fxbug.dev/117437): launch factoryfs as a component so we can remove the legacy mode.
    fn mode(&self) -> Mode<'_> {
        Mode::Legacy(LegacyConfig {
            binary_path: cstr!("/pkg/bin/factoryfs"),
            generic_args: {
                let mut args = vec![];
                if self.verbose {
                    args.push(cstr!("--verbose"));
                }
                args
            },
            ..Default::default()
        })
    }

    fn disk_format(&self) -> format::DiskFormat {
        format::DiskFormat::FactoryFs
    }
}