Increment v_opencount before potentially blocking in dopen to avoid
[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.16 2004/05/26 00:11:56 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/tty.h>
51
52 #include <vm/vm.h>
53 #include <vm/vm_object.h>
54 #include <vm/vm_page.h>
55 #include <vm/vm_pager.h>
56
57 #include <sys/buf2.h>
58
59 static int      spec_advlock (struct vop_advlock_args *);  
60 static int      spec_bmap (struct vop_bmap_args *);
61 static int      spec_close (struct vop_close_args *);
62 static int      spec_freeblks (struct vop_freeblks_args *);
63 static int      spec_fsync (struct  vop_fsync_args *);
64 static int      spec_getpages (struct vop_getpages_args *);
65 static int      spec_inactive (struct  vop_inactive_args *);
66 static int      spec_ioctl (struct vop_ioctl_args *);
67 static int      spec_open (struct vop_open_args *);
68 static int      spec_poll (struct vop_poll_args *);
69 static int      spec_kqfilter (struct vop_kqfilter_args *);
70 static int      spec_print (struct vop_print_args *);
71 static int      spec_read (struct vop_read_args *);  
72 static int      spec_strategy (struct vop_strategy_args *);
73 static int      spec_write (struct vop_write_args *);
74
75 vop_t **spec_vnodeop_p;
76 static struct vnodeopv_entry_desc spec_vnodeop_entries[] = {
77         { &vop_default_desc,            (vop_t *) vop_defaultop },
78         { &vop_access_desc,             (vop_t *) vop_ebadf },
79         { &vop_advlock_desc,            (vop_t *) spec_advlock },
80         { &vop_bmap_desc,               (vop_t *) spec_bmap },
81         { &vop_close_desc,              (vop_t *) spec_close },
82         { &vop_create_desc,             (vop_t *) vop_panic },
83         { &vop_freeblks_desc,           (vop_t *) spec_freeblks },
84         { &vop_fsync_desc,              (vop_t *) spec_fsync },
85         { &vop_getpages_desc,           (vop_t *) spec_getpages },
86         { &vop_inactive_desc,           (vop_t *) spec_inactive },
87         { &vop_ioctl_desc,              (vop_t *) spec_ioctl },
88         { &vop_lease_desc,              (vop_t *) vop_null },
89         { &vop_link_desc,               (vop_t *) vop_panic },
90         { &vop_mkdir_desc,              (vop_t *) vop_panic },
91         { &vop_mknod_desc,              (vop_t *) vop_panic },
92         { &vop_open_desc,               (vop_t *) spec_open },
93         { &vop_pathconf_desc,           (vop_t *) vop_stdpathconf },
94         { &vop_poll_desc,               (vop_t *) spec_poll },
95         { &vop_kqfilter_desc,           (vop_t *) spec_kqfilter },
96         { &vop_print_desc,              (vop_t *) spec_print },
97         { &vop_read_desc,               (vop_t *) spec_read },
98         { &vop_readdir_desc,            (vop_t *) vop_panic },
99         { &vop_readlink_desc,           (vop_t *) vop_panic },
100         { &vop_reallocblks_desc,        (vop_t *) vop_panic },
101         { &vop_reclaim_desc,            (vop_t *) vop_null },
102         { &vop_remove_desc,             (vop_t *) vop_panic },
103         { &vop_rename_desc,             (vop_t *) vop_panic },
104         { &vop_rmdir_desc,              (vop_t *) vop_panic },
105         { &vop_setattr_desc,            (vop_t *) vop_ebadf },
106         { &vop_strategy_desc,           (vop_t *) spec_strategy },
107         { &vop_symlink_desc,            (vop_t *) vop_panic },
108         { &vop_write_desc,              (vop_t *) spec_write },
109         { NULL, NULL }
110 };
111 static struct vnodeopv_desc spec_vnodeop_opv_desc =
112         { &spec_vnodeop_p, spec_vnodeop_entries };
113
114 VNODEOP_SET(spec_vnodeop_opv_desc);
115
116 extern int dev_ref_debug;
117
118 /*
119  * spec_vnoperate(struct vnodeop_desc *a_desc, ...)
120  */
121 int
122 spec_vnoperate(struct vop_generic_args *ap)
123 {
124         return (VOCALL(spec_vnodeop_p, ap->a_desc->vdesc_offset, ap));
125 }
126
127 static void spec_getpages_iodone (struct buf *bp);
128
129 /*
130  * Open a special file.
131  *
132  * spec_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
133  *           struct thread *a_td)
134  */
135 /* ARGSUSED */
136 static int
137 spec_open(struct vop_open_args *ap)
138 {
139         struct vnode *vp = ap->a_vp;
140         dev_t dev;
141         int error;
142         int isblk = (vp->v_type == VBLK) ? 1 : 0;
143         const char *cp;
144
145         /*
146          * Don't allow open if fs is mounted -nodev.
147          */
148         if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_NODEV))
149                 return (ENXIO);
150
151         /*
152          * Resolve the device.  If the vnode is already open v_rdev may
153          * already be resolved.  However, if the device changes out from
154          * under us we report it (and, for now, we allow it).
155          */
156         if (vp->v_rdev != NULL) {
157                 dev = udev2dev(vp->v_udev, isblk);
158                 if (dev != vp->v_rdev) {
159                         printf(
160                             "Warning: spec_open: dev %s was lost",
161                             vp->v_rdev->si_name);
162                         v_release_rdev(vp);
163                         error = v_associate_rdev(vp, 
164                                         udev2dev(vp->v_udev, isblk));
165                         if (error)
166                                 printf(", reacquisition failed\n");
167                         else
168                                 printf(", reacquisition successful\n");
169                 } else {
170                         error = 0;
171                 }
172         } else {
173                 error = v_associate_rdev(vp, udev2dev(vp->v_udev, isblk));
174         }
175         if (error)
176                 return(error);
177
178         /*
179          * Prevent degenerate open/close sequences from nulling out rdev.
180          */
181         ++vp->v_opencount;
182         dev = vp->v_rdev;
183         KKASSERT(dev != NULL);
184
185         /*
186          * Make this field valid before any I/O in ->d_open.  XXX the
187          * device itself should probably be required to initialize
188          * this field in d_open.
189          */
190         if (!dev->si_iosize_max)
191                 dev->si_iosize_max = DFLTPHYS;
192
193         /*
194          * XXX: Disks get special billing here, but it is mostly wrong.
195          * XXX: diskpartitions can overlap and the real checks should
196          * XXX: take this into account, and consequently they need to
197          * XXX: live in the diskslicing code.  Some checks do.
198          */
199         if (vn_isdisk(vp, NULL) && ap->a_cred != FSCRED && 
200             (ap->a_mode & FWRITE)) {
201                 /*
202                  * Never allow opens for write if the device is mounted R/W
203                  */
204                 if (vp->v_rdev && vp->v_rdev->si_mountpoint &&
205                     !(vp->v_rdev->si_mountpoint->mnt_flag & MNT_RDONLY)) {
206                                 error = EBUSY;
207                                 goto done;
208                 }
209
210                 /*
211                  * When running in secure mode, do not allow opens
212                  * for writing if the device is mounted
213                  */
214                 if (securelevel >= 1 && vfs_mountedon(vp)) {
215                         error = EPERM;
216                         goto done;
217                 }
218
219                 /*
220                  * When running in very secure mode, do not allow
221                  * opens for writing of any devices.
222                  */
223                 if (securelevel >= 2) {
224                         error = EPERM;
225                         goto done;
226                 }
227         }
228
229         /* XXX: Special casing of ttys for deadfs.  Probably redundant */
230         if (dev_dflags(dev) & D_TTY)
231                 vp->v_flag |= VISTTY;
232
233         /*
234          * dev_dopen() is always called for each open.  dev_dclose() is
235          * only called for the last close unless D_TRACKCLOSE is set.
236          */
237         VOP_UNLOCK(vp, NULL, 0, ap->a_td);
238         error = dev_dopen(dev, ap->a_mode, S_IFCHR, ap->a_td);
239         vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, ap->a_td);
240
241         if (error)
242                 goto done;
243
244         if (dev_dflags(dev) & D_TTY) {
245                 if (dev->si_tty) {
246                         struct tty *tp;
247                         tp = dev->si_tty;
248                         if (!tp->t_stop) {
249                                 printf("Warning:%s: no t_stop, using nottystop\n", devtoname(dev));
250                                 tp->t_stop = nottystop;
251                         }
252                 }
253         }
254
255         if (vn_isdisk(vp, NULL)) {
256                 if (!dev->si_bsize_phys)
257                         dev->si_bsize_phys = DEV_BSIZE;
258         }
259         if ((dev_dflags(dev) & D_DISK) == 0) {
260                 cp = devtoname(dev);
261                 if (*cp == '#') {
262                         printf("WARNING: driver %s should register devices with make_dev() (dev_t = \"%s\")\n",
263                             dev_dname(dev), cp);
264                 }
265         }
266         if (dev_ref_debug)
267                 printf("spec_open: %s %d\n", dev->si_name, vp->v_opencount);
268 done:
269         if (error) {
270                 KKASSERT(vp->v_opencount > 0);
271                 if (--vp->v_opencount == 0)
272                         v_release_rdev(vp);
273         }
274         return (error);
275 }
276
277 /*
278  * Vnode op for read
279  *
280  * spec_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
281  *           struct ucred *a_cred)
282  */
283 /* ARGSUSED */
284 static int
285 spec_read(struct vop_read_args *ap)
286 {
287         struct vnode *vp;
288         struct thread *td;
289         struct uio *uio;
290         dev_t dev;
291         int error;
292
293         vp = ap->a_vp;
294         dev = vp->v_rdev;
295         uio = ap->a_uio;
296         td = uio->uio_td;
297
298         if (uio->uio_resid == 0)
299                 return (0);
300
301         VOP_UNLOCK(vp, NULL, 0, td);
302         error = dev_dread(dev, uio, ap->a_ioflag);
303         vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
304         return (error);
305 }
306
307 /*
308  * Vnode op for write
309  *
310  * spec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
311  *            struct ucred *a_cred)
312  */
313 /* ARGSUSED */
314 static int
315 spec_write(struct vop_write_args *ap)
316 {
317         struct vnode *vp;
318         struct thread *td;
319         struct uio *uio;
320         dev_t dev;
321         int error;
322
323         vp = ap->a_vp;
324         dev = vp->v_rdev;
325         uio = ap->a_uio;
326         td = uio->uio_td;
327
328         VOP_UNLOCK(vp, NULL, 0, td);
329         error = dev_dwrite(dev, uio, ap->a_ioflag);
330         vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
331         return (error);
332 }
333
334 /*
335  * Device ioctl operation.
336  *
337  * spec_ioctl(struct vnode *a_vp, int a_command, caddr_t a_data,
338  *            int a_fflag, struct ucred *a_cred, struct thread *a_td)
339  */
340 /* ARGSUSED */
341 static int
342 spec_ioctl(struct vop_ioctl_args *ap)
343 {
344         dev_t dev;
345
346         dev = ap->a_vp->v_rdev;
347         return (dev_dioctl(dev, ap->a_command, ap->a_data,
348                     ap->a_fflag, ap->a_td));
349 }
350
351 /*
352  * spec_poll(struct vnode *a_vp, int a_events, struct ucred *a_cred,
353  *           struct thread *a_td)
354  */
355 /* ARGSUSED */
356 static int
357 spec_poll(struct vop_poll_args *ap)
358 {
359         dev_t dev;
360
361         dev = ap->a_vp->v_rdev;
362         return (dev_dpoll(dev, ap->a_events, ap->a_td));
363 }
364
365 /*
366  * spec_kqfilter(struct vnode *a_vp, struct knote *a_kn)
367  */
368 /* ARGSUSED */
369 static int
370 spec_kqfilter(struct vop_kqfilter_args *ap)
371 {
372         dev_t dev;
373
374         dev = ap->a_vp->v_rdev;
375         return (dev_dkqfilter(dev, ap->a_kn));
376 }
377
378 /*
379  * Synch buffers associated with a block device
380  *
381  * spec_fsync(struct vnode *a_vp, struct ucred *a_cred,
382  *            int a_waitfor, struct thread *a_td)
383  */
384 /* ARGSUSED */
385 static int
386 spec_fsync(struct vop_fsync_args *ap)
387 {
388         struct vnode *vp = ap->a_vp;
389         struct buf *bp;
390         struct buf *nbp;
391         int s;
392         int maxretry = 10000;   /* large, arbitrarily chosen */
393
394         if (!vn_isdisk(vp, NULL))
395                 return (0);
396
397 loop1:
398         /*
399          * MARK/SCAN initialization to avoid infinite loops
400          */
401         s = splbio();
402         for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp;
403              bp = TAILQ_NEXT(bp, b_vnbufs)) {
404                 bp->b_flags &= ~B_SCANNED;
405         }
406         splx(s);
407
408         /*
409          * Flush all dirty buffers associated with a block device.
410          */
411 loop2:
412         s = splbio();
413         for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
414                 nbp = TAILQ_NEXT(bp, b_vnbufs);
415                 if ((bp->b_flags & B_SCANNED) != 0)
416                         continue;
417                 bp->b_flags |= B_SCANNED;
418                 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
419                         continue;
420                 if ((bp->b_flags & B_DELWRI) == 0)
421                         panic("spec_fsync: not dirty");
422                 if ((vp->v_flag & VOBJBUF) && (bp->b_flags & B_CLUSTEROK)) {
423                         BUF_UNLOCK(bp);
424                         vfs_bio_awrite(bp);
425                         splx(s);
426                 } else {
427                         bremfree(bp);
428                         splx(s);
429                         bawrite(bp);
430                 }
431                 goto loop2;
432         }
433
434         /*
435          * If synchronous the caller expects us to completely resolve all
436          * dirty buffers in the system.  Wait for in-progress I/O to
437          * complete (which could include background bitmap writes), then
438          * retry if dirty blocks still exist.
439          */
440         if (ap->a_waitfor == MNT_WAIT) {
441                 while (vp->v_numoutput) {
442                         vp->v_flag |= VBWAIT;
443                         (void) tsleep((caddr_t)&vp->v_numoutput, 0, "spfsyn", 0);
444                 }
445                 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
446                         if (--maxretry != 0) {
447                                 splx(s);
448                                 goto loop1;
449                         }
450                         vprint("spec_fsync: giving up on dirty", vp);
451                 }
452         }
453         splx(s);
454         return (0);
455 }
456
457 /*
458  * spec_inactive(struct vnode *a_vp, struct thread *a_td)
459  */
460 static int
461 spec_inactive(struct vop_inactive_args *ap)
462 {
463         VOP_UNLOCK(ap->a_vp, NULL, 0, ap->a_td);
464         return (0);
465 }
466
467 /*
468  * Just call the device strategy routine
469  *
470  * spec_strategy(struct vnode *a_vp, struct buf *a_bp)
471  */
472 static int
473 spec_strategy(struct vop_strategy_args *ap)
474 {
475         struct buf *bp;
476         struct vnode *vp;
477         struct mount *mp;
478
479         bp = ap->a_bp;
480         if (((bp->b_flags & B_READ) == 0) &&
481                 (LIST_FIRST(&bp->b_dep)) != NULL && bioops.io_start)
482                 (*bioops.io_start)(bp);
483
484         /*
485          * Collect statistics on synchronous and asynchronous read
486          * and write counts for disks that have associated filesystems.
487          */
488         vp = ap->a_vp;
489         if (vn_isdisk(vp, NULL) && (mp = vp->v_rdev->si_mountpoint) != NULL) {
490                 if ((bp->b_flags & B_READ) == 0) {
491                         if (bp->b_lock.lk_lockholder == LK_KERNTHREAD)
492                                 mp->mnt_stat.f_asyncwrites++;
493                         else
494                                 mp->mnt_stat.f_syncwrites++;
495                 } else {
496                         if (bp->b_lock.lk_lockholder == LK_KERNTHREAD)
497                                 mp->mnt_stat.f_asyncreads++;
498                         else
499                                 mp->mnt_stat.f_syncreads++;
500                 }
501         }
502         bp->b_dev = vp->v_rdev;
503         BUF_STRATEGY(bp, 0);
504         return (0);
505 }
506
507 /*
508  * spec_freeblks(struct vnode *a_vp, daddr_t a_addr, daddr_t a_length)
509  */
510 static int
511 spec_freeblks(struct vop_freeblks_args *ap)
512 {
513         struct buf *bp;
514
515         /*
516          * XXX: This assumes that strategy does the deed right away.
517          * XXX: this may not be TRTTD.
518          */
519         if ((dev_dflags(ap->a_vp->v_rdev) & D_CANFREE) == 0)
520                 return (0);
521         bp = geteblk(ap->a_length);
522         bp->b_flags |= B_FREEBUF;
523         bp->b_dev = ap->a_vp->v_rdev;
524         bp->b_blkno = ap->a_addr;
525         bp->b_offset = dbtob(ap->a_addr);
526         bp->b_bcount = ap->a_length;
527         BUF_STRATEGY(bp, 0);
528         return (0);
529 }
530
531 /*
532  * Implement degenerate case where the block requested is the block
533  * returned, and assume that the entire device is contiguous in regards
534  * to the contiguous block range (runp and runb).
535  *
536  * spec_bmap(struct vnode *a_vp, daddr_t a_bn, struct vnode **a_vpp,
537  *           daddr_t *a_bnp, int *a_runp, int *a_runb)
538  */
539 static int
540 spec_bmap(struct vop_bmap_args *ap)
541 {
542         struct vnode *vp = ap->a_vp;
543         int runp = 0;
544         int runb = 0;
545
546         if (ap->a_vpp != NULL)
547                 *ap->a_vpp = vp;
548         if (ap->a_bnp != NULL)
549                 *ap->a_bnp = ap->a_bn;
550         if (vp->v_mount != NULL)
551                 runp = runb = MAXBSIZE / vp->v_mount->mnt_stat.f_iosize;
552         if (ap->a_runp != NULL)
553                 *ap->a_runp = runp;
554         if (ap->a_runb != NULL)
555                 *ap->a_runb = runb;
556         return (0);
557 }
558
559 /*
560  * Device close routine
561  *
562  * spec_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
563  *            struct thread *a_td)
564  */
565 /* ARGSUSED */
566 static int
567 spec_close(struct vop_close_args *ap)
568 {
569         struct proc *p = ap->a_td->td_proc;
570         struct vnode *vp = ap->a_vp;
571         dev_t dev = vp->v_rdev;
572         int error;
573
574         /*
575          * Hack: a tty device that is a controlling terminal
576          * has a reference from the session structure.
577          * We cannot easily tell that a character device is
578          * a controlling terminal, unless it is the closing
579          * process' controlling terminal.  In that case,
580          * if the reference count is 2 (this last descriptor
581          * plus the session), release the reference from the session.
582          */
583         reference_dev(dev);
584         if (vcount(vp) == 2 && vp->v_opencount == 1 && 
585             p && (vp->v_flag & VXLOCK) == 0 && vp == p->p_session->s_ttyvp) {
586                 vrele(vp);
587                 p->p_session->s_ttyvp = NULL;
588         }
589
590         /*
591          * Vnodes can be opened and close multiple times.  Do not really
592          * close the device unless (1) it is being closed forcibly,
593          * (2) the device wants to track closes, or (3) this is the last
594          * vnode doing its last close on the device.
595          *
596          * XXX the VXLOCK (force close) case can leave vnodes referencing
597          * a closed device.
598          */
599         if ((vp->v_flag & VXLOCK) ||
600             (dev_dflags(dev) & D_TRACKCLOSE) ||
601             (vcount(vp) <= 1 && vp->v_opencount == 1)) {
602                 error = dev_dclose(dev, ap->a_fflag, S_IFCHR, ap->a_td);
603         } else {
604                 error = 0;
605         }
606
607         /*
608          * Track the actual opens and closes on the vnode.  The last close
609          * disassociates the rdev.
610          */
611         KKASSERT(vp->v_opencount > 0);
612         if (dev_ref_debug)
613                 printf("spec_close: %s %d\n", dev->si_name, vp->v_opencount - 1);
614         if (--vp->v_opencount == 0)
615                 v_release_rdev(vp);
616         release_dev(dev);
617         return(error);
618 }
619
620 /*
621  * Print out the contents of a special device vnode.
622  *
623  * spec_print(struct vnode *a_vp)
624  */
625 static int
626 spec_print(struct vop_print_args *ap)
627 {
628         printf("tag VT_NON, dev %s\n", devtoname(ap->a_vp->v_rdev));
629         return (0);
630 }
631
632 /*
633  * Special device advisory byte-level locks.
634  *
635  * spec_advlock(struct vnode *a_vp, caddr_t a_id, int a_op,
636  *              struct flock *a_fl, int a_flags)
637  */
638 /* ARGSUSED */
639 static int
640 spec_advlock(struct vop_advlock_args *ap)
641 {
642         return (ap->a_flags & F_FLOCK ? EOPNOTSUPP : EINVAL);
643 }
644
645 static void
646 spec_getpages_iodone(struct buf *bp)
647 {
648         bp->b_flags |= B_DONE;
649         wakeup(bp);
650 }
651
652 static int
653 spec_getpages(struct vop_getpages_args *ap)
654 {
655         vm_offset_t kva;
656         int error;
657         int i, pcount, size, s;
658         daddr_t blkno;
659         struct buf *bp;
660         vm_page_t m;
661         vm_ooffset_t offset;
662         int toff, nextoff, nread;
663         struct vnode *vp = ap->a_vp;
664         int blksiz;
665         int gotreqpage;
666
667         error = 0;
668         pcount = round_page(ap->a_count) / PAGE_SIZE;
669
670         /*
671          * Calculate the offset of the transfer and do sanity check.
672          * FreeBSD currently only supports an 8 TB range due to b_blkno
673          * being in DEV_BSIZE ( usually 512 ) byte chunks on call to
674          * VOP_STRATEGY.  XXX
675          */
676         offset = IDX_TO_OFF(ap->a_m[0]->pindex) + ap->a_offset;
677
678 #define DADDR_T_BIT     (sizeof(daddr_t)*8)
679 #define OFFSET_MAX      ((1LL << (DADDR_T_BIT + DEV_BSHIFT)) - 1)
680
681         if (offset < 0 || offset > OFFSET_MAX) {
682                 /* XXX still no %q in kernel. */
683                 printf("spec_getpages: preposterous offset 0x%x%08x\n",
684                        (u_int)((u_quad_t)offset >> 32),
685                        (u_int)(offset & 0xffffffff));
686                 return (VM_PAGER_ERROR);
687         }
688
689         blkno = btodb(offset);
690
691         /*
692          * Round up physical size for real devices.  We cannot round using
693          * v_mount's block size data because v_mount has nothing to do with
694          * the device.  i.e. it's usually '/dev'.  We need the physical block
695          * size for the device itself.
696          *
697          * We can't use v_rdev->si_mountpoint because it only exists when the
698          * block device is mounted.  However, we can use v_rdev.
699          */
700
701         if (vn_isdisk(vp, NULL))
702                 blksiz = vp->v_rdev->si_bsize_phys;
703         else
704                 blksiz = DEV_BSIZE;
705
706         size = (ap->a_count + blksiz - 1) & ~(blksiz - 1);
707
708         bp = getpbuf(NULL);
709         kva = (vm_offset_t)bp->b_data;
710
711         /*
712          * Map the pages to be read into the kva.
713          */
714         pmap_qenter(kva, ap->a_m, pcount);
715
716         /* Build a minimal buffer header. */
717         bp->b_flags = B_READ | B_CALL;
718         bp->b_iodone = spec_getpages_iodone;
719
720         /* B_PHYS is not set, but it is nice to fill this in. */
721         bp->b_blkno = blkno;
722         bp->b_lblkno = blkno;
723         pbgetvp(ap->a_vp, bp);
724         bp->b_bcount = size;
725         bp->b_bufsize = size;
726         bp->b_resid = 0;
727         bp->b_runningbufspace = bp->b_bufsize;
728         runningbufspace += bp->b_runningbufspace;
729
730         mycpu->gd_cnt.v_vnodein++;
731         mycpu->gd_cnt.v_vnodepgsin += pcount;
732
733         /* Do the input. */
734         VOP_STRATEGY(bp->b_vp, bp);
735
736         s = splbio();
737
738         /* We definitely need to be at splbio here. */
739         while ((bp->b_flags & B_DONE) == 0) {
740                 tsleep(bp, 0, "spread", 0);
741         }
742
743         splx(s);
744
745         if ((bp->b_flags & B_ERROR) != 0) {
746                 if (bp->b_error)
747                         error = bp->b_error;
748                 else
749                         error = EIO;
750         }
751
752         nread = size - bp->b_resid;
753
754         if (nread < ap->a_count) {
755                 bzero((caddr_t)kva + nread,
756                         ap->a_count - nread);
757         }
758         pmap_qremove(kva, pcount);
759
760
761         gotreqpage = 0;
762         for (i = 0, toff = 0; i < pcount; i++, toff = nextoff) {
763                 nextoff = toff + PAGE_SIZE;
764                 m = ap->a_m[i];
765
766                 m->flags &= ~PG_ZERO;
767
768                 if (nextoff <= nread) {
769                         m->valid = VM_PAGE_BITS_ALL;
770                         vm_page_undirty(m);
771                 } else if (toff < nread) {
772                         /*
773                          * Since this is a VM request, we have to supply the
774                          * unaligned offset to allow vm_page_set_validclean()
775                          * to zero sub-DEV_BSIZE'd portions of the page.
776                          */
777                         vm_page_set_validclean(m, 0, nread - toff);
778                 } else {
779                         m->valid = 0;
780                         vm_page_undirty(m);
781                 }
782
783                 if (i != ap->a_reqpage) {
784                         /*
785                          * Just in case someone was asking for this page we
786                          * now tell them that it is ok to use.
787                          */
788                         if (!error || (m->valid == VM_PAGE_BITS_ALL)) {
789                                 if (m->valid) {
790                                         if (m->flags & PG_WANTED) {
791                                                 vm_page_activate(m);
792                                         } else {
793                                                 vm_page_deactivate(m);
794                                         }
795                                         vm_page_wakeup(m);
796                                 } else {
797                                         vm_page_free(m);
798                                 }
799                         } else {
800                                 vm_page_free(m);
801                         }
802                 } else if (m->valid) {
803                         gotreqpage = 1;
804                         /*
805                          * Since this is a VM request, we need to make the
806                          * entire page presentable by zeroing invalid sections.
807                          */
808                         if (m->valid != VM_PAGE_BITS_ALL)
809                             vm_page_zero_invalid(m, FALSE);
810                 }
811         }
812         if (!gotreqpage) {
813                 m = ap->a_m[ap->a_reqpage];
814                 printf(
815             "spec_getpages:(%s) I/O read failure: (error=%d) bp %p vp %p\n",
816                         devtoname(bp->b_dev), error, bp, bp->b_vp);
817                 printf(
818             "               size: %d, resid: %ld, a_count: %d, valid: 0x%x\n",
819                     size, bp->b_resid, ap->a_count, m->valid);
820                 printf(
821             "               nread: %d, reqpage: %d, pindex: %lu, pcount: %d\n",
822                     nread, ap->a_reqpage, (u_long)m->pindex, pcount);
823                 /*
824                  * Free the buffer header back to the swap buffer pool.
825                  */
826                 relpbuf(bp, NULL);
827                 return VM_PAGER_ERROR;
828         }
829         /*
830          * Free the buffer header back to the swap buffer pool.
831          */
832         relpbuf(bp, NULL);
833         return VM_PAGER_OK;
834 }