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