Home | History | Annotate | Line # | Download | only in genfs
genfs_vnops.c revision 1.109
      1  1.109      yamt /*	$NetBSD: genfs_vnops.c,v 1.109 2005/11/12 22:29:53 yamt Exp $	*/
      2    1.6      fvdl 
      3    1.6      fvdl /*
      4    1.6      fvdl  * Copyright (c) 1982, 1986, 1989, 1993
      5    1.6      fvdl  *	The Regents of the University of California.  All rights reserved.
      6    1.6      fvdl  *
      7    1.6      fvdl  * Redistribution and use in source and binary forms, with or without
      8    1.6      fvdl  * modification, are permitted provided that the following conditions
      9    1.6      fvdl  * are met:
     10    1.6      fvdl  * 1. Redistributions of source code must retain the above copyright
     11    1.6      fvdl  *    notice, this list of conditions and the following disclaimer.
     12    1.6      fvdl  * 2. Redistributions in binary form must reproduce the above copyright
     13    1.6      fvdl  *    notice, this list of conditions and the following disclaimer in the
     14    1.6      fvdl  *    documentation and/or other materials provided with the distribution.
     15   1.81       agc  * 3. Neither the name of the University nor the names of its contributors
     16    1.6      fvdl  *    may be used to endorse or promote products derived from this software
     17    1.6      fvdl  *    without specific prior written permission.
     18    1.6      fvdl  *
     19    1.6      fvdl  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20    1.6      fvdl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21    1.6      fvdl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22    1.6      fvdl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23    1.6      fvdl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24    1.6      fvdl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25    1.6      fvdl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26    1.6      fvdl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27    1.6      fvdl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28    1.6      fvdl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29    1.6      fvdl  * SUCH DAMAGE.
     30    1.6      fvdl  *
     31    1.6      fvdl  */
     32   1.40     lukem 
     33   1.40     lukem #include <sys/cdefs.h>
     34  1.109      yamt __KERNEL_RCSID(0, "$NetBSD: genfs_vnops.c,v 1.109 2005/11/12 22:29:53 yamt Exp $");
     35    1.5     perry 
     36   1.92       dbj #if defined(_KERNEL_OPT)
     37    1.8   thorpej #include "opt_nfsserver.h"
     38   1.92       dbj #endif
     39    1.8   thorpej 
     40    1.1   mycroft #include <sys/param.h>
     41    1.1   mycroft #include <sys/systm.h>
     42    1.6      fvdl #include <sys/proc.h>
     43    1.1   mycroft #include <sys/kernel.h>
     44    1.1   mycroft #include <sys/mount.h>
     45    1.1   mycroft #include <sys/namei.h>
     46    1.1   mycroft #include <sys/vnode.h>
     47   1.13  wrstuden #include <sys/fcntl.h>
     48    1.1   mycroft #include <sys/malloc.h>
     49    1.3   mycroft #include <sys/poll.h>
     50   1.37       chs #include <sys/mman.h>
     51   1.66  jdolecek #include <sys/file.h>
     52    1.1   mycroft 
     53    1.1   mycroft #include <miscfs/genfs/genfs.h>
     54   1.37       chs #include <miscfs/genfs/genfs_node.h>
     55    1.6      fvdl #include <miscfs/specfs/specdev.h>
     56    1.1   mycroft 
     57   1.21       chs #include <uvm/uvm.h>
     58   1.21       chs #include <uvm/uvm_pager.h>
     59   1.21       chs 
     60    1.8   thorpej #ifdef NFSSERVER
     61    1.8   thorpej #include <nfs/rpcv2.h>
     62    1.8   thorpej #include <nfs/nfsproto.h>
     63    1.8   thorpej #include <nfs/nfs.h>
     64    1.8   thorpej #include <nfs/nqnfs.h>
     65    1.8   thorpej #include <nfs/nfs_var.h>
     66    1.8   thorpej #endif
     67    1.8   thorpej 
     68   1.63     enami static __inline void genfs_rel_pages(struct vm_page **, int);
     69   1.70  christos static void filt_genfsdetach(struct knote *);
     70   1.70  christos static int filt_genfsread(struct knote *, long);
     71   1.70  christos static int filt_genfsvnode(struct knote *, long);
     72   1.70  christos 
     73   1.63     enami 
     74   1.41  christos #define MAX_READ_AHEAD	16 	/* XXXUBC 16 */
     75   1.63     enami int genfs_rapages = MAX_READ_AHEAD; /* # of pages in each chunk of readahead */
     76   1.63     enami int genfs_racount = 2;		/* # of page chunks to readahead */
     77   1.63     enami int genfs_raskip = 2;		/* # of busy page chunks allowed to skip */
     78   1.41  christos 
     79    1.1   mycroft int
     80   1.53     enami genfs_poll(void *v)
     81    1.1   mycroft {
     82    1.3   mycroft 	struct vop_poll_args /* {
     83    1.1   mycroft 		struct vnode *a_vp;
     84    1.3   mycroft 		int a_events;
     85   1.80      fvdl 		struct proc *a_p;
     86    1.1   mycroft 	} */ *ap = v;
     87    1.1   mycroft 
     88    1.3   mycroft 	return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
     89    1.1   mycroft }
     90    1.1   mycroft 
     91    1.1   mycroft int
     92   1.53     enami genfs_seek(void *v)
     93    1.4    kleink {
     94    1.4    kleink 	struct vop_seek_args /* {
     95    1.4    kleink 		struct vnode *a_vp;
     96    1.4    kleink 		off_t a_oldoff;
     97    1.4    kleink 		off_t a_newoff;
     98    1.4    kleink 		struct ucred *a_ucred;
     99    1.4    kleink 	} */ *ap = v;
    100    1.4    kleink 
    101    1.4    kleink 	if (ap->a_newoff < 0)
    102    1.4    kleink 		return (EINVAL);
    103    1.4    kleink 
    104    1.4    kleink 	return (0);
    105    1.4    kleink }
    106    1.4    kleink 
    107    1.4    kleink int
    108   1.53     enami genfs_abortop(void *v)
    109    1.1   mycroft {
    110    1.1   mycroft 	struct vop_abortop_args /* {
    111    1.1   mycroft 		struct vnode *a_dvp;
    112    1.1   mycroft 		struct componentname *a_cnp;
    113    1.1   mycroft 	} */ *ap = v;
    114   1.53     enami 
    115    1.1   mycroft 	if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
    116   1.19   thorpej 		PNBUF_PUT(ap->a_cnp->cn_pnbuf);
    117    1.1   mycroft 	return (0);
    118   1.13  wrstuden }
    119   1.13  wrstuden 
    120   1.13  wrstuden int
    121   1.53     enami genfs_fcntl(void *v)
    122   1.13  wrstuden {
    123   1.13  wrstuden 	struct vop_fcntl_args /* {
    124   1.13  wrstuden 		struct vnode *a_vp;
    125   1.13  wrstuden 		u_int a_command;
    126   1.13  wrstuden 		caddr_t a_data;
    127   1.13  wrstuden 		int a_fflag;
    128   1.13  wrstuden 		struct ucred *a_cred;
    129   1.80      fvdl 		struct proc *a_p;
    130   1.13  wrstuden 	} */ *ap = v;
    131   1.13  wrstuden 
    132   1.13  wrstuden 	if (ap->a_command == F_SETFL)
    133   1.13  wrstuden 		return (0);
    134   1.13  wrstuden 	else
    135   1.13  wrstuden 		return (EOPNOTSUPP);
    136    1.1   mycroft }
    137    1.1   mycroft 
    138    1.1   mycroft /*ARGSUSED*/
    139    1.1   mycroft int
    140   1.53     enami genfs_badop(void *v)
    141    1.1   mycroft {
    142    1.1   mycroft 
    143    1.1   mycroft 	panic("genfs: bad op");
    144    1.1   mycroft }
    145    1.1   mycroft 
    146    1.1   mycroft /*ARGSUSED*/
    147    1.1   mycroft int
    148   1.53     enami genfs_nullop(void *v)
    149    1.1   mycroft {
    150    1.1   mycroft 
    151    1.1   mycroft 	return (0);
    152   1.10    kleink }
    153   1.10    kleink 
    154   1.10    kleink /*ARGSUSED*/
    155   1.10    kleink int
    156   1.53     enami genfs_einval(void *v)
    157   1.10    kleink {
    158   1.10    kleink 
    159   1.10    kleink 	return (EINVAL);
    160    1.1   mycroft }
    161    1.1   mycroft 
    162   1.12  wrstuden /*
    163   1.74  jdolecek  * Called when an fs doesn't support a particular vop.
    164   1.74  jdolecek  * This takes care to vrele, vput, or vunlock passed in vnodes.
    165   1.12  wrstuden  */
    166   1.12  wrstuden int
    167   1.75  jdolecek genfs_eopnotsupp(void *v)
    168   1.12  wrstuden {
    169   1.12  wrstuden 	struct vop_generic_args /*
    170   1.12  wrstuden 		struct vnodeop_desc *a_desc;
    171   1.53     enami 		/ * other random data follows, presumably * /
    172   1.12  wrstuden 	} */ *ap = v;
    173   1.12  wrstuden 	struct vnodeop_desc *desc = ap->a_desc;
    174   1.74  jdolecek 	struct vnode *vp, *vp_last = NULL;
    175   1.12  wrstuden 	int flags, i, j, offset;
    176   1.12  wrstuden 
    177   1.12  wrstuden 	flags = desc->vdesc_flags;
    178   1.12  wrstuden 	for (i = 0; i < VDESC_MAX_VPS; flags >>=1, i++) {
    179   1.12  wrstuden 		if ((offset = desc->vdesc_vp_offsets[i]) == VDESC_NO_OFFSET)
    180   1.12  wrstuden 			break;	/* stop at end of list */
    181   1.12  wrstuden 		if ((j = flags & VDESC_VP0_WILLPUT)) {
    182   1.53     enami 			vp = *VOPARG_OFFSETTO(struct vnode **, offset, ap);
    183   1.74  jdolecek 
    184   1.74  jdolecek 			/* Skip if NULL */
    185   1.74  jdolecek 			if (!vp)
    186   1.74  jdolecek 				continue;
    187   1.74  jdolecek 
    188   1.12  wrstuden 			switch (j) {
    189   1.12  wrstuden 			case VDESC_VP0_WILLPUT:
    190   1.74  jdolecek 				/* Check for dvp == vp cases */
    191   1.74  jdolecek 				if (vp == vp_last)
    192   1.74  jdolecek 					vrele(vp);
    193   1.74  jdolecek 				else {
    194   1.74  jdolecek 					vput(vp);
    195   1.74  jdolecek 					vp_last = vp;
    196   1.74  jdolecek 				}
    197   1.12  wrstuden 				break;
    198   1.12  wrstuden 			case VDESC_VP0_WILLUNLOCK:
    199   1.12  wrstuden 				VOP_UNLOCK(vp, 0);
    200   1.12  wrstuden 				break;
    201   1.12  wrstuden 			case VDESC_VP0_WILLRELE:
    202   1.12  wrstuden 				vrele(vp);
    203   1.12  wrstuden 				break;
    204   1.12  wrstuden 			}
    205   1.12  wrstuden 		}
    206   1.12  wrstuden 	}
    207   1.12  wrstuden 
    208   1.12  wrstuden 	return (EOPNOTSUPP);
    209   1.12  wrstuden }
    210   1.12  wrstuden 
    211    1.1   mycroft /*ARGSUSED*/
    212    1.1   mycroft int
    213   1.53     enami genfs_ebadf(void *v)
    214    1.1   mycroft {
    215    1.1   mycroft 
    216    1.1   mycroft 	return (EBADF);
    217    1.9  matthias }
    218    1.9  matthias 
    219    1.9  matthias /* ARGSUSED */
    220    1.9  matthias int
    221   1.53     enami genfs_enoioctl(void *v)
    222    1.9  matthias {
    223    1.9  matthias 
    224   1.51    atatat 	return (EPASSTHROUGH);
    225    1.6      fvdl }
    226    1.6      fvdl 
    227    1.6      fvdl 
    228    1.6      fvdl /*
    229   1.15      fvdl  * Eliminate all activity associated with the requested vnode
    230    1.6      fvdl  * and with all vnodes aliased to the requested vnode.
    231    1.6      fvdl  */
    232    1.6      fvdl int
    233   1.53     enami genfs_revoke(void *v)
    234    1.6      fvdl {
    235    1.6      fvdl 	struct vop_revoke_args /* {
    236    1.6      fvdl 		struct vnode *a_vp;
    237    1.6      fvdl 		int a_flags;
    238    1.6      fvdl 	} */ *ap = v;
    239    1.6      fvdl 	struct vnode *vp, *vq;
    240   1.80      fvdl 	struct proc *p = curproc;	/* XXX */
    241    1.6      fvdl 
    242    1.6      fvdl #ifdef DIAGNOSTIC
    243    1.6      fvdl 	if ((ap->a_flags & REVOKEALL) == 0)
    244    1.6      fvdl 		panic("genfs_revoke: not revokeall");
    245    1.6      fvdl #endif
    246    1.6      fvdl 
    247    1.6      fvdl 	vp = ap->a_vp;
    248    1.6      fvdl 	simple_lock(&vp->v_interlock);
    249    1.6      fvdl 
    250    1.6      fvdl 	if (vp->v_flag & VALIASED) {
    251    1.6      fvdl 		/*
    252    1.6      fvdl 		 * If a vgone (or vclean) is already in progress,
    253    1.6      fvdl 		 * wait until it is done and return.
    254    1.6      fvdl 		 */
    255    1.6      fvdl 		if (vp->v_flag & VXLOCK) {
    256    1.6      fvdl 			vp->v_flag |= VXWANT;
    257   1.83        pk 			ltsleep(vp, PINOD|PNORELOCK, "vop_revokeall", 0,
    258   1.83        pk 				&vp->v_interlock);
    259    1.6      fvdl 			return (0);
    260    1.6      fvdl 		}
    261    1.6      fvdl 		/*
    262    1.6      fvdl 		 * Ensure that vp will not be vgone'd while we
    263    1.6      fvdl 		 * are eliminating its aliases.
    264    1.6      fvdl 		 */
    265    1.6      fvdl 		vp->v_flag |= VXLOCK;
    266    1.6      fvdl 		simple_unlock(&vp->v_interlock);
    267    1.6      fvdl 		while (vp->v_flag & VALIASED) {
    268    1.6      fvdl 			simple_lock(&spechash_slock);
    269    1.6      fvdl 			for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
    270    1.6      fvdl 				if (vq->v_rdev != vp->v_rdev ||
    271    1.6      fvdl 				    vq->v_type != vp->v_type || vp == vq)
    272    1.6      fvdl 					continue;
    273    1.6      fvdl 				simple_unlock(&spechash_slock);
    274    1.6      fvdl 				vgone(vq);
    275    1.6      fvdl 				break;
    276    1.6      fvdl 			}
    277    1.6      fvdl 			if (vq == NULLVP)
    278    1.6      fvdl 				simple_unlock(&spechash_slock);
    279    1.6      fvdl 		}
    280    1.6      fvdl 		/*
    281    1.6      fvdl 		 * Remove the lock so that vgone below will
    282    1.6      fvdl 		 * really eliminate the vnode after which time
    283    1.6      fvdl 		 * vgone will awaken any sleepers.
    284    1.6      fvdl 		 */
    285    1.6      fvdl 		simple_lock(&vp->v_interlock);
    286    1.6      fvdl 		vp->v_flag &= ~VXLOCK;
    287    1.6      fvdl 	}
    288   1.80      fvdl 	vgonel(vp, p);
    289    1.6      fvdl 	return (0);
    290    1.6      fvdl }
    291    1.6      fvdl 
    292    1.6      fvdl /*
    293   1.12  wrstuden  * Lock the node.
    294    1.6      fvdl  */
    295    1.6      fvdl int
    296   1.53     enami genfs_lock(void *v)
    297    1.6      fvdl {
    298    1.6      fvdl 	struct vop_lock_args /* {
    299    1.6      fvdl 		struct vnode *a_vp;
    300    1.6      fvdl 		int a_flags;
    301    1.6      fvdl 	} */ *ap = v;
    302    1.6      fvdl 	struct vnode *vp = ap->a_vp;
    303    1.6      fvdl 
    304   1.86   hannken 	return (lockmgr(vp->v_vnlock, ap->a_flags, &vp->v_interlock));
    305    1.6      fvdl }
    306    1.6      fvdl 
    307    1.6      fvdl /*
    308   1.12  wrstuden  * Unlock the node.
    309    1.6      fvdl  */
    310    1.6      fvdl int
    311   1.53     enami genfs_unlock(void *v)
    312    1.6      fvdl {
    313    1.6      fvdl 	struct vop_unlock_args /* {
    314    1.6      fvdl 		struct vnode *a_vp;
    315    1.6      fvdl 		int a_flags;
    316    1.6      fvdl 	} */ *ap = v;
    317    1.6      fvdl 	struct vnode *vp = ap->a_vp;
    318    1.6      fvdl 
    319   1.86   hannken 	return (lockmgr(vp->v_vnlock, ap->a_flags | LK_RELEASE,
    320   1.53     enami 	    &vp->v_interlock));
    321    1.6      fvdl }
    322    1.6      fvdl 
    323    1.6      fvdl /*
    324   1.12  wrstuden  * Return whether or not the node is locked.
    325    1.6      fvdl  */
    326    1.6      fvdl int
    327   1.53     enami genfs_islocked(void *v)
    328    1.6      fvdl {
    329    1.6      fvdl 	struct vop_islocked_args /* {
    330    1.6      fvdl 		struct vnode *a_vp;
    331    1.6      fvdl 	} */ *ap = v;
    332    1.6      fvdl 	struct vnode *vp = ap->a_vp;
    333    1.6      fvdl 
    334   1.86   hannken 	return (lockstatus(vp->v_vnlock));
    335   1.12  wrstuden }
    336   1.12  wrstuden 
    337   1.12  wrstuden /*
    338   1.12  wrstuden  * Stubs to use when there is no locking to be done on the underlying object.
    339   1.12  wrstuden  */
    340   1.12  wrstuden int
    341   1.53     enami genfs_nolock(void *v)
    342   1.12  wrstuden {
    343   1.12  wrstuden 	struct vop_lock_args /* {
    344   1.12  wrstuden 		struct vnode *a_vp;
    345   1.12  wrstuden 		int a_flags;
    346   1.80      fvdl 		struct proc *a_p;
    347   1.12  wrstuden 	} */ *ap = v;
    348   1.12  wrstuden 
    349   1.12  wrstuden 	/*
    350   1.12  wrstuden 	 * Since we are not using the lock manager, we must clear
    351   1.12  wrstuden 	 * the interlock here.
    352   1.12  wrstuden 	 */
    353   1.12  wrstuden 	if (ap->a_flags & LK_INTERLOCK)
    354   1.12  wrstuden 		simple_unlock(&ap->a_vp->v_interlock);
    355   1.12  wrstuden 	return (0);
    356   1.12  wrstuden }
    357   1.12  wrstuden 
    358   1.12  wrstuden int
    359   1.53     enami genfs_nounlock(void *v)
    360   1.12  wrstuden {
    361   1.53     enami 
    362   1.12  wrstuden 	return (0);
    363   1.12  wrstuden }
    364   1.12  wrstuden 
    365   1.12  wrstuden int
    366   1.53     enami genfs_noislocked(void *v)
    367   1.12  wrstuden {
    368   1.53     enami 
    369   1.12  wrstuden 	return (0);
    370    1.8   thorpej }
    371    1.8   thorpej 
    372    1.8   thorpej /*
    373    1.8   thorpej  * Local lease check for NFS servers.  Just set up args and let
    374    1.8   thorpej  * nqsrv_getlease() do the rest.  If NFSSERVER is not in the kernel,
    375    1.8   thorpej  * this is a null operation.
    376    1.8   thorpej  */
    377    1.8   thorpej int
    378   1.53     enami genfs_lease_check(void *v)
    379    1.8   thorpej {
    380    1.8   thorpej #ifdef NFSSERVER
    381    1.8   thorpej 	struct vop_lease_args /* {
    382    1.8   thorpej 		struct vnode *a_vp;
    383   1.80      fvdl 		struct proc *a_p;
    384    1.8   thorpej 		struct ucred *a_cred;
    385    1.8   thorpej 		int a_flag;
    386    1.8   thorpej 	} */ *ap = v;
    387    1.8   thorpej 	u_int32_t duration = 0;
    388    1.8   thorpej 	int cache;
    389    1.8   thorpej 	u_quad_t frev;
    390    1.8   thorpej 
    391    1.8   thorpej 	(void) nqsrv_getlease(ap->a_vp, &duration, ND_CHECK | ap->a_flag,
    392   1.80      fvdl 	    NQLOCALSLP, ap->a_p, (struct mbuf *)0, &cache, &frev, ap->a_cred);
    393    1.8   thorpej 	return (0);
    394    1.8   thorpej #else
    395    1.8   thorpej 	return (0);
    396    1.8   thorpej #endif /* NFSSERVER */
    397   1.34       chs }
    398   1.34       chs 
    399   1.34       chs int
    400   1.53     enami genfs_mmap(void *v)
    401   1.34       chs {
    402   1.53     enami 
    403   1.53     enami 	return (0);
    404   1.21       chs }
    405   1.21       chs 
    406   1.63     enami static __inline void
    407   1.63     enami genfs_rel_pages(struct vm_page **pgs, int npages)
    408   1.63     enami {
    409   1.63     enami 	int i;
    410   1.63     enami 
    411   1.63     enami 	for (i = 0; i < npages; i++) {
    412   1.63     enami 		struct vm_page *pg = pgs[i];
    413   1.63     enami 
    414   1.63     enami 		if (pg == NULL)
    415   1.63     enami 			continue;
    416   1.63     enami 		if (pg->flags & PG_FAKE) {
    417   1.63     enami 			pg->flags |= PG_RELEASED;
    418   1.63     enami 		}
    419   1.63     enami 	}
    420   1.64     enami 	uvm_lock_pageq();
    421   1.63     enami 	uvm_page_unbusy(pgs, npages);
    422   1.64     enami 	uvm_unlock_pageq();
    423   1.63     enami }
    424   1.63     enami 
    425   1.21       chs /*
    426   1.21       chs  * generic VM getpages routine.
    427   1.21       chs  * Return PG_BUSY pages for the given range,
    428   1.21       chs  * reading from backing store if necessary.
    429   1.21       chs  */
    430   1.21       chs 
    431   1.21       chs int
    432   1.53     enami genfs_getpages(void *v)
    433   1.21       chs {
    434   1.21       chs 	struct vop_getpages_args /* {
    435   1.21       chs 		struct vnode *a_vp;
    436   1.21       chs 		voff_t a_offset;
    437   1.33       chs 		struct vm_page **a_m;
    438   1.21       chs 		int *a_count;
    439   1.21       chs 		int a_centeridx;
    440   1.21       chs 		vm_prot_t a_access_type;
    441   1.21       chs 		int a_advice;
    442   1.21       chs 		int a_flags;
    443   1.21       chs 	} */ *ap = v;
    444   1.21       chs 
    445   1.30       chs 	off_t newsize, diskeof, memeof;
    446   1.26       chs 	off_t offset, origoffset, startoffset, endoffset, raoffset;
    447   1.21       chs 	daddr_t lbn, blkno;
    448   1.21       chs 	int s, i, error, npages, orignpages, npgs, run, ridx, pidx, pcount;
    449   1.37       chs 	int fs_bshift, fs_bsize, dev_bshift;
    450   1.21       chs 	int flags = ap->a_flags;
    451   1.21       chs 	size_t bytes, iobytes, tailbytes, totalbytes, skipbytes;
    452   1.21       chs 	vaddr_t kva;
    453   1.21       chs 	struct buf *bp, *mbp;
    454   1.21       chs 	struct vnode *vp = ap->a_vp;
    455   1.36       chs 	struct vnode *devvp;
    456   1.37       chs 	struct genfs_node *gp = VTOG(vp);
    457   1.37       chs 	struct uvm_object *uobj = &vp->v_uobj;
    458   1.77      yamt 	struct vm_page *pg, **pgs, *pgs_onstack[MAX_READ_AHEAD];
    459   1.77      yamt 	int pgs_size;
    460   1.69   thorpej 	struct ucred *cred = curproc->p_ucred;		/* XXXUBC curlwp */
    461   1.21       chs 	boolean_t async = (flags & PGO_SYNCIO) == 0;
    462   1.21       chs 	boolean_t write = (ap->a_access_type & VM_PROT_WRITE) != 0;
    463   1.21       chs 	boolean_t sawhole = FALSE;
    464   1.37       chs 	boolean_t overwrite = (flags & PGO_OVERWRITE) != 0;
    465  1.100      yamt 	boolean_t blockalloc = write && (flags & PGO_NOBLOCKALLOC) == 0;
    466   1.21       chs 	UVMHIST_FUNC("genfs_getpages"); UVMHIST_CALLED(ubchist);
    467   1.21       chs 
    468   1.30       chs 	UVMHIST_LOG(ubchist, "vp %p off 0x%x/%x count %d",
    469   1.53     enami 	    vp, ap->a_offset >> 32, ap->a_offset, *ap->a_count);
    470   1.30       chs 
    471  1.109      yamt 	KASSERT(vp->v_type == VREG || vp->v_type == VBLK);
    472  1.109      yamt 
    473   1.21       chs 	/* XXXUBC temp limit */
    474   1.41  christos 	if (*ap->a_count > MAX_READ_AHEAD) {
    475   1.37       chs 		panic("genfs_getpages: too many pages");
    476   1.21       chs 	}
    477   1.21       chs 
    478   1.26       chs 	error = 0;
    479   1.26       chs 	origoffset = ap->a_offset;
    480   1.26       chs 	orignpages = *ap->a_count;
    481   1.72  perseant 	GOP_SIZE(vp, vp->v_size, &diskeof, GOP_SIZE_READ);
    482   1.26       chs 	if (flags & PGO_PASTEOF) {
    483   1.37       chs 		newsize = MAX(vp->v_size,
    484   1.53     enami 		    origoffset + (orignpages << PAGE_SHIFT));
    485   1.82      yamt 		GOP_SIZE(vp, newsize, &memeof, GOP_SIZE_READ|GOP_SIZE_MEM);
    486   1.26       chs 	} else {
    487   1.82      yamt 		GOP_SIZE(vp, vp->v_size, &memeof, GOP_SIZE_READ|GOP_SIZE_MEM);
    488   1.21       chs 	}
    489   1.30       chs 	KASSERT(ap->a_centeridx >= 0 || ap->a_centeridx <= orignpages);
    490   1.30       chs 	KASSERT((origoffset & (PAGE_SIZE - 1)) == 0 && origoffset >= 0);
    491   1.30       chs 	KASSERT(orignpages > 0);
    492   1.95       chs 
    493   1.95       chs 	/*
    494   1.95       chs 	 * Bounds-check the request.
    495   1.95       chs 	 */
    496   1.95       chs 
    497   1.95       chs 	if (origoffset + (ap->a_centeridx << PAGE_SHIFT) >= memeof) {
    498   1.95       chs 		if ((flags & PGO_LOCKED) == 0) {
    499   1.95       chs 			simple_unlock(&uobj->vmobjlock);
    500   1.95       chs 		}
    501   1.95       chs 		UVMHIST_LOG(ubchist, "off 0x%x count %d goes past EOF 0x%x",
    502   1.95       chs 		    origoffset, *ap->a_count, memeof,0);
    503   1.95       chs 		return (EINVAL);
    504   1.95       chs 	}
    505   1.21       chs 
    506   1.99      yamt 	/* uobj is locked */
    507   1.99      yamt 
    508  1.103      yamt 	if ((flags & PGO_NOTIMESTAMP) == 0 &&
    509  1.103      yamt 	    (vp->v_type == VREG ||
    510  1.103      yamt 	    (vp->v_mount->mnt_flag & MNT_NODEVMTIME) == 0)) {
    511  1.103      yamt 		int updflags = 0;
    512  1.103      yamt 
    513  1.103      yamt 		if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0) {
    514  1.103      yamt 			updflags = GOP_UPDATE_ACCESSED;
    515  1.103      yamt 		}
    516  1.103      yamt 		if (write) {
    517  1.103      yamt 			updflags |= GOP_UPDATE_MODIFIED;
    518  1.103      yamt 		}
    519  1.103      yamt 		if (updflags != 0) {
    520  1.103      yamt 			GOP_MARKUPDATE(vp, updflags);
    521  1.103      yamt 		}
    522  1.103      yamt 	}
    523  1.103      yamt 
    524  1.101      yamt 	if (write) {
    525  1.101      yamt 		gp->g_dirtygen++;
    526  1.101      yamt 		if ((vp->v_flag & VONWORKLST) == 0) {
    527  1.101      yamt 			vn_syncer_add_to_worklist(vp, filedelay);
    528  1.101      yamt 		}
    529  1.103      yamt 		if ((vp->v_flag & (VWRITEMAP|VWRITEMAPDIRTY)) == VWRITEMAP) {
    530  1.103      yamt 			vp->v_flag |= VWRITEMAPDIRTY;
    531  1.103      yamt 		}
    532   1.99      yamt 	}
    533   1.99      yamt 
    534   1.21       chs 	/*
    535   1.21       chs 	 * For PGO_LOCKED requests, just return whatever's in memory.
    536   1.21       chs 	 */
    537   1.21       chs 
    538   1.21       chs 	if (flags & PGO_LOCKED) {
    539   1.21       chs 		uvn_findpages(uobj, origoffset, ap->a_count, ap->a_m,
    540   1.54     enami 		    UFP_NOWAIT|UFP_NOALLOC| (write ? UFP_NORDONLY : 0));
    541   1.21       chs 
    542   1.53     enami 		return (ap->a_m[ap->a_centeridx] == NULL ? EBUSY : 0);
    543   1.21       chs 	}
    544   1.21       chs 
    545   1.21       chs 	/*
    546   1.21       chs 	 * find the requested pages and make some simple checks.
    547   1.21       chs 	 * leave space in the page array for a whole block.
    548   1.21       chs 	 */
    549   1.21       chs 
    550   1.36       chs 	if (vp->v_type == VREG) {
    551   1.36       chs 		fs_bshift = vp->v_mount->mnt_fs_bshift;
    552   1.36       chs 		dev_bshift = vp->v_mount->mnt_dev_bshift;
    553   1.36       chs 	} else {
    554   1.36       chs 		fs_bshift = DEV_BSHIFT;
    555   1.36       chs 		dev_bshift = DEV_BSHIFT;
    556   1.36       chs 	}
    557   1.21       chs 	fs_bsize = 1 << fs_bshift;
    558   1.21       chs 
    559   1.30       chs 	orignpages = MIN(orignpages,
    560   1.30       chs 	    round_page(memeof - origoffset) >> PAGE_SHIFT);
    561   1.21       chs 	npages = orignpages;
    562   1.21       chs 	startoffset = origoffset & ~(fs_bsize - 1);
    563   1.53     enami 	endoffset = round_page((origoffset + (npages << PAGE_SHIFT) +
    564   1.53     enami 	    fs_bsize - 1) & ~(fs_bsize - 1));
    565   1.30       chs 	endoffset = MIN(endoffset, round_page(memeof));
    566   1.21       chs 	ridx = (origoffset - startoffset) >> PAGE_SHIFT;
    567   1.21       chs 
    568   1.77      yamt 	pgs_size = sizeof(struct vm_page *) *
    569   1.77      yamt 	    ((endoffset - startoffset) >> PAGE_SHIFT);
    570   1.77      yamt 	if (pgs_size > sizeof(pgs_onstack)) {
    571   1.77      yamt 		pgs = malloc(pgs_size, M_DEVBUF, M_NOWAIT | M_ZERO);
    572   1.78    simonb 		if (pgs == NULL) {
    573   1.78    simonb 			simple_unlock(&uobj->vmobjlock);
    574   1.78    simonb 			return (ENOMEM);
    575   1.78    simonb 		}
    576   1.77      yamt 	} else {
    577   1.77      yamt 		pgs = pgs_onstack;
    578   1.77      yamt 		memset(pgs, 0, pgs_size);
    579   1.77      yamt 	}
    580   1.63     enami 	UVMHIST_LOG(ubchist, "ridx %d npages %d startoff %ld endoff %ld",
    581   1.63     enami 	    ridx, npages, startoffset, endoffset);
    582   1.63     enami 	if (uvn_findpages(uobj, origoffset, &npages, &pgs[ridx],
    583   1.63     enami 	    async ? UFP_NOWAIT : UFP_ALL) != orignpages) {
    584   1.63     enami 		KASSERT(async != 0);
    585   1.63     enami 		genfs_rel_pages(&pgs[ridx], orignpages);
    586   1.63     enami 		simple_unlock(&uobj->vmobjlock);
    587   1.77      yamt 		if (pgs != pgs_onstack)
    588   1.77      yamt 			free(pgs, M_DEVBUF);
    589   1.63     enami 		return (EBUSY);
    590   1.63     enami 	}
    591   1.21       chs 
    592   1.21       chs 	/*
    593   1.21       chs 	 * if the pages are already resident, just return them.
    594   1.21       chs 	 */
    595   1.21       chs 
    596   1.21       chs 	for (i = 0; i < npages; i++) {
    597   1.97  christos 		struct vm_page *pg1 = pgs[ridx + i];
    598   1.21       chs 
    599   1.97  christos 		if ((pg1->flags & PG_FAKE) ||
    600  1.100      yamt 		    (blockalloc && (pg1->flags & PG_RDONLY))) {
    601   1.21       chs 			break;
    602   1.21       chs 		}
    603   1.21       chs 	}
    604   1.21       chs 	if (i == npages) {
    605   1.21       chs 		UVMHIST_LOG(ubchist, "returning cached pages", 0,0,0,0);
    606   1.21       chs 		raoffset = origoffset + (orignpages << PAGE_SHIFT);
    607   1.26       chs 		npages += ridx;
    608   1.21       chs 		goto raout;
    609   1.21       chs 	}
    610   1.21       chs 
    611   1.21       chs 	/*
    612   1.37       chs 	 * if PGO_OVERWRITE is set, don't bother reading the pages.
    613   1.37       chs 	 */
    614   1.37       chs 
    615   1.37       chs 	if (flags & PGO_OVERWRITE) {
    616   1.37       chs 		UVMHIST_LOG(ubchist, "PGO_OVERWRITE",0,0,0,0);
    617   1.37       chs 
    618   1.37       chs 		for (i = 0; i < npages; i++) {
    619   1.97  christos 			struct vm_page *pg1 = pgs[ridx + i];
    620   1.37       chs 
    621   1.97  christos 			pg1->flags &= ~(PG_RDONLY|PG_CLEAN);
    622   1.37       chs 		}
    623   1.37       chs 		npages += ridx;
    624   1.37       chs 		goto out;
    625   1.37       chs 	}
    626   1.37       chs 
    627   1.37       chs 	/*
    628   1.21       chs 	 * the page wasn't resident and we're not overwriting,
    629   1.21       chs 	 * so we're going to have to do some i/o.
    630   1.21       chs 	 * find any additional pages needed to cover the expanded range.
    631   1.21       chs 	 */
    632   1.21       chs 
    633   1.35       chs 	npages = (endoffset - startoffset) >> PAGE_SHIFT;
    634   1.35       chs 	if (startoffset != origoffset || npages != orignpages) {
    635   1.21       chs 
    636   1.21       chs 		/*
    637   1.37       chs 		 * we need to avoid deadlocks caused by locking
    638   1.21       chs 		 * additional pages at lower offsets than pages we
    639   1.37       chs 		 * already have locked.  unlock them all and start over.
    640   1.21       chs 		 */
    641   1.21       chs 
    642   1.63     enami 		genfs_rel_pages(&pgs[ridx], orignpages);
    643   1.77      yamt 		memset(pgs, 0, pgs_size);
    644   1.21       chs 
    645   1.21       chs 		UVMHIST_LOG(ubchist, "reset npages start 0x%x end 0x%x",
    646   1.53     enami 		    startoffset, endoffset, 0,0);
    647   1.21       chs 		npgs = npages;
    648   1.63     enami 		if (uvn_findpages(uobj, startoffset, &npgs, pgs,
    649   1.63     enami 		    async ? UFP_NOWAIT : UFP_ALL) != npages) {
    650   1.63     enami 			KASSERT(async != 0);
    651   1.63     enami 			genfs_rel_pages(pgs, npages);
    652   1.63     enami 			simple_unlock(&uobj->vmobjlock);
    653   1.77      yamt 			if (pgs != pgs_onstack)
    654   1.77      yamt 				free(pgs, M_DEVBUF);
    655   1.63     enami 			return (EBUSY);
    656   1.63     enami 		}
    657   1.21       chs 	}
    658   1.21       chs 	simple_unlock(&uobj->vmobjlock);
    659   1.21       chs 
    660   1.21       chs 	/*
    661   1.21       chs 	 * read the desired page(s).
    662   1.21       chs 	 */
    663   1.21       chs 
    664   1.21       chs 	totalbytes = npages << PAGE_SHIFT;
    665   1.30       chs 	bytes = MIN(totalbytes, MAX(diskeof - startoffset, 0));
    666   1.21       chs 	tailbytes = totalbytes - bytes;
    667   1.21       chs 	skipbytes = 0;
    668   1.21       chs 
    669   1.53     enami 	kva = uvm_pagermapin(pgs, npages,
    670   1.53     enami 	    UVMPAGER_MAPIN_READ | UVMPAGER_MAPIN_WAITOK);
    671   1.21       chs 
    672   1.21       chs 	s = splbio();
    673   1.21       chs 	mbp = pool_get(&bufpool, PR_WAITOK);
    674   1.21       chs 	splx(s);
    675   1.73   thorpej 	BUF_INIT(mbp);
    676   1.21       chs 	mbp->b_bufsize = totalbytes;
    677   1.21       chs 	mbp->b_data = (void *)kva;
    678   1.21       chs 	mbp->b_resid = mbp->b_bcount = bytes;
    679   1.65      fvdl 	mbp->b_flags = B_BUSY|B_READ| (async ? B_CALL|B_ASYNC : 0);
    680   1.37       chs 	mbp->b_iodone = (async ? uvm_aio_biodone : 0);
    681   1.21       chs 	mbp->b_vp = vp;
    682   1.21       chs 
    683   1.21       chs 	/*
    684   1.31       chs 	 * if EOF is in the middle of the range, zero the part past EOF.
    685   1.38       chs 	 * if the page including EOF is not PG_FAKE, skip over it since
    686   1.38       chs 	 * in that case it has valid data that we need to preserve.
    687   1.21       chs 	 */
    688   1.21       chs 
    689   1.31       chs 	if (tailbytes > 0) {
    690   1.38       chs 		size_t tailstart = bytes;
    691   1.38       chs 
    692   1.38       chs 		if ((pgs[bytes >> PAGE_SHIFT]->flags & PG_FAKE) == 0) {
    693   1.38       chs 			tailstart = round_page(tailstart);
    694   1.38       chs 			tailbytes -= tailstart - bytes;
    695   1.38       chs 		}
    696   1.37       chs 		UVMHIST_LOG(ubchist, "tailbytes %p 0x%x 0x%x",
    697   1.53     enami 		    kva, tailstart, tailbytes,0);
    698   1.38       chs 		memset((void *)(kva + tailstart), 0, tailbytes);
    699   1.21       chs 	}
    700   1.21       chs 
    701   1.21       chs 	/*
    702   1.21       chs 	 * now loop over the pages, reading as needed.
    703   1.21       chs 	 */
    704   1.21       chs 
    705  1.100      yamt 	if (blockalloc) {
    706   1.37       chs 		lockmgr(&gp->g_glock, LK_EXCLUSIVE, NULL);
    707   1.21       chs 	} else {
    708   1.37       chs 		lockmgr(&gp->g_glock, LK_SHARED, NULL);
    709   1.21       chs 	}
    710   1.21       chs 
    711   1.21       chs 	bp = NULL;
    712   1.21       chs 	for (offset = startoffset;
    713   1.53     enami 	    bytes > 0;
    714   1.53     enami 	    offset += iobytes, bytes -= iobytes) {
    715   1.21       chs 
    716   1.21       chs 		/*
    717   1.21       chs 		 * skip pages which don't need to be read.
    718   1.21       chs 		 */
    719   1.21       chs 
    720   1.21       chs 		pidx = (offset - startoffset) >> PAGE_SHIFT;
    721  1.100      yamt 		while ((pgs[pidx]->flags & PG_FAKE) == 0) {
    722   1.21       chs 			size_t b;
    723   1.21       chs 
    724   1.24       chs 			KASSERT((offset & (PAGE_SIZE - 1)) == 0);
    725  1.100      yamt 			if ((pgs[pidx]->flags & PG_RDONLY)) {
    726  1.100      yamt 				sawhole = TRUE;
    727  1.100      yamt 			}
    728   1.26       chs 			b = MIN(PAGE_SIZE, bytes);
    729   1.21       chs 			offset += b;
    730   1.21       chs 			bytes -= b;
    731   1.21       chs 			skipbytes += b;
    732   1.21       chs 			pidx++;
    733   1.21       chs 			UVMHIST_LOG(ubchist, "skipping, new offset 0x%x",
    734   1.53     enami 			    offset, 0,0,0);
    735   1.21       chs 			if (bytes == 0) {
    736   1.21       chs 				goto loopdone;
    737   1.21       chs 			}
    738   1.21       chs 		}
    739   1.21       chs 
    740   1.21       chs 		/*
    741   1.21       chs 		 * bmap the file to find out the blkno to read from and
    742   1.21       chs 		 * how much we can read in one i/o.  if bmap returns an error,
    743   1.21       chs 		 * skip the rest of the top-level i/o.
    744   1.21       chs 		 */
    745   1.21       chs 
    746   1.21       chs 		lbn = offset >> fs_bshift;
    747   1.36       chs 		error = VOP_BMAP(vp, lbn, &devvp, &blkno, &run);
    748   1.21       chs 		if (error) {
    749   1.21       chs 			UVMHIST_LOG(ubchist, "VOP_BMAP lbn 0x%x -> %d\n",
    750   1.53     enami 			    lbn, error,0,0);
    751   1.21       chs 			skipbytes += bytes;
    752   1.21       chs 			goto loopdone;
    753   1.21       chs 		}
    754   1.21       chs 
    755   1.21       chs 		/*
    756   1.21       chs 		 * see how many pages can be read with this i/o.
    757   1.21       chs 		 * reduce the i/o size if necessary to avoid
    758   1.21       chs 		 * overwriting pages with valid data.
    759   1.21       chs 		 */
    760   1.21       chs 
    761   1.26       chs 		iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
    762   1.26       chs 		    bytes);
    763   1.21       chs 		if (offset + iobytes > round_page(offset)) {
    764   1.21       chs 			pcount = 1;
    765   1.21       chs 			while (pidx + pcount < npages &&
    766   1.53     enami 			    pgs[pidx + pcount]->flags & PG_FAKE) {
    767   1.21       chs 				pcount++;
    768   1.21       chs 			}
    769   1.26       chs 			iobytes = MIN(iobytes, (pcount << PAGE_SHIFT) -
    770   1.53     enami 			    (offset - trunc_page(offset)));
    771   1.21       chs 		}
    772   1.21       chs 
    773   1.21       chs 		/*
    774   1.53     enami 		 * if this block isn't allocated, zero it instead of
    775  1.100      yamt 		 * reading it.  unless we are going to allocate blocks,
    776  1.100      yamt 		 * mark the pages we zeroed PG_RDONLY.
    777   1.21       chs 		 */
    778   1.21       chs 
    779   1.21       chs 		if (blkno < 0) {
    780   1.53     enami 			int holepages = (round_page(offset + iobytes) -
    781   1.53     enami 			    trunc_page(offset)) >> PAGE_SHIFT;
    782   1.21       chs 			UVMHIST_LOG(ubchist, "lbn 0x%x -> HOLE", lbn,0,0,0);
    783   1.21       chs 
    784   1.21       chs 			sawhole = TRUE;
    785   1.21       chs 			memset((char *)kva + (offset - startoffset), 0,
    786   1.53     enami 			    iobytes);
    787   1.21       chs 			skipbytes += iobytes;
    788   1.21       chs 
    789   1.35       chs 			for (i = 0; i < holepages; i++) {
    790   1.35       chs 				if (write) {
    791   1.35       chs 					pgs[pidx + i]->flags &= ~PG_CLEAN;
    792  1.100      yamt 				}
    793  1.100      yamt 				if (!blockalloc) {
    794   1.21       chs 					pgs[pidx + i]->flags |= PG_RDONLY;
    795   1.21       chs 				}
    796   1.21       chs 			}
    797   1.21       chs 			continue;
    798   1.21       chs 		}
    799   1.21       chs 
    800   1.21       chs 		/*
    801   1.21       chs 		 * allocate a sub-buf for this piece of the i/o
    802   1.21       chs 		 * (or just use mbp if there's only 1 piece),
    803   1.21       chs 		 * and start it going.
    804   1.21       chs 		 */
    805   1.21       chs 
    806   1.21       chs 		if (offset == startoffset && iobytes == bytes) {
    807   1.21       chs 			bp = mbp;
    808   1.21       chs 		} else {
    809   1.21       chs 			s = splbio();
    810   1.21       chs 			bp = pool_get(&bufpool, PR_WAITOK);
    811   1.21       chs 			splx(s);
    812   1.73   thorpej 			BUF_INIT(bp);
    813   1.21       chs 			bp->b_data = (char *)kva + offset - startoffset;
    814   1.21       chs 			bp->b_resid = bp->b_bcount = iobytes;
    815   1.67      yamt 			bp->b_flags = B_BUSY|B_READ|B_CALL|B_ASYNC;
    816   1.21       chs 			bp->b_iodone = uvm_aio_biodone1;
    817   1.21       chs 			bp->b_vp = vp;
    818   1.37       chs 			bp->b_proc = NULL;
    819   1.21       chs 		}
    820   1.21       chs 		bp->b_lblkno = 0;
    821   1.21       chs 		bp->b_private = mbp;
    822   1.21       chs 
    823   1.21       chs 		/* adjust physical blkno for partial blocks */
    824   1.25      fvdl 		bp->b_blkno = blkno + ((offset - ((off_t)lbn << fs_bshift)) >>
    825   1.53     enami 		    dev_bshift);
    826   1.21       chs 
    827   1.53     enami 		UVMHIST_LOG(ubchist,
    828   1.53     enami 		    "bp %p offset 0x%x bcount 0x%x blkno 0x%x",
    829   1.53     enami 		    bp, offset, iobytes, bp->b_blkno);
    830   1.21       chs 
    831   1.84      yamt 		if (async)
    832   1.84      yamt 			BIO_SETPRIO(bp, BPRIO_TIMELIMITED);
    833   1.84      yamt 		else
    834   1.84      yamt 			BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
    835  1.109      yamt 
    836  1.109      yamt 		VOP_STRATEGY(devvp, bp);
    837   1.21       chs 	}
    838   1.21       chs 
    839   1.21       chs loopdone:
    840   1.21       chs 	if (skipbytes) {
    841   1.21       chs 		s = splbio();
    842   1.21       chs 		if (error) {
    843   1.21       chs 			mbp->b_flags |= B_ERROR;
    844   1.21       chs 			mbp->b_error = error;
    845   1.21       chs 		}
    846   1.21       chs 		mbp->b_resid -= skipbytes;
    847   1.21       chs 		if (mbp->b_resid == 0) {
    848   1.21       chs 			biodone(mbp);
    849   1.21       chs 		}
    850   1.21       chs 		splx(s);
    851   1.21       chs 	}
    852   1.21       chs 
    853   1.21       chs 	if (async) {
    854   1.32       chs 		UVMHIST_LOG(ubchist, "returning 0 (async)",0,0,0,0);
    855   1.37       chs 		lockmgr(&gp->g_glock, LK_RELEASE, NULL);
    856   1.77      yamt 		if (pgs != pgs_onstack)
    857   1.77      yamt 			free(pgs, M_DEVBUF);
    858   1.53     enami 		return (0);
    859   1.21       chs 	}
    860   1.21       chs 	if (bp != NULL) {
    861   1.21       chs 		error = biowait(mbp);
    862   1.21       chs 	}
    863   1.21       chs 	s = splbio();
    864   1.21       chs 	pool_put(&bufpool, mbp);
    865   1.21       chs 	splx(s);
    866   1.21       chs 	uvm_pagermapout(kva, npages);
    867   1.24       chs 	raoffset = startoffset + totalbytes;
    868   1.21       chs 
    869   1.21       chs 	/*
    870   1.21       chs 	 * if this we encountered a hole then we have to do a little more work.
    871   1.21       chs 	 * for read faults, we marked the page PG_RDONLY so that future
    872   1.21       chs 	 * write accesses to the page will fault again.
    873   1.21       chs 	 * for write faults, we must make sure that the backing store for
    874   1.21       chs 	 * the page is completely allocated while the pages are locked.
    875   1.21       chs 	 */
    876   1.21       chs 
    877  1.100      yamt 	if (!error && sawhole && blockalloc) {
    878   1.37       chs 		error = GOP_ALLOC(vp, startoffset, npages << PAGE_SHIFT, 0,
    879   1.53     enami 		    cred);
    880   1.37       chs 		UVMHIST_LOG(ubchist, "gop_alloc off 0x%x/0x%x -> %d",
    881   1.37       chs 		    startoffset, npages << PAGE_SHIFT, error,0);
    882  1.100      yamt 		if (!error) {
    883  1.100      yamt 			for (i = 0; i < npages; i++) {
    884  1.100      yamt 				if (pgs[i] == NULL) {
    885  1.100      yamt 					continue;
    886  1.100      yamt 				}
    887  1.100      yamt 				pgs[i]->flags &= ~(PG_CLEAN|PG_RDONLY);
    888  1.100      yamt 				UVMHIST_LOG(ubchist, "mark dirty pg %p",
    889  1.100      yamt 				    pgs[i],0,0,0);
    890  1.100      yamt 			}
    891  1.100      yamt 		}
    892   1.21       chs 	}
    893   1.37       chs 	lockmgr(&gp->g_glock, LK_RELEASE, NULL);
    894   1.21       chs 	simple_lock(&uobj->vmobjlock);
    895   1.21       chs 
    896   1.21       chs 	/*
    897   1.21       chs 	 * see if we want to start any readahead.
    898   1.21       chs 	 * XXXUBC for now, just read the next 128k on 64k boundaries.
    899   1.21       chs 	 * this is pretty nonsensical, but it is 50% faster than reading
    900   1.21       chs 	 * just the next 64k.
    901   1.21       chs 	 */
    902   1.21       chs 
    903   1.21       chs raout:
    904   1.24       chs 	if (!error && !async && !write && ((int)raoffset & 0xffff) == 0 &&
    905   1.21       chs 	    PAGE_SHIFT <= 16) {
    906   1.41  christos 		off_t rasize;
    907   1.97  christos 		int rapages, err, j, skipped;
    908   1.21       chs 
    909   1.41  christos 		/* XXXUBC temp limit, from above */
    910   1.63     enami 		rapages = MIN(MIN(1 << (16 - PAGE_SHIFT), MAX_READ_AHEAD),
    911   1.63     enami 		    genfs_rapages);
    912   1.63     enami 		rasize = rapages << PAGE_SHIFT;
    913   1.97  christos 		for (j = skipped = 0; j < genfs_racount; j++) {
    914   1.93  drochner 
    915   1.93  drochner 			if (raoffset >= memeof)
    916   1.93  drochner 				break;
    917   1.93  drochner 
    918   1.63     enami 			err = VOP_GETPAGES(vp, raoffset, NULL, &rapages, 0,
    919  1.103      yamt 			    VM_PROT_READ, 0, PGO_NOTIMESTAMP);
    920   1.63     enami 			simple_lock(&uobj->vmobjlock);
    921   1.63     enami 			if (err) {
    922   1.63     enami 				if (err != EBUSY ||
    923   1.63     enami 				    skipped++ == genfs_raskip)
    924   1.63     enami 					break;
    925   1.63     enami 			}
    926   1.63     enami 			raoffset += rasize;
    927   1.63     enami 			rapages = rasize >> PAGE_SHIFT;
    928   1.63     enami 		}
    929   1.21       chs 	}
    930   1.21       chs 
    931   1.21       chs 	/*
    932   1.21       chs 	 * we're almost done!  release the pages...
    933   1.21       chs 	 * for errors, we free the pages.
    934   1.21       chs 	 * otherwise we activate them and mark them as valid and clean.
    935   1.21       chs 	 * also, unbusy pages that were not actually requested.
    936   1.21       chs 	 */
    937   1.21       chs 
    938   1.21       chs 	if (error) {
    939   1.21       chs 		for (i = 0; i < npages; i++) {
    940   1.21       chs 			if (pgs[i] == NULL) {
    941   1.21       chs 				continue;
    942   1.21       chs 			}
    943   1.21       chs 			UVMHIST_LOG(ubchist, "examining pg %p flags 0x%x",
    944   1.53     enami 			    pgs[i], pgs[i]->flags, 0,0);
    945   1.26       chs 			if (pgs[i]->flags & PG_FAKE) {
    946   1.37       chs 				pgs[i]->flags |= PG_RELEASED;
    947   1.21       chs 			}
    948   1.21       chs 		}
    949   1.37       chs 		uvm_lock_pageq();
    950   1.37       chs 		uvm_page_unbusy(pgs, npages);
    951   1.21       chs 		uvm_unlock_pageq();
    952   1.21       chs 		simple_unlock(&uobj->vmobjlock);
    953   1.21       chs 		UVMHIST_LOG(ubchist, "returning error %d", error,0,0,0);
    954   1.77      yamt 		if (pgs != pgs_onstack)
    955   1.77      yamt 			free(pgs, M_DEVBUF);
    956   1.53     enami 		return (error);
    957   1.21       chs 	}
    958   1.21       chs 
    959   1.37       chs out:
    960   1.21       chs 	UVMHIST_LOG(ubchist, "succeeding, npages %d", npages,0,0,0);
    961   1.26       chs 	uvm_lock_pageq();
    962   1.21       chs 	for (i = 0; i < npages; i++) {
    963   1.37       chs 		pg = pgs[i];
    964   1.37       chs 		if (pg == NULL) {
    965   1.21       chs 			continue;
    966   1.21       chs 		}
    967   1.21       chs 		UVMHIST_LOG(ubchist, "examining pg %p flags 0x%x",
    968   1.53     enami 		    pg, pg->flags, 0,0);
    969   1.37       chs 		if (pg->flags & PG_FAKE && !overwrite) {
    970   1.37       chs 			pg->flags &= ~(PG_FAKE);
    971   1.21       chs 			pmap_clear_modify(pgs[i]);
    972   1.21       chs 		}
    973  1.100      yamt 		KASSERT(!write || !blockalloc || (pg->flags & PG_RDONLY) == 0);
    974   1.21       chs 		if (i < ridx || i >= ridx + orignpages || async) {
    975   1.21       chs 			UVMHIST_LOG(ubchist, "unbusy pg %p offset 0x%x",
    976   1.53     enami 			    pg, pg->offset,0,0);
    977   1.37       chs 			if (pg->flags & PG_WANTED) {
    978   1.37       chs 				wakeup(pg);
    979   1.37       chs 			}
    980   1.37       chs 			if (pg->flags & PG_FAKE) {
    981   1.37       chs 				KASSERT(overwrite);
    982   1.37       chs 				uvm_pagezero(pg);
    983   1.37       chs 			}
    984   1.37       chs 			if (pg->flags & PG_RELEASED) {
    985   1.37       chs 				uvm_pagefree(pg);
    986   1.26       chs 				continue;
    987   1.21       chs 			}
    988   1.37       chs 			uvm_pageactivate(pg);
    989   1.37       chs 			pg->flags &= ~(PG_WANTED|PG_BUSY|PG_FAKE);
    990   1.37       chs 			UVM_PAGE_OWN(pg, NULL);
    991   1.21       chs 		}
    992   1.21       chs 	}
    993   1.26       chs 	uvm_unlock_pageq();
    994   1.21       chs 	simple_unlock(&uobj->vmobjlock);
    995   1.21       chs 	if (ap->a_m != NULL) {
    996   1.21       chs 		memcpy(ap->a_m, &pgs[ridx],
    997   1.53     enami 		    orignpages * sizeof(struct vm_page *));
    998   1.21       chs 	}
    999   1.77      yamt 	if (pgs != pgs_onstack)
   1000   1.77      yamt 		free(pgs, M_DEVBUF);
   1001   1.53     enami 	return (0);
   1002   1.21       chs }
   1003   1.21       chs 
   1004   1.21       chs /*
   1005   1.21       chs  * generic VM putpages routine.
   1006   1.21       chs  * Write the given range of pages to backing store.
   1007   1.37       chs  *
   1008   1.37       chs  * => "offhi == 0" means flush all pages at or after "offlo".
   1009   1.37       chs  * => object should be locked by caller.   we may _unlock_ the object
   1010   1.37       chs  *	if (and only if) we need to clean a page (PGO_CLEANIT), or
   1011   1.37       chs  *	if PGO_SYNCIO is set and there are pages busy.
   1012   1.37       chs  *	we return with the object locked.
   1013   1.37       chs  * => if PGO_CLEANIT or PGO_SYNCIO is set, we may block (due to I/O).
   1014   1.37       chs  *	thus, a caller might want to unlock higher level resources
   1015   1.37       chs  *	(e.g. vm_map) before calling flush.
   1016   1.37       chs  * => if neither PGO_CLEANIT nor PGO_SYNCIO is set, then we will neither
   1017   1.37       chs  *	unlock the object nor block.
   1018   1.37       chs  * => if PGO_ALLPAGES is set, then all pages in the object will be processed.
   1019   1.37       chs  * => NOTE: we rely on the fact that the object's memq is a TAILQ and
   1020   1.37       chs  *	that new pages are inserted on the tail end of the list.   thus,
   1021   1.37       chs  *	we can make a complete pass through the object in one go by starting
   1022   1.37       chs  *	at the head and working towards the tail (new pages are put in
   1023   1.37       chs  *	front of us).
   1024   1.37       chs  * => NOTE: we are allowed to lock the page queues, so the caller
   1025   1.37       chs  *	must not be holding the page queue lock.
   1026   1.37       chs  *
   1027   1.37       chs  * note on "cleaning" object and PG_BUSY pages:
   1028   1.37       chs  *	this routine is holding the lock on the object.   the only time
   1029   1.37       chs  *	that it can run into a PG_BUSY page that it does not own is if
   1030   1.37       chs  *	some other process has started I/O on the page (e.g. either
   1031   1.37       chs  *	a pagein, or a pageout).    if the PG_BUSY page is being paged
   1032   1.37       chs  *	in, then it can not be dirty (!PG_CLEAN) because no one has
   1033   1.37       chs  *	had a chance to modify it yet.    if the PG_BUSY page is being
   1034   1.37       chs  *	paged out then it means that someone else has already started
   1035   1.53     enami  *	cleaning the page for us (how nice!).    in this case, if we
   1036   1.37       chs  *	have syncio specified, then after we make our pass through the
   1037   1.53     enami  *	object we need to wait for the other PG_BUSY pages to clear
   1038   1.37       chs  *	off (i.e. we need to do an iosync).   also note that once a
   1039   1.37       chs  *	page is PG_BUSY it must stay in its object until it is un-busyed.
   1040   1.37       chs  *
   1041   1.37       chs  * note on page traversal:
   1042   1.37       chs  *	we can traverse the pages in an object either by going down the
   1043   1.37       chs  *	linked list in "uobj->memq", or we can go over the address range
   1044   1.37       chs  *	by page doing hash table lookups for each address.    depending
   1045   1.53     enami  *	on how many pages are in the object it may be cheaper to do one
   1046   1.37       chs  *	or the other.   we set "by_list" to true if we are using memq.
   1047   1.37       chs  *	if the cost of a hash lookup was equal to the cost of the list
   1048   1.37       chs  *	traversal we could compare the number of pages in the start->stop
   1049   1.37       chs  *	range to the total number of pages in the object.   however, it
   1050   1.37       chs  *	seems that a hash table lookup is more expensive than the linked
   1051   1.53     enami  *	list traversal, so we multiply the number of pages in the
   1052   1.37       chs  *	range by an estimate of the relatively higher cost of the hash lookup.
   1053   1.21       chs  */
   1054   1.21       chs 
   1055   1.21       chs int
   1056   1.53     enami genfs_putpages(void *v)
   1057   1.21       chs {
   1058   1.21       chs 	struct vop_putpages_args /* {
   1059   1.21       chs 		struct vnode *a_vp;
   1060   1.37       chs 		voff_t a_offlo;
   1061   1.37       chs 		voff_t a_offhi;
   1062   1.21       chs 		int a_flags;
   1063   1.21       chs 	} */ *ap = v;
   1064   1.37       chs 	struct vnode *vp = ap->a_vp;
   1065   1.37       chs 	struct uvm_object *uobj = &vp->v_uobj;
   1066   1.46       chs 	struct simplelock *slock = &uobj->vmobjlock;
   1067   1.37       chs 	off_t startoff = ap->a_offlo;
   1068   1.37       chs 	off_t endoff = ap->a_offhi;
   1069   1.37       chs 	off_t off;
   1070   1.37       chs 	int flags = ap->a_flags;
   1071   1.76       tls 	/* Even for strange MAXPHYS, the shift rounds down to a page */
   1072   1.76       tls 	const int maxpages = MAXPHYS >> PAGE_SHIFT;
   1073   1.37       chs 	int i, s, error, npages, nback;
   1074   1.37       chs 	int freeflag;
   1075   1.60     enami 	struct vm_page *pgs[maxpages], *pg, *nextpg, *tpg, curmp, endmp;
   1076   1.97  christos 	boolean_t wasclean, by_list, needs_clean, yld;
   1077   1.37       chs 	boolean_t async = (flags & PGO_SYNCIO) == 0;
   1078   1.56     enami 	boolean_t pagedaemon = curproc == uvm.pagedaemon_proc;
   1079   1.70  christos 	struct lwp *l = curlwp ? curlwp : &lwp0;
   1080  1.101      yamt 	struct genfs_node *gp = VTOG(vp);
   1081  1.101      yamt 	int dirtygen;
   1082  1.103      yamt 	boolean_t modified = FALSE;
   1083  1.104      yamt 	boolean_t cleanall;
   1084   1.70  christos 
   1085   1.37       chs 	UVMHIST_FUNC("genfs_putpages"); UVMHIST_CALLED(ubchist);
   1086   1.37       chs 
   1087   1.37       chs 	KASSERT(flags & (PGO_CLEANIT|PGO_FREE|PGO_DEACTIVATE));
   1088   1.37       chs 	KASSERT((startoff & PAGE_MASK) == 0 && (endoff & PAGE_MASK) == 0);
   1089   1.37       chs 	KASSERT(startoff < endoff || endoff == 0);
   1090   1.37       chs 
   1091   1.37       chs 	UVMHIST_LOG(ubchist, "vp %p pages %d off 0x%x len 0x%x",
   1092   1.37       chs 	    vp, uobj->uo_npages, startoff, endoff - startoff);
   1093  1.103      yamt 
   1094  1.103      yamt 	KASSERT((vp->v_flag & VONWORKLST) != 0 ||
   1095  1.103      yamt 	    (vp->v_flag & VWRITEMAPDIRTY) == 0);
   1096   1.37       chs 	if (uobj->uo_npages == 0) {
   1097   1.62  perseant 		s = splbio();
   1098  1.103      yamt 		if (vp->v_flag & VONWORKLST) {
   1099  1.103      yamt 			vp->v_flag &= ~VWRITEMAPDIRTY;
   1100  1.103      yamt 			if (LIST_FIRST(&vp->v_dirtyblkhd) == NULL) {
   1101  1.103      yamt 				vp->v_flag &= ~VONWORKLST;
   1102  1.103      yamt 				LIST_REMOVE(vp, v_synclist);
   1103  1.103      yamt 			}
   1104   1.37       chs 		}
   1105   1.62  perseant 		splx(s);
   1106   1.46       chs 		simple_unlock(slock);
   1107   1.53     enami 		return (0);
   1108   1.37       chs 	}
   1109   1.37       chs 
   1110   1.37       chs 	/*
   1111   1.37       chs 	 * the vnode has pages, set up to process the request.
   1112   1.37       chs 	 */
   1113   1.37       chs 
   1114   1.37       chs 	error = 0;
   1115   1.44       chs 	s = splbio();
   1116   1.71        pk 	simple_lock(&global_v_numoutput_slock);
   1117   1.44       chs 	wasclean = (vp->v_numoutput == 0);
   1118   1.71        pk 	simple_unlock(&global_v_numoutput_slock);
   1119   1.44       chs 	splx(s);
   1120   1.37       chs 	off = startoff;
   1121   1.37       chs 	if (endoff == 0 || flags & PGO_ALLPAGES) {
   1122   1.37       chs 		endoff = trunc_page(LLONG_MAX);
   1123   1.37       chs 	}
   1124   1.37       chs 	by_list = (uobj->uo_npages <=
   1125   1.37       chs 	    ((endoff - startoff) >> PAGE_SHIFT) * UVM_PAGE_HASH_PENALTY);
   1126   1.37       chs 
   1127  1.102      yamt #if !defined(DEBUG)
   1128  1.102      yamt 	/*
   1129  1.102      yamt 	 * if this vnode is known not to have dirty pages,
   1130  1.102      yamt 	 * don't bother to clean it out.
   1131  1.102      yamt 	 */
   1132  1.102      yamt 
   1133  1.102      yamt 	if ((vp->v_flag & VONWORKLST) == 0) {
   1134  1.102      yamt 		if ((flags & (PGO_FREE|PGO_DEACTIVATE)) == 0) {
   1135  1.102      yamt 			goto skip_scan;
   1136  1.102      yamt 		}
   1137  1.102      yamt 		flags &= ~PGO_CLEANIT;
   1138  1.102      yamt 	}
   1139  1.102      yamt #endif /* !defined(DEBUG) */
   1140  1.102      yamt 
   1141   1.37       chs 	/*
   1142   1.37       chs 	 * start the loop.  when scanning by list, hold the last page
   1143   1.37       chs 	 * in the list before we start.  pages allocated after we start
   1144   1.37       chs 	 * will be added to the end of the list, so we can stop at the
   1145   1.37       chs 	 * current last page.
   1146   1.37       chs 	 */
   1147   1.37       chs 
   1148  1.104      yamt 	cleanall = (flags & PGO_CLEANIT) != 0 && wasclean &&
   1149  1.104      yamt 	    startoff == 0 && endoff == trunc_page(LLONG_MAX) &&
   1150  1.104      yamt 	    (vp->v_flag & VONWORKLST) != 0;
   1151  1.101      yamt 	dirtygen = gp->g_dirtygen;
   1152   1.56     enami 	freeflag = pagedaemon ? PG_PAGEOUT : PG_RELEASED;
   1153   1.37       chs 	curmp.uobject = uobj;
   1154   1.37       chs 	curmp.offset = (voff_t)-1;
   1155   1.37       chs 	curmp.flags = PG_BUSY;
   1156   1.37       chs 	endmp.uobject = uobj;
   1157   1.37       chs 	endmp.offset = (voff_t)-1;
   1158   1.37       chs 	endmp.flags = PG_BUSY;
   1159   1.37       chs 	if (by_list) {
   1160   1.37       chs 		pg = TAILQ_FIRST(&uobj->memq);
   1161   1.37       chs 		TAILQ_INSERT_TAIL(&uobj->memq, &endmp, listq);
   1162   1.70  christos 		PHOLD(l);
   1163   1.37       chs 	} else {
   1164   1.37       chs 		pg = uvm_pagelookup(uobj, off);
   1165   1.37       chs 	}
   1166   1.37       chs 	nextpg = NULL;
   1167   1.37       chs 	while (by_list || off < endoff) {
   1168   1.37       chs 
   1169   1.37       chs 		/*
   1170   1.37       chs 		 * if the current page is not interesting, move on to the next.
   1171   1.37       chs 		 */
   1172   1.37       chs 
   1173   1.37       chs 		KASSERT(pg == NULL || pg->uobject == uobj);
   1174   1.37       chs 		KASSERT(pg == NULL ||
   1175   1.53     enami 		    (pg->flags & (PG_RELEASED|PG_PAGEOUT)) == 0 ||
   1176   1.53     enami 		    (pg->flags & PG_BUSY) != 0);
   1177   1.37       chs 		if (by_list) {
   1178   1.37       chs 			if (pg == &endmp) {
   1179   1.37       chs 				break;
   1180   1.37       chs 			}
   1181   1.37       chs 			if (pg->offset < startoff || pg->offset >= endoff ||
   1182   1.37       chs 			    pg->flags & (PG_RELEASED|PG_PAGEOUT)) {
   1183  1.101      yamt 				if (pg->flags & (PG_RELEASED|PG_PAGEOUT)) {
   1184  1.101      yamt 					wasclean = FALSE;
   1185  1.101      yamt 				}
   1186   1.37       chs 				pg = TAILQ_NEXT(pg, listq);
   1187   1.37       chs 				continue;
   1188   1.37       chs 			}
   1189   1.37       chs 			off = pg->offset;
   1190  1.101      yamt 		} else if (pg == NULL || pg->flags & (PG_RELEASED|PG_PAGEOUT)) {
   1191  1.101      yamt 			if (pg != NULL) {
   1192  1.101      yamt 				wasclean = FALSE;
   1193  1.101      yamt 			}
   1194   1.37       chs 			off += PAGE_SIZE;
   1195   1.37       chs 			if (off < endoff) {
   1196   1.37       chs 				pg = uvm_pagelookup(uobj, off);
   1197   1.37       chs 			}
   1198   1.37       chs 			continue;
   1199   1.37       chs 		}
   1200   1.21       chs 
   1201   1.37       chs 		/*
   1202   1.37       chs 		 * if the current page needs to be cleaned and it's busy,
   1203   1.37       chs 		 * wait for it to become unbusy.
   1204   1.37       chs 		 */
   1205   1.37       chs 
   1206   1.97  christos 		yld = (l->l_cpu->ci_schedstate.spc_flags &
   1207   1.56     enami 		    SPCF_SHOULDYIELD) && !pagedaemon;
   1208   1.97  christos 		if (pg->flags & PG_BUSY || yld) {
   1209   1.72  perseant 			UVMHIST_LOG(ubchist, "busy %p", pg,0,0,0);
   1210   1.72  perseant 			if (flags & PGO_BUSYFAIL && pg->flags & PG_BUSY) {
   1211   1.72  perseant 				UVMHIST_LOG(ubchist, "busyfail %p", pg, 0,0,0);
   1212   1.72  perseant 				error = EDEADLK;
   1213   1.72  perseant 				break;
   1214   1.72  perseant 			}
   1215   1.56     enami 			KASSERT(!pagedaemon);
   1216   1.37       chs 			if (by_list) {
   1217   1.37       chs 				TAILQ_INSERT_BEFORE(pg, &curmp, listq);
   1218   1.37       chs 				UVMHIST_LOG(ubchist, "curmp next %p",
   1219   1.53     enami 				    TAILQ_NEXT(&curmp, listq), 0,0,0);
   1220   1.37       chs 			}
   1221   1.97  christos 			if (yld) {
   1222   1.49       chs 				simple_unlock(slock);
   1223   1.69   thorpej 				preempt(1);
   1224   1.49       chs 				simple_lock(slock);
   1225   1.49       chs 			} else {
   1226   1.49       chs 				pg->flags |= PG_WANTED;
   1227   1.49       chs 				UVM_UNLOCK_AND_WAIT(pg, slock, 0, "genput", 0);
   1228   1.49       chs 				simple_lock(slock);
   1229   1.49       chs 			}
   1230   1.37       chs 			if (by_list) {
   1231   1.37       chs 				UVMHIST_LOG(ubchist, "after next %p",
   1232   1.53     enami 				    TAILQ_NEXT(&curmp, listq), 0,0,0);
   1233   1.37       chs 				pg = TAILQ_NEXT(&curmp, listq);
   1234   1.37       chs 				TAILQ_REMOVE(&uobj->memq, &curmp, listq);
   1235   1.37       chs 			} else {
   1236   1.37       chs 				pg = uvm_pagelookup(uobj, off);
   1237   1.37       chs 			}
   1238   1.37       chs 			continue;
   1239   1.49       chs 		}
   1240   1.49       chs 
   1241   1.49       chs 		/*
   1242   1.49       chs 		 * if we're freeing, remove all mappings of the page now.
   1243   1.49       chs 		 * if we're cleaning, check if the page is needs to be cleaned.
   1244   1.49       chs 		 */
   1245   1.49       chs 
   1246   1.49       chs 		if (flags & PGO_FREE) {
   1247   1.49       chs 			pmap_page_protect(pg, VM_PROT_NONE);
   1248  1.101      yamt 		} else if (flags & PGO_CLEANIT) {
   1249  1.101      yamt 
   1250  1.101      yamt 			/*
   1251  1.101      yamt 			 * if we still have some hope to pull this vnode off
   1252  1.101      yamt 			 * from the syncer queue, write-protect the page.
   1253  1.101      yamt 			 */
   1254  1.101      yamt 
   1255  1.104      yamt 			if (cleanall && wasclean &&
   1256  1.104      yamt 			    gp->g_dirtygen == dirtygen) {
   1257  1.104      yamt 
   1258  1.104      yamt 				/*
   1259  1.104      yamt 				 * uobj pages get wired only by uvm_fault
   1260  1.104      yamt 				 * where uobj is locked.
   1261  1.104      yamt 				 */
   1262  1.104      yamt 
   1263  1.104      yamt 				if (pg->wire_count == 0) {
   1264  1.104      yamt 					pmap_page_protect(pg,
   1265  1.104      yamt 					    VM_PROT_READ|VM_PROT_EXECUTE);
   1266  1.104      yamt 				} else {
   1267  1.104      yamt 					cleanall = FALSE;
   1268  1.104      yamt 				}
   1269  1.101      yamt 			}
   1270   1.49       chs 		}
   1271  1.101      yamt 
   1272   1.49       chs 		if (flags & PGO_CLEANIT) {
   1273   1.49       chs 			needs_clean = pmap_clear_modify(pg) ||
   1274   1.53     enami 			    (pg->flags & PG_CLEAN) == 0;
   1275   1.49       chs 			pg->flags |= PG_CLEAN;
   1276   1.49       chs 		} else {
   1277   1.49       chs 			needs_clean = FALSE;
   1278   1.37       chs 		}
   1279   1.37       chs 
   1280   1.37       chs 		/*
   1281   1.37       chs 		 * if we're cleaning, build a cluster.
   1282   1.37       chs 		 * the cluster will consist of pages which are currently dirty,
   1283   1.37       chs 		 * but they will be returned to us marked clean.
   1284   1.37       chs 		 * if not cleaning, just operate on the one page.
   1285   1.37       chs 		 */
   1286   1.37       chs 
   1287   1.37       chs 		if (needs_clean) {
   1288  1.101      yamt 			KDASSERT((vp->v_flag & VONWORKLST));
   1289   1.37       chs 			wasclean = FALSE;
   1290   1.37       chs 			memset(pgs, 0, sizeof(pgs));
   1291   1.37       chs 			pg->flags |= PG_BUSY;
   1292   1.37       chs 			UVM_PAGE_OWN(pg, "genfs_putpages");
   1293   1.37       chs 
   1294   1.37       chs 			/*
   1295   1.37       chs 			 * first look backward.
   1296   1.37       chs 			 */
   1297   1.37       chs 
   1298   1.60     enami 			npages = MIN(maxpages >> 1, off >> PAGE_SHIFT);
   1299   1.37       chs 			nback = npages;
   1300   1.37       chs 			uvn_findpages(uobj, off - PAGE_SIZE, &nback, &pgs[0],
   1301   1.37       chs 			    UFP_NOWAIT|UFP_NOALLOC|UFP_DIRTYONLY|UFP_BACKWARD);
   1302   1.37       chs 			if (nback) {
   1303   1.37       chs 				memmove(&pgs[0], &pgs[npages - nback],
   1304   1.37       chs 				    nback * sizeof(pgs[0]));
   1305   1.47     enami 				if (npages - nback < nback)
   1306   1.47     enami 					memset(&pgs[nback], 0,
   1307   1.47     enami 					    (npages - nback) * sizeof(pgs[0]));
   1308   1.47     enami 				else
   1309   1.47     enami 					memset(&pgs[npages - nback], 0,
   1310   1.47     enami 					    nback * sizeof(pgs[0]));
   1311   1.37       chs 			}
   1312   1.37       chs 
   1313   1.37       chs 			/*
   1314   1.37       chs 			 * then plug in our page of interest.
   1315   1.37       chs 			 */
   1316   1.37       chs 
   1317   1.37       chs 			pgs[nback] = pg;
   1318   1.37       chs 
   1319   1.37       chs 			/*
   1320   1.37       chs 			 * then look forward to fill in the remaining space in
   1321   1.37       chs 			 * the array of pages.
   1322   1.37       chs 			 */
   1323   1.37       chs 
   1324   1.60     enami 			npages = maxpages - nback - 1;
   1325   1.37       chs 			uvn_findpages(uobj, off + PAGE_SIZE, &npages,
   1326   1.37       chs 			    &pgs[nback + 1],
   1327   1.37       chs 			    UFP_NOWAIT|UFP_NOALLOC|UFP_DIRTYONLY);
   1328   1.37       chs 			npages += nback + 1;
   1329   1.37       chs 		} else {
   1330   1.37       chs 			pgs[0] = pg;
   1331   1.37       chs 			npages = 1;
   1332   1.61     enami 			nback = 0;
   1333   1.37       chs 		}
   1334   1.37       chs 
   1335   1.37       chs 		/*
   1336   1.37       chs 		 * apply FREE or DEACTIVATE options if requested.
   1337   1.37       chs 		 */
   1338   1.37       chs 
   1339   1.37       chs 		if (flags & (PGO_DEACTIVATE|PGO_FREE)) {
   1340   1.37       chs 			uvm_lock_pageq();
   1341   1.37       chs 		}
   1342   1.37       chs 		for (i = 0; i < npages; i++) {
   1343   1.37       chs 			tpg = pgs[i];
   1344   1.37       chs 			KASSERT(tpg->uobject == uobj);
   1345   1.59     enami 			if (by_list && tpg == TAILQ_NEXT(pg, listq))
   1346   1.59     enami 				pg = tpg;
   1347   1.91     enami 			if (tpg->offset < startoff || tpg->offset >= endoff)
   1348   1.91     enami 				continue;
   1349   1.37       chs 			if (flags & PGO_DEACTIVATE &&
   1350   1.37       chs 			    (tpg->pqflags & PQ_INACTIVE) == 0 &&
   1351   1.37       chs 			    tpg->wire_count == 0) {
   1352   1.37       chs 				(void) pmap_clear_reference(tpg);
   1353   1.37       chs 				uvm_pagedeactivate(tpg);
   1354   1.37       chs 			} else if (flags & PGO_FREE) {
   1355   1.37       chs 				pmap_page_protect(tpg, VM_PROT_NONE);
   1356   1.37       chs 				if (tpg->flags & PG_BUSY) {
   1357   1.37       chs 					tpg->flags |= freeflag;
   1358   1.56     enami 					if (pagedaemon) {
   1359   1.37       chs 						uvmexp.paging++;
   1360   1.37       chs 						uvm_pagedequeue(tpg);
   1361   1.37       chs 					}
   1362   1.37       chs 				} else {
   1363   1.59     enami 
   1364   1.59     enami 					/*
   1365   1.59     enami 					 * ``page is not busy''
   1366   1.59     enami 					 * implies that npages is 1
   1367   1.59     enami 					 * and needs_clean is false.
   1368   1.59     enami 					 */
   1369   1.59     enami 
   1370   1.37       chs 					nextpg = TAILQ_NEXT(tpg, listq);
   1371   1.37       chs 					uvm_pagefree(tpg);
   1372   1.89     enami 					if (pagedaemon)
   1373   1.89     enami 						uvmexp.pdfreed++;
   1374   1.37       chs 				}
   1375   1.37       chs 			}
   1376   1.37       chs 		}
   1377   1.37       chs 		if (flags & (PGO_DEACTIVATE|PGO_FREE)) {
   1378   1.37       chs 			uvm_unlock_pageq();
   1379   1.37       chs 		}
   1380   1.37       chs 		if (needs_clean) {
   1381  1.103      yamt 			modified = TRUE;
   1382   1.37       chs 
   1383   1.37       chs 			/*
   1384   1.37       chs 			 * start the i/o.  if we're traversing by list,
   1385   1.37       chs 			 * keep our place in the list with a marker page.
   1386   1.37       chs 			 */
   1387   1.37       chs 
   1388   1.37       chs 			if (by_list) {
   1389   1.37       chs 				TAILQ_INSERT_AFTER(&uobj->memq, pg, &curmp,
   1390   1.37       chs 				    listq);
   1391   1.37       chs 			}
   1392   1.46       chs 			simple_unlock(slock);
   1393   1.37       chs 			error = GOP_WRITE(vp, pgs, npages, flags);
   1394   1.46       chs 			simple_lock(slock);
   1395   1.37       chs 			if (by_list) {
   1396   1.37       chs 				pg = TAILQ_NEXT(&curmp, listq);
   1397   1.37       chs 				TAILQ_REMOVE(&uobj->memq, &curmp, listq);
   1398   1.37       chs 			}
   1399   1.37       chs 			if (error) {
   1400   1.37       chs 				break;
   1401   1.37       chs 			}
   1402   1.37       chs 			if (by_list) {
   1403   1.37       chs 				continue;
   1404   1.37       chs 			}
   1405   1.37       chs 		}
   1406   1.37       chs 
   1407   1.37       chs 		/*
   1408   1.37       chs 		 * find the next page and continue if there was no error.
   1409   1.37       chs 		 */
   1410   1.37       chs 
   1411   1.37       chs 		if (by_list) {
   1412   1.37       chs 			if (nextpg) {
   1413   1.37       chs 				pg = nextpg;
   1414   1.37       chs 				nextpg = NULL;
   1415   1.37       chs 			} else {
   1416   1.37       chs 				pg = TAILQ_NEXT(pg, listq);
   1417   1.37       chs 			}
   1418   1.37       chs 		} else {
   1419   1.61     enami 			off += (npages - nback) << PAGE_SHIFT;
   1420   1.37       chs 			if (off < endoff) {
   1421   1.37       chs 				pg = uvm_pagelookup(uobj, off);
   1422   1.37       chs 			}
   1423   1.37       chs 		}
   1424   1.37       chs 	}
   1425   1.37       chs 	if (by_list) {
   1426   1.37       chs 		TAILQ_REMOVE(&uobj->memq, &endmp, listq);
   1427   1.70  christos 		PRELE(l);
   1428   1.37       chs 	}
   1429   1.37       chs 
   1430  1.103      yamt 	if (modified && (vp->v_flag & VWRITEMAPDIRTY) != 0 &&
   1431  1.103      yamt 	    (vp->v_type == VREG ||
   1432  1.103      yamt 	    (vp->v_mount->mnt_flag & MNT_NODEVMTIME) == 0)) {
   1433  1.103      yamt 		GOP_MARKUPDATE(vp, GOP_UPDATE_MODIFIED);
   1434  1.103      yamt 	}
   1435  1.103      yamt 
   1436   1.37       chs 	/*
   1437   1.37       chs 	 * if we're cleaning and there was nothing to clean,
   1438   1.37       chs 	 * take us off the syncer list.  if we started any i/o
   1439   1.37       chs 	 * and we're doing sync i/o, wait for all writes to finish.
   1440   1.37       chs 	 */
   1441   1.37       chs 
   1442   1.62  perseant 	s = splbio();
   1443  1.104      yamt 	if (cleanall && wasclean && gp->g_dirtygen == dirtygen &&
   1444  1.104      yamt 	    (vp->v_flag & VONWORKLST) != 0) {
   1445  1.103      yamt 		vp->v_flag &= ~VWRITEMAPDIRTY;
   1446  1.103      yamt 		if (LIST_FIRST(&vp->v_dirtyblkhd) == NULL) {
   1447  1.103      yamt 			vp->v_flag &= ~VONWORKLST;
   1448  1.103      yamt 			LIST_REMOVE(vp, v_synclist);
   1449  1.103      yamt 		}
   1450   1.37       chs 	}
   1451   1.62  perseant 	splx(s);
   1452  1.102      yamt 
   1453  1.102      yamt #if !defined(DEBUG)
   1454  1.102      yamt skip_scan:
   1455  1.102      yamt #endif /* !defined(DEBUG) */
   1456   1.37       chs 	if (!wasclean && !async) {
   1457   1.37       chs 		s = splbio();
   1458   1.71        pk 		/*
   1459   1.71        pk 		 * XXX - we want simple_unlock(&global_v_numoutput_slock);
   1460   1.71        pk 		 *	 but the slot in ltsleep() is taken!
   1461   1.71        pk 		 * XXX - try to recover from missed wakeups with a timeout..
   1462   1.71        pk 		 *	 must think of something better.
   1463   1.71        pk 		 */
   1464   1.37       chs 		while (vp->v_numoutput != 0) {
   1465   1.37       chs 			vp->v_flag |= VBWAIT;
   1466   1.46       chs 			UVM_UNLOCK_AND_WAIT(&vp->v_numoutput, slock, FALSE,
   1467   1.71        pk 			    "genput2", hz);
   1468   1.46       chs 			simple_lock(slock);
   1469   1.37       chs 		}
   1470   1.37       chs 		splx(s);
   1471   1.37       chs 	}
   1472   1.37       chs 	simple_unlock(&uobj->vmobjlock);
   1473   1.53     enami 	return (error);
   1474   1.37       chs }
   1475   1.37       chs 
   1476   1.37       chs int
   1477   1.37       chs genfs_gop_write(struct vnode *vp, struct vm_page **pgs, int npages, int flags)
   1478   1.37       chs {
   1479   1.37       chs 	int s, error, run;
   1480   1.37       chs 	int fs_bshift, dev_bshift;
   1481   1.21       chs 	vaddr_t kva;
   1482   1.21       chs 	off_t eof, offset, startoffset;
   1483   1.21       chs 	size_t bytes, iobytes, skipbytes;
   1484   1.21       chs 	daddr_t lbn, blkno;
   1485   1.21       chs 	struct vm_page *pg;
   1486   1.21       chs 	struct buf *mbp, *bp;
   1487   1.36       chs 	struct vnode *devvp;
   1488   1.37       chs 	boolean_t async = (flags & PGO_SYNCIO) == 0;
   1489   1.39     enami 	UVMHIST_FUNC("genfs_gop_write"); UVMHIST_CALLED(ubchist);
   1490   1.21       chs 
   1491   1.37       chs 	UVMHIST_LOG(ubchist, "vp %p pgs %p npages %d flags 0x%x",
   1492   1.37       chs 	    vp, pgs, npages, flags);
   1493   1.21       chs 
   1494   1.72  perseant 	GOP_SIZE(vp, vp->v_size, &eof, GOP_SIZE_WRITE);
   1495   1.36       chs 	if (vp->v_type == VREG) {
   1496   1.36       chs 		fs_bshift = vp->v_mount->mnt_fs_bshift;
   1497   1.36       chs 		dev_bshift = vp->v_mount->mnt_dev_bshift;
   1498   1.36       chs 	} else {
   1499   1.36       chs 		fs_bshift = DEV_BSHIFT;
   1500   1.36       chs 		dev_bshift = DEV_BSHIFT;
   1501   1.36       chs 	}
   1502   1.37       chs 	error = 0;
   1503   1.37       chs 	pg = pgs[0];
   1504   1.21       chs 	startoffset = pg->offset;
   1505   1.26       chs 	bytes = MIN(npages << PAGE_SHIFT, eof - startoffset);
   1506   1.21       chs 	skipbytes = 0;
   1507   1.21       chs 	KASSERT(bytes != 0);
   1508   1.21       chs 
   1509   1.53     enami 	kva = uvm_pagermapin(pgs, npages,
   1510   1.53     enami 	    UVMPAGER_MAPIN_WRITE | UVMPAGER_MAPIN_WAITOK);
   1511   1.21       chs 
   1512   1.21       chs 	s = splbio();
   1513   1.71        pk 	simple_lock(&global_v_numoutput_slock);
   1514   1.21       chs 	vp->v_numoutput += 2;
   1515   1.71        pk 	simple_unlock(&global_v_numoutput_slock);
   1516   1.21       chs 	mbp = pool_get(&bufpool, PR_WAITOK);
   1517   1.73   thorpej 	BUF_INIT(mbp);
   1518   1.21       chs 	UVMHIST_LOG(ubchist, "vp %p mbp %p num now %d bytes 0x%x",
   1519   1.53     enami 	    vp, mbp, vp->v_numoutput, bytes);
   1520   1.21       chs 	splx(s);
   1521   1.21       chs 	mbp->b_bufsize = npages << PAGE_SHIFT;
   1522   1.21       chs 	mbp->b_data = (void *)kva;
   1523   1.21       chs 	mbp->b_resid = mbp->b_bcount = bytes;
   1524   1.45       chs 	mbp->b_flags = B_BUSY|B_WRITE|B_AGE| (async ? (B_CALL|B_ASYNC) : 0);
   1525   1.21       chs 	mbp->b_iodone = uvm_aio_biodone;
   1526   1.21       chs 	mbp->b_vp = vp;
   1527   1.21       chs 
   1528   1.21       chs 	bp = NULL;
   1529   1.21       chs 	for (offset = startoffset;
   1530   1.53     enami 	    bytes > 0;
   1531   1.53     enami 	    offset += iobytes, bytes -= iobytes) {
   1532   1.21       chs 		lbn = offset >> fs_bshift;
   1533   1.36       chs 		error = VOP_BMAP(vp, lbn, &devvp, &blkno, &run);
   1534   1.21       chs 		if (error) {
   1535   1.21       chs 			UVMHIST_LOG(ubchist, "VOP_BMAP() -> %d", error,0,0,0);
   1536   1.21       chs 			skipbytes += bytes;
   1537   1.21       chs 			bytes = 0;
   1538   1.21       chs 			break;
   1539   1.21       chs 		}
   1540   1.21       chs 
   1541   1.26       chs 		iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
   1542   1.26       chs 		    bytes);
   1543   1.21       chs 		if (blkno == (daddr_t)-1) {
   1544   1.21       chs 			skipbytes += iobytes;
   1545   1.21       chs 			continue;
   1546   1.21       chs 		}
   1547   1.21       chs 
   1548   1.21       chs 		/* if it's really one i/o, don't make a second buf */
   1549   1.21       chs 		if (offset == startoffset && iobytes == bytes) {
   1550   1.21       chs 			bp = mbp;
   1551   1.21       chs 		} else {
   1552   1.21       chs 			s = splbio();
   1553   1.71        pk 			V_INCR_NUMOUTPUT(vp);
   1554   1.21       chs 			bp = pool_get(&bufpool, PR_WAITOK);
   1555   1.21       chs 			UVMHIST_LOG(ubchist, "vp %p bp %p num now %d",
   1556   1.53     enami 			    vp, bp, vp->v_numoutput, 0);
   1557   1.21       chs 			splx(s);
   1558   1.73   thorpej 			BUF_INIT(bp);
   1559   1.21       chs 			bp->b_data = (char *)kva +
   1560   1.53     enami 			    (vaddr_t)(offset - pg->offset);
   1561   1.21       chs 			bp->b_resid = bp->b_bcount = iobytes;
   1562   1.45       chs 			bp->b_flags = B_BUSY|B_WRITE|B_CALL|B_ASYNC;
   1563   1.21       chs 			bp->b_iodone = uvm_aio_biodone1;
   1564   1.21       chs 			bp->b_vp = vp;
   1565   1.21       chs 		}
   1566   1.21       chs 		bp->b_lblkno = 0;
   1567   1.21       chs 		bp->b_private = mbp;
   1568   1.37       chs 		if (devvp->v_type == VBLK) {
   1569   1.37       chs 			bp->b_dev = devvp->v_rdev;
   1570   1.37       chs 		}
   1571   1.21       chs 
   1572   1.21       chs 		/* adjust physical blkno for partial blocks */
   1573   1.25      fvdl 		bp->b_blkno = blkno + ((offset - ((off_t)lbn << fs_bshift)) >>
   1574   1.53     enami 		    dev_bshift);
   1575   1.53     enami 		UVMHIST_LOG(ubchist,
   1576   1.53     enami 		    "vp %p offset 0x%x bcount 0x%x blkno 0x%x",
   1577   1.53     enami 		    vp, offset, bp->b_bcount, bp->b_blkno);
   1578   1.84      yamt 		if (curproc == uvm.pagedaemon_proc)
   1579   1.84      yamt 			BIO_SETPRIO(bp, BPRIO_TIMELIMITED);
   1580   1.84      yamt 		else if (async)
   1581   1.84      yamt 			BIO_SETPRIO(bp, BPRIO_TIMENONCRITICAL);
   1582   1.84      yamt 		else
   1583   1.84      yamt 			BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
   1584   1.85   hannken 		VOP_STRATEGY(bp->b_vp, bp);
   1585   1.21       chs 	}
   1586   1.21       chs 	if (skipbytes) {
   1587   1.29       chs 		UVMHIST_LOG(ubchist, "skipbytes %d", skipbytes, 0,0,0);
   1588   1.21       chs 		s = splbio();
   1589   1.29       chs 		if (error) {
   1590   1.29       chs 			mbp->b_flags |= B_ERROR;
   1591   1.29       chs 			mbp->b_error = error;
   1592   1.29       chs 		}
   1593   1.37       chs 		mbp->b_resid -= skipbytes;
   1594   1.21       chs 		if (mbp->b_resid == 0) {
   1595   1.21       chs 			biodone(mbp);
   1596   1.21       chs 		}
   1597   1.21       chs 		splx(s);
   1598   1.21       chs 	}
   1599   1.21       chs 	if (async) {
   1600   1.32       chs 		UVMHIST_LOG(ubchist, "returning 0 (async)", 0,0,0,0);
   1601   1.53     enami 		return (0);
   1602   1.21       chs 	}
   1603   1.37       chs 	UVMHIST_LOG(ubchist, "waiting for mbp %p", mbp,0,0,0);
   1604   1.37       chs 	error = biowait(mbp);
   1605   1.37       chs 	uvm_aio_aiodone(mbp);
   1606   1.21       chs 	UVMHIST_LOG(ubchist, "returning, error %d", error,0,0,0);
   1607   1.53     enami 	return (error);
   1608   1.42       chs }
   1609   1.42       chs 
   1610   1.42       chs /*
   1611   1.42       chs  * VOP_PUTPAGES() for vnodes which never have pages.
   1612   1.42       chs  */
   1613   1.42       chs 
   1614   1.42       chs int
   1615   1.42       chs genfs_null_putpages(void *v)
   1616   1.42       chs {
   1617   1.42       chs 	struct vop_putpages_args /* {
   1618   1.42       chs 		struct vnode *a_vp;
   1619   1.42       chs 		voff_t a_offlo;
   1620   1.42       chs 		voff_t a_offhi;
   1621   1.42       chs 		int a_flags;
   1622   1.42       chs 	} */ *ap = v;
   1623   1.42       chs 	struct vnode *vp = ap->a_vp;
   1624   1.42       chs 
   1625   1.42       chs 	KASSERT(vp->v_uobj.uo_npages == 0);
   1626   1.42       chs 	simple_unlock(&vp->v_interlock);
   1627   1.42       chs 	return (0);
   1628   1.21       chs }
   1629   1.21       chs 
   1630   1.37       chs void
   1631   1.98      yamt genfs_node_init(struct vnode *vp, const struct genfs_ops *ops)
   1632   1.37       chs {
   1633   1.37       chs 	struct genfs_node *gp = VTOG(vp);
   1634   1.37       chs 
   1635   1.37       chs 	lockinit(&gp->g_glock, PINOD, "glock", 0, 0);
   1636   1.37       chs 	gp->g_op = ops;
   1637   1.37       chs }
   1638   1.37       chs 
   1639   1.37       chs void
   1640   1.72  perseant genfs_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
   1641   1.21       chs {
   1642   1.21       chs 	int bsize;
   1643   1.21       chs 
   1644   1.37       chs 	bsize = 1 << vp->v_mount->mnt_fs_bshift;
   1645   1.37       chs 	*eobp = (size + bsize - 1) & ~(bsize - 1);
   1646   1.43       chs }
   1647   1.43       chs 
   1648   1.43       chs int
   1649   1.43       chs genfs_compat_getpages(void *v)
   1650   1.43       chs {
   1651   1.43       chs 	struct vop_getpages_args /* {
   1652   1.43       chs 		struct vnode *a_vp;
   1653   1.43       chs 		voff_t a_offset;
   1654   1.43       chs 		struct vm_page **a_m;
   1655   1.43       chs 		int *a_count;
   1656   1.43       chs 		int a_centeridx;
   1657   1.43       chs 		vm_prot_t a_access_type;
   1658   1.43       chs 		int a_advice;
   1659   1.43       chs 		int a_flags;
   1660   1.43       chs 	} */ *ap = v;
   1661   1.43       chs 
   1662   1.43       chs 	off_t origoffset;
   1663   1.43       chs 	struct vnode *vp = ap->a_vp;
   1664   1.43       chs 	struct uvm_object *uobj = &vp->v_uobj;
   1665   1.43       chs 	struct vm_page *pg, **pgs;
   1666   1.43       chs 	vaddr_t kva;
   1667   1.43       chs 	int i, error, orignpages, npages;
   1668   1.43       chs 	struct iovec iov;
   1669   1.43       chs 	struct uio uio;
   1670   1.43       chs 	struct ucred *cred = curproc->p_ucred;
   1671   1.43       chs 	boolean_t write = (ap->a_access_type & VM_PROT_WRITE) != 0;
   1672   1.43       chs 
   1673   1.43       chs 	error = 0;
   1674   1.43       chs 	origoffset = ap->a_offset;
   1675   1.43       chs 	orignpages = *ap->a_count;
   1676   1.43       chs 	pgs = ap->a_m;
   1677   1.43       chs 
   1678   1.43       chs 	if (write && (vp->v_flag & VONWORKLST) == 0) {
   1679   1.43       chs 		vn_syncer_add_to_worklist(vp, filedelay);
   1680   1.43       chs 	}
   1681   1.43       chs 	if (ap->a_flags & PGO_LOCKED) {
   1682   1.43       chs 		uvn_findpages(uobj, origoffset, ap->a_count, ap->a_m,
   1683   1.54     enami 		    UFP_NOWAIT|UFP_NOALLOC| (write ? UFP_NORDONLY : 0));
   1684   1.43       chs 
   1685   1.53     enami 		return (ap->a_m[ap->a_centeridx] == NULL ? EBUSY : 0);
   1686   1.43       chs 	}
   1687   1.43       chs 	if (origoffset + (ap->a_centeridx << PAGE_SHIFT) >= vp->v_size) {
   1688   1.43       chs 		simple_unlock(&uobj->vmobjlock);
   1689   1.53     enami 		return (EINVAL);
   1690   1.43       chs 	}
   1691   1.43       chs 	npages = orignpages;
   1692   1.43       chs 	uvn_findpages(uobj, origoffset, &npages, pgs, UFP_ALL);
   1693   1.43       chs 	simple_unlock(&uobj->vmobjlock);
   1694   1.53     enami 	kva = uvm_pagermapin(pgs, npages,
   1695   1.53     enami 	    UVMPAGER_MAPIN_READ | UVMPAGER_MAPIN_WAITOK);
   1696   1.43       chs 	for (i = 0; i < npages; i++) {
   1697   1.43       chs 		pg = pgs[i];
   1698   1.43       chs 		if ((pg->flags & PG_FAKE) == 0) {
   1699   1.43       chs 			continue;
   1700   1.43       chs 		}
   1701   1.43       chs 		iov.iov_base = (char *)kva + (i << PAGE_SHIFT);
   1702   1.43       chs 		iov.iov_len = PAGE_SIZE;
   1703   1.43       chs 		uio.uio_iov = &iov;
   1704   1.43       chs 		uio.uio_iovcnt = 1;
   1705   1.43       chs 		uio.uio_offset = origoffset + (i << PAGE_SHIFT);
   1706   1.43       chs 		uio.uio_segflg = UIO_SYSSPACE;
   1707   1.43       chs 		uio.uio_rw = UIO_READ;
   1708   1.43       chs 		uio.uio_resid = PAGE_SIZE;
   1709   1.88     skrll 		uio.uio_procp = NULL;
   1710   1.87      yamt 		/* XXX vn_lock */
   1711   1.43       chs 		error = VOP_READ(vp, &uio, 0, cred);
   1712   1.43       chs 		if (error) {
   1713   1.43       chs 			break;
   1714   1.52       chs 		}
   1715   1.52       chs 		if (uio.uio_resid) {
   1716   1.52       chs 			memset(iov.iov_base, 0, uio.uio_resid);
   1717   1.43       chs 		}
   1718   1.43       chs 	}
   1719   1.43       chs 	uvm_pagermapout(kva, npages);
   1720   1.43       chs 	simple_lock(&uobj->vmobjlock);
   1721   1.43       chs 	uvm_lock_pageq();
   1722   1.43       chs 	for (i = 0; i < npages; i++) {
   1723   1.43       chs 		pg = pgs[i];
   1724   1.43       chs 		if (error && (pg->flags & PG_FAKE) != 0) {
   1725   1.43       chs 			pg->flags |= PG_RELEASED;
   1726   1.43       chs 		} else {
   1727   1.43       chs 			pmap_clear_modify(pg);
   1728   1.43       chs 			uvm_pageactivate(pg);
   1729   1.43       chs 		}
   1730   1.43       chs 	}
   1731   1.43       chs 	if (error) {
   1732   1.43       chs 		uvm_page_unbusy(pgs, npages);
   1733   1.43       chs 	}
   1734   1.43       chs 	uvm_unlock_pageq();
   1735   1.43       chs 	simple_unlock(&uobj->vmobjlock);
   1736   1.53     enami 	return (error);
   1737   1.43       chs }
   1738   1.43       chs 
   1739   1.43       chs int
   1740   1.43       chs genfs_compat_gop_write(struct vnode *vp, struct vm_page **pgs, int npages,
   1741   1.43       chs     int flags)
   1742   1.43       chs {
   1743   1.43       chs 	off_t offset;
   1744   1.43       chs 	struct iovec iov;
   1745   1.43       chs 	struct uio uio;
   1746   1.43       chs 	struct ucred *cred = curproc->p_ucred;
   1747   1.43       chs 	struct buf *bp;
   1748   1.43       chs 	vaddr_t kva;
   1749   1.43       chs 	int s, error;
   1750   1.43       chs 
   1751   1.43       chs 	offset = pgs[0]->offset;
   1752   1.53     enami 	kva = uvm_pagermapin(pgs, npages,
   1753   1.53     enami 	    UVMPAGER_MAPIN_WRITE | UVMPAGER_MAPIN_WAITOK);
   1754   1.43       chs 
   1755   1.43       chs 	iov.iov_base = (void *)kva;
   1756   1.43       chs 	iov.iov_len = npages << PAGE_SHIFT;
   1757   1.43       chs 	uio.uio_iov = &iov;
   1758   1.68      yamt 	uio.uio_iovcnt = 1;
   1759   1.43       chs 	uio.uio_offset = offset;
   1760   1.43       chs 	uio.uio_segflg = UIO_SYSSPACE;
   1761   1.43       chs 	uio.uio_rw = UIO_WRITE;
   1762   1.43       chs 	uio.uio_resid = npages << PAGE_SHIFT;
   1763   1.88     skrll 	uio.uio_procp = NULL;
   1764   1.87      yamt 	/* XXX vn_lock */
   1765   1.43       chs 	error = VOP_WRITE(vp, &uio, 0, cred);
   1766   1.43       chs 
   1767   1.43       chs 	s = splbio();
   1768   1.71        pk 	V_INCR_NUMOUTPUT(vp);
   1769   1.43       chs 	bp = pool_get(&bufpool, PR_WAITOK);
   1770   1.43       chs 	splx(s);
   1771   1.43       chs 
   1772   1.73   thorpej 	BUF_INIT(bp);
   1773   1.43       chs 	bp->b_flags = B_BUSY | B_WRITE | B_AGE;
   1774   1.43       chs 	bp->b_vp = vp;
   1775   1.43       chs 	bp->b_lblkno = offset >> vp->v_mount->mnt_fs_bshift;
   1776   1.43       chs 	bp->b_data = (char *)kva;
   1777   1.43       chs 	bp->b_bcount = npages << PAGE_SHIFT;
   1778   1.43       chs 	bp->b_bufsize = npages << PAGE_SHIFT;
   1779   1.43       chs 	bp->b_resid = 0;
   1780   1.43       chs 	if (error) {
   1781   1.43       chs 		bp->b_flags |= B_ERROR;
   1782   1.43       chs 		bp->b_error = error;
   1783   1.43       chs 	}
   1784   1.43       chs 	uvm_aio_aiodone(bp);
   1785   1.53     enami 	return (error);
   1786   1.66  jdolecek }
   1787   1.66  jdolecek 
   1788   1.66  jdolecek static void
   1789   1.66  jdolecek filt_genfsdetach(struct knote *kn)
   1790   1.66  jdolecek {
   1791   1.66  jdolecek 	struct vnode *vp = (struct vnode *)kn->kn_hook;
   1792   1.66  jdolecek 
   1793   1.66  jdolecek 	/* XXXLUKEM lock the struct? */
   1794   1.66  jdolecek 	SLIST_REMOVE(&vp->v_klist, kn, knote, kn_selnext);
   1795   1.66  jdolecek }
   1796   1.66  jdolecek 
   1797   1.66  jdolecek static int
   1798   1.66  jdolecek filt_genfsread(struct knote *kn, long hint)
   1799   1.66  jdolecek {
   1800   1.66  jdolecek 	struct vnode *vp = (struct vnode *)kn->kn_hook;
   1801   1.66  jdolecek 
   1802   1.66  jdolecek 	/*
   1803   1.66  jdolecek 	 * filesystem is gone, so set the EOF flag and schedule
   1804   1.66  jdolecek 	 * the knote for deletion.
   1805   1.66  jdolecek 	 */
   1806   1.66  jdolecek 	if (hint == NOTE_REVOKE) {
   1807   1.66  jdolecek 		kn->kn_flags |= (EV_EOF | EV_ONESHOT);
   1808   1.66  jdolecek 		return (1);
   1809   1.66  jdolecek 	}
   1810   1.66  jdolecek 
   1811   1.66  jdolecek 	/* XXXLUKEM lock the struct? */
   1812   1.66  jdolecek 	kn->kn_data = vp->v_size - kn->kn_fp->f_offset;
   1813   1.66  jdolecek         return (kn->kn_data != 0);
   1814   1.66  jdolecek }
   1815   1.66  jdolecek 
   1816   1.66  jdolecek static int
   1817   1.66  jdolecek filt_genfsvnode(struct knote *kn, long hint)
   1818   1.66  jdolecek {
   1819   1.66  jdolecek 
   1820   1.66  jdolecek 	if (kn->kn_sfflags & hint)
   1821   1.66  jdolecek 		kn->kn_fflags |= hint;
   1822   1.66  jdolecek 	if (hint == NOTE_REVOKE) {
   1823   1.66  jdolecek 		kn->kn_flags |= EV_EOF;
   1824   1.66  jdolecek 		return (1);
   1825   1.66  jdolecek 	}
   1826   1.66  jdolecek 	return (kn->kn_fflags != 0);
   1827   1.66  jdolecek }
   1828   1.66  jdolecek 
   1829   1.96     perry static const struct filterops genfsread_filtops =
   1830   1.66  jdolecek 	{ 1, NULL, filt_genfsdetach, filt_genfsread };
   1831   1.96     perry static const struct filterops genfsvnode_filtops =
   1832   1.66  jdolecek 	{ 1, NULL, filt_genfsdetach, filt_genfsvnode };
   1833   1.66  jdolecek 
   1834   1.66  jdolecek int
   1835   1.66  jdolecek genfs_kqfilter(void *v)
   1836   1.66  jdolecek {
   1837   1.66  jdolecek 	struct vop_kqfilter_args /* {
   1838   1.66  jdolecek 		struct vnode	*a_vp;
   1839   1.66  jdolecek 		struct knote	*a_kn;
   1840   1.66  jdolecek 	} */ *ap = v;
   1841   1.66  jdolecek 	struct vnode *vp;
   1842   1.66  jdolecek 	struct knote *kn;
   1843   1.66  jdolecek 
   1844   1.66  jdolecek 	vp = ap->a_vp;
   1845   1.66  jdolecek 	kn = ap->a_kn;
   1846   1.66  jdolecek 	switch (kn->kn_filter) {
   1847   1.66  jdolecek 	case EVFILT_READ:
   1848   1.66  jdolecek 		kn->kn_fop = &genfsread_filtops;
   1849   1.66  jdolecek 		break;
   1850   1.66  jdolecek 	case EVFILT_VNODE:
   1851   1.66  jdolecek 		kn->kn_fop = &genfsvnode_filtops;
   1852   1.66  jdolecek 		break;
   1853   1.66  jdolecek 	default:
   1854   1.66  jdolecek 		return (1);
   1855   1.66  jdolecek 	}
   1856   1.66  jdolecek 
   1857   1.66  jdolecek 	kn->kn_hook = vp;
   1858   1.66  jdolecek 
   1859   1.66  jdolecek 	/* XXXLUKEM lock the struct? */
   1860   1.66  jdolecek 	SLIST_INSERT_HEAD(&vp->v_klist, kn, kn_selnext);
   1861   1.66  jdolecek 
   1862   1.66  jdolecek 	return (0);
   1863    1.1   mycroft }
   1864