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>
24 #include <afs/bubasics.h>
25 #include "budb_errs.h"
27 #include "error_macros.h"
28 #include "afs/audit.h"
32 struct memoryDB db; /* really allocate it here */
38 LogError(0, "db_panic: %s\n", reason);
49 memset(&db, 0, sizeof(db));
51 if ((code = InitDBalloc()) || (code = InitDBhash()))
56 /* package up seek and write into one procedure for ease of use */
59 * write a portion of the database
61 * pos - offset into the database (disk address). If this is in the
62 * database header, then buff must be a ptr to a portion of
64 * buff - the information to write
65 * len - size of the write
69 dbwrite(struct ubik_trans *ut, afs_int32 pos, void *buff, afs_int32 len)
73 if (((pos < sizeof(db.h)) && (buff != (char *)&db.h + pos))
74 || (pos >= ntohl(db.h.eofPtr))) {
75 Log("dbwrite: Illegal attempt to write at location 0 or past EOF\n");
79 code = ubik_Seek(ut, 0, pos);
81 LogError(code, "dbwrite: ubik_Seek to %d failed\n", pos);
84 code = ubik_Write(ut, buff, len);
86 LogError(code, "dbwrite: ubik_Write failed\n");
91 if (((++pollCount) % 4) == 0) { /* Poll every 4 reads/writes */
98 /* same thing for read */
101 dbread(struct ubik_trans *ut, afs_int32 pos, void *buff, afs_int32 len)
105 if (pos >= ntohl(db.h.eofPtr)) {
106 LogError(0, "dbread: Attempt to read @%d (past EOF)\n", pos);
110 code = ubik_Seek(ut, 0, pos);
112 LogError(code, "dbread: ubik_Seek to %d failed\n", pos);
115 code = ubik_Read(ut, buff, len);
117 LogError(code, "dbread: ubik_Read pos %d, buff %d, len %d\n", pos,
123 if (((++pollCount) % 4) == 0) { /* Poll every 4 reads/writes */
130 /* Same as dbread excepts it does checking */
132 cdbread(struct ubik_trans *ut, int type, afs_int32 pos, void *buff, afs_int32 len)
136 code = checkDiskAddress(pos, type, 0, 0);
138 LogError(code, "cdbread: Bad Address for block %d (addr 0x%x)\n",
143 code = ubik_Seek(ut, 0, pos);
145 LogError(code, "cdbread: ubik_Seek to 0x%x failed\n", pos);
148 code = ubik_Read(ut, buff, len);
150 LogError(code, "cdbread: ubik_Read pos 0x%x, buff %d, len %d\n", pos,
156 if (((++pollCount) % 4) == 0) { /* Poll every 4 reads/writes */
163 /* check that the database has been initialized. Be careful to fail in a safe
164 manner, to avoid bogusly reinitializing the db. */
167 CheckInit(ut, db_init)
168 struct ubik_trans *ut;
169 int (*db_init) (); /* procedure to call if rebuilding DB */
171 register afs_int32 code;
173 /* Don't read header if not necessary */
174 if (!ubik_CacheUpdate(ut))
177 ObtainWriteLock(&db.lock);
179 db.h.eofPtr = htonl(sizeof(db.h)); /* for sanity check in dbread */
180 code = dbread(ut, 0, (char *)&db.h, sizeof(db.h));
184 if ((ntohl(db.h.version) != BUDB_VERSION)
185 || (ntohl(db.h.checkVersion) != BUDB_VERSION)) {
187 if ((ntohl(db.h.version) == 0) || (ntohl(db.h.checkVersion) == 0))
190 LogError(0, "DB version should be %d; Initial = %d; Terminal = %d\n",
191 BUDB_VERSION, ntohl(db.h.version), ntohl(db.h.checkVersion));
195 db.readTime = time(0);
196 ht_Reset(&db.volName);
197 ht_Reset(&db.tapeName);
198 ht_Reset(&db.dumpName);
199 ht_Reset(&db.dumpIden);
202 ReleaseWriteLock(&db.lock);
204 if ((code == UEOF) || (code == BUDB_EMPTY)) {
206 LogDebug(0, "No data base - Building new one\n");
208 /* try to write a good header */
209 memset(&db.h, 0, sizeof(db.h));
210 db.h.version = htonl(BUDB_VERSION);
211 db.h.checkVersion = htonl(BUDB_VERSION);
212 db.h.lastUpdate = db.h.lastDumpId = htonl(time(0));
213 db.h.eofPtr = htonl(sizeof(db.h));
215 /* text ptrs cleared by bzero */
218 code = dbwrite(ut, 0, (char *)&db.h, sizeof(db.h));
220 code = BUDB_IO; /* return the error code */
222 code = db_init(ut); /* initialize the db */
224 LogDebug(0, "No data base\n");
228 LogDebug(0, "I/O Error\n");