diff --git a/kernel/src/device/registry/block.rs b/kernel/src/device/registry/block.rs index 2102390c1..61867f8d2 100644 --- a/kernel/src/device/registry/block.rs +++ b/kernel/src/device/registry/block.rs @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MPL-2.0 -use aster_block::BlockDevice; +use aster_block::{BlockDevice, SECTOR_SIZE}; use aster_virtio::device::block::device::BlockDevice as VirtIoBlockDevice; use device_id::DeviceId; use ostd::mm::VmIo; @@ -15,7 +15,7 @@ use crate::{ }, prelude::*, process::signal::{PollHandle, Pollable}, - thread::kernel_thread::ThreadOptions, + thread::kernel_thread::ThreadOptions, util::ioctl::{RawIoctl, dispatch_ioctl}, }; pub(super) fn init_in_first_kthread() { @@ -47,6 +47,14 @@ pub(super) fn init_in_first_process(path_resolver: &PathResolver) -> Result<()> Ok(()) } + +mod ioctl_defs { + use crate::util::ioctl::{OutData, ioc}; + + // Reference: + pub(super) type BlkGetSize64 = ioc!(BLKGETSIZE64, 0x12, 114, OutData); +} + /// Represents a block device inode in the filesystem. // // TODO: This type wraps an `Arc` in another `Arc` just to implement the `Device` @@ -127,6 +135,18 @@ impl FileIo for OpenBlockFile { fn is_offset_aware(&self) -> bool { true } + + fn ioctl(&self, raw_ioctl: RawIoctl) -> Result { + use ioctl_defs::*; + dispatch_ioctl!(match raw_ioctl { + cmd @ BlkGetSize64 => { + let size = (self.0.metadata().nr_sectors * SECTOR_SIZE) as u64; + cmd.write(&size)?; + Ok(0) + } + _ => return_errno_with_message!(Errno::ENOTTY, "unsupported ioctl on block device"), + }) + } } pub(super) fn lookup(id: DeviceId) -> Option> {