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

//! Storage related interfaces
//! Those interface allows to simplify IStorage <=> StorageDevice layer.

use crate::LibUserResult;
use sunrise_libuser::error::{Error, FileSystemError};
use storage_device::{BlockDevice, StorageDevice};
use storage_device::storage_device::StorageBlockDevice;
use crate::interface::filesystem::FileOperations;

use alloc::sync::Arc;
use alloc::boxed::Box;
use spin::Mutex;
use core::fmt::Debug;

/// This is the interface for a raw device, usually a block device.
pub trait IStorage : StorageDevice + Debug + Sync + Send {
    /// Set the total size of the storage in bytes.
    fn set_size(&mut self, new_size: u64) -> LibUserResult<()>;
}

#[derive(Debug)]
/// Wrapper over a IStorage that permit to access only a partition.
pub struct PartitionStorage {
    /// The backing IStorage implementation
    inner: Arc<Mutex<Box<dyn IStorage<Error = Error>>>>,

    /// The start of the partition.
    partition_start: u64,

    /// The size of the partition.
    partition_len: u64
}

impl PartitionStorage {
    /// Create a new PartitionStorage
    pub fn new(inner: Arc<Mutex<Box<dyn IStorage<Error = Error>>>>, partition_start: u64, partition_len: u64) -> Self {
        PartitionStorage { inner, partition_start, partition_len}
    }
}

impl StorageDevice for PartitionStorage {
    type Error = Error;

    fn read(&mut self, offset: u64, buf: &mut [u8]) -> Result<(), Error> {
        if offset + buf.len() as u64 > self.len()? {
            return Err(FileSystemError::OutOfRange.into())
        }

        self.inner.lock().read(self.partition_start + offset, buf)
    }

    fn write(&mut self, offset: u64, buf: &[u8]) -> Result<(), Error> {
        if offset + buf.len() as u64 > self.len()? {
            return Err(FileSystemError::OutOfRange.into())
        }

        self.inner.lock().write(self.partition_start + offset, buf)
    }

    fn flush(&mut self) -> LibUserResult<()> {
        self.inner.lock().flush()
    }

    fn len(&mut self) -> Result<u64, Error> {
        Ok(self.partition_len)
    }
}

impl IStorage for PartitionStorage {
    fn set_size(&mut self, _new_size: u64) -> LibUserResult<()> {
        Err(FileSystemError::UnsupportedOperation.into())
    }
}

/// Wrapper around a [FileOperations] exposing a StorageDevice interface.
#[repr(transparent)]
#[derive(Debug)]
pub struct FileStorage<F: FileOperations>(F);

impl<F: FileOperations> FileStorage<F> {
    /// Creates a new StorageDevice from the given FileOperations.
    pub fn new(f: F) -> FileStorage<F> {
        FileStorage(f)
    }
}

impl<F: FileOperations> StorageDevice for FileStorage<F> {
    type Error = Error;

    /// Read the data at the given ``offset`` in the storage into a given buffer.
    fn read(&mut self, offset: u64, mut buf: &mut [u8]) -> LibUserResult<()> {
        while !buf.is_empty() {
            let data_read = FileOperations::read(&mut self.0, offset, buf)?;
            if data_read == 0 {
                return Err(FileSystemError::OutOfRange.into());
            }
            buf = &mut buf[data_read as usize..];
        }
        Ok(())
    }

    /// Write the data from the given buffer at the given ``offset`` in the storage.
    fn write(&mut self, offset: u64, buf: &[u8]) -> LibUserResult<()> {
        FileOperations::write(&mut self.0, offset, buf)
    }

    /// Writes every dirty data to the storage.
    fn flush(&mut self) -> LibUserResult<()> {
        FileOperations::flush(&mut self.0)
    }

    fn len(&mut self) -> Result<u64, Error> {
        FileOperations::get_len(&mut self.0)
    }
}

impl<F: FileOperations> IStorage for FileStorage<F> {
    /// Set the total size of the storage in bytes.
    fn set_size(&mut self, new_size: u64) -> LibUserResult<()> {
        FileOperations::set_len(&mut self.0, new_size)
    }
}

impl<B> IStorage for StorageBlockDevice<B>
where
    B: BlockDevice<Error = Error> + Send + Sync,
    B::Block: Send + Sync
{
    fn set_size(&mut self, _new_size: u64) -> Result<(), B::Error> {
        Err(FileSystemError::ReadOnlyFileSystem.into())
    }
}