xref: /haiku/headers/private/kernel/disk_device_manager/KDiskDeviceManager.h (revision d9cebac2b77547b7064f22497514eecd2d047160)
1 /*
2  * Copyright 2004-2007, 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 	status_t ScanPartition(KPartition* partition, bool async);
70 
71 	partition_id CreateFileDevice(const char *filePath,
72 		bool *newlyCreated = NULL, bool async = true);
73 	status_t DeleteFileDevice(const char *filePath);
74 	status_t DeleteFileDevice(partition_id id);
75 
76 	// manager must be locked
77 	int32 CountDevices();
78 	KDiskDevice *NextDevice(int32 *cookie);
79 
80 	bool PartitionAdded(KPartition *partition);		// implementation internal
81 	bool PartitionRemoved(KPartition *partition);	//
82 	bool DeletePartition(KPartition *partition);	//
83 
84 	// Jobs
85 
86 	// manager must be locked
87 	KDiskDeviceJob *FindJob(disk_job_id id);
88 	int32 CountJobs();
89 	KDiskDeviceJob *NextJob(int32 *cookie);
90 
91 	// manager must be locked
92 	status_t AddJobQueue(KDiskDeviceJobQueue *jobQueue);
93 		// the device must be write locked
94 	status_t RemoveJobQueue(KDiskDeviceJobQueue *jobQueue);
95 	status_t DeleteJobQueue(KDiskDeviceJobQueue *jobQueue);
96 		// called when the execution is done
97 	int32 CountJobQueues();
98 	KDiskDeviceJobQueue *NextJobQueue(int32 *cookie);
99 
100 	KDiskDeviceJobFactory *JobFactory() const;
101 
102 	// manager must *not* be locked
103 	status_t UpdateBusyPartitions(KDiskDevice *device);
104 	status_t UpdateJobStatus(KDiskDeviceJob *job, uint32 status,
105 							 bool updateBusyPartitions);
106 
107 	// Disk Systems
108 
109 	// manager must be locked
110 	KDiskSystem *FindDiskSystem(const char *name);
111 	KDiskSystem *FindDiskSystem(disk_system_id id);
112 	int32 CountDiskSystems();
113 	KDiskSystem *NextDiskSystem(int32 *cookie);
114 
115 	KDiskSystem *LoadDiskSystem(const char *name);
116 	KDiskSystem *LoadDiskSystem(disk_system_id id);
117 	KDiskSystem *LoadNextDiskSystem(int32 *cookie);
118 
119 	// Watching
120 
121 	// TODO: Watching service for the kernel. The userland watching is handled
122 	// by the registrar.
123 
124 	status_t InitialDeviceScan();
125 	status_t RescanDiskSystems();
126 
127 private:
128 	static void _CheckMediaStatusDaemon(void* self, int iteration);
129 	void _CheckMediaStatus();
130 
131 	status_t _RescanDiskSystems(bool fileSystems);
132 
133 	status_t _AddPartitioningSystem(const char *name);
134 	status_t _AddFileSystem(const char *name);
135 	status_t _AddDiskSystem(KDiskSystem *diskSystem);
136 
137 	bool _AddDevice(KDiskDevice *device);
138 	bool _RemoveDevice(KDiskDevice *device);
139 
140 	bool _RemoveJobQueue(KDiskDeviceJobQueue *jobQueue);
141 
142 	status_t _UpdateBusyPartitions(KDiskDevice *device);
143 	status_t _UpdateJobStatus(KDiskDeviceJob *job, uint32 status,
144 							  bool updateBusyPartitions);
145 
146 	status_t _Scan(const char *path);
147 	status_t _ScanPartition(KPartition *partition, bool async);
148 		// the manager must be locked and the device write locked
149 
150 	struct DeviceMap;
151 	struct DiskSystemMap;
152 	struct JobMap;
153 	struct JobQueueVector;
154 	struct PartitionMap;
155 	struct PartitionSet;
156 
157 	BLocker						fLock;
158 	DeviceMap					*fDevices;
159 	PartitionMap				*fPartitions;
160 	DiskSystemMap				*fDiskSystems;
161 	PartitionSet				*fObsoletePartitions;
162 	JobMap						*fJobs;
163 	JobQueueVector				*fJobQueues;
164 	KDiskDeviceJobFactory		*fJobFactory;
165 
166 	static KDiskDeviceManager	*sDefaultManager;
167 };
168 
169 } // namespace DiskDevice
170 } // namespace BPrivate
171 
172 using BPrivate::DiskDevice::KDiskDeviceManager;
173 
174 #endif	// _K_DISK_DEVICE_MANAGER_H
175