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