Home | History | Annotate | Line # | Download | only in uvm
uvm_vnode.c revision 1.87.4.1
      1 /*	$NetBSD: uvm_vnode.c,v 1.87.4.1 2007/12/08 18:21:47 mjf Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997 Charles D. Cranor and Washington University.
      5  * Copyright (c) 1991, 1993
      6  *      The Regents of the University of California.
      7  * Copyright (c) 1990 University of Utah.
      8  *
      9  * All rights reserved.
     10  *
     11  * This code is derived from software contributed to Berkeley by
     12  * the Systems Programming Group of the University of Utah Computer
     13  * Science Department.
     14  *
     15  * Redistribution and use in source and binary forms, with or without
     16  * modification, are permitted provided that the following conditions
     17  * are met:
     18  * 1. Redistributions of source code must retain the above copyright
     19  *    notice, this list of conditions and the following disclaimer.
     20  * 2. Redistributions in binary form must reproduce the above copyright
     21  *    notice, this list of conditions and the following disclaimer in the
     22  *    documentation and/or other materials provided with the distribution.
     23  * 3. All advertising materials mentioning features or use of this software
     24  *    must display the following acknowledgement:
     25  *      This product includes software developed by Charles D. Cranor,
     26  *	Washington University, the University of California, Berkeley and
     27  *	its contributors.
     28  * 4. Neither the name of the University nor the names of its contributors
     29  *    may be used to endorse or promote products derived from this software
     30  *    without specific prior written permission.
     31  *
     32  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     35  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     42  * SUCH DAMAGE.
     43  *
     44  *      @(#)vnode_pager.c       8.8 (Berkeley) 2/13/94
     45  * from: Id: uvm_vnode.c,v 1.1.2.26 1998/02/02 20:38:07 chuck Exp
     46  */
     47 
     48 /*
     49  * uvm_vnode.c: the vnode pager.
     50  */
     51 
     52 #include <sys/cdefs.h>
     53 __KERNEL_RCSID(0, "$NetBSD: uvm_vnode.c,v 1.87.4.1 2007/12/08 18:21:47 mjf Exp $");
     54 
     55 #include "fs_nfs.h"
     56 #include "opt_uvmhist.h"
     57 #include "opt_ddb.h"
     58 
     59 #include <sys/param.h>
     60 #include <sys/systm.h>
     61 #include <sys/kernel.h>
     62 #include <sys/proc.h>
     63 #include <sys/malloc.h>
     64 #include <sys/vnode.h>
     65 #include <sys/disklabel.h>
     66 #include <sys/ioctl.h>
     67 #include <sys/fcntl.h>
     68 #include <sys/conf.h>
     69 #include <sys/pool.h>
     70 #include <sys/mount.h>
     71 
     72 #include <miscfs/specfs/specdev.h>
     73 
     74 #include <uvm/uvm.h>
     75 #include <uvm/uvm_readahead.h>
     76 
     77 /*
     78  * functions
     79  */
     80 
     81 static void	uvn_detach(struct uvm_object *);
     82 static int	uvn_get(struct uvm_object *, voff_t, struct vm_page **, int *,
     83 			int, vm_prot_t, int, int);
     84 static int	uvn_put(struct uvm_object *, voff_t, voff_t, int);
     85 static void	uvn_reference(struct uvm_object *);
     86 
     87 static int	uvn_findpage(struct uvm_object *, voff_t, struct vm_page **,
     88 			     int);
     89 
     90 /*
     91  * master pager structure
     92  */
     93 
     94 const struct uvm_pagerops uvm_vnodeops = {
     95 	.pgo_reference = uvn_reference,
     96 	.pgo_detach = uvn_detach,
     97 	.pgo_get = uvn_get,
     98 	.pgo_put = uvn_put,
     99 };
    100 
    101 /*
    102  * the ops!
    103  */
    104 
    105 /*
    106  * uvn_reference
    107  *
    108  * duplicate a reference to a VM object.  Note that the reference
    109  * count must already be at least one (the passed in reference) so
    110  * there is no chance of the uvn being killed or locked out here.
    111  *
    112  * => caller must call with object unlocked.
    113  * => caller must be using the same accessprot as was used at attach time
    114  */
    115 
    116 static void
    117 uvn_reference(struct uvm_object *uobj)
    118 {
    119 	VREF((struct vnode *)uobj);
    120 }
    121 
    122 
    123 /*
    124  * uvn_detach
    125  *
    126  * remove a reference to a VM object.
    127  *
    128  * => caller must call with object unlocked and map locked.
    129  */
    130 
    131 static void
    132 uvn_detach(struct uvm_object *uobj)
    133 {
    134 	vrele((struct vnode *)uobj);
    135 }
    136 
    137 /*
    138  * uvn_put: flush page data to backing store.
    139  *
    140  * => object must be locked on entry!   VOP_PUTPAGES must unlock it.
    141  * => flags: PGO_SYNCIO -- use sync. I/O
    142  * => note: caller must set PG_CLEAN and pmap_clear_modify (if needed)
    143  */
    144 
    145 static int
    146 uvn_put(struct uvm_object *uobj, voff_t offlo, voff_t offhi, int flags)
    147 {
    148 	struct vnode *vp = (struct vnode *)uobj;
    149 	int error;
    150 
    151 	LOCK_ASSERT(simple_lock_held(&vp->v_interlock));
    152 	error = VOP_PUTPAGES(vp, offlo, offhi, flags);
    153 	return error;
    154 }
    155 
    156 
    157 /*
    158  * uvn_get: get pages (synchronously) from backing store
    159  *
    160  * => prefer map unlocked (not required)
    161  * => object must be locked!  we will _unlock_ it before starting any I/O.
    162  * => flags: PGO_ALLPAGES: get all of the pages
    163  *           PGO_LOCKED: fault data structures are locked
    164  * => NOTE: offset is the offset of pps[0], _NOT_ pps[centeridx]
    165  * => NOTE: caller must check for released pages!!
    166  */
    167 
    168 static int
    169 uvn_get(struct uvm_object *uobj, voff_t offset,
    170     struct vm_page **pps /* IN/OUT */,
    171     int *npagesp /* IN (OUT if PGO_LOCKED)*/,
    172     int centeridx, vm_prot_t access_type, int advice, int flags)
    173 {
    174 	struct vnode *vp = (struct vnode *)uobj;
    175 	int error;
    176 
    177 	UVMHIST_FUNC("uvn_get"); UVMHIST_CALLED(ubchist);
    178 
    179 	UVMHIST_LOG(ubchist, "vp %p off 0x%x", vp, (int)offset, 0,0);
    180 
    181 	if ((access_type & VM_PROT_WRITE) == 0 && (flags & PGO_LOCKED) == 0) {
    182 		simple_unlock(&vp->v_interlock);
    183 		vn_ra_allocctx(vp);
    184 		uvm_ra_request(vp->v_ractx, advice, uobj, offset,
    185 		    *npagesp << PAGE_SHIFT);
    186 		simple_lock(&vp->v_interlock);
    187 	}
    188 
    189 	error = VOP_GETPAGES(vp, offset, pps, npagesp, centeridx,
    190 			     access_type, advice, flags);
    191 
    192 	LOCK_ASSERT(((flags & PGO_LOCKED) != 0 &&
    193 		     simple_lock_held(&vp->v_interlock)) ||
    194 		    (flags & PGO_LOCKED) == 0);
    195 	return error;
    196 }
    197 
    198 
    199 /*
    200  * uvn_findpages:
    201  * return the page for the uobj and offset requested, allocating if needed.
    202  * => uobj must be locked.
    203  * => returned pages will be BUSY.
    204  */
    205 
    206 int
    207 uvn_findpages(struct uvm_object *uobj, voff_t offset, int *npagesp,
    208     struct vm_page **pgs, int flags)
    209 {
    210 	int i, count, found, npages, rv;
    211 
    212 	count = found = 0;
    213 	npages = *npagesp;
    214 	if (flags & UFP_BACKWARD) {
    215 		for (i = npages - 1; i >= 0; i--, offset -= PAGE_SIZE) {
    216 			rv = uvn_findpage(uobj, offset, &pgs[i], flags);
    217 			if (rv == 0) {
    218 				if (flags & UFP_DIRTYONLY)
    219 					break;
    220 			} else
    221 				found++;
    222 			count++;
    223 		}
    224 	} else {
    225 		for (i = 0; i < npages; i++, offset += PAGE_SIZE) {
    226 			rv = uvn_findpage(uobj, offset, &pgs[i], flags);
    227 			if (rv == 0) {
    228 				if (flags & UFP_DIRTYONLY)
    229 					break;
    230 			} else
    231 				found++;
    232 			count++;
    233 		}
    234 	}
    235 	*npagesp = count;
    236 	return (found);
    237 }
    238 
    239 static int
    240 uvn_findpage(struct uvm_object *uobj, voff_t offset, struct vm_page **pgp,
    241     int flags)
    242 {
    243 	struct vm_page *pg;
    244 	bool dirty;
    245 	UVMHIST_FUNC("uvn_findpage"); UVMHIST_CALLED(ubchist);
    246 	UVMHIST_LOG(ubchist, "vp %p off 0x%lx", uobj, offset,0,0);
    247 
    248 	if (*pgp != NULL) {
    249 		UVMHIST_LOG(ubchist, "dontcare", 0,0,0,0);
    250 		return 0;
    251 	}
    252 	for (;;) {
    253 		/* look for an existing page */
    254 		pg = uvm_pagelookup(uobj, offset);
    255 
    256 		/* nope?  allocate one now */
    257 		if (pg == NULL) {
    258 			if (flags & UFP_NOALLOC) {
    259 				UVMHIST_LOG(ubchist, "noalloc", 0,0,0,0);
    260 				return 0;
    261 			}
    262 			pg = uvm_pagealloc(uobj, offset, NULL, 0);
    263 			if (pg == NULL) {
    264 				if (flags & UFP_NOWAIT) {
    265 					UVMHIST_LOG(ubchist, "nowait",0,0,0,0);
    266 					return 0;
    267 				}
    268 				simple_unlock(&uobj->vmobjlock);
    269 				uvm_wait("uvn_fp1");
    270 				simple_lock(&uobj->vmobjlock);
    271 				continue;
    272 			}
    273 			UVMHIST_LOG(ubchist, "alloced %p", pg,0,0,0);
    274 			break;
    275 		} else if (flags & UFP_NOCACHE) {
    276 			UVMHIST_LOG(ubchist, "nocache",0,0,0,0);
    277 			return 0;
    278 		}
    279 
    280 		/* page is there, see if we need to wait on it */
    281 		if ((pg->flags & PG_BUSY) != 0) {
    282 			if (flags & UFP_NOWAIT) {
    283 				UVMHIST_LOG(ubchist, "nowait",0,0,0,0);
    284 				return 0;
    285 			}
    286 			pg->flags |= PG_WANTED;
    287 			UVMHIST_LOG(ubchist, "wait %p", pg,0,0,0);
    288 			UVM_UNLOCK_AND_WAIT(pg, &uobj->vmobjlock, 0,
    289 					    "uvn_fp2", 0);
    290 			simple_lock(&uobj->vmobjlock);
    291 			continue;
    292 		}
    293 
    294 		/* skip PG_RDONLY pages if requested */
    295 		if ((flags & UFP_NORDONLY) && (pg->flags & PG_RDONLY)) {
    296 			UVMHIST_LOG(ubchist, "nordonly",0,0,0,0);
    297 			return 0;
    298 		}
    299 
    300 		/* stop on clean pages if requested */
    301 		if (flags & UFP_DIRTYONLY) {
    302 			dirty = pmap_clear_modify(pg) ||
    303 				(pg->flags & PG_CLEAN) == 0;
    304 			pg->flags |= PG_CLEAN;
    305 			if (!dirty) {
    306 				UVMHIST_LOG(ubchist, "dirtonly", 0,0,0,0);
    307 				return 0;
    308 			}
    309 		}
    310 
    311 		/* mark the page BUSY and we're done. */
    312 		pg->flags |= PG_BUSY;
    313 		UVM_PAGE_OWN(pg, "uvn_findpage");
    314 		UVMHIST_LOG(ubchist, "found %p", pg,0,0,0);
    315 		break;
    316 	}
    317 	*pgp = pg;
    318 	return 1;
    319 }
    320 
    321 /*
    322  * uvm_vnp_setsize: grow or shrink a vnode uobj
    323  *
    324  * grow   => just update size value
    325  * shrink => toss un-needed pages
    326  *
    327  * => we assume that the caller has a reference of some sort to the
    328  *	vnode in question so that it will not be yanked out from under
    329  *	us.
    330  */
    331 
    332 void
    333 uvm_vnp_setsize(struct vnode *vp, voff_t newsize)
    334 {
    335 	struct uvm_object *uobj = &vp->v_uobj;
    336 	voff_t pgend = round_page(newsize);
    337 	voff_t oldsize;
    338 	UVMHIST_FUNC("uvm_vnp_setsize"); UVMHIST_CALLED(ubchist);
    339 
    340 	simple_lock(&uobj->vmobjlock);
    341 	UVMHIST_LOG(ubchist, "vp %p old 0x%x new 0x%x",
    342 	    vp, vp->v_size, newsize, 0);
    343 
    344 	/*
    345 	 * now check if the size has changed: if we shrink we had better
    346 	 * toss some pages...
    347 	 */
    348 
    349 	KASSERT(newsize != VSIZENOTSET);
    350 	KASSERT(vp->v_size <= vp->v_writesize);
    351 	KASSERT(vp->v_size == vp->v_writesize ||
    352 	    newsize == vp->v_writesize || newsize <= vp->v_size);
    353 
    354 	oldsize = vp->v_writesize;
    355 	KASSERT(oldsize != VSIZENOTSET || pgend > oldsize);
    356 
    357 	if (oldsize > pgend) {
    358 		(void) uvn_put(uobj, pgend, 0, PGO_FREE | PGO_SYNCIO);
    359 		simple_lock(&uobj->vmobjlock);
    360 	}
    361 	vp->v_size = vp->v_writesize = newsize;
    362 	simple_unlock(&uobj->vmobjlock);
    363 }
    364 
    365 void
    366 uvm_vnp_setwritesize(struct vnode *vp, voff_t newsize)
    367 {
    368 
    369 	simple_lock(&vp->v_interlock);
    370 	KASSERT(newsize != VSIZENOTSET);
    371 	KASSERT(vp->v_size != VSIZENOTSET);
    372 	KASSERT(vp->v_writesize != VSIZENOTSET);
    373 	KASSERT(vp->v_size <= vp->v_writesize);
    374 	KASSERT(vp->v_size <= newsize);
    375 	vp->v_writesize = newsize;
    376 	simple_unlock(&vp->v_interlock);
    377 }
    378 
    379 /*
    380  * uvm_vnp_zerorange:  set a range of bytes in a file to zero.
    381  */
    382 
    383 void
    384 uvm_vnp_zerorange(struct vnode *vp, off_t off, size_t len)
    385 {
    386 	void *win;
    387 	int flags;
    388 
    389 	/*
    390 	 * XXXUBC invent kzero() and use it
    391 	 */
    392 
    393 	while (len) {
    394 		vsize_t bytelen = len;
    395 
    396 		win = ubc_alloc(&vp->v_uobj, off, &bytelen, UVM_ADV_NORMAL,
    397 		    UBC_WRITE);
    398 		memset(win, 0, bytelen);
    399 		flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
    400 		ubc_release(win, flags);
    401 
    402 		off += bytelen;
    403 		len -= bytelen;
    404 	}
    405 }
    406 
    407 bool
    408 uvn_text_p(struct uvm_object *uobj)
    409 {
    410 	struct vnode *vp = (struct vnode *)uobj;
    411 
    412 	return (vp->v_iflag & VI_EXECMAP) != 0;
    413 }
    414 
    415 bool
    416 uvn_clean_p(struct uvm_object *uobj)
    417 {
    418 	struct vnode *vp = (struct vnode *)uobj;
    419 
    420 	return (vp->v_iflag & VI_ONWORKLST) == 0;
    421 }
    422 
    423 bool
    424 uvn_needs_writefault_p(struct uvm_object *uobj)
    425 {
    426 	struct vnode *vp = (struct vnode *)uobj;
    427 
    428 	return uvn_clean_p(uobj) ||
    429 	    (vp->v_iflag & (VI_WRMAP|VI_WRMAPDIRTY)) == VI_WRMAP;
    430 }
    431