2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)vm_swap.c 8.5 (Berkeley) 2/17/94
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/sysproto.h>
45 #include <sys/namei.h>
46 #include <sys/dmap.h> /* XXX */
47 #include <sys/vnode.h>
48 #include <sys/fcntl.h>
49 #include <sys/blist.h>
50 #include <sys/kernel.h>
54 #include <sys/sysctl.h>
56 #include <vm/vm_extern.h>
57 #include <vm/vm_zone.h>
58 #include <vm/swap_pager.h>
61 * Indirect driver for multi-controller paging.
67 static struct swdevt should_be_malloced[NSWAPDEV];
68 struct swdevt *swdevt = should_be_malloced;
69 static int nswap; /* first block after the interleaved devs */
70 int nswdev = NSWAPDEV;
72 static int ncswdev = 0; /* # of configured swap devs */
73 SYSCTL_INT(_vm, OID_AUTO, nswapdev, CTLFLAG_RD,
74 &ncswdev, 0, "Number of swap devices");
76 static int swapdev_strategy __P((struct vop_strategy_args *ap));
77 struct vnode *swapdev_vp;
82 * VOP_STRATEGY() for swapdev_vp.
83 * Perform swap strategy interleave device selection.
85 * The bp is expected to be locked and *not* B_DONE on call.
90 struct vop_strategy_args /* {
95 int s, sz, off, seg, index;
96 register struct swdevt *sp;
101 sz = howmany(bp->b_bcount, PAGE_SIZE);
104 * Convert interleaved swap into per-device swap. Note that
105 * the block size is left in PAGE_SIZE'd chunks (for the newswap)
109 off = bp->b_blkno % dmmax;
110 if (off + sz > dmmax) {
111 bp->b_error = EINVAL;
112 bp->b_ioflags |= BIO_ERROR;
116 seg = bp->b_blkno / dmmax;
117 index = seg % nswdev;
119 bp->b_blkno = seg * dmmax + off;
124 if (bp->b_blkno + sz > sp->sw_nblks) {
125 bp->b_error = EINVAL;
126 bp->b_ioflags |= BIO_ERROR;
130 bp->b_dev = sp->sw_device;
131 if (sp->sw_vp == NULL) {
132 bp->b_error = ENODEV;
133 bp->b_ioflags |= BIO_ERROR;
139 * Convert from PAGE_SIZE'd to DEV_BSIZE'd chunks for the actual I/O
141 bp->b_blkno = ctodb(bp->b_blkno);
145 if (bp->b_iocmd == BIO_WRITE) {
149 if ((vp->v_flag & VBWAIT) && vp->v_numoutput <= 0) {
150 vp->v_flag &= ~VBWAIT;
151 wakeup(&vp->v_numoutput);
154 sp->sw_vp->v_numoutput++;
156 pbreassignbuf(bp, sp->sw_vp);
163 * Create a special vnode op vector for swapdev_vp - we only use
164 * VOP_STRATEGY(), everything else returns an error.
166 vop_t **swapdev_vnodeop_p;
167 static struct vnodeopv_entry_desc swapdev_vnodeop_entries[] = {
168 { &vop_default_desc, (vop_t *) vop_defaultop },
169 { &vop_strategy_desc, (vop_t *) swapdev_strategy },
172 static struct vnodeopv_desc swapdev_vnodeop_opv_desc =
173 { &swapdev_vnodeop_p, swapdev_vnodeop_entries };
175 VNODEOP_SET(swapdev_vnodeop_opv_desc);
178 * System call swapon(name) enables swapping on device name,
179 * which must be in the swdevsw. Return EBUSY
180 * if already swapping on this device.
182 #ifndef _SYS_SYSPROTO_H_
192 struct swapon_args *uap;
194 register struct vnode *vp;
203 * Swap metadata may not fit in the KVM if we have physical
206 if (swap_zone == NULL)
209 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->name, p);
214 NDFREE(&nd, NDF_ONLY_PNBUF);
217 vn_isdisk(vp, &error);
220 error = swaponvp(p, vp, vp->v_rdev, 0);
229 * Swfree(index) frees the index'th portion of the swap map.
230 * Each of the nswdev devices provides 1/nswdev'th of the swap
231 * space, which is laid out with blocks of dmmax pages circularly
234 * The new swap code uses page-sized blocks. The old swap code used
235 * DEV_BSIZE'd chunks.
237 * XXX locking when multiple swapon's run in parallel
240 swaponvp(p, vp, dev, nblks)
247 register struct swdevt *sp;
248 register swblk_t vsbase;
252 u_long aligned_nblks;
253 struct sysctl_oid *oid;
257 error = getnewvnode(VT_NON, NULL, swapdev_vnodeop_p,
260 panic("Cannot get vnode for swapdev");
261 swapdev_vp->v_type = VNON; /* Untyped */
264 ASSERT_VOP_UNLOCKED(vp, "swaponvp");
265 for (sp = swdevt, index = 0 ; index < nswdev; index++, sp++) {
274 (void) vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
275 error = VOP_OPEN(vp, FREAD | FWRITE, p->p_ucred, p);
276 (void) VOP_UNLOCK(vp, 0, p);
280 if (nblks == 0 && dev != NODEV && (devsw(dev)->d_psize == 0 ||
281 (nblks = (*devsw(dev)->d_psize) (dev)) == -1)) {
282 (void) VOP_CLOSE(vp, FREAD | FWRITE, p->p_ucred, p);
286 (void) VOP_CLOSE(vp, FREAD | FWRITE, p->p_ucred, p);
291 * If we go beyond this, we get overflows in the radix
294 if (nblks > 0x40000000 / BLIST_META_RADIX / nswdev) {
295 printf("exceeded maximum of %d blocks per swap unit\n",
296 0x40000000 / BLIST_META_RADIX / nswdev);
297 (void) VOP_CLOSE(vp, FREAD | FWRITE, p->p_ucred, p);
301 * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks.
302 * First chop nblks off to page-align it, then convert.
304 * sw->sw_nblks is in page-sized chunks now too.
306 nblks &= ~(ctodb(1) - 1);
307 nblks = dbtoc(nblks);
310 sp->sw_dev = dev2udev(dev);
312 sp->sw_flags |= SW_FREED;
313 sp->sw_nblks = nblks;
317 * nblks, nswap, and dmmax are PAGE_SIZE'd parameters now, not
318 * DEV_BSIZE'd. aligned_nblks is used to calculate the
319 * size of the swap bitmap, taking into account the stripe size.
321 aligned_nblks = (nblks + (dmmax - 1)) & ~(u_long)(dmmax - 1);
323 if (aligned_nblks * nswdev > nswap)
324 nswap = aligned_nblks * nswdev;
326 if (swapblist == NULL)
327 swapblist = blist_create(nswap);
329 blist_resize(&swapblist, nswap, 0);
331 for (dvbase = dmmax; dvbase < nblks; dvbase += dmmax) {
332 blk = min(nblks - dvbase, dmmax);
333 vsbase = index * dmmax + dvbase * nswdev;
334 blist_free(swapblist, vsbase, blk);
338 * Add sysctl entries for new swap area. XXX: if some day swap devices
339 * can be removed, add an oid member to swdevt.
341 if (snprintf(name, sizeof(name), "swapdev%d", ncswdev) < sizeof(name)) {
342 oid = SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_vm),
343 OID_AUTO, name, CTLFLAG_RD, NULL, "Swap device stats");
345 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
346 "flags", CTLFLAG_RD, &sp->sw_flags, 0, "Flags");
347 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
348 "nblks", CTLFLAG_RD, &sp->sw_nblks, 0,
350 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
351 "used", CTLFLAG_RD, &sp->sw_used, 0,
352 "Number of blocks in use");
353 SYSCTL_ADD_OPAQUE(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
354 "dev", CTLFLAG_RD, &sp->sw_dev, sizeof(sp->sw_dev),
355 "T,dev_t", "Device");
358 printf("XXX: swaponvp() name buffer too small!\n");