1 |
– |
/* Copyright (c) 1999 Silicon Graphics, Inc. */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ SGI"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Routines for managing holodeck files |
6 |
|
* |
11 |
|
|
12 |
|
#ifndef CACHESIZE |
13 |
|
#ifdef BIGMEM |
14 |
< |
#define CACHESIZE 32 /* default cache size (Mbytes, 0==inf) */ |
14 |
> |
#define CACHESIZE 17 /* default cache size (Mbytes, 0==inf) */ |
15 |
|
#else |
16 |
< |
#define CACHESIZE 12 |
16 |
> |
#define CACHESIZE 5 |
17 |
|
#endif |
18 |
|
#endif |
19 |
|
#ifndef FREEBEAMS |
27 |
|
#endif |
28 |
|
#ifndef FF_DEFAULT |
29 |
|
/* when to free a beam fragment */ |
30 |
< |
#define FF_DEFAULT (FF_ALLOC|FF_WRITE|FF_KILL) |
30 |
> |
#define FF_DEFAULT (FF_WRITE|FF_KILL) |
31 |
|
#endif |
32 |
|
#ifndef MINDIRSEL |
33 |
|
/* minimum directory seek length */ |
49 |
|
|
50 |
|
static struct fraglist { |
51 |
|
short nlinks; /* number of holodeck sections using us */ |
52 |
< |
short writerr; /* write error encountered */ |
52 |
> |
short writable; /* 0 read-only, <0 write error encountered */ |
53 |
|
int nfrags; /* number of known fragments */ |
54 |
|
BEAMI *fi; /* fragments, descending file position */ |
55 |
|
long flen; /* last known file length */ |
58 |
|
static int nhdfragls; /* size of hdfragl array */ |
59 |
|
|
60 |
|
|
61 |
+ |
HOLO * |
62 |
+ |
hdalloc(hproto) /* allocate and set holodeck section based on grid */ |
63 |
+ |
HDGRID *hproto; |
64 |
+ |
{ |
65 |
+ |
HOLO hdhead; |
66 |
+ |
register HOLO *hp; |
67 |
+ |
int n; |
68 |
+ |
/* copy grid to temporary header */ |
69 |
+ |
bcopy((char *)hproto, (char *)&hdhead, sizeof(HDGRID)); |
70 |
+ |
/* compute grid vectors and sizes */ |
71 |
+ |
hdcompgrid(&hdhead); |
72 |
+ |
/* allocate header with directory */ |
73 |
+ |
n = sizeof(HOLO)+nbeams(&hdhead)*sizeof(BEAMI); |
74 |
+ |
if ((hp = (HOLO *)malloc(n)) == NULL) |
75 |
+ |
return(NULL); |
76 |
+ |
/* copy header information */ |
77 |
+ |
copystruct(hp, &hdhead); |
78 |
+ |
/* allocate and clear beam list */ |
79 |
+ |
hp->bl = (BEAM **)malloc((nbeams(hp)+1)*sizeof(BEAM *)+sizeof(BEAM)); |
80 |
+ |
if (hp->bl == NULL) { |
81 |
+ |
free((void *)hp); |
82 |
+ |
return(NULL); |
83 |
+ |
} |
84 |
+ |
bzero((char *)hp->bl, (nbeams(hp)+1)*sizeof(BEAM *)+sizeof(BEAM)); |
85 |
+ |
hp->bl[0] = (BEAM *)(hp->bl+nbeams(hp)+1); /* set blglob(hp) */ |
86 |
+ |
hp->fd = -1; |
87 |
+ |
hp->dirty = 0; |
88 |
+ |
hp->priv = NULL; |
89 |
+ |
/* clear beam directory */ |
90 |
+ |
bzero((char *)hp->bi, (nbeams(hp)+1)*sizeof(BEAMI)); |
91 |
+ |
return(hp); /* all is well */ |
92 |
+ |
} |
93 |
+ |
|
94 |
+ |
|
95 |
|
char * |
96 |
|
hdrealloc(ptr, siz, rout) /* (re)allocate memory, retry then error */ |
97 |
|
char *ptr; |
116 |
|
} |
117 |
|
|
118 |
|
|
119 |
< |
hdattach(fd) /* start tracking file fragments for some section */ |
119 |
> |
hdattach(fd, wr) /* start tracking file fragments for some section */ |
120 |
|
register int fd; |
121 |
+ |
int wr; |
122 |
|
{ |
123 |
|
if (fd >= nhdfragls) { |
124 |
|
hdfragl = (struct fraglist *)hdrealloc((char *)hdfragl, |
128 |
|
nhdfragls = fd+1; |
129 |
|
} |
130 |
|
hdfragl[fd].nlinks++; |
131 |
< |
hdfragl[fd].flen = lseek(fd, 0L, 2); /* get file length */ |
131 |
> |
hdfragl[fd].writable = wr; /* set writable flag */ |
132 |
> |
hdfragl[fd].flen = lseek(fd, (off_t)0L, 2); /* get file length */ |
133 |
|
} |
134 |
|
|
135 |
|
|
142 |
|
if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks) |
143 |
|
return; |
144 |
|
if (!--hdfragl[fd].nlinks && hdfragl[fd].nfrags) { |
145 |
< |
free((char *)hdfragl[fd].fi); |
145 |
> |
free((void *)hdfragl[fd].fi); |
146 |
|
hdfragl[fd].fi = NULL; |
147 |
|
hdfragl[fd].nfrags = 0; |
148 |
|
} |
156 |
|
{ |
157 |
|
long rtrunc; |
158 |
|
long fpos; |
159 |
+ |
int writable; |
160 |
|
register HOLO *hp; |
161 |
|
register int n; |
162 |
|
/* prepare for system errors */ |
163 |
|
errno = 0; |
164 |
< |
if ((fpos = lseek(fd, 0L, 1)) < 0) |
164 |
> |
if ((fpos = lseek(fd, (off_t)0L, 1)) < 0) |
165 |
|
error(SYSTEM, "cannot determine holodeck file position"); |
166 |
|
if (hproto == NULL) { /* assume we're loading it */ |
167 |
|
HDGRID hpr; |
182 |
|
error(WARNING, "dirty holodeck section"); |
183 |
|
break; |
184 |
|
} |
185 |
< |
} else { /* assume we're creating it */ |
185 |
> |
/* check writability */ |
186 |
> |
if (fd < nhdfragls && hdfragl[fd].nlinks) |
187 |
> |
writable = hdfragl[fd].writable; |
188 |
> |
else |
189 |
> |
writable = lseek(fd, (off_t)fpos, 0) == fpos && |
190 |
> |
write(fd, (char *)hp, sizeof(HDGRID)) == |
191 |
> |
sizeof(HDGRID); |
192 |
> |
} else { /* else assume we're creating it */ |
193 |
|
if ((hp = hdalloc(hproto)) == NULL) |
194 |
|
goto memerr; |
195 |
|
/* write header and skeleton */ |
198 |
|
sizeof(HDGRID) || |
199 |
|
write(fd, (char *)(hp->bi+1), n) != n) |
200 |
|
error(SYSTEM, "cannot write header to holodeck file"); |
201 |
+ |
writable = 1; |
202 |
|
} |
203 |
|
hp->fd = fd; |
204 |
|
hp->dirty = 0; |
205 |
|
biglob(hp)->fo = fpos + sizeof(HDGRID); |
206 |
|
/* start tracking fragments */ |
207 |
< |
hdattach(fd); |
207 |
> |
hdattach(fd, writable); |
208 |
|
/* check rays on disk */ |
209 |
|
fpos = hdfilen(fd); |
210 |
|
biglob(hp)->nrd = rtrunc = 0; |
233 |
|
} |
234 |
|
|
235 |
|
|
236 |
< |
markdirty(hp, i) /* mark holodeck directory position dirty */ |
236 |
> |
hdmarkdirty(hp, i) /* mark holodeck directory position dirty */ |
237 |
|
register HOLO *hp; |
238 |
|
int i; |
239 |
|
{ |
242 |
|
register int j; |
243 |
|
|
244 |
|
if (!hp->dirty++) { /* write smudge first time */ |
245 |
< |
if (lseek(hp->fd, biglob(hp)->fo+(i-1)*sizeof(BEAMI), 0) < 0 |
245 |
> |
if (lseek(hp->fd, (off_t)(biglob(hp)->fo+(i-1)*sizeof(BEAMI)), 0) < 0 |
246 |
|
|| write(hp->fd, (char *)&smudge, |
247 |
|
sizeof(BEAMI)) != sizeof(BEAMI)) |
248 |
< |
error(SYSTEM, "seek/write error in markdirty"); |
248 |
> |
error(SYSTEM, "seek/write error in hdmarkdirty"); |
249 |
|
hp->dirseg[0].s = i; |
250 |
|
hp->dirseg[0].n = 1; |
251 |
|
return; |
305 |
|
return(0); |
306 |
|
errno = 0; /* write dirty segments */ |
307 |
|
for (j = 0; j < hp->dirty; j++) { |
308 |
< |
if (lseek(hp->fd, biglob(hp)->fo + |
309 |
< |
(hp->dirseg[j].s-1)*sizeof(BEAMI), 0) < 0) |
308 |
> |
if (lseek(hp->fd, (off_t)(biglob(hp)->fo + |
309 |
> |
(hp->dirseg[j].s-1)*sizeof(BEAMI)), 0) < 0) |
310 |
|
error(SYSTEM, "cannot seek on holodeck file"); |
311 |
|
n = hp->dirseg[j].n * sizeof(BEAMI); |
312 |
|
if (write(hp->fd, (char *)(hp->bi+hp->dirseg[j].s), n) != n) |
355 |
|
if (fd < 0) |
356 |
|
return(-1); |
357 |
|
if (fd >= nhdfragls || !hdfragl[fd].nlinks) { |
358 |
< |
if ((fpos = lseek(fd, 0L, 1)) < 0) |
358 |
> |
if ((fpos = lseek(fd, (off_t)0L, 1)) < 0) |
359 |
|
return(-1); |
360 |
< |
flen = lseek(fd, 0L, 2); |
361 |
< |
lseek(fd, fpos, 0); |
360 |
> |
flen = lseek(fd, (off_t)0L, 2); |
361 |
> |
lseek(fd, (off_t)fpos, 0); |
362 |
|
return(flen); |
363 |
|
} |
364 |
|
return(hdfragl[fd].flen); |
413 |
|
blglob(hp)->nrm += n; |
414 |
|
if ((n = hp->bl[i]->nrm = hp->bi[i].nrd)) { |
415 |
|
errno = 0; |
416 |
< |
if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) |
416 |
> |
if (lseek(hp->fd, (off_t)hp->bi[i].fo, 0) < 0) |
417 |
|
error(SYSTEM, "seek error on holodeck file"); |
418 |
|
n *= sizeof(RAYVAL); |
419 |
|
if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) |
452 |
|
hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdgetbeam"); |
453 |
|
blglob(hp)->nrm += hp->bl[i]->nrm = n; |
454 |
|
errno = 0; |
455 |
< |
if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) |
455 |
> |
if (lseek(hp->fd, (off_t)hp->bi[i].fo, 0) < 0) |
456 |
|
error(SYSTEM, "seek error on holodeck file"); |
457 |
|
n *= sizeof(RAYVAL); |
458 |
|
if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) |
543 |
|
DCHECK(hp->fd < 0 | hp->fd >= nhdfragls || !hdfragl[hp->fd].nlinks, |
544 |
|
CONSISTENCY, "bad file descriptor in hdfreefrag"); |
545 |
|
f = &hdfragl[hp->fd]; |
546 |
+ |
if (!f->writable) |
547 |
+ |
return(0); |
548 |
|
if (f->nfrags % FRAGBLK == 0) { /* delete empty remnants */ |
549 |
|
for (j = k = 0; k < f->nfrags; j++, k++) { |
550 |
|
while (f->fi[k].nrd == 0) |
603 |
|
biglob(hp)->nrd -= bi->nrd; /* tell fragment it's free */ |
604 |
|
bi->nrd = 0; |
605 |
|
bi->fo = 0L; |
606 |
+ |
hdmarkdirty(hp, i); /* assume we'll reallocate */ |
607 |
|
return(1); |
608 |
|
} |
609 |
|
|
666 |
|
register HOLO *hp; |
667 |
|
register int i; |
668 |
|
{ |
669 |
+ |
int fragfreed; |
670 |
|
unsigned int4 nrays; |
671 |
|
unsigned int n; |
672 |
|
long nfo; |
673 |
|
/* check file status */ |
674 |
< |
if (hdfragl[hp->fd].writerr) |
675 |
< |
return(-1); |
674 |
> |
if (hdfragl[hp->fd].writable <= 0) |
675 |
> |
return(hdfragl[hp->fd].writable); |
676 |
|
DCHECK(i < 1 | i > nbeams(hp), |
677 |
|
CONSISTENCY, "bad beam index in hdsyncbeam"); |
678 |
|
/* is current fragment OK? */ |
679 |
|
if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd) |
680 |
|
return(0); |
681 |
< |
if (hdfragflags&FF_WRITE && hp->bi[i].nrd) |
682 |
< |
hdfreefrag(hp, i); /* relinquish old fragment */ |
681 |
> |
/* relinquish old fragment? */ |
682 |
> |
fragfreed = hdfragflags&FF_WRITE && hp->bi[i].nrd && hdfreefrag(hp,i); |
683 |
|
if (nrays) { /* get and write new fragment */ |
684 |
|
nfo = hdallocfrag(hp->fd, nrays); |
685 |
|
errno = 0; |
686 |
< |
if (lseek(hp->fd, nfo, 0) < 0) |
686 |
> |
if (lseek(hp->fd, (off_t)nfo, 0) < 0) |
687 |
|
error(SYSTEM, "cannot seek on holodeck file"); |
688 |
|
n = hp->bl[i]->nrm * sizeof(RAYVAL); |
689 |
|
if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) { |
690 |
< |
hdfragl[hp->fd].writerr++; |
690 |
> |
hdfragl[hp->fd].writable = -1; |
691 |
|
hdsync(NULL, 0); /* sync directories */ |
692 |
|
error(SYSTEM, "write error in hdsyncbeam"); |
693 |
|
} |
696 |
|
hp->bi[i].fo = 0L; |
697 |
|
biglob(hp)->nrd += nrays - hp->bi[i].nrd; |
698 |
|
hp->bi[i].nrd = nrays; |
699 |
< |
markdirty(hp, i); /* section directory now out of date */ |
699 |
> |
if (!fragfreed) |
700 |
> |
hdmarkdirty(hp, i); /* need to flag dir. ent. */ |
701 |
|
return(1); |
702 |
|
} |
703 |
|
|
715 |
|
nchanged += hdfreebeam(hdlist[i], 0); |
716 |
|
return(nchanged); |
717 |
|
} |
718 |
< |
if (hdfragl[hp->fd].writerr) /* check for file error */ |
718 |
> |
if (hdfragl[hp->fd].writable < 0) /* check for file error */ |
719 |
|
return(0); |
720 |
|
if (i == 0) { /* clear entire holodeck */ |
721 |
+ |
if (blglob(hp)->nrm == 0) |
722 |
+ |
return(0); /* already clear */ |
723 |
|
nchanged = 0; |
724 |
|
for (i = nbeams(hp); i > 0; i--) |
725 |
|
if (hp->bl[i] != NULL) |
726 |
|
nchanged += hdfreebeam(hp, i); |
727 |
+ |
DCHECK(blglob(hp)->nrm != 0, |
728 |
+ |
CONSISTENCY, "bad beam count in hdfreebeam"); |
729 |
|
return(nchanged); |
730 |
|
} |
731 |
|
DCHECK(i < 1 | i > nbeams(hp), |
737 |
|
if (nchanged) |
738 |
|
hdsyncbeam(hp, i); /* write new fragment */ |
739 |
|
blglob(hp)->nrm -= hp->bl[i]->nrm; |
740 |
< |
free((char *)hp->bl[i]); /* free memory */ |
740 |
> |
free((void *)hp->bl[i]); /* free memory */ |
741 |
|
hp->bl[i] = NULL; |
742 |
|
return(nchanged); |
743 |
|
} |
757 |
|
return(nchanged); |
758 |
|
} |
759 |
|
if (i == 0) { /* clobber entire holodeck */ |
760 |
+ |
if (biglob(hp)->nrd == 0 & blglob(hp)->nrm == 0) |
761 |
+ |
return(0); /* already empty */ |
762 |
|
nchanged = 0; |
763 |
+ |
nchanged = 0; |
764 |
|
for (i = nbeams(hp); i > 0; i--) |
765 |
|
if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL) |
766 |
|
nchanged += hdkillbeam(hp, i); |
768 |
|
CONSISTENCY, "bad beam count in hdkillbeam"); |
769 |
|
return(nchanged); |
770 |
|
} |
771 |
< |
DCHECK(i < 1 | i > nbeams(hp), |
772 |
< |
CONSISTENCY, "bad beam index to hdkillbeam"); |
771 |
> |
DCHECK(i < 1 | i > nbeams(hp), CONSISTENCY, |
772 |
> |
"bad beam index to hdkillbeam"); |
773 |
> |
DCHECK(!hdfragl[hp->fd].writable, CONSISTENCY, |
774 |
> |
"hdkillbeam called on read-only holodeck"); |
775 |
|
if (hp->bl[i] != NULL) { /* free memory */ |
776 |
|
blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm; |
777 |
< |
free((char *)hp->bl[i]); |
777 |
> |
free((void *)hp->bl[i]); |
778 |
> |
hp->bl[i] = NULL; |
779 |
|
} else |
780 |
|
nchanged = hp->bi[i].nrd; |
781 |
< |
if (hp->bi[i].nrd) { |
782 |
< |
if (hdfragflags&FF_KILL) |
783 |
< |
hdfreefrag(hp, i); |
727 |
< |
biglob(hp)->nrd -= hp->bi[i].nrd; |
728 |
< |
hp->bi[i].nrd = 0; /* make sure it's gone */ |
781 |
> |
if (hp->bi[i].nrd && !(hdfragflags&FF_KILL && hdfreefrag(hp,i))) { |
782 |
> |
biglob(hp)->nrd -= hp->bi[i].nrd; /* free failed */ |
783 |
> |
hp->bi[i].nrd = 0; |
784 |
|
hp->bi[i].fo = 0L; |
785 |
+ |
hdmarkdirty(hp, i); |
786 |
|
} |
731 |
– |
hp->bl[i] = NULL; |
787 |
|
return(nchanged); |
788 |
|
} |
789 |
|
|
872 |
|
if (hp == NULL) { /* NULL means clean up everything */ |
873 |
|
while (hdlist[0] != NULL) |
874 |
|
hddone(hdlist[0]); |
875 |
< |
free((char *)hdfragl); |
875 |
> |
free((void *)hdfragl); |
876 |
|
hdfragl = NULL; nhdfragls = 0; |
877 |
|
return; |
878 |
|
} |
879 |
|
/* flush all data and free memory */ |
880 |
< |
hdfreebeam(hp, 0); |
826 |
< |
hdsync(hp, 0); |
880 |
> |
hdflush(hp); |
881 |
|
/* release fragment resources */ |
882 |
|
hdrelease(hp->fd); |
883 |
|
/* remove hp from active list */ |
887 |
|
i++; |
888 |
|
break; |
889 |
|
} |
890 |
< |
free((char *)hp->bl); /* free beam list */ |
891 |
< |
free((char *)hp); /* free holodeck struct */ |
890 |
> |
free((void *)hp->bl); /* free beam list */ |
891 |
> |
free((void *)hp); /* free holodeck struct */ |
892 |
|
} |