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"; |
13 |
|
#include "holo.h" |
14 |
|
|
15 |
|
#ifndef CACHESIZE |
16 |
< |
#define CACHESIZE 16 /* default cache size (Mbytes, 0==inf) */ |
16 |
> |
#ifdef BIGMEM |
17 |
> |
#define CACHESIZE 17 /* default cache size (Mbytes, 0==inf) */ |
18 |
> |
#else |
19 |
> |
#define CACHESIZE 5 |
20 |
|
#endif |
21 |
+ |
#endif |
22 |
|
#ifndef FREEBEAMS |
23 |
|
#define FREEBEAMS 1500 /* maximum beams to free at a time */ |
24 |
|
#endif |
30 |
|
#endif |
31 |
|
#ifndef FF_DEFAULT |
32 |
|
/* when to free a beam fragment */ |
33 |
< |
#define FF_DEFAULT (FF_ALLOC|FF_WRITE|FF_KILL) |
33 |
> |
#define FF_DEFAULT (FF_WRITE|FF_KILL) |
34 |
|
#endif |
35 |
|
#ifndef MINDIRSEL |
36 |
|
/* minimum directory seek length */ |
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; |
236 |
|
} |
237 |
|
|
238 |
|
|
239 |
< |
markdirty(hp, i) /* mark holodeck directory position dirty */ |
239 |
> |
hdmarkdirty(hp, i) /* mark holodeck directory position dirty */ |
240 |
|
register HOLO *hp; |
241 |
|
int i; |
242 |
|
{ |
248 |
|
if (lseek(hp->fd, biglob(hp)->fo+(i-1)*sizeof(BEAMI), 0) < 0 |
249 |
|
|| write(hp->fd, (char *)&smudge, |
250 |
|
sizeof(BEAMI)) != sizeof(BEAMI)) |
251 |
< |
error(SYSTEM, "seek/write error in markdirty"); |
251 |
> |
error(SYSTEM, "seek/write error in hdmarkdirty"); |
252 |
|
hp->dirseg[0].s = i; |
253 |
|
hp->dirseg[0].n = 1; |
254 |
|
return; |
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) |
606 |
|
biglob(hp)->nrd -= bi->nrd; /* tell fragment it's free */ |
607 |
|
bi->nrd = 0; |
608 |
|
bi->fo = 0L; |
609 |
+ |
hdmarkdirty(hp, i); /* assume we'll reallocate */ |
610 |
|
return(1); |
611 |
|
} |
612 |
|
|
669 |
|
register HOLO *hp; |
670 |
|
register int i; |
671 |
|
{ |
672 |
+ |
int fragfreed; |
673 |
|
unsigned int4 nrays; |
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? */ |
682 |
|
if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd) |
683 |
|
return(0); |
684 |
< |
if (hdfragflags&FF_WRITE && hp->bi[i].nrd) |
685 |
< |
hdfreefrag(hp, i); /* relinquish old fragment */ |
684 |
> |
/* relinquish old fragment? */ |
685 |
> |
fragfreed = hdfragflags&FF_WRITE && hp->bi[i].nrd && hdfreefrag(hp,i); |
686 |
|
if (nrays) { /* get and write new fragment */ |
687 |
|
nfo = hdallocfrag(hp->fd, nrays); |
688 |
|
errno = 0; |
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 |
|
} |
699 |
|
hp->bi[i].fo = 0L; |
700 |
|
biglob(hp)->nrd += nrays - hp->bi[i].nrd; |
701 |
|
hp->bi[i].nrd = nrays; |
702 |
< |
markdirty(hp, i); /* section directory now out of date */ |
702 |
> |
if (!fragfreed) |
703 |
> |
hdmarkdirty(hp, i); /* need to flag dir. ent. */ |
704 |
|
return(1); |
705 |
|
} |
706 |
|
|
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) |
725 |
+ |
return(0); /* already clear */ |
726 |
|
nchanged = 0; |
727 |
|
for (i = nbeams(hp); i > 0; i--) |
728 |
|
if (hp->bl[i] != NULL) |
729 |
|
nchanged += hdfreebeam(hp, i); |
730 |
+ |
DCHECK(blglob(hp)->nrm != 0, |
731 |
+ |
CONSISTENCY, "bad beam count in hdfreebeam"); |
732 |
|
return(nchanged); |
733 |
|
} |
734 |
|
DCHECK(i < 1 | i > nbeams(hp), |
760 |
|
return(nchanged); |
761 |
|
} |
762 |
|
if (i == 0) { /* clobber entire holodeck */ |
763 |
+ |
if (biglob(hp)->nrd == 0 & blglob(hp)->nrm == 0) |
764 |
+ |
return(0); /* already empty */ |
765 |
|
nchanged = 0; |
766 |
+ |
nchanged = 0; |
767 |
|
for (i = nbeams(hp); i > 0; i--) |
768 |
|
if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL) |
769 |
|
nchanged += hdkillbeam(hp, i); |
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]); |
781 |
+ |
hp->bl[i] = NULL; |
782 |
|
} else |
783 |
|
nchanged = hp->bi[i].nrd; |
784 |
< |
if (hp->bi[i].nrd) { |
785 |
< |
if (hdfragflags&FF_KILL) |
786 |
< |
hdfreefrag(hp, i); |
723 |
< |
biglob(hp)->nrd -= hp->bi[i].nrd; |
724 |
< |
hp->bi[i].nrd = 0; /* make sure it's gone */ |
784 |
> |
if (hp->bi[i].nrd && !(hdfragflags&FF_KILL && hdfreefrag(hp,i))) { |
785 |
> |
biglob(hp)->nrd -= hp->bi[i].nrd; /* free failed */ |
786 |
> |
hp->bi[i].nrd = 0; |
787 |
|
hp->bi[i].fo = 0L; |
788 |
+ |
hdmarkdirty(hp, i); |
789 |
|
} |
727 |
– |
hp->bl[i] = NULL; |
790 |
|
return(nchanged); |
791 |
|
} |
792 |
|
|
880 |
|
return; |
881 |
|
} |
882 |
|
/* flush all data and free memory */ |
883 |
< |
hdfreebeam(hp, 0); |
822 |
< |
hdsync(hp, 0); |
883 |
> |
hdflush(hp); |
884 |
|
/* release fragment resources */ |
885 |
|
hdrelease(hp->fd); |
886 |
|
/* remove hp from active list */ |