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