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