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>
22 #include <netinet/in.h>
26 #include <sys/types.h>
32 #include <afs/tcdata.h>
33 #include <afs/bubasics.h>
35 #include "error_macros.h"
37 #define BELLCHAR 7 /* ascii for bell */
39 /* GLOBAL CONFIGURATION PARAMETERS */
41 extern int queryoperator;
43 /* Handle for the information read from all the tapes of a dump */
44 afs_int32 tapepos; /* when read a label, remember its position */
46 struct butm_tapeLabel tapeLabel, dumpLabel;
47 struct budb_dumpEntry dumpEntry;
48 afs_int32 initialDumpId;
52 extern struct tapeConfig globalTapeConfig;
53 extern struct deviceSyncNode *deviceLatch;
58 * print out the tape (dump) label.
61 PrintDumpLabel(struct butm_tapeLabel *labelptr)
63 char tapeName[BU_MAXTAPELEN + 32];
66 printf("Dump label\n");
67 printf("----------\n");
68 TAPENAME(tapeName, labelptr->pName, labelptr->dumpid);
69 printf("permanent tape name = %s\n", tapeName);
70 TAPENAME(tapeName, labelptr->AFSName, labelptr->dumpid);
71 printf("AFS tape name = %s\n", tapeName);
72 t = labelptr->creationTime;
73 printf("creationTime = %s", ctime(&t));
74 if (labelptr->expirationDate) {
75 t = labelptr->expirationDate;
76 printf("expirationDate = %s", cTIME(&t));
78 printf("cell = %s\n", labelptr->cell);
79 printf("size = %u Kbytes\n", labelptr->size);
80 printf("dump path = %s\n", labelptr->dumpPath);
82 if (labelptr->structVersion >= TAPE_VERSION_3) {
83 printf("dump id = %u\n", labelptr->dumpid);
84 printf("useCount = %d\n", labelptr->useCount);
86 printf("-- End of dump label --\n\n");
90 * print the contents of a volume header.
93 PrintVolumeHeader(struct volumeHeader *volHeader)
97 printf("-- volume --\n");
98 printf("volume name: %s\n", volHeader->volumeName);
99 printf("volume ID %d\n", volHeader->volumeID);
100 /* printf("server %d\n", volHeader->server); */
101 printf("dumpSetName: %s\n", volHeader->dumpSetName);
102 printf("dumpID %d\n", volHeader->dumpID);
103 printf("level %d\n", volHeader->level);
104 printf("parentID %d\n", volHeader->parentID);
105 printf("endTime %d\n", volHeader->endTime);
106 /* printf("versionflags %d\n", volHeader->versionflags); */
107 t = volHeader->cloneDate;
108 printf("clonedate %s\n", ctime(&t));
112 * ask a question. returns true or false
126 printf("%s? (y/n) ", st);
128 response = getchar();
131 else if (response == 'n' || response == EOF)
133 printf("please answer y/n\n");
138 * Skips the volume data on the tape. The end of the volume data is
139 * detected by the presence of the volume trailer or by an EOF indication
140 * from butm. This algorithm should be replaced by one that always
141 * detects based on the volume trailer, returning the trailer to the
142 * caller. This is of course more painful.
144 * curTapePtr - tape info structure
145 * Tape must be positioned after volume header
148 * 3 - empty volume, requires special handling
150 * Tape positioned after data, but before file end marker block.
151 * In case of errors, positioned after the error causing block
153 #define BIGCHUNK 102400
156 scanVolData(afs_int32 taskId, struct butm_tapeInfo *curTapePtr, afs_int32 tapeVersion, struct volumeHeader *volumeHeader, struct volumeHeader *volumeTrailer, afs_uint32 *bytesRead)
158 afs_int32 headBytes, tailBytes;
161 int hasdata[2], curr, prev;
162 afs_uint32 chunkSize = 0;
165 afs_int32 rcode, tcode;
167 memset(volumeHeader, 0, sizeof(struct volumeHeader));
169 block = (char *)malloc(2 * BUTM_BLOCKSIZE);
171 return (TC_NOMEMORY);
172 buffer[0] = &block[sizeof(struct blockMark)];
173 buffer[1] = &block[BUTM_BLOCKSIZE + sizeof(struct blockMark)];
174 hasdata[0] = hasdata[1] = 0;
177 tcode = NextFile(curTapePtr); /* guarantees we are at a filemark */
181 /* Read the FileBegin FileMark */
182 code = butm_ReadFileBegin(curTapePtr);
185 * Tapes made with 3.0 have no software EOT markers. Therefore
186 * at this point, we will most likely get a read error, indicating
187 * the end of this dump
189 if ((tapeVersion == TAPE_VERSION_0)
190 || (tapeVersion == TAPE_VERSION_1)) {
192 * then a tape error is possible at this point, and it
193 * signals the end of the dump. Tapes that are continued
194 * have an EOT marker.
196 TapeLog(0, taskId, code, curTapePtr->error,
197 "Read error - end-of-dump inferred\n");
201 if (code != BUTM_EOD)
202 ErrorLog(0, taskId, code, curTapePtr->error,
203 "Can't read FileBegin on tape\n");
207 /* now read the volume header */
208 code = ReadVolHeader(taskId, curTapePtr, volumeHeader);
215 /* Check for abort in the middle of scanning data */
216 if (*bytesRead >= chunkSize) {
217 if (checkAbortByTaskId(taskId))
218 ERROR_EXIT(TC_ABORTEDBYREQUEST);
219 chunkSize += BIGCHUNK;
223 * Read volume date - If prematurely hit the HW EOF
224 * marker, check to see if data contains a volumetrailer.
227 butm_ReadFileData(curTapePtr, buffer[curr], BUTM_BLKSIZE,
231 if ((rcode == BUTM_EOF) || (rcode == BUTM_ENDVOLUME))
234 ErrorLog(0, taskId, rcode, curTapePtr->error,
235 "Can't read FileData on tape\n");
239 *bytesRead += nbytes;
241 if ((nbytes != BUTM_BLKSIZE)
243 (FindVolTrailer(buffer[curr], nbytes, &tailBytes, volumeTrailer)))
246 curr = ((curr == 0) ? 1 : 0); /* Switch buffers */
249 /* Now verify that there is a volume trailer and its valid and copy it */
250 prev = ((curr == 0) ? 1 : 0);
252 (buffer[prev], (hasdata[prev] ? BUTM_BLKSIZE : 0), &headBytes,
253 buffer[curr], nbytes, &tailBytes, volumeTrailer)) {
254 code = TC_MISSINGTRAILER;
255 ErrorLog(0, taskId, code, 0, "Missing volume trailer on tape\n");
257 /* subtract size of the volume trailer from data read */
258 *bytesRead -= sizeof(struct volumeHeader);
262 * If we didn't hit the EOF while reading data, read FileEnd marker
266 tcode = butm_ReadFileEnd(curTapePtr);
268 ErrorLog(0, taskId, tcode, curTapePtr->error,
269 "Can't read EOF on tape\n");
281 * generate the name of the next tape label expected
283 * ptr to static string
287 nextTapeLabel(char *prevTapeName)
292 static char buffer[BU_MAXTAPELEN];
296 /* extract information from previous tape label */
297 strcpy(buffer, prevTapeName);
298 prevdot = strrchr(buffer, '.');
303 seq = extractTapeSeq(prevTapeName);
305 sprintf(prevdot, "%-d", seq);
311 * Read all the information on a tape. If to add to the database, queue
312 * onto list so another thread adds the entries to the database.
314 * taskid - butc task number.
315 * tapeInfoPtr - Tape information.
316 * scanInfoPtr - Info to keep so we can add entries to the db.
319 * non-0 - error. Abort the scan.
320 * moreTapes set to 1 if this is not the last tape in the dump,
321 * 0 if the last tape,
322 * -1 don't know if last tape or not.
325 afs_int32 RcreateDump();
328 readDump(afs_uint32 taskId, struct butm_tapeInfo *tapeInfoPtr, struct tapeScanInfo *scanInfoPtr)
331 afs_int32 nbytes, flags, seq;
332 int newDump = 1, newTape = 1;
333 afs_int32 tapePosition;
334 afs_int32 code = 0, tcode;
336 struct volumeHeader volHeader, volTrailer;
337 struct budb_tapeEntry tapeEntry;
338 struct budb_volumeEntry volEntry;
341 PrintDumpLabel(&scanInfoPtr->dumpLabel);
343 while (moreTapes) { /* While there is a tape to read *//*t */
345 while (1) { /* Read each volume on the tape *//*w */
347 tapePosition = tapeInfoPtr->position; /* remember position */
350 * Skip the volume data
353 scanVolData(taskId, tapeInfoPtr,
354 scanInfoPtr->tapeLabel.structVersion, &volHeader,
355 &volTrailer, &nbytes);
359 if (tcode == TC_ABORTEDBYREQUEST) { /* Aborted */
363 if (tcode == BUTM_EOD) {
364 moreTapes = 0; /* the end of the dump */
368 /* Found a volume but it's incomplete. Skip over these */
369 if (volHeader.volumeID) {
370 TapeLog(0, taskId, tcode, 0,
371 "Warning: volume %s (%u) ignored. Incomplete\n",
372 volHeader.volumeName, volHeader.volumeID);
376 /* No volume was found. We may have hit the EOT or a
377 * bad-spot. Try to skip over this spot.
379 if (badscan < 2) { /* allow 2 errors, then fail */
380 TapeLog(0, taskId, tcode, 0,
381 "Warning: Error in scanning tape - will try skipping volume\n");
384 if (scanInfoPtr->tapeLabel.structVersion >= TAPE_VERSION_4) {
385 TapeLog(0, taskId, tcode, 0,
386 "Warning: Error in scanning tape - end-of-tape inferred\n");
387 moreTapes = 1; /* then assume next tape */
389 ErrorLog(0, taskId, tcode, 0, "Error in scanning tape\n");
390 /* will ask if there is a next tape */
395 PrintVolumeHeader(&volHeader);
397 /* If this is not the first volume fragment, make sure it follows
398 * the last volume fragment
401 if ((volEntry.dump != volHeader.dumpID)
402 || (volEntry.id != volHeader.volumeID)
403 || (volEntry.seq != volHeader.frag - 2)
404 || (strcmp(volEntry.name, volHeader.volumeName))) {
406 "Warning: volume %s (%u) ignored. Incomplete - no last fragment\n",
407 volEntry.name, volEntry.id);
409 if (scanInfoPtr->addDbFlag) {
410 tcode = flushSavedEntries(DUMP_FAILED);
418 /* If this is the first volume fragment, make sure says so */
419 if (scanInfoPtr->addDbFlag && !volEntry.dump
420 && (volHeader.frag != 1)) {
422 "Warning: volume %s (%u) ignored. Incomplete - no first fragment\n",
423 volHeader.volumeName, volHeader.volumeID);
426 /* Check that this volume belongs to the dump we are scanning */
427 else if (scanInfoPtr->dumpLabel.dumpid
428 && (volHeader.dumpID != scanInfoPtr->dumpLabel.dumpid)) {
430 "Warning: volume %s (%u) ignored. Expected DumpId %u, got %u\n",
431 volHeader.volumeName, volHeader.volumeID,
432 scanInfoPtr->dumpLabel.dumpid, volHeader.dumpID);
435 /* Passed tests, Now add to the database (if dbadd flag is set) */
436 else if (scanInfoPtr->addDbFlag) {
437 /* Have enough information to create a dump entry */
439 tcode = RcreateDump(scanInfoPtr, &volHeader);
441 ErrorLog(0, taskId, tcode, 0,
442 "Can't add dump %u to database\n",
449 /* Have enough information to create a tape entry */
451 seq = extractTapeSeq(scanInfoPtr->tapeLabel.AFSName);
453 ERROR_EXIT(TC_INTERNALERROR);
456 useTape(&tapeEntry, volHeader.dumpID,
457 TNAME(&scanInfoPtr->tapeLabel), seq,
458 scanInfoPtr->tapeLabel.useCount,
459 scanInfoPtr->dumpLabel.creationTime,
460 scanInfoPtr->dumpLabel.expirationDate,
463 char gotName[BU_MAXTAPELEN + 32];
465 LABELNAME(gotName, &scanInfoPtr->tapeLabel);
466 ErrorLog(0, taskId, tcode, 0,
467 "Can't add tape %s for dump %u to database\n",
468 gotName, volHeader.dumpID);
474 /* Create the volume entry */
475 flags = ((volHeader.frag == 1) ? BUDB_VOL_FIRSTFRAG : 0);
476 if (!volTrailer.contd)
477 flags |= BUDB_VOL_LASTFRAG;
479 addVolume(&volEntry, volHeader.dumpID,
480 TNAME(&scanInfoPtr->tapeLabel),
481 volHeader.volumeName, volHeader.volumeID,
482 volHeader.cloneDate, tapePosition, nbytes,
483 (volHeader.frag - 1), flags);
485 ErrorLog(0, taskId, tcode, 0,
486 "Can't add volume %s (%u) for dump %u to database\n",
487 volHeader.volumeName, volHeader.volumeID,
493 if (volTrailer.contd) {
494 /* No need to read the EOD marker, we know there is a next tape */
498 if (scanInfoPtr->addDbFlag) {
499 tcode = flushSavedEntries(DUMP_SUCCESS);
508 if (scanInfoPtr->addDbFlag) {
510 finishTape(&tapeEntry,
511 (tapeInfoPtr->kBytes +
512 (tapeInfoPtr->nBytes ? 1 : 0)));
514 char gotName[BU_MAXTAPELEN + 32];
516 LABELNAME(gotName, &scanInfoPtr->tapeLabel);
517 ErrorLog(0, taskId, tcode, 0,
518 "Can't mark tape %s 'completed' for dump %u in database\n",
519 gotName, tapeEntry.dump);
525 /* Ask if there is another tape if we can't figure it out */
527 moreTapes = (queryoperator ? Ask("Are there more tapes") : 1);
529 /* Get the next tape label */
534 unmountTape(taskId, tapeInfoPtr);
536 tapeName = nextTapeLabel(scanInfoPtr->tapeLabel.AFSName);
537 dumpid = scanInfoPtr->tapeLabel.dumpid;
539 getScanTape(taskId, tapeInfoPtr, tapeName, dumpid, 1,
540 &scanInfoPtr->tapeLabel);
548 if (scanInfoPtr->addDbFlag) {
549 tcode = finishDump(&scanInfoPtr->dumpEntry);
551 ErrorLog(0, taskId, tcode, 0,
552 "Can't mark dump %u 'completed' in database\n",
553 scanInfoPtr->dumpEntry.id);
556 tcode = flushSavedEntries(DUMP_SUCCESS);
566 /* Will read a dump, then see if there is a dump following it and
567 * try to read that dump too.
568 * The first tape label is the first dumpLabel.
571 readDumps(afs_uint32 taskId, struct butm_tapeInfo *tapeInfoPtr, struct tapeScanInfo *scanInfoPtr)
575 memcpy(&scanInfoPtr->dumpLabel, &scanInfoPtr->tapeLabel,
576 sizeof(struct butm_tapeLabel));
579 code = readDump(taskId, tapeInfoPtr, scanInfoPtr);
583 if (scanInfoPtr->tapeLabel.structVersion < TAPE_VERSION_4)
586 /* Remember the initial dump and see if appended dump exists */
588 if (!scanInfoPtr->initialDumpId)
589 scanInfoPtr->initialDumpId = scanInfoPtr->dumpEntry.id;
591 c = butm_ReadLabel(tapeInfoPtr, &scanInfoPtr->dumpLabel, 0); /* no rewind */
592 tapepos = tapeInfoPtr->position - 1;
602 getScanTape(afs_int32 taskId, struct butm_tapeInfo *tapeInfoPtr, char *tname, afs_int32 tapeId, int prompt, struct butm_tapeLabel *tapeLabelPtr)
607 char tapename[BU_MAXTAPELEN + 32];
608 char gotname[BU_MAXTAPELEN + 32];
611 /* prompt for a tape */
614 PromptForTape(SCANOPCODE, tname, tapeId, taskId, tapecount);
621 code = butm_Mount(tapeInfoPtr, ""); /* open the tape device */
623 TapeLog(0, taskId, code, tapeInfoPtr->error, "Can't open tape\n");
627 /* read the label on the tape */
628 code = butm_ReadLabel(tapeInfoPtr, tapeLabelPtr, 1); /* rewind tape */
630 ErrorLog(0, taskId, code, tapeInfoPtr->error,
631 "Can't read tape label\n");
634 tapepos = tapeInfoPtr->position - 1;
636 /* Now check that the tape is good */
637 TAPENAME(tapename, tname, tapeId);
638 TAPENAME(gotname, tapeLabelPtr->AFSName, tapeLabelPtr->dumpid);
640 curseq = extractTapeSeq(tapeLabelPtr->AFSName);
642 /* Label can't be null or a bad name */
643 if (!strcmp(tapeLabelPtr->AFSName, "") || (curseq <= 0)) {
644 TLog(taskId, "Expected tape with dump, label seen %s\n", gotname);
648 /* Label can't be a database tape */
649 if (databaseTape(tapeLabelPtr->AFSName)) {
651 "Expected tape with dump. Can't scan database tape %s\n",
656 /* If no name, accept any tape */
657 if (strcmp(tname, "") == 0) {
658 break; /* Start scan on any tape */
661 break; /* The first tape */
663 TLog(taskId, "Expected first tape of dump, label seen %s\n",
670 if (strcmp(tname, tapeLabelPtr->AFSName)
671 || ((tapeLabelPtr->structVersion >= TAPE_VERSION_3)
672 && (tapeLabelPtr->dumpid != tapeId))) {
673 TLog(taskId, "Tape label expected %s, label seen %s\n", tapename,
678 /* We have the correct tape */
682 unmountTape(taskId, tapeInfoPtr);
690 * This set of code fragments read a tape, and add the information to
691 * the database. Builds a literal structure.
696 ScanDumps(struct scanTapeIf *ptr)
698 struct butm_tapeInfo curTapeInfo;
699 struct tapeScanInfo tapeScanInfo;
703 taskId = ptr->taskId;
704 setStatus(taskId, DRIVE_WAIT);
705 EnterDeviceQueue(deviceLatch);
706 clearStatus(taskId, DRIVE_WAIT);
710 TLog(taskId, "ScanTape and add to the database\n");
712 TLog(taskId, "Scantape\n");
714 memset(&tapeScanInfo, 0, sizeof(tapeScanInfo));
715 tapeScanInfo.addDbFlag = ptr->addDbFlag;
717 memset(&curTapeInfo, 0, sizeof(curTapeInfo));
718 curTapeInfo.structVersion = BUTM_MAJORVERSION;
719 code = butm_file_Instantiate(&curTapeInfo, &globalTapeConfig);
721 ErrorLog(0, taskId, code, curTapeInfo.error,
722 "Can't initialize tape module\n");
727 getScanTape(taskId, &curTapeInfo, "", 0, autoQuery,
728 &tapeScanInfo.tapeLabel);
732 code = readDumps(taskId, &curTapeInfo, &tapeScanInfo);
737 unmountTape(taskId, &curTapeInfo);
740 if (code == TC_ABORTEDBYREQUEST) {
741 ErrorLog(0, taskId, 0, 0, "Scantape: Aborted by request\n");
742 clearStatus(taskId, ABORT_REQUEST);
743 setStatus(taskId, ABORT_DONE);
745 ErrorLog(0, taskId, code, 0, "Scantape: Finished with errors\n");
746 setStatus(taskId, TASK_ERROR);
748 TLog(taskId, "Scantape: Finished\n");
752 setStatus(taskId, TASK_DONE);
753 LeaveDeviceQueue(deviceLatch);
764 validatePath(struct butm_tapeLabel *labelptr, char *pathptr)
767 char tapeName[BU_MAXTAPELEN];
770 if (strlen(pathptr) > BU_MAX_DUMP_PATH - 1) {
771 fprintf(stderr, "Invalid pathname - too long\n");
778 strcpy(tapeName, labelptr->AFSName);
780 tp = strrchr(tapeName, '.');
785 up = strrchr(pathptr, '/');
787 fprintf(stderr, "Invalid path name, missing /\n");
792 if (strcmp(up, tp) != 0) {
793 fprintf(stderr, "Invalid path name\n");
795 "Mismatch between tape dump name '%s' and users dump name '%s'\n",
803 * return a pointer to a (static) volume set name string.
805 * ptr - ptr to a dump name
807 * 0 - error. Can't extract volumeset name.
808 * ptr - to static volumeset string.
812 volumesetNamePtr(char *ptr)
814 static char vsname[BU_MAXUNAMELEN];
818 dotPtr = strchr(ptr, '.');
822 dotIndex = dotPtr - ptr;
823 if ((dotIndex + 1) > sizeof(vsname))
824 return (0); /* name too long */
826 strncpy(&vsname[0], ptr, dotIndex);
827 vsname[dotIndex] = 0; /* ensure null terminated */
833 extractDumpName(char *ptr)
835 static char dname[BU_MAXTAPELEN];
839 dotPtr = strrchr(ptr, '.');
843 dotIndex = dotPtr - ptr;
844 if ((dotIndex + 1) > sizeof(dname))
845 return (0); /* name too long */
847 strncpy(&dname[0], ptr, dotIndex);
848 dname[dotIndex] = 0; /* ensure null terminated */
854 * The routine assumes that tape names have an embedded sequence number
855 * as the trialing component. It is suggested that any tape naming
856 * changes retain the trailing seq. number
858 * tapename - ptr to tape name
860 * 0 or positive - sequence number
861 * -1 - error, couldn't extract sequence number
865 extractTapeSeq(char *tapename)
869 sptr = strrchr(tapename, '.');
877 * returns true or false depending on whether the tape is
878 * a database tape or not.
881 databaseTape(char *tapeName)
886 sptr = strrchr(tapeName, '.');
890 c = (int)((char *) sptr - (char *) tapeName);
891 if (strncmp(tapeName, DUMP_TAPE_NAME, c) == 0)
898 RcreateDump(struct tapeScanInfo *tapeScanInfoPtr, struct volumeHeader *volHeaderPtr)
901 struct butm_tapeLabel *dumpLabelPtr = &tapeScanInfoPtr->dumpLabel;
902 struct budb_dumpEntry *dumpEntryPtr = &tapeScanInfoPtr->dumpEntry;
904 /* construct dump entry */
905 memset(dumpEntryPtr, 0, sizeof(struct budb_dumpEntry));
906 dumpEntryPtr->id = volHeaderPtr->dumpID;
907 dumpEntryPtr->initialDumpID = tapeScanInfoPtr->initialDumpId;
908 dumpEntryPtr->parent = volHeaderPtr->parentID;
909 dumpEntryPtr->level = volHeaderPtr->level;
910 dumpEntryPtr->created = volHeaderPtr->dumpID; /* time dump was created */
911 dumpEntryPtr->flags = 0;
912 dumpEntryPtr->incTime = 0;
913 dumpEntryPtr->nVolumes = 0;
914 strcpy(dumpEntryPtr->volumeSetName,
915 volumesetNamePtr(volHeaderPtr->dumpSetName));
916 strcpy(dumpEntryPtr->dumpPath, dumpLabelPtr->dumpPath);
917 strcpy(dumpEntryPtr->name, volHeaderPtr->dumpSetName);
918 default_tapeset(&dumpEntryPtr->tapes, volHeaderPtr->dumpSetName);
919 dumpEntryPtr->tapes.b = extractTapeSeq(dumpLabelPtr->AFSName);
920 copy_ktcPrincipal_to_budbPrincipal(&dumpLabelPtr->creator,
921 &dumpEntryPtr->dumper);
923 code = bcdb_CreateDump(dumpEntryPtr);