Deleted Added
full compact
md.c (111119) md.c (111815)
1/*
2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
8 *
1/*
2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
8 *
9 * $FreeBSD: head/sys/dev/md/md.c 111119 2003-02-19 05:47:46Z imp $
9 * $FreeBSD: head/sys/dev/md/md.c 111815 2003-03-03 12:15:54Z phk $
10 *
11 */
12
13/*
14 * The following functions are based in the vn(4) driver: mdstart_swap(),
15 * mdstart_vnode(), mdcreate_swap(), mdcreate_vnode() and mddestroy(),
16 * and as such under the following copyright:
17 *
18 * Copyright (c) 1988 University of Utah.
19 * Copyright (c) 1990, 1993
20 * The Regents of the University of California. All rights reserved.
21 *
22 * This code is derived from software contributed to Berkeley by
23 * the Systems Programming Group of the University of Utah Computer
24 * Science Department.
25 *
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
28 * are met:
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
41 *
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 *
54 * from: Utah Hdr: vn.c 1.13 94/04/02
55 *
56 * from: @(#)vn.c 8.6 (Berkeley) 4/1/94
57 * From: src/sys/dev/vn/vn.c,v 1.122 2000/12/16 16:06:03
58 */
59
60#include "opt_geom.h"
61#include "opt_md.h"
62
63#include <sys/param.h>
64#include <sys/systm.h>
65#include <sys/bio.h>
66#include <sys/conf.h>
67#include <sys/devicestat.h>
68#include <sys/disk.h>
69#include <sys/fcntl.h>
70#include <sys/kernel.h>
71#include <sys/kthread.h>
72#include <sys/linker.h>
73#include <sys/lock.h>
74#include <sys/malloc.h>
75#include <sys/mdioctl.h>
76#include <sys/mutex.h>
77#include <sys/namei.h>
78#include <sys/proc.h>
79#include <sys/queue.h>
80#include <sys/stdint.h>
81#include <sys/sysctl.h>
82#include <sys/vnode.h>
83
84#include <geom/geom.h>
85
86#include <vm/vm.h>
87#include <vm/vm_object.h>
88#include <vm/vm_page.h>
89#include <vm/vm_pager.h>
90#include <vm/swap_pager.h>
91#include <vm/uma.h>
92
93#define MD_MODVER 1
94
95#define MD_SHUTDOWN 0x10000 /* Tell worker thread to terminate. */
96
97#ifndef MD_NSECT
98#define MD_NSECT (10000 * 2)
99#endif
100
101static MALLOC_DEFINE(M_MD, "MD disk", "Memory Disk");
102static MALLOC_DEFINE(M_MDSECT, "MD sectors", "Memory Disk Sectors");
103
104static int md_debug;
105SYSCTL_INT(_debug, OID_AUTO, mddebug, CTLFLAG_RW, &md_debug, 0, "");
106
107#if defined(MD_ROOT) && defined(MD_ROOT_SIZE)
108/* Image gets put here: */
109static u_char mfs_root[MD_ROOT_SIZE*1024] = "MFS Filesystem goes here";
110static u_char end_mfs_root[] __unused = "MFS Filesystem had better STOP here";
111#endif
112
113static int mdrootready;
114static int mdunits;
115static dev_t status_dev = 0;
116
117#define CDEV_MAJOR 95
118
119static d_ioctl_t mdctlioctl;
120
121static struct cdevsw mdctl_cdevsw = {
10 *
11 */
12
13/*
14 * The following functions are based in the vn(4) driver: mdstart_swap(),
15 * mdstart_vnode(), mdcreate_swap(), mdcreate_vnode() and mddestroy(),
16 * and as such under the following copyright:
17 *
18 * Copyright (c) 1988 University of Utah.
19 * Copyright (c) 1990, 1993
20 * The Regents of the University of California. All rights reserved.
21 *
22 * This code is derived from software contributed to Berkeley by
23 * the Systems Programming Group of the University of Utah Computer
24 * Science Department.
25 *
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
28 * are met:
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
41 *
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 *
54 * from: Utah Hdr: vn.c 1.13 94/04/02
55 *
56 * from: @(#)vn.c 8.6 (Berkeley) 4/1/94
57 * From: src/sys/dev/vn/vn.c,v 1.122 2000/12/16 16:06:03
58 */
59
60#include "opt_geom.h"
61#include "opt_md.h"
62
63#include <sys/param.h>
64#include <sys/systm.h>
65#include <sys/bio.h>
66#include <sys/conf.h>
67#include <sys/devicestat.h>
68#include <sys/disk.h>
69#include <sys/fcntl.h>
70#include <sys/kernel.h>
71#include <sys/kthread.h>
72#include <sys/linker.h>
73#include <sys/lock.h>
74#include <sys/malloc.h>
75#include <sys/mdioctl.h>
76#include <sys/mutex.h>
77#include <sys/namei.h>
78#include <sys/proc.h>
79#include <sys/queue.h>
80#include <sys/stdint.h>
81#include <sys/sysctl.h>
82#include <sys/vnode.h>
83
84#include <geom/geom.h>
85
86#include <vm/vm.h>
87#include <vm/vm_object.h>
88#include <vm/vm_page.h>
89#include <vm/vm_pager.h>
90#include <vm/swap_pager.h>
91#include <vm/uma.h>
92
93#define MD_MODVER 1
94
95#define MD_SHUTDOWN 0x10000 /* Tell worker thread to terminate. */
96
97#ifndef MD_NSECT
98#define MD_NSECT (10000 * 2)
99#endif
100
101static MALLOC_DEFINE(M_MD, "MD disk", "Memory Disk");
102static MALLOC_DEFINE(M_MDSECT, "MD sectors", "Memory Disk Sectors");
103
104static int md_debug;
105SYSCTL_INT(_debug, OID_AUTO, mddebug, CTLFLAG_RW, &md_debug, 0, "");
106
107#if defined(MD_ROOT) && defined(MD_ROOT_SIZE)
108/* Image gets put here: */
109static u_char mfs_root[MD_ROOT_SIZE*1024] = "MFS Filesystem goes here";
110static u_char end_mfs_root[] __unused = "MFS Filesystem had better STOP here";
111#endif
112
113static int mdrootready;
114static int mdunits;
115static dev_t status_dev = 0;
116
117#define CDEV_MAJOR 95
118
119static d_ioctl_t mdctlioctl;
120
121static struct cdevsw mdctl_cdevsw = {
122 /* open */ nullopen,
123 /* close */ nullclose,
124 /* read */ noread,
125 /* write */ nowrite,
126 /* ioctl */ mdctlioctl,
127 /* poll */ nopoll,
128 /* mmap */ nommap,
129 /* strategy */ nostrategy,
130 /* name */ MD_NAME,
131 /* maj */ CDEV_MAJOR
122 .d_open = nullopen,
123 .d_close = nullclose,
124 .d_ioctl = mdctlioctl,
125 .d_name = MD_NAME,
126 .d_maj = CDEV_MAJOR
132};
133
134
135static LIST_HEAD(, md_s) md_softc_list = LIST_HEAD_INITIALIZER(&md_softc_list);
136
137#define NINDIR (PAGE_SIZE / sizeof(uintptr_t))
138#define NMASK (NINDIR-1)
139static int nshift;
140
141struct indir {
142 uintptr_t *array;
143 uint total;
144 uint used;
145 uint shift;
146};
147
148struct md_s {
149 int unit;
150 LIST_ENTRY(md_s) list;
151 struct devstat stats;
152 struct bio_queue_head bio_queue;
153 struct mtx queue_mtx;
154 struct disk disk;
155 dev_t dev;
156 enum md_types type;
157 unsigned nsect;
158 unsigned opencount;
159 unsigned secsize;
160 unsigned flags;
161 char name[20];
162 struct proc *procp;
163 struct g_geom *gp;
164 struct g_provider *pp;
165
166 /* MD_MALLOC related fields */
167 struct indir *indir;
168 uma_zone_t uma;
169
170 /* MD_PRELOAD related fields */
171 u_char *pl_ptr;
172 unsigned pl_len;
173
174 /* MD_VNODE related fields */
175 struct vnode *vnode;
176 struct ucred *cred;
177
178 /* MD_SWAP related fields */
179 vm_object_t object;
180};
181
182static int mddestroy(struct md_s *sc, struct thread *td);
183
184static struct indir *
185new_indir(uint shift)
186{
187 struct indir *ip;
188
189 ip = malloc(sizeof *ip, M_MD, M_NOWAIT | M_ZERO);
190 if (ip == NULL)
191 return (NULL);
192 ip->array = malloc(sizeof(uintptr_t) * NINDIR,
193 M_MDSECT, M_NOWAIT | M_ZERO);
194 if (ip->array == NULL) {
195 free(ip, M_MD);
196 return (NULL);
197 }
198 ip->total = NINDIR;
199 ip->shift = shift;
200 return (ip);
201}
202
203static void
204del_indir(struct indir *ip)
205{
206
207 free(ip->array, M_MDSECT);
208 free(ip, M_MD);
209}
210
211static void
212destroy_indir(struct md_s *sc, struct indir *ip)
213{
214 int i;
215
216 for (i = 0; i < NINDIR; i++) {
217 if (!ip->array[i])
218 continue;
219 if (ip->shift)
220 destroy_indir(sc, (struct indir*)(ip->array[i]));
221 else if (ip->array[i] > 255)
222 uma_zfree(sc->uma, (void *)(ip->array[i]));
223 }
224 del_indir(ip);
225}
226
227/*
228 * This function does the math and alloctes the top level "indir" structure
229 * for a device of "size" sectors.
230 */
231
232static struct indir *
233dimension(off_t size)
234{
235 off_t rcnt;
236 struct indir *ip;
237 int i, layer;
238
239 rcnt = size;
240 layer = 0;
241 while (rcnt > NINDIR) {
242 rcnt /= NINDIR;
243 layer++;
244 }
245 /* figure out log2(NINDIR) */
246 for (i = NINDIR, nshift = -1; i; nshift++)
247 i >>= 1;
248
249 /*
250 * XXX: the top layer is probably not fully populated, so we allocate
251 * too much space for ip->array in new_indir() here.
252 */
253 ip = new_indir(layer * nshift);
254 return (ip);
255}
256
257/*
258 * Read a given sector
259 */
260
261static uintptr_t
262s_read(struct indir *ip, off_t offset)
263{
264 struct indir *cip;
265 int idx;
266 uintptr_t up;
267
268 if (md_debug > 1)
269 printf("s_read(%jd)\n", (intmax_t)offset);
270 up = 0;
271 for (cip = ip; cip != NULL;) {
272 if (cip->shift) {
273 idx = (offset >> cip->shift) & NMASK;
274 up = cip->array[idx];
275 cip = (struct indir *)up;
276 continue;
277 }
278 idx = offset & NMASK;
279 return (cip->array[idx]);
280 }
281 return (0);
282}
283
284/*
285 * Write a given sector, prune the tree if the value is 0
286 */
287
288static int
289s_write(struct indir *ip, off_t offset, uintptr_t ptr)
290{
291 struct indir *cip, *lip[10];
292 int idx, li;
293 uintptr_t up;
294
295 if (md_debug > 1)
296 printf("s_write(%jd, %p)\n", (intmax_t)offset, (void *)ptr);
297 up = 0;
298 li = 0;
299 cip = ip;
300 for (;;) {
301 lip[li++] = cip;
302 if (cip->shift) {
303 idx = (offset >> cip->shift) & NMASK;
304 up = cip->array[idx];
305 if (up != 0) {
306 cip = (struct indir *)up;
307 continue;
308 }
309 /* Allocate branch */
310 cip->array[idx] =
311 (uintptr_t)new_indir(cip->shift - nshift);
312 if (cip->array[idx] == 0)
313 return (ENOSPC);
314 cip->used++;
315 up = cip->array[idx];
316 cip = (struct indir *)up;
317 continue;
318 }
319 /* leafnode */
320 idx = offset & NMASK;
321 up = cip->array[idx];
322 if (up != 0)
323 cip->used--;
324 cip->array[idx] = ptr;
325 if (ptr != 0)
326 cip->used++;
327 break;
328 }
329 if (cip->used != 0 || li == 1)
330 return (0);
331 li--;
332 while (cip->used == 0 && cip != ip) {
333 li--;
334 idx = (offset >> lip[li]->shift) & NMASK;
335 up = lip[li]->array[idx];
336 KASSERT(up == (uintptr_t)cip, ("md screwed up"));
337 del_indir(cip);
338 lip[li]->array[idx] = 0;
339 lip[li]->used--;
340 cip = lip[li];
341 }
342 return (0);
343}
344
345
346struct g_class g_md_class = {
347 "MD",
348 NULL,
349 NULL,
350 G_CLASS_INITIALIZER
351
352};
353
354static int
355g_md_access(struct g_provider *pp, int r, int w, int e)
356{
357 struct md_s *sc;
358
359 sc = pp->geom->softc;
360 r += pp->acr;
361 w += pp->acw;
362 e += pp->ace;
363 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
364 sc->opencount = 1;
365 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
366 sc->opencount = 0;
367 }
368 return (0);
369}
370
371static void
372g_md_start(struct bio *bp)
373{
374 struct md_s *sc;
375
376 sc = bp->bio_to->geom->softc;
377
378 switch(bp->bio_cmd) {
379 case BIO_GETATTR:
380 case BIO_SETATTR:
381 g_io_deliver(bp, EOPNOTSUPP);
382 return;
383 }
384 bp->bio_blkno = bp->bio_offset >> DEV_BSHIFT;
385 bp->bio_pblkno = bp->bio_offset / sc->secsize;
386 bp->bio_bcount = bp->bio_length;
387 mtx_lock(&sc->queue_mtx);
388 bioqdisksort(&sc->bio_queue, bp);
389 mtx_unlock(&sc->queue_mtx);
390
391 wakeup(sc);
392}
393
394DECLARE_GEOM_CLASS(g_md_class, g_md);
395
396
397static int
398mdstart_malloc(struct md_s *sc, struct bio *bp)
399{
400 int i, error;
401 u_char *dst;
402 unsigned secno, nsec, uc;
403 uintptr_t sp, osp;
404
405 nsec = bp->bio_bcount / sc->secsize;
406 secno = bp->bio_pblkno;
407 dst = bp->bio_data;
408 error = 0;
409 while (nsec--) {
410 osp = s_read(sc->indir, secno);
411 if (bp->bio_cmd == BIO_DELETE) {
412 if (osp != 0)
413 error = s_write(sc->indir, secno, 0);
414 } else if (bp->bio_cmd == BIO_READ) {
415 if (osp == 0)
416 bzero(dst, sc->secsize);
417 else if (osp <= 255)
418 for (i = 0; i < sc->secsize; i++)
419 dst[i] = osp;
420 else
421 bcopy((void *)osp, dst, sc->secsize);
422 osp = 0;
423 } else if (bp->bio_cmd == BIO_WRITE) {
424 if (sc->flags & MD_COMPRESS) {
425 uc = dst[0];
426 for (i = 1; i < sc->secsize; i++)
427 if (dst[i] != uc)
428 break;
429 } else {
430 i = 0;
431 uc = 0;
432 }
433 if (i == sc->secsize) {
434 if (osp != uc)
435 error = s_write(sc->indir, secno, uc);
436 } else {
437 if (osp <= 255) {
438 sp = (uintptr_t) uma_zalloc(
439 sc->uma, M_NOWAIT);
440 if (sp == 0) {
441 error = ENOSPC;
442 break;
443 }
444 bcopy(dst, (void *)sp, sc->secsize);
445 error = s_write(sc->indir, secno, sp);
446 } else {
447 bcopy(dst, (void *)osp, sc->secsize);
448 osp = 0;
449 }
450 }
451 } else {
452 error = EOPNOTSUPP;
453 }
454 if (osp > 255)
455 uma_zfree(sc->uma, (void*)osp);
456 if (error)
457 break;
458 secno++;
459 dst += sc->secsize;
460 }
461 bp->bio_resid = 0;
462 return (error);
463}
464
465static int
466mdstart_preload(struct md_s *sc, struct bio *bp)
467{
468
469 if (bp->bio_cmd == BIO_DELETE) {
470 } else if (bp->bio_cmd == BIO_READ) {
471 bcopy(sc->pl_ptr + (bp->bio_pblkno << DEV_BSHIFT), bp->bio_data, bp->bio_bcount);
472 } else {
473 bcopy(bp->bio_data, sc->pl_ptr + (bp->bio_pblkno << DEV_BSHIFT), bp->bio_bcount);
474 }
475 bp->bio_resid = 0;
476 return (0);
477}
478
479static int
480mdstart_vnode(struct md_s *sc, struct bio *bp)
481{
482 int error;
483 struct uio auio;
484 struct iovec aiov;
485 struct mount *mp;
486
487 /*
488 * VNODE I/O
489 *
490 * If an error occurs, we set BIO_ERROR but we do not set
491 * B_INVAL because (for a write anyway), the buffer is
492 * still valid.
493 */
494
495 bzero(&auio, sizeof(auio));
496
497 aiov.iov_base = bp->bio_data;
498 aiov.iov_len = bp->bio_bcount;
499 auio.uio_iov = &aiov;
500 auio.uio_iovcnt = 1;
501 auio.uio_offset = (vm_ooffset_t)bp->bio_pblkno * sc->secsize;
502 auio.uio_segflg = UIO_SYSSPACE;
503 if(bp->bio_cmd == BIO_READ)
504 auio.uio_rw = UIO_READ;
505 else
506 auio.uio_rw = UIO_WRITE;
507 auio.uio_resid = bp->bio_bcount;
508 auio.uio_td = curthread;
509 /*
510 * When reading set IO_DIRECT to try to avoid double-caching
511 * the data. When writing IO_DIRECT is not optimal, but we
512 * must set IO_NOWDRAIN to avoid a wdrain deadlock.
513 */
514 if (bp->bio_cmd == BIO_READ) {
515 vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY, curthread);
516 error = VOP_READ(sc->vnode, &auio, IO_DIRECT, sc->cred);
517 } else {
518 (void) vn_start_write(sc->vnode, &mp, V_WAIT);
519 vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY, curthread);
520 error = VOP_WRITE(sc->vnode, &auio, IO_NOWDRAIN, sc->cred);
521 vn_finished_write(mp);
522 }
523 VOP_UNLOCK(sc->vnode, 0, curthread);
524 bp->bio_resid = auio.uio_resid;
525 return (error);
526}
527
528static void
529mddone_swap(struct bio *bp)
530{
531
532 bp->bio_completed = bp->bio_length - bp->bio_resid;
533 g_std_done(bp);
534}
535
536static int
537mdstart_swap(struct md_s *sc, struct bio *bp)
538{
539 {
540 struct bio *bp2;
541
542 bp2 = g_clone_bio(bp);
543 bp2->bio_done = mddone_swap;
544 bp2->bio_blkno = bp2->bio_offset >> DEV_BSHIFT;
545 bp2->bio_pblkno = bp2->bio_offset / sc->secsize;
546 bp2->bio_bcount = bp2->bio_length;
547 bp = bp2;
548 }
549
550 bp->bio_resid = 0;
551 if ((bp->bio_cmd == BIO_DELETE) && (sc->flags & MD_RESERVE))
552 biodone(bp);
553 else
554 vm_pager_strategy(sc->object, bp);
555 return (-1);
556}
557
558static void
559md_kthread(void *arg)
560{
561 struct md_s *sc;
562 struct bio *bp;
563 int error, hasgiant;
564
565 sc = arg;
566 curthread->td_base_pri = PRIBIO;
567
568 switch (sc->type) {
569 case MD_SWAP:
570 case MD_VNODE:
571 mtx_lock(&Giant);
572 hasgiant = 1;
573 break;
574 case MD_MALLOC:
575 case MD_PRELOAD:
576 default:
577 hasgiant = 0;
578 break;
579 }
580
581 for (;;) {
582 mtx_lock(&sc->queue_mtx);
583 bp = bioq_first(&sc->bio_queue);
584 if (bp)
585 bioq_remove(&sc->bio_queue, bp);
586 if (!bp) {
587 if (sc->flags & MD_SHUTDOWN) {
588 mtx_unlock(&sc->queue_mtx);
589 sc->procp = NULL;
590 wakeup(&sc->procp);
591 if (!hasgiant)
592 mtx_lock(&Giant);
593 kthread_exit(0);
594 }
595 msleep(sc, &sc->queue_mtx, PRIBIO | PDROP, "mdwait", 0);
596 continue;
597 }
598 mtx_unlock(&sc->queue_mtx);
599
600 switch (sc->type) {
601 case MD_MALLOC:
602 devstat_start_transaction(&sc->stats);
603 error = mdstart_malloc(sc, bp);
604 break;
605 case MD_PRELOAD:
606 devstat_start_transaction(&sc->stats);
607 error = mdstart_preload(sc, bp);
608 break;
609 case MD_VNODE:
610 devstat_start_transaction(&sc->stats);
611 error = mdstart_vnode(sc, bp);
612 break;
613 case MD_SWAP:
614 error = mdstart_swap(sc, bp);
615 break;
616 default:
617 panic("Impossible md(type)");
618 break;
619 }
620
621 if (error != -1) {
622 bp->bio_completed = bp->bio_length;
623 g_io_deliver(bp, error);
624 }
625 }
626}
627
628static struct md_s *
629mdfind(int unit)
630{
631 struct md_s *sc;
632
633 /* XXX: LOCK(unique unit numbers) */
634 LIST_FOREACH(sc, &md_softc_list, list) {
635 if (sc->unit == unit)
636 break;
637 }
638 /* XXX: UNLOCK(unique unit numbers) */
639 return (sc);
640}
641
642static struct md_s *
643mdnew(int unit)
644{
645 struct md_s *sc;
646 int error, max = -1;
647
648 /* XXX: LOCK(unique unit numbers) */
649 LIST_FOREACH(sc, &md_softc_list, list) {
650 if (sc->unit == unit) {
651 /* XXX: UNLOCK(unique unit numbers) */
652 return (NULL);
653 }
654 if (sc->unit > max)
655 max = sc->unit;
656 }
657 if (unit == -1)
658 unit = max + 1;
659 if (unit > 255)
660 return (NULL);
661 sc = (struct md_s *)malloc(sizeof *sc, M_MD, M_WAITOK | M_ZERO);
662 sc->unit = unit;
663 bioq_init(&sc->bio_queue);
664 mtx_init(&sc->queue_mtx, "md bio queue", NULL, MTX_DEF);
665 sprintf(sc->name, "md%d", unit);
666 error = kthread_create(md_kthread, sc, &sc->procp, 0, 0,"%s", sc->name);
667 if (error) {
668 free(sc, M_MD);
669 return (NULL);
670 }
671 LIST_INSERT_HEAD(&md_softc_list, sc, list);
672 /* XXX: UNLOCK(unique unit numbers) */
673 return (sc);
674}
675
676static void
677mdinit(struct md_s *sc)
678{
679
680 devstat_add_entry(&sc->stats, MD_NAME, sc->unit, sc->secsize,
681 DEVSTAT_NO_ORDERED_TAGS,
682 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_OTHER,
683 DEVSTAT_PRIORITY_OTHER);
684 {
685 struct g_geom *gp;
686 struct g_provider *pp;
687
688 DROP_GIANT();
689 g_topology_lock();
690 gp = g_new_geomf(&g_md_class, "md%d", sc->unit);
691 gp->start = g_md_start;
692 gp->access = g_md_access;
693 gp->softc = sc;
694 pp = g_new_providerf(gp, "md%d", sc->unit);
695 pp->mediasize = (off_t)sc->nsect * sc->secsize;
696 pp->sectorsize = sc->secsize;
697 sc->gp = gp;
698 sc->pp = pp;
699 g_error_provider(pp, 0);
700 g_topology_unlock();
701 PICKUP_GIANT();
702 }
703}
704
705/*
706 * XXX: we should check that the range they feed us is mapped.
707 * XXX: we should implement read-only.
708 */
709
710static int
711mdcreate_preload(struct md_ioctl *mdio)
712{
713 struct md_s *sc;
714
715 if (mdio->md_size == 0)
716 return (EINVAL);
717 if (mdio->md_options & ~(MD_AUTOUNIT))
718 return (EINVAL);
719 if (mdio->md_options & MD_AUTOUNIT) {
720 sc = mdnew(-1);
721 if (sc == NULL)
722 return (ENOMEM);
723 mdio->md_unit = sc->unit;
724 } else {
725 sc = mdnew(mdio->md_unit);
726 if (sc == NULL)
727 return (EBUSY);
728 }
729 sc->type = MD_PRELOAD;
730 sc->secsize = DEV_BSIZE;
731 sc->nsect = mdio->md_size;
732 sc->flags = mdio->md_options & MD_FORCE;
733 /* Cast to pointer size, then to pointer to avoid warning */
734 sc->pl_ptr = (u_char *)(uintptr_t)mdio->md_base;
735 sc->pl_len = (mdio->md_size << DEV_BSHIFT);
736 mdinit(sc);
737 return (0);
738}
739
740
741static int
742mdcreate_malloc(struct md_ioctl *mdio)
743{
744 struct md_s *sc;
745 off_t u;
746 uintptr_t sp;
747 int error;
748
749 error = 0;
750 if (mdio->md_size == 0)
751 return (EINVAL);
752 if (mdio->md_options & ~(MD_AUTOUNIT | MD_COMPRESS | MD_RESERVE))
753 return (EINVAL);
754 /* Compression doesn't make sense if we have reserved space */
755 if (mdio->md_options & MD_RESERVE)
756 mdio->md_options &= ~MD_COMPRESS;
757 if (mdio->md_options & MD_AUTOUNIT) {
758 sc = mdnew(-1);
759 if (sc == NULL)
760 return (ENOMEM);
761 mdio->md_unit = sc->unit;
762 } else {
763 sc = mdnew(mdio->md_unit);
764 if (sc == NULL)
765 return (EBUSY);
766 }
767 sc->type = MD_MALLOC;
768 sc->secsize = DEV_BSIZE;
769 sc->nsect = mdio->md_size;
770 sc->flags = mdio->md_options & (MD_COMPRESS | MD_FORCE);
771 sc->indir = dimension(sc->nsect);
772 sc->uma = uma_zcreate(sc->name, sc->secsize,
773 NULL, NULL, NULL, NULL, 0x1ff, 0);
774 if (mdio->md_options & MD_RESERVE) {
775 for (u = 0; u < sc->nsect; u++) {
776 sp = (uintptr_t) uma_zalloc(sc->uma, M_NOWAIT | M_ZERO);
777 if (sp != 0)
778 error = s_write(sc->indir, u, sp);
779 else
780 error = ENOMEM;
781 if (error)
782 break;
783 }
784 }
785 if (error) {
786 mddestroy(sc, NULL);
787 return (error);
788 }
789 mdinit(sc);
790 if (!(mdio->md_options & MD_RESERVE))
791 sc->pp->flags |= G_PF_CANDELETE;
792 return (0);
793}
794
795
796static int
797mdsetcred(struct md_s *sc, struct ucred *cred)
798{
799 char *tmpbuf;
800 int error = 0;
801
802 /*
803 * Set credits in our softc
804 */
805
806 if (sc->cred)
807 crfree(sc->cred);
808 sc->cred = crhold(cred);
809
810 /*
811 * Horrible kludge to establish credentials for NFS XXX.
812 */
813
814 if (sc->vnode) {
815 struct uio auio;
816 struct iovec aiov;
817
818 tmpbuf = malloc(sc->secsize, M_TEMP, M_WAITOK);
819 bzero(&auio, sizeof(auio));
820
821 aiov.iov_base = tmpbuf;
822 aiov.iov_len = sc->secsize;
823 auio.uio_iov = &aiov;
824 auio.uio_iovcnt = 1;
825 auio.uio_offset = 0;
826 auio.uio_rw = UIO_READ;
827 auio.uio_segflg = UIO_SYSSPACE;
828 auio.uio_resid = aiov.iov_len;
829 vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY, curthread);
830 error = VOP_READ(sc->vnode, &auio, 0, sc->cred);
831 VOP_UNLOCK(sc->vnode, 0, curthread);
832 free(tmpbuf, M_TEMP);
833 }
834 return (error);
835}
836
837static int
838mdcreate_vnode(struct md_ioctl *mdio, struct thread *td)
839{
840 struct md_s *sc;
841 struct vattr vattr;
842 struct nameidata nd;
843 int error, flags;
844
845 flags = FREAD|FWRITE;
846 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, mdio->md_file, td);
847 error = vn_open(&nd, &flags, 0);
848 if (error) {
849 if (error != EACCES && error != EPERM && error != EROFS)
850 return (error);
851 flags &= ~FWRITE;
852 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, mdio->md_file, td);
853 error = vn_open(&nd, &flags, 0);
854 if (error)
855 return (error);
856 }
857 NDFREE(&nd, NDF_ONLY_PNBUF);
858 if (nd.ni_vp->v_type != VREG ||
859 (error = VOP_GETATTR(nd.ni_vp, &vattr, td->td_ucred, td))) {
860 VOP_UNLOCK(nd.ni_vp, 0, td);
861 (void) vn_close(nd.ni_vp, flags, td->td_ucred, td);
862 return (error ? error : EINVAL);
863 }
864 VOP_UNLOCK(nd.ni_vp, 0, td);
865
866 if (mdio->md_options & MD_AUTOUNIT) {
867 sc = mdnew(-1);
868 mdio->md_unit = sc->unit;
869 } else {
870 sc = mdnew(mdio->md_unit);
871 }
872 if (sc == NULL) {
873 (void) vn_close(nd.ni_vp, flags, td->td_ucred, td);
874 return (EBUSY);
875 }
876
877 sc->type = MD_VNODE;
878 sc->flags = mdio->md_options & MD_FORCE;
879 if (!(flags & FWRITE))
880 sc->flags |= MD_READONLY;
881 sc->secsize = DEV_BSIZE;
882 sc->vnode = nd.ni_vp;
883
884 /*
885 * If the size is specified, override the file attributes.
886 */
887 if (mdio->md_size)
888 sc->nsect = mdio->md_size;
889 else
890 sc->nsect = vattr.va_size / sc->secsize; /* XXX: round up ? */
891 if (sc->nsect == 0) {
892 mddestroy(sc, td);
893 return (EINVAL);
894 }
895 error = mdsetcred(sc, td->td_ucred);
896 if (error) {
897 mddestroy(sc, td);
898 return (error);
899 }
900 mdinit(sc);
901 return (0);
902}
903
904static int
905mddestroy(struct md_s *sc, struct thread *td)
906{
907
908 GIANT_REQUIRED;
909
910 mtx_destroy(&sc->queue_mtx);
911 devstat_remove_entry(&sc->stats);
912 {
913 if (sc->gp) {
914 sc->gp->flags |= G_GEOM_WITHER;
915 sc->gp->softc = NULL;
916 }
917 if (sc->pp)
918 g_orphan_provider(sc->pp, ENXIO);
919 }
920 sc->flags |= MD_SHUTDOWN;
921 wakeup(sc);
922 while (sc->procp != NULL)
923 tsleep(&sc->procp, PRIBIO, "mddestroy", hz / 10);
924 if (sc->vnode != NULL)
925 (void)vn_close(sc->vnode, sc->flags & MD_READONLY ?
926 FREAD : (FREAD|FWRITE), sc->cred, td);
927 if (sc->cred != NULL)
928 crfree(sc->cred);
929 if (sc->object != NULL) {
930 vm_pager_deallocate(sc->object);
931 }
932 if (sc->indir)
933 destroy_indir(sc, sc->indir);
934 if (sc->uma)
935 uma_zdestroy(sc->uma);
936
937 /* XXX: LOCK(unique unit numbers) */
938 LIST_REMOVE(sc, list);
939 /* XXX: UNLOCK(unique unit numbers) */
940 free(sc, M_MD);
941 return (0);
942}
943
944static int
945mdcreate_swap(struct md_ioctl *mdio, struct thread *td)
946{
947 int error;
948 struct md_s *sc;
949
950 GIANT_REQUIRED;
951
952 if (mdio->md_options & MD_AUTOUNIT) {
953 sc = mdnew(-1);
954 mdio->md_unit = sc->unit;
955 } else {
956 sc = mdnew(mdio->md_unit);
957 }
958 if (sc == NULL)
959 return (EBUSY);
960
961 sc->type = MD_SWAP;
962
963 /*
964 * Range check. Disallow negative sizes or any size less then the
965 * size of a page. Then round to a page.
966 */
967
968 if (mdio->md_size == 0) {
969 mddestroy(sc, td);
970 return (EDOM);
971 }
972
973 /*
974 * Allocate an OBJT_SWAP object.
975 *
976 * sc_secsize is PAGE_SIZE'd
977 *
978 * mdio->size is in DEV_BSIZE'd chunks.
979 * Note the truncation.
980 */
981
982 sc->secsize = PAGE_SIZE;
983 sc->nsect = mdio->md_size / (PAGE_SIZE / DEV_BSIZE);
984 sc->object = vm_pager_allocate(OBJT_SWAP, NULL, sc->secsize * (vm_offset_t)sc->nsect, VM_PROT_DEFAULT, 0);
985 sc->flags = mdio->md_options & MD_FORCE;
986 if (mdio->md_options & MD_RESERVE) {
987 if (swap_pager_reserve(sc->object, 0, sc->nsect) < 0) {
988 vm_pager_deallocate(sc->object);
989 sc->object = NULL;
990 mddestroy(sc, td);
991 return (EDOM);
992 }
993 }
994 error = mdsetcred(sc, td->td_ucred);
995 if (error) {
996 mddestroy(sc, td);
997 return (error);
998 }
999 mdinit(sc);
1000 if (!(mdio->md_options & MD_RESERVE))
1001 sc->pp->flags |= G_PF_CANDELETE;
1002 return (0);
1003}
1004
1005static int
1006mddetach(int unit, struct thread *td)
1007{
1008 struct md_s *sc;
1009
1010 sc = mdfind(unit);
1011 if (sc == NULL)
1012 return (ENOENT);
1013 if (sc->opencount != 0 && !(sc->flags & MD_FORCE))
1014 return (EBUSY);
1015 switch(sc->type) {
1016 case MD_VNODE:
1017 case MD_SWAP:
1018 case MD_MALLOC:
1019 case MD_PRELOAD:
1020 return (mddestroy(sc, td));
1021 default:
1022 return (EOPNOTSUPP);
1023 }
1024}
1025
1026static int
1027mdctlioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
1028{
1029 struct md_ioctl *mdio;
1030 struct md_s *sc;
1031 int i;
1032
1033 if (md_debug)
1034 printf("mdctlioctl(%s %lx %p %x %p)\n",
1035 devtoname(dev), cmd, addr, flags, td);
1036
1037 /*
1038 * We assert the version number in the individual ioctl
1039 * handlers instead of out here because (a) it is possible we
1040 * may add another ioctl in the future which doesn't read an
1041 * mdio, and (b) the correct return value for an unknown ioctl
1042 * is ENOIOCTL, not EINVAL.
1043 */
1044 mdio = (struct md_ioctl *)addr;
1045 switch (cmd) {
1046 case MDIOCATTACH:
1047 if (mdio->md_version != MDIOVERSION)
1048 return (EINVAL);
1049 switch (mdio->md_type) {
1050 case MD_MALLOC:
1051 return (mdcreate_malloc(mdio));
1052 case MD_PRELOAD:
1053 return (mdcreate_preload(mdio));
1054 case MD_VNODE:
1055 return (mdcreate_vnode(mdio, td));
1056 case MD_SWAP:
1057 return (mdcreate_swap(mdio, td));
1058 default:
1059 return (EINVAL);
1060 }
1061 case MDIOCDETACH:
1062 if (mdio->md_version != MDIOVERSION)
1063 return (EINVAL);
1064 if (mdio->md_file != NULL || mdio->md_size != 0 ||
1065 mdio->md_options != 0)
1066 return (EINVAL);
1067 return (mddetach(mdio->md_unit, td));
1068 case MDIOCQUERY:
1069 if (mdio->md_version != MDIOVERSION)
1070 return (EINVAL);
1071 sc = mdfind(mdio->md_unit);
1072 if (sc == NULL)
1073 return (ENOENT);
1074 mdio->md_type = sc->type;
1075 mdio->md_options = sc->flags;
1076 switch (sc->type) {
1077 case MD_MALLOC:
1078 mdio->md_size = sc->nsect;
1079 break;
1080 case MD_PRELOAD:
1081 mdio->md_size = sc->nsect;
1082 mdio->md_base = (uint64_t)(intptr_t)sc->pl_ptr;
1083 break;
1084 case MD_SWAP:
1085 mdio->md_size = sc->nsect * (PAGE_SIZE / DEV_BSIZE);
1086 break;
1087 case MD_VNODE:
1088 mdio->md_size = sc->nsect;
1089 /* XXX fill this in */
1090 mdio->md_file = NULL;
1091 break;
1092 }
1093 return (0);
1094 case MDIOCLIST:
1095 i = 1;
1096 LIST_FOREACH(sc, &md_softc_list, list) {
1097 if (i == MDNPAD - 1)
1098 mdio->md_pad[i] = -1;
1099 else
1100 mdio->md_pad[i++] = sc->unit;
1101 }
1102 mdio->md_pad[0] = i - 1;
1103 return (0);
1104 default:
1105 return (ENOIOCTL);
1106 };
1107 return (ENOIOCTL);
1108}
1109
1110static void
1111md_preloaded(u_char *image, unsigned length)
1112{
1113 struct md_s *sc;
1114
1115 sc = mdnew(-1);
1116 if (sc == NULL)
1117 return;
1118 sc->type = MD_PRELOAD;
1119 sc->secsize = DEV_BSIZE;
1120 sc->nsect = length / DEV_BSIZE;
1121 sc->pl_ptr = image;
1122 sc->pl_len = length;
1123 if (sc->unit == 0)
1124 mdrootready = 1;
1125 mdinit(sc);
1126}
1127
1128static void
1129md_drvinit(void *unused)
1130{
1131
1132 caddr_t mod;
1133 caddr_t c;
1134 u_char *ptr, *name, *type;
1135 unsigned len;
1136
1137#ifdef MD_ROOT_SIZE
1138 md_preloaded(mfs_root, MD_ROOT_SIZE*1024);
1139#endif
1140 mod = NULL;
1141 while ((mod = preload_search_next_name(mod)) != NULL) {
1142 name = (char *)preload_search_info(mod, MODINFO_NAME);
1143 type = (char *)preload_search_info(mod, MODINFO_TYPE);
1144 if (name == NULL)
1145 continue;
1146 if (type == NULL)
1147 continue;
1148 if (strcmp(type, "md_image") && strcmp(type, "mfs_root"))
1149 continue;
1150 c = preload_search_info(mod, MODINFO_ADDR);
1151 ptr = *(u_char **)c;
1152 c = preload_search_info(mod, MODINFO_SIZE);
1153 len = *(size_t *)c;
1154 printf("%s%d: Preloaded image <%s> %d bytes at %p\n",
1155 MD_NAME, mdunits, name, len, ptr);
1156 md_preloaded(ptr, len);
1157 }
1158 status_dev = make_dev(&mdctl_cdevsw, 0xffff00ff, UID_ROOT, GID_WHEEL,
1159 0600, MDCTL_NAME);
1160}
1161
1162static int
1163md_modevent(module_t mod, int type, void *data)
1164{
1165 int error;
1166 struct md_s *sc;
1167
1168 switch (type) {
1169 case MOD_LOAD:
1170 md_drvinit(NULL);
1171 break;
1172 case MOD_UNLOAD:
1173 LIST_FOREACH(sc, &md_softc_list, list) {
1174 error = mddetach(sc->unit, curthread);
1175 if (error != 0)
1176 return (error);
1177 }
1178 if (status_dev)
1179 destroy_dev(status_dev);
1180 status_dev = 0;
1181 break;
1182 default:
1183 break;
1184 }
1185 return (0);
1186}
1187
1188static moduledata_t md_mod = {
1189 MD_NAME,
1190 md_modevent,
1191 NULL
1192};
1193DECLARE_MODULE(md, md_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE+CDEV_MAJOR);
1194MODULE_VERSION(md, MD_MODVER);
1195
1196
1197#ifdef MD_ROOT
1198static void
1199md_takeroot(void *junk)
1200{
1201 if (mdrootready)
1202 rootdevnames[0] = "ufs:/dev/md0";
1203}
1204
1205SYSINIT(md_root, SI_SUB_MOUNT_ROOT, SI_ORDER_FIRST, md_takeroot, NULL);
1206#endif /* MD_ROOT */
127};
128
129
130static LIST_HEAD(, md_s) md_softc_list = LIST_HEAD_INITIALIZER(&md_softc_list);
131
132#define NINDIR (PAGE_SIZE / sizeof(uintptr_t))
133#define NMASK (NINDIR-1)
134static int nshift;
135
136struct indir {
137 uintptr_t *array;
138 uint total;
139 uint used;
140 uint shift;
141};
142
143struct md_s {
144 int unit;
145 LIST_ENTRY(md_s) list;
146 struct devstat stats;
147 struct bio_queue_head bio_queue;
148 struct mtx queue_mtx;
149 struct disk disk;
150 dev_t dev;
151 enum md_types type;
152 unsigned nsect;
153 unsigned opencount;
154 unsigned secsize;
155 unsigned flags;
156 char name[20];
157 struct proc *procp;
158 struct g_geom *gp;
159 struct g_provider *pp;
160
161 /* MD_MALLOC related fields */
162 struct indir *indir;
163 uma_zone_t uma;
164
165 /* MD_PRELOAD related fields */
166 u_char *pl_ptr;
167 unsigned pl_len;
168
169 /* MD_VNODE related fields */
170 struct vnode *vnode;
171 struct ucred *cred;
172
173 /* MD_SWAP related fields */
174 vm_object_t object;
175};
176
177static int mddestroy(struct md_s *sc, struct thread *td);
178
179static struct indir *
180new_indir(uint shift)
181{
182 struct indir *ip;
183
184 ip = malloc(sizeof *ip, M_MD, M_NOWAIT | M_ZERO);
185 if (ip == NULL)
186 return (NULL);
187 ip->array = malloc(sizeof(uintptr_t) * NINDIR,
188 M_MDSECT, M_NOWAIT | M_ZERO);
189 if (ip->array == NULL) {
190 free(ip, M_MD);
191 return (NULL);
192 }
193 ip->total = NINDIR;
194 ip->shift = shift;
195 return (ip);
196}
197
198static void
199del_indir(struct indir *ip)
200{
201
202 free(ip->array, M_MDSECT);
203 free(ip, M_MD);
204}
205
206static void
207destroy_indir(struct md_s *sc, struct indir *ip)
208{
209 int i;
210
211 for (i = 0; i < NINDIR; i++) {
212 if (!ip->array[i])
213 continue;
214 if (ip->shift)
215 destroy_indir(sc, (struct indir*)(ip->array[i]));
216 else if (ip->array[i] > 255)
217 uma_zfree(sc->uma, (void *)(ip->array[i]));
218 }
219 del_indir(ip);
220}
221
222/*
223 * This function does the math and alloctes the top level "indir" structure
224 * for a device of "size" sectors.
225 */
226
227static struct indir *
228dimension(off_t size)
229{
230 off_t rcnt;
231 struct indir *ip;
232 int i, layer;
233
234 rcnt = size;
235 layer = 0;
236 while (rcnt > NINDIR) {
237 rcnt /= NINDIR;
238 layer++;
239 }
240 /* figure out log2(NINDIR) */
241 for (i = NINDIR, nshift = -1; i; nshift++)
242 i >>= 1;
243
244 /*
245 * XXX: the top layer is probably not fully populated, so we allocate
246 * too much space for ip->array in new_indir() here.
247 */
248 ip = new_indir(layer * nshift);
249 return (ip);
250}
251
252/*
253 * Read a given sector
254 */
255
256static uintptr_t
257s_read(struct indir *ip, off_t offset)
258{
259 struct indir *cip;
260 int idx;
261 uintptr_t up;
262
263 if (md_debug > 1)
264 printf("s_read(%jd)\n", (intmax_t)offset);
265 up = 0;
266 for (cip = ip; cip != NULL;) {
267 if (cip->shift) {
268 idx = (offset >> cip->shift) & NMASK;
269 up = cip->array[idx];
270 cip = (struct indir *)up;
271 continue;
272 }
273 idx = offset & NMASK;
274 return (cip->array[idx]);
275 }
276 return (0);
277}
278
279/*
280 * Write a given sector, prune the tree if the value is 0
281 */
282
283static int
284s_write(struct indir *ip, off_t offset, uintptr_t ptr)
285{
286 struct indir *cip, *lip[10];
287 int idx, li;
288 uintptr_t up;
289
290 if (md_debug > 1)
291 printf("s_write(%jd, %p)\n", (intmax_t)offset, (void *)ptr);
292 up = 0;
293 li = 0;
294 cip = ip;
295 for (;;) {
296 lip[li++] = cip;
297 if (cip->shift) {
298 idx = (offset >> cip->shift) & NMASK;
299 up = cip->array[idx];
300 if (up != 0) {
301 cip = (struct indir *)up;
302 continue;
303 }
304 /* Allocate branch */
305 cip->array[idx] =
306 (uintptr_t)new_indir(cip->shift - nshift);
307 if (cip->array[idx] == 0)
308 return (ENOSPC);
309 cip->used++;
310 up = cip->array[idx];
311 cip = (struct indir *)up;
312 continue;
313 }
314 /* leafnode */
315 idx = offset & NMASK;
316 up = cip->array[idx];
317 if (up != 0)
318 cip->used--;
319 cip->array[idx] = ptr;
320 if (ptr != 0)
321 cip->used++;
322 break;
323 }
324 if (cip->used != 0 || li == 1)
325 return (0);
326 li--;
327 while (cip->used == 0 && cip != ip) {
328 li--;
329 idx = (offset >> lip[li]->shift) & NMASK;
330 up = lip[li]->array[idx];
331 KASSERT(up == (uintptr_t)cip, ("md screwed up"));
332 del_indir(cip);
333 lip[li]->array[idx] = 0;
334 lip[li]->used--;
335 cip = lip[li];
336 }
337 return (0);
338}
339
340
341struct g_class g_md_class = {
342 "MD",
343 NULL,
344 NULL,
345 G_CLASS_INITIALIZER
346
347};
348
349static int
350g_md_access(struct g_provider *pp, int r, int w, int e)
351{
352 struct md_s *sc;
353
354 sc = pp->geom->softc;
355 r += pp->acr;
356 w += pp->acw;
357 e += pp->ace;
358 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
359 sc->opencount = 1;
360 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
361 sc->opencount = 0;
362 }
363 return (0);
364}
365
366static void
367g_md_start(struct bio *bp)
368{
369 struct md_s *sc;
370
371 sc = bp->bio_to->geom->softc;
372
373 switch(bp->bio_cmd) {
374 case BIO_GETATTR:
375 case BIO_SETATTR:
376 g_io_deliver(bp, EOPNOTSUPP);
377 return;
378 }
379 bp->bio_blkno = bp->bio_offset >> DEV_BSHIFT;
380 bp->bio_pblkno = bp->bio_offset / sc->secsize;
381 bp->bio_bcount = bp->bio_length;
382 mtx_lock(&sc->queue_mtx);
383 bioqdisksort(&sc->bio_queue, bp);
384 mtx_unlock(&sc->queue_mtx);
385
386 wakeup(sc);
387}
388
389DECLARE_GEOM_CLASS(g_md_class, g_md);
390
391
392static int
393mdstart_malloc(struct md_s *sc, struct bio *bp)
394{
395 int i, error;
396 u_char *dst;
397 unsigned secno, nsec, uc;
398 uintptr_t sp, osp;
399
400 nsec = bp->bio_bcount / sc->secsize;
401 secno = bp->bio_pblkno;
402 dst = bp->bio_data;
403 error = 0;
404 while (nsec--) {
405 osp = s_read(sc->indir, secno);
406 if (bp->bio_cmd == BIO_DELETE) {
407 if (osp != 0)
408 error = s_write(sc->indir, secno, 0);
409 } else if (bp->bio_cmd == BIO_READ) {
410 if (osp == 0)
411 bzero(dst, sc->secsize);
412 else if (osp <= 255)
413 for (i = 0; i < sc->secsize; i++)
414 dst[i] = osp;
415 else
416 bcopy((void *)osp, dst, sc->secsize);
417 osp = 0;
418 } else if (bp->bio_cmd == BIO_WRITE) {
419 if (sc->flags & MD_COMPRESS) {
420 uc = dst[0];
421 for (i = 1; i < sc->secsize; i++)
422 if (dst[i] != uc)
423 break;
424 } else {
425 i = 0;
426 uc = 0;
427 }
428 if (i == sc->secsize) {
429 if (osp != uc)
430 error = s_write(sc->indir, secno, uc);
431 } else {
432 if (osp <= 255) {
433 sp = (uintptr_t) uma_zalloc(
434 sc->uma, M_NOWAIT);
435 if (sp == 0) {
436 error = ENOSPC;
437 break;
438 }
439 bcopy(dst, (void *)sp, sc->secsize);
440 error = s_write(sc->indir, secno, sp);
441 } else {
442 bcopy(dst, (void *)osp, sc->secsize);
443 osp = 0;
444 }
445 }
446 } else {
447 error = EOPNOTSUPP;
448 }
449 if (osp > 255)
450 uma_zfree(sc->uma, (void*)osp);
451 if (error)
452 break;
453 secno++;
454 dst += sc->secsize;
455 }
456 bp->bio_resid = 0;
457 return (error);
458}
459
460static int
461mdstart_preload(struct md_s *sc, struct bio *bp)
462{
463
464 if (bp->bio_cmd == BIO_DELETE) {
465 } else if (bp->bio_cmd == BIO_READ) {
466 bcopy(sc->pl_ptr + (bp->bio_pblkno << DEV_BSHIFT), bp->bio_data, bp->bio_bcount);
467 } else {
468 bcopy(bp->bio_data, sc->pl_ptr + (bp->bio_pblkno << DEV_BSHIFT), bp->bio_bcount);
469 }
470 bp->bio_resid = 0;
471 return (0);
472}
473
474static int
475mdstart_vnode(struct md_s *sc, struct bio *bp)
476{
477 int error;
478 struct uio auio;
479 struct iovec aiov;
480 struct mount *mp;
481
482 /*
483 * VNODE I/O
484 *
485 * If an error occurs, we set BIO_ERROR but we do not set
486 * B_INVAL because (for a write anyway), the buffer is
487 * still valid.
488 */
489
490 bzero(&auio, sizeof(auio));
491
492 aiov.iov_base = bp->bio_data;
493 aiov.iov_len = bp->bio_bcount;
494 auio.uio_iov = &aiov;
495 auio.uio_iovcnt = 1;
496 auio.uio_offset = (vm_ooffset_t)bp->bio_pblkno * sc->secsize;
497 auio.uio_segflg = UIO_SYSSPACE;
498 if(bp->bio_cmd == BIO_READ)
499 auio.uio_rw = UIO_READ;
500 else
501 auio.uio_rw = UIO_WRITE;
502 auio.uio_resid = bp->bio_bcount;
503 auio.uio_td = curthread;
504 /*
505 * When reading set IO_DIRECT to try to avoid double-caching
506 * the data. When writing IO_DIRECT is not optimal, but we
507 * must set IO_NOWDRAIN to avoid a wdrain deadlock.
508 */
509 if (bp->bio_cmd == BIO_READ) {
510 vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY, curthread);
511 error = VOP_READ(sc->vnode, &auio, IO_DIRECT, sc->cred);
512 } else {
513 (void) vn_start_write(sc->vnode, &mp, V_WAIT);
514 vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY, curthread);
515 error = VOP_WRITE(sc->vnode, &auio, IO_NOWDRAIN, sc->cred);
516 vn_finished_write(mp);
517 }
518 VOP_UNLOCK(sc->vnode, 0, curthread);
519 bp->bio_resid = auio.uio_resid;
520 return (error);
521}
522
523static void
524mddone_swap(struct bio *bp)
525{
526
527 bp->bio_completed = bp->bio_length - bp->bio_resid;
528 g_std_done(bp);
529}
530
531static int
532mdstart_swap(struct md_s *sc, struct bio *bp)
533{
534 {
535 struct bio *bp2;
536
537 bp2 = g_clone_bio(bp);
538 bp2->bio_done = mddone_swap;
539 bp2->bio_blkno = bp2->bio_offset >> DEV_BSHIFT;
540 bp2->bio_pblkno = bp2->bio_offset / sc->secsize;
541 bp2->bio_bcount = bp2->bio_length;
542 bp = bp2;
543 }
544
545 bp->bio_resid = 0;
546 if ((bp->bio_cmd == BIO_DELETE) && (sc->flags & MD_RESERVE))
547 biodone(bp);
548 else
549 vm_pager_strategy(sc->object, bp);
550 return (-1);
551}
552
553static void
554md_kthread(void *arg)
555{
556 struct md_s *sc;
557 struct bio *bp;
558 int error, hasgiant;
559
560 sc = arg;
561 curthread->td_base_pri = PRIBIO;
562
563 switch (sc->type) {
564 case MD_SWAP:
565 case MD_VNODE:
566 mtx_lock(&Giant);
567 hasgiant = 1;
568 break;
569 case MD_MALLOC:
570 case MD_PRELOAD:
571 default:
572 hasgiant = 0;
573 break;
574 }
575
576 for (;;) {
577 mtx_lock(&sc->queue_mtx);
578 bp = bioq_first(&sc->bio_queue);
579 if (bp)
580 bioq_remove(&sc->bio_queue, bp);
581 if (!bp) {
582 if (sc->flags & MD_SHUTDOWN) {
583 mtx_unlock(&sc->queue_mtx);
584 sc->procp = NULL;
585 wakeup(&sc->procp);
586 if (!hasgiant)
587 mtx_lock(&Giant);
588 kthread_exit(0);
589 }
590 msleep(sc, &sc->queue_mtx, PRIBIO | PDROP, "mdwait", 0);
591 continue;
592 }
593 mtx_unlock(&sc->queue_mtx);
594
595 switch (sc->type) {
596 case MD_MALLOC:
597 devstat_start_transaction(&sc->stats);
598 error = mdstart_malloc(sc, bp);
599 break;
600 case MD_PRELOAD:
601 devstat_start_transaction(&sc->stats);
602 error = mdstart_preload(sc, bp);
603 break;
604 case MD_VNODE:
605 devstat_start_transaction(&sc->stats);
606 error = mdstart_vnode(sc, bp);
607 break;
608 case MD_SWAP:
609 error = mdstart_swap(sc, bp);
610 break;
611 default:
612 panic("Impossible md(type)");
613 break;
614 }
615
616 if (error != -1) {
617 bp->bio_completed = bp->bio_length;
618 g_io_deliver(bp, error);
619 }
620 }
621}
622
623static struct md_s *
624mdfind(int unit)
625{
626 struct md_s *sc;
627
628 /* XXX: LOCK(unique unit numbers) */
629 LIST_FOREACH(sc, &md_softc_list, list) {
630 if (sc->unit == unit)
631 break;
632 }
633 /* XXX: UNLOCK(unique unit numbers) */
634 return (sc);
635}
636
637static struct md_s *
638mdnew(int unit)
639{
640 struct md_s *sc;
641 int error, max = -1;
642
643 /* XXX: LOCK(unique unit numbers) */
644 LIST_FOREACH(sc, &md_softc_list, list) {
645 if (sc->unit == unit) {
646 /* XXX: UNLOCK(unique unit numbers) */
647 return (NULL);
648 }
649 if (sc->unit > max)
650 max = sc->unit;
651 }
652 if (unit == -1)
653 unit = max + 1;
654 if (unit > 255)
655 return (NULL);
656 sc = (struct md_s *)malloc(sizeof *sc, M_MD, M_WAITOK | M_ZERO);
657 sc->unit = unit;
658 bioq_init(&sc->bio_queue);
659 mtx_init(&sc->queue_mtx, "md bio queue", NULL, MTX_DEF);
660 sprintf(sc->name, "md%d", unit);
661 error = kthread_create(md_kthread, sc, &sc->procp, 0, 0,"%s", sc->name);
662 if (error) {
663 free(sc, M_MD);
664 return (NULL);
665 }
666 LIST_INSERT_HEAD(&md_softc_list, sc, list);
667 /* XXX: UNLOCK(unique unit numbers) */
668 return (sc);
669}
670
671static void
672mdinit(struct md_s *sc)
673{
674
675 devstat_add_entry(&sc->stats, MD_NAME, sc->unit, sc->secsize,
676 DEVSTAT_NO_ORDERED_TAGS,
677 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_OTHER,
678 DEVSTAT_PRIORITY_OTHER);
679 {
680 struct g_geom *gp;
681 struct g_provider *pp;
682
683 DROP_GIANT();
684 g_topology_lock();
685 gp = g_new_geomf(&g_md_class, "md%d", sc->unit);
686 gp->start = g_md_start;
687 gp->access = g_md_access;
688 gp->softc = sc;
689 pp = g_new_providerf(gp, "md%d", sc->unit);
690 pp->mediasize = (off_t)sc->nsect * sc->secsize;
691 pp->sectorsize = sc->secsize;
692 sc->gp = gp;
693 sc->pp = pp;
694 g_error_provider(pp, 0);
695 g_topology_unlock();
696 PICKUP_GIANT();
697 }
698}
699
700/*
701 * XXX: we should check that the range they feed us is mapped.
702 * XXX: we should implement read-only.
703 */
704
705static int
706mdcreate_preload(struct md_ioctl *mdio)
707{
708 struct md_s *sc;
709
710 if (mdio->md_size == 0)
711 return (EINVAL);
712 if (mdio->md_options & ~(MD_AUTOUNIT))
713 return (EINVAL);
714 if (mdio->md_options & MD_AUTOUNIT) {
715 sc = mdnew(-1);
716 if (sc == NULL)
717 return (ENOMEM);
718 mdio->md_unit = sc->unit;
719 } else {
720 sc = mdnew(mdio->md_unit);
721 if (sc == NULL)
722 return (EBUSY);
723 }
724 sc->type = MD_PRELOAD;
725 sc->secsize = DEV_BSIZE;
726 sc->nsect = mdio->md_size;
727 sc->flags = mdio->md_options & MD_FORCE;
728 /* Cast to pointer size, then to pointer to avoid warning */
729 sc->pl_ptr = (u_char *)(uintptr_t)mdio->md_base;
730 sc->pl_len = (mdio->md_size << DEV_BSHIFT);
731 mdinit(sc);
732 return (0);
733}
734
735
736static int
737mdcreate_malloc(struct md_ioctl *mdio)
738{
739 struct md_s *sc;
740 off_t u;
741 uintptr_t sp;
742 int error;
743
744 error = 0;
745 if (mdio->md_size == 0)
746 return (EINVAL);
747 if (mdio->md_options & ~(MD_AUTOUNIT | MD_COMPRESS | MD_RESERVE))
748 return (EINVAL);
749 /* Compression doesn't make sense if we have reserved space */
750 if (mdio->md_options & MD_RESERVE)
751 mdio->md_options &= ~MD_COMPRESS;
752 if (mdio->md_options & MD_AUTOUNIT) {
753 sc = mdnew(-1);
754 if (sc == NULL)
755 return (ENOMEM);
756 mdio->md_unit = sc->unit;
757 } else {
758 sc = mdnew(mdio->md_unit);
759 if (sc == NULL)
760 return (EBUSY);
761 }
762 sc->type = MD_MALLOC;
763 sc->secsize = DEV_BSIZE;
764 sc->nsect = mdio->md_size;
765 sc->flags = mdio->md_options & (MD_COMPRESS | MD_FORCE);
766 sc->indir = dimension(sc->nsect);
767 sc->uma = uma_zcreate(sc->name, sc->secsize,
768 NULL, NULL, NULL, NULL, 0x1ff, 0);
769 if (mdio->md_options & MD_RESERVE) {
770 for (u = 0; u < sc->nsect; u++) {
771 sp = (uintptr_t) uma_zalloc(sc->uma, M_NOWAIT | M_ZERO);
772 if (sp != 0)
773 error = s_write(sc->indir, u, sp);
774 else
775 error = ENOMEM;
776 if (error)
777 break;
778 }
779 }
780 if (error) {
781 mddestroy(sc, NULL);
782 return (error);
783 }
784 mdinit(sc);
785 if (!(mdio->md_options & MD_RESERVE))
786 sc->pp->flags |= G_PF_CANDELETE;
787 return (0);
788}
789
790
791static int
792mdsetcred(struct md_s *sc, struct ucred *cred)
793{
794 char *tmpbuf;
795 int error = 0;
796
797 /*
798 * Set credits in our softc
799 */
800
801 if (sc->cred)
802 crfree(sc->cred);
803 sc->cred = crhold(cred);
804
805 /*
806 * Horrible kludge to establish credentials for NFS XXX.
807 */
808
809 if (sc->vnode) {
810 struct uio auio;
811 struct iovec aiov;
812
813 tmpbuf = malloc(sc->secsize, M_TEMP, M_WAITOK);
814 bzero(&auio, sizeof(auio));
815
816 aiov.iov_base = tmpbuf;
817 aiov.iov_len = sc->secsize;
818 auio.uio_iov = &aiov;
819 auio.uio_iovcnt = 1;
820 auio.uio_offset = 0;
821 auio.uio_rw = UIO_READ;
822 auio.uio_segflg = UIO_SYSSPACE;
823 auio.uio_resid = aiov.iov_len;
824 vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY, curthread);
825 error = VOP_READ(sc->vnode, &auio, 0, sc->cred);
826 VOP_UNLOCK(sc->vnode, 0, curthread);
827 free(tmpbuf, M_TEMP);
828 }
829 return (error);
830}
831
832static int
833mdcreate_vnode(struct md_ioctl *mdio, struct thread *td)
834{
835 struct md_s *sc;
836 struct vattr vattr;
837 struct nameidata nd;
838 int error, flags;
839
840 flags = FREAD|FWRITE;
841 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, mdio->md_file, td);
842 error = vn_open(&nd, &flags, 0);
843 if (error) {
844 if (error != EACCES && error != EPERM && error != EROFS)
845 return (error);
846 flags &= ~FWRITE;
847 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, mdio->md_file, td);
848 error = vn_open(&nd, &flags, 0);
849 if (error)
850 return (error);
851 }
852 NDFREE(&nd, NDF_ONLY_PNBUF);
853 if (nd.ni_vp->v_type != VREG ||
854 (error = VOP_GETATTR(nd.ni_vp, &vattr, td->td_ucred, td))) {
855 VOP_UNLOCK(nd.ni_vp, 0, td);
856 (void) vn_close(nd.ni_vp, flags, td->td_ucred, td);
857 return (error ? error : EINVAL);
858 }
859 VOP_UNLOCK(nd.ni_vp, 0, td);
860
861 if (mdio->md_options & MD_AUTOUNIT) {
862 sc = mdnew(-1);
863 mdio->md_unit = sc->unit;
864 } else {
865 sc = mdnew(mdio->md_unit);
866 }
867 if (sc == NULL) {
868 (void) vn_close(nd.ni_vp, flags, td->td_ucred, td);
869 return (EBUSY);
870 }
871
872 sc->type = MD_VNODE;
873 sc->flags = mdio->md_options & MD_FORCE;
874 if (!(flags & FWRITE))
875 sc->flags |= MD_READONLY;
876 sc->secsize = DEV_BSIZE;
877 sc->vnode = nd.ni_vp;
878
879 /*
880 * If the size is specified, override the file attributes.
881 */
882 if (mdio->md_size)
883 sc->nsect = mdio->md_size;
884 else
885 sc->nsect = vattr.va_size / sc->secsize; /* XXX: round up ? */
886 if (sc->nsect == 0) {
887 mddestroy(sc, td);
888 return (EINVAL);
889 }
890 error = mdsetcred(sc, td->td_ucred);
891 if (error) {
892 mddestroy(sc, td);
893 return (error);
894 }
895 mdinit(sc);
896 return (0);
897}
898
899static int
900mddestroy(struct md_s *sc, struct thread *td)
901{
902
903 GIANT_REQUIRED;
904
905 mtx_destroy(&sc->queue_mtx);
906 devstat_remove_entry(&sc->stats);
907 {
908 if (sc->gp) {
909 sc->gp->flags |= G_GEOM_WITHER;
910 sc->gp->softc = NULL;
911 }
912 if (sc->pp)
913 g_orphan_provider(sc->pp, ENXIO);
914 }
915 sc->flags |= MD_SHUTDOWN;
916 wakeup(sc);
917 while (sc->procp != NULL)
918 tsleep(&sc->procp, PRIBIO, "mddestroy", hz / 10);
919 if (sc->vnode != NULL)
920 (void)vn_close(sc->vnode, sc->flags & MD_READONLY ?
921 FREAD : (FREAD|FWRITE), sc->cred, td);
922 if (sc->cred != NULL)
923 crfree(sc->cred);
924 if (sc->object != NULL) {
925 vm_pager_deallocate(sc->object);
926 }
927 if (sc->indir)
928 destroy_indir(sc, sc->indir);
929 if (sc->uma)
930 uma_zdestroy(sc->uma);
931
932 /* XXX: LOCK(unique unit numbers) */
933 LIST_REMOVE(sc, list);
934 /* XXX: UNLOCK(unique unit numbers) */
935 free(sc, M_MD);
936 return (0);
937}
938
939static int
940mdcreate_swap(struct md_ioctl *mdio, struct thread *td)
941{
942 int error;
943 struct md_s *sc;
944
945 GIANT_REQUIRED;
946
947 if (mdio->md_options & MD_AUTOUNIT) {
948 sc = mdnew(-1);
949 mdio->md_unit = sc->unit;
950 } else {
951 sc = mdnew(mdio->md_unit);
952 }
953 if (sc == NULL)
954 return (EBUSY);
955
956 sc->type = MD_SWAP;
957
958 /*
959 * Range check. Disallow negative sizes or any size less then the
960 * size of a page. Then round to a page.
961 */
962
963 if (mdio->md_size == 0) {
964 mddestroy(sc, td);
965 return (EDOM);
966 }
967
968 /*
969 * Allocate an OBJT_SWAP object.
970 *
971 * sc_secsize is PAGE_SIZE'd
972 *
973 * mdio->size is in DEV_BSIZE'd chunks.
974 * Note the truncation.
975 */
976
977 sc->secsize = PAGE_SIZE;
978 sc->nsect = mdio->md_size / (PAGE_SIZE / DEV_BSIZE);
979 sc->object = vm_pager_allocate(OBJT_SWAP, NULL, sc->secsize * (vm_offset_t)sc->nsect, VM_PROT_DEFAULT, 0);
980 sc->flags = mdio->md_options & MD_FORCE;
981 if (mdio->md_options & MD_RESERVE) {
982 if (swap_pager_reserve(sc->object, 0, sc->nsect) < 0) {
983 vm_pager_deallocate(sc->object);
984 sc->object = NULL;
985 mddestroy(sc, td);
986 return (EDOM);
987 }
988 }
989 error = mdsetcred(sc, td->td_ucred);
990 if (error) {
991 mddestroy(sc, td);
992 return (error);
993 }
994 mdinit(sc);
995 if (!(mdio->md_options & MD_RESERVE))
996 sc->pp->flags |= G_PF_CANDELETE;
997 return (0);
998}
999
1000static int
1001mddetach(int unit, struct thread *td)
1002{
1003 struct md_s *sc;
1004
1005 sc = mdfind(unit);
1006 if (sc == NULL)
1007 return (ENOENT);
1008 if (sc->opencount != 0 && !(sc->flags & MD_FORCE))
1009 return (EBUSY);
1010 switch(sc->type) {
1011 case MD_VNODE:
1012 case MD_SWAP:
1013 case MD_MALLOC:
1014 case MD_PRELOAD:
1015 return (mddestroy(sc, td));
1016 default:
1017 return (EOPNOTSUPP);
1018 }
1019}
1020
1021static int
1022mdctlioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
1023{
1024 struct md_ioctl *mdio;
1025 struct md_s *sc;
1026 int i;
1027
1028 if (md_debug)
1029 printf("mdctlioctl(%s %lx %p %x %p)\n",
1030 devtoname(dev), cmd, addr, flags, td);
1031
1032 /*
1033 * We assert the version number in the individual ioctl
1034 * handlers instead of out here because (a) it is possible we
1035 * may add another ioctl in the future which doesn't read an
1036 * mdio, and (b) the correct return value for an unknown ioctl
1037 * is ENOIOCTL, not EINVAL.
1038 */
1039 mdio = (struct md_ioctl *)addr;
1040 switch (cmd) {
1041 case MDIOCATTACH:
1042 if (mdio->md_version != MDIOVERSION)
1043 return (EINVAL);
1044 switch (mdio->md_type) {
1045 case MD_MALLOC:
1046 return (mdcreate_malloc(mdio));
1047 case MD_PRELOAD:
1048 return (mdcreate_preload(mdio));
1049 case MD_VNODE:
1050 return (mdcreate_vnode(mdio, td));
1051 case MD_SWAP:
1052 return (mdcreate_swap(mdio, td));
1053 default:
1054 return (EINVAL);
1055 }
1056 case MDIOCDETACH:
1057 if (mdio->md_version != MDIOVERSION)
1058 return (EINVAL);
1059 if (mdio->md_file != NULL || mdio->md_size != 0 ||
1060 mdio->md_options != 0)
1061 return (EINVAL);
1062 return (mddetach(mdio->md_unit, td));
1063 case MDIOCQUERY:
1064 if (mdio->md_version != MDIOVERSION)
1065 return (EINVAL);
1066 sc = mdfind(mdio->md_unit);
1067 if (sc == NULL)
1068 return (ENOENT);
1069 mdio->md_type = sc->type;
1070 mdio->md_options = sc->flags;
1071 switch (sc->type) {
1072 case MD_MALLOC:
1073 mdio->md_size = sc->nsect;
1074 break;
1075 case MD_PRELOAD:
1076 mdio->md_size = sc->nsect;
1077 mdio->md_base = (uint64_t)(intptr_t)sc->pl_ptr;
1078 break;
1079 case MD_SWAP:
1080 mdio->md_size = sc->nsect * (PAGE_SIZE / DEV_BSIZE);
1081 break;
1082 case MD_VNODE:
1083 mdio->md_size = sc->nsect;
1084 /* XXX fill this in */
1085 mdio->md_file = NULL;
1086 break;
1087 }
1088 return (0);
1089 case MDIOCLIST:
1090 i = 1;
1091 LIST_FOREACH(sc, &md_softc_list, list) {
1092 if (i == MDNPAD - 1)
1093 mdio->md_pad[i] = -1;
1094 else
1095 mdio->md_pad[i++] = sc->unit;
1096 }
1097 mdio->md_pad[0] = i - 1;
1098 return (0);
1099 default:
1100 return (ENOIOCTL);
1101 };
1102 return (ENOIOCTL);
1103}
1104
1105static void
1106md_preloaded(u_char *image, unsigned length)
1107{
1108 struct md_s *sc;
1109
1110 sc = mdnew(-1);
1111 if (sc == NULL)
1112 return;
1113 sc->type = MD_PRELOAD;
1114 sc->secsize = DEV_BSIZE;
1115 sc->nsect = length / DEV_BSIZE;
1116 sc->pl_ptr = image;
1117 sc->pl_len = length;
1118 if (sc->unit == 0)
1119 mdrootready = 1;
1120 mdinit(sc);
1121}
1122
1123static void
1124md_drvinit(void *unused)
1125{
1126
1127 caddr_t mod;
1128 caddr_t c;
1129 u_char *ptr, *name, *type;
1130 unsigned len;
1131
1132#ifdef MD_ROOT_SIZE
1133 md_preloaded(mfs_root, MD_ROOT_SIZE*1024);
1134#endif
1135 mod = NULL;
1136 while ((mod = preload_search_next_name(mod)) != NULL) {
1137 name = (char *)preload_search_info(mod, MODINFO_NAME);
1138 type = (char *)preload_search_info(mod, MODINFO_TYPE);
1139 if (name == NULL)
1140 continue;
1141 if (type == NULL)
1142 continue;
1143 if (strcmp(type, "md_image") && strcmp(type, "mfs_root"))
1144 continue;
1145 c = preload_search_info(mod, MODINFO_ADDR);
1146 ptr = *(u_char **)c;
1147 c = preload_search_info(mod, MODINFO_SIZE);
1148 len = *(size_t *)c;
1149 printf("%s%d: Preloaded image <%s> %d bytes at %p\n",
1150 MD_NAME, mdunits, name, len, ptr);
1151 md_preloaded(ptr, len);
1152 }
1153 status_dev = make_dev(&mdctl_cdevsw, 0xffff00ff, UID_ROOT, GID_WHEEL,
1154 0600, MDCTL_NAME);
1155}
1156
1157static int
1158md_modevent(module_t mod, int type, void *data)
1159{
1160 int error;
1161 struct md_s *sc;
1162
1163 switch (type) {
1164 case MOD_LOAD:
1165 md_drvinit(NULL);
1166 break;
1167 case MOD_UNLOAD:
1168 LIST_FOREACH(sc, &md_softc_list, list) {
1169 error = mddetach(sc->unit, curthread);
1170 if (error != 0)
1171 return (error);
1172 }
1173 if (status_dev)
1174 destroy_dev(status_dev);
1175 status_dev = 0;
1176 break;
1177 default:
1178 break;
1179 }
1180 return (0);
1181}
1182
1183static moduledata_t md_mod = {
1184 MD_NAME,
1185 md_modevent,
1186 NULL
1187};
1188DECLARE_MODULE(md, md_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE+CDEV_MAJOR);
1189MODULE_VERSION(md, MD_MODVER);
1190
1191
1192#ifdef MD_ROOT
1193static void
1194md_takeroot(void *junk)
1195{
1196 if (mdrootready)
1197 rootdevnames[0] = "ufs:/dev/md0";
1198}
1199
1200SYSINIT(md_root, SI_SUB_MOUNT_ROOT, SI_ORDER_FIRST, md_takeroot, NULL);
1201#endif /* MD_ROOT */