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>
14 #include <sys/types.h>
25 #include <netinet/in.h>
29 /* #ifdef AFS_PTHREAD_ENV */
30 #if 0 /* temporary hack - klm */
37 #include <afs/afsutil.h>
39 #define UBIK_INTERNALS 1
43 * These routines are called via the proc ptr in the ubik_dbase structure. They provide access to
44 * the physical disk, by converting the file numbers being processed ( >= 0 for user data space, < 0
45 * for ubik system files, such as the log) to actual pathnames to open, read, write, truncate, sync,
50 static struct fdcache {
54 } fdcache[MAXFDCACHE];
56 static char pbuffer[1024];
59 * \warning Beware, when using this function, of the header in front of most files.
62 uphys_open(struct ubik_dbase *adbase, afs_int32 afid)
68 struct fdcache *bestfd;
70 /* initialize package */
74 for (i = 0; i < MAXFDCACHE; tfd++, i++) {
75 tfd->fd = -1; /* invalid value */
76 tfd->fileID = -10000; /* invalid value */
81 /* scan file descr cache */
82 for (tfd = fdcache, i = 0; i < MAXFDCACHE; i++, tfd++) {
83 if (afid == tfd->fileID && tfd->refCount == 0) { /* don't use open fd */
84 lseek(tfd->fd, 0, 0); /* reset ptr just like open would have */
90 /* not found, open it and try to enter in cache */
91 afs_snprintf(pbuffer, sizeof(pbuffer), "%s.DB%s%d", adbase->pathName,
92 (afid<0)?"SYS":"", (afid<0)?-afid:afid);
93 fd = open(pbuffer, O_CREAT | O_RDWR, 0600);
95 /* try opening read-only */
96 fd = open(pbuffer, O_RDONLY, 0);
101 /* enter it in the cache */
104 for (i = 0; i < MAXFDCACHE; i++, tfd++) { /* look for empty slot */
110 if (!bestfd) { /* look for reclaimable slot */
112 for (i = 0; i < MAXFDCACHE; i++, tfd++) {
113 if (tfd->refCount == 0) {
119 if (bestfd) { /* found a usable slot */
124 tfd->refCount = 1; /* us */
128 /* finally, we're done */
133 * \brief Close the file, maintaining ref count in cache structure.
144 for (i = 0; i < MAXFDCACHE; i++, tfd++) {
145 if (tfd->fd == afd) {
146 if (tfd->fileID != -10000) {
150 if (tfd->refCount > 0) {
152 if (tfd->refCount == 0) {
167 uphys_stat(struct ubik_dbase *adbase, afs_int32 afid, struct ubik_stat *astat)
173 fd = uphys_open(adbase, afid);
176 code = fstat(fd, &tstat);
181 astat->mtime = tstat.st_mtime;
182 code = tstat.st_size - HDRSIZE;
191 uphys_read(struct ubik_dbase *adbase, afs_int32 afile,
192 void *abuffer, afs_int32 apos, afs_int32 alength)
197 fd = uphys_open(adbase, afile);
200 code = lseek(fd, apos + HDRSIZE, 0);
205 code = read(fd, abuffer, alength);
211 uphys_write(struct ubik_dbase *adbase, afs_int32 afile,
212 void *abuffer, afs_int32 apos, afs_int32 alength)
218 fd = uphys_open(adbase, afile);
221 code = lseek(fd, apos + HDRSIZE, 0);
226 length = write(fd, abuffer, alength);
227 code = uphys_close(fd);
235 uphys_truncate(struct ubik_dbase *adbase, afs_int32 afile,
239 fd = uphys_open(adbase, afile);
242 code = ftruncate(fd, asize + HDRSIZE);
248 * \brief Get number of dbase files.
250 * \todo Really should scan dir for data.
253 uphys_getnfiles(struct ubik_dbase *adbase)
255 /* really should scan dir for data */
260 * \brief Get database label, with \p aversion in host order.
263 uphys_getlabel(struct ubik_dbase *adbase, afs_int32 afile,
264 struct ubik_version *aversion)
266 struct ubik_hdr thdr;
269 fd = uphys_open(adbase, afile);
272 code = read(fd, &thdr, sizeof(thdr));
273 if (code != sizeof(thdr)) {
277 aversion->epoch = ntohl(thdr.version.epoch);
278 aversion->counter = ntohl(thdr.version.counter);
284 * \brief Label database, with \p aversion in host order.
287 uphys_setlabel(struct ubik_dbase *adbase, afs_int32 afile,
288 struct ubik_version *aversion)
290 struct ubik_hdr thdr;
293 fd = uphys_open(adbase, afile);
296 thdr.version.epoch = htonl(aversion->epoch);
297 thdr.version.counter = htonl(aversion->counter);
298 thdr.magic = htonl(UBIK_MAGIC);
299 thdr.size = htons(HDRSIZE);
300 code = write(fd, &thdr, sizeof(thdr));
301 fsync(fd); /* preserve over crash */
303 if (code != sizeof(thdr)) {
310 uphys_sync(struct ubik_dbase *adbase, afs_int32 afile)
313 fd = uphys_open(adbase, afile);
320 uphys_invalidate(struct ubik_dbase *adbase, afs_int32 afid)
325 /* scan file descr cache */
326 for (tfd = fdcache, i = 0; i < MAXFDCACHE; i++, tfd++) {
327 if (afid == tfd->fileID) {
328 tfd->fileID = -10000;
329 if (tfd->fd >= 0 && tfd->refCount == 0) {