Tue Sep 23 08:56:15 2008 UTC ()
Move test for __SWAP_BROKEN here.


(ad)
diff -r1.139 -r1.140 src/sys/uvm/uvm_swap.c

cvs diff -r1.139 -r1.140 src/sys/uvm/uvm_swap.c (switch to unified diff)

--- src/sys/uvm/uvm_swap.c 2008/05/29 14:51:27 1.139
+++ src/sys/uvm/uvm_swap.c 2008/09/23 08:56:15 1.140
@@ -1,1277 +1,1281 @@ @@ -1,1277 +1,1281 @@
1/* $NetBSD: uvm_swap.c,v 1.139 2008/05/29 14:51:27 mrg Exp $ */ 1/* $NetBSD: uvm_swap.c,v 1.140 2008/09/23 08:56:15 ad Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1995, 1996, 1997 Matthew R. Green 4 * Copyright (c) 1995, 1996, 1997 Matthew R. Green
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
15 * 15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE. 26 * SUCH DAMAGE.
27 * 27 *
28 * from: NetBSD: vm_swap.c,v 1.52 1997/12/02 13:47:37 pk Exp 28 * from: NetBSD: vm_swap.c,v 1.52 1997/12/02 13:47:37 pk Exp
29 * from: Id: uvm_swap.c,v 1.1.2.42 1998/02/02 20:38:06 chuck Exp 29 * from: Id: uvm_swap.c,v 1.1.2.42 1998/02/02 20:38:06 chuck Exp
30 */ 30 */
31 31
32#include <sys/cdefs.h> 32#include <sys/cdefs.h>
33__KERNEL_RCSID(0, "$NetBSD: uvm_swap.c,v 1.139 2008/05/29 14:51:27 mrg Exp $"); 33__KERNEL_RCSID(0, "$NetBSD: uvm_swap.c,v 1.140 2008/09/23 08:56:15 ad Exp $");
34 34
35#include "fs_nfs.h" 35#include "fs_nfs.h"
36#include "opt_uvmhist.h" 36#include "opt_uvmhist.h"
37#include "opt_compat_netbsd.h" 37#include "opt_compat_netbsd.h"
38#include "opt_ddb.h" 38#include "opt_ddb.h"
39 39
40#include <sys/param.h> 40#include <sys/param.h>
41#include <sys/systm.h> 41#include <sys/systm.h>
42#include <sys/buf.h> 42#include <sys/buf.h>
43#include <sys/bufq.h> 43#include <sys/bufq.h>
44#include <sys/conf.h> 44#include <sys/conf.h>
45#include <sys/proc.h> 45#include <sys/proc.h>
46#include <sys/namei.h> 46#include <sys/namei.h>
47#include <sys/disklabel.h> 47#include <sys/disklabel.h>
48#include <sys/errno.h> 48#include <sys/errno.h>
49#include <sys/kernel.h> 49#include <sys/kernel.h>
50#include <sys/malloc.h> 50#include <sys/malloc.h>
51#include <sys/vnode.h> 51#include <sys/vnode.h>
52#include <sys/file.h> 52#include <sys/file.h>
53#include <sys/vmem.h> 53#include <sys/vmem.h>
54#include <sys/blist.h> 54#include <sys/blist.h>
55#include <sys/mount.h> 55#include <sys/mount.h>
56#include <sys/pool.h> 56#include <sys/pool.h>
57#include <sys/syscallargs.h> 57#include <sys/syscallargs.h>
58#include <sys/swap.h> 58#include <sys/swap.h>
59#include <sys/kauth.h> 59#include <sys/kauth.h>
60#include <sys/sysctl.h> 60#include <sys/sysctl.h>
61#include <sys/workqueue.h> 61#include <sys/workqueue.h>
62 62
63#include <uvm/uvm.h> 63#include <uvm/uvm.h>
64 64
65#include <miscfs/specfs/specdev.h> 65#include <miscfs/specfs/specdev.h>
66 66
67/* 67/*
68 * uvm_swap.c: manage configuration and i/o to swap space. 68 * uvm_swap.c: manage configuration and i/o to swap space.
69 */ 69 */
70 70
71/* 71/*
72 * swap space is managed in the following way: 72 * swap space is managed in the following way:
73 * 73 *
74 * each swap partition or file is described by a "swapdev" structure. 74 * each swap partition or file is described by a "swapdev" structure.
75 * each "swapdev" structure contains a "swapent" structure which contains 75 * each "swapdev" structure contains a "swapent" structure which contains
76 * information that is passed up to the user (via system calls). 76 * information that is passed up to the user (via system calls).
77 * 77 *
78 * each swap partition is assigned a "priority" (int) which controls 78 * each swap partition is assigned a "priority" (int) which controls
79 * swap parition usage. 79 * swap parition usage.
80 * 80 *
81 * the system maintains a global data structure describing all swap 81 * the system maintains a global data structure describing all swap
82 * partitions/files. there is a sorted LIST of "swappri" structures 82 * partitions/files. there is a sorted LIST of "swappri" structures
83 * which describe "swapdev"'s at that priority. this LIST is headed 83 * which describe "swapdev"'s at that priority. this LIST is headed
84 * by the "swap_priority" global var. each "swappri" contains a 84 * by the "swap_priority" global var. each "swappri" contains a
85 * CIRCLEQ of "swapdev" structures at that priority. 85 * CIRCLEQ of "swapdev" structures at that priority.
86 * 86 *
87 * locking: 87 * locking:
88 * - swap_syscall_lock (krwlock_t): this lock serializes the swapctl 88 * - swap_syscall_lock (krwlock_t): this lock serializes the swapctl
89 * system call and prevents the swap priority list from changing 89 * system call and prevents the swap priority list from changing
90 * while we are in the middle of a system call (e.g. SWAP_STATS). 90 * while we are in the middle of a system call (e.g. SWAP_STATS).
91 * - uvm_swap_data_lock (kmutex_t): this lock protects all swap data 91 * - uvm_swap_data_lock (kmutex_t): this lock protects all swap data
92 * structures including the priority list, the swapdev structures, 92 * structures including the priority list, the swapdev structures,
93 * and the swapmap arena. 93 * and the swapmap arena.
94 * 94 *
95 * each swap device has the following info: 95 * each swap device has the following info:
96 * - swap device in use (could be disabled, preventing future use) 96 * - swap device in use (could be disabled, preventing future use)
97 * - swap enabled (allows new allocations on swap) 97 * - swap enabled (allows new allocations on swap)
98 * - map info in /dev/drum 98 * - map info in /dev/drum
99 * - vnode pointer 99 * - vnode pointer
100 * for swap files only: 100 * for swap files only:
101 * - block size 101 * - block size
102 * - max byte count in buffer 102 * - max byte count in buffer
103 * - buffer 103 * - buffer
104 * 104 *
105 * userland controls and configures swap with the swapctl(2) system call. 105 * userland controls and configures swap with the swapctl(2) system call.
106 * the sys_swapctl performs the following operations: 106 * the sys_swapctl performs the following operations:
107 * [1] SWAP_NSWAP: returns the number of swap devices currently configured 107 * [1] SWAP_NSWAP: returns the number of swap devices currently configured
108 * [2] SWAP_STATS: given a pointer to an array of swapent structures 108 * [2] SWAP_STATS: given a pointer to an array of swapent structures
109 * (passed in via "arg") of a size passed in via "misc" ... we load 109 * (passed in via "arg") of a size passed in via "misc" ... we load
110 * the current swap config into the array. The actual work is done 110 * the current swap config into the array. The actual work is done
111 * in the uvm_swap_stats(9) function. 111 * in the uvm_swap_stats(9) function.
112 * [3] SWAP_ON: given a pathname in arg (could be device or file) and a 112 * [3] SWAP_ON: given a pathname in arg (could be device or file) and a
113 * priority in "misc", start swapping on it. 113 * priority in "misc", start swapping on it.
114 * [4] SWAP_OFF: as SWAP_ON, but stops swapping to a device 114 * [4] SWAP_OFF: as SWAP_ON, but stops swapping to a device
115 * [5] SWAP_CTL: changes the priority of a swap device (new priority in 115 * [5] SWAP_CTL: changes the priority of a swap device (new priority in
116 * "misc") 116 * "misc")
117 */ 117 */
118 118
119/* 119/*
120 * swapdev: describes a single swap partition/file 120 * swapdev: describes a single swap partition/file
121 * 121 *
122 * note the following should be true: 122 * note the following should be true:
123 * swd_inuse <= swd_nblks [number of blocks in use is <= total blocks] 123 * swd_inuse <= swd_nblks [number of blocks in use is <= total blocks]
124 * swd_nblks <= swd_mapsize [because mapsize includes miniroot+disklabel] 124 * swd_nblks <= swd_mapsize [because mapsize includes miniroot+disklabel]
125 */ 125 */
126struct swapdev { 126struct swapdev {
127 struct oswapent swd_ose; 127 struct oswapent swd_ose;
128#define swd_dev swd_ose.ose_dev /* device id */ 128#define swd_dev swd_ose.ose_dev /* device id */
129#define swd_flags swd_ose.ose_flags /* flags:inuse/enable/fake */ 129#define swd_flags swd_ose.ose_flags /* flags:inuse/enable/fake */
130#define swd_priority swd_ose.ose_priority /* our priority */ 130#define swd_priority swd_ose.ose_priority /* our priority */
131 /* also: swd_ose.ose_nblks, swd_ose.ose_inuse */ 131 /* also: swd_ose.ose_nblks, swd_ose.ose_inuse */
132 char *swd_path; /* saved pathname of device */ 132 char *swd_path; /* saved pathname of device */
133 int swd_pathlen; /* length of pathname */ 133 int swd_pathlen; /* length of pathname */
134 int swd_npages; /* #pages we can use */ 134 int swd_npages; /* #pages we can use */
135 int swd_npginuse; /* #pages in use */ 135 int swd_npginuse; /* #pages in use */
136 int swd_npgbad; /* #pages bad */ 136 int swd_npgbad; /* #pages bad */
137 int swd_drumoffset; /* page0 offset in drum */ 137 int swd_drumoffset; /* page0 offset in drum */
138 int swd_drumsize; /* #pages in drum */ 138 int swd_drumsize; /* #pages in drum */
139 blist_t swd_blist; /* blist for this swapdev */ 139 blist_t swd_blist; /* blist for this swapdev */
140 struct vnode *swd_vp; /* backing vnode */ 140 struct vnode *swd_vp; /* backing vnode */
141 CIRCLEQ_ENTRY(swapdev) swd_next; /* priority circleq */ 141 CIRCLEQ_ENTRY(swapdev) swd_next; /* priority circleq */
142 142
143 int swd_bsize; /* blocksize (bytes) */ 143 int swd_bsize; /* blocksize (bytes) */
144 int swd_maxactive; /* max active i/o reqs */ 144 int swd_maxactive; /* max active i/o reqs */
145 struct bufq_state *swd_tab; /* buffer list */ 145 struct bufq_state *swd_tab; /* buffer list */
146 int swd_active; /* number of active buffers */ 146 int swd_active; /* number of active buffers */
147}; 147};
148 148
149/* 149/*
150 * swap device priority entry; the list is kept sorted on `spi_priority'. 150 * swap device priority entry; the list is kept sorted on `spi_priority'.
151 */ 151 */
152struct swappri { 152struct swappri {
153 int spi_priority; /* priority */ 153 int spi_priority; /* priority */
154 CIRCLEQ_HEAD(spi_swapdev, swapdev) spi_swapdev; 154 CIRCLEQ_HEAD(spi_swapdev, swapdev) spi_swapdev;
155 /* circleq of swapdevs at this priority */ 155 /* circleq of swapdevs at this priority */
156 LIST_ENTRY(swappri) spi_swappri; /* global list of pri's */ 156 LIST_ENTRY(swappri) spi_swappri; /* global list of pri's */
157}; 157};
158 158
159/* 159/*
160 * The following two structures are used to keep track of data transfers 160 * The following two structures are used to keep track of data transfers
161 * on swap devices associated with regular files. 161 * on swap devices associated with regular files.
162 * NOTE: this code is more or less a copy of vnd.c; we use the same 162 * NOTE: this code is more or less a copy of vnd.c; we use the same
163 * structure names here to ease porting.. 163 * structure names here to ease porting..
164 */ 164 */
165struct vndxfer { 165struct vndxfer {
166 struct buf *vx_bp; /* Pointer to parent buffer */ 166 struct buf *vx_bp; /* Pointer to parent buffer */
167 struct swapdev *vx_sdp; 167 struct swapdev *vx_sdp;
168 int vx_error; 168 int vx_error;
169 int vx_pending; /* # of pending aux buffers */ 169 int vx_pending; /* # of pending aux buffers */
170 int vx_flags; 170 int vx_flags;
171#define VX_BUSY 1 171#define VX_BUSY 1
172#define VX_DEAD 2 172#define VX_DEAD 2
173}; 173};
174 174
175struct vndbuf { 175struct vndbuf {
176 struct buf vb_buf; 176 struct buf vb_buf;
177 struct vndxfer *vb_xfer; 177 struct vndxfer *vb_xfer;
178}; 178};
179 179
180 180
181/* 181/*
182 * We keep a of pool vndbuf's and vndxfer structures. 182 * We keep a of pool vndbuf's and vndxfer structures.
183 */ 183 */
184POOL_INIT(vndxfer_pool, sizeof(struct vndxfer), 0, 0, 0, "swp vnx", NULL, 184POOL_INIT(vndxfer_pool, sizeof(struct vndxfer), 0, 0, 0, "swp vnx", NULL,
185 IPL_BIO); 185 IPL_BIO);
186POOL_INIT(vndbuf_pool, sizeof(struct vndbuf), 0, 0, 0, "swp vnd", NULL, 186POOL_INIT(vndbuf_pool, sizeof(struct vndbuf), 0, 0, 0, "swp vnd", NULL,
187 IPL_BIO); 187 IPL_BIO);
188 188
189/* 189/*
190 * local variables 190 * local variables
191 */ 191 */
192MALLOC_DEFINE(M_VMSWAP, "VM swap", "VM swap structures"); 192MALLOC_DEFINE(M_VMSWAP, "VM swap", "VM swap structures");
193static vmem_t *swapmap; /* controls the mapping of /dev/drum */ 193static vmem_t *swapmap; /* controls the mapping of /dev/drum */
194 194
195/* list of all active swap devices [by priority] */ 195/* list of all active swap devices [by priority] */
196LIST_HEAD(swap_priority, swappri); 196LIST_HEAD(swap_priority, swappri);
197static struct swap_priority swap_priority; 197static struct swap_priority swap_priority;
198 198
199/* locks */ 199/* locks */
200static krwlock_t swap_syscall_lock; 200static krwlock_t swap_syscall_lock;
201 201
202/* workqueue and use counter for swap to regular files */ 202/* workqueue and use counter for swap to regular files */
203static int sw_reg_count = 0; 203static int sw_reg_count = 0;
204static struct workqueue *sw_reg_workqueue; 204static struct workqueue *sw_reg_workqueue;
205 205
206/* 206/*
207 * prototypes 207 * prototypes
208 */ 208 */
209static struct swapdev *swapdrum_getsdp(int); 209static struct swapdev *swapdrum_getsdp(int);
210 210
211static struct swapdev *swaplist_find(struct vnode *, bool); 211static struct swapdev *swaplist_find(struct vnode *, bool);
212static void swaplist_insert(struct swapdev *, 212static void swaplist_insert(struct swapdev *,
213 struct swappri *, int); 213 struct swappri *, int);
214static void swaplist_trim(void); 214static void swaplist_trim(void);
215 215
216static int swap_on(struct lwp *, struct swapdev *); 216static int swap_on(struct lwp *, struct swapdev *);
217static int swap_off(struct lwp *, struct swapdev *); 217static int swap_off(struct lwp *, struct swapdev *);
218 218
219static void uvm_swap_stats_locked(int, struct swapent *, int, register_t *); 219static void uvm_swap_stats_locked(int, struct swapent *, int, register_t *);
220 220
221static void sw_reg_strategy(struct swapdev *, struct buf *, int); 221static void sw_reg_strategy(struct swapdev *, struct buf *, int);
222static void sw_reg_biodone(struct buf *); 222static void sw_reg_biodone(struct buf *);
223static void sw_reg_iodone(struct work *wk, void *dummy); 223static void sw_reg_iodone(struct work *wk, void *dummy);
224static void sw_reg_start(struct swapdev *); 224static void sw_reg_start(struct swapdev *);
225 225
226static int uvm_swap_io(struct vm_page **, int, int, int); 226static int uvm_swap_io(struct vm_page **, int, int, int);
227 227
228/* 228/*
229 * uvm_swap_init: init the swap system data structures and locks 229 * uvm_swap_init: init the swap system data structures and locks
230 * 230 *
231 * => called at boot time from init_main.c after the filesystems 231 * => called at boot time from init_main.c after the filesystems
232 * are brought up (which happens after uvm_init()) 232 * are brought up (which happens after uvm_init())
233 */ 233 */
234void 234void
235uvm_swap_init(void) 235uvm_swap_init(void)
236{ 236{
237 UVMHIST_FUNC("uvm_swap_init"); 237 UVMHIST_FUNC("uvm_swap_init");
238 238
239 UVMHIST_CALLED(pdhist); 239 UVMHIST_CALLED(pdhist);
240 /* 240 /*
241 * first, init the swap list, its counter, and its lock. 241 * first, init the swap list, its counter, and its lock.
242 * then get a handle on the vnode for /dev/drum by using 242 * then get a handle on the vnode for /dev/drum by using
243 * the its dev_t number ("swapdev", from MD conf.c). 243 * the its dev_t number ("swapdev", from MD conf.c).
244 */ 244 */
245 245
246 LIST_INIT(&swap_priority); 246 LIST_INIT(&swap_priority);
247 uvmexp.nswapdev = 0; 247 uvmexp.nswapdev = 0;
248 rw_init(&swap_syscall_lock); 248 rw_init(&swap_syscall_lock);
249 cv_init(&uvm.scheduler_cv, "schedule"); 249 cv_init(&uvm.scheduler_cv, "schedule");
250 mutex_init(&uvm_swap_data_lock, MUTEX_DEFAULT, IPL_NONE); 250 mutex_init(&uvm_swap_data_lock, MUTEX_DEFAULT, IPL_NONE);
251 251
252 /* XXXSMP should be at IPL_VM, but for audio interrupt handlers. */ 252 /* XXXSMP should be at IPL_VM, but for audio interrupt handlers. */
253 mutex_init(&uvm_scheduler_mutex, MUTEX_SPIN, IPL_SCHED); 253 mutex_init(&uvm_scheduler_mutex, MUTEX_SPIN, IPL_SCHED);
254 254
255 if (bdevvp(swapdev, &swapdev_vp)) 255 if (bdevvp(swapdev, &swapdev_vp))
256 panic("uvm_swap_init: can't get vnode for swap device"); 256 panic("uvm_swap_init: can't get vnode for swap device");
257 if (vn_lock(swapdev_vp, LK_EXCLUSIVE | LK_RETRY)) 257 if (vn_lock(swapdev_vp, LK_EXCLUSIVE | LK_RETRY))
258 panic("uvm_swap_init: can't lock swap device"); 258 panic("uvm_swap_init: can't lock swap device");
259 if (VOP_OPEN(swapdev_vp, FREAD | FWRITE, NOCRED)) 259 if (VOP_OPEN(swapdev_vp, FREAD | FWRITE, NOCRED))
260 panic("uvm_swap_init: can't open swap device"); 260 panic("uvm_swap_init: can't open swap device");
261 VOP_UNLOCK(swapdev_vp, 0); 261 VOP_UNLOCK(swapdev_vp, 0);
262 262
263 /* 263 /*
264 * create swap block resource map to map /dev/drum. the range 264 * create swap block resource map to map /dev/drum. the range
265 * from 1 to INT_MAX allows 2 gigablocks of swap space. note 265 * from 1 to INT_MAX allows 2 gigablocks of swap space. note
266 * that block 0 is reserved (used to indicate an allocation 266 * that block 0 is reserved (used to indicate an allocation
267 * failure, or no allocation). 267 * failure, or no allocation).
268 */ 268 */
269 swapmap = vmem_create("swapmap", 1, INT_MAX - 1, 1, NULL, NULL, NULL, 0, 269 swapmap = vmem_create("swapmap", 1, INT_MAX - 1, 1, NULL, NULL, NULL, 0,
270 VM_NOSLEEP, IPL_NONE); 270 VM_NOSLEEP, IPL_NONE);
271 if (swapmap == 0) 271 if (swapmap == 0)
272 panic("uvm_swap_init: extent_create failed"); 272 panic("uvm_swap_init: extent_create failed");
273 273
274 /* 274 /*
275 * done! 275 * done!
276 */ 276 */
277 uvm.swap_running = true; 277 uvm.swap_running = true;
 278#ifdef __SWAP_BROKEN
 279 uvm.swapout_enabled = 0;
 280#else
278 uvm.swapout_enabled = 1; 281 uvm.swapout_enabled = 1;
 282#endif
279 UVMHIST_LOG(pdhist, "<- done", 0, 0, 0, 0); 283 UVMHIST_LOG(pdhist, "<- done", 0, 0, 0, 0);
280 284
281 sysctl_createv(NULL, 0, NULL, NULL, 285 sysctl_createv(NULL, 0, NULL, NULL,
282 CTLFLAG_READWRITE, 286 CTLFLAG_READWRITE,
283 CTLTYPE_INT, "swapout", 287 CTLTYPE_INT, "swapout",
284 SYSCTL_DESCR("Set 0 to disable swapout of kernel stacks"), 288 SYSCTL_DESCR("Set 0 to disable swapout of kernel stacks"),
285 NULL, 0, &uvm.swapout_enabled, 0, CTL_VM, CTL_CREATE, CTL_EOL); 289 NULL, 0, &uvm.swapout_enabled, 0, CTL_VM, CTL_CREATE, CTL_EOL);
286} 290}
287 291
288/* 292/*
289 * swaplist functions: functions that operate on the list of swap 293 * swaplist functions: functions that operate on the list of swap
290 * devices on the system. 294 * devices on the system.
291 */ 295 */
292 296
293/* 297/*
294 * swaplist_insert: insert swap device "sdp" into the global list 298 * swaplist_insert: insert swap device "sdp" into the global list
295 * 299 *
296 * => caller must hold both swap_syscall_lock and uvm_swap_data_lock 300 * => caller must hold both swap_syscall_lock and uvm_swap_data_lock
297 * => caller must provide a newly malloc'd swappri structure (we will 301 * => caller must provide a newly malloc'd swappri structure (we will
298 * FREE it if we don't need it... this it to prevent malloc blocking 302 * FREE it if we don't need it... this it to prevent malloc blocking
299 * here while adding swap) 303 * here while adding swap)
300 */ 304 */
301static void 305static void
302swaplist_insert(struct swapdev *sdp, struct swappri *newspp, int priority) 306swaplist_insert(struct swapdev *sdp, struct swappri *newspp, int priority)
303{ 307{
304 struct swappri *spp, *pspp; 308 struct swappri *spp, *pspp;
305 UVMHIST_FUNC("swaplist_insert"); UVMHIST_CALLED(pdhist); 309 UVMHIST_FUNC("swaplist_insert"); UVMHIST_CALLED(pdhist);
306 310
307 /* 311 /*
308 * find entry at or after which to insert the new device. 312 * find entry at or after which to insert the new device.
309 */ 313 */
310 pspp = NULL; 314 pspp = NULL;
311 LIST_FOREACH(spp, &swap_priority, spi_swappri) { 315 LIST_FOREACH(spp, &swap_priority, spi_swappri) {
312 if (priority <= spp->spi_priority) 316 if (priority <= spp->spi_priority)
313 break; 317 break;
314 pspp = spp; 318 pspp = spp;
315 } 319 }
316 320
317 /* 321 /*
318 * new priority? 322 * new priority?
319 */ 323 */
320 if (spp == NULL || spp->spi_priority != priority) { 324 if (spp == NULL || spp->spi_priority != priority) {
321 spp = newspp; /* use newspp! */ 325 spp = newspp; /* use newspp! */
322 UVMHIST_LOG(pdhist, "created new swappri = %d", 326 UVMHIST_LOG(pdhist, "created new swappri = %d",
323 priority, 0, 0, 0); 327 priority, 0, 0, 0);
324 328
325 spp->spi_priority = priority; 329 spp->spi_priority = priority;
326 CIRCLEQ_INIT(&spp->spi_swapdev); 330 CIRCLEQ_INIT(&spp->spi_swapdev);
327 331
328 if (pspp) 332 if (pspp)
329 LIST_INSERT_AFTER(pspp, spp, spi_swappri); 333 LIST_INSERT_AFTER(pspp, spp, spi_swappri);
330 else 334 else
331 LIST_INSERT_HEAD(&swap_priority, spp, spi_swappri); 335 LIST_INSERT_HEAD(&swap_priority, spp, spi_swappri);
332 } else { 336 } else {
333 /* we don't need a new priority structure, free it */ 337 /* we don't need a new priority structure, free it */
334 FREE(newspp, M_VMSWAP); 338 FREE(newspp, M_VMSWAP);
335 } 339 }
336 340
337 /* 341 /*
338 * priority found (or created). now insert on the priority's 342 * priority found (or created). now insert on the priority's
339 * circleq list and bump the total number of swapdevs. 343 * circleq list and bump the total number of swapdevs.
340 */ 344 */
341 sdp->swd_priority = priority; 345 sdp->swd_priority = priority;
342 CIRCLEQ_INSERT_TAIL(&spp->spi_swapdev, sdp, swd_next); 346 CIRCLEQ_INSERT_TAIL(&spp->spi_swapdev, sdp, swd_next);
343 uvmexp.nswapdev++; 347 uvmexp.nswapdev++;
344} 348}
345 349
346/* 350/*
347 * swaplist_find: find and optionally remove a swap device from the 351 * swaplist_find: find and optionally remove a swap device from the
348 * global list. 352 * global list.
349 * 353 *
350 * => caller must hold both swap_syscall_lock and uvm_swap_data_lock 354 * => caller must hold both swap_syscall_lock and uvm_swap_data_lock
351 * => we return the swapdev we found (and removed) 355 * => we return the swapdev we found (and removed)
352 */ 356 */
353static struct swapdev * 357static struct swapdev *
354swaplist_find(struct vnode *vp, bool remove) 358swaplist_find(struct vnode *vp, bool remove)
355{ 359{
356 struct swapdev *sdp; 360 struct swapdev *sdp;
357 struct swappri *spp; 361 struct swappri *spp;
358 362
359 /* 363 /*
360 * search the lists for the requested vp 364 * search the lists for the requested vp
361 */ 365 */
362 366
363 LIST_FOREACH(spp, &swap_priority, spi_swappri) { 367 LIST_FOREACH(spp, &swap_priority, spi_swappri) {
364 CIRCLEQ_FOREACH(sdp, &spp->spi_swapdev, swd_next) { 368 CIRCLEQ_FOREACH(sdp, &spp->spi_swapdev, swd_next) {
365 if (sdp->swd_vp == vp) { 369 if (sdp->swd_vp == vp) {
366 if (remove) { 370 if (remove) {
367 CIRCLEQ_REMOVE(&spp->spi_swapdev, 371 CIRCLEQ_REMOVE(&spp->spi_swapdev,
368 sdp, swd_next); 372 sdp, swd_next);
369 uvmexp.nswapdev--; 373 uvmexp.nswapdev--;
370 } 374 }
371 return(sdp); 375 return(sdp);
372 } 376 }
373 } 377 }
374 } 378 }
375 return (NULL); 379 return (NULL);
376} 380}
377 381
378/* 382/*
379 * swaplist_trim: scan priority list for empty priority entries and kill 383 * swaplist_trim: scan priority list for empty priority entries and kill
380 * them. 384 * them.
381 * 385 *
382 * => caller must hold both swap_syscall_lock and uvm_swap_data_lock 386 * => caller must hold both swap_syscall_lock and uvm_swap_data_lock
383 */ 387 */
384static void 388static void
385swaplist_trim(void) 389swaplist_trim(void)
386{ 390{
387 struct swappri *spp, *nextspp; 391 struct swappri *spp, *nextspp;
388 392
389 for (spp = LIST_FIRST(&swap_priority); spp != NULL; spp = nextspp) { 393 for (spp = LIST_FIRST(&swap_priority); spp != NULL; spp = nextspp) {
390 nextspp = LIST_NEXT(spp, spi_swappri); 394 nextspp = LIST_NEXT(spp, spi_swappri);
391 if (CIRCLEQ_FIRST(&spp->spi_swapdev) != 395 if (CIRCLEQ_FIRST(&spp->spi_swapdev) !=
392 (void *)&spp->spi_swapdev) 396 (void *)&spp->spi_swapdev)
393 continue; 397 continue;
394 LIST_REMOVE(spp, spi_swappri); 398 LIST_REMOVE(spp, spi_swappri);
395 free(spp, M_VMSWAP); 399 free(spp, M_VMSWAP);
396 } 400 }
397} 401}
398 402
399/* 403/*
400 * swapdrum_getsdp: given a page offset in /dev/drum, convert it back 404 * swapdrum_getsdp: given a page offset in /dev/drum, convert it back
401 * to the "swapdev" that maps that section of the drum. 405 * to the "swapdev" that maps that section of the drum.
402 * 406 *
403 * => each swapdev takes one big contig chunk of the drum 407 * => each swapdev takes one big contig chunk of the drum
404 * => caller must hold uvm_swap_data_lock 408 * => caller must hold uvm_swap_data_lock
405 */ 409 */
406static struct swapdev * 410static struct swapdev *
407swapdrum_getsdp(int pgno) 411swapdrum_getsdp(int pgno)
408{ 412{
409 struct swapdev *sdp; 413 struct swapdev *sdp;
410 struct swappri *spp; 414 struct swappri *spp;
411 415
412 LIST_FOREACH(spp, &swap_priority, spi_swappri) { 416 LIST_FOREACH(spp, &swap_priority, spi_swappri) {
413 CIRCLEQ_FOREACH(sdp, &spp->spi_swapdev, swd_next) { 417 CIRCLEQ_FOREACH(sdp, &spp->spi_swapdev, swd_next) {
414 if (sdp->swd_flags & SWF_FAKE) 418 if (sdp->swd_flags & SWF_FAKE)
415 continue; 419 continue;
416 if (pgno >= sdp->swd_drumoffset && 420 if (pgno >= sdp->swd_drumoffset &&
417 pgno < (sdp->swd_drumoffset + sdp->swd_drumsize)) { 421 pgno < (sdp->swd_drumoffset + sdp->swd_drumsize)) {
418 return sdp; 422 return sdp;
419 } 423 }
420 } 424 }
421 } 425 }
422 return NULL; 426 return NULL;
423} 427}
424 428
425 429
426/* 430/*
427 * sys_swapctl: main entry point for swapctl(2) system call 431 * sys_swapctl: main entry point for swapctl(2) system call
428 * [with two helper functions: swap_on and swap_off] 432 * [with two helper functions: swap_on and swap_off]
429 */ 433 */
430int 434int
431sys_swapctl(struct lwp *l, const struct sys_swapctl_args *uap, register_t *retval) 435sys_swapctl(struct lwp *l, const struct sys_swapctl_args *uap, register_t *retval)
432{ 436{
433 /* { 437 /* {
434 syscallarg(int) cmd; 438 syscallarg(int) cmd;
435 syscallarg(void *) arg; 439 syscallarg(void *) arg;
436 syscallarg(int) misc; 440 syscallarg(int) misc;
437 } */ 441 } */
438 struct vnode *vp; 442 struct vnode *vp;
439 struct nameidata nd; 443 struct nameidata nd;
440 struct swappri *spp; 444 struct swappri *spp;
441 struct swapdev *sdp; 445 struct swapdev *sdp;
442 struct swapent *sep; 446 struct swapent *sep;
443#define SWAP_PATH_MAX (PATH_MAX + 1) 447#define SWAP_PATH_MAX (PATH_MAX + 1)
444 char *userpath; 448 char *userpath;
445 size_t len; 449 size_t len;
446 int error, misc; 450 int error, misc;
447 int priority; 451 int priority;
448 UVMHIST_FUNC("sys_swapctl"); UVMHIST_CALLED(pdhist); 452 UVMHIST_FUNC("sys_swapctl"); UVMHIST_CALLED(pdhist);
449 453
450 misc = SCARG(uap, misc); 454 misc = SCARG(uap, misc);
451 455
452 /* 456 /*
453 * ensure serialized syscall access by grabbing the swap_syscall_lock 457 * ensure serialized syscall access by grabbing the swap_syscall_lock
454 */ 458 */
455 rw_enter(&swap_syscall_lock, RW_WRITER); 459 rw_enter(&swap_syscall_lock, RW_WRITER);
456 460
457 userpath = malloc(SWAP_PATH_MAX, M_TEMP, M_WAITOK); 461 userpath = malloc(SWAP_PATH_MAX, M_TEMP, M_WAITOK);
458 /* 462 /*
459 * we handle the non-priv NSWAP and STATS request first. 463 * we handle the non-priv NSWAP and STATS request first.
460 * 464 *
461 * SWAP_NSWAP: return number of config'd swap devices 465 * SWAP_NSWAP: return number of config'd swap devices
462 * [can also be obtained with uvmexp sysctl] 466 * [can also be obtained with uvmexp sysctl]
463 */ 467 */
464 if (SCARG(uap, cmd) == SWAP_NSWAP) { 468 if (SCARG(uap, cmd) == SWAP_NSWAP) {
465 UVMHIST_LOG(pdhist, "<- done SWAP_NSWAP=%d", uvmexp.nswapdev, 469 UVMHIST_LOG(pdhist, "<- done SWAP_NSWAP=%d", uvmexp.nswapdev,
466 0, 0, 0); 470 0, 0, 0);
467 *retval = uvmexp.nswapdev; 471 *retval = uvmexp.nswapdev;
468 error = 0; 472 error = 0;
469 goto out; 473 goto out;
470 } 474 }
471 475
472 /* 476 /*
473 * SWAP_STATS: get stats on current # of configured swap devs 477 * SWAP_STATS: get stats on current # of configured swap devs
474 * 478 *
475 * note that the swap_priority list can't change as long 479 * note that the swap_priority list can't change as long
476 * as we are holding the swap_syscall_lock. we don't want 480 * as we are holding the swap_syscall_lock. we don't want
477 * to grab the uvm_swap_data_lock because we may fault&sleep during 481 * to grab the uvm_swap_data_lock because we may fault&sleep during
478 * copyout() and we don't want to be holding that lock then! 482 * copyout() and we don't want to be holding that lock then!
479 */ 483 */
480 if (SCARG(uap, cmd) == SWAP_STATS 484 if (SCARG(uap, cmd) == SWAP_STATS
481#if defined(COMPAT_13) 485#if defined(COMPAT_13)
482 || SCARG(uap, cmd) == SWAP_OSTATS 486 || SCARG(uap, cmd) == SWAP_OSTATS
483#endif 487#endif
484 ) { 488 ) {
485 if ((size_t)misc > (size_t)uvmexp.nswapdev) 489 if ((size_t)misc > (size_t)uvmexp.nswapdev)
486 misc = uvmexp.nswapdev; 490 misc = uvmexp.nswapdev;
487#if defined(COMPAT_13) 491#if defined(COMPAT_13)
488 if (SCARG(uap, cmd) == SWAP_OSTATS) 492 if (SCARG(uap, cmd) == SWAP_OSTATS)
489 len = sizeof(struct oswapent) * misc; 493 len = sizeof(struct oswapent) * misc;
490 else 494 else
491#endif 495#endif
492 len = sizeof(struct swapent) * misc; 496 len = sizeof(struct swapent) * misc;
493 sep = (struct swapent *)malloc(len, M_TEMP, M_WAITOK); 497 sep = (struct swapent *)malloc(len, M_TEMP, M_WAITOK);
494 498
495 uvm_swap_stats_locked(SCARG(uap, cmd), sep, misc, retval); 499 uvm_swap_stats_locked(SCARG(uap, cmd), sep, misc, retval);
496 error = copyout(sep, SCARG(uap, arg), len); 500 error = copyout(sep, SCARG(uap, arg), len);
497 501
498 free(sep, M_TEMP); 502 free(sep, M_TEMP);
499 UVMHIST_LOG(pdhist, "<- done SWAP_STATS", 0, 0, 0, 0); 503 UVMHIST_LOG(pdhist, "<- done SWAP_STATS", 0, 0, 0, 0);
500 goto out; 504 goto out;
501 } 505 }
502 if (SCARG(uap, cmd) == SWAP_GETDUMPDEV) { 506 if (SCARG(uap, cmd) == SWAP_GETDUMPDEV) {
503 dev_t *devp = (dev_t *)SCARG(uap, arg); 507 dev_t *devp = (dev_t *)SCARG(uap, arg);
504 508
505 error = copyout(&dumpdev, devp, sizeof(dumpdev)); 509 error = copyout(&dumpdev, devp, sizeof(dumpdev));
506 goto out; 510 goto out;
507 } 511 }
508 512
509 /* 513 /*
510 * all other requests require superuser privs. verify. 514 * all other requests require superuser privs. verify.
511 */ 515 */
512 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SWAPCTL, 516 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SWAPCTL,
513 0, NULL, NULL, NULL))) 517 0, NULL, NULL, NULL)))
514 goto out; 518 goto out;
515 519
516 if (SCARG(uap, cmd) == SWAP_DUMPOFF) { 520 if (SCARG(uap, cmd) == SWAP_DUMPOFF) {
517 /* drop the current dump device */ 521 /* drop the current dump device */
518 dumpdev = NODEV; 522 dumpdev = NODEV;
519 dumpcdev = NODEV; 523 dumpcdev = NODEV;
520 cpu_dumpconf(); 524 cpu_dumpconf();
521 goto out; 525 goto out;
522 } 526 }
523 527
524 /* 528 /*
525 * at this point we expect a path name in arg. we will 529 * at this point we expect a path name in arg. we will
526 * use namei() to gain a vnode reference (vref), and lock 530 * use namei() to gain a vnode reference (vref), and lock
527 * the vnode (VOP_LOCK). 531 * the vnode (VOP_LOCK).
528 * 532 *
529 * XXX: a NULL arg means use the root vnode pointer (e.g. for 533 * XXX: a NULL arg means use the root vnode pointer (e.g. for
530 * miniroot) 534 * miniroot)
531 */ 535 */
532 if (SCARG(uap, arg) == NULL) { 536 if (SCARG(uap, arg) == NULL) {
533 vp = rootvp; /* miniroot */ 537 vp = rootvp; /* miniroot */
534 if (vget(vp, LK_EXCLUSIVE)) { 538 if (vget(vp, LK_EXCLUSIVE)) {
535 error = EBUSY; 539 error = EBUSY;
536 goto out; 540 goto out;
537 } 541 }
538 if (SCARG(uap, cmd) == SWAP_ON && 542 if (SCARG(uap, cmd) == SWAP_ON &&
539 copystr("miniroot", userpath, SWAP_PATH_MAX, &len)) 543 copystr("miniroot", userpath, SWAP_PATH_MAX, &len))
540 panic("swapctl: miniroot copy failed"); 544 panic("swapctl: miniroot copy failed");
541 } else { 545 } else {
542 int space; 546 int space;
543 char *where; 547 char *where;
544 548
545 if (SCARG(uap, cmd) == SWAP_ON) { 549 if (SCARG(uap, cmd) == SWAP_ON) {
546 if ((error = copyinstr(SCARG(uap, arg), userpath, 550 if ((error = copyinstr(SCARG(uap, arg), userpath,
547 SWAP_PATH_MAX, &len))) 551 SWAP_PATH_MAX, &len)))
548 goto out; 552 goto out;
549 space = UIO_SYSSPACE; 553 space = UIO_SYSSPACE;
550 where = userpath; 554 where = userpath;
551 } else { 555 } else {
552 space = UIO_USERSPACE; 556 space = UIO_USERSPACE;
553 where = (char *)SCARG(uap, arg); 557 where = (char *)SCARG(uap, arg);
554 } 558 }
555 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, 559 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT,
556 space, where); 560 space, where);
557 if ((error = namei(&nd))) 561 if ((error = namei(&nd)))
558 goto out; 562 goto out;
559 vp = nd.ni_vp; 563 vp = nd.ni_vp;
560 } 564 }
561 /* note: "vp" is referenced and locked */ 565 /* note: "vp" is referenced and locked */
562 566
563 error = 0; /* assume no error */ 567 error = 0; /* assume no error */
564 switch(SCARG(uap, cmd)) { 568 switch(SCARG(uap, cmd)) {
565 569
566 case SWAP_DUMPDEV: 570 case SWAP_DUMPDEV:
567 if (vp->v_type != VBLK) { 571 if (vp->v_type != VBLK) {
568 error = ENOTBLK; 572 error = ENOTBLK;
569 break; 573 break;
570 } 574 }
571 if (bdevsw_lookup(vp->v_rdev)) { 575 if (bdevsw_lookup(vp->v_rdev)) {
572 dumpdev = vp->v_rdev; 576 dumpdev = vp->v_rdev;
573 dumpcdev = devsw_blk2chr(dumpdev); 577 dumpcdev = devsw_blk2chr(dumpdev);
574 } else 578 } else
575 dumpdev = NODEV; 579 dumpdev = NODEV;
576 cpu_dumpconf(); 580 cpu_dumpconf();
577 break; 581 break;
578 582
579 case SWAP_CTL: 583 case SWAP_CTL:
580 /* 584 /*
581 * get new priority, remove old entry (if any) and then 585 * get new priority, remove old entry (if any) and then
582 * reinsert it in the correct place. finally, prune out 586 * reinsert it in the correct place. finally, prune out
583 * any empty priority structures. 587 * any empty priority structures.
584 */ 588 */
585 priority = SCARG(uap, misc); 589 priority = SCARG(uap, misc);
586 spp = malloc(sizeof *spp, M_VMSWAP, M_WAITOK); 590 spp = malloc(sizeof *spp, M_VMSWAP, M_WAITOK);
587 mutex_enter(&uvm_swap_data_lock); 591 mutex_enter(&uvm_swap_data_lock);
588 if ((sdp = swaplist_find(vp, true)) == NULL) { 592 if ((sdp = swaplist_find(vp, true)) == NULL) {
589 error = ENOENT; 593 error = ENOENT;
590 } else { 594 } else {
591 swaplist_insert(sdp, spp, priority); 595 swaplist_insert(sdp, spp, priority);
592 swaplist_trim(); 596 swaplist_trim();
593 } 597 }
594 mutex_exit(&uvm_swap_data_lock); 598 mutex_exit(&uvm_swap_data_lock);
595 if (error) 599 if (error)
596 free(spp, M_VMSWAP); 600 free(spp, M_VMSWAP);
597 break; 601 break;
598 602
599 case SWAP_ON: 603 case SWAP_ON:
600 604
601 /* 605 /*
602 * check for duplicates. if none found, then insert a 606 * check for duplicates. if none found, then insert a
603 * dummy entry on the list to prevent someone else from 607 * dummy entry on the list to prevent someone else from
604 * trying to enable this device while we are working on 608 * trying to enable this device while we are working on
605 * it. 609 * it.
606 */ 610 */
607 611
608 priority = SCARG(uap, misc); 612 priority = SCARG(uap, misc);
609 sdp = malloc(sizeof *sdp, M_VMSWAP, M_WAITOK); 613 sdp = malloc(sizeof *sdp, M_VMSWAP, M_WAITOK);
610 spp = malloc(sizeof *spp, M_VMSWAP, M_WAITOK); 614 spp = malloc(sizeof *spp, M_VMSWAP, M_WAITOK);
611 memset(sdp, 0, sizeof(*sdp)); 615 memset(sdp, 0, sizeof(*sdp));
612 sdp->swd_flags = SWF_FAKE; 616 sdp->swd_flags = SWF_FAKE;
613 sdp->swd_vp = vp; 617 sdp->swd_vp = vp;
614 sdp->swd_dev = (vp->v_type == VBLK) ? vp->v_rdev : NODEV; 618 sdp->swd_dev = (vp->v_type == VBLK) ? vp->v_rdev : NODEV;
615 bufq_alloc(&sdp->swd_tab, "disksort", BUFQ_SORT_RAWBLOCK); 619 bufq_alloc(&sdp->swd_tab, "disksort", BUFQ_SORT_RAWBLOCK);
616 mutex_enter(&uvm_swap_data_lock); 620 mutex_enter(&uvm_swap_data_lock);
617 if (swaplist_find(vp, false) != NULL) { 621 if (swaplist_find(vp, false) != NULL) {
618 error = EBUSY; 622 error = EBUSY;
619 mutex_exit(&uvm_swap_data_lock); 623 mutex_exit(&uvm_swap_data_lock);
620 bufq_free(sdp->swd_tab); 624 bufq_free(sdp->swd_tab);
621 free(sdp, M_VMSWAP); 625 free(sdp, M_VMSWAP);
622 free(spp, M_VMSWAP); 626 free(spp, M_VMSWAP);
623 break; 627 break;
624 } 628 }
625 swaplist_insert(sdp, spp, priority); 629 swaplist_insert(sdp, spp, priority);
626 mutex_exit(&uvm_swap_data_lock); 630 mutex_exit(&uvm_swap_data_lock);
627 631
628 sdp->swd_pathlen = len; 632 sdp->swd_pathlen = len;
629 sdp->swd_path = malloc(sdp->swd_pathlen, M_VMSWAP, M_WAITOK); 633 sdp->swd_path = malloc(sdp->swd_pathlen, M_VMSWAP, M_WAITOK);
630 if (copystr(userpath, sdp->swd_path, sdp->swd_pathlen, 0) != 0) 634 if (copystr(userpath, sdp->swd_path, sdp->swd_pathlen, 0) != 0)
631 panic("swapctl: copystr"); 635 panic("swapctl: copystr");
632 636
633 /* 637 /*
634 * we've now got a FAKE placeholder in the swap list. 638 * we've now got a FAKE placeholder in the swap list.
635 * now attempt to enable swap on it. if we fail, undo 639 * now attempt to enable swap on it. if we fail, undo
636 * what we've done and kill the fake entry we just inserted. 640 * what we've done and kill the fake entry we just inserted.
637 * if swap_on is a success, it will clear the SWF_FAKE flag 641 * if swap_on is a success, it will clear the SWF_FAKE flag
638 */ 642 */
639 643
640 if ((error = swap_on(l, sdp)) != 0) { 644 if ((error = swap_on(l, sdp)) != 0) {
641 mutex_enter(&uvm_swap_data_lock); 645 mutex_enter(&uvm_swap_data_lock);
642 (void) swaplist_find(vp, true); /* kill fake entry */ 646 (void) swaplist_find(vp, true); /* kill fake entry */
643 swaplist_trim(); 647 swaplist_trim();
644 mutex_exit(&uvm_swap_data_lock); 648 mutex_exit(&uvm_swap_data_lock);
645 bufq_free(sdp->swd_tab); 649 bufq_free(sdp->swd_tab);
646 free(sdp->swd_path, M_VMSWAP); 650 free(sdp->swd_path, M_VMSWAP);
647 free(sdp, M_VMSWAP); 651 free(sdp, M_VMSWAP);
648 break; 652 break;
649 } 653 }
650 break; 654 break;
651 655
652 case SWAP_OFF: 656 case SWAP_OFF:
653 mutex_enter(&uvm_swap_data_lock); 657 mutex_enter(&uvm_swap_data_lock);
654 if ((sdp = swaplist_find(vp, false)) == NULL) { 658 if ((sdp = swaplist_find(vp, false)) == NULL) {
655 mutex_exit(&uvm_swap_data_lock); 659 mutex_exit(&uvm_swap_data_lock);
656 error = ENXIO; 660 error = ENXIO;
657 break; 661 break;
658 } 662 }
659 663
660 /* 664 /*
661 * If a device isn't in use or enabled, we 665 * If a device isn't in use or enabled, we
662 * can't stop swapping from it (again). 666 * can't stop swapping from it (again).
663 */ 667 */
664 if ((sdp->swd_flags & (SWF_INUSE|SWF_ENABLE)) == 0) { 668 if ((sdp->swd_flags & (SWF_INUSE|SWF_ENABLE)) == 0) {
665 mutex_exit(&uvm_swap_data_lock); 669 mutex_exit(&uvm_swap_data_lock);
666 error = EBUSY; 670 error = EBUSY;
667 break; 671 break;
668 } 672 }
669 673
670 /* 674 /*
671 * do the real work. 675 * do the real work.
672 */ 676 */
673 error = swap_off(l, sdp); 677 error = swap_off(l, sdp);
674 break; 678 break;
675 679
676 default: 680 default:
677 error = EINVAL; 681 error = EINVAL;
678 } 682 }
679 683
680 /* 684 /*
681 * done! release the ref gained by namei() and unlock. 685 * done! release the ref gained by namei() and unlock.
682 */ 686 */
683 vput(vp); 687 vput(vp);
684 688
685out: 689out:
686 free(userpath, M_TEMP); 690 free(userpath, M_TEMP);
687 rw_exit(&swap_syscall_lock); 691 rw_exit(&swap_syscall_lock);
688 692
689 UVMHIST_LOG(pdhist, "<- done! error=%d", error, 0, 0, 0); 693 UVMHIST_LOG(pdhist, "<- done! error=%d", error, 0, 0, 0);
690 return (error); 694 return (error);
691} 695}
692 696
693/* 697/*
694 * swap_stats: implements swapctl(SWAP_STATS). The function is kept 698 * swap_stats: implements swapctl(SWAP_STATS). The function is kept
695 * away from sys_swapctl() in order to allow COMPAT_* swapctl() 699 * away from sys_swapctl() in order to allow COMPAT_* swapctl()
696 * emulation to use it directly without going through sys_swapctl(). 700 * emulation to use it directly without going through sys_swapctl().
697 * The problem with using sys_swapctl() there is that it involves 701 * The problem with using sys_swapctl() there is that it involves
698 * copying the swapent array to the stackgap, and this array's size 702 * copying the swapent array to the stackgap, and this array's size
699 * is not known at build time. Hence it would not be possible to 703 * is not known at build time. Hence it would not be possible to
700 * ensure it would fit in the stackgap in any case. 704 * ensure it would fit in the stackgap in any case.
701 */ 705 */
702void 706void
703uvm_swap_stats(int cmd, struct swapent *sep, int sec, register_t *retval) 707uvm_swap_stats(int cmd, struct swapent *sep, int sec, register_t *retval)
704{ 708{
705 709
706 rw_enter(&swap_syscall_lock, RW_READER); 710 rw_enter(&swap_syscall_lock, RW_READER);
707 uvm_swap_stats_locked(cmd, sep, sec, retval); 711 uvm_swap_stats_locked(cmd, sep, sec, retval);
708 rw_exit(&swap_syscall_lock); 712 rw_exit(&swap_syscall_lock);
709} 713}
710 714
711static void 715static void
712uvm_swap_stats_locked(int cmd, struct swapent *sep, int sec, register_t *retval) 716uvm_swap_stats_locked(int cmd, struct swapent *sep, int sec, register_t *retval)
713{ 717{
714 struct swappri *spp; 718 struct swappri *spp;
715 struct swapdev *sdp; 719 struct swapdev *sdp;
716 int count = 0; 720 int count = 0;
717 721
718 LIST_FOREACH(spp, &swap_priority, spi_swappri) { 722 LIST_FOREACH(spp, &swap_priority, spi_swappri) {
719 for (sdp = CIRCLEQ_FIRST(&spp->spi_swapdev); 723 for (sdp = CIRCLEQ_FIRST(&spp->spi_swapdev);
720 sdp != (void *)&spp->spi_swapdev && sec-- > 0; 724 sdp != (void *)&spp->spi_swapdev && sec-- > 0;
721 sdp = CIRCLEQ_NEXT(sdp, swd_next)) { 725 sdp = CIRCLEQ_NEXT(sdp, swd_next)) {
722 /* 726 /*
723 * backwards compatibility for system call. 727 * backwards compatibility for system call.
724 * note that we use 'struct oswapent' as an 728 * note that we use 'struct oswapent' as an
725 * overlay into both 'struct swapdev' and 729 * overlay into both 'struct swapdev' and
726 * the userland 'struct swapent', as we 730 * the userland 'struct swapent', as we
727 * want to retain backwards compatibility 731 * want to retain backwards compatibility
728 * with NetBSD 1.3. 732 * with NetBSD 1.3.
729 */ 733 */
730 sdp->swd_ose.ose_inuse = 734 sdp->swd_ose.ose_inuse =
731 btodb((uint64_t)sdp->swd_npginuse << 735 btodb((uint64_t)sdp->swd_npginuse <<
732 PAGE_SHIFT); 736 PAGE_SHIFT);
733 (void)memcpy(sep, &sdp->swd_ose, 737 (void)memcpy(sep, &sdp->swd_ose,
734 sizeof(struct oswapent)); 738 sizeof(struct oswapent));
735 739
736 /* now copy out the path if necessary */ 740 /* now copy out the path if necessary */
737#if !defined(COMPAT_13) 741#if !defined(COMPAT_13)
738 (void) cmd; 742 (void) cmd;
739#endif 743#endif
740#if defined(COMPAT_13) 744#if defined(COMPAT_13)
741 if (cmd == SWAP_STATS) 745 if (cmd == SWAP_STATS)
742#endif 746#endif
743 (void)memcpy(&sep->se_path, sdp->swd_path, 747 (void)memcpy(&sep->se_path, sdp->swd_path,
744 sdp->swd_pathlen); 748 sdp->swd_pathlen);
745 749
746 count++; 750 count++;
747#if defined(COMPAT_13) 751#if defined(COMPAT_13)
748 if (cmd == SWAP_OSTATS) 752 if (cmd == SWAP_OSTATS)
749 sep = (struct swapent *) 753 sep = (struct swapent *)
750 ((struct oswapent *)sep + 1); 754 ((struct oswapent *)sep + 1);
751 else 755 else
752#endif 756#endif
753 sep++; 757 sep++;
754 } 758 }
755 } 759 }
756 760
757 *retval = count; 761 *retval = count;
758 return; 762 return;
759} 763}
760 764
761/* 765/*
762 * swap_on: attempt to enable a swapdev for swapping. note that the 766 * swap_on: attempt to enable a swapdev for swapping. note that the
763 * swapdev is already on the global list, but disabled (marked 767 * swapdev is already on the global list, but disabled (marked
764 * SWF_FAKE). 768 * SWF_FAKE).
765 * 769 *
766 * => we avoid the start of the disk (to protect disk labels) 770 * => we avoid the start of the disk (to protect disk labels)
767 * => we also avoid the miniroot, if we are swapping to root. 771 * => we also avoid the miniroot, if we are swapping to root.
768 * => caller should leave uvm_swap_data_lock unlocked, we may lock it 772 * => caller should leave uvm_swap_data_lock unlocked, we may lock it
769 * if needed. 773 * if needed.
770 */ 774 */
771static int 775static int
772swap_on(struct lwp *l, struct swapdev *sdp) 776swap_on(struct lwp *l, struct swapdev *sdp)
773{ 777{
774 struct vnode *vp; 778 struct vnode *vp;
775 int error, npages, nblocks, size; 779 int error, npages, nblocks, size;
776 long addr; 780 long addr;
777 u_long result; 781 u_long result;
778 struct vattr va; 782 struct vattr va;
779#ifdef NFS 783#ifdef NFS
780 extern int (**nfsv2_vnodeop_p)(void *); 784 extern int (**nfsv2_vnodeop_p)(void *);
781#endif /* NFS */ 785#endif /* NFS */
782 const struct bdevsw *bdev; 786 const struct bdevsw *bdev;
783 dev_t dev; 787 dev_t dev;
784 UVMHIST_FUNC("swap_on"); UVMHIST_CALLED(pdhist); 788 UVMHIST_FUNC("swap_on"); UVMHIST_CALLED(pdhist);
785 789
786 /* 790 /*
787 * we want to enable swapping on sdp. the swd_vp contains 791 * we want to enable swapping on sdp. the swd_vp contains
788 * the vnode we want (locked and ref'd), and the swd_dev 792 * the vnode we want (locked and ref'd), and the swd_dev
789 * contains the dev_t of the file, if it a block device. 793 * contains the dev_t of the file, if it a block device.
790 */ 794 */
791 795
792 vp = sdp->swd_vp; 796 vp = sdp->swd_vp;
793 dev = sdp->swd_dev; 797 dev = sdp->swd_dev;
794 798
795 /* 799 /*
796 * open the swap file (mostly useful for block device files to 800 * open the swap file (mostly useful for block device files to
797 * let device driver know what is up). 801 * let device driver know what is up).
798 * 802 *
799 * we skip the open/close for root on swap because the root 803 * we skip the open/close for root on swap because the root
800 * has already been opened when root was mounted (mountroot). 804 * has already been opened when root was mounted (mountroot).
801 */ 805 */
802 if (vp != rootvp) { 806 if (vp != rootvp) {
803 if ((error = VOP_OPEN(vp, FREAD|FWRITE, l->l_cred))) 807 if ((error = VOP_OPEN(vp, FREAD|FWRITE, l->l_cred)))
804 return (error); 808 return (error);
805 } 809 }
806 810
807 /* XXX this only works for block devices */ 811 /* XXX this only works for block devices */
808 UVMHIST_LOG(pdhist, " dev=%d, major(dev)=%d", dev, major(dev), 0,0); 812 UVMHIST_LOG(pdhist, " dev=%d, major(dev)=%d", dev, major(dev), 0,0);
809 813
810 /* 814 /*
811 * we now need to determine the size of the swap area. for 815 * we now need to determine the size of the swap area. for
812 * block specials we can call the d_psize function. 816 * block specials we can call the d_psize function.
813 * for normal files, we must stat [get attrs]. 817 * for normal files, we must stat [get attrs].
814 * 818 *
815 * we put the result in nblks. 819 * we put the result in nblks.
816 * for normal files, we also want the filesystem block size 820 * for normal files, we also want the filesystem block size
817 * (which we get with statfs). 821 * (which we get with statfs).
818 */ 822 */
819 switch (vp->v_type) { 823 switch (vp->v_type) {
820 case VBLK: 824 case VBLK:
821 bdev = bdevsw_lookup(dev); 825 bdev = bdevsw_lookup(dev);
822 if (bdev == NULL || bdev->d_psize == NULL || 826 if (bdev == NULL || bdev->d_psize == NULL ||
823 (nblocks = (*bdev->d_psize)(dev)) == -1) { 827 (nblocks = (*bdev->d_psize)(dev)) == -1) {
824 error = ENXIO; 828 error = ENXIO;
825 goto bad; 829 goto bad;
826 } 830 }
827 break; 831 break;
828 832
829 case VREG: 833 case VREG:
830 if ((error = VOP_GETATTR(vp, &va, l->l_cred))) 834 if ((error = VOP_GETATTR(vp, &va, l->l_cred)))
831 goto bad; 835 goto bad;
832 nblocks = (int)btodb(va.va_size); 836 nblocks = (int)btodb(va.va_size);
833 if ((error = 837 if ((error =
834 VFS_STATVFS(vp->v_mount, &vp->v_mount->mnt_stat)) != 0) 838 VFS_STATVFS(vp->v_mount, &vp->v_mount->mnt_stat)) != 0)
835 goto bad; 839 goto bad;
836 840
837 sdp->swd_bsize = vp->v_mount->mnt_stat.f_iosize; 841 sdp->swd_bsize = vp->v_mount->mnt_stat.f_iosize;
838 /* 842 /*
839 * limit the max # of outstanding I/O requests we issue 843 * limit the max # of outstanding I/O requests we issue
840 * at any one time. take it easy on NFS servers. 844 * at any one time. take it easy on NFS servers.
841 */ 845 */
842#ifdef NFS 846#ifdef NFS
843 if (vp->v_op == nfsv2_vnodeop_p) 847 if (vp->v_op == nfsv2_vnodeop_p)
844 sdp->swd_maxactive = 2; /* XXX */ 848 sdp->swd_maxactive = 2; /* XXX */
845 else 849 else
846#endif /* NFS */ 850#endif /* NFS */
847 sdp->swd_maxactive = 8; /* XXX */ 851 sdp->swd_maxactive = 8; /* XXX */
848 break; 852 break;
849 853
850 default: 854 default:
851 error = ENXIO; 855 error = ENXIO;
852 goto bad; 856 goto bad;
853 } 857 }
854 858
855 /* 859 /*
856 * save nblocks in a safe place and convert to pages. 860 * save nblocks in a safe place and convert to pages.
857 */ 861 */
858 862
859 sdp->swd_ose.ose_nblks = nblocks; 863 sdp->swd_ose.ose_nblks = nblocks;
860 npages = dbtob((uint64_t)nblocks) >> PAGE_SHIFT; 864 npages = dbtob((uint64_t)nblocks) >> PAGE_SHIFT;
861 865
862 /* 866 /*
863 * for block special files, we want to make sure that leave 867 * for block special files, we want to make sure that leave
864 * the disklabel and bootblocks alone, so we arrange to skip 868 * the disklabel and bootblocks alone, so we arrange to skip
865 * over them (arbitrarily choosing to skip PAGE_SIZE bytes). 869 * over them (arbitrarily choosing to skip PAGE_SIZE bytes).
866 * note that because of this the "size" can be less than the 870 * note that because of this the "size" can be less than the
867 * actual number of blocks on the device. 871 * actual number of blocks on the device.
868 */ 872 */
869 if (vp->v_type == VBLK) { 873 if (vp->v_type == VBLK) {
870 /* we use pages 1 to (size - 1) [inclusive] */ 874 /* we use pages 1 to (size - 1) [inclusive] */
871 size = npages - 1; 875 size = npages - 1;
872 addr = 1; 876 addr = 1;
873 } else { 877 } else {
874 /* we use pages 0 to (size - 1) [inclusive] */ 878 /* we use pages 0 to (size - 1) [inclusive] */
875 size = npages; 879 size = npages;
876 addr = 0; 880 addr = 0;
877 } 881 }
878 882
879 /* 883 /*
880 * make sure we have enough blocks for a reasonable sized swap 884 * make sure we have enough blocks for a reasonable sized swap
881 * area. we want at least one page. 885 * area. we want at least one page.
882 */ 886 */
883 887
884 if (size < 1) { 888 if (size < 1) {
885 UVMHIST_LOG(pdhist, " size <= 1!!", 0, 0, 0, 0); 889 UVMHIST_LOG(pdhist, " size <= 1!!", 0, 0, 0, 0);
886 error = EINVAL; 890 error = EINVAL;
887 goto bad; 891 goto bad;
888 } 892 }
889 893
890 UVMHIST_LOG(pdhist, " dev=%x: size=%d addr=%ld\n", dev, size, addr, 0); 894 UVMHIST_LOG(pdhist, " dev=%x: size=%d addr=%ld\n", dev, size, addr, 0);
891 895
892 /* 896 /*
893 * now we need to allocate an extent to manage this swap device 897 * now we need to allocate an extent to manage this swap device
894 */ 898 */
895 899
896 sdp->swd_blist = blist_create(npages); 900 sdp->swd_blist = blist_create(npages);
897 /* mark all expect the `saved' region free. */ 901 /* mark all expect the `saved' region free. */
898 blist_free(sdp->swd_blist, addr, size); 902 blist_free(sdp->swd_blist, addr, size);
899 903
900 /* 904 /*
901 * if the vnode we are swapping to is the root vnode 905 * if the vnode we are swapping to is the root vnode
902 * (i.e. we are swapping to the miniroot) then we want 906 * (i.e. we are swapping to the miniroot) then we want
903 * to make sure we don't overwrite it. do a statfs to 907 * to make sure we don't overwrite it. do a statfs to
904 * find its size and skip over it. 908 * find its size and skip over it.
905 */ 909 */
906 if (vp == rootvp) { 910 if (vp == rootvp) {
907 struct mount *mp; 911 struct mount *mp;
908 struct statvfs *sp; 912 struct statvfs *sp;
909 int rootblocks, rootpages; 913 int rootblocks, rootpages;
910 914
911 mp = rootvnode->v_mount; 915 mp = rootvnode->v_mount;
912 sp = &mp->mnt_stat; 916 sp = &mp->mnt_stat;
913 rootblocks = sp->f_blocks * btodb(sp->f_frsize); 917 rootblocks = sp->f_blocks * btodb(sp->f_frsize);
914 /* 918 /*
915 * XXX: sp->f_blocks isn't the total number of 919 * XXX: sp->f_blocks isn't the total number of
916 * blocks in the filesystem, it's the number of 920 * blocks in the filesystem, it's the number of
917 * data blocks. so, our rootblocks almost 921 * data blocks. so, our rootblocks almost
918 * definitely underestimates the total size 922 * definitely underestimates the total size
919 * of the filesystem - how badly depends on the 923 * of the filesystem - how badly depends on the
920 * details of the filesystem type. there isn't 924 * details of the filesystem type. there isn't
921 * an obvious way to deal with this cleanly 925 * an obvious way to deal with this cleanly
922 * and perfectly, so for now we just pad our 926 * and perfectly, so for now we just pad our
923 * rootblocks estimate with an extra 5 percent. 927 * rootblocks estimate with an extra 5 percent.
924 */ 928 */
925 rootblocks += (rootblocks >> 5) + 929 rootblocks += (rootblocks >> 5) +
926 (rootblocks >> 6) + 930 (rootblocks >> 6) +
927 (rootblocks >> 7); 931 (rootblocks >> 7);
928 rootpages = round_page(dbtob(rootblocks)) >> PAGE_SHIFT; 932 rootpages = round_page(dbtob(rootblocks)) >> PAGE_SHIFT;
929 if (rootpages > size) 933 if (rootpages > size)
930 panic("swap_on: miniroot larger than swap?"); 934 panic("swap_on: miniroot larger than swap?");
931 935
932 if (rootpages != blist_fill(sdp->swd_blist, addr, rootpages)) { 936 if (rootpages != blist_fill(sdp->swd_blist, addr, rootpages)) {
933 panic("swap_on: unable to preserve miniroot"); 937 panic("swap_on: unable to preserve miniroot");
934 } 938 }
935 939
936 size -= rootpages; 940 size -= rootpages;
937 printf("Preserved %d pages of miniroot ", rootpages); 941 printf("Preserved %d pages of miniroot ", rootpages);
938 printf("leaving %d pages of swap\n", size); 942 printf("leaving %d pages of swap\n", size);
939 } 943 }
940 944
941 /* 945 /*
942 * add a ref to vp to reflect usage as a swap device. 946 * add a ref to vp to reflect usage as a swap device.
943 */ 947 */
944 vref(vp); 948 vref(vp);
945 949
946 /* 950 /*
947 * now add the new swapdev to the drum and enable. 951 * now add the new swapdev to the drum and enable.
948 */ 952 */
949 result = vmem_alloc(swapmap, npages, VM_BESTFIT | VM_SLEEP); 953 result = vmem_alloc(swapmap, npages, VM_BESTFIT | VM_SLEEP);
950 if (result == 0) 954 if (result == 0)
951 panic("swapdrum_add"); 955 panic("swapdrum_add");
952 /* 956 /*
953 * If this is the first regular swap create the workqueue. 957 * If this is the first regular swap create the workqueue.
954 * => Protected by swap_syscall_lock. 958 * => Protected by swap_syscall_lock.
955 */ 959 */
956 if (vp->v_type != VBLK) { 960 if (vp->v_type != VBLK) {
957 if (sw_reg_count++ == 0) { 961 if (sw_reg_count++ == 0) {
958 KASSERT(sw_reg_workqueue == NULL); 962 KASSERT(sw_reg_workqueue == NULL);
959 if (workqueue_create(&sw_reg_workqueue, "swapiod", 963 if (workqueue_create(&sw_reg_workqueue, "swapiod",
960 sw_reg_iodone, NULL, PRIBIO, IPL_BIO, 0) != 0) 964 sw_reg_iodone, NULL, PRIBIO, IPL_BIO, 0) != 0)
961 panic("swap_add: workqueue_create failed"); 965 panic("swap_add: workqueue_create failed");
962 } 966 }
963 } 967 }
964 968
965 sdp->swd_drumoffset = (int)result; 969 sdp->swd_drumoffset = (int)result;
966 sdp->swd_drumsize = npages; 970 sdp->swd_drumsize = npages;
967 sdp->swd_npages = size; 971 sdp->swd_npages = size;
968 mutex_enter(&uvm_swap_data_lock); 972 mutex_enter(&uvm_swap_data_lock);
969 sdp->swd_flags &= ~SWF_FAKE; /* going live */ 973 sdp->swd_flags &= ~SWF_FAKE; /* going live */
970 sdp->swd_flags |= (SWF_INUSE|SWF_ENABLE); 974 sdp->swd_flags |= (SWF_INUSE|SWF_ENABLE);
971 uvmexp.swpages += size; 975 uvmexp.swpages += size;
972 uvmexp.swpgavail += size; 976 uvmexp.swpgavail += size;
973 mutex_exit(&uvm_swap_data_lock); 977 mutex_exit(&uvm_swap_data_lock);
974 return (0); 978 return (0);
975 979
976 /* 980 /*
977 * failure: clean up and return error. 981 * failure: clean up and return error.
978 */ 982 */
979 983
980bad: 984bad:
981 if (sdp->swd_blist) { 985 if (sdp->swd_blist) {
982 blist_destroy(sdp->swd_blist); 986 blist_destroy(sdp->swd_blist);
983 } 987 }
984 if (vp != rootvp) { 988 if (vp != rootvp) {
985 (void)VOP_CLOSE(vp, FREAD|FWRITE, l->l_cred); 989 (void)VOP_CLOSE(vp, FREAD|FWRITE, l->l_cred);
986 } 990 }
987 return (error); 991 return (error);
988} 992}
989 993
990/* 994/*
991 * swap_off: stop swapping on swapdev 995 * swap_off: stop swapping on swapdev
992 * 996 *
993 * => swap data should be locked, we will unlock. 997 * => swap data should be locked, we will unlock.
994 */ 998 */
995static int 999static int
996swap_off(struct lwp *l, struct swapdev *sdp) 1000swap_off(struct lwp *l, struct swapdev *sdp)
997{ 1001{
998 int npages = sdp->swd_npages; 1002 int npages = sdp->swd_npages;
999 int error = 0; 1003 int error = 0;
1000 1004
1001 UVMHIST_FUNC("swap_off"); UVMHIST_CALLED(pdhist); 1005 UVMHIST_FUNC("swap_off"); UVMHIST_CALLED(pdhist);
1002 UVMHIST_LOG(pdhist, " dev=%x, npages=%d", sdp->swd_dev,npages,0,0); 1006 UVMHIST_LOG(pdhist, " dev=%x, npages=%d", sdp->swd_dev,npages,0,0);
1003 1007
1004 /* disable the swap area being removed */ 1008 /* disable the swap area being removed */
1005 sdp->swd_flags &= ~SWF_ENABLE; 1009 sdp->swd_flags &= ~SWF_ENABLE;
1006 uvmexp.swpgavail -= npages; 1010 uvmexp.swpgavail -= npages;
1007 mutex_exit(&uvm_swap_data_lock); 1011 mutex_exit(&uvm_swap_data_lock);
1008 1012
1009 /* 1013 /*
1010 * the idea is to find all the pages that are paged out to this 1014 * the idea is to find all the pages that are paged out to this
1011 * device, and page them all in. in uvm, swap-backed pageable 1015 * device, and page them all in. in uvm, swap-backed pageable
1012 * memory can take two forms: aobjs and anons. call the 1016 * memory can take two forms: aobjs and anons. call the
1013 * swapoff hook for each subsystem to bring in pages. 1017 * swapoff hook for each subsystem to bring in pages.
1014 */ 1018 */
1015 1019
1016 if (uao_swap_off(sdp->swd_drumoffset, 1020 if (uao_swap_off(sdp->swd_drumoffset,
1017 sdp->swd_drumoffset + sdp->swd_drumsize) || 1021 sdp->swd_drumoffset + sdp->swd_drumsize) ||
1018 amap_swap_off(sdp->swd_drumoffset, 1022 amap_swap_off(sdp->swd_drumoffset,
1019 sdp->swd_drumoffset + sdp->swd_drumsize)) { 1023 sdp->swd_drumoffset + sdp->swd_drumsize)) {
1020 error = ENOMEM; 1024 error = ENOMEM;
1021 } else if (sdp->swd_npginuse > sdp->swd_npgbad) { 1025 } else if (sdp->swd_npginuse > sdp->swd_npgbad) {
1022 error = EBUSY; 1026 error = EBUSY;
1023 } 1027 }
1024 1028
1025 if (error) { 1029 if (error) {
1026 mutex_enter(&uvm_swap_data_lock); 1030 mutex_enter(&uvm_swap_data_lock);
1027 sdp->swd_flags |= SWF_ENABLE; 1031 sdp->swd_flags |= SWF_ENABLE;
1028 uvmexp.swpgavail += npages; 1032 uvmexp.swpgavail += npages;
1029 mutex_exit(&uvm_swap_data_lock); 1033 mutex_exit(&uvm_swap_data_lock);
1030 1034
1031 return error; 1035 return error;
1032 } 1036 }
1033 1037
1034 /* 1038 /*
1035 * If this is the last regular swap destroy the workqueue. 1039 * If this is the last regular swap destroy the workqueue.
1036 * => Protected by swap_syscall_lock. 1040 * => Protected by swap_syscall_lock.
1037 */ 1041 */
1038 if (sdp->swd_vp->v_type != VBLK) { 1042 if (sdp->swd_vp->v_type != VBLK) {
1039 KASSERT(sw_reg_count > 0); 1043 KASSERT(sw_reg_count > 0);
1040 KASSERT(sw_reg_workqueue != NULL); 1044 KASSERT(sw_reg_workqueue != NULL);
1041 if (--sw_reg_count == 0) { 1045 if (--sw_reg_count == 0) {
1042 workqueue_destroy(sw_reg_workqueue); 1046 workqueue_destroy(sw_reg_workqueue);
1043 sw_reg_workqueue = NULL; 1047 sw_reg_workqueue = NULL;
1044 } 1048 }
1045 } 1049 }
1046 1050
1047 /* 1051 /*
1048 * done with the vnode. 1052 * done with the vnode.
1049 * drop our ref on the vnode before calling VOP_CLOSE() 1053 * drop our ref on the vnode before calling VOP_CLOSE()
1050 * so that spec_close() can tell if this is the last close. 1054 * so that spec_close() can tell if this is the last close.
1051 */ 1055 */
1052 vrele(sdp->swd_vp); 1056 vrele(sdp->swd_vp);
1053 if (sdp->swd_vp != rootvp) { 1057 if (sdp->swd_vp != rootvp) {
1054 (void) VOP_CLOSE(sdp->swd_vp, FREAD|FWRITE, l->l_cred); 1058 (void) VOP_CLOSE(sdp->swd_vp, FREAD|FWRITE, l->l_cred);
1055 } 1059 }
1056 1060
1057 mutex_enter(&uvm_swap_data_lock); 1061 mutex_enter(&uvm_swap_data_lock);
1058 uvmexp.swpages -= npages; 1062 uvmexp.swpages -= npages;
1059 uvmexp.swpginuse -= sdp->swd_npgbad; 1063 uvmexp.swpginuse -= sdp->swd_npgbad;
1060 1064
1061 if (swaplist_find(sdp->swd_vp, true) == NULL) 1065 if (swaplist_find(sdp->swd_vp, true) == NULL)
1062 panic("swap_off: swapdev not in list"); 1066 panic("swap_off: swapdev not in list");
1063 swaplist_trim(); 1067 swaplist_trim();
1064 mutex_exit(&uvm_swap_data_lock); 1068 mutex_exit(&uvm_swap_data_lock);
1065 1069
1066 /* 1070 /*
1067 * free all resources! 1071 * free all resources!
1068 */ 1072 */
1069 vmem_free(swapmap, sdp->swd_drumoffset, sdp->swd_drumsize); 1073 vmem_free(swapmap, sdp->swd_drumoffset, sdp->swd_drumsize);
1070 blist_destroy(sdp->swd_blist); 1074 blist_destroy(sdp->swd_blist);
1071 bufq_free(sdp->swd_tab); 1075 bufq_free(sdp->swd_tab);
1072 free(sdp, M_VMSWAP); 1076 free(sdp, M_VMSWAP);
1073 return (0); 1077 return (0);
1074} 1078}
1075 1079
1076/* 1080/*
1077 * /dev/drum interface and i/o functions 1081 * /dev/drum interface and i/o functions
1078 */ 1082 */
1079 1083
1080/* 1084/*
1081 * swstrategy: perform I/O on the drum 1085 * swstrategy: perform I/O on the drum
1082 * 1086 *
1083 * => we must map the i/o request from the drum to the correct swapdev. 1087 * => we must map the i/o request from the drum to the correct swapdev.
1084 */ 1088 */
1085static void 1089static void
1086swstrategy(struct buf *bp) 1090swstrategy(struct buf *bp)
1087{ 1091{
1088 struct swapdev *sdp; 1092 struct swapdev *sdp;
1089 struct vnode *vp; 1093 struct vnode *vp;
1090 int pageno, bn; 1094 int pageno, bn;
1091 UVMHIST_FUNC("swstrategy"); UVMHIST_CALLED(pdhist); 1095 UVMHIST_FUNC("swstrategy"); UVMHIST_CALLED(pdhist);
1092 1096
1093 /* 1097 /*
1094 * convert block number to swapdev. note that swapdev can't 1098 * convert block number to swapdev. note that swapdev can't
1095 * be yanked out from under us because we are holding resources 1099 * be yanked out from under us because we are holding resources
1096 * in it (i.e. the blocks we are doing I/O on). 1100 * in it (i.e. the blocks we are doing I/O on).
1097 */ 1101 */
1098 pageno = dbtob((int64_t)bp->b_blkno) >> PAGE_SHIFT; 1102 pageno = dbtob((int64_t)bp->b_blkno) >> PAGE_SHIFT;
1099 mutex_enter(&uvm_swap_data_lock); 1103 mutex_enter(&uvm_swap_data_lock);
1100 sdp = swapdrum_getsdp(pageno); 1104 sdp = swapdrum_getsdp(pageno);
1101 mutex_exit(&uvm_swap_data_lock); 1105 mutex_exit(&uvm_swap_data_lock);
1102 if (sdp == NULL) { 1106 if (sdp == NULL) {
1103 bp->b_error = EINVAL; 1107 bp->b_error = EINVAL;
1104 biodone(bp); 1108 biodone(bp);
1105 UVMHIST_LOG(pdhist, " failed to get swap device", 0, 0, 0, 0); 1109 UVMHIST_LOG(pdhist, " failed to get swap device", 0, 0, 0, 0);
1106 return; 1110 return;
1107 } 1111 }
1108 1112
1109 /* 1113 /*
1110 * convert drum page number to block number on this swapdev. 1114 * convert drum page number to block number on this swapdev.
1111 */ 1115 */
1112 1116
1113 pageno -= sdp->swd_drumoffset; /* page # on swapdev */ 1117 pageno -= sdp->swd_drumoffset; /* page # on swapdev */
1114 bn = btodb((uint64_t)pageno << PAGE_SHIFT); /* convert to diskblock */ 1118 bn = btodb((uint64_t)pageno << PAGE_SHIFT); /* convert to diskblock */
1115 1119
1116 UVMHIST_LOG(pdhist, " %s: mapoff=%x bn=%x bcount=%ld", 1120 UVMHIST_LOG(pdhist, " %s: mapoff=%x bn=%x bcount=%ld",
1117 ((bp->b_flags & B_READ) == 0) ? "write" : "read", 1121 ((bp->b_flags & B_READ) == 0) ? "write" : "read",
1118 sdp->swd_drumoffset, bn, bp->b_bcount); 1122 sdp->swd_drumoffset, bn, bp->b_bcount);
1119 1123
1120 /* 1124 /*
1121 * for block devices we finish up here. 1125 * for block devices we finish up here.
1122 * for regular files we have to do more work which we delegate 1126 * for regular files we have to do more work which we delegate
1123 * to sw_reg_strategy(). 1127 * to sw_reg_strategy().
1124 */ 1128 */
1125 1129
1126 vp = sdp->swd_vp; /* swapdev vnode pointer */ 1130 vp = sdp->swd_vp; /* swapdev vnode pointer */
1127 switch (vp->v_type) { 1131 switch (vp->v_type) {
1128 default: 1132 default:
1129 panic("swstrategy: vnode type 0x%x", vp->v_type); 1133 panic("swstrategy: vnode type 0x%x", vp->v_type);
1130 1134
1131 case VBLK: 1135 case VBLK:
1132 1136
1133 /* 1137 /*
1134 * must convert "bp" from an I/O on /dev/drum to an I/O 1138 * must convert "bp" from an I/O on /dev/drum to an I/O
1135 * on the swapdev (sdp). 1139 * on the swapdev (sdp).
1136 */ 1140 */
1137 bp->b_blkno = bn; /* swapdev block number */ 1141 bp->b_blkno = bn; /* swapdev block number */
1138 bp->b_dev = sdp->swd_dev; /* swapdev dev_t */ 1142 bp->b_dev = sdp->swd_dev; /* swapdev dev_t */
1139 1143
1140 /* 1144 /*
1141 * if we are doing a write, we have to redirect the i/o on 1145 * if we are doing a write, we have to redirect the i/o on
1142 * drum's v_numoutput counter to the swapdevs. 1146 * drum's v_numoutput counter to the swapdevs.
1143 */ 1147 */
1144 if ((bp->b_flags & B_READ) == 0) { 1148 if ((bp->b_flags & B_READ) == 0) {
1145 mutex_enter(bp->b_objlock); 1149 mutex_enter(bp->b_objlock);
1146 vwakeup(bp); /* kills one 'v_numoutput' on drum */ 1150 vwakeup(bp); /* kills one 'v_numoutput' on drum */
1147 mutex_exit(bp->b_objlock); 1151 mutex_exit(bp->b_objlock);
1148 mutex_enter(&vp->v_interlock); 1152 mutex_enter(&vp->v_interlock);
1149 vp->v_numoutput++; /* put it on swapdev */ 1153 vp->v_numoutput++; /* put it on swapdev */
1150 mutex_exit(&vp->v_interlock); 1154 mutex_exit(&vp->v_interlock);
1151 } 1155 }
1152 1156
1153 /* 1157 /*
1154 * finally plug in swapdev vnode and start I/O 1158 * finally plug in swapdev vnode and start I/O
1155 */ 1159 */
1156 bp->b_vp = vp; 1160 bp->b_vp = vp;
1157 bp->b_objlock = &vp->v_interlock; 1161 bp->b_objlock = &vp->v_interlock;
1158 VOP_STRATEGY(vp, bp); 1162 VOP_STRATEGY(vp, bp);
1159 return; 1163 return;
1160 1164
1161 case VREG: 1165 case VREG:
1162 /* 1166 /*
1163 * delegate to sw_reg_strategy function. 1167 * delegate to sw_reg_strategy function.
1164 */ 1168 */
1165 sw_reg_strategy(sdp, bp, bn); 1169 sw_reg_strategy(sdp, bp, bn);
1166 return; 1170 return;
1167 } 1171 }
1168 /* NOTREACHED */ 1172 /* NOTREACHED */
1169} 1173}
1170 1174
1171/* 1175/*
1172 * swread: the read function for the drum (just a call to physio) 1176 * swread: the read function for the drum (just a call to physio)
1173 */ 1177 */
1174/*ARGSUSED*/ 1178/*ARGSUSED*/
1175static int 1179static int
1176swread(dev_t dev, struct uio *uio, int ioflag) 1180swread(dev_t dev, struct uio *uio, int ioflag)
1177{ 1181{
1178 UVMHIST_FUNC("swread"); UVMHIST_CALLED(pdhist); 1182 UVMHIST_FUNC("swread"); UVMHIST_CALLED(pdhist);
1179 1183
1180 UVMHIST_LOG(pdhist, " dev=%x offset=%qx", dev, uio->uio_offset, 0, 0); 1184 UVMHIST_LOG(pdhist, " dev=%x offset=%qx", dev, uio->uio_offset, 0, 0);
1181 return (physio(swstrategy, NULL, dev, B_READ, minphys, uio)); 1185 return (physio(swstrategy, NULL, dev, B_READ, minphys, uio));
1182} 1186}
1183 1187
1184/* 1188/*
1185 * swwrite: the write function for the drum (just a call to physio) 1189 * swwrite: the write function for the drum (just a call to physio)
1186 */ 1190 */
1187/*ARGSUSED*/ 1191/*ARGSUSED*/
1188static int 1192static int
1189swwrite(dev_t dev, struct uio *uio, int ioflag) 1193swwrite(dev_t dev, struct uio *uio, int ioflag)
1190{ 1194{
1191 UVMHIST_FUNC("swwrite"); UVMHIST_CALLED(pdhist); 1195 UVMHIST_FUNC("swwrite"); UVMHIST_CALLED(pdhist);
1192 1196
1193 UVMHIST_LOG(pdhist, " dev=%x offset=%qx", dev, uio->uio_offset, 0, 0); 1197 UVMHIST_LOG(pdhist, " dev=%x offset=%qx", dev, uio->uio_offset, 0, 0);
1194 return (physio(swstrategy, NULL, dev, B_WRITE, minphys, uio)); 1198 return (physio(swstrategy, NULL, dev, B_WRITE, minphys, uio));
1195} 1199}
1196 1200
1197const struct bdevsw swap_bdevsw = { 1201const struct bdevsw swap_bdevsw = {
1198 nullopen, nullclose, swstrategy, noioctl, nodump, nosize, D_OTHER, 1202 nullopen, nullclose, swstrategy, noioctl, nodump, nosize, D_OTHER,
1199}; 1203};
1200 1204
1201const struct cdevsw swap_cdevsw = { 1205const struct cdevsw swap_cdevsw = {
1202 nullopen, nullclose, swread, swwrite, noioctl, 1206 nullopen, nullclose, swread, swwrite, noioctl,
1203 nostop, notty, nopoll, nommap, nokqfilter, D_OTHER, 1207 nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
1204}; 1208};
1205 1209
1206/* 1210/*
1207 * sw_reg_strategy: handle swap i/o to regular files 1211 * sw_reg_strategy: handle swap i/o to regular files
1208 */ 1212 */
1209static void 1213static void
1210sw_reg_strategy(struct swapdev *sdp, struct buf *bp, int bn) 1214sw_reg_strategy(struct swapdev *sdp, struct buf *bp, int bn)
1211{ 1215{
1212 struct vnode *vp; 1216 struct vnode *vp;
1213 struct vndxfer *vnx; 1217 struct vndxfer *vnx;
1214 daddr_t nbn; 1218 daddr_t nbn;
1215 char *addr; 1219 char *addr;
1216 off_t byteoff; 1220 off_t byteoff;
1217 int s, off, nra, error, sz, resid; 1221 int s, off, nra, error, sz, resid;
1218 UVMHIST_FUNC("sw_reg_strategy"); UVMHIST_CALLED(pdhist); 1222 UVMHIST_FUNC("sw_reg_strategy"); UVMHIST_CALLED(pdhist);
1219 1223
1220 /* 1224 /*
1221 * allocate a vndxfer head for this transfer and point it to 1225 * allocate a vndxfer head for this transfer and point it to
1222 * our buffer. 1226 * our buffer.
1223 */ 1227 */
1224 vnx = pool_get(&vndxfer_pool, PR_WAITOK); 1228 vnx = pool_get(&vndxfer_pool, PR_WAITOK);
1225 vnx->vx_flags = VX_BUSY; 1229 vnx->vx_flags = VX_BUSY;
1226 vnx->vx_error = 0; 1230 vnx->vx_error = 0;
1227 vnx->vx_pending = 0; 1231 vnx->vx_pending = 0;
1228 vnx->vx_bp = bp; 1232 vnx->vx_bp = bp;
1229 vnx->vx_sdp = sdp; 1233 vnx->vx_sdp = sdp;
1230 1234
1231 /* 1235 /*
1232 * setup for main loop where we read filesystem blocks into 1236 * setup for main loop where we read filesystem blocks into
1233 * our buffer. 1237 * our buffer.
1234 */ 1238 */
1235 error = 0; 1239 error = 0;
1236 bp->b_resid = bp->b_bcount; /* nothing transfered yet! */ 1240 bp->b_resid = bp->b_bcount; /* nothing transfered yet! */
1237 addr = bp->b_data; /* current position in buffer */ 1241 addr = bp->b_data; /* current position in buffer */
1238 byteoff = dbtob((uint64_t)bn); 1242 byteoff = dbtob((uint64_t)bn);
1239 1243
1240 for (resid = bp->b_resid; resid; resid -= sz) { 1244 for (resid = bp->b_resid; resid; resid -= sz) {
1241 struct vndbuf *nbp; 1245 struct vndbuf *nbp;
1242 1246
1243 /* 1247 /*
1244 * translate byteoffset into block number. return values: 1248 * translate byteoffset into block number. return values:
1245 * vp = vnode of underlying device 1249 * vp = vnode of underlying device
1246 * nbn = new block number (on underlying vnode dev) 1250 * nbn = new block number (on underlying vnode dev)
1247 * nra = num blocks we can read-ahead (excludes requested 1251 * nra = num blocks we can read-ahead (excludes requested
1248 * block) 1252 * block)
1249 */ 1253 */
1250 nra = 0; 1254 nra = 0;
1251 error = VOP_BMAP(sdp->swd_vp, byteoff / sdp->swd_bsize, 1255 error = VOP_BMAP(sdp->swd_vp, byteoff / sdp->swd_bsize,
1252 &vp, &nbn, &nra); 1256 &vp, &nbn, &nra);
1253 1257
1254 if (error == 0 && nbn == (daddr_t)-1) { 1258 if (error == 0 && nbn == (daddr_t)-1) {
1255 /* 1259 /*
1256 * this used to just set error, but that doesn't 1260 * this used to just set error, but that doesn't
1257 * do the right thing. Instead, it causes random 1261 * do the right thing. Instead, it causes random
1258 * memory errors. The panic() should remain until 1262 * memory errors. The panic() should remain until
1259 * this condition doesn't destabilize the system. 1263 * this condition doesn't destabilize the system.
1260 */ 1264 */
1261#if 1 1265#if 1
1262 panic("sw_reg_strategy: swap to sparse file"); 1266 panic("sw_reg_strategy: swap to sparse file");
1263#else 1267#else
1264 error = EIO; /* failure */ 1268 error = EIO; /* failure */
1265#endif 1269#endif
1266 } 1270 }
1267 1271
1268 /* 1272 /*
1269 * punt if there was an error or a hole in the file. 1273 * punt if there was an error or a hole in the file.
1270 * we must wait for any i/o ops we have already started 1274 * we must wait for any i/o ops we have already started
1271 * to finish before returning. 1275 * to finish before returning.
1272 * 1276 *
1273 * XXX we could deal with holes here but it would be 1277 * XXX we could deal with holes here but it would be
1274 * a hassle (in the write case). 1278 * a hassle (in the write case).
1275 */ 1279 */
1276 if (error) { 1280 if (error) {
1277 s = splbio(); 1281 s = splbio();