| 1 |
< |
/* Copyright (c) 1999 Silicon Graphics, Inc. */ |
| 1 |
> |
/* Copyright (c) 1999 Regents of the University of California */ |
| 2 |
|
|
| 3 |
|
#ifndef lint |
| 4 |
|
static char SCCSid[] = "$SunId$ SGI"; |
| 52 |
|
|
| 53 |
|
static struct fraglist { |
| 54 |
|
short nlinks; /* number of holodeck sections using us */ |
| 55 |
< |
short writerr; /* write error encountered */ |
| 55 |
> |
short writable; /* 0 read-only, <0 write error encountered */ |
| 56 |
|
int nfrags; /* number of known fragments */ |
| 57 |
|
BEAMI *fi; /* fragments, descending file position */ |
| 58 |
|
long flen; /* last known file length */ |
| 61 |
|
static int nhdfragls; /* size of hdfragl array */ |
| 62 |
|
|
| 63 |
|
|
| 64 |
+ |
HOLO * |
| 65 |
+ |
hdalloc(hproto) /* allocate and set holodeck section based on grid */ |
| 66 |
+ |
HDGRID *hproto; |
| 67 |
+ |
{ |
| 68 |
+ |
HOLO hdhead; |
| 69 |
+ |
register HOLO *hp; |
| 70 |
+ |
int n; |
| 71 |
+ |
/* copy grid to temporary header */ |
| 72 |
+ |
bcopy((char *)hproto, (char *)&hdhead, sizeof(HDGRID)); |
| 73 |
+ |
/* compute grid vectors and sizes */ |
| 74 |
+ |
hdcompgrid(&hdhead); |
| 75 |
+ |
/* allocate header with directory */ |
| 76 |
+ |
n = sizeof(HOLO)+nbeams(&hdhead)*sizeof(BEAMI); |
| 77 |
+ |
if ((hp = (HOLO *)malloc(n)) == NULL) |
| 78 |
+ |
return(NULL); |
| 79 |
+ |
/* copy header information */ |
| 80 |
+ |
copystruct(hp, &hdhead); |
| 81 |
+ |
/* allocate and clear beam list */ |
| 82 |
+ |
hp->bl = (BEAM **)malloc((nbeams(hp)+1)*sizeof(BEAM *)+sizeof(BEAM)); |
| 83 |
+ |
if (hp->bl == NULL) { |
| 84 |
+ |
free((char *)hp); |
| 85 |
+ |
return(NULL); |
| 86 |
+ |
} |
| 87 |
+ |
bzero((char *)hp->bl, (nbeams(hp)+1)*sizeof(BEAM *)+sizeof(BEAM)); |
| 88 |
+ |
hp->bl[0] = (BEAM *)(hp->bl+nbeams(hp)+1); /* set blglob(hp) */ |
| 89 |
+ |
hp->fd = -1; |
| 90 |
+ |
hp->dirty = 0; |
| 91 |
+ |
hp->priv = NULL; |
| 92 |
+ |
/* clear beam directory */ |
| 93 |
+ |
bzero((char *)hp->bi, (nbeams(hp)+1)*sizeof(BEAMI)); |
| 94 |
+ |
return(hp); /* all is well */ |
| 95 |
+ |
} |
| 96 |
+ |
|
| 97 |
+ |
|
| 98 |
|
char * |
| 99 |
|
hdrealloc(ptr, siz, rout) /* (re)allocate memory, retry then error */ |
| 100 |
|
char *ptr; |
| 119 |
|
} |
| 120 |
|
|
| 121 |
|
|
| 122 |
< |
hdattach(fd) /* start tracking file fragments for some section */ |
| 122 |
> |
hdattach(fd, wr) /* start tracking file fragments for some section */ |
| 123 |
|
register int fd; |
| 124 |
+ |
int wr; |
| 125 |
|
{ |
| 126 |
|
if (fd >= nhdfragls) { |
| 127 |
|
hdfragl = (struct fraglist *)hdrealloc((char *)hdfragl, |
| 131 |
|
nhdfragls = fd+1; |
| 132 |
|
} |
| 133 |
|
hdfragl[fd].nlinks++; |
| 134 |
+ |
hdfragl[fd].writable = wr; /* set writable flag */ |
| 135 |
|
hdfragl[fd].flen = lseek(fd, 0L, 2); /* get file length */ |
| 136 |
|
} |
| 137 |
|
|
| 159 |
|
{ |
| 160 |
|
long rtrunc; |
| 161 |
|
long fpos; |
| 162 |
+ |
int writable; |
| 163 |
|
register HOLO *hp; |
| 164 |
|
register int n; |
| 165 |
|
/* prepare for system errors */ |
| 185 |
|
error(WARNING, "dirty holodeck section"); |
| 186 |
|
break; |
| 187 |
|
} |
| 188 |
< |
} else { /* assume we're creating it */ |
| 188 |
> |
/* check writability */ |
| 189 |
> |
if (fd < nhdfragls && hdfragl[fd].nlinks) |
| 190 |
> |
writable = hdfragl[fd].writable; |
| 191 |
> |
else |
| 192 |
> |
writable = lseek(fd, fpos, 0) == fpos && |
| 193 |
> |
write(fd, (char *)hp, sizeof(HDGRID)) == |
| 194 |
> |
sizeof(HDGRID); |
| 195 |
> |
} else { /* else assume we're creating it */ |
| 196 |
|
if ((hp = hdalloc(hproto)) == NULL) |
| 197 |
|
goto memerr; |
| 198 |
|
/* write header and skeleton */ |
| 201 |
|
sizeof(HDGRID) || |
| 202 |
|
write(fd, (char *)(hp->bi+1), n) != n) |
| 203 |
|
error(SYSTEM, "cannot write header to holodeck file"); |
| 204 |
+ |
writable = 1; |
| 205 |
|
} |
| 206 |
|
hp->fd = fd; |
| 207 |
|
hp->dirty = 0; |
| 208 |
|
biglob(hp)->fo = fpos + sizeof(HDGRID); |
| 209 |
|
/* start tracking fragments */ |
| 210 |
< |
hdattach(fd); |
| 210 |
> |
hdattach(fd, writable); |
| 211 |
|
/* check rays on disk */ |
| 212 |
|
fpos = hdfilen(fd); |
| 213 |
|
biglob(hp)->nrd = rtrunc = 0; |
| 546 |
|
DCHECK(hp->fd < 0 | hp->fd >= nhdfragls || !hdfragl[hp->fd].nlinks, |
| 547 |
|
CONSISTENCY, "bad file descriptor in hdfreefrag"); |
| 548 |
|
f = &hdfragl[hp->fd]; |
| 549 |
+ |
if (!f->writable) |
| 550 |
+ |
return(0); |
| 551 |
|
if (f->nfrags % FRAGBLK == 0) { /* delete empty remnants */ |
| 552 |
|
for (j = k = 0; k < f->nfrags; j++, k++) { |
| 553 |
|
while (f->fi[k].nrd == 0) |
| 674 |
|
unsigned int n; |
| 675 |
|
long nfo; |
| 676 |
|
/* check file status */ |
| 677 |
< |
if (hdfragl[hp->fd].writerr) |
| 678 |
< |
return(-1); |
| 677 |
> |
if (hdfragl[hp->fd].writable <= 0) |
| 678 |
> |
return(hdfragl[hp->fd].writable); |
| 679 |
|
DCHECK(i < 1 | i > nbeams(hp), |
| 680 |
|
CONSISTENCY, "bad beam index in hdsyncbeam"); |
| 681 |
|
/* is current fragment OK? */ |
| 690 |
|
error(SYSTEM, "cannot seek on holodeck file"); |
| 691 |
|
n = hp->bl[i]->nrm * sizeof(RAYVAL); |
| 692 |
|
if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) { |
| 693 |
< |
hdfragl[hp->fd].writerr++; |
| 693 |
> |
hdfragl[hp->fd].writable = -1; |
| 694 |
|
hdsync(NULL, 0); /* sync directories */ |
| 695 |
|
error(SYSTEM, "write error in hdsyncbeam"); |
| 696 |
|
} |
| 718 |
|
nchanged += hdfreebeam(hdlist[i], 0); |
| 719 |
|
return(nchanged); |
| 720 |
|
} |
| 721 |
< |
if (hdfragl[hp->fd].writerr) /* check for file error */ |
| 721 |
> |
if (hdfragl[hp->fd].writable < 0) /* check for file error */ |
| 722 |
|
return(0); |
| 723 |
|
if (i == 0) { /* clear entire holodeck */ |
| 724 |
|
if (blglob(hp)->nrm == 0) |
| 771 |
|
CONSISTENCY, "bad beam count in hdkillbeam"); |
| 772 |
|
return(nchanged); |
| 773 |
|
} |
| 774 |
< |
DCHECK(i < 1 | i > nbeams(hp), |
| 775 |
< |
CONSISTENCY, "bad beam index to hdkillbeam"); |
| 774 |
> |
DCHECK(i < 1 | i > nbeams(hp), CONSISTENCY, |
| 775 |
> |
"bad beam index to hdkillbeam"); |
| 776 |
> |
DCHECK(!hdfragl[hp->fd].writable, CONSISTENCY, |
| 777 |
> |
"hdkillbeam called on read-only holodeck"); |
| 778 |
|
if (hp->bl[i] != NULL) { /* free memory */ |
| 779 |
|
blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm; |
| 780 |
|
free((char *)hp->bl[i]); |