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
use crate::sys::error::FsResult;
use crate::sys::file::{FileInner, FileStatus};
use crate::sys::metadata::MD_USER_DATA_SIZE;
use crate::sys::node::{FileNode, FileNodeRef, NodeType};
use crate::sys::node::{ATTACHED_DATA_NODES_COUNT, CHILD_MHT_NODES_COUNT, NODE_SIZE};
use sgx_types::error::SgxStatus;
impl FileInner {
pub fn get_data_node(&mut self) -> FsResult<FileNodeRef> {
ensure!(
self.offset >= MD_USER_DATA_SIZE,
esgx!(SgxStatus::Unexpected)
);
let data_node = if ((self.offset - MD_USER_DATA_SIZE) % NODE_SIZE == 0)
&& (self.offset == self.metadata.encrypted_plain.size)
{
self.append_data_node()
} else {
self.read_data_node()
};
if let Ok(ref data_node) = data_node {
self.bump_mht_node(data_node);
}
self.shrink_cache()?;
data_node
}
fn get_mht_node(&mut self) -> FsResult<FileNodeRef> {
ensure!(
self.offset >= MD_USER_DATA_SIZE,
esgx!(SgxStatus::Unexpected)
);
let (logic_number, _) = self.get_mht_node_numbers();
if logic_number == 0 {
return Ok(self.root_mht.clone());
}
if ((self.offset - MD_USER_DATA_SIZE) % (ATTACHED_DATA_NODES_COUNT as usize * NODE_SIZE)
== 0)
&& self.offset == self.metadata.encrypted_plain.size
{
self.append_mht_node(logic_number)
} else {
self.read_mht_node(logic_number)
}
}
fn append_mht_node(&mut self, logic_number: u64) -> FsResult<FileNodeRef> {
let parent_mht_node = self.read_mht_node((logic_number - 1) / CHILD_MHT_NODES_COUNT)?;
let physical_number = 1 + logic_number * (ATTACHED_DATA_NODES_COUNT + 1);
let mut mht_node = FileNode::new(
NodeType::Mht,
logic_number,
physical_number,
self.metadata.encrypt_flags(),
);
mht_node.parent = Some(parent_mht_node);
let mht_node = FileNode::build_ref(mht_node);
ensure!(
self.cache.push(physical_number, mht_node.clone()),
esgx!(SgxStatus::Unexpected)
);
Ok(mht_node)
}
fn read_mht_node(&mut self, logic_number: u64) -> FsResult<FileNodeRef> {
if logic_number == 0 {
return Ok(self.root_mht.clone());
}
let physical_number = 1 + logic_number * (ATTACHED_DATA_NODES_COUNT + 1);
if let Some(mht_node) = self.cache.find(physical_number) {
return Ok(mht_node);
}
let parent_mht_node = self.read_mht_node((logic_number - 1) / CHILD_MHT_NODES_COUNT)?;
let mut mht_node = FileNode::new(
NodeType::Mht,
logic_number,
physical_number,
self.metadata.encrypt_flags(),
);
mht_node.parent = Some(parent_mht_node);
mht_node.read_from_disk(&mut self.host_file)?;
let gcm_data = mht_node.get_gcm_data().ok_or(SgxStatus::Unexpected)?;
mht_node.decrypt(&gcm_data.key, &gcm_data.mac)?;
let mht_node = FileNode::build_ref(mht_node);
ensure!(
self.cache.push(physical_number, mht_node.clone()),
esgx!(SgxStatus::Unexpected)
);
Ok(mht_node)
}
fn append_data_node(&mut self) -> FsResult<FileNodeRef> {
let mht_node = self.get_mht_node()?;
let (logic_number, physical_number) = self.get_data_node_numbers();
let mut data_node = FileNode::new(
NodeType::Data,
logic_number,
physical_number,
self.metadata.encrypt_flags(),
);
data_node.parent = Some(mht_node);
let data_node = FileNode::build_ref(data_node);
ensure!(
self.cache.push(physical_number, data_node.clone()),
esgx!(SgxStatus::Unexpected)
);
Ok(data_node)
}
fn read_data_node(&mut self) -> FsResult<FileNodeRef> {
let (logic_number, physical_number) = self.get_data_node_numbers();
if let Some(data_node) = self.cache.find(physical_number) {
return Ok(data_node);
}
let mht_node = self.get_mht_node()?;
let mut data_node = FileNode::new(
NodeType::Data,
logic_number,
physical_number,
self.metadata.encrypt_flags(),
);
data_node.parent = Some(mht_node);
data_node.read_from_disk(&mut self.host_file)?;
let gcm_data = data_node.get_gcm_data().ok_or(SgxStatus::Unexpected)?;
data_node.decrypt(&gcm_data.key, &gcm_data.mac)?;
let data_node = FileNode::build_ref(data_node);
ensure!(
self.cache.push(physical_number, data_node.clone()),
esgx!(SgxStatus::Unexpected)
);
Ok(data_node)
}
fn bump_mht_node(&mut self, node: &FileNodeRef) {
let mut parent = node.borrow().parent.clone();
while let Some(mht) = parent {
let mht = mht.borrow();
if !mht.is_root_mht() {
self.cache.move_to_head(mht.ciphertext.physical_number);
parent = mht.parent.clone();
} else {
break;
}
}
}
fn shrink_cache(&mut self) -> FsResult {
while self.cache.len() > self.max_cache_page {
let node = self.cache.back().ok_or(SgxStatus::Unexpected)?;
if !node.borrow().need_writing {
let _node = self.cache.pop_back();
} else {
self.internal_flush(false).map_err(|error| {
if self.status.is_ok() {
self.set_file_status(FileStatus::FlushError);
}
error
})?;
}
}
Ok(())
}
fn get_node_numbers(&self) -> (u64, u64, u64, u64) {
if self.offset < MD_USER_DATA_SIZE {
return (0, 0, 0, 0);
}
let data_logic_number = ((self.offset - MD_USER_DATA_SIZE) / NODE_SIZE) as u64;
let mht_logic_number = data_logic_number / ATTACHED_DATA_NODES_COUNT;
let data_physical_number = data_logic_number + 1 + 1 + mht_logic_number;
let mht_physical_number =
data_physical_number - data_logic_number % ATTACHED_DATA_NODES_COUNT - 1;
(
mht_logic_number,
data_logic_number,
mht_physical_number,
data_physical_number,
)
}
#[inline]
fn get_data_node_numbers(&self) -> (u64, u64) {
let (_, logic, _, physical) = self.get_node_numbers();
(logic, physical)
}
#[inline]
fn get_mht_node_numbers(&self) -> (u64, u64) {
let (logic, _, physical, _) = self.get_node_numbers();
(logic, physical)
}
}