xref: /titanic_51/usr/src/uts/common/vm/vm_swap.c (revision 9d2159663a6316391e58ae8fc8a1e1a63dc9789c)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 1987, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
26 /*	  All Rights Reserved  	*/
27 
28 /*
29  * University Copyright- Copyright (c) 1982, 1986, 1988
30  * The Regents of the University of California
31  * All Rights Reserved
32  *
33  * University Acknowledgment- Portions of this document are derived from
34  * software developed by the University of California, Berkeley, and its
35  * contributors.
36  */
37 
38 /*
39  * Each physical swap area has an associated bitmap representing
40  * its physical storage. The bitmap records which swap slots are
41  * currently allocated or freed.  Allocation is done by searching
42  * through the bitmap for the first free slot. Thus, there's
43  * no linear relation between offset within the swap device and the
44  * address (within its segment(s)) of the page that the slot backs;
45  * instead, it's an arbitrary one-to-one mapping.
46  *
47  * Associated with each swap area is a swapinfo structure.  These
48  * structures are linked into a linear list that determines the
49  * ordering of swap areas in the logical swap device.  Each contains a
50  * pointer to the corresponding bitmap, the area's size, and its
51  * associated vnode.
52  */
53 
54 #include <sys/types.h>
55 #include <sys/inttypes.h>
56 #include <sys/param.h>
57 #include <sys/t_lock.h>
58 #include <sys/sysmacros.h>
59 #include <sys/systm.h>
60 #include <sys/errno.h>
61 #include <sys/kmem.h>
62 #include <sys/vfs.h>
63 #include <sys/vnode.h>
64 #include <sys/pathname.h>
65 #include <sys/cmn_err.h>
66 #include <sys/vtrace.h>
67 #include <sys/swap.h>
68 #include <sys/dumphdr.h>
69 #include <sys/debug.h>
70 #include <sys/fs/snode.h>
71 #include <sys/fs/swapnode.h>
72 #include <sys/policy.h>
73 #include <sys/zone.h>
74 
75 #include <vm/as.h>
76 #include <vm/seg.h>
77 #include <vm/page.h>
78 #include <vm/seg_vn.h>
79 #include <vm/hat.h>
80 #include <vm/anon.h>
81 #include <vm/seg_map.h>
82 
83 /*
84  * To balance the load among multiple swap areas, we don't allow
85  * more than swap_maxcontig allocations to be satisfied from a
86  * single swap area before moving on to the next swap area.  This
87  * effectively "interleaves" allocations among the many swap areas.
88  */
89 int swap_maxcontig;	/* set by anon_init() to 1 Mb */
90 
91 #define	MINIROOTSIZE	12000	/* ~6 Meg XXX */
92 
93 /*
94  * XXX - this lock is a kludge. It serializes some aspects of swapadd() and
95  * swapdel() (namely VOP_OPEN, VOP_CLOSE, VN_RELE).  It protects against
96  * somebody swapadd'ing and getting swap slots from a vnode, while someone
97  * else is in the process of closing or rele'ing it.
98  */
99 static kmutex_t swap_lock;
100 
101 kmutex_t swapinfo_lock;
102 
103 /*
104  * protected by the swapinfo_lock
105  */
106 struct swapinfo	*swapinfo;
107 
108 static	struct	swapinfo *silast;
109 static	int	nswapfiles;
110 
111 static u_offset_t	swap_getoff(struct swapinfo *);
112 static int	swapadd(struct vnode *, ulong_t, ulong_t, char *);
113 static int	swapdel(struct vnode *, ulong_t);
114 static int	swapslot_free(struct vnode *, u_offset_t, struct swapinfo *);
115 
116 /*
117  * swap device bitmap allocation macros
118  */
119 #define	MAPSHIFT	5
120 #define	NBBW		(NBPW * NBBY)	/* number of bits per word */
121 #define	TESTBIT(map, i)		(((map)[(i) >> MAPSHIFT] & (1 << (i) % NBBW)))
122 #define	SETBIT(map, i)		(((map)[(i) >> MAPSHIFT] |= (1 << (i) % NBBW)))
123 #define	CLEARBIT(map, i)	(((map)[(i) >> MAPSHIFT] &= ~(1 << (i) % NBBW)))
124 
125 int swap_debug = 0;	/* set for debug printf's */
126 int swap_verify = 0;	/* set to verify slots when freeing and allocating */
127 
128 uint_t swapalloc_maxcontig;
129 
130 /*
131  * Allocate a range of up to *lenp contiguous slots (page) from a physical
132  * swap device. Flags are one of:
133  *	SA_NOT  Must have a slot from a physical swap device other than the
134  * 		the one containing input (*vpp, *offp).
135  * Less slots than requested may be returned. *lenp allocated slots are
136  * returned starting at *offp on *vpp.
137  * Returns 1 for a successful allocation, 0 for couldn't allocate any slots.
138  */
139 int
140 swap_phys_alloc(
141 	struct vnode **vpp,
142 	u_offset_t *offp,
143 	size_t *lenp,
144 	uint_t flags)
145 {
146 	struct swapinfo *sip;
147 	offset_t soff, noff;
148 	size_t len;
149 
150 	mutex_enter(&swapinfo_lock);
151 	sip = silast;
152 
153 	/* Find a desirable physical device and allocate from it. */
154 	do {
155 		if (sip == NULL)
156 			break;
157 		if (!(sip->si_flags & ST_INDEL) &&
158 		    (spgcnt_t)sip->si_nfpgs > 0) {
159 			/* Caller wants other than specified swap device */
160 			if (flags & SA_NOT) {
161 				if (*vpp != sip->si_vp ||
162 				    *offp < sip->si_soff ||
163 				    *offp >= sip->si_eoff)
164 					goto found;
165 			/* Caller is loose, will take anything */
166 			} else
167 				goto found;
168 		} else if (sip->si_nfpgs == 0)
169 			sip->si_allocs = 0;
170 		if ((sip = sip->si_next) == NULL)
171 			sip = swapinfo;
172 	} while (sip != silast);
173 	mutex_exit(&swapinfo_lock);
174 	return (0);
175 found:
176 	soff = swap_getoff(sip);
177 	sip->si_nfpgs--;
178 	if (soff == -1)
179 		panic("swap_alloc: swap_getoff failed!");
180 
181 	for (len = PAGESIZE; len < *lenp; len += PAGESIZE) {
182 		if (sip->si_nfpgs == 0)
183 			break;
184 		if (swapalloc_maxcontig && len >= swapalloc_maxcontig)
185 			break;
186 		noff = swap_getoff(sip);
187 		if (noff == -1) {
188 			break;
189 		} else if (noff != soff + len) {
190 			CLEARBIT(sip->si_swapslots, btop(noff - sip->si_soff));
191 			break;
192 		}
193 		sip->si_nfpgs--;
194 	}
195 	*vpp = sip->si_vp;
196 	*offp = soff;
197 	*lenp = len;
198 	ASSERT((spgcnt_t)sip->si_nfpgs >= 0);
199 	sip->si_allocs += btop(len);
200 	if (sip->si_allocs >= swap_maxcontig) {
201 		sip->si_allocs = 0;
202 		if ((silast = sip->si_next) == NULL)
203 			silast = swapinfo;
204 	}
205 	TRACE_2(TR_FAC_VM, TR_SWAP_ALLOC,
206 	    "swap_alloc:sip %p offset %lx", sip, soff);
207 	mutex_exit(&swapinfo_lock);
208 	return (1);
209 }
210 
211 int swap_backsearch = 0;
212 
213 /*
214  * Get a free offset on swap device sip.
215  * Return >=0 offset if succeeded, -1 for failure.
216  */
217 static u_offset_t
218 swap_getoff(struct swapinfo *sip)
219 {
220 	uint_t *sp, *ep;
221 	size_t aoff, boff, poff, slotnumber;
222 
223 	ASSERT(MUTEX_HELD(&swapinfo_lock));
224 
225 	sip->si_alloccnt++;
226 	for (sp = &sip->si_swapslots[sip->si_hint >> MAPSHIFT],
227 	    ep = &sip->si_swapslots[sip->si_mapsize / NBPW]; sp < ep; sp++) {
228 		if (*sp != (uint_t)0xffffffff)
229 			goto foundentry;
230 		else
231 			sip->si_checkcnt++;
232 	}
233 	SWAP_PRINT(SW_ALLOC,
234 	    "swap_getoff: couldn't find slot from hint %ld to end\n",
235 	    sip->si_hint, 0, 0, 0, 0);
236 	/*
237 	 * Go backwards? Check for faster method XXX
238 	 */
239 	if (swap_backsearch) {
240 		for (sp = &sip->si_swapslots[sip->si_hint >> MAPSHIFT],
241 		    ep = sip->si_swapslots; sp > ep; sp--) {
242 			if (*sp != (uint_t)0xffffffff)
243 				goto foundentry;
244 			else
245 				sip->si_checkcnt++;
246 		}
247 	} else {
248 		for (sp = sip->si_swapslots,
249 		    ep = &sip->si_swapslots[sip->si_hint >> MAPSHIFT];
250 		    sp < ep; sp++) {
251 			if (*sp != (uint_t)0xffffffff)
252 				goto foundentry;
253 			else
254 				sip->si_checkcnt++;
255 		}
256 	}
257 	if (*sp == 0xffffffff) {
258 		cmn_err(CE_WARN, "No free swap slots!");
259 		return ((u_offset_t)-1);
260 	}
261 
262 foundentry:
263 	/*
264 	 * aoff is the page number offset (in bytes) of the si_swapslots
265 	 * array element containing a free page
266 	 *
267 	 * boff is the page number offset of the free page
268 	 * (i.e. cleared bit) in si_swapslots[aoff].
269 	 */
270 	aoff = ((char *)sp - (char *)sip->si_swapslots) * NBBY;
271 
272 	for (boff = (sip->si_hint % NBBW); boff < NBBW; boff++) {
273 		if (!TESTBIT(sip->si_swapslots, aoff + boff))
274 			goto foundslot;
275 		else
276 			sip->si_checkcnt++;
277 	}
278 	for (boff = 0; boff < (sip->si_hint % NBBW); boff++) {
279 		if (!TESTBIT(sip->si_swapslots, aoff + boff))
280 			goto foundslot;
281 		else
282 			sip->si_checkcnt++;
283 	}
284 	panic("swap_getoff: didn't find slot in word hint %ld", sip->si_hint);
285 
286 foundslot:
287 	/*
288 	 * Return the offset of the free page in swap device.
289 	 * Convert page number of byte offset and add starting
290 	 * offset of swap device.
291 	 */
292 	slotnumber = aoff + boff;
293 	SWAP_PRINT(SW_ALLOC, "swap_getoff: allocating slot %ld\n",
294 	    slotnumber, 0, 0, 0, 0);
295 	poff = ptob(slotnumber);
296 	if (poff + sip->si_soff >= sip->si_eoff)
297 		printf("ptob(aoff(%ld) + boff(%ld))(%ld) >= eoff(%ld)\n",
298 		    aoff, boff, ptob(slotnumber), (long)sip->si_eoff);
299 	ASSERT(poff < sip->si_eoff);
300 	/*
301 	 * We could verify here that the slot isn't already allocated
302 	 * by looking through all the anon slots.
303 	 */
304 	SETBIT(sip->si_swapslots, slotnumber);
305 	sip->si_hint = slotnumber + 1;	/* hint = next slot */
306 	return (poff + sip->si_soff);
307 }
308 
309 /*
310  * Free a swap page.
311  */
312 void
313 swap_phys_free(struct vnode *vp, u_offset_t off, size_t len)
314 {
315 	struct swapinfo *sip;
316 	ssize_t pagenumber, npage;
317 
318 	mutex_enter(&swapinfo_lock);
319 	sip = swapinfo;
320 
321 	do {
322 		if (sip->si_vp == vp &&
323 		    sip->si_soff <= off && off < sip->si_eoff) {
324 			for (pagenumber = btop(off - sip->si_soff),
325 			    npage = btop(len) + pagenumber;
326 			    pagenumber < npage; pagenumber++) {
327 				SWAP_PRINT(SW_ALLOC,
328 				    "swap_phys_free: freeing slot %ld on "
329 				    "sip %p\n",
330 				    pagenumber, sip, 0, 0, 0);
331 				if (!TESTBIT(sip->si_swapslots, pagenumber)) {
332 					panic(
333 					    "swap_phys_free: freeing free slot "
334 					    "%p,%lx\n", (void *)vp,
335 					    ptob(pagenumber) + sip->si_soff);
336 				}
337 				CLEARBIT(sip->si_swapslots, pagenumber);
338 				sip->si_nfpgs++;
339 			}
340 			ASSERT(sip->si_nfpgs <= sip->si_npgs);
341 			mutex_exit(&swapinfo_lock);
342 			return;
343 		}
344 	} while ((sip = sip->si_next) != NULL);
345 	panic("swap_phys_free");
346 	/*NOTREACHED*/
347 }
348 
349 /*
350  * Return the anon struct corresponding for the given
351  * <vnode, off> if it is part of the virtual swap device.
352  * Return the anon struct if found, otherwise NULL.
353  */
354 struct anon *
355 swap_anon(struct vnode *vp, u_offset_t off)
356 {
357 	struct anon *ap;
358 
359 	ASSERT(MUTEX_HELD(AH_MUTEX(vp, off)));
360 
361 	for (ap = anon_hash[ANON_HASH(vp, off)]; ap != NULL; ap = ap->an_hash) {
362 		if (ap->an_vp == vp && ap->an_off == off)
363 			return (ap);
364 	}
365 	return (NULL);
366 }
367 
368 
369 /*
370  * Determine if the vp offset range overlap a swap device.
371  */
372 int
373 swap_in_range(struct vnode *vp, u_offset_t offset, size_t len)
374 {
375 	struct swapinfo *sip;
376 	u_offset_t eoff;
377 
378 	eoff = offset + len;
379 	ASSERT(eoff > offset);
380 
381 	mutex_enter(&swapinfo_lock);
382 	sip = swapinfo;
383 	if (vp && sip) {
384 		do {
385 			if (vp != sip->si_vp || eoff <= sip->si_soff ||
386 			    offset >= sip->si_eoff)
387 				continue;
388 			mutex_exit(&swapinfo_lock);
389 			return (1);
390 		} while ((sip = sip->si_next) != NULL);
391 	}
392 	mutex_exit(&swapinfo_lock);
393 	return (0);
394 }
395 
396 /*
397  * See if name is one of our swap files
398  * even though lookupname failed.
399  * This can be used by swapdel to delete
400  * swap resources on remote machines
401  * where the link has gone down.
402  */
403 static struct vnode *
404 swapdel_byname(
405 	char 	*name,			/* pathname to delete */
406 	ulong_t lowblk) 	/* Low block number of area to delete */
407 {
408 	struct swapinfo **sipp, *osip;
409 	u_offset_t soff;
410 
411 	/*
412 	 * Find the swap file entry for the file to
413 	 * be deleted. Skip any entries that are in
414 	 * transition.
415 	 */
416 
417 	soff = ptob(btopr(lowblk << SCTRSHFT)); /* must be page aligned */
418 
419 	mutex_enter(&swapinfo_lock);
420 	for (sipp = &swapinfo; (osip = *sipp) != NULL; sipp = &osip->si_next) {
421 		if ((strcmp(osip->si_pname, name) == 0) &&
422 		    (osip->si_soff == soff) && (osip->si_flags == 0)) {
423 			struct vnode *vp = osip->si_vp;
424 
425 			VN_HOLD(vp);
426 			mutex_exit(&swapinfo_lock);
427 			return (vp);
428 		}
429 	}
430 	mutex_exit(&swapinfo_lock);
431 	return (NULL);
432 }
433 
434 
435 /*
436  * New system call to manipulate swap files.
437  */
438 int
439 swapctl(int sc_cmd, void *sc_arg, int *rv)
440 {
441 	struct swapinfo *sip, *csip, *tsip;
442 	int error = 0;
443 	struct swapent st, *ust;
444 	struct swapres sr;
445 	struct vnode *vp;
446 	int cnt = 0;
447 	int tmp_nswapfiles;
448 	int nswap;
449 	int length, nlen;
450 	int gplen = 0, plen;
451 	char *swapname;
452 	char *pname;
453 	char *tpname;
454 	struct anoninfo ai;
455 	spgcnt_t avail;
456 	int global = INGLOBALZONE(curproc);
457 	struct zone *zp = curproc->p_zone;
458 
459 	/*
460 	 * When running in a zone we want to hide the details of the swap
461 	 * devices: we report there only being one swap device named "swap"
462 	 * having a size equal to the sum of the sizes of all real swap devices
463 	 * on the system.
464 	 */
465 	switch (sc_cmd) {
466 	case SC_GETNSWP:
467 		if (global)
468 			*rv = nswapfiles;
469 		else
470 			*rv = 1;
471 		return (0);
472 
473 	case SC_AINFO:
474 		/*
475 		 * Return anoninfo information with these changes:
476 		 * ani_max = maximum amount of swap space
477 		 *	(including potentially available physical memory)
478 		 * ani_free = amount of unallocated anonymous memory
479 		 *	(some of which might be reserved and including
480 		 *	 potentially available physical memory)
481 		 * ani_resv = amount of claimed (reserved) anonymous memory
482 		 */
483 		avail = MAX((spgcnt_t)(availrmem - swapfs_minfree), 0);
484 		ai.ani_max = (k_anoninfo.ani_max +
485 		    k_anoninfo.ani_mem_resv) + avail;
486 
487 		ai.ani_free = k_anoninfo.ani_free + avail;
488 
489 		ai.ani_resv = k_anoninfo.ani_phys_resv +
490 		    k_anoninfo.ani_mem_resv;
491 
492 		if (!global && zp->zone_max_swap_ctl != UINT64_MAX) {
493 			/*
494 			 * We're in a non-global zone with a swap cap.  We
495 			 * always report the system-wide values for the global
496 			 * zone, even though it too can have a swap cap.
497 			 */
498 
499 			/*
500 			 * For a swap-capped zone, the numbers are contrived
501 			 * since we don't have a correct value of 'reserved'
502 			 * for the zone.
503 			 *
504 			 * The ani_max value is always the zone's swap cap.
505 			 *
506 			 * The ani_free value is always the difference between
507 			 * the cap and the amount of swap in use by the zone.
508 			 *
509 			 * The ani_resv value is typically set to be the amount
510 			 * of swap in use by the zone, but can be adjusted
511 			 * upwards to indicate how much swap is currently
512 			 * unavailable to that zone due to usage by entities
513 			 * outside the zone.
514 			 *
515 			 * This works as follows.
516 			 *
517 			 * In the 'swap -s' output, the data is displayed
518 			 * as follows:
519 			 *    allocated = ani_max  - ani_free
520 			 *    reserved  = ani_resv - allocated
521 			 *    available = ani_max  - ani_resv
522 			 *
523 			 * Taking a contrived example, if the swap cap is 100
524 			 * and the amount of swap used by the zone is 75, this
525 			 * gives:
526 			 *    allocated = ani_max  - ani_free  = 100 - 25 = 75
527 			 *    reserved  = ani_resv - allocated =  75 - 75 =  0
528 			 *    available = ani_max  - ani_resv  = 100 - 75 = 25
529 			 *
530 			 * In this typical case, you can see that the 'swap -s'
531 			 * 'reserved' will always be 0 inside a swap capped
532 			 * zone.
533 			 *
534 			 * However, if the system as a whole has less free
535 			 * swap than the zone limits allow, then we adjust
536 			 * the ani_resv value up so that it is the difference
537 			 * between the zone cap and the amount of free system
538 			 * swap.  Taking the above example, but when the
539 			 * system as a whole only has 20 of swap available, we
540 			 * get an ani_resv of 100 - 20 = 80.  This gives:
541 			 *    allocated = ani_max  - ani_free  = 100 - 25 = 75
542 			 *    reserved  = ani_resv - allocated =  80 - 75 =  5
543 			 *    available = ani_max  - ani_resv  = 100 - 80 = 20
544 			 *
545 			 * In this case, you can see how the ani_resv value is
546 			 * tweaked up to make the 'swap -s' numbers work inside
547 			 * the zone.
548 			 */
549 			rctl_qty_t cap, used;
550 			pgcnt_t pgcap, sys_avail;
551 
552 			mutex_enter(&zp->zone_mem_lock);
553 			cap = zp->zone_max_swap_ctl;
554 			used = zp->zone_max_swap;
555 			mutex_exit(&zp->zone_mem_lock);
556 
557 			pgcap = MIN(btop(cap), ai.ani_max);
558 			ai.ani_free = pgcap - btop(used);
559 
560 			/* Get the system-wide swap currently available. */
561 			sys_avail = ai.ani_max - ai.ani_resv;
562 			if (sys_avail < ai.ani_free)
563 				ai.ani_resv = pgcap - sys_avail;
564 			else
565 				ai.ani_resv = btop(used);
566 
567 			ai.ani_max = pgcap;
568 		}
569 
570 		if (copyout(&ai, sc_arg, sizeof (struct anoninfo)) != 0)
571 			return (EFAULT);
572 		return (0);
573 
574 	case SC_LIST:
575 		if (copyin(sc_arg, &length, sizeof (int)) != 0)
576 			return (EFAULT);
577 		if (!global) {
578 			struct swapent st;
579 			char *swappath = "swap";
580 
581 			if (length < 1)
582 				return (ENOMEM);
583 			ust = (swapent_t *)((swaptbl_t *)sc_arg)->swt_ent;
584 			if (copyin(ust, &st, sizeof (swapent_t)) != 0)
585 				return (EFAULT);
586 			st.ste_start = PAGESIZE >> SCTRSHFT;
587 			st.ste_length = (off_t)0;
588 			st.ste_pages = 0;
589 			st.ste_free = 0;
590 			st.ste_flags = 0;
591 
592 			mutex_enter(&swapinfo_lock);
593 			for (sip = swapinfo, nswap = 0;
594 			    sip != NULL && nswap < nswapfiles;
595 			    sip = sip->si_next, nswap++) {
596 				st.ste_length +=
597 				    (sip->si_eoff - sip->si_soff) >> SCTRSHFT;
598 				st.ste_pages += sip->si_npgs;
599 				st.ste_free += sip->si_nfpgs;
600 			}
601 			mutex_exit(&swapinfo_lock);
602 
603 			if (zp->zone_max_swap_ctl != UINT64_MAX) {
604 				rctl_qty_t cap, used;
605 
606 				mutex_enter(&zp->zone_mem_lock);
607 				cap = zp->zone_max_swap_ctl;
608 				used = zp->zone_max_swap;
609 				mutex_exit(&zp->zone_mem_lock);
610 
611 				st.ste_length = MIN(cap, st.ste_length);
612 				st.ste_pages = MIN(btop(cap), st.ste_pages);
613 				st.ste_free = MIN(st.ste_pages - btop(used),
614 				    st.ste_free);
615 			}
616 
617 			if (copyout(&st, ust, sizeof (swapent_t)) != 0 ||
618 			    copyout(swappath, st.ste_path,
619 			    strlen(swappath) + 1) != 0) {
620 				return (EFAULT);
621 			}
622 			*rv = 1;
623 			return (0);
624 		}
625 beginning:
626 		tmp_nswapfiles = nswapfiles;
627 		/* Return an error if not enough space for the whole table. */
628 		if (length < tmp_nswapfiles)
629 			return (ENOMEM);
630 		/*
631 		 * Get memory to hold the swap entries and their names. We'll
632 		 * copy the real entries into these and then copy these out.
633 		 * Allocating the pathname memory is only a guess so we may
634 		 * find that we need more and have to do it again.
635 		 * All this is because we have to hold the anon lock while
636 		 * traversing the swapinfo list, and we can't be doing copyouts
637 		 * and/or kmem_alloc()s during this.
638 		 */
639 		csip = kmem_zalloc(tmp_nswapfiles * sizeof (struct swapinfo),
640 		    KM_SLEEP);
641 retry:
642 		nlen = tmp_nswapfiles * (gplen += 100);
643 		pname = kmem_zalloc(nlen, KM_SLEEP);
644 
645 		mutex_enter(&swapinfo_lock);
646 
647 		if (tmp_nswapfiles != nswapfiles) {
648 			mutex_exit(&swapinfo_lock);
649 			kmem_free(pname, nlen);
650 			kmem_free(csip,
651 			    tmp_nswapfiles * sizeof (struct swapinfo));
652 			gplen = 0;
653 			goto beginning;
654 		}
655 		for (sip = swapinfo, tsip = csip, tpname = pname, nswap = 0;
656 		    sip && nswap < tmp_nswapfiles;
657 		    sip = sip->si_next, tsip++, tpname += plen, nswap++) {
658 			plen = sip->si_pnamelen;
659 			if (tpname + plen - pname > nlen) {
660 				mutex_exit(&swapinfo_lock);
661 				kmem_free(pname, nlen);
662 				goto retry;
663 			}
664 			*tsip = *sip;
665 			tsip->si_pname = tpname;
666 			(void) strcpy(tsip->si_pname, sip->si_pname);
667 		}
668 		mutex_exit(&swapinfo_lock);
669 
670 		if (sip) {
671 			error = ENOMEM;
672 			goto lout;
673 		}
674 		ust = (swapent_t *)((swaptbl_t *)sc_arg)->swt_ent;
675 		for (tsip = csip, cnt = 0; cnt < nswap;  tsip++, ust++, cnt++) {
676 			if (copyin(ust, &st, sizeof (swapent_t)) != 0) {
677 				error = EFAULT;
678 				goto lout;
679 			}
680 			st.ste_flags = tsip->si_flags;
681 			st.ste_length =
682 			    (tsip->si_eoff - tsip->si_soff) >> SCTRSHFT;
683 			st.ste_start = tsip->si_soff >> SCTRSHFT;
684 			st.ste_pages = tsip->si_npgs;
685 			st.ste_free = tsip->si_nfpgs;
686 			if (copyout(&st, ust, sizeof (swapent_t)) != 0) {
687 				error = EFAULT;
688 				goto lout;
689 			}
690 			if (!tsip->si_pnamelen)
691 				continue;
692 			if (copyout(tsip->si_pname, st.ste_path,
693 			    tsip->si_pnamelen) != 0) {
694 				error = EFAULT;
695 				goto lout;
696 			}
697 		}
698 		*rv = nswap;
699 lout:
700 		kmem_free(csip, tmp_nswapfiles * sizeof (struct swapinfo));
701 		kmem_free(pname, nlen);
702 		return (error);
703 
704 	case SC_ADD:
705 	case SC_REMOVE:
706 		break;
707 	default:
708 		return (EINVAL);
709 	}
710 	if ((error = secpolicy_swapctl(CRED())) != 0)
711 		return (error);
712 
713 	if (copyin(sc_arg, &sr, sizeof (swapres_t)))
714 		return (EFAULT);
715 
716 	/* Allocate the space to read in pathname */
717 	if ((swapname = kmem_alloc(MAXPATHLEN, KM_NOSLEEP)) == NULL)
718 		return (ENOMEM);
719 
720 	error = copyinstr(sr.sr_name, swapname, MAXPATHLEN, 0);
721 	if (error)
722 		goto out;
723 
724 	error = lookupname(swapname, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp);
725 	if (error) {
726 		if (sc_cmd == SC_ADD)
727 			goto out;
728 		/* see if we match by name */
729 		vp = swapdel_byname(swapname, (size_t)sr.sr_start);
730 		if (vp == NULL)
731 			goto out;
732 	}
733 
734 	if (vp->v_flag & (VNOMAP | VNOSWAP)) {
735 		VN_RELE(vp);
736 		error = ENOSYS;
737 		goto out;
738 	}
739 	switch (vp->v_type) {
740 	case VBLK:
741 		break;
742 
743 	case VREG:
744 		if (vp->v_vfsp && vn_is_readonly(vp))
745 			error = EROFS;
746 		else
747 			error = VOP_ACCESS(vp, VREAD|VWRITE, 0, CRED(), NULL);
748 		break;
749 
750 	case VDIR:
751 		error = EISDIR;
752 		break;
753 	default:
754 		error = ENOSYS;
755 		break;
756 	}
757 	if (error == 0) {
758 		if (sc_cmd == SC_REMOVE)
759 			error = swapdel(vp, sr.sr_start);
760 		else
761 			error = swapadd(vp, sr.sr_start,
762 			    sr.sr_length, swapname);
763 	}
764 	VN_RELE(vp);
765 out:
766 	kmem_free(swapname, MAXPATHLEN);
767 	return (error);
768 }
769 
770 #if defined(_LP64) && defined(_SYSCALL32)
771 
772 int
773 swapctl32(int sc_cmd, void *sc_arg, int *rv)
774 {
775 	struct swapinfo *sip, *csip, *tsip;
776 	int error = 0;
777 	struct swapent32 st, *ust;
778 	struct swapres32 sr;
779 	struct vnode *vp;
780 	int cnt = 0;
781 	int tmp_nswapfiles;
782 	int nswap;
783 	int length, nlen;
784 	int gplen = 0, plen;
785 	char *swapname;
786 	char *pname;
787 	char *tpname;
788 	struct anoninfo32 ai;
789 	size_t s;
790 	spgcnt_t avail;
791 	int global = INGLOBALZONE(curproc);
792 	struct zone *zp = curproc->p_zone;
793 
794 	/*
795 	 * When running in a zone we want to hide the details of the swap
796 	 * devices: we report there only being one swap device named "swap"
797 	 * having a size equal to the sum of the sizes of all real swap devices
798 	 * on the system.
799 	 */
800 	switch (sc_cmd) {
801 	case SC_GETNSWP:
802 		if (global)
803 			*rv = nswapfiles;
804 		else
805 			*rv = 1;
806 		return (0);
807 
808 	case SC_AINFO:
809 		/*
810 		 * Return anoninfo information with these changes:
811 		 * ani_max = maximum amount of swap space
812 		 *	(including potentially available physical memory)
813 		 * ani_free = amount of unallocated anonymous memory
814 		 *	(some of which might be reserved and including
815 		 *	 potentially available physical memory)
816 		 * ani_resv = amount of claimed (reserved) anonymous memory
817 		 */
818 		avail = MAX((spgcnt_t)(availrmem - swapfs_minfree), 0);
819 		s = (k_anoninfo.ani_max + k_anoninfo.ani_mem_resv) + avail;
820 		if (s > UINT32_MAX)
821 			return (EOVERFLOW);
822 		ai.ani_max = s;
823 
824 		s = k_anoninfo.ani_free + avail;
825 		if (s > UINT32_MAX)
826 			return (EOVERFLOW);
827 		ai.ani_free = s;
828 
829 		s = k_anoninfo.ani_phys_resv + k_anoninfo.ani_mem_resv;
830 		if (s > UINT32_MAX)
831 			return (EOVERFLOW);
832 		ai.ani_resv = s;
833 
834 		if (!global && zp->zone_max_swap_ctl != UINT64_MAX) {
835 			/*
836 			 * We're in a non-global zone with a swap cap.  We
837 			 * always report the system-wide values for the global
838 			 * zone, even though it too can have a swap cap.
839 			 * See the comment for the SC_AINFO case in swapctl()
840 			 * which explains the following logic.
841 			 */
842 			rctl_qty_t cap, used;
843 			pgcnt_t pgcap, sys_avail;
844 
845 			mutex_enter(&zp->zone_mem_lock);
846 			cap = zp->zone_max_swap_ctl;
847 			used = zp->zone_max_swap;
848 			mutex_exit(&zp->zone_mem_lock);
849 
850 			pgcap = MIN(btop(cap), ai.ani_max);
851 			ai.ani_free = pgcap - btop(used);
852 
853 			/* Get the system-wide swap currently available. */
854 			sys_avail = ai.ani_max - ai.ani_resv;
855 			if (sys_avail < ai.ani_free)
856 				ai.ani_resv = pgcap - sys_avail;
857 			else
858 				ai.ani_resv = btop(used);
859 
860 			ai.ani_max = pgcap;
861 		}
862 
863 		if (copyout(&ai, sc_arg, sizeof (ai)) != 0)
864 			return (EFAULT);
865 		return (0);
866 
867 	case SC_LIST:
868 		if (copyin(sc_arg, &length, sizeof (int32_t)) != 0)
869 			return (EFAULT);
870 		if (!global) {
871 			struct swapent32 st;
872 			char *swappath = "swap";
873 
874 			if (length < 1)
875 				return (ENOMEM);
876 			ust = (swapent32_t *)((swaptbl32_t *)sc_arg)->swt_ent;
877 			if (copyin(ust, &st, sizeof (swapent32_t)) != 0)
878 				return (EFAULT);
879 			st.ste_start = PAGESIZE >> SCTRSHFT;
880 			st.ste_length = (off_t)0;
881 			st.ste_pages = 0;
882 			st.ste_free = 0;
883 			st.ste_flags = 0;
884 
885 			mutex_enter(&swapinfo_lock);
886 			for (sip = swapinfo, nswap = 0;
887 			    sip != NULL && nswap < nswapfiles;
888 			    sip = sip->si_next, nswap++) {
889 				st.ste_length +=
890 				    (sip->si_eoff - sip->si_soff) >> SCTRSHFT;
891 				st.ste_pages += sip->si_npgs;
892 				st.ste_free += sip->si_nfpgs;
893 			}
894 			mutex_exit(&swapinfo_lock);
895 
896 			if (zp->zone_max_swap_ctl != UINT64_MAX) {
897 				rctl_qty_t cap, used;
898 
899 				mutex_enter(&zp->zone_mem_lock);
900 				cap = zp->zone_max_swap_ctl;
901 				used = zp->zone_max_swap;
902 				mutex_exit(&zp->zone_mem_lock);
903 
904 				st.ste_length = MIN(cap, st.ste_length);
905 				st.ste_pages = MIN(btop(cap), st.ste_pages);
906 				st.ste_free = MIN(st.ste_pages - btop(used),
907 				    st.ste_free);
908 			}
909 
910 			if (copyout(&st, ust, sizeof (swapent32_t)) != 0 ||
911 			    copyout(swappath, (caddr_t)(uintptr_t)st.ste_path,
912 			    strlen(swappath) + 1) != 0) {
913 				return (EFAULT);
914 			}
915 			*rv = 1;
916 			return (0);
917 		}
918 beginning:
919 		tmp_nswapfiles = nswapfiles;
920 		/* Return an error if not enough space for the whole table. */
921 		if (length < tmp_nswapfiles)
922 			return (ENOMEM);
923 		/*
924 		 * Get memory to hold the swap entries and their names. We'll
925 		 * copy the real entries into these and then copy these out.
926 		 * Allocating the pathname memory is only a guess so we may
927 		 * find that we need more and have to do it again.
928 		 * All this is because we have to hold the anon lock while
929 		 * traversing the swapinfo list, and we can't be doing copyouts
930 		 * and/or kmem_alloc()s during this.
931 		 */
932 		csip = kmem_zalloc(tmp_nswapfiles * sizeof (*csip), KM_SLEEP);
933 retry:
934 		nlen = tmp_nswapfiles * (gplen += 100);
935 		pname = kmem_zalloc(nlen, KM_SLEEP);
936 
937 		mutex_enter(&swapinfo_lock);
938 
939 		if (tmp_nswapfiles != nswapfiles) {
940 			mutex_exit(&swapinfo_lock);
941 			kmem_free(pname, nlen);
942 			kmem_free(csip, tmp_nswapfiles * sizeof (*csip));
943 			gplen = 0;
944 			goto beginning;
945 		}
946 		for (sip = swapinfo, tsip = csip, tpname = pname, nswap = 0;
947 		    (sip != NULL) && (nswap < tmp_nswapfiles);
948 		    sip = sip->si_next, tsip++, tpname += plen, nswap++) {
949 			plen = sip->si_pnamelen;
950 			if (tpname + plen - pname > nlen) {
951 				mutex_exit(&swapinfo_lock);
952 				kmem_free(pname, nlen);
953 				goto retry;
954 			}
955 			*tsip = *sip;
956 			tsip->si_pname = tpname;
957 			(void) strcpy(tsip->si_pname, sip->si_pname);
958 		}
959 		mutex_exit(&swapinfo_lock);
960 
961 		if (sip != NULL) {
962 			error = ENOMEM;
963 			goto lout;
964 		}
965 		ust = (swapent32_t *)((swaptbl32_t *)sc_arg)->swt_ent;
966 		for (tsip = csip, cnt = 0; cnt < nswap;  tsip++, ust++, cnt++) {
967 			if (copyin(ust, &st, sizeof (*ust)) != 0) {
968 				error = EFAULT;
969 				goto lout;
970 			}
971 			st.ste_flags = tsip->si_flags;
972 			st.ste_length =
973 			    (tsip->si_eoff - tsip->si_soff) >> SCTRSHFT;
974 			st.ste_start = tsip->si_soff >> SCTRSHFT;
975 			st.ste_pages = tsip->si_npgs;
976 			st.ste_free = tsip->si_nfpgs;
977 			if (copyout(&st, ust, sizeof (st)) != 0) {
978 				error = EFAULT;
979 				goto lout;
980 			}
981 			if (!tsip->si_pnamelen)
982 				continue;
983 			if (copyout(tsip->si_pname,
984 			    (caddr_t)(uintptr_t)st.ste_path,
985 			    tsip->si_pnamelen) != 0) {
986 				error = EFAULT;
987 				goto lout;
988 			}
989 		}
990 		*rv = nswap;
991 lout:
992 		kmem_free(csip, tmp_nswapfiles * sizeof (*csip));
993 		kmem_free(pname, nlen);
994 		return (error);
995 
996 	case SC_ADD:
997 	case SC_REMOVE:
998 		break;
999 	default:
1000 		return (EINVAL);
1001 	}
1002 	if ((error = secpolicy_swapctl(CRED())) != 0)
1003 		return (error);
1004 
1005 	if (copyin(sc_arg, &sr, sizeof (sr)))
1006 		return (EFAULT);
1007 
1008 	/* Allocate the space to read in pathname */
1009 	if ((swapname = kmem_alloc(MAXPATHLEN, KM_NOSLEEP)) == NULL)
1010 		return (ENOMEM);
1011 
1012 	error = copyinstr((caddr_t)(uintptr_t)sr.sr_name,
1013 	    swapname, MAXPATHLEN, NULL);
1014 	if (error)
1015 		goto out;
1016 
1017 	error = lookupname(swapname, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp);
1018 	if (error) {
1019 		if (sc_cmd == SC_ADD)
1020 			goto out;
1021 		/* see if we match by name */
1022 		vp = swapdel_byname(swapname, (uint_t)sr.sr_start);
1023 		if (vp == NULL)
1024 			goto out;
1025 	}
1026 
1027 	if (vp->v_flag & (VNOMAP | VNOSWAP)) {
1028 		VN_RELE(vp);
1029 		error = ENOSYS;
1030 		goto out;
1031 	}
1032 	switch (vp->v_type) {
1033 	case VBLK:
1034 		break;
1035 
1036 	case VREG:
1037 		if (vp->v_vfsp && vn_is_readonly(vp))
1038 			error = EROFS;
1039 		else
1040 			error = VOP_ACCESS(vp, VREAD|VWRITE, 0, CRED(), NULL);
1041 		break;
1042 
1043 	case VDIR:
1044 		error = EISDIR;
1045 		break;
1046 	default:
1047 		error = ENOSYS;
1048 		break;
1049 	}
1050 	if (error == 0) {
1051 		if (sc_cmd == SC_REMOVE)
1052 			error = swapdel(vp, sr.sr_start);
1053 		else
1054 			error = swapadd(vp, sr.sr_start, sr.sr_length,
1055 			    swapname);
1056 	}
1057 	VN_RELE(vp);
1058 out:
1059 	kmem_free(swapname, MAXPATHLEN);
1060 	return (error);
1061 }
1062 
1063 #endif /* _LP64 && _SYSCALL32 */
1064 
1065 /*
1066  * Add a new swap file.
1067  */
1068 int
1069 swapadd(struct vnode *vp, ulong_t lowblk, ulong_t nblks, char *swapname)
1070 {
1071 	struct swapinfo **sipp, *nsip = NULL, *esip = NULL;
1072 	struct vnode *cvp;
1073 	struct vattr vattr;
1074 	pgcnt_t pages;
1075 	u_offset_t soff, eoff;
1076 	int error;
1077 	ssize_t i, start, end;
1078 	ushort_t wasswap;
1079 	ulong_t startblk;
1080 	size_t	returned_mem;
1081 
1082 	SWAP_PRINT(SW_CTL, "swapadd: vp %p lowblk %ld nblks %ld swapname %s\n",
1083 	    vp, lowblk, nblks, swapname, 0);
1084 	/*
1085 	 * Get the real vnode. (If vp is not a specnode it just returns vp, so
1086 	 * it does the right thing, but having this code know about specnodes
1087 	 * violates the spirit of having it be indepedent of vnode type.)
1088 	 */
1089 	cvp = common_specvp(vp);
1090 
1091 	/*
1092 	 * Or in VISSWAP so file system has chance to deny swap-ons during open.
1093 	 */
1094 	mutex_enter(&cvp->v_lock);
1095 	wasswap = cvp->v_flag & VISSWAP;
1096 	cvp->v_flag |= VISSWAP;
1097 	mutex_exit(&cvp->v_lock);
1098 
1099 	mutex_enter(&swap_lock);
1100 	if (error = VOP_OPEN(&cvp, FREAD|FWRITE, CRED(), NULL)) {
1101 		mutex_exit(&swap_lock);
1102 		/* restore state of v_flag */
1103 		if (!wasswap) {
1104 			mutex_enter(&cvp->v_lock);
1105 			cvp->v_flag &= ~VISSWAP;
1106 			mutex_exit(&cvp->v_lock);
1107 		}
1108 		return (error);
1109 	}
1110 	mutex_exit(&swap_lock);
1111 
1112 	/*
1113 	 * Get partition size. Return error if empty partition,
1114 	 * or if request does not fit within the partition.
1115 	 * If this is the first swap device, we can reduce
1116 	 * the size of the swap area to match what is
1117 	 * available.  This can happen if the system was built
1118 	 * on a machine with a different size swap partition.
1119 	 */
1120 	vattr.va_mask = AT_SIZE;
1121 	if (error = VOP_GETATTR(cvp, &vattr, ATTR_COMM, CRED(), NULL))
1122 		goto out;
1123 
1124 	/*
1125 	 * Specfs returns a va_size of MAXOFFSET_T (UNKNOWN_SIZE) when the
1126 	 * size of the device can't be determined.
1127 	 */
1128 	if ((vattr.va_size == 0) || (vattr.va_size == MAXOFFSET_T)) {
1129 		error = EINVAL;
1130 		goto out;
1131 	}
1132 
1133 #ifdef	_ILP32
1134 	/*
1135 	 * No support for large swap in 32-bit OS, if the size of the swap is
1136 	 * bigger than MAXOFF32_T then the size used by swapfs must be limited.
1137 	 * This limitation is imposed by the swap subsystem itself, a D_64BIT
1138 	 * driver as the target of swap operation should be able to field
1139 	 * the IO.
1140 	 */
1141 	if (vattr.va_size > MAXOFF32_T) {
1142 		cmn_err(CE_NOTE,
1143 		    "!swap device %s truncated from 0x%llx to 0x%x bytes",
1144 		    swapname, vattr.va_size, MAXOFF32_T);
1145 		vattr.va_size = MAXOFF32_T;
1146 	}
1147 #endif	/* _ILP32 */
1148 
1149 	/* Fail if file not writeable (try to set size to current size) */
1150 	vattr.va_mask = AT_SIZE;
1151 	if (error = VOP_SETATTR(cvp, &vattr, 0, CRED(), NULL))
1152 		goto out;
1153 
1154 	/* Fail if fs does not support VOP_PAGEIO */
1155 	error = VOP_PAGEIO(cvp, (page_t *)NULL, (u_offset_t)0, 0, 0, CRED(),
1156 	    NULL);
1157 
1158 	if (error == ENOSYS)
1159 		goto out;
1160 	else
1161 		error = 0;
1162 	/*
1163 	 * If swapping on the root filesystem don't put swap blocks that
1164 	 * correspond to the miniroot filesystem on the swap free list.
1165 	 */
1166 	if (cvp == rootdir)
1167 		startblk = roundup(MINIROOTSIZE<<SCTRSHFT, klustsize)>>SCTRSHFT;
1168 	else				/* Skip 1st page (disk label) */
1169 		startblk = (ulong_t)(lowblk ? lowblk : 1);
1170 
1171 	soff = startblk << SCTRSHFT;
1172 	if (soff >= vattr.va_size) {
1173 		error = EINVAL;
1174 		goto out;
1175 	}
1176 
1177 	/*
1178 	 * If user specified 0 blks, use the size of the device
1179 	 */
1180 	eoff = nblks ?  soff + (nblks - (startblk - lowblk) << SCTRSHFT) :
1181 	    vattr.va_size;
1182 
1183 	SWAP_PRINT(SW_CTL, "swapadd: va_size %ld soff %ld eoff %ld\n",
1184 	    vattr.va_size, soff, eoff, 0, 0);
1185 
1186 	if (eoff > vattr.va_size) {
1187 		error = EINVAL;
1188 		goto out;
1189 	}
1190 
1191 	/*
1192 	 * The starting and ending offsets must be page aligned.
1193 	 * Round soff up to next page boundary, round eoff
1194 	 * down to previous page boundary.
1195 	 */
1196 	soff = ptob(btopr(soff));
1197 	eoff = ptob(btop(eoff));
1198 	if (soff >= eoff) {
1199 		SWAP_PRINT(SW_CTL, "swapadd: soff %ld >= eoff %ld\n",
1200 		    soff, eoff, 0, 0, 0);
1201 		error = EINVAL;
1202 		goto out;
1203 	}
1204 
1205 	pages = btop(eoff - soff);
1206 
1207 	/* Allocate and partially set up the new swapinfo */
1208 	nsip = kmem_zalloc(sizeof (struct swapinfo), KM_SLEEP);
1209 	nsip->si_vp = cvp;
1210 
1211 	nsip->si_soff = soff;
1212 	nsip->si_eoff = eoff;
1213 	nsip->si_hint = 0;
1214 	nsip->si_checkcnt = nsip->si_alloccnt = 0;
1215 
1216 	nsip->si_pnamelen = (int)strlen(swapname) + 1;
1217 	nsip->si_pname = (char *)kmem_zalloc(nsip->si_pnamelen, KM_SLEEP);
1218 	bcopy(swapname, nsip->si_pname, nsip->si_pnamelen - 1);
1219 	SWAP_PRINT(SW_CTL, "swapadd: allocating swapinfo for %s, %ld pages\n",
1220 	    swapname, pages, 0, 0, 0);
1221 	/*
1222 	 * Size of swapslots map in bytes
1223 	 */
1224 	nsip->si_mapsize = P2ROUNDUP(pages, NBBW) / NBBY;
1225 	nsip->si_swapslots = kmem_zalloc(nsip->si_mapsize, KM_SLEEP);
1226 
1227 	/*
1228 	 * Permanently set the bits that can't ever be allocated,
1229 	 * i.e. those from the ending offset to the round up slot for the
1230 	 * swapslots bit map.
1231 	 */
1232 	start = pages;
1233 	end = P2ROUNDUP(pages, NBBW);
1234 	for (i = start; i < end; i++) {
1235 		SWAP_PRINT(SW_CTL, "swapadd: set bit for page %ld\n", i,
1236 		    0, 0, 0, 0);
1237 		SETBIT(nsip->si_swapslots, i);
1238 	}
1239 	nsip->si_npgs = nsip->si_nfpgs = pages;
1240 	/*
1241 	 * Now check to see if we can add it. We wait til now to check because
1242 	 * we need the swapinfo_lock and we don't want sleep with it (e.g.,
1243 	 * during kmem_alloc()) while we're setting up the swapinfo.
1244 	 */
1245 	mutex_enter(&swapinfo_lock);
1246 	for (sipp = &swapinfo; (esip = *sipp) != NULL; sipp = &esip->si_next) {
1247 		if (esip->si_vp == cvp) {
1248 			if (esip->si_soff == soff && esip->si_npgs == pages &&
1249 			    (esip->si_flags & ST_DOINGDEL)) {
1250 				/*
1251 				 * We are adding a device that we are in the
1252 				 * middle of deleting. Just clear the
1253 				 * ST_DOINGDEL flag to signal this and
1254 				 * the deletion routine will eventually notice
1255 				 * it and add it back.
1256 				 */
1257 				esip->si_flags &= ~ST_DOINGDEL;
1258 				mutex_exit(&swapinfo_lock);
1259 				goto out;
1260 			}
1261 			/* disallow overlapping swap files */
1262 			if ((soff < esip->si_eoff) && (eoff > esip->si_soff)) {
1263 				error = EEXIST;
1264 				mutex_exit(&swapinfo_lock);
1265 				goto out;
1266 			}
1267 		}
1268 	}
1269 
1270 	nswapfiles++;
1271 
1272 	/*
1273 	 * add new swap device to list and shift allocations to it
1274 	 * before updating the anoninfo counters
1275 	 */
1276 	*sipp = nsip;
1277 	silast = nsip;
1278 
1279 	/*
1280 	 * Update the total amount of reservable swap space
1281 	 * accounting properly for swap space from physical memory
1282 	 */
1283 	/* New swap device soaks up currently reserved memory swap */
1284 	mutex_enter(&anoninfo_lock);
1285 
1286 	ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap);
1287 	ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv);
1288 
1289 	k_anoninfo.ani_max += pages;
1290 	ANI_ADD(pages);
1291 	if (k_anoninfo.ani_mem_resv > k_anoninfo.ani_locked_swap) {
1292 		returned_mem = MIN(k_anoninfo.ani_mem_resv -
1293 		    k_anoninfo.ani_locked_swap,
1294 		    k_anoninfo.ani_max - k_anoninfo.ani_phys_resv);
1295 
1296 		ANI_ADD(-returned_mem);
1297 		k_anoninfo.ani_free -= returned_mem;
1298 		k_anoninfo.ani_mem_resv -= returned_mem;
1299 		k_anoninfo.ani_phys_resv += returned_mem;
1300 
1301 		mutex_enter(&freemem_lock);
1302 		availrmem += returned_mem;
1303 		mutex_exit(&freemem_lock);
1304 	}
1305 	/*
1306 	 * At boot time, to permit booting small memory machines using
1307 	 * only physical memory as swap space, we allowed a dangerously
1308 	 * large amount of memory to be used as swap space; now that
1309 	 * more physical backing store is available bump down the amount
1310 	 * we can get from memory to a safer size.
1311 	 */
1312 	if (swapfs_minfree < swapfs_desfree) {
1313 		mutex_enter(&freemem_lock);
1314 		if (availrmem > swapfs_desfree || !k_anoninfo.ani_mem_resv)
1315 			swapfs_minfree = swapfs_desfree;
1316 		mutex_exit(&freemem_lock);
1317 	}
1318 
1319 	SWAP_PRINT(SW_CTL, "swapadd: ani_max %ld ani_free %ld\n",
1320 	    k_anoninfo.ani_free, k_anoninfo.ani_free, 0, 0, 0);
1321 
1322 	mutex_exit(&anoninfo_lock);
1323 
1324 	mutex_exit(&swapinfo_lock);
1325 
1326 	/* Initialize the dump device */
1327 	mutex_enter(&dump_lock);
1328 	if (dumpvp == NULL)
1329 		(void) dumpinit(vp, swapname, 0);
1330 	mutex_exit(&dump_lock);
1331 
1332 	VN_HOLD(cvp);
1333 out:
1334 	if (error || esip) {
1335 		SWAP_PRINT(SW_CTL, "swapadd: error (%d)\n", error, 0, 0, 0, 0);
1336 
1337 		if (!wasswap) {
1338 			mutex_enter(&cvp->v_lock);
1339 			cvp->v_flag &= ~VISSWAP;
1340 			mutex_exit(&cvp->v_lock);
1341 		}
1342 		if (nsip) {
1343 			kmem_free(nsip->si_swapslots, (size_t)nsip->si_mapsize);
1344 			kmem_free(nsip->si_pname, nsip->si_pnamelen);
1345 			kmem_free(nsip, sizeof (*nsip));
1346 		}
1347 		mutex_enter(&swap_lock);
1348 		(void) VOP_CLOSE(cvp, FREAD|FWRITE, 1, (offset_t)0, CRED(),
1349 		    NULL);
1350 		mutex_exit(&swap_lock);
1351 	}
1352 	return (error);
1353 }
1354 
1355 /*
1356  * Delete a swap file.
1357  */
1358 static int
1359 swapdel(
1360 	struct vnode *vp,
1361 	ulong_t lowblk) /* Low block number of area to delete. */
1362 {
1363 	struct swapinfo **sipp, *osip = NULL;
1364 	struct vnode *cvp;
1365 	u_offset_t soff;
1366 	int error = 0;
1367 	u_offset_t toff = 0;
1368 	struct vnode *tvp = NULL;
1369 	spgcnt_t pages;
1370 	struct anon **app, *ap;
1371 	kmutex_t *ahm;
1372 	pgcnt_t adjust_swap = 0;
1373 
1374 	/* Find the swap file entry for the file to be deleted */
1375 	cvp = common_specvp(vp);
1376 
1377 
1378 	lowblk = lowblk ? lowblk : 1; 	/* Skip first page (disk label) */
1379 	soff = ptob(btopr(lowblk << SCTRSHFT)); /* must be page aligned */
1380 
1381 	mutex_enter(&swapinfo_lock);
1382 	for (sipp = &swapinfo; (osip = *sipp) != NULL; sipp = &osip->si_next) {
1383 		if ((osip->si_vp == cvp) &&
1384 		    (osip->si_soff == soff) && (osip->si_flags == 0))
1385 			break;
1386 	}
1387 
1388 	/* If the file was not found, error.  */
1389 	if (osip == NULL) {
1390 		error = EINVAL;
1391 		mutex_exit(&swapinfo_lock);
1392 		goto out;
1393 	}
1394 
1395 	pages = osip->si_npgs;
1396 
1397 	/*
1398 	 * Do not delete if we will be low on swap pages.
1399 	 */
1400 	mutex_enter(&anoninfo_lock);
1401 
1402 	ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap);
1403 	ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv);
1404 
1405 	mutex_enter(&freemem_lock);
1406 	if (((k_anoninfo.ani_max - k_anoninfo.ani_phys_resv) +
1407 	    MAX((spgcnt_t)(availrmem - swapfs_minfree), 0)) < pages) {
1408 		mutex_exit(&freemem_lock);
1409 		mutex_exit(&anoninfo_lock);
1410 		error = ENOMEM;
1411 		cmn_err(CE_WARN, "swapdel - too few free pages");
1412 		mutex_exit(&swapinfo_lock);
1413 		goto out;
1414 	}
1415 	mutex_exit(&freemem_lock);
1416 
1417 	k_anoninfo.ani_max -= pages;
1418 
1419 	/* If needed, reserve memory swap to replace old device */
1420 	if (k_anoninfo.ani_phys_resv > k_anoninfo.ani_max) {
1421 		adjust_swap = k_anoninfo.ani_phys_resv - k_anoninfo.ani_max;
1422 		k_anoninfo.ani_phys_resv -= adjust_swap;
1423 		k_anoninfo.ani_mem_resv += adjust_swap;
1424 		mutex_enter(&freemem_lock);
1425 		availrmem -= adjust_swap;
1426 		mutex_exit(&freemem_lock);
1427 		ANI_ADD(adjust_swap);
1428 	}
1429 	ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap);
1430 	ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv);
1431 	mutex_exit(&anoninfo_lock);
1432 
1433 	ANI_ADD(-pages);
1434 
1435 	/*
1436 	 * Set the delete flag.  This prevents anyone from allocating more
1437 	 * pages from this file. Also set ST_DOINGDEL. Someone who wants to
1438 	 * add the file back while we're deleting it will signify by clearing
1439 	 * this flag.
1440 	 */
1441 	osip->si_flags |= ST_INDEL|ST_DOINGDEL;
1442 	mutex_exit(&swapinfo_lock);
1443 
1444 	/*
1445 	 * Free all the allocated physical slots for this file. We do this
1446 	 * by walking through the entire anon hash array, because we need
1447 	 * to update all the anon slots that have physical swap slots on
1448 	 * this file, and this is the only way to find them all. We go back
1449 	 * to the beginning of a bucket after each slot is freed because the
1450 	 * anonhash_lock is not held during the free and thus the hash table
1451 	 * may change under us.
1452 	 */
1453 	for (app = anon_hash; app < &anon_hash[ANON_HASH_SIZE]; app++) {
1454 		ahm = &anonhash_lock[(app - anon_hash) &
1455 		    (AH_LOCK_SIZE - 1)].pad_mutex;
1456 		mutex_enter(ahm);
1457 top:
1458 		for (ap = *app; ap != NULL; ap = ap->an_hash) {
1459 			if (ap->an_pvp == cvp &&
1460 			    ap->an_poff >= osip->si_soff &&
1461 			    ap->an_poff < osip->si_eoff) {
1462 				ASSERT(TESTBIT(osip->si_swapslots,
1463 				    btop((size_t)(ap->an_poff -
1464 				    osip->si_soff))));
1465 				tvp = ap->an_vp;
1466 				toff = ap->an_off;
1467 				VN_HOLD(tvp);
1468 				mutex_exit(ahm);
1469 
1470 				error = swapslot_free(tvp, toff, osip);
1471 
1472 				VN_RELE(tvp);
1473 				mutex_enter(ahm);
1474 				if (!error && (osip->si_flags & ST_DOINGDEL)) {
1475 					goto top;
1476 				} else {
1477 					if (error) {
1478 						cmn_err(CE_WARN,
1479 						    "swapslot_free failed %d",
1480 						    error);
1481 					}
1482 
1483 					/*
1484 					 * Add device back before making it
1485 					 * visible.
1486 					 */
1487 					mutex_enter(&swapinfo_lock);
1488 					osip->si_flags &=
1489 					    ~(ST_INDEL | ST_DOINGDEL);
1490 					mutex_exit(&swapinfo_lock);
1491 
1492 					/*
1493 					 * Update the anon space available
1494 					 */
1495 					mutex_enter(&anoninfo_lock);
1496 
1497 					k_anoninfo.ani_phys_resv += adjust_swap;
1498 					k_anoninfo.ani_mem_resv -= adjust_swap;
1499 					k_anoninfo.ani_max += pages;
1500 
1501 					mutex_enter(&freemem_lock);
1502 					availrmem += adjust_swap;
1503 					mutex_exit(&freemem_lock);
1504 
1505 					mutex_exit(&anoninfo_lock);
1506 
1507 					ANI_ADD(pages);
1508 
1509 					mutex_exit(ahm);
1510 					goto out;
1511 				}
1512 			}
1513 		}
1514 		mutex_exit(ahm);
1515 	}
1516 
1517 	/* All done, they'd better all be free! */
1518 	mutex_enter(&swapinfo_lock);
1519 	ASSERT(osip->si_nfpgs == osip->si_npgs);
1520 
1521 	/* Now remove it from the swapinfo list */
1522 	for (sipp = &swapinfo; *sipp != NULL; sipp = &(*sipp)->si_next) {
1523 		if (*sipp == osip)
1524 			break;
1525 	}
1526 	ASSERT(*sipp);
1527 	*sipp = osip->si_next;
1528 	if (silast == osip)
1529 		if ((silast = osip->si_next) == NULL)
1530 			silast = swapinfo;
1531 	nswapfiles--;
1532 	mutex_exit(&swapinfo_lock);
1533 
1534 	kmem_free(osip->si_swapslots, osip->si_mapsize);
1535 	kmem_free(osip->si_pname, osip->si_pnamelen);
1536 	kmem_free(osip, sizeof (*osip));
1537 
1538 	mutex_enter(&dump_lock);
1539 	if (cvp == dumpvp)
1540 		dumpfini();
1541 	mutex_exit(&dump_lock);
1542 
1543 	/* Release the vnode */
1544 
1545 	mutex_enter(&swap_lock);
1546 	(void) VOP_CLOSE(cvp, FREAD|FWRITE, 1, (offset_t)0, CRED(), NULL);
1547 	mutex_enter(&cvp->v_lock);
1548 	cvp->v_flag &= ~VISSWAP;
1549 	mutex_exit(&cvp->v_lock);
1550 	VN_RELE(cvp);
1551 	mutex_exit(&swap_lock);
1552 out:
1553 	return (error);
1554 }
1555 
1556 /*
1557  * Free up a physical swap slot on swapinfo sip, currently in use by the
1558  * anonymous page whose name is (vp, off).
1559  */
1560 static int
1561 swapslot_free(
1562 	struct vnode *vp,
1563 	u_offset_t off,
1564 	struct swapinfo *sip)
1565 {
1566 	struct page *pp = NULL;
1567 	struct anon *ap = NULL;
1568 	int error = 0;
1569 	kmutex_t *ahm;
1570 	struct vnode *pvp = NULL;
1571 	u_offset_t poff;
1572 	int	alloc_pg = 0;
1573 
1574 	ASSERT(sip->si_vp != NULL);
1575 	/*
1576 	 * Get the page for the old swap slot if exists or create a new one.
1577 	 */
1578 again:
1579 	if ((pp = page_lookup(vp, off, SE_SHARED)) == NULL) {
1580 		pp = page_create_va(vp, off, PAGESIZE, PG_WAIT | PG_EXCL,
1581 		    segkmap, NULL);
1582 		if (pp == NULL)
1583 			goto again;
1584 		alloc_pg = 1;
1585 
1586 		error = swap_getphysname(vp, off, &pvp, &poff);
1587 		if (error || pvp != sip->si_vp || poff < sip->si_soff ||
1588 		    poff >= sip->si_eoff) {
1589 			page_io_unlock(pp);
1590 			/*LINTED: constant in conditional context*/
1591 			VN_DISPOSE(pp, B_INVAL, 0, kcred);
1592 			return (0);
1593 		}
1594 
1595 		error = VOP_PAGEIO(pvp, pp, poff, PAGESIZE, B_READ,
1596 		    CRED(), NULL);
1597 		if (error) {
1598 			page_io_unlock(pp);
1599 			if (error == EFAULT)
1600 				error = 0;
1601 			/*LINTED: constant in conditional context*/
1602 			VN_DISPOSE(pp, B_INVAL, 0, kcred);
1603 			return (error);
1604 		}
1605 	}
1606 
1607 	/*
1608 	 * The anon could have been removed by anon_decref* and/or reallocated
1609 	 * by anon layer (an_pvp == NULL) with the same vp, off.
1610 	 * In this case the page which has been allocated needs to
1611 	 * be freed.
1612 	 */
1613 	if (!alloc_pg)
1614 		page_io_lock(pp);
1615 	ahm = AH_MUTEX(vp, off);
1616 	mutex_enter(ahm);
1617 	ap = swap_anon(vp, off);
1618 	if ((ap == NULL || ap->an_pvp == NULL) && alloc_pg) {
1619 		mutex_exit(ahm);
1620 		page_io_unlock(pp);
1621 		/*LINTED: constant in conditional context*/
1622 		VN_DISPOSE(pp, B_INVAL, 0, kcred);
1623 		return (0);
1624 	}
1625 
1626 	/*
1627 	 * Free the physical slot. It may have been freed up and replaced with
1628 	 * another one while we were getting the page so we have to re-verify
1629 	 * that this is really one we want. If we do free the slot we have
1630 	 * to mark the page modified, as its backing store is now gone.
1631 	 */
1632 	if ((ap != NULL) && (ap->an_pvp == sip->si_vp && ap->an_poff >=
1633 	    sip->si_soff && ap->an_poff < sip->si_eoff)) {
1634 		swap_phys_free(ap->an_pvp, ap->an_poff, PAGESIZE);
1635 		ap->an_pvp = NULL;
1636 		ap->an_poff = 0;
1637 		mutex_exit(ahm);
1638 		hat_setmod(pp);
1639 	} else {
1640 		mutex_exit(ahm);
1641 	}
1642 	page_io_unlock(pp);
1643 	page_unlock(pp);
1644 	return (0);
1645 }
1646 
1647 
1648 /*
1649  * Get contig physical backing store for vp, in the range
1650  * [*offp, *offp + *lenp), May back a subrange of this, but must
1651  * always include the requested offset or fail. Returns the offsets
1652  * backed as [*offp, *offp + *lenp) and the physical offsets used to
1653  * back them from *pvpp in the range [*pstartp, *pstartp + *lenp).
1654  * Returns 	0 for success
1655  * 		SE_NOANON -- no anon slot for requested paged
1656  *		SE_NOSWAP -- no physical swap space available
1657  */
1658 int
1659 swap_newphysname(
1660 	struct vnode *vp,
1661 	u_offset_t offset,
1662 	u_offset_t *offp,
1663 	size_t *lenp,
1664 	struct vnode **pvpp,
1665 	u_offset_t *poffp)
1666 {
1667 	struct anon *ap = NULL;		/* anon slot for vp, off */
1668 	int error = 0;
1669 	struct vnode *pvp;
1670 	u_offset_t poff, pstart, prem;
1671 	size_t plen;
1672 	u_offset_t off, start;
1673 	kmutex_t *ahm;
1674 
1675 	ASSERT(*offp <= offset && offset < *offp + *lenp);
1676 
1677 	/* Get new physical swap slots. */
1678 	plen = *lenp;
1679 	if (!swap_phys_alloc(&pvp, &pstart, &plen, 0)) {
1680 		/*
1681 		 * No swap available so return error unless requested
1682 		 * offset is already backed in which case return that.
1683 		 */
1684 		ahm = AH_MUTEX(vp, offset);
1685 		mutex_enter(ahm);
1686 		if ((ap = swap_anon(vp, offset)) == NULL) {
1687 			error = SE_NOANON;
1688 			mutex_exit(ahm);
1689 			return (error);
1690 		}
1691 		error = (ap->an_pvp ? 0 : SE_NOSWAP);
1692 		*offp = offset;
1693 		*lenp = PAGESIZE;
1694 		*pvpp = ap->an_pvp;
1695 		*poffp = ap->an_poff;
1696 		mutex_exit(ahm);
1697 		return (error);
1698 	}
1699 
1700 	/*
1701 	 * We got plen (<= *lenp) contig slots. Use these to back a
1702 	 * subrange of [*offp, *offp + *lenp) which includes offset.
1703 	 * For now we just put offset at the end of the kluster.
1704 	 * Clearly there are other possible choices - which is best?
1705 	 */
1706 	start = MAX(*offp,
1707 	    (offset + PAGESIZE > plen) ? (offset + PAGESIZE - plen) : 0);
1708 	ASSERT(start + plen <= *offp + *lenp);
1709 
1710 	for (off = start, poff = pstart; poff < pstart + plen;
1711 	    off += PAGESIZE, poff += PAGESIZE) {
1712 		ahm = AH_MUTEX(vp, off);
1713 		mutex_enter(ahm);
1714 		if ((ap = swap_anon(vp, off)) != NULL) {
1715 			/* Free old slot if any, and assign new one */
1716 			if (ap->an_pvp)
1717 				swap_phys_free(ap->an_pvp, ap->an_poff,
1718 				    PAGESIZE);
1719 			ap->an_pvp = pvp;
1720 			ap->an_poff = poff;
1721 		} else {	/* No anon slot for a klustered page, quit. */
1722 			prem = (pstart + plen) - poff;
1723 			/* Already did requested page, do partial kluster */
1724 			if (off > offset) {
1725 				plen = poff - pstart;
1726 				error = 0;
1727 			/* Fail on requested page, error */
1728 			} else if (off == offset)  {
1729 				error = SE_NOANON;
1730 			/* Fail on prior page, fail on requested page, error */
1731 			} else if ((ap = swap_anon(vp, offset)) == NULL) {
1732 				error = SE_NOANON;
1733 			/* Fail on prior page, got requested page, do only it */
1734 			} else {
1735 				/* Free old slot if any, and assign new one */
1736 				if (ap->an_pvp)
1737 					swap_phys_free(ap->an_pvp, ap->an_poff,
1738 					    PAGESIZE);
1739 				ap->an_pvp = pvp;
1740 				ap->an_poff = poff;
1741 				/* One page kluster */
1742 				start = offset;
1743 				plen = PAGESIZE;
1744 				pstart = poff;
1745 				poff += PAGESIZE;
1746 				prem -= PAGESIZE;
1747 			}
1748 			/* Free unassigned slots */
1749 			swap_phys_free(pvp, poff, prem);
1750 			mutex_exit(ahm);
1751 			break;
1752 		}
1753 		mutex_exit(ahm);
1754 	}
1755 	ASSERT(*offp <= start && start + plen <= *offp + *lenp);
1756 	ASSERT(start <= offset && offset < start + plen);
1757 	*offp = start;
1758 	*lenp = plen;
1759 	*pvpp = pvp;
1760 	*poffp = pstart;
1761 	return (error);
1762 }
1763 
1764 
1765 /*
1766  * Get the physical swap backing store location for a given anonymous page
1767  * named (vp, off). The backing store name is returned in (*pvpp, *poffp).
1768  * Returns	0 		success
1769  *		EIDRM --	no anon slot (page is not allocated)
1770  */
1771 int
1772 swap_getphysname(
1773 	struct vnode *vp,
1774 	u_offset_t off,
1775 	struct vnode **pvpp,
1776 	u_offset_t *poffp)
1777 {
1778 	struct anon *ap;
1779 	int error = 0;
1780 	kmutex_t *ahm;
1781 
1782 	ahm = AH_MUTEX(vp, off);
1783 	mutex_enter(ahm);
1784 
1785 	/* Get anon slot for vp, off */
1786 	ap = swap_anon(vp, off);
1787 	if (ap == NULL) {
1788 		error = EIDRM;
1789 		goto out;
1790 	}
1791 	*pvpp = ap->an_pvp;
1792 	*poffp = ap->an_poff;
1793 out:
1794 	mutex_exit(ahm);
1795 	return (error);
1796 }
1797