#include <sys/types.h>
#include <sys/inttypes.h>
#include <sys/param.h>
#include <sys/t_lock.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/kmem.h>
#include <sys/vfs.h>
#include <sys/vnode.h>
#include <sys/pathname.h>
#include <sys/cmn_err.h>
#include <sys/vtrace.h>
#include <sys/swap.h>
#include <sys/dumphdr.h>
#include <sys/debug.h>
#include <sys/fs/snode.h>
#include <sys/fs/swapnode.h>
#include <sys/policy.h>
#include <sys/zone.h>
#include <vm/as.h>
#include <vm/seg.h>
#include <vm/page.h>
#include <vm/seg_vn.h>
#include <vm/hat.h>
#include <vm/anon.h>
#include <vm/seg_map.h>
int swap_maxcontig;
#define MINIROOTSIZE 12000
static kmutex_t swap_lock;
kmutex_t swapinfo_lock;
extern struct swapinfo *swapinfo;
static struct swapinfo *silast;
static int nswapfiles;
static u_offset_t swap_getoff(struct swapinfo *);
static int swapadd(struct vnode *, ulong_t, ulong_t, char *);
static int swapdel(struct vnode *, ulong_t);
static int swapslot_free(struct vnode *, u_offset_t, struct swapinfo *);
#define MAPSHIFT 5
#define NBBW (NBPW * NBBY)
#define TESTBIT(map, i) (((map)[(i) >> MAPSHIFT] & (1 << (i) % NBBW)))
#define SETBIT(map, i) (((map)[(i) >> MAPSHIFT] |= (1 << (i) % NBBW)))
#define CLEARBIT(map, i) (((map)[(i) >> MAPSHIFT] &= ~(1 << (i) % NBBW)))
int swap_debug = 0;
int swap_verify = 0;
uint_t swapalloc_maxcontig;
int
swap_phys_alloc(
struct vnode **vpp,
u_offset_t *offp,
size_t *lenp,
uint_t flags)
{
struct swapinfo *sip;
offset_t soff, noff;
size_t len;
mutex_enter(&swapinfo_lock);
sip = silast;
do {
if (sip == NULL)
break;
if (!(sip->si_flags & ST_INDEL) &&
(spgcnt_t)sip->si_nfpgs > 0) {
if (flags & SA_NOT) {
if (*vpp != sip->si_vp ||
*offp < sip->si_soff ||
*offp >= sip->si_eoff)
goto found;
} else
goto found;
} else if (sip->si_nfpgs == 0)
sip->si_allocs = 0;
if ((sip = sip->si_next) == NULL)
sip = swapinfo;
} while (sip != silast);
mutex_exit(&swapinfo_lock);
return (0);
found:
soff = swap_getoff(sip);
sip->si_nfpgs--;
if (soff == -1)
panic("swap_alloc: swap_getoff failed!");
for (len = PAGESIZE; len < *lenp; len += PAGESIZE) {
if (sip->si_nfpgs == 0)
break;
if (swapalloc_maxcontig && len >= swapalloc_maxcontig)
break;
noff = swap_getoff(sip);
if (noff == -1) {
break;
} else if (noff != soff + len) {
CLEARBIT(sip->si_swapslots, btop(noff - sip->si_soff));
break;
}
sip->si_nfpgs--;
}
*vpp = sip->si_vp;
*offp = soff;
*lenp = len;
ASSERT((spgcnt_t)sip->si_nfpgs >= 0);
sip->si_allocs += btop(len);
if (sip->si_allocs >= swap_maxcontig) {
sip->si_allocs = 0;
if ((silast = sip->si_next) == NULL)
silast = swapinfo;
}
TRACE_2(TR_FAC_VM, TR_SWAP_ALLOC,
"swap_alloc:sip %p offset %lx", sip, soff);
mutex_exit(&swapinfo_lock);
return (1);
}
int swap_backsearch = 0;
static u_offset_t
swap_getoff(struct swapinfo *sip)
{
uint_t *sp, *ep;
size_t aoff, boff, poff, slotnumber;
ASSERT(MUTEX_HELD(&swapinfo_lock));
sip->si_alloccnt++;
for (sp = &sip->si_swapslots[sip->si_hint >> MAPSHIFT],
ep = &sip->si_swapslots[sip->si_mapsize / NBPW]; sp < ep; sp++) {
if (*sp != (uint_t)0xffffffff)
goto foundentry;
else
sip->si_checkcnt++;
}
SWAP_PRINT(SW_ALLOC,
"swap_getoff: couldn't find slot from hint %ld to end\n",
sip->si_hint, 0, 0, 0, 0);
if (swap_backsearch) {
for (sp = &sip->si_swapslots[sip->si_hint >> MAPSHIFT],
ep = sip->si_swapslots; sp > ep; sp--) {
if (*sp != (uint_t)0xffffffff)
goto foundentry;
else
sip->si_checkcnt++;
}
} else {
for (sp = sip->si_swapslots,
ep = &sip->si_swapslots[sip->si_hint >> MAPSHIFT];
sp < ep; sp++) {
if (*sp != (uint_t)0xffffffff)
goto foundentry;
else
sip->si_checkcnt++;
}
}
if (*sp == 0xffffffff) {
cmn_err(CE_WARN, "No free swap slots!");
return ((u_offset_t)-1);
}
foundentry:
aoff = ((char *)sp - (char *)sip->si_swapslots) * NBBY;
for (boff = (sip->si_hint % NBBW); boff < NBBW; boff++) {
if (!TESTBIT(sip->si_swapslots, aoff + boff))
goto foundslot;
else
sip->si_checkcnt++;
}
for (boff = 0; boff < (sip->si_hint % NBBW); boff++) {
if (!TESTBIT(sip->si_swapslots, aoff + boff))
goto foundslot;
else
sip->si_checkcnt++;
}
panic("swap_getoff: didn't find slot in word hint %ld", sip->si_hint);
foundslot:
slotnumber = aoff + boff;
SWAP_PRINT(SW_ALLOC, "swap_getoff: allocating slot %ld\n",
slotnumber, 0, 0, 0, 0);
poff = ptob(slotnumber);
if (poff + sip->si_soff >= sip->si_eoff)
printf("ptob(aoff(%ld) + boff(%ld))(%ld) >= eoff(%ld)\n",
aoff, boff, ptob(slotnumber), (long)sip->si_eoff);
ASSERT(poff < sip->si_eoff);
SETBIT(sip->si_swapslots, slotnumber);
sip->si_hint = slotnumber + 1;
return (poff + sip->si_soff);
}
void
swap_phys_free(struct vnode *vp, u_offset_t off, size_t len)
{
struct swapinfo *sip;
ssize_t pagenumber, npage;
mutex_enter(&swapinfo_lock);
sip = swapinfo;
do {
if (sip->si_vp == vp &&
sip->si_soff <= off && off < sip->si_eoff) {
for (pagenumber = btop(off - sip->si_soff),
npage = btop(len) + pagenumber;
pagenumber < npage; pagenumber++) {
SWAP_PRINT(SW_ALLOC,
"swap_phys_free: freeing slot %ld on "
"sip %p\n",
pagenumber, sip, 0, 0, 0);
if (!TESTBIT(sip->si_swapslots, pagenumber)) {
panic(
"swap_phys_free: freeing free slot "
"%p,%lx\n", (void *)vp,
ptob(pagenumber) + sip->si_soff);
}
CLEARBIT(sip->si_swapslots, pagenumber);
sip->si_nfpgs++;
}
ASSERT(sip->si_nfpgs <= sip->si_npgs);
mutex_exit(&swapinfo_lock);
return;
}
} while ((sip = sip->si_next) != NULL);
panic("swap_phys_free");
}
struct anon *
swap_anon(struct vnode *vp, u_offset_t off)
{
struct anon *ap;
ASSERT(MUTEX_HELD(AH_MUTEX(vp, off)));
for (ap = anon_hash[ANON_HASH(vp, off)]; ap != NULL; ap = ap->an_hash) {
if (ap->an_vp == vp && ap->an_off == off)
return (ap);
}
return (NULL);
}
int
swap_in_range(struct vnode *vp, u_offset_t offset, size_t len)
{
struct swapinfo *sip;
u_offset_t eoff;
eoff = offset + len;
ASSERT(eoff > offset);
mutex_enter(&swapinfo_lock);
sip = swapinfo;
if (vp && sip) {
do {
if (vp != sip->si_vp || eoff <= sip->si_soff ||
offset >= sip->si_eoff)
continue;
mutex_exit(&swapinfo_lock);
return (1);
} while ((sip = sip->si_next) != NULL);
}
mutex_exit(&swapinfo_lock);
return (0);
}
static struct vnode *
swapdel_byname(
char *name,
ulong_t lowblk)
{
struct swapinfo **sipp, *osip;
u_offset_t soff;
soff = ptob(btopr(lowblk << SCTRSHFT));
mutex_enter(&swapinfo_lock);
for (sipp = &swapinfo; (osip = *sipp) != NULL; sipp = &osip->si_next) {
if ((strcmp(osip->si_pname, name) == 0) &&
(osip->si_soff == soff) && (osip->si_flags == 0)) {
struct vnode *vp = osip->si_vp;
VN_HOLD(vp);
mutex_exit(&swapinfo_lock);
return (vp);
}
}
mutex_exit(&swapinfo_lock);
return (NULL);
}
int
swapctl(int sc_cmd, void *sc_arg, int *rv)
{
struct swapinfo *sip, *csip, *tsip;
int error = 0;
struct swapent st, *ust;
struct swapres sr;
struct vnode *vp;
int cnt = 0;
int tmp_nswapfiles;
int nswap;
int length, nlen;
int gplen = 0, plen;
char *swapname;
char *pname;
char *tpname;
struct anoninfo ai;
spgcnt_t avail;
int global = INGLOBALZONE(curproc);
struct zone *zp = curproc->p_zone;
switch (sc_cmd) {
case SC_GETNSWP:
if (global)
*rv = nswapfiles;
else
*rv = 1;
return (0);
case SC_AINFO:
avail = MAX((spgcnt_t)(availrmem - swapfs_minfree), 0);
ai.ani_max = (k_anoninfo.ani_max +
k_anoninfo.ani_mem_resv) + avail;
set_anoninfo();
ai.ani_free = k_anoninfo.ani_free + avail;
ai.ani_resv = k_anoninfo.ani_phys_resv +
k_anoninfo.ani_mem_resv;
if (!global && zp->zone_max_swap_ctl != UINT64_MAX) {
rctl_qty_t cap, used;
pgcnt_t pgcap, sys_avail;
mutex_enter(&zp->zone_mem_lock);
cap = zp->zone_max_swap_ctl;
used = zp->zone_max_swap;
mutex_exit(&zp->zone_mem_lock);
pgcap = MIN(btop(cap), ai.ani_max);
ai.ani_free = pgcap - btop(used);
sys_avail = ai.ani_max - ai.ani_resv;
if (sys_avail < ai.ani_free)
ai.ani_resv = pgcap - sys_avail;
else
ai.ani_resv = btop(used);
ai.ani_max = pgcap;
}
if (copyout(&ai, sc_arg, sizeof (struct anoninfo)) != 0)
return (EFAULT);
return (0);
case SC_LIST:
if (copyin(sc_arg, &length, sizeof (int)) != 0)
return (EFAULT);
if (!global) {
struct swapent st;
char *swappath = "swap";
if (length < 1)
return (ENOMEM);
ust = (swapent_t *)((swaptbl_t *)sc_arg)->swt_ent;
if (copyin(ust, &st, sizeof (swapent_t)) != 0)
return (EFAULT);
st.ste_start = PAGESIZE >> SCTRSHFT;
st.ste_length = (off_t)0;
st.ste_pages = 0;
st.ste_free = 0;
st.ste_flags = 0;
mutex_enter(&swapinfo_lock);
for (sip = swapinfo, nswap = 0;
sip != NULL && nswap < nswapfiles;
sip = sip->si_next, nswap++) {
st.ste_length +=
(sip->si_eoff - sip->si_soff) >> SCTRSHFT;
st.ste_pages += sip->si_npgs;
st.ste_free += sip->si_nfpgs;
}
mutex_exit(&swapinfo_lock);
if (zp->zone_max_swap_ctl != UINT64_MAX) {
rctl_qty_t cap, used;
mutex_enter(&zp->zone_mem_lock);
cap = zp->zone_max_swap_ctl;
used = zp->zone_max_swap;
mutex_exit(&zp->zone_mem_lock);
st.ste_length = MIN(cap, st.ste_length);
st.ste_pages = MIN(btop(cap), st.ste_pages);
st.ste_free = MIN(st.ste_pages - btop(used),
st.ste_free);
}
if (copyout(&st, ust, sizeof (swapent_t)) != 0 ||
copyout(swappath, st.ste_path,
strlen(swappath) + 1) != 0) {
return (EFAULT);
}
*rv = 1;
return (0);
}
beginning:
mutex_enter(&swapinfo_lock);
tmp_nswapfiles = nswapfiles;
mutex_exit(&swapinfo_lock);
if (tmp_nswapfiles < 1) {
*rv = 0;
return (0);
}
if (length < tmp_nswapfiles)
return (ENOMEM);
csip = kmem_zalloc(tmp_nswapfiles * sizeof (struct swapinfo),
KM_SLEEP);
retry:
nlen = tmp_nswapfiles * (gplen += 100);
pname = kmem_zalloc(nlen, KM_SLEEP);
mutex_enter(&swapinfo_lock);
if (tmp_nswapfiles != nswapfiles) {
mutex_exit(&swapinfo_lock);
kmem_free(pname, nlen);
kmem_free(csip,
tmp_nswapfiles * sizeof (struct swapinfo));
gplen = 0;
goto beginning;
}
for (sip = swapinfo, tsip = csip, tpname = pname, nswap = 0;
sip && nswap < tmp_nswapfiles;
sip = sip->si_next, tsip++, tpname += plen, nswap++) {
plen = sip->si_pnamelen;
if (tpname + plen - pname > nlen) {
mutex_exit(&swapinfo_lock);
kmem_free(pname, nlen);
goto retry;
}
*tsip = *sip;
tsip->si_pname = tpname;
(void) strcpy(tsip->si_pname, sip->si_pname);
}
mutex_exit(&swapinfo_lock);
if (sip) {
error = ENOMEM;
goto lout;
}
ust = (swapent_t *)((swaptbl_t *)sc_arg)->swt_ent;
for (tsip = csip, cnt = 0; cnt < nswap; tsip++, ust++, cnt++) {
if (copyin(ust, &st, sizeof (swapent_t)) != 0) {
error = EFAULT;
goto lout;
}
st.ste_flags = tsip->si_flags;
st.ste_length =
(tsip->si_eoff - tsip->si_soff) >> SCTRSHFT;
st.ste_start = tsip->si_soff >> SCTRSHFT;
st.ste_pages = tsip->si_npgs;
st.ste_free = tsip->si_nfpgs;
if (copyout(&st, ust, sizeof (swapent_t)) != 0) {
error = EFAULT;
goto lout;
}
if (!tsip->si_pnamelen)
continue;
if (copyout(tsip->si_pname, st.ste_path,
tsip->si_pnamelen) != 0) {
error = EFAULT;
goto lout;
}
}
*rv = nswap;
lout:
kmem_free(csip, tmp_nswapfiles * sizeof (struct swapinfo));
kmem_free(pname, nlen);
return (error);
case SC_ADD:
case SC_REMOVE:
break;
default:
return (EINVAL);
}
if ((error = secpolicy_swapctl(CRED())) != 0)
return (error);
if (copyin(sc_arg, &sr, sizeof (swapres_t)))
return (EFAULT);
if ((swapname = kmem_alloc(MAXPATHLEN, KM_NOSLEEP)) == NULL)
return (ENOMEM);
error = copyinstr(sr.sr_name, swapname, MAXPATHLEN, 0);
if (error)
goto out;
error = lookupname(swapname, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp);
if (error) {
if (sc_cmd == SC_ADD)
goto out;
vp = swapdel_byname(swapname, (size_t)sr.sr_start);
if (vp == NULL)
goto out;
}
if (vp->v_flag & (VNOMAP | VNOSWAP)) {
VN_RELE(vp);
error = ENOSYS;
goto out;
}
switch (vp->v_type) {
case VBLK:
break;
case VREG:
if (vp->v_vfsp && vn_is_readonly(vp))
error = EROFS;
else
error = VOP_ACCESS(vp, VREAD|VWRITE, 0, CRED(), NULL);
break;
case VDIR:
error = EISDIR;
break;
default:
error = ENOSYS;
break;
}
if (error == 0) {
if (sc_cmd == SC_REMOVE)
error = swapdel(vp, sr.sr_start);
else
error = swapadd(vp, sr.sr_start,
sr.sr_length, swapname);
}
VN_RELE(vp);
out:
kmem_free(swapname, MAXPATHLEN);
return (error);
}
#if defined(_LP64) && defined(_SYSCALL32)
int
swapctl32(int sc_cmd, void *sc_arg, int *rv)
{
struct swapinfo *sip, *csip, *tsip;
int error = 0;
struct swapent32 st, *ust;
struct swapres32 sr;
struct vnode *vp;
int cnt = 0;
int tmp_nswapfiles;
int nswap;
int length, nlen;
int gplen = 0, plen;
char *swapname;
char *pname;
char *tpname;
struct anoninfo32 ai;
size_t s;
spgcnt_t avail;
int global = INGLOBALZONE(curproc);
struct zone *zp = curproc->p_zone;
switch (sc_cmd) {
case SC_GETNSWP:
if (global)
*rv = nswapfiles;
else
*rv = 1;
return (0);
case SC_AINFO:
avail = MAX((spgcnt_t)(availrmem - swapfs_minfree), 0);
s = (k_anoninfo.ani_max + k_anoninfo.ani_mem_resv) + avail;
if (s > UINT32_MAX)
return (EOVERFLOW);
ai.ani_max = s;
set_anoninfo();
s = k_anoninfo.ani_free + avail;
if (s > UINT32_MAX)
return (EOVERFLOW);
ai.ani_free = s;
s = k_anoninfo.ani_phys_resv + k_anoninfo.ani_mem_resv;
if (s > UINT32_MAX)
return (EOVERFLOW);
ai.ani_resv = s;
if (!global && zp->zone_max_swap_ctl != UINT64_MAX) {
rctl_qty_t cap, used;
pgcnt_t pgcap, sys_avail;
mutex_enter(&zp->zone_mem_lock);
cap = zp->zone_max_swap_ctl;
used = zp->zone_max_swap;
mutex_exit(&zp->zone_mem_lock);
pgcap = MIN(btop(cap), ai.ani_max);
ai.ani_free = pgcap - btop(used);
sys_avail = ai.ani_max - ai.ani_resv;
if (sys_avail < ai.ani_free)
ai.ani_resv = pgcap - sys_avail;
else
ai.ani_resv = btop(used);
ai.ani_max = pgcap;
}
if (copyout(&ai, sc_arg, sizeof (ai)) != 0)
return (EFAULT);
return (0);
case SC_LIST:
if (copyin(sc_arg, &length, sizeof (int32_t)) != 0)
return (EFAULT);
if (!global) {
struct swapent32 st;
char *swappath = "swap";
if (length < 1)
return (ENOMEM);
ust = (swapent32_t *)((swaptbl32_t *)sc_arg)->swt_ent;
if (copyin(ust, &st, sizeof (swapent32_t)) != 0)
return (EFAULT);
st.ste_start = PAGESIZE >> SCTRSHFT;
st.ste_length = (off_t)0;
st.ste_pages = 0;
st.ste_free = 0;
st.ste_flags = 0;
mutex_enter(&swapinfo_lock);
for (sip = swapinfo, nswap = 0;
sip != NULL && nswap < nswapfiles;
sip = sip->si_next, nswap++) {
st.ste_length +=
(sip->si_eoff - sip->si_soff) >> SCTRSHFT;
st.ste_pages += sip->si_npgs;
st.ste_free += sip->si_nfpgs;
}
mutex_exit(&swapinfo_lock);
if (zp->zone_max_swap_ctl != UINT64_MAX) {
rctl_qty_t cap, used;
mutex_enter(&zp->zone_mem_lock);
cap = zp->zone_max_swap_ctl;
used = zp->zone_max_swap;
mutex_exit(&zp->zone_mem_lock);
st.ste_length = MIN(cap, st.ste_length);
st.ste_pages = MIN(btop(cap), st.ste_pages);
st.ste_free = MIN(st.ste_pages - btop(used),
st.ste_free);
}
if (copyout(&st, ust, sizeof (swapent32_t)) != 0 ||
copyout(swappath, (caddr_t)(uintptr_t)st.ste_path,
strlen(swappath) + 1) != 0) {
return (EFAULT);
}
*rv = 1;
return (0);
}
beginning:
mutex_enter(&swapinfo_lock);
tmp_nswapfiles = nswapfiles;
mutex_exit(&swapinfo_lock);
if (tmp_nswapfiles < 1) {
*rv = 0;
return (0);
}
if (length < tmp_nswapfiles)
return (ENOMEM);
csip = kmem_zalloc(tmp_nswapfiles * sizeof (*csip), KM_SLEEP);
retry:
nlen = tmp_nswapfiles * (gplen += 100);
pname = kmem_zalloc(nlen, KM_SLEEP);
mutex_enter(&swapinfo_lock);
if (tmp_nswapfiles != nswapfiles) {
mutex_exit(&swapinfo_lock);
kmem_free(pname, nlen);
kmem_free(csip, tmp_nswapfiles * sizeof (*csip));
gplen = 0;
goto beginning;
}
for (sip = swapinfo, tsip = csip, tpname = pname, nswap = 0;
(sip != NULL) && (nswap < tmp_nswapfiles);
sip = sip->si_next, tsip++, tpname += plen, nswap++) {
plen = sip->si_pnamelen;
if (tpname + plen - pname > nlen) {
mutex_exit(&swapinfo_lock);
kmem_free(pname, nlen);
goto retry;
}
*tsip = *sip;
tsip->si_pname = tpname;
(void) strcpy(tsip->si_pname, sip->si_pname);
}
mutex_exit(&swapinfo_lock);
if (sip != NULL) {
error = ENOMEM;
goto lout;
}
ust = (swapent32_t *)((swaptbl32_t *)sc_arg)->swt_ent;
for (tsip = csip, cnt = 0; cnt < nswap; tsip++, ust++, cnt++) {
if (copyin(ust, &st, sizeof (*ust)) != 0) {
error = EFAULT;
goto lout;
}
st.ste_flags = tsip->si_flags;
st.ste_length =
(tsip->si_eoff - tsip->si_soff) >> SCTRSHFT;
st.ste_start = tsip->si_soff >> SCTRSHFT;
st.ste_pages = tsip->si_npgs;
st.ste_free = tsip->si_nfpgs;
if (copyout(&st, ust, sizeof (st)) != 0) {
error = EFAULT;
goto lout;
}
if (!tsip->si_pnamelen)
continue;
if (copyout(tsip->si_pname,
(caddr_t)(uintptr_t)st.ste_path,
tsip->si_pnamelen) != 0) {
error = EFAULT;
goto lout;
}
}
*rv = nswap;
lout:
kmem_free(csip, tmp_nswapfiles * sizeof (*csip));
kmem_free(pname, nlen);
return (error);
case SC_ADD:
case SC_REMOVE:
break;
default:
return (EINVAL);
}
if ((error = secpolicy_swapctl(CRED())) != 0)
return (error);
if (copyin(sc_arg, &sr, sizeof (sr)))
return (EFAULT);
if ((swapname = kmem_alloc(MAXPATHLEN, KM_NOSLEEP)) == NULL)
return (ENOMEM);
error = copyinstr((caddr_t)(uintptr_t)sr.sr_name,
swapname, MAXPATHLEN, NULL);
if (error)
goto out;
error = lookupname(swapname, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp);
if (error) {
if (sc_cmd == SC_ADD)
goto out;
vp = swapdel_byname(swapname, (uint_t)sr.sr_start);
if (vp == NULL)
goto out;
}
if (vp->v_flag & (VNOMAP | VNOSWAP)) {
VN_RELE(vp);
error = ENOSYS;
goto out;
}
switch (vp->v_type) {
case VBLK:
break;
case VREG:
if (vp->v_vfsp && vn_is_readonly(vp))
error = EROFS;
else
error = VOP_ACCESS(vp, VREAD|VWRITE, 0, CRED(), NULL);
break;
case VDIR:
error = EISDIR;
break;
default:
error = ENOSYS;
break;
}
if (error == 0) {
if (sc_cmd == SC_REMOVE)
error = swapdel(vp, sr.sr_start);
else
error = swapadd(vp, sr.sr_start, sr.sr_length,
swapname);
}
VN_RELE(vp);
out:
kmem_free(swapname, MAXPATHLEN);
return (error);
}
#endif
int
swapadd(struct vnode *vp, ulong_t lowblk, ulong_t nblks, char *swapname)
{
struct swapinfo **sipp, *nsip = NULL, *esip = NULL;
struct vnode *cvp;
struct vattr vattr;
pgcnt_t pages;
u_offset_t soff, eoff;
int error;
ssize_t i, start, end;
ushort_t wasswap;
ulong_t startblk;
size_t returned_mem;
SWAP_PRINT(SW_CTL, "swapadd: vp %p lowblk %ld nblks %ld swapname %s\n",
vp, lowblk, nblks, swapname, 0);
cvp = common_specvp(vp);
mutex_enter(&cvp->v_lock);
wasswap = cvp->v_flag & VISSWAP;
cvp->v_flag |= VISSWAP;
mutex_exit(&cvp->v_lock);
mutex_enter(&swap_lock);
if (error = VOP_OPEN(&cvp, FREAD|FWRITE, CRED(), NULL)) {
mutex_exit(&swap_lock);
if (!wasswap) {
mutex_enter(&cvp->v_lock);
cvp->v_flag &= ~VISSWAP;
mutex_exit(&cvp->v_lock);
}
return (error);
}
mutex_exit(&swap_lock);
vattr.va_mask = AT_SIZE;
if (error = VOP_GETATTR(cvp, &vattr, ATTR_COMM, CRED(), NULL))
goto out;
if ((vattr.va_size == 0) || (vattr.va_size == MAXOFFSET_T)) {
error = EINVAL;
goto out;
}
#ifdef _ILP32
if (vattr.va_size > MAXOFF32_T) {
cmn_err(CE_NOTE,
"!swap device %s truncated from 0x%llx to 0x%x bytes",
swapname, vattr.va_size, MAXOFF32_T);
vattr.va_size = MAXOFF32_T;
}
#endif
vattr.va_mask = AT_SIZE;
if (error = VOP_SETATTR(cvp, &vattr, 0, CRED(), NULL))
goto out;
error = VOP_PAGEIO(cvp, (page_t *)NULL, (u_offset_t)0, 0, 0, CRED(),
NULL);
if (error == ENOSYS)
goto out;
else
error = 0;
if (cvp == rootdir)
startblk = roundup(MINIROOTSIZE<<SCTRSHFT, klustsize)>>SCTRSHFT;
else
startblk = (ulong_t)(lowblk ? lowblk : 1);
soff = startblk << SCTRSHFT;
if (soff >= vattr.va_size) {
error = EINVAL;
goto out;
}
eoff = nblks ? soff + (nblks - (startblk - lowblk) << SCTRSHFT) :
vattr.va_size;
SWAP_PRINT(SW_CTL, "swapadd: va_size %ld soff %ld eoff %ld\n",
vattr.va_size, soff, eoff, 0, 0);
if (eoff > vattr.va_size) {
error = EINVAL;
goto out;
}
soff = ptob(btopr(soff));
eoff = ptob(btop(eoff));
if (soff >= eoff) {
SWAP_PRINT(SW_CTL, "swapadd: soff %ld >= eoff %ld\n",
soff, eoff, 0, 0, 0);
error = EINVAL;
goto out;
}
pages = btop(eoff - soff);
nsip = kmem_zalloc(sizeof (struct swapinfo), KM_SLEEP);
nsip->si_vp = cvp;
nsip->si_soff = soff;
nsip->si_eoff = eoff;
nsip->si_hint = 0;
nsip->si_checkcnt = nsip->si_alloccnt = 0;
nsip->si_pnamelen = (int)strlen(swapname) + 1;
nsip->si_pname = (char *)kmem_zalloc(nsip->si_pnamelen, KM_SLEEP);
bcopy(swapname, nsip->si_pname, nsip->si_pnamelen - 1);
SWAP_PRINT(SW_CTL, "swapadd: allocating swapinfo for %s, %ld pages\n",
swapname, pages, 0, 0, 0);
nsip->si_mapsize = P2ROUNDUP(pages, NBBW) / NBBY;
nsip->si_swapslots = kmem_zalloc(nsip->si_mapsize, KM_SLEEP);
start = pages;
end = P2ROUNDUP(pages, NBBW);
for (i = start; i < end; i++) {
SWAP_PRINT(SW_CTL, "swapadd: set bit for page %ld\n", i,
0, 0, 0, 0);
SETBIT(nsip->si_swapslots, i);
}
nsip->si_npgs = nsip->si_nfpgs = pages;
mutex_enter(&swapinfo_lock);
for (sipp = &swapinfo; (esip = *sipp) != NULL; sipp = &esip->si_next) {
if (esip->si_vp == cvp) {
if (esip->si_soff == soff && esip->si_npgs == pages &&
(esip->si_flags & ST_DOINGDEL)) {
esip->si_flags &= ~ST_DOINGDEL;
mutex_exit(&swapinfo_lock);
goto out;
}
if ((soff < esip->si_eoff) && (eoff > esip->si_soff)) {
error = EEXIST;
mutex_exit(&swapinfo_lock);
goto out;
}
}
}
nswapfiles++;
*sipp = nsip;
silast = nsip;
mutex_enter(&anoninfo_lock);
ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap);
ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv);
k_anoninfo.ani_max += pages;
ANI_ADD(pages);
if (k_anoninfo.ani_mem_resv > k_anoninfo.ani_locked_swap) {
returned_mem = MIN(k_anoninfo.ani_mem_resv -
k_anoninfo.ani_locked_swap,
k_anoninfo.ani_max - k_anoninfo.ani_phys_resv);
ANI_ADD(-returned_mem);
k_anoninfo.ani_free -= returned_mem;
k_anoninfo.ani_mem_resv -= returned_mem;
k_anoninfo.ani_phys_resv += returned_mem;
mutex_enter(&freemem_lock);
availrmem += returned_mem;
mutex_exit(&freemem_lock);
}
if (swapfs_minfree < swapfs_desfree) {
mutex_enter(&freemem_lock);
if (availrmem > swapfs_desfree || !k_anoninfo.ani_mem_resv)
swapfs_minfree = swapfs_desfree;
mutex_exit(&freemem_lock);
}
SWAP_PRINT(SW_CTL, "swapadd: ani_max %ld ani_free %ld\n",
k_anoninfo.ani_free, k_anoninfo.ani_free, 0, 0, 0);
mutex_exit(&anoninfo_lock);
mutex_exit(&swapinfo_lock);
mutex_enter(&dump_lock);
if (dumpvp == NULL)
(void) dumpinit(vp, swapname, 0);
mutex_exit(&dump_lock);
VN_HOLD(cvp);
out:
if (error || esip) {
SWAP_PRINT(SW_CTL, "swapadd: error (%d)\n", error, 0, 0, 0, 0);
if (!wasswap) {
mutex_enter(&cvp->v_lock);
cvp->v_flag &= ~VISSWAP;
mutex_exit(&cvp->v_lock);
}
if (nsip) {
kmem_free(nsip->si_swapslots, (size_t)nsip->si_mapsize);
kmem_free(nsip->si_pname, nsip->si_pnamelen);
kmem_free(nsip, sizeof (*nsip));
}
mutex_enter(&swap_lock);
(void) VOP_CLOSE(cvp, FREAD|FWRITE, 1, (offset_t)0, CRED(),
NULL);
mutex_exit(&swap_lock);
}
return (error);
}
static int
swapdel(
struct vnode *vp,
ulong_t lowblk)
{
struct swapinfo **sipp, *osip = NULL;
struct vnode *cvp;
u_offset_t soff;
int error = 0;
u_offset_t toff = 0;
struct vnode *tvp = NULL;
spgcnt_t pages;
struct anon **app, *ap;
kmutex_t *ahm;
pgcnt_t adjust_swap = 0;
cvp = common_specvp(vp);
lowblk = lowblk ? lowblk : 1;
soff = ptob(btopr(lowblk << SCTRSHFT));
mutex_enter(&swapinfo_lock);
for (sipp = &swapinfo; (osip = *sipp) != NULL; sipp = &osip->si_next) {
if ((osip->si_vp == cvp) &&
(osip->si_soff == soff) && (osip->si_flags == 0))
break;
}
if (osip == NULL) {
error = EINVAL;
mutex_exit(&swapinfo_lock);
goto out;
}
pages = osip->si_npgs;
mutex_enter(&anoninfo_lock);
ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap);
ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv);
mutex_enter(&freemem_lock);
if (((k_anoninfo.ani_max - k_anoninfo.ani_phys_resv) +
MAX((spgcnt_t)(availrmem - swapfs_minfree), 0)) < pages) {
mutex_exit(&freemem_lock);
mutex_exit(&anoninfo_lock);
error = ENOMEM;
cmn_err(CE_WARN, "swapdel - too few free pages");
mutex_exit(&swapinfo_lock);
goto out;
}
mutex_exit(&freemem_lock);
k_anoninfo.ani_max -= pages;
if (k_anoninfo.ani_phys_resv > k_anoninfo.ani_max) {
adjust_swap = k_anoninfo.ani_phys_resv - k_anoninfo.ani_max;
k_anoninfo.ani_phys_resv -= adjust_swap;
k_anoninfo.ani_mem_resv += adjust_swap;
mutex_enter(&freemem_lock);
availrmem -= adjust_swap;
mutex_exit(&freemem_lock);
ANI_ADD(adjust_swap);
}
ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap);
ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv);
mutex_exit(&anoninfo_lock);
ANI_ADD(-pages);
osip->si_flags |= ST_INDEL|ST_DOINGDEL;
mutex_exit(&swapinfo_lock);
for (app = anon_hash; app < &anon_hash[ANON_HASH_SIZE]; app++) {
ahm = &anonhash_lock[(app - anon_hash) &
(AH_LOCK_SIZE - 1)].pad_mutex;
mutex_enter(ahm);
top:
for (ap = *app; ap != NULL; ap = ap->an_hash) {
if (ap->an_pvp == cvp &&
ap->an_poff >= osip->si_soff &&
ap->an_poff < osip->si_eoff) {
ASSERT(TESTBIT(osip->si_swapslots,
btop((size_t)(ap->an_poff -
osip->si_soff))));
tvp = ap->an_vp;
toff = ap->an_off;
VN_HOLD(tvp);
mutex_exit(ahm);
error = swapslot_free(tvp, toff, osip);
VN_RELE(tvp);
mutex_enter(ahm);
if (!error && (osip->si_flags & ST_DOINGDEL)) {
goto top;
} else {
if (error) {
cmn_err(CE_WARN,
"swapslot_free failed %d",
error);
}
mutex_enter(&swapinfo_lock);
osip->si_flags &=
~(ST_INDEL | ST_DOINGDEL);
mutex_exit(&swapinfo_lock);
mutex_enter(&anoninfo_lock);
k_anoninfo.ani_phys_resv += adjust_swap;
k_anoninfo.ani_mem_resv -= adjust_swap;
k_anoninfo.ani_max += pages;
mutex_enter(&freemem_lock);
availrmem += adjust_swap;
mutex_exit(&freemem_lock);
mutex_exit(&anoninfo_lock);
ANI_ADD(pages);
mutex_exit(ahm);
goto out;
}
}
}
mutex_exit(ahm);
}
mutex_enter(&swapinfo_lock);
ASSERT(osip->si_nfpgs == osip->si_npgs);
for (sipp = &swapinfo; *sipp != NULL; sipp = &(*sipp)->si_next) {
if (*sipp == osip)
break;
}
ASSERT(*sipp);
*sipp = osip->si_next;
if (silast == osip)
if ((silast = osip->si_next) == NULL)
silast = swapinfo;
nswapfiles--;
mutex_exit(&swapinfo_lock);
kmem_free(osip->si_swapslots, osip->si_mapsize);
kmem_free(osip->si_pname, osip->si_pnamelen);
kmem_free(osip, sizeof (*osip));
mutex_enter(&dump_lock);
if (cvp == dumpvp)
dumpfini();
mutex_exit(&dump_lock);
mutex_enter(&swap_lock);
(void) VOP_CLOSE(cvp, FREAD|FWRITE, 1, (offset_t)0, CRED(), NULL);
mutex_enter(&cvp->v_lock);
cvp->v_flag &= ~VISSWAP;
mutex_exit(&cvp->v_lock);
VN_RELE(cvp);
mutex_exit(&swap_lock);
out:
return (error);
}
static int
swapslot_free(
struct vnode *vp,
u_offset_t off,
struct swapinfo *sip)
{
struct page *pp = NULL;
struct anon *ap = NULL;
int error = 0;
kmutex_t *ahm;
struct vnode *pvp = NULL;
u_offset_t poff;
int alloc_pg = 0;
ASSERT(sip->si_vp != NULL);
again:
if ((pp = page_lookup(vp, off, SE_SHARED)) == NULL) {
pp = page_create_va(vp, off, PAGESIZE, PG_WAIT | PG_EXCL,
segkmap, NULL);
if (pp == NULL)
goto again;
alloc_pg = 1;
error = swap_getphysname(vp, off, &pvp, &poff);
if (error || pvp != sip->si_vp || poff < sip->si_soff ||
poff >= sip->si_eoff) {
page_io_unlock(pp);
VN_DISPOSE(pp, B_INVAL, 0, kcred);
return (0);
}
error = VOP_PAGEIO(pvp, pp, poff, PAGESIZE, B_READ,
CRED(), NULL);
if (error) {
page_io_unlock(pp);
if (error == EFAULT)
error = 0;
VN_DISPOSE(pp, B_INVAL, 0, kcred);
return (error);
}
}
if (!alloc_pg)
page_io_lock(pp);
ahm = AH_MUTEX(vp, off);
mutex_enter(ahm);
ap = swap_anon(vp, off);
if ((ap == NULL || ap->an_pvp == NULL) && alloc_pg) {
mutex_exit(ahm);
page_io_unlock(pp);
VN_DISPOSE(pp, B_INVAL, 0, kcred);
return (0);
}
if ((ap != NULL) && (ap->an_pvp == sip->si_vp && ap->an_poff >=
sip->si_soff && ap->an_poff < sip->si_eoff)) {
swap_phys_free(ap->an_pvp, ap->an_poff, PAGESIZE);
ap->an_pvp = NULL;
ap->an_poff = 0;
mutex_exit(ahm);
hat_setmod(pp);
} else {
mutex_exit(ahm);
}
page_io_unlock(pp);
page_unlock(pp);
return (0);
}
int
swap_newphysname(
struct vnode *vp,
u_offset_t offset,
u_offset_t *offp,
size_t *lenp,
struct vnode **pvpp,
u_offset_t *poffp)
{
struct anon *ap = NULL;
int error = 0;
struct vnode *pvp;
u_offset_t poff, pstart, prem;
size_t plen;
u_offset_t off, start;
kmutex_t *ahm;
ASSERT(*offp <= offset && offset < *offp + *lenp);
plen = *lenp;
if (!swap_phys_alloc(&pvp, &pstart, &plen, 0)) {
ahm = AH_MUTEX(vp, offset);
mutex_enter(ahm);
if ((ap = swap_anon(vp, offset)) == NULL) {
error = SE_NOANON;
mutex_exit(ahm);
return (error);
}
error = (ap->an_pvp ? 0 : SE_NOSWAP);
*offp = offset;
*lenp = PAGESIZE;
*pvpp = ap->an_pvp;
*poffp = ap->an_poff;
mutex_exit(ahm);
return (error);
}
start = MAX(*offp,
(offset + PAGESIZE > plen) ? (offset + PAGESIZE - plen) : 0);
ASSERT(start + plen <= *offp + *lenp);
for (off = start, poff = pstart; poff < pstart + plen;
off += PAGESIZE, poff += PAGESIZE) {
ahm = AH_MUTEX(vp, off);
mutex_enter(ahm);
if ((ap = swap_anon(vp, off)) != NULL) {
if (ap->an_pvp)
swap_phys_free(ap->an_pvp, ap->an_poff,
PAGESIZE);
ap->an_pvp = pvp;
ap->an_poff = poff;
} else {
prem = (pstart + plen) - poff;
if (off > offset) {
plen = poff - pstart;
error = 0;
} else if (off == offset) {
error = SE_NOANON;
} else if ((ap = swap_anon(vp, offset)) == NULL) {
error = SE_NOANON;
} else {
if (ap->an_pvp)
swap_phys_free(ap->an_pvp, ap->an_poff,
PAGESIZE);
ap->an_pvp = pvp;
ap->an_poff = poff;
start = offset;
plen = PAGESIZE;
pstart = poff;
poff += PAGESIZE;
prem -= PAGESIZE;
}
swap_phys_free(pvp, poff, prem);
mutex_exit(ahm);
break;
}
mutex_exit(ahm);
}
ASSERT(*offp <= start && start + plen <= *offp + *lenp);
ASSERT(start <= offset && offset < start + plen);
*offp = start;
*lenp = plen;
*pvpp = pvp;
*poffp = pstart;
return (error);
}
int
swap_getphysname(
struct vnode *vp,
u_offset_t off,
struct vnode **pvpp,
u_offset_t *poffp)
{
struct anon *ap;
int error = 0;
kmutex_t *ahm;
ahm = AH_MUTEX(vp, off);
mutex_enter(ahm);
ap = swap_anon(vp, off);
if (ap == NULL) {
error = EIDRM;
goto out;
}
*pvpp = ap->an_pvp;
*poffp = ap->an_poff;
out:
mutex_exit(ahm);
return (error);
}