vnd.c revision 1.23 1 /* $NetBSD: vnd.c,v 1.23 1996/01/07 22:03:33 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1988 University of Utah.
5 * Copyright (c) 1990, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * the Systems Programming Group of the University of Utah Computer
10 * Science Department.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * from: Utah $Hdr: vn.c 1.13 94/04/02$
41 *
42 * @(#)vn.c 8.6 (Berkeley) 4/1/94
43 */
44
45 /*
46 * Vnode disk driver.
47 *
48 * Block/character interface to a vnode. Allows one to treat a file
49 * as a disk (e.g. build a filesystem in it, mount it, etc.).
50 *
51 * NOTE 1: This uses the VOP_BMAP/VOP_STRATEGY interface to the vnode
52 * instead of a simple VOP_RDWR. We do this to avoid distorting the
53 * local buffer cache.
54 *
55 * NOTE 2: There is a security issue involved with this driver.
56 * Once mounted all access to the contents of the "mapped" file via
57 * the special file is controlled by the permissions on the special
58 * file, the protection of the mapped file is ignored (effectively,
59 * by using root credentials in all transactions).
60 *
61 * NOTE 3: Doesn't interact with leases, should it?
62 */
63
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/namei.h>
67 #include <sys/proc.h>
68 #include <sys/errno.h>
69 #include <sys/buf.h>
70 #include <sys/malloc.h>
71 #include <sys/ioctl.h>
72 #include <sys/disklabel.h>
73 #include <sys/device.h>
74 #include <sys/disk.h>
75 #include <sys/stat.h>
76 #include <sys/conf.h>
77 #include <sys/mount.h>
78 #include <sys/vnode.h>
79 #include <sys/file.h>
80 #include <sys/uio.h>
81
82 #include <miscfs/specfs/specdev.h>
83
84 #include <dev/vndioctl.h>
85
86 #ifdef DEBUG
87 int dovndcluster = 1;
88 int vnddebug = 0x00;
89 #define VDB_FOLLOW 0x01
90 #define VDB_INIT 0x02
91 #define VDB_IO 0x04
92 #endif
93
94 #define b_cylin b_resid
95
96 #define vndunit(x) DISKUNIT(x)
97
98 struct vndbuf {
99 struct buf vb_buf;
100 struct buf *vb_obp;
101 };
102
103 #define getvndbuf() \
104 ((struct vndbuf *)malloc(sizeof(struct vndbuf), M_DEVBUF, M_WAITOK))
105 #define putvndbuf(vbp) \
106 free((caddr_t)(vbp), M_DEVBUF)
107
108 struct vnd_softc {
109 int sc_flags; /* flags */
110 size_t sc_size; /* size of vnd */
111 struct vnode *sc_vp; /* vnode */
112 struct ucred *sc_cred; /* credentials */
113 int sc_maxactive; /* max # of active requests */
114 struct buf sc_tab; /* transfer queue */
115 char sc_xname[8]; /* XXX external name */
116 struct disk sc_dkdev; /* generic disk device info */
117 };
118
119 /* sc_flags */
120 #define VNF_ALIVE 0x01
121 #define VNF_INITED 0x02
122 #define VNF_WANTED 0x40
123 #define VNF_LOCKED 0x80
124
125 struct vnd_softc *vnd_softc;
126 int numvnd = 0;
127
128 /* {b,c}devsw[] function prototypes */
129 dev_type_open(vndopen);
130 dev_type_close(vndclose);
131 dev_type_strategy(vndstrategy);
132 dev_type_ioctl(vndioctl);
133 dev_type_read(vndread);
134 dev_type_write(vndwrite);
135
136 /* called by main() at boot time */
137 void vndattach __P((int));
138
139 void vndclear __P((struct vnd_softc *));
140 void vndstart __P((struct vnd_softc *));
141 int vndsetcred __P((struct vnd_softc *, struct ucred *));
142 void vndthrottle __P((struct vnd_softc *, struct vnode *));
143
144 static int vndlock __P((struct vnd_softc *));
145 static void vndunlock __P((struct vnd_softc *));
146
147 void
148 vndattach(num)
149 int num;
150 {
151 char *mem;
152 register u_long size;
153
154 if (num <= 0)
155 return;
156 size = num * sizeof(struct vnd_softc);
157 mem = malloc(size, M_DEVBUF, M_NOWAIT);
158 if (mem == NULL) {
159 printf("WARNING: no memory for vnode disks\n");
160 return;
161 }
162 bzero(mem, size);
163 vnd_softc = (struct vnd_softc *)mem;
164 numvnd = num;
165 }
166
167 int
168 vndopen(dev, flags, mode, p)
169 dev_t dev;
170 int flags, mode;
171 struct proc *p;
172 {
173 int unit = vndunit(dev);
174 struct vnd_softc *sc;
175 int error = 0, part, pmask;
176
177 /*
178 * XXX Should support disklabels.
179 */
180
181 #ifdef DEBUG
182 if (vnddebug & VDB_FOLLOW)
183 printf("vndopen(%x, %x, %x, %x)\n", dev, flags, mode, p);
184 #endif
185 if (unit >= numvnd)
186 return (ENXIO);
187 sc = &vnd_softc[unit];
188
189 if (error = vndlock(sc))
190 return (error);
191
192 part = DISKPART(dev);
193 pmask = (1 << part);
194
195 /* Prevent our unit from being unconfigured while open. */
196 switch (mode) {
197 case S_IFCHR:
198 sc->sc_dkdev.dk_copenmask |= pmask;
199 break;
200
201 case S_IFBLK:
202 sc->sc_dkdev.dk_bopenmask |= pmask;
203 break;
204 }
205 sc->sc_dkdev.dk_openmask =
206 sc->sc_dkdev.dk_copenmask | sc->sc_dkdev.dk_bopenmask;
207
208 vndunlock(sc);
209 return (0);
210 }
211
212 int
213 vndclose(dev, flags, mode, p)
214 dev_t dev;
215 int flags, mode;
216 struct proc *p;
217 {
218 int unit = vndunit(dev);
219 struct vnd_softc *sc;
220 int error = 0, part;
221
222 #ifdef DEBUG
223 if (vnddebug & VDB_FOLLOW)
224 printf("vndclose(%x, %x, %x, %x)\n", dev, flags, mode, p);
225 #endif
226
227 if (unit >= numvnd)
228 return (ENXIO);
229 sc = &vnd_softc[unit];
230
231 if (error = vndlock(sc))
232 return (error);
233
234 part = DISKPART(dev);
235
236 /* ...that much closer to allowing unconfiguration... */
237 switch (mode) {
238 case S_IFCHR:
239 sc->sc_dkdev.dk_copenmask &= ~(1 << part);
240 break;
241
242 case S_IFBLK:
243 sc->sc_dkdev.dk_bopenmask &= ~(1 << part);
244 break;
245 }
246 sc->sc_dkdev.dk_openmask =
247 sc->sc_dkdev.dk_copenmask | sc->sc_dkdev.dk_bopenmask;
248
249 vndunlock(sc);
250 return (0);
251 }
252
253 /*
254 * Break the request into bsize pieces and submit using VOP_BMAP/VOP_STRATEGY.
255 * Note that this driver can only be used for swapping over NFS on the hp
256 * since nfs_strategy on the vax cannot handle u-areas and page tables.
257 */
258 void
259 vndstrategy(bp)
260 register struct buf *bp;
261 {
262 int unit = vndunit(bp->b_dev);
263 register struct vnd_softc *vnd = &vnd_softc[unit];
264 register struct vndbuf *nbp;
265 register int bn, bsize, resid;
266 register caddr_t addr;
267 int sz, flags, error;
268 extern void vndiodone();
269
270 #ifdef DEBUG
271 if (vnddebug & VDB_FOLLOW)
272 printf("vndstrategy(%x): unit %d\n", bp, unit);
273 #endif
274 if ((vnd->sc_flags & VNF_INITED) == 0) {
275 bp->b_error = ENXIO;
276 bp->b_flags |= B_ERROR;
277 biodone(bp);
278 return;
279 }
280 bn = bp->b_blkno;
281 sz = howmany(bp->b_bcount, DEV_BSIZE);
282 bp->b_resid = bp->b_bcount;
283 if (bn < 0 || bn + sz > vnd->sc_size) {
284 if (bn != vnd->sc_size) {
285 bp->b_error = EINVAL;
286 bp->b_flags |= B_ERROR;
287 }
288 biodone(bp);
289 return;
290 }
291 bn = dbtob(bn);
292 bsize = vnd->sc_vp->v_mount->mnt_stat.f_iosize;
293 addr = bp->b_data;
294 flags = bp->b_flags | B_CALL;
295 for (resid = bp->b_resid; resid; resid -= sz) {
296 struct vnode *vp;
297 daddr_t nbn;
298 int off, s, nra;
299
300 nra = 0;
301 VOP_LOCK(vnd->sc_vp);
302 error = VOP_BMAP(vnd->sc_vp, bn / bsize, &vp, &nbn, &nra);
303 VOP_UNLOCK(vnd->sc_vp);
304 if (error == 0 && (long)nbn == -1)
305 error = EIO;
306 #ifdef DEBUG
307 if (!dovndcluster)
308 nra = 0;
309 #endif
310
311 if (off = bn % bsize)
312 sz = bsize - off;
313 else
314 sz = (1 + nra) * bsize;
315 if (resid < sz)
316 sz = resid;
317 #ifdef DEBUG
318 if (vnddebug & VDB_IO)
319 printf("vndstrategy: vp %x/%x bn %x/%x sz %x\n",
320 vnd->sc_vp, vp, bn, nbn, sz);
321 #endif
322
323 nbp = getvndbuf();
324 nbp->vb_buf.b_flags = flags;
325 nbp->vb_buf.b_bcount = sz;
326 nbp->vb_buf.b_bufsize = bp->b_bufsize;
327 nbp->vb_buf.b_error = 0;
328 if (vp->v_type == VBLK || vp->v_type == VCHR)
329 nbp->vb_buf.b_dev = vp->v_rdev;
330 else
331 nbp->vb_buf.b_dev = NODEV;
332 nbp->vb_buf.b_data = addr;
333 nbp->vb_buf.b_blkno = nbn + btodb(off);
334 nbp->vb_buf.b_proc = bp->b_proc;
335 nbp->vb_buf.b_iodone = vndiodone;
336 nbp->vb_buf.b_vp = vp;
337 nbp->vb_buf.b_rcred = vnd->sc_cred; /* XXX crdup? */
338 nbp->vb_buf.b_wcred = vnd->sc_cred; /* XXX crdup? */
339 nbp->vb_buf.b_dirtyoff = bp->b_dirtyoff;
340 nbp->vb_buf.b_dirtyend = bp->b_dirtyend;
341 nbp->vb_buf.b_validoff = bp->b_validoff;
342 nbp->vb_buf.b_validend = bp->b_validend;
343
344 /* save a reference to the old buffer */
345 nbp->vb_obp = bp;
346
347 /*
348 * If there was an error or a hole in the file...punt.
349 * Note that we deal with this after the nbp allocation.
350 * This ensures that we properly clean up any operations
351 * that we have already fired off.
352 *
353 * XXX we could deal with holes here but it would be
354 * a hassle (in the write case).
355 */
356 if (error) {
357 nbp->vb_buf.b_error = error;
358 nbp->vb_buf.b_flags |= B_ERROR;
359 bp->b_resid -= (resid - sz);
360 biodone(&nbp->vb_buf);
361 return;
362 }
363 /*
364 * Just sort by block number
365 */
366 nbp->vb_buf.b_cylin = nbp->vb_buf.b_blkno;
367 s = splbio();
368 disksort(&vnd->sc_tab, &nbp->vb_buf);
369 if (vnd->sc_tab.b_active < vnd->sc_maxactive) {
370 vnd->sc_tab.b_active++;
371 vndstart(vnd);
372 }
373 splx(s);
374 bn += sz;
375 addr += sz;
376 }
377 }
378
379 /*
380 * Feed requests sequentially.
381 * We do it this way to keep from flooding NFS servers if we are connected
382 * to an NFS file. This places the burden on the client rather than the
383 * server.
384 */
385 void
386 vndstart(vnd)
387 register struct vnd_softc *vnd;
388 {
389 register struct buf *bp;
390
391 /*
392 * Dequeue now since lower level strategy routine might
393 * queue using same links
394 */
395 bp = vnd->sc_tab.b_actf;
396 vnd->sc_tab.b_actf = bp->b_actf;
397 #ifdef DEBUG
398 if (vnddebug & VDB_IO)
399 printf("vndstart(%d): bp %x vp %x blkno %x addr %x cnt %x\n",
400 vnd-vnd_softc, bp, bp->b_vp, bp->b_blkno, bp->b_data,
401 bp->b_bcount);
402 #endif
403
404 /* Instrumentation. */
405 disk_busy(&vnd->sc_dkdev);
406
407 if ((bp->b_flags & B_READ) == 0)
408 bp->b_vp->v_numoutput++;
409 VOP_STRATEGY(bp);
410 }
411
412 void
413 vndiodone(vbp)
414 register struct vndbuf *vbp;
415 {
416 register struct buf *pbp = vbp->vb_obp;
417 register struct vnd_softc *vnd = &vnd_softc[vndunit(pbp->b_dev)];
418 int s;
419
420 s = splbio();
421 #ifdef DEBUG
422 if (vnddebug & VDB_IO)
423 printf("vndiodone(%d): vbp %x vp %x blkno %x addr %x cnt %x\n",
424 vnd-vnd_softc, vbp, vbp->vb_buf.b_vp, vbp->vb_buf.b_blkno,
425 vbp->vb_buf.b_data, vbp->vb_buf.b_bcount);
426 #endif
427
428 if (vbp->vb_buf.b_error) {
429 #ifdef DEBUG
430 if (vnddebug & VDB_IO)
431 printf("vndiodone: vbp %x error %d\n", vbp,
432 vbp->vb_buf.b_error);
433 #endif
434 pbp->b_flags |= B_ERROR;
435 pbp->b_error = biowait(&vbp->vb_buf);
436 }
437 pbp->b_resid -= vbp->vb_buf.b_bcount;
438 putvndbuf(vbp);
439 disk_unbusy(&vnd->sc_dkdev, (pbp->b_bcount - pbp->b_resid));
440 if (pbp->b_resid == 0) {
441 #ifdef DEBUG
442 if (vnddebug & VDB_IO)
443 printf("vndiodone: pbp %x iodone\n", pbp);
444 #endif
445 biodone(pbp);
446 }
447 if (vnd->sc_tab.b_actf)
448 vndstart(vnd);
449 else
450 vnd->sc_tab.b_active--;
451 splx(s);
452 }
453
454 /* ARGSUSED */
455 int
456 vndread(dev, uio, flags)
457 dev_t dev;
458 struct uio *uio;
459 int flags;
460 {
461 int unit = vndunit(dev);
462 struct vnd_softc *sc;
463
464 #ifdef DEBUG
465 if (vnddebug & VDB_FOLLOW)
466 printf("vndread(%x, %x)\n", dev, uio);
467 #endif
468
469 if (unit >= numvnd)
470 return (ENXIO);
471 sc = &vnd_softc[unit];
472
473 if ((sc->sc_flags & VNF_INITED) == 0)
474 return (ENXIO);
475
476 return (physio(vndstrategy, NULL, dev, B_READ, minphys, uio));
477 }
478
479 /* ARGSUSED */
480 int
481 vndwrite(dev, uio, flags)
482 dev_t dev;
483 struct uio *uio;
484 int flags;
485 {
486 int unit = vndunit(dev);
487 struct vnd_softc *sc;
488
489 #ifdef DEBUG
490 if (vnddebug & VDB_FOLLOW)
491 printf("vndwrite(%x, %x)\n", dev, uio);
492 #endif
493
494 if (unit >= numvnd)
495 return (ENXIO);
496 sc = &vnd_softc[unit];
497
498 if ((sc->sc_flags & VNF_INITED) == 0)
499 return (ENXIO);
500
501 return (physio(vndstrategy, NULL, dev, B_WRITE, minphys, uio));
502 }
503
504 /* ARGSUSED */
505 int
506 vndioctl(dev, cmd, data, flag, p)
507 dev_t dev;
508 u_long cmd;
509 caddr_t data;
510 int flag;
511 struct proc *p;
512 {
513 int unit = vndunit(dev);
514 register struct vnd_softc *vnd;
515 struct vnd_ioctl *vio;
516 struct vattr vattr;
517 struct nameidata nd;
518 int error, part, pmask, s;
519
520 #ifdef DEBUG
521 if (vnddebug & VDB_FOLLOW)
522 printf("vndioctl(%x, %lx, %x, %x, %x): unit %d\n",
523 dev, cmd, data, flag, p, unit);
524 #endif
525 error = suser(p->p_ucred, &p->p_acflag);
526 if (error)
527 return (error);
528 if (unit >= numvnd)
529 return (ENXIO);
530
531 vnd = &vnd_softc[unit];
532 vio = (struct vnd_ioctl *)data;
533 switch (cmd) {
534
535 case VNDIOCSET:
536 if (vnd->sc_flags & VNF_INITED)
537 return (EBUSY);
538
539 if (error = vndlock(vnd))
540 return (error);
541
542 /*
543 * Always open for read and write.
544 * This is probably bogus, but it lets vn_open()
545 * weed out directories, sockets, etc. so we don't
546 * have to worry about them.
547 */
548 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, vio->vnd_file, p);
549 if (error = vn_open(&nd, FREAD|FWRITE, 0)) {
550 vndunlock(vnd);
551 return(error);
552 }
553 if (error = VOP_GETATTR(nd.ni_vp, &vattr, p->p_ucred, p)) {
554 VOP_UNLOCK(nd.ni_vp);
555 (void) vn_close(nd.ni_vp, FREAD|FWRITE, p->p_ucred, p);
556 vndunlock(vnd);
557 return(error);
558 }
559 VOP_UNLOCK(nd.ni_vp);
560 vnd->sc_vp = nd.ni_vp;
561 vnd->sc_size = btodb(vattr.va_size); /* note truncation */
562 if (error = vndsetcred(vnd, p->p_ucred)) {
563 (void) vn_close(nd.ni_vp, FREAD|FWRITE, p->p_ucred, p);
564 vndunlock(vnd);
565 return(error);
566 }
567 vndthrottle(vnd, vnd->sc_vp);
568 vio->vnd_size = dbtob(vnd->sc_size);
569 vnd->sc_flags |= VNF_INITED;
570 #ifdef DEBUG
571 if (vnddebug & VDB_INIT)
572 printf("vndioctl: SET vp %x size %x\n",
573 vnd->sc_vp, vnd->sc_size);
574 #endif
575
576 /* Attach the disk. */
577 bzero(vnd->sc_xname, sizeof(vnd->sc_xname)); /* XXX */
578 sprintf(vnd->sc_xname, "vnd%d", unit); /* XXX */
579 vnd->sc_dkdev.dk_name = vnd->sc_xname;
580 disk_attach(&vnd->sc_dkdev);
581
582 vndunlock(vnd);
583
584 break;
585
586 case VNDIOCCLR:
587 if ((vnd->sc_flags & VNF_INITED) == 0)
588 return (ENXIO);
589
590 if (error = vndlock(vnd))
591 return (error);
592
593 /*
594 * Don't unconfigure if any other partitions are open
595 * or if both the character and block flavors of this
596 * partition are open.
597 */
598 part = DISKPART(dev);
599 pmask = (1 << part);
600 if ((vnd->sc_dkdev.dk_openmask & ~pmask) ||
601 ((vnd->sc_dkdev.dk_bopenmask & pmask) &&
602 (vnd->sc_dkdev.dk_copenmask & pmask))) {
603 vndunlock(vnd);
604 return (EBUSY);
605 }
606
607 vndclear(vnd);
608 #ifdef DEBUG
609 if (vnddebug & VDB_INIT)
610 printf("vndioctl: CLRed\n");
611 #endif
612
613 /* Detatch the disk. */
614 disk_detatch(&vnd->sc_dkdev);
615
616 /* This must be atomic. */
617 s = splhigh();
618 vndunlock(vnd);
619 bzero(vnd, sizeof(struct vnd_softc));
620 splx(s);
621
622 break;
623
624 /*
625 * XXX Should support disklabels.
626 */
627
628 default:
629 return(ENOTTY);
630 }
631
632 return (0);
633 }
634
635 /*
636 * Duplicate the current processes' credentials. Since we are called only
637 * as the result of a SET ioctl and only root can do that, any future access
638 * to this "disk" is essentially as root. Note that credentials may change
639 * if some other uid can write directly to the mapped file (NFS).
640 */
641 int
642 vndsetcred(vnd, cred)
643 register struct vnd_softc *vnd;
644 struct ucred *cred;
645 {
646 struct uio auio;
647 struct iovec aiov;
648 char *tmpbuf;
649 int error;
650
651 vnd->sc_cred = crdup(cred);
652 tmpbuf = malloc(DEV_BSIZE, M_TEMP, M_WAITOK);
653
654 /* XXX: Horrible kludge to establish credentials for NFS */
655 aiov.iov_base = tmpbuf;
656 aiov.iov_len = min(DEV_BSIZE, dbtob(vnd->sc_size));
657 auio.uio_iov = &aiov;
658 auio.uio_iovcnt = 1;
659 auio.uio_offset = 0;
660 auio.uio_rw = UIO_READ;
661 auio.uio_segflg = UIO_SYSSPACE;
662 auio.uio_resid = aiov.iov_len;
663 VOP_LOCK(vnd->sc_vp);
664 error = VOP_READ(vnd->sc_vp, &auio, 0, vnd->sc_cred);
665 VOP_UNLOCK(vnd->sc_vp);
666
667 free(tmpbuf, M_TEMP);
668 return (error);
669 }
670
671 /*
672 * Set maxactive based on FS type
673 */
674 void
675 vndthrottle(vnd, vp)
676 register struct vnd_softc *vnd;
677 struct vnode *vp;
678 {
679 #ifdef NFSCLIENT
680 extern int (**nfsv2_vnodeop_p)();
681
682 if (vp->v_op == nfsv2_vnodeop_p)
683 vnd->sc_maxactive = 2;
684 else
685 #endif
686 vnd->sc_maxactive = 8;
687
688 if (vnd->sc_maxactive < 1)
689 vnd->sc_maxactive = 1;
690 }
691
692 void
693 vndshutdown()
694 {
695 register struct vnd_softc *vnd;
696
697 for (vnd = &vnd_softc[0]; vnd < &vnd_softc[numvnd]; vnd++)
698 if (vnd->sc_flags & VNF_INITED)
699 vndclear(vnd);
700 }
701
702 void
703 vndclear(vnd)
704 register struct vnd_softc *vnd;
705 {
706 register struct vnode *vp = vnd->sc_vp;
707 struct proc *p = curproc; /* XXX */
708
709 #ifdef DEBUG
710 if (vnddebug & VDB_FOLLOW)
711 printf("vndclear(%x): vp %x\n", vp);
712 #endif
713 vnd->sc_flags &= ~VNF_INITED;
714 if (vp == (struct vnode *)0)
715 panic("vndioctl: null vp");
716 (void) vn_close(vp, FREAD|FWRITE, vnd->sc_cred, p);
717 crfree(vnd->sc_cred);
718 vnd->sc_vp = (struct vnode *)0;
719 vnd->sc_cred = (struct ucred *)0;
720 vnd->sc_size = 0;
721 }
722
723 int
724 vndsize(dev)
725 dev_t dev;
726 {
727 int unit = vndunit(dev);
728 register struct vnd_softc *vnd = &vnd_softc[unit];
729
730 if (unit >= numvnd || (vnd->sc_flags & VNF_INITED) == 0)
731 return(-1);
732 return(vnd->sc_size);
733 }
734
735 int
736 vnddump(dev, blkno, va, size)
737 dev_t dev;
738 daddr_t blkno;
739 caddr_t va;
740 size_t size;
741 {
742
743 /* Not implemented. */
744 return ENXIO;
745 }
746
747 /*
748 * Wait interruptibly for an exclusive lock.
749 *
750 * XXX
751 * Several drivers do this; it should be abstracted and made MP-safe.
752 */
753 static int
754 vndlock(sc)
755 struct vnd_softc *sc;
756 {
757 int error;
758
759 while ((sc->sc_flags & VNF_LOCKED) != 0) {
760 sc->sc_flags |= VNF_WANTED;
761 if ((error = tsleep(sc, PRIBIO | PCATCH, "vndlck", 0)) != 0)
762 return (error);
763 }
764 sc->sc_flags |= VNF_LOCKED;
765 return (0);
766 }
767
768 /*
769 * Unlock and wake up any waiters.
770 */
771 static void
772 vndunlock(sc)
773 struct vnd_softc *sc;
774 {
775
776 sc->sc_flags &= ~VNF_LOCKED;
777 if ((sc->sc_flags & VNF_WANTED) != 0) {
778 sc->sc_flags &= ~VNF_WANTED;
779 wakeup(sc);
780 }
781 }
782