File: block_glue.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 (711 lines) | stat: -rw-r--r-- 15,848 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
/*
 * 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.
 */
/*
 * Glue for Linux block drivers.
 */

#include <oskit/c/assert.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>

#include "block.h"
#include "irq.h"
#include "shared.h"
#include "osenv.h"

/*
 * Maximum number of block buffers we allocate at a time.
 */
#define MAX_BUF	32

/*
 * Flags for osenv_mem_alloc when allocating a buffer.
 */
#define BUF_MEM_FLAGS	(OSENV_PHYS_WIRED|OSENV_PHYS_CONTIG|OSENV_VIRT_EQ_PHYS)

/*
 * Driver file operations table.
 */
struct device_struct blkdevs[MAX_BLKDEV];

#if 1
/* blk_dev_struct is:
 *      *request_fn
 *      *current_request
 */
struct blk_dev_struct blk_dev[MAX_BLKDEV]; /* initialized by blk_dev_init() */
#else
/*
 * Driver request function table.
 */
struct blk_dev_struct blk_dev[MAX_BLKDEV] = {
	{ NULL, NULL },		/* 0 no_dev */
	{ NULL, NULL },		/* 1 dev mem */
	{ NULL, NULL },		/* 2 dev fd */
	{ NULL, NULL },		/* 3 dev ide0 or hd */
	{ NULL, NULL },		/* 4 dev ttyx */
	{ NULL, NULL },		/* 5 dev tty */
	{ NULL, NULL },		/* 6 dev lp */
	{ NULL, NULL },		/* 7 dev pipes */
	{ NULL, NULL },		/* 8 dev sd */
	{ NULL, NULL },		/* 9 dev st */
	{ NULL, NULL },		/* 10 */
	{ NULL, NULL },		/* 11 */
	{ NULL, NULL },		/* 12 */
	{ NULL, NULL },		/* 13 */
	{ NULL, NULL },		/* 14 */
	{ NULL, NULL },		/* 15 */
	{ NULL, NULL },		/* 16 */
	{ NULL, NULL },		/* 17 */
	{ NULL, NULL },		/* 18 */
	{ NULL, NULL },		/* 19 */
	{ NULL, NULL },		/* 20 */
	{ NULL, NULL },		/* 21 */
	{ NULL, NULL }		/* 22 dev ide1 */
};
#endif

/*
 * blk_size contains the size of all block-devices in units of 1024 byte
 * sectors:
 *
 * blk_size[MAJOR][MINOR]
 *
 * if (!blk_size[MAJOR]) then no minor size checking is done.
 */
int *blk_size[MAX_BLKDEV] = { NULL, NULL, };

/*
 * blksize_size contains the size of all block-devices:
 *
 * blksize_size[MAJOR][MINOR]
 *
 * if (!blksize_size[MAJOR]) then 1024 bytes is assumed.
 */
int *blksize_size[MAX_BLKDEV] = { NULL, NULL, };

/*
 * hardsect_size contains the size of the hardware sector of a device.
 *
 * hardsect_size[MAJOR][MINOR]
 *
 * if (!hardsect_size[MAJOR])
 *		then 512 bytes is assumed.
 * else
 *		sector_size is hardsect_size[MAJOR][MINOR]
 * This is currently set by some scsi device and read by the msdos fs driver
 * This might be a some uses later.
 */
int *hardsect_size[MAX_BLKDEV] = { NULL, NULL, };

/*
 * This specifies how many sectors to read ahead on the disk.
 * This is unused in the OSKIT.  It is here to make drivers compile.
 */
int read_ahead[MAX_BLKDEV] = {0, };

/*
 * The following tunes the read-ahead algorithm in mm/filemap.c
 * This is unused in the OSKIT.  It is here to make drivers compile.
 */
int * max_readahead[MAX_BLKDEV] = { NULL, NULL, };

/*
 * Max number of sectors per request
 * This is unused in the OSKIT.  It is here to make drivers compile.
 */
int * max_sectors[MAX_BLKDEV] = { NULL, NULL, };

/*
 * Wait queue on which drivers sleep waiting for request structures.
  */
struct wait_queue *wait_for_request = NULL;

/*
 * Is called with the request spinlock aquired.
 * NOTE: the device-specific queue() functions
 * have to be atomic!
 */
static inline struct request **
get_queue(kdev_t dev)
{
	struct blk_dev_struct *bdev = blk_dev + MAJOR(dev);

	if (bdev->queue)
		return bdev->queue(dev);
	return &(bdev->current_request);
}

/*
 * Enqueue an I/O request on a driver's queue.
 */
static inline void
enqueue_request(struct request *req)
{
	struct blk_dev_struct *dev;
	struct request *tmp, **current_request;

	dev = blk_dev + MAJOR(req->rq_dev);

	linux_cli();

	current_request = get_queue(req->rq_dev);
	tmp = *current_request;
	if (!tmp) {
		*current_request = req;
		(*dev->request_fn)();

		linux_sti();

		return;
	}
	while (tmp->next) {
		if ((IN_ORDER(tmp, req) || !IN_ORDER(tmp, tmp->next))
		    && IN_ORDER(req, tmp->next))
			break;
		tmp = tmp->next;
	}
	req->next = tmp->next;
	tmp->next = req;
	if (scsi_blk_major(MAJOR(req->rq_dev)))
		(*dev->request_fn)();

	linux_sti();
}

#define FREE_BUFFERS(num) {						\
	int i;								\
									\
	for (i = 0; i < (num); i++)					\
		oskit_linux_mem_free(bhp[i]->b_data, BUF_MEM_FLAGS, po->bsize);\
	oskit_linux_mem_free(bhp, OSENV_PHYS_WIRED,			\
		      (sizeof(struct buffer_head *)			\
		       + sizeof(struct buffer_head)) * nbuf);		\
	if (req)							\
		oskit_linux_mem_free(req, OSENV_PHYS_WIRED, sizeof(*req));\
}

/*
 * Read/write data from/to a device.
 */
static int
block_io(int rw, struct inode *inode, struct file *filp, char *buf, int count)
{
	int err, i, nb, nbuf, resid;
	struct buffer_head *bh, **bhp;
	struct peropen *po;
	struct request *req;
	struct semaphore sem = MUTEX_LOCKED;
	unsigned blk, nsect, off;

	if (count < 0)
		return (-EINVAL);

	po = inode->i_emul_data;
	resid = count;
	blk = filp->f_pos >> po->bshift;
	off = 0;
	err = 0;
	req = NULL;

	assert((count & po->bmask) == 0);
	assert((filp->f_pos & po->bmask) == 0);

	/*
	 * Read/write full sectors.
	 * XXX: Get rid of `512'!!!
	 */
	if (resid >= 512) {
		/*
		 * Allocate buffers.
		 */
		nsect = resid >> 9;
		nbuf = (nsect + (po->bsize >> 9) - 1) / (po->bsize >> 9);
		if (nbuf > MAX_BUF)
			nbuf = MAX_BUF;
		bhp = oskit_linux_mem_alloc((sizeof(struct buffer_head *)
				      + sizeof(struct buffer_head)) * nbuf,
				     OSENV_PHYS_WIRED, 0);
		if (!bhp)
			return (-ENOMEM);
		bh = (struct buffer_head *)(bhp + nbuf);
		for (i = 0; i < nbuf; i++) {
			bhp[i] = bh + i;
			bhp[i]->b_dev = inode->i_rdev;
			bhp[i]->b_wait = NULL;
			bhp[i]->b_data = oskit_linux_mem_alloc(po->bsize,
							BUF_MEM_FLAGS,
							po->bsize);
			if (!bh->b_data) {
				FREE_BUFFERS(i);
				return (-ENOMEM);
			}
		}
		req = oskit_linux_mem_alloc(sizeof(*req), OSENV_PHYS_WIRED, 0);
		if (!req) {
			FREE_BUFFERS(nbuf);
			return (-ENOMEM);
		}
		do {
			/*
			 * Construct buffer list.
			 */
			req->nr_sectors = 0;
			for (i = 0; i < nbuf && nsect; i++) {
				bhp[i]->b_state = 1 << BH_Lock;
				bhp[i]->b_blocknr = blk + i;
				bhp[i]->b_reqnext = bhp[i] + 1;
				if (po->bsize <= (nsect << 9))
					bhp[i]->b_size = po->bsize;
				else
					bhp[i]->b_size = nsect << 9;
				if (rw == WRITE) {
					bhp[i]->b_state |= 1 << BH_Dirty;
					memcpy(bhp[i]->b_data,
					       buf + off,
					       bhp[i]->b_size);
					off += bhp[i]->b_size;
				}
				req->nr_sectors += bhp[i]->b_size >> 9;
				nsect -= bhp[i]->b_size >> 9;
				resid -= bhp[i]->b_size;
			}


			bhp[i-1]->b_reqnext = NULL;
			nb = i;

			/*
			 * Construct request.
			 */
			req->sector = blk << (po->bshift - 9);
			req->current_nr_sectors = bhp[0]->b_size >> 9;
			req->buffer = bhp[0]->b_data;
			req->rq_status = RQ_ACTIVE;
			req->rq_dev = inode->i_rdev;
			req->cmd = rw;
			req->errors = 0;
			req->sem = &sem;
			req->bh = bhp[0];
			req->bhtail = bhp[nb - 1];
			req->next = NULL;

			blk += nb;

			/*
			 * Enqueue request and wait for I/O to complete.
			 */
			enqueue_request(req);
			/* XXX???? WHY was calling __down ? */
			down(&sem);
			if (req->errors) {
				FREE_BUFFERS(nbuf);
				return (-EIO);
			}

			if (rw == READ) {
				for (i = 0; i < nb; i++) {
				        memcpy(buf+off, bhp[i]->b_data,
					       bhp[i]->b_size);
					off += bhp[i]->b_size;
				}
			}
		} while (nsect);

		FREE_BUFFERS(nbuf);
	}

	return 0;
}

/*
 * Generic routine to read from a block device.
 */
int
block_read(struct file *file, char *buf, size_t count, loff_t *off)
{
	return (block_io(READ, file->f_inode, file, buf, count));
}

/*
 * Generic routine to write to a block device.
 */
int
block_write(struct file *file, const char *buf, size_t count, loff_t *off)
{
	return (block_io(WRITE, file->f_inode, file, (char*)buf, count));
}

/*
 * Register a block driver.
 */
int
register_blkdev(unsigned major, const char *name, struct file_operations *fops)
{
	if (major == 0) {
		for (major = MAX_BLKDEV - 1; major > 0; major--)
			if (!blkdevs[major].fops)
				break;
		if (major == 0)
			return (-EBUSY);
	} else {
		if (major >= MAX_BLKDEV)
			return (-EINVAL);
		if (blkdevs[major].fops && blkdevs[major].fops != fops)
			return (-EBUSY);
	}

	blkdevs[major].name = name;
	blkdevs[major].fops = fops;
	return (0);
}

/*
 * Unregister a block driver.
 */
int
unregister_blkdev(unsigned major, const char *name)
{
	if (major >= MAX_BLKDEV
	    || !blkdevs[major].fops
	    || strcmp(blkdevs[major].name, name))
		return (-EINVAL);

	blkdevs[major].fops = NULL;
	return (0);
}

/*
 * Allocate a block buffer.
 */
struct buffer_head *
getblk(kdev_t dev, int block, int size)
{
	struct buffer_head *bh;

	bh = oskit_linux_mem_alloc(sizeof(*bh), OSENV_PHYS_WIRED, 0);
	if (!bh)
		return (NULL);
	bh->b_data = oskit_linux_mem_alloc(size, BUF_MEM_FLAGS, size);
	if (!bh->b_data) {
		oskit_linux_mem_free(bh, OSENV_PHYS_WIRED, sizeof(*bh));
		return (NULL);
	}
	bh->b_dev = dev;
	bh->b_size = size;
	bh->b_state = 1 << BH_Lock;
	bh->b_blocknr = block;
	bh->b_wait = NULL;
	bh->b_reqnext = NULL;
	return (bh);
}

/*
 * Release a buffer.
 */
void
__brelse(struct buffer_head *bh)
{
	oskit_linux_mem_free(bh->b_data, BUF_MEM_FLAGS, bh->b_size);
	oskit_linux_mem_free(bh, OSENV_PHYS_WIRED, sizeof(*bh));
}

/*
 * Read data from a device.
 */
struct buffer_head *
bread(kdev_t dev, int block, int size)
{
	struct buffer_head *bh;

	bh = getblk(dev, block, size);
	if (!bh)
		return (NULL);
	ll_rw_block(READ, 1, &bh);
	wait_on_buffer(bh);
	if (!buffer_uptodate(bh)) {
		__brelse(bh);
		return (NULL);
	}
	return (bh);
}

#define NREQ	50

/*
 * Perform I/O request on a list of buffers.
 */
void
ll_rw_block(int rw, int nr, struct buffer_head **bh)
{
	int i, bsize, bshift;
	struct request *req;
	static int initdone = 0;
	static struct request requests[NREQ];	/* XXX */
#if 0
	struct wait_queue wait = { current, NULL };
#endif

	if (!initdone) {
		for (i = 0; i < NREQ; i++)
			requests[i].rq_status = RQ_INACTIVE;
		initdone = 1;
	}

	/*
	 * Find an unused request structure.
	 */
	linux_cli();

	while (1) {
		for (i = 0; i < NREQ; i++)
			if (requests[i].rq_status == RQ_INACTIVE)
				break;
		if (i < NREQ)
			break;
		sleep_on(&wait_for_request);
	}

	linux_sti();

	req = &requests[i];

	/*
	 * Compute device block size.
	 */
	bsize = BLOCK_SIZE;
	if (blksize_size[MAJOR(bh[0]->b_dev)]
	    && blksize_size[MAJOR(bh[0]->b_dev)][MINOR(bh[0]->b_dev)])
		bsize = blksize_size[MAJOR(bh[0]->b_dev)][MINOR(bh[0]->b_dev)];
	for (i = bsize, bshift = 0; i != 1; bshift++, i >>= 1)
		;

	/*
	 * Construct request.
	 */
	for (i = 0, req->nr_sectors = 0; i < nr - 1; i++) {
		req->nr_sectors += bh[i]->b_size >> 9;
		bh[i]->b_reqnext = bh[i + 1];
	}
	req->nr_sectors += bh[i]->b_size >> 9;
	bh[i]->b_reqnext = NULL;
	req->rq_status = RQ_ACTIVE;
	req->rq_dev = bh[0]->b_dev;
	req->cmd = rw;
	req->errors = 0;
	req->sector = bh[0]->b_blocknr << (bshift - 9);
	req->current_nr_sectors = bh[0]->b_size >> 9;
	req->buffer = bh[0]->b_data;
	req->bh = bh[0];
	req->bhtail = bh[nr - 1];
	req->next = NULL;

	/*
	 * Queue request.
	 */
	enqueue_request(req);
}

/*
 * This routine checks whether a removable media has been changed,
 * and invalidates all buffer-cache-entries in that case. This
 * is a relatively slow routine, so we have to try to minimize using
 * it. Thus it is called only upon a 'mount' or 'open'. This
 * is the best way of combining speed and utility, I think.
 * People changing diskettes in the middle of an operation deserve
 * to loose :-)
 */
int
check_disk_change(kdev_t dev)
{
	unsigned i;
	struct file_operations *fops;

	i = MAJOR(dev);
	if (i >= MAX_BLKDEV || (fops = blkdevs[i].fops) == NULL)
		return (0);
	if (fops->check_media_change == NULL)
		return (0);
	if (!(*fops->check_media_change)(dev))
		return (0);
	osenv_log(OSENV_LOG_NOTICE, "Disk change detected on device %s\n",
		kdevname(dev));
	if (fops->revalidate)
		(*fops->revalidate)(dev);

	return (1);
}

#undef  NR_REQUEST
#define NR_REQUEST  64
static struct request all_requests[NR_REQUEST];

/* RO fail safe mechanism */
static long ro_bits[MAX_BLKDEV][8];

int
is_read_only(kdev_t dev)
{
	int minor,major;

	major = MAJOR(dev);
	minor = MINOR(dev);
	if (major < 0 || major >= MAX_BLKDEV)
		return (0);
	return (ro_bits[major][minor >> 5] & (1 << (minor & 31)));
}

void
set_device_ro(kdev_t dev,int flag)
{
	int minor,major;

	major = MAJOR(dev);
	minor = MINOR(dev);
	if (major < 0 || major >= MAX_BLKDEV)
		return;
	if (flag)
		ro_bits[major][minor >> 5] |= 1 << (minor & 31);
	else
		ro_bits[major][minor >> 5] &= ~(1 << (minor & 31));
}

/*
 * Called by genhd.c to initialize block devices.
 */
int
blk_dev_init(void)
{
	struct request * req;
	struct blk_dev_struct *dev;

	for (dev = blk_dev + MAX_BLKDEV; dev-- != blk_dev;) {
		dev->request_fn      = NULL;
		dev->current_request = NULL;
		dev->queue           = NULL;
/* added these; changes in linux init */
		dev->plug.rq_status  = RQ_INACTIVE;
		dev->plug.cmd        = -1;
		dev->plug.next       = NULL;
		dev->plug_tq.sync    = 0;	/* XXX */
		dev->plug_tq.routine = unplug_device;	/* XXX */
		dev->plug_tq.data    = dev;
	}

	req = all_requests + NR_REQUEST;
	while (--req >= all_requests) {
		req->rq_status = RQ_INACTIVE;
		req->next = NULL;
	}
	memset(ro_bits,0,sizeof(ro_bits));

	return 0;
}

/*
 * This routine from linux/drivers/block/ll_rw_blk.c.
 */
int
end_that_request_first( struct request *req, int uptodate, char *name )
{
	struct buffer_head * bh;
	int nsect;

	req->errors = 0;
	if (!uptodate) {
		printk(KERN_ERR
		       "end_request: I/O error, dev %s (%s), sector %lu\n",
		       kdevname(req->rq_dev), name, req->sector);
#ifdef OSKIT
		nsect = 0;
		while (req->bh) {
			bh = req->bh;
			req->bh = bh->b_reqnext;
			mark_buffer_uptodate(bh, 0);
			unlock_buffer(bh);
		}
		return 0;
#else
		if ((bh = req->bh) != NULL) {
			nsect = bh->b_size >> 9;
			req->nr_sectors--;
			req->nr_sectors &= ~(nsect - 1);
			req->sector += nsect;
			req->sector &= ~(nsect - 1);
		}
#endif
	}

	if ((bh = req->bh) != NULL) {
		req->bh = bh->b_reqnext;
		bh->b_reqnext = NULL;
#ifdef OSKIT
		mark_buffer_uptodate(bh, uptodate);
		unlock_buffer(bh);
#else
		bh->b_end_io(bh, uptodate);
#endif
		if ((bh = req->bh) != NULL) {
			req->current_nr_sectors = bh->b_size >> 9;
			if (req->nr_sectors < req->current_nr_sectors) {
				req->nr_sectors = req->current_nr_sectors;
				printk("end_request: buffer-list destroyed\n");
			}
			req->buffer = bh->b_data;
			return 1;
		}
	}
	return 0;
}

/*
 * This routine from linux/drivers/block/ll_rw_blk.c.
 */
void
end_that_request_last( struct request *req )
{
	if (req->sem != NULL)
		up(req->sem);
	req->rq_status = RQ_INACTIVE;
	wake_up(&wait_for_request);
#ifdef OSKIT
	{
		unsigned long flags;
		save_flags(flags);
		cli();
		(*blk_dev[MAJOR(req->rq_dev)].request_fn)();
		restore_flags(flags);
	}
#endif
}

/*
 * Dummy do-nothing functions whose purpose
 * is merely to satisfy undefined references from Linux code
 * without having to change the Linux code itself.
 */
void chr_dev_init(void) { }
void net_dev_init(void) { }
void console_map_init(void) { }

void unplug_device(void * data)
{
	printk(KERN_CRIT "Unplug device called\n");
}