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, '.');
141 alph = "abcdefjhijklmnopqrstuvwxyz";
144 viceName(value / 26 - 1);
147 printf("%c", alph[r]);
152 * aindex - index into bc_dumpTasks that describes this dump.
157 struct bc_dumpTask *dumpTaskPtr;
159 char vname[BU_MAXNAMELEN];
160 struct budb_dumpEntry *dumpDescr, dumpDescr1, dumpDescr2;
161 struct budb_volumeEntry *volumeEntries;
162 struct bc_volumeDump *tvol;
163 afs_int32 code = 0, tcode;
164 afs_int32 tapedumpid, parent;
166 afs_int32 nentries = 0;
167 afs_int32 last, next, ve, vecount;
168 struct bc_tapeItem *ti, *pti, *nti;
169 struct bc_tapeList *tapeList = (struct bc_tapeList *)0;
170 struct bc_tapeList *tle, *ptle, *ntle;
172 afs_int32 tapeid, tid;
174 struct tc_restoreArray rpcArray; /* the rpc structure we use */
175 struct tc_restoreDesc *tcarray = (struct tc_restoreDesc *)0;
177 afs_int32 i, startentry, todump;
178 afs_int32 port, nextport;
179 afs_int32 level, tapeseq;
180 afs_int32 foundvolume, voldumplevel;
181 struct rx_connection *aconn = (struct rx_connection *)0;
182 statusP statusPtr, newStatusPtr;
184 struct dumpinfo *dumpinfolist = NULL;
185 struct dumpinfo *pdi, *ndi, *di, *dlevels;
186 struct volinfo *pvi, *nvi, *vi;
188 int num_dlevels = 20;
190 afs_int32 serverAll; /* The server to which all volumes are to be restore to */
191 afs_int32 partitionAll; /* Likewise for partition */
192 struct hostent *hostPtr;
197 extern statusP createStatusNode();
198 extern statusP findStatus();
200 dlevels = (struct dumpinfo *) malloc(num_dlevels * sizeof(*dlevels));
202 dumpTaskPtr = &bc_dumpTasks[aindex];
203 serverAll = HOSTADDR(&dumpTaskPtr->destServer);
204 partitionAll = dumpTaskPtr->destPartition;
206 volumeEntries = (struct budb_volumeEntry *)
207 malloc(MAXTAPESATONCE * sizeof(struct budb_volumeEntry));
208 if (!volumeEntries) {
209 afs_com_err(whoami, BC_NOMEM, "");
213 /* For each volume to restore, find which dump it's most recent full or
214 * incremental is on and thread onto our dump list (from oldest to newest
215 * dump). Also hang the volume off of the dump (no particular order).
217 for (tvol = dumpTaskPtr->volumes; tvol; tvol = tvol->next) { /*tvol */
218 strcpy(vname, tvol->name);
219 dumpDescr = &dumpDescr1;
220 if (dumpTaskPtr->parentDumpID > 0) /* Told which dump to try */
222 /* Right now, this assumes that all volumes listed will be
223 * from the given dumpID. FIXME
225 code = bcdb_FindDumpByID(dumpTaskPtr->parentDumpID, dumpDescr);
228 afs_com_err(whoami, code, "Couldn't look up info for dump %d\n",
229 dumpTaskPtr->parentDumpID);
232 code = bcdb_FindVolumes(dumpTaskPtr->parentDumpID, vname, volumeEntries,
233 last, &next, MAXTAPESATONCE, &vecount);
236 if (!BackupName(vname))
238 strcat(vname, ".backup");
239 code = bcdb_FindVolumes(dumpTaskPtr->parentDumpID, vname, volumeEntries,
240 last, &next, MAXTAPESATONCE, &vecount);
246 code = bcdb_FindDump(vname, dumpTaskPtr->fromDate, dumpDescr);
247 if (!BackupName(vname)) { /* See if backup volume is there */
248 strcat(vname, ".backup");
249 dumpDescr = &dumpDescr2;
251 code = bcdb_FindDump(vname, dumpTaskPtr->fromDate, dumpDescr);
253 if (code) { /* Can't find backup, go with first results */
254 strcpy(vname, tvol->name);
255 dumpDescr = &dumpDescr1;
257 } else if (!tcode) { /* Both found an entry, go with latest result */
258 if (dumpDescr1.created > dumpDescr2.created) {
259 strcpy(vname, tvol->name);
260 dumpDescr = &dumpDescr1;
267 if (code) { /* If FindDump took an error */
268 afs_com_err(whoami, code, "; Can't find any dump for volume %s",
273 /* look to see if this dump has already been found */
274 for (pdi = 0, di = dumpinfolist; di; pdi = di, di = di->next) {
275 if (di->DumpId < dumpDescr->id) {
278 } else if (di->DumpId == dumpDescr->id) {
283 /* If didn't find it, create one and thread into list */
285 di = (struct dumpinfo *)malloc(sizeof(struct dumpinfo));
287 afs_com_err(whoami, BC_NOMEM, "");
290 memset(di, 0, sizeof(struct dumpinfo));
292 di->DumpId = dumpDescr->id;
293 di->initialDumpId = dumpDescr->initialDumpID;
294 di->parentDumpId = dumpDescr->parent;
295 di->level = dumpDescr->level;
298 di->next = dumpinfolist;
301 di->next = pdi->next;
306 /* Create one and thread into list */
307 vi = (struct volinfo *)malloc(sizeof(struct volinfo));
309 afs_com_err(whoami, BC_NOMEM, "");
312 memset(vi, 0, sizeof(struct volinfo));
314 vi->volname = (char *)malloc(strlen(vname) + 1);
317 afs_com_err(whoami, BC_NOMEM, "");
321 strcpy(vi->volname, vname);
323 vi->server = serverAll;
324 vi->partition = partitionAll;
326 vi->server = HOSTADDR(&tvol->server);
327 vi->partition = tvol->partition;
330 /* thread onto end of list */
332 di->volinfolist = vi;
334 di->lastinlist->next = vi;
338 /* For each of the above dumps we found (they could be increments), find
339 * the dump's lineage (up to the full dump).
341 for (di = dumpinfolist; di; di = di->next) {
342 /* Find each of the parent dumps */
343 memcpy(&dlevels[0], di, sizeof(struct dumpinfo));
344 for (lvl = 1, parent = dlevels[0].parentDumpId; parent;
345 parent = dlevels[lvl].parentDumpId, lvl++) {
346 if (lvl >= num_dlevels) { /* running out of dump levels */
347 struct dumpinfo *tdl = dlevels;
349 num_dlevels += num_dlevels; /* double */
350 dlevels = (struct dumpinfo *) malloc(num_dlevels * sizeof(*dlevels));
351 memcpy(dlevels, tdl, (num_dlevels/2) * sizeof(*dlevels));
354 code = bcdb_FindDumpByID(parent, &dumpDescr1);
356 for (vi = di->volinfolist; vi; vi = vi->next) {
357 afs_com_err(whoami, code,
358 "; Can't find parent DumpID %u for volume %s",
359 parent, vi->volname);
364 dlevels[lvl].DumpId = dumpDescr1.id;
365 dlevels[lvl].initialDumpId = dumpDescr1.initialDumpID;
366 dlevels[lvl].parentDumpId = dumpDescr1.parent;
367 dlevels[lvl].level = dumpDescr1.level;
370 /* For each of the volumes that has a dump in this lineage (vi),
371 * find where it is in each dump level (lv) starting at level 0 and
372 * going to higest. Each dump level could contain one or more
373 * fragments (vecount) of the volume (volume fragments span tapes).
374 * Each volume fragment is sorted by tapeid, tape sequence, and tape
377 for (vi = di->volinfolist; vi; vi = vi->next) {
378 tle = tapeList; /* Use for searching the tapelist */
379 ptle = 0; /* The previous tape list entry */
380 tlid = 0; /* tapeid of first entry */
381 /* Volume's dump-level. May not be same as dump's dump-level. This
382 * value gets incremented everytime the volume is found on a dump.
386 /* Do from level 0 dump to highest level dump */
387 for (lv = (lvl - 1); lv >= 0; lv--) {
388 foundvolume = 0; /* If found volume on this dump */
390 (dlevels[lv].initialDumpId ? dlevels[lv].
391 initialDumpId : dlevels[lv].DumpId);
393 /* Cycle through the volume fragments for this volume on the tape */
394 for (last = 0, c = 0; last >= 0; last = next, c++) {
396 bcdb_FindVolumes(dlevels[lv].DumpId, vi->volname,
397 volumeEntries, last, &next,
398 MAXTAPESATONCE, &vecount);
400 /* Volume wasn't found on the tape - so continue.
401 * This can happen when volume doesn't exist during level 0 dump
402 * but exists for incremental dump.
404 if (code == BUDB_ENDOFLIST) {
409 afs_com_err(whoami, code,
410 "; Can't find volume %s in DumpID %u",
411 vi->volname, dlevels[lv].DumpId);
415 /* If we have made the Findvolumes call more than once for the same
416 * volume, then begin the search at the top. This sort assumes that
417 * we are collecting information from lowest level dump (full dump)
418 * to the highest level dump (highest incremental)
419 * and from first fragment to last fragment at each level. If
420 * there is more than one call to FindVolumes, this is not true
429 /* For each of the volume fragments that we read, sort them into
430 * the list of tapes they are on. Sort by tapeid, then tape sequence.
431 * Do from first fragment (where volume begins) to last fragment.
433 for (ve = (vecount - 1); ve >= 0; ve--) {
434 foundvolume = 1; /* Found the volume on this dump */
436 tapeseq = volumeEntries[ve].tapeSeq;
438 /* Determine where in the list of tapes this should go */
439 for (; tle; ptle = tle, tle = tle->next) {
441 (tle->initialDumpID ? tle->
442 initialDumpID : tle->dumpID);
444 /* Sort by tapeids. BUT, we don't want add an entry in the middle
445 * of a dumpset (might split a volume fragmented across tapes).
446 * So make sure we step to next dumpset. tlid is the tapeid of
447 * the last tape we added a volume to. This can happen when an
448 * incremental was appended to a tape created prior its parent-
449 * dump's tape, and needs to be restored after it.
455 } /* Allocate and insert a tape entry */
458 /* Found the tapeid (the dumpset). Check if its the correct
461 else if (tapeid == tid) {
462 if (tapeseq < tle->tapeNumber) {
466 /* Allocate and insert a tape entry */
467 if (tapeseq == tle->tapeNumber) {
470 /* Don't allocate tape entry */
471 foundtape = 1; /* Found dumpset but not the tape */
474 /* Prevously found the tapeid (the dumpset) but this tape
475 * sequence not included (the tapeid has changed). So add
476 * this tape entry to the end of its dumpset.
478 else if (foundtape) {
482 /* Allocate and insert a tape entry */
485 /* Allocate a new tapelist entry if one not found */
487 tle = (struct bc_tapeList *)
488 malloc(sizeof(struct bc_tapeList));
490 afs_com_err(whoami, BC_NOMEM, "");
493 memset(tle, 0, sizeof(struct bc_tapeList));
496 (char *)malloc(strlen(volumeEntries[ve].tape)
498 if (!tle->tapeName) {
500 afs_com_err(whoami, BC_NOMEM, "");
504 strcpy(tle->tapeName, volumeEntries[ve].tape);
505 tle->dumpID = dlevels[lv].DumpId;
506 tle->initialDumpID = dlevels[lv].initialDumpId;
507 tle->tapeNumber = tapeseq;
509 /* Now thread the tape onto the list */
511 tle->next = tapeList;
514 tle->next = ptle->next;
519 (tle->initialDumpID ? tle->initialDumpID : tle->
522 /* Now place the volume fragment into the correct position on
523 * this tapelist entry. Duplicate entries are ignored.
525 for (pti = 0, ti = tle->restoreList; ti;
526 pti = ti, ti = ti->next) {
527 if (volumeEntries[ve].position < ti->position) {
531 /* Allocate an entry */
532 else if (volumeEntries[ve].position ==
535 } /* Duplicate entry */
538 /* If didn't find one, allocate and thread onto list.
539 * Remember the server and partition.
542 ti = (struct bc_tapeItem *)
543 malloc(sizeof(struct bc_tapeItem));
545 afs_com_err(whoami, BC_NOMEM, "");
548 memset(ti, 0, sizeof(struct bc_tapeItem));
551 (char *)malloc(strlen(volumeEntries[ve].name)
553 if (!ti->volumeName) {
555 afs_com_err(whoami, BC_NOMEM, "");
559 strcpy(ti->volumeName, volumeEntries[ve].name);
560 ti->server = vi->server;
561 ti->partition = vi->partition;
562 ti->oid = volumeEntries[ve].id;
563 ti->position = volumeEntries[ve].position;
564 ti->vollevel = voldumplevel;
565 ti->dumplevel = dlevels[lv].level;
566 ti->lastdump = ((lv == 0) ? 1 : 0);
569 ti->next = tle->restoreList;
570 tle->restoreList = ti;
572 ti->next = pti->next;
578 } /* ve: for each returned volume fragment by bcdb_FindVolumes() */
579 } /* last: Multiple calls to bcdb_FindVolumes() */
583 } /* lv: For each dump level */
584 } /* vi: For each volume to search for in the dump hierarchy */
585 } /* di: For each dump */
588 afs_com_err(whoami, 0, "No volumes to restore");
592 if (dumpTaskPtr->dontExecute) {
593 for (tle = tapeList; tle; tle = tle->next) {
594 for (ti = tle->restoreList; ti; ti = ti->next) {
596 (tle->initialDumpID ? tle->initialDumpID : tle->dumpID);
597 strcpy(vname, ti->volumeName);
599 if (dumpTaskPtr->newExt)
600 strcat(vname, dumpTaskPtr->newExt);
602 /* print volume to restore and the tape and position its on */
605 ("Restore volume %s (ID %d) from tape %s (%u), position %d",
606 ti->volumeName, ti->oid, tle->tapeName, tapedumpid,
609 if (strcmp(ti->volumeName, vname) != 0)
610 printf(" as %s", vname);
615 /* print in format recognized by volsetrestore */
619 gethostbyaddr((char *)&haddr, sizeof(haddr), AF_INET);
621 printf("%s", hostPtr->h_name);
623 printf("%u", ti->server);
626 viceName(ti->partition);
628 printf(" %s # as %s; %s (%u); pos %d;", ti->volumeName,
629 vname, tle->tapeName, tapedumpid, ti->position);
632 printf(" %s", ctime(&did));
640 /* Allocate a list of volumes to restore */
642 (struct tc_restoreDesc *)malloc(nentries *
643 sizeof(struct tc_restoreDesc));
645 afs_com_err(whoami, BC_NOMEM, "");
648 memset(tcarray, 0, nentries * sizeof(struct tc_restoreDesc));
650 /* Fill in the array with the list above */
652 for (tle = tapeList; tle; tle = tle->next) {
653 for (ti = tle->restoreList; ti; ti = ti->next) {
654 tcarray[i].origVid = ti->oid; /* means unknown */
656 tcarray[i].flags = 0;
657 if (ti->vollevel == 0)
658 tcarray[i].flags |= RDFLAG_FIRSTDUMP;
660 tcarray[i].flags |= RDFLAG_LASTDUMP;
661 tcarray[i].position = ti->position;
662 tcarray[i].dbDumpId = tle->dumpID;
663 tcarray[i].initialDumpId = tle->initialDumpID;
664 tcarray[i].hostAddr = ti->server; /* just the internet address */
665 tcarray[i].partition = ti->partition;
667 strcpy(tcarray[i].tapeName, tle->tapeName);
668 strcpy(tcarray[i].oldName, ti->volumeName);
669 strcpy(tcarray[i].newName, ti->volumeName);
670 StripBackup(tcarray[i].newName);
671 if (dumpTaskPtr->newExt)
672 strcat(tcarray[i].newName, dumpTaskPtr->newExt);
674 tcarray[i].dumpLevel = ti->dumplevel;
679 printf("Starting restore\n");
681 /* Loop until all of the tape entries are used */
682 for (startentry = 0; startentry < nentries; startentry += todump) {
683 /* Get all the next tape entries with the same port offset */
684 if (dumpTaskPtr->portCount == 0) {
686 todump = nentries - startentry;
687 } else if (dumpTaskPtr->portCount == 1) {
688 port = dumpTaskPtr->portOffset[0];
689 todump = nentries - startentry;
691 level = tcarray[startentry].dumpLevel;
695 dumpTaskPtr->portCount -
696 1 ? level : dumpTaskPtr->portCount - 1)];
698 for (todump = 1; ((startentry + todump) < nentries); todump++) {
699 level = tcarray[startentry + todump].dumpLevel;
703 dumpTaskPtr->portCount -
704 1 ? level : dumpTaskPtr->portCount - 1)];
705 if (port != nextport)
706 break; /* break if we change ports */
710 rpcArray.tc_restoreArray_len = todump;
711 rpcArray.tc_restoreArray_val = &tcarray[startentry];
713 code = ConnectButc(dumpTaskPtr->config, port, &aconn);
717 if (tcarray[startentry].dumpLevel == 0)
718 printf("\nFull restore being processed on port %d\n", port);
720 printf("\nIncremental restore being processed on port %d\n",
724 TC_PerformRestore(aconn, "DumpSetName", &rpcArray,
725 &dumpTaskPtr->dumpID);
727 afs_com_err(whoami, code, "; Failed to start restore");
731 /* setup status monitor node */
732 statusPtr = createStatusNode();
734 statusPtr->port = port;
735 statusPtr->taskId = dumpTaskPtr->dumpID;
736 statusPtr->jobNumber = bc_jobNumber();
737 statusPtr->flags &= ~STARTING; /* clearStatus to examine */
738 if (tcarray[startentry].dumpLevel == 0)
739 sprintf(statusPtr->taskName, "Full Restore");
741 sprintf(statusPtr->taskName, "Incremental Restore");
744 /* Wait until this restore pass is complete before starting the next
745 * pass. Query the statusWatcher for the status
749 newStatusPtr = findStatus(dumpTaskPtr->dumpID);
752 flags & (ABORT_REQUEST | ABORT_SENT | ABORT_DONE |
756 } else if (newStatusPtr->flags & (TASK_DONE)) {
766 rx_DestroyConnection(aconn);
767 aconn = (struct rx_connection *)0;
770 /* free up everything */
773 rx_DestroyConnection(aconn);
778 /* Free the dumpinfo list and its volumes */
779 for (di = dumpinfolist; di; di = ndi) {
780 for (vi = di->volinfolist; vi; vi = nvi) {
790 /* Free the tape lists and items */
791 for (tle = tapeList; tle; tle = ntle) {
792 for (ti = tle->restoreList; ti; ti = nti) {
795 free(ti->volumeName);
804 /* free local-like things we alloacted to save stack space */