xref: /haiku/headers/private/kernel/disk_device_manager/KPartition.h (revision 981f1b1135291a4fca290fbdf69910dc2f24abdd)
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 	bool IsPublished() const;
52 
53 	void SetBusy(bool busy);
54 	bool IsBusy() const;
55 	bool IsBusy(bool includeDescendants);
56 	bool CheckAndMarkBusy(bool includeDescendants);
57 	void MarkBusy(bool includeDescendants);
58 	void UnmarkBusy(bool includeDescendants);
59 
60 	void SetOffset(off_t offset);
61 	off_t Offset() const;		// 0 for devices
62 
63 	void SetSize(off_t size);
64 	off_t Size() const;
65 
66 	void SetContentSize(off_t size);
67 	off_t ContentSize() const;
68 
69 	void SetBlockSize(uint32 blockSize);
70 	uint32 BlockSize() const;
71 
72 	void SetIndex(int32 index);
73 	int32 Index() const;		// 0 for devices
74 
75 	void SetStatus(uint32 status);
76 	uint32 Status() const;
77 	bool IsUninitialized() const;
78 
79 	void SetFlags(uint32 flags);	// comprises the ones below
80 	void AddFlags(uint32 flags);
81 	void ClearFlags(uint32 flags);
82 	uint32 Flags() const;
83 	bool ContainsFileSystem() const;
84 	bool ContainsPartitioningSystem() const;
85 	bool IsReadOnly() const;
86 	bool IsMounted() const;
87 
88 	bool IsDevice() const;
89 
90 	status_t SetName(const char *name);
91 	const char *Name() const;
92 
93 	status_t SetContentName(const char *name);
94 	const char *ContentName() const;
95 
96 	status_t SetType(const char *type);
97 	const char *Type() const;
98 
99 	const char *ContentType() const;
100 		// ContentType() == DiskSystem()->NamePretty()
101 
102 	// access to C style partition data
103 	partition_data *PartitionData();
104 	const partition_data *PartitionData() const;
105 
106 	virtual void SetID(partition_id id);
107 	partition_id ID() const;
108 
109 	virtual status_t GetPath(KPath *path) const;
110 		// no setter (see BDiskDevice) -- built on the fly
111 
112 	void SetVolumeID(dev_t volumeID);
113 	dev_t VolumeID() const;
114 
115 	void SetMountCookie(void *cookie);
116 	void *MountCookie() const;
117 
118 	virtual status_t Mount(uint32 mountFlags, const char *parameters);
119 	virtual status_t Unmount();
120 
121 	// Parameters
122 
123 	status_t SetParameters(const char *parameters);
124 	const char *Parameters() const;
125 
126 	status_t SetContentParameters(const char *parameters);
127 	const char *ContentParameters() const;
128 
129 	// Hierarchy
130 
131 	void SetDevice(KDiskDevice *device);
132 	KDiskDevice *Device() const;
133 
134 	void SetParent(KPartition *parent);
135 	KPartition *Parent() const;
136 
137 	status_t AddChild(KPartition *partition, int32 index = -1);
138 	status_t CreateChild(partition_id id, int32 index, off_t offset, off_t size,
139 		KPartition **child = NULL);
140 	bool RemoveChild(int32 index);
141 	bool RemoveChild(KPartition *child);
142 	bool RemoveAllChildren();
143 	KPartition *ChildAt(int32 index) const;
144 	int32 CountChildren() const;
145 	int32 CountDescendants() const;
146 
147 	KPartition *VisitEachDescendant(KPartitionVisitor *visitor);
148 
149 	// DiskSystem
150 
151 	void SetDiskSystem(KDiskSystem *diskSystem, float priority = 0.0f);
152 	KDiskSystem *DiskSystem() const;
153 	float DiskSystemPriority() const;
154 	KDiskSystem *ParentDiskSystem() const;
155 		// When setting a disk system, it must already be loaded.
156 		// The partition will load it too, hence it won't be unloaded before
157 		// it is unset here.
158 
159 	void SetCookie(void *cookie);
160 	void *Cookie() const;
161 
162 	void SetContentCookie(void *cookie);
163 	void *ContentCookie() const;
164 
165 	// Listener Support
166 
167 	bool AddListener(KPartitionListener *listener);
168 	bool RemoveListener(KPartitionListener *listener);
169 
170 	// Change Tracking
171 
172 	void Changed(uint32 flags, uint32 clearFlags = 0);
173 	void SetChangeFlags(uint32 flags);
174 	uint32 ChangeFlags() const;
175 	int32 ChangeCounter() const;
176 	status_t UninitializeContents(bool logChanges = true);
177 
178 	void SetAlgorithmData(uint32 data);
179 	uint32 AlgorithmData() const;
180 		// temporary storage freely usable by algorithms
181 
182 	virtual void WriteUserData(UserDataWriter &writer,
183 							   user_partition_data *data);
184 
185 	virtual void Dump(bool deep, int32 level);
186 
187 protected:
188 	void FireOffsetChanged(off_t offset);
189 	void FireSizeChanged(off_t size);
190 	void FireContentSizeChanged(off_t size);
191 	void FireBlockSizeChanged(uint32 blockSize);
192 	void FireIndexChanged(int32 index);
193 	void FireStatusChanged(uint32 status);
194 	void FireFlagsChanged(uint32 flags);
195 	void FireNameChanged(const char *name);
196 	void FireContentNameChanged(const char *name);
197 	void FireTypeChanged(const char *type);
198 	void FireIDChanged(partition_id id);
199 	void FireVolumeIDChanged(dev_t volumeID);
200 	void FireMountCookieChanged(void *cookie);
201 	void FireParametersChanged(const char *parameters);
202 	void FireContentParametersChanged(const char *parameters);
203 	void FireChildAdded(KPartition *child, int32 index);
204 	void FireChildRemoved(KPartition *child, int32 index);
205 	void FireDiskSystemChanged(KDiskSystem *diskSystem);
206 	void FireCookieChanged(void *cookie);
207 	void FireContentCookieChanged(void *cookie);
208 
209 private:
210 	void _UpdateChildIndices(int32 index);
211 	static int32 _NextID();
212 
213 protected:
214 	typedef Vector<KPartition*> PartitionVector;
215 	struct ListenerSet;
216 
217 	partition_data		fPartitionData;
218 	PartitionVector		fChildren;
219 	KDiskDevice			*fDevice;
220 	KPartition			*fParent;
221 	KDiskSystem			*fDiskSystem;
222 	float				fDiskSystemPriority;
223 	ListenerSet			*fListeners;
224 	uint32				fChangeFlags;
225 	int32				fChangeCounter;
226 	uint32				fAlgorithmData;
227 	int32				fReferenceCount;
228 	bool				fObsolete;
229 	bool				fPublished;
230 	static int32		fNextID;
231 };
232 
233 } // namespace DiskDevice
234 } // namespace BPrivate
235 
236 using BPrivate::DiskDevice::KPartition;
237 
238 #endif	// _K_DISK_DEVICE_PARTITION_H
239