summaryrefslogtreecommitdiffstats
path: root/sys/vm
diff options
context:
space:
mode:
authorjulian <julian@FreeBSD.org>1999-03-14 09:20:01 +0000
committerjulian <julian@FreeBSD.org>1999-03-14 09:20:01 +0000
commit0c3f3973d253c1aedd05a44d33615546f4ea3c9d (patch)
tree98d1b3e17dccced065927853ce8eff90fe432af1 /sys/vm
parent105e87d9a2deffdb189a0b80eacc001d79ed0fad (diff)
downloadFreeBSD-src-0c3f3973d253c1aedd05a44d33615546f4ea3c9d.zip
FreeBSD-src-0c3f3973d253c1aedd05a44d33615546f4ea3c9d.tar.gz
Submitted by: Matt Dillon <dillon@freebsd.org>
The old VN device broke in -4.x when the definition of B_PAGING changed. This patch fixes this plus implements additional capabilities. The new VN device can be backed by a file ( as per normal ), or it can be directly backed by swap. Due to dependencies in VM include files (on opt_xxx options) the new vn device cannot be a module yet. This will be fixed in a later commit. This commit delimitted by tags {PRE,POST}_MATT_VNDEV
Diffstat (limited to 'sys/vm')
-rw-r--r--sys/vm/swap_pager.c259
-rw-r--r--sys/vm/vm_pager.c156
-rw-r--r--sys/vm/vm_pager.h11
3 files changed, 406 insertions, 20 deletions
diff --git a/sys/vm/swap_pager.c b/sys/vm/swap_pager.c
index bfe2b39..85837e9 100644
--- a/sys/vm/swap_pager.c
+++ b/sys/vm/swap_pager.c
@@ -64,7 +64,7 @@
*
* @(#)swap_pager.c 8.9 (Berkeley) 3/21/94
*
- * $Id: swap_pager.c,v 1.115 1999/02/21 08:30:49 dillon Exp $
+ * $Id: swap_pager.c,v 1.116 1999/02/21 08:34:15 dillon Exp $
*/
#include <sys/param.h>
@@ -150,6 +150,7 @@ static void swap_pager_dealloc __P((vm_object_t object));
static int swap_pager_getpages __P((vm_object_t, vm_page_t *, int, int));
static void swap_pager_init __P((void));
static void swap_pager_unswapped __P((vm_page_t));
+static void swap_pager_strategy __P((vm_object_t, struct buf *));
struct pagerops swappagerops = {
swap_pager_init, /* early system initialization of pager */
@@ -158,7 +159,8 @@ struct pagerops swappagerops = {
swap_pager_getpages, /* pagein */
swap_pager_putpages, /* pageout */
swap_pager_haspage, /* get backing store status for page */
- swap_pager_unswapped /* remove swap related to page */
+ swap_pager_unswapped, /* remove swap related to page */
+ swap_pager_strategy /* pager strategy call */
};
/*
@@ -748,6 +750,230 @@ swap_pager_unswapped(m)
}
/*
+ * SWAP_PAGER_STRATEGY() - read, write, free blocks
+ *
+ * This implements the vm_pager_strategy() interface to swap and allows
+ * other parts of the system to directly access swap as backing store
+ * through vm_objects of type OBJT_SWAP. This is intended to be a
+ * cacheless interface ( i.e. caching occurs at higher levels ).
+ * Therefore we do not maintain any resident pages. All I/O goes
+ * directly from and to the swap device.
+ *
+ * Note that b_blkno is scaled for PAGE_SIZE
+ *
+ * We currently attempt to run I/O synchronously or asynchronously as
+ * the caller requests. This isn't perfect because we loose error
+ * sequencing when we run multiple ops in parallel to satisfy a request.
+ * But this is swap, so we let it all hang out.
+ */
+
+static void
+swap_pager_strategy(vm_object_t object, struct buf *bp)
+{
+ vm_pindex_t start;
+ int count;
+ char *data;
+ struct buf *nbp = NULL;
+
+ if (bp->b_bcount & PAGE_MASK) {
+ bp->b_error = EINVAL;
+ bp->b_flags |= B_ERROR | B_INVAL;
+ biodone(bp);
+ printf("swap_pager_strategy: bp %p b_vp %p blk %d size %d, not page bounded\n", bp, bp->b_vp, (int)bp->b_pblkno, (int)bp->b_bcount);
+ return;
+ }
+
+ /*
+ * Clear error indication, initialize page index, count, data pointer.
+ */
+
+ bp->b_error = 0;
+ bp->b_flags &= ~B_ERROR;
+ bp->b_resid = bp->b_bcount;
+
+ start = bp->b_pblkno;
+ count = howmany(bp->b_bcount, PAGE_SIZE);
+ data = bp->b_data;
+
+ /*
+ * Execute strategy function
+ */
+
+ if (bp->b_flags & B_FREEBUF) {
+ /*
+ * FREE PAGE(s) - destroy underlying swap that is no longer
+ * needed.
+ */
+ int s;
+
+ s = splvm();
+ swp_pager_meta_free(object, start, count);
+ splx(s);
+ bp->b_resid = 0;
+ } else if (bp->b_flags & B_READ) {
+ /*
+ * READ FROM SWAP - read directly from swap backing store,
+ * zero-fill as appropriate.
+ *
+ * Note: the count == 0 case is beyond the end of the
+ * buffer. This is a special case to close out any
+ * left over nbp.
+ */
+
+ while (count > 0) {
+ daddr_t blk;
+ int s;
+
+ s = splvm();
+ blk = swp_pager_meta_ctl(object, start, 0);
+ splx(s);
+
+ /*
+ * Do we have to flush our current collection?
+ */
+
+ if (
+ nbp && (
+ (blk & SWAPBLK_NONE) ||
+ nbp->b_blkno + btoc(nbp->b_bcount) != blk
+ )
+ ) {
+ ++cnt.v_swapin;
+ cnt.v_swappgsin += btoc(nbp->b_bcount);
+ flushchainbuf(nbp);
+ nbp = NULL;
+ }
+
+ /*
+ * Add to collection
+ */
+ if (blk & SWAPBLK_NONE) {
+ s = splbio();
+ bp->b_resid -= PAGE_SIZE;
+ splx(s);
+ bzero(data, PAGE_SIZE);
+ } else {
+ if (nbp == NULL) {
+ nbp = getchainbuf(bp, swapdev_vp, B_READ|B_ASYNC);
+ nbp->b_blkno = blk;
+ nbp->b_data = data;
+ }
+ nbp->b_bcount += PAGE_SIZE;
+ }
+ --count;
+ ++start;
+ data += PAGE_SIZE;
+ }
+ } else {
+ /*
+ * WRITE TO SWAP - [re]allocate swap and write.
+ */
+ while (count > 0) {
+ int i;
+ int s;
+ int n;
+ daddr_t blk;
+
+ n = min(count, BLIST_MAX_ALLOC);
+ n = min(n, nsw_cluster_max);
+
+ s = splvm();
+ for (;;) {
+ blk = swp_pager_getswapspace(n);
+ if (blk != SWAPBLK_NONE)
+ break;
+ n >>= 1;
+ if (n == 0)
+ break;
+ }
+ if (n == 0) {
+ bp->b_error = ENOMEM;
+ bp->b_flags |= B_ERROR;
+ splx(s);
+ break;
+ }
+
+ /*
+ * Oops, too big if it crosses a stripe
+ *
+ * 1111000000
+ * 111111
+ * 1000001
+ */
+ if ((blk ^ (blk + n)) & dmmax_mask) {
+ int j = ((blk + dmmax) & dmmax_mask) - blk;
+ swp_pager_freeswapspace(blk + j, n - j);
+ n = j;
+ }
+
+ swp_pager_meta_free(object, start, n);
+
+ splx(s);
+
+ if (nbp) {
+ ++cnt.v_swapout;
+ cnt.v_swappgsout += btoc(nbp->b_bcount);
+ flushchainbuf(nbp);
+ }
+
+ nbp = getchainbuf(bp, swapdev_vp, B_ASYNC);
+
+ nbp->b_blkno = blk;
+ nbp->b_data = data;
+ nbp->b_bcount = PAGE_SIZE * n;
+
+ /*
+ * Must set dirty range for NFS to work. dirtybeg &
+ * off are already 0.
+ */
+ nbp->b_dirtyend = nbp->b_bcount;
+
+ ++cnt.v_swapout;
+ cnt.v_swappgsout += n;
+
+ s = splbio();
+ for (i = 0; i < n; ++i) {
+ swp_pager_meta_build(
+ object,
+ start + i,
+ blk + i,
+ 1
+ );
+ }
+ splx(s);
+
+ count -= n;
+ start += n;
+ data += PAGE_SIZE * n;
+ }
+ }
+
+ /*
+ * Cleanup. Commit last nbp either async or sync, and either
+ * wait for it synchronously or make it auto-biodone itself and
+ * the parent bp.
+ */
+
+ if (nbp) {
+ if ((bp->b_flags & B_ASYNC) == 0)
+ nbp->b_flags &= ~B_ASYNC;
+ if (nbp->b_flags & B_READ) {
+ ++cnt.v_swapin;
+ cnt.v_swappgsin += btoc(nbp->b_bcount);
+ } else {
+ ++cnt.v_swapout;
+ cnt.v_swappgsout += btoc(nbp->b_bcount);
+ }
+ flushchainbuf(nbp);
+ }
+ if (bp->b_flags & B_ASYNC) {
+ autochaindone(bp);
+ } else {
+ waitchainbuf(bp, 0, 1);
+ }
+}
+
+/*
* SWAP_PAGER_GETPAGES() - bring pages in from swap
*
* Attempt to retrieve (m, count) pages from backing store, but make
@@ -886,9 +1112,9 @@ swap_pager_getpages(object, m, count, reqpage)
bp->b_iodone = swp_pager_async_iodone;
bp->b_proc = &proc0; /* XXX (but without B_PHYS set this is ok) */
bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
+ bp->b_data = (caddr_t) kva;
crhold(bp->b_rcred);
crhold(bp->b_wcred);
- bp->b_data = (caddr_t) kva;
/*
* b_blkno is in page-sized chunks. swapblk is valid, too, so
* we don't have to mask it against SWAPBLK_MASK.
@@ -1039,7 +1265,7 @@ swap_pager_putpages(object, m, count, sync, rtvals)
/*
* Step 2
*
- * Update nsw parameters from swap_async_max sysctl values.
+ * Update nsw parameters from swap_async_max sysctl values.
* Do not let the sysop crash the machine with bogus numbers.
*/
@@ -1133,10 +1359,10 @@ swap_pager_putpages(object, m, count, sync, rtvals)
if (sync == TRUE) {
bp = getpbuf(&nsw_wcount_sync);
- bp->b_flags = B_BUSY;
+ bp->b_flags = B_BUSY | B_CALL;
} else {
bp = getpbuf(&nsw_wcount_async);
- bp->b_flags = B_BUSY | B_ASYNC;
+ bp->b_flags = B_BUSY | B_CALL | B_ASYNC;
}
bp->b_spc = NULL; /* not used, but NULL-out anyway */
@@ -1144,17 +1370,15 @@ swap_pager_putpages(object, m, count, sync, rtvals)
bp->b_proc = &proc0; /* XXX (but without B_PHYS this is ok) */
bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
-
- if (bp->b_rcred != NOCRED)
- crhold(bp->b_rcred);
- if (bp->b_wcred != NOCRED)
- crhold(bp->b_wcred);
- pbgetvp(swapdev_vp, bp);
-
bp->b_bcount = PAGE_SIZE * n;
bp->b_bufsize = PAGE_SIZE * n;
bp->b_blkno = blk;
+ crhold(bp->b_rcred);
+ crhold(bp->b_wcred);
+
+ pbgetvp(swapdev_vp, bp);
+
s = splvm();
for (j = 0; j < n; ++j) {
@@ -1172,8 +1396,12 @@ swap_pager_putpages(object, m, count, sync, rtvals)
vm_page_flag_set(mreq, PG_SWAPINPROG);
bp->b_pages[j] = mreq;
}
- bp->b_flags |= B_CALL;
bp->b_npages = n;
+ /*
+ * Must set dirty range for NFS to work.
+ */
+ bp->b_dirtyoff = 0;
+ bp->b_dirtyend = bp->b_bcount;
cnt.v_swapout++;
cnt.v_swappgsout += bp->b_npages;
@@ -1187,8 +1415,6 @@ swap_pager_putpages(object, m, count, sync, rtvals)
if (sync == FALSE) {
bp->b_iodone = swp_pager_async_iodone;
- bp->b_dirtyoff = 0;
- bp->b_dirtyend = bp->b_bcount;
VOP_STRATEGY(bp->b_vp, bp);
for (j = 0; j < n; ++j)
@@ -1220,7 +1446,6 @@ swap_pager_putpages(object, m, count, sync, rtvals)
for (j = 0; j < n; ++j)
rtvals[i+j] = VM_PAGER_PEND;
-
/*
* Now that we are through with the bp, we can call the
* normal async completion, which frees everything up.
diff --git a/sys/vm/vm_pager.c b/sys/vm/vm_pager.c
index 2b8eb64..4645935 100644
--- a/sys/vm/vm_pager.c
+++ b/sys/vm/vm_pager.c
@@ -61,7 +61,7 @@
* any improvements or extensions that they make and grant Carnegie the
* rights to redistribute these changes.
*
- * $Id: vm_pager.c,v 1.42 1999/01/21 10:15:24 dillon Exp $
+ * $Id: vm_pager.c,v 1.43 1999/01/24 02:32:15 dillon Exp $
*/
/*
@@ -72,9 +72,11 @@
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
+#include <sys/vnode.h>
#include <sys/buf.h>
#include <sys/ucred.h>
#include <sys/malloc.h>
+#include <sys/proc.h>
#include <vm/vm.h>
#include <vm/vm_param.h>
@@ -252,6 +254,25 @@ vm_pager_deallocate(object)
}
/*
+ * vm_pager_strategy:
+ *
+ * called with no specific spl
+ * Execute strategy routine directly to pager.
+ */
+
+void
+vm_pager_strategy(vm_object_t object, struct buf *bp)
+{
+ if (pagertab[object->type]->pgo_strategy) {
+ (*pagertab[object->type]->pgo_strategy)(object, bp);
+ } else {
+ bp->b_flags |= B_ERROR;
+ bp->b_error = ENXIO;
+ biodone(bp);
+ }
+}
+
+/*
* vm_pager_get_pages() - inline, see vm/vm_pager.h
* vm_pager_put_pages() - inline, see vm/vm_pager.h
* vm_pager_has_page() - inline, see vm/vm_pager.h
@@ -442,3 +463,136 @@ relpbuf(bp, pfreecnt)
}
splx(s);
}
+
+/********************************************************
+ * CHAINING FUNCTIONS *
+ ********************************************************
+ *
+ * These functions support recursion of I/O operations
+ * on bp's, typically by chaining one or more 'child' bp's
+ * to the parent. Synchronous, asynchronous, and semi-synchronous
+ * chaining is possible.
+ */
+
+/*
+ * vm_pager_chain_iodone:
+ *
+ * io completion routine for child bp. Currently we fudge a bit
+ * on dealing with b_resid. Since users of these routines may issue
+ * multiple children simultaniously, sequencing of the error can be lost.
+ */
+
+static void
+vm_pager_chain_iodone(struct buf *nbp)
+{
+ struct buf *bp;
+
+ if ((bp = nbp->b_chain.parent) != NULL) {
+ if (nbp->b_flags & B_ERROR) {
+ bp->b_flags |= B_ERROR;
+ bp->b_error = nbp->b_error;
+ } else if (nbp->b_resid != 0) {
+ bp->b_flags |= B_ERROR;
+ bp->b_error = EINVAL;
+ } else {
+ bp->b_resid -= nbp->b_bcount;
+ }
+ nbp->b_chain.parent = NULL;
+ --bp->b_chain.count;
+ if (bp->b_flags & B_WANTED) {
+ bp->b_flags &= ~B_WANTED;
+ wakeup(bp);
+ }
+ if (!bp->b_chain.count && (bp->b_flags & B_AUTOCHAINDONE)) {
+ bp->b_flags &= ~B_AUTOCHAINDONE;
+ if (bp->b_resid != 0 && !(bp->b_flags & B_ERROR)) {
+ bp->b_flags |= B_ERROR;
+ bp->b_error = EINVAL;
+ }
+ biodone(bp);
+ }
+ }
+ nbp->b_flags |= B_DONE;
+ nbp->b_flags &= ~(B_ASYNC|B_WANTED);
+ relpbuf(nbp, NULL);
+}
+
+/*
+ * getchainbuf:
+ *
+ * Obtain a physical buffer and chain it to its parent buffer. When
+ * I/O completes, the parent buffer will be B_SIGNAL'd. Errors are
+ * automatically propogated to the parent
+ */
+
+struct buf *
+getchainbuf(struct buf *bp, struct vnode *vp, int flags)
+{
+ struct buf *nbp = getpbuf(NULL);
+
+ nbp->b_chain.parent = bp;
+ ++bp->b_chain.count;
+
+ if (bp->b_chain.count > 4)
+ waitchainbuf(bp, 4, 0);
+
+ nbp->b_flags = B_BUSY | B_CALL | (bp->b_flags & B_ORDERED) | flags;
+ nbp->b_proc = &proc0;
+ nbp->b_rcred = nbp->b_proc->p_ucred;
+ nbp->b_wcred = nbp->b_proc->p_ucred;
+ nbp->b_iodone = vm_pager_chain_iodone;
+
+ crhold(nbp->b_rcred);
+ crhold(nbp->b_wcred);
+
+ if (vp)
+ pbgetvp(vp, nbp);
+ return(nbp);
+}
+
+void
+flushchainbuf(struct buf *nbp)
+{
+ if (nbp->b_bcount) {
+ nbp->b_bufsize = nbp->b_bcount;
+ if ((nbp->b_flags & B_READ) == 0)
+ nbp->b_dirtyend = nbp->b_bcount;
+ VOP_STRATEGY(nbp->b_vp, nbp);
+ } else {
+ biodone(nbp);
+ }
+}
+
+void
+waitchainbuf(struct buf *bp, int count, int done)
+{
+ int s;
+
+ s = splbio();
+ while (bp->b_chain.count > count) {
+ bp->b_flags |= B_WANTED;
+ tsleep(bp, PRIBIO + 4, "bpchain", 0);
+ }
+ if (done) {
+ if (bp->b_resid != 0 && !(bp->b_flags & B_ERROR)) {
+ bp->b_flags |= B_ERROR;
+ bp->b_error = EINVAL;
+ }
+ biodone(bp);
+ }
+ splx(s);
+}
+
+void
+autochaindone(struct buf *bp)
+{
+ int s;
+
+ s = splbio();
+ if (bp->b_chain.count == 0)
+ biodone(bp);
+ else
+ bp->b_flags |= B_AUTOCHAINDONE;
+ splx(s);
+}
+
diff --git a/sys/vm/vm_pager.h b/sys/vm/vm_pager.h
index e0b402e..82b6574 100644
--- a/sys/vm/vm_pager.h
+++ b/sys/vm/vm_pager.h
@@ -36,7 +36,7 @@
* SUCH DAMAGE.
*
* @(#)vm_pager.h 8.4 (Berkeley) 1/12/94
- * $Id: vm_pager.h,v 1.19 1999/01/21 10:15:47 dillon Exp $
+ * $Id: vm_pager.h,v 1.20 1999/01/24 02:32:15 dillon Exp $
*/
/*
@@ -50,6 +50,8 @@
TAILQ_HEAD(pagerlst, vm_object);
+struct buf;
+
struct pagerops {
void (*pgo_init) __P((void)); /* Initialize pager. */
vm_object_t (*pgo_alloc) __P((void *, vm_ooffset_t, vm_prot_t, vm_ooffset_t)); /* Allocate pager. */
@@ -58,6 +60,7 @@ struct pagerops {
void (*pgo_putpages) __P((vm_object_t, vm_page_t *, int, int, int *)); /* Put (write) page. */
boolean_t (*pgo_haspage) __P((vm_object_t, vm_pindex_t, int *, int *)); /* Does pager have page? */
void (*pgo_pageunswapped) __P((vm_page_t));
+ void (*pgo_strategy) __P((vm_object_t, struct buf *));
};
/*
@@ -101,6 +104,11 @@ vm_offset_t vm_pager_map_page __P((vm_page_t));
void vm_pager_sync __P((void));
void vm_pager_unmap_pages __P((vm_offset_t, int));
void vm_pager_unmap_page __P((vm_offset_t));
+void vm_pager_strategy __P((vm_object_t object, struct buf *bp));
+struct buf *getchainbuf(struct buf *bp, struct vnode *vp, int flags);
+void flushchainbuf(struct buf *nbp);
+void waitchainbuf(struct buf *bp, int count, int done);
+void autochaindone(struct buf *bp);
static __inline int
vm_pager_get_pages(
@@ -149,7 +157,6 @@ vm_pager_page_unswapped(vm_page_t m)
(*pagertab[m->object->type]->pgo_pageunswapped)(m);
}
-
#endif
#endif /* _VM_PAGER_ */
OpenPOWER on IntegriCloud