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