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>
20 #include <netinet/in.h>
24 #include <sys/types.h>
30 #include <afs/tcdata.h>
31 #include <afs/bubasics.h>
33 #include "error_macros.h"
34 #include "butc_internal.h"
36 #define BELLCHAR 7 /* ascii for bell */
38 /* GLOBAL CONFIGURATION PARAMETERS */
40 extern int queryoperator;
42 /* Handle for the information read from all the tapes of a dump */
43 afs_int32 tapepos; /* when read a label, remember its position */
45 struct butm_tapeLabel tapeLabel, dumpLabel;
46 struct budb_dumpEntry dumpEntry;
47 afs_int32 initialDumpId;
51 extern struct tapeConfig globalTapeConfig;
52 extern struct deviceSyncNode *deviceLatch;
57 * print out the tape (dump) label.
60 PrintDumpLabel(struct butm_tapeLabel *labelptr)
62 char tapeName[BU_MAXTAPELEN + 32];
65 printf("Dump label\n");
66 printf("----------\n");
67 TAPENAME(tapeName, labelptr->pName, labelptr->dumpid);
68 printf("permanent tape name = %s\n", tapeName);
69 TAPENAME(tapeName, labelptr->AFSName, labelptr->dumpid);
70 printf("AFS tape name = %s\n", tapeName);
71 t = labelptr->creationTime;
72 printf("creationTime = %s", ctime(&t));
73 if (labelptr->expirationDate) {
74 t = labelptr->expirationDate;
75 printf("expirationDate = %s", cTIME(&t));
77 printf("cell = %s\n", labelptr->cell);
78 printf("size = %u Kbytes\n", labelptr->size);
79 printf("dump path = %s\n", labelptr->dumpPath);
81 if (labelptr->structVersion >= TAPE_VERSION_3) {
82 printf("dump id = %u\n", labelptr->dumpid);
83 printf("useCount = %d\n", labelptr->useCount);
85 printf("-- End of dump label --\n\n");
89 * print the contents of a volume header.
92 PrintVolumeHeader(struct volumeHeader *volHeader)
96 printf("-- volume --\n");
97 printf("volume name: %s\n", volHeader->volumeName);
98 printf("volume ID %d\n", volHeader->volumeID);
99 /* printf("server %d\n", volHeader->server); */
100 printf("dumpSetName: %s\n", volHeader->dumpSetName);
101 printf("dumpID %d\n", volHeader->dumpID);
102 printf("level %d\n", volHeader->level);
103 printf("parentID %d\n", volHeader->parentID);
104 printf("endTime %d\n", volHeader->endTime);
105 /* printf("versionflags %d\n", volHeader->versionflags); */
106 t = volHeader->cloneDate;
107 printf("clonedate %s\n", ctime(&t));
111 * ask a question. returns true or false
125 printf("%s? (y/n) ", st);
127 response = getchar();
130 else if (response == 'n' || response == EOF)
132 printf("please answer y/n\n");
137 * Skips the volume data on the tape. The end of the volume data is
138 * detected by the presence of the volume trailer or by an EOF indication
139 * from butm. This algorithm should be replaced by one that always
140 * detects based on the volume trailer, returning the trailer to the
141 * caller. This is of course more painful.
143 * curTapePtr - tape info structure
144 * Tape must be positioned after volume header
147 * 3 - empty volume, requires special handling
149 * Tape positioned after data, but before file end marker block.
150 * In case of errors, positioned after the error causing block
152 #define BIGCHUNK 102400
155 scanVolData(afs_int32 taskId, struct butm_tapeInfo *curTapePtr, afs_int32 tapeVersion, struct volumeHeader *volumeHeader, struct volumeHeader *volumeTrailer, afs_uint32 *bytesRead)
157 afs_int32 headBytes, tailBytes;
160 int hasdata[2], curr, prev;
161 afs_uint32 chunkSize = 0;
164 afs_int32 rcode, tcode;
166 memset(volumeHeader, 0, sizeof(struct volumeHeader));
168 block = (char *)malloc(2 * BUTM_BLOCKSIZE);
170 return (TC_NOMEMORY);
171 buffer[0] = &block[sizeof(struct blockMark)];
172 buffer[1] = &block[BUTM_BLOCKSIZE + sizeof(struct blockMark)];
173 hasdata[0] = hasdata[1] = 0;
176 tcode = NextFile(curTapePtr); /* guarantees we are at a filemark */
180 /* Read the FileBegin FileMark */
181 code = butm_ReadFileBegin(curTapePtr);
184 * Tapes made with 3.0 have no software EOT markers. Therefore
185 * at this point, we will most likely get a read error, indicating
186 * the end of this dump
188 if ((tapeVersion == TAPE_VERSION_0)
189 || (tapeVersion == TAPE_VERSION_1)) {
191 * then a tape error is possible at this point, and it
192 * signals the end of the dump. Tapes that are continued
193 * have an EOT marker.
195 TapeLog(0, taskId, code, curTapePtr->error,
196 "Read error - end-of-dump inferred\n");
200 if (code != BUTM_EOD)
201 ErrorLog(0, taskId, code, curTapePtr->error,
202 "Can't read FileBegin on tape\n");
206 /* now read the volume header */
207 code = ReadVolHeader(taskId, curTapePtr, volumeHeader);
214 /* Check for abort in the middle of scanning data */
215 if (*bytesRead >= chunkSize) {
216 if (checkAbortByTaskId(taskId))
217 ERROR_EXIT(TC_ABORTEDBYREQUEST);
218 chunkSize += BIGCHUNK;
222 * Read volume date - If prematurely hit the HW EOF
223 * marker, check to see if data contains a volumetrailer.
226 butm_ReadFileData(curTapePtr, buffer[curr], BUTM_BLKSIZE,
230 if ((rcode == BUTM_EOF) || (rcode == BUTM_ENDVOLUME))
233 ErrorLog(0, taskId, rcode, curTapePtr->error,
234 "Can't read FileData on tape\n");
238 *bytesRead += nbytes;
240 if ((nbytes != BUTM_BLKSIZE)
242 (FindVolTrailer(buffer[curr], nbytes, &tailBytes, volumeTrailer)))
245 curr = ((curr == 0) ? 1 : 0); /* Switch buffers */
248 /* Now verify that there is a volume trailer and its valid and copy it */
249 prev = ((curr == 0) ? 1 : 0);
251 (buffer[prev], (hasdata[prev] ? BUTM_BLKSIZE : 0), &headBytes,
252 buffer[curr], nbytes, &tailBytes, volumeTrailer)) {
253 code = TC_MISSINGTRAILER;
254 ErrorLog(0, taskId, code, 0, "Missing volume trailer on tape\n");
256 /* subtract size of the volume trailer from data read */
257 *bytesRead -= sizeof(struct volumeHeader);
261 * If we didn't hit the EOF while reading data, read FileEnd marker
265 tcode = butm_ReadFileEnd(curTapePtr);
267 ErrorLog(0, taskId, tcode, curTapePtr->error,
268 "Can't read EOF on tape\n");
280 * generate the name of the next tape label expected
282 * ptr to static string
286 nextTapeLabel(char *prevTapeName)
291 static char buffer[BU_MAXTAPELEN];
295 /* extract information from previous tape label */
296 strcpy(buffer, prevTapeName);
297 prevdot = strrchr(buffer, '.');
302 seq = extractTapeSeq(prevTapeName);
304 sprintf(prevdot, "%-d", seq);
310 * Read all the information on a tape. If to add to the database, queue
311 * onto list so another thread adds the entries to the database.
313 * taskid - butc task number.
314 * tapeInfoPtr - Tape information.
315 * scanInfoPtr - Info to keep so we can add entries to the db.
318 * non-0 - error. Abort the scan.
319 * moreTapes set to 1 if this is not the last tape in the dump,
320 * 0 if the last tape,
321 * -1 don't know if last tape or not.
324 afs_int32 RcreateDump();
327 readDump(afs_uint32 taskId, struct butm_tapeInfo *tapeInfoPtr, struct tapeScanInfo *scanInfoPtr)
330 afs_int32 nbytes, flags, seq;
331 int newDump = 1, newTape = 1;
332 afs_int32 tapePosition;
333 afs_int32 code = 0, tcode;
335 struct volumeHeader volHeader, volTrailer;
336 struct budb_tapeEntry tapeEntry;
337 struct budb_volumeEntry volEntry;
340 PrintDumpLabel(&scanInfoPtr->dumpLabel);
342 while (moreTapes) { /* While there is a tape to read *//*t */
344 while (1) { /* Read each volume on the tape *//*w */
346 tapePosition = tapeInfoPtr->position; /* remember position */
349 * Skip the volume data
352 scanVolData(taskId, tapeInfoPtr,
353 scanInfoPtr->tapeLabel.structVersion, &volHeader,
354 &volTrailer, &nbytes);
358 if (tcode == TC_ABORTEDBYREQUEST) { /* Aborted */
362 if (tcode == BUTM_EOD) {
363 moreTapes = 0; /* the end of the dump */
367 /* Found a volume but it's incomplete. Skip over these */
368 if (volHeader.volumeID) {
369 TapeLog(0, taskId, tcode, 0,
370 "Warning: volume %s (%u) ignored. Incomplete\n",
371 volHeader.volumeName, volHeader.volumeID);
375 /* No volume was found. We may have hit the EOT or a
376 * bad-spot. Try to skip over this spot.
378 if (badscan < 2) { /* allow 2 errors, then fail */
379 TapeLog(0, taskId, tcode, 0,
380 "Warning: Error in scanning tape - will try skipping volume\n");
383 if (scanInfoPtr->tapeLabel.structVersion >= TAPE_VERSION_4) {
384 TapeLog(0, taskId, tcode, 0,
385 "Warning: Error in scanning tape - end-of-tape inferred\n");
386 moreTapes = 1; /* then assume next tape */
388 ErrorLog(0, taskId, tcode, 0, "Error in scanning tape\n");
389 /* will ask if there is a next tape */
394 PrintVolumeHeader(&volHeader);
396 /* If this is not the first volume fragment, make sure it follows
397 * the last volume fragment
400 if ((volEntry.dump != volHeader.dumpID)
401 || (volEntry.id != volHeader.volumeID)
402 || (volEntry.seq != volHeader.frag - 2)
403 || (strcmp(volEntry.name, volHeader.volumeName))) {
405 "Warning: volume %s (%u) ignored. Incomplete - no last fragment\n",
406 volEntry.name, volEntry.id);
408 if (scanInfoPtr->addDbFlag) {
409 tcode = flushSavedEntries(DUMP_FAILED);
417 /* If this is the first volume fragment, make sure says so */
418 if (scanInfoPtr->addDbFlag && !volEntry.dump
419 && (volHeader.frag != 1)) {
421 "Warning: volume %s (%u) ignored. Incomplete - no first fragment\n",
422 volHeader.volumeName, volHeader.volumeID);
425 /* Check that this volume belongs to the dump we are scanning */
426 else if (scanInfoPtr->dumpLabel.dumpid
427 && (volHeader.dumpID != scanInfoPtr->dumpLabel.dumpid)) {
429 "Warning: volume %s (%u) ignored. Expected DumpId %u, got %u\n",
430 volHeader.volumeName, volHeader.volumeID,
431 scanInfoPtr->dumpLabel.dumpid, volHeader.dumpID);
434 /* Passed tests, Now add to the database (if dbadd flag is set) */
435 else if (scanInfoPtr->addDbFlag) {
436 /* Have enough information to create a dump entry */
438 tcode = RcreateDump(scanInfoPtr, &volHeader);
440 ErrorLog(0, taskId, tcode, 0,
441 "Can't add dump %u to database\n",
448 /* Have enough information to create a tape entry */
450 seq = extractTapeSeq(scanInfoPtr->tapeLabel.AFSName);
452 ERROR_EXIT(TC_INTERNALERROR);
455 useTape(&tapeEntry, volHeader.dumpID,
456 TNAME(&scanInfoPtr->tapeLabel), seq,
457 scanInfoPtr->tapeLabel.useCount,
458 scanInfoPtr->dumpLabel.creationTime,
459 scanInfoPtr->dumpLabel.expirationDate,
462 char gotName[BU_MAXTAPELEN + 32];
464 LABELNAME(gotName, &scanInfoPtr->tapeLabel);
465 ErrorLog(0, taskId, tcode, 0,
466 "Can't add tape %s for dump %u to database\n",
467 gotName, volHeader.dumpID);
473 /* Create the volume entry */
474 flags = ((volHeader.frag == 1) ? BUDB_VOL_FIRSTFRAG : 0);
475 if (!volTrailer.contd)
476 flags |= BUDB_VOL_LASTFRAG;
478 addVolume(&volEntry, volHeader.dumpID,
479 TNAME(&scanInfoPtr->tapeLabel),
480 volHeader.volumeName, volHeader.volumeID,
481 volHeader.cloneDate, tapePosition, nbytes,
482 (volHeader.frag - 1), flags);
484 ErrorLog(0, taskId, tcode, 0,
485 "Can't add volume %s (%u) for dump %u to database\n",
486 volHeader.volumeName, volHeader.volumeID,
492 if (volTrailer.contd) {
493 /* No need to read the EOD marker, we know there is a next tape */
497 if (scanInfoPtr->addDbFlag) {
498 tcode = flushSavedEntries(DUMP_SUCCESS);
507 if (scanInfoPtr->addDbFlag) {
509 finishTape(&tapeEntry,
510 (tapeInfoPtr->kBytes +
511 (tapeInfoPtr->nBytes ? 1 : 0)));
513 char gotName[BU_MAXTAPELEN + 32];
515 LABELNAME(gotName, &scanInfoPtr->tapeLabel);
516 ErrorLog(0, taskId, tcode, 0,
517 "Can't mark tape %s 'completed' for dump %u in database\n",
518 gotName, tapeEntry.dump);
524 /* Ask if there is another tape if we can't figure it out */
526 moreTapes = (queryoperator ? Ask("Are there more tapes") : 1);
528 /* Get the next tape label */
533 unmountTape(taskId, tapeInfoPtr);
535 tapeName = nextTapeLabel(scanInfoPtr->tapeLabel.AFSName);
536 dumpid = scanInfoPtr->tapeLabel.dumpid;
538 getScanTape(taskId, tapeInfoPtr, tapeName, dumpid, 1,
539 &scanInfoPtr->tapeLabel);
547 if (scanInfoPtr->addDbFlag) {
548 tcode = finishDump(&scanInfoPtr->dumpEntry);
550 ErrorLog(0, taskId, tcode, 0,
551 "Can't mark dump %u 'completed' in database\n",
552 scanInfoPtr->dumpEntry.id);
555 tcode = flushSavedEntries(DUMP_SUCCESS);
565 /* Will read a dump, then see if there is a dump following it and
566 * try to read that dump too.
567 * The first tape label is the first dumpLabel.
570 readDumps(afs_uint32 taskId, struct butm_tapeInfo *tapeInfoPtr, struct tapeScanInfo *scanInfoPtr)
574 memcpy(&scanInfoPtr->dumpLabel, &scanInfoPtr->tapeLabel,
575 sizeof(struct butm_tapeLabel));
578 code = readDump(taskId, tapeInfoPtr, scanInfoPtr);
582 if (scanInfoPtr->tapeLabel.structVersion < TAPE_VERSION_4)
585 /* Remember the initial dump and see if appended dump exists */
587 if (!scanInfoPtr->initialDumpId)
588 scanInfoPtr->initialDumpId = scanInfoPtr->dumpEntry.id;
590 c = butm_ReadLabel(tapeInfoPtr, &scanInfoPtr->dumpLabel, 0); /* no rewind */
591 tapepos = tapeInfoPtr->position - 1;
601 getScanTape(afs_int32 taskId, struct butm_tapeInfo *tapeInfoPtr, char *tname, afs_int32 tapeId, int prompt, struct butm_tapeLabel *tapeLabelPtr)
606 char tapename[BU_MAXTAPELEN + 32];
607 char gotname[BU_MAXTAPELEN + 32];
610 /* prompt for a tape */
613 PromptForTape(SCANOPCODE, tname, tapeId, taskId, tapecount);
620 code = butm_Mount(tapeInfoPtr, ""); /* open the tape device */
622 TapeLog(0, taskId, code, tapeInfoPtr->error, "Can't open tape\n");
626 /* read the label on the tape */
627 code = butm_ReadLabel(tapeInfoPtr, tapeLabelPtr, 1); /* rewind tape */
629 ErrorLog(0, taskId, code, tapeInfoPtr->error,
630 "Can't read tape label\n");
633 tapepos = tapeInfoPtr->position - 1;
635 /* Now check that the tape is good */
636 TAPENAME(tapename, tname, tapeId);
637 TAPENAME(gotname, tapeLabelPtr->AFSName, tapeLabelPtr->dumpid);
639 curseq = extractTapeSeq(tapeLabelPtr->AFSName);
641 /* Label can't be null or a bad name */
642 if (!strcmp(tapeLabelPtr->AFSName, "") || (curseq <= 0)) {
643 TLog(taskId, "Expected tape with dump, label seen %s\n", gotname);
647 /* Label can't be a database tape */
648 if (databaseTape(tapeLabelPtr->AFSName)) {
650 "Expected tape with dump. Can't scan database tape %s\n",
655 /* If no name, accept any tape */
656 if (strcmp(tname, "") == 0) {
657 break; /* Start scan on any tape */
660 break; /* The first tape */
662 TLog(taskId, "Expected first tape of dump, label seen %s\n",
669 if (strcmp(tname, tapeLabelPtr->AFSName)
670 || ((tapeLabelPtr->structVersion >= TAPE_VERSION_3)
671 && (tapeLabelPtr->dumpid != tapeId))) {
672 TLog(taskId, "Tape label expected %s, label seen %s\n", tapename,
677 /* We have the correct tape */
681 unmountTape(taskId, tapeInfoPtr);
689 * This set of code fragments read a tape, and add the information to
690 * the database. Builds a literal structure.
695 ScanDumps(void *param)
697 struct scanTapeIf *ptr = (struct scanTapeIf *)param;
699 struct butm_tapeInfo curTapeInfo;
700 struct tapeScanInfo tapeScanInfo;
704 taskId = ptr->taskId;
705 setStatus(taskId, DRIVE_WAIT);
706 EnterDeviceQueue(deviceLatch);
707 clearStatus(taskId, DRIVE_WAIT);
711 TLog(taskId, "ScanTape and add to the database\n");
713 TLog(taskId, "Scantape\n");
715 memset(&tapeScanInfo, 0, sizeof(tapeScanInfo));
716 tapeScanInfo.addDbFlag = ptr->addDbFlag;
718 memset(&curTapeInfo, 0, sizeof(curTapeInfo));
719 curTapeInfo.structVersion = BUTM_MAJORVERSION;
720 code = butm_file_Instantiate(&curTapeInfo, &globalTapeConfig);
722 ErrorLog(0, taskId, code, curTapeInfo.error,
723 "Can't initialize tape module\n");
728 getScanTape(taskId, &curTapeInfo, "", 0, autoQuery,
729 &tapeScanInfo.tapeLabel);
733 code = readDumps(taskId, &curTapeInfo, &tapeScanInfo);
738 unmountTape(taskId, &curTapeInfo);
741 if (code == TC_ABORTEDBYREQUEST) {
742 ErrorLog(0, taskId, 0, 0, "Scantape: Aborted by request\n");
743 clearStatus(taskId, ABORT_REQUEST);
744 setStatus(taskId, ABORT_DONE);
746 ErrorLog(0, taskId, code, 0, "Scantape: Finished with errors\n");
747 setStatus(taskId, TASK_ERROR);
749 TLog(taskId, "Scantape: Finished\n");
753 setStatus(taskId, TASK_DONE);
754 LeaveDeviceQueue(deviceLatch);
755 return (void *)(code);
765 validatePath(struct butm_tapeLabel *labelptr, char *pathptr)
768 char tapeName[BU_MAXTAPELEN];
771 if (strlen(pathptr) > BU_MAX_DUMP_PATH - 1) {
772 fprintf(stderr, "Invalid pathname - too long\n");
779 strcpy(tapeName, labelptr->AFSName);
781 tp = strrchr(tapeName, '.');
786 up = strrchr(pathptr, '/');
788 fprintf(stderr, "Invalid path name, missing /\n");
793 if (strcmp(up, tp) != 0) {
794 fprintf(stderr, "Invalid path name\n");
796 "Mismatch between tape dump name '%s' and users dump name '%s'\n",
804 * return a pointer to a (static) volume set name string.
806 * ptr - ptr to a dump name
808 * 0 - error. Can't extract volumeset name.
809 * ptr - to static volumeset string.
813 volumesetNamePtr(char *ptr)
815 static char vsname[BU_MAXUNAMELEN];
819 dotPtr = strchr(ptr, '.');
823 dotIndex = dotPtr - ptr;
824 if ((dotIndex + 1) > sizeof(vsname))
825 return (0); /* name too long */
827 strncpy(&vsname[0], ptr, dotIndex);
828 vsname[dotIndex] = 0; /* ensure null terminated */
834 extractDumpName(char *ptr)
836 static char dname[BU_MAXTAPELEN];
840 dotPtr = strrchr(ptr, '.');
844 dotIndex = dotPtr - ptr;
845 if ((dotIndex + 1) > sizeof(dname))
846 return (0); /* name too long */
848 strncpy(&dname[0], ptr, dotIndex);
849 dname[dotIndex] = 0; /* ensure null terminated */
855 * The routine assumes that tape names have an embedded sequence number
856 * as the trialing component. It is suggested that any tape naming
857 * changes retain the trailing seq. number
859 * tapename - ptr to tape name
861 * 0 or positive - sequence number
862 * -1 - error, couldn't extract sequence number
866 extractTapeSeq(char *tapename)
870 sptr = strrchr(tapename, '.');
878 * returns true or false depending on whether the tape is
879 * a database tape or not.
882 databaseTape(char *tapeName)
887 sptr = strrchr(tapeName, '.');
891 c = (int)((char *) sptr - (char *) tapeName);
892 if (strncmp(tapeName, DUMP_TAPE_NAME, c) == 0)
899 RcreateDump(struct tapeScanInfo *tapeScanInfoPtr, struct volumeHeader *volHeaderPtr)
902 struct butm_tapeLabel *dumpLabelPtr = &tapeScanInfoPtr->dumpLabel;
903 struct budb_dumpEntry *dumpEntryPtr = &tapeScanInfoPtr->dumpEntry;
905 /* construct dump entry */
906 memset(dumpEntryPtr, 0, sizeof(struct budb_dumpEntry));
907 dumpEntryPtr->id = volHeaderPtr->dumpID;
908 dumpEntryPtr->initialDumpID = tapeScanInfoPtr->initialDumpId;
909 dumpEntryPtr->parent = volHeaderPtr->parentID;
910 dumpEntryPtr->level = volHeaderPtr->level;
911 dumpEntryPtr->created = volHeaderPtr->dumpID; /* time dump was created */
912 dumpEntryPtr->flags = 0;
913 dumpEntryPtr->incTime = 0;
914 dumpEntryPtr->nVolumes = 0;
915 strcpy(dumpEntryPtr->volumeSetName,
916 volumesetNamePtr(volHeaderPtr->dumpSetName));
917 strcpy(dumpEntryPtr->dumpPath, dumpLabelPtr->dumpPath);
918 strcpy(dumpEntryPtr->name, volHeaderPtr->dumpSetName);
919 default_tapeset(&dumpEntryPtr->tapes, volHeaderPtr->dumpSetName);
920 dumpEntryPtr->tapes.b = extractTapeSeq(dumpLabelPtr->AFSName);
921 copy_ktcPrincipal_to_budbPrincipal(&dumpLabelPtr->creator,
922 &dumpEntryPtr->dumper);
924 code = bcdb_CreateDump(dumpEntryPtr);