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>
15 #include <sys/types.h>
26 #include <netinet/in.h>
30 /* #ifdef AFS_PTHREAD_ENV */
31 #if 0 /* temporary hack - klm */
38 #include <afs/afsutil.h>
40 #define UBIK_INTERNALS 1
44 * These routines are called via the proc ptr in the ubik_dbase structure. They provide access to
45 * the physical disk, by converting the file numbers being processed ( >= 0 for user data space, < 0
46 * for ubik system files, such as the log) to actual pathnames to open, read, write, truncate, sync,
51 static struct fdcache {
55 } fdcache[MAXFDCACHE];
57 static char pbuffer[1024];
60 * \warning Beware, when using this function, of the header in front of most files.
63 uphys_open(struct ubik_dbase *adbase, afs_int32 afid)
69 struct fdcache *bestfd;
71 /* initialize package */
75 for (i = 0; i < MAXFDCACHE; tfd++, i++) {
76 tfd->fd = -1; /* invalid value */
77 tfd->fileID = -10000; /* invalid value */
82 /* scan file descr cache */
83 for (tfd = fdcache, i = 0; i < MAXFDCACHE; i++, tfd++) {
84 if (afid == tfd->fileID && tfd->refCount == 0) { /* don't use open fd */
85 lseek(tfd->fd, 0, 0); /* reset ptr just like open would have */
91 /* not found, open it and try to enter in cache */
92 afs_snprintf(pbuffer, sizeof(pbuffer), "%s.DB%s%d", adbase->pathName,
93 (afid<0)?"SYS":"", (afid<0)?-afid:afid);
94 fd = open(pbuffer, O_CREAT | O_RDWR, 0600);
96 /* try opening read-only */
97 fd = open(pbuffer, O_RDONLY, 0);
102 /* enter it in the cache */
105 for (i = 0; i < MAXFDCACHE; i++, tfd++) { /* look for empty slot */
111 if (!bestfd) { /* look for reclaimable slot */
113 for (i = 0; i < MAXFDCACHE; i++, tfd++) {
114 if (tfd->refCount == 0) {
120 if (bestfd) { /* found a usable slot */
125 tfd->refCount = 1; /* us */
129 /* finally, we're done */
134 * \brief Close the file, maintaining ref count in cache structure.
145 for (i = 0; i < MAXFDCACHE; i++, tfd++) {
146 if (tfd->fd == afd) {
147 if (tfd->fileID != -10000) {
151 if (tfd->refCount > 0) {
153 if (tfd->refCount == 0) {
168 uphys_stat(struct ubik_dbase *adbase, afs_int32 afid, struct ubik_stat *astat)
174 fd = uphys_open(adbase, afid);
177 code = fstat(fd, &tstat);
182 astat->mtime = tstat.st_mtime;
183 code = tstat.st_size - HDRSIZE;
192 uphys_read(struct ubik_dbase *adbase, afs_int32 afile,
193 void *abuffer, afs_int32 apos, afs_int32 alength)
198 fd = uphys_open(adbase, afile);
201 code = lseek(fd, apos + HDRSIZE, 0);
206 code = read(fd, abuffer, alength);
212 uphys_write(struct ubik_dbase *adbase, afs_int32 afile,
213 void *abuffer, afs_int32 apos, afs_int32 alength)
219 fd = uphys_open(adbase, afile);
222 code = lseek(fd, apos + HDRSIZE, 0);
227 length = write(fd, abuffer, alength);
228 code = uphys_close(fd);
236 uphys_truncate(struct ubik_dbase *adbase, afs_int32 afile,
240 fd = uphys_open(adbase, afile);
243 code = ftruncate(fd, asize + HDRSIZE);
249 * \brief Get number of dbase files.
251 * \todo Really should scan dir for data.
254 uphys_getnfiles(struct ubik_dbase *adbase)
256 /* really should scan dir for data */
261 * \brief Get database label, with \p aversion in host order.
264 uphys_getlabel(struct ubik_dbase *adbase, afs_int32 afile,
265 struct ubik_version *aversion)
267 struct ubik_hdr thdr;
270 fd = uphys_open(adbase, afile);
273 code = read(fd, &thdr, sizeof(thdr));
274 if (code != sizeof(thdr)) {
278 aversion->epoch = ntohl(thdr.version.epoch);
279 aversion->counter = ntohl(thdr.version.counter);
285 * \brief Label database, with \p aversion in host order.
288 uphys_setlabel(struct ubik_dbase *adbase, afs_int32 afile,
289 struct ubik_version *aversion)
291 struct ubik_hdr thdr;
294 fd = uphys_open(adbase, afile);
297 thdr.version.epoch = htonl(aversion->epoch);
298 thdr.version.counter = htonl(aversion->counter);
299 thdr.magic = htonl(UBIK_MAGIC);
300 thdr.size = htons(HDRSIZE);
301 code = write(fd, &thdr, sizeof(thdr));
302 fsync(fd); /* preserve over crash */
304 if (code != sizeof(thdr)) {
311 uphys_sync(struct ubik_dbase *adbase, afs_int32 afile)
314 fd = uphys_open(adbase, afile);
321 uphys_invalidate(struct ubik_dbase *adbase, afs_int32 afid)
326 /* scan file descr cache */
327 for (tfd = fdcache, i = 0; i < MAXFDCACHE; i++, tfd++) {
328 if (afid == tfd->fileID) {
329 tfd->fileID = -10000;
330 if (tfd->fd >= 0 && tfd->refCount == 0) {