proc->thread stage 4: rework the VFS and DEVICE subsystems to take thread
[dragonfly.git] / sys / dev / disk / vn / vn.c
1 /*
2  * Copyright (c) 1988 University of Utah.
3  * Copyright (c) 1990, 1993
4  *      The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * the Systems Programming Group of the University of Utah Computer
8  * Science Department.
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 University of
21  *      California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  * from: Utah Hdr: vn.c 1.13 94/04/02
39  *
40  *      from: @(#)vn.c  8.6 (Berkeley) 4/1/94
41  * $FreeBSD: src/sys/dev/vn/vn.c,v 1.105.2.4 2001/11/18 07:11:00 dillon Exp $
42  * $DragonFly: src/sys/dev/disk/vn/vn.c,v 1.4 2003/06/25 03:55:51 dillon Exp $
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/kernel.h>
67 #include <sys/proc.h>
68 #include <sys/namei.h>
69 #include <sys/buf.h>
70 #include <sys/malloc.h>
71 #include <sys/mount.h>
72 #include <sys/vnode.h>
73 #include <sys/fcntl.h>
74 #include <sys/conf.h>
75 #include <sys/disklabel.h>
76 #include <sys/diskslice.h>
77 #include <sys/stat.h>
78 #include <sys/conf.h>
79 #include <sys/module.h>
80 #include <sys/vnioctl.h>
81
82 #include <vm/vm.h>
83 #include <vm/vm_object.h>
84 #include <vm/vm_page.h>
85 #include <vm/vm_pager.h>
86 #include <vm/vm_pageout.h>
87 #include <vm/swap_pager.h>
88 #include <vm/vm_extern.h>
89 #include <vm/vm_zone.h>
90
91 static  d_ioctl_t       vnioctl;
92 static  d_open_t        vnopen;
93 static  d_close_t       vnclose;
94 static  d_psize_t       vnsize;
95 static  d_strategy_t    vnstrategy;
96
97 #define CDEV_MAJOR 43
98 #define BDEV_MAJOR 15
99
100 #define VN_BSIZE_BEST   8192
101
102 /*
103  * cdevsw
104  *      D_DISK          we want to look like a disk
105  *      D_CANFREE       We support B_FREEBUF
106  */
107
108 static struct cdevsw vn_cdevsw = {
109         /* open */      vnopen,
110         /* close */     vnclose,
111         /* read */      physread,
112         /* write */     physwrite,
113         /* ioctl */     vnioctl,
114         /* poll */      nopoll,
115         /* mmap */      nommap,
116         /* strategy */  vnstrategy,
117         /* name */      "vn",
118         /* maj */       CDEV_MAJOR,
119         /* dump */      nodump,
120         /* psize */     vnsize,
121         /* flags */     D_DISK|D_CANFREE,
122         /* bmaj */      BDEV_MAJOR
123 };
124
125 #define getvnbuf()      \
126         ((struct buf *)malloc(sizeof(struct buf), M_DEVBUF, M_WAITOK))
127
128 #define putvnbuf(bp)    \
129         free((caddr_t)(bp), M_DEVBUF)
130
131 struct vn_softc {
132         int             sc_unit;
133         int             sc_flags;       /* flags                        */
134         int             sc_size;        /* size of vn, sc_secsize scale */
135         int             sc_secsize;     /* sector size                  */
136         struct diskslices *sc_slices;
137         struct vnode    *sc_vp;         /* vnode if not NULL            */
138         vm_object_t     sc_object;      /* backing object if not NULL   */
139         struct ucred    *sc_cred;       /* credentials                  */
140         int              sc_maxactive;  /* max # of active requests     */
141         struct buf       sc_tab;        /* transfer queue               */
142         u_long           sc_options;    /* options                      */
143         dev_t            sc_devlist;    /* devices that refer to this unit */
144         SLIST_ENTRY(vn_softc) sc_list;
145 };
146
147 static SLIST_HEAD(, vn_softc) vn_list;
148
149 /* sc_flags */
150 #define VNF_INITED      0x01
151 #define VNF_READONLY    0x02
152
153 static u_long   vn_options;
154
155 #define IFOPT(vn,opt) if (((vn)->sc_options|vn_options) & (opt))
156 #define TESTOPT(vn,opt) (((vn)->sc_options|vn_options) & (opt))
157
158 static int      vnsetcred (struct vn_softc *vn, struct ucred *cred);
159 static void     vnclear (struct vn_softc *vn);
160 static int      vn_modevent (module_t, int, void *);
161 static int      vniocattach_file (struct vn_softc *, struct vn_ioctl *, dev_t dev, int flag, struct thread *p);
162 static int      vniocattach_swap (struct vn_softc *, struct vn_ioctl *, dev_t dev, int flag, struct thread *p);
163
164 static  int
165 vnclose(dev_t dev, int flags, int mode, struct thread *td)
166 {
167         struct vn_softc *vn = dev->si_drv1;
168
169         IFOPT(vn, VN_LABELS)
170                 if (vn->sc_slices != NULL)
171                         dsclose(dev, mode, vn->sc_slices);
172         return (0);
173 }
174
175 static struct vn_softc *
176 vnfindvn(dev_t dev)
177 {
178         int unit;
179         struct vn_softc *vn;
180
181         unit = dkunit(dev);
182         vn = dev->si_drv1;
183         if (!vn) {
184                 SLIST_FOREACH(vn, &vn_list, sc_list) {
185                         if (vn->sc_unit == unit) {
186                                 dev->si_drv2 = vn->sc_devlist;
187                                 vn->sc_devlist = dev;
188                                 dev->si_drv1 = vn;
189                                 break;
190                         }
191                 }
192         }
193         if (!vn) {
194                 vn = malloc(sizeof *vn, M_DEVBUF, M_WAITOK);
195                 if (!vn)
196                         return (NULL);
197                 bzero(vn, sizeof *vn);
198                 vn->sc_unit = unit;
199                 dev->si_drv1 = vn;
200                 vn->sc_devlist = make_dev(&vn_cdevsw, 0,
201                     UID_ROOT, GID_OPERATOR, 0640, "vn%d", unit);
202                 vn->sc_devlist->si_drv1 = vn;
203                 vn->sc_devlist->si_drv2 = NULL;
204                 if (vn->sc_devlist != dev) {
205                         dev->si_drv2 = vn->sc_devlist;
206                         vn->sc_devlist = dev;
207                 }
208                 SLIST_INSERT_HEAD(&vn_list, vn, sc_list);
209         }
210         return (vn);
211 }
212
213 static  int
214 vnopen(dev_t dev, int flags, int mode, struct thread *td)
215 {
216         struct vn_softc *vn;
217
218         /*
219          * Locate preexisting device
220          */
221
222         if ((vn = dev->si_drv1) == NULL)
223                 vn = vnfindvn(dev);
224
225         /*
226          * Update si_bsize fields for device.  This data will be overriden by
227          * the slice/parition code for vn accesses through partitions, and
228          * used directly if you open the 'whole disk' device.
229          *
230          * si_bsize_best must be reinitialized in case VN has been 
231          * reconfigured, plus make it at least VN_BSIZE_BEST for efficiency.
232          */
233         dev->si_bsize_phys = vn->sc_secsize;
234         dev->si_bsize_best = vn->sc_secsize;
235         if (dev->si_bsize_best < VN_BSIZE_BEST)
236                 dev->si_bsize_best = VN_BSIZE_BEST;
237
238         if ((flags & FWRITE) && (vn->sc_flags & VNF_READONLY))
239                 return (EACCES);
240
241         IFOPT(vn, VN_FOLLOW)
242                 printf("vnopen(%s, 0x%x, 0x%x, %p)\n",
243                     devtoname(dev), flags, mode, (void *)td);
244
245         /*
246          * Initialize label
247          */
248
249         IFOPT(vn, VN_LABELS) {
250                 if (vn->sc_flags & VNF_INITED) {
251                         struct disklabel label;
252
253                         /* Build label for whole disk. */
254                         bzero(&label, sizeof label);
255                         label.d_secsize = vn->sc_secsize;
256                         label.d_nsectors = 32;
257                         label.d_ntracks = 64 / (vn->sc_secsize / DEV_BSIZE);
258                         label.d_secpercyl = label.d_nsectors * label.d_ntracks;
259                         label.d_ncylinders = vn->sc_size / label.d_secpercyl;
260                         label.d_secperunit = vn->sc_size;
261                         label.d_partitions[RAW_PART].p_size = vn->sc_size;
262
263                         return (dsopen(dev, mode, 0, &vn->sc_slices, &label));
264                 }
265                 if (dkslice(dev) != WHOLE_DISK_SLICE ||
266                     dkpart(dev) != RAW_PART ||
267                     mode != S_IFCHR) {
268                         return (ENXIO);
269                 }
270         }
271         return(0);
272 }
273
274 /*
275  *      vnstrategy:
276  *
277  *      Run strategy routine for VN device.  We use VOP_READ/VOP_WRITE calls
278  *      for vnode-backed vn's, and the new vm_pager_strategy() call for
279  *      vm_object-backed vn's.
280  *
281  *      Currently B_ASYNC is only partially handled - for OBJT_SWAP I/O only.
282  *
283  *      NOTE: bp->b_blkno is DEV_BSIZE'd.  We must generate bp->b_pblkno for
284  *      our uio or vn_pager_strategy() call that is vn->sc_secsize'd
285  */
286
287 static  void
288 vnstrategy(struct buf *bp)
289 {
290         int unit;
291         struct vn_softc *vn;
292         int error;
293
294         unit = dkunit(bp->b_dev);
295         vn = bp->b_dev->si_drv1;
296         if (!vn)
297                 vn = vnfindvn(bp->b_dev);
298
299         IFOPT(vn, VN_DEBUG)
300                 printf("vnstrategy(%p): unit %d\n", bp, unit);
301
302         if ((vn->sc_flags & VNF_INITED) == 0) {
303                 bp->b_error = ENXIO;
304                 bp->b_flags |= B_ERROR;
305                 biodone(bp);
306                 return;
307         }
308
309         bp->b_resid = bp->b_bcount;
310
311         IFOPT(vn, VN_LABELS) {
312                 if (vn->sc_slices != NULL && dscheck(bp, vn->sc_slices) <= 0) {
313                         bp->b_flags |= B_INVAL;
314                         biodone(bp);
315                         return;
316                 }
317         } else {
318                 int pbn;        /* in sc_secsize chunks */
319                 long sz;        /* in sc_secsize chunks */
320
321                 /*
322                  * Check for required alignment.  Transfers must be a valid
323                  * multiple of the sector size.
324                  */
325                 if (bp->b_bcount % vn->sc_secsize != 0 ||
326                     bp->b_blkno % (vn->sc_secsize / DEV_BSIZE) != 0) {
327                         bp->b_error = EINVAL;
328                         bp->b_flags |= B_ERROR | B_INVAL;
329                         biodone(bp);
330                         return;
331                 }
332
333                 pbn = bp->b_blkno / (vn->sc_secsize / DEV_BSIZE);
334                 sz = howmany(bp->b_bcount, vn->sc_secsize);
335
336                 /*
337                  * If out of bounds return an error.  If at the EOF point,
338                  * simply read or write less.
339                  */
340                 if (pbn < 0 || pbn >= vn->sc_size) {
341                         if (pbn != vn->sc_size) {
342                                 bp->b_error = EINVAL;
343                                 bp->b_flags |= B_ERROR | B_INVAL;
344                         }
345                         biodone(bp);
346                         return;
347                 }
348
349                 /*
350                  * If the request crosses EOF, truncate the request.
351                  */
352                 if (pbn + sz > vn->sc_size) {
353                         bp->b_bcount = (vn->sc_size - pbn) * vn->sc_secsize;
354                         bp->b_resid = bp->b_bcount;
355                 }
356                 bp->b_pblkno = pbn;
357         }
358
359         if (vn->sc_vp && (bp->b_flags & B_FREEBUF)) {
360                 /*
361                  * Not handled for vnode-backed element yet.
362                  */
363                 biodone(bp);
364         } else if (vn->sc_vp) {
365                 /*
366                  * VNODE I/O
367                  *
368                  * If an error occurs, we set B_ERROR but we do not set 
369                  * B_INVAL because (for a write anyway), the buffer is 
370                  * still valid.
371                  */
372                 struct uio auio;
373                 struct iovec aiov;
374
375                 bzero(&auio, sizeof(auio));
376
377                 aiov.iov_base = bp->b_data;
378                 aiov.iov_len = bp->b_bcount;
379                 auio.uio_iov = &aiov;
380                 auio.uio_iovcnt = 1;
381                 auio.uio_offset = (vm_ooffset_t)bp->b_pblkno * vn->sc_secsize;
382                 auio.uio_segflg = UIO_SYSSPACE;
383                 if( bp->b_flags & B_READ)
384                         auio.uio_rw = UIO_READ;
385                 else
386                         auio.uio_rw = UIO_WRITE;
387                 auio.uio_resid = bp->b_bcount;
388                 auio.uio_td = curthread;
389                 vn_lock(vn->sc_vp, LK_EXCLUSIVE | LK_RETRY, curthread);
390                 if (bp->b_flags & B_READ)
391                         error = VOP_READ(vn->sc_vp, &auio, IO_DIRECT, vn->sc_cred);
392                 else
393                         error = VOP_WRITE(vn->sc_vp, &auio, IO_NOWDRAIN, vn->sc_cred);
394                 VOP_UNLOCK(vn->sc_vp, 0, curthread);
395                 bp->b_resid = auio.uio_resid;
396
397                 if (error) {
398                         bp->b_error = error;
399                         bp->b_flags |= B_ERROR;
400                 }
401                 biodone(bp);
402         } else if (vn->sc_object) {
403                 /*
404                  * OBJT_SWAP I/O
405                  *
406                  * ( handles read, write, freebuf )
407                  *
408                  * Note: if we pre-reserved swap, B_FREEBUF is disabled
409                  */
410                 KASSERT((bp->b_bufsize & (vn->sc_secsize - 1)) == 0,
411                     ("vnstrategy: buffer %p too small for physio", bp));
412
413                 if ((bp->b_flags & B_FREEBUF) && TESTOPT(vn, VN_RESERVE)) {
414                         biodone(bp);
415                 } else {
416                         vm_pager_strategy(vn->sc_object, bp);
417                 }
418         } else {
419                 bp->b_flags |= B_ERROR;
420                 bp->b_error = EINVAL;
421                 biodone(bp);
422         }
423 }
424
425 /* ARGSUSED */
426 static  int
427 vnioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
428 {
429         struct vn_softc *vn;
430         struct vn_ioctl *vio;
431         int error;
432         u_long *f;
433
434         vn = dev->si_drv1;
435         IFOPT(vn,VN_FOLLOW)
436                 printf("vnioctl(%s, 0x%lx, %p, 0x%x, %p): unit %d\n",
437                     devtoname(dev), cmd, (void *)data, flag, (void *)td,
438                     dkunit(dev));
439
440         switch (cmd) {
441         case VNIOCATTACH:
442         case VNIOCDETACH:
443         case VNIOCGSET:
444         case VNIOCGCLEAR:
445         case VNIOCUSET:
446         case VNIOCUCLEAR:
447                 goto vn_specific;
448         }
449
450         IFOPT(vn,VN_LABELS) {
451                 if (vn->sc_slices != NULL) {
452                         error = dsioctl(dev, cmd, data, flag, &vn->sc_slices);
453                         if (error != ENOIOCTL)
454                                 return (error);
455                 }
456                 if (dkslice(dev) != WHOLE_DISK_SLICE ||
457                     dkpart(dev) != RAW_PART)
458                         return (ENOTTY);
459         }
460
461     vn_specific:
462
463         error = suser(td);
464         if (error)
465                 return (error);
466
467         vio = (struct vn_ioctl *)data;
468         f = (u_long*)data;
469         switch (cmd) {
470
471         case VNIOCATTACH:
472                 if (vn->sc_flags & VNF_INITED)
473                         return(EBUSY);
474
475                 if (vio->vn_file == NULL)
476                         error = vniocattach_swap(vn, vio, dev, flag, td);
477                 else
478                         error = vniocattach_file(vn, vio, dev, flag, td);
479                 break;
480
481         case VNIOCDETACH:
482                 if ((vn->sc_flags & VNF_INITED) == 0)
483                         return(ENXIO);
484                 /*
485                  * XXX handle i/o in progress.  Return EBUSY, or wait, or
486                  * flush the i/o.
487                  * XXX handle multiple opens of the device.  Return EBUSY,
488                  * or revoke the fd's.
489                  * How are these problems handled for removable and failing
490                  * hardware devices? (Hint: They are not)
491                  */
492                 vnclear(vn);
493                 IFOPT(vn, VN_FOLLOW)
494                         printf("vnioctl: CLRed\n");
495                 break;
496
497         case VNIOCGSET:
498                 vn_options |= *f;
499                 *f = vn_options;
500                 break;
501
502         case VNIOCGCLEAR:
503                 vn_options &= ~(*f);
504                 *f = vn_options;
505                 break;
506
507         case VNIOCUSET:
508                 vn->sc_options |= *f;
509                 *f = vn->sc_options;
510                 break;
511
512         case VNIOCUCLEAR:
513                 vn->sc_options &= ~(*f);
514                 *f = vn->sc_options;
515                 break;
516
517         default:
518                 error = ENOTTY;
519                 break;
520         }
521         return(error);
522 }
523
524 /*
525  *      vniocattach_file:
526  *
527  *      Attach a file to a VN partition.  Return the size in the vn_size
528  *      field.
529  */
530
531 static int
532 vniocattach_file(vn, vio, dev, flag, td)
533         struct vn_softc *vn;
534         struct vn_ioctl *vio;
535         dev_t dev;
536         int flag;
537         struct thread *td;
538 {
539         struct vattr vattr;
540         struct nameidata nd;
541         int error, flags;
542         struct proc *p = td->td_proc;
543
544         KKASSERT(p != NULL);
545
546         flags = FREAD|FWRITE;
547         NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, vio->vn_file, td);
548         error = vn_open(&nd, flags, 0);
549         if (error) {
550                 if (error != EACCES && error != EPERM && error != EROFS)
551                         return (error);
552                 flags &= ~FWRITE;
553                 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, vio->vn_file, td);
554                 error = vn_open(&nd, flags, 0);
555                 if (error)
556                         return (error);
557         }
558         NDFREE(&nd, NDF_ONLY_PNBUF);
559         if (nd.ni_vp->v_type != VREG ||
560             (error = VOP_GETATTR(nd.ni_vp, &vattr, p->p_ucred, td))) {
561                 VOP_UNLOCK(nd.ni_vp, 0, td);
562                 (void) vn_close(nd.ni_vp, flags, p->p_ucred, td);
563                 return (error ? error : EINVAL);
564         }
565         VOP_UNLOCK(nd.ni_vp, 0, td);
566         vn->sc_secsize = DEV_BSIZE;
567         vn->sc_vp = nd.ni_vp;
568
569         /*
570          * If the size is specified, override the file attributes.  Note that
571          * the vn_size argument is in PAGE_SIZE sized blocks.
572          */
573         if (vio->vn_size)
574                 vn->sc_size = (quad_t)vio->vn_size * PAGE_SIZE / vn->sc_secsize;
575         else
576                 vn->sc_size = vattr.va_size / vn->sc_secsize;
577         error = vnsetcred(vn, p->p_ucred);
578         if (error) {
579                 (void) vn_close(nd.ni_vp, flags, p->p_ucred, td);
580                 return(error);
581         }
582         vn->sc_flags |= VNF_INITED;
583         if (flags == FREAD)
584                 vn->sc_flags |= VNF_READONLY;
585         IFOPT(vn, VN_LABELS) {
586                 /*
587                  * Reopen so that `ds' knows which devices are open.
588                  * If this is the first VNIOCSET, then we've
589                  * guaranteed that the device is the cdev and that
590                  * no other slices or labels are open.  Otherwise,
591                  * we rely on VNIOCCLR not being abused.
592                  */
593                 error = vnopen(dev, flag, S_IFCHR, td);
594                 if (error)
595                         vnclear(vn);
596         }
597         IFOPT(vn, VN_FOLLOW)
598                 printf("vnioctl: SET vp %p size %x blks\n",
599                        vn->sc_vp, vn->sc_size);
600         return(0);
601 }
602
603 /*
604  *      vniocattach_swap:
605  *
606  *      Attach swap backing store to a VN partition of the size specified
607  *      in vn_size.
608  */
609
610 static int
611 vniocattach_swap(vn, vio, dev, flag, td)
612         struct vn_softc *vn;
613         struct vn_ioctl *vio;
614         dev_t dev;
615         int flag;
616         struct thread *td;
617 {
618         int error;
619         struct proc *p = td->td_proc;
620
621         KKASSERT(p != NULL);
622         /*
623          * Range check.  Disallow negative sizes or any size less then the
624          * size of a page.  Then round to a page.
625          */
626
627         if (vio->vn_size <= 0)
628                 return(EDOM);
629
630         /*
631          * Allocate an OBJT_SWAP object.
632          *
633          * sc_secsize is PAGE_SIZE'd
634          *
635          * vio->vn_size is in PAGE_SIZE'd chunks.
636          * sc_size must be in PAGE_SIZE'd chunks.  
637          * Note the truncation.
638          */
639
640         vn->sc_secsize = PAGE_SIZE;
641         vn->sc_size = vio->vn_size;
642         vn->sc_object = 
643          vm_pager_allocate(OBJT_SWAP, NULL, vn->sc_secsize * (vm_ooffset_t)vio->vn_size, VM_PROT_DEFAULT, 0);
644         IFOPT(vn, VN_RESERVE) {
645                 if (swap_pager_reserve(vn->sc_object, 0, vn->sc_size) < 0) {
646                         vm_pager_deallocate(vn->sc_object);
647                         vn->sc_object = NULL;
648                         return(EDOM);
649                 }
650         }
651         vn->sc_flags |= VNF_INITED;
652
653         error = vnsetcred(vn, p->p_ucred);
654         if (error == 0) {
655                 IFOPT(vn, VN_LABELS) {
656                         /*
657                          * Reopen so that `ds' knows which devices are open.
658                          * If this is the first VNIOCSET, then we've
659                          * guaranteed that the device is the cdev and that
660                          * no other slices or labels are open.  Otherwise,
661                          * we rely on VNIOCCLR not being abused.
662                          */
663                         error = vnopen(dev, flag, S_IFCHR, td);
664                 }
665         }
666         if (error == 0) {
667                 IFOPT(vn, VN_FOLLOW) {
668                         printf("vnioctl: SET vp %p size %x\n",
669                                vn->sc_vp, vn->sc_size);
670                 }
671         }
672         if (error)
673                 vnclear(vn);
674         return(error);
675 }
676
677 /*
678  * Duplicate the current processes' credentials.  Since we are called only
679  * as the result of a SET ioctl and only root can do that, any future access
680  * to this "disk" is essentially as root.  Note that credentials may change
681  * if some other uid can write directly to the mapped file (NFS).
682  */
683 int
684 vnsetcred(struct vn_softc *vn, struct ucred *cred)
685 {
686         char *tmpbuf;
687         int error = 0;
688
689         /*
690          * Set credits in our softc
691          */
692
693         if (vn->sc_cred)
694                 crfree(vn->sc_cred);
695         vn->sc_cred = crdup(cred);
696
697         /*
698          * Horrible kludge to establish credentials for NFS  XXX.
699          */
700
701         if (vn->sc_vp) {
702                 struct uio auio;
703                 struct iovec aiov;
704
705                 tmpbuf = malloc(vn->sc_secsize, M_TEMP, M_WAITOK);
706                 bzero(&auio, sizeof(auio));
707
708                 aiov.iov_base = tmpbuf;
709                 aiov.iov_len = vn->sc_secsize;
710                 auio.uio_iov = &aiov;
711                 auio.uio_iovcnt = 1;
712                 auio.uio_offset = 0;
713                 auio.uio_rw = UIO_READ;
714                 auio.uio_segflg = UIO_SYSSPACE;
715                 auio.uio_resid = aiov.iov_len;
716                 vn_lock(vn->sc_vp, LK_EXCLUSIVE | LK_RETRY, curthread);
717                 error = VOP_READ(vn->sc_vp, &auio, 0, vn->sc_cred);
718                 VOP_UNLOCK(vn->sc_vp, 0, curthread);
719                 free(tmpbuf, M_TEMP);
720         }
721         return (error);
722 }
723
724 void
725 vnclear(struct vn_softc *vn)
726 {
727         struct thread *td = curthread;          /* XXX */
728
729         IFOPT(vn, VN_FOLLOW)
730                 printf("vnclear(%p): vp=%p\n", vn, vn->sc_vp);
731         if (vn->sc_slices != NULL)
732                 dsgone(&vn->sc_slices);
733         vn->sc_flags &= ~VNF_INITED;
734         if (vn->sc_vp != NULL) {
735                 (void)vn_close(vn->sc_vp, vn->sc_flags & VNF_READONLY ?
736                     FREAD : (FREAD|FWRITE), vn->sc_cred, td);
737                 vn->sc_vp = NULL;
738         }
739         vn->sc_flags &= ~VNF_READONLY;
740         if (vn->sc_cred) {
741                 crfree(vn->sc_cred);
742                 vn->sc_cred = NULL;
743         }
744         if (vn->sc_object != NULL) {
745                 vm_pager_deallocate(vn->sc_object);
746                 vn->sc_object = NULL;
747         }
748         vn->sc_size = 0;
749 }
750
751 static  int
752 vnsize(dev_t dev)
753 {
754         struct vn_softc *vn;
755
756         vn = dev->si_drv1;
757         if (!vn)
758                 return(-1);
759         if ((vn->sc_flags & VNF_INITED) == 0)
760                 return(-1);
761
762         return(vn->sc_size);
763 }
764
765 static int 
766 vn_modevent(module_t mod, int type, void *data)
767 {
768         struct vn_softc *vn;
769         dev_t dev;
770
771         switch (type) {
772         case MOD_LOAD:
773                 cdevsw_add(&vn_cdevsw);
774                 break;
775
776         case MOD_UNLOAD:
777                 /* fall through */
778         case MOD_SHUTDOWN:
779                 for (;;) {
780                         vn = SLIST_FIRST(&vn_list);
781                         if (!vn)
782                                 break;
783                         SLIST_REMOVE_HEAD(&vn_list, sc_list);
784                         if (vn->sc_flags & VNF_INITED)
785                                 vnclear(vn);
786                         /* Cleanup all dev_t's that refer to this unit */
787                         while ((dev = vn->sc_devlist) != NULL) {
788                                 vn->sc_devlist = dev->si_drv2;
789                                 dev->si_drv1 = dev->si_drv2 = NULL;
790                                 /* If the last one, destroy it. */
791                                 if (vn->sc_devlist == NULL)
792                                         destroy_dev(dev);
793                         }
794                         free(vn, M_DEVBUF);
795                 }
796                 cdevsw_remove(&vn_cdevsw);
797                 break;
798         default:
799                 break;
800         }
801         return 0;
802 }
803
804 DEV_MODULE(vn, vn_modevent, 0);