280735fe79ef54085b44ecb0ef737d428ce26297
[openafs.git] / src / budb / dbs_dump.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
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
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12 #include <afs/stds.h>
13
14 #include <roken.h>
15
16 #include <afs/opr.h>
17 #include <lock.h>
18 #include <ubik.h>
19 #include <lwp.h>
20 #include <rx/rx.h>
21 #include <rx/rxkad.h>
22 #include <afs/cellconfig.h>
23 #include <afs/audit.h>
24 #include <afs/afsutil.h>
25
26 #include "budb.h"
27 #include "budb_errs.h"
28 #include "database.h"
29 #include "budb_internal.h"
30 #include "error_macros.h"
31 #include "globals.h"
32
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 *);
36
37 /* dump ubik database - interface routines */
38
39 /* badEntry
40  *      no checking for now.
41  */
42
43 afs_int32
44 badEntry(afs_uint32 dbAddr)
45 {
46     /* return entry ok */
47     return (0);
48 }
49
50 /* setupDbDump
51  *      decode the arguments passed via LWP and dump the database.
52  */
53
54 void *
55 setupDbDump(void *param)
56 {
57     int writeFid = (intptr_t)param;
58     afs_int32 code = 0;
59
60     afs_pthread_setname_self("Database Dumper");
61     code = InitRPC(&dumpSyncPtr->ut, LOCKREAD, 1);
62     if (code)
63         goto error_exit;
64
65     code = writeDatabase(dumpSyncPtr->ut, writeFid);
66     if (code)
67         LogError(code, "writeDatabase failed\n");
68
69     code = close(writeFid);
70     if (code)
71         LogError(code, "pipe writer close failed\n");
72
73     LogDebug(5, "writeDatabase complete\n");
74
75   error_exit:
76     if (dumpSyncPtr->ut)
77         ubik_EndTrans(dumpSyncPtr->ut);
78     return (void *)(intptr_t)(code);
79 }
80
81
82 afs_int32
83 SBUDB_DumpDB(struct rx_call *call, int firstcall, afs_int32 maxLength,
84              charListT *charListPtr, afs_int32 *done)
85 {
86     afs_int32 code;
87
88     code = DumpDB(call, firstcall, maxLength, charListPtr, done);
89     osi_auditU(call, BUDB_DmpDBEvent, code, AUD_END);
90     return code;
91 }
92
93 afs_int32
94 DumpDB(struct rx_call *call,
95        int firstcall,           /* 1 - init.  0 - no init */
96        afs_int32 maxLength,
97        charListT *charListPtr,
98        afs_int32 *done)
99 {
100 #ifdef AFS_PTHREAD_ENV
101     pthread_t dumperPid, watcherPid;
102     pthread_attr_t dumperPid_tattr;
103     pthread_attr_t watcherPid_tattr;
104 #else
105     PROCESS dumperPid, watcherPid;
106 #endif
107     int readSize;
108     afs_int32 code = 0;
109
110     if (callPermitted(call) == 0)
111         ERROR(BUDB_NOTPERMITTED);
112
113     ObtainWriteLock(&dumpSyncPtr->ds_lock);
114
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.
117      */
118     if (maxLength == 0) {
119         charListPtr->charListT_val = NULL;
120         charListPtr->charListT_len = 0;
121
122         *done = ((dumpSyncPtr->statusFlags == 0) ? 1 : 0);
123
124         /* reset the clock on dump timeout */
125         dumpSyncPtr->timeToLive = time(0) + DUMP_TTL_INC;
126         goto error_exit;
127     }
128
129     if (dumpSyncPtr->statusFlags == 0) {
130         if (!firstcall)
131             ERROR(BUDB_DUMPFAILED);
132
133         LogDebug(5, "Setup dump\n");
134
135         /* no dump in progress - setup and retake lock */
136         memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
137 /*      ObtainWriteLock(&dumpSyncPtr->ds_lock); */
138
139         /* mark dump in progress */
140         dumpSyncPtr->statusFlags = 1;
141
142         code = pipe(dumpSyncPtr->pipeFid);
143         if (code)
144             ERROR(errno);
145
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.
149          */
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);
154
155         /* Initialize the thread attributes and launch the thread */
156
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,
161                                   &dumperPid_tattr,
162                                   (void *)setupDbDump, NULL) == 0);
163
164 #else
165         code =
166             LWP_CreateProcess(setupDbDump, 16384, 1,
167                               (void *)(intptr_t)dumpSyncPtr->pipeFid[1],
168                               "Database Dumper", &dumperPid);
169         if (code)
170             goto error_exit;
171 #endif
172
173         dumpSyncPtr->dumperPid = dumperPid;
174         dumpSyncPtr->timeToLive = time(0) + DUMP_TTL_INC;
175
176 #ifdef AFS_PTHREAD_ENV
177         /* Initialize the thread attributes and launch the thread */
178
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,
183                                   &watcherPid_tattr,
184                                   (void *)dumpWatcher, NULL) == 0);
185 #else
186         /* now create the watcher thread */
187         code =
188             LWP_CreateProcess(dumpWatcher, 16384, 1, 0,
189                               "Database Dump Watchdog", &watcherPid);
190 #endif
191     } else if (firstcall)
192         ERROR(BUDB_LOCKED);
193
194     /* now read the database and feed it to the rpc connection */
195
196     /* wait for data */
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)) {
201             break;
202         }
203
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);
209 #else
210             code = LWP_SignalProcess(&dumpSyncPtr->ds_writerStatus);
211             if (code)
212                 LogError(code, "BUDB_DumpDB: signal delivery failed\n");
213 #endif
214         }
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);
222 #else
223         LWP_WaitProcess(&dumpSyncPtr->ds_readerStatus);
224 #endif
225         ObtainWriteLock(&dumpSyncPtr->ds_lock);
226     }
227
228     charListPtr->charListT_val = malloc(maxLength);
229     readSize =
230         read(dumpSyncPtr->pipeFid[0], charListPtr->charListT_val, maxLength);
231
232     /* reset the clock on dump timeout */
233     dumpSyncPtr->timeToLive = time(0) + DUMP_TTL_INC;
234
235     LogDebug(4, "read of len %d returned %d\n", maxLength, readSize);
236
237     charListPtr->charListT_len = readSize;
238
239     if (readSize == 0) {        /* last chunk */
240         *done = 1;
241         close(dumpSyncPtr->pipeFid[0]);
242         dumpSyncPtr->statusFlags = 0;
243     } else
244         *done = 0;
245
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);
251 #else
252         code = LWP_SignalProcess(&dumpSyncPtr->ds_writerStatus);
253         if (code)
254             LogError(code, "BUDB_DumpDB: signal delivery failed\n");
255 #endif
256     }
257
258   error_exit:
259     if (!code && (dumpSyncPtr->ds_writerStatus == DS_DONE_ERROR))
260         code = -1;
261     ReleaseWriteLock(&dumpSyncPtr->ds_lock);
262     return (code);
263 }
264
265 afs_int32
266 SBUDB_RestoreDbHeader(struct rx_call *call, struct DbHeader *header)
267 {
268     afs_int32 code;
269
270     code = RestoreDbHeader(call, header);
271     osi_auditU(call, BUDB_RstDBHEvent, code, AUD_END);
272     return code;
273 }
274
275 afs_int32
276 RestoreDbHeader(struct rx_call *call, struct DbHeader *header)
277 {
278     struct ubik_trans *ut = 0;
279     afs_int32 code = 0;
280
281     extern struct memoryDB db;
282
283     if (callPermitted(call) == 0)
284         ERROR(BUDB_NOTPERMITTED);
285
286     code = InitRPC(&ut, LOCKWRITE, 1);
287     if (code)
288         goto error_exit;
289
290     if (header->dbversion != ntohl(db.h.version))
291         ERROR(BUDB_VERSIONMISMATCH);
292
293     /* merge rather than replace the header information */
294     if (db.h.lastDumpId < htonl(header->lastDumpId))
295         db.h.lastDumpId = htonl(header->lastDumpId);
296
297     if (db.h.lastTapeId < htonl(header->lastTapeId))
298         db.h.lastTapeId = htonl(header->lastTapeId);
299
300     if (db.h.lastInstanceId < htonl(header->lastInstanceId))
301         db.h.lastInstanceId = htonl(header->lastInstanceId);
302
303     code = dbwrite(ut, 0, (char *)&db.h, sizeof(db.h));
304     if (code)
305         code = BUDB_IO;
306
307   error_exit:
308     if (ut)
309         ubik_EndTrans(ut);
310     return (code);
311 }
312
313 /* dumpWatcher
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.
319  */
320
321 void *
322 dumpWatcher(void *unused)
323 {
324     afs_int32 code;
325
326     afs_pthread_setname_self("Database Dump Watchdog");
327     while (1) {                 /*w */
328
329         /* printf("dumpWatcher\n"); */
330         ObtainWriteLock(&dumpSyncPtr->ds_lock);
331
332         if (dumpSyncPtr->statusFlags == 0) {
333             /* dump has finished */
334             goto exit;
335         }
336
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");
343
344             close(dumpSyncPtr->pipeFid[0]);
345             close(dumpSyncPtr->pipeFid[1]);
346 #ifdef AFS_PTHREAD_ENV
347             opr_Verify(pthread_cancel(dumpSyncPtr->dumperPid) == 0);
348 #else
349             code = LWP_DestroyProcess(dumpSyncPtr->dumperPid);
350             if (code)
351                 LogError(code, "dumpWatcher: failed to kill dump thread\n");
352 #endif
353
354             if (dumpSyncPtr->ut) {
355                 code = ubik_AbortTrans(dumpSyncPtr->ut);
356                 if (code)
357                     LogError(code, "Aborting dump transaction\n");
358             }
359
360             memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
361             goto exit;
362         }
363         /*i */
364         ReleaseWriteLock(&dumpSyncPtr->ds_lock);
365 #ifdef AFS_PTHREAD_ENV
366         sleep(5);
367 #else
368         IOMGR_Sleep(5);
369 #endif
370     }                           /*w */
371
372   exit:
373     ReleaseWriteLock(&dumpSyncPtr->ds_lock);
374     /* printf("dumpWatcher exit\n"); */
375     return (0);
376 }