uvm_vnode.c revision 1.22.2.1.2.6 1 /* $NetBSD: uvm_vnode.c,v 1.22.2.1.2.6 1999/08/06 12:47:28 chs 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 #include "fs_nfs.h"
49 #include "opt_uvmhist.h"
50 #include "opt_ddb.h"
51
52 /*
53 * uvm_vnode.c: the vnode pager.
54 */
55
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/kernel.h>
59 #include <sys/proc.h>
60 #include <sys/malloc.h>
61 #include <sys/vnode.h>
62 #include <sys/disklabel.h>
63 #include <sys/ioctl.h>
64 #include <sys/fcntl.h>
65 #include <sys/conf.h>
66 #include <sys/pool.h>
67 #include <sys/mount.h>
68
69 #include <miscfs/specfs/specdev.h>
70
71 #include <vm/vm.h>
72 #include <vm/vm_page.h>
73 #include <vm/vm_kern.h>
74
75 #include <uvm/uvm.h>
76 #include <uvm/uvm_vnode.h>
77
78 /*
79 * private global data structure
80 *
81 * we keep a list of writeable active vnode-backed VM objects for sync op.
82 * we keep a simpleq of vnodes that are currently being sync'd.
83 */
84
85 LIST_HEAD(uvn_list_struct, uvm_vnode);
86 static struct uvn_list_struct uvn_wlist; /* writeable uvns */
87 static simple_lock_data_t uvn_wl_lock; /* locks uvn_wlist */
88
89 SIMPLEQ_HEAD(uvn_sq_struct, uvm_vnode);
90 static struct uvn_sq_struct uvn_sync_q; /* sync'ing uvns */
91 lock_data_t uvn_sync_lock; /* locks sync operation */
92
93 /*
94 * functions
95 */
96
97 static int uvn_asyncget __P((struct uvm_object *, vaddr_t,
98 int));
99 struct uvm_object * uvn_attach __P((void *, vm_prot_t));
100 static void uvn_cluster __P((struct uvm_object *, vaddr_t,
101 vaddr_t *, vaddr_t *));
102 static void uvn_detach __P((struct uvm_object *));
103 static int uvn_findpage __P((struct uvm_object *, vaddr_t,
104 struct vm_page **, int));
105 static boolean_t uvn_flush __P((struct uvm_object *, vaddr_t,
106 vaddr_t, int));
107 static int uvn_get __P((struct uvm_object *, vaddr_t,
108 vm_page_t *, int *, int,
109 vm_prot_t, int, int));
110 static void uvn_init __P((void));
111 static int uvn_put __P((struct uvm_object *, vm_page_t *,
112 int, boolean_t));
113 static void uvn_reference __P((struct uvm_object *));
114 static boolean_t uvn_releasepg __P((struct vm_page *,
115 struct vm_page **));
116 static void uvn_doasyncget __P((struct vm_page **, size_t,
117 daddr_t));
118
119 /*
120 * master pager structure
121 */
122
123 struct uvm_pagerops uvm_vnodeops = {
124 uvn_init,
125 uvn_reference,
126 uvn_detach,
127 NULL, /* no specialized fault routine required */
128 uvn_flush,
129 uvn_get,
130 uvn_asyncget,
131 uvn_put,
132 uvn_cluster,
133 uvm_mk_pcluster, /* use generic version of this: see uvm_pager.c */
134 uvm_shareprot, /* !NULL: allow us in share maps */
135 NULL, /* AIO-DONE function (not until we have asyncio) */
136 uvn_releasepg,
137 };
138
139 /*
140 * the ops!
141 */
142
143 /*
144 * uvn_init
145 *
146 * init pager private data structures.
147 */
148
149 static void
150 uvn_init()
151 {
152
153 LIST_INIT(&uvn_wlist);
154 simple_lock_init(&uvn_wl_lock);
155 /* note: uvn_sync_q init'd in uvm_vnp_sync() */
156 lockinit(&uvn_sync_lock, PVM, "uvnsync", 0, 0);
157 }
158
159 /*
160 * uvn_attach
161 *
162 * attach a vnode structure to a VM object. if the vnode is already
163 * attached, then just bump the reference count by one and return the
164 * VM object. if not already attached, attach and return the new VM obj.
165 * the "accessprot" tells the max access the attaching thread wants to
166 * our pages.
167 *
168 * => caller must _not_ already be holding the lock on the uvm_object.
169 * => in fact, nothing should be locked so that we can sleep here.
170 * => note that uvm_object is first thing in vnode structure, so their
171 * pointers are equiv.
172 */
173
174 struct uvm_object *
175 uvn_attach(arg, accessprot)
176 void *arg;
177 vm_prot_t accessprot;
178 {
179 struct vnode *vp = arg;
180 struct uvm_vnode *uvn = &vp->v_uvm;
181 struct vattr vattr;
182 int result;
183 struct partinfo pi;
184 off_t used_vnode_size;
185 UVMHIST_FUNC("uvn_attach"); UVMHIST_CALLED(maphist);
186
187 UVMHIST_LOG(maphist, "(vn=0x%x)", arg,0,0,0);
188
189 used_vnode_size = (u_quad_t)0; /* XXX gcc -Wuninitialized */
190
191 /*
192 * first get a lock on the uvn.
193 */
194 simple_lock(&uvn->u_obj.vmobjlock);
195 while (uvn->u_flags & VXLOCK) {
196 uvn->u_flags |= VXWANT;
197 UVMHIST_LOG(maphist, " SLEEPING on blocked vn",0,0,0,0);
198 UVM_UNLOCK_AND_WAIT(uvn, &uvn->u_obj.vmobjlock, FALSE,
199 "uvn_attach", 0);
200 simple_lock(&uvn->u_obj.vmobjlock);
201 UVMHIST_LOG(maphist," WOKE UP",0,0,0,0);
202 }
203
204 /*
205 * if we're mapping a BLK device, make sure it is a disk.
206 */
207 if (vp->v_type == VBLK && bdevsw[major(vp->v_rdev)].d_type != D_DISK) {
208 simple_unlock(&uvn->u_obj.vmobjlock);
209 UVMHIST_LOG(maphist,"<- done (VBLK not D_DISK!)", 0,0,0,0);
210 return(NULL);
211 }
212
213 /* check for new writeable uvn */
214 if ((accessprot & VM_PROT_WRITE) != 0 &&
215 (uvn->u_flags & VDIRTY) == 0) {
216 simple_lock(&uvn_wl_lock);
217 uvn->u_flags |= VDIRTY;
218 LIST_INSERT_HEAD(&uvn_wlist, uvn, u_wlist);
219 simple_unlock(&uvn_wl_lock);
220 }
221 #ifdef DIAGNOSTIC
222 if (vp->v_type != VREG) {
223 panic("uvn_attach: vp %p not VREG", vp);
224 }
225 #endif
226
227 /*
228 * set up our idea of the size
229 * if this hasn't been done already.
230 */
231 if (uvn->u_size == VSIZENOTSET) {
232
233 uvn->u_flags |= VXLOCK;
234 simple_unlock(&uvn->u_obj.vmobjlock); /* drop lock in case we sleep */
235 /* XXX: curproc? */
236 if (vp->v_type == VBLK) {
237 /*
238 * We could implement this as a specfs getattr call, but:
239 *
240 * (1) VOP_GETATTR() would get the file system
241 * vnode operation, not the specfs operation.
242 *
243 * (2) All we want is the size, anyhow.
244 */
245 result = (*bdevsw[major(vp->v_rdev)].d_ioctl)(vp->v_rdev,
246 DIOCGPART, (caddr_t)&pi, FREAD, curproc);
247 if (result == 0) {
248 /* XXX should remember blocksize */
249 used_vnode_size = (u_quad_t)pi.disklab->d_secsize *
250 (u_quad_t)pi.part->p_size;
251 }
252 } else {
253 result = VOP_GETATTR(vp, &vattr, curproc->p_ucred, curproc);
254 if (result == 0)
255 used_vnode_size = vattr.va_size;
256 }
257
258
259 /*
260 * make sure that the newsize fits within a vaddr_t
261 * XXX: need to revise addressing data types
262 */
263 if (used_vnode_size > (vaddr_t) -PAGE_SIZE) {
264 #ifdef DEBUG
265 printf("uvn_attach: vn %p size truncated %qx->%x\n", vp,
266 (long long)used_vnode_size, -PAGE_SIZE);
267 #endif
268 used_vnode_size = (vaddr_t) -PAGE_SIZE;
269 }
270
271 /* relock object */
272 simple_lock(&uvn->u_obj.vmobjlock);
273
274 if (uvn->u_flags & VXWANT)
275 wakeup(uvn);
276 uvn->u_flags &= ~(VXLOCK|VXWANT);
277
278 if (result != 0) {
279 simple_unlock(&uvn->u_obj.vmobjlock); /* drop lock */
280 UVMHIST_LOG(maphist,"<- done (VOP_GETATTR FAILED!)", 0,0,0,0);
281 return(NULL);
282 }
283 uvn->u_size = used_vnode_size;
284
285 }
286
287 /* unlock and return */
288 simple_unlock(&uvn->u_obj.vmobjlock);
289 UVMHIST_LOG(maphist,"<- done, refcnt=%d", uvn->u_obj.uo_refs,
290 0, 0, 0);
291 return (&uvn->u_obj);
292 }
293
294
295 /*
296 * uvn_reference
297 *
298 * duplicate a reference to a VM object. Note that the reference
299 * count must already be at least one (the passed in reference) so
300 * there is no chance of the uvn being killed or locked out here.
301 *
302 * => caller must call with object unlocked.
303 * => caller must be using the same accessprot as was used at attach time
304 */
305
306
307 static void
308 uvn_reference(uobj)
309 struct uvm_object *uobj;
310 {
311 UVMHIST_FUNC("uvn_reference"); UVMHIST_CALLED(maphist);
312
313 VREF((struct vnode *)uobj);
314 }
315
316 /*
317 * uvn_detach
318 *
319 * remove a reference to a VM object.
320 *
321 * => caller must call with object unlocked and map locked.
322 * => this starts the detach process, but doesn't have to finish it
323 * (async i/o could still be pending).
324 */
325 static void
326 uvn_detach(uobj)
327 struct uvm_object *uobj;
328 {
329 UVMHIST_FUNC("uvn_detach"); UVMHIST_CALLED(maphist);
330 vrele((struct vnode *)uobj);
331 }
332
333 /*
334 * uvm_vnp_terminate: external hook to clear out a vnode's VM
335 *
336 * called in two cases:
337 * [1] when a persisting vnode vm object (i.e. one with a zero reference
338 * count) needs to be freed so that a vnode can be reused. this
339 * happens under "getnewvnode" in vfs_subr.c. if the vnode from
340 * the free list is still attached (i.e. not VBAD) then vgone is
341 * called. as part of the vgone trace this should get called to
342 * free the vm object. this is the common case.
343 * [2] when a filesystem is being unmounted by force (MNT_FORCE,
344 * "umount -f") the vgone() function is called on active vnodes
345 * on the mounted file systems to kill their data (the vnodes become
346 * "dead" ones [see src/sys/miscfs/deadfs/...]). that results in a
347 * call here (even if the uvn is still in use -- i.e. has a non-zero
348 * reference count). this case happens at "umount -f" and during a
349 * "reboot/halt" operation.
350 *
351 * => the caller must XLOCK and VOP_LOCK the vnode before calling us
352 * [protects us from getting a vnode that is already in the DYING
353 * state...]
354 * => unlike uvn_detach, this function must not return until all the
355 * uvn's pages are disposed of.
356 * => in case [2] the uvn is still alive after this call, but all I/O
357 * ops will fail (due to the backing vnode now being "dead"). this
358 * will prob. kill any process using the uvn due to pgo_get failing.
359 */
360
361 void
362 uvm_vnp_terminate(vp)
363 struct vnode *vp;
364 {
365 struct uvm_vnode *uvn = &vp->v_uvm;
366 if (uvn->u_flags & VDIRTY) {
367 simple_lock(&uvn_wl_lock);
368 LIST_REMOVE(uvn, u_wlist);
369 uvn->u_flags &= ~(VDIRTY);
370 simple_unlock(&uvn_wl_lock);
371 }
372 }
373
374 /*
375 * uvn_releasepg: handled a released page in a uvn
376 *
377 * => "pg" is a PG_BUSY [caller owns it], PG_RELEASED page that we need
378 * to dispose of.
379 * => caller must handled PG_WANTED case
380 * => called with page's object locked, pageq's unlocked
381 * => returns TRUE if page's object is still alive, FALSE if we
382 * killed the page's object. if we return TRUE, then we
383 * return with the object locked.
384 * => if (nextpgp != NULL) => we return pageq.tqe_next here, and return
385 * with the page queues locked [for pagedaemon]
386 * => if (nextpgp == NULL) => we return with page queues unlocked [normal case]
387 * => we kill the uvn if it is not referenced and we are suppose to
388 * kill it ("relkill").
389 */
390
391 boolean_t
392 uvn_releasepg(pg, nextpgp)
393 struct vm_page *pg;
394 struct vm_page **nextpgp; /* OUT */
395 {
396 #ifdef DIAGNOSTIC
397 if ((pg->flags & PG_RELEASED) == 0)
398 panic("uvn_releasepg: page not released!");
399 #endif
400
401 /*
402 * dispose of the page [caller handles PG_WANTED]
403 */
404 pmap_page_protect(PMAP_PGARG(pg), VM_PROT_NONE);
405 uvm_lock_pageq();
406 if (nextpgp)
407 *nextpgp = pg->pageq.tqe_next; /* next page for daemon */
408 uvm_pagefree(pg);
409 if (!nextpgp)
410 uvm_unlock_pageq();
411
412 return (TRUE);
413 }
414
415 /*
416 * NOTE: currently we have to use VOP_READ/VOP_WRITE because they go
417 * through the buffer cache and allow I/O in any size. These VOPs use
418 * synchronous i/o. [vs. VOP_STRATEGY which can be async, but doesn't
419 * go through the buffer cache or allow I/O sizes larger than a
420 * block]. we will eventually want to change this.
421 *
422 * issues to consider:
423 * uvm provides the uvm_aiodesc structure for async i/o management.
424 * there are two tailq's in the uvm. structure... one for pending async
425 * i/o and one for "done" async i/o. to do an async i/o one puts
426 * an aiodesc on the "pending" list (protected by splbio()), starts the
427 * i/o and returns VM_PAGER_PEND. when the i/o is done, we expect
428 * some sort of "i/o done" function to be called (at splbio(), interrupt
429 * time). this function should remove the aiodesc from the pending list
430 * and place it on the "done" list and wakeup the daemon. the daemon
431 * will run at normal spl() and will remove all items from the "done"
432 * list and call the "aiodone" hook for each done request (see uvm_pager.c).
433 * [in the old vm code, this was done by calling the "put" routine with
434 * null arguments which made the code harder to read and understand because
435 * you had one function ("put") doing two things.]
436 *
437 * so the current pager needs:
438 * int uvn_aiodone(struct uvm_aiodesc *)
439 *
440 * => return KERN_SUCCESS (aio finished, free it). otherwise requeue for
441 * later collection.
442 * => called with pageq's locked by the daemon.
443 *
444 * general outline:
445 * - "try" to lock object. if fail, just return (will try again later)
446 * - drop "u_nio" (this req is done!)
447 * - if (object->iosync && u_naio == 0) { wakeup &uvn->u_naio }
448 * - get "page" structures (atop?).
449 * - handle "wanted" pages
450 * - handle "released" pages [using pgo_releasepg]
451 * >>> pgo_releasepg may kill the object
452 * dont forget to look at "object" wanted flag in all cases.
453 */
454
455
456 /*
457 * uvn_flush: flush pages out of a uvm object.
458 *
459 * => object should be locked by caller. we may _unlock_ the object
460 * if (and only if) we need to clean a page (PGO_CLEANIT).
461 * we return with the object locked.
462 * => if PGO_CLEANIT is set, we may block (due to I/O). thus, a caller
463 * might want to unlock higher level resources (e.g. vm_map)
464 * before calling flush.
465 * => if PGO_CLEANIT is not set, then we will neither unlock the object
466 * or block.
467 * => if PGO_ALLPAGE is set, then all pages in the object are valid targets
468 * for flushing.
469 * => NOTE: we rely on the fact that the object's memq is a TAILQ and
470 * that new pages are inserted on the tail end of the list. thus,
471 * we can make a complete pass through the object in one go by starting
472 * at the head and working towards the tail (new pages are put in
473 * front of us).
474 * => NOTE: we are allowed to lock the page queues, so the caller
475 * must not be holding the lock on them [e.g. pagedaemon had
476 * better not call us with the queues locked]
477 * => we return TRUE unless we encountered some sort of I/O error
478 *
479 * comment on "cleaning" object and PG_BUSY pages:
480 * this routine is holding the lock on the object. the only time
481 * that it can run into a PG_BUSY page that it does not own is if
482 * some other process has started I/O on the page (e.g. either
483 * a pagein, or a pageout). if the PG_BUSY page is being paged
484 * in, then it can not be dirty (!PG_CLEAN) because no one has
485 * had a chance to modify it yet. if the PG_BUSY page is being
486 * paged out then it means that someone else has already started
487 * cleaning the page for us (how nice!). in this case, if we
488 * have syncio specified, then after we make our pass through the
489 * object we need to wait for the other PG_BUSY pages to clear
490 * off (i.e. we need to do an iosync). also note that once a
491 * page is PG_BUSY it must stay in its object until it is un-busyed.
492 *
493 * note on page traversal:
494 * we can traverse the pages in an object either by going down the
495 * linked list in "uobj->memq", or we can go over the address range
496 * by page doing hash table lookups for each address. depending
497 * on how many pages are in the object it may be cheaper to do one
498 * or the other. we set "by_list" to true if we are using memq.
499 * if the cost of a hash lookup was equal to the cost of the list
500 * traversal we could compare the number of pages in the start->stop
501 * range to the total number of pages in the object. however, it
502 * seems that a hash table lookup is more expensive than the linked
503 * list traversal, so we multiply the number of pages in the
504 * start->stop range by a penalty which we define below.
505 */
506
507 #define UVN_HASH_PENALTY 4 /* XXX: a guess */
508
509 static boolean_t
510 uvn_flush(uobj, start, stop, flags)
511 struct uvm_object *uobj;
512 vaddr_t start, stop;
513 int flags;
514 {
515 struct uvm_vnode *uvn = (struct uvm_vnode *) uobj;
516 struct vnode *vp = (struct vnode *)uobj;
517 struct vm_page *pp, *ppnext, *ptmp;
518 struct vm_page *pps[MAXBSIZE >> PAGE_SHIFT], **ppsp;
519 int s;
520 int npages, result, lcv;
521 boolean_t retval, need_iosync, by_list, needs_clean;
522 vaddr_t curoff;
523 u_short pp_version;
524 UVMHIST_FUNC("uvn_flush"); UVMHIST_CALLED(maphist);
525
526 if (uvn->u_size == VSIZENOTSET) {
527 #ifdef DEBUG
528 void vp_name(void *);
529
530 printf("uvn_flush: size not set vp %p\n", uvn);
531 if ((flags & PGO_ALLPAGES) == 0)
532 printf("... and PGO_ALLPAGES not set: "
533 "start 0x%lx end 0x%lx flags 0x%x\n",
534 start, stop, flags);
535 vprint("uvn_flush VSIZENOTSET", vp);
536 vp_name(uvn);
537 #endif
538 flags |= PGO_ALLPAGES;
539 }
540
541 curoff = 0; /* XXX: shut up gcc */
542 /*
543 * get init vals and determine how we are going to traverse object
544 */
545
546 need_iosync = FALSE;
547 retval = TRUE; /* return value */
548 if (flags & PGO_ALLPAGES) {
549 start = 0;
550 stop = -1;
551 by_list = TRUE; /* always go by the list */
552 } else {
553 start = trunc_page(start);
554 stop = round_page(stop);
555 if (stop > round_page(uvn->u_size)) {
556 printf("uvn_flush: oor vp %p start 0x%x stop 0x%x "
557 "size 0x%x\n", uvn, (int)start, (int)stop,
558 (int)round_page(uvn->u_size));
559 }
560
561 by_list = (uobj->uo_npages <=
562 ((stop - start) >> PAGE_SHIFT) * UVN_HASH_PENALTY);
563 }
564
565 UVMHIST_LOG(maphist,
566 " flush start=0x%x, stop=0x%x, by_list=%d, flags=0x%x",
567 start, stop, by_list, flags);
568
569 /*
570 * PG_CLEANCHK: this bit is used by the pgo_mk_pcluster function as
571 * a _hint_ as to how up to date the PG_CLEAN bit is. if the hint
572 * is wrong it will only prevent us from clustering... it won't break
573 * anything. we clear all PG_CLEANCHK bits here, and pgo_mk_pcluster
574 * will set them as it syncs PG_CLEAN. This is only an issue if we
575 * are looking at non-inactive pages (because inactive page's PG_CLEAN
576 * bit is always up to date since there are no mappings).
577 * [borrowed PG_CLEANCHK idea from FreeBSD VM]
578 */
579
580 if ((flags & PGO_CLEANIT) != 0 &&
581 uobj->pgops->pgo_mk_pcluster != NULL) {
582 if (by_list) {
583 for (pp = TAILQ_FIRST(&uobj->memq);
584 pp != NULL ;
585 pp = TAILQ_NEXT(pp, listq)) {
586 if (pp->offset < start ||
587 (pp->offset >= stop && stop != -1))
588 continue;
589 pp->flags &= ~PG_CLEANCHK;
590 }
591
592 } else { /* by hash */
593 for (curoff = start ; curoff < stop;
594 curoff += PAGE_SIZE) {
595 pp = uvm_pagelookup(uobj, curoff);
596 if (pp)
597 pp->flags &= ~PG_CLEANCHK;
598 }
599 }
600 }
601
602 /*
603 * now do it. note: we must update ppnext in body of loop or we
604 * will get stuck. we need to use ppnext because we may free "pp"
605 * before doing the next loop.
606 */
607
608 if (by_list) {
609 pp = TAILQ_FIRST(&uobj->memq);
610 } else {
611 curoff = start;
612 pp = uvm_pagelookup(uobj, curoff);
613 }
614
615 ppnext = NULL; /* XXX: shut up gcc */
616 ppsp = NULL; /* XXX: shut up gcc */
617 uvm_lock_pageq(); /* page queues locked */
618
619 /* locked: both page queues and uobj */
620 for ( ; (by_list && pp != NULL) ||
621 (!by_list && curoff < stop) ; pp = ppnext) {
622
623 if (by_list) {
624
625 /*
626 * range check
627 */
628
629 if (pp->offset < start || pp->offset >= stop) {
630 ppnext = TAILQ_NEXT(pp, listq);
631 continue;
632 }
633
634 } else {
635
636 /*
637 * null check
638 */
639
640 curoff += PAGE_SIZE;
641 if (pp == NULL) {
642 if (curoff < stop)
643 ppnext = uvm_pagelookup(uobj, curoff);
644 continue;
645 }
646
647 }
648
649 /*
650 * handle case where we do not need to clean page (either
651 * because we are not clean or because page is not dirty or
652 * is busy):
653 *
654 * NOTE: we are allowed to deactivate a non-wired active
655 * PG_BUSY page, but once a PG_BUSY page is on the inactive
656 * queue it must stay put until it is !PG_BUSY (so as not to
657 * confuse pagedaemon).
658 */
659
660 if ((flags & PGO_CLEANIT) == 0 || (pp->flags & PG_BUSY) != 0) {
661 needs_clean = FALSE;
662 if ((pp->flags & PG_BUSY) != 0 &&
663 (flags & (PGO_CLEANIT|PGO_SYNCIO)) ==
664 (PGO_CLEANIT|PGO_SYNCIO))
665 need_iosync = TRUE;
666 } else {
667 /*
668 * freeing: nuke all mappings so we can sync
669 * PG_CLEAN bit with no race
670 */
671 if ((pp->flags & PG_CLEAN) != 0 &&
672 (flags & PGO_FREE) != 0 &&
673 (pp->pqflags & PQ_ACTIVE) != 0)
674 pmap_page_protect(PMAP_PGARG(pp), VM_PROT_NONE);
675 if ((pp->flags & PG_CLEAN) != 0 &&
676 pmap_is_modified(PMAP_PGARG(pp)))
677 pp->flags &= ~(PG_CLEAN);
678 pp->flags |= PG_CLEANCHK; /* update "hint" */
679
680 needs_clean = ((pp->flags & PG_CLEAN) == 0);
681 }
682
683 /*
684 * if we don't need a clean... load ppnext and dispose of pp
685 */
686 if (!needs_clean) {
687 /* load ppnext */
688 if (by_list)
689 ppnext = pp->listq.tqe_next;
690 else {
691 if (curoff < stop)
692 ppnext = uvm_pagelookup(uobj, curoff);
693 }
694
695 /* now dispose of pp */
696 if (flags & PGO_DEACTIVATE) {
697 if ((pp->pqflags & PQ_INACTIVE) == 0 &&
698 pp->wire_count == 0) {
699 pmap_page_protect(PMAP_PGARG(pp),
700 VM_PROT_NONE);
701 uvm_pagedeactivate(pp);
702 }
703
704 } else if (flags & PGO_FREE) {
705 if (pp->flags & PG_BUSY) {
706 /* release busy pages */
707 pp->flags |= PG_RELEASED;
708 } else {
709 pmap_page_protect(PMAP_PGARG(pp),
710 VM_PROT_NONE);
711 /* removed page from object */
712 uvm_pagefree(pp);
713 }
714 }
715 /* ppnext is valid so we can continue... */
716 continue;
717 }
718
719 /*
720 * pp points to a page in the locked object that we are
721 * working on. if it is !PG_CLEAN,!PG_BUSY and we asked
722 * for cleaning (PGO_CLEANIT). we clean it now.
723 *
724 * let uvm_pager_put attempted a clustered page out.
725 * note: locked: uobj and page queues.
726 */
727
728 pp->flags |= PG_BUSY; /* we 'own' page now */
729 UVM_PAGE_OWN(pp, "uvn_flush");
730 pmap_page_protect(PMAP_PGARG(pp), VM_PROT_READ);
731 pp_version = pp->version;
732 ReTry:
733 ppsp = pps;
734 npages = sizeof(pps) / sizeof(struct vm_page *);
735
736 /* locked: page queues, uobj */
737 result = uvm_pager_put(uobj, pp, &ppsp, &npages,
738 flags | PGO_DOACTCLUST, start, stop);
739 /* unlocked: page queues, uobj */
740
741 /*
742 * at this point nothing is locked. if we did an async I/O
743 * it is remotely possible for the async i/o to complete and
744 * the page "pp" be freed or what not before we get a chance
745 * to relock the object. in order to detect this, we have
746 * saved the version number of the page in "pp_version".
747 */
748
749 /* relock! */
750 simple_lock(&uobj->vmobjlock);
751 uvm_lock_pageq();
752
753 /*
754 * VM_PAGER_AGAIN: given the structure of this pager, this
755 * can only happen when we are doing async I/O and can't
756 * map the pages into kernel memory (pager_map) due to lack
757 * of vm space. if this happens we drop back to sync I/O.
758 */
759
760 if (result == VM_PAGER_AGAIN) {
761 /*
762 * it is unlikely, but page could have been released
763 * while we had the object lock dropped. we ignore
764 * this now and retry the I/O. we will detect and
765 * handle the released page after the syncio I/O
766 * completes.
767 */
768 #ifdef DIAGNOSTIC
769 if (flags & PGO_SYNCIO)
770 panic("uvn_flush: PGO_SYNCIO return 'try again' error (impossible)");
771 #endif
772 flags |= PGO_SYNCIO;
773 goto ReTry;
774 }
775
776 /*
777 * the cleaning operation is now done. finish up. note that
778 * on error (!OK, !PEND) uvm_pager_put drops the cluster for us.
779 * if success (OK, PEND) then uvm_pager_put returns the cluster
780 * to us in ppsp/npages.
781 */
782
783 /*
784 * for pending async i/o if we are not deactivating/freeing
785 * we can move on to the next page.
786 */
787
788 if (result == VM_PAGER_PEND) {
789
790 if ((flags & (PGO_DEACTIVATE|PGO_FREE)) == 0) {
791 /*
792 * no per-page ops: refresh ppnext and continue
793 */
794 if (by_list) {
795 if (pp->version == pp_version)
796 ppnext = pp->listq.tqe_next;
797 else
798 /* reset */
799 ppnext = uobj->memq.tqh_first;
800 } else {
801 if (curoff < stop)
802 ppnext = uvm_pagelookup(uobj,
803 curoff);
804 }
805 continue;
806 }
807
808 /* need to do anything here? */
809 }
810
811 /*
812 * need to look at each page of the I/O operation. we defer
813 * processing "pp" until the last trip through this "for" loop
814 * so that we can load "ppnext" for the main loop after we
815 * play with the cluster pages [thus the "npages + 1" in the
816 * loop below].
817 */
818
819 for (lcv = 0 ; lcv < npages + 1 ; lcv++) {
820
821 /*
822 * handle ppnext for outside loop, and saving pp
823 * until the end.
824 */
825 if (lcv < npages) {
826 if (ppsp[lcv] == pp)
827 continue; /* skip pp until the end */
828 ptmp = ppsp[lcv];
829 } else {
830 ptmp = pp;
831
832 /* set up next page for outer loop */
833 if (by_list) {
834 if (pp->version == pp_version)
835 ppnext = pp->listq.tqe_next;
836 else
837 /* reset */
838 ppnext = uobj->memq.tqh_first;
839 } else {
840 if (curoff < stop)
841 ppnext = uvm_pagelookup(uobj, curoff);
842 }
843 }
844
845 /*
846 * verify the page didn't get moved while obj was
847 * unlocked
848 */
849 if (result == VM_PAGER_PEND && ptmp->uobject != uobj)
850 continue;
851
852 /*
853 * unbusy the page if I/O is done. note that for
854 * pending I/O it is possible that the I/O op
855 * finished before we relocked the object (in
856 * which case the page is no longer busy).
857 */
858
859 if (result != VM_PAGER_PEND) {
860 if (ptmp->flags & PG_WANTED)
861 /* still holding object lock */
862 wakeup(ptmp);
863
864 ptmp->flags &= ~(PG_WANTED|PG_BUSY);
865 UVM_PAGE_OWN(ptmp, NULL);
866 if (ptmp->flags & PG_RELEASED) {
867
868 /* pgo_releasepg wants this */
869 uvm_unlock_pageq();
870 if (!uvn_releasepg(ptmp, NULL))
871 return (TRUE);
872
873 uvm_lock_pageq(); /* relock */
874 continue; /* next page */
875
876 } else {
877 ptmp->flags |= (PG_CLEAN|PG_CLEANCHK);
878 if ((flags & PGO_FREE) == 0)
879 pmap_clear_modify(
880 PMAP_PGARG(ptmp));
881 }
882 }
883
884 /*
885 * dispose of page
886 */
887
888 if (flags & PGO_DEACTIVATE) {
889 if ((pp->pqflags & PQ_INACTIVE) == 0 &&
890 pp->wire_count == 0) {
891 pmap_page_protect(PMAP_PGARG(ptmp),
892 VM_PROT_NONE);
893 uvm_pagedeactivate(ptmp);
894 }
895
896 } else if (flags & PGO_FREE) {
897 if (result == VM_PAGER_PEND) {
898 if ((ptmp->flags & PG_BUSY) != 0)
899 /* signal for i/o done */
900 ptmp->flags |= PG_RELEASED;
901 } else {
902 if (result != VM_PAGER_OK) {
903 printf("uvn_flush: obj=%p, "
904 "offset=0x%lx. error %d\n",
905 pp->uobject, pp->offset,
906 result);
907 printf("uvn_flush: WARNING: "
908 "changes to page may be "
909 "lost!\n");
910 retval = FALSE;
911 }
912 pmap_page_protect(PMAP_PGARG(ptmp),
913 VM_PROT_NONE);
914 uvm_pagefree(ptmp);
915 }
916 }
917
918 } /* end of "lcv" for loop */
919
920 } /* end of "pp" for loop */
921
922 /*
923 * done with pagequeues: unlock
924 */
925 uvm_unlock_pageq();
926
927 /*
928 * now wait for all I/O if required.
929 */
930 if (need_iosync) {
931 UVMHIST_LOG(maphist," <<DOING IOSYNC>>",0,0,0,0);
932
933 /*
934 * XXX this doesn't use the new two-flag scheme,
935 * but to use that, all i/o initiators will have to change.
936 */
937
938 s = splbio();
939 while (vp->v_numoutput != 0) {
940 UVMHIST_LOG(ubchist, "waiting for vp %p num %d",
941 vp, vp->v_numoutput,0,0);
942
943 vp->v_flag |= VBWAIT;
944 UVM_UNLOCK_AND_WAIT(&vp->v_numoutput,
945 &uvn->u_obj.vmobjlock,
946 FALSE, "uvn_flush",0);
947 simple_lock(&uvn->u_obj.vmobjlock);
948 }
949 splx(s);
950 }
951
952 /* return, with object locked! */
953 UVMHIST_LOG(maphist,"<- done (retval=0x%x)",retval,0,0,0);
954 return(retval);
955 }
956
957 /*
958 * uvn_cluster
959 *
960 * we are about to do I/O in an object at offset. this function is called
961 * to establish a range of offsets around "offset" in which we can cluster
962 * I/O.
963 *
964 * - currently doesn't matter if obj locked or not.
965 */
966
967 static void
968 uvn_cluster(uobj, offset, loffset, hoffset)
969 struct uvm_object *uobj;
970 vaddr_t offset;
971 vaddr_t *loffset, *hoffset; /* OUT */
972 {
973 struct uvm_vnode *uvn = (struct uvm_vnode *)uobj;
974
975 *loffset = offset;
976 *hoffset = min(offset + MAXBSIZE, round_page(uvn->u_size));
977 }
978
979 /*
980 * uvn_put: flush page data to backing store.
981 *
982 * => object must be locked! we will _unlock_ it before starting I/O.
983 * => flags: PGO_SYNCIO -- use sync. I/O
984 * => note: caller must set PG_CLEAN and pmap_clear_modify (if needed)
985 */
986
987 static int
988 uvn_put(uobj, pps, npages, flags)
989 struct uvm_object *uobj;
990 struct vm_page **pps;
991 int npages, flags;
992 {
993 struct vnode *vp = (struct vnode *)uobj;
994 int error, sync;
995
996 sync = (flags & PGO_SYNCIO) ? 1 : 0;
997
998 simple_unlock(&uobj->vmobjlock);
999 error = VOP_PUTPAGES(vp, pps, npages, sync, NULL);
1000
1001 return uvm_errno2vmerror(error);
1002 }
1003
1004
1005 /*
1006 * uvn_get: get pages (synchronously) from backing store
1007 *
1008 * => prefer map unlocked (not required)
1009 * => object must be locked! we will _unlock_ it before starting any I/O.
1010 * => flags: PGO_ALLPAGES: get all of the pages
1011 * PGO_LOCKED: fault data structures are locked
1012 * => NOTE: offset is the offset of pps[0], _NOT_ pps[centeridx]
1013 * => NOTE: caller must check for released pages!!
1014 */
1015
1016 static int
1017 uvn_get(uobj, offset, pps, npagesp, centeridx, access_type, advice, flags)
1018 struct uvm_object *uobj;
1019 vaddr_t offset;
1020 struct vm_page **pps; /* IN/OUT */
1021 int *npagesp; /* IN (OUT if PGO_LOCKED) */
1022 int centeridx;
1023 vm_prot_t access_type;
1024 int advice, flags;
1025 {
1026 struct vnode *vp = (struct vnode *)uobj;
1027 int error;
1028 UVMHIST_FUNC("uvn_get"); UVMHIST_CALLED(ubchist);
1029 UVMHIST_LOG(ubchist, "vp %p off 0x%x", vp, (int)offset, 0,0);
1030
1031 error = VOP_GETPAGES(vp, offset, pps, npagesp, centeridx,
1032 access_type, advice, flags);
1033 return uvm_errno2vmerror(error);
1034 }
1035
1036 /*
1037 * uvn_findpages:
1038 * return the page for the uobj and offset requested, allocating if needed.
1039 * => uobj must be locked.
1040 * => returned page will be BUSY.
1041 */
1042
1043 void
1044 uvn_findpages(uobj, offset, npagesp, pps, flags)
1045 struct uvm_object *uobj;
1046 vaddr_t offset;
1047 int *npagesp;
1048 struct vm_page **pps;
1049 int flags;
1050 {
1051 int i, rv, npages;
1052
1053 rv = 0;
1054 npages = *npagesp;
1055 for (i = 0; i < npages; i++, offset += PAGE_SIZE) {
1056 rv += uvn_findpage(uobj, offset, &pps[i], flags);
1057 }
1058 *npagesp = rv;
1059 }
1060
1061
1062 static int
1063 uvn_findpage(uobj, offset, pps, flags)
1064 struct uvm_object *uobj;
1065 vaddr_t offset;
1066 struct vm_page **pps;
1067 int flags;
1068 {
1069 struct vm_page *ptmp;
1070 UVMHIST_FUNC("uvn_findpage"); UVMHIST_CALLED(ubchist);
1071 UVMHIST_LOG(ubchist, "vp %p off 0x%lx", uobj, offset,0,0);
1072
1073 if (*pps != NULL) {
1074 UVMHIST_LOG(ubchist, "dontcare", 0,0,0,0);
1075 return 0;
1076 }
1077 for (;;) {
1078 /* look for an existing page */
1079 ptmp = uvm_pagelookup(uobj, offset);
1080
1081 /* nope? allocate one now */
1082 if (ptmp == NULL) {
1083 if (flags & UFP_NOALLOC) {
1084 UVMHIST_LOG(ubchist, "noalloc", 0,0,0,0);
1085 return 0;
1086 }
1087 ptmp = uvm_pagealloc(uobj, offset, NULL, 0);
1088 if (ptmp == NULL) {
1089 if (flags & UFP_NOWAIT) {
1090 UVMHIST_LOG(ubchist, "nowait",0,0,0,0);
1091 return 0;
1092 }
1093 simple_unlock(&uobj->vmobjlock);
1094 uvm_wait("uvn_fp1");
1095 simple_lock(&uobj->vmobjlock);
1096 continue;
1097 }
1098 UVMHIST_LOG(ubchist, "alloced",0,0,0,0);
1099 break;
1100 } else if (flags & UFP_NOCACHE) {
1101 UVMHIST_LOG(ubchist, "nocache",0,0,0,0);
1102 return 0;
1103 }
1104
1105 /* page is there, see if we need to wait on it */
1106 if ((ptmp->flags & (PG_BUSY|PG_RELEASED)) != 0) {
1107 if (flags & UFP_NOWAIT) {
1108 UVMHIST_LOG(ubchist, "nowait",0,0,0,0);
1109 return 0;
1110 }
1111 ptmp->flags |= PG_WANTED;
1112 UVM_UNLOCK_AND_WAIT(ptmp, &uobj->vmobjlock, 0,
1113 "uvn_fp2",0);
1114 simple_lock(&uobj->vmobjlock);
1115 continue;
1116 }
1117
1118 /* skip PG_RDONLY pages if requested */
1119 if ((flags & UFP_NORDONLY) && (ptmp->flags & PG_RDONLY)) {
1120 UVMHIST_LOG(ubchist, "nordonly",0,0,0,0);
1121 return 0;
1122 }
1123 /* BUSY the page and we're done. */
1124 ptmp->flags |= PG_BUSY;
1125 UVM_PAGE_OWN(ptmp, "uvn_findpage");
1126 UVMHIST_LOG(ubchist, "found",0,0,0,0);
1127 break;
1128 }
1129 *pps = ptmp;
1130 return 1;
1131 }
1132
1133 /*
1134 * uvn_asyncget: start async I/O to bring pages into ram
1135 *
1136 * => caller must lock object(???XXX: see if this is best)
1137 * => could be called from uvn_get or a madvise() fault-ahead.
1138 * => if it fails, it doesn't matter.
1139 */
1140
1141 static int
1142 uvn_asyncget(uobj, offset, npages)
1143 struct uvm_object *uobj;
1144 vaddr_t offset;
1145 int npages;
1146 {
1147
1148 /*
1149 * XXXCDC: we can't do async I/O yet
1150 */
1151 printf("uvn_asyncget called\n");
1152 return (KERN_SUCCESS);
1153 }
1154
1155 /*
1156 * uvm_vnp_setsize: grow or shrink a vnode uvn
1157 *
1158 * grow => just update size value
1159 * shrink => toss un-needed pages
1160 *
1161 * => we assume that the caller has a reference of some sort to the
1162 * vnode in question so that it will not be yanked out from under
1163 * us.
1164 *
1165 * called from:
1166 * => truncate fns (ext2fs_truncate, ffs_truncate, detrunc[msdos])
1167 * => "write" fns (ext2fs_write, WRITE [ufs/ufs], msdosfs_write, nfs_write)
1168 * => ffs_balloc [XXX: why? doesn't WRITE handle?]
1169 * => NFS: nfs_loadattrcache, nfs_getattrcache, nfs_setattr
1170 * => union fs: union_newsize
1171 */
1172
1173 void
1174 uvm_vnp_setsize(vp, newsize)
1175 struct vnode *vp;
1176 u_quad_t newsize;
1177 {
1178 struct uvm_vnode *uvn = &vp->v_uvm;
1179
1180 /*
1181 * lock uvn and check for valid object, and if valid: do it!
1182 */
1183 simple_lock(&uvn->u_obj.vmobjlock);
1184
1185 /*
1186 * make sure that the newsize fits within a vaddr_t
1187 * XXX: need to revise addressing data types
1188 */
1189
1190 if (newsize > (vaddr_t) -PAGE_SIZE) {
1191 #ifdef DEBUG
1192 printf("uvm_vnp_setsize: vn %p size truncated "
1193 "%qx->%lx\n", vp, (long long)newsize,
1194 (vaddr_t)-PAGE_SIZE);
1195 #endif
1196 newsize = (vaddr_t)-PAGE_SIZE;
1197 }
1198
1199 /*
1200 * now check if the size has changed: if we shrink we had better
1201 * toss some pages...
1202 */
1203
1204 if (uvn->u_size > newsize && uvn->u_size != VSIZENOTSET) {
1205 (void) uvn_flush(&uvn->u_obj, (vaddr_t)newsize,
1206 uvn->u_size, PGO_FREE);
1207 }
1208 uvn->u_size = (vaddr_t)newsize;
1209 simple_unlock(&uvn->u_obj.vmobjlock);
1210 }
1211
1212 /*
1213 * uvm_vnp_sync: flush all dirty VM pages back to their backing vnodes.
1214 *
1215 * => called from sys_sync with no VM structures locked
1216 * => only one process can do a sync at a time (because the uvn
1217 * structure only has one queue for sync'ing). we ensure this
1218 * by holding the uvn_sync_lock while the sync is in progress.
1219 * other processes attempting a sync will sleep on this lock
1220 * until we are done.
1221 */
1222
1223 void
1224 uvm_vnp_sync(mp)
1225 struct mount *mp;
1226 {
1227 struct uvm_vnode *uvn;
1228 struct vnode *vp;
1229 boolean_t got_lock;
1230
1231 /*
1232 * step 1: ensure we are only ones using the uvn_sync_q by locking
1233 * our lock...
1234 */
1235 lockmgr(&uvn_sync_lock, LK_EXCLUSIVE, (void *)0);
1236
1237 /*
1238 * step 2: build up a simpleq of uvns of interest based on the
1239 * write list. we gain a reference to uvns of interest. must
1240 * be careful about locking uvn's since we will be holding uvn_wl_lock
1241 * in the body of the loop.
1242 */
1243 SIMPLEQ_INIT(&uvn_sync_q);
1244 simple_lock(&uvn_wl_lock);
1245 for (uvn = LIST_FIRST(&uvn_wlist); uvn != NULL;
1246 uvn = LIST_NEXT(uvn, u_wlist)) {
1247
1248 vp = (struct vnode *) uvn;
1249 if (mp && vp->v_mount != mp)
1250 continue;
1251
1252 /* attempt to gain reference */
1253 while ((got_lock = simple_lock_try(&uvn->u_obj.vmobjlock)) ==
1254 FALSE &&
1255 (uvn->u_flags & VXLOCK) == 0)
1256 /* spin */ ;
1257
1258 /*
1259 * we will exit the loop if either if the following are true:
1260 * - we got the lock [always true if NCPU == 1]
1261 * - we failed to get the lock but noticed the vnode was
1262 * "blocked" -- in this case the vnode must be a dying
1263 * vnode, and since dying vnodes are in the process of
1264 * being flushed out, we can safely skip this one
1265 *
1266 * we want to skip over the vnode if we did not get the lock,
1267 * or if the vnode is already dying (due to the above logic).
1268 *
1269 * note that uvn must already be valid because we found it on
1270 * the wlist (this also means it can't be ALOCK'd).
1271 */
1272 if (!got_lock || (uvn->u_flags & VXLOCK) != 0) {
1273 if (got_lock)
1274 simple_unlock(&uvn->u_obj.vmobjlock);
1275 continue; /* skip it */
1276 }
1277
1278 vget(vp, LK_INTERLOCK);
1279
1280 /*
1281 * got it!
1282 */
1283 SIMPLEQ_INSERT_HEAD(&uvn_sync_q, uvn, u_syncq);
1284 }
1285 simple_unlock(&uvn_wl_lock);
1286
1287 /*
1288 * step 3: we now have a list of uvn's that may need cleaning.
1289 * we are holding the uvn_sync_lock, but have dropped the uvn_wl_lock
1290 * (so we can now safely lock uvn's again).
1291 */
1292
1293 for (uvn = uvn_sync_q.sqh_first ; uvn ; uvn = uvn->u_syncq.sqe_next) {
1294 simple_lock(&uvn->u_obj.vmobjlock);
1295 uvn_flush(&uvn->u_obj, 0, 0,
1296 PGO_CLEANIT|PGO_ALLPAGES|PGO_DOACTCLUST);
1297
1298 /*
1299 * if we have the only reference and we just cleaned the uvn,
1300 * then we can pull it out of the VDIRTY state
1301 * thus allowing us to avoid thinking about flushing it again
1302 * on later sync ops.
1303 */
1304 if (uvn->u_obj.uo_refs == 1 && (uvn->u_flags & VDIRTY)) {
1305 simple_lock(&uvn_wl_lock);
1306 LIST_REMOVE(uvn, u_wlist);
1307 uvn->u_flags &= ~VDIRTY;
1308 simple_unlock(&uvn_wl_lock);
1309 }
1310
1311 simple_unlock(&uvn->u_obj.vmobjlock);
1312
1313 /* now drop our reference to the uvn */
1314 uvn_detach(&uvn->u_obj);
1315 }
1316
1317 /*
1318 * done! release sync lock
1319 */
1320 lockmgr(&uvn_sync_lock, LK_RELEASE, (void *)0);
1321 }
1322
1323
1324 /*
1325 * uvm_vnp_zerorange: set a range of bytes in a file to zero.
1326 */
1327
1328 void
1329 uvm_vnp_zerorange(vp, off, len)
1330 struct vnode *vp;
1331 off_t off;
1332 size_t len;
1333 {
1334 void *win;
1335
1336 /*
1337 * XXX invent kzero() and use it
1338 */
1339
1340 while (len) {
1341 vsize_t bytelen = len;
1342
1343 win = ubc_alloc(&vp->v_uvm.u_obj, off, &bytelen, UBC_WRITE);
1344 memset(win, 0, bytelen);
1345 ubc_release(win, 0);
1346
1347 off += bytelen;
1348 len -= bytelen;
1349 }
1350 }
1351
1352 /*
1353 * uvn_doasyncget: start one readahead i/o.
1354 */
1355
1356 static void
1357 uvn_doasyncget(pgs, bytes, blkno)
1358 struct vm_page **pgs;
1359 size_t bytes;
1360 daddr_t blkno;
1361 {
1362 struct buf *bp;
1363 struct vnode *vp = (struct vnode *)pgs[0]->uobject;
1364 int pages = roundup(bytes, PAGE_SIZE) >> PAGE_SHIFT;
1365 int s;
1366 UVMHIST_FUNC("uvn_doasyncget"); UVMHIST_CALLED(ubchist);
1367
1368 UVMHIST_LOG(ubchist, "vp %p offset 0x%x bytes 0x%x blkno 0x%x",
1369 vp, (int)pgs[0]->offset, (int)bytes, (int)blkno);
1370
1371 s = splbio();
1372 bp = pool_get(&bufpool, PR_WAITOK);
1373 splx(s);
1374 bp->b_data = (void *)uvm_pagermapin(pgs, pages, M_WAITOK);
1375 bp->b_flags = B_BUSY|B_READ|B_CALL|B_ASYNC;
1376 bp->b_iodone = uvm_aio_biodone;
1377 bp->b_lblkno = 0;
1378 bp->b_blkno = blkno;
1379 bp->b_bufsize = pages << PAGE_SHIFT;
1380 bp->b_bcount = bytes;
1381 bp->b_vp = vp;
1382 UVMHIST_LOG(ubchist, "bp %p", bp, 0,0,0);
1383
1384 VOP_STRATEGY(bp);
1385 }
1386
1387 #define MAXRAPAGES 16
1388
1389 /*
1390 * asynchronously create pages for a vnode and read their data.
1391 */
1392
1393 void
1394 uvm_vnp_asyncget(vp, off, len)
1395 struct vnode *vp;
1396 off_t off;
1397 size_t len;
1398 {
1399 off_t filesize = vp->v_uvm.u_size;
1400 struct vm_page *pgs[MAXRAPAGES];
1401 struct uvm_object *uobj = &vp->v_uvm.u_obj;
1402 daddr_t lbn, blkno;
1403 int bshift = vp->v_mount->mnt_fs_bshift;
1404 int dev_bshift = vp->v_mount->mnt_dev_bshift;
1405 int i, npages, npgs, startidx, run, bytes, startpage, endpage;
1406 int count;
1407 UVMHIST_FUNC("uvn_asyncget"); UVMHIST_CALLED(ubchist);
1408
1409 if (off != trunc_page(off)) {
1410 panic("off 0x%x not page-aligned", (int)off);
1411 }
1412
1413 UVMHIST_LOG(ubchist, "asyncget off 0x%x len 0x%x",
1414 (int)off, (int)len,0,0);
1415
1416 count = round_page(len) >> PAGE_SHIFT;
1417 while (count > 0) {
1418 if (off >= filesize) {
1419 return;
1420 }
1421
1422 lbn = off >> bshift;
1423 if (VOP_BMAP(vp, lbn, NULL, &blkno, &run) != 0) {
1424 return;
1425 }
1426
1427 UVMHIST_LOG(ubchist, "bmap lbn 0x%x bn 0x%x",
1428 (int)lbn, (int)blkno,0,0);
1429
1430 /* don't do readahead past file holes... */
1431 if (blkno == (daddr_t)-1) {
1432 return;
1433 }
1434
1435 startpage = off >> PAGE_SHIFT;
1436 endpage = min(roundup(off + 1 + (run << bshift), 1 << bshift),
1437 round_page(filesize)) >> PAGE_SHIFT;
1438 npages = min(endpage - startpage, min(count, MAXRAPAGES));
1439
1440 UVMHIST_LOG(ubchist, "off 0x%x run 0x%x "
1441 "startpage %d endpage %d",
1442 (int)off, run, startpage, endpage);
1443 UVMHIST_LOG(ubchist, "runend 0x%x fileend 0x%x sum 0x%x",
1444 (int)roundup(off + 1 + (run << bshift),
1445 (1 << bshift)),
1446 (int)round_page(filesize),
1447 (int)(off + 1 + (run << bshift)), 0);
1448
1449 if (npages == 0) {
1450 return;
1451 }
1452
1453 memset(pgs, 0, npages * sizeof(pgs[0]));
1454
1455 simple_lock(&uobj->vmobjlock);
1456 npgs = npages;
1457 uvn_findpages(uobj, off, &npgs, pgs, UFP_NOWAIT | UFP_NOCACHE);
1458 simple_unlock(&uobj->vmobjlock);
1459
1460 blkno += (off - (lbn << bshift)) >> dev_bshift;
1461
1462 /*
1463 * activate any pages we just allocated.
1464 */
1465
1466 for (i = 0; i < npages; i++) {
1467 if (pgs[i] == NULL) {
1468 continue;
1469 }
1470 uvm_pageactivate(pgs[i]);
1471 }
1472
1473 /*
1474 * start i/os on the pages.
1475 */
1476
1477 for (i = 0; i < npages; i++) {
1478 for (startidx = i; i < npages; i++) {
1479 if (pgs[i] == NULL) {
1480 break;
1481 }
1482 }
1483 if (i > startidx) {
1484 bytes = min((i - startidx) << PAGE_SHIFT,
1485 filesize - pgs[startidx]->offset);
1486 bytes = roundup(bytes, 1 << dev_bshift);
1487
1488 UVMHIST_LOG(ubchist, "bytes i %d startidx %d "
1489 "filesize 0x%x pgoff 0x%x",
1490 i, startidx, (int)filesize,
1491 (int)pgs[startidx]->offset);
1492
1493 uvn_doasyncget(&pgs[startidx], bytes,
1494 blkno + startidx *
1495 (PAGE_SIZE >> dev_bshift));
1496 }
1497 }
1498
1499 off += npages << PAGE_SHIFT;
1500 count -= npages;
1501
1502 /* XXX for now, don't loop */
1503 return;
1504 }
1505 }
1506