devswap: improve setswapchan()

- check for unusable file types like directories and append-only files.
- we should eigther error without any side effects or succeed.
This commit is contained in:
cinap_lenrek 2021-10-23 15:12:27 +00:00
parent db971a6189
commit 1180631421

View file

@ -383,16 +383,15 @@ needpages(void*)
static void static void
setswapchan(Chan *c) setswapchan(Chan *c)
{ {
uvlong s;
if(waserror()){ if(waserror()){
cclose(c); cclose(c);
nexterror(); nexterror();
} }
if(swapimage.c != nil) {
if(swapalloc.free != conf.nswap) if(c->qid.type & (QTDIR|QTAPPEND|QTAUTH))
error(Einuse); error(Ebadarg);
cclose(swapimage.c);
swapimage.c = nil;
}
/* /*
* if this isn't a file, set the swap space * if this isn't a file, set the swap space
@ -400,17 +399,28 @@ setswapchan(Chan *c)
*/ */
if(devtab[c->type]->dc != L'M'){ if(devtab[c->type]->dc != L'M'){
Dir *d = dirchanstat(c); Dir *d = dirchanstat(c);
if(d->length < (vlong)conf.nswppo*BY2PG){ s = d->length / BY2PG;
free(d);
error("swap device too small");
}
if(d->length < (vlong)conf.nswap*BY2PG){
conf.nswap = d->length/BY2PG;
swapalloc.top = &swapalloc.swmap[conf.nswap];
swapalloc.free = conf.nswap;
}
free(d); free(d);
} else {
s = conf.nswap;
} }
if(s < conf.nswppo)
error("swap device too small");
if(swapimage.c != nil) {
if(swapalloc.free != conf.nswap)
error(Einuse);
cclose(swapimage.c);
swapimage.c = nil;
}
if(s < conf.nswap){
conf.nswap = s;
swapalloc.top = &swapalloc.swmap[conf.nswap];
swapalloc.free = conf.nswap;
}
c->flag &= ~CCACHE; c->flag &= ~CCACHE;
cclunk(c); cclunk(c);
poperror(); poperror();