Remove advertising clause from all that isn't contrib or userland bin.
[dragonfly.git] / sys / vm / vm_swap.c
blobf390629c5219de905c2fba8b4df2cf9d9219b67e
1 /*
2 * (MPSAFE)
4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 4. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
31 * @(#)vm_swap.c 8.5 (Berkeley) 2/17/94
32 * $FreeBSD: src/sys/vm/vm_swap.c,v 1.96.2.2 2001/10/14 18:46:47 iedowse Exp $
35 #include "opt_swap.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/sysproto.h>
40 #include <sys/buf.h>
41 #include <sys/proc.h>
42 #include <sys/priv.h>
43 #include <sys/nlookup.h>
44 #include <sys/sysctl.h>
45 #include <sys/dmap.h> /* XXX */
46 #include <sys/vnode.h>
47 #include <sys/fcntl.h>
48 #include <sys/blist.h>
49 #include <sys/kernel.h>
50 #include <sys/lock.h>
51 #include <sys/conf.h>
52 #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 #include <vm/vm_param.h>
60 #include <sys/thread2.h>
61 #include <sys/mplock2.h>
62 #include <sys/mutex2.h>
63 #include <sys/spinlock2.h>
66 * Indirect driver for multi-controller paging.
69 #ifndef NSWAPDEV
70 #define NSWAPDEV 4
71 #endif
72 static struct swdevt should_be_malloced[NSWAPDEV];
73 struct swdevt *swdevt = should_be_malloced; /* exported to pstat/systat */
74 static swblk_t nswap; /* first block after the interleaved devs */
75 static struct mtx swap_mtx = MTX_INITIALIZER;
76 int nswdev = NSWAPDEV; /* exported to pstat/systat */
77 int vm_swap_size;
78 int vm_swap_max;
80 static int swapoff_one(int index);
81 struct vnode *swapdev_vp;
84 * (struct vnode *a_vp, struct bio *b_bio)
86 * vn_strategy() for swapdev_vp. Perform swap strategy interleave device
87 * selection.
89 * No requirements.
91 static int
92 swapdev_strategy(struct vop_strategy_args *ap)
94 struct bio *bio = ap->a_bio;
95 struct bio *nbio;
96 struct buf *bp = bio->bio_buf;
97 int sz, off, seg, index, blkno, nblkno;
98 struct swdevt *sp;
99 sz = howmany(bp->b_bcount, PAGE_SIZE);
100 blkno = (int)(bio->bio_offset >> PAGE_SHIFT);
103 * Convert interleaved swap into per-device swap. Note that
104 * the block size is left in PAGE_SIZE'd chunks (for the newswap)
105 * here.
107 nbio = push_bio(bio);
108 if (nswdev > 1) {
109 off = blkno % dmmax;
110 if (off + sz > dmmax) {
111 bp->b_error = EINVAL;
112 bp->b_flags |= B_ERROR;
113 biodone(bio);
114 return 0;
116 seg = blkno / dmmax;
117 index = seg % nswdev;
118 seg /= nswdev;
119 nbio->bio_offset = (off_t)(seg * dmmax + off) << PAGE_SHIFT;
120 } else {
121 index = 0;
122 nbio->bio_offset = bio->bio_offset;
124 nblkno = (int)(nbio->bio_offset >> PAGE_SHIFT);
125 sp = &swdevt[index];
126 if (nblkno + sz > sp->sw_nblks) {
127 bp->b_error = EINVAL;
128 bp->b_flags |= B_ERROR;
129 /* I/O was never started on nbio, must biodone(bio) */
130 biodone(bio);
131 return 0;
133 if (sp->sw_vp == NULL) {
134 bp->b_error = ENODEV;
135 bp->b_flags |= B_ERROR;
136 /* I/O was never started on nbio, must biodone(bio) */
137 biodone(bio);
138 return 0;
142 * Issue a strategy call on the appropriate swap vnode. Note that
143 * bp->b_vp is not modified. Strategy code is always supposed to
144 * use the passed vp.
146 * We have to use vn_strategy() here even if we know we have a
147 * device in order to properly break up requests which exceed the
148 * device's DMA limits.
150 vn_strategy(sp->sw_vp, nbio);
151 return 0;
154 static int
155 swapdev_inactive(struct vop_inactive_args *ap)
157 vrecycle(ap->a_vp);
158 return(0);
161 static int
162 swapdev_reclaim(struct vop_reclaim_args *ap)
164 return(0);
168 * Create a special vnode op vector for swapdev_vp - we only use
169 * vn_strategy(), everything else returns an error.
171 static struct vop_ops swapdev_vnode_vops = {
172 .vop_default = vop_defaultop,
173 .vop_strategy = swapdev_strategy,
174 .vop_inactive = swapdev_inactive,
175 .vop_reclaim = swapdev_reclaim
177 static struct vop_ops *swapdev_vnode_vops_p = &swapdev_vnode_vops;
179 VNODEOP_SET(swapdev_vnode_vops);
182 * swapon_args(char *name)
184 * System call swapon(name) enables swapping on device name,
185 * which must be in the swdevsw. Return EBUSY
186 * if already swapping on this device.
188 * No requirements.
191 sys_swapon(struct swapon_args *uap)
193 struct thread *td = curthread;
194 struct vattr attr;
195 struct vnode *vp;
196 struct nlookupdata nd;
197 int error;
199 error = priv_check(td, PRIV_ROOT);
200 if (error)
201 return (error);
203 mtx_lock(&swap_mtx);
204 get_mplock();
205 vp = NULL;
206 error = nlookup_init(&nd, uap->name, UIO_USERSPACE, NLC_FOLLOW);
207 if (error == 0)
208 error = nlookup(&nd);
209 if (error == 0)
210 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
211 nlookup_done(&nd);
212 if (error) {
213 rel_mplock();
214 mtx_unlock(&swap_mtx);
215 return (error);
218 if (vn_isdisk(vp, &error)) {
219 error = swaponvp(td, vp, 0);
220 } else if (vp->v_type == VREG && vp->v_tag == VT_NFS &&
221 (error = VOP_GETATTR(vp, &attr)) == 0) {
223 * Allow direct swapping to NFS regular files in the same
224 * way that nfs_mountroot() sets up diskless swapping.
226 error = swaponvp(td, vp, attr.va_size / DEV_BSIZE);
228 if (error)
229 vrele(vp);
230 rel_mplock();
231 mtx_unlock(&swap_mtx);
233 return (error);
237 * Swfree(index) frees the index'th portion of the swap map.
238 * Each of the nswdev devices provides 1/nswdev'th of the swap
239 * space, which is laid out with blocks of dmmax pages circularly
240 * among the devices.
242 * The new swap code uses page-sized blocks. The old swap code used
243 * DEV_BSIZE'd chunks.
245 * XXX locking when multiple swapon's run in parallel
248 swaponvp(struct thread *td, struct vnode *vp, u_quad_t nblks)
250 swblk_t aligned_nblks;
251 int64_t dpsize;
252 struct ucred *cred;
253 struct swdevt *sp;
254 swblk_t vsbase;
255 swblk_t dvbase;
256 cdev_t dev;
257 int index;
258 int error;
259 swblk_t blk;
261 cred = td->td_ucred;
263 lwkt_gettoken(&vm_token); /* needed for vm_swap_size and blist */
264 mtx_lock(&swap_mtx);
266 if (!swapdev_vp) {
267 error = getspecialvnode(VT_NON, NULL, &swapdev_vnode_vops_p,
268 &swapdev_vp, 0, 0);
269 if (error)
270 panic("Cannot get vnode for swapdev");
271 swapdev_vp->v_type = VNON; /* Untyped */
272 vx_unlock(swapdev_vp);
275 for (sp = swdevt, index = 0 ; index < nswdev; index++, sp++) {
276 if (sp->sw_vp == vp) {
277 error = EBUSY;
278 goto done;
280 if (!sp->sw_vp)
281 goto found;
284 error = EINVAL;
285 goto done;
286 found:
287 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
288 error = VOP_OPEN(vp, FREAD | FWRITE, cred, NULL);
289 vn_unlock(vp);
290 if (error)
291 goto done;
294 * v_rdev is not valid until after the VOP_OPEN() call. dev_psize()
295 * must be supported if a character device has been specified.
297 if (vp->v_type == VCHR)
298 dev = vp->v_rdev;
299 else
300 dev = NULL;
302 if (nblks == 0 && dev != NULL) {
303 dpsize = dev_dpsize(dev);
304 if (dpsize == -1) {
305 VOP_CLOSE(vp, FREAD | FWRITE);
306 error = ENXIO;
307 goto done;
309 nblks = (u_quad_t)dpsize;
311 if (nblks == 0) {
312 VOP_CLOSE(vp, FREAD | FWRITE);
313 error = ENXIO;
314 goto done;
318 * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks.
319 * First chop nblks off to page-align it, then convert.
321 * sw->sw_nblks is in page-sized chunks now too.
323 nblks &= ~(u_quad_t)(ctodb(1) - 1);
324 nblks = dbtoc(nblks);
327 * Post-conversion nblks must not be >= BLIST_MAXBLKS, and
328 * we impose a 4-swap-device limit so we have to divide it out
329 * further. Going beyond this will result in overflows in the
330 * blist code.
332 * Post-conversion nblks must fit within a (swblk_t), which
333 * this test also ensures.
335 if (nblks > BLIST_MAXBLKS / nswdev) {
336 kprintf("exceeded maximum of %d blocks per swap unit\n",
337 (int)BLIST_MAXBLKS / nswdev);
338 VOP_CLOSE(vp, FREAD | FWRITE);
339 error = ENXIO;
340 goto done;
343 sp->sw_vp = vp;
344 sp->sw_dev = dev2udev(dev);
345 sp->sw_device = dev;
346 sp->sw_flags = SW_FREED;
347 sp->sw_nused = 0;
350 * nblks, nswap, and dmmax are PAGE_SIZE'd parameters now, not
351 * DEV_BSIZE'd. aligned_nblks is used to calculate the
352 * size of the swap bitmap, taking into account the stripe size.
354 aligned_nblks = (swblk_t)((nblks + (dmmax - 1)) & ~(u_long)(dmmax - 1));
355 sp->sw_nblks = aligned_nblks;
357 if (aligned_nblks * nswdev > nswap)
358 nswap = aligned_nblks * nswdev;
360 if (swapblist == NULL)
361 swapblist = blist_create(nswap);
362 else
363 blist_resize(&swapblist, nswap, 0);
365 for (dvbase = dmmax; dvbase < aligned_nblks; dvbase += dmmax) {
366 blk = min(aligned_nblks - dvbase, dmmax);
367 vsbase = index * dmmax + dvbase * nswdev;
368 blist_free(swapblist, vsbase, blk);
369 vm_swap_size += blk;
370 vm_swap_max += blk;
372 swap_pager_newswap();
373 error = 0;
374 done:
375 mtx_unlock(&swap_mtx);
376 lwkt_reltoken(&vm_token);
377 return (error);
381 * swapoff_args(char *name)
383 * System call swapoff(name) disables swapping on device name,
384 * which must be an active swap device. Return ENOMEM
385 * if there is not enough memory to page in the contents of
386 * the given device.
388 * No requirements.
391 sys_swapoff(struct swapoff_args *uap)
393 struct vnode *vp;
394 struct nlookupdata nd;
395 struct swdevt *sp;
396 int error, index;
398 error = priv_check(curthread, PRIV_ROOT);
399 if (error)
400 return (error);
402 mtx_lock(&swap_mtx);
403 get_mplock();
404 vp = NULL;
405 error = nlookup_init(&nd, uap->name, UIO_USERSPACE, NLC_FOLLOW);
406 if (error == 0)
407 error = nlookup(&nd);
408 if (error == 0)
409 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
410 nlookup_done(&nd);
411 if (error)
412 goto done;
414 for (sp = swdevt, index = 0; index < nswdev; index++, sp++) {
415 if (sp->sw_vp == vp)
416 goto found;
418 error = EINVAL;
419 goto done;
420 found:
421 error = swapoff_one(index);
423 done:
424 rel_mplock();
425 mtx_unlock(&swap_mtx);
426 return (error);
429 static int
430 swapoff_one(int index)
432 swblk_t blk, aligned_nblks;
433 swblk_t dvbase, vsbase;
434 u_int pq_active_clean, pq_inactive_clean;
435 struct swdevt *sp;
436 struct vm_page marker;
437 vm_page_t m;
438 int q;
440 mtx_lock(&swap_mtx);
442 sp = &swdevt[index];
443 aligned_nblks = sp->sw_nblks;
444 pq_active_clean = pq_inactive_clean = 0;
447 * We can turn off this swap device safely only if the
448 * available virtual memory in the system will fit the amount
449 * of data we will have to page back in, plus an epsilon so
450 * the system doesn't become critically low on swap space.
452 for (q = 0; q < PQ_L2_SIZE; ++q) {
453 bzero(&marker, sizeof(marker));
454 marker.flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER;
455 marker.queue = PQ_ACTIVE + q;
456 marker.pc = q;
457 marker.wire_count = 1;
459 vm_page_queues_spin_lock(marker.queue);
460 TAILQ_INSERT_HEAD(&vm_page_queues[marker.queue].pl,
461 &marker, pageq);
463 while ((m = TAILQ_NEXT(&marker, pageq)) != NULL) {
464 TAILQ_REMOVE(&vm_page_queues[marker.queue].pl,
465 &marker, pageq);
466 TAILQ_INSERT_AFTER(&vm_page_queues[marker.queue].pl, m,
467 &marker, pageq);
468 if (m->flags & (PG_MARKER | PG_FICTITIOUS))
469 continue;
471 if (vm_page_busy_try(m, FALSE) == 0) {
472 vm_page_queues_spin_unlock(marker.queue);
473 if (m->dirty == 0) {
474 vm_page_test_dirty(m);
475 if (m->dirty == 0)
476 ++pq_active_clean;
478 vm_page_wakeup(m);
479 vm_page_queues_spin_lock(marker.queue);
482 TAILQ_REMOVE(&vm_page_queues[marker.queue].pl, &marker, pageq);
483 vm_page_queues_spin_unlock(marker.queue);
485 marker.queue = PQ_INACTIVE + q;
486 marker.pc = q;
487 vm_page_queues_spin_lock(marker.queue);
488 TAILQ_INSERT_HEAD(&vm_page_queues[marker.queue].pl,
489 &marker, pageq);
491 while ((m = TAILQ_NEXT(&marker, pageq)) != NULL) {
492 TAILQ_REMOVE(
493 &vm_page_queues[marker.queue].pl,
494 &marker, pageq);
495 TAILQ_INSERT_AFTER(
496 &vm_page_queues[marker.queue].pl,
497 m, &marker, pageq);
498 if (m->flags & (PG_MARKER | PG_FICTITIOUS))
499 continue;
501 if (vm_page_busy_try(m, FALSE) == 0) {
502 vm_page_queues_spin_unlock(marker.queue);
503 if (m->dirty == 0) {
504 vm_page_test_dirty(m);
505 if (m->dirty == 0)
506 ++pq_inactive_clean;
508 vm_page_wakeup(m);
509 vm_page_queues_spin_lock(marker.queue);
512 TAILQ_REMOVE(&vm_page_queues[marker.queue].pl,
513 &marker, pageq);
514 vm_page_queues_spin_unlock(marker.queue);
517 if (vmstats.v_free_count + vmstats.v_cache_count + pq_active_clean +
518 pq_inactive_clean + vm_swap_size < aligned_nblks + nswap_lowat) {
519 mtx_unlock(&swap_mtx);
520 return (ENOMEM);
524 * Prevent further allocations on this device
526 sp->sw_flags |= SW_CLOSING;
527 for (dvbase = dmmax; dvbase < aligned_nblks; dvbase += dmmax) {
528 blk = min(aligned_nblks - dvbase, dmmax);
529 vsbase = index * dmmax + dvbase * nswdev;
530 vm_swap_size -= blist_fill(swapblist, vsbase, blk);
531 vm_swap_max -= blk;
535 * Page in the contents of the device and close it.
537 if (swap_pager_swapoff(index) && swap_pager_swapoff(index)) {
538 mtx_unlock(&swap_mtx);
539 return (EINTR);
542 VOP_CLOSE(sp->sw_vp, FREAD | FWRITE);
543 vrele(sp->sw_vp);
544 bzero(swdevt + index, sizeof(struct swdevt));
547 * Resize the bitmap based on the nem largest swap device,
548 * or free the bitmap if there are no more devices.
550 for (sp = swdevt, aligned_nblks = 0; sp < swdevt + nswdev; sp++) {
551 if (sp->sw_vp)
552 aligned_nblks = max(aligned_nblks, sp->sw_nblks);
555 nswap = aligned_nblks * nswdev;
557 if (nswap == 0) {
558 blist_destroy(swapblist);
559 swapblist = NULL;
560 vrele(swapdev_vp);
561 swapdev_vp = NULL;
562 } else {
563 blist_resize(&swapblist, nswap, 0);
566 mtx_unlock(&swap_mtx);
567 return (0);
571 * Account for swap space in individual swdevt's. The caller ensures
572 * that the provided range falls into a single swdevt.
574 * +count space freed
575 * -count space allocated
577 void
578 swapacctspace(swblk_t base, swblk_t count)
580 int index;
581 int seg;
583 vm_swap_size += count;
584 seg = base / dmmax;
585 index = seg % nswdev;
586 swdevt[index].sw_nused -= count;
590 * Retrieve swap info
592 static int
593 sysctl_vm_swap_info(SYSCTL_HANDLER_ARGS)
595 struct xswdev xs;
596 struct swdevt *sp;
597 int error;
598 int n;
600 error = 0;
601 for (n = 0; n < nswdev; ++n) {
602 sp = &swdevt[n];
604 xs.xsw_size = sizeof(xs);
605 xs.xsw_version = XSWDEV_VERSION;
606 xs.xsw_blksize = PAGE_SIZE;
607 xs.xsw_dev = sp->sw_dev;
608 xs.xsw_flags = sp->sw_flags;
609 xs.xsw_nblks = sp->sw_nblks;
610 xs.xsw_used = sp->sw_nused;
612 error = SYSCTL_OUT(req, &xs, sizeof(xs));
613 if (error)
614 break;
616 return (error);
619 SYSCTL_INT(_vm, OID_AUTO, nswapdev, CTLFLAG_RD, &nswdev, 0,
620 "Number of swap devices");
621 SYSCTL_NODE(_vm, OID_AUTO, swap_info_array, CTLFLAG_RD, sysctl_vm_swap_info,
622 "Swap statistics by device");