uvm_vnode.c revision 1.81.2.8 1 /* $NetBSD: uvm_vnode.c,v 1.81.2.8 2007/07/15 15:53:08 ad 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.81.2.8 2007/07/15 15:53:08 ad 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_attach
109 *
110 * attach a vnode structure to a VM object. if the vnode is already
111 * attached, then just bump the reference count by one and return the
112 * VM object. if not already attached, attach and return the new VM obj.
113 * the "accessprot" tells the max access the attaching thread wants to
114 * our pages.
115 *
116 * => caller must _not_ already be holding the lock on the uvm_object.
117 * => in fact, nothing should be locked so that we can sleep here.
118 * => note that uvm_object is first thing in vnode structure, so their
119 * pointers are equiv.
120 */
121
122 struct uvm_object *
123 uvn_attach(void *arg, vm_prot_t accessprot)
124 {
125 struct vnode *vp = arg;
126 struct uvm_object *uobj = &vp->v_uobj;
127 struct vattr vattr;
128 int result;
129 struct partinfo pi;
130 voff_t used_vnode_size;
131 UVMHIST_FUNC("uvn_attach"); UVMHIST_CALLED(maphist);
132
133 UVMHIST_LOG(maphist, "(vn=0x%x)", arg,0,0,0);
134 used_vnode_size = (voff_t)0;
135
136 /*
137 * first get a lock on the uobj.
138 */
139
140 mutex_enter(&uobj->vmobjlock);
141 while (vp->v_iflag & VI_XLOCK) {
142 UVMHIST_LOG(maphist, " SLEEPING on blocked vn",0,0,0,0);
143 vwait(vp, VI_XLOCK);
144 UVMHIST_LOG(maphist," WOKE UP",0,0,0,0);
145 }
146
147 /*
148 * if we're mapping a BLK device, make sure it is a disk.
149 */
150 if (vp->v_type == VBLK) {
151 if (bdev_type(vp->v_rdev) != D_DISK) {
152 mutex_exit(&uobj->vmobjlock);
153 UVMHIST_LOG(maphist,"<- done (VBLK not D_DISK!)",
154 0,0,0,0);
155 return(NULL);
156 }
157 }
158 KASSERT(vp->v_type == VREG || vp->v_type == VBLK);
159
160 /*
161 * set up our idea of the size
162 * if this hasn't been done already.
163 */
164 if (vp->v_size == VSIZENOTSET) {
165
166
167 vp->v_iflag |= VI_XLOCK;
168 mutex_exit(&uobj->vmobjlock); /* drop lock in case we sleep */
169 /* XXX: curproc? */
170 if (vp->v_type == VBLK) {
171 /*
172 * We could implement this as a specfs getattr call, but:
173 *
174 * (1) VOP_GETATTR() would get the file system
175 * vnode operation, not the specfs operation.
176 *
177 * (2) All we want is the size, anyhow.
178 */
179 result = bdev_ioctl(vp->v_rdev, DIOCGPART, (void *)&pi,
180 FREAD, curlwp);
181 if (result == 0) {
182 /* XXX should remember blocksize */
183 used_vnode_size = (voff_t)pi.disklab->d_secsize *
184 (voff_t)pi.part->p_size;
185 }
186 } else {
187 result = VOP_GETATTR(vp, &vattr, curlwp->l_cred, curlwp);
188 if (result == 0)
189 used_vnode_size = vattr.va_size;
190 }
191
192 /* relock object */
193 mutex_enter(&uobj->vmobjlock);
194 vunwait(vp, VI_XLOCK);
195
196 if (result != 0) {
197 mutex_exit(&uobj->vmobjlock);
198 UVMHIST_LOG(maphist,"<- done (VOP_GETATTR FAILED!)", 0,0,0,0);
199 return(NULL);
200 }
201 vp->v_size = vp->v_writesize = used_vnode_size;
202
203 }
204
205 mutex_exit(&uobj->vmobjlock);
206 UVMHIST_LOG(maphist,"<- done, refcnt=%d", vp->v_usecount,
207 0, 0, 0);
208
209 return uobj;
210 }
211
212
213 /*
214 * uvn_reference
215 *
216 * duplicate a reference to a VM object. Note that the reference
217 * count must already be at least one (the passed in reference) so
218 * there is no chance of the uvn being killed or locked out here.
219 *
220 * => caller must call with object unlocked.
221 * => caller must be using the same accessprot as was used at attach time
222 */
223
224 static void
225 uvn_reference(struct uvm_object *uobj)
226 {
227 VREF((struct vnode *)uobj);
228 }
229
230
231 /*
232 * uvn_detach
233 *
234 * remove a reference to a VM object.
235 *
236 * => caller must call with object unlocked and map locked.
237 */
238
239 static void
240 uvn_detach(struct uvm_object *uobj)
241 {
242 vrele((struct vnode *)uobj);
243 }
244
245 /*
246 * uvn_put: flush page data to backing store.
247 *
248 * => object must be locked on entry! VOP_PUTPAGES must unlock it.
249 * => flags: PGO_SYNCIO -- use sync. I/O
250 * => note: caller must set PG_CLEAN and pmap_clear_modify (if needed)
251 */
252
253 static int
254 uvn_put(struct uvm_object *uobj, voff_t offlo, voff_t offhi, int flags)
255 {
256 struct vnode *vp = (struct vnode *)uobj;
257 int error;
258
259 KASSERT(mutex_owned(&vp->v_interlock));
260 error = VOP_PUTPAGES(vp, offlo, offhi, flags);
261
262 return error;
263 }
264
265
266 /*
267 * uvn_get: get pages (synchronously) from backing store
268 *
269 * => prefer map unlocked (not required)
270 * => object must be locked! we will _unlock_ it before starting any I/O.
271 * => flags: PGO_ALLPAGES: get all of the pages
272 * PGO_LOCKED: fault data structures are locked
273 * => NOTE: offset is the offset of pps[0], _NOT_ pps[centeridx]
274 * => NOTE: caller must check for released pages!!
275 */
276
277 static int
278 uvn_get(struct uvm_object *uobj, voff_t offset,
279 struct vm_page **pps /* IN/OUT */,
280 int *npagesp /* IN (OUT if PGO_LOCKED)*/,
281 int centeridx, vm_prot_t access_type, int advice, int flags)
282 {
283 struct vnode *vp = (struct vnode *)uobj;
284 int error;
285
286 UVMHIST_FUNC("uvn_get"); UVMHIST_CALLED(ubchist);
287
288 UVMHIST_LOG(ubchist, "vp %p off 0x%x", vp, (int)offset, 0,0);
289
290 if ((access_type & VM_PROT_WRITE) == 0 && (flags & PGO_LOCKED) == 0) {
291 mutex_exit(&vp->v_interlock);
292 vn_ra_allocctx(vp);
293 uvm_ra_request(vp->v_ractx, advice, uobj, offset,
294 *npagesp << PAGE_SHIFT);
295 mutex_enter(&vp->v_interlock);
296 }
297
298 error = VOP_GETPAGES(vp, offset, pps, npagesp, centeridx,
299 access_type, advice, flags);
300
301 KASSERT(((flags & PGO_LOCKED) != 0 &&
302 mutex_owned(&vp->v_interlock)) ||
303 ((flags & PGO_LOCKED) == 0 &&
304 !mutex_owned(&vp->v_interlock)));
305 return error;
306 }
307
308
309 /*
310 * uvn_findpages:
311 * return the page for the uobj and offset requested, allocating if needed.
312 * => uobj must be locked.
313 * => returned pages will be BUSY.
314 */
315
316 int
317 uvn_findpages(struct uvm_object *uobj, voff_t offset, int *npagesp,
318 struct vm_page **pgs, int flags)
319 {
320 int i, count, found, npages, rv;
321
322 count = found = 0;
323 npages = *npagesp;
324 if (flags & UFP_BACKWARD) {
325 for (i = npages - 1; i >= 0; i--, offset -= PAGE_SIZE) {
326 rv = uvn_findpage(uobj, offset, &pgs[i], flags);
327 if (rv == 0) {
328 if (flags & UFP_DIRTYONLY)
329 break;
330 } else
331 found++;
332 count++;
333 }
334 } else {
335 for (i = 0; i < npages; i++, offset += PAGE_SIZE) {
336 rv = uvn_findpage(uobj, offset, &pgs[i], flags);
337 if (rv == 0) {
338 if (flags & UFP_DIRTYONLY)
339 break;
340 } else
341 found++;
342 count++;
343 }
344 }
345 *npagesp = count;
346 return (found);
347 }
348
349 static int
350 uvn_findpage(struct uvm_object *uobj, voff_t offset, struct vm_page **pgp,
351 int flags)
352 {
353 struct vm_page *pg;
354 bool dirty;
355 UVMHIST_FUNC("uvn_findpage"); UVMHIST_CALLED(ubchist);
356 UVMHIST_LOG(ubchist, "vp %p off 0x%lx", uobj, offset,0,0);
357
358 if (*pgp != NULL) {
359 UVMHIST_LOG(ubchist, "dontcare", 0,0,0,0);
360 return 0;
361 }
362 for (;;) {
363 /* look for an existing page */
364 pg = uvm_pagelookup(uobj, offset);
365
366 /* nope? allocate one now */
367 if (pg == NULL) {
368 if (flags & UFP_NOALLOC) {
369 UVMHIST_LOG(ubchist, "noalloc", 0,0,0,0);
370 return 0;
371 }
372 pg = uvm_pagealloc(uobj, offset, NULL, 0);
373 if (pg == NULL) {
374 if (flags & UFP_NOWAIT) {
375 UVMHIST_LOG(ubchist, "nowait",0,0,0,0);
376 return 0;
377 }
378 mutex_exit(&uobj->vmobjlock);
379 uvm_wait("uvn_fp1");
380 mutex_enter(&uobj->vmobjlock);
381 continue;
382 }
383 UVMHIST_LOG(ubchist, "alloced %p", pg,0,0,0);
384 break;
385 } else if (flags & UFP_NOCACHE) {
386 UVMHIST_LOG(ubchist, "nocache",0,0,0,0);
387 return 0;
388 }
389
390 /* page is there, see if we need to wait on it */
391 if ((pg->flags & PG_BUSY) != 0) {
392 if (flags & UFP_NOWAIT) {
393 UVMHIST_LOG(ubchist, "nowait",0,0,0,0);
394 return 0;
395 }
396 pg->flags |= PG_WANTED;
397 UVMHIST_LOG(ubchist, "wait %p", pg,0,0,0);
398 UVM_UNLOCK_AND_WAIT(pg, &uobj->vmobjlock, 0,
399 "uvn_fp2", 0);
400 mutex_enter(&uobj->vmobjlock);
401 continue;
402 }
403
404 /* skip PG_RDONLY pages if requested */
405 if ((flags & UFP_NORDONLY) && (pg->flags & PG_RDONLY)) {
406 UVMHIST_LOG(ubchist, "nordonly",0,0,0,0);
407 return 0;
408 }
409
410 /* stop on clean pages if requested */
411 if (flags & UFP_DIRTYONLY) {
412 dirty = pmap_clear_modify(pg) ||
413 (pg->flags & PG_CLEAN) == 0;
414 pg->flags |= PG_CLEAN;
415 if (!dirty) {
416 UVMHIST_LOG(ubchist, "dirtonly", 0,0,0,0);
417 return 0;
418 }
419 }
420
421 /* mark the page BUSY and we're done. */
422 pg->flags |= PG_BUSY;
423 UVM_PAGE_OWN(pg, "uvn_findpage");
424 UVMHIST_LOG(ubchist, "found %p", pg,0,0,0);
425 break;
426 }
427 *pgp = pg;
428 return 1;
429 }
430
431 /*
432 * uvm_vnp_setsize: grow or shrink a vnode uobj
433 *
434 * grow => just update size value
435 * shrink => toss un-needed pages
436 *
437 * => we assume that the caller has a reference of some sort to the
438 * vnode in question so that it will not be yanked out from under
439 * us.
440 */
441
442 void
443 uvm_vnp_setsize(struct vnode *vp, voff_t newsize)
444 {
445 struct uvm_object *uobj = &vp->v_uobj;
446 voff_t pgend = round_page(newsize);
447 voff_t oldsize;
448 UVMHIST_FUNC("uvm_vnp_setsize"); UVMHIST_CALLED(ubchist);
449
450 mutex_enter(&uobj->vmobjlock);
451 UVMHIST_LOG(ubchist, "vp %p old 0x%x new 0x%x",
452 vp, vp->v_size, newsize, 0);
453
454 /*
455 * now check if the size has changed: if we shrink we had better
456 * toss some pages...
457 */
458
459 if (vp->v_writesize != VSIZENOTSET) {
460 KASSERT(vp->v_size <= vp->v_writesize);
461 KASSERT(vp->v_size == vp->v_writesize ||
462 newsize == vp->v_writesize || newsize <= vp->v_size);
463 oldsize = vp->v_writesize;
464 } else {
465 oldsize = vp->v_size;
466 }
467 if (oldsize > pgend && oldsize != VSIZENOTSET) {
468 (void) uvn_put(uobj, pgend, 0, PGO_FREE | PGO_SYNCIO);
469 mutex_enter(&uobj->vmobjlock);
470 }
471 vp->v_size = vp->v_writesize = newsize;
472 mutex_exit(&uobj->vmobjlock);
473 }
474
475 void
476 uvm_vnp_setwritesize(struct vnode *vp, voff_t newsize)
477 {
478
479 mutex_enter(&vp->v_interlock);
480 KASSERT(vp->v_size != VSIZENOTSET);
481 KASSERT(vp->v_writesize != VSIZENOTSET);
482 KASSERT(vp->v_size <= vp->v_writesize);
483 KASSERT(vp->v_size <= newsize);
484 vp->v_writesize = newsize;
485 mutex_exit(&vp->v_interlock);
486 }
487
488 /*
489 * uvm_vnp_zerorange: set a range of bytes in a file to zero.
490 */
491
492 void
493 uvm_vnp_zerorange(struct vnode *vp, off_t off, size_t len)
494 {
495 void *win;
496 int flags;
497
498 /*
499 * XXXUBC invent kzero() and use it
500 */
501
502 while (len) {
503 vsize_t bytelen = len;
504
505 win = ubc_alloc(&vp->v_uobj, off, &bytelen, UVM_ADV_NORMAL,
506 UBC_WRITE);
507 memset(win, 0, bytelen);
508 flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
509 ubc_release(win, flags);
510
511 off += bytelen;
512 len -= bytelen;
513 }
514 }
515
516 bool
517 uvn_text_p(struct uvm_object *uobj)
518 {
519 struct vnode *vp = (struct vnode *)uobj;
520
521 return (vp->v_iflag & VI_EXECMAP) != 0;
522 }
523
524 bool
525 uvn_clean_p(struct uvm_object *uobj)
526 {
527 struct vnode *vp = (struct vnode *)uobj;
528
529 return (vp->v_iflag & VI_ONWORKLST) == 0;
530 }
531
532 bool
533 uvn_needs_writefault_p(struct uvm_object *uobj)
534 {
535 struct vnode *vp = (struct vnode *)uobj;
536
537 return uvn_clean_p(uobj) ||
538 (vp->v_iflag & (VI_WRMAP|VI_WRMAPDIRTY)) == VI_WRMAP;
539 }
540