--- ray/src/hd/holofile.c 1999/03/09 08:40:11 3.39 +++ ray/src/hd/holofile.c 2003/10/22 02:06:34 3.54 @@ -1,22 +1,25 @@ -/* Copyright (c) 1999 Silicon Graphics, Inc. */ - #ifndef lint -static char SCCSid[] = "$SunId$ SGI"; +static const char RCSid[] = "$Id: holofile.c,v 3.54 2003/10/22 02:06:34 greg Exp $"; #endif - /* * Routines for managing holodeck files * * 9/30/97 GWLarson */ +#include "copyright.h" + +#include + +#include "platform.h" + #include "holo.h" #ifndef CACHESIZE -#ifdef BIGMEM -#define CACHESIZE 17 /* default cache size (Mbytes, 0==inf) */ -#else +#ifdef SMLMEM #define CACHESIZE 5 +#else +#define CACHESIZE 17 /* default cache size (Mbytes, 0==inf) */ #endif #endif #ifndef FREEBEAMS @@ -52,15 +55,49 @@ HOLO *hdlist[HDMAX+1]; /* holodeck pointers (NULL term static struct fraglist { short nlinks; /* number of holodeck sections using us */ - short writerr; /* write error encountered */ + short writable; /* 0 read-only, <0 write error encountered */ int nfrags; /* number of known fragments */ BEAMI *fi; /* fragments, descending file position */ - long flen; /* last known file length */ + off_t flen; /* last known file length */ } *hdfragl; /* fragment lists, indexed by file descriptor */ static int nhdfragls; /* size of hdfragl array */ +HOLO * +hdalloc(hproto) /* allocate and set holodeck section based on grid */ +HDGRID *hproto; +{ + HOLO hdhead; + register HOLO *hp; + int n; + /* copy grid to temporary header */ + memcpy((void *)&hdhead, (void *)hproto, sizeof(HDGRID)); + /* compute grid vectors and sizes */ + hdcompgrid(&hdhead); + /* allocate header with directory */ + n = sizeof(HOLO)+nbeams(&hdhead)*sizeof(BEAMI); + if ((hp = (HOLO *)malloc(n)) == NULL) + return(NULL); + /* copy header information */ + *hp = hdhead; + /* allocate and clear beam list */ + hp->bl = (BEAM **)malloc((nbeams(hp)+1)*sizeof(BEAM *)+sizeof(BEAM)); + if (hp->bl == NULL) { + free((void *)hp); + return(NULL); + } + memset((void *)hp->bl, '\0', (nbeams(hp)+1)*sizeof(BEAM *)+sizeof(BEAM)); + hp->bl[0] = (BEAM *)(hp->bl+nbeams(hp)+1); /* set blglob(hp) */ + hp->fd = -1; + hp->dirty = 0; + hp->priv = NULL; + /* clear beam directory */ + memset((void *)hp->bi, '\0', (nbeams(hp)+1)*sizeof(BEAMI)); + return(hp); /* all is well */ +} + + char * hdrealloc(ptr, siz, rout) /* (re)allocate memory, retry then error */ char *ptr; @@ -70,7 +107,7 @@ char *rout; register char *newp; /* call malloc/realloc */ if (ptr == NULL) newp = (char *)malloc(siz); - else newp = (char *)realloc(ptr, siz); + else newp = (char *)realloc((void *)ptr, siz); /* check success */ if (newp == NULL && rout != NULL) { hdfreecache(25, NULL); /* free some memory */ @@ -85,18 +122,21 @@ char *rout; } -hdattach(fd) /* start tracking file fragments for some section */ +hdattach(fd, wr) /* start tracking file fragments for some section */ register int fd; +int wr; { if (fd >= nhdfragls) { hdfragl = (struct fraglist *)hdrealloc((char *)hdfragl, (fd+1)*sizeof(struct fraglist), "hdattach"); - bzero((char *)(hdfragl+nhdfragls), - (fd+1-nhdfragls)*sizeof(struct fraglist)); + memset((void *)(hdfragl+nhdfragls), + '\0', (fd+1-nhdfragls)*sizeof(struct fraglist)); nhdfragls = fd+1; } hdfragl[fd].nlinks++; - hdfragl[fd].flen = lseek(fd, 0L, 2); /* get file length */ + hdfragl[fd].writable = wr; /* set writable flag */ + /* get file length */ + hdfragl[fd].flen = lseek(fd, (off_t)0, SEEK_END); } @@ -106,10 +146,10 @@ register int fd; hdrelease(fd) /* stop tracking file fragments for some section */ register int fd; { - if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks) + if ((fd < 0) | (fd >= nhdfragls) || !hdfragl[fd].nlinks) return; if (!--hdfragl[fd].nlinks && hdfragl[fd].nfrags) { - free((char *)hdfragl[fd].fi); + free((void *)hdfragl[fd].fi); hdfragl[fd].fi = NULL; hdfragl[fd].nfrags = 0; } @@ -121,13 +161,14 @@ hdinit(fd, hproto) /* initialize a holodeck section in int fd; /* corresponding file descriptor */ HDGRID *hproto; /* holodeck section grid */ { - long rtrunc; - long fpos; + off_t rtrunc; + off_t fpos; + int writable; register HOLO *hp; register int n; /* prepare for system errors */ errno = 0; - if ((fpos = lseek(fd, 0L, 1)) < 0) + if ((fpos = lseek(fd, (off_t)0, SEEK_CUR)) < 0) error(SYSTEM, "cannot determine holodeck file position"); if (hproto == NULL) { /* assume we're loading it */ HDGRID hpr; @@ -148,7 +189,14 @@ HDGRID *hproto; /* holodeck section grid */ error(WARNING, "dirty holodeck section"); break; } - } else { /* assume we're creating it */ + /* check writability */ + if (fd < nhdfragls && hdfragl[fd].nlinks) + writable = hdfragl[fd].writable; + else + writable = lseek(fd, fpos, SEEK_SET) == fpos && + write(fd, (char *)hp, sizeof(HDGRID)) == + sizeof(HDGRID); + } else { /* else assume we're creating it */ if ((hp = hdalloc(hproto)) == NULL) goto memerr; /* write header and skeleton */ @@ -157,22 +205,24 @@ HDGRID *hproto; /* holodeck section grid */ sizeof(HDGRID) || write(fd, (char *)(hp->bi+1), n) != n) error(SYSTEM, "cannot write header to holodeck file"); + writable = 1; } hp->fd = fd; hp->dirty = 0; biglob(hp)->fo = fpos + sizeof(HDGRID); /* start tracking fragments */ - hdattach(fd); + hdattach(fd, writable); /* check rays on disk */ fpos = hdfilen(fd); biglob(hp)->nrd = rtrunc = 0; for (n = hproto == NULL ? nbeams(hp) : 0; n > 0; n--) - if (hp->bi[n].nrd) + if (hp->bi[n].nrd) { if (hp->bi[n].fo+hp->bi[n].nrd*sizeof(RAYVAL) > fpos) { rtrunc += hp->bi[n].nrd; hp->bi[n].nrd = 0; } else biglob(hp)->nrd += hp->bi[n].nrd; + } if (rtrunc) { sprintf(errmsg, "truncated section, %ld rays lost (%.1f%%)", rtrunc, 100.*rtrunc/(rtrunc+biglob(hp)->nrd)); @@ -200,8 +250,9 @@ int i; register int j; if (!hp->dirty++) { /* write smudge first time */ - if (lseek(hp->fd, biglob(hp)->fo+(i-1)*sizeof(BEAMI), 0) < 0 - || write(hp->fd, (char *)&smudge, + if (lseek(hp->fd, biglob(hp)->fo+(i-1)*sizeof(BEAMI), + SEEK_SET) < 0 || + write(hp->fd, (char *)&smudge, sizeof(BEAMI)) != sizeof(BEAMI)) error(SYSTEM, "seek/write error in hdmarkdirty"); hp->dirseg[0].s = i; @@ -215,7 +266,7 @@ int i; hp->dirseg[j].n = 1; break; } - copystruct(hp->dirseg+j, hp->dirseg+(j-1)); + *(hp->dirseg+j) = *(hp->dirseg+(j-1)); } do { /* check neighbors */ mindist = nbeams(hp); /* find closest */ @@ -237,7 +288,7 @@ int i; hp->dirseg[minpos].n - hp->dirseg[j].s; hp->dirty--; while (++j < hp->dirty) /* close the gap */ - copystruct(hp->dirseg+j, hp->dirseg+(j+1)); + *(hp->dirseg+j) = *(hp->dirseg+(j+1)); } while (mindist <= MINDIRSEL); } @@ -264,7 +315,7 @@ int all; errno = 0; /* write dirty segments */ for (j = 0; j < hp->dirty; j++) { if (lseek(hp->fd, biglob(hp)->fo + - (hp->dirseg[j].s-1)*sizeof(BEAMI), 0) < 0) + (hp->dirseg[j].s-1)*sizeof(BEAMI), SEEK_SET) < 0) error(SYSTEM, "cannot seek on holodeck file"); n = hp->dirseg[j].n * sizeof(BEAMI); if (write(hp->fd, (char *)(hp->bi+hp->dirseg[j].s), n) != n) @@ -304,31 +355,31 @@ int all; /* include overhead (painful) */ } -long +off_t hdfilen(fd) /* return file length for fd */ int fd; { - long fpos, flen; + off_t fpos, flen; if (fd < 0) return(-1); if (fd >= nhdfragls || !hdfragl[fd].nlinks) { - if ((fpos = lseek(fd, 0L, 1)) < 0) + if ((fpos = lseek(fd, (off_t)0, SEEK_CUR)) < 0) return(-1); - flen = lseek(fd, 0L, 2); - lseek(fd, fpos, 0); + flen = lseek(fd, (off_t)0, SEEK_END); + lseek(fd, fpos, SEEK_SET); return(flen); } return(hdfragl[fd].flen); } -long +off_t hdfiluse(fd, all) /* compute file usage (in bytes) */ int fd; /* open file descriptor to check */ int all; /* include overhead and unflushed data */ { - long total = 0; + off_t total = 0; register int i, j; for (j = 0; hdlist[j] != NULL; j++) { @@ -359,7 +410,7 @@ int nr; /* number of new rays desired */ int n; if (nr <= 0) return(NULL); - CHECK(i < 1 | i > nbeams(hp), + CHECK((i < 1) | (i > nbeams(hp)), CONSISTENCY, "bad beam index given to hdnewrays"); if (hp->bl[i] != NULL) hp->bl[i]->tick = hdclock; /* preempt swap */ @@ -371,7 +422,7 @@ int nr; /* number of new rays desired */ blglob(hp)->nrm += n; if ((n = hp->bl[i]->nrm = hp->bi[i].nrd)) { errno = 0; - if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) + if (lseek(hp->fd, hp->bi[i].fo, SEEK_SET) < 0) error(SYSTEM, "seek error on holodeck file"); n *= sizeof(RAYVAL); if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) @@ -387,7 +438,7 @@ int nr; /* number of new rays desired */ hdfreefrag(hp, i); /* relinquish old fragment */ p = hdbray(hp->bl[i]) + hp->bl[i]->nrm; hp->bl[i]->nrm += nr; /* update in-core structure */ - bzero((char *)p, nr*sizeof(RAYVAL)); + memset((void *)p, '\0', nr*sizeof(RAYVAL)); blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */ return(p); /* point to new rays */ } @@ -400,7 +451,7 @@ register int i; { register int n; - CHECK(i < 1 | i > nbeams(hp), + CHECK((i < 1) | (i > nbeams(hp)), CONSISTENCY, "bad beam index given to hdgetbeam"); if (hp->bl[i] == NULL) { /* load from disk */ if (!(n = hp->bi[i].nrd)) @@ -410,7 +461,7 @@ register int i; hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdgetbeam"); blglob(hp)->nrm += hp->bl[i]->nrm = n; errno = 0; - if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) + if (lseek(hp->fd, hp->bi[i].fo, SEEK_SET) < 0) error(SYSTEM, "seek error on holodeck file"); n *= sizeof(RAYVAL); if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) @@ -427,15 +478,15 @@ int hdfilord(hb1, hb2) /* order beams for quick loading */ register HDBEAMI *hb1, *hb2; { - register long c; + register off_t c; /* residents go first */ if (hb2->h->bl[hb2->b] != NULL) return(hb1->h->bl[hb1->b] == NULL); if (hb1->h->bl[hb1->b] != NULL) return(-1); /* otherwise sort by file descriptor */ - if ((c = hb1->h->fd - hb2->h->fd)) - return(c); + if (hb1->h->fd != hb2->h->fd) + return(hb1->h->fd - hb2->h->fd); /* then by position in file */ c = hb1->h->bi[hb1->b].fo - hb2->h->bi[hb2->b].fo; return(c > 0 ? 1 : c < 0 ? -1 : 0); @@ -445,7 +496,7 @@ register HDBEAMI *hb1, *hb2; hdloadbeams(hb, n, bf) /* load a list of beams in optimal order */ register HDBEAMI *hb; /* list gets sorted by hdfilord() */ int n; /* list length */ -int (*bf)(); /* callback function (optional) */ +void (*bf)(); /* callback function (optional) */ { unsigned origcachesize, memuse; int bytesloaded, needbytes, bytes2free; @@ -454,10 +505,10 @@ int (*bf)(); /* callback function (optional) */ /* precheck consistency */ if (n <= 0) return; for (i = n; i--; ) - if (hb[i].h==NULL || hb[i].b<1 | hb[i].b>nbeams(hb[i].h)) + if (hb[i].h==NULL || (hb[i].b<1) | (hb[i].b>nbeams(hb[i].h))) error(CONSISTENCY, "bad beam in hdloadbeams"); /* sort list for optimal access */ - qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord); + qsort((void *)hb, n, sizeof(HDBEAMI), hdfilord); bytesloaded = 0; /* run through loaded beams */ for ( ; n && (bp = hb->h->bl[hb->b]) != NULL; n--, hb++) { bp->tick = hdclock; /* preempt swap */ @@ -501,13 +552,15 @@ int i; DCHECK(hp->fd < 0 | hp->fd >= nhdfragls || !hdfragl[hp->fd].nlinks, CONSISTENCY, "bad file descriptor in hdfreefrag"); f = &hdfragl[hp->fd]; + if (!f->writable) + return(0); if (f->nfrags % FRAGBLK == 0) { /* delete empty remnants */ for (j = k = 0; k < f->nfrags; j++, k++) { while (f->fi[k].nrd == 0) if (++k >= f->nfrags) goto endloop; if (k > j) - copystruct(f->fi+j, f->fi+k); + *(f->fi+j) = *(f->fi+k); } endloop: f->nfrags = j; @@ -525,7 +578,7 @@ int i; if (f->fi == NULL) newp = (BEAMI *)malloc((j+FRAGBLK)*sizeof(BEAMI)); else - newp = (BEAMI *)realloc((char *)f->fi, + newp = (BEAMI *)realloc((void *)f->fi, (j+FRAGBLK)*sizeof(BEAMI)); if (newp == NULL) { f->nfrags--; /* graceful failure */ @@ -539,7 +592,7 @@ int i; f->fi[j].nrd = bi->nrd; break; } - copystruct(f->fi+j, f->fi+(j-1)); + *(f->fi+j) = *(f->fi+(j-1)); } /* coalesce adjacent fragments */ /* successors never empty */ @@ -558,7 +611,7 @@ int i; } biglob(hp)->nrd -= bi->nrd; /* tell fragment it's free */ bi->nrd = 0; - bi->fo = 0L; + bi->fo = 0; hdmarkdirty(hp, i); /* assume we'll reallocate */ return(1); } @@ -568,12 +621,12 @@ int hdfragOK(fd, listlen, listsiz) /* get fragment list status for file */ int fd; int *listlen; -register int4 *listsiz; +register int32 *listsiz; { register struct fraglist *f; register int i; - if (fd < 0 | fd >= nhdfragls || !(f = &hdfragl[fd])->nlinks) + if ((fd < 0) | (fd >= nhdfragls) || !(f = &hdfragl[fd])->nlinks) return(0); /* listless */ if (listlen != NULL) *listlen = f->nfrags; @@ -588,14 +641,14 @@ register int4 *listsiz; } -long +off_t hdallocfrag(fd, nrays) /* allocate a file fragment */ int fd; -unsigned int4 nrays; +uint32 nrays; { register struct fraglist *f; register int j; - long nfo; + off_t nfo; if (nrays == 0) return(-1L); @@ -623,12 +676,12 @@ register HOLO *hp; register int i; { int fragfreed; - unsigned int4 nrays; + uint32 nrays; unsigned int n; - long nfo; + off_t nfo; /* check file status */ - if (hdfragl[hp->fd].writerr) - return(-1); + if (hdfragl[hp->fd].writable <= 0) + return(hdfragl[hp->fd].writable); DCHECK(i < 1 | i > nbeams(hp), CONSISTENCY, "bad beam index in hdsyncbeam"); /* is current fragment OK? */ @@ -639,17 +692,17 @@ register int i; if (nrays) { /* get and write new fragment */ nfo = hdallocfrag(hp->fd, nrays); errno = 0; - if (lseek(hp->fd, nfo, 0) < 0) + if (lseek(hp->fd, nfo, SEEK_SET) < 0) error(SYSTEM, "cannot seek on holodeck file"); n = hp->bl[i]->nrm * sizeof(RAYVAL); if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) { - hdfragl[hp->fd].writerr++; + hdfragl[hp->fd].writable = -1; hdsync(NULL, 0); /* sync directories */ error(SYSTEM, "write error in hdsyncbeam"); } hp->bi[i].fo = nfo; } else - hp->bi[i].fo = 0L; + hp->bi[i].fo = 0; biglob(hp)->nrd += nrays - hp->bi[i].nrd; hp->bi[i].nrd = nrays; if (!fragfreed) @@ -671,7 +724,7 @@ register int i; nchanged += hdfreebeam(hdlist[i], 0); return(nchanged); } - if (hdfragl[hp->fd].writerr) /* check for file error */ + if (hdfragl[hp->fd].writable < 0) /* check for file error */ return(0); if (i == 0) { /* clear entire holodeck */ if (blglob(hp)->nrm == 0) @@ -693,7 +746,7 @@ register int i; if (nchanged) hdsyncbeam(hp, i); /* write new fragment */ blglob(hp)->nrm -= hp->bl[i]->nrm; - free((char *)hp->bl[i]); /* free memory */ + free((void *)hp->bl[i]); /* free memory */ hp->bl[i] = NULL; return(nchanged); } @@ -713,7 +766,7 @@ register int i; return(nchanged); } if (i == 0) { /* clobber entire holodeck */ - if (biglob(hp)->nrd == 0 & blglob(hp)->nrm == 0) + if ((biglob(hp)->nrd == 0) & (blglob(hp)->nrm == 0)) return(0); /* already empty */ nchanged = 0; nchanged = 0; @@ -724,18 +777,20 @@ register int i; CONSISTENCY, "bad beam count in hdkillbeam"); return(nchanged); } - DCHECK(i < 1 | i > nbeams(hp), - CONSISTENCY, "bad beam index to hdkillbeam"); + DCHECK(i < 1 | i > nbeams(hp), CONSISTENCY, + "bad beam index to hdkillbeam"); + DCHECK(!hdfragl[hp->fd].writable, CONSISTENCY, + "hdkillbeam called on read-only holodeck"); if (hp->bl[i] != NULL) { /* free memory */ blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm; - free((char *)hp->bl[i]); + free((void *)hp->bl[i]); hp->bl[i] = NULL; } else nchanged = hp->bi[i].nrd; if (hp->bi[i].nrd && !(hdfragflags&FF_KILL && hdfreefrag(hp,i))) { biglob(hp)->nrd -= hp->bi[i].nrd; /* free failed */ hp->bi[i].nrd = 0; - hp->bi[i].fo = 0L; + hp->bi[i].fo = 0; hdmarkdirty(hp, i); } return(nchanged); @@ -767,7 +822,7 @@ register HOLO *hp; /* section we're adding from */ hb[j].b = i; break; } - copystruct(hb+j, hb+(j-1)); + *(hb+j) = *(hb+(j-1)); } } return(nents); /* return new list length */ @@ -826,7 +881,7 @@ register HOLO *hp; /* NULL means clean up all */ if (hp == NULL) { /* NULL means clean up everything */ while (hdlist[0] != NULL) hddone(hdlist[0]); - free((char *)hdfragl); + free((void *)hdfragl); hdfragl = NULL; nhdfragls = 0; return; } @@ -841,6 +896,6 @@ register HOLO *hp; /* NULL means clean up all */ i++; break; } - free((char *)hp->bl); /* free beam list */ - free((char *)hp); /* free holodeck struct */ + free((void *)hp->bl); /* free beam list */ + free((void *)hp); /* free holodeck struct */ }