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>
21 #include <netinet/in.h>
23 #include <sys/resource.h>
27 #include <sys/types.h>
38 #include <afs/cellconfig.h>
41 #include "budb_errs.h"
43 #include "error_macros.h"
45 #include "afs/audit.h"
47 void *dumpWatcher(void *);
49 /* dump ubik database - interface routines */
52 * no checking for now.
64 * decode the arguments passed via LWP and dump the database.
68 setupDbDump(void *param)
70 int writeFid = (int)param;
73 code = InitRPC(&dumpSyncPtr->ut, LOCKREAD, 1);
77 code = writeDatabase(dumpSyncPtr->ut, writeFid);
79 LogError(code, "writeDatabase failed\n");
81 code = close(writeFid);
83 LogError(code, "pipe writer close failed\n");
85 LogDebug(5, "writeDatabase complete\n");
89 ubik_EndTrans(dumpSyncPtr->ut);
90 return (void *)(code);
94 afs_int32 DumpDB(), RestoreDbHeader();
96 SBUDB_DumpDB(call, firstcall, maxLength, charListPtr, done)
100 charListT *charListPtr;
105 code = DumpDB(call, firstcall, maxLength, charListPtr, done);
106 osi_auditU(call, BUDB_DmpDBEvent, code, AUD_END);
111 DumpDB(call, firstcall, maxLength, charListPtr, done)
112 struct rx_call *call;
113 int firstcall; /* 1 - init. 0 - no init */
115 charListT *charListPtr;
118 #if defined(AFS_PTHREAD_ENV) && defined(UBIK_PTHREAD_ENV)
119 pthread_t dumperPid, watcherPid;
120 pthread_attr_t dumperPid_tattr;
121 pthread_attr_t watcherPid_tattr;
124 PROCESS dumperPid, watcherPid;
129 if (callPermitted(call) == 0)
130 ERROR(BUDB_NOTPERMITTED);
132 ObtainWriteLock(&dumpSyncPtr->ds_lock);
134 /* If asking for zero bytes, then this is a call to reset the timeToLive
135 * timer. Reset it if there is a dump in progress.
137 if (maxLength == 0) {
138 charListPtr->charListT_val = NULL;
139 charListPtr->charListT_len = 0;
141 *done = ((dumpSyncPtr->statusFlags == 0) ? 1 : 0);
143 /* reset the clock on dump timeout */
144 dumpSyncPtr->timeToLive = time(0) + DUMP_TTL_INC;
148 if (dumpSyncPtr->statusFlags == 0) {
150 ERROR(BUDB_DUMPFAILED);
152 LogDebug(5, "Setup dump\n");
154 /* no dump in progress - setup and retake lock */
155 memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
156 /* ObtainWriteLock(&dumpSyncPtr->ds_lock); */
158 /* mark dump in progress */
159 dumpSyncPtr->statusFlags = 1;
161 code = pipe(dumpSyncPtr->pipeFid);
165 #if defined(AFS_PTHREAD_ENV) && defined(UBIK_PTHREAD_ENV)
166 /* Initialize the condition variables and the mutexes we use
167 * to signal and synchronize the reader and writer threads.
169 assert(pthread_cond_init(&dumpSyncPtr->ds_readerStatus_cond, (const pthread_condattr_t *)0) == 0);
170 assert(pthread_cond_init(&dumpSyncPtr->ds_writerStatus_cond, (const pthread_condattr_t *)0) == 0);
171 assert(pthread_mutex_init(&dumpSyncPtr->ds_readerStatus_mutex, (const pthread_mutexattr_t *)0) == 0);
172 assert(pthread_mutex_init(&dumpSyncPtr->ds_writerStatus_mutex, (const pthread_mutexattr_t *)0) == 0);
174 /* Initialize the thread attributes and launch the thread */
176 assert(pthread_attr_init(&dumperPid_tattr) == 0);
177 assert(pthread_attr_setdetachstate(&dumperPid_tattr, PTHREAD_CREATE_DETACHED) == 0);
178 assert(pthread_create(&dumperPid, &dumperPid_tattr, (void *)setupDbDump, NULL) == 0);
182 LWP_CreateProcess(setupDbDump, 16384, 1,
183 (void *)dumpSyncPtr->pipeFid[1],
184 "Database Dumper", &dumperPid);
189 dumpSyncPtr->dumperPid = dumperPid;
190 dumpSyncPtr->timeToLive = time(0) + DUMP_TTL_INC;
192 #if defined(AFS_PTHREAD_ENV) && defined(UBIK_PTHREAD_ENV)
193 /* Initialize the thread attributes and launch the thread */
195 assert(pthread_attr_init(&watcherPid_tattr) == 0);
196 assert(pthread_attr_setdetachstate(&watcherPid_tattr, PTHREAD_CREATE_DETACHED) == 0);
197 assert(pthread_create(&watcherPid, &watcherPid_tattr, (void *)dumpWatcher, NULL) == 0);
199 /* now create the watcher thread */
201 LWP_CreateProcess(dumpWatcher, 16384, 1, 0,
202 "Database Dump Watchdog", &watcherPid);
204 } else if (firstcall)
207 /* now read the database and feed it to the rpc connection */
210 while (dumpSyncPtr->ds_bytes == 0) {
211 /* if no more data */
212 if ((dumpSyncPtr->ds_writerStatus == DS_DONE)
213 || (dumpSyncPtr->ds_writerStatus == DS_DONE_ERROR)) {
217 if (dumpSyncPtr->ds_writerStatus == DS_WAITING) {
218 LogDebug(6, "wakup writer\n");
219 dumpSyncPtr->ds_writerStatus = 0;
220 #if defined(AFS_PTHREAD_ENV) && defined(UBIK_PTHREAD_ENV)
221 assert(pthread_cond_broadcast(&dumpSyncPtr->ds_writerStatus_cond) == 0);
223 code = LWP_SignalProcess(&dumpSyncPtr->ds_writerStatus);
225 LogError(code, "BUDB_DumpDB: signal delivery failed\n");
228 LogDebug(6, "wait for writer\n");
229 dumpSyncPtr->ds_readerStatus = DS_WAITING;
230 ReleaseWriteLock(&dumpSyncPtr->ds_lock);
231 #if defined(AFS_PTHREAD_ENV) && defined(UBIK_PTHREAD_ENV)
232 assert(pthread_mutex_lock(&dumpSyncPtr->ds_readerStatus_mutex) == 0);
233 assert(pthread_cond_wait(&dumpSyncPtr->ds_readerStatus_cond, &dumpSyncPtr->ds_readerStatus_mutex) == 0);
234 assert(pthread_mutex_unlock(&dumpSyncPtr->ds_readerStatus_mutex) == 0);
236 LWP_WaitProcess(&dumpSyncPtr->ds_readerStatus);
238 ObtainWriteLock(&dumpSyncPtr->ds_lock);
241 charListPtr->charListT_val = (char *)malloc(maxLength);
243 read(dumpSyncPtr->pipeFid[0], charListPtr->charListT_val, maxLength);
245 /* reset the clock on dump timeout */
246 dumpSyncPtr->timeToLive = time(0) + DUMP_TTL_INC;
248 LogDebug(4, "read of len %d returned %d\n", maxLength, readSize);
250 charListPtr->charListT_len = readSize;
252 if (readSize == 0) { /* last chunk */
254 close(dumpSyncPtr->pipeFid[0]);
255 dumpSyncPtr->statusFlags = 0;
259 dumpSyncPtr->ds_bytes -= readSize;
260 if (dumpSyncPtr->ds_writerStatus == DS_WAITING) {
261 dumpSyncPtr->ds_writerStatus = 0;
262 #if defined(AFS_PTHREAD_ENV) && defined(UBIK_PTHREAD_ENV)
263 assert(pthread_cond_broadcast(&dumpSyncPtr->ds_writerStatus_cond) == 0);
265 code = LWP_SignalProcess(&dumpSyncPtr->ds_writerStatus);
267 LogError(code, "BUDB_DumpDB: signal delivery failed\n");
272 if (!code && (dumpSyncPtr->ds_writerStatus == DS_DONE_ERROR))
274 ReleaseWriteLock(&dumpSyncPtr->ds_lock);
279 SBUDB_RestoreDbHeader(call, header)
280 struct rx_call *call;
281 struct DbHeader *header;
285 code = RestoreDbHeader(call, header);
286 osi_auditU(call, BUDB_RstDBHEvent, code, AUD_END);
291 RestoreDbHeader(call, header)
292 struct rx_call *call;
293 struct DbHeader *header;
295 struct ubik_trans *ut = 0;
298 extern struct memoryDB db;
300 if (callPermitted(call) == 0)
301 ERROR(BUDB_NOTPERMITTED);
303 code = InitRPC(&ut, LOCKWRITE, 1);
307 if (header->dbversion != ntohl(db.h.version))
308 ERROR(BUDB_VERSIONMISMATCH);
310 /* merge rather than replace the header information */
311 if (db.h.lastDumpId < htonl(header->lastDumpId))
312 db.h.lastDumpId = htonl(header->lastDumpId);
314 if (db.h.lastTapeId < htonl(header->lastTapeId))
315 db.h.lastTapeId = htonl(header->lastTapeId);
317 if (db.h.lastInstanceId < htonl(header->lastInstanceId))
318 db.h.lastInstanceId = htonl(header->lastInstanceId);
320 code = dbwrite(ut, 0, (char *)&db.h, sizeof(db.h));
331 * monitors the state of a database dump. If the dump calls do not
332 * reset the time to live value, the dump times out. In that case,
333 * we kill the database traversal thread and clean up all the other
334 * state. Most importantly, the database is unlocked so that other
335 * transactions can proceed.
339 dumpWatcher(void *unused)
345 /* printf("dumpWatcher\n"); */
346 ObtainWriteLock(&dumpSyncPtr->ds_lock);
348 if (dumpSyncPtr->statusFlags == 0) {
349 /* dump has finished */
353 /* check time to live */
354 if (time(0) > dumpSyncPtr->timeToLive) { /*i */
355 /* dump has exceeded the allocated time - terminate it */
356 LogError(0, "Database dump timeout exceeded: %s",
357 ctime(&dumpSyncPtr->timeToLive));
358 LogError(0, "Terminating database dump\n");
360 close(dumpSyncPtr->pipeFid[0]);
361 close(dumpSyncPtr->pipeFid[1]);
362 #if defined(AFS_PTHREAD_ENV) && defined(UBIK_PTHREAD_ENV)
363 assert(pthread_cancel(dumpSyncPtr->dumperPid) == 0);
365 code = LWP_DestroyProcess(dumpSyncPtr->dumperPid);
367 LogError(code, "dumpWatcher: failed to kill dump thread\n");
370 if (dumpSyncPtr->ut) {
371 code = ubik_AbortTrans(dumpSyncPtr->ut);
373 LogError(code, "Aborting dump transaction\n");
376 memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
380 ReleaseWriteLock(&dumpSyncPtr->ds_lock);
381 #if defined(AFS_PTHREAD_ENV) && defined(UBIK_PTHREAD_ENV)
389 ReleaseWriteLock(&dumpSyncPtr->ds_lock);
390 /* printf("dumpWatcher exit\n"); */