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;
56 * print out the tape (dump) label.
59 PrintDumpLabel(struct butm_tapeLabel *labelptr)
61 char tapeName[BU_MAXTAPELEN + 32];
64 printf("Dump label\n");
65 printf("----------\n");
66 TAPENAME(tapeName, labelptr->pName, labelptr->dumpid);
67 printf("permanent tape name = %s\n", tapeName);
68 TAPENAME(tapeName, labelptr->AFSName, labelptr->dumpid);
69 printf("AFS tape name = %s\n", tapeName);
70 t = labelptr->creationTime;
71 printf("creationTime = %s", ctime(&t));
72 if (labelptr->expirationDate) {
73 t = labelptr->expirationDate;
74 printf("expirationDate = %s", cTIME(&t));
76 printf("cell = %s\n", labelptr->cell);
77 printf("size = %u Kbytes\n", labelptr->size);
78 printf("dump path = %s\n", labelptr->dumpPath);
80 if (labelptr->structVersion >= TAPE_VERSION_3) {
81 printf("dump id = %u\n", labelptr->dumpid);
82 printf("useCount = %d\n", labelptr->useCount);
84 printf("-- End of dump label --\n\n");
88 * print the contents of a volume header.
91 PrintVolumeHeader(struct volumeHeader *volHeader)
95 printf("-- volume --\n");
96 printf("volume name: %s\n", volHeader->volumeName);
97 printf("volume ID %d\n", volHeader->volumeID);
98 /* printf("server %d\n", volHeader->server); */
99 printf("dumpSetName: %s\n", volHeader->dumpSetName);
100 printf("dumpID %d\n", volHeader->dumpID);
101 printf("level %d\n", volHeader->level);
102 printf("parentID %d\n", volHeader->parentID);
103 printf("endTime %d\n", volHeader->endTime);
104 /* printf("versionflags %d\n", volHeader->versionflags); */
105 t = volHeader->cloneDate;
106 printf("clonedate %s\n", ctime(&t));
110 * ask a question. returns true or false
124 printf("%s? (y/n) ", st);
126 response = getchar();
129 else if (response == 'n' || response == EOF)
131 printf("please answer y/n\n");
136 * Skips the volume data on the tape. The end of the volume data is
137 * detected by the presence of the volume trailer or by an EOF indication
138 * from butm. This algorithm should be replaced by one that always
139 * detects based on the volume trailer, returning the trailer to the
140 * caller. This is of course more painful.
142 * curTapePtr - tape info structure
143 * Tape must be positioned after volume header
146 * 3 - empty volume, requires special handling
148 * Tape positioned after data, but before file end marker block.
149 * In case of errors, positioned after the error causing block
151 #define BIGCHUNK 102400
154 scanVolData(afs_int32 taskId, struct butm_tapeInfo *curTapePtr, afs_int32 tapeVersion, struct volumeHeader *volumeHeader, struct volumeHeader *volumeTrailer, afs_uint32 *bytesRead)
156 afs_int32 headBytes, tailBytes;
159 int hasdata[2], curr, prev;
160 afs_uint32 chunkSize = 0;
163 afs_int32 rcode, tcode;
165 memset(volumeHeader, 0, sizeof(struct volumeHeader));
167 block = (char *)malloc(2 * BUTM_BLOCKSIZE);
169 return (TC_NOMEMORY);
170 buffer[0] = &block[sizeof(struct blockMark)];
171 buffer[1] = &block[BUTM_BLOCKSIZE + sizeof(struct blockMark)];
172 hasdata[0] = hasdata[1] = 0;
175 tcode = NextFile(curTapePtr); /* guarantees we are at a filemark */
179 /* Read the FileBegin FileMark */
180 code = butm_ReadFileBegin(curTapePtr);
183 * Tapes made with 3.0 have no software EOT markers. Therefore
184 * at this point, we will most likely get a read error, indicating
185 * the end of this dump
187 if ((tapeVersion == TAPE_VERSION_0)
188 || (tapeVersion == TAPE_VERSION_1)) {
190 * then a tape error is possible at this point, and it
191 * signals the end of the dump. Tapes that are continued
192 * have an EOT marker.
194 TapeLog(0, taskId, code, curTapePtr->error,
195 "Read error - end-of-dump inferred\n");
199 if (code != BUTM_EOD)
200 ErrorLog(0, taskId, code, curTapePtr->error,
201 "Can't read FileBegin on tape\n");
205 /* now read the volume header */
206 code = ReadVolHeader(taskId, curTapePtr, volumeHeader);
213 /* Check for abort in the middle of scanning data */
214 if (*bytesRead >= chunkSize) {
215 if (checkAbortByTaskId(taskId))
216 ERROR_EXIT(TC_ABORTEDBYREQUEST);
217 chunkSize += BIGCHUNK;
221 * Read volume date - If prematurely hit the HW EOF
222 * marker, check to see if data contains a volumetrailer.
225 butm_ReadFileData(curTapePtr, buffer[curr], BUTM_BLKSIZE,
229 if ((rcode == BUTM_EOF) || (rcode == BUTM_ENDVOLUME))
232 ErrorLog(0, taskId, rcode, curTapePtr->error,
233 "Can't read FileData on tape\n");
237 *bytesRead += nbytes;
239 if ((nbytes != BUTM_BLKSIZE)
241 (FindVolTrailer(buffer[curr], nbytes, &tailBytes, volumeTrailer)))
244 curr = ((curr == 0) ? 1 : 0); /* Switch buffers */
247 /* Now verify that there is a volume trailer and its valid and copy it */
248 prev = ((curr == 0) ? 1 : 0);
250 (buffer[prev], (hasdata[prev] ? BUTM_BLKSIZE : 0), &headBytes,
251 buffer[curr], nbytes, &tailBytes, volumeTrailer)) {
252 code = TC_MISSINGTRAILER;
253 ErrorLog(0, taskId, code, 0, "Missing volume trailer on tape\n");
255 /* subtract size of the volume trailer from data read */
256 *bytesRead -= sizeof(struct volumeHeader);
260 * If we didn't hit the EOF while reading data, read FileEnd marker
264 tcode = butm_ReadFileEnd(curTapePtr);
266 ErrorLog(0, taskId, tcode, curTapePtr->error,
267 "Can't read EOF on tape\n");
279 * generate the name of the next tape label expected
281 * ptr to static string
285 nextTapeLabel(char *prevTapeName)
290 static char buffer[BU_MAXTAPELEN];
294 /* extract information from previous tape label */
295 strcpy(buffer, prevTapeName);
296 prevdot = strrchr(buffer, '.');
301 seq = extractTapeSeq(prevTapeName);
303 sprintf(prevdot, "%-d", seq);
309 * Read all the information on a tape. If to add to the database, queue
310 * onto list so another thread adds the entries to the database.
312 * taskid - butc task number.
313 * tapeInfoPtr - Tape information.
314 * scanInfoPtr - Info to keep so we can add entries to the db.
317 * non-0 - error. Abort the scan.
318 * moreTapes set to 1 if this is not the last tape in the dump,
319 * 0 if the last tape,
320 * -1 don't know if last tape or not.
323 afs_int32 RcreateDump();
326 readDump(afs_uint32 taskId, struct butm_tapeInfo *tapeInfoPtr, struct tapeScanInfo *scanInfoPtr)
329 afs_int32 nbytes, flags, seq;
330 int newDump = 1, newTape = 1;
331 afs_int32 tapePosition;
332 afs_int32 code = 0, tcode;
334 struct volumeHeader volHeader, volTrailer;
335 struct budb_tapeEntry tapeEntry;
336 struct budb_volumeEntry volEntry;
339 PrintDumpLabel(&scanInfoPtr->dumpLabel);
341 while (moreTapes) { /* While there is a tape to read *//*t */
343 while (1) { /* Read each volume on the tape *//*w */
345 tapePosition = tapeInfoPtr->position; /* remember position */
348 * Skip the volume data
351 scanVolData(taskId, tapeInfoPtr,
352 scanInfoPtr->tapeLabel.structVersion, &volHeader,
353 &volTrailer, &nbytes);
357 if (tcode == TC_ABORTEDBYREQUEST) { /* Aborted */
361 if (tcode == BUTM_EOD) {
362 moreTapes = 0; /* the end of the dump */
366 /* Found a volume but it's incomplete. Skip over these */
367 if (volHeader.volumeID) {
368 TapeLog(0, taskId, tcode, 0,
369 "Warning: volume %s (%u) ignored. Incomplete\n",
370 volHeader.volumeName, volHeader.volumeID);
374 /* No volume was found. We may have hit the EOT or a
375 * bad-spot. Try to skip over this spot.
377 if (badscan < 2) { /* allow 2 errors, then fail */
378 TapeLog(0, taskId, tcode, 0,
379 "Warning: Error in scanning tape - will try skipping volume\n");
382 if (scanInfoPtr->tapeLabel.structVersion >= TAPE_VERSION_4) {
383 TapeLog(0, taskId, tcode, 0,
384 "Warning: Error in scanning tape - end-of-tape inferred\n");
385 moreTapes = 1; /* then assume next tape */
387 ErrorLog(0, taskId, tcode, 0, "Error in scanning tape\n");
388 /* will ask if there is a next tape */
393 PrintVolumeHeader(&volHeader);
395 /* If this is not the first volume fragment, make sure it follows
396 * the last volume fragment
399 if ((volEntry.dump != volHeader.dumpID)
400 || (volEntry.id != volHeader.volumeID)
401 || (volEntry.seq != volHeader.frag - 2)
402 || (strcmp(volEntry.name, volHeader.volumeName))) {
404 "Warning: volume %s (%u) ignored. Incomplete - no last fragment\n",
405 volEntry.name, volEntry.id);
407 if (scanInfoPtr->addDbFlag) {
408 tcode = flushSavedEntries(DUMP_FAILED);
416 /* If this is the first volume fragment, make sure says so */
417 if (scanInfoPtr->addDbFlag && !volEntry.dump
418 && (volHeader.frag != 1)) {
420 "Warning: volume %s (%u) ignored. Incomplete - no first fragment\n",
421 volHeader.volumeName, volHeader.volumeID);
424 /* Check that this volume belongs to the dump we are scanning */
425 else if (scanInfoPtr->dumpLabel.dumpid
426 && (volHeader.dumpID != scanInfoPtr->dumpLabel.dumpid)) {
428 "Warning: volume %s (%u) ignored. Expected DumpId %u, got %u\n",
429 volHeader.volumeName, volHeader.volumeID,
430 scanInfoPtr->dumpLabel.dumpid, volHeader.dumpID);
433 /* Passed tests, Now add to the database (if dbadd flag is set) */
434 else if (scanInfoPtr->addDbFlag) {
435 /* Have enough information to create a dump entry */
437 tcode = RcreateDump(scanInfoPtr, &volHeader);
439 ErrorLog(0, taskId, tcode, 0,
440 "Can't add dump %u to database\n",
447 /* Have enough information to create a tape entry */
449 seq = extractTapeSeq(scanInfoPtr->tapeLabel.AFSName);
451 ERROR_EXIT(TC_INTERNALERROR);
454 useTape(&tapeEntry, volHeader.dumpID,
455 TNAME(&scanInfoPtr->tapeLabel), seq,
456 scanInfoPtr->tapeLabel.useCount,
457 scanInfoPtr->dumpLabel.creationTime,
458 scanInfoPtr->dumpLabel.expirationDate,
461 char gotName[BU_MAXTAPELEN + 32];
463 LABELNAME(gotName, &scanInfoPtr->tapeLabel);
464 ErrorLog(0, taskId, tcode, 0,
465 "Can't add tape %s for dump %u to database\n",
466 gotName, volHeader.dumpID);
472 /* Create the volume entry */
473 flags = ((volHeader.frag == 1) ? BUDB_VOL_FIRSTFRAG : 0);
474 if (!volTrailer.contd)
475 flags |= BUDB_VOL_LASTFRAG;
477 addVolume(&volEntry, volHeader.dumpID,
478 TNAME(&scanInfoPtr->tapeLabel),
479 volHeader.volumeName, volHeader.volumeID,
480 volHeader.cloneDate, tapePosition, nbytes,
481 (volHeader.frag - 1), flags);
483 ErrorLog(0, taskId, tcode, 0,
484 "Can't add volume %s (%u) for dump %u to database\n",
485 volHeader.volumeName, volHeader.volumeID,
491 if (volTrailer.contd) {
492 /* No need to read the EOD marker, we know there is a next tape */
496 if (scanInfoPtr->addDbFlag) {
497 tcode = flushSavedEntries(DUMP_SUCCESS);
506 if (scanInfoPtr->addDbFlag) {
508 finishTape(&tapeEntry,
509 (tapeInfoPtr->kBytes +
510 (tapeInfoPtr->nBytes ? 1 : 0)));
512 char gotName[BU_MAXTAPELEN + 32];
514 LABELNAME(gotName, &scanInfoPtr->tapeLabel);
515 ErrorLog(0, taskId, tcode, 0,
516 "Can't mark tape %s 'completed' for dump %u in database\n",
517 gotName, tapeEntry.dump);
523 /* Ask if there is another tape if we can't figure it out */
525 moreTapes = (queryoperator ? Ask("Are there more tapes") : 1);
527 /* Get the next tape label */
532 unmountTape(taskId, tapeInfoPtr);
534 tapeName = nextTapeLabel(scanInfoPtr->tapeLabel.AFSName);
535 dumpid = scanInfoPtr->tapeLabel.dumpid;
537 getScanTape(taskId, tapeInfoPtr, tapeName, dumpid, 1,
538 &scanInfoPtr->tapeLabel);
546 if (scanInfoPtr->addDbFlag) {
547 tcode = finishDump(&scanInfoPtr->dumpEntry);
549 ErrorLog(0, taskId, tcode, 0,
550 "Can't mark dump %u 'completed' in database\n",
551 scanInfoPtr->dumpEntry.id);
554 tcode = flushSavedEntries(DUMP_SUCCESS);
564 /* Will read a dump, then see if there is a dump following it and
565 * try to read that dump too.
566 * The first tape label is the first dumpLabel.
569 readDumps(afs_uint32 taskId, struct butm_tapeInfo *tapeInfoPtr, struct tapeScanInfo *scanInfoPtr)
573 memcpy(&scanInfoPtr->dumpLabel, &scanInfoPtr->tapeLabel,
574 sizeof(struct butm_tapeLabel));
577 code = readDump(taskId, tapeInfoPtr, scanInfoPtr);
581 if (scanInfoPtr->tapeLabel.structVersion < TAPE_VERSION_4)
584 /* Remember the initial dump and see if appended dump exists */
586 if (!scanInfoPtr->initialDumpId)
587 scanInfoPtr->initialDumpId = scanInfoPtr->dumpEntry.id;
589 c = butm_ReadLabel(tapeInfoPtr, &scanInfoPtr->dumpLabel, 0); /* no rewind */
590 tapepos = tapeInfoPtr->position - 1;
600 getScanTape(afs_int32 taskId, struct butm_tapeInfo *tapeInfoPtr, char *tname, afs_int32 tapeId, int prompt, struct butm_tapeLabel *tapeLabelPtr)
605 char tapename[BU_MAXTAPELEN + 32];
606 char gotname[BU_MAXTAPELEN + 32];
609 /* prompt for a tape */
612 PromptForTape(SCANOPCODE, tname, tapeId, taskId, tapecount);
619 code = butm_Mount(tapeInfoPtr, ""); /* open the tape device */
621 TapeLog(0, taskId, code, tapeInfoPtr->error, "Can't open tape\n");
625 /* read the label on the tape */
626 code = butm_ReadLabel(tapeInfoPtr, tapeLabelPtr, 1); /* rewind tape */
628 ErrorLog(0, taskId, code, tapeInfoPtr->error,
629 "Can't read tape label\n");
632 tapepos = tapeInfoPtr->position - 1;
634 /* Now check that the tape is good */
635 TAPENAME(tapename, tname, tapeId);
636 TAPENAME(gotname, tapeLabelPtr->AFSName, tapeLabelPtr->dumpid);
638 curseq = extractTapeSeq(tapeLabelPtr->AFSName);
640 /* Label can't be null or a bad name */
641 if (!strcmp(tapeLabelPtr->AFSName, "") || (curseq <= 0)) {
642 TLog(taskId, "Expected tape with dump, label seen %s\n", gotname);
646 /* Label can't be a database tape */
647 if (databaseTape(tapeLabelPtr->AFSName)) {
649 "Expected tape with dump. Can't scan database tape %s\n",
654 /* If no name, accept any tape */
655 if (strcmp(tname, "") == 0) {
656 break; /* Start scan on any tape */
659 break; /* The first tape */
661 TLog(taskId, "Expected first tape of dump, label seen %s\n",
668 if (strcmp(tname, tapeLabelPtr->AFSName)
669 || ((tapeLabelPtr->structVersion >= TAPE_VERSION_3)
670 && (tapeLabelPtr->dumpid != tapeId))) {
671 TLog(taskId, "Tape label expected %s, label seen %s\n", tapename,
676 /* We have the correct tape */
680 unmountTape(taskId, tapeInfoPtr);
688 * This set of code fragments read a tape, and add the information to
689 * the database. Builds a literal structure.
694 ScanDumps(struct scanTapeIf *ptr)
696 struct butm_tapeInfo curTapeInfo;
697 struct tapeScanInfo tapeScanInfo;
701 taskId = ptr->taskId;
702 setStatus(taskId, DRIVE_WAIT);
703 EnterDeviceQueue(deviceLatch);
704 clearStatus(taskId, DRIVE_WAIT);
708 TLog(taskId, "ScanTape and add to the database\n");
710 TLog(taskId, "Scantape\n");
712 memset(&tapeScanInfo, 0, sizeof(tapeScanInfo));
713 tapeScanInfo.addDbFlag = ptr->addDbFlag;
715 memset(&curTapeInfo, 0, sizeof(curTapeInfo));
716 curTapeInfo.structVersion = BUTM_MAJORVERSION;
717 code = butm_file_Instantiate(&curTapeInfo, &globalTapeConfig);
719 ErrorLog(0, taskId, code, curTapeInfo.error,
720 "Can't initialize tape module\n");
725 getScanTape(taskId, &curTapeInfo, "", 0, autoQuery,
726 &tapeScanInfo.tapeLabel);
730 code = readDumps(taskId, &curTapeInfo, &tapeScanInfo);
735 unmountTape(taskId, &curTapeInfo);
738 if (code == TC_ABORTEDBYREQUEST) {
739 ErrorLog(0, taskId, 0, 0, "Scantape: Aborted by request\n");
740 clearStatus(taskId, ABORT_REQUEST);
741 setStatus(taskId, ABORT_DONE);
743 ErrorLog(0, taskId, code, 0, "Scantape: Finished with errors\n");
744 setStatus(taskId, TASK_ERROR);
746 TLog(taskId, "Scantape: Finished\n");
750 setStatus(taskId, TASK_DONE);
751 LeaveDeviceQueue(deviceLatch);
762 validatePath(struct butm_tapeLabel *labelptr, char *pathptr)
765 char tapeName[BU_MAXTAPELEN];
768 if (strlen(pathptr) > BU_MAX_DUMP_PATH - 1) {
769 fprintf(stderr, "Invalid pathname - too long\n");
776 strcpy(tapeName, labelptr->AFSName);
778 tp = strrchr(tapeName, '.');
783 up = strrchr(pathptr, '/');
785 fprintf(stderr, "Invalid path name, missing /\n");
790 if (strcmp(up, tp) != 0) {
791 fprintf(stderr, "Invalid path name\n");
793 "Mismatch between tape dump name '%s' and users dump name '%s'\n",
801 * return a pointer to a (static) volume set name string.
803 * ptr - ptr to a dump name
805 * 0 - error. Can't extract volumeset name.
806 * ptr - to static volumeset string.
810 volumesetNamePtr(char *ptr)
812 static char vsname[BU_MAXUNAMELEN];
816 dotPtr = strchr(ptr, '.');
820 dotIndex = dotPtr - ptr;
821 if ((dotIndex + 1) > sizeof(vsname))
822 return (0); /* name too long */
824 strncpy(&vsname[0], ptr, dotIndex);
825 vsname[dotIndex] = 0; /* ensure null terminated */
831 extractDumpName(char *ptr)
833 static char dname[BU_MAXTAPELEN];
837 dotPtr = strrchr(ptr, '.');
841 dotIndex = dotPtr - ptr;
842 if ((dotIndex + 1) > sizeof(dname))
843 return (0); /* name too long */
845 strncpy(&dname[0], ptr, dotIndex);
846 dname[dotIndex] = 0; /* ensure null terminated */
852 * The routine assumes that tape names have an embedded sequence number
853 * as the trialing component. It is suggested that any tape naming
854 * changes retain the trailing seq. number
856 * tapename - ptr to tape name
858 * 0 or positive - sequence number
859 * -1 - error, couldn't extract sequence number
863 extractTapeSeq(char *tapename)
867 sptr = strrchr(tapename, '.');
875 * returns true or false depending on whether the tape is
876 * a database tape or not.
879 databaseTape(char *tapeName)
884 sptr = strrchr(tapeName, '.');
888 c = (int)((char *) sptr - (char *) tapeName);
889 if (strncmp(tapeName, DUMP_TAPE_NAME, c) == 0)
896 RcreateDump(struct tapeScanInfo *tapeScanInfoPtr, struct volumeHeader *volHeaderPtr)
899 struct butm_tapeLabel *dumpLabelPtr = &tapeScanInfoPtr->dumpLabel;
900 struct budb_dumpEntry *dumpEntryPtr = &tapeScanInfoPtr->dumpEntry;
902 /* construct dump entry */
903 memset(dumpEntryPtr, 0, sizeof(struct budb_dumpEntry));
904 dumpEntryPtr->id = volHeaderPtr->dumpID;
905 dumpEntryPtr->initialDumpID = tapeScanInfoPtr->initialDumpId;
906 dumpEntryPtr->parent = volHeaderPtr->parentID;
907 dumpEntryPtr->level = volHeaderPtr->level;
908 dumpEntryPtr->created = volHeaderPtr->dumpID; /* time dump was created */
909 dumpEntryPtr->flags = 0;
910 dumpEntryPtr->incTime = 0;
911 dumpEntryPtr->nVolumes = 0;
912 strcpy(dumpEntryPtr->volumeSetName,
913 volumesetNamePtr(volHeaderPtr->dumpSetName));
914 strcpy(dumpEntryPtr->dumpPath, dumpLabelPtr->dumpPath);
915 strcpy(dumpEntryPtr->name, volHeaderPtr->dumpSetName);
916 default_tapeset(&dumpEntryPtr->tapes, volHeaderPtr->dumpSetName);
917 dumpEntryPtr->tapes.b = extractTapeSeq(dumpLabelPtr->AFSName);
918 copy_ktcPrincipal_to_budbPrincipal(&dumpLabelPtr->creator,
919 &dumpEntryPtr->dumper);
921 code = bcdb_CreateDump(dumpEntryPtr);