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