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