xref: /haiku/headers/private/kernel/disk_device_manager/KPartition.h (revision 4a55cc230cf7566cadcbb23b1928eefff8aea9a2)
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 	void SetVolumeID(dev_t volumeID);
119 	dev_t VolumeID() const;
120 
121 	void SetMountCookie(void *cookie);
122 	void *MountCookie() const;
123 
124 	virtual status_t Mount(uint32 mountFlags, const char *parameters);
125 	virtual status_t Unmount();
126 
127 	// Parameters
128 
129 	status_t SetParameters(const char *parameters);
130 	const char *Parameters() const;
131 
132 	status_t SetContentParameters(const char *parameters);
133 	const char *ContentParameters() const;
134 
135 	// Hierarchy
136 
137 	void SetDevice(KDiskDevice *device);
138 	KDiskDevice *Device() const;
139 
140 	void SetParent(KPartition *parent);
141 	KPartition *Parent() const;
142 
143 	status_t AddChild(KPartition *partition, int32 index = -1);
144 	status_t CreateChild(partition_id id, int32 index, off_t offset, off_t size,
145 		KPartition **child = NULL);
146 	bool RemoveChild(int32 index);
147 	bool RemoveChild(KPartition *child);
148 	bool RemoveAllChildren();
149 	KPartition *ChildAt(int32 index) const;
150 	int32 CountChildren() const;
151 	int32 CountDescendants() const;
152 
153 	KPartition *VisitEachDescendant(KPartitionVisitor *visitor);
154 
155 	// DiskSystem
156 
157 	void SetDiskSystem(KDiskSystem *diskSystem, float priority = 0.0f);
158 	KDiskSystem *DiskSystem() const;
159 	float DiskSystemPriority() const;
160 	KDiskSystem *ParentDiskSystem() const;
161 		// When setting a disk system, it must already be loaded.
162 		// The partition will load it too, hence it won't be unloaded before
163 		// it is unset here.
164 
165 	void SetCookie(void *cookie);
166 	void *Cookie() const;
167 
168 	void SetContentCookie(void *cookie);
169 	void *ContentCookie() const;
170 
171 	// Listener Support
172 
173 	bool AddListener(KPartitionListener *listener);
174 	bool RemoveListener(KPartitionListener *listener);
175 
176 	// Change Tracking
177 
178 	void Changed(uint32 flags, uint32 clearFlags = 0);
179 	void SetChangeFlags(uint32 flags);
180 	uint32 ChangeFlags() const;
181 	int32 ChangeCounter() const;
182 	status_t UninitializeContents(bool logChanges = true);
183 
184 	void SetAlgorithmData(uint32 data);
185 	uint32 AlgorithmData() const;
186 		// temporary storage freely usable by algorithms
187 
188 	virtual void WriteUserData(UserDataWriter &writer,
189 							   user_partition_data *data);
190 
191 	virtual void Dump(bool deep, int32 level);
192 
193 protected:
194 	void FireOffsetChanged(off_t offset);
195 	void FireSizeChanged(off_t size);
196 	void FireContentSizeChanged(off_t size);
197 	void FireBlockSizeChanged(uint32 blockSize);
198 	void FireIndexChanged(int32 index);
199 	void FireStatusChanged(uint32 status);
200 	void FireFlagsChanged(uint32 flags);
201 	void FireNameChanged(const char *name);
202 	void FireContentNameChanged(const char *name);
203 	void FireTypeChanged(const char *type);
204 	void FireIDChanged(partition_id id);
205 	void FireVolumeIDChanged(dev_t volumeID);
206 	void FireMountCookieChanged(void *cookie);
207 	void FireParametersChanged(const char *parameters);
208 	void FireContentParametersChanged(const char *parameters);
209 	void FireChildAdded(KPartition *child, int32 index);
210 	void FireChildRemoved(KPartition *child, int32 index);
211 	void FireDiskSystemChanged(KDiskSystem *diskSystem);
212 	void FireCookieChanged(void *cookie);
213 	void FireContentCookieChanged(void *cookie);
214 
215 private:
216 	void _UpdateChildIndices(int32 start, int32 end);
217 	static int32 _NextID();
218 
219 protected:
220 	typedef Vector<KPartition*> PartitionVector;
221 	struct ListenerSet;
222 
223 	partition_data		fPartitionData;
224 	PartitionVector		fChildren;
225 	KDiskDevice			*fDevice;
226 	KPartition			*fParent;
227 	KDiskSystem			*fDiskSystem;
228 	float				fDiskSystemPriority;
229 	ListenerSet			*fListeners;
230 	uint32				fChangeFlags;
231 	int32				fChangeCounter;
232 	uint32				fAlgorithmData;
233 	int32				fReferenceCount;
234 	bool				fObsolete;
235 	char				*fPublishedName;
236 	static int32		sNextID;
237 };
238 
239 } // namespace DiskDevice
240 } // namespace BPrivate
241 
242 using BPrivate::DiskDevice::KPartition;
243 
244 #endif	// _K_DISK_DEVICE_PARTITION_H
245