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(struct ubik_trans *ut, afs_int32 pos, void *buff, afs_int32 len)
82 if (((pos < sizeof(db.h)) && (buff != (char *)&db.h + pos))
83 || (pos >= ntohl(db.h.eofPtr))) {
84 Log("dbwrite: Illegal attempt to write at location 0 or past EOF\n");
88 code = ubik_Seek(ut, 0, pos);
90 LogError(code, "dbwrite: ubik_Seek to %d failed\n", pos);
93 code = ubik_Write(ut, buff, len);
95 LogError(code, "dbwrite: ubik_Write failed\n");
100 if (((++pollCount) % 4) == 0) { /* Poll every 4 reads/writes */
107 /* same thing for read */
110 dbread(struct ubik_trans *ut, afs_int32 pos, void *buff, afs_int32 len)
114 if (pos >= ntohl(db.h.eofPtr)) {
115 LogError(0, "dbread: Attempt to read @%d (past EOF)\n", pos);
119 code = ubik_Seek(ut, 0, pos);
121 LogError(code, "dbread: ubik_Seek to %d failed\n", pos);
124 code = ubik_Read(ut, buff, len);
126 LogError(code, "dbread: ubik_Read pos %d, buff %d, len %d\n", pos,
132 if (((++pollCount) % 4) == 0) { /* Poll every 4 reads/writes */
139 /* Same as dbread excepts it does checking */
141 cdbread(struct ubik_trans *ut, int type, afs_int32 pos, void *buff, afs_int32 len)
145 code = checkDiskAddress(pos, type, 0, 0);
147 LogError(code, "cdbread: Bad Address for block %d (addr 0x%x)\n",
152 code = ubik_Seek(ut, 0, pos);
154 LogError(code, "cdbread: ubik_Seek to 0x%x failed\n", pos);
157 code = ubik_Read(ut, buff, len);
159 LogError(code, "cdbread: ubik_Read pos 0x%x, buff %d, len %d\n", pos,
165 if (((++pollCount) % 4) == 0) { /* Poll every 4 reads/writes */
172 /* check that the database has been initialized. Be careful to fail in a safe
173 manner, to avoid bogusly reinitializing the db. */
176 CheckInit(ut, db_init)
177 struct ubik_trans *ut;
178 int (*db_init) (); /* procedure to call if rebuilding DB */
180 register afs_int32 code;
182 /* Don't read header if not necessary */
183 if (!ubik_CacheUpdate(ut))
186 ObtainWriteLock(&db.lock);
188 db.h.eofPtr = htonl(sizeof(db.h)); /* for sanity check in dbread */
189 code = dbread(ut, 0, (char *)&db.h, sizeof(db.h));
193 if ((ntohl(db.h.version) != BUDB_VERSION)
194 || (ntohl(db.h.checkVersion) != BUDB_VERSION)) {
196 if ((ntohl(db.h.version) == 0) || (ntohl(db.h.checkVersion) == 0))
199 LogError(0, "DB version should be %d; Initial = %d; Terminal = %d\n",
200 BUDB_VERSION, ntohl(db.h.version), ntohl(db.h.checkVersion));
204 db.readTime = time(0);
205 ht_Reset(&db.volName);
206 ht_Reset(&db.tapeName);
207 ht_Reset(&db.dumpName);
208 ht_Reset(&db.dumpIden);
211 ReleaseWriteLock(&db.lock);
213 if ((code == UEOF) || (code == BUDB_EMPTY)) {
215 LogDebug(0, "No data base - Building new one\n");
217 /* try to write a good header */
218 memset(&db.h, 0, sizeof(db.h));
219 db.h.version = htonl(BUDB_VERSION);
220 db.h.checkVersion = htonl(BUDB_VERSION);
221 db.h.lastUpdate = db.h.lastDumpId = htonl(time(0));
222 db.h.eofPtr = htonl(sizeof(db.h));
224 /* text ptrs cleared by bzero */
227 code = dbwrite(ut, 0, (char *)&db.h, sizeof(db.h));
229 code = BUDB_IO; /* return the error code */
231 code = db_init(ut); /* initialize the db */
233 LogDebug(0, "No data base\n");
237 LogDebug(0, "I/O Error\n");