xref: /haiku/headers/private/kernel/disk_device_manager/KPartition.h (revision 93aeb8c3bc3f13cb1f282e3e749258a23790d947)
1 // KPartition.h
2 
3 #ifndef _K_DISK_DEVICE_PARTITION_H
4 #define _K_DISK_DEVICE_PARTITION_H
5 
6 #include <disk_device_manager.h>
7 #include <Vector.h>
8 
9 struct user_partition_data;
10 
11 namespace BPrivate {
12 namespace DiskDevice {
13 
14 class UserDataWriter;
15 
16 class KDiskDevice;
17 class KDiskSystem;
18 class KPartitionListener;
19 class KPartitionVisitor;
20 class KPath;
21 class KPhysicalPartition;
22 class KShadowPartition;
23 
24 class KPartition {
25 public:
26 	KPartition(partition_id id = -1);
27 	virtual ~KPartition();
28 
29 	// Reference counting. As long as there's at least one referrer, the
30 	// object won't be deleted.
31 	// manager must be locked (Unregister() locks itself)
32 	void Register();
33 	void Unregister();
34 	int32 CountReferences() const;
35 
36 	void MarkObsolete();
37 		// called by the manager only
38 	bool IsObsolete() const;
39 
40 	virtual bool PrepareForRemoval();
41 	virtual bool PrepareForDeletion();
42 
43 	virtual status_t Open(int flags, int *fd);
44 	virtual status_t PublishDevice();
45 	virtual status_t UnpublishDevice();
46 
47 	void SetBusy(bool busy);
48 	bool IsBusy() const;
49 		// == jobs which may affect this partition are scheduled/in progress
50 	void SetDescendantBusy(bool busy);
51 	bool IsDescendantBusy() const;
52 		// == jobs which may affect a descendant of this partition are
53 		// scheduled/in progress; IsBusy() => IsDescendantBusy()
54 		// In the userland API, both can probably be mapped to one flag.
55 
56 	void SetOffset(off_t offset);
57 	off_t Offset() const;		// 0 for devices
58 
59 	void SetSize(off_t size);
60 	off_t Size() const;
61 
62 	void SetContentSize(off_t size);
63 	off_t ContentSize() const;
64 
65 	void SetBlockSize(uint32 blockSize);
66 	uint32 BlockSize() const;
67 
68 	void SetIndex(int32 index);
69 	int32 Index() const;		// 0 for devices
70 
71 	void SetStatus(uint32 status);
72 	uint32 Status() const;
73 	bool IsUninitialized() const;
74 
75 	void SetFlags(uint32 flags);	// comprises the ones below
76 	void AddFlags(uint32 flags);
77 	void ClearFlags(uint32 flags);
78 	uint32 Flags() const;
79 	bool ContainsFileSystem() const;
80 	bool ContainsPartitioningSystem() const;
81 	bool IsReadOnly() const;
82 	bool IsMounted() const;
83 
84 	bool IsDevice() const;
85 
86 	status_t SetName(const char *name);
87 	const char *Name() const;
88 
89 	status_t SetContentName(const char *name);
90 	const char *ContentName() const;
91 
92 	status_t SetType(const char *type);
93 	const char *Type() const;
94 
95 	const char *ContentType() const;
96 		// ContentType() == DiskSystem()->NamePretty()
97 
98 	// access to C style partition data
99 	partition_data *PartitionData();
100 	const partition_data *PartitionData() const;
101 
102 	virtual void SetID(partition_id id);
103 	partition_id ID() const;
104 
105 	virtual status_t GetPath(KPath *path) const;
106 		// no setter (see BDiskDevice) -- built on the fly
107 
108 	void SetVolumeID(dev_t volumeID);
109 	dev_t VolumeID() const;
110 
111 	void SetMountCookie(void *cookie);
112 	void *MountCookie() const;
113 
114 	virtual status_t Mount(uint32 mountFlags, const char *parameters);
115 	virtual status_t Unmount();
116 
117 	// Parameters
118 
119 	status_t SetParameters(const char *parameters);
120 	const char *Parameters() const;
121 
122 	status_t SetContentParameters(const char *parameters);
123 	const char *ContentParameters() const;
124 
125 	// Hierarchy
126 
127 	void SetDevice(KDiskDevice *device);
128 	KDiskDevice *Device() const;
129 
130 	void SetParent(KPartition *parent);
131 	KPartition *Parent() const;
132 
133 	status_t AddChild(KPartition *partition, int32 index = -1);
134 	virtual status_t CreateChild(partition_id id, int32 index,
135 								 KPartition **child = NULL) = 0;
136 	bool RemoveChild(int32 index);
137 	bool RemoveChild(KPartition *child);
138 	bool RemoveAllChildren();
139 	KPartition *ChildAt(int32 index) const;
140 	int32 CountChildren() const;
141 	int32 CountDescendants() const;
142 
143 	KPartition *VisitEachDescendant(KPartitionVisitor *visitor);
144 
145 	// Shadow Partition
146 
147 	virtual status_t CreateShadowPartition();	// creates a complete tree
148 	virtual void UnsetShadowPartition(bool doDelete);
149 	virtual KShadowPartition *ShadowPartition() const = 0;
150 	virtual bool IsShadowPartition() const = 0;
151 	virtual KPhysicalPartition *PhysicalPartition() const = 0;
152 
153 	// DiskSystem
154 
155 	void SetDiskSystem(KDiskSystem *diskSystem);
156 	KDiskSystem *DiskSystem() const;
157 	KDiskSystem *ParentDiskSystem() const;
158 		// When setting a disk system, it must already be loaded.
159 		// The partition will load it too, hence it won't be unloaded before
160 		// it is unset here.
161 
162 	void SetCookie(void *cookie);
163 	void *Cookie() const;
164 
165 	void SetContentCookie(void *cookie);
166 	void *ContentCookie() const;
167 
168 	// Listener Support
169 
170 	bool AddListener(KPartitionListener *listener);
171 	bool RemoveListener(KPartitionListener *listener);
172 
173 	// Change Tracking
174 
175 	void Changed(uint32 flags, uint32 clearFlags = 0);
176 	void SetChangeFlags(uint32 flags);
177 	uint32 ChangeFlags() const;
178 	int32 ChangeCounter() const;
179 	status_t UninitializeContents(bool logChanges = true);
180 
181 	void SetAlgorithmData(uint32 data);
182 	uint32 AlgorithmData() const;
183 		// temporary storage freely usable by algorithms
184 
185 	virtual void WriteUserData(UserDataWriter &writer,
186 							   user_partition_data *data);
187 
188 	virtual void Dump(bool deep, int32 level);
189 
190 protected:
191 	void FireOffsetChanged(off_t offset);
192 	void FireSizeChanged(off_t size);
193 	void FireContentSizeChanged(off_t size);
194 	void FireBlockSizeChanged(uint32 blockSize);
195 	void FireIndexChanged(int32 index);
196 	void FireStatusChanged(uint32 status);
197 	void FireFlagsChanged(uint32 flags);
198 	void FireNameChanged(const char *name);
199 	void FireContentNameChanged(const char *name);
200 	void FireTypeChanged(const char *type);
201 	void FireIDChanged(partition_id id);
202 	void FireVolumeIDChanged(dev_t volumeID);
203 	void FireMountCookieChanged(void *cookie);
204 	void FireParametersChanged(const char *parameters);
205 	void FireContentParametersChanged(const char *parameters);
206 	void FireChildAdded(KPartition *child, int32 index);
207 	void FireChildRemoved(KPartition *child, int32 index);
208 	void FireDiskSystemChanged(KDiskSystem *diskSystem);
209 	void FireCookieChanged(void *cookie);
210 	void FireContentCookieChanged(void *cookie);
211 
212 private:
213 	void _UpdateChildIndices(int32 index);
214 	static int32 _NextID();
215 
216 protected:
217 	typedef Vector<KPartition*> PartitionVector;
218 	struct ListenerSet;
219 
220 	partition_data		fPartitionData;
221 	PartitionVector		fChildren;
222 	KDiskDevice			*fDevice;
223 	KPartition			*fParent;
224 	KDiskSystem			*fDiskSystem;
225 	ListenerSet			*fListeners;
226 	uint32				fChangeFlags;
227 	int32				fChangeCounter;
228 	uint32				fAlgorithmData;
229 	int32				fReferenceCount;
230 	bool				fObsolete;
231 	static int32		fNextID;
232 };
233 
234 } // namespace DiskDevice
235 } // namespace BPrivate
236 
237 using BPrivate::DiskDevice::KPartition;
238 
239 #endif	// _K_DISK_DEVICE_PARTITION_H
240