vnd.c revision 1.100 1 /* $NetBSD: vnd.c,v 1.100 2003/06/28 14:21:32 darrenr 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 <sys/cdefs.h>
101 __KERNEL_RCSID(0, "$NetBSD: vnd.c,v 1.100 2003/06/28 14:21:32 darrenr Exp $");
102
103 #if defined(_KERNEL_OPT)
104 #include "fs_nfs.h"
105 #endif
106
107 #include <sys/param.h>
108 #include <sys/systm.h>
109 #include <sys/namei.h>
110 #include <sys/proc.h>
111 #include <sys/errno.h>
112 #include <sys/buf.h>
113 #include <sys/malloc.h>
114 #include <sys/ioctl.h>
115 #include <sys/disklabel.h>
116 #include <sys/device.h>
117 #include <sys/disk.h>
118 #include <sys/stat.h>
119 #include <sys/mount.h>
120 #include <sys/vnode.h>
121 #include <sys/file.h>
122 #include <sys/uio.h>
123 #include <sys/conf.h>
124
125 #include <miscfs/specfs/specdev.h>
126
127 #include <dev/vndvar.h>
128
129 #if defined(VNDDEBUG) && !defined(DEBUG)
130 #define DEBUG
131 #endif
132
133 #ifdef DEBUG
134 int dovndcluster = 1;
135 #define VDB_FOLLOW 0x01
136 #define VDB_INIT 0x02
137 #define VDB_IO 0x04
138 #define VDB_LABEL 0x08
139 int vnddebug = 0x00;
140 #endif
141
142 #define vndunit(x) DISKUNIT(x)
143
144 struct vndxfer {
145 struct buf *vx_bp; /* Pointer to parent buffer */
146 int vx_error;
147 int vx_pending; /* # of pending aux buffers */
148 int vx_flags;
149 #define VX_BUSY 1
150 };
151
152 struct vndbuf {
153 struct buf vb_buf;
154 struct vndxfer *vb_xfer;
155 };
156
157 #define VND_GETXFER(vnd) pool_get(&(vnd)->sc_vxpool, PR_NOWAIT)
158 #define VND_PUTXFER(vnd, vx) pool_put(&(vnd)->sc_vxpool, (vx))
159
160 #define VND_GETBUF(vnd) pool_get(&(vnd)->sc_vbpool, PR_NOWAIT)
161 #define VND_PUTBUF(vnd, vb) pool_put(&(vnd)->sc_vbpool, (vb))
162
163 struct vnd_softc *vnd_softc;
164 int numvnd = 0;
165
166 #define VNDLABELDEV(dev) \
167 (MAKEDISKDEV(major((dev)), vndunit((dev)), RAW_PART))
168
169 /* called by main() at boot time (XXX: and the LKM driver) */
170 void vndattach __P((int));
171 int vnddetach __P((void));
172
173 void vndclear __P((struct vnd_softc *, int));
174 void vndstart __P((struct vnd_softc *));
175 int vndsetcred __P((struct vnd_softc *, struct ucred *));
176 void vndthrottle __P((struct vnd_softc *, struct vnode *));
177 void vndiodone __P((struct buf *));
178 #if 0
179 void vndshutdown __P((void));
180 #endif
181
182 void vndgetdefaultlabel __P((struct vnd_softc *, struct disklabel *));
183 void vndgetdisklabel __P((dev_t));
184
185 static int vndlock __P((struct vnd_softc *));
186 static void vndunlock __P((struct vnd_softc *));
187
188 dev_type_open(vndopen);
189 dev_type_close(vndclose);
190 dev_type_read(vndread);
191 dev_type_write(vndwrite);
192 dev_type_ioctl(vndioctl);
193 dev_type_strategy(vndstrategy);
194 dev_type_dump(vnddump);
195 dev_type_size(vndsize);
196
197 const struct bdevsw vnd_bdevsw = {
198 vndopen, vndclose, vndstrategy, vndioctl, vnddump, vndsize, D_DISK
199 };
200
201 const struct cdevsw vnd_cdevsw = {
202 vndopen, vndclose, vndread, vndwrite, vndioctl,
203 nostop, notty, nopoll, nommap, nokqfilter, D_DISK
204 };
205
206 int vndattached = 0;
207
208 void
209 vndattach(num)
210 int num;
211 {
212 int i;
213 char *mem;
214
215 if (vndattached)
216 return;
217 vndattached = 1;
218 if (num <= 0)
219 return;
220 i = num * sizeof(struct vnd_softc);
221 mem = malloc(i, M_DEVBUF, M_NOWAIT|M_ZERO);
222 if (mem == NULL) {
223 printf("WARNING: no memory for vnode disks\n");
224 return;
225 }
226 vnd_softc = (struct vnd_softc *)mem;
227 numvnd = num;
228
229 for (i = 0; i < numvnd; i++) {
230 vnd_softc[i].sc_unit = i;
231 bufq_alloc(&vnd_softc[i].sc_tab,
232 BUFQ_DISKSORT|BUFQ_SORT_RAWBLOCK);
233 }
234 }
235
236 int
237 vnddetach()
238 {
239 int i;
240
241 /* First check we aren't in use. */
242 for (i = 0; i < numvnd; i++)
243 if (vnd_softc[i].sc_flags & VNF_INITED)
244 return (EBUSY);
245
246 for (i = 0; i < numvnd; i++)
247 bufq_free(&vnd_softc[i].sc_tab);
248
249 free(vnd_softc, M_DEVBUF);
250 vndattached = 0;
251
252 return (0);
253 }
254
255 int
256 vndopen(dev, flags, mode, l)
257 dev_t dev;
258 int flags, mode;
259 struct lwp *l;
260 {
261 int unit = vndunit(dev);
262 struct vnd_softc *sc;
263 int error = 0, part, pmask;
264 struct disklabel *lp;
265
266 #ifdef DEBUG
267 if (vnddebug & VDB_FOLLOW)
268 printf("vndopen(0x%x, 0x%x, 0x%x, %p)\n", dev, flags, mode, l);
269 #endif
270 if (unit >= numvnd)
271 return (ENXIO);
272 sc = &vnd_softc[unit];
273
274 if ((error = vndlock(sc)) != 0)
275 return (error);
276
277 lp = sc->sc_dkdev.dk_label;
278
279 part = DISKPART(dev);
280 pmask = (1 << part);
281
282 /*
283 * If we're initialized, check to see if there are any other
284 * open partitions. If not, then it's safe to update the
285 * in-core disklabel. Only read the disklabel if it is
286 * not realdy valid.
287 */
288 if ((sc->sc_flags & (VNF_INITED|VNF_VLABEL)) == VNF_INITED &&
289 sc->sc_dkdev.dk_openmask == 0)
290 vndgetdisklabel(dev);
291
292 /* Check that the partitions exists. */
293 if (part != RAW_PART) {
294 if (((sc->sc_flags & VNF_INITED) == 0) ||
295 ((part >= lp->d_npartitions) ||
296 (lp->d_partitions[part].p_fstype == FS_UNUSED))) {
297 error = ENXIO;
298 goto done;
299 }
300 }
301
302 /* Prevent our unit from being unconfigured while open. */
303 switch (mode) {
304 case S_IFCHR:
305 sc->sc_dkdev.dk_copenmask |= pmask;
306 break;
307
308 case S_IFBLK:
309 sc->sc_dkdev.dk_bopenmask |= pmask;
310 break;
311 }
312 sc->sc_dkdev.dk_openmask =
313 sc->sc_dkdev.dk_copenmask | sc->sc_dkdev.dk_bopenmask;
314
315 done:
316 vndunlock(sc);
317 return (error);
318 }
319
320 int
321 vndclose(dev, flags, mode, l)
322 dev_t dev;
323 int flags, mode;
324 struct lwp *l;
325 {
326 int unit = vndunit(dev);
327 struct vnd_softc *sc;
328 int error = 0, part;
329
330 #ifdef DEBUG
331 if (vnddebug & VDB_FOLLOW)
332 printf("vndclose(0x%x, 0x%x, 0x%x, %p)\n", dev, flags, mode, l);
333 #endif
334
335 if (unit >= numvnd)
336 return (ENXIO);
337 sc = &vnd_softc[unit];
338
339 if ((error = vndlock(sc)) != 0)
340 return (error);
341
342 part = DISKPART(dev);
343
344 /* ...that much closer to allowing unconfiguration... */
345 switch (mode) {
346 case S_IFCHR:
347 sc->sc_dkdev.dk_copenmask &= ~(1 << part);
348 break;
349
350 case S_IFBLK:
351 sc->sc_dkdev.dk_bopenmask &= ~(1 << part);
352 break;
353 }
354 sc->sc_dkdev.dk_openmask =
355 sc->sc_dkdev.dk_copenmask | sc->sc_dkdev.dk_bopenmask;
356
357 if (sc->sc_dkdev.dk_openmask == 0) {
358 if ((sc->sc_flags & VNF_KLABEL) == 0)
359 sc->sc_flags &= ~VNF_VLABEL;
360 }
361
362 vndunlock(sc);
363 return (0);
364 }
365
366 /*
367 * Break the request into bsize pieces and submit using VOP_BMAP/VOP_STRATEGY.
368 */
369 void
370 vndstrategy(bp)
371 struct buf *bp;
372 {
373 int unit = vndunit(bp->b_dev);
374 struct vnd_softc *vnd = &vnd_softc[unit];
375 struct vndxfer *vnx;
376 int s, bsize, resid;
377 off_t bn;
378 caddr_t addr;
379 int sz, flags, error, wlabel;
380 struct disklabel *lp;
381 struct partition *pp;
382
383 #ifdef DEBUG
384 if (vnddebug & VDB_FOLLOW)
385 printf("vndstrategy(%p): unit %d\n", bp, unit);
386 #endif
387 if ((vnd->sc_flags & VNF_INITED) == 0) {
388 bp->b_error = ENXIO;
389 bp->b_flags |= B_ERROR;
390 goto done;
391 }
392
393 /* If it's a nil transfer, wake up the top half now. */
394 if (bp->b_bcount == 0)
395 goto done;
396
397 lp = vnd->sc_dkdev.dk_label;
398
399 /*
400 * The transfer must be a whole number of blocks.
401 */
402 if ((bp->b_bcount % lp->d_secsize) != 0) {
403 bp->b_error = EINVAL;
404 bp->b_flags |= B_ERROR;
405 goto done;
406 }
407
408 /*
409 * Do bounds checking and adjust transfer. If there's an error,
410 * the bounds check will flag that for us.
411 */
412 wlabel = vnd->sc_flags & (VNF_WLABEL|VNF_LABELLING);
413 if (DISKPART(bp->b_dev) != RAW_PART)
414 if (bounds_check_with_label(&vnd->sc_dkdev, bp, wlabel) <= 0)
415 goto done;
416
417 /*
418 * check if we're read-only.
419 */
420 if ((vnd->sc_flags & VNF_READONLY) && !(bp->b_flags & B_READ)) {
421 bp->b_error = EACCES;
422 bp->b_flags |= B_ERROR;
423 goto done;
424 }
425
426 bp->b_resid = bp->b_bcount;
427
428 /*
429 * Put the block number in terms of the logical blocksize
430 * of the "device".
431 */
432 bn = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
433
434 /*
435 * Translate the partition-relative block number to an absolute.
436 */
437 if (DISKPART(bp->b_dev) != RAW_PART) {
438 pp = &vnd->sc_dkdev.dk_label->d_partitions[DISKPART(bp->b_dev)];
439 bn += pp->p_offset;
440 }
441
442 /* ...and convert to a byte offset within the file. */
443 bn *= lp->d_secsize;
444
445 if (vnd->sc_vp->v_mount == NULL) {
446 bp->b_error = ENXIO;
447 bp->b_flags |= B_ERROR;
448 goto done;
449 }
450 bsize = vnd->sc_vp->v_mount->mnt_stat.f_iosize;
451 addr = bp->b_data;
452 flags = (bp->b_flags & (B_READ|B_ASYNC)) | B_CALL;
453
454 /* Allocate a header for this transfer and link it to the buffer */
455 s = splbio();
456 vnx = VND_GETXFER(vnd);
457 splx(s);
458 vnx->vx_flags = VX_BUSY;
459 vnx->vx_error = 0;
460 vnx->vx_pending = 0;
461 vnx->vx_bp = bp;
462
463 for (resid = bp->b_resid; resid; resid -= sz) {
464 struct vndbuf *nbp;
465 struct vnode *vp;
466 daddr_t nbn;
467 int off, nra;
468
469 nra = 0;
470 vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY | LK_CANRECURSE);
471 error = VOP_BMAP(vnd->sc_vp, bn / bsize, &vp, &nbn, &nra);
472 VOP_UNLOCK(vnd->sc_vp, 0);
473
474 if (error == 0 && (long)nbn == -1)
475 error = EIO;
476
477 /*
478 * If there was an error or a hole in the file...punt.
479 * Note that we may have to wait for any operations
480 * that we have already fired off before releasing
481 * the buffer.
482 *
483 * XXX we could deal with holes here but it would be
484 * a hassle (in the write case).
485 */
486 if (error) {
487 s = splbio();
488 vnx->vx_error = error;
489 goto out;
490 }
491
492 #ifdef DEBUG
493 if (!dovndcluster)
494 nra = 0;
495 #endif
496
497 if ((off = bn % bsize) != 0)
498 sz = bsize - off;
499 else
500 sz = (1 + nra) * bsize;
501 if (resid < sz)
502 sz = resid;
503 #ifdef DEBUG
504 if (vnddebug & VDB_IO)
505 printf("vndstrategy: vp %p/%p bn 0x%qx/0x%" PRIx64
506 " sz 0x%x\n",
507 vnd->sc_vp, vp, (long long)bn, nbn, sz);
508 #endif
509
510 s = splbio();
511 nbp = VND_GETBUF(vnd);
512 splx(s);
513 BUF_INIT(&nbp->vb_buf);
514 nbp->vb_buf.b_flags = flags;
515 nbp->vb_buf.b_bcount = sz;
516 nbp->vb_buf.b_bufsize = round_page((ulong)addr + sz)
517 - trunc_page((ulong) addr);
518 nbp->vb_buf.b_error = 0;
519 nbp->vb_buf.b_data = addr;
520 nbp->vb_buf.b_blkno = nbp->vb_buf.b_rawblkno = nbn + btodb(off);
521 nbp->vb_buf.b_proc = bp->b_proc;
522 nbp->vb_buf.b_iodone = vndiodone;
523 nbp->vb_buf.b_vp = NULLVP;
524
525 nbp->vb_xfer = vnx;
526
527 /*
528 * Just sort by block number
529 */
530 s = splbio();
531 if (vnx->vx_error != 0) {
532 VND_PUTBUF(vnd, nbp);
533 goto out;
534 }
535 vnx->vx_pending++;
536 bgetvp(vp, &nbp->vb_buf);
537 BUFQ_PUT(&vnd->sc_tab, &nbp->vb_buf);
538 vndstart(vnd);
539 splx(s);
540 bn += sz;
541 addr += sz;
542 }
543
544 s = splbio();
545
546 out: /* Arrive here at splbio */
547 vnx->vx_flags &= ~VX_BUSY;
548 if (vnx->vx_pending == 0) {
549 if (vnx->vx_error != 0) {
550 bp->b_error = vnx->vx_error;
551 bp->b_flags |= B_ERROR;
552 }
553 VND_PUTXFER(vnd, vnx);
554 biodone(bp);
555 }
556 splx(s);
557 return;
558
559 done:
560 biodone(bp);
561 }
562
563 /*
564 * Feed requests sequentially.
565 * We do it this way to keep from flooding NFS servers if we are connected
566 * to an NFS file. This places the burden on the client rather than the
567 * server.
568 */
569 void
570 vndstart(vnd)
571 struct vnd_softc *vnd;
572 {
573 struct buf *bp;
574
575 /*
576 * Dequeue now since lower level strategy routine might
577 * queue using same links
578 */
579
580 if ((vnd->sc_flags & VNF_BUSY) != 0)
581 return;
582
583 vnd->sc_flags |= VNF_BUSY;
584
585 while (vnd->sc_active < vnd->sc_maxactive) {
586 bp = BUFQ_GET(&vnd->sc_tab);
587 if (bp == NULL)
588 break;
589 vnd->sc_active++;
590 #ifdef DEBUG
591 if (vnddebug & VDB_IO)
592 printf("vndstart(%ld): bp %p vp %p blkno 0x%" PRIx64
593 " flags %lx addr %p cnt 0x%lx\n",
594 (long) (vnd-vnd_softc), bp, bp->b_vp, bp->b_blkno,
595 bp->b_flags, bp->b_data, bp->b_bcount);
596 #endif
597
598 /* Instrumentation. */
599 disk_busy(&vnd->sc_dkdev);
600
601 if ((bp->b_flags & B_READ) == 0)
602 bp->b_vp->v_numoutput++;
603 VOP_STRATEGY(bp);
604 }
605 vnd->sc_flags &= ~VNF_BUSY;
606 }
607
608 void
609 vndiodone(bp)
610 struct buf *bp;
611 {
612 struct vndbuf *vbp = (struct vndbuf *) bp;
613 struct vndxfer *vnx = (struct vndxfer *)vbp->vb_xfer;
614 struct buf *pbp = vnx->vx_bp;
615 struct vnd_softc *vnd = &vnd_softc[vndunit(pbp->b_dev)];
616 int s, resid;
617
618 s = splbio();
619 #ifdef DEBUG
620 if (vnddebug & VDB_IO)
621 printf("vndiodone(%ld): vbp %p vp %p blkno 0x%" PRIx64
622 " addr %p cnt 0x%lx\n",
623 (long) (vnd-vnd_softc), vbp, vbp->vb_buf.b_vp,
624 vbp->vb_buf.b_blkno, vbp->vb_buf.b_data,
625 vbp->vb_buf.b_bcount);
626 #endif
627
628 resid = vbp->vb_buf.b_bcount - vbp->vb_buf.b_resid;
629 pbp->b_resid -= resid;
630 disk_unbusy(&vnd->sc_dkdev, resid, (pbp->b_flags & B_READ));
631 vnx->vx_pending--;
632
633 if (vbp->vb_buf.b_error) {
634 #ifdef DEBUG
635 if (vnddebug & VDB_IO)
636 printf("vndiodone: vbp %p error %d\n", vbp,
637 vbp->vb_buf.b_error);
638 #endif
639 vnx->vx_error = vbp->vb_buf.b_error;
640 }
641
642 if (vbp->vb_buf.b_vp != NULLVP)
643 brelvp(&vbp->vb_buf);
644
645 VND_PUTBUF(vnd, vbp);
646
647 /*
648 * Wrap up this transaction if it has run to completion or, in
649 * case of an error, when all auxiliary buffers have returned.
650 */
651 if (vnx->vx_error != 0) {
652 pbp->b_flags |= B_ERROR;
653 pbp->b_error = vnx->vx_error;
654 if ((vnx->vx_flags & VX_BUSY) == 0 && vnx->vx_pending == 0) {
655
656 #ifdef DEBUG
657 if (vnddebug & VDB_IO)
658 printf("vndiodone: pbp %p iodone: error %d\n",
659 pbp, vnx->vx_error);
660 #endif
661 VND_PUTXFER(vnd, vnx);
662 biodone(pbp);
663 }
664 } else if (pbp->b_resid == 0) {
665
666 #ifdef DIAGNOSTIC
667 if (vnx->vx_pending != 0)
668 panic("vndiodone: vnx pending: %d", vnx->vx_pending);
669 #endif
670
671 if ((vnx->vx_flags & VX_BUSY) == 0) {
672 #ifdef DEBUG
673 if (vnddebug & VDB_IO)
674 printf("vndiodone: pbp %p iodone\n", pbp);
675 #endif
676 VND_PUTXFER(vnd, vnx);
677 biodone(pbp);
678 }
679 }
680
681 vnd->sc_active--;
682 vndstart(vnd);
683 splx(s);
684 }
685
686 /* ARGSUSED */
687 int
688 vndread(dev, uio, flags)
689 dev_t dev;
690 struct uio *uio;
691 int flags;
692 {
693 int unit = vndunit(dev);
694 struct vnd_softc *sc;
695
696 #ifdef DEBUG
697 if (vnddebug & VDB_FOLLOW)
698 printf("vndread(0x%x, %p)\n", dev, uio);
699 #endif
700
701 if (unit >= numvnd)
702 return (ENXIO);
703 sc = &vnd_softc[unit];
704
705 if ((sc->sc_flags & VNF_INITED) == 0)
706 return (ENXIO);
707
708 return (physio(vndstrategy, NULL, dev, B_READ, minphys, uio));
709 }
710
711 /* ARGSUSED */
712 int
713 vndwrite(dev, uio, flags)
714 dev_t dev;
715 struct uio *uio;
716 int flags;
717 {
718 int unit = vndunit(dev);
719 struct vnd_softc *sc;
720
721 #ifdef DEBUG
722 if (vnddebug & VDB_FOLLOW)
723 printf("vndwrite(0x%x, %p)\n", dev, uio);
724 #endif
725
726 if (unit >= numvnd)
727 return (ENXIO);
728 sc = &vnd_softc[unit];
729
730 if ((sc->sc_flags & VNF_INITED) == 0)
731 return (ENXIO);
732
733 return (physio(vndstrategy, NULL, dev, B_WRITE, minphys, uio));
734 }
735
736 /* ARGSUSED */
737 int
738 vndioctl(dev, cmd, data, flag, l)
739 dev_t dev;
740 u_long cmd;
741 caddr_t data;
742 int flag;
743 struct lwp *l;
744 {
745 int unit = vndunit(dev);
746 struct vnd_softc *vnd;
747 struct vnd_ioctl *vio;
748 struct vattr vattr;
749 struct nameidata nd;
750 int error, part, pmask;
751 size_t geomsize;
752 struct proc *p = l->l_proc;
753 int fflags;
754 #ifdef __HAVE_OLD_DISKLABEL
755 struct disklabel newlabel;
756 #endif
757
758 #ifdef DEBUG
759 if (vnddebug & VDB_FOLLOW)
760 printf("vndioctl(0x%x, 0x%lx, %p, 0x%x, %p): unit %d\n",
761 dev, cmd, data, flag, p, unit);
762 #endif
763 if (unit >= numvnd)
764 return (ENXIO);
765
766 vnd = &vnd_softc[unit];
767 vio = (struct vnd_ioctl *)data;
768
769 /* Must be open for writes for these commands... */
770 switch (cmd) {
771 case VNDIOCSET:
772 case VNDIOCCLR:
773 case DIOCSDINFO:
774 case DIOCWDINFO:
775 #ifdef __HAVE_OLD_DISKLABEL
776 case ODIOCSDINFO:
777 case ODIOCWDINFO:
778 #endif
779 case DIOCKLABEL:
780 case DIOCWLABEL:
781 if ((flag & FWRITE) == 0)
782 return (EBADF);
783 }
784
785 /* Must be initialized for these... */
786 switch (cmd) {
787 case VNDIOCCLR:
788 case DIOCGDINFO:
789 case DIOCSDINFO:
790 case DIOCWDINFO:
791 case DIOCGPART:
792 case DIOCKLABEL:
793 case DIOCWLABEL:
794 case DIOCGDEFLABEL:
795 #ifdef __HAVE_OLD_DISKLABEL
796 case ODIOCGDINFO:
797 case ODIOCSDINFO:
798 case ODIOCWDINFO:
799 case ODIOCGDEFLABEL:
800 #endif
801 if ((vnd->sc_flags & VNF_INITED) == 0)
802 return (ENXIO);
803 }
804
805 switch (cmd) {
806 case VNDIOCSET:
807 if (vnd->sc_flags & VNF_INITED)
808 return (EBUSY);
809
810 if ((error = vndlock(vnd)) != 0)
811 return (error);
812
813 fflags = FREAD;
814 if ((vio->vnd_flags & VNDIOF_READONLY) == 0)
815 fflags |= FWRITE;
816 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, vio->vnd_file, l);
817 if ((error = vn_open(&nd, fflags, 0)) != 0)
818 goto unlock_and_exit;
819 error = VOP_GETATTR(nd.ni_vp, &vattr, p->p_ucred, l);
820 VOP_UNLOCK(nd.ni_vp, 0);
821 if (!error && nd.ni_vp->v_type != VREG)
822 error = EOPNOTSUPP;
823 if (error)
824 goto close_and_exit;
825 vnd->sc_vp = nd.ni_vp;
826 vnd->sc_size = btodb(vattr.va_size); /* note truncation */
827
828 /*
829 * Use pseudo-geometry specified. If none was provided,
830 * use "standard" Adaptec fictitious geometry.
831 */
832 if (vio->vnd_flags & VNDIOF_HASGEOM) {
833
834 memcpy(&vnd->sc_geom, &vio->vnd_geom,
835 sizeof(vio->vnd_geom));
836
837 /*
838 * Sanity-check the sector size.
839 * XXX Don't allow secsize < DEV_BSIZE. Should
840 * XXX we?
841 */
842 if (vnd->sc_geom.vng_secsize < DEV_BSIZE ||
843 (vnd->sc_geom.vng_secsize % DEV_BSIZE) != 0) {
844 error = EINVAL;
845 goto close_and_exit;
846 }
847
848 /*
849 * Compute the size (in DEV_BSIZE blocks) specified
850 * by the geometry.
851 */
852 geomsize = (vnd->sc_geom.vng_nsectors *
853 vnd->sc_geom.vng_ntracks *
854 vnd->sc_geom.vng_ncylinders) *
855 (vnd->sc_geom.vng_secsize / DEV_BSIZE);
856
857 /*
858 * Sanity-check the size against the specified
859 * geometry.
860 */
861 if (vnd->sc_size < geomsize) {
862 error = EINVAL;
863 goto close_and_exit;
864 }
865 } else {
866 /*
867 * Size must be at least 2048 DEV_BSIZE blocks
868 * (1M) in order to use this geometry.
869 */
870 if (vnd->sc_size < (32 * 64)) {
871 error = EINVAL;
872 goto close_and_exit;
873 }
874
875 vnd->sc_geom.vng_secsize = DEV_BSIZE;
876 vnd->sc_geom.vng_nsectors = 32;
877 vnd->sc_geom.vng_ntracks = 64;
878 vnd->sc_geom.vng_ncylinders = vnd->sc_size / (64 * 32);
879 }
880
881 if (vio->vnd_flags & VNDIOF_READONLY) {
882 vnd->sc_flags |= VNF_READONLY;
883 }
884
885 if ((error = vndsetcred(vnd, p->p_ucred)) != 0)
886 goto close_and_exit;
887 vndthrottle(vnd, vnd->sc_vp);
888 vio->vnd_size = dbtob(vnd->sc_size);
889 vnd->sc_flags |= VNF_INITED;
890 #ifdef DEBUG
891 if (vnddebug & VDB_INIT)
892 printf("vndioctl: SET vp %p size 0x%lx %d/%d/%d/%d\n",
893 vnd->sc_vp, (unsigned long) vnd->sc_size,
894 vnd->sc_geom.vng_secsize,
895 vnd->sc_geom.vng_nsectors,
896 vnd->sc_geom.vng_ntracks,
897 vnd->sc_geom.vng_ncylinders);
898 #endif
899
900 /* Attach the disk. */
901 memset(vnd->sc_xname, 0, sizeof(vnd->sc_xname)); /* XXX */
902 sprintf(vnd->sc_xname, "vnd%d", unit); /* XXX */
903 vnd->sc_dkdev.dk_name = vnd->sc_xname;
904 disk_attach(&vnd->sc_dkdev);
905
906 /* Initialize the xfer and buffer pools. */
907 pool_init(&vnd->sc_vxpool, sizeof(struct vndxfer), 0,
908 0, 0, "vndxpl", NULL);
909 pool_init(&vnd->sc_vbpool, sizeof(struct vndbuf), 0,
910 0, 0, "vndbpl", NULL);
911
912 /* Try and read the disklabel. */
913 vndgetdisklabel(dev);
914
915 vndunlock(vnd);
916
917 break;
918
919 close_and_exit:
920 (void) vn_close(nd.ni_vp, fflags, p->p_ucred, l);
921 unlock_and_exit:
922 vndunlock(vnd);
923 return (error);
924
925 case VNDIOCCLR:
926 if ((error = vndlock(vnd)) != 0)
927 return (error);
928
929 /*
930 * Don't unconfigure if any other partitions are open
931 * or if both the character and block flavors of this
932 * partition are open.
933 */
934 part = DISKPART(dev);
935 pmask = (1 << part);
936 if (((vnd->sc_dkdev.dk_openmask & ~pmask) ||
937 ((vnd->sc_dkdev.dk_bopenmask & pmask) &&
938 (vnd->sc_dkdev.dk_copenmask & pmask))) &&
939 !(vio->vnd_flags & VNDIOF_FORCE)) {
940 vndunlock(vnd);
941 return (EBUSY);
942 }
943
944 /*
945 * XXX vndclear() might call vndclose() implicitely;
946 * release lock to avoid recursion
947 */
948 vndunlock(vnd);
949 vndclear(vnd, minor(dev));
950 #ifdef DEBUG
951 if (vnddebug & VDB_INIT)
952 printf("vndioctl: CLRed\n");
953 #endif
954
955 /* Destroy the xfer and buffer pools. */
956 pool_destroy(&vnd->sc_vxpool);
957 pool_destroy(&vnd->sc_vbpool);
958
959 /* Detatch the disk. */
960 disk_detach(&vnd->sc_dkdev);
961
962 break;
963
964 case VNDIOCGET: {
965 struct vnd_user *vnu;
966 struct vattr va;
967
968 vnu = (struct vnd_user *)data;
969
970 if (vnu->vnu_unit == -1)
971 vnu->vnu_unit = unit;
972 if (vnu->vnu_unit >= numvnd)
973 return (ENXIO);
974 if (vnu->vnu_unit < 0)
975 return (EINVAL);
976
977 vnd = &vnd_softc[vnu->vnu_unit];
978
979 if (vnd->sc_flags & VNF_INITED) {
980 error = VOP_GETATTR(vnd->sc_vp, &va, p->p_ucred, l);
981 if (error)
982 return (error);
983 vnu->vnu_dev = va.va_fsid;
984 vnu->vnu_ino = va.va_fileid;
985 }
986 else {
987 /* unused is not an error */
988 vnu->vnu_dev = 0;
989 vnu->vnu_ino = 0;
990 }
991
992 break;
993 }
994
995 case DIOCGDINFO:
996 *(struct disklabel *)data = *(vnd->sc_dkdev.dk_label);
997 break;
998
999 #ifdef __HAVE_OLD_DISKLABEL
1000 case ODIOCGDINFO:
1001 newlabel = *(vnd->sc_dkdev.dk_label);
1002 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
1003 return ENOTTY;
1004 memcpy(data, &newlabel, sizeof (struct olddisklabel));
1005 break;
1006 #endif
1007
1008 case DIOCGPART:
1009 ((struct partinfo *)data)->disklab = vnd->sc_dkdev.dk_label;
1010 ((struct partinfo *)data)->part =
1011 &vnd->sc_dkdev.dk_label->d_partitions[DISKPART(dev)];
1012 break;
1013
1014 case DIOCWDINFO:
1015 case DIOCSDINFO:
1016 #ifdef __HAVE_OLD_DISKLABEL
1017 case ODIOCWDINFO:
1018 case ODIOCSDINFO:
1019 #endif
1020 {
1021 struct disklabel *lp;
1022
1023 if ((error = vndlock(vnd)) != 0)
1024 return (error);
1025
1026 vnd->sc_flags |= VNF_LABELLING;
1027
1028 #ifdef __HAVE_OLD_DISKLABEL
1029 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
1030 memset(&newlabel, 0, sizeof newlabel);
1031 memcpy(&newlabel, data, sizeof (struct olddisklabel));
1032 lp = &newlabel;
1033 } else
1034 #endif
1035 lp = (struct disklabel *)data;
1036
1037 error = setdisklabel(vnd->sc_dkdev.dk_label,
1038 lp, 0, vnd->sc_dkdev.dk_cpulabel);
1039 if (error == 0) {
1040 if (cmd == DIOCWDINFO
1041 #ifdef __HAVE_OLD_DISKLABEL
1042 || cmd == ODIOCWDINFO
1043 #endif
1044 )
1045 error = writedisklabel(VNDLABELDEV(dev),
1046 vndstrategy, vnd->sc_dkdev.dk_label,
1047 vnd->sc_dkdev.dk_cpulabel);
1048 }
1049
1050 vnd->sc_flags &= ~VNF_LABELLING;
1051
1052 vndunlock(vnd);
1053
1054 if (error)
1055 return (error);
1056 break;
1057 }
1058
1059 case DIOCKLABEL:
1060 if (*(int *)data != 0)
1061 vnd->sc_flags |= VNF_KLABEL;
1062 else
1063 vnd->sc_flags &= ~VNF_KLABEL;
1064 break;
1065
1066 case DIOCWLABEL:
1067 if (*(int *)data != 0)
1068 vnd->sc_flags |= VNF_WLABEL;
1069 else
1070 vnd->sc_flags &= ~VNF_WLABEL;
1071 break;
1072
1073 case DIOCGDEFLABEL:
1074 vndgetdefaultlabel(vnd, (struct disklabel *)data);
1075 break;
1076
1077 #ifdef __HAVE_OLD_DISKLABEL
1078 case ODIOCGDEFLABEL:
1079 vndgetdefaultlabel(vnd, &newlabel);
1080 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
1081 return ENOTTY;
1082 memcpy(data, &newlabel, sizeof (struct olddisklabel));
1083 break;
1084 #endif
1085
1086 default:
1087 return (ENOTTY);
1088 }
1089
1090 return (0);
1091 }
1092
1093 /*
1094 * Duplicate the current processes' credentials. Since we are called only
1095 * as the result of a SET ioctl and only root can do that, any future access
1096 * to this "disk" is essentially as root. Note that credentials may change
1097 * if some other uid can write directly to the mapped file (NFS).
1098 */
1099 int
1100 vndsetcred(vnd, cred)
1101 struct vnd_softc *vnd;
1102 struct ucred *cred;
1103 {
1104 struct uio auio;
1105 struct iovec aiov;
1106 char *tmpbuf;
1107 int error;
1108
1109 vnd->sc_cred = crdup(cred);
1110 tmpbuf = malloc(DEV_BSIZE, M_TEMP, M_WAITOK);
1111
1112 /* XXX: Horrible kludge to establish credentials for NFS */
1113 aiov.iov_base = tmpbuf;
1114 aiov.iov_len = min(DEV_BSIZE, dbtob(vnd->sc_size));
1115 auio.uio_iov = &aiov;
1116 auio.uio_iovcnt = 1;
1117 auio.uio_offset = 0;
1118 auio.uio_rw = UIO_READ;
1119 auio.uio_segflg = UIO_SYSSPACE;
1120 auio.uio_resid = aiov.iov_len;
1121 vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY);
1122 error = VOP_READ(vnd->sc_vp, &auio, 0, vnd->sc_cred);
1123 if (error == 0) {
1124 /*
1125 * Because vnd does all IO directly through the vnode
1126 * we need to flush (at least) the buffer from the above
1127 * VOP_READ from the buffer cache to prevent cache
1128 * incoherencies. Also, be careful to write dirty
1129 * buffers back to stable storage.
1130 */
1131 error = vinvalbuf(vnd->sc_vp, V_SAVE, vnd->sc_cred,
1132 curlwp, 0, 0);
1133 }
1134 VOP_UNLOCK(vnd->sc_vp, 0);
1135
1136 free(tmpbuf, M_TEMP);
1137 return (error);
1138 }
1139
1140 /*
1141 * Set maxactive based on FS type
1142 */
1143 void
1144 vndthrottle(vnd, vp)
1145 struct vnd_softc *vnd;
1146 struct vnode *vp;
1147 {
1148 #ifdef NFS
1149 extern int (**nfsv2_vnodeop_p) __P((void *));
1150
1151 if (vp->v_op == nfsv2_vnodeop_p)
1152 vnd->sc_maxactive = 2;
1153 else
1154 #endif
1155 vnd->sc_maxactive = 8;
1156
1157 if (vnd->sc_maxactive < 1)
1158 vnd->sc_maxactive = 1;
1159 }
1160
1161 #if 0
1162 void
1163 vndshutdown()
1164 {
1165 struct vnd_softc *vnd;
1166
1167 for (vnd = &vnd_softc[0]; vnd < &vnd_softc[numvnd]; vnd++)
1168 if (vnd->sc_flags & VNF_INITED)
1169 vndclear(vnd);
1170 }
1171 #endif
1172
1173 void
1174 vndclear(vnd, myminor)
1175 struct vnd_softc *vnd;
1176 int myminor;
1177 {
1178 struct vnode *vp = vnd->sc_vp;
1179 struct lwp *l = curlwp;
1180 int fflags = FREAD;
1181 int bmaj, cmaj, i, mn;
1182
1183 #ifdef DEBUG
1184 if (vnddebug & VDB_FOLLOW)
1185 printf("vndclear(%p): vp %p\n", vnd, vp);
1186 #endif
1187 /* locate the major number */
1188 bmaj = bdevsw_lookup_major(&vnd_bdevsw);
1189 cmaj = cdevsw_lookup_major(&vnd_cdevsw);
1190
1191 /* Nuke the vnodes for any open instances */
1192 for (i = 0; i < MAXPARTITIONS; i++) {
1193 mn = DISKMINOR(vnd->sc_unit, i);
1194 vdevgone(bmaj, mn, mn, VBLK);
1195 if (mn != myminor) /* XXX avoid to kill own vnode */
1196 vdevgone(cmaj, mn, mn, VCHR);
1197 }
1198
1199 if ((vnd->sc_flags & VNF_READONLY) == 0)
1200 fflags |= FWRITE;
1201 vnd->sc_flags &= ~(VNF_INITED | VNF_READONLY | VNF_VLABEL);
1202 if (vp == (struct vnode *)0)
1203 panic("vndioctl: null vp");
1204 (void) vn_close(vp, fflags, vnd->sc_cred, l);
1205 crfree(vnd->sc_cred);
1206 vnd->sc_vp = (struct vnode *)0;
1207 vnd->sc_cred = (struct ucred *)0;
1208 vnd->sc_size = 0;
1209 }
1210
1211 int
1212 vndsize(dev)
1213 dev_t dev;
1214 {
1215 struct vnd_softc *sc;
1216 struct disklabel *lp;
1217 int part, unit, omask;
1218 int size;
1219
1220 unit = vndunit(dev);
1221 if (unit >= numvnd)
1222 return (-1);
1223 sc = &vnd_softc[unit];
1224
1225 if ((sc->sc_flags & VNF_INITED) == 0)
1226 return (-1);
1227
1228 part = DISKPART(dev);
1229 omask = sc->sc_dkdev.dk_openmask & (1 << part);
1230 lp = sc->sc_dkdev.dk_label;
1231
1232 if (omask == 0 && vndopen(dev, 0, S_IFBLK, curlwp)) /* XXX */
1233 return (-1);
1234
1235 if (lp->d_partitions[part].p_fstype != FS_SWAP)
1236 size = -1;
1237 else
1238 size = lp->d_partitions[part].p_size *
1239 (lp->d_secsize / DEV_BSIZE);
1240
1241 if (omask == 0 && vndclose(dev, 0, S_IFBLK, curlwp)) /* XXX */
1242 return (-1);
1243
1244 return (size);
1245 }
1246
1247 int
1248 vnddump(dev, blkno, va, size)
1249 dev_t dev;
1250 daddr_t blkno;
1251 caddr_t va;
1252 size_t size;
1253 {
1254
1255 /* Not implemented. */
1256 return ENXIO;
1257 }
1258
1259 void
1260 vndgetdefaultlabel(sc, lp)
1261 struct vnd_softc *sc;
1262 struct disklabel *lp;
1263 {
1264 struct vndgeom *vng = &sc->sc_geom;
1265 struct partition *pp;
1266
1267 memset(lp, 0, sizeof(*lp));
1268
1269 lp->d_secperunit = sc->sc_size / (vng->vng_secsize / DEV_BSIZE);
1270 lp->d_secsize = vng->vng_secsize;
1271 lp->d_nsectors = vng->vng_nsectors;
1272 lp->d_ntracks = vng->vng_ntracks;
1273 lp->d_ncylinders = vng->vng_ncylinders;
1274 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1275
1276 strncpy(lp->d_typename, "vnd", sizeof(lp->d_typename));
1277 lp->d_type = DTYPE_VND;
1278 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
1279 lp->d_rpm = 3600;
1280 lp->d_interleave = 1;
1281 lp->d_flags = 0;
1282
1283 pp = &lp->d_partitions[RAW_PART];
1284 pp->p_offset = 0;
1285 pp->p_size = lp->d_secperunit;
1286 pp->p_fstype = FS_UNUSED;
1287 lp->d_npartitions = RAW_PART + 1;
1288
1289 lp->d_magic = DISKMAGIC;
1290 lp->d_magic2 = DISKMAGIC;
1291 lp->d_checksum = dkcksum(lp);
1292 }
1293
1294 /*
1295 * Read the disklabel from a vnd. If one is not present, create a fake one.
1296 */
1297 void
1298 vndgetdisklabel(dev)
1299 dev_t dev;
1300 {
1301 struct vnd_softc *sc = &vnd_softc[vndunit(dev)];
1302 const char *errstring;
1303 struct disklabel *lp = sc->sc_dkdev.dk_label;
1304 struct cpu_disklabel *clp = sc->sc_dkdev.dk_cpulabel;
1305 int i;
1306
1307 memset(clp, 0, sizeof(*clp));
1308
1309 vndgetdefaultlabel(sc, lp);
1310
1311 /*
1312 * Call the generic disklabel extraction routine.
1313 */
1314 errstring = readdisklabel(VNDLABELDEV(dev), vndstrategy, lp, clp);
1315 if (errstring) {
1316 /*
1317 * Lack of disklabel is common, but we print the warning
1318 * anyway, since it might contain other useful information.
1319 */
1320 printf("%s: %s\n", sc->sc_xname, errstring);
1321
1322 /*
1323 * For historical reasons, if there's no disklabel
1324 * present, all partitions must be FS_BSDFFS and
1325 * occupy the entire disk.
1326 */
1327 for (i = 0; i < MAXPARTITIONS; i++) {
1328 /*
1329 * Don't wipe out port specific hack (such as
1330 * dos partition hack of i386 port).
1331 */
1332 if (lp->d_partitions[i].p_size != 0)
1333 continue;
1334
1335 lp->d_partitions[i].p_size = lp->d_secperunit;
1336 lp->d_partitions[i].p_offset = 0;
1337 lp->d_partitions[i].p_fstype = FS_BSDFFS;
1338 }
1339
1340 strncpy(lp->d_packname, "default label",
1341 sizeof(lp->d_packname));
1342
1343 lp->d_npartitions = MAXPARTITIONS;
1344 lp->d_checksum = dkcksum(lp);
1345 }
1346
1347 /* In-core label now valid. */
1348 sc->sc_flags |= VNF_VLABEL;
1349 }
1350
1351 /*
1352 * Wait interruptibly for an exclusive lock.
1353 *
1354 * XXX
1355 * Several drivers do this; it should be abstracted and made MP-safe.
1356 */
1357 static int
1358 vndlock(sc)
1359 struct vnd_softc *sc;
1360 {
1361 int error;
1362
1363 while ((sc->sc_flags & VNF_LOCKED) != 0) {
1364 sc->sc_flags |= VNF_WANTED;
1365 if ((error = tsleep(sc, PRIBIO | PCATCH, "vndlck", 0)) != 0)
1366 return (error);
1367 }
1368 sc->sc_flags |= VNF_LOCKED;
1369 return (0);
1370 }
1371
1372 /*
1373 * Unlock and wake up any waiters.
1374 */
1375 static void
1376 vndunlock(sc)
1377 struct vnd_softc *sc;
1378 {
1379
1380 sc->sc_flags &= ~VNF_LOCKED;
1381 if ((sc->sc_flags & VNF_WANTED) != 0) {
1382 sc->sc_flags &= ~VNF_WANTED;
1383 wakeup(sc);
1384 }
1385 }
1386