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