xref: /haiku/src/system/kernel/disk_device_manager/KDiskSystem.cpp (revision 71452e98334eaac603bf542d159e24788a46bebb)
1 /*
2  * Copyright 2003-2011, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <stdio.h>
8 #include <stdlib.h>
9 
10 #include <KernelExport.h>
11 #include <util/kernel_cpp.h>
12 
13 #include "ddm_userland_interface.h"
14 #include "KDiskDeviceManager.h"
15 #include "KDiskDeviceUtils.h"
16 #include "KDiskSystem.h"
17 
18 
19 // debugging
20 //#define TRACE_KDISK_SYSTEM
21 #ifdef TRACE_KDISK_SYSTEM
22 #	define TRACE(x...) dprintf(x)
23 #else
24 #	define TRACE(x...)	do { } while (false)
25 #endif
26 
27 
28 // constructor
29 KDiskSystem::KDiskSystem(const char *name)
30 	: fID(_NextID()),
31 	  fName(NULL),
32 	  fShortName(NULL),
33 	  fPrettyName(NULL),
34 	  fLoadCounter(0)
35 {
36 	set_string(fName, name);
37 }
38 
39 
40 // destructor
41 KDiskSystem::~KDiskSystem()
42 {
43 	free(fName);
44 	free(fShortName);
45 	free(fPrettyName);
46 }
47 
48 
49 // Init
50 status_t
51 KDiskSystem::Init()
52 {
53 	return fName ? B_OK : B_NO_MEMORY;
54 }
55 
56 
57 // SetID
58 /*void
59 KDiskSystem::SetID(disk_system_id id)
60 {
61 	fID = id;
62 }*/
63 
64 
65 // ID
66 disk_system_id
67 KDiskSystem::ID() const
68 {
69 	return fID;
70 }
71 
72 
73 // Name
74 const char *
75 KDiskSystem::Name() const
76 {
77 	return fName;
78 }
79 
80 
81 // ShortName
82 const char *
83 KDiskSystem::ShortName() const
84 {
85 	return fShortName;
86 }
87 
88 
89 // PrettyName
90 const char *
91 KDiskSystem::PrettyName() const
92 {
93 	return fPrettyName;
94 }
95 
96 
97 // Flags
98 uint32
99 KDiskSystem::Flags() const
100 {
101 	return fFlags;
102 }
103 
104 
105 // IsFileSystem
106 bool
107 KDiskSystem::IsFileSystem() const
108 {
109 	return (fFlags & B_DISK_SYSTEM_IS_FILE_SYSTEM);
110 }
111 
112 
113 // IsPartitioningSystem
114 bool
115 KDiskSystem::IsPartitioningSystem() const
116 {
117 	return !(fFlags & B_DISK_SYSTEM_IS_FILE_SYSTEM);
118 }
119 
120 
121 // GetInfo
122 void
123 KDiskSystem::GetInfo(user_disk_system_info *info)
124 {
125 	if (!info)
126 		return;
127 
128 	info->id = ID();
129 	strlcpy(info->name, Name(), sizeof(info->name));
130 	strlcpy(info->short_name, ShortName(), sizeof(info->short_name));
131 	strlcpy(info->pretty_name, PrettyName(), sizeof(info->pretty_name));
132 	info->flags = Flags();
133 }
134 
135 
136 // Load
137 status_t
138 KDiskSystem::Load()
139 {
140 	ManagerLocker locker(KDiskDeviceManager::Default());
141 TRACE("KDiskSystem::Load(): %s -> %ld\n", Name(), fLoadCounter + 1);
142 	status_t error = B_OK;
143 	if (fLoadCounter == 0)
144 		error = LoadModule();
145 	if (error == B_OK)
146 		fLoadCounter++;
147 	return error;
148 }
149 
150 
151 // Unload
152 void
153 KDiskSystem::Unload()
154 {
155 	ManagerLocker locker(KDiskDeviceManager::Default());
156 TRACE("KDiskSystem::Unload(): %s -> %ld\n", Name(), fLoadCounter - 1);
157 	if (fLoadCounter > 0 && --fLoadCounter == 0)
158 		UnloadModule();
159 }
160 
161 
162 // IsLoaded
163 bool
164 KDiskSystem::IsLoaded() const
165 {
166 	ManagerLocker locker(KDiskDeviceManager::Default());
167 	return (fLoadCounter > 0);
168 }
169 
170 
171 // Identify
172 float
173 KDiskSystem::Identify(KPartition *partition, void **cookie)
174 {
175 	// to be implemented by derived classes
176 	return -1;
177 }
178 
179 
180 // Scan
181 status_t
182 KDiskSystem::Scan(KPartition *partition, void *cookie)
183 {
184 	// to be implemented by derived classes
185 	return B_ERROR;
186 }
187 
188 
189 // FreeIdentifyCookie
190 void
191 KDiskSystem::FreeIdentifyCookie(KPartition *partition, void *cookie)
192 {
193 	// to be implemented by derived classes
194 }
195 
196 
197 // FreeCookie
198 void
199 KDiskSystem::FreeCookie(KPartition *partition)
200 {
201 	// to be implemented by derived classes
202 }
203 
204 
205 // FreeContentCookie
206 void
207 KDiskSystem::FreeContentCookie(KPartition *partition)
208 {
209 	// to be implemented by derived classes
210 }
211 
212 
213 // Defragment
214 status_t
215 KDiskSystem::Defragment(KPartition* partition, disk_job_id job)
216 {
217 	// to be implemented by derived classes
218 	return B_ERROR;
219 }
220 
221 
222 // Repair
223 status_t
224 KDiskSystem::Repair(KPartition* partition, bool checkOnly, disk_job_id job)
225 {
226 	// to be implemented by derived classes
227 	return B_ERROR;
228 }
229 
230 
231 // Resize
232 status_t
233 KDiskSystem::Resize(KPartition* partition, off_t size, disk_job_id job)
234 {
235 	// to be implemented by derived classes
236 	return B_ERROR;
237 }
238 
239 
240 // ResizeChild
241 status_t
242 KDiskSystem::ResizeChild(KPartition* child, off_t size, disk_job_id job)
243 {
244 	// to be implemented by derived classes
245 	return B_ERROR;
246 }
247 
248 
249 // Move
250 status_t
251 KDiskSystem::Move(KPartition* partition, off_t offset, disk_job_id job)
252 {
253 	// to be implemented by derived classes
254 	return B_ERROR;
255 }
256 
257 
258 // MoveChild
259 status_t
260 KDiskSystem::MoveChild(KPartition* child, off_t offset, disk_job_id job)
261 {
262 	// to be implemented by derived classes
263 	return B_ERROR;
264 }
265 
266 
267 // SetName
268 status_t
269 KDiskSystem::SetName(KPartition* partition, const char* name, disk_job_id job)
270 {
271 	// to be implemented by derived classes
272 	return B_ERROR;
273 }
274 
275 
276 // SetContentName
277 status_t
278 KDiskSystem::SetContentName(KPartition* partition, const char* name,
279 	disk_job_id job)
280 {
281 	// to be implemented by derived classes
282 	return B_ERROR;
283 }
284 
285 
286 // SetType
287 status_t
288 KDiskSystem::SetType(KPartition* partition, const char *type, disk_job_id job)
289 {
290 	// to be implemented by derived classes
291 	return B_ERROR;
292 }
293 
294 
295 // SetParameters
296 status_t
297 KDiskSystem::SetParameters(KPartition* partition, const char* parameters,
298 	disk_job_id job)
299 {
300 	// to be implemented by derived classes
301 	return B_ERROR;
302 }
303 
304 
305 // SetContentParameters
306 status_t
307 KDiskSystem::SetContentParameters(KPartition* partition,
308 	const char* parameters, disk_job_id job)
309 {
310 	// to be implemented by derived classes
311 	return B_ERROR;
312 }
313 
314 
315 // Initialize
316 status_t
317 KDiskSystem::Initialize(KPartition* partition, const char* name,
318 	const char* parameters, disk_job_id job)
319 {
320 	// to be implemented by derived classes
321 	return B_ERROR;
322 }
323 
324 
325 status_t
326 KDiskSystem::Uninitialize(KPartition* partition, disk_job_id job)
327 {
328 	// to be implemented by derived classes
329 	return B_NOT_SUPPORTED;
330 }
331 
332 
333 // CreateChild
334 status_t
335 KDiskSystem::CreateChild(KPartition* partition, off_t offset, off_t size,
336 	const char* type, const char* name, const char* parameters, disk_job_id job,
337 	KPartition **child, partition_id childID)
338 {
339 	// to be implemented by derived classes
340 	return B_ERROR;
341 }
342 
343 
344 // DeleteChild
345 status_t
346 KDiskSystem::DeleteChild(KPartition* child, disk_job_id job)
347 {
348 	// to be implemented by derived classes
349 	return B_ERROR;
350 }
351 
352 
353 // LoadModule
354 status_t
355 KDiskSystem::LoadModule()
356 {
357 	// to be implemented by derived classes
358 	return B_ERROR;
359 }
360 
361 
362 // UnloadModule
363 void
364 KDiskSystem::UnloadModule()
365 {
366 	// to be implemented by derived classes
367 }
368 
369 
370 // SetShortName
371 status_t
372 KDiskSystem::SetShortName(const char *name)
373 {
374 	return set_string(fShortName, name);
375 }
376 
377 
378 // SetPrettyName
379 status_t
380 KDiskSystem::SetPrettyName(const char *name)
381 {
382 	return set_string(fPrettyName, name);
383 }
384 
385 
386 // SetFlags
387 void
388 KDiskSystem::SetFlags(uint32 flags)
389 {
390 	fFlags = flags;
391 }
392 
393 
394 // _NextID
395 int32
396 KDiskSystem::_NextID()
397 {
398 	return atomic_add(&fNextID, 1);
399 }
400 
401 
402 // fNextID
403 int32 KDiskSystem::fNextID = 0;
404 
405