1 /* 2 * (MPSAFE) 3 * 4 * Copyright (c) 1982, 1986, 1989, 1993 5 * The Regents of the University of California. 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. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)vm_swap.c 8.5 (Berkeley) 2/17/94 36 * $FreeBSD: src/sys/vm/vm_swap.c,v 1.96.2.2 2001/10/14 18:46:47 iedowse Exp $ 37 * $DragonFly: src/sys/vm/vm_swap.c,v 1.36 2007/07/20 17:21:54 dillon Exp $ 38 */ 39 40 #include "opt_swap.h" 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/sysproto.h> 45 #include <sys/buf.h> 46 #include <sys/proc.h> 47 #include <sys/priv.h> 48 #include <sys/nlookup.h> 49 #include <sys/sysctl.h> 50 #include <sys/dmap.h> /* XXX */ 51 #include <sys/vnode.h> 52 #include <sys/fcntl.h> 53 #include <sys/blist.h> 54 #include <sys/kernel.h> 55 #include <sys/lock.h> 56 #include <sys/conf.h> 57 #include <sys/stat.h> 58 59 #include <vm/vm.h> 60 #include <vm/vm_extern.h> 61 #include <vm/swap_pager.h> 62 #include <vm/vm_zone.h> 63 #include <vm/vm_param.h> 64 65 #include <sys/thread2.h> 66 #include <sys/mplock2.h> 67 #include <sys/mutex2.h> 68 69 /* 70 * Indirect driver for multi-controller paging. 71 */ 72 73 #ifndef NSWAPDEV 74 #define NSWAPDEV 4 75 #endif 76 static struct swdevt should_be_malloced[NSWAPDEV]; 77 struct swdevt *swdevt = should_be_malloced; /* exported to pstat/systat */ 78 static swblk_t nswap; /* first block after the interleaved devs */ 79 static struct mtx swap_mtx = MTX_INITIALIZER; 80 int nswdev = NSWAPDEV; /* exported to pstat/systat */ 81 int vm_swap_size; 82 int vm_swap_max; 83 84 static int swapdev_strategy (struct vop_strategy_args *ap); 85 struct vnode *swapdev_vp; 86 87 /* 88 * (struct vnode *a_vp, struct bio *b_bio) 89 * 90 * vn_strategy() for swapdev_vp. Perform swap strategy interleave device 91 * selection. 92 * 93 * No requirements. 94 */ 95 static int 96 swapdev_strategy(struct vop_strategy_args *ap) 97 { 98 struct bio *bio = ap->a_bio; 99 struct bio *nbio; 100 struct buf *bp = bio->bio_buf; 101 int sz, off, seg, index, blkno, nblkno; 102 struct swdevt *sp; 103 struct vnode *vp; 104 105 vp = ap->a_vp; 106 sz = howmany(bp->b_bcount, PAGE_SIZE); 107 blkno = (int)(bio->bio_offset >> PAGE_SHIFT); 108 109 /* 110 * Convert interleaved swap into per-device swap. Note that 111 * the block size is left in PAGE_SIZE'd chunks (for the newswap) 112 * here. 113 */ 114 nbio = push_bio(bio); 115 if (nswdev > 1) { 116 off = blkno % dmmax; 117 if (off + sz > dmmax) { 118 bp->b_error = EINVAL; 119 bp->b_flags |= B_ERROR; 120 biodone(bio); 121 return 0; 122 } 123 seg = blkno / dmmax; 124 index = seg % nswdev; 125 seg /= nswdev; 126 nbio->bio_offset = (off_t)(seg * dmmax + off) << PAGE_SHIFT; 127 } else { 128 index = 0; 129 nbio->bio_offset = bio->bio_offset; 130 } 131 nblkno = (int)(nbio->bio_offset >> PAGE_SHIFT); 132 sp = &swdevt[index]; 133 if (nblkno + sz > sp->sw_nblks) { 134 bp->b_error = EINVAL; 135 bp->b_flags |= B_ERROR; 136 /* I/O was never started on nbio, must biodone(bio) */ 137 biodone(bio); 138 return 0; 139 } 140 if (sp->sw_vp == NULL) { 141 bp->b_error = ENODEV; 142 bp->b_flags |= B_ERROR; 143 /* I/O was never started on nbio, must biodone(bio) */ 144 biodone(bio); 145 return 0; 146 } 147 148 /* 149 * Issue a strategy call on the appropriate swap vnode. Note that 150 * bp->b_vp is not modified. Strategy code is always supposed to 151 * use the passed vp. 152 * 153 * We have to use vn_strategy() here even if we know we have a 154 * device in order to properly break up requests which exceed the 155 * device's DMA limits. 156 */ 157 vn_strategy(sp->sw_vp, nbio); 158 return 0; 159 } 160 161 /* 162 * Create a special vnode op vector for swapdev_vp - we only use 163 * vn_strategy(), everything else returns an error. 164 */ 165 static struct vop_ops swapdev_vnode_vops = { 166 .vop_default = vop_defaultop, 167 .vop_strategy = swapdev_strategy 168 }; 169 static struct vop_ops *swapdev_vnode_vops_p = &swapdev_vnode_vops; 170 171 VNODEOP_SET(swapdev_vnode_vops); 172 173 /* 174 * swapon_args(char *name) 175 * 176 * System call swapon(name) enables swapping on device name, 177 * which must be in the swdevsw. Return EBUSY 178 * if already swapping on this device. 179 * 180 * No requirements. 181 */ 182 int 183 sys_swapon(struct swapon_args *uap) 184 { 185 struct thread *td = curthread; 186 struct vattr attr; 187 struct vnode *vp; 188 struct nlookupdata nd; 189 int error; 190 struct ucred *cred; 191 192 cred = td->td_ucred; 193 194 error = priv_check(td, PRIV_ROOT); 195 if (error) 196 return (error); 197 198 mtx_lock(&swap_mtx); 199 get_mplock(); 200 vp = NULL; 201 error = nlookup_init(&nd, uap->name, UIO_USERSPACE, NLC_FOLLOW); 202 if (error == 0) 203 error = nlookup(&nd); 204 if (error == 0) 205 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp); 206 nlookup_done(&nd); 207 if (error) { 208 rel_mplock(); 209 mtx_unlock(&swap_mtx); 210 return (error); 211 } 212 213 if (vn_isdisk(vp, &error)) { 214 error = swaponvp(td, vp, 0); 215 } else if (vp->v_type == VREG && vp->v_tag == VT_NFS && 216 (error = VOP_GETATTR(vp, &attr)) == 0) { 217 /* 218 * Allow direct swapping to NFS regular files in the same 219 * way that nfs_mountroot() sets up diskless swapping. 220 */ 221 error = swaponvp(td, vp, attr.va_size / DEV_BSIZE); 222 } 223 if (error) 224 vrele(vp); 225 rel_mplock(); 226 mtx_unlock(&swap_mtx); 227 228 return (error); 229 } 230 231 /* 232 * Swfree(index) frees the index'th portion of the swap map. 233 * Each of the nswdev devices provides 1/nswdev'th of the swap 234 * space, which is laid out with blocks of dmmax pages circularly 235 * among the devices. 236 * 237 * The new swap code uses page-sized blocks. The old swap code used 238 * DEV_BSIZE'd chunks. 239 * 240 * XXX locking when multiple swapon's run in parallel 241 */ 242 int 243 swaponvp(struct thread *td, struct vnode *vp, u_quad_t nblks) 244 { 245 swblk_t aligned_nblks; 246 int64_t dpsize; 247 struct ucred *cred; 248 struct swdevt *sp; 249 swblk_t vsbase; 250 swblk_t dvbase; 251 cdev_t dev; 252 int index; 253 int error; 254 long blk; 255 256 cred = td->td_ucred; 257 258 mtx_lock(&swap_mtx); 259 260 if (!swapdev_vp) { 261 error = getspecialvnode(VT_NON, NULL, &swapdev_vnode_vops_p, 262 &swapdev_vp, 0, 0); 263 if (error) 264 panic("Cannot get vnode for swapdev"); 265 swapdev_vp->v_type = VNON; /* Untyped */ 266 vx_unlock(swapdev_vp); 267 } 268 269 for (sp = swdevt, index = 0 ; index < nswdev; index++, sp++) { 270 if (sp->sw_vp == vp) { 271 mtx_unlock(&swap_mtx); 272 return EBUSY; 273 } 274 if (!sp->sw_vp) 275 goto found; 276 277 } 278 mtx_unlock(&swap_mtx); 279 return EINVAL; 280 found: 281 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 282 error = VOP_OPEN(vp, FREAD | FWRITE, cred, NULL); 283 vn_unlock(vp); 284 if (error) { 285 mtx_unlock(&swap_mtx); 286 return (error); 287 } 288 289 /* 290 * v_rdev is not valid until after the VOP_OPEN() call. dev_psize() 291 * must be supported if a character device has been specified. 292 */ 293 if (vp->v_type == VCHR) 294 dev = vp->v_rdev; 295 else 296 dev = NULL; 297 298 if (nblks == 0 && dev != NULL) { 299 dpsize = dev_dpsize(dev); 300 if (dpsize == -1) { 301 VOP_CLOSE(vp, FREAD | FWRITE); 302 mtx_unlock(&swap_mtx); 303 return (ENXIO); 304 } 305 nblks = (u_quad_t)dpsize; 306 } 307 if (nblks == 0) { 308 VOP_CLOSE(vp, FREAD | FWRITE); 309 mtx_unlock(&swap_mtx); 310 return (ENXIO); 311 } 312 313 /* 314 * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks. 315 * First chop nblks off to page-align it, then convert. 316 * 317 * sw->sw_nblks is in page-sized chunks now too. 318 */ 319 nblks &= ~(u_quad_t)(ctodb(1) - 1); 320 nblks = dbtoc(nblks); 321 322 /* 323 * Post-conversion nblks must not be >= BLIST_MAXBLKS, and 324 * we impose a 4-swap-device limit so we have to divide it out 325 * further. Going beyond this will result in overflows in the 326 * blist code. 327 * 328 * Post-conversion nblks must fit within a (swblk_t), which 329 * this test also ensures. 330 */ 331 if (nblks > BLIST_MAXBLKS / nswdev) { 332 kprintf("exceeded maximum of %d blocks per swap unit\n", 333 (int)BLIST_MAXBLKS / nswdev); 334 VOP_CLOSE(vp, FREAD | FWRITE); 335 mtx_unlock(&swap_mtx); 336 return (ENXIO); 337 } 338 339 sp->sw_vp = vp; 340 sp->sw_dev = dev2udev(dev); 341 sp->sw_device = dev; 342 sp->sw_flags |= SW_FREED; 343 sp->sw_nused = 0; 344 345 /* 346 * nblks, nswap, and dmmax are PAGE_SIZE'd parameters now, not 347 * DEV_BSIZE'd. aligned_nblks is used to calculate the 348 * size of the swap bitmap, taking into account the stripe size. 349 */ 350 aligned_nblks = (swblk_t)((nblks + (dmmax - 1)) & ~(u_long)(dmmax - 1)); 351 sp->sw_nblks = aligned_nblks; 352 353 if (aligned_nblks * nswdev > nswap) 354 nswap = aligned_nblks * nswdev; 355 356 if (swapblist == NULL) 357 swapblist = blist_create(nswap); 358 else 359 blist_resize(&swapblist, nswap, 0); 360 361 for (dvbase = dmmax; dvbase < aligned_nblks; dvbase += dmmax) { 362 blk = min(aligned_nblks - dvbase, dmmax); 363 vsbase = index * dmmax + dvbase * nswdev; 364 blist_free(swapblist, vsbase, blk); 365 vm_swap_size += blk; 366 vm_swap_max += blk; 367 } 368 swap_pager_newswap(); 369 370 mtx_unlock(&swap_mtx); 371 return (0); 372 } 373 374 /* 375 * Account for swap space in individual swdevt's. The caller ensures 376 * that the provided range falls into a single swdevt. 377 * 378 * +count space freed 379 * -count space allocated 380 */ 381 void 382 swapacctspace(swblk_t base, swblk_t count) 383 { 384 int index; 385 int seg; 386 387 vm_swap_size += count; 388 seg = base / dmmax; 389 index = seg % nswdev; 390 swdevt[index].sw_nused -= count; 391 } 392 393 /* 394 * Retrieve swap info 395 */ 396 static int 397 sysctl_vm_swap_info(SYSCTL_HANDLER_ARGS) 398 { 399 struct xswdev xs; 400 struct swdevt *sp; 401 int error; 402 int n; 403 404 error = 0; 405 for (n = 0; n < nswdev; ++n) { 406 sp = &swdevt[n]; 407 408 xs.xsw_size = sizeof(xs); 409 xs.xsw_version = XSWDEV_VERSION; 410 xs.xsw_blksize = PAGE_SIZE; 411 xs.xsw_dev = sp->sw_dev; 412 xs.xsw_flags = sp->sw_flags; 413 xs.xsw_nblks = sp->sw_nblks; 414 xs.xsw_used = sp->sw_nused; 415 416 error = SYSCTL_OUT(req, &xs, sizeof(xs)); 417 if (error) 418 break; 419 } 420 return (error); 421 } 422 423 SYSCTL_INT(_vm, OID_AUTO, nswapdev, CTLFLAG_RD, &nswdev, 0, 424 "Number of swap devices"); 425 SYSCTL_NODE(_vm, OID_AUTO, swap_info_array, CTLFLAG_RD, sysctl_vm_swap_info, 426 "Swap statistics by device"); 427