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