uvm_swap.c revision 1.148 1 /* $NetBSD: uvm_swap.c,v 1.148 2010/02/02 15:00:34 wiz 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.148 2010/02/02 15:00:34 wiz 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 if ((error =
876 VFS_STATVFS(vp->v_mount, &vp->v_mount->mnt_stat)) != 0)
877 goto bad;
878
879 sdp->swd_bsize = vp->v_mount->mnt_stat.f_iosize;
880 /*
881 * limit the max # of outstanding I/O requests we issue
882 * at any one time. take it easy on NFS servers.
883 */
884 #ifdef NFS
885 if (vp->v_op == nfsv2_vnodeop_p)
886 sdp->swd_maxactive = 2; /* XXX */
887 else
888 #endif /* NFS */
889 sdp->swd_maxactive = 8; /* XXX */
890 break;
891
892 default:
893 error = ENXIO;
894 goto bad;
895 }
896
897 /*
898 * save nblocks in a safe place and convert to pages.
899 */
900
901 sdp->swd_nblks = nblocks;
902 npages = dbtob((uint64_t)nblocks) >> PAGE_SHIFT;
903
904 /*
905 * for block special files, we want to make sure that leave
906 * the disklabel and bootblocks alone, so we arrange to skip
907 * over them (arbitrarily choosing to skip PAGE_SIZE bytes).
908 * note that because of this the "size" can be less than the
909 * actual number of blocks on the device.
910 */
911 if (vp->v_type == VBLK) {
912 /* we use pages 1 to (size - 1) [inclusive] */
913 size = npages - 1;
914 addr = 1;
915 } else {
916 /* we use pages 0 to (size - 1) [inclusive] */
917 size = npages;
918 addr = 0;
919 }
920
921 /*
922 * make sure we have enough blocks for a reasonable sized swap
923 * area. we want at least one page.
924 */
925
926 if (size < 1) {
927 UVMHIST_LOG(pdhist, " size <= 1!!", 0, 0, 0, 0);
928 error = EINVAL;
929 goto bad;
930 }
931
932 UVMHIST_LOG(pdhist, " dev=%x: size=%d addr=%ld\n", dev, size, addr, 0);
933
934 /*
935 * now we need to allocate an extent to manage this swap device
936 */
937
938 sdp->swd_blist = blist_create(npages);
939 /* mark all expect the `saved' region free. */
940 blist_free(sdp->swd_blist, addr, size);
941
942 /*
943 * if the vnode we are swapping to is the root vnode
944 * (i.e. we are swapping to the miniroot) then we want
945 * to make sure we don't overwrite it. do a statfs to
946 * find its size and skip over it.
947 */
948 if (vp == rootvp) {
949 struct mount *mp;
950 struct statvfs *sp;
951 int rootblocks, rootpages;
952
953 mp = rootvnode->v_mount;
954 sp = &mp->mnt_stat;
955 rootblocks = sp->f_blocks * btodb(sp->f_frsize);
956 /*
957 * XXX: sp->f_blocks isn't the total number of
958 * blocks in the filesystem, it's the number of
959 * data blocks. so, our rootblocks almost
960 * definitely underestimates the total size
961 * of the filesystem - how badly depends on the
962 * details of the filesystem type. there isn't
963 * an obvious way to deal with this cleanly
964 * and perfectly, so for now we just pad our
965 * rootblocks estimate with an extra 5 percent.
966 */
967 rootblocks += (rootblocks >> 5) +
968 (rootblocks >> 6) +
969 (rootblocks >> 7);
970 rootpages = round_page(dbtob(rootblocks)) >> PAGE_SHIFT;
971 if (rootpages > size)
972 panic("swap_on: miniroot larger than swap?");
973
974 if (rootpages != blist_fill(sdp->swd_blist, addr, rootpages)) {
975 panic("swap_on: unable to preserve miniroot");
976 }
977
978 size -= rootpages;
979 printf("Preserved %d pages of miniroot ", rootpages);
980 printf("leaving %d pages of swap\n", size);
981 }
982
983 /*
984 * add a ref to vp to reflect usage as a swap device.
985 */
986 vref(vp);
987
988 /*
989 * now add the new swapdev to the drum and enable.
990 */
991 result = vmem_alloc(swapmap, npages, VM_BESTFIT | VM_SLEEP);
992 if (result == 0)
993 panic("swapdrum_add");
994 /*
995 * If this is the first regular swap create the workqueue.
996 * => Protected by swap_syscall_lock.
997 */
998 if (vp->v_type != VBLK) {
999 if (sw_reg_count++ == 0) {
1000 KASSERT(sw_reg_workqueue == NULL);
1001 if (workqueue_create(&sw_reg_workqueue, "swapiod",
1002 sw_reg_iodone, NULL, PRIBIO, IPL_BIO, 0) != 0)
1003 panic("%s: workqueue_create failed", __func__);
1004 }
1005 }
1006
1007 sdp->swd_drumoffset = (int)result;
1008 sdp->swd_drumsize = npages;
1009 sdp->swd_npages = size;
1010 mutex_enter(&uvm_swap_data_lock);
1011 sdp->swd_flags &= ~SWF_FAKE; /* going live */
1012 sdp->swd_flags |= (SWF_INUSE|SWF_ENABLE);
1013 uvmexp.swpages += size;
1014 uvmexp.swpgavail += size;
1015 mutex_exit(&uvm_swap_data_lock);
1016 return (0);
1017
1018 /*
1019 * failure: clean up and return error.
1020 */
1021
1022 bad:
1023 if (sdp->swd_blist) {
1024 blist_destroy(sdp->swd_blist);
1025 }
1026 if (vp != rootvp) {
1027 (void)VOP_CLOSE(vp, FREAD|FWRITE, l->l_cred);
1028 }
1029 return (error);
1030 }
1031
1032 /*
1033 * swap_off: stop swapping on swapdev
1034 *
1035 * => swap data should be locked, we will unlock.
1036 */
1037 static int
1038 swap_off(struct lwp *l, struct swapdev *sdp)
1039 {
1040 int npages = sdp->swd_npages;
1041 int error = 0;
1042
1043 UVMHIST_FUNC("swap_off"); UVMHIST_CALLED(pdhist);
1044 UVMHIST_LOG(pdhist, " dev=%x, npages=%d", sdp->swd_dev,npages,0,0);
1045
1046 /* disable the swap area being removed */
1047 sdp->swd_flags &= ~SWF_ENABLE;
1048 uvmexp.swpgavail -= npages;
1049 mutex_exit(&uvm_swap_data_lock);
1050
1051 /*
1052 * the idea is to find all the pages that are paged out to this
1053 * device, and page them all in. in uvm, swap-backed pageable
1054 * memory can take two forms: aobjs and anons. call the
1055 * swapoff hook for each subsystem to bring in pages.
1056 */
1057
1058 if (uao_swap_off(sdp->swd_drumoffset,
1059 sdp->swd_drumoffset + sdp->swd_drumsize) ||
1060 amap_swap_off(sdp->swd_drumoffset,
1061 sdp->swd_drumoffset + sdp->swd_drumsize)) {
1062 error = ENOMEM;
1063 } else if (sdp->swd_npginuse > sdp->swd_npgbad) {
1064 error = EBUSY;
1065 }
1066
1067 if (error) {
1068 mutex_enter(&uvm_swap_data_lock);
1069 sdp->swd_flags |= SWF_ENABLE;
1070 uvmexp.swpgavail += npages;
1071 mutex_exit(&uvm_swap_data_lock);
1072
1073 return error;
1074 }
1075
1076 /*
1077 * If this is the last regular swap destroy the workqueue.
1078 * => Protected by swap_syscall_lock.
1079 */
1080 if (sdp->swd_vp->v_type != VBLK) {
1081 KASSERT(sw_reg_count > 0);
1082 KASSERT(sw_reg_workqueue != NULL);
1083 if (--sw_reg_count == 0) {
1084 workqueue_destroy(sw_reg_workqueue);
1085 sw_reg_workqueue = NULL;
1086 }
1087 }
1088
1089 /*
1090 * done with the vnode.
1091 * drop our ref on the vnode before calling VOP_CLOSE()
1092 * so that spec_close() can tell if this is the last close.
1093 */
1094 vrele(sdp->swd_vp);
1095 if (sdp->swd_vp != rootvp) {
1096 (void) VOP_CLOSE(sdp->swd_vp, FREAD|FWRITE, l->l_cred);
1097 }
1098
1099 mutex_enter(&uvm_swap_data_lock);
1100 uvmexp.swpages -= npages;
1101 uvmexp.swpginuse -= sdp->swd_npgbad;
1102
1103 if (swaplist_find(sdp->swd_vp, true) == NULL)
1104 panic("%s: swapdev not in list", __func__);
1105 swaplist_trim();
1106 mutex_exit(&uvm_swap_data_lock);
1107
1108 /*
1109 * free all resources!
1110 */
1111 vmem_free(swapmap, sdp->swd_drumoffset, sdp->swd_drumsize);
1112 blist_destroy(sdp->swd_blist);
1113 bufq_free(sdp->swd_tab);
1114 free(sdp, M_VMSWAP);
1115 return (0);
1116 }
1117
1118 /*
1119 * /dev/drum interface and i/o functions
1120 */
1121
1122 /*
1123 * swstrategy: perform I/O on the drum
1124 *
1125 * => we must map the i/o request from the drum to the correct swapdev.
1126 */
1127 static void
1128 swstrategy(struct buf *bp)
1129 {
1130 struct swapdev *sdp;
1131 struct vnode *vp;
1132 int pageno, bn;
1133 UVMHIST_FUNC("swstrategy"); UVMHIST_CALLED(pdhist);
1134
1135 /*
1136 * convert block number to swapdev. note that swapdev can't
1137 * be yanked out from under us because we are holding resources
1138 * in it (i.e. the blocks we are doing I/O on).
1139 */
1140 pageno = dbtob((int64_t)bp->b_blkno) >> PAGE_SHIFT;
1141 mutex_enter(&uvm_swap_data_lock);
1142 sdp = swapdrum_getsdp(pageno);
1143 mutex_exit(&uvm_swap_data_lock);
1144 if (sdp == NULL) {
1145 bp->b_error = EINVAL;
1146 biodone(bp);
1147 UVMHIST_LOG(pdhist, " failed to get swap device", 0, 0, 0, 0);
1148 return;
1149 }
1150
1151 /*
1152 * convert drum page number to block number on this swapdev.
1153 */
1154
1155 pageno -= sdp->swd_drumoffset; /* page # on swapdev */
1156 bn = btodb((uint64_t)pageno << PAGE_SHIFT); /* convert to diskblock */
1157
1158 UVMHIST_LOG(pdhist, " %s: mapoff=%x bn=%x bcount=%ld",
1159 ((bp->b_flags & B_READ) == 0) ? "write" : "read",
1160 sdp->swd_drumoffset, bn, bp->b_bcount);
1161
1162 /*
1163 * for block devices we finish up here.
1164 * for regular files we have to do more work which we delegate
1165 * to sw_reg_strategy().
1166 */
1167
1168 vp = sdp->swd_vp; /* swapdev vnode pointer */
1169 switch (vp->v_type) {
1170 default:
1171 panic("%s: vnode type 0x%x", __func__, vp->v_type);
1172
1173 case VBLK:
1174
1175 /*
1176 * must convert "bp" from an I/O on /dev/drum to an I/O
1177 * on the swapdev (sdp).
1178 */
1179 bp->b_blkno = bn; /* swapdev block number */
1180 bp->b_dev = sdp->swd_dev; /* swapdev dev_t */
1181
1182 /*
1183 * if we are doing a write, we have to redirect the i/o on
1184 * drum's v_numoutput counter to the swapdevs.
1185 */
1186 if ((bp->b_flags & B_READ) == 0) {
1187 mutex_enter(bp->b_objlock);
1188 vwakeup(bp); /* kills one 'v_numoutput' on drum */
1189 mutex_exit(bp->b_objlock);
1190 mutex_enter(&vp->v_interlock);
1191 vp->v_numoutput++; /* put it on swapdev */
1192 mutex_exit(&vp->v_interlock);
1193 }
1194
1195 /*
1196 * finally plug in swapdev vnode and start I/O
1197 */
1198 bp->b_vp = vp;
1199 bp->b_objlock = &vp->v_interlock;
1200 VOP_STRATEGY(vp, bp);
1201 return;
1202
1203 case VREG:
1204 /*
1205 * delegate to sw_reg_strategy function.
1206 */
1207 sw_reg_strategy(sdp, bp, bn);
1208 return;
1209 }
1210 /* NOTREACHED */
1211 }
1212
1213 /*
1214 * swread: the read function for the drum (just a call to physio)
1215 */
1216 /*ARGSUSED*/
1217 static int
1218 swread(dev_t dev, struct uio *uio, int ioflag)
1219 {
1220 UVMHIST_FUNC("swread"); UVMHIST_CALLED(pdhist);
1221
1222 UVMHIST_LOG(pdhist, " dev=%x offset=%qx", dev, uio->uio_offset, 0, 0);
1223 return (physio(swstrategy, NULL, dev, B_READ, minphys, uio));
1224 }
1225
1226 /*
1227 * swwrite: the write function for the drum (just a call to physio)
1228 */
1229 /*ARGSUSED*/
1230 static int
1231 swwrite(dev_t dev, struct uio *uio, int ioflag)
1232 {
1233 UVMHIST_FUNC("swwrite"); UVMHIST_CALLED(pdhist);
1234
1235 UVMHIST_LOG(pdhist, " dev=%x offset=%qx", dev, uio->uio_offset, 0, 0);
1236 return (physio(swstrategy, NULL, dev, B_WRITE, minphys, uio));
1237 }
1238
1239 const struct bdevsw swap_bdevsw = {
1240 nullopen, nullclose, swstrategy, noioctl, nodump, nosize, D_OTHER,
1241 };
1242
1243 const struct cdevsw swap_cdevsw = {
1244 nullopen, nullclose, swread, swwrite, noioctl,
1245 nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
1246 };
1247
1248 /*
1249 * sw_reg_strategy: handle swap i/o to regular files
1250 */
1251 static void
1252 sw_reg_strategy(struct swapdev *sdp, struct buf *bp, int bn)
1253 {
1254 struct vnode *vp;
1255 struct vndxfer *vnx;
1256 daddr_t nbn;
1257 char *addr;
1258 off_t byteoff;
1259 int s, off, nra, error, sz, resid;
1260 UVMHIST_FUNC("sw_reg_strategy"); UVMHIST_CALLED(pdhist);
1261
1262 /*
1263 * allocate a vndxfer head for this transfer and point it to
1264 * our buffer.
1265 */
1266 vnx = pool_get(&vndxfer_pool, PR_WAITOK);
1267 vnx->vx_flags = VX_BUSY;
1268 vnx->vx_error = 0;
1269 vnx->vx_pending = 0;
1270 vnx->vx_bp = bp;
1271 vnx->vx_sdp = sdp;
1272
1273 /*
1274 * setup for main loop where we read filesystem blocks into
1275 * our buffer.
1276 */
1277 error = 0;
1278 bp->b_resid = bp->b_bcount; /* nothing transfered yet! */
1279 addr = bp->b_data; /* current position in buffer */
1280 byteoff = dbtob((uint64_t)bn);
1281
1282 for (resid = bp->b_resid; resid; resid -= sz) {
1283 struct vndbuf *nbp;
1284
1285 /*
1286 * translate byteoffset into block number. return values:
1287 * vp = vnode of underlying device
1288 * nbn = new block number (on underlying vnode dev)
1289 * nra = num blocks we can read-ahead (excludes requested
1290 * block)
1291 */
1292 nra = 0;
1293 error = VOP_BMAP(sdp->swd_vp, byteoff / sdp->swd_bsize,
1294 &vp, &nbn, &nra);
1295
1296 if (error == 0 && nbn == (daddr_t)-1) {
1297 /*
1298 * this used to just set error, but that doesn't
1299 * do the right thing. Instead, it causes random
1300 * memory errors. The panic() should remain until
1301 * this condition doesn't destabilize the system.
1302 */
1303 #if 1
1304 panic("%s: swap to sparse file", __func__);
1305 #else
1306 error = EIO; /* failure */
1307 #endif
1308 }
1309
1310 /*
1311 * punt if there was an error or a hole in the file.
1312 * we must wait for any i/o ops we have already started
1313 * to finish before returning.
1314 *
1315 * XXX we could deal with holes here but it would be
1316 * a hassle (in the write case).
1317 */
1318 if (error) {
1319 s = splbio();
1320 vnx->vx_error = error; /* pass error up */
1321 goto out;
1322 }
1323
1324 /*
1325 * compute the size ("sz") of this transfer (in bytes).
1326 */
1327 off = byteoff % sdp->swd_bsize;
1328 sz = (1 + nra) * sdp->swd_bsize - off;
1329 if (sz > resid)
1330 sz = resid;
1331
1332 UVMHIST_LOG(pdhist, "sw_reg_strategy: "
1333 "vp %p/%p offset 0x%x/0x%x",
1334 sdp->swd_vp, vp, byteoff, nbn);
1335
1336 /*
1337 * now get a buf structure. note that the vb_buf is
1338 * at the front of the nbp structure so that you can
1339 * cast pointers between the two structure easily.
1340 */
1341 nbp = pool_get(&vndbuf_pool, PR_WAITOK);
1342 buf_init(&nbp->vb_buf);
1343 nbp->vb_buf.b_flags = bp->b_flags;
1344 nbp->vb_buf.b_cflags = bp->b_cflags;
1345 nbp->vb_buf.b_oflags = bp->b_oflags;
1346 nbp->vb_buf.b_bcount = sz;
1347 nbp->vb_buf.b_bufsize = sz;
1348 nbp->vb_buf.b_error = 0;
1349 nbp->vb_buf.b_data = addr;
1350 nbp->vb_buf.b_lblkno = 0;
1351 nbp->vb_buf.b_blkno = nbn + btodb(off);
1352 nbp->vb_buf.b_rawblkno = nbp->vb_buf.b_blkno;
1353 nbp->vb_buf.b_iodone = sw_reg_biodone;
1354 nbp->vb_buf.b_vp = vp;
1355 nbp->vb_buf.b_objlock = &vp->v_interlock;
1356 if (vp->v_type == VBLK) {
1357 nbp->vb_buf.b_dev = vp->v_rdev;
1358 }
1359
1360 nbp->vb_xfer = vnx; /* patch it back in to vnx */
1361
1362 /*
1363 * Just sort by block number
1364 */
1365 s = splbio();
1366 if (vnx->vx_error != 0) {
1367 buf_destroy(&nbp->vb_buf);
1368 pool_put(&vndbuf_pool, nbp);
1369 goto out;
1370 }
1371 vnx->vx_pending++;
1372
1373 /* sort it in and start I/O if we are not over our limit */
1374 /* XXXAD locking */
1375 bufq_put(sdp->swd_tab, &nbp->vb_buf);
1376 sw_reg_start(sdp);
1377 splx(s);
1378
1379 /*
1380 * advance to the next I/O
1381 */
1382 byteoff += sz;
1383 addr += sz;
1384 }
1385
1386 s = splbio();
1387
1388 out: /* Arrive here at splbio */
1389 vnx->vx_flags &= ~VX_BUSY;
1390 if (vnx->vx_pending == 0) {
1391 error = vnx->vx_error;
1392 pool_put(&vndxfer_pool, vnx);
1393 bp->b_error = error;
1394 biodone(bp);
1395 }
1396 splx(s);
1397 }
1398
1399 /*
1400 * sw_reg_start: start an I/O request on the requested swapdev
1401 *
1402 * => reqs are sorted by b_rawblkno (above)
1403 */
1404 static void
1405 sw_reg_start(struct swapdev *sdp)
1406 {
1407 struct buf *bp;
1408 struct vnode *vp;
1409 UVMHIST_FUNC("sw_reg_start"); UVMHIST_CALLED(pdhist);
1410
1411 /* recursion control */
1412 if ((sdp->swd_flags & SWF_BUSY) != 0)
1413 return;
1414
1415 sdp->swd_flags |= SWF_BUSY;
1416
1417 while (sdp->swd_active < sdp->swd_maxactive) {
1418 bp = bufq_get(sdp->swd_tab);
1419 if (bp == NULL)
1420 break;
1421 sdp->swd_active++;
1422
1423 UVMHIST_LOG(pdhist,
1424 "sw_reg_start: bp %p vp %p blkno %p cnt %lx",
1425 bp, bp->b_vp, bp->b_blkno, bp->b_bcount);
1426 vp = bp->b_vp;
1427 KASSERT(bp->b_objlock == &vp->v_interlock);
1428 if ((bp->b_flags & B_READ) == 0) {
1429 mutex_enter(&vp->v_interlock);
1430 vp->v_numoutput++;
1431 mutex_exit(&vp->v_interlock);
1432 }
1433 VOP_STRATEGY(vp, bp);
1434 }
1435 sdp->swd_flags &= ~SWF_BUSY;
1436 }
1437
1438 /*
1439 * sw_reg_biodone: one of our i/o's has completed
1440 */
1441 static void
1442 sw_reg_biodone(struct buf *bp)
1443 {
1444 workqueue_enqueue(sw_reg_workqueue, &bp->b_work, NULL);
1445 }
1446
1447 /*
1448 * sw_reg_iodone: one of our i/o's has completed and needs post-i/o cleanup
1449 *
1450 * => note that we can recover the vndbuf struct by casting the buf ptr
1451 */
1452 static void
1453 sw_reg_iodone(struct work *wk, void *dummy)
1454 {
1455 struct vndbuf *vbp = (void *)wk;
1456 struct vndxfer *vnx = vbp->vb_xfer;
1457 struct buf *pbp = vnx->vx_bp; /* parent buffer */
1458 struct swapdev *sdp = vnx->vx_sdp;
1459 int s, resid, error;
1460 KASSERT(&vbp->vb_buf.b_work == wk);
1461 UVMHIST_FUNC("sw_reg_iodone"); UVMHIST_CALLED(pdhist);
1462
1463 UVMHIST_LOG(pdhist, " vbp=%p vp=%p blkno=%x addr=%p",
1464 vbp, vbp->vb_buf.b_vp, vbp->vb_buf.b_blkno, vbp->vb_buf.b_data);
1465 UVMHIST_LOG(pdhist, " cnt=%lx resid=%lx",
1466 vbp->vb_buf.b_bcount, vbp->vb_buf.b_resid, 0, 0);
1467
1468 /*
1469 * protect vbp at splbio and update.
1470 */
1471
1472 s = splbio();
1473 resid = vbp->vb_buf.b_bcount - vbp->vb_buf.b_resid;
1474 pbp->b_resid -= resid;
1475 vnx->vx_pending--;
1476
1477 if (vbp->vb_buf.b_error != 0) {
1478 /* pass error upward */
1479 error = vbp->vb_buf.b_error ? vbp->vb_buf.b_error : EIO;
1480 UVMHIST_LOG(pdhist, " got error=%d !", error, 0, 0, 0);
1481 vnx->vx_error = error;
1482 }
1483
1484 /*
1485 * kill vbp structure
1486 */
1487 buf_destroy(&vbp->vb_buf);
1488 pool_put(&vndbuf_pool, vbp);
1489
1490 /*
1491 * wrap up this transaction if it has run to completion or, in
1492 * case of an error, when all auxiliary buffers have returned.
1493 */
1494 if (vnx->vx_error != 0) {
1495 /* pass error upward */
1496 error = vnx->vx_error;
1497 if ((vnx->vx_flags & VX_BUSY) == 0 && vnx->vx_pending == 0) {
1498 pbp->b_error = error;
1499 biodone(pbp);
1500 pool_put(&vndxfer_pool, vnx);
1501 }
1502 } else if (pbp->b_resid == 0) {
1503 KASSERT(vnx->vx_pending == 0);
1504 if ((vnx->vx_flags & VX_BUSY) == 0) {
1505 UVMHIST_LOG(pdhist, " iodone error=%d !",
1506 pbp, vnx->vx_error, 0, 0);
1507 biodone(pbp);
1508 pool_put(&vndxfer_pool, vnx);
1509 }
1510 }
1511
1512 /*
1513 * done! start next swapdev I/O if one is pending
1514 */
1515 sdp->swd_active--;
1516 sw_reg_start(sdp);
1517 splx(s);
1518 }
1519
1520
1521 /*
1522 * uvm_swap_alloc: allocate space on swap
1523 *
1524 * => allocation is done "round robin" down the priority list, as we
1525 * allocate in a priority we "rotate" the circle queue.
1526 * => space can be freed with uvm_swap_free
1527 * => we return the page slot number in /dev/drum (0 == invalid slot)
1528 * => we lock uvm_swap_data_lock
1529 * => XXXMRG: "LESSOK" INTERFACE NEEDED TO EXTENT SYSTEM
1530 */
1531 int
1532 uvm_swap_alloc(int *nslots /* IN/OUT */, bool lessok)
1533 {
1534 struct swapdev *sdp;
1535 struct swappri *spp;
1536 UVMHIST_FUNC("uvm_swap_alloc"); UVMHIST_CALLED(pdhist);
1537
1538 /*
1539 * no swap devices configured yet? definite failure.
1540 */
1541 if (uvmexp.nswapdev < 1)
1542 return 0;
1543
1544 /*
1545 * lock data lock, convert slots into blocks, and enter loop
1546 */
1547 mutex_enter(&uvm_swap_data_lock);
1548
1549 ReTry: /* XXXMRG */
1550 LIST_FOREACH(spp, &swap_priority, spi_swappri) {
1551 CIRCLEQ_FOREACH(sdp, &spp->spi_swapdev, swd_next) {
1552 uint64_t result;
1553
1554 /* if it's not enabled, then we can't swap from it */
1555 if ((sdp->swd_flags & SWF_ENABLE) == 0)
1556 continue;
1557 if (sdp->swd_npginuse + *nslots > sdp->swd_npages)
1558 continue;
1559 result = blist_alloc(sdp->swd_blist, *nslots);
1560 if (result == BLIST_NONE) {
1561 continue;
1562 }
1563 KASSERT(result < sdp->swd_drumsize);
1564
1565 /*
1566 * successful allocation! now rotate the circleq.
1567 */
1568 CIRCLEQ_REMOVE(&spp->spi_swapdev, sdp, swd_next);
1569 CIRCLEQ_INSERT_TAIL(&spp->spi_swapdev, sdp, swd_next);
1570 sdp->swd_npginuse += *nslots;
1571 uvmexp.swpginuse += *nslots;
1572 mutex_exit(&uvm_swap_data_lock);
1573 /* done! return drum slot number */
1574 UVMHIST_LOG(pdhist,
1575 "success! returning %d slots starting at %d",
1576 *nslots, result + sdp->swd_drumoffset, 0, 0);
1577 return (result + sdp->swd_drumoffset);
1578 }
1579 }
1580
1581 /* XXXMRG: BEGIN HACK */
1582 if (*nslots > 1 && lessok) {
1583 *nslots = 1;
1584 /* XXXMRG: ugh! blist should support this for us */
1585 goto ReTry;
1586 }
1587 /* XXXMRG: END HACK */
1588
1589 mutex_exit(&uvm_swap_data_lock);
1590 return 0;
1591 }
1592
1593 /*
1594 * uvm_swapisfull: return true if most of available swap is allocated
1595 * and in use. we don't count some small portion as it may be inaccessible
1596 * to us at any given moment, for example if there is lock contention or if
1597 * pages are busy.
1598 */
1599 bool
1600 uvm_swapisfull(void)
1601 {
1602 int swpgonly;
1603 bool rv;
1604
1605 mutex_enter(&uvm_swap_data_lock);
1606 KASSERT(uvmexp.swpgonly <= uvmexp.swpages);
1607 swpgonly = (int)((uint64_t)uvmexp.swpgonly * 100 /
1608 uvm_swapisfull_factor);
1609 rv = (swpgonly >= uvmexp.swpgavail);
1610 mutex_exit(&uvm_swap_data_lock);
1611
1612 return (rv);
1613 }
1614
1615 /*
1616 * uvm_swap_markbad: keep track of swap ranges where we've had i/o errors
1617 *
1618 * => we lock uvm_swap_data_lock
1619 */
1620 void
1621 uvm_swap_markbad(int startslot, int nslots)
1622 {
1623 struct swapdev *sdp;
1624 UVMHIST_FUNC("uvm_swap_markbad"); UVMHIST_CALLED(pdhist);
1625
1626 mutex_enter(&uvm_swap_data_lock);
1627 sdp = swapdrum_getsdp(startslot);
1628 KASSERT(sdp != NULL);
1629
1630 /*
1631 * we just keep track of how many pages have been marked bad
1632 * in this device, to make everything add up in swap_off().
1633 * we assume here that the range of slots will all be within
1634 * one swap device.
1635 */
1636
1637 KASSERT(uvmexp.swpgonly >= nslots);
1638 uvmexp.swpgonly -= nslots;
1639 sdp->swd_npgbad += nslots;
1640 UVMHIST_LOG(pdhist, "now %d bad", sdp->swd_npgbad, 0,0,0);
1641 mutex_exit(&uvm_swap_data_lock);
1642 }
1643
1644 /*
1645 * uvm_swap_free: free swap slots
1646 *
1647 * => this can be all or part of an allocation made by uvm_swap_alloc
1648 * => we lock uvm_swap_data_lock
1649 */
1650 void
1651 uvm_swap_free(int startslot, int nslots)
1652 {
1653 struct swapdev *sdp;
1654 UVMHIST_FUNC("uvm_swap_free"); UVMHIST_CALLED(pdhist);
1655
1656 UVMHIST_LOG(pdhist, "freeing %d slots starting at %d", nslots,
1657 startslot, 0, 0);
1658
1659 /*
1660 * ignore attempts to free the "bad" slot.
1661 */
1662
1663 if (startslot == SWSLOT_BAD) {
1664 return;
1665 }
1666
1667 /*
1668 * convert drum slot offset back to sdp, free the blocks
1669 * in the extent, and return. must hold pri lock to do
1670 * lookup and access the extent.
1671 */
1672
1673 mutex_enter(&uvm_swap_data_lock);
1674 sdp = swapdrum_getsdp(startslot);
1675 KASSERT(uvmexp.nswapdev >= 1);
1676 KASSERT(sdp != NULL);
1677 KASSERT(sdp->swd_npginuse >= nslots);
1678 blist_free(sdp->swd_blist, startslot - sdp->swd_drumoffset, nslots);
1679 sdp->swd_npginuse -= nslots;
1680 uvmexp.swpginuse -= nslots;
1681 mutex_exit(&uvm_swap_data_lock);
1682 }
1683
1684 /*
1685 * uvm_swap_put: put any number of pages into a contig place on swap
1686 *
1687 * => can be sync or async
1688 */
1689
1690 int
1691 uvm_swap_put(int swslot, struct vm_page **ppsp, int npages, int flags)
1692 {
1693 int error;
1694
1695 error = uvm_swap_io(ppsp, swslot, npages, B_WRITE |
1696 ((flags & PGO_SYNCIO) ? 0 : B_ASYNC));
1697 return error;
1698 }
1699
1700 /*
1701 * uvm_swap_get: get a single page from swap
1702 *
1703 * => usually a sync op (from fault)
1704 */
1705
1706 int
1707 uvm_swap_get(struct vm_page *page, int swslot, int flags)
1708 {
1709 int error;
1710
1711 uvmexp.nswget++;
1712 KASSERT(flags & PGO_SYNCIO);
1713 if (swslot == SWSLOT_BAD) {
1714 return EIO;
1715 }
1716
1717 error = uvm_swap_io(&page, swslot, 1, B_READ |
1718 ((flags & PGO_SYNCIO) ? 0 : B_ASYNC));
1719 if (error == 0) {
1720
1721 /*
1722 * this page is no longer only in swap.
1723 */
1724
1725 mutex_enter(&uvm_swap_data_lock);
1726 KASSERT(uvmexp.swpgonly > 0);
1727 uvmexp.swpgonly--;
1728 mutex_exit(&uvm_swap_data_lock);
1729 }
1730 return error;
1731 }
1732
1733 /*
1734 * uvm_swap_io: do an i/o operation to swap
1735 */
1736
1737 static int
1738 uvm_swap_io(struct vm_page **pps, int startslot, int npages, int flags)
1739 {
1740 daddr_t startblk;
1741 struct buf *bp;
1742 vaddr_t kva;
1743 int error, mapinflags;
1744 bool write, async;
1745 UVMHIST_FUNC("uvm_swap_io"); UVMHIST_CALLED(pdhist);
1746
1747 UVMHIST_LOG(pdhist, "<- called, startslot=%d, npages=%d, flags=%d",
1748 startslot, npages, flags, 0);
1749
1750 write = (flags & B_READ) == 0;
1751 async = (flags & B_ASYNC) != 0;
1752
1753 /*
1754 * allocate a buf for the i/o.
1755 */
1756
1757 KASSERT(curlwp != uvm.pagedaemon_lwp || (write && async));
1758 bp = getiobuf(swapdev_vp, curlwp != uvm.pagedaemon_lwp);
1759 if (bp == NULL) {
1760 uvm_aio_aiodone_pages(pps, npages, true, ENOMEM);
1761 return ENOMEM;
1762 }
1763
1764 /*
1765 * convert starting drum slot to block number
1766 */
1767
1768 startblk = btodb((uint64_t)startslot << PAGE_SHIFT);
1769
1770 /*
1771 * first, map the pages into the kernel.
1772 */
1773
1774 mapinflags = !write ?
1775 UVMPAGER_MAPIN_WAITOK|UVMPAGER_MAPIN_READ :
1776 UVMPAGER_MAPIN_WAITOK|UVMPAGER_MAPIN_WRITE;
1777 kva = uvm_pagermapin(pps, npages, mapinflags);
1778
1779 /*
1780 * fill in the bp/sbp. we currently route our i/o through
1781 * /dev/drum's vnode [swapdev_vp].
1782 */
1783
1784 bp->b_cflags = BC_BUSY | BC_NOCACHE;
1785 bp->b_flags = (flags & (B_READ|B_ASYNC));
1786 bp->b_proc = &proc0; /* XXX */
1787 bp->b_vnbufs.le_next = NOLIST;
1788 bp->b_data = (void *)kva;
1789 bp->b_blkno = startblk;
1790 bp->b_bufsize = bp->b_bcount = npages << PAGE_SHIFT;
1791
1792 /*
1793 * bump v_numoutput (counter of number of active outputs).
1794 */
1795
1796 if (write) {
1797 mutex_enter(&swapdev_vp->v_interlock);
1798 swapdev_vp->v_numoutput++;
1799 mutex_exit(&swapdev_vp->v_interlock);
1800 }
1801
1802 /*
1803 * for async ops we must set up the iodone handler.
1804 */
1805
1806 if (async) {
1807 bp->b_iodone = uvm_aio_biodone;
1808 UVMHIST_LOG(pdhist, "doing async!", 0, 0, 0, 0);
1809 if (curlwp == uvm.pagedaemon_lwp)
1810 BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
1811 else
1812 BIO_SETPRIO(bp, BPRIO_TIMELIMITED);
1813 } else {
1814 bp->b_iodone = NULL;
1815 BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
1816 }
1817 UVMHIST_LOG(pdhist,
1818 "about to start io: data = %p blkno = 0x%x, bcount = %ld",
1819 bp->b_data, bp->b_blkno, bp->b_bcount, 0);
1820
1821 /*
1822 * now we start the I/O, and if async, return.
1823 */
1824
1825 VOP_STRATEGY(swapdev_vp, bp);
1826 if (async)
1827 return 0;
1828
1829 /*
1830 * must be sync i/o. wait for it to finish
1831 */
1832
1833 error = biowait(bp);
1834
1835 /*
1836 * kill the pager mapping
1837 */
1838
1839 uvm_pagermapout(kva, npages);
1840
1841 /*
1842 * now dispose of the buf and we're done.
1843 */
1844
1845 if (write) {
1846 mutex_enter(&swapdev_vp->v_interlock);
1847 vwakeup(bp);
1848 mutex_exit(&swapdev_vp->v_interlock);
1849 }
1850 putiobuf(bp);
1851 UVMHIST_LOG(pdhist, "<- done (sync) error=%d", error, 0, 0, 0);
1852
1853 return (error);
1854 }
1855