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