--- ray/src/hd/holofile.c 1999/02/10 17:09:20 3.37 +++ ray/src/hd/holofile.c 1999/08/05 15:53:09 3.41 @@ -1,4 +1,4 @@ -/* Copyright (c) 1999 Silicon Graphics, Inc. */ +/* Copyright (c) 1999 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ SGI"; @@ -14,9 +14,9 @@ static char SCCSid[] = "$SunId$ SGI"; #ifndef CACHESIZE #ifdef BIGMEM -#define CACHESIZE 32 /* default cache size (Mbytes, 0==inf) */ +#define CACHESIZE 17 /* default cache size (Mbytes, 0==inf) */ #else -#define CACHESIZE 12 +#define CACHESIZE 5 #endif #endif #ifndef FREEBEAMS @@ -30,7 +30,7 @@ static char SCCSid[] = "$SunId$ SGI"; #endif #ifndef FF_DEFAULT /* when to free a beam fragment */ -#define FF_DEFAULT (FF_ALLOC|FF_WRITE|FF_KILL) +#define FF_DEFAULT (FF_WRITE|FF_KILL) #endif #ifndef MINDIRSEL /* minimum directory seek length */ @@ -52,7 +52,7 @@ 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 */ @@ -61,6 +61,40 @@ static struct fraglist { 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 */ + bcopy((char *)hproto, (char *)&hdhead, 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 */ + copystruct(hp, &hdhead); + /* allocate and clear beam list */ + hp->bl = (BEAM **)malloc((nbeams(hp)+1)*sizeof(BEAM *)+sizeof(BEAM)); + if (hp->bl == NULL) { + free((char *)hp); + return(NULL); + } + bzero((char *)hp->bl, (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 */ + bzero((char *)hp->bi, (nbeams(hp)+1)*sizeof(BEAMI)); + return(hp); /* all is well */ +} + + char * hdrealloc(ptr, siz, rout) /* (re)allocate memory, retry then error */ char *ptr; @@ -85,8 +119,9 @@ 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, @@ -96,6 +131,7 @@ register int fd; nhdfragls = fd+1; } hdfragl[fd].nlinks++; + hdfragl[fd].writable = wr; /* set writable flag */ hdfragl[fd].flen = lseek(fd, 0L, 2); /* get file length */ } @@ -123,6 +159,7 @@ HDGRID *hproto; /* holodeck section grid */ { long rtrunc; long fpos; + int writable; register HOLO *hp; register int n; /* prepare for system errors */ @@ -148,7 +185,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, 0) == 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,12 +201,13 @@ 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; @@ -501,6 +546,8 @@ 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) @@ -559,6 +606,7 @@ int i; biglob(hp)->nrd -= bi->nrd; /* tell fragment it's free */ bi->nrd = 0; bi->fo = 0L; + hdmarkdirty(hp, i); /* assume we'll reallocate */ return(1); } @@ -621,19 +669,20 @@ hdsyncbeam(hp, i) /* sync beam in memory with beam on register HOLO *hp; register int i; { + int fragfreed; unsigned int4 nrays; unsigned int n; long 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? */ if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd) return(0); - if (hdfragflags&FF_WRITE && hp->bi[i].nrd) - hdfreefrag(hp, i); /* relinquish old fragment */ + /* relinquish old fragment? */ + fragfreed = hdfragflags&FF_WRITE && hp->bi[i].nrd && hdfreefrag(hp,i); if (nrays) { /* get and write new fragment */ nfo = hdallocfrag(hp->fd, nrays); errno = 0; @@ -641,7 +690,7 @@ register int i; 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"); } @@ -650,7 +699,8 @@ register int i; hp->bi[i].fo = 0L; biglob(hp)->nrd += nrays - hp->bi[i].nrd; hp->bi[i].nrd = nrays; - hdmarkdirty(hp, i); /* section directory now out of date */ + if (!fragfreed) + hdmarkdirty(hp, i); /* need to flag dir. ent. */ return(1); } @@ -668,7 +718,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) @@ -721,21 +771,22 @@ 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]); + hp->bl[i] = NULL; } else nchanged = hp->bi[i].nrd; - if (hp->bi[i].nrd) { - if (hdfragflags&FF_KILL) - hdfreefrag(hp, i); - biglob(hp)->nrd -= hp->bi[i].nrd; - hp->bi[i].nrd = 0; /* make sure it's gone */ + 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; + hdmarkdirty(hp, i); } - hp->bl[i] = NULL; return(nchanged); }