summaryrefslogtreecommitdiff
path: root/minix/lib/libbdev/bdev.c
blob: 36413efbedc165eb0a96df7933a4678f70485816 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/* libbdev - block device interfacing library, by D.C. van Moolenbroek */

#include <minix/drivers.h>
#include <minix/bdev.h>
#include <minix/ioctl.h>
#include <assert.h>

#include "const.h"
#include "type.h"
#include "proto.h"

void bdev_driver(dev_t dev, char *label)
{
/* Associate a driver with the given (major) device, using its endpoint.
 * File system usage note: typically called from mount and newdriver.
 */
  static int first = TRUE;

  if (first) {
	/* Initialize the driver endpoint array. */
	bdev_driver_init();

	first = FALSE;
  }

  bdev_update(dev, label);
}

static int bdev_retry(int *driver_tries, int *transfer_tries, int *result)
{
/* Return TRUE iff the call result implies that we should retry the operation.
 */

  switch (*result) {
  case ERESTART:
	/* We get this error internally if the driver has restarted and the
	 * current operation may now go through. Check the retry count for
	 * driver restarts first, as we don't want to keep trying forever.
	 */
	if (++*driver_tries < DRIVER_TRIES)
		return TRUE;

	*result = EDEADSRCDST;

	break;

  case EIO:
	/* The 'transfer_tries' pointer is non-NULL if this was a transfer
	 * request. If we get back an I/O failure, keep retrying the request
	 * until we hit the transfer retry limit.
	 */
	if (transfer_tries != NULL && ++*transfer_tries < TRANSFER_TRIES)
		return TRUE;

	break;
  }

  return FALSE;
}

static int bdev_opcl(int req, dev_t dev, int bits)
{
/* Open or close the given minor device.
 */
  message m;
  int r, driver_tries = 0;

  do {
	memset(&m, 0, sizeof(m));
	m.m_type = req;
	m.m_lbdev_lblockdriver_msg.minor = minor(dev);
	m.m_lbdev_lblockdriver_msg.access = bits;

	r = bdev_sendrec(dev, &m);
  } while (bdev_retry(&driver_tries, NULL, &r));

  return r;
}

int bdev_open(dev_t dev, int bits)
{
/* Open the given minor device.
 * File system usage note: typically called from mount, after bdev_driver.
 */
  int r;

  r = bdev_opcl(BDEV_OPEN, dev, bits);

  if (r == OK)
	bdev_minor_add(dev, bits);

  return r;
}

int bdev_close(dev_t dev)
{
/* Close the given minor device.
 * File system usage note: typically called from unmount.
 */
  int r;

  bdev_flush_asyn(dev);

  r = bdev_opcl(BDEV_CLOSE, dev, 0);

  if (r == OK)
	bdev_minor_del(dev);

  return r;
}

static int bdev_rdwt_setup(int req, dev_t dev, u64_t pos, char *buf,
  size_t count, int flags, message *m)
{
/* Set up a single-buffer read/write request.
 */
  endpoint_t endpt;
  cp_grant_id_t grant;
  int perm;

  assert((ssize_t) count >= 0);

  if ((endpt = bdev_driver_get(dev)) == NONE)
	return EDEADSRCDST;

  perm = (req == BDEV_READ) ? CPF_WRITE : CPF_READ;

  grant = cpf_grant_direct(endpt, (vir_bytes) buf, count, perm);

  if (!GRANT_VALID(grant)) {
	printf("bdev: unable to allocate grant!\n");
	return EINVAL;
  }

  memset(m, 0, sizeof(*m));
  m->m_type = req;
  m->m_lbdev_lblockdriver_msg.minor = minor(dev);
  m->m_lbdev_lblockdriver_msg.pos = pos;
  m->m_lbdev_lblockdriver_msg.count = count;
  m->m_lbdev_lblockdriver_msg.grant = grant;
  m->m_lbdev_lblockdriver_msg.flags = flags;

  return OK;
}

static void bdev_rdwt_cleanup(const message *m)
{
/* Clean up a single-buffer read/write request.
 */

  cpf_revoke(m->m_lbdev_lblockdriver_msg.grant);
}

static ssize_t bdev_rdwt(int req, dev_t dev, u64_t pos, char *buf,
  size_t count, int flags)
{
/* Perform a synchronous read or write call using a single buffer.
 */
  message m;
  int r, driver_tries = 0, transfer_tries = 0;

  do {
	if ((r = bdev_rdwt_setup(req, dev, pos, buf, count, flags, &m)) != OK)
		break;

	r = bdev_sendrec(dev, &m);

	bdev_rdwt_cleanup(&m);
  } while (bdev_retry(&driver_tries, &transfer_tries, &r));

  return r;
}

static int bdev_vrdwt_setup(int req, dev_t dev, u64_t pos, iovec_t *vec,
  int count, int flags, message *m, iovec_s_t *gvec)
{
/* Set up a vectored read/write request.
 */
  ssize_t size;
  endpoint_t endpt;
  cp_grant_id_t grant;
  int i, perm;

  assert(count <= NR_IOREQS);

  if ((endpt = bdev_driver_get(dev)) == NONE)
	return EDEADSRCDST;

  perm = (req == BDEV_GATHER) ? CPF_WRITE : CPF_READ;
  size = 0;

  for (i = 0; i < count; i++) {
	grant = cpf_grant_direct(endpt, vec[i].iov_addr, vec[i].iov_size,
		perm);

	if (!GRANT_VALID(grant)) {
		printf("bdev: unable to allocate grant!\n");

		for (i--; i >= 0; i--)
			cpf_revoke(gvec[i].iov_grant);

		return EINVAL;
	}

	gvec[i].iov_grant = grant;
	gvec[i].iov_size = vec[i].iov_size;

	assert(vec[i].iov_size > 0);
	assert((ssize_t) (size + vec[i].iov_size) > size);

	size += vec[i].iov_size;
  }

  grant = cpf_grant_direct(endpt, (vir_bytes) gvec, sizeof(gvec[0]) * count,
	CPF_READ);

  if (!GRANT_VALID(grant)) {
	printf("bdev: unable to allocate grant!\n");

	for (i = count - 1; i >= 0; i--)
		cpf_revoke(gvec[i].iov_grant);

	return EINVAL;
  }

  memset(m, 0, sizeof(*m));
  m->m_type = req;
  m->m_lbdev_lblockdriver_msg.minor = minor(dev);
  m->m_lbdev_lblockdriver_msg.pos = pos;
  m->m_lbdev_lblockdriver_msg.count = count;
  m->m_lbdev_lblockdriver_msg.grant = grant;
  m->m_lbdev_lblockdriver_msg.flags = flags;

  return OK;
}

static void bdev_vrdwt_cleanup(const message *m, iovec_s_t *gvec)
{
/* Clean up a vectored read/write request.
 */
  cp_grant_id_t grant;
  int i;

  grant = m->m_lbdev_lblockdriver_msg.grant;

  cpf_revoke(grant);

  for (i = m->m_lbdev_lblockdriver_msg.count - 1; i >= 0; i--)
	cpf_revoke(gvec[i].iov_grant);
}

static ssize_t bdev_vrdwt(int req, dev_t dev, u64_t pos, iovec_t *vec,
  int count, int flags)
{
/* Perform a synchronous read or write call using a vector of buffers.
 */
  iovec_s_t gvec[NR_IOREQS];
  message m;
  int r, driver_tries = 0, transfer_tries = 0;

  do {
	if ((r = bdev_vrdwt_setup(req, dev, pos, vec, count, flags, &m,
			gvec)) != OK)
		break;

	r = bdev_sendrec(dev, &m);

	bdev_vrdwt_cleanup(&m, gvec);
  } while (bdev_retry(&driver_tries, &transfer_tries, &r));

  return r;
}

ssize_t bdev_read(dev_t dev, u64_t pos, char *buf, size_t count, int flags)
{
/* Perform a synchronous read call into a single buffer.
 */

  return bdev_rdwt(BDEV_READ, dev, pos, buf, count, flags);
}

ssize_t bdev_write(dev_t dev, u64_t pos, char *buf, size_t count, int flags)
{
/* Perform a synchronous write call from a single buffer.
 */

  return bdev_rdwt(BDEV_WRITE, dev, pos, buf, count, flags);
}

ssize_t bdev_gather(dev_t dev, u64_t pos, iovec_t *vec, int count, int flags)
{
/* Perform a synchronous read call into a vector of buffers.
 */

  return bdev_vrdwt(BDEV_GATHER, dev, pos, vec, count, flags);
}

ssize_t bdev_scatter(dev_t dev, u64_t pos, iovec_t *vec, int count, int flags)
{
/* Perform a synchronous write call from a vector of buffers.
 */

  return bdev_vrdwt(BDEV_SCATTER, dev, pos, vec, count, flags);
}

static int bdev_ioctl_setup(dev_t dev, unsigned long request, void *buf,
  endpoint_t user_endpt, message *m)
{
/* Set up an I/O control request.
 */
  endpoint_t endpt;
  size_t size;
  cp_grant_id_t grant;
  int perm;

  if ((endpt = bdev_driver_get(dev)) == NONE)
	return EDEADSRCDST;

  if (_MINIX_IOCTL_BIG(request))
	size = _MINIX_IOCTL_SIZE_BIG(request);
  else
	size = _MINIX_IOCTL_SIZE(request);

  perm = 0;
  if (_MINIX_IOCTL_IOR(request)) perm |= CPF_WRITE;
  if (_MINIX_IOCTL_IOW(request)) perm |= CPF_READ;

  /* The size may be 0, in which case 'buf' need not be a valid pointer. */
  grant = cpf_grant_direct(endpt, (vir_bytes) buf, size, perm);

  if (!GRANT_VALID(grant)) {
	printf("bdev: unable to allocate grant!\n");
	return EINVAL;
  }

  memset(m, 0, sizeof(*m));
  m->m_type = BDEV_IOCTL;
  m->m_lbdev_lblockdriver_msg.minor = minor(dev);
  m->m_lbdev_lblockdriver_msg.request = request;
  m->m_lbdev_lblockdriver_msg.grant = grant;
  m->m_lbdev_lblockdriver_msg.user = user_endpt;

  return OK;
}

static void bdev_ioctl_cleanup(const message *m)
{
/* Clean up an I/O control request.
 */

  cpf_revoke(m->m_lbdev_lblockdriver_msg.grant);
}

int bdev_ioctl(dev_t dev, unsigned long request, void *buf,
  endpoint_t user_endpt)
{
/* Perform a synchronous I/O control request.
 */
  message m;
  int r, driver_tries = 0;

  do {
	if ((r = bdev_ioctl_setup(dev, request, buf, user_endpt, &m)) != OK)
		break;

	r = bdev_sendrec(dev, &m);

	bdev_ioctl_cleanup(&m);
  } while (bdev_retry(&driver_tries, NULL, &r));

  return r;
}

void bdev_flush_asyn(dev_t dev)
{
/* Flush all ongoing asynchronous requests to the given minor device. This
 * involves blocking until all I/O for it has completed.
 * File system usage note: typically called from flush.
 */
  bdev_call_t *call;

  while ((call = bdev_call_find(dev)) != NULL)
	(void) bdev_wait_asyn(call->id);
}

static bdev_id_t bdev_rdwt_asyn(int req, dev_t dev, u64_t pos, char *buf,
	size_t count, int flags, bdev_callback_t callback, bdev_param_t param)
{
/* Perform an asynchronous read or write call using a single buffer.
 */
  bdev_call_t *call;
  int r;

  if ((call = bdev_call_alloc(1)) == NULL)
	return ENOMEM;

  if ((r = bdev_rdwt_setup(req, dev, pos, buf, count, flags, &call->msg)) !=
		OK) {
	bdev_call_free(call);

	return r;
  }

  if ((r = bdev_senda(dev, &call->msg, call->id)) != OK) {
	bdev_rdwt_cleanup(&call->msg);

	bdev_call_free(call);

	return r;
  }

  call->dev = dev;
  call->callback = callback;
  call->param = param;
  call->driver_tries = 0;
  call->transfer_tries = 0;
  call->vec[0].iov_addr = (vir_bytes) buf;
  call->vec[0].iov_size = count;

  return call->id;
}

static bdev_id_t bdev_vrdwt_asyn(int req, dev_t dev, u64_t pos, iovec_t *vec,
	int count, int flags, bdev_callback_t callback, bdev_param_t param)
{
/* Perform an asynchronous read or write call using a vector of buffers.
 */
  bdev_call_t *call;
  int r;

  if ((call = bdev_call_alloc(count)) == NULL)
	return ENOMEM;

  if ((r = bdev_vrdwt_setup(req, dev, pos, vec, count, flags, &call->msg,
		call->gvec)) != OK) {
	bdev_call_free(call);

	return r;
  }

  if ((r = bdev_senda(dev, &call->msg, call->id)) != OK) {
	bdev_vrdwt_cleanup(&call->msg, call->gvec);

	bdev_call_free(call);

	return r;
  }

  call->dev = dev;
  call->callback = callback;
  call->param = param;
  call->driver_tries = 0;
  call->transfer_tries = 0;
  memcpy(call->vec, vec, sizeof(vec[0]) * count);

  return call->id;
}

bdev_id_t bdev_read_asyn(dev_t dev, u64_t pos, char *buf, size_t count,
	int flags, bdev_callback_t callback, bdev_param_t param)
{
/* Perform an asynchronous read call into a single buffer.
 */

  return bdev_rdwt_asyn(BDEV_READ, dev, pos, buf, count, flags, callback,
	param);
}

bdev_id_t bdev_write_asyn(dev_t dev, u64_t pos, char *buf, size_t count,
	int flags, bdev_callback_t callback, bdev_param_t param)
{
/* Perform an asynchronous write call from a single buffer.
 */

  return bdev_rdwt_asyn(BDEV_WRITE, dev, pos, buf, count, flags, callback,
	param);
}

bdev_id_t bdev_gather_asyn(dev_t dev, u64_t pos, iovec_t *vec, int count,
	int flags, bdev_callback_t callback, bdev_param_t param)
{
/* Perform an asynchronous read call into a vector of buffers.
 */

  return bdev_vrdwt_asyn(BDEV_GATHER, dev, pos, vec, count, flags, callback,
	param);
}

bdev_id_t bdev_scatter_asyn(dev_t dev, u64_t pos, iovec_t *vec, int count,
	int flags, bdev_callback_t callback, bdev_param_t param)
{
/* Perform an asynchronous write call into a vector of buffers.
 */

  return bdev_vrdwt_asyn(BDEV_SCATTER, dev, pos, vec, count, flags, callback,
	param);
}

bdev_id_t bdev_ioctl_asyn(dev_t dev, unsigned long request, void *buf,
	endpoint_t user_endpt, bdev_callback_t callback, bdev_param_t param)
{
/* Perform an asynchronous I/O control request.
 */
  bdev_call_t *call;
  int r;

  if ((call = bdev_call_alloc(1)) == NULL)
	return ENOMEM;

  if ((r = bdev_ioctl_setup(dev, request, buf, user_endpt,
		&call->msg)) != OK) {
	bdev_call_free(call);

	return r;
  }

  if ((r = bdev_senda(dev, &call->msg, call->id)) != OK) {
	bdev_ioctl_cleanup(&call->msg);

	bdev_call_free(call);

	return r;
  }

  call->dev = dev;
  call->callback = callback;
  call->param = param;
  call->driver_tries = 0;
  call->vec[0].iov_addr = (vir_bytes) buf;

  return call->id;
}

void bdev_callback_asyn(bdev_call_t *call, int result)
{
/* Perform the callback for an asynchronous request, with the given result.
 * Clean up the call structure afterwards.
 */

  /* If this was a transfer request and the result is EIO, we may want to retry
   * the request first.
   */
  switch (call->msg.m_type) {
  case BDEV_READ:
  case BDEV_WRITE:
  case BDEV_GATHER:
  case BDEV_SCATTER:
	if (result == EIO && ++call->transfer_tries < TRANSFER_TRIES) {
		result = bdev_senda(call->dev, &call->msg, call->id);

		if (result == OK)
			return;
	}
  }

  /* Clean up. */
  switch (call->msg.m_type) {
  case BDEV_READ:
  case BDEV_WRITE:
	bdev_rdwt_cleanup(&call->msg);

	break;

  case BDEV_GATHER:
  case BDEV_SCATTER:
	bdev_vrdwt_cleanup(&call->msg, call->gvec);

	break;

  case BDEV_IOCTL:
	bdev_ioctl_cleanup(&call->msg);

	break;

  default:
	assert(0);
  }

  /* Call the callback function. */
  /* FIXME: we assume all reasonable ssize_t values can be stored in an int. */
  call->callback(call->dev, call->id, call->param, result);

  /* Free up the call structure. */
  bdev_call_free(call);
}

int bdev_restart_asyn(bdev_call_t *call)
{
/* The driver for the given call has restarted, and may now have a new
 * endpoint. Recreate and resend the request for the given call.
 */
  int type, r = OK;

  /* Update and check the retry limit for driver restarts first. */
  if (++call->driver_tries >= DRIVER_TRIES)
	return EDEADSRCDST;

  /* Recreate all grants for the new endpoint. */
  type = call->msg.m_type;

  switch (type) {
  case BDEV_READ:
  case BDEV_WRITE:
	bdev_rdwt_cleanup(&call->msg);

	r = bdev_rdwt_setup(type, call->dev,
		call->msg.m_lbdev_lblockdriver_msg.pos,
		(char *) call->vec[0].iov_addr, call->msg.m_lbdev_lblockdriver_msg.count,
		call->msg.m_lbdev_lblockdriver_msg.flags, &call->msg);

	break;

  case BDEV_GATHER:
  case BDEV_SCATTER:
	bdev_vrdwt_cleanup(&call->msg, call->gvec);

	r = bdev_vrdwt_setup(type, call->dev,
		call->msg.m_lbdev_lblockdriver_msg.pos,
		call->vec, call->msg.m_lbdev_lblockdriver_msg.count, call->msg.m_lbdev_lblockdriver_msg.flags,
		&call->msg, call->gvec);

	break;

  case BDEV_IOCTL:
	bdev_ioctl_cleanup(&call->msg);

	r = bdev_ioctl_setup(call->dev, call->msg.m_lbdev_lblockdriver_msg.request,
		(char *) call->vec[0].iov_addr, call->msg.m_lbdev_lblockdriver_msg.user,
		&call->msg);

	break;

  default:
	assert(0);
  }

  if (r != OK)
	return r;

  /* Try to resend the request. */
  return bdev_senda(call->dev, &call->msg, call->id);
}