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
|
/* $NetBSD: ramdisk.c,v 1.1 1995/10/08 23:30:57 gwr Exp $ */
/*
* Copyright (c) 1995 Gordon W. Ross, Leo Weppelman.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
* 4. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by
* Gordon W. Ross and Leo Weppelman.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This implements a general-puspose RAM-disk.
* See ramdisk.h for notes on the config types.
*
* Note that this driver provides the same functionality
* as the MFS filesystem hack, but this is better because
* you can use this for any filesystem type you'd like!
*
* Credit for most of the kmem ramdisk code goes to:
* Leo Weppelman (atari) and Phil Nelson (pc532)
* Credit for the ideas behind the "user space RAM" code goes
* to the authors of the MFS implementation.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/buf.h>
#include <sys/device.h>
#include <vm/vm.h>
#include <vm/vm_kern.h>
/* Don't want all those other VM headers... */
extern vm_offset_t kmem_alloc __P((vm_map_t, vm_size_t));
#include <dev/ramdisk.h>
/*
* By default, include the user-space functionality.
* Use: option RAMDISK_SERVER=0 to turn it off.
*/
#ifndef RAMDISK_SERVER
#define RAMDISK_SERVER 1
#endif
/*
* XXX: the "control" unit is (base unit + 16).
* We should just use the cdev as the "control", but
* that interferes with the security stuff preventing
* simulatneous use of raw and block devices.
*
* XXX Assumption: 16 RAM-disks are enough!
*/
#define RD_IS_CTRL(unit) (unit & 0x10)
#define RD_UNIT(unit) (unit & 0xF)
/*
* XXX - This is just for a sanity check. Only
* applies to kernel-space RAM disk allocations.
*/
#define RD_KMEM_MAX_SIZE 0x100000 /* 1MB */
/* autoconfig stuff... */
struct rd_softc {
struct device sc_dev; /* REQUIRED first entry */
struct rd_conf sc_rd;
struct buf *sc_buflist;
int sc_flags;
};
/* shorthand for fields in sc_rd: */
#define sc_addr sc_rd.rd_addr
#define sc_size sc_rd.rd_size
#define sc_type sc_rd.rd_type
/* flags */
#define RD_ISOPEN 0x01
#define RD_SERVED 0x02
static int rd_match (struct device *, void *self, void *);
static void rd_attach(struct device *, struct device *self, void *);
struct cfdriver rdcd = {
NULL, "rd", rd_match, rd_attach,
DV_DISK, sizeof(struct device), NULL, 0 };
static int
rd_match(parent, self, aux)
struct device *parent;
void *self;
void *aux;
{
return(1);
}
static void
rd_attach(parent, self, aux)
struct device *parent, *self;
void *aux;
{
struct rd_softc *sc = (struct rd_softc *)self;
/* XXX - Could accept aux info here to set the config. */
#ifdef RAMDISK_HOOKS
/*
* This external function might setup a pre-loaded disk.
* All it would need to do is setup the rd_conf struct.
* See sys/arch/sun3/dev/rd_root.c for an example.
*/
rd_attach_hook(sc->sc_dev.dv_unit, &sc->sc_rd);
#endif
printf("\n");
}
/*
* operational routines:
* open, close, read, write, strategy,
* ioctl, dump, size
*/
void rdstrategy __P((struct buf *bp));
#if RAMDISK_SERVER
static int rd_server_loop __P((struct rd_softc *sc));
static int rd_ioctl_server __P((struct rd_softc *sc,
struct rd_conf *urd, struct proc *proc));
#endif
int rddump(dev, blkno, va, size)
dev_t dev;
daddr_t blkno;
caddr_t va;
size_t size;
{
return ENODEV;
}
int rdsize(dev_t dev)
{
int unit;
struct rd_softc *sc;
/* Disallow control units. */
unit = minor(dev);
if (unit >= rdcd.cd_ndevs)
return 0;
sc = rdcd.cd_devs[unit];
if (sc == NULL)
return 0;
if (sc->sc_type == RD_UNCONFIGURED)
return 0;
return (sc->sc_size >> DEV_BSHIFT);
}
int rdopen(dev, flag, fmt, proc)
dev_t dev;
int flag, fmt;
struct proc *proc;
{
int md, unit;
struct rd_softc *sc;
md = minor(dev);
unit = RD_UNIT(md);
if (unit >= rdcd.cd_ndevs)
return ENXIO;
sc = rdcd.cd_devs[unit];
if (sc == NULL)
return ENXIO;
/*
* The control device is not exclusive, and can
* open uninitialized units (so you can setconf).
*/
if (RD_IS_CTRL(md))
return 0;
#ifdef RAMDISK_HOOKS
/* Call the open hook to allow loading the device. */
rd_open_hook(unit, &sc->sc_rd);
#endif
/*
* This is a normal, "slave" device, so
* enforce initialized, exclusive open.
*/
if (sc->sc_type == RD_UNCONFIGURED)
return ENXIO;
if (sc->sc_flags & RD_ISOPEN)
return EBUSY;
return 0;
}
int rdclose(dev, flag, fmt, proc)
dev_t dev;
int flag, fmt;
struct proc *proc;
{
int md, unit;
struct rd_softc *sc;
md = minor(dev);
unit = RD_UNIT(md);
sc = rdcd.cd_devs[unit];
if (RD_IS_CTRL(md))
return 0;
/* Normal device. */
sc->sc_flags = 0;
return 0;
}
int
rdread(dev, uio)
dev_t dev;
struct uio *uio;
{
return (physio(rdstrategy, NULL, dev, B_READ, minphys, uio));
}
int
rdwrite(dev, uio)
dev_t dev;
struct uio *uio;
{
return (physio(rdstrategy, NULL, dev, B_WRITE, minphys, uio));
}
/*
* Handle I/O requests, either directly, or
* by passing them to the server process.
*/
void
rdstrategy(bp)
struct buf *bp;
{
int md, unit;
struct rd_softc *sc;
caddr_t addr;
size_t off, xfer;
md = minor(bp->b_dev);
unit = RD_UNIT(md);
sc = rdcd.cd_devs[unit];
switch (sc->sc_type) {
#if RAMDISK_SERVER
case RD_UMEM_SERVER:
/* Just add this job to the server's queue. */
bp->b_actf = sc->sc_buflist;
sc->sc_buflist = bp;
if (bp->b_actf == NULL) {
/* server queue was empty. */
wakeup((caddr_t)sc);
/* see rd_server_loop() */
}
/* no biodone in this case */
return;
#endif /* RAMDISK_SERVER */
case RD_KMEM_FIXED:
case RD_KMEM_ALLOCATED:
/* These are in kernel space. Access directly. */
bp->b_resid = bp->b_bcount;
off = (bp->b_blkno << DEV_BSHIFT);
if (off >= sc->sc_size) {
if (bp->b_flags & B_READ)
break; /* EOF */
goto set_eio;
}
xfer = bp->b_resid;
if (xfer > (sc->sc_size - off))
xfer = (sc->sc_size - off);
addr = sc->sc_addr + off;
if (bp->b_flags & B_READ)
bcopy(addr, bp->b_data, xfer);
else
bcopy(bp->b_data, addr, xfer);
bp->b_resid -= xfer;
break;
default:
bp->b_resid = bp->b_bcount;
set_eio:
bp->b_error = EIO;
bp->b_flags |= B_ERROR;
break;
}
biodone(bp);
}
int
rdioctl(dev, cmd, data, flag, proc)
dev_t dev;
u_long cmd;
int flag;
caddr_t data;
struct proc *proc;
{
int md, unit;
struct rd_softc *sc;
struct rd_conf *urd;
md = minor(dev);
unit = RD_UNIT(md);
sc = rdcd.cd_devs[unit];
/* If this is not the control device, punt! */
if (RD_IS_CTRL(md) == 0)
return ENOTTY;
urd = (struct rd_conf *)data;
switch (cmd) {
case RD_GETCONF:
*urd = sc->sc_rd;
return 0;
case RD_SETCONF:
/* Can only set it once. */
if (sc->sc_type != RD_UNCONFIGURED)
break;
switch (urd->rd_type) {
case RD_KMEM_ALLOCATED:
return rd_ioctl_kalloc(sc, urd, proc);
#if RAMDISK_SERVER
case RD_UMEM_SERVER:
return rd_ioctl_server(sc, urd, proc);
#endif
default:
break;
}
break;
}
return EINVAL;
}
/*
* Handle ioctl RD_SETCONF for (sc_type == RD_KMEM_ALLOCATED)
* Just allocate some kernel memory and return.
*/
int
rd_ioctl_kalloc(sc, urd, proc)
struct rd_softc *sc;
struct rd_conf *urd;
struct proc *proc;
{
vm_offset_t addr;
vm_size_t size;
/* Sanity check the size. */
size = urd->rd_size;
if (size > RD_KMEM_MAX_SIZE)
return EINVAL;
addr = kmem_alloc(kernel_map, size);
if (!addr)
return ENOMEM;
/* This unit is now configured. */
sc->sc_addr = (caddr_t)addr; /* kernel space */
sc->sc_size = (size_t)size;
sc->sc_type = RD_KMEM_ALLOCATED;
return 0;
}
#if RAMDISK_SERVER
/*
* Handle ioctl RD_SETCONF for (sc_type == RD_KMEM_ALLOCATED)
* Set config, then become the I/O server for this unit.
*/
int
rd_ioctl_server(sc, urd, proc)
struct rd_softc *sc;
struct rd_conf *urd;
struct proc *proc;
{
vm_offset_t end;
int error;
/* Sanity check addr, size. */
end = (vm_offset_t) (urd->rd_addr + urd->rd_size);
if ((end >= VM_MAXUSER_ADDRESS) ||
(end < ((vm_offset_t) urd->rd_addr)) )
return EINVAL;
/* This unit is now configured. */
sc->sc_addr = urd->rd_addr; /* user space */
sc->sc_size = urd->rd_size;
sc->sc_type = RD_UMEM_SERVER;
/* Become the server daemon */
error = rd_server_loop(sc);
/* This server is now going away! */
sc->sc_type = RD_UNCONFIGURED;
sc->sc_addr = 0;
sc->sc_size = 0;
return (error);
}
int rd_sleep_pri = PWAIT | PCATCH;
static int
rd_server_loop(sc)
struct rd_softc *sc;
{
struct buf *bp;
caddr_t addr; /* user space address */
size_t off; /* offset into "device" */
size_t xfer; /* amount to transfer */
int error;
for (;;) {
/* Wait for some work to arrive. */
while (sc->sc_buflist == NULL) {
error = tsleep((caddr_t)sc, rd_sleep_pri, "rd_idle", 0);
if (error)
return error;
}
/* Unlink buf from head of list. */
bp = sc->sc_buflist;
sc->sc_buflist = bp->b_actf;
bp->b_actf = NULL;
/* Do the transfer to/from user space. */
error = 0;
bp->b_resid = bp->b_bcount;
off = (bp->b_blkno << DEV_BSHIFT);
if (off >= sc->sc_size) {
if (bp->b_flags & B_READ)
goto done; /* EOF (not an error) */
error = EIO;
goto done;
}
xfer = bp->b_resid;
if (xfer > (sc->sc_size - off))
xfer = (sc->sc_size - off);
addr = sc->sc_addr + off;
if (bp->b_flags & B_READ)
error = copyin(addr, bp->b_data, xfer);
else
error = copyout(bp->b_data, addr, xfer);
if (!error)
bp->b_resid -= xfer;
done:
if (error) {
bp->b_error = error;
bp->b_flags |= B_ERROR;
}
biodone(bp);
}
}
#endif /* RAMDISK_SERVER */
|