1 /* $OpenBSD: uvm_vnode.c,v 1.138 2024/12/27 12:04:40 mpi Exp $ */
2 /* $NetBSD: uvm_vnode.c,v 1.36 2000/11/24 20:34:01 chs Exp $ */
3
4 /*
5 * Copyright (c) 1997 Charles D. Cranor and Washington University.
6 * Copyright (c) 1991, 1993
7 * The Regents of the University of California.
8 * Copyright (c) 1990 University of Utah.
9 *
10 * All rights reserved.
11 *
12 * This code is derived from software contributed to Berkeley by
13 * the Systems Programming Group of the University of Utah Computer
14 * Science Department.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)vnode_pager.c 8.8 (Berkeley) 2/13/94
41 * from: Id: uvm_vnode.c,v 1.1.2.26 1998/02/02 20:38:07 chuck Exp
42 */
43
44 /*
45 * uvm_vnode.c: the vnode pager.
46 */
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/proc.h>
51 #include <sys/malloc.h>
52 #include <sys/vnode.h>
53 #include <sys/lock.h>
54 #include <sys/disklabel.h>
55 #include <sys/fcntl.h>
56 #include <sys/conf.h>
57 #include <sys/rwlock.h>
58 #include <sys/dkio.h>
59 #include <sys/specdev.h>
60
61 #include <uvm/uvm.h>
62 #include <uvm/uvm_vnode.h>
63
64 /*
65 * private global data structure
66 *
67 * we keep a list of writeable active vnode-backed VM objects for sync op.
68 * we keep a simpleq of vnodes that are currently being sync'd.
69 */
70
71 LIST_HEAD(, uvm_vnode) uvn_wlist; /* [K] writeable uvns */
72 SIMPLEQ_HEAD(, uvm_vnode) uvn_sync_q; /* [S] sync'ing uvns */
73 struct rwlock uvn_sync_lock; /* locks sync operation */
74
75 extern int rebooting;
76
77 /*
78 * functions
79 */
80 void uvn_cluster(struct uvm_object *, voff_t, voff_t *, voff_t *);
81 void uvn_detach(struct uvm_object *);
82 boolean_t uvn_flush(struct uvm_object *, voff_t, voff_t, int);
83 int uvn_get(struct uvm_object *, voff_t, vm_page_t *, int *, int,
84 vm_prot_t, int, int);
85 void uvn_init(void);
86 int uvn_io(struct uvm_vnode *, vm_page_t *, int, int, int);
87 int uvn_put(struct uvm_object *, vm_page_t *, int, boolean_t);
88 void uvn_reference(struct uvm_object *);
89
90 /*
91 * master pager structure
92 */
93 const struct uvm_pagerops uvm_vnodeops = {
94 .pgo_init = uvn_init,
95 .pgo_reference = uvn_reference,
96 .pgo_detach = uvn_detach,
97 .pgo_flush = uvn_flush,
98 .pgo_get = uvn_get,
99 .pgo_put = uvn_put,
100 .pgo_cluster = uvn_cluster,
101 /* use generic version of this: see uvm_pager.c */
102 .pgo_mk_pcluster = uvm_mk_pcluster,
103 };
104
105 /*
106 * the ops!
107 */
108 /*
109 * uvn_init
110 *
111 * init pager private data structures.
112 */
113 void
uvn_init(void)114 uvn_init(void)
115 {
116
117 LIST_INIT(&uvn_wlist);
118 /* note: uvn_sync_q init'd in uvm_vnp_sync() */
119 rw_init_flags(&uvn_sync_lock, "uvnsync", RWL_IS_VNODE);
120 }
121
122 /*
123 * uvn_attach
124 *
125 * attach a vnode structure to a VM object. if the vnode is already
126 * attached, then just bump the reference count by one and return the
127 * VM object. if not already attached, attach and return the new VM obj.
128 * the "accessprot" tells the max access the attaching thread wants to
129 * our pages.
130 *
131 * => in fact, nothing should be locked so that we can sleep here.
132 * => note that uvm_object is first thing in vnode structure, so their
133 * pointers are equiv.
134 */
135 struct uvm_object *
uvn_attach(struct vnode * vp,vm_prot_t accessprot)136 uvn_attach(struct vnode *vp, vm_prot_t accessprot)
137 {
138 struct uvm_vnode *uvn = vp->v_uvm;
139 struct vattr vattr;
140 int oldflags, result;
141 struct partinfo pi;
142 u_quad_t used_vnode_size = 0;
143
144 /* if we're mapping a BLK device, make sure it is a disk. */
145 if (vp->v_type == VBLK && bdevsw[major(vp->v_rdev)].d_type != D_DISK) {
146 return NULL;
147 }
148
149 /* first get a lock on the uvn. */
150 rw_enter(uvn->u_obj.vmobjlock, RW_WRITE);
151 while (uvn->u_flags & UVM_VNODE_BLOCKED) {
152 uvn->u_flags |= UVM_VNODE_WANTED;
153 rwsleep_nsec(uvn, uvn->u_obj.vmobjlock, PVM, "uvn_attach",
154 INFSLP);
155 }
156
157 /*
158 * now uvn must not be in a blocked state.
159 * first check to see if it is already active, in which case
160 * we can bump the reference count, check to see if we need to
161 * add it to the writeable list, and then return.
162 */
163 if (uvn->u_flags & UVM_VNODE_VALID) { /* already active? */
164
165 /* regain vref if we were persisting */
166 if (uvn->u_obj.uo_refs == 0) {
167 vref(vp);
168 }
169 uvn->u_obj.uo_refs++; /* bump uvn ref! */
170
171 /* check for new writeable uvn */
172 if ((accessprot & PROT_WRITE) != 0 &&
173 (uvn->u_flags & UVM_VNODE_WRITEABLE) == 0) {
174 uvn->u_flags |= UVM_VNODE_WRITEABLE;
175 KERNEL_ASSERT_LOCKED();
176 LIST_INSERT_HEAD(&uvn_wlist, uvn, u_wlist);
177 }
178
179 rw_exit(uvn->u_obj.vmobjlock);
180 return (&uvn->u_obj);
181 }
182
183 /*
184 * need to call VOP_GETATTR() to get the attributes, but that could
185 * block (due to I/O), so we want to unlock the object before calling.
186 * however, we want to keep anyone else from playing with the object
187 * while it is unlocked. to do this we set UVM_VNODE_ALOCK which
188 * prevents anyone from attaching to the vnode until we are done with
189 * it.
190 */
191 uvn->u_flags = UVM_VNODE_ALOCK;
192 rw_exit(uvn->u_obj.vmobjlock);
193
194 if (vp->v_type == VBLK) {
195 /*
196 * We could implement this as a specfs getattr call, but:
197 *
198 * (1) VOP_GETATTR() would get the file system
199 * vnode operation, not the specfs operation.
200 *
201 * (2) All we want is the size, anyhow.
202 */
203 result = (*bdevsw[major(vp->v_rdev)].d_ioctl)(vp->v_rdev,
204 DIOCGPART, (caddr_t)&pi, FREAD, curproc);
205 if (result == 0) {
206 /* XXX should remember blocksize */
207 used_vnode_size = (u_quad_t)pi.disklab->d_secsize *
208 (u_quad_t)DL_GETPSIZE(pi.part);
209 }
210 } else {
211 result = VOP_GETATTR(vp, &vattr, curproc->p_ucred, curproc);
212 if (result == 0)
213 used_vnode_size = vattr.va_size;
214 }
215
216 if (result != 0) {
217 rw_enter(uvn->u_obj.vmobjlock, RW_WRITE);
218 if (uvn->u_flags & UVM_VNODE_WANTED)
219 wakeup(uvn);
220 uvn->u_flags = 0;
221 rw_exit(uvn->u_obj.vmobjlock);
222 return NULL;
223 }
224
225 /*
226 * make sure that the newsize fits within a vaddr_t
227 * XXX: need to revise addressing data types
228 */
229 #ifdef DEBUG
230 if (vp->v_type == VBLK)
231 printf("used_vnode_size = %llu\n", (long long)used_vnode_size);
232 #endif
233
234 /* now set up the uvn. */
235 KASSERT(uvn->u_obj.uo_refs == 0);
236 uvn->u_obj.uo_refs++;
237 oldflags = uvn->u_flags;
238 uvn->u_flags = UVM_VNODE_VALID|UVM_VNODE_CANPERSIST;
239 uvn->u_nio = 0;
240 uvn->u_size = used_vnode_size;
241
242 /*
243 * add a reference to the vnode. this reference will stay as long
244 * as there is a valid mapping of the vnode. dropped when the
245 * reference count goes to zero [and we either free or persist].
246 */
247 vref(vp);
248
249 /* if write access, we need to add it to the wlist */
250 if (accessprot & PROT_WRITE) {
251 uvn->u_flags |= UVM_VNODE_WRITEABLE; /* we are on wlist! */
252 KERNEL_ASSERT_LOCKED();
253 LIST_INSERT_HEAD(&uvn_wlist, uvn, u_wlist);
254 }
255
256 if (oldflags & UVM_VNODE_WANTED)
257 wakeup(uvn);
258
259 return &uvn->u_obj;
260 }
261
262
263 /*
264 * uvn_reference
265 *
266 * duplicate a reference to a VM object. Note that the reference
267 * count must already be at least one (the passed in reference) so
268 * there is no chance of the uvn being killed out here.
269 *
270 * => caller must be using the same accessprot as was used at attach time
271 */
272
273
274 void
uvn_reference(struct uvm_object * uobj)275 uvn_reference(struct uvm_object *uobj)
276 {
277 #ifdef DEBUG
278 struct uvm_vnode *uvn = (struct uvm_vnode *) uobj;
279 #endif
280
281 rw_enter(uobj->vmobjlock, RW_WRITE);
282 #ifdef DEBUG
283 if ((uvn->u_flags & UVM_VNODE_VALID) == 0) {
284 printf("uvn_reference: ref=%d, flags=0x%x\n",
285 uobj->uo_refs, uvn->u_flags);
286 panic("uvn_reference: invalid state");
287 }
288 #endif
289 uobj->uo_refs++;
290 rw_exit(uobj->vmobjlock);
291 }
292
293 /*
294 * uvn_detach
295 *
296 * remove a reference to a VM object.
297 *
298 * => caller must call with map locked.
299 * => this starts the detach process, but doesn't have to finish it
300 * (async i/o could still be pending).
301 */
302 void
uvn_detach(struct uvm_object * uobj)303 uvn_detach(struct uvm_object *uobj)
304 {
305 struct uvm_vnode *uvn;
306 struct vnode *vp;
307 int oldflags;
308
309 rw_enter(uobj->vmobjlock, RW_WRITE);
310 uobj->uo_refs--; /* drop ref! */
311 if (uobj->uo_refs) { /* still more refs */
312 rw_exit(uobj->vmobjlock);
313 return;
314 }
315
316 KERNEL_LOCK();
317 /* get other pointers ... */
318 uvn = (struct uvm_vnode *) uobj;
319 vp = uvn->u_vnode;
320
321 /*
322 * clear VTEXT flag now that there are no mappings left (VTEXT is used
323 * to keep an active text file from being overwritten).
324 */
325 vp->v_flag &= ~VTEXT;
326
327 /*
328 * we just dropped the last reference to the uvn. see if we can
329 * let it "stick around".
330 */
331 if (uvn->u_flags & UVM_VNODE_CANPERSIST) {
332 /* won't block */
333 uvn_flush(uobj, 0, 0, PGO_DEACTIVATE|PGO_ALLPAGES);
334 goto out;
335 }
336
337 /* its a goner! */
338 uvn->u_flags |= UVM_VNODE_DYING;
339
340 /*
341 * even though we may unlock in flush, no one can gain a reference
342 * to us until we clear the "dying" flag [because it blocks
343 * attaches]. we will not do that until after we've disposed of all
344 * the pages with uvn_flush(). note that before the flush the only
345 * pages that could be marked PG_BUSY are ones that are in async
346 * pageout by the daemon. (there can't be any pending "get"'s
347 * because there are no references to the object).
348 */
349 (void) uvn_flush(uobj, 0, 0, PGO_CLEANIT|PGO_FREE|PGO_ALLPAGES);
350
351 /*
352 * given the structure of this pager, the above flush request will
353 * create the following state: all the pages that were in the object
354 * have either been free'd or they are marked PG_BUSY and in the
355 * middle of an async io. If we still have pages we set the "relkill"
356 * state, so that in the case the vnode gets terminated we know
357 * to leave it alone. Otherwise we'll kill the vnode when it's empty.
358 */
359 uvn->u_flags |= UVM_VNODE_RELKILL;
360 /* wait on any outstanding io */
361 while (uobj->uo_npages && uvn->u_flags & UVM_VNODE_RELKILL) {
362 uvn->u_flags |= UVM_VNODE_IOSYNC;
363 rwsleep_nsec(&uvn->u_nio, uobj->vmobjlock, PVM, "uvn_term",
364 INFSLP);
365 }
366
367 if ((uvn->u_flags & UVM_VNODE_RELKILL) == 0) {
368 rw_exit(uobj->vmobjlock);
369 KERNEL_UNLOCK();
370 return;
371 }
372
373 /*
374 * kill object now. note that we can't be on the sync q because
375 * all references are gone.
376 */
377 if (uvn->u_flags & UVM_VNODE_WRITEABLE) {
378 LIST_REMOVE(uvn, u_wlist);
379 }
380 KASSERT(RBT_EMPTY(uvm_objtree, &uobj->memt));
381 oldflags = uvn->u_flags;
382 uvn->u_flags = 0;
383
384 /* wake up any sleepers */
385 if (oldflags & UVM_VNODE_WANTED)
386 wakeup(uvn);
387 out:
388 rw_exit(uobj->vmobjlock);
389
390 /* drop our reference to the vnode. */
391 vrele(vp);
392 KERNEL_UNLOCK();
393 }
394
395 /*
396 * uvm_vnp_terminate: external hook to clear out a vnode's VM
397 *
398 * called in two cases:
399 * [1] when a persisting vnode vm object (i.e. one with a zero reference
400 * count) needs to be freed so that a vnode can be reused. this
401 * happens under "getnewvnode" in vfs_subr.c. if the vnode from
402 * the free list is still attached (i.e. not VBAD) then vgone is
403 * called. as part of the vgone trace this should get called to
404 * free the vm object. this is the common case.
405 * [2] when a filesystem is being unmounted by force (MNT_FORCE,
406 * "umount -f") the vgone() function is called on active vnodes
407 * on the mounted file systems to kill their data (the vnodes become
408 * "dead" ones [see src/sys/miscfs/deadfs/...]). that results in a
409 * call here (even if the uvn is still in use -- i.e. has a non-zero
410 * reference count). this case happens at "umount -f" and during a
411 * "reboot/halt" operation.
412 *
413 * => the caller must XLOCK and VOP_LOCK the vnode before calling us
414 * [protects us from getting a vnode that is already in the DYING
415 * state...]
416 * => in case [2] the uvn is still alive after this call, but all I/O
417 * ops will fail (due to the backing vnode now being "dead"). this
418 * will prob. kill any process using the uvn due to pgo_get failing.
419 */
420 void
uvm_vnp_terminate(struct vnode * vp)421 uvm_vnp_terminate(struct vnode *vp)
422 {
423 struct uvm_vnode *uvn = vp->v_uvm;
424 struct uvm_object *uobj = &uvn->u_obj;
425 int oldflags;
426
427 /* check if it is valid */
428 rw_enter(uobj->vmobjlock, RW_WRITE);
429 if ((uvn->u_flags & UVM_VNODE_VALID) == 0) {
430 rw_exit(uobj->vmobjlock);
431 return;
432 }
433
434 /*
435 * must be a valid uvn that is not already dying (because XLOCK
436 * protects us from that). the uvn can't in the ALOCK state
437 * because it is valid, and uvn's that are in the ALOCK state haven't
438 * been marked valid yet.
439 */
440 #ifdef DEBUG
441 /*
442 * debug check: are we yanking the vnode out from under our uvn?
443 */
444 if (uvn->u_obj.uo_refs) {
445 printf("uvm_vnp_terminate(%p): terminating active vnode "
446 "(refs=%d)\n", uvn, uvn->u_obj.uo_refs);
447 }
448 #endif
449
450 /*
451 * it is possible that the uvn was detached and is in the relkill
452 * state [i.e. waiting for async i/o to finish].
453 * we take over the vnode now and cancel the relkill.
454 * we want to know when the i/o is done so we can recycle right
455 * away. note that a uvn can only be in the RELKILL state if it
456 * has a zero reference count.
457 */
458 if (uvn->u_flags & UVM_VNODE_RELKILL)
459 uvn->u_flags &= ~UVM_VNODE_RELKILL; /* cancel RELKILL */
460
461 /*
462 * block the uvn by setting the dying flag, and then flush the
463 * pages.
464 *
465 * also, note that we tell I/O that we are already VOP_LOCK'd so
466 * that uvn_io doesn't attempt to VOP_LOCK again.
467 *
468 * XXXCDC: setting VNISLOCKED on an active uvn which is being terminated
469 * due to a forceful unmount might not be a good idea. maybe we
470 * need a way to pass in this info to uvn_flush through a
471 * pager-defined PGO_ constant [currently there are none].
472 */
473 uvn->u_flags |= UVM_VNODE_DYING|UVM_VNODE_VNISLOCKED;
474
475 (void) uvn_flush(&uvn->u_obj, 0, 0, PGO_CLEANIT|PGO_FREE|PGO_ALLPAGES);
476
477 /*
478 * as we just did a flush we expect all the pages to be gone or in
479 * the process of going. sleep to wait for the rest to go [via iosync].
480 */
481 while (uvn->u_obj.uo_npages) {
482 #ifdef DEBUG
483 struct vm_page *pp;
484 RBT_FOREACH(pp, uvm_objtree, &uvn->u_obj.memt) {
485 if ((pp->pg_flags & PG_BUSY) == 0)
486 panic("uvm_vnp_terminate: detected unbusy pg");
487 }
488 if (uvn->u_nio == 0)
489 panic("uvm_vnp_terminate: no I/O to wait for?");
490 printf("uvm_vnp_terminate: waiting for I/O to fin.\n");
491 /*
492 * XXXCDC: this is unlikely to happen without async i/o so we
493 * put a printf in just to keep an eye on it.
494 */
495 #endif
496 uvn->u_flags |= UVM_VNODE_IOSYNC;
497 rwsleep_nsec(&uvn->u_nio, uobj->vmobjlock, PVM, "uvn_term",
498 INFSLP);
499 }
500
501 /*
502 * done. now we free the uvn if its reference count is zero
503 * (true if we are zapping a persisting uvn). however, if we are
504 * terminating a uvn with active mappings we let it live ... future
505 * calls down to the vnode layer will fail.
506 */
507 oldflags = uvn->u_flags;
508 if (uvn->u_obj.uo_refs) {
509 /*
510 * uvn must live on it is dead-vnode state until all references
511 * are gone. restore flags. clear CANPERSIST state.
512 */
513 uvn->u_flags &= ~(UVM_VNODE_DYING|UVM_VNODE_VNISLOCKED|
514 UVM_VNODE_WANTED|UVM_VNODE_CANPERSIST);
515 } else {
516 /*
517 * free the uvn now. note that the vref reference is already
518 * gone [it is dropped when we enter the persist state].
519 */
520 if (uvn->u_flags & UVM_VNODE_IOSYNCWANTED)
521 panic("uvm_vnp_terminate: io sync wanted bit set");
522
523 if (uvn->u_flags & UVM_VNODE_WRITEABLE) {
524 LIST_REMOVE(uvn, u_wlist);
525 }
526 uvn->u_flags = 0; /* uvn is history, clear all bits */
527 }
528
529 if (oldflags & UVM_VNODE_WANTED)
530 wakeup(uvn);
531
532 rw_exit(uobj->vmobjlock);
533 }
534
535 /*
536 * NOTE: currently we have to use VOP_READ/VOP_WRITE because they go
537 * through the buffer cache and allow I/O in any size. These VOPs use
538 * synchronous i/o. [vs. VOP_STRATEGY which can be async, but doesn't
539 * go through the buffer cache or allow I/O sizes larger than a
540 * block]. we will eventually want to change this.
541 *
542 * issues to consider:
543 * uvm provides the uvm_aiodesc structure for async i/o management.
544 * there are two tailq's in the uvm. structure... one for pending async
545 * i/o and one for "done" async i/o. to do an async i/o one puts
546 * an aiodesc on the "pending" list (protected by splbio()), starts the
547 * i/o and returns VM_PAGER_PEND. when the i/o is done, we expect
548 * some sort of "i/o done" function to be called (at splbio(), interrupt
549 * time). this function should remove the aiodesc from the pending list
550 * and place it on the "done" list and wakeup the daemon. the daemon
551 * will run at normal spl() and will remove all items from the "done"
552 * list and call the "aiodone" hook for each done request (see uvm_pager.c).
553 * [in the old vm code, this was done by calling the "put" routine with
554 * null arguments which made the code harder to read and understand because
555 * you had one function ("put") doing two things.]
556 *
557 * so the current pager needs:
558 * int uvn_aiodone(struct uvm_aiodesc *)
559 *
560 * => return 0 (aio finished, free it). otherwise requeue for later collection.
561 * => called with pageq's locked by the daemon.
562 *
563 * general outline:
564 * - drop "u_nio" (this req is done!)
565 * - if (object->iosync && u_naio == 0) { wakeup &uvn->u_naio }
566 * - get "page" structures (atop?).
567 * - handle "wanted" pages
568 * dont forget to look at "object" wanted flag in all cases.
569 */
570
571 /*
572 * uvn_flush: flush pages out of a uvm object.
573 *
574 * => if PGO_CLEANIT is set, we may block (due to I/O). thus, a caller
575 * might want to unlock higher level resources (e.g. vm_map)
576 * before calling flush.
577 * => if PGO_CLEANIT is not set, then we will not block
578 * => if PGO_ALLPAGE is set, then all pages in the object are valid targets
579 * for flushing.
580 * => NOTE: we are allowed to lock the page queues, so the caller
581 * must not be holding the lock on them [e.g. pagedaemon had
582 * better not call us with the queues locked]
583 * => we return TRUE unless we encountered some sort of I/O error
584 *
585 * comment on "cleaning" object and PG_BUSY pages:
586 * this routine is holding the lock on the object. the only time
587 * that it can run into a PG_BUSY page that it does not own is if
588 * some other process has started I/O on the page (e.g. either
589 * a pagein, or a pageout). if the PG_BUSY page is being paged
590 * in, then it can not be dirty (!PG_CLEAN) because no one has
591 * had a chance to modify it yet. if the PG_BUSY page is being
592 * paged out then it means that someone else has already started
593 * cleaning the page for us (how nice!). in this case, if we
594 * have syncio specified, then after we make our pass through the
595 * object we need to wait for the other PG_BUSY pages to clear
596 * off (i.e. we need to do an iosync). also note that once a
597 * page is PG_BUSY it must stay in its object until it is un-busyed.
598 */
599 boolean_t
uvn_flush(struct uvm_object * uobj,voff_t start,voff_t stop,int flags)600 uvn_flush(struct uvm_object *uobj, voff_t start, voff_t stop, int flags)
601 {
602 struct uvm_vnode *uvn = (struct uvm_vnode *) uobj;
603 struct vm_page *pp, *ptmp;
604 struct vm_page *pps[MAXBSIZE >> PAGE_SHIFT], **ppsp;
605 struct pglist dead;
606 int npages, result, lcv;
607 boolean_t retval, need_iosync, needs_clean;
608 voff_t curoff;
609
610 KASSERT(rw_write_held(uobj->vmobjlock));
611 TAILQ_INIT(&dead);
612
613 /* get init vals and determine how we are going to traverse object */
614 need_iosync = FALSE;
615 retval = TRUE; /* return value */
616 if (flags & PGO_ALLPAGES) {
617 start = 0;
618 stop = round_page(uvn->u_size);
619 } else {
620 start = trunc_page(start);
621 stop = MIN(round_page(stop), round_page(uvn->u_size));
622 }
623
624 /*
625 * PG_CLEANCHK: this bit is used by the pgo_mk_pcluster function as
626 * a _hint_ as to how up to date the PG_CLEAN bit is. if the hint
627 * is wrong it will only prevent us from clustering... it won't break
628 * anything. we clear all PG_CLEANCHK bits here, and pgo_mk_pcluster
629 * will set them as it syncs PG_CLEAN. This is only an issue if we
630 * are looking at non-inactive pages (because inactive page's PG_CLEAN
631 * bit is always up to date since there are no mappings).
632 * [borrowed PG_CLEANCHK idea from FreeBSD VM]
633 */
634 if ((flags & PGO_CLEANIT) != 0) {
635 KASSERT(uobj->pgops->pgo_mk_pcluster != 0);
636 for (curoff = start ; curoff < stop; curoff += PAGE_SIZE) {
637 if ((pp = uvm_pagelookup(uobj, curoff)) != NULL)
638 atomic_clearbits_int(&pp->pg_flags,
639 PG_CLEANCHK);
640 }
641 }
642
643 ppsp = NULL; /* XXX: shut up gcc */
644 uvm_lock_pageq();
645 /* locked: both page queues */
646 for (curoff = start; curoff < stop; curoff += PAGE_SIZE) {
647 if ((pp = uvm_pagelookup(uobj, curoff)) == NULL)
648 continue;
649 /*
650 * handle case where we do not need to clean page (either
651 * because we are not clean or because page is not dirty or
652 * is busy):
653 *
654 * NOTE: we are allowed to deactivate a non-wired active
655 * PG_BUSY page, but once a PG_BUSY page is on the inactive
656 * queue it must stay put until it is !PG_BUSY (so as not to
657 * confuse pagedaemon).
658 */
659 if ((flags & PGO_CLEANIT) == 0 || (pp->pg_flags & PG_BUSY) != 0) {
660 needs_clean = FALSE;
661 if ((pp->pg_flags & PG_BUSY) != 0 &&
662 (flags & (PGO_CLEANIT|PGO_SYNCIO)) ==
663 (PGO_CLEANIT|PGO_SYNCIO))
664 need_iosync = TRUE;
665 } else {
666 /*
667 * freeing: nuke all mappings so we can sync
668 * PG_CLEAN bit with no race
669 */
670 if ((pp->pg_flags & PG_CLEAN) != 0 &&
671 (flags & PGO_FREE) != 0 &&
672 (pp->pg_flags & PQ_ACTIVE) != 0)
673 pmap_page_protect(pp, PROT_NONE);
674 if ((pp->pg_flags & PG_CLEAN) != 0 &&
675 pmap_is_modified(pp))
676 atomic_clearbits_int(&pp->pg_flags, PG_CLEAN);
677 atomic_setbits_int(&pp->pg_flags, PG_CLEANCHK);
678
679 needs_clean = ((pp->pg_flags & PG_CLEAN) == 0);
680 }
681
682 /* if we don't need a clean, deactivate/free pages then cont. */
683 if (!needs_clean) {
684 if (flags & PGO_DEACTIVATE) {
685 if (pp->wire_count == 0) {
686 uvm_pagedeactivate(pp);
687 }
688 } else if (flags & PGO_FREE) {
689 if (pp->pg_flags & PG_BUSY) {
690 uvm_unlock_pageq();
691 uvm_pagewait(pp, uobj->vmobjlock,
692 "uvn_flsh");
693 rw_enter(uobj->vmobjlock, RW_WRITE);
694 uvm_lock_pageq();
695 curoff -= PAGE_SIZE;
696 continue;
697 } else {
698 pmap_page_protect(pp, PROT_NONE);
699 /* removed page from object */
700 uvm_pageclean(pp);
701 TAILQ_INSERT_HEAD(&dead, pp, pageq);
702 }
703 }
704 continue;
705 }
706
707 /*
708 * pp points to a page in the object that we are
709 * working on. if it is !PG_CLEAN,!PG_BUSY and we asked
710 * for cleaning (PGO_CLEANIT). we clean it now.
711 *
712 * let uvm_pager_put attempted a clustered page out.
713 * note: locked: page queues.
714 */
715 atomic_setbits_int(&pp->pg_flags, PG_BUSY);
716 UVM_PAGE_OWN(pp, "uvn_flush");
717 pmap_page_protect(pp, PROT_READ);
718 /* if we're async, free the page in aiodoned */
719 if ((flags & (PGO_FREE|PGO_SYNCIO)) == PGO_FREE)
720 atomic_setbits_int(&pp->pg_flags, PG_RELEASED);
721 ReTry:
722 ppsp = pps;
723 npages = sizeof(pps) / sizeof(struct vm_page *);
724
725 result = uvm_pager_put(uobj, pp, &ppsp, &npages,
726 flags | PGO_DOACTCLUST, start, stop);
727
728 /*
729 * if we did an async I/O it is remotely possible for the
730 * async i/o to complete and the page "pp" be freed or what
731 * not before we get a chance to relock the object. Therefore,
732 * we only touch it when it won't be freed, RELEASED took care
733 * of the rest.
734 */
735 uvm_lock_pageq();
736
737 /*
738 * VM_PAGER_AGAIN: given the structure of this pager, this
739 * can only happen when we are doing async I/O and can't
740 * map the pages into kernel memory (pager_map) due to lack
741 * of vm space. if this happens we drop back to sync I/O.
742 */
743 if (result == VM_PAGER_AGAIN) {
744 /*
745 * it is unlikely, but page could have been released
746 * we ignore this now and retry the I/O.
747 * we will detect and
748 * handle the released page after the syncio I/O
749 * completes.
750 */
751 #ifdef DIAGNOSTIC
752 if (flags & PGO_SYNCIO)
753 panic("%s: PGO_SYNCIO return 'try again' error (impossible)", __func__);
754 #endif
755 flags |= PGO_SYNCIO;
756 if (flags & PGO_FREE)
757 atomic_clearbits_int(&pp->pg_flags,
758 PG_RELEASED);
759
760 goto ReTry;
761 }
762
763 /*
764 * the cleaning operation is now done. finish up. note that
765 * on error (!OK, !PEND) uvm_pager_put drops the cluster for us.
766 * if success (OK, PEND) then uvm_pager_put returns the cluster
767 * to us in ppsp/npages.
768 */
769 /*
770 * for pending async i/o if we are not deactivating
771 * we can move on to the next page. aiodoned deals with
772 * the freeing case for us.
773 */
774 if (result == VM_PAGER_PEND && (flags & PGO_DEACTIVATE) == 0)
775 continue;
776
777 /*
778 * need to look at each page of the I/O operation, and do what
779 * we gotta do.
780 */
781 for (lcv = 0 ; lcv < npages; lcv++) {
782 ptmp = ppsp[lcv];
783 /*
784 * verify the page didn't get moved
785 */
786 if (result == VM_PAGER_PEND && ptmp->uobject != uobj)
787 continue;
788
789 /*
790 * unbusy the page if I/O is done. note that for
791 * pending I/O it is possible that the I/O op
792 * finished
793 * (in which case the page is no longer busy).
794 */
795 if (result != VM_PAGER_PEND) {
796 if (ptmp->pg_flags & PG_WANTED)
797 wakeup(ptmp);
798
799 atomic_clearbits_int(&ptmp->pg_flags,
800 PG_WANTED|PG_BUSY);
801 UVM_PAGE_OWN(ptmp, NULL);
802 atomic_setbits_int(&ptmp->pg_flags,
803 PG_CLEAN|PG_CLEANCHK);
804 if ((flags & PGO_FREE) == 0)
805 pmap_clear_modify(ptmp);
806 }
807
808 /* dispose of page */
809 if (flags & PGO_DEACTIVATE) {
810 if (ptmp->wire_count == 0) {
811 uvm_pagedeactivate(ptmp);
812 }
813 } else if (flags & PGO_FREE &&
814 result != VM_PAGER_PEND) {
815 if (result != VM_PAGER_OK) {
816 static struct timeval lasttime;
817 static const struct timeval interval =
818 { 5, 0 };
819
820 if (ratecheck(&lasttime, &interval)) {
821 printf("%s: obj=%p, "
822 "offset=0x%llx. error "
823 "during pageout.\n",
824 __func__, pp->uobject,
825 (long long)pp->offset);
826 printf("%s: WARNING: "
827 "changes to page may be "
828 "lost!\n", __func__);
829 }
830 retval = FALSE;
831 }
832 pmap_page_protect(ptmp, PROT_NONE);
833 uvm_pageclean(ptmp);
834 TAILQ_INSERT_TAIL(&dead, ptmp, pageq);
835 }
836
837 } /* end of "lcv" for loop */
838
839 } /* end of "pp" for loop */
840
841 /* done with pagequeues: unlock */
842 uvm_unlock_pageq();
843
844 /* now wait for all I/O if required. */
845 if (need_iosync) {
846 while (uvn->u_nio != 0) {
847 uvn->u_flags |= UVM_VNODE_IOSYNC;
848 rwsleep_nsec(&uvn->u_nio, uobj->vmobjlock, PVM,
849 "uvn_flush", INFSLP);
850 }
851 if (uvn->u_flags & UVM_VNODE_IOSYNCWANTED)
852 wakeup(&uvn->u_flags);
853 uvn->u_flags &= ~(UVM_VNODE_IOSYNC|UVM_VNODE_IOSYNCWANTED);
854 }
855
856 uvm_pglistfree(&dead);
857
858 return retval;
859 }
860
861 /*
862 * uvn_cluster
863 *
864 * we are about to do I/O in an object at offset. this function is called
865 * to establish a range of offsets around "offset" in which we can cluster
866 * I/O.
867 */
868
869 void
uvn_cluster(struct uvm_object * uobj,voff_t offset,voff_t * loffset,voff_t * hoffset)870 uvn_cluster(struct uvm_object *uobj, voff_t offset, voff_t *loffset,
871 voff_t *hoffset)
872 {
873 struct uvm_vnode *uvn = (struct uvm_vnode *) uobj;
874 *loffset = offset;
875
876 KASSERT(rw_write_held(uobj->vmobjlock));
877
878 if (*loffset >= uvn->u_size)
879 panic("uvn_cluster: offset out of range");
880
881 /*
882 * XXX: old pager claims we could use VOP_BMAP to get maxcontig value.
883 */
884 *hoffset = *loffset + MAXBSIZE;
885 if (*hoffset > round_page(uvn->u_size)) /* past end? */
886 *hoffset = round_page(uvn->u_size);
887 }
888
889 /*
890 * uvn_put: flush page data to backing store.
891 *
892 * => prefer map unlocked (not required)
893 * => flags: PGO_SYNCIO -- use sync. I/O
894 * => note: caller must set PG_CLEAN and pmap_clear_modify (if needed)
895 * => XXX: currently we use VOP_READ/VOP_WRITE which are only sync.
896 * [thus we never do async i/o! see iodone comment]
897 */
898 int
uvn_put(struct uvm_object * uobj,struct vm_page ** pps,int npages,int flags)899 uvn_put(struct uvm_object *uobj, struct vm_page **pps, int npages, int flags)
900 {
901 struct uvm_vnode *uvn = (struct uvm_vnode *)uobj;
902 int dying, retval;
903
904 KASSERT(rw_write_held(uobj->vmobjlock));
905
906 /*
907 * Unless we're recycling this vnode, grab a reference to it
908 * to prevent it from being recycled from under our feet.
909 * This also makes sure we can don't panic if we end up in
910 * uvn_vnp_uncache() as a result of the I/O operation as that
911 * function assumes we hold a reference.
912 *
913 * If the vnode is in the process of being recycled by someone
914 * else, grabbing a reference will fail. In that case the
915 * pages will already be written out by whoever is cleaning
916 * the vnode, so simply return VM_PAGER_AGAIN such that we
917 * skip these pages.
918 */
919 dying = (uvn->u_flags & UVM_VNODE_DYING);
920 if (!dying) {
921 if (vget(uvn->u_vnode, LK_NOWAIT))
922 return VM_PAGER_AGAIN;
923 }
924
925 retval = uvn_io((struct uvm_vnode*)uobj, pps, npages, flags, UIO_WRITE);
926
927 if (!dying)
928 vrele(uvn->u_vnode);
929
930 return retval;
931 }
932
933 /*
934 * uvn_get: get pages (synchronously) from backing store
935 *
936 * => prefer map unlocked (not required)
937 * => flags: PGO_ALLPAGES: get all of the pages
938 * PGO_LOCKED: fault data structures are locked
939 * => NOTE: offset is the offset of pps[0], _NOT_ pps[centeridx]
940 * => NOTE: caller must check for released pages!!
941 */
942 int
uvn_get(struct uvm_object * uobj,voff_t offset,struct vm_page ** pps,int * npagesp,int centeridx,vm_prot_t access_type,int advice,int flags)943 uvn_get(struct uvm_object *uobj, voff_t offset, struct vm_page **pps,
944 int *npagesp, int centeridx, vm_prot_t access_type, int advice, int flags)
945 {
946 voff_t current_offset;
947 struct vm_page *ptmp;
948 int lcv, result, gotpages;
949 boolean_t done;
950
951 KASSERT(rw_lock_held(uobj->vmobjlock));
952 KASSERT(rw_write_held(uobj->vmobjlock) ||
953 ((flags & PGO_LOCKED) != 0 && (access_type & PROT_WRITE) == 0));
954
955 /* step 1: handled the case where fault data structures are locked. */
956 if (flags & PGO_LOCKED) {
957 /*
958 * gotpages is the current number of pages we've gotten (which
959 * we pass back up to caller via *npagesp.
960 */
961 gotpages = 0;
962
963 /*
964 * step 1a: get pages that are already resident. only do this
965 * if the data structures are locked (i.e. the first time
966 * through).
967 */
968 done = TRUE; /* be optimistic */
969
970 for (lcv = 0, current_offset = offset ; lcv < *npagesp ;
971 lcv++, current_offset += PAGE_SIZE) {
972 /* do we care about this page? if not, skip it */
973 if (pps[lcv] == PGO_DONTCARE)
974 continue;
975
976 /* lookup page */
977 ptmp = uvm_pagelookup(uobj, current_offset);
978
979 /*
980 * to be useful must get a non-busy page
981 */
982 if (ptmp == NULL || (ptmp->pg_flags & PG_BUSY) != 0) {
983 if (lcv == centeridx ||
984 (flags & PGO_ALLPAGES) != 0)
985 /* need to do a wait or I/O! */
986 done = FALSE;
987 continue;
988 }
989
990 /*
991 * useful page: busy it and plug it in our
992 * result array
993 */
994 pps[lcv] = ptmp;
995 gotpages++;
996
997 }
998
999 /*
1000 * XXX: given the "advice", should we consider async read-ahead?
1001 * XXX: fault current does deactivate of pages behind us. is
1002 * this good (other callers might now).
1003 */
1004 /*
1005 * XXX: read-ahead currently handled by buffer cache (bread)
1006 * level.
1007 * XXX: no async i/o available.
1008 * XXX: so we don't do anything now.
1009 */
1010
1011 /*
1012 * step 1c: now we've either done everything needed or we to
1013 * unlock and do some waiting or I/O.
1014 */
1015 *npagesp = gotpages; /* let caller know */
1016 return done ? VM_PAGER_OK : VM_PAGER_UNLOCK;
1017 }
1018
1019 /*
1020 * step 2: get non-resident or busy pages.
1021 * data structures are unlocked.
1022 *
1023 * XXX: because we can't do async I/O at this level we get things
1024 * page at a time (otherwise we'd chunk). the VOP_READ() will do
1025 * async-read-ahead for us at a lower level.
1026 */
1027 for (lcv = 0, current_offset = offset;
1028 lcv < *npagesp ; lcv++, current_offset += PAGE_SIZE) {
1029
1030 /* skip over pages we've already gotten or don't want */
1031 /* skip over pages we don't _have_ to get */
1032 if (pps[lcv] != NULL || (lcv != centeridx &&
1033 (flags & PGO_ALLPAGES) == 0))
1034 continue;
1035
1036 /*
1037 * we have yet to locate the current page (pps[lcv]). we first
1038 * look for a page that is already at the current offset. if
1039 * we fine a page, we check to see if it is busy or released.
1040 * if that is the case, then we sleep on the page until it is
1041 * no longer busy or released and repeat the lookup. if the
1042 * page we found is neither busy nor released, then we busy it
1043 * (so we own it) and plug it into pps[lcv]. this breaks the
1044 * following while loop and indicates we are ready to move on
1045 * to the next page in the "lcv" loop above.
1046 *
1047 * if we exit the while loop with pps[lcv] still set to NULL,
1048 * then it means that we allocated a new busy/fake/clean page
1049 * ptmp in the object and we need to do I/O to fill in the data.
1050 */
1051 while (pps[lcv] == NULL) { /* top of "pps" while loop */
1052 /* look for a current page */
1053 ptmp = uvm_pagelookup(uobj, current_offset);
1054
1055 /* nope? allocate one now (if we can) */
1056 if (ptmp == NULL) {
1057 ptmp = uvm_pagealloc(uobj, current_offset,
1058 NULL, 0);
1059
1060 /* out of RAM? */
1061 if (ptmp == NULL) {
1062 uvm_wait("uvn_getpage");
1063
1064 /* goto top of pps while loop */
1065 continue;
1066 }
1067
1068 /*
1069 * got new page ready for I/O. break pps
1070 * while loop. pps[lcv] is still NULL.
1071 */
1072 break;
1073 }
1074
1075 /* page is there, see if we need to wait on it */
1076 if ((ptmp->pg_flags & PG_BUSY) != 0) {
1077 uvm_pagewait(ptmp, uobj->vmobjlock, "uvn_get");
1078 rw_enter(uobj->vmobjlock, RW_WRITE);
1079 continue; /* goto top of pps while loop */
1080 }
1081
1082 /*
1083 * if we get here then the page has become resident
1084 * and unbusy between steps 1 and 2. we busy it
1085 * now (so we own it) and set pps[lcv] (so that we
1086 * exit the while loop).
1087 */
1088 atomic_setbits_int(&ptmp->pg_flags, PG_BUSY);
1089 UVM_PAGE_OWN(ptmp, "uvn_get2");
1090 pps[lcv] = ptmp;
1091 }
1092
1093 /*
1094 * if we own the a valid page at the correct offset, pps[lcv]
1095 * will point to it. nothing more to do except go to the
1096 * next page.
1097 */
1098 if (pps[lcv])
1099 continue; /* next lcv */
1100
1101 /*
1102 * we have a "fake/busy/clean" page that we just allocated. do
1103 * I/O to fill it with valid data.
1104 */
1105 result = uvn_io((struct uvm_vnode *) uobj, &ptmp, 1,
1106 PGO_SYNCIO|PGO_NOWAIT, UIO_READ);
1107
1108 /*
1109 * I/O done. because we used syncio the result can not be
1110 * PEND or AGAIN.
1111 */
1112 if (result != VM_PAGER_OK) {
1113 if (ptmp->pg_flags & PG_WANTED)
1114 wakeup(ptmp);
1115
1116 atomic_clearbits_int(&ptmp->pg_flags,
1117 PG_WANTED|PG_BUSY);
1118 UVM_PAGE_OWN(ptmp, NULL);
1119 uvm_lock_pageq();
1120 uvm_pagefree(ptmp);
1121 uvm_unlock_pageq();
1122 rw_exit(uobj->vmobjlock);
1123 return result;
1124 }
1125
1126 /*
1127 * we got the page! clear the fake flag (indicates valid
1128 * data now in page) and plug into our result array. note
1129 * that page is still busy.
1130 *
1131 * it is the callers job to:
1132 * => check if the page is released
1133 * => unbusy the page
1134 * => activate the page
1135 */
1136
1137 /* data is valid ... */
1138 atomic_clearbits_int(&ptmp->pg_flags, PG_FAKE);
1139 pmap_clear_modify(ptmp); /* ... and clean */
1140 pps[lcv] = ptmp;
1141
1142 }
1143
1144
1145 rw_exit(uobj->vmobjlock);
1146 return (VM_PAGER_OK);
1147 }
1148
1149 /*
1150 * uvn_io: do I/O to a vnode
1151 *
1152 * => prefer map unlocked (not required)
1153 * => flags: PGO_SYNCIO -- use sync. I/O
1154 * => XXX: currently we use VOP_READ/VOP_WRITE which are only sync.
1155 * [thus we never do async i/o! see iodone comment]
1156 */
1157
1158 int
uvn_io(struct uvm_vnode * uvn,vm_page_t * pps,int npages,int flags,int rw)1159 uvn_io(struct uvm_vnode *uvn, vm_page_t *pps, int npages, int flags, int rw)
1160 {
1161 struct uvm_object *uobj = &uvn->u_obj;
1162 struct vnode *vn;
1163 struct uio uio;
1164 struct iovec iov;
1165 vaddr_t kva;
1166 off_t file_offset;
1167 int waitf, result, mapinflags;
1168 size_t got, wanted;
1169 int vnlocked, netunlocked = 0;
1170 int lkflags = (flags & PGO_NOWAIT) ? LK_NOWAIT : 0;
1171 voff_t uvnsize;
1172
1173 KASSERT(rw_write_held(uobj->vmobjlock));
1174
1175 /* init values */
1176 waitf = (flags & PGO_SYNCIO) ? M_WAITOK : M_NOWAIT;
1177 vn = uvn->u_vnode;
1178 file_offset = pps[0]->offset;
1179
1180 /* check for sync'ing I/O. */
1181 while (uvn->u_flags & UVM_VNODE_IOSYNC) {
1182 if (waitf == M_NOWAIT) {
1183 return VM_PAGER_AGAIN;
1184 }
1185 uvn->u_flags |= UVM_VNODE_IOSYNCWANTED;
1186 rwsleep_nsec(&uvn->u_flags, uobj->vmobjlock, PVM, "uvn_iosync",
1187 INFSLP);
1188 }
1189
1190 /* check size */
1191 if (file_offset >= uvn->u_size) {
1192 return VM_PAGER_BAD;
1193 }
1194
1195 /* first try and map the pages in (without waiting) */
1196 mapinflags = (rw == UIO_READ) ?
1197 UVMPAGER_MAPIN_READ : UVMPAGER_MAPIN_WRITE;
1198
1199 kva = uvm_pagermapin(pps, npages, mapinflags);
1200 if (kva == 0 && waitf == M_NOWAIT) {
1201 return VM_PAGER_AGAIN;
1202 }
1203
1204 /*
1205 * ok, now bump u_nio up. at this point we are done with uvn
1206 * and can unlock it. if we still don't have a kva, try again
1207 * (this time with sleep ok).
1208 */
1209 uvn->u_nio++; /* we have an I/O in progress! */
1210 vnlocked = (uvn->u_flags & UVM_VNODE_VNISLOCKED);
1211 uvnsize = uvn->u_size;
1212 rw_exit(uobj->vmobjlock);
1213 if (kva == 0)
1214 kva = uvm_pagermapin(pps, npages,
1215 mapinflags | UVMPAGER_MAPIN_WAITOK);
1216
1217 /*
1218 * ok, mapped in. our pages are PG_BUSY so they are not going to
1219 * get touched (so we can look at "offset" without having to lock
1220 * the object). set up for I/O.
1221 */
1222 /* fill out uio/iov */
1223 iov.iov_base = (caddr_t) kva;
1224 wanted = (size_t)npages << PAGE_SHIFT;
1225 if (file_offset + wanted > uvnsize)
1226 wanted = uvnsize - file_offset; /* XXX: needed? */
1227 iov.iov_len = wanted;
1228 uio.uio_iov = &iov;
1229 uio.uio_iovcnt = 1;
1230 uio.uio_offset = file_offset;
1231 uio.uio_segflg = UIO_SYSSPACE;
1232 uio.uio_rw = rw;
1233 uio.uio_resid = wanted;
1234 uio.uio_procp = curproc;
1235
1236 /*
1237 * This process may already have the NET_LOCK(), if we
1238 * faulted in copyin() or copyout() in the network stack.
1239 */
1240 if (rw_status(&netlock) == RW_WRITE) {
1241 NET_UNLOCK();
1242 netunlocked = 1;
1243 }
1244
1245 /* do the I/O! (XXX: curproc?) */
1246 /*
1247 * This process may already have this vnode locked, if we faulted in
1248 * copyin() or copyout() on a region backed by this vnode
1249 * while doing I/O to the vnode. If this is the case, don't
1250 * panic.. instead, return the error to the user.
1251 *
1252 * XXX this is a stopgap to prevent a panic.
1253 * Ideally, this kind of operation *should* work.
1254 */
1255 result = 0;
1256 KERNEL_LOCK();
1257 if (!vnlocked)
1258 result = vn_lock(vn, LK_EXCLUSIVE | LK_RECURSEFAIL | lkflags);
1259 if (result == 0) {
1260 /* NOTE: vnode now locked! */
1261 if (rw == UIO_READ)
1262 result = VOP_READ(vn, &uio, 0, curproc->p_ucred);
1263 else
1264 result = VOP_WRITE(vn, &uio,
1265 (flags & PGO_PDFREECLUST) ? IO_NOCACHE : 0,
1266 curproc->p_ucred);
1267
1268 if (!vnlocked)
1269 VOP_UNLOCK(vn);
1270
1271 }
1272 KERNEL_UNLOCK();
1273
1274 if (netunlocked)
1275 NET_LOCK();
1276
1277
1278 /* NOTE: vnode now unlocked (unless vnislocked) */
1279 /*
1280 * result == unix style errno (0 == OK!)
1281 *
1282 * zero out rest of buffer (if needed)
1283 */
1284 if (result == 0) {
1285 got = wanted - uio.uio_resid;
1286
1287 if (wanted && got == 0) {
1288 result = EIO; /* XXX: error? */
1289 } else if (got < PAGE_SIZE * npages && rw == UIO_READ) {
1290 memset((void *) (kva + got), 0,
1291 ((size_t)npages << PAGE_SHIFT) - got);
1292 }
1293 }
1294
1295 /* now remove pager mapping */
1296 uvm_pagermapout(kva, npages);
1297
1298 /* now clean up the object (i.e. drop I/O count) */
1299 rw_enter(uobj->vmobjlock, RW_WRITE);
1300 uvn->u_nio--; /* I/O DONE! */
1301 if ((uvn->u_flags & UVM_VNODE_IOSYNC) != 0 && uvn->u_nio == 0) {
1302 wakeup(&uvn->u_nio);
1303 }
1304
1305 if (result == 0) {
1306 return VM_PAGER_OK;
1307 } else if (result == EBUSY) {
1308 KASSERT(flags & PGO_NOWAIT);
1309 return VM_PAGER_AGAIN;
1310 } else {
1311 if (rebooting) {
1312 KERNEL_LOCK();
1313 while (rebooting)
1314 tsleep_nsec(&rebooting, PVM, "uvndead", INFSLP);
1315 KERNEL_UNLOCK();
1316 }
1317 return VM_PAGER_ERROR;
1318 }
1319 }
1320
1321 /*
1322 * uvm_vnp_uncache: disable "persisting" in a vnode... when last reference
1323 * is gone we will kill the object (flushing dirty pages back to the vnode
1324 * if needed).
1325 *
1326 * => returns TRUE if there was no uvm_object attached or if there was
1327 * one and we killed it [i.e. if there is no active uvn]
1328 * => called with the vnode VOP_LOCK'd [we will unlock it for I/O, if
1329 * needed]
1330 *
1331 * => XXX: given that we now kill uvn's when a vnode is recycled (without
1332 * having to hold a reference on the vnode) and given a working
1333 * uvm_vnp_sync(), how does that effect the need for this function?
1334 * [XXXCDC: seems like it can die?]
1335 *
1336 * => XXX: this function should DIE once we merge the VM and buffer
1337 * cache.
1338 *
1339 * research shows that this is called in the following places:
1340 * ext2fs_truncate, ffs_truncate, detrunc[msdosfs]: called when vnode
1341 * changes sizes
1342 * ext2fs_write, WRITE [ufs_readwrite], msdosfs_write: called when we
1343 * are written to
1344 * ex2fs_chmod, ufs_chmod: called if VTEXT vnode and the sticky bit
1345 * is off
1346 * ffs_realloccg: when we can't extend the current block and have
1347 * to allocate a new one we call this [XXX: why?]
1348 * nfsrv_rename, rename_files: called when the target filename is there
1349 * and we want to remove it
1350 * nfsrv_remove, sys_unlink: called on file we are removing
1351 * nfsrv_access: if VTEXT and we want WRITE access and we don't uncache
1352 * then return "text busy"
1353 * nfs_open: seems to uncache any file opened with nfs
1354 * vn_writechk: if VTEXT vnode and can't uncache return "text busy"
1355 * fusefs_open: uncaches any file that is opened
1356 * fusefs_write: uncaches on every write
1357 */
1358
1359 int
uvm_vnp_uncache(struct vnode * vp)1360 uvm_vnp_uncache(struct vnode *vp)
1361 {
1362 struct uvm_vnode *uvn = vp->v_uvm;
1363 struct uvm_object *uobj = &uvn->u_obj;
1364
1365 /* lock uvn part of the vnode and check if we need to do anything */
1366
1367 rw_enter(uobj->vmobjlock, RW_WRITE);
1368 if ((uvn->u_flags & UVM_VNODE_VALID) == 0 ||
1369 (uvn->u_flags & UVM_VNODE_BLOCKED) != 0) {
1370 rw_exit(uobj->vmobjlock);
1371 return TRUE;
1372 }
1373
1374 /*
1375 * we have a valid, non-blocked uvn. clear persist flag.
1376 * if uvn is currently active we can return now.
1377 */
1378 uvn->u_flags &= ~UVM_VNODE_CANPERSIST;
1379 if (uvn->u_obj.uo_refs) {
1380 rw_exit(uobj->vmobjlock);
1381 return FALSE;
1382 }
1383
1384 /*
1385 * uvn is currently persisting! we have to gain a reference to
1386 * it so that we can call uvn_detach to kill the uvn.
1387 */
1388 vref(vp); /* seems ok, even with VOP_LOCK */
1389 uvn->u_obj.uo_refs++; /* value is now 1 */
1390 rw_exit(uobj->vmobjlock);
1391
1392 #ifdef VFSLCKDEBUG
1393 /*
1394 * carry over sanity check from old vnode pager: the vnode should
1395 * be VOP_LOCK'd, and we confirm it here.
1396 */
1397 if ((vp->v_flag & VLOCKSWORK) && !VOP_ISLOCKED(vp))
1398 panic("uvm_vnp_uncache: vnode not locked!");
1399 #endif
1400
1401 /*
1402 * now drop our reference to the vnode. if we have the sole
1403 * reference to the vnode then this will cause it to die [as we
1404 * just cleared the persist flag]. we have to unlock the vnode
1405 * while we are doing this as it may trigger I/O.
1406 *
1407 * XXX: it might be possible for uvn to get reclaimed while we are
1408 * unlocked causing us to return TRUE when we should not. we ignore
1409 * this as a false-positive return value doesn't hurt us.
1410 */
1411 VOP_UNLOCK(vp);
1412 uvn_detach(&uvn->u_obj);
1413 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1414
1415 return TRUE;
1416 }
1417
1418 /*
1419 * uvm_vnp_setsize: grow or shrink a vnode uvn
1420 *
1421 * grow => just update size value
1422 * shrink => toss un-needed pages
1423 *
1424 * => we assume that the caller has a reference of some sort to the
1425 * vnode in question so that it will not be yanked out from under
1426 * us.
1427 *
1428 * called from:
1429 * => truncate fns (ext2fs_truncate, ffs_truncate, detrunc[msdos],
1430 * fusefs_setattr)
1431 * => "write" fns (ext2fs_write, WRITE [ufs/ufs], msdosfs_write, nfs_write
1432 * fusefs_write)
1433 * => ffs_balloc [XXX: why? doesn't WRITE handle?]
1434 * => NFS: nfs_loadattrcache, nfs_getattrcache, nfs_setattr
1435 * => union fs: union_newsize
1436 */
1437
1438 void
uvm_vnp_setsize(struct vnode * vp,off_t newsize)1439 uvm_vnp_setsize(struct vnode *vp, off_t newsize)
1440 {
1441 struct uvm_vnode *uvn = vp->v_uvm;
1442 struct uvm_object *uobj = &uvn->u_obj;
1443
1444 KERNEL_ASSERT_LOCKED();
1445
1446 rw_enter(uobj->vmobjlock, RW_WRITE);
1447
1448 /* lock uvn and check for valid object, and if valid: do it! */
1449 if (uvn->u_flags & UVM_VNODE_VALID) {
1450
1451 /*
1452 * now check if the size has changed: if we shrink we had better
1453 * toss some pages...
1454 */
1455
1456 if (uvn->u_size > newsize) {
1457 (void)uvn_flush(&uvn->u_obj, newsize,
1458 uvn->u_size, PGO_FREE);
1459 }
1460 uvn->u_size = newsize;
1461 }
1462 rw_exit(uobj->vmobjlock);
1463 }
1464
1465 /*
1466 * uvm_vnp_sync: flush all dirty VM pages back to their backing vnodes.
1467 *
1468 * => called from sys_sync with no VM structures locked
1469 * => only one process can do a sync at a time (because the uvn
1470 * structure only has one queue for sync'ing). we ensure this
1471 * by holding the uvn_sync_lock while the sync is in progress.
1472 * other processes attempting a sync will sleep on this lock
1473 * until we are done.
1474 */
1475 void
uvm_vnp_sync(struct mount * mp)1476 uvm_vnp_sync(struct mount *mp)
1477 {
1478 struct uvm_vnode *uvn;
1479 struct vnode *vp;
1480
1481 /*
1482 * step 1: ensure we are only ones using the uvn_sync_q by locking
1483 * our lock...
1484 */
1485 rw_enter_write(&uvn_sync_lock);
1486
1487 /*
1488 * step 2: build up a simpleq of uvns of interest based on the
1489 * write list. we gain a reference to uvns of interest.
1490 */
1491 SIMPLEQ_INIT(&uvn_sync_q);
1492 LIST_FOREACH(uvn, &uvn_wlist, u_wlist) {
1493 vp = uvn->u_vnode;
1494 if (mp && vp->v_mount != mp)
1495 continue;
1496
1497 /*
1498 * If the vnode is "blocked" it means it must be dying, which
1499 * in turn means its in the process of being flushed out so
1500 * we can safely skip it.
1501 *
1502 * note that uvn must already be valid because we found it on
1503 * the wlist (this also means it can't be ALOCK'd).
1504 */
1505 if ((uvn->u_flags & UVM_VNODE_BLOCKED) != 0)
1506 continue;
1507
1508 /*
1509 * gain reference. watch out for persisting uvns (need to
1510 * regain vnode REF).
1511 */
1512 if (uvn->u_obj.uo_refs == 0)
1513 vref(vp);
1514 uvn->u_obj.uo_refs++;
1515
1516 SIMPLEQ_INSERT_HEAD(&uvn_sync_q, uvn, u_syncq);
1517 }
1518
1519 /* step 3: we now have a list of uvn's that may need cleaning. */
1520 SIMPLEQ_FOREACH(uvn, &uvn_sync_q, u_syncq) {
1521 rw_enter(uvn->u_obj.vmobjlock, RW_WRITE);
1522 #ifdef DEBUG
1523 if (uvn->u_flags & UVM_VNODE_DYING) {
1524 printf("uvm_vnp_sync: dying vnode on sync list\n");
1525 }
1526 #endif
1527 uvn_flush(&uvn->u_obj, 0, 0, PGO_CLEANIT|PGO_ALLPAGES|PGO_DOACTCLUST);
1528
1529 /*
1530 * if we have the only reference and we just cleaned the uvn,
1531 * then we can pull it out of the UVM_VNODE_WRITEABLE state
1532 * thus allowing us to avoid thinking about flushing it again
1533 * on later sync ops.
1534 */
1535 if (uvn->u_obj.uo_refs == 1 &&
1536 (uvn->u_flags & UVM_VNODE_WRITEABLE)) {
1537 LIST_REMOVE(uvn, u_wlist);
1538 uvn->u_flags &= ~UVM_VNODE_WRITEABLE;
1539 }
1540 rw_exit(uvn->u_obj.vmobjlock);
1541
1542 /* now drop our reference to the uvn */
1543 uvn_detach(&uvn->u_obj);
1544 }
1545
1546 rw_exit_write(&uvn_sync_lock);
1547 }
1548