xref: /dragonfly/sys/vm/vm_swap.c (revision 1de703da)
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
20  *
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
31  * SUCH DAMAGE.
32  *
33  *	@(#)vm_swap.c	8.5 (Berkeley) 2/17/94
34  * $FreeBSD: src/sys/vm/vm_swap.c,v 1.96.2.2 2001/10/14 18:46:47 iedowse Exp $
35  * $DragonFly: src/sys/vm/vm_swap.c,v 1.2 2003/06/17 04:29:00 dillon Exp $
36  */
37 
38 #include "opt_swap.h"
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/sysproto.h>
43 #include <sys/buf.h>
44 #include <sys/proc.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>
51 #include <sys/lock.h>
52 #include <sys/conf.h>
53 #include <sys/stat.h>
54 #include <vm/vm.h>
55 #include <vm/vm_extern.h>
56 #include <vm/swap_pager.h>
57 #include <vm/vm_zone.h>
58 
59 /*
60  * Indirect driver for multi-controller paging.
61  */
62 
63 #ifndef NSWAPDEV
64 #define NSWAPDEV	4
65 #endif
66 static struct swdevt should_be_malloced[NSWAPDEV];
67 static struct swdevt *swdevt = should_be_malloced;
68 static int nswap;		/* first block after the interleaved devs */
69 static int nswdev = NSWAPDEV;
70 int vm_swap_size;
71 
72 static int swapdev_strategy __P((struct vop_strategy_args *ap));
73 struct vnode *swapdev_vp;
74 
75 /*
76  *	swapdev_strategy:
77  *
78  *	VOP_STRATEGY() for swapdev_vp.
79  *	Perform swap strategy interleave device selection.
80  *
81  *	The bp is expected to be locked and *not* B_DONE on call.
82  */
83 
84 static int
85 swapdev_strategy(ap)
86 	struct vop_strategy_args /* {
87 		struct vnode *a_vp;
88 		struct buf *a_bp;
89 	} */ *ap;
90 {
91 	int s, sz, off, seg, index;
92 	register struct swdevt *sp;
93 	struct vnode *vp;
94 	struct buf *bp;
95 
96 	bp = ap->a_bp;
97 	sz = howmany(bp->b_bcount, PAGE_SIZE);
98 
99 	/*
100 	 * Convert interleaved swap into per-device swap.  Note that
101 	 * the block size is left in PAGE_SIZE'd chunks (for the newswap)
102 	 * here.
103 	 */
104 	if (nswdev > 1) {
105 		off = bp->b_blkno % dmmax;
106 		if (off + sz > dmmax) {
107 			bp->b_error = EINVAL;
108 			bp->b_flags |= B_ERROR;
109 			biodone(bp);
110 			return 0;
111 		}
112 		seg = bp->b_blkno / dmmax;
113 		index = seg % nswdev;
114 		seg /= nswdev;
115 		bp->b_blkno = seg * dmmax + off;
116 	} else {
117 		index = 0;
118 	}
119 	sp = &swdevt[index];
120 	if (bp->b_blkno + sz > sp->sw_nblks) {
121 		bp->b_error = EINVAL;
122 		bp->b_flags |= B_ERROR;
123 		biodone(bp);
124 		return 0;
125 	}
126 	bp->b_dev = sp->sw_device;
127 	if (sp->sw_vp == NULL) {
128 		bp->b_error = ENODEV;
129 		bp->b_flags |= B_ERROR;
130 		biodone(bp);
131 		return 0;
132 	}
133 
134 	/*
135 	 * Convert from PAGE_SIZE'd to DEV_BSIZE'd chunks for the actual I/O
136 	 */
137 	bp->b_blkno = ctodb(bp->b_blkno);
138 
139 	vhold(sp->sw_vp);
140 	s = splvm();
141 	if ((bp->b_flags & B_READ) == 0) {
142 		vp = bp->b_vp;
143 		if (vp) {
144 			vp->v_numoutput--;
145 			if ((vp->v_flag & VBWAIT) && vp->v_numoutput <= 0) {
146 				vp->v_flag &= ~VBWAIT;
147 				wakeup(&vp->v_numoutput);
148 			}
149 		}
150 		sp->sw_vp->v_numoutput++;
151 	}
152 	pbreassignbuf(bp, sp->sw_vp);
153 	splx(s);
154 	VOP_STRATEGY(bp->b_vp, bp);
155 	return 0;
156 }
157 
158 /*
159  * Create a special vnode op vector for swapdev_vp - we only use
160  * VOP_STRATEGY(), everything else returns an error.
161  */
162 vop_t **swapdev_vnodeop_p;
163 static struct vnodeopv_entry_desc swapdev_vnodeop_entries[] = {
164 	{ &vop_default_desc,		(vop_t *) vop_defaultop },
165 	{ &vop_strategy_desc,		(vop_t *) swapdev_strategy },
166 	{ NULL, NULL }
167 };
168 static struct vnodeopv_desc swapdev_vnodeop_opv_desc =
169 	{ &swapdev_vnodeop_p, swapdev_vnodeop_entries };
170 
171 VNODEOP_SET(swapdev_vnodeop_opv_desc);
172 
173 /*
174  * System call swapon(name) enables swapping on device name,
175  * which must be in the swdevsw.  Return EBUSY
176  * if already swapping on this device.
177  */
178 #ifndef _SYS_SYSPROTO_H_
179 struct swapon_args {
180 	char *name;
181 };
182 #endif
183 
184 /* ARGSUSED */
185 int
186 swapon(p, uap)
187 	struct proc *p;
188 	struct swapon_args *uap;
189 {
190 	struct vattr attr;
191 	register struct vnode *vp;
192 	struct nameidata nd;
193 	int error;
194 
195 	error = suser(p);
196 	if (error)
197 		return (error);
198 
199 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->name, p);
200 	error = namei(&nd);
201 	if (error)
202 		return (error);
203 
204 	NDFREE(&nd, NDF_ONLY_PNBUF);
205 	vp = nd.ni_vp;
206 
207 	if (vn_isdisk(vp, &error))
208 		error = swaponvp(p, vp, vp->v_rdev, 0);
209 	else if (vp->v_type == VREG && vp->v_tag == VT_NFS &&
210 	    (error = VOP_GETATTR(vp, &attr, p->p_ucred, p)) == 0) {
211 		/*
212 		 * Allow direct swapping to NFS regular files in the same
213 		 * way that nfs_mountroot() sets up diskless swapping.
214 		 */
215 		error = swaponvp(p, vp, NODEV, attr.va_size / DEV_BSIZE);
216 	}
217 
218 	if (error)
219 		vrele(vp);
220 
221 	return (error);
222 }
223 
224 /*
225  * Swfree(index) frees the index'th portion of the swap map.
226  * Each of the nswdev devices provides 1/nswdev'th of the swap
227  * space, which is laid out with blocks of dmmax pages circularly
228  * among the devices.
229  *
230  * The new swap code uses page-sized blocks.  The old swap code used
231  * DEV_BSIZE'd chunks.
232  *
233  * XXX locking when multiple swapon's run in parallel
234  */
235 int
236 swaponvp(p, vp, dev, nblks)
237 	struct proc *p;
238 	struct vnode *vp;
239 	dev_t dev;
240 	u_long nblks;
241 {
242 	int index;
243 	register struct swdevt *sp;
244 	register swblk_t vsbase;
245 	register long blk;
246 	swblk_t dvbase;
247 	int error;
248 	u_long aligned_nblks;
249 
250 	if (!swapdev_vp) {
251 		error = getnewvnode(VT_NON, NULL, swapdev_vnodeop_p,
252 		    &swapdev_vp);
253 		if (error)
254 			panic("Cannot get vnode for swapdev");
255 		swapdev_vp->v_type = VNON;	/* Untyped */
256 	}
257 
258 	ASSERT_VOP_UNLOCKED(vp, "swaponvp");
259 	for (sp = swdevt, index = 0 ; index < nswdev; index++, sp++) {
260 		if (sp->sw_vp == vp)
261 			return EBUSY;
262 		if (!sp->sw_vp)
263 			goto found;
264 
265 	}
266 	return EINVAL;
267     found:
268 	(void) vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
269 	error = VOP_OPEN(vp, FREAD | FWRITE, p->p_ucred, p);
270 	(void) VOP_UNLOCK(vp, 0, p);
271 	if (error)
272 		return (error);
273 
274 	if (nblks == 0 && dev != NODEV && (devsw(dev)->d_psize == 0 ||
275 	    (nblks = (*devsw(dev)->d_psize) (dev)) == -1)) {
276 		(void) VOP_CLOSE(vp, FREAD | FWRITE, p->p_ucred, p);
277 		return (ENXIO);
278 	}
279 	if (nblks == 0) {
280 		(void) VOP_CLOSE(vp, FREAD | FWRITE, p->p_ucred, p);
281 		return (ENXIO);
282 	}
283 
284 	/*
285 	 * If we go beyond this, we get overflows in the radix
286 	 * tree bitmap code.
287 	 */
288 	if (nblks > 0x40000000 / BLIST_META_RADIX / nswdev) {
289 		printf("exceeded maximum of %d blocks per swap unit\n",
290 			0x40000000 / BLIST_META_RADIX / nswdev);
291 		(void) VOP_CLOSE(vp, FREAD | FWRITE, p->p_ucred, p);
292 		return (ENXIO);
293 	}
294 	/*
295 	 * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks.
296 	 * First chop nblks off to page-align it, then convert.
297 	 *
298 	 * sw->sw_nblks is in page-sized chunks now too.
299 	 */
300 	nblks &= ~(ctodb(1) - 1);
301 	nblks = dbtoc(nblks);
302 
303 	sp->sw_vp = vp;
304 	sp->sw_dev = dev2udev(dev);
305 	sp->sw_device = dev;
306 	sp->sw_flags |= SW_FREED;
307 	sp->sw_nblks = nblks;
308 
309 	/*
310 	 * nblks, nswap, and dmmax are PAGE_SIZE'd parameters now, not
311 	 * DEV_BSIZE'd.   aligned_nblks is used to calculate the
312 	 * size of the swap bitmap, taking into account the stripe size.
313 	 */
314 	aligned_nblks = (nblks + (dmmax - 1)) & ~(u_long)(dmmax - 1);
315 
316 	if (aligned_nblks * nswdev > nswap)
317 		nswap = aligned_nblks * nswdev;
318 
319 	if (swapblist == NULL)
320 		swapblist = blist_create(nswap);
321 	else
322 		blist_resize(&swapblist, nswap, 0);
323 
324 	for (dvbase = dmmax; dvbase < nblks; dvbase += dmmax) {
325 		blk = min(nblks - dvbase, dmmax);
326 		vsbase = index * dmmax + dvbase * nswdev;
327 		blist_free(swapblist, vsbase, blk);
328 		vm_swap_size += blk;
329 	}
330 
331 	return (0);
332 }
333