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>
16 #include <sys/types.h>
23 #include <netinet/in.h>
27 #include <afs/procmgmt.h>
33 #include <afs/tcdata.h>
34 #include <afs/bubasics.h> /* PA */
35 #include <afs/budb_client.h>
36 #include <afs/volser.h>
37 #include <afs/com_err.h>
38 #include "error_macros.h"
39 #include <afs/afsutil.h>
41 #include "butc_xbsa.h"
43 /* GLOBAL CONFIGURATION PARAMETERS */
44 extern int queryoperator;
45 extern int tapemounted;
46 extern char *opencallout;
47 extern char *closecallout;
49 extern char *extractDumpName();
50 extern int BufferSize; /* Size in B stored for header info */
51 FILE *restoretofilefd;
53 extern char *restoretofile;
54 extern int forcemultiple;
57 /* XBSA Global Parameters */
60 struct butx_transactionInfo butxInfo;
63 static struct TapeBlock { /* A 16KB tapeblock */
64 char mark[BUTM_HDRSIZE]; /* Header info */
65 char data[BUTM_BLKSIZE]; /* data */
68 afs_int32 dataSize; /* Size of data to read on each xbsa_ReadObjectData() call (CONF_XBSA) */
69 afs_int32 tapeblocks; /* Number of tape datablocks in buffer (!CONF_XBSA) */
72 * Need to re-write to:
73 * 1) non-interactive tape handling (optional)
74 * 2) compute tape and volume sizes for the database
75 * 3) compute and use tape id's for tape tracking (put on tape label)
76 * 4) status management
79 /* All the relevant info shared between Restorer and restoreVolume */
80 struct restoreParams {
81 struct dumpNode *nodePtr;
83 char mntTapeName[BU_MAXTAPELEN];
85 struct butm_tapeInfo *tapeInfoPtr;
88 /* Abort checks are done after each BIGCHUNK of data transfer */
89 #define BIGCHUNK 102400
91 #define HEADER_CHECKS(vhptr, header) \
93 afs_int32 magic, versionflags; \
95 versionflags = ntohl(vhptr.versionflags); \
96 if ( versionflags == TAPE_VERSION_0 || \
97 versionflags == TAPE_VERSION_1 || \
98 versionflags == TAPE_VERSION_2 || \
99 versionflags == TAPE_VERSION_3 || \
100 versionflags == TAPE_VERSION_4 ) { \
102 magic = ntohl(vhptr.magic); /* another sanity check */ \
103 if (magic == TC_VOLBEGINMAGIC || \
104 magic == TC_VOLENDMAGIC || \
105 magic == TC_VOLCONTD ) { \
107 memcpy(header, &vhptr, sizeof(struct volumeHeader)); \
110 } /* versionflags */ \
114 extern FILE *ErrorlogIO;
115 extern FILE *centralLogIO;
116 extern FILE *lastLogIO;
117 extern afs_int32 lastPass; /* Set true during last pass of dump */
118 extern int debugLevel;
119 extern int autoQuery;
120 extern struct tapeConfig globalTapeConfig;
121 extern struct deviceSyncNode *deviceLatch;
122 extern char globalCellName[];
126 /* forward declaration */
127 afs_int32 readVolumeHeader( /*char *buffer,afs_int32 bufloc,(struct volumeHeader *)vhptr */ );
129 /* The on-disk volume header or trailer can differ in size from platform to platform */
130 static struct TapeBlock tapeBlock;
131 char tapeVolumeHT[sizeof(struct volumeHeader) + 2 * sizeof(char)];
134 PrintLog(log, error1, error2, str, a, b, c, d, e, f, g, h, i, j)
136 afs_int32 error1, error2;
137 char *str, *a, *b, *c, *d, *e, *f, *g, *h, *i, *j;
141 fprintf(log, str, a, b, c, d, e, f, g, h, i, j);
146 err1 = "Volume needs to be salvaged";
149 err1 = "Bad vnode number quoted";
152 err1 = "Volume not attached, does not exist, or not on line";
155 err1 = "Volume already exists";
158 err1 = "Volume is not in service";
161 err1 = "Volume is off line";
164 err1 = "Volume is already on line";
167 err1 = "Partition is full";
170 err1 = "Volume max quota exceeded";
173 err1 = "Volume temporarily unavailable";
176 err1 = "Volume has moved to another server";
179 err1 = (char *)afs_error_message(error1);
180 err2 = (char *)afs_error_table_name(error1);
184 fprintf(log, " Possible communication failure");
186 fprintf(log, " %s: %s", err2, err1);
188 fprintf(log, ": %s", afs_error_message(error2));
195 TapeLog(debug, task, error1, error2, str, a, b, c, d, e, f, g, h, i, j)
197 afs_int32 task, error1, error2;
198 char *str, *a, *b, *c, *d, *e, *f, *g, *h, *i, *j;
201 char tbuffer[32], *timestr;
204 timestr = afs_ctime(&now, tbuffer, sizeof(tbuffer));
207 fprintf(logIO, "%s: ", timestr);
209 fprintf(logIO, "Task %u: ", task);
210 PrintLog(logIO, error1, error2, str, a, b, c, d, e, f, g, h, i, j);
212 if (lastPass && lastLogIO) {
213 fprintf(lastLogIO, "%s: ", timestr);
215 fprintf(lastLogIO, "Task %u: ", task);
216 PrintLog(lastLogIO, error1, error2, str, a, b, c, d, e, f, g, h, i,
220 /* Now print to the screen if debug level requires */
221 if (debug <= debugLevel)
222 PrintLog(stdout, error1, error2, str, a, b, c, d, e, f, g, h, i, j);
226 TLog(task, str, a, b, c, d, e, f, g, h, i, j)
228 char *str, *a, *b, *c, *d, *e, *f, *g, *h, *i, *j;
230 /* Sends message to TapeLog and stdout */
231 TapeLog(0, task, 0, 0, str, a, b, c, d, e, f, g, h, i, j);
235 ErrorLog(debug, task, error1, error2, str, a, b, c, d, e, f, g, h, i, j)
237 afs_int32 task, error1, error2;
238 char *str, *a, *b, *c, *d, *e, *f, *g, *h, *i, *j;
241 char tbuffer[32], *timestr;
244 timestr = afs_ctime(&now, tbuffer, sizeof(tbuffer));
246 fprintf(ErrorlogIO, "%s: ", timestr);
248 /* Print the time and task number */
250 fprintf(ErrorlogIO, "Task %u: ", task);
251 PrintLog(ErrorlogIO, error1, error2, str, a, b, c, d, e, f, g, h, i, j);
253 TapeLog(debug, task, error1, error2, str, a, b, c, d, e, f, g, h, i, j);
257 ELog(task, str, a, b, c, d, e, f, g, h, i, j)
259 char *str, *a, *b, *c, *d, *e, *f, *g, *h, *i, *j;
261 /* Sends message to ErrorLog, TapeLog and stdout */
262 ErrorLog(0, task, 0, 0, str, a, b, c, d, e, f, g, h, i, j);
265 /* first proc called by anybody who intends to use the device */
267 EnterDeviceQueue(devLatch)
268 struct deviceSyncNode *devLatch;
270 ObtainWriteLock(&(devLatch->lock));
271 devLatch->flags = TC_DEVICEINUSE;
274 /* last proc called by anybody finishing using the device */
276 LeaveDeviceQueue(devLatch)
277 struct deviceSyncNode *devLatch;
280 ReleaseWriteLock(&(devLatch->lock));
283 #define BELLTIME 60 /* 60 seconds before a bell rings */
284 #define BELLCHAR 7 /* ascii for bell */
287 #ifdef AFS_PTHREAD_ENV
289 /* WaitForKeystroke : Wait until a key has been struck or time (secconds)
290 * runs out and return to caller. The NT version of this function will return
291 * immediately after a key has been pressed (doesn't wait for cr).
293 * seconds: wait for <seconds> seconds before returning. If seconds < 0,
296 * 1: Keyboard input available
297 * 0: seconds elapsed. Timeout.
299 * STOLEN FROM LWP_WaitForKeystroke()
302 WaitForKeystroke(int seconds)
304 time_t startTime, nowTime;
306 struct timeval twait;
315 /* check if we have a keystroke */
321 /* sleep for LWP_KEYSTROKE_DELAY ms and let other
323 select(0, 0, 0, 0, &twait);
325 if (seconds > 0) { /* we only worry about elapsed time if
326 * not looping forever (seconds < 0) */
328 timeleft = seconds - difftime(nowTime, startTime);
330 } while (timeleft > 0);
333 #else /* AFS_NT40)ENV */
334 extern int WaitForKeystroke(int);
336 * STOLEN FROM LWP_WaitForKeystroke()
339 WaitForKeystroke(int seconds)
343 struct timeval twait;
344 struct timeval *tp = NULL;
346 #ifdef AFS_LINUX20_ENV
347 if (stdin->_IO_read_ptr < stdin->_IO_read_end)
354 FD_SET(fileno(stdin), &rdfds);
357 twait.tv_sec = seconds;
361 code = select(1 + fileno(stdin), &rdfds, NULL, NULL, tp);
362 return (code == 1) ? 1 : 0;
366 /* GetResponseKey() - Waits for a specified period of time and
367 * returns a char when one has been typed by the user.
369 * seconds - how long to wait for a key press.
370 * *key - char entered by user
372 * 0 - Time ran out before the user typed a key.
373 * 1 - Valid char is being returned.
375 * STOLEN FROM LWP_GetResponseKey();
378 GetResponseKey(int seconds, char *key)
383 return 0; /* need space to store char */
384 fflush(stdin); /* flush all existing data and start anew */
386 rc = WaitForKeystroke(seconds);
387 if (rc == 0) { /* time ran out */
392 /* now read the char. */
394 *key = getche(); /* get char and echo it to screen */
400 #endif /* AFS_PTHREAD_ENV
405 * * only external clients are in recoverDb.c. Was static. PA
415 #ifdef AFS_PTHREAD_ENV
416 w = WaitForKeystroke(0);
418 w = LWP_WaitForKeystroke(0);
419 #endif /* AFS_PTHREAD_ENV */
426 #endif /* AFS_NT40_ENV */
434 callOutRoutine(taskId, tapePath, flag, name, dbDumpId, tapecount)
457 callOut = opencallout;
460 strcpy(Sopcode, "restore");
463 strcpy(Sopcode, "appenddump");
466 strcpy(Sopcode, "dump");
469 strcpy(Sopcode, "labeltape");
471 case READLABELOPCODE:
472 strcpy(Sopcode, "readlabel");
475 strcpy(Sopcode, "scantape");
477 case RESTOREDBOPCODE:
478 strcpy(Sopcode, "restoredb");
481 strcpy(Sopcode, "savedb");
484 strcpy(Sopcode, "unmount");
485 callOut = closecallout;
488 strcpy(Sopcode, "unknown");
492 if (!callOut) /* no script to call */
495 strcpy(ScallOut, callOut);
496 CO_argv[0] = ScallOut;
498 strcpy(StapePath, tapePath);
499 CO_argv[1] = StapePath;
501 CO_argv[2] = Sopcode;
503 if (flag == CLOSEOPCODE) {
506 sprintf(Scount, "%d", tapecount);
509 /* The tape label name - special case labeltape */
510 if (!name || (strcmp(name, "") == 0)) /* no label */
511 strcpy(Stape, "none");
512 else { /* labeltape */
514 if (!strcmp(name, TC_NULLTAPENAME)) /* pass "<NULL>" instead of <NULL> */
515 strcpy(Stape, TC_QUOTEDNULLTAPENAME);
524 strcpy(Sdumpid, "none");
526 sprintf(Sdumpid, "%u", dbDumpId);
527 CO_argv[5] = Sdumpid;
534 pid = spawnprocve(callOut, CO_argv, CO_envp, 2);
536 ErrorLog(0, taskId, errno, 0,
537 "Call to %s outside routine %s failed\n", Sopcode, callOut);
546 * Unmounts a tape and prints a warning if it can't unmount it.
547 * Regardless of error, the closecallout routine will be called
548 * (unless a tape is not mounted in the first place).
550 unmountTape(taskId, tapeInfoPtr)
552 struct butm_tapeInfo *tapeInfoPtr;
555 int cpid, status, rcpid;
557 code = butm_Dismount(tapeInfoPtr);
558 if (code && (code != BUTM_NOMOUNT))
559 ErrorLog(0, taskId, code, (tapeInfoPtr)->error,
560 "Warning: Can't close tape\n");
562 if (tapemounted && closecallout) {
563 setStatus(taskId, CALL_WAIT);
566 callOutRoutine(taskId, globalTapeConfig.device, CLOSEOPCODE, "",
568 while (cpid) { /* Wait until return */
570 rcpid = waitpid(cpid, &status, WNOHANG);
575 if (rcpid == -1 && errno != EINTR) {
577 afs_com_err(whoami, errno,
578 "Error waiting for callout script to terminate.");
581 #ifdef AFS_PTHREAD_ENV
587 if (checkAbortByTaskId(taskId)) {
588 TLog(taskId, "Callout routine has been aborted\n");
589 if (kill(cpid, SIGKILL)) /* Cancel callout */
590 ErrorLog(0, taskId, errno, 0,
591 "Kill of callout process %d failed\n", cpid);
596 clearStatus(taskId, CALL_WAIT);
600 * print out prompt to operator
602 * PromptForTape only.
606 PrintPrompt(flag, name, dumpid)
610 char tapename[BU_MAXTAPELEN + 32];
613 TAPENAME(tapename, name, dumpid);
615 printf("******* OPERATOR ATTENTION *******\n");
616 printf("Device : %s \n", globalTapeConfig.device);
619 case READOPCODE: /* mount for restore */
620 printf("Please put in tape %s for reading", tapename);
623 case APPENDOPCODE: /* mount for dump (appends) */
625 dn = extractDumpName(name);
628 printf("Please put in last tape of dump set for appending dump");
631 ("Please put in last tape of dump set for appending dump %s (DumpID %u)",
635 case WRITEOPCODE: /* mount for dump */
636 if (strcmp(name, "") == 0)
637 printf("Please put in tape for writing");
639 /* The name is what we are going to label the tape as */
641 printf("Please put in tape %s for writing", tapename);
644 case LABELOPCODE: /* mount for labeltape */
645 printf("Please put in tape to be labelled as %s", tapename);
648 case READLABELOPCODE: /* mount for readlabel */
649 printf("Please put in tape whose label is to be read");
652 case SCANOPCODE: /* mount for scantape */
653 if (strcmp(name, "") == 0)
654 printf("Please put in tape to be scanned");
656 printf("Please put in tape %s for scanning", tapename);
659 case RESTOREDBOPCODE: /* Mount for restoredb */
660 printf("Please insert a tape %s for the database restore", tapename);
663 case SAVEDBOPCODE: /* Mount for savedb */
664 printf("Please insert a writeable tape %s for the database dump",
671 printf(" and hit return when done\n");
675 * Prompt the operator to change the tape.
676 * Use to be a void routine but now returns an error. Some calls
677 * don't use the error code.
679 * only external clients are in recoverDb.c. Was static PA
682 PromptForTape(flag, name, dbDumpId, taskId, tapecount)
685 afs_uint32 dbDumpId; /* Specific dump ID - If non-zero */
689 register afs_int32 code = 0;
694 int cpid, status, rcpid;
696 if (checkAbortByTaskId(taskId))
697 ERROR_EXIT(TC_ABORTEDBYREQUEST);
700 TapeLog(2, taskId, 0, 0, "Prompt for tape %s (%u)\n", name, dbDumpId);
702 TapeLog(2, taskId, 0, 0, "Prompt for tape %s\n", name);
704 CallOut = (opencallout ? 1 : 0);
706 setStatus(taskId, CALL_WAIT);
709 callOutRoutine(taskId, globalTapeConfig.device, flag, name,
710 dbDumpId, tapecount);
712 CallOut = 0; /* prompt at screen */
714 while (CallOut) { /* Check if callout routine finished */
716 rcpid = waitpid(cpid, &status, WNOHANG);
720 else if (WIFEXITED(status))
721 wcode = WEXITSTATUS(status);
726 break; /* All done */
727 } else if (wcode == 1) {
728 ERROR_EXIT(TC_ABORTEDBYREQUEST); /* Abort */
729 } else if ((flag == READOPCODE) && (wcode == 3)) {
730 ERROR_EXIT(TC_SKIPTAPE); /* Restore: skip the tape */
733 "Callout routine has exited with code %d: will prompt\n",
735 CallOut = 0; /* Switch to keyboard input */
739 /* if waitpid experienced an error, we prompt */
740 if (rcpid == -1 && errno != EINTR) {
741 afs_com_err(whoami, errno,
742 "Error waiting for callout script to terminate.");
744 "Can't get exit status from callout script. will prompt\n");
748 #ifdef AFS_PTHREAD_ENV
754 if (checkAbortByTaskId(taskId)) {
756 ("This tape operation has been aborted by the coordinator.\n");
758 if (kill(cpid, SIGKILL)) /* Cancel callout */
759 ErrorLog(0, taskId, errno, 0,
760 "Kill of callout process %d failed\n", cpid);
762 ERROR_EXIT(TC_ABORTEDBYREQUEST);
768 clearStatus(taskId, CALL_WAIT);
769 setStatus(taskId, OPR_WAIT);
771 PrintPrompt(flag, name, dbDumpId);
773 /* Loop until we get ok to go ahead (or abort) */
775 if (time(0) > start + BELLTIME) {
781 #ifdef AFS_PTHREAD_ENV
782 wcode = GetResponseKey(5, &inchr); /* inchr stores key read */
784 wcode = LWP_GetResponseKey(5, &inchr); /* inchr stores key read */
786 if (wcode == 1) { /* keyboard input is available */
788 if ((inchr == 'a') || (inchr == 'A')) {
789 printf("This tape operation has been aborted.\n");
790 ERROR_EXIT(TC_ABORTEDBYREQUEST); /* Abort command */
791 } else if ((flag == READOPCODE)
792 && ((inchr == 's') || (inchr == 'S'))) {
793 printf("This tape will be skipped.\n");
794 ERROR_EXIT(TC_SKIPTAPE); /* Restore: skip the tape */
796 break; /* continue */
799 if (checkAbortByTaskId(taskId)) {
801 ("This tape operation has been aborted by the coordinator.\n");
802 ERROR_EXIT(TC_ABORTEDBYREQUEST);
808 printf("Thanks, now proceeding with tape ");
810 case RESTOREDBOPCODE:
816 printf("append writing");
828 case READLABELOPCODE:
829 printf("label reading");
841 printf(" operation.\n");
843 printf("**********************************\n");
845 TapeLog(2, taskId, 0, 0, "Proceeding with tape operation\n");
849 clearStatus(taskId, (OPR_WAIT | CALL_WAIT));
855 * convert the fields in the tapeVolHeader into host byte order,
856 * placing the converted copy of the structure into the hostVolHeader
858 * tapeVolHeader - points to volume header read from tape
859 * hostVolHeader - pointer to struct for result
861 * hostVolHeader - information in host byte order
865 VolHeaderToHost(hostVolHeader, tapeVolHeader)
866 struct volumeHeader *hostVolHeader, *tapeVolHeader;
868 switch (ntohl(tapeVolHeader->versionflags)) {
870 /* sizes in bytes and fields in host order */
871 memcpy(tapeVolHeader, hostVolHeader, sizeof(struct volumeHeader));
876 case TAPE_VERSION_3: /* for present */
878 /* sizes in K and fields in network order */
879 /* do the conversion field by field */
881 strcpy(hostVolHeader->preamble, tapeVolHeader->preamble);
882 strcpy(hostVolHeader->postamble, tapeVolHeader->postamble);
883 strcpy(hostVolHeader->volumeName, tapeVolHeader->volumeName);
884 strcpy(hostVolHeader->dumpSetName, tapeVolHeader->dumpSetName);
885 hostVolHeader->volumeID = ntohl(tapeVolHeader->volumeID);
886 hostVolHeader->server = ntohl(tapeVolHeader->server);
887 hostVolHeader->part = ntohl(tapeVolHeader->part);
888 hostVolHeader->from = ntohl(tapeVolHeader->from);
889 hostVolHeader->frag = ntohl(tapeVolHeader->frag);
890 hostVolHeader->magic = ntohl(tapeVolHeader->magic);
891 hostVolHeader->contd = ntohl(tapeVolHeader->contd);
892 hostVolHeader->dumpID = ntohl(tapeVolHeader->dumpID);
893 hostVolHeader->level = ntohl(tapeVolHeader->level);
894 hostVolHeader->parentID = ntohl(tapeVolHeader->parentID);
895 hostVolHeader->endTime = ntohl(tapeVolHeader->endTime);
896 hostVolHeader->versionflags = ntohl(tapeVolHeader->versionflags);
897 hostVolHeader->cloneDate = ntohl(tapeVolHeader->cloneDate);
901 return (TC_BADVOLHEADER);
907 ReadVolHeader(taskId, tapeInfoPtr, volHeaderPtr)
909 struct butm_tapeInfo *tapeInfoPtr;
910 struct volumeHeader *volHeaderPtr;
914 struct volumeHeader volHead;
916 /* Read the volume header */
918 butm_ReadFileData(tapeInfoPtr, tapeBlock.data, sizeof(tapeVolumeHT),
921 ErrorLog(0, taskId, code, tapeInfoPtr->error,
922 "Can't read volume header on tape\n");
926 code = readVolumeHeader(tapeBlock.data, 0L, &volHead);
928 ErrorLog(0, taskId, code, 0,
929 "Can't find volume header on tape block\n");
933 code = VolHeaderToHost(volHeaderPtr, &volHead);
935 ErrorLog(0, taskId, code, 0, "Can't convert volume header\n");
944 GetVolumeHead(taskId, tapeInfoPtr, position, volName, volId)
946 struct butm_tapeInfo *tapeInfoPtr;
952 struct volumeHeader tapeVolHeader;
954 /* Position directly to the volume and read the header */
956 code = butm_Seek(tapeInfoPtr, position);
958 ErrorLog(0, taskId, code, tapeInfoPtr->error,
959 "Can't seek to position %u on tape\n", position);
963 code = butm_ReadFileBegin(tapeInfoPtr);
965 ErrorLog(0, taskId, code, tapeInfoPtr->error,
966 "Can't read FileBegin on tape\n");
970 /* Read the volume header */
971 code = ReadVolHeader(taskId, tapeInfoPtr, &tapeVolHeader);
975 /* Check if volume header matches */
976 if (strcmp(tapeVolHeader.volumeName, volName))
977 ERROR_EXIT(TC_BADVOLHEADER);
978 if (volId && (tapeVolHeader.volumeID != volId))
979 ERROR_EXIT(TC_BADVOLHEADER);
980 if (tapeVolHeader.magic != TC_VOLBEGINMAGIC)
981 ERROR_EXIT(TC_BADVOLHEADER);
984 /* Do a sequential search for the volume */
987 code = butm_ReadFileBegin(tapeInfoPtr);
989 ErrorLog(0, taskId, code, tapeInfoPtr->error,
990 "Can't read FileBegin on tape\n");
994 code = ReadVolHeader(taskId, tapeInfoPtr, &tapeVolHeader);
996 ERROR_EXIT(TC_VOLUMENOTONTAPE);
998 /* Test if we found the volume */
999 if ((strcmp(tapeVolHeader.volumeName, volName) == 0)
1000 && (!volId || (volId == tapeVolHeader.volumeID)))
1003 /* skip to the next HW EOF marker */
1004 code = SeekFile(tapeInfoPtr, 1);
1006 ErrorLog(0, taskId, code, tapeInfoPtr->error,
1007 "Can't seek to next EOF on tape\n");
1018 GetRestoreTape(taskId, tapeInfoPtr, tname, tapeID, prompt)
1020 struct butm_tapeInfo *tapeInfoPtr;
1025 struct butm_tapeLabel tapeLabel;
1026 afs_int32 code = 0, rc;
1028 struct budb_dumpEntry dumpEntry;
1030 /* Make sure that the dump/tape is not a XBSA dump */
1031 rc = bcdb_FindDumpByID(tapeID, &dumpEntry);
1032 if (!rc && (dumpEntry.flags & (BUDB_DUMP_ADSM | BUDB_DUMP_BUTA))) {
1033 ErrorLog(0, taskId, 0, 0,
1034 "Volumes from dump %u are XBSA dumps (skipping)\n", tapeID);
1035 ERROR_EXIT(TC_SKIPTAPE);
1041 PromptForTape(READOPCODE, tname, tapeID, taskId, tapecount);
1048 code = butm_Mount(tapeInfoPtr, tname);
1050 TapeLog(0, taskId, code, tapeInfoPtr->error, "Can't open tape\n");
1054 code = butm_ReadLabel(tapeInfoPtr, &tapeLabel, 1);
1056 ErrorLog(0, taskId, code, tapeInfoPtr->error,
1057 "Can't read tape label\n");
1061 /* Now check the label to see if the tapename matches or tapeids match */
1062 if (strcmp(TNAME(&tapeLabel), tname)
1063 || ((tapeLabel.structVersion >= TAPE_VERSION_3)
1064 && (tapeLabel.dumpid != tapeID))) {
1065 char expectedName[BU_MAXTAPELEN + 32],
1066 gotName[BU_MAXTAPELEN + 32];
1068 TAPENAME(expectedName, tname, tapeID);
1069 LABELNAME(gotName, &tapeLabel);
1071 TapeLog(0, taskId, 0, 0,
1072 "Tape label expected %s, label seen %s\n", expectedName,
1080 unmountTape(taskId, tapeInfoPtr);
1088 xbsaRestoreVolumeData(call, rparamsPtr)
1089 register struct rx_call *call;
1090 struct restoreParams *rparamsPtr;
1094 afs_int32 curChunk, rc;
1095 afs_uint32 totalWritten;
1096 afs_int32 headBytes, tailBytes, w;
1098 struct volumeHeader volTrailer;
1099 afs_int32 vtsize = 0;
1101 struct dumpNode *nodePtr;
1102 struct tc_restoreDesc *Restore;
1103 afs_int32 bytesRead, tbuffersize, endData = 0;
1104 char *buffer = (char *)bufferBlock, tbuffer[256];
1106 nodePtr = rparamsPtr->nodePtr;
1107 Restore = nodePtr->restores;
1108 taskId = nodePtr->taskID;
1110 /* Read the volume fragment one block at a time until
1111 * find a volume trailer
1113 curChunk = BIGCHUNK + 1;
1118 rc = xbsa_ReadObjectData(&butxInfo, buffer, dataSize, &bytesRead,
1120 if (restoretofile && (bytesRead > 0)) {
1121 fwrite(buffer, bytesRead, 1, restoretofilefd); /* Save to a file */
1123 if (rc != XBSA_SUCCESS) {
1124 ErrorLog(0, taskId, rc, 0,
1125 "Unable to read volume data from the server\n");
1129 /* Periodically update status structure and check if should abort */
1130 curChunk += bytesRead;
1131 if (curChunk > BIGCHUNK) {
1134 nodePtr->statusNodePtr->nKBytes = totalWritten / 1024;
1137 if (checkAbortByTaskId(taskId))
1138 ERROR_EXIT(TC_ABORTEDBYREQUEST);
1141 if (!endData && (bytesRead > 0)) {
1142 /* Fill tbuffer up with data from end of buffer and write
1143 * the remainder of buffer out.
1145 if ((tbuffersize == 0) || (bytesRead >= sizeof(tbuffer))) {
1146 /* Write out contents of tbuffer */
1148 w = rx_Write(call, tbuffer, tbuffersize);
1149 if (w != tbuffersize) {
1150 ErrorLog(0, taskId, -1, 0,
1151 "Error in RX write: Wrote %d bytes\n", w);
1156 /* fill tbuffer with end of buffer */
1157 bytesRead -= sizeof(tbuffer);
1158 memcpy(tbuffer, buffer + bytesRead, sizeof(tbuffer));
1159 tbuffersize = sizeof(tbuffer);
1160 /* Write out whatever is left over in buffer */
1162 w = rx_Write(call, buffer, bytesRead);
1163 if (w != bytesRead) {
1164 ErrorLog(0, taskId, -1, 0,
1165 "Error in RX data write: Wrote %d bytes\n",
1172 } else if ((tbuffersize + bytesRead) <= sizeof(tbuffer)) {
1173 /* Copy all of buffer into tbuffer (it will fit) */
1174 memcpy(tbuffer + tbuffersize, buffer, bytesRead);
1175 tbuffersize += bytesRead;
1178 /* We need to write some of tbuffer out and fill it with buffer */
1179 int towrite = bytesRead - (sizeof(tbuffer) - tbuffersize);
1180 w = rx_Write(call, tbuffer, towrite);
1182 ErrorLog(0, taskId, -1, 0,
1183 "Error in RX write: Wrote %d bytes\n", w);
1189 /* Move the data in tbuffer up */
1190 memcpy(tbuffer, tbuffer + towrite, tbuffersize);
1192 /* Now copy buffer in */
1193 memcpy(tbuffer + tbuffersize, buffer, bytesRead);
1194 tbuffersize += bytesRead;
1200 /* Pull the volume trailer from the last two buffers */
1202 FindVolTrailer2(tbuffer, tbuffersize, &headBytes, buffer, bytesRead,
1203 &tailBytes, &volTrailer);
1206 ErrorLog(0, taskId, TC_MISSINGTRAILER, 0, "Missing volume trailer\n");
1207 ERROR_EXIT(TC_MISSINGTRAILER);
1210 /* Now rx_write the data in the last two blocks */
1212 w = rx_Write(call, tbuffer, headBytes);
1213 if (w != headBytes) {
1214 ErrorLog(0, taskId, -1, 0,
1215 "Error in RX trail1 write: Wrote %d bytes\n", w);
1221 w = rx_Write(call, buffer, tailBytes);
1222 if (w != tailBytes) {
1223 ErrorLog(0, taskId, -1, 0,
1224 "Error in RX trail2 write: Wrote %d bytes\n", w);
1236 * sends the contents of volume dump to Rx Stream associated
1241 restoreVolumeData(call, rparamsPtr)
1242 register struct rx_call *call;
1243 struct restoreParams *rparamsPtr;
1246 afs_uint32 totalWritten = 0;
1248 afs_int32 headBytes, tailBytes, w;
1250 afs_int32 nbytes; /* # bytes data in last tape block read */
1251 struct volumeHeader tapeVolTrailer;
1254 afs_int32 startRbuf, endRbuf, startWbuf, endWbuf, buf, pbuf, lastbuf;
1255 struct tc_restoreDesc *Restore;
1256 struct dumpNode *nodePtr;
1257 struct butm_tapeInfo *tapeInfoPtr;
1259 afs_int32 origVolID;
1261 nodePtr = rparamsPtr->nodePtr;
1262 taskId = nodePtr->taskID;
1263 Restore = nodePtr->restores;
1264 tapeInfoPtr = rparamsPtr->tapeInfoPtr;
1265 origVolName = Restore[rparamsPtr->frag].oldName;
1266 origVolID = Restore[rparamsPtr->frag].origVid;
1268 /* Read the volume one fragment at a time */
1269 while (rparamsPtr->frag < nodePtr->arraySize) {
1271 curChunk = BIGCHUNK + 1; /* Check if should abort */
1273 /* Read the volume fragment one block at a time until
1274 * find a volume trailer
1280 while (moretoread) {
1281 /* Fill the circular buffer with tape blocks
1282 * Search for volume trailer in the process.
1287 butm_ReadFileData(tapeInfoPtr, bufferBlock[buf].data,
1288 BUTM_BLKSIZE, &nbytes);
1290 ErrorLog(0, taskId, code, tapeInfoPtr->error,
1291 "Can't read FileData on tape %s\n",
1292 rparamsPtr->mntTapeName);
1295 curChunk += BUTM_BLKSIZE;
1297 /* Periodically update status structure and check if should abort */
1298 if (curChunk > BIGCHUNK) {
1302 nodePtr->statusNodePtr->nKBytes = totalWritten / 1024;
1305 if (checkAbortByTaskId(taskId))
1306 ERROR_EXIT(TC_ABORTEDBYREQUEST);
1309 /* step to next block in buffer */
1311 buf = ((buf + 1) == tapeblocks) ? 0 : (buf + 1);
1313 /* If this is the end of the volume, the exit the loop */
1314 if ((nbytes != BUTM_BLKSIZE)
1317 (bufferBlock[pbuf].data, nbytes, &tailBytes,
1321 } while (moretoread && (buf != endRbuf));
1323 /* Write the buffer upto (but not including) the last read block
1324 * If volume is completely read, then leave the last two blocks.
1326 lastbuf = endWbuf = pbuf;
1327 if (!moretoread && (endWbuf != startWbuf))
1328 endWbuf = (endWbuf == 0) ? (tapeblocks - 1) : (endWbuf - 1);
1330 for (buf = startWbuf; buf != endWbuf;
1331 buf = (((buf + 1) == tapeblocks) ? 0 : (buf + 1))) {
1332 w = rx_Write(call, bufferBlock[buf].data, BUTM_BLKSIZE);
1333 if (w != BUTM_BLKSIZE) {
1334 ErrorLog(0, taskId, -1, 0, "Error in RX write\n");
1337 totalWritten += BUTM_BLKSIZE;
1340 /* Setup pointers to refill buffer */
1341 startRbuf = ((lastbuf + 1) == tapeblocks) ? 0 : (lastbuf + 1);
1343 startWbuf = endWbuf;
1346 /* lastbuf is last block read and it has nbytes of data
1347 * startWbuf is the 2nd to last block read
1348 * Seach for the volume trailer in these two blocks.
1350 if (lastbuf == startWbuf)
1352 FindVolTrailer2(NULL, 0, &headBytes,
1353 bufferBlock[lastbuf].data, nbytes, &tailBytes,
1357 FindVolTrailer2(bufferBlock[startWbuf].data, BUTM_BLKSIZE,
1358 &headBytes, bufferBlock[lastbuf].data, nbytes,
1359 &tailBytes, &tapeVolTrailer);
1361 ErrorLog(0, taskId, TC_MISSINGTRAILER, 0,
1362 "Missing volume trailer on tape %s\n",
1363 rparamsPtr->mntTapeName);
1364 ERROR_EXIT(TC_MISSINGTRAILER);
1367 /* Now rx_write the data in the last two blocks */
1369 w = rx_Write(call, bufferBlock[startWbuf].data, headBytes);
1370 if (w != headBytes) {
1371 ErrorLog(0, taskId, -1, 0, "Error in RX write\n");
1374 totalWritten += headBytes;
1377 w = rx_Write(call, bufferBlock[lastbuf].data, tailBytes);
1378 if (w != tailBytes) {
1379 ErrorLog(0, taskId, -1, 0, "Error in RX write\n");
1382 totalWritten += tailBytes;
1385 /* Exit the loop if the volume is not continued on next tape */
1386 if (!tapeVolTrailer.contd)
1387 break; /* We've read the entire volume */
1389 /* Volume is continued on next tape.
1390 * Step to the next volume fragment and prompt for its tape.
1391 * When a volume has multiple frags, those frags are on different
1392 * tapes. So we know that we need to prompt for a tape.
1395 if (rparamsPtr->frag >= nodePtr->arraySize)
1398 unmountTape(taskId, tapeInfoPtr);
1399 strcpy(rparamsPtr->mntTapeName, Restore[rparamsPtr->frag].tapeName);
1400 rparamsPtr->tapeID =
1401 (Restore[rparamsPtr->frag].
1402 initialDumpId ? Restore[rparamsPtr->frag].
1403 initialDumpId : Restore[rparamsPtr->frag].dbDumpId);
1405 GetRestoreTape(taskId, tapeInfoPtr, rparamsPtr->mntTapeName,
1406 rparamsPtr->tapeID, 1);
1410 /* Position to the frag and read the volume header */
1412 GetVolumeHead(taskId, tapeInfoPtr,
1413 Restore[rparamsPtr->frag].position, origVolName,
1416 ErrorLog(0, taskId, code, 0,
1417 "Can't find volume %s (%u) on tape %s\n", origVolName,
1418 origVolID, rparamsPtr->mntTapeName);
1419 ERROR_EXIT(TC_VOLUMENOTONTAPE);
1428 * Find all the volumes on a specific tape and mark them to skip.
1430 SkipTape(Restore, size, index, tapename, tapeid, taskid)
1431 struct tc_restoreDesc *Restore;
1432 afs_int32 size, index, tapeid, taskid;
1437 for (i = index; i < size; i++) {
1438 if (Restore[i].flags & RDFLAG_SKIP)
1441 (Restore[i].initialDumpId ? Restore[i].initialDumpId : Restore[i].
1443 if ((strcmp(Restore[i].tapeName, tapename) == 0) && (tid == tapeid)) {
1444 SkipVolume(Restore, size, i, Restore[i].origVid, taskid);
1451 * Find all the entries for a volume and mark them to skip.
1453 SkipVolume(Restore, size, index, volid, taskid)
1454 struct tc_restoreDesc *Restore;
1455 afs_int32 size, index, volid, taskid;
1460 for (i = index; i < size; i++) {
1461 if (Restore[i].flags & RDFLAG_SKIP)
1463 if (Restore[i].origVid == volid) {
1464 Restore[i].flags |= RDFLAG_SKIP;
1466 TLog(taskid, "Restore: Skipping %svolume %s (%u)\n",
1467 ((Restore[i].dumpLevel == 0) ? "" : "remainder of "),
1468 Restore[i].oldName, volid);
1476 xbsaRestoreVolume(taskId, restoreInfo, rparamsPtr)
1478 struct tc_restoreDesc *restoreInfo;
1479 struct restoreParams *rparamsPtr;
1484 afs_int32 newServer, newPart, newVolId;
1486 int restoreflags, havetrans = 0, startread = 0;
1487 afs_int32 bytesRead, endData = 0;
1489 struct budb_dumpEntry dumpEntry;
1490 char volumeNameStr[XBSA_MAX_PATHNAME], dumpIdStr[XBSA_MAX_OSNAME];
1491 struct volumeHeader volHeader, hostVolHeader;
1493 if (restoretofile) {
1494 restoretofilefd = fopen(restoretofile, "w+");
1497 dumpID = restoreInfo->dbDumpId;
1499 rc = bcdb_FindDumpByID(dumpID, &dumpEntry);
1501 ErrorLog(0, taskId, rc, 0, "Can't read database for dump %u\n",
1506 /* ADSM servers restore ADSM and BUTA dumps */
1507 if ((xbsaType == XBSA_SERVER_TYPE_ADSM)
1508 && !(dumpEntry.flags & (BUDB_DUMP_ADSM | BUDB_DUMP_BUTA))) {
1510 "The dump requested by this restore operation for the "
1511 "volumeset is incompatible with this instance of butc\n");
1512 /* Skip the entire dump (one dump per tape) */
1513 ERROR_EXIT(TC_SKIPTAPE);
1516 /* make sure we are connected to the correct server. */
1517 if ((strlen((char *)dumpEntry.tapes.tapeServer) != 0)
1518 && (strcmp((char *)dumpEntry.tapes.tapeServer, butxInfo.serverName) !=
1520 if ((dumpEntry.flags & (BUDB_DUMP_XBSA_NSS | BUDB_DUMP_BUTA))
1521 && !forcemultiple) {
1523 "Dump %d is on server %s but butc is connected "
1524 "to server %s (attempting to restore)\n", dumpID,
1525 (char *)dumpEntry.tapes.tapeServer, butxInfo.serverName);
1528 "Dump %d is on server %s but butc is connected "
1529 "to server %s (switching servers)\n", dumpID,
1530 (char *)dumpEntry.tapes.tapeServer, butxInfo.serverName);
1532 rc = InitToServer(taskId, &butxInfo,
1533 (char *)dumpEntry.tapes.tapeServer);
1534 if (rc != XBSA_SUCCESS)
1535 ERROR_EXIT(TC_SKIPTAPE);
1539 /* Start a transaction and query the server for the correct fileset dump */
1540 rc = xbsa_BeginTrans(&butxInfo);
1541 if (rc != XBSA_SUCCESS) {
1542 ELog(taskId, "Unable to create a new transaction\n");
1543 ERROR_EXIT(TC_SKIPTAPE);
1547 if (dumpEntry.flags & BUDB_DUMP_BUTA) { /* old buta style names */
1548 sprintf(dumpIdStr, "/%d", dumpID);
1549 strcpy(volumeNameStr, "/");
1550 strcat(volumeNameStr, restoreInfo->oldName);
1551 } else { /* new butc names */
1552 extern char *butcdumpIdStr;
1553 strcpy(dumpIdStr, butcdumpIdStr);
1554 sprintf(volumeNameStr, "/%d", dumpID);
1555 strcat(volumeNameStr, "/");
1556 strcat(volumeNameStr, restoreInfo->oldName);
1559 rc = xbsa_QueryObject(&butxInfo, dumpIdStr, volumeNameStr);
1560 if (rc != XBSA_SUCCESS) {
1562 "Unable to locate object (%s) of dump (%s) on the server\n",
1563 volumeNameStr, dumpIdStr);
1567 rc = xbsa_EndTrans(&butxInfo);
1569 if (rc != XBSA_SUCCESS) {
1570 ELog(taskId, "Unable to terminate the current transaction\n");
1574 if (checkAbortByTaskId(taskId))
1575 ERROR_EXIT(TC_ABORTEDBYREQUEST);
1577 /* Now start a transaction on the volume to restore and read the
1578 * volumeheader. We do this before starting a transaction on
1579 * volserver to restore the volume because the XBSA server may take
1580 * a while to mount and seek to the volume causing the volserver to
1583 rc = xbsa_BeginTrans(&butxInfo);
1584 if (rc != XBSA_SUCCESS) {
1585 ELog(taskId, "Unable to create a new transaction\n");
1586 ERROR_EXIT(TC_SKIPTAPE);
1590 rc = xbsa_ReadObjectBegin(&butxInfo, (char *)&volHeader,
1591 sizeof(volHeader), &bytesRead, &endData);
1592 if (restoretofile && (bytesRead > 0)) {
1593 fwrite((char *)&volHeader, bytesRead, 1, restoretofilefd); /* Save to a file */
1595 if (rc != XBSA_SUCCESS) {
1597 "Unable to begin reading of the volume from the server\n");
1602 if ((bytesRead != sizeof(volHeader)) || endData) {
1604 "The size of data read (%d) does not equal the size of data requested (%d)\n",
1605 bytesRead, sizeof(volHeader));
1606 ERROR_EXIT(TC_BADVOLHEADER);
1609 /* convert and check the volume header */
1610 rc = VolHeaderToHost(&hostVolHeader, &volHeader);
1612 ErrorLog(0, taskId, code, 0, "Can't convert volume header\n");
1616 if ((strcmp(hostVolHeader.volumeName, restoreInfo->oldName) != 0)
1617 || (restoreInfo->origVid
1618 && (hostVolHeader.volumeID != restoreInfo->origVid))
1619 || (hostVolHeader.magic != TC_VOLBEGINMAGIC))
1620 ERROR_EXIT(TC_BADVOLHEADER);
1622 /* Set up prior restoring volume data */
1623 newVolName = restoreInfo->newName;
1624 newVolId = restoreInfo->vid;
1625 newServer = restoreInfo->hostAddr;
1626 newPart = restoreInfo->partition;
1628 if ((restoreInfo->dumpLevel == 0)
1629 || (restoreInfo->flags & RDFLAG_FIRSTDUMP))
1630 restoreflags |= RV_FULLRST;
1631 if (!(restoreInfo->flags & RDFLAG_LASTDUMP))
1632 restoreflags |= RV_OFFLINE;
1634 if (checkAbortByTaskId(taskId))
1635 ERROR_EXIT(TC_ABORTEDBYREQUEST);
1637 /* Start the restore of the volume data. This is the code we want to return */
1639 UV_RestoreVolume(htonl(newServer), newPart, newVolId, newVolName,
1640 restoreflags, xbsaRestoreVolumeData,
1641 (char *)rparamsPtr);
1644 rc = xbsa_ReadObjectEnd(&butxInfo);
1645 if (rc != XBSA_SUCCESS) {
1647 "Unable to terminate reading of the volume from the server\n");
1653 rc = xbsa_EndTrans(&butxInfo);
1654 if (rc != XBSA_SUCCESS) {
1655 ELog(taskId, "Unable to terminate the current transaction\n");
1661 if (restoretofile && restoretofilefd) {
1662 fclose(restoretofilefd);
1668 restoreVolume(taskId, restoreInfo, rparamsPtr)
1670 struct tc_restoreDesc *restoreInfo;
1671 struct restoreParams *rparamsPtr;
1673 afs_int32 code = 0, rc;
1674 afs_int32 newServer, newPart, newVolId;
1678 struct butm_tapeInfo *tapeInfoPtr = rparamsPtr->tapeInfoPtr;
1680 /* Check if we need a tape and prompt for one if so */
1682 (restoreInfo->initialDumpId ? restoreInfo->
1683 initialDumpId : restoreInfo->dbDumpId);
1684 if ((rparamsPtr->frag == 0)
1685 || (strcmp(restoreInfo->tapeName, rparamsPtr->mntTapeName) != 0)
1686 || (tapeID != rparamsPtr->tapeID)) {
1687 /* Unmount the previous tape */
1688 unmountTape(taskId, tapeInfoPtr);
1690 /* Remember this new tape */
1691 strcpy(rparamsPtr->mntTapeName, restoreInfo->tapeName);
1692 rparamsPtr->tapeID = tapeID;
1694 /* Mount a new tape */
1695 rc = GetRestoreTape(taskId, tapeInfoPtr, rparamsPtr->mntTapeName,
1697 ((rparamsPtr->frag == 0) ? autoQuery : 1));
1702 /* Seek to the correct spot and read the header information */
1703 rc = GetVolumeHead(taskId, tapeInfoPtr, restoreInfo->position,
1704 restoreInfo->oldName, restoreInfo->origVid);
1708 /* Set up prior restoring volume data */
1709 newVolName = restoreInfo->newName;
1710 newVolId = restoreInfo->vid;
1711 newServer = restoreInfo->hostAddr;
1712 newPart = restoreInfo->partition;
1714 if ((restoreInfo->dumpLevel == 0)
1715 || (restoreInfo->flags & RDFLAG_FIRSTDUMP))
1716 restoreflags |= RV_FULLRST;
1717 if (!(restoreInfo->flags & RDFLAG_LASTDUMP))
1718 restoreflags |= RV_OFFLINE;
1720 if (checkAbortByTaskId(taskId))
1721 ERROR_EXIT(TC_ABORTEDBYREQUEST);
1723 /* Start the restore of the volume data. This is the code we
1727 UV_RestoreVolume(htonl(newServer), newPart, newVolId, newVolName,
1728 restoreflags, restoreVolumeData, (char *)rparamsPtr);
1730 /* Read the FileEnd marker for the volume and step to next FM */
1731 rc = butm_ReadFileEnd(tapeInfoPtr);
1733 ErrorLog(0, taskId, rc, tapeInfoPtr->error,
1734 "Can't read EOF on tape\n");
1742 * created as a LWP by the server stub, <newNode> is a pointer to all
1743 * the parameters Restorer needs
1746 struct dumpNode *newNode;
1748 afs_int32 code = 0, tcode;
1751 struct butm_tapeInfo tapeInfo;
1752 struct tc_restoreDesc *Restore;
1753 struct tc_restoreDesc *RestoreDesc;
1754 struct restoreParams rparams;
1755 afs_int32 allocbufferSize;
1756 time_t startTime, endTime;
1757 afs_int32 goodrestore = 0;
1759 taskId = newNode->taskID;
1760 setStatus(taskId, DRIVE_WAIT);
1761 EnterDeviceQueue(deviceLatch);
1762 clearStatus(taskId, DRIVE_WAIT);
1765 TLog(taskId, "Restore\n");
1767 memset(&tapeInfo, 0, sizeof(tapeInfo));
1769 tapeInfo.structVersion = BUTM_MAJORVERSION;
1770 tcode = butm_file_Instantiate(&tapeInfo, &globalTapeConfig);
1772 ErrorLog(0, taskId, tcode, tapeInfo.error,
1773 "Can't initialize the tape module\n");
1778 if (checkAbortByTaskId(taskId))
1779 ERROR_EXIT(TC_ABORTEDBYREQUEST);
1781 memset(&rparams, 0, sizeof(rparams));
1782 rparams.nodePtr = newNode;
1783 rparams.tapeInfoPtr = &tapeInfo;
1784 Restore = newNode->restores; /* Array of vol fragments to restore */
1786 /* Allocate memory in which to restore the volumes data into */
1788 allocbufferSize = dataSize = BufferSize;
1790 /* Must have at least two tape blocks */
1791 tapeblocks = BufferSize / BUTM_BLOCKSIZE;
1794 allocbufferSize = tapeblocks * BUTM_BLOCKSIZE; /* This many full tapeblocks */
1797 bufferBlock = (struct TapeBlock *)malloc(allocbufferSize);
1799 ERROR_EXIT(TC_NOMEMORY);
1800 memset(bufferBlock, 0, allocbufferSize);
1802 startTime = time(0);
1803 for (rparams.frag = 0; (rparams.frag < newNode->arraySize);
1805 RestoreDesc = &Restore[rparams.frag];
1807 /* Skip the volume if it was requested to */
1808 if (RestoreDesc->flags & RDFLAG_SKIP) {
1809 if (RestoreDesc->flags & RDFLAG_LASTDUMP) {
1810 /* If the volume was restored, should bring it online */
1815 newVolName = RestoreDesc->newName;
1817 /* Make sure the server to restore to is good */
1818 if (!RestoreDesc->hostAddr) {
1819 ErrorLog(0, taskId, 0, 0, "Illegal host ID 0 for volume %s\n",
1821 ERROR_EXIT(TC_INTERNALERROR);
1824 if (checkAbortByTaskId(taskId))
1825 ERROR_EXIT(TC_ABORTEDBYREQUEST);
1827 TapeLog(1, taskId, 0, 0, "Restoring volume %s\n", newVolName);
1829 strncpy(newNode->statusNodePtr->volumeName, newVolName,
1833 /* restoreVolume function takes care of all the related fragments
1834 * spanning various tapes. On return the complete volume has been
1838 tcode = xbsaRestoreVolume(taskId, RestoreDesc, &rparams);
1840 tcode = restoreVolume(taskId, RestoreDesc, &rparams);
1843 if (tcode == TC_ABORTEDBYREQUEST) {
1845 } else if (tcode == TC_SKIPTAPE) {
1848 (RestoreDesc->initialDumpId ? RestoreDesc->
1849 initialDumpId : RestoreDesc->dbDumpId);
1850 SkipTape(Restore, newNode->arraySize, rparams.frag,
1851 RestoreDesc->tapeName, tapeID, taskId);
1853 ErrorLog(0, taskId, tcode, 0, "Can't restore volume %s\n",
1855 SkipVolume(Restore, newNode->arraySize, rparams.frag,
1856 RestoreDesc->origVid, taskId);
1868 unmountTape(taskId, &tapeInfo);
1871 code = InitToServer(taskId, &butxInfo, 0); /* Return to original server */
1878 if (code == TC_ABORTEDBYREQUEST) {
1879 ErrorLog(0, taskId, 0, 0, "Restore: Aborted by request\n");
1880 clearStatus(taskId, ABORT_REQUEST);
1881 setStatus(taskId, ABORT_DONE);
1883 TapeLog(0, taskId, code, 0, "Restore: Finished with errors\n");
1884 setStatus(taskId, TASK_ERROR);
1886 TLog(taskId, "Restore: Finished\n");
1889 if (centralLogIO && startTime) {
1891 afs_int32 hrs, min, sec, tmp;
1893 struct tm tmstart, tmend;
1895 localtime_r(&startTime, &tmstart);
1896 localtime_r(&endTime, &tmend);
1897 timediff = (int)endTime - (int)startTime;
1898 hrs = timediff / 3600;
1899 tmp = timediff % 3600;
1904 "%-5d %02d/%02d/%04d %02d:%02d:%02d "
1905 "%02d/%02d/%04d %02d:%02d:%02d " "%02d:%02d:%02d "
1906 "%d of %d volume%s restored\n", taskId, tmstart.tm_mon + 1,
1907 tmstart.tm_mday, tmstart.tm_year + 1900, tmstart.tm_hour,
1908 tmstart.tm_min, tmstart.tm_sec, tmend.tm_mon + 1,
1909 tmend.tm_mday, tmend.tm_year + 1900, tmend.tm_hour,
1910 tmend.tm_min, tmend.tm_sec, hrs, min, sec, goodrestore,
1911 newNode->arraySize, ((newNode->arraySize > 1) ? "s" : ""));
1913 fwrite(line, strlen(line), 1, centralLogIO);
1914 fflush(centralLogIO);
1917 setStatus(taskId, TASK_DONE);
1920 LeaveDeviceQueue(deviceLatch);
1924 /* this is just scaffolding, creates new tape label with name <tapeName> */
1926 GetNewLabel(tapeInfoPtr, pName, AFSName, tapeLabel)
1927 struct butm_tapeInfo *tapeInfoPtr;
1928 char *pName, *AFSName;
1929 struct butm_tapeLabel *tapeLabel;
1932 struct timezone tzp;
1935 memset(tapeLabel, 0, sizeof(struct butm_tapeLabel));
1938 butm_GetSize(tapeInfoPtr, &size);
1940 size = globalTapeConfig.capacity;
1942 size = 0; /* no tape size */
1944 gettimeofday(&tp, &tzp);
1946 tapeLabel->structVersion = CUR_TAPE_VERSION;
1947 tapeLabel->creationTime = tp.tv_sec;
1948 tapeLabel->size = size;
1949 tapeLabel->expirationDate = 0; /* 1970 sometime */
1950 tapeLabel->dumpPath[0] = 0; /* no path name */
1951 tapeLabel->useCount = 0;
1952 strcpy(tapeLabel->AFSName, AFSName);
1953 strcpy(tapeLabel->pName, pName);
1954 strcpy(tapeLabel->cell, globalCellName);
1955 strcpy(tapeLabel->comment, "AFS Backup Software");
1956 strcpy(tapeLabel->creator.name, "AFS 3.6");
1957 strcpy(tapeLabel->creator.instance, "");
1958 strcpy(tapeLabel->creator.cell, globalCellName);
1961 /* extracts trailer out of buffer, nbytes is set to total data in buffer - trailer size */
1963 ExtractTrailer(buffer, size, nbytes, volTrailerPtr)
1967 struct volumeHeader *volTrailerPtr;
1971 struct volumeHeader tempTrailer;
1975 (size - sizeof(struct volumeHeader) + sizeof(tempTrailer.pad));
1977 code = readVolumeHeader(buffer, startPos, &tempTrailer);
1979 code = VolHeaderToHost(volTrailerPtr, &tempTrailer);
1985 return 1; /* saw the trailer */
1991 return 0; /* did not see the trailer */
1995 FindVolTrailer(buffer, size, dSize, volTrailerPtr)
1998 struct volumeHeader *volTrailerPtr;
1999 afs_int32 *dSize; /* dataSize */
2001 afs_int32 offset, s;
2008 s = sizeof(struct volumeHeader) + sizeof(volTrailerPtr->pad);
2012 found = ExtractTrailer((buffer + size - s), s, &offset, volTrailerPtr);
2014 *dSize -= (s - offset);
2019 FindVolTrailer2(buffera, sizea, dataSizea, bufferb, sizeb, dataSizeb,
2023 afs_int32 *dataSizea;
2026 afs_int32 *dataSizeb;
2027 struct volumeHeader *volTrailerPtr;
2029 afs_int32 offset, s;
2030 afs_int32 headB, tailB;
2040 s = sizeof(struct volumeHeader) + sizeof(volTrailerPtr->pad);
2042 found = FindVolTrailer(bufferb, sizeb, dataSizeb, volTrailerPtr);
2045 headB = (s - sizeb); /*(s > sizeb) */
2046 if (headB > sizea) {
2053 memset(tapeVolumeHT, 0, sizeof(tapeVolumeHT));
2055 memcpy(tapeVolumeHT, buffera + sizea - headB, headB);
2057 memcpy(tapeVolumeHT + headB, bufferb, tailB);
2058 if (ExtractTrailer(tapeVolumeHT, s, &offset, volTrailerPtr)) {
2060 if (offset > headB) {
2061 /* *dataSizea remains unchanged */
2062 *dataSizeb = offset - headB;
2064 *dataSizea -= (headB - offset); /*(headB >= offset) */
2072 /* Returns true or false depending on whether the tape is expired or not */
2075 ExpirationDate(dumpid)
2079 Date expiration = 0;
2080 struct budb_dumpEntry dumpEntry;
2081 struct budb_tapeEntry tapeEntry;
2082 struct budb_volumeEntry volEntry;
2086 * Get the expiration date from DB if its there. The expiration of any tape
2087 * will be the most future expiration of any dump in the set. Can't use
2088 * bcdb_FindTape because dumpid here pertains to the initial dump id.
2090 code = bcdb_FindLastTape(dumpid, &dumpEntry, &tapeEntry, &volEntry);
2092 expiration = tapeEntry.expires;
2094 return (expiration);
2098 tapeExpired(tapeLabelPtr)
2099 struct butm_tapeLabel *tapeLabelPtr;
2103 struct timezone tzp;
2105 expiration = ExpirationDate(tapeLabelPtr->dumpid);
2107 expiration = tapeLabelPtr->expirationDate;
2109 gettimeofday(&tp, &tzp);
2110 return ((expiration < tp.tv_sec) ? 1 : 0);
2114 * given the label on the tape, delete any old information from the
2117 * Deletes all entries that match the volset.dumpnode
2118 * and the dump path.
2121 updateTapeLabel(labelIfPtr, tapeInfoPtr, newLabelPtr)
2122 struct labelTapeIf *labelIfPtr;
2123 struct butm_tapeInfo *tapeInfoPtr;
2124 struct butm_tapeLabel *newLabelPtr;
2126 struct butm_tapeLabel oldLabel;
2127 afs_int32 i, code = 0;
2129 int tapeIsLabeled = 0;
2130 int interactiveFlag;
2133 interactiveFlag = autoQuery;
2134 taskId = labelIfPtr->taskId;
2137 if (interactiveFlag) {
2139 PromptForTape(LABELOPCODE, TNAME(newLabelPtr), 0,
2140 labelIfPtr->taskId, tapecount);
2144 interactiveFlag = 1;
2147 /* mount the tape */
2148 code = butm_Mount(tapeInfoPtr, newLabelPtr->AFSName);
2150 TapeLog(0, taskId, code, tapeInfoPtr->error, "Can't open tape\n");
2154 code = butm_ReadLabel(tapeInfoPtr, &oldLabel, 1); /* will rewind the tape */
2158 if ((strcmp(newLabelPtr->AFSName, "") != 0)
2159 && (strcmp(oldLabel.pName, "") != 0)) {
2160 /* We are setting the AFS name, yet tape
2161 * has a permanent name (not allowed).
2163 TLog(taskId, "Can't label. Tape has permanent label '%s'\n",
2168 if (!tapeExpired(&oldLabel)) {
2169 if (!queryoperator) {
2170 TLog(taskId, "This tape has not expired\n");
2173 if (Ask("This tape has not expired - proceed") == 0)
2177 /* Keep the permanent name */
2178 if (strcmp(newLabelPtr->pName, "") == 0) {
2179 strcpy(newLabelPtr->pName, oldLabel.pName);
2180 } else if (strcmp(newLabelPtr->pName, TC_NULLTAPENAME) == 0) {
2181 strcpy(newLabelPtr->pName, "");
2185 /* extract useful information from the old label */
2186 if (tapeIsLabeled && oldLabel.structVersion >= TAPE_VERSION_3) {
2187 newLabelPtr->dumpid = 0;
2188 newLabelPtr->useCount = oldLabel.useCount + 1;
2191 /* now write the new label */
2192 code = butm_Create(tapeInfoPtr, newLabelPtr, 1); /* will rewind the tape */
2194 ErrorLog(0, taskId, code, tapeInfoPtr->error,
2195 "Can't label tape\n");
2202 unmountTape(taskId, tapeInfoPtr);
2205 /* delete obsolete information from the database */
2206 if (tapeIsLabeled && oldLabel.structVersion >= TAPE_VERSION_3) {
2207 /* delete based on dump id */
2208 if (oldLabel.dumpid) {
2209 i = bcdb_deleteDump(oldLabel.dumpid, 0, 0, 0);
2210 if (i && (i != BUDB_NOENT))
2211 ErrorLog(0, taskId, i, 0,
2212 "Warning: Can't delete old dump %u from database\n",
2218 unmountTape(taskId, tapeInfoPtr);
2223 * LWP created by the server stub. Labels the tape with name and size
2224 * specified by <label>
2227 Labeller(labelIfPtr)
2228 struct labelTapeIf *labelIfPtr;
2230 struct tc_tapeLabel *label = &labelIfPtr->label;
2232 struct butm_tapeLabel newTapeLabel;
2233 struct butm_tapeInfo tapeInfo;
2237 taskId = labelIfPtr->taskId;
2238 setStatus(taskId, DRIVE_WAIT);
2239 EnterDeviceQueue(deviceLatch);
2240 clearStatus(taskId, DRIVE_WAIT);
2243 TLog(taskId, "Labeltape\n");
2245 memset(&tapeInfo, 0, sizeof(tapeInfo));
2246 tapeInfo.structVersion = BUTM_MAJORVERSION;
2247 code = butm_file_Instantiate(&tapeInfo, &globalTapeConfig);
2249 ErrorLog(0, taskId, code, tapeInfo.error,
2250 "Can't initialize the tape module\n");
2254 GetNewLabel(&tapeInfo, label->pname, label->afsname, &newTapeLabel);
2256 newTapeLabel.size = label->size;
2258 newTapeLabel.size = globalTapeConfig.capacity;
2260 code = updateTapeLabel(labelIfPtr, &tapeInfo, &newTapeLabel);
2265 if (code == TC_ABORTEDBYREQUEST) {
2266 ErrorLog(0, taskId, 0, 0, "Labeltape: Aborted by request\n");
2267 clearStatus(taskId, ABORT_REQUEST);
2268 setStatus(taskId, ABORT_DONE);
2270 ErrorLog(0, taskId, code, 0, "Labeltape: Finished with errors\n");
2271 setStatus(taskId, TASK_ERROR);
2273 TLog(taskId, "Labelled tape %s size %u Kbytes\n",
2274 TNAME(&newTapeLabel), newTapeLabel.size);
2276 setStatus(labelIfPtr->taskId, TASK_DONE);
2279 LeaveDeviceQueue(deviceLatch);
2284 * print out the tape label.
2287 PrintTapeLabel(labelptr)
2288 struct butm_tapeLabel *labelptr;
2290 char tapeName[BU_MAXTAPELEN + 32];
2293 printf("Tape label\n");
2294 printf("----------\n");
2295 TAPENAME(tapeName, labelptr->pName, labelptr->dumpid);
2296 printf("permanent tape name = %s\n", tapeName);
2297 TAPENAME(tapeName, labelptr->AFSName, labelptr->dumpid);
2298 printf("AFS tape name = %s\n", tapeName);
2299 t = labelptr->creationTime;
2300 printf("creationTime = %s", ctime(&t));
2301 if (labelptr->expirationDate) {
2302 t = labelptr->expirationDate;
2303 printf("expirationDate = %s", cTIME(&t));
2305 printf("cell = %s\n", labelptr->cell);
2306 printf("size = %u Kbytes\n", labelptr->size);
2307 printf("dump path = %s\n", labelptr->dumpPath);
2309 if (labelptr->structVersion >= TAPE_VERSION_3) {
2310 printf("dump id = %u\n", labelptr->dumpid);
2311 printf("useCount = %d\n", labelptr->useCount);
2313 printf("-- End of tape label --\n\n");
2317 * Read the label from a tape.
2318 * Currently prints out a "detailed" summary of the label but passes
2319 * back only selected fields.
2323 struct tc_tapeLabel *label;
2325 struct butm_tapeLabel newTapeLabel;
2326 struct butm_tapeInfo tapeInfo;
2330 int interactiveFlag;
2333 EnterDeviceQueue(deviceLatch);
2334 taskId = allocTaskId(); /* reqd for lower level rtns */
2337 TLog(taskId, "Readlabel\n");
2339 memset(&tapeInfo, 0, sizeof(tapeInfo));
2340 tapeInfo.structVersion = BUTM_MAJORVERSION;
2341 code = butm_file_Instantiate(&tapeInfo, &globalTapeConfig);
2343 ErrorLog(0, taskId, code, tapeInfo.error,
2344 "Can't initialize the tape module\n");
2347 memset(&newTapeLabel, 0, sizeof(newTapeLabel));
2349 interactiveFlag = autoQuery;
2352 if (interactiveFlag) {
2353 code = PromptForTape(READLABELOPCODE, "", 0, taskId, tapecount);
2357 interactiveFlag = 1;
2360 code = butm_Mount(&tapeInfo, "");
2362 TapeLog(0, taskId, code, tapeInfo.error, "Can't open tape\n");
2368 unmountTape(taskId, &tapeInfo);
2371 code = butm_ReadLabel(&tapeInfo, &newTapeLabel, 1); /* will rewind the tape */
2373 if (code == BUTM_NOLABEL) {
2374 printf("Tape is unlabelled\n");
2377 ErrorLog(1, taskId, code, tapeInfo.error, "Can't read tape label\n");
2381 /* copy the fields to be passed to the caller */
2382 label->size = newTapeLabel.size;
2383 label->tapeId = newTapeLabel.dumpid;
2384 strcpy(label->afsname, newTapeLabel.AFSName);
2385 strcpy(label->pname, newTapeLabel.pName);
2388 expir = ExpirationDate(newTapeLabel.dumpid);
2390 newTapeLabel.expirationDate = expir;
2392 PrintTapeLabel(&newTapeLabel);
2395 unmountTape(taskId, &tapeInfo);
2397 if (code == TC_ABORTEDBYREQUEST)
2398 ErrorLog(0, taskId, 0, 0, "ReadLabel: Aborted by request\n");
2399 else if (code && (code != BUTM_NOLABEL))
2400 ErrorLog(0, taskId, code, 0, "ReadLabel: Finished with errors\n");
2402 TLog(taskId, "ReadLabel: Finished\n");
2404 LeaveDeviceQueue(deviceLatch);
2408 /* Function to read volume header and trailer structure from tape, taking
2409 into consideration, different word alignment rules.
2412 readVolumeHeader(buffer, bufloc, header)
2414 char *buffer; /* buffer to read header from */
2415 /*in */ afs_int32 bufloc;
2416 /* header's location in buffer */
2417 /*out */ struct volumeHeader *header;
2418 /* header structure */
2421 struct volumeHeader vhptr, *tempvhptr;
2422 afs_int32 firstSplice = (afs_int32) ((char*)& vhptr.pad - (char*) & vhptr);
2423 int padLen = sizeof(vhptr.pad); /* pad to achieve 4 byte alignment */
2424 int nextSplice = sizeof(struct volumeHeader) - firstSplice - padLen;
2426 /* Four cases are to be handled
2428 * Volume Header (byte alignment)
2429 * -----------------------
2436 * -----------------------
2438 * Case 2 and Case 3 are identical cases and handled the same way.
2439 * Case 1 and Case 4 are separate cases. In one case the pad needs
2440 * to be removed and in the other, it needs to be spliced in. The
2441 * four cases are handled as follows
2443 tempvhptr = (struct volumeHeader *)(buffer + bufloc);
2444 if ((strncmp(tempvhptr->preamble, "H++NAME#", 8) == 0)
2445 && (strncmp(tempvhptr->postamble, "T--NAME#", 8) == 0)) {
2446 /* Handle Cases 2 & 3 */
2447 memcpy(&vhptr, buffer + bufloc, sizeof(struct volumeHeader));
2448 HEADER_CHECKS(vhptr, header);
2451 memset(&vhptr, 0, sizeof(struct volumeHeader));
2452 memcpy(&vhptr, buffer + bufloc, firstSplice);
2453 memset(&vhptr.pad, 0, padLen);
2454 memcpy(&vhptr.volumeID, buffer + bufloc + firstSplice, nextSplice);
2455 HEADER_CHECKS(vhptr, header);
2458 memset(&vhptr, 0, sizeof(struct volumeHeader));
2459 memcpy(&vhptr, buffer + bufloc, firstSplice);
2460 memcpy(&vhptr + firstSplice, buffer + bufloc + firstSplice + padLen,
2462 HEADER_CHECKS(vhptr, header);
2465 return (TC_BADVOLHEADER);