Home | History | Annotate | Line # | Download | only in genfs
genfs_vnops.c revision 1.35.2.1
      1  1.35.2.1   thorpej /*	$NetBSD: genfs_vnops.c,v 1.35.2.1 2001/08/25 06:16:53 thorpej 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.6      fvdl  * 3. All advertising materials mentioning features or use of this software
     16       1.6      fvdl  *    must display the following acknowledgement:
     17       1.6      fvdl  *	This product includes software developed by the University of
     18       1.6      fvdl  *	California, Berkeley and its contributors.
     19       1.6      fvdl  * 4. Neither the name of the University nor the names of its contributors
     20       1.6      fvdl  *    may be used to endorse or promote products derived from this software
     21       1.6      fvdl  *    without specific prior written permission.
     22       1.6      fvdl  *
     23       1.6      fvdl  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24       1.6      fvdl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25       1.6      fvdl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26       1.6      fvdl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27       1.6      fvdl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28       1.6      fvdl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29       1.6      fvdl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30       1.6      fvdl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31       1.6      fvdl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32       1.6      fvdl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33       1.6      fvdl  * SUCH DAMAGE.
     34       1.6      fvdl  *
     35       1.6      fvdl  */
     36       1.5     perry 
     37       1.8   thorpej #include "opt_nfsserver.h"
     38       1.8   thorpej 
     39       1.1   mycroft #include <sys/param.h>
     40       1.1   mycroft #include <sys/systm.h>
     41       1.6      fvdl #include <sys/proc.h>
     42       1.1   mycroft #include <sys/kernel.h>
     43       1.1   mycroft #include <sys/mount.h>
     44       1.1   mycroft #include <sys/namei.h>
     45       1.1   mycroft #include <sys/vnode.h>
     46      1.13  wrstuden #include <sys/fcntl.h>
     47       1.1   mycroft #include <sys/malloc.h>
     48       1.3   mycroft #include <sys/poll.h>
     49       1.1   mycroft 
     50       1.1   mycroft #include <miscfs/genfs/genfs.h>
     51       1.6      fvdl #include <miscfs/specfs/specdev.h>
     52       1.1   mycroft 
     53      1.21       chs #include <uvm/uvm.h>
     54      1.21       chs #include <uvm/uvm_pager.h>
     55      1.21       chs 
     56       1.8   thorpej #ifdef NFSSERVER
     57       1.8   thorpej #include <nfs/rpcv2.h>
     58       1.8   thorpej #include <nfs/nfsproto.h>
     59       1.8   thorpej #include <nfs/nfs.h>
     60       1.8   thorpej #include <nfs/nqnfs.h>
     61       1.8   thorpej #include <nfs/nfs_var.h>
     62       1.8   thorpej #endif
     63       1.8   thorpej 
     64       1.1   mycroft int
     65       1.3   mycroft genfs_poll(v)
     66       1.1   mycroft 	void *v;
     67       1.1   mycroft {
     68       1.3   mycroft 	struct vop_poll_args /* {
     69       1.1   mycroft 		struct vnode *a_vp;
     70       1.3   mycroft 		int a_events;
     71       1.1   mycroft 		struct proc *a_p;
     72       1.1   mycroft 	} */ *ap = v;
     73       1.1   mycroft 
     74       1.3   mycroft 	return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
     75       1.1   mycroft }
     76       1.1   mycroft 
     77       1.1   mycroft int
     78       1.1   mycroft genfs_fsync(v)
     79       1.1   mycroft 	void *v;
     80       1.1   mycroft {
     81       1.1   mycroft 	struct vop_fsync_args /* {
     82       1.1   mycroft 		struct vnode *a_vp;
     83       1.1   mycroft 		struct ucred *a_cred;
     84       1.7    kleink 		int a_flags;
     85      1.20      fvdl 		off_t offlo;
     86      1.20      fvdl 		off_t offhi;
     87       1.1   mycroft 		struct proc *a_p;
     88       1.1   mycroft 	} */ *ap = v;
     89      1.16  augustss 	struct vnode *vp = ap->a_vp;
     90      1.11   mycroft 	int wait;
     91       1.1   mycroft 
     92      1.11   mycroft 	wait = (ap->a_flags & FSYNC_WAIT) != 0;
     93      1.11   mycroft 	vflushbuf(vp, wait);
     94      1.11   mycroft 	if ((ap->a_flags & FSYNC_DATAONLY) != 0)
     95       1.7    kleink 		return (0);
     96      1.11   mycroft 	else
     97      1.18   mycroft 		return (VOP_UPDATE(vp, NULL, NULL, wait ? UPDATE_WAIT : 0));
     98       1.1   mycroft }
     99       1.1   mycroft 
    100       1.1   mycroft int
    101       1.4    kleink genfs_seek(v)
    102       1.4    kleink 	void *v;
    103       1.4    kleink {
    104       1.4    kleink 	struct vop_seek_args /* {
    105       1.4    kleink 		struct vnode *a_vp;
    106       1.4    kleink 		off_t a_oldoff;
    107       1.4    kleink 		off_t a_newoff;
    108       1.4    kleink 		struct ucred *a_ucred;
    109       1.4    kleink 	} */ *ap = v;
    110       1.4    kleink 
    111       1.4    kleink 	if (ap->a_newoff < 0)
    112       1.4    kleink 		return (EINVAL);
    113       1.4    kleink 
    114       1.4    kleink 	return (0);
    115       1.4    kleink }
    116       1.4    kleink 
    117       1.4    kleink int
    118       1.1   mycroft genfs_abortop(v)
    119       1.1   mycroft 	void *v;
    120       1.1   mycroft {
    121       1.1   mycroft 	struct vop_abortop_args /* {
    122       1.1   mycroft 		struct vnode *a_dvp;
    123       1.1   mycroft 		struct componentname *a_cnp;
    124       1.1   mycroft 	} */ *ap = v;
    125       1.1   mycroft 
    126       1.1   mycroft 	if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
    127      1.19   thorpej 		PNBUF_PUT(ap->a_cnp->cn_pnbuf);
    128       1.1   mycroft 	return (0);
    129      1.13  wrstuden }
    130      1.13  wrstuden 
    131      1.13  wrstuden int
    132      1.13  wrstuden genfs_fcntl(v)
    133      1.13  wrstuden 	void *v;
    134      1.13  wrstuden {
    135      1.13  wrstuden 	struct vop_fcntl_args /* {
    136      1.13  wrstuden 		struct vnode *a_vp;
    137      1.13  wrstuden 		u_int a_command;
    138      1.13  wrstuden 		caddr_t a_data;
    139      1.13  wrstuden 		int a_fflag;
    140      1.13  wrstuden 		struct ucred *a_cred;
    141      1.13  wrstuden 		struct proc *a_p;
    142      1.13  wrstuden 	} */ *ap = v;
    143      1.13  wrstuden 
    144      1.13  wrstuden 	if (ap->a_command == F_SETFL)
    145      1.13  wrstuden 		return (0);
    146      1.13  wrstuden 	else
    147      1.13  wrstuden 		return (EOPNOTSUPP);
    148       1.1   mycroft }
    149       1.1   mycroft 
    150       1.1   mycroft /*ARGSUSED*/
    151       1.1   mycroft int
    152       1.1   mycroft genfs_badop(v)
    153       1.1   mycroft 	void *v;
    154       1.1   mycroft {
    155       1.1   mycroft 
    156       1.1   mycroft 	panic("genfs: bad op");
    157       1.1   mycroft }
    158       1.1   mycroft 
    159       1.1   mycroft /*ARGSUSED*/
    160       1.1   mycroft int
    161       1.1   mycroft genfs_nullop(v)
    162       1.1   mycroft 	void *v;
    163       1.1   mycroft {
    164       1.1   mycroft 
    165       1.1   mycroft 	return (0);
    166      1.10    kleink }
    167      1.10    kleink 
    168      1.10    kleink /*ARGSUSED*/
    169      1.10    kleink int
    170      1.10    kleink genfs_einval(v)
    171      1.10    kleink 	void *v;
    172      1.10    kleink {
    173      1.10    kleink 
    174      1.10    kleink 	return (EINVAL);
    175       1.1   mycroft }
    176       1.1   mycroft 
    177       1.1   mycroft /*ARGSUSED*/
    178       1.1   mycroft int
    179       1.1   mycroft genfs_eopnotsupp(v)
    180       1.1   mycroft 	void *v;
    181       1.1   mycroft {
    182       1.1   mycroft 
    183       1.1   mycroft 	return (EOPNOTSUPP);
    184       1.1   mycroft }
    185       1.1   mycroft 
    186      1.12  wrstuden /*
    187      1.12  wrstuden  * Called when an fs doesn't support a particular vop but the vop needs to
    188      1.12  wrstuden  * vrele, vput, or vunlock passed in vnodes.
    189      1.12  wrstuden  */
    190      1.12  wrstuden int
    191      1.12  wrstuden genfs_eopnotsupp_rele(v)
    192      1.12  wrstuden 	void *v;
    193      1.12  wrstuden {
    194      1.12  wrstuden 	struct vop_generic_args /*
    195      1.12  wrstuden 		struct vnodeop_desc *a_desc;
    196      1.12  wrstuden 		/ * other random data follows, presumably * /
    197      1.12  wrstuden 	} */ *ap = v;
    198      1.12  wrstuden 	struct vnodeop_desc *desc = ap->a_desc;
    199      1.12  wrstuden 	struct vnode *vp;
    200      1.12  wrstuden 	int flags, i, j, offset;
    201      1.12  wrstuden 
    202      1.12  wrstuden 	flags = desc->vdesc_flags;
    203      1.12  wrstuden 	for (i = 0; i < VDESC_MAX_VPS; flags >>=1, i++) {
    204      1.12  wrstuden 		if ((offset = desc->vdesc_vp_offsets[i]) == VDESC_NO_OFFSET)
    205      1.12  wrstuden 			break;	/* stop at end of list */
    206      1.12  wrstuden 		if ((j = flags & VDESC_VP0_WILLPUT)) {
    207      1.12  wrstuden 			vp = *VOPARG_OFFSETTO(struct vnode**,offset,ap);
    208      1.12  wrstuden 			switch (j) {
    209      1.12  wrstuden 			case VDESC_VP0_WILLPUT:
    210      1.12  wrstuden 				vput(vp);
    211      1.12  wrstuden 				break;
    212      1.12  wrstuden 			case VDESC_VP0_WILLUNLOCK:
    213      1.12  wrstuden 				VOP_UNLOCK(vp, 0);
    214      1.12  wrstuden 				break;
    215      1.12  wrstuden 			case VDESC_VP0_WILLRELE:
    216      1.12  wrstuden 				vrele(vp);
    217      1.12  wrstuden 				break;
    218      1.12  wrstuden 			}
    219      1.12  wrstuden 		}
    220      1.12  wrstuden 	}
    221      1.12  wrstuden 
    222      1.12  wrstuden 	return (EOPNOTSUPP);
    223      1.12  wrstuden }
    224      1.12  wrstuden 
    225       1.1   mycroft /*ARGSUSED*/
    226       1.1   mycroft int
    227       1.1   mycroft genfs_ebadf(v)
    228       1.1   mycroft 	void *v;
    229       1.1   mycroft {
    230       1.1   mycroft 
    231       1.1   mycroft 	return (EBADF);
    232       1.9  matthias }
    233       1.9  matthias 
    234       1.9  matthias /* ARGSUSED */
    235       1.9  matthias int
    236       1.9  matthias genfs_enoioctl(v)
    237       1.9  matthias 	void *v;
    238       1.9  matthias {
    239       1.9  matthias 
    240       1.9  matthias 	return (ENOTTY);
    241       1.6      fvdl }
    242       1.6      fvdl 
    243       1.6      fvdl 
    244       1.6      fvdl /*
    245      1.15      fvdl  * Eliminate all activity associated with the requested vnode
    246       1.6      fvdl  * and with all vnodes aliased to the requested vnode.
    247       1.6      fvdl  */
    248       1.6      fvdl int
    249       1.6      fvdl genfs_revoke(v)
    250       1.6      fvdl 	void *v;
    251       1.6      fvdl {
    252       1.6      fvdl 	struct vop_revoke_args /* {
    253       1.6      fvdl 		struct vnode *a_vp;
    254       1.6      fvdl 		int a_flags;
    255       1.6      fvdl 	} */ *ap = v;
    256       1.6      fvdl 	struct vnode *vp, *vq;
    257       1.6      fvdl 	struct proc *p = curproc;	/* XXX */
    258       1.6      fvdl 
    259       1.6      fvdl #ifdef DIAGNOSTIC
    260       1.6      fvdl 	if ((ap->a_flags & REVOKEALL) == 0)
    261       1.6      fvdl 		panic("genfs_revoke: not revokeall");
    262       1.6      fvdl #endif
    263       1.6      fvdl 
    264       1.6      fvdl 	vp = ap->a_vp;
    265       1.6      fvdl 	simple_lock(&vp->v_interlock);
    266       1.6      fvdl 
    267       1.6      fvdl 	if (vp->v_flag & VALIASED) {
    268       1.6      fvdl 		/*
    269       1.6      fvdl 		 * If a vgone (or vclean) is already in progress,
    270       1.6      fvdl 		 * wait until it is done and return.
    271       1.6      fvdl 		 */
    272       1.6      fvdl 		if (vp->v_flag & VXLOCK) {
    273       1.6      fvdl 			vp->v_flag |= VXWANT;
    274       1.6      fvdl 			simple_unlock(&vp->v_interlock);
    275       1.6      fvdl 			tsleep((caddr_t)vp, PINOD, "vop_revokeall", 0);
    276       1.6      fvdl 			return (0);
    277       1.6      fvdl 		}
    278       1.6      fvdl 		/*
    279       1.6      fvdl 		 * Ensure that vp will not be vgone'd while we
    280       1.6      fvdl 		 * are eliminating its aliases.
    281       1.6      fvdl 		 */
    282       1.6      fvdl 		vp->v_flag |= VXLOCK;
    283       1.6      fvdl 		simple_unlock(&vp->v_interlock);
    284       1.6      fvdl 		while (vp->v_flag & VALIASED) {
    285       1.6      fvdl 			simple_lock(&spechash_slock);
    286       1.6      fvdl 			for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
    287       1.6      fvdl 				if (vq->v_rdev != vp->v_rdev ||
    288       1.6      fvdl 				    vq->v_type != vp->v_type || vp == vq)
    289       1.6      fvdl 					continue;
    290       1.6      fvdl 				simple_unlock(&spechash_slock);
    291       1.6      fvdl 				vgone(vq);
    292       1.6      fvdl 				break;
    293       1.6      fvdl 			}
    294       1.6      fvdl 			if (vq == NULLVP)
    295       1.6      fvdl 				simple_unlock(&spechash_slock);
    296       1.6      fvdl 		}
    297       1.6      fvdl 		/*
    298       1.6      fvdl 		 * Remove the lock so that vgone below will
    299       1.6      fvdl 		 * really eliminate the vnode after which time
    300       1.6      fvdl 		 * vgone will awaken any sleepers.
    301       1.6      fvdl 		 */
    302       1.6      fvdl 		simple_lock(&vp->v_interlock);
    303       1.6      fvdl 		vp->v_flag &= ~VXLOCK;
    304       1.6      fvdl 	}
    305       1.6      fvdl 	vgonel(vp, p);
    306       1.6      fvdl 	return (0);
    307       1.6      fvdl }
    308       1.6      fvdl 
    309       1.6      fvdl /*
    310      1.12  wrstuden  * Lock the node.
    311       1.6      fvdl  */
    312       1.6      fvdl int
    313      1.12  wrstuden genfs_lock(v)
    314       1.6      fvdl 	void *v;
    315       1.6      fvdl {
    316       1.6      fvdl 	struct vop_lock_args /* {
    317       1.6      fvdl 		struct vnode *a_vp;
    318       1.6      fvdl 		int a_flags;
    319       1.6      fvdl 	} */ *ap = v;
    320       1.6      fvdl 	struct vnode *vp = ap->a_vp;
    321       1.6      fvdl 
    322      1.12  wrstuden 	return (lockmgr(&vp->v_lock, ap->a_flags, &vp->v_interlock));
    323       1.6      fvdl }
    324       1.6      fvdl 
    325       1.6      fvdl /*
    326      1.12  wrstuden  * Unlock the node.
    327       1.6      fvdl  */
    328       1.6      fvdl int
    329      1.12  wrstuden genfs_unlock(v)
    330       1.6      fvdl 	void *v;
    331       1.6      fvdl {
    332       1.6      fvdl 	struct vop_unlock_args /* {
    333       1.6      fvdl 		struct vnode *a_vp;
    334       1.6      fvdl 		int a_flags;
    335       1.6      fvdl 	} */ *ap = v;
    336       1.6      fvdl 	struct vnode *vp = ap->a_vp;
    337       1.6      fvdl 
    338      1.12  wrstuden 	return (lockmgr(&vp->v_lock, ap->a_flags | LK_RELEASE,
    339      1.12  wrstuden 		&vp->v_interlock));
    340       1.6      fvdl }
    341       1.6      fvdl 
    342       1.6      fvdl /*
    343      1.12  wrstuden  * Return whether or not the node is locked.
    344       1.6      fvdl  */
    345       1.6      fvdl int
    346      1.12  wrstuden genfs_islocked(v)
    347       1.6      fvdl 	void *v;
    348       1.6      fvdl {
    349       1.6      fvdl 	struct vop_islocked_args /* {
    350       1.6      fvdl 		struct vnode *a_vp;
    351       1.6      fvdl 	} */ *ap = v;
    352       1.6      fvdl 	struct vnode *vp = ap->a_vp;
    353       1.6      fvdl 
    354      1.12  wrstuden 	return (lockstatus(&vp->v_lock));
    355      1.12  wrstuden }
    356      1.12  wrstuden 
    357      1.12  wrstuden /*
    358      1.12  wrstuden  * Stubs to use when there is no locking to be done on the underlying object.
    359      1.12  wrstuden  */
    360      1.12  wrstuden int
    361      1.12  wrstuden genfs_nolock(v)
    362      1.12  wrstuden 	void *v;
    363      1.12  wrstuden {
    364      1.12  wrstuden 	struct vop_lock_args /* {
    365      1.12  wrstuden 		struct vnode *a_vp;
    366      1.12  wrstuden 		int a_flags;
    367      1.12  wrstuden 		struct proc *a_p;
    368      1.12  wrstuden 	} */ *ap = v;
    369      1.12  wrstuden 
    370      1.12  wrstuden 	/*
    371      1.12  wrstuden 	 * Since we are not using the lock manager, we must clear
    372      1.12  wrstuden 	 * the interlock here.
    373      1.12  wrstuden 	 */
    374      1.12  wrstuden 	if (ap->a_flags & LK_INTERLOCK)
    375      1.12  wrstuden 		simple_unlock(&ap->a_vp->v_interlock);
    376      1.12  wrstuden 	return (0);
    377      1.12  wrstuden }
    378      1.12  wrstuden 
    379      1.12  wrstuden int
    380      1.12  wrstuden genfs_nounlock(v)
    381      1.12  wrstuden 	void *v;
    382      1.12  wrstuden {
    383      1.12  wrstuden 	return (0);
    384      1.12  wrstuden }
    385      1.12  wrstuden 
    386      1.12  wrstuden int
    387      1.12  wrstuden genfs_noislocked(v)
    388      1.12  wrstuden 	void *v;
    389      1.12  wrstuden {
    390      1.12  wrstuden 	return (0);
    391       1.8   thorpej }
    392       1.8   thorpej 
    393       1.8   thorpej /*
    394       1.8   thorpej  * Local lease check for NFS servers.  Just set up args and let
    395       1.8   thorpej  * nqsrv_getlease() do the rest.  If NFSSERVER is not in the kernel,
    396       1.8   thorpej  * this is a null operation.
    397       1.8   thorpej  */
    398       1.8   thorpej int
    399       1.8   thorpej genfs_lease_check(v)
    400       1.8   thorpej 	void *v;
    401       1.8   thorpej {
    402       1.8   thorpej #ifdef NFSSERVER
    403       1.8   thorpej 	struct vop_lease_args /* {
    404       1.8   thorpej 		struct vnode *a_vp;
    405       1.8   thorpej 		struct proc *a_p;
    406       1.8   thorpej 		struct ucred *a_cred;
    407       1.8   thorpej 		int a_flag;
    408       1.8   thorpej 	} */ *ap = v;
    409       1.8   thorpej 	u_int32_t duration = 0;
    410       1.8   thorpej 	int cache;
    411       1.8   thorpej 	u_quad_t frev;
    412       1.8   thorpej 
    413       1.8   thorpej 	(void) nqsrv_getlease(ap->a_vp, &duration, ND_CHECK | ap->a_flag,
    414       1.8   thorpej 	    NQLOCALSLP, ap->a_p, (struct mbuf *)0, &cache, &frev, ap->a_cred);
    415       1.8   thorpej 	return (0);
    416       1.8   thorpej #else
    417       1.8   thorpej 	return (0);
    418       1.8   thorpej #endif /* NFSSERVER */
    419      1.34       chs }
    420      1.34       chs 
    421      1.34       chs int
    422      1.34       chs genfs_mmap(v)
    423      1.34       chs 	void *v;
    424      1.34       chs {
    425      1.34       chs 	return 0;
    426      1.21       chs }
    427      1.21       chs 
    428      1.21       chs /*
    429      1.21       chs  * generic VM getpages routine.
    430      1.21       chs  * Return PG_BUSY pages for the given range,
    431      1.21       chs  * reading from backing store if necessary.
    432      1.21       chs  */
    433      1.21       chs 
    434      1.21       chs int
    435      1.21       chs genfs_getpages(v)
    436      1.21       chs 	void *v;
    437      1.21       chs {
    438      1.21       chs 	struct vop_getpages_args /* {
    439      1.21       chs 		struct vnode *a_vp;
    440      1.21       chs 		voff_t a_offset;
    441      1.33       chs 		struct vm_page **a_m;
    442      1.21       chs 		int *a_count;
    443      1.21       chs 		int a_centeridx;
    444      1.21       chs 		vm_prot_t a_access_type;
    445      1.21       chs 		int a_advice;
    446      1.21       chs 		int a_flags;
    447      1.21       chs 	} */ *ap = v;
    448      1.21       chs 
    449      1.30       chs 	off_t newsize, diskeof, memeof;
    450      1.26       chs 	off_t offset, origoffset, startoffset, endoffset, raoffset;
    451      1.21       chs 	daddr_t lbn, blkno;
    452      1.21       chs 	int s, i, error, npages, orignpages, npgs, run, ridx, pidx, pcount;
    453      1.21       chs 	int fs_bshift, fs_bsize, dev_bshift, dev_bsize;
    454      1.21       chs 	int flags = ap->a_flags;
    455      1.21       chs 	size_t bytes, iobytes, tailbytes, totalbytes, skipbytes;
    456      1.21       chs 	vaddr_t kva;
    457      1.21       chs 	struct buf *bp, *mbp;
    458      1.21       chs 	struct vnode *vp = ap->a_vp;
    459  1.35.2.1   thorpej 	struct vnode *devvp;
    460      1.21       chs 	struct uvm_object *uobj = &vp->v_uvm.u_obj;
    461      1.21       chs 	struct vm_page *pgs[16];			/* XXXUBC 16 */
    462      1.21       chs 	struct ucred *cred = curproc->p_ucred;		/* XXXUBC curproc */
    463      1.21       chs 	boolean_t async = (flags & PGO_SYNCIO) == 0;
    464      1.21       chs 	boolean_t write = (ap->a_access_type & VM_PROT_WRITE) != 0;
    465      1.21       chs 	boolean_t sawhole = FALSE;
    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.30       chs 		    vp, ap->a_offset >> 32, ap->a_offset, *ap->a_count);
    470      1.30       chs 
    471      1.21       chs 	/* XXXUBC temp limit */
    472      1.21       chs 	if (*ap->a_count > 16) {
    473      1.21       chs 		return EINVAL;
    474      1.21       chs 	}
    475      1.21       chs 
    476      1.26       chs 	error = 0;
    477      1.26       chs 	origoffset = ap->a_offset;
    478      1.26       chs 	orignpages = *ap->a_count;
    479      1.30       chs 	error = VOP_SIZE(vp, vp->v_uvm.u_size, &diskeof);
    480      1.30       chs 	if (error) {
    481      1.30       chs 		return error;
    482      1.30       chs 	}
    483      1.26       chs 	if (flags & PGO_PASTEOF) {
    484      1.26       chs 		newsize = MAX(vp->v_uvm.u_size,
    485      1.26       chs 			      origoffset + (orignpages << PAGE_SHIFT));
    486      1.30       chs 		error = VOP_SIZE(vp, newsize, &memeof);
    487      1.30       chs 		if (error) {
    488      1.30       chs 			return error;
    489      1.30       chs 		}
    490      1.26       chs 	} else {
    491      1.30       chs 		memeof = diskeof;
    492      1.21       chs 	}
    493      1.30       chs 	KASSERT(ap->a_centeridx >= 0 || ap->a_centeridx <= orignpages);
    494      1.30       chs 	KASSERT((origoffset & (PAGE_SIZE - 1)) == 0 && origoffset >= 0);
    495      1.30       chs 	KASSERT(orignpages > 0);
    496      1.21       chs 
    497      1.21       chs 	/*
    498      1.21       chs 	 * Bounds-check the request.
    499      1.21       chs 	 */
    500      1.21       chs 
    501      1.30       chs 	if (origoffset + (ap->a_centeridx << PAGE_SHIFT) >= memeof) {
    502      1.21       chs 		if ((flags & PGO_LOCKED) == 0) {
    503      1.21       chs 			simple_unlock(&uobj->vmobjlock);
    504      1.21       chs 		}
    505      1.21       chs 		UVMHIST_LOG(ubchist, "off 0x%x count %d goes past EOF 0x%x",
    506      1.30       chs 			    origoffset, *ap->a_count, memeof,0);
    507      1.21       chs 		return EINVAL;
    508      1.21       chs 	}
    509      1.21       chs 
    510      1.21       chs 	/*
    511      1.21       chs 	 * For PGO_LOCKED requests, just return whatever's in memory.
    512      1.21       chs 	 */
    513      1.21       chs 
    514      1.21       chs 	if (flags & PGO_LOCKED) {
    515      1.21       chs 		uvn_findpages(uobj, origoffset, ap->a_count, ap->a_m,
    516      1.21       chs 			      UFP_NOWAIT|UFP_NOALLOC|UFP_NORDONLY);
    517      1.21       chs 
    518      1.21       chs 		return ap->a_m[ap->a_centeridx] == NULL ? EBUSY : 0;
    519      1.21       chs 	}
    520      1.21       chs 
    521      1.21       chs 	/* vnode is VOP_LOCKed, uobj is locked */
    522      1.21       chs 
    523      1.21       chs 	if (write && (vp->v_flag & VONWORKLST) == 0) {
    524      1.21       chs 		vn_syncer_add_to_worklist(vp, filedelay);
    525      1.21       chs 	}
    526      1.21       chs 
    527      1.21       chs 	/*
    528      1.21       chs 	 * find the requested pages and make some simple checks.
    529      1.21       chs 	 * leave space in the page array for a whole block.
    530      1.21       chs 	 */
    531      1.21       chs 
    532  1.35.2.1   thorpej 	if (vp->v_type == VREG) {
    533  1.35.2.1   thorpej 		fs_bshift = vp->v_mount->mnt_fs_bshift;
    534  1.35.2.1   thorpej 		dev_bshift = vp->v_mount->mnt_dev_bshift;
    535  1.35.2.1   thorpej 	} else {
    536  1.35.2.1   thorpej 		fs_bshift = DEV_BSHIFT;
    537  1.35.2.1   thorpej 		dev_bshift = DEV_BSHIFT;
    538  1.35.2.1   thorpej 	}
    539      1.21       chs 	fs_bsize = 1 << fs_bshift;
    540      1.21       chs 	dev_bsize = 1 << dev_bshift;
    541      1.30       chs 	KASSERT((diskeof & (dev_bsize - 1)) == 0);
    542      1.30       chs 	KASSERT((memeof & (dev_bsize - 1)) == 0);
    543      1.21       chs 
    544      1.30       chs 	orignpages = MIN(orignpages,
    545      1.30       chs 	    round_page(memeof - origoffset) >> PAGE_SHIFT);
    546      1.21       chs 	npages = orignpages;
    547      1.21       chs 	startoffset = origoffset & ~(fs_bsize - 1);
    548      1.21       chs 	endoffset = round_page((origoffset + (npages << PAGE_SHIFT)
    549      1.21       chs 				+ fs_bsize - 1) & ~(fs_bsize - 1));
    550      1.30       chs 	endoffset = MIN(endoffset, round_page(memeof));
    551      1.21       chs 	ridx = (origoffset - startoffset) >> PAGE_SHIFT;
    552      1.21       chs 
    553      1.21       chs 	memset(pgs, 0, sizeof(pgs));
    554      1.21       chs 	uvn_findpages(uobj, origoffset, &npages, &pgs[ridx], UFP_ALL);
    555      1.21       chs 
    556      1.21       chs 	/*
    557      1.21       chs 	 * if PGO_OVERWRITE is set, don't bother reading the pages.
    558      1.21       chs 	 * PGO_OVERWRITE also means that the caller guarantees
    559      1.21       chs 	 * that the pages already have backing store allocated.
    560      1.21       chs 	 */
    561      1.21       chs 
    562      1.21       chs 	if (flags & PGO_OVERWRITE) {
    563      1.21       chs 		UVMHIST_LOG(ubchist, "PGO_OVERWRITE",0,0,0,0);
    564      1.21       chs 
    565      1.21       chs 		for (i = 0; i < npages; i++) {
    566      1.21       chs 			struct vm_page *pg = pgs[ridx + i];
    567      1.21       chs 
    568      1.21       chs 			if (pg->flags & PG_FAKE) {
    569      1.21       chs 				uvm_pagezero(pg);
    570      1.21       chs 				pg->flags &= ~(PG_FAKE);
    571      1.21       chs 			}
    572      1.21       chs 			pg->flags &= ~(PG_RDONLY);
    573      1.21       chs 		}
    574      1.26       chs 		npages += ridx;
    575      1.21       chs 		goto out;
    576      1.21       chs 	}
    577      1.21       chs 
    578      1.21       chs 	/*
    579      1.21       chs 	 * if the pages are already resident, just return them.
    580      1.21       chs 	 */
    581      1.21       chs 
    582      1.21       chs 	for (i = 0; i < npages; i++) {
    583      1.21       chs 		struct vm_page *pg = pgs[ridx + i];
    584      1.21       chs 
    585      1.21       chs 		if ((pg->flags & PG_FAKE) ||
    586      1.21       chs 		    (write && (pg->flags & PG_RDONLY))) {
    587      1.21       chs 			break;
    588      1.21       chs 		}
    589      1.21       chs 	}
    590      1.21       chs 	if (i == npages) {
    591      1.21       chs 		UVMHIST_LOG(ubchist, "returning cached pages", 0,0,0,0);
    592      1.21       chs 		raoffset = origoffset + (orignpages << PAGE_SHIFT);
    593      1.26       chs 		npages += ridx;
    594      1.21       chs 		goto raout;
    595      1.21       chs 	}
    596      1.21       chs 
    597      1.21       chs 	/*
    598      1.21       chs 	 * the page wasn't resident and we're not overwriting,
    599      1.21       chs 	 * so we're going to have to do some i/o.
    600      1.21       chs 	 * find any additional pages needed to cover the expanded range.
    601      1.21       chs 	 */
    602      1.21       chs 
    603      1.35       chs 	npages = (endoffset - startoffset) >> PAGE_SHIFT;
    604      1.35       chs 	if (startoffset != origoffset || npages != orignpages) {
    605      1.21       chs 
    606      1.21       chs 		/*
    607      1.21       chs 		 * XXXUBC we need to avoid deadlocks caused by locking
    608      1.21       chs 		 * additional pages at lower offsets than pages we
    609      1.21       chs 		 * already have locked.  for now, unlock them all and
    610      1.21       chs 		 * start over.
    611      1.21       chs 		 */
    612      1.21       chs 
    613      1.35       chs 		for (i = 0; i < orignpages; i++) {
    614      1.21       chs 			struct vm_page *pg = pgs[ridx + i];
    615      1.21       chs 
    616      1.21       chs 			if (pg->flags & PG_FAKE) {
    617      1.21       chs 				pg->flags |= PG_RELEASED;
    618      1.21       chs 			}
    619      1.21       chs 		}
    620      1.35       chs 		uvm_page_unbusy(&pgs[ridx], orignpages);
    621      1.21       chs 		memset(pgs, 0, sizeof(pgs));
    622      1.21       chs 
    623      1.21       chs 		UVMHIST_LOG(ubchist, "reset npages start 0x%x end 0x%x",
    624      1.21       chs 			    startoffset, endoffset, 0,0);
    625      1.21       chs 		npgs = npages;
    626      1.21       chs 		uvn_findpages(uobj, startoffset, &npgs, pgs, UFP_ALL);
    627      1.21       chs 	}
    628      1.21       chs 	simple_unlock(&uobj->vmobjlock);
    629      1.21       chs 
    630      1.21       chs 	/*
    631      1.21       chs 	 * read the desired page(s).
    632      1.21       chs 	 */
    633      1.21       chs 
    634      1.21       chs 	totalbytes = npages << PAGE_SHIFT;
    635      1.30       chs 	bytes = MIN(totalbytes, MAX(diskeof - startoffset, 0));
    636      1.21       chs 	tailbytes = totalbytes - bytes;
    637      1.21       chs 	skipbytes = 0;
    638      1.21       chs 
    639      1.21       chs 	kva = uvm_pagermapin(pgs, npages, UVMPAGER_MAPIN_WAITOK |
    640      1.21       chs 			     UVMPAGER_MAPIN_READ);
    641      1.21       chs 
    642      1.21       chs 	s = splbio();
    643      1.21       chs 	mbp = pool_get(&bufpool, PR_WAITOK);
    644      1.21       chs 	splx(s);
    645      1.21       chs 	mbp->b_bufsize = totalbytes;
    646      1.21       chs 	mbp->b_data = (void *)kva;
    647      1.21       chs 	mbp->b_resid = mbp->b_bcount = bytes;
    648      1.21       chs 	mbp->b_flags = B_BUSY|B_READ| (async ? B_CALL : 0);
    649      1.21       chs 	mbp->b_iodone = uvm_aio_biodone;
    650      1.21       chs 	mbp->b_vp = vp;
    651      1.21       chs 	LIST_INIT(&mbp->b_dep);
    652      1.21       chs 
    653      1.21       chs 	/*
    654      1.31       chs 	 * if EOF is in the middle of the range, zero the part past EOF.
    655      1.21       chs 	 */
    656      1.21       chs 
    657      1.31       chs 	if (tailbytes > 0) {
    658      1.21       chs 		memset((void *)(kva + bytes), 0, tailbytes);
    659      1.21       chs 	}
    660      1.21       chs 
    661      1.21       chs 	/*
    662      1.21       chs 	 * now loop over the pages, reading as needed.
    663      1.21       chs 	 */
    664      1.21       chs 
    665      1.21       chs 	if (write) {
    666      1.21       chs 		lockmgr(&vp->v_glock, LK_EXCLUSIVE, NULL);
    667      1.21       chs 	} else {
    668      1.21       chs 		lockmgr(&vp->v_glock, LK_SHARED, NULL);
    669      1.21       chs 	}
    670      1.21       chs 
    671      1.21       chs 	bp = NULL;
    672      1.21       chs 	for (offset = startoffset;
    673      1.21       chs 	     bytes > 0;
    674      1.21       chs 	     offset += iobytes, bytes -= iobytes) {
    675      1.21       chs 
    676      1.21       chs 		/*
    677      1.21       chs 		 * skip pages which don't need to be read.
    678      1.21       chs 		 */
    679      1.21       chs 
    680      1.21       chs 		pidx = (offset - startoffset) >> PAGE_SHIFT;
    681      1.35       chs 		while ((pgs[pidx]->flags & (PG_FAKE|PG_RDONLY)) == 0) {
    682      1.21       chs 			size_t b;
    683      1.21       chs 
    684      1.24       chs 			KASSERT((offset & (PAGE_SIZE - 1)) == 0);
    685      1.26       chs 			b = MIN(PAGE_SIZE, bytes);
    686      1.21       chs 			offset += b;
    687      1.21       chs 			bytes -= b;
    688      1.21       chs 			skipbytes += b;
    689      1.21       chs 			pidx++;
    690      1.21       chs 			UVMHIST_LOG(ubchist, "skipping, new offset 0x%x",
    691      1.21       chs 				    offset, 0,0,0);
    692      1.21       chs 			if (bytes == 0) {
    693      1.21       chs 				goto loopdone;
    694      1.21       chs 			}
    695      1.21       chs 		}
    696      1.21       chs 
    697      1.21       chs 		/*
    698      1.21       chs 		 * bmap the file to find out the blkno to read from and
    699      1.21       chs 		 * how much we can read in one i/o.  if bmap returns an error,
    700      1.21       chs 		 * skip the rest of the top-level i/o.
    701      1.21       chs 		 */
    702      1.21       chs 
    703      1.21       chs 		lbn = offset >> fs_bshift;
    704  1.35.2.1   thorpej 		error = VOP_BMAP(vp, lbn, &devvp, &blkno, &run);
    705      1.21       chs 		if (error) {
    706      1.21       chs 			UVMHIST_LOG(ubchist, "VOP_BMAP lbn 0x%x -> %d\n",
    707      1.21       chs 				    lbn, error,0,0);
    708      1.21       chs 			skipbytes += bytes;
    709      1.21       chs 			goto loopdone;
    710      1.21       chs 		}
    711      1.21       chs 
    712      1.21       chs 		/*
    713      1.21       chs 		 * see how many pages can be read with this i/o.
    714      1.21       chs 		 * reduce the i/o size if necessary to avoid
    715      1.21       chs 		 * overwriting pages with valid data.
    716      1.21       chs 		 */
    717      1.21       chs 
    718      1.26       chs 		iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
    719      1.26       chs 		    bytes);
    720      1.21       chs 		if (offset + iobytes > round_page(offset)) {
    721      1.21       chs 			pcount = 1;
    722      1.21       chs 			while (pidx + pcount < npages &&
    723      1.21       chs 			       pgs[pidx + pcount]->flags & PG_FAKE) {
    724      1.21       chs 				pcount++;
    725      1.21       chs 			}
    726      1.26       chs 			iobytes = MIN(iobytes, (pcount << PAGE_SHIFT) -
    727      1.21       chs 				      (offset - trunc_page(offset)));
    728      1.21       chs 		}
    729      1.21       chs 
    730      1.21       chs 		/*
    731      1.21       chs 		 * if this block isn't allocated, zero it instead of reading it.
    732      1.21       chs 		 * if this is a read access, mark the pages we zeroed PG_RDONLY.
    733      1.21       chs 		 */
    734      1.21       chs 
    735      1.21       chs 		if (blkno < 0) {
    736      1.35       chs 			int holepages = (round_page(offset + iobytes) -
    737      1.35       chs 					 trunc_page(offset)) >> PAGE_SHIFT;
    738      1.21       chs 			UVMHIST_LOG(ubchist, "lbn 0x%x -> HOLE", lbn,0,0,0);
    739      1.21       chs 
    740      1.21       chs 			sawhole = TRUE;
    741      1.21       chs 			memset((char *)kva + (offset - startoffset), 0,
    742      1.21       chs 			       iobytes);
    743      1.21       chs 			skipbytes += iobytes;
    744      1.21       chs 
    745      1.35       chs 			for (i = 0; i < holepages; i++) {
    746      1.35       chs 				if (write) {
    747      1.35       chs 					pgs[pidx + i]->flags &= ~PG_CLEAN;
    748      1.35       chs 				} else {
    749      1.21       chs 					pgs[pidx + i]->flags |= PG_RDONLY;
    750      1.21       chs 				}
    751      1.21       chs 			}
    752      1.21       chs 			continue;
    753      1.21       chs 		}
    754      1.21       chs 
    755      1.21       chs 		/*
    756      1.21       chs 		 * allocate a sub-buf for this piece of the i/o
    757      1.21       chs 		 * (or just use mbp if there's only 1 piece),
    758      1.21       chs 		 * and start it going.
    759      1.21       chs 		 */
    760      1.21       chs 
    761      1.21       chs 		if (offset == startoffset && iobytes == bytes) {
    762      1.21       chs 			bp = mbp;
    763      1.21       chs 		} else {
    764      1.21       chs 			s = splbio();
    765      1.21       chs 			bp = pool_get(&bufpool, PR_WAITOK);
    766      1.21       chs 			splx(s);
    767      1.21       chs 			bp->b_data = (char *)kva + offset - startoffset;
    768      1.21       chs 			bp->b_resid = bp->b_bcount = iobytes;
    769      1.21       chs 			bp->b_flags = B_BUSY|B_READ|B_CALL;
    770      1.21       chs 			bp->b_iodone = uvm_aio_biodone1;
    771      1.21       chs 			bp->b_vp = vp;
    772      1.21       chs 			LIST_INIT(&bp->b_dep);
    773      1.21       chs 		}
    774      1.21       chs 		bp->b_lblkno = 0;
    775      1.21       chs 		bp->b_private = mbp;
    776  1.35.2.1   thorpej 		bp->b_dev = devvp->v_rdev;
    777      1.21       chs 
    778      1.21       chs 		/* adjust physical blkno for partial blocks */
    779      1.25      fvdl 		bp->b_blkno = blkno + ((offset - ((off_t)lbn << fs_bshift)) >>
    780      1.21       chs 				       dev_bshift);
    781      1.21       chs 
    782      1.21       chs 		UVMHIST_LOG(ubchist, "bp %p offset 0x%x bcount 0x%x blkno 0x%x",
    783      1.21       chs 			    bp, offset, iobytes, bp->b_blkno);
    784      1.21       chs 
    785      1.21       chs 		VOP_STRATEGY(bp);
    786      1.21       chs 	}
    787      1.21       chs 
    788      1.21       chs loopdone:
    789      1.21       chs 	if (skipbytes) {
    790      1.21       chs 		s = splbio();
    791      1.21       chs 		if (error) {
    792      1.21       chs 			mbp->b_flags |= B_ERROR;
    793      1.21       chs 			mbp->b_error = error;
    794      1.21       chs 		}
    795      1.21       chs 		mbp->b_resid -= skipbytes;
    796      1.21       chs 		if (mbp->b_resid == 0) {
    797      1.21       chs 			biodone(mbp);
    798      1.21       chs 		}
    799      1.21       chs 		splx(s);
    800      1.21       chs 	}
    801      1.21       chs 
    802      1.21       chs 	if (async) {
    803      1.32       chs 		UVMHIST_LOG(ubchist, "returning 0 (async)",0,0,0,0);
    804      1.21       chs 		lockmgr(&vp->v_glock, LK_RELEASE, NULL);
    805      1.32       chs 		return 0;
    806      1.21       chs 	}
    807      1.21       chs 	if (bp != NULL) {
    808      1.21       chs 		error = biowait(mbp);
    809      1.21       chs 	}
    810      1.21       chs 	s = splbio();
    811      1.21       chs 	pool_put(&bufpool, mbp);
    812      1.21       chs 	splx(s);
    813      1.21       chs 	uvm_pagermapout(kva, npages);
    814      1.24       chs 	raoffset = startoffset + totalbytes;
    815      1.21       chs 
    816      1.21       chs 	/*
    817      1.21       chs 	 * if this we encountered a hole then we have to do a little more work.
    818      1.21       chs 	 * for read faults, we marked the page PG_RDONLY so that future
    819      1.21       chs 	 * write accesses to the page will fault again.
    820      1.21       chs 	 * for write faults, we must make sure that the backing store for
    821      1.21       chs 	 * the page is completely allocated while the pages are locked.
    822      1.21       chs 	 */
    823      1.21       chs 
    824      1.21       chs 	if (error == 0 && sawhole && write) {
    825      1.21       chs 		error = VOP_BALLOCN(vp, startoffset, npages << PAGE_SHIFT,
    826      1.21       chs 				   cred, 0);
    827      1.21       chs 		if (error) {
    828      1.21       chs 			UVMHIST_LOG(ubchist, "balloc lbn 0x%x -> %d",
    829      1.21       chs 				    lbn, error,0,0);
    830      1.21       chs 			lockmgr(&vp->v_glock, LK_RELEASE, NULL);
    831      1.21       chs 			simple_lock(&uobj->vmobjlock);
    832      1.21       chs 			goto out;
    833      1.21       chs 		}
    834      1.21       chs 	}
    835      1.21       chs 	lockmgr(&vp->v_glock, LK_RELEASE, NULL);
    836      1.21       chs 	simple_lock(&uobj->vmobjlock);
    837      1.21       chs 
    838      1.21       chs 	/*
    839      1.21       chs 	 * see if we want to start any readahead.
    840      1.21       chs 	 * XXXUBC for now, just read the next 128k on 64k boundaries.
    841      1.21       chs 	 * this is pretty nonsensical, but it is 50% faster than reading
    842      1.21       chs 	 * just the next 64k.
    843      1.21       chs 	 */
    844      1.21       chs 
    845      1.21       chs raout:
    846      1.24       chs 	if (!error && !async && !write && ((int)raoffset & 0xffff) == 0 &&
    847      1.21       chs 	    PAGE_SHIFT <= 16) {
    848      1.21       chs 		int racount;
    849      1.21       chs 
    850      1.21       chs 		racount = 1 << (16 - PAGE_SHIFT);
    851      1.21       chs 		(void) VOP_GETPAGES(vp, raoffset, NULL, &racount, 0,
    852      1.21       chs 				    VM_PROT_READ, 0, 0);
    853      1.21       chs 		simple_lock(&uobj->vmobjlock);
    854      1.21       chs 
    855      1.21       chs 		racount = 1 << (16 - PAGE_SHIFT);
    856      1.21       chs 		(void) VOP_GETPAGES(vp, raoffset + 0x10000, NULL, &racount, 0,
    857      1.21       chs 				    VM_PROT_READ, 0, 0);
    858      1.21       chs 		simple_lock(&uobj->vmobjlock);
    859      1.21       chs 	}
    860      1.21       chs 
    861      1.21       chs 	/*
    862      1.21       chs 	 * we're almost done!  release the pages...
    863      1.21       chs 	 * for errors, we free the pages.
    864      1.21       chs 	 * otherwise we activate them and mark them as valid and clean.
    865      1.21       chs 	 * also, unbusy pages that were not actually requested.
    866      1.21       chs 	 */
    867      1.21       chs 
    868      1.21       chs out:
    869      1.21       chs 	if (error) {
    870      1.21       chs 		uvm_lock_pageq();
    871      1.21       chs 		for (i = 0; i < npages; i++) {
    872      1.21       chs 			if (pgs[i] == NULL) {
    873      1.21       chs 				continue;
    874      1.21       chs 			}
    875      1.21       chs 			UVMHIST_LOG(ubchist, "examining pg %p flags 0x%x",
    876      1.21       chs 				    pgs[i], pgs[i]->flags, 0,0);
    877      1.26       chs 			if (pgs[i]->flags & PG_WANTED) {
    878      1.26       chs 				wakeup(pgs[i]);
    879      1.26       chs 			}
    880      1.26       chs 			if (pgs[i]->flags & PG_RELEASED) {
    881      1.26       chs 				uvm_unlock_pageq();
    882      1.26       chs 				(uobj->pgops->pgo_releasepg)(pgs[i], NULL);
    883      1.26       chs 				uvm_lock_pageq();
    884      1.21       chs 				continue;
    885      1.21       chs 			}
    886      1.26       chs 			if (pgs[i]->flags & PG_FAKE) {
    887      1.26       chs 				uvm_pagefree(pgs[i]);
    888      1.29       chs 				continue;
    889      1.21       chs 			}
    890      1.29       chs 			uvm_pageactivate(pgs[i]);
    891      1.29       chs 			pgs[i]->flags &= ~(PG_WANTED|PG_BUSY);
    892      1.29       chs 			UVM_PAGE_OWN(pgs[i], NULL);
    893      1.21       chs 		}
    894      1.21       chs 		uvm_unlock_pageq();
    895      1.21       chs 		simple_unlock(&uobj->vmobjlock);
    896      1.21       chs 		UVMHIST_LOG(ubchist, "returning error %d", error,0,0,0);
    897      1.21       chs 		return error;
    898      1.21       chs 	}
    899      1.21       chs 
    900      1.21       chs 	UVMHIST_LOG(ubchist, "succeeding, npages %d", npages,0,0,0);
    901      1.26       chs 	uvm_lock_pageq();
    902      1.21       chs 	for (i = 0; i < npages; i++) {
    903      1.21       chs 		if (pgs[i] == NULL) {
    904      1.21       chs 			continue;
    905      1.21       chs 		}
    906      1.21       chs 		UVMHIST_LOG(ubchist, "examining pg %p flags 0x%x",
    907      1.21       chs 			    pgs[i], pgs[i]->flags, 0,0);
    908      1.21       chs 		if (pgs[i]->flags & PG_FAKE) {
    909      1.21       chs 			UVMHIST_LOG(ubchist, "unfaking pg %p offset 0x%x",
    910      1.21       chs 				    pgs[i], pgs[i]->offset,0,0);
    911      1.21       chs 			pgs[i]->flags &= ~(PG_FAKE);
    912      1.21       chs 			pmap_clear_modify(pgs[i]);
    913      1.21       chs 			pmap_clear_reference(pgs[i]);
    914      1.21       chs 		}
    915      1.21       chs 		if (write) {
    916      1.21       chs 			pgs[i]->flags &= ~(PG_RDONLY);
    917      1.21       chs 		}
    918      1.21       chs 		if (i < ridx || i >= ridx + orignpages || async) {
    919      1.21       chs 			UVMHIST_LOG(ubchist, "unbusy pg %p offset 0x%x",
    920      1.21       chs 				    pgs[i], pgs[i]->offset,0,0);
    921      1.21       chs 			if (pgs[i]->flags & PG_WANTED) {
    922      1.21       chs 				wakeup(pgs[i]);
    923      1.21       chs 			}
    924      1.26       chs 			if (pgs[i]->flags & PG_RELEASED) {
    925      1.26       chs 				uvm_unlock_pageq();
    926      1.26       chs 				(uobj->pgops->pgo_releasepg)(pgs[i], NULL);
    927      1.26       chs 				uvm_lock_pageq();
    928      1.26       chs 				continue;
    929      1.21       chs 			}
    930      1.26       chs 			uvm_pageactivate(pgs[i]);
    931      1.21       chs 			pgs[i]->flags &= ~(PG_WANTED|PG_BUSY);
    932      1.21       chs 			UVM_PAGE_OWN(pgs[i], NULL);
    933      1.21       chs 		}
    934      1.21       chs 	}
    935      1.26       chs 	uvm_unlock_pageq();
    936      1.21       chs 	simple_unlock(&uobj->vmobjlock);
    937      1.21       chs 	if (ap->a_m != NULL) {
    938      1.21       chs 		memcpy(ap->a_m, &pgs[ridx],
    939      1.21       chs 		       orignpages * sizeof(struct vm_page *));
    940      1.21       chs 	}
    941      1.21       chs 	return 0;
    942      1.21       chs }
    943      1.21       chs 
    944      1.21       chs /*
    945      1.21       chs  * generic VM putpages routine.
    946      1.21       chs  * Write the given range of pages to backing store.
    947      1.21       chs  */
    948      1.21       chs 
    949      1.21       chs int
    950      1.21       chs genfs_putpages(v)
    951      1.21       chs 	void *v;
    952      1.21       chs {
    953      1.21       chs 	struct vop_putpages_args /* {
    954      1.21       chs 		struct vnode *a_vp;
    955      1.21       chs 		struct vm_page **a_m;
    956      1.21       chs 		int a_count;
    957      1.21       chs 		int a_flags;
    958      1.21       chs 		int *a_rtvals;
    959      1.21       chs 	} */ *ap = v;
    960      1.21       chs 
    961      1.29       chs 	int s, error, npages, run;
    962      1.21       chs 	int fs_bshift, dev_bshift, dev_bsize;
    963      1.21       chs 	vaddr_t kva;
    964      1.21       chs 	off_t eof, offset, startoffset;
    965      1.21       chs 	size_t bytes, iobytes, skipbytes;
    966      1.21       chs 	daddr_t lbn, blkno;
    967      1.21       chs 	struct vm_page *pg;
    968      1.21       chs 	struct buf *mbp, *bp;
    969      1.21       chs 	struct vnode *vp = ap->a_vp;
    970  1.35.2.1   thorpej 	struct vnode *devvp;
    971      1.21       chs 	boolean_t async = (ap->a_flags & PGO_SYNCIO) == 0;
    972      1.21       chs 	UVMHIST_FUNC("genfs_putpages"); UVMHIST_CALLED(ubchist);
    973      1.29       chs 	UVMHIST_LOG(ubchist, "vp %p offset 0x%x count %d",
    974      1.29       chs 		    vp, ap->a_m[0]->offset, ap->a_count, 0);
    975      1.21       chs 
    976      1.21       chs 	simple_unlock(&vp->v_uvm.u_obj.vmobjlock);
    977      1.21       chs 
    978      1.21       chs 	error = VOP_SIZE(vp, vp->v_uvm.u_size, &eof);
    979      1.21       chs 	if (error) {
    980      1.21       chs 		return error;
    981      1.21       chs 	}
    982      1.21       chs 
    983      1.29       chs 	error = 0;
    984      1.21       chs 	npages = ap->a_count;
    985  1.35.2.1   thorpej 	if (vp->v_type == VREG) {
    986  1.35.2.1   thorpej 		fs_bshift = vp->v_mount->mnt_fs_bshift;
    987  1.35.2.1   thorpej 		dev_bshift = vp->v_mount->mnt_dev_bshift;
    988  1.35.2.1   thorpej 	} else {
    989  1.35.2.1   thorpej 		fs_bshift = DEV_BSHIFT;
    990  1.35.2.1   thorpej 		dev_bshift = DEV_BSHIFT;
    991  1.35.2.1   thorpej 	}
    992      1.21       chs 	dev_bsize = 1 << dev_bshift;
    993      1.21       chs 	KASSERT((eof & (dev_bsize - 1)) == 0);
    994      1.21       chs 
    995      1.21       chs 	pg = ap->a_m[0];
    996      1.21       chs 	startoffset = pg->offset;
    997      1.26       chs 	bytes = MIN(npages << PAGE_SHIFT, eof - startoffset);
    998      1.21       chs 	skipbytes = 0;
    999      1.21       chs 	KASSERT(bytes != 0);
   1000      1.21       chs 
   1001      1.21       chs 	kva = uvm_pagermapin(ap->a_m, npages, UVMPAGER_MAPIN_WAITOK);
   1002      1.21       chs 
   1003      1.21       chs 	s = splbio();
   1004      1.21       chs 	vp->v_numoutput += 2;
   1005      1.21       chs 	mbp = pool_get(&bufpool, PR_WAITOK);
   1006      1.21       chs 	UVMHIST_LOG(ubchist, "vp %p mbp %p num now %d bytes 0x%x",
   1007      1.21       chs 		    vp, mbp, vp->v_numoutput, bytes);
   1008      1.21       chs 	splx(s);
   1009      1.21       chs 	mbp->b_bufsize = npages << PAGE_SHIFT;
   1010      1.21       chs 	mbp->b_data = (void *)kva;
   1011      1.21       chs 	mbp->b_resid = mbp->b_bcount = bytes;
   1012      1.21       chs 	mbp->b_flags = B_BUSY|B_WRITE|B_AGE |
   1013      1.21       chs 		(async ? B_CALL : 0) |
   1014      1.21       chs 		(curproc == uvm.pagedaemon_proc ? B_PDAEMON : 0);
   1015      1.21       chs 	mbp->b_iodone = uvm_aio_biodone;
   1016      1.21       chs 	mbp->b_vp = vp;
   1017      1.21       chs 	LIST_INIT(&mbp->b_dep);
   1018      1.21       chs 
   1019      1.21       chs 	bp = NULL;
   1020      1.21       chs 	for (offset = startoffset;
   1021      1.21       chs 	     bytes > 0;
   1022      1.21       chs 	     offset += iobytes, bytes -= iobytes) {
   1023      1.21       chs 		lbn = offset >> fs_bshift;
   1024  1.35.2.1   thorpej 		error = VOP_BMAP(vp, lbn, &devvp, &blkno, &run);
   1025      1.21       chs 		if (error) {
   1026      1.21       chs 			UVMHIST_LOG(ubchist, "VOP_BMAP() -> %d", error,0,0,0);
   1027      1.21       chs 			skipbytes += bytes;
   1028      1.21       chs 			bytes = 0;
   1029      1.21       chs 			break;
   1030      1.21       chs 		}
   1031      1.21       chs 
   1032      1.26       chs 		iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
   1033      1.26       chs 		    bytes);
   1034      1.21       chs 		if (blkno == (daddr_t)-1) {
   1035      1.21       chs 			skipbytes += iobytes;
   1036      1.21       chs 			continue;
   1037      1.21       chs 		}
   1038      1.21       chs 
   1039      1.21       chs 		/* if it's really one i/o, don't make a second buf */
   1040      1.21       chs 		if (offset == startoffset && iobytes == bytes) {
   1041      1.21       chs 			bp = mbp;
   1042      1.21       chs 		} else {
   1043      1.21       chs 			s = splbio();
   1044      1.21       chs 			vp->v_numoutput++;
   1045      1.21       chs 			bp = pool_get(&bufpool, PR_WAITOK);
   1046      1.21       chs 			UVMHIST_LOG(ubchist, "vp %p bp %p num now %d",
   1047      1.21       chs 				    vp, bp, vp->v_numoutput, 0);
   1048      1.21       chs 			splx(s);
   1049      1.21       chs 			bp->b_data = (char *)kva +
   1050      1.21       chs 				(vaddr_t)(offset - pg->offset);
   1051      1.21       chs 			bp->b_resid = bp->b_bcount = iobytes;
   1052      1.21       chs 			bp->b_flags = B_BUSY|B_WRITE|B_CALL|B_ASYNC;
   1053      1.21       chs 			bp->b_iodone = uvm_aio_biodone1;
   1054      1.21       chs 			bp->b_vp = vp;
   1055      1.21       chs 			LIST_INIT(&bp->b_dep);
   1056      1.21       chs 		}
   1057      1.21       chs 		bp->b_lblkno = 0;
   1058      1.21       chs 		bp->b_private = mbp;
   1059  1.35.2.1   thorpej 		bp->b_dev = devvp->v_rdev;
   1060      1.21       chs 
   1061      1.21       chs 		/* adjust physical blkno for partial blocks */
   1062      1.25      fvdl 		bp->b_blkno = blkno + ((offset - ((off_t)lbn << fs_bshift)) >>
   1063      1.21       chs 				       dev_bshift);
   1064      1.21       chs 		UVMHIST_LOG(ubchist, "vp %p offset 0x%x bcount 0x%x blkno 0x%x",
   1065      1.21       chs 			    vp, offset, bp->b_bcount, bp->b_blkno);
   1066      1.21       chs 		VOP_STRATEGY(bp);
   1067      1.21       chs 	}
   1068      1.21       chs 	if (skipbytes) {
   1069      1.29       chs 		UVMHIST_LOG(ubchist, "skipbytes %d", skipbytes, 0,0,0);
   1070      1.21       chs 		s = splbio();
   1071      1.21       chs 		mbp->b_resid -= skipbytes;
   1072      1.29       chs 		if (error) {
   1073      1.29       chs 			mbp->b_flags |= B_ERROR;
   1074      1.29       chs 			mbp->b_error = error;
   1075      1.29       chs 		}
   1076      1.21       chs 		if (mbp->b_resid == 0) {
   1077      1.21       chs 			biodone(mbp);
   1078      1.21       chs 		}
   1079      1.21       chs 		splx(s);
   1080      1.21       chs 	}
   1081      1.21       chs 	if (async) {
   1082      1.32       chs 		UVMHIST_LOG(ubchist, "returning 0 (async)", 0,0,0,0);
   1083      1.32       chs 		return 0;
   1084      1.21       chs 	}
   1085      1.21       chs 	if (bp != NULL) {
   1086      1.21       chs 		UVMHIST_LOG(ubchist, "waiting for mbp %p", mbp,0,0,0);
   1087      1.29       chs 		error = biowait(mbp);
   1088      1.21       chs 	}
   1089      1.22       chs 	if (bioops.io_pageiodone) {
   1090      1.22       chs 		(*bioops.io_pageiodone)(mbp);
   1091      1.21       chs 	}
   1092      1.21       chs 	s = splbio();
   1093      1.21       chs 	vwakeup(mbp);
   1094      1.21       chs 	pool_put(&bufpool, mbp);
   1095      1.21       chs 	splx(s);
   1096      1.21       chs 	uvm_pagermapout(kva, npages);
   1097      1.21       chs 	UVMHIST_LOG(ubchist, "returning, error %d", error,0,0,0);
   1098      1.29       chs 	return error;
   1099      1.21       chs }
   1100      1.21       chs 
   1101      1.21       chs int
   1102      1.21       chs genfs_size(v)
   1103      1.21       chs 	void *v;
   1104      1.21       chs {
   1105      1.21       chs 	struct vop_size_args /* {
   1106      1.21       chs 		struct vnode *a_vp;
   1107      1.21       chs 		off_t a_size;
   1108      1.21       chs 		off_t *a_eobp;
   1109      1.21       chs 	} */ *ap = v;
   1110      1.21       chs 	int bsize;
   1111      1.21       chs 
   1112      1.21       chs 	bsize = 1 << ap->a_vp->v_mount->mnt_fs_bshift;
   1113      1.24       chs 	*ap->a_eobp = (ap->a_size + bsize - 1) & ~(bsize - 1);
   1114      1.21       chs 	return 0;
   1115       1.1   mycroft }
   1116