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 */
92 #ifndef AFS_PTHREAD_ENV
100 /* same thing for read */
103 dbread(struct ubik_trans *ut, afs_int32 pos, void *buff, afs_int32 len)
107 if (pos >= ntohl(db.h.eofPtr)) {
108 LogError(0, "dbread: Attempt to read @%d (past EOF)\n", pos);
112 code = ubik_Seek(ut, 0, pos);
114 LogError(code, "dbread: ubik_Seek to %d failed\n", pos);
117 code = ubik_Read(ut, buff, len);
119 LogError(code, "dbread: ubik_Read pos %d, buff %d, len %d\n", pos,
125 if (((++pollCount) % 4) == 0) { /* Poll every 4 reads/writes */
126 #ifndef AFS_PTHREAD_ENV
134 /* Same as dbread excepts it does checking */
136 cdbread(struct ubik_trans *ut, int type, afs_int32 pos, void *buff, afs_int32 len)
140 code = checkDiskAddress(pos, type, 0, 0);
142 LogError(code, "cdbread: Bad Address for block %d (addr 0x%x)\n",
147 code = ubik_Seek(ut, 0, pos);
149 LogError(code, "cdbread: ubik_Seek to 0x%x failed\n", pos);
152 code = ubik_Read(ut, buff, len);
154 LogError(code, "cdbread: ubik_Read pos 0x%x, buff %d, len %d\n", pos,
160 if (((++pollCount) % 4) == 0) { /* Poll every 4 reads/writes */
161 #ifndef AFS_PTHREAD_ENV
169 /* check that the database has been initialized. Be careful to fail in a safe
170 manner, to avoid bogusly reinitializing the db. */
173 CheckInit(ut, db_init)
174 struct ubik_trans *ut;
175 int (*db_init) (); /* procedure to call if rebuilding DB */
177 register afs_int32 code;
179 /* Don't read header if not necessary */
180 if (!ubik_CacheUpdate(ut))
183 ObtainWriteLock(&db.lock);
185 db.h.eofPtr = htonl(sizeof(db.h)); /* for sanity check in dbread */
186 code = dbread(ut, 0, (char *)&db.h, sizeof(db.h));
190 if ((ntohl(db.h.version) != BUDB_VERSION)
191 || (ntohl(db.h.checkVersion) != BUDB_VERSION)) {
193 if ((ntohl(db.h.version) == 0) || (ntohl(db.h.checkVersion) == 0))
196 LogError(0, "DB version should be %d; Initial = %d; Terminal = %d\n",
197 BUDB_VERSION, ntohl(db.h.version), ntohl(db.h.checkVersion));
201 db.readTime = time(0);
202 ht_Reset(&db.volName);
203 ht_Reset(&db.tapeName);
204 ht_Reset(&db.dumpName);
205 ht_Reset(&db.dumpIden);
208 ReleaseWriteLock(&db.lock);
210 if ((code == UEOF) || (code == BUDB_EMPTY)) {
212 LogDebug(0, "No data base - Building new one\n");
214 /* try to write a good header */
215 memset(&db.h, 0, sizeof(db.h));
216 db.h.version = htonl(BUDB_VERSION);
217 db.h.checkVersion = htonl(BUDB_VERSION);
218 db.h.lastUpdate = db.h.lastDumpId = htonl(time(0));
219 db.h.eofPtr = htonl(sizeof(db.h));
221 /* text ptrs cleared by bzero */
224 code = dbwrite(ut, 0, (char *)&db.h, sizeof(db.h));
226 code = BUDB_IO; /* return the error code */
228 code = db_init(ut); /* initialize the db */
230 LogDebug(0, "No data base\n");
234 LogDebug(0, "I/O Error\n");