Skip to content
This repository was archived by the owner on Jul 25, 2024. It is now read-only.

Commit 6c1ad4a

Browse files
author
Joe Grund
authored
Use zpool_search_import instead of zpool_find_import (#85)
* Use zpool_search_import instead of zpool_find_import Fixes #84. Signed-off-by: Joe Grund <jgrund@whamcloud.io> * initalize importargs Signed-off-by: Joe Grund <jgrund@whamcloud.io> * update bitfield args Signed-off-by: Joe Grund <jgrund@whamcloud.io>
1 parent 0b05a07 commit 6c1ad4a

8 files changed

Lines changed: 417 additions & 174 deletions

File tree

Cargo.lock

Lines changed: 150 additions & 157 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libzfs-sys/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "libzfs-sys"
3-
version = "0.5.10"
3+
version = "0.5.11"
44
description = "Rust bindings to libzfs"
55
license = "MIT"
66
repository = "https://github.com/whamcloud/rust-libzfs"
@@ -12,5 +12,5 @@ build = "build.rs"
1212
nvpair-sys = "0.1"
1313

1414
[build-dependencies]
15-
bindgen = "0.43.1"
16-
pkg-config = "0.3.14"
15+
bindgen = "0.51.0"
16+
pkg-config = "0.3.15"

libzfs-sys/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ fn main() {
7878
.whitelist_function("thread_fini")
7979
.whitelist_function("zpool_import")
8080
.whitelist_function("zpool_export")
81-
.whitelist_function("zpool_find_import")
81+
.whitelist_function("zpool_search_import")
8282
.whitelist_function("zpool_iter")
8383
.whitelist_function("zpool_open_canfail")
8484
.whitelist_function("zpool_close")

libzfs-sys/src/bindings.rs

Lines changed: 232 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,86 @@
11
/* automatically generated by rust-bindgen */
22

3+
#[repr(C)]
4+
#[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
5+
pub struct __BindgenBitfieldUnit<Storage, Align> {
6+
storage: Storage,
7+
align: [Align; 0],
8+
}
9+
impl<Storage, Align> __BindgenBitfieldUnit<Storage, Align> {
10+
#[inline]
11+
pub const fn new(storage: Storage) -> Self {
12+
Self { storage, align: [] }
13+
}
14+
}
15+
impl<Storage, Align> __BindgenBitfieldUnit<Storage, Align>
16+
where
17+
Storage: AsRef<[u8]> + AsMut<[u8]>,
18+
{
19+
#[inline]
20+
pub fn get_bit(&self, index: usize) -> bool {
21+
debug_assert!(index / 8 < self.storage.as_ref().len());
22+
let byte_index = index / 8;
23+
let byte = self.storage.as_ref()[byte_index];
24+
let bit_index = if cfg!(target_endian = "big") {
25+
7 - (index % 8)
26+
} else {
27+
index % 8
28+
};
29+
let mask = 1 << bit_index;
30+
byte & mask == mask
31+
}
32+
#[inline]
33+
pub fn set_bit(&mut self, index: usize, val: bool) {
34+
debug_assert!(index / 8 < self.storage.as_ref().len());
35+
let byte_index = index / 8;
36+
let byte = &mut self.storage.as_mut()[byte_index];
37+
let bit_index = if cfg!(target_endian = "big") {
38+
7 - (index % 8)
39+
} else {
40+
index % 8
41+
};
42+
let mask = 1 << bit_index;
43+
if val {
44+
*byte |= mask;
45+
} else {
46+
*byte &= !mask;
47+
}
48+
}
49+
#[inline]
50+
pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 {
51+
debug_assert!(bit_width <= 64);
52+
debug_assert!(bit_offset / 8 < self.storage.as_ref().len());
53+
debug_assert!((bit_offset + (bit_width as usize)) / 8 <= self.storage.as_ref().len());
54+
let mut val = 0;
55+
for i in 0..(bit_width as usize) {
56+
if self.get_bit(i + bit_offset) {
57+
let index = if cfg!(target_endian = "big") {
58+
bit_width as usize - 1 - i
59+
} else {
60+
i
61+
};
62+
val |= 1 << index;
63+
}
64+
}
65+
val
66+
}
67+
#[inline]
68+
pub fn set(&mut self, bit_offset: usize, bit_width: u8, val: u64) {
69+
debug_assert!(bit_width <= 64);
70+
debug_assert!(bit_offset / 8 < self.storage.as_ref().len());
71+
debug_assert!((bit_offset + (bit_width as usize)) / 8 <= self.storage.as_ref().len());
72+
for i in 0..(bit_width as usize) {
73+
let mask = 1 << i;
74+
let val_bit_is_set = val & mask == mask;
75+
let index = if cfg!(target_endian = "big") {
76+
bit_width as usize - 1 - i
77+
} else {
78+
i
79+
};
80+
self.set_bit(index + bit_offset, val_bit_is_set);
81+
}
82+
}
83+
}
384
pub const ZFS_MAX_DATASET_NAME_LEN: u32 = 256;
485
pub const ZPROP_VALUE: &'static [u8; 6usize] = b"value\0";
586
pub const ZPOOL_CONFIG_POOL_NAME: &'static [u8; 5usize] = b"name\0";
@@ -81,7 +162,7 @@ impl ::std::ops::BitAndAssign for zfs_type_t {
81162
self.0 &= rhs.0;
82163
}
83164
}
84-
#[repr(C)]
165+
#[repr(transparent)]
85166
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
86167
pub struct zfs_type_t(pub u32);
87168
pub const dmu_objset_type_DMU_OST_NONE: dmu_objset_type = 0;
@@ -661,11 +742,158 @@ extern "C" {
661742
altroot: *mut ::std::os::raw::c_char,
662743
) -> ::std::os::raw::c_int;
663744
}
745+
#[repr(C)]
746+
#[derive(Debug, Copy, Clone)]
747+
pub struct importargs {
748+
pub path: *mut *mut ::std::os::raw::c_char,
749+
pub paths: ::std::os::raw::c_int,
750+
pub poolname: *mut ::std::os::raw::c_char,
751+
pub guid: u64,
752+
pub cachefile: *mut ::std::os::raw::c_char,
753+
pub _bitfield_1: __BindgenBitfieldUnit<[u8; 1usize], u8>,
754+
pub __bindgen_padding_0: [u8; 7usize],
755+
}
756+
#[test]
757+
fn bindgen_test_layout_importargs() {
758+
assert_eq!(
759+
::std::mem::size_of::<importargs>(),
760+
48usize,
761+
concat!("Size of: ", stringify!(importargs))
762+
);
763+
assert_eq!(
764+
::std::mem::align_of::<importargs>(),
765+
8usize,
766+
concat!("Alignment of ", stringify!(importargs))
767+
);
768+
assert_eq!(
769+
unsafe { &(*(::std::ptr::null::<importargs>())).path as *const _ as usize },
770+
0usize,
771+
concat!(
772+
"Offset of field: ",
773+
stringify!(importargs),
774+
"::",
775+
stringify!(path)
776+
)
777+
);
778+
assert_eq!(
779+
unsafe { &(*(::std::ptr::null::<importargs>())).paths as *const _ as usize },
780+
8usize,
781+
concat!(
782+
"Offset of field: ",
783+
stringify!(importargs),
784+
"::",
785+
stringify!(paths)
786+
)
787+
);
788+
assert_eq!(
789+
unsafe { &(*(::std::ptr::null::<importargs>())).poolname as *const _ as usize },
790+
16usize,
791+
concat!(
792+
"Offset of field: ",
793+
stringify!(importargs),
794+
"::",
795+
stringify!(poolname)
796+
)
797+
);
798+
assert_eq!(
799+
unsafe { &(*(::std::ptr::null::<importargs>())).guid as *const _ as usize },
800+
24usize,
801+
concat!(
802+
"Offset of field: ",
803+
stringify!(importargs),
804+
"::",
805+
stringify!(guid)
806+
)
807+
);
808+
assert_eq!(
809+
unsafe { &(*(::std::ptr::null::<importargs>())).cachefile as *const _ as usize },
810+
32usize,
811+
concat!(
812+
"Offset of field: ",
813+
stringify!(importargs),
814+
"::",
815+
stringify!(cachefile)
816+
)
817+
);
818+
}
819+
impl importargs {
820+
#[inline]
821+
pub fn can_be_active(&self) -> ::std::os::raw::c_int {
822+
unsafe { ::std::mem::transmute(self._bitfield_1.get(0usize, 1u8) as u32) }
823+
}
824+
#[inline]
825+
pub fn set_can_be_active(&mut self, val: ::std::os::raw::c_int) {
826+
unsafe {
827+
let val: u32 = ::std::mem::transmute(val);
828+
self._bitfield_1.set(0usize, 1u8, val as u64)
829+
}
830+
}
831+
#[inline]
832+
pub fn unique(&self) -> ::std::os::raw::c_int {
833+
unsafe { ::std::mem::transmute(self._bitfield_1.get(1usize, 1u8) as u32) }
834+
}
835+
#[inline]
836+
pub fn set_unique(&mut self, val: ::std::os::raw::c_int) {
837+
unsafe {
838+
let val: u32 = ::std::mem::transmute(val);
839+
self._bitfield_1.set(1usize, 1u8, val as u64)
840+
}
841+
}
842+
#[inline]
843+
pub fn exists(&self) -> ::std::os::raw::c_int {
844+
unsafe { ::std::mem::transmute(self._bitfield_1.get(2usize, 1u8) as u32) }
845+
}
846+
#[inline]
847+
pub fn set_exists(&mut self, val: ::std::os::raw::c_int) {
848+
unsafe {
849+
let val: u32 = ::std::mem::transmute(val);
850+
self._bitfield_1.set(2usize, 1u8, val as u64)
851+
}
852+
}
853+
#[inline]
854+
pub fn scan(&self) -> ::std::os::raw::c_int {
855+
unsafe { ::std::mem::transmute(self._bitfield_1.get(3usize, 1u8) as u32) }
856+
}
857+
#[inline]
858+
pub fn set_scan(&mut self, val: ::std::os::raw::c_int) {
859+
unsafe {
860+
let val: u32 = ::std::mem::transmute(val);
861+
self._bitfield_1.set(3usize, 1u8, val as u64)
862+
}
863+
}
864+
#[inline]
865+
pub fn new_bitfield_1(
866+
can_be_active: ::std::os::raw::c_int,
867+
unique: ::std::os::raw::c_int,
868+
exists: ::std::os::raw::c_int,
869+
scan: ::std::os::raw::c_int,
870+
) -> __BindgenBitfieldUnit<[u8; 1usize], u8> {
871+
let mut __bindgen_bitfield_unit: __BindgenBitfieldUnit<[u8; 1usize], u8> =
872+
Default::default();
873+
__bindgen_bitfield_unit.set(0usize, 1u8, {
874+
let can_be_active: u32 = unsafe { ::std::mem::transmute(can_be_active) };
875+
can_be_active as u64
876+
});
877+
__bindgen_bitfield_unit.set(1usize, 1u8, {
878+
let unique: u32 = unsafe { ::std::mem::transmute(unique) };
879+
unique as u64
880+
});
881+
__bindgen_bitfield_unit.set(2usize, 1u8, {
882+
let exists: u32 = unsafe { ::std::mem::transmute(exists) };
883+
exists as u64
884+
});
885+
__bindgen_bitfield_unit.set(3usize, 1u8, {
886+
let scan: u32 = unsafe { ::std::mem::transmute(scan) };
887+
scan as u64
888+
});
889+
__bindgen_bitfield_unit
890+
}
891+
}
892+
pub type importargs_t = importargs;
664893
extern "C" {
665-
pub fn zpool_find_import(
894+
pub fn zpool_search_import(
666895
arg1: *mut libzfs_handle_t,
667-
arg2: ::std::os::raw::c_int,
668-
arg3: *mut *mut ::std::os::raw::c_char,
896+
arg2: *mut importargs_t,
669897
) -> *mut nvlist_t;
670898
}
671899
extern "C" {

libzfs-sys/src/lib.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
2727
extern crate nvpair_sys;
2828
use nvpair_sys::*;
29+
2930
include!("bindings.rs");
3031

3132
fn utf8_to_string(bytes: &[u8]) -> String {
@@ -96,6 +97,18 @@ pub fn zfs_type_dataset() -> zfs_type_t {
9697
zfs_type_t::ZFS_TYPE_FILESYSTEM | zfs_type_t::ZFS_TYPE_VOLUME | zfs_type_t::ZFS_TYPE_SNAPSHOT
9798
}
9899

100+
pub fn import_args() -> importargs {
101+
importargs {
102+
path: std::ptr::null_mut(),
103+
paths: 0,
104+
poolname: std::ptr::null_mut(),
105+
guid: 0,
106+
cachefile: std::ptr::null_mut(),
107+
_bitfield_1: importargs::new_bitfield_1(0, 1, 1, 0),
108+
__bindgen_padding_0: [0, 0, 0, 0, 0, 0, 0],
109+
}
110+
}
111+
99112
/// Converts a `Vec<u64>` to `vdev_stat_t`
100113
pub fn to_vdev_stat(mut xs: Vec<u64>) -> vdev_stat_t {
101114
xs.shrink_to_fit();
@@ -203,7 +216,10 @@ mod tests {
203216

204217
let (nvl, nvp) = unsafe {
205218
thread_init();
206-
let nvl = zpool_find_import(h, 0, ptr::null_mut());
219+
220+
let mut args = import_args();
221+
222+
let nvl = zpool_search_import(h, &mut args as *mut importargs);
207223
thread_fini();
208224

209225
let nvp = nvlist_next_nvpair(nvl, ptr::null_mut());

libzfs/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
[package]
22
name = "libzfs"
3-
version = "0.6.14"
3+
version = "0.6.15"
44
authors = ["IML Team <iml@whamcloud.com>"]
55
description = "Rust wrapper around libzfs-sys"
66
license = "MIT"
77

88
[dependencies]
9-
libzfs-sys = { path = "../libzfs-sys", version = "0.5.10"}
9+
libzfs-sys = { path = "../libzfs-sys", version = "0.5.11"}
1010
libzfs-types = { path = "../libzfs-types", version = "0.1.1" }
1111
nvpair-sys = "0.1"
1212
serde = "1.0"
1313
serde_derive = "1.0"
1414
foreign-types = "0.3"
1515
cstr-argument = "0.1"
16-
lazy_static = "1.1"
16+
lazy_static = "1.3"

libzfs/src/libzfs.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ impl Libzfs {
6767
let _l = LOCK.lock().unwrap();
6868
unsafe {
6969
sys::thread_init();
70-
let x = sys::zpool_find_import(self.raw, 0, ptr::null_mut());
70+
let mut args = sys::import_args();
71+
72+
let x = sys::zpool_search_import(self.raw, &mut args as *mut sys::importargs);
7173
sys::thread_fini();
7274

7375
nvpair::NvList::from_ptr(x)
@@ -91,7 +93,8 @@ impl Libzfs {
9193
0 => Ok(()),
9294
x => Err(LibZfsError::Io(Error::from_raw_os_error(x))),
9395
}
94-
}).collect()
96+
})
97+
.collect()
9598
}
9699
pub fn export_all(&mut self, pools: &[Zpool]) -> Result<Vec<()>> {
97100
pools

libzfs/src/zfs.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ impl Zfs {
7070
.map(|v| ZProp {
7171
name: x.user_prop().to_owned().into_string().unwrap(),
7272
value: v.into_string().unwrap(),
73-
}).ok(),
73+
})
74+
.ok(),
7475
y => {
7576
let raw = CString::new("0".repeat(buff_size)).unwrap().into_raw();
7677

@@ -100,7 +101,8 @@ impl Zfs {
100101
None
101102
}
102103
}
103-
}).collect::<Vec<_>>();
104+
})
105+
.collect::<Vec<_>>();
104106

105107
Ok(xs)
106108
}
@@ -171,7 +173,8 @@ mod tests {
171173
"guid".to_owned(),
172174
"createtxg".to_owned()
173175
]
174-
.contains(&x.name)).collect::<Vec<ZProp>>(),
176+
.contains(&x.name))
177+
.collect::<Vec<ZProp>>(),
175178
vec![
176179
ZProp {
177180
name: "name".to_owned(),

0 commit comments

Comments
 (0)