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>
31 #include <afs/tcdata.h>
32 #include <afs/bubasics.h>
34 #include "error_macros.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;
55 * print out the tape (dump) label.
58 PrintDumpLabel(struct butm_tapeLabel *labelptr)
60 char tapeName[BU_MAXTAPELEN + 32];
63 printf("Dump label\n");
64 printf("----------\n");
65 TAPENAME(tapeName, labelptr->pName, labelptr->dumpid);
66 printf("permanent tape name = %s\n", tapeName);
67 TAPENAME(tapeName, labelptr->AFSName, labelptr->dumpid);
68 printf("AFS tape name = %s\n", tapeName);
69 t = labelptr->creationTime;
70 printf("creationTime = %s", ctime(&t));
71 if (labelptr->expirationDate) {
72 t = labelptr->expirationDate;
73 printf("expirationDate = %s", cTIME(&t));
75 printf("cell = %s\n", labelptr->cell);
76 printf("size = %u Kbytes\n", labelptr->size);
77 printf("dump path = %s\n", labelptr->dumpPath);
79 if (labelptr->structVersion >= TAPE_VERSION_3) {
80 printf("dump id = %u\n", labelptr->dumpid);
81 printf("useCount = %d\n", labelptr->useCount);
83 printf("-- End of dump label --\n\n");
87 * print the contents of a volume header.
90 PrintVolumeHeader(struct volumeHeader *volHeader)
94 printf("-- volume --\n");
95 printf("volume name: %s\n", volHeader->volumeName);
96 printf("volume ID %d\n", volHeader->volumeID);
97 /* printf("server %d\n", volHeader->server); */
98 printf("dumpSetName: %s\n", volHeader->dumpSetName);
99 printf("dumpID %d\n", volHeader->dumpID);
100 printf("level %d\n", volHeader->level);
101 printf("parentID %d\n", volHeader->parentID);
102 printf("endTime %d\n", volHeader->endTime);
103 /* printf("versionflags %d\n", volHeader->versionflags); */
104 t = volHeader->cloneDate;
105 printf("clonedate %s\n", ctime(&t));
109 * ask a question. returns true or false
123 printf("%s? (y/n) ", st);
125 response = getchar();
128 else if (response == 'n' || response == EOF)
130 printf("please answer y/n\n");
135 * Skips the volume data on the tape. The end of the volume data is
136 * detected by the presence of the volume trailer or by an EOF indication
137 * from butm. This algorithm should be replaced by one that always
138 * detects based on the volume trailer, returning the trailer to the
139 * caller. This is of course more painful.
141 * curTapePtr - tape info structure
142 * Tape must be positioned after volume header
145 * 3 - empty volume, requires special handling
147 * Tape positioned after data, but before file end marker block.
148 * In case of errors, positioned after the error causing block
150 #define BIGCHUNK 102400
153 scanVolData(afs_int32 taskId, struct butm_tapeInfo *curTapePtr, afs_int32 tapeVersion, struct volumeHeader *volumeHeader, struct volumeHeader *volumeTrailer, afs_uint32 *bytesRead)
155 afs_int32 headBytes, tailBytes;
158 int hasdata[2], curr, prev;
159 afs_uint32 chunkSize = 0;
162 afs_int32 rcode, tcode;
164 memset(volumeHeader, 0, sizeof(struct volumeHeader));
166 block = (char *)malloc(2 * BUTM_BLOCKSIZE);
168 return (TC_NOMEMORY);
169 buffer[0] = &block[sizeof(struct blockMark)];
170 buffer[1] = &block[BUTM_BLOCKSIZE + sizeof(struct blockMark)];
171 hasdata[0] = hasdata[1] = 0;
174 tcode = NextFile(curTapePtr); /* guarantees we are at a filemark */
178 /* Read the FileBegin FileMark */
179 code = butm_ReadFileBegin(curTapePtr);
182 * Tapes made with 3.0 have no software EOT markers. Therefore
183 * at this point, we will most likely get a read error, indicating
184 * the end of this dump
186 if ((tapeVersion == TAPE_VERSION_0)
187 || (tapeVersion == TAPE_VERSION_1)) {
189 * then a tape error is possible at this point, and it
190 * signals the end of the dump. Tapes that are continued
191 * have an EOT marker.
193 TapeLog(0, taskId, code, curTapePtr->error,
194 "Read error - end-of-dump inferred\n");
198 if (code != BUTM_EOD)
199 ErrorLog(0, taskId, code, curTapePtr->error,
200 "Can't read FileBegin on tape\n");
204 /* now read the volume header */
205 code = ReadVolHeader(taskId, curTapePtr, volumeHeader);
212 /* Check for abort in the middle of scanning data */
213 if (*bytesRead >= chunkSize) {
214 if (checkAbortByTaskId(taskId))
215 ERROR_EXIT(TC_ABORTEDBYREQUEST);
216 chunkSize += BIGCHUNK;
220 * Read volume date - If prematurely hit the HW EOF
221 * marker, check to see if data contains a volumetrailer.
224 butm_ReadFileData(curTapePtr, buffer[curr], BUTM_BLKSIZE,
228 if ((rcode == BUTM_EOF) || (rcode == BUTM_ENDVOLUME))
231 ErrorLog(0, taskId, rcode, curTapePtr->error,
232 "Can't read FileData on tape\n");
236 *bytesRead += nbytes;
238 if ((nbytes != BUTM_BLKSIZE)
240 (FindVolTrailer(buffer[curr], nbytes, &tailBytes, volumeTrailer)))
243 curr = ((curr == 0) ? 1 : 0); /* Switch buffers */
246 /* Now verify that there is a volume trailer and its valid and copy it */
247 prev = ((curr == 0) ? 1 : 0);
249 (buffer[prev], (hasdata[prev] ? BUTM_BLKSIZE : 0), &headBytes,
250 buffer[curr], nbytes, &tailBytes, volumeTrailer)) {
251 code = TC_MISSINGTRAILER;
252 ErrorLog(0, taskId, code, 0, "Missing volume trailer on tape\n");
254 /* subtract size of the volume trailer from data read */
255 *bytesRead -= sizeof(struct volumeHeader);
259 * If we didn't hit the EOF while reading data, read FileEnd marker
263 tcode = butm_ReadFileEnd(curTapePtr);
265 ErrorLog(0, taskId, tcode, curTapePtr->error,
266 "Can't read EOF on tape\n");
278 * generate the name of the next tape label expected
280 * ptr to static string
284 nextTapeLabel(char *prevTapeName)
289 static char buffer[BU_MAXTAPELEN];
293 /* extract information from previous tape label */
294 strcpy(buffer, prevTapeName);
295 prevdot = strrchr(buffer, '.');
300 seq = extractTapeSeq(prevTapeName);
302 sprintf(prevdot, "%-d", seq);
308 * Read all the information on a tape. If to add to the database, queue
309 * onto list so another thread adds the entries to the database.
311 * taskid - butc task number.
312 * tapeInfoPtr - Tape information.
313 * scanInfoPtr - Info to keep so we can add entries to the db.
316 * non-0 - error. Abort the scan.
317 * moreTapes set to 1 if this is not the last tape in the dump,
318 * 0 if the last tape,
319 * -1 don't know if last tape or not.
322 afs_int32 RcreateDump();
325 readDump(afs_uint32 taskId, struct butm_tapeInfo *tapeInfoPtr, struct tapeScanInfo *scanInfoPtr)
328 afs_int32 nbytes, flags, seq;
329 int newDump = 1, newTape = 1;
330 afs_int32 tapePosition;
331 afs_int32 code = 0, tcode;
333 struct volumeHeader volHeader, volTrailer;
334 struct budb_tapeEntry tapeEntry;
335 struct budb_volumeEntry volEntry;
338 PrintDumpLabel(&scanInfoPtr->dumpLabel);
340 while (moreTapes) { /* While there is a tape to read *//*t */
342 while (1) { /* Read each volume on the tape *//*w */
344 tapePosition = tapeInfoPtr->position; /* remember position */
347 * Skip the volume data
350 scanVolData(taskId, tapeInfoPtr,
351 scanInfoPtr->tapeLabel.structVersion, &volHeader,
352 &volTrailer, &nbytes);
356 if (tcode == TC_ABORTEDBYREQUEST) { /* Aborted */
360 if (tcode == BUTM_EOD) {
361 moreTapes = 0; /* the end of the dump */
365 /* Found a volume but it's incomplete. Skip over these */
366 if (volHeader.volumeID) {
367 TapeLog(0, taskId, tcode, 0,
368 "Warning: volume %s (%u) ignored. Incomplete\n",
369 volHeader.volumeName, volHeader.volumeID);
373 /* No volume was found. We may have hit the EOT or a
374 * bad-spot. Try to skip over this spot.
376 if (badscan < 2) { /* allow 2 errors, then fail */
377 TapeLog(0, taskId, tcode, 0,
378 "Warning: Error in scanning tape - will try skipping volume\n");
381 if (scanInfoPtr->tapeLabel.structVersion >= TAPE_VERSION_4) {
382 TapeLog(0, taskId, tcode, 0,
383 "Warning: Error in scanning tape - end-of-tape inferred\n");
384 moreTapes = 1; /* then assume next tape */
386 ErrorLog(0, taskId, tcode, 0, "Error in scanning tape\n");
387 /* will ask if there is a next tape */
392 PrintVolumeHeader(&volHeader);
394 /* If this is not the first volume fragment, make sure it follows
395 * the last volume fragment
398 if ((volEntry.dump != volHeader.dumpID)
399 || (volEntry.id != volHeader.volumeID)
400 || (volEntry.seq != volHeader.frag - 2)
401 || (strcmp(volEntry.name, volHeader.volumeName))) {
403 "Warning: volume %s (%u) ignored. Incomplete - no last fragment\n",
404 volEntry.name, volEntry.id);
406 if (scanInfoPtr->addDbFlag) {
407 tcode = flushSavedEntries(DUMP_FAILED);
415 /* If this is the first volume fragment, make sure says so */
416 if (scanInfoPtr->addDbFlag && !volEntry.dump
417 && (volHeader.frag != 1)) {
419 "Warning: volume %s (%u) ignored. Incomplete - no first fragment\n",
420 volHeader.volumeName, volHeader.volumeID);
423 /* Check that this volume belongs to the dump we are scanning */
424 else if (scanInfoPtr->dumpLabel.dumpid
425 && (volHeader.dumpID != scanInfoPtr->dumpLabel.dumpid)) {
427 "Warning: volume %s (%u) ignored. Expected DumpId %u, got %u\n",
428 volHeader.volumeName, volHeader.volumeID,
429 scanInfoPtr->dumpLabel.dumpid, volHeader.dumpID);
432 /* Passed tests, Now add to the database (if dbadd flag is set) */
433 else if (scanInfoPtr->addDbFlag) {
434 /* Have enough information to create a dump entry */
436 tcode = RcreateDump(scanInfoPtr, &volHeader);
438 ErrorLog(0, taskId, tcode, 0,
439 "Can't add dump %u to database\n",
446 /* Have enough information to create a tape entry */
448 seq = extractTapeSeq(scanInfoPtr->tapeLabel.AFSName);
450 ERROR_EXIT(TC_INTERNALERROR);
453 useTape(&tapeEntry, volHeader.dumpID,
454 TNAME(&scanInfoPtr->tapeLabel), seq,
455 scanInfoPtr->tapeLabel.useCount,
456 scanInfoPtr->dumpLabel.creationTime,
457 scanInfoPtr->dumpLabel.expirationDate,
460 char gotName[BU_MAXTAPELEN + 32];
462 LABELNAME(gotName, &scanInfoPtr->tapeLabel);
463 ErrorLog(0, taskId, tcode, 0,
464 "Can't add tape %s for dump %u to database\n",
465 gotName, volHeader.dumpID);
471 /* Create the volume entry */
472 flags = ((volHeader.frag == 1) ? BUDB_VOL_FIRSTFRAG : 0);
473 if (!volTrailer.contd)
474 flags |= BUDB_VOL_LASTFRAG;
476 addVolume(&volEntry, volHeader.dumpID,
477 TNAME(&scanInfoPtr->tapeLabel),
478 volHeader.volumeName, volHeader.volumeID,
479 volHeader.cloneDate, tapePosition, nbytes,
480 (volHeader.frag - 1), flags);
482 ErrorLog(0, taskId, tcode, 0,
483 "Can't add volume %s (%u) for dump %u to database\n",
484 volHeader.volumeName, volHeader.volumeID,
490 if (volTrailer.contd) {
491 /* No need to read the EOD marker, we know there is a next tape */
495 if (scanInfoPtr->addDbFlag) {
496 tcode = flushSavedEntries(DUMP_SUCCESS);
505 if (scanInfoPtr->addDbFlag) {
507 finishTape(&tapeEntry,
508 (tapeInfoPtr->kBytes +
509 (tapeInfoPtr->nBytes ? 1 : 0)));
511 char gotName[BU_MAXTAPELEN + 32];
513 LABELNAME(gotName, &scanInfoPtr->tapeLabel);
514 ErrorLog(0, taskId, tcode, 0,
515 "Can't mark tape %s 'completed' for dump %u in database\n",
516 gotName, tapeEntry.dump);
522 /* Ask if there is another tape if we can't figure it out */
524 moreTapes = (queryoperator ? Ask("Are there more tapes") : 1);
526 /* Get the next tape label */
531 unmountTape(taskId, tapeInfoPtr);
533 tapeName = nextTapeLabel(scanInfoPtr->tapeLabel.AFSName);
534 dumpid = scanInfoPtr->tapeLabel.dumpid;
536 getScanTape(taskId, tapeInfoPtr, tapeName, dumpid, 1,
537 &scanInfoPtr->tapeLabel);
545 if (scanInfoPtr->addDbFlag) {
546 tcode = finishDump(&scanInfoPtr->dumpEntry);
548 ErrorLog(0, taskId, tcode, 0,
549 "Can't mark dump %u 'completed' in database\n",
550 scanInfoPtr->dumpEntry.id);
553 tcode = flushSavedEntries(DUMP_SUCCESS);
563 /* Will read a dump, then see if there is a dump following it and
564 * try to read that dump too.
565 * The first tape label is the first dumpLabel.
568 readDumps(afs_uint32 taskId, struct butm_tapeInfo *tapeInfoPtr, struct tapeScanInfo *scanInfoPtr)
572 memcpy(&scanInfoPtr->dumpLabel, &scanInfoPtr->tapeLabel,
573 sizeof(struct butm_tapeLabel));
576 code = readDump(taskId, tapeInfoPtr, scanInfoPtr);
580 if (scanInfoPtr->tapeLabel.structVersion < TAPE_VERSION_4)
583 /* Remember the initial dump and see if appended dump exists */
585 if (!scanInfoPtr->initialDumpId)
586 scanInfoPtr->initialDumpId = scanInfoPtr->dumpEntry.id;
588 c = butm_ReadLabel(tapeInfoPtr, &scanInfoPtr->dumpLabel, 0); /* no rewind */
589 tapepos = tapeInfoPtr->position - 1;
599 getScanTape(afs_int32 taskId, struct butm_tapeInfo *tapeInfoPtr, char *tname, afs_int32 tapeId, int prompt, struct butm_tapeLabel *tapeLabelPtr)
604 char tapename[BU_MAXTAPELEN + 32];
605 char gotname[BU_MAXTAPELEN + 32];
608 /* prompt for a tape */
611 PromptForTape(SCANOPCODE, tname, tapeId, taskId, tapecount);
618 code = butm_Mount(tapeInfoPtr, ""); /* open the tape device */
620 TapeLog(0, taskId, code, tapeInfoPtr->error, "Can't open tape\n");
624 /* read the label on the tape */
625 code = butm_ReadLabel(tapeInfoPtr, tapeLabelPtr, 1); /* rewind tape */
627 ErrorLog(0, taskId, code, tapeInfoPtr->error,
628 "Can't read tape label\n");
631 tapepos = tapeInfoPtr->position - 1;
633 /* Now check that the tape is good */
634 TAPENAME(tapename, tname, tapeId);
635 TAPENAME(gotname, tapeLabelPtr->AFSName, tapeLabelPtr->dumpid);
637 curseq = extractTapeSeq(tapeLabelPtr->AFSName);
639 /* Label can't be null or a bad name */
640 if (!strcmp(tapeLabelPtr->AFSName, "") || (curseq <= 0)) {
641 TLog(taskId, "Expected tape with dump, label seen %s\n", gotname);
645 /* Label can't be a database tape */
646 if (databaseTape(tapeLabelPtr->AFSName)) {
648 "Expected tape with dump. Can't scan database tape %s\n",
653 /* If no name, accept any tape */
654 if (strcmp(tname, "") == 0) {
655 break; /* Start scan on any tape */
658 break; /* The first tape */
660 TLog(taskId, "Expected first tape of dump, label seen %s\n",
667 if (strcmp(tname, tapeLabelPtr->AFSName)
668 || ((tapeLabelPtr->structVersion >= TAPE_VERSION_3)
669 && (tapeLabelPtr->dumpid != tapeId))) {
670 TLog(taskId, "Tape label expected %s, label seen %s\n", tapename,
675 /* We have the correct tape */
679 unmountTape(taskId, tapeInfoPtr);
687 * This set of code fragments read a tape, and add the information to
688 * the database. Builds a literal structure.
693 ScanDumps(struct scanTapeIf *ptr)
695 struct butm_tapeInfo curTapeInfo;
696 struct tapeScanInfo tapeScanInfo;
700 taskId = ptr->taskId;
701 setStatus(taskId, DRIVE_WAIT);
702 EnterDeviceQueue(deviceLatch);
703 clearStatus(taskId, DRIVE_WAIT);
707 TLog(taskId, "ScanTape and add to the database\n");
709 TLog(taskId, "Scantape\n");
711 memset(&tapeScanInfo, 0, sizeof(tapeScanInfo));
712 tapeScanInfo.addDbFlag = ptr->addDbFlag;
714 memset(&curTapeInfo, 0, sizeof(curTapeInfo));
715 curTapeInfo.structVersion = BUTM_MAJORVERSION;
716 code = butm_file_Instantiate(&curTapeInfo, &globalTapeConfig);
718 ErrorLog(0, taskId, code, curTapeInfo.error,
719 "Can't initialize tape module\n");
724 getScanTape(taskId, &curTapeInfo, "", 0, autoQuery,
725 &tapeScanInfo.tapeLabel);
729 code = readDumps(taskId, &curTapeInfo, &tapeScanInfo);
734 unmountTape(taskId, &curTapeInfo);
737 if (code == TC_ABORTEDBYREQUEST) {
738 ErrorLog(0, taskId, 0, 0, "Scantape: Aborted by request\n");
739 clearStatus(taskId, ABORT_REQUEST);
740 setStatus(taskId, ABORT_DONE);
742 ErrorLog(0, taskId, code, 0, "Scantape: Finished with errors\n");
743 setStatus(taskId, TASK_ERROR);
745 TLog(taskId, "Scantape: Finished\n");
749 setStatus(taskId, TASK_DONE);
750 LeaveDeviceQueue(deviceLatch);
761 validatePath(struct butm_tapeLabel *labelptr, char *pathptr)
764 char tapeName[BU_MAXTAPELEN];
767 if (strlen(pathptr) > BU_MAX_DUMP_PATH - 1) {
768 fprintf(stderr, "Invalid pathname - too long\n");
775 strcpy(tapeName, labelptr->AFSName);
777 tp = strrchr(tapeName, '.');
782 up = strrchr(pathptr, '/');
784 fprintf(stderr, "Invalid path name, missing /\n");
789 if (strcmp(up, tp) != 0) {
790 fprintf(stderr, "Invalid path name\n");
792 "Mismatch between tape dump name '%s' and users dump name '%s'\n",
800 * return a pointer to a (static) volume set name string.
802 * ptr - ptr to a dump name
804 * 0 - error. Can't extract volumeset name.
805 * ptr - to static volumeset string.
809 volumesetNamePtr(char *ptr)
811 static char vsname[BU_MAXUNAMELEN];
815 dotPtr = strchr(ptr, '.');
819 dotIndex = dotPtr - ptr;
820 if ((dotIndex + 1) > sizeof(vsname))
821 return (0); /* name too long */
823 strncpy(&vsname[0], ptr, dotIndex);
824 vsname[dotIndex] = 0; /* ensure null terminated */
830 extractDumpName(char *ptr)
832 static char dname[BU_MAXTAPELEN];
836 dotPtr = strrchr(ptr, '.');
840 dotIndex = dotPtr - ptr;
841 if ((dotIndex + 1) > sizeof(dname))
842 return (0); /* name too long */
844 strncpy(&dname[0], ptr, dotIndex);
845 dname[dotIndex] = 0; /* ensure null terminated */
851 * The routine assumes that tape names have an embedded sequence number
852 * as the trialing component. It is suggested that any tape naming
853 * changes retain the trailing seq. number
855 * tapename - ptr to tape name
857 * 0 or positive - sequence number
858 * -1 - error, couldn't extract sequence number
862 extractTapeSeq(char *tapename)
866 sptr = strrchr(tapename, '.');
874 * returns true or false depending on whether the tape is
875 * a database tape or not.
878 databaseTape(char *tapeName)
883 sptr = strrchr(tapeName, '.');
887 c = (int)((char *) sptr - (char *) tapeName);
888 if (strncmp(tapeName, DUMP_TAPE_NAME, c) == 0)
895 RcreateDump(struct tapeScanInfo *tapeScanInfoPtr, struct volumeHeader *volHeaderPtr)
898 struct butm_tapeLabel *dumpLabelPtr = &tapeScanInfoPtr->dumpLabel;
899 struct budb_dumpEntry *dumpEntryPtr = &tapeScanInfoPtr->dumpEntry;
901 /* construct dump entry */
902 memset(dumpEntryPtr, 0, sizeof(struct budb_dumpEntry));
903 dumpEntryPtr->id = volHeaderPtr->dumpID;
904 dumpEntryPtr->initialDumpID = tapeScanInfoPtr->initialDumpId;
905 dumpEntryPtr->parent = volHeaderPtr->parentID;
906 dumpEntryPtr->level = volHeaderPtr->level;
907 dumpEntryPtr->created = volHeaderPtr->dumpID; /* time dump was created */
908 dumpEntryPtr->flags = 0;
909 dumpEntryPtr->incTime = 0;
910 dumpEntryPtr->nVolumes = 0;
911 strcpy(dumpEntryPtr->volumeSetName,
912 volumesetNamePtr(volHeaderPtr->dumpSetName));
913 strcpy(dumpEntryPtr->dumpPath, dumpLabelPtr->dumpPath);
914 strcpy(dumpEntryPtr->name, volHeaderPtr->dumpSetName);
915 default_tapeset(&dumpEntryPtr->tapes, volHeaderPtr->dumpSetName);
916 dumpEntryPtr->tapes.b = extractTapeSeq(dumpLabelPtr->AFSName);
917 copy_ktcPrincipal_to_budbPrincipal(&dumpLabelPtr->creator,
918 &dumpEntryPtr->dumper);
920 code = bcdb_CreateDump(dumpEntryPtr);