diff --git a/headers/private/kernel/disk_device_manager/KDiskDeviceManager.h b/headers/private/kernel/disk_device_manager/KDiskDeviceManager.h index 12103bc627..81010d4463 100644 --- a/headers/private/kernel/disk_device_manager/KDiskDeviceManager.h +++ b/headers/private/kernel/disk_device_manager/KDiskDeviceManager.h @@ -36,6 +36,12 @@ public: // Disk Device / Partition Management + // manager must be locked + KDiskDevice *FindDevice(const char *path, bool noShadow = true); + KDiskDevice *FindDevice(partition_id id, bool noShadow = true); + KPartition *FindPartition(const char *path, bool noShadow = true); + KPartition *FindPartition(partition_id id, bool noShadow = true); + KDiskDevice *RegisterDevice(const char *path, bool noShadow = true); KDiskDevice *RegisterDevice(partition_id id, bool noShadow = true); KPartition *RegisterPartition(const char *path, bool noShadow = true); @@ -74,11 +80,14 @@ public: // by the registrar. private: - KPartition *_FindPartition(partition_id id) const; + status_t _AddPartitioningSystem(const char *name); + status_t _AddFileSystem(const char *name); + status_t _AddDiskSystem(KDiskSystem *diskSystem); BLocker fLock; List fDevices; // TODO: Optimize! List fPartitions; // + List fDiskSystems; // static KDiskDeviceManager *fDefaultManager; }; diff --git a/src/kernel/core/disk_device_manager/KDiskDeviceManager.cpp b/src/kernel/core/disk_device_manager/KDiskDeviceManager.cpp index ba79b4da58..9debe67ef7 100644 --- a/src/kernel/core/disk_device_manager/KDiskDeviceManager.cpp +++ b/src/kernel/core/disk_device_manager/KDiskDeviceManager.cpp @@ -1,24 +1,53 @@ // KDiskDeviceManager.cpp +#include #include #include #include "KDiskDevice.h" #include "KDiskDeviceManager.h" #include "KDiskDeviceUtils.h" +#include "KDiskSystem.h" +#include "KFileSystem.h" #include "KPartition.h" +#include "KPartitioningSystem.h" // TODO: Remove when not longer needed. using BPrivate::DiskDevice::KDiskDeviceJob; using BPrivate::DiskDevice::KDiskDeviceJobQueue; -using BPrivate::DiskDevice::KDiskSystem; + +// directories for partitioning and file system modules +static const char *kPartitioningSystemPrefix = "partitioning_systems"; +static const char *kFileSystemPrefix = "file_systems"; // constructor KDiskDeviceManager::KDiskDeviceManager() : fLock("disk device manager"), fDevices(20), - fPartitions(100) + fPartitions(100), + fDiskSystems(20) { + // add partitioning systems + if (void *list = open_module_list(kPartitioningSystemPrefix)) { + char moduleName[B_PATH_NAME_LENGTH]; + for (size_t bufferSize = sizeof(moduleName); + read_next_module_name(list, moduleName, &bufferSize) == B_OK; + bufferSize = sizeof(moduleName)) { + _AddPartitioningSystem(moduleName); + } + close_module_list(list); + } + // add file systems + if (void *list = open_module_list(kFileSystemPrefix)) { + char moduleName[B_PATH_NAME_LENGTH]; + for (size_t bufferSize = sizeof(moduleName); + read_next_module_name(list, moduleName, &bufferSize) == B_OK; + bufferSize = sizeof(moduleName)) { + _AddFileSystem(moduleName); + } + close_module_list(list); + } + // TODO: Watch the disk systems and the relevant directories. } // destructor @@ -81,17 +110,65 @@ KDiskDeviceManager::Unlock() fLock.Unlock(); } +// FindDevice +KDiskDevice * +KDiskDeviceManager::FindDevice(const char *path, bool noShadow) +{ +// TODO: Handle shadows correctly! + for (int32 i = 0; KDiskDevice *device = fDevices.ItemAt(i); i++) { + if (device->Path() && !strcmp(path, device->Path())) + return device; + } + return NULL; +} + +// FindDevice +KDiskDevice * +KDiskDeviceManager::FindDevice(partition_id id, bool noShadow) +{ +// TODO: Handle shadows correctly! + if (KPartition *partition = FindPartition(id)) + return partition->Device(); + return NULL; +} + +// FindPartition +KPartition * +KDiskDeviceManager::FindPartition(const char *path, bool noShadow) +{ +// TODO: Handle shadows correctly! +// TODO: Optimize! + for (int32 i = 0; KPartition *partition = fPartitions.ItemAt(i); i++) { + char partitionPath[B_PATH_NAME_LENGTH]; + if (partition->GetPath(partitionPath) == B_OK + && !strcmp(path, partitionPath)) { + return partition; + } + } + return NULL; +} + +// FindPartition +KPartition * +KDiskDeviceManager::FindPartition(partition_id id, bool noShadow) +{ +// TODO: Handle shadows correctly! + for (int32 i = 0; KPartition *partition = fPartitions.ItemAt(i); i++) { + if (partition->ID() == id) + return partition; + } + return NULL; +} + // RegisterDevice KDiskDevice * KDiskDeviceManager::RegisterDevice(const char *path, bool noShadow) { // TODO: Handle shadows correctly! if (ManagerLocker locker = this) { - for (int32 i = 0; KDiskDevice *device = fDevices.ItemAt(i); i++) { - if (device->Path() && !strcmp(path, device->Path())) { - device->Register(); - return device; - } + if (KDiskDevice *device = FindDevice(path, noShadow)) { + device->Register(); + return device; } } return NULL; @@ -103,8 +180,7 @@ KDiskDeviceManager::RegisterDevice(partition_id id, bool noShadow) { // TODO: Handle shadows correctly! if (ManagerLocker locker = this) { - if (KPartition *partition = _FindPartition(id)) { - KDiskDevice *device = partition->Device(); + if (KDiskDevice *device = FindDevice(id, noShadow)) { device->Register(); return device; } @@ -116,16 +192,10 @@ KDiskDeviceManager::RegisterDevice(partition_id id, bool noShadow) KPartition * KDiskDeviceManager::RegisterPartition(const char *path, bool noShadow) { -// TODO: Handle shadows correctly! -// TODO: Optimize! if (ManagerLocker locker = this) { - for (int32 i = 0; KPartition *partition = fPartitions.ItemAt(i); i++) { - char partitionPath[B_PATH_NAME_LENGTH]; - if (partition->GetPath(partitionPath) == B_OK - && !strcmp(path, partitionPath)) { - partition->Register(); - return partition; - } + if (KPartition *partition = FindPartition(path, noShadow)) { + partition->Register(); + return partition; } } return NULL; @@ -135,9 +205,8 @@ KDiskDeviceManager::RegisterPartition(const char *path, bool noShadow) KPartition * KDiskDeviceManager::RegisterPartition(partition_id id, bool noShadow) { -// TODO: Handle shadows correctly! if (ManagerLocker locker = this) { - if (KPartition *partition = _FindPartition(id)) { + if (KPartition *partition = FindPartition(id, noShadow)) { partition->Register(); return partition; } @@ -229,7 +298,10 @@ KDiskDeviceManager::JobQueueAt(int32 index) KDiskSystem * KDiskDeviceManager::DiskSystemWithName(const char *name) { - // not implemented + for (int32 i = 0; KDiskSystem *diskSystem = fDiskSystems.ItemAt(i); i++) { + if (!strcmp(name, diskSystem->Name())) + return diskSystem; + } return NULL; } @@ -237,7 +309,10 @@ KDiskDeviceManager::DiskSystemWithName(const char *name) KDiskSystem * KDiskDeviceManager::DiskSystemWithID(disk_system_id id) { - // not implemented + for (int32 i = 0; KDiskSystem *diskSystem = fDiskSystems.ItemAt(i); i++) { + if (diskSystem->ID() == id) + return diskSystem; + } return NULL; } @@ -245,28 +320,59 @@ KDiskDeviceManager::DiskSystemWithID(disk_system_id id) int32 KDiskDeviceManager::CountDiskSystems() { - // not implemented - return 0; + return fDiskSystems.CountItems(); } // DiskSystemAt KDiskSystem * KDiskDeviceManager::DiskSystemAt(int32 index) { - // not implemented - return NULL; + return fDiskSystems.ItemAt(index); } -// _FindPartition -KPartition * -KDiskDeviceManager::_FindPartition(partition_id id) const +// _AddPartitioningSystem +status_t +KDiskDeviceManager::_AddPartitioningSystem(const char *name) { - for (int32 i = 0; KPartition *partition = fPartitions.ItemAt(i); i++) { - if (partition->ID() == id) - return partition; - } + if (!name) + return B_BAD_VALUE; + KDiskSystem *diskSystem = new(nothrow) KPartitioningSystem(name); + if (!diskSystem) + return B_NO_MEMORY; + return _AddDiskSystem(diskSystem); } +// _AddFileSystem +status_t +KDiskDeviceManager::_AddFileSystem(const char *name) +{ +// TODO: Uncomment when KFileSystem is implemented. +/* + if (!name) + return B_BAD_VALUE; + KDiskSystem *diskSystem = new(nothrow) KFileSystem(name); + if (!diskSystem) + return B_NO_MEMORY; + return _AddDiskSystem(diskSystem); +*/ + return B_ERROR; +} + +// _AddDiskSystem +status_t +KDiskDeviceManager::_AddDiskSystem(KDiskSystem *diskSystem) +{ + if (!diskSystem) + return B_BAD_VALUE; + status_t error = diskSystem->Init(); + if (error == B_OK && !fDiskSystems.AddItem(diskSystem)) + error = B_NO_MEMORY; + if (error != B_OK) + delete diskSystem; + return error; +} + + // singleton instance KDiskDeviceManager *KDiskDeviceManager::fDefaultManager = NULL;