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