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/dmap.h> /* XXX */ 50 #include <sys/vnode.h> 51 #include <sys/fcntl.h> 52 #include <sys/blist.h> 53 #include <sys/kernel.h> 54 #include <sys/lock.h> 55 #include <sys/conf.h> 56 #include <sys/stat.h> 57 58 #include <vm/vm.h> 59 #include <vm/vm_extern.h> 60 #include <vm/swap_pager.h> 61 #include <vm/vm_zone.h> 62 63 #include <sys/thread2.h> 64 #include <sys/mplock2.h> 65 #include <sys/mutex2.h> 66 67 /* 68 * Indirect driver for multi-controller paging. 69 */ 70 71 #ifndef NSWAPDEV 72 #define NSWAPDEV 4 73 #endif 74 static struct swdevt should_be_malloced[NSWAPDEV]; 75 struct swdevt *swdevt = should_be_malloced; /* exported to pstat/systat */ 76 static swblk_t nswap; /* first block after the interleaved devs */ 77 static struct mtx swap_mtx = MTX_INITIALIZER; 78 int nswdev = NSWAPDEV; /* exported to pstat/systat */ 79 int vm_swap_size; 80 int vm_swap_max; 81 82 static int swapdev_strategy (struct vop_strategy_args *ap); 83 struct vnode *swapdev_vp; 84 85 /* 86 * (struct vnode *a_vp, struct bio *b_bio) 87 * 88 * vn_strategy() for swapdev_vp. Perform swap strategy interleave device 89 * selection. 90 * 91 * No requirements. 92 */ 93 static int 94 swapdev_strategy(struct vop_strategy_args *ap) 95 { 96 struct bio *bio = ap->a_bio; 97 struct bio *nbio; 98 struct buf *bp = bio->bio_buf; 99 int sz, off, seg, index, blkno, nblkno; 100 struct swdevt *sp; 101 struct vnode *vp; 102 103 vp = ap->a_vp; 104 sz = howmany(bp->b_bcount, PAGE_SIZE); 105 blkno = (int)(bio->bio_offset >> PAGE_SHIFT); 106 107 /* 108 * Convert interleaved swap into per-device swap. Note that 109 * the block size is left in PAGE_SIZE'd chunks (for the newswap) 110 * here. 111 */ 112 nbio = push_bio(bio); 113 if (nswdev > 1) { 114 off = blkno % dmmax; 115 if (off + sz > dmmax) { 116 bp->b_error = EINVAL; 117 bp->b_flags |= B_ERROR; 118 biodone(bio); 119 return 0; 120 } 121 seg = blkno / dmmax; 122 index = seg % nswdev; 123 seg /= nswdev; 124 nbio->bio_offset = (off_t)(seg * dmmax + off) << PAGE_SHIFT; 125 } else { 126 index = 0; 127 nbio->bio_offset = bio->bio_offset; 128 } 129 nblkno = (int)(nbio->bio_offset >> PAGE_SHIFT); 130 sp = &swdevt[index]; 131 if (nblkno + sz > sp->sw_nblks) { 132 bp->b_error = EINVAL; 133 bp->b_flags |= B_ERROR; 134 /* I/O was never started on nbio, must biodone(bio) */ 135 biodone(bio); 136 return 0; 137 } 138 if (sp->sw_vp == NULL) { 139 bp->b_error = ENODEV; 140 bp->b_flags |= B_ERROR; 141 /* I/O was never started on nbio, must biodone(bio) */ 142 biodone(bio); 143 return 0; 144 } 145 146 /* 147 * Issue a strategy call on the appropriate swap vnode. Note that 148 * bp->b_vp is not modified. Strategy code is always supposed to 149 * use the passed vp. 150 * 151 * We have to use vn_strategy() here even if we know we have a 152 * device in order to properly break up requests which exceed the 153 * device's DMA limits. 154 */ 155 vn_strategy(sp->sw_vp, nbio); 156 return 0; 157 } 158 159 /* 160 * Create a special vnode op vector for swapdev_vp - we only use 161 * vn_strategy(), everything else returns an error. 162 */ 163 static struct vop_ops swapdev_vnode_vops = { 164 .vop_default = vop_defaultop, 165 .vop_strategy = swapdev_strategy 166 }; 167 static struct vop_ops *swapdev_vnode_vops_p = &swapdev_vnode_vops; 168 169 VNODEOP_SET(swapdev_vnode_vops); 170 171 /* 172 * swapon_args(char *name) 173 * 174 * System call swapon(name) enables swapping on device name, 175 * which must be in the swdevsw. Return EBUSY 176 * if already swapping on this device. 177 * 178 * No requirements. 179 */ 180 int 181 sys_swapon(struct swapon_args *uap) 182 { 183 struct thread *td = curthread; 184 struct vattr attr; 185 struct vnode *vp; 186 struct nlookupdata nd; 187 int error; 188 struct ucred *cred; 189 190 cred = td->td_ucred; 191 192 error = priv_check(td, PRIV_ROOT); 193 if (error) 194 return (error); 195 196 mtx_lock(&swap_mtx); 197 get_mplock(); 198 vp = NULL; 199 error = nlookup_init(&nd, uap->name, UIO_USERSPACE, NLC_FOLLOW); 200 if (error == 0) 201 error = nlookup(&nd); 202 if (error == 0) 203 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp); 204 nlookup_done(&nd); 205 if (error) { 206 rel_mplock(); 207 mtx_unlock(&swap_mtx); 208 return (error); 209 } 210 211 if (vn_isdisk(vp, &error)) { 212 error = swaponvp(td, vp, 0); 213 } else if (vp->v_type == VREG && vp->v_tag == VT_NFS && 214 (error = VOP_GETATTR(vp, &attr)) == 0) { 215 /* 216 * Allow direct swapping to NFS regular files in the same 217 * way that nfs_mountroot() sets up diskless swapping. 218 */ 219 error = swaponvp(td, vp, attr.va_size / DEV_BSIZE); 220 } 221 if (error) 222 vrele(vp); 223 rel_mplock(); 224 mtx_unlock(&swap_mtx); 225 226 return (error); 227 } 228 229 /* 230 * Swfree(index) frees the index'th portion of the swap map. 231 * Each of the nswdev devices provides 1/nswdev'th of the swap 232 * space, which is laid out with blocks of dmmax pages circularly 233 * among the devices. 234 * 235 * The new swap code uses page-sized blocks. The old swap code used 236 * DEV_BSIZE'd chunks. 237 * 238 * XXX locking when multiple swapon's run in parallel 239 */ 240 int 241 swaponvp(struct thread *td, struct vnode *vp, u_quad_t nblks) 242 { 243 swblk_t aligned_nblks; 244 int64_t dpsize; 245 struct ucred *cred; 246 struct swdevt *sp; 247 swblk_t vsbase; 248 swblk_t dvbase; 249 cdev_t dev; 250 int index; 251 int error; 252 long blk; 253 254 cred = td->td_ucred; 255 256 mtx_lock(&swap_mtx); 257 258 if (!swapdev_vp) { 259 error = getspecialvnode(VT_NON, NULL, &swapdev_vnode_vops_p, 260 &swapdev_vp, 0, 0); 261 if (error) 262 panic("Cannot get vnode for swapdev"); 263 swapdev_vp->v_type = VNON; /* Untyped */ 264 vx_unlock(swapdev_vp); 265 } 266 267 for (sp = swdevt, index = 0 ; index < nswdev; index++, sp++) { 268 if (sp->sw_vp == vp) { 269 mtx_unlock(&swap_mtx); 270 return EBUSY; 271 } 272 if (!sp->sw_vp) 273 goto found; 274 275 } 276 mtx_unlock(&swap_mtx); 277 return EINVAL; 278 found: 279 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 280 error = VOP_OPEN(vp, FREAD | FWRITE, cred, NULL); 281 vn_unlock(vp); 282 if (error) { 283 mtx_unlock(&swap_mtx); 284 return (error); 285 } 286 287 /* 288 * v_rdev is not valid until after the VOP_OPEN() call. dev_psize() 289 * must be supported if a character device has been specified. 290 */ 291 if (vp->v_type == VCHR) 292 dev = vp->v_rdev; 293 else 294 dev = NULL; 295 296 if (nblks == 0 && dev != NULL) { 297 dpsize = dev_dpsize(dev); 298 if (dpsize == -1) { 299 VOP_CLOSE(vp, FREAD | FWRITE); 300 mtx_unlock(&swap_mtx); 301 return (ENXIO); 302 } 303 nblks = (u_quad_t)dpsize; 304 } 305 if (nblks == 0) { 306 VOP_CLOSE(vp, FREAD | FWRITE); 307 mtx_unlock(&swap_mtx); 308 return (ENXIO); 309 } 310 311 /* 312 * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks. 313 * First chop nblks off to page-align it, then convert. 314 * 315 * sw->sw_nblks is in page-sized chunks now too. 316 */ 317 nblks &= ~(u_quad_t)(ctodb(1) - 1); 318 nblks = dbtoc(nblks); 319 320 /* 321 * Post-conversion nblks must not be >= BLIST_MAXBLKS, and 322 * we impose a 4-swap-device limit so we have to divide it out 323 * further. Going beyond this will result in overflows in the 324 * blist code. 325 * 326 * Post-conversion nblks must fit within a (swblk_t), which 327 * this test also ensures. 328 */ 329 if (nblks > BLIST_MAXBLKS / nswdev) { 330 kprintf("exceeded maximum of %d blocks per swap unit\n", 331 (int)BLIST_MAXBLKS / nswdev); 332 VOP_CLOSE(vp, FREAD | FWRITE); 333 mtx_unlock(&swap_mtx); 334 return (ENXIO); 335 } 336 337 sp->sw_vp = vp; 338 sp->sw_dev = dev2udev(dev); 339 sp->sw_device = dev; 340 sp->sw_flags |= SW_FREED; 341 sp->sw_nblks = (swblk_t)nblks; 342 343 /* 344 * nblks, nswap, and dmmax are PAGE_SIZE'd parameters now, not 345 * DEV_BSIZE'd. aligned_nblks is used to calculate the 346 * size of the swap bitmap, taking into account the stripe size. 347 */ 348 aligned_nblks = (swblk_t)((nblks + (dmmax - 1)) & ~(u_long)(dmmax - 1)); 349 350 if (aligned_nblks * nswdev > nswap) 351 nswap = aligned_nblks * nswdev; 352 353 if (swapblist == NULL) 354 swapblist = blist_create(nswap); 355 else 356 blist_resize(&swapblist, nswap, 0); 357 358 for (dvbase = dmmax; dvbase < nblks; dvbase += dmmax) { 359 blk = min(nblks - dvbase, dmmax); 360 vsbase = index * dmmax + dvbase * nswdev; 361 blist_free(swapblist, vsbase, blk); 362 vm_swap_size += blk; 363 vm_swap_max += blk; 364 } 365 swap_pager_newswap(); 366 367 mtx_unlock(&swap_mtx); 368 return (0); 369 } 370