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
use crate::protocol::prelude::*;
#[cfg(test)]
mod forget_test;
#[derive(Debug)]
pub struct ForgetRequestItem {
node_id: NodeId,
lookup_count: u64,
}
impl ForgetRequestItem {
pub fn node_id(&self) -> NodeId {
self.node_id
}
pub fn lookup_count(&self) -> u64 {
self.lookup_count
}
}
pub struct ForgetRequest<'a> {
forget: Option<fuse_kernel::fuse_forget_one>,
batch_forgets: &'a [fuse_kernel::fuse_forget_one],
}
impl<'a> ForgetRequest<'a> {
pub fn items(&self) -> impl Iterator<Item = ForgetRequestItem> + 'a {
self.items_impl()
}
fn items_impl(&self) -> ForgetRequestIter<'a> {
match self.forget {
Some(item) => ForgetRequestIter::One(Some(item)),
None => ForgetRequestIter::Batch(self.batch_forgets),
}
}
}
impl fmt::Debug for ForgetRequest<'_> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("ForgetRequest")
.field("items", &self.items_impl())
.finish()
}
}
impl<'a> fuse_io::DecodeRequest<'a> for ForgetRequest<'a> {
fn decode_request(
mut dec: fuse_io::RequestDecoder<'a>,
) -> Result<Self, Error> {
let header = dec.header();
if header.opcode == fuse_kernel::FUSE_BATCH_FORGET {
let raw: &'a fuse_kernel::fuse_batch_forget_in =
dec.next_sized()?;
let batch_size =
raw.count * size_of::<fuse_kernel::fuse_forget_one>() as u32;
let batch_bytes = dec.next_bytes(batch_size)?;
let batch_forgets: &'a [fuse_kernel::fuse_forget_one] = unsafe {
slice::from_raw_parts(
batch_bytes.as_ptr() as *const fuse_kernel::fuse_forget_one,
raw.count as usize,
)
};
return Ok(Self {
forget: None,
batch_forgets,
});
}
debug_assert!(header.opcode == fuse_kernel::FUSE_FORGET);
let raw: &fuse_kernel::fuse_forget_in = dec.next_sized()?;
Ok(Self {
forget: Some(fuse_kernel::fuse_forget_one {
nodeid: header.nodeid,
nlookup: raw.nlookup,
}),
batch_forgets: &[],
})
}
}
enum ForgetRequestIter<'a> {
One(Option<fuse_kernel::fuse_forget_one>),
Batch(&'a [fuse_kernel::fuse_forget_one]),
}
impl ForgetRequestIter<'_> {
fn clone(&self) -> Self {
match self {
Self::One(x) => Self::One(*x),
Self::Batch(x) => Self::Batch(x),
}
}
}
impl Iterator for ForgetRequestIter<'_> {
type Item = ForgetRequestItem;
fn next(&mut self) -> Option<Self::Item> {
match self {
Self::One(None) => return None,
Self::One(Some(item)) => {
let item = *item;
*self = Self::One(None);
match NodeId::new(item.nodeid) {
Some(node_id) => {
return Some(ForgetRequestItem {
node_id,
lookup_count: item.nlookup,
});
},
None => return None,
};
},
Self::Batch(items) => {
let (head, tail) = next_batch_item(items);
*self = Self::Batch(tail);
return head;
},
}
}
}
fn next_batch_item(
mut items: &[fuse_kernel::fuse_forget_one],
) -> (Option<ForgetRequestItem>, &[fuse_kernel::fuse_forget_one]) {
loop {
match items.split_first() {
None => return (None, &[]),
Some((head, tail)) => match NodeId::new(head.nodeid) {
None => {
items = tail;
},
Some(node_id) => {
let next = Some(ForgetRequestItem {
node_id,
lookup_count: head.nlookup,
});
return (next, tail);
},
},
}
}
}
impl fmt::Debug for ForgetRequestIter<'_> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_list().entries(self.clone()).finish()
}
}