@@ -16,6 +16,7 @@ use crate::{
1616 from_kernel_result,
1717 io_buffer:: { IoBufferReader , IoBufferWriter } ,
1818 iov_iter:: IovIter ,
19+ prelude:: * ,
1920 sync:: CondVar ,
2021 types:: PointerWrapper ,
2122 user_ptr:: { UserSlicePtr , UserSlicePtrReader , UserSlicePtrWriter } ,
@@ -254,24 +255,24 @@ impl<A: FileOpenAdapter, T: FileOpener<A::Arg>> FileOperationsVtable<A, T> {
254255 const VTABLE : bindings:: file_operations = bindings:: file_operations {
255256 open : Some ( open_callback :: < A , T > ) ,
256257 release : Some ( release_callback :: < T > ) ,
257- read : if T :: TO_USE . read {
258+ read : if T :: HAS_READ {
258259 Some ( read_callback :: < T > )
259260 } else {
260261 None
261262 } ,
262- write : if T :: TO_USE . write {
263+ write : if T :: HAS_WRITE {
263264 Some ( write_callback :: < T > )
264265 } else {
265266 None
266267 } ,
267- llseek : if T :: TO_USE . seek {
268+ llseek : if T :: HAS_SEEK {
268269 Some ( llseek_callback :: < T > )
269270 } else {
270271 None
271272 } ,
272273
273274 check_flags : None ,
274- compat_ioctl : if T :: TO_USE . compat_ioctl {
275+ compat_ioctl : if T :: HAS_COMPAT_IOCTL {
275276 Some ( compat_ioctl_callback :: < T > )
276277 } else {
277278 None
@@ -282,7 +283,7 @@ impl<A: FileOpenAdapter, T: FileOpener<A::Arg>> FileOperationsVtable<A, T> {
282283 fasync : None ,
283284 flock : None ,
284285 flush : None ,
285- fsync : if T :: TO_USE . fsync {
286+ fsync : if T :: HAS_FSYNC {
286287 Some ( fsync_callback :: < T > )
287288 } else {
288289 None
@@ -292,19 +293,19 @@ impl<A: FileOpenAdapter, T: FileOpener<A::Arg>> FileOperationsVtable<A, T> {
292293 iterate_shared : None ,
293294 iopoll : None ,
294295 lock : None ,
295- mmap : if T :: TO_USE . mmap {
296+ mmap : if T :: HAS_MMAP {
296297 Some ( mmap_callback :: < T > )
297298 } else {
298299 None
299300 } ,
300301 mmap_supported_flags : 0 ,
301302 owner : ptr:: null_mut ( ) ,
302- poll : if T :: TO_USE . poll {
303+ poll : if T :: HAS_POLL {
303304 Some ( poll_callback :: < T > )
304305 } else {
305306 None
306307 } ,
307- read_iter : if T :: TO_USE . read_iter {
308+ read_iter : if T :: HAS_READ {
308309 Some ( read_iter_callback :: < T > )
309310 } else {
310311 None
@@ -315,12 +316,12 @@ impl<A: FileOpenAdapter, T: FileOpener<A::Arg>> FileOperationsVtable<A, T> {
315316 show_fdinfo : None ,
316317 splice_read : None ,
317318 splice_write : None ,
318- unlocked_ioctl : if T :: TO_USE . ioctl {
319+ unlocked_ioctl : if T :: HAS_IOCTL {
319320 Some ( unlocked_ioctl_callback :: < T > )
320321 } else {
321322 None
322323 } ,
323- write_iter : if T :: TO_USE . write_iter {
324+ write_iter : if T :: HAS_WRITE {
324325 Some ( write_iter_callback :: < T > )
325326 } else {
326327 None
@@ -337,69 +338,6 @@ impl<A: FileOpenAdapter, T: FileOpener<A::Arg>> FileOperationsVtable<A, T> {
337338 }
338339}
339340
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-
403341/// Allows the handling of ioctls defined with the `_IO`, `_IOR`, `_IOW`, and `_IOWR` macros.
404342///
405343/// 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 {
527465/// File descriptors may be used from multiple threads/processes concurrently, so your type must be
528466/// [`Sync`]. It must also be [`Send`] because [`FileOperations::release`] will be called from the
529467/// thread that decrements that associated file's refcount to zero.
468+ #[ vtable]
530469pub trait FileOperations : Send + Sync + Sized {
531- /// The methods to use to populate [`struct file_operations`].
532- const TO_USE : ToUse ;
533-
534470 /// The pointer type that will be used to hold ourselves.
535471 type Wrapper : PointerWrapper = Box < Self > ;
536472
0 commit comments