xref: /openbsd-src/sys/dev/vnd.c (revision 0b7734b3d77bb9b21afec6f4621cae6c805dbd45)
1 /*	$OpenBSD: vnd.c,v 1.158 2016/03/19 12:04:15 natano Exp $	*/
2 /*	$NetBSD: vnd.c,v 1.26 1996/03/30 23:06:11 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1988 University of Utah.
6  * Copyright (c) 1990, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * This code is derived from software contributed to Berkeley by
10  * the Systems Programming Group of the University of Utah Computer
11  * Science Department.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 /*
39  * There is a security issue involved with this driver.
40  *
41  * Once mounted all access to the contents of the "mapped" file via
42  * the special file is controlled by the permissions on the special
43  * file, the protection of the mapped file is ignored (effectively,
44  * by using root credentials in all transactions).
45  *
46  */
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/namei.h>
51 #include <sys/proc.h>
52 #include <sys/errno.h>
53 #include <sys/limits.h>
54 #include <sys/buf.h>
55 #include <sys/malloc.h>
56 #include <sys/ioctl.h>
57 #include <sys/disklabel.h>
58 #include <sys/device.h>
59 #include <sys/disk.h>
60 #include <sys/stat.h>
61 #include <sys/vnode.h>
62 #include <sys/file.h>
63 #include <sys/uio.h>
64 #include <sys/conf.h>
65 #include <sys/dkio.h>
66 #include <sys/specdev.h>
67 
68 #include <crypto/blf.h>
69 
70 #include <dev/vndioctl.h>
71 
72 #ifdef VNDDEBUG
73 int vnddebug = 0x00;
74 #define	VDB_FOLLOW	0x01
75 #define	VDB_INIT	0x02
76 #define	VDB_IO		0x04
77 #define	DNPRINTF(f, p...)	do { if ((f) & vnddebug) printf(p); } while (0)
78 #else
79 #define	DNPRINTF(f, p...)	/* nothing */
80 #endif	/* VNDDEBUG */
81 
82 struct vnd_softc {
83 	struct device	 sc_dev;
84 	struct disk	 sc_dk;
85 
86 	char		 sc_file[VNDNLEN];	/* file we're covering */
87 	int		 sc_flags;		/* flags */
88 	size_t		 sc_size;		/* size of vnd in sectors */
89 	size_t		 sc_secsize;		/* sector size in bytes */
90 	size_t		 sc_nsectors;		/* # of sectors per track */
91 	size_t		 sc_ntracks;		/* # of tracks per cylinder */
92 	struct vnode	*sc_vp;			/* vnode */
93 	struct ucred	*sc_cred;		/* credentials */
94 	blf_ctx		*sc_keyctx;		/* key context */
95 };
96 
97 /* sc_flags */
98 #define	VNF_INITED	0x0001
99 #define	VNF_HAVELABEL	0x0002
100 #define	VNF_READONLY	0x0004
101 
102 #define	VNDRW(v)	((v)->sc_flags & VNF_READONLY ? FREAD : FREAD|FWRITE)
103 
104 struct vnd_softc *vnd_softc;
105 int numvnd = 0;
106 
107 /* called by main() at boot time */
108 void	vndattach(int);
109 
110 void	vndclear(struct vnd_softc *);
111 int	vndsetcred(struct vnd_softc *, struct ucred *);
112 int	vndgetdisklabel(dev_t, struct vnd_softc *, struct disklabel *, int);
113 void	vndencrypt(struct vnd_softc *, caddr_t, size_t, daddr_t, int);
114 void	vndencryptbuf(struct vnd_softc *, struct buf *, int);
115 size_t	vndbdevsize(struct vnode *, struct proc *);
116 
117 void
118 vndencrypt(struct vnd_softc *sc, caddr_t addr, size_t size, daddr_t off,
119     int encrypt)
120 {
121 	int i, bsize;
122 	u_char iv[8];
123 
124 	bsize = dbtob(1);
125 	for (i = 0; i < size/bsize; i++) {
126 		memset(iv, 0, sizeof(iv));
127 		memcpy(iv, &off, sizeof(off));
128 		blf_ecb_encrypt(sc->sc_keyctx, iv, sizeof(iv));
129 		if (encrypt)
130 			blf_cbc_encrypt(sc->sc_keyctx, iv, addr, bsize);
131 		else
132 			blf_cbc_decrypt(sc->sc_keyctx, iv, addr, bsize);
133 
134 		addr += bsize;
135 		off++;
136 	}
137 }
138 
139 void
140 vndencryptbuf(struct vnd_softc *sc, struct buf *bp, int encrypt)
141 {
142 	vndencrypt(sc, bp->b_data, bp->b_bcount, bp->b_blkno, encrypt);
143 }
144 
145 void
146 vndattach(int num)
147 {
148 	char *mem;
149 	int i;
150 
151 	if (num <= 0)
152 		return;
153 	mem = mallocarray(num, sizeof(struct vnd_softc), M_DEVBUF,
154 	    M_NOWAIT | M_ZERO);
155 	if (mem == NULL) {
156 		printf("WARNING: no memory for vnode disks\n");
157 		return;
158 	}
159 	vnd_softc = (struct vnd_softc *)mem;
160 	for (i = 0; i < num; i++) {
161 		struct vnd_softc *sc = &vnd_softc[i];
162 
163 		sc->sc_dev.dv_unit = i;
164 		snprintf(sc->sc_dev.dv_xname, sizeof(sc->sc_dev.dv_xname),
165 		    "vnd%d", i);
166 		disk_construct(&sc->sc_dk);
167 		device_ref(&sc->sc_dev);
168 	}
169 	numvnd = num;
170 }
171 
172 int
173 vndopen(dev_t dev, int flags, int mode, struct proc *p)
174 {
175 	int unit = DISKUNIT(dev);
176 	struct vnd_softc *sc;
177 	int error = 0, part;
178 
179 	DNPRINTF(VDB_FOLLOW, "vndopen(%x, %x, %x, %p)\n", dev, flags, mode, p);
180 
181 	if (unit >= numvnd)
182 		return (ENXIO);
183 	sc = &vnd_softc[unit];
184 
185 	if ((error = disk_lock(&sc->sc_dk)) != 0)
186 		return (error);
187 
188 	if ((flags & FWRITE) && (sc->sc_flags & VNF_READONLY)) {
189 		error = EROFS;
190 		goto bad;
191 	}
192 
193 	if ((sc->sc_flags & VNF_INITED) &&
194 	    (sc->sc_flags & VNF_HAVELABEL) == 0 &&
195 	    sc->sc_dk.dk_openmask == 0) {
196 		sc->sc_flags |= VNF_HAVELABEL;
197 		vndgetdisklabel(dev, sc, sc->sc_dk.dk_label, 0);
198 	}
199 
200 	part = DISKPART(dev);
201 	error = disk_openpart(&sc->sc_dk, part, mode,
202 	    (sc->sc_flags & VNF_HAVELABEL) != 0);
203 
204 bad:
205 	disk_unlock(&sc->sc_dk);
206 	return (error);
207 }
208 
209 /*
210  * Load the label information on the named device
211  */
212 int
213 vndgetdisklabel(dev_t dev, struct vnd_softc *sc, struct disklabel *lp,
214     int spoofonly)
215 {
216 	memset(lp, 0, sizeof(struct disklabel));
217 
218 	lp->d_secsize = sc->sc_secsize;
219 	lp->d_nsectors = sc->sc_nsectors;
220 	lp->d_ntracks = sc->sc_ntracks;
221 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
222 	lp->d_ncylinders = sc->sc_size / lp->d_secpercyl;
223 
224 	strncpy(lp->d_typename, "vnd device", sizeof(lp->d_typename));
225 	lp->d_type = DTYPE_VND;
226 	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
227 	DL_SETDSIZE(lp, sc->sc_size);
228 	lp->d_flags = 0;
229 	lp->d_version = 1;
230 
231 	lp->d_magic = DISKMAGIC;
232 	lp->d_magic2 = DISKMAGIC;
233 	lp->d_checksum = dkcksum(lp);
234 
235 	/* Call the generic disklabel extraction routine */
236 	return readdisklabel(DISKLABELDEV(dev), vndstrategy, lp, spoofonly);
237 }
238 
239 int
240 vndclose(dev_t dev, int flags, int mode, struct proc *p)
241 {
242 	int unit = DISKUNIT(dev);
243 	struct vnd_softc *sc;
244 	int part;
245 
246 	DNPRINTF(VDB_FOLLOW, "vndclose(%x, %x, %x, %p)\n", dev, flags, mode, p);
247 
248 	if (unit >= numvnd)
249 		return (ENXIO);
250 	sc = &vnd_softc[unit];
251 
252 	disk_lock_nointr(&sc->sc_dk);
253 
254 	part = DISKPART(dev);
255 
256 	disk_closepart(&sc->sc_dk, part, mode);
257 
258 #if 0
259 	if (sc->sc_dk.dk_openmask == 0)
260 		sc->sc_flags &= ~VNF_HAVELABEL;
261 #endif
262 
263 	disk_unlock(&sc->sc_dk);
264 	return (0);
265 }
266 
267 void
268 vndstrategy(struct buf *bp)
269 {
270 	int unit = DISKUNIT(bp->b_dev);
271 	struct vnd_softc *sc;
272 	struct partition *p;
273 	off_t off;
274 	long origbcount;
275 	int s;
276 
277 	DNPRINTF(VDB_FOLLOW, "vndstrategy(%p): unit %d\n", bp, unit);
278 
279 	if (unit >= numvnd) {
280 		bp->b_error = ENXIO;
281 		goto bad;
282 	}
283 	sc = &vnd_softc[unit];
284 
285 	if ((sc->sc_flags & VNF_HAVELABEL) == 0) {
286 		bp->b_error = ENXIO;
287 		goto bad;
288 	}
289 
290 	/*
291 	 * Many of the distrib scripts assume they can issue arbitrary
292 	 * sized requests to raw vnd devices irrespective of the
293 	 * emulated disk geometry.
294 	 *
295 	 * To continue supporting this, round the block count up to a
296 	 * multiple of d_secsize for bounds_check_with_label(), and
297 	 * then restore afterwards.
298 	 *
299 	 * We only do this for non-encrypted vnd, because encryption
300 	 * requires operating on blocks at a time.
301 	 */
302 	origbcount = bp->b_bcount;
303 	if (sc->sc_keyctx == NULL) {
304 		u_int32_t secsize = sc->sc_dk.dk_label->d_secsize;
305 		bp->b_bcount = ((origbcount + secsize - 1) & ~(secsize - 1));
306 #ifdef DIAGNOSTIC
307 		if (bp->b_bcount != origbcount) {
308 			struct proc *pr = curproc;
309 			printf("%s: sloppy %s from proc %d (%s): "
310 			    "blkno %lld bcount %ld\n", sc->sc_dev.dv_xname,
311 			    (bp->b_flags & B_READ) ? "read" : "write",
312 			    pr->p_pid, pr->p_comm, (long long)bp->b_blkno,
313 			    origbcount);
314 		}
315 #endif
316 	}
317 
318 	if (bounds_check_with_label(bp, sc->sc_dk.dk_label) == -1) {
319 		bp->b_resid = bp->b_bcount = origbcount;
320 		goto done;
321 	}
322 
323 	if (origbcount < bp->b_bcount)
324 		bp->b_bcount = origbcount;
325 
326 	p = &sc->sc_dk.dk_label->d_partitions[DISKPART(bp->b_dev)];
327 	off = DL_GETPOFFSET(p) * sc->sc_dk.dk_label->d_secsize +
328 	    (u_int64_t)bp->b_blkno * DEV_BSIZE;
329 
330 	if (sc->sc_keyctx && !(bp->b_flags & B_READ))
331 		vndencryptbuf(sc, bp, 1);
332 
333 	/*
334 	 * Use IO_NOLIMIT because upper layer has already checked I/O
335 	 * for limits, so there is no need to do it again.
336 	 */
337 	bp->b_error = vn_rdwr((bp->b_flags & B_READ) ? UIO_READ : UIO_WRITE,
338 	    sc->sc_vp, bp->b_data, bp->b_bcount, off, UIO_SYSSPACE, IO_NOLIMIT,
339 	    sc->sc_cred, &bp->b_resid, curproc);
340 	if (bp->b_error)
341 		bp->b_flags |= B_ERROR;
342 
343 	/* Data in buffer cache needs to be in clear */
344 	if (sc->sc_keyctx)
345 		vndencryptbuf(sc, bp, 0);
346 
347 	goto done;
348 
349  bad:
350 	bp->b_flags |= B_ERROR;
351 	bp->b_resid = bp->b_bcount;
352  done:
353 	s = splbio();
354 	biodone(bp);
355 	splx(s);
356 }
357 
358 /* ARGSUSED */
359 int
360 vndread(dev_t dev, struct uio *uio, int flags)
361 {
362 	return (physio(vndstrategy, dev, B_READ, minphys, uio));
363 }
364 
365 /* ARGSUSED */
366 int
367 vndwrite(dev_t dev, struct uio *uio, int flags)
368 {
369 	return (physio(vndstrategy, dev, B_WRITE, minphys, uio));
370 }
371 
372 size_t
373 vndbdevsize(struct vnode *vp, struct proc *p)
374 {
375 	struct partinfo pi;
376 	struct bdevsw *bsw;
377 	dev_t dev;
378 
379 	dev = vp->v_rdev;
380 	bsw = bdevsw_lookup(dev);
381 	if (bsw->d_ioctl == NULL)
382 		return (0);
383 	if (bsw->d_ioctl(dev, DIOCGPART, (caddr_t)&pi, FREAD, p))
384 		return (0);
385 	DNPRINTF(VDB_INIT, "vndbdevsize: size %llu secsize %u\n",
386 	    DL_GETPSIZE(pi.part), pi.disklab->d_secsize);
387 	return (DL_GETPSIZE(pi.part));
388 }
389 
390 /* ARGSUSED */
391 int
392 vndioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
393 {
394 	int unit = DISKUNIT(dev);
395 	struct disklabel *lp;
396 	struct vnd_softc *sc;
397 	struct vnd_ioctl *vio;
398 	struct vnd_user *vnu;
399 	struct vattr vattr;
400 	struct nameidata nd;
401 	int error, part, pmask;
402 
403 	DNPRINTF(VDB_FOLLOW, "vndioctl(%x, %lx, %p, %x, %p): unit %d\n",
404 	    dev, cmd, addr, flag, p, unit);
405 
406 	error = suser(p, 0);
407 	if (error)
408 		return (error);
409 	if (unit >= numvnd)
410 		return (ENXIO);
411 
412 	sc = &vnd_softc[unit];
413 	vio = (struct vnd_ioctl *)addr;
414 	switch (cmd) {
415 
416 	case VNDIOCSET:
417 		if (sc->sc_flags & VNF_INITED)
418 			return (EBUSY);
419 
420 		/* Geometry eventually has to fit into label fields */
421 		if (vio->vnd_secsize > UINT_MAX ||
422 		    vio->vnd_ntracks > UINT_MAX ||
423 		    vio->vnd_nsectors > UINT_MAX)
424 			return (EINVAL);
425 
426 		if ((error = disk_lock(&sc->sc_dk)) != 0)
427 			return (error);
428 
429 		if ((error = copyinstr(vio->vnd_file, sc->sc_file,
430 		    sizeof(sc->sc_file), NULL))) {
431 			disk_unlock(&sc->sc_dk);
432 			return (error);
433 		}
434 
435 		/* Set geometry for device. */
436 		sc->sc_secsize = vio->vnd_secsize;
437 		sc->sc_ntracks = vio->vnd_ntracks;
438 		sc->sc_nsectors = vio->vnd_nsectors;
439 
440 		/*
441 		 * Open for read and write first. This lets vn_open() weed out
442 		 * directories, sockets, etc. so we don't have to worry about
443 		 * them.
444 		 */
445 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, vio->vnd_file, p);
446 		sc->sc_flags &= ~VNF_READONLY;
447 		error = vn_open(&nd, FREAD|FWRITE, 0);
448 		if (error == EROFS) {
449 			sc->sc_flags |= VNF_READONLY;
450 			error = vn_open(&nd, FREAD, 0);
451 		}
452 		if (error) {
453 			disk_unlock(&sc->sc_dk);
454 			return (error);
455 		}
456 
457 		if (nd.ni_vp->v_type == VBLK)
458 			sc->sc_size = vndbdevsize(nd.ni_vp, p);
459 		else {
460 			error = VOP_GETATTR(nd.ni_vp, &vattr, p->p_ucred, p);
461 			if (error) {
462 				VOP_UNLOCK(nd.ni_vp, p);
463 				vn_close(nd.ni_vp, VNDRW(sc), p->p_ucred, p);
464 				disk_unlock(&sc->sc_dk);
465 				return (error);
466 			}
467 			sc->sc_size = vattr.va_size / sc->sc_secsize;
468 		}
469 		VOP_UNLOCK(nd.ni_vp, p);
470 		sc->sc_vp = nd.ni_vp;
471 		if ((error = vndsetcred(sc, p->p_ucred)) != 0) {
472 			(void) vn_close(nd.ni_vp, VNDRW(sc), p->p_ucred, p);
473 			disk_unlock(&sc->sc_dk);
474 			return (error);
475 		}
476 
477 		if (vio->vnd_keylen > 0) {
478 			char key[BLF_MAXUTILIZED];
479 
480 			if (vio->vnd_keylen > sizeof(key))
481 				vio->vnd_keylen = sizeof(key);
482 
483 			if ((error = copyin(vio->vnd_key, key,
484 			    vio->vnd_keylen)) != 0) {
485 				(void) vn_close(nd.ni_vp, VNDRW(sc),
486 				    p->p_ucred, p);
487 				disk_unlock(&sc->sc_dk);
488 				return (error);
489 			}
490 
491 			sc->sc_keyctx = malloc(sizeof(*sc->sc_keyctx), M_DEVBUF,
492 			    M_WAITOK);
493 			blf_key(sc->sc_keyctx, key, vio->vnd_keylen);
494 			explicit_bzero(key, vio->vnd_keylen);
495 		} else
496 			sc->sc_keyctx = NULL;
497 
498 		vio->vnd_size = sc->sc_size * sc->sc_secsize;
499 		sc->sc_flags |= VNF_INITED;
500 
501 		DNPRINTF(VDB_INIT, "vndioctl: SET vp %p size %llx\n",
502 		    sc->sc_vp, (unsigned long long)sc->sc_size);
503 
504 		/* Attach the disk. */
505 		sc->sc_dk.dk_name = sc->sc_dev.dv_xname;
506 		disk_attach(&sc->sc_dev, &sc->sc_dk);
507 
508 		disk_unlock(&sc->sc_dk);
509 
510 		break;
511 
512 	case VNDIOCCLR:
513 		if ((sc->sc_flags & VNF_INITED) == 0)
514 			return (ENXIO);
515 
516 		if ((error = disk_lock(&sc->sc_dk)) != 0)
517 			return (error);
518 
519 		/*
520 		 * Don't unconfigure if any other partitions are open
521 		 * or if both the character and block flavors of this
522 		 * partition are open.
523 		 */
524 		part = DISKPART(dev);
525 		pmask = (1 << part);
526 		if ((sc->sc_dk.dk_openmask & ~pmask) ||
527 		    ((sc->sc_dk.dk_bopenmask & pmask) &&
528 		    (sc->sc_dk.dk_copenmask & pmask))) {
529 			disk_unlock(&sc->sc_dk);
530 			return (EBUSY);
531 		}
532 
533 		vndclear(sc);
534 		DNPRINTF(VDB_INIT, "vndioctl: CLRed\n");
535 
536 		/* Free crypto key */
537 		if (sc->sc_keyctx) {
538 			explicit_bzero(sc->sc_keyctx, sizeof(*sc->sc_keyctx));
539 			free(sc->sc_keyctx, M_DEVBUF, sizeof(*sc->sc_keyctx));
540 		}
541 
542 		/* Detach the disk. */
543 		disk_detach(&sc->sc_dk);
544 		disk_unlock(&sc->sc_dk);
545 		break;
546 
547 	case VNDIOCGET:
548 		vnu = (struct vnd_user *)addr;
549 
550 		if (vnu->vnu_unit == -1)
551 			vnu->vnu_unit = unit;
552 		if (vnu->vnu_unit >= numvnd)
553 			return (ENXIO);
554 		if (vnu->vnu_unit < 0)
555 			return (EINVAL);
556 
557 		sc = &vnd_softc[vnu->vnu_unit];
558 
559 		if (sc->sc_flags & VNF_INITED) {
560 			error = VOP_GETATTR(sc->sc_vp, &vattr, p->p_ucred, p);
561 			if (error)
562 				return (error);
563 
564 			strlcpy(vnu->vnu_file, sc->sc_file,
565 			    sizeof(vnu->vnu_file));
566 			vnu->vnu_dev = vattr.va_fsid;
567 			vnu->vnu_ino = vattr.va_fileid;
568 		} else {
569 			vnu->vnu_dev = 0;
570 			vnu->vnu_ino = 0;
571 		}
572 
573 		break;
574 
575 	case DIOCRLDINFO:
576 		if ((sc->sc_flags & VNF_HAVELABEL) == 0)
577 			return (ENOTTY);
578 		lp = malloc(sizeof(*lp), M_TEMP, M_WAITOK);
579 		vndgetdisklabel(dev, sc, lp, 0);
580 		*(sc->sc_dk.dk_label) = *lp;
581 		free(lp, M_TEMP, sizeof(*lp));
582 		return (0);
583 
584 	case DIOCGPDINFO:
585 		if ((sc->sc_flags & VNF_HAVELABEL) == 0)
586 			return (ENOTTY);
587 		vndgetdisklabel(dev, sc, (struct disklabel *)addr, 1);
588 		return (0);
589 
590 	case DIOCGDINFO:
591 		if ((sc->sc_flags & VNF_HAVELABEL) == 0)
592 			return (ENOTTY);
593 		*(struct disklabel *)addr = *(sc->sc_dk.dk_label);
594 		return (0);
595 
596 	case DIOCGPART:
597 		if ((sc->sc_flags & VNF_HAVELABEL) == 0)
598 			return (ENOTTY);
599 		((struct partinfo *)addr)->disklab = sc->sc_dk.dk_label;
600 		((struct partinfo *)addr)->part =
601 		    &sc->sc_dk.dk_label->d_partitions[DISKPART(dev)];
602 		return (0);
603 
604 	case DIOCWDINFO:
605 	case DIOCSDINFO:
606 		if ((sc->sc_flags & VNF_HAVELABEL) == 0)
607 			return (ENOTTY);
608 		if ((flag & FWRITE) == 0)
609 			return (EBADF);
610 
611 		if ((error = disk_lock(&sc->sc_dk)) != 0)
612 			return (error);
613 
614 		error = setdisklabel(sc->sc_dk.dk_label,
615 		    (struct disklabel *)addr, /* sc->sc_dk.dk_openmask */ 0);
616 		if (error == 0) {
617 			if (cmd == DIOCWDINFO)
618 				error = writedisklabel(DISKLABELDEV(dev),
619 				    vndstrategy, sc->sc_dk.dk_label);
620 		}
621 
622 		disk_unlock(&sc->sc_dk);
623 		return (error);
624 
625 	default:
626 		return (ENOTTY);
627 	}
628 
629 	return (0);
630 }
631 
632 /*
633  * Duplicate the current processes' credentials.  Since we are called only
634  * as the result of a SET ioctl and only root can do that, any future access
635  * to this "disk" is essentially as root.  Note that credentials may change
636  * if some other uid can write directly to the mapped file (NFS).
637  */
638 int
639 vndsetcred(struct vnd_softc *sc, struct ucred *cred)
640 {
641 	void *buf;
642 	size_t size;
643 	int error;
644 
645 	sc->sc_cred = crdup(cred);
646 	buf = malloc(DEV_BSIZE, M_TEMP, M_WAITOK);
647 	size = MIN(DEV_BSIZE, sc->sc_size * sc->sc_secsize);
648 
649 	/* XXX: Horrible kludge to establish credentials for NFS */
650 	error = vn_rdwr(UIO_READ, sc->sc_vp, buf, size, 0, UIO_SYSSPACE, 0,
651 	    sc->sc_cred, NULL, curproc);
652 
653 	free(buf, M_TEMP, DEV_BSIZE);
654 	return (error);
655 }
656 
657 void
658 vndclear(struct vnd_softc *sc)
659 {
660 	struct vnode *vp = sc->sc_vp;
661 	struct proc *p = curproc;		/* XXX */
662 
663 	DNPRINTF(VDB_FOLLOW, "vndclear(%p): vp %p\n", sc, vp);
664 
665 	if (vp == NULL)
666 		panic("vndioctl: null vp");
667 	(void) vn_close(vp, VNDRW(sc), sc->sc_cred, p);
668 	crfree(sc->sc_cred);
669 	sc->sc_flags = 0;
670 	sc->sc_vp = NULL;
671 	sc->sc_cred = NULL;
672 	sc->sc_size = 0;
673 	memset(sc->sc_file, 0, sizeof(sc->sc_file));
674 }
675 
676 daddr_t
677 vndsize(dev_t dev)
678 {
679 	/* We don't support swapping to vnd anymore. */
680 	return (-1);
681 }
682 
683 int
684 vnddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
685 {
686 	/* Not implemented. */
687 	return (ENXIO);
688 }
689