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
// Copyright 2020 John Millikin and the rust-fuse contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

use crate::internal::types::ProtocolVersion;
use crate::protocol::prelude::*;

#[cfg(test)]
mod fuse_init_test;

// FuseInitRequest {{{

/// Request type for [`FuseHandlers::fuse_init`].
///
/// [`FuseHandlers::fuse_init`]: ../../trait.FuseHandlers.html#method.fuse_init
pub struct FuseInitRequest<'a> {
	phantom: PhantomData<&'a ()>,
	version: ProtocolVersion,
	max_readahead: u32,
	flags: FuseInitFlags,
}

impl FuseInitRequest<'_> {
	pub fn version(&self) -> ProtocolVersion {
		self.version
	}

	pub fn max_readahead(&self) -> u32 {
		self.max_readahead
	}

	pub fn set_max_readahead(&mut self, max_readahead: u32) {
		self.max_readahead = max_readahead;
	}

	pub fn flags(&self) -> &FuseInitFlags {
		&self.flags
	}

	pub fn flags_mut(&mut self) -> &mut FuseInitFlags {
		&mut self.flags
	}
}

impl fmt::Debug for FuseInitRequest<'_> {
	fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
		fmt.debug_struct("FuseInitRequest")
			.field("version", &self.version)
			.field("max_readahead", &self.max_readahead)
			.field("flags", &self.flags)
			.finish()
	}
}

#[repr(C)]
struct fuse_init_in_v7p1 {
	major: u32,
	minor: u32,
}

impl<'a> fuse_io::DecodeRequest<'a> for FuseInitRequest<'_> {
	fn decode_request(
		mut dec: fuse_io::RequestDecoder<'a>,
	) -> Result<Self, Error> {
		debug_assert!(dec.header().opcode == fuse_kernel::FUSE_INIT);

		// There are two cases where we can't read past the version fields:
		//
		// * Very old protocol versions have a smaller init frame, containing
		//   only the (major, minor) version tuple. Trying to read the modern
		//   frame size would cause EOF.
		//
		// * Mismatch in the major version can't be handled at the request
		//   parsing layer. Per the version negotiation docs, a newer major
		//   version from the kernel should be rejected by sending a response
		//   containing the library's major version.
		let raw_v7p1: &'a fuse_init_in_v7p1 = dec.peek_sized()?;
		if raw_v7p1.minor < 6
			|| raw_v7p1.major != fuse_kernel::FUSE_KERNEL_VERSION
		{
			return Ok(FuseInitRequest {
				phantom: PhantomData,
				version: ProtocolVersion::new(raw_v7p1.major, raw_v7p1.minor),
				max_readahead: 0,
				flags: FuseInitFlags::from_bits(0),
			});
		}

		let raw: &'a fuse_kernel::fuse_init_in = dec.next_sized()?;
		Ok(FuseInitRequest {
			phantom: PhantomData,
			version: ProtocolVersion::new(raw.major, raw.minor),
			max_readahead: raw.max_readahead,
			flags: FuseInitFlags::from_bits(raw.flags),
		})
	}
}

// }}}

// FuseInitResponse {{{

/// Response type for [`FuseHandlers::fuse_init`].
///
/// [`FuseHandlers::fuse_init`]: ../../trait.FuseHandlers.html#method.fuse_init
pub struct FuseInitResponse {
	raw: fuse_kernel::fuse_init_out,
	flags: FuseInitFlags,
}

impl FuseInitResponse {
	pub fn new() -> FuseInitResponse {
		Self {
			raw: fuse_kernel::fuse_init_out {
				major: 0,
				minor: 0,
				max_readahead: 0,
				flags: 0,
				max_background: 0,
				congestion_threshold: 0,
				max_write: 0,
				time_gran: 0,
				unused: [0; 9],
			},
			flags: FuseInitFlags::new(),
		}
	}

	pub(crate) fn version(&self) -> ProtocolVersion {
		ProtocolVersion::new(self.raw.major, self.raw.minor)
	}

	pub(crate) fn set_version(&mut self, v: ProtocolVersion) {
		self.raw.major = v.major();
		self.raw.minor = v.minor();
	}

	pub fn flags(&self) -> &FuseInitFlags {
		&self.flags
	}

	pub fn flags_mut(&mut self) -> &mut FuseInitFlags {
		&mut self.flags
	}

	pub fn max_readahead(&self) -> u32 {
		self.raw.max_readahead
	}

	pub fn set_max_readahead(&mut self, max_readahead: u32) {
		self.raw.max_readahead = max_readahead;
	}

	pub fn max_background(&self) -> u16 {
		self.raw.max_background
	}

	pub fn set_max_background(&mut self, max_background: u16) {
		self.raw.max_background = max_background;
	}

	pub fn congestion_threshold(&self) -> u16 {
		self.raw.congestion_threshold
	}

	pub fn set_congestion_threshold(&mut self, congestion_threshold: u16) {
		self.raw.congestion_threshold = congestion_threshold;
	}

	pub fn max_write(&self) -> u32 {
		self.raw.max_write
	}

	pub fn set_max_write(&mut self, max_write: u32) {
		self.raw.max_write = max_write;
	}

	pub fn time_granularity(&self) -> u32 {
		self.raw.time_gran
	}

	pub fn set_time_granularity(&mut self, granularity: u32) {
		self.raw.time_gran = granularity;
	}
}

impl fmt::Debug for FuseInitResponse {
	fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
		fmt.debug_struct("FuseInitResponse")
			.field("max_readahead", &self.max_readahead())
			.field("flags", self.flags())
			.field("max_background", &self.max_background())
			.field("congestion_threshold", &self.congestion_threshold())
			.field("max_write", &self.max_write())
			.field("time_granularity", &self.time_granularity())
			.finish()
	}
}

#[repr(C)]
struct fuse_init_out_v7p1 {
	major: u32,
	minor: u32,
}

#[repr(C)]
struct fuse_init_out_v7p5 {
	major: u32,
	minor: u32,
	max_readahead: u32,        // v7.6
	flags: u32,                // v7.6
	max_background: u16,       // v7.6
	congestion_threshold: u16, // v7.6
	max_write: u32,
}

impl fuse_io::EncodeResponse for FuseInitResponse {
	fn encode_response<'a, Chan: fuse_io::Channel>(
		&'a self,
		enc: fuse_io::ResponseEncoder<Chan>,
	) -> Result<(), Chan::Error> {
		if self.raw.minor >= 23 {
			let mut out = self.raw;
			out.flags = self.flags.to_bits();
			return enc.encode_sized(&out);
		}

		if self.raw.minor >= 5 {
			return enc.encode_sized(&fuse_init_out_v7p5 {
				major: self.raw.major,
				minor: self.raw.minor,
				max_readahead: self.raw.max_readahead,
				flags: self.flags.to_bits(),
				max_background: self.raw.max_background,
				congestion_threshold: self.raw.congestion_threshold,
				max_write: self.raw.max_write,
			});
		}

		enc.encode_sized(&fuse_init_out_v7p1 {
			major: self.raw.major,
			minor: self.raw.minor,
		})
	}
}

// }}}

// FuseInitFlags {{{

bitflags_struct! {
	pub struct FuseInitFlags(u32);

	fuse_kernel::FUSE_ASYNC_READ: async_read,
	fuse_kernel::FUSE_POSIX_LOCKS: posix_locks,
	fuse_kernel::FUSE_FILE_OPS: file_ops,
	fuse_kernel::FUSE_ATOMIC_O_TRUNC: atomic_o_trunc,
	fuse_kernel::FUSE_EXPORT_SUPPORT: export_support,
	fuse_kernel::FUSE_BIG_WRITES: big_writes,
	fuse_kernel::FUSE_DONT_MASK: dont_mask,
	fuse_kernel::FUSE_SPLICE_WRITE: splice_write,
	fuse_kernel::FUSE_SPLICE_MOVE: splice_move,
	fuse_kernel::FUSE_SPLICE_READ: splice_read,
	fuse_kernel::FUSE_FLOCK_LOCKS: flock_locks,
	fuse_kernel::FUSE_HAS_IOCTL_DIR: has_ioctl_dir,
	fuse_kernel::FUSE_AUTO_INVAL_DATA: auto_inval_data,
	fuse_kernel::FUSE_DO_READDIRPLUS: do_readdirplus,
	fuse_kernel::FUSE_READDIRPLUS_AUTO: readdirplus_auto,
	fuse_kernel::FUSE_ASYNC_DIO: async_dio,
	fuse_kernel::FUSE_WRITEBACK_CACHE: writeback_cache,
	fuse_kernel::FUSE_NO_OPEN_SUPPORT: no_open_support,
	fuse_kernel::FUSE_PARALLEL_DIROPS: parallel_dirops,
	fuse_kernel::FUSE_HANDLE_KILLPRIV: handle_killpriv,
	fuse_kernel::FUSE_POSIX_ACL: posix_acl,
	fuse_kernel::FUSE_ABORT_ERROR: abort_error,
}

// }}}