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