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>
19 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
30 #include <afs/bubasics.h>
32 #include <afs/com_err.h>
35 #include <afs/vlserver.h>
36 #include "error_macros.h"
37 #include "bucoord_prototypes.h"
39 extern struct bc_dumpTask bc_dumpTasks[BC_MAXSIMDUMPS];
42 #define MAXTAPESATONCE 10
44 #define HOSTADDR(sockaddr) (sockaddr)->sin_addr.s_addr
46 /* local structure to keep track of volumes and the dumps from which
47 * they should be restored
50 struct dumpinfo *next;
51 struct volinfo *volinfolist;
52 struct volinfo *lastinlist;
54 afs_int32 initialDumpId;
55 afs_int32 parentDumpId;
66 /* local structure used to keep track of a tape, including a list of
67 * volumes to restore from that tape (bc_tapeItem)
70 struct bc_tapeList *next; /* next guy in list */
71 char *tapeName; /* name of the tape */
72 afs_int32 dumpID; /* dump located on this tape */
73 afs_int32 initialDumpID; /* initial dump located on this tape */
74 afs_int32 tapeNumber; /* which tape in the dump */
75 struct bc_tapeItem *restoreList; /* volumes to restore from this tape */
78 /* each tape has a list of volumes to restore; hangs off of a struct
79 bc_tapeList. Kept sorted so that we can read the tape once to do
80 everything we need to do. */
82 struct bc_tapeItem *next;
83 char *volumeName; /* volume to restore */
84 afs_int32 position; /* file slot on this tape */
85 afs_int32 oid; /* id of the volume */
86 afs_int32 dumplevel; /* level # of the containing dump (0 == top level) */
87 afs_int32 vollevel; /* level # of the containing volume (0 == full dump) */
90 int lastdump; /* The last incremental to restore */
93 /* strip .backup from the end of a name */
95 StripBackup(char *aname)
99 if ((j = BackupName(aname))) {
107 BackupName(char *aname)
112 if ((j > 7) && (strcmp(".backup", aname + j - 7) == 0))
114 if ((j > 9) && (strcmp(".readonly", aname + j - 9) == 0))
120 extractTapeSeq(char *tapename)
124 sptr = strrchr(tapename, '.');
137 alph = "abcdefjhijklmnopqrstuvwxyz";
140 viceName(value / 26 - 1);
143 printf("%c", alph[r]);
148 * aindex - index into bc_dumpTasks that describes this dump.
151 bc_Restorer(afs_int32 aindex)
153 struct bc_dumpTask *dumpTaskPtr;
155 char vname[BU_MAXNAMELEN];
156 struct budb_dumpEntry *dumpDescr, dumpDescr1, dumpDescr2;
157 struct budb_volumeEntry *volumeEntries;
158 struct bc_volumeDump *tvol;
159 afs_int32 code = 0, tcode;
160 afs_int32 tapedumpid, parent;
162 afs_int32 nentries = 0;
164 afs_int32 next, ve, vecount;
165 struct bc_tapeItem *ti, *pti, *nti;
166 struct bc_tapeList *tapeList = (struct bc_tapeList *)0;
167 struct bc_tapeList *tle, *ptle, *ntle;
169 afs_int32 tapeid, tid;
171 struct tc_restoreArray rpcArray; /* the rpc structure we use */
172 struct tc_restoreDesc *tcarray = (struct tc_restoreDesc *)0;
174 afs_int32 i, startentry, todump;
175 afs_int32 port, nextport;
176 afs_int32 level, tapeseq;
177 afs_int32 foundvolume, voldumplevel;
178 struct rx_connection *aconn = (struct rx_connection *)0;
179 statusP statusPtr, newStatusPtr;
181 struct dumpinfo *dumpinfolist = NULL;
182 struct dumpinfo *pdi, *ndi, *di, *dlevels;
183 struct volinfo *nvi, *vi;
185 int num_dlevels = 20;
187 afs_int32 serverAll; /* The server to which all volumes are to be restore to */
188 afs_int32 partitionAll; /* Likewise for partition */
189 struct hostent *hostPtr;
194 dlevels = (struct dumpinfo *) malloc(num_dlevels * sizeof(*dlevels));
196 dumpTaskPtr = &bc_dumpTasks[aindex];
197 serverAll = HOSTADDR(&dumpTaskPtr->destServer);
198 partitionAll = dumpTaskPtr->destPartition;
200 volumeEntries = (struct budb_volumeEntry *)
201 malloc(MAXTAPESATONCE * sizeof(struct budb_volumeEntry));
202 if (!volumeEntries) {
203 afs_com_err(whoami, BC_NOMEM, "");
207 /* For each volume to restore, find which dump it's most recent full or
208 * incremental is on and thread onto our dump list (from oldest to newest
209 * dump). Also hang the volume off of the dump (no particular order).
211 for (tvol = dumpTaskPtr->volumes; tvol; tvol = tvol->next) { /*tvol */
212 strcpy(vname, tvol->name);
213 dumpDescr = &dumpDescr1;
214 if (dumpTaskPtr->parentDumpID > 0) /* Told which dump to try */
216 /* Right now, this assumes that all volumes listed will be
217 * from the given dumpID. FIXME
219 code = bcdb_FindDumpByID(dumpTaskPtr->parentDumpID, dumpDescr);
222 afs_com_err(whoami, code, "Couldn't look up info for dump %d\n",
223 dumpTaskPtr->parentDumpID);
226 code = bcdb_FindVolumes(dumpTaskPtr->parentDumpID, vname, volumeEntries,
227 last, &next, MAXTAPESATONCE, &vecount);
230 if (!BackupName(vname))
232 strcat(vname, ".backup");
233 code = bcdb_FindVolumes(dumpTaskPtr->parentDumpID, vname, volumeEntries,
234 last, &next, MAXTAPESATONCE, &vecount);
240 code = bcdb_FindDump(vname, dumpTaskPtr->fromDate, dumpDescr);
241 if (!BackupName(vname)) { /* See if backup volume is there */
242 strcat(vname, ".backup");
243 dumpDescr = &dumpDescr2;
245 code = bcdb_FindDump(vname, dumpTaskPtr->fromDate, dumpDescr);
247 if (code) { /* Can't find backup, go with first results */
248 strcpy(vname, tvol->name);
249 dumpDescr = &dumpDescr1;
251 } else if (!tcode) { /* Both found an entry, go with latest result */
252 if (dumpDescr1.created > dumpDescr2.created) {
253 strcpy(vname, tvol->name);
254 dumpDescr = &dumpDescr1;
261 if (code) { /* If FindDump took an error */
262 afs_com_err(whoami, code, "; Can't find any dump for volume %s",
267 /* look to see if this dump has already been found */
268 for (pdi = 0, di = dumpinfolist; di; pdi = di, di = di->next) {
269 if (di->DumpId < dumpDescr->id) {
272 } else if (di->DumpId == dumpDescr->id) {
277 /* If didn't find it, create one and thread into list */
279 di = (struct dumpinfo *)malloc(sizeof(struct dumpinfo));
281 afs_com_err(whoami, BC_NOMEM, "");
284 memset(di, 0, sizeof(struct dumpinfo));
286 di->DumpId = dumpDescr->id;
287 di->initialDumpId = dumpDescr->initialDumpID;
288 di->parentDumpId = dumpDescr->parent;
289 di->level = dumpDescr->level;
292 di->next = dumpinfolist;
295 di->next = pdi->next;
300 /* Create one and thread into list */
301 vi = (struct volinfo *)malloc(sizeof(struct volinfo));
303 afs_com_err(whoami, BC_NOMEM, "");
306 memset(vi, 0, sizeof(struct volinfo));
308 vi->volname = (char *)malloc(strlen(vname) + 1);
311 afs_com_err(whoami, BC_NOMEM, "");
315 strcpy(vi->volname, vname);
317 vi->server = serverAll;
318 vi->partition = partitionAll;
320 vi->server = HOSTADDR(&tvol->server);
321 vi->partition = tvol->partition;
324 /* thread onto end of list */
326 di->volinfolist = vi;
328 di->lastinlist->next = vi;
332 /* For each of the above dumps we found (they could be increments), find
333 * the dump's lineage (up to the full dump).
335 for (di = dumpinfolist; di; di = di->next) {
336 /* Find each of the parent dumps */
337 memcpy(&dlevels[0], di, sizeof(struct dumpinfo));
338 for (lvl = 1, parent = dlevels[0].parentDumpId; parent;
339 parent = dlevels[lvl].parentDumpId, lvl++) {
340 if (lvl >= num_dlevels) { /* running out of dump levels */
341 struct dumpinfo *tdl = dlevels;
343 num_dlevels += num_dlevels; /* double */
344 dlevels = (struct dumpinfo *) malloc(num_dlevels * sizeof(*dlevels));
345 memcpy(dlevels, tdl, (num_dlevels/2) * sizeof(*dlevels));
348 code = bcdb_FindDumpByID(parent, &dumpDescr1);
350 for (vi = di->volinfolist; vi; vi = vi->next) {
351 afs_com_err(whoami, code,
352 "; Can't find parent DumpID %u for volume %s",
353 parent, vi->volname);
358 dlevels[lvl].DumpId = dumpDescr1.id;
359 dlevels[lvl].initialDumpId = dumpDescr1.initialDumpID;
360 dlevels[lvl].parentDumpId = dumpDescr1.parent;
361 dlevels[lvl].level = dumpDescr1.level;
364 /* For each of the volumes that has a dump in this lineage (vi),
365 * find where it is in each dump level (lv) starting at level 0 and
366 * going to higest. Each dump level could contain one or more
367 * fragments (vecount) of the volume (volume fragments span tapes).
368 * Each volume fragment is sorted by tapeid, tape sequence, and tape
371 for (vi = di->volinfolist; vi; vi = vi->next) {
372 tle = tapeList; /* Use for searching the tapelist */
373 ptle = 0; /* The previous tape list entry */
374 tlid = 0; /* tapeid of first entry */
375 /* Volume's dump-level. May not be same as dump's dump-level. This
376 * value gets incremented everytime the volume is found on a dump.
380 /* Do from level 0 dump to highest level dump */
381 for (lv = (lvl - 1); lv >= 0; lv--) {
382 foundvolume = 0; /* If found volume on this dump */
384 (dlevels[lv].initialDumpId ? dlevels[lv].
385 initialDumpId : dlevels[lv].DumpId);
387 /* Cycle through the volume fragments for this volume on the tape */
388 for (last = 0, c = 0; last >= 0; last = next, c++) {
390 bcdb_FindVolumes(dlevels[lv].DumpId, vi->volname,
391 volumeEntries, last, &next,
392 MAXTAPESATONCE, &vecount);
394 /* Volume wasn't found on the tape - so continue.
395 * This can happen when volume doesn't exist during level 0 dump
396 * but exists for incremental dump.
398 if (code == BUDB_ENDOFLIST) {
403 afs_com_err(whoami, code,
404 "; Can't find volume %s in DumpID %u",
405 vi->volname, dlevels[lv].DumpId);
409 /* If we have made the Findvolumes call more than once for the same
410 * volume, then begin the search at the top. This sort assumes that
411 * we are collecting information from lowest level dump (full dump)
412 * to the highest level dump (highest incremental)
413 * and from first fragment to last fragment at each level. If
414 * there is more than one call to FindVolumes, this is not true
423 /* For each of the volume fragments that we read, sort them into
424 * the list of tapes they are on. Sort by tapeid, then tape sequence.
425 * Do from first fragment (where volume begins) to last fragment.
427 for (ve = (vecount - 1); ve >= 0; ve--) {
428 foundvolume = 1; /* Found the volume on this dump */
430 tapeseq = volumeEntries[ve].tapeSeq;
432 /* Determine where in the list of tapes this should go */
433 for (; tle; ptle = tle, tle = tle->next) {
435 (tle->initialDumpID ? tle->
436 initialDumpID : tle->dumpID);
438 /* Sort by tapeids. BUT, we don't want add an entry in the middle
439 * of a dumpset (might split a volume fragmented across tapes).
440 * So make sure we step to next dumpset. tlid is the tapeid of
441 * the last tape we added a volume to. This can happen when an
442 * incremental was appended to a tape created prior its parent-
443 * dump's tape, and needs to be restored after it.
449 } /* Allocate and insert a tape entry */
452 /* Found the tapeid (the dumpset). Check if its the correct
455 else if (tapeid == tid) {
456 if (tapeseq < tle->tapeNumber) {
460 /* Allocate and insert a tape entry */
461 if (tapeseq == tle->tapeNumber) {
464 /* Don't allocate tape entry */
465 foundtape = 1; /* Found dumpset but not the tape */
468 /* Prevously found the tapeid (the dumpset) but this tape
469 * sequence not included (the tapeid has changed). So add
470 * this tape entry to the end of its dumpset.
472 else if (foundtape) {
476 /* Allocate and insert a tape entry */
479 /* Allocate a new tapelist entry if one not found */
481 tle = (struct bc_tapeList *)
482 malloc(sizeof(struct bc_tapeList));
484 afs_com_err(whoami, BC_NOMEM, "");
487 memset(tle, 0, sizeof(struct bc_tapeList));
490 (char *)malloc(strlen(volumeEntries[ve].tape)
492 if (!tle->tapeName) {
494 afs_com_err(whoami, BC_NOMEM, "");
498 strcpy(tle->tapeName, volumeEntries[ve].tape);
499 tle->dumpID = dlevels[lv].DumpId;
500 tle->initialDumpID = dlevels[lv].initialDumpId;
501 tle->tapeNumber = tapeseq;
503 /* Now thread the tape onto the list */
505 tle->next = tapeList;
508 tle->next = ptle->next;
513 (tle->initialDumpID ? tle->initialDumpID : tle->
516 /* Now place the volume fragment into the correct position on
517 * this tapelist entry. Duplicate entries are ignored.
519 for (pti = 0, ti = tle->restoreList; ti;
520 pti = ti, ti = ti->next) {
521 if (volumeEntries[ve].position < ti->position) {
525 /* Allocate an entry */
526 else if (volumeEntries[ve].position ==
529 } /* Duplicate entry */
532 /* If didn't find one, allocate and thread onto list.
533 * Remember the server and partition.
536 ti = (struct bc_tapeItem *)
537 malloc(sizeof(struct bc_tapeItem));
539 afs_com_err(whoami, BC_NOMEM, "");
542 memset(ti, 0, sizeof(struct bc_tapeItem));
545 (char *)malloc(strlen(volumeEntries[ve].name)
547 if (!ti->volumeName) {
549 afs_com_err(whoami, BC_NOMEM, "");
553 strcpy(ti->volumeName, volumeEntries[ve].name);
554 ti->server = vi->server;
555 ti->partition = vi->partition;
556 ti->oid = volumeEntries[ve].id;
557 ti->position = volumeEntries[ve].position;
558 ti->vollevel = voldumplevel;
559 ti->dumplevel = dlevels[lv].level;
560 ti->lastdump = ((lv == 0) ? 1 : 0);
563 ti->next = tle->restoreList;
564 tle->restoreList = ti;
566 ti->next = pti->next;
572 } /* ve: for each returned volume fragment by bcdb_FindVolumes() */
573 } /* last: Multiple calls to bcdb_FindVolumes() */
577 } /* lv: For each dump level */
578 } /* vi: For each volume to search for in the dump hierarchy */
579 } /* di: For each dump */
582 afs_com_err(whoami, 0, "No volumes to restore");
586 if (dumpTaskPtr->dontExecute) {
587 for (tle = tapeList; tle; tle = tle->next) {
588 for (ti = tle->restoreList; ti; ti = ti->next) {
590 (tle->initialDumpID ? tle->initialDumpID : tle->dumpID);
591 strcpy(vname, ti->volumeName);
593 if (dumpTaskPtr->newExt)
594 strcat(vname, dumpTaskPtr->newExt);
596 /* print volume to restore and the tape and position its on */
599 ("Restore volume %s (ID %d) from tape %s (%u), position %d",
600 ti->volumeName, ti->oid, tle->tapeName, tapedumpid,
603 if (strcmp(ti->volumeName, vname) != 0)
604 printf(" as %s", vname);
609 /* print in format recognized by volsetrestore */
613 gethostbyaddr((char *)&haddr, sizeof(haddr), AF_INET);
615 printf("%s", hostPtr->h_name);
617 printf("%u", ti->server);
620 viceName(ti->partition);
622 printf(" %s # as %s; %s (%u); pos %d;", ti->volumeName,
623 vname, tle->tapeName, tapedumpid, ti->position);
626 printf(" %s", ctime(&did));
634 /* Allocate a list of volumes to restore */
636 (struct tc_restoreDesc *)malloc(nentries *
637 sizeof(struct tc_restoreDesc));
639 afs_com_err(whoami, BC_NOMEM, "");
642 memset(tcarray, 0, nentries * sizeof(struct tc_restoreDesc));
644 /* Fill in the array with the list above */
646 for (tle = tapeList; tle; tle = tle->next) {
647 for (ti = tle->restoreList; ti; ti = ti->next) {
648 tcarray[i].origVid = ti->oid; /* means unknown */
650 tcarray[i].flags = 0;
651 if (ti->vollevel == 0)
652 tcarray[i].flags |= RDFLAG_FIRSTDUMP;
654 tcarray[i].flags |= RDFLAG_LASTDUMP;
655 tcarray[i].position = ti->position;
656 tcarray[i].dbDumpId = tle->dumpID;
657 tcarray[i].initialDumpId = tle->initialDumpID;
658 tcarray[i].hostAddr = ti->server; /* just the internet address */
659 tcarray[i].partition = ti->partition;
661 strcpy(tcarray[i].tapeName, tle->tapeName);
662 strcpy(tcarray[i].oldName, ti->volumeName);
663 strcpy(tcarray[i].newName, ti->volumeName);
664 StripBackup(tcarray[i].newName);
665 if (dumpTaskPtr->newExt)
666 strcat(tcarray[i].newName, dumpTaskPtr->newExt);
668 tcarray[i].dumpLevel = ti->dumplevel;
673 printf("Starting restore\n");
675 /* Loop until all of the tape entries are used */
676 for (startentry = 0; startentry < nentries; startentry += todump) {
677 /* Get all the next tape entries with the same port offset */
678 if (dumpTaskPtr->portCount == 0) {
680 todump = nentries - startentry;
681 } else if (dumpTaskPtr->portCount == 1) {
682 port = dumpTaskPtr->portOffset[0];
683 todump = nentries - startentry;
685 level = tcarray[startentry].dumpLevel;
689 dumpTaskPtr->portCount -
690 1 ? level : dumpTaskPtr->portCount - 1)];
692 for (todump = 1; ((startentry + todump) < nentries); todump++) {
693 level = tcarray[startentry + todump].dumpLevel;
697 dumpTaskPtr->portCount -
698 1 ? level : dumpTaskPtr->portCount - 1)];
699 if (port != nextport)
700 break; /* break if we change ports */
704 rpcArray.tc_restoreArray_len = todump;
705 rpcArray.tc_restoreArray_val = &tcarray[startentry];
707 code = ConnectButc(dumpTaskPtr->config, port, &aconn);
711 if (tcarray[startentry].dumpLevel == 0)
712 printf("\nFull restore being processed on port %d\n", port);
714 printf("\nIncremental restore being processed on port %d\n",
718 TC_PerformRestore(aconn, "DumpSetName", &rpcArray,
719 &dumpTaskPtr->dumpID);
721 afs_com_err(whoami, code, "; Failed to start restore");
725 /* setup status monitor node */
726 statusPtr = createStatusNode();
728 statusPtr->port = port;
729 statusPtr->taskId = dumpTaskPtr->dumpID;
730 statusPtr->jobNumber = bc_jobNumber();
731 statusPtr->flags &= ~STARTING; /* clearStatus to examine */
732 if (tcarray[startentry].dumpLevel == 0)
733 sprintf(statusPtr->taskName, "Full Restore");
735 sprintf(statusPtr->taskName, "Incremental Restore");
738 /* Wait until this restore pass is complete before starting the next
739 * pass. Query the statusWatcher for the status
743 newStatusPtr = findStatus(dumpTaskPtr->dumpID);
746 flags & (ABORT_REQUEST | ABORT_SENT | ABORT_DONE |
750 } else if (newStatusPtr->flags & (TASK_DONE)) {
760 rx_DestroyConnection(aconn);
761 aconn = (struct rx_connection *)0;
764 /* free up everything */
767 rx_DestroyConnection(aconn);
772 /* Free the dumpinfo list and its volumes */
773 for (di = dumpinfolist; di; di = ndi) {
774 for (vi = di->volinfolist; vi; vi = nvi) {
784 /* Free the tape lists and items */
785 for (tle = tapeList; tle; tle = ntle) {
786 for (ti = tle->restoreList; ti; ti = nti) {
789 free(ti->volumeName);
798 /* free local-like things we alloacted to save stack space */