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/resource.h>
25 #include <sys/types.h>
36 #include <afs/cellconfig.h>
39 #include "budb_errs.h"
41 #include "budb_prototypes.h"
42 #include "error_macros.h"
44 #include "afs/audit.h"
46 afs_int32 DumpDB(struct rx_call *, int, afs_int32, charListT *, afs_int32 *);
47 afs_int32 RestoreDbHeader(struct rx_call *, struct DbHeader *);
48 void *dumpWatcher(void *);
50 /* dump ubik database - interface routines */
53 * no checking for now.
57 badEntry(afs_uint32 dbAddr)
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);
95 SBUDB_DumpDB(struct rx_call *call, int firstcall, afs_int32 maxLength,
96 charListT *charListPtr, afs_int32 *done)
100 code = DumpDB(call, firstcall, maxLength, charListPtr, done);
101 osi_auditU(call, BUDB_DmpDBEvent, code, AUD_END);
106 DumpDB(struct rx_call *call,
107 int firstcall, /* 1 - init. 0 - no init */
109 charListT *charListPtr,
112 #ifdef AFS_PTHREAD_ENV
113 pthread_t dumperPid, watcherPid;
114 pthread_attr_t dumperPid_tattr;
115 pthread_attr_t watcherPid_tattr;
117 PROCESS dumperPid, watcherPid;
122 if (callPermitted(call) == 0)
123 ERROR(BUDB_NOTPERMITTED);
125 ObtainWriteLock(&dumpSyncPtr->ds_lock);
127 /* If asking for zero bytes, then this is a call to reset the timeToLive
128 * timer. Reset it if there is a dump in progress.
130 if (maxLength == 0) {
131 charListPtr->charListT_val = NULL;
132 charListPtr->charListT_len = 0;
134 *done = ((dumpSyncPtr->statusFlags == 0) ? 1 : 0);
136 /* reset the clock on dump timeout */
137 dumpSyncPtr->timeToLive = time(0) + DUMP_TTL_INC;
141 if (dumpSyncPtr->statusFlags == 0) {
143 ERROR(BUDB_DUMPFAILED);
145 LogDebug(5, "Setup dump\n");
147 /* no dump in progress - setup and retake lock */
148 memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
149 /* ObtainWriteLock(&dumpSyncPtr->ds_lock); */
151 /* mark dump in progress */
152 dumpSyncPtr->statusFlags = 1;
154 code = pipe(dumpSyncPtr->pipeFid);
158 #ifdef AFS_PTHREAD_ENV
159 /* Initialize the condition variables and the mutexes we use
160 * to signal and synchronize the reader and writer threads.
162 assert(pthread_cond_init(&dumpSyncPtr->ds_readerStatus_cond, (const pthread_condattr_t *)0) == 0);
163 assert(pthread_cond_init(&dumpSyncPtr->ds_writerStatus_cond, (const pthread_condattr_t *)0) == 0);
164 assert(pthread_mutex_init(&dumpSyncPtr->ds_readerStatus_mutex, (const pthread_mutexattr_t *)0) == 0);
165 assert(pthread_mutex_init(&dumpSyncPtr->ds_writerStatus_mutex, (const pthread_mutexattr_t *)0) == 0);
167 /* Initialize the thread attributes and launch the thread */
169 assert(pthread_attr_init(&dumperPid_tattr) == 0);
170 assert(pthread_attr_setdetachstate(&dumperPid_tattr, PTHREAD_CREATE_DETACHED) == 0);
171 assert(pthread_create(&dumperPid, &dumperPid_tattr, (void *)setupDbDump, NULL) == 0);
175 LWP_CreateProcess(setupDbDump, 16384, 1,
176 (void *)dumpSyncPtr->pipeFid[1],
177 "Database Dumper", &dumperPid);
182 dumpSyncPtr->dumperPid = dumperPid;
183 dumpSyncPtr->timeToLive = time(0) + DUMP_TTL_INC;
185 #ifdef AFS_PTHREAD_ENV
186 /* Initialize the thread attributes and launch the thread */
188 assert(pthread_attr_init(&watcherPid_tattr) == 0);
189 assert(pthread_attr_setdetachstate(&watcherPid_tattr, PTHREAD_CREATE_DETACHED) == 0);
190 assert(pthread_create(&watcherPid, &watcherPid_tattr, (void *)dumpWatcher, NULL) == 0);
192 /* now create the watcher thread */
194 LWP_CreateProcess(dumpWatcher, 16384, 1, 0,
195 "Database Dump Watchdog", &watcherPid);
197 } else if (firstcall)
200 /* now read the database and feed it to the rpc connection */
203 while (dumpSyncPtr->ds_bytes == 0) {
204 /* if no more data */
205 if ((dumpSyncPtr->ds_writerStatus == DS_DONE)
206 || (dumpSyncPtr->ds_writerStatus == DS_DONE_ERROR)) {
210 if (dumpSyncPtr->ds_writerStatus == DS_WAITING) {
211 LogDebug(6, "wakup writer\n");
212 dumpSyncPtr->ds_writerStatus = 0;
213 #ifdef AFS_PTHREAD_ENV
214 assert(pthread_cond_broadcast(&dumpSyncPtr->ds_writerStatus_cond) == 0);
216 code = LWP_SignalProcess(&dumpSyncPtr->ds_writerStatus);
218 LogError(code, "BUDB_DumpDB: signal delivery failed\n");
221 LogDebug(6, "wait for writer\n");
222 dumpSyncPtr->ds_readerStatus = DS_WAITING;
223 ReleaseWriteLock(&dumpSyncPtr->ds_lock);
224 #ifdef AFS_PTHREAD_ENV
225 assert(pthread_mutex_lock(&dumpSyncPtr->ds_readerStatus_mutex) == 0);
226 assert(pthread_cond_wait(&dumpSyncPtr->ds_readerStatus_cond, &dumpSyncPtr->ds_readerStatus_mutex) == 0);
227 assert(pthread_mutex_unlock(&dumpSyncPtr->ds_readerStatus_mutex) == 0);
229 LWP_WaitProcess(&dumpSyncPtr->ds_readerStatus);
231 ObtainWriteLock(&dumpSyncPtr->ds_lock);
234 charListPtr->charListT_val = (char *)malloc(maxLength);
236 read(dumpSyncPtr->pipeFid[0], charListPtr->charListT_val, maxLength);
238 /* reset the clock on dump timeout */
239 dumpSyncPtr->timeToLive = time(0) + DUMP_TTL_INC;
241 LogDebug(4, "read of len %d returned %d\n", maxLength, readSize);
243 charListPtr->charListT_len = readSize;
245 if (readSize == 0) { /* last chunk */
247 close(dumpSyncPtr->pipeFid[0]);
248 dumpSyncPtr->statusFlags = 0;
252 dumpSyncPtr->ds_bytes -= readSize;
253 if (dumpSyncPtr->ds_writerStatus == DS_WAITING) {
254 dumpSyncPtr->ds_writerStatus = 0;
255 #ifdef AFS_PTHREAD_ENV
256 assert(pthread_cond_broadcast(&dumpSyncPtr->ds_writerStatus_cond) == 0);
258 code = LWP_SignalProcess(&dumpSyncPtr->ds_writerStatus);
260 LogError(code, "BUDB_DumpDB: signal delivery failed\n");
265 if (!code && (dumpSyncPtr->ds_writerStatus == DS_DONE_ERROR))
267 ReleaseWriteLock(&dumpSyncPtr->ds_lock);
272 SBUDB_RestoreDbHeader(struct rx_call *call, struct DbHeader *header)
276 code = RestoreDbHeader(call, header);
277 osi_auditU(call, BUDB_RstDBHEvent, code, AUD_END);
282 RestoreDbHeader(struct rx_call *call, struct DbHeader *header)
284 struct ubik_trans *ut = 0;
287 extern struct memoryDB db;
289 if (callPermitted(call) == 0)
290 ERROR(BUDB_NOTPERMITTED);
292 code = InitRPC(&ut, LOCKWRITE, 1);
296 if (header->dbversion != ntohl(db.h.version))
297 ERROR(BUDB_VERSIONMISMATCH);
299 /* merge rather than replace the header information */
300 if (db.h.lastDumpId < htonl(header->lastDumpId))
301 db.h.lastDumpId = htonl(header->lastDumpId);
303 if (db.h.lastTapeId < htonl(header->lastTapeId))
304 db.h.lastTapeId = htonl(header->lastTapeId);
306 if (db.h.lastInstanceId < htonl(header->lastInstanceId))
307 db.h.lastInstanceId = htonl(header->lastInstanceId);
309 code = dbwrite(ut, 0, (char *)&db.h, sizeof(db.h));
320 * monitors the state of a database dump. If the dump calls do not
321 * reset the time to live value, the dump times out. In that case,
322 * we kill the database traversal thread and clean up all the other
323 * state. Most importantly, the database is unlocked so that other
324 * transactions can proceed.
328 dumpWatcher(void *unused)
334 /* printf("dumpWatcher\n"); */
335 ObtainWriteLock(&dumpSyncPtr->ds_lock);
337 if (dumpSyncPtr->statusFlags == 0) {
338 /* dump has finished */
342 /* check time to live */
343 if (time(0) > dumpSyncPtr->timeToLive) { /*i */
344 /* dump has exceeded the allocated time - terminate it */
345 LogError(0, "Database dump timeout exceeded: %s",
346 ctime(&dumpSyncPtr->timeToLive));
347 LogError(0, "Terminating database dump\n");
349 close(dumpSyncPtr->pipeFid[0]);
350 close(dumpSyncPtr->pipeFid[1]);
351 #ifdef AFS_PTHREAD_ENV
352 assert(pthread_cancel(dumpSyncPtr->dumperPid) == 0);
354 code = LWP_DestroyProcess(dumpSyncPtr->dumperPid);
356 LogError(code, "dumpWatcher: failed to kill dump thread\n");
359 if (dumpSyncPtr->ut) {
360 code = ubik_AbortTrans(dumpSyncPtr->ut);
362 LogError(code, "Aborting dump transaction\n");
365 memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
369 ReleaseWriteLock(&dumpSyncPtr->ds_lock);
370 #ifdef AFS_PTHREAD_ENV
378 ReleaseWriteLock(&dumpSyncPtr->ds_lock);
379 /* printf("dumpWatcher exit\n"); */