xref: /haiku/headers/private/kernel/disk_device_manager/KDiskDevice.h (revision 4f00613311d0bd6b70fa82ce19931c41f071ea4e)
1 // KDiskDevice.h
2 
3 #ifndef _K_DISK_DEVICE_H
4 #define _K_DISK_DEVICE_H
5 
6 #include <OS.h>
7 
8 #include "KPhysicalPartition.h"
9 #include "RWLocker.h"
10 
11 namespace BPrivate {
12 namespace DiskDevice {
13 
14 class UserDataWriter;
15 
16 class KDiskDevice : public KPhysicalPartition {
17 public:
18 	KDiskDevice(partition_id id = -1);
19 	virtual ~KDiskDevice();
20 
21 	status_t SetTo(const char *path);
22 	void Unset();
23 	virtual status_t InitCheck() const;
24 		// TODO: probably superfluous
25 
26 	// A read lock owner can be sure that the device (incl. all of its
27 	// partitions won't be changed).
28 	// A write lock owner is moreover allowed to make changes.
29 	// The hierarchy is additionally protected by the disk device manager's
30 	// lock -- only a device write lock owner is allowed to change it, but
31 	// manager lock owners can be sure, that it won't change.
32 	bool ReadLock();
33 	void ReadUnlock();
34 	bool IsReadLocked(bool orWriteLocked = true);
35 	bool WriteLock();
36 	void WriteUnlock();
37 	bool IsWriteLocked();
38 
39 	virtual bool PrepareForRemoval();
40 
41 	virtual void SetID(partition_id id);
42 
43 	virtual status_t PublishDevice();
44 	virtual status_t UnpublishDevice();
45 
46 	void SetDeviceFlags(uint32 flags);	// comprises the ones below
47 	uint32 DeviceFlags() const;
48 	bool IsReadOnlyMedia() const;
49 	bool IsWriteOnce() const;
50 	bool IsRemovable() const;
51 	bool HasMedia() const;
52 
53 	status_t SetPath(const char *path);
54 		// TODO: Remove this method or make it private. Once initialized the
55 		// path must not be changed.
56 	const char *Path() const;
57 	virtual status_t GetPath(KPath *path) const;
58 
59 	// File descriptor: Set only from a kernel thread, valid only for
60 	// kernel threads.
61 	void SetFD(int fd);
62 	int FD() const;
63 
64 	// access to C style device data
65 	disk_device_data *DeviceData();
66 	const disk_device_data *DeviceData() const;
67 
68 	status_t CreateShadowDevice(team_id team);
69 	status_t DeleteShadowDevice();
70 	void SetShadowOwner(team_id team);
71 	team_id ShadowOwner() const;
72 
73 	virtual void WriteUserData(UserDataWriter &writer,
74 		user_partition_data *data);
75 	void WriteUserData(UserDataWriter &writer, bool shadow);
76 
77 	virtual void Dump(bool deep = true, int32 level = 0);
78 
79 protected:
80 	virtual status_t GetMediaStatus(status_t *mediaStatus);
81 	virtual status_t GetGeometry(device_geometry *geometry);
82 
83 private:
84 	void _InitPartitionData();
85 
86 	disk_device_data	fDeviceData;
87 	RWLocker			fLocker;
88 	int					fFD;
89 	status_t			fMediaStatus;
90 	team_id				fShadowOwner;
91 };
92 
93 } // namespace DiskDevice
94 } // namespace BPrivate
95 
96 using BPrivate::DiskDevice::KDiskDevice;
97 
98 #endif	// _K_DISK_DEVICE_H
99