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
14 #include <afsconfig.h>
15 #include <afs/param.h>
21 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
32 #include <afs/bubasics.h>
34 #include <afs/com_err.h>
37 #include <afs/vlserver.h>
38 #include "error_macros.h"
40 extern struct bc_dumpTask bc_dumpTasks[BC_MAXSIMDUMPS];
43 #define MAXTAPESATONCE 10
45 #define HOSTADDR(sockaddr) (sockaddr)->sin_addr.s_addr
47 /* local structure to keep track of volumes and the dumps from which
48 * they should be restored
51 struct dumpinfo *next;
52 struct volinfo *volinfolist;
53 struct volinfo *lastinlist;
55 afs_int32 initialDumpId;
56 afs_int32 parentDumpId;
67 /* local structure used to keep track of a tape, including a list of
68 * volumes to restore from that tape (bc_tapeItem)
71 struct bc_tapeList *next; /* next guy in list */
72 char *tapeName; /* name of the tape */
73 afs_int32 dumpID; /* dump located on this tape */
74 afs_int32 initialDumpID; /* initial dump located on this tape */
75 afs_int32 tapeNumber; /* which tape in the dump */
76 struct bc_tapeItem *restoreList; /* volumes to restore from this tape */
79 /* each tape has a list of volumes to restore; hangs off of a struct
80 bc_tapeList. Kept sorted so that we can read the tape once to do
81 everything we need to do. */
83 struct bc_tapeItem *next;
84 char *volumeName; /* volume to restore */
85 afs_int32 position; /* file slot on this tape */
86 afs_int32 oid; /* id of the volume */
87 afs_int32 dumplevel; /* level # of the containing dump (0 == top level) */
88 afs_int32 vollevel; /* level # of the containing volume (0 == full dump) */
91 int lastdump; /* The last incremental to restore */
94 /* strip .backup from the end of a name */
101 if (j = BackupName(aname)) {
115 if ((j > 7) && (strcmp(".backup", aname + j - 7) == 0))
117 if ((j > 9) && (strcmp(".readonly", aname + j - 9) == 0))
122 extractTapeSeq(tapename)
127 sptr = strrchr(tapename, '.');
140 alph = "abcdefjhijklmnopqrstuvwxyz";
143 viceName(value / 26 - 1);
146 printf("%c", alph[r]);
151 * aindex - index into bc_dumpTasks that describes this dump.
156 struct bc_dumpTask *dumpTaskPtr;
158 char vname[BU_MAXNAMELEN];
159 struct budb_dumpEntry *dumpDescr, dumpDescr1, dumpDescr2;
160 struct budb_volumeEntry *volumeEntries;
161 struct bc_volumeDump *tvol;
162 afs_int32 code = 0, tcode;
163 afs_int32 tapedumpid, parent;
165 afs_int32 nentries = 0;
166 afs_int32 last, next, ve, vecount;
167 struct bc_tapeItem *ti, *pti, *nti;
168 struct bc_tapeList *tapeList = (struct bc_tapeList *)0;
169 struct bc_tapeList *tle, *ptle, *ntle;
171 afs_int32 tapeid, tid;
173 struct tc_restoreArray rpcArray; /* the rpc structure we use */
174 struct tc_restoreDesc *tcarray = (struct tc_restoreDesc *)0;
176 afs_int32 i, startentry, todump;
177 afs_int32 port, nextport;
178 afs_int32 level, tapeseq;
179 afs_int32 foundvolume, voldumplevel;
180 struct rx_connection *aconn = (struct rx_connection *)0;
181 statusP statusPtr, newStatusPtr;
183 struct dumpinfo *dumpinfolist = NULL;
184 struct dumpinfo *pdi, *ndi, *di, *dlevels;
185 struct volinfo *pvi, *nvi, *vi;
187 int num_dlevels = 20;
189 afs_int32 serverAll; /* The server to which all volumes are to be restore to */
190 afs_int32 partitionAll; /* Likewise for partition */
191 struct hostent *hostPtr;
196 extern statusP createStatusNode();
197 extern statusP findStatus();
199 dlevels = (struct dumpinfo *) malloc(num_dlevels * sizeof(*dlevels));
201 dumpTaskPtr = &bc_dumpTasks[aindex];
202 serverAll = HOSTADDR(&dumpTaskPtr->destServer);
203 partitionAll = dumpTaskPtr->destPartition;
205 volumeEntries = (struct budb_volumeEntry *)
206 malloc(MAXTAPESATONCE * sizeof(struct budb_volumeEntry));
207 if (!volumeEntries) {
208 afs_com_err(whoami, BC_NOMEM, "");
212 /* For each volume to restore, find which dump it's most recent full or
213 * incremental is on and thread onto our dump list (from oldest to newest
214 * dump). Also hang the volume off of the dump (no particular order).
216 for (tvol = dumpTaskPtr->volumes; tvol; tvol = tvol->next) { /*tvol */
217 strcpy(vname, tvol->name);
218 dumpDescr = &dumpDescr1;
219 if (dumpTaskPtr->parentDumpID > 0) /* Told which dump to try */
221 /* Right now, this assumes that all volumes listed will be
222 * from the given dumpID. FIXME
224 code = bcdb_FindDumpByID(dumpTaskPtr->parentDumpID, dumpDescr);
227 afs_com_err(whoami, "Couldn't look up info for dump %d\n",
228 dumpTaskPtr->parentDumpID);
231 code = bcdb_FindVolumes(dumpTaskPtr->parentDumpID, vname, volumeEntries,
232 last, &next, MAXTAPESATONCE, &vecount);
235 if (!BackupName(vname))
237 strcat(vname, ".backup");
238 code = bcdb_FindVolumes(dumpTaskPtr->parentDumpID, vname, volumeEntries,
239 last, &next, MAXTAPESATONCE, &vecount);
245 code = bcdb_FindDump(vname, dumpTaskPtr->fromDate, dumpDescr);
246 if (!BackupName(vname)) { /* See if backup volume is there */
247 strcat(vname, ".backup");
248 dumpDescr = &dumpDescr2;
250 code = bcdb_FindDump(vname, dumpTaskPtr->fromDate, dumpDescr);
252 if (code) { /* Can't find backup, go with first results */
253 strcpy(vname, tvol->name);
254 dumpDescr = &dumpDescr1;
256 } else if (!tcode) { /* Both found an entry, go with latest result */
257 if (dumpDescr1.created > dumpDescr2.created) {
258 strcpy(vname, tvol->name);
259 dumpDescr = &dumpDescr1;
266 if (code) { /* If FindDump took an error */
267 afs_com_err(whoami, code, "; Can't find any dump for volume %s",
272 /* look to see if this dump has already been found */
273 for (pdi = 0, di = dumpinfolist; di; pdi = di, di = di->next) {
274 if (di->DumpId < dumpDescr->id) {
277 } else if (di->DumpId == dumpDescr->id) {
282 /* If didn't find it, create one and thread into list */
284 di = (struct dumpinfo *)malloc(sizeof(struct dumpinfo));
286 afs_com_err(whoami, BC_NOMEM, "");
289 memset(di, 0, sizeof(struct dumpinfo));
291 di->DumpId = dumpDescr->id;
292 di->initialDumpId = dumpDescr->initialDumpID;
293 di->parentDumpId = dumpDescr->parent;
294 di->level = dumpDescr->level;
297 di->next = dumpinfolist;
300 di->next = pdi->next;
305 /* Create one and thread into list */
306 vi = (struct volinfo *)malloc(sizeof(struct volinfo));
308 afs_com_err(whoami, BC_NOMEM, "");
311 memset(vi, 0, sizeof(struct volinfo));
313 vi->volname = (char *)malloc(strlen(vname) + 1);
316 afs_com_err(whoami, BC_NOMEM, "");
320 strcpy(vi->volname, vname);
322 vi->server = serverAll;
323 vi->partition = partitionAll;
325 vi->server = HOSTADDR(&tvol->server);
326 vi->partition = tvol->partition;
329 /* thread onto end of list */
331 di->volinfolist = vi;
333 di->lastinlist->next = vi;
337 /* For each of the above dumps we found (they could be increments), find
338 * the dump's lineage (up to the full dump).
340 for (di = dumpinfolist; di; di = di->next) {
341 /* Find each of the parent dumps */
342 memcpy(&dlevels[0], di, sizeof(struct dumpinfo));
343 for (lvl = 1, parent = dlevels[0].parentDumpId; parent;
344 parent = dlevels[lvl].parentDumpId, lvl++) {
345 if (lvl >= num_dlevels) { /* running out of dump levels */
346 struct dumpinfo *tdl = dlevels;
348 num_dlevels += num_dlevels; /* double */
349 dlevels = (struct dumpinfo *) malloc(num_dlevels * sizeof(*dlevels));
350 memcpy(dlevels, tdl, (num_dlevels/2) * sizeof(*dlevels));
353 code = bcdb_FindDumpByID(parent, &dumpDescr1);
355 for (vi = di->volinfolist; vi; vi = vi->next) {
356 afs_com_err(whoami, code,
357 "; Can't find parent DumpID %u for volume %s",
358 parent, vi->volname);
363 dlevels[lvl].DumpId = dumpDescr1.id;
364 dlevels[lvl].initialDumpId = dumpDescr1.initialDumpID;
365 dlevels[lvl].parentDumpId = dumpDescr1.parent;
366 dlevels[lvl].level = dumpDescr1.level;
369 /* For each of the volumes that has a dump in this lineage (vi),
370 * find where it is in each dump level (lv) starting at level 0 and
371 * going to higest. Each dump level could contain one or more
372 * fragments (vecount) of the volume (volume fragments span tapes).
373 * Each volume fragment is sorted by tapeid, tape sequence, and tape
376 for (vi = di->volinfolist; vi; vi = vi->next) {
377 tle = tapeList; /* Use for searching the tapelist */
378 ptle = 0; /* The previous tape list entry */
379 tlid = 0; /* tapeid of first entry */
380 /* Volume's dump-level. May not be same as dump's dump-level. This
381 * value gets incremented everytime the volume is found on a dump.
385 /* Do from level 0 dump to highest level dump */
386 for (lv = (lvl - 1); lv >= 0; lv--) {
387 foundvolume = 0; /* If found volume on this dump */
389 (dlevels[lv].initialDumpId ? dlevels[lv].
390 initialDumpId : dlevels[lv].DumpId);
392 /* Cycle through the volume fragments for this volume on the tape */
393 for (last = 0, c = 0; last >= 0; last = next, c++) {
395 bcdb_FindVolumes(dlevels[lv].DumpId, vi->volname,
396 volumeEntries, last, &next,
397 MAXTAPESATONCE, &vecount);
399 /* Volume wasn't found on the tape - so continue.
400 * This can happen when volume doesn't exist during level 0 dump
401 * but exists for incremental dump.
403 if (code == BUDB_ENDOFLIST) {
408 afs_com_err(whoami, code,
409 "; Can't find volume %s in DumpID %u",
410 vi->volname, dlevels[lv].DumpId);
414 /* If we have made the Findvolumes call more than once for the same
415 * volume, then begin the search at the top. This sort assumes that
416 * we are collecting information from lowest level dump (full dump)
417 * to the highest level dump (highest incremental)
418 * and from first fragment to last fragment at each level. If
419 * there is more than one call to FindVolumes, this is not true
428 /* For each of the volume fragments that we read, sort them into
429 * the list of tapes they are on. Sort by tapeid, then tape sequence.
430 * Do from first fragment (where volume begins) to last fragment.
432 for (ve = (vecount - 1); ve >= 0; ve--) {
433 foundvolume = 1; /* Found the volume on this dump */
435 tapeseq = volumeEntries[ve].tapeSeq;
437 /* Determine where in the list of tapes this should go */
438 for (; tle; ptle = tle, tle = tle->next) {
440 (tle->initialDumpID ? tle->
441 initialDumpID : tle->dumpID);
443 /* Sort by tapeids. BUT, we don't want add an entry in the middle
444 * of a dumpset (might split a volume fragmented across tapes).
445 * So make sure we step to next dumpset. tlid is the tapeid of
446 * the last tape we added a volume to. This can happen when an
447 * incremental was appended to a tape created prior its parent-
448 * dump's tape, and needs to be restored after it.
454 } /* Allocate and insert a tape entry */
457 /* Found the tapeid (the dumpset). Check if its the correct
460 else if (tapeid == tid) {
461 if (tapeseq < tle->tapeNumber) {
465 /* Allocate and insert a tape entry */
466 if (tapeseq == tle->tapeNumber) {
469 /* Don't allocate tape entry */
470 foundtape = 1; /* Found dumpset but not the tape */
473 /* Prevously found the tapeid (the dumpset) but this tape
474 * sequence not included (the tapeid has changed). So add
475 * this tape entry to the end of its dumpset.
477 else if (foundtape) {
481 /* Allocate and insert a tape entry */
484 /* Allocate a new tapelist entry if one not found */
486 tle = (struct bc_tapeList *)
487 malloc(sizeof(struct bc_tapeList));
489 afs_com_err(whoami, BC_NOMEM, "");
492 memset(tle, 0, sizeof(struct bc_tapeList));
495 (char *)malloc(strlen(volumeEntries[ve].tape)
497 if (!tle->tapeName) {
499 afs_com_err(whoami, BC_NOMEM, "");
503 strcpy(tle->tapeName, volumeEntries[ve].tape);
504 tle->dumpID = dlevels[lv].DumpId;
505 tle->initialDumpID = dlevels[lv].initialDumpId;
506 tle->tapeNumber = tapeseq;
508 /* Now thread the tape onto the list */
510 tle->next = tapeList;
513 tle->next = ptle->next;
518 (tle->initialDumpID ? tle->initialDumpID : tle->
521 /* Now place the volume fragment into the correct position on
522 * this tapelist entry. Duplicate entries are ignored.
524 for (pti = 0, ti = tle->restoreList; ti;
525 pti = ti, ti = ti->next) {
526 if (volumeEntries[ve].position < ti->position) {
530 /* Allocate an entry */
531 else if (volumeEntries[ve].position ==
534 } /* Duplicate entry */
537 /* If didn't find one, allocate and thread onto list.
538 * Remember the server and partition.
541 ti = (struct bc_tapeItem *)
542 malloc(sizeof(struct bc_tapeItem));
544 afs_com_err(whoami, BC_NOMEM, "");
547 memset(ti, 0, sizeof(struct bc_tapeItem));
550 (char *)malloc(strlen(volumeEntries[ve].name)
552 if (!ti->volumeName) {
554 afs_com_err(whoami, BC_NOMEM, "");
558 strcpy(ti->volumeName, volumeEntries[ve].name);
559 ti->server = vi->server;
560 ti->partition = vi->partition;
561 ti->oid = volumeEntries[ve].id;
562 ti->position = volumeEntries[ve].position;
563 ti->vollevel = voldumplevel;
564 ti->dumplevel = dlevels[lv].level;
565 ti->lastdump = ((lv == 0) ? 1 : 0);
568 ti->next = tle->restoreList;
569 tle->restoreList = ti;
571 ti->next = pti->next;
577 } /* ve: for each returned volume fragment by bcdb_FindVolumes() */
578 } /* last: Multiple calls to bcdb_FindVolumes() */
582 } /* lv: For each dump level */
583 } /* vi: For each volume to search for in the dump hierarchy */
584 } /* di: For each dump */
587 afs_com_err(whoami, 0, "No volumes to restore");
591 if (dumpTaskPtr->dontExecute) {
592 for (tle = tapeList; tle; tle = tle->next) {
593 for (ti = tle->restoreList; ti; ti = ti->next) {
595 (tle->initialDumpID ? tle->initialDumpID : tle->dumpID);
596 strcpy(vname, ti->volumeName);
598 if (dumpTaskPtr->newExt)
599 strcat(vname, dumpTaskPtr->newExt);
601 /* print volume to restore and the tape and position its on */
604 ("Restore volume %s (ID %d) from tape %s (%u), position %d",
605 ti->volumeName, ti->oid, tle->tapeName, tapedumpid,
608 if (strcmp(ti->volumeName, vname) != 0)
609 printf(" as %s", vname);
614 /* print in format recognized by volsetrestore */
618 gethostbyaddr((char *)&haddr, sizeof(haddr), AF_INET);
620 printf("%s", hostPtr->h_name);
622 printf("%u", ti->server);
625 viceName(ti->partition);
627 printf(" %s # as %s; %s (%u); pos %d;", ti->volumeName,
628 vname, tle->tapeName, tapedumpid, ti->position);
631 printf(" %s", ctime(&did));
639 /* Allocate a list of volumes to restore */
641 (struct tc_restoreDesc *)malloc(nentries *
642 sizeof(struct tc_restoreDesc));
644 afs_com_err(whoami, BC_NOMEM, "");
647 memset(tcarray, 0, nentries * sizeof(struct tc_restoreDesc));
649 /* Fill in the array with the list above */
651 for (tle = tapeList; tle; tle = tle->next) {
652 for (ti = tle->restoreList; ti; ti = ti->next) {
653 tcarray[i].origVid = ti->oid; /* means unknown */
655 tcarray[i].flags = 0;
656 if (ti->vollevel == 0)
657 tcarray[i].flags |= RDFLAG_FIRSTDUMP;
659 tcarray[i].flags |= RDFLAG_LASTDUMP;
660 tcarray[i].position = ti->position;
661 tcarray[i].dbDumpId = tle->dumpID;
662 tcarray[i].initialDumpId = tle->initialDumpID;
663 tcarray[i].hostAddr = ti->server; /* just the internet address */
664 tcarray[i].partition = ti->partition;
666 strcpy(tcarray[i].tapeName, tle->tapeName);
667 strcpy(tcarray[i].oldName, ti->volumeName);
668 strcpy(tcarray[i].newName, ti->volumeName);
669 StripBackup(tcarray[i].newName);
670 if (dumpTaskPtr->newExt)
671 strcat(tcarray[i].newName, dumpTaskPtr->newExt);
673 tcarray[i].dumpLevel = ti->dumplevel;
678 printf("Starting restore\n");
680 /* Loop until all of the tape entries are used */
681 for (startentry = 0; startentry < nentries; startentry += todump) {
682 /* Get all the next tape entries with the same port offset */
683 if (dumpTaskPtr->portCount == 0) {
685 todump = nentries - startentry;
686 } else if (dumpTaskPtr->portCount == 1) {
687 port = dumpTaskPtr->portOffset[0];
688 todump = nentries - startentry;
690 level = tcarray[startentry].dumpLevel;
694 dumpTaskPtr->portCount -
695 1 ? level : dumpTaskPtr->portCount - 1)];
697 for (todump = 1; ((startentry + todump) < nentries); todump++) {
698 level = tcarray[startentry + todump].dumpLevel;
702 dumpTaskPtr->portCount -
703 1 ? level : dumpTaskPtr->portCount - 1)];
704 if (port != nextport)
705 break; /* break if we change ports */
709 rpcArray.tc_restoreArray_len = todump;
710 rpcArray.tc_restoreArray_val = &tcarray[startentry];
712 code = ConnectButc(dumpTaskPtr->config, port, &aconn);
716 if (tcarray[startentry].dumpLevel == 0)
717 printf("\nFull restore being processed on port %d\n", port);
719 printf("\nIncremental restore being processed on port %d\n",
723 TC_PerformRestore(aconn, "DumpSetName", &rpcArray,
724 &dumpTaskPtr->dumpID);
726 afs_com_err(whoami, code, "; Failed to start restore");
730 /* setup status monitor node */
731 statusPtr = createStatusNode();
733 statusPtr->port = port;
734 statusPtr->taskId = dumpTaskPtr->dumpID;
735 statusPtr->jobNumber = bc_jobNumber();
736 statusPtr->flags &= ~STARTING; /* clearStatus to examine */
737 if (tcarray[startentry].dumpLevel == 0)
738 sprintf(statusPtr->taskName, "Full Restore");
740 sprintf(statusPtr->taskName, "Incremental Restore");
743 /* Wait until this restore pass is complete before starting the next
744 * pass. Query the statusWatcher for the status
748 newStatusPtr = findStatus(dumpTaskPtr->dumpID);
751 flags & (ABORT_REQUEST | ABORT_SENT | ABORT_DONE |
755 } else if (newStatusPtr->flags & (TASK_DONE)) {
765 rx_DestroyConnection(aconn);
766 aconn = (struct rx_connection *)0;
769 /* free up everything */
772 rx_DestroyConnection(aconn);
777 /* Free the dumpinfo list and its volumes */
778 for (di = dumpinfolist; di; di = ndi) {
779 for (vi = di->volinfolist; vi; vi = nvi) {
789 /* Free the tape lists and items */
790 for (tle = tapeList; tle; tle = ntle) {
791 for (ti = tle->restoreList; ti; ti = nti) {
794 free(ti->volumeName);
803 /* free local-like things we alloacted to save stack space */