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>
18 #include <afs/afsutil.h>
20 #define UBIK_INTERNALS 1
24 * These routines are called via the proc ptr in the ubik_dbase structure. They provide access to
25 * the physical disk, by converting the file numbers being processed ( >= 0 for user data space, < 0
26 * for ubik system files, such as the log) to actual pathnames to open, read, write, truncate, sync,
31 static struct fdcache {
35 } fdcache[MAXFDCACHE];
37 /* Cache a stdio handle for a given database file, for uphys_buf_append
38 * operations. We only use buf_append for one file at a time, so only try to
39 * cache a single file handle, since that's all we should need. */
40 static struct buf_fdcache {
45 static char pbuffer[1024];
47 static int uphys_buf_flush(struct ubik_dbase *adbase, afs_int32 afid);
50 * \warning Beware, when using this function, of the header in front of most files.
53 uphys_open(struct ubik_dbase *adbase, afs_int32 afid)
59 struct fdcache *bestfd;
61 /* initialize package */
65 for (i = 0; i < MAXFDCACHE; tfd++, i++) {
66 tfd->fd = -1; /* invalid value */
67 tfd->fileID = -10000; /* invalid value */
72 /* scan file descr cache */
73 for (tfd = fdcache, i = 0; i < MAXFDCACHE; i++, tfd++) {
74 if (afid == tfd->fileID && tfd->refCount == 0) { /* don't use open fd */
80 /* not found, open it and try to enter in cache */
81 snprintf(pbuffer, sizeof(pbuffer), "%s.DB%s%d", adbase->pathName,
82 (afid<0)?"SYS":"", (afid<0)?-afid:afid);
83 fd = open(pbuffer, O_CREAT | O_RDWR, 0600);
85 /* try opening read-only */
86 fd = open(pbuffer, O_RDONLY, 0);
91 /* enter it in the cache */
94 for (i = 0; i < MAXFDCACHE; i++, tfd++) { /* look for empty slot */
100 if (!bestfd) { /* look for reclaimable slot */
102 for (i = 0; i < MAXFDCACHE; i++, tfd++) {
103 if (tfd->refCount == 0) {
109 if (bestfd) { /* found a usable slot */
114 tfd->refCount = 1; /* us */
118 /* finally, we're done */
123 * \brief Close the file, maintaining ref count in cache structure.
134 for (i = 0; i < MAXFDCACHE; i++, tfd++) {
135 if (tfd->fd == afd) {
136 if (tfd->fileID != -10000) {
140 if (tfd->refCount > 0) {
142 if (tfd->refCount == 0) {
157 uphys_stat(struct ubik_dbase *adbase, afs_int32 afid, struct ubik_stat *astat)
163 fd = uphys_open(adbase, afid);
166 code = fstat(fd, &tstat);
171 code = tstat.st_size - HDRSIZE;
180 uphys_read(struct ubik_dbase *adbase, afs_int32 afile,
181 void *abuffer, afs_int32 apos, afs_int32 alength)
186 fd = uphys_open(adbase, afile);
189 code = lseek(fd, apos + HDRSIZE, 0);
194 code = read(fd, abuffer, alength);
200 uphys_write(struct ubik_dbase *adbase, afs_int32 afile,
201 void *abuffer, afs_int32 apos, afs_int32 alength)
207 fd = uphys_open(adbase, afile);
210 code = lseek(fd, apos + HDRSIZE, 0);
215 length = write(fd, abuffer, alength);
216 code = uphys_close(fd);
224 uphys_truncate(struct ubik_dbase *adbase, afs_int32 afile,
229 /* Just in case there's memory-buffered data for this file, flush it before
231 if (uphys_buf_flush(adbase, afile) < 0) {
235 fd = uphys_open(adbase, afile);
238 code = ftruncate(fd, asize + HDRSIZE);
244 * \brief Get number of dbase files.
246 * \todo Really should scan dir for data.
249 uphys_getnfiles(struct ubik_dbase *adbase)
251 /* really should scan dir for data */
256 * \brief Get database label, with \p aversion in host order.
259 uphys_getlabel(struct ubik_dbase *adbase, afs_int32 afile,
260 struct ubik_version *aversion)
262 struct ubik_hdr thdr;
265 fd = uphys_open(adbase, afile);
268 if (lseek(fd, 0, 0) < 0) {
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);
297 memset(&thdr, 0, sizeof(thdr));
299 thdr.version.epoch = htonl(aversion->epoch);
300 thdr.version.counter = htonl(aversion->counter);
301 thdr.magic = htonl(UBIK_MAGIC);
302 thdr.size = htons(HDRSIZE);
303 if (lseek(fd, 0, 0) < 0) {
307 code = write(fd, &thdr, sizeof(thdr));
308 fsync(fd); /* preserve over crash */
310 if (code != sizeof(thdr)) {
317 uphys_sync(struct ubik_dbase *adbase, afs_int32 afile)
321 /* Flush any in-memory data, so we can sync it. */
322 if (uphys_buf_flush(adbase, afile) < 0) {
326 fd = uphys_open(adbase, afile);
333 uphys_invalidate(struct ubik_dbase *adbase, afs_int32 afid)
338 /* scan file descr cache */
339 for (tfd = fdcache, i = 0; i < MAXFDCACHE; i++, tfd++) {
340 if (afid == tfd->fileID) {
341 tfd->fileID = -10000;
342 if (tfd->fd >= 0 && tfd->refCount == 0) {
352 uphys_buf_append_open(struct ubik_dbase *adbase, afs_int32 afid)
354 /* If we have a cached handle open for this file, just return it. */
355 if (buf_fdcache.stream && buf_fdcache.fileID == afid) {
356 return buf_fdcache.stream;
359 /* Otherwise, close the existing handle, and open a new handle for the
362 if (buf_fdcache.stream) {
363 fclose(buf_fdcache.stream);
366 snprintf(pbuffer, sizeof(pbuffer), "%s.DB%s%d", adbase->pathName,
367 (afid<0)?"SYS":"", (afid<0)?-afid:afid);
368 buf_fdcache.stream = fopen(pbuffer, "a");
369 buf_fdcache.fileID = afid;
370 return buf_fdcache.stream;
374 uphys_buf_flush(struct ubik_dbase *adbase, afs_int32 afid)
376 if (buf_fdcache.stream && buf_fdcache.fileID == afid) {
377 int code = fflush(buf_fdcache.stream);
385 /* Append the given data to the given database file, allowing the data to be
386 * buffered in memory. */
388 uphys_buf_append(struct ubik_dbase *adbase, afs_int32 afid, void *adata,
394 stream = uphys_buf_append_open(adbase, afid);
399 code = fwrite(adata, alength, 1, stream);