Home | History | Annotate | Line # | Download | only in uvm
uvm_vnode.c revision 1.85
      1 /*	$NetBSD: uvm_vnode.c,v 1.85 2007/08/04 09:42:58 pooka 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.85 2007/08/04 09:42:58 pooka 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 struct uvm_pagerops uvm_vnodeops = {
     95 	NULL,
     96 	uvn_reference,
     97 	uvn_detach,
     98 	NULL,
     99 	uvn_get,
    100 	uvn_put,
    101 };
    102 
    103 /*
    104  * the ops!
    105  */
    106 
    107 /*
    108  * uvn_reference
    109  *
    110  * duplicate a reference to a VM object.  Note that the reference
    111  * count must already be at least one (the passed in reference) so
    112  * there is no chance of the uvn being killed or locked out here.
    113  *
    114  * => caller must call with object unlocked.
    115  * => caller must be using the same accessprot as was used at attach time
    116  */
    117 
    118 static void
    119 uvn_reference(struct uvm_object *uobj)
    120 {
    121 	VREF((struct vnode *)uobj);
    122 }
    123 
    124 
    125 /*
    126  * uvn_detach
    127  *
    128  * remove a reference to a VM object.
    129  *
    130  * => caller must call with object unlocked and map locked.
    131  */
    132 
    133 static void
    134 uvn_detach(struct uvm_object *uobj)
    135 {
    136 	vrele((struct vnode *)uobj);
    137 }
    138 
    139 /*
    140  * uvn_put: flush page data to backing store.
    141  *
    142  * => object must be locked on entry!   VOP_PUTPAGES must unlock it.
    143  * => flags: PGO_SYNCIO -- use sync. I/O
    144  * => note: caller must set PG_CLEAN and pmap_clear_modify (if needed)
    145  */
    146 
    147 static int
    148 uvn_put(struct uvm_object *uobj, voff_t offlo, voff_t offhi, int flags)
    149 {
    150 	struct vnode *vp = (struct vnode *)uobj;
    151 	int error;
    152 
    153 	LOCK_ASSERT(simple_lock_held(&vp->v_interlock));
    154 	error = VOP_PUTPAGES(vp, offlo, offhi, flags);
    155 	LOCK_ASSERT(!simple_lock_held(&vp->v_interlock));
    156 	return error;
    157 }
    158 
    159 
    160 /*
    161  * uvn_get: get pages (synchronously) from backing store
    162  *
    163  * => prefer map unlocked (not required)
    164  * => object must be locked!  we will _unlock_ it before starting any I/O.
    165  * => flags: PGO_ALLPAGES: get all of the pages
    166  *           PGO_LOCKED: fault data structures are locked
    167  * => NOTE: offset is the offset of pps[0], _NOT_ pps[centeridx]
    168  * => NOTE: caller must check for released pages!!
    169  */
    170 
    171 static int
    172 uvn_get(struct uvm_object *uobj, voff_t offset,
    173     struct vm_page **pps /* IN/OUT */,
    174     int *npagesp /* IN (OUT if PGO_LOCKED)*/,
    175     int centeridx, vm_prot_t access_type, int advice, int flags)
    176 {
    177 	struct vnode *vp = (struct vnode *)uobj;
    178 	int error;
    179 
    180 	UVMHIST_FUNC("uvn_get"); UVMHIST_CALLED(ubchist);
    181 
    182 	UVMHIST_LOG(ubchist, "vp %p off 0x%x", vp, (int)offset, 0,0);
    183 
    184 	if ((access_type & VM_PROT_WRITE) == 0 && (flags & PGO_LOCKED) == 0) {
    185 		simple_unlock(&vp->v_interlock);
    186 		vn_ra_allocctx(vp);
    187 		uvm_ra_request(vp->v_ractx, advice, uobj, offset,
    188 		    *npagesp << PAGE_SHIFT);
    189 		simple_lock(&vp->v_interlock);
    190 	}
    191 
    192 	error = VOP_GETPAGES(vp, offset, pps, npagesp, centeridx,
    193 			     access_type, advice, flags);
    194 
    195 	LOCK_ASSERT(((flags & PGO_LOCKED) != 0 &&
    196 		     simple_lock_held(&vp->v_interlock)) ||
    197 		    ((flags & PGO_LOCKED) == 0 &&
    198 		     !simple_lock_held(&vp->v_interlock)));
    199 	return error;
    200 }
    201 
    202 
    203 /*
    204  * uvn_findpages:
    205  * return the page for the uobj and offset requested, allocating if needed.
    206  * => uobj must be locked.
    207  * => returned pages will be BUSY.
    208  */
    209 
    210 int
    211 uvn_findpages(struct uvm_object *uobj, voff_t offset, int *npagesp,
    212     struct vm_page **pgs, int flags)
    213 {
    214 	int i, count, found, npages, rv;
    215 
    216 	count = found = 0;
    217 	npages = *npagesp;
    218 	if (flags & UFP_BACKWARD) {
    219 		for (i = npages - 1; i >= 0; i--, offset -= PAGE_SIZE) {
    220 			rv = uvn_findpage(uobj, offset, &pgs[i], flags);
    221 			if (rv == 0) {
    222 				if (flags & UFP_DIRTYONLY)
    223 					break;
    224 			} else
    225 				found++;
    226 			count++;
    227 		}
    228 	} else {
    229 		for (i = 0; i < npages; i++, offset += PAGE_SIZE) {
    230 			rv = uvn_findpage(uobj, offset, &pgs[i], flags);
    231 			if (rv == 0) {
    232 				if (flags & UFP_DIRTYONLY)
    233 					break;
    234 			} else
    235 				found++;
    236 			count++;
    237 		}
    238 	}
    239 	*npagesp = count;
    240 	return (found);
    241 }
    242 
    243 static int
    244 uvn_findpage(struct uvm_object *uobj, voff_t offset, struct vm_page **pgp,
    245     int flags)
    246 {
    247 	struct vm_page *pg;
    248 	bool dirty;
    249 	UVMHIST_FUNC("uvn_findpage"); UVMHIST_CALLED(ubchist);
    250 	UVMHIST_LOG(ubchist, "vp %p off 0x%lx", uobj, offset,0,0);
    251 
    252 	if (*pgp != NULL) {
    253 		UVMHIST_LOG(ubchist, "dontcare", 0,0,0,0);
    254 		return 0;
    255 	}
    256 	for (;;) {
    257 		/* look for an existing page */
    258 		pg = uvm_pagelookup(uobj, offset);
    259 
    260 		/* nope?  allocate one now */
    261 		if (pg == NULL) {
    262 			if (flags & UFP_NOALLOC) {
    263 				UVMHIST_LOG(ubchist, "noalloc", 0,0,0,0);
    264 				return 0;
    265 			}
    266 			pg = uvm_pagealloc(uobj, offset, NULL, 0);
    267 			if (pg == NULL) {
    268 				if (flags & UFP_NOWAIT) {
    269 					UVMHIST_LOG(ubchist, "nowait",0,0,0,0);
    270 					return 0;
    271 				}
    272 				simple_unlock(&uobj->vmobjlock);
    273 				uvm_wait("uvn_fp1");
    274 				simple_lock(&uobj->vmobjlock);
    275 				continue;
    276 			}
    277 			UVMHIST_LOG(ubchist, "alloced %p", pg,0,0,0);
    278 			break;
    279 		} else if (flags & UFP_NOCACHE) {
    280 			UVMHIST_LOG(ubchist, "nocache",0,0,0,0);
    281 			return 0;
    282 		}
    283 
    284 		/* page is there, see if we need to wait on it */
    285 		if ((pg->flags & PG_BUSY) != 0) {
    286 			if (flags & UFP_NOWAIT) {
    287 				UVMHIST_LOG(ubchist, "nowait",0,0,0,0);
    288 				return 0;
    289 			}
    290 			pg->flags |= PG_WANTED;
    291 			UVMHIST_LOG(ubchist, "wait %p", pg,0,0,0);
    292 			UVM_UNLOCK_AND_WAIT(pg, &uobj->vmobjlock, 0,
    293 					    "uvn_fp2", 0);
    294 			simple_lock(&uobj->vmobjlock);
    295 			continue;
    296 		}
    297 
    298 		/* skip PG_RDONLY pages if requested */
    299 		if ((flags & UFP_NORDONLY) && (pg->flags & PG_RDONLY)) {
    300 			UVMHIST_LOG(ubchist, "nordonly",0,0,0,0);
    301 			return 0;
    302 		}
    303 
    304 		/* stop on clean pages if requested */
    305 		if (flags & UFP_DIRTYONLY) {
    306 			dirty = pmap_clear_modify(pg) ||
    307 				(pg->flags & PG_CLEAN) == 0;
    308 			pg->flags |= PG_CLEAN;
    309 			if (!dirty) {
    310 				UVMHIST_LOG(ubchist, "dirtonly", 0,0,0,0);
    311 				return 0;
    312 			}
    313 		}
    314 
    315 		/* mark the page BUSY and we're done. */
    316 		pg->flags |= PG_BUSY;
    317 		UVM_PAGE_OWN(pg, "uvn_findpage");
    318 		UVMHIST_LOG(ubchist, "found %p", pg,0,0,0);
    319 		break;
    320 	}
    321 	*pgp = pg;
    322 	return 1;
    323 }
    324 
    325 /*
    326  * uvm_vnp_setsize: grow or shrink a vnode uobj
    327  *
    328  * grow   => just update size value
    329  * shrink => toss un-needed pages
    330  *
    331  * => we assume that the caller has a reference of some sort to the
    332  *	vnode in question so that it will not be yanked out from under
    333  *	us.
    334  */
    335 
    336 void
    337 uvm_vnp_setsize(struct vnode *vp, voff_t newsize)
    338 {
    339 	struct uvm_object *uobj = &vp->v_uobj;
    340 	voff_t pgend = round_page(newsize);
    341 	voff_t oldsize;
    342 	UVMHIST_FUNC("uvm_vnp_setsize"); UVMHIST_CALLED(ubchist);
    343 
    344 	simple_lock(&uobj->vmobjlock);
    345 	UVMHIST_LOG(ubchist, "vp %p old 0x%x new 0x%x",
    346 	    vp, vp->v_size, newsize, 0);
    347 
    348 	/*
    349 	 * now check if the size has changed: if we shrink we had better
    350 	 * toss some pages...
    351 	 */
    352 
    353 	KASSERT(newsize != VSIZENOTSET);
    354 	KASSERT(vp->v_size <= vp->v_writesize);
    355 	KASSERT(vp->v_size == vp->v_writesize ||
    356 	    newsize == vp->v_writesize || newsize <= vp->v_size);
    357 
    358 	oldsize = vp->v_writesize;
    359 	KASSERT(oldsize != VSIZENOTSET || pgend > oldsize);
    360 
    361 	if (oldsize > pgend) {
    362 		(void) uvn_put(uobj, pgend, 0, PGO_FREE | PGO_SYNCIO);
    363 		simple_lock(&uobj->vmobjlock);
    364 	}
    365 	vp->v_size = vp->v_writesize = newsize;
    366 	simple_unlock(&uobj->vmobjlock);
    367 }
    368 
    369 void
    370 uvm_vnp_setwritesize(struct vnode *vp, voff_t newsize)
    371 {
    372 
    373 	simple_lock(&vp->v_interlock);
    374 	KASSERT(newsize != VSIZENOTSET);
    375 	KASSERT(vp->v_size != VSIZENOTSET);
    376 	KASSERT(vp->v_writesize != VSIZENOTSET);
    377 	KASSERT(vp->v_size <= vp->v_writesize);
    378 	KASSERT(vp->v_size <= newsize);
    379 	vp->v_writesize = newsize;
    380 	simple_unlock(&vp->v_interlock);
    381 }
    382 
    383 /*
    384  * uvm_vnp_zerorange:  set a range of bytes in a file to zero.
    385  */
    386 
    387 void
    388 uvm_vnp_zerorange(struct vnode *vp, off_t off, size_t len)
    389 {
    390 	void *win;
    391 	int flags;
    392 
    393 	/*
    394 	 * XXXUBC invent kzero() and use it
    395 	 */
    396 
    397 	while (len) {
    398 		vsize_t bytelen = len;
    399 
    400 		win = ubc_alloc(&vp->v_uobj, off, &bytelen, UVM_ADV_NORMAL,
    401 		    UBC_WRITE);
    402 		memset(win, 0, bytelen);
    403 		flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
    404 		ubc_release(win, flags);
    405 
    406 		off += bytelen;
    407 		len -= bytelen;
    408 	}
    409 }
    410 
    411 bool
    412 uvn_text_p(struct uvm_object *uobj)
    413 {
    414 	struct vnode *vp = (struct vnode *)uobj;
    415 
    416 	return (vp->v_flag & VEXECMAP) != 0;
    417 }
    418 
    419 bool
    420 uvn_clean_p(struct uvm_object *uobj)
    421 {
    422 	struct vnode *vp = (struct vnode *)uobj;
    423 
    424 	return (vp->v_flag & VONWORKLST) == 0;
    425 }
    426 
    427 bool
    428 uvn_needs_writefault_p(struct uvm_object *uobj)
    429 {
    430 	struct vnode *vp = (struct vnode *)uobj;
    431 
    432 	return uvn_clean_p(uobj) ||
    433 	    (vp->v_flag & (VWRITEMAP|VWRITEMAPDIRTY)) == VWRITEMAP;
    434 }
    435