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
use std::ffi::CStr;
use std::io::{self, Read};
use std::mem::size_of;
use std::sync::Arc;
use arc_swap::ArcSwap;
use super::filesystem::{FileSystem, ZeroCopyReader, ZeroCopyWriter};
use crate::abi::linux_abi::*;
use crate::transport::{FileReadWriteVolatile, Reader, Writer};
use crate::{bytes_to_cstr, Error, Result};
#[cfg(feature = "async-io")]
mod async_io;
mod sync_io;
pub use sync_io::MetricsHook;
const MAX_BUFFER_SIZE: u32 = 1 << 20;
const MAX_REQ_PAGES: u16 = 256;
const DIRENT_PADDING: [u8; 8] = [0; 8];
pub struct Server<F: FileSystem + Sync> {
fs: F,
vers: ArcSwap<ServerVersion>,
}
impl<F: FileSystem + Sync> Server<F> {
pub fn new(fs: F) -> Server<F> {
Server {
fs,
vers: ArcSwap::new(Arc::new(ServerVersion {
major: KERNEL_VERSION,
minor: KERNEL_MINOR_VERSION,
})),
}
}
}
struct ZcReader<'a>(Reader<'a>);
impl<'a> ZeroCopyReader for ZcReader<'a> {
fn read_to(
&mut self,
f: &mut dyn FileReadWriteVolatile,
count: usize,
off: u64,
) -> io::Result<usize> {
self.0.read_to_at(f, count, off)
}
}
impl<'a> io::Read for ZcReader<'a> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.0.read(buf)
}
}
struct ZcWriter<'a>(Writer<'a>);
impl<'a> ZeroCopyWriter for ZcWriter<'a> {
fn write_from(
&mut self,
f: &mut dyn FileReadWriteVolatile,
count: usize,
off: u64,
) -> io::Result<usize> {
self.0.write_from_at(f, count, off)
}
}
impl<'a> io::Write for ZcWriter<'a> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.0.write(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.0.flush()
}
}
#[allow(dead_code)]
struct ServerVersion {
major: u32,
minor: u32,
}
struct ServerUtil();
impl ServerUtil {
fn get_message_body(
r: &mut Reader,
in_header: &InHeader,
sub_hdr_sz: usize,
) -> Result<Vec<u8>> {
let len = (in_header.len as usize)
.checked_sub(size_of::<InHeader>())
.and_then(|l| l.checked_sub(sub_hdr_sz))
.ok_or(Error::InvalidHeaderLength)?;
let mut buf = Vec::<u8>::with_capacity(len);
unsafe { buf.set_len(len) };
r.read_exact(&mut buf).map_err(Error::DecodeMessage)?;
Ok(buf)
}
fn extract_two_cstrs(buf: &[u8]) -> Result<(&CStr, &CStr)> {
if let Some(mut pos) = buf.iter().position(|x| *x == 0) {
let first = CStr::from_bytes_with_nul(&buf[0..=pos]).map_err(Error::InvalidCString)?;
pos += 1;
if pos < buf.len() {
return Ok((first, bytes_to_cstr(&buf[pos..])?));
}
}
Err(Error::DecodeMessage(std::io::Error::from_raw_os_error(
libc::EINVAL,
)))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_extract_cstrs() {
assert_eq!(
ServerUtil::extract_two_cstrs(&[0x1u8, 0x2u8, 0x0, 0x3, 0x0]).unwrap(),
(
CStr::from_bytes_with_nul(&[0x1u8, 0x2u8, 0x0]).unwrap(),
CStr::from_bytes_with_nul(&[0x3u8, 0x0]).unwrap(),
)
);
assert_eq!(
ServerUtil::extract_two_cstrs(&[0x1u8, 0x2u8, 0x0, 0x3, 0x0, 0x0]).unwrap(),
(
CStr::from_bytes_with_nul(&[0x1u8, 0x2u8, 0x0]).unwrap(),
CStr::from_bytes_with_nul(&[0x3u8, 0x0]).unwrap(),
)
);
assert_eq!(
ServerUtil::extract_two_cstrs(&[0x1u8, 0x2u8, 0x0, 0x3, 0x0, 0x4]).unwrap(),
(
CStr::from_bytes_with_nul(&[0x1u8, 0x2u8, 0x0]).unwrap(),
CStr::from_bytes_with_nul(&[0x3u8, 0x0]).unwrap(),
)
);
assert_eq!(
ServerUtil::extract_two_cstrs(&[0x1u8, 0x2u8, 0x0, 0x0, 0x4]).unwrap(),
(
CStr::from_bytes_with_nul(&[0x1u8, 0x2u8, 0x0]).unwrap(),
CStr::from_bytes_with_nul(&[0x0]).unwrap(),
)
);
ServerUtil::extract_two_cstrs(&[0x1u8, 0x2u8, 0x0, 0x3]).unwrap_err();
ServerUtil::extract_two_cstrs(&[0x1u8, 0x2u8, 0x0]).unwrap_err();
ServerUtil::extract_two_cstrs(&[0x1u8, 0x2u8]).unwrap_err();
}
}