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 <afs/budb_client.h>
34 #include <afs/budb_prototypes.h>
35 #include <afs/butm_prototypes.h>
36 #include <afs/bucoord_prototypes.h>
37 #include "error_macros.h"
38 #include "butc_internal.h"
40 #define BELLCHAR 7 /* ascii for bell */
42 /* GLOBAL CONFIGURATION PARAMETERS */
44 extern int queryoperator;
46 /* Handle for the information read from all the tapes of a dump */
47 afs_int32 tapepos; /* when read a label, remember its position */
49 struct butm_tapeLabel tapeLabel, dumpLabel;
50 struct budb_dumpEntry dumpEntry;
51 afs_int32 initialDumpId;
55 extern struct tapeConfig globalTapeConfig;
56 extern struct deviceSyncNode *deviceLatch;
58 static int readDump(afs_uint32, struct butm_tapeInfo *,
59 struct tapeScanInfo *);
60 afs_int32 getScanTape(afs_int32, struct butm_tapeInfo *, char *,
61 afs_int32, int prompt, struct butm_tapeLabel *);
62 afs_int32 RcreateDump(struct tapeScanInfo *, struct volumeHeader *);
63 void copy_ktcPrincipal_to_budbPrincipal(struct ktc_principal *,
64 struct budb_principal *);
67 * print out the tape (dump) label.
70 PrintDumpLabel(struct butm_tapeLabel *labelptr)
72 char tapeName[BU_MAXTAPELEN + 32];
75 printf("Dump label\n");
76 printf("----------\n");
77 TAPENAME(tapeName, labelptr->pName, labelptr->dumpid);
78 printf("permanent tape name = %s\n", tapeName);
79 TAPENAME(tapeName, labelptr->AFSName, labelptr->dumpid);
80 printf("AFS tape name = %s\n", tapeName);
81 t = labelptr->creationTime;
82 printf("creationTime = %s", ctime(&t));
83 if (labelptr->expirationDate) {
84 t = labelptr->expirationDate;
85 printf("expirationDate = %s", cTIME(&t));
87 printf("cell = %s\n", labelptr->cell);
88 printf("size = %u Kbytes\n", labelptr->size);
89 printf("dump path = %s\n", labelptr->dumpPath);
91 if (labelptr->structVersion >= TAPE_VERSION_3) {
92 printf("dump id = %u\n", labelptr->dumpid);
93 printf("useCount = %d\n", labelptr->useCount);
95 printf("-- End of dump label --\n\n");
99 * print the contents of a volume header.
102 PrintVolumeHeader(struct volumeHeader *volHeader)
106 printf("-- volume --\n");
107 printf("volume name: %s\n", volHeader->volumeName);
108 printf("volume ID %d\n", volHeader->volumeID);
109 /* printf("server %d\n", volHeader->server); */
110 printf("dumpSetName: %s\n", volHeader->dumpSetName);
111 printf("dumpID %d\n", volHeader->dumpID);
112 printf("level %d\n", volHeader->level);
113 printf("parentID %d\n", volHeader->parentID);
114 printf("endTime %d\n", volHeader->endTime);
115 /* printf("versionflags %d\n", volHeader->versionflags); */
116 t = volHeader->cloneDate;
117 printf("clonedate %s\n", ctime(&t));
121 * ask a question. returns true or false
135 printf("%s? (y/n) ", st);
137 response = getchar();
140 else if (response == 'n' || response == EOF)
142 printf("please answer y/n\n");
147 * Skips the volume data on the tape. The end of the volume data is
148 * detected by the presence of the volume trailer or by an EOF indication
149 * from butm. This algorithm should be replaced by one that always
150 * detects based on the volume trailer, returning the trailer to the
151 * caller. This is of course more painful.
153 * curTapePtr - tape info structure
154 * Tape must be positioned after volume header
157 * 3 - empty volume, requires special handling
159 * Tape positioned after data, but before file end marker block.
160 * In case of errors, positioned after the error causing block
162 #define BIGCHUNK 102400
165 scanVolData(afs_int32 taskId, struct butm_tapeInfo *curTapePtr,
166 afs_int32 tapeVersion, struct volumeHeader *volumeHeader,
167 struct volumeHeader *volumeTrailer, afs_uint32 *bytesRead)
169 afs_int32 headBytes, tailBytes;
172 int hasdata[2], curr, prev;
173 afs_uint32 chunkSize = 0;
176 afs_int32 rcode, tcode;
178 memset(volumeHeader, 0, sizeof(struct volumeHeader));
180 block = (char *)malloc(2 * BUTM_BLOCKSIZE);
182 return (TC_NOMEMORY);
183 buffer[0] = &block[sizeof(struct blockMark)];
184 buffer[1] = &block[BUTM_BLOCKSIZE + sizeof(struct blockMark)];
185 hasdata[0] = hasdata[1] = 0;
188 tcode = NextFile(curTapePtr); /* guarantees we are at a filemark */
192 /* Read the FileBegin FileMark */
193 code = butm_ReadFileBegin(curTapePtr);
196 * Tapes made with 3.0 have no software EOT markers. Therefore
197 * at this point, we will most likely get a read error, indicating
198 * the end of this dump
200 if ((tapeVersion == TAPE_VERSION_0)
201 || (tapeVersion == TAPE_VERSION_1)) {
203 * then a tape error is possible at this point, and it
204 * signals the end of the dump. Tapes that are continued
205 * have an EOT marker.
207 TapeLog(0, taskId, code, curTapePtr->error,
208 "Read error - end-of-dump inferred\n");
212 if (code != BUTM_EOD)
213 ErrorLog(0, taskId, code, curTapePtr->error,
214 "Can't read FileBegin on tape\n");
218 /* now read the volume header */
219 code = ReadVolHeader(taskId, curTapePtr, volumeHeader);
226 /* Check for abort in the middle of scanning data */
227 if (*bytesRead >= chunkSize) {
228 if (checkAbortByTaskId(taskId))
229 ERROR_EXIT(TC_ABORTEDBYREQUEST);
230 chunkSize += BIGCHUNK;
234 * Read volume date - If prematurely hit the HW EOF
235 * marker, check to see if data contains a volumetrailer.
238 butm_ReadFileData(curTapePtr, buffer[curr], BUTM_BLKSIZE,
242 if ((rcode == BUTM_EOF) || (rcode == BUTM_ENDVOLUME))
245 ErrorLog(0, taskId, rcode, curTapePtr->error,
246 "Can't read FileData on tape\n");
250 *bytesRead += nbytes;
252 if ((nbytes != BUTM_BLKSIZE)
254 (FindVolTrailer(buffer[curr], nbytes, &tailBytes, volumeTrailer)))
257 curr = ((curr == 0) ? 1 : 0); /* Switch buffers */
260 /* Now verify that there is a volume trailer and its valid and copy it */
261 prev = ((curr == 0) ? 1 : 0);
263 (buffer[prev], (hasdata[prev] ? BUTM_BLKSIZE : 0), &headBytes,
264 buffer[curr], nbytes, &tailBytes, volumeTrailer)) {
265 code = TC_MISSINGTRAILER;
266 ErrorLog(0, taskId, code, 0, "Missing volume trailer on tape\n");
268 /* subtract size of the volume trailer from data read */
269 *bytesRead -= sizeof(struct volumeHeader);
273 * If we didn't hit the EOF while reading data, read FileEnd marker
277 tcode = butm_ReadFileEnd(curTapePtr);
279 ErrorLog(0, taskId, tcode, curTapePtr->error,
280 "Can't read EOF on tape\n");
292 * generate the name of the next tape label expected
294 * ptr to static string
298 nextTapeLabel(char *prevTapeName)
303 static char buffer[BU_MAXTAPELEN];
307 /* extract information from previous tape label */
308 strcpy(buffer, prevTapeName);
309 prevdot = strrchr(buffer, '.');
314 seq = extractTapeSeq(prevTapeName);
316 sprintf(prevdot, "%-d", seq);
322 * Read all the information on a tape. If to add to the database, queue
323 * onto list so another thread adds the entries to the database.
325 * taskid - butc task number.
326 * tapeInfoPtr - Tape information.
327 * scanInfoPtr - Info to keep so we can add entries to the db.
330 * non-0 - error. Abort the scan.
331 * moreTapes set to 1 if this is not the last tape in the dump,
332 * 0 if the last tape,
333 * -1 don't know if last tape or not.
337 readDump(afs_uint32 taskId, struct butm_tapeInfo *tapeInfoPtr,
338 struct tapeScanInfo *scanInfoPtr)
341 afs_int32 flags, seq;
342 afs_uint32 nbytes = 0;
343 int newDump = 1, newTape = 1;
344 afs_int32 tapePosition;
345 afs_int32 code = 0, tcode;
347 struct volumeHeader volHeader, volTrailer;
348 struct budb_tapeEntry tapeEntry;
349 struct budb_volumeEntry volEntry;
352 PrintDumpLabel(&scanInfoPtr->dumpLabel);
354 while (moreTapes) { /* While there is a tape to read *//*t */
356 while (1) { /* Read each volume on the tape *//*w */
358 tapePosition = tapeInfoPtr->position; /* remember position */
361 * Skip the volume data
364 scanVolData(taskId, tapeInfoPtr,
365 scanInfoPtr->tapeLabel.structVersion, &volHeader,
366 &volTrailer, &nbytes);
370 if (tcode == TC_ABORTEDBYREQUEST) { /* Aborted */
374 if (tcode == BUTM_EOD) {
375 moreTapes = 0; /* the end of the dump */
379 /* Found a volume but it's incomplete. Skip over these */
380 if (volHeader.volumeID) {
381 TapeLog(0, taskId, tcode, 0,
382 "Warning: volume %s (%u) ignored. Incomplete\n",
383 volHeader.volumeName, volHeader.volumeID);
387 /* No volume was found. We may have hit the EOT or a
388 * bad-spot. Try to skip over this spot.
390 if (badscan < 2) { /* allow 2 errors, then fail */
391 TapeLog(0, taskId, tcode, 0,
392 "Warning: Error in scanning tape - will try skipping volume\n");
395 if (scanInfoPtr->tapeLabel.structVersion >= TAPE_VERSION_4) {
396 TapeLog(0, taskId, tcode, 0,
397 "Warning: Error in scanning tape - end-of-tape inferred\n");
398 moreTapes = 1; /* then assume next tape */
400 ErrorLog(0, taskId, tcode, 0, "Error in scanning tape\n");
401 /* will ask if there is a next tape */
406 PrintVolumeHeader(&volHeader);
408 /* If this is not the first volume fragment, make sure it follows
409 * the last volume fragment
412 if ((volEntry.dump != volHeader.dumpID)
413 || (volEntry.id != volHeader.volumeID)
414 || (volEntry.seq != volHeader.frag - 2)
415 || (strcmp(volEntry.name, volHeader.volumeName))) {
417 "Warning: volume %s (%u) ignored. Incomplete - no last fragment\n",
418 volEntry.name, volEntry.id);
420 if (scanInfoPtr->addDbFlag) {
421 tcode = flushSavedEntries(DUMP_FAILED);
429 /* If this is the first volume fragment, make sure says so */
430 if (scanInfoPtr->addDbFlag && !volEntry.dump
431 && (volHeader.frag != 1)) {
433 "Warning: volume %s (%u) ignored. Incomplete - no first fragment\n",
434 volHeader.volumeName, volHeader.volumeID);
437 /* Check that this volume belongs to the dump we are scanning */
438 else if (scanInfoPtr->dumpLabel.dumpid
439 && (volHeader.dumpID != scanInfoPtr->dumpLabel.dumpid)) {
441 "Warning: volume %s (%u) ignored. Expected DumpId %u, got %u\n",
442 volHeader.volumeName, volHeader.volumeID,
443 scanInfoPtr->dumpLabel.dumpid, volHeader.dumpID);
446 /* Passed tests, Now add to the database (if dbadd flag is set) */
447 else if (scanInfoPtr->addDbFlag) {
448 /* Have enough information to create a dump entry */
450 tcode = RcreateDump(scanInfoPtr, &volHeader);
452 ErrorLog(0, taskId, tcode, 0,
453 "Can't add dump %u to database\n",
460 /* Have enough information to create a tape entry */
462 seq = extractTapeSeq(scanInfoPtr->tapeLabel.AFSName);
464 ERROR_EXIT(TC_INTERNALERROR);
467 useTape(&tapeEntry, volHeader.dumpID,
468 TNAME(&scanInfoPtr->tapeLabel), seq,
469 scanInfoPtr->tapeLabel.useCount,
470 scanInfoPtr->dumpLabel.creationTime,
471 scanInfoPtr->dumpLabel.expirationDate,
474 char gotName[BU_MAXTAPELEN + 32];
476 LABELNAME(gotName, &scanInfoPtr->tapeLabel);
477 ErrorLog(0, taskId, tcode, 0,
478 "Can't add tape %s for dump %u to database\n",
479 gotName, volHeader.dumpID);
485 /* Create the volume entry */
486 flags = ((volHeader.frag == 1) ? BUDB_VOL_FIRSTFRAG : 0);
487 if (!volTrailer.contd)
488 flags |= BUDB_VOL_LASTFRAG;
490 addVolume(&volEntry, volHeader.dumpID,
491 TNAME(&scanInfoPtr->tapeLabel),
492 volHeader.volumeName, volHeader.volumeID,
493 volHeader.cloneDate, tapePosition, nbytes,
494 (volHeader.frag - 1), flags);
496 ErrorLog(0, taskId, tcode, 0,
497 "Can't add volume %s (%u) for dump %u to database\n",
498 volHeader.volumeName, volHeader.volumeID,
504 if (volTrailer.contd) {
505 /* No need to read the EOD marker, we know there is a next tape */
509 if (scanInfoPtr->addDbFlag) {
510 tcode = flushSavedEntries(DUMP_SUCCESS);
519 if (scanInfoPtr->addDbFlag) {
521 finishTape(&tapeEntry,
522 (tapeInfoPtr->kBytes +
523 (tapeInfoPtr->nBytes ? 1 : 0)));
525 char gotName[BU_MAXTAPELEN + 32];
527 LABELNAME(gotName, &scanInfoPtr->tapeLabel);
528 ErrorLog(0, taskId, tcode, 0,
529 "Can't mark tape %s 'completed' for dump %u in database\n",
530 gotName, tapeEntry.dump);
536 /* Ask if there is another tape if we can't figure it out */
538 moreTapes = (queryoperator ? Ask("Are there more tapes") : 1);
540 /* Get the next tape label */
545 unmountTape(taskId, tapeInfoPtr);
547 tapeName = nextTapeLabel(scanInfoPtr->tapeLabel.AFSName);
548 dumpid = scanInfoPtr->tapeLabel.dumpid;
550 getScanTape(taskId, tapeInfoPtr, tapeName, dumpid, 1,
551 &scanInfoPtr->tapeLabel);
559 if (scanInfoPtr->addDbFlag) {
560 tcode = finishDump(&scanInfoPtr->dumpEntry);
562 ErrorLog(0, taskId, tcode, 0,
563 "Can't mark dump %u 'completed' in database\n",
564 scanInfoPtr->dumpEntry.id);
567 tcode = flushSavedEntries(DUMP_SUCCESS);
577 /* Will read a dump, then see if there is a dump following it and
578 * try to read that dump too.
579 * The first tape label is the first dumpLabel.
582 readDumps(afs_uint32 taskId, struct butm_tapeInfo *tapeInfoPtr,
583 struct tapeScanInfo *scanInfoPtr)
587 memcpy(&scanInfoPtr->dumpLabel, &scanInfoPtr->tapeLabel,
588 sizeof(struct butm_tapeLabel));
591 code = readDump(taskId, tapeInfoPtr, scanInfoPtr);
595 if (scanInfoPtr->tapeLabel.structVersion < TAPE_VERSION_4)
598 /* Remember the initial dump and see if appended dump exists */
600 if (!scanInfoPtr->initialDumpId)
601 scanInfoPtr->initialDumpId = scanInfoPtr->dumpEntry.id;
603 c = butm_ReadLabel(tapeInfoPtr, &scanInfoPtr->dumpLabel, 0); /* no rewind */
604 tapepos = tapeInfoPtr->position - 1;
614 getScanTape(afs_int32 taskId, struct butm_tapeInfo *tapeInfoPtr, char *tname,
615 afs_int32 tapeId, int prompt, struct butm_tapeLabel *tapeLabelPtr)
620 char tapename[BU_MAXTAPELEN + 32];
621 char gotname[BU_MAXTAPELEN + 32];
624 /* prompt for a tape */
627 PromptForTape(SCANOPCODE, tname, tapeId, taskId, tapecount);
634 code = butm_Mount(tapeInfoPtr, ""); /* open the tape device */
636 TapeLog(0, taskId, code, tapeInfoPtr->error, "Can't open tape\n");
640 /* read the label on the tape */
641 code = butm_ReadLabel(tapeInfoPtr, tapeLabelPtr, 1); /* rewind tape */
643 ErrorLog(0, taskId, code, tapeInfoPtr->error,
644 "Can't read tape label\n");
647 tapepos = tapeInfoPtr->position - 1;
649 /* Now check that the tape is good */
650 TAPENAME(tapename, tname, tapeId);
651 TAPENAME(gotname, tapeLabelPtr->AFSName, tapeLabelPtr->dumpid);
653 curseq = extractTapeSeq(tapeLabelPtr->AFSName);
655 /* Label can't be null or a bad name */
656 if (!strcmp(tapeLabelPtr->AFSName, "") || (curseq <= 0)) {
657 TLog(taskId, "Expected tape with dump, label seen %s\n", gotname);
661 /* Label can't be a database tape */
662 if (databaseTape(tapeLabelPtr->AFSName)) {
664 "Expected tape with dump. Can't scan database tape %s\n",
669 /* If no name, accept any tape */
670 if (strcmp(tname, "") == 0) {
671 break; /* Start scan on any tape */
674 break; /* The first tape */
676 TLog(taskId, "Expected first tape of dump, label seen %s\n",
683 if (strcmp(tname, tapeLabelPtr->AFSName)
684 || ((tapeLabelPtr->structVersion >= TAPE_VERSION_3)
685 && (tapeLabelPtr->dumpid != tapeId))) {
686 TLog(taskId, "Tape label expected %s, label seen %s\n", tapename,
691 /* We have the correct tape */
695 unmountTape(taskId, tapeInfoPtr);
703 * This set of code fragments read a tape, and add the information to
704 * the database. Builds a literal structure.
709 ScanDumps(void *param)
711 struct scanTapeIf *ptr = (struct scanTapeIf *)param;
713 struct butm_tapeInfo curTapeInfo;
714 struct tapeScanInfo tapeScanInfo;
718 taskId = ptr->taskId;
719 setStatus(taskId, DRIVE_WAIT);
720 EnterDeviceQueue(deviceLatch);
721 clearStatus(taskId, DRIVE_WAIT);
725 TLog(taskId, "ScanTape and add to the database\n");
727 TLog(taskId, "Scantape\n");
729 memset(&tapeScanInfo, 0, sizeof(tapeScanInfo));
730 tapeScanInfo.addDbFlag = ptr->addDbFlag;
732 memset(&curTapeInfo, 0, sizeof(curTapeInfo));
733 curTapeInfo.structVersion = BUTM_MAJORVERSION;
734 code = butm_file_Instantiate(&curTapeInfo, &globalTapeConfig);
736 ErrorLog(0, taskId, code, curTapeInfo.error,
737 "Can't initialize tape module\n");
742 getScanTape(taskId, &curTapeInfo, "", 0, autoQuery,
743 &tapeScanInfo.tapeLabel);
747 code = readDumps(taskId, &curTapeInfo, &tapeScanInfo);
752 unmountTape(taskId, &curTapeInfo);
755 if (code == TC_ABORTEDBYREQUEST) {
756 ErrorLog(0, taskId, 0, 0, "Scantape: Aborted by request\n");
757 clearStatus(taskId, ABORT_REQUEST);
758 setStatus(taskId, ABORT_DONE);
760 ErrorLog(0, taskId, code, 0, "Scantape: Finished with errors\n");
761 setStatus(taskId, TASK_ERROR);
763 TLog(taskId, "Scantape: Finished\n");
767 setStatus(taskId, TASK_DONE);
768 LeaveDeviceQueue(deviceLatch);
769 return (void *)(intptr_t)(code);
779 validatePath(struct butm_tapeLabel *labelptr, char *pathptr)
782 char tapeName[BU_MAXTAPELEN];
785 if (strlen(pathptr) > BU_MAX_DUMP_PATH - 1) {
786 fprintf(stderr, "Invalid pathname - too long\n");
793 strcpy(tapeName, labelptr->AFSName);
795 tp = strrchr(tapeName, '.');
800 up = strrchr(pathptr, '/');
802 fprintf(stderr, "Invalid path name, missing /\n");
807 if (strcmp(up, tp) != 0) {
808 fprintf(stderr, "Invalid path name\n");
810 "Mismatch between tape dump name '%s' and users dump name '%s'\n",
818 * return a pointer to a (static) volume set name string.
820 * ptr - ptr to a dump name
822 * 0 - error. Can't extract volumeset name.
823 * ptr - to static volumeset string.
827 volumesetNamePtr(char *ptr)
829 static char vsname[BU_MAXUNAMELEN];
833 dotPtr = strchr(ptr, '.');
837 dotIndex = dotPtr - ptr;
838 if ((dotIndex + 1) > sizeof(vsname))
839 return (0); /* name too long */
841 strncpy(&vsname[0], ptr, dotIndex);
842 vsname[dotIndex] = 0; /* ensure null terminated */
848 extractDumpName(char *ptr)
850 static char dname[BU_MAXTAPELEN];
854 dotPtr = strrchr(ptr, '.');
858 dotIndex = dotPtr - ptr;
859 if ((dotIndex + 1) > sizeof(dname))
860 return (0); /* name too long */
862 strncpy(&dname[0], ptr, dotIndex);
863 dname[dotIndex] = 0; /* ensure null terminated */
869 * The routine assumes that tape names have an embedded sequence number
870 * as the trialing component. It is suggested that any tape naming
871 * changes retain the trailing seq. number
873 * tapename - ptr to tape name
875 * 0 or positive - sequence number
876 * -1 - error, couldn't extract sequence number
880 extractTapeSeq(char *tapename)
884 sptr = strrchr(tapename, '.');
892 * returns true or false depending on whether the tape is
893 * a database tape or not.
896 databaseTape(char *tapeName)
901 sptr = strrchr(tapeName, '.');
905 c = (int)((char *) sptr - (char *) tapeName);
906 if (strncmp(tapeName, DUMP_TAPE_NAME, c) == 0)
913 RcreateDump(struct tapeScanInfo *tapeScanInfoPtr,
914 struct volumeHeader *volHeaderPtr)
917 struct butm_tapeLabel *dumpLabelPtr = &tapeScanInfoPtr->dumpLabel;
918 struct budb_dumpEntry *dumpEntryPtr = &tapeScanInfoPtr->dumpEntry;
920 /* construct dump entry */
921 memset(dumpEntryPtr, 0, sizeof(struct budb_dumpEntry));
922 dumpEntryPtr->id = volHeaderPtr->dumpID;
923 dumpEntryPtr->initialDumpID = tapeScanInfoPtr->initialDumpId;
924 dumpEntryPtr->parent = volHeaderPtr->parentID;
925 dumpEntryPtr->level = volHeaderPtr->level;
926 dumpEntryPtr->created = volHeaderPtr->dumpID; /* time dump was created */
927 dumpEntryPtr->flags = 0;
928 dumpEntryPtr->incTime = 0;
929 dumpEntryPtr->nVolumes = 0;
930 strcpy(dumpEntryPtr->volumeSetName,
931 volumesetNamePtr(volHeaderPtr->dumpSetName));
932 strcpy(dumpEntryPtr->dumpPath, dumpLabelPtr->dumpPath);
933 strcpy(dumpEntryPtr->name, volHeaderPtr->dumpSetName);
934 default_tapeset(&dumpEntryPtr->tapes, volHeaderPtr->dumpSetName);
935 dumpEntryPtr->tapes.b = extractTapeSeq(dumpLabelPtr->AFSName);
936 copy_ktcPrincipal_to_budbPrincipal(&dumpLabelPtr->creator,
937 &dumpEntryPtr->dumper);
939 code = bcdb_CreateDump(dumpEntryPtr);