vnode_pager.c revision 242476
11590Srgrimes/*-
21590Srgrimes * Copyright (c) 1990 University of Utah.
31590Srgrimes * Copyright (c) 1991 The Regents of the University of California.
41590Srgrimes * All rights reserved.
51590Srgrimes * Copyright (c) 1993, 1994 John S. Dyson
61590Srgrimes * Copyright (c) 1995, David Greenman
71590Srgrimes *
81590Srgrimes * This code is derived from software contributed to Berkeley by
91590Srgrimes * the Systems Programming Group of the University of Utah Computer
101590Srgrimes * Science Department.
111590Srgrimes *
121590Srgrimes * Redistribution and use in source and binary forms, with or without
131590Srgrimes * modification, are permitted provided that the following conditions
141590Srgrimes * are met:
151590Srgrimes * 1. Redistributions of source code must retain the above copyright
161590Srgrimes *    notice, this list of conditions and the following disclaimer.
171590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
181590Srgrimes *    notice, this list of conditions and the following disclaimer in the
191590Srgrimes *    documentation and/or other materials provided with the distribution.
201590Srgrimes * 3. All advertising materials mentioning features or use of this software
211590Srgrimes *    must display the following acknowledgement:
221590Srgrimes *	This product includes software developed by the University of
231590Srgrimes *	California, Berkeley and its contributors.
241590Srgrimes * 4. Neither the name of the University nor the names of its contributors
251590Srgrimes *    may be used to endorse or promote products derived from this software
261590Srgrimes *    without specific prior written permission.
271590Srgrimes *
281590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
291590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3080290Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
311590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
321590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33131954Stjr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34131954Stjr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
351590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
361590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
371590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
381590Srgrimes * SUCH DAMAGE.
391590Srgrimes *
401590Srgrimes *	from: @(#)vnode_pager.c	7.5 (Berkeley) 4/20/91
411590Srgrimes */
421590Srgrimes
431590Srgrimes/*
441590Srgrimes * Page to/from files (vnodes).
451590Srgrimes */
461590Srgrimes
471590Srgrimes/*
481590Srgrimes * TODO:
491590Srgrimes *	Implement VOP_GETPAGES/PUTPAGES interface for filesystems. Will
501590Srgrimes *	greatly re-simplify the vnode_pager.
511590Srgrimes */
521590Srgrimes
53131954Stjr#include <sys/cdefs.h>
54131954Stjr__FBSDID("$FreeBSD: head/sys/vm/vnode_pager.c 242476 2012-11-02 13:56:36Z kib $");
551590Srgrimes
561590Srgrimes#include <sys/param.h>
571590Srgrimes#include <sys/systm.h>
581590Srgrimes#include <sys/proc.h>
591590Srgrimes#include <sys/vnode.h>
601590Srgrimes#include <sys/mount.h>
611590Srgrimes#include <sys/bio.h>
621590Srgrimes#include <sys/buf.h>
631590Srgrimes#include <sys/vmmeter.h>
641590Srgrimes#include <sys/limits.h>
651590Srgrimes#include <sys/conf.h>
661590Srgrimes#include <sys/sf_buf.h>
671590Srgrimes
681590Srgrimes#include <machine/atomic.h>
691590Srgrimes
701590Srgrimes#include <vm/vm.h>
711590Srgrimes#include <vm/vm_param.h>
721590Srgrimes#include <vm/vm_object.h>
731590Srgrimes#include <vm/vm_page.h>
741590Srgrimes#include <vm/vm_pager.h>
75102944Sdwmalone#include <vm/vm_map.h>
761590Srgrimes#include <vm/vnode_pager.h>
77102944Sdwmalone#include <vm/vm_extern.h>
7896787Stjr
7996793Stjrstatic int vnode_pager_addr(struct vnode *vp, vm_ooffset_t address,
8096793Stjr    daddr_t *rtaddress, int *run);
811590Srgrimesstatic int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m);
8296793Stjrstatic int vnode_pager_input_old(vm_object_t object, vm_page_t m);
831590Srgrimesstatic void vnode_pager_dealloc(vm_object_t);
8492920Simpstatic int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int);
8592920Simpstatic void vnode_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *);
8692920Simpstatic boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
8792920Simpstatic vm_object_t vnode_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
8892920Simp    vm_ooffset_t, struct ucred *cred);
8992920Simp
9092920Simpstruct pagerops vnodepagerops = {
91131954Stjr	.pgo_alloc =	vnode_pager_alloc,
9292920Simp	.pgo_dealloc =	vnode_pager_dealloc,
9392920Simp	.pgo_getpages =	vnode_pager_getpages,
9492920Simp	.pgo_putpages =	vnode_pager_putpages,
9592920Simp	.pgo_haspage =	vnode_pager_haspage,
9692920Simp};
9792920Simp
9892920Simpint vnode_pbuf_freecnt;
9992920Simp
10092920Simp/* Create the VM system backing object for this vnode */
101131954Stjrint
10292920Simpvnode_create_vobject(struct vnode *vp, off_t isize, struct thread *td)
10392920Simp{
10492920Simp	vm_object_t object;
105	vm_ooffset_t size = isize;
106	struct vattr va;
107
108	if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE)
109		return (0);
110
111	while ((object = vp->v_object) != NULL) {
112		VM_OBJECT_LOCK(object);
113		if (!(object->flags & OBJ_DEAD)) {
114			VM_OBJECT_UNLOCK(object);
115			return (0);
116		}
117		VOP_UNLOCK(vp, 0);
118		vm_object_set_flag(object, OBJ_DISCONNECTWNT);
119		msleep(object, VM_OBJECT_MTX(object), PDROP | PVM, "vodead", 0);
120		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
121	}
122
123	if (size == 0) {
124		if (vn_isdisk(vp, NULL)) {
125			size = IDX_TO_OFF(INT_MAX);
126		} else {
127			if (VOP_GETATTR(vp, &va, td->td_ucred))
128				return (0);
129			size = va.va_size;
130		}
131	}
132
133	object = vnode_pager_alloc(vp, size, 0, 0, td->td_ucred);
134	/*
135	 * Dereference the reference we just created.  This assumes
136	 * that the object is associated with the vp.
137	 */
138	VM_OBJECT_LOCK(object);
139	object->ref_count--;
140	VM_OBJECT_UNLOCK(object);
141	vrele(vp);
142
143	KASSERT(vp->v_object != NULL, ("vnode_create_vobject: NULL object"));
144
145	return (0);
146}
147
148void
149vnode_destroy_vobject(struct vnode *vp)
150{
151	struct vm_object *obj;
152
153	obj = vp->v_object;
154	if (obj == NULL)
155		return;
156	ASSERT_VOP_ELOCKED(vp, "vnode_destroy_vobject");
157	VM_OBJECT_LOCK(obj);
158	if (obj->ref_count == 0) {
159		/*
160		 * vclean() may be called twice. The first time
161		 * removes the primary reference to the object,
162		 * the second time goes one further and is a
163		 * special-case to terminate the object.
164		 *
165		 * don't double-terminate the object
166		 */
167		if ((obj->flags & OBJ_DEAD) == 0)
168			vm_object_terminate(obj);
169		else
170			VM_OBJECT_UNLOCK(obj);
171	} else {
172		/*
173		 * Woe to the process that tries to page now :-).
174		 */
175		vm_pager_deallocate(obj);
176		VM_OBJECT_UNLOCK(obj);
177	}
178	vp->v_object = NULL;
179}
180
181
182/*
183 * Allocate (or lookup) pager for a vnode.
184 * Handle is a vnode pointer.
185 *
186 * MPSAFE
187 */
188vm_object_t
189vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
190    vm_ooffset_t offset, struct ucred *cred)
191{
192	vm_object_t object;
193	struct vnode *vp;
194
195	/*
196	 * Pageout to vnode, no can do yet.
197	 */
198	if (handle == NULL)
199		return (NULL);
200
201	vp = (struct vnode *) handle;
202
203	/*
204	 * If the object is being terminated, wait for it to
205	 * go away.
206	 */
207retry:
208	while ((object = vp->v_object) != NULL) {
209		VM_OBJECT_LOCK(object);
210		if ((object->flags & OBJ_DEAD) == 0)
211			break;
212		vm_object_set_flag(object, OBJ_DISCONNECTWNT);
213		msleep(object, VM_OBJECT_MTX(object), PDROP | PVM, "vadead", 0);
214	}
215
216	if (vp->v_usecount == 0)
217		panic("vnode_pager_alloc: no vnode reference");
218
219	if (object == NULL) {
220		/*
221		 * Add an object of the appropriate size
222		 */
223		object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size)));
224
225		object->un_pager.vnp.vnp_size = size;
226		object->un_pager.vnp.writemappings = 0;
227
228		object->handle = handle;
229		VI_LOCK(vp);
230		if (vp->v_object != NULL) {
231			/*
232			 * Object has been created while we were sleeping
233			 */
234			VI_UNLOCK(vp);
235			vm_object_destroy(object);
236			goto retry;
237		}
238		vp->v_object = object;
239		VI_UNLOCK(vp);
240	} else {
241		object->ref_count++;
242		VM_OBJECT_UNLOCK(object);
243	}
244	vref(vp);
245	return (object);
246}
247
248/*
249 *	The object must be locked.
250 */
251static void
252vnode_pager_dealloc(object)
253	vm_object_t object;
254{
255	struct vnode *vp;
256	int refs;
257
258	vp = object->handle;
259	if (vp == NULL)
260		panic("vnode_pager_dealloc: pager already dealloced");
261
262	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
263	vm_object_pip_wait(object, "vnpdea");
264	refs = object->ref_count;
265
266	object->handle = NULL;
267	object->type = OBJT_DEAD;
268	if (object->flags & OBJ_DISCONNECTWNT) {
269		vm_object_clear_flag(object, OBJ_DISCONNECTWNT);
270		wakeup(object);
271	}
272	ASSERT_VOP_ELOCKED(vp, "vnode_pager_dealloc");
273	if (object->un_pager.vnp.writemappings > 0) {
274		object->un_pager.vnp.writemappings = 0;
275		VOP_ADD_WRITECOUNT(vp, -1);
276		CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
277		    __func__, vp, vp->v_writecount);
278	}
279	vp->v_object = NULL;
280	VOP_UNSET_TEXT(vp);
281	VM_OBJECT_UNLOCK(object);
282	while (refs-- > 0)
283		vunref(vp);
284	VM_OBJECT_LOCK(object);
285}
286
287static boolean_t
288vnode_pager_haspage(object, pindex, before, after)
289	vm_object_t object;
290	vm_pindex_t pindex;
291	int *before;
292	int *after;
293{
294	struct vnode *vp = object->handle;
295	daddr_t bn;
296	int err;
297	daddr_t reqblock;
298	int poff;
299	int bsize;
300	int pagesperblock, blocksperpage;
301
302	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
303	/*
304	 * If no vp or vp is doomed or marked transparent to VM, we do not
305	 * have the page.
306	 */
307	if (vp == NULL || vp->v_iflag & VI_DOOMED)
308		return FALSE;
309	/*
310	 * If the offset is beyond end of file we do
311	 * not have the page.
312	 */
313	if (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)
314		return FALSE;
315
316	bsize = vp->v_mount->mnt_stat.f_iosize;
317	pagesperblock = bsize / PAGE_SIZE;
318	blocksperpage = 0;
319	if (pagesperblock > 0) {
320		reqblock = pindex / pagesperblock;
321	} else {
322		blocksperpage = (PAGE_SIZE / bsize);
323		reqblock = pindex * blocksperpage;
324	}
325	VM_OBJECT_UNLOCK(object);
326	err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before);
327	VM_OBJECT_LOCK(object);
328	if (err)
329		return TRUE;
330	if (bn == -1)
331		return FALSE;
332	if (pagesperblock > 0) {
333		poff = pindex - (reqblock * pagesperblock);
334		if (before) {
335			*before *= pagesperblock;
336			*before += poff;
337		}
338		if (after) {
339			int numafter;
340			*after *= pagesperblock;
341			numafter = pagesperblock - (poff + 1);
342			if (IDX_TO_OFF(pindex + numafter) >
343			    object->un_pager.vnp.vnp_size) {
344				numafter =
345		    		    OFF_TO_IDX(object->un_pager.vnp.vnp_size) -
346				    pindex;
347			}
348			*after += numafter;
349		}
350	} else {
351		if (before) {
352			*before /= blocksperpage;
353		}
354
355		if (after) {
356			*after /= blocksperpage;
357		}
358	}
359	return TRUE;
360}
361
362/*
363 * Lets the VM system know about a change in size for a file.
364 * We adjust our own internal size and flush any cached pages in
365 * the associated object that are affected by the size change.
366 *
367 * Note: this routine may be invoked as a result of a pager put
368 * operation (possibly at object termination time), so we must be careful.
369 */
370void
371vnode_pager_setsize(vp, nsize)
372	struct vnode *vp;
373	vm_ooffset_t nsize;
374{
375	vm_object_t object;
376	vm_page_t m;
377	vm_pindex_t nobjsize;
378
379	if ((object = vp->v_object) == NULL)
380		return;
381/* 	ASSERT_VOP_ELOCKED(vp, "vnode_pager_setsize and not locked vnode"); */
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			    0);
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			 * Update the valid bits to reflect the blocks that
420			 * have been zeroed.  Some of these valid bits may
421			 * have already been set.
422			 */
423			vm_page_set_valid_range(m, base, size);
424
425			/*
426			 * Round "base" to the next block boundary so that the
427			 * dirty bit for a partially zeroed block is not
428			 * cleared.
429			 */
430			base = roundup2(base, DEV_BSIZE);
431
432			/*
433			 * Clear out partial-page dirty bits.
434			 *
435			 * note that we do not clear out the valid
436			 * bits.  This would prevent bogus_page
437			 * replacement from working properly.
438			 */
439			vm_page_clear_dirty(m, base, PAGE_SIZE - base);
440		} else if ((nsize & PAGE_MASK) &&
441		    vm_page_is_cached(object, OFF_TO_IDX(nsize))) {
442			vm_page_cache_free(object, OFF_TO_IDX(nsize),
443			    nobjsize);
444		}
445	}
446	object->un_pager.vnp.vnp_size = nsize;
447	object->size = nobjsize;
448	VM_OBJECT_UNLOCK(object);
449}
450
451/*
452 * calculate the linear (byte) disk address of specified virtual
453 * file address
454 */
455static int
456vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress,
457    int *run)
458{
459	int bsize;
460	int err;
461	daddr_t vblock;
462	daddr_t voffset;
463
464	if (address < 0)
465		return -1;
466
467	if (vp->v_iflag & VI_DOOMED)
468		return -1;
469
470	bsize = vp->v_mount->mnt_stat.f_iosize;
471	vblock = address / bsize;
472	voffset = address % bsize;
473
474	err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL);
475	if (err == 0) {
476		if (*rtaddress != -1)
477			*rtaddress += voffset / DEV_BSIZE;
478		if (run) {
479			*run += 1;
480			*run *= bsize/PAGE_SIZE;
481			*run -= voffset/PAGE_SIZE;
482		}
483	}
484
485	return (err);
486}
487
488/*
489 * small block filesystem vnode pager input
490 */
491static int
492vnode_pager_input_smlfs(object, m)
493	vm_object_t object;
494	vm_page_t m;
495{
496	struct vnode *vp;
497	struct bufobj *bo;
498	struct buf *bp;
499	struct sf_buf *sf;
500	daddr_t fileaddr;
501	vm_offset_t bsize;
502	vm_page_bits_t bits;
503	int error, i;
504
505	error = 0;
506	vp = object->handle;
507	if (vp->v_iflag & VI_DOOMED)
508		return VM_PAGER_BAD;
509
510	bsize = vp->v_mount->mnt_stat.f_iosize;
511
512	VOP_BMAP(vp, 0, &bo, 0, NULL, NULL);
513
514	sf = sf_buf_alloc(m, 0);
515
516	for (i = 0; i < PAGE_SIZE / bsize; i++) {
517		vm_ooffset_t address;
518
519		bits = vm_page_bits(i * bsize, bsize);
520		if (m->valid & bits)
521			continue;
522
523		address = IDX_TO_OFF(m->pindex) + i * bsize;
524		if (address >= object->un_pager.vnp.vnp_size) {
525			fileaddr = -1;
526		} else {
527			error = vnode_pager_addr(vp, address, &fileaddr, NULL);
528			if (error)
529				break;
530		}
531		if (fileaddr != -1) {
532			bp = getpbuf(&vnode_pbuf_freecnt);
533
534			/* build a minimal buffer header */
535			bp->b_iocmd = BIO_READ;
536			bp->b_iodone = bdone;
537			KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
538			KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
539			bp->b_rcred = crhold(curthread->td_ucred);
540			bp->b_wcred = crhold(curthread->td_ucred);
541			bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize;
542			bp->b_blkno = fileaddr;
543			pbgetbo(bo, bp);
544			bp->b_vp = vp;
545			bp->b_bcount = bsize;
546			bp->b_bufsize = bsize;
547			bp->b_runningbufspace = bp->b_bufsize;
548			atomic_add_long(&runningbufspace, bp->b_runningbufspace);
549
550			/* do the input */
551			bp->b_iooffset = dbtob(bp->b_blkno);
552			bstrategy(bp);
553
554			bwait(bp, PVM, "vnsrd");
555
556			if ((bp->b_ioflags & BIO_ERROR) != 0)
557				error = EIO;
558
559			/*
560			 * free the buffer header back to the swap buffer pool
561			 */
562			bp->b_vp = NULL;
563			pbrelbo(bp);
564			relpbuf(bp, &vnode_pbuf_freecnt);
565			if (error)
566				break;
567		} else
568			bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize);
569		KASSERT((m->dirty & bits) == 0,
570		    ("vnode_pager_input_smlfs: page %p is dirty", m));
571		VM_OBJECT_LOCK(object);
572		m->valid |= bits;
573		VM_OBJECT_UNLOCK(object);
574	}
575	sf_buf_free(sf);
576	if (error) {
577		return VM_PAGER_ERROR;
578	}
579	return VM_PAGER_OK;
580}
581
582/*
583 * old style vnode pager input routine
584 */
585static int
586vnode_pager_input_old(object, m)
587	vm_object_t object;
588	vm_page_t m;
589{
590	struct uio auio;
591	struct iovec aiov;
592	int error;
593	int size;
594	struct sf_buf *sf;
595	struct vnode *vp;
596
597	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
598	error = 0;
599
600	/*
601	 * Return failure if beyond current EOF
602	 */
603	if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
604		return VM_PAGER_BAD;
605	} else {
606		size = PAGE_SIZE;
607		if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
608			size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
609		vp = object->handle;
610		VM_OBJECT_UNLOCK(object);
611
612		/*
613		 * Allocate a kernel virtual address and initialize so that
614		 * we can use VOP_READ/WRITE routines.
615		 */
616		sf = sf_buf_alloc(m, 0);
617
618		aiov.iov_base = (caddr_t)sf_buf_kva(sf);
619		aiov.iov_len = size;
620		auio.uio_iov = &aiov;
621		auio.uio_iovcnt = 1;
622		auio.uio_offset = IDX_TO_OFF(m->pindex);
623		auio.uio_segflg = UIO_SYSSPACE;
624		auio.uio_rw = UIO_READ;
625		auio.uio_resid = size;
626		auio.uio_td = curthread;
627
628		error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
629		if (!error) {
630			int count = size - auio.uio_resid;
631
632			if (count == 0)
633				error = EINVAL;
634			else if (count != PAGE_SIZE)
635				bzero((caddr_t)sf_buf_kva(sf) + count,
636				    PAGE_SIZE - count);
637		}
638		sf_buf_free(sf);
639
640		VM_OBJECT_LOCK(object);
641	}
642	KASSERT(m->dirty == 0, ("vnode_pager_input_old: page %p is dirty", m));
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 */
660static int
661vnode_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
671	vp = object->handle;
672	VM_OBJECT_UNLOCK(object);
673	rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0);
674	KASSERT(rtval != EOPNOTSUPP,
675	    ("vnode_pager: FS getpages not implemented\n"));
676	VM_OBJECT_LOCK(object);
677	return rtval;
678}
679
680/*
681 * This is now called from local media FS's to operate against their
682 * own vnodes if they fail to implement VOP_GETPAGES.
683 */
684int
685vnode_pager_generic_getpages(vp, m, bytecount, reqpage)
686	struct vnode *vp;
687	vm_page_t *m;
688	int bytecount;
689	int reqpage;
690{
691	vm_object_t object;
692	vm_offset_t kva;
693	off_t foff, tfoff, nextoff;
694	int i, j, size, bsize, first;
695	daddr_t firstaddr, reqblock;
696	struct bufobj *bo;
697	int runpg;
698	int runend;
699	struct buf *bp;
700	int count;
701	int error;
702
703	object = vp->v_object;
704	count = bytecount / PAGE_SIZE;
705
706	KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
707	    ("vnode_pager_generic_getpages does not support devices"));
708	if (vp->v_iflag & VI_DOOMED)
709		return VM_PAGER_BAD;
710
711	bsize = vp->v_mount->mnt_stat.f_iosize;
712
713	/* get the UNDERLYING device for the file with VOP_BMAP() */
714
715	/*
716	 * originally, we did not check for an error return value -- assuming
717	 * an fs always has a bmap entry point -- that assumption is wrong!!!
718	 */
719	foff = IDX_TO_OFF(m[reqpage]->pindex);
720
721	/*
722	 * if we can't bmap, use old VOP code
723	 */
724	error = VOP_BMAP(vp, foff / bsize, &bo, &reqblock, NULL, NULL);
725	if (error == EOPNOTSUPP) {
726		VM_OBJECT_LOCK(object);
727
728		for (i = 0; i < count; i++)
729			if (i != reqpage) {
730				vm_page_lock(m[i]);
731				vm_page_free(m[i]);
732				vm_page_unlock(m[i]);
733			}
734		PCPU_INC(cnt.v_vnodein);
735		PCPU_INC(cnt.v_vnodepgsin);
736		error = vnode_pager_input_old(object, m[reqpage]);
737		VM_OBJECT_UNLOCK(object);
738		return (error);
739	} else if (error != 0) {
740		VM_OBJECT_LOCK(object);
741		for (i = 0; i < count; i++)
742			if (i != reqpage) {
743				vm_page_lock(m[i]);
744				vm_page_free(m[i]);
745				vm_page_unlock(m[i]);
746			}
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		for (i = 0; i < count; i++)
759			if (i != reqpage) {
760				vm_page_lock(m[i]);
761				vm_page_free(m[i]);
762				vm_page_unlock(m[i]);
763			}
764		VM_OBJECT_UNLOCK(object);
765		PCPU_INC(cnt.v_vnodein);
766		PCPU_INC(cnt.v_vnodepgsin);
767		return vnode_pager_input_smlfs(object, m[reqpage]);
768	}
769
770	/*
771	 * If we have a completely valid page available to us, we can
772	 * clean up and return.  Otherwise we have to re-read the
773	 * media.
774	 */
775	VM_OBJECT_LOCK(object);
776	if (m[reqpage]->valid == VM_PAGE_BITS_ALL) {
777		for (i = 0; i < count; i++)
778			if (i != reqpage) {
779				vm_page_lock(m[i]);
780				vm_page_free(m[i]);
781				vm_page_unlock(m[i]);
782			}
783		VM_OBJECT_UNLOCK(object);
784		return VM_PAGER_OK;
785	} else if (reqblock == -1) {
786		pmap_zero_page(m[reqpage]);
787		KASSERT(m[reqpage]->dirty == 0,
788		    ("vnode_pager_generic_getpages: page %p is dirty", m));
789		m[reqpage]->valid = VM_PAGE_BITS_ALL;
790		for (i = 0; i < count; i++)
791			if (i != reqpage) {
792				vm_page_lock(m[i]);
793				vm_page_free(m[i]);
794				vm_page_unlock(m[i]);
795			}
796		VM_OBJECT_UNLOCK(object);
797		return (VM_PAGER_OK);
798	}
799	m[reqpage]->valid = 0;
800	VM_OBJECT_UNLOCK(object);
801
802	/*
803	 * here on direct device I/O
804	 */
805	firstaddr = -1;
806
807	/*
808	 * calculate the run that includes the required page
809	 */
810	for (first = 0, i = 0; i < count; i = runend) {
811		if (vnode_pager_addr(vp, IDX_TO_OFF(m[i]->pindex), &firstaddr,
812		    &runpg) != 0) {
813			VM_OBJECT_LOCK(object);
814			for (; i < count; i++)
815				if (i != reqpage) {
816					vm_page_lock(m[i]);
817					vm_page_free(m[i]);
818					vm_page_unlock(m[i]);
819				}
820			VM_OBJECT_UNLOCK(object);
821			return (VM_PAGER_ERROR);
822		}
823		if (firstaddr == -1) {
824			VM_OBJECT_LOCK(object);
825			if (i == reqpage && foff < object->un_pager.vnp.vnp_size) {
826				panic("vnode_pager_getpages: unexpected missing page: firstaddr: %jd, foff: 0x%jx%08jx, vnp_size: 0x%jx%08jx",
827				    (intmax_t)firstaddr, (uintmax_t)(foff >> 32),
828				    (uintmax_t)foff,
829				    (uintmax_t)
830				    (object->un_pager.vnp.vnp_size >> 32),
831				    (uintmax_t)object->un_pager.vnp.vnp_size);
832			}
833			vm_page_lock(m[i]);
834			vm_page_free(m[i]);
835			vm_page_unlock(m[i]);
836			VM_OBJECT_UNLOCK(object);
837			runend = i + 1;
838			first = runend;
839			continue;
840		}
841		runend = i + runpg;
842		if (runend <= reqpage) {
843			VM_OBJECT_LOCK(object);
844			for (j = i; j < runend; j++) {
845				vm_page_lock(m[j]);
846				vm_page_free(m[j]);
847				vm_page_unlock(m[j]);
848			}
849			VM_OBJECT_UNLOCK(object);
850		} else {
851			if (runpg < (count - first)) {
852				VM_OBJECT_LOCK(object);
853				for (i = first + runpg; i < count; i++) {
854					vm_page_lock(m[i]);
855					vm_page_free(m[i]);
856					vm_page_unlock(m[i]);
857				}
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_vp = vp;
919	bp->b_bcount = size;
920	bp->b_bufsize = size;
921	bp->b_runningbufspace = bp->b_bufsize;
922	atomic_add_long(&runningbufspace, bp->b_runningbufspace);
923
924	PCPU_INC(cnt.v_vnodein);
925	PCPU_ADD(cnt.v_vnodepgsin, count);
926
927	/* do the input */
928	bp->b_iooffset = dbtob(bp->b_blkno);
929	bstrategy(bp);
930
931	bwait(bp, PVM, "vnread");
932
933	if ((bp->b_ioflags & BIO_ERROR) != 0)
934		error = EIO;
935
936	if (!error) {
937		if (size != count * PAGE_SIZE)
938			bzero((caddr_t) kva + size, PAGE_SIZE * count - size);
939	}
940	pmap_qremove(kva, count);
941
942	/*
943	 * free the buffer header back to the swap buffer pool
944	 */
945	bp->b_vp = NULL;
946	pbrelbo(bp);
947	relpbuf(bp, &vnode_pbuf_freecnt);
948
949	VM_OBJECT_LOCK(object);
950	for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) {
951		vm_page_t mt;
952
953		nextoff = tfoff + PAGE_SIZE;
954		mt = m[i];
955
956		if (nextoff <= object->un_pager.vnp.vnp_size) {
957			/*
958			 * Read filled up entire page.
959			 */
960			mt->valid = VM_PAGE_BITS_ALL;
961			KASSERT(mt->dirty == 0,
962			    ("vnode_pager_generic_getpages: page %p is dirty",
963			    mt));
964			KASSERT(!pmap_page_is_mapped(mt),
965			    ("vnode_pager_generic_getpages: page %p is mapped",
966			    mt));
967		} else {
968			/*
969			 * Read did not fill up entire page.
970			 *
971			 * Currently we do not set the entire page valid,
972			 * we just try to clear the piece that we couldn't
973			 * read.
974			 */
975			vm_page_set_valid_range(mt, 0,
976			    object->un_pager.vnp.vnp_size - tfoff);
977			KASSERT((mt->dirty & vm_page_bits(0,
978			    object->un_pager.vnp.vnp_size - tfoff)) == 0,
979			    ("vnode_pager_generic_getpages: page %p is dirty",
980			    mt));
981		}
982
983		if (i != reqpage)
984			vm_page_readahead_finish(mt);
985	}
986	VM_OBJECT_UNLOCK(object);
987	if (error) {
988		printf("vnode_pager_getpages: I/O read error\n");
989	}
990	return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
991}
992
993/*
994 * EOPNOTSUPP is no longer legal.  For local media VFS's that do not
995 * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
996 * vnode_pager_generic_putpages() to implement the previous behaviour.
997 *
998 * All other FS's should use the bypass to get to the local media
999 * backing vp's VOP_PUTPAGES.
1000 */
1001static void
1002vnode_pager_putpages(object, m, count, sync, rtvals)
1003	vm_object_t object;
1004	vm_page_t *m;
1005	int count;
1006	boolean_t sync;
1007	int *rtvals;
1008{
1009	int rtval;
1010	struct vnode *vp;
1011	int bytes = count * PAGE_SIZE;
1012
1013	/*
1014	 * Force synchronous operation if we are extremely low on memory
1015	 * to prevent a low-memory deadlock.  VOP operations often need to
1016	 * allocate more memory to initiate the I/O ( i.e. do a BMAP
1017	 * operation ).  The swapper handles the case by limiting the amount
1018	 * of asynchronous I/O, but that sort of solution doesn't scale well
1019	 * for the vnode pager without a lot of work.
1020	 *
1021	 * Also, the backing vnode's iodone routine may not wake the pageout
1022	 * daemon up.  This should be probably be addressed XXX.
1023	 */
1024
1025	if ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min)
1026		sync |= OBJPC_SYNC;
1027
1028	/*
1029	 * Call device-specific putpages function
1030	 */
1031	vp = object->handle;
1032	VM_OBJECT_UNLOCK(object);
1033	rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0);
1034	KASSERT(rtval != EOPNOTSUPP,
1035	    ("vnode_pager: stale FS putpages\n"));
1036	VM_OBJECT_LOCK(object);
1037}
1038
1039
1040/*
1041 * This is now called from local media FS's to operate against their
1042 * own vnodes if they fail to implement VOP_PUTPAGES.
1043 *
1044 * This is typically called indirectly via the pageout daemon and
1045 * clustering has already typically occured, so in general we ask the
1046 * underlying filesystem to write the data out asynchronously rather
1047 * then delayed.
1048 */
1049int
1050vnode_pager_generic_putpages(struct vnode *vp, vm_page_t *ma, int bytecount,
1051    int flags, int *rtvals)
1052{
1053	int i;
1054	vm_object_t object;
1055	vm_page_t m;
1056	int count;
1057
1058	int maxsize, ncount;
1059	vm_ooffset_t poffset;
1060	struct uio auio;
1061	struct iovec aiov;
1062	int error;
1063	int ioflags;
1064	int ppscheck = 0;
1065	static struct timeval lastfail;
1066	static int curfail;
1067
1068	object = vp->v_object;
1069	count = bytecount / PAGE_SIZE;
1070
1071	for (i = 0; i < count; i++)
1072		rtvals[i] = VM_PAGER_ERROR;
1073
1074	if ((int64_t)ma[0]->pindex < 0) {
1075		printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%lx)\n",
1076		    (long)ma[0]->pindex, (u_long)ma[0]->dirty);
1077		rtvals[0] = VM_PAGER_BAD;
1078		return VM_PAGER_BAD;
1079	}
1080
1081	maxsize = count * PAGE_SIZE;
1082	ncount = count;
1083
1084	poffset = IDX_TO_OFF(ma[0]->pindex);
1085
1086	/*
1087	 * If the page-aligned write is larger then the actual file we
1088	 * have to invalidate pages occuring beyond the file EOF.  However,
1089	 * there is an edge case where a file may not be page-aligned where
1090	 * the last page is partially invalid.  In this case the filesystem
1091	 * may not properly clear the dirty bits for the entire page (which
1092	 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
1093	 * With the page locked we are free to fix-up the dirty bits here.
1094	 *
1095	 * We do not under any circumstances truncate the valid bits, as
1096	 * this will screw up bogus page replacement.
1097	 */
1098	VM_OBJECT_LOCK(object);
1099	if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
1100		if (object->un_pager.vnp.vnp_size > poffset) {
1101			int pgoff;
1102
1103			maxsize = object->un_pager.vnp.vnp_size - poffset;
1104			ncount = btoc(maxsize);
1105			if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
1106				/*
1107				 * If the object is locked and the following
1108				 * conditions hold, then the page's dirty
1109				 * field cannot be concurrently changed by a
1110				 * pmap operation.
1111				 */
1112				m = ma[ncount - 1];
1113				KASSERT(m->busy > 0,
1114		("vnode_pager_generic_putpages: page %p is not busy", m));
1115				KASSERT(!pmap_page_is_write_mapped(m),
1116		("vnode_pager_generic_putpages: page %p is not read-only", m));
1117				vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
1118				    pgoff);
1119			}
1120		} else {
1121			maxsize = 0;
1122			ncount = 0;
1123		}
1124		if (ncount < count) {
1125			for (i = ncount; i < count; i++) {
1126				rtvals[i] = VM_PAGER_BAD;
1127			}
1128		}
1129	}
1130	VM_OBJECT_UNLOCK(object);
1131
1132	/*
1133	 * pageouts are already clustered, use IO_ASYNC to force a bawrite()
1134	 * rather then a bdwrite() to prevent paging I/O from saturating
1135	 * the buffer cache.  Dummy-up the sequential heuristic to cause
1136	 * large ranges to cluster.  If neither IO_SYNC or IO_ASYNC is set,
1137	 * the system decides how to cluster.
1138	 */
1139	ioflags = IO_VMIO;
1140	if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL))
1141		ioflags |= IO_SYNC;
1142	else if ((flags & VM_PAGER_CLUSTER_OK) == 0)
1143		ioflags |= IO_ASYNC;
1144	ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0;
1145	ioflags |= IO_SEQMAX << IO_SEQSHIFT;
1146
1147	aiov.iov_base = (caddr_t) 0;
1148	aiov.iov_len = maxsize;
1149	auio.uio_iov = &aiov;
1150	auio.uio_iovcnt = 1;
1151	auio.uio_offset = poffset;
1152	auio.uio_segflg = UIO_NOCOPY;
1153	auio.uio_rw = UIO_WRITE;
1154	auio.uio_resid = maxsize;
1155	auio.uio_td = (struct thread *) 0;
1156	error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred);
1157	PCPU_INC(cnt.v_vnodeout);
1158	PCPU_ADD(cnt.v_vnodepgsout, ncount);
1159
1160	if (error) {
1161		if ((ppscheck = ppsratecheck(&lastfail, &curfail, 1)))
1162			printf("vnode_pager_putpages: I/O error %d\n", error);
1163	}
1164	if (auio.uio_resid) {
1165		if (ppscheck || ppsratecheck(&lastfail, &curfail, 1))
1166			printf("vnode_pager_putpages: residual I/O %zd at %lu\n",
1167			    auio.uio_resid, (u_long)ma[0]->pindex);
1168	}
1169	for (i = 0; i < ncount; i++) {
1170		rtvals[i] = VM_PAGER_OK;
1171	}
1172	return rtvals[0];
1173}
1174
1175void
1176vnode_pager_undirty_pages(vm_page_t *ma, int *rtvals, int written)
1177{
1178	vm_object_t obj;
1179	int i, pos;
1180
1181	if (written == 0)
1182		return;
1183	obj = ma[0]->object;
1184	VM_OBJECT_LOCK(obj);
1185	for (i = 0, pos = 0; pos < written; i++, pos += PAGE_SIZE) {
1186		if (pos < trunc_page(written)) {
1187			rtvals[i] = VM_PAGER_OK;
1188			vm_page_undirty(ma[i]);
1189		} else {
1190			/* Partially written page. */
1191			rtvals[i] = VM_PAGER_AGAIN;
1192			vm_page_clear_dirty(ma[i], 0, written & PAGE_MASK);
1193		}
1194	}
1195	VM_OBJECT_UNLOCK(obj);
1196}
1197
1198void
1199vnode_pager_update_writecount(vm_object_t object, vm_offset_t start,
1200    vm_offset_t end)
1201{
1202	struct vnode *vp;
1203	vm_ooffset_t old_wm;
1204
1205	VM_OBJECT_LOCK(object);
1206	if (object->type != OBJT_VNODE) {
1207		VM_OBJECT_UNLOCK(object);
1208		return;
1209	}
1210	old_wm = object->un_pager.vnp.writemappings;
1211	object->un_pager.vnp.writemappings += (vm_ooffset_t)end - start;
1212	vp = object->handle;
1213	if (old_wm == 0 && object->un_pager.vnp.writemappings != 0) {
1214		ASSERT_VOP_ELOCKED(vp, "v_writecount inc");
1215		VOP_ADD_WRITECOUNT(vp, 1);
1216		CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d",
1217		    __func__, vp, vp->v_writecount);
1218	} else if (old_wm != 0 && object->un_pager.vnp.writemappings == 0) {
1219		ASSERT_VOP_ELOCKED(vp, "v_writecount dec");
1220		VOP_ADD_WRITECOUNT(vp, -1);
1221		CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
1222		    __func__, vp, vp->v_writecount);
1223	}
1224	VM_OBJECT_UNLOCK(object);
1225}
1226
1227void
1228vnode_pager_release_writecount(vm_object_t object, vm_offset_t start,
1229    vm_offset_t end)
1230{
1231	struct vnode *vp;
1232	struct mount *mp;
1233	vm_offset_t inc;
1234
1235	VM_OBJECT_LOCK(object);
1236
1237	/*
1238	 * First, recheck the object type to account for the race when
1239	 * the vnode is reclaimed.
1240	 */
1241	if (object->type != OBJT_VNODE) {
1242		VM_OBJECT_UNLOCK(object);
1243		return;
1244	}
1245
1246	/*
1247	 * Optimize for the case when writemappings is not going to
1248	 * zero.
1249	 */
1250	inc = end - start;
1251	if (object->un_pager.vnp.writemappings != inc) {
1252		object->un_pager.vnp.writemappings -= inc;
1253		VM_OBJECT_UNLOCK(object);
1254		return;
1255	}
1256
1257	vp = object->handle;
1258	vhold(vp);
1259	VM_OBJECT_UNLOCK(object);
1260	mp = NULL;
1261	vn_start_write(vp, &mp, V_WAIT);
1262	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1263
1264	/*
1265	 * Decrement the object's writemappings, by swapping the start
1266	 * and end arguments for vnode_pager_update_writecount().  If
1267	 * there was not a race with vnode reclaimation, then the
1268	 * vnode's v_writecount is decremented.
1269	 */
1270	vnode_pager_update_writecount(object, end, start);
1271	VOP_UNLOCK(vp, 0);
1272	vdrop(vp);
1273	if (mp != NULL)
1274		vn_finished_write(mp);
1275}
1276