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