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