1 /*-
2 * Copyright (c) 1990 University of Utah.
3 * Copyright (c) 1991 The Regents of the University of California.
4 * All rights reserved.
5 * Copyright (c) 1993, 1994 John S. Dyson
6 * Copyright (c) 1995, David Greenman
7 *
8 * This code is derived from software contributed to Berkeley by
9 * the Systems Programming Group of the University of Utah Computer
10 * Science Department.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. 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 * from: @(#)vnode_pager.c 7.5 (Berkeley) 4/20/91
41 */
42
43 /*
44 * Page to/from files (vnodes).
45 */
46
47 /*
48 * TODO:
49 * Implement VOP_GETPAGES/PUTPAGES interface for filesystems. Will
50 * greatly re-simplify the vnode_pager.
51 */
52
53 #include <sys/cdefs.h>
54 __FBSDID("$FreeBSD$");
55
56 #include "opt_vm.h"
57
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/proc.h>
61 #include <sys/vnode.h>
62 #include <sys/mount.h>
63 #include <sys/bio.h>
64 #include <sys/buf.h>
65 #include <sys/vmmeter.h>
66 #include <sys/limits.h>
67 #include <sys/conf.h>
68 #include <sys/rwlock.h>
69 #include <sys/sf_buf.h>
70
71 #include <machine/atomic.h>
72
73 #include <vm/vm.h>
74 #include <vm/vm_param.h>
75 #include <vm/vm_object.h>
76 #include <vm/vm_page.h>
77 #include <vm/vm_pager.h>
78 #include <vm/vm_map.h>
79 #include <vm/vnode_pager.h>
80 #include <vm/vm_extern.h>
81
82 static int vnode_pager_addr(struct vnode *vp, vm_ooffset_t address,
83 daddr_t *rtaddress, int *run);
84 static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m);
85 static int vnode_pager_input_old(vm_object_t object, vm_page_t m);
86 static void vnode_pager_dealloc(vm_object_t);
87 static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *);
88 static int vnode_pager_getpages_async(vm_object_t, vm_page_t *, int, int *,
89 int *, vop_getpages_iodone_t, void *);
90 static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, int, int *);
91 static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
92 static vm_object_t vnode_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
93 vm_ooffset_t, struct ucred *cred);
94 static int vnode_pager_generic_getpages_done(struct buf *);
95 static void vnode_pager_generic_getpages_done_async(struct buf *);
96
97 struct pagerops vnodepagerops = {
98 .pgo_alloc = vnode_pager_alloc,
99 .pgo_dealloc = vnode_pager_dealloc,
100 .pgo_getpages = vnode_pager_getpages,
101 .pgo_getpages_async = vnode_pager_getpages_async,
102 .pgo_putpages = vnode_pager_putpages,
103 .pgo_haspage = vnode_pager_haspage,
104 };
105
106 int vnode_pbuf_freecnt;
107 int vnode_async_pbuf_freecnt;
108
109 /* Create the VM system backing object for this vnode */
110 int
vnode_create_vobject(struct vnode * vp,off_t isize,struct thread * td)111 vnode_create_vobject(struct vnode *vp, off_t isize, struct thread *td)
112 {
113 vm_object_t object;
114 vm_ooffset_t size = isize;
115 struct vattr va;
116
117 if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE)
118 return (0);
119
120 while ((object = vp->v_object) != NULL) {
121 VM_OBJECT_WLOCK(object);
122 if (!(object->flags & OBJ_DEAD)) {
123 VM_OBJECT_WUNLOCK(object);
124 return (0);
125 }
126 VOP_UNLOCK(vp, 0);
127 vm_object_set_flag(object, OBJ_DISCONNECTWNT);
128 VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vodead", 0);
129 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
130 }
131
132 if (size == 0) {
133 if (vn_isdisk(vp, NULL)) {
134 size = IDX_TO_OFF(INT_MAX);
135 } else {
136 if (VOP_GETATTR(vp, &va, td->td_ucred))
137 return (0);
138 size = va.va_size;
139 }
140 }
141
142 object = vnode_pager_alloc(vp, size, 0, 0, td->td_ucred);
143 /*
144 * Dereference the reference we just created. This assumes
145 * that the object is associated with the vp.
146 */
147 VM_OBJECT_WLOCK(object);
148 object->ref_count--;
149 VM_OBJECT_WUNLOCK(object);
150 vrele(vp);
151
152 KASSERT(vp->v_object != NULL, ("vnode_create_vobject: NULL object"));
153
154 return (0);
155 }
156
157 void
vnode_destroy_vobject(struct vnode * vp)158 vnode_destroy_vobject(struct vnode *vp)
159 {
160 struct vm_object *obj;
161
162 obj = vp->v_object;
163 if (obj == NULL)
164 return;
165 ASSERT_VOP_ELOCKED(vp, "vnode_destroy_vobject");
166 VM_OBJECT_WLOCK(obj);
167 if (obj->ref_count == 0) {
168 /*
169 * don't double-terminate the object
170 */
171 if ((obj->flags & OBJ_DEAD) == 0)
172 vm_object_terminate(obj);
173 else
174 VM_OBJECT_WUNLOCK(obj);
175 } else {
176 /*
177 * Woe to the process that tries to page now :-).
178 */
179 vm_pager_deallocate(obj);
180 VM_OBJECT_WUNLOCK(obj);
181 }
182 vp->v_object = NULL;
183 }
184
185
186 /*
187 * Allocate (or lookup) pager for a vnode.
188 * Handle is a vnode pointer.
189 *
190 * MPSAFE
191 */
192 vm_object_t
vnode_pager_alloc(void * handle,vm_ooffset_t size,vm_prot_t prot,vm_ooffset_t offset,struct ucred * cred)193 vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
194 vm_ooffset_t offset, struct ucred *cred)
195 {
196 vm_object_t object;
197 struct vnode *vp;
198
199 /*
200 * Pageout to vnode, no can do yet.
201 */
202 if (handle == NULL)
203 return (NULL);
204
205 vp = (struct vnode *) handle;
206
207 /*
208 * If the object is being terminated, wait for it to
209 * go away.
210 */
211 retry:
212 while ((object = vp->v_object) != NULL) {
213 VM_OBJECT_WLOCK(object);
214 if ((object->flags & OBJ_DEAD) == 0)
215 break;
216 vm_object_set_flag(object, OBJ_DISCONNECTWNT);
217 VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vadead", 0);
218 }
219
220 KASSERT(vp->v_usecount != 0, ("vnode_pager_alloc: no vnode reference"));
221
222 if (object == NULL) {
223 /*
224 * Add an object of the appropriate size
225 */
226 object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size)));
227
228 object->un_pager.vnp.vnp_size = size;
229 object->un_pager.vnp.writemappings = 0;
230
231 object->handle = handle;
232 VI_LOCK(vp);
233 if (vp->v_object != NULL) {
234 /*
235 * Object has been created while we were sleeping
236 */
237 VI_UNLOCK(vp);
238 VM_OBJECT_WLOCK(object);
239 KASSERT(object->ref_count == 1,
240 ("leaked ref %p %d", object, object->ref_count));
241 object->type = OBJT_DEAD;
242 object->ref_count = 0;
243 VM_OBJECT_WUNLOCK(object);
244 vm_object_destroy(object);
245 goto retry;
246 }
247 vp->v_object = object;
248 VI_UNLOCK(vp);
249 } else {
250 object->ref_count++;
251 #if VM_NRESERVLEVEL > 0
252 vm_object_color(object, 0);
253 #endif
254 VM_OBJECT_WUNLOCK(object);
255 }
256 vref(vp);
257 return (object);
258 }
259
260 /*
261 * The object must be locked.
262 */
263 static void
vnode_pager_dealloc(vm_object_t object)264 vnode_pager_dealloc(vm_object_t object)
265 {
266 struct vnode *vp;
267 int refs;
268
269 vp = object->handle;
270 if (vp == NULL)
271 panic("vnode_pager_dealloc: pager already dealloced");
272
273 VM_OBJECT_ASSERT_WLOCKED(object);
274 vm_object_pip_wait(object, "vnpdea");
275 refs = object->ref_count;
276
277 object->handle = NULL;
278 object->type = OBJT_DEAD;
279 if (object->flags & OBJ_DISCONNECTWNT) {
280 vm_object_clear_flag(object, OBJ_DISCONNECTWNT);
281 wakeup(object);
282 }
283 ASSERT_VOP_ELOCKED(vp, "vnode_pager_dealloc");
284 if (object->un_pager.vnp.writemappings > 0) {
285 object->un_pager.vnp.writemappings = 0;
286 VOP_ADD_WRITECOUNT(vp, -1);
287 CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
288 __func__, vp, vp->v_writecount);
289 }
290 vp->v_object = NULL;
291 VOP_UNSET_TEXT(vp);
292 VM_OBJECT_WUNLOCK(object);
293 while (refs-- > 0)
294 vunref(vp);
295 VM_OBJECT_WLOCK(object);
296 }
297
298 static boolean_t
vnode_pager_haspage(vm_object_t object,vm_pindex_t pindex,int * before,int * after)299 vnode_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
300 int *after)
301 {
302 struct vnode *vp = object->handle;
303 daddr_t bn;
304 int err;
305 daddr_t reqblock;
306 int poff;
307 int bsize;
308 int pagesperblock, blocksperpage;
309
310 VM_OBJECT_ASSERT_WLOCKED(object);
311 /*
312 * If no vp or vp is doomed or marked transparent to VM, we do not
313 * have the page.
314 */
315 if (vp == NULL || vp->v_iflag & VI_DOOMED)
316 return FALSE;
317 /*
318 * If the offset is beyond end of file we do
319 * not have the page.
320 */
321 if (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)
322 return FALSE;
323
324 bsize = vp->v_mount->mnt_stat.f_iosize;
325 pagesperblock = bsize / PAGE_SIZE;
326 blocksperpage = 0;
327 if (pagesperblock > 0) {
328 reqblock = pindex / pagesperblock;
329 } else {
330 blocksperpage = (PAGE_SIZE / bsize);
331 reqblock = pindex * blocksperpage;
332 }
333 VM_OBJECT_WUNLOCK(object);
334 err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before);
335 VM_OBJECT_WLOCK(object);
336 if (err)
337 return TRUE;
338 if (bn == -1)
339 return FALSE;
340 if (pagesperblock > 0) {
341 poff = pindex - (reqblock * pagesperblock);
342 if (before) {
343 *before *= pagesperblock;
344 *before += poff;
345 }
346 if (after) {
347 /*
348 * The BMAP vop can report a partial block in the
349 * 'after', but must not report blocks after EOF.
350 * Assert the latter, and truncate 'after' in case
351 * of the former.
352 */
353 KASSERT((reqblock + *after) * pagesperblock <
354 roundup2(object->size, pagesperblock),
355 ("%s: reqblock %jd after %d size %ju", __func__,
356 (intmax_t )reqblock, *after,
357 (uintmax_t )object->size));
358 *after *= pagesperblock;
359 *after += pagesperblock - (poff + 1);
360 if (pindex + *after >= object->size)
361 *after = object->size - 1 - pindex;
362 }
363 } else {
364 if (before) {
365 *before /= blocksperpage;
366 }
367
368 if (after) {
369 *after /= blocksperpage;
370 }
371 }
372 return TRUE;
373 }
374
375 /*
376 * Lets the VM system know about a change in size for a file.
377 * We adjust our own internal size and flush any cached pages in
378 * the associated object that are affected by the size change.
379 *
380 * Note: this routine may be invoked as a result of a pager put
381 * operation (possibly at object termination time), so we must be careful.
382 */
383 void
vnode_pager_setsize(struct vnode * vp,vm_ooffset_t nsize)384 vnode_pager_setsize(struct vnode *vp, vm_ooffset_t nsize)
385 {
386 vm_object_t object;
387 vm_page_t m;
388 vm_pindex_t nobjsize;
389
390 if ((object = vp->v_object) == NULL)
391 return;
392 /* ASSERT_VOP_ELOCKED(vp, "vnode_pager_setsize and not locked vnode"); */
393 VM_OBJECT_WLOCK(object);
394 if (object->type == OBJT_DEAD) {
395 VM_OBJECT_WUNLOCK(object);
396 return;
397 }
398 KASSERT(object->type == OBJT_VNODE,
399 ("not vnode-backed object %p", object));
400 if (nsize == object->un_pager.vnp.vnp_size) {
401 /*
402 * Hasn't changed size
403 */
404 VM_OBJECT_WUNLOCK(object);
405 return;
406 }
407 nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
408 if (nsize < object->un_pager.vnp.vnp_size) {
409 /*
410 * File has shrunk. Toss any cached pages beyond the new EOF.
411 */
412 if (nobjsize < object->size)
413 vm_object_page_remove(object, nobjsize, object->size,
414 0);
415 /*
416 * this gets rid of garbage at the end of a page that is now
417 * only partially backed by the vnode.
418 *
419 * XXX for some reason (I don't know yet), if we take a
420 * completely invalid page and mark it partially valid
421 * it can screw up NFS reads, so we don't allow the case.
422 */
423 if ((nsize & PAGE_MASK) &&
424 (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL &&
425 m->valid != 0) {
426 int base = (int)nsize & PAGE_MASK;
427 int size = PAGE_SIZE - base;
428
429 /*
430 * Clear out partial-page garbage in case
431 * the page has been mapped.
432 */
433 pmap_zero_page_area(m, base, size);
434
435 /*
436 * Update the valid bits to reflect the blocks that
437 * have been zeroed. Some of these valid bits may
438 * have already been set.
439 */
440 vm_page_set_valid_range(m, base, size);
441
442 /*
443 * Round "base" to the next block boundary so that the
444 * dirty bit for a partially zeroed block is not
445 * cleared.
446 */
447 base = roundup2(base, DEV_BSIZE);
448
449 /*
450 * Clear out partial-page dirty bits.
451 *
452 * note that we do not clear out the valid
453 * bits. This would prevent bogus_page
454 * replacement from working properly.
455 */
456 vm_page_clear_dirty(m, base, PAGE_SIZE - base);
457 }
458 }
459 object->un_pager.vnp.vnp_size = nsize;
460 object->size = nobjsize;
461 VM_OBJECT_WUNLOCK(object);
462 }
463
464 /*
465 * calculate the linear (byte) disk address of specified virtual
466 * file address
467 */
468 static int
vnode_pager_addr(struct vnode * vp,vm_ooffset_t address,daddr_t * rtaddress,int * run)469 vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress,
470 int *run)
471 {
472 int bsize;
473 int err;
474 daddr_t vblock;
475 daddr_t voffset;
476
477 if (address < 0)
478 return -1;
479
480 if (vp->v_iflag & VI_DOOMED)
481 return -1;
482
483 bsize = vp->v_mount->mnt_stat.f_iosize;
484 vblock = address / bsize;
485 voffset = address % bsize;
486
487 err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL);
488 if (err == 0) {
489 if (*rtaddress != -1)
490 *rtaddress += voffset / DEV_BSIZE;
491 if (run) {
492 *run += 1;
493 *run *= bsize/PAGE_SIZE;
494 *run -= voffset/PAGE_SIZE;
495 }
496 }
497
498 return (err);
499 }
500
501 /*
502 * small block filesystem vnode pager input
503 */
504 static int
vnode_pager_input_smlfs(vm_object_t object,vm_page_t m)505 vnode_pager_input_smlfs(vm_object_t object, vm_page_t m)
506 {
507 struct vnode *vp;
508 struct bufobj *bo;
509 struct buf *bp;
510 struct sf_buf *sf;
511 daddr_t fileaddr;
512 vm_offset_t bsize;
513 vm_page_bits_t bits;
514 int error, i;
515
516 error = 0;
517 vp = object->handle;
518 if (vp->v_iflag & VI_DOOMED)
519 return VM_PAGER_BAD;
520
521 bsize = vp->v_mount->mnt_stat.f_iosize;
522
523 VOP_BMAP(vp, 0, &bo, 0, NULL, NULL);
524
525 sf = sf_buf_alloc(m, 0);
526
527 for (i = 0; i < PAGE_SIZE / bsize; i++) {
528 vm_ooffset_t address;
529
530 bits = vm_page_bits(i * bsize, bsize);
531 if (m->valid & bits)
532 continue;
533
534 address = IDX_TO_OFF(m->pindex) + i * bsize;
535 if (address >= object->un_pager.vnp.vnp_size) {
536 fileaddr = -1;
537 } else {
538 error = vnode_pager_addr(vp, address, &fileaddr, NULL);
539 if (error)
540 break;
541 }
542 if (fileaddr != -1) {
543 bp = getpbuf(&vnode_pbuf_freecnt);
544
545 /* build a minimal buffer header */
546 bp->b_iocmd = BIO_READ;
547 bp->b_iodone = bdone;
548 KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
549 KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
550 bp->b_rcred = crhold(curthread->td_ucred);
551 bp->b_wcred = crhold(curthread->td_ucred);
552 bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize;
553 bp->b_blkno = fileaddr;
554 pbgetbo(bo, bp);
555 bp->b_vp = vp;
556 bp->b_bcount = bsize;
557 bp->b_bufsize = bsize;
558 bp->b_runningbufspace = bp->b_bufsize;
559 atomic_add_long(&runningbufspace, bp->b_runningbufspace);
560
561 /* do the input */
562 bp->b_iooffset = dbtob(bp->b_blkno);
563 bstrategy(bp);
564
565 bwait(bp, PVM, "vnsrd");
566
567 if ((bp->b_ioflags & BIO_ERROR) != 0)
568 error = EIO;
569
570 /*
571 * free the buffer header back to the swap buffer pool
572 */
573 bp->b_vp = NULL;
574 pbrelbo(bp);
575 relpbuf(bp, &vnode_pbuf_freecnt);
576 if (error)
577 break;
578 } else
579 bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize);
580 KASSERT((m->dirty & bits) == 0,
581 ("vnode_pager_input_smlfs: page %p is dirty", m));
582 VM_OBJECT_WLOCK(object);
583 m->valid |= bits;
584 VM_OBJECT_WUNLOCK(object);
585 }
586 sf_buf_free(sf);
587 if (error) {
588 return VM_PAGER_ERROR;
589 }
590 return VM_PAGER_OK;
591 }
592
593 /*
594 * old style vnode pager input routine
595 */
596 static int
vnode_pager_input_old(vm_object_t object,vm_page_t m)597 vnode_pager_input_old(vm_object_t object, vm_page_t m)
598 {
599 struct uio auio;
600 struct iovec aiov;
601 int error;
602 int size;
603 struct sf_buf *sf;
604 struct vnode *vp;
605
606 VM_OBJECT_ASSERT_WLOCKED(object);
607 error = 0;
608
609 /*
610 * Return failure if beyond current EOF
611 */
612 if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
613 return VM_PAGER_BAD;
614 } else {
615 size = PAGE_SIZE;
616 if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
617 size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
618 vp = object->handle;
619 VM_OBJECT_WUNLOCK(object);
620
621 /*
622 * Allocate a kernel virtual address and initialize so that
623 * we can use VOP_READ/WRITE routines.
624 */
625 sf = sf_buf_alloc(m, 0);
626
627 aiov.iov_base = (caddr_t)sf_buf_kva(sf);
628 aiov.iov_len = size;
629 auio.uio_iov = &aiov;
630 auio.uio_iovcnt = 1;
631 auio.uio_offset = IDX_TO_OFF(m->pindex);
632 auio.uio_segflg = UIO_SYSSPACE;
633 auio.uio_rw = UIO_READ;
634 auio.uio_resid = size;
635 auio.uio_td = curthread;
636
637 error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
638 if (!error) {
639 int count = size - auio.uio_resid;
640
641 if (count == 0)
642 error = EINVAL;
643 else if (count != PAGE_SIZE)
644 bzero((caddr_t)sf_buf_kva(sf) + count,
645 PAGE_SIZE - count);
646 }
647 sf_buf_free(sf);
648
649 VM_OBJECT_WLOCK(object);
650 }
651 KASSERT(m->dirty == 0, ("vnode_pager_input_old: page %p is dirty", m));
652 if (!error)
653 m->valid = VM_PAGE_BITS_ALL;
654 return error ? VM_PAGER_ERROR : VM_PAGER_OK;
655 }
656
657 /*
658 * generic vnode pager input routine
659 */
660
661 /*
662 * Local media VFS's that do not implement their own VOP_GETPAGES
663 * should have their VOP_GETPAGES call to vnode_pager_generic_getpages()
664 * to implement the previous behaviour.
665 *
666 * All other FS's should use the bypass to get to the local media
667 * backing vp's VOP_GETPAGES.
668 */
669 static int
vnode_pager_getpages(vm_object_t object,vm_page_t * m,int count,int * rbehind,int * rahead)670 vnode_pager_getpages(vm_object_t object, vm_page_t *m, int count, int *rbehind,
671 int *rahead)
672 {
673 struct vnode *vp;
674 int rtval;
675
676 vp = object->handle;
677 VM_OBJECT_WUNLOCK(object);
678 rtval = VOP_GETPAGES(vp, m, count, rbehind, rahead);
679 KASSERT(rtval != EOPNOTSUPP,
680 ("vnode_pager: FS getpages not implemented\n"));
681 VM_OBJECT_WLOCK(object);
682 return rtval;
683 }
684
685 static int
vnode_pager_getpages_async(vm_object_t object,vm_page_t * m,int count,int * rbehind,int * rahead,vop_getpages_iodone_t iodone,void * arg)686 vnode_pager_getpages_async(vm_object_t object, vm_page_t *m, int count,
687 int *rbehind, int *rahead, vop_getpages_iodone_t iodone, void *arg)
688 {
689 struct vnode *vp;
690 int rtval;
691
692 vp = object->handle;
693 VM_OBJECT_WUNLOCK(object);
694 rtval = VOP_GETPAGES_ASYNC(vp, m, count, rbehind, rahead, iodone, arg);
695 KASSERT(rtval != EOPNOTSUPP,
696 ("vnode_pager: FS getpages_async not implemented\n"));
697 VM_OBJECT_WLOCK(object);
698 return (rtval);
699 }
700
701 /*
702 * The implementation of VOP_GETPAGES() and VOP_GETPAGES_ASYNC() for
703 * local filesystems, where partially valid pages can only occur at
704 * the end of file.
705 */
706 int
vnode_pager_local_getpages(struct vop_getpages_args * ap)707 vnode_pager_local_getpages(struct vop_getpages_args *ap)
708 {
709
710 return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
711 ap->a_rbehind, ap->a_rahead, NULL, NULL));
712 }
713
714 int
vnode_pager_local_getpages_async(struct vop_getpages_async_args * ap)715 vnode_pager_local_getpages_async(struct vop_getpages_async_args *ap)
716 {
717
718 return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
719 ap->a_rbehind, ap->a_rahead, ap->a_iodone, ap->a_arg));
720 }
721
722 /*
723 * This is now called from local media FS's to operate against their
724 * own vnodes if they fail to implement VOP_GETPAGES.
725 */
726 int
vnode_pager_generic_getpages(struct vnode * vp,vm_page_t * m,int count,int * a_rbehind,int * a_rahead,vop_getpages_iodone_t iodone,void * arg)727 vnode_pager_generic_getpages(struct vnode *vp, vm_page_t *m, int count,
728 int *a_rbehind, int *a_rahead, vop_getpages_iodone_t iodone, void *arg)
729 {
730 vm_object_t object;
731 struct bufobj *bo;
732 struct buf *bp;
733 off_t foff;
734 int bsize, pagesperblock, *freecnt;
735 int error, before, after, rbehind, rahead, poff, i;
736 int bytecount, secmask;
737
738 KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
739 ("%s does not support devices", __func__));
740
741 if (vp->v_iflag & VI_DOOMED)
742 return (VM_PAGER_BAD);
743
744 object = vp->v_object;
745 foff = IDX_TO_OFF(m[0]->pindex);
746 bsize = vp->v_mount->mnt_stat.f_iosize;
747 pagesperblock = bsize / PAGE_SIZE;
748
749 KASSERT(foff < object->un_pager.vnp.vnp_size,
750 ("%s: page %p offset beyond vp %p size", __func__, m[0], vp));
751 KASSERT(count <= sizeof(bp->b_pages),
752 ("%s: requested %d pages", __func__, count));
753
754 /*
755 * The last page has valid blocks. Invalid part can only
756 * exist at the end of file, and the page is made fully valid
757 * by zeroing in vm_pager_get_pages().
758 */
759 if (m[count - 1]->valid != 0 && --count == 0) {
760 if (iodone != NULL)
761 iodone(arg, m, 1, 0);
762 return (VM_PAGER_OK);
763 }
764
765 /*
766 * Synchronous and asynchronous paging operations use different
767 * free pbuf counters. This is done to avoid asynchronous requests
768 * to consume all pbufs.
769 * Allocate the pbuf at the very beginning of the function, so that
770 * if we are low on certain kind of pbufs don't even proceed to BMAP,
771 * but sleep.
772 */
773 freecnt = iodone != NULL ?
774 &vnode_async_pbuf_freecnt : &vnode_pbuf_freecnt;
775 bp = getpbuf(freecnt);
776
777 /*
778 * Get the underlying device blocks for the file with VOP_BMAP().
779 * If the file system doesn't support VOP_BMAP, use old way of
780 * getting pages via VOP_READ.
781 */
782 error = VOP_BMAP(vp, foff / bsize, &bo, &bp->b_blkno, &after, &before);
783 if (error == EOPNOTSUPP) {
784 relpbuf(bp, freecnt);
785 VM_OBJECT_WLOCK(object);
786 for (i = 0; i < count; i++) {
787 PCPU_INC(cnt.v_vnodein);
788 PCPU_INC(cnt.v_vnodepgsin);
789 error = vnode_pager_input_old(object, m[i]);
790 if (error)
791 break;
792 }
793 VM_OBJECT_WUNLOCK(object);
794 return (error);
795 } else if (error != 0) {
796 relpbuf(bp, freecnt);
797 return (VM_PAGER_ERROR);
798 }
799
800 /*
801 * If the file system supports BMAP, but blocksize is smaller
802 * than a page size, then use special small filesystem code.
803 */
804 if (pagesperblock == 0) {
805 for (i = 0; i < count; i++) {
806 PCPU_INC(cnt.v_vnodein);
807 PCPU_INC(cnt.v_vnodepgsin);
808 error = vnode_pager_input_smlfs(object, m[i]);
809 if (error)
810 break;
811 }
812 return (error);
813 }
814
815 /*
816 * A sparse file can be encountered only for a single page request,
817 * which may not be preceeded by call to vm_pager_haspage().
818 */
819 if (bp->b_blkno == -1) {
820 KASSERT(count == 1,
821 ("%s: array[%d] request to a sparse file %p", __func__,
822 count, vp));
823 relpbuf(bp, freecnt);
824 pmap_zero_page(m[0]);
825 KASSERT(m[0]->dirty == 0, ("%s: page %p is dirty",
826 __func__, m[0]));
827 VM_OBJECT_WLOCK(object);
828 m[0]->valid = VM_PAGE_BITS_ALL;
829 VM_OBJECT_WUNLOCK(object);
830 return (VM_PAGER_OK);
831 }
832
833 bp->b_blkno += (foff % bsize) / DEV_BSIZE;
834
835 /* Recalculate blocks available after/before to pages. */
836 poff = (foff % bsize) / PAGE_SIZE;
837 before *= pagesperblock;
838 before += poff;
839 after *= pagesperblock;
840 after += pagesperblock - (poff + 1);
841 if (m[0]->pindex + after >= object->size)
842 after = object->size - 1 - m[0]->pindex;
843 KASSERT(count <= after + 1, ("%s: %d pages asked, can do only %d",
844 __func__, count, after + 1));
845 after -= count - 1;
846
847 /* Trim requested rbehind/rahead to possible values. */
848 rbehind = a_rbehind ? *a_rbehind : 0;
849 rahead = a_rahead ? *a_rahead : 0;
850 rbehind = min(rbehind, before);
851 rbehind = min(rbehind, m[0]->pindex);
852 rahead = min(rahead, after);
853 rahead = min(rahead, object->size - m[count - 1]->pindex);
854 KASSERT(rbehind + rahead + count <= sizeof(bp->b_pages),
855 ("%s: behind %d ahead %d count %d", __func__,
856 rbehind, rahead, count));
857
858 /*
859 * Fill in the bp->b_pages[] array with requested and optional
860 * read behind or read ahead pages. Read behind pages are looked
861 * up in a backward direction, down to a first cached page. Same
862 * for read ahead pages, but there is no need to shift the array
863 * in case of encountering a cached page.
864 */
865 i = bp->b_npages = 0;
866 if (rbehind) {
867 vm_pindex_t startpindex, tpindex;
868 vm_page_t p;
869
870 VM_OBJECT_WLOCK(object);
871 startpindex = m[0]->pindex - rbehind;
872 if ((p = TAILQ_PREV(m[0], pglist, listq)) != NULL &&
873 p->pindex >= startpindex)
874 startpindex = p->pindex + 1;
875
876 /* tpindex is unsigned; beware of numeric underflow. */
877 for (tpindex = m[0]->pindex - 1;
878 tpindex >= startpindex && tpindex < m[0]->pindex;
879 tpindex--, i++) {
880 p = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
881 if (p == NULL) {
882 /* Shift the array. */
883 for (int j = 0; j < i; j++)
884 bp->b_pages[j] = bp->b_pages[j +
885 tpindex + 1 - startpindex];
886 break;
887 }
888 bp->b_pages[tpindex - startpindex] = p;
889 }
890
891 bp->b_pgbefore = i;
892 bp->b_npages += i;
893 bp->b_blkno -= IDX_TO_OFF(i) / DEV_BSIZE;
894 } else
895 bp->b_pgbefore = 0;
896
897 /* Requested pages. */
898 for (int j = 0; j < count; j++, i++)
899 bp->b_pages[i] = m[j];
900 bp->b_npages += count;
901
902 if (rahead) {
903 vm_pindex_t endpindex, tpindex;
904 vm_page_t p;
905
906 if (!VM_OBJECT_WOWNED(object))
907 VM_OBJECT_WLOCK(object);
908 endpindex = m[count - 1]->pindex + rahead + 1;
909 if ((p = TAILQ_NEXT(m[count - 1], listq)) != NULL &&
910 p->pindex < endpindex)
911 endpindex = p->pindex;
912 if (endpindex > object->size)
913 endpindex = object->size;
914
915 for (tpindex = m[count - 1]->pindex + 1;
916 tpindex < endpindex; i++, tpindex++) {
917 p = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
918 if (p == NULL)
919 break;
920 bp->b_pages[i] = p;
921 }
922
923 bp->b_pgafter = i - bp->b_npages;
924 bp->b_npages = i;
925 } else
926 bp->b_pgafter = 0;
927
928 if (VM_OBJECT_WOWNED(object))
929 VM_OBJECT_WUNLOCK(object);
930
931 /* Report back actual behind/ahead read. */
932 if (a_rbehind)
933 *a_rbehind = bp->b_pgbefore;
934 if (a_rahead)
935 *a_rahead = bp->b_pgafter;
936
937 KASSERT(bp->b_npages <= sizeof(bp->b_pages),
938 ("%s: buf %p overflowed", __func__, bp));
939
940 /*
941 * Recalculate first offset and bytecount with regards to read behind.
942 * Truncate bytecount to vnode real size and round up physical size
943 * for real devices.
944 */
945 foff = IDX_TO_OFF(bp->b_pages[0]->pindex);
946 bytecount = bp->b_npages << PAGE_SHIFT;
947 if ((foff + bytecount) > object->un_pager.vnp.vnp_size)
948 bytecount = object->un_pager.vnp.vnp_size - foff;
949 secmask = bo->bo_bsize - 1;
950 KASSERT(secmask < PAGE_SIZE && secmask > 0,
951 ("%s: sector size %d too large", __func__, secmask + 1));
952 bytecount = (bytecount + secmask) & ~secmask;
953
954 /*
955 * And map the pages to be read into the kva, if the filesystem
956 * requires mapped buffers.
957 */
958 if ((vp->v_mount->mnt_kern_flag & MNTK_UNMAPPED_BUFS) != 0 &&
959 unmapped_buf_allowed) {
960 bp->b_data = unmapped_buf;
961 bp->b_offset = 0;
962 } else {
963 bp->b_data = bp->b_kvabase;
964 pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages, bp->b_npages);
965 }
966
967 /* Build a minimal buffer header. */
968 bp->b_iocmd = BIO_READ;
969 KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
970 KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
971 bp->b_rcred = crhold(curthread->td_ucred);
972 bp->b_wcred = crhold(curthread->td_ucred);
973 pbgetbo(bo, bp);
974 bp->b_vp = vp;
975 bp->b_bcount = bp->b_bufsize = bp->b_runningbufspace = bytecount;
976 bp->b_iooffset = dbtob(bp->b_blkno);
977
978 atomic_add_long(&runningbufspace, bp->b_runningbufspace);
979 PCPU_INC(cnt.v_vnodein);
980 PCPU_ADD(cnt.v_vnodepgsin, bp->b_npages);
981
982 if (iodone != NULL) { /* async */
983 bp->b_pgiodone = iodone;
984 bp->b_caller1 = arg;
985 bp->b_iodone = vnode_pager_generic_getpages_done_async;
986 bp->b_flags |= B_ASYNC;
987 BUF_KERNPROC(bp);
988 bstrategy(bp);
989 return (VM_PAGER_OK);
990 } else {
991 bp->b_iodone = bdone;
992 bstrategy(bp);
993 bwait(bp, PVM, "vnread");
994 error = vnode_pager_generic_getpages_done(bp);
995 for (i = 0; i < bp->b_npages; i++)
996 bp->b_pages[i] = NULL;
997 bp->b_vp = NULL;
998 pbrelbo(bp);
999 relpbuf(bp, &vnode_pbuf_freecnt);
1000 return (error != 0 ? VM_PAGER_ERROR : VM_PAGER_OK);
1001 }
1002 }
1003
1004 static void
vnode_pager_generic_getpages_done_async(struct buf * bp)1005 vnode_pager_generic_getpages_done_async(struct buf *bp)
1006 {
1007 int error;
1008
1009 error = vnode_pager_generic_getpages_done(bp);
1010 /* Run the iodone upon the requested range. */
1011 bp->b_pgiodone(bp->b_caller1, bp->b_pages + bp->b_pgbefore,
1012 bp->b_npages - bp->b_pgbefore - bp->b_pgafter, error);
1013 for (int i = 0; i < bp->b_npages; i++)
1014 bp->b_pages[i] = NULL;
1015 bp->b_vp = NULL;
1016 pbrelbo(bp);
1017 relpbuf(bp, &vnode_async_pbuf_freecnt);
1018 }
1019
1020 static int
vnode_pager_generic_getpages_done(struct buf * bp)1021 vnode_pager_generic_getpages_done(struct buf *bp)
1022 {
1023 vm_object_t object;
1024 off_t tfoff, nextoff;
1025 int i, error;
1026
1027 error = (bp->b_ioflags & BIO_ERROR) != 0 ? EIO : 0;
1028 object = bp->b_vp->v_object;
1029
1030 if (error == 0 && bp->b_bcount != bp->b_npages * PAGE_SIZE) {
1031 if (!buf_mapped(bp)) {
1032 bp->b_data = bp->b_kvabase;
1033 pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages,
1034 bp->b_npages);
1035 }
1036 bzero(bp->b_data + bp->b_bcount,
1037 PAGE_SIZE * bp->b_npages - bp->b_bcount);
1038 }
1039 if (buf_mapped(bp)) {
1040 pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages);
1041 bp->b_data = unmapped_buf;
1042 }
1043
1044 VM_OBJECT_WLOCK(object);
1045 for (i = 0, tfoff = IDX_TO_OFF(bp->b_pages[0]->pindex);
1046 i < bp->b_npages; i++, tfoff = nextoff) {
1047 vm_page_t mt;
1048
1049 nextoff = tfoff + PAGE_SIZE;
1050 mt = bp->b_pages[i];
1051
1052 if (nextoff <= object->un_pager.vnp.vnp_size) {
1053 /*
1054 * Read filled up entire page.
1055 */
1056 mt->valid = VM_PAGE_BITS_ALL;
1057 KASSERT(mt->dirty == 0,
1058 ("%s: page %p is dirty", __func__, mt));
1059 KASSERT(!pmap_page_is_mapped(mt),
1060 ("%s: page %p is mapped", __func__, mt));
1061 } else {
1062 /*
1063 * Read did not fill up entire page.
1064 *
1065 * Currently we do not set the entire page valid,
1066 * we just try to clear the piece that we couldn't
1067 * read.
1068 */
1069 vm_page_set_valid_range(mt, 0,
1070 object->un_pager.vnp.vnp_size - tfoff);
1071 KASSERT((mt->dirty & vm_page_bits(0,
1072 object->un_pager.vnp.vnp_size - tfoff)) == 0,
1073 ("%s: page %p is dirty", __func__, mt));
1074 }
1075
1076 if (i < bp->b_pgbefore || i >= bp->b_npages - bp->b_pgafter)
1077 vm_page_readahead_finish(mt);
1078 }
1079 VM_OBJECT_WUNLOCK(object);
1080 if (error != 0)
1081 printf("%s: I/O read error %d\n", __func__, error);
1082
1083 return (error);
1084 }
1085
1086 /*
1087 * EOPNOTSUPP is no longer legal. For local media VFS's that do not
1088 * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
1089 * vnode_pager_generic_putpages() to implement the previous behaviour.
1090 *
1091 * All other FS's should use the bypass to get to the local media
1092 * backing vp's VOP_PUTPAGES.
1093 */
1094 static void
vnode_pager_putpages(vm_object_t object,vm_page_t * m,int count,int flags,int * rtvals)1095 vnode_pager_putpages(vm_object_t object, vm_page_t *m, int count,
1096 int flags, int *rtvals)
1097 {
1098 int rtval;
1099 struct vnode *vp;
1100 int bytes = count * PAGE_SIZE;
1101
1102 /*
1103 * Force synchronous operation if we are extremely low on memory
1104 * to prevent a low-memory deadlock. VOP operations often need to
1105 * allocate more memory to initiate the I/O ( i.e. do a BMAP
1106 * operation ). The swapper handles the case by limiting the amount
1107 * of asynchronous I/O, but that sort of solution doesn't scale well
1108 * for the vnode pager without a lot of work.
1109 *
1110 * Also, the backing vnode's iodone routine may not wake the pageout
1111 * daemon up. This should be probably be addressed XXX.
1112 */
1113
1114 if (vm_cnt.v_free_count < vm_cnt.v_pageout_free_min)
1115 flags |= VM_PAGER_PUT_SYNC;
1116
1117 /*
1118 * Call device-specific putpages function
1119 */
1120 vp = object->handle;
1121 VM_OBJECT_WUNLOCK(object);
1122 rtval = VOP_PUTPAGES(vp, m, bytes, flags, rtvals);
1123 KASSERT(rtval != EOPNOTSUPP,
1124 ("vnode_pager: stale FS putpages\n"));
1125 VM_OBJECT_WLOCK(object);
1126 }
1127
1128
1129 /*
1130 * This is now called from local media FS's to operate against their
1131 * own vnodes if they fail to implement VOP_PUTPAGES.
1132 *
1133 * This is typically called indirectly via the pageout daemon and
1134 * clustering has already typically occured, so in general we ask the
1135 * underlying filesystem to write the data out asynchronously rather
1136 * then delayed.
1137 */
1138 int
vnode_pager_generic_putpages(struct vnode * vp,vm_page_t * ma,int bytecount,int flags,int * rtvals)1139 vnode_pager_generic_putpages(struct vnode *vp, vm_page_t *ma, int bytecount,
1140 int flags, int *rtvals)
1141 {
1142 int i;
1143 vm_object_t object;
1144 vm_page_t m;
1145 int count;
1146
1147 int maxsize, ncount;
1148 vm_ooffset_t poffset;
1149 struct uio auio;
1150 struct iovec aiov;
1151 int error;
1152 int ioflags;
1153 int ppscheck = 0;
1154 static struct timeval lastfail;
1155 static int curfail;
1156
1157 object = vp->v_object;
1158 count = bytecount / PAGE_SIZE;
1159
1160 for (i = 0; i < count; i++)
1161 rtvals[i] = VM_PAGER_ERROR;
1162
1163 if ((int64_t)ma[0]->pindex < 0) {
1164 printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%lx)\n",
1165 (long)ma[0]->pindex, (u_long)ma[0]->dirty);
1166 rtvals[0] = VM_PAGER_BAD;
1167 return VM_PAGER_BAD;
1168 }
1169
1170 maxsize = count * PAGE_SIZE;
1171 ncount = count;
1172
1173 poffset = IDX_TO_OFF(ma[0]->pindex);
1174
1175 /*
1176 * If the page-aligned write is larger then the actual file we
1177 * have to invalidate pages occuring beyond the file EOF. However,
1178 * there is an edge case where a file may not be page-aligned where
1179 * the last page is partially invalid. In this case the filesystem
1180 * may not properly clear the dirty bits for the entire page (which
1181 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
1182 * With the page locked we are free to fix-up the dirty bits here.
1183 *
1184 * We do not under any circumstances truncate the valid bits, as
1185 * this will screw up bogus page replacement.
1186 */
1187 VM_OBJECT_WLOCK(object);
1188 if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
1189 if (object->un_pager.vnp.vnp_size > poffset) {
1190 int pgoff;
1191
1192 maxsize = object->un_pager.vnp.vnp_size - poffset;
1193 ncount = btoc(maxsize);
1194 if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
1195 /*
1196 * If the object is locked and the following
1197 * conditions hold, then the page's dirty
1198 * field cannot be concurrently changed by a
1199 * pmap operation.
1200 */
1201 m = ma[ncount - 1];
1202 vm_page_assert_sbusied(m);
1203 KASSERT(!pmap_page_is_write_mapped(m),
1204 ("vnode_pager_generic_putpages: page %p is not read-only", m));
1205 vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
1206 pgoff);
1207 }
1208 } else {
1209 maxsize = 0;
1210 ncount = 0;
1211 }
1212 if (ncount < count) {
1213 for (i = ncount; i < count; i++) {
1214 rtvals[i] = VM_PAGER_BAD;
1215 }
1216 }
1217 }
1218 VM_OBJECT_WUNLOCK(object);
1219
1220 /*
1221 * pageouts are already clustered, use IO_ASYNC to force a bawrite()
1222 * rather then a bdwrite() to prevent paging I/O from saturating
1223 * the buffer cache. Dummy-up the sequential heuristic to cause
1224 * large ranges to cluster. If neither IO_SYNC or IO_ASYNC is set,
1225 * the system decides how to cluster.
1226 */
1227 ioflags = IO_VMIO;
1228 if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL))
1229 ioflags |= IO_SYNC;
1230 else if ((flags & VM_PAGER_CLUSTER_OK) == 0)
1231 ioflags |= IO_ASYNC;
1232 ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0;
1233 ioflags |= IO_SEQMAX << IO_SEQSHIFT;
1234
1235 aiov.iov_base = (caddr_t) 0;
1236 aiov.iov_len = maxsize;
1237 auio.uio_iov = &aiov;
1238 auio.uio_iovcnt = 1;
1239 auio.uio_offset = poffset;
1240 auio.uio_segflg = UIO_NOCOPY;
1241 auio.uio_rw = UIO_WRITE;
1242 auio.uio_resid = maxsize;
1243 auio.uio_td = (struct thread *) 0;
1244 error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred);
1245 PCPU_INC(cnt.v_vnodeout);
1246 PCPU_ADD(cnt.v_vnodepgsout, ncount);
1247
1248 if (error) {
1249 if ((ppscheck = ppsratecheck(&lastfail, &curfail, 1)))
1250 printf("vnode_pager_putpages: I/O error %d\n", error);
1251 }
1252 if (auio.uio_resid) {
1253 if (ppscheck || ppsratecheck(&lastfail, &curfail, 1))
1254 printf("vnode_pager_putpages: residual I/O %zd at %lu\n",
1255 auio.uio_resid, (u_long)ma[0]->pindex);
1256 }
1257 for (i = 0; i < ncount; i++) {
1258 rtvals[i] = VM_PAGER_OK;
1259 }
1260 return rtvals[0];
1261 }
1262
1263 void
vnode_pager_undirty_pages(vm_page_t * ma,int * rtvals,int written)1264 vnode_pager_undirty_pages(vm_page_t *ma, int *rtvals, int written)
1265 {
1266 vm_object_t obj;
1267 int i, pos;
1268
1269 if (written == 0)
1270 return;
1271 obj = ma[0]->object;
1272 VM_OBJECT_WLOCK(obj);
1273 for (i = 0, pos = 0; pos < written; i++, pos += PAGE_SIZE) {
1274 if (pos < trunc_page(written)) {
1275 rtvals[i] = VM_PAGER_OK;
1276 vm_page_undirty(ma[i]);
1277 } else {
1278 /* Partially written page. */
1279 rtvals[i] = VM_PAGER_AGAIN;
1280 vm_page_clear_dirty(ma[i], 0, written & PAGE_MASK);
1281 }
1282 }
1283 VM_OBJECT_WUNLOCK(obj);
1284 }
1285
1286 void
vnode_pager_update_writecount(vm_object_t object,vm_offset_t start,vm_offset_t end)1287 vnode_pager_update_writecount(vm_object_t object, vm_offset_t start,
1288 vm_offset_t end)
1289 {
1290 struct vnode *vp;
1291 vm_ooffset_t old_wm;
1292
1293 VM_OBJECT_WLOCK(object);
1294 if (object->type != OBJT_VNODE) {
1295 VM_OBJECT_WUNLOCK(object);
1296 return;
1297 }
1298 old_wm = object->un_pager.vnp.writemappings;
1299 object->un_pager.vnp.writemappings += (vm_ooffset_t)end - start;
1300 vp = object->handle;
1301 if (old_wm == 0 && object->un_pager.vnp.writemappings != 0) {
1302 ASSERT_VOP_ELOCKED(vp, "v_writecount inc");
1303 VOP_ADD_WRITECOUNT(vp, 1);
1304 CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d",
1305 __func__, vp, vp->v_writecount);
1306 } else if (old_wm != 0 && object->un_pager.vnp.writemappings == 0) {
1307 ASSERT_VOP_ELOCKED(vp, "v_writecount dec");
1308 VOP_ADD_WRITECOUNT(vp, -1);
1309 CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
1310 __func__, vp, vp->v_writecount);
1311 }
1312 VM_OBJECT_WUNLOCK(object);
1313 }
1314
1315 void
vnode_pager_release_writecount(vm_object_t object,vm_offset_t start,vm_offset_t end)1316 vnode_pager_release_writecount(vm_object_t object, vm_offset_t start,
1317 vm_offset_t end)
1318 {
1319 struct vnode *vp;
1320 struct mount *mp;
1321 vm_offset_t inc;
1322
1323 VM_OBJECT_WLOCK(object);
1324
1325 /*
1326 * First, recheck the object type to account for the race when
1327 * the vnode is reclaimed.
1328 */
1329 if (object->type != OBJT_VNODE) {
1330 VM_OBJECT_WUNLOCK(object);
1331 return;
1332 }
1333
1334 /*
1335 * Optimize for the case when writemappings is not going to
1336 * zero.
1337 */
1338 inc = end - start;
1339 if (object->un_pager.vnp.writemappings != inc) {
1340 object->un_pager.vnp.writemappings -= inc;
1341 VM_OBJECT_WUNLOCK(object);
1342 return;
1343 }
1344
1345 vp = object->handle;
1346 vhold(vp);
1347 VM_OBJECT_WUNLOCK(object);
1348 mp = NULL;
1349 vn_start_write(vp, &mp, V_WAIT);
1350 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1351
1352 /*
1353 * Decrement the object's writemappings, by swapping the start
1354 * and end arguments for vnode_pager_update_writecount(). If
1355 * there was not a race with vnode reclaimation, then the
1356 * vnode's v_writecount is decremented.
1357 */
1358 vnode_pager_update_writecount(object, end, start);
1359 VOP_UNLOCK(vp, 0);
1360 vdrop(vp);
1361 if (mp != NULL)
1362 vn_finished_write(mp);
1363 }
1364