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 osi_Assert(pthread_attr_init(&dumperPid_tattr) == 0);
158 osi_Assert(pthread_attr_setdetachstate(&dumperPid_tattr, PTHREAD_CREATE_DETACHED) == 0);
159 osi_Assert(pthread_create(&dumperPid, &dumperPid_tattr, (void *)setupDbDump, NULL) == 0);
163 LWP_CreateProcess(setupDbDump, 16384, 1,
164 (void *)(intptr_t)dumpSyncPtr->pipeFid[1],
165 "Database Dumper", &dumperPid);
170 dumpSyncPtr->dumperPid = dumperPid;
171 dumpSyncPtr->timeToLive = time(0) + DUMP_TTL_INC;
173 #ifdef AFS_PTHREAD_ENV
174 /* Initialize the thread attributes and launch the thread */
176 osi_Assert(pthread_attr_init(&watcherPid_tattr) == 0);
177 osi_Assert(pthread_attr_setdetachstate(&watcherPid_tattr, PTHREAD_CREATE_DETACHED) == 0);
178 osi_Assert(pthread_create(&watcherPid, &watcherPid_tattr, (void *)dumpWatcher, NULL) == 0);
180 /* now create the watcher thread */
182 LWP_CreateProcess(dumpWatcher, 16384, 1, 0,
183 "Database Dump Watchdog", &watcherPid);
185 } else if (firstcall)
188 /* now read the database and feed it to the rpc connection */
191 while (dumpSyncPtr->ds_bytes == 0) {
192 /* if no more data */
193 if ((dumpSyncPtr->ds_writerStatus == DS_DONE)
194 || (dumpSyncPtr->ds_writerStatus == DS_DONE_ERROR)) {
198 if (dumpSyncPtr->ds_writerStatus == DS_WAITING) {
199 LogDebug(6, "wakup writer\n");
200 dumpSyncPtr->ds_writerStatus = 0;
201 #ifdef AFS_PTHREAD_ENV
202 CV_BROADCAST(&dumpSyncPtr->ds_writerStatus_cond);
204 code = LWP_SignalProcess(&dumpSyncPtr->ds_writerStatus);
206 LogError(code, "BUDB_DumpDB: signal delivery failed\n");
209 LogDebug(6, "wait for writer\n");
210 dumpSyncPtr->ds_readerStatus = DS_WAITING;
211 ReleaseWriteLock(&dumpSyncPtr->ds_lock);
212 #ifdef AFS_PTHREAD_ENV
213 MUTEX_ENTER(&dumpSyncPtr->ds_readerStatus_mutex);
214 CV_WAIT(&dumpSyncPtr->ds_readerStatus_cond, &dumpSyncPtr->ds_readerStatus_mutex);
215 MUTEX_EXIT(&dumpSyncPtr->ds_readerStatus_mutex);
217 LWP_WaitProcess(&dumpSyncPtr->ds_readerStatus);
219 ObtainWriteLock(&dumpSyncPtr->ds_lock);
222 charListPtr->charListT_val = malloc(maxLength);
224 read(dumpSyncPtr->pipeFid[0], charListPtr->charListT_val, maxLength);
226 /* reset the clock on dump timeout */
227 dumpSyncPtr->timeToLive = time(0) + DUMP_TTL_INC;
229 LogDebug(4, "read of len %d returned %d\n", maxLength, readSize);
231 charListPtr->charListT_len = readSize;
233 if (readSize == 0) { /* last chunk */
235 close(dumpSyncPtr->pipeFid[0]);
236 dumpSyncPtr->statusFlags = 0;
240 dumpSyncPtr->ds_bytes -= readSize;
241 if (dumpSyncPtr->ds_writerStatus == DS_WAITING) {
242 dumpSyncPtr->ds_writerStatus = 0;
243 #ifdef AFS_PTHREAD_ENV
244 CV_BROADCAST(&dumpSyncPtr->ds_writerStatus_cond);
246 code = LWP_SignalProcess(&dumpSyncPtr->ds_writerStatus);
248 LogError(code, "BUDB_DumpDB: signal delivery failed\n");
253 if (!code && (dumpSyncPtr->ds_writerStatus == DS_DONE_ERROR))
255 ReleaseWriteLock(&dumpSyncPtr->ds_lock);
260 SBUDB_RestoreDbHeader(struct rx_call *call, struct DbHeader *header)
264 code = RestoreDbHeader(call, header);
265 osi_auditU(call, BUDB_RstDBHEvent, code, AUD_END);
270 RestoreDbHeader(struct rx_call *call, struct DbHeader *header)
272 struct ubik_trans *ut = 0;
275 extern struct memoryDB db;
277 if (callPermitted(call) == 0)
278 ERROR(BUDB_NOTPERMITTED);
280 code = InitRPC(&ut, LOCKWRITE, 1);
284 if (header->dbversion != ntohl(db.h.version))
285 ERROR(BUDB_VERSIONMISMATCH);
287 /* merge rather than replace the header information */
288 if (db.h.lastDumpId < htonl(header->lastDumpId))
289 db.h.lastDumpId = htonl(header->lastDumpId);
291 if (db.h.lastTapeId < htonl(header->lastTapeId))
292 db.h.lastTapeId = htonl(header->lastTapeId);
294 if (db.h.lastInstanceId < htonl(header->lastInstanceId))
295 db.h.lastInstanceId = htonl(header->lastInstanceId);
297 code = dbwrite(ut, 0, (char *)&db.h, sizeof(db.h));
308 * monitors the state of a database dump. If the dump calls do not
309 * reset the time to live value, the dump times out. In that case,
310 * we kill the database traversal thread and clean up all the other
311 * state. Most importantly, the database is unlocked so that other
312 * transactions can proceed.
316 dumpWatcher(void *unused)
320 afs_pthread_setname_self("Database Dump Watchdog");
323 /* printf("dumpWatcher\n"); */
324 ObtainWriteLock(&dumpSyncPtr->ds_lock);
326 if (dumpSyncPtr->statusFlags == 0) {
327 /* dump has finished */
331 /* check time to live */
332 if (time(0) > dumpSyncPtr->timeToLive) { /*i */
333 /* dump has exceeded the allocated time - terminate it */
334 LogError(0, "Database dump timeout exceeded: %s",
335 ctime(&dumpSyncPtr->timeToLive));
336 LogError(0, "Terminating database dump\n");
338 close(dumpSyncPtr->pipeFid[0]);
339 close(dumpSyncPtr->pipeFid[1]);
340 #ifdef AFS_PTHREAD_ENV
341 osi_Assert(pthread_cancel(dumpSyncPtr->dumperPid) == 0);
343 code = LWP_DestroyProcess(dumpSyncPtr->dumperPid);
345 LogError(code, "dumpWatcher: failed to kill dump thread\n");
348 if (dumpSyncPtr->ut) {
349 code = ubik_AbortTrans(dumpSyncPtr->ut);
351 LogError(code, "Aborting dump transaction\n");
354 memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
358 ReleaseWriteLock(&dumpSyncPtr->ds_lock);
359 #ifdef AFS_PTHREAD_ENV
367 ReleaseWriteLock(&dumpSyncPtr->ds_lock);
368 /* printf("dumpWatcher exit\n"); */