1 /* 2 * Copyright 2004-2006, Haiku, Inc. All rights reserved. 3 * Copyright 2003-2004, Ingo Weinhold, bonefish@cs.tu-berlin.de. All rights reserved. 4 * 5 * Distributed under the terms of the MIT License. 6 */ 7 #ifndef _K_DISK_DEVICE_MANAGER_H 8 #define _K_DISK_DEVICE_MANAGER_H 9 10 11 #include "disk_device_manager.h" 12 #include "Locker.h" 13 14 15 namespace BPrivate { 16 namespace DiskDevice { 17 18 class KDiskDevice; 19 class KDiskDeviceJob; 20 class KDiskDeviceJobFactory; 21 class KDiskDeviceJobQueue; 22 class KDiskSystem; 23 class KFileDiskDevice; 24 class KPartition; 25 26 class KDiskDeviceManager { 27 public: 28 KDiskDeviceManager(); 29 ~KDiskDeviceManager(); 30 31 status_t InitCheck() const; 32 33 // Singleton Creation, Deletion, and Access 34 35 static status_t CreateDefault(); 36 static void DeleteDefault(); 37 static KDiskDeviceManager *Default(); 38 39 // Locking 40 41 bool Lock(); 42 void Unlock(); 43 44 // Disk Device / Partition Management 45 46 // manager must be locked 47 KDiskDevice *FindDevice(const char *path); 48 KDiskDevice *FindDevice(partition_id id, bool deviceOnly = true); 49 KPartition *FindPartition(const char *path, bool noShadow = false); 50 KPartition *FindPartition(partition_id id, bool noShadow = false); 51 KFileDiskDevice *FindFileDevice(const char *filePath); 52 53 KDiskDevice *RegisterDevice(const char *path); 54 KDiskDevice *RegisterDevice(partition_id id, bool deviceOnly = true); 55 KDiskDevice *RegisterNextDevice(int32 *cookie); 56 KPartition *RegisterPartition(const char *path, bool noShadow = false); 57 KPartition *RegisterPartition(partition_id id, bool noShadow = false); 58 KFileDiskDevice *RegisterFileDevice(const char *filePath); 59 60 KDiskDevice *ReadLockDevice(partition_id id, bool deviceOnly = true); 61 KDiskDevice *WriteLockDevice(partition_id id, bool deviceOnly = true); 62 // The device is also registered and must be unregistered by the 63 // caller. 64 KPartition *ReadLockPartition(partition_id id); 65 KPartition *WriteLockPartition(partition_id id); 66 // Both the device and the partition is also registered and must be 67 // unregistered by the caller. 68 69 partition_id CreateFileDevice(const char *filePath, 70 bool *newlyCreated = NULL, bool async = true); 71 status_t DeleteFileDevice(const char *filePath); 72 status_t DeleteFileDevice(partition_id id); 73 74 // manager must be locked 75 int32 CountDevices(); 76 KDiskDevice *NextDevice(int32 *cookie); 77 78 bool PartitionAdded(KPartition *partition); // implementation internal 79 bool PartitionRemoved(KPartition *partition); // 80 bool DeletePartition(KPartition *partition); // 81 82 // Jobs 83 84 // manager must be locked 85 KDiskDeviceJob *FindJob(disk_job_id id); 86 int32 CountJobs(); 87 KDiskDeviceJob *NextJob(int32 *cookie); 88 89 // manager must be locked 90 status_t AddJobQueue(KDiskDeviceJobQueue *jobQueue); 91 // the device must be write locked 92 status_t RemoveJobQueue(KDiskDeviceJobQueue *jobQueue); 93 status_t DeleteJobQueue(KDiskDeviceJobQueue *jobQueue); 94 // called when the execution is done 95 int32 CountJobQueues(); 96 KDiskDeviceJobQueue *NextJobQueue(int32 *cookie); 97 98 KDiskDeviceJobFactory *JobFactory() const; 99 100 // manager must *not* be locked 101 status_t UpdateBusyPartitions(KDiskDevice *device); 102 status_t UpdateJobStatus(KDiskDeviceJob *job, uint32 status, 103 bool updateBusyPartitions); 104 105 // Disk Systems 106 107 // manager must be locked 108 KDiskSystem *FindDiskSystem(const char *name); 109 KDiskSystem *FindDiskSystem(disk_system_id id); 110 int32 CountDiskSystems(); 111 KDiskSystem *NextDiskSystem(int32 *cookie); 112 113 KDiskSystem *LoadDiskSystem(const char *name); 114 KDiskSystem *LoadDiskSystem(disk_system_id id); 115 KDiskSystem *LoadNextDiskSystem(int32 *cookie); 116 117 // Watching 118 119 // TODO: Watching service for the kernel. The userland watching is handled 120 // by the registrar. 121 122 status_t InitialDeviceScan(); 123 status_t RescanDiskSystems(); 124 125 private: 126 status_t _RescanDiskSystems(bool fileSystems); 127 128 status_t _AddPartitioningSystem(const char *name); 129 status_t _AddFileSystem(const char *name); 130 status_t _AddDiskSystem(KDiskSystem *diskSystem); 131 132 bool _AddDevice(KDiskDevice *device); 133 bool _RemoveDevice(KDiskDevice *device); 134 135 bool _RemoveJobQueue(KDiskDeviceJobQueue *jobQueue); 136 137 status_t _UpdateBusyPartitions(KDiskDevice *device); 138 status_t _UpdateJobStatus(KDiskDeviceJob *job, uint32 status, 139 bool updateBusyPartitions); 140 141 status_t _Scan(const char *path); 142 status_t _ScanPartition(KPartition *partition, bool async); 143 // the manager must be locked and the device write locked 144 145 struct DeviceMap; 146 struct DiskSystemMap; 147 struct JobMap; 148 struct JobQueueVector; 149 struct PartitionMap; 150 struct PartitionSet; 151 152 BLocker fLock; 153 DeviceMap *fDevices; 154 PartitionMap *fPartitions; 155 DiskSystemMap *fDiskSystems; 156 PartitionSet *fObsoletePartitions; 157 JobMap *fJobs; 158 JobQueueVector *fJobQueues; 159 KDiskDeviceJobFactory *fJobFactory; 160 161 static KDiskDeviceManager *sDefaultManager; 162 }; 163 164 } // namespace DiskDevice 165 } // namespace BPrivate 166 167 using BPrivate::DiskDevice::KDiskDeviceManager; 168 169 #endif // _K_DISK_DEVICE_MANAGER_H 170