rust: Implement can_cache
[nbdkit/ericb.git] / plugins / rust / src / lib.rs
blob25af2fe648dad5cb8ed9172ccdf91367da12b646
1 // nbdkit
2 // Copyright (C) 2019 Red Hat Inc.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
15 // * Neither the name of Red Hat nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without
17 // specific prior written permission.
19 // THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
20 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
23 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 // USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 // OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 // SUCH DAMAGE.
32 use std::mem;
33 use std::os::raw::{c_char, c_int, c_void};
35 // This struct describes the plugin ABI which your plugin_init()
36 // function must return.
37 #[repr(C)]
38 pub struct Plugin {
39     _struct_size: u64,
40     _api_version: c_int,
41     _thread_model: c_int,
43     pub name: *const c_char,
44     pub longname: *const c_char,
45     pub version: *const c_char,
46     pub description: *const c_char,
48     pub load: Option<extern fn ()>,
49     pub unload: Option<extern fn ()>,
51     pub config: Option<extern fn (*const c_char, *const c_char)>,
52     pub config_complete: Option<extern fn () -> c_int>,
53     pub config_help: *const c_char,
55     pub open: extern fn (c_int) -> *mut c_void,
56     pub close: Option<extern fn (*mut c_void)>,
58     pub get_size: extern fn (*mut c_void) -> i64,
60     pub can_write: Option<extern fn (*mut c_void) -> c_int>,
61     pub can_flush: Option<extern fn (*mut c_void) -> c_int>,
62     pub is_rotational: Option<extern fn (*mut c_void) -> c_int>,
63     pub can_trim: Option<extern fn (*mut c_void) -> c_int>,
65     // Slots for old v1 API functions.
66     _pread_old: Option<extern fn ()>,
67     _pwrite_old: Option<extern fn ()>,
68     _flush_old: Option<extern fn ()>,
69     _trim_old: Option<extern fn ()>,
70     _zero_old: Option<extern fn ()>,
72     errno_is_preserved: c_int,
74     pub dump_plugin: Option<extern fn ()>,
76     pub can_zero: Option<extern fn (*mut c_void) -> c_int>,
77     pub can_fua: Option<extern fn (*mut c_void) -> c_int>,
79     pub pread: extern fn (h: *mut c_void, buf: *mut c_char, count: u32,
80                           offset: u64,
81                           flags: u32) -> c_int,
82     pub pwrite: Option<extern fn (h: *mut c_void, buf: *const c_char,
83                                   count: u32, offset: u64,
84                                   flags: u32) -> c_int>,
85     pub flush: Option<extern fn (h: *mut c_void, flags: u32) -> c_int>,
86     pub trim: Option<extern fn (h: *mut c_void,
87                                 count: u32, offset: u64,
88                                 flags: u32) -> c_int>,
89     pub zero: Option<extern fn (h: *mut c_void,
90                                 count: u32, offset: u64,
91                                 flags: u32) -> c_int>,
93     pub magic_config_key: *const c_char,
95     pub can_multi_conn: Option<extern fn (h: *mut c_void) -> c_int>,
97     // Slots for extents functions, which needs more integration.
98     _can_extents: Option<extern fn ()>,
99     _extents: Option<extern fn ()>,
101     pub can_cache: Option<extern fn (h: *mut c_void) -> c_int>,
102     pub cache: Option<extern fn (h: *mut c_void,
103                                  count: u32, offset: u64,
104                                  flags: u32) -> c_int>,
107 pub enum ThreadModel {
108     SerializeConnections = 0,
109     SerializeAllRequests = 1,
110     SerializeRequests = 2,
111     Parallel = 3,
114 impl Plugin {
115     pub fn new (thread_model: ThreadModel,
116                 name: *const c_char,
117                 open: extern fn (c_int) -> *mut c_void,
118                 get_size: extern fn (*mut c_void) -> i64,
119                 pread: extern fn (h: *mut c_void, buf: *mut c_char,
120                                   count: u32, offset: u64,
121                                   flags: u32) -> c_int) -> Plugin {
122         Plugin {
123             _struct_size: mem::size_of::<Plugin>() as u64,
124             _api_version: 2,
125             _thread_model: thread_model as c_int,
126             name: name,
127             longname: std::ptr::null(),
128             version: std::ptr::null(),
129             description: std::ptr::null(),
130             load: None,
131             unload: None,
132             config: None,
133             config_complete: None,
134             config_help: std::ptr::null(),
135             open: open,
136             close: None,
137             get_size: get_size,
138             can_write: None,
139             can_flush: None,
140             is_rotational: None,
141             can_trim: None,
142             _pread_old: None,
143             _pwrite_old: None,
144             _flush_old: None,
145             _trim_old: None,
146             _zero_old: None,
147             errno_is_preserved: 0,
148             dump_plugin: None,
149             can_zero: None,
150             can_fua: None,
151             pread: pread,
152             pwrite: None,
153             flush: None,
154             trim: None,
155             zero: None,
156             magic_config_key: std::ptr::null(),
157             can_multi_conn: None,
158             _can_extents: None,
159             _extents: None,
160             can_cache: None,
161             cache: None,
162         }
163     }