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