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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
#![deny(missing_docs)]
#![forbid(unsafe_code)]
extern crate byteorder;
#[derive(Debug, Default, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct Export {
pub size: u64,
pub readonly: bool,
pub resizeable: bool,
pub rotational: bool,
pub send_trim: bool,
pub send_flush: bool,
}
fn strerror(s: &'static str) -> std::io::Result<()> {
Err(std::io::Error::new(std::io::ErrorKind::InvalidData, s))
}
fn mycopy<R: ?Sized, W: ?Sized>(
reader: &mut R,
writer: &mut W,
buf: &mut [u8],
mut limit: usize,
) -> ::std::io::Result<u64>
where
R: ::std::io::Read,
W: ::std::io::Write,
{
let mut written = 0;
loop {
let to_read = buf.len().min(limit);
let len = match reader.read(&mut buf[0..to_read]) {
Ok(0) => return Ok(written),
Ok(len) => len,
Err(ref e) if e.kind() == ::std::io::ErrorKind::Interrupted => continue,
Err(e) => return Err(e),
};
writer.write_all(&buf[..len])?;
written += len as u64;
limit -= len;
if limit == 0 {
return Ok(written);
}
}
}
pub mod server {
use super::consts::*;
use super::{mycopy, strerror};
use byteorder::{BigEndian as BE, ReadBytesExt, WriteBytesExt};
use std::io::{Error, Read, Result, Seek, SeekFrom, Write};
#[doc(hidden)]
pub fn oldstyle_header<W: Write>(mut c: W, size: u64, flags: u32) -> Result<()> {
c.write_all(b"NBDMAGIC")?;
c.write_all(b"\x00\x00\x42\x02\x81\x86\x12\x53")?;
c.write_u64::<BE>(size)?;
c.write_u32::<BE>(flags)?;
c.write_all(&[0; 124])?;
c.flush()?;
Ok(())
}
fn reply<IO: Write + Read>(mut c: IO, clopt: u32, rtype: u32, data: &[u8]) -> Result<()> {
c.write_u64::<BE>(0x3e889045565a9)?;
c.write_u32::<BE>(clopt)?;
c.write_u32::<BE>(rtype)?;
c.write_u32::<BE>(data.len() as u32)?;
c.write_all(data)?;
c.flush()?;
Ok(())
}
pub use super::Export;
pub fn handshake<IO: Write + Read>(mut c: IO, export: &Export) -> Result<()> {
let hs_flags = NBD_FLAG_FIXED_NEWSTYLE;
c.write_all(b"NBDMAGIC")?;
c.write_all(b"IHAVEOPT")?;
c.write_u16::<BE>(hs_flags)?;
c.flush()?;
let client_flags = c.read_u32::<BE>()?;
if client_flags != NBD_FLAG_C_FIXED_NEWSTYLE {
strerror("Invalid client flag")?;
}
loop {
let client_optmagic = c.read_u64::<BE>()?;
if client_optmagic != 0x49484156454F5054 {
strerror("Invalid client optmagic")?;
}
let clopt = c.read_u32::<BE>()?;
let optlen = c.read_u32::<BE>()?;
if optlen > 100000 {
strerror("Suspiciously big option length")?;
}
let mut opt = vec![0; optlen as usize];
c.read_exact(&mut opt)?;
match clopt {
NBD_OPT_EXPORT_NAME => {
c.write_u64::<BE>(export.size)?;
let mut flags = NBD_FLAG_HAS_FLAGS;
if export.readonly {
flags |= NBD_FLAG_READ_ONLY
} else {
flags |= NBD_FLAG_SEND_FLUSH
};
if export.resizeable {
flags |= NBD_FLAG_SEND_RESIZE
};
if export.rotational {
flags |= NBD_FLAG_ROTATIONAL
};
if export.send_trim {
flags |= NBD_FLAG_SEND_TRIM
};
c.write_u16::<BE>(flags)?;
c.write_all(&[0; 124])?;
c.flush()?;
return Ok(());
}
NBD_OPT_ABORT => {
reply(&mut c, clopt, NBD_REP_ACK, b"")?;
strerror("Client abort")?;
}
NBD_OPT_LIST => {
if optlen != 0 {
strerror("NBD_OPT_LIST with content")?;
}
reply(&mut c, clopt, NBD_REP_SERVER, b"\x00\x00\x00\x07rustnbd")?;
reply(&mut c, clopt, NBD_REP_ACK, b"")?;
}
NBD_OPT_STARTTLS => {
strerror("TLS not supported")?;
}
NBD_OPT_INFO => {
reply(&mut c, clopt, NBD_REP_ERR_UNSUP, b"")?;
}
NBD_OPT_GO => {
reply(&mut c, clopt, NBD_REP_ERR_UNSUP, b"")?;
}
_ => {
strerror("Invalid client option type")?;
}
}
}
}
fn replyt<IO: Write + Read>(mut c: IO, error: u32, handle: u64) -> Result<()> {
c.write_u32::<BE>(0x67446698)?;
c.write_u32::<BE>(error)?;
c.write_u64::<BE>(handle)?;
Ok(())
}
fn replyte<IO: Write + Read>(mut c: IO, error: Error, handle: u64) -> Result<()> {
let ec = if let Some(x) = error.raw_os_error() {
if (x as u32) != 0 {
x as u32
} else {
5
}
} else {
5
};
replyt(&mut c, ec, handle)
}
pub fn transmission<IO, D>(mut c: IO, mut data: D) -> Result<()>
where
IO: Read + Write,
D: Read + Write + Seek,
{
let mut buf = vec![0; 65536];
loop {
let magic = c.read_u32::<BE>()?;
if magic != 0x25609513 {
strerror("Invalid request magic")?;
}
let _flags = c.read_u16::<BE>()?;
let typ = c.read_u16::<BE>()?;
let handle = c.read_u64::<BE>()?;
let offset = c.read_u64::<BE>()?;
let length = c.read_u32::<BE>()?;
match typ {
NBD_CMD_READ => {
if let Err(e) = data.seek(SeekFrom::Start(offset)) {
replyte(&mut c, e, handle)?;
} else {
replyt(&mut c, 0, handle)?;
match mycopy(&mut data, &mut c, &mut buf, length as usize) {
Err(e) => replyte(&mut c, e, handle)?,
Ok(x) if x == (length as u64) => {}
Ok(_) => {
strerror("sudden EOF")?;
}
}
}
}
NBD_CMD_WRITE => {
if let Err(e) = data.seek(SeekFrom::Start(offset)) {
replyte(&mut c, e, handle)?;
} else {
match mycopy(&mut c, &mut data, &mut buf, length as usize) {
Err(e) => replyte(&mut c, e, handle)?,
Ok(x) if x == (length as u64) => {
replyt(&mut c, 0, handle)?;
}
Ok(_) => {
strerror("sudden EOF")?;
}
}
}
}
NBD_CMD_DISC => {
return Ok(());
}
NBD_CMD_FLUSH => {
data.flush()?;
replyt(&mut c, 0, handle)?;
}
NBD_CMD_TRIM => {
replyt(&mut c, 38, handle)?;
}
NBD_CMD_WRITE_ZEROES => {
replyt(&mut c, 38, handle)?;
}
_ => strerror("Unknown command from client")?,
}
c.flush()?;
}
}
pub const DEFAULT_TCP_PORT: u16 = 10809;
}
pub mod client {
use super::consts::*;
use super::{strerror, CheckedAddI64, ClampToU32};
use byteorder::{BigEndian as BE, ReadBytesExt, WriteBytesExt};
use std::io::{Error, ErrorKind, Read, Result, Seek, SeekFrom, Write};
pub use super::Export;
fn fill_in_flags(export: &mut Export, flags: u16) {
if flags & NBD_FLAG_HAS_FLAGS != 0 {
if flags & NBD_FLAG_READ_ONLY != 0 {
export.readonly = true;
}
if flags & NBD_FLAG_SEND_RESIZE != 0 {
export.resizeable = true;
}
if flags & NBD_FLAG_ROTATIONAL != 0 {
export.rotational = true;
}
if flags & NBD_FLAG_SEND_TRIM != 0 {
export.send_trim = true;
}
if flags & NBD_FLAG_SEND_FLUSH != 0 {
export.send_flush = true;
}
}
}
pub fn handshake<IO: Write + Read>(mut c: IO, name: &[u8]) -> Result<Export> {
let mut signature = [0; 8];
c.read_exact(&mut signature)?;
if signature != *b"NBDMAGIC" {
strerror("Invalid magic1")?;
}
c.read_exact(&mut signature)?;
let (size, flags) = if signature == *b"IHAVEOPT" {
let _hs_flags = c.read_u16::<BE>()?;
c.write_u32::<BE>(NBD_FLAG_C_FIXED_NEWSTYLE)?;
c.write_u64::<BE>(0x49484156454F5054)?;
c.write_u32::<BE>(NBD_OPT_EXPORT_NAME)?;
c.write_u32::<BE>(name.len() as u32)?;
c.write_all(name)?;
c.flush()?;
let size = c.read_u64::<BE>()?;
let flags = c.read_u16::<BE>()?;
let mut z = [0; 124];
c.read_exact(&mut z)?;
if z[..] != [0; 124][..] {
strerror("Expected 124 bytes of zeroes are not zeroes")?;
}
(size, flags)
} else if signature == *b"\x00\x00\x42\x02\x81\x86\x12\x53" {
if name != b"" {
strerror("Old style server does not support named exports")?;
};
let size = c.read_u64::<BE>()?;
let flags = c.read_u32::<BE>()?;
let mut z = [0; 124];
c.read_exact(&mut z)?;
if z[..] != [0; 124][..] {
strerror("Expected 124 bytes of zeroes are not zeroes")?;
}
let flags = flags as u16;
(size, flags)
} else {
strerror("Invalid magic2")?;
unreachable!()
};
let mut e = Export::default();
e.size = size;
fill_in_flags(&mut e, flags);
Ok(e)
}
pub struct NbdClient<IO: Write + Read> {
c: IO,
seek_pos: u64,
size: u64,
}
impl<IO: Write + Read> NbdClient<IO> {
pub fn new(c: IO, export: &Export) -> Self {
NbdClient {
c,
seek_pos: 0,
size: export.size,
}
}
}
impl<IO: Write + Read> Seek for NbdClient<IO> {
fn seek(&mut self, sf: SeekFrom) -> Result<u64> {
match sf {
SeekFrom::Start(x) => {
self.seek_pos = x;
}
SeekFrom::Current(x) => {
if let Some(xx) = self.seek_pos.checked_add_i64(x) {
self.seek_pos = xx;
} else {
strerror("Invalid seek")?;
}
}
SeekFrom::End(x) => {
if let Some(xx) = self.size.checked_add_i64(x) {
self.seek_pos = xx;
} else {
strerror("Invalid seek")?;
}
}
}
Ok(self.seek_pos)
}
}
fn check_err(error: u32) -> Result<()> {
match error {
1 => Err(Error::new(ErrorKind::PermissionDenied, "from device")),
5 => Err(Error::new(ErrorKind::Other, "EIO")),
12 => Err(Error::new(ErrorKind::Other, "ENOMEM")),
22 => Err(Error::new(ErrorKind::Other, "EINVAL")),
28 => Err(Error::new(ErrorKind::Other, "ENOSPC")),
0 => Ok(()),
_ => Err(Error::new(ErrorKind::Other, "other error from device")),
}
}
fn getreply<IO: Write + Read>(mut c: IO) -> Result<()> {
let signature = c.read_u32::<BE>()?;
let error = c.read_u32::<BE>()?;
let handle = c.read_u64::<BE>()?;
if signature != 0x67446698 {
strerror("Invalid signature for incoming reply")?;
}
if handle != 0 {
strerror("Unexpected handle")?;
};
check_err(error)?;
Ok(())
}
fn sendrequest<IO: Write + Read>(mut c: IO, cmd: u16, offset: u64, len: u32) -> Result<()> {
c.write_u32::<BE>(0x25609513)?;
c.write_u16::<BE>(0)?;
c.write_u16::<BE>(cmd)?;
c.write_u64::<BE>(0)?;
c.write_u64::<BE>(offset)?;
c.write_u32::<BE>(len)?;
c.flush()?;
Ok(())
}
impl<IO: Write + Read> NbdClient<IO> {
fn get_effective_len(&self, buflen: usize) -> Result<u32> {
if self.seek_pos == self.size {
return Ok(0);
}
if self.seek_pos > self.size {
strerror("Trying to read or write past the end of the device")?;
}
let maxlen = (self.size - self.seek_pos).clamp_to_u32();
let len = buflen.clamp_to_u32().min(maxlen);
Ok(len)
}
}
impl<IO: Write + Read> Read for NbdClient<IO> {
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
let len = self.get_effective_len(buf.len())?;
if len == 0 {
return Ok(0);
}
sendrequest(&mut self.c, NBD_CMD_READ, self.seek_pos, len)?;
getreply(&mut self.c)?;
self.c.read_exact(&mut buf[0..(len as usize)])?;
self.seek_pos += len as u64;
Ok(len as usize)
}
}
impl<IO: Write + Read> Write for NbdClient<IO> {
fn write(&mut self, buf: &[u8]) -> Result<usize> {
let len = self.get_effective_len(buf.len())?;
if len == 0 {
return Ok(0);
}
sendrequest(&mut self.c, NBD_CMD_WRITE, self.seek_pos, len)?;
self.c.write_all(&buf[0..(len as usize)])?;
self.c.flush()?;
getreply(&mut self.c)?;
self.seek_pos += len as u64;
Ok(len as usize)
}
fn flush(&mut self) -> Result<()> {
sendrequest(&mut self.c, NBD_CMD_FLUSH, 0, 0)?;
getreply(&mut self.c)?;
Ok(())
}
}
pub trait NbdExt {
fn trim(&mut self, length: usize) -> Result<()>;
fn resize(&mut self, newsize: u64) -> Result<()>;
}
impl<IO: Write + Read> NbdExt for NbdClient<IO> {
fn trim(&mut self, length: usize) -> Result<()> {
let len = self.get_effective_len(length)?;
if len == 0 {
return Ok(());
}
sendrequest(&mut self.c, NBD_CMD_TRIM, self.seek_pos, len)?;
getreply(&mut self.c)?;
Ok(())
}
fn resize(&mut self, newsize: u64) -> Result<()> {
sendrequest(&mut self.c, NBD_CMD_RESIZE, newsize, 0)?;
getreply(&mut self.c)?;
self.size = newsize;
Ok(())
}
}
}
#[allow(dead_code)]
mod consts {
pub const NBD_OPT_EXPORT_NAME: u32 = 1;
pub const NBD_OPT_ABORT: u32 = 2;
pub const NBD_OPT_LIST: u32 = 3;
pub const NBD_OPT_STARTTLS: u32 = 5;
pub const NBD_OPT_INFO: u32 = 6;
pub const NBD_OPT_GO: u32 = 7;
pub const NBD_REP_ACK: u32 = 1;
pub const NBD_REP_SERVER: u32 = 2;
pub const NBD_REP_INFO: u32 = 3;
pub const NBD_REP_FLAG_ERROR: u32 = (1 << 31);
pub const NBD_REP_ERR_UNSUP: u32 = (1 | NBD_REP_FLAG_ERROR);
pub const NBD_REP_ERR_POLICY: u32 = (2 | NBD_REP_FLAG_ERROR);
pub const NBD_REP_ERR_INVALID: u32 = (3 | NBD_REP_FLAG_ERROR);
pub const NBD_REP_ERR_PLATFORM: u32 = (4 | NBD_REP_FLAG_ERROR);
pub const NBD_REP_ERR_TLS_REQD: u32 = (5 | NBD_REP_FLAG_ERROR);
pub const NBD_REP_ERR_UNKNOWN: u32 = (6 | NBD_REP_FLAG_ERROR);
pub const NBD_REP_ERR_BLOCK_SIZE_REQD: u32 = (8 | NBD_REP_FLAG_ERROR);
pub const NBD_FLAG_FIXED_NEWSTYLE: u16 = (1 << 0);
pub const NBD_FLAG_NO_ZEROES: u16 = (1 << 1);
pub const NBD_FLAG_C_FIXED_NEWSTYLE: u32 = NBD_FLAG_FIXED_NEWSTYLE as u32;
pub const NBD_FLAG_C_NO_ZEROES: u32 = NBD_FLAG_NO_ZEROES as u32;
pub const NBD_INFO_EXPORT: u16 = 0;
pub const NBD_INFO_NAME: u16 = 1;
pub const NBD_INFO_DESCRIPTION: u16 = 2;
pub const NBD_INFO_BLOCK_SIZE: u16 = 3;
pub const NBD_FLAG_HAS_FLAGS: u16 = (1 << 0);
pub const NBD_FLAG_READ_ONLY: u16 = (1 << 1);
pub const NBD_FLAG_SEND_FLUSH: u16 = (1 << 2);
pub const NBD_FLAG_SEND_FUA: u16 = (1 << 3);
pub const NBD_FLAG_ROTATIONAL: u16 = (1 << 4);
pub const NBD_FLAG_SEND_TRIM: u16 = (1 << 5);
pub const NBD_FLAG_SEND_WRITE_ZEROES: u16 = (1 << 6);
pub const NBD_FLAG_CAN_MULTI_CONN: u16 = (1 << 8);
pub const NBD_FLAG_SEND_RESIZE: u16 = (1 << 9);
pub const NBD_CMD_READ: u16 = 0;
pub const NBD_CMD_WRITE: u16 = 1;
pub const NBD_CMD_DISC: u16 = 2;
pub const NBD_CMD_FLUSH: u16 = 3;
pub const NBD_CMD_TRIM: u16 = 4;
pub const NBD_CMD_WRITE_ZEROES: u16 = 6;
pub const NBD_CMD_RESIZE: u16 = 8;
}
trait CheckedAddI64
where
Self: Sized,
{
fn checked_add_i64(self, rhs: i64) -> Option<Self>;
}
impl CheckedAddI64 for u64 {
fn checked_add_i64(self, rhs: i64) -> Option<Self> {
if rhs >= 0 {
self.checked_add(rhs as u64)
} else {
if let Some(x) = rhs.checked_neg() {
self.checked_sub(x as u64)
} else {
None
}
}
}
}
trait ClampToU32
where
Self: Sized,
{
fn clamp_to_u32(self) -> u32;
}
impl ClampToU32 for usize {
fn clamp_to_u32(self) -> u32 {
if self > u32::max_value() as usize {
u32::max_value()
} else {
self as u32
}
}
}
impl ClampToU32 for u64 {
fn clamp_to_u32(self) -> u32 {
if self > u32::max_value() as u64 {
u32::max_value()
} else {
self as u32
}
}
}