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