getpages/putpages fixup part 1 - Add support for UIO_NOCOPY VOP_WRITEs to
[dragonfly.git] / sys / vfs / specfs / spec_vnops.c
1 /*
2  * Copyright (c) 1989, 1993, 1995
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      @(#)spec_vnops.c        8.14 (Berkeley) 5/21/95
34  * $FreeBSD: src/sys/miscfs/specfs/spec_vnops.c,v 1.131.2.4 2001/02/26 04:23:20 jlemon Exp $
35  * $DragonFly: src/sys/vfs/specfs/spec_vnops.c,v 1.56 2007/08/21 17:26:48 dillon Exp $
36  */
37
38 #include <sys/param.h>
39 #include <sys/proc.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/conf.h>
43 #include <sys/buf.h>
44 #include <sys/device.h>
45 #include <sys/mount.h>
46 #include <sys/vnode.h>
47 #include <sys/stat.h>
48 #include <sys/fcntl.h>
49 #include <sys/vmmeter.h>
50 #include <sys/bus.h>
51 #include <sys/tty.h>
52
53 #include <vm/vm.h>
54 #include <vm/vm_object.h>
55 #include <vm/vm_page.h>
56 #include <vm/vm_pager.h>
57
58 #include <machine/limits.h>
59
60 #include <sys/buf2.h>
61
62 #include <sys/thread2.h>
63
64 /*
65  * Specfs chained debugging (bitmask)
66  *
67  * 0 - disable debugging
68  * 1 - report chained I/Os
69  * 2 - force 4K chained I/Os
70  */
71 #define SPEC_CHAIN_DEBUG        0
72
73 static int      spec_advlock (struct vop_advlock_args *);  
74 static int      spec_bmap (struct vop_bmap_args *);
75 static int      spec_close (struct vop_close_args *);
76 static int      spec_freeblks (struct vop_freeblks_args *);
77 static int      spec_fsync (struct  vop_fsync_args *);
78 static int      spec_getpages (struct vop_getpages_args *);
79 static int      spec_inactive (struct  vop_inactive_args *);
80 static int      spec_ioctl (struct vop_ioctl_args *);
81 static int      spec_open (struct vop_open_args *);
82 static int      spec_poll (struct vop_poll_args *);
83 static int      spec_kqfilter (struct vop_kqfilter_args *);
84 static int      spec_print (struct vop_print_args *);
85 static int      spec_read (struct vop_read_args *);  
86 static int      spec_strategy (struct vop_strategy_args *);
87 static int      spec_write (struct vop_write_args *);
88 static void     spec_strategy_done(struct bio *nbio);
89
90 struct vop_ops spec_vnode_vops = {
91         .vop_default =          vop_defaultop,
92         .vop_access =           (void *)vop_ebadf,
93         .vop_advlock =          spec_advlock,
94         .vop_bmap =             spec_bmap,
95         .vop_close =            spec_close,
96         .vop_old_create =       (void *)vop_panic,
97         .vop_freeblks =         spec_freeblks,
98         .vop_fsync =            spec_fsync,
99         .vop_getpages =         spec_getpages,
100         .vop_inactive =         spec_inactive,
101         .vop_ioctl =            spec_ioctl,
102         .vop_old_link =         (void *)vop_panic,
103         .vop_old_mkdir =        (void *)vop_panic,
104         .vop_old_mknod =        (void *)vop_panic,
105         .vop_open =             spec_open,
106         .vop_pathconf =         vop_stdpathconf,
107         .vop_poll =             spec_poll,
108         .vop_kqfilter =         spec_kqfilter,
109         .vop_print =            spec_print,
110         .vop_read =             spec_read,
111         .vop_readdir =          (void *)vop_panic,
112         .vop_readlink =         (void *)vop_panic,
113         .vop_reallocblks =      (void *)vop_panic,
114         .vop_reclaim =          (void *)vop_null,
115         .vop_old_remove =       (void *)vop_panic,
116         .vop_old_rename =       (void *)vop_panic,
117         .vop_old_rmdir =        (void *)vop_panic,
118         .vop_setattr =          (void *)vop_ebadf,
119         .vop_strategy =         spec_strategy,
120         .vop_old_symlink =      (void *)vop_panic,
121         .vop_write =            spec_write
122 };
123
124 struct vop_ops *spec_vnode_vops_p = &spec_vnode_vops;
125
126 VNODEOP_SET(spec_vnode_vops);
127
128 extern int dev_ref_debug;
129
130 /*
131  * spec_vnoperate()
132  */
133 int
134 spec_vnoperate(struct vop_generic_args *ap)
135 {
136         return (VOCALL(&spec_vnode_vops, ap));
137 }
138
139 static void spec_getpages_iodone (struct bio *bio);
140
141 /*
142  * Open a special file.
143  *
144  * spec_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
145  *           struct file *a_fp)
146  */
147 /* ARGSUSED */
148 static int
149 spec_open(struct vop_open_args *ap)
150 {
151         struct vnode *vp = ap->a_vp;
152         cdev_t dev;
153         int error;
154         const char *cp;
155
156         /*
157          * Don't allow open if fs is mounted -nodev.
158          */
159         if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_NODEV))
160                 return (ENXIO);
161         if (vp->v_type == VBLK)
162                 return (ENXIO);
163
164         /*
165          * Resolve the device.  If the vnode is already open v_rdev may
166          * already be resolved.  However, if the device changes out from
167          * under us we report it (and, for now, we allow it).  Since
168          * v_release_rdev() zero's v_opencount, we have to save and restore
169          * it when replacing the rdev reference.
170          */
171         if (vp->v_rdev != NULL) {
172                 dev = get_dev(vp->v_umajor, vp->v_uminor);
173                 if (dev != vp->v_rdev) {
174                         int oc = vp->v_opencount;
175                         kprintf(
176                             "Warning: spec_open: dev %s was lost",
177                             vp->v_rdev->si_name);
178                         v_release_rdev(vp);
179                         error = v_associate_rdev(vp, 
180                                         get_dev(vp->v_umajor, vp->v_uminor));
181                         if (error) {
182                                 kprintf(", reacquisition failed\n");
183                         } else {
184                                 vp->v_opencount = oc;
185                                 kprintf(", reacquisition successful\n");
186                         }
187                 } else {
188                         error = 0;
189                 }
190         } else {
191                 error = v_associate_rdev(vp, get_dev(vp->v_umajor, vp->v_uminor));
192         }
193         if (error)
194                 return(error);
195
196         /*
197          * Prevent degenerate open/close sequences from nulling out rdev.
198          */
199         dev = vp->v_rdev;
200         KKASSERT(dev != NULL);
201
202         /*
203          * Make this field valid before any I/O in ->d_open.  XXX the
204          * device itself should probably be required to initialize
205          * this field in d_open.
206          */
207         if (!dev->si_iosize_max)
208                 dev->si_iosize_max = DFLTPHYS;
209
210         /*
211          * XXX: Disks get special billing here, but it is mostly wrong.
212          * XXX: diskpartitions can overlap and the real checks should
213          * XXX: take this into account, and consequently they need to
214          * XXX: live in the diskslicing code.  Some checks do.
215          */
216         if (vn_isdisk(vp, NULL) && ap->a_cred != FSCRED && 
217             (ap->a_mode & FWRITE)) {
218                 /*
219                  * Never allow opens for write if the device is mounted R/W
220                  */
221                 if (vp->v_rdev && vp->v_rdev->si_mountpoint &&
222                     !(vp->v_rdev->si_mountpoint->mnt_flag & MNT_RDONLY)) {
223                                 error = EBUSY;
224                                 goto done;
225                 }
226
227                 /*
228                  * When running in secure mode, do not allow opens
229                  * for writing if the device is mounted
230                  */
231                 if (securelevel >= 1 && vfs_mountedon(vp)) {
232                         error = EPERM;
233                         goto done;
234                 }
235
236                 /*
237                  * When running in very secure mode, do not allow
238                  * opens for writing of any devices.
239                  */
240                 if (securelevel >= 2) {
241                         error = EPERM;
242                         goto done;
243                 }
244         }
245
246         /* XXX: Special casing of ttys for deadfs.  Probably redundant */
247         if (dev_dflags(dev) & D_TTY)
248                 vp->v_flag |= VISTTY;
249
250         /*
251          * dev_dopen() is always called for each open.  dev_dclose() is
252          * only called for the last close unless D_TRACKCLOSE is set.
253          */
254         vn_unlock(vp);
255         error = dev_dopen(dev, ap->a_mode, S_IFCHR, ap->a_cred);
256         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
257
258         if (error)
259                 goto done;
260
261         if (dev_dflags(dev) & D_TTY) {
262                 if (dev->si_tty) {
263                         struct tty *tp;
264                         tp = dev->si_tty;
265                         if (!tp->t_stop) {
266                                 kprintf("Warning:%s: no t_stop, using nottystop\n", devtoname(dev));
267                                 tp->t_stop = nottystop;
268                         }
269                 }
270         }
271
272         /*
273          * If this is 'disk' or disk-like device, associate a VM object
274          * with it.
275          */
276         if (vn_isdisk(vp, NULL)) {
277                 if (!dev->si_bsize_phys)
278                         dev->si_bsize_phys = DEV_BSIZE;
279                 vinitvmio(vp, IDX_TO_OFF(INT_MAX));
280         }
281         if ((dev_dflags(dev) & D_DISK) == 0) {
282                 cp = devtoname(dev);
283                 if (*cp == '#') {
284                         kprintf("WARNING: driver %s should register devices with make_dev() (cdev_t = \"%s\")\n",
285                             dev_dname(dev), cp);
286                 }
287         }
288
289         /*
290          * If we were handed a file pointer we may be able to install a
291          * shortcut which issues device read and write operations directly
292          * from the fileops rather then having to go through spec_read()
293          * and spec_write().
294          */
295         if (ap->a_fp)
296                 vn_setspecops(ap->a_fp);
297
298         if (dev_ref_debug)
299                 kprintf("spec_open: %s %d\n", dev->si_name, vp->v_opencount);
300 done:
301         if (error) {
302                 if (vp->v_opencount == 0)
303                         v_release_rdev(vp);
304         } else {
305                 vop_stdopen(ap);
306         }
307         return (error);
308 }
309
310 /*
311  * Vnode op for read
312  *
313  * spec_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
314  *           struct ucred *a_cred)
315  */
316 /* ARGSUSED */
317 static int
318 spec_read(struct vop_read_args *ap)
319 {
320         struct vnode *vp;
321         struct thread *td;
322         struct uio *uio;
323         cdev_t dev;
324         int error;
325
326         vp = ap->a_vp;
327         dev = vp->v_rdev;
328         uio = ap->a_uio;
329         td = uio->uio_td;
330
331         if (dev == NULL)                /* device was revoked */
332                 return (EBADF);
333         if (uio->uio_resid == 0)
334                 return (0);
335
336         vn_unlock(vp);
337         error = dev_dread(dev, uio, ap->a_ioflag);
338         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
339         return (error);
340 }
341
342 /*
343  * Vnode op for write
344  *
345  * spec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
346  *            struct ucred *a_cred)
347  */
348 /* ARGSUSED */
349 static int
350 spec_write(struct vop_write_args *ap)
351 {
352         struct vnode *vp;
353         struct thread *td;
354         struct uio *uio;
355         cdev_t dev;
356         int error;
357
358         vp = ap->a_vp;
359         dev = vp->v_rdev;
360         uio = ap->a_uio;
361         td = uio->uio_td;
362
363         KKASSERT(uio->uio_segflg != UIO_NOCOPY);
364
365         if (dev == NULL)                /* device was revoked */
366                 return (EBADF);
367
368         vn_unlock(vp);
369         error = dev_dwrite(dev, uio, ap->a_ioflag);
370         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
371         return (error);
372 }
373
374 /*
375  * Device ioctl operation.
376  *
377  * spec_ioctl(struct vnode *a_vp, int a_command, caddr_t a_data,
378  *            int a_fflag, struct ucred *a_cred)
379  */
380 /* ARGSUSED */
381 static int
382 spec_ioctl(struct vop_ioctl_args *ap)
383 {
384         cdev_t dev;
385
386         if ((dev = ap->a_vp->v_rdev) == NULL)
387                 return (EBADF);         /* device was revoked */
388
389         return (dev_dioctl(dev, ap->a_command, ap->a_data,
390                     ap->a_fflag, ap->a_cred));
391 }
392
393 /*
394  * spec_poll(struct vnode *a_vp, int a_events, struct ucred *a_cred)
395  */
396 /* ARGSUSED */
397 static int
398 spec_poll(struct vop_poll_args *ap)
399 {
400         cdev_t dev;
401
402         if ((dev = ap->a_vp->v_rdev) == NULL)
403                 return (EBADF);         /* device was revoked */
404         return (dev_dpoll(dev, ap->a_events));
405 }
406
407 /*
408  * spec_kqfilter(struct vnode *a_vp, struct knote *a_kn)
409  */
410 /* ARGSUSED */
411 static int
412 spec_kqfilter(struct vop_kqfilter_args *ap)
413 {
414         cdev_t dev;
415
416         if ((dev = ap->a_vp->v_rdev) == NULL)
417                 return (EBADF);         /* device was revoked */
418         return (dev_dkqfilter(dev, ap->a_kn));
419 }
420
421 /*
422  * Synch buffers associated with a block device
423  *
424  * spec_fsync(struct vnode *a_vp, int a_waitfor)
425  */
426 /* ARGSUSED */
427 static int
428 spec_fsync(struct vop_fsync_args *ap)
429 {
430         struct vnode *vp = ap->a_vp;
431         int error;
432
433         if (!vn_isdisk(vp, NULL))
434                 return (0);
435
436         /*
437          * Flush all dirty buffers associated with a block device.
438          */
439         error = vfsync(vp, ap->a_waitfor, 10000, NULL, NULL);
440         return (error);
441 }
442
443 /*
444  * spec_inactive(struct vnode *a_vp)
445  */
446 static int
447 spec_inactive(struct vop_inactive_args *ap)
448 {
449         return (0);
450 }
451
452 /*
453  * Convert a vnode strategy call into a device strategy call.  Vnode strategy
454  * calls are not limited to device DMA limits so we have to deal with the
455  * case.
456  *
457  * spec_strategy(struct vnode *a_vp, struct bio *a_bio)
458  */
459 static int
460 spec_strategy(struct vop_strategy_args *ap)
461 {
462         struct bio *bio = ap->a_bio;
463         struct buf *bp = bio->bio_buf;
464         struct buf *nbp;
465         struct vnode *vp;
466         struct mount *mp;
467         int chunksize;
468         int maxiosize;
469
470         if (bp->b_cmd != BUF_CMD_READ &&
471             (LIST_FIRST(&bp->b_dep)) != NULL && bioops.io_start) {
472                 (*bioops.io_start)(bp);
473         }
474
475         /*
476          * Collect statistics on synchronous and asynchronous read
477          * and write counts for disks that have associated filesystems.
478          */
479         vp = ap->a_vp;
480         KKASSERT(vp->v_rdev != NULL);   /* XXX */
481         if (vn_isdisk(vp, NULL) && (mp = vp->v_rdev->si_mountpoint) != NULL) {
482                 if (bp->b_cmd == BUF_CMD_READ) {
483                         if (bp->b_flags & B_ASYNC)
484                                 mp->mnt_stat.f_asyncreads++;
485                         else
486                                 mp->mnt_stat.f_syncreads++;
487                 } else {
488                         if (bp->b_flags & B_ASYNC)
489                                 mp->mnt_stat.f_asyncwrites++;
490                         else
491                                 mp->mnt_stat.f_syncwrites++;
492                 }
493         }
494
495         /*
496          * Device iosize limitations only apply to read and write.  Shortcut
497          * the I/O if it fits.
498          */
499         if ((maxiosize = vp->v_rdev->si_iosize_max) == 0) {
500                 kprintf("%s: si_iosize_max not set!\n", dev_dname(vp->v_rdev));
501                 maxiosize = MAXPHYS;
502         }
503 #if SPEC_CHAIN_DEBUG & 2
504         maxiosize = 4096;
505 #endif
506         if (bp->b_bcount <= maxiosize ||
507             (bp->b_cmd != BUF_CMD_READ && bp->b_cmd != BUF_CMD_WRITE)) {
508                 dev_dstrategy_chain(vp->v_rdev, bio);
509                 return (0);
510         }
511
512         /*
513          * Clone the buffer and set up an I/O chain to chunk up the I/O.
514          */
515         nbp = kmalloc(sizeof(*bp), M_DEVBUF, M_INTWAIT|M_ZERO);
516         initbufbio(nbp);
517         LIST_INIT(&nbp->b_dep);
518         BUF_LOCKINIT(nbp);
519         BUF_LOCK(nbp, LK_EXCLUSIVE);
520         BUF_KERNPROC(nbp);
521         nbp->b_vp = vp;
522         nbp->b_flags = B_PAGING | (bp->b_flags & B_BNOCLIP);
523         nbp->b_data = bp->b_data;
524         nbp->b_bio1.bio_done = spec_strategy_done;
525         nbp->b_bio1.bio_offset = bio->bio_offset;
526         nbp->b_bio1.bio_caller_info1.ptr = bio;
527
528         /*
529          * Start the first transfer
530          */
531         if (vn_isdisk(vp, NULL))
532                 chunksize = vp->v_rdev->si_bsize_phys;
533         else
534                 chunksize = DEV_BSIZE;
535         chunksize = maxiosize / chunksize * chunksize;
536 #if SPEC_CHAIN_DEBUG & 1
537         kprintf("spec_strategy chained I/O chunksize=%d\n", chunksize);
538 #endif
539         nbp->b_cmd = bp->b_cmd;
540         nbp->b_bcount = chunksize;
541         nbp->b_bufsize = chunksize;     /* used to detect a short I/O */
542         nbp->b_bio1.bio_caller_info2.index = chunksize;
543
544 #if SPEC_CHAIN_DEBUG & 1
545         kprintf("spec_strategy: chain %p offset %d/%d bcount %d\n",
546                 bp, 0, bp->b_bcount, nbp->b_bcount);
547 #endif
548
549         dev_dstrategy(vp->v_rdev, &nbp->b_bio1);
550         return (0);
551 }
552
553 /*
554  * Chunked up transfer completion routine - chain transfers until done
555  */
556 static
557 void
558 spec_strategy_done(struct bio *nbio)
559 {
560         struct buf *nbp = nbio->bio_buf;
561         struct bio *bio = nbio->bio_caller_info1.ptr;   /* original bio */
562         struct buf *bp = bio->bio_buf;                  /* original bp */
563         int chunksize = nbio->bio_caller_info2.index;   /* chunking */
564         int boffset = nbp->b_data - bp->b_data;
565
566         if (nbp->b_flags & B_ERROR) {
567                 /*
568                  * An error terminates the chain, propogate the error back
569                  * to the original bp
570                  */
571                 bp->b_flags |= B_ERROR;
572                 bp->b_error = nbp->b_error;
573                 bp->b_resid = bp->b_bcount - boffset +
574                               (nbp->b_bcount - nbp->b_resid);
575 #if SPEC_CHAIN_DEBUG & 1
576                 kprintf("spec_strategy: chain %p error %d bcount %d/%d\n",
577                         bp, bp->b_error, bp->b_bcount,
578                         bp->b_bcount - bp->b_resid);
579 #endif
580                 kfree(nbp, M_DEVBUF);
581                 biodone(bio);
582         } else if (nbp->b_resid) {
583                 /*
584                  * A short read or write terminates the chain
585                  */
586                 bp->b_error = nbp->b_error;
587                 bp->b_resid = bp->b_bcount - boffset +
588                               (nbp->b_bcount - nbp->b_resid);
589 #if SPEC_CHAIN_DEBUG & 1
590                 kprintf("spec_strategy: chain %p short read(1) bcount %d/%d\n",
591                         bp, bp->b_bcount - bp->b_resid, bp->b_bcount);
592 #endif
593                 kfree(nbp, M_DEVBUF);
594                 biodone(bio);
595         } else if (nbp->b_bcount != nbp->b_bufsize) {
596                 /*
597                  * A short read or write can also occur by truncating b_bcount
598                  */
599 #if SPEC_CHAIN_DEBUG & 1
600                 kprintf("spec_strategy: chain %p short read(2) bcount %d/%d\n",
601                         bp, nbp->b_bcount + boffset, bp->b_bcount);
602 #endif
603                 bp->b_error = 0;
604                 bp->b_bcount = nbp->b_bcount + boffset; 
605                 bp->b_resid = nbp->b_resid;
606                 kfree(nbp, M_DEVBUF);
607                 biodone(bio);
608         } else if (nbp->b_bcount + boffset == bp->b_bcount) {
609                 /*
610                  * No more data terminates the chain
611                  */
612 #if SPEC_CHAIN_DEBUG & 1
613                 kprintf("spec_strategy: chain %p finished bcount %d\n",
614                         bp, bp->b_bcount);
615 #endif
616                 bp->b_error = 0;
617                 bp->b_resid = 0;
618                 kfree(nbp, M_DEVBUF);
619                 biodone(bio);
620         } else {
621                 /*
622                  * Continue the chain
623                  */
624                 boffset += nbp->b_bcount;
625                 nbp->b_data = bp->b_data + boffset;
626                 nbp->b_bcount = bp->b_bcount - boffset;
627                 if (nbp->b_bcount > chunksize)
628                         nbp->b_bcount = chunksize;
629                 nbp->b_bio1.bio_done = spec_strategy_done;
630                 nbp->b_bio1.bio_offset = bio->bio_offset + boffset;
631
632 #if SPEC_CHAIN_DEBUG & 1
633                 kprintf("spec_strategy: chain %p offset %d/%d bcount %d\n",
634                         bp, boffset, bp->b_bcount, nbp->b_bcount);
635 #endif
636
637                 dev_dstrategy(nbp->b_vp->v_rdev, &nbp->b_bio1);
638         }
639 }
640
641 /*
642  * spec_freeblks(struct vnode *a_vp, daddr_t a_addr, daddr_t a_length)
643  */
644 static int
645 spec_freeblks(struct vop_freeblks_args *ap)
646 {
647         struct buf *bp;
648
649         /*
650          * XXX: This assumes that strategy does the deed right away.
651          * XXX: this may not be TRTTD.
652          */
653         KKASSERT(ap->a_vp->v_rdev != NULL);
654         if ((dev_dflags(ap->a_vp->v_rdev) & D_CANFREE) == 0)
655                 return (0);
656         bp = geteblk(ap->a_length);
657         bp->b_cmd = BUF_CMD_FREEBLKS;
658         bp->b_bio1.bio_offset = ap->a_offset;
659         bp->b_bcount = ap->a_length;
660         dev_dstrategy(ap->a_vp->v_rdev, &bp->b_bio1);
661         return (0);
662 }
663
664 /*
665  * Implement degenerate case where the block requested is the block
666  * returned, and assume that the entire device is contiguous in regards
667  * to the contiguous block range (runp and runb).
668  *
669  * spec_bmap(struct vnode *a_vp, off_t a_loffset,
670  *           off_t *a_doffsetp, int *a_runp, int *a_runb)
671  */
672 static int
673 spec_bmap(struct vop_bmap_args *ap)
674 {
675         if (ap->a_doffsetp != NULL)
676                 *ap->a_doffsetp = ap->a_loffset;
677         if (ap->a_runp != NULL)
678                 *ap->a_runp = MAXBSIZE;
679         if (ap->a_runb != NULL) {
680                 if (ap->a_loffset < MAXBSIZE)
681                         *ap->a_runb = (int)ap->a_loffset;
682                 else
683                         *ap->a_runb = MAXBSIZE;
684         }
685         return (0);
686 }
687
688 /*
689  * Device close routine
690  *
691  * spec_close(struct vnode *a_vp, int a_fflag)
692  *
693  * NOTE: the vnode may or may not be locked on call.
694  */
695 /* ARGSUSED */
696 static int
697 spec_close(struct vop_close_args *ap)
698 {
699         struct proc *p = curproc;
700         struct vnode *vp = ap->a_vp;
701         cdev_t dev = vp->v_rdev;
702         int error;
703         int needrelock;
704
705         /*
706          * Hack: a tty device that is a controlling terminal
707          * has a reference from the session structure.
708          * We cannot easily tell that a character device is
709          * a controlling terminal, unless it is the closing
710          * process' controlling terminal.  In that case,
711          * if the reference count is 2 (this last descriptor
712          * plus the session), release the reference from the session.
713          *
714          * It is possible for v_opencount to be 0 or 1 in this case, 0
715          * because the tty might have been revoked.
716          */
717         if (dev)
718                 reference_dev(dev);
719         if (vcount(vp) == 2 && vp->v_opencount <= 1 && 
720             p && vp == p->p_session->s_ttyvp) {
721                 p->p_session->s_ttyvp = NULL;
722                 vrele(vp);
723         }
724
725         /*
726          * Vnodes can be opened and close multiple times.  Do not really
727          * close the device unless (1) it is being closed forcibly,
728          * (2) the device wants to track closes, or (3) this is the last
729          * vnode doing its last close on the device.
730          *
731          * XXX the VXLOCK (force close) case can leave vnodes referencing
732          * a closed device.
733          */
734         if (dev && ((vp->v_flag & VRECLAIMED) ||
735             (dev_dflags(dev) & D_TRACKCLOSE) ||
736             (vcount(vp) <= 1 && vp->v_opencount == 1))) {
737                 needrelock = 0;
738                 if (vn_islocked(vp)) {
739                         needrelock = 1;
740                         vn_unlock(vp);
741                 }
742                 error = dev_dclose(dev, ap->a_fflag, S_IFCHR);
743                 if (needrelock)
744                         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
745         } else {
746                 error = 0;
747         }
748
749         /*
750          * Track the actual opens and closes on the vnode.  The last close
751          * disassociates the rdev.  If the rdev is already disassociated 
752          * the vnode might have been revoked and no further opencount
753          * tracking occurs.
754          */
755         if (dev) {
756                 /*KKASSERT(vp->v_opencount > 0);*/
757                 if (dev_ref_debug) {
758                         kprintf("spec_close: %s %d\n",
759                                 dev->si_name, vp->v_opencount - 1);
760                 }
761                 if (vp->v_opencount == 1)
762                         v_release_rdev(vp);
763                 release_dev(dev);
764         }
765         vop_stdclose(ap);
766         return(error);
767 }
768
769 /*
770  * Print out the contents of a special device vnode.
771  *
772  * spec_print(struct vnode *a_vp)
773  */
774 static int
775 spec_print(struct vop_print_args *ap)
776 {
777         kprintf("tag VT_NON, dev %s\n", devtoname(ap->a_vp->v_rdev));
778         return (0);
779 }
780
781 /*
782  * Special device advisory byte-level locks.
783  *
784  * spec_advlock(struct vnode *a_vp, caddr_t a_id, int a_op,
785  *              struct flock *a_fl, int a_flags)
786  */
787 /* ARGSUSED */
788 static int
789 spec_advlock(struct vop_advlock_args *ap)
790 {
791         return ((ap->a_flags & F_POSIX) ? EINVAL : EOPNOTSUPP);
792 }
793
794 static void
795 spec_getpages_iodone(struct bio *bio)
796 {
797         bio->bio_buf->b_cmd = BUF_CMD_DONE;
798         wakeup(bio->bio_buf);
799 }
800
801 /*
802  * spec_getpages() - get pages associated with device vnode.
803  *
804  * Note that spec_read and spec_write do not use the buffer cache, so we
805  * must fully implement getpages here.
806  */
807 static int
808 spec_getpages(struct vop_getpages_args *ap)
809 {
810         vm_offset_t kva;
811         int error;
812         int i, pcount, size;
813         struct buf *bp;
814         vm_page_t m;
815         vm_ooffset_t offset;
816         int toff, nextoff, nread;
817         struct vnode *vp = ap->a_vp;
818         int blksiz;
819         int gotreqpage;
820
821         error = 0;
822         pcount = round_page(ap->a_count) / PAGE_SIZE;
823
824         /*
825          * Calculate the offset of the transfer and do sanity check.
826          */
827         offset = IDX_TO_OFF(ap->a_m[0]->pindex) + ap->a_offset;
828
829         /*
830          * Round up physical size for real devices.  We cannot round using
831          * v_mount's block size data because v_mount has nothing to do with
832          * the device.  i.e. it's usually '/dev'.  We need the physical block
833          * size for the device itself.
834          *
835          * We can't use v_rdev->si_mountpoint because it only exists when the
836          * block device is mounted.  However, we can use v_rdev.
837          */
838
839         if (vn_isdisk(vp, NULL))
840                 blksiz = vp->v_rdev->si_bsize_phys;
841         else
842                 blksiz = DEV_BSIZE;
843
844         size = (ap->a_count + blksiz - 1) & ~(blksiz - 1);
845
846         bp = getpbuf(NULL);
847         kva = (vm_offset_t)bp->b_data;
848
849         /*
850          * Map the pages to be read into the kva.
851          */
852         pmap_qenter(kva, ap->a_m, pcount);
853
854         /* Build a minimal buffer header. */
855         bp->b_cmd = BUF_CMD_READ;
856         bp->b_bcount = size;
857         bp->b_resid = 0;
858         bp->b_runningbufspace = size;
859         runningbufspace += bp->b_runningbufspace;
860
861         bp->b_bio1.bio_offset = offset;
862         bp->b_bio1.bio_done = spec_getpages_iodone;
863
864         mycpu->gd_cnt.v_vnodein++;
865         mycpu->gd_cnt.v_vnodepgsin += pcount;
866
867         /* Do the input. */
868         vn_strategy(ap->a_vp, &bp->b_bio1);
869
870         crit_enter();
871
872         /* We definitely need to be at splbio here. */
873         while (bp->b_cmd != BUF_CMD_DONE)
874                 tsleep(bp, 0, "spread", 0);
875
876         crit_exit();
877
878         if (bp->b_flags & B_ERROR) {
879                 if (bp->b_error)
880                         error = bp->b_error;
881                 else
882                         error = EIO;
883         }
884
885         /*
886          * If EOF is encountered we must zero-extend the result in order
887          * to ensure that the page does not contain garabge.  When no
888          * error occurs, an early EOF is indicated if b_bcount got truncated.
889          * b_resid is relative to b_bcount and should be 0, but some devices
890          * might indicate an EOF with b_resid instead of truncating b_bcount.
891          */
892         nread = bp->b_bcount - bp->b_resid;
893         if (nread < ap->a_count)
894                 bzero((caddr_t)kva + nread, ap->a_count - nread);
895         pmap_qremove(kva, pcount);
896
897         gotreqpage = 0;
898         for (i = 0, toff = 0; i < pcount; i++, toff = nextoff) {
899                 nextoff = toff + PAGE_SIZE;
900                 m = ap->a_m[i];
901
902                 m->flags &= ~PG_ZERO;
903
904                 if (nextoff <= nread) {
905                         m->valid = VM_PAGE_BITS_ALL;
906                         vm_page_undirty(m);
907                 } else if (toff < nread) {
908                         /*
909                          * Since this is a VM request, we have to supply the
910                          * unaligned offset to allow vm_page_set_validclean()
911                          * to zero sub-DEV_BSIZE'd portions of the page.
912                          */
913                         vm_page_set_validclean(m, 0, nread - toff);
914                 } else {
915                         m->valid = 0;
916                         vm_page_undirty(m);
917                 }
918
919                 if (i != ap->a_reqpage) {
920                         /*
921                          * Just in case someone was asking for this page we
922                          * now tell them that it is ok to use.
923                          */
924                         if (!error || (m->valid == VM_PAGE_BITS_ALL)) {
925                                 if (m->valid) {
926                                         if (m->flags & PG_WANTED) {
927                                                 vm_page_activate(m);
928                                         } else {
929                                                 vm_page_deactivate(m);
930                                         }
931                                         vm_page_wakeup(m);
932                                 } else {
933                                         vm_page_free(m);
934                                 }
935                         } else {
936                                 vm_page_free(m);
937                         }
938                 } else if (m->valid) {
939                         gotreqpage = 1;
940                         /*
941                          * Since this is a VM request, we need to make the
942                          * entire page presentable by zeroing invalid sections.
943                          */
944                         if (m->valid != VM_PAGE_BITS_ALL)
945                             vm_page_zero_invalid(m, FALSE);
946                 }
947         }
948         if (!gotreqpage) {
949                 m = ap->a_m[ap->a_reqpage];
950                 kprintf(
951             "spec_getpages:(%s) I/O read failure: (error=%d) bp %p vp %p\n",
952                         devtoname(vp->v_rdev), error, bp, bp->b_vp);
953                 kprintf(
954             "               size: %d, resid: %d, a_count: %d, valid: 0x%x\n",
955                     size, bp->b_resid, ap->a_count, m->valid);
956                 kprintf(
957             "               nread: %d, reqpage: %d, pindex: %lu, pcount: %d\n",
958                     nread, ap->a_reqpage, (u_long)m->pindex, pcount);
959                 /*
960                  * Free the buffer header back to the swap buffer pool.
961                  */
962                 relpbuf(bp, NULL);
963                 return VM_PAGER_ERROR;
964         }
965         /*
966          * Free the buffer header back to the swap buffer pool.
967          */
968         relpbuf(bp, NULL);
969         return VM_PAGER_OK;
970 }