]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/vm/vm_swap.c
This commit was generated by cvs2svn to compensate for changes in r74713,
[FreeBSD/FreeBSD.git] / sys / vm / vm_swap.c
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      @(#)vm_swap.c   8.5 (Berkeley) 2/17/94
34  * $FreeBSD$
35  */
36
37 #include "opt_swap.h"
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/sysproto.h>
42 #include <sys/bio.h>
43 #include <sys/buf.h>
44 #include <sys/proc.h>
45 #include <sys/namei.h>
46 #include <sys/dmap.h>           /* XXX */
47 #include <sys/vnode.h>
48 #include <sys/fcntl.h>
49 #include <sys/blist.h>
50 #include <sys/kernel.h>
51 #include <sys/lock.h>
52 #include <sys/conf.h>
53 #include <sys/stat.h>
54 #include <sys/sysctl.h>
55 #include <vm/vm.h>
56 #include <vm/vm_extern.h>
57 #include <vm/vm_zone.h>
58 #include <vm/swap_pager.h>
59
60 /*
61  * Indirect driver for multi-controller paging.
62  */
63
64 #ifndef NSWAPDEV
65 #define NSWAPDEV        4
66 #endif
67 static struct swdevt should_be_malloced[NSWAPDEV];
68 struct swdevt *swdevt = should_be_malloced;
69 static int nswap;               /* first block after the interleaved devs */
70 int nswdev = NSWAPDEV;
71 int vm_swap_size;
72 static int ncswdev = 0;         /* # of configured swap devs */
73 SYSCTL_INT(_vm, OID_AUTO, nswapdev, CTLFLAG_RD, 
74    &ncswdev, 0, "Number of swap devices");
75
76 static int swapdev_strategy __P((struct vop_strategy_args *ap));
77 struct vnode *swapdev_vp;
78
79 /*
80  *      swapdev_strategy:
81  *
82  *      VOP_STRATEGY() for swapdev_vp.
83  *      Perform swap strategy interleave device selection.
84  *
85  *      The bp is expected to be locked and *not* B_DONE on call.
86  */
87
88 static int
89 swapdev_strategy(ap)
90         struct vop_strategy_args /* {
91                 struct vnode *a_vp;
92                 struct buf *a_bp;
93         } */ *ap;
94 {
95         int s, sz, off, seg, index;
96         register struct swdevt *sp;
97         struct vnode *vp;
98         struct buf *bp;
99
100         bp = ap->a_bp;
101         sz = howmany(bp->b_bcount, PAGE_SIZE);
102
103         /*
104          * Convert interleaved swap into per-device swap.  Note that
105          * the block size is left in PAGE_SIZE'd chunks (for the newswap)
106          * here.
107          */
108         if (nswdev > 1) {
109                 off = bp->b_blkno % dmmax;
110                 if (off + sz > dmmax) {
111                         bp->b_error = EINVAL;
112                         bp->b_ioflags |= BIO_ERROR;
113                         bufdone(bp);
114                         return 0;
115                 }
116                 seg = bp->b_blkno / dmmax;
117                 index = seg % nswdev;
118                 seg /= nswdev;
119                 bp->b_blkno = seg * dmmax + off;
120         } else {
121                 index = 0;
122         }
123         sp = &swdevt[index];
124         if (bp->b_blkno + sz > sp->sw_nblks) {
125                 bp->b_error = EINVAL;
126                 bp->b_ioflags |= BIO_ERROR;
127                 bufdone(bp);
128                 return 0;
129         }
130         bp->b_dev = sp->sw_device;
131         if (sp->sw_vp == NULL) {
132                 bp->b_error = ENODEV;
133                 bp->b_ioflags |= BIO_ERROR;
134                 bufdone(bp);
135                 return 0;
136         }
137
138         /*
139          * Convert from PAGE_SIZE'd to DEV_BSIZE'd chunks for the actual I/O
140          */
141         bp->b_blkno = ctodb(bp->b_blkno);
142
143         vhold(sp->sw_vp);
144         s = splvm();
145         if (bp->b_iocmd == BIO_WRITE) {
146                 vp = bp->b_vp;
147                 if (vp) {
148                         vp->v_numoutput--;
149                         if ((vp->v_flag & VBWAIT) && vp->v_numoutput <= 0) {
150                                 vp->v_flag &= ~VBWAIT;
151                                 wakeup(&vp->v_numoutput);
152                         }
153                 }
154                 sp->sw_vp->v_numoutput++;
155         }
156         pbreassignbuf(bp, sp->sw_vp);
157         splx(s);
158         BUF_STRATEGY(bp);
159         return 0;
160 }
161
162 /*
163  * Create a special vnode op vector for swapdev_vp - we only use
164  * VOP_STRATEGY(), everything else returns an error.
165  */
166 vop_t **swapdev_vnodeop_p;
167 static struct vnodeopv_entry_desc swapdev_vnodeop_entries[] = {  
168         { &vop_default_desc,            (vop_t *) vop_defaultop },
169         { &vop_strategy_desc,           (vop_t *) swapdev_strategy },
170         { NULL, NULL }
171 };
172 static struct vnodeopv_desc swapdev_vnodeop_opv_desc =
173         { &swapdev_vnodeop_p, swapdev_vnodeop_entries };
174
175 VNODEOP_SET(swapdev_vnodeop_opv_desc);
176
177 /*
178  * System call swapon(name) enables swapping on device name,
179  * which must be in the swdevsw.  Return EBUSY
180  * if already swapping on this device.
181  */
182 #ifndef _SYS_SYSPROTO_H_
183 struct swapon_args {
184         char *name;
185 };
186 #endif
187
188 /* ARGSUSED */
189 int
190 swapon(p, uap)
191         struct proc *p;
192         struct swapon_args *uap;
193 {
194         register struct vnode *vp;
195         struct nameidata nd;
196         int error;
197
198         error = suser(p);
199         if (error)
200                 return (error);
201
202         /*
203          * Swap metadata may not fit in the KVM if we have physical
204          * memory of >1GB.
205          */
206         if (swap_zone == NULL)
207                 return (ENOMEM);
208
209         NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->name, p);
210         error = namei(&nd);
211         if (error)
212                 return (error);
213
214         NDFREE(&nd, NDF_ONLY_PNBUF);
215         vp = nd.ni_vp;
216
217         vn_isdisk(vp, &error);
218
219         if (!error)
220                 error = swaponvp(p, vp, vp->v_rdev, 0);
221
222         if (error)
223                 vrele(vp);
224
225         return (error);
226 }
227
228 /*
229  * Swfree(index) frees the index'th portion of the swap map.
230  * Each of the nswdev devices provides 1/nswdev'th of the swap
231  * space, which is laid out with blocks of dmmax pages circularly
232  * among the devices.
233  *
234  * The new swap code uses page-sized blocks.  The old swap code used
235  * DEV_BSIZE'd chunks.
236  *
237  * XXX locking when multiple swapon's run in parallel
238  */
239 int
240 swaponvp(p, vp, dev, nblks)
241         struct proc *p;
242         struct vnode *vp;
243         dev_t dev;
244         u_long nblks;
245 {
246         int index;
247         register struct swdevt *sp;
248         register swblk_t vsbase;
249         register long blk;
250         swblk_t dvbase;
251         int error;
252         u_long aligned_nblks;
253         struct sysctl_oid *oid;
254         char name[11];
255
256         if (!swapdev_vp) {
257                 error = getnewvnode(VT_NON, NULL, swapdev_vnodeop_p,
258                     &swapdev_vp);
259                 if (error)
260                         panic("Cannot get vnode for swapdev");
261                 swapdev_vp->v_type = VNON;      /* Untyped */
262         }
263
264         ASSERT_VOP_UNLOCKED(vp, "swaponvp");
265         for (sp = swdevt, index = 0 ; index < nswdev; index++, sp++) {
266                 if (sp->sw_vp == vp)
267                         return EBUSY;
268                 if (!sp->sw_vp)
269                         goto found;
270
271         }
272         return EINVAL;
273     found:
274         (void) vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
275         error = VOP_OPEN(vp, FREAD | FWRITE, p->p_ucred, p);
276         (void) VOP_UNLOCK(vp, 0, p);
277         if (error)
278                 return (error);
279
280         if (nblks == 0 && dev != NODEV && (devsw(dev)->d_psize == 0 ||
281             (nblks = (*devsw(dev)->d_psize) (dev)) == -1)) {
282                 (void) VOP_CLOSE(vp, FREAD | FWRITE, p->p_ucred, p);
283                 return (ENXIO);
284         }
285         if (nblks == 0) {
286                 (void) VOP_CLOSE(vp, FREAD | FWRITE, p->p_ucred, p);
287                 return (ENXIO);
288         }
289
290         /*
291          * If we go beyond this, we get overflows in the radix
292          * tree bitmap code.
293          */
294         if (nblks > 0x40000000 / BLIST_META_RADIX / nswdev) {
295                 printf("exceeded maximum of %d blocks per swap unit\n",
296                         0x40000000 / BLIST_META_RADIX / nswdev);
297                 (void) VOP_CLOSE(vp, FREAD | FWRITE, p->p_ucred, p);
298                 return (ENXIO);
299         }
300         /*
301          * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks.
302          * First chop nblks off to page-align it, then convert.
303          * 
304          * sw->sw_nblks is in page-sized chunks now too.
305          */
306         nblks &= ~(ctodb(1) - 1);
307         nblks = dbtoc(nblks);
308
309         sp->sw_vp = vp;
310         sp->sw_dev = dev2udev(dev);
311         sp->sw_device = dev;
312         sp->sw_flags |= SW_FREED;
313         sp->sw_nblks = nblks;
314         sp->sw_used = 0;
315
316         /*
317          * nblks, nswap, and dmmax are PAGE_SIZE'd parameters now, not
318          * DEV_BSIZE'd.   aligned_nblks is used to calculate the
319          * size of the swap bitmap, taking into account the stripe size.
320          */
321         aligned_nblks = (nblks + (dmmax - 1)) & ~(u_long)(dmmax - 1);
322
323         if (aligned_nblks * nswdev > nswap)
324                 nswap = aligned_nblks * nswdev;
325
326         if (swapblist == NULL)
327                 swapblist = blist_create(nswap);
328         else
329                 blist_resize(&swapblist, nswap, 0);
330
331         for (dvbase = dmmax; dvbase < nblks; dvbase += dmmax) {
332                 blk = min(nblks - dvbase, dmmax);
333                 vsbase = index * dmmax + dvbase * nswdev;
334                 blist_free(swapblist, vsbase, blk);
335                 vm_swap_size += blk;
336         }
337         /* 
338          * Add sysctl entries for new swap area. XXX: if some day swap devices 
339          * can be removed, add an oid member to swdevt.
340          */
341         if (snprintf(name, sizeof(name), "swapdev%d", ncswdev) < sizeof(name)) {
342                 oid = SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_vm), 
343                     OID_AUTO, name, CTLFLAG_RD, NULL, "Swap device stats");
344                 if (oid != NULL) {
345                         SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 
346                             "flags", CTLFLAG_RD, &sp->sw_flags, 0, "Flags");
347                         SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 
348                             "nblks", CTLFLAG_RD, &sp->sw_nblks, 0, 
349                             "Number of blocks");
350                         SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 
351                             "used", CTLFLAG_RD, &sp->sw_used, 0, 
352                             "Number of blocks in use");
353                         SYSCTL_ADD_OPAQUE(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 
354                             "dev", CTLFLAG_RD, &sp->sw_dev, sizeof(sp->sw_dev), 
355                             "T,dev_t", "Device");
356                 }
357         } else
358                 printf("XXX: swaponvp() name buffer too small!\n");
359         ncswdev++;
360
361         return (0);
362 }