Change the kernel dev_t, representing a pointer to a specinfo structure,
[dragonfly.git] / sys / vm / vm_swap.c
CommitLineData
984263bc
MD
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 $
b13267a5 35 * $DragonFly: src/sys/vm/vm_swap.c,v 1.30 2006/09/10 01:26:41 dillon Exp $
984263bc
MD
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>
fad57d0e 45#include <sys/nlookup.h>
984263bc
MD
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>
cdd46d2e 54#include <sys/thread2.h>
984263bc
MD
55#include <vm/vm.h>
56#include <vm/vm_extern.h>
57#include <vm/swap_pager.h>
58#include <vm/vm_zone.h>
59
60/*
61 * Indirect driver for multi-controller paging.
62 */
63
64#ifndef NSWAPDEV
65#define NSWAPDEV 4
66#endif
67static struct swdevt should_be_malloced[NSWAPDEV];
68static struct swdevt *swdevt = should_be_malloced;
69static int nswap; /* first block after the interleaved devs */
70static int nswdev = NSWAPDEV;
71int vm_swap_size;
72
1388df65 73static int swapdev_strategy (struct vop_strategy_args *ap);
984263bc
MD
74struct vnode *swapdev_vp;
75
76/*
77 * swapdev_strategy:
78 *
81b5c339 79 * vn_strategy() for swapdev_vp.
984263bc
MD
80 * Perform swap strategy interleave device selection.
81 *
10f3fee5 82 * The bp is expected to be locked and on call.
81b5c339
MD
83 *
84 * (struct vnode *a_vp, struct bio *b_bio)
984263bc
MD
85 */
86
87static int
81b5c339 88swapdev_strategy(struct vop_strategy_args *ap)
984263bc 89{
81b5c339
MD
90 struct bio *bio = ap->a_bio;
91 struct bio *nbio;
92 struct buf *bp = bio->bio_buf;
54078292 93 int sz, off, seg, index, blkno, nblkno;
5f910b2f 94 struct swdevt *sp;
984263bc 95 struct vnode *vp;
984263bc 96
81b5c339 97 vp = ap->a_vp;
984263bc 98 sz = howmany(bp->b_bcount, PAGE_SIZE);
54078292 99 blkno = (int)(bio->bio_offset >> PAGE_SHIFT);
984263bc
MD
100
101 /*
102 * Convert interleaved swap into per-device swap. Note that
103 * the block size is left in PAGE_SIZE'd chunks (for the newswap)
104 * here.
105 */
81b5c339 106 nbio = push_bio(bio);
984263bc 107 if (nswdev > 1) {
54078292 108 off = blkno % dmmax;
984263bc
MD
109 if (off + sz > dmmax) {
110 bp->b_error = EINVAL;
111 bp->b_flags |= B_ERROR;
81b5c339 112 biodone(bio);
984263bc
MD
113 return 0;
114 }
54078292 115 seg = blkno / dmmax;
984263bc
MD
116 index = seg % nswdev;
117 seg /= nswdev;
54078292 118 nbio->bio_offset = (off_t)(seg * dmmax + off) << PAGE_SHIFT;
984263bc
MD
119 } else {
120 index = 0;
54078292 121 nbio->bio_offset = bio->bio_offset;
984263bc 122 }
54078292 123 nblkno = (int)(nbio->bio_offset >> PAGE_SHIFT);
984263bc 124 sp = &swdevt[index];
54078292 125 if (nblkno + sz > sp->sw_nblks) {
984263bc
MD
126 bp->b_error = EINVAL;
127 bp->b_flags |= B_ERROR;
81b5c339
MD
128 /* I/O was never started on nbio, must biodone(bio) */
129 biodone(bio);
984263bc
MD
130 return 0;
131 }
984263bc
MD
132 if (sp->sw_vp == NULL) {
133 bp->b_error = ENODEV;
134 bp->b_flags |= B_ERROR;
81b5c339
MD
135 /* I/O was never started on nbio, must biodone(bio) */
136 biodone(bio);
984263bc
MD
137 return 0;
138 }
139
140 /*
81b5c339
MD
141 * Issue a strategy call on the appropriate swap vnode. Note that
142 * bp->b_vp is not modified. Strategy code is always supposed to
143 * use the passed vp.
144 *
145 * XXX do a dev_dstrategy() call on sp->sw_device instead of on
146 * sp->sw_vp ?
984263bc 147 */
81b5c339 148 vn_strategy(sp->sw_vp, nbio);
984263bc
MD
149 return 0;
150}
151
152/*
153 * Create a special vnode op vector for swapdev_vp - we only use
81b5c339 154 * vn_strategy(), everything else returns an error.
984263bc 155 */
66a1ddf5
MD
156static struct vop_ops swapdev_vnode_vops = {
157 .vop_default = vop_defaultop,
158 .vop_strategy = swapdev_strategy
984263bc 159};
66a1ddf5 160static struct vop_ops *swapdev_vnode_vops_p = &swapdev_vnode_vops;
984263bc 161
66a1ddf5 162VNODEOP_SET(swapdev_vnode_vops);
984263bc
MD
163
164/*
41c20dac
MD
165 * swapon_args(char *name)
166 *
984263bc
MD
167 * System call swapon(name) enables swapping on device name,
168 * which must be in the swdevsw. Return EBUSY
169 * if already swapping on this device.
170 */
984263bc
MD
171/* ARGSUSED */
172int
753fd850 173sys_swapon(struct swapon_args *uap)
984263bc 174{
dadab5e9 175 struct thread *td = curthread;
984263bc 176 struct vattr attr;
5f910b2f 177 struct vnode *vp;
fad57d0e 178 struct nlookupdata nd;
984263bc 179 int error;
dadab5e9 180 struct ucred *cred;
984263bc 181
dadab5e9
MD
182 KKASSERT(td->td_proc);
183 cred = td->td_proc->p_ucred;
184
185 error = suser(td);
984263bc
MD
186 if (error)
187 return (error);
188
fad57d0e
MD
189 vp = NULL;
190 error = nlookup_init(&nd, uap->name, UIO_USERSPACE, NLC_FOLLOW);
191 if (error == 0)
192 error = nlookup(&nd);
193 if (error == 0)
194 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
195 nlookup_done(&nd);
984263bc
MD
196 if (error)
197 return (error);
198
984263bc 199 if (vn_isdisk(vp, &error))
e4c9c0c8 200 error = swaponvp(td, vp, 0);
984263bc 201 else if (vp->v_type == VREG && vp->v_tag == VT_NFS &&
87de5057 202 (error = VOP_GETATTR(vp, &attr)) == 0) {
984263bc
MD
203 /*
204 * Allow direct swapping to NFS regular files in the same
205 * way that nfs_mountroot() sets up diskless swapping.
206 */
e4c9c0c8 207 error = swaponvp(td, vp, attr.va_size / DEV_BSIZE);
984263bc
MD
208 }
209
210 if (error)
211 vrele(vp);
212
213 return (error);
214}
215
216/*
217 * Swfree(index) frees the index'th portion of the swap map.
218 * Each of the nswdev devices provides 1/nswdev'th of the swap
219 * space, which is laid out with blocks of dmmax pages circularly
220 * among the devices.
221 *
222 * The new swap code uses page-sized blocks. The old swap code used
223 * DEV_BSIZE'd chunks.
224 *
225 * XXX locking when multiple swapon's run in parallel
226 */
227int
e4c9c0c8 228swaponvp(struct thread *td, struct vnode *vp, u_long nblks)
984263bc 229{
e4c9c0c8
MD
230 u_long aligned_nblks;
231 struct ucred *cred;
5f910b2f
RG
232 struct swdevt *sp;
233 swblk_t vsbase;
984263bc 234 swblk_t dvbase;
b13267a5 235 cdev_t dev;
e4c9c0c8 236 int index;
984263bc 237 int error;
e4c9c0c8 238 long blk;
dadab5e9
MD
239
240 KKASSERT(td->td_proc);
241 cred = td->td_proc->p_ucred;
984263bc
MD
242
243 if (!swapdev_vp) {
66a1ddf5 244 error = getspecialvnode(VT_NON, NULL, &swapdev_vnode_vops_p,
3446c007 245 &swapdev_vp, 0, 0);
984263bc
MD
246 if (error)
247 panic("Cannot get vnode for swapdev");
248 swapdev_vp->v_type = VNON; /* Untyped */
5fd012e0 249 vx_unlock(swapdev_vp);
984263bc
MD
250 }
251
984263bc
MD
252 for (sp = swdevt, index = 0 ; index < nswdev; index++, sp++) {
253 if (sp->sw_vp == vp)
254 return EBUSY;
255 if (!sp->sw_vp)
256 goto found;
257
258 }
259 return EINVAL;
260 found:
ca466bae 261 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
87de5057 262 error = VOP_OPEN(vp, FREAD | FWRITE, cred, NULL);
a11aaa81 263 vn_unlock(vp);
984263bc
MD
264 if (error)
265 return (error);
266
e4c9c0c8
MD
267 /*
268 * v_rdev is not valid until after the VOP_OPEN() call. dev_psize()
269 * must be supported if a character device has been specified.
270 */
271 if (vp->v_type == VCHR)
272 dev = vp->v_rdev;
273 else
5711af4f 274 dev = NOCDEV;
e4c9c0c8 275
5711af4f 276 if (nblks == 0 && dev != NOCDEV && ((nblks = dev_dpsize(dev)) == -1)) {
87de5057 277 VOP_CLOSE(vp, FREAD | FWRITE);
984263bc
MD
278 return (ENXIO);
279 }
280 if (nblks == 0) {
87de5057 281 VOP_CLOSE(vp, FREAD | FWRITE);
984263bc
MD
282 return (ENXIO);
283 }
284
285 /*
286 * If we go beyond this, we get overflows in the radix
287 * tree bitmap code.
288 */
289 if (nblks > 0x40000000 / BLIST_META_RADIX / nswdev) {
290 printf("exceeded maximum of %d blocks per swap unit\n",
291 0x40000000 / BLIST_META_RADIX / nswdev);
87de5057 292 VOP_CLOSE(vp, FREAD | FWRITE);
984263bc
MD
293 return (ENXIO);
294 }
295 /*
296 * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks.
297 * First chop nblks off to page-align it, then convert.
298 *
299 * sw->sw_nblks is in page-sized chunks now too.
300 */
301 nblks &= ~(ctodb(1) - 1);
302 nblks = dbtoc(nblks);
303
304 sp->sw_vp = vp;
305 sp->sw_dev = dev2udev(dev);
306 sp->sw_device = dev;
307 sp->sw_flags |= SW_FREED;
308 sp->sw_nblks = nblks;
309
310 /*
311 * nblks, nswap, and dmmax are PAGE_SIZE'd parameters now, not
312 * DEV_BSIZE'd. aligned_nblks is used to calculate the
313 * size of the swap bitmap, taking into account the stripe size.
314 */
315 aligned_nblks = (nblks + (dmmax - 1)) & ~(u_long)(dmmax - 1);
316
317 if (aligned_nblks * nswdev > nswap)
318 nswap = aligned_nblks * nswdev;
319
320 if (swapblist == NULL)
321 swapblist = blist_create(nswap);
322 else
323 blist_resize(&swapblist, nswap, 0);
324
325 for (dvbase = dmmax; dvbase < nblks; dvbase += dmmax) {
326 blk = min(nblks - dvbase, dmmax);
327 vsbase = index * dmmax + dvbase * nswdev;
328 blist_free(swapblist, vsbase, blk);
329 vm_swap_size += blk;
330 }
331
332 return (0);
333}