uvm_swap.c revision 1.129.10.2 1 1.129.10.2 ad /* $NetBSD: uvm_swap.c,v 1.129.10.2 2007/07/29 13:31:19 ad Exp $ */
2 1.129.10.2 ad
3 1.129.10.2 ad /*
4 1.129.10.2 ad * Copyright (c) 1995, 1996, 1997 Matthew R. Green
5 1.129.10.2 ad * All rights reserved.
6 1.129.10.2 ad *
7 1.129.10.2 ad * Redistribution and use in source and binary forms, with or without
8 1.129.10.2 ad * modification, are permitted provided that the following conditions
9 1.129.10.2 ad * are met:
10 1.129.10.2 ad * 1. Redistributions of source code must retain the above copyright
11 1.129.10.2 ad * notice, this list of conditions and the following disclaimer.
12 1.129.10.2 ad * 2. Redistributions in binary form must reproduce the above copyright
13 1.129.10.2 ad * notice, this list of conditions and the following disclaimer in the
14 1.129.10.2 ad * documentation and/or other materials provided with the distribution.
15 1.129.10.2 ad * 3. The name of the author may not be used to endorse or promote products
16 1.129.10.2 ad * derived from this software without specific prior written permission.
17 1.129.10.2 ad *
18 1.129.10.2 ad * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.129.10.2 ad * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.129.10.2 ad * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.129.10.2 ad * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.129.10.2 ad * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 1.129.10.2 ad * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 1.129.10.2 ad * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 1.129.10.2 ad * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 1.129.10.2 ad * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.129.10.2 ad * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.129.10.2 ad * SUCH DAMAGE.
29 1.129.10.2 ad *
30 1.129.10.2 ad * from: NetBSD: vm_swap.c,v 1.52 1997/12/02 13:47:37 pk Exp
31 1.129.10.2 ad * from: Id: uvm_swap.c,v 1.1.2.42 1998/02/02 20:38:06 chuck Exp
32 1.129.10.2 ad */
33 1.129.10.2 ad
34 1.129.10.2 ad #include <sys/cdefs.h>
35 1.129.10.2 ad __KERNEL_RCSID(0, "$NetBSD: uvm_swap.c,v 1.129.10.2 2007/07/29 13:31:19 ad Exp $");
36 1.129.10.2 ad
37 1.129.10.2 ad #include "fs_nfs.h"
38 1.129.10.2 ad #include "opt_uvmhist.h"
39 1.129.10.2 ad #include "opt_compat_netbsd.h"
40 1.129.10.2 ad #include "opt_ddb.h"
41 1.129.10.2 ad
42 1.129.10.2 ad #include <sys/param.h>
43 1.129.10.2 ad #include <sys/systm.h>
44 1.129.10.2 ad #include <sys/buf.h>
45 1.129.10.2 ad #include <sys/bufq.h>
46 1.129.10.2 ad #include <sys/conf.h>
47 1.129.10.2 ad #include <sys/proc.h>
48 1.129.10.2 ad #include <sys/namei.h>
49 1.129.10.2 ad #include <sys/disklabel.h>
50 1.129.10.2 ad #include <sys/errno.h>
51 1.129.10.2 ad #include <sys/kernel.h>
52 1.129.10.2 ad #include <sys/malloc.h>
53 1.129.10.2 ad #include <sys/vnode.h>
54 1.129.10.2 ad #include <sys/file.h>
55 1.129.10.2 ad #include <sys/vmem.h>
56 1.129.10.2 ad #include <sys/blist.h>
57 1.129.10.2 ad #include <sys/mount.h>
58 1.129.10.2 ad #include <sys/pool.h>
59 1.129.10.2 ad #include <sys/syscallargs.h>
60 1.129.10.2 ad #include <sys/swap.h>
61 1.129.10.2 ad #include <sys/kauth.h>
62 1.129.10.2 ad #include <sys/sysctl.h>
63 1.129.10.2 ad
64 1.129.10.2 ad #include <uvm/uvm.h>
65 1.129.10.2 ad
66 1.129.10.2 ad #include <miscfs/specfs/specdev.h>
67 1.129.10.2 ad
68 1.129.10.2 ad /*
69 1.129.10.2 ad * uvm_swap.c: manage configuration and i/o to swap space.
70 1.129.10.2 ad */
71 1.129.10.2 ad
72 1.129.10.2 ad /*
73 1.129.10.2 ad * swap space is managed in the following way:
74 1.129.10.2 ad *
75 1.129.10.2 ad * each swap partition or file is described by a "swapdev" structure.
76 1.129.10.2 ad * each "swapdev" structure contains a "swapent" structure which contains
77 1.129.10.2 ad * information that is passed up to the user (via system calls).
78 1.129.10.2 ad *
79 1.129.10.2 ad * each swap partition is assigned a "priority" (int) which controls
80 1.129.10.2 ad * swap parition usage.
81 1.129.10.2 ad *
82 1.129.10.2 ad * the system maintains a global data structure describing all swap
83 1.129.10.2 ad * partitions/files. there is a sorted LIST of "swappri" structures
84 1.129.10.2 ad * which describe "swapdev"'s at that priority. this LIST is headed
85 1.129.10.2 ad * by the "swap_priority" global var. each "swappri" contains a
86 1.129.10.2 ad * CIRCLEQ of "swapdev" structures at that priority.
87 1.129.10.2 ad *
88 1.129.10.2 ad * locking:
89 1.129.10.2 ad * - swap_syscall_lock (krwlock_t): this lock serializes the swapctl
90 1.129.10.2 ad * system call and prevents the swap priority list from changing
91 1.129.10.2 ad * while we are in the middle of a system call (e.g. SWAP_STATS).
92 1.129.10.2 ad * - uvm_swap_data_lock (kmutex_t): this lock protects all swap data
93 1.129.10.2 ad * structures including the priority list, the swapdev structures,
94 1.129.10.2 ad * and the swapmap arena.
95 1.129.10.2 ad *
96 1.129.10.2 ad * each swap device has the following info:
97 1.129.10.2 ad * - swap device in use (could be disabled, preventing future use)
98 1.129.10.2 ad * - swap enabled (allows new allocations on swap)
99 1.129.10.2 ad * - map info in /dev/drum
100 1.129.10.2 ad * - vnode pointer
101 1.129.10.2 ad * for swap files only:
102 1.129.10.2 ad * - block size
103 1.129.10.2 ad * - max byte count in buffer
104 1.129.10.2 ad * - buffer
105 1.129.10.2 ad *
106 1.129.10.2 ad * userland controls and configures swap with the swapctl(2) system call.
107 1.129.10.2 ad * the sys_swapctl performs the following operations:
108 1.129.10.2 ad * [1] SWAP_NSWAP: returns the number of swap devices currently configured
109 1.129.10.2 ad * [2] SWAP_STATS: given a pointer to an array of swapent structures
110 1.129.10.2 ad * (passed in via "arg") of a size passed in via "misc" ... we load
111 1.129.10.2 ad * the current swap config into the array. The actual work is done
112 1.129.10.2 ad * in the uvm_swap_stats(9) function.
113 1.129.10.2 ad * [3] SWAP_ON: given a pathname in arg (could be device or file) and a
114 1.129.10.2 ad * priority in "misc", start swapping on it.
115 1.129.10.2 ad * [4] SWAP_OFF: as SWAP_ON, but stops swapping to a device
116 1.129.10.2 ad * [5] SWAP_CTL: changes the priority of a swap device (new priority in
117 1.129.10.2 ad * "misc")
118 1.129.10.2 ad */
119 1.129.10.2 ad
120 1.129.10.2 ad /*
121 1.129.10.2 ad * swapdev: describes a single swap partition/file
122 1.129.10.2 ad *
123 1.129.10.2 ad * note the following should be true:
124 1.129.10.2 ad * swd_inuse <= swd_nblks [number of blocks in use is <= total blocks]
125 1.129.10.2 ad * swd_nblks <= swd_mapsize [because mapsize includes miniroot+disklabel]
126 1.129.10.2 ad */
127 1.129.10.2 ad struct swapdev {
128 1.129.10.2 ad struct oswapent swd_ose;
129 1.129.10.2 ad #define swd_dev swd_ose.ose_dev /* device id */
130 1.129.10.2 ad #define swd_flags swd_ose.ose_flags /* flags:inuse/enable/fake */
131 1.129.10.2 ad #define swd_priority swd_ose.ose_priority /* our priority */
132 1.129.10.2 ad /* also: swd_ose.ose_nblks, swd_ose.ose_inuse */
133 1.129.10.2 ad char *swd_path; /* saved pathname of device */
134 1.129.10.2 ad int swd_pathlen; /* length of pathname */
135 1.129.10.2 ad int swd_npages; /* #pages we can use */
136 1.129.10.2 ad int swd_npginuse; /* #pages in use */
137 1.129.10.2 ad int swd_npgbad; /* #pages bad */
138 1.129.10.2 ad int swd_drumoffset; /* page0 offset in drum */
139 1.129.10.2 ad int swd_drumsize; /* #pages in drum */
140 1.129.10.2 ad blist_t swd_blist; /* blist for this swapdev */
141 1.129.10.2 ad struct vnode *swd_vp; /* backing vnode */
142 1.129.10.2 ad CIRCLEQ_ENTRY(swapdev) swd_next; /* priority circleq */
143 1.129.10.2 ad
144 1.129.10.2 ad int swd_bsize; /* blocksize (bytes) */
145 1.129.10.2 ad int swd_maxactive; /* max active i/o reqs */
146 1.129.10.2 ad struct bufq_state *swd_tab; /* buffer list */
147 1.129.10.2 ad int swd_active; /* number of active buffers */
148 1.129.10.2 ad };
149 1.129.10.2 ad
150 1.129.10.2 ad /*
151 1.129.10.2 ad * swap device priority entry; the list is kept sorted on `spi_priority'.
152 1.129.10.2 ad */
153 1.129.10.2 ad struct swappri {
154 1.129.10.2 ad int spi_priority; /* priority */
155 1.129.10.2 ad CIRCLEQ_HEAD(spi_swapdev, swapdev) spi_swapdev;
156 1.129.10.2 ad /* circleq of swapdevs at this priority */
157 1.129.10.2 ad LIST_ENTRY(swappri) spi_swappri; /* global list of pri's */
158 1.129.10.2 ad };
159 1.129.10.2 ad
160 1.129.10.2 ad /*
161 1.129.10.2 ad * The following two structures are used to keep track of data transfers
162 1.129.10.2 ad * on swap devices associated with regular files.
163 1.129.10.2 ad * NOTE: this code is more or less a copy of vnd.c; we use the same
164 1.129.10.2 ad * structure names here to ease porting..
165 1.129.10.2 ad */
166 1.129.10.2 ad struct vndxfer {
167 1.129.10.2 ad struct buf *vx_bp; /* Pointer to parent buffer */
168 1.129.10.2 ad struct swapdev *vx_sdp;
169 1.129.10.2 ad int vx_error;
170 1.129.10.2 ad int vx_pending; /* # of pending aux buffers */
171 1.129.10.2 ad int vx_flags;
172 1.129.10.2 ad #define VX_BUSY 1
173 1.129.10.2 ad #define VX_DEAD 2
174 1.129.10.2 ad };
175 1.129.10.2 ad
176 1.129.10.2 ad struct vndbuf {
177 1.129.10.2 ad struct buf vb_buf;
178 1.129.10.2 ad struct vndxfer *vb_xfer;
179 1.129.10.2 ad };
180 1.129.10.2 ad
181 1.129.10.2 ad
182 1.129.10.2 ad /*
183 1.129.10.2 ad * We keep a of pool vndbuf's and vndxfer structures.
184 1.129.10.2 ad */
185 1.129.10.2 ad POOL_INIT(vndxfer_pool, sizeof(struct vndxfer), 0, 0, 0, "swp vnx", NULL,
186 1.129.10.2 ad IPL_BIO);
187 1.129.10.2 ad POOL_INIT(vndbuf_pool, sizeof(struct vndbuf), 0, 0, 0, "swp vnd", NULL,
188 1.129.10.2 ad IPL_BIO);
189 1.129.10.2 ad
190 1.129.10.2 ad #define getvndxfer(vnx) do { \
191 1.129.10.2 ad int sp = splbio(); \
192 1.129.10.2 ad vnx = pool_get(&vndxfer_pool, PR_WAITOK); \
193 1.129.10.2 ad splx(sp); \
194 1.129.10.2 ad } while (/*CONSTCOND*/ 0)
195 1.129.10.2 ad
196 1.129.10.2 ad #define putvndxfer(vnx) { \
197 1.129.10.2 ad pool_put(&vndxfer_pool, (void *)(vnx)); \
198 1.129.10.2 ad }
199 1.129.10.2 ad
200 1.129.10.2 ad #define getvndbuf(vbp) do { \
201 1.129.10.2 ad int sp = splbio(); \
202 1.129.10.2 ad vbp = pool_get(&vndbuf_pool, PR_WAITOK); \
203 1.129.10.2 ad splx(sp); \
204 1.129.10.2 ad } while (/*CONSTCOND*/ 0)
205 1.129.10.2 ad
206 1.129.10.2 ad #define putvndbuf(vbp) { \
207 1.129.10.2 ad pool_put(&vndbuf_pool, (void *)(vbp)); \
208 1.129.10.2 ad }
209 1.129.10.2 ad
210 1.129.10.2 ad /*
211 1.129.10.2 ad * local variables
212 1.129.10.2 ad */
213 1.129.10.2 ad MALLOC_DEFINE(M_VMSWAP, "VM swap", "VM swap structures");
214 1.129.10.2 ad static vmem_t *swapmap; /* controls the mapping of /dev/drum */
215 1.129.10.2 ad
216 1.129.10.2 ad /* list of all active swap devices [by priority] */
217 1.129.10.2 ad LIST_HEAD(swap_priority, swappri);
218 1.129.10.2 ad static struct swap_priority swap_priority;
219 1.129.10.2 ad
220 1.129.10.2 ad /* locks */
221 1.129.10.2 ad static krwlock_t swap_syscall_lock;
222 1.129.10.2 ad
223 1.129.10.2 ad /*
224 1.129.10.2 ad * prototypes
225 1.129.10.2 ad */
226 1.129.10.2 ad static struct swapdev *swapdrum_getsdp(int);
227 1.129.10.2 ad
228 1.129.10.2 ad static struct swapdev *swaplist_find(struct vnode *, bool);
229 1.129.10.2 ad static void swaplist_insert(struct swapdev *,
230 1.129.10.2 ad struct swappri *, int);
231 1.129.10.2 ad static void swaplist_trim(void);
232 1.129.10.2 ad
233 1.129.10.2 ad static int swap_on(struct lwp *, struct swapdev *);
234 1.129.10.2 ad static int swap_off(struct lwp *, struct swapdev *);
235 1.129.10.2 ad
236 1.129.10.2 ad static void uvm_swap_stats_locked(int, struct swapent *, int, register_t *);
237 1.129.10.2 ad
238 1.129.10.2 ad static void sw_reg_strategy(struct swapdev *, struct buf *, int);
239 1.129.10.2 ad static void sw_reg_iodone(struct buf *);
240 1.129.10.2 ad static void sw_reg_start(struct swapdev *);
241 1.129.10.2 ad
242 1.129.10.2 ad static int uvm_swap_io(struct vm_page **, int, int, int);
243 1.129.10.2 ad
244 1.129.10.2 ad /*
245 1.129.10.2 ad * uvm_swap_init: init the swap system data structures and locks
246 1.129.10.2 ad *
247 1.129.10.2 ad * => called at boot time from init_main.c after the filesystems
248 1.129.10.2 ad * are brought up (which happens after uvm_init())
249 1.129.10.2 ad */
250 1.129.10.2 ad void
251 1.129.10.2 ad uvm_swap_init(void)
252 1.129.10.2 ad {
253 1.129.10.2 ad UVMHIST_FUNC("uvm_swap_init");
254 1.129.10.2 ad
255 1.129.10.2 ad UVMHIST_CALLED(pdhist);
256 1.129.10.2 ad /*
257 1.129.10.2 ad * first, init the swap list, its counter, and its lock.
258 1.129.10.2 ad * then get a handle on the vnode for /dev/drum by using
259 1.129.10.2 ad * the its dev_t number ("swapdev", from MD conf.c).
260 1.129.10.2 ad */
261 1.129.10.2 ad
262 1.129.10.2 ad LIST_INIT(&swap_priority);
263 1.129.10.2 ad uvmexp.nswapdev = 0;
264 1.129.10.2 ad rw_init(&swap_syscall_lock);
265 1.129.10.2 ad cv_init(&uvm.scheduler_cv, "schedule");
266 1.129.10.2 ad /* XXXSMP should be adaptive, but needs vmobjlock replaced */
267 1.129.10.2 ad mutex_init(&uvm_swap_data_lock, MUTEX_SPIN, IPL_NONE);
268 1.129.10.2 ad
269 1.129.10.2 ad /* XXXSMP should be at IPL_VM, but for audio interrupt handlers. */
270 1.129.10.2 ad mutex_init(&uvm_scheduler_mutex, MUTEX_SPIN, IPL_SCHED);
271 1.129.10.2 ad
272 1.129.10.2 ad if (bdevvp(swapdev, &swapdev_vp))
273 1.129.10.2 ad panic("uvm_swap_init: can't get vnode for swap device");
274 1.129.10.2 ad
275 1.129.10.2 ad /*
276 1.129.10.2 ad * create swap block resource map to map /dev/drum. the range
277 1.129.10.2 ad * from 1 to INT_MAX allows 2 gigablocks of swap space. note
278 1.129.10.2 ad * that block 0 is reserved (used to indicate an allocation
279 1.129.10.2 ad * failure, or no allocation).
280 1.129.10.2 ad */
281 1.129.10.2 ad swapmap = vmem_create("swapmap", 1, INT_MAX - 1, 1, NULL, NULL, NULL, 0,
282 1.129.10.2 ad VM_NOSLEEP, IPL_NONE);
283 1.129.10.2 ad if (swapmap == 0)
284 1.129.10.2 ad panic("uvm_swap_init: extent_create failed");
285 1.129.10.2 ad
286 1.129.10.2 ad /*
287 1.129.10.2 ad * done!
288 1.129.10.2 ad */
289 1.129.10.2 ad uvm.swap_running = true;
290 1.129.10.2 ad uvm.swapout_enabled = 1;
291 1.129.10.2 ad UVMHIST_LOG(pdhist, "<- done", 0, 0, 0, 0);
292 1.129.10.2 ad
293 1.129.10.2 ad sysctl_createv(NULL, 0, NULL, NULL,
294 1.129.10.2 ad CTLFLAG_READWRITE,
295 1.129.10.2 ad CTLTYPE_INT, "swapout",
296 1.129.10.2 ad SYSCTL_DESCR("Set 0 to disable swapout of kernel stacks"),
297 1.129.10.2 ad NULL, 0, &uvm.swapout_enabled, 0, CTL_VM, CTL_CREATE, CTL_EOL);
298 1.129.10.2 ad }
299 1.129.10.2 ad
300 1.129.10.2 ad /*
301 1.129.10.2 ad * swaplist functions: functions that operate on the list of swap
302 1.129.10.2 ad * devices on the system.
303 1.129.10.2 ad */
304 1.129.10.2 ad
305 1.129.10.2 ad /*
306 1.129.10.2 ad * swaplist_insert: insert swap device "sdp" into the global list
307 1.129.10.2 ad *
308 1.129.10.2 ad * => caller must hold both swap_syscall_lock and uvm_swap_data_lock
309 1.129.10.2 ad * => caller must provide a newly malloc'd swappri structure (we will
310 1.129.10.2 ad * FREE it if we don't need it... this it to prevent malloc blocking
311 1.129.10.2 ad * here while adding swap)
312 1.129.10.2 ad */
313 1.129.10.2 ad static void
314 1.129.10.2 ad swaplist_insert(struct swapdev *sdp, struct swappri *newspp, int priority)
315 1.129.10.2 ad {
316 1.129.10.2 ad struct swappri *spp, *pspp;
317 1.129.10.2 ad UVMHIST_FUNC("swaplist_insert"); UVMHIST_CALLED(pdhist);
318 1.129.10.2 ad
319 1.129.10.2 ad /*
320 1.129.10.2 ad * find entry at or after which to insert the new device.
321 1.129.10.2 ad */
322 1.129.10.2 ad pspp = NULL;
323 1.129.10.2 ad LIST_FOREACH(spp, &swap_priority, spi_swappri) {
324 1.129.10.2 ad if (priority <= spp->spi_priority)
325 1.129.10.2 ad break;
326 1.129.10.2 ad pspp = spp;
327 1.129.10.2 ad }
328 1.129.10.2 ad
329 1.129.10.2 ad /*
330 1.129.10.2 ad * new priority?
331 1.129.10.2 ad */
332 1.129.10.2 ad if (spp == NULL || spp->spi_priority != priority) {
333 1.129.10.2 ad spp = newspp; /* use newspp! */
334 1.129.10.2 ad UVMHIST_LOG(pdhist, "created new swappri = %d",
335 1.129.10.2 ad priority, 0, 0, 0);
336 1.129.10.2 ad
337 1.129.10.2 ad spp->spi_priority = priority;
338 1.129.10.2 ad CIRCLEQ_INIT(&spp->spi_swapdev);
339 1.129.10.2 ad
340 1.129.10.2 ad if (pspp)
341 1.129.10.2 ad LIST_INSERT_AFTER(pspp, spp, spi_swappri);
342 1.129.10.2 ad else
343 1.129.10.2 ad LIST_INSERT_HEAD(&swap_priority, spp, spi_swappri);
344 1.129.10.2 ad } else {
345 1.129.10.2 ad /* we don't need a new priority structure, free it */
346 1.129.10.2 ad FREE(newspp, M_VMSWAP);
347 1.129.10.2 ad }
348 1.129.10.2 ad
349 1.129.10.2 ad /*
350 1.129.10.2 ad * priority found (or created). now insert on the priority's
351 1.129.10.2 ad * circleq list and bump the total number of swapdevs.
352 1.129.10.2 ad */
353 1.129.10.2 ad sdp->swd_priority = priority;
354 1.129.10.2 ad CIRCLEQ_INSERT_TAIL(&spp->spi_swapdev, sdp, swd_next);
355 1.129.10.2 ad uvmexp.nswapdev++;
356 1.129.10.2 ad }
357 1.129.10.2 ad
358 1.129.10.2 ad /*
359 1.129.10.2 ad * swaplist_find: find and optionally remove a swap device from the
360 1.129.10.2 ad * global list.
361 1.129.10.2 ad *
362 1.129.10.2 ad * => caller must hold both swap_syscall_lock and uvm_swap_data_lock
363 1.129.10.2 ad * => we return the swapdev we found (and removed)
364 1.129.10.2 ad */
365 1.129.10.2 ad static struct swapdev *
366 1.129.10.2 ad swaplist_find(struct vnode *vp, bool remove)
367 1.129.10.2 ad {
368 1.129.10.2 ad struct swapdev *sdp;
369 1.129.10.2 ad struct swappri *spp;
370 1.129.10.2 ad
371 1.129.10.2 ad /*
372 1.129.10.2 ad * search the lists for the requested vp
373 1.129.10.2 ad */
374 1.129.10.2 ad
375 1.129.10.2 ad LIST_FOREACH(spp, &swap_priority, spi_swappri) {
376 1.129.10.2 ad CIRCLEQ_FOREACH(sdp, &spp->spi_swapdev, swd_next) {
377 1.129.10.2 ad if (sdp->swd_vp == vp) {
378 1.129.10.2 ad if (remove) {
379 1.129.10.2 ad CIRCLEQ_REMOVE(&spp->spi_swapdev,
380 1.129.10.2 ad sdp, swd_next);
381 1.129.10.2 ad uvmexp.nswapdev--;
382 1.129.10.2 ad }
383 1.129.10.2 ad return(sdp);
384 1.129.10.2 ad }
385 1.129.10.2 ad }
386 1.129.10.2 ad }
387 1.129.10.2 ad return (NULL);
388 1.129.10.2 ad }
389 1.129.10.2 ad
390 1.129.10.2 ad /*
391 1.129.10.2 ad * swaplist_trim: scan priority list for empty priority entries and kill
392 1.129.10.2 ad * them.
393 1.129.10.2 ad *
394 1.129.10.2 ad * => caller must hold both swap_syscall_lock and uvm_swap_data_lock
395 1.129.10.2 ad */
396 1.129.10.2 ad static void
397 1.129.10.2 ad swaplist_trim(void)
398 1.129.10.2 ad {
399 1.129.10.2 ad struct swappri *spp, *nextspp;
400 1.129.10.2 ad
401 1.129.10.2 ad for (spp = LIST_FIRST(&swap_priority); spp != NULL; spp = nextspp) {
402 1.129.10.2 ad nextspp = LIST_NEXT(spp, spi_swappri);
403 1.129.10.2 ad if (CIRCLEQ_FIRST(&spp->spi_swapdev) !=
404 1.129.10.2 ad (void *)&spp->spi_swapdev)
405 1.129.10.2 ad continue;
406 1.129.10.2 ad LIST_REMOVE(spp, spi_swappri);
407 1.129.10.2 ad free(spp, M_VMSWAP);
408 1.129.10.2 ad }
409 1.129.10.2 ad }
410 1.129.10.2 ad
411 1.129.10.2 ad /*
412 1.129.10.2 ad * swapdrum_getsdp: given a page offset in /dev/drum, convert it back
413 1.129.10.2 ad * to the "swapdev" that maps that section of the drum.
414 1.129.10.2 ad *
415 1.129.10.2 ad * => each swapdev takes one big contig chunk of the drum
416 1.129.10.2 ad * => caller must hold uvm_swap_data_lock
417 1.129.10.2 ad */
418 1.129.10.2 ad static struct swapdev *
419 1.129.10.2 ad swapdrum_getsdp(int pgno)
420 1.129.10.2 ad {
421 1.129.10.2 ad struct swapdev *sdp;
422 1.129.10.2 ad struct swappri *spp;
423 1.129.10.2 ad
424 1.129.10.2 ad LIST_FOREACH(spp, &swap_priority, spi_swappri) {
425 1.129.10.2 ad CIRCLEQ_FOREACH(sdp, &spp->spi_swapdev, swd_next) {
426 1.129.10.2 ad if (sdp->swd_flags & SWF_FAKE)
427 1.129.10.2 ad continue;
428 1.129.10.2 ad if (pgno >= sdp->swd_drumoffset &&
429 1.129.10.2 ad pgno < (sdp->swd_drumoffset + sdp->swd_drumsize)) {
430 1.129.10.2 ad return sdp;
431 1.129.10.2 ad }
432 1.129.10.2 ad }
433 1.129.10.2 ad }
434 1.129.10.2 ad return NULL;
435 1.129.10.2 ad }
436 1.129.10.2 ad
437 1.129.10.2 ad
438 1.129.10.2 ad /*
439 1.129.10.2 ad * sys_swapctl: main entry point for swapctl(2) system call
440 1.129.10.2 ad * [with two helper functions: swap_on and swap_off]
441 1.129.10.2 ad */
442 1.129.10.2 ad int
443 1.129.10.2 ad sys_swapctl(struct lwp *l, void *v, register_t *retval)
444 1.129.10.2 ad {
445 1.129.10.2 ad struct sys_swapctl_args /* {
446 1.129.10.2 ad syscallarg(int) cmd;
447 1.129.10.2 ad syscallarg(void *) arg;
448 1.129.10.2 ad syscallarg(int) misc;
449 1.129.10.2 ad } */ *uap = (struct sys_swapctl_args *)v;
450 1.129.10.2 ad struct vnode *vp;
451 1.129.10.2 ad struct nameidata nd;
452 1.129.10.2 ad struct swappri *spp;
453 1.129.10.2 ad struct swapdev *sdp;
454 1.129.10.2 ad struct swapent *sep;
455 1.129.10.2 ad #define SWAP_PATH_MAX (PATH_MAX + 1)
456 1.129.10.2 ad char *userpath;
457 1.129.10.2 ad size_t len;
458 1.129.10.2 ad int error, misc;
459 1.129.10.2 ad int priority;
460 1.129.10.2 ad UVMHIST_FUNC("sys_swapctl"); UVMHIST_CALLED(pdhist);
461 1.129.10.2 ad
462 1.129.10.2 ad misc = SCARG(uap, misc);
463 1.129.10.2 ad
464 1.129.10.2 ad /*
465 1.129.10.2 ad * ensure serialized syscall access by grabbing the swap_syscall_lock
466 1.129.10.2 ad */
467 1.129.10.2 ad rw_enter(&swap_syscall_lock, RW_WRITER);
468 1.129.10.2 ad
469 1.129.10.2 ad userpath = malloc(SWAP_PATH_MAX, M_TEMP, M_WAITOK);
470 1.129.10.2 ad /*
471 1.129.10.2 ad * we handle the non-priv NSWAP and STATS request first.
472 1.129.10.2 ad *
473 1.129.10.2 ad * SWAP_NSWAP: return number of config'd swap devices
474 1.129.10.2 ad * [can also be obtained with uvmexp sysctl]
475 1.129.10.2 ad */
476 1.129.10.2 ad if (SCARG(uap, cmd) == SWAP_NSWAP) {
477 1.129.10.2 ad UVMHIST_LOG(pdhist, "<- done SWAP_NSWAP=%d", uvmexp.nswapdev,
478 1.129.10.2 ad 0, 0, 0);
479 1.129.10.2 ad *retval = uvmexp.nswapdev;
480 1.129.10.2 ad error = 0;
481 1.129.10.2 ad goto out;
482 1.129.10.2 ad }
483 1.129.10.2 ad
484 1.129.10.2 ad /*
485 1.129.10.2 ad * SWAP_STATS: get stats on current # of configured swap devs
486 1.129.10.2 ad *
487 1.129.10.2 ad * note that the swap_priority list can't change as long
488 1.129.10.2 ad * as we are holding the swap_syscall_lock. we don't want
489 1.129.10.2 ad * to grab the uvm_swap_data_lock because we may fault&sleep during
490 1.129.10.2 ad * copyout() and we don't want to be holding that lock then!
491 1.129.10.2 ad */
492 1.129.10.2 ad if (SCARG(uap, cmd) == SWAP_STATS
493 1.129.10.2 ad #if defined(COMPAT_13)
494 1.129.10.2 ad || SCARG(uap, cmd) == SWAP_OSTATS
495 1.129.10.2 ad #endif
496 1.129.10.2 ad ) {
497 1.129.10.2 ad if ((size_t)misc > (size_t)uvmexp.nswapdev)
498 1.129.10.2 ad misc = uvmexp.nswapdev;
499 1.129.10.2 ad #if defined(COMPAT_13)
500 1.129.10.2 ad if (SCARG(uap, cmd) == SWAP_OSTATS)
501 1.129.10.2 ad len = sizeof(struct oswapent) * misc;
502 1.129.10.2 ad else
503 1.129.10.2 ad #endif
504 1.129.10.2 ad len = sizeof(struct swapent) * misc;
505 1.129.10.2 ad sep = (struct swapent *)malloc(len, M_TEMP, M_WAITOK);
506 1.129.10.2 ad
507 1.129.10.2 ad uvm_swap_stats_locked(SCARG(uap, cmd), sep, misc, retval);
508 1.129.10.2 ad error = copyout(sep, SCARG(uap, arg), len);
509 1.129.10.2 ad
510 1.129.10.2 ad free(sep, M_TEMP);
511 1.129.10.2 ad UVMHIST_LOG(pdhist, "<- done SWAP_STATS", 0, 0, 0, 0);
512 1.129.10.2 ad goto out;
513 1.129.10.2 ad }
514 1.129.10.2 ad if (SCARG(uap, cmd) == SWAP_GETDUMPDEV) {
515 1.129.10.2 ad dev_t *devp = (dev_t *)SCARG(uap, arg);
516 1.129.10.2 ad
517 1.129.10.2 ad error = copyout(&dumpdev, devp, sizeof(dumpdev));
518 1.129.10.2 ad goto out;
519 1.129.10.2 ad }
520 1.129.10.2 ad
521 1.129.10.2 ad /*
522 1.129.10.2 ad * all other requests require superuser privs. verify.
523 1.129.10.2 ad */
524 1.129.10.2 ad if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SWAPCTL,
525 1.129.10.2 ad 0, NULL, NULL, NULL)))
526 1.129.10.2 ad goto out;
527 1.129.10.2 ad
528 1.129.10.2 ad if (SCARG(uap, cmd) == SWAP_DUMPOFF) {
529 1.129.10.2 ad /* drop the current dump device */
530 1.129.10.2 ad dumpdev = NODEV;
531 1.129.10.2 ad cpu_dumpconf();
532 1.129.10.2 ad goto out;
533 1.129.10.2 ad }
534 1.129.10.2 ad
535 1.129.10.2 ad /*
536 1.129.10.2 ad * at this point we expect a path name in arg. we will
537 1.129.10.2 ad * use namei() to gain a vnode reference (vref), and lock
538 1.129.10.2 ad * the vnode (VOP_LOCK).
539 1.129.10.2 ad *
540 1.129.10.2 ad * XXX: a NULL arg means use the root vnode pointer (e.g. for
541 1.129.10.2 ad * miniroot)
542 1.129.10.2 ad */
543 1.129.10.2 ad if (SCARG(uap, arg) == NULL) {
544 1.129.10.2 ad vp = rootvp; /* miniroot */
545 1.129.10.2 ad if (vget(vp, LK_EXCLUSIVE)) {
546 1.129.10.2 ad error = EBUSY;
547 1.129.10.2 ad goto out;
548 1.129.10.2 ad }
549 1.129.10.2 ad if (SCARG(uap, cmd) == SWAP_ON &&
550 1.129.10.2 ad copystr("miniroot", userpath, SWAP_PATH_MAX, &len))
551 1.129.10.2 ad panic("swapctl: miniroot copy failed");
552 1.129.10.2 ad } else {
553 1.129.10.2 ad int space;
554 1.129.10.2 ad char *where;
555 1.129.10.2 ad
556 1.129.10.2 ad if (SCARG(uap, cmd) == SWAP_ON) {
557 1.129.10.2 ad if ((error = copyinstr(SCARG(uap, arg), userpath,
558 1.129.10.2 ad SWAP_PATH_MAX, &len)))
559 1.129.10.2 ad goto out;
560 1.129.10.2 ad space = UIO_SYSSPACE;
561 1.129.10.2 ad where = userpath;
562 1.129.10.2 ad } else {
563 1.129.10.2 ad space = UIO_USERSPACE;
564 1.129.10.2 ad where = (char *)SCARG(uap, arg);
565 1.129.10.2 ad }
566 1.129.10.2 ad NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, space, where, l);
567 1.129.10.2 ad if ((error = namei(&nd)))
568 1.129.10.2 ad goto out;
569 1.129.10.2 ad vp = nd.ni_vp;
570 1.129.10.2 ad }
571 1.129.10.2 ad /* note: "vp" is referenced and locked */
572 1.129.10.2 ad
573 1.129.10.2 ad error = 0; /* assume no error */
574 1.129.10.2 ad switch(SCARG(uap, cmd)) {
575 1.129.10.2 ad
576 1.129.10.2 ad case SWAP_DUMPDEV:
577 1.129.10.2 ad if (vp->v_type != VBLK) {
578 1.129.10.2 ad error = ENOTBLK;
579 1.129.10.2 ad break;
580 1.129.10.2 ad }
581 1.129.10.2 ad if (bdevsw_lookup(vp->v_rdev))
582 1.129.10.2 ad dumpdev = vp->v_rdev;
583 1.129.10.2 ad else
584 1.129.10.2 ad dumpdev = NODEV;
585 1.129.10.2 ad cpu_dumpconf();
586 1.129.10.2 ad break;
587 1.129.10.2 ad
588 1.129.10.2 ad case SWAP_CTL:
589 1.129.10.2 ad /*
590 1.129.10.2 ad * get new priority, remove old entry (if any) and then
591 1.129.10.2 ad * reinsert it in the correct place. finally, prune out
592 1.129.10.2 ad * any empty priority structures.
593 1.129.10.2 ad */
594 1.129.10.2 ad priority = SCARG(uap, misc);
595 1.129.10.2 ad spp = malloc(sizeof *spp, M_VMSWAP, M_WAITOK);
596 1.129.10.2 ad mutex_enter(&uvm_swap_data_lock);
597 1.129.10.2 ad if ((sdp = swaplist_find(vp, true)) == NULL) {
598 1.129.10.2 ad error = ENOENT;
599 1.129.10.2 ad } else {
600 1.129.10.2 ad swaplist_insert(sdp, spp, priority);
601 1.129.10.2 ad swaplist_trim();
602 1.129.10.2 ad }
603 1.129.10.2 ad mutex_exit(&uvm_swap_data_lock);
604 1.129.10.2 ad if (error)
605 1.129.10.2 ad free(spp, M_VMSWAP);
606 1.129.10.2 ad break;
607 1.129.10.2 ad
608 1.129.10.2 ad case SWAP_ON:
609 1.129.10.2 ad
610 1.129.10.2 ad /*
611 1.129.10.2 ad * check for duplicates. if none found, then insert a
612 1.129.10.2 ad * dummy entry on the list to prevent someone else from
613 1.129.10.2 ad * trying to enable this device while we are working on
614 1.129.10.2 ad * it.
615 1.129.10.2 ad */
616 1.129.10.2 ad
617 1.129.10.2 ad priority = SCARG(uap, misc);
618 1.129.10.2 ad sdp = malloc(sizeof *sdp, M_VMSWAP, M_WAITOK);
619 1.129.10.2 ad spp = malloc(sizeof *spp, M_VMSWAP, M_WAITOK);
620 1.129.10.2 ad memset(sdp, 0, sizeof(*sdp));
621 1.129.10.2 ad sdp->swd_flags = SWF_FAKE;
622 1.129.10.2 ad sdp->swd_vp = vp;
623 1.129.10.2 ad sdp->swd_dev = (vp->v_type == VBLK) ? vp->v_rdev : NODEV;
624 1.129.10.2 ad bufq_alloc(&sdp->swd_tab, "disksort", BUFQ_SORT_RAWBLOCK);
625 1.129.10.2 ad mutex_enter(&uvm_swap_data_lock);
626 1.129.10.2 ad if (swaplist_find(vp, false) != NULL) {
627 1.129.10.2 ad error = EBUSY;
628 1.129.10.2 ad mutex_exit(&uvm_swap_data_lock);
629 1.129.10.2 ad bufq_free(sdp->swd_tab);
630 1.129.10.2 ad free(sdp, M_VMSWAP);
631 1.129.10.2 ad free(spp, M_VMSWAP);
632 1.129.10.2 ad break;
633 1.129.10.2 ad }
634 1.129.10.2 ad swaplist_insert(sdp, spp, priority);
635 1.129.10.2 ad mutex_exit(&uvm_swap_data_lock);
636 1.129.10.2 ad
637 1.129.10.2 ad sdp->swd_pathlen = len;
638 1.129.10.2 ad sdp->swd_path = malloc(sdp->swd_pathlen, M_VMSWAP, M_WAITOK);
639 1.129.10.2 ad if (copystr(userpath, sdp->swd_path, sdp->swd_pathlen, 0) != 0)
640 1.129.10.2 ad panic("swapctl: copystr");
641 1.129.10.2 ad
642 1.129.10.2 ad /*
643 1.129.10.2 ad * we've now got a FAKE placeholder in the swap list.
644 1.129.10.2 ad * now attempt to enable swap on it. if we fail, undo
645 1.129.10.2 ad * what we've done and kill the fake entry we just inserted.
646 1.129.10.2 ad * if swap_on is a success, it will clear the SWF_FAKE flag
647 1.129.10.2 ad */
648 1.129.10.2 ad
649 1.129.10.2 ad if ((error = swap_on(l, sdp)) != 0) {
650 1.129.10.2 ad mutex_enter(&uvm_swap_data_lock);
651 1.129.10.2 ad (void) swaplist_find(vp, true); /* kill fake entry */
652 1.129.10.2 ad swaplist_trim();
653 1.129.10.2 ad mutex_exit(&uvm_swap_data_lock);
654 1.129.10.2 ad bufq_free(sdp->swd_tab);
655 1.129.10.2 ad free(sdp->swd_path, M_VMSWAP);
656 1.129.10.2 ad free(sdp, M_VMSWAP);
657 1.129.10.2 ad break;
658 1.129.10.2 ad }
659 1.129.10.2 ad break;
660 1.129.10.2 ad
661 1.129.10.2 ad case SWAP_OFF:
662 1.129.10.2 ad mutex_enter(&uvm_swap_data_lock);
663 1.129.10.2 ad if ((sdp = swaplist_find(vp, false)) == NULL) {
664 1.129.10.2 ad mutex_exit(&uvm_swap_data_lock);
665 1.129.10.2 ad error = ENXIO;
666 1.129.10.2 ad break;
667 1.129.10.2 ad }
668 1.129.10.2 ad
669 1.129.10.2 ad /*
670 1.129.10.2 ad * If a device isn't in use or enabled, we
671 1.129.10.2 ad * can't stop swapping from it (again).
672 1.129.10.2 ad */
673 1.129.10.2 ad if ((sdp->swd_flags & (SWF_INUSE|SWF_ENABLE)) == 0) {
674 1.129.10.2 ad mutex_exit(&uvm_swap_data_lock);
675 1.129.10.2 ad error = EBUSY;
676 1.129.10.2 ad break;
677 1.129.10.2 ad }
678 1.129.10.2 ad
679 1.129.10.2 ad /*
680 1.129.10.2 ad * do the real work.
681 1.129.10.2 ad */
682 1.129.10.2 ad error = swap_off(l, sdp);
683 1.129.10.2 ad break;
684 1.129.10.2 ad
685 1.129.10.2 ad default:
686 1.129.10.2 ad error = EINVAL;
687 1.129.10.2 ad }
688 1.129.10.2 ad
689 1.129.10.2 ad /*
690 1.129.10.2 ad * done! release the ref gained by namei() and unlock.
691 1.129.10.2 ad */
692 1.129.10.2 ad vput(vp);
693 1.129.10.2 ad
694 1.129.10.2 ad out:
695 1.129.10.2 ad free(userpath, M_TEMP);
696 1.129.10.2 ad rw_exit(&swap_syscall_lock);
697 1.129.10.2 ad
698 1.129.10.2 ad UVMHIST_LOG(pdhist, "<- done! error=%d", error, 0, 0, 0);
699 1.129.10.2 ad return (error);
700 1.129.10.2 ad }
701 1.129.10.2 ad
702 1.129.10.2 ad /*
703 1.129.10.2 ad * swap_stats: implements swapctl(SWAP_STATS). The function is kept
704 1.129.10.2 ad * away from sys_swapctl() in order to allow COMPAT_* swapctl()
705 1.129.10.2 ad * emulation to use it directly without going through sys_swapctl().
706 1.129.10.2 ad * The problem with using sys_swapctl() there is that it involves
707 1.129.10.2 ad * copying the swapent array to the stackgap, and this array's size
708 1.129.10.2 ad * is not known at build time. Hence it would not be possible to
709 1.129.10.2 ad * ensure it would fit in the stackgap in any case.
710 1.129.10.2 ad */
711 1.129.10.2 ad void
712 1.129.10.2 ad uvm_swap_stats(int cmd, struct swapent *sep, int sec, register_t *retval)
713 1.129.10.2 ad {
714 1.129.10.2 ad
715 1.129.10.2 ad rw_enter(&swap_syscall_lock, RW_READER);
716 1.129.10.2 ad uvm_swap_stats_locked(cmd, sep, sec, retval);
717 1.129.10.2 ad rw_exit(&swap_syscall_lock);
718 1.129.10.2 ad }
719 1.129.10.2 ad
720 1.129.10.2 ad static void
721 1.129.10.2 ad uvm_swap_stats_locked(int cmd, struct swapent *sep, int sec, register_t *retval)
722 1.129.10.2 ad {
723 1.129.10.2 ad struct swappri *spp;
724 1.129.10.2 ad struct swapdev *sdp;
725 1.129.10.2 ad int count = 0;
726 1.129.10.2 ad
727 1.129.10.2 ad LIST_FOREACH(spp, &swap_priority, spi_swappri) {
728 1.129.10.2 ad for (sdp = CIRCLEQ_FIRST(&spp->spi_swapdev);
729 1.129.10.2 ad sdp != (void *)&spp->spi_swapdev && sec-- > 0;
730 1.129.10.2 ad sdp = CIRCLEQ_NEXT(sdp, swd_next)) {
731 1.129.10.2 ad /*
732 1.129.10.2 ad * backwards compatibility for system call.
733 1.129.10.2 ad * note that we use 'struct oswapent' as an
734 1.129.10.2 ad * overlay into both 'struct swapdev' and
735 1.129.10.2 ad * the userland 'struct swapent', as we
736 1.129.10.2 ad * want to retain backwards compatibility
737 1.129.10.2 ad * with NetBSD 1.3.
738 1.129.10.2 ad */
739 1.129.10.2 ad sdp->swd_ose.ose_inuse =
740 1.129.10.2 ad btodb((uint64_t)sdp->swd_npginuse <<
741 1.129.10.2 ad PAGE_SHIFT);
742 1.129.10.2 ad (void)memcpy(sep, &sdp->swd_ose,
743 1.129.10.2 ad sizeof(struct oswapent));
744 1.129.10.2 ad
745 1.129.10.2 ad /* now copy out the path if necessary */
746 1.129.10.2 ad #if !defined(COMPAT_13)
747 1.129.10.2 ad (void) cmd;
748 1.129.10.2 ad #endif
749 1.129.10.2 ad #if defined(COMPAT_13)
750 1.129.10.2 ad if (cmd == SWAP_STATS)
751 1.129.10.2 ad #endif
752 1.129.10.2 ad (void)memcpy(&sep->se_path, sdp->swd_path,
753 1.129.10.2 ad sdp->swd_pathlen);
754 1.129.10.2 ad
755 1.129.10.2 ad count++;
756 1.129.10.2 ad #if defined(COMPAT_13)
757 1.129.10.2 ad if (cmd == SWAP_OSTATS)
758 1.129.10.2 ad sep = (struct swapent *)
759 1.129.10.2 ad ((struct oswapent *)sep + 1);
760 1.129.10.2 ad else
761 1.129.10.2 ad #endif
762 1.129.10.2 ad sep++;
763 1.129.10.2 ad }
764 1.129.10.2 ad }
765 1.129.10.2 ad
766 1.129.10.2 ad *retval = count;
767 1.129.10.2 ad return;
768 1.129.10.2 ad }
769 1.129.10.2 ad
770 1.129.10.2 ad /*
771 1.129.10.2 ad * swap_on: attempt to enable a swapdev for swapping. note that the
772 1.129.10.2 ad * swapdev is already on the global list, but disabled (marked
773 1.129.10.2 ad * SWF_FAKE).
774 1.129.10.2 ad *
775 1.129.10.2 ad * => we avoid the start of the disk (to protect disk labels)
776 1.129.10.2 ad * => we also avoid the miniroot, if we are swapping to root.
777 1.129.10.2 ad * => caller should leave uvm_swap_data_lock unlocked, we may lock it
778 1.129.10.2 ad * if needed.
779 1.129.10.2 ad */
780 1.129.10.2 ad static int
781 1.129.10.2 ad swap_on(struct lwp *l, struct swapdev *sdp)
782 1.129.10.2 ad {
783 1.129.10.2 ad struct vnode *vp;
784 1.129.10.2 ad int error, npages, nblocks, size;
785 1.129.10.2 ad long addr;
786 1.129.10.2 ad u_long result;
787 1.129.10.2 ad struct vattr va;
788 1.129.10.2 ad #ifdef NFS
789 1.129.10.2 ad extern int (**nfsv2_vnodeop_p)(void *);
790 1.129.10.2 ad #endif /* NFS */
791 1.129.10.2 ad const struct bdevsw *bdev;
792 1.129.10.2 ad dev_t dev;
793 1.129.10.2 ad UVMHIST_FUNC("swap_on"); UVMHIST_CALLED(pdhist);
794 1.129.10.2 ad
795 1.129.10.2 ad /*
796 1.129.10.2 ad * we want to enable swapping on sdp. the swd_vp contains
797 1.129.10.2 ad * the vnode we want (locked and ref'd), and the swd_dev
798 1.129.10.2 ad * contains the dev_t of the file, if it a block device.
799 1.129.10.2 ad */
800 1.129.10.2 ad
801 1.129.10.2 ad vp = sdp->swd_vp;
802 1.129.10.2 ad dev = sdp->swd_dev;
803 1.129.10.2 ad
804 1.129.10.2 ad /*
805 1.129.10.2 ad * open the swap file (mostly useful for block device files to
806 1.129.10.2 ad * let device driver know what is up).
807 1.129.10.2 ad *
808 1.129.10.2 ad * we skip the open/close for root on swap because the root
809 1.129.10.2 ad * has already been opened when root was mounted (mountroot).
810 1.129.10.2 ad */
811 1.129.10.2 ad if (vp != rootvp) {
812 1.129.10.2 ad if ((error = VOP_OPEN(vp, FREAD|FWRITE, l->l_cred, l)))
813 1.129.10.2 ad return (error);
814 1.129.10.2 ad }
815 1.129.10.2 ad
816 1.129.10.2 ad /* XXX this only works for block devices */
817 1.129.10.2 ad UVMHIST_LOG(pdhist, " dev=%d, major(dev)=%d", dev, major(dev), 0,0);
818 1.129.10.2 ad
819 1.129.10.2 ad /*
820 1.129.10.2 ad * we now need to determine the size of the swap area. for
821 1.129.10.2 ad * block specials we can call the d_psize function.
822 1.129.10.2 ad * for normal files, we must stat [get attrs].
823 1.129.10.2 ad *
824 1.129.10.2 ad * we put the result in nblks.
825 1.129.10.2 ad * for normal files, we also want the filesystem block size
826 1.129.10.2 ad * (which we get with statfs).
827 1.129.10.2 ad */
828 1.129.10.2 ad switch (vp->v_type) {
829 1.129.10.2 ad case VBLK:
830 1.129.10.2 ad bdev = bdevsw_lookup(dev);
831 1.129.10.2 ad if (bdev == NULL || bdev->d_psize == NULL ||
832 1.129.10.2 ad (nblocks = (*bdev->d_psize)(dev)) == -1) {
833 1.129.10.2 ad error = ENXIO;
834 1.129.10.2 ad goto bad;
835 1.129.10.2 ad }
836 1.129.10.2 ad break;
837 1.129.10.2 ad
838 1.129.10.2 ad case VREG:
839 1.129.10.2 ad if ((error = VOP_GETATTR(vp, &va, l->l_cred, l)))
840 1.129.10.2 ad goto bad;
841 1.129.10.2 ad nblocks = (int)btodb(va.va_size);
842 1.129.10.2 ad if ((error =
843 1.129.10.2 ad VFS_STATVFS(vp->v_mount, &vp->v_mount->mnt_stat, l)) != 0)
844 1.129.10.2 ad goto bad;
845 1.129.10.2 ad
846 1.129.10.2 ad sdp->swd_bsize = vp->v_mount->mnt_stat.f_iosize;
847 1.129.10.2 ad /*
848 1.129.10.2 ad * limit the max # of outstanding I/O requests we issue
849 1.129.10.2 ad * at any one time. take it easy on NFS servers.
850 1.129.10.2 ad */
851 1.129.10.2 ad #ifdef NFS
852 1.129.10.2 ad if (vp->v_op == nfsv2_vnodeop_p)
853 1.129.10.2 ad sdp->swd_maxactive = 2; /* XXX */
854 1.129.10.2 ad else
855 1.129.10.2 ad #endif /* NFS */
856 1.129.10.2 ad sdp->swd_maxactive = 8; /* XXX */
857 1.129.10.2 ad break;
858 1.129.10.2 ad
859 1.129.10.2 ad default:
860 1.129.10.2 ad error = ENXIO;
861 1.129.10.2 ad goto bad;
862 1.129.10.2 ad }
863 1.129.10.2 ad
864 1.129.10.2 ad /*
865 1.129.10.2 ad * save nblocks in a safe place and convert to pages.
866 1.129.10.2 ad */
867 1.129.10.2 ad
868 1.129.10.2 ad sdp->swd_ose.ose_nblks = nblocks;
869 1.129.10.2 ad npages = dbtob((uint64_t)nblocks) >> PAGE_SHIFT;
870 1.129.10.2 ad
871 1.129.10.2 ad /*
872 1.129.10.2 ad * for block special files, we want to make sure that leave
873 1.129.10.2 ad * the disklabel and bootblocks alone, so we arrange to skip
874 1.129.10.2 ad * over them (arbitrarily choosing to skip PAGE_SIZE bytes).
875 1.129.10.2 ad * note that because of this the "size" can be less than the
876 1.129.10.2 ad * actual number of blocks on the device.
877 1.129.10.2 ad */
878 1.129.10.2 ad if (vp->v_type == VBLK) {
879 1.129.10.2 ad /* we use pages 1 to (size - 1) [inclusive] */
880 1.129.10.2 ad size = npages - 1;
881 1.129.10.2 ad addr = 1;
882 1.129.10.2 ad } else {
883 1.129.10.2 ad /* we use pages 0 to (size - 1) [inclusive] */
884 1.129.10.2 ad size = npages;
885 1.129.10.2 ad addr = 0;
886 1.129.10.2 ad }
887 1.129.10.2 ad
888 1.129.10.2 ad /*
889 1.129.10.2 ad * make sure we have enough blocks for a reasonable sized swap
890 1.129.10.2 ad * area. we want at least one page.
891 1.129.10.2 ad */
892 1.129.10.2 ad
893 1.129.10.2 ad if (size < 1) {
894 1.129.10.2 ad UVMHIST_LOG(pdhist, " size <= 1!!", 0, 0, 0, 0);
895 1.129.10.2 ad error = EINVAL;
896 1.129.10.2 ad goto bad;
897 1.129.10.2 ad }
898 1.129.10.2 ad
899 1.129.10.2 ad UVMHIST_LOG(pdhist, " dev=%x: size=%d addr=%ld\n", dev, size, addr, 0);
900 1.129.10.2 ad
901 1.129.10.2 ad /*
902 1.129.10.2 ad * now we need to allocate an extent to manage this swap device
903 1.129.10.2 ad */
904 1.129.10.2 ad
905 1.129.10.2 ad sdp->swd_blist = blist_create(npages);
906 1.129.10.2 ad /* mark all expect the `saved' region free. */
907 1.129.10.2 ad blist_free(sdp->swd_blist, addr, size);
908 1.129.10.2 ad
909 1.129.10.2 ad /*
910 1.129.10.2 ad * if the vnode we are swapping to is the root vnode
911 1.129.10.2 ad * (i.e. we are swapping to the miniroot) then we want
912 1.129.10.2 ad * to make sure we don't overwrite it. do a statfs to
913 1.129.10.2 ad * find its size and skip over it.
914 1.129.10.2 ad */
915 1.129.10.2 ad if (vp == rootvp) {
916 1.129.10.2 ad struct mount *mp;
917 1.129.10.2 ad struct statvfs *sp;
918 1.129.10.2 ad int rootblocks, rootpages;
919 1.129.10.2 ad
920 1.129.10.2 ad mp = rootvnode->v_mount;
921 1.129.10.2 ad sp = &mp->mnt_stat;
922 1.129.10.2 ad rootblocks = sp->f_blocks * btodb(sp->f_frsize);
923 1.129.10.2 ad /*
924 1.129.10.2 ad * XXX: sp->f_blocks isn't the total number of
925 1.129.10.2 ad * blocks in the filesystem, it's the number of
926 1.129.10.2 ad * data blocks. so, our rootblocks almost
927 1.129.10.2 ad * definitely underestimates the total size
928 1.129.10.2 ad * of the filesystem - how badly depends on the
929 1.129.10.2 ad * details of the filesystem type. there isn't
930 1.129.10.2 ad * an obvious way to deal with this cleanly
931 1.129.10.2 ad * and perfectly, so for now we just pad our
932 1.129.10.2 ad * rootblocks estimate with an extra 5 percent.
933 1.129.10.2 ad */
934 1.129.10.2 ad rootblocks += (rootblocks >> 5) +
935 1.129.10.2 ad (rootblocks >> 6) +
936 1.129.10.2 ad (rootblocks >> 7);
937 1.129.10.2 ad rootpages = round_page(dbtob(rootblocks)) >> PAGE_SHIFT;
938 1.129.10.2 ad if (rootpages > size)
939 1.129.10.2 ad panic("swap_on: miniroot larger than swap?");
940 1.129.10.2 ad
941 1.129.10.2 ad if (rootpages != blist_fill(sdp->swd_blist, addr, rootpages)) {
942 1.129.10.2 ad panic("swap_on: unable to preserve miniroot");
943 1.129.10.2 ad }
944 1.129.10.2 ad
945 1.129.10.2 ad size -= rootpages;
946 1.129.10.2 ad printf("Preserved %d pages of miniroot ", rootpages);
947 1.129.10.2 ad printf("leaving %d pages of swap\n", size);
948 1.129.10.2 ad }
949 1.129.10.2 ad
950 1.129.10.2 ad /*
951 1.129.10.2 ad * add a ref to vp to reflect usage as a swap device.
952 1.129.10.2 ad */
953 1.129.10.2 ad vref(vp);
954 1.129.10.2 ad
955 1.129.10.2 ad /*
956 1.129.10.2 ad * now add the new swapdev to the drum and enable.
957 1.129.10.2 ad */
958 1.129.10.2 ad result = vmem_alloc(swapmap, npages, VM_BESTFIT | VM_SLEEP);
959 1.129.10.2 ad if (result == 0)
960 1.129.10.2 ad panic("swapdrum_add");
961 1.129.10.2 ad
962 1.129.10.2 ad sdp->swd_drumoffset = (int)result;
963 1.129.10.2 ad sdp->swd_drumsize = npages;
964 1.129.10.2 ad sdp->swd_npages = size;
965 1.129.10.2 ad mutex_enter(&uvm_swap_data_lock);
966 1.129.10.2 ad sdp->swd_flags &= ~SWF_FAKE; /* going live */
967 1.129.10.2 ad sdp->swd_flags |= (SWF_INUSE|SWF_ENABLE);
968 1.129.10.2 ad uvmexp.swpages += size;
969 1.129.10.2 ad uvmexp.swpgavail += size;
970 1.129.10.2 ad mutex_exit(&uvm_swap_data_lock);
971 1.129.10.2 ad return (0);
972 1.129.10.2 ad
973 1.129.10.2 ad /*
974 1.129.10.2 ad * failure: clean up and return error.
975 1.129.10.2 ad */
976 1.129.10.2 ad
977 1.129.10.2 ad bad:
978 1.129.10.2 ad if (sdp->swd_blist) {
979 1.129.10.2 ad blist_destroy(sdp->swd_blist);
980 1.129.10.2 ad }
981 1.129.10.2 ad if (vp != rootvp) {
982 1.129.10.2 ad (void)VOP_CLOSE(vp, FREAD|FWRITE, l->l_cred, l);
983 1.129.10.2 ad }
984 1.129.10.2 ad return (error);
985 1.129.10.2 ad }
986 1.129.10.2 ad
987 1.129.10.2 ad /*
988 1.129.10.2 ad * swap_off: stop swapping on swapdev
989 1.129.10.2 ad *
990 1.129.10.2 ad * => swap data should be locked, we will unlock.
991 1.129.10.2 ad */
992 1.129.10.2 ad static int
993 1.129.10.2 ad swap_off(struct lwp *l, struct swapdev *sdp)
994 1.129.10.2 ad {
995 1.129.10.2 ad int npages = sdp->swd_npages;
996 1.129.10.2 ad int error = 0;
997 1.129.10.2 ad
998 1.129.10.2 ad UVMHIST_FUNC("swap_off"); UVMHIST_CALLED(pdhist);
999 1.129.10.2 ad UVMHIST_LOG(pdhist, " dev=%x, npages=%d", sdp->swd_dev,npages,0,0);
1000 1.129.10.2 ad
1001 1.129.10.2 ad /* disable the swap area being removed */
1002 1.129.10.2 ad sdp->swd_flags &= ~SWF_ENABLE;
1003 1.129.10.2 ad uvmexp.swpgavail -= npages;
1004 1.129.10.2 ad mutex_exit(&uvm_swap_data_lock);
1005 1.129.10.2 ad
1006 1.129.10.2 ad /*
1007 1.129.10.2 ad * the idea is to find all the pages that are paged out to this
1008 1.129.10.2 ad * device, and page them all in. in uvm, swap-backed pageable
1009 1.129.10.2 ad * memory can take two forms: aobjs and anons. call the
1010 1.129.10.2 ad * swapoff hook for each subsystem to bring in pages.
1011 1.129.10.2 ad */
1012 1.129.10.2 ad
1013 1.129.10.2 ad if (uao_swap_off(sdp->swd_drumoffset,
1014 1.129.10.2 ad sdp->swd_drumoffset + sdp->swd_drumsize) ||
1015 1.129.10.2 ad amap_swap_off(sdp->swd_drumoffset,
1016 1.129.10.2 ad sdp->swd_drumoffset + sdp->swd_drumsize)) {
1017 1.129.10.2 ad error = ENOMEM;
1018 1.129.10.2 ad } else if (sdp->swd_npginuse > sdp->swd_npgbad) {
1019 1.129.10.2 ad error = EBUSY;
1020 1.129.10.2 ad }
1021 1.129.10.2 ad
1022 1.129.10.2 ad if (error) {
1023 1.129.10.2 ad mutex_enter(&uvm_swap_data_lock);
1024 1.129.10.2 ad sdp->swd_flags |= SWF_ENABLE;
1025 1.129.10.2 ad uvmexp.swpgavail += npages;
1026 1.129.10.2 ad mutex_exit(&uvm_swap_data_lock);
1027 1.129.10.2 ad
1028 1.129.10.2 ad return error;
1029 1.129.10.2 ad }
1030 1.129.10.2 ad
1031 1.129.10.2 ad /*
1032 1.129.10.2 ad * done with the vnode.
1033 1.129.10.2 ad * drop our ref on the vnode before calling VOP_CLOSE()
1034 1.129.10.2 ad * so that spec_close() can tell if this is the last close.
1035 1.129.10.2 ad */
1036 1.129.10.2 ad vrele(sdp->swd_vp);
1037 1.129.10.2 ad if (sdp->swd_vp != rootvp) {
1038 1.129.10.2 ad (void) VOP_CLOSE(sdp->swd_vp, FREAD|FWRITE, l->l_cred, l);
1039 1.129.10.2 ad }
1040 1.129.10.2 ad
1041 1.129.10.2 ad mutex_enter(&uvm_swap_data_lock);
1042 1.129.10.2 ad uvmexp.swpages -= npages;
1043 1.129.10.2 ad uvmexp.swpginuse -= sdp->swd_npgbad;
1044 1.129.10.2 ad
1045 1.129.10.2 ad if (swaplist_find(sdp->swd_vp, true) == NULL)
1046 1.129.10.2 ad panic("swap_off: swapdev not in list");
1047 1.129.10.2 ad swaplist_trim();
1048 1.129.10.2 ad mutex_exit(&uvm_swap_data_lock);
1049 1.129.10.2 ad
1050 1.129.10.2 ad /*
1051 1.129.10.2 ad * free all resources!
1052 1.129.10.2 ad */
1053 1.129.10.2 ad vmem_free(swapmap, sdp->swd_drumoffset, sdp->swd_drumsize);
1054 1.129.10.2 ad blist_destroy(sdp->swd_blist);
1055 1.129.10.2 ad bufq_free(sdp->swd_tab);
1056 1.129.10.2 ad free(sdp, M_VMSWAP);
1057 1.129.10.2 ad return (0);
1058 1.129.10.2 ad }
1059 1.129.10.2 ad
1060 1.129.10.2 ad /*
1061 1.129.10.2 ad * /dev/drum interface and i/o functions
1062 1.129.10.2 ad */
1063 1.129.10.2 ad
1064 1.129.10.2 ad /*
1065 1.129.10.2 ad * swstrategy: perform I/O on the drum
1066 1.129.10.2 ad *
1067 1.129.10.2 ad * => we must map the i/o request from the drum to the correct swapdev.
1068 1.129.10.2 ad */
1069 1.129.10.2 ad static void
1070 1.129.10.2 ad swstrategy(struct buf *bp)
1071 1.129.10.2 ad {
1072 1.129.10.2 ad struct swapdev *sdp;
1073 1.129.10.2 ad struct vnode *vp;
1074 1.129.10.2 ad int s, pageno, bn;
1075 1.129.10.2 ad UVMHIST_FUNC("swstrategy"); UVMHIST_CALLED(pdhist);
1076 1.129.10.2 ad
1077 1.129.10.2 ad /*
1078 1.129.10.2 ad * convert block number to swapdev. note that swapdev can't
1079 1.129.10.2 ad * be yanked out from under us because we are holding resources
1080 1.129.10.2 ad * in it (i.e. the blocks we are doing I/O on).
1081 1.129.10.2 ad */
1082 1.129.10.2 ad pageno = dbtob((int64_t)bp->b_blkno) >> PAGE_SHIFT;
1083 1.129.10.2 ad mutex_enter(&uvm_swap_data_lock);
1084 1.129.10.2 ad sdp = swapdrum_getsdp(pageno);
1085 1.129.10.2 ad mutex_exit(&uvm_swap_data_lock);
1086 1.129.10.2 ad if (sdp == NULL) {
1087 1.129.10.2 ad bp->b_error = EINVAL;
1088 1.129.10.2 ad biodone(bp);
1089 1.129.10.2 ad UVMHIST_LOG(pdhist, " failed to get swap device", 0, 0, 0, 0);
1090 1.129.10.2 ad return;
1091 1.129.10.2 ad }
1092 1.129.10.2 ad
1093 1.129.10.2 ad /*
1094 1.129.10.2 ad * convert drum page number to block number on this swapdev.
1095 1.129.10.2 ad */
1096 1.129.10.2 ad
1097 1.129.10.2 ad pageno -= sdp->swd_drumoffset; /* page # on swapdev */
1098 1.129.10.2 ad bn = btodb((uint64_t)pageno << PAGE_SHIFT); /* convert to diskblock */
1099 1.129.10.2 ad
1100 1.129.10.2 ad UVMHIST_LOG(pdhist, " %s: mapoff=%x bn=%x bcount=%ld",
1101 1.129.10.2 ad ((bp->b_flags & B_READ) == 0) ? "write" : "read",
1102 1.129.10.2 ad sdp->swd_drumoffset, bn, bp->b_bcount);
1103 1.129.10.2 ad
1104 1.129.10.2 ad /*
1105 1.129.10.2 ad * for block devices we finish up here.
1106 1.129.10.2 ad * for regular files we have to do more work which we delegate
1107 1.129.10.2 ad * to sw_reg_strategy().
1108 1.129.10.2 ad */
1109 1.129.10.2 ad
1110 1.129.10.2 ad switch (sdp->swd_vp->v_type) {
1111 1.129.10.2 ad default:
1112 1.129.10.2 ad panic("swstrategy: vnode type 0x%x", sdp->swd_vp->v_type);
1113 1.129.10.2 ad
1114 1.129.10.2 ad case VBLK:
1115 1.129.10.2 ad
1116 1.129.10.2 ad /*
1117 1.129.10.2 ad * must convert "bp" from an I/O on /dev/drum to an I/O
1118 1.129.10.2 ad * on the swapdev (sdp).
1119 1.129.10.2 ad */
1120 1.129.10.2 ad s = splbio();
1121 1.129.10.2 ad bp->b_blkno = bn; /* swapdev block number */
1122 1.129.10.2 ad vp = sdp->swd_vp; /* swapdev vnode pointer */
1123 1.129.10.2 ad bp->b_dev = sdp->swd_dev; /* swapdev dev_t */
1124 1.129.10.2 ad
1125 1.129.10.2 ad /*
1126 1.129.10.2 ad * if we are doing a write, we have to redirect the i/o on
1127 1.129.10.2 ad * drum's v_numoutput counter to the swapdevs.
1128 1.129.10.2 ad */
1129 1.129.10.2 ad if ((bp->b_flags & B_READ) == 0) {
1130 1.129.10.2 ad vwakeup(bp); /* kills one 'v_numoutput' on drum */
1131 1.129.10.2 ad V_INCR_NUMOUTPUT(vp); /* put it on swapdev */
1132 1.129.10.2 ad }
1133 1.129.10.2 ad
1134 1.129.10.2 ad /*
1135 1.129.10.2 ad * finally plug in swapdev vnode and start I/O
1136 1.129.10.2 ad */
1137 1.129.10.2 ad bp->b_vp = vp;
1138 1.129.10.2 ad splx(s);
1139 1.129.10.2 ad VOP_STRATEGY(vp, bp);
1140 1.129.10.2 ad return;
1141 1.129.10.2 ad
1142 1.129.10.2 ad case VREG:
1143 1.129.10.2 ad /*
1144 1.129.10.2 ad * delegate to sw_reg_strategy function.
1145 1.129.10.2 ad */
1146 1.129.10.2 ad sw_reg_strategy(sdp, bp, bn);
1147 1.129.10.2 ad return;
1148 1.129.10.2 ad }
1149 1.129.10.2 ad /* NOTREACHED */
1150 1.129.10.2 ad }
1151 1.129.10.2 ad
1152 1.129.10.2 ad /*
1153 1.129.10.2 ad * swread: the read function for the drum (just a call to physio)
1154 1.129.10.2 ad */
1155 1.129.10.2 ad /*ARGSUSED*/
1156 1.129.10.2 ad static int
1157 1.129.10.2 ad swread(dev_t dev, struct uio *uio, int ioflag)
1158 1.129.10.2 ad {
1159 1.129.10.2 ad UVMHIST_FUNC("swread"); UVMHIST_CALLED(pdhist);
1160 1.129.10.2 ad
1161 1.129.10.2 ad UVMHIST_LOG(pdhist, " dev=%x offset=%qx", dev, uio->uio_offset, 0, 0);
1162 1.129.10.2 ad return (physio(swstrategy, NULL, dev, B_READ, minphys, uio));
1163 1.129.10.2 ad }
1164 1.129.10.2 ad
1165 1.129.10.2 ad /*
1166 1.129.10.2 ad * swwrite: the write function for the drum (just a call to physio)
1167 1.129.10.2 ad */
1168 1.129.10.2 ad /*ARGSUSED*/
1169 1.129.10.2 ad static int
1170 1.129.10.2 ad swwrite(dev_t dev, struct uio *uio, int ioflag)
1171 1.129.10.2 ad {
1172 1.129.10.2 ad UVMHIST_FUNC("swwrite"); UVMHIST_CALLED(pdhist);
1173 1.129.10.2 ad
1174 1.129.10.2 ad UVMHIST_LOG(pdhist, " dev=%x offset=%qx", dev, uio->uio_offset, 0, 0);
1175 1.129.10.2 ad return (physio(swstrategy, NULL, dev, B_WRITE, minphys, uio));
1176 1.129.10.2 ad }
1177 1.129.10.2 ad
1178 1.129.10.2 ad const struct bdevsw swap_bdevsw = {
1179 1.129.10.2 ad noopen, noclose, swstrategy, noioctl, nodump, nosize, D_OTHER,
1180 1.129.10.2 ad };
1181 1.129.10.2 ad
1182 1.129.10.2 ad const struct cdevsw swap_cdevsw = {
1183 1.129.10.2 ad nullopen, nullclose, swread, swwrite, noioctl,
1184 1.129.10.2 ad nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
1185 1.129.10.2 ad };
1186 1.129.10.2 ad
1187 1.129.10.2 ad /*
1188 1.129.10.2 ad * sw_reg_strategy: handle swap i/o to regular files
1189 1.129.10.2 ad */
1190 1.129.10.2 ad static void
1191 1.129.10.2 ad sw_reg_strategy(struct swapdev *sdp, struct buf *bp, int bn)
1192 1.129.10.2 ad {
1193 1.129.10.2 ad struct vnode *vp;
1194 1.129.10.2 ad struct vndxfer *vnx;
1195 1.129.10.2 ad daddr_t nbn;
1196 1.129.10.2 ad char *addr;
1197 1.129.10.2 ad off_t byteoff;
1198 1.129.10.2 ad int s, off, nra, error, sz, resid;
1199 1.129.10.2 ad UVMHIST_FUNC("sw_reg_strategy"); UVMHIST_CALLED(pdhist);
1200 1.129.10.2 ad
1201 1.129.10.2 ad /*
1202 1.129.10.2 ad * allocate a vndxfer head for this transfer and point it to
1203 1.129.10.2 ad * our buffer.
1204 1.129.10.2 ad */
1205 1.129.10.2 ad getvndxfer(vnx);
1206 1.129.10.2 ad vnx->vx_flags = VX_BUSY;
1207 1.129.10.2 ad vnx->vx_error = 0;
1208 1.129.10.2 ad vnx->vx_pending = 0;
1209 1.129.10.2 ad vnx->vx_bp = bp;
1210 1.129.10.2 ad vnx->vx_sdp = sdp;
1211 1.129.10.2 ad
1212 1.129.10.2 ad /*
1213 1.129.10.2 ad * setup for main loop where we read filesystem blocks into
1214 1.129.10.2 ad * our buffer.
1215 1.129.10.2 ad */
1216 1.129.10.2 ad error = 0;
1217 1.129.10.2 ad bp->b_resid = bp->b_bcount; /* nothing transfered yet! */
1218 1.129.10.2 ad addr = bp->b_data; /* current position in buffer */
1219 1.129.10.2 ad byteoff = dbtob((uint64_t)bn);
1220 1.129.10.2 ad
1221 1.129.10.2 ad for (resid = bp->b_resid; resid; resid -= sz) {
1222 1.129.10.2 ad struct vndbuf *nbp;
1223 1.129.10.2 ad
1224 1.129.10.2 ad /*
1225 1.129.10.2 ad * translate byteoffset into block number. return values:
1226 1.129.10.2 ad * vp = vnode of underlying device
1227 1.129.10.2 ad * nbn = new block number (on underlying vnode dev)
1228 1.129.10.2 ad * nra = num blocks we can read-ahead (excludes requested
1229 1.129.10.2 ad * block)
1230 1.129.10.2 ad */
1231 1.129.10.2 ad nra = 0;
1232 1.129.10.2 ad error = VOP_BMAP(sdp->swd_vp, byteoff / sdp->swd_bsize,
1233 1.129.10.2 ad &vp, &nbn, &nra);
1234 1.129.10.2 ad
1235 1.129.10.2 ad if (error == 0 && nbn == (daddr_t)-1) {
1236 1.129.10.2 ad /*
1237 1.129.10.2 ad * this used to just set error, but that doesn't
1238 1.129.10.2 ad * do the right thing. Instead, it causes random
1239 1.129.10.2 ad * memory errors. The panic() should remain until
1240 1.129.10.2 ad * this condition doesn't destabilize the system.
1241 1.129.10.2 ad */
1242 1.129.10.2 ad #if 1
1243 1.129.10.2 ad panic("sw_reg_strategy: swap to sparse file");
1244 1.129.10.2 ad #else
1245 1.129.10.2 ad error = EIO; /* failure */
1246 1.129.10.2 ad #endif
1247 1.129.10.2 ad }
1248 1.129.10.2 ad
1249 1.129.10.2 ad /*
1250 1.129.10.2 ad * punt if there was an error or a hole in the file.
1251 1.129.10.2 ad * we must wait for any i/o ops we have already started
1252 1.129.10.2 ad * to finish before returning.
1253 1.129.10.2 ad *
1254 1.129.10.2 ad * XXX we could deal with holes here but it would be
1255 1.129.10.2 ad * a hassle (in the write case).
1256 1.129.10.2 ad */
1257 1.129.10.2 ad if (error) {
1258 1.129.10.2 ad s = splbio();
1259 1.129.10.2 ad vnx->vx_error = error; /* pass error up */
1260 1.129.10.2 ad goto out;
1261 1.129.10.2 ad }
1262 1.129.10.2 ad
1263 1.129.10.2 ad /*
1264 1.129.10.2 ad * compute the size ("sz") of this transfer (in bytes).
1265 1.129.10.2 ad */
1266 1.129.10.2 ad off = byteoff % sdp->swd_bsize;
1267 1.129.10.2 ad sz = (1 + nra) * sdp->swd_bsize - off;
1268 1.129.10.2 ad if (sz > resid)
1269 1.129.10.2 ad sz = resid;
1270 1.129.10.2 ad
1271 1.129.10.2 ad UVMHIST_LOG(pdhist, "sw_reg_strategy: "
1272 1.129.10.2 ad "vp %p/%p offset 0x%x/0x%x",
1273 1.129.10.2 ad sdp->swd_vp, vp, byteoff, nbn);
1274 1.129.10.2 ad
1275 1.129.10.2 ad /*
1276 1.129.10.2 ad * now get a buf structure. note that the vb_buf is
1277 1.129.10.2 ad * at the front of the nbp structure so that you can
1278 1.129.10.2 ad * cast pointers between the two structure easily.
1279 1.129.10.2 ad */
1280 1.129.10.2 ad getvndbuf(nbp);
1281 1.129.10.2 ad BUF_INIT(&nbp->vb_buf);
1282 1.129.10.2 ad nbp->vb_buf.b_flags = bp->b_flags | B_CALL;
1283 1.129.10.2 ad nbp->vb_buf.b_bcount = sz;
1284 1.129.10.2 ad nbp->vb_buf.b_bufsize = sz;
1285 1.129.10.2 ad nbp->vb_buf.b_error = 0;
1286 1.129.10.2 ad nbp->vb_buf.b_data = addr;
1287 1.129.10.2 ad nbp->vb_buf.b_lblkno = 0;
1288 1.129.10.2 ad nbp->vb_buf.b_blkno = nbn + btodb(off);
1289 1.129.10.2 ad nbp->vb_buf.b_rawblkno = nbp->vb_buf.b_blkno;
1290 1.129.10.2 ad nbp->vb_buf.b_iodone = sw_reg_iodone;
1291 1.129.10.2 ad nbp->vb_buf.b_vp = vp;
1292 1.129.10.2 ad if (vp->v_type == VBLK) {
1293 1.129.10.2 ad nbp->vb_buf.b_dev = vp->v_rdev;
1294 1.129.10.2 ad }
1295 1.129.10.2 ad
1296 1.129.10.2 ad nbp->vb_xfer = vnx; /* patch it back in to vnx */
1297 1.129.10.2 ad
1298 1.129.10.2 ad /*
1299 1.129.10.2 ad * Just sort by block number
1300 1.129.10.2 ad */
1301 1.129.10.2 ad s = splbio();
1302 1.129.10.2 ad if (vnx->vx_error != 0) {
1303 1.129.10.2 ad putvndbuf(nbp);
1304 1.129.10.2 ad goto out;
1305 1.129.10.2 ad }
1306 1.129.10.2 ad vnx->vx_pending++;
1307 1.129.10.2 ad
1308 1.129.10.2 ad /* sort it in and start I/O if we are not over our limit */
1309 1.129.10.2 ad BUFQ_PUT(sdp->swd_tab, &nbp->vb_buf);
1310 1.129.10.2 ad sw_reg_start(sdp);
1311 1.129.10.2 ad splx(s);
1312 1.129.10.2 ad
1313 1.129.10.2 ad /*
1314 1.129.10.2 ad * advance to the next I/O
1315 1.129.10.2 ad */
1316 1.129.10.2 ad byteoff += sz;
1317 1.129.10.2 ad addr += sz;
1318 1.129.10.2 ad }
1319 1.129.10.2 ad
1320 1.129.10.2 ad s = splbio();
1321 1.129.10.2 ad
1322 1.129.10.2 ad out: /* Arrive here at splbio */
1323 1.129.10.2 ad vnx->vx_flags &= ~VX_BUSY;
1324 1.129.10.2 ad if (vnx->vx_pending == 0) {
1325 1.129.10.2 ad if (vnx->vx_error != 0)
1326 1.129.10.2 ad bp->b_error = vnx->vx_error;
1327 1.129.10.2 ad putvndxfer(vnx);
1328 1.129.10.2 ad biodone(bp);
1329 1.129.10.2 ad }
1330 1.129.10.2 ad splx(s);
1331 1.129.10.2 ad }
1332 1.129.10.2 ad
1333 1.129.10.2 ad /*
1334 1.129.10.2 ad * sw_reg_start: start an I/O request on the requested swapdev
1335 1.129.10.2 ad *
1336 1.129.10.2 ad * => reqs are sorted by b_rawblkno (above)
1337 1.129.10.2 ad */
1338 1.129.10.2 ad static void
1339 1.129.10.2 ad sw_reg_start(struct swapdev *sdp)
1340 1.129.10.2 ad {
1341 1.129.10.2 ad struct buf *bp;
1342 1.129.10.2 ad UVMHIST_FUNC("sw_reg_start"); UVMHIST_CALLED(pdhist);
1343 1.129.10.2 ad
1344 1.129.10.2 ad /* recursion control */
1345 1.129.10.2 ad if ((sdp->swd_flags & SWF_BUSY) != 0)
1346 1.129.10.2 ad return;
1347 1.129.10.2 ad
1348 1.129.10.2 ad sdp->swd_flags |= SWF_BUSY;
1349 1.129.10.2 ad
1350 1.129.10.2 ad while (sdp->swd_active < sdp->swd_maxactive) {
1351 1.129.10.2 ad bp = BUFQ_GET(sdp->swd_tab);
1352 1.129.10.2 ad if (bp == NULL)
1353 1.129.10.2 ad break;
1354 1.129.10.2 ad sdp->swd_active++;
1355 1.129.10.2 ad
1356 1.129.10.2 ad UVMHIST_LOG(pdhist,
1357 1.129.10.2 ad "sw_reg_start: bp %p vp %p blkno %p cnt %lx",
1358 1.129.10.2 ad bp, bp->b_vp, bp->b_blkno, bp->b_bcount);
1359 1.129.10.2 ad if ((bp->b_flags & B_READ) == 0)
1360 1.129.10.2 ad V_INCR_NUMOUTPUT(bp->b_vp);
1361 1.129.10.2 ad
1362 1.129.10.2 ad VOP_STRATEGY(bp->b_vp, bp);
1363 1.129.10.2 ad }
1364 1.129.10.2 ad sdp->swd_flags &= ~SWF_BUSY;
1365 1.129.10.2 ad }
1366 1.129.10.2 ad
1367 1.129.10.2 ad /*
1368 1.129.10.2 ad * sw_reg_iodone: one of our i/o's has completed and needs post-i/o cleanup
1369 1.129.10.2 ad *
1370 1.129.10.2 ad * => note that we can recover the vndbuf struct by casting the buf ptr
1371 1.129.10.2 ad */
1372 1.129.10.2 ad static void
1373 1.129.10.2 ad sw_reg_iodone(struct buf *bp)
1374 1.129.10.2 ad {
1375 1.129.10.2 ad struct vndbuf *vbp = (struct vndbuf *) bp;
1376 1.129.10.2 ad struct vndxfer *vnx = vbp->vb_xfer;
1377 1.129.10.2 ad struct buf *pbp = vnx->vx_bp; /* parent buffer */
1378 1.129.10.2 ad struct swapdev *sdp = vnx->vx_sdp;
1379 1.129.10.2 ad int s, resid, error;
1380 1.129.10.2 ad UVMHIST_FUNC("sw_reg_iodone"); UVMHIST_CALLED(pdhist);
1381 1.129.10.2 ad
1382 1.129.10.2 ad UVMHIST_LOG(pdhist, " vbp=%p vp=%p blkno=%x addr=%p",
1383 1.129.10.2 ad vbp, vbp->vb_buf.b_vp, vbp->vb_buf.b_blkno, vbp->vb_buf.b_data);
1384 1.129.10.2 ad UVMHIST_LOG(pdhist, " cnt=%lx resid=%lx",
1385 1.129.10.2 ad vbp->vb_buf.b_bcount, vbp->vb_buf.b_resid, 0, 0);
1386 1.129.10.2 ad
1387 1.129.10.2 ad /*
1388 1.129.10.2 ad * protect vbp at splbio and update.
1389 1.129.10.2 ad */
1390 1.129.10.2 ad
1391 1.129.10.2 ad s = splbio();
1392 1.129.10.2 ad resid = vbp->vb_buf.b_bcount - vbp->vb_buf.b_resid;
1393 1.129.10.2 ad pbp->b_resid -= resid;
1394 1.129.10.2 ad vnx->vx_pending--;
1395 1.129.10.2 ad
1396 1.129.10.2 ad if (vbp->vb_buf.b_error != 0) {
1397 1.129.10.2 ad /* pass error upward */
1398 1.129.10.2 ad error = vbp->vb_buf.b_error;
1399 1.129.10.2 ad UVMHIST_LOG(pdhist, " got error=%d !", error, 0, 0, 0);
1400 1.129.10.2 ad vnx->vx_error = error;
1401 1.129.10.2 ad }
1402 1.129.10.2 ad
1403 1.129.10.2 ad /*
1404 1.129.10.2 ad * kill vbp structure
1405 1.129.10.2 ad */
1406 1.129.10.2 ad putvndbuf(vbp);
1407 1.129.10.2 ad
1408 1.129.10.2 ad /*
1409 1.129.10.2 ad * wrap up this transaction if it has run to completion or, in
1410 1.129.10.2 ad * case of an error, when all auxiliary buffers have returned.
1411 1.129.10.2 ad */
1412 1.129.10.2 ad if (vnx->vx_error != 0) {
1413 1.129.10.2 ad /* pass error upward */
1414 1.129.10.2 ad pbp->b_error = vnx->vx_error;
1415 1.129.10.2 ad if ((vnx->vx_flags & VX_BUSY) == 0 && vnx->vx_pending == 0) {
1416 1.129.10.2 ad putvndxfer(vnx);
1417 1.129.10.2 ad biodone(pbp);
1418 1.129.10.2 ad }
1419 1.129.10.2 ad } else if (pbp->b_resid == 0) {
1420 1.129.10.2 ad KASSERT(vnx->vx_pending == 0);
1421 1.129.10.2 ad if ((vnx->vx_flags & VX_BUSY) == 0) {
1422 1.129.10.2 ad UVMHIST_LOG(pdhist, " iodone error=%d !",
1423 1.129.10.2 ad pbp, vnx->vx_error, 0, 0);
1424 1.129.10.2 ad putvndxfer(vnx);
1425 1.129.10.2 ad biodone(pbp);
1426 1.129.10.2 ad }
1427 1.129.10.2 ad }
1428 1.129.10.2 ad
1429 1.129.10.2 ad /*
1430 1.129.10.2 ad * done! start next swapdev I/O if one is pending
1431 1.129.10.2 ad */
1432 1.129.10.2 ad sdp->swd_active--;
1433 1.129.10.2 ad sw_reg_start(sdp);
1434 1.129.10.2 ad splx(s);
1435 1.129.10.2 ad }
1436 1.129.10.2 ad
1437 1.129.10.2 ad
1438 1.129.10.2 ad /*
1439 1.129.10.2 ad * uvm_swap_alloc: allocate space on swap
1440 1.129.10.2 ad *
1441 1.129.10.2 ad * => allocation is done "round robin" down the priority list, as we
1442 1.129.10.2 ad * allocate in a priority we "rotate" the circle queue.
1443 1.129.10.2 ad * => space can be freed with uvm_swap_free
1444 1.129.10.2 ad * => we return the page slot number in /dev/drum (0 == invalid slot)
1445 1.129.10.2 ad * => we lock uvm_swap_data_lock
1446 1.129.10.2 ad * => XXXMRG: "LESSOK" INTERFACE NEEDED TO EXTENT SYSTEM
1447 1.129.10.2 ad */
1448 1.129.10.2 ad int
1449 1.129.10.2 ad uvm_swap_alloc(int *nslots /* IN/OUT */, bool lessok)
1450 1.129.10.2 ad {
1451 1.129.10.2 ad struct swapdev *sdp;
1452 1.129.10.2 ad struct swappri *spp;
1453 1.129.10.2 ad UVMHIST_FUNC("uvm_swap_alloc"); UVMHIST_CALLED(pdhist);
1454 1.129.10.2 ad
1455 1.129.10.2 ad /*
1456 1.129.10.2 ad * no swap devices configured yet? definite failure.
1457 1.129.10.2 ad */
1458 1.129.10.2 ad if (uvmexp.nswapdev < 1)
1459 1.129.10.2 ad return 0;
1460 1.129.10.2 ad
1461 1.129.10.2 ad /*
1462 1.129.10.2 ad * lock data lock, convert slots into blocks, and enter loop
1463 1.129.10.2 ad */
1464 1.129.10.2 ad mutex_enter(&uvm_swap_data_lock);
1465 1.129.10.2 ad
1466 1.129.10.2 ad ReTry: /* XXXMRG */
1467 1.129.10.2 ad LIST_FOREACH(spp, &swap_priority, spi_swappri) {
1468 1.129.10.2 ad CIRCLEQ_FOREACH(sdp, &spp->spi_swapdev, swd_next) {
1469 1.129.10.2 ad uint64_t result;
1470 1.129.10.2 ad
1471 1.129.10.2 ad /* if it's not enabled, then we can't swap from it */
1472 1.129.10.2 ad if ((sdp->swd_flags & SWF_ENABLE) == 0)
1473 1.129.10.2 ad continue;
1474 1.129.10.2 ad if (sdp->swd_npginuse + *nslots > sdp->swd_npages)
1475 1.129.10.2 ad continue;
1476 1.129.10.2 ad result = blist_alloc(sdp->swd_blist, *nslots);
1477 1.129.10.2 ad if (result == BLIST_NONE) {
1478 1.129.10.2 ad continue;
1479 1.129.10.2 ad }
1480 1.129.10.2 ad KASSERT(result < sdp->swd_drumsize);
1481 1.129.10.2 ad
1482 1.129.10.2 ad /*
1483 1.129.10.2 ad * successful allocation! now rotate the circleq.
1484 1.129.10.2 ad */
1485 1.129.10.2 ad CIRCLEQ_REMOVE(&spp->spi_swapdev, sdp, swd_next);
1486 1.129.10.2 ad CIRCLEQ_INSERT_TAIL(&spp->spi_swapdev, sdp, swd_next);
1487 1.129.10.2 ad sdp->swd_npginuse += *nslots;
1488 1.129.10.2 ad uvmexp.swpginuse += *nslots;
1489 1.129.10.2 ad mutex_exit(&uvm_swap_data_lock);
1490 1.129.10.2 ad /* done! return drum slot number */
1491 1.129.10.2 ad UVMHIST_LOG(pdhist,
1492 1.129.10.2 ad "success! returning %d slots starting at %d",
1493 1.129.10.2 ad *nslots, result + sdp->swd_drumoffset, 0, 0);
1494 1.129.10.2 ad return (result + sdp->swd_drumoffset);
1495 1.129.10.2 ad }
1496 1.129.10.2 ad }
1497 1.129.10.2 ad
1498 1.129.10.2 ad /* XXXMRG: BEGIN HACK */
1499 1.129.10.2 ad if (*nslots > 1 && lessok) {
1500 1.129.10.2 ad *nslots = 1;
1501 1.129.10.2 ad /* XXXMRG: ugh! blist should support this for us */
1502 1.129.10.2 ad goto ReTry;
1503 1.129.10.2 ad }
1504 1.129.10.2 ad /* XXXMRG: END HACK */
1505 1.129.10.2 ad
1506 1.129.10.2 ad mutex_exit(&uvm_swap_data_lock);
1507 1.129.10.2 ad return 0;
1508 1.129.10.2 ad }
1509 1.129.10.2 ad
1510 1.129.10.2 ad bool
1511 1.129.10.2 ad uvm_swapisfull(void)
1512 1.129.10.2 ad {
1513 1.129.10.2 ad bool rv;
1514 1.129.10.2 ad
1515 1.129.10.2 ad mutex_enter(&uvm_swap_data_lock);
1516 1.129.10.2 ad KASSERT(uvmexp.swpgonly <= uvmexp.swpages);
1517 1.129.10.2 ad rv = (uvmexp.swpgonly >= uvmexp.swpgavail);
1518 1.129.10.2 ad mutex_exit(&uvm_swap_data_lock);
1519 1.129.10.2 ad
1520 1.129.10.2 ad return (rv);
1521 1.129.10.2 ad }
1522 1.129.10.2 ad
1523 1.129.10.2 ad /*
1524 1.129.10.2 ad * uvm_swap_markbad: keep track of swap ranges where we've had i/o errors
1525 1.129.10.2 ad *
1526 1.129.10.2 ad * => we lock uvm_swap_data_lock
1527 1.129.10.2 ad */
1528 1.129.10.2 ad void
1529 1.129.10.2 ad uvm_swap_markbad(int startslot, int nslots)
1530 1.129.10.2 ad {
1531 1.129.10.2 ad struct swapdev *sdp;
1532 1.129.10.2 ad UVMHIST_FUNC("uvm_swap_markbad"); UVMHIST_CALLED(pdhist);
1533 1.129.10.2 ad
1534 1.129.10.2 ad mutex_enter(&uvm_swap_data_lock);
1535 1.129.10.2 ad sdp = swapdrum_getsdp(startslot);
1536 1.129.10.2 ad KASSERT(sdp != NULL);
1537 1.129.10.2 ad
1538 1.129.10.2 ad /*
1539 1.129.10.2 ad * we just keep track of how many pages have been marked bad
1540 1.129.10.2 ad * in this device, to make everything add up in swap_off().
1541 1.129.10.2 ad * we assume here that the range of slots will all be within
1542 1.129.10.2 ad * one swap device.
1543 1.129.10.2 ad */
1544 1.129.10.2 ad
1545 1.129.10.2 ad KASSERT(uvmexp.swpgonly >= nslots);
1546 1.129.10.2 ad uvmexp.swpgonly -= nslots;
1547 1.129.10.2 ad sdp->swd_npgbad += nslots;
1548 1.129.10.2 ad UVMHIST_LOG(pdhist, "now %d bad", sdp->swd_npgbad, 0,0,0);
1549 1.129.10.2 ad mutex_exit(&uvm_swap_data_lock);
1550 1.129.10.2 ad }
1551 1.129.10.2 ad
1552 1.129.10.2 ad /*
1553 1.129.10.2 ad * uvm_swap_free: free swap slots
1554 1.129.10.2 ad *
1555 1.129.10.2 ad * => this can be all or part of an allocation made by uvm_swap_alloc
1556 1.129.10.2 ad * => we lock uvm_swap_data_lock
1557 1.129.10.2 ad */
1558 1.129.10.2 ad void
1559 1.129.10.2 ad uvm_swap_free(int startslot, int nslots)
1560 1.129.10.2 ad {
1561 1.129.10.2 ad struct swapdev *sdp;
1562 1.129.10.2 ad UVMHIST_FUNC("uvm_swap_free"); UVMHIST_CALLED(pdhist);
1563 1.129.10.2 ad
1564 1.129.10.2 ad UVMHIST_LOG(pdhist, "freeing %d slots starting at %d", nslots,
1565 1.129.10.2 ad startslot, 0, 0);
1566 1.129.10.2 ad
1567 1.129.10.2 ad /*
1568 1.129.10.2 ad * ignore attempts to free the "bad" slot.
1569 1.129.10.2 ad */
1570 1.129.10.2 ad
1571 1.129.10.2 ad if (startslot == SWSLOT_BAD) {
1572 1.129.10.2 ad return;
1573 1.129.10.2 ad }
1574 1.129.10.2 ad
1575 1.129.10.2 ad /*
1576 1.129.10.2 ad * convert drum slot offset back to sdp, free the blocks
1577 1.129.10.2 ad * in the extent, and return. must hold pri lock to do
1578 1.129.10.2 ad * lookup and access the extent.
1579 1.129.10.2 ad */
1580 1.129.10.2 ad
1581 1.129.10.2 ad mutex_enter(&uvm_swap_data_lock);
1582 1.129.10.2 ad sdp = swapdrum_getsdp(startslot);
1583 1.129.10.2 ad KASSERT(uvmexp.nswapdev >= 1);
1584 1.129.10.2 ad KASSERT(sdp != NULL);
1585 1.129.10.2 ad KASSERT(sdp->swd_npginuse >= nslots);
1586 1.129.10.2 ad blist_free(sdp->swd_blist, startslot - sdp->swd_drumoffset, nslots);
1587 1.129.10.2 ad sdp->swd_npginuse -= nslots;
1588 1.129.10.2 ad uvmexp.swpginuse -= nslots;
1589 1.129.10.2 ad mutex_exit(&uvm_swap_data_lock);
1590 1.129.10.2 ad }
1591 1.129.10.2 ad
1592 1.129.10.2 ad /*
1593 1.129.10.2 ad * uvm_swap_put: put any number of pages into a contig place on swap
1594 1.129.10.2 ad *
1595 1.129.10.2 ad * => can be sync or async
1596 1.129.10.2 ad */
1597 1.129.10.2 ad
1598 1.129.10.2 ad int
1599 1.129.10.2 ad uvm_swap_put(int swslot, struct vm_page **ppsp, int npages, int flags)
1600 1.129.10.2 ad {
1601 1.129.10.2 ad int error;
1602 1.129.10.2 ad
1603 1.129.10.2 ad error = uvm_swap_io(ppsp, swslot, npages, B_WRITE |
1604 1.129.10.2 ad ((flags & PGO_SYNCIO) ? 0 : B_ASYNC));
1605 1.129.10.2 ad return error;
1606 1.129.10.2 ad }
1607 1.129.10.2 ad
1608 1.129.10.2 ad /*
1609 1.129.10.2 ad * uvm_swap_get: get a single page from swap
1610 1.129.10.2 ad *
1611 1.129.10.2 ad * => usually a sync op (from fault)
1612 1.129.10.2 ad */
1613 1.129.10.2 ad
1614 1.129.10.2 ad int
1615 1.129.10.2 ad uvm_swap_get(struct vm_page *page, int swslot, int flags)
1616 1.129.10.2 ad {
1617 1.129.10.2 ad int error;
1618 1.129.10.2 ad
1619 1.129.10.2 ad uvmexp.nswget++;
1620 1.129.10.2 ad KASSERT(flags & PGO_SYNCIO);
1621 1.129.10.2 ad if (swslot == SWSLOT_BAD) {
1622 1.129.10.2 ad return EIO;
1623 1.129.10.2 ad }
1624 1.129.10.2 ad
1625 1.129.10.2 ad error = uvm_swap_io(&page, swslot, 1, B_READ |
1626 1.129.10.2 ad ((flags & PGO_SYNCIO) ? 0 : B_ASYNC));
1627 1.129.10.2 ad if (error == 0) {
1628 1.129.10.2 ad
1629 1.129.10.2 ad /*
1630 1.129.10.2 ad * this page is no longer only in swap.
1631 1.129.10.2 ad */
1632 1.129.10.2 ad
1633 1.129.10.2 ad mutex_enter(&uvm_swap_data_lock);
1634 1.129.10.2 ad KASSERT(uvmexp.swpgonly > 0);
1635 1.129.10.2 ad uvmexp.swpgonly--;
1636 1.129.10.2 ad mutex_exit(&uvm_swap_data_lock);
1637 1.129.10.2 ad }
1638 1.129.10.2 ad return error;
1639 1.129.10.2 ad }
1640 1.129.10.2 ad
1641 1.129.10.2 ad /*
1642 1.129.10.2 ad * uvm_swap_io: do an i/o operation to swap
1643 1.129.10.2 ad */
1644 1.129.10.2 ad
1645 1.129.10.2 ad static int
1646 1.129.10.2 ad uvm_swap_io(struct vm_page **pps, int startslot, int npages, int flags)
1647 1.129.10.2 ad {
1648 1.129.10.2 ad daddr_t startblk;
1649 1.129.10.2 ad struct buf *bp;
1650 1.129.10.2 ad vaddr_t kva;
1651 1.129.10.2 ad int error, s, mapinflags;
1652 1.129.10.2 ad bool write, async;
1653 1.129.10.2 ad UVMHIST_FUNC("uvm_swap_io"); UVMHIST_CALLED(pdhist);
1654 1.129.10.2 ad
1655 1.129.10.2 ad UVMHIST_LOG(pdhist, "<- called, startslot=%d, npages=%d, flags=%d",
1656 1.129.10.2 ad startslot, npages, flags, 0);
1657 1.129.10.2 ad
1658 1.129.10.2 ad write = (flags & B_READ) == 0;
1659 1.129.10.2 ad async = (flags & B_ASYNC) != 0;
1660 1.129.10.2 ad
1661 1.129.10.2 ad /*
1662 1.129.10.2 ad * convert starting drum slot to block number
1663 1.129.10.2 ad */
1664 1.129.10.2 ad
1665 1.129.10.2 ad startblk = btodb((uint64_t)startslot << PAGE_SHIFT);
1666 1.129.10.2 ad
1667 1.129.10.2 ad /*
1668 1.129.10.2 ad * first, map the pages into the kernel.
1669 1.129.10.2 ad */
1670 1.129.10.2 ad
1671 1.129.10.2 ad mapinflags = !write ?
1672 1.129.10.2 ad UVMPAGER_MAPIN_WAITOK|UVMPAGER_MAPIN_READ :
1673 1.129.10.2 ad UVMPAGER_MAPIN_WAITOK|UVMPAGER_MAPIN_WRITE;
1674 1.129.10.2 ad kva = uvm_pagermapin(pps, npages, mapinflags);
1675 1.129.10.2 ad
1676 1.129.10.2 ad /*
1677 1.129.10.2 ad * now allocate a buf for the i/o.
1678 1.129.10.2 ad */
1679 1.129.10.2 ad
1680 1.129.10.2 ad bp = getiobuf();
1681 1.129.10.2 ad
1682 1.129.10.2 ad /*
1683 1.129.10.2 ad * fill in the bp/sbp. we currently route our i/o through
1684 1.129.10.2 ad * /dev/drum's vnode [swapdev_vp].
1685 1.129.10.2 ad */
1686 1.129.10.2 ad
1687 1.129.10.2 ad bp->b_flags = B_BUSY | B_NOCACHE | (flags & (B_READ|B_ASYNC));
1688 1.129.10.2 ad bp->b_proc = &proc0; /* XXX */
1689 1.129.10.2 ad bp->b_vnbufs.le_next = NOLIST;
1690 1.129.10.2 ad bp->b_data = (void *)kva;
1691 1.129.10.2 ad bp->b_blkno = startblk;
1692 1.129.10.2 ad bp->b_vp = swapdev_vp;
1693 1.129.10.2 ad bp->b_bufsize = bp->b_bcount = npages << PAGE_SHIFT;
1694 1.129.10.2 ad
1695 1.129.10.2 ad /*
1696 1.129.10.2 ad * bump v_numoutput (counter of number of active outputs).
1697 1.129.10.2 ad */
1698 1.129.10.2 ad
1699 1.129.10.2 ad if (write) {
1700 1.129.10.2 ad s = splbio();
1701 1.129.10.2 ad V_INCR_NUMOUTPUT(swapdev_vp);
1702 1.129.10.2 ad splx(s);
1703 1.129.10.2 ad }
1704 1.129.10.2 ad
1705 1.129.10.2 ad /*
1706 1.129.10.2 ad * for async ops we must set up the iodone handler.
1707 1.129.10.2 ad */
1708 1.129.10.2 ad
1709 1.129.10.2 ad if (async) {
1710 1.129.10.2 ad bp->b_flags |= B_CALL;
1711 1.129.10.2 ad bp->b_iodone = uvm_aio_biodone;
1712 1.129.10.2 ad UVMHIST_LOG(pdhist, "doing async!", 0, 0, 0, 0);
1713 1.129.10.2 ad if (curlwp == uvm.pagedaemon_lwp)
1714 1.129.10.2 ad BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
1715 1.129.10.2 ad else
1716 1.129.10.2 ad BIO_SETPRIO(bp, BPRIO_TIMELIMITED);
1717 1.129.10.2 ad } else {
1718 1.129.10.2 ad BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
1719 1.129.10.2 ad }
1720 1.129.10.2 ad UVMHIST_LOG(pdhist,
1721 1.129.10.2 ad "about to start io: data = %p blkno = 0x%x, bcount = %ld",
1722 1.129.10.2 ad bp->b_data, bp->b_blkno, bp->b_bcount, 0);
1723 1.129.10.2 ad
1724 1.129.10.2 ad /*
1725 1.129.10.2 ad * now we start the I/O, and if async, return.
1726 1.129.10.2 ad */
1727 1.129.10.2 ad
1728 1.129.10.2 ad VOP_STRATEGY(swapdev_vp, bp);
1729 1.129.10.2 ad if (async)
1730 1.129.10.2 ad return 0;
1731 1.129.10.2 ad
1732 1.129.10.2 ad /*
1733 1.129.10.2 ad * must be sync i/o. wait for it to finish
1734 1.129.10.2 ad */
1735 1.129.10.2 ad
1736 1.129.10.2 ad error = biowait(bp);
1737 1.129.10.2 ad
1738 1.129.10.2 ad /*
1739 1.129.10.2 ad * kill the pager mapping
1740 1.129.10.2 ad */
1741 1.129.10.2 ad
1742 1.129.10.2 ad uvm_pagermapout(kva, npages);
1743 1.129.10.2 ad
1744 1.129.10.2 ad /*
1745 1.129.10.2 ad * now dispose of the buf and we're done.
1746 1.129.10.2 ad */
1747 1.129.10.2 ad
1748 1.129.10.2 ad s = splbio();
1749 1.129.10.2 ad if (write)
1750 1.129.10.2 ad vwakeup(bp);
1751 1.129.10.2 ad putiobuf(bp);
1752 1.129.10.2 ad splx(s);
1753 1.129.10.2 ad UVMHIST_LOG(pdhist, "<- done (sync) error=%d", error, 0, 0, 0);
1754 1.129.10.2 ad return (error);
1755 1.129.10.2 ad }
1756