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