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>
17 #include <netinet/in.h>
19 #include <sys/types.h>
22 #include <afs/bubasics.h>
23 #include "budb_errs.h"
25 #include "error_macros.h"
26 #include "budb_internal.h"
27 #include "afs/audit.h"
31 struct memoryDB db; /* really allocate it here */
34 db_panic(char *reason)
36 LogError(0, "db_panic: %s\n", reason);
47 memset(&db, 0, sizeof(db));
49 if ((code = InitDBalloc()) || (code = InitDBhash()))
54 /* package up seek and write into one procedure for ease of use */
57 * write a portion of the database
59 * pos - offset into the database (disk address). If this is in the
60 * database header, then buff must be a ptr to a portion of
62 * buff - the information to write
63 * len - size of the write
67 dbwrite(struct ubik_trans *ut, afs_int32 pos, void *buff, afs_int32 len)
71 if (((pos < sizeof(db.h)) && (buff != (char *)&db.h + pos))
72 || (pos >= ntohl(db.h.eofPtr))) {
73 Log("dbwrite: Illegal attempt to write at location 0 or past EOF\n");
77 code = ubik_Seek(ut, 0, pos);
79 LogError(code, "dbwrite: ubik_Seek to %d failed\n", pos);
82 code = ubik_Write(ut, buff, len);
84 LogError(code, "dbwrite: ubik_Write failed\n");
89 if (((++pollCount) % 4) == 0) { /* Poll every 4 reads/writes */
90 #ifndef AFS_PTHREAD_ENV
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 */
124 #ifndef AFS_PTHREAD_ENV
132 /* Same as dbread excepts it does checking */
134 cdbread(struct ubik_trans *ut, int type, afs_int32 pos, void *buff, afs_int32 len)
138 code = checkDiskAddress(pos, type, 0, 0);
140 LogError(code, "cdbread: Bad Address for block %d (addr 0x%x)\n",
145 code = ubik_Seek(ut, 0, pos);
147 LogError(code, "cdbread: ubik_Seek to 0x%x failed\n", pos);
150 code = ubik_Read(ut, buff, len);
152 LogError(code, "cdbread: ubik_Read pos 0x%x, buff %d, len %d\n", pos,
158 if (((++pollCount) % 4) == 0) { /* Poll every 4 reads/writes */
159 #ifndef AFS_PTHREAD_ENV
167 /* check that the database has been initialized. Be careful to fail in a safe
168 manner, to avoid bogusly reinitializing the db. */
171 CheckInit(struct ubik_trans *ut,
172 int (*db_init) (struct ubik_trans *ut)) /* call if rebuilding DB */
174 register afs_int32 code;
176 /* Don't read header if not necessary */
177 if (!ubik_CacheUpdate(ut))
180 ObtainWriteLock(&db.lock);
182 db.h.eofPtr = htonl(sizeof(db.h)); /* for sanity check in dbread */
183 code = dbread(ut, 0, (char *)&db.h, sizeof(db.h));
187 if ((ntohl(db.h.version) != BUDB_VERSION)
188 || (ntohl(db.h.checkVersion) != BUDB_VERSION)) {
190 if ((ntohl(db.h.version) == 0) || (ntohl(db.h.checkVersion) == 0))
193 LogError(0, "DB version should be %d; Initial = %d; Terminal = %d\n",
194 BUDB_VERSION, ntohl(db.h.version), ntohl(db.h.checkVersion));
198 db.readTime = time(0);
199 ht_Reset(&db.volName);
200 ht_Reset(&db.tapeName);
201 ht_Reset(&db.dumpName);
202 ht_Reset(&db.dumpIden);
205 ReleaseWriteLock(&db.lock);
207 if ((code == UEOF) || (code == BUDB_EMPTY)) {
209 LogDebug(0, "No data base - Building new one\n");
211 /* try to write a good header */
212 memset(&db.h, 0, sizeof(db.h));
213 db.h.version = htonl(BUDB_VERSION);
214 db.h.checkVersion = htonl(BUDB_VERSION);
215 db.h.lastUpdate = db.h.lastDumpId = htonl(time(0));
216 db.h.eofPtr = htonl(sizeof(db.h));
218 /* text ptrs cleared by bzero */
221 code = dbwrite(ut, 0, (char *)&db.h, sizeof(db.h));
223 code = BUDB_IO; /* return the error code */
225 code = db_init(ut); /* initialize the db */
227 LogDebug(0, "No data base\n");
231 LogDebug(0, "I/O Error\n");