From 7eb12758edddf94398736970fdbe256d8d34da3a Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Wed, 25 Jun 2003 00:04:55 +0000 Subject: [PATCH] * Fixed the locking functions: A lock also requires a reference to be kept. * Fixed find_{disk_device,partition}(). * Implemented delete_partition(). The device/partition management functions should now be complete. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@3654 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- .../disk_device_manager.cpp | 44 ++++++++++++------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/src/kernel/core/disk_device_manager/disk_device_manager.cpp b/src/kernel/core/disk_device_manager/disk_device_manager.cpp index 6a2bf37431..aff13d9919 100644 --- a/src/kernel/core/disk_device_manager/disk_device_manager.cpp +++ b/src/kernel/core/disk_device_manager/disk_device_manager.cpp @@ -19,10 +19,12 @@ disk_device_data * write_lock_disk_device(partition_id partitionID) { KDiskDeviceManager *manager = KDiskDeviceManager::Default(); - KDiskDevice *device = manager->RegisterDevice(partitionID); - if (device && device->WriteLock()) { + if (KDiskDevice *device = manager->RegisterDevice(partitionID)) { + if (device->WriteLock()) + return device->DeviceData(); + // Only unregister, when the locking fails. The guarantees, that the + // lock owner also has a reference. device->Unregister(); - return device->DeviceData(); } return NULL; } @@ -32,9 +34,12 @@ void write_unlock_disk_device(partition_id partitionID) { KDiskDeviceManager *manager = KDiskDeviceManager::Default(); - KDiskDevice *device = manager->RegisterDevice(partitionID); - if (device) { - device->WriteUnlock(); + if (KDiskDevice *device = manager->RegisterDevice(partitionID)) { + bool isLocked = device->IsWriteLocked(); + if (isLocked) { + device->WriteUnlock(); + device->Unregister(); + } device->Unregister(); } } @@ -44,10 +49,12 @@ disk_device_data * read_lock_disk_device(partition_id partitionID) { KDiskDeviceManager *manager = KDiskDeviceManager::Default(); - KDiskDevice *device = manager->RegisterDevice(partitionID); - if (device && device->ReadLock()) { + if (KDiskDevice *device = manager->RegisterDevice(partitionID)) { + if (device->ReadLock()) + return device->DeviceData(); + // Only unregister, when the locking fails. The guarantees, that the + // lock owner also has a reference. device->Unregister(); - return device->DeviceData(); } return NULL; } @@ -57,9 +64,12 @@ void read_unlock_disk_device(partition_id partitionID) { KDiskDeviceManager *manager = KDiskDeviceManager::Default(); - KDiskDevice *device = manager->RegisterDevice(partitionID); - if (device) { - device->ReadUnlock(); + if (KDiskDevice *device = manager->RegisterDevice(partitionID)) { + bool isLocked = device->IsReadLocked(false); + if (isLocked) { + device->ReadUnlock(); + device->Unregister(); + } device->Unregister(); } } @@ -71,7 +81,7 @@ find_disk_device(const char *path) KDiskDeviceManager *manager = KDiskDeviceManager::Default(); partition_id id = -1; if (KDiskDevice *device = manager->RegisterDevice(path)) { - device->ID(); + id = device->ID(); device->Unregister(); } return id; @@ -84,7 +94,7 @@ find_partition(const char *path) KDiskDeviceManager *manager = KDiskDeviceManager::Default(); partition_id id = -1; if (KPartition *partition = manager->RegisterPartition(path)) { - partition->ID(); + id = partition->ID(); partition->Unregister(); } return id; @@ -153,7 +163,11 @@ DBG(OUT(" partition %ld not found\n", partitionID)); bool delete_partition(partition_id partitionID) { - // not implemented + KDiskDeviceManager *manager = KDiskDeviceManager::Default(); + if (KPartition *partition = manager->FindPartition(partitionID)) { + if (KPartition *parent = partition->Parent()) + return parent->RemoveChild(partition); + } return false; }