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>
22 #include <afs/cellconfig.h>
23 #include <afs/audit.h>
24 #include <afs/afsutil.h>
27 #include "budb_errs.h"
29 #include "budb_internal.h"
30 #include "error_macros.h"
33 afs_int32 DumpDB(struct rx_call *, int, afs_int32, charListT *, afs_int32 *);
34 afs_int32 RestoreDbHeader(struct rx_call *, struct DbHeader *);
35 void *dumpWatcher(void *);
37 /* dump ubik database - interface routines */
40 * no checking for now.
44 badEntry(afs_uint32 dbAddr)
51 * decode the arguments passed via LWP and dump the database.
55 setupDbDump(void *param)
57 int writeFid = (intptr_t)param;
60 afs_pthread_setname_self("Database Dumper");
61 code = InitRPC(&dumpSyncPtr->ut, LOCKREAD, 1);
65 code = writeDatabase(dumpSyncPtr->ut, writeFid);
67 LogError(code, "writeDatabase failed\n");
69 code = close(writeFid);
71 LogError(code, "pipe writer close failed\n");
73 LogDebug(5, "writeDatabase complete\n");
77 ubik_EndTrans(dumpSyncPtr->ut);
78 return (void *)(intptr_t)(code);
83 SBUDB_DumpDB(struct rx_call *call, int firstcall, afs_int32 maxLength,
84 charListT *charListPtr, afs_int32 *done)
88 code = DumpDB(call, firstcall, maxLength, charListPtr, done);
89 osi_auditU(call, BUDB_DmpDBEvent, code, AUD_END);
94 DumpDB(struct rx_call *call,
95 int firstcall, /* 1 - init. 0 - no init */
97 charListT *charListPtr,
100 #ifdef AFS_PTHREAD_ENV
101 pthread_t dumperPid, watcherPid;
102 pthread_attr_t dumperPid_tattr;
103 pthread_attr_t watcherPid_tattr;
105 PROCESS dumperPid, watcherPid;
110 if (callPermitted(call) == 0)
111 ERROR(BUDB_NOTPERMITTED);
113 ObtainWriteLock(&dumpSyncPtr->ds_lock);
115 /* If asking for zero bytes, then this is a call to reset the timeToLive
116 * timer. Reset it if there is a dump in progress.
118 if (maxLength == 0) {
119 charListPtr->charListT_val = NULL;
120 charListPtr->charListT_len = 0;
122 *done = ((dumpSyncPtr->statusFlags == 0) ? 1 : 0);
124 /* reset the clock on dump timeout */
125 dumpSyncPtr->timeToLive = time(0) + DUMP_TTL_INC;
129 if (dumpSyncPtr->statusFlags == 0) {
131 ERROR(BUDB_DUMPFAILED);
133 LogDebug(5, "Setup dump\n");
135 /* no dump in progress - setup and retake lock */
136 memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
137 /* ObtainWriteLock(&dumpSyncPtr->ds_lock); */
139 /* mark dump in progress */
140 dumpSyncPtr->statusFlags = 1;
142 code = pipe(dumpSyncPtr->pipeFid);
146 #ifdef AFS_PTHREAD_ENV
147 /* Initialize the condition variables and the mutexes we use
148 * to signal and synchronize the reader and writer threads.
150 CV_INIT(&dumpSyncPtr->ds_readerStatus_cond, "reader cond", CV_DEFAULT, 0);
151 CV_INIT(&dumpSyncPtr->ds_writerStatus_cond, "writer cond", CV_DEFAULT, 0);
152 MUTEX_INIT(&dumpSyncPtr->ds_readerStatus_mutex, "reader", MUTEX_DEFAULT, 0);
153 MUTEX_INIT(&dumpSyncPtr->ds_writerStatus_mutex, "writer", MUTEX_DEFAULT, 0);
155 /* Initialize the thread attributes and launch the thread */
157 opr_Verify(pthread_attr_init(&dumperPid_tattr) == 0);
158 opr_Verify(pthread_attr_setdetachstate(&dumperPid_tattr,
159 PTHREAD_CREATE_DETACHED) == 0);
160 opr_Verify(pthread_create(&dumperPid,
162 (void *)setupDbDump, NULL) == 0);
166 LWP_CreateProcess(setupDbDump, 16384, 1,
167 (void *)(intptr_t)dumpSyncPtr->pipeFid[1],
168 "Database Dumper", &dumperPid);
173 dumpSyncPtr->dumperPid = dumperPid;
174 dumpSyncPtr->timeToLive = time(0) + DUMP_TTL_INC;
176 #ifdef AFS_PTHREAD_ENV
177 /* Initialize the thread attributes and launch the thread */
179 opr_Verify(pthread_attr_init(&watcherPid_tattr) == 0);
180 opr_Verify(pthread_attr_setdetachstate(&watcherPid_tattr,
181 PTHREAD_CREATE_DETACHED) == 0);
182 opr_Verify(pthread_create(&watcherPid,
184 (void *)dumpWatcher, NULL) == 0);
186 /* now create the watcher thread */
188 LWP_CreateProcess(dumpWatcher, 16384, 1, 0,
189 "Database Dump Watchdog", &watcherPid);
191 } else if (firstcall)
194 /* now read the database and feed it to the rpc connection */
197 while (dumpSyncPtr->ds_bytes == 0) {
198 /* if no more data */
199 if ((dumpSyncPtr->ds_writerStatus == DS_DONE)
200 || (dumpSyncPtr->ds_writerStatus == DS_DONE_ERROR)) {
204 if (dumpSyncPtr->ds_writerStatus == DS_WAITING) {
205 LogDebug(6, "wakup writer\n");
206 dumpSyncPtr->ds_writerStatus = 0;
207 #ifdef AFS_PTHREAD_ENV
208 CV_BROADCAST(&dumpSyncPtr->ds_writerStatus_cond);
210 code = LWP_SignalProcess(&dumpSyncPtr->ds_writerStatus);
212 LogError(code, "BUDB_DumpDB: signal delivery failed\n");
215 LogDebug(6, "wait for writer\n");
216 dumpSyncPtr->ds_readerStatus = DS_WAITING;
217 ReleaseWriteLock(&dumpSyncPtr->ds_lock);
218 #ifdef AFS_PTHREAD_ENV
219 MUTEX_ENTER(&dumpSyncPtr->ds_readerStatus_mutex);
220 CV_WAIT(&dumpSyncPtr->ds_readerStatus_cond, &dumpSyncPtr->ds_readerStatus_mutex);
221 MUTEX_EXIT(&dumpSyncPtr->ds_readerStatus_mutex);
223 LWP_WaitProcess(&dumpSyncPtr->ds_readerStatus);
225 ObtainWriteLock(&dumpSyncPtr->ds_lock);
228 charListPtr->charListT_val = malloc(maxLength);
230 read(dumpSyncPtr->pipeFid[0], charListPtr->charListT_val, maxLength);
232 /* reset the clock on dump timeout */
233 dumpSyncPtr->timeToLive = time(0) + DUMP_TTL_INC;
235 LogDebug(4, "read of len %d returned %d\n", maxLength, readSize);
237 charListPtr->charListT_len = readSize;
239 if (readSize == 0) { /* last chunk */
241 close(dumpSyncPtr->pipeFid[0]);
242 dumpSyncPtr->statusFlags = 0;
246 dumpSyncPtr->ds_bytes -= readSize;
247 if (dumpSyncPtr->ds_writerStatus == DS_WAITING) {
248 dumpSyncPtr->ds_writerStatus = 0;
249 #ifdef AFS_PTHREAD_ENV
250 CV_BROADCAST(&dumpSyncPtr->ds_writerStatus_cond);
252 code = LWP_SignalProcess(&dumpSyncPtr->ds_writerStatus);
254 LogError(code, "BUDB_DumpDB: signal delivery failed\n");
259 if (!code && (dumpSyncPtr->ds_writerStatus == DS_DONE_ERROR))
261 ReleaseWriteLock(&dumpSyncPtr->ds_lock);
266 SBUDB_RestoreDbHeader(struct rx_call *call, struct DbHeader *header)
270 code = RestoreDbHeader(call, header);
271 osi_auditU(call, BUDB_RstDBHEvent, code, AUD_END);
276 RestoreDbHeader(struct rx_call *call, struct DbHeader *header)
278 struct ubik_trans *ut = 0;
281 extern struct memoryDB db;
283 if (callPermitted(call) == 0)
284 ERROR(BUDB_NOTPERMITTED);
286 code = InitRPC(&ut, LOCKWRITE, 1);
290 if (header->dbversion != ntohl(db.h.version))
291 ERROR(BUDB_VERSIONMISMATCH);
293 /* merge rather than replace the header information */
294 if (db.h.lastDumpId < htonl(header->lastDumpId))
295 db.h.lastDumpId = htonl(header->lastDumpId);
297 if (db.h.lastTapeId < htonl(header->lastTapeId))
298 db.h.lastTapeId = htonl(header->lastTapeId);
300 if (db.h.lastInstanceId < htonl(header->lastInstanceId))
301 db.h.lastInstanceId = htonl(header->lastInstanceId);
303 code = dbwrite(ut, 0, (char *)&db.h, sizeof(db.h));
314 * monitors the state of a database dump. If the dump calls do not
315 * reset the time to live value, the dump times out. In that case,
316 * we kill the database traversal thread and clean up all the other
317 * state. Most importantly, the database is unlocked so that other
318 * transactions can proceed.
322 dumpWatcher(void *unused)
326 afs_pthread_setname_self("Database Dump Watchdog");
329 /* printf("dumpWatcher\n"); */
330 ObtainWriteLock(&dumpSyncPtr->ds_lock);
332 if (dumpSyncPtr->statusFlags == 0) {
333 /* dump has finished */
337 /* check time to live */
338 if (time(0) > dumpSyncPtr->timeToLive) { /*i */
339 /* dump has exceeded the allocated time - terminate it */
340 LogError(0, "Database dump timeout exceeded: %s",
341 ctime(&dumpSyncPtr->timeToLive));
342 LogError(0, "Terminating database dump\n");
344 close(dumpSyncPtr->pipeFid[0]);
345 close(dumpSyncPtr->pipeFid[1]);
346 #ifdef AFS_PTHREAD_ENV
347 opr_Verify(pthread_cancel(dumpSyncPtr->dumperPid) == 0);
349 code = LWP_DestroyProcess(dumpSyncPtr->dumperPid);
351 LogError(code, "dumpWatcher: failed to kill dump thread\n");
354 if (dumpSyncPtr->ut) {
355 code = ubik_AbortTrans(dumpSyncPtr->ut);
357 LogError(code, "Aborting dump transaction\n");
360 memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
364 ReleaseWriteLock(&dumpSyncPtr->ds_lock);
365 #ifdef AFS_PTHREAD_ENV
373 ReleaseWriteLock(&dumpSyncPtr->ds_lock);
374 /* printf("dumpWatcher exit\n"); */