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