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