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>
26 #include "budb_errs.h"
28 #include "budb_internal.h"
29 #include "error_macros.h"
32 afs_int32 DumpDB(struct rx_call *, int, afs_int32, charListT *, afs_int32 *);
33 afs_int32 RestoreDbHeader(struct rx_call *, struct DbHeader *);
34 void *dumpWatcher(void *);
36 /* dump ubik database - interface routines */
39 * no checking for now.
43 badEntry(afs_uint32 dbAddr)
50 * decode the arguments passed via LWP and dump the database.
54 setupDbDump(void *param)
56 int writeFid = (intptr_t)param;
59 code = InitRPC(&dumpSyncPtr->ut, LOCKREAD, 1);
63 code = writeDatabase(dumpSyncPtr->ut, writeFid);
65 LogError(code, "writeDatabase failed\n");
67 code = close(writeFid);
69 LogError(code, "pipe writer close failed\n");
71 LogDebug(5, "writeDatabase complete\n");
75 ubik_EndTrans(dumpSyncPtr->ut);
76 return (void *)(intptr_t)(code);
81 SBUDB_DumpDB(struct rx_call *call, int firstcall, afs_int32 maxLength,
82 charListT *charListPtr, afs_int32 *done)
86 code = DumpDB(call, firstcall, maxLength, charListPtr, done);
87 osi_auditU(call, BUDB_DmpDBEvent, code, AUD_END);
92 DumpDB(struct rx_call *call,
93 int firstcall, /* 1 - init. 0 - no init */
95 charListT *charListPtr,
98 #ifdef AFS_PTHREAD_ENV
99 pthread_t dumperPid, watcherPid;
100 pthread_attr_t dumperPid_tattr;
101 pthread_attr_t watcherPid_tattr;
103 PROCESS dumperPid, watcherPid;
108 if (callPermitted(call) == 0)
109 ERROR(BUDB_NOTPERMITTED);
111 ObtainWriteLock(&dumpSyncPtr->ds_lock);
113 /* If asking for zero bytes, then this is a call to reset the timeToLive
114 * timer. Reset it if there is a dump in progress.
116 if (maxLength == 0) {
117 charListPtr->charListT_val = NULL;
118 charListPtr->charListT_len = 0;
120 *done = ((dumpSyncPtr->statusFlags == 0) ? 1 : 0);
122 /* reset the clock on dump timeout */
123 dumpSyncPtr->timeToLive = time(0) + DUMP_TTL_INC;
127 if (dumpSyncPtr->statusFlags == 0) {
129 ERROR(BUDB_DUMPFAILED);
131 LogDebug(5, "Setup dump\n");
133 /* no dump in progress - setup and retake lock */
134 memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
135 /* ObtainWriteLock(&dumpSyncPtr->ds_lock); */
137 /* mark dump in progress */
138 dumpSyncPtr->statusFlags = 1;
140 code = pipe(dumpSyncPtr->pipeFid);
144 #ifdef AFS_PTHREAD_ENV
145 /* Initialize the condition variables and the mutexes we use
146 * to signal and synchronize the reader and writer threads.
148 CV_INIT(&dumpSyncPtr->ds_readerStatus_cond, "reader cond", CV_DEFAULT, 0);
149 CV_INIT(&dumpSyncPtr->ds_writerStatus_cond, "writer cond", CV_DEFAULT, 0);
150 MUTEX_INIT(&dumpSyncPtr->ds_readerStatus_mutex, "reader", MUTEX_DEFAULT, 0);
151 MUTEX_INIT(&dumpSyncPtr->ds_writerStatus_mutex, "writer", MUTEX_DEFAULT, 0);
153 /* Initialize the thread attributes and launch the thread */
155 osi_Assert(pthread_attr_init(&dumperPid_tattr) == 0);
156 osi_Assert(pthread_attr_setdetachstate(&dumperPid_tattr, PTHREAD_CREATE_DETACHED) == 0);
157 osi_Assert(pthread_create(&dumperPid, &dumperPid_tattr, (void *)setupDbDump, NULL) == 0);
161 LWP_CreateProcess(setupDbDump, 16384, 1,
162 (void *)(intptr_t)dumpSyncPtr->pipeFid[1],
163 "Database Dumper", &dumperPid);
168 dumpSyncPtr->dumperPid = dumperPid;
169 dumpSyncPtr->timeToLive = time(0) + DUMP_TTL_INC;
171 #ifdef AFS_PTHREAD_ENV
172 /* Initialize the thread attributes and launch the thread */
174 osi_Assert(pthread_attr_init(&watcherPid_tattr) == 0);
175 osi_Assert(pthread_attr_setdetachstate(&watcherPid_tattr, PTHREAD_CREATE_DETACHED) == 0);
176 osi_Assert(pthread_create(&watcherPid, &watcherPid_tattr, (void *)dumpWatcher, NULL) == 0);
178 /* now create the watcher thread */
180 LWP_CreateProcess(dumpWatcher, 16384, 1, 0,
181 "Database Dump Watchdog", &watcherPid);
183 } else if (firstcall)
186 /* now read the database and feed it to the rpc connection */
189 while (dumpSyncPtr->ds_bytes == 0) {
190 /* if no more data */
191 if ((dumpSyncPtr->ds_writerStatus == DS_DONE)
192 || (dumpSyncPtr->ds_writerStatus == DS_DONE_ERROR)) {
196 if (dumpSyncPtr->ds_writerStatus == DS_WAITING) {
197 LogDebug(6, "wakup writer\n");
198 dumpSyncPtr->ds_writerStatus = 0;
199 #ifdef AFS_PTHREAD_ENV
200 CV_BROADCAST(&dumpSyncPtr->ds_writerStatus_cond);
202 code = LWP_SignalProcess(&dumpSyncPtr->ds_writerStatus);
204 LogError(code, "BUDB_DumpDB: signal delivery failed\n");
207 LogDebug(6, "wait for writer\n");
208 dumpSyncPtr->ds_readerStatus = DS_WAITING;
209 ReleaseWriteLock(&dumpSyncPtr->ds_lock);
210 #ifdef AFS_PTHREAD_ENV
211 MUTEX_ENTER(&dumpSyncPtr->ds_readerStatus_mutex);
212 CV_WAIT(&dumpSyncPtr->ds_readerStatus_cond, &dumpSyncPtr->ds_readerStatus_mutex);
213 MUTEX_EXIT(&dumpSyncPtr->ds_readerStatus_mutex);
215 LWP_WaitProcess(&dumpSyncPtr->ds_readerStatus);
217 ObtainWriteLock(&dumpSyncPtr->ds_lock);
220 charListPtr->charListT_val = (char *)malloc(maxLength);
222 read(dumpSyncPtr->pipeFid[0], charListPtr->charListT_val, maxLength);
224 /* reset the clock on dump timeout */
225 dumpSyncPtr->timeToLive = time(0) + DUMP_TTL_INC;
227 LogDebug(4, "read of len %d returned %d\n", maxLength, readSize);
229 charListPtr->charListT_len = readSize;
231 if (readSize == 0) { /* last chunk */
233 close(dumpSyncPtr->pipeFid[0]);
234 dumpSyncPtr->statusFlags = 0;
238 dumpSyncPtr->ds_bytes -= readSize;
239 if (dumpSyncPtr->ds_writerStatus == DS_WAITING) {
240 dumpSyncPtr->ds_writerStatus = 0;
241 #ifdef AFS_PTHREAD_ENV
242 CV_BROADCAST(&dumpSyncPtr->ds_writerStatus_cond);
244 code = LWP_SignalProcess(&dumpSyncPtr->ds_writerStatus);
246 LogError(code, "BUDB_DumpDB: signal delivery failed\n");
251 if (!code && (dumpSyncPtr->ds_writerStatus == DS_DONE_ERROR))
253 ReleaseWriteLock(&dumpSyncPtr->ds_lock);
258 SBUDB_RestoreDbHeader(struct rx_call *call, struct DbHeader *header)
262 code = RestoreDbHeader(call, header);
263 osi_auditU(call, BUDB_RstDBHEvent, code, AUD_END);
268 RestoreDbHeader(struct rx_call *call, struct DbHeader *header)
270 struct ubik_trans *ut = 0;
273 extern struct memoryDB db;
275 if (callPermitted(call) == 0)
276 ERROR(BUDB_NOTPERMITTED);
278 code = InitRPC(&ut, LOCKWRITE, 1);
282 if (header->dbversion != ntohl(db.h.version))
283 ERROR(BUDB_VERSIONMISMATCH);
285 /* merge rather than replace the header information */
286 if (db.h.lastDumpId < htonl(header->lastDumpId))
287 db.h.lastDumpId = htonl(header->lastDumpId);
289 if (db.h.lastTapeId < htonl(header->lastTapeId))
290 db.h.lastTapeId = htonl(header->lastTapeId);
292 if (db.h.lastInstanceId < htonl(header->lastInstanceId))
293 db.h.lastInstanceId = htonl(header->lastInstanceId);
295 code = dbwrite(ut, 0, (char *)&db.h, sizeof(db.h));
306 * monitors the state of a database dump. If the dump calls do not
307 * reset the time to live value, the dump times out. In that case,
308 * we kill the database traversal thread and clean up all the other
309 * state. Most importantly, the database is unlocked so that other
310 * transactions can proceed.
314 dumpWatcher(void *unused)
320 /* printf("dumpWatcher\n"); */
321 ObtainWriteLock(&dumpSyncPtr->ds_lock);
323 if (dumpSyncPtr->statusFlags == 0) {
324 /* dump has finished */
328 /* check time to live */
329 if (time(0) > dumpSyncPtr->timeToLive) { /*i */
330 /* dump has exceeded the allocated time - terminate it */
331 LogError(0, "Database dump timeout exceeded: %s",
332 ctime(&dumpSyncPtr->timeToLive));
333 LogError(0, "Terminating database dump\n");
335 close(dumpSyncPtr->pipeFid[0]);
336 close(dumpSyncPtr->pipeFid[1]);
337 #ifdef AFS_PTHREAD_ENV
338 osi_Assert(pthread_cancel(dumpSyncPtr->dumperPid) == 0);
340 code = LWP_DestroyProcess(dumpSyncPtr->dumperPid);
342 LogError(code, "dumpWatcher: failed to kill dump thread\n");
345 if (dumpSyncPtr->ut) {
346 code = ubik_AbortTrans(dumpSyncPtr->ut);
348 LogError(code, "Aborting dump transaction\n");
351 memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
355 ReleaseWriteLock(&dumpSyncPtr->ds_lock);
356 #ifdef AFS_PTHREAD_ENV
364 ReleaseWriteLock(&dumpSyncPtr->ds_lock);
365 /* printf("dumpWatcher exit\n"); */