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>
23 #include <netinet/in.h>
27 #include <sys/types.h>
33 #include <afs/tcdata.h>
34 #include <afs/bubasics.h>
36 #include <afs/budb_client.h>
37 #include <afs/budb_prototypes.h>
38 #include <afs/butm_prototypes.h>
39 #include <afs/bucoord_prototypes.h>
40 #include "error_macros.h"
41 #include "butc_internal.h"
43 #define BELLCHAR 7 /* ascii for bell */
45 /* GLOBAL CONFIGURATION PARAMETERS */
47 extern int queryoperator;
49 /* Handle for the information read from all the tapes of a dump */
50 afs_int32 tapepos; /* when read a label, remember its position */
52 struct butm_tapeLabel tapeLabel, dumpLabel;
53 struct budb_dumpEntry dumpEntry;
54 afs_int32 initialDumpId;
58 extern struct tapeConfig globalTapeConfig;
59 extern struct deviceSyncNode *deviceLatch;
61 static int readDump(afs_uint32, struct butm_tapeInfo *,
62 struct tapeScanInfo *);
63 afs_int32 getScanTape(afs_int32, struct butm_tapeInfo *, char *,
64 afs_int32, int prompt, struct butm_tapeLabel *);
65 afs_int32 RcreateDump(struct tapeScanInfo *, struct volumeHeader *);
66 void copy_ktcPrincipal_to_budbPrincipal(struct ktc_principal *,
67 struct budb_principal *);
70 * print out the tape (dump) label.
73 PrintDumpLabel(struct butm_tapeLabel *labelptr)
75 char tapeName[BU_MAXTAPELEN + 32];
78 printf("Dump label\n");
79 printf("----------\n");
80 TAPENAME(tapeName, labelptr->pName, labelptr->dumpid);
81 printf("permanent tape name = %s\n", tapeName);
82 TAPENAME(tapeName, labelptr->AFSName, labelptr->dumpid);
83 printf("AFS tape name = %s\n", tapeName);
84 t = labelptr->creationTime;
85 printf("creationTime = %s", ctime(&t));
86 if (labelptr->expirationDate) {
87 t = labelptr->expirationDate;
88 printf("expirationDate = %s", cTIME(&t));
90 printf("cell = %s\n", labelptr->cell);
91 printf("size = %u Kbytes\n", labelptr->size);
92 printf("dump path = %s\n", labelptr->dumpPath);
94 if (labelptr->structVersion >= TAPE_VERSION_3) {
95 printf("dump id = %u\n", labelptr->dumpid);
96 printf("useCount = %d\n", labelptr->useCount);
98 printf("-- End of dump label --\n\n");
102 * print the contents of a volume header.
105 PrintVolumeHeader(struct volumeHeader *volHeader)
109 printf("-- volume --\n");
110 printf("volume name: %s\n", volHeader->volumeName);
111 printf("volume ID %d\n", volHeader->volumeID);
112 /* printf("server %d\n", volHeader->server); */
113 printf("dumpSetName: %s\n", volHeader->dumpSetName);
114 printf("dumpID %d\n", volHeader->dumpID);
115 printf("level %d\n", volHeader->level);
116 printf("parentID %d\n", volHeader->parentID);
117 printf("endTime %d\n", volHeader->endTime);
118 /* printf("versionflags %d\n", volHeader->versionflags); */
119 t = volHeader->cloneDate;
120 printf("clonedate %s\n", ctime(&t));
124 * ask a question. returns true or false
138 printf("%s? (y/n) ", st);
140 response = getchar();
143 else if (response == 'n' || response == EOF)
145 printf("please answer y/n\n");
150 * Skips the volume data on the tape. The end of the volume data is
151 * detected by the presence of the volume trailer or by an EOF indication
152 * from butm. This algorithm should be replaced by one that always
153 * detects based on the volume trailer, returning the trailer to the
154 * caller. This is of course more painful.
156 * curTapePtr - tape info structure
157 * Tape must be positioned after volume header
160 * 3 - empty volume, requires special handling
162 * Tape positioned after data, but before file end marker block.
163 * In case of errors, positioned after the error causing block
165 #define BIGCHUNK 102400
168 scanVolData(afs_int32 taskId, struct butm_tapeInfo *curTapePtr,
169 afs_int32 tapeVersion, struct volumeHeader *volumeHeader,
170 struct volumeHeader *volumeTrailer, afs_uint32 *bytesRead)
172 afs_int32 headBytes, tailBytes;
175 int hasdata[2], curr, prev;
176 afs_uint32 chunkSize = 0;
179 afs_int32 rcode, tcode;
181 memset(volumeHeader, 0, sizeof(struct volumeHeader));
183 block = (char *)malloc(2 * BUTM_BLOCKSIZE);
185 return (TC_NOMEMORY);
186 buffer[0] = &block[sizeof(struct blockMark)];
187 buffer[1] = &block[BUTM_BLOCKSIZE + sizeof(struct blockMark)];
188 hasdata[0] = hasdata[1] = 0;
191 tcode = NextFile(curTapePtr); /* guarantees we are at a filemark */
195 /* Read the FileBegin FileMark */
196 code = butm_ReadFileBegin(curTapePtr);
199 * Tapes made with 3.0 have no software EOT markers. Therefore
200 * at this point, we will most likely get a read error, indicating
201 * the end of this dump
203 if ((tapeVersion == TAPE_VERSION_0)
204 || (tapeVersion == TAPE_VERSION_1)) {
206 * then a tape error is possible at this point, and it
207 * signals the end of the dump. Tapes that are continued
208 * have an EOT marker.
210 TapeLog(0, taskId, code, curTapePtr->error,
211 "Read error - end-of-dump inferred\n");
215 if (code != BUTM_EOD)
216 ErrorLog(0, taskId, code, curTapePtr->error,
217 "Can't read FileBegin on tape\n");
221 /* now read the volume header */
222 code = ReadVolHeader(taskId, curTapePtr, volumeHeader);
229 /* Check for abort in the middle of scanning data */
230 if (*bytesRead >= chunkSize) {
231 if (checkAbortByTaskId(taskId))
232 ERROR_EXIT(TC_ABORTEDBYREQUEST);
233 chunkSize += BIGCHUNK;
237 * Read volume date - If prematurely hit the HW EOF
238 * marker, check to see if data contains a volumetrailer.
241 butm_ReadFileData(curTapePtr, buffer[curr], BUTM_BLKSIZE,
245 if ((rcode == BUTM_EOF) || (rcode == BUTM_ENDVOLUME))
248 ErrorLog(0, taskId, rcode, curTapePtr->error,
249 "Can't read FileData on tape\n");
253 *bytesRead += nbytes;
255 if ((nbytes != BUTM_BLKSIZE)
257 (FindVolTrailer(buffer[curr], nbytes, &tailBytes, volumeTrailer)))
260 curr = ((curr == 0) ? 1 : 0); /* Switch buffers */
263 /* Now verify that there is a volume trailer and its valid and copy it */
264 prev = ((curr == 0) ? 1 : 0);
266 (buffer[prev], (hasdata[prev] ? BUTM_BLKSIZE : 0), &headBytes,
267 buffer[curr], nbytes, &tailBytes, volumeTrailer)) {
268 code = TC_MISSINGTRAILER;
269 ErrorLog(0, taskId, code, 0, "Missing volume trailer on tape\n");
271 /* subtract size of the volume trailer from data read */
272 *bytesRead -= sizeof(struct volumeHeader);
276 * If we didn't hit the EOF while reading data, read FileEnd marker
280 tcode = butm_ReadFileEnd(curTapePtr);
282 ErrorLog(0, taskId, tcode, curTapePtr->error,
283 "Can't read EOF on tape\n");
295 * generate the name of the next tape label expected
297 * ptr to static string
301 nextTapeLabel(char *prevTapeName)
306 static char buffer[BU_MAXTAPELEN];
310 /* extract information from previous tape label */
311 strcpy(buffer, prevTapeName);
312 prevdot = strrchr(buffer, '.');
317 seq = extractTapeSeq(prevTapeName);
319 sprintf(prevdot, "%-d", seq);
325 * Read all the information on a tape. If to add to the database, queue
326 * onto list so another thread adds the entries to the database.
328 * taskid - butc task number.
329 * tapeInfoPtr - Tape information.
330 * scanInfoPtr - Info to keep so we can add entries to the db.
333 * non-0 - error. Abort the scan.
334 * moreTapes set to 1 if this is not the last tape in the dump,
335 * 0 if the last tape,
336 * -1 don't know if last tape or not.
340 readDump(afs_uint32 taskId, struct butm_tapeInfo *tapeInfoPtr,
341 struct tapeScanInfo *scanInfoPtr)
344 afs_int32 flags, seq;
345 afs_uint32 nbytes = 0;
346 int newDump = 1, newTape = 1;
347 afs_int32 tapePosition;
348 afs_int32 code = 0, tcode;
350 struct volumeHeader volHeader, volTrailer;
351 struct budb_tapeEntry tapeEntry;
352 struct budb_volumeEntry volEntry;
355 PrintDumpLabel(&scanInfoPtr->dumpLabel);
357 while (moreTapes) { /* While there is a tape to read *//*t */
359 while (1) { /* Read each volume on the tape *//*w */
361 tapePosition = tapeInfoPtr->position; /* remember position */
364 * Skip the volume data
367 scanVolData(taskId, tapeInfoPtr,
368 scanInfoPtr->tapeLabel.structVersion, &volHeader,
369 &volTrailer, &nbytes);
373 if (tcode == TC_ABORTEDBYREQUEST) { /* Aborted */
377 if (tcode == BUTM_EOD) {
378 moreTapes = 0; /* the end of the dump */
382 /* Found a volume but it's incomplete. Skip over these */
383 if (volHeader.volumeID) {
384 TapeLog(0, taskId, tcode, 0,
385 "Warning: volume %s (%u) ignored. Incomplete\n",
386 volHeader.volumeName, volHeader.volumeID);
390 /* No volume was found. We may have hit the EOT or a
391 * bad-spot. Try to skip over this spot.
393 if (badscan < 2) { /* allow 2 errors, then fail */
394 TapeLog(0, taskId, tcode, 0,
395 "Warning: Error in scanning tape - will try skipping volume\n");
398 if (scanInfoPtr->tapeLabel.structVersion >= TAPE_VERSION_4) {
399 TapeLog(0, taskId, tcode, 0,
400 "Warning: Error in scanning tape - end-of-tape inferred\n");
401 moreTapes = 1; /* then assume next tape */
403 ErrorLog(0, taskId, tcode, 0, "Error in scanning tape\n");
404 /* will ask if there is a next tape */
409 PrintVolumeHeader(&volHeader);
411 /* If this is not the first volume fragment, make sure it follows
412 * the last volume fragment
415 if ((volEntry.dump != volHeader.dumpID)
416 || (volEntry.id != volHeader.volumeID)
417 || (volEntry.seq != volHeader.frag - 2)
418 || (strcmp(volEntry.name, volHeader.volumeName))) {
420 "Warning: volume %s (%u) ignored. Incomplete - no last fragment\n",
421 volEntry.name, volEntry.id);
423 if (scanInfoPtr->addDbFlag) {
424 tcode = flushSavedEntries(DUMP_FAILED);
432 /* If this is the first volume fragment, make sure says so */
433 if (scanInfoPtr->addDbFlag && !volEntry.dump
434 && (volHeader.frag != 1)) {
436 "Warning: volume %s (%u) ignored. Incomplete - no first fragment\n",
437 volHeader.volumeName, volHeader.volumeID);
440 /* Check that this volume belongs to the dump we are scanning */
441 else if (scanInfoPtr->dumpLabel.dumpid
442 && (volHeader.dumpID != scanInfoPtr->dumpLabel.dumpid)) {
444 "Warning: volume %s (%u) ignored. Expected DumpId %u, got %u\n",
445 volHeader.volumeName, volHeader.volumeID,
446 scanInfoPtr->dumpLabel.dumpid, volHeader.dumpID);
449 /* Passed tests, Now add to the database (if dbadd flag is set) */
450 else if (scanInfoPtr->addDbFlag) {
451 /* Have enough information to create a dump entry */
453 tcode = RcreateDump(scanInfoPtr, &volHeader);
455 ErrorLog(0, taskId, tcode, 0,
456 "Can't add dump %u to database\n",
463 /* Have enough information to create a tape entry */
465 seq = extractTapeSeq(scanInfoPtr->tapeLabel.AFSName);
467 ERROR_EXIT(TC_INTERNALERROR);
470 useTape(&tapeEntry, volHeader.dumpID,
471 TNAME(&scanInfoPtr->tapeLabel), seq,
472 scanInfoPtr->tapeLabel.useCount,
473 scanInfoPtr->dumpLabel.creationTime,
474 scanInfoPtr->dumpLabel.expirationDate,
477 char gotName[BU_MAXTAPELEN + 32];
479 LABELNAME(gotName, &scanInfoPtr->tapeLabel);
480 ErrorLog(0, taskId, tcode, 0,
481 "Can't add tape %s for dump %u to database\n",
482 gotName, volHeader.dumpID);
488 /* Create the volume entry */
489 flags = ((volHeader.frag == 1) ? BUDB_VOL_FIRSTFRAG : 0);
490 if (!volTrailer.contd)
491 flags |= BUDB_VOL_LASTFRAG;
493 addVolume(&volEntry, volHeader.dumpID,
494 TNAME(&scanInfoPtr->tapeLabel),
495 volHeader.volumeName, volHeader.volumeID,
496 volHeader.cloneDate, tapePosition, nbytes,
497 (volHeader.frag - 1), flags);
499 ErrorLog(0, taskId, tcode, 0,
500 "Can't add volume %s (%u) for dump %u to database\n",
501 volHeader.volumeName, volHeader.volumeID,
507 if (volTrailer.contd) {
508 /* No need to read the EOD marker, we know there is a next tape */
512 if (scanInfoPtr->addDbFlag) {
513 tcode = flushSavedEntries(DUMP_SUCCESS);
522 if (scanInfoPtr->addDbFlag) {
524 finishTape(&tapeEntry,
525 (tapeInfoPtr->kBytes +
526 (tapeInfoPtr->nBytes ? 1 : 0)));
528 char gotName[BU_MAXTAPELEN + 32];
530 LABELNAME(gotName, &scanInfoPtr->tapeLabel);
531 ErrorLog(0, taskId, tcode, 0,
532 "Can't mark tape %s 'completed' for dump %u in database\n",
533 gotName, tapeEntry.dump);
539 /* Ask if there is another tape if we can't figure it out */
541 moreTapes = (queryoperator ? Ask("Are there more tapes") : 1);
543 /* Get the next tape label */
548 unmountTape(taskId, tapeInfoPtr);
550 tapeName = nextTapeLabel(scanInfoPtr->tapeLabel.AFSName);
551 dumpid = scanInfoPtr->tapeLabel.dumpid;
553 getScanTape(taskId, tapeInfoPtr, tapeName, dumpid, 1,
554 &scanInfoPtr->tapeLabel);
562 if (scanInfoPtr->addDbFlag) {
563 tcode = finishDump(&scanInfoPtr->dumpEntry);
565 ErrorLog(0, taskId, tcode, 0,
566 "Can't mark dump %u 'completed' in database\n",
567 scanInfoPtr->dumpEntry.id);
570 tcode = flushSavedEntries(DUMP_SUCCESS);
580 /* Will read a dump, then see if there is a dump following it and
581 * try to read that dump too.
582 * The first tape label is the first dumpLabel.
585 readDumps(afs_uint32 taskId, struct butm_tapeInfo *tapeInfoPtr,
586 struct tapeScanInfo *scanInfoPtr)
590 memcpy(&scanInfoPtr->dumpLabel, &scanInfoPtr->tapeLabel,
591 sizeof(struct butm_tapeLabel));
594 code = readDump(taskId, tapeInfoPtr, scanInfoPtr);
598 if (scanInfoPtr->tapeLabel.structVersion < TAPE_VERSION_4)
601 /* Remember the initial dump and see if appended dump exists */
603 if (!scanInfoPtr->initialDumpId)
604 scanInfoPtr->initialDumpId = scanInfoPtr->dumpEntry.id;
606 c = butm_ReadLabel(tapeInfoPtr, &scanInfoPtr->dumpLabel, 0); /* no rewind */
607 tapepos = tapeInfoPtr->position - 1;
617 getScanTape(afs_int32 taskId, struct butm_tapeInfo *tapeInfoPtr, char *tname,
618 afs_int32 tapeId, int prompt, struct butm_tapeLabel *tapeLabelPtr)
623 char tapename[BU_MAXTAPELEN + 32];
624 char gotname[BU_MAXTAPELEN + 32];
627 /* prompt for a tape */
630 PromptForTape(SCANOPCODE, tname, tapeId, taskId, tapecount);
637 code = butm_Mount(tapeInfoPtr, ""); /* open the tape device */
639 TapeLog(0, taskId, code, tapeInfoPtr->error, "Can't open tape\n");
643 /* read the label on the tape */
644 code = butm_ReadLabel(tapeInfoPtr, tapeLabelPtr, 1); /* rewind tape */
646 ErrorLog(0, taskId, code, tapeInfoPtr->error,
647 "Can't read tape label\n");
650 tapepos = tapeInfoPtr->position - 1;
652 /* Now check that the tape is good */
653 TAPENAME(tapename, tname, tapeId);
654 TAPENAME(gotname, tapeLabelPtr->AFSName, tapeLabelPtr->dumpid);
656 curseq = extractTapeSeq(tapeLabelPtr->AFSName);
658 /* Label can't be null or a bad name */
659 if (!strcmp(tapeLabelPtr->AFSName, "") || (curseq <= 0)) {
660 TLog(taskId, "Expected tape with dump, label seen %s\n", gotname);
664 /* Label can't be a database tape */
665 if (databaseTape(tapeLabelPtr->AFSName)) {
667 "Expected tape with dump. Can't scan database tape %s\n",
672 /* If no name, accept any tape */
673 if (strcmp(tname, "") == 0) {
674 break; /* Start scan on any tape */
677 break; /* The first tape */
679 TLog(taskId, "Expected first tape of dump, label seen %s\n",
686 if (strcmp(tname, tapeLabelPtr->AFSName)
687 || ((tapeLabelPtr->structVersion >= TAPE_VERSION_3)
688 && (tapeLabelPtr->dumpid != tapeId))) {
689 TLog(taskId, "Tape label expected %s, label seen %s\n", tapename,
694 /* We have the correct tape */
698 unmountTape(taskId, tapeInfoPtr);
706 * This set of code fragments read a tape, and add the information to
707 * the database. Builds a literal structure.
712 ScanDumps(void *param)
714 struct scanTapeIf *ptr = (struct scanTapeIf *)param;
716 struct butm_tapeInfo curTapeInfo;
717 struct tapeScanInfo tapeScanInfo;
721 taskId = ptr->taskId;
722 setStatus(taskId, DRIVE_WAIT);
723 EnterDeviceQueue(deviceLatch);
724 clearStatus(taskId, DRIVE_WAIT);
728 TLog(taskId, "ScanTape and add to the database\n");
730 TLog(taskId, "Scantape\n");
732 memset(&tapeScanInfo, 0, sizeof(tapeScanInfo));
733 tapeScanInfo.addDbFlag = ptr->addDbFlag;
735 memset(&curTapeInfo, 0, sizeof(curTapeInfo));
736 curTapeInfo.structVersion = BUTM_MAJORVERSION;
737 code = butm_file_Instantiate(&curTapeInfo, &globalTapeConfig);
739 ErrorLog(0, taskId, code, curTapeInfo.error,
740 "Can't initialize tape module\n");
745 getScanTape(taskId, &curTapeInfo, "", 0, autoQuery,
746 &tapeScanInfo.tapeLabel);
750 code = readDumps(taskId, &curTapeInfo, &tapeScanInfo);
755 unmountTape(taskId, &curTapeInfo);
758 if (code == TC_ABORTEDBYREQUEST) {
759 ErrorLog(0, taskId, 0, 0, "Scantape: Aborted by request\n");
760 clearStatus(taskId, ABORT_REQUEST);
761 setStatus(taskId, ABORT_DONE);
763 ErrorLog(0, taskId, code, 0, "Scantape: Finished with errors\n");
764 setStatus(taskId, TASK_ERROR);
766 TLog(taskId, "Scantape: Finished\n");
770 setStatus(taskId, TASK_DONE);
771 LeaveDeviceQueue(deviceLatch);
772 return (void *)(intptr_t)(code);
782 validatePath(struct butm_tapeLabel *labelptr, char *pathptr)
785 char tapeName[BU_MAXTAPELEN];
788 if (strlen(pathptr) > BU_MAX_DUMP_PATH - 1) {
789 fprintf(stderr, "Invalid pathname - too long\n");
796 strcpy(tapeName, labelptr->AFSName);
798 tp = strrchr(tapeName, '.');
803 up = strrchr(pathptr, '/');
805 fprintf(stderr, "Invalid path name, missing /\n");
810 if (strcmp(up, tp) != 0) {
811 fprintf(stderr, "Invalid path name\n");
813 "Mismatch between tape dump name '%s' and users dump name '%s'\n",
821 * return a pointer to a (static) volume set name string.
823 * ptr - ptr to a dump name
825 * 0 - error. Can't extract volumeset name.
826 * ptr - to static volumeset string.
830 volumesetNamePtr(char *ptr)
832 static char vsname[BU_MAXUNAMELEN];
836 dotPtr = strchr(ptr, '.');
840 dotIndex = dotPtr - ptr;
841 if ((dotIndex + 1) > sizeof(vsname))
842 return (0); /* name too long */
844 strncpy(&vsname[0], ptr, dotIndex);
845 vsname[dotIndex] = 0; /* ensure null terminated */
851 extractDumpName(char *ptr)
853 static char dname[BU_MAXTAPELEN];
857 dotPtr = strrchr(ptr, '.');
861 dotIndex = dotPtr - ptr;
862 if ((dotIndex + 1) > sizeof(dname))
863 return (0); /* name too long */
865 strncpy(&dname[0], ptr, dotIndex);
866 dname[dotIndex] = 0; /* ensure null terminated */
872 * The routine assumes that tape names have an embedded sequence number
873 * as the trialing component. It is suggested that any tape naming
874 * changes retain the trailing seq. number
876 * tapename - ptr to tape name
878 * 0 or positive - sequence number
879 * -1 - error, couldn't extract sequence number
883 extractTapeSeq(char *tapename)
887 sptr = strrchr(tapename, '.');
895 * returns true or false depending on whether the tape is
896 * a database tape or not.
899 databaseTape(char *tapeName)
904 sptr = strrchr(tapeName, '.');
908 c = (int)((char *) sptr - (char *) tapeName);
909 if (strncmp(tapeName, DUMP_TAPE_NAME, c) == 0)
916 RcreateDump(struct tapeScanInfo *tapeScanInfoPtr,
917 struct volumeHeader *volHeaderPtr)
920 struct butm_tapeLabel *dumpLabelPtr = &tapeScanInfoPtr->dumpLabel;
921 struct budb_dumpEntry *dumpEntryPtr = &tapeScanInfoPtr->dumpEntry;
923 /* construct dump entry */
924 memset(dumpEntryPtr, 0, sizeof(struct budb_dumpEntry));
925 dumpEntryPtr->id = volHeaderPtr->dumpID;
926 dumpEntryPtr->initialDumpID = tapeScanInfoPtr->initialDumpId;
927 dumpEntryPtr->parent = volHeaderPtr->parentID;
928 dumpEntryPtr->level = volHeaderPtr->level;
929 dumpEntryPtr->created = volHeaderPtr->dumpID; /* time dump was created */
930 dumpEntryPtr->flags = 0;
931 dumpEntryPtr->incTime = 0;
932 dumpEntryPtr->nVolumes = 0;
933 strcpy(dumpEntryPtr->volumeSetName,
934 volumesetNamePtr(volHeaderPtr->dumpSetName));
935 strcpy(dumpEntryPtr->dumpPath, dumpLabelPtr->dumpPath);
936 strcpy(dumpEntryPtr->name, volHeaderPtr->dumpSetName);
937 default_tapeset(&dumpEntryPtr->tapes, volHeaderPtr->dumpSetName);
938 dumpEntryPtr->tapes.b = extractTapeSeq(dumpLabelPtr->AFSName);
939 copy_ktcPrincipal_to_budbPrincipal(&dumpLabelPtr->creator,
940 &dumpEntryPtr->dumper);
942 code = bcdb_CreateDump(dumpEntryPtr);