xref: /openbsd-src/sys/dev/vnd.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: vnd.c,v 1.153 2014/07/12 18:48:51 tedu 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 		bzero(iv, sizeof(iv));
127 		bcopy(&off, iv, 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 	u_long size;
150 	int i;
151 
152 	if (num <= 0)
153 		return;
154 	size = num * sizeof(struct vnd_softc);
155 	mem = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
156 	if (mem == NULL) {
157 		printf("WARNING: no memory for vnode disks\n");
158 		return;
159 	}
160 	vnd_softc = (struct vnd_softc *)mem;
161 	for (i = 0; i < num; i++) {
162 		struct vnd_softc *sc = &vnd_softc[i];
163 
164 		sc->sc_dev.dv_unit = i;
165 		snprintf(sc->sc_dev.dv_xname, sizeof(sc->sc_dev.dv_xname),
166 		    "vnd%d", i);
167 		disk_construct(&sc->sc_dk);
168 		device_ref(&sc->sc_dev);
169 	}
170 	numvnd = num;
171 }
172 
173 int
174 vndopen(dev_t dev, int flags, int mode, struct proc *p)
175 {
176 	int unit = DISKUNIT(dev);
177 	struct vnd_softc *sc;
178 	int error = 0, part;
179 
180 	DNPRINTF(VDB_FOLLOW, "vndopen(%x, %x, %x, %p)\n", dev, flags, mode, p);
181 
182 	if (unit >= numvnd)
183 		return (ENXIO);
184 	sc = &vnd_softc[unit];
185 
186 	if ((error = disk_lock(&sc->sc_dk)) != 0)
187 		return (error);
188 
189 	if ((flags & FWRITE) && (sc->sc_flags & VNF_READONLY)) {
190 		error = EROFS;
191 		goto bad;
192 	}
193 
194 	if ((sc->sc_flags & VNF_INITED) &&
195 	    (sc->sc_flags & VNF_HAVELABEL) == 0 &&
196 	    sc->sc_dk.dk_openmask == 0) {
197 		sc->sc_flags |= VNF_HAVELABEL;
198 		vndgetdisklabel(dev, sc, sc->sc_dk.dk_label, 0);
199 	}
200 
201 	part = DISKPART(dev);
202 	error = disk_openpart(&sc->sc_dk, part, mode,
203 	    (sc->sc_flags & VNF_HAVELABEL) != 0);
204 
205 bad:
206 	disk_unlock(&sc->sc_dk);
207 	return (error);
208 }
209 
210 /*
211  * Load the label information on the named device
212  */
213 int
214 vndgetdisklabel(dev_t dev, struct vnd_softc *sc, struct disklabel *lp,
215     int spoofonly)
216 {
217 	bzero(lp, sizeof(struct disklabel));
218 
219 	lp->d_secsize = sc->sc_secsize;
220 	lp->d_nsectors = sc->sc_nsectors;
221 	lp->d_ntracks = sc->sc_ntracks;
222 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
223 	lp->d_ncylinders = sc->sc_size / lp->d_secpercyl;
224 
225 	strncpy(lp->d_typename, "vnd device", sizeof(lp->d_typename));
226 	lp->d_type = DTYPE_VND;
227 	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
228 	DL_SETDSIZE(lp, sc->sc_size);
229 	lp->d_flags = 0;
230 	lp->d_version = 1;
231 
232 	lp->d_magic = DISKMAGIC;
233 	lp->d_magic2 = DISKMAGIC;
234 	lp->d_checksum = dkcksum(lp);
235 
236 	/* Call the generic disklabel extraction routine */
237 	return readdisklabel(DISKLABELDEV(dev), vndstrategy, lp, spoofonly);
238 }
239 
240 int
241 vndclose(dev_t dev, int flags, int mode, struct proc *p)
242 {
243 	int unit = DISKUNIT(dev);
244 	struct vnd_softc *sc;
245 	int part;
246 
247 	DNPRINTF(VDB_FOLLOW, "vndclose(%x, %x, %x, %p)\n", dev, flags, mode, p);
248 
249 	if (unit >= numvnd)
250 		return (ENXIO);
251 	sc = &vnd_softc[unit];
252 
253 	disk_lock_nointr(&sc->sc_dk);
254 
255 	part = DISKPART(dev);
256 
257 	disk_closepart(&sc->sc_dk, part, mode);
258 
259 #if 0
260 	if (sc->sc_dk.dk_openmask == 0)
261 		sc->sc_flags &= ~VNF_HAVELABEL;
262 #endif
263 
264 	disk_unlock(&sc->sc_dk);
265 	return (0);
266 }
267 
268 void
269 vndstrategy(struct buf *bp)
270 {
271 	int unit = DISKUNIT(bp->b_dev);
272 	struct vnd_softc *sc;
273 	struct partition *p;
274 	off_t off;
275 	long origbcount;
276 	int s;
277 
278 	DNPRINTF(VDB_FOLLOW, "vndstrategy(%p): unit %d\n", bp, unit);
279 
280 	if (unit >= numvnd) {
281 		bp->b_error = ENXIO;
282 		goto bad;
283 	}
284 	sc = &vnd_softc[unit];
285 
286 	if ((sc->sc_flags & VNF_HAVELABEL) == 0) {
287 		bp->b_error = ENXIO;
288 		goto bad;
289 	}
290 
291 	/*
292 	 * Many of the distrib scripts assume they can issue arbitrary
293 	 * sized requests to raw vnd devices irrespective of the
294 	 * emulated disk geometry.
295 	 *
296 	 * To continue supporting this, round the block count up to a
297 	 * multiple of d_secsize for bounds_check_with_label(), and
298 	 * then restore afterwards.
299 	 *
300 	 * We only do this for non-encrypted vnd, because encryption
301 	 * requires operating on blocks at a time.
302 	 */
303 	origbcount = bp->b_bcount;
304 	if (sc->sc_keyctx == NULL) {
305 		u_int32_t secsize = sc->sc_dk.dk_label->d_secsize;
306 		bp->b_bcount = ((origbcount + secsize - 1) & ~(secsize - 1));
307 #ifdef DIAGNOSTIC
308 		if (bp->b_bcount != origbcount) {
309 			struct proc *pr = curproc;
310 			printf("%s: sloppy %s from proc %d (%s): "
311 			    "blkno %lld bcount %ld\n", sc->sc_dev.dv_xname,
312 			    (bp->b_flags & B_READ) ? "read" : "write",
313 			    pr->p_pid, pr->p_comm, (long long)bp->b_blkno,
314 			    origbcount);
315 		}
316 #endif
317 	}
318 
319 	if (bounds_check_with_label(bp, sc->sc_dk.dk_label) == -1) {
320 		bp->b_resid = bp->b_bcount = origbcount;
321 		goto done;
322 	}
323 
324 	if (origbcount < bp->b_bcount)
325 		bp->b_bcount = origbcount;
326 
327 	p = &sc->sc_dk.dk_label->d_partitions[DISKPART(bp->b_dev)];
328 	off = DL_GETPOFFSET(p) * sc->sc_dk.dk_label->d_secsize +
329 	    (u_int64_t)bp->b_blkno * DEV_BSIZE;
330 
331 	if (sc->sc_keyctx && !(bp->b_flags & B_READ))
332 		vndencryptbuf(sc, bp, 1);
333 
334 	/*
335 	 * Use IO_NOLIMIT because upper layer has already checked I/O
336 	 * for limits, so there is no need to do it again.
337 	 */
338 	bp->b_error = vn_rdwr((bp->b_flags & B_READ) ? UIO_READ : UIO_WRITE,
339 	    sc->sc_vp, bp->b_data, bp->b_bcount, off, UIO_SYSSPACE, IO_NOLIMIT,
340 	    sc->sc_cred, &bp->b_resid, curproc);
341 	if (bp->b_error)
342 		bp->b_flags |= B_ERROR;
343 
344 	/* Data in buffer cache needs to be in clear */
345 	if (sc->sc_keyctx)
346 		vndencryptbuf(sc, bp, 0);
347 
348 	goto done;
349 
350  bad:
351 	bp->b_flags |= B_ERROR;
352 	bp->b_resid = bp->b_bcount;
353  done:
354 	s = splbio();
355 	biodone(bp);
356 	splx(s);
357 }
358 
359 /* ARGSUSED */
360 int
361 vndread(dev_t dev, struct uio *uio, int flags)
362 {
363 	return (physio(vndstrategy, dev, B_READ, minphys, uio));
364 }
365 
366 /* ARGSUSED */
367 int
368 vndwrite(dev_t dev, struct uio *uio, int flags)
369 {
370 	return (physio(vndstrategy, dev, B_WRITE, minphys, uio));
371 }
372 
373 size_t
374 vndbdevsize(struct vnode *vp, struct proc *p)
375 {
376 	struct partinfo pi;
377 	struct bdevsw *bsw;
378 	dev_t dev;
379 
380 	dev = vp->v_rdev;
381 	bsw = bdevsw_lookup(dev);
382 	if (bsw->d_ioctl == NULL)
383 		return (0);
384 	if (bsw->d_ioctl(dev, DIOCGPART, (caddr_t)&pi, FREAD, p))
385 		return (0);
386 	DNPRINTF(VDB_INIT, "vndbdevsize: size %llu secsize %u\n",
387 	    DL_GETPSIZE(pi.part), pi.disklab->d_secsize);
388 	return (DL_GETPSIZE(pi.part));
389 }
390 
391 /* ARGSUSED */
392 int
393 vndioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
394 {
395 	int unit = DISKUNIT(dev);
396 	struct disklabel *lp;
397 	struct vnd_softc *sc;
398 	struct vnd_ioctl *vio;
399 	struct vnd_user *vnu;
400 	struct vattr vattr;
401 	struct nameidata nd;
402 	int error, part, pmask;
403 
404 	DNPRINTF(VDB_FOLLOW, "vndioctl(%x, %lx, %p, %x, %p): unit %d\n",
405 	    dev, cmd, addr, flag, p, unit);
406 
407 	error = suser(p, 0);
408 	if (error)
409 		return (error);
410 	if (unit >= numvnd)
411 		return (ENXIO);
412 
413 	sc = &vnd_softc[unit];
414 	vio = (struct vnd_ioctl *)addr;
415 	switch (cmd) {
416 
417 	case VNDIOCSET:
418 		if (sc->sc_flags & VNF_INITED)
419 			return (EBUSY);
420 
421 		/* Geometry eventually has to fit into label fields */
422 		if (vio->vnd_secsize > UINT_MAX ||
423 		    vio->vnd_ntracks > UINT_MAX ||
424 		    vio->vnd_nsectors > UINT_MAX)
425 			return (EINVAL);
426 
427 		if ((error = disk_lock(&sc->sc_dk)) != 0)
428 			return (error);
429 
430 		if ((error = copyinstr(vio->vnd_file, sc->sc_file,
431 		    sizeof(sc->sc_file), NULL))) {
432 			disk_unlock(&sc->sc_dk);
433 			return (error);
434 		}
435 
436 		/* Set geometry for device. */
437 		sc->sc_secsize = vio->vnd_secsize;
438 		sc->sc_ntracks = vio->vnd_ntracks;
439 		sc->sc_nsectors = vio->vnd_nsectors;
440 
441 		/*
442 		 * Open for read and write first. This lets vn_open() weed out
443 		 * directories, sockets, etc. so we don't have to worry about
444 		 * them.
445 		 */
446 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, vio->vnd_file, p);
447 		sc->sc_flags &= ~VNF_READONLY;
448 		error = vn_open(&nd, FREAD|FWRITE, 0);
449 		if (error == EROFS) {
450 			sc->sc_flags |= VNF_READONLY;
451 			error = vn_open(&nd, FREAD, 0);
452 		}
453 		if (error) {
454 			disk_unlock(&sc->sc_dk);
455 			return (error);
456 		}
457 
458 		if (nd.ni_vp->v_type == VBLK)
459 			sc->sc_size = vndbdevsize(nd.ni_vp, p);
460 		else {
461 			error = VOP_GETATTR(nd.ni_vp, &vattr, p->p_ucred, p);
462 			if (error) {
463 				VOP_UNLOCK(nd.ni_vp, 0, p);
464 				vn_close(nd.ni_vp, VNDRW(sc), p->p_ucred, p);
465 				disk_unlock(&sc->sc_dk);
466 				return (error);
467 			}
468 			sc->sc_size = vattr.va_size / sc->sc_secsize;
469 		}
470 		VOP_UNLOCK(nd.ni_vp, 0, p);
471 		sc->sc_vp = nd.ni_vp;
472 		if ((error = vndsetcred(sc, p->p_ucred)) != 0) {
473 			(void) vn_close(nd.ni_vp, VNDRW(sc), p->p_ucred, p);
474 			disk_unlock(&sc->sc_dk);
475 			return (error);
476 		}
477 
478 		if (vio->vnd_keylen > 0) {
479 			char key[BLF_MAXUTILIZED];
480 
481 			if (vio->vnd_keylen > sizeof(key))
482 				vio->vnd_keylen = sizeof(key);
483 
484 			if ((error = copyin(vio->vnd_key, key,
485 			    vio->vnd_keylen)) != 0) {
486 				(void) vn_close(nd.ni_vp, VNDRW(sc),
487 				    p->p_ucred, p);
488 				disk_unlock(&sc->sc_dk);
489 				return (error);
490 			}
491 
492 			sc->sc_keyctx = malloc(sizeof(*sc->sc_keyctx), M_DEVBUF,
493 			    M_WAITOK);
494 			blf_key(sc->sc_keyctx, key, vio->vnd_keylen);
495 			explicit_bzero(key, vio->vnd_keylen);
496 		} else
497 			sc->sc_keyctx = NULL;
498 
499 		vio->vnd_size = sc->sc_size * sc->sc_secsize;
500 		sc->sc_flags |= VNF_INITED;
501 
502 		DNPRINTF(VDB_INIT, "vndioctl: SET vp %p size %llx\n",
503 		    sc->sc_vp, (unsigned long long)sc->sc_size);
504 
505 		/* Attach the disk. */
506 		sc->sc_dk.dk_name = sc->sc_dev.dv_xname;
507 		disk_attach(&sc->sc_dev, &sc->sc_dk);
508 
509 		disk_unlock(&sc->sc_dk);
510 
511 		break;
512 
513 	case VNDIOCCLR:
514 		if ((sc->sc_flags & VNF_INITED) == 0)
515 			return (ENXIO);
516 
517 		if ((error = disk_lock(&sc->sc_dk)) != 0)
518 			return (error);
519 
520 		/*
521 		 * Don't unconfigure if any other partitions are open
522 		 * or if both the character and block flavors of this
523 		 * partition are open.
524 		 */
525 		part = DISKPART(dev);
526 		pmask = (1 << part);
527 		if ((sc->sc_dk.dk_openmask & ~pmask) ||
528 		    ((sc->sc_dk.dk_bopenmask & pmask) &&
529 		    (sc->sc_dk.dk_copenmask & pmask))) {
530 			disk_unlock(&sc->sc_dk);
531 			return (EBUSY);
532 		}
533 
534 		vndclear(sc);
535 		DNPRINTF(VDB_INIT, "vndioctl: CLRed\n");
536 
537 		/* Free crypto key */
538 		if (sc->sc_keyctx) {
539 			explicit_bzero(sc->sc_keyctx, sizeof(*sc->sc_keyctx));
540 			free(sc->sc_keyctx, M_DEVBUF, 0);
541 		}
542 
543 		/* Detach the disk. */
544 		disk_detach(&sc->sc_dk);
545 		disk_unlock(&sc->sc_dk);
546 		break;
547 
548 	case VNDIOCGET:
549 		vnu = (struct vnd_user *)addr;
550 
551 		if (vnu->vnu_unit == -1)
552 			vnu->vnu_unit = unit;
553 		if (vnu->vnu_unit >= numvnd)
554 			return (ENXIO);
555 		if (vnu->vnu_unit < 0)
556 			return (EINVAL);
557 
558 		sc = &vnd_softc[vnu->vnu_unit];
559 
560 		if (sc->sc_flags & VNF_INITED) {
561 			error = VOP_GETATTR(sc->sc_vp, &vattr, p->p_ucred, p);
562 			if (error)
563 				return (error);
564 
565 			strlcpy(vnu->vnu_file, sc->sc_file,
566 			    sizeof(vnu->vnu_file));
567 			vnu->vnu_dev = vattr.va_fsid;
568 			vnu->vnu_ino = vattr.va_fileid;
569 		} else {
570 			vnu->vnu_dev = 0;
571 			vnu->vnu_ino = 0;
572 		}
573 
574 		break;
575 
576 	case DIOCRLDINFO:
577 		if ((sc->sc_flags & VNF_HAVELABEL) == 0)
578 			return (ENOTTY);
579 		lp = malloc(sizeof(*lp), M_TEMP, M_WAITOK);
580 		vndgetdisklabel(dev, sc, lp, 0);
581 		*(sc->sc_dk.dk_label) = *lp;
582 		free(lp, M_TEMP, 0);
583 		return (0);
584 
585 	case DIOCGPDINFO:
586 		if ((sc->sc_flags & VNF_HAVELABEL) == 0)
587 			return (ENOTTY);
588 		vndgetdisklabel(dev, sc, (struct disklabel *)addr, 1);
589 		return (0);
590 
591 	case DIOCGDINFO:
592 		if ((sc->sc_flags & VNF_HAVELABEL) == 0)
593 			return (ENOTTY);
594 		*(struct disklabel *)addr = *(sc->sc_dk.dk_label);
595 		return (0);
596 
597 	case DIOCGPART:
598 		if ((sc->sc_flags & VNF_HAVELABEL) == 0)
599 			return (ENOTTY);
600 		((struct partinfo *)addr)->disklab = sc->sc_dk.dk_label;
601 		((struct partinfo *)addr)->part =
602 		    &sc->sc_dk.dk_label->d_partitions[DISKPART(dev)];
603 		return (0);
604 
605 	case DIOCWDINFO:
606 	case DIOCSDINFO:
607 		if ((sc->sc_flags & VNF_HAVELABEL) == 0)
608 			return (ENOTTY);
609 		if ((flag & FWRITE) == 0)
610 			return (EBADF);
611 
612 		if ((error = disk_lock(&sc->sc_dk)) != 0)
613 			return (error);
614 
615 		error = setdisklabel(sc->sc_dk.dk_label,
616 		    (struct disklabel *)addr, /* sc->sc_dk.dk_openmask */ 0);
617 		if (error == 0) {
618 			if (cmd == DIOCWDINFO)
619 				error = writedisklabel(DISKLABELDEV(dev),
620 				    vndstrategy, sc->sc_dk.dk_label);
621 		}
622 
623 		disk_unlock(&sc->sc_dk);
624 		return (error);
625 
626 	default:
627 		return (ENOTTY);
628 	}
629 
630 	return (0);
631 }
632 
633 /*
634  * Duplicate the current processes' credentials.  Since we are called only
635  * as the result of a SET ioctl and only root can do that, any future access
636  * to this "disk" is essentially as root.  Note that credentials may change
637  * if some other uid can write directly to the mapped file (NFS).
638  */
639 int
640 vndsetcred(struct vnd_softc *sc, struct ucred *cred)
641 {
642 	void *buf;
643 	size_t size;
644 	int error;
645 
646 	sc->sc_cred = crdup(cred);
647 	buf = malloc(DEV_BSIZE, M_TEMP, M_WAITOK);
648 	size = MIN(DEV_BSIZE, sc->sc_size * sc->sc_secsize);
649 
650 	/* XXX: Horrible kludge to establish credentials for NFS */
651 	error = vn_rdwr(UIO_READ, sc->sc_vp, buf, size, 0, UIO_SYSSPACE, 0,
652 	    sc->sc_cred, NULL, curproc);
653 
654 	free(buf, M_TEMP, 0);
655 	return (error);
656 }
657 
658 void
659 vndclear(struct vnd_softc *sc)
660 {
661 	struct vnode *vp = sc->sc_vp;
662 	struct proc *p = curproc;		/* XXX */
663 
664 	DNPRINTF(VDB_FOLLOW, "vndclear(%p): vp %p\n", sc, vp);
665 
666 	if (vp == NULL)
667 		panic("vndioctl: null vp");
668 	(void) vn_close(vp, VNDRW(sc), sc->sc_cred, p);
669 	crfree(sc->sc_cred);
670 	sc->sc_flags = 0;
671 	sc->sc_vp = NULL;
672 	sc->sc_cred = NULL;
673 	sc->sc_size = 0;
674 	bzero(sc->sc_file, sizeof(sc->sc_file));
675 }
676 
677 daddr_t
678 vndsize(dev_t dev)
679 {
680 	/* We don't support swapping to vnd anymore. */
681 	return (-1);
682 }
683 
684 int
685 vnddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
686 {
687 	/* Not implemented. */
688 	return (ENXIO);
689 }
690