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