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