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