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