FreeBSD/Linux Kernel Cross Reference
sys/vm/vnode_pager.c
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: releng/5.4/sys/vm/vnode_pager.c 142370 2005-02-24 07:39:42Z alc $");
55
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/proc.h>
59 #include <sys/vnode.h>
60 #include <sys/mount.h>
61 #include <sys/bio.h>
62 #include <sys/buf.h>
63 #include <sys/vmmeter.h>
64 #include <sys/conf.h>
65 #include <sys/sf_buf.h>
66
67 #include <vm/vm.h>
68 #include <vm/vm_object.h>
69 #include <vm/vm_page.h>
70 #include <vm/vm_pager.h>
71 #include <vm/vm_map.h>
72 #include <vm/vnode_pager.h>
73 #include <vm/vm_extern.h>
74
75 static void vnode_pager_init(void);
76 static vm_offset_t vnode_pager_addr(struct vnode *vp, vm_ooffset_t address,
77 int *run);
78 static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m);
79 static int vnode_pager_input_old(vm_object_t object, vm_page_t m);
80 static void vnode_pager_dealloc(vm_object_t);
81 static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int);
82 static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *);
83 static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
84
85 struct pagerops vnodepagerops = {
86 .pgo_init = vnode_pager_init,
87 .pgo_alloc = vnode_pager_alloc,
88 .pgo_dealloc = vnode_pager_dealloc,
89 .pgo_getpages = vnode_pager_getpages,
90 .pgo_putpages = vnode_pager_putpages,
91 .pgo_haspage = vnode_pager_haspage,
92 };
93
94 int vnode_pbuf_freecnt;
95
96 static void
97 vnode_pager_init(void)
98 {
99
100 vnode_pbuf_freecnt = nswbuf / 2 + 1;
101 }
102
103 /*
104 * Allocate (or lookup) pager for a vnode.
105 * Handle is a vnode pointer.
106 *
107 * MPSAFE
108 */
109 vm_object_t
110 vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
111 vm_ooffset_t offset)
112 {
113 vm_object_t object;
114 struct vnode *vp;
115
116 /*
117 * Pageout to vnode, no can do yet.
118 */
119 if (handle == NULL)
120 return (NULL);
121
122 vp = (struct vnode *) handle;
123
124 ASSERT_VOP_LOCKED(vp, "vnode_pager_alloc");
125
126 /*
127 * Prevent race condition when allocating the object. This
128 * can happen with NFS vnodes since the nfsnode isn't locked.
129 */
130 VI_LOCK(vp);
131 while (vp->v_iflag & VI_OLOCK) {
132 vp->v_iflag |= VI_OWANT;
133 msleep(vp, VI_MTX(vp), PVM, "vnpobj", 0);
134 }
135 vp->v_iflag |= VI_OLOCK;
136 VI_UNLOCK(vp);
137
138 /*
139 * If the object is being terminated, wait for it to
140 * go away.
141 */
142 while ((object = vp->v_object) != NULL) {
143 VM_OBJECT_LOCK(object);
144 if ((object->flags & OBJ_DEAD) == 0)
145 break;
146 vm_object_set_flag(object, OBJ_DISCONNECTWNT);
147 msleep(object, VM_OBJECT_MTX(object), PDROP | PVM, "vadead", 0);
148 }
149
150 if (vp->v_usecount == 0)
151 panic("vnode_pager_alloc: no vnode reference");
152
153 if (object == NULL) {
154 /*
155 * And an object of the appropriate size
156 */
157 object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size)));
158
159 object->un_pager.vnp.vnp_size = size;
160
161 object->handle = handle;
162 vp->v_object = object;
163 } else {
164 object->ref_count++;
165 VM_OBJECT_UNLOCK(object);
166 }
167 VI_LOCK(vp);
168 vp->v_usecount++;
169 vp->v_iflag &= ~VI_OLOCK;
170 if (vp->v_iflag & VI_OWANT) {
171 vp->v_iflag &= ~VI_OWANT;
172 wakeup(vp);
173 }
174 VI_UNLOCK(vp);
175 return (object);
176 }
177
178 /*
179 * The object must be locked.
180 */
181 static void
182 vnode_pager_dealloc(object)
183 vm_object_t object;
184 {
185 struct vnode *vp = object->handle;
186
187 if (vp == NULL)
188 panic("vnode_pager_dealloc: pager already dealloced");
189
190 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
191 vm_object_pip_wait(object, "vnpdea");
192
193 object->handle = NULL;
194 object->type = OBJT_DEAD;
195 if (object->flags & OBJ_DISCONNECTWNT) {
196 vm_object_clear_flag(object, OBJ_DISCONNECTWNT);
197 wakeup(object);
198 }
199 ASSERT_VOP_LOCKED(vp, "vnode_pager_dealloc");
200 vp->v_object = NULL;
201 vp->v_vflag &= ~(VV_TEXT | VV_OBJBUF);
202 }
203
204 static boolean_t
205 vnode_pager_haspage(object, pindex, before, after)
206 vm_object_t object;
207 vm_pindex_t pindex;
208 int *before;
209 int *after;
210 {
211 struct vnode *vp = object->handle;
212 daddr_t bn;
213 int err;
214 daddr_t reqblock;
215 int poff;
216 int bsize;
217 int pagesperblock, blocksperpage;
218
219 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
220 /*
221 * If no vp or vp is doomed or marked transparent to VM, we do not
222 * have the page.
223 */
224 if (vp == NULL)
225 return FALSE;
226
227 VI_LOCK(vp);
228 if (vp->v_iflag & VI_DOOMED) {
229 VI_UNLOCK(vp);
230 return FALSE;
231 }
232 VI_UNLOCK(vp);
233 /*
234 * If filesystem no longer mounted or offset beyond end of file we do
235 * not have the page.
236 */
237 if ((vp->v_mount == NULL) ||
238 (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size))
239 return FALSE;
240
241 bsize = vp->v_mount->mnt_stat.f_iosize;
242 pagesperblock = bsize / PAGE_SIZE;
243 blocksperpage = 0;
244 if (pagesperblock > 0) {
245 reqblock = pindex / pagesperblock;
246 } else {
247 blocksperpage = (PAGE_SIZE / bsize);
248 reqblock = pindex * blocksperpage;
249 }
250 VM_OBJECT_UNLOCK(object);
251 mtx_lock(&Giant);
252 err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before);
253 mtx_unlock(&Giant);
254 VM_OBJECT_LOCK(object);
255 if (err)
256 return TRUE;
257 if (bn == -1)
258 return FALSE;
259 if (pagesperblock > 0) {
260 poff = pindex - (reqblock * pagesperblock);
261 if (before) {
262 *before *= pagesperblock;
263 *before += poff;
264 }
265 if (after) {
266 int numafter;
267 *after *= pagesperblock;
268 numafter = pagesperblock - (poff + 1);
269 if (IDX_TO_OFF(pindex + numafter) >
270 object->un_pager.vnp.vnp_size) {
271 numafter =
272 OFF_TO_IDX(object->un_pager.vnp.vnp_size) -
273 pindex;
274 }
275 *after += numafter;
276 }
277 } else {
278 if (before) {
279 *before /= blocksperpage;
280 }
281
282 if (after) {
283 *after /= blocksperpage;
284 }
285 }
286 return TRUE;
287 }
288
289 /*
290 * Lets the VM system know about a change in size for a file.
291 * We adjust our own internal size and flush any cached pages in
292 * the associated object that are affected by the size change.
293 *
294 * Note: this routine may be invoked as a result of a pager put
295 * operation (possibly at object termination time), so we must be careful.
296 */
297 void
298 vnode_pager_setsize(vp, nsize)
299 struct vnode *vp;
300 vm_ooffset_t nsize;
301 {
302 vm_object_t object;
303 vm_page_t m;
304 vm_pindex_t nobjsize;
305
306 if ((object = vp->v_object) == NULL)
307 return;
308 VM_OBJECT_LOCK(object);
309 if (nsize == object->un_pager.vnp.vnp_size) {
310 /*
311 * Hasn't changed size
312 */
313 VM_OBJECT_UNLOCK(object);
314 return;
315 }
316 nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
317 if (nsize < object->un_pager.vnp.vnp_size) {
318 /*
319 * File has shrunk. Toss any cached pages beyond the new EOF.
320 */
321 if (nobjsize < object->size)
322 vm_object_page_remove(object, nobjsize, object->size,
323 FALSE);
324 /*
325 * this gets rid of garbage at the end of a page that is now
326 * only partially backed by the vnode.
327 *
328 * XXX for some reason (I don't know yet), if we take a
329 * completely invalid page and mark it partially valid
330 * it can screw up NFS reads, so we don't allow the case.
331 */
332 if ((nsize & PAGE_MASK) &&
333 (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL &&
334 m->valid != 0) {
335 int base = (int)nsize & PAGE_MASK;
336 int size = PAGE_SIZE - base;
337
338 /*
339 * Clear out partial-page garbage in case
340 * the page has been mapped.
341 */
342 pmap_zero_page_area(m, base, size);
343
344 /*
345 * XXX work around SMP data integrity race
346 * by unmapping the page from user processes.
347 * The garbage we just cleared may be mapped
348 * to a user process running on another cpu
349 * and this code is not running through normal
350 * I/O channels which handle SMP issues for
351 * us, so unmap page to synchronize all cpus.
352 *
353 * XXX should vm_pager_unmap_page() have
354 * dealt with this?
355 */
356 vm_page_lock_queues();
357 pmap_remove_all(m);
358
359 /*
360 * Clear out partial-page dirty bits. This
361 * has the side effect of setting the valid
362 * bits, but that is ok. There are a bunch
363 * of places in the VM system where we expected
364 * m->dirty == VM_PAGE_BITS_ALL. The file EOF
365 * case is one of them. If the page is still
366 * partially dirty, make it fully dirty.
367 *
368 * note that we do not clear out the valid
369 * bits. This would prevent bogus_page
370 * replacement from working properly.
371 */
372 vm_page_set_validclean(m, base, size);
373 if (m->dirty != 0)
374 m->dirty = VM_PAGE_BITS_ALL;
375 vm_page_unlock_queues();
376 }
377 }
378 object->un_pager.vnp.vnp_size = nsize;
379 object->size = nobjsize;
380 VM_OBJECT_UNLOCK(object);
381 }
382
383 /*
384 * calculate the linear (byte) disk address of specified virtual
385 * file address
386 */
387 static vm_offset_t
388 vnode_pager_addr(vp, address, run)
389 struct vnode *vp;
390 vm_ooffset_t address;
391 int *run;
392 {
393 int rtaddress;
394 int bsize;
395 daddr_t block;
396 int err;
397 daddr_t vblock;
398 int voffset;
399
400 GIANT_REQUIRED;
401 if (address < 0)
402 return -1;
403
404 if (vp->v_mount == NULL)
405 return -1;
406
407 bsize = vp->v_mount->mnt_stat.f_iosize;
408 vblock = address / bsize;
409 voffset = address % bsize;
410
411 err = VOP_BMAP(vp, vblock, NULL, &block, run, NULL);
412
413 if (err || (block == -1))
414 rtaddress = -1;
415 else {
416 rtaddress = block + voffset / DEV_BSIZE;
417 if (run) {
418 *run += 1;
419 *run *= bsize/PAGE_SIZE;
420 *run -= voffset/PAGE_SIZE;
421 }
422 }
423
424 return rtaddress;
425 }
426
427 /*
428 * small block filesystem vnode pager input
429 */
430 static int
431 vnode_pager_input_smlfs(object, m)
432 vm_object_t object;
433 vm_page_t m;
434 {
435 int i;
436 struct vnode *dp, *vp;
437 struct buf *bp;
438 struct sf_buf *sf;
439 int fileaddr;
440 vm_offset_t bsize;
441 int error = 0;
442
443 GIANT_REQUIRED;
444
445 vp = object->handle;
446 if (vp->v_mount == NULL)
447 return VM_PAGER_BAD;
448
449 bsize = vp->v_mount->mnt_stat.f_iosize;
450
451 VOP_BMAP(vp, 0, &dp, 0, NULL, NULL);
452
453 sf = sf_buf_alloc(m, 0);
454
455 for (i = 0; i < PAGE_SIZE / bsize; i++) {
456 vm_ooffset_t address;
457
458 if (vm_page_bits(i * bsize, bsize) & m->valid)
459 continue;
460
461 address = IDX_TO_OFF(m->pindex) + i * bsize;
462 if (address >= object->un_pager.vnp.vnp_size) {
463 fileaddr = -1;
464 } else {
465 fileaddr = vnode_pager_addr(vp, address, NULL);
466 }
467 if (fileaddr != -1) {
468 bp = getpbuf(&vnode_pbuf_freecnt);
469
470 /* build a minimal buffer header */
471 bp->b_iocmd = BIO_READ;
472 bp->b_iodone = bdone;
473 KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
474 KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
475 bp->b_rcred = crhold(curthread->td_ucred);
476 bp->b_wcred = crhold(curthread->td_ucred);
477 bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize;
478 bp->b_blkno = fileaddr;
479 pbgetvp(dp, bp);
480 bp->b_bcount = bsize;
481 bp->b_bufsize = bsize;
482 bp->b_runningbufspace = bp->b_bufsize;
483 runningbufspace += bp->b_runningbufspace;
484
485 /* do the input */
486 bp->b_iooffset = dbtob(bp->b_blkno);
487 if (dp->v_type == VCHR)
488 VOP_SPECSTRATEGY(bp->b_vp, bp);
489 else
490 VOP_STRATEGY(bp->b_vp, bp);
491
492 /* we definitely need to be at splvm here */
493
494 bwait(bp, PVM, "vnsrd");
495
496 if ((bp->b_ioflags & BIO_ERROR) != 0)
497 error = EIO;
498
499 /*
500 * free the buffer header back to the swap buffer pool
501 */
502 relpbuf(bp, &vnode_pbuf_freecnt);
503 if (error)
504 break;
505
506 VM_OBJECT_LOCK(object);
507 vm_page_lock_queues();
508 vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
509 vm_page_unlock_queues();
510 VM_OBJECT_UNLOCK(object);
511 } else {
512 VM_OBJECT_LOCK(object);
513 vm_page_lock_queues();
514 vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
515 vm_page_unlock_queues();
516 VM_OBJECT_UNLOCK(object);
517 bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize);
518 }
519 }
520 sf_buf_free(sf);
521 vm_page_lock_queues();
522 pmap_clear_modify(m);
523 vm_page_unlock_queues();
524 if (error) {
525 return VM_PAGER_ERROR;
526 }
527 return VM_PAGER_OK;
528
529 }
530
531
532 /*
533 * old style vnode pager input routine
534 */
535 static int
536 vnode_pager_input_old(object, m)
537 vm_object_t object;
538 vm_page_t m;
539 {
540 struct uio auio;
541 struct iovec aiov;
542 int error;
543 int size;
544 struct sf_buf *sf;
545 struct vnode *vp;
546
547 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
548 error = 0;
549
550 /*
551 * Return failure if beyond current EOF
552 */
553 if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
554 return VM_PAGER_BAD;
555 } else {
556 size = PAGE_SIZE;
557 if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
558 size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
559 vp = object->handle;
560 VM_OBJECT_UNLOCK(object);
561
562 /*
563 * Allocate a kernel virtual address and initialize so that
564 * we can use VOP_READ/WRITE routines.
565 */
566 sf = sf_buf_alloc(m, 0);
567
568 aiov.iov_base = (caddr_t)sf_buf_kva(sf);
569 aiov.iov_len = size;
570 auio.uio_iov = &aiov;
571 auio.uio_iovcnt = 1;
572 auio.uio_offset = IDX_TO_OFF(m->pindex);
573 auio.uio_segflg = UIO_SYSSPACE;
574 auio.uio_rw = UIO_READ;
575 auio.uio_resid = size;
576 auio.uio_td = curthread;
577
578 error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
579 if (!error) {
580 int count = size - auio.uio_resid;
581
582 if (count == 0)
583 error = EINVAL;
584 else if (count != PAGE_SIZE)
585 bzero((caddr_t)sf_buf_kva(sf) + count,
586 PAGE_SIZE - count);
587 }
588 sf_buf_free(sf);
589
590 VM_OBJECT_LOCK(object);
591 }
592 vm_page_lock_queues();
593 pmap_clear_modify(m);
594 vm_page_undirty(m);
595 vm_page_unlock_queues();
596 if (!error)
597 m->valid = VM_PAGE_BITS_ALL;
598 return error ? VM_PAGER_ERROR : VM_PAGER_OK;
599 }
600
601 /*
602 * generic vnode pager input routine
603 */
604
605 /*
606 * Local media VFS's that do not implement their own VOP_GETPAGES
607 * should have their VOP_GETPAGES call to vnode_pager_generic_getpages()
608 * to implement the previous behaviour.
609 *
610 * All other FS's should use the bypass to get to the local media
611 * backing vp's VOP_GETPAGES.
612 */
613 static int
614 vnode_pager_getpages(object, m, count, reqpage)
615 vm_object_t object;
616 vm_page_t *m;
617 int count;
618 int reqpage;
619 {
620 int rtval;
621 struct vnode *vp;
622 int bytes = count * PAGE_SIZE;
623
624 vp = object->handle;
625 VM_OBJECT_UNLOCK(object);
626 mtx_lock(&Giant);
627 rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0);
628 KASSERT(rtval != EOPNOTSUPP,
629 ("vnode_pager: FS getpages not implemented\n"));
630 mtx_unlock(&Giant);
631 VM_OBJECT_LOCK(object);
632 return rtval;
633 }
634
635 /*
636 * This is now called from local media FS's to operate against their
637 * own vnodes if they fail to implement VOP_GETPAGES.
638 */
639 int
640 vnode_pager_generic_getpages(vp, m, bytecount, reqpage)
641 struct vnode *vp;
642 vm_page_t *m;
643 int bytecount;
644 int reqpage;
645 {
646 vm_object_t object;
647 vm_offset_t kva;
648 off_t foff, tfoff, nextoff;
649 int i, j, size, bsize, first, firstaddr;
650 struct vnode *dp;
651 int runpg;
652 int runend;
653 struct buf *bp;
654 int count;
655 int error = 0;
656
657 GIANT_REQUIRED;
658 object = vp->v_object;
659 count = bytecount / PAGE_SIZE;
660
661 if (vp->v_mount == NULL)
662 return VM_PAGER_BAD;
663
664 bsize = vp->v_mount->mnt_stat.f_iosize;
665
666 /* get the UNDERLYING device for the file with VOP_BMAP() */
667
668 /*
669 * originally, we did not check for an error return value -- assuming
670 * an fs always has a bmap entry point -- that assumption is wrong!!!
671 */
672 foff = IDX_TO_OFF(m[reqpage]->pindex);
673
674 /*
675 * if we can't bmap, use old VOP code
676 */
677 if (VOP_BMAP(vp, 0, &dp, 0, NULL, NULL)) {
678 VM_OBJECT_LOCK(object);
679 vm_page_lock_queues();
680 for (i = 0; i < count; i++)
681 if (i != reqpage)
682 vm_page_free(m[i]);
683 vm_page_unlock_queues();
684 cnt.v_vnodein++;
685 cnt.v_vnodepgsin++;
686 error = vnode_pager_input_old(object, m[reqpage]);
687 VM_OBJECT_UNLOCK(object);
688 return (error);
689
690 /*
691 * if the blocksize is smaller than a page size, then use
692 * special small filesystem code. NFS sometimes has a small
693 * blocksize, but it can handle large reads itself.
694 */
695 } else if ((PAGE_SIZE / bsize) > 1 &&
696 (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) {
697 VM_OBJECT_LOCK(object);
698 vm_page_lock_queues();
699 for (i = 0; i < count; i++)
700 if (i != reqpage)
701 vm_page_free(m[i]);
702 vm_page_unlock_queues();
703 VM_OBJECT_UNLOCK(object);
704 cnt.v_vnodein++;
705 cnt.v_vnodepgsin++;
706 return vnode_pager_input_smlfs(object, m[reqpage]);
707 }
708
709 /*
710 * If we have a completely valid page available to us, we can
711 * clean up and return. Otherwise we have to re-read the
712 * media.
713 */
714 VM_OBJECT_LOCK(object);
715 if (m[reqpage]->valid == VM_PAGE_BITS_ALL) {
716 vm_page_lock_queues();
717 for (i = 0; i < count; i++)
718 if (i != reqpage)
719 vm_page_free(m[i]);
720 vm_page_unlock_queues();
721 VM_OBJECT_UNLOCK(object);
722 return VM_PAGER_OK;
723 }
724 m[reqpage]->valid = 0;
725 VM_OBJECT_UNLOCK(object);
726
727 /*
728 * here on direct device I/O
729 */
730 firstaddr = -1;
731
732 /*
733 * calculate the run that includes the required page
734 */
735 for (first = 0, i = 0; i < count; i = runend) {
736 firstaddr = vnode_pager_addr(vp,
737 IDX_TO_OFF(m[i]->pindex), &runpg);
738 if (firstaddr == -1) {
739 VM_OBJECT_LOCK(object);
740 if (i == reqpage && foff < object->un_pager.vnp.vnp_size) {
741 panic("vnode_pager_getpages: unexpected missing page: firstaddr: %d, foff: 0x%jx%08jx, vnp_size: 0x%jx%08jx",
742 firstaddr, (uintmax_t)(foff >> 32),
743 (uintmax_t)foff,
744 (uintmax_t)
745 (object->un_pager.vnp.vnp_size >> 32),
746 (uintmax_t)object->un_pager.vnp.vnp_size);
747 }
748 vm_page_lock_queues();
749 vm_page_free(m[i]);
750 vm_page_unlock_queues();
751 VM_OBJECT_UNLOCK(object);
752 runend = i + 1;
753 first = runend;
754 continue;
755 }
756 runend = i + runpg;
757 if (runend <= reqpage) {
758 VM_OBJECT_LOCK(object);
759 vm_page_lock_queues();
760 for (j = i; j < runend; j++)
761 vm_page_free(m[j]);
762 vm_page_unlock_queues();
763 VM_OBJECT_UNLOCK(object);
764 } else {
765 if (runpg < (count - first)) {
766 VM_OBJECT_LOCK(object);
767 vm_page_lock_queues();
768 for (i = first + runpg; i < count; i++)
769 vm_page_free(m[i]);
770 vm_page_unlock_queues();
771 VM_OBJECT_UNLOCK(object);
772 count = first + runpg;
773 }
774 break;
775 }
776 first = runend;
777 }
778
779 /*
780 * the first and last page have been calculated now, move input pages
781 * to be zero based...
782 */
783 if (first != 0) {
784 for (i = first; i < count; i++) {
785 m[i - first] = m[i];
786 }
787 count -= first;
788 reqpage -= first;
789 }
790
791 /*
792 * calculate the file virtual address for the transfer
793 */
794 foff = IDX_TO_OFF(m[0]->pindex);
795
796 /*
797 * calculate the size of the transfer
798 */
799 size = count * PAGE_SIZE;
800 if ((foff + size) > object->un_pager.vnp.vnp_size)
801 size = object->un_pager.vnp.vnp_size - foff;
802
803 /*
804 * round up physical size for real devices.
805 */
806 if (dp->v_type == VBLK || dp->v_type == VCHR) {
807 int secmask = dp->v_rdev->si_bsize_phys - 1;
808 KASSERT(secmask < PAGE_SIZE, ("vnode_pager_generic_getpages: sector size %d too large\n", secmask + 1));
809 size = (size + secmask) & ~secmask;
810 }
811
812 bp = getpbuf(&vnode_pbuf_freecnt);
813 kva = (vm_offset_t) bp->b_data;
814
815 /*
816 * and map the pages to be read into the kva
817 */
818 pmap_qenter(kva, m, count);
819
820 /* build a minimal buffer header */
821 bp->b_iocmd = BIO_READ;
822 bp->b_iodone = bdone;
823 KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
824 KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
825 bp->b_rcred = crhold(curthread->td_ucred);
826 bp->b_wcred = crhold(curthread->td_ucred);
827 bp->b_blkno = firstaddr;
828 pbgetvp(dp, bp);
829 bp->b_bcount = size;
830 bp->b_bufsize = size;
831 bp->b_runningbufspace = bp->b_bufsize;
832 runningbufspace += bp->b_runningbufspace;
833
834 cnt.v_vnodein++;
835 cnt.v_vnodepgsin += count;
836
837 /* do the input */
838 bp->b_iooffset = dbtob(bp->b_blkno);
839 if (dp->v_type == VCHR)
840 VOP_SPECSTRATEGY(bp->b_vp, bp);
841 else
842 VOP_STRATEGY(bp->b_vp, bp);
843
844 bwait(bp, PVM, "vnread");
845
846 if ((bp->b_ioflags & BIO_ERROR) != 0)
847 error = EIO;
848
849 if (!error) {
850 if (size != count * PAGE_SIZE)
851 bzero((caddr_t) kva + size, PAGE_SIZE * count - size);
852 }
853 pmap_qremove(kva, count);
854
855 /*
856 * free the buffer header back to the swap buffer pool
857 */
858 relpbuf(bp, &vnode_pbuf_freecnt);
859
860 VM_OBJECT_LOCK(object);
861 vm_page_lock_queues();
862 for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) {
863 vm_page_t mt;
864
865 nextoff = tfoff + PAGE_SIZE;
866 mt = m[i];
867
868 if (nextoff <= object->un_pager.vnp.vnp_size) {
869 /*
870 * Read filled up entire page.
871 */
872 mt->valid = VM_PAGE_BITS_ALL;
873 vm_page_undirty(mt); /* should be an assert? XXX */
874 pmap_clear_modify(mt);
875 } else {
876 /*
877 * Read did not fill up entire page. Since this
878 * is getpages, the page may be mapped, so we have
879 * to zero the invalid portions of the page even
880 * though we aren't setting them valid.
881 *
882 * Currently we do not set the entire page valid,
883 * we just try to clear the piece that we couldn't
884 * read.
885 */
886 vm_page_set_validclean(mt, 0,
887 object->un_pager.vnp.vnp_size - tfoff);
888 /* handled by vm_fault now */
889 /* vm_page_zero_invalid(mt, FALSE); */
890 }
891
892 if (i != reqpage) {
893
894 /*
895 * whether or not to leave the page activated is up in
896 * the air, but we should put the page on a page queue
897 * somewhere. (it already is in the object). Result:
898 * It appears that empirical results show that
899 * deactivating pages is best.
900 */
901
902 /*
903 * just in case someone was asking for this page we
904 * now tell them that it is ok to use
905 */
906 if (!error) {
907 if (mt->flags & PG_WANTED)
908 vm_page_activate(mt);
909 else
910 vm_page_deactivate(mt);
911 vm_page_wakeup(mt);
912 } else {
913 vm_page_free(mt);
914 }
915 }
916 }
917 vm_page_unlock_queues();
918 VM_OBJECT_UNLOCK(object);
919 if (error) {
920 printf("vnode_pager_getpages: I/O read error\n");
921 }
922 return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
923 }
924
925 /*
926 * EOPNOTSUPP is no longer legal. For local media VFS's that do not
927 * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
928 * vnode_pager_generic_putpages() to implement the previous behaviour.
929 *
930 * All other FS's should use the bypass to get to the local media
931 * backing vp's VOP_PUTPAGES.
932 */
933 static void
934 vnode_pager_putpages(object, m, count, sync, rtvals)
935 vm_object_t object;
936 vm_page_t *m;
937 int count;
938 boolean_t sync;
939 int *rtvals;
940 {
941 int rtval;
942 struct vnode *vp;
943 struct mount *mp;
944 int bytes = count * PAGE_SIZE;
945
946 GIANT_REQUIRED;
947 /*
948 * Force synchronous operation if we are extremely low on memory
949 * to prevent a low-memory deadlock. VOP operations often need to
950 * allocate more memory to initiate the I/O ( i.e. do a BMAP
951 * operation ). The swapper handles the case by limiting the amount
952 * of asynchronous I/O, but that sort of solution doesn't scale well
953 * for the vnode pager without a lot of work.
954 *
955 * Also, the backing vnode's iodone routine may not wake the pageout
956 * daemon up. This should be probably be addressed XXX.
957 */
958
959 if ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min)
960 sync |= OBJPC_SYNC;
961
962 /*
963 * Call device-specific putpages function
964 */
965 vp = object->handle;
966 VM_OBJECT_UNLOCK(object);
967 if (vp->v_type != VREG)
968 mp = NULL;
969 (void)vn_start_write(vp, &mp, V_WAIT);
970 rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0);
971 KASSERT(rtval != EOPNOTSUPP,
972 ("vnode_pager: stale FS putpages\n"));
973 vn_finished_write(mp);
974 VM_OBJECT_LOCK(object);
975 }
976
977
978 /*
979 * This is now called from local media FS's to operate against their
980 * own vnodes if they fail to implement VOP_PUTPAGES.
981 *
982 * This is typically called indirectly via the pageout daemon and
983 * clustering has already typically occured, so in general we ask the
984 * underlying filesystem to write the data out asynchronously rather
985 * then delayed.
986 */
987 int
988 vnode_pager_generic_putpages(vp, m, bytecount, flags, rtvals)
989 struct vnode *vp;
990 vm_page_t *m;
991 int bytecount;
992 int flags;
993 int *rtvals;
994 {
995 int i;
996 vm_object_t object;
997 int count;
998
999 int maxsize, ncount;
1000 vm_ooffset_t poffset;
1001 struct uio auio;
1002 struct iovec aiov;
1003 int error;
1004 int ioflags;
1005
1006 GIANT_REQUIRED;
1007 object = vp->v_object;
1008 count = bytecount / PAGE_SIZE;
1009
1010 for (i = 0; i < count; i++)
1011 rtvals[i] = VM_PAGER_AGAIN;
1012
1013 if ((int64_t)m[0]->pindex < 0) {
1014 printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%lx)\n",
1015 (long)m[0]->pindex, (u_long)m[0]->dirty);
1016 rtvals[0] = VM_PAGER_BAD;
1017 return VM_PAGER_BAD;
1018 }
1019
1020 maxsize = count * PAGE_SIZE;
1021 ncount = count;
1022
1023 poffset = IDX_TO_OFF(m[0]->pindex);
1024
1025 /*
1026 * If the page-aligned write is larger then the actual file we
1027 * have to invalidate pages occuring beyond the file EOF. However,
1028 * there is an edge case where a file may not be page-aligned where
1029 * the last page is partially invalid. In this case the filesystem
1030 * may not properly clear the dirty bits for the entire page (which
1031 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
1032 * With the page locked we are free to fix-up the dirty bits here.
1033 *
1034 * We do not under any circumstances truncate the valid bits, as
1035 * this will screw up bogus page replacement.
1036 */
1037 if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
1038 if (object->un_pager.vnp.vnp_size > poffset) {
1039 int pgoff;
1040
1041 maxsize = object->un_pager.vnp.vnp_size - poffset;
1042 ncount = btoc(maxsize);
1043 if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
1044 vm_page_lock_queues();
1045 vm_page_clear_dirty(m[ncount - 1], pgoff,
1046 PAGE_SIZE - pgoff);
1047 vm_page_unlock_queues();
1048 }
1049 } else {
1050 maxsize = 0;
1051 ncount = 0;
1052 }
1053 if (ncount < count) {
1054 for (i = ncount; i < count; i++) {
1055 rtvals[i] = VM_PAGER_BAD;
1056 }
1057 }
1058 }
1059
1060 /*
1061 * pageouts are already clustered, use IO_ASYNC t o force a bawrite()
1062 * rather then a bdwrite() to prevent paging I/O from saturating
1063 * the buffer cache. Dummy-up the sequential heuristic to cause
1064 * large ranges to cluster. If neither IO_SYNC or IO_ASYNC is set,
1065 * the system decides how to cluster.
1066 */
1067 ioflags = IO_VMIO;
1068 if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL))
1069 ioflags |= IO_SYNC;
1070 else if ((flags & VM_PAGER_CLUSTER_OK) == 0)
1071 ioflags |= IO_ASYNC;
1072 ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0;
1073 ioflags |= IO_SEQMAX << IO_SEQSHIFT;
1074
1075 aiov.iov_base = (caddr_t) 0;
1076 aiov.iov_len = maxsize;
1077 auio.uio_iov = &aiov;
1078 auio.uio_iovcnt = 1;
1079 auio.uio_offset = poffset;
1080 auio.uio_segflg = UIO_NOCOPY;
1081 auio.uio_rw = UIO_WRITE;
1082 auio.uio_resid = maxsize;
1083 auio.uio_td = (struct thread *) 0;
1084 error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred);
1085 cnt.v_vnodeout++;
1086 cnt.v_vnodepgsout += ncount;
1087
1088 if (error) {
1089 printf("vnode_pager_putpages: I/O error %d\n", error);
1090 }
1091 if (auio.uio_resid) {
1092 printf("vnode_pager_putpages: residual I/O %d at %lu\n",
1093 auio.uio_resid, (u_long)m[0]->pindex);
1094 }
1095 for (i = 0; i < ncount; i++) {
1096 rtvals[i] = VM_PAGER_OK;
1097 }
1098 return rtvals[0];
1099 }
1100
1101 struct vnode *
1102 vnode_pager_lock(vm_object_t first_object)
1103 {
1104 struct vnode *vp;
1105 vm_object_t backing_object, object;
1106
1107 VM_OBJECT_LOCK_ASSERT(first_object, MA_OWNED);
1108 for (object = first_object; object != NULL; object = backing_object) {
1109 if (object->type != OBJT_VNODE) {
1110 if ((backing_object = object->backing_object) != NULL)
1111 VM_OBJECT_LOCK(backing_object);
1112 if (object != first_object)
1113 VM_OBJECT_UNLOCK(object);
1114 continue;
1115 }
1116 retry:
1117 if (object->flags & OBJ_DEAD) {
1118 if (object != first_object)
1119 VM_OBJECT_UNLOCK(object);
1120 return NULL;
1121 }
1122 vp = object->handle;
1123 VI_LOCK(vp);
1124 VM_OBJECT_UNLOCK(object);
1125 if (first_object != object)
1126 VM_OBJECT_UNLOCK(first_object);
1127 if (vget(vp, LK_CANRECURSE | LK_INTERLOCK | LK_NOPAUSE |
1128 LK_RETRY | LK_SHARED, curthread)) {
1129 VM_OBJECT_LOCK(first_object);
1130 if (object != first_object)
1131 VM_OBJECT_LOCK(object);
1132 if (object->type != OBJT_VNODE) {
1133 if (object != first_object)
1134 VM_OBJECT_UNLOCK(object);
1135 return NULL;
1136 }
1137 printf("vnode_pager_lock: retrying\n");
1138 goto retry;
1139 }
1140 VM_OBJECT_LOCK(first_object);
1141 return (vp);
1142 }
1143 return NULL;
1144 }
Cache object: c10d96f71f91145354ce49cf4e41b29e
|