Skip to content

Commit 8d88d50

Browse files
committed
rust: use #[vtable] for kernel::file::Operations
Signed-off-by: Gary Guo <[email protected]>
1 parent 8b87872 commit 8d88d50

File tree

8 files changed

+21
-92
lines changed

8 files changed

+21
-92
lines changed

drivers/android/process.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -805,12 +805,11 @@ impl IoctlHandler for Process {
805805
}
806806
}
807807

808+
#[vtable]
808809
impl file::Operations for Process {
809810
type Data = Ref<Self>;
810811
type OpenData = Ref<Context>;
811812

812-
kernel::declare_file_operations!(ioctl, compat_ioctl, mmap, poll);
813-
814813
fn open(ctx: &Ref<Context>, file: &File) -> Result<Self::Data> {
815814
Self::new(ctx.clone(), file.cred().into())
816815
}

drivers/char/hw_random/bcm2835_rng_rust.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ module_platform_driver! {
1717

1818
struct RngDevice;
1919

20+
#[vtable]
2021
impl file::Operations for RngDevice {
21-
kernel::declare_file_operations!(read);
22-
2322
fn open(_open_data: &(), _file: &File) -> Result {
2423
Ok(())
2524
}

rust/kernel/file.rs

Lines changed: 12 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//! C headers: [`include/linux/fs.h`](../../../../include/linux/fs.h) and
66
//! [`include/linux/file.h`](../../../../include/linux/file.h)
77
8+
use crate::prelude::*;
89
use crate::{
910
bindings,
1011
cred::Credential,
@@ -468,24 +469,24 @@ impl<A: OpenAdapter<T::OpenData>, T: Operations> OperationsVtable<A, T> {
468469
const VTABLE: bindings::file_operations = bindings::file_operations {
469470
open: Some(Self::open_callback),
470471
release: Some(Self::release_callback),
471-
read: if T::TO_USE.read {
472+
read: if T::HAS_READ {
472473
Some(Self::read_callback)
473474
} else {
474475
None
475476
},
476-
write: if T::TO_USE.write {
477+
write: if T::HAS_WRITE {
477478
Some(Self::write_callback)
478479
} else {
479480
None
480481
},
481-
llseek: if T::TO_USE.seek {
482+
llseek: if T::HAS_SEEK {
482483
Some(Self::llseek_callback)
483484
} else {
484485
None
485486
},
486487

487488
check_flags: None,
488-
compat_ioctl: if T::TO_USE.compat_ioctl {
489+
compat_ioctl: if T::HAS_COMPAT_IOCTL {
489490
Some(Self::compat_ioctl_callback)
490491
} else {
491492
None
@@ -496,7 +497,7 @@ impl<A: OpenAdapter<T::OpenData>, T: Operations> OperationsVtable<A, T> {
496497
fasync: None,
497498
flock: None,
498499
flush: None,
499-
fsync: if T::TO_USE.fsync {
500+
fsync: if T::HAS_FSYNC {
500501
Some(Self::fsync_callback)
501502
} else {
502503
None
@@ -506,19 +507,19 @@ impl<A: OpenAdapter<T::OpenData>, T: Operations> OperationsVtable<A, T> {
506507
iterate_shared: None,
507508
iopoll: None,
508509
lock: None,
509-
mmap: if T::TO_USE.mmap {
510+
mmap: if T::HAS_MMAP {
510511
Some(Self::mmap_callback)
511512
} else {
512513
None
513514
},
514515
mmap_supported_flags: 0,
515516
owner: ptr::null_mut(),
516-
poll: if T::TO_USE.poll {
517+
poll: if T::HAS_POLL {
517518
Some(Self::poll_callback)
518519
} else {
519520
None
520521
},
521-
read_iter: if T::TO_USE.read_iter {
522+
read_iter: if T::HAS_READ {
522523
Some(Self::read_iter_callback)
523524
} else {
524525
None
@@ -529,13 +530,13 @@ impl<A: OpenAdapter<T::OpenData>, T: Operations> OperationsVtable<A, T> {
529530
show_fdinfo: None,
530531
splice_read: None,
531532
splice_write: None,
532-
unlocked_ioctl: if T::TO_USE.ioctl {
533+
unlocked_ioctl: if T::HAS_IOCTL {
533534
Some(Self::unlocked_ioctl_callback)
534535
} else {
535536
None
536537
},
537538
uring_cmd: None,
538-
write_iter: if T::TO_USE.write_iter {
539+
write_iter: if T::HAS_WRITE {
539540
Some(Self::write_iter_callback)
540541
} else {
541542
None
@@ -552,69 +553,6 @@ impl<A: OpenAdapter<T::OpenData>, T: Operations> OperationsVtable<A, T> {
552553
}
553554
}
554555

555-
/// Represents which fields of [`struct file_operations`] should be populated with pointers.
556-
pub struct ToUse {
557-
/// The `read` field of [`struct file_operations`].
558-
pub read: bool,
559-
560-
/// The `read_iter` field of [`struct file_operations`].
561-
pub read_iter: bool,
562-
563-
/// The `write` field of [`struct file_operations`].
564-
pub write: bool,
565-
566-
/// The `write_iter` field of [`struct file_operations`].
567-
pub write_iter: bool,
568-
569-
/// The `llseek` field of [`struct file_operations`].
570-
pub seek: bool,
571-
572-
/// The `unlocked_ioctl` field of [`struct file_operations`].
573-
pub ioctl: bool,
574-
575-
/// The `compat_ioctl` field of [`struct file_operations`].
576-
pub compat_ioctl: bool,
577-
578-
/// The `fsync` field of [`struct file_operations`].
579-
pub fsync: bool,
580-
581-
/// The `mmap` field of [`struct file_operations`].
582-
pub mmap: bool,
583-
584-
/// The `poll` field of [`struct file_operations`].
585-
pub poll: bool,
586-
}
587-
588-
/// A constant version where all values are to set to `false`, that is, all supported fields will
589-
/// be set to null pointers.
590-
pub const USE_NONE: ToUse = ToUse {
591-
read: false,
592-
read_iter: false,
593-
write: false,
594-
write_iter: false,
595-
seek: false,
596-
ioctl: false,
597-
compat_ioctl: false,
598-
fsync: false,
599-
mmap: false,
600-
poll: false,
601-
};
602-
603-
/// Defines the [`Operations::TO_USE`] field based on a list of fields to be populated.
604-
#[macro_export]
605-
macro_rules! declare_file_operations {
606-
() => {
607-
const TO_USE: $crate::file::ToUse = $crate::file::USE_NONE;
608-
};
609-
($($i:ident),+) => {
610-
const TO_USE: kernel::file::ToUse =
611-
$crate::file::ToUse {
612-
$($i: true),+ ,
613-
..$crate::file::USE_NONE
614-
};
615-
};
616-
}
617-
618556
/// Allows the handling of ioctls defined with the `_IO`, `_IOR`, `_IOW`, and `_IOWR` macros.
619557
///
620558
/// For each macro, there is a handler function that takes the appropriate types as arguments.
@@ -742,10 +680,8 @@ pub trait OpenAdapter<T: Sync> {
742680
/// File descriptors may be used from multiple threads/processes concurrently, so your type must be
743681
/// [`Sync`]. It must also be [`Send`] because [`Operations::release`] will be called from the
744682
/// thread that decrements that associated file's refcount to zero.
683+
#[vtable]
745684
pub trait Operations {
746-
/// The methods to use to populate [`struct file_operations`].
747-
const TO_USE: ToUse;
748-
749685
/// The type of the context data returned by [`Operations::open`] and made available to
750686
/// other methods.
751687
type Data: PointerWrapper + Send + Sync = ();

rust/kernel/miscdev.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,8 @@ impl<T: file::Operations<OpenData = ()>> crate::Module for Module<T> {
275275
/// #[derive(Default)]
276276
/// struct MyFile;
277277
///
278-
/// impl kernel::file::Operations for MyFile {
279-
/// kernel::declare_file_operations!();
280-
/// }
278+
/// #[vtable]
279+
/// impl kernel::file::Operations for MyFile {}
281280
/// ```
282281
#[macro_export]
283282
macro_rules! module_misc_device {

samples/rust/rust_chrdev.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ module! {
1515

1616
struct RustFile;
1717

18+
#[vtable]
1819
impl file::Operations for RustFile {
19-
kernel::declare_file_operations!();
20-
2120
fn open(_shared: &(), _file: &file::File) -> Result {
2221
Ok(())
2322
}

samples/rust/rust_miscdev.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,11 @@ impl SharedState {
5151
}
5252

5353
struct Token;
54+
#[vtable]
5455
impl file::Operations for Token {
5556
type Data = Ref<SharedState>;
5657
type OpenData = Ref<SharedState>;
5758

58-
kernel::declare_file_operations!(read, write);
59-
6059
fn open(shared: &Ref<SharedState>, _file: &File) -> Result<Self::Data> {
6160
Ok(shared.clone())
6261
}

samples/rust/rust_random.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ module_misc_device! {
2121

2222
struct RandomFile;
2323

24+
#[vtable]
2425
impl file::Operations for RandomFile {
25-
kernel::declare_file_operations!(read, write, read_iter, write_iter);
26-
2726
fn open(_data: &(), _file: &File) -> Result {
2827
Ok(())
2928
}

samples/rust/rust_semaphore.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
1616
use core::sync::atomic::{AtomicU64, Ordering};
1717
use kernel::{
18-
condvar_init, declare_file_operations,
18+
condvar_init,
1919
file::{self, File, IoctlCommand, IoctlHandler},
2020
io_buffer::{IoBufferReader, IoBufferWriter},
2121
miscdev::Registration,
@@ -61,12 +61,11 @@ impl FileState {
6161
}
6262
}
6363

64+
#[vtable]
6465
impl file::Operations for FileState {
6566
type Data = Box<Self>;
6667
type OpenData = Ref<Semaphore>;
6768

68-
declare_file_operations!(read, write, ioctl);
69-
7069
fn open(shared: &Ref<Semaphore>, _file: &File) -> Result<Box<Self>> {
7170
Ok(Box::try_new(Self {
7271
read_count: AtomicU64::new(0),

0 commit comments

Comments
 (0)