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