|
| 1 | +use alloc::{boxed::Box, vec}; |
| 2 | +use core::mem; |
| 3 | + |
| 4 | +use axdriver::prelude::*; |
| 5 | + |
| 6 | +fn take<'a>(buf: &mut &'a [u8], cnt: usize) -> &'a [u8] { |
| 7 | + let (first, rem) = buf.split_at(cnt); |
| 8 | + *buf = rem; |
| 9 | + first |
| 10 | +} |
| 11 | + |
| 12 | +fn take_mut<'a>(buf: &mut &'a mut [u8], cnt: usize) -> &'a mut [u8] { |
| 13 | + // use mem::take to circumvent lifetime issues |
| 14 | + let (first, rem) = mem::take(buf).split_at_mut(cnt); |
| 15 | + *buf = rem; |
| 16 | + first |
| 17 | +} |
| 18 | + |
| 19 | +/// A disk device with a cursor. |
| 20 | +pub struct SeekableDisk { |
| 21 | + dev: AxBlockDevice, |
| 22 | + |
| 23 | + block_id: u64, |
| 24 | + offset: usize, |
| 25 | + block_size_log2: u8, |
| 26 | + |
| 27 | + read_buffer: Box<[u8]>, |
| 28 | + write_buffer: Box<[u8]>, |
| 29 | + /// Whether we have unsaved changes in the write buffer. |
| 30 | + /// |
| 31 | + /// It's guaranteed that when `offset == 0`, write_buffer_dirty is false. |
| 32 | + write_buffer_dirty: bool, |
| 33 | +} |
| 34 | + |
| 35 | +impl SeekableDisk { |
| 36 | + /// Create a new disk. |
| 37 | + pub fn new(dev: AxBlockDevice) -> Self { |
| 38 | + assert!(dev.block_size().is_power_of_two()); |
| 39 | + let block_size_log2 = dev.block_size().trailing_zeros() as u8; |
| 40 | + let read_buffer = vec![0u8; dev.block_size()].into_boxed_slice(); |
| 41 | + let write_buffer = vec![0u8; dev.block_size()].into_boxed_slice(); |
| 42 | + Self { |
| 43 | + dev, |
| 44 | + block_id: 0, |
| 45 | + offset: 0, |
| 46 | + block_size_log2, |
| 47 | + read_buffer, |
| 48 | + write_buffer, |
| 49 | + write_buffer_dirty: false, |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /// Get the size of the disk. |
| 54 | + pub fn size(&self) -> u64 { |
| 55 | + self.dev.num_blocks() << self.block_size_log2 |
| 56 | + } |
| 57 | + |
| 58 | + /// Get the block size. |
| 59 | + pub fn block_size(&self) -> usize { |
| 60 | + 1 << self.block_size_log2 |
| 61 | + } |
| 62 | + |
| 63 | + /// Get the position of the cursor. |
| 64 | + pub fn position(&self) -> u64 { |
| 65 | + (self.block_id << self.block_size_log2) + self.offset as u64 |
| 66 | + } |
| 67 | + |
| 68 | + /// Set the position of the cursor. |
| 69 | + pub fn set_position(&mut self, pos: u64) -> DevResult<()> { |
| 70 | + self.flush()?; |
| 71 | + self.block_id = pos >> self.block_size_log2; |
| 72 | + self.offset = pos as usize & (self.block_size() - 1); |
| 73 | + Ok(()) |
| 74 | + } |
| 75 | + |
| 76 | + /// Write all pending changes to the disk. |
| 77 | + pub fn flush(&mut self) -> DevResult<()> { |
| 78 | + if self.write_buffer_dirty { |
| 79 | + self.dev.write_block(self.block_id, &self.write_buffer)?; |
| 80 | + self.write_buffer_dirty = false; |
| 81 | + } |
| 82 | + Ok(()) |
| 83 | + } |
| 84 | + |
| 85 | + fn read_partial(&mut self, buf: &mut &mut [u8]) -> DevResult<usize> { |
| 86 | + self.flush()?; |
| 87 | + self.dev.read_block(self.block_id, &mut self.read_buffer)?; |
| 88 | + |
| 89 | + let data = &self.read_buffer[self.offset..]; |
| 90 | + let length = buf.len().min(data.len()); |
| 91 | + take_mut(buf, length).copy_from_slice(&data[..length]); |
| 92 | + |
| 93 | + self.offset += length; |
| 94 | + if self.offset == self.block_size() { |
| 95 | + self.block_id += 1; |
| 96 | + self.offset = 0; |
| 97 | + } |
| 98 | + |
| 99 | + Ok(length) |
| 100 | + } |
| 101 | + |
| 102 | + /// Read from the disk, returns the number of bytes read. |
| 103 | + pub fn read(&mut self, mut buf: &mut [u8]) -> DevResult<usize> { |
| 104 | + let mut read = 0; |
| 105 | + if self.offset != 0 { |
| 106 | + read += self.read_partial(&mut buf)?; |
| 107 | + } |
| 108 | + if buf.len() >= self.block_size() { |
| 109 | + let blocks = buf.len() >> self.block_size_log2; |
| 110 | + let length = blocks << self.block_size_log2; |
| 111 | + self.dev |
| 112 | + .read_block(self.block_id, take_mut(&mut buf, length))?; |
| 113 | + read += length; |
| 114 | + |
| 115 | + self.block_id += blocks as u64; |
| 116 | + } |
| 117 | + if !buf.is_empty() { |
| 118 | + read += self.read_partial(&mut buf)?; |
| 119 | + } |
| 120 | + |
| 121 | + Ok(read) |
| 122 | + } |
| 123 | + |
| 124 | + fn write_partial(&mut self, buf: &mut &[u8]) -> DevResult<usize> { |
| 125 | + if !self.write_buffer_dirty { |
| 126 | + self.dev.read_block(self.block_id, &mut self.write_buffer)?; |
| 127 | + self.write_buffer_dirty = true; |
| 128 | + } |
| 129 | + |
| 130 | + let data = &mut self.write_buffer[self.offset..]; |
| 131 | + let length = buf.len().min(data.len()); |
| 132 | + data[..length].copy_from_slice(take(buf, length)); |
| 133 | + |
| 134 | + self.offset += length; |
| 135 | + if self.offset == self.block_size() { |
| 136 | + self.flush()?; |
| 137 | + self.block_id += 1; |
| 138 | + self.offset = 0; |
| 139 | + } |
| 140 | + |
| 141 | + Ok(length) |
| 142 | + } |
| 143 | + |
| 144 | + /// Write to the disk, returns the number of bytes written. |
| 145 | + pub fn write(&mut self, mut buf: &[u8]) -> DevResult<usize> { |
| 146 | + let mut written = 0; |
| 147 | + if self.offset != 0 { |
| 148 | + written += self.write_partial(&mut buf)?; |
| 149 | + } |
| 150 | + if buf.len() >= self.block_size() { |
| 151 | + let blocks = buf.len() >> self.block_size_log2; |
| 152 | + let length = blocks << self.block_size_log2; |
| 153 | + self.dev |
| 154 | + .write_block(self.block_id, take(&mut buf, length))?; |
| 155 | + written += length; |
| 156 | + |
| 157 | + self.block_id += blocks as u64; |
| 158 | + } |
| 159 | + if !buf.is_empty() { |
| 160 | + written += self.write_partial(&mut buf)?; |
| 161 | + } |
| 162 | + |
| 163 | + Ok(written) |
| 164 | + } |
| 165 | +} |
0 commit comments