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