1 use core::{any::Any, fmt::Debug};
2
3 use alloc::{
4 collections::LinkedList,
5 string::{String, ToString},
6 sync::{Arc, Weak},
7 vec::Vec,
8 };
9 use bitmap::traits::BitMapOps;
10 use log::error;
11 use system_error::SystemError;
12 use unified_init::macros::unified_init;
13 use virtio_drivers::device::blk::{VirtIOBlk, SECTOR_SIZE};
14
15 use crate::{
16 driver::{
17 base::{
18 block::{
19 block_device::{BlockDevName, BlockDevice, BlockId, GeneralBlockRange, LBA_SIZE},
20 disk_info::Partition,
21 manager::{block_dev_manager, BlockDevMeta},
22 },
23 class::Class,
24 device::{
25 bus::Bus,
26 driver::{Driver, DriverCommonData},
27 Device, DeviceCommonData, DeviceId, DeviceType, IdTable,
28 },
29 kobject::{KObjType, KObject, KObjectCommonData, KObjectState, LockedKObjectState},
30 kset::KSet,
31 },
32 virtio::{
33 sysfs::{virtio_bus, virtio_device_manager, virtio_driver_manager},
34 transport::VirtIOTransport,
35 virtio_impl::HalImpl,
36 VirtIODevice, VirtIODeviceIndex, VirtIODriver, VirtIODriverCommonData, VirtioDeviceId,
37 VIRTIO_VENDOR_ID,
38 },
39 },
40 exception::{irqdesc::IrqReturn, IrqNumber},
41 filesystem::{kernfs::KernFSInode, mbr::MbrDiskPartionTable},
42 init::initcall::INITCALL_POSTCORE,
43 libs::{
44 rwlock::{RwLockReadGuard, RwLockWriteGuard},
45 spinlock::{SpinLock, SpinLockGuard},
46 },
47 };
48
49 const VIRTIO_BLK_BASENAME: &str = "virtio_blk";
50
51 static mut VIRTIO_BLK_DRIVER: Option<Arc<VirtIOBlkDriver>> = None;
52
53 #[inline(always)]
54 #[allow(dead_code)]
virtio_blk_driver() -> Arc<VirtIOBlkDriver>55 fn virtio_blk_driver() -> Arc<VirtIOBlkDriver> {
56 unsafe { VIRTIO_BLK_DRIVER.as_ref().unwrap().clone() }
57 }
58
59 /// Get the first virtio block device
60 #[allow(dead_code)]
virtio_blk_0() -> Option<Arc<VirtIOBlkDevice>>61 pub fn virtio_blk_0() -> Option<Arc<VirtIOBlkDevice>> {
62 virtio_blk_driver()
63 .devices()
64 .first()
65 .cloned()
66 .map(|dev| dev.arc_any().downcast().unwrap())
67 }
68
virtio_blk( transport: VirtIOTransport, dev_id: Arc<DeviceId>, dev_parent: Option<Arc<dyn Device>>, )69 pub fn virtio_blk(
70 transport: VirtIOTransport,
71 dev_id: Arc<DeviceId>,
72 dev_parent: Option<Arc<dyn Device>>,
73 ) {
74 let device = VirtIOBlkDevice::new(transport, dev_id);
75 if let Some(device) = device {
76 if let Some(dev_parent) = dev_parent {
77 device.set_dev_parent(Some(Arc::downgrade(&dev_parent)));
78 }
79 virtio_device_manager()
80 .device_add(device.clone() as Arc<dyn VirtIODevice>)
81 .expect("Add virtio blk failed");
82 }
83 }
84
85 static mut VIRTIOBLK_MANAGER: Option<VirtIOBlkManager> = None;
86
87 #[inline]
virtioblk_manager() -> &'static VirtIOBlkManager88 fn virtioblk_manager() -> &'static VirtIOBlkManager {
89 unsafe { VIRTIOBLK_MANAGER.as_ref().unwrap() }
90 }
91
92 #[unified_init(INITCALL_POSTCORE)]
virtioblk_manager_init() -> Result<(), SystemError>93 fn virtioblk_manager_init() -> Result<(), SystemError> {
94 unsafe {
95 VIRTIOBLK_MANAGER = Some(VirtIOBlkManager::new());
96 }
97 Ok(())
98 }
99
100 pub struct VirtIOBlkManager {
101 inner: SpinLock<InnerVirtIOBlkManager>,
102 }
103
104 struct InnerVirtIOBlkManager {
105 id_bmp: bitmap::StaticBitmap<{ VirtIOBlkManager::MAX_DEVICES }>,
106 devname: [Option<BlockDevName>; VirtIOBlkManager::MAX_DEVICES],
107 }
108
109 impl VirtIOBlkManager {
110 pub const MAX_DEVICES: usize = 25;
111
new() -> Self112 pub fn new() -> Self {
113 Self {
114 inner: SpinLock::new(InnerVirtIOBlkManager {
115 id_bmp: bitmap::StaticBitmap::new(),
116 devname: [const { None }; Self::MAX_DEVICES],
117 }),
118 }
119 }
120
inner(&self) -> SpinLockGuard<InnerVirtIOBlkManager>121 fn inner(&self) -> SpinLockGuard<InnerVirtIOBlkManager> {
122 self.inner.lock()
123 }
124
alloc_id(&self) -> Option<BlockDevName>125 pub fn alloc_id(&self) -> Option<BlockDevName> {
126 let mut inner = self.inner();
127 let idx = inner.id_bmp.first_false_index()?;
128 inner.id_bmp.set(idx, true);
129 let name = Self::format_name(idx);
130 inner.devname[idx] = Some(name.clone());
131 Some(name)
132 }
133
134 /// Generate a new block device name like 'vda', 'vdb', etc.
format_name(id: usize) -> BlockDevName135 fn format_name(id: usize) -> BlockDevName {
136 let x = (b'a' + id as u8) as char;
137 BlockDevName::new(format!("vd{}", x), id)
138 }
139
140 #[allow(dead_code)]
free_id(&self, id: usize)141 pub fn free_id(&self, id: usize) {
142 if id >= Self::MAX_DEVICES {
143 return;
144 }
145 self.inner().id_bmp.set(id, false);
146 self.inner().devname[id] = None;
147 }
148 }
149
150 /// virtio block device
151 #[derive(Debug)]
152 #[cast_to([sync] VirtIODevice)]
153 #[cast_to([sync] Device)]
154 pub struct VirtIOBlkDevice {
155 blkdev_meta: BlockDevMeta,
156 dev_id: Arc<DeviceId>,
157 inner: SpinLock<InnerVirtIOBlkDevice>,
158 locked_kobj_state: LockedKObjectState,
159 self_ref: Weak<Self>,
160 }
161
162 unsafe impl Send for VirtIOBlkDevice {}
163 unsafe impl Sync for VirtIOBlkDevice {}
164
165 impl VirtIOBlkDevice {
new(transport: VirtIOTransport, dev_id: Arc<DeviceId>) -> Option<Arc<Self>>166 pub fn new(transport: VirtIOTransport, dev_id: Arc<DeviceId>) -> Option<Arc<Self>> {
167 // 设置中断
168 if let Err(err) = transport.setup_irq(dev_id.clone()) {
169 error!("VirtIOBlkDevice '{dev_id:?}' setup_irq failed: {:?}", err);
170 return None;
171 }
172
173 let devname = virtioblk_manager().alloc_id()?;
174 let irq = Some(transport.irq());
175 let device_inner = VirtIOBlk::<HalImpl, VirtIOTransport>::new(transport);
176 if let Err(e) = device_inner {
177 error!("VirtIOBlkDevice '{dev_id:?}' create failed: {:?}", e);
178 return None;
179 }
180
181 let mut device_inner: VirtIOBlk<HalImpl, VirtIOTransport> = device_inner.unwrap();
182 device_inner.enable_interrupts();
183 let dev = Arc::new_cyclic(|self_ref| Self {
184 blkdev_meta: BlockDevMeta::new(devname),
185 self_ref: self_ref.clone(),
186 dev_id,
187 locked_kobj_state: LockedKObjectState::default(),
188 inner: SpinLock::new(InnerVirtIOBlkDevice {
189 device_inner,
190 name: None,
191 virtio_index: None,
192 device_common: DeviceCommonData::default(),
193 kobject_common: KObjectCommonData::default(),
194 irq,
195 }),
196 });
197
198 Some(dev)
199 }
200
inner(&self) -> SpinLockGuard<InnerVirtIOBlkDevice>201 fn inner(&self) -> SpinLockGuard<InnerVirtIOBlkDevice> {
202 self.inner.lock()
203 }
204 }
205
206 impl BlockDevice for VirtIOBlkDevice {
dev_name(&self) -> &BlockDevName207 fn dev_name(&self) -> &BlockDevName {
208 &self.blkdev_meta.devname
209 }
210
blkdev_meta(&self) -> &BlockDevMeta211 fn blkdev_meta(&self) -> &BlockDevMeta {
212 &self.blkdev_meta
213 }
214
disk_range(&self) -> GeneralBlockRange215 fn disk_range(&self) -> GeneralBlockRange {
216 let inner = self.inner();
217 let blocks = inner.device_inner.capacity() as usize * SECTOR_SIZE / LBA_SIZE;
218 drop(inner);
219 log::debug!(
220 "VirtIOBlkDevice '{:?}' disk_range: 0..{}",
221 self.dev_name(),
222 blocks
223 );
224 GeneralBlockRange::new(0, blocks).unwrap()
225 }
226
read_at_sync( &self, lba_id_start: BlockId, count: usize, buf: &mut [u8], ) -> Result<usize, SystemError>227 fn read_at_sync(
228 &self,
229 lba_id_start: BlockId,
230 count: usize,
231 buf: &mut [u8],
232 ) -> Result<usize, SystemError> {
233 let mut inner = self.inner();
234
235 inner
236 .device_inner
237 .read_blocks(lba_id_start, &mut buf[..count * LBA_SIZE])
238 .map_err(|e| {
239 error!(
240 "VirtIOBlkDevice '{:?}' read_at_sync failed: {:?}",
241 self.dev_id, e
242 );
243 SystemError::EIO
244 })?;
245
246 Ok(count)
247 }
248
write_at_sync( &self, lba_id_start: BlockId, count: usize, buf: &[u8], ) -> Result<usize, SystemError>249 fn write_at_sync(
250 &self,
251 lba_id_start: BlockId,
252 count: usize,
253 buf: &[u8],
254 ) -> Result<usize, SystemError> {
255 self.inner()
256 .device_inner
257 .write_blocks(lba_id_start, &buf[..count * LBA_SIZE])
258 .map_err(|_| SystemError::EIO)?;
259 Ok(count)
260 }
261
sync(&self) -> Result<(), SystemError>262 fn sync(&self) -> Result<(), SystemError> {
263 Ok(())
264 }
265
blk_size_log2(&self) -> u8266 fn blk_size_log2(&self) -> u8 {
267 9
268 }
269
as_any_ref(&self) -> &dyn Any270 fn as_any_ref(&self) -> &dyn Any {
271 self
272 }
273
device(&self) -> Arc<dyn Device>274 fn device(&self) -> Arc<dyn Device> {
275 self.self_ref.upgrade().unwrap()
276 }
277
block_size(&self) -> usize278 fn block_size(&self) -> usize {
279 todo!()
280 }
281
partitions(&self) -> Vec<Arc<Partition>>282 fn partitions(&self) -> Vec<Arc<Partition>> {
283 let device = self.self_ref.upgrade().unwrap() as Arc<dyn BlockDevice>;
284 let mbr_table = MbrDiskPartionTable::from_disk(device.clone())
285 .expect("Failed to get MBR partition table");
286 mbr_table.partitions(Arc::downgrade(&device))
287 }
288 }
289
290 struct InnerVirtIOBlkDevice {
291 device_inner: VirtIOBlk<HalImpl, VirtIOTransport>,
292 name: Option<String>,
293 virtio_index: Option<VirtIODeviceIndex>,
294 device_common: DeviceCommonData,
295 kobject_common: KObjectCommonData,
296 irq: Option<IrqNumber>,
297 }
298
299 impl Debug for InnerVirtIOBlkDevice {
fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result300 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
301 f.debug_struct("InnerVirtIOBlkDevice").finish()
302 }
303 }
304
305 impl VirtIODevice for VirtIOBlkDevice {
irq(&self) -> Option<IrqNumber>306 fn irq(&self) -> Option<IrqNumber> {
307 self.inner().irq
308 }
309
handle_irq( &self, _irq: crate::exception::IrqNumber, ) -> Result<IrqReturn, system_error::SystemError>310 fn handle_irq(
311 &self,
312 _irq: crate::exception::IrqNumber,
313 ) -> Result<IrqReturn, system_error::SystemError> {
314 // todo: handle virtio blk irq
315 Ok(crate::exception::irqdesc::IrqReturn::Handled)
316 }
317
dev_id(&self) -> &Arc<DeviceId>318 fn dev_id(&self) -> &Arc<DeviceId> {
319 &self.dev_id
320 }
321
set_device_name(&self, name: String)322 fn set_device_name(&self, name: String) {
323 self.inner().name = Some(name);
324 }
325
device_name(&self) -> String326 fn device_name(&self) -> String {
327 self.inner()
328 .name
329 .clone()
330 .unwrap_or_else(|| VIRTIO_BLK_BASENAME.to_string())
331 }
332
set_virtio_device_index(&self, index: VirtIODeviceIndex)333 fn set_virtio_device_index(&self, index: VirtIODeviceIndex) {
334 self.inner().virtio_index = Some(index);
335 }
336
virtio_device_index(&self) -> Option<VirtIODeviceIndex>337 fn virtio_device_index(&self) -> Option<VirtIODeviceIndex> {
338 self.inner().virtio_index
339 }
340
device_type_id(&self) -> u32341 fn device_type_id(&self) -> u32 {
342 virtio_drivers::transport::DeviceType::Block as u32
343 }
344
vendor(&self) -> u32345 fn vendor(&self) -> u32 {
346 VIRTIO_VENDOR_ID.into()
347 }
348 }
349
350 impl Device for VirtIOBlkDevice {
dev_type(&self) -> DeviceType351 fn dev_type(&self) -> DeviceType {
352 DeviceType::Net
353 }
354
id_table(&self) -> IdTable355 fn id_table(&self) -> IdTable {
356 IdTable::new(VIRTIO_BLK_BASENAME.to_string(), None)
357 }
358
bus(&self) -> Option<Weak<dyn Bus>>359 fn bus(&self) -> Option<Weak<dyn Bus>> {
360 self.inner().device_common.bus.clone()
361 }
362
set_bus(&self, bus: Option<Weak<dyn Bus>>)363 fn set_bus(&self, bus: Option<Weak<dyn Bus>>) {
364 self.inner().device_common.bus = bus;
365 }
366
class(&self) -> Option<Arc<dyn Class>>367 fn class(&self) -> Option<Arc<dyn Class>> {
368 let mut guard = self.inner();
369 let r = guard.device_common.class.clone()?.upgrade();
370 if r.is_none() {
371 guard.device_common.class = None;
372 }
373
374 return r;
375 }
376
set_class(&self, class: Option<Weak<dyn Class>>)377 fn set_class(&self, class: Option<Weak<dyn Class>>) {
378 self.inner().device_common.class = class;
379 }
380
driver(&self) -> Option<Arc<dyn Driver>>381 fn driver(&self) -> Option<Arc<dyn Driver>> {
382 let r = self.inner().device_common.driver.clone()?.upgrade();
383 if r.is_none() {
384 self.inner().device_common.driver = None;
385 }
386
387 return r;
388 }
389
set_driver(&self, driver: Option<Weak<dyn Driver>>)390 fn set_driver(&self, driver: Option<Weak<dyn Driver>>) {
391 self.inner().device_common.driver = driver;
392 }
393
is_dead(&self) -> bool394 fn is_dead(&self) -> bool {
395 false
396 }
397
can_match(&self) -> bool398 fn can_match(&self) -> bool {
399 self.inner().device_common.can_match
400 }
401
set_can_match(&self, can_match: bool)402 fn set_can_match(&self, can_match: bool) {
403 self.inner().device_common.can_match = can_match;
404 }
405
state_synced(&self) -> bool406 fn state_synced(&self) -> bool {
407 true
408 }
409
dev_parent(&self) -> Option<Weak<dyn Device>>410 fn dev_parent(&self) -> Option<Weak<dyn Device>> {
411 self.inner().device_common.get_parent_weak_or_clear()
412 }
413
set_dev_parent(&self, parent: Option<Weak<dyn Device>>)414 fn set_dev_parent(&self, parent: Option<Weak<dyn Device>>) {
415 self.inner().device_common.parent = parent;
416 }
417 }
418
419 impl KObject for VirtIOBlkDevice {
as_any_ref(&self) -> &dyn Any420 fn as_any_ref(&self) -> &dyn Any {
421 self
422 }
423
set_inode(&self, inode: Option<Arc<KernFSInode>>)424 fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
425 self.inner().kobject_common.kern_inode = inode;
426 }
427
inode(&self) -> Option<Arc<KernFSInode>>428 fn inode(&self) -> Option<Arc<KernFSInode>> {
429 self.inner().kobject_common.kern_inode.clone()
430 }
431
parent(&self) -> Option<Weak<dyn KObject>>432 fn parent(&self) -> Option<Weak<dyn KObject>> {
433 self.inner().kobject_common.parent.clone()
434 }
435
set_parent(&self, parent: Option<Weak<dyn KObject>>)436 fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
437 self.inner().kobject_common.parent = parent;
438 }
439
kset(&self) -> Option<Arc<KSet>>440 fn kset(&self) -> Option<Arc<KSet>> {
441 self.inner().kobject_common.kset.clone()
442 }
443
set_kset(&self, kset: Option<Arc<KSet>>)444 fn set_kset(&self, kset: Option<Arc<KSet>>) {
445 self.inner().kobject_common.kset = kset;
446 }
447
kobj_type(&self) -> Option<&'static dyn KObjType>448 fn kobj_type(&self) -> Option<&'static dyn KObjType> {
449 self.inner().kobject_common.kobj_type
450 }
451
name(&self) -> String452 fn name(&self) -> String {
453 self.device_name()
454 }
455
set_name(&self, _name: String)456 fn set_name(&self, _name: String) {
457 // do nothing
458 }
459
kobj_state(&self) -> RwLockReadGuard<KObjectState>460 fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
461 self.locked_kobj_state.read()
462 }
463
kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState>464 fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
465 self.locked_kobj_state.write()
466 }
467
set_kobj_state(&self, state: KObjectState)468 fn set_kobj_state(&self, state: KObjectState) {
469 *self.locked_kobj_state.write() = state;
470 }
471
set_kobj_type(&self, ktype: Option<&'static dyn KObjType>)472 fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) {
473 self.inner().kobject_common.kobj_type = ktype;
474 }
475 }
476
477 #[unified_init(INITCALL_POSTCORE)]
virtio_blk_driver_init() -> Result<(), SystemError>478 fn virtio_blk_driver_init() -> Result<(), SystemError> {
479 let driver = VirtIOBlkDriver::new();
480 virtio_driver_manager()
481 .register(driver.clone() as Arc<dyn VirtIODriver>)
482 .expect("Add virtio net driver failed");
483 unsafe {
484 VIRTIO_BLK_DRIVER = Some(driver);
485 }
486
487 return Ok(());
488 }
489
490 #[derive(Debug)]
491 #[cast_to([sync] VirtIODriver)]
492 #[cast_to([sync] Driver)]
493 struct VirtIOBlkDriver {
494 inner: SpinLock<InnerVirtIOBlkDriver>,
495 kobj_state: LockedKObjectState,
496 }
497
498 impl VirtIOBlkDriver {
new() -> Arc<Self>499 pub fn new() -> Arc<Self> {
500 let inner = InnerVirtIOBlkDriver {
501 virtio_driver_common: VirtIODriverCommonData::default(),
502 driver_common: DriverCommonData::default(),
503 kobj_common: KObjectCommonData::default(),
504 };
505
506 let id_table = VirtioDeviceId::new(
507 virtio_drivers::transport::DeviceType::Block as u32,
508 VIRTIO_VENDOR_ID.into(),
509 );
510 let result = VirtIOBlkDriver {
511 inner: SpinLock::new(inner),
512 kobj_state: LockedKObjectState::default(),
513 };
514 result.add_virtio_id(id_table);
515
516 return Arc::new(result);
517 }
518
inner(&self) -> SpinLockGuard<InnerVirtIOBlkDriver>519 fn inner(&self) -> SpinLockGuard<InnerVirtIOBlkDriver> {
520 return self.inner.lock();
521 }
522 }
523
524 #[derive(Debug)]
525 struct InnerVirtIOBlkDriver {
526 virtio_driver_common: VirtIODriverCommonData,
527 driver_common: DriverCommonData,
528 kobj_common: KObjectCommonData,
529 }
530
531 impl VirtIODriver for VirtIOBlkDriver {
probe(&self, device: &Arc<dyn VirtIODevice>) -> Result<(), SystemError>532 fn probe(&self, device: &Arc<dyn VirtIODevice>) -> Result<(), SystemError> {
533 let dev = device
534 .clone()
535 .arc_any()
536 .downcast::<VirtIOBlkDevice>()
537 .map_err(|_| {
538 error!(
539 "VirtIOBlkDriver::probe() failed: device is not a VirtIO block device. Device: '{:?}'",
540 device.name()
541 );
542 SystemError::EINVAL
543 })?;
544
545 block_dev_manager().register(dev as Arc<dyn BlockDevice>)?;
546 return Ok(());
547 }
548
virtio_id_table(&self) -> LinkedList<crate::driver::virtio::VirtioDeviceId>549 fn virtio_id_table(&self) -> LinkedList<crate::driver::virtio::VirtioDeviceId> {
550 self.inner().virtio_driver_common.id_table.clone()
551 }
552
add_virtio_id(&self, id: VirtioDeviceId)553 fn add_virtio_id(&self, id: VirtioDeviceId) {
554 self.inner().virtio_driver_common.id_table.push_back(id);
555 }
556 }
557
558 impl Driver for VirtIOBlkDriver {
id_table(&self) -> Option<IdTable>559 fn id_table(&self) -> Option<IdTable> {
560 Some(IdTable::new(VIRTIO_BLK_BASENAME.to_string(), None))
561 }
562
add_device(&self, device: Arc<dyn Device>)563 fn add_device(&self, device: Arc<dyn Device>) {
564 let iface = device
565 .arc_any()
566 .downcast::<VirtIOBlkDevice>()
567 .expect("VirtIOBlkDriver::add_device() failed: device is not a VirtIOBlkDevice");
568
569 self.inner()
570 .driver_common
571 .devices
572 .push(iface as Arc<dyn Device>);
573 }
574
delete_device(&self, device: &Arc<dyn Device>)575 fn delete_device(&self, device: &Arc<dyn Device>) {
576 let _iface = device
577 .clone()
578 .arc_any()
579 .downcast::<VirtIOBlkDevice>()
580 .expect("VirtIOBlkDriver::delete_device() failed: device is not a VirtIOBlkDevice");
581
582 let mut guard = self.inner();
583 let index = guard
584 .driver_common
585 .devices
586 .iter()
587 .position(|dev| Arc::ptr_eq(device, dev))
588 .expect("VirtIOBlkDriver::delete_device() failed: device not found");
589
590 guard.driver_common.devices.remove(index);
591 }
592
devices(&self) -> Vec<Arc<dyn Device>>593 fn devices(&self) -> Vec<Arc<dyn Device>> {
594 self.inner().driver_common.devices.clone()
595 }
596
bus(&self) -> Option<Weak<dyn Bus>>597 fn bus(&self) -> Option<Weak<dyn Bus>> {
598 Some(Arc::downgrade(&virtio_bus()) as Weak<dyn Bus>)
599 }
600
set_bus(&self, _bus: Option<Weak<dyn Bus>>)601 fn set_bus(&self, _bus: Option<Weak<dyn Bus>>) {
602 // do nothing
603 }
604 }
605
606 impl KObject for VirtIOBlkDriver {
as_any_ref(&self) -> &dyn Any607 fn as_any_ref(&self) -> &dyn Any {
608 self
609 }
610
set_inode(&self, inode: Option<Arc<KernFSInode>>)611 fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
612 self.inner().kobj_common.kern_inode = inode;
613 }
614
inode(&self) -> Option<Arc<KernFSInode>>615 fn inode(&self) -> Option<Arc<KernFSInode>> {
616 self.inner().kobj_common.kern_inode.clone()
617 }
618
parent(&self) -> Option<Weak<dyn KObject>>619 fn parent(&self) -> Option<Weak<dyn KObject>> {
620 self.inner().kobj_common.parent.clone()
621 }
622
set_parent(&self, parent: Option<Weak<dyn KObject>>)623 fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
624 self.inner().kobj_common.parent = parent;
625 }
626
kset(&self) -> Option<Arc<KSet>>627 fn kset(&self) -> Option<Arc<KSet>> {
628 self.inner().kobj_common.kset.clone()
629 }
630
set_kset(&self, kset: Option<Arc<KSet>>)631 fn set_kset(&self, kset: Option<Arc<KSet>>) {
632 self.inner().kobj_common.kset = kset;
633 }
634
kobj_type(&self) -> Option<&'static dyn KObjType>635 fn kobj_type(&self) -> Option<&'static dyn KObjType> {
636 self.inner().kobj_common.kobj_type
637 }
638
set_kobj_type(&self, ktype: Option<&'static dyn KObjType>)639 fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) {
640 self.inner().kobj_common.kobj_type = ktype;
641 }
642
name(&self) -> String643 fn name(&self) -> String {
644 VIRTIO_BLK_BASENAME.to_string()
645 }
646
set_name(&self, _name: String)647 fn set_name(&self, _name: String) {
648 // do nothing
649 }
650
kobj_state(&self) -> RwLockReadGuard<KObjectState>651 fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
652 self.kobj_state.read()
653 }
654
kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState>655 fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
656 self.kobj_state.write()
657 }
658
set_kobj_state(&self, state: KObjectState)659 fn set_kobj_state(&self, state: KObjectState) {
660 *self.kobj_state.write() = state;
661 }
662 }
663