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