@@ -16,6 +16,7 @@ use crate::{
16
16
from_kernel_result,
17
17
io_buffer:: { IoBufferReader , IoBufferWriter } ,
18
18
iov_iter:: IovIter ,
19
+ prelude:: * ,
19
20
sync:: CondVar ,
20
21
types:: PointerWrapper ,
21
22
user_ptr:: { UserSlicePtr , UserSlicePtrReader , UserSlicePtrWriter } ,
@@ -254,24 +255,24 @@ impl<A: FileOpenAdapter, T: FileOpener<A::Arg>> FileOperationsVtable<A, T> {
254
255
const VTABLE : bindings:: file_operations = bindings:: file_operations {
255
256
open : Some ( open_callback :: < A , T > ) ,
256
257
release : Some ( release_callback :: < T > ) ,
257
- read : if T :: TO_USE . read {
258
+ read : if T :: HAS_READ {
258
259
Some ( read_callback :: < T > )
259
260
} else {
260
261
None
261
262
} ,
262
- write : if T :: TO_USE . write {
263
+ write : if T :: HAS_WRITE {
263
264
Some ( write_callback :: < T > )
264
265
} else {
265
266
None
266
267
} ,
267
- llseek : if T :: TO_USE . seek {
268
+ llseek : if T :: HAS_SEEK {
268
269
Some ( llseek_callback :: < T > )
269
270
} else {
270
271
None
271
272
} ,
272
273
273
274
check_flags : None ,
274
- compat_ioctl : if T :: TO_USE . compat_ioctl {
275
+ compat_ioctl : if T :: HAS_COMPAT_IOCTL {
275
276
Some ( compat_ioctl_callback :: < T > )
276
277
} else {
277
278
None
@@ -282,7 +283,7 @@ impl<A: FileOpenAdapter, T: FileOpener<A::Arg>> FileOperationsVtable<A, T> {
282
283
fasync : None ,
283
284
flock : None ,
284
285
flush : None ,
285
- fsync : if T :: TO_USE . fsync {
286
+ fsync : if T :: HAS_FSYNC {
286
287
Some ( fsync_callback :: < T > )
287
288
} else {
288
289
None
@@ -292,19 +293,19 @@ impl<A: FileOpenAdapter, T: FileOpener<A::Arg>> FileOperationsVtable<A, T> {
292
293
iterate_shared : None ,
293
294
iopoll : None ,
294
295
lock : None ,
295
- mmap : if T :: TO_USE . mmap {
296
+ mmap : if T :: HAS_MMAP {
296
297
Some ( mmap_callback :: < T > )
297
298
} else {
298
299
None
299
300
} ,
300
301
mmap_supported_flags : 0 ,
301
302
owner : ptr:: null_mut ( ) ,
302
- poll : if T :: TO_USE . poll {
303
+ poll : if T :: HAS_POLL {
303
304
Some ( poll_callback :: < T > )
304
305
} else {
305
306
None
306
307
} ,
307
- read_iter : if T :: TO_USE . read_iter {
308
+ read_iter : if T :: HAS_READ {
308
309
Some ( read_iter_callback :: < T > )
309
310
} else {
310
311
None
@@ -315,12 +316,12 @@ impl<A: FileOpenAdapter, T: FileOpener<A::Arg>> FileOperationsVtable<A, T> {
315
316
show_fdinfo : None ,
316
317
splice_read : None ,
317
318
splice_write : None ,
318
- unlocked_ioctl : if T :: TO_USE . ioctl {
319
+ unlocked_ioctl : if T :: HAS_IOCTL {
319
320
Some ( unlocked_ioctl_callback :: < T > )
320
321
} else {
321
322
None
322
323
} ,
323
- write_iter : if T :: TO_USE . write_iter {
324
+ write_iter : if T :: HAS_WRITE {
324
325
Some ( write_iter_callback :: < T > )
325
326
} else {
326
327
None
@@ -337,69 +338,6 @@ impl<A: FileOpenAdapter, T: FileOpener<A::Arg>> FileOperationsVtable<A, T> {
337
338
}
338
339
}
339
340
340
- /// Represents which fields of [`struct file_operations`] should be populated with pointers.
341
- pub struct ToUse {
342
- /// The `read` field of [`struct file_operations`].
343
- pub read : bool ,
344
-
345
- /// The `read_iter` field of [`struct file_operations`].
346
- pub read_iter : bool ,
347
-
348
- /// The `write` field of [`struct file_operations`].
349
- pub write : bool ,
350
-
351
- /// The `write_iter` field of [`struct file_operations`].
352
- pub write_iter : bool ,
353
-
354
- /// The `llseek` field of [`struct file_operations`].
355
- pub seek : bool ,
356
-
357
- /// The `unlocked_ioctl` field of [`struct file_operations`].
358
- pub ioctl : bool ,
359
-
360
- /// The `compat_ioctl` field of [`struct file_operations`].
361
- pub compat_ioctl : bool ,
362
-
363
- /// The `fsync` field of [`struct file_operations`].
364
- pub fsync : bool ,
365
-
366
- /// The `mmap` field of [`struct file_operations`].
367
- pub mmap : bool ,
368
-
369
- /// The `poll` field of [`struct file_operations`].
370
- pub poll : bool ,
371
- }
372
-
373
- /// A constant version where all values are to set to `false`, that is, all supported fields will
374
- /// be set to null pointers.
375
- pub const USE_NONE : ToUse = ToUse {
376
- read : false ,
377
- read_iter : false ,
378
- write : false ,
379
- write_iter : false ,
380
- seek : false ,
381
- ioctl : false ,
382
- compat_ioctl : false ,
383
- fsync : false ,
384
- mmap : false ,
385
- poll : false ,
386
- } ;
387
-
388
- /// Defines the [`FileOperations::TO_USE`] field based on a list of fields to be populated.
389
- #[ macro_export]
390
- macro_rules! declare_file_operations {
391
- ( ) => {
392
- const TO_USE : $crate:: file_operations:: ToUse = $crate:: file_operations:: USE_NONE ;
393
- } ;
394
- ( $( $i: ident) ,+) => {
395
- const TO_USE : kernel:: file_operations:: ToUse =
396
- $crate:: file_operations:: ToUse {
397
- $( $i: true ) ,+ ,
398
- ..$crate:: file_operations:: USE_NONE
399
- } ;
400
- } ;
401
- }
402
-
403
341
/// Allows the handling of ioctls defined with the `_IO`, `_IOR`, `_IOW`, and `_IOWR` macros.
404
342
///
405
343
/// For each macro, there is a handler function that takes the appropriate types as arguments.
@@ -527,10 +465,8 @@ impl<T: FileOperations<Wrapper = Box<T>> + Default> FileOpener<()> for T {
527
465
/// File descriptors may be used from multiple threads/processes concurrently, so your type must be
528
466
/// [`Sync`]. It must also be [`Send`] because [`FileOperations::release`] will be called from the
529
467
/// thread that decrements that associated file's refcount to zero.
468
+ #[ vtable]
530
469
pub trait FileOperations : Send + Sync + Sized {
531
- /// The methods to use to populate [`struct file_operations`].
532
- const TO_USE : ToUse ;
533
-
534
470
/// The pointer type that will be used to hold ourselves.
535
471
type Wrapper : PointerWrapper = Box < Self > ;
536
472
0 commit comments