1/*
2 * Copyright 2004-2009, 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#include <Notifications.h>
14
15
16namespace BPrivate {
17namespace DiskDevice {
18
19class KDiskDevice;
20class KDiskSystem;
21class KFileDiskDevice;
22class KPartition;
23
24class KDiskDeviceManager {
25public:
26	KDiskDeviceManager();
27	~KDiskDeviceManager();
28
29	status_t InitCheck() const;
30
31	// Singleton Creation, Deletion, and Access
32
33	static status_t CreateDefault();
34	static void DeleteDefault();
35	static KDiskDeviceManager *Default();
36
37	// Locking
38
39	bool Lock();
40	void Unlock();
41
42	// Disk Device / Partition Management
43
44	DefaultUserNotificationService& Notifications();
45	void Notify(const KMessage& event, uint32 eventMask);
46
47	// manager must be locked
48	KDiskDevice *FindDevice(const char *path);
49	KDiskDevice *FindDevice(partition_id id, bool deviceOnly = true);
50	KPartition *FindPartition(const char *path);
51	KPartition *FindPartition(partition_id id);
52	KFileDiskDevice *FindFileDevice(const char *filePath);
53
54	KDiskDevice *RegisterDevice(const char *path);
55	KDiskDevice *RegisterDevice(partition_id id, bool deviceOnly = true);
56	KDiskDevice *RegisterNextDevice(int32 *cookie);
57	KPartition *RegisterPartition(const char *path);
58	KPartition *RegisterPartition(partition_id id);
59	KFileDiskDevice *RegisterFileDevice(const char *filePath);
60
61	KDiskDevice *ReadLockDevice(partition_id id, bool deviceOnly = true);
62	KDiskDevice *WriteLockDevice(partition_id id, bool deviceOnly = true);
63		// The device is also registered and must be unregistered by the
64		// caller.
65	KPartition *ReadLockPartition(partition_id id);
66	KPartition *WriteLockPartition(partition_id id);
67		// Both the device and the partition is also registered and must be
68		// unregistered by the caller.
69
70	status_t ScanPartition(KPartition* partition);
71
72	partition_id CreateDevice(const char *path, bool *newlyCreated = NULL);
73	status_t DeleteDevice(const char *path);
74
75	partition_id CreateFileDevice(const char* filePath,
76		bool* newlyCreated = NULL);
77	status_t DeleteFileDevice(const char *filePath);
78	status_t DeleteFileDevice(partition_id id);
79
80	// manager must be locked
81	int32 CountDevices();
82	KDiskDevice *NextDevice(int32 *cookie);
83
84	bool PartitionAdded(KPartition *partition);		// implementation internal
85	bool PartitionRemoved(KPartition *partition);	//
86	bool DeletePartition(KPartition *partition);	//
87
88	// Disk Systems
89
90	// manager must be locked
91	KDiskSystem *FindDiskSystem(const char *name, bool byPrettyName = false);
92	KDiskSystem *FindDiskSystem(disk_system_id id);
93	int32 CountDiskSystems();
94	KDiskSystem *NextDiskSystem(int32 *cookie);
95
96	KDiskSystem *LoadDiskSystem(const char *name, bool byPrettyName = false);
97	KDiskSystem *LoadDiskSystem(disk_system_id id);
98	KDiskSystem *LoadNextDiskSystem(int32 *cookie);
99
100	status_t InitialDeviceScan();
101	status_t RescanDiskSystems();
102	status_t StartMonitoring();
103
104private:
105	struct DeviceMap;
106	struct DiskSystemMap;
107	struct PartitionMap;
108	struct PartitionSet;
109	class DiskSystemWatcher;
110	class DeviceWatcher;
111	class DiskNotifications;
112
113	static status_t _CheckMediaStatusDaemon(void* self);
114	status_t _CheckMediaStatus();
115
116	status_t _RescanDiskSystems(DiskSystemMap& addedSystems, bool fileSystems);
117
118	status_t _AddPartitioningSystem(const char *name);
119	status_t _AddFileSystem(const char *name);
120	status_t _AddDiskSystem(KDiskSystem *diskSystem);
121
122	bool _AddDevice(KDiskDevice *device);
123	bool _RemoveDevice(KDiskDevice *device);
124
125	status_t _Scan(const char *path);
126	status_t _ScanPartition(KPartition *partition, bool async,
127		DiskSystemMap* restrictScan = NULL);
128		// the manager must be locked and the device write locked
129	status_t _ScanPartition(KPartition *partition,
130		DiskSystemMap* restrictScan);
131
132	status_t _AddRemoveMonitoring(const char *path, bool add);
133
134	void _NotifyDeviceEvent(KDiskDevice* device, int32 event, uint32 mask);
135
136	recursive_lock				fLock;
137	DeviceMap					*fDevices;
138	PartitionMap				*fPartitions;
139	DiskSystemMap				*fDiskSystems;
140	PartitionSet				*fObsoletePartitions;
141	thread_id					fMediaChecker;
142	volatile bool				fTerminating;
143	DiskSystemWatcher			*fDiskSystemWatcher;
144	DeviceWatcher				*fDeviceWatcher;
145	DiskNotifications*			fNotifications;
146
147	static KDiskDeviceManager	*sDefaultManager;
148};
149
150} // namespace DiskDevice
151} // namespace BPrivate
152
153using BPrivate::DiskDevice::KDiskDeviceManager;
154
155#endif	// _K_DISK_DEVICE_MANAGER_H
156