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
//! The steps writes and truncates share under the lock of a file: the size
//! of the stored file, the header the change works under, and the retry after
//! a block that does not open.
use std::io;
use std::mem;
use tokio::sync::RwLock;
use super::block::FileCipher;
use super::format::{FILE_ID_LEN, NONCE_LEN};
use super::handle::EncryptedHandle;
use super::header::Header;
use super::node::{FileMeta, HeaderState};
use super::random::{random_file_id, random_nonces};
use super::size::{logical_size, stored_size};
use crate::backend::ShareBackend;
use crate::error::{SmbError, SmbResult};
/// Where a change draws its random bytes.
pub(super) trait Random: Send {
/// One nonce for each of `nonces`, none of them all zero.
fn nonces(&mut self, nonces: &mut [[u8; NONCE_LEN]]) -> Result<(), getrandom::Error>;
/// A file id that is not all zero.
fn file_id(&mut self) -> Result<[u8; FILE_ID_LEN], getrandom::Error>;
}
/// The OS generator.
pub(super) struct OsRandom;
impl Random for OsRandom {
fn nonces(&mut self, nonces: &mut [[u8; NONCE_LEN]]) -> Result<(), getrandom::Error> {
random_nonces(nonces)
}
fn file_id(&mut self) -> Result<[u8; FILE_ID_LEN], getrandom::Error> {
random_file_id()
}
}
/// The generator failed.
pub(super) fn random_error(error: getrandom::Error) -> SmbError {
SmbError::Io(io::Error::other(error))
}
/// A change of the contents of a file.
#[derive(Clone, Copy)]
pub(super) enum Change<'a> {
Write {
offset: u64,
data: &'a [u8],
},
/// Sets the length to `len`, which takes `target` stored bytes.
Truncate {
len: u64,
target: u64,
},
}
/// How one try at a change ended.
pub(super) enum Attempt {
Done,
/// The block with that index, in the file with that id, does not open.
Corrupt {
block: u64,
used: [u8; FILE_ID_LEN],
},
}
/// The sizes of a stored file after `normalize`.
#[derive(Clone, Copy)]
pub(super) struct Normal {
/// Bytes of the file as clients see it.
pub(super) logical: u64,
/// Bytes of the stored file: the size `logical` gives.
pub(super) end: u64,
}
/// The header a change works under, and the cipher of its key.
pub(super) struct Taken {
pub(super) header: Header,
pub(super) cipher: Box<FileCipher>,
/// The header is new and not in the stored file yet.
pub(super) fresh: bool,
}
impl Taken {
/// Puts the header back into `meta`. A new header goes back only when
/// the change is `done`, which means it is in the stored file.
pub(super) fn put_back(self, meta: &mut FileMeta, done: bool) {
if !self.fresh || done {
meta.header = HeaderState::Present(self.header, self.cipher);
}
}
}
impl<B: ShareBackend> EncryptedHandle<B> {
/// Cuts the stored file to the size its logical size gives: a torn
/// header, a header alone or a fragment after the last block does not
/// stay in the file. A stored size that passes the largest one fails with
/// `InvalidParameter`.
pub(super) async fn normalize(&self) -> SmbResult<Normal> {
let shift = self.shared.settings.block_shift;
let stored = self.stored_size().await?;
let logical = logical_size(shift, stored).size;
let end = stored_size(shift, logical).ok_or(SmbError::InvalidParameter)?;
if end != stored {
self.inner.truncate(end).await?;
}
Ok(Normal { logical, end })
}
/// The header a change works under, for a stored file of `end` bytes. A
/// file without data gets a header with a new file id, which
/// `Taken::put_back` caches once it is stored. The header of a file with
/// data is taken out of `meta` while the change runs; a change that is
/// dropped leaves it to be read again.
pub(super) async fn take_header(
&self,
meta: &mut FileMeta,
end: u64,
random: &mut dyn Random,
) -> SmbResult<Taken> {
if end == 0 {
meta.header = HeaderState::Absent;
let id = random.file_id().map_err(random_error)?;
let header = Header::new(self.shared.settings.block_shift, id)
.expect("the generator returns a non-zero file id");
let cipher = Box::new(FileCipher::new(&self.shared.keys, header));
return Ok(Taken {
header,
cipher,
fresh: true,
});
}
let (header, cipher) = match mem::replace(&mut meta.header, HeaderState::Unknown) {
HeaderState::Present(header, cipher) => (header, cipher),
HeaderState::Unknown | HeaderState::Absent => self.read_header().await?,
};
Ok(Taken {
header,
cipher,
fresh: false,
})
}
/// Applies `change` to the file under its exclusive lock. A block that
/// does not open makes the header reload, once. The change runs again if
/// the stored file has a different file id, or none, and fails with the
/// block error otherwise. An error of the reload is returned instead.
pub(super) async fn change_file(
&self,
file: &RwLock<FileMeta>,
change: Change<'_>,
random: &mut dyn Random,
) -> SmbResult<()> {
let mut meta = file.write().await;
let mut reloaded = false;
loop {
let attempt = match change {
Change::Write { offset, data } => {
self.write_once(&mut meta, offset, data, &mut *random)
.await?
}
Change::Truncate { len, target } => {
self.truncate_once(&mut meta, len, target, &mut *random)
.await?
}
};
let Attempt::Corrupt { block, used } = attempt else {
return Ok(());
};
if !reloaded && self.reload(&mut meta, used).await? {
reloaded = true;
continue;
}
return Err(self.block_error(block));
}
}
}