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"
40 struct memoryDB db; /* really allocate it here */
46 LogError(0, "db_panic: %s\n", reason);
57 memset(&db, 0, sizeof(db));
59 if ((code = InitDBalloc()) || (code = InitDBhash()))
64 /* package up seek and write into one procedure for ease of use */
67 * write a portion of the database
69 * pos - offset into the database (disk address). If this is in the
70 * database header, then buff must be a ptr to a portion of
72 * buff - the information to write
73 * len - size of the write
77 dbwrite(struct ubik_trans *ut, afs_int32 pos, void *buff, afs_int32 len)
81 if (((pos < sizeof(db.h)) && (buff != (char *)&db.h + pos))
82 || (pos >= ntohl(db.h.eofPtr))) {
83 Log("dbwrite: Illegal attempt to write at location 0 or past EOF\n");
87 code = ubik_Seek(ut, 0, pos);
89 LogError(code, "dbwrite: ubik_Seek to %d failed\n", pos);
92 code = ubik_Write(ut, buff, len);
94 LogError(code, "dbwrite: ubik_Write failed\n");
99 if (((++pollCount) % 4) == 0) { /* Poll every 4 reads/writes */
106 /* same thing for read */
109 dbread(struct ubik_trans *ut, afs_int32 pos, void *buff, afs_int32 len)
113 if (pos >= ntohl(db.h.eofPtr)) {
114 LogError(0, "dbread: Attempt to read @%d (past EOF)\n", pos);
118 code = ubik_Seek(ut, 0, pos);
120 LogError(code, "dbread: ubik_Seek to %d failed\n", pos);
123 code = ubik_Read(ut, buff, len);
125 LogError(code, "dbread: ubik_Read pos %d, buff %d, len %d\n", pos,
131 if (((++pollCount) % 4) == 0) { /* Poll every 4 reads/writes */
138 /* Same as dbread excepts it does checking */
140 cdbread(struct ubik_trans *ut, int type, afs_int32 pos, void *buff, afs_int32 len)
144 code = checkDiskAddress(pos, type, 0, 0);
146 LogError(code, "cdbread: Bad Address for block %d (addr 0x%x)\n",
151 code = ubik_Seek(ut, 0, pos);
153 LogError(code, "cdbread: ubik_Seek to 0x%x failed\n", pos);
156 code = ubik_Read(ut, buff, len);
158 LogError(code, "cdbread: ubik_Read pos 0x%x, buff %d, len %d\n", pos,
164 if (((++pollCount) % 4) == 0) { /* Poll every 4 reads/writes */
171 /* check that the database has been initialized. Be careful to fail in a safe
172 manner, to avoid bogusly reinitializing the db. */
175 CheckInit(ut, db_init)
176 struct ubik_trans *ut;
177 int (*db_init) (); /* procedure to call if rebuilding DB */
179 register afs_int32 code;
181 /* Don't read header if not necessary */
182 if (!ubik_CacheUpdate(ut))
185 ObtainWriteLock(&db.lock);
187 db.h.eofPtr = htonl(sizeof(db.h)); /* for sanity check in dbread */
188 code = dbread(ut, 0, (char *)&db.h, sizeof(db.h));
192 if ((ntohl(db.h.version) != BUDB_VERSION)
193 || (ntohl(db.h.checkVersion) != BUDB_VERSION)) {
195 if ((ntohl(db.h.version) == 0) || (ntohl(db.h.checkVersion) == 0))
198 LogError(0, "DB version should be %d; Initial = %d; Terminal = %d\n",
199 BUDB_VERSION, ntohl(db.h.version), ntohl(db.h.checkVersion));
203 db.readTime = time(0);
204 ht_Reset(&db.volName);
205 ht_Reset(&db.tapeName);
206 ht_Reset(&db.dumpName);
207 ht_Reset(&db.dumpIden);
210 ReleaseWriteLock(&db.lock);
212 if ((code == UEOF) || (code == BUDB_EMPTY)) {
214 LogDebug(0, "No data base - Building new one\n");
216 /* try to write a good header */
217 memset(&db.h, 0, sizeof(db.h));
218 db.h.version = htonl(BUDB_VERSION);
219 db.h.checkVersion = htonl(BUDB_VERSION);
220 db.h.lastUpdate = db.h.lastDumpId = htonl(time(0));
221 db.h.eofPtr = htonl(sizeof(db.h));
223 /* text ptrs cleared by bzero */
226 code = dbwrite(ut, 0, (char *)&db.h, sizeof(db.h));
228 code = BUDB_IO; /* return the error code */
230 code = db_init(ut); /* initialize the db */
232 LogDebug(0, "No data base\n");
236 LogDebug(0, "I/O Error\n");