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 _AddPartitioningSystem(const char *name); 127 status_t _AddFileSystem(const char *name); 128 status_t _AddDiskSystem(KDiskSystem *diskSystem); 129 130 bool _AddDevice(KDiskDevice *device); 131 bool _RemoveDevice(KDiskDevice *device); 132 133 bool _RemoveJobQueue(KDiskDeviceJobQueue *jobQueue); 134 135 status_t _UpdateBusyPartitions(KDiskDevice *device); 136 status_t _UpdateJobStatus(KDiskDeviceJob *job, uint32 status, 137 bool updateBusyPartitions); 138 139 status_t _Scan(const char *path); 140 status_t _ScanPartition(KPartition *partition, bool async); 141 // the manager must be locked and the device write locked 142 143 struct DeviceMap; 144 struct DiskSystemMap; 145 struct JobMap; 146 struct JobQueueVector; 147 struct PartitionMap; 148 struct PartitionSet; 149 150 BLocker fLock; 151 DeviceMap *fDevices; 152 PartitionMap *fPartitions; 153 DiskSystemMap *fDiskSystems; 154 PartitionSet *fObsoletePartitions; 155 JobMap *fJobs; 156 JobQueueVector *fJobQueues; 157 KDiskDeviceJobFactory *fJobFactory; 158 159 static KDiskDeviceManager *sDefaultManager; 160 }; 161 162 } // namespace DiskDevice 163 } // namespace BPrivate 164 165 using BPrivate::DiskDevice::KDiskDeviceManager; 166 167 #endif // _K_DISK_DEVICE_MANAGER_H 168