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