9d189c2a7147c289b1b65258dce9a428ed93b870
[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
13 RCSID
14     ("$Header$");
15
16 #ifdef AFS_NT40_ENV
17 #include <winsock2.h>
18 #include <io.h>
19 #include <fcntl.h>
20 #else
21 #include <netinet/in.h>
22 #include <sys/time.h>
23 #include <sys/resource.h>
24 #include <sys/file.h>
25 #endif
26 #include <time.h>
27 #include <sys/types.h>
28 #include <afs/stds.h>
29 #include <stdio.h>
30 #include <lock.h>
31 #include <ubik.h>
32 #include <lwp.h>
33 #include <rx/xdr.h>
34 #include <rx/rx.h>
35 #include <rx/rxkad.h>
36 #include <string.h>
37 #include <des.h>
38 #include <afs/cellconfig.h>
39 #include <errno.h>
40 #include "budb.h"
41 #include "budb_errs.h"
42 #include "database.h"
43 #include "error_macros.h"
44 #include "globals.h"
45 #include "afs/audit.h"
46
47
48 /* dump ubik database - interface routines */
49
50 /* badEntry
51  *      no checking for now.
52  */
53
54 afs_int32
55 badEntry(dbAddr)
56      afs_uint32 dbAddr;
57 {
58     /* return entry ok */
59     return (0);
60 }
61
62 /* setupDbDump
63  *      decode the arguments passed via LWP and dump the database.
64  */
65
66 setupDbDump(writeFid)
67      int writeFid;
68 {
69     afs_int32 code = 0;
70
71     code = InitRPC(&dumpSyncPtr->ut, LOCKREAD, 1);
72     if (code)
73         goto error_exit;
74
75     code = writeDatabase(dumpSyncPtr->ut, writeFid);
76     if (code)
77         LogError(code, "writeDatabase failed\n");
78
79     code = close(writeFid);
80     if (code)
81         LogError(code, "pipe writer close failed\n");
82
83     LogDebug(5, "writeDatabase complete\n");
84
85   error_exit:
86     if (dumpSyncPtr->ut)
87         ubik_EndTrans(dumpSyncPtr->ut);
88     return (code);
89 }
90
91
92 afs_int32 DumpDB(), RestoreDbHeader();
93 afs_int32
94 SBUDB_DumpDB(call, firstcall, maxLength, charListPtr, done)
95      struct rx_call *call;
96      int firstcall;
97      afs_int32 maxLength;
98      charListT *charListPtr;
99      afs_int32 *done;
100 {
101     afs_int32 code;
102
103     code = DumpDB(call, firstcall, maxLength, charListPtr, done);
104     osi_auditU(call, BUDB_DmpDBEvent, code, AUD_END);
105     return code;
106 }
107
108 afs_int32
109 DumpDB(call, firstcall, maxLength, charListPtr, done)
110      struct rx_call *call;
111      int firstcall;             /* 1 - init.  0 - no init */
112      afs_int32 maxLength;
113      charListT *charListPtr;
114      afs_int32 *done;
115 {
116     PROCESS dumperPid, watcherPid;
117     int readSize;
118     afs_int32 code = 0;
119     extern dumpWatcher();
120
121     if (callPermitted(call) == 0)
122         ERROR(BUDB_NOTPERMITTED);
123
124     ObtainWriteLock(&dumpSyncPtr->ds_lock);
125
126     /* If asking for zero bytes, then this is a call to reset the timeToLive
127      * timer. Reset it if there is a dump in progress. 
128      */
129     if (maxLength == 0) {
130         charListPtr->charListT_val = NULL;
131         charListPtr->charListT_len = 0;
132
133         *done = ((dumpSyncPtr->statusFlags == 0) ? 1 : 0);
134
135         /* reset the clock on dump timeout */
136         dumpSyncPtr->timeToLive = time(0) + DUMP_TTL_INC;
137         goto error_exit;
138     }
139
140     if (dumpSyncPtr->statusFlags == 0) {
141         if (!firstcall)
142             ERROR(BUDB_DUMPFAILED);
143
144         LogDebug(5, "Setup dump\n");
145
146         /* no dump in progress - setup and retake lock */
147         memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
148 /*      ObtainWriteLock(&dumpSyncPtr->ds_lock); */
149
150         /* mark dump in progress */
151         dumpSyncPtr->statusFlags = 1;
152
153         code = pipe(dumpSyncPtr->pipeFid);
154         if (code)
155             ERROR(errno);
156
157         code =
158             LWP_CreateProcess(setupDbDump, 16384, 1,
159                               (void *)dumpSyncPtr->pipeFid[1],
160                               "Database Dumper", &dumperPid);
161         if (code)
162             goto error_exit;
163
164         dumpSyncPtr->dumperPid = dumperPid;
165         dumpSyncPtr->timeToLive = time(0) + DUMP_TTL_INC;
166
167         /* now create the watcher thread */
168         code =
169             LWP_CreateProcess(dumpWatcher, 16384, 1, 0,
170                               "Database Dump Watchdog", &watcherPid);
171     } else if (firstcall)
172         ERROR(BUDB_LOCKED);
173
174     /* now read the database and feed it to the rpc connection */
175
176     /* wait for data */
177     while (dumpSyncPtr->ds_bytes == 0) {
178         /* if no more data */
179         if ((dumpSyncPtr->ds_writerStatus == DS_DONE)
180             || (dumpSyncPtr->ds_writerStatus == DS_DONE_ERROR)) {
181             break;
182         }
183
184         if (dumpSyncPtr->ds_writerStatus == DS_WAITING) {
185             LogDebug(6, "wakup writer\n");
186             dumpSyncPtr->ds_writerStatus = 0;
187             code = LWP_SignalProcess(&dumpSyncPtr->ds_writerStatus);
188             if (code)
189                 LogError(code, "BUDB_DumpDB: signal delivery failed\n");
190         }
191         LogDebug(6, "wait for writer\n");
192         dumpSyncPtr->ds_readerStatus = DS_WAITING;
193         ReleaseWriteLock(&dumpSyncPtr->ds_lock);
194         LWP_WaitProcess(&dumpSyncPtr->ds_readerStatus);
195         ObtainWriteLock(&dumpSyncPtr->ds_lock);
196     }
197
198     charListPtr->charListT_val = (char *)malloc(maxLength);
199     readSize =
200         read(dumpSyncPtr->pipeFid[0], charListPtr->charListT_val, maxLength);
201
202     /* reset the clock on dump timeout */
203     dumpSyncPtr->timeToLive = time(0) + DUMP_TTL_INC;
204
205     LogDebug(4, "read of len %d returned %d\n", maxLength, readSize);
206
207     charListPtr->charListT_len = readSize;
208
209     if (readSize == 0) {        /* last chunk */
210         *done = 1;
211         close(dumpSyncPtr->pipeFid[0]);
212         dumpSyncPtr->statusFlags = 0;
213     } else
214         *done = 0;
215
216     dumpSyncPtr->ds_bytes -= readSize;
217     if (dumpSyncPtr->ds_writerStatus == DS_WAITING) {
218         dumpSyncPtr->ds_writerStatus = 0;
219         code = LWP_SignalProcess(&dumpSyncPtr->ds_writerStatus);
220         if (code)
221             LogError(code, "BUDB_DumpDB: signal delivery failed\n");
222     }
223
224   error_exit:
225     if (!code && (dumpSyncPtr->ds_writerStatus == DS_DONE_ERROR))
226         code = -1;
227     ReleaseWriteLock(&dumpSyncPtr->ds_lock);
228     return (code);
229 }
230
231 afs_int32
232 SBUDB_RestoreDbHeader(call, header)
233      struct rx_call *call;
234      struct DbHeader *header;
235 {
236     afs_int32 code;
237
238     code = RestoreDbHeader(call, header);
239     osi_auditU(call, BUDB_RstDBHEvent, code, AUD_END);
240     return code;
241 }
242
243 afs_int32
244 RestoreDbHeader(call, header)
245      struct rx_call *call;
246      struct DbHeader *header;
247 {
248     struct ubik_trans *ut = 0;
249     afs_int32 code = 0;
250
251     extern struct memoryDB db;
252
253     if (callPermitted(call) == 0)
254         ERROR(BUDB_NOTPERMITTED);
255
256     code = InitRPC(&ut, LOCKWRITE, 1);
257     if (code)
258         goto error_exit;
259
260     if (header->dbversion != ntohl(db.h.version))
261         ERROR(BUDB_VERSIONMISMATCH);
262
263     /* merge rather than replace the header information */
264     if (db.h.lastDumpId < htonl(header->lastDumpId))
265         db.h.lastDumpId = htonl(header->lastDumpId);
266
267     if (db.h.lastTapeId < htonl(header->lastTapeId))
268         db.h.lastTapeId = htonl(header->lastTapeId);
269
270     if (db.h.lastInstanceId < htonl(header->lastInstanceId))
271         db.h.lastInstanceId = htonl(header->lastInstanceId);
272
273     code = dbwrite(ut, 0, (char *)&db.h, sizeof(db.h));
274     if (code)
275         code = BUDB_IO;
276
277   error_exit:
278     if (ut)
279         ubik_EndTrans(ut);
280     return (code);
281 }
282
283 /* dumpWatcher
284  *      monitors the state of a database dump. If the dump calls do not 
285  *      reset the time to live value, the dump times out. In that case,
286  *      we kill the database traversal thread and clean up all the other 
287  *      state. Most importantly, the database is unlocked so that other
288  *      transactions can proceed.
289  */
290
291 dumpWatcher()
292 {
293     afs_int32 code;
294
295     while (1) {                 /*w */
296
297         /* printf("dumpWatcher\n"); */
298         ObtainWriteLock(&dumpSyncPtr->ds_lock);
299
300         if (dumpSyncPtr->statusFlags == 0) {
301             /* dump has finished */
302             goto exit;
303         }
304
305         /* check time to live */
306         if (time(0) > dumpSyncPtr->timeToLive) {        /*i */
307             /* dump has exceeded the allocated time - terminate it */
308             LogError(0, "Database dump timeout exceeded: %s",
309                      ctime(&dumpSyncPtr->timeToLive));
310             LogError(0, "Terminating database dump\n");
311
312             close(dumpSyncPtr->pipeFid[0]);
313             close(dumpSyncPtr->pipeFid[1]);
314
315             code = LWP_DestroyProcess(dumpSyncPtr->dumperPid);
316             if (code)
317                 LogError(code, "dumpWatcher: failed to kill dump thread\n");
318
319             if (dumpSyncPtr->ut) {
320                 code = ubik_AbortTrans(dumpSyncPtr->ut);
321                 if (code)
322                     LogError(code, "Aborting dump transaction\n");
323             }
324
325             memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
326             goto exit;
327         }
328         /*i */
329         ReleaseWriteLock(&dumpSyncPtr->ds_lock);
330         IOMGR_Sleep(5);
331     }                           /*w */
332
333   exit:
334     ReleaseWriteLock(&dumpSyncPtr->ds_lock);
335     /* printf("dumpWatcher exit\n"); */
336     return (0);
337 }