vnd.c revision 1.70 1 /* $NetBSD: vnd.c,v 1.70 2001/01/07 18:09:01 fvdl Exp $ */
2
3 /*-
4 * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (c) 1988 University of Utah.
41 * Copyright (c) 1990, 1993
42 * The Regents of the University of California. All rights reserved.
43 *
44 * This code is derived from software contributed to Berkeley by
45 * the Systems Programming Group of the University of Utah Computer
46 * Science Department.
47 *
48 * Redistribution and use in source and binary forms, with or without
49 * modification, are permitted provided that the following conditions
50 * are met:
51 * 1. Redistributions of source code must retain the above copyright
52 * notice, this list of conditions and the following disclaimer.
53 * 2. Redistributions in binary form must reproduce the above copyright
54 * notice, this list of conditions and the following disclaimer in the
55 * documentation and/or other materials provided with the distribution.
56 * 3. All advertising materials mentioning features or use of this software
57 * must display the following acknowledgement:
58 * This product includes software developed by the University of
59 * California, Berkeley and its contributors.
60 * 4. Neither the name of the University nor the names of its contributors
61 * may be used to endorse or promote products derived from this software
62 * without specific prior written permission.
63 *
64 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
65 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
66 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
67 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
68 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
69 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
70 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
71 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
72 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
73 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
74 * SUCH DAMAGE.
75 *
76 * from: Utah $Hdr: vn.c 1.13 94/04/02$
77 *
78 * @(#)vn.c 8.9 (Berkeley) 5/14/95
79 */
80
81 /*
82 * Vnode disk driver.
83 *
84 * Block/character interface to a vnode. Allows one to treat a file
85 * as a disk (e.g. build a filesystem in it, mount it, etc.).
86 *
87 * NOTE 1: This uses the VOP_BMAP/VOP_STRATEGY interface to the vnode
88 * instead of a simple VOP_RDWR. We do this to avoid distorting the
89 * local buffer cache.
90 *
91 * NOTE 2: There is a security issue involved with this driver.
92 * Once mounted all access to the contents of the "mapped" file via
93 * the special file is controlled by the permissions on the special
94 * file, the protection of the mapped file is ignored (effectively,
95 * by using root credentials in all transactions).
96 *
97 * NOTE 3: Doesn't interact with leases, should it?
98 */
99
100 #include "fs_nfs.h"
101
102 #include <sys/param.h>
103 #include <sys/systm.h>
104 #include <sys/namei.h>
105 #include <sys/proc.h>
106 #include <sys/errno.h>
107 #include <sys/buf.h>
108 #include <sys/malloc.h>
109 #include <sys/ioctl.h>
110 #include <sys/disklabel.h>
111 #include <sys/device.h>
112 #include <sys/disk.h>
113 #include <sys/stat.h>
114 #include <sys/mount.h>
115 #include <sys/vnode.h>
116 #include <sys/file.h>
117 #include <sys/uio.h>
118 #include <sys/conf.h>
119
120 #include <miscfs/specfs/specdev.h>
121
122 #include <dev/vndvar.h>
123
124 #if defined(VNDDEBUG) && !defined(DEBUG)
125 #define DEBUG
126 #endif
127
128 #ifdef DEBUG
129 int dovndcluster = 1;
130 #define VDB_FOLLOW 0x01
131 #define VDB_INIT 0x02
132 #define VDB_IO 0x04
133 #define VDB_LABEL 0x08
134 int vnddebug = 0x00;
135 #endif
136
137 #define vndunit(x) DISKUNIT(x)
138
139 struct vndxfer {
140 struct buf *vx_bp; /* Pointer to parent buffer */
141 int vx_error;
142 int vx_pending; /* # of pending aux buffers */
143 int vx_flags;
144 #define VX_BUSY 1
145 };
146
147 struct vndbuf {
148 struct buf vb_buf;
149 struct vndxfer *vb_xfer;
150 };
151
152 #define VND_GETXFER(vnd) pool_get(&(vnd)->sc_vxpool, PR_NOWAIT)
153 #define VND_PUTXFER(vnd, vx) pool_put(&(vnd)->sc_vxpool, (vx))
154
155 #define VND_GETBUF(vnd) pool_get(&(vnd)->sc_vbpool, PR_NOWAIT)
156 #define VND_PUTBUF(vnd, vb) pool_put(&(vnd)->sc_vbpool, (vb))
157
158 struct vnd_softc *vnd_softc;
159 int numvnd = 0;
160
161 #define VNDLABELDEV(dev) \
162 (MAKEDISKDEV(major((dev)), vndunit((dev)), RAW_PART))
163
164 /* called by main() at boot time */
165 void vndattach __P((int));
166
167 void vndclear __P((struct vnd_softc *));
168 void vndstart __P((struct vnd_softc *));
169 int vndsetcred __P((struct vnd_softc *, struct ucred *));
170 void vndthrottle __P((struct vnd_softc *, struct vnode *));
171 void vndiodone __P((struct buf *));
172 void vndshutdown __P((void));
173
174 void vndgetdefaultlabel __P((struct vnd_softc *, struct disklabel *));
175 void vndgetdisklabel __P((dev_t));
176
177 static int vndlock __P((struct vnd_softc *));
178 static void vndunlock __P((struct vnd_softc *));
179
180 void
181 vndattach(num)
182 int num;
183 {
184 int i;
185 char *mem;
186
187 if (num <= 0)
188 return;
189 i = num * sizeof(struct vnd_softc);
190 mem = malloc(i, M_DEVBUF, M_NOWAIT);
191 if (mem == NULL) {
192 printf("WARNING: no memory for vnode disks\n");
193 return;
194 }
195 bzero(mem, i);
196 vnd_softc = (struct vnd_softc *)mem;
197 numvnd = num;
198
199 for (i = 0; i < numvnd; i++)
200 BUFQ_INIT(&vnd_softc[i].sc_tab);
201 }
202
203 int
204 vndopen(dev, flags, mode, p)
205 dev_t dev;
206 int flags, mode;
207 struct proc *p;
208 {
209 int unit = vndunit(dev);
210 struct vnd_softc *sc;
211 int error = 0, part, pmask;
212 struct disklabel *lp;
213
214 #ifdef DEBUG
215 if (vnddebug & VDB_FOLLOW)
216 printf("vndopen(0x%x, 0x%x, 0x%x, %p)\n", dev, flags, mode, p);
217 #endif
218 if (unit >= numvnd)
219 return (ENXIO);
220 sc = &vnd_softc[unit];
221
222 if ((error = vndlock(sc)) != 0)
223 return (error);
224
225 lp = sc->sc_dkdev.dk_label;
226
227 part = DISKPART(dev);
228 pmask = (1 << part);
229
230 /*
231 * If we're initialized, check to see if there are any other
232 * open partitions. If not, then it's safe to update the
233 * in-core disklabel.
234 */
235 if ((sc->sc_flags & VNF_INITED) && (sc->sc_dkdev.dk_openmask == 0))
236 vndgetdisklabel(dev);
237
238 /* Check that the partitions exists. */
239 if (part != RAW_PART) {
240 if (((sc->sc_flags & VNF_INITED) == 0) ||
241 ((part >= lp->d_npartitions) ||
242 (lp->d_partitions[part].p_fstype == FS_UNUSED))) {
243 error = ENXIO;
244 goto done;
245 }
246 }
247
248 /* Prevent our unit from being unconfigured while open. */
249 switch (mode) {
250 case S_IFCHR:
251 sc->sc_dkdev.dk_copenmask |= pmask;
252 break;
253
254 case S_IFBLK:
255 sc->sc_dkdev.dk_bopenmask |= pmask;
256 break;
257 }
258 sc->sc_dkdev.dk_openmask =
259 sc->sc_dkdev.dk_copenmask | sc->sc_dkdev.dk_bopenmask;
260
261 done:
262 vndunlock(sc);
263 return (error);
264 }
265
266 int
267 vndclose(dev, flags, mode, p)
268 dev_t dev;
269 int flags, mode;
270 struct proc *p;
271 {
272 int unit = vndunit(dev);
273 struct vnd_softc *sc;
274 int error = 0, part;
275
276 #ifdef DEBUG
277 if (vnddebug & VDB_FOLLOW)
278 printf("vndclose(0x%x, 0x%x, 0x%x, %p)\n", dev, flags, mode, p);
279 #endif
280
281 if (unit >= numvnd)
282 return (ENXIO);
283 sc = &vnd_softc[unit];
284
285 if ((error = vndlock(sc)) != 0)
286 return (error);
287
288 part = DISKPART(dev);
289
290 /* ...that much closer to allowing unconfiguration... */
291 switch (mode) {
292 case S_IFCHR:
293 sc->sc_dkdev.dk_copenmask &= ~(1 << part);
294 break;
295
296 case S_IFBLK:
297 sc->sc_dkdev.dk_bopenmask &= ~(1 << part);
298 break;
299 }
300 sc->sc_dkdev.dk_openmask =
301 sc->sc_dkdev.dk_copenmask | sc->sc_dkdev.dk_bopenmask;
302
303 vndunlock(sc);
304 return (0);
305 }
306
307 /*
308 * Break the request into bsize pieces and submit using VOP_BMAP/VOP_STRATEGY.
309 */
310 void
311 vndstrategy(bp)
312 struct buf *bp;
313 {
314 int unit = vndunit(bp->b_dev);
315 struct vnd_softc *vnd = &vnd_softc[unit];
316 struct vndxfer *vnx;
317 int s, bsize, resid;
318 off_t bn;
319 caddr_t addr;
320 int sz, flags, error, wlabel;
321 struct disklabel *lp;
322 struct partition *pp;
323
324 #ifdef DEBUG
325 if (vnddebug & VDB_FOLLOW)
326 printf("vndstrategy(%p): unit %d\n", bp, unit);
327 #endif
328 if ((vnd->sc_flags & VNF_INITED) == 0) {
329 bp->b_error = ENXIO;
330 bp->b_flags |= B_ERROR;
331 goto done;
332 }
333
334 /* If it's a nil transfer, wake up the top half now. */
335 if (bp->b_bcount == 0)
336 goto done;
337
338 lp = vnd->sc_dkdev.dk_label;
339
340 /*
341 * The transfer must be a whole number of blocks.
342 */
343 if ((bp->b_bcount % lp->d_secsize) != 0) {
344 bp->b_error = EINVAL;
345 bp->b_flags |= B_ERROR;
346 goto done;
347 }
348
349 /*
350 * Do bounds checking and adjust transfer. If there's an error,
351 * the bounds check will flag that for us.
352 */
353 wlabel = vnd->sc_flags & (VNF_WLABEL|VNF_LABELLING);
354 if (DISKPART(bp->b_dev) != RAW_PART)
355 if (bounds_check_with_label(bp, lp, wlabel) <= 0)
356 goto done;
357
358 bp->b_resid = bp->b_bcount;
359
360 /*
361 * Put the block number in terms of the logical blocksize
362 * of the "device".
363 */
364 bn = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
365
366 /*
367 * Translate the partition-relative block number to an absolute.
368 */
369 if (DISKPART(bp->b_dev) != RAW_PART) {
370 pp = &vnd->sc_dkdev.dk_label->d_partitions[DISKPART(bp->b_dev)];
371 bn += pp->p_offset;
372 }
373
374 /* ...and convert to a byte offset within the file. */
375 bn *= lp->d_secsize;
376
377 bsize = vnd->sc_vp->v_mount->mnt_stat.f_iosize;
378 addr = bp->b_data;
379 flags = (bp->b_flags & (B_READ|B_ASYNC)) | B_CALL;
380
381 /* Allocate a header for this transfer and link it to the buffer */
382 s = splbio();
383 vnx = VND_GETXFER(vnd);
384 splx(s);
385 vnx->vx_flags = VX_BUSY;
386 vnx->vx_error = 0;
387 vnx->vx_pending = 0;
388 vnx->vx_bp = bp;
389
390 for (resid = bp->b_resid; resid; resid -= sz) {
391 struct vndbuf *nbp;
392 struct vnode *vp;
393 daddr_t nbn;
394 int off, nra;
395
396 nra = 0;
397 vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY | LK_CANRECURSE);
398 error = VOP_BMAP(vnd->sc_vp, bn / bsize, &vp, &nbn, &nra);
399 VOP_UNLOCK(vnd->sc_vp, 0);
400
401 if (error == 0 && (long)nbn == -1)
402 error = EIO;
403
404 /*
405 * If there was an error or a hole in the file...punt.
406 * Note that we may have to wait for any operations
407 * that we have already fired off before releasing
408 * the buffer.
409 *
410 * XXX we could deal with holes here but it would be
411 * a hassle (in the write case).
412 */
413 if (error) {
414 s = splbio();
415 vnx->vx_error = error;
416 goto out;
417 }
418
419 #ifdef DEBUG
420 if (!dovndcluster)
421 nra = 0;
422 #endif
423
424 if ((off = bn % bsize) != 0)
425 sz = bsize - off;
426 else
427 sz = (1 + nra) * bsize;
428 if (resid < sz)
429 sz = resid;
430 #ifdef DEBUG
431 if (vnddebug & VDB_IO)
432 printf("vndstrategy: vp %p/%p bn 0x%qx/0x%x sz 0x%x\n",
433 vnd->sc_vp, vp, (long long)bn, nbn, sz);
434 #endif
435
436 s = splbio();
437 nbp = VND_GETBUF(vnd);
438 splx(s);
439 nbp->vb_buf.b_flags = flags;
440 nbp->vb_buf.b_bcount = sz;
441 nbp->vb_buf.b_bufsize = bp->b_bufsize;
442 nbp->vb_buf.b_error = 0;
443 nbp->vb_buf.b_data = addr;
444 nbp->vb_buf.b_blkno = nbp->vb_buf.b_rawblkno = nbn + btodb(off);
445 nbp->vb_buf.b_proc = bp->b_proc;
446 nbp->vb_buf.b_iodone = vndiodone;
447 nbp->vb_buf.b_vp = NULLVP;
448 LIST_INIT(&nbp->vb_buf.b_dep);
449
450 nbp->vb_xfer = vnx;
451
452 /*
453 * Just sort by block number
454 */
455 s = splbio();
456 if (vnx->vx_error != 0) {
457 VND_PUTBUF(vnd, nbp);
458 goto out;
459 }
460 vnx->vx_pending++;
461 bgetvp(vp, &nbp->vb_buf);
462 disksort_blkno(&vnd->sc_tab, &nbp->vb_buf);
463 vndstart(vnd);
464 splx(s);
465 bn += sz;
466 addr += sz;
467 }
468
469 s = splbio();
470
471 out: /* Arrive here at splbio */
472 vnx->vx_flags &= ~VX_BUSY;
473 if (vnx->vx_pending == 0) {
474 if (vnx->vx_error != 0) {
475 bp->b_error = vnx->vx_error;
476 bp->b_flags |= B_ERROR;
477 }
478 VND_PUTXFER(vnd, vnx);
479 biodone(bp);
480 }
481 splx(s);
482 return;
483
484 done:
485 biodone(bp);
486 }
487
488 /*
489 * Feed requests sequentially.
490 * We do it this way to keep from flooding NFS servers if we are connected
491 * to an NFS file. This places the burden on the client rather than the
492 * server.
493 */
494 void
495 vndstart(vnd)
496 struct vnd_softc *vnd;
497 {
498 struct buf *bp;
499
500 /*
501 * Dequeue now since lower level strategy routine might
502 * queue using same links
503 */
504
505 if ((vnd->sc_flags & VNF_BUSY) != 0)
506 return;
507
508 vnd->sc_flags |= VNF_BUSY;
509
510 while (vnd->sc_active < vnd->sc_maxactive) {
511 bp = BUFQ_FIRST(&vnd->sc_tab);
512 if (bp == NULL)
513 break;
514 BUFQ_REMOVE(&vnd->sc_tab, bp);
515 vnd->sc_active++;
516 #ifdef DEBUG
517 if (vnddebug & VDB_IO)
518 printf("vndstart(%ld): bp %p vp %p blkno 0x%x"
519 " flags %lx addr %p cnt 0x%lx\n",
520 (long) (vnd-vnd_softc), bp, bp->b_vp, bp->b_blkno,
521 bp->b_flags, bp->b_data, bp->b_bcount);
522 #endif
523
524 /* Instrumentation. */
525 disk_busy(&vnd->sc_dkdev);
526
527 if ((bp->b_flags & B_READ) == 0)
528 bp->b_vp->v_numoutput++;
529 VOP_STRATEGY(bp);
530 }
531 vnd->sc_flags &= ~VNF_BUSY;
532 }
533
534 void
535 vndiodone(bp)
536 struct buf *bp;
537 {
538 struct vndbuf *vbp = (struct vndbuf *) bp;
539 struct vndxfer *vnx = (struct vndxfer *)vbp->vb_xfer;
540 struct buf *pbp = vnx->vx_bp;
541 struct vnd_softc *vnd = &vnd_softc[vndunit(pbp->b_dev)];
542 int s, resid;
543
544 s = splbio();
545 #ifdef DEBUG
546 if (vnddebug & VDB_IO)
547 printf("vndiodone(%ld): vbp %p vp %p blkno 0x%x addr %p cnt 0x%lx\n",
548 (long) (vnd-vnd_softc), vbp, vbp->vb_buf.b_vp,
549 vbp->vb_buf.b_blkno, vbp->vb_buf.b_data,
550 vbp->vb_buf.b_bcount);
551 #endif
552
553 resid = vbp->vb_buf.b_bcount - vbp->vb_buf.b_resid;
554 pbp->b_resid -= resid;
555 disk_unbusy(&vnd->sc_dkdev, resid);
556 vnx->vx_pending--;
557
558 if (vbp->vb_buf.b_error) {
559 #ifdef DEBUG
560 if (vnddebug & VDB_IO)
561 printf("vndiodone: vbp %p error %d\n", vbp,
562 vbp->vb_buf.b_error);
563 #endif
564 vnx->vx_error = vbp->vb_buf.b_error;
565 }
566
567 if (vbp->vb_buf.b_vp != NULLVP)
568 brelvp(&vbp->vb_buf);
569
570 VND_PUTBUF(vnd, vbp);
571
572 /*
573 * Wrap up this transaction if it has run to completion or, in
574 * case of an error, when all auxiliary buffers have returned.
575 */
576 if (vnx->vx_error != 0) {
577 pbp->b_flags |= B_ERROR;
578 pbp->b_error = vnx->vx_error;
579 if ((vnx->vx_flags & VX_BUSY) == 0 && vnx->vx_pending == 0) {
580
581 #ifdef DEBUG
582 if (vnddebug & VDB_IO)
583 printf("vndiodone: pbp %p iodone: error %d\n",
584 pbp, vnx->vx_error);
585 #endif
586 VND_PUTXFER(vnd, vnx);
587 biodone(pbp);
588 }
589 } else if (pbp->b_resid == 0) {
590
591 #ifdef DIAGNOSTIC
592 if (vnx->vx_pending != 0)
593 panic("vndiodone: vnx pending: %d", vnx->vx_pending);
594 #endif
595
596 if ((vnx->vx_flags & VX_BUSY) == 0) {
597 #ifdef DEBUG
598 if (vnddebug & VDB_IO)
599 printf("vndiodone: pbp %p iodone\n", pbp);
600 #endif
601 VND_PUTXFER(vnd, vnx);
602 biodone(pbp);
603 }
604 }
605
606 vnd->sc_active--;
607 vndstart(vnd);
608 splx(s);
609 }
610
611 /* ARGSUSED */
612 int
613 vndread(dev, uio, flags)
614 dev_t dev;
615 struct uio *uio;
616 int flags;
617 {
618 int unit = vndunit(dev);
619 struct vnd_softc *sc;
620
621 #ifdef DEBUG
622 if (vnddebug & VDB_FOLLOW)
623 printf("vndread(0x%x, %p)\n", dev, uio);
624 #endif
625
626 if (unit >= numvnd)
627 return (ENXIO);
628 sc = &vnd_softc[unit];
629
630 if ((sc->sc_flags & VNF_INITED) == 0)
631 return (ENXIO);
632
633 return (physio(vndstrategy, NULL, dev, B_READ, minphys, uio));
634 }
635
636 /* ARGSUSED */
637 int
638 vndwrite(dev, uio, flags)
639 dev_t dev;
640 struct uio *uio;
641 int flags;
642 {
643 int unit = vndunit(dev);
644 struct vnd_softc *sc;
645
646 #ifdef DEBUG
647 if (vnddebug & VDB_FOLLOW)
648 printf("vndwrite(0x%x, %p)\n", dev, uio);
649 #endif
650
651 if (unit >= numvnd)
652 return (ENXIO);
653 sc = &vnd_softc[unit];
654
655 if ((sc->sc_flags & VNF_INITED) == 0)
656 return (ENXIO);
657
658 return (physio(vndstrategy, NULL, dev, B_WRITE, minphys, uio));
659 }
660
661 /* ARGSUSED */
662 int
663 vndioctl(dev, cmd, data, flag, p)
664 dev_t dev;
665 u_long cmd;
666 caddr_t data;
667 int flag;
668 struct proc *p;
669 {
670 int unit = vndunit(dev);
671 struct vnd_softc *vnd;
672 struct vnd_ioctl *vio;
673 struct vattr vattr;
674 struct nameidata nd;
675 int error, part, pmask;
676 size_t geomsize;
677 #ifdef __HAVE_OLD_DISKLABEL
678 struct disklabel newlabel;
679 #endif
680
681 #ifdef DEBUG
682 if (vnddebug & VDB_FOLLOW)
683 printf("vndioctl(0x%x, 0x%lx, %p, 0x%x, %p): unit %d\n",
684 dev, cmd, data, flag, p, unit);
685 #endif
686 error = suser(p->p_ucred, &p->p_acflag);
687 if (error)
688 return (error);
689 if (unit >= numvnd)
690 return (ENXIO);
691
692 vnd = &vnd_softc[unit];
693 vio = (struct vnd_ioctl *)data;
694
695 /* Must be open for writes for these commands... */
696 switch (cmd) {
697 case VNDIOCSET:
698 case VNDIOCCLR:
699 case DIOCSDINFO:
700 case DIOCWDINFO:
701 #ifdef __HAVE_OLD_DISKLABEL
702 case ODIOCSDINFO:
703 case ODIOCWDINFO:
704 #endif
705 case DIOCWLABEL:
706 if ((flag & FWRITE) == 0)
707 return (EBADF);
708 }
709
710 /* Must be initialized for these... */
711 switch (cmd) {
712 case VNDIOCCLR:
713 case DIOCGDINFO:
714 case DIOCSDINFO:
715 case DIOCWDINFO:
716 case DIOCGPART:
717 case DIOCWLABEL:
718 case DIOCGDEFLABEL:
719 #ifdef __HAVE_OLD_DISKLABEL
720 case ODIOCGDINFO:
721 case ODIOCSDINFO:
722 case ODIOCWDINFO:
723 case ODIOCGDEFLABEL:
724 #endif
725 if ((vnd->sc_flags & VNF_INITED) == 0)
726 return (ENXIO);
727 }
728
729 switch (cmd) {
730 case VNDIOCSET:
731 if (vnd->sc_flags & VNF_INITED)
732 return (EBUSY);
733
734 if ((error = vndlock(vnd)) != 0)
735 return (error);
736
737 /*
738 * Always open for read and write.
739 * This is probably bogus, but it lets vn_open()
740 * weed out directories, sockets, etc. so we don't
741 * have to worry about them.
742 */
743 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, vio->vnd_file, p);
744 if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
745 vndunlock(vnd);
746 return(error);
747 }
748 error = VOP_GETATTR(nd.ni_vp, &vattr, p->p_ucred, p);
749 if (error) {
750 VOP_UNLOCK(nd.ni_vp, 0);
751 (void) vn_close(nd.ni_vp, FREAD|FWRITE, p->p_ucred, p);
752 vndunlock(vnd);
753 return(error);
754 }
755 VOP_UNLOCK(nd.ni_vp, 0);
756 vnd->sc_vp = nd.ni_vp;
757 vnd->sc_size = btodb(vattr.va_size); /* note truncation */
758
759 /*
760 * Use pseudo-geometry specified. If none was provided,
761 * use "standard" Adaptec fictitious geometry.
762 */
763 if (vio->vnd_flags & VNDIOF_HASGEOM) {
764
765 bcopy(&vio->vnd_geom, &vnd->sc_geom,
766 sizeof(vio->vnd_geom));
767
768 /*
769 * Sanity-check the sector size.
770 * XXX Don't allow secsize < DEV_BSIZE. Should
771 * XXX we?
772 */
773 if (vnd->sc_geom.vng_secsize < DEV_BSIZE ||
774 (vnd->sc_geom.vng_secsize % DEV_BSIZE) != 0) {
775 (void) vn_close(nd.ni_vp, FREAD|FWRITE,
776 p->p_ucred, p);
777 vndunlock(vnd);
778 return (EINVAL);
779 }
780
781 /*
782 * Compute the size (in DEV_BSIZE blocks) specified
783 * by the geometry.
784 */
785 geomsize = (vnd->sc_geom.vng_nsectors *
786 vnd->sc_geom.vng_ntracks *
787 vnd->sc_geom.vng_ncylinders) *
788 (vnd->sc_geom.vng_secsize / DEV_BSIZE);
789
790 /*
791 * Sanity-check the size against the specified
792 * geometry.
793 */
794 if (vnd->sc_size < geomsize) {
795 (void) vn_close(nd.ni_vp, FREAD|FWRITE,
796 p->p_ucred, p);
797 vndunlock(vnd);
798 return (EINVAL);
799 }
800 } else {
801 /*
802 * Size must be at least 2048 DEV_BSIZE blocks
803 * (1M) in order to use this geometry.
804 */
805 if (vnd->sc_size < (32 * 64)) {
806 vndunlock(vnd);
807 return (EINVAL);
808 }
809
810 vnd->sc_geom.vng_secsize = DEV_BSIZE;
811 vnd->sc_geom.vng_nsectors = 32;
812 vnd->sc_geom.vng_ntracks = 64;
813 vnd->sc_geom.vng_ncylinders = vnd->sc_size / (64 * 32);
814
815 /*
816 * Compute the actual size allowed by this geometry.
817 */
818 geomsize = 32 * 64 * vnd->sc_geom.vng_ncylinders;
819 }
820
821 /*
822 * Truncate the size to that specified by
823 * the geometry.
824 * XXX Should we even bother with this?
825 */
826 vnd->sc_size = geomsize;
827
828 if ((error = vndsetcred(vnd, p->p_ucred)) != 0) {
829 (void) vn_close(nd.ni_vp, FREAD|FWRITE, p->p_ucred, p);
830 vndunlock(vnd);
831 return(error);
832 }
833 vndthrottle(vnd, vnd->sc_vp);
834 vio->vnd_size = dbtob(vnd->sc_size);
835 vnd->sc_flags |= VNF_INITED;
836 #ifdef DEBUG
837 if (vnddebug & VDB_INIT)
838 printf("vndioctl: SET vp %p size 0x%lx %d/%d/%d/%d\n",
839 vnd->sc_vp, (unsigned long) vnd->sc_size,
840 vnd->sc_geom.vng_secsize,
841 vnd->sc_geom.vng_nsectors,
842 vnd->sc_geom.vng_ntracks,
843 vnd->sc_geom.vng_ncylinders);
844 #endif
845
846 /* Attach the disk. */
847 bzero(vnd->sc_xname, sizeof(vnd->sc_xname)); /* XXX */
848 sprintf(vnd->sc_xname, "vnd%d", unit); /* XXX */
849 vnd->sc_dkdev.dk_name = vnd->sc_xname;
850 disk_attach(&vnd->sc_dkdev);
851
852 /* Initialize the xfer and buffer pools. */
853 pool_init(&vnd->sc_vxpool, sizeof(struct vndxfer), 0,
854 0, 0, "vndxpl", 0, NULL, NULL, M_DEVBUF);
855 pool_init(&vnd->sc_vbpool, sizeof(struct vndbuf), 0,
856 0, 0, "vndbpl", 0, NULL, NULL, M_DEVBUF);
857
858 /* Try and read the disklabel. */
859 vndgetdisklabel(dev);
860
861 vndunlock(vnd);
862
863 break;
864
865 case VNDIOCCLR:
866 if ((error = vndlock(vnd)) != 0)
867 return (error);
868
869 /*
870 * Don't unconfigure if any other partitions are open
871 * or if both the character and block flavors of this
872 * partition are open.
873 */
874 part = DISKPART(dev);
875 pmask = (1 << part);
876 if ((vnd->sc_dkdev.dk_openmask & ~pmask) ||
877 ((vnd->sc_dkdev.dk_bopenmask & pmask) &&
878 (vnd->sc_dkdev.dk_copenmask & pmask))) {
879 vndunlock(vnd);
880 return (EBUSY);
881 }
882
883 vndclear(vnd);
884 #ifdef DEBUG
885 if (vnddebug & VDB_INIT)
886 printf("vndioctl: CLRed\n");
887 #endif
888
889 /* Destroy the xfer and buffer pools. */
890 pool_destroy(&vnd->sc_vxpool);
891 pool_destroy(&vnd->sc_vbpool);
892
893 /* Detatch the disk. */
894 disk_detach(&vnd->sc_dkdev);
895
896 vndunlock(vnd);
897
898 break;
899
900 case DIOCGDINFO:
901 *(struct disklabel *)data = *(vnd->sc_dkdev.dk_label);
902 break;
903
904 #ifdef __HAVE_OLD_DISKLABEL
905 case ODIOCGDINFO:
906 newlabel = *(vnd->sc_dkdev.dk_label);
907 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
908 newlabel.d_npartitions = OLDMAXPARTITIONS;
909 memcpy(data, &newlabel, sizeof (struct olddisklabel));
910 break;
911 #endif
912
913 case DIOCGPART:
914 ((struct partinfo *)data)->disklab = vnd->sc_dkdev.dk_label;
915 ((struct partinfo *)data)->part =
916 &vnd->sc_dkdev.dk_label->d_partitions[DISKPART(dev)];
917 break;
918
919 case DIOCWDINFO:
920 case DIOCSDINFO:
921 #ifdef __HAVE_OLD_DISKLABEL
922 case ODIOCWDINFO:
923 case ODIOCSDINFO:
924 #endif
925 {
926 struct disklabel *lp;
927
928 if ((error = vndlock(vnd)) != 0)
929 return (error);
930
931 vnd->sc_flags |= VNF_LABELLING;
932
933 #ifdef __HAVE_OLD_DISKLABEL
934 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
935 memset(&newlabel, 0, sizeof newlabel);
936 memcpy(&newlabel, data, sizeof (struct olddisklabel));
937 lp = &newlabel;
938 } else
939 #endif
940 lp = (struct disklabel *)data;
941
942 error = setdisklabel(vnd->sc_dkdev.dk_label,
943 lp, 0, vnd->sc_dkdev.dk_cpulabel);
944 if (error == 0) {
945 if (cmd == DIOCWDINFO
946 #ifdef __HAVE_OLD_DISKLABEL
947 || cmd == ODIOCWDINFO
948 #endif
949 )
950 error = writedisklabel(VNDLABELDEV(dev),
951 vndstrategy, vnd->sc_dkdev.dk_label,
952 vnd->sc_dkdev.dk_cpulabel);
953 }
954
955 vnd->sc_flags &= ~VNF_LABELLING;
956
957 vndunlock(vnd);
958
959 if (error)
960 return (error);
961 break;
962 }
963
964 case DIOCWLABEL:
965 if (*(int *)data != 0)
966 vnd->sc_flags |= VNF_WLABEL;
967 else
968 vnd->sc_flags &= ~VNF_WLABEL;
969 break;
970
971 case DIOCGDEFLABEL:
972 vndgetdefaultlabel(vnd, (struct disklabel *)data);
973 break;
974
975 #ifdef __HAVE_OLD_DISKLABEL
976 case ODIOCGDEFLABEL:
977 vndgetdefaultlabel(vnd, &newlabel);
978 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
979 newlabel.d_npartitions = OLDMAXPARTITIONS;
980 memcpy(data, &newlabel, sizeof (struct olddisklabel));
981 break;
982 #endif
983
984 default:
985 return (ENOTTY);
986 }
987
988 return (0);
989 }
990
991 /*
992 * Duplicate the current processes' credentials. Since we are called only
993 * as the result of a SET ioctl and only root can do that, any future access
994 * to this "disk" is essentially as root. Note that credentials may change
995 * if some other uid can write directly to the mapped file (NFS).
996 */
997 int
998 vndsetcred(vnd, cred)
999 struct vnd_softc *vnd;
1000 struct ucred *cred;
1001 {
1002 struct uio auio;
1003 struct iovec aiov;
1004 char *tmpbuf;
1005 int error;
1006
1007 vnd->sc_cred = crdup(cred);
1008 tmpbuf = malloc(DEV_BSIZE, M_TEMP, M_WAITOK);
1009
1010 /* XXX: Horrible kludge to establish credentials for NFS */
1011 aiov.iov_base = tmpbuf;
1012 aiov.iov_len = min(DEV_BSIZE, dbtob(vnd->sc_size));
1013 auio.uio_iov = &aiov;
1014 auio.uio_iovcnt = 1;
1015 auio.uio_offset = 0;
1016 auio.uio_rw = UIO_READ;
1017 auio.uio_segflg = UIO_SYSSPACE;
1018 auio.uio_resid = aiov.iov_len;
1019 vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY);
1020 error = VOP_READ(vnd->sc_vp, &auio, 0, vnd->sc_cred);
1021 if (error == 0) {
1022 /*
1023 * Because vnd does all IO directly through the vnode
1024 * we need to flush (at least) the buffer from the above
1025 * VOP_READ from the buffer cache to prevent cache
1026 * incoherencies. Also, be careful to write dirty
1027 * buffers back to stable storage.
1028 */
1029 error = vinvalbuf(vnd->sc_vp, V_SAVE, vnd->sc_cred,
1030 curproc, 0, 0);
1031 }
1032 VOP_UNLOCK(vnd->sc_vp, 0);
1033
1034 free(tmpbuf, M_TEMP);
1035 return (error);
1036 }
1037
1038 /*
1039 * Set maxactive based on FS type
1040 */
1041 void
1042 vndthrottle(vnd, vp)
1043 struct vnd_softc *vnd;
1044 struct vnode *vp;
1045 {
1046 #ifdef NFS
1047 extern int (**nfsv2_vnodeop_p) __P((void *));
1048
1049 if (vp->v_op == nfsv2_vnodeop_p)
1050 vnd->sc_maxactive = 2;
1051 else
1052 #endif
1053 vnd->sc_maxactive = 8;
1054
1055 if (vnd->sc_maxactive < 1)
1056 vnd->sc_maxactive = 1;
1057 }
1058
1059 void
1060 vndshutdown()
1061 {
1062 struct vnd_softc *vnd;
1063
1064 for (vnd = &vnd_softc[0]; vnd < &vnd_softc[numvnd]; vnd++)
1065 if (vnd->sc_flags & VNF_INITED)
1066 vndclear(vnd);
1067 }
1068
1069 void
1070 vndclear(vnd)
1071 struct vnd_softc *vnd;
1072 {
1073 struct vnode *vp = vnd->sc_vp;
1074 struct proc *p = curproc; /* XXX */
1075
1076 #ifdef DEBUG
1077 if (vnddebug & VDB_FOLLOW)
1078 printf("vndclear(%p): vp %p\n", vnd, vp);
1079 #endif
1080 vnd->sc_flags &= ~VNF_INITED;
1081 if (vp == (struct vnode *)0)
1082 panic("vndioctl: null vp");
1083 (void) vn_close(vp, FREAD|FWRITE, vnd->sc_cred, p);
1084 crfree(vnd->sc_cred);
1085 vnd->sc_vp = (struct vnode *)0;
1086 vnd->sc_cred = (struct ucred *)0;
1087 vnd->sc_size = 0;
1088 }
1089
1090 int
1091 vndsize(dev)
1092 dev_t dev;
1093 {
1094 struct vnd_softc *sc;
1095 struct disklabel *lp;
1096 int part, unit, omask;
1097 int size;
1098
1099 unit = vndunit(dev);
1100 if (unit >= numvnd)
1101 return (-1);
1102 sc = &vnd_softc[unit];
1103
1104 if ((sc->sc_flags & VNF_INITED) == 0)
1105 return (-1);
1106
1107 part = DISKPART(dev);
1108 omask = sc->sc_dkdev.dk_openmask & (1 << part);
1109 lp = sc->sc_dkdev.dk_label;
1110
1111 if (omask == 0 && vndopen(dev, 0, S_IFBLK, curproc))
1112 return (-1);
1113
1114 if (lp->d_partitions[part].p_fstype != FS_SWAP)
1115 size = -1;
1116 else
1117 size = lp->d_partitions[part].p_size *
1118 (lp->d_secsize / DEV_BSIZE);
1119
1120 if (omask == 0 && vndclose(dev, 0, S_IFBLK, curproc))
1121 return (-1);
1122
1123 return (size);
1124 }
1125
1126 int
1127 vnddump(dev, blkno, va, size)
1128 dev_t dev;
1129 daddr_t blkno;
1130 caddr_t va;
1131 size_t size;
1132 {
1133
1134 /* Not implemented. */
1135 return ENXIO;
1136 }
1137
1138 void
1139 vndgetdefaultlabel(sc, lp)
1140 struct vnd_softc *sc;
1141 struct disklabel *lp;
1142 {
1143 struct vndgeom *vng = &sc->sc_geom;
1144 struct partition *pp;
1145
1146 bzero(lp, sizeof(*lp));
1147
1148 lp->d_secperunit = sc->sc_size / (vng->vng_secsize / DEV_BSIZE);
1149 lp->d_secsize = vng->vng_secsize;
1150 lp->d_nsectors = vng->vng_nsectors;
1151 lp->d_ntracks = vng->vng_ntracks;
1152 lp->d_ncylinders = vng->vng_ncylinders;
1153 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1154
1155 strncpy(lp->d_typename, "vnd", sizeof(lp->d_typename));
1156 lp->d_type = DTYPE_VND;
1157 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
1158 lp->d_rpm = 3600;
1159 lp->d_interleave = 1;
1160 lp->d_flags = 0;
1161
1162 pp = &lp->d_partitions[RAW_PART];
1163 pp->p_offset = 0;
1164 pp->p_size = lp->d_secperunit;
1165 pp->p_fstype = FS_UNUSED;
1166 lp->d_npartitions = RAW_PART + 1;
1167
1168 lp->d_magic = DISKMAGIC;
1169 lp->d_magic2 = DISKMAGIC;
1170 lp->d_checksum = dkcksum(lp);
1171 }
1172
1173 /*
1174 * Read the disklabel from a vnd. If one is not present, create a fake one.
1175 */
1176 void
1177 vndgetdisklabel(dev)
1178 dev_t dev;
1179 {
1180 struct vnd_softc *sc = &vnd_softc[vndunit(dev)];
1181 char *errstring;
1182 struct disklabel *lp = sc->sc_dkdev.dk_label;
1183 struct cpu_disklabel *clp = sc->sc_dkdev.dk_cpulabel;
1184 int i;
1185
1186 bzero(clp, sizeof(*clp));
1187
1188 vndgetdefaultlabel(sc, lp);
1189
1190 /*
1191 * Call the generic disklabel extraction routine.
1192 */
1193 errstring = readdisklabel(VNDLABELDEV(dev), vndstrategy, lp, clp);
1194 if (errstring) {
1195 /*
1196 * Lack of disklabel is common, but we print the warning
1197 * anyway, since it might contain other useful information.
1198 */
1199 printf("%s: %s\n", sc->sc_xname, errstring);
1200
1201 /*
1202 * For historical reasons, if there's no disklabel
1203 * present, all partitions must be FS_BSDFFS and
1204 * occupy the entire disk.
1205 */
1206 for (i = 0; i < MAXPARTITIONS; i++) {
1207 /*
1208 * Don't wipe out port specific hack (such as
1209 * dos partition hack of i386 port).
1210 */
1211 if (lp->d_partitions[i].p_fstype != FS_UNUSED)
1212 continue;
1213
1214 lp->d_partitions[i].p_size = lp->d_secperunit;
1215 lp->d_partitions[i].p_offset = 0;
1216 lp->d_partitions[i].p_fstype = FS_BSDFFS;
1217 }
1218
1219 strncpy(lp->d_packname, "default label",
1220 sizeof(lp->d_packname));
1221
1222 lp->d_checksum = dkcksum(lp);
1223 }
1224 }
1225
1226 /*
1227 * Wait interruptibly for an exclusive lock.
1228 *
1229 * XXX
1230 * Several drivers do this; it should be abstracted and made MP-safe.
1231 */
1232 static int
1233 vndlock(sc)
1234 struct vnd_softc *sc;
1235 {
1236 int error;
1237
1238 while ((sc->sc_flags & VNF_LOCKED) != 0) {
1239 sc->sc_flags |= VNF_WANTED;
1240 if ((error = tsleep(sc, PRIBIO | PCATCH, "vndlck", 0)) != 0)
1241 return (error);
1242 }
1243 sc->sc_flags |= VNF_LOCKED;
1244 return (0);
1245 }
1246
1247 /*
1248 * Unlock and wake up any waiters.
1249 */
1250 static void
1251 vndunlock(sc)
1252 struct vnd_softc *sc;
1253 {
1254
1255 sc->sc_flags &= ~VNF_LOCKED;
1256 if ((sc->sc_flags & VNF_WANTED) != 0) {
1257 sc->sc_flags &= ~VNF_WANTED;
1258 wakeup(sc);
1259 }
1260 }
1261