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>
19 #include <netinet/in.h>
21 #include <sys/types.h>
25 #include <afs/bubasics.h>
26 #include "budb_errs.h"
28 #include "error_macros.h"
29 #include "afs/audit.h"
41 struct memoryDB db; /* really allocate it here */
47 LogError(0, "db_panic: %s\n", reason);
58 memset(&db, 0, sizeof(db));
60 if ((code = InitDBalloc()) || (code = InitDBhash()))
65 /* package up seek and write into one procedure for ease of use */
68 * write a portion of the database
70 * pos - offset into the database (disk address). If this is in the
71 * database header, then buff must be a ptr to a portion of
73 * buff - the information to write
74 * len - size of the write
78 dbwrite(ut, pos, buff, len)
79 struct ubik_trans *ut;
86 if (((pos < sizeof(db.h)) && (buff != (char *)&db.h + pos))
87 || (pos >= ntohl(db.h.eofPtr))) {
88 Log("dbwrite: Illegal attempt to write at location 0 or past EOF\n");
92 code = ubik_Seek(ut, 0, pos);
94 LogError(code, "dbwrite: ubik_Seek to %d failed\n", pos);
97 code = ubik_Write(ut, buff, len);
99 LogError(code, "dbwrite: ubik_Write failed\n");
104 if (((++pollCount) % 4) == 0) { /* Poll every 4 reads/writes */
111 /* same thing for read */
114 dbread(ut, pos, buff, len)
115 struct ubik_trans *ut;
122 if (pos >= ntohl(db.h.eofPtr)) {
123 LogError(0, "dbread: Attempt to read @%d (past EOF)\n", pos);
127 code = ubik_Seek(ut, 0, pos);
129 LogError(code, "dbread: ubik_Seek to %d failed\n", pos);
132 code = ubik_Read(ut, buff, len);
134 LogError(code, "dbread: ubik_Read pos %d, buff %d, len %d\n", pos,
140 if (((++pollCount) % 4) == 0) { /* Poll every 4 reads/writes */
147 /* Same as dbread excepts it does checking */
149 cdbread(ut, type, pos, buff, len)
150 struct ubik_trans *ut;
158 code = checkDiskAddress(pos, type, 0, 0);
160 LogError(code, "cdbread: Bad Address for block %d (addr 0x%x)\n",
165 code = ubik_Seek(ut, 0, pos);
167 LogError(code, "cdbread: ubik_Seek to 0x%x failed\n", pos);
170 code = ubik_Read(ut, buff, len);
172 LogError(code, "cdbread: ubik_Read pos 0x%x, buff %d, len %d\n", pos,
178 if (((++pollCount) % 4) == 0) { /* Poll every 4 reads/writes */
185 /* check that the database has been initialized. Be careful to fail in a safe
186 manner, to avoid bogusly reinitializing the db. */
189 CheckInit(ut, db_init)
190 struct ubik_trans *ut;
191 int (*db_init) (); /* procedure to call if rebuilding DB */
193 register afs_int32 code;
195 /* Don't read header if not necessary */
196 if (!ubik_CacheUpdate(ut))
199 ObtainWriteLock(&db.lock);
201 db.h.eofPtr = htonl(sizeof(db.h)); /* for sanity check in dbread */
202 code = dbread(ut, 0, (char *)&db.h, sizeof(db.h));
206 if ((ntohl(db.h.version) != BUDB_VERSION)
207 || (ntohl(db.h.checkVersion) != BUDB_VERSION)) {
209 if ((ntohl(db.h.version) == 0) || (ntohl(db.h.checkVersion) == 0))
212 LogError(0, "DB version should be %d; Initial = %d; Terminal = %d\n",
213 BUDB_VERSION, ntohl(db.h.version), ntohl(db.h.checkVersion));
217 db.readTime = time(0);
218 ht_Reset(&db.volName);
219 ht_Reset(&db.tapeName);
220 ht_Reset(&db.dumpName);
221 ht_Reset(&db.dumpIden);
224 ReleaseWriteLock(&db.lock);
226 if ((code == UEOF) || (code == BUDB_EMPTY)) {
228 LogDebug(0, "No data base - Building new one\n");
230 /* try to write a good header */
231 memset(&db.h, 0, sizeof(db.h));
232 db.h.version = htonl(BUDB_VERSION);
233 db.h.checkVersion = htonl(BUDB_VERSION);
234 db.h.lastUpdate = db.h.lastDumpId = htonl(time(0));
235 db.h.eofPtr = htonl(sizeof(db.h));
237 /* text ptrs cleared by bzero */
240 code = dbwrite(ut, 0, (char *)&db.h, sizeof(db.h));
242 code = BUDB_IO; /* return the error code */
244 code = db_init(ut); /* initialize the db */
246 LogDebug(0, "No data base\n");
250 LogDebug(0, "I/O Error\n");