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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

// Allow the clippy error cast_lossless in this module.
// Otherwise, clippy will suggest that "as u64" be converted to "64::from".
// Unfortunately, the locations it suggests are all in macros, and u64
// does not implement From<usize>. It is preferable to use the macros
// uniformly for both usize and the other u* types.
// I don't think that casting from usize to u64 could be lossy, unless the
// code is running on a machine with 128 bit pointers, so this is not a
// pressing worry.
#![allow(cast_lossless)]

use consts::SECTOR_SIZE;

use std::borrow::Borrow;
use std::fmt;
use std::iter::Sum;
use std::ops::{Add, Deref, Div, Mul, Rem};

use serde;

use super::deviceinfo::{DM_NAME_LEN, DM_UUID_LEN};
use super::errors::ErrorKind;
use super::result::{DmError, DmResult};

/// a kernel defined block size constant for any DM meta device
/// a DM meta device may store cache device or thinpool device metadata
/// defined in drivers/md/persistent-data/dm-space-map-metadata.h as
/// DM_SM_METADATA_BLOCK_SIZE.
const META_BLOCK_SIZE: Sectors = Sectors(8);

/// The maximum size of a metadata device.
/// defined in drivers/md/persistent-data/dm-space-map-metadata.h as
/// DM_SM_METADATA_MAX_BLOCKS.
/// As far as I can tell, this is not a limit on the size of a designated
/// metadata device, but instead on the possible usage of that device.
#[allow(dead_code)]
const MAX_META_DEV_SIZE: MetaBlocks = MetaBlocks(255 * ((1 << 14) - 64));

// division by self
macro_rules! self_div {
    ($T:ident) => {
        impl Div<$T> for $T {
            type Output = u64;
            fn div(self, rhs: $T) -> u64 {
                self.0 / *rhs
            }
        }
    };
}

// implement Debug in a more compact style, e.g. "Sectors(0)"
macro_rules! debug {
    ($T:ident) => {
        impl fmt::Debug for $T {
            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                write!(f, stringify!($T))?;
                write!(f, "({})", **self)
            }
        }
    };
}

// macros for implementing serialize and deserialize on all types
macro_rules! serde {
    ($T:ident) => {
        impl serde::Serialize for $T {
            fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
            where
                S: serde::Serializer,
            {
                serializer.serialize_u64(**self)
            }
        }

        impl<'de> serde::Deserialize<'de> for $T {
            fn deserialize<D>(deserializer: D) -> Result<$T, D::Error>
            where
                D: serde::de::Deserializer<'de>,
            {
                Ok($T(serde::Deserialize::deserialize(deserializer)?))
            }
        }
    };
}

// macros for implementing Sum on all types
macro_rules! sum {
    ($T:ident) => {
        impl Sum for $T {
            fn sum<I: Iterator<Item = $T>>(iter: I) -> $T {
                iter.fold($T::default(), Add::add)
            }
        }
    };
}

// macros for unsigned operations on Sectors and Bytes
macro_rules! unsigned_div {
    ($t:ty, $T:ident) => {
        impl Div<$t> for $T {
            type Output = $T;
            fn div(self, rhs: $t) -> $T {
                $T(self.0 / rhs as u64)
            }
        }
    };
}

macro_rules! unsigned_mul {
    ($t:ty, $T:ident) => {
        impl Mul<$t> for $T {
            type Output = $T;
            fn mul(self, rhs: $t) -> $T {
                $T(self.0 * rhs as u64)
            }
        }

        impl Mul<$T> for $t {
            type Output = $T;
            fn mul(self, rhs: $T) -> $T {
                $T(self as u64 * rhs.0)
            }
        }
    };
}

macro_rules! unsigned_rem {
    ($t:ty, $T:ident) => {
        impl Rem<$t> for $T {
            type Output = $T;
            fn rem(self, rhs: $t) -> $T {
                $T(self.0 % rhs as u64)
            }
        }
    };
}

macro_rules! rem {
    ($T:ident) => {
        impl Rem<$T> for $T {
            type Output = $T;
            fn rem(self, rhs: $T) -> $T {
                $T(self.0 % rhs.0 as u64)
            }
        }
    };
}

macro_rules! checked_add {
    ($T: ident) => {
        /// Add two items of type $T, return None if overflow.
        pub fn checked_add(&self, other: $T) -> Option<$T> {
            self.0.checked_add(other.0).map($T)
        }
    }
}

macro_attr! {
    #[derive(Clone, Copy, Default, Eq, Ord, PartialEq, PartialOrd)]
    /// A type for Data Blocks as used by the thin pool.
    pub struct DataBlocks(pub u64);
}

NewtypeAdd! { () pub struct DataBlocks(u64); }
NewtypeAddAssign! { () pub struct DataBlocks(u64); }
NewtypeDeref! { () pub struct DataBlocks(u64); }
NewtypeFrom! { () pub struct DataBlocks(u64); }
NewtypeSub! { () pub struct DataBlocks(u64); }
NewtypeSubAssign! { () pub struct DataBlocks(u64); }

self_div!(DataBlocks);
serde!(DataBlocks);
debug!(DataBlocks);

unsigned_div!(u64, DataBlocks);
unsigned_div!(u32, DataBlocks);
unsigned_div!(u16, DataBlocks);
unsigned_div!(u8, DataBlocks);
unsigned_div!(usize, DataBlocks);

unsigned_mul!(u64, DataBlocks);
unsigned_mul!(u32, DataBlocks);
unsigned_mul!(u16, DataBlocks);
unsigned_mul!(u8, DataBlocks);
unsigned_mul!(usize, DataBlocks);

impl fmt::Display for DataBlocks {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{} data blocks", self.0)
    }
}

macro_attr! {
    #[derive(Clone, Copy, Default, Eq, Ord, PartialEq, PartialOrd)]
    /// A type for Meta Data blocks as used by the thin pool.
    /// MetaBlocks have a kernel defined constant size of META_BLOCK_SIZE
    pub struct MetaBlocks(pub u64);
}

impl MetaBlocks {
    /// Return the number of Sectors in the MetaBlocks.
    pub fn sectors(self) -> Sectors {
        self.0 * META_BLOCK_SIZE
    }
}

NewtypeAdd! { () pub struct MetaBlocks(u64); }
NewtypeAddAssign! { () pub struct MetaBlocks(u64); }
NewtypeDeref! { () pub struct MetaBlocks(u64); }
NewtypeFrom! { () pub struct MetaBlocks(u64); }
NewtypeSub! { () pub struct MetaBlocks(u64); }
NewtypeSubAssign! { () pub struct MetaBlocks(u64); }

self_div!(MetaBlocks);
serde!(MetaBlocks);
debug!(MetaBlocks);

unsigned_div!(u64, MetaBlocks);
unsigned_div!(u32, MetaBlocks);
unsigned_div!(u16, MetaBlocks);
unsigned_div!(u8, MetaBlocks);
unsigned_div!(usize, MetaBlocks);

unsigned_mul!(u64, MetaBlocks);
unsigned_mul!(u32, MetaBlocks);
unsigned_mul!(u16, MetaBlocks);
unsigned_mul!(u8, MetaBlocks);
unsigned_mul!(usize, MetaBlocks);

impl fmt::Display for MetaBlocks {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{} meta blocks", self.0)
    }
}

macro_attr! {
    #[derive(Clone, Copy, Default, Eq, Ord, PartialEq, PartialOrd)]
    /// Structure to represent bytes
    pub struct Bytes(pub u64);
}

impl Bytes {
    /// Return the number of Sectors fully contained in these bytes.
    pub fn sectors(self) -> Sectors {
        Sectors(self.0 / SECTOR_SIZE as u64)
    }

    checked_add!(Bytes);
}

NewtypeAdd! { () pub struct Bytes(u64); }
NewtypeAddAssign! { () pub struct Bytes(u64); }
NewtypeDeref! { () pub struct Bytes(u64); }
NewtypeFrom! { () pub struct Bytes(u64); }
NewtypeSub! { () pub struct Bytes(u64); }
NewtypeSubAssign! { () pub struct Bytes(u64); }

self_div!(Bytes);
serde!(Bytes);
debug!(Bytes);
sum!(Bytes);

unsigned_div!(u64, Bytes);
unsigned_div!(u32, Bytes);
unsigned_div!(u16, Bytes);
unsigned_div!(u8, Bytes);
unsigned_div!(usize, Bytes);

unsigned_mul!(u64, Bytes);
unsigned_mul!(u32, Bytes);
unsigned_mul!(u16, Bytes);
unsigned_mul!(u8, Bytes);
unsigned_mul!(usize, Bytes);

impl fmt::Display for Bytes {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{} bytes", self.0)
    }
}

macro_attr! {
    #[derive(Clone, Copy, Default, Eq, Ord, PartialEq, PartialOrd)]
    /// A separate type to store counts and offsets expressed in
    /// 512-byte sectors.
    pub struct Sectors(pub u64);
}

impl Sectors {
    /// The number of bytes in these sectors.
    pub fn bytes(self) -> Bytes {
        Bytes(self.0 * SECTOR_SIZE as u64)
    }

    /// The number of whole metablocks contained in these sectors.
    pub fn metablocks(self) -> MetaBlocks {
        MetaBlocks(self / META_BLOCK_SIZE)
    }

    checked_add!(Sectors);
}

NewtypeAdd! { () pub struct Sectors(u64); }
NewtypeAddAssign! { () pub struct Sectors(u64); }
NewtypeDeref! { () pub struct Sectors(u64); }
NewtypeFrom! { () pub struct Sectors(u64); }
NewtypeSub! { () pub struct Sectors(u64); }
NewtypeSubAssign! { () pub struct Sectors(u64); }

self_div!(Sectors);
serde!(Sectors);
debug!(Sectors);
sum!(Sectors);

unsigned_div!(u64, Sectors);
unsigned_div!(u32, Sectors);
unsigned_div!(u16, Sectors);
unsigned_div!(u8, Sectors);
unsigned_div!(usize, Sectors);

unsigned_mul!(u64, Sectors);
unsigned_mul!(u32, Sectors);
unsigned_mul!(u16, Sectors);
unsigned_mul!(u8, Sectors);
unsigned_mul!(usize, Sectors);

unsigned_rem!(u64, Sectors);
unsigned_rem!(u32, Sectors);
unsigned_rem!(u16, Sectors);
unsigned_rem!(u8, Sectors);
unsigned_rem!(usize, Sectors);
rem!(Sectors);

impl fmt::Display for Sectors {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{} sectors", self.0)
    }
}

/// Returns an error if value is unsuitable.
fn str_check(value: &str, max_allowed_chars: usize) -> DmResult<()> {
    if !value.is_ascii() {
        let err_msg = format!("value {} has some non-ascii characters", value);
        return Err(DmError::Core(ErrorKind::InvalidArgument(err_msg).into()));
    }
    let num_chars = value.len();
    if num_chars == 0 {
        return Err(DmError::Core(
            ErrorKind::InvalidArgument("value has zero characters".into()).into(),
        ));
    }
    if num_chars > max_allowed_chars {
        let err_msg = format!(
            "value {} has {} chars which is greater than maximum allowed {}",
            value, num_chars, max_allowed_chars
        );
        return Err(DmError::Core(ErrorKind::InvalidArgument(err_msg).into()));
    }
    Ok(())
}

/// Define borrowed and owned versions of string types that guarantee
/// conformance to DM restrictions, such as maximum length.
// This implementation follows the example of Path/PathBuf as closely as
// possible.
macro_rules! str_id {
    ($B:ident, $O:ident, $MAX:ident, $check:ident) => {
        /// The borrowed version of the DM identifier.
        #[derive(Debug, PartialEq, Eq, Hash)]
        pub struct $B {
            inner: str,
        }

        /// The owned version of the DM identifier.
        #[derive(Debug, Clone, PartialEq, Eq, Hash)]
        pub struct $O {
            inner: String,
        }

        impl $B {
            /// Create a new borrowed identifier from a `&str`.
            pub fn new(value: &str) -> DmResult<&$B> {
                $check(value, $MAX - 1)?;
                Ok(unsafe { &*(value as *const str as *const $B) })
            }

            /// Get the inner value as bytes
            pub fn as_bytes(&self) -> &[u8] {
                self.inner.as_bytes()
            }
        }

        impl ToOwned for $B {
            type Owned = $O;
            fn to_owned(&self) -> $O {
                $O {
                    inner: self.inner.to_owned(),
                }
            }
        }

        impl fmt::Display for $B {
            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                write!(f, "{}", &self.inner)
            }
        }

        impl $O {
            /// Construct a new owned identifier.
            pub fn new(value: String) -> DmResult<$O> {
                $check(&value, $MAX - 1)?;
                Ok($O { inner: value })
            }
        }

        impl AsRef<$B> for $O {
            fn as_ref(&self) -> &$B {
                self
            }
        }

        impl Borrow<$B> for $O {
            fn borrow(&self) -> &$B {
                self.deref()
            }
        }

        impl Deref for $O {
            type Target = $B;
            fn deref(&self) -> &$B {
                $B::new(&self.inner).expect("inner satisfies all correctness criteria for $B::new")
            }
        }
    };
}

/// A devicemapper name. Really just a string, but also the argument type of
/// DevId::Name. Used in function arguments to indicate that the function
/// takes only a name, not a devicemapper uuid.
str_id!(DmName, DmNameBuf, DM_NAME_LEN, str_check);

/// A devicemapper uuid. A devicemapper uuid has a devicemapper-specific
/// format.
str_id!(DmUuid, DmUuidBuf, DM_UUID_LEN, str_check);

/// Used as a parameter for functions that take either a Device name
/// or a Device UUID.
#[derive(Debug, PartialEq, Eq)]
pub enum DevId<'a> {
    /// The parameter is the device's name
    Name(&'a DmName),
    /// The parameter is the device's devicemapper uuid
    Uuid(&'a DmUuid),
}

impl<'a> fmt::Display for DevId<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            DevId::Name(name) => write!(f, "{}", name),
            DevId::Uuid(uuid) => write!(f, "{}", uuid),
        }
    }
}

/// Number of bytes in Struct_dm_target_spec::target_type field.
const DM_TARGET_TYPE_LEN: usize = 16;

str_id!(TargetType, TargetTypeBuf, DM_TARGET_TYPE_LEN, str_check);

#[cfg(test)]
mod tests {
    use super::super::errors::Error;

    use super::*;

    #[test]
    /// Verify that Sectors can be multiplied by a usize.
    /// The real test is that this tests compiles at all.
    fn test_usize() {
        assert_eq!(Sectors(0) * 32usize, Sectors(0));
    }

    #[test]
    /// Verify that creating an empty DmName is an error.
    pub fn test_empty_name() {
        assert!(match DmName::new("") {
            Err(DmError::Core(Error(ErrorKind::InvalidArgument(_), _))) => true,
            _ => false,
        })
    }

    #[test]
    /// Test the return types of various % operations
    pub fn test_sectors_remainder() {
        assert_eq!(Sectors(13) % Sectors(11), Sectors(2));
        assert_eq!(Sectors(13) % 11usize, Sectors(2));
    }
}