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>
27 /* #ifdef AFS_PTHREAD_ENV */
28 #if 0 /* temporary hack - klm */
38 #define UBIK_INTERNALS 1
42 * These routines are called via the proc ptr in the ubik_dbase structure. They provide access to
43 * the physical disk, by converting the file numbers being processed ( >= 0 for user data space, < 0
44 * for ubik system files, such as the log) to actual pathnames to open, read, write, truncate, sync,
49 static struct fdcache {
53 } fdcache[MAXFDCACHE];
55 static char pbuffer[1024];
58 * \warning Beware, when using this function, of the header in front of most files.
61 uphys_open(register struct ubik_dbase *adbase, afs_int32 afid)
67 register struct fdcache *tfd;
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.
136 uphys_close(register int afd)
139 register struct fdcache *tfd;
144 for (i = 0; i < MAXFDCACHE; i++, tfd++) {
146 if (tfd->fileID != -10000) {
150 if (tfd->refCount > 0) {
152 if (tfd->refCount == 0) {
166 uphys_stat(struct ubik_dbase *adbase, afs_int32 afid, struct ubik_stat *astat)
170 register afs_int32 code;
172 fd = uphys_open(adbase, afid);
175 code = fstat(fd, &tstat);
180 astat->mtime = tstat.st_mtime;
181 code = tstat.st_size - HDRSIZE;
190 uphys_read(register struct ubik_dbase *adbase, afs_int32 afile,
191 register char *abuffer, afs_int32 apos, afs_int32 alength)
194 register afs_int32 code;
196 fd = uphys_open(adbase, afile);
199 code = lseek(fd, apos + HDRSIZE, 0);
204 code = read(fd, abuffer, alength);
210 uphys_write(register struct ubik_dbase *adbase, afs_int32 afile,
211 register char *abuffer, afs_int32 apos, afs_int32 alength)
214 register afs_int32 code;
217 fd = uphys_open(adbase, afile);
220 code = lseek(fd, apos + HDRSIZE, 0);
225 length = write(fd, abuffer, alength);
226 code = uphys_close(fd);
234 uphys_truncate(register struct ubik_dbase *adbase, afs_int32 afile,
237 register afs_int32 code, fd;
238 fd = uphys_open(adbase, afile);
241 code = ftruncate(fd, asize + HDRSIZE);
247 * \brief Get number of dbase files.
249 * \todo Really should scan dir for data.
252 uphys_getnfiles(register struct ubik_dbase *adbase)
254 /* really should scan dir for data */
259 * \brief Get database label, with \p aversion in host order.
262 uphys_getlabel(register struct ubik_dbase *adbase, afs_int32 afile,
263 struct ubik_version *aversion)
265 struct ubik_hdr thdr;
266 register afs_int32 code, fd;
268 fd = uphys_open(adbase, afile);
271 code = read(fd, &thdr, sizeof(thdr));
272 if (code != sizeof(thdr)) {
276 aversion->epoch = ntohl(thdr.version.epoch);
277 aversion->counter = ntohl(thdr.version.counter);
283 * \brief Label database, with \p aversion in host order.
286 uphys_setlabel(register struct ubik_dbase *adbase, afs_int32 afile,
287 struct ubik_version *aversion)
289 struct ubik_hdr thdr;
290 register afs_int32 code, fd;
292 fd = uphys_open(adbase, afile);
295 thdr.version.epoch = htonl(aversion->epoch);
296 thdr.version.counter = htonl(aversion->counter);
297 thdr.magic = htonl(UBIK_MAGIC);
298 thdr.size = htons(HDRSIZE);
299 code = write(fd, &thdr, sizeof(thdr));
300 fsync(fd); /* preserve over crash */
302 if (code != sizeof(thdr)) {
309 uphys_sync(register struct ubik_dbase *adbase, afs_int32 afile)
311 register afs_int32 code, fd;
312 fd = uphys_open(adbase, afile);
319 uphys_invalidate(register struct ubik_dbase *adbase, afs_int32 afid)
322 register struct fdcache *tfd;
324 /* scan file descr cache */
325 for (tfd = fdcache, i = 0; i < MAXFDCACHE; i++, tfd++) {
326 if (afid == tfd->fileID) {
327 tfd->fileID = -10000;
328 if (tfd->fd >= 0 && tfd->refCount == 0) {