Home | History | Annotate | Line # | Download | only in uvm
uvm_swap.c revision 1.195
      1  1.195  riastrad /*	$NetBSD: uvm_swap.c,v 1.195 2020/06/29 23:40:28 riastradh Exp $	*/
      2    1.1       mrg 
      3    1.1       mrg /*
      4  1.144       mrg  * Copyright (c) 1995, 1996, 1997, 2009 Matthew R. Green
      5    1.1       mrg  * All rights reserved.
      6    1.1       mrg  *
      7    1.1       mrg  * Redistribution and use in source and binary forms, with or without
      8    1.1       mrg  * modification, are permitted provided that the following conditions
      9    1.1       mrg  * are met:
     10    1.1       mrg  * 1. Redistributions of source code must retain the above copyright
     11    1.1       mrg  *    notice, this list of conditions and the following disclaimer.
     12    1.1       mrg  * 2. Redistributions in binary form must reproduce the above copyright
     13    1.1       mrg  *    notice, this list of conditions and the following disclaimer in the
     14    1.1       mrg  *    documentation and/or other materials provided with the distribution.
     15    1.1       mrg  *
     16    1.1       mrg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17    1.1       mrg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18    1.1       mrg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19    1.1       mrg  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20    1.1       mrg  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21    1.1       mrg  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22    1.1       mrg  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23    1.1       mrg  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24    1.1       mrg  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25    1.1       mrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26    1.1       mrg  * SUCH DAMAGE.
     27    1.3       mrg  *
     28    1.3       mrg  * from: NetBSD: vm_swap.c,v 1.52 1997/12/02 13:47:37 pk Exp
     29    1.3       mrg  * from: Id: uvm_swap.c,v 1.1.2.42 1998/02/02 20:38:06 chuck Exp
     30    1.1       mrg  */
     31   1.57     lukem 
     32   1.57     lukem #include <sys/cdefs.h>
     33  1.195  riastrad __KERNEL_RCSID(0, "$NetBSD: uvm_swap.c,v 1.195 2020/06/29 23:40:28 riastradh Exp $");
     34    1.5       mrg 
     35    1.5       mrg #include "opt_uvmhist.h"
     36   1.16       mrg #include "opt_compat_netbsd.h"
     37   1.41       chs #include "opt_ddb.h"
     38    1.1       mrg 
     39    1.1       mrg #include <sys/param.h>
     40    1.1       mrg #include <sys/systm.h>
     41  1.183       uwe #include <sys/atomic.h>
     42    1.1       mrg #include <sys/buf.h>
     43   1.89      yamt #include <sys/bufq.h>
     44   1.36       mrg #include <sys/conf.h>
     45  1.187  riastrad #include <sys/cprng.h>
     46    1.1       mrg #include <sys/proc.h>
     47    1.1       mrg #include <sys/namei.h>
     48    1.1       mrg #include <sys/disklabel.h>
     49    1.1       mrg #include <sys/errno.h>
     50    1.1       mrg #include <sys/kernel.h>
     51    1.1       mrg #include <sys/vnode.h>
     52    1.1       mrg #include <sys/file.h>
     53  1.110      yamt #include <sys/vmem.h>
     54   1.90      yamt #include <sys/blist.h>
     55    1.1       mrg #include <sys/mount.h>
     56   1.12        pk #include <sys/pool.h>
     57  1.159      para #include <sys/kmem.h>
     58    1.1       mrg #include <sys/syscallargs.h>
     59   1.17       mrg #include <sys/swap.h>
     60  1.100      elad #include <sys/kauth.h>
     61  1.125        ad #include <sys/sysctl.h>
     62  1.130   hannken #include <sys/workqueue.h>
     63    1.1       mrg 
     64    1.1       mrg #include <uvm/uvm.h>
     65    1.1       mrg 
     66    1.1       mrg #include <miscfs/specfs/specdev.h>
     67    1.1       mrg 
     68  1.194  riastrad #include <crypto/aes/aes.h>
     69  1.187  riastrad 
     70    1.1       mrg /*
     71    1.1       mrg  * uvm_swap.c: manage configuration and i/o to swap space.
     72    1.1       mrg  */
     73    1.1       mrg 
     74    1.1       mrg /*
     75    1.1       mrg  * swap space is managed in the following way:
     76   1.51       chs  *
     77    1.1       mrg  * each swap partition or file is described by a "swapdev" structure.
     78    1.1       mrg  * each "swapdev" structure contains a "swapent" structure which contains
     79    1.1       mrg  * information that is passed up to the user (via system calls).
     80    1.1       mrg  *
     81    1.1       mrg  * each swap partition is assigned a "priority" (int) which controls
     82    1.1       mrg  * swap parition usage.
     83    1.1       mrg  *
     84    1.1       mrg  * the system maintains a global data structure describing all swap
     85    1.1       mrg  * partitions/files.   there is a sorted LIST of "swappri" structures
     86    1.1       mrg  * which describe "swapdev"'s at that priority.   this LIST is headed
     87   1.51       chs  * by the "swap_priority" global var.    each "swappri" contains a
     88  1.164  christos  * TAILQ of "swapdev" structures at that priority.
     89    1.1       mrg  *
     90    1.1       mrg  * locking:
     91  1.127        ad  *  - swap_syscall_lock (krwlock_t): this lock serializes the swapctl
     92    1.1       mrg  *    system call and prevents the swap priority list from changing
     93    1.1       mrg  *    while we are in the middle of a system call (e.g. SWAP_STATS).
     94  1.127        ad  *  - uvm_swap_data_lock (kmutex_t): this lock protects all swap data
     95    1.1       mrg  *    structures including the priority list, the swapdev structures,
     96  1.110      yamt  *    and the swapmap arena.
     97    1.1       mrg  *
     98    1.1       mrg  * each swap device has the following info:
     99    1.1       mrg  *  - swap device in use (could be disabled, preventing future use)
    100    1.1       mrg  *  - swap enabled (allows new allocations on swap)
    101    1.1       mrg  *  - map info in /dev/drum
    102    1.1       mrg  *  - vnode pointer
    103    1.1       mrg  * for swap files only:
    104    1.1       mrg  *  - block size
    105    1.1       mrg  *  - max byte count in buffer
    106    1.1       mrg  *  - buffer
    107    1.1       mrg  *
    108    1.1       mrg  * userland controls and configures swap with the swapctl(2) system call.
    109    1.1       mrg  * the sys_swapctl performs the following operations:
    110    1.1       mrg  *  [1] SWAP_NSWAP: returns the number of swap devices currently configured
    111   1.51       chs  *  [2] SWAP_STATS: given a pointer to an array of swapent structures
    112    1.1       mrg  *	(passed in via "arg") of a size passed in via "misc" ... we load
    113   1.85  junyoung  *	the current swap config into the array. The actual work is done
    114  1.155     rmind  *	in the uvm_swap_stats() function.
    115    1.1       mrg  *  [3] SWAP_ON: given a pathname in arg (could be device or file) and a
    116    1.1       mrg  *	priority in "misc", start swapping on it.
    117    1.1       mrg  *  [4] SWAP_OFF: as SWAP_ON, but stops swapping to a device
    118    1.1       mrg  *  [5] SWAP_CTL: changes the priority of a swap device (new priority in
    119    1.1       mrg  *	"misc")
    120    1.1       mrg  */
    121    1.1       mrg 
    122    1.1       mrg /*
    123    1.1       mrg  * swapdev: describes a single swap partition/file
    124    1.1       mrg  *
    125    1.1       mrg  * note the following should be true:
    126    1.1       mrg  * swd_inuse <= swd_nblks  [number of blocks in use is <= total blocks]
    127    1.1       mrg  * swd_nblks <= swd_mapsize [because mapsize includes miniroot+disklabel]
    128    1.1       mrg  */
    129    1.1       mrg struct swapdev {
    130  1.144       mrg 	dev_t			swd_dev;	/* device id */
    131  1.144       mrg 	int			swd_flags;	/* flags:inuse/enable/fake */
    132  1.144       mrg 	int			swd_priority;	/* our priority */
    133  1.144       mrg 	int			swd_nblks;	/* blocks in this device */
    134   1.16       mrg 	char			*swd_path;	/* saved pathname of device */
    135   1.16       mrg 	int			swd_pathlen;	/* length of pathname */
    136   1.16       mrg 	int			swd_npages;	/* #pages we can use */
    137   1.16       mrg 	int			swd_npginuse;	/* #pages in use */
    138   1.32       chs 	int			swd_npgbad;	/* #pages bad */
    139   1.16       mrg 	int			swd_drumoffset;	/* page0 offset in drum */
    140   1.16       mrg 	int			swd_drumsize;	/* #pages in drum */
    141   1.90      yamt 	blist_t			swd_blist;	/* blist for this swapdev */
    142   1.16       mrg 	struct vnode		*swd_vp;	/* backing vnode */
    143  1.165  christos 	TAILQ_ENTRY(swapdev)	swd_next;	/* priority tailq */
    144    1.1       mrg 
    145   1.16       mrg 	int			swd_bsize;	/* blocksize (bytes) */
    146   1.16       mrg 	int			swd_maxactive;	/* max active i/o reqs */
    147   1.96      yamt 	struct bufq_state	*swd_tab;	/* buffer list */
    148   1.33   thorpej 	int			swd_active;	/* number of active buffers */
    149  1.187  riastrad 
    150  1.190  riastrad 	volatile uint32_t	*swd_encmap;	/* bitmap of encrypted slots */
    151  1.194  riastrad 	struct aesenc		swd_enckey;	/* AES key expanded for enc */
    152  1.194  riastrad 	struct aesdec		swd_deckey;	/* AES key expanded for dec */
    153  1.187  riastrad 	bool			swd_encinit;	/* true if keys initialized */
    154    1.1       mrg };
    155    1.1       mrg 
    156    1.1       mrg /*
    157    1.1       mrg  * swap device priority entry; the list is kept sorted on `spi_priority'.
    158    1.1       mrg  */
    159    1.1       mrg struct swappri {
    160    1.1       mrg 	int			spi_priority;     /* priority */
    161  1.164  christos 	TAILQ_HEAD(spi_swapdev, swapdev)	spi_swapdev;
    162  1.165  christos 	/* tailq of swapdevs at this priority */
    163    1.1       mrg 	LIST_ENTRY(swappri)	spi_swappri;      /* global list of pri's */
    164    1.1       mrg };
    165    1.1       mrg 
    166    1.1       mrg /*
    167    1.1       mrg  * The following two structures are used to keep track of data transfers
    168    1.1       mrg  * on swap devices associated with regular files.
    169    1.1       mrg  * NOTE: this code is more or less a copy of vnd.c; we use the same
    170    1.1       mrg  * structure names here to ease porting..
    171    1.1       mrg  */
    172    1.1       mrg struct vndxfer {
    173    1.1       mrg 	struct buf	*vx_bp;		/* Pointer to parent buffer */
    174    1.1       mrg 	struct swapdev	*vx_sdp;
    175    1.1       mrg 	int		vx_error;
    176    1.1       mrg 	int		vx_pending;	/* # of pending aux buffers */
    177    1.1       mrg 	int		vx_flags;
    178    1.1       mrg #define VX_BUSY		1
    179    1.1       mrg #define VX_DEAD		2
    180    1.1       mrg };
    181    1.1       mrg 
    182    1.1       mrg struct vndbuf {
    183    1.1       mrg 	struct buf	vb_buf;
    184    1.1       mrg 	struct vndxfer	*vb_xfer;
    185    1.1       mrg };
    186    1.1       mrg 
    187  1.144       mrg /*
    188   1.12        pk  * We keep a of pool vndbuf's and vndxfer structures.
    189    1.1       mrg  */
    190  1.146     pooka static struct pool vndxfer_pool, vndbuf_pool;
    191    1.1       mrg 
    192    1.1       mrg /*
    193    1.1       mrg  * local variables
    194    1.1       mrg  */
    195  1.110      yamt static vmem_t *swapmap;	/* controls the mapping of /dev/drum */
    196    1.1       mrg 
    197    1.1       mrg /* list of all active swap devices [by priority] */
    198    1.1       mrg LIST_HEAD(swap_priority, swappri);
    199    1.1       mrg static struct swap_priority swap_priority;
    200    1.1       mrg 
    201    1.1       mrg /* locks */
    202  1.182        ad static kmutex_t uvm_swap_data_lock __cacheline_aligned;
    203  1.117        ad static krwlock_t swap_syscall_lock;
    204    1.1       mrg 
    205  1.130   hannken /* workqueue and use counter for swap to regular files */
    206  1.130   hannken static int sw_reg_count = 0;
    207  1.130   hannken static struct workqueue *sw_reg_workqueue;
    208  1.130   hannken 
    209  1.141        ad /* tuneables */
    210  1.141        ad u_int uvm_swapisfull_factor = 99;
    211  1.189  riastrad bool uvm_swap_encrypt = false;
    212  1.141        ad 
    213    1.1       mrg /*
    214    1.1       mrg  * prototypes
    215    1.1       mrg  */
    216   1.85  junyoung static struct swapdev	*swapdrum_getsdp(int);
    217    1.1       mrg 
    218  1.120      matt static struct swapdev	*swaplist_find(struct vnode *, bool);
    219   1.85  junyoung static void		 swaplist_insert(struct swapdev *,
    220   1.85  junyoung 					 struct swappri *, int);
    221   1.85  junyoung static void		 swaplist_trim(void);
    222    1.1       mrg 
    223   1.97  christos static int swap_on(struct lwp *, struct swapdev *);
    224   1.97  christos static int swap_off(struct lwp *, struct swapdev *);
    225    1.1       mrg 
    226   1.85  junyoung static void sw_reg_strategy(struct swapdev *, struct buf *, int);
    227  1.130   hannken static void sw_reg_biodone(struct buf *);
    228  1.130   hannken static void sw_reg_iodone(struct work *wk, void *dummy);
    229   1.85  junyoung static void sw_reg_start(struct swapdev *);
    230    1.1       mrg 
    231   1.85  junyoung static int uvm_swap_io(struct vm_page **, int, int, int);
    232    1.1       mrg 
    233  1.187  riastrad static void uvm_swap_genkey(struct swapdev *);
    234  1.189  riastrad static void uvm_swap_encryptpage(struct swapdev *, void *, int);
    235  1.189  riastrad static void uvm_swap_decryptpage(struct swapdev *, void *, int);
    236  1.187  riastrad 
    237  1.190  riastrad static size_t
    238  1.190  riastrad encmap_size(size_t npages)
    239  1.190  riastrad {
    240  1.190  riastrad 	struct swapdev *sdp;
    241  1.190  riastrad 	const size_t bytesperword = sizeof(sdp->swd_encmap[0]);
    242  1.190  riastrad 	const size_t bitsperword = NBBY * bytesperword;
    243  1.190  riastrad 	const size_t nbits = npages; /* one bit for each page */
    244  1.190  riastrad 	const size_t nwords = howmany(nbits, bitsperword);
    245  1.190  riastrad 	const size_t nbytes = nwords * bytesperword;
    246  1.190  riastrad 
    247  1.190  riastrad 	return nbytes;
    248  1.190  riastrad }
    249  1.190  riastrad 
    250    1.1       mrg /*
    251    1.1       mrg  * uvm_swap_init: init the swap system data structures and locks
    252    1.1       mrg  *
    253   1.51       chs  * => called at boot time from init_main.c after the filesystems
    254    1.1       mrg  *	are brought up (which happens after uvm_init())
    255    1.1       mrg  */
    256    1.1       mrg void
    257   1.93   thorpej uvm_swap_init(void)
    258    1.1       mrg {
    259    1.1       mrg 	UVMHIST_FUNC("uvm_swap_init");
    260    1.1       mrg 
    261    1.1       mrg 	UVMHIST_CALLED(pdhist);
    262    1.1       mrg 	/*
    263    1.1       mrg 	 * first, init the swap list, its counter, and its lock.
    264    1.1       mrg 	 * then get a handle on the vnode for /dev/drum by using
    265    1.1       mrg 	 * the its dev_t number ("swapdev", from MD conf.c).
    266    1.1       mrg 	 */
    267    1.1       mrg 
    268    1.1       mrg 	LIST_INIT(&swap_priority);
    269    1.1       mrg 	uvmexp.nswapdev = 0;
    270  1.117        ad 	rw_init(&swap_syscall_lock);
    271  1.134        ad 	mutex_init(&uvm_swap_data_lock, MUTEX_DEFAULT, IPL_NONE);
    272   1.12        pk 
    273    1.1       mrg 	if (bdevvp(swapdev, &swapdev_vp))
    274  1.145       mrg 		panic("%s: can't get vnode for swap device", __func__);
    275  1.136   hannken 	if (vn_lock(swapdev_vp, LK_EXCLUSIVE | LK_RETRY))
    276  1.145       mrg 		panic("%s: can't lock swap device", __func__);
    277  1.135   hannken 	if (VOP_OPEN(swapdev_vp, FREAD | FWRITE, NOCRED))
    278  1.145       mrg 		panic("%s: can't open swap device", __func__);
    279  1.151   hannken 	VOP_UNLOCK(swapdev_vp);
    280    1.1       mrg 
    281    1.1       mrg 	/*
    282    1.1       mrg 	 * create swap block resource map to map /dev/drum.   the range
    283    1.1       mrg 	 * from 1 to INT_MAX allows 2 gigablocks of swap space.  note
    284   1.51       chs 	 * that block 0 is reserved (used to indicate an allocation
    285    1.1       mrg 	 * failure, or no allocation).
    286    1.1       mrg 	 */
    287  1.110      yamt 	swapmap = vmem_create("swapmap", 1, INT_MAX - 1, 1, NULL, NULL, NULL, 0,
    288  1.126        ad 	    VM_NOSLEEP, IPL_NONE);
    289  1.147     rmind 	if (swapmap == 0) {
    290  1.145       mrg 		panic("%s: vmem_create failed", __func__);
    291  1.147     rmind 	}
    292  1.146     pooka 
    293  1.146     pooka 	pool_init(&vndxfer_pool, sizeof(struct vndxfer), 0, 0, 0, "swp vnx",
    294  1.146     pooka 	    NULL, IPL_BIO);
    295  1.146     pooka 	pool_init(&vndbuf_pool, sizeof(struct vndbuf), 0, 0, 0, "swp vnd",
    296  1.146     pooka 	    NULL, IPL_BIO);
    297  1.147     rmind 
    298  1.147     rmind 	UVMHIST_LOG(pdhist, "<- done", 0, 0, 0, 0);
    299    1.1       mrg }
    300    1.1       mrg 
    301    1.1       mrg /*
    302    1.1       mrg  * swaplist functions: functions that operate on the list of swap
    303    1.1       mrg  * devices on the system.
    304    1.1       mrg  */
    305    1.1       mrg 
    306    1.1       mrg /*
    307    1.1       mrg  * swaplist_insert: insert swap device "sdp" into the global list
    308    1.1       mrg  *
    309  1.127        ad  * => caller must hold both swap_syscall_lock and uvm_swap_data_lock
    310  1.154     rmind  * => caller must provide a newly allocated swappri structure (we will
    311  1.154     rmind  *	FREE it if we don't need it... this it to prevent allocation
    312  1.154     rmind  *	blocking here while adding swap)
    313    1.1       mrg  */
    314    1.1       mrg static void
    315   1.93   thorpej swaplist_insert(struct swapdev *sdp, struct swappri *newspp, int priority)
    316    1.1       mrg {
    317    1.1       mrg 	struct swappri *spp, *pspp;
    318    1.1       mrg 	UVMHIST_FUNC("swaplist_insert"); UVMHIST_CALLED(pdhist);
    319    1.1       mrg 
    320  1.190  riastrad 	KASSERT(rw_write_held(&swap_syscall_lock));
    321  1.190  riastrad 	KASSERT(mutex_owned(&uvm_swap_data_lock));
    322  1.190  riastrad 
    323    1.1       mrg 	/*
    324    1.1       mrg 	 * find entry at or after which to insert the new device.
    325    1.1       mrg 	 */
    326   1.55       chs 	pspp = NULL;
    327   1.55       chs 	LIST_FOREACH(spp, &swap_priority, spi_swappri) {
    328    1.1       mrg 		if (priority <= spp->spi_priority)
    329    1.1       mrg 			break;
    330    1.1       mrg 		pspp = spp;
    331    1.1       mrg 	}
    332    1.1       mrg 
    333    1.1       mrg 	/*
    334    1.1       mrg 	 * new priority?
    335    1.1       mrg 	 */
    336    1.1       mrg 	if (spp == NULL || spp->spi_priority != priority) {
    337    1.1       mrg 		spp = newspp;  /* use newspp! */
    338  1.175  pgoyette 		UVMHIST_LOG(pdhist, "created new swappri = %jd",
    339   1.32       chs 			    priority, 0, 0, 0);
    340    1.1       mrg 
    341    1.1       mrg 		spp->spi_priority = priority;
    342  1.164  christos 		TAILQ_INIT(&spp->spi_swapdev);
    343    1.1       mrg 
    344    1.1       mrg 		if (pspp)
    345    1.1       mrg 			LIST_INSERT_AFTER(pspp, spp, spi_swappri);
    346    1.1       mrg 		else
    347    1.1       mrg 			LIST_INSERT_HEAD(&swap_priority, spp, spi_swappri);
    348    1.1       mrg 	} else {
    349    1.1       mrg 	  	/* we don't need a new priority structure, free it */
    350  1.159      para 		kmem_free(newspp, sizeof(*newspp));
    351    1.1       mrg 	}
    352    1.1       mrg 
    353    1.1       mrg 	/*
    354    1.1       mrg 	 * priority found (or created).   now insert on the priority's
    355  1.165  christos 	 * tailq list and bump the total number of swapdevs.
    356    1.1       mrg 	 */
    357    1.1       mrg 	sdp->swd_priority = priority;
    358  1.164  christos 	TAILQ_INSERT_TAIL(&spp->spi_swapdev, sdp, swd_next);
    359    1.1       mrg 	uvmexp.nswapdev++;
    360    1.1       mrg }
    361    1.1       mrg 
    362    1.1       mrg /*
    363    1.1       mrg  * swaplist_find: find and optionally remove a swap device from the
    364    1.1       mrg  *	global list.
    365    1.1       mrg  *
    366  1.127        ad  * => caller must hold both swap_syscall_lock and uvm_swap_data_lock
    367    1.1       mrg  * => we return the swapdev we found (and removed)
    368    1.1       mrg  */
    369    1.1       mrg static struct swapdev *
    370  1.119   thorpej swaplist_find(struct vnode *vp, bool remove)
    371    1.1       mrg {
    372    1.1       mrg 	struct swapdev *sdp;
    373    1.1       mrg 	struct swappri *spp;
    374    1.1       mrg 
    375  1.190  riastrad 	KASSERT(rw_lock_held(&swap_syscall_lock));
    376  1.190  riastrad 	KASSERT(remove ? rw_write_held(&swap_syscall_lock) : 1);
    377  1.190  riastrad 	KASSERT(mutex_owned(&uvm_swap_data_lock));
    378  1.190  riastrad 
    379    1.1       mrg 	/*
    380    1.1       mrg 	 * search the lists for the requested vp
    381    1.1       mrg 	 */
    382   1.55       chs 
    383   1.55       chs 	LIST_FOREACH(spp, &swap_priority, spi_swappri) {
    384  1.164  christos 		TAILQ_FOREACH(sdp, &spp->spi_swapdev, swd_next) {
    385    1.1       mrg 			if (sdp->swd_vp == vp) {
    386    1.1       mrg 				if (remove) {
    387  1.164  christos 					TAILQ_REMOVE(&spp->spi_swapdev,
    388    1.1       mrg 					    sdp, swd_next);
    389    1.1       mrg 					uvmexp.nswapdev--;
    390    1.1       mrg 				}
    391    1.1       mrg 				return(sdp);
    392    1.1       mrg 			}
    393   1.55       chs 		}
    394    1.1       mrg 	}
    395    1.1       mrg 	return (NULL);
    396    1.1       mrg }
    397    1.1       mrg 
    398  1.113      elad /*
    399    1.1       mrg  * swaplist_trim: scan priority list for empty priority entries and kill
    400    1.1       mrg  *	them.
    401    1.1       mrg  *
    402  1.127        ad  * => caller must hold both swap_syscall_lock and uvm_swap_data_lock
    403    1.1       mrg  */
    404    1.1       mrg static void
    405   1.93   thorpej swaplist_trim(void)
    406    1.1       mrg {
    407    1.1       mrg 	struct swappri *spp, *nextspp;
    408    1.1       mrg 
    409  1.190  riastrad 	KASSERT(rw_write_held(&swap_syscall_lock));
    410  1.190  riastrad 	KASSERT(mutex_owned(&uvm_swap_data_lock));
    411  1.190  riastrad 
    412  1.161     rmind 	LIST_FOREACH_SAFE(spp, &swap_priority, spi_swappri, nextspp) {
    413  1.167   mlelstv 		if (!TAILQ_EMPTY(&spp->spi_swapdev))
    414    1.1       mrg 			continue;
    415    1.1       mrg 		LIST_REMOVE(spp, spi_swappri);
    416  1.159      para 		kmem_free(spp, sizeof(*spp));
    417    1.1       mrg 	}
    418    1.1       mrg }
    419    1.1       mrg 
    420    1.1       mrg /*
    421    1.1       mrg  * swapdrum_getsdp: given a page offset in /dev/drum, convert it back
    422    1.1       mrg  *	to the "swapdev" that maps that section of the drum.
    423    1.1       mrg  *
    424    1.1       mrg  * => each swapdev takes one big contig chunk of the drum
    425  1.127        ad  * => caller must hold uvm_swap_data_lock
    426    1.1       mrg  */
    427    1.1       mrg static struct swapdev *
    428   1.93   thorpej swapdrum_getsdp(int pgno)
    429    1.1       mrg {
    430    1.1       mrg 	struct swapdev *sdp;
    431    1.1       mrg 	struct swappri *spp;
    432   1.51       chs 
    433  1.190  riastrad 	KASSERT(mutex_owned(&uvm_swap_data_lock));
    434  1.190  riastrad 
    435   1.55       chs 	LIST_FOREACH(spp, &swap_priority, spi_swappri) {
    436  1.164  christos 		TAILQ_FOREACH(sdp, &spp->spi_swapdev, swd_next) {
    437   1.48      fvdl 			if (sdp->swd_flags & SWF_FAKE)
    438   1.48      fvdl 				continue;
    439    1.1       mrg 			if (pgno >= sdp->swd_drumoffset &&
    440    1.1       mrg 			    pgno < (sdp->swd_drumoffset + sdp->swd_drumsize)) {
    441    1.1       mrg 				return sdp;
    442    1.1       mrg 			}
    443   1.48      fvdl 		}
    444   1.55       chs 	}
    445    1.1       mrg 	return NULL;
    446    1.1       mrg }
    447    1.1       mrg 
    448  1.190  riastrad /*
    449  1.190  riastrad  * swapdrum_sdp_is: true iff the swap device for pgno is sdp
    450  1.190  riastrad  *
    451  1.190  riastrad  * => for use in positive assertions only; result is not stable
    452  1.190  riastrad  */
    453  1.190  riastrad static bool __debugused
    454  1.190  riastrad swapdrum_sdp_is(int pgno, struct swapdev *sdp)
    455  1.190  riastrad {
    456  1.190  riastrad 	bool result;
    457  1.190  riastrad 
    458  1.190  riastrad 	mutex_enter(&uvm_swap_data_lock);
    459  1.190  riastrad 	result = swapdrum_getsdp(pgno) == sdp;
    460  1.190  riastrad 	mutex_exit(&uvm_swap_data_lock);
    461  1.190  riastrad 
    462  1.190  riastrad 	return result;
    463  1.190  riastrad }
    464  1.190  riastrad 
    465  1.173      maxv void swapsys_lock(krw_t op)
    466  1.173      maxv {
    467  1.173      maxv 	rw_enter(&swap_syscall_lock, op);
    468  1.173      maxv }
    469  1.173      maxv 
    470  1.173      maxv void swapsys_unlock(void)
    471  1.173      maxv {
    472  1.173      maxv 	rw_exit(&swap_syscall_lock);
    473  1.173      maxv }
    474    1.1       mrg 
    475  1.176  christos static void
    476  1.176  christos swapent_cvt(struct swapent *se, const struct swapdev *sdp, int inuse)
    477  1.176  christos {
    478  1.176  christos 	se->se_dev = sdp->swd_dev;
    479  1.176  christos 	se->se_flags = sdp->swd_flags;
    480  1.176  christos 	se->se_nblks = sdp->swd_nblks;
    481  1.176  christos 	se->se_inuse = inuse;
    482  1.176  christos 	se->se_priority = sdp->swd_priority;
    483  1.176  christos 	KASSERT(sdp->swd_pathlen < sizeof(se->se_path));
    484  1.176  christos 	strcpy(se->se_path, sdp->swd_path);
    485  1.176  christos }
    486  1.176  christos 
    487  1.180       kre int (*uvm_swap_stats13)(const struct sys_swapctl_args *, register_t *) =
    488  1.177  christos     (void *)enosys;
    489  1.177  christos int (*uvm_swap_stats50)(const struct sys_swapctl_args *, register_t *) =
    490  1.177  christos     (void *)enosys;
    491  1.176  christos 
    492    1.1       mrg /*
    493    1.1       mrg  * sys_swapctl: main entry point for swapctl(2) system call
    494    1.1       mrg  * 	[with two helper functions: swap_on and swap_off]
    495    1.1       mrg  */
    496    1.1       mrg int
    497  1.133       dsl sys_swapctl(struct lwp *l, const struct sys_swapctl_args *uap, register_t *retval)
    498    1.1       mrg {
    499  1.133       dsl 	/* {
    500    1.1       mrg 		syscallarg(int) cmd;
    501    1.1       mrg 		syscallarg(void *) arg;
    502    1.1       mrg 		syscallarg(int) misc;
    503  1.133       dsl 	} */
    504    1.1       mrg 	struct vnode *vp;
    505    1.1       mrg 	struct nameidata nd;
    506    1.1       mrg 	struct swappri *spp;
    507    1.1       mrg 	struct swapdev *sdp;
    508  1.101  christos #define SWAP_PATH_MAX (PATH_MAX + 1)
    509  1.101  christos 	char	*userpath;
    510  1.161     rmind 	size_t	len = 0;
    511  1.176  christos 	int	error;
    512    1.1       mrg 	int	priority;
    513    1.1       mrg 	UVMHIST_FUNC("sys_swapctl"); UVMHIST_CALLED(pdhist);
    514    1.1       mrg 
    515    1.1       mrg 	/*
    516    1.1       mrg 	 * we handle the non-priv NSWAP and STATS request first.
    517    1.1       mrg 	 *
    518   1.51       chs 	 * SWAP_NSWAP: return number of config'd swap devices
    519    1.1       mrg 	 * [can also be obtained with uvmexp sysctl]
    520    1.1       mrg 	 */
    521    1.1       mrg 	if (SCARG(uap, cmd) == SWAP_NSWAP) {
    522  1.161     rmind 		const int nswapdev = uvmexp.nswapdev;
    523  1.175  pgoyette 		UVMHIST_LOG(pdhist, "<- done SWAP_NSWAP=%jd", nswapdev,
    524  1.175  pgoyette 		    0, 0, 0);
    525  1.161     rmind 		*retval = nswapdev;
    526  1.161     rmind 		return 0;
    527    1.1       mrg 	}
    528    1.1       mrg 
    529  1.161     rmind 	userpath = kmem_alloc(SWAP_PATH_MAX, KM_SLEEP);
    530  1.161     rmind 
    531  1.161     rmind 	/*
    532  1.161     rmind 	 * ensure serialized syscall access by grabbing the swap_syscall_lock
    533  1.161     rmind 	 */
    534  1.161     rmind 	rw_enter(&swap_syscall_lock, RW_WRITER);
    535  1.161     rmind 
    536    1.1       mrg 	/*
    537    1.1       mrg 	 * SWAP_STATS: get stats on current # of configured swap devs
    538    1.1       mrg 	 *
    539   1.51       chs 	 * note that the swap_priority list can't change as long
    540    1.1       mrg 	 * as we are holding the swap_syscall_lock.  we don't want
    541  1.127        ad 	 * to grab the uvm_swap_data_lock because we may fault&sleep during
    542    1.1       mrg 	 * copyout() and we don't want to be holding that lock then!
    543    1.1       mrg 	 */
    544  1.176  christos 	switch (SCARG(uap, cmd)) {
    545  1.177  christos 	case SWAP_STATS13:
    546  1.177  christos 		error = (*uvm_swap_stats13)(uap, retval);
    547  1.177  christos 		goto out;
    548  1.177  christos 	case SWAP_STATS50:
    549  1.177  christos 		error = (*uvm_swap_stats50)(uap, retval);
    550  1.177  christos 		goto out;
    551  1.176  christos 	case SWAP_STATS:
    552  1.176  christos 		error = uvm_swap_stats(SCARG(uap, arg), SCARG(uap, misc),
    553  1.177  christos 		    NULL, sizeof(struct swapent), retval);
    554   1.16       mrg 		UVMHIST_LOG(pdhist, "<- done SWAP_STATS", 0, 0, 0, 0);
    555   1.16       mrg 		goto out;
    556  1.176  christos 
    557  1.176  christos 	case SWAP_GETDUMPDEV:
    558  1.176  christos 		error = copyout(&dumpdev, SCARG(uap, arg), sizeof(dumpdev));
    559   1.55       chs 		goto out;
    560  1.176  christos 	default:
    561  1.176  christos 		break;
    562   1.55       chs 	}
    563    1.1       mrg 
    564    1.1       mrg 	/*
    565    1.1       mrg 	 * all other requests require superuser privs.   verify.
    566    1.1       mrg 	 */
    567  1.106      elad 	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SWAPCTL,
    568  1.106      elad 	    0, NULL, NULL, NULL)))
    569   1.16       mrg 		goto out;
    570    1.1       mrg 
    571  1.104    martin 	if (SCARG(uap, cmd) == SWAP_DUMPOFF) {
    572  1.104    martin 		/* drop the current dump device */
    573  1.104    martin 		dumpdev = NODEV;
    574  1.138    kardel 		dumpcdev = NODEV;
    575  1.104    martin 		cpu_dumpconf();
    576  1.104    martin 		goto out;
    577  1.104    martin 	}
    578  1.104    martin 
    579    1.1       mrg 	/*
    580    1.1       mrg 	 * at this point we expect a path name in arg.   we will
    581    1.1       mrg 	 * use namei() to gain a vnode reference (vref), and lock
    582    1.1       mrg 	 * the vnode (VOP_LOCK).
    583    1.1       mrg 	 *
    584    1.1       mrg 	 * XXX: a NULL arg means use the root vnode pointer (e.g. for
    585   1.16       mrg 	 * miniroot)
    586    1.1       mrg 	 */
    587    1.1       mrg 	if (SCARG(uap, arg) == NULL) {
    588    1.1       mrg 		vp = rootvp;		/* miniroot */
    589  1.152   hannken 		vref(vp);
    590  1.152   hannken 		if (vn_lock(vp, LK_EXCLUSIVE)) {
    591  1.152   hannken 			vrele(vp);
    592   1.16       mrg 			error = EBUSY;
    593   1.16       mrg 			goto out;
    594    1.1       mrg 		}
    595   1.16       mrg 		if (SCARG(uap, cmd) == SWAP_ON &&
    596  1.101  christos 		    copystr("miniroot", userpath, SWAP_PATH_MAX, &len))
    597   1.16       mrg 			panic("swapctl: miniroot copy failed");
    598    1.1       mrg 	} else {
    599  1.153  dholland 		struct pathbuf *pb;
    600   1.16       mrg 
    601  1.153  dholland 		/*
    602  1.153  dholland 		 * This used to allow copying in one extra byte
    603  1.153  dholland 		 * (SWAP_PATH_MAX instead of PATH_MAX) for SWAP_ON.
    604  1.153  dholland 		 * This was completely pointless because if anyone
    605  1.153  dholland 		 * used that extra byte namei would fail with
    606  1.153  dholland 		 * ENAMETOOLONG anyway, so I've removed the excess
    607  1.153  dholland 		 * logic. - dholland 20100215
    608  1.153  dholland 		 */
    609  1.153  dholland 
    610  1.153  dholland 		error = pathbuf_copyin(SCARG(uap, arg), &pb);
    611  1.153  dholland 		if (error) {
    612  1.153  dholland 			goto out;
    613  1.153  dholland 		}
    614   1.16       mrg 		if (SCARG(uap, cmd) == SWAP_ON) {
    615  1.153  dholland 			/* get a copy of the string */
    616  1.153  dholland 			pathbuf_copystring(pb, userpath, SWAP_PATH_MAX);
    617  1.153  dholland 			len = strlen(userpath) + 1;
    618  1.153  dholland 		}
    619  1.153  dholland 		NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
    620  1.153  dholland 		if ((error = namei(&nd))) {
    621  1.153  dholland 			pathbuf_destroy(pb);
    622  1.153  dholland 			goto out;
    623    1.1       mrg 		}
    624    1.1       mrg 		vp = nd.ni_vp;
    625  1.153  dholland 		pathbuf_destroy(pb);
    626    1.1       mrg 	}
    627    1.1       mrg 	/* note: "vp" is referenced and locked */
    628    1.1       mrg 
    629    1.1       mrg 	error = 0;		/* assume no error */
    630    1.1       mrg 	switch(SCARG(uap, cmd)) {
    631   1.40       mrg 
    632   1.24       mrg 	case SWAP_DUMPDEV:
    633   1.24       mrg 		if (vp->v_type != VBLK) {
    634   1.24       mrg 			error = ENOTBLK;
    635   1.45        pk 			break;
    636   1.24       mrg 		}
    637  1.138    kardel 		if (bdevsw_lookup(vp->v_rdev)) {
    638  1.109       mrg 			dumpdev = vp->v_rdev;
    639  1.138    kardel 			dumpcdev = devsw_blk2chr(dumpdev);
    640  1.138    kardel 		} else
    641  1.109       mrg 			dumpdev = NODEV;
    642   1.68  drochner 		cpu_dumpconf();
    643   1.24       mrg 		break;
    644   1.24       mrg 
    645    1.1       mrg 	case SWAP_CTL:
    646    1.1       mrg 		/*
    647    1.1       mrg 		 * get new priority, remove old entry (if any) and then
    648    1.1       mrg 		 * reinsert it in the correct place.  finally, prune out
    649    1.1       mrg 		 * any empty priority structures.
    650    1.1       mrg 		 */
    651    1.1       mrg 		priority = SCARG(uap, misc);
    652  1.159      para 		spp = kmem_alloc(sizeof(*spp), KM_SLEEP);
    653  1.127        ad 		mutex_enter(&uvm_swap_data_lock);
    654  1.120      matt 		if ((sdp = swaplist_find(vp, true)) == NULL) {
    655    1.1       mrg 			error = ENOENT;
    656    1.1       mrg 		} else {
    657    1.1       mrg 			swaplist_insert(sdp, spp, priority);
    658    1.1       mrg 			swaplist_trim();
    659    1.1       mrg 		}
    660  1.127        ad 		mutex_exit(&uvm_swap_data_lock);
    661    1.1       mrg 		if (error)
    662  1.159      para 			kmem_free(spp, sizeof(*spp));
    663    1.1       mrg 		break;
    664    1.1       mrg 
    665    1.1       mrg 	case SWAP_ON:
    666   1.32       chs 
    667    1.1       mrg 		/*
    668    1.1       mrg 		 * check for duplicates.   if none found, then insert a
    669    1.1       mrg 		 * dummy entry on the list to prevent someone else from
    670    1.1       mrg 		 * trying to enable this device while we are working on
    671    1.1       mrg 		 * it.
    672    1.1       mrg 		 */
    673   1.32       chs 
    674    1.1       mrg 		priority = SCARG(uap, misc);
    675  1.160     rmind 		sdp = kmem_zalloc(sizeof(*sdp), KM_SLEEP);
    676  1.159      para 		spp = kmem_alloc(sizeof(*spp), KM_SLEEP);
    677   1.67       chs 		sdp->swd_flags = SWF_FAKE;
    678   1.67       chs 		sdp->swd_vp = vp;
    679   1.67       chs 		sdp->swd_dev = (vp->v_type == VBLK) ? vp->v_rdev : NODEV;
    680   1.96      yamt 		bufq_alloc(&sdp->swd_tab, "disksort", BUFQ_SORT_RAWBLOCK);
    681  1.127        ad 		mutex_enter(&uvm_swap_data_lock);
    682  1.120      matt 		if (swaplist_find(vp, false) != NULL) {
    683    1.1       mrg 			error = EBUSY;
    684  1.127        ad 			mutex_exit(&uvm_swap_data_lock);
    685   1.96      yamt 			bufq_free(sdp->swd_tab);
    686  1.159      para 			kmem_free(sdp, sizeof(*sdp));
    687  1.159      para 			kmem_free(spp, sizeof(*spp));
    688   1.16       mrg 			break;
    689    1.1       mrg 		}
    690    1.1       mrg 		swaplist_insert(sdp, spp, priority);
    691  1.127        ad 		mutex_exit(&uvm_swap_data_lock);
    692    1.1       mrg 
    693  1.161     rmind 		KASSERT(len > 0);
    694   1.16       mrg 		sdp->swd_pathlen = len;
    695  1.161     rmind 		sdp->swd_path = kmem_alloc(len, KM_SLEEP);
    696  1.161     rmind 		if (copystr(userpath, sdp->swd_path, len, 0) != 0)
    697   1.19        pk 			panic("swapctl: copystr");
    698   1.32       chs 
    699    1.1       mrg 		/*
    700    1.1       mrg 		 * we've now got a FAKE placeholder in the swap list.
    701    1.1       mrg 		 * now attempt to enable swap on it.  if we fail, undo
    702    1.1       mrg 		 * what we've done and kill the fake entry we just inserted.
    703    1.1       mrg 		 * if swap_on is a success, it will clear the SWF_FAKE flag
    704    1.1       mrg 		 */
    705   1.32       chs 
    706   1.97  christos 		if ((error = swap_on(l, sdp)) != 0) {
    707  1.127        ad 			mutex_enter(&uvm_swap_data_lock);
    708  1.120      matt 			(void) swaplist_find(vp, true);  /* kill fake entry */
    709    1.1       mrg 			swaplist_trim();
    710  1.127        ad 			mutex_exit(&uvm_swap_data_lock);
    711   1.96      yamt 			bufq_free(sdp->swd_tab);
    712  1.159      para 			kmem_free(sdp->swd_path, sdp->swd_pathlen);
    713  1.159      para 			kmem_free(sdp, sizeof(*sdp));
    714    1.1       mrg 			break;
    715    1.1       mrg 		}
    716    1.1       mrg 		break;
    717    1.1       mrg 
    718    1.1       mrg 	case SWAP_OFF:
    719  1.127        ad 		mutex_enter(&uvm_swap_data_lock);
    720  1.120      matt 		if ((sdp = swaplist_find(vp, false)) == NULL) {
    721  1.127        ad 			mutex_exit(&uvm_swap_data_lock);
    722    1.1       mrg 			error = ENXIO;
    723    1.1       mrg 			break;
    724    1.1       mrg 		}
    725   1.32       chs 
    726    1.1       mrg 		/*
    727    1.1       mrg 		 * If a device isn't in use or enabled, we
    728    1.1       mrg 		 * can't stop swapping from it (again).
    729    1.1       mrg 		 */
    730    1.1       mrg 		if ((sdp->swd_flags & (SWF_INUSE|SWF_ENABLE)) == 0) {
    731  1.127        ad 			mutex_exit(&uvm_swap_data_lock);
    732    1.1       mrg 			error = EBUSY;
    733   1.16       mrg 			break;
    734    1.1       mrg 		}
    735    1.1       mrg 
    736    1.1       mrg 		/*
    737   1.32       chs 		 * do the real work.
    738    1.1       mrg 		 */
    739   1.97  christos 		error = swap_off(l, sdp);
    740    1.1       mrg 		break;
    741    1.1       mrg 
    742    1.1       mrg 	default:
    743    1.1       mrg 		error = EINVAL;
    744    1.1       mrg 	}
    745    1.1       mrg 
    746    1.1       mrg 	/*
    747   1.39       chs 	 * done!  release the ref gained by namei() and unlock.
    748    1.1       mrg 	 */
    749    1.1       mrg 	vput(vp);
    750   1.16       mrg out:
    751  1.160     rmind 	rw_exit(&swap_syscall_lock);
    752  1.159      para 	kmem_free(userpath, SWAP_PATH_MAX);
    753    1.1       mrg 
    754  1.175  pgoyette 	UVMHIST_LOG(pdhist, "<- done!  error=%jd", error, 0, 0, 0);
    755    1.1       mrg 	return (error);
    756   1.61      manu }
    757   1.61      manu 
    758   1.85  junyoung /*
    759  1.155     rmind  * uvm_swap_stats: implements swapctl(SWAP_STATS). The function is kept
    760   1.85  junyoung  * away from sys_swapctl() in order to allow COMPAT_* swapctl()
    761   1.61      manu  * emulation to use it directly without going through sys_swapctl().
    762   1.61      manu  * The problem with using sys_swapctl() there is that it involves
    763   1.61      manu  * copying the swapent array to the stackgap, and this array's size
    764   1.85  junyoung  * is not known at build time. Hence it would not be possible to
    765   1.61      manu  * ensure it would fit in the stackgap in any case.
    766   1.61      manu  */
    767  1.176  christos int
    768  1.180       kre uvm_swap_stats(char *ptr, int misc,
    769  1.176  christos     void (*f)(void *, const struct swapent *), size_t len,
    770  1.176  christos     register_t *retval)
    771   1.61      manu {
    772   1.61      manu 	struct swappri *spp;
    773   1.61      manu 	struct swapdev *sdp;
    774  1.176  christos 	struct swapent sep;
    775   1.61      manu 	int count = 0;
    776  1.176  christos 	int error;
    777  1.176  christos 
    778  1.176  christos 	KASSERT(len <= sizeof(sep));
    779  1.176  christos 	if (len == 0)
    780  1.176  christos 		return ENOSYS;
    781  1.176  christos 
    782  1.176  christos 	if (misc < 0)
    783  1.176  christos 		return EINVAL;
    784  1.176  christos 
    785  1.176  christos 	if (misc == 0 || uvmexp.nswapdev == 0)
    786  1.176  christos 		return 0;
    787  1.176  christos 
    788  1.176  christos 	/* Make sure userland cannot exhaust kernel memory */
    789  1.176  christos 	if ((size_t)misc > (size_t)uvmexp.nswapdev)
    790  1.176  christos 		misc = uvmexp.nswapdev;
    791   1.61      manu 
    792  1.173      maxv 	KASSERT(rw_lock_held(&swap_syscall_lock));
    793  1.173      maxv 
    794   1.61      manu 	LIST_FOREACH(spp, &swap_priority, spi_swappri) {
    795  1.164  christos 		TAILQ_FOREACH(sdp, &spp->spi_swapdev, swd_next) {
    796  1.144       mrg 			int inuse;
    797  1.144       mrg 
    798  1.176  christos 			if (misc-- <= 0)
    799  1.161     rmind 				break;
    800  1.161     rmind 
    801  1.144       mrg 			inuse = btodb((uint64_t)sdp->swd_npginuse <<
    802   1.61      manu 			    PAGE_SHIFT);
    803   1.85  junyoung 
    804  1.178      maxv 			memset(&sep, 0, sizeof(sep));
    805  1.176  christos 			swapent_cvt(&sep, sdp, inuse);
    806  1.176  christos 			if (f)
    807  1.176  christos 				(*f)(&sep, &sep);
    808  1.176  christos 			if ((error = copyout(&sep, ptr, len)) != 0)
    809  1.176  christos 				return error;
    810  1.176  christos 			ptr += len;
    811   1.61      manu 			count++;
    812   1.61      manu 		}
    813   1.61      manu 	}
    814   1.61      manu 	*retval = count;
    815  1.176  christos 	return 0;
    816    1.1       mrg }
    817    1.1       mrg 
    818    1.1       mrg /*
    819    1.1       mrg  * swap_on: attempt to enable a swapdev for swapping.   note that the
    820    1.1       mrg  *	swapdev is already on the global list, but disabled (marked
    821    1.1       mrg  *	SWF_FAKE).
    822    1.1       mrg  *
    823    1.1       mrg  * => we avoid the start of the disk (to protect disk labels)
    824    1.1       mrg  * => we also avoid the miniroot, if we are swapping to root.
    825  1.127        ad  * => caller should leave uvm_swap_data_lock unlocked, we may lock it
    826    1.1       mrg  *	if needed.
    827    1.1       mrg  */
    828    1.1       mrg static int
    829   1.97  christos swap_on(struct lwp *l, struct swapdev *sdp)
    830    1.1       mrg {
    831    1.1       mrg 	struct vnode *vp;
    832    1.1       mrg 	int error, npages, nblocks, size;
    833    1.1       mrg 	long addr;
    834  1.157    dyoung 	vmem_addr_t result;
    835    1.1       mrg 	struct vattr va;
    836    1.1       mrg 	dev_t dev;
    837    1.1       mrg 	UVMHIST_FUNC("swap_on"); UVMHIST_CALLED(pdhist);
    838    1.1       mrg 
    839    1.1       mrg 	/*
    840    1.1       mrg 	 * we want to enable swapping on sdp.   the swd_vp contains
    841    1.1       mrg 	 * the vnode we want (locked and ref'd), and the swd_dev
    842    1.1       mrg 	 * contains the dev_t of the file, if it a block device.
    843    1.1       mrg 	 */
    844    1.1       mrg 
    845    1.1       mrg 	vp = sdp->swd_vp;
    846    1.1       mrg 	dev = sdp->swd_dev;
    847    1.1       mrg 
    848    1.1       mrg 	/*
    849    1.1       mrg 	 * open the swap file (mostly useful for block device files to
    850    1.1       mrg 	 * let device driver know what is up).
    851    1.1       mrg 	 *
    852    1.1       mrg 	 * we skip the open/close for root on swap because the root
    853    1.1       mrg 	 * has already been opened when root was mounted (mountroot).
    854    1.1       mrg 	 */
    855    1.1       mrg 	if (vp != rootvp) {
    856  1.131     pooka 		if ((error = VOP_OPEN(vp, FREAD|FWRITE, l->l_cred)))
    857    1.1       mrg 			return (error);
    858    1.1       mrg 	}
    859    1.1       mrg 
    860    1.1       mrg 	/* XXX this only works for block devices */
    861  1.175  pgoyette 	UVMHIST_LOG(pdhist, "  dev=%jd, major(dev)=%jd", dev, major(dev), 0, 0);
    862    1.1       mrg 
    863    1.1       mrg 	/*
    864    1.1       mrg 	 * we now need to determine the size of the swap area.   for
    865    1.1       mrg 	 * block specials we can call the d_psize function.
    866    1.1       mrg 	 * for normal files, we must stat [get attrs].
    867    1.1       mrg 	 *
    868    1.1       mrg 	 * we put the result in nblks.
    869    1.1       mrg 	 * for normal files, we also want the filesystem block size
    870    1.1       mrg 	 * (which we get with statfs).
    871    1.1       mrg 	 */
    872    1.1       mrg 	switch (vp->v_type) {
    873    1.1       mrg 	case VBLK:
    874  1.158       mrg 		if ((nblocks = bdev_size(dev)) == -1) {
    875    1.1       mrg 			error = ENXIO;
    876    1.1       mrg 			goto bad;
    877    1.1       mrg 		}
    878    1.1       mrg 		break;
    879    1.1       mrg 
    880    1.1       mrg 	case VREG:
    881  1.131     pooka 		if ((error = VOP_GETATTR(vp, &va, l->l_cred)))
    882    1.1       mrg 			goto bad;
    883    1.1       mrg 		nblocks = (int)btodb(va.va_size);
    884  1.149   mlelstv 		sdp->swd_bsize = 1 << vp->v_mount->mnt_fs_bshift;
    885    1.1       mrg 		/*
    886    1.1       mrg 		 * limit the max # of outstanding I/O requests we issue
    887    1.1       mrg 		 * at any one time.   take it easy on NFS servers.
    888    1.1       mrg 		 */
    889  1.150     pooka 		if (vp->v_tag == VT_NFS)
    890    1.1       mrg 			sdp->swd_maxactive = 2; /* XXX */
    891    1.1       mrg 		else
    892    1.1       mrg 			sdp->swd_maxactive = 8; /* XXX */
    893    1.1       mrg 		break;
    894    1.1       mrg 
    895    1.1       mrg 	default:
    896    1.1       mrg 		error = ENXIO;
    897    1.1       mrg 		goto bad;
    898    1.1       mrg 	}
    899    1.1       mrg 
    900    1.1       mrg 	/*
    901    1.1       mrg 	 * save nblocks in a safe place and convert to pages.
    902    1.1       mrg 	 */
    903    1.1       mrg 
    904  1.144       mrg 	sdp->swd_nblks = nblocks;
    905   1.99      matt 	npages = dbtob((uint64_t)nblocks) >> PAGE_SHIFT;
    906    1.1       mrg 
    907    1.1       mrg 	/*
    908    1.1       mrg 	 * for block special files, we want to make sure that leave
    909    1.1       mrg 	 * the disklabel and bootblocks alone, so we arrange to skip
    910   1.32       chs 	 * over them (arbitrarily choosing to skip PAGE_SIZE bytes).
    911    1.1       mrg 	 * note that because of this the "size" can be less than the
    912    1.1       mrg 	 * actual number of blocks on the device.
    913    1.1       mrg 	 */
    914    1.1       mrg 	if (vp->v_type == VBLK) {
    915    1.1       mrg 		/* we use pages 1 to (size - 1) [inclusive] */
    916    1.1       mrg 		size = npages - 1;
    917    1.1       mrg 		addr = 1;
    918    1.1       mrg 	} else {
    919    1.1       mrg 		/* we use pages 0 to (size - 1) [inclusive] */
    920    1.1       mrg 		size = npages;
    921    1.1       mrg 		addr = 0;
    922    1.1       mrg 	}
    923    1.1       mrg 
    924    1.1       mrg 	/*
    925    1.1       mrg 	 * make sure we have enough blocks for a reasonable sized swap
    926    1.1       mrg 	 * area.   we want at least one page.
    927    1.1       mrg 	 */
    928    1.1       mrg 
    929    1.1       mrg 	if (size < 1) {
    930    1.1       mrg 		UVMHIST_LOG(pdhist, "  size <= 1!!", 0, 0, 0, 0);
    931    1.1       mrg 		error = EINVAL;
    932    1.1       mrg 		goto bad;
    933    1.1       mrg 	}
    934    1.1       mrg 
    935  1.175  pgoyette 	UVMHIST_LOG(pdhist, "  dev=%jx: size=%jd addr=%jd", dev, size, addr, 0);
    936    1.1       mrg 
    937    1.1       mrg 	/*
    938    1.1       mrg 	 * now we need to allocate an extent to manage this swap device
    939    1.1       mrg 	 */
    940    1.1       mrg 
    941   1.90      yamt 	sdp->swd_blist = blist_create(npages);
    942   1.90      yamt 	/* mark all expect the `saved' region free. */
    943   1.90      yamt 	blist_free(sdp->swd_blist, addr, size);
    944    1.1       mrg 
    945    1.1       mrg 	/*
    946  1.187  riastrad 	 * allocate space to for swap encryption state and mark the
    947  1.187  riastrad 	 * keys uninitialized so we generate them lazily
    948  1.187  riastrad 	 */
    949  1.190  riastrad 	sdp->swd_encmap = kmem_zalloc(encmap_size(npages), KM_SLEEP);
    950  1.187  riastrad 	sdp->swd_encinit = false;
    951  1.187  riastrad 
    952  1.187  riastrad 	/*
    953   1.51       chs 	 * if the vnode we are swapping to is the root vnode
    954    1.1       mrg 	 * (i.e. we are swapping to the miniroot) then we want
    955   1.51       chs 	 * to make sure we don't overwrite it.   do a statfs to
    956    1.1       mrg 	 * find its size and skip over it.
    957    1.1       mrg 	 */
    958    1.1       mrg 	if (vp == rootvp) {
    959    1.1       mrg 		struct mount *mp;
    960   1.86  christos 		struct statvfs *sp;
    961    1.1       mrg 		int rootblocks, rootpages;
    962    1.1       mrg 
    963    1.1       mrg 		mp = rootvnode->v_mount;
    964    1.1       mrg 		sp = &mp->mnt_stat;
    965   1.86  christos 		rootblocks = sp->f_blocks * btodb(sp->f_frsize);
    966   1.64  fredette 		/*
    967   1.64  fredette 		 * XXX: sp->f_blocks isn't the total number of
    968   1.64  fredette 		 * blocks in the filesystem, it's the number of
    969   1.64  fredette 		 * data blocks.  so, our rootblocks almost
    970   1.85  junyoung 		 * definitely underestimates the total size
    971   1.64  fredette 		 * of the filesystem - how badly depends on the
    972   1.85  junyoung 		 * details of the filesystem type.  there isn't
    973   1.64  fredette 		 * an obvious way to deal with this cleanly
    974   1.85  junyoung 		 * and perfectly, so for now we just pad our
    975   1.64  fredette 		 * rootblocks estimate with an extra 5 percent.
    976   1.64  fredette 		 */
    977   1.64  fredette 		rootblocks += (rootblocks >> 5) +
    978   1.64  fredette 			(rootblocks >> 6) +
    979   1.64  fredette 			(rootblocks >> 7);
    980   1.20       chs 		rootpages = round_page(dbtob(rootblocks)) >> PAGE_SHIFT;
    981   1.32       chs 		if (rootpages > size)
    982    1.1       mrg 			panic("swap_on: miniroot larger than swap?");
    983    1.1       mrg 
    984   1.90      yamt 		if (rootpages != blist_fill(sdp->swd_blist, addr, rootpages)) {
    985    1.1       mrg 			panic("swap_on: unable to preserve miniroot");
    986   1.90      yamt 		}
    987    1.1       mrg 
    988   1.32       chs 		size -= rootpages;
    989    1.1       mrg 		printf("Preserved %d pages of miniroot ", rootpages);
    990   1.32       chs 		printf("leaving %d pages of swap\n", size);
    991    1.1       mrg 	}
    992    1.1       mrg 
    993   1.39       chs 	/*
    994   1.39       chs 	 * add a ref to vp to reflect usage as a swap device.
    995   1.39       chs 	 */
    996   1.39       chs 	vref(vp);
    997   1.39       chs 
    998    1.1       mrg 	/*
    999    1.1       mrg 	 * now add the new swapdev to the drum and enable.
   1000    1.1       mrg 	 */
   1001  1.157    dyoung 	error = vmem_alloc(swapmap, npages, VM_BESTFIT | VM_SLEEP, &result);
   1002  1.157    dyoung 	if (error != 0)
   1003   1.48      fvdl 		panic("swapdrum_add");
   1004  1.130   hannken 	/*
   1005  1.130   hannken 	 * If this is the first regular swap create the workqueue.
   1006  1.130   hannken 	 * => Protected by swap_syscall_lock.
   1007  1.130   hannken 	 */
   1008  1.130   hannken 	if (vp->v_type != VBLK) {
   1009  1.130   hannken 		if (sw_reg_count++ == 0) {
   1010  1.130   hannken 			KASSERT(sw_reg_workqueue == NULL);
   1011  1.130   hannken 			if (workqueue_create(&sw_reg_workqueue, "swapiod",
   1012  1.130   hannken 			    sw_reg_iodone, NULL, PRIBIO, IPL_BIO, 0) != 0)
   1013  1.145       mrg 				panic("%s: workqueue_create failed", __func__);
   1014  1.130   hannken 		}
   1015  1.130   hannken 	}
   1016   1.48      fvdl 
   1017   1.48      fvdl 	sdp->swd_drumoffset = (int)result;
   1018   1.48      fvdl 	sdp->swd_drumsize = npages;
   1019   1.48      fvdl 	sdp->swd_npages = size;
   1020  1.127        ad 	mutex_enter(&uvm_swap_data_lock);
   1021    1.1       mrg 	sdp->swd_flags &= ~SWF_FAKE;	/* going live */
   1022    1.1       mrg 	sdp->swd_flags |= (SWF_INUSE|SWF_ENABLE);
   1023   1.32       chs 	uvmexp.swpages += size;
   1024   1.81        pk 	uvmexp.swpgavail += size;
   1025  1.127        ad 	mutex_exit(&uvm_swap_data_lock);
   1026    1.1       mrg 	return (0);
   1027    1.1       mrg 
   1028    1.1       mrg 	/*
   1029   1.43       chs 	 * failure: clean up and return error.
   1030    1.1       mrg 	 */
   1031   1.43       chs 
   1032   1.43       chs bad:
   1033   1.90      yamt 	if (sdp->swd_blist) {
   1034   1.90      yamt 		blist_destroy(sdp->swd_blist);
   1035   1.43       chs 	}
   1036   1.43       chs 	if (vp != rootvp) {
   1037  1.131     pooka 		(void)VOP_CLOSE(vp, FREAD|FWRITE, l->l_cred);
   1038   1.43       chs 	}
   1039    1.1       mrg 	return (error);
   1040    1.1       mrg }
   1041    1.1       mrg 
   1042    1.1       mrg /*
   1043    1.1       mrg  * swap_off: stop swapping on swapdev
   1044    1.1       mrg  *
   1045   1.32       chs  * => swap data should be locked, we will unlock.
   1046    1.1       mrg  */
   1047    1.1       mrg static int
   1048   1.97  christos swap_off(struct lwp *l, struct swapdev *sdp)
   1049    1.1       mrg {
   1050   1.91      yamt 	int npages = sdp->swd_npages;
   1051   1.91      yamt 	int error = 0;
   1052   1.81        pk 
   1053    1.1       mrg 	UVMHIST_FUNC("swap_off"); UVMHIST_CALLED(pdhist);
   1054  1.175  pgoyette 	UVMHIST_LOG(pdhist, "  dev=%jx, npages=%jd", sdp->swd_dev,npages, 0, 0);
   1055    1.1       mrg 
   1056  1.190  riastrad 	KASSERT(rw_write_held(&swap_syscall_lock));
   1057  1.190  riastrad 	KASSERT(mutex_owned(&uvm_swap_data_lock));
   1058  1.190  riastrad 
   1059   1.32       chs 	/* disable the swap area being removed */
   1060    1.1       mrg 	sdp->swd_flags &= ~SWF_ENABLE;
   1061   1.81        pk 	uvmexp.swpgavail -= npages;
   1062  1.127        ad 	mutex_exit(&uvm_swap_data_lock);
   1063   1.32       chs 
   1064   1.32       chs 	/*
   1065   1.32       chs 	 * the idea is to find all the pages that are paged out to this
   1066   1.32       chs 	 * device, and page them all in.  in uvm, swap-backed pageable
   1067   1.32       chs 	 * memory can take two forms: aobjs and anons.  call the
   1068   1.32       chs 	 * swapoff hook for each subsystem to bring in pages.
   1069   1.32       chs 	 */
   1070    1.1       mrg 
   1071   1.32       chs 	if (uao_swap_off(sdp->swd_drumoffset,
   1072   1.32       chs 			 sdp->swd_drumoffset + sdp->swd_drumsize) ||
   1073   1.91      yamt 	    amap_swap_off(sdp->swd_drumoffset,
   1074   1.32       chs 			  sdp->swd_drumoffset + sdp->swd_drumsize)) {
   1075   1.91      yamt 		error = ENOMEM;
   1076   1.91      yamt 	} else if (sdp->swd_npginuse > sdp->swd_npgbad) {
   1077   1.91      yamt 		error = EBUSY;
   1078   1.91      yamt 	}
   1079   1.51       chs 
   1080   1.91      yamt 	if (error) {
   1081  1.127        ad 		mutex_enter(&uvm_swap_data_lock);
   1082   1.32       chs 		sdp->swd_flags |= SWF_ENABLE;
   1083   1.81        pk 		uvmexp.swpgavail += npages;
   1084  1.127        ad 		mutex_exit(&uvm_swap_data_lock);
   1085   1.91      yamt 
   1086   1.91      yamt 		return error;
   1087   1.32       chs 	}
   1088    1.1       mrg 
   1089    1.1       mrg 	/*
   1090  1.130   hannken 	 * If this is the last regular swap destroy the workqueue.
   1091  1.130   hannken 	 * => Protected by swap_syscall_lock.
   1092  1.130   hannken 	 */
   1093  1.130   hannken 	if (sdp->swd_vp->v_type != VBLK) {
   1094  1.130   hannken 		KASSERT(sw_reg_count > 0);
   1095  1.130   hannken 		KASSERT(sw_reg_workqueue != NULL);
   1096  1.130   hannken 		if (--sw_reg_count == 0) {
   1097  1.130   hannken 			workqueue_destroy(sw_reg_workqueue);
   1098  1.130   hannken 			sw_reg_workqueue = NULL;
   1099  1.130   hannken 		}
   1100  1.130   hannken 	}
   1101  1.130   hannken 
   1102  1.130   hannken 	/*
   1103   1.58     enami 	 * done with the vnode.
   1104   1.39       chs 	 * drop our ref on the vnode before calling VOP_CLOSE()
   1105   1.39       chs 	 * so that spec_close() can tell if this is the last close.
   1106    1.1       mrg 	 */
   1107   1.39       chs 	vrele(sdp->swd_vp);
   1108   1.32       chs 	if (sdp->swd_vp != rootvp) {
   1109  1.131     pooka 		(void) VOP_CLOSE(sdp->swd_vp, FREAD|FWRITE, l->l_cred);
   1110   1.32       chs 	}
   1111   1.32       chs 
   1112  1.127        ad 	mutex_enter(&uvm_swap_data_lock);
   1113   1.81        pk 	uvmexp.swpages -= npages;
   1114   1.82        pk 	uvmexp.swpginuse -= sdp->swd_npgbad;
   1115    1.1       mrg 
   1116  1.120      matt 	if (swaplist_find(sdp->swd_vp, true) == NULL)
   1117  1.145       mrg 		panic("%s: swapdev not in list", __func__);
   1118   1.32       chs 	swaplist_trim();
   1119  1.127        ad 	mutex_exit(&uvm_swap_data_lock);
   1120    1.1       mrg 
   1121   1.32       chs 	/*
   1122   1.32       chs 	 * free all resources!
   1123   1.32       chs 	 */
   1124  1.110      yamt 	vmem_free(swapmap, sdp->swd_drumoffset, sdp->swd_drumsize);
   1125   1.90      yamt 	blist_destroy(sdp->swd_blist);
   1126   1.96      yamt 	bufq_free(sdp->swd_tab);
   1127  1.190  riastrad 	kmem_free(__UNVOLATILE(sdp->swd_encmap),
   1128  1.190  riastrad 	    encmap_size(sdp->swd_drumsize));
   1129  1.187  riastrad 	explicit_memset(&sdp->swd_enckey, 0, sizeof sdp->swd_enckey);
   1130  1.187  riastrad 	explicit_memset(&sdp->swd_deckey, 0, sizeof sdp->swd_deckey);
   1131  1.159      para 	kmem_free(sdp, sizeof(*sdp));
   1132    1.1       mrg 	return (0);
   1133    1.1       mrg }
   1134    1.1       mrg 
   1135  1.164  christos void
   1136  1.164  christos uvm_swap_shutdown(struct lwp *l)
   1137  1.164  christos {
   1138  1.164  christos 	struct swapdev *sdp;
   1139  1.164  christos 	struct swappri *spp;
   1140  1.164  christos 	struct vnode *vp;
   1141  1.164  christos 	int error;
   1142  1.164  christos 
   1143  1.182        ad 	printf("turning off swap...");
   1144  1.164  christos 	rw_enter(&swap_syscall_lock, RW_WRITER);
   1145  1.164  christos 	mutex_enter(&uvm_swap_data_lock);
   1146  1.164  christos again:
   1147  1.164  christos 	LIST_FOREACH(spp, &swap_priority, spi_swappri)
   1148  1.164  christos 		TAILQ_FOREACH(sdp, &spp->spi_swapdev, swd_next) {
   1149  1.164  christos 			if (sdp->swd_flags & SWF_FAKE)
   1150  1.164  christos 				continue;
   1151  1.164  christos 			if ((sdp->swd_flags & (SWF_INUSE|SWF_ENABLE)) == 0)
   1152  1.164  christos 				continue;
   1153  1.164  christos #ifdef DEBUG
   1154  1.164  christos 			printf("\nturning off swap on %s...",
   1155  1.164  christos 			    sdp->swd_path);
   1156  1.164  christos #endif
   1157  1.164  christos 			if (vn_lock(vp = sdp->swd_vp, LK_EXCLUSIVE)) {
   1158  1.164  christos 				error = EBUSY;
   1159  1.164  christos 				vp = NULL;
   1160  1.164  christos 			} else
   1161  1.164  christos 				error = 0;
   1162  1.164  christos 			if (!error) {
   1163  1.164  christos 				error = swap_off(l, sdp);
   1164  1.164  christos 				mutex_enter(&uvm_swap_data_lock);
   1165  1.164  christos 			}
   1166  1.164  christos 			if (error) {
   1167  1.164  christos 				printf("stopping swap on %s failed "
   1168  1.164  christos 				    "with error %d\n", sdp->swd_path, error);
   1169  1.164  christos 				TAILQ_REMOVE(&spp->spi_swapdev, sdp,
   1170  1.164  christos 				    swd_next);
   1171  1.164  christos 				uvmexp.nswapdev--;
   1172  1.164  christos 				swaplist_trim();
   1173  1.164  christos 				if (vp)
   1174  1.164  christos 					vput(vp);
   1175  1.164  christos 			}
   1176  1.164  christos 			goto again;
   1177  1.164  christos 		}
   1178  1.164  christos 	printf(" done\n");
   1179  1.164  christos 	mutex_exit(&uvm_swap_data_lock);
   1180  1.164  christos 	rw_exit(&swap_syscall_lock);
   1181  1.164  christos }
   1182  1.164  christos 
   1183  1.164  christos 
   1184    1.1       mrg /*
   1185    1.1       mrg  * /dev/drum interface and i/o functions
   1186    1.1       mrg  */
   1187    1.1       mrg 
   1188    1.1       mrg /*
   1189    1.1       mrg  * swstrategy: perform I/O on the drum
   1190    1.1       mrg  *
   1191    1.1       mrg  * => we must map the i/o request from the drum to the correct swapdev.
   1192    1.1       mrg  */
   1193   1.94   thorpej static void
   1194   1.93   thorpej swstrategy(struct buf *bp)
   1195    1.1       mrg {
   1196    1.1       mrg 	struct swapdev *sdp;
   1197    1.1       mrg 	struct vnode *vp;
   1198  1.134        ad 	int pageno, bn;
   1199    1.1       mrg 	UVMHIST_FUNC("swstrategy"); UVMHIST_CALLED(pdhist);
   1200    1.1       mrg 
   1201    1.1       mrg 	/*
   1202    1.1       mrg 	 * convert block number to swapdev.   note that swapdev can't
   1203    1.1       mrg 	 * be yanked out from under us because we are holding resources
   1204    1.1       mrg 	 * in it (i.e. the blocks we are doing I/O on).
   1205    1.1       mrg 	 */
   1206   1.41       chs 	pageno = dbtob((int64_t)bp->b_blkno) >> PAGE_SHIFT;
   1207  1.127        ad 	mutex_enter(&uvm_swap_data_lock);
   1208    1.1       mrg 	sdp = swapdrum_getsdp(pageno);
   1209  1.127        ad 	mutex_exit(&uvm_swap_data_lock);
   1210    1.1       mrg 	if (sdp == NULL) {
   1211    1.1       mrg 		bp->b_error = EINVAL;
   1212  1.163  riastrad 		bp->b_resid = bp->b_bcount;
   1213    1.1       mrg 		biodone(bp);
   1214    1.1       mrg 		UVMHIST_LOG(pdhist, "  failed to get swap device", 0, 0, 0, 0);
   1215    1.1       mrg 		return;
   1216    1.1       mrg 	}
   1217    1.1       mrg 
   1218    1.1       mrg 	/*
   1219    1.1       mrg 	 * convert drum page number to block number on this swapdev.
   1220    1.1       mrg 	 */
   1221    1.1       mrg 
   1222   1.32       chs 	pageno -= sdp->swd_drumoffset;	/* page # on swapdev */
   1223   1.99      matt 	bn = btodb((uint64_t)pageno << PAGE_SHIFT); /* convert to diskblock */
   1224    1.1       mrg 
   1225  1.175  pgoyette 	UVMHIST_LOG(pdhist, "  Rd/Wr (0/1) %jd: mapoff=%jx bn=%jx bcount=%jd",
   1226  1.175  pgoyette 		((bp->b_flags & B_READ) == 0) ? 1 : 0,
   1227    1.1       mrg 		sdp->swd_drumoffset, bn, bp->b_bcount);
   1228    1.1       mrg 
   1229    1.1       mrg 	/*
   1230    1.1       mrg 	 * for block devices we finish up here.
   1231   1.32       chs 	 * for regular files we have to do more work which we delegate
   1232    1.1       mrg 	 * to sw_reg_strategy().
   1233    1.1       mrg 	 */
   1234    1.1       mrg 
   1235  1.134        ad 	vp = sdp->swd_vp;		/* swapdev vnode pointer */
   1236  1.134        ad 	switch (vp->v_type) {
   1237    1.1       mrg 	default:
   1238  1.145       mrg 		panic("%s: vnode type 0x%x", __func__, vp->v_type);
   1239   1.32       chs 
   1240    1.1       mrg 	case VBLK:
   1241    1.1       mrg 
   1242    1.1       mrg 		/*
   1243    1.1       mrg 		 * must convert "bp" from an I/O on /dev/drum to an I/O
   1244    1.1       mrg 		 * on the swapdev (sdp).
   1245    1.1       mrg 		 */
   1246    1.1       mrg 		bp->b_blkno = bn;		/* swapdev block number */
   1247    1.1       mrg 		bp->b_dev = sdp->swd_dev;	/* swapdev dev_t */
   1248    1.1       mrg 
   1249    1.1       mrg 		/*
   1250    1.1       mrg 		 * if we are doing a write, we have to redirect the i/o on
   1251    1.1       mrg 		 * drum's v_numoutput counter to the swapdevs.
   1252    1.1       mrg 		 */
   1253    1.1       mrg 		if ((bp->b_flags & B_READ) == 0) {
   1254  1.134        ad 			mutex_enter(bp->b_objlock);
   1255    1.1       mrg 			vwakeup(bp);	/* kills one 'v_numoutput' on drum */
   1256  1.134        ad 			mutex_exit(bp->b_objlock);
   1257  1.156     rmind 			mutex_enter(vp->v_interlock);
   1258  1.134        ad 			vp->v_numoutput++;	/* put it on swapdev */
   1259  1.156     rmind 			mutex_exit(vp->v_interlock);
   1260    1.1       mrg 		}
   1261    1.1       mrg 
   1262   1.41       chs 		/*
   1263    1.1       mrg 		 * finally plug in swapdev vnode and start I/O
   1264    1.1       mrg 		 */
   1265    1.1       mrg 		bp->b_vp = vp;
   1266  1.156     rmind 		bp->b_objlock = vp->v_interlock;
   1267   1.84   hannken 		VOP_STRATEGY(vp, bp);
   1268    1.1       mrg 		return;
   1269   1.32       chs 
   1270    1.1       mrg 	case VREG:
   1271    1.1       mrg 		/*
   1272   1.32       chs 		 * delegate to sw_reg_strategy function.
   1273    1.1       mrg 		 */
   1274    1.1       mrg 		sw_reg_strategy(sdp, bp, bn);
   1275    1.1       mrg 		return;
   1276    1.1       mrg 	}
   1277    1.1       mrg 	/* NOTREACHED */
   1278    1.1       mrg }
   1279    1.1       mrg 
   1280    1.1       mrg /*
   1281   1.94   thorpej  * swread: the read function for the drum (just a call to physio)
   1282   1.94   thorpej  */
   1283   1.94   thorpej /*ARGSUSED*/
   1284   1.94   thorpej static int
   1285  1.112      yamt swread(dev_t dev, struct uio *uio, int ioflag)
   1286   1.94   thorpej {
   1287   1.94   thorpej 	UVMHIST_FUNC("swread"); UVMHIST_CALLED(pdhist);
   1288   1.94   thorpej 
   1289  1.175  pgoyette 	UVMHIST_LOG(pdhist, "  dev=%jx offset=%jx", dev, uio->uio_offset, 0, 0);
   1290   1.94   thorpej 	return (physio(swstrategy, NULL, dev, B_READ, minphys, uio));
   1291   1.94   thorpej }
   1292   1.94   thorpej 
   1293   1.94   thorpej /*
   1294   1.94   thorpej  * swwrite: the write function for the drum (just a call to physio)
   1295   1.94   thorpej  */
   1296   1.94   thorpej /*ARGSUSED*/
   1297   1.94   thorpej static int
   1298  1.112      yamt swwrite(dev_t dev, struct uio *uio, int ioflag)
   1299   1.94   thorpej {
   1300   1.94   thorpej 	UVMHIST_FUNC("swwrite"); UVMHIST_CALLED(pdhist);
   1301   1.94   thorpej 
   1302  1.175  pgoyette 	UVMHIST_LOG(pdhist, "  dev=%jx offset=%jx", dev, uio->uio_offset, 0, 0);
   1303   1.94   thorpej 	return (physio(swstrategy, NULL, dev, B_WRITE, minphys, uio));
   1304   1.94   thorpej }
   1305   1.94   thorpej 
   1306   1.94   thorpej const struct bdevsw swap_bdevsw = {
   1307  1.168  dholland 	.d_open = nullopen,
   1308  1.168  dholland 	.d_close = nullclose,
   1309  1.168  dholland 	.d_strategy = swstrategy,
   1310  1.168  dholland 	.d_ioctl = noioctl,
   1311  1.168  dholland 	.d_dump = nodump,
   1312  1.168  dholland 	.d_psize = nosize,
   1313  1.171  dholland 	.d_discard = nodiscard,
   1314  1.168  dholland 	.d_flag = D_OTHER
   1315   1.94   thorpej };
   1316   1.94   thorpej 
   1317   1.94   thorpej const struct cdevsw swap_cdevsw = {
   1318  1.168  dholland 	.d_open = nullopen,
   1319  1.168  dholland 	.d_close = nullclose,
   1320  1.168  dholland 	.d_read = swread,
   1321  1.168  dholland 	.d_write = swwrite,
   1322  1.168  dholland 	.d_ioctl = noioctl,
   1323  1.168  dholland 	.d_stop = nostop,
   1324  1.168  dholland 	.d_tty = notty,
   1325  1.168  dholland 	.d_poll = nopoll,
   1326  1.168  dholland 	.d_mmap = nommap,
   1327  1.168  dholland 	.d_kqfilter = nokqfilter,
   1328  1.172  dholland 	.d_discard = nodiscard,
   1329  1.168  dholland 	.d_flag = D_OTHER,
   1330   1.94   thorpej };
   1331   1.94   thorpej 
   1332   1.94   thorpej /*
   1333    1.1       mrg  * sw_reg_strategy: handle swap i/o to regular files
   1334    1.1       mrg  */
   1335    1.1       mrg static void
   1336   1.93   thorpej sw_reg_strategy(struct swapdev *sdp, struct buf *bp, int bn)
   1337    1.1       mrg {
   1338    1.1       mrg 	struct vnode	*vp;
   1339    1.1       mrg 	struct vndxfer	*vnx;
   1340   1.44     enami 	daddr_t		nbn;
   1341  1.122  christos 	char 		*addr;
   1342   1.44     enami 	off_t		byteoff;
   1343    1.9       mrg 	int		s, off, nra, error, sz, resid;
   1344    1.1       mrg 	UVMHIST_FUNC("sw_reg_strategy"); UVMHIST_CALLED(pdhist);
   1345    1.1       mrg 
   1346    1.1       mrg 	/*
   1347    1.1       mrg 	 * allocate a vndxfer head for this transfer and point it to
   1348    1.1       mrg 	 * our buffer.
   1349    1.1       mrg 	 */
   1350  1.134        ad 	vnx = pool_get(&vndxfer_pool, PR_WAITOK);
   1351    1.1       mrg 	vnx->vx_flags = VX_BUSY;
   1352    1.1       mrg 	vnx->vx_error = 0;
   1353    1.1       mrg 	vnx->vx_pending = 0;
   1354    1.1       mrg 	vnx->vx_bp = bp;
   1355    1.1       mrg 	vnx->vx_sdp = sdp;
   1356    1.1       mrg 
   1357    1.1       mrg 	/*
   1358    1.1       mrg 	 * setup for main loop where we read filesystem blocks into
   1359    1.1       mrg 	 * our buffer.
   1360    1.1       mrg 	 */
   1361    1.1       mrg 	error = 0;
   1362  1.185   msaitoh 	bp->b_resid = bp->b_bcount;	/* nothing transferred yet! */
   1363    1.1       mrg 	addr = bp->b_data;		/* current position in buffer */
   1364   1.99      matt 	byteoff = dbtob((uint64_t)bn);
   1365    1.1       mrg 
   1366    1.1       mrg 	for (resid = bp->b_resid; resid; resid -= sz) {
   1367    1.1       mrg 		struct vndbuf	*nbp;
   1368    1.1       mrg 
   1369    1.1       mrg 		/*
   1370    1.1       mrg 		 * translate byteoffset into block number.  return values:
   1371    1.1       mrg 		 *   vp = vnode of underlying device
   1372    1.1       mrg 		 *  nbn = new block number (on underlying vnode dev)
   1373    1.1       mrg 		 *  nra = num blocks we can read-ahead (excludes requested
   1374    1.1       mrg 		 *	block)
   1375    1.1       mrg 		 */
   1376    1.1       mrg 		nra = 0;
   1377    1.1       mrg 		error = VOP_BMAP(sdp->swd_vp, byteoff / sdp->swd_bsize,
   1378    1.1       mrg 				 	&vp, &nbn, &nra);
   1379    1.1       mrg 
   1380   1.32       chs 		if (error == 0 && nbn == (daddr_t)-1) {
   1381   1.51       chs 			/*
   1382   1.23      marc 			 * this used to just set error, but that doesn't
   1383   1.23      marc 			 * do the right thing.  Instead, it causes random
   1384   1.23      marc 			 * memory errors.  The panic() should remain until
   1385   1.23      marc 			 * this condition doesn't destabilize the system.
   1386   1.23      marc 			 */
   1387   1.23      marc #if 1
   1388  1.145       mrg 			panic("%s: swap to sparse file", __func__);
   1389   1.23      marc #else
   1390    1.1       mrg 			error = EIO;	/* failure */
   1391   1.23      marc #endif
   1392   1.23      marc 		}
   1393    1.1       mrg 
   1394    1.1       mrg 		/*
   1395    1.1       mrg 		 * punt if there was an error or a hole in the file.
   1396    1.1       mrg 		 * we must wait for any i/o ops we have already started
   1397    1.1       mrg 		 * to finish before returning.
   1398    1.1       mrg 		 *
   1399    1.1       mrg 		 * XXX we could deal with holes here but it would be
   1400    1.1       mrg 		 * a hassle (in the write case).
   1401    1.1       mrg 		 */
   1402    1.1       mrg 		if (error) {
   1403    1.1       mrg 			s = splbio();
   1404    1.1       mrg 			vnx->vx_error = error;	/* pass error up */
   1405    1.1       mrg 			goto out;
   1406    1.1       mrg 		}
   1407    1.1       mrg 
   1408    1.1       mrg 		/*
   1409    1.1       mrg 		 * compute the size ("sz") of this transfer (in bytes).
   1410    1.1       mrg 		 */
   1411   1.41       chs 		off = byteoff % sdp->swd_bsize;
   1412   1.41       chs 		sz = (1 + nra) * sdp->swd_bsize - off;
   1413   1.41       chs 		if (sz > resid)
   1414    1.1       mrg 			sz = resid;
   1415    1.1       mrg 
   1416   1.41       chs 		UVMHIST_LOG(pdhist, "sw_reg_strategy: "
   1417  1.175  pgoyette 		    "vp %#jx/%#jx offset 0x%jx/0x%jx",
   1418  1.175  pgoyette 		    (uintptr_t)sdp->swd_vp, (uintptr_t)vp, byteoff, nbn);
   1419    1.1       mrg 
   1420    1.1       mrg 		/*
   1421    1.1       mrg 		 * now get a buf structure.   note that the vb_buf is
   1422    1.1       mrg 		 * at the front of the nbp structure so that you can
   1423    1.1       mrg 		 * cast pointers between the two structure easily.
   1424    1.1       mrg 		 */
   1425  1.134        ad 		nbp = pool_get(&vndbuf_pool, PR_WAITOK);
   1426  1.134        ad 		buf_init(&nbp->vb_buf);
   1427  1.134        ad 		nbp->vb_buf.b_flags    = bp->b_flags;
   1428  1.134        ad 		nbp->vb_buf.b_cflags   = bp->b_cflags;
   1429  1.134        ad 		nbp->vb_buf.b_oflags   = bp->b_oflags;
   1430    1.1       mrg 		nbp->vb_buf.b_bcount   = sz;
   1431   1.12        pk 		nbp->vb_buf.b_bufsize  = sz;
   1432    1.1       mrg 		nbp->vb_buf.b_error    = 0;
   1433    1.1       mrg 		nbp->vb_buf.b_data     = addr;
   1434   1.41       chs 		nbp->vb_buf.b_lblkno   = 0;
   1435    1.1       mrg 		nbp->vb_buf.b_blkno    = nbn + btodb(off);
   1436   1.34   thorpej 		nbp->vb_buf.b_rawblkno = nbp->vb_buf.b_blkno;
   1437  1.130   hannken 		nbp->vb_buf.b_iodone   = sw_reg_biodone;
   1438   1.53       chs 		nbp->vb_buf.b_vp       = vp;
   1439  1.156     rmind 		nbp->vb_buf.b_objlock  = vp->v_interlock;
   1440   1.53       chs 		if (vp->v_type == VBLK) {
   1441   1.53       chs 			nbp->vb_buf.b_dev = vp->v_rdev;
   1442   1.53       chs 		}
   1443    1.1       mrg 
   1444    1.1       mrg 		nbp->vb_xfer = vnx;	/* patch it back in to vnx */
   1445    1.1       mrg 
   1446    1.1       mrg 		/*
   1447    1.1       mrg 		 * Just sort by block number
   1448    1.1       mrg 		 */
   1449    1.1       mrg 		s = splbio();
   1450    1.1       mrg 		if (vnx->vx_error != 0) {
   1451  1.134        ad 			buf_destroy(&nbp->vb_buf);
   1452  1.134        ad 			pool_put(&vndbuf_pool, nbp);
   1453    1.1       mrg 			goto out;
   1454    1.1       mrg 		}
   1455    1.1       mrg 		vnx->vx_pending++;
   1456    1.1       mrg 
   1457    1.1       mrg 		/* sort it in and start I/O if we are not over our limit */
   1458  1.134        ad 		/* XXXAD locking */
   1459  1.143      yamt 		bufq_put(sdp->swd_tab, &nbp->vb_buf);
   1460    1.1       mrg 		sw_reg_start(sdp);
   1461    1.1       mrg 		splx(s);
   1462    1.1       mrg 
   1463    1.1       mrg 		/*
   1464    1.1       mrg 		 * advance to the next I/O
   1465    1.1       mrg 		 */
   1466    1.9       mrg 		byteoff += sz;
   1467    1.1       mrg 		addr += sz;
   1468    1.1       mrg 	}
   1469    1.1       mrg 
   1470    1.1       mrg 	s = splbio();
   1471    1.1       mrg 
   1472    1.1       mrg out: /* Arrive here at splbio */
   1473    1.1       mrg 	vnx->vx_flags &= ~VX_BUSY;
   1474    1.1       mrg 	if (vnx->vx_pending == 0) {
   1475  1.134        ad 		error = vnx->vx_error;
   1476  1.134        ad 		pool_put(&vndxfer_pool, vnx);
   1477  1.134        ad 		bp->b_error = error;
   1478    1.1       mrg 		biodone(bp);
   1479    1.1       mrg 	}
   1480    1.1       mrg 	splx(s);
   1481    1.1       mrg }
   1482    1.1       mrg 
   1483    1.1       mrg /*
   1484    1.1       mrg  * sw_reg_start: start an I/O request on the requested swapdev
   1485    1.1       mrg  *
   1486   1.65   hannken  * => reqs are sorted by b_rawblkno (above)
   1487    1.1       mrg  */
   1488    1.1       mrg static void
   1489   1.93   thorpej sw_reg_start(struct swapdev *sdp)
   1490    1.1       mrg {
   1491    1.1       mrg 	struct buf	*bp;
   1492  1.134        ad 	struct vnode	*vp;
   1493    1.1       mrg 	UVMHIST_FUNC("sw_reg_start"); UVMHIST_CALLED(pdhist);
   1494    1.1       mrg 
   1495    1.8       mrg 	/* recursion control */
   1496    1.1       mrg 	if ((sdp->swd_flags & SWF_BUSY) != 0)
   1497    1.1       mrg 		return;
   1498    1.1       mrg 
   1499    1.1       mrg 	sdp->swd_flags |= SWF_BUSY;
   1500    1.1       mrg 
   1501   1.33   thorpej 	while (sdp->swd_active < sdp->swd_maxactive) {
   1502  1.143      yamt 		bp = bufq_get(sdp->swd_tab);
   1503    1.1       mrg 		if (bp == NULL)
   1504    1.1       mrg 			break;
   1505   1.33   thorpej 		sdp->swd_active++;
   1506    1.1       mrg 
   1507    1.1       mrg 		UVMHIST_LOG(pdhist,
   1508  1.175  pgoyette 		    "sw_reg_start:  bp %#jx vp %#jx blkno %#jx cnt %jx",
   1509  1.175  pgoyette 		    (uintptr_t)bp, (uintptr_t)bp->b_vp, (uintptr_t)bp->b_blkno,
   1510  1.175  pgoyette 		    bp->b_bcount);
   1511  1.134        ad 		vp = bp->b_vp;
   1512  1.156     rmind 		KASSERT(bp->b_objlock == vp->v_interlock);
   1513  1.134        ad 		if ((bp->b_flags & B_READ) == 0) {
   1514  1.156     rmind 			mutex_enter(vp->v_interlock);
   1515  1.134        ad 			vp->v_numoutput++;
   1516  1.156     rmind 			mutex_exit(vp->v_interlock);
   1517  1.134        ad 		}
   1518  1.134        ad 		VOP_STRATEGY(vp, bp);
   1519    1.1       mrg 	}
   1520    1.1       mrg 	sdp->swd_flags &= ~SWF_BUSY;
   1521    1.1       mrg }
   1522    1.1       mrg 
   1523    1.1       mrg /*
   1524  1.130   hannken  * sw_reg_biodone: one of our i/o's has completed
   1525  1.130   hannken  */
   1526  1.130   hannken static void
   1527  1.130   hannken sw_reg_biodone(struct buf *bp)
   1528  1.130   hannken {
   1529  1.130   hannken 	workqueue_enqueue(sw_reg_workqueue, &bp->b_work, NULL);
   1530  1.130   hannken }
   1531  1.130   hannken 
   1532  1.130   hannken /*
   1533    1.1       mrg  * sw_reg_iodone: one of our i/o's has completed and needs post-i/o cleanup
   1534    1.1       mrg  *
   1535    1.1       mrg  * => note that we can recover the vndbuf struct by casting the buf ptr
   1536    1.1       mrg  */
   1537    1.1       mrg static void
   1538  1.130   hannken sw_reg_iodone(struct work *wk, void *dummy)
   1539    1.1       mrg {
   1540  1.130   hannken 	struct vndbuf *vbp = (void *)wk;
   1541    1.1       mrg 	struct vndxfer *vnx = vbp->vb_xfer;
   1542    1.1       mrg 	struct buf *pbp = vnx->vx_bp;		/* parent buffer */
   1543    1.1       mrg 	struct swapdev	*sdp = vnx->vx_sdp;
   1544   1.72       chs 	int s, resid, error;
   1545  1.130   hannken 	KASSERT(&vbp->vb_buf.b_work == wk);
   1546    1.1       mrg 	UVMHIST_FUNC("sw_reg_iodone"); UVMHIST_CALLED(pdhist);
   1547    1.1       mrg 
   1548  1.175  pgoyette 	UVMHIST_LOG(pdhist, "  vbp=%#jx vp=%#jx blkno=%jx addr=%#jx",
   1549  1.175  pgoyette 	    (uintptr_t)vbp, (uintptr_t)vbp->vb_buf.b_vp, vbp->vb_buf.b_blkno,
   1550  1.175  pgoyette 	    (uintptr_t)vbp->vb_buf.b_data);
   1551  1.175  pgoyette 	UVMHIST_LOG(pdhist, "  cnt=%jx resid=%jx",
   1552    1.1       mrg 	    vbp->vb_buf.b_bcount, vbp->vb_buf.b_resid, 0, 0);
   1553    1.1       mrg 
   1554    1.1       mrg 	/*
   1555    1.1       mrg 	 * protect vbp at splbio and update.
   1556    1.1       mrg 	 */
   1557    1.1       mrg 
   1558    1.1       mrg 	s = splbio();
   1559    1.1       mrg 	resid = vbp->vb_buf.b_bcount - vbp->vb_buf.b_resid;
   1560    1.1       mrg 	pbp->b_resid -= resid;
   1561    1.1       mrg 	vnx->vx_pending--;
   1562    1.1       mrg 
   1563  1.129        ad 	if (vbp->vb_buf.b_error != 0) {
   1564    1.1       mrg 		/* pass error upward */
   1565  1.134        ad 		error = vbp->vb_buf.b_error ? vbp->vb_buf.b_error : EIO;
   1566  1.175  pgoyette 		UVMHIST_LOG(pdhist, "  got error=%jd !", error, 0, 0, 0);
   1567   1.72       chs 		vnx->vx_error = error;
   1568   1.35       chs 	}
   1569   1.35       chs 
   1570   1.35       chs 	/*
   1571    1.1       mrg 	 * kill vbp structure
   1572    1.1       mrg 	 */
   1573  1.134        ad 	buf_destroy(&vbp->vb_buf);
   1574  1.134        ad 	pool_put(&vndbuf_pool, vbp);
   1575    1.1       mrg 
   1576    1.1       mrg 	/*
   1577    1.1       mrg 	 * wrap up this transaction if it has run to completion or, in
   1578    1.1       mrg 	 * case of an error, when all auxiliary buffers have returned.
   1579    1.1       mrg 	 */
   1580    1.1       mrg 	if (vnx->vx_error != 0) {
   1581    1.1       mrg 		/* pass error upward */
   1582  1.134        ad 		error = vnx->vx_error;
   1583    1.1       mrg 		if ((vnx->vx_flags & VX_BUSY) == 0 && vnx->vx_pending == 0) {
   1584  1.134        ad 			pbp->b_error = error;
   1585    1.1       mrg 			biodone(pbp);
   1586  1.134        ad 			pool_put(&vndxfer_pool, vnx);
   1587    1.1       mrg 		}
   1588   1.11        pk 	} else if (pbp->b_resid == 0) {
   1589   1.46       chs 		KASSERT(vnx->vx_pending == 0);
   1590    1.1       mrg 		if ((vnx->vx_flags & VX_BUSY) == 0) {
   1591  1.175  pgoyette 			UVMHIST_LOG(pdhist, "  iodone, pbp=%#jx error=%jd !",
   1592  1.175  pgoyette 			    (uintptr_t)pbp, vnx->vx_error, 0, 0);
   1593    1.1       mrg 			biodone(pbp);
   1594  1.134        ad 			pool_put(&vndxfer_pool, vnx);
   1595    1.1       mrg 		}
   1596    1.1       mrg 	}
   1597    1.1       mrg 
   1598    1.1       mrg 	/*
   1599    1.1       mrg 	 * done!   start next swapdev I/O if one is pending
   1600    1.1       mrg 	 */
   1601   1.33   thorpej 	sdp->swd_active--;
   1602    1.1       mrg 	sw_reg_start(sdp);
   1603    1.1       mrg 	splx(s);
   1604    1.1       mrg }
   1605    1.1       mrg 
   1606    1.1       mrg 
   1607    1.1       mrg /*
   1608    1.1       mrg  * uvm_swap_alloc: allocate space on swap
   1609    1.1       mrg  *
   1610    1.1       mrg  * => allocation is done "round robin" down the priority list, as we
   1611    1.1       mrg  *	allocate in a priority we "rotate" the circle queue.
   1612    1.1       mrg  * => space can be freed with uvm_swap_free
   1613    1.1       mrg  * => we return the page slot number in /dev/drum (0 == invalid slot)
   1614  1.127        ad  * => we lock uvm_swap_data_lock
   1615    1.1       mrg  * => XXXMRG: "LESSOK" INTERFACE NEEDED TO EXTENT SYSTEM
   1616    1.1       mrg  */
   1617    1.1       mrg int
   1618  1.119   thorpej uvm_swap_alloc(int *nslots /* IN/OUT */, bool lessok)
   1619    1.1       mrg {
   1620    1.1       mrg 	struct swapdev *sdp;
   1621    1.1       mrg 	struct swappri *spp;
   1622    1.1       mrg 	UVMHIST_FUNC("uvm_swap_alloc"); UVMHIST_CALLED(pdhist);
   1623    1.1       mrg 
   1624    1.1       mrg 	/*
   1625    1.1       mrg 	 * no swap devices configured yet?   definite failure.
   1626    1.1       mrg 	 */
   1627    1.1       mrg 	if (uvmexp.nswapdev < 1)
   1628    1.1       mrg 		return 0;
   1629   1.51       chs 
   1630    1.1       mrg 	/*
   1631  1.162  jakllsch 	 * XXXJAK: BEGIN HACK
   1632  1.162  jakllsch 	 *
   1633  1.162  jakllsch 	 * blist_alloc() in subr_blist.c will panic if we try to allocate
   1634  1.162  jakllsch 	 * too many slots.
   1635  1.162  jakllsch 	 */
   1636  1.162  jakllsch 	if (*nslots > BLIST_MAX_ALLOC) {
   1637  1.162  jakllsch 		if (__predict_false(lessok == false))
   1638  1.162  jakllsch 			return 0;
   1639  1.162  jakllsch 		*nslots = BLIST_MAX_ALLOC;
   1640  1.162  jakllsch 	}
   1641  1.162  jakllsch 	/* XXXJAK: END HACK */
   1642  1.162  jakllsch 
   1643  1.162  jakllsch 	/*
   1644    1.1       mrg 	 * lock data lock, convert slots into blocks, and enter loop
   1645    1.1       mrg 	 */
   1646  1.127        ad 	mutex_enter(&uvm_swap_data_lock);
   1647    1.1       mrg 
   1648    1.1       mrg ReTry:	/* XXXMRG */
   1649   1.55       chs 	LIST_FOREACH(spp, &swap_priority, spi_swappri) {
   1650  1.164  christos 		TAILQ_FOREACH(sdp, &spp->spi_swapdev, swd_next) {
   1651   1.90      yamt 			uint64_t result;
   1652   1.90      yamt 
   1653    1.1       mrg 			/* if it's not enabled, then we can't swap from it */
   1654    1.1       mrg 			if ((sdp->swd_flags & SWF_ENABLE) == 0)
   1655    1.1       mrg 				continue;
   1656    1.1       mrg 			if (sdp->swd_npginuse + *nslots > sdp->swd_npages)
   1657    1.1       mrg 				continue;
   1658   1.90      yamt 			result = blist_alloc(sdp->swd_blist, *nslots);
   1659   1.90      yamt 			if (result == BLIST_NONE) {
   1660    1.1       mrg 				continue;
   1661    1.1       mrg 			}
   1662   1.90      yamt 			KASSERT(result < sdp->swd_drumsize);
   1663    1.1       mrg 
   1664    1.1       mrg 			/*
   1665  1.165  christos 			 * successful allocation!  now rotate the tailq.
   1666    1.1       mrg 			 */
   1667  1.164  christos 			TAILQ_REMOVE(&spp->spi_swapdev, sdp, swd_next);
   1668  1.164  christos 			TAILQ_INSERT_TAIL(&spp->spi_swapdev, sdp, swd_next);
   1669    1.1       mrg 			sdp->swd_npginuse += *nslots;
   1670    1.1       mrg 			uvmexp.swpginuse += *nslots;
   1671  1.127        ad 			mutex_exit(&uvm_swap_data_lock);
   1672    1.1       mrg 			/* done!  return drum slot number */
   1673    1.1       mrg 			UVMHIST_LOG(pdhist,
   1674  1.175  pgoyette 			    "success!  returning %jd slots starting at %jd",
   1675    1.1       mrg 			    *nslots, result + sdp->swd_drumoffset, 0, 0);
   1676   1.55       chs 			return (result + sdp->swd_drumoffset);
   1677    1.1       mrg 		}
   1678    1.1       mrg 	}
   1679    1.1       mrg 
   1680    1.1       mrg 	/* XXXMRG: BEGIN HACK */
   1681    1.1       mrg 	if (*nslots > 1 && lessok) {
   1682    1.1       mrg 		*nslots = 1;
   1683   1.90      yamt 		/* XXXMRG: ugh!  blist should support this for us */
   1684   1.90      yamt 		goto ReTry;
   1685    1.1       mrg 	}
   1686    1.1       mrg 	/* XXXMRG: END HACK */
   1687    1.1       mrg 
   1688  1.127        ad 	mutex_exit(&uvm_swap_data_lock);
   1689   1.55       chs 	return 0;
   1690    1.1       mrg }
   1691    1.1       mrg 
   1692  1.141        ad /*
   1693  1.141        ad  * uvm_swapisfull: return true if most of available swap is allocated
   1694  1.141        ad  * and in use.  we don't count some small portion as it may be inaccessible
   1695  1.141        ad  * to us at any given moment, for example if there is lock contention or if
   1696  1.141        ad  * pages are busy.
   1697  1.141        ad  */
   1698  1.119   thorpej bool
   1699   1.81        pk uvm_swapisfull(void)
   1700   1.81        pk {
   1701  1.141        ad 	int swpgonly;
   1702  1.119   thorpej 	bool rv;
   1703   1.81        pk 
   1704  1.127        ad 	mutex_enter(&uvm_swap_data_lock);
   1705   1.81        pk 	KASSERT(uvmexp.swpgonly <= uvmexp.swpages);
   1706  1.141        ad 	swpgonly = (int)((uint64_t)uvmexp.swpgonly * 100 /
   1707  1.141        ad 	    uvm_swapisfull_factor);
   1708  1.141        ad 	rv = (swpgonly >= uvmexp.swpgavail);
   1709  1.127        ad 	mutex_exit(&uvm_swap_data_lock);
   1710   1.81        pk 
   1711   1.81        pk 	return (rv);
   1712   1.81        pk }
   1713   1.81        pk 
   1714    1.1       mrg /*
   1715   1.32       chs  * uvm_swap_markbad: keep track of swap ranges where we've had i/o errors
   1716   1.32       chs  *
   1717  1.127        ad  * => we lock uvm_swap_data_lock
   1718   1.32       chs  */
   1719   1.32       chs void
   1720   1.93   thorpej uvm_swap_markbad(int startslot, int nslots)
   1721   1.32       chs {
   1722   1.32       chs 	struct swapdev *sdp;
   1723   1.32       chs 	UVMHIST_FUNC("uvm_swap_markbad"); UVMHIST_CALLED(pdhist);
   1724   1.32       chs 
   1725  1.127        ad 	mutex_enter(&uvm_swap_data_lock);
   1726   1.32       chs 	sdp = swapdrum_getsdp(startslot);
   1727   1.82        pk 	KASSERT(sdp != NULL);
   1728   1.32       chs 
   1729   1.32       chs 	/*
   1730   1.32       chs 	 * we just keep track of how many pages have been marked bad
   1731   1.32       chs 	 * in this device, to make everything add up in swap_off().
   1732   1.32       chs 	 * we assume here that the range of slots will all be within
   1733   1.32       chs 	 * one swap device.
   1734   1.32       chs 	 */
   1735   1.41       chs 
   1736   1.82        pk 	KASSERT(uvmexp.swpgonly >= nslots);
   1737  1.182        ad 	atomic_add_int(&uvmexp.swpgonly, -nslots);
   1738   1.32       chs 	sdp->swd_npgbad += nslots;
   1739  1.175  pgoyette 	UVMHIST_LOG(pdhist, "now %jd bad", sdp->swd_npgbad, 0,0,0);
   1740  1.127        ad 	mutex_exit(&uvm_swap_data_lock);
   1741   1.32       chs }
   1742   1.32       chs 
   1743   1.32       chs /*
   1744    1.1       mrg  * uvm_swap_free: free swap slots
   1745    1.1       mrg  *
   1746    1.1       mrg  * => this can be all or part of an allocation made by uvm_swap_alloc
   1747  1.127        ad  * => we lock uvm_swap_data_lock
   1748    1.1       mrg  */
   1749    1.1       mrg void
   1750   1.93   thorpej uvm_swap_free(int startslot, int nslots)
   1751    1.1       mrg {
   1752    1.1       mrg 	struct swapdev *sdp;
   1753    1.1       mrg 	UVMHIST_FUNC("uvm_swap_free"); UVMHIST_CALLED(pdhist);
   1754    1.1       mrg 
   1755  1.175  pgoyette 	UVMHIST_LOG(pdhist, "freeing %jd slots starting at %jd", nslots,
   1756    1.1       mrg 	    startslot, 0, 0);
   1757   1.32       chs 
   1758   1.32       chs 	/*
   1759   1.32       chs 	 * ignore attempts to free the "bad" slot.
   1760   1.32       chs 	 */
   1761   1.46       chs 
   1762   1.32       chs 	if (startslot == SWSLOT_BAD) {
   1763   1.32       chs 		return;
   1764   1.32       chs 	}
   1765   1.32       chs 
   1766    1.1       mrg 	/*
   1767   1.51       chs 	 * convert drum slot offset back to sdp, free the blocks
   1768   1.51       chs 	 * in the extent, and return.   must hold pri lock to do
   1769    1.1       mrg 	 * lookup and access the extent.
   1770    1.1       mrg 	 */
   1771   1.46       chs 
   1772  1.127        ad 	mutex_enter(&uvm_swap_data_lock);
   1773    1.1       mrg 	sdp = swapdrum_getsdp(startslot);
   1774   1.46       chs 	KASSERT(uvmexp.nswapdev >= 1);
   1775   1.46       chs 	KASSERT(sdp != NULL);
   1776   1.46       chs 	KASSERT(sdp->swd_npginuse >= nslots);
   1777   1.90      yamt 	blist_free(sdp->swd_blist, startslot - sdp->swd_drumoffset, nslots);
   1778    1.1       mrg 	sdp->swd_npginuse -= nslots;
   1779    1.1       mrg 	uvmexp.swpginuse -= nslots;
   1780  1.127        ad 	mutex_exit(&uvm_swap_data_lock);
   1781    1.1       mrg }
   1782    1.1       mrg 
   1783    1.1       mrg /*
   1784    1.1       mrg  * uvm_swap_put: put any number of pages into a contig place on swap
   1785    1.1       mrg  *
   1786    1.1       mrg  * => can be sync or async
   1787    1.1       mrg  */
   1788   1.54       chs 
   1789    1.1       mrg int
   1790   1.93   thorpej uvm_swap_put(int swslot, struct vm_page **ppsp, int npages, int flags)
   1791    1.1       mrg {
   1792   1.56       chs 	int error;
   1793    1.1       mrg 
   1794   1.56       chs 	error = uvm_swap_io(ppsp, swslot, npages, B_WRITE |
   1795    1.1       mrg 	    ((flags & PGO_SYNCIO) ? 0 : B_ASYNC));
   1796   1.56       chs 	return error;
   1797    1.1       mrg }
   1798    1.1       mrg 
   1799    1.1       mrg /*
   1800    1.1       mrg  * uvm_swap_get: get a single page from swap
   1801    1.1       mrg  *
   1802    1.1       mrg  * => usually a sync op (from fault)
   1803    1.1       mrg  */
   1804   1.54       chs 
   1805    1.1       mrg int
   1806   1.93   thorpej uvm_swap_get(struct vm_page *page, int swslot, int flags)
   1807    1.1       mrg {
   1808   1.56       chs 	int error;
   1809    1.1       mrg 
   1810  1.184        ad 	atomic_inc_uint(&uvmexp.nswget);
   1811   1.46       chs 	KASSERT(flags & PGO_SYNCIO);
   1812   1.32       chs 	if (swslot == SWSLOT_BAD) {
   1813   1.47       chs 		return EIO;
   1814   1.32       chs 	}
   1815   1.81        pk 
   1816   1.56       chs 	error = uvm_swap_io(&page, swslot, 1, B_READ |
   1817    1.1       mrg 	    ((flags & PGO_SYNCIO) ? 0 : B_ASYNC));
   1818   1.56       chs 	if (error == 0) {
   1819   1.47       chs 
   1820   1.26       chs 		/*
   1821   1.54       chs 		 * this page is no longer only in swap.
   1822   1.26       chs 		 */
   1823   1.47       chs 
   1824   1.56       chs 		KASSERT(uvmexp.swpgonly > 0);
   1825  1.182        ad 		atomic_dec_uint(&uvmexp.swpgonly);
   1826   1.26       chs 	}
   1827   1.56       chs 	return error;
   1828    1.1       mrg }
   1829    1.1       mrg 
   1830    1.1       mrg /*
   1831    1.1       mrg  * uvm_swap_io: do an i/o operation to swap
   1832    1.1       mrg  */
   1833    1.1       mrg 
   1834    1.1       mrg static int
   1835   1.93   thorpej uvm_swap_io(struct vm_page **pps, int startslot, int npages, int flags)
   1836    1.1       mrg {
   1837    1.1       mrg 	daddr_t startblk;
   1838    1.1       mrg 	struct	buf *bp;
   1839   1.15       eeh 	vaddr_t kva;
   1840  1.134        ad 	int	error, mapinflags;
   1841  1.187  riastrad 	bool write, async, swap_encrypt;
   1842    1.1       mrg 	UVMHIST_FUNC("uvm_swap_io"); UVMHIST_CALLED(pdhist);
   1843    1.1       mrg 
   1844  1.175  pgoyette 	UVMHIST_LOG(pdhist, "<- called, startslot=%jd, npages=%jd, flags=%jd",
   1845    1.1       mrg 	    startslot, npages, flags, 0);
   1846   1.32       chs 
   1847   1.41       chs 	write = (flags & B_READ) == 0;
   1848   1.41       chs 	async = (flags & B_ASYNC) != 0;
   1849  1.189  riastrad 	swap_encrypt = atomic_load_relaxed(&uvm_swap_encrypt);
   1850   1.41       chs 
   1851    1.1       mrg 	/*
   1852  1.137      yamt 	 * allocate a buf for the i/o.
   1853  1.137      yamt 	 */
   1854  1.137      yamt 
   1855  1.137      yamt 	KASSERT(curlwp != uvm.pagedaemon_lwp || (write && async));
   1856  1.137      yamt 	bp = getiobuf(swapdev_vp, curlwp != uvm.pagedaemon_lwp);
   1857  1.137      yamt 	if (bp == NULL) {
   1858  1.137      yamt 		uvm_aio_aiodone_pages(pps, npages, true, ENOMEM);
   1859  1.137      yamt 		return ENOMEM;
   1860  1.137      yamt 	}
   1861  1.137      yamt 
   1862  1.137      yamt 	/*
   1863    1.1       mrg 	 * convert starting drum slot to block number
   1864    1.1       mrg 	 */
   1865   1.54       chs 
   1866   1.99      matt 	startblk = btodb((uint64_t)startslot << PAGE_SHIFT);
   1867    1.1       mrg 
   1868    1.1       mrg 	/*
   1869   1.54       chs 	 * first, map the pages into the kernel.
   1870   1.41       chs 	 */
   1871   1.41       chs 
   1872   1.54       chs 	mapinflags = !write ?
   1873   1.54       chs 		UVMPAGER_MAPIN_WAITOK|UVMPAGER_MAPIN_READ :
   1874   1.54       chs 		UVMPAGER_MAPIN_WAITOK|UVMPAGER_MAPIN_WRITE;
   1875  1.187  riastrad 	if (write && swap_encrypt)	/* need to encrypt in-place */
   1876  1.187  riastrad 		mapinflags |= UVMPAGER_MAPIN_READ;
   1877   1.41       chs 	kva = uvm_pagermapin(pps, npages, mapinflags);
   1878    1.1       mrg 
   1879   1.51       chs 	/*
   1880  1.187  riastrad 	 * encrypt writes in place if requested
   1881  1.187  riastrad 	 */
   1882  1.187  riastrad 
   1883  1.187  riastrad 	if (write) do {
   1884  1.187  riastrad 		struct swapdev *sdp;
   1885  1.187  riastrad 		int i;
   1886  1.187  riastrad 
   1887  1.187  riastrad 		/*
   1888  1.187  riastrad 		 * Get the swapdev so we can discriminate on the
   1889  1.187  riastrad 		 * encryption state.  There may or may not be an
   1890  1.187  riastrad 		 * encryption key generated; we may or may not be asked
   1891  1.187  riastrad 		 * to encrypt swap.
   1892  1.187  riastrad 		 *
   1893  1.187  riastrad 		 * 1. NO KEY, NO ENCRYPTION: Nothing to do.
   1894  1.187  riastrad 		 *
   1895  1.187  riastrad 		 * 2. NO KEY, BUT ENCRYPTION: Generate a key, encrypt,
   1896  1.187  riastrad 		 *    and mark the slots encrypted.
   1897  1.187  riastrad 		 *
   1898  1.187  riastrad 		 * 3. KEY, BUT NO ENCRYPTION: The slots may already be
   1899  1.187  riastrad 		 *    marked encrypted from a past life.  Mark them not
   1900  1.187  riastrad 		 *    encrypted.
   1901  1.187  riastrad 		 *
   1902  1.187  riastrad 		 * 4. KEY, ENCRYPTION: Encrypt and mark the slots
   1903  1.187  riastrad 		 *    encrypted.
   1904  1.187  riastrad 		 */
   1905  1.190  riastrad 		mutex_enter(&uvm_swap_data_lock);
   1906  1.187  riastrad 		sdp = swapdrum_getsdp(startslot);
   1907  1.187  riastrad 		if (!sdp->swd_encinit) {
   1908  1.190  riastrad 			if (!swap_encrypt) {
   1909  1.190  riastrad 				mutex_exit(&uvm_swap_data_lock);
   1910  1.187  riastrad 				break;
   1911  1.190  riastrad 			}
   1912  1.187  riastrad 			uvm_swap_genkey(sdp);
   1913  1.187  riastrad 		}
   1914  1.187  riastrad 		KASSERT(sdp->swd_encinit);
   1915  1.190  riastrad 		mutex_exit(&uvm_swap_data_lock);
   1916  1.187  riastrad 
   1917  1.192  jdolecek 		for (i = 0; i < npages; i++) {
   1918  1.192  jdolecek 			int s = startslot + i;
   1919  1.192  jdolecek 			KDASSERT(swapdrum_sdp_is(s, sdp));
   1920  1.192  jdolecek 			KASSERT(s >= sdp->swd_drumoffset);
   1921  1.192  jdolecek 			s -= sdp->swd_drumoffset;
   1922  1.192  jdolecek 			KASSERT(s < sdp->swd_drumsize);
   1923  1.192  jdolecek 
   1924  1.192  jdolecek 			if (swap_encrypt) {
   1925  1.189  riastrad 				uvm_swap_encryptpage(sdp,
   1926  1.188  riastrad 				    (void *)(kva + (vsize_t)i*PAGE_SIZE), s);
   1927  1.190  riastrad 				atomic_or_32(&sdp->swd_encmap[s/32],
   1928  1.190  riastrad 				    __BIT(s%32));
   1929  1.192  jdolecek 			} else {
   1930  1.190  riastrad 				atomic_and_32(&sdp->swd_encmap[s/32],
   1931  1.190  riastrad 				    ~__BIT(s%32));
   1932  1.187  riastrad 			}
   1933  1.187  riastrad 		}
   1934  1.187  riastrad 	} while (0);
   1935  1.187  riastrad 
   1936  1.187  riastrad 	/*
   1937    1.1       mrg 	 * fill in the bp/sbp.   we currently route our i/o through
   1938    1.1       mrg 	 * /dev/drum's vnode [swapdev_vp].
   1939    1.1       mrg 	 */
   1940   1.54       chs 
   1941  1.134        ad 	bp->b_cflags = BC_BUSY | BC_NOCACHE;
   1942  1.134        ad 	bp->b_flags = (flags & (B_READ|B_ASYNC));
   1943    1.1       mrg 	bp->b_proc = &proc0;	/* XXX */
   1944   1.12        pk 	bp->b_vnbufs.le_next = NOLIST;
   1945  1.122  christos 	bp->b_data = (void *)kva;
   1946    1.1       mrg 	bp->b_blkno = startblk;
   1947   1.41       chs 	bp->b_bufsize = bp->b_bcount = npages << PAGE_SHIFT;
   1948    1.1       mrg 
   1949   1.51       chs 	/*
   1950   1.41       chs 	 * bump v_numoutput (counter of number of active outputs).
   1951    1.1       mrg 	 */
   1952   1.54       chs 
   1953   1.41       chs 	if (write) {
   1954  1.156     rmind 		mutex_enter(swapdev_vp->v_interlock);
   1955  1.134        ad 		swapdev_vp->v_numoutput++;
   1956  1.156     rmind 		mutex_exit(swapdev_vp->v_interlock);
   1957    1.1       mrg 	}
   1958    1.1       mrg 
   1959    1.1       mrg 	/*
   1960   1.41       chs 	 * for async ops we must set up the iodone handler.
   1961    1.1       mrg 	 */
   1962   1.54       chs 
   1963   1.41       chs 	if (async) {
   1964  1.186       chs 		bp->b_iodone = uvm_aio_aiodone;
   1965    1.1       mrg 		UVMHIST_LOG(pdhist, "doing async!", 0, 0, 0, 0);
   1966  1.126        ad 		if (curlwp == uvm.pagedaemon_lwp)
   1967   1.83      yamt 			BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
   1968   1.83      yamt 		else
   1969   1.83      yamt 			BIO_SETPRIO(bp, BPRIO_TIMELIMITED);
   1970   1.83      yamt 	} else {
   1971  1.134        ad 		bp->b_iodone = NULL;
   1972   1.83      yamt 		BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
   1973    1.1       mrg 	}
   1974    1.1       mrg 	UVMHIST_LOG(pdhist,
   1975  1.175  pgoyette 	    "about to start io: data = %#jx blkno = 0x%jx, bcount = %jd",
   1976  1.175  pgoyette 	    (uintptr_t)bp->b_data, bp->b_blkno, bp->b_bcount, 0);
   1977    1.1       mrg 
   1978    1.1       mrg 	/*
   1979    1.1       mrg 	 * now we start the I/O, and if async, return.
   1980    1.1       mrg 	 */
   1981   1.54       chs 
   1982   1.84   hannken 	VOP_STRATEGY(swapdev_vp, bp);
   1983  1.190  riastrad 	if (async) {
   1984  1.190  riastrad 		/*
   1985  1.190  riastrad 		 * Reads are always synchronous; if this changes, we
   1986  1.190  riastrad 		 * need to add an asynchronous path for decryption.
   1987  1.190  riastrad 		 */
   1988  1.193  jdolecek 		KASSERT(write);
   1989   1.47       chs 		return 0;
   1990  1.190  riastrad 	}
   1991    1.1       mrg 
   1992    1.1       mrg 	/*
   1993    1.1       mrg 	 * must be sync i/o.   wait for it to finish
   1994    1.1       mrg 	 */
   1995   1.54       chs 
   1996   1.47       chs 	error = biowait(bp);
   1997  1.191  riastrad 	if (error)
   1998  1.191  riastrad 		goto out;
   1999    1.1       mrg 
   2000    1.1       mrg 	/*
   2001  1.187  riastrad 	 * decrypt reads in place if needed
   2002  1.187  riastrad 	 */
   2003  1.187  riastrad 
   2004  1.187  riastrad 	if (!write) do {
   2005  1.187  riastrad 		struct swapdev *sdp;
   2006  1.190  riastrad 		bool encinit;
   2007  1.187  riastrad 		int i;
   2008  1.187  riastrad 
   2009  1.190  riastrad 		/*
   2010  1.190  riastrad 		 * Get the sdp.  Everything about it except the encinit
   2011  1.190  riastrad 		 * bit, saying whether the encryption key is
   2012  1.190  riastrad 		 * initialized or not, and the encrypted bit for each
   2013  1.190  riastrad 		 * page, is stable until all swap pages have been
   2014  1.190  riastrad 		 * released and the device is removed.
   2015  1.190  riastrad 		 */
   2016  1.190  riastrad 		mutex_enter(&uvm_swap_data_lock);
   2017  1.187  riastrad 		sdp = swapdrum_getsdp(startslot);
   2018  1.190  riastrad 		encinit = sdp->swd_encinit;
   2019  1.190  riastrad 		mutex_exit(&uvm_swap_data_lock);
   2020  1.190  riastrad 
   2021  1.190  riastrad 		if (!encinit)
   2022  1.187  riastrad 			/*
   2023  1.187  riastrad 			 * If there's no encryption key, there's no way
   2024  1.187  riastrad 			 * any of these slots can be encrypted, so
   2025  1.187  riastrad 			 * nothing to do here.
   2026  1.187  riastrad 			 */
   2027  1.187  riastrad 			break;
   2028  1.187  riastrad 		for (i = 0; i < npages; i++) {
   2029  1.187  riastrad 			int s = startslot + i;
   2030  1.190  riastrad 			KDASSERT(swapdrum_sdp_is(s, sdp));
   2031  1.187  riastrad 			KASSERT(s >= sdp->swd_drumoffset);
   2032  1.187  riastrad 			s -= sdp->swd_drumoffset;
   2033  1.187  riastrad 			KASSERT(s < sdp->swd_drumsize);
   2034  1.190  riastrad 			if ((atomic_load_relaxed(&sdp->swd_encmap[s/32]) &
   2035  1.190  riastrad 				__BIT(s%32)) == 0)
   2036  1.187  riastrad 				continue;
   2037  1.189  riastrad 			uvm_swap_decryptpage(sdp,
   2038  1.188  riastrad 			    (void *)(kva + (vsize_t)i*PAGE_SIZE), s);
   2039  1.187  riastrad 		}
   2040  1.187  riastrad 	} while (0);
   2041  1.191  riastrad out:
   2042  1.187  riastrad 	/*
   2043    1.1       mrg 	 * kill the pager mapping
   2044    1.1       mrg 	 */
   2045   1.54       chs 
   2046    1.1       mrg 	uvm_pagermapout(kva, npages);
   2047    1.1       mrg 
   2048    1.1       mrg 	/*
   2049   1.54       chs 	 * now dispose of the buf and we're done.
   2050    1.1       mrg 	 */
   2051   1.54       chs 
   2052  1.134        ad 	if (write) {
   2053  1.156     rmind 		mutex_enter(swapdev_vp->v_interlock);
   2054   1.41       chs 		vwakeup(bp);
   2055  1.156     rmind 		mutex_exit(swapdev_vp->v_interlock);
   2056  1.134        ad 	}
   2057   1.98      yamt 	putiobuf(bp);
   2058  1.175  pgoyette 	UVMHIST_LOG(pdhist, "<- done (sync)  error=%jd", error, 0, 0, 0);
   2059  1.134        ad 
   2060   1.47       chs 	return (error);
   2061    1.1       mrg }
   2062  1.187  riastrad 
   2063  1.187  riastrad /*
   2064  1.187  riastrad  * uvm_swap_genkey(sdp)
   2065  1.187  riastrad  *
   2066  1.187  riastrad  *	Generate a key for swap encryption.
   2067  1.187  riastrad  */
   2068  1.187  riastrad static void
   2069  1.187  riastrad uvm_swap_genkey(struct swapdev *sdp)
   2070  1.187  riastrad {
   2071  1.187  riastrad 	uint8_t key[32];
   2072  1.187  riastrad 
   2073  1.187  riastrad 	KASSERT(!sdp->swd_encinit);
   2074  1.187  riastrad 
   2075  1.187  riastrad 	cprng_strong(kern_cprng, key, sizeof key, 0);
   2076  1.194  riastrad 	aes_setenckey256(&sdp->swd_enckey, key);
   2077  1.194  riastrad 	aes_setdeckey256(&sdp->swd_deckey, key);
   2078  1.187  riastrad 	explicit_memset(key, 0, sizeof key);
   2079  1.187  riastrad 
   2080  1.187  riastrad 	sdp->swd_encinit = true;
   2081  1.187  riastrad }
   2082  1.187  riastrad 
   2083  1.187  riastrad /*
   2084  1.189  riastrad  * uvm_swap_encryptpage(sdp, kva, slot)
   2085  1.187  riastrad  *
   2086  1.187  riastrad  *	Encrypt one page of data at kva for the specified slot number
   2087  1.187  riastrad  *	in the swap device.
   2088  1.187  riastrad  */
   2089  1.187  riastrad static void
   2090  1.189  riastrad uvm_swap_encryptpage(struct swapdev *sdp, void *kva, int slot)
   2091  1.187  riastrad {
   2092  1.195  riastrad 	uint8_t preiv[16] __aligned(16) = {0}, iv[16] __aligned(16);
   2093  1.187  riastrad 
   2094  1.187  riastrad 	/* iv := AES_k(le32enc(slot) || 0^96) */
   2095  1.187  riastrad 	le32enc(preiv, slot);
   2096  1.194  riastrad 	aes_enc(&sdp->swd_enckey, (const void *)preiv, iv, AES_256_NROUNDS);
   2097  1.187  riastrad 
   2098  1.187  riastrad 	/* *kva := AES-CBC_k(iv, *kva) */
   2099  1.194  riastrad 	aes_cbc_enc(&sdp->swd_enckey, kva, kva, PAGE_SIZE, iv,
   2100  1.194  riastrad 	    AES_256_NROUNDS);
   2101  1.187  riastrad 
   2102  1.187  riastrad 	explicit_memset(&iv, 0, sizeof iv);
   2103  1.187  riastrad }
   2104  1.187  riastrad 
   2105  1.187  riastrad /*
   2106  1.189  riastrad  * uvm_swap_decryptpage(sdp, kva, slot)
   2107  1.187  riastrad  *
   2108  1.187  riastrad  *	Decrypt one page of data at kva for the specified slot number
   2109  1.187  riastrad  *	in the swap device.
   2110  1.187  riastrad  */
   2111  1.187  riastrad static void
   2112  1.189  riastrad uvm_swap_decryptpage(struct swapdev *sdp, void *kva, int slot)
   2113  1.187  riastrad {
   2114  1.195  riastrad 	uint8_t preiv[16] __aligned(16) = {0}, iv[16] __aligned(16);
   2115  1.187  riastrad 
   2116  1.187  riastrad 	/* iv := AES_k(le32enc(slot) || 0^96) */
   2117  1.187  riastrad 	le32enc(preiv, slot);
   2118  1.194  riastrad 	aes_enc(&sdp->swd_enckey, (const void *)preiv, iv, AES_256_NROUNDS);
   2119  1.187  riastrad 
   2120  1.187  riastrad 	/* *kva := AES-CBC^{-1}_k(iv, *kva) */
   2121  1.194  riastrad 	aes_cbc_dec(&sdp->swd_deckey, kva, kva, PAGE_SIZE, iv,
   2122  1.194  riastrad 	    AES_256_NROUNDS);
   2123  1.187  riastrad 
   2124  1.187  riastrad 	explicit_memset(&iv, 0, sizeof iv);
   2125  1.187  riastrad }
   2126  1.187  riastrad 
   2127  1.187  riastrad SYSCTL_SETUP(sysctl_uvmswap_setup, "sysctl uvmswap setup")
   2128  1.187  riastrad {
   2129  1.187  riastrad 
   2130  1.187  riastrad 	sysctl_createv(clog, 0, NULL, NULL,
   2131  1.187  riastrad 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_BOOL, "swap_encrypt",
   2132  1.187  riastrad 	    SYSCTL_DESCR("Encrypt data when swapped out to disk"),
   2133  1.189  riastrad 	    NULL, 0, &uvm_swap_encrypt, 0,
   2134  1.187  riastrad 	    CTL_VM, CTL_CREATE, CTL_EOL);
   2135  1.187  riastrad }
   2136