uvm_vnode.c revision 1.2 1 1.2 thorpej /* $NetBSD: uvm_vnode.c,v 1.2 1998/02/06 22:32:35 thorpej Exp $ */
2 1.1 mrg
3 1.1 mrg /*
4 1.1 mrg * XXXCDC: "ROUGH DRAFT" QUALITY UVM PRE-RELEASE FILE!
5 1.1 mrg * >>>USE AT YOUR OWN RISK, WORK IS NOT FINISHED<<<
6 1.1 mrg */
7 1.1 mrg /*
8 1.1 mrg * Copyright (c) 1997 Charles D. Cranor and Washington University.
9 1.1 mrg * Copyright (c) 1991, 1993
10 1.1 mrg * The Regents of the University of California.
11 1.1 mrg * Copyright (c) 1990 University of Utah.
12 1.1 mrg *
13 1.1 mrg * All rights reserved.
14 1.1 mrg *
15 1.1 mrg * This code is derived from software contributed to Berkeley by
16 1.1 mrg * the Systems Programming Group of the University of Utah Computer
17 1.1 mrg * Science Department.
18 1.1 mrg *
19 1.1 mrg * Redistribution and use in source and binary forms, with or without
20 1.1 mrg * modification, are permitted provided that the following conditions
21 1.1 mrg * are met:
22 1.1 mrg * 1. Redistributions of source code must retain the above copyright
23 1.1 mrg * notice, this list of conditions and the following disclaimer.
24 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
25 1.1 mrg * notice, this list of conditions and the following disclaimer in the
26 1.1 mrg * documentation and/or other materials provided with the distribution.
27 1.1 mrg * 3. All advertising materials mentioning features or use of this software
28 1.1 mrg * must display the following acknowledgement:
29 1.1 mrg * This product includes software developed by Charles D. Cranor,
30 1.1 mrg * Washington University, the University of California, Berkeley and
31 1.1 mrg * its contributors.
32 1.1 mrg * 4. Neither the name of the University nor the names of its contributors
33 1.1 mrg * may be used to endorse or promote products derived from this software
34 1.1 mrg * without specific prior written permission.
35 1.1 mrg *
36 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
37 1.1 mrg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 1.1 mrg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39 1.1 mrg * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
40 1.1 mrg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41 1.1 mrg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42 1.1 mrg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 1.1 mrg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44 1.1 mrg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45 1.1 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 1.1 mrg * SUCH DAMAGE.
47 1.1 mrg *
48 1.1 mrg * @(#)vnode_pager.c 8.8 (Berkeley) 2/13/94
49 1.1 mrg */
50 1.1 mrg
51 1.1 mrg /*
52 1.1 mrg * uvm_vnode.c: the vnode pager.
53 1.1 mrg */
54 1.1 mrg
55 1.1 mrg #include <sys/param.h>
56 1.1 mrg #include <sys/systm.h>
57 1.1 mrg #include <sys/mount.h>
58 1.1 mrg #include <sys/proc.h>
59 1.1 mrg #include <sys/malloc.h>
60 1.1 mrg #include <sys/vnode.h>
61 1.1 mrg
62 1.1 mrg #include <vm/vm.h>
63 1.1 mrg #include <vm/vm_page.h>
64 1.1 mrg #include <vm/vm_kern.h>
65 1.1 mrg
66 1.1 mrg #include <sys/syscallargs.h>
67 1.1 mrg
68 1.1 mrg #include <uvm/uvm.h>
69 1.1 mrg #include <uvm/uvm_vnode.h>
70 1.1 mrg
71 1.1 mrg UVMHIST_DECL(maphist);
72 1.1 mrg
73 1.1 mrg /*
74 1.1 mrg * private global data structure
75 1.1 mrg *
76 1.1 mrg * we keep a list of writeable active vnode-backed VM objects for sync op.
77 1.1 mrg * we keep a simpleq of vnodes that are currently being sync'd.
78 1.1 mrg */
79 1.1 mrg
80 1.1 mrg LIST_HEAD(uvn_list_struct, uvm_vnode);
81 1.1 mrg static struct uvn_list_struct uvn_wlist; /* writeable uvns */
82 1.1 mrg #if NCPU > 1
83 1.1 mrg static simple_lock_data_t uvn_wl_lock; /* locks uvn_wlist */
84 1.1 mrg #endif
85 1.1 mrg
86 1.1 mrg SIMPLEQ_HEAD(uvn_sq_struct, uvm_vnode);
87 1.1 mrg static struct uvn_sq_struct uvn_sync_q; /* sync'ing uvns */
88 1.1 mrg lock_data_t uvn_sync_lock; /* locks sync operation */
89 1.1 mrg
90 1.1 mrg /*
91 1.1 mrg * functions
92 1.1 mrg */
93 1.1 mrg
94 1.1 mrg static int uvn_asyncget __P((struct uvm_object *, vm_offset_t,
95 1.1 mrg int));
96 1.1 mrg struct uvm_object *uvn_attach __P((void *, vm_prot_t));
97 1.1 mrg static void uvn_cluster __P((struct uvm_object *, vm_offset_t,
98 1.1 mrg vm_offset_t *, vm_offset_t *));
99 1.1 mrg static void uvn_detach __P((struct uvm_object *));
100 1.1 mrg static boolean_t uvn_flush __P((struct uvm_object *, vm_offset_t,
101 1.1 mrg vm_offset_t, int));
102 1.1 mrg static int uvn_get __P((struct uvm_object *, vm_offset_t,
103 1.1 mrg vm_page_t *, int *, int,
104 1.1 mrg vm_prot_t, int, int));
105 1.1 mrg static void uvn_init __P((void));
106 1.1 mrg static int uvn_io __P((struct uvm_vnode *, vm_page_t *,
107 1.1 mrg int, int, int));
108 1.1 mrg static int uvn_put __P((struct uvm_object *, vm_page_t *,
109 1.1 mrg int, boolean_t));
110 1.1 mrg static void uvn_reference __P((struct uvm_object *));
111 1.1 mrg static boolean_t uvn_releasepg __P((struct vm_page *,
112 1.1 mrg struct vm_page **));
113 1.1 mrg
114 1.1 mrg /*
115 1.1 mrg * master pager structure
116 1.1 mrg */
117 1.1 mrg
118 1.1 mrg struct uvm_pagerops uvm_vnodeops = {
119 1.1 mrg uvn_init,
120 1.1 mrg uvn_attach,
121 1.1 mrg uvn_reference,
122 1.1 mrg uvn_detach,
123 1.1 mrg NULL, /* no specialized fault routine required */
124 1.1 mrg uvn_flush,
125 1.1 mrg uvn_get,
126 1.1 mrg uvn_asyncget,
127 1.1 mrg uvn_put,
128 1.1 mrg uvn_cluster,
129 1.1 mrg uvm_mk_pcluster, /* use generic version of this: see uvm_pager.c */
130 1.1 mrg uvm_shareprot, /* !NULL: allow us in share maps */
131 1.1 mrg NULL, /* AIO-DONE function (not until we have asyncio) */
132 1.1 mrg uvn_releasepg,
133 1.1 mrg };
134 1.1 mrg
135 1.1 mrg /*
136 1.1 mrg * the ops!
137 1.1 mrg */
138 1.1 mrg
139 1.1 mrg /*
140 1.1 mrg * uvn_init
141 1.1 mrg *
142 1.1 mrg * init pager private data structures.
143 1.1 mrg */
144 1.1 mrg
145 1.1 mrg static void uvn_init()
146 1.1 mrg
147 1.1 mrg {
148 1.1 mrg LIST_INIT(&uvn_wlist);
149 1.1 mrg simple_lock_init(&uvn_wl_lock);
150 1.1 mrg /* note: uvn_sync_q init'd in uvm_vnp_sync() */
151 1.1 mrg lockinit(&uvn_sync_lock, PVM, "uvnsync", 0, 0);
152 1.1 mrg }
153 1.1 mrg
154 1.1 mrg /*
155 1.1 mrg * uvn_attach
156 1.1 mrg *
157 1.1 mrg * attach a vnode structure to a VM object. if the vnode is already
158 1.1 mrg * attached, then just bump the reference count by one and return the
159 1.1 mrg * VM object. if not already attached, attach and return the new VM obj.
160 1.1 mrg * the "accessprot" tells the max access the attaching thread wants to
161 1.1 mrg * our pages.
162 1.1 mrg *
163 1.1 mrg * => caller must _not_ already be holding the lock on the uvm_object.
164 1.1 mrg * => in fact, nothing should be locked so that we can sleep here.
165 1.1 mrg * => note that uvm_object is first thing in vnode structure, so their
166 1.1 mrg * pointers are equiv.
167 1.1 mrg */
168 1.1 mrg
169 1.1 mrg struct uvm_object *uvn_attach(arg, accessprot)
170 1.1 mrg
171 1.1 mrg void *arg;
172 1.1 mrg vm_prot_t accessprot;
173 1.1 mrg
174 1.1 mrg {
175 1.1 mrg struct vnode *vp = arg;
176 1.1 mrg struct uvm_vnode *uvn = &vp->v_uvm;
177 1.1 mrg struct vattr vattr;
178 1.1 mrg int oldflags, result;
179 1.1 mrg u_quad_t used_vnode_size;
180 1.1 mrg UVMHIST_FUNC("uvn_attach"); UVMHIST_CALLED(maphist);
181 1.1 mrg
182 1.1 mrg UVMHIST_LOG(maphist, "(vn=0x%x)", arg,0,0,0);
183 1.1 mrg
184 1.1 mrg /*
185 1.1 mrg * first get a lock on the uvn.
186 1.1 mrg */
187 1.1 mrg simple_lock(&uvn->u_obj.vmobjlock);
188 1.1 mrg while (uvn->u_flags & UVM_VNODE_BLOCKED) {
189 1.1 mrg uvn->u_flags |= UVM_VNODE_WANTED;
190 1.1 mrg UVMHIST_LOG(maphist, " SLEEPING on blocked vn",0,0,0,0);
191 1.1 mrg UVM_UNLOCK_AND_WAIT(uvn, &uvn->u_obj.vmobjlock, FALSE, "uvn_attach",0);
192 1.1 mrg simple_lock(&uvn->u_obj.vmobjlock);
193 1.1 mrg UVMHIST_LOG(maphist," WOKE UP",0,0,0,0);
194 1.1 mrg }
195 1.1 mrg
196 1.1 mrg /*
197 1.1 mrg * now we have lock and uvn must not be in a blocked state.
198 1.1 mrg * first check to see if it is already active, in which case
199 1.1 mrg * we can bump the reference count, check to see if we need to
200 1.1 mrg * add it to the writeable list, and then return.
201 1.1 mrg */
202 1.1 mrg if (uvn->u_flags & UVM_VNODE_VALID) { /* already active? */
203 1.1 mrg
204 1.1 mrg /* regain VREF if we were persisting */
205 1.1 mrg if (uvn->u_obj.uo_refs == 0) {
206 1.1 mrg VREF(vp);
207 1.1 mrg UVMHIST_LOG(maphist," VREF (reclaim persisting vnode)", 0,0,0,0);
208 1.1 mrg }
209 1.1 mrg uvn->u_obj.uo_refs++; /* bump uvn ref! */
210 1.1 mrg
211 1.1 mrg /* check for new writeable uvn */
212 1.1 mrg if ((accessprot & VM_PROT_WRITE) != 0 &&
213 1.1 mrg (uvn->u_flags & UVM_VNODE_WRITEABLE) == 0) {
214 1.1 mrg simple_lock(&uvn_wl_lock);
215 1.1 mrg LIST_INSERT_HEAD(&uvn_wlist, uvn, u_wlist);
216 1.1 mrg simple_unlock(&uvn_wl_lock);
217 1.1 mrg uvn->u_flags |= UVM_VNODE_WRITEABLE; /* we are now on wlist! */
218 1.1 mrg }
219 1.1 mrg
220 1.1 mrg /* unlock and return */
221 1.1 mrg simple_unlock(&uvn->u_obj.vmobjlock);
222 1.1 mrg UVMHIST_LOG(maphist,"<- done, refcnt=%d", uvn->u_obj.uo_refs,0,0,0);
223 1.1 mrg return(&uvn->u_obj);
224 1.1 mrg }
225 1.1 mrg
226 1.1 mrg /*
227 1.1 mrg * need to call VOP_GETATTR() to get the attributes, but that could
228 1.1 mrg * block (due to I/O), so we want to unlock the object before calling.
229 1.1 mrg * however, we want to keep anyone else from playing with the object
230 1.1 mrg * while it is unlocked. to do this we set UVM_VNODE_ALOCK which
231 1.1 mrg * prevents anyone from attaching to the vnode until we are done with
232 1.1 mrg * it.
233 1.1 mrg */
234 1.1 mrg uvn->u_flags = UVM_VNODE_ALOCK;
235 1.1 mrg simple_unlock(&uvn->u_obj.vmobjlock); /* drop lock in case we sleep */
236 1.1 mrg /* XXX: curproc? */
237 1.1 mrg result = VOP_GETATTR(vp, &vattr, curproc->p_ucred, curproc);
238 1.1 mrg
239 1.1 mrg /*
240 1.1 mrg * make sure that the newsize fits within a vm_offset_t
241 1.1 mrg * XXX: need to revise addressing data types
242 1.1 mrg */
243 1.1 mrg used_vnode_size = vattr.va_size;
244 1.1 mrg if (used_vnode_size > (vm_offset_t) -PAGE_SIZE) {
245 1.1 mrg #ifdef DEBUG
246 1.1 mrg printf("uvn_attach: vn %p size truncated %qx->%x\n", vp, used_vnode_size,
247 1.1 mrg -PAGE_SIZE);
248 1.1 mrg #endif
249 1.1 mrg used_vnode_size = (vm_offset_t) -PAGE_SIZE;
250 1.1 mrg }
251 1.1 mrg
252 1.1 mrg /* relock object */
253 1.1 mrg simple_lock(&uvn->u_obj.vmobjlock);
254 1.1 mrg
255 1.1 mrg if (result != 0) {
256 1.1 mrg if (uvn->u_flags & UVM_VNODE_WANTED)
257 1.1 mrg wakeup(uvn);
258 1.1 mrg uvn->u_flags = 0;
259 1.1 mrg simple_unlock(&uvn->u_obj.vmobjlock); /* drop lock */
260 1.1 mrg UVMHIST_LOG(maphist,"<- done (VOP_GETATTR FAILED!)", 0,0,0,0);
261 1.1 mrg return(NULL);
262 1.1 mrg }
263 1.1 mrg
264 1.1 mrg /*
265 1.1 mrg * now set up the uvn.
266 1.1 mrg */
267 1.1 mrg uvn->u_obj.pgops = &uvm_vnodeops;
268 1.1 mrg TAILQ_INIT(&uvn->u_obj.memq);
269 1.1 mrg uvn->u_obj.uo_npages = 0;
270 1.1 mrg uvn->u_obj.uo_refs = 1; /* just us... */
271 1.1 mrg oldflags = uvn->u_flags;
272 1.1 mrg uvn->u_flags = UVM_VNODE_VALID|UVM_VNODE_CANPERSIST;
273 1.1 mrg uvn->u_nio = 0;
274 1.1 mrg uvn->u_size = used_vnode_size;
275 1.1 mrg
276 1.1 mrg /* if write access, we need to add it to the wlist */
277 1.1 mrg if (accessprot & VM_PROT_WRITE) {
278 1.1 mrg simple_lock(&uvn_wl_lock);
279 1.1 mrg LIST_INSERT_HEAD(&uvn_wlist, uvn, u_wlist);
280 1.1 mrg simple_unlock(&uvn_wl_lock);
281 1.1 mrg uvn->u_flags |= UVM_VNODE_WRITEABLE; /* we are on wlist! */
282 1.1 mrg }
283 1.1 mrg
284 1.1 mrg /*
285 1.1 mrg * add a reference to the vnode. this reference will stay as long
286 1.1 mrg * as there is a valid mapping of the vnode. dropped when the reference
287 1.1 mrg * count goes to zero [and we either free or persist].
288 1.1 mrg */
289 1.1 mrg VREF(vp);
290 1.1 mrg simple_unlock(&uvn->u_obj.vmobjlock);
291 1.1 mrg if (oldflags & UVM_VNODE_WANTED)
292 1.1 mrg wakeup(uvn);
293 1.1 mrg
294 1.1 mrg UVMHIST_LOG(maphist,"<- done/VREF, ret 0x%x", &uvn->u_obj,0,0,0);
295 1.1 mrg return(&uvn->u_obj);
296 1.1 mrg }
297 1.1 mrg
298 1.1 mrg
299 1.1 mrg /*
300 1.1 mrg * uvn_reference
301 1.1 mrg *
302 1.1 mrg * duplicate a reference to a VM object. Note that the reference
303 1.1 mrg * count must already be at least one (the passed in reference) so
304 1.1 mrg * there is no chance of the uvn being killed or locked out here.
305 1.1 mrg *
306 1.1 mrg * => caller must call with object unlocked.
307 1.1 mrg * => caller must be using the same accessprot as was used at attach time
308 1.1 mrg */
309 1.1 mrg
310 1.1 mrg
311 1.1 mrg static void uvn_reference(uobj)
312 1.1 mrg
313 1.1 mrg struct uvm_object *uobj;
314 1.1 mrg
315 1.1 mrg {
316 1.1 mrg #ifdef DIAGNOSTIC
317 1.1 mrg struct uvm_vnode *uvn = (struct uvm_vnode *) uobj;
318 1.1 mrg #endif
319 1.1 mrg UVMHIST_FUNC("uvn_reference"); UVMHIST_CALLED(maphist);
320 1.1 mrg
321 1.1 mrg simple_lock(&uobj->vmobjlock);
322 1.1 mrg #ifdef DIAGNOSTIC
323 1.1 mrg if ((uvn->u_flags & UVM_VNODE_VALID) == 0) {
324 1.1 mrg printf("uvn_reference: ref=%d, flags=0x%x\n", uvn->u_flags, uobj->uo_refs);
325 1.1 mrg panic("uvn_reference: invalid state");
326 1.1 mrg }
327 1.1 mrg #endif
328 1.1 mrg uobj->uo_refs++;
329 1.1 mrg UVMHIST_LOG(maphist, "<- done (uobj=0x%x, ref = %d)",
330 1.1 mrg uobj, uobj->uo_refs,0,0);
331 1.1 mrg simple_unlock(&uobj->vmobjlock);
332 1.1 mrg }
333 1.1 mrg
334 1.1 mrg /*
335 1.1 mrg * uvn_detach
336 1.1 mrg *
337 1.1 mrg * remove a reference to a VM object.
338 1.1 mrg *
339 1.1 mrg * => caller must call with object unlocked and map locked.
340 1.1 mrg * => this starts the detach process, but doesn't have to finish it
341 1.1 mrg * (async i/o could still be pending).
342 1.1 mrg */
343 1.1 mrg
344 1.1 mrg static void uvn_detach(uobj)
345 1.1 mrg
346 1.1 mrg struct uvm_object *uobj;
347 1.1 mrg
348 1.1 mrg {
349 1.1 mrg struct uvm_vnode *uvn;
350 1.1 mrg struct vnode *vp;
351 1.1 mrg int oldflags;
352 1.1 mrg UVMHIST_FUNC("uvn_detach"); UVMHIST_CALLED(maphist);
353 1.1 mrg
354 1.1 mrg simple_lock(&uobj->vmobjlock);
355 1.1 mrg
356 1.1 mrg UVMHIST_LOG(maphist," (uobj=0x%x) ref=%d", uobj,uobj->uo_refs,0,0);
357 1.1 mrg uobj->uo_refs--; /* drop ref! */
358 1.1 mrg if (uobj->uo_refs) { /* still more refs */
359 1.1 mrg simple_unlock(&uobj->vmobjlock);
360 1.1 mrg UVMHIST_LOG(maphist, "<- done (rc>0)", 0,0,0,0);
361 1.1 mrg return;
362 1.1 mrg }
363 1.1 mrg
364 1.1 mrg /*
365 1.1 mrg * get other pointers ...
366 1.1 mrg */
367 1.1 mrg
368 1.1 mrg uvn = (struct uvm_vnode *) uobj;
369 1.1 mrg vp = (struct vnode *) uobj;
370 1.1 mrg
371 1.1 mrg /*
372 1.1 mrg * clear VTEXT flag now that there are no mappings left (VTEXT is used
373 1.1 mrg * to keep an active text file from being overwritten).
374 1.1 mrg */
375 1.1 mrg vp->v_flag &= ~VTEXT;
376 1.1 mrg
377 1.1 mrg /*
378 1.1 mrg * we just dropped the last reference to the uvn. see if we can
379 1.1 mrg * let it "stick around".
380 1.1 mrg */
381 1.1 mrg
382 1.1 mrg if (uvn->u_flags & UVM_VNODE_CANPERSIST) {
383 1.1 mrg uvn_flush(uobj, 0, 0, PGO_DEACTIVATE|PGO_ALLPAGES); /* won't block */
384 1.1 mrg vrele(vp); /* drop vnode reference */
385 1.1 mrg simple_unlock(&uobj->vmobjlock);
386 1.1 mrg UVMHIST_LOG(maphist,"<- done/vrele! (persist)", 0,0,0,0);
387 1.1 mrg return;
388 1.1 mrg }
389 1.1 mrg
390 1.1 mrg /*
391 1.1 mrg * its a goner!
392 1.1 mrg */
393 1.1 mrg
394 1.1 mrg UVMHIST_LOG(maphist," its a goner (flushing)!", 0,0,0,0);
395 1.1 mrg
396 1.1 mrg uvn->u_flags |= UVM_VNODE_DYING;
397 1.1 mrg
398 1.1 mrg /*
399 1.1 mrg * even though we may unlock in flush, no one can gain a reference
400 1.1 mrg * to us until we clear the "dying" flag [because it blocks
401 1.1 mrg * attaches]. we will not do that until after we've disposed of all
402 1.1 mrg * the pages with uvn_flush(). note that before the flush the only
403 1.1 mrg * pages that could be marked PG_BUSY are ones that are in async
404 1.1 mrg * pageout by the daemon. (there can't be any pending "get"'s
405 1.1 mrg * because there are no references to the object).
406 1.1 mrg */
407 1.1 mrg
408 1.1 mrg (void) uvn_flush(uobj, 0, 0, PGO_CLEANIT|PGO_FREE|PGO_ALLPAGES);
409 1.1 mrg
410 1.1 mrg UVMHIST_LOG(maphist," its a goner (done flush)!", 0,0,0,0);
411 1.1 mrg
412 1.1 mrg /*
413 1.1 mrg * given the structure of this pager, the above flush request will
414 1.1 mrg * create the following state: all the pages that were in the object
415 1.1 mrg * have either been free'd or they are marked PG_BUSY|PG_RELEASED.
416 1.1 mrg * the PG_BUSY bit was set either by us or the daemon for async I/O.
417 1.1 mrg * in either case, if we have pages left we can't kill the object
418 1.1 mrg * yet because i/o is pending. in this case we set the "relkill"
419 1.1 mrg * flag which will cause pgo_releasepg to kill the object once all
420 1.1 mrg * the I/O's are done [pgo_releasepg will be called from the aiodone
421 1.1 mrg * routine or from the page daemon].
422 1.1 mrg */
423 1.1 mrg
424 1.1 mrg if (uobj->uo_npages) { /* I/O pending. iodone will free */
425 1.1 mrg #ifdef DIAGNOSTIC
426 1.1 mrg /*
427 1.1 mrg * XXXCDC: very unlikely to happen until we have async i/o so print
428 1.1 mrg * a little info message in case it does.
429 1.1 mrg */
430 1.1 mrg printf("uvn_detach: vn %p has pages left after flush - relkill mode\n",
431 1.1 mrg uobj);
432 1.1 mrg #endif
433 1.1 mrg uvn->u_flags |= UVM_VNODE_RELKILL;
434 1.1 mrg simple_unlock(&uobj->vmobjlock);
435 1.1 mrg UVMHIST_LOG(maphist,"<- done! (releasepg will kill obj)", 0,0,0,0);
436 1.1 mrg return;
437 1.1 mrg }
438 1.1 mrg
439 1.1 mrg /*
440 1.1 mrg * kill object now. note that we can't be on the sync q because
441 1.1 mrg * all references are gone.
442 1.1 mrg */
443 1.1 mrg if (uvn->u_flags & UVM_VNODE_WRITEABLE) {
444 1.1 mrg simple_lock(&uvn_wl_lock); /* protect uvn_wlist */
445 1.1 mrg LIST_REMOVE(uvn, u_wlist);
446 1.1 mrg simple_unlock(&uvn_wl_lock);
447 1.1 mrg }
448 1.1 mrg #ifdef DIAGNOSTIC
449 1.1 mrg if (uobj->memq.tqh_first != NULL)
450 1.1 mrg panic("uvn_deref: vnode VM object still has pages afer syncio/free flush");
451 1.1 mrg #endif
452 1.1 mrg oldflags = uvn->u_flags;
453 1.1 mrg uvn->u_flags = 0;
454 1.1 mrg simple_unlock(&uobj->vmobjlock);
455 1.1 mrg
456 1.1 mrg /* wake up any sleepers */
457 1.1 mrg if (oldflags & UVM_VNODE_WANTED)
458 1.1 mrg wakeup(uvn);
459 1.1 mrg
460 1.1 mrg /*
461 1.1 mrg * drop our reference to the vnode.
462 1.1 mrg */
463 1.1 mrg vrele(vp);
464 1.1 mrg UVMHIST_LOG(maphist,"<- done (vrele) final", 0,0,0,0);
465 1.1 mrg
466 1.1 mrg return;
467 1.1 mrg }
468 1.1 mrg
469 1.1 mrg /*
470 1.1 mrg * uvm_vnp_terminate: external hook to clear out a vnode's VM
471 1.1 mrg *
472 1.1 mrg * called when a persisting vnode vm object (i.e. one with zero
473 1.1 mrg * reference count) needs to be freed so that a vnode can be reused.
474 1.1 mrg * this happens under "getnewvnode" in vfs_subr.c. if the vnode (from
475 1.1 mrg * the free list) is still attached (i.e. not VBAD) then vgone is
476 1.1 mrg * called. as part of the vgone trace this should get called to free
477 1.1 mrg * the vm object. note that the vnode must be XLOCK'd before calling
478 1.1 mrg * here. the XLOCK protects us from getting a vnode which is already
479 1.1 mrg * in the DYING state.
480 1.1 mrg *
481 1.1 mrg * => the vnode must be XLOCK'd _and_ VOP_LOCK()'d by caller
482 1.1 mrg * => unlike uvn_detach, this function must not return until the
483 1.1 mrg * old uvm_object is dead.
484 1.1 mrg */
485 1.1 mrg
486 1.1 mrg void uvm_vnp_terminate(vp)
487 1.1 mrg
488 1.1 mrg struct vnode *vp;
489 1.1 mrg
490 1.1 mrg {
491 1.1 mrg struct uvm_vnode *uvn = &vp->v_uvm;
492 1.1 mrg UVMHIST_FUNC("uvm_vnp_terminate"); UVMHIST_CALLED(maphist);
493 1.1 mrg
494 1.1 mrg /*
495 1.1 mrg * lock object and check if it is valid
496 1.1 mrg */
497 1.1 mrg simple_lock(&uvn->u_obj.vmobjlock);
498 1.1 mrg UVMHIST_LOG(maphist, " vp=0x%x, ref=%d, flag=0x%x", vp, uvn->u_obj.uo_refs,
499 1.1 mrg uvn->u_flags, 0);
500 1.1 mrg if ((uvn->u_flags & UVM_VNODE_VALID) == 0) {
501 1.1 mrg simple_unlock(&uvn->u_obj.vmobjlock);
502 1.1 mrg UVMHIST_LOG(maphist, "<- done (not active)", 0, 0, 0, 0);
503 1.1 mrg return;
504 1.1 mrg }
505 1.1 mrg
506 1.1 mrg /*
507 1.1 mrg * must be a valid, persisting vnode that is not already dying.
508 1.1 mrg * can't be ALOCK because that would require someone else having
509 1.1 mrg * a valid reference to the underlying vnode (and if that was true
510 1.1 mrg * then the kernel wouldn't want to terminate it).
511 1.1 mrg */
512 1.1 mrg if ((uvn->u_flags &
513 1.1 mrg (UVM_VNODE_VALID|UVM_VNODE_CANPERSIST|UVM_VNODE_ALOCK)) !=
514 1.1 mrg (UVM_VNODE_VALID|UVM_VNODE_CANPERSIST)) {
515 1.1 mrg printf("uvm_vnp_terminate: flags = 0x%x, refs=%d\n", uvn->u_flags,
516 1.1 mrg uvn->u_obj.uo_refs);
517 1.1 mrg panic("uvm_vnp_terminate: uvn in unexpected state");
518 1.1 mrg }
519 1.1 mrg
520 1.1 mrg #ifdef DIAGNOSTIC
521 1.1 mrg /*
522 1.1 mrg * diagnostic check: is uvn persisting?
523 1.1 mrg */
524 1.1 mrg if (uvn->u_obj.uo_refs) {
525 1.1 mrg printf("uvm_vnp_terminate(%p): warning: object still active with %d refs\n",
526 1.1 mrg uvn, uvn->u_obj.uo_refs);
527 1.1 mrg }
528 1.1 mrg #endif
529 1.1 mrg
530 1.1 mrg /*
531 1.1 mrg * it is possible that the uvn was detached and is in the relkill
532 1.1 mrg * state [i.e. waiting for async i/o to finish so that releasepg can
533 1.1 mrg * kill object]. we take over the vnode now and cancel the relkill.
534 1.1 mrg * we want to know when the i/o is done so we can recycle right
535 1.1 mrg * away.
536 1.1 mrg */
537 1.1 mrg
538 1.1 mrg if (uvn->u_flags & UVM_VNODE_RELKILL)
539 1.1 mrg uvn->u_flags &= ~UVM_VNODE_RELKILL; /* cancel RELKILL */
540 1.1 mrg
541 1.1 mrg /*
542 1.1 mrg * block the uvn by setting the dying flag, and then flush the
543 1.1 mrg * pages. (note that flush may unlock object while doing I/O, but
544 1.1 mrg * it will re-lock it before it returns control here).
545 1.1 mrg *
546 1.1 mrg * also, note that we tell I/O that we are already VOP_LOCK'd so
547 1.1 mrg * that uvn_io doesn't attempt to VOP_LOCK again.
548 1.1 mrg */
549 1.1 mrg uvn->u_flags |= UVM_VNODE_DYING|UVM_VNODE_VNISLOCKED;
550 1.1 mrg
551 1.1 mrg (void) uvn_flush(&uvn->u_obj, 0, 0, PGO_CLEANIT|PGO_FREE|PGO_ALLPAGES);
552 1.1 mrg
553 1.1 mrg /*
554 1.1 mrg * as we just did a flush we expect all the pages to be gone or in
555 1.1 mrg * the process of going. sleep to wait for the rest to go [via iosync].
556 1.1 mrg */
557 1.1 mrg
558 1.1 mrg while (uvn->u_obj.uo_npages) {
559 1.1 mrg #ifdef DIAGNOSTIC
560 1.1 mrg struct vm_page *pp;
561 1.1 mrg for (pp = uvn->u_obj.memq.tqh_first ; pp != NULL ;
562 1.1 mrg pp = pp->listq.tqe_next) {
563 1.1 mrg if ((pp->flags & PG_BUSY) == 0)
564 1.1 mrg panic("uvm_vnp_terminate: detected unbusy page");
565 1.1 mrg }
566 1.1 mrg if (uvn->u_nio == 0)
567 1.1 mrg panic("uvm_vnp_terminate: no I/O to wait for?");
568 1.1 mrg printf("uvm_vnp_terminate: waiting for I/O to fin.\n");
569 1.1 mrg /*
570 1.1 mrg * XXXCDC: this is unlikely to happen without async i/o so we
571 1.1 mrg * put a printf in just to keep an eye on it.
572 1.1 mrg */
573 1.1 mrg #endif
574 1.1 mrg uvn->u_flags |= UVM_VNODE_IOSYNC;
575 1.1 mrg UVM_UNLOCK_AND_WAIT(&uvn->u_nio, &uvn->u_obj.vmobjlock, FALSE,
576 1.1 mrg "uvn_term",0);
577 1.1 mrg simple_lock(&uvn->u_obj.vmobjlock);
578 1.1 mrg }
579 1.1 mrg
580 1.1 mrg /*
581 1.1 mrg * free the uvn now. note that the VREF reference is already gone
582 1.1 mrg * [it is dropped when we enter the persist state].
583 1.1 mrg */
584 1.1 mrg if (uvn->u_flags & UVM_VNODE_IOSYNCWANTED)
585 1.1 mrg panic("uvm_vnp_terminate: io sync wanted bit set");
586 1.1 mrg
587 1.1 mrg if (uvn->u_flags & UVM_VNODE_WRITEABLE) {
588 1.1 mrg simple_lock(&uvn_wl_lock);
589 1.1 mrg LIST_REMOVE(uvn, u_wlist);
590 1.1 mrg simple_unlock(&uvn_wl_lock);
591 1.1 mrg }
592 1.1 mrg if (uvn->u_flags & UVM_VNODE_WANTED)
593 1.1 mrg wakeup(uvn); /* object lock still held */
594 1.1 mrg uvn->u_flags = 0;
595 1.1 mrg simple_unlock(&uvn->u_obj.vmobjlock);
596 1.1 mrg UVMHIST_LOG(maphist, "<- done", 0, 0, 0, 0);
597 1.1 mrg
598 1.1 mrg }
599 1.1 mrg
600 1.1 mrg /*
601 1.1 mrg * uvn_releasepg: handled a released page in a uvn
602 1.1 mrg *
603 1.1 mrg * => "pg" is a PG_BUSY [caller owns it], PG_RELEASED page that we need
604 1.1 mrg * to dispose of.
605 1.1 mrg * => caller must handled PG_WANTED case
606 1.1 mrg * => called with page's object locked, pageq's unlocked
607 1.1 mrg * => returns TRUE if page's object is still alive, FALSE if we
608 1.1 mrg * killed the page's object. if we return TRUE, then we
609 1.1 mrg * return with the object locked.
610 1.1 mrg * => if (nextpgp != NULL) => we return pageq.tqe_next here, and return
611 1.1 mrg * with the page queues locked [for pagedaemon]
612 1.1 mrg * => if (nextpgp == NULL) => we return with page queues unlocked [normal case]
613 1.1 mrg * => we kill the uvn if it is not referenced and we are suppose to
614 1.1 mrg * kill it ("relkill").
615 1.1 mrg */
616 1.1 mrg
617 1.1 mrg boolean_t uvn_releasepg(pg, nextpgp)
618 1.1 mrg
619 1.1 mrg struct vm_page *pg;
620 1.1 mrg struct vm_page **nextpgp; /* OUT */
621 1.1 mrg
622 1.1 mrg {
623 1.1 mrg struct uvm_vnode *uvn = (struct uvm_vnode *) pg->uobject;
624 1.1 mrg #ifdef DIAGNOSTIC
625 1.1 mrg if ((pg->flags & PG_RELEASED) == 0)
626 1.1 mrg panic("uvn_releasepg: page not released!");
627 1.1 mrg #endif
628 1.1 mrg
629 1.1 mrg /*
630 1.1 mrg * dispose of the page [caller handles PG_WANTED]
631 1.1 mrg */
632 1.1 mrg pmap_page_protect(PMAP_PGARG(pg), VM_PROT_NONE);
633 1.1 mrg uvm_lock_pageq();
634 1.1 mrg if (nextpgp)
635 1.1 mrg *nextpgp = pg->pageq.tqe_next; /* next page for daemon */
636 1.1 mrg uvm_pagefree(pg);
637 1.1 mrg if (!nextpgp)
638 1.1 mrg uvm_unlock_pageq();
639 1.1 mrg
640 1.1 mrg /*
641 1.1 mrg * now see if we need to kill the object
642 1.1 mrg */
643 1.1 mrg if (uvn->u_flags & UVM_VNODE_RELKILL) {
644 1.1 mrg if (uvn->u_obj.uo_refs)
645 1.1 mrg panic("uvn_releasepg: kill flag set on referenced object!");
646 1.1 mrg if (uvn->u_obj.uo_npages == 0) {
647 1.1 mrg if (uvn->u_flags & UVM_VNODE_WRITEABLE) {
648 1.1 mrg simple_lock(&uvn_wl_lock);
649 1.1 mrg LIST_REMOVE(uvn, u_wlist);
650 1.1 mrg simple_unlock(&uvn_wl_lock);
651 1.1 mrg }
652 1.1 mrg #ifdef DIAGNOSTIC
653 1.1 mrg if (uvn->u_obj.memq.tqh_first)
654 1.1 mrg panic("uvn_releasepg: pages in object with npages == 0");
655 1.1 mrg #endif
656 1.1 mrg if (uvn->u_flags & UVM_VNODE_WANTED)
657 1.1 mrg wakeup(uvn); /* still holding object lock */
658 1.1 mrg uvn->u_flags = 0; /* DEAD! */
659 1.1 mrg simple_unlock(&uvn->u_obj.vmobjlock);
660 1.1 mrg return(FALSE);
661 1.1 mrg }
662 1.1 mrg }
663 1.1 mrg return(TRUE);
664 1.1 mrg }
665 1.1 mrg
666 1.1 mrg /*
667 1.1 mrg * NOTE: currently we have to use VOP_READ/VOP_WRITE because they go
668 1.1 mrg * through the buffer cache and allow I/O in any size. These VOPs use
669 1.1 mrg * synchronous i/o. [vs. VOP_STRATEGY which can be async, but doesn't
670 1.1 mrg * go through the buffer cache or allow I/O sizes larger than a
671 1.1 mrg * block]. we will eventually want to change this.
672 1.1 mrg *
673 1.1 mrg * issues to consider:
674 1.1 mrg * uvm provides the uvm_aiodesc structure for async i/o management.
675 1.1 mrg * there are two tailq's in the uvm. structure... one for pending async
676 1.1 mrg * i/o and one for "done" async i/o. to do an async i/o one puts
677 1.1 mrg * an aiodesc on the "pending" list (protected by splbio()), starts the
678 1.1 mrg * i/o and returns VM_PAGER_PEND. when the i/o is done, we expect
679 1.1 mrg * some sort of "i/o done" function to be called (at splbio(), interrupt
680 1.1 mrg * time). this function should remove the aiodesc from the pending list
681 1.1 mrg * and place it on the "done" list and wakeup the daemon. the daemon
682 1.1 mrg * will run at normal spl() and will remove all items from the "done"
683 1.1 mrg * list and call the "aiodone" hook for each done request (see uvm_pager.c).
684 1.1 mrg * [in the old vm code, this was done by calling the "put" routine with
685 1.1 mrg * null arguments which made the code harder to read and understand because
686 1.1 mrg * you had one function ("put") doing two things.]
687 1.1 mrg *
688 1.1 mrg * so the current pager needs:
689 1.1 mrg * int uvn_aiodone(struct uvm_aiodesc *)
690 1.1 mrg *
691 1.1 mrg * => return KERN_SUCCESS (aio finished, free it). otherwise requeue for
692 1.1 mrg * later collection.
693 1.1 mrg * => called with pageq's locked by the daemon.
694 1.1 mrg *
695 1.1 mrg * general outline:
696 1.1 mrg * - "try" to lock object. if fail, just return (will try again later)
697 1.1 mrg * - drop "u_nio" (this req is done!)
698 1.1 mrg * - if (object->iosync && u_naio == 0) { wakeup &uvn->u_naio }
699 1.1 mrg * - get "page" structures (atop?).
700 1.1 mrg * - handle "wanted" pages
701 1.1 mrg * - handle "released" pages [using pgo_releasepg]
702 1.1 mrg * >>> pgo_releasepg may kill the object
703 1.1 mrg * dont forget to look at "object" wanted flag in all cases.
704 1.1 mrg */
705 1.1 mrg
706 1.1 mrg
707 1.1 mrg /*
708 1.1 mrg * uvn_flush: flush pages out of a uvm object.
709 1.1 mrg *
710 1.1 mrg * => object should be locked by caller. we may _unlock_ the object
711 1.1 mrg * if (and only if) we need to clean a page (PGO_CLEANIT).
712 1.1 mrg * we return with the object locked.
713 1.1 mrg * => if PGO_CLEANIT is set, we may block (due to I/O). thus, a caller
714 1.1 mrg * might want to unlock higher level resources (e.g. vm_map)
715 1.1 mrg * before calling flush.
716 1.1 mrg * => if PGO_CLEANIT is not set, then we will neither unlock the object
717 1.1 mrg * or block.
718 1.1 mrg * => if PGO_ALLPAGE is set, then all pages in the object are valid targets
719 1.1 mrg * for flushing.
720 1.1 mrg * => NOTE: we rely on the fact that the object's memq is a TAILQ and
721 1.1 mrg * that new pages are inserted on the tail end of the list. thus,
722 1.1 mrg * we can make a complete pass through the object in one go by starting
723 1.1 mrg * at the head and working towards the tail (new pages are put in
724 1.1 mrg * front of us).
725 1.1 mrg * => NOTE: we are allowed to lock the page queues, so the caller
726 1.1 mrg * must not be holding the lock on them [e.g. pagedaemon had
727 1.1 mrg * better not call us with the queues locked]
728 1.1 mrg * => we return TRUE unless we encountered some sort of I/O error
729 1.1 mrg *
730 1.1 mrg * comment on "cleaning" object and PG_BUSY pages:
731 1.1 mrg * this routine is holding the lock on the object. the only time
732 1.1 mrg * that it can run into a PG_BUSY page that it does not own is if
733 1.1 mrg * some other process has started I/O on the page (e.g. either
734 1.1 mrg * a pagein, or a pageout). if the PG_BUSY page is being paged
735 1.1 mrg * in, then it can not be dirty (!PG_CLEAN) because no one has
736 1.1 mrg * had a chance to modify it yet. if the PG_BUSY page is being
737 1.1 mrg * paged out then it means that someone else has already started
738 1.1 mrg * cleaning the page for us (how nice!). in this case, if we
739 1.1 mrg * have syncio specified, then after we make our pass through the
740 1.1 mrg * object we need to wait for the other PG_BUSY pages to clear
741 1.1 mrg * off (i.e. we need to do an iosync). also note that once a
742 1.1 mrg * page is PG_BUSY it must stay in its object until it is un-busyed.
743 1.1 mrg *
744 1.1 mrg * note on page traversal:
745 1.1 mrg * we can traverse the pages in an object either by going down the
746 1.1 mrg * linked list in "uobj->memq", or we can go over the address range
747 1.1 mrg * by page doing hash table lookups for each address. depending
748 1.1 mrg * on how many pages are in the object it may be cheaper to do one
749 1.1 mrg * or the other. we set "by_list" to true if we are using memq.
750 1.1 mrg * if the cost of a hash lookup was equal to the cost of the list
751 1.1 mrg * traversal we could compare the number of pages in the start->stop
752 1.1 mrg * range to the total number of pages in the object. however, it
753 1.1 mrg * seems that a hash table lookup is more expensive than the linked
754 1.1 mrg * list traversal, so we multiply the number of pages in the
755 1.1 mrg * start->stop range by a penalty which we define below.
756 1.1 mrg */
757 1.1 mrg
758 1.1 mrg #define UVN_HASH_PENALTY 4 /* a guess */
759 1.1 mrg
760 1.1 mrg static boolean_t uvn_flush(uobj, start, stop, flags)
761 1.1 mrg
762 1.1 mrg struct uvm_object *uobj;
763 1.1 mrg vm_offset_t start, stop;
764 1.1 mrg int flags;
765 1.1 mrg
766 1.1 mrg {
767 1.1 mrg struct uvm_vnode *uvn = (struct uvm_vnode *) uobj;
768 1.1 mrg struct vm_page *pp, *ppnext, *ptmp;
769 1.1 mrg struct vm_page *pps[MAXBSIZE/PAGE_SIZE], **ppsp;
770 1.1 mrg int npages, result, lcv;
771 1.1 mrg boolean_t retval, need_iosync, by_list, needs_clean;
772 1.1 mrg vm_offset_t curoff;
773 1.1 mrg u_short pp_version;
774 1.1 mrg UVMHIST_FUNC("uvn_flush"); UVMHIST_CALLED(maphist);
775 1.1 mrg
776 1.1 mrg curoff = 0; /* XXX: shut up gcc */
777 1.1 mrg /*
778 1.1 mrg * get init vals and determine how we are going to traverse object
779 1.1 mrg */
780 1.1 mrg
781 1.1 mrg need_iosync = FALSE;
782 1.1 mrg retval = TRUE; /* return value */
783 1.1 mrg if (flags & PGO_ALLPAGES) {
784 1.1 mrg start = 0;
785 1.1 mrg stop = round_page(uvn->u_size);
786 1.1 mrg by_list = TRUE; /* always go by the list */
787 1.1 mrg } else {
788 1.1 mrg start = trunc_page(start);
789 1.1 mrg stop = round_page(stop);
790 1.1 mrg if (stop > round_page(uvn->u_size))
791 1.1 mrg printf("uvn_flush: strange, got an out of range flush (fixed)\n");
792 1.1 mrg
793 1.1 mrg by_list = (uobj->uo_npages <=
794 1.1 mrg ((stop - start) / PAGE_SIZE) * UVN_HASH_PENALTY);
795 1.1 mrg }
796 1.1 mrg
797 1.1 mrg UVMHIST_LOG(maphist," flush start=0x%x, stop=0x%x, by_list=%d, flags=0x%x",
798 1.1 mrg start, stop, by_list, flags);
799 1.1 mrg
800 1.1 mrg /*
801 1.1 mrg * PG_CLEANCHK: this bit is used by the pgo_mk_pcluster function as
802 1.1 mrg * a _hint_ as to how up to date the PG_CLEAN bit is. if the hint
803 1.1 mrg * is wrong it will only prevent us from clustering... it won't break
804 1.1 mrg * anything. we clear all PG_CLEANCHK bits here, and pgo_mk_pcluster
805 1.1 mrg * will set them as it syncs PG_CLEAN. This is only an issue if we
806 1.1 mrg * are looking at non-inactive pages (because inactive page's PG_CLEAN
807 1.1 mrg * bit is always up to date since there are no mappings).
808 1.1 mrg * [borrowed PG_CLEANCHK idea from FreeBSD VM]
809 1.1 mrg */
810 1.1 mrg
811 1.1 mrg if ((flags & PGO_CLEANIT) != 0 && uobj->pgops->pgo_mk_pcluster != NULL) {
812 1.1 mrg
813 1.1 mrg if (by_list) {
814 1.1 mrg for (pp = uobj->memq.tqh_first ; pp != NULL ; pp = pp->listq.tqe_next) {
815 1.1 mrg if (pp->offset < start || pp->offset >= stop)
816 1.1 mrg continue;
817 1.1 mrg pp->flags &= ~PG_CLEANCHK;
818 1.1 mrg }
819 1.1 mrg
820 1.1 mrg } else { /* by hash */
821 1.1 mrg for (curoff = start ; curoff < stop ; curoff += PAGE_SIZE) {
822 1.1 mrg pp = uvm_pagelookup(uobj, curoff);
823 1.1 mrg if (pp)
824 1.1 mrg pp->flags &= ~PG_CLEANCHK;
825 1.1 mrg }
826 1.1 mrg }
827 1.1 mrg
828 1.1 mrg }
829 1.1 mrg
830 1.1 mrg /*
831 1.1 mrg * now do it. note: we must update ppnext in body of loop or we
832 1.1 mrg * will get stuck. we need to use ppnext because we may free "pp"
833 1.1 mrg * before doing the next loop.
834 1.1 mrg */
835 1.1 mrg
836 1.1 mrg if (by_list) {
837 1.1 mrg pp = uobj->memq.tqh_first;
838 1.1 mrg } else {
839 1.1 mrg curoff = start;
840 1.1 mrg pp = uvm_pagelookup(uobj, curoff);
841 1.1 mrg }
842 1.1 mrg
843 1.1 mrg ppnext = NULL; /* XXX: shut up gcc */
844 1.1 mrg ppsp = NULL; /* XXX: shut up gcc */
845 1.1 mrg uvm_lock_pageq(); /* page queues locked */
846 1.1 mrg
847 1.1 mrg /* locked: both page queues and uobj */
848 1.1 mrg for ( ; (by_list && pp != NULL) ||
849 1.1 mrg (!by_list && curoff < stop) ; pp = ppnext) {
850 1.1 mrg
851 1.1 mrg if (by_list) {
852 1.1 mrg
853 1.1 mrg /*
854 1.1 mrg * range check
855 1.1 mrg */
856 1.1 mrg
857 1.1 mrg if (pp->offset < start || pp->offset >= stop) {
858 1.1 mrg ppnext = pp->listq.tqe_next;
859 1.1 mrg continue;
860 1.1 mrg }
861 1.1 mrg
862 1.1 mrg } else {
863 1.1 mrg
864 1.1 mrg /*
865 1.1 mrg * null check
866 1.1 mrg */
867 1.1 mrg
868 1.1 mrg curoff += PAGE_SIZE;
869 1.1 mrg if (pp == NULL) {
870 1.1 mrg if (curoff < stop)
871 1.1 mrg ppnext = uvm_pagelookup(uobj, curoff);
872 1.1 mrg continue;
873 1.1 mrg }
874 1.1 mrg
875 1.1 mrg }
876 1.1 mrg
877 1.1 mrg /*
878 1.1 mrg * handle case where we do not need to clean page (either because
879 1.1 mrg * we are not clean or because page is not dirty or is busy):
880 1.1 mrg *
881 1.1 mrg * NOTE: we are allowed to deactivate a non-wired active PG_BUSY page,
882 1.1 mrg * but once a PG_BUSY page is on the inactive queue it must
883 1.1 mrg * stay put until it is !PG_BUSY (so as not to confuse pagedaemon).
884 1.1 mrg */
885 1.1 mrg
886 1.1 mrg if ((flags & PGO_CLEANIT) == 0 || (pp->flags & PG_BUSY) != 0) {
887 1.1 mrg needs_clean = FALSE;
888 1.1 mrg if ((pp->flags & PG_BUSY) != 0 &&
889 1.1 mrg (flags & (PGO_CLEANIT|PGO_SYNCIO)) == (PGO_CLEANIT|PGO_SYNCIO))
890 1.1 mrg need_iosync = TRUE;
891 1.1 mrg } else {
892 1.1 mrg /* freeing: nuke all mappings so we can sync PG_CLEAN bit with no race */
893 1.1 mrg if ((pp->flags & PG_CLEAN) != 0 &&
894 1.1 mrg (flags & PGO_FREE) != 0 && (pp->pqflags & PQ_ACTIVE) != 0)
895 1.1 mrg pmap_page_protect(PMAP_PGARG(pp), VM_PROT_NONE);
896 1.1 mrg if ((pp->flags & PG_CLEAN) != 0 && pmap_is_modified(PMAP_PGARG(pp)))
897 1.1 mrg pp->flags &= ~(PG_CLEAN);
898 1.1 mrg pp->flags |= PG_CLEANCHK; /* update "hint" */
899 1.1 mrg
900 1.1 mrg needs_clean = ((pp->flags & PG_CLEAN) == 0);
901 1.1 mrg }
902 1.1 mrg
903 1.1 mrg /*
904 1.1 mrg * if we don't need a clean... load ppnext and dispose of pp
905 1.1 mrg */
906 1.1 mrg if (!needs_clean) {
907 1.1 mrg /* load ppnext */
908 1.1 mrg if (by_list)
909 1.1 mrg ppnext = pp->listq.tqe_next;
910 1.1 mrg else {
911 1.1 mrg if (curoff < stop)
912 1.1 mrg ppnext = uvm_pagelookup(uobj, curoff);
913 1.1 mrg }
914 1.1 mrg
915 1.1 mrg /* now dispose of pp */
916 1.1 mrg if (flags & PGO_DEACTIVATE) {
917 1.1 mrg if ((pp->pqflags & PQ_INACTIVE) == 0 && pp->wire_count == 0) {
918 1.1 mrg pmap_page_protect(PMAP_PGARG(pp), VM_PROT_NONE);
919 1.1 mrg uvm_pagedeactivate(pp);
920 1.1 mrg }
921 1.1 mrg
922 1.1 mrg } else if (flags & PGO_FREE) {
923 1.1 mrg if (pp->flags & PG_BUSY) {
924 1.1 mrg pp->flags |= PG_RELEASED; /* release busy pages */
925 1.1 mrg } else {
926 1.1 mrg pmap_page_protect(PMAP_PGARG(pp), VM_PROT_NONE);
927 1.1 mrg uvm_pagefree(pp); /* removed page from object */
928 1.1 mrg }
929 1.1 mrg
930 1.1 mrg }
931 1.1 mrg /* ppnext is valid so we can continue... */
932 1.1 mrg continue;
933 1.1 mrg }
934 1.1 mrg
935 1.1 mrg /*
936 1.1 mrg * pp points to a page in the locked object that we are working on.
937 1.1 mrg * if it is !PG_CLEAN,!PG_BUSY and we asked for cleaning (PGO_CLEANIT).
938 1.1 mrg * we clean it now.
939 1.1 mrg *
940 1.1 mrg * let uvm_pager_put attempted a clustered page out.
941 1.1 mrg * note: locked: uobj and page queues.
942 1.1 mrg */
943 1.1 mrg
944 1.1 mrg pp->flags |= PG_BUSY; /* we 'own' page now */
945 1.1 mrg UVM_PAGE_OWN(pp, "uvn_flush");
946 1.1 mrg pmap_page_protect(PMAP_PGARG(pp), VM_PROT_READ);
947 1.1 mrg pp_version = pp->version;
948 1.1 mrg ReTry:
949 1.1 mrg ppsp = pps;
950 1.1 mrg npages = sizeof(pps) / sizeof(struct vm_page *);
951 1.1 mrg
952 1.1 mrg /* locked: page queues, uobj */
953 1.1 mrg result = uvm_pager_put(uobj, pp, &ppsp, &npages,
954 1.1 mrg flags | PGO_DOACTCLUST, start, stop);
955 1.1 mrg /* unlocked: page queues, uobj */
956 1.1 mrg
957 1.1 mrg /*
958 1.1 mrg * at this point nothing is locked. if we did an async I/O
959 1.1 mrg * it is remotely possible for the async i/o to complete and
960 1.1 mrg * the page "pp" be freed or what not before we get a chance
961 1.1 mrg * to relock the object. in order to detect this, we have
962 1.1 mrg * saved the version number of the page in "pp_version".
963 1.1 mrg */
964 1.1 mrg
965 1.1 mrg /* relock! */
966 1.1 mrg simple_lock(&uobj->vmobjlock);
967 1.1 mrg uvm_lock_pageq();
968 1.1 mrg
969 1.1 mrg /*
970 1.1 mrg * VM_PAGER_AGAIN: given the structure of this pager, this
971 1.1 mrg * can only happen when we are doing async I/O and can't
972 1.1 mrg * map the pages into kernel memory (pager_map) due to lack
973 1.1 mrg * of vm space. if this happens we drop back to sync I/O.
974 1.1 mrg */
975 1.1 mrg
976 1.1 mrg if (result == VM_PAGER_AGAIN) {
977 1.1 mrg /*
978 1.1 mrg * it is unlikely, but page could have been released while we
979 1.1 mrg * had the object lock dropped. we ignore this now and retry
980 1.1 mrg * the I/O. we will detect and handle the released page after
981 1.1 mrg * the syncio I/O completes.
982 1.1 mrg */
983 1.1 mrg #ifdef DIAGNOSTIC
984 1.1 mrg if (flags & PGO_SYNCIO)
985 1.1 mrg panic("uvn_flush: PGO_SYNCIO return 'try again' error (impossible)");
986 1.1 mrg #endif
987 1.1 mrg flags |= PGO_SYNCIO;
988 1.1 mrg goto ReTry;
989 1.1 mrg }
990 1.1 mrg
991 1.1 mrg /*
992 1.1 mrg * the cleaning operation is now done. finish up. note that
993 1.1 mrg * on error (!OK, !PEND) uvm_pager_put drops the cluster for us.
994 1.1 mrg * if success (OK, PEND) then uvm_pager_put returns the cluster
995 1.1 mrg * to us in ppsp/npages.
996 1.1 mrg */
997 1.1 mrg
998 1.1 mrg /*
999 1.1 mrg * for pending async i/o if we are not deactivating/freeing we can
1000 1.1 mrg * move on to the next page.
1001 1.1 mrg */
1002 1.1 mrg
1003 1.1 mrg if (result == VM_PAGER_PEND) {
1004 1.1 mrg
1005 1.1 mrg if ((flags & (PGO_DEACTIVATE|PGO_FREE)) == 0) {
1006 1.1 mrg /* no per-page ops: refresh ppnext and continue */
1007 1.1 mrg if (by_list) {
1008 1.1 mrg if (pp->version == pp_version)
1009 1.1 mrg ppnext = pp->listq.tqe_next;
1010 1.1 mrg else
1011 1.1 mrg ppnext = uobj->memq.tqh_first; /* reset */
1012 1.1 mrg } else {
1013 1.1 mrg if (curoff < stop)
1014 1.1 mrg ppnext = uvm_pagelookup(uobj, curoff);
1015 1.1 mrg }
1016 1.1 mrg continue;
1017 1.1 mrg }
1018 1.1 mrg
1019 1.1 mrg /* need to do anything here? */
1020 1.1 mrg }
1021 1.1 mrg
1022 1.1 mrg /*
1023 1.1 mrg * need to look at each page of the I/O operation. we defer
1024 1.1 mrg * processing "pp" until the last trip through this "for" loop
1025 1.1 mrg * so that we can load "ppnext" for the main loop after we
1026 1.1 mrg * play with the cluster pages [thus the "npages + 1" in the
1027 1.1 mrg * loop below].
1028 1.1 mrg */
1029 1.1 mrg
1030 1.1 mrg for (lcv = 0 ; lcv < npages + 1 ; lcv++) {
1031 1.1 mrg
1032 1.1 mrg /*
1033 1.1 mrg * handle ppnext for outside loop, and saving pp until the end.
1034 1.1 mrg */
1035 1.1 mrg if (lcv < npages) {
1036 1.1 mrg if (ppsp[lcv] == pp)
1037 1.1 mrg continue; /* skip pp until the end */
1038 1.1 mrg ptmp = ppsp[lcv];
1039 1.1 mrg } else {
1040 1.1 mrg ptmp = pp;
1041 1.1 mrg
1042 1.1 mrg /* set up next page for outer loop */
1043 1.1 mrg if (by_list) {
1044 1.1 mrg if (pp->version == pp_version)
1045 1.1 mrg ppnext = pp->listq.tqe_next;
1046 1.1 mrg else
1047 1.1 mrg ppnext = uobj->memq.tqh_first; /* reset */
1048 1.1 mrg } else {
1049 1.1 mrg if (curoff < stop)
1050 1.1 mrg ppnext = uvm_pagelookup(uobj, curoff);
1051 1.1 mrg }
1052 1.1 mrg }
1053 1.1 mrg
1054 1.1 mrg /*
1055 1.1 mrg * verify the page didn't get moved while obj was unlocked
1056 1.1 mrg */
1057 1.1 mrg if (result == VM_PAGER_PEND && ptmp->uobject != uobj)
1058 1.1 mrg continue;
1059 1.1 mrg
1060 1.1 mrg /*
1061 1.1 mrg * unbusy the page if I/O is done. note that for pending
1062 1.1 mrg * I/O it is possible that the I/O op finished before we
1063 1.1 mrg * relocked the object (in which case the page is no longer
1064 1.1 mrg * busy).
1065 1.1 mrg */
1066 1.1 mrg
1067 1.1 mrg if (result != VM_PAGER_PEND) {
1068 1.1 mrg if (ptmp->flags & PG_WANTED)
1069 1.1 mrg thread_wakeup(ptmp); /* still holding object lock */
1070 1.1 mrg ptmp->flags &= ~(PG_WANTED|PG_BUSY);
1071 1.1 mrg UVM_PAGE_OWN(ptmp, NULL);
1072 1.1 mrg if (ptmp->flags & PG_RELEASED) {
1073 1.1 mrg
1074 1.1 mrg uvm_unlock_pageq(); /* pgo_releasepg wants this */
1075 1.1 mrg if (!uvn_releasepg(ptmp, NULL)) {
1076 1.1 mrg return(TRUE);
1077 1.1 mrg }
1078 1.1 mrg uvm_lock_pageq(); /* relock */
1079 1.1 mrg continue; /* next page */
1080 1.1 mrg
1081 1.1 mrg } else {
1082 1.1 mrg ptmp->flags |= (PG_CLEAN|PG_CLEANCHK);
1083 1.1 mrg if ((flags & PGO_FREE) == 0)
1084 1.1 mrg pmap_clear_modify(PMAP_PGARG(ptmp));
1085 1.1 mrg }
1086 1.1 mrg }
1087 1.1 mrg
1088 1.1 mrg /*
1089 1.1 mrg * dispose of page
1090 1.1 mrg */
1091 1.1 mrg
1092 1.1 mrg if (flags & PGO_DEACTIVATE) {
1093 1.1 mrg if ((pp->pqflags & PQ_INACTIVE) == 0 && pp->wire_count == 0) {
1094 1.1 mrg pmap_page_protect(PMAP_PGARG(ptmp), VM_PROT_NONE);
1095 1.1 mrg uvm_pagedeactivate(ptmp);
1096 1.1 mrg }
1097 1.1 mrg
1098 1.1 mrg } else if (flags & PGO_FREE) {
1099 1.1 mrg if (result == VM_PAGER_PEND) {
1100 1.1 mrg if ((ptmp->flags & PG_BUSY) != 0)
1101 1.1 mrg ptmp->flags |= PG_RELEASED; /* signal for i/o done */
1102 1.1 mrg } else {
1103 1.1 mrg if (result != VM_PAGER_OK) {
1104 1.1 mrg printf("uvn_flush: obj=%p, offset=0x%lx. error during pageout.\n",
1105 1.1 mrg pp->uobject, pp->offset);
1106 1.1 mrg printf("uvn_flush: WARNING: changes to page may be lost!\n");
1107 1.1 mrg retval = FALSE;
1108 1.1 mrg }
1109 1.1 mrg pmap_page_protect(PMAP_PGARG(ptmp), VM_PROT_NONE);
1110 1.1 mrg uvm_pagefree(ptmp);
1111 1.1 mrg }
1112 1.1 mrg }
1113 1.1 mrg
1114 1.1 mrg } /* end of "lcv" for loop */
1115 1.1 mrg
1116 1.1 mrg } /* end of "pp" for loop */
1117 1.1 mrg
1118 1.1 mrg /*
1119 1.1 mrg * done with pagequeues: unlock
1120 1.1 mrg */
1121 1.1 mrg uvm_unlock_pageq();
1122 1.1 mrg
1123 1.1 mrg /*
1124 1.1 mrg * now wait for all I/O if required.
1125 1.1 mrg */
1126 1.1 mrg if (need_iosync) {
1127 1.1 mrg
1128 1.1 mrg UVMHIST_LOG(maphist," <<DOING IOSYNC>>",0,0,0,0);
1129 1.1 mrg while (uvn->u_nio != 0) {
1130 1.1 mrg uvn->u_flags |= UVM_VNODE_IOSYNC;
1131 1.1 mrg UVM_UNLOCK_AND_WAIT(&uvn->u_nio, &uvn->u_obj.vmobjlock,
1132 1.1 mrg FALSE, "uvn_flush",0);
1133 1.1 mrg simple_lock(&uvn->u_obj.vmobjlock);
1134 1.1 mrg }
1135 1.1 mrg if (uvn->u_flags & UVM_VNODE_IOSYNCWANTED)
1136 1.1 mrg wakeup(&uvn->u_flags);
1137 1.1 mrg uvn->u_flags &= ~(UVM_VNODE_IOSYNC|UVM_VNODE_IOSYNCWANTED);
1138 1.1 mrg }
1139 1.1 mrg
1140 1.1 mrg /* return, with object locked! */
1141 1.1 mrg UVMHIST_LOG(maphist,"<- done (retval=0x%x)",retval,0,0,0);
1142 1.1 mrg return(retval);
1143 1.1 mrg }
1144 1.1 mrg
1145 1.1 mrg /*
1146 1.1 mrg * uvn_cluster
1147 1.1 mrg *
1148 1.1 mrg * we are about to do I/O in an object at offset. this function is called
1149 1.1 mrg * to establish a range of offsets around "offset" in which we can cluster
1150 1.1 mrg * I/O.
1151 1.1 mrg *
1152 1.1 mrg * - currently doesn't matter if obj locked or not.
1153 1.1 mrg */
1154 1.1 mrg
1155 1.1 mrg static void uvn_cluster(uobj, offset, loffset, hoffset)
1156 1.1 mrg
1157 1.1 mrg struct uvm_object *uobj;
1158 1.1 mrg vm_offset_t offset;
1159 1.1 mrg vm_offset_t *loffset, *hoffset; /* OUT */
1160 1.1 mrg
1161 1.1 mrg {
1162 1.1 mrg struct uvm_vnode *uvn = (struct uvm_vnode *) uobj;
1163 1.1 mrg *loffset = offset;
1164 1.1 mrg
1165 1.1 mrg if (*loffset >= uvn->u_size)
1166 1.1 mrg panic("uvn_cluster: offset out of range");
1167 1.1 mrg
1168 1.1 mrg /*
1169 1.1 mrg * XXX: old pager claims we could use VOP_BMAP to get maxcontig value.
1170 1.1 mrg */
1171 1.1 mrg *hoffset = *loffset + MAXBSIZE;
1172 1.1 mrg if (*hoffset > round_page(uvn->u_size)) /* past end? */
1173 1.1 mrg *hoffset = round_page(uvn->u_size);
1174 1.1 mrg
1175 1.1 mrg return;
1176 1.1 mrg }
1177 1.1 mrg
1178 1.1 mrg /*
1179 1.1 mrg * uvn_put: flush page data to backing store.
1180 1.1 mrg *
1181 1.1 mrg * => prefer map unlocked (not required)
1182 1.1 mrg * => object must be locked! we will _unlock_ it before starting I/O.
1183 1.1 mrg * => flags: PGO_SYNCIO -- use sync. I/O
1184 1.1 mrg * => note: caller must set PG_CLEAN and pmap_clear_modify (if needed)
1185 1.1 mrg * => XXX: currently we use VOP_READ/VOP_WRITE which are only sync.
1186 1.1 mrg * [thus we never do async i/o! see iodone comment]
1187 1.1 mrg */
1188 1.1 mrg
1189 1.1 mrg static int uvn_put(uobj, pps, npages, flags)
1190 1.1 mrg
1191 1.1 mrg struct uvm_object *uobj;
1192 1.1 mrg struct vm_page **pps;
1193 1.1 mrg int npages, flags;
1194 1.1 mrg
1195 1.1 mrg {
1196 1.1 mrg int retval;
1197 1.1 mrg
1198 1.1 mrg /* note: object locked */
1199 1.1 mrg retval = uvn_io((struct uvm_vnode*)uobj, pps, npages, flags, UIO_WRITE);
1200 1.1 mrg /* note: object unlocked */
1201 1.1 mrg
1202 1.1 mrg return(retval);
1203 1.1 mrg }
1204 1.1 mrg
1205 1.1 mrg
1206 1.1 mrg /*
1207 1.1 mrg * uvn_get: get pages (synchronously) from backing store
1208 1.1 mrg *
1209 1.1 mrg * => prefer map unlocked (not required)
1210 1.1 mrg * => object must be locked! we will _unlock_ it before starting any I/O.
1211 1.1 mrg * => flags: PGO_ALLPAGES: get all of the pages
1212 1.1 mrg * PGO_LOCKED: fault data structures are locked
1213 1.1 mrg * => NOTE: offset is the offset of pps[0], _NOT_ pps[centeridx]
1214 1.1 mrg * => NOTE: caller must check for released pages!!
1215 1.1 mrg */
1216 1.1 mrg
1217 1.1 mrg static int uvn_get(uobj, offset, pps, npagesp, centeridx,
1218 1.1 mrg access_type, advice, flags)
1219 1.1 mrg
1220 1.1 mrg struct uvm_object *uobj;
1221 1.1 mrg vm_offset_t offset;
1222 1.1 mrg struct vm_page **pps; /* IN/OUT */
1223 1.1 mrg int *npagesp; /* IN (OUT if PGO_LOCKED) */
1224 1.1 mrg int centeridx, advice, flags;
1225 1.1 mrg vm_prot_t access_type;
1226 1.1 mrg
1227 1.1 mrg {
1228 1.1 mrg vm_offset_t current_offset;
1229 1.1 mrg struct vm_page *ptmp;
1230 1.1 mrg int lcv, result, gotpages;
1231 1.1 mrg boolean_t done;
1232 1.1 mrg UVMHIST_FUNC("uvn_get"); UVMHIST_CALLED(maphist);
1233 1.1 mrg UVMHIST_LOG(maphist, "flags=%d", flags,0,0,0);
1234 1.1 mrg
1235 1.1 mrg /*
1236 1.1 mrg * step 1: handled the case where fault data structures are locked.
1237 1.1 mrg */
1238 1.1 mrg
1239 1.1 mrg if (flags & PGO_LOCKED) {
1240 1.1 mrg
1241 1.1 mrg /*
1242 1.1 mrg * gotpages is the current number of pages we've gotten (which
1243 1.1 mrg * we pass back up to caller via *npagesp.
1244 1.1 mrg */
1245 1.1 mrg
1246 1.1 mrg gotpages = 0;
1247 1.1 mrg
1248 1.1 mrg /*
1249 1.1 mrg * step 1a: get pages that are already resident. only do this
1250 1.1 mrg * if the data structures are locked (i.e. the first time through).
1251 1.1 mrg */
1252 1.1 mrg
1253 1.1 mrg done = TRUE; /* be optimistic */
1254 1.1 mrg
1255 1.1 mrg for (lcv = 0, current_offset = offset ;
1256 1.1 mrg lcv < *npagesp ; lcv++, current_offset += PAGE_SIZE) {
1257 1.1 mrg
1258 1.1 mrg /* do we care about this page? if not, skip it */
1259 1.1 mrg if (pps[lcv] == PGO_DONTCARE)
1260 1.1 mrg continue;
1261 1.1 mrg
1262 1.1 mrg /* lookup page */
1263 1.1 mrg ptmp = uvm_pagelookup(uobj, current_offset);
1264 1.1 mrg
1265 1.1 mrg /* to be useful must get a non-busy, non-released page */
1266 1.1 mrg if (ptmp == NULL || (ptmp->flags & (PG_BUSY|PG_RELEASED)) != 0) {
1267 1.1 mrg if (lcv == centeridx || (flags & PGO_ALLPAGES) != 0)
1268 1.1 mrg done = FALSE; /* need to do a wait or I/O! */
1269 1.1 mrg continue;
1270 1.1 mrg }
1271 1.1 mrg
1272 1.1 mrg /* useful page: busy/lock it and plug it in our result array */
1273 1.1 mrg ptmp->flags |= PG_BUSY; /* loan up to caller */
1274 1.1 mrg UVM_PAGE_OWN(ptmp, "uvn_get1");
1275 1.1 mrg pps[lcv] = ptmp;
1276 1.1 mrg gotpages++;
1277 1.1 mrg
1278 1.1 mrg } /* "for" lcv loop */
1279 1.1 mrg
1280 1.1 mrg /*
1281 1.1 mrg * XXX: given the "advice", should we consider async read-ahead?
1282 1.1 mrg * XXX: fault current does deactive of pages behind us. is
1283 1.1 mrg * this good (other callers might now).
1284 1.1 mrg */
1285 1.1 mrg /*
1286 1.1 mrg * XXX: read-ahead currently handled by buffer cache (bread) level.
1287 1.1 mrg * XXX: no async i/o available.
1288 1.1 mrg * XXX: so we don't do anything now.
1289 1.1 mrg */
1290 1.1 mrg
1291 1.1 mrg /*
1292 1.1 mrg * step 1c: now we've either done everything needed or we to unlock
1293 1.1 mrg * and do some waiting or I/O.
1294 1.1 mrg */
1295 1.1 mrg
1296 1.1 mrg *npagesp = gotpages; /* let caller know */
1297 1.1 mrg if (done)
1298 1.1 mrg return(VM_PAGER_OK); /* bingo! */
1299 1.1 mrg else
1300 1.1 mrg return(VM_PAGER_UNLOCK); /* EEK! Need to unlock and I/O */
1301 1.1 mrg }
1302 1.1 mrg
1303 1.1 mrg /*
1304 1.1 mrg * step 2: get non-resident or busy pages.
1305 1.1 mrg * object is locked. data structures are unlocked.
1306 1.1 mrg *
1307 1.1 mrg * XXX: because we can't do async I/O at this level we get things
1308 1.1 mrg * page at a time (otherwise we'd chunk). the VOP_READ() will do
1309 1.1 mrg * async-read-ahead for us at a lower level.
1310 1.1 mrg */
1311 1.1 mrg
1312 1.1 mrg for (lcv = 0, current_offset = offset ;
1313 1.1 mrg lcv < *npagesp ; lcv++, current_offset += PAGE_SIZE) {
1314 1.1 mrg
1315 1.1 mrg /* skip over pages we've already gotten or don't want */
1316 1.1 mrg /* skip over pages we don't _have_ to get */
1317 1.1 mrg if (pps[lcv] != NULL ||
1318 1.1 mrg (lcv != centeridx && (flags & PGO_ALLPAGES) == 0))
1319 1.1 mrg continue;
1320 1.1 mrg
1321 1.1 mrg /*
1322 1.1 mrg * we have yet to locate the current page (pps[lcv]). we first
1323 1.1 mrg * look for a page that is already at the current offset. if we
1324 1.1 mrg * fine a page, we check to see if it is busy or released. if that
1325 1.1 mrg * is the case, then we sleep on the page until it is no longer busy
1326 1.1 mrg * or released and repeat the lookup. if the page we found is
1327 1.1 mrg * neither busy nor released, then we busy it (so we own it) and
1328 1.1 mrg * plug it into pps[lcv]. this breaks the following while loop
1329 1.1 mrg * and indicates we are ready to move on to the next page in the
1330 1.1 mrg * "lcv" loop above.
1331 1.1 mrg *
1332 1.1 mrg * if we exit the while loop with pps[lcv] still set to NULL, then
1333 1.1 mrg * it means that we allocated a new busy/fake/clean page ptmp in the
1334 1.1 mrg * object and we need to do I/O to fill in the data.
1335 1.1 mrg */
1336 1.1 mrg
1337 1.1 mrg while (pps[lcv] == NULL) { /* top of "pps" while loop */
1338 1.1 mrg
1339 1.1 mrg /* look for a current page */
1340 1.1 mrg ptmp = uvm_pagelookup(uobj, current_offset);
1341 1.1 mrg
1342 1.1 mrg /* nope? allocate one now (if we can) */
1343 1.1 mrg if (ptmp == NULL) {
1344 1.1 mrg
1345 1.1 mrg ptmp = uvm_pagealloc(uobj, current_offset, NULL); /* alloc */
1346 1.1 mrg
1347 1.1 mrg /* out of RAM? */
1348 1.1 mrg if (ptmp == NULL) {
1349 1.1 mrg simple_unlock(&uobj->vmobjlock);
1350 1.1 mrg uvm_wait("uvn_getpage");
1351 1.1 mrg simple_lock(&uobj->vmobjlock);
1352 1.1 mrg continue; /* goto top of pps while loop */
1353 1.1 mrg }
1354 1.1 mrg
1355 1.1 mrg /*
1356 1.1 mrg * got new page ready for I/O. break pps while loop. pps[lcv] is
1357 1.1 mrg * still NULL.
1358 1.1 mrg */
1359 1.1 mrg break;
1360 1.1 mrg }
1361 1.1 mrg
1362 1.1 mrg /* page is there, see if we need to wait on it */
1363 1.1 mrg if ((ptmp->flags & (PG_BUSY|PG_RELEASED)) != 0) {
1364 1.1 mrg ptmp->flags |= PG_WANTED;
1365 1.1 mrg UVM_UNLOCK_AND_WAIT(ptmp,&uobj->vmobjlock,0,"uvn_get",0);
1366 1.1 mrg simple_lock(&uobj->vmobjlock);
1367 1.1 mrg continue; /* goto top of pps while loop */
1368 1.1 mrg }
1369 1.1 mrg
1370 1.1 mrg /*
1371 1.1 mrg * if we get here then the page has become resident and unbusy
1372 1.1 mrg * between steps 1 and 2. we busy it now (so we own it) and set
1373 1.1 mrg * pps[lcv] (so that we exit the while loop).
1374 1.1 mrg */
1375 1.1 mrg ptmp->flags |= PG_BUSY;
1376 1.1 mrg UVM_PAGE_OWN(ptmp, "uvn_get2");
1377 1.1 mrg pps[lcv] = ptmp;
1378 1.1 mrg }
1379 1.1 mrg
1380 1.1 mrg /*
1381 1.1 mrg * if we own the a valid page at the correct offset, pps[lcv] will
1382 1.1 mrg * point to it. nothing more to do except go to the next page.
1383 1.1 mrg */
1384 1.1 mrg
1385 1.1 mrg if (pps[lcv])
1386 1.1 mrg continue; /* next lcv */
1387 1.1 mrg
1388 1.1 mrg /*
1389 1.1 mrg * we have a "fake/busy/clean" page that we just allocated. do
1390 1.1 mrg * I/O to fill it with valid data. note that object must be
1391 1.1 mrg * locked going into uvn_io, but will be unlocked afterwards.
1392 1.1 mrg */
1393 1.1 mrg
1394 1.1 mrg result = uvn_io((struct uvm_vnode *) uobj, &ptmp, 1, PGO_SYNCIO, UIO_READ);
1395 1.1 mrg
1396 1.1 mrg /*
1397 1.1 mrg * I/O done. object is unlocked (by uvn_io). because we used
1398 1.1 mrg * syncio the result can not be PEND or AGAIN. we must relock
1399 1.1 mrg * and check for errors.
1400 1.1 mrg */
1401 1.1 mrg
1402 1.1 mrg /* lock object. check for errors. */
1403 1.1 mrg simple_lock(&uobj->vmobjlock);
1404 1.1 mrg if (result != VM_PAGER_OK) {
1405 1.1 mrg if (ptmp->flags & PG_WANTED)
1406 1.1 mrg thread_wakeup(ptmp); /* object lock still held */
1407 1.1 mrg ptmp->flags &= ~(PG_WANTED|PG_BUSY);
1408 1.1 mrg UVM_PAGE_OWN(ptmp, NULL);
1409 1.1 mrg uvm_lock_pageq();
1410 1.1 mrg uvm_pagefree(ptmp);
1411 1.1 mrg uvm_unlock_pageq();
1412 1.1 mrg simple_unlock(&uobj->vmobjlock);
1413 1.1 mrg return(result);
1414 1.1 mrg }
1415 1.1 mrg
1416 1.1 mrg /*
1417 1.1 mrg * we got the page! clear the fake flag (indicates valid data now
1418 1.1 mrg * in page) and plug into our result array. note that page is still
1419 1.1 mrg * busy.
1420 1.1 mrg *
1421 1.1 mrg * it is the callers job to:
1422 1.1 mrg * => check if the page is released
1423 1.1 mrg * => unbusy the page
1424 1.1 mrg * => activate the page
1425 1.1 mrg */
1426 1.1 mrg
1427 1.1 mrg ptmp->flags &= ~PG_FAKE; /* data is valid ... */
1428 1.1 mrg pmap_clear_modify(PMAP_PGARG(ptmp)); /* ... and clean */
1429 1.1 mrg pps[lcv] = ptmp;
1430 1.1 mrg
1431 1.1 mrg } /* lcv loop */
1432 1.1 mrg
1433 1.1 mrg /*
1434 1.1 mrg * finally, unlock object and return.
1435 1.1 mrg */
1436 1.1 mrg
1437 1.1 mrg simple_unlock(&uobj->vmobjlock);
1438 1.1 mrg return(VM_PAGER_OK);
1439 1.1 mrg }
1440 1.1 mrg
1441 1.1 mrg /*
1442 1.1 mrg * uvn_asyncget: start async I/O to bring pages into ram
1443 1.1 mrg *
1444 1.1 mrg * => caller must lock object(???XXX: see if this is best)
1445 1.1 mrg * => could be called from uvn_get or a madvise() fault-ahead.
1446 1.1 mrg * => if it fails, it doesn't matter.
1447 1.1 mrg */
1448 1.1 mrg
1449 1.1 mrg static int uvn_asyncget(uobj, offset, npages)
1450 1.1 mrg
1451 1.1 mrg struct uvm_object *uobj;
1452 1.1 mrg vm_offset_t offset;
1453 1.1 mrg int npages;
1454 1.1 mrg
1455 1.1 mrg {
1456 1.1 mrg /*
1457 1.1 mrg * XXXCDC: we can't do async I/O yet
1458 1.1 mrg */
1459 1.1 mrg printf("uvn_asyncget called\n");
1460 1.1 mrg return(KERN_SUCCESS);
1461 1.1 mrg }
1462 1.1 mrg
1463 1.1 mrg /*
1464 1.1 mrg * uvn_io: do I/O to a vnode
1465 1.1 mrg *
1466 1.1 mrg * => prefer map unlocked (not required)
1467 1.1 mrg * => object must be locked! we will _unlock_ it before starting I/O.
1468 1.1 mrg * => flags: PGO_SYNCIO -- use sync. I/O
1469 1.1 mrg * => XXX: currently we use VOP_READ/VOP_WRITE which are only sync.
1470 1.1 mrg * [thus we never do async i/o! see iodone comment]
1471 1.1 mrg */
1472 1.1 mrg
1473 1.1 mrg static int uvn_io(uvn, pps, npages, flags, rw)
1474 1.1 mrg
1475 1.1 mrg struct uvm_vnode *uvn;
1476 1.1 mrg vm_page_t *pps;
1477 1.1 mrg int npages, flags, rw;
1478 1.1 mrg
1479 1.1 mrg {
1480 1.1 mrg struct vnode *vn;
1481 1.1 mrg struct uio uio;
1482 1.1 mrg struct iovec iov;
1483 1.1 mrg vm_offset_t kva, file_offset;
1484 1.1 mrg int waitf, result, got, wanted;
1485 1.1 mrg UVMHIST_FUNC("uvn_io"); UVMHIST_CALLED(maphist);
1486 1.1 mrg
1487 1.1 mrg UVMHIST_LOG(maphist, "rw=%d", rw,0,0,0);
1488 1.1 mrg
1489 1.1 mrg /*
1490 1.1 mrg * init values
1491 1.1 mrg */
1492 1.1 mrg
1493 1.1 mrg waitf = (flags & PGO_SYNCIO) ? M_WAITOK : M_NOWAIT;
1494 1.1 mrg vn = (struct vnode *) uvn;
1495 1.1 mrg file_offset = pps[0]->offset;
1496 1.1 mrg
1497 1.1 mrg /*
1498 1.1 mrg * check for sync'ing I/O.
1499 1.1 mrg */
1500 1.1 mrg
1501 1.1 mrg while (uvn->u_flags & UVM_VNODE_IOSYNC) {
1502 1.1 mrg if (waitf == M_NOWAIT) {
1503 1.1 mrg simple_unlock(&uvn->u_obj.vmobjlock);
1504 1.1 mrg UVMHIST_LOG(maphist,"<- try again (iosync)",0,0,0,0);
1505 1.1 mrg return(VM_PAGER_AGAIN);
1506 1.1 mrg }
1507 1.1 mrg uvn->u_flags |= UVM_VNODE_IOSYNCWANTED;
1508 1.1 mrg UVM_UNLOCK_AND_WAIT(&uvn->u_flags, &uvn->u_obj.vmobjlock,
1509 1.1 mrg FALSE, "uvn_iosync",0);
1510 1.1 mrg simple_lock(&uvn->u_obj.vmobjlock);
1511 1.1 mrg }
1512 1.1 mrg
1513 1.1 mrg /*
1514 1.1 mrg * check size
1515 1.1 mrg */
1516 1.1 mrg
1517 1.1 mrg if (file_offset >= uvn->u_size) {
1518 1.1 mrg simple_unlock(&uvn->u_obj.vmobjlock);
1519 1.1 mrg UVMHIST_LOG(maphist,"<- BAD (size check)",0,0,0,0);
1520 1.1 mrg #ifdef DIAGNOSTIC
1521 1.1 mrg printf("uvn_io: note: size check fired\n");
1522 1.1 mrg #endif
1523 1.1 mrg return(VM_PAGER_BAD);
1524 1.1 mrg }
1525 1.1 mrg
1526 1.1 mrg /*
1527 1.1 mrg * first try and map the pages in (without waiting)
1528 1.1 mrg */
1529 1.1 mrg
1530 1.1 mrg kva = uvm_pagermapin(pps, npages, NULL, M_NOWAIT);
1531 1.1 mrg if (kva == NULL && waitf == M_NOWAIT) {
1532 1.1 mrg simple_unlock(&uvn->u_obj.vmobjlock);
1533 1.1 mrg UVMHIST_LOG(maphist,"<- mapin failed (try again)",0,0,0,0);
1534 1.1 mrg return(VM_PAGER_AGAIN);
1535 1.1 mrg }
1536 1.1 mrg
1537 1.1 mrg /*
1538 1.1 mrg * ok, now bump u_nio up. at this point we are done with uvn
1539 1.1 mrg * and can unlock it. if we still don't have a kva, try again
1540 1.1 mrg * (this time with sleep ok).
1541 1.1 mrg */
1542 1.1 mrg
1543 1.1 mrg uvn->u_nio++; /* we have an I/O in progress! */
1544 1.1 mrg simple_unlock(&uvn->u_obj.vmobjlock);
1545 1.1 mrg /* NOTE: object now unlocked */
1546 1.1 mrg if (kva == NULL) {
1547 1.1 mrg kva = uvm_pagermapin(pps, npages, NULL, M_WAITOK);
1548 1.1 mrg }
1549 1.1 mrg
1550 1.1 mrg /*
1551 1.1 mrg * ok, mapped in. our pages are PG_BUSY so they are not going to
1552 1.1 mrg * get touched (so we can look at "offset" without having to lock
1553 1.1 mrg * the object). set up for I/O.
1554 1.1 mrg */
1555 1.1 mrg
1556 1.1 mrg /*
1557 1.1 mrg * fill out uio/iov
1558 1.1 mrg */
1559 1.1 mrg
1560 1.1 mrg iov.iov_base = (caddr_t) kva;
1561 1.1 mrg wanted = npages * PAGE_SIZE;
1562 1.1 mrg if (file_offset + wanted > uvn->u_size)
1563 1.1 mrg wanted = uvn->u_size - file_offset; /* XXX: needed? */
1564 1.1 mrg iov.iov_len = wanted;
1565 1.1 mrg uio.uio_iov = &iov;
1566 1.1 mrg uio.uio_iovcnt = 1;
1567 1.1 mrg uio.uio_offset = file_offset;
1568 1.1 mrg uio.uio_segflg = UIO_SYSSPACE;
1569 1.1 mrg uio.uio_rw = rw;
1570 1.1 mrg uio.uio_resid = wanted;
1571 1.1 mrg uio.uio_procp = NULL;
1572 1.1 mrg
1573 1.1 mrg /*
1574 1.1 mrg * do the I/O! (XXX: curproc?)
1575 1.1 mrg */
1576 1.1 mrg
1577 1.1 mrg UVMHIST_LOG(maphist, "calling VOP",0,0,0,0);
1578 1.1 mrg
1579 1.1 mrg if ((uvn->u_flags & UVM_VNODE_VNISLOCKED) == 0)
1580 1.1 mrg VOP_LOCK(vn);
1581 1.1 mrg /* NOTE: vnode now locked! */
1582 1.1 mrg
1583 1.1 mrg if (rw == UIO_READ)
1584 1.1 mrg result = VOP_READ(vn, &uio, 0, curproc->p_ucred);
1585 1.1 mrg else
1586 1.1 mrg result = VOP_WRITE(vn, &uio, 0, curproc->p_ucred);
1587 1.1 mrg
1588 1.1 mrg if ((uvn->u_flags & UVM_VNODE_VNISLOCKED) == 0)
1589 1.1 mrg VOP_UNLOCK(vn);
1590 1.1 mrg /* NOTE: vnode now unlocked (unless vnislocked) */
1591 1.1 mrg
1592 1.1 mrg UVMHIST_LOG(maphist, "done calling VOP",0,0,0,0);
1593 1.1 mrg
1594 1.1 mrg /*
1595 1.1 mrg * result == unix style errno (0 == OK!)
1596 1.1 mrg *
1597 1.1 mrg * zero out rest of buffer (if needed)
1598 1.1 mrg */
1599 1.1 mrg
1600 1.1 mrg if (result == 0) {
1601 1.1 mrg got = wanted - uio.uio_resid;
1602 1.1 mrg
1603 1.1 mrg if (wanted && got == 0) {
1604 1.1 mrg result = EIO; /* XXX: error? */
1605 1.1 mrg } else if (got < PAGE_SIZE * npages && rw == UIO_READ) {
1606 1.1 mrg bzero((void *) (kva + got), (PAGE_SIZE * npages) - got);
1607 1.1 mrg }
1608 1.1 mrg }
1609 1.1 mrg
1610 1.1 mrg /*
1611 1.1 mrg * now remove pager mapping
1612 1.1 mrg */
1613 1.1 mrg uvm_pagermapout(kva, npages);
1614 1.1 mrg
1615 1.1 mrg /*
1616 1.1 mrg * now clean up the object (i.e. drop I/O count)
1617 1.1 mrg */
1618 1.1 mrg
1619 1.1 mrg simple_lock(&uvn->u_obj.vmobjlock);
1620 1.1 mrg /* NOTE: object now locked! */
1621 1.1 mrg
1622 1.1 mrg uvn->u_nio--; /* I/O DONE! */
1623 1.1 mrg if ((uvn->u_flags & UVM_VNODE_IOSYNC) != 0 && uvn->u_nio == 0) {
1624 1.1 mrg wakeup(&uvn->u_nio);
1625 1.1 mrg }
1626 1.1 mrg simple_unlock(&uvn->u_obj.vmobjlock);
1627 1.1 mrg /* NOTE: object now unlocked! */
1628 1.1 mrg
1629 1.1 mrg /*
1630 1.1 mrg * done!
1631 1.1 mrg */
1632 1.1 mrg
1633 1.1 mrg UVMHIST_LOG(maphist, "<- done (result %d)", result,0,0,0);
1634 1.1 mrg if (result == 0)
1635 1.1 mrg return(VM_PAGER_OK);
1636 1.1 mrg else
1637 1.1 mrg return(VM_PAGER_ERROR);
1638 1.1 mrg }
1639 1.1 mrg
1640 1.1 mrg /*
1641 1.1 mrg * uvm_vnp_uncache: disable "persisting" in a vnode... when last reference
1642 1.1 mrg * is gone we will kill the object (flushing dirty pages back to the vnode
1643 1.1 mrg * if needed).
1644 1.1 mrg *
1645 1.1 mrg * => returns TRUE if there was no uvm_object attached or if there was
1646 1.1 mrg * one and we killed it [i.e. if there is no active uvn]
1647 1.1 mrg * => called with the vnode VOP_LOCK'd [we will unlock it for I/O, if
1648 1.1 mrg * needed]
1649 1.1 mrg *
1650 1.1 mrg * => XXX: given that we now kill uvn's when a vnode is recycled (without
1651 1.1 mrg * having to hold a reference on the vnode) and given a working
1652 1.1 mrg * uvm_vnp_sync(), how does that effect the need for this function?
1653 1.1 mrg * [XXXCDC: seems like it can die?]
1654 1.1 mrg *
1655 1.1 mrg * => XXX: this function should DIE once we merge the VM and buffer
1656 1.1 mrg * cache.
1657 1.1 mrg *
1658 1.1 mrg * research shows that this is called in the following places:
1659 1.1 mrg * ext2fs_truncate, ffs_truncate, detrunc[msdosfs]: called when vnode
1660 1.1 mrg * changes sizes
1661 1.1 mrg * ext2fs_write, WRITE [ufs_readwrite], msdosfs_write: called when we
1662 1.1 mrg * are written to
1663 1.1 mrg * ex2fs_chmod, ufs_chmod: called if VTEXT vnode and the sticky bit
1664 1.1 mrg * is off
1665 1.1 mrg * ffs_realloccg: when we can't extend the current block and have
1666 1.1 mrg * to allocate a new one we call this [XXX: why?]
1667 1.1 mrg * nfsrv_rename, rename_files: called when the target filename is there
1668 1.1 mrg * and we want to remove it
1669 1.1 mrg * nfsrv_remove, sys_unlink: called on file we are removing
1670 1.1 mrg * nfsrv_access: if VTEXT and we want WRITE access and we don't uncache
1671 1.1 mrg * then return "text busy"
1672 1.1 mrg * nfs_open: seems to uncache any file opened with nfs
1673 1.1 mrg * vn_writechk: if VTEXT vnode and can't uncache return "text busy"
1674 1.1 mrg */
1675 1.1 mrg
1676 1.1 mrg boolean_t uvm_vnp_uncache(vp)
1677 1.1 mrg
1678 1.1 mrg struct vnode *vp;
1679 1.1 mrg
1680 1.1 mrg {
1681 1.1 mrg struct uvm_vnode *uvn = &vp->v_uvm;
1682 1.1 mrg
1683 1.1 mrg /*
1684 1.1 mrg * lock uvn part of the vnode and check to see if we need to do anything
1685 1.1 mrg */
1686 1.1 mrg
1687 1.1 mrg simple_lock(&uvn->u_obj.vmobjlock);
1688 1.1 mrg if ((uvn->u_flags & UVM_VNODE_VALID) == 0 ||
1689 1.1 mrg (uvn->u_flags & UVM_VNODE_BLOCKED) != 0) {
1690 1.1 mrg simple_unlock(&uvn->u_obj.vmobjlock);
1691 1.1 mrg return(TRUE);
1692 1.1 mrg }
1693 1.1 mrg
1694 1.1 mrg /*
1695 1.1 mrg * we have a valid, non-blocked uvn. clear persist flag.
1696 1.1 mrg * if uvn is currently active we can return now.
1697 1.1 mrg */
1698 1.1 mrg
1699 1.1 mrg uvn->u_flags &= ~UVM_VNODE_CANPERSIST;
1700 1.1 mrg if (uvn->u_obj.uo_refs) {
1701 1.1 mrg simple_unlock(&uvn->u_obj.vmobjlock);
1702 1.1 mrg return(FALSE);
1703 1.1 mrg }
1704 1.1 mrg
1705 1.1 mrg /*
1706 1.1 mrg * uvn is currently persisting! we have to gain a reference to
1707 1.1 mrg * it so that we can call uvn_detach to kill the uvn.
1708 1.1 mrg */
1709 1.1 mrg
1710 1.1 mrg VREF(vp); /* seems ok, even with VOP_LOCK */
1711 1.1 mrg uvn->u_obj.uo_refs++; /* value is now 1 */
1712 1.1 mrg simple_unlock(&uvn->u_obj.vmobjlock);
1713 1.1 mrg
1714 1.1 mrg
1715 1.1 mrg #ifdef DEBUG
1716 1.1 mrg /*
1717 1.1 mrg * carry over sanity check from old vnode pager: the vnode should
1718 1.1 mrg * be VOP_LOCK'd, and we confirm it here.
1719 1.1 mrg */
1720 1.1 mrg if (!VOP_ISLOCKED(vp)) {
1721 1.1 mrg boolean_t is_ok_anyway = FALSE;
1722 1.1 mrg #ifdef NFS
1723 1.1 mrg extern int (**nfsv2_vnodeop_p) __P((void *));
1724 1.1 mrg extern int (**spec_nfsv2nodeop_p) __P((void *));
1725 1.1 mrg #ifdef FIFO
1726 1.1 mrg extern int (**fifo_nfsv2nodeop_p) __P((void *));
1727 1.1 mrg #endif /* FIFO */
1728 1.1 mrg
1729 1.1 mrg /* vnode is NOT VOP_LOCKed: some vnode types _never_ lock */
1730 1.1 mrg if (vp->v_op == nfsv2_vnodeop_p || vp->v_op == spec_nfsv2nodeop_p) {
1731 1.1 mrg is_ok_anyway = TRUE;
1732 1.1 mrg }
1733 1.1 mrg #ifdef FIFO
1734 1.1 mrg if (vp->v_op == fifo_nfsv2nodeop_p) {
1735 1.1 mrg is_ok_anyway = TRUE;
1736 1.1 mrg }
1737 1.1 mrg #endif /* FIFO */
1738 1.1 mrg #endif /* NFS */
1739 1.1 mrg if (!is_ok_anyway)
1740 1.1 mrg panic("uvm_vnp_uncache: vnode not locked!");
1741 1.1 mrg }
1742 1.1 mrg #endif /* DEBUG */
1743 1.1 mrg
1744 1.1 mrg /*
1745 1.1 mrg * now drop our reference to the vnode. if we have the sole
1746 1.1 mrg * reference to the vnode then this will cause it to die [as we
1747 1.1 mrg * just cleared the persist flag]. we have to unlock the vnode
1748 1.1 mrg * while we are doing this as it may trigger I/O.
1749 1.1 mrg *
1750 1.1 mrg * XXX: it might be possible for uvn to get reclaimed while we are
1751 1.1 mrg * unlocked causing us to return TRUE when we should not. we ignore
1752 1.1 mrg * this as a false-positive return value doesn't hurt us.
1753 1.1 mrg */
1754 1.1 mrg VOP_UNLOCK(vp);
1755 1.1 mrg uvn_detach(&uvn->u_obj);
1756 1.1 mrg VOP_LOCK(vp);
1757 1.1 mrg
1758 1.1 mrg /*
1759 1.1 mrg * and return...
1760 1.1 mrg */
1761 1.1 mrg
1762 1.1 mrg return(TRUE);
1763 1.1 mrg }
1764 1.1 mrg
1765 1.1 mrg /*
1766 1.1 mrg * uvm_vnp_setsize: grow or shrink a vnode uvn
1767 1.1 mrg *
1768 1.1 mrg * grow => just update size value
1769 1.1 mrg * shrink => toss un-needed pages
1770 1.1 mrg *
1771 1.1 mrg * => we assume that the caller has a reference of some sort to the
1772 1.1 mrg * vnode in question so that it will not be yanked out from under
1773 1.1 mrg * us.
1774 1.1 mrg *
1775 1.1 mrg * called from:
1776 1.1 mrg * => truncate fns (ext2fs_truncate, ffs_truncate, detrunc[msdos])
1777 1.1 mrg * => "write" fns (ext2fs_write, WRITE [ufs/ufs], msdosfs_write, nfs_write)
1778 1.1 mrg * => ffs_balloc [XXX: why? doesn't WRITE handle?]
1779 1.1 mrg * => NFS: nfs_loadattrcache, nfs_getattrcache, nfs_setattr
1780 1.1 mrg * => union fs: union_newsize
1781 1.1 mrg */
1782 1.1 mrg
1783 1.1 mrg void uvm_vnp_setsize(vp, newsize)
1784 1.1 mrg
1785 1.1 mrg struct vnode *vp;
1786 1.1 mrg u_quad_t newsize;
1787 1.1 mrg
1788 1.1 mrg {
1789 1.1 mrg struct uvm_vnode *uvn = &vp->v_uvm;
1790 1.1 mrg
1791 1.1 mrg /*
1792 1.1 mrg * lock uvn and check for valid object, and if valid: do it!
1793 1.1 mrg */
1794 1.1 mrg simple_lock(&uvn->u_obj.vmobjlock);
1795 1.1 mrg if (uvn->u_flags & UVM_VNODE_VALID) {
1796 1.1 mrg
1797 1.1 mrg /*
1798 1.1 mrg * make sure that the newsize fits within a vm_offset_t
1799 1.1 mrg * XXX: need to revise addressing data types
1800 1.1 mrg */
1801 1.1 mrg
1802 1.1 mrg if (newsize > (vm_offset_t) -PAGE_SIZE) {
1803 1.1 mrg #ifdef DEBUG
1804 1.1 mrg printf("uvm_vnp_setsize: vn %p size truncated %qx->%lx\n", vp, newsize,
1805 1.1 mrg (vm_offset_t) -PAGE_SIZE);
1806 1.1 mrg #endif
1807 1.1 mrg newsize = (vm_offset_t) -PAGE_SIZE;
1808 1.1 mrg }
1809 1.1 mrg
1810 1.1 mrg /*
1811 1.1 mrg * now check if the size has changed: if we shrink we had better
1812 1.1 mrg * toss some pages...
1813 1.1 mrg */
1814 1.1 mrg
1815 1.1 mrg if (uvn->u_size > newsize) {
1816 1.1 mrg (void) uvn_flush(&uvn->u_obj, (vm_offset_t) newsize,
1817 1.1 mrg uvn->u_size, PGO_FREE);
1818 1.1 mrg }
1819 1.1 mrg uvn->u_size = (vm_offset_t)newsize;
1820 1.1 mrg }
1821 1.1 mrg simple_unlock(&uvn->u_obj.vmobjlock);
1822 1.1 mrg
1823 1.1 mrg /*
1824 1.1 mrg * done
1825 1.1 mrg */
1826 1.1 mrg return;
1827 1.1 mrg }
1828 1.1 mrg
1829 1.1 mrg /*
1830 1.1 mrg * uvm_vnp_sync: flush all dirty VM pages back to their backing vnodes.
1831 1.1 mrg *
1832 1.1 mrg * => called from sys_sync with no VM structures locked
1833 1.1 mrg * => only one process can do a sync at a time (because the uvn
1834 1.1 mrg * structure only has one queue for sync'ing). we ensure this
1835 1.1 mrg * by holding the uvn_sync_lock while the sync is in progress.
1836 1.1 mrg * other processes attempting a sync will sleep on this lock
1837 1.1 mrg * until we are done.
1838 1.1 mrg */
1839 1.1 mrg
1840 1.1 mrg void uvm_vnp_sync(mp)
1841 1.1 mrg
1842 1.1 mrg struct mount *mp;
1843 1.1 mrg
1844 1.1 mrg {
1845 1.1 mrg struct uvm_vnode *uvn;
1846 1.1 mrg struct vnode *vp;
1847 1.1 mrg boolean_t got_lock;
1848 1.1 mrg
1849 1.1 mrg /*
1850 1.1 mrg * step 1: ensure we are only ones using the uvn_sync_q by locking
1851 1.1 mrg * our lock...
1852 1.1 mrg */
1853 1.1 mrg lockmgr(&uvn_sync_lock, LK_EXCLUSIVE, (void *)0, curproc);
1854 1.1 mrg
1855 1.1 mrg /*
1856 1.1 mrg * step 2: build up a simpleq of uvns of interest based on the
1857 1.1 mrg * write list. we gain a reference to uvns of interest. must
1858 1.1 mrg * be careful about locking uvn's since we will be holding uvn_wl_lock
1859 1.1 mrg * in the body of the loop.
1860 1.1 mrg */
1861 1.1 mrg SIMPLEQ_INIT(&uvn_sync_q);
1862 1.1 mrg simple_lock(&uvn_wl_lock);
1863 1.1 mrg for (uvn = uvn_wlist.lh_first ; uvn != NULL ; uvn = uvn->u_wlist.le_next) {
1864 1.1 mrg
1865 1.1 mrg vp = (struct vnode *) uvn;
1866 1.1 mrg if (mp && vp->v_mount != mp)
1867 1.1 mrg continue;
1868 1.1 mrg
1869 1.1 mrg /* attempt to gain reference */
1870 1.1 mrg while ((got_lock = simple_lock_try(&uvn->u_obj.vmobjlock)) == FALSE &&
1871 1.1 mrg (uvn->u_flags & UVM_VNODE_BLOCKED) == 0)
1872 1.1 mrg /*spin*/;
1873 1.1 mrg
1874 1.1 mrg /*
1875 1.1 mrg * we will exit the loop if we were unable to get the lock and we
1876 1.1 mrg * detected that the vnode was "blocked" ... if it is blocked then
1877 1.1 mrg * it must be a dying vnode. since dying vnodes are in the process
1878 1.1 mrg * of being flushed out we can safely skip it.
1879 1.1 mrg *
1880 1.1 mrg * note that uvn must already be valid because we found it on the
1881 1.1 mrg * wlist (this also means it can't be ALOCK'd).
1882 1.1 mrg */
1883 1.1 mrg if (!got_lock)
1884 1.1 mrg continue;
1885 1.1 mrg
1886 1.1 mrg /*
1887 1.1 mrg * gain reference. watch out for persisting uvns (need to regain
1888 1.1 mrg * vnode REF).
1889 1.1 mrg */
1890 1.1 mrg if (uvn->u_obj.uo_refs == 0)
1891 1.1 mrg VREF(vp);
1892 1.1 mrg uvn->u_obj.uo_refs++;
1893 1.1 mrg simple_unlock(&uvn->u_obj.vmobjlock);
1894 1.1 mrg
1895 1.1 mrg /*
1896 1.1 mrg * got it!
1897 1.1 mrg */
1898 1.1 mrg SIMPLEQ_INSERT_HEAD(&uvn_sync_q, uvn, u_syncq);
1899 1.1 mrg }
1900 1.1 mrg simple_unlock(&uvn_wl_lock);
1901 1.1 mrg
1902 1.1 mrg /*
1903 1.1 mrg * step 3: we now have a list of uvn's that may need cleaning.
1904 1.1 mrg * we are holding the uvn_sync_lock, but have dropped the uvn_wl_lock
1905 1.1 mrg * (so we can now safely lock uvn's again).
1906 1.1 mrg */
1907 1.1 mrg
1908 1.1 mrg for (uvn = uvn_sync_q.sqh_first ; uvn ; uvn = uvn->u_syncq.sqe_next) {
1909 1.1 mrg simple_lock(&uvn->u_obj.vmobjlock);
1910 1.1 mrg #ifdef DIAGNOSTIC
1911 1.1 mrg if (uvn->u_flags & UVM_VNODE_DYING) {
1912 1.1 mrg printf("uvm_vnp_sync: dying vnode on sync list\n");
1913 1.1 mrg }
1914 1.1 mrg #endif
1915 1.1 mrg uvn_flush(&uvn->u_obj, 0, 0, PGO_CLEANIT|PGO_ALLPAGES|PGO_DOACTCLUST);
1916 1.1 mrg
1917 1.1 mrg /*
1918 1.1 mrg * if we have the only reference and we just cleaned the uvn, then
1919 1.1 mrg * we can pull it out of the UVM_VNODE_WRITEABLE state thus allowing
1920 1.1 mrg * us to avoid thinking about flushing it again on later sync ops.
1921 1.1 mrg */
1922 1.1 mrg if (uvn->u_obj.uo_refs == 1 && (uvn->u_flags & UVM_VNODE_WRITEABLE)) {
1923 1.1 mrg LIST_REMOVE(uvn, u_wlist);
1924 1.1 mrg uvn->u_flags &= ~UVM_VNODE_WRITEABLE;
1925 1.1 mrg }
1926 1.1 mrg
1927 1.1 mrg simple_unlock(&uvn->u_obj.vmobjlock);
1928 1.1 mrg
1929 1.1 mrg /* now drop our reference to the uvn */
1930 1.1 mrg uvn_detach(&uvn->u_obj);
1931 1.1 mrg }
1932 1.1 mrg
1933 1.1 mrg /*
1934 1.1 mrg * done! release sync lock
1935 1.1 mrg */
1936 1.1 mrg lockmgr(&uvn_sync_lock, LK_RELEASE, (void *)0, curproc);
1937 1.1 mrg }
1938