Home | History | Annotate | Line # | Download | only in uvm
uvm_vnode.c revision 1.81.2.6
      1  1.81.2.6        ad /*	$NetBSD: uvm_vnode.c,v 1.81.2.6 2007/06/17 21:32:23 ad Exp $	*/
      2       1.1       mrg 
      3       1.1       mrg /*
      4       1.1       mrg  * Copyright (c) 1997 Charles D. Cranor and Washington University.
      5       1.1       mrg  * Copyright (c) 1991, 1993
      6      1.49       chs  *      The Regents of the University of California.
      7       1.1       mrg  * Copyright (c) 1990 University of Utah.
      8       1.1       mrg  *
      9       1.1       mrg  * All rights reserved.
     10       1.1       mrg  *
     11       1.1       mrg  * This code is derived from software contributed to Berkeley by
     12       1.1       mrg  * the Systems Programming Group of the University of Utah Computer
     13       1.1       mrg  * Science Department.
     14       1.1       mrg  *
     15       1.1       mrg  * Redistribution and use in source and binary forms, with or without
     16       1.1       mrg  * modification, are permitted provided that the following conditions
     17       1.1       mrg  * are met:
     18       1.1       mrg  * 1. Redistributions of source code must retain the above copyright
     19       1.1       mrg  *    notice, this list of conditions and the following disclaimer.
     20       1.1       mrg  * 2. Redistributions in binary form must reproduce the above copyright
     21       1.1       mrg  *    notice, this list of conditions and the following disclaimer in the
     22       1.1       mrg  *    documentation and/or other materials provided with the distribution.
     23       1.1       mrg  * 3. All advertising materials mentioning features or use of this software
     24       1.1       mrg  *    must display the following acknowledgement:
     25       1.1       mrg  *      This product includes software developed by Charles D. Cranor,
     26      1.49       chs  *	Washington University, the University of California, Berkeley and
     27       1.1       mrg  *	its contributors.
     28       1.1       mrg  * 4. Neither the name of the University nor the names of its contributors
     29       1.1       mrg  *    may be used to endorse or promote products derived from this software
     30       1.1       mrg  *    without specific prior written permission.
     31       1.1       mrg  *
     32       1.1       mrg  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     33       1.1       mrg  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     34       1.1       mrg  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     35       1.1       mrg  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     36       1.1       mrg  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     37       1.1       mrg  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     38       1.1       mrg  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     39       1.1       mrg  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     40       1.1       mrg  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     41       1.1       mrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     42       1.1       mrg  * SUCH DAMAGE.
     43       1.1       mrg  *
     44       1.1       mrg  *      @(#)vnode_pager.c       8.8 (Berkeley) 2/13/94
     45       1.3       mrg  * from: Id: uvm_vnode.c,v 1.1.2.26 1998/02/02 20:38:07 chuck Exp
     46       1.1       mrg  */
     47       1.1       mrg 
     48      1.55     lukem /*
     49      1.55     lukem  * uvm_vnode.c: the vnode pager.
     50      1.55     lukem  */
     51      1.55     lukem 
     52      1.55     lukem #include <sys/cdefs.h>
     53  1.81.2.6        ad __KERNEL_RCSID(0, "$NetBSD: uvm_vnode.c,v 1.81.2.6 2007/06/17 21:32:23 ad Exp $");
     54      1.55     lukem 
     55       1.6   thorpej #include "fs_nfs.h"
     56       1.4       mrg #include "opt_uvmhist.h"
     57      1.37       chs #include "opt_ddb.h"
     58       1.1       mrg 
     59       1.1       mrg #include <sys/param.h>
     60       1.1       mrg #include <sys/systm.h>
     61      1.37       chs #include <sys/kernel.h>
     62       1.1       mrg #include <sys/proc.h>
     63       1.1       mrg #include <sys/malloc.h>
     64       1.1       mrg #include <sys/vnode.h>
     65      1.13   thorpej #include <sys/disklabel.h>
     66      1.13   thorpej #include <sys/ioctl.h>
     67      1.13   thorpej #include <sys/fcntl.h>
     68      1.13   thorpej #include <sys/conf.h>
     69      1.37       chs #include <sys/pool.h>
     70      1.37       chs #include <sys/mount.h>
     71      1.13   thorpej 
     72      1.13   thorpej #include <miscfs/specfs/specdev.h>
     73       1.1       mrg 
     74       1.1       mrg #include <uvm/uvm.h>
     75      1.68      yamt #include <uvm/uvm_readahead.h>
     76       1.1       mrg 
     77       1.1       mrg /*
     78       1.1       mrg  * functions
     79       1.1       mrg  */
     80       1.1       mrg 
     81      1.66   thorpej static void	uvn_detach(struct uvm_object *);
     82      1.66   thorpej static int	uvn_get(struct uvm_object *, voff_t, struct vm_page **, int *,
     83      1.66   thorpej 			int, vm_prot_t, int, int);
     84      1.66   thorpej static int	uvn_put(struct uvm_object *, voff_t, voff_t, int);
     85      1.66   thorpej static void	uvn_reference(struct uvm_object *);
     86      1.52       chs 
     87      1.66   thorpej static int	uvn_findpage(struct uvm_object *, voff_t, struct vm_page **,
     88      1.66   thorpej 			     int);
     89       1.1       mrg 
     90       1.1       mrg /*
     91       1.1       mrg  * master pager structure
     92       1.1       mrg  */
     93       1.1       mrg 
     94       1.1       mrg struct uvm_pagerops uvm_vnodeops = {
     95      1.37       chs 	NULL,
     96       1.8       mrg 	uvn_reference,
     97       1.8       mrg 	uvn_detach,
     98      1.37       chs 	NULL,
     99       1.8       mrg 	uvn_get,
    100       1.8       mrg 	uvn_put,
    101       1.1       mrg };
    102       1.1       mrg 
    103       1.1       mrg /*
    104       1.1       mrg  * the ops!
    105       1.1       mrg  */
    106       1.1       mrg 
    107       1.1       mrg /*
    108       1.1       mrg  * uvn_attach
    109       1.1       mrg  *
    110       1.1       mrg  * attach a vnode structure to a VM object.  if the vnode is already
    111       1.1       mrg  * attached, then just bump the reference count by one and return the
    112       1.1       mrg  * VM object.   if not already attached, attach and return the new VM obj.
    113       1.1       mrg  * the "accessprot" tells the max access the attaching thread wants to
    114       1.1       mrg  * our pages.
    115       1.1       mrg  *
    116       1.1       mrg  * => caller must _not_ already be holding the lock on the uvm_object.
    117       1.1       mrg  * => in fact, nothing should be locked so that we can sleep here.
    118       1.1       mrg  * => note that uvm_object is first thing in vnode structure, so their
    119       1.1       mrg  *    pointers are equiv.
    120       1.1       mrg  */
    121       1.1       mrg 
    122       1.8       mrg struct uvm_object *
    123      1.77      yamt uvn_attach(void *arg, vm_prot_t accessprot)
    124       1.8       mrg {
    125       1.8       mrg 	struct vnode *vp = arg;
    126      1.52       chs 	struct uvm_object *uobj = &vp->v_uobj;
    127       1.8       mrg 	struct vattr vattr;
    128      1.59   gehenna 	const struct bdevsw *bdev;
    129      1.37       chs 	int result;
    130      1.13   thorpej 	struct partinfo pi;
    131      1.37       chs 	voff_t used_vnode_size;
    132       1.8       mrg 	UVMHIST_FUNC("uvn_attach"); UVMHIST_CALLED(maphist);
    133       1.8       mrg 
    134       1.8       mrg 	UVMHIST_LOG(maphist, "(vn=0x%x)", arg,0,0,0);
    135      1.37       chs 	used_vnode_size = (voff_t)0;
    136      1.13   thorpej 
    137       1.8       mrg 	/*
    138      1.52       chs 	 * first get a lock on the uobj.
    139       1.8       mrg 	 */
    140      1.52       chs 
    141  1.81.2.1        ad 	mutex_enter(&uobj->vmobjlock);
    142  1.81.2.6        ad 	if (vp->v_iflag & VI_XLOCK) {
    143       1.8       mrg 		UVMHIST_LOG(maphist, "  SLEEPING on blocked vn",0,0,0,0);
    144  1.81.2.6        ad 		vwait(vp, VI_XLOCK);
    145       1.8       mrg 		UVMHIST_LOG(maphist,"  WOKE UP",0,0,0,0);
    146       1.8       mrg 	}
    147       1.1       mrg 
    148       1.8       mrg 	/*
    149      1.18    bouyer 	 * if we're mapping a BLK device, make sure it is a disk.
    150      1.13   thorpej 	 */
    151      1.59   gehenna 	if (vp->v_type == VBLK) {
    152  1.81.2.4        ad 		if (bdev_type(vp->v_rdev) != D_DISK) {
    153  1.81.2.1        ad 			mutex_exit(&uobj->vmobjlock);
    154      1.59   gehenna 			UVMHIST_LOG(maphist,"<- done (VBLK not D_DISK!)",
    155      1.59   gehenna 				    0,0,0,0);
    156      1.59   gehenna 			return(NULL);
    157      1.59   gehenna 		}
    158      1.13   thorpej 	}
    159      1.51       chs 	KASSERT(vp->v_type == VREG || vp->v_type == VBLK);
    160      1.37       chs 
    161      1.13   thorpej 	/*
    162      1.37       chs 	 * set up our idea of the size
    163      1.37       chs 	 * if this hasn't been done already.
    164       1.8       mrg 	 */
    165      1.52       chs 	if (vp->v_size == VSIZENOTSET) {
    166       1.8       mrg 
    167      1.52       chs 
    168  1.81.2.6        ad 	vp->v_iflag |= VI_XLOCK;
    169  1.81.2.1        ad 	mutex_exit(&uobj->vmobjlock); /* drop lock in case we sleep */
    170       1.8       mrg 		/* XXX: curproc? */
    171      1.13   thorpej 	if (vp->v_type == VBLK) {
    172      1.13   thorpej 		/*
    173      1.13   thorpej 		 * We could implement this as a specfs getattr call, but:
    174      1.13   thorpej 		 *
    175      1.13   thorpej 		 *	(1) VOP_GETATTR() would get the file system
    176      1.13   thorpej 		 *	    vnode operation, not the specfs operation.
    177      1.13   thorpej 		 *
    178      1.13   thorpej 		 *	(2) All we want is the size, anyhow.
    179      1.13   thorpej 		 */
    180      1.59   gehenna 		bdev = bdevsw_lookup(vp->v_rdev);
    181      1.59   gehenna 		if (bdev != NULL) {
    182      1.59   gehenna 			result = (*bdev->d_ioctl)(vp->v_rdev, DIOCGPART,
    183      1.81  christos 						  (void *)&pi, FREAD, curlwp);
    184      1.59   gehenna 		} else {
    185      1.59   gehenna 			result = ENXIO;
    186      1.59   gehenna 		}
    187      1.13   thorpej 		if (result == 0) {
    188      1.13   thorpej 			/* XXX should remember blocksize */
    189      1.37       chs 			used_vnode_size = (voff_t)pi.disklab->d_secsize *
    190      1.37       chs 			    (voff_t)pi.part->p_size;
    191      1.13   thorpej 		}
    192      1.13   thorpej 	} else {
    193      1.71        ad 		result = VOP_GETATTR(vp, &vattr, curlwp->l_cred, curlwp);
    194      1.13   thorpej 		if (result == 0)
    195      1.13   thorpej 			used_vnode_size = vattr.va_size;
    196       1.8       mrg 	}
    197       1.1       mrg 
    198       1.8       mrg 	/* relock object */
    199  1.81.2.1        ad 	mutex_enter(&uobj->vmobjlock);
    200  1.81.2.6        ad 	vunwait(vp, VI_XLOCK);
    201       1.1       mrg 
    202       1.8       mrg 	if (result != 0) {
    203  1.81.2.1        ad 		mutex_exit(&uobj->vmobjlock);
    204       1.8       mrg 		UVMHIST_LOG(maphist,"<- done (VOP_GETATTR FAILED!)", 0,0,0,0);
    205       1.8       mrg 		return(NULL);
    206       1.8       mrg 	}
    207  1.81.2.5        ad 	vp->v_size = vp->v_writesize = used_vnode_size;
    208       1.8       mrg 
    209       1.8       mrg 	}
    210       1.8       mrg 
    211  1.81.2.1        ad 	mutex_exit(&uobj->vmobjlock);
    212      1.52       chs 	UVMHIST_LOG(maphist,"<- done, refcnt=%d", vp->v_usecount,
    213      1.37       chs 	    0, 0, 0);
    214  1.81.2.1        ad 
    215      1.52       chs 	return uobj;
    216       1.1       mrg }
    217       1.1       mrg 
    218       1.1       mrg 
    219       1.1       mrg /*
    220       1.1       mrg  * uvn_reference
    221       1.1       mrg  *
    222       1.1       mrg  * duplicate a reference to a VM object.  Note that the reference
    223      1.49       chs  * count must already be at least one (the passed in reference) so
    224       1.1       mrg  * there is no chance of the uvn being killed or locked out here.
    225       1.1       mrg  *
    226      1.49       chs  * => caller must call with object unlocked.
    227       1.1       mrg  * => caller must be using the same accessprot as was used at attach time
    228       1.1       mrg  */
    229       1.1       mrg 
    230      1.66   thorpej static void
    231      1.65   thorpej uvn_reference(struct uvm_object *uobj)
    232       1.1       mrg {
    233      1.37       chs 	VREF((struct vnode *)uobj);
    234       1.1       mrg }
    235       1.1       mrg 
    236      1.52       chs 
    237       1.1       mrg /*
    238       1.1       mrg  * uvn_detach
    239       1.1       mrg  *
    240       1.1       mrg  * remove a reference to a VM object.
    241       1.1       mrg  *
    242       1.1       mrg  * => caller must call with object unlocked and map locked.
    243       1.1       mrg  */
    244      1.52       chs 
    245      1.66   thorpej static void
    246      1.65   thorpej uvn_detach(struct uvm_object *uobj)
    247       1.8       mrg {
    248      1.37       chs 	vrele((struct vnode *)uobj);
    249       1.1       mrg }
    250       1.1       mrg 
    251       1.1       mrg /*
    252       1.1       mrg  * uvn_put: flush page data to backing store.
    253       1.1       mrg  *
    254      1.53  sommerfe  * => object must be locked on entry!   VOP_PUTPAGES must unlock it.
    255       1.1       mrg  * => flags: PGO_SYNCIO -- use sync. I/O
    256       1.1       mrg  * => note: caller must set PG_CLEAN and pmap_clear_modify (if needed)
    257       1.1       mrg  */
    258       1.1       mrg 
    259      1.66   thorpej static int
    260      1.65   thorpej uvn_put(struct uvm_object *uobj, voff_t offlo, voff_t offhi, int flags)
    261       1.1       mrg {
    262      1.37       chs 	struct vnode *vp = (struct vnode *)uobj;
    263      1.37       chs 	int error;
    264       1.1       mrg 
    265  1.81.2.1        ad 	KASSERT(mutex_owned(&vp->v_interlock));
    266      1.54       chs 	error = VOP_PUTPAGES(vp, offlo, offhi, flags);
    267  1.81.2.1        ad 
    268      1.48       chs 	return error;
    269       1.1       mrg }
    270       1.1       mrg 
    271       1.1       mrg 
    272       1.1       mrg /*
    273       1.1       mrg  * uvn_get: get pages (synchronously) from backing store
    274       1.1       mrg  *
    275       1.1       mrg  * => prefer map unlocked (not required)
    276       1.1       mrg  * => object must be locked!  we will _unlock_ it before starting any I/O.
    277       1.1       mrg  * => flags: PGO_ALLPAGES: get all of the pages
    278       1.1       mrg  *           PGO_LOCKED: fault data structures are locked
    279       1.1       mrg  * => NOTE: offset is the offset of pps[0], _NOT_ pps[centeridx]
    280       1.1       mrg  * => NOTE: caller must check for released pages!!
    281       1.1       mrg  */
    282      1.49       chs 
    283      1.66   thorpej static int
    284      1.65   thorpej uvn_get(struct uvm_object *uobj, voff_t offset,
    285      1.65   thorpej     struct vm_page **pps /* IN/OUT */,
    286      1.65   thorpej     int *npagesp /* IN (OUT if PGO_LOCKED)*/,
    287      1.65   thorpej     int centeridx, vm_prot_t access_type, int advice, int flags)
    288       1.8       mrg {
    289      1.37       chs 	struct vnode *vp = (struct vnode *)uobj;
    290      1.37       chs 	int error;
    291      1.67      yamt 
    292      1.37       chs 	UVMHIST_FUNC("uvn_get"); UVMHIST_CALLED(ubchist);
    293      1.37       chs 
    294      1.37       chs 	UVMHIST_LOG(ubchist, "vp %p off 0x%x", vp, (int)offset, 0,0);
    295      1.68      yamt 
    296      1.68      yamt 	if ((access_type & VM_PROT_WRITE) == 0 && (flags & PGO_LOCKED) == 0) {
    297  1.81.2.1        ad 		mutex_exit(&vp->v_interlock);
    298      1.68      yamt 		vn_ra_allocctx(vp);
    299      1.68      yamt 		uvm_ra_request(vp->v_ractx, advice, uobj, offset,
    300      1.68      yamt 		    *npagesp << PAGE_SHIFT);
    301  1.81.2.1        ad 		mutex_enter(&vp->v_interlock);
    302      1.68      yamt 	}
    303      1.68      yamt 
    304      1.37       chs 	error = VOP_GETPAGES(vp, offset, pps, npagesp, centeridx,
    305      1.37       chs 			     access_type, advice, flags);
    306      1.67      yamt 
    307  1.81.2.1        ad 	KASSERT(((flags & PGO_LOCKED) != 0 &&
    308  1.81.2.1        ad 		     mutex_owned(&vp->v_interlock)) ||
    309      1.78       chs 		    ((flags & PGO_LOCKED) == 0 &&
    310  1.81.2.1        ad 		     !mutex_owned(&vp->v_interlock)));
    311      1.48       chs 	return error;
    312      1.37       chs }
    313       1.8       mrg 
    314       1.8       mrg 
    315      1.37       chs /*
    316      1.37       chs  * uvn_findpages:
    317      1.37       chs  * return the page for the uobj and offset requested, allocating if needed.
    318      1.37       chs  * => uobj must be locked.
    319      1.52       chs  * => returned pages will be BUSY.
    320      1.37       chs  */
    321       1.1       mrg 
    322      1.58     enami int
    323      1.65   thorpej uvn_findpages(struct uvm_object *uobj, voff_t offset, int *npagesp,
    324      1.65   thorpej     struct vm_page **pgs, int flags)
    325      1.37       chs {
    326      1.58     enami 	int i, count, found, npages, rv;
    327       1.8       mrg 
    328      1.58     enami 	count = found = 0;
    329      1.37       chs 	npages = *npagesp;
    330      1.52       chs 	if (flags & UFP_BACKWARD) {
    331      1.52       chs 		for (i = npages - 1; i >= 0; i--, offset -= PAGE_SIZE) {
    332      1.52       chs 			rv = uvn_findpage(uobj, offset, &pgs[i], flags);
    333      1.58     enami 			if (rv == 0) {
    334      1.58     enami 				if (flags & UFP_DIRTYONLY)
    335      1.58     enami 					break;
    336      1.58     enami 			} else
    337      1.58     enami 				found++;
    338      1.52       chs 			count++;
    339      1.52       chs 		}
    340      1.52       chs 	} else {
    341      1.52       chs 		for (i = 0; i < npages; i++, offset += PAGE_SIZE) {
    342      1.52       chs 			rv = uvn_findpage(uobj, offset, &pgs[i], flags);
    343      1.58     enami 			if (rv == 0) {
    344      1.58     enami 				if (flags & UFP_DIRTYONLY)
    345      1.58     enami 					break;
    346      1.58     enami 			} else
    347      1.58     enami 				found++;
    348      1.52       chs 			count++;
    349      1.52       chs 		}
    350      1.37       chs 	}
    351      1.52       chs 	*npagesp = count;
    352      1.58     enami 	return (found);
    353      1.37       chs }
    354       1.8       mrg 
    355      1.66   thorpej static int
    356      1.65   thorpej uvn_findpage(struct uvm_object *uobj, voff_t offset, struct vm_page **pgp,
    357      1.65   thorpej     int flags)
    358      1.37       chs {
    359      1.37       chs 	struct vm_page *pg;
    360      1.79   thorpej 	bool dirty;
    361      1.37       chs 	UVMHIST_FUNC("uvn_findpage"); UVMHIST_CALLED(ubchist);
    362      1.37       chs 	UVMHIST_LOG(ubchist, "vp %p off 0x%lx", uobj, offset,0,0);
    363       1.8       mrg 
    364      1.37       chs 	if (*pgp != NULL) {
    365      1.37       chs 		UVMHIST_LOG(ubchist, "dontcare", 0,0,0,0);
    366      1.37       chs 		return 0;
    367      1.37       chs 	}
    368      1.37       chs 	for (;;) {
    369      1.37       chs 		/* look for an existing page */
    370      1.37       chs 		pg = uvm_pagelookup(uobj, offset);
    371      1.37       chs 
    372      1.52       chs 		/* nope?  allocate one now */
    373      1.37       chs 		if (pg == NULL) {
    374      1.37       chs 			if (flags & UFP_NOALLOC) {
    375      1.37       chs 				UVMHIST_LOG(ubchist, "noalloc", 0,0,0,0);
    376      1.37       chs 				return 0;
    377      1.37       chs 			}
    378      1.47       chs 			pg = uvm_pagealloc(uobj, offset, NULL, 0);
    379      1.37       chs 			if (pg == NULL) {
    380      1.37       chs 				if (flags & UFP_NOWAIT) {
    381      1.37       chs 					UVMHIST_LOG(ubchist, "nowait",0,0,0,0);
    382      1.37       chs 					return 0;
    383       1.8       mrg 				}
    384  1.81.2.1        ad 				mutex_exit(&uobj->vmobjlock);
    385      1.37       chs 				uvm_wait("uvn_fp1");
    386  1.81.2.1        ad 				mutex_enter(&uobj->vmobjlock);
    387      1.37       chs 				continue;
    388      1.47       chs 			}
    389      1.52       chs 			UVMHIST_LOG(ubchist, "alloced %p", pg,0,0,0);
    390      1.37       chs 			break;
    391      1.37       chs 		} else if (flags & UFP_NOCACHE) {
    392      1.37       chs 			UVMHIST_LOG(ubchist, "nocache",0,0,0,0);
    393      1.37       chs 			return 0;
    394       1.8       mrg 		}
    395       1.8       mrg 
    396      1.37       chs 		/* page is there, see if we need to wait on it */
    397      1.52       chs 		if ((pg->flags & PG_BUSY) != 0) {
    398      1.37       chs 			if (flags & UFP_NOWAIT) {
    399      1.37       chs 				UVMHIST_LOG(ubchist, "nowait",0,0,0,0);
    400      1.37       chs 				return 0;
    401      1.37       chs 			}
    402      1.37       chs 			pg->flags |= PG_WANTED;
    403      1.58     enami 			UVMHIST_LOG(ubchist, "wait %p", pg,0,0,0);
    404      1.37       chs 			UVM_UNLOCK_AND_WAIT(pg, &uobj->vmobjlock, 0,
    405      1.37       chs 					    "uvn_fp2", 0);
    406  1.81.2.1        ad 			mutex_enter(&uobj->vmobjlock);
    407      1.37       chs 			continue;
    408       1.8       mrg 		}
    409      1.49       chs 
    410      1.37       chs 		/* skip PG_RDONLY pages if requested */
    411      1.37       chs 		if ((flags & UFP_NORDONLY) && (pg->flags & PG_RDONLY)) {
    412      1.37       chs 			UVMHIST_LOG(ubchist, "nordonly",0,0,0,0);
    413      1.37       chs 			return 0;
    414       1.8       mrg 		}
    415       1.8       mrg 
    416      1.52       chs 		/* stop on clean pages if requested */
    417      1.52       chs 		if (flags & UFP_DIRTYONLY) {
    418      1.52       chs 			dirty = pmap_clear_modify(pg) ||
    419      1.52       chs 				(pg->flags & PG_CLEAN) == 0;
    420      1.52       chs 			pg->flags |= PG_CLEAN;
    421      1.52       chs 			if (!dirty) {
    422      1.58     enami 				UVMHIST_LOG(ubchist, "dirtonly", 0,0,0,0);
    423      1.52       chs 				return 0;
    424      1.52       chs 			}
    425      1.52       chs 		}
    426      1.52       chs 
    427      1.37       chs 		/* mark the page BUSY and we're done. */
    428      1.37       chs 		pg->flags |= PG_BUSY;
    429      1.37       chs 		UVM_PAGE_OWN(pg, "uvn_findpage");
    430      1.52       chs 		UVMHIST_LOG(ubchist, "found %p", pg,0,0,0);
    431      1.37       chs 		break;
    432       1.8       mrg 	}
    433      1.37       chs 	*pgp = pg;
    434      1.37       chs 	return 1;
    435       1.1       mrg }
    436       1.1       mrg 
    437       1.1       mrg /*
    438      1.52       chs  * uvm_vnp_setsize: grow or shrink a vnode uobj
    439       1.1       mrg  *
    440       1.1       mrg  * grow   => just update size value
    441       1.1       mrg  * shrink => toss un-needed pages
    442       1.1       mrg  *
    443      1.49       chs  * => we assume that the caller has a reference of some sort to the
    444       1.1       mrg  *	vnode in question so that it will not be yanked out from under
    445       1.1       mrg  *	us.
    446       1.1       mrg  */
    447       1.1       mrg 
    448       1.8       mrg void
    449      1.65   thorpej uvm_vnp_setsize(struct vnode *vp, voff_t newsize)
    450       1.8       mrg {
    451      1.52       chs 	struct uvm_object *uobj = &vp->v_uobj;
    452      1.46     enami 	voff_t pgend = round_page(newsize);
    453      1.72      yamt 	voff_t oldsize;
    454      1.37       chs 	UVMHIST_FUNC("uvm_vnp_setsize"); UVMHIST_CALLED(ubchist);
    455      1.37       chs 
    456  1.81.2.1        ad 	mutex_enter(&uobj->vmobjlock);
    457      1.52       chs 	UVMHIST_LOG(ubchist, "vp %p old 0x%x new 0x%x",
    458      1.52       chs 	    vp, vp->v_size, newsize, 0);
    459       1.1       mrg 
    460       1.8       mrg 	/*
    461      1.37       chs 	 * now check if the size has changed: if we shrink we had better
    462      1.37       chs 	 * toss some pages...
    463       1.8       mrg 	 */
    464       1.1       mrg 
    465  1.81.2.5        ad 	if (vp->v_writesize != VSIZENOTSET) {
    466  1.81.2.5        ad 		KASSERT(vp->v_size <= vp->v_writesize);
    467  1.81.2.5        ad 		KASSERT(vp->v_size == vp->v_writesize ||
    468  1.81.2.5        ad 		    newsize == vp->v_writesize || newsize <= vp->v_size);
    469  1.81.2.5        ad 		oldsize = vp->v_writesize;
    470  1.81.2.5        ad 	} else {
    471  1.81.2.5        ad 		oldsize = vp->v_size;
    472  1.81.2.5        ad 	}
    473      1.72      yamt 	if (oldsize > pgend && oldsize != VSIZENOTSET) {
    474      1.57       chs 		(void) uvn_put(uobj, pgend, 0, PGO_FREE | PGO_SYNCIO);
    475  1.81.2.1        ad 		mutex_enter(&uobj->vmobjlock);
    476       1.8       mrg 	}
    477  1.81.2.5        ad 	vp->v_size = vp->v_writesize = newsize;
    478  1.81.2.1        ad 	mutex_exit(&uobj->vmobjlock);
    479       1.1       mrg }
    480       1.1       mrg 
    481  1.81.2.5        ad void
    482  1.81.2.5        ad uvm_vnp_setwritesize(struct vnode *vp, voff_t newsize)
    483  1.81.2.5        ad {
    484  1.81.2.5        ad 
    485  1.81.2.5        ad 	mutex_enter(&vp->v_interlock);
    486  1.81.2.5        ad 	KASSERT(vp->v_size != VSIZENOTSET);
    487  1.81.2.5        ad 	KASSERT(vp->v_writesize != VSIZENOTSET);
    488  1.81.2.5        ad 	KASSERT(vp->v_size <= vp->v_writesize);
    489  1.81.2.5        ad 	KASSERT(vp->v_size <= newsize);
    490  1.81.2.5        ad 	vp->v_writesize = newsize;
    491  1.81.2.5        ad 	mutex_exit(&vp->v_interlock);
    492  1.81.2.5        ad }
    493  1.81.2.5        ad 
    494       1.1       mrg /*
    495      1.37       chs  * uvm_vnp_zerorange:  set a range of bytes in a file to zero.
    496       1.1       mrg  */
    497       1.1       mrg 
    498       1.8       mrg void
    499      1.65   thorpej uvm_vnp_zerorange(struct vnode *vp, off_t off, size_t len)
    500       1.8       mrg {
    501      1.64       chs 	void *win;
    502      1.64       chs 	int flags;
    503      1.64       chs 
    504      1.64       chs 	/*
    505      1.64       chs 	 * XXXUBC invent kzero() and use it
    506      1.64       chs 	 */
    507       1.8       mrg 
    508      1.64       chs 	while (len) {
    509      1.64       chs 		vsize_t bytelen = len;
    510      1.64       chs 
    511      1.68      yamt 		win = ubc_alloc(&vp->v_uobj, off, &bytelen, UVM_ADV_NORMAL,
    512      1.68      yamt 		    UBC_WRITE);
    513      1.64       chs 		memset(win, 0, bytelen);
    514      1.64       chs 		flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
    515      1.64       chs 		ubc_release(win, flags);
    516      1.64       chs 
    517      1.64       chs 		off += bytelen;
    518      1.64       chs 		len -= bytelen;
    519      1.64       chs 	}
    520       1.1       mrg }
    521      1.75      yamt 
    522      1.79   thorpej bool
    523      1.75      yamt uvn_text_p(struct uvm_object *uobj)
    524      1.75      yamt {
    525      1.75      yamt 	struct vnode *vp = (struct vnode *)uobj;
    526      1.75      yamt 
    527  1.81.2.6        ad 	return (vp->v_iflag & VI_EXECMAP) != 0;
    528      1.75      yamt }
    529      1.75      yamt 
    530      1.79   thorpej bool
    531      1.75      yamt uvn_clean_p(struct uvm_object *uobj)
    532      1.75      yamt {
    533      1.75      yamt 	struct vnode *vp = (struct vnode *)uobj;
    534      1.75      yamt 
    535  1.81.2.6        ad 	return (vp->v_iflag & VI_ONWORKLST) == 0;
    536      1.75      yamt }
    537      1.75      yamt 
    538      1.79   thorpej bool
    539      1.75      yamt uvn_needs_writefault_p(struct uvm_object *uobj)
    540      1.75      yamt {
    541      1.75      yamt 	struct vnode *vp = (struct vnode *)uobj;
    542      1.75      yamt 
    543      1.75      yamt 	return uvn_clean_p(uobj) ||
    544  1.81.2.6        ad 	    (vp->v_iflag & (VI_WRMAP|VI_WRMAPDIRTY)) == VI_WRMAP;
    545      1.75      yamt }
    546