HAMMER 60I/Many: Mirroring
[dragonfly.git] / sys / vm / vm_swap.c
blob10c29f265f18b722dd08f9041e9512c544436eee
1 /*
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
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.
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.
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 $
38 #include "opt_swap.h"
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/nlookup.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 <sys/thread2.h>
55 #include <vm/vm.h>
56 #include <vm/vm_extern.h>
57 #include <vm/swap_pager.h>
58 #include <vm/vm_zone.h>
61 * Indirect driver for multi-controller paging.
64 #ifndef NSWAPDEV
65 #define NSWAPDEV 4
66 #endif
67 static struct swdevt should_be_malloced[NSWAPDEV];
68 struct swdevt *swdevt = should_be_malloced; /* exported to pstat/systat */
69 static int nswap; /* first block after the interleaved devs */
70 int nswdev = NSWAPDEV; /* exported to pstat/systat */
71 int vm_swap_size;
73 static int swapdev_strategy (struct vop_strategy_args *ap);
74 struct vnode *swapdev_vp;
77 * swapdev_strategy:
79 * vn_strategy() for swapdev_vp.
80 * Perform swap strategy interleave device selection.
82 * The bp is expected to be locked and on call.
84 * (struct vnode *a_vp, struct bio *b_bio)
87 static int
88 swapdev_strategy(struct vop_strategy_args *ap)
90 struct bio *bio = ap->a_bio;
91 struct bio *nbio;
92 struct buf *bp = bio->bio_buf;
93 int sz, off, seg, index, blkno, nblkno;
94 struct swdevt *sp;
95 struct vnode *vp;
97 vp = ap->a_vp;
98 sz = howmany(bp->b_bcount, PAGE_SIZE);
99 blkno = (int)(bio->bio_offset >> PAGE_SHIFT);
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.
106 nbio = push_bio(bio);
107 if (nswdev > 1) {
108 off = blkno % dmmax;
109 if (off + sz > dmmax) {
110 bp->b_error = EINVAL;
111 bp->b_flags |= B_ERROR;
112 biodone(bio);
113 return 0;
115 seg = blkno / dmmax;
116 index = seg % nswdev;
117 seg /= nswdev;
118 nbio->bio_offset = (off_t)(seg * dmmax + off) << PAGE_SHIFT;
119 } else {
120 index = 0;
121 nbio->bio_offset = bio->bio_offset;
123 nblkno = (int)(nbio->bio_offset >> PAGE_SHIFT);
124 sp = &swdevt[index];
125 if (nblkno + sz > sp->sw_nblks) {
126 bp->b_error = EINVAL;
127 bp->b_flags |= B_ERROR;
128 /* I/O was never started on nbio, must biodone(bio) */
129 biodone(bio);
130 return 0;
132 if (sp->sw_vp == NULL) {
133 bp->b_error = ENODEV;
134 bp->b_flags |= B_ERROR;
135 /* I/O was never started on nbio, must biodone(bio) */
136 biodone(bio);
137 return 0;
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.
145 * We have to use vn_strategy() here even if we know we have a
146 * device in order to properly break up requests which exceed the
147 * device's DMA limits.
149 vn_strategy(sp->sw_vp, nbio);
150 return 0;
154 * Create a special vnode op vector for swapdev_vp - we only use
155 * vn_strategy(), everything else returns an error.
157 static struct vop_ops swapdev_vnode_vops = {
158 .vop_default = vop_defaultop,
159 .vop_strategy = swapdev_strategy
161 static struct vop_ops *swapdev_vnode_vops_p = &swapdev_vnode_vops;
163 VNODEOP_SET(swapdev_vnode_vops);
166 * swapon_args(char *name)
168 * System call swapon(name) enables swapping on device name,
169 * which must be in the swdevsw. Return EBUSY
170 * if already swapping on this device.
172 /* ARGSUSED */
174 sys_swapon(struct swapon_args *uap)
176 struct thread *td = curthread;
177 struct vattr attr;
178 struct vnode *vp;
179 struct nlookupdata nd;
180 int error;
181 struct ucred *cred;
183 KKASSERT(td->td_proc);
184 cred = td->td_proc->p_ucred;
186 error = suser(td);
187 if (error)
188 return (error);
190 vp = NULL;
191 error = nlookup_init(&nd, uap->name, UIO_USERSPACE, NLC_FOLLOW);
192 if (error == 0)
193 error = nlookup(&nd);
194 if (error == 0)
195 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
196 nlookup_done(&nd);
197 if (error)
198 return (error);
200 if (vn_isdisk(vp, &error))
201 error = swaponvp(td, vp, 0);
202 else if (vp->v_type == VREG && vp->v_tag == VT_NFS &&
203 (error = VOP_GETATTR(vp, &attr)) == 0) {
205 * Allow direct swapping to NFS regular files in the same
206 * way that nfs_mountroot() sets up diskless swapping.
208 error = swaponvp(td, vp, attr.va_size / DEV_BSIZE);
211 if (error)
212 vrele(vp);
214 return (error);
218 * Swfree(index) frees the index'th portion of the swap map.
219 * Each of the nswdev devices provides 1/nswdev'th of the swap
220 * space, which is laid out with blocks of dmmax pages circularly
221 * among the devices.
223 * The new swap code uses page-sized blocks. The old swap code used
224 * DEV_BSIZE'd chunks.
226 * XXX locking when multiple swapon's run in parallel
229 swaponvp(struct thread *td, struct vnode *vp, u_long nblks)
231 u_long aligned_nblks;
232 int64_t dpsize;
233 struct ucred *cred;
234 struct swdevt *sp;
235 swblk_t vsbase;
236 swblk_t dvbase;
237 cdev_t dev;
238 int index;
239 int error;
240 long blk;
242 KKASSERT(td->td_proc);
243 cred = td->td_proc->p_ucred;
245 if (!swapdev_vp) {
246 error = getspecialvnode(VT_NON, NULL, &swapdev_vnode_vops_p,
247 &swapdev_vp, 0, 0);
248 if (error)
249 panic("Cannot get vnode for swapdev");
250 swapdev_vp->v_type = VNON; /* Untyped */
251 vx_unlock(swapdev_vp);
254 for (sp = swdevt, index = 0 ; index < nswdev; index++, sp++) {
255 if (sp->sw_vp == vp)
256 return EBUSY;
257 if (!sp->sw_vp)
258 goto found;
261 return EINVAL;
262 found:
263 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
264 error = VOP_OPEN(vp, FREAD | FWRITE, cred, NULL);
265 vn_unlock(vp);
266 if (error)
267 return (error);
270 * v_rdev is not valid until after the VOP_OPEN() call. dev_psize()
271 * must be supported if a character device has been specified.
273 if (vp->v_type == VCHR)
274 dev = vp->v_rdev;
275 else
276 dev = NULL;
278 if (nblks == 0 && dev != NULL) {
279 dpsize = dev_dpsize(dev);
280 if (dpsize == -1) {
281 VOP_CLOSE(vp, FREAD | FWRITE);
282 return (ENXIO);
284 if ((u_int64_t)dpsize < 0x100000000ULL)
285 nblks = (u_long)dpsize;
286 else
287 nblks = 0xffffffffU;
289 if (nblks == 0) {
290 VOP_CLOSE(vp, FREAD | FWRITE);
291 return (ENXIO);
295 * If we go beyond this, we get overflows in the radix
296 * tree bitmap code.
298 if (nblks > 0x40000000 / BLIST_META_RADIX / nswdev) {
299 kprintf("exceeded maximum of %d blocks per swap unit\n",
300 0x40000000 / BLIST_META_RADIX / nswdev);
301 VOP_CLOSE(vp, FREAD | FWRITE);
302 return (ENXIO);
305 * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks.
306 * First chop nblks off to page-align it, then convert.
308 * sw->sw_nblks is in page-sized chunks now too.
310 nblks &= ~(ctodb(1) - 1);
311 nblks = dbtoc(nblks);
313 sp->sw_vp = vp;
314 sp->sw_dev = dev2udev(dev);
315 sp->sw_device = dev;
316 sp->sw_flags |= SW_FREED;
317 sp->sw_nblks = nblks;
320 * nblks, nswap, and dmmax are PAGE_SIZE'd parameters now, not
321 * DEV_BSIZE'd. aligned_nblks is used to calculate the
322 * size of the swap bitmap, taking into account the stripe size.
324 aligned_nblks = (nblks + (dmmax - 1)) & ~(u_long)(dmmax - 1);
326 if (aligned_nblks * nswdev > nswap)
327 nswap = aligned_nblks * nswdev;
329 if (swapblist == NULL)
330 swapblist = blist_create(nswap);
331 else
332 blist_resize(&swapblist, nswap, 0);
334 for (dvbase = dmmax; dvbase < nblks; dvbase += dmmax) {
335 blk = min(nblks - dvbase, dmmax);
336 vsbase = index * dmmax + dvbase * nswdev;
337 blist_free(swapblist, vsbase, blk);
338 vm_swap_size += blk;
341 return (0);