2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
16 #include <sys/types.h>
23 #include <netinet/in.h>
31 #define UBIK_INTERNALS 1
34 /* these routines are called via the proc ptr in the ubik_dbase structure. They provide access to
35 * the physical disk, by converting the file numbers being processed (>= 0 for user data space, < 0
36 * for ubik system files, such as the log) to actual pathnames to open, read, write, truncate, sync,
41 static struct fdcache {
45 } fdcache[MAXFDCACHE];
47 static char pbuffer[1024];
49 /* beware, when using this function, of the header in front of most files */
51 uphys_open(register struct ubik_dbase *adbase, afs_int32 afid)
57 register struct fdcache *tfd;
58 struct fdcache *bestfd;
60 /* initialize package */
64 for (i = 0; i < MAXFDCACHE; tfd++, i++) {
65 tfd->fd = -1; /* invalid value */
66 tfd->fileID = -10000; /* invalid value */
71 /* scan file descr cache */
72 for (tfd = fdcache, i = 0; i < MAXFDCACHE; i++, tfd++) {
73 if (afid == tfd->fileID && tfd->refCount == 0) { /* don't use open fd */
74 lseek(tfd->fd, 0, 0); /* reset ptr just like open would have */
80 /* not found, open it and try to enter in cache */
81 strcpy(pbuffer, adbase->pathName);
82 strcat(pbuffer, ".DB");
85 strcat(pbuffer, "SYS");
88 sprintf(temp, "%d", i);
89 strcat(pbuffer, temp);
90 fd = open(pbuffer, O_CREAT | O_RDWR, 0600);
92 /* try opening read-only */
93 fd = open(pbuffer, O_RDONLY, 0);
98 /* enter it in the cache */
101 for (i = 0; i < MAXFDCACHE; i++, tfd++) { /* look for empty slot */
107 if (!bestfd) { /* look for reclaimable slot */
109 for (i = 0; i < MAXFDCACHE; i++, tfd++) {
110 if (tfd->refCount == 0) {
116 if (bestfd) { /* found a usable slot */
121 tfd->refCount = 1; /* us */
125 /* finally, we're done */
129 /* close the file, maintaining ref count in cache structure */
131 uphys_close(register int afd)
134 register struct fdcache *tfd;
139 for (i = 0; i < MAXFDCACHE; i++, tfd++) {
140 if (tfd->fd == afd) {
149 uphys_stat(struct ubik_dbase *adbase, afs_int32 afid, struct ubik_stat *astat)
153 register afs_int32 code;
155 fd = uphys_open(adbase, afid);
158 code = fstat(fd, &tstat);
163 astat->mtime = tstat.st_mtime;
164 code = tstat.st_size - HDRSIZE;
173 uphys_read(register struct ubik_dbase *adbase, afs_int32 afile,
174 register char *abuffer, afs_int32 apos, afs_int32 alength)
177 register afs_int32 code;
179 fd = uphys_open(adbase, afile);
182 code = lseek(fd, apos + HDRSIZE, 0);
187 code = read(fd, abuffer, alength);
193 uphys_write(register struct ubik_dbase *adbase, afs_int32 afile,
194 register char *abuffer, afs_int32 apos, afs_int32 alength)
197 register afs_int32 code;
200 fd = uphys_open(adbase, afile);
203 code = lseek(fd, apos + HDRSIZE, 0);
208 length = write(fd, abuffer, alength);
209 code = uphys_close(fd);
217 uphys_truncate(register struct ubik_dbase *adbase, afs_int32 afile,
220 register afs_int32 code, fd;
221 fd = uphys_open(adbase, afile);
224 code = ftruncate(fd, asize + HDRSIZE);
229 /* get number of dbase files */
231 uphys_getnfiles(register struct ubik_dbase *adbase)
233 /* really should scan dir for data */
237 /* get database label, with aversion in host order */
239 uphys_getlabel(register struct ubik_dbase *adbase, afs_int32 afile,
240 struct ubik_version *aversion)
242 struct ubik_hdr thdr;
243 register afs_int32 code, fd;
245 fd = uphys_open(adbase, afile);
248 code = read(fd, &thdr, sizeof(thdr));
249 if (code != sizeof(thdr)) {
253 aversion->epoch = ntohl(thdr.version.epoch);
254 aversion->counter = ntohl(thdr.version.counter);
259 /* label database, with aversion in host order */
261 uphys_setlabel(register struct ubik_dbase *adbase, afs_int32 afile,
262 struct ubik_version *aversion)
264 struct ubik_hdr thdr;
265 register afs_int32 code, fd;
267 fd = uphys_open(adbase, afile);
270 thdr.version.epoch = htonl(aversion->epoch);
271 thdr.version.counter = htonl(aversion->counter);
272 thdr.magic = htonl(UBIK_MAGIC);
273 thdr.size = htons(HDRSIZE);
274 code = write(fd, &thdr, sizeof(thdr));
275 fsync(fd); /* preserve over crash */
277 if (code != sizeof(thdr)) {
284 uphys_sync(register struct ubik_dbase *adbase, afs_int32 afile)
286 register afs_int32 code, fd;
287 fd = uphys_open(adbase, afile);