rnull.rs 1.8 KB

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