Annotation of src/sys/uvm/uvm_vnode.c, Revision 1.62.2.5
1.62.2.5! skrll 1: /* $NetBSD: uvm_vnode.c,v 1.62.2.4 2004/09/21 13:39:31 skrll Exp $ */
1.1 mrg 2:
3: /*
4: * Copyright (c) 1997 Charles D. Cranor and Washington University.
5: * Copyright (c) 1991, 1993
1.49 chs 6: * The Regents of the University of California.
1.1 mrg 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,
1.49 chs 26: * Washington University, the University of California, Berkeley and
1.1 mrg 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
1.3 mrg 45: * from: Id: uvm_vnode.c,v 1.1.2.26 1998/02/02 20:38:07 chuck Exp
1.1 mrg 46: */
47:
1.55 lukem 48: /*
49: * uvm_vnode.c: the vnode pager.
50: */
51:
52: #include <sys/cdefs.h>
1.62.2.5! skrll 53: __KERNEL_RCSID(0, "$NetBSD: uvm_vnode.c,v 1.62.2.4 2004/09/21 13:39:31 skrll Exp $");
1.55 lukem 54:
1.6 thorpej 55: #include "fs_nfs.h"
1.4 mrg 56: #include "opt_uvmhist.h"
1.37 chs 57: #include "opt_ddb.h"
1.1 mrg 58:
59: #include <sys/param.h>
60: #include <sys/systm.h>
1.37 chs 61: #include <sys/kernel.h>
1.1 mrg 62: #include <sys/proc.h>
63: #include <sys/malloc.h>
64: #include <sys/vnode.h>
1.13 thorpej 65: #include <sys/disklabel.h>
66: #include <sys/ioctl.h>
67: #include <sys/fcntl.h>
68: #include <sys/conf.h>
1.37 chs 69: #include <sys/pool.h>
70: #include <sys/mount.h>
1.13 thorpej 71:
72: #include <miscfs/specfs/specdev.h>
1.1 mrg 73:
74: #include <uvm/uvm.h>
75:
76: /*
77: * functions
78: */
79:
1.62.2.2 skrll 80: void uvn_detach(struct uvm_object *);
81: int uvn_get(struct uvm_object *, voff_t, struct vm_page **, int *, int,
82: vm_prot_t, int, int);
83: int uvn_put(struct uvm_object *, voff_t, voff_t, int);
84: void uvn_reference(struct uvm_object *);
1.52 chs 85:
1.62.2.2 skrll 86: int uvn_findpage(struct uvm_object *, voff_t, struct vm_page **, int);
1.1 mrg 87:
88: /*
89: * master pager structure
90: */
91:
92: struct uvm_pagerops uvm_vnodeops = {
1.37 chs 93: NULL,
1.8 mrg 94: uvn_reference,
95: uvn_detach,
1.37 chs 96: NULL,
1.8 mrg 97: uvn_get,
98: uvn_put,
1.1 mrg 99: };
100:
101: /*
102: * the ops!
103: */
104:
105: /*
106: * uvn_attach
107: *
108: * attach a vnode structure to a VM object. if the vnode is already
109: * attached, then just bump the reference count by one and return the
110: * VM object. if not already attached, attach and return the new VM obj.
111: * the "accessprot" tells the max access the attaching thread wants to
112: * our pages.
113: *
114: * => caller must _not_ already be holding the lock on the uvm_object.
115: * => in fact, nothing should be locked so that we can sleep here.
116: * => note that uvm_object is first thing in vnode structure, so their
117: * pointers are equiv.
118: */
119:
1.8 mrg 120: struct uvm_object *
121: uvn_attach(arg, accessprot)
122: void *arg;
123: vm_prot_t accessprot;
124: {
125: struct vnode *vp = arg;
1.52 chs 126: struct uvm_object *uobj = &vp->v_uobj;
1.8 mrg 127: struct vattr vattr;
1.59 gehenna 128: const struct bdevsw *bdev;
1.37 chs 129: int result;
1.13 thorpej 130: struct partinfo pi;
1.37 chs 131: voff_t used_vnode_size;
1.8 mrg 132: UVMHIST_FUNC("uvn_attach"); UVMHIST_CALLED(maphist);
133:
134: UVMHIST_LOG(maphist, "(vn=0x%x)", arg,0,0,0);
1.37 chs 135: used_vnode_size = (voff_t)0;
1.13 thorpej 136:
1.8 mrg 137: /*
1.52 chs 138: * first get a lock on the uobj.
1.8 mrg 139: */
1.52 chs 140:
141: simple_lock(&uobj->vmobjlock);
142: while (vp->v_flag & VXLOCK) {
143: vp->v_flag |= VXWANT;
1.8 mrg 144: UVMHIST_LOG(maphist, " SLEEPING on blocked vn",0,0,0,0);
1.52 chs 145: UVM_UNLOCK_AND_WAIT(uobj, &uobj->vmobjlock, FALSE,
1.8 mrg 146: "uvn_attach", 0);
1.52 chs 147: simple_lock(&uobj->vmobjlock);
1.8 mrg 148: UVMHIST_LOG(maphist," WOKE UP",0,0,0,0);
149: }
1.1 mrg 150:
1.8 mrg 151: /*
1.18 bouyer 152: * if we're mapping a BLK device, make sure it is a disk.
1.13 thorpej 153: */
1.59 gehenna 154: if (vp->v_type == VBLK) {
155: bdev = bdevsw_lookup(vp->v_rdev);
156: if (bdev == NULL || bdev->d_type != D_DISK) {
157: simple_unlock(&uobj->vmobjlock);
158: UVMHIST_LOG(maphist,"<- done (VBLK not D_DISK!)",
159: 0,0,0,0);
160: return(NULL);
161: }
1.13 thorpej 162: }
1.51 chs 163: KASSERT(vp->v_type == VREG || vp->v_type == VBLK);
1.37 chs 164:
1.13 thorpej 165: /*
1.37 chs 166: * set up our idea of the size
167: * if this hasn't been done already.
1.8 mrg 168: */
1.52 chs 169: if (vp->v_size == VSIZENOTSET) {
1.8 mrg 170:
1.52 chs 171:
172: vp->v_flag |= VXLOCK;
173: simple_unlock(&uobj->vmobjlock); /* drop lock in case we sleep */
1.8 mrg 174: /* XXX: curproc? */
1.13 thorpej 175: if (vp->v_type == VBLK) {
176: /*
177: * We could implement this as a specfs getattr call, but:
178: *
179: * (1) VOP_GETATTR() would get the file system
180: * vnode operation, not the specfs operation.
181: *
182: * (2) All we want is the size, anyhow.
183: */
1.59 gehenna 184: bdev = bdevsw_lookup(vp->v_rdev);
185: if (bdev != NULL) {
186: result = (*bdev->d_ioctl)(vp->v_rdev, DIOCGPART,
1.62.2.4 skrll 187: (caddr_t)&pi, FREAD, curlwp);
1.59 gehenna 188: } else {
189: result = ENXIO;
190: }
1.13 thorpej 191: if (result == 0) {
192: /* XXX should remember blocksize */
1.37 chs 193: used_vnode_size = (voff_t)pi.disklab->d_secsize *
194: (voff_t)pi.part->p_size;
1.13 thorpej 195: }
196: } else {
1.62.2.4 skrll 197: result = VOP_GETATTR(vp, &vattr, curproc->p_ucred, curlwp);
1.13 thorpej 198: if (result == 0)
199: used_vnode_size = vattr.va_size;
1.8 mrg 200: }
1.1 mrg 201:
1.8 mrg 202: /* relock object */
1.52 chs 203: simple_lock(&uobj->vmobjlock);
1.37 chs 204:
1.52 chs 205: if (vp->v_flag & VXWANT) {
206: wakeup(vp);
207: }
208: vp->v_flag &= ~(VXLOCK|VXWANT);
1.1 mrg 209:
1.8 mrg 210: if (result != 0) {
1.52 chs 211: simple_unlock(&uobj->vmobjlock);
1.8 mrg 212: UVMHIST_LOG(maphist,"<- done (VOP_GETATTR FAILED!)", 0,0,0,0);
213: return(NULL);
214: }
1.52 chs 215: vp->v_size = used_vnode_size;
1.8 mrg 216:
217: }
218:
1.52 chs 219: simple_unlock(&uobj->vmobjlock);
220: UVMHIST_LOG(maphist,"<- done, refcnt=%d", vp->v_usecount,
1.37 chs 221: 0, 0, 0);
1.52 chs 222: return uobj;
1.1 mrg 223: }
224:
225:
226: /*
227: * uvn_reference
228: *
229: * duplicate a reference to a VM object. Note that the reference
1.49 chs 230: * count must already be at least one (the passed in reference) so
1.1 mrg 231: * there is no chance of the uvn being killed or locked out here.
232: *
1.49 chs 233: * => caller must call with object unlocked.
1.1 mrg 234: * => caller must be using the same accessprot as was used at attach time
235: */
236:
1.52 chs 237: void
1.8 mrg 238: uvn_reference(uobj)
239: struct uvm_object *uobj;
1.1 mrg 240: {
1.37 chs 241: VREF((struct vnode *)uobj);
1.1 mrg 242: }
243:
1.52 chs 244:
1.1 mrg 245: /*
246: * uvn_detach
247: *
248: * remove a reference to a VM object.
249: *
250: * => caller must call with object unlocked and map locked.
251: */
1.52 chs 252:
253: void
1.8 mrg 254: uvn_detach(uobj)
255: struct uvm_object *uobj;
256: {
1.37 chs 257: vrele((struct vnode *)uobj);
1.1 mrg 258: }
259:
260: /*
261: * uvn_put: flush page data to backing store.
262: *
1.53 sommerfe 263: * => object must be locked on entry! VOP_PUTPAGES must unlock it.
1.1 mrg 264: * => flags: PGO_SYNCIO -- use sync. I/O
265: * => note: caller must set PG_CLEAN and pmap_clear_modify (if needed)
266: */
267:
1.52 chs 268: int
1.54 chs 269: uvn_put(uobj, offlo, offhi, flags)
1.8 mrg 270: struct uvm_object *uobj;
1.54 chs 271: voff_t offlo;
272: voff_t offhi;
1.52 chs 273: int flags;
1.1 mrg 274: {
1.37 chs 275: struct vnode *vp = (struct vnode *)uobj;
276: int error;
1.1 mrg 277:
1.53 sommerfe 278: LOCK_ASSERT(simple_lock_held(&vp->v_interlock));
1.54 chs 279: error = VOP_PUTPAGES(vp, offlo, offhi, flags);
1.53 sommerfe 280: LOCK_ASSERT(!simple_lock_held(&vp->v_interlock));
1.48 chs 281: return error;
1.1 mrg 282: }
283:
284:
285: /*
286: * uvn_get: get pages (synchronously) from backing store
287: *
288: * => prefer map unlocked (not required)
289: * => object must be locked! we will _unlock_ it before starting any I/O.
290: * => flags: PGO_ALLPAGES: get all of the pages
291: * PGO_LOCKED: fault data structures are locked
292: * => NOTE: offset is the offset of pps[0], _NOT_ pps[centeridx]
293: * => NOTE: caller must check for released pages!!
294: */
1.49 chs 295:
1.52 chs 296: int
1.8 mrg 297: uvn_get(uobj, offset, pps, npagesp, centeridx, access_type, advice, flags)
298: struct uvm_object *uobj;
1.30 kleink 299: voff_t offset;
1.8 mrg 300: struct vm_page **pps; /* IN/OUT */
301: int *npagesp; /* IN (OUT if PGO_LOCKED) */
1.37 chs 302: int centeridx;
1.8 mrg 303: vm_prot_t access_type;
1.37 chs 304: int advice, flags;
1.8 mrg 305: {
1.37 chs 306: struct vnode *vp = (struct vnode *)uobj;
307: int error;
308: UVMHIST_FUNC("uvn_get"); UVMHIST_CALLED(ubchist);
309:
310: UVMHIST_LOG(ubchist, "vp %p off 0x%x", vp, (int)offset, 0,0);
311: error = VOP_GETPAGES(vp, offset, pps, npagesp, centeridx,
312: access_type, advice, flags);
1.48 chs 313: return error;
1.37 chs 314: }
1.8 mrg 315:
316:
1.37 chs 317: /*
318: * uvn_findpages:
319: * return the page for the uobj and offset requested, allocating if needed.
320: * => uobj must be locked.
1.52 chs 321: * => returned pages will be BUSY.
1.37 chs 322: */
1.1 mrg 323:
1.58 enami 324: int
1.52 chs 325: uvn_findpages(uobj, offset, npagesp, pgs, flags)
1.37 chs 326: struct uvm_object *uobj;
327: voff_t offset;
328: int *npagesp;
1.52 chs 329: struct vm_page **pgs;
1.37 chs 330: int flags;
331: {
1.58 enami 332: int i, count, found, npages, rv;
1.8 mrg 333:
1.58 enami 334: count = found = 0;
1.37 chs 335: npages = *npagesp;
1.52 chs 336: if (flags & UFP_BACKWARD) {
337: for (i = npages - 1; i >= 0; i--, offset -= PAGE_SIZE) {
338: rv = uvn_findpage(uobj, offset, &pgs[i], flags);
1.58 enami 339: if (rv == 0) {
340: if (flags & UFP_DIRTYONLY)
341: break;
342: } else
343: found++;
1.52 chs 344: count++;
345: }
346: } else {
347: for (i = 0; i < npages; i++, offset += PAGE_SIZE) {
348: rv = uvn_findpage(uobj, offset, &pgs[i], flags);
1.58 enami 349: if (rv == 0) {
350: if (flags & UFP_DIRTYONLY)
351: break;
352: } else
353: found++;
1.52 chs 354: count++;
355: }
1.37 chs 356: }
1.52 chs 357: *npagesp = count;
1.58 enami 358: return (found);
1.37 chs 359: }
1.8 mrg 360:
1.52 chs 361: int
1.37 chs 362: uvn_findpage(uobj, offset, pgp, flags)
363: struct uvm_object *uobj;
364: voff_t offset;
365: struct vm_page **pgp;
366: int flags;
367: {
368: struct vm_page *pg;
1.52 chs 369: boolean_t dirty;
1.37 chs 370: UVMHIST_FUNC("uvn_findpage"); UVMHIST_CALLED(ubchist);
371: UVMHIST_LOG(ubchist, "vp %p off 0x%lx", uobj, offset,0,0);
1.8 mrg 372:
1.37 chs 373: if (*pgp != NULL) {
374: UVMHIST_LOG(ubchist, "dontcare", 0,0,0,0);
375: return 0;
376: }
377: for (;;) {
378: /* look for an existing page */
379: pg = uvm_pagelookup(uobj, offset);
380:
1.52 chs 381: /* nope? allocate one now */
1.37 chs 382: if (pg == NULL) {
383: if (flags & UFP_NOALLOC) {
384: UVMHIST_LOG(ubchist, "noalloc", 0,0,0,0);
385: return 0;
386: }
1.47 chs 387: pg = uvm_pagealloc(uobj, offset, NULL, 0);
1.37 chs 388: if (pg == NULL) {
389: if (flags & UFP_NOWAIT) {
390: UVMHIST_LOG(ubchist, "nowait",0,0,0,0);
391: return 0;
1.8 mrg 392: }
1.37 chs 393: simple_unlock(&uobj->vmobjlock);
394: uvm_wait("uvn_fp1");
1.8 mrg 395: simple_lock(&uobj->vmobjlock);
1.37 chs 396: continue;
1.47 chs 397: }
1.52 chs 398: UVMHIST_LOG(ubchist, "alloced %p", pg,0,0,0);
1.37 chs 399: break;
400: } else if (flags & UFP_NOCACHE) {
401: UVMHIST_LOG(ubchist, "nocache",0,0,0,0);
402: return 0;
1.8 mrg 403: }
404:
1.37 chs 405: /* page is there, see if we need to wait on it */
1.52 chs 406: if ((pg->flags & PG_BUSY) != 0) {
1.37 chs 407: if (flags & UFP_NOWAIT) {
408: UVMHIST_LOG(ubchist, "nowait",0,0,0,0);
409: return 0;
410: }
411: pg->flags |= PG_WANTED;
1.58 enami 412: UVMHIST_LOG(ubchist, "wait %p", pg,0,0,0);
1.37 chs 413: UVM_UNLOCK_AND_WAIT(pg, &uobj->vmobjlock, 0,
414: "uvn_fp2", 0);
415: simple_lock(&uobj->vmobjlock);
416: continue;
1.8 mrg 417: }
1.49 chs 418:
1.37 chs 419: /* skip PG_RDONLY pages if requested */
420: if ((flags & UFP_NORDONLY) && (pg->flags & PG_RDONLY)) {
421: UVMHIST_LOG(ubchist, "nordonly",0,0,0,0);
422: return 0;
1.8 mrg 423: }
424:
1.52 chs 425: /* stop on clean pages if requested */
426: if (flags & UFP_DIRTYONLY) {
427: dirty = pmap_clear_modify(pg) ||
428: (pg->flags & PG_CLEAN) == 0;
429: pg->flags |= PG_CLEAN;
430: if (!dirty) {
1.58 enami 431: UVMHIST_LOG(ubchist, "dirtonly", 0,0,0,0);
1.52 chs 432: return 0;
433: }
434: }
435:
1.37 chs 436: /* mark the page BUSY and we're done. */
437: pg->flags |= PG_BUSY;
438: UVM_PAGE_OWN(pg, "uvn_findpage");
1.52 chs 439: UVMHIST_LOG(ubchist, "found %p", pg,0,0,0);
1.37 chs 440: break;
1.8 mrg 441: }
1.37 chs 442: *pgp = pg;
443: return 1;
1.1 mrg 444: }
445:
446: /*
1.52 chs 447: * uvm_vnp_setsize: grow or shrink a vnode uobj
1.1 mrg 448: *
449: * grow => just update size value
450: * shrink => toss un-needed pages
451: *
1.49 chs 452: * => we assume that the caller has a reference of some sort to the
1.1 mrg 453: * vnode in question so that it will not be yanked out from under
454: * us.
455: */
456:
1.8 mrg 457: void
458: uvm_vnp_setsize(vp, newsize)
459: struct vnode *vp;
1.30 kleink 460: voff_t newsize;
1.8 mrg 461: {
1.52 chs 462: struct uvm_object *uobj = &vp->v_uobj;
1.46 enami 463: voff_t pgend = round_page(newsize);
1.37 chs 464: UVMHIST_FUNC("uvm_vnp_setsize"); UVMHIST_CALLED(ubchist);
465:
1.52 chs 466: simple_lock(&uobj->vmobjlock);
467: UVMHIST_LOG(ubchist, "vp %p old 0x%x new 0x%x",
468: vp, vp->v_size, newsize, 0);
1.1 mrg 469:
1.8 mrg 470: /*
1.37 chs 471: * now check if the size has changed: if we shrink we had better
472: * toss some pages...
1.8 mrg 473: */
1.1 mrg 474:
1.52 chs 475: if (vp->v_size > pgend && vp->v_size != VSIZENOTSET) {
1.57 chs 476: (void) uvn_put(uobj, pgend, 0, PGO_FREE | PGO_SYNCIO);
1.52 chs 477: } else {
478: simple_unlock(&uobj->vmobjlock);
1.8 mrg 479: }
1.52 chs 480: vp->v_size = newsize;
1.1 mrg 481: }
482:
483: /*
1.37 chs 484: * uvm_vnp_zerorange: set a range of bytes in a file to zero.
1.1 mrg 485: */
486:
1.8 mrg 487: void
1.37 chs 488: uvm_vnp_zerorange(vp, off, len)
489: struct vnode *vp;
490: off_t off;
491: size_t len;
1.8 mrg 492: {
1.62.2.5! skrll 493: void *win;
! 494: int flags;
! 495:
! 496: /*
! 497: * XXXUBC invent kzero() and use it
! 498: */
1.8 mrg 499:
1.62.2.5! skrll 500: while (len) {
! 501: vsize_t bytelen = len;
! 502:
! 503: win = ubc_alloc(&vp->v_uobj, off, &bytelen, UBC_WRITE);
! 504: memset(win, 0, bytelen);
! 505: flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
! 506: ubc_release(win, flags);
! 507:
! 508: off += bytelen;
! 509: len -= bytelen;
! 510: }
1.1 mrg 511: }
CVSweb <webmaster@jp.NetBSD.org>