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