Home | History | Annotate | Line # | Download | only in genfs
genfs_vnops.c revision 1.13.2.5
      1  1.13.2.5    bouyer /*	$NetBSD: genfs_vnops.c,v 1.13.2.5 2001/02/11 19:16:58 bouyer 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.13.2.2    bouyer #include <uvm/uvm.h>
     54  1.13.2.2    bouyer #include <uvm/uvm_pager.h>
     55  1.13.2.2    bouyer 
     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.13.2.1    bouyer 		off_t offlo;
     86  1.13.2.1    bouyer 		off_t offhi;
     87       1.1   mycroft 		struct proc *a_p;
     88       1.1   mycroft 	} */ *ap = v;
     89  1.13.2.1    bouyer 	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.13.2.1    bouyer 		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.13.2.1    bouyer 		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.13.2.1    bouyer  * 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.13.2.2    bouyer }
    420  1.13.2.2    bouyer 
    421  1.13.2.2    bouyer /*
    422  1.13.2.2    bouyer  * generic VM getpages routine.
    423  1.13.2.2    bouyer  * Return PG_BUSY pages for the given range,
    424  1.13.2.2    bouyer  * reading from backing store if necessary.
    425  1.13.2.2    bouyer  */
    426  1.13.2.2    bouyer 
    427  1.13.2.2    bouyer int
    428  1.13.2.2    bouyer genfs_getpages(v)
    429  1.13.2.2    bouyer 	void *v;
    430  1.13.2.2    bouyer {
    431  1.13.2.2    bouyer 	struct vop_getpages_args /* {
    432  1.13.2.2    bouyer 		struct vnode *a_vp;
    433  1.13.2.2    bouyer 		voff_t a_offset;
    434  1.13.2.2    bouyer 		vm_page_t *a_m;
    435  1.13.2.2    bouyer 		int *a_count;
    436  1.13.2.2    bouyer 		int a_centeridx;
    437  1.13.2.2    bouyer 		vm_prot_t a_access_type;
    438  1.13.2.2    bouyer 		int a_advice;
    439  1.13.2.2    bouyer 		int a_flags;
    440  1.13.2.2    bouyer 	} */ *ap = v;
    441  1.13.2.2    bouyer 
    442  1.13.2.5    bouyer 	off_t newsize, eof;
    443  1.13.2.5    bouyer 	off_t offset, origoffset, startoffset, endoffset, raoffset;
    444  1.13.2.2    bouyer 	daddr_t lbn, blkno;
    445  1.13.2.2    bouyer 	int s, i, error, npages, orignpages, npgs, run, ridx, pidx, pcount;
    446  1.13.2.2    bouyer 	int fs_bshift, fs_bsize, dev_bshift, dev_bsize;
    447  1.13.2.2    bouyer 	int flags = ap->a_flags;
    448  1.13.2.2    bouyer 	size_t bytes, iobytes, tailbytes, totalbytes, skipbytes;
    449  1.13.2.2    bouyer 	vaddr_t kva;
    450  1.13.2.2    bouyer 	struct buf *bp, *mbp;
    451  1.13.2.2    bouyer 	struct vnode *vp = ap->a_vp;
    452  1.13.2.2    bouyer 	struct uvm_object *uobj = &vp->v_uvm.u_obj;
    453  1.13.2.2    bouyer 	struct vm_page *pgs[16];			/* XXXUBC 16 */
    454  1.13.2.2    bouyer 	struct ucred *cred = curproc->p_ucred;		/* XXXUBC curproc */
    455  1.13.2.2    bouyer 	boolean_t async = (flags & PGO_SYNCIO) == 0;
    456  1.13.2.2    bouyer 	boolean_t write = (ap->a_access_type & VM_PROT_WRITE) != 0;
    457  1.13.2.2    bouyer 	boolean_t sawhole = FALSE;
    458  1.13.2.2    bouyer 	UVMHIST_FUNC("genfs_getpages"); UVMHIST_CALLED(ubchist);
    459  1.13.2.2    bouyer 
    460  1.13.2.2    bouyer 	/* XXXUBC temp limit */
    461  1.13.2.2    bouyer 	if (*ap->a_count > 16) {
    462  1.13.2.2    bouyer 		return EINVAL;
    463  1.13.2.2    bouyer 	}
    464  1.13.2.2    bouyer 
    465  1.13.2.5    bouyer 	error = 0;
    466  1.13.2.5    bouyer 	origoffset = ap->a_offset;
    467  1.13.2.5    bouyer 	orignpages = *ap->a_count;
    468  1.13.2.5    bouyer 	if (flags & PGO_PASTEOF) {
    469  1.13.2.5    bouyer 		newsize = MAX(vp->v_uvm.u_size,
    470  1.13.2.5    bouyer 			      origoffset + (orignpages << PAGE_SHIFT));
    471  1.13.2.5    bouyer 	} else {
    472  1.13.2.5    bouyer 		newsize = vp->v_uvm.u_size;
    473  1.13.2.5    bouyer 	}
    474  1.13.2.5    bouyer 	error = VOP_SIZE(vp, newsize, &eof);
    475  1.13.2.2    bouyer 	if (error) {
    476  1.13.2.2    bouyer 		return error;
    477  1.13.2.2    bouyer 	}
    478  1.13.2.2    bouyer 
    479  1.13.2.2    bouyer #ifdef DIAGNOSTIC
    480  1.13.2.2    bouyer 	if (ap->a_centeridx < 0 || ap->a_centeridx > *ap->a_count) {
    481  1.13.2.2    bouyer 		panic("genfs_getpages: centeridx %d out of range",
    482  1.13.2.2    bouyer 		      ap->a_centeridx);
    483  1.13.2.2    bouyer 	}
    484  1.13.2.5    bouyer 	if (origoffset & (PAGE_SIZE - 1) || origoffset < 0) {
    485  1.13.2.2    bouyer 		panic("genfs_getpages: offset 0x%x", (int)ap->a_offset);
    486  1.13.2.2    bouyer 	}
    487  1.13.2.2    bouyer 	if (*ap->a_count < 0) {
    488  1.13.2.2    bouyer 		panic("genfs_getpages: count %d < 0", *ap->a_count);
    489  1.13.2.2    bouyer 	}
    490  1.13.2.2    bouyer #endif
    491  1.13.2.2    bouyer 
    492  1.13.2.2    bouyer 	/*
    493  1.13.2.2    bouyer 	 * Bounds-check the request.
    494  1.13.2.2    bouyer 	 */
    495  1.13.2.2    bouyer 
    496  1.13.2.2    bouyer 	if (origoffset + (ap->a_centeridx << PAGE_SHIFT) >= eof &&
    497  1.13.2.2    bouyer 	    (flags & PGO_PASTEOF) == 0) {
    498  1.13.2.2    bouyer 		if ((flags & PGO_LOCKED) == 0) {
    499  1.13.2.2    bouyer 			simple_unlock(&uobj->vmobjlock);
    500  1.13.2.2    bouyer 		}
    501  1.13.2.2    bouyer 		UVMHIST_LOG(ubchist, "off 0x%x count %d goes past EOF 0x%x",
    502  1.13.2.2    bouyer 			    origoffset, *ap->a_count, eof,0);
    503  1.13.2.2    bouyer 		return EINVAL;
    504  1.13.2.2    bouyer 	}
    505  1.13.2.2    bouyer 
    506  1.13.2.2    bouyer 	/*
    507  1.13.2.2    bouyer 	 * For PGO_LOCKED requests, just return whatever's in memory.
    508  1.13.2.2    bouyer 	 */
    509  1.13.2.2    bouyer 
    510  1.13.2.2    bouyer 	if (flags & PGO_LOCKED) {
    511  1.13.2.2    bouyer 		uvn_findpages(uobj, origoffset, ap->a_count, ap->a_m,
    512  1.13.2.2    bouyer 			      UFP_NOWAIT|UFP_NOALLOC|UFP_NORDONLY);
    513  1.13.2.2    bouyer 
    514  1.13.2.2    bouyer 		return ap->a_m[ap->a_centeridx] == NULL ? EBUSY : 0;
    515  1.13.2.2    bouyer 	}
    516  1.13.2.2    bouyer 
    517  1.13.2.2    bouyer 	/* vnode is VOP_LOCKed, uobj is locked */
    518  1.13.2.2    bouyer 
    519  1.13.2.2    bouyer 	if (write && (vp->v_flag & VONWORKLST) == 0) {
    520  1.13.2.2    bouyer 		vn_syncer_add_to_worklist(vp, filedelay);
    521  1.13.2.2    bouyer 	}
    522  1.13.2.2    bouyer 
    523  1.13.2.2    bouyer 	/*
    524  1.13.2.2    bouyer 	 * find the requested pages and make some simple checks.
    525  1.13.2.2    bouyer 	 * leave space in the page array for a whole block.
    526  1.13.2.2    bouyer 	 */
    527  1.13.2.2    bouyer 
    528  1.13.2.2    bouyer 	fs_bshift = vp->v_mount->mnt_fs_bshift;
    529  1.13.2.2    bouyer 	fs_bsize = 1 << fs_bshift;
    530  1.13.2.2    bouyer 	dev_bshift = vp->v_mount->mnt_dev_bshift;
    531  1.13.2.2    bouyer 	dev_bsize = 1 << dev_bshift;
    532  1.13.2.2    bouyer 	KASSERT((eof & (dev_bsize - 1)) == 0);
    533  1.13.2.2    bouyer 
    534  1.13.2.5    bouyer 	if ((flags & PGO_PASTEOF) == 0) {
    535  1.13.2.5    bouyer 		orignpages = MIN(orignpages,
    536  1.13.2.5    bouyer 		    round_page(eof - origoffset) >> PAGE_SHIFT);
    537  1.13.2.2    bouyer 	}
    538  1.13.2.2    bouyer 	npages = orignpages;
    539  1.13.2.2    bouyer 	startoffset = origoffset & ~(fs_bsize - 1);
    540  1.13.2.2    bouyer 	endoffset = round_page((origoffset + (npages << PAGE_SHIFT)
    541  1.13.2.2    bouyer 				+ fs_bsize - 1) & ~(fs_bsize - 1));
    542  1.13.2.5    bouyer 	if ((flags & PGO_PASTEOF) == 0) {
    543  1.13.2.5    bouyer 		endoffset = MIN(endoffset, round_page(eof));
    544  1.13.2.5    bouyer 	}
    545  1.13.2.2    bouyer 	ridx = (origoffset - startoffset) >> PAGE_SHIFT;
    546  1.13.2.2    bouyer 
    547  1.13.2.2    bouyer 	memset(pgs, 0, sizeof(pgs));
    548  1.13.2.2    bouyer 	uvn_findpages(uobj, origoffset, &npages, &pgs[ridx], UFP_ALL);
    549  1.13.2.2    bouyer 
    550  1.13.2.2    bouyer 	/*
    551  1.13.2.2    bouyer 	 * if PGO_OVERWRITE is set, don't bother reading the pages.
    552  1.13.2.2    bouyer 	 * PGO_OVERWRITE also means that the caller guarantees
    553  1.13.2.2    bouyer 	 * that the pages already have backing store allocated.
    554  1.13.2.2    bouyer 	 */
    555  1.13.2.2    bouyer 
    556  1.13.2.2    bouyer 	if (flags & PGO_OVERWRITE) {
    557  1.13.2.2    bouyer 		UVMHIST_LOG(ubchist, "PGO_OVERWRITE",0,0,0,0);
    558  1.13.2.2    bouyer 
    559  1.13.2.2    bouyer 		for (i = 0; i < npages; i++) {
    560  1.13.2.2    bouyer 			struct vm_page *pg = pgs[ridx + i];
    561  1.13.2.2    bouyer 
    562  1.13.2.2    bouyer 			if (pg->flags & PG_FAKE) {
    563  1.13.2.2    bouyer 				uvm_pagezero(pg);
    564  1.13.2.2    bouyer 				pg->flags &= ~(PG_FAKE);
    565  1.13.2.2    bouyer 			}
    566  1.13.2.2    bouyer 			pg->flags &= ~(PG_RDONLY);
    567  1.13.2.2    bouyer 		}
    568  1.13.2.5    bouyer 		npages += ridx;
    569  1.13.2.2    bouyer 		goto out;
    570  1.13.2.2    bouyer 	}
    571  1.13.2.2    bouyer 
    572  1.13.2.2    bouyer 	/*
    573  1.13.2.2    bouyer 	 * if the pages are already resident, just return them.
    574  1.13.2.2    bouyer 	 */
    575  1.13.2.2    bouyer 
    576  1.13.2.2    bouyer 	for (i = 0; i < npages; i++) {
    577  1.13.2.2    bouyer 		struct vm_page *pg = pgs[ridx + i];
    578  1.13.2.2    bouyer 
    579  1.13.2.2    bouyer 		if ((pg->flags & PG_FAKE) ||
    580  1.13.2.2    bouyer 		    (write && (pg->flags & PG_RDONLY))) {
    581  1.13.2.2    bouyer 			break;
    582  1.13.2.2    bouyer 		}
    583  1.13.2.2    bouyer 	}
    584  1.13.2.2    bouyer 	if (i == npages) {
    585  1.13.2.2    bouyer 		UVMHIST_LOG(ubchist, "returning cached pages", 0,0,0,0);
    586  1.13.2.2    bouyer 		raoffset = origoffset + (orignpages << PAGE_SHIFT);
    587  1.13.2.5    bouyer 		npages += ridx;
    588  1.13.2.2    bouyer 		goto raout;
    589  1.13.2.2    bouyer 	}
    590  1.13.2.2    bouyer 
    591  1.13.2.2    bouyer 	/*
    592  1.13.2.2    bouyer 	 * the page wasn't resident and we're not overwriting,
    593  1.13.2.2    bouyer 	 * so we're going to have to do some i/o.
    594  1.13.2.2    bouyer 	 * find any additional pages needed to cover the expanded range.
    595  1.13.2.2    bouyer 	 */
    596  1.13.2.2    bouyer 
    597  1.13.2.2    bouyer 	if (startoffset != origoffset) {
    598  1.13.2.2    bouyer 
    599  1.13.2.2    bouyer 		/*
    600  1.13.2.2    bouyer 		 * XXXUBC we need to avoid deadlocks caused by locking
    601  1.13.2.2    bouyer 		 * additional pages at lower offsets than pages we
    602  1.13.2.2    bouyer 		 * already have locked.  for now, unlock them all and
    603  1.13.2.2    bouyer 		 * start over.
    604  1.13.2.2    bouyer 		 */
    605  1.13.2.2    bouyer 
    606  1.13.2.2    bouyer 		for (i = 0; i < npages; i++) {
    607  1.13.2.2    bouyer 			struct vm_page *pg = pgs[ridx + i];
    608  1.13.2.2    bouyer 
    609  1.13.2.2    bouyer 			if (pg->flags & PG_FAKE) {
    610  1.13.2.2    bouyer 				pg->flags |= PG_RELEASED;
    611  1.13.2.2    bouyer 			}
    612  1.13.2.2    bouyer 		}
    613  1.13.2.2    bouyer 		uvm_page_unbusy(&pgs[ridx], npages);
    614  1.13.2.2    bouyer 		memset(pgs, 0, sizeof(pgs));
    615  1.13.2.2    bouyer 
    616  1.13.2.2    bouyer 		UVMHIST_LOG(ubchist, "reset npages start 0x%x end 0x%x",
    617  1.13.2.2    bouyer 			    startoffset, endoffset, 0,0);
    618  1.13.2.2    bouyer 		npages = (endoffset - startoffset) >> PAGE_SHIFT;
    619  1.13.2.2    bouyer 		npgs = npages;
    620  1.13.2.2    bouyer 		uvn_findpages(uobj, startoffset, &npgs, pgs, UFP_ALL);
    621  1.13.2.2    bouyer 	}
    622  1.13.2.2    bouyer 	simple_unlock(&uobj->vmobjlock);
    623  1.13.2.2    bouyer 
    624  1.13.2.2    bouyer 	/*
    625  1.13.2.2    bouyer 	 * read the desired page(s).
    626  1.13.2.2    bouyer 	 */
    627  1.13.2.2    bouyer 
    628  1.13.2.2    bouyer 	totalbytes = npages << PAGE_SHIFT;
    629  1.13.2.5    bouyer 	bytes = MIN(totalbytes, eof - startoffset);
    630  1.13.2.2    bouyer 	tailbytes = totalbytes - bytes;
    631  1.13.2.2    bouyer 	skipbytes = 0;
    632  1.13.2.2    bouyer 
    633  1.13.2.2    bouyer 	kva = uvm_pagermapin(pgs, npages, UVMPAGER_MAPIN_WAITOK |
    634  1.13.2.2    bouyer 			     UVMPAGER_MAPIN_READ);
    635  1.13.2.2    bouyer 
    636  1.13.2.2    bouyer 	s = splbio();
    637  1.13.2.2    bouyer 	mbp = pool_get(&bufpool, PR_WAITOK);
    638  1.13.2.2    bouyer 	splx(s);
    639  1.13.2.2    bouyer 	mbp->b_bufsize = totalbytes;
    640  1.13.2.2    bouyer 	mbp->b_data = (void *)kva;
    641  1.13.2.2    bouyer 	mbp->b_resid = mbp->b_bcount = bytes;
    642  1.13.2.2    bouyer 	mbp->b_flags = B_BUSY|B_READ| (async ? B_CALL : 0);
    643  1.13.2.2    bouyer 	mbp->b_iodone = uvm_aio_biodone;
    644  1.13.2.2    bouyer 	mbp->b_vp = vp;
    645  1.13.2.2    bouyer 	LIST_INIT(&mbp->b_dep);
    646  1.13.2.2    bouyer 
    647  1.13.2.2    bouyer 	/*
    648  1.13.2.2    bouyer 	 * if EOF is in the middle of the last page, zero the part past EOF.
    649  1.13.2.2    bouyer 	 */
    650  1.13.2.2    bouyer 
    651  1.13.2.3    bouyer 	if (tailbytes > 0 && (pgs[bytes >> PAGE_SHIFT]->flags & PG_FAKE)) {
    652  1.13.2.2    bouyer 		memset((void *)(kva + bytes), 0, tailbytes);
    653  1.13.2.2    bouyer 	}
    654  1.13.2.2    bouyer 
    655  1.13.2.2    bouyer 	/*
    656  1.13.2.2    bouyer 	 * now loop over the pages, reading as needed.
    657  1.13.2.2    bouyer 	 */
    658  1.13.2.2    bouyer 
    659  1.13.2.2    bouyer 	if (write) {
    660  1.13.2.2    bouyer 		lockmgr(&vp->v_glock, LK_EXCLUSIVE, NULL);
    661  1.13.2.2    bouyer 	} else {
    662  1.13.2.2    bouyer 		lockmgr(&vp->v_glock, LK_SHARED, NULL);
    663  1.13.2.2    bouyer 	}
    664  1.13.2.2    bouyer 
    665  1.13.2.2    bouyer 	bp = NULL;
    666  1.13.2.2    bouyer 	for (offset = startoffset;
    667  1.13.2.2    bouyer 	     bytes > 0;
    668  1.13.2.2    bouyer 	     offset += iobytes, bytes -= iobytes) {
    669  1.13.2.2    bouyer 
    670  1.13.2.2    bouyer 		/*
    671  1.13.2.2    bouyer 		 * skip pages which don't need to be read.
    672  1.13.2.2    bouyer 		 */
    673  1.13.2.2    bouyer 
    674  1.13.2.2    bouyer 		pidx = (offset - startoffset) >> PAGE_SHIFT;
    675  1.13.2.2    bouyer 		while ((pgs[pidx]->flags & PG_FAKE) == 0) {
    676  1.13.2.2    bouyer 			size_t b;
    677  1.13.2.2    bouyer 
    678  1.13.2.4    bouyer 			KASSERT((offset & (PAGE_SIZE - 1)) == 0);
    679  1.13.2.5    bouyer 			b = MIN(PAGE_SIZE, bytes);
    680  1.13.2.2    bouyer 			offset += b;
    681  1.13.2.2    bouyer 			bytes -= b;
    682  1.13.2.2    bouyer 			skipbytes += b;
    683  1.13.2.2    bouyer 			pidx++;
    684  1.13.2.2    bouyer 			UVMHIST_LOG(ubchist, "skipping, new offset 0x%x",
    685  1.13.2.2    bouyer 				    offset, 0,0,0);
    686  1.13.2.2    bouyer 			if (bytes == 0) {
    687  1.13.2.2    bouyer 				goto loopdone;
    688  1.13.2.2    bouyer 			}
    689  1.13.2.2    bouyer 		}
    690  1.13.2.2    bouyer 
    691  1.13.2.2    bouyer 		/*
    692  1.13.2.2    bouyer 		 * bmap the file to find out the blkno to read from and
    693  1.13.2.2    bouyer 		 * how much we can read in one i/o.  if bmap returns an error,
    694  1.13.2.2    bouyer 		 * skip the rest of the top-level i/o.
    695  1.13.2.2    bouyer 		 */
    696  1.13.2.2    bouyer 
    697  1.13.2.2    bouyer 		lbn = offset >> fs_bshift;
    698  1.13.2.2    bouyer 		error = VOP_BMAP(vp, lbn, NULL, &blkno, &run);
    699  1.13.2.2    bouyer 		if (error) {
    700  1.13.2.2    bouyer 			UVMHIST_LOG(ubchist, "VOP_BMAP lbn 0x%x -> %d\n",
    701  1.13.2.2    bouyer 				    lbn, error,0,0);
    702  1.13.2.2    bouyer 			skipbytes += bytes;
    703  1.13.2.2    bouyer 			goto loopdone;
    704  1.13.2.2    bouyer 		}
    705  1.13.2.2    bouyer 
    706  1.13.2.2    bouyer 		/*
    707  1.13.2.2    bouyer 		 * see how many pages can be read with this i/o.
    708  1.13.2.2    bouyer 		 * reduce the i/o size if necessary to avoid
    709  1.13.2.2    bouyer 		 * overwriting pages with valid data.
    710  1.13.2.2    bouyer 		 */
    711  1.13.2.2    bouyer 
    712  1.13.2.5    bouyer 		iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
    713  1.13.2.5    bouyer 		    bytes);
    714  1.13.2.2    bouyer 		if (offset + iobytes > round_page(offset)) {
    715  1.13.2.2    bouyer 			pcount = 1;
    716  1.13.2.2    bouyer 			while (pidx + pcount < npages &&
    717  1.13.2.2    bouyer 			       pgs[pidx + pcount]->flags & PG_FAKE) {
    718  1.13.2.2    bouyer 				pcount++;
    719  1.13.2.2    bouyer 			}
    720  1.13.2.5    bouyer 			iobytes = MIN(iobytes, (pcount << PAGE_SHIFT) -
    721  1.13.2.2    bouyer 				      (offset - trunc_page(offset)));
    722  1.13.2.2    bouyer 		}
    723  1.13.2.2    bouyer 
    724  1.13.2.2    bouyer 		/*
    725  1.13.2.2    bouyer 		 * if this block isn't allocated, zero it instead of reading it.
    726  1.13.2.2    bouyer 		 * if this is a read access, mark the pages we zeroed PG_RDONLY.
    727  1.13.2.2    bouyer 		 */
    728  1.13.2.2    bouyer 
    729  1.13.2.2    bouyer 		if (blkno < 0) {
    730  1.13.2.2    bouyer 			UVMHIST_LOG(ubchist, "lbn 0x%x -> HOLE", lbn,0,0,0);
    731  1.13.2.2    bouyer 
    732  1.13.2.2    bouyer 			sawhole = TRUE;
    733  1.13.2.2    bouyer 			memset((char *)kva + (offset - startoffset), 0,
    734  1.13.2.2    bouyer 			       iobytes);
    735  1.13.2.2    bouyer 			skipbytes += iobytes;
    736  1.13.2.2    bouyer 
    737  1.13.2.2    bouyer 			if (!write) {
    738  1.13.2.2    bouyer 				int holepages =
    739  1.13.2.2    bouyer 					(round_page(offset + iobytes) -
    740  1.13.2.2    bouyer 					 trunc_page(offset)) >> PAGE_SHIFT;
    741  1.13.2.2    bouyer 				for (i = 0; i < holepages; i++) {
    742  1.13.2.2    bouyer 					pgs[pidx + i]->flags |= PG_RDONLY;
    743  1.13.2.2    bouyer 				}
    744  1.13.2.2    bouyer 			}
    745  1.13.2.2    bouyer 			continue;
    746  1.13.2.2    bouyer 		}
    747  1.13.2.2    bouyer 
    748  1.13.2.2    bouyer 		/*
    749  1.13.2.2    bouyer 		 * allocate a sub-buf for this piece of the i/o
    750  1.13.2.2    bouyer 		 * (or just use mbp if there's only 1 piece),
    751  1.13.2.2    bouyer 		 * and start it going.
    752  1.13.2.2    bouyer 		 */
    753  1.13.2.2    bouyer 
    754  1.13.2.2    bouyer 		if (offset == startoffset && iobytes == bytes) {
    755  1.13.2.2    bouyer 			bp = mbp;
    756  1.13.2.2    bouyer 		} else {
    757  1.13.2.2    bouyer 			s = splbio();
    758  1.13.2.2    bouyer 			bp = pool_get(&bufpool, PR_WAITOK);
    759  1.13.2.2    bouyer 			splx(s);
    760  1.13.2.2    bouyer 			bp->b_data = (char *)kva + offset - startoffset;
    761  1.13.2.2    bouyer 			bp->b_resid = bp->b_bcount = iobytes;
    762  1.13.2.2    bouyer 			bp->b_flags = B_BUSY|B_READ|B_CALL;
    763  1.13.2.2    bouyer 			bp->b_iodone = uvm_aio_biodone1;
    764  1.13.2.2    bouyer 			bp->b_vp = vp;
    765  1.13.2.2    bouyer 			LIST_INIT(&bp->b_dep);
    766  1.13.2.2    bouyer 		}
    767  1.13.2.2    bouyer 		bp->b_lblkno = 0;
    768  1.13.2.2    bouyer 		bp->b_private = mbp;
    769  1.13.2.2    bouyer 
    770  1.13.2.2    bouyer 		/* adjust physical blkno for partial blocks */
    771  1.13.2.5    bouyer 		bp->b_blkno = blkno + ((offset - ((off_t)lbn << fs_bshift)) >>
    772  1.13.2.2    bouyer 				       dev_bshift);
    773  1.13.2.2    bouyer 
    774  1.13.2.2    bouyer 		UVMHIST_LOG(ubchist, "bp %p offset 0x%x bcount 0x%x blkno 0x%x",
    775  1.13.2.2    bouyer 			    bp, offset, iobytes, bp->b_blkno);
    776  1.13.2.2    bouyer 
    777  1.13.2.2    bouyer 		VOP_STRATEGY(bp);
    778  1.13.2.2    bouyer 	}
    779  1.13.2.2    bouyer 
    780  1.13.2.2    bouyer loopdone:
    781  1.13.2.2    bouyer 	if (skipbytes) {
    782  1.13.2.2    bouyer 		s = splbio();
    783  1.13.2.2    bouyer 		if (error) {
    784  1.13.2.2    bouyer 			mbp->b_flags |= B_ERROR;
    785  1.13.2.2    bouyer 			mbp->b_error = error;
    786  1.13.2.2    bouyer 		}
    787  1.13.2.2    bouyer 		mbp->b_resid -= skipbytes;
    788  1.13.2.2    bouyer 		if (mbp->b_resid == 0) {
    789  1.13.2.2    bouyer 			biodone(mbp);
    790  1.13.2.2    bouyer 		}
    791  1.13.2.2    bouyer 		splx(s);
    792  1.13.2.2    bouyer 	}
    793  1.13.2.2    bouyer 
    794  1.13.2.2    bouyer 	if (async) {
    795  1.13.2.2    bouyer 		UVMHIST_LOG(ubchist, "returning PEND",0,0,0,0);
    796  1.13.2.2    bouyer 		lockmgr(&vp->v_glock, LK_RELEASE, NULL);
    797  1.13.2.2    bouyer 		return EINPROGRESS;
    798  1.13.2.2    bouyer 	}
    799  1.13.2.2    bouyer 	if (bp != NULL) {
    800  1.13.2.2    bouyer 		error = biowait(mbp);
    801  1.13.2.2    bouyer 	}
    802  1.13.2.2    bouyer 	s = splbio();
    803  1.13.2.2    bouyer 	pool_put(&bufpool, mbp);
    804  1.13.2.2    bouyer 	splx(s);
    805  1.13.2.2    bouyer 	uvm_pagermapout(kva, npages);
    806  1.13.2.4    bouyer 	raoffset = startoffset + totalbytes;
    807  1.13.2.2    bouyer 
    808  1.13.2.2    bouyer 	/*
    809  1.13.2.2    bouyer 	 * if this we encountered a hole then we have to do a little more work.
    810  1.13.2.2    bouyer 	 * for read faults, we marked the page PG_RDONLY so that future
    811  1.13.2.2    bouyer 	 * write accesses to the page will fault again.
    812  1.13.2.2    bouyer 	 * for write faults, we must make sure that the backing store for
    813  1.13.2.2    bouyer 	 * the page is completely allocated while the pages are locked.
    814  1.13.2.2    bouyer 	 */
    815  1.13.2.2    bouyer 
    816  1.13.2.2    bouyer 	if (error == 0 && sawhole && write) {
    817  1.13.2.2    bouyer 		error = VOP_BALLOCN(vp, startoffset, npages << PAGE_SHIFT,
    818  1.13.2.2    bouyer 				   cred, 0);
    819  1.13.2.2    bouyer 		if (error) {
    820  1.13.2.2    bouyer 			UVMHIST_LOG(ubchist, "balloc lbn 0x%x -> %d",
    821  1.13.2.2    bouyer 				    lbn, error,0,0);
    822  1.13.2.2    bouyer 			lockmgr(&vp->v_glock, LK_RELEASE, NULL);
    823  1.13.2.2    bouyer 			simple_lock(&uobj->vmobjlock);
    824  1.13.2.2    bouyer 			goto out;
    825  1.13.2.2    bouyer 		}
    826  1.13.2.2    bouyer 	}
    827  1.13.2.2    bouyer 	lockmgr(&vp->v_glock, LK_RELEASE, NULL);
    828  1.13.2.2    bouyer 	simple_lock(&uobj->vmobjlock);
    829  1.13.2.2    bouyer 
    830  1.13.2.2    bouyer 	/*
    831  1.13.2.2    bouyer 	 * see if we want to start any readahead.
    832  1.13.2.2    bouyer 	 * XXXUBC for now, just read the next 128k on 64k boundaries.
    833  1.13.2.2    bouyer 	 * this is pretty nonsensical, but it is 50% faster than reading
    834  1.13.2.2    bouyer 	 * just the next 64k.
    835  1.13.2.2    bouyer 	 */
    836  1.13.2.2    bouyer 
    837  1.13.2.2    bouyer raout:
    838  1.13.2.4    bouyer 	if (!error && !async && !write && ((int)raoffset & 0xffff) == 0 &&
    839  1.13.2.2    bouyer 	    PAGE_SHIFT <= 16) {
    840  1.13.2.2    bouyer 		int racount;
    841  1.13.2.2    bouyer 
    842  1.13.2.2    bouyer 		racount = 1 << (16 - PAGE_SHIFT);
    843  1.13.2.2    bouyer 		(void) VOP_GETPAGES(vp, raoffset, NULL, &racount, 0,
    844  1.13.2.2    bouyer 				    VM_PROT_READ, 0, 0);
    845  1.13.2.2    bouyer 		simple_lock(&uobj->vmobjlock);
    846  1.13.2.2    bouyer 
    847  1.13.2.2    bouyer 		racount = 1 << (16 - PAGE_SHIFT);
    848  1.13.2.2    bouyer 		(void) VOP_GETPAGES(vp, raoffset + 0x10000, NULL, &racount, 0,
    849  1.13.2.2    bouyer 				    VM_PROT_READ, 0, 0);
    850  1.13.2.2    bouyer 		simple_lock(&uobj->vmobjlock);
    851  1.13.2.2    bouyer 	}
    852  1.13.2.2    bouyer 
    853  1.13.2.2    bouyer 	/*
    854  1.13.2.2    bouyer 	 * we're almost done!  release the pages...
    855  1.13.2.2    bouyer 	 * for errors, we free the pages.
    856  1.13.2.2    bouyer 	 * otherwise we activate them and mark them as valid and clean.
    857  1.13.2.2    bouyer 	 * also, unbusy pages that were not actually requested.
    858  1.13.2.2    bouyer 	 */
    859  1.13.2.2    bouyer 
    860  1.13.2.2    bouyer out:
    861  1.13.2.2    bouyer 	if (error) {
    862  1.13.2.2    bouyer 		uvm_lock_pageq();
    863  1.13.2.2    bouyer 		for (i = 0; i < npages; i++) {
    864  1.13.2.2    bouyer 			if (pgs[i] == NULL) {
    865  1.13.2.2    bouyer 				continue;
    866  1.13.2.2    bouyer 			}
    867  1.13.2.2    bouyer 			UVMHIST_LOG(ubchist, "examining pg %p flags 0x%x",
    868  1.13.2.2    bouyer 				    pgs[i], pgs[i]->flags, 0,0);
    869  1.13.2.2    bouyer 			if (pgs[i]->flags & PG_WANTED) {
    870  1.13.2.2    bouyer 				wakeup(pgs[i]);
    871  1.13.2.2    bouyer 			}
    872  1.13.2.5    bouyer 			if (pgs[i]->flags & PG_RELEASED) {
    873  1.13.2.5    bouyer 				uvm_unlock_pageq();
    874  1.13.2.5    bouyer 				(uobj->pgops->pgo_releasepg)(pgs[i], NULL);
    875  1.13.2.5    bouyer 				uvm_lock_pageq();
    876  1.13.2.5    bouyer 				continue;
    877  1.13.2.5    bouyer 			}
    878  1.13.2.5    bouyer 			if (pgs[i]->flags & PG_FAKE) {
    879  1.13.2.5    bouyer 				uvm_pagefree(pgs[i]);
    880  1.13.2.5    bouyer 			}
    881  1.13.2.2    bouyer 		}
    882  1.13.2.2    bouyer 		uvm_unlock_pageq();
    883  1.13.2.2    bouyer 		simple_unlock(&uobj->vmobjlock);
    884  1.13.2.2    bouyer 		UVMHIST_LOG(ubchist, "returning error %d", error,0,0,0);
    885  1.13.2.2    bouyer 		return error;
    886  1.13.2.2    bouyer 	}
    887  1.13.2.2    bouyer 
    888  1.13.2.2    bouyer 	UVMHIST_LOG(ubchist, "succeeding, npages %d", npages,0,0,0);
    889  1.13.2.5    bouyer 	uvm_lock_pageq();
    890  1.13.2.2    bouyer 	for (i = 0; i < npages; i++) {
    891  1.13.2.2    bouyer 		if (pgs[i] == NULL) {
    892  1.13.2.2    bouyer 			continue;
    893  1.13.2.2    bouyer 		}
    894  1.13.2.2    bouyer 		UVMHIST_LOG(ubchist, "examining pg %p flags 0x%x",
    895  1.13.2.2    bouyer 			    pgs[i], pgs[i]->flags, 0,0);
    896  1.13.2.2    bouyer 		if (pgs[i]->flags & PG_FAKE) {
    897  1.13.2.2    bouyer 			UVMHIST_LOG(ubchist, "unfaking pg %p offset 0x%x",
    898  1.13.2.2    bouyer 				    pgs[i], pgs[i]->offset,0,0);
    899  1.13.2.2    bouyer 			pgs[i]->flags &= ~(PG_FAKE);
    900  1.13.2.2    bouyer 			pmap_clear_modify(pgs[i]);
    901  1.13.2.2    bouyer 			pmap_clear_reference(pgs[i]);
    902  1.13.2.2    bouyer 		}
    903  1.13.2.2    bouyer 		if (write) {
    904  1.13.2.2    bouyer 			pgs[i]->flags &= ~(PG_RDONLY);
    905  1.13.2.2    bouyer 		}
    906  1.13.2.2    bouyer 		if (i < ridx || i >= ridx + orignpages || async) {
    907  1.13.2.2    bouyer 			UVMHIST_LOG(ubchist, "unbusy pg %p offset 0x%x",
    908  1.13.2.2    bouyer 				    pgs[i], pgs[i]->offset,0,0);
    909  1.13.2.2    bouyer 			if (pgs[i]->flags & PG_WANTED) {
    910  1.13.2.2    bouyer 				wakeup(pgs[i]);
    911  1.13.2.2    bouyer 			}
    912  1.13.2.5    bouyer 			if (pgs[i]->flags & PG_RELEASED) {
    913  1.13.2.5    bouyer 				uvm_unlock_pageq();
    914  1.13.2.5    bouyer 				(uobj->pgops->pgo_releasepg)(pgs[i], NULL);
    915  1.13.2.5    bouyer 				uvm_lock_pageq();
    916  1.13.2.5    bouyer 				continue;
    917  1.13.2.2    bouyer 			}
    918  1.13.2.5    bouyer 			uvm_pageactivate(pgs[i]);
    919  1.13.2.2    bouyer 			pgs[i]->flags &= ~(PG_WANTED|PG_BUSY);
    920  1.13.2.2    bouyer 			UVM_PAGE_OWN(pgs[i], NULL);
    921  1.13.2.2    bouyer 		}
    922  1.13.2.2    bouyer 	}
    923  1.13.2.5    bouyer 	uvm_unlock_pageq();
    924  1.13.2.2    bouyer 	simple_unlock(&uobj->vmobjlock);
    925  1.13.2.2    bouyer 	if (ap->a_m != NULL) {
    926  1.13.2.2    bouyer 		memcpy(ap->a_m, &pgs[ridx],
    927  1.13.2.2    bouyer 		       orignpages * sizeof(struct vm_page *));
    928  1.13.2.2    bouyer 	}
    929  1.13.2.2    bouyer 	return 0;
    930  1.13.2.2    bouyer }
    931  1.13.2.2    bouyer 
    932  1.13.2.2    bouyer /*
    933  1.13.2.2    bouyer  * generic VM putpages routine.
    934  1.13.2.2    bouyer  * Write the given range of pages to backing store.
    935  1.13.2.2    bouyer  */
    936  1.13.2.2    bouyer 
    937  1.13.2.2    bouyer int
    938  1.13.2.2    bouyer genfs_putpages(v)
    939  1.13.2.2    bouyer 	void *v;
    940  1.13.2.2    bouyer {
    941  1.13.2.2    bouyer 	struct vop_putpages_args /* {
    942  1.13.2.2    bouyer 		struct vnode *a_vp;
    943  1.13.2.2    bouyer 		struct vm_page **a_m;
    944  1.13.2.2    bouyer 		int a_count;
    945  1.13.2.2    bouyer 		int a_flags;
    946  1.13.2.2    bouyer 		int *a_rtvals;
    947  1.13.2.2    bouyer 	} */ *ap = v;
    948  1.13.2.2    bouyer 
    949  1.13.2.2    bouyer 	int s, error, error2, npages, run;
    950  1.13.2.2    bouyer 	int fs_bshift, dev_bshift, dev_bsize;
    951  1.13.2.2    bouyer 	vaddr_t kva;
    952  1.13.2.2    bouyer 	off_t eof, offset, startoffset;
    953  1.13.2.2    bouyer 	size_t bytes, iobytes, skipbytes;
    954  1.13.2.2    bouyer 	daddr_t lbn, blkno;
    955  1.13.2.2    bouyer 	struct vm_page *pg;
    956  1.13.2.2    bouyer 	struct buf *mbp, *bp;
    957  1.13.2.2    bouyer 	struct vnode *vp = ap->a_vp;
    958  1.13.2.2    bouyer 	boolean_t async = (ap->a_flags & PGO_SYNCIO) == 0;
    959  1.13.2.2    bouyer 	UVMHIST_FUNC("genfs_putpages"); UVMHIST_CALLED(ubchist);
    960  1.13.2.2    bouyer 
    961  1.13.2.2    bouyer 	simple_unlock(&vp->v_uvm.u_obj.vmobjlock);
    962  1.13.2.2    bouyer 
    963  1.13.2.2    bouyer 	error = VOP_SIZE(vp, vp->v_uvm.u_size, &eof);
    964  1.13.2.2    bouyer 	if (error) {
    965  1.13.2.2    bouyer 		return error;
    966  1.13.2.2    bouyer 	}
    967  1.13.2.2    bouyer 
    968  1.13.2.2    bouyer 	error = error2 = 0;
    969  1.13.2.2    bouyer 	npages = ap->a_count;
    970  1.13.2.2    bouyer 	fs_bshift = vp->v_mount->mnt_fs_bshift;
    971  1.13.2.2    bouyer 	dev_bshift = vp->v_mount->mnt_dev_bshift;
    972  1.13.2.2    bouyer 	dev_bsize = 1 << dev_bshift;
    973  1.13.2.2    bouyer 	KASSERT((eof & (dev_bsize - 1)) == 0);
    974  1.13.2.2    bouyer 
    975  1.13.2.2    bouyer 	pg = ap->a_m[0];
    976  1.13.2.2    bouyer 	startoffset = pg->offset;
    977  1.13.2.5    bouyer 	bytes = MIN(npages << PAGE_SHIFT, eof - startoffset);
    978  1.13.2.2    bouyer 	skipbytes = 0;
    979  1.13.2.2    bouyer 	KASSERT(bytes != 0);
    980  1.13.2.2    bouyer 
    981  1.13.2.2    bouyer 	kva = uvm_pagermapin(ap->a_m, npages, UVMPAGER_MAPIN_WAITOK);
    982  1.13.2.2    bouyer 
    983  1.13.2.2    bouyer 	s = splbio();
    984  1.13.2.2    bouyer 	vp->v_numoutput += 2;
    985  1.13.2.2    bouyer 	mbp = pool_get(&bufpool, PR_WAITOK);
    986  1.13.2.2    bouyer 	UVMHIST_LOG(ubchist, "vp %p mbp %p num now %d bytes 0x%x",
    987  1.13.2.2    bouyer 		    vp, mbp, vp->v_numoutput, bytes);
    988  1.13.2.2    bouyer 	splx(s);
    989  1.13.2.2    bouyer 	mbp->b_bufsize = npages << PAGE_SHIFT;
    990  1.13.2.2    bouyer 	mbp->b_data = (void *)kva;
    991  1.13.2.2    bouyer 	mbp->b_resid = mbp->b_bcount = bytes;
    992  1.13.2.2    bouyer 	mbp->b_flags = B_BUSY|B_WRITE|B_AGE |
    993  1.13.2.2    bouyer 		(async ? B_CALL : 0) |
    994  1.13.2.2    bouyer 		(curproc == uvm.pagedaemon_proc ? B_PDAEMON : 0);
    995  1.13.2.2    bouyer 	mbp->b_iodone = uvm_aio_biodone;
    996  1.13.2.2    bouyer 	mbp->b_vp = vp;
    997  1.13.2.2    bouyer 	LIST_INIT(&mbp->b_dep);
    998  1.13.2.2    bouyer 
    999  1.13.2.2    bouyer 	bp = NULL;
   1000  1.13.2.2    bouyer 	for (offset = startoffset;
   1001  1.13.2.2    bouyer 	     bytes > 0;
   1002  1.13.2.2    bouyer 	     offset += iobytes, bytes -= iobytes) {
   1003  1.13.2.2    bouyer 		lbn = offset >> fs_bshift;
   1004  1.13.2.2    bouyer 		error = VOP_BMAP(vp, lbn, NULL, &blkno, &run);
   1005  1.13.2.2    bouyer 		if (error) {
   1006  1.13.2.2    bouyer 			UVMHIST_LOG(ubchist, "VOP_BMAP() -> %d", error,0,0,0);
   1007  1.13.2.2    bouyer 			skipbytes += bytes;
   1008  1.13.2.2    bouyer 			bytes = 0;
   1009  1.13.2.2    bouyer 			break;
   1010  1.13.2.2    bouyer 		}
   1011  1.13.2.2    bouyer 
   1012  1.13.2.5    bouyer 		iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
   1013  1.13.2.5    bouyer 		    bytes);
   1014  1.13.2.2    bouyer 		if (blkno == (daddr_t)-1) {
   1015  1.13.2.2    bouyer 			skipbytes += iobytes;
   1016  1.13.2.2    bouyer 			continue;
   1017  1.13.2.2    bouyer 		}
   1018  1.13.2.2    bouyer 
   1019  1.13.2.2    bouyer 		/* if it's really one i/o, don't make a second buf */
   1020  1.13.2.2    bouyer 		if (offset == startoffset && iobytes == bytes) {
   1021  1.13.2.2    bouyer 			bp = mbp;
   1022  1.13.2.2    bouyer 		} else {
   1023  1.13.2.2    bouyer 			s = splbio();
   1024  1.13.2.2    bouyer 			vp->v_numoutput++;
   1025  1.13.2.2    bouyer 			bp = pool_get(&bufpool, PR_WAITOK);
   1026  1.13.2.2    bouyer 			UVMHIST_LOG(ubchist, "vp %p bp %p num now %d",
   1027  1.13.2.2    bouyer 				    vp, bp, vp->v_numoutput, 0);
   1028  1.13.2.2    bouyer 			splx(s);
   1029  1.13.2.2    bouyer 			bp->b_data = (char *)kva +
   1030  1.13.2.2    bouyer 				(vaddr_t)(offset - pg->offset);
   1031  1.13.2.2    bouyer 			bp->b_resid = bp->b_bcount = iobytes;
   1032  1.13.2.2    bouyer 			bp->b_flags = B_BUSY|B_WRITE|B_CALL|B_ASYNC;
   1033  1.13.2.2    bouyer 			bp->b_iodone = uvm_aio_biodone1;
   1034  1.13.2.2    bouyer 			bp->b_vp = vp;
   1035  1.13.2.2    bouyer 			LIST_INIT(&bp->b_dep);
   1036  1.13.2.2    bouyer 		}
   1037  1.13.2.2    bouyer 		bp->b_lblkno = 0;
   1038  1.13.2.2    bouyer 		bp->b_private = mbp;
   1039  1.13.2.2    bouyer 
   1040  1.13.2.2    bouyer 		/* adjust physical blkno for partial blocks */
   1041  1.13.2.5    bouyer 		bp->b_blkno = blkno + ((offset - ((off_t)lbn << fs_bshift)) >>
   1042  1.13.2.2    bouyer 				       dev_bshift);
   1043  1.13.2.2    bouyer 		UVMHIST_LOG(ubchist, "vp %p offset 0x%x bcount 0x%x blkno 0x%x",
   1044  1.13.2.2    bouyer 			    vp, offset, bp->b_bcount, bp->b_blkno);
   1045  1.13.2.2    bouyer 		VOP_STRATEGY(bp);
   1046  1.13.2.2    bouyer 	}
   1047  1.13.2.2    bouyer 	if (skipbytes) {
   1048  1.13.2.2    bouyer 		UVMHIST_LOG(ubchist, "skipbytes %d", bytes, 0,0,0);
   1049  1.13.2.2    bouyer 		s = splbio();
   1050  1.13.2.2    bouyer 		mbp->b_resid -= skipbytes;
   1051  1.13.2.2    bouyer 		if (mbp->b_resid == 0) {
   1052  1.13.2.2    bouyer 			biodone(mbp);
   1053  1.13.2.2    bouyer 		}
   1054  1.13.2.2    bouyer 		splx(s);
   1055  1.13.2.2    bouyer 	}
   1056  1.13.2.2    bouyer 	if (async) {
   1057  1.13.2.2    bouyer 		UVMHIST_LOG(ubchist, "returning PEND", 0,0,0,0);
   1058  1.13.2.2    bouyer 		return EINPROGRESS;
   1059  1.13.2.2    bouyer 	}
   1060  1.13.2.2    bouyer 	if (bp != NULL) {
   1061  1.13.2.2    bouyer 		UVMHIST_LOG(ubchist, "waiting for mbp %p", mbp,0,0,0);
   1062  1.13.2.2    bouyer 		error2 = biowait(mbp);
   1063  1.13.2.2    bouyer 	}
   1064  1.13.2.2    bouyer 	if (bioops.io_pageiodone) {
   1065  1.13.2.2    bouyer 		(*bioops.io_pageiodone)(mbp);
   1066  1.13.2.2    bouyer 	}
   1067  1.13.2.2    bouyer 	s = splbio();
   1068  1.13.2.2    bouyer 	vwakeup(mbp);
   1069  1.13.2.2    bouyer 	pool_put(&bufpool, mbp);
   1070  1.13.2.2    bouyer 	splx(s);
   1071  1.13.2.2    bouyer 	uvm_pagermapout(kva, npages);
   1072  1.13.2.2    bouyer 	UVMHIST_LOG(ubchist, "returning, error %d", error,0,0,0);
   1073  1.13.2.2    bouyer 	return error ? error : error2;
   1074  1.13.2.2    bouyer }
   1075  1.13.2.2    bouyer 
   1076  1.13.2.2    bouyer int
   1077  1.13.2.2    bouyer genfs_size(v)
   1078  1.13.2.2    bouyer 	void *v;
   1079  1.13.2.2    bouyer {
   1080  1.13.2.2    bouyer 	struct vop_size_args /* {
   1081  1.13.2.2    bouyer 		struct vnode *a_vp;
   1082  1.13.2.2    bouyer 		off_t a_size;
   1083  1.13.2.2    bouyer 		off_t *a_eobp;
   1084  1.13.2.2    bouyer 	} */ *ap = v;
   1085  1.13.2.2    bouyer 	int bsize;
   1086  1.13.2.2    bouyer 
   1087  1.13.2.2    bouyer 	bsize = 1 << ap->a_vp->v_mount->mnt_fs_bshift;
   1088  1.13.2.4    bouyer 	*ap->a_eobp = (ap->a_size + bsize - 1) & ~(bsize - 1);
   1089  1.13.2.2    bouyer 	return 0;
   1090       1.1   mycroft }
   1091