File: block.c

package info (click to toggle)
oskit 0.97.20000202-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 58,008 kB
  • ctags: 172,612
  • sloc: ansic: 832,827; asm: 7,640; sh: 3,920; yacc: 3,664; perl: 1,457; lex: 427; makefile: 337; csh: 141; awk: 78
file content (748 lines) | stat: -rw-r--r-- 18,599 bytes parent folder | download
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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
/*
 * Copyright (c) 1996-1999 The University of Utah and the Flux Group.
 * 
 * This file is part of the OSKit Linux Glue Libraries, which are free
 * software, also known as "open source;" you can redistribute them and/or
 * modify them under the terms of the GNU General Public License (GPL),
 * version 2, as published by the Free Software Foundation (FSF).
 * 
 * The OSKit is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GPL for more details.  You should have
 * received a copy of the GPL along with the OSKit; see the file COPYING.  If
 * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
 */
/*
 * Implement the device interface for Linux block drivers.
 *
 * XXX: The read/write routines presently assume a linear buffer
 * and do not make use of the buffer operations vectors.
 */

#include <oskit/dev/blk.h>
#include <oskit/dev/isa.h>
#include <oskit/dev/native.h>
#include <oskit/dev/linux.h>

#include <linux/fs.h>
#include <linux/blk.h>
#include <linux/fcntl.h>
#include <linux/major.h>
#include <linux/kdev_t.h>
#include <linux/string.h> /* memcmp */

#include "glue.h"
#include "block.h"
#include "linux_emul.h"
#include "sched.h"
#include "shared.h"
#include "osenv.h"

/*
 * Name to Linux device number mapping table.
 * XXX All this stuff should be separated out and moved to libfdev.
 */
static struct {
	char	*name;
	kdev_t	dev;
} name_to_dev[] = {
	{ "sd0", MKDEV(SCSI_DISK0_MAJOR, 0) },
	{ "sd1", MKDEV(SCSI_DISK0_MAJOR, 1) },
	{ "sd2", MKDEV(SCSI_DISK0_MAJOR, 2) },
	{ "sd3", MKDEV(SCSI_DISK0_MAJOR, 3) },
	{ "sd4", MKDEV(SCSI_DISK0_MAJOR, 4) },
	{ "sd5", MKDEV(SCSI_DISK0_MAJOR, 5) },
	{ "sd6", MKDEV(SCSI_DISK0_MAJOR, 6) },
	{ "sd7", MKDEV(SCSI_DISK0_MAJOR, 7) },
	{ "sda", MKDEV(SCSI_DISK0_MAJOR, 0) },
	{ "sdb", MKDEV(SCSI_DISK0_MAJOR, 1) },
	{ "sdc", MKDEV(SCSI_DISK0_MAJOR, 2) },
	{ "sdd", MKDEV(SCSI_DISK0_MAJOR, 3) },
	{ "sde", MKDEV(SCSI_DISK0_MAJOR, 4) },
	{ "sdf", MKDEV(SCSI_DISK0_MAJOR, 5) },
	{ "sdg", MKDEV(SCSI_DISK0_MAJOR, 6) },
	{ "sdh", MKDEV(SCSI_DISK0_MAJOR, 7) },
	{ "wd0", MKDEV(IDE0_MAJOR, 0) },
	{ "wd1", MKDEV(IDE0_MAJOR, 1) },
	{ "wd2", MKDEV(IDE1_MAJOR, 0) },
	{ "wd3", MKDEV(IDE1_MAJOR, 1) },
	{ "hda", MKDEV(IDE0_MAJOR, 0) },
	{ "hdb", MKDEV(IDE0_MAJOR, 1) },
	{ "hdc", MKDEV(IDE1_MAJOR, 0) },
	{ "hdd", MKDEV(IDE1_MAJOR, 1) },
};
#define NAME_TABLE_SIZE	(sizeof(name_to_dev) / sizeof(name_to_dev[0]))

/*
 * List of open devices.
 */
static struct peropen *open_list;

/*
 * Forward declarations.
 */
static OSKIT_COMDECL blkio_query(oskit_blkio_t *io, const struct oskit_guid *iid,
				void **out_ihandle);
static OSKIT_COMDECL_U blkio_addref(oskit_blkio_t *io);
static OSKIT_COMDECL_U blkio_release(oskit_blkio_t *io);
static OSKIT_COMDECL_U blkio_getblocksize(oskit_blkio_t *io);
static OSKIT_COMDECL blkio_read(oskit_blkio_t *io, void *buf,
			       oskit_off_t offset, oskit_size_t amount,
			       oskit_size_t *out_actual);
static OSKIT_COMDECL blkio_write(oskit_blkio_t *io, const void *buf,
				oskit_off_t offset, oskit_size_t amount,
				oskit_size_t *out_actual);
static OSKIT_COMDECL blkio_getsize(oskit_blkio_t *io, oskit_off_t *out_size);
static OSKIT_COMDECL blkio_setsize(oskit_blkio_t *io, oskit_off_t new_size);

static OSKIT_COMDECL bdev_query(oskit_blkdev_t *dev, const struct oskit_guid *iid,
			       void **out_ihandle);
static OSKIT_COMDECL_U bdev_addref(oskit_blkdev_t *dev);
static OSKIT_COMDECL_U bdev_release(oskit_blkdev_t *dev);
static OSKIT_COMDECL bdev_getinfo(oskit_blkdev_t *fdev, oskit_devinfo_t *out_info);
static OSKIT_COMDECL bdev_getdriver(oskit_blkdev_t *fdev,
				   oskit_driver_t **out_driver);
static OSKIT_COMDECL bdev_open(oskit_blkdev_t *dev, unsigned mode,
			      oskit_blkio_t **out_blkio);

/*
 * Driver operations vector for the per-open block I/O interface.
 */
static struct oskit_blkio_ops block_io_ops = {
	blkio_query,
	blkio_addref,
	blkio_release,
	blkio_getblocksize,
	blkio_read,
	blkio_write,
	blkio_getsize,
	blkio_setsize
};

/*
 * Driver operations vector for the block device interface.
 */
static struct oskit_blkdev_ops blk_dev_ops = {
	bdev_query,
	bdev_addref,
	bdev_release,
	bdev_getinfo,
	bdev_getdriver,
	bdev_open
};

/*
 * Register a Linux block device in the fdev device tree.
 */
oskit_error_t oskit_linux_block_register(int major, unsigned ioaddr,
					oskit_devinfo_t *info, oskit_driver_t *drv)
{
	/* Initialize the block device interface operations vector */
	blkdevs[major].blki.ops = &blk_dev_ops;
	blkdevs[major].info = info;
	blkdevs[major].drv = drv;
	oskit_driver_addref(drv);

	/* Add this block device node to the ISA bus */
	return osenv_isabus_addchild(ioaddr,
				 (oskit_device_t*)&blkdevs[major].blki);
}

/*
 * Remove a device from the open list.
 */
static inline void
open_list_remove(struct peropen *po)
{
	struct peropen *o, **pp;

	for (o = open_list, pp = &open_list; o; pp = &o->next, o = o->next) {
		if (o == po) {
			*pp = po->next;
			return;
		}
	}
}

/*
 * Open a Linux block device given its Linux name.
 */
oskit_error_t
oskit_linux_block_open(const char *name, unsigned flags, oskit_blkio_t **out_io)
{
	int i, len, part;
	kdev_t kdev;
	struct gendisk *gd;

	/*
	 * Convert name to device number.
	 */
	for (i = 0; i < NAME_TABLE_SIZE; i++) {
		len = strlen(name_to_dev[i].name);
		if (!strncmp(name_to_dev[i].name, name, len))
			break;
	}

	if (i == NAME_TABLE_SIZE
	    || !blkdevs[MAJOR(name_to_dev[i].dev)].fops)
		return (OSKIT_E_DEV_NOSUCH_DEV);

	kdev = name_to_dev[i].dev;

	/*
	 * Linux partition support.
	 *
	 * NOTE: This is only here for testing.
	 * It is NOT for general use; use the partition library.
	 */
	i = strlen(name);
	if (i - len > 1)
		return (OSKIT_E_DEV_NOSUCH_DEV);

	if (i - len == 1) {
		if (*(name + len) >= 'a' && *(name + len) <= 'f')
			part = *(name + len) - 'a' + 1;
		else if (*(name + len) >= '0' && *(name + len) <= '9')
			part = *(name + len) - '0';
		else
			return (OSKIT_E_DEV_NOSUCH_DEV);
	} else
		part = 0;

	/*
	 * Construct device number.
	 */

	for (gd = gendisk_head; gd; gd = gd->next) {
		if (gd->major == MAJOR(kdev)) {
			if (part >= gd->max_p)
				return (OSKIT_E_DEV_NOSUCH_DEV);

			kdev = MKDEV(MAJOR(kdev),
				     MINOR(kdev) << gd->minor_shift);
			kdev |= part;
			break;
		}
	}

	return oskit_linux_block_open_kdev(kdev, gd, flags, out_io);
}

/*
 * Open a Linux block device given its Linux device number.
 * The gendisk parameter need only be non-NULL if opening a partition;
 * it can be NULL when opening the entire disk ("partition 0").
 */
oskit_error_t
oskit_linux_block_open_kdev(int kdev, struct gendisk *gd,
		     unsigned flags, oskit_blkio_t **out_io)
{
	int err, i, mode;
	struct file file;
	struct peropen *po;
	struct task_struct ts;

	OSKIT_LINUX_CREATE_CURRENT(ts);

	if (flags & ~OSKIT_DEV_OPEN_ALL) {
		err = OSKIT_E_DEV_BADPARAM;
		goto finish;
	}

	switch (flags & (OSKIT_DEV_OPEN_READ|OSKIT_DEV_OPEN_WRITE)) {

	case OSKIT_DEV_OPEN_READ:
		mode = O_RDONLY;
		break;

	case OSKIT_DEV_OPEN_WRITE:
		mode = O_WRONLY;
		break;

	case OSKIT_DEV_OPEN_READ|OSKIT_DEV_OPEN_WRITE:
		mode = O_RDWR;
		break;

	default:
		err = OSKIT_E_DEV_BADPARAM;
		goto finish;
	}

	/*
	 * Linux uses 1k blocks by default.
	 * We WANT sector-sized blocks in all cases.
	 */
	if (!hardsect_size[MAJOR(kdev)]) {
		osenv_log(OSENV_LOG_DEBUG,
			"default sector size = 512b on %d,%d\n",
			MAJOR(kdev), MINOR(kdev));
		blksize_size[MAJOR(kdev)][MINOR(kdev)] = 512;

	} else if (blksize_size[MAJOR(kdev)][MINOR(kdev)]!=
	    hardsect_size[MAJOR(kdev)][MINOR(kdev)]) {
		osenv_log(OSENV_LOG_DEBUG,
			"setting block size to sector size on %d,%d\n",
			MAJOR(kdev), MINOR(kdev));
		blksize_size[MAJOR(kdev)][MINOR(kdev)] =
		    hardsect_size[MAJOR(kdev)][MINOR(kdev)];

		/* this is a Linux limitation: */
		if (hardsect_size[MAJOR(kdev)][MINOR(kdev)] < 512) {
			err = OSKIT_E_DEV_BADOP;	/* XXX */
			goto finish;
		}
	}

	/*
	 * Search the open list.
	 */
	while (1) {
		for (po = open_list; po; po = po->next)
			if (po->inode.i_rdev == kdev && po->mode == mode)
				break;
		if (po) {
			if (po->busy) {
				po->want = 1;
				sleep_on(&po->waitq);
				continue;
			}
			po->count++;
			*out_io = &po->ioi;
			err = 0;
			goto finish;
		}
		break;
	}

	/*
	 * Allocate and initialize a device structure.
	 */
	po = oskit_linux_mem_alloc(sizeof(struct peropen),
				  OSENV_PHYS_WIRED, 0);
	if (!po) {
		err = OSKIT_E_OUTOFMEMORY;
		goto finish;
	}
	po->ioi.ops = &block_io_ops;
	po->fops = blkdevs[MAJOR(kdev)].fops;
	po->count = 1;
	po->busy = 1;
	po->want = 0;
	po->mode = mode;
	init_waitqueue(&po->waitq);

	po->inode.i_rdev = kdev;

	po->inode.i_emul_data = po;

	po->next = open_list;
	open_list = po->next;

	if (!po->fops->open)
		err = 0;
	else {
		struct dentry dentry;
		file.f_mode = mode;
		file.f_flags = 0;
		file.f_dentry = &dentry;
		dentry.d_inode = &po->inode;

		err = (*po->fops->open)(&po->inode, &file);
	}

	if (err) {
		open_list_remove(po);
		if (po->want) {
			po->want = 0;
			wake_up(&po->waitq);
		}
		err = linux_to_oskit_error(err);
		oskit_linux_mem_free(po, OSENV_PHYS_WIRED,
				    sizeof(struct peropen));
	} else {
		/*
		 * Now that the device has been opened,
		 * we can extract its size.  This might
		 * not have been set properly in blk_size
		 * before the open call, e.g. for the floppy driver.
		 */
		if (gd)
			po->size = gd->part[MINOR(kdev)].nr_sects;
		else {
			osenv_assert(blk_size[MAJOR(kdev)] &&
				     blk_size[MAJOR(kdev)][MINOR(kdev)]);

			po->size = (blk_size[MAJOR(kdev)][MINOR(kdev)]
				    << (BLOCK_SIZE_BITS - DISK_BLOCK_BITS));
		}

		if (blksize_size[MAJOR(kdev)]
		    && blksize_size[MAJOR(kdev)][MINOR(kdev)]) {
			int bshift = DISK_BLOCK_BITS;
			po->bsize = blksize_size[MAJOR(kdev)][MINOR(kdev)];
			for (i = po->bsize >> DISK_BLOCK_BITS; i != 1; i >>= 1)
				bshift++;
			po->bshift = bshift;
		}
		else {
			/* XXX: Linux uses BLOCK_SIZE=1024, not 512 */
			po->bsize = DISK_BLOCK_SIZE;
			po->bshift = DISK_BLOCK_BITS;
		}
		po->bmask = po->bsize - 1;

		po->busy = 0;
		*out_io = &po->ioi;
		if (po->want) {
			po->want = 0;
			wake_up(&po->waitq);
		}
	}
 finish:
	OSKIT_LINUX_DESTROY_CURRENT();
	return (err);
}


/*** Methods implementing the per-open block I/O interface ***/

/*
 * Query a block I/O object for its interfaces.
 * This is extremely easy because we only export one interface
 * (plus its base type, IUnknown).
 */
static OSKIT_COMDECL
blkio_query(oskit_blkio_t *io, const struct oskit_guid *iid, void **out_ihandle)
{
	struct peropen *po;

	po = (struct peropen*)io;
	if (po == NULL)
		panic("%s:%d: null peropen", __FILE__, __LINE__);
	if (po->count == 0)
		panic("%s:%d: bad count", __FILE__, __LINE__);

	if (memcmp(iid, &oskit_iunknown_iid, sizeof(*iid)) == 0 ||
	    memcmp(iid, &oskit_blkio_iid, sizeof(*iid)) == 0) {
		*out_ihandle = &po->ioi;
		++po->count;
		return 0;
	}

	*out_ihandle = NULL;
	return OSKIT_E_NOINTERFACE;
}

/*
 * Clone a reference to a device's block I/O interface.
 */
static OSKIT_COMDECL_U
blkio_addref(oskit_blkio_t *io)
{
	struct peropen *po;

	po = (struct peropen*)io;
	if (po == NULL)
		panic("%s:%d: null peropen", __FILE__, __LINE__);
	if (po->count == 0)
		panic("%s:%d: bad count", __FILE__, __LINE__);

	return ++po->count;
}

/*
 * Close ("release") a device.
 */
static OSKIT_COMDECL_U
blkio_release(oskit_blkio_t *io)
{
	struct file file;
	struct peropen *po;
	unsigned newcount;
	struct task_struct ts;

	OSKIT_LINUX_CREATE_CURRENT(ts);

	po = (struct peropen*)io;
	if (po == NULL)
		panic("%s:%d: null peropen", __FILE__, __LINE__);
	if (po->count == 0)
		panic("%s:%d: bad count", __FILE__, __LINE__);

	while (po->busy) {
		po->want = 1;
		wake_up(&po->waitq);
	}
	newcount = --po->count;
	if (newcount == 0) {
		if (po->fops->release) {
			struct dentry dentry;
			file.f_mode = po->mode;
			file.f_flags = 0;
			file.f_dentry = &dentry;
			dentry.d_inode = &po->inode;
			po->busy = 1;
			(*po->fops->release)(&po->inode, &file);
		}
		open_list_remove(po);
		if (po->want) {
			po->want = 0;
			wake_up(&po->waitq);
		}
		oskit_linux_mem_free(po, OSENV_PHYS_WIRED,
				    sizeof(struct peropen));
	}

	OSKIT_LINUX_DESTROY_CURRENT();

	return newcount;
}

/*
 * Return the block size of this device
 */
static OSKIT_COMDECL_U
blkio_getblocksize(oskit_blkio_t *io)
{
	struct peropen *po;

	po = (struct peropen*)io;
	if (po == NULL)
		panic("%s:%d: null peropen", __FILE__, __LINE__);
	if (po->count == 0)
		panic("%s:%d: bad count", __FILE__, __LINE__);

	return po->bsize;
}

/*
 * Read from a device.
 */
static OSKIT_COMDECL
blkio_read(oskit_blkio_t *io, void *buf,
	   oskit_off_t off, oskit_size_t count, oskit_size_t *amount_read)
{
	int err;
	struct file file;
	struct dentry dentry;
	struct peropen *po;
	unsigned bn, sz;
	struct task_struct ts;

	po = (struct peropen*)io;
	if (po == NULL)
		panic("%s:%d: null peropen", __FILE__, __LINE__);
	if (!po->fops->read || po->mode == O_WRONLY)
		return (OSKIT_E_DEV_BADOP);
	bn = off >> DISK_BLOCK_BITS;
	if (count == 0 || bn == po->size) {
		*amount_read = 0;
		return (0);
	}
	if (bn > po->size)
		return (OSKIT_E_DEV_BADPARAM);

	OSKIT_LINUX_CREATE_CURRENT(ts);

	if ((off & (DISK_BLOCK_SIZE - 1)) == 0)
		sz = (count + (DISK_BLOCK_SIZE - 1)) >> DISK_BLOCK_BITS;
	else {
		sz = 1;
		if (count > DISK_BLOCK_SIZE - (off & (DISK_BLOCK_SIZE - 1)))
			sz += (count - (off & (DISK_BLOCK_SIZE - 1))
			       + (DISK_BLOCK_SIZE - 1)) >> DISK_BLOCK_BITS;
	}
	if (po->size - bn < sz)
		sz = po->size - bn;
	if ((sz << DISK_BLOCK_BITS) - (off & (DISK_BLOCK_SIZE - 1)) < count)
		count = (sz << DISK_BLOCK_BITS) - (off & (DISK_BLOCK_SIZE - 1));
	file.f_mode = po->mode;
	file.f_flags = 0;
	file.f_pos = off;
	file.f_inode = &po->inode;
	file.f_dentry = &dentry;
	dentry.d_inode = &po->inode;

	err = (*po->fops->read)(&file, buf, count, 0);
	if (err) {
		err = linux_to_oskit_error(err);
		*amount_read = 0;	/* XXX */
	} else
		*amount_read = count;

	OSKIT_LINUX_DESTROY_CURRENT();

	return (err);
}

/*
 * Write to a device.
 */
static OSKIT_COMDECL
blkio_write(oskit_blkio_t *io, const void *buf,
	oskit_off_t off, oskit_size_t count, oskit_size_t *amount_written)
{
	int err;
	struct file file;
	struct dentry dentry;
	struct peropen *po;
	unsigned bn, sz;
	struct task_struct ts;

	po = (struct peropen*)io;
	if (po == NULL)
		panic("%s:%d: null peropen", __FILE__, __LINE__);
	if (!po->fops->write || po->mode == O_RDONLY)
		return (OSKIT_E_DEV_BADOP);
	bn = off >> DISK_BLOCK_BITS;
	if (count == 0 || bn == po->size) {
		*amount_written = 0;
		return (0);
	}

	OSKIT_LINUX_CREATE_CURRENT(ts);

	if (bn > po->size)
		return (OSKIT_E_DEV_BADPARAM);
	if ((off & (DISK_BLOCK_SIZE - 1)) == 0)
		sz = (count + (DISK_BLOCK_SIZE - 1)) >> DISK_BLOCK_BITS;
	else {
		sz = 1;
		if (count > DISK_BLOCK_SIZE - (off & (DISK_BLOCK_SIZE - 1)))
			sz += (count - (off & (DISK_BLOCK_SIZE - 1))
			       + (DISK_BLOCK_SIZE - 1)) >> DISK_BLOCK_BITS;
	}
	if (po->size - bn < sz)
		sz = po->size - bn;
	if ((sz << DISK_BLOCK_BITS) - (off & (DISK_BLOCK_SIZE - 1)) < count)
		count = (sz << DISK_BLOCK_BITS) - (off & (DISK_BLOCK_SIZE - 1));
	file.f_mode = po->mode;
	file.f_flags = 0;
	file.f_pos = off;
	file.f_inode = &po->inode;
	file.f_dentry = &dentry;
	dentry.d_inode = &po->inode;
	err = (po->fops->write)(&file, buf, count, 0);
	if (err) {
		osenv_log(OSENV_LOG_NOTICE, "WRITE error %d, 0x%x\n", err, err);
		err = linux_to_oskit_error(err);
		*amount_written = 0;	/* XXX */
	} else
		*amount_written = count;

	OSKIT_LINUX_DESTROY_CURRENT();

	return (err);
}

static OSKIT_COMDECL
blkio_getsize(oskit_blkio_t *io, oskit_off_t *out_size)
{
	struct peropen *po;

	po = (struct peropen*)io;
	if (po == NULL)
		panic("%s:%d: null peropen", __FILE__, __LINE__);

	/* XXX The Linux ide driver sets the size to 0x7fffffff for
	 * IDE CD-ROM's and tape.  Ack!
	 *
	 * ref: linux/src/drivers/block/ide.c:current_capacity()
	 */
	if (po->size == 0x7fffffff)
		return OSKIT_E_NOTIMPL;

	/*
	 * peropen size is in 512 byte sectors.
	 * Note that without this cast, we truncate the result to 32 bits!
	 */
	*out_size = (oskit_off_t)po->size * 512;

	return 0;
}

static OSKIT_COMDECL
blkio_setsize(oskit_blkio_t *io, oskit_off_t new_size)
{
	return OSKIT_E_NOTIMPL;
}


/*** Methods implementing the default fdev block device interface ***/
/*
 * This implementation is only used for Linux block device drivers
 * that don't multiplex multiple actual devices based on minor number.
 */

static OSKIT_COMDECL
bdev_query(oskit_blkdev_t *fdev, const struct oskit_guid *iid, void **out_ihandle)
{
	int major = (struct device_struct*)fdev - blkdevs;

	if (major < 0 || major >= MAX_BLKDEV)
		panic("%s:%d: bad blkdev poitner", __FILE__, __LINE__);

	if (memcmp(iid, &oskit_iunknown_iid, sizeof(*iid)) == 0 ||
	    memcmp(iid, &oskit_device_iid, sizeof(*iid)) == 0 ||
	    memcmp(iid, &oskit_blkdev_iid, sizeof(*iid)) == 0) {
		*out_ihandle = &blkdevs[major].blki;
		return 0;
	}

	*out_ihandle = NULL;
	return OSKIT_E_NOINTERFACE;
}

static OSKIT_COMDECL_U bdev_addref(oskit_blkdev_t *dev)
{
	/* No reference counting for block device nodes */
	return 1;
}

static OSKIT_COMDECL_U bdev_release(oskit_blkdev_t *dev)
{
	/* No reference counting for block device nodes */
	return 1;
}

static OSKIT_COMDECL bdev_getinfo(oskit_blkdev_t *fdev, oskit_devinfo_t *out_info)
{
	int major = (struct device_struct*)fdev - blkdevs;

	if (major < 0 || major >= MAX_BLKDEV)
		panic("%s:%d: bad blkdev poitner", __FILE__, __LINE__);
	if (blkdevs[major].info == NULL)
		panic("%s:%d: no device info registered", __FILE__, __LINE__);

	*out_info = *blkdevs[major].info;
	return 0;
}

static OSKIT_COMDECL bdev_getdriver(oskit_blkdev_t *fdev,
				      oskit_driver_t **out_driver)
{
	int major = (struct device_struct*)fdev - blkdevs;

	if (major < 0 || major >= MAX_BLKDEV)
		panic("%s:%d: bad blkdev poitner", __FILE__, __LINE__);
	if (blkdevs[major].drv == NULL)
		panic("%s:%d: no driver registered", __FILE__, __LINE__);

	*out_driver = blkdevs[major].drv;
	oskit_driver_addref(blkdevs[major].drv);
	return 0;
}

static OSKIT_COMDECL bdev_open(oskit_blkdev_t *fdev, unsigned mode,
				oskit_blkio_t **out_blkio)
{
	int major = (struct device_struct*)fdev - blkdevs;

	if (major < 0 || major >= MAX_BLKDEV)
		panic("%s:%d: bad blkdev poitner", __FILE__, __LINE__);
	if (blkdevs[major].drv == NULL)
		panic("%s:%d: no driver registered", __FILE__, __LINE__);

	return oskit_linux_block_open_kdev(MKDEV(major, 0), NULL, mode,
					  out_blkio);
}