]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/vm/swap_pager.c
Commit a missing piece of r364302.
[FreeBSD/FreeBSD.git] / sys / vm / swap_pager.c
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1998 Matthew Dillon,
5  * Copyright (c) 1994 John S. Dyson
6  * Copyright (c) 1990 University of Utah.
7  * Copyright (c) 1982, 1986, 1989, 1993
8  *      The Regents of the University of California.  All rights reserved.
9  *
10  * This code is derived from software contributed to Berkeley by
11  * the Systems Programming Group of the University of Utah Computer
12  * Science Department.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. All advertising materials mentioning features or use of this software
23  *    must display the following acknowledgement:
24  *      This product includes software developed by the University of
25  *      California, Berkeley and its contributors.
26  * 4. Neither the name of the University nor the names of its contributors
27  *    may be used to endorse or promote products derived from this software
28  *    without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40  * SUCH DAMAGE.
41  *
42  *                              New Swap System
43  *                              Matthew Dillon
44  *
45  * Radix Bitmap 'blists'.
46  *
47  *      - The new swapper uses the new radix bitmap code.  This should scale
48  *        to arbitrarily small or arbitrarily large swap spaces and an almost
49  *        arbitrary degree of fragmentation.
50  *
51  * Features:
52  *
53  *      - on the fly reallocation of swap during putpages.  The new system
54  *        does not try to keep previously allocated swap blocks for dirty
55  *        pages.
56  *
57  *      - on the fly deallocation of swap
58  *
59  *      - No more garbage collection required.  Unnecessarily allocated swap
60  *        blocks only exist for dirty vm_page_t's now and these are already
61  *        cycled (in a high-load system) by the pager.  We also do on-the-fly
62  *        removal of invalidated swap blocks when a page is destroyed
63  *        or renamed.
64  *
65  * from: Utah $Hdr: swap_pager.c 1.4 91/04/30$
66  *
67  *      @(#)swap_pager.c        8.9 (Berkeley) 3/21/94
68  *      @(#)vm_swap.c   8.5 (Berkeley) 2/17/94
69  */
70
71 #include <sys/cdefs.h>
72 __FBSDID("$FreeBSD$");
73
74 #include "opt_vm.h"
75
76 #include <sys/param.h>
77 #include <sys/bio.h>
78 #include <sys/blist.h>
79 #include <sys/buf.h>
80 #include <sys/conf.h>
81 #include <sys/disk.h>
82 #include <sys/disklabel.h>
83 #include <sys/eventhandler.h>
84 #include <sys/fcntl.h>
85 #include <sys/lock.h>
86 #include <sys/kernel.h>
87 #include <sys/mount.h>
88 #include <sys/namei.h>
89 #include <sys/malloc.h>
90 #include <sys/pctrie.h>
91 #include <sys/priv.h>
92 #include <sys/proc.h>
93 #include <sys/racct.h>
94 #include <sys/resource.h>
95 #include <sys/resourcevar.h>
96 #include <sys/rwlock.h>
97 #include <sys/sbuf.h>
98 #include <sys/sysctl.h>
99 #include <sys/sysproto.h>
100 #include <sys/systm.h>
101 #include <sys/sx.h>
102 #include <sys/vmmeter.h>
103 #include <sys/vnode.h>
104
105 #include <security/mac/mac_framework.h>
106
107 #include <vm/vm.h>
108 #include <vm/pmap.h>
109 #include <vm/vm_map.h>
110 #include <vm/vm_kern.h>
111 #include <vm/vm_object.h>
112 #include <vm/vm_page.h>
113 #include <vm/vm_pager.h>
114 #include <vm/vm_pageout.h>
115 #include <vm/vm_param.h>
116 #include <vm/swap_pager.h>
117 #include <vm/vm_extern.h>
118 #include <vm/uma.h>
119
120 #include <geom/geom.h>
121
122 /*
123  * MAX_PAGEOUT_CLUSTER must be a power of 2 between 1 and 64.
124  * The 64-page limit is due to the radix code (kern/subr_blist.c).
125  */
126 #ifndef MAX_PAGEOUT_CLUSTER
127 #define MAX_PAGEOUT_CLUSTER     32
128 #endif
129
130 #if !defined(SWB_NPAGES)
131 #define SWB_NPAGES      MAX_PAGEOUT_CLUSTER
132 #endif
133
134 #define SWAP_META_PAGES         PCTRIE_COUNT
135
136 /*
137  * A swblk structure maps each page index within a
138  * SWAP_META_PAGES-aligned and sized range to the address of an
139  * on-disk swap block (or SWAPBLK_NONE). The collection of these
140  * mappings for an entire vm object is implemented as a pc-trie.
141  */
142 struct swblk {
143         vm_pindex_t     p;
144         daddr_t         d[SWAP_META_PAGES];
145 };
146
147 static MALLOC_DEFINE(M_VMPGDATA, "vm_pgdata", "swap pager private data");
148 static struct mtx sw_dev_mtx;
149 static TAILQ_HEAD(, swdevt) swtailq = TAILQ_HEAD_INITIALIZER(swtailq);
150 static struct swdevt *swdevhd;  /* Allocate from here next */
151 static int nswapdev;            /* Number of swap devices */
152 int swap_pager_avail;
153 static struct sx swdev_syscall_lock;    /* serialize swap(on|off) */
154
155 static __exclusive_cache_line u_long swap_reserved;
156 static u_long swap_total;
157 static int sysctl_page_shift(SYSCTL_HANDLER_ARGS);
158
159 static SYSCTL_NODE(_vm_stats, OID_AUTO, swap, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
160     "VM swap stats");
161
162 SYSCTL_PROC(_vm, OID_AUTO, swap_reserved, CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE,
163     &swap_reserved, 0, sysctl_page_shift, "A", 
164     "Amount of swap storage needed to back all allocated anonymous memory.");
165 SYSCTL_PROC(_vm, OID_AUTO, swap_total, CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE,
166     &swap_total, 0, sysctl_page_shift, "A", 
167     "Total amount of available swap storage.");
168
169 static int overcommit = 0;
170 SYSCTL_INT(_vm, VM_OVERCOMMIT, overcommit, CTLFLAG_RW, &overcommit, 0,
171     "Configure virtual memory overcommit behavior. See tuning(7) "
172     "for details.");
173 static unsigned long swzone;
174 SYSCTL_ULONG(_vm, OID_AUTO, swzone, CTLFLAG_RD, &swzone, 0,
175     "Actual size of swap metadata zone");
176 static unsigned long swap_maxpages;
177 SYSCTL_ULONG(_vm, OID_AUTO, swap_maxpages, CTLFLAG_RD, &swap_maxpages, 0,
178     "Maximum amount of swap supported");
179
180 static COUNTER_U64_DEFINE_EARLY(swap_free_deferred);
181 SYSCTL_COUNTER_U64(_vm_stats_swap, OID_AUTO, free_deferred,
182     CTLFLAG_RD, &swap_free_deferred,
183     "Number of pages that deferred freeing swap space");
184
185 static COUNTER_U64_DEFINE_EARLY(swap_free_completed);
186 SYSCTL_COUNTER_U64(_vm_stats_swap, OID_AUTO, free_completed,
187     CTLFLAG_RD, &swap_free_completed,
188     "Number of deferred frees completed");
189
190 /* bits from overcommit */
191 #define SWAP_RESERVE_FORCE_ON           (1 << 0)
192 #define SWAP_RESERVE_RLIMIT_ON          (1 << 1)
193 #define SWAP_RESERVE_ALLOW_NONWIRED     (1 << 2)
194
195 static int
196 sysctl_page_shift(SYSCTL_HANDLER_ARGS)
197 {
198         uint64_t newval;
199         u_long value = *(u_long *)arg1;
200
201         newval = ((uint64_t)value) << PAGE_SHIFT;
202         return (sysctl_handle_64(oidp, &newval, 0, req));
203 }
204
205 static bool
206 swap_reserve_by_cred_rlimit(u_long pincr, struct ucred *cred, int oc)
207 {
208         struct uidinfo *uip;
209         u_long prev;
210
211         uip = cred->cr_ruidinfo;
212
213         prev = atomic_fetchadd_long(&uip->ui_vmsize, pincr);
214         if ((oc & SWAP_RESERVE_RLIMIT_ON) != 0 &&
215             prev + pincr > lim_cur(curthread, RLIMIT_SWAP) &&
216             priv_check(curthread, PRIV_VM_SWAP_NORLIMIT) != 0) {
217                 prev = atomic_fetchadd_long(&uip->ui_vmsize, -pincr);
218                 KASSERT(prev >= pincr, ("negative vmsize for uid = %d\n", uip->ui_uid));
219                 return (false);
220         }
221         return (true);
222 }
223
224 static void
225 swap_release_by_cred_rlimit(u_long pdecr, struct ucred *cred)
226 {
227         struct uidinfo *uip;
228 #ifdef INVARIANTS
229         u_long prev;
230 #endif
231
232         uip = cred->cr_ruidinfo;
233
234 #ifdef INVARIANTS
235         prev = atomic_fetchadd_long(&uip->ui_vmsize, -pdecr);
236         KASSERT(prev >= pdecr, ("negative vmsize for uid = %d\n", uip->ui_uid));
237 #else
238         atomic_subtract_long(&uip->ui_vmsize, pdecr);
239 #endif
240 }
241
242 static void
243 swap_reserve_force_rlimit(u_long pincr, struct ucred *cred)
244 {
245         struct uidinfo *uip;
246
247         uip = cred->cr_ruidinfo;
248         atomic_add_long(&uip->ui_vmsize, pincr);
249 }
250
251 bool
252 swap_reserve(vm_ooffset_t incr)
253 {
254
255         return (swap_reserve_by_cred(incr, curthread->td_ucred));
256 }
257
258 bool
259 swap_reserve_by_cred(vm_ooffset_t incr, struct ucred *cred)
260 {
261         u_long r, s, prev, pincr;
262 #ifdef RACCT
263         int error;
264 #endif
265         int oc;
266         static int curfail;
267         static struct timeval lastfail;
268
269         KASSERT((incr & PAGE_MASK) == 0, ("%s: incr: %ju & PAGE_MASK", __func__,
270             (uintmax_t)incr));
271
272 #ifdef RACCT
273         if (RACCT_ENABLED()) {
274                 PROC_LOCK(curproc);
275                 error = racct_add(curproc, RACCT_SWAP, incr);
276                 PROC_UNLOCK(curproc);
277                 if (error != 0)
278                         return (false);
279         }
280 #endif
281
282         pincr = atop(incr);
283         prev = atomic_fetchadd_long(&swap_reserved, pincr);
284         r = prev + pincr;
285         s = swap_total;
286         oc = atomic_load_int(&overcommit);
287         if (r > s && (oc & SWAP_RESERVE_ALLOW_NONWIRED) != 0) {
288                 s += vm_cnt.v_page_count - vm_cnt.v_free_reserved -
289                     vm_wire_count();
290         }
291         if ((oc & SWAP_RESERVE_FORCE_ON) != 0 && r > s &&
292             priv_check(curthread, PRIV_VM_SWAP_NOQUOTA) != 0) {
293                 prev = atomic_fetchadd_long(&swap_reserved, -pincr);
294                 KASSERT(prev >= pincr, ("swap_reserved < incr on overcommit fail"));
295                 goto out_error;
296         }
297
298         if (!swap_reserve_by_cred_rlimit(pincr, cred, oc)) {
299                 prev = atomic_fetchadd_long(&swap_reserved, -pincr);
300                 KASSERT(prev >= pincr, ("swap_reserved < incr on overcommit fail"));
301                 goto out_error;
302         }
303
304         return (true);
305
306 out_error:
307         if (ppsratecheck(&lastfail, &curfail, 1)) {
308                 printf("uid %d, pid %d: swap reservation for %jd bytes failed\n",
309                     cred->cr_ruidinfo->ui_uid, curproc->p_pid, incr);
310         }
311 #ifdef RACCT
312         if (RACCT_ENABLED()) {
313                 PROC_LOCK(curproc);
314                 racct_sub(curproc, RACCT_SWAP, incr);
315                 PROC_UNLOCK(curproc);
316         }
317 #endif
318
319         return (false);
320 }
321
322 void
323 swap_reserve_force(vm_ooffset_t incr)
324 {
325         u_long pincr;
326
327         KASSERT((incr & PAGE_MASK) == 0, ("%s: incr: %ju & PAGE_MASK", __func__,
328             (uintmax_t)incr));
329
330 #ifdef RACCT
331         if (RACCT_ENABLED()) {
332                 PROC_LOCK(curproc);
333                 racct_add_force(curproc, RACCT_SWAP, incr);
334                 PROC_UNLOCK(curproc);
335         }
336 #endif
337         pincr = atop(incr);
338         atomic_add_long(&swap_reserved, pincr);
339         swap_reserve_force_rlimit(pincr, curthread->td_ucred);
340 }
341
342 void
343 swap_release(vm_ooffset_t decr)
344 {
345         struct ucred *cred;
346
347         PROC_LOCK(curproc);
348         cred = curproc->p_ucred;
349         swap_release_by_cred(decr, cred);
350         PROC_UNLOCK(curproc);
351 }
352
353 void
354 swap_release_by_cred(vm_ooffset_t decr, struct ucred *cred)
355 {
356         u_long pdecr;
357 #ifdef INVARIANTS
358         u_long prev;
359 #endif
360
361         KASSERT((decr & PAGE_MASK) == 0, ("%s: decr: %ju & PAGE_MASK", __func__,
362             (uintmax_t)decr));
363
364         pdecr = atop(decr);
365 #ifdef INVARIANTS
366         prev = atomic_fetchadd_long(&swap_reserved, -pdecr);
367         KASSERT(prev >= pdecr, ("swap_reserved < decr"));
368 #else
369         atomic_subtract_long(&swap_reserved, pdecr);
370 #endif
371
372         swap_release_by_cred_rlimit(pdecr, cred);
373 #ifdef RACCT
374         if (racct_enable)
375                 racct_sub_cred(cred, RACCT_SWAP, decr);
376 #endif
377 }
378
379 static int swap_pager_full = 2; /* swap space exhaustion (task killing) */
380 static int swap_pager_almost_full = 1; /* swap space exhaustion (w/hysteresis)*/
381 static struct mtx swbuf_mtx;    /* to sync nsw_wcount_async */
382 static int nsw_wcount_async;    /* limit async write buffers */
383 static int nsw_wcount_async_max;/* assigned maximum                     */
384 static int nsw_cluster_max;     /* maximum VOP I/O allowed              */
385
386 static int sysctl_swap_async_max(SYSCTL_HANDLER_ARGS);
387 SYSCTL_PROC(_vm, OID_AUTO, swap_async_max, CTLTYPE_INT | CTLFLAG_RW |
388     CTLFLAG_MPSAFE, NULL, 0, sysctl_swap_async_max, "I",
389     "Maximum running async swap ops");
390 static int sysctl_swap_fragmentation(SYSCTL_HANDLER_ARGS);
391 SYSCTL_PROC(_vm, OID_AUTO, swap_fragmentation, CTLTYPE_STRING | CTLFLAG_RD |
392     CTLFLAG_MPSAFE, NULL, 0, sysctl_swap_fragmentation, "A",
393     "Swap Fragmentation Info");
394
395 static struct sx sw_alloc_sx;
396
397 /*
398  * "named" and "unnamed" anon region objects.  Try to reduce the overhead
399  * of searching a named list by hashing it just a little.
400  */
401
402 #define NOBJLISTS               8
403
404 #define NOBJLIST(handle)        \
405         (&swap_pager_object_list[((int)(intptr_t)handle >> 4) & (NOBJLISTS-1)])
406
407 static struct pagerlst  swap_pager_object_list[NOBJLISTS];
408 static uma_zone_t swwbuf_zone;
409 static uma_zone_t swrbuf_zone;
410 static uma_zone_t swblk_zone;
411 static uma_zone_t swpctrie_zone;
412
413 /*
414  * pagerops for OBJT_SWAP - "swap pager".  Some ops are also global procedure
415  * calls hooked from other parts of the VM system and do not appear here.
416  * (see vm/swap_pager.h).
417  */
418 static vm_object_t
419                 swap_pager_alloc(void *handle, vm_ooffset_t size,
420                     vm_prot_t prot, vm_ooffset_t offset, struct ucred *);
421 static void     swap_pager_dealloc(vm_object_t object);
422 static int      swap_pager_getpages(vm_object_t, vm_page_t *, int, int *,
423     int *);
424 static int      swap_pager_getpages_async(vm_object_t, vm_page_t *, int, int *,
425     int *, pgo_getpages_iodone_t, void *);
426 static void     swap_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *);
427 static boolean_t
428                 swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, int *after);
429 static void     swap_pager_init(void);
430 static void     swap_pager_unswapped(vm_page_t);
431 static void     swap_pager_swapoff(struct swdevt *sp);
432 static void     swap_pager_update_writecount(vm_object_t object,
433     vm_offset_t start, vm_offset_t end);
434 static void     swap_pager_release_writecount(vm_object_t object,
435     vm_offset_t start, vm_offset_t end);
436
437 struct pagerops swappagerops = {
438         .pgo_init =     swap_pager_init,        /* early system initialization of pager */
439         .pgo_alloc =    swap_pager_alloc,       /* allocate an OBJT_SWAP object         */
440         .pgo_dealloc =  swap_pager_dealloc,     /* deallocate an OBJT_SWAP object       */
441         .pgo_getpages = swap_pager_getpages,    /* pagein                               */
442         .pgo_getpages_async = swap_pager_getpages_async, /* pagein (async)              */
443         .pgo_putpages = swap_pager_putpages,    /* pageout                              */
444         .pgo_haspage =  swap_pager_haspage,     /* get backing store status for page    */
445         .pgo_pageunswapped = swap_pager_unswapped,      /* remove swap related to page          */
446         .pgo_update_writecount = swap_pager_update_writecount,
447         .pgo_release_writecount = swap_pager_release_writecount,
448 };
449
450 /*
451  * swap_*() routines are externally accessible.  swp_*() routines are
452  * internal.
453  */
454 static int nswap_lowat = 128;   /* in pages, swap_pager_almost_full warn */
455 static int nswap_hiwat = 512;   /* in pages, swap_pager_almost_full warn */
456
457 SYSCTL_INT(_vm, OID_AUTO, dmmax, CTLFLAG_RD, &nsw_cluster_max, 0,
458     "Maximum size of a swap block in pages");
459
460 static void     swp_sizecheck(void);
461 static void     swp_pager_async_iodone(struct buf *bp);
462 static bool     swp_pager_swblk_empty(struct swblk *sb, int start, int limit);
463 static void     swp_pager_free_empty_swblk(vm_object_t, struct swblk *sb);
464 static int      swapongeom(struct vnode *);
465 static int      swaponvp(struct thread *, struct vnode *, u_long);
466 static int      swapoff_one(struct swdevt *sp, struct ucred *cred);
467
468 /*
469  * Swap bitmap functions
470  */
471 static void     swp_pager_freeswapspace(daddr_t blk, daddr_t npages);
472 static daddr_t  swp_pager_getswapspace(int *npages);
473
474 /*
475  * Metadata functions
476  */
477 static daddr_t swp_pager_meta_build(vm_object_t, vm_pindex_t, daddr_t);
478 static void swp_pager_meta_free(vm_object_t, vm_pindex_t, vm_pindex_t);
479 static void swp_pager_meta_transfer(vm_object_t src, vm_object_t dst,
480     vm_pindex_t pindex, vm_pindex_t count);
481 static void swp_pager_meta_free_all(vm_object_t);
482 static daddr_t swp_pager_meta_lookup(vm_object_t, vm_pindex_t);
483
484 static void
485 swp_pager_init_freerange(daddr_t *start, daddr_t *num)
486 {
487
488         *start = SWAPBLK_NONE;
489         *num = 0;
490 }
491
492 static void
493 swp_pager_update_freerange(daddr_t *start, daddr_t *num, daddr_t addr)
494 {
495
496         if (*start + *num == addr) {
497                 (*num)++;
498         } else {
499                 swp_pager_freeswapspace(*start, *num);
500                 *start = addr;
501                 *num = 1;
502         }
503 }
504
505 static void *
506 swblk_trie_alloc(struct pctrie *ptree)
507 {
508
509         return (uma_zalloc(swpctrie_zone, M_NOWAIT | (curproc == pageproc ?
510             M_USE_RESERVE : 0)));
511 }
512
513 static void
514 swblk_trie_free(struct pctrie *ptree, void *node)
515 {
516
517         uma_zfree(swpctrie_zone, node);
518 }
519
520 PCTRIE_DEFINE(SWAP, swblk, p, swblk_trie_alloc, swblk_trie_free);
521
522 /*
523  * SWP_SIZECHECK() -    update swap_pager_full indication
524  *
525  *      update the swap_pager_almost_full indication and warn when we are
526  *      about to run out of swap space, using lowat/hiwat hysteresis.
527  *
528  *      Clear swap_pager_full ( task killing ) indication when lowat is met.
529  *
530  *      No restrictions on call
531  *      This routine may not block.
532  */
533 static void
534 swp_sizecheck(void)
535 {
536
537         if (swap_pager_avail < nswap_lowat) {
538                 if (swap_pager_almost_full == 0) {
539                         printf("swap_pager: out of swap space\n");
540                         swap_pager_almost_full = 1;
541                 }
542         } else {
543                 swap_pager_full = 0;
544                 if (swap_pager_avail > nswap_hiwat)
545                         swap_pager_almost_full = 0;
546         }
547 }
548
549 /*
550  * SWAP_PAGER_INIT() -  initialize the swap pager!
551  *
552  *      Expected to be started from system init.  NOTE:  This code is run
553  *      before much else so be careful what you depend on.  Most of the VM
554  *      system has yet to be initialized at this point.
555  */
556 static void
557 swap_pager_init(void)
558 {
559         /*
560          * Initialize object lists
561          */
562         int i;
563
564         for (i = 0; i < NOBJLISTS; ++i)
565                 TAILQ_INIT(&swap_pager_object_list[i]);
566         mtx_init(&sw_dev_mtx, "swapdev", NULL, MTX_DEF);
567         sx_init(&sw_alloc_sx, "swspsx");
568         sx_init(&swdev_syscall_lock, "swsysc");
569 }
570
571 /*
572  * SWAP_PAGER_SWAP_INIT() - swap pager initialization from pageout process
573  *
574  *      Expected to be started from pageout process once, prior to entering
575  *      its main loop.
576  */
577 void
578 swap_pager_swap_init(void)
579 {
580         unsigned long n, n2;
581
582         /*
583          * Number of in-transit swap bp operations.  Don't
584          * exhaust the pbufs completely.  Make sure we
585          * initialize workable values (0 will work for hysteresis
586          * but it isn't very efficient).
587          *
588          * The nsw_cluster_max is constrained by the bp->b_pages[]
589          * array, which has MAXPHYS / PAGE_SIZE entries, and our locally
590          * defined MAX_PAGEOUT_CLUSTER.   Also be aware that swap ops are
591          * constrained by the swap device interleave stripe size.
592          *
593          * Currently we hardwire nsw_wcount_async to 4.  This limit is
594          * designed to prevent other I/O from having high latencies due to
595          * our pageout I/O.  The value 4 works well for one or two active swap
596          * devices but is probably a little low if you have more.  Even so,
597          * a higher value would probably generate only a limited improvement
598          * with three or four active swap devices since the system does not
599          * typically have to pageout at extreme bandwidths.   We will want
600          * at least 2 per swap devices, and 4 is a pretty good value if you
601          * have one NFS swap device due to the command/ack latency over NFS.
602          * So it all works out pretty well.
603          */
604         nsw_cluster_max = min(MAXPHYS / PAGE_SIZE, MAX_PAGEOUT_CLUSTER);
605
606         nsw_wcount_async = 4;
607         nsw_wcount_async_max = nsw_wcount_async;
608         mtx_init(&swbuf_mtx, "async swbuf mutex", NULL, MTX_DEF);
609
610         swwbuf_zone = pbuf_zsecond_create("swwbuf", nswbuf / 4);
611         swrbuf_zone = pbuf_zsecond_create("swrbuf", nswbuf / 2);
612
613         /*
614          * Initialize our zone, taking the user's requested size or
615          * estimating the number we need based on the number of pages
616          * in the system.
617          */
618         n = maxswzone != 0 ? maxswzone / sizeof(struct swblk) :
619             vm_cnt.v_page_count / 2;
620         swpctrie_zone = uma_zcreate("swpctrie", pctrie_node_size(), NULL, NULL,
621             pctrie_zone_init, NULL, UMA_ALIGN_PTR, 0);
622         if (swpctrie_zone == NULL)
623                 panic("failed to create swap pctrie zone.");
624         swblk_zone = uma_zcreate("swblk", sizeof(struct swblk), NULL, NULL,
625             NULL, NULL, _Alignof(struct swblk) - 1, 0);
626         if (swblk_zone == NULL)
627                 panic("failed to create swap blk zone.");
628         n2 = n;
629         do {
630                 if (uma_zone_reserve_kva(swblk_zone, n))
631                         break;
632                 /*
633                  * if the allocation failed, try a zone two thirds the
634                  * size of the previous attempt.
635                  */
636                 n -= ((n + 2) / 3);
637         } while (n > 0);
638
639         /*
640          * Often uma_zone_reserve_kva() cannot reserve exactly the
641          * requested size.  Account for the difference when
642          * calculating swap_maxpages.
643          */
644         n = uma_zone_get_max(swblk_zone);
645
646         if (n < n2)
647                 printf("Swap blk zone entries changed from %lu to %lu.\n",
648                     n2, n);
649         /* absolute maximum we can handle assuming 100% efficiency */
650         swap_maxpages = n * SWAP_META_PAGES;
651         swzone = n * sizeof(struct swblk);
652         if (!uma_zone_reserve_kva(swpctrie_zone, n))
653                 printf("Cannot reserve swap pctrie zone, "
654                     "reduce kern.maxswzone.\n");
655 }
656
657 static vm_object_t
658 swap_pager_alloc_init(void *handle, struct ucred *cred, vm_ooffset_t size,
659     vm_ooffset_t offset)
660 {
661         vm_object_t object;
662
663         if (cred != NULL) {
664                 if (!swap_reserve_by_cred(size, cred))
665                         return (NULL);
666                 crhold(cred);
667         }
668
669         /*
670          * The un_pager.swp.swp_blks trie is initialized by
671          * vm_object_allocate() to ensure the correct order of
672          * visibility to other threads.
673          */
674         object = vm_object_allocate(OBJT_SWAP, OFF_TO_IDX(offset +
675             PAGE_MASK + size));
676
677         object->un_pager.swp.writemappings = 0;
678         object->handle = handle;
679         if (cred != NULL) {
680                 object->cred = cred;
681                 object->charge = size;
682         }
683         return (object);
684 }
685
686 /*
687  * SWAP_PAGER_ALLOC() - allocate a new OBJT_SWAP VM object and instantiate
688  *                      its metadata structures.
689  *
690  *      This routine is called from the mmap and fork code to create a new
691  *      OBJT_SWAP object.
692  *
693  *      This routine must ensure that no live duplicate is created for
694  *      the named object request, which is protected against by
695  *      holding the sw_alloc_sx lock in case handle != NULL.
696  */
697 static vm_object_t
698 swap_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
699     vm_ooffset_t offset, struct ucred *cred)
700 {
701         vm_object_t object;
702
703         if (handle != NULL) {
704                 /*
705                  * Reference existing named region or allocate new one.  There
706                  * should not be a race here against swp_pager_meta_build()
707                  * as called from vm_page_remove() in regards to the lookup
708                  * of the handle.
709                  */
710                 sx_xlock(&sw_alloc_sx);
711                 object = vm_pager_object_lookup(NOBJLIST(handle), handle);
712                 if (object == NULL) {
713                         object = swap_pager_alloc_init(handle, cred, size,
714                             offset);
715                         if (object != NULL) {
716                                 TAILQ_INSERT_TAIL(NOBJLIST(object->handle),
717                                     object, pager_object_list);
718                         }
719                 }
720                 sx_xunlock(&sw_alloc_sx);
721         } else {
722                 object = swap_pager_alloc_init(handle, cred, size, offset);
723         }
724         return (object);
725 }
726
727 /*
728  * SWAP_PAGER_DEALLOC() -       remove swap metadata from object
729  *
730  *      The swap backing for the object is destroyed.  The code is
731  *      designed such that we can reinstantiate it later, but this
732  *      routine is typically called only when the entire object is
733  *      about to be destroyed.
734  *
735  *      The object must be locked.
736  */
737 static void
738 swap_pager_dealloc(vm_object_t object)
739 {
740
741         VM_OBJECT_ASSERT_WLOCKED(object);
742         KASSERT((object->flags & OBJ_DEAD) != 0, ("dealloc of reachable obj"));
743
744         /*
745          * Remove from list right away so lookups will fail if we block for
746          * pageout completion.
747          */
748         if ((object->flags & OBJ_ANON) == 0 && object->handle != NULL) {
749                 VM_OBJECT_WUNLOCK(object);
750                 sx_xlock(&sw_alloc_sx);
751                 TAILQ_REMOVE(NOBJLIST(object->handle), object,
752                     pager_object_list);
753                 sx_xunlock(&sw_alloc_sx);
754                 VM_OBJECT_WLOCK(object);
755         }
756
757         vm_object_pip_wait(object, "swpdea");
758
759         /*
760          * Free all remaining metadata.  We only bother to free it from
761          * the swap meta data.  We do not attempt to free swapblk's still
762          * associated with vm_page_t's for this object.  We do not care
763          * if paging is still in progress on some objects.
764          */
765         swp_pager_meta_free_all(object);
766         object->handle = NULL;
767         object->type = OBJT_DEAD;
768 }
769
770 /************************************************************************
771  *                      SWAP PAGER BITMAP ROUTINES                      *
772  ************************************************************************/
773
774 /*
775  * SWP_PAGER_GETSWAPSPACE() -   allocate raw swap space
776  *
777  *      Allocate swap for up to the requested number of pages.  The
778  *      starting swap block number (a page index) is returned or
779  *      SWAPBLK_NONE if the allocation failed.
780  *
781  *      Also has the side effect of advising that somebody made a mistake
782  *      when they configured swap and didn't configure enough.
783  *
784  *      This routine may not sleep.
785  *
786  *      We allocate in round-robin fashion from the configured devices.
787  */
788 static daddr_t
789 swp_pager_getswapspace(int *io_npages)
790 {
791         daddr_t blk;
792         struct swdevt *sp;
793         int mpages, npages;
794
795         KASSERT(*io_npages >= 1,
796             ("%s: npages not positive", __func__));
797         blk = SWAPBLK_NONE;
798         mpages = *io_npages;
799         npages = imin(BLIST_MAX_ALLOC, mpages);
800         mtx_lock(&sw_dev_mtx);
801         sp = swdevhd;
802         while (!TAILQ_EMPTY(&swtailq)) {
803                 if (sp == NULL)
804                         sp = TAILQ_FIRST(&swtailq);
805                 if ((sp->sw_flags & SW_CLOSING) == 0)
806                         blk = blist_alloc(sp->sw_blist, &npages, mpages);
807                 if (blk != SWAPBLK_NONE)
808                         break;
809                 sp = TAILQ_NEXT(sp, sw_list);
810                 if (swdevhd == sp) {
811                         if (npages == 1)
812                                 break;
813                         mpages = npages - 1;
814                         npages >>= 1;
815                 }
816         }
817         if (blk != SWAPBLK_NONE) {
818                 *io_npages = npages;
819                 blk += sp->sw_first;
820                 sp->sw_used += npages;
821                 swap_pager_avail -= npages;
822                 swp_sizecheck();
823                 swdevhd = TAILQ_NEXT(sp, sw_list);
824         } else {
825                 if (swap_pager_full != 2) {
826                         printf("swp_pager_getswapspace(%d): failed\n",
827                             *io_npages);
828                         swap_pager_full = 2;
829                         swap_pager_almost_full = 1;
830                 }
831                 swdevhd = NULL;
832         }
833         mtx_unlock(&sw_dev_mtx);
834         return (blk);
835 }
836
837 static bool
838 swp_pager_isondev(daddr_t blk, struct swdevt *sp)
839 {
840
841         return (blk >= sp->sw_first && blk < sp->sw_end);
842 }
843
844 static void
845 swp_pager_strategy(struct buf *bp)
846 {
847         struct swdevt *sp;
848
849         mtx_lock(&sw_dev_mtx);
850         TAILQ_FOREACH(sp, &swtailq, sw_list) {
851                 if (swp_pager_isondev(bp->b_blkno, sp)) {
852                         mtx_unlock(&sw_dev_mtx);
853                         if ((sp->sw_flags & SW_UNMAPPED) != 0 &&
854                             unmapped_buf_allowed) {
855                                 bp->b_data = unmapped_buf;
856                                 bp->b_offset = 0;
857                         } else {
858                                 pmap_qenter((vm_offset_t)bp->b_data,
859                                     &bp->b_pages[0], bp->b_bcount / PAGE_SIZE);
860                         }
861                         sp->sw_strategy(bp, sp);
862                         return;
863                 }
864         }
865         panic("Swapdev not found");
866 }
867
868
869 /*
870  * SWP_PAGER_FREESWAPSPACE() -  free raw swap space
871  *
872  *      This routine returns the specified swap blocks back to the bitmap.
873  *
874  *      This routine may not sleep.
875  */
876 static void
877 swp_pager_freeswapspace(daddr_t blk, daddr_t npages)
878 {
879         struct swdevt *sp;
880
881         if (npages == 0)
882                 return;
883         mtx_lock(&sw_dev_mtx);
884         TAILQ_FOREACH(sp, &swtailq, sw_list) {
885                 if (swp_pager_isondev(blk, sp)) {
886                         sp->sw_used -= npages;
887                         /*
888                          * If we are attempting to stop swapping on
889                          * this device, we don't want to mark any
890                          * blocks free lest they be reused.
891                          */
892                         if ((sp->sw_flags & SW_CLOSING) == 0) {
893                                 blist_free(sp->sw_blist, blk - sp->sw_first,
894                                     npages);
895                                 swap_pager_avail += npages;
896                                 swp_sizecheck();
897                         }
898                         mtx_unlock(&sw_dev_mtx);
899                         return;
900                 }
901         }
902         panic("Swapdev not found");
903 }
904
905 /*
906  * SYSCTL_SWAP_FRAGMENTATION() -        produce raw swap space stats
907  */
908 static int
909 sysctl_swap_fragmentation(SYSCTL_HANDLER_ARGS)
910 {
911         struct sbuf sbuf;
912         struct swdevt *sp;
913         const char *devname;
914         int error;
915
916         error = sysctl_wire_old_buffer(req, 0);
917         if (error != 0)
918                 return (error);
919         sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
920         mtx_lock(&sw_dev_mtx);
921         TAILQ_FOREACH(sp, &swtailq, sw_list) {
922                 if (vn_isdisk(sp->sw_vp, NULL))
923                         devname = devtoname(sp->sw_vp->v_rdev);
924                 else
925                         devname = "[file]";
926                 sbuf_printf(&sbuf, "\nFree space on device %s:\n", devname);
927                 blist_stats(sp->sw_blist, &sbuf);
928         }
929         mtx_unlock(&sw_dev_mtx);
930         error = sbuf_finish(&sbuf);
931         sbuf_delete(&sbuf);
932         return (error);
933 }
934
935 /*
936  * SWAP_PAGER_FREESPACE() -     frees swap blocks associated with a page
937  *                              range within an object.
938  *
939  *      This is a globally accessible routine.
940  *
941  *      This routine removes swapblk assignments from swap metadata.
942  *
943  *      The external callers of this routine typically have already destroyed
944  *      or renamed vm_page_t's associated with this range in the object so
945  *      we should be ok.
946  *
947  *      The object must be locked.
948  */
949 void
950 swap_pager_freespace(vm_object_t object, vm_pindex_t start, vm_size_t size)
951 {
952
953         swp_pager_meta_free(object, start, size);
954 }
955
956 /*
957  * SWAP_PAGER_RESERVE() - reserve swap blocks in object
958  *
959  *      Assigns swap blocks to the specified range within the object.  The
960  *      swap blocks are not zeroed.  Any previous swap assignment is destroyed.
961  *
962  *      Returns 0 on success, -1 on failure.
963  */
964 int
965 swap_pager_reserve(vm_object_t object, vm_pindex_t start, vm_size_t size)
966 {
967         daddr_t addr, blk, n_free, s_free;
968         int i, j, n;
969
970         swp_pager_init_freerange(&s_free, &n_free);
971         VM_OBJECT_WLOCK(object);
972         for (i = 0; i < size; i += n) {
973                 n = size - i;
974                 blk = swp_pager_getswapspace(&n);
975                 if (blk == SWAPBLK_NONE) {
976                         swp_pager_meta_free(object, start, i);
977                         VM_OBJECT_WUNLOCK(object);
978                         return (-1);
979                 }
980                 for (j = 0; j < n; ++j) {
981                         addr = swp_pager_meta_build(object,
982                             start + i + j, blk + j);
983                         if (addr != SWAPBLK_NONE)
984                                 swp_pager_update_freerange(&s_free, &n_free,
985                                     addr);
986                 }
987         }
988         swp_pager_freeswapspace(s_free, n_free);
989         VM_OBJECT_WUNLOCK(object);
990         return (0);
991 }
992
993 static bool
994 swp_pager_xfer_source(vm_object_t srcobject, vm_object_t dstobject,
995     vm_pindex_t pindex, daddr_t addr)
996 {
997         daddr_t dstaddr;
998
999         KASSERT(srcobject->type == OBJT_SWAP,
1000             ("%s: Srcobject not swappable", __func__));
1001         if (dstobject->type == OBJT_SWAP &&
1002             swp_pager_meta_lookup(dstobject, pindex) != SWAPBLK_NONE) {
1003                 /* Caller should destroy the source block. */
1004                 return (false);
1005         }
1006
1007         /*
1008          * Destination has no swapblk and is not resident, transfer source.
1009          * swp_pager_meta_build() can sleep.
1010          */
1011         VM_OBJECT_WUNLOCK(srcobject);
1012         dstaddr = swp_pager_meta_build(dstobject, pindex, addr);
1013         KASSERT(dstaddr == SWAPBLK_NONE,
1014             ("Unexpected destination swapblk"));
1015         VM_OBJECT_WLOCK(srcobject);
1016
1017         return (true);
1018 }
1019
1020 /*
1021  * SWAP_PAGER_COPY() -  copy blocks from source pager to destination pager
1022  *                      and destroy the source.
1023  *
1024  *      Copy any valid swapblks from the source to the destination.  In
1025  *      cases where both the source and destination have a valid swapblk,
1026  *      we keep the destination's.
1027  *
1028  *      This routine is allowed to sleep.  It may sleep allocating metadata
1029  *      indirectly through swp_pager_meta_build().
1030  *
1031  *      The source object contains no vm_page_t's (which is just as well)
1032  *
1033  *      The source object is of type OBJT_SWAP.
1034  *
1035  *      The source and destination objects must be locked.
1036  *      Both object locks may temporarily be released.
1037  */
1038 void
1039 swap_pager_copy(vm_object_t srcobject, vm_object_t dstobject,
1040     vm_pindex_t offset, int destroysource)
1041 {
1042
1043         VM_OBJECT_ASSERT_WLOCKED(srcobject);
1044         VM_OBJECT_ASSERT_WLOCKED(dstobject);
1045
1046         /*
1047          * If destroysource is set, we remove the source object from the
1048          * swap_pager internal queue now.
1049          */
1050         if (destroysource && (srcobject->flags & OBJ_ANON) == 0 &&
1051             srcobject->handle != NULL) {
1052                 VM_OBJECT_WUNLOCK(srcobject);
1053                 VM_OBJECT_WUNLOCK(dstobject);
1054                 sx_xlock(&sw_alloc_sx);
1055                 TAILQ_REMOVE(NOBJLIST(srcobject->handle), srcobject,
1056                     pager_object_list);
1057                 sx_xunlock(&sw_alloc_sx);
1058                 VM_OBJECT_WLOCK(dstobject);
1059                 VM_OBJECT_WLOCK(srcobject);
1060         }
1061
1062         /*
1063          * Transfer source to destination.
1064          */
1065         swp_pager_meta_transfer(srcobject, dstobject, offset, dstobject->size);
1066
1067         /*
1068          * Free left over swap blocks in source.
1069          *
1070          * We have to revert the type to OBJT_DEFAULT so we do not accidentally
1071          * double-remove the object from the swap queues.
1072          */
1073         if (destroysource) {
1074                 swp_pager_meta_free_all(srcobject);
1075                 /*
1076                  * Reverting the type is not necessary, the caller is going
1077                  * to destroy srcobject directly, but I'm doing it here
1078                  * for consistency since we've removed the object from its
1079                  * queues.
1080                  */
1081                 srcobject->type = OBJT_DEFAULT;
1082         }
1083 }
1084
1085 /*
1086  * SWAP_PAGER_HASPAGE() -       determine if we have good backing store for
1087  *                              the requested page.
1088  *
1089  *      We determine whether good backing store exists for the requested
1090  *      page and return TRUE if it does, FALSE if it doesn't.
1091  *
1092  *      If TRUE, we also try to determine how much valid, contiguous backing
1093  *      store exists before and after the requested page.
1094  */
1095 static boolean_t
1096 swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
1097     int *after)
1098 {
1099         daddr_t blk, blk0;
1100         int i;
1101
1102         VM_OBJECT_ASSERT_LOCKED(object);
1103         KASSERT(object->type == OBJT_SWAP,
1104             ("%s: object not swappable", __func__));
1105
1106         /*
1107          * do we have good backing store at the requested index ?
1108          */
1109         blk0 = swp_pager_meta_lookup(object, pindex);
1110         if (blk0 == SWAPBLK_NONE) {
1111                 if (before)
1112                         *before = 0;
1113                 if (after)
1114                         *after = 0;
1115                 return (FALSE);
1116         }
1117
1118         /*
1119          * find backwards-looking contiguous good backing store
1120          */
1121         if (before != NULL) {
1122                 for (i = 1; i < SWB_NPAGES; i++) {
1123                         if (i > pindex)
1124                                 break;
1125                         blk = swp_pager_meta_lookup(object, pindex - i);
1126                         if (blk != blk0 - i)
1127                                 break;
1128                 }
1129                 *before = i - 1;
1130         }
1131
1132         /*
1133          * find forward-looking contiguous good backing store
1134          */
1135         if (after != NULL) {
1136                 for (i = 1; i < SWB_NPAGES; i++) {
1137                         blk = swp_pager_meta_lookup(object, pindex + i);
1138                         if (blk != blk0 + i)
1139                                 break;
1140                 }
1141                 *after = i - 1;
1142         }
1143         return (TRUE);
1144 }
1145
1146 /*
1147  * SWAP_PAGER_PAGE_UNSWAPPED() - remove swap backing store related to page
1148  *
1149  *      This removes any associated swap backing store, whether valid or
1150  *      not, from the page.
1151  *
1152  *      This routine is typically called when a page is made dirty, at
1153  *      which point any associated swap can be freed.  MADV_FREE also
1154  *      calls us in a special-case situation
1155  *
1156  *      NOTE!!!  If the page is clean and the swap was valid, the caller
1157  *      should make the page dirty before calling this routine.  This routine
1158  *      does NOT change the m->dirty status of the page.  Also: MADV_FREE
1159  *      depends on it.
1160  *
1161  *      This routine may not sleep.
1162  *
1163  *      The object containing the page may be locked.
1164  */
1165 static void
1166 swap_pager_unswapped(vm_page_t m)
1167 {
1168         struct swblk *sb;
1169         vm_object_t obj;
1170
1171         /*
1172          * Handle enqueing deferred frees first.  If we do not have the
1173          * object lock we wait for the page daemon to clear the space.
1174          */
1175         obj = m->object;
1176         if (!VM_OBJECT_WOWNED(obj)) {
1177                 VM_PAGE_OBJECT_BUSY_ASSERT(m);
1178                 /*
1179                  * The caller is responsible for synchronization but we
1180                  * will harmlessly handle races.  This is typically provided
1181                  * by only calling unswapped() when a page transitions from
1182                  * clean to dirty.
1183                  */
1184                 if ((m->a.flags & (PGA_SWAP_SPACE | PGA_SWAP_FREE)) ==
1185                     PGA_SWAP_SPACE) {
1186                         vm_page_aflag_set(m, PGA_SWAP_FREE);
1187                         counter_u64_add(swap_free_deferred, 1);
1188                 }
1189                 return;
1190         }
1191         if ((m->a.flags & PGA_SWAP_FREE) != 0)
1192                 counter_u64_add(swap_free_completed, 1);
1193         vm_page_aflag_clear(m, PGA_SWAP_FREE | PGA_SWAP_SPACE);
1194
1195         /*
1196          * The meta data only exists if the object is OBJT_SWAP
1197          * and even then might not be allocated yet.
1198          */
1199         KASSERT(m->object->type == OBJT_SWAP,
1200             ("Free object not swappable"));
1201
1202         sb = SWAP_PCTRIE_LOOKUP(&m->object->un_pager.swp.swp_blks,
1203             rounddown(m->pindex, SWAP_META_PAGES));
1204         if (sb == NULL)
1205                 return;
1206         if (sb->d[m->pindex % SWAP_META_PAGES] == SWAPBLK_NONE)
1207                 return;
1208         swp_pager_freeswapspace(sb->d[m->pindex % SWAP_META_PAGES], 1);
1209         sb->d[m->pindex % SWAP_META_PAGES] = SWAPBLK_NONE;
1210         swp_pager_free_empty_swblk(m->object, sb);
1211 }
1212
1213 /*
1214  * swap_pager_getpages() - bring pages in from swap
1215  *
1216  *      Attempt to page in the pages in array "ma" of length "count".  The
1217  *      caller may optionally specify that additional pages preceding and
1218  *      succeeding the specified range be paged in.  The number of such pages
1219  *      is returned in the "rbehind" and "rahead" parameters, and they will
1220  *      be in the inactive queue upon return.
1221  *
1222  *      The pages in "ma" must be busied and will remain busied upon return.
1223  */
1224 static int
1225 swap_pager_getpages_locked(vm_object_t object, vm_page_t *ma, int count,
1226     int *rbehind, int *rahead)
1227 {
1228         struct buf *bp;
1229         vm_page_t bm, mpred, msucc, p;
1230         vm_pindex_t pindex;
1231         daddr_t blk;
1232         int i, maxahead, maxbehind, reqcount;
1233
1234         VM_OBJECT_ASSERT_WLOCKED(object);
1235         reqcount = count;
1236
1237         KASSERT(object->type == OBJT_SWAP,
1238             ("%s: object not swappable", __func__));
1239         if (!swap_pager_haspage(object, ma[0]->pindex, &maxbehind, &maxahead)) {
1240                 VM_OBJECT_WUNLOCK(object);
1241                 return (VM_PAGER_FAIL);
1242         }
1243
1244         KASSERT(reqcount - 1 <= maxahead,
1245             ("page count %d extends beyond swap block", reqcount));
1246
1247         /*
1248          * Do not transfer any pages other than those that are xbusied
1249          * when running during a split or collapse operation.  This
1250          * prevents clustering from re-creating pages which are being
1251          * moved into another object.
1252          */
1253         if ((object->flags & (OBJ_SPLIT | OBJ_DEAD)) != 0) {
1254                 maxahead = reqcount - 1;
1255                 maxbehind = 0;
1256         }
1257
1258         /*
1259          * Clip the readahead and readbehind ranges to exclude resident pages.
1260          */
1261         if (rahead != NULL) {
1262                 *rahead = imin(*rahead, maxahead - (reqcount - 1));
1263                 pindex = ma[reqcount - 1]->pindex;
1264                 msucc = TAILQ_NEXT(ma[reqcount - 1], listq);
1265                 if (msucc != NULL && msucc->pindex - pindex - 1 < *rahead)
1266                         *rahead = msucc->pindex - pindex - 1;
1267         }
1268         if (rbehind != NULL) {
1269                 *rbehind = imin(*rbehind, maxbehind);
1270                 pindex = ma[0]->pindex;
1271                 mpred = TAILQ_PREV(ma[0], pglist, listq);
1272                 if (mpred != NULL && pindex - mpred->pindex - 1 < *rbehind)
1273                         *rbehind = pindex - mpred->pindex - 1;
1274         }
1275
1276         bm = ma[0];
1277         for (i = 0; i < count; i++)
1278                 ma[i]->oflags |= VPO_SWAPINPROG;
1279
1280         /*
1281          * Allocate readahead and readbehind pages.
1282          */
1283         if (rbehind != NULL) {
1284                 for (i = 1; i <= *rbehind; i++) {
1285                         p = vm_page_alloc(object, ma[0]->pindex - i,
1286                             VM_ALLOC_NORMAL);
1287                         if (p == NULL)
1288                                 break;
1289                         p->oflags |= VPO_SWAPINPROG;
1290                         bm = p;
1291                 }
1292                 *rbehind = i - 1;
1293         }
1294         if (rahead != NULL) {
1295                 for (i = 0; i < *rahead; i++) {
1296                         p = vm_page_alloc(object,
1297                             ma[reqcount - 1]->pindex + i + 1, VM_ALLOC_NORMAL);
1298                         if (p == NULL)
1299                                 break;
1300                         p->oflags |= VPO_SWAPINPROG;
1301                 }
1302                 *rahead = i;
1303         }
1304         if (rbehind != NULL)
1305                 count += *rbehind;
1306         if (rahead != NULL)
1307                 count += *rahead;
1308
1309         vm_object_pip_add(object, count);
1310
1311         pindex = bm->pindex;
1312         blk = swp_pager_meta_lookup(object, pindex);
1313         KASSERT(blk != SWAPBLK_NONE,
1314             ("no swap blocking containing %p(%jx)", object, (uintmax_t)pindex));
1315
1316         VM_OBJECT_WUNLOCK(object);
1317         bp = uma_zalloc(swrbuf_zone, M_WAITOK);
1318         /* Pages cannot leave the object while busy. */
1319         for (i = 0, p = bm; i < count; i++, p = TAILQ_NEXT(p, listq)) {
1320                 MPASS(p->pindex == bm->pindex + i);
1321                 bp->b_pages[i] = p;
1322         }
1323
1324         bp->b_flags |= B_PAGING;
1325         bp->b_iocmd = BIO_READ;
1326         bp->b_iodone = swp_pager_async_iodone;
1327         bp->b_rcred = crhold(thread0.td_ucred);
1328         bp->b_wcred = crhold(thread0.td_ucred);
1329         bp->b_blkno = blk;
1330         bp->b_bcount = PAGE_SIZE * count;
1331         bp->b_bufsize = PAGE_SIZE * count;
1332         bp->b_npages = count;
1333         bp->b_pgbefore = rbehind != NULL ? *rbehind : 0;
1334         bp->b_pgafter = rahead != NULL ? *rahead : 0;
1335
1336         VM_CNT_INC(v_swapin);
1337         VM_CNT_ADD(v_swappgsin, count);
1338
1339         /*
1340          * perform the I/O.  NOTE!!!  bp cannot be considered valid after
1341          * this point because we automatically release it on completion.
1342          * Instead, we look at the one page we are interested in which we
1343          * still hold a lock on even through the I/O completion.
1344          *
1345          * The other pages in our ma[] array are also released on completion,
1346          * so we cannot assume they are valid anymore either.
1347          *
1348          * NOTE: b_blkno is destroyed by the call to swapdev_strategy
1349          */
1350         BUF_KERNPROC(bp);
1351         swp_pager_strategy(bp);
1352
1353         /*
1354          * Wait for the pages we want to complete.  VPO_SWAPINPROG is always
1355          * cleared on completion.  If an I/O error occurs, SWAPBLK_NONE
1356          * is set in the metadata for each page in the request.
1357          */
1358         VM_OBJECT_WLOCK(object);
1359         /* This could be implemented more efficiently with aflags */
1360         while ((ma[0]->oflags & VPO_SWAPINPROG) != 0) {
1361                 ma[0]->oflags |= VPO_SWAPSLEEP;
1362                 VM_CNT_INC(v_intrans);
1363                 if (VM_OBJECT_SLEEP(object, &object->handle, PSWP,
1364                     "swread", hz * 20)) {
1365                         printf(
1366 "swap_pager: indefinite wait buffer: bufobj: %p, blkno: %jd, size: %ld\n",
1367                             bp->b_bufobj, (intmax_t)bp->b_blkno, bp->b_bcount);
1368                 }
1369         }
1370         VM_OBJECT_WUNLOCK(object);
1371
1372         /*
1373          * If we had an unrecoverable read error pages will not be valid.
1374          */
1375         for (i = 0; i < reqcount; i++)
1376                 if (ma[i]->valid != VM_PAGE_BITS_ALL)
1377                         return (VM_PAGER_ERROR);
1378
1379         return (VM_PAGER_OK);
1380
1381         /*
1382          * A final note: in a low swap situation, we cannot deallocate swap
1383          * and mark a page dirty here because the caller is likely to mark
1384          * the page clean when we return, causing the page to possibly revert
1385          * to all-zero's later.
1386          */
1387 }
1388
1389 static int
1390 swap_pager_getpages(vm_object_t object, vm_page_t *ma, int count,
1391     int *rbehind, int *rahead)
1392 {
1393
1394         VM_OBJECT_WLOCK(object);
1395         return (swap_pager_getpages_locked(object, ma, count, rbehind, rahead));
1396 }
1397
1398 /*
1399  *      swap_pager_getpages_async():
1400  *
1401  *      Right now this is emulation of asynchronous operation on top of
1402  *      swap_pager_getpages().
1403  */
1404 static int
1405 swap_pager_getpages_async(vm_object_t object, vm_page_t *ma, int count,
1406     int *rbehind, int *rahead, pgo_getpages_iodone_t iodone, void *arg)
1407 {
1408         int r, error;
1409
1410         r = swap_pager_getpages(object, ma, count, rbehind, rahead);
1411         switch (r) {
1412         case VM_PAGER_OK:
1413                 error = 0;
1414                 break;
1415         case VM_PAGER_ERROR:
1416                 error = EIO;
1417                 break;
1418         case VM_PAGER_FAIL:
1419                 error = EINVAL;
1420                 break;
1421         default:
1422                 panic("unhandled swap_pager_getpages() error %d", r);
1423         }
1424         (iodone)(arg, ma, count, error);
1425
1426         return (r);
1427 }
1428
1429 /*
1430  *      swap_pager_putpages:
1431  *
1432  *      Assign swap (if necessary) and initiate I/O on the specified pages.
1433  *
1434  *      We support both OBJT_DEFAULT and OBJT_SWAP objects.  DEFAULT objects
1435  *      are automatically converted to SWAP objects.
1436  *
1437  *      In a low memory situation we may block in VOP_STRATEGY(), but the new
1438  *      vm_page reservation system coupled with properly written VFS devices
1439  *      should ensure that no low-memory deadlock occurs.  This is an area
1440  *      which needs work.
1441  *
1442  *      The parent has N vm_object_pip_add() references prior to
1443  *      calling us and will remove references for rtvals[] that are
1444  *      not set to VM_PAGER_PEND.  We need to remove the rest on I/O
1445  *      completion.
1446  *
1447  *      The parent has soft-busy'd the pages it passes us and will unbusy
1448  *      those whose rtvals[] entry is not set to VM_PAGER_PEND on return.
1449  *      We need to unbusy the rest on I/O completion.
1450  */
1451 static void
1452 swap_pager_putpages(vm_object_t object, vm_page_t *ma, int count,
1453     int flags, int *rtvals)
1454 {
1455         struct buf *bp;
1456         daddr_t addr, blk, n_free, s_free;
1457         vm_page_t mreq;
1458         int i, j, n;
1459         bool async;
1460
1461         KASSERT(count == 0 || ma[0]->object == object,
1462             ("%s: object mismatch %p/%p",
1463             __func__, object, ma[0]->object));
1464
1465         /*
1466          * Step 1
1467          *
1468          * Turn object into OBJT_SWAP.  Force sync if not a pageout process.
1469          */
1470         if (object->type != OBJT_SWAP) {
1471                 addr = swp_pager_meta_build(object, 0, SWAPBLK_NONE);
1472                 KASSERT(addr == SWAPBLK_NONE,
1473                     ("unexpected object swap block"));
1474         }
1475         VM_OBJECT_WUNLOCK(object);
1476         async = curproc == pageproc && (flags & VM_PAGER_PUT_SYNC) == 0;
1477         swp_pager_init_freerange(&s_free, &n_free);
1478
1479         /*
1480          * Step 2
1481          *
1482          * Assign swap blocks and issue I/O.  We reallocate swap on the fly.
1483          * The page is left dirty until the pageout operation completes
1484          * successfully.
1485          */
1486         for (i = 0; i < count; i += n) {
1487                 /* Maximum I/O size is limited by maximum swap block size. */
1488                 n = min(count - i, nsw_cluster_max);
1489
1490                 if (async) {
1491                         mtx_lock(&swbuf_mtx);
1492                         while (nsw_wcount_async == 0)
1493                                 msleep(&nsw_wcount_async, &swbuf_mtx, PVM,
1494                                     "swbufa", 0);
1495                         nsw_wcount_async--;
1496                         mtx_unlock(&swbuf_mtx);
1497                 }
1498
1499                 /* Get a block of swap of size up to size n. */
1500                 VM_OBJECT_WLOCK(object);
1501                 blk = swp_pager_getswapspace(&n);
1502                 if (blk == SWAPBLK_NONE) {
1503                         VM_OBJECT_WUNLOCK(object);
1504                         mtx_lock(&swbuf_mtx);
1505                         if (++nsw_wcount_async == 1)
1506                                 wakeup(&nsw_wcount_async);
1507                         mtx_unlock(&swbuf_mtx);
1508                         for (j = 0; j < n; ++j)
1509                                 rtvals[i + j] = VM_PAGER_FAIL;
1510                         continue;
1511                 }
1512                 for (j = 0; j < n; ++j) {
1513                         mreq = ma[i + j];
1514                         vm_page_aflag_clear(mreq, PGA_SWAP_FREE);
1515                         addr = swp_pager_meta_build(mreq->object, mreq->pindex,
1516                             blk + j);
1517                         if (addr != SWAPBLK_NONE)
1518                                 swp_pager_update_freerange(&s_free, &n_free,
1519                                     addr);
1520                         MPASS(mreq->dirty == VM_PAGE_BITS_ALL);
1521                         mreq->oflags |= VPO_SWAPINPROG;
1522                 }
1523                 VM_OBJECT_WUNLOCK(object);
1524
1525                 bp = uma_zalloc(swwbuf_zone, M_WAITOK);
1526                 if (async)
1527                         bp->b_flags = B_ASYNC;
1528                 bp->b_flags |= B_PAGING;
1529                 bp->b_iocmd = BIO_WRITE;
1530
1531                 bp->b_rcred = crhold(thread0.td_ucred);
1532                 bp->b_wcred = crhold(thread0.td_ucred);
1533                 bp->b_bcount = PAGE_SIZE * n;
1534                 bp->b_bufsize = PAGE_SIZE * n;
1535                 bp->b_blkno = blk;
1536                 for (j = 0; j < n; j++)
1537                         bp->b_pages[j] = ma[i + j];
1538                 bp->b_npages = n;
1539
1540                 /*
1541                  * Must set dirty range for NFS to work.
1542                  */
1543                 bp->b_dirtyoff = 0;
1544                 bp->b_dirtyend = bp->b_bcount;
1545
1546                 VM_CNT_INC(v_swapout);
1547                 VM_CNT_ADD(v_swappgsout, bp->b_npages);
1548
1549                 /*
1550                  * We unconditionally set rtvals[] to VM_PAGER_PEND so that we
1551                  * can call the async completion routine at the end of a
1552                  * synchronous I/O operation.  Otherwise, our caller would
1553                  * perform duplicate unbusy and wakeup operations on the page
1554                  * and object, respectively.
1555                  */
1556                 for (j = 0; j < n; j++)
1557                         rtvals[i + j] = VM_PAGER_PEND;
1558
1559                 /*
1560                  * asynchronous
1561                  *
1562                  * NOTE: b_blkno is destroyed by the call to swapdev_strategy.
1563                  */
1564                 if (async) {
1565                         bp->b_iodone = swp_pager_async_iodone;
1566                         BUF_KERNPROC(bp);
1567                         swp_pager_strategy(bp);
1568                         continue;
1569                 }
1570
1571                 /*
1572                  * synchronous
1573                  *
1574                  * NOTE: b_blkno is destroyed by the call to swapdev_strategy.
1575                  */
1576                 bp->b_iodone = bdone;
1577                 swp_pager_strategy(bp);
1578
1579                 /*
1580                  * Wait for the sync I/O to complete.
1581                  */
1582                 bwait(bp, PVM, "swwrt");
1583
1584                 /*
1585                  * Now that we are through with the bp, we can call the
1586                  * normal async completion, which frees everything up.
1587                  */
1588                 swp_pager_async_iodone(bp);
1589         }
1590         swp_pager_freeswapspace(s_free, n_free);
1591         VM_OBJECT_WLOCK(object);
1592 }
1593
1594 /*
1595  *      swp_pager_async_iodone:
1596  *
1597  *      Completion routine for asynchronous reads and writes from/to swap.
1598  *      Also called manually by synchronous code to finish up a bp.
1599  *
1600  *      This routine may not sleep.
1601  */
1602 static void
1603 swp_pager_async_iodone(struct buf *bp)
1604 {
1605         int i;
1606         vm_object_t object = NULL;
1607
1608         /*
1609          * Report error - unless we ran out of memory, in which case
1610          * we've already logged it in swapgeom_strategy().
1611          */
1612         if (bp->b_ioflags & BIO_ERROR && bp->b_error != ENOMEM) {
1613                 printf(
1614                     "swap_pager: I/O error - %s failed; blkno %ld,"
1615                         "size %ld, error %d\n",
1616                     ((bp->b_iocmd == BIO_READ) ? "pagein" : "pageout"),
1617                     (long)bp->b_blkno,
1618                     (long)bp->b_bcount,
1619                     bp->b_error
1620                 );
1621         }
1622
1623         /*
1624          * remove the mapping for kernel virtual
1625          */
1626         if (buf_mapped(bp))
1627                 pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages);
1628         else
1629                 bp->b_data = bp->b_kvabase;
1630
1631         if (bp->b_npages) {
1632                 object = bp->b_pages[0]->object;
1633                 VM_OBJECT_WLOCK(object);
1634         }
1635
1636         /*
1637          * cleanup pages.  If an error occurs writing to swap, we are in
1638          * very serious trouble.  If it happens to be a disk error, though,
1639          * we may be able to recover by reassigning the swap later on.  So
1640          * in this case we remove the m->swapblk assignment for the page
1641          * but do not free it in the rlist.  The errornous block(s) are thus
1642          * never reallocated as swap.  Redirty the page and continue.
1643          */
1644         for (i = 0; i < bp->b_npages; ++i) {
1645                 vm_page_t m = bp->b_pages[i];
1646
1647                 m->oflags &= ~VPO_SWAPINPROG;
1648                 if (m->oflags & VPO_SWAPSLEEP) {
1649                         m->oflags &= ~VPO_SWAPSLEEP;
1650                         wakeup(&object->handle);
1651                 }
1652
1653                 /* We always have space after I/O, successful or not. */
1654                 vm_page_aflag_set(m, PGA_SWAP_SPACE);
1655
1656                 if (bp->b_ioflags & BIO_ERROR) {
1657                         /*
1658                          * If an error occurs I'd love to throw the swapblk
1659                          * away without freeing it back to swapspace, so it
1660                          * can never be used again.  But I can't from an
1661                          * interrupt.
1662                          */
1663                         if (bp->b_iocmd == BIO_READ) {
1664                                 /*
1665                                  * NOTE: for reads, m->dirty will probably
1666                                  * be overridden by the original caller of
1667                                  * getpages so don't play cute tricks here.
1668                                  */
1669                                 vm_page_invalid(m);
1670                         } else {
1671                                 /*
1672                                  * If a write error occurs, reactivate page
1673                                  * so it doesn't clog the inactive list,
1674                                  * then finish the I/O.
1675                                  */
1676                                 MPASS(m->dirty == VM_PAGE_BITS_ALL);
1677
1678                                 /* PQ_UNSWAPPABLE? */
1679                                 vm_page_activate(m);
1680                                 vm_page_sunbusy(m);
1681                         }
1682                 } else if (bp->b_iocmd == BIO_READ) {
1683                         /*
1684                          * NOTE: for reads, m->dirty will probably be
1685                          * overridden by the original caller of getpages so
1686                          * we cannot set them in order to free the underlying
1687                          * swap in a low-swap situation.  I don't think we'd
1688                          * want to do that anyway, but it was an optimization
1689                          * that existed in the old swapper for a time before
1690                          * it got ripped out due to precisely this problem.
1691                          */
1692                         KASSERT(!pmap_page_is_mapped(m),
1693                             ("swp_pager_async_iodone: page %p is mapped", m));
1694                         KASSERT(m->dirty == 0,
1695                             ("swp_pager_async_iodone: page %p is dirty", m));
1696
1697                         vm_page_valid(m);
1698                         if (i < bp->b_pgbefore ||
1699                             i >= bp->b_npages - bp->b_pgafter)
1700                                 vm_page_readahead_finish(m);
1701                 } else {
1702                         /*
1703                          * For write success, clear the dirty
1704                          * status, then finish the I/O ( which decrements the
1705                          * busy count and possibly wakes waiter's up ).
1706                          * A page is only written to swap after a period of
1707                          * inactivity.  Therefore, we do not expect it to be
1708                          * reused.
1709                          */
1710                         KASSERT(!pmap_page_is_write_mapped(m),
1711                             ("swp_pager_async_iodone: page %p is not write"
1712                             " protected", m));
1713                         vm_page_undirty(m);
1714                         vm_page_deactivate_noreuse(m);
1715                         vm_page_sunbusy(m);
1716                 }
1717         }
1718
1719         /*
1720          * adjust pip.  NOTE: the original parent may still have its own
1721          * pip refs on the object.
1722          */
1723         if (object != NULL) {
1724                 vm_object_pip_wakeupn(object, bp->b_npages);
1725                 VM_OBJECT_WUNLOCK(object);
1726         }
1727
1728         /*
1729          * swapdev_strategy() manually sets b_vp and b_bufobj before calling
1730          * bstrategy(). Set them back to NULL now we're done with it, or we'll
1731          * trigger a KASSERT in relpbuf().
1732          */
1733         if (bp->b_vp) {
1734                     bp->b_vp = NULL;
1735                     bp->b_bufobj = NULL;
1736         }
1737         /*
1738          * release the physical I/O buffer
1739          */
1740         if (bp->b_flags & B_ASYNC) {
1741                 mtx_lock(&swbuf_mtx);
1742                 if (++nsw_wcount_async == 1)
1743                         wakeup(&nsw_wcount_async);
1744                 mtx_unlock(&swbuf_mtx);
1745         }
1746         uma_zfree((bp->b_iocmd == BIO_READ) ? swrbuf_zone : swwbuf_zone, bp);
1747 }
1748
1749 int
1750 swap_pager_nswapdev(void)
1751 {
1752
1753         return (nswapdev);
1754 }
1755
1756 static void
1757 swp_pager_force_dirty(vm_page_t m)
1758 {
1759
1760         vm_page_dirty(m);
1761         swap_pager_unswapped(m);
1762         vm_page_launder(m);
1763 }
1764
1765 /*
1766  *      swap_pager_swapoff_object:
1767  *
1768  *      Page in all of the pages that have been paged out for an object
1769  *      to a swap device.
1770  */
1771 static void
1772 swap_pager_swapoff_object(struct swdevt *sp, vm_object_t object)
1773 {
1774         struct swblk *sb;
1775         vm_page_t m;
1776         vm_pindex_t pi;
1777         daddr_t blk;
1778         int i, nv, rahead, rv;
1779
1780         KASSERT(object->type == OBJT_SWAP,
1781             ("%s: Object not swappable", __func__));
1782
1783         for (pi = 0; (sb = SWAP_PCTRIE_LOOKUP_GE(
1784             &object->un_pager.swp.swp_blks, pi)) != NULL; ) {
1785                 if ((object->flags & OBJ_DEAD) != 0) {
1786                         /*
1787                          * Make sure that pending writes finish before
1788                          * returning.
1789                          */
1790                         vm_object_pip_wait(object, "swpoff");
1791                         swp_pager_meta_free_all(object);
1792                         break;
1793                 }
1794                 for (i = 0; i < SWAP_META_PAGES; i++) {
1795                         /*
1796                          * Count the number of contiguous valid blocks.
1797                          */
1798                         for (nv = 0; nv < SWAP_META_PAGES - i; nv++) {
1799                                 blk = sb->d[i + nv];
1800                                 if (!swp_pager_isondev(blk, sp) ||
1801                                     blk == SWAPBLK_NONE)
1802                                         break;
1803                         }
1804                         if (nv == 0)
1805                                 continue;
1806
1807                         /*
1808                          * Look for a page corresponding to the first
1809                          * valid block and ensure that any pending paging
1810                          * operations on it are complete.  If the page is valid,
1811                          * mark it dirty and free the swap block.  Try to batch
1812                          * this operation since it may cause sp to be freed,
1813                          * meaning that we must restart the scan.  Avoid busying
1814                          * valid pages since we may block forever on kernel
1815                          * stack pages.
1816                          */
1817                         m = vm_page_lookup(object, sb->p + i);
1818                         if (m == NULL) {
1819                                 m = vm_page_alloc(object, sb->p + i,
1820                                     VM_ALLOC_NORMAL | VM_ALLOC_WAITFAIL);
1821                                 if (m == NULL)
1822                                         break;
1823                         } else {
1824                                 if ((m->oflags & VPO_SWAPINPROG) != 0) {
1825                                         m->oflags |= VPO_SWAPSLEEP;
1826                                         VM_OBJECT_SLEEP(object, &object->handle,
1827                                             PSWP, "swpoff", 0);
1828                                         break;
1829                                 }
1830                                 if (vm_page_all_valid(m)) {
1831                                         do {
1832                                                 swp_pager_force_dirty(m);
1833                                         } while (--nv > 0 &&
1834                                             (m = vm_page_next(m)) != NULL &&
1835                                             vm_page_all_valid(m) &&
1836                                             (m->oflags & VPO_SWAPINPROG) == 0);
1837                                         break;
1838                                 }
1839                                 if (!vm_page_busy_acquire(m, VM_ALLOC_WAITFAIL))
1840                                         break;
1841                         }
1842
1843                         vm_object_pip_add(object, 1);
1844                         rahead = SWAP_META_PAGES;
1845                         rv = swap_pager_getpages_locked(object, &m, 1, NULL,
1846                             &rahead);
1847                         if (rv != VM_PAGER_OK)
1848                                 panic("%s: read from swap failed: %d",
1849                                     __func__, rv);
1850                         vm_object_pip_wakeupn(object, 1);
1851                         VM_OBJECT_WLOCK(object);
1852                         vm_page_xunbusy(m);
1853
1854                         /*
1855                          * The object lock was dropped so we must restart the
1856                          * scan of this swap block.  Pages paged in during this
1857                          * iteration will be marked dirty in a future iteration.
1858                          */
1859                         break;
1860                 }
1861                 if (i == SWAP_META_PAGES)
1862                         pi = sb->p + SWAP_META_PAGES;
1863         }
1864 }
1865
1866 /*
1867  *      swap_pager_swapoff:
1868  *
1869  *      Page in all of the pages that have been paged out to the
1870  *      given device.  The corresponding blocks in the bitmap must be
1871  *      marked as allocated and the device must be flagged SW_CLOSING.
1872  *      There may be no processes swapped out to the device.
1873  *
1874  *      This routine may block.
1875  */
1876 static void
1877 swap_pager_swapoff(struct swdevt *sp)
1878 {
1879         vm_object_t object;
1880         int retries;
1881
1882         sx_assert(&swdev_syscall_lock, SA_XLOCKED);
1883
1884         retries = 0;
1885 full_rescan:
1886         mtx_lock(&vm_object_list_mtx);
1887         TAILQ_FOREACH(object, &vm_object_list, object_list) {
1888                 if (object->type != OBJT_SWAP)
1889                         continue;
1890                 mtx_unlock(&vm_object_list_mtx);
1891                 /* Depends on type-stability. */
1892                 VM_OBJECT_WLOCK(object);
1893
1894                 /*
1895                  * Dead objects are eventually terminated on their own.
1896                  */
1897                 if ((object->flags & OBJ_DEAD) != 0)
1898                         goto next_obj;
1899
1900                 /*
1901                  * Sync with fences placed after pctrie
1902                  * initialization.  We must not access pctrie below
1903                  * unless we checked that our object is swap and not
1904                  * dead.
1905                  */
1906                 atomic_thread_fence_acq();
1907                 if (object->type != OBJT_SWAP)
1908                         goto next_obj;
1909
1910                 swap_pager_swapoff_object(sp, object);
1911 next_obj:
1912                 VM_OBJECT_WUNLOCK(object);
1913                 mtx_lock(&vm_object_list_mtx);
1914         }
1915         mtx_unlock(&vm_object_list_mtx);
1916
1917         if (sp->sw_used) {
1918                 /*
1919                  * Objects may be locked or paging to the device being
1920                  * removed, so we will miss their pages and need to
1921                  * make another pass.  We have marked this device as
1922                  * SW_CLOSING, so the activity should finish soon.
1923                  */
1924                 retries++;
1925                 if (retries > 100) {
1926                         panic("swapoff: failed to locate %d swap blocks",
1927                             sp->sw_used);
1928                 }
1929                 pause("swpoff", hz / 20);
1930                 goto full_rescan;
1931         }
1932         EVENTHANDLER_INVOKE(swapoff, sp);
1933 }
1934
1935 /************************************************************************
1936  *                              SWAP META DATA                          *
1937  ************************************************************************
1938  *
1939  *      These routines manipulate the swap metadata stored in the
1940  *      OBJT_SWAP object.
1941  *
1942  *      Swap metadata is implemented with a global hash and not directly
1943  *      linked into the object.  Instead the object simply contains
1944  *      appropriate tracking counters.
1945  */
1946
1947 /*
1948  * SWP_PAGER_SWBLK_EMPTY() - is a range of blocks free?
1949  */
1950 static bool
1951 swp_pager_swblk_empty(struct swblk *sb, int start, int limit)
1952 {
1953         int i;
1954
1955         MPASS(0 <= start && start <= limit && limit <= SWAP_META_PAGES);
1956         for (i = start; i < limit; i++) {
1957                 if (sb->d[i] != SWAPBLK_NONE)
1958                         return (false);
1959         }
1960         return (true);
1961 }
1962
1963 /*
1964  * SWP_PAGER_FREE_EMPTY_SWBLK() - frees if a block is free
1965  *
1966  *  Nothing is done if the block is still in use.
1967  */
1968 static void
1969 swp_pager_free_empty_swblk(vm_object_t object, struct swblk *sb)
1970 {
1971
1972         if (swp_pager_swblk_empty(sb, 0, SWAP_META_PAGES)) {
1973                 SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, sb->p);
1974                 uma_zfree(swblk_zone, sb);
1975         }
1976 }
1977    
1978 /*
1979  * SWP_PAGER_META_BUILD() -     add swap block to swap meta data for object
1980  *
1981  *      We first convert the object to a swap object if it is a default
1982  *      object.
1983  *
1984  *      The specified swapblk is added to the object's swap metadata.  If
1985  *      the swapblk is not valid, it is freed instead.  Any previously
1986  *      assigned swapblk is returned.
1987  */
1988 static daddr_t
1989 swp_pager_meta_build(vm_object_t object, vm_pindex_t pindex, daddr_t swapblk)
1990 {
1991         static volatile int swblk_zone_exhausted, swpctrie_zone_exhausted;
1992         struct swblk *sb, *sb1;
1993         vm_pindex_t modpi, rdpi;
1994         daddr_t prev_swapblk;
1995         int error, i;
1996
1997         VM_OBJECT_ASSERT_WLOCKED(object);
1998
1999         /*
2000          * Convert default object to swap object if necessary
2001          */
2002         if (object->type != OBJT_SWAP) {
2003                 pctrie_init(&object->un_pager.swp.swp_blks);
2004
2005                 /*
2006                  * Ensure that swap_pager_swapoff()'s iteration over
2007                  * object_list does not see a garbage pctrie.
2008                  */
2009                 atomic_thread_fence_rel();
2010
2011                 object->type = OBJT_SWAP;
2012                 object->un_pager.swp.writemappings = 0;
2013                 KASSERT((object->flags & OBJ_ANON) != 0 ||
2014                     object->handle == NULL,
2015                     ("default pager %p with handle %p",
2016                     object, object->handle));
2017         }
2018
2019         rdpi = rounddown(pindex, SWAP_META_PAGES);
2020         sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, rdpi);
2021         if (sb == NULL) {
2022                 if (swapblk == SWAPBLK_NONE)
2023                         return (SWAPBLK_NONE);
2024                 for (;;) {
2025                         sb = uma_zalloc(swblk_zone, M_NOWAIT | (curproc ==
2026                             pageproc ? M_USE_RESERVE : 0));
2027                         if (sb != NULL) {
2028                                 sb->p = rdpi;
2029                                 for (i = 0; i < SWAP_META_PAGES; i++)
2030                                         sb->d[i] = SWAPBLK_NONE;
2031                                 if (atomic_cmpset_int(&swblk_zone_exhausted,
2032                                     1, 0))
2033                                         printf("swblk zone ok\n");
2034                                 break;
2035                         }
2036                         VM_OBJECT_WUNLOCK(object);
2037                         if (uma_zone_exhausted(swblk_zone)) {
2038                                 if (atomic_cmpset_int(&swblk_zone_exhausted,
2039                                     0, 1))
2040                                         printf("swap blk zone exhausted, "
2041                                             "increase kern.maxswzone\n");
2042                                 vm_pageout_oom(VM_OOM_SWAPZ);
2043                                 pause("swzonxb", 10);
2044                         } else
2045                                 uma_zwait(swblk_zone);
2046                         VM_OBJECT_WLOCK(object);
2047                         sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks,
2048                             rdpi);
2049                         if (sb != NULL)
2050                                 /*
2051                                  * Somebody swapped out a nearby page,
2052                                  * allocating swblk at the rdpi index,
2053                                  * while we dropped the object lock.
2054                                  */
2055                                 goto allocated;
2056                 }
2057                 for (;;) {
2058                         error = SWAP_PCTRIE_INSERT(
2059                             &object->un_pager.swp.swp_blks, sb);
2060                         if (error == 0) {
2061                                 if (atomic_cmpset_int(&swpctrie_zone_exhausted,
2062                                     1, 0))
2063                                         printf("swpctrie zone ok\n");
2064                                 break;
2065                         }
2066                         VM_OBJECT_WUNLOCK(object);
2067                         if (uma_zone_exhausted(swpctrie_zone)) {
2068                                 if (atomic_cmpset_int(&swpctrie_zone_exhausted,
2069                                     0, 1))
2070                                         printf("swap pctrie zone exhausted, "
2071                                             "increase kern.maxswzone\n");
2072                                 vm_pageout_oom(VM_OOM_SWAPZ);
2073                                 pause("swzonxp", 10);
2074                         } else
2075                                 uma_zwait(swpctrie_zone);
2076                         VM_OBJECT_WLOCK(object);
2077                         sb1 = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks,
2078                             rdpi);
2079                         if (sb1 != NULL) {
2080                                 uma_zfree(swblk_zone, sb);
2081                                 sb = sb1;
2082                                 goto allocated;
2083                         }
2084                 }
2085         }
2086 allocated:
2087         MPASS(sb->p == rdpi);
2088
2089         modpi = pindex % SWAP_META_PAGES;
2090         /* Return prior contents of metadata. */
2091         prev_swapblk = sb->d[modpi];
2092         /* Enter block into metadata. */
2093         sb->d[modpi] = swapblk;
2094
2095         /*
2096          * Free the swblk if we end up with the empty page run.
2097          */
2098         if (swapblk == SWAPBLK_NONE)
2099                 swp_pager_free_empty_swblk(object, sb);
2100         return (prev_swapblk);
2101 }
2102
2103 /*
2104  * SWP_PAGER_META_TRANSFER() - free a range of blocks in the srcobject's swap
2105  * metadata, or transfer it into dstobject.
2106  *
2107  *      This routine will free swap metadata structures as they are cleaned
2108  *      out.
2109  */
2110 static void
2111 swp_pager_meta_transfer(vm_object_t srcobject, vm_object_t dstobject,
2112     vm_pindex_t pindex, vm_pindex_t count)
2113 {
2114         struct swblk *sb;
2115         daddr_t n_free, s_free;
2116         vm_pindex_t offset, last;
2117         int i, limit, start;
2118
2119         VM_OBJECT_ASSERT_WLOCKED(srcobject);
2120         if (srcobject->type != OBJT_SWAP || count == 0)
2121                 return;
2122
2123         swp_pager_init_freerange(&s_free, &n_free);
2124         offset = pindex;
2125         last = pindex + count;
2126         for (;;) {
2127                 sb = SWAP_PCTRIE_LOOKUP_GE(&srcobject->un_pager.swp.swp_blks,
2128                     rounddown(pindex, SWAP_META_PAGES));
2129                 if (sb == NULL || sb->p >= last)
2130                         break;
2131                 start = pindex > sb->p ? pindex - sb->p : 0;
2132                 limit = last - sb->p < SWAP_META_PAGES ? last - sb->p :
2133                     SWAP_META_PAGES;
2134                 for (i = start; i < limit; i++) {
2135                         if (sb->d[i] == SWAPBLK_NONE)
2136                                 continue;
2137                         if (dstobject == NULL ||
2138                             !swp_pager_xfer_source(srcobject, dstobject, 
2139                             sb->p + i - offset, sb->d[i])) {
2140                                 swp_pager_update_freerange(&s_free, &n_free,
2141                                     sb->d[i]);
2142                         }
2143                         sb->d[i] = SWAPBLK_NONE;
2144                 }
2145                 pindex = sb->p + SWAP_META_PAGES;
2146                 if (swp_pager_swblk_empty(sb, 0, start) &&
2147                     swp_pager_swblk_empty(sb, limit, SWAP_META_PAGES)) {
2148                         SWAP_PCTRIE_REMOVE(&srcobject->un_pager.swp.swp_blks,
2149                             sb->p);
2150                         uma_zfree(swblk_zone, sb);
2151                 }
2152         }
2153         swp_pager_freeswapspace(s_free, n_free);
2154 }
2155
2156 /*
2157  * SWP_PAGER_META_FREE() - free a range of blocks in the object's swap metadata
2158  *
2159  *      The requested range of blocks is freed, with any associated swap
2160  *      returned to the swap bitmap.
2161  *
2162  *      This routine will free swap metadata structures as they are cleaned
2163  *      out.  This routine does *NOT* operate on swap metadata associated
2164  *      with resident pages.
2165  */
2166 static void
2167 swp_pager_meta_free(vm_object_t object, vm_pindex_t pindex, vm_pindex_t count)
2168 {
2169         swp_pager_meta_transfer(object, NULL, pindex, count);
2170 }
2171
2172 /*
2173  * SWP_PAGER_META_FREE_ALL() - destroy all swap metadata associated with object
2174  *
2175  *      This routine locates and destroys all swap metadata associated with
2176  *      an object.
2177  */
2178 static void
2179 swp_pager_meta_free_all(vm_object_t object)
2180 {
2181         struct swblk *sb;
2182         daddr_t n_free, s_free;
2183         vm_pindex_t pindex;
2184         int i;
2185
2186         VM_OBJECT_ASSERT_WLOCKED(object);
2187         if (object->type != OBJT_SWAP)
2188                 return;
2189
2190         swp_pager_init_freerange(&s_free, &n_free);
2191         for (pindex = 0; (sb = SWAP_PCTRIE_LOOKUP_GE(
2192             &object->un_pager.swp.swp_blks, pindex)) != NULL;) {
2193                 pindex = sb->p + SWAP_META_PAGES;
2194                 for (i = 0; i < SWAP_META_PAGES; i++) {
2195                         if (sb->d[i] == SWAPBLK_NONE)
2196                                 continue;
2197                         swp_pager_update_freerange(&s_free, &n_free, sb->d[i]);
2198                 }
2199                 SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, sb->p);
2200                 uma_zfree(swblk_zone, sb);
2201         }
2202         swp_pager_freeswapspace(s_free, n_free);
2203 }
2204
2205 /*
2206  * SWP_PAGER_METACTL() -  misc control of swap meta data.
2207  *
2208  *      This routine is capable of looking up, or removing swapblk
2209  *      assignments in the swap meta data.  It returns the swapblk being
2210  *      looked-up, popped, or SWAPBLK_NONE if the block was invalid.
2211  *
2212  *      When acting on a busy resident page and paging is in progress, we
2213  *      have to wait until paging is complete but otherwise can act on the
2214  *      busy page.
2215  */
2216 static daddr_t
2217 swp_pager_meta_lookup(vm_object_t object, vm_pindex_t pindex)
2218 {
2219         struct swblk *sb;
2220
2221         VM_OBJECT_ASSERT_LOCKED(object);
2222
2223         /*
2224          * The meta data only exists if the object is OBJT_SWAP
2225          * and even then might not be allocated yet.
2226          */
2227         KASSERT(object->type == OBJT_SWAP,
2228             ("Lookup object not swappable"));
2229
2230         sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks,
2231             rounddown(pindex, SWAP_META_PAGES));
2232         if (sb == NULL)
2233                 return (SWAPBLK_NONE);
2234         return (sb->d[pindex % SWAP_META_PAGES]);
2235 }
2236
2237 /*
2238  * Returns the least page index which is greater than or equal to the
2239  * parameter pindex and for which there is a swap block allocated.
2240  * Returns object's size if the object's type is not swap or if there
2241  * are no allocated swap blocks for the object after the requested
2242  * pindex.
2243  */
2244 vm_pindex_t
2245 swap_pager_find_least(vm_object_t object, vm_pindex_t pindex)
2246 {
2247         struct swblk *sb;
2248         int i;
2249
2250         VM_OBJECT_ASSERT_LOCKED(object);
2251         if (object->type != OBJT_SWAP)
2252                 return (object->size);
2253
2254         sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks,
2255             rounddown(pindex, SWAP_META_PAGES));
2256         if (sb == NULL)
2257                 return (object->size);
2258         if (sb->p < pindex) {
2259                 for (i = pindex % SWAP_META_PAGES; i < SWAP_META_PAGES; i++) {
2260                         if (sb->d[i] != SWAPBLK_NONE)
2261                                 return (sb->p + i);
2262                 }
2263                 sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks,
2264                     roundup(pindex, SWAP_META_PAGES));
2265                 if (sb == NULL)
2266                         return (object->size);
2267         }
2268         for (i = 0; i < SWAP_META_PAGES; i++) {
2269                 if (sb->d[i] != SWAPBLK_NONE)
2270                         return (sb->p + i);
2271         }
2272
2273         /*
2274          * We get here if a swblk is present in the trie but it
2275          * doesn't map any blocks.
2276          */
2277         MPASS(0);
2278         return (object->size);
2279 }
2280
2281 /*
2282  * System call swapon(name) enables swapping on device name,
2283  * which must be in the swdevsw.  Return EBUSY
2284  * if already swapping on this device.
2285  */
2286 #ifndef _SYS_SYSPROTO_H_
2287 struct swapon_args {
2288         char *name;
2289 };
2290 #endif
2291
2292 /*
2293  * MPSAFE
2294  */
2295 /* ARGSUSED */
2296 int
2297 sys_swapon(struct thread *td, struct swapon_args *uap)
2298 {
2299         struct vattr attr;
2300         struct vnode *vp;
2301         struct nameidata nd;
2302         int error;
2303
2304         error = priv_check(td, PRIV_SWAPON);
2305         if (error)
2306                 return (error);
2307
2308         sx_xlock(&swdev_syscall_lock);
2309
2310         /*
2311          * Swap metadata may not fit in the KVM if we have physical
2312          * memory of >1GB.
2313          */
2314         if (swblk_zone == NULL) {
2315                 error = ENOMEM;
2316                 goto done;
2317         }
2318
2319         NDINIT(&nd, LOOKUP, ISOPEN | FOLLOW | AUDITVNODE1, UIO_USERSPACE,
2320             uap->name, td);
2321         error = namei(&nd);
2322         if (error)
2323                 goto done;
2324
2325         NDFREE(&nd, NDF_ONLY_PNBUF);
2326         vp = nd.ni_vp;
2327
2328         if (vn_isdisk(vp, &error)) {
2329                 error = swapongeom(vp);
2330         } else if (vp->v_type == VREG &&
2331             (vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 &&
2332             (error = VOP_GETATTR(vp, &attr, td->td_ucred)) == 0) {
2333                 /*
2334                  * Allow direct swapping to NFS regular files in the same
2335                  * way that nfs_mountroot() sets up diskless swapping.
2336                  */
2337                 error = swaponvp(td, vp, attr.va_size / DEV_BSIZE);
2338         }
2339
2340         if (error)
2341                 vrele(vp);
2342 done:
2343         sx_xunlock(&swdev_syscall_lock);
2344         return (error);
2345 }
2346
2347 /*
2348  * Check that the total amount of swap currently configured does not
2349  * exceed half the theoretical maximum.  If it does, print a warning
2350  * message.
2351  */
2352 static void
2353 swapon_check_swzone(void)
2354 {
2355
2356         /* recommend using no more than half that amount */
2357         if (swap_total > swap_maxpages / 2) {
2358                 printf("warning: total configured swap (%lu pages) "
2359                     "exceeds maximum recommended amount (%lu pages).\n",
2360                     swap_total, swap_maxpages / 2);
2361                 printf("warning: increase kern.maxswzone "
2362                     "or reduce amount of swap.\n");
2363         }
2364 }
2365
2366 static void
2367 swaponsomething(struct vnode *vp, void *id, u_long nblks,
2368     sw_strategy_t *strategy, sw_close_t *close, dev_t dev, int flags)
2369 {
2370         struct swdevt *sp, *tsp;
2371         daddr_t dvbase;
2372
2373         /*
2374          * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks.
2375          * First chop nblks off to page-align it, then convert.
2376          *
2377          * sw->sw_nblks is in page-sized chunks now too.
2378          */
2379         nblks &= ~(ctodb(1) - 1);
2380         nblks = dbtoc(nblks);
2381
2382         sp = malloc(sizeof *sp, M_VMPGDATA, M_WAITOK | M_ZERO);
2383         sp->sw_blist = blist_create(nblks, M_WAITOK);
2384         sp->sw_vp = vp;
2385         sp->sw_id = id;
2386         sp->sw_dev = dev;
2387         sp->sw_nblks = nblks;
2388         sp->sw_used = 0;
2389         sp->sw_strategy = strategy;
2390         sp->sw_close = close;
2391         sp->sw_flags = flags;
2392
2393         /*
2394          * Do not free the first blocks in order to avoid overwriting
2395          * any bsd label at the front of the partition
2396          */
2397         blist_free(sp->sw_blist, howmany(BBSIZE, PAGE_SIZE),
2398             nblks - howmany(BBSIZE, PAGE_SIZE));
2399
2400         dvbase = 0;
2401         mtx_lock(&sw_dev_mtx);
2402         TAILQ_FOREACH(tsp, &swtailq, sw_list) {
2403                 if (tsp->sw_end >= dvbase) {
2404                         /*
2405                          * We put one uncovered page between the devices
2406                          * in order to definitively prevent any cross-device
2407                          * I/O requests
2408                          */
2409                         dvbase = tsp->sw_end + 1;
2410                 }
2411         }
2412         sp->sw_first = dvbase;
2413         sp->sw_end = dvbase + nblks;
2414         TAILQ_INSERT_TAIL(&swtailq, sp, sw_list);
2415         nswapdev++;
2416         swap_pager_avail += nblks - howmany(BBSIZE, PAGE_SIZE);
2417         swap_total += nblks;
2418         swapon_check_swzone();
2419         swp_sizecheck();
2420         mtx_unlock(&sw_dev_mtx);
2421         EVENTHANDLER_INVOKE(swapon, sp);
2422 }
2423
2424 /*
2425  * SYSCALL: swapoff(devname)
2426  *
2427  * Disable swapping on the given device.
2428  *
2429  * XXX: Badly designed system call: it should use a device index
2430  * rather than filename as specification.  We keep sw_vp around
2431  * only to make this work.
2432  */
2433 #ifndef _SYS_SYSPROTO_H_
2434 struct swapoff_args {
2435         char *name;
2436 };
2437 #endif
2438
2439 /*
2440  * MPSAFE
2441  */
2442 /* ARGSUSED */
2443 int
2444 sys_swapoff(struct thread *td, struct swapoff_args *uap)
2445 {
2446         struct vnode *vp;
2447         struct nameidata nd;
2448         struct swdevt *sp;
2449         int error;
2450
2451         error = priv_check(td, PRIV_SWAPOFF);
2452         if (error)
2453                 return (error);
2454
2455         sx_xlock(&swdev_syscall_lock);
2456
2457         NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, UIO_USERSPACE, uap->name,
2458             td);
2459         error = namei(&nd);
2460         if (error)
2461                 goto done;
2462         NDFREE(&nd, NDF_ONLY_PNBUF);
2463         vp = nd.ni_vp;
2464
2465         mtx_lock(&sw_dev_mtx);
2466         TAILQ_FOREACH(sp, &swtailq, sw_list) {
2467                 if (sp->sw_vp == vp)
2468                         break;
2469         }
2470         mtx_unlock(&sw_dev_mtx);
2471         if (sp == NULL) {
2472                 error = EINVAL;
2473                 goto done;
2474         }
2475         error = swapoff_one(sp, td->td_ucred);
2476 done:
2477         sx_xunlock(&swdev_syscall_lock);
2478         return (error);
2479 }
2480
2481 static int
2482 swapoff_one(struct swdevt *sp, struct ucred *cred)
2483 {
2484         u_long nblks;
2485 #ifdef MAC
2486         int error;
2487 #endif
2488
2489         sx_assert(&swdev_syscall_lock, SA_XLOCKED);
2490 #ifdef MAC
2491         (void) vn_lock(sp->sw_vp, LK_EXCLUSIVE | LK_RETRY);
2492         error = mac_system_check_swapoff(cred, sp->sw_vp);
2493         (void) VOP_UNLOCK(sp->sw_vp);
2494         if (error != 0)
2495                 return (error);
2496 #endif
2497         nblks = sp->sw_nblks;
2498
2499         /*
2500          * We can turn off this swap device safely only if the
2501          * available virtual memory in the system will fit the amount
2502          * of data we will have to page back in, plus an epsilon so
2503          * the system doesn't become critically low on swap space.
2504          */
2505         if (vm_free_count() + swap_pager_avail < nblks + nswap_lowat)
2506                 return (ENOMEM);
2507
2508         /*
2509          * Prevent further allocations on this device.
2510          */
2511         mtx_lock(&sw_dev_mtx);
2512         sp->sw_flags |= SW_CLOSING;
2513         swap_pager_avail -= blist_fill(sp->sw_blist, 0, nblks);
2514         swap_total -= nblks;
2515         mtx_unlock(&sw_dev_mtx);
2516
2517         /*
2518          * Page in the contents of the device and close it.
2519          */
2520         swap_pager_swapoff(sp);
2521
2522         sp->sw_close(curthread, sp);
2523         mtx_lock(&sw_dev_mtx);
2524         sp->sw_id = NULL;
2525         TAILQ_REMOVE(&swtailq, sp, sw_list);
2526         nswapdev--;
2527         if (nswapdev == 0) {
2528                 swap_pager_full = 2;
2529                 swap_pager_almost_full = 1;
2530         }
2531         if (swdevhd == sp)
2532                 swdevhd = NULL;
2533         mtx_unlock(&sw_dev_mtx);
2534         blist_destroy(sp->sw_blist);
2535         free(sp, M_VMPGDATA);
2536         return (0);
2537 }
2538
2539 void
2540 swapoff_all(void)
2541 {
2542         struct swdevt *sp, *spt;
2543         const char *devname;
2544         int error;
2545
2546         sx_xlock(&swdev_syscall_lock);
2547
2548         mtx_lock(&sw_dev_mtx);
2549         TAILQ_FOREACH_SAFE(sp, &swtailq, sw_list, spt) {
2550                 mtx_unlock(&sw_dev_mtx);
2551                 if (vn_isdisk(sp->sw_vp, NULL))
2552                         devname = devtoname(sp->sw_vp->v_rdev);
2553                 else
2554                         devname = "[file]";
2555                 error = swapoff_one(sp, thread0.td_ucred);
2556                 if (error != 0) {
2557                         printf("Cannot remove swap device %s (error=%d), "
2558                             "skipping.\n", devname, error);
2559                 } else if (bootverbose) {
2560                         printf("Swap device %s removed.\n", devname);
2561                 }
2562                 mtx_lock(&sw_dev_mtx);
2563         }
2564         mtx_unlock(&sw_dev_mtx);
2565
2566         sx_xunlock(&swdev_syscall_lock);
2567 }
2568
2569 void
2570 swap_pager_status(int *total, int *used)
2571 {
2572
2573         *total = swap_total;
2574         *used = swap_total - swap_pager_avail -
2575             nswapdev * howmany(BBSIZE, PAGE_SIZE);
2576 }
2577
2578 int
2579 swap_dev_info(int name, struct xswdev *xs, char *devname, size_t len)
2580 {
2581         struct swdevt *sp;
2582         const char *tmp_devname;
2583         int error, n;
2584
2585         n = 0;
2586         error = ENOENT;
2587         mtx_lock(&sw_dev_mtx);
2588         TAILQ_FOREACH(sp, &swtailq, sw_list) {
2589                 if (n != name) {
2590                         n++;
2591                         continue;
2592                 }
2593                 xs->xsw_version = XSWDEV_VERSION;
2594                 xs->xsw_dev = sp->sw_dev;
2595                 xs->xsw_flags = sp->sw_flags;
2596                 xs->xsw_nblks = sp->sw_nblks;
2597                 xs->xsw_used = sp->sw_used;
2598                 if (devname != NULL) {
2599                         if (vn_isdisk(sp->sw_vp, NULL))
2600                                 tmp_devname = devtoname(sp->sw_vp->v_rdev);
2601                         else
2602                                 tmp_devname = "[file]";
2603                         strncpy(devname, tmp_devname, len);
2604                 }
2605                 error = 0;
2606                 break;
2607         }
2608         mtx_unlock(&sw_dev_mtx);
2609         return (error);
2610 }
2611
2612 #if defined(COMPAT_FREEBSD11)
2613 #define XSWDEV_VERSION_11       1
2614 struct xswdev11 {
2615         u_int   xsw_version;
2616         uint32_t xsw_dev;
2617         int     xsw_flags;
2618         int     xsw_nblks;
2619         int     xsw_used;
2620 };
2621 #endif
2622
2623 #if defined(__amd64__) && defined(COMPAT_FREEBSD32)
2624 struct xswdev32 {
2625         u_int   xsw_version;
2626         u_int   xsw_dev1, xsw_dev2;
2627         int     xsw_flags;
2628         int     xsw_nblks;
2629         int     xsw_used;
2630 };
2631 #endif
2632
2633 static int
2634 sysctl_vm_swap_info(SYSCTL_HANDLER_ARGS)
2635 {
2636         struct xswdev xs;
2637 #if defined(__amd64__) && defined(COMPAT_FREEBSD32)
2638         struct xswdev32 xs32;
2639 #endif
2640 #if defined(COMPAT_FREEBSD11)
2641         struct xswdev11 xs11;
2642 #endif
2643         int error;
2644
2645         if (arg2 != 1)                  /* name length */
2646                 return (EINVAL);
2647         error = swap_dev_info(*(int *)arg1, &xs, NULL, 0);
2648         if (error != 0)
2649                 return (error);
2650 #if defined(__amd64__) && defined(COMPAT_FREEBSD32)
2651         if (req->oldlen == sizeof(xs32)) {
2652                 xs32.xsw_version = XSWDEV_VERSION;
2653                 xs32.xsw_dev1 = xs.xsw_dev;
2654                 xs32.xsw_dev2 = xs.xsw_dev >> 32;
2655                 xs32.xsw_flags = xs.xsw_flags;
2656                 xs32.xsw_nblks = xs.xsw_nblks;
2657                 xs32.xsw_used = xs.xsw_used;
2658                 error = SYSCTL_OUT(req, &xs32, sizeof(xs32));
2659                 return (error);
2660         }
2661 #endif
2662 #if defined(COMPAT_FREEBSD11)
2663         if (req->oldlen == sizeof(xs11)) {
2664                 xs11.xsw_version = XSWDEV_VERSION_11;
2665                 xs11.xsw_dev = xs.xsw_dev; /* truncation */
2666                 xs11.xsw_flags = xs.xsw_flags;
2667                 xs11.xsw_nblks = xs.xsw_nblks;
2668                 xs11.xsw_used = xs.xsw_used;
2669                 error = SYSCTL_OUT(req, &xs11, sizeof(xs11));
2670                 return (error);
2671         }
2672 #endif
2673         error = SYSCTL_OUT(req, &xs, sizeof(xs));
2674         return (error);
2675 }
2676
2677 SYSCTL_INT(_vm, OID_AUTO, nswapdev, CTLFLAG_RD, &nswapdev, 0,
2678     "Number of swap devices");
2679 SYSCTL_NODE(_vm, OID_AUTO, swap_info, CTLFLAG_RD | CTLFLAG_MPSAFE,
2680     sysctl_vm_swap_info,
2681     "Swap statistics by device");
2682
2683 /*
2684  * Count the approximate swap usage in pages for a vmspace.  The
2685  * shadowed or not yet copied on write swap blocks are not accounted.
2686  * The map must be locked.
2687  */
2688 long
2689 vmspace_swap_count(struct vmspace *vmspace)
2690 {
2691         vm_map_t map;
2692         vm_map_entry_t cur;
2693         vm_object_t object;
2694         struct swblk *sb;
2695         vm_pindex_t e, pi;
2696         long count;
2697         int i;
2698
2699         map = &vmspace->vm_map;
2700         count = 0;
2701
2702         VM_MAP_ENTRY_FOREACH(cur, map) {
2703                 if ((cur->eflags & MAP_ENTRY_IS_SUB_MAP) != 0)
2704                         continue;
2705                 object = cur->object.vm_object;
2706                 if (object == NULL || object->type != OBJT_SWAP)
2707                         continue;
2708                 VM_OBJECT_RLOCK(object);
2709                 if (object->type != OBJT_SWAP)
2710                         goto unlock;
2711                 pi = OFF_TO_IDX(cur->offset);
2712                 e = pi + OFF_TO_IDX(cur->end - cur->start);
2713                 for (;; pi = sb->p + SWAP_META_PAGES) {
2714                         sb = SWAP_PCTRIE_LOOKUP_GE(
2715                             &object->un_pager.swp.swp_blks, pi);
2716                         if (sb == NULL || sb->p >= e)
2717                                 break;
2718                         for (i = 0; i < SWAP_META_PAGES; i++) {
2719                                 if (sb->p + i < e &&
2720                                     sb->d[i] != SWAPBLK_NONE)
2721                                         count++;
2722                         }
2723                 }
2724 unlock:
2725                 VM_OBJECT_RUNLOCK(object);
2726         }
2727         return (count);
2728 }
2729
2730 /*
2731  * GEOM backend
2732  *
2733  * Swapping onto disk devices.
2734  *
2735  */
2736
2737 static g_orphan_t swapgeom_orphan;
2738
2739 static struct g_class g_swap_class = {
2740         .name = "SWAP",
2741         .version = G_VERSION,
2742         .orphan = swapgeom_orphan,
2743 };
2744
2745 DECLARE_GEOM_CLASS(g_swap_class, g_class);
2746
2747
2748 static void
2749 swapgeom_close_ev(void *arg, int flags)
2750 {
2751         struct g_consumer *cp;
2752
2753         cp = arg;
2754         g_access(cp, -1, -1, 0);
2755         g_detach(cp);
2756         g_destroy_consumer(cp);
2757 }
2758
2759 /*
2760  * Add a reference to the g_consumer for an inflight transaction.
2761  */
2762 static void
2763 swapgeom_acquire(struct g_consumer *cp)
2764 {
2765
2766         mtx_assert(&sw_dev_mtx, MA_OWNED);
2767         cp->index++;
2768 }
2769
2770 /*
2771  * Remove a reference from the g_consumer.  Post a close event if all
2772  * references go away, since the function might be called from the
2773  * biodone context.
2774  */
2775 static void
2776 swapgeom_release(struct g_consumer *cp, struct swdevt *sp)
2777 {
2778
2779         mtx_assert(&sw_dev_mtx, MA_OWNED);
2780         cp->index--;
2781         if (cp->index == 0) {
2782                 if (g_post_event(swapgeom_close_ev, cp, M_NOWAIT, NULL) == 0)
2783                         sp->sw_id = NULL;
2784         }
2785 }
2786
2787 static void
2788 swapgeom_done(struct bio *bp2)
2789 {
2790         struct swdevt *sp;
2791         struct buf *bp;
2792         struct g_consumer *cp;
2793
2794         bp = bp2->bio_caller2;
2795         cp = bp2->bio_from;
2796         bp->b_ioflags = bp2->bio_flags;
2797         if (bp2->bio_error)
2798                 bp->b_ioflags |= BIO_ERROR;
2799         bp->b_resid = bp->b_bcount - bp2->bio_completed;
2800         bp->b_error = bp2->bio_error;
2801         bp->b_caller1 = NULL;
2802         bufdone(bp);
2803         sp = bp2->bio_caller1;
2804         mtx_lock(&sw_dev_mtx);
2805         swapgeom_release(cp, sp);
2806         mtx_unlock(&sw_dev_mtx);
2807         g_destroy_bio(bp2);
2808 }
2809
2810 static void
2811 swapgeom_strategy(struct buf *bp, struct swdevt *sp)
2812 {
2813         struct bio *bio;
2814         struct g_consumer *cp;
2815
2816         mtx_lock(&sw_dev_mtx);
2817         cp = sp->sw_id;
2818         if (cp == NULL) {
2819                 mtx_unlock(&sw_dev_mtx);
2820                 bp->b_error = ENXIO;
2821                 bp->b_ioflags |= BIO_ERROR;
2822                 bufdone(bp);
2823                 return;
2824         }
2825         swapgeom_acquire(cp);
2826         mtx_unlock(&sw_dev_mtx);
2827         if (bp->b_iocmd == BIO_WRITE)
2828                 bio = g_new_bio();
2829         else
2830                 bio = g_alloc_bio();
2831         if (bio == NULL) {
2832                 mtx_lock(&sw_dev_mtx);
2833                 swapgeom_release(cp, sp);
2834                 mtx_unlock(&sw_dev_mtx);
2835                 bp->b_error = ENOMEM;
2836                 bp->b_ioflags |= BIO_ERROR;
2837                 printf("swap_pager: cannot allocate bio\n");
2838                 bufdone(bp);
2839                 return;
2840         }
2841
2842         bp->b_caller1 = bio;
2843         bio->bio_caller1 = sp;
2844         bio->bio_caller2 = bp;
2845         bio->bio_cmd = bp->b_iocmd;
2846         bio->bio_offset = (bp->b_blkno - sp->sw_first) * PAGE_SIZE;
2847         bio->bio_length = bp->b_bcount;
2848         bio->bio_done = swapgeom_done;
2849         if (!buf_mapped(bp)) {
2850                 bio->bio_ma = bp->b_pages;
2851                 bio->bio_data = unmapped_buf;
2852                 bio->bio_ma_offset = (vm_offset_t)bp->b_offset & PAGE_MASK;
2853                 bio->bio_ma_n = bp->b_npages;
2854                 bio->bio_flags |= BIO_UNMAPPED;
2855         } else {
2856                 bio->bio_data = bp->b_data;
2857                 bio->bio_ma = NULL;
2858         }
2859         g_io_request(bio, cp);
2860         return;
2861 }
2862
2863 static void
2864 swapgeom_orphan(struct g_consumer *cp)
2865 {
2866         struct swdevt *sp;
2867         int destroy;
2868
2869         mtx_lock(&sw_dev_mtx);
2870         TAILQ_FOREACH(sp, &swtailq, sw_list) {
2871                 if (sp->sw_id == cp) {
2872                         sp->sw_flags |= SW_CLOSING;
2873                         break;
2874                 }
2875         }
2876         /*
2877          * Drop reference we were created with. Do directly since we're in a
2878          * special context where we don't have to queue the call to
2879          * swapgeom_close_ev().
2880          */
2881         cp->index--;
2882         destroy = ((sp != NULL) && (cp->index == 0));
2883         if (destroy)
2884                 sp->sw_id = NULL;
2885         mtx_unlock(&sw_dev_mtx);
2886         if (destroy)
2887                 swapgeom_close_ev(cp, 0);
2888 }
2889
2890 static void
2891 swapgeom_close(struct thread *td, struct swdevt *sw)
2892 {
2893         struct g_consumer *cp;
2894
2895         mtx_lock(&sw_dev_mtx);
2896         cp = sw->sw_id;
2897         sw->sw_id = NULL;
2898         mtx_unlock(&sw_dev_mtx);
2899
2900         /*
2901          * swapgeom_close() may be called from the biodone context,
2902          * where we cannot perform topology changes.  Delegate the
2903          * work to the events thread.
2904          */
2905         if (cp != NULL)
2906                 g_waitfor_event(swapgeom_close_ev, cp, M_WAITOK, NULL);
2907 }
2908
2909 static int
2910 swapongeom_locked(struct cdev *dev, struct vnode *vp)
2911 {
2912         struct g_provider *pp;
2913         struct g_consumer *cp;
2914         static struct g_geom *gp;
2915         struct swdevt *sp;
2916         u_long nblks;
2917         int error;
2918
2919         pp = g_dev_getprovider(dev);
2920         if (pp == NULL)
2921                 return (ENODEV);
2922         mtx_lock(&sw_dev_mtx);
2923         TAILQ_FOREACH(sp, &swtailq, sw_list) {
2924                 cp = sp->sw_id;
2925                 if (cp != NULL && cp->provider == pp) {
2926                         mtx_unlock(&sw_dev_mtx);
2927                         return (EBUSY);
2928                 }
2929         }
2930         mtx_unlock(&sw_dev_mtx);
2931         if (gp == NULL)
2932                 gp = g_new_geomf(&g_swap_class, "swap");
2933         cp = g_new_consumer(gp);
2934         cp->index = 1;  /* Number of active I/Os, plus one for being active. */
2935         cp->flags |=  G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
2936         g_attach(cp, pp);
2937         /*
2938          * XXX: Every time you think you can improve the margin for
2939          * footshooting, somebody depends on the ability to do so:
2940          * savecore(8) wants to write to our swapdev so we cannot
2941          * set an exclusive count :-(
2942          */
2943         error = g_access(cp, 1, 1, 0);
2944         if (error != 0) {
2945                 g_detach(cp);
2946                 g_destroy_consumer(cp);
2947                 return (error);
2948         }
2949         nblks = pp->mediasize / DEV_BSIZE;
2950         swaponsomething(vp, cp, nblks, swapgeom_strategy,
2951             swapgeom_close, dev2udev(dev),
2952             (pp->flags & G_PF_ACCEPT_UNMAPPED) != 0 ? SW_UNMAPPED : 0);
2953         return (0);
2954 }
2955
2956 static int
2957 swapongeom(struct vnode *vp)
2958 {
2959         int error;
2960
2961         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2962         if (vp->v_type != VCHR || VN_IS_DOOMED(vp)) {
2963                 error = ENOENT;
2964         } else {
2965                 g_topology_lock();
2966                 error = swapongeom_locked(vp->v_rdev, vp);
2967                 g_topology_unlock();
2968         }
2969         VOP_UNLOCK(vp);
2970         return (error);
2971 }
2972
2973 /*
2974  * VNODE backend
2975  *
2976  * This is used mainly for network filesystem (read: probably only tested
2977  * with NFS) swapfiles.
2978  *
2979  */
2980
2981 static void
2982 swapdev_strategy(struct buf *bp, struct swdevt *sp)
2983 {
2984         struct vnode *vp2;
2985
2986         bp->b_blkno = ctodb(bp->b_blkno - sp->sw_first);
2987
2988         vp2 = sp->sw_id;
2989         vhold(vp2);
2990         if (bp->b_iocmd == BIO_WRITE) {
2991                 if (bp->b_bufobj)
2992                         bufobj_wdrop(bp->b_bufobj);
2993                 bufobj_wref(&vp2->v_bufobj);
2994         }
2995         if (bp->b_bufobj != &vp2->v_bufobj)
2996                 bp->b_bufobj = &vp2->v_bufobj;
2997         bp->b_vp = vp2;
2998         bp->b_iooffset = dbtob(bp->b_blkno);
2999         bstrategy(bp);
3000         return;
3001 }
3002
3003 static void
3004 swapdev_close(struct thread *td, struct swdevt *sp)
3005 {
3006
3007         VOP_CLOSE(sp->sw_vp, FREAD | FWRITE, td->td_ucred, td);
3008         vrele(sp->sw_vp);
3009 }
3010
3011
3012 static int
3013 swaponvp(struct thread *td, struct vnode *vp, u_long nblks)
3014 {
3015         struct swdevt *sp;
3016         int error;
3017
3018         if (nblks == 0)
3019                 return (ENXIO);
3020         mtx_lock(&sw_dev_mtx);
3021         TAILQ_FOREACH(sp, &swtailq, sw_list) {
3022                 if (sp->sw_id == vp) {
3023                         mtx_unlock(&sw_dev_mtx);
3024                         return (EBUSY);
3025                 }
3026         }
3027         mtx_unlock(&sw_dev_mtx);
3028
3029         (void) vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3030 #ifdef MAC
3031         error = mac_system_check_swapon(td->td_ucred, vp);
3032         if (error == 0)
3033 #endif
3034                 error = VOP_OPEN(vp, FREAD | FWRITE, td->td_ucred, td, NULL);
3035         (void) VOP_UNLOCK(vp);
3036         if (error)
3037                 return (error);
3038
3039         swaponsomething(vp, vp, nblks, swapdev_strategy, swapdev_close,
3040             NODEV, 0);
3041         return (0);
3042 }
3043
3044 static int
3045 sysctl_swap_async_max(SYSCTL_HANDLER_ARGS)
3046 {
3047         int error, new, n;
3048
3049         new = nsw_wcount_async_max;
3050         error = sysctl_handle_int(oidp, &new, 0, req);
3051         if (error != 0 || req->newptr == NULL)
3052                 return (error);
3053
3054         if (new > nswbuf / 2 || new < 1)
3055                 return (EINVAL);
3056
3057         mtx_lock(&swbuf_mtx);
3058         while (nsw_wcount_async_max != new) {
3059                 /*
3060                  * Adjust difference.  If the current async count is too low,
3061                  * we will need to sqeeze our update slowly in.  Sleep with a
3062                  * higher priority than getpbuf() to finish faster.
3063                  */
3064                 n = new - nsw_wcount_async_max;
3065                 if (nsw_wcount_async + n >= 0) {
3066                         nsw_wcount_async += n;
3067                         nsw_wcount_async_max += n;
3068                         wakeup(&nsw_wcount_async);
3069                 } else {
3070                         nsw_wcount_async_max -= nsw_wcount_async;
3071                         nsw_wcount_async = 0;
3072                         msleep(&nsw_wcount_async, &swbuf_mtx, PSWP,
3073                             "swpsysctl", 0);
3074                 }
3075         }
3076         mtx_unlock(&swbuf_mtx);
3077
3078         return (0);
3079 }
3080
3081 static void
3082 swap_pager_update_writecount(vm_object_t object, vm_offset_t start,
3083     vm_offset_t end)
3084 {
3085
3086         VM_OBJECT_WLOCK(object);
3087         KASSERT((object->flags & OBJ_ANON) == 0,
3088             ("Splittable object with writecount"));
3089         object->un_pager.swp.writemappings += (vm_ooffset_t)end - start;
3090         VM_OBJECT_WUNLOCK(object);
3091 }
3092
3093 static void
3094 swap_pager_release_writecount(vm_object_t object, vm_offset_t start,
3095     vm_offset_t end)
3096 {
3097
3098         VM_OBJECT_WLOCK(object);
3099         KASSERT((object->flags & OBJ_ANON) == 0,
3100             ("Splittable object with writecount"));
3101         object->un_pager.swp.writemappings -= (vm_ooffset_t)end - start;
3102         VM_OBJECT_WUNLOCK(object);
3103 }