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>
19 #include <afs/tcdata.h>
20 #include <afs/bubasics.h>
22 #include <afs/budb_client.h>
23 #include <afs/budb_prototypes.h>
24 #include <afs/butm_prototypes.h>
25 #include <afs/bucoord_prototypes.h>
27 #include "error_macros.h"
28 #include "butc_internal.h"
30 #define BELLCHAR 7 /* ascii for bell */
32 /* GLOBAL CONFIGURATION PARAMETERS */
34 extern int queryoperator;
36 /* Handle for the information read from all the tapes of a dump */
37 afs_int32 tapepos; /* when read a label, remember its position */
39 struct butm_tapeLabel tapeLabel, dumpLabel;
40 struct budb_dumpEntry dumpEntry;
41 afs_int32 initialDumpId;
45 extern struct tapeConfig globalTapeConfig;
46 extern struct deviceSyncNode *deviceLatch;
48 static int readDump(afs_uint32, struct butm_tapeInfo *,
49 struct tapeScanInfo *);
50 afs_int32 getScanTape(afs_int32, struct butm_tapeInfo *, char *,
51 afs_int32, int prompt, struct butm_tapeLabel *);
52 afs_int32 RcreateDump(struct tapeScanInfo *, struct volumeHeader *);
53 void copy_ktcPrincipal_to_budbPrincipal(struct ktc_principal *,
54 struct budb_principal *);
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,
156 afs_int32 tapeVersion, struct volumeHeader *volumeHeader,
157 struct volumeHeader *volumeTrailer, afs_uint32 *bytesRead)
159 afs_int32 headBytes, tailBytes;
162 int hasdata[2], curr, prev;
163 afs_uint32 chunkSize = 0;
166 afs_int32 rcode, tcode;
168 memset(volumeHeader, 0, sizeof(struct volumeHeader));
170 block = (char *)malloc(2 * BUTM_BLOCKSIZE);
172 return (TC_NOMEMORY);
173 buffer[0] = &block[sizeof(struct blockMark)];
174 buffer[1] = &block[BUTM_BLOCKSIZE + sizeof(struct blockMark)];
175 hasdata[0] = hasdata[1] = 0;
178 tcode = NextFile(curTapePtr); /* guarantees we are at a filemark */
182 /* Read the FileBegin FileMark */
183 code = butm_ReadFileBegin(curTapePtr);
186 * Tapes made with 3.0 have no software EOT markers. Therefore
187 * at this point, we will most likely get a read error, indicating
188 * the end of this dump
190 if ((tapeVersion == TAPE_VERSION_0)
191 || (tapeVersion == TAPE_VERSION_1)) {
193 * then a tape error is possible at this point, and it
194 * signals the end of the dump. Tapes that are continued
195 * have an EOT marker.
197 TapeLog(0, taskId, code, curTapePtr->error,
198 "Read error - end-of-dump inferred\n");
202 if (code != BUTM_EOD)
203 ErrorLog(0, taskId, code, curTapePtr->error,
204 "Can't read FileBegin on tape\n");
208 /* now read the volume header */
209 code = ReadVolHeader(taskId, curTapePtr, volumeHeader);
216 /* Check for abort in the middle of scanning data */
217 if (*bytesRead >= chunkSize) {
218 if (checkAbortByTaskId(taskId))
219 ERROR_EXIT(TC_ABORTEDBYREQUEST);
220 chunkSize += BIGCHUNK;
224 * Read volume date - If prematurely hit the HW EOF
225 * marker, check to see if data contains a volumetrailer.
228 butm_ReadFileData(curTapePtr, buffer[curr], BUTM_BLKSIZE,
232 if ((rcode == BUTM_EOF) || (rcode == BUTM_ENDVOLUME))
235 ErrorLog(0, taskId, rcode, curTapePtr->error,
236 "Can't read FileData on tape\n");
240 *bytesRead += nbytes;
242 if ((nbytes != BUTM_BLKSIZE)
244 (FindVolTrailer(buffer[curr], nbytes, &tailBytes, volumeTrailer)))
247 curr = ((curr == 0) ? 1 : 0); /* Switch buffers */
250 /* Now verify that there is a volume trailer and its valid and copy it */
251 prev = ((curr == 0) ? 1 : 0);
253 (buffer[prev], (hasdata[prev] ? BUTM_BLKSIZE : 0), &headBytes,
254 buffer[curr], nbytes, &tailBytes, volumeTrailer)) {
255 code = TC_MISSINGTRAILER;
256 ErrorLog(0, taskId, code, 0, "Missing volume trailer on tape\n");
258 /* subtract size of the volume trailer from data read */
259 *bytesRead -= sizeof(struct volumeHeader);
263 * If we didn't hit the EOF while reading data, read FileEnd marker
267 tcode = butm_ReadFileEnd(curTapePtr);
269 ErrorLog(0, taskId, tcode, curTapePtr->error,
270 "Can't read EOF on tape\n");
282 * generate the name of the next tape label expected
284 * ptr to static string
288 nextTapeLabel(char *prevTapeName)
293 static char buffer[BU_MAXTAPELEN];
297 /* extract information from previous tape label */
298 strcpy(buffer, prevTapeName);
299 prevdot = strrchr(buffer, '.');
304 seq = extractTapeSeq(prevTapeName);
306 sprintf(prevdot, "%-d", seq);
312 * Read all the information on a tape. If to add to the database, queue
313 * onto list so another thread adds the entries to the database.
315 * taskid - butc task number.
316 * tapeInfoPtr - Tape information.
317 * scanInfoPtr - Info to keep so we can add entries to the db.
320 * non-0 - error. Abort the scan.
321 * moreTapes set to 1 if this is not the last tape in the dump,
322 * 0 if the last tape,
323 * -1 don't know if last tape or not.
327 readDump(afs_uint32 taskId, struct butm_tapeInfo *tapeInfoPtr,
328 struct tapeScanInfo *scanInfoPtr)
331 afs_int32 flags, seq;
332 afs_uint32 nbytes = 0;
333 int newDump = 1, newTape = 1;
334 afs_int32 tapePosition;
335 afs_int32 code = 0, tcode;
337 struct volumeHeader volHeader, volTrailer;
338 struct budb_tapeEntry tapeEntry;
339 struct budb_volumeEntry volEntry;
342 PrintDumpLabel(&scanInfoPtr->dumpLabel);
344 while (moreTapes) { /* While there is a tape to read *//*t */
346 while (1) { /* Read each volume on the tape *//*w */
348 tapePosition = tapeInfoPtr->position; /* remember position */
351 * Skip the volume data
354 scanVolData(taskId, tapeInfoPtr,
355 scanInfoPtr->tapeLabel.structVersion, &volHeader,
356 &volTrailer, &nbytes);
360 if (tcode == TC_ABORTEDBYREQUEST) { /* Aborted */
364 if (tcode == BUTM_EOD) {
365 moreTapes = 0; /* the end of the dump */
369 /* Found a volume but it's incomplete. Skip over these */
370 if (volHeader.volumeID) {
371 TapeLog(0, taskId, tcode, 0,
372 "Warning: volume %s (%u) ignored. Incomplete\n",
373 volHeader.volumeName, volHeader.volumeID);
377 /* No volume was found. We may have hit the EOT or a
378 * bad-spot. Try to skip over this spot.
380 if (badscan < 2) { /* allow 2 errors, then fail */
381 TapeLog(0, taskId, tcode, 0,
382 "Warning: Error in scanning tape - will try skipping volume\n");
385 if (scanInfoPtr->tapeLabel.structVersion >= TAPE_VERSION_4) {
386 TapeLog(0, taskId, tcode, 0,
387 "Warning: Error in scanning tape - end-of-tape inferred\n");
388 moreTapes = 1; /* then assume next tape */
390 ErrorLog(0, taskId, tcode, 0, "Error in scanning tape\n");
391 /* will ask if there is a next tape */
396 PrintVolumeHeader(&volHeader);
398 /* If this is not the first volume fragment, make sure it follows
399 * the last volume fragment
402 if ((volEntry.dump != volHeader.dumpID)
403 || (volEntry.id != volHeader.volumeID)
404 || (volEntry.seq != volHeader.frag - 2)
405 || (strcmp(volEntry.name, volHeader.volumeName))) {
407 "Warning: volume %s (%u) ignored. Incomplete - no last fragment\n",
408 volEntry.name, volEntry.id);
410 if (scanInfoPtr->addDbFlag) {
411 tcode = flushSavedEntries(DUMP_FAILED);
419 /* If this is the first volume fragment, make sure says so */
420 if (scanInfoPtr->addDbFlag && !volEntry.dump
421 && (volHeader.frag != 1)) {
423 "Warning: volume %s (%u) ignored. Incomplete - no first fragment\n",
424 volHeader.volumeName, volHeader.volumeID);
427 /* Check that this volume belongs to the dump we are scanning */
428 else if (scanInfoPtr->dumpLabel.dumpid
429 && (volHeader.dumpID != scanInfoPtr->dumpLabel.dumpid)) {
431 "Warning: volume %s (%u) ignored. Expected DumpId %u, got %u\n",
432 volHeader.volumeName, volHeader.volumeID,
433 scanInfoPtr->dumpLabel.dumpid, volHeader.dumpID);
436 /* Passed tests, Now add to the database (if dbadd flag is set) */
437 else if (scanInfoPtr->addDbFlag) {
438 /* Have enough information to create a dump entry */
440 tcode = RcreateDump(scanInfoPtr, &volHeader);
442 ErrorLog(0, taskId, tcode, 0,
443 "Can't add dump %u to database\n",
450 /* Have enough information to create a tape entry */
452 seq = extractTapeSeq(scanInfoPtr->tapeLabel.AFSName);
454 ERROR_EXIT(TC_INTERNALERROR);
457 useTape(&tapeEntry, volHeader.dumpID,
458 TNAME(&scanInfoPtr->tapeLabel), seq,
459 scanInfoPtr->tapeLabel.useCount,
460 scanInfoPtr->dumpLabel.creationTime,
461 scanInfoPtr->dumpLabel.expirationDate,
464 char gotName[BU_MAXTAPELEN + 32];
466 LABELNAME(gotName, &scanInfoPtr->tapeLabel);
467 ErrorLog(0, taskId, tcode, 0,
468 "Can't add tape %s for dump %u to database\n",
469 gotName, volHeader.dumpID);
475 /* Create the volume entry */
476 flags = ((volHeader.frag == 1) ? BUDB_VOL_FIRSTFRAG : 0);
477 if (!volTrailer.contd)
478 flags |= BUDB_VOL_LASTFRAG;
480 addVolume(&volEntry, volHeader.dumpID,
481 TNAME(&scanInfoPtr->tapeLabel),
482 volHeader.volumeName, volHeader.volumeID,
483 volHeader.cloneDate, tapePosition, nbytes,
484 (volHeader.frag - 1), flags);
486 ErrorLog(0, taskId, tcode, 0,
487 "Can't add volume %s (%u) for dump %u to database\n",
488 volHeader.volumeName, volHeader.volumeID,
494 if (volTrailer.contd) {
495 /* No need to read the EOD marker, we know there is a next tape */
499 if (scanInfoPtr->addDbFlag) {
500 tcode = flushSavedEntries(DUMP_SUCCESS);
509 if (scanInfoPtr->addDbFlag) {
511 finishTape(&tapeEntry,
512 (tapeInfoPtr->kBytes +
513 (tapeInfoPtr->nBytes ? 1 : 0)));
515 char gotName[BU_MAXTAPELEN + 32];
517 LABELNAME(gotName, &scanInfoPtr->tapeLabel);
518 ErrorLog(0, taskId, tcode, 0,
519 "Can't mark tape %s 'completed' for dump %u in database\n",
520 gotName, tapeEntry.dump);
526 /* Ask if there is another tape if we can't figure it out */
528 moreTapes = (queryoperator ? Ask("Are there more tapes") : 1);
530 /* Get the next tape label */
535 unmountTape(taskId, tapeInfoPtr);
537 tapeName = nextTapeLabel(scanInfoPtr->tapeLabel.AFSName);
538 dumpid = scanInfoPtr->tapeLabel.dumpid;
540 getScanTape(taskId, tapeInfoPtr, tapeName, dumpid, 1,
541 &scanInfoPtr->tapeLabel);
549 if (scanInfoPtr->addDbFlag) {
550 tcode = finishDump(&scanInfoPtr->dumpEntry);
552 ErrorLog(0, taskId, tcode, 0,
553 "Can't mark dump %u 'completed' in database\n",
554 scanInfoPtr->dumpEntry.id);
557 tcode = flushSavedEntries(DUMP_SUCCESS);
567 /* Will read a dump, then see if there is a dump following it and
568 * try to read that dump too.
569 * The first tape label is the first dumpLabel.
572 readDumps(afs_uint32 taskId, struct butm_tapeInfo *tapeInfoPtr,
573 struct tapeScanInfo *scanInfoPtr)
577 memcpy(&scanInfoPtr->dumpLabel, &scanInfoPtr->tapeLabel,
578 sizeof(struct butm_tapeLabel));
581 code = readDump(taskId, tapeInfoPtr, scanInfoPtr);
585 if (scanInfoPtr->tapeLabel.structVersion < TAPE_VERSION_4)
588 /* Remember the initial dump and see if appended dump exists */
590 if (!scanInfoPtr->initialDumpId)
591 scanInfoPtr->initialDumpId = scanInfoPtr->dumpEntry.id;
593 c = butm_ReadLabel(tapeInfoPtr, &scanInfoPtr->dumpLabel, 0); /* no rewind */
594 tapepos = tapeInfoPtr->position - 1;
604 getScanTape(afs_int32 taskId, struct butm_tapeInfo *tapeInfoPtr, char *tname,
605 afs_int32 tapeId, int prompt, struct butm_tapeLabel *tapeLabelPtr)
610 char tapename[BU_MAXTAPELEN + 32];
611 char gotname[BU_MAXTAPELEN + 32];
614 /* prompt for a tape */
617 PromptForTape(SCANOPCODE, tname, tapeId, taskId, tapecount);
624 code = butm_Mount(tapeInfoPtr, ""); /* open the tape device */
626 TapeLog(0, taskId, code, tapeInfoPtr->error, "Can't open tape\n");
630 /* read the label on the tape */
631 code = butm_ReadLabel(tapeInfoPtr, tapeLabelPtr, 1); /* rewind tape */
633 ErrorLog(0, taskId, code, tapeInfoPtr->error,
634 "Can't read tape label\n");
637 tapepos = tapeInfoPtr->position - 1;
639 /* Now check that the tape is good */
640 TAPENAME(tapename, tname, tapeId);
641 TAPENAME(gotname, tapeLabelPtr->AFSName, tapeLabelPtr->dumpid);
643 curseq = extractTapeSeq(tapeLabelPtr->AFSName);
645 /* Label can't be null or a bad name */
646 if (!strcmp(tapeLabelPtr->AFSName, "") || (curseq <= 0)) {
647 TLog(taskId, "Expected tape with dump, label seen %s\n", gotname);
651 /* Label can't be a database tape */
652 if (databaseTape(tapeLabelPtr->AFSName)) {
654 "Expected tape with dump. Can't scan database tape %s\n",
659 /* If no name, accept any tape */
660 if (strcmp(tname, "") == 0) {
661 break; /* Start scan on any tape */
664 break; /* The first tape */
666 TLog(taskId, "Expected first tape of dump, label seen %s\n",
673 if (strcmp(tname, tapeLabelPtr->AFSName)
674 || ((tapeLabelPtr->structVersion >= TAPE_VERSION_3)
675 && (tapeLabelPtr->dumpid != tapeId))) {
676 TLog(taskId, "Tape label expected %s, label seen %s\n", tapename,
681 /* We have the correct tape */
685 unmountTape(taskId, tapeInfoPtr);
693 * This set of code fragments read a tape, and add the information to
694 * the database. Builds a literal structure.
699 ScanDumps(void *param)
701 struct scanTapeIf *ptr = (struct scanTapeIf *)param;
703 struct butm_tapeInfo curTapeInfo;
704 struct tapeScanInfo tapeScanInfo;
708 taskId = ptr->taskId;
709 setStatus(taskId, DRIVE_WAIT);
710 EnterDeviceQueue(deviceLatch);
711 clearStatus(taskId, DRIVE_WAIT);
715 TLog(taskId, "ScanTape and add to the database\n");
717 TLog(taskId, "Scantape\n");
719 memset(&tapeScanInfo, 0, sizeof(tapeScanInfo));
720 tapeScanInfo.addDbFlag = ptr->addDbFlag;
722 memset(&curTapeInfo, 0, sizeof(curTapeInfo));
723 curTapeInfo.structVersion = BUTM_MAJORVERSION;
724 code = butm_file_Instantiate(&curTapeInfo, &globalTapeConfig);
726 ErrorLog(0, taskId, code, curTapeInfo.error,
727 "Can't initialize tape module\n");
732 getScanTape(taskId, &curTapeInfo, "", 0, autoQuery,
733 &tapeScanInfo.tapeLabel);
737 code = readDumps(taskId, &curTapeInfo, &tapeScanInfo);
742 unmountTape(taskId, &curTapeInfo);
745 if (code == TC_ABORTEDBYREQUEST) {
746 ErrorLog(0, taskId, 0, 0, "Scantape: Aborted by request\n");
747 clearStatus(taskId, ABORT_REQUEST);
748 setStatus(taskId, ABORT_DONE);
750 ErrorLog(0, taskId, code, 0, "Scantape: Finished with errors\n");
751 setStatus(taskId, TASK_ERROR);
753 TLog(taskId, "Scantape: Finished\n");
757 setStatus(taskId, TASK_DONE);
758 LeaveDeviceQueue(deviceLatch);
759 return (void *)(intptr_t)(code);
769 validatePath(struct butm_tapeLabel *labelptr, char *pathptr)
772 char tapeName[BU_MAXTAPELEN];
775 if (strlen(pathptr) > BU_MAX_DUMP_PATH - 1) {
776 fprintf(stderr, "Invalid pathname - too long\n");
783 strcpy(tapeName, labelptr->AFSName);
785 tp = strrchr(tapeName, '.');
790 up = strrchr(pathptr, '/');
792 fprintf(stderr, "Invalid path name, missing /\n");
797 if (strcmp(up, tp) != 0) {
798 fprintf(stderr, "Invalid path name\n");
800 "Mismatch between tape dump name '%s' and users dump name '%s'\n",
808 * return a pointer to a (static) volume set name string.
810 * ptr - ptr to a dump name
812 * 0 - error. Can't extract volumeset name.
813 * ptr - to static volumeset string.
817 volumesetNamePtr(char *ptr)
819 static char vsname[BU_MAXUNAMELEN];
823 dotPtr = strchr(ptr, '.');
827 dotIndex = dotPtr - ptr;
828 if ((dotIndex + 1) > sizeof(vsname))
829 return (0); /* name too long */
831 strncpy(&vsname[0], ptr, dotIndex);
832 vsname[dotIndex] = 0; /* ensure null terminated */
838 extractDumpName(char *ptr)
840 static char dname[BU_MAXTAPELEN];
844 dotPtr = strrchr(ptr, '.');
848 dotIndex = dotPtr - ptr;
849 if ((dotIndex + 1) > sizeof(dname))
850 return (0); /* name too long */
852 strncpy(&dname[0], ptr, dotIndex);
853 dname[dotIndex] = 0; /* ensure null terminated */
859 * The routine assumes that tape names have an embedded sequence number
860 * as the trialing component. It is suggested that any tape naming
861 * changes retain the trailing seq. number
863 * tapename - ptr to tape name
865 * 0 or positive - sequence number
866 * -1 - error, couldn't extract sequence number
870 extractTapeSeq(char *tapename)
874 sptr = strrchr(tapename, '.');
882 * returns true or false depending on whether the tape is
883 * a database tape or not.
886 databaseTape(char *tapeName)
891 sptr = strrchr(tapeName, '.');
895 c = (int)((char *) sptr - (char *) tapeName);
896 if (strncmp(tapeName, DUMP_TAPE_NAME, c) == 0)
903 RcreateDump(struct tapeScanInfo *tapeScanInfoPtr,
904 struct volumeHeader *volHeaderPtr)
907 struct butm_tapeLabel *dumpLabelPtr = &tapeScanInfoPtr->dumpLabel;
908 struct budb_dumpEntry *dumpEntryPtr = &tapeScanInfoPtr->dumpEntry;
910 /* construct dump entry */
911 memset(dumpEntryPtr, 0, sizeof(struct budb_dumpEntry));
912 dumpEntryPtr->id = volHeaderPtr->dumpID;
913 dumpEntryPtr->initialDumpID = tapeScanInfoPtr->initialDumpId;
914 dumpEntryPtr->parent = volHeaderPtr->parentID;
915 dumpEntryPtr->level = volHeaderPtr->level;
916 dumpEntryPtr->created = volHeaderPtr->dumpID; /* time dump was created */
917 dumpEntryPtr->flags = 0;
918 dumpEntryPtr->incTime = 0;
919 dumpEntryPtr->nVolumes = 0;
920 strcpy(dumpEntryPtr->volumeSetName,
921 volumesetNamePtr(volHeaderPtr->dumpSetName));
922 strcpy(dumpEntryPtr->dumpPath, dumpLabelPtr->dumpPath);
923 strcpy(dumpEntryPtr->name, volHeaderPtr->dumpSetName);
924 default_tapeset(&dumpEntryPtr->tapes, volHeaderPtr->dumpSetName);
925 dumpEntryPtr->tapes.b = extractTapeSeq(dumpLabelPtr->AFSName);
926 copy_ktcPrincipal_to_budbPrincipal(&dumpLabelPtr->creator,
927 &dumpEntryPtr->dumper);
929 code = bcdb_CreateDump(dumpEntryPtr);