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