1 // SPDX-License-Identifier: GPL-2.0 2 3 //! This is a Rust implementation of the C null block driver. 4 //! 5 //! Supported features: 6 //! 7 //! - blk-mq interface 8 //! - direct completion 9 //! - block size 4k 10 //! 11 //! The driver is not configurable. 12 13 use kernel::{ 14 alloc::flags, 15 block::mq::{ 16 self, 17 gen_disk::{self, GenDisk}, 18 Operations, TagSet, 19 }, 20 error::Result, 21 new_mutex, pr_info, 22 prelude::*, 23 sync::{Arc, Mutex}, 24 types::ARef, 25 }; 26 27 module! { 28 type: NullBlkModule, 29 name: "rnull_mod", 30 author: "Andreas Hindborg", 31 description: "Rust implementation of the C null block driver", 32 license: "GPL v2", 33 } 34 35 struct NullBlkModule { 36 _disk: Pin<KBox<Mutex<GenDisk<NullBlkDevice>>>>, 37 } 38 39 impl kernel::Module for NullBlkModule { 40 fn init(_module: &'static ThisModule) -> Result<Self> { 41 pr_info!("Rust null_blk loaded\n"); 42 let tagset = Arc::pin_init(TagSet::new(1, 256, 1), flags::GFP_KERNEL)?; 43 44 let disk = gen_disk::GenDiskBuilder::new() 45 .capacity_sectors(4096 << 11) 46 .logical_block_size(4096)? 47 .physical_block_size(4096)? 48 .rotational(false) 49 .build(format_args!("rnullb{}", 0), tagset)?; 50 51 let disk = KBox::pin_init(new_mutex!(disk, "nullb:disk"), flags::GFP_KERNEL)?; 52 53 Ok(Self { _disk: disk }) 54 } 55 } 56 57 struct NullBlkDevice; 58 59 #[vtable] 60 impl Operations for NullBlkDevice { 61 #[inline(always)] 62 fn queue_rq(rq: ARef<mq::Request<Self>>, _is_last: bool) -> Result { 63 mq::Request::end_ok(rq) 64 .map_err(|_e| kernel::error::code::EIO) 65 // We take no refcounts on the request, so we expect to be able to 66 // end the request. The request reference must be unique at this 67 // point, and so `end_ok` cannot fail. 68 .expect("Fatal error - expected to be able to end request"); 69 70 Ok(()) 71 } 72 73 fn commit_rqs() {} 74 } 75