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
29KDiskSystem::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
41KDiskSystem::~KDiskSystem()
42{
43	free(fName);
44	free(fShortName);
45	free(fPrettyName);
46}
47
48
49// Init
50status_t
51KDiskSystem::Init()
52{
53	return fName ? B_OK : B_NO_MEMORY;
54}
55
56
57// SetID
58/*void
59KDiskSystem::SetID(disk_system_id id)
60{
61	fID = id;
62}*/
63
64
65// ID
66disk_system_id
67KDiskSystem::ID() const
68{
69	return fID;
70}
71
72
73// Name
74const char *
75KDiskSystem::Name() const
76{
77	return fName;
78}
79
80
81// ShortName
82const char *
83KDiskSystem::ShortName() const
84{
85	return fShortName;
86}
87
88
89// PrettyName
90const char *
91KDiskSystem::PrettyName() const
92{
93	return fPrettyName;
94}
95
96
97// Flags
98uint32
99KDiskSystem::Flags() const
100{
101	return fFlags;
102}
103
104
105// IsFileSystem
106bool
107KDiskSystem::IsFileSystem() const
108{
109	return (fFlags & B_DISK_SYSTEM_IS_FILE_SYSTEM);
110}
111
112
113// IsPartitioningSystem
114bool
115KDiskSystem::IsPartitioningSystem() const
116{
117	return !(fFlags & B_DISK_SYSTEM_IS_FILE_SYSTEM);
118}
119
120
121// GetInfo
122void
123KDiskSystem::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
137status_t
138KDiskSystem::Load()
139{
140	ManagerLocker locker(KDiskDeviceManager::Default());
141TRACE("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
152void
153KDiskSystem::Unload()
154{
155	ManagerLocker locker(KDiskDeviceManager::Default());
156TRACE("KDiskSystem::Unload(): %s -> %ld\n", Name(), fLoadCounter - 1);
157	if (fLoadCounter > 0 && --fLoadCounter == 0)
158		UnloadModule();
159}
160
161
162// IsLoaded
163bool
164KDiskSystem::IsLoaded() const
165{
166	ManagerLocker locker(KDiskDeviceManager::Default());
167	return (fLoadCounter > 0);
168}
169
170
171// Identify
172float
173KDiskSystem::Identify(KPartition *partition, void **cookie)
174{
175	// to be implemented by derived classes
176	return -1;
177}
178
179
180// Scan
181status_t
182KDiskSystem::Scan(KPartition *partition, void *cookie)
183{
184	// to be implemented by derived classes
185	return B_ERROR;
186}
187
188
189// FreeIdentifyCookie
190void
191KDiskSystem::FreeIdentifyCookie(KPartition *partition, void *cookie)
192{
193	// to be implemented by derived classes
194}
195
196
197// FreeCookie
198void
199KDiskSystem::FreeCookie(KPartition *partition)
200{
201	// to be implemented by derived classes
202}
203
204
205// FreeContentCookie
206void
207KDiskSystem::FreeContentCookie(KPartition *partition)
208{
209	// to be implemented by derived classes
210}
211
212
213// Defragment
214status_t
215KDiskSystem::Defragment(KPartition* partition, disk_job_id job)
216{
217	// to be implemented by derived classes
218	return B_ERROR;
219}
220
221
222// Repair
223status_t
224KDiskSystem::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
232status_t
233KDiskSystem::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
241status_t
242KDiskSystem::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
250status_t
251KDiskSystem::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
259status_t
260KDiskSystem::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
268status_t
269KDiskSystem::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
277status_t
278KDiskSystem::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
287status_t
288KDiskSystem::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
296status_t
297KDiskSystem::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
306status_t
307KDiskSystem::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
316status_t
317KDiskSystem::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
325status_t
326KDiskSystem::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
334status_t
335KDiskSystem::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
345status_t
346KDiskSystem::DeleteChild(KPartition* child, disk_job_id job)
347{
348	// to be implemented by derived classes
349	return B_ERROR;
350}
351
352
353// LoadModule
354status_t
355KDiskSystem::LoadModule()
356{
357	// to be implemented by derived classes
358	return B_ERROR;
359}
360
361
362// UnloadModule
363void
364KDiskSystem::UnloadModule()
365{
366	// to be implemented by derived classes
367}
368
369
370// SetShortName
371status_t
372KDiskSystem::SetShortName(const char *name)
373{
374	return set_string(fShortName, name);
375}
376
377
378// SetPrettyName
379status_t
380KDiskSystem::SetPrettyName(const char *name)
381{
382	return set_string(fPrettyName, name);
383}
384
385
386// SetFlags
387void
388KDiskSystem::SetFlags(uint32 flags)
389{
390	fFlags = flags;
391}
392
393
394// _NextID
395int32
396KDiskSystem::_NextID()
397{
398	return atomic_add(&fNextID, 1);
399}
400
401
402// fNextID
403int32 KDiskSystem::fNextID = 0;
404
405