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>
13 #include <afs/procmgmt.h>
16 #ifdef IGNORE_SOME_GCC_WARNINGS
17 # pragma GCC diagnostic warning "-Wimplicit-function-declaration"
22 #include <afs/afsint.h>
23 #include <afs/afs_assert.h>
24 #include <afs/prs_fs.h>
28 #include <afs/cellconfig.h>
32 #include <afs/volser.h>
33 #include <afs/vlserver.h>
34 #include <afs/tcdata.h>
36 #include <afs/budb_client.h>
37 #include <afs/bubasics.h>
38 #include <afs/bucoord_prototypes.h>
39 #include <afs/butm_prototypes.h>
40 #include <afs/budb_prototypes.h>
41 #include <afs/afsutil.h>
43 #include "butc_internal.h"
44 #include "error_macros.h"
46 /* GLOBAL CONFIGURATION PARAMETERS */
47 #define BIGCHUNK 102400
49 extern int dump_namecheck;
58 static void initTapeBuffering(void);
59 static int writeDbDump(struct butm_tapeInfo *, afs_uint32, Date, afs_uint32);
60 static int restoreDbEntries(struct butm_tapeInfo *, struct rstTapeInfo *);
62 int getTapeData(struct butm_tapeInfo *, struct rstTapeInfo *, void *,
64 int restoreDbHeader(struct butm_tapeInfo *, struct rstTapeInfo *,
65 struct structDumpHeader *);
66 int restoreDbDump(struct butm_tapeInfo *, struct rstTapeInfo *,
67 struct structDumpHeader *);
68 int restoreText(struct butm_tapeInfo *, struct rstTapeInfo *,
69 struct structDumpHeader *);
73 void * KeepAlive(void *);
75 * create a dump entry for a saved database
79 CreateDBDump(struct budb_dumpEntry *dumpEntryPtr)
83 memset(dumpEntryPtr, 0, sizeof(struct budb_dumpEntry));
85 strcpy(dumpEntryPtr->name, DUMP_TAPE_NAME);
86 strcpy(dumpEntryPtr->tapes.format, DUMP_TAPE_NAME);
87 strcat(dumpEntryPtr->tapes.format, ".%d");
88 strcpy(dumpEntryPtr->volumeSetName, "");
89 strcpy(dumpEntryPtr->dumpPath, "");
90 dumpEntryPtr->created = 0; /* let database assign it */
91 dumpEntryPtr->incTime = 0;
92 dumpEntryPtr->nVolumes = 0;
93 dumpEntryPtr->initialDumpID = 0;
94 dumpEntryPtr->parent = 0;
95 dumpEntryPtr->level = 0;
96 dumpEntryPtr->tapes.maxTapes = 0;
97 dumpEntryPtr->tapes.b = 1;
99 /* now call the database to create the entry */
100 code = bcdb_CreateDump(dumpEntryPtr);
104 struct tapeEntryList {
105 struct tapeEntryList *next;
106 afs_uint32 oldDumpId;
107 struct budb_tapeEntry tapeEnt;
109 struct tapeEntryList *listEntryHead;
110 struct tapeEntryList *listEntryPtr;
111 #define tapeEntryPtr (&listEntryPtr->tapeEnt)
112 struct budb_dumpEntry lastDump; /* the last dump of this volset */
115 * Load a DB tape, read and over write its label.
116 * Leave the tape mounted.
119 GetDBTape(afs_int32 taskId, Date expires, struct butm_tapeInfo *tapeInfoPtr,
120 afs_uint32 dumpid, afs_int32 sequence, int queryFlag,
125 char tapeName[BU_MAXTAPELEN];
131 struct butm_tapeLabel oldTapeLabel, newLabel;
132 struct tapeEntryList *endList;
134 /* construct the name of the tape */
135 sprintf(tapeName, "%s.%-d", DUMP_TAPE_NAME, sequence);
137 interactiveFlag = queryFlag;
140 while (!*wroteLabel) { /*w */
141 if (interactiveFlag) { /* need a tape to write */
143 PromptForTape(SAVEDBOPCODE, tapeName, dumpid, taskId,
151 code = butm_Mount(tapeInfoPtr, tapeName);
153 TapeLog(0, taskId, code, tapeInfoPtr->error, "Can't open tape\n");
157 memset(&oldTapeLabel, 0, sizeof(oldTapeLabel));
158 code = butm_ReadLabel(tapeInfoPtr, &oldTapeLabel, 1); /* rewind tape */
160 oldTapeLabel.useCount = 0; /* no label exists */
161 oldTapeLabel.structVersion = 0;
162 strcpy(oldTapeLabel.pName, "");
164 /* If tape has a name, it must be null or database tape name */
165 if (dump_namecheck && strcmp(oldTapeLabel.AFSName, "")
166 && !databaseTape(oldTapeLabel.AFSName)) {
167 char gotName[BU_MAXTAPELEN + 32];
169 LABELNAME(gotName, &oldTapeLabel);
171 "This tape %s must be a database tape or NULL tape\n",
175 unmountTape(taskId, tapeInfoPtr);
179 /* Do not overwrite a tape that belongs to this dump */
180 if (oldTapeLabel.dumpid && (oldTapeLabel.dumpid == dumpid)) {
181 ErrorLog(0, taskId, 0, 0,
182 "Can't overwrite tape containing the dump in progress\n");
186 /* On first tape, the savedb has not started yet, so the database is not locked
187 * and we can therefore, access information from it. This is easier to do because
188 * database dumps don't have appended dumps (nor appended).
192 struct budb_dumpEntry de, de2;
194 /* Verify the tape has not expired
195 * Early database dumps don't have a dumpid
197 if (!tapeExpired(&oldTapeLabel)) {
198 TLog(taskId, "This tape has not expired\n");
202 /* Since the dumpset on this tape will be deleted from database, check if
203 * any of the dumps in this dumpset are most-recent-dumps.
205 for (dmp = oldTapeLabel.dumpid; dmp; dmp = de.appendedDumpID) {
206 if (dmp == lastDump.id) {
207 memcpy(&de, &lastDump, sizeof(de));
208 memcpy(&de2, &lastDump, sizeof(de2));
210 code = bcdb_FindDumpByID(dmp, &de);
213 sprintf(strlevel, "%d", de.level);
215 bcdb_FindLatestDump(de.volumeSetName, strlevel,
221 if (de.id == de2.id) {
222 if (strcmp(DUMP_TAPE_NAME, de2.name) == 0) {
223 ErrorLog(0, taskId, 0, 0,
224 "Warning: Overwriting most recent dump %s (DumpID %u)\n",
227 ErrorLog(0, taskId, 0, 0,
228 "Warning: Overwriting most recent dump of the '%s' volumeset: %s (DumpID %u)\n",
229 de.volumeSetName, de.name, de.id);
235 /* Otherwise, the savedb is in progress and we can't
236 * access the database (it's locked). So we rely on the
237 * information available (and not the backup database).
240 /* Check the tape's expiration date. Use the expiration on the label */
241 gettimeofday(&tp, NULL);
243 if (curTime < oldTapeLabel.expirationDate) {
244 TLog(taskId, "This tape has not expired\n");
248 /* Check if this previous-dump of the dump-in-progress is on this tape */
249 if (oldTapeLabel.dumpid
250 && (oldTapeLabel.dumpid == lastDump.id)) {
251 ErrorLog(0, taskId, 0, 0,
252 "Warning: Overwriting most recent dump %s (DumpID %u)\n",
253 lastDump.name, lastDump.id);
259 GetNewLabel(tapeInfoPtr, oldTapeLabel.pName, tapeName, &newLabel);
260 newLabel.expirationDate = expires;
261 newLabel.useCount = oldTapeLabel.useCount + 1;
262 newLabel.dumpid = dumpid;
263 newLabel.size = tapeInfoPtr->tapeSize;
265 code = butm_Create(tapeInfoPtr, &newLabel, 1); /* rewind tape */
267 TapeLog(0, taskId, code, tapeInfoPtr->error,
268 "Can't label tape\n");
274 /* Initialize a tapeEntry for later inclusion into the database */
276 (struct tapeEntryList *)malloc(sizeof(struct tapeEntryList));
278 ERROR_EXIT(TC_NOMEMORY);
279 memset(listEntryPtr, 0, sizeof(struct tapeEntryList));
281 /* Remember dumpid so we can delete it later */
282 if ((oldTapeLabel.structVersion >= TAPE_VERSION_3)
283 && oldTapeLabel.dumpid)
284 listEntryPtr->oldDumpId = oldTapeLabel.dumpid;
286 /* Fill in tape entry so we can save it later */
287 strcpy(tapeEntryPtr->name, TNAME(&newLabel));
288 tapeEntryPtr->flags = BUDB_TAPE_BEINGWRITTEN;
289 tapeEntryPtr->written = newLabel.creationTime;
290 tapeEntryPtr->expires = expires;
291 tapeEntryPtr->seq = sequence;
292 tapeEntryPtr->useCount = oldTapeLabel.useCount + 1;
293 tapeEntryPtr->dump = dumpid;
294 tapeEntryPtr->useKBytes = 0;
295 tapeEntryPtr->labelpos = 0;
297 /* Thread onto end of single-linked list */
299 endList = listEntryHead;
300 while (endList->next)
301 endList = endList->next;
302 endList->next = listEntryPtr;
304 listEntryHead = listEntryPtr;
312 * With the list of tapes, free the structures.
318 struct tapeEntryList *next;
320 listEntryPtr = listEntryHead;
321 while (listEntryPtr) {
322 next = listEntryPtr->next;
327 listEntryHead = NULL;
332 * With the list of tapes, add them to the database.
333 * Also delete any olddumpids that are around.
337 addTapesToDb(afs_int32 taskId)
341 struct tapeEntryList *next;
343 listEntryPtr = listEntryHead;
344 while (listEntryPtr) {
345 next = listEntryPtr->next;
347 /* Remove the old database entry */
348 if (listEntryPtr->oldDumpId) {
349 i = bcdb_deleteDump(listEntryPtr->oldDumpId, 0, 0, 0);
350 if (i && (i != BUDB_NOENT)) {
351 ErrorLog(0, taskId, i, 0,
352 "Unable to delete old DB entry %u.\n",
353 listEntryPtr->oldDumpId);
357 /* Add the tape to the database */
358 code = bcdb_UseTape(tapeEntryPtr, &new);
360 ErrorLog(0, taskId, code, 0, "Can't add tape to database: %s\n",
365 code = bcdb_FinishTape(tapeEntryPtr);
367 ErrorLog(0, taskId, code, 0, "Can't finish tape: %s\n",
381 * this code assumes that the blocksize on reads is smaller than
382 * the blocksize on writes
386 writeDbDump(struct butm_tapeInfo *tapeInfoPtr, afs_uint32 taskId,
387 Date expires, afs_uint32 dumpid)
390 afs_int32 writeBufNbytes = 0;
391 char *writeBlock = 0;
392 char *writeBuffer = 0;
394 afs_int32 transferSize;
396 char *readBufPtr = NULL;
397 afs_int32 maxReadSize;
402 afs_int32 chunksize = 0;
403 afs_int32 tc_EndMargin, tc_KEndMargin, kRemaining;
407 #ifdef AFS_PTHREAD_ENV
409 pthread_attr_t tattr;
415 extern struct tapeConfig globalTapeConfig;
416 extern struct udbHandleS udbHandle;
418 blockSize = BUTM_BLKSIZE;
419 writeBlock = (char *)malloc(BUTM_BLOCKSIZE);
421 ERROR_EXIT(TC_NOMEMORY);
423 writeBuffer = writeBlock + sizeof(struct blockMark);
424 memset(writeBuffer, 0, BUTM_BLKSIZE);
428 * The margin of space to check for end of tape is set to the
429 * amount of space used to write an end-of-tape multiplied by 2.
430 * The amount of space is size of a 16K EODump marker, its EOF
431 * marker, and up to two EOF markers done on close (1 16K blocks +
434 tc_EndMargin = (16384 + 3 * globalTapeConfig.fileMarkSize) * 2;
435 tc_KEndMargin = tc_EndMargin / 1024;
437 /* have to write enclose the dump in file marks */
438 code = butm_WriteFileBegin(tapeInfoPtr);
440 ErrorLog(0, taskId, code, tapeInfoPtr->error,
441 "Can't write FileBegin on tape\n");
445 writeBufPtr = &writeBuffer[0];
448 charList.charListT_val = 0;
449 charList.charListT_len = 0;
452 /* When no data in buffer, read data from the budb_server */
453 if (charList.charListT_len == 0) {
454 /* get more data. let rx allocate space */
455 if (charList.charListT_val) {
456 free(charList.charListT_val);
457 charList.charListT_val = 0;
462 ubik_Call_SingleServer(BUDB_DumpDB, udbHandle.uh_client,
463 UF_SINGLESERVER, firstcall,
464 maxReadSize, &charList, &done);
466 ErrorLog(0, taskId, code, 0, "Can't read database\n");
470 /* If this if the first call to the budb server, create a thread
471 * that will keep the connection alive (during tape changes).
474 #ifdef AFS_PTHREAD_ENV
475 code = pthread_attr_init(&tattr);
477 ErrorLog(0, taskId, code, 0,
478 "Can't pthread_attr_init Keep-alive process\n");
483 pthread_attr_setdetachstate(&tattr,
484 PTHREAD_CREATE_DETACHED);
486 ErrorLog(0, taskId, code, 0,
487 "Can't pthread_attr_setdetachstate Keep-alive process\n");
492 code = pthread_create(&alivePid, &tattr, KeepAlive, 0);
493 AFS_SIGSET_RESTORE();
496 LWP_CreateProcess(KeepAlive, 16384, 1, (void *)NULL,
497 "Keep-alive process", &alivePid);
499 /* XXX should we check code here ??? XXX */
503 readBufPtr = charList.charListT_val;
506 if ((charList.charListT_len == 0) && done)
509 /* compute how many bytes and transfer to the write Buffer */
511 (charList.charListT_len <
513 writeBufNbytes)) ? charList.charListT_len : (blockSize -
516 memcpy(writeBufPtr, readBufPtr, transferSize);
517 charList.charListT_len -= transferSize;
518 writeBufPtr += transferSize;
519 readBufPtr += transferSize;
520 writeBufNbytes += transferSize;
522 /* If filled the write buffer, then write it to tape */
523 if (writeBufNbytes == blockSize) {
524 code = butm_WriteFileData(tapeInfoPtr, writeBuffer, 1, blockSize);
526 ErrorLog(0, taskId, code, tapeInfoPtr->error,
527 "Can't write data on tape\n");
531 memset(writeBuffer, 0, blockSize);
532 writeBufPtr = &writeBuffer[0];
535 /* Every BIGCHUNK bytes check if aborted */
536 chunksize += blockSize;
537 if (chunksize > BIGCHUNK) {
539 if (checkAbortByTaskId(taskId))
540 ERROR_EXIT(TC_ABORTEDBYREQUEST);
544 * check if tape is full - since we filled a blockSize worth of data
545 * assume that there is more data.
547 kRemaining = butm_remainingKSpace(tapeInfoPtr);
548 if (kRemaining < tc_KEndMargin) {
549 code = butm_WriteFileEnd(tapeInfoPtr);
551 ErrorLog(0, taskId, code, tapeInfoPtr->error,
552 "Can't write FileEnd on tape\n");
556 code = butm_WriteEOT(tapeInfoPtr);
558 ErrorLog(0, taskId, code, tapeInfoPtr->error,
559 "Can't write end-of-dump on tape\n");
563 /* Mark tape as having been written */
564 tapeEntryPtr->useKBytes =
565 tapeInfoPtr->kBytes + (tapeInfoPtr->nBytes ? 1 : 0);
566 tapeEntryPtr->flags = BUDB_TAPE_WRITTEN;
568 unmountTape(taskId, tapeInfoPtr);
570 /* Get next tape and writes its label */
573 GetDBTape(taskId, expires, tapeInfoPtr, dumpid, sequence,
578 code = butm_WriteFileBegin(tapeInfoPtr);
580 ErrorLog(0, taskId, code, tapeInfoPtr->error,
581 "Can't write FileBegin on tape\n");
588 /* no more data to be read - if necessary, flush out the last buffer */
589 if (writeBufNbytes > 0) {
590 code = butm_WriteFileData(tapeInfoPtr, writeBuffer, 1, blockSize);
592 ErrorLog(1, taskId, code, tapeInfoPtr->error,
593 "Can't write data on tape\n");
598 code = butm_WriteFileEnd(tapeInfoPtr);
600 ErrorLog(0, taskId, code, tapeInfoPtr->error,
601 "Can't write FileEnd on tape\n");
605 /* Mark tape as having been written */
606 tapeEntryPtr->useKBytes =
607 tapeInfoPtr->kBytes + (tapeInfoPtr->nBytes ? 1 : 0);
608 tapeEntryPtr->flags = BUDB_TAPE_WRITTEN;
611 /* Let the KeepAlive process stop on its own */
613 ubik_Call_SingleServer(BUDB_DumpDB, udbHandle.uh_client,
614 UF_END_SINGLESERVER, 0);
618 if (charList.charListT_val)
619 free(charList.charListT_val);
624 * dump backup database to tape
628 saveDbToTape(void *param)
630 struct saveDbIf *saveDbIfPtr = (struct saveDbIf *)param;
637 struct butm_tapeInfo tapeInfo;
638 struct budb_dumpEntry dumpEntry;
640 extern struct deviceSyncNode *deviceLatch;
641 extern struct tapeConfig globalTapeConfig;
643 expires = (saveDbIfPtr->archiveTime ? NEVERDATE : 0);
644 taskId = saveDbIfPtr->taskId;
646 setStatus(taskId, DRIVE_WAIT);
647 EnterDeviceQueue(deviceLatch); /* lock tape device */
648 clearStatus(taskId, DRIVE_WAIT);
651 TLog(taskId, "SaveDb\n");
653 tapeInfo.structVersion = BUTM_MAJORVERSION;
654 code = butm_file_Instantiate(&tapeInfo, &globalTapeConfig);
656 ErrorLog(0, taskId, code, tapeInfo.error,
657 "Can't initialize tape module\n");
661 /* Determine what the last database dump was */
662 memset(&lastDump, 0, sizeof(lastDump));
663 code = bcdb_FindLatestDump("", "", &lastDump);
665 if (code != BUDB_NODUMPNAME) {
666 ErrorLog(0, taskId, code, 0, "Can't read backup database\n");
669 memset(&lastDump, 0, sizeof(lastDump));
672 code = CreateDBDump(&dumpEntry); /* Create a dump for this tape */
674 ErrorLog(0, taskId, code, 0, "Can't create dump in database\n");
679 listEntryHead = NULL;
681 /* Get the tape and write a new label to it */
683 GetDBTape(taskId, expires, &tapeInfo, dumpEntry.id, 1, autoQuery,
687 * If did not write the label, remove created dump
688 * Else if wrote the label, remove old dump from db so it's not saved.
691 i = bcdb_deleteDump(dumpEntry.id, 0, 0, 0);
693 if (i && (i != BUDB_NOENT))
694 ErrorLog(0, taskId, i, 0, "Unable to delete DB entry %u.\n",
696 } else if (listEntryHead->oldDumpId) {
697 i = bcdb_deleteDump(listEntryHead->oldDumpId, 0, 0, 0);
698 listEntryHead->oldDumpId = 0;
699 if (i && (i != BUDB_NOENT)) {
700 ErrorLog(0, taskId, i, 0, "Unable to delete old DB entry %u.\n",
701 listEntryHead->oldDumpId);
708 TapeLog(1, taskId, 0, 0, "Tape accepted - now dumping database\n");
710 /* we have a writable tape */
711 code = writeDbDump(&tapeInfo, taskId, expires, dumpEntry.id);
715 /* Now delete the entries between time 0 and archive-time */
716 if (saveDbIfPtr->archiveTime)
717 code = bcdb_deleteDump(0, 0, saveDbIfPtr->archiveTime, 0);
720 unmountTape(taskId, &tapeInfo);
722 /* Add this dump's tapes to the database and mark it finished */
724 i = addTapesToDb(taskId);
728 i = bcdb_FinishDump(&dumpEntry);
734 if (code == TC_ABORTEDBYREQUEST) {
735 TLog(taskId, "SaveDb: Aborted by request\n");
736 clearStatus(taskId, ABORT_REQUEST);
737 setStatus(taskId, ABORT_DONE);
739 TapeLog(0, taskId, code, 0, "SaveDb: Finished with errors\n");
740 setStatus(taskId, TASK_ERROR);
742 TLog(taskId, "SaveDb: Finished\n");
744 setStatus(taskId, TASK_DONE);
747 LeaveDeviceQueue(deviceLatch);
748 return (void *)(intptr_t)(code);
753 * Make a database dump entry given a tape label.
757 makeDbDumpEntry(struct budb_tapeEntry *tapeEntPtr,
758 struct budb_dumpEntry *dumpEntryPtr)
760 memset(dumpEntryPtr, 0, sizeof(struct budb_dumpEntry));
762 dumpEntryPtr->id = tapeEntPtr->dump;
763 dumpEntryPtr->initialDumpID = 0;
764 dumpEntryPtr->parent = 0;
765 dumpEntryPtr->level = 0;
766 dumpEntryPtr->flags = 0;
768 strcpy(dumpEntryPtr->volumeSetName, "");
769 strcpy(dumpEntryPtr->dumpPath, "");
770 strcpy(dumpEntryPtr->name, DUMP_TAPE_NAME);
772 dumpEntryPtr->created = tapeEntPtr->dump;
773 dumpEntryPtr->incTime = 0;
774 dumpEntryPtr->nVolumes = 0;
776 strcpy(dumpEntryPtr->tapes.format, DUMP_TAPE_NAME);
777 strcat(dumpEntryPtr->tapes.format, ".%d");
778 dumpEntryPtr->tapes.b = tapeEntPtr->seq;
779 dumpEntryPtr->tapes.maxTapes = 0;
784 * prompt for a specific database tape
788 readDbTape(struct butm_tapeInfo *tapeInfoPtr,
789 struct rstTapeInfo *rstTapeInfoPtr, int query)
794 struct butm_tapeLabel oldTapeLabel;
795 char AFStapeName[BU_MAXTAPELEN], tapeName[BU_MAXTAPELEN];
796 struct tapeEntryList *endList;
798 struct budb_dumpEntry de;
799 struct budb_tapeEntry te;
801 taskId = rstTapeInfoPtr->taskId;
802 interactiveFlag = query;
804 /* construct the name of the tape */
805 sprintf(AFStapeName, "%s.%-d", DUMP_TAPE_NAME, rstTapeInfoPtr->tapeSeq);
806 strcpy(tapeName, AFStapeName);
808 /* Will prompt for the latest saved database tape, but will accept any one */
809 if (rstTapeInfoPtr->tapeSeq == 1) {
810 code = bcdb_FindLatestDump("", "", &de);
812 rstTapeInfoPtr->dumpid = de.id;
814 if (rstTapeInfoPtr->dumpid) {
816 bcdb_FindTapeSeq(rstTapeInfoPtr->dumpid, rstTapeInfoPtr->tapeSeq,
819 strcpy(tapeName, te.name);
824 if (interactiveFlag) { /* need a tape to read */
826 PromptForTape(RESTOREDBOPCODE, tapeName,
827 rstTapeInfoPtr->dumpid, taskId, tapecount);
834 code = butm_Mount(tapeInfoPtr, tapeName);
836 TapeLog(0, taskId, code, tapeInfoPtr->error, "Can't open tape\n");
840 code = butm_ReadLabel(tapeInfoPtr, &oldTapeLabel, 1); /* will rewind the tape */
842 TapeLog(0, taskId, code, tapeInfoPtr->error,
843 "Can't read tape label\n");
847 /* Check for name of tape and matching dump id (if applicable). */
848 if ((strcmp(oldTapeLabel.AFSName, AFStapeName) != 0)
849 || ((rstTapeInfoPtr->tapeSeq != 1)
850 && (oldTapeLabel.dumpid != rstTapeInfoPtr->dumpid))) {
851 char expTape[BU_MAXTAPELEN + 32];
852 char gotTape[BU_MAXTAPELEN + 32];
854 TAPENAME(expTape, tapeName, rstTapeInfoPtr->dumpid);
855 TAPENAME(gotTape, oldTapeLabel.AFSName, oldTapeLabel.dumpid);
857 TLog(taskId, "Tape label expected %s, label seen %s\n", expTape,
862 if (rstTapeInfoPtr->tapeSeq == 1) /* Remember this dumpId */
863 rstTapeInfoPtr->dumpid = oldTapeLabel.dumpid;
868 unmountTape(taskId, tapeInfoPtr);
872 /* Initialize a tapeEntry for later inclusion into the database */
874 (struct tapeEntryList *)malloc(sizeof(struct tapeEntryList));
876 ERROR_EXIT(TC_NOMEMORY);
877 memset(listEntryPtr, 0, sizeof(struct tapeEntryList));
879 /* Fill in tape entry so we can save it later */
880 strcpy(tapeEntryPtr->name, TNAME(&oldTapeLabel));
881 tapeEntryPtr->dump = oldTapeLabel.dumpid;
882 tapeEntryPtr->flags = BUDB_TAPE_BEINGWRITTEN;
883 tapeEntryPtr->written = oldTapeLabel.creationTime;
884 tapeEntryPtr->expires = oldTapeLabel.expirationDate;
885 tapeEntryPtr->seq = extractTapeSeq(oldTapeLabel.AFSName);
886 tapeEntryPtr->useCount = oldTapeLabel.useCount;
887 tapeEntryPtr->useKBytes = 0;
888 tapeEntryPtr->labelpos = 0;
890 /* Thread onto end of single-linked list */
892 endList = listEntryHead;
893 while (endList->next)
894 endList = endList->next;
895 endList->next = listEntryPtr;
897 listEntryHead = listEntryPtr;
903 static afs_int32 nbytes = 0; /* # bytes left in buffer */
905 initTapeBuffering(void)
912 * restore all the items on the tape
914 * tape positioned after tape label
918 restoreDbEntries(struct butm_tapeInfo *tapeInfoPtr,
919 struct rstTapeInfo *rstTapeInfoPtr)
921 struct structDumpHeader netItemHeader, hostItemHeader;
923 afs_int32 taskId, code = 0;
926 taskId = rstTapeInfoPtr->taskId;
928 /* clear state for the buffer routine(s) */
931 code = butm_ReadFileBegin(tapeInfoPtr);
933 ErrorLog(0, taskId, code, tapeInfoPtr->error,
934 "Can't read FileBegin on tape\n");
938 /* get the first item-header */
939 memset(&netItemHeader, 0, sizeof(netItemHeader));
941 getTapeData(tapeInfoPtr, rstTapeInfoPtr, &netItemHeader,
942 sizeof(netItemHeader));
945 structDumpHeader_ntoh(&netItemHeader, &hostItemHeader);
948 switch (hostItemHeader.type) {
951 restoreDbHeader(tapeInfoPtr, rstTapeInfoPtr, &hostItemHeader);
957 if (++count > 25) { /*every 25 dumps, wait */
962 restoreDbDump(tapeInfoPtr, rstTapeInfoPtr, &hostItemHeader);
972 case SD_TEXT_DUMPSCHEDULE:
973 case SD_TEXT_VOLUMESET:
974 case SD_TEXT_TAPEHOSTS:
975 code = restoreText(tapeInfoPtr, rstTapeInfoPtr, &hostItemHeader);
985 TLog(taskId, "Unknown database header type %d\n",
986 hostItemHeader.type);
992 code = butm_ReadFileEnd(tapeInfoPtr);
994 ErrorLog(0, taskId, code, tapeInfoPtr->error,
995 "Can't read EOF on tape\n");
999 /* Mark tape as having been written */
1000 tapeEntryPtr->useKBytes =
1001 tapeInfoPtr->kBytes + (tapeInfoPtr->nBytes ? 1 : 0);
1002 tapeEntryPtr->flags = BUDB_TAPE_WRITTEN;
1008 /* restoreDbFromTape
1009 * restore the backup database from tape.
1013 restoreDbFromTape(void *param)
1015 afs_uint32 taskId = (intptr_t) param;
1018 struct butm_tapeInfo tapeInfo;
1019 struct rstTapeInfo rstTapeInfo;
1020 struct budb_dumpEntry dumpEntry;
1022 extern struct tapeConfig globalTapeConfig;
1023 extern struct deviceSyncNode *deviceLatch;
1025 setStatus(taskId, DRIVE_WAIT);
1026 EnterDeviceQueue(deviceLatch); /* lock tape device */
1027 clearStatus(taskId, DRIVE_WAIT);
1030 TLog(taskId, "RestoreDb\n");
1032 tapeInfo.structVersion = BUTM_MAJORVERSION;
1033 code = butm_file_Instantiate(&tapeInfo, &globalTapeConfig);
1035 ErrorLog(0, taskId, code, tapeInfo.error,
1036 "Can't initialize tape module\n");
1040 listEntryHead = NULL;
1042 rstTapeInfo.taskId = taskId;
1043 rstTapeInfo.tapeSeq = 1;
1044 rstTapeInfo.dumpid = 0;
1046 code = readDbTape(&tapeInfo, &rstTapeInfo, autoQuery);
1050 code = restoreDbEntries(&tapeInfo, &rstTapeInfo);
1055 /* Now put this dump into the database */
1056 /* Make a dump entry from first tape */
1057 listEntryPtr = listEntryHead;
1059 makeDbDumpEntry(tapeEntryPtr, &dumpEntry);
1060 if (dumpEntry.id != 0) {
1061 i = bcdb_CreateDump(&dumpEntry);
1063 if (i == BUDB_DUMPIDEXISTS)
1065 "Dump id %d not added to database - already exists\n",
1068 TapeLog(0, taskId, i, 0,
1069 "Dump id %d not added to database\n",
1072 i = addTapesToDb(taskId);
1076 i = bcdb_FinishDump(&dumpEntry);
1084 unmountTape(taskId, &tapeInfo);
1087 if (code == TC_ABORTEDBYREQUEST) {
1088 TLog(taskId, "RestoreDb: Aborted by request\n");
1089 clearStatus(taskId, ABORT_REQUEST);
1090 setStatus(taskId, ABORT_DONE);
1092 TapeLog(0, taskId, code, 0, "RestoreDb: Finished with errors\n");
1093 setStatus(taskId, TASK_ERROR);
1095 TLog(taskId, "RestoreDb: Finished\n");
1098 LeaveDeviceQueue(deviceLatch);
1099 setStatus(taskId, TASK_DONE);
1101 return (void *)(intptr_t)(code);
1106 * While dumping the database, keeps the connection alive.
1107 * Every 10 seconds, wake up and ask to read 0 bytes of the database.
1108 * This resets the database's internal timer so that it does not
1109 * prematuraly quit (on asking for new tapes and such).
1111 * Use the same udbHandle as writeDbDump so we go to the same server.
1114 KeepAlive(void *unused)
1120 extern struct udbHandleS udbHandle;
1123 #ifdef AFS_PTHREAD_ENV
1128 charList.charListT_val = 0;
1129 charList.charListT_len = 0;
1131 ubik_Call_SingleServer(BUDB_DumpDB, udbHandle.uh_client,
1132 UF_SINGLESERVER, 0, 0, &charList, &done);
1141 * restore special items in the header
1145 restoreDbHeader(struct butm_tapeInfo *tapeInfo,
1146 struct rstTapeInfo *rstTapeInfoPtr,
1147 struct structDumpHeader *nextHeader)
1149 struct structDumpHeader netItemHeader;
1150 struct DbHeader netDbHeader, hostDbHeader;
1153 extern struct udbHandleS udbHandle;
1155 /* Read the database header */
1156 memset(&netDbHeader, 0, sizeof(netDbHeader));
1158 getTapeData(tapeInfo, rstTapeInfoPtr, &netDbHeader,
1159 sizeof(netDbHeader));
1162 DbHeader_ntoh(&netDbHeader, &hostDbHeader);
1164 /* Add the database header to the database */
1166 ubik_BUDB_RestoreDbHeader(udbHandle.uh_client, 0,
1169 ErrorLog(0, rstTapeInfoPtr->taskId, code, 0,
1170 "Can't restore DB Header\n");
1174 /* get the next item-header */
1175 memset(nextHeader, 0, sizeof(*nextHeader));
1177 getTapeData(tapeInfo, rstTapeInfoPtr, &netItemHeader,
1178 sizeof(netItemHeader));
1181 structDumpHeader_ntoh(&netItemHeader, nextHeader);
1189 * restore a single dump, including all its tapes and volumes, from
1192 * nextHeader - ptr to structure for return value
1194 * nextHeader - next structure header from tape
1196 * upon entry, the dump structure header has been read confirming that
1197 * a database dump tree exists on the tape
1201 restoreDbDump(struct butm_tapeInfo *tapeInfo,
1202 struct rstTapeInfo *rstTapeInfoPtr,
1203 struct structDumpHeader *nextHeader)
1205 struct budb_dumpEntry netDumpEntry, hostDumpEntry;
1206 struct budb_tapeEntry netTapeEntry, hostTapeEntry;
1207 struct budb_volumeEntry netVolumeEntry, hostVolumeEntry;
1208 struct structDumpHeader netItemHeader;
1209 int restoreThisDump = 1;
1212 extern struct udbHandleS udbHandle;
1214 /* read dump entry */
1215 memset(&netDumpEntry, 0, sizeof(netDumpEntry));
1217 getTapeData(tapeInfo, rstTapeInfoPtr, &netDumpEntry,
1218 sizeof(netDumpEntry));
1222 /* If database tape does not have a dumpid (AFS 3.3) then no initial/appended dumps */
1223 if (rstTapeInfoPtr->dumpid == 0) {
1224 netDumpEntry.initialDumpID = 0;
1225 netDumpEntry.appendedDumpID = 0;
1228 dumpEntry_ntoh(&netDumpEntry, &hostDumpEntry);
1230 /* The dump entry for this database tape is incomplete, so don't include it */
1231 if (hostDumpEntry.id == rstTapeInfoPtr->dumpid)
1232 restoreThisDump = 0;
1234 /* add the dump to the database */
1235 if (restoreThisDump) {
1237 threadEntryDir(&hostDumpEntry, sizeof(hostDumpEntry),
1243 /* get the next item-header */
1244 memset(nextHeader, 0, sizeof(*nextHeader));
1246 getTapeData(tapeInfo, rstTapeInfoPtr, &netItemHeader,
1247 sizeof(netItemHeader));
1250 structDumpHeader_ntoh(&netItemHeader, nextHeader);
1252 /* Add every tape to the db */
1253 while (nextHeader->type == SD_TAPE) { /*t */
1255 /* read the tape entry */
1256 memset(&netTapeEntry, 0, sizeof(netTapeEntry));
1258 getTapeData(tapeInfo, rstTapeInfoPtr, &netTapeEntry,
1259 sizeof(netTapeEntry));
1262 tapeEntry_ntoh(&netTapeEntry, &hostTapeEntry);
1264 /* Add the tape to the database */
1265 if (restoreThisDump) {
1267 threadEntryDir(&hostTapeEntry, sizeof(hostTapeEntry),
1273 /* get the next item-header */
1274 memset(nextHeader, 0, sizeof(*nextHeader));
1276 getTapeData(tapeInfo, rstTapeInfoPtr, &netItemHeader,
1277 sizeof(netItemHeader));
1280 structDumpHeader_ntoh(&netItemHeader, nextHeader);
1282 /* Add every volume to the db */
1283 while (nextHeader->type == SD_VOLUME) { /*v */
1285 /* read the volume entry */
1286 memset(&netVolumeEntry, 0, sizeof(netVolumeEntry));
1288 getTapeData(tapeInfo, rstTapeInfoPtr, &netVolumeEntry,
1289 sizeof(netVolumeEntry));
1292 volumeEntry_ntoh(&netVolumeEntry, &hostVolumeEntry);
1294 if (restoreThisDump) {
1296 threadEntryDir(&hostVolumeEntry, sizeof(hostVolumeEntry),
1302 /* get the next item-header */
1303 memset(nextHeader, 0, sizeof(*nextHeader));
1305 getTapeData(tapeInfo, rstTapeInfoPtr, &netItemHeader,
1306 sizeof(netItemHeader));
1309 structDumpHeader_ntoh(&netItemHeader, nextHeader);
1312 /* Finish the tape */
1313 if (restoreThisDump) {
1315 threadEntryDir(&hostTapeEntry, sizeof(hostTapeEntry),
1322 /* Finish the dump */
1323 if (restoreThisDump) {
1325 threadEntryDir(&hostDumpEntry, sizeof(hostDumpEntry),
1336 * Save the specified file as configuration text in the ubik database.
1337 * Have to setup the client text structure so that we can call
1338 * the routine to transmit the text to the db.
1342 saveTextFile(afs_int32 taskId, afs_int32 textType, char *fileName)
1344 udbClientTextP ctPtr = 0;
1348 ctPtr = (udbClientTextP) malloc(sizeof(*ctPtr));
1350 ERROR_EXIT(TC_NOMEMORY);
1352 memset(ctPtr, 0, sizeof(*ctPtr));
1353 ctPtr->textType = textType;
1355 /* lock the text in the database */
1356 code = bc_LockText(ctPtr);
1358 ErrorLog(0, taskId, code, 0, "Can't lock text file\n");
1363 ctPtr->textStream = fopen(fileName, "r");
1364 if (!ctPtr->textStream) {
1365 ErrorLog(0, taskId, errno, 0, "Can't open text file\n");
1369 /* now send the text to the database */
1370 code = bcdb_SaveTextFile(ctPtr);
1372 ErrorLog(0, taskId, code, 0, "Can't save text file\n");
1378 if (ctPtr->textStream)
1379 fclose(ctPtr->textStream);
1381 bc_UnlockText(ctPtr);
1388 * read the text off the tape, and store it in the appropriate
1389 * text type in the database.
1391 * nextHeader - ptr to struct for return information
1393 * nextHeader - struct header for next item on the tape
1397 restoreText(struct butm_tapeInfo *tapeInfo,
1398 struct rstTapeInfo *rstTapeInfoPtr,
1399 struct structDumpHeader *nextHeader)
1403 char *readBuffer = 0;
1404 afs_int32 readBlockSize;
1405 afs_int32 transferSize;
1406 struct structDumpHeader netItemHeader;
1410 udbClientTextP ctPtr = 0;
1413 ctPtr = (udbClientTextP) malloc(sizeof(*ctPtr));
1415 ERROR_EXIT(TC_NOMEMORY);
1417 /* determine the type of text block */
1418 switch (nextHeader->type) {
1419 case SD_TEXT_DUMPSCHEDULE:
1420 textType = TB_DUMPSCHEDULE;
1423 case SD_TEXT_VOLUMESET:
1424 textType = TB_VOLUMESET;
1427 case SD_TEXT_TAPEHOSTS:
1428 textType = TB_TAPEHOSTS;
1432 ErrorLog(0, rstTapeInfoPtr->taskId, TC_INTERNALERROR, 0,
1433 "Unknown text block\n");
1434 ERROR_EXIT(TC_INTERNALERROR);
1438 /* open the text file */
1439 sprintf(filename, "%s/bu_XXXXXX", gettmpdir());
1440 fid = mkstemp(filename);
1442 ErrorLog(0, rstTapeInfoPtr->taskId, errno, 0,
1443 "Can't open temporary text file: %s\n", filename);
1447 /* allocate buffer for text */
1448 readBlockSize = BUTM_BLKSIZE;
1449 readBuffer = (char *)malloc(readBlockSize);
1451 ERROR_EXIT(TC_NOMEMORY);
1453 /* read the text into the temporary file */
1454 nbytes = nextHeader->size;
1455 while (nbytes > 0) {
1456 transferSize = (readBlockSize < nbytes) ? readBlockSize : nbytes;
1458 /* read it from the tape */
1460 getTapeData(tapeInfo, rstTapeInfoPtr, readBuffer, transferSize);
1464 /* write to the file */
1465 if (write(fid, readBuffer, transferSize) != transferSize) {
1466 ErrorLog(0, rstTapeInfoPtr->taskId, errno, 0,
1467 "Can't write temporary text file: %s\n", filename);
1471 nbytes -= transferSize;
1476 code = saveTextFile(rstTapeInfoPtr->taskId, textType, filename);
1481 /* get the next item-header */
1482 memset(nextHeader, 0, sizeof(*nextHeader));
1484 getTapeData(tapeInfo, rstTapeInfoPtr, &netItemHeader,
1485 sizeof(netItemHeader));
1488 structDumpHeader_ntoh(&netItemHeader, nextHeader);
1503 /* ----------------------------------
1504 * Tape data buffering - for reading database dumps
1505 * ----------------------------------
1508 static char *tapeReadBuffer = 0; /* input buffer */
1509 static char *tapeReadBufferPtr = 0; /* position in buffer */
1512 * Read information from tape, and place the requested number of bytes
1513 * in the buffer supplied
1516 * rstTapeInfoPtr - Info about the dump being restored.
1517 * buffer - buffer for requested data
1518 * requestedBytes - no. of bytes requested
1520 * fn retn - 0, ok, n, error
1524 getTapeData(struct butm_tapeInfo *tapeInfoPtr,
1525 struct rstTapeInfo *rstTapeInfoPtr,
1526 void *out, afs_int32 requestedBytes)
1528 char *buffer = (char *) out;
1529 afs_int32 taskId, transferBytes;
1532 taskId = rstTapeInfoPtr->taskId;
1534 if (checkAbortByTaskId(taskId))
1535 ERROR_EXIT(TC_ABORTEDBYREQUEST);
1537 if (!tapeReadBuffer) {
1538 tapeReadBuffer = (char *)malloc(BUTM_BLOCKSIZE);
1539 if (!tapeReadBuffer)
1540 ERROR_EXIT(TC_NOMEMORY);
1543 while (requestedBytes > 0) {
1545 tapeReadBufferPtr = &tapeReadBuffer[sizeof(struct blockMark)];
1549 butm_ReadFileData(tapeInfoPtr, tapeReadBufferPtr,
1550 BUTM_BLKSIZE, &nbytes);
1552 /* detect if we hit the end-of-tape and get next tape */
1553 if (code == BUTM_ENDVOLUME) {
1554 /* Update fields in tape entry for this tape */
1555 tapeEntryPtr->flags = BUDB_TAPE_WRITTEN;
1556 tapeEntryPtr->useKBytes =
1557 tapeInfoPtr->kBytes + (tapeInfoPtr->nBytes ? 1 : 0);
1559 unmountTape(taskId, tapeInfoPtr);
1561 rstTapeInfoPtr->tapeSeq++;
1562 code = readDbTape(tapeInfoPtr, rstTapeInfoPtr, 1);
1566 code = butm_ReadFileBegin(tapeInfoPtr);
1568 ErrorLog(0, taskId, code, tapeInfoPtr->error,
1569 "Can't read FileBegin on tape\n");
1576 ErrorLog(0, taskId, code, tapeInfoPtr->error,
1577 "Can't read FileData on tape\n");
1583 transferBytes = (nbytes < requestedBytes) ? nbytes : requestedBytes;
1584 memcpy(buffer, tapeReadBufferPtr, transferBytes);
1585 tapeReadBufferPtr += transferBytes;
1586 buffer += transferBytes;
1587 nbytes -= transferBytes;
1588 requestedBytes -= transferBytes;