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