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