xref: /dragonfly/sys/vm/vm_swap.c (revision 768af85b)
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.36 2007/07/20 17:21:54 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/priv.h>
46 #include <sys/nlookup.h>
47 #include <sys/dmap.h>		/* XXX */
48 #include <sys/vnode.h>
49 #include <sys/fcntl.h>
50 #include <sys/blist.h>
51 #include <sys/kernel.h>
52 #include <sys/lock.h>
53 #include <sys/conf.h>
54 #include <sys/stat.h>
55 
56 #include <vm/vm.h>
57 #include <vm/vm_extern.h>
58 #include <vm/swap_pager.h>
59 #include <vm/vm_zone.h>
60 
61 #include <sys/thread2.h>
62 #include <sys/mplock2.h>
63 
64 /*
65  * Indirect driver for multi-controller paging.
66  */
67 
68 #ifndef NSWAPDEV
69 #define NSWAPDEV	4
70 #endif
71 static struct swdevt should_be_malloced[NSWAPDEV];
72 struct swdevt *swdevt = should_be_malloced;	/* exported to pstat/systat */
73 static swblk_t nswap;		/* first block after the interleaved devs */
74 int nswdev = NSWAPDEV;				/* exported to pstat/systat */
75 int vm_swap_size;
76 int vm_swap_max;
77 
78 static int swapdev_strategy (struct vop_strategy_args *ap);
79 struct vnode *swapdev_vp;
80 
81 /*
82  *	swapdev_strategy:
83  *
84  *	vn_strategy() for swapdev_vp.
85  *	Perform swap strategy interleave device selection.
86  *
87  *	The bp is expected to be locked and on call.
88  *
89  *	(struct vnode *a_vp, struct bio *b_bio)
90  */
91 
92 static int
93 swapdev_strategy(struct vop_strategy_args *ap)
94 {
95 	struct bio *bio = ap->a_bio;
96 	struct bio *nbio;
97 	struct buf *bp = bio->bio_buf;
98 	int sz, off, seg, index, blkno, nblkno;
99 	struct swdevt *sp;
100 	struct vnode *vp;
101 
102 	vp = ap->a_vp;
103 	sz = howmany(bp->b_bcount, PAGE_SIZE);
104 	blkno = (int)(bio->bio_offset >> PAGE_SHIFT);
105 
106 	/*
107 	 * Convert interleaved swap into per-device swap.  Note that
108 	 * the block size is left in PAGE_SIZE'd chunks (for the newswap)
109 	 * here.
110 	 */
111 	nbio = push_bio(bio);
112 	if (nswdev > 1) {
113 		off = blkno % dmmax;
114 		if (off + sz > dmmax) {
115 			bp->b_error = EINVAL;
116 			bp->b_flags |= B_ERROR;
117 			biodone(bio);
118 			return 0;
119 		}
120 		seg = blkno / dmmax;
121 		index = seg % nswdev;
122 		seg /= nswdev;
123 		nbio->bio_offset = (off_t)(seg * dmmax + off) << PAGE_SHIFT;
124 	} else {
125 		index = 0;
126 		nbio->bio_offset = bio->bio_offset;
127 	}
128 	nblkno = (int)(nbio->bio_offset >> PAGE_SHIFT);
129 	sp = &swdevt[index];
130 	if (nblkno + sz > sp->sw_nblks) {
131 		bp->b_error = EINVAL;
132 		bp->b_flags |= B_ERROR;
133 		/* I/O was never started on nbio, must biodone(bio) */
134 		biodone(bio);
135 		return 0;
136 	}
137 	if (sp->sw_vp == NULL) {
138 		bp->b_error = ENODEV;
139 		bp->b_flags |= B_ERROR;
140 		/* I/O was never started on nbio, must biodone(bio) */
141 		biodone(bio);
142 		return 0;
143 	}
144 
145 	/*
146 	 * Issue a strategy call on the appropriate swap vnode.  Note that
147 	 * bp->b_vp is not modified.  Strategy code is always supposed to
148 	 * use the passed vp.
149 	 *
150 	 * We have to use vn_strategy() here even if we know we have a
151 	 * device in order to properly break up requests which exceed the
152 	 * device's DMA limits.
153 	 */
154 	vn_strategy(sp->sw_vp, nbio);
155 	return 0;
156 }
157 
158 /*
159  * Create a special vnode op vector for swapdev_vp - we only use
160  * vn_strategy(), everything else returns an error.
161  */
162 static struct vop_ops swapdev_vnode_vops = {
163 	.vop_default =		vop_defaultop,
164 	.vop_strategy =		swapdev_strategy
165 };
166 static struct vop_ops *swapdev_vnode_vops_p = &swapdev_vnode_vops;
167 
168 VNODEOP_SET(swapdev_vnode_vops);
169 
170 /*
171  * swapon_args(char *name)
172  *
173  * System call swapon(name) enables swapping on device name,
174  * which must be in the swdevsw.  Return EBUSY
175  * if already swapping on this device.
176  *
177  * MPALMOSTSAFE
178  */
179 int
180 sys_swapon(struct swapon_args *uap)
181 {
182 	struct thread *td = curthread;
183 	struct vattr attr;
184 	struct vnode *vp;
185 	struct nlookupdata nd;
186 	int error;
187 	struct ucred *cred;
188 
189 	cred = td->td_ucred;
190 
191 	error = priv_check(td, PRIV_ROOT);
192 	if (error)
193 		return (error);
194 
195 	get_mplock();
196 	vp = NULL;
197 	error = nlookup_init(&nd, uap->name, UIO_USERSPACE, NLC_FOLLOW);
198 	if (error == 0)
199 		error = nlookup(&nd);
200 	if (error == 0)
201 		error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
202 	nlookup_done(&nd);
203 	if (error) {
204 		rel_mplock();
205 		return (error);
206 	}
207 
208 	if (vn_isdisk(vp, &error)) {
209 		error = swaponvp(td, vp, 0);
210 	} else if (vp->v_type == VREG && vp->v_tag == VT_NFS &&
211 		   (error = VOP_GETATTR(vp, &attr)) == 0) {
212 		/*
213 		 * Allow direct swapping to NFS regular files in the same
214 		 * way that nfs_mountroot() sets up diskless swapping.
215 		 */
216 		error = swaponvp(td, vp, attr.va_size / DEV_BSIZE);
217 	}
218 	if (error)
219 		vrele(vp);
220 	rel_mplock();
221 
222 	return (error);
223 }
224 
225 /*
226  * Swfree(index) frees the index'th portion of the swap map.
227  * Each of the nswdev devices provides 1/nswdev'th of the swap
228  * space, which is laid out with blocks of dmmax pages circularly
229  * among the devices.
230  *
231  * The new swap code uses page-sized blocks.  The old swap code used
232  * DEV_BSIZE'd chunks.
233  *
234  * XXX locking when multiple swapon's run in parallel
235  */
236 int
237 swaponvp(struct thread *td, struct vnode *vp, u_quad_t nblks)
238 {
239 	swblk_t aligned_nblks;
240 	int64_t dpsize;
241 	struct ucred *cred;
242 	struct swdevt *sp;
243 	swblk_t vsbase;
244 	swblk_t dvbase;
245 	cdev_t dev;
246 	int index;
247 	int error;
248 	long blk;
249 
250 	cred = td->td_ucred;
251 
252 	if (!swapdev_vp) {
253 		error = getspecialvnode(VT_NON, NULL, &swapdev_vnode_vops_p,
254 				    &swapdev_vp, 0, 0);
255 		if (error)
256 			panic("Cannot get vnode for swapdev");
257 		swapdev_vp->v_type = VNON;	/* Untyped */
258 		vx_unlock(swapdev_vp);
259 	}
260 
261 	for (sp = swdevt, index = 0 ; index < nswdev; index++, sp++) {
262 		if (sp->sw_vp == vp)
263 			return EBUSY;
264 		if (!sp->sw_vp)
265 			goto found;
266 
267 	}
268 	return EINVAL;
269     found:
270 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
271 	error = VOP_OPEN(vp, FREAD | FWRITE, cred, NULL);
272 	vn_unlock(vp);
273 	if (error)
274 		return (error);
275 
276 	/*
277 	 * v_rdev is not valid until after the VOP_OPEN() call.  dev_psize()
278 	 * must be supported if a character device has been specified.
279 	 */
280 	if (vp->v_type == VCHR)
281 		dev = vp->v_rdev;
282 	else
283 		dev = NULL;
284 
285 	if (nblks == 0 && dev != NULL) {
286 		dpsize = dev_dpsize(dev);
287 		if (dpsize == -1) {
288 			VOP_CLOSE(vp, FREAD | FWRITE);
289 			return (ENXIO);
290 		}
291 		nblks = (u_quad_t)dpsize;
292 	}
293 	if (nblks == 0) {
294 		VOP_CLOSE(vp, FREAD | FWRITE);
295 		return (ENXIO);
296 	}
297 
298 	/*
299 	 * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks.
300 	 * First chop nblks off to page-align it, then convert.
301 	 *
302 	 * sw->sw_nblks is in page-sized chunks now too.
303 	 */
304 	nblks &= ~(u_quad_t)(ctodb(1) - 1);
305 	nblks = dbtoc(nblks);
306 
307 	/*
308 	 * Post-conversion nblks must not be >= BLIST_MAXBLKS, and
309 	 * we impose a 4-swap-device limit so we have to divide it out
310 	 * further.  Going beyond this will result in overflows in the
311 	 * blist code.
312 	 *
313 	 * Post-conversion nblks must fit within a (swblk_t), which
314 	 * this test also ensures.
315 	 */
316 	if (nblks > BLIST_MAXBLKS / nswdev) {
317 		kprintf("exceeded maximum of %d blocks per swap unit\n",
318 			(int)BLIST_MAXBLKS / nswdev);
319 		VOP_CLOSE(vp, FREAD | FWRITE);
320 		return (ENXIO);
321 	}
322 
323 	sp->sw_vp = vp;
324 	sp->sw_dev = dev2udev(dev);
325 	sp->sw_device = dev;
326 	sp->sw_flags |= SW_FREED;
327 	sp->sw_nblks = (swblk_t)nblks;
328 
329 	/*
330 	 * nblks, nswap, and dmmax are PAGE_SIZE'd parameters now, not
331 	 * DEV_BSIZE'd.   aligned_nblks is used to calculate the
332 	 * size of the swap bitmap, taking into account the stripe size.
333 	 */
334 	aligned_nblks = (swblk_t)((nblks + (dmmax - 1)) & ~(u_long)(dmmax - 1));
335 
336 	if (aligned_nblks * nswdev > nswap)
337 		nswap = aligned_nblks * nswdev;
338 
339 	if (swapblist == NULL)
340 		swapblist = blist_create(nswap);
341 	else
342 		blist_resize(&swapblist, nswap, 0);
343 
344 	for (dvbase = dmmax; dvbase < nblks; dvbase += dmmax) {
345 		blk = min(nblks - dvbase, dmmax);
346 		vsbase = index * dmmax + dvbase * nswdev;
347 		blist_free(swapblist, vsbase, blk);
348 		vm_swap_size += blk;
349 		vm_swap_max += blk;
350 	}
351 	swap_pager_newswap();
352 
353 	return (0);
354 }
355