xref: /haiku/src/add-ons/kernel/drivers/disk/virtual/remote_disk/remote_disk.cpp (revision 4a55cc230cf7566cadcbb23b1928eefff8aea9a2)
1 /*
2  * Copyright 2007, Ingo Weinhold <bonefish@cs.tu-berlin.de>.
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 
6 #include <string.h>
7 
8 #include <KernelExport.h>
9 #include <Drivers.h>
10 
11 #include <lock.h>
12 #include <util/AutoLock.h>
13 #include <util/kernel_cpp.h>
14 
15 #include "RemoteDisk.h"
16 
17 
18 //#define TRACE_REMOTE_DISK
19 #ifdef TRACE_REMOTE_DISK
20 #	define TRACE(x) dprintf x
21 #else
22 #	define TRACE(x) do {} while (false)
23 #endif
24 
25 
26 const bigtime_t kInitRetryDelay	= 10 * 1000000LL;	// 10 s
27 
28 enum {
29 	MAX_REMOTE_DISKS	= 1
30 };
31 
32 
33 struct RemoteDiskDevice : recursive_lock {
34 	RemoteDisk*		remoteDisk;
35 	bigtime_t		lastInitRetryTime;
36 
37 	RemoteDiskDevice()
38 		:
39 		remoteDisk(NULL),
40 		lastInitRetryTime(-1)
41 	{
42 	}
43 
44 	~RemoteDiskDevice()
45 	{
46 		delete remoteDisk;
47 		Uninit();
48 	}
49 
50 	status_t Init()
51 	{
52 		recursive_lock_init(this, "remote disk device");
53 		return B_OK;
54 	}
55 
56 	void Uninit()
57 	{
58 		recursive_lock_destroy(this);
59 	}
60 
61 	status_t LazyInitDisk()
62 	{
63 		if (remoteDisk)
64 			return B_OK;
65 
66 		// don't try to init, if the last attempt wasn't long enough ago
67 		if (lastInitRetryTime >= 0
68 			&& system_time() < lastInitRetryTime + kInitRetryDelay) {
69 			return B_ERROR;
70 		}
71 
72 		// create the object
73 		remoteDisk = new(nothrow) RemoteDisk;
74 		if (!remoteDisk) {
75 			lastInitRetryTime = system_time();
76 			return B_NO_MEMORY;
77 		}
78 
79 		// find a server
80 		TRACE(("remote_disk: FindAnyRemoteDisk()\n"));
81 		status_t error = remoteDisk->FindAnyRemoteDisk();
82 		if (error != B_OK) {
83 			delete remoteDisk;
84 			remoteDisk = NULL;
85 			lastInitRetryTime = system_time();
86 			return B_NO_MEMORY;
87 		}
88 
89 		return B_OK;
90 	}
91 
92 	void GetGeometry(device_geometry* geometry, bool bios)
93 	{
94 		// TODO: Respect "bios" argument!
95 		geometry->bytes_per_sector = REMOTE_DISK_BLOCK_SIZE;
96 		geometry->sectors_per_track = 1;
97 		geometry->cylinder_count = remoteDisk->Size() / REMOTE_DISK_BLOCK_SIZE;
98 		geometry->head_count = 1;
99 		geometry->device_type = B_DISK;
100 		geometry->removable = true;
101 		geometry->read_only = remoteDisk->IsReadOnly();
102 		geometry->write_once = false;
103 		geometry->bytes_per_physical_sector = REMOTE_DISK_BLOCK_SIZE;
104 	}
105 };
106 
107 typedef RecursiveLocker DeviceLocker;
108 
109 
110 static const char* kPublishedNames[] = {
111 	"disk/virtual/remote_disk/0/raw",
112 //	"misc/remote_disk_control",
113 	NULL
114 };
115 
116 static RemoteDiskDevice* sDevices;
117 
118 
119 // #pragma mark - internal functions
120 
121 
122 // device_for_name
123 static RemoteDiskDevice*
124 device_for_name(const char* name)
125 {
126 	for (int i = 0; i < MAX_REMOTE_DISKS; i++) {
127 		if (strcmp(name, kPublishedNames[i]) == 0)
128 			return sDevices + i;
129 	}
130 	return NULL;
131 }
132 
133 
134 // #pragma mark - data device hooks
135 
136 
137 static status_t
138 remote_disk_open(const char* name, uint32 flags, void** cookie)
139 {
140 	RemoteDiskDevice* device = device_for_name(name);
141 	TRACE(("remote_disk_open(\"%s\") -> %p\n", name, device));
142 	if (!device)
143 		return B_BAD_VALUE;
144 
145 	DeviceLocker locker(device);
146 	status_t error = device->LazyInitDisk();
147 	if (error != B_OK)
148 		return error;
149 
150 	*cookie = device;
151 
152 	return B_OK;
153 }
154 
155 
156 static status_t
157 remote_disk_close(void* cookie)
158 {
159 	TRACE(("remote_disk_close(%p)\n", cookie));
160 
161 	// nothing to do
162 	return B_OK;
163 }
164 
165 
166 static status_t
167 remote_disk_read(void* cookie, off_t position, void* buffer, size_t* numBytes)
168 {
169 	TRACE(("remote_disk_read(%p, %lld, %p, %lu)\n", cookie, position, buffer,
170 		*numBytes));
171 
172 	RemoteDiskDevice* device = (RemoteDiskDevice*)cookie;
173 	DeviceLocker locker(device);
174 
175 	ssize_t bytesRead = device->remoteDisk->ReadAt(position, buffer, *numBytes);
176 	if (bytesRead < 0) {
177 		*numBytes = 0;
178 		TRACE(("remote_disk_read() failed: %s\n", strerror(bytesRead)));
179 		return bytesRead;
180 	}
181 
182 	*numBytes = bytesRead;
183 	TRACE(("remote_disk_read() done: %ld\n", bytesRead));
184 	return B_OK;
185 }
186 
187 
188 static status_t
189 remote_disk_write(void* cookie, off_t position, const void* buffer,
190 	size_t* numBytes)
191 {
192 	TRACE(("remote_disk_write(%p, %lld, %p, %lu)\n", cookie, position, buffer,
193 		*numBytes));
194 
195 	RemoteDiskDevice* device = (RemoteDiskDevice*)cookie;
196 	DeviceLocker locker(device);
197 
198 	ssize_t bytesWritten = device->remoteDisk->WriteAt(position, buffer,
199 		*numBytes);
200 	if (bytesWritten < 0) {
201 		*numBytes = 0;
202 		TRACE(("remote_disk_write() failed: %s\n", strerror(bytesRead)));
203 		return bytesWritten;
204 	}
205 
206 	*numBytes = bytesWritten;
207 	TRACE(("remote_disk_written() done: %ld\n", bytesWritten));
208 	return B_OK;
209 }
210 
211 
212 static status_t
213 remote_disk_control(void* cookie, uint32 op, void* arg, size_t len)
214 {
215 	TRACE(("remote_disk_control(%p, %lu, %p, %lu)\n", cookie, op, arg, len));
216 
217 	RemoteDiskDevice* device = (RemoteDiskDevice*)cookie;
218 	DeviceLocker locker(device);
219 
220 	// used data device
221 	switch (op) {
222 		case B_GET_DEVICE_SIZE:
223 			TRACE(("remote_disk: B_GET_DEVICE_SIZE\n"));
224 			*(size_t*)arg = device->remoteDisk->Size();
225 			return B_OK;
226 
227 		case B_SET_NONBLOCKING_IO:
228 			TRACE(("remote_disk: B_SET_NONBLOCKING_IO\n"));
229 			return B_OK;
230 
231 		case B_SET_BLOCKING_IO:
232 			TRACE(("remote_disk: B_SET_BLOCKING_IO\n"));
233 			return B_OK;
234 
235 		case B_GET_READ_STATUS:
236 			TRACE(("remote_disk: B_GET_READ_STATUS\n"));
237 			*(bool*)arg = true;
238 			return B_OK;
239 
240 		case B_GET_WRITE_STATUS:
241 			TRACE(("remote_disk: B_GET_WRITE_STATUS\n"));
242 			*(bool*)arg = true;
243 			return B_OK;
244 
245 		case B_GET_ICON:
246 		{
247 			TRACE(("remote_disk: B_GET_ICON\n"));
248 			return B_BAD_VALUE;
249 		}
250 
251 		case B_GET_BIOS_GEOMETRY:
252 		case B_GET_GEOMETRY:
253 			TRACE(("remote_disk: %s\n",
254 				op == B_GET_BIOS_GEOMETRY ? "B_GET_BIOS_GEOMETRY" : "B_GET_GEOMETRY"));
255 			if (buffer == NULL || length > sizeof(device_geometry))
256 				return B_BAD_VALUE;
257 			device_geometry geometry;
258 			device->GetGeometry(&geometry, op == B_GET_BIOS_GEOMETRY);
259 			return user_memcpy(buffer, &geometry, length);
260 
261 		case B_GET_MEDIA_STATUS:
262 			TRACE(("remote_disk: B_GET_MEDIA_STATUS\n"));
263 			*(status_t*)arg = B_NO_ERROR;
264 			return B_OK;
265 
266 		case B_SET_UNINTERRUPTABLE_IO:
267 			TRACE(("remote_disk: B_SET_UNINTERRUPTABLE_IO\n"));
268 			return B_OK;
269 
270 		case B_SET_INTERRUPTABLE_IO:
271 			TRACE(("remote_disk: B_SET_INTERRUPTABLE_IO\n"));
272 			return B_OK;
273 
274 		case B_FLUSH_DRIVE_CACHE:
275 			TRACE(("remote_disk: B_FLUSH_DRIVE_CACHE\n"));
276 			return B_OK;
277 
278 		case B_GET_BIOS_DRIVE_ID:
279 			TRACE(("remote_disk: B_GET_BIOS_DRIVE_ID\n"));
280 			*(uint8*)arg = 0xF8;
281 			return B_OK;
282 
283 		case B_GET_DRIVER_FOR_DEVICE:
284 		case B_SET_DEVICE_SIZE:
285 		case B_SET_PARTITION:
286 		case B_FORMAT_DEVICE:
287 		case B_EJECT_DEVICE:
288 		case B_LOAD_MEDIA:
289 		case B_GET_NEXT_OPEN_DEVICE:
290 			TRACE(("remote_disk: another ioctl: %lx (%lu)\n", op, op));
291 			return B_BAD_VALUE;
292 
293 		default:
294 			TRACE(("remote_disk: unknown ioctl: %lx (%lu)\n", op, op));
295 			return B_BAD_VALUE;
296 	}
297 }
298 
299 
300 static status_t
301 remote_disk_free(void* cookie)
302 {
303 	TRACE(("remote_disk_free(%p)\n", cookie));
304 
305 	// nothing to do
306 	return B_OK;
307 }
308 
309 
310 static device_hooks sDataDeviceHooks = {
311 	remote_disk_open,
312 	remote_disk_close,
313 	remote_disk_free,
314 	remote_disk_control,
315 	remote_disk_read,
316 	remote_disk_write
317 };
318 
319 
320 // #pragma mark - public API
321 
322 
323 int32 api_version = B_CUR_DRIVER_API_VERSION;
324 
325 
326 status_t
327 init_hardware(void)
328 {
329 	TRACE(("remote_disk: init_hardware()\n"));
330 
331 	return B_OK;
332 }
333 
334 
335 status_t
336 init_driver(void)
337 {
338 	TRACE(("remote_disk: init_driver()\n"));
339 
340 	sDevices = new(nothrow) RemoteDiskDevice[MAX_REMOTE_DISKS];
341 	if (!sDevices)
342 		return B_NO_MEMORY;
343 
344 	status_t error = B_OK;
345 	for (int i = 0; error == B_OK && i < MAX_REMOTE_DISKS; i++)
346 		error = sDevices[i].Init();
347 
348 	if (error != B_OK) {
349 		delete[] sDevices;
350 		sDevices = NULL;
351 		return error;
352 	}
353 
354 	return B_OK;
355 }
356 
357 
358 void
359 uninit_driver(void)
360 {
361 	TRACE(("remote_disk: uninit_driver()\n"));
362 
363 	delete[] sDevices;
364 }
365 
366 
367 const char**
368 publish_devices(void)
369 {
370 	TRACE(("remote_disk: publish_devices()\n"));
371 	return kPublishedNames;
372 }
373 
374 
375 device_hooks*
376 find_device(const char* name)
377 {
378 	TRACE(("remote_disk: find_device(%s)\n", name));
379 	return &sDataDeviceHooks;
380 }
381 
382