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/vm_param.h>
59 #include <vm/swap_pager.h>
62 * Indirect driver for multi-controller paging.
68 static struct swdevt should_be_malloced[NSWAPDEV];
69 struct swdevt *swdevt = should_be_malloced;
70 static int nswap; /* first block after the interleaved devs */
71 int nswdev = NSWAPDEV;
74 static int swapdev_strategy __P((struct vop_strategy_args *ap));
75 struct vnode *swapdev_vp;
80 * VOP_STRATEGY() for swapdev_vp.
81 * Perform swap strategy interleave device selection.
83 * The bp is expected to be locked and *not* B_DONE on call.
88 struct vop_strategy_args /* {
93 int s, sz, off, seg, index;
99 sz = howmany(bp->b_bcount, PAGE_SIZE);
102 * Convert interleaved swap into per-device swap. Note that
103 * the block size is left in PAGE_SIZE'd chunks (for the newswap)
107 off = bp->b_blkno % dmmax;
108 if (off + sz > dmmax) {
109 bp->b_error = EINVAL;
110 bp->b_ioflags |= BIO_ERROR;
114 seg = bp->b_blkno / dmmax;
115 index = seg % nswdev;
117 bp->b_blkno = seg * dmmax + off;
122 if (bp->b_blkno + sz > sp->sw_nblks) {
123 bp->b_error = EINVAL;
124 bp->b_ioflags |= BIO_ERROR;
128 bp->b_dev = sp->sw_device;
129 if (sp->sw_vp == NULL) {
130 bp->b_error = ENODEV;
131 bp->b_ioflags |= BIO_ERROR;
137 * Convert from PAGE_SIZE'd to DEV_BSIZE'd chunks for the actual I/O
139 bp->b_blkno = ctodb(bp->b_blkno);
143 if (bp->b_iocmd == BIO_WRITE) {
147 if ((vp->v_flag & VBWAIT) && vp->v_numoutput <= 0) {
148 vp->v_flag &= ~VBWAIT;
149 wakeup(&vp->v_numoutput);
152 sp->sw_vp->v_numoutput++;
154 pbreassignbuf(bp, sp->sw_vp);
161 * Create a special vnode op vector for swapdev_vp - we only use
162 * VOP_STRATEGY(), everything else returns an error.
164 vop_t **swapdev_vnodeop_p;
165 static struct vnodeopv_entry_desc swapdev_vnodeop_entries[] = {
166 { &vop_default_desc, (vop_t *) vop_defaultop },
167 { &vop_strategy_desc, (vop_t *) swapdev_strategy },
170 static struct vnodeopv_desc swapdev_vnodeop_opv_desc =
171 { &swapdev_vnodeop_p, swapdev_vnodeop_entries };
173 VNODEOP_SET(swapdev_vnodeop_opv_desc);
176 * System call swapon(name) enables swapping on device name,
177 * which must be in the swdevsw. Return EBUSY
178 * if already swapping on this device.
180 #ifndef _SYS_SYSPROTO_H_
190 struct swapon_args *uap;
201 * Swap metadata may not fit in the KVM if we have physical
204 if (swap_zone == NULL)
207 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->name, p);
212 NDFREE(&nd, NDF_ONLY_PNBUF);
215 vn_isdisk(vp, &error);
218 error = swaponvp(p, vp, vp->v_rdev, 0);
227 * Swfree(index) frees the index'th portion of the swap map.
228 * Each of the nswdev devices provides 1/nswdev'th of the swap
229 * space, which is laid out with blocks of dmmax pages circularly
232 * The new swap code uses page-sized blocks. The old swap code used
233 * DEV_BSIZE'd chunks.
235 * XXX locking when multiple swapon's run in parallel
238 swaponvp(p, vp, dev, nblks)
250 u_long aligned_nblks;
253 error = getnewvnode(VT_NON, NULL, swapdev_vnodeop_p,
256 panic("Cannot get vnode for swapdev");
257 swapdev_vp->v_type = VNON; /* Untyped */
260 ASSERT_VOP_UNLOCKED(vp, "swaponvp");
261 for (sp = swdevt, index = 0 ; index < nswdev; index++, sp++) {
270 (void) vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
271 error = VOP_OPEN(vp, FREAD | FWRITE, p->p_ucred, p);
272 (void) VOP_UNLOCK(vp, 0, p);
276 if (nblks == 0 && dev != NODEV && (devsw(dev)->d_psize == 0 ||
277 (nblks = (*devsw(dev)->d_psize) (dev)) == -1)) {
278 (void) VOP_CLOSE(vp, FREAD | FWRITE, p->p_ucred, p);
282 (void) VOP_CLOSE(vp, FREAD | FWRITE, p->p_ucred, p);
287 * If we go beyond this, we get overflows in the radix
290 if (nblks > 0x40000000 / BLIST_META_RADIX / nswdev) {
291 printf("exceeded maximum of %d blocks per swap unit\n",
292 0x40000000 / BLIST_META_RADIX / nswdev);
293 (void) VOP_CLOSE(vp, FREAD | FWRITE, p->p_ucred, p);
297 * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks.
298 * First chop nblks off to page-align it, then convert.
300 * sw->sw_nblks is in page-sized chunks now too.
302 nblks &= ~(ctodb(1) - 1);
303 nblks = dbtoc(nblks);
306 sp->sw_dev = dev2udev(dev);
308 sp->sw_flags |= SW_FREED;
309 sp->sw_nblks = nblks;
313 * nblks, nswap, and dmmax are PAGE_SIZE'd parameters now, not
314 * DEV_BSIZE'd. aligned_nblks is used to calculate the
315 * size of the swap bitmap, taking into account the stripe size.
317 aligned_nblks = (nblks + (dmmax - 1)) & ~(u_long)(dmmax - 1);
319 if (aligned_nblks * nswdev > nswap)
320 nswap = aligned_nblks * nswdev;
322 if (swapblist == NULL)
323 swapblist = blist_create(nswap);
325 blist_resize(&swapblist, nswap, 0);
327 for (dvbase = dmmax; dvbase < nblks; dvbase += dmmax) {
328 blk = min(nblks - dvbase, dmmax);
329 vsbase = index * dmmax + dvbase * nswdev;
330 blist_free(swapblist, vsbase, blk);
338 sysctl_vm_swap_info(SYSCTL_HANDLER_ARGS)
340 int *name = (int *)arg1;
345 if (arg2 != 1) /* name length */
348 for (sp = swdevt, i = 0, n = 0 ; i < nswdev; i++, sp++) {
351 xs.xsw_version = XSWDEV_VERSION;
352 xs.xsw_dev = sp->sw_dev;
353 xs.xsw_flags = sp->sw_flags;
354 xs.xsw_nblks = sp->sw_nblks;
355 xs.xsw_used = sp->sw_used;
357 error = SYSCTL_OUT(req, &xs, sizeof(xs));
367 SYSCTL_NODE(_vm, OID_AUTO, swap_info, CTLFLAG_RD, sysctl_vm_swap_info,
368 "Swap statistics by device");