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