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