fs_management/
lib.rs

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
// 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 fidl_fuchsia_fs_startup::{
    CompressionAlgorithm, EvictionPolicyOverride, FormatOptions, StartOptions,
};

use std::convert::From;
use std::sync::Arc;

// Re-export errors as public.
pub use error::{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";

#[derive(Clone)]
pub enum ComponentType {
    /// Launch the filesystem as a static child, using the configured name in the options 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 struct Options<'a> {
    /// For static children, the name specifies the name of the child.  For dynamic children, the
    /// component URL is "fuchsia-boot:///{component-name}#meta/{component-name}.cm" or
    /// "#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. It will try to
    /// create a child component via the absolute URL and then fallback to the relative URL.
    pub component_name: &'a str,

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

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

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

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

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

    /// 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, Default)]
pub enum BlobCompression {
    #[default]
    ZSTDChunked,
    Uncompressed,
}

impl From<&str> for BlobCompression {
    fn from(value: &str) -> Self {
        match value {
            "zstd_chunked" => Self::ZSTDChunked,
            "uncompressed" => Self::Uncompressed,
            _ => Default::default(),
        }
    }
}

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

impl From<&str> for BlobEvictionPolicy {
    fn from(value: &str) -> Self {
        match value {
            "never_evict" => Self::NeverEvict,
            "evict_immediately" => Self::EvictImmediately,
            _ => Default::default(),
        }
    }
}

/// 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: BlobCompression,
    pub write_compression_level: Option<i32>,
    pub cache_eviction_policy_override: 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 options(&self) -> Options<'_> {
        Options {
            component_name: "blobfs",
            reuse_component_after_serving: false,
            format_options: FormatOptions {
                verbose: Some(self.verbose),
                deprecated_padded_blobfs_format: Some(self.deprecated_padded_blobfs_format),
                num_inodes: if self.num_inodes > 0 { Some(self.num_inodes) } else { None },
                ..Default::default()
            },
            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,
                    startup_profiling_seconds: 0,
                };
                start_options.write_compression_algorithm = match &self.write_compression_algorithm
                {
                    BlobCompression::ZSTDChunked => CompressionAlgorithm::ZstdChunked,
                    BlobCompression::Uncompressed => CompressionAlgorithm::Uncompressed,
                };
                start_options.cache_eviction_policy_override =
                    match &self.cache_eviction_policy_override {
                        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 options(&self) -> Options<'_> {
        Options {
            component_name: "minfs",
            reuse_component_after_serving: false,
            format_options: FormatOptions {
                verbose: Some(self.verbose),
                fvm_data_slices: Some(self.fvm_data_slices),
                ..Default::default()
            },
            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,
                startup_profiling_seconds: 0,
            },
            component_type: self.component_type.clone(),
        }
    }

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

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

/// Fxfs Filesystem Configuration
#[derive(Clone)]
pub struct Fxfs {
    // Start Options
    pub readonly: bool,
    pub fsck_after_every_transaction: bool,
    pub component_type: ComponentType,
    pub startup_profiling_seconds: Option<u32>,
}

impl Default for Fxfs {
    fn default() -> Self {
        Self {
            readonly: false,
            fsck_after_every_transaction: false,
            component_type: Default::default(),
            startup_profiling_seconds: None,
        }
    }
}

impl Fxfs {
    /// 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 options(&self) -> Options<'_> {
        Options {
            component_name: "fxfs",
            reuse_component_after_serving: true,
            format_options: FormatOptions { verbose: Some(false), ..Default::default() },
            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,
                startup_profiling_seconds: self.startup_profiling_seconds.unwrap_or(0),
            },
            component_type: self.component_type.clone(),
        }
    }

    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 options(&self) -> Options<'_> {
        Options {
            component_name: "f2fs",
            reuse_component_after_serving: false,
            format_options: FormatOptions::default(),
            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,
                startup_profiling_seconds: 0,
            },
            component_type: self.component_type.clone(),
        }
    }
    fn is_multi_volume(&self) -> bool {
        false
    }

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

/// FvmFilesystem Configuration
#[derive(Clone)]
pub struct Fvm {
    pub component_type: ComponentType,
}

impl Default for Fvm {
    fn default() -> Self {
        Self { component_type: Default::default() }
    }
}

impl Fvm {
    /// 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 Fvm, 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 Fvm {
    fn options(&self) -> Options<'_> {
        Options {
            component_name: "fvm",
            reuse_component_after_serving: true,
            format_options: FormatOptions::default(),
            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,
                startup_profiling_seconds: 0,
            },
            component_type: self.component_type.clone(),
        }
    }

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

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

/// Gpt Configuration
#[derive(Clone)]
pub struct Gpt {
    pub component_type: ComponentType,
}

impl Default for Gpt {
    fn default() -> Self {
        Self { component_type: Default::default() }
    }
}

impl Gpt {
    /// 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 Gpt, 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 Gpt {
    fn options(&self) -> Options<'_> {
        Options {
            component_name: "storage-host",
            reuse_component_after_serving: true,
            format_options: FormatOptions::default(),
            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,
                startup_profiling_seconds: 0,
            },
            component_type: self.component_type.clone(),
        }
    }

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

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