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>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
33 #include <afs/bubasics.h>
35 #include <afs/com_err.h>
38 #include <afs/vlserver.h>
39 #include "error_macros.h"
41 extern struct bc_dumpTask bc_dumpTasks[BC_MAXSIMDUMPS];
44 #define BC_MAXLEVELS 20
45 #define MAXTAPESATONCE 10
48 #define HOSTADDR(sockaddr) (sockaddr)->sin_addr.S_un.S_addr
50 #define HOSTADDR(sockaddr) (sockaddr)->sin_addr.s_addr
53 /* local structure to keep track of volumes and the dumps from which
54 * they should be restored
57 struct dumpinfo *next;
58 struct volinfo *volinfolist;
59 struct volinfo *lastinlist;
61 afs_int32 initialDumpId;
62 afs_int32 parentDumpId;
73 /* local structure used to keep track of a tape, including a list of
74 * volumes to restore from that tape (bc_tapeItem)
77 struct bc_tapeList *next; /* next guy in list */
78 char *tapeName; /* name of the tape */
79 afs_int32 dumpID; /* dump located on this tape */
80 afs_int32 initialDumpID; /* initial dump located on this tape */
81 afs_int32 tapeNumber; /* which tape in the dump */
82 struct bc_tapeItem *restoreList; /* volumes to restore from this tape */
85 /* each tape has a list of volumes to restore; hangs off of a struct
86 bc_tapeList. Kept sorted so that we can read the tape once to do
87 everything we need to do. */
89 struct bc_tapeItem *next;
90 char *volumeName; /* volume to restore */
91 afs_int32 position; /* file slot on this tape */
92 afs_int32 oid; /* id of the volume */
93 afs_int32 dumplevel; /* level # of the containing dump (0 == top level) */
94 afs_int32 vollevel; /* level # of the containing volume (0 == full dump) */
97 int lastdump; /* The last incremental to restore */
100 /* strip .backup from the end of a name */
103 register char *aname;
107 if (j = BackupName(aname)) {
121 if ((j > 7) && (strcmp(".backup", aname + j - 7) == 0))
123 if ((j > 9) && (strcmp(".readonly", aname + j - 9) == 0))
128 extractTapeSeq(tapename)
133 sptr = strrchr(tapename, '.');
146 alph = "abcdefjhijklmnopqrstuvwxyz";
149 viceName(value / 26 - 1);
152 printf("%c", alph[r]);
157 * aindex - index into bc_dumpTasks that describes this dump.
162 struct bc_dumpTask *dumpTaskPtr;
164 char vname[BU_MAXNAMELEN];
165 struct budb_dumpEntry *dumpDescr, dumpDescr1, dumpDescr2;
166 struct budb_volumeEntry *volumeEntries;
167 struct bc_volumeDump *tvol;
168 afs_int32 code = 0, tcode;
169 afs_int32 tapedumpid, parent;
171 afs_int32 nentries = 0;
172 afs_int32 last, next, ve, vecount;
173 struct bc_tapeItem *ti, *pti, *nti;
174 struct bc_tapeList *tapeList = (struct bc_tapeList *)0;
175 struct bc_tapeList *tle, *ptle, *ntle;
177 afs_int32 tapeid, tid;
179 struct tc_restoreArray rpcArray; /* the rpc structure we use */
180 struct tc_restoreDesc *tcarray = (struct tc_restoreDesc *)0;
182 afs_int32 i, startentry, todump;
183 afs_int32 port, nextport;
184 afs_int32 level, tapeseq;
185 afs_int32 foundvolume, voldumplevel;
186 struct rx_connection *aconn = (struct rx_connection *)0;
187 statusP statusPtr, newStatusPtr;
189 struct dumpinfo *dumpinfolist = NULL;
190 struct dumpinfo *pdi, *ndi, *di, dlevels[BC_MAXLEVELS];
191 struct volinfo *pvi, *nvi, *vi;
194 afs_int32 serverAll; /* The server to which all volumes are to be restore to */
195 afs_int32 partitionAll; /* Likewise for partition */
196 struct hostent *hostPtr;
201 extern statusP createStatusNode();
202 extern statusP findStatus();
204 dumpTaskPtr = &bc_dumpTasks[aindex];
205 serverAll = HOSTADDR(&dumpTaskPtr->destServer);
206 partitionAll = dumpTaskPtr->destPartition;
208 volumeEntries = (struct budb_volumeEntry *)
209 malloc(MAXTAPESATONCE * sizeof(struct budb_volumeEntry));
210 if (!volumeEntries) {
211 com_err(whoami, BC_NOMEM, "");
215 /* For each volume to restore, find which dump it's most recent full or
216 * incremental is on and thread onto our dump list (from oldest to newest
217 * dump). Also hang the volume off of the dump (no particular order).
219 for (tvol = dumpTaskPtr->volumes; tvol; tvol = tvol->next) { /*tvol */
220 strcpy(vname, tvol->name);
221 dumpDescr = &dumpDescr1;
222 code = bcdb_FindDump(vname, dumpTaskPtr->fromDate, dumpDescr);
224 if (!BackupName(vname)) { /* See if backup volume is there */
225 strcat(vname, ".backup");
226 dumpDescr = &dumpDescr2;
228 code = bcdb_FindDump(vname, dumpTaskPtr->fromDate, dumpDescr);
230 if (code) { /* Can't find backup, go with first results */
231 strcpy(vname, tvol->name);
232 dumpDescr = &dumpDescr1;
234 } else if (!tcode) { /* Both found an entry, go with latest result */
235 if (dumpDescr1.created > dumpDescr2.created) {
236 strcpy(vname, tvol->name);
237 dumpDescr = &dumpDescr1;
243 if (code) { /* If FindDump took an error */
244 com_err(whoami, code, "; Can't find any dump for volume %s",
249 /* look to see if this dump has already been found */
250 for (pdi = 0, di = dumpinfolist; di; pdi = di, di = di->next) {
251 if (di->DumpId < dumpDescr->id) {
254 } else if (di->DumpId == dumpDescr->id) {
259 /* If didn't find it, create one and thread into list */
261 di = (struct dumpinfo *)malloc(sizeof(struct dumpinfo));
263 com_err(whoami, BC_NOMEM, "");
266 memset(di, 0, sizeof(struct dumpinfo));
268 di->DumpId = dumpDescr->id;
269 di->initialDumpId = dumpDescr->initialDumpID;
270 di->parentDumpId = dumpDescr->parent;
271 di->level = dumpDescr->level;
274 di->next = dumpinfolist;
277 di->next = pdi->next;
282 /* Create one and thread into list */
283 vi = (struct volinfo *)malloc(sizeof(struct volinfo));
285 com_err(whoami, BC_NOMEM, "");
288 memset(vi, 0, sizeof(struct volinfo));
290 vi->volname = (char *)malloc(strlen(vname) + 1);
293 com_err(whoami, BC_NOMEM, "");
297 strcpy(vi->volname, vname);
299 vi->server = serverAll;
300 vi->partition = partitionAll;
302 vi->server = HOSTADDR(&tvol->server);
303 vi->partition = tvol->partition;
306 /* thread onto end of list */
308 di->volinfolist = vi;
310 di->lastinlist->next = vi;
314 /* For each of the above dumps we found (they could be increments), find
315 * the dump's lineage (up to the full dump).
317 for (di = dumpinfolist; di; di = di->next) {
318 /* Find each of the parent dumps */
319 memcpy(&dlevels[0], di, sizeof(struct dumpinfo));
320 for (lvl = 1, parent = dlevels[0].parentDumpId; parent;
321 parent = dlevels[lvl].parentDumpId, lvl++) {
322 code = bcdb_FindDumpByID(parent, &dumpDescr1);
324 for (vi = di->volinfolist; vi; vi = vi->next) {
325 com_err(whoami, code,
326 "; Can't find parent DumpID %u for volume %s",
327 parent, vi->volname);
332 dlevels[lvl].DumpId = dumpDescr1.id;
333 dlevels[lvl].initialDumpId = dumpDescr1.initialDumpID;
334 dlevels[lvl].parentDumpId = dumpDescr1.parent;
335 dlevels[lvl].level = dumpDescr1.level;
338 /* For each of the volumes that has a dump in this lineage (vi),
339 * find where it is in each dump level (lv) starting at level 0 and
340 * going to higest. Each dump level could contain one or more
341 * fragments (vecount) of the volume (volume fragments span tapes).
342 * Each volume fragment is sorted by tapeid, tape sequence, and tape
345 for (vi = di->volinfolist; vi; vi = vi->next) {
346 tle = tapeList; /* Use for searching the tapelist */
347 ptle = 0; /* The previous tape list entry */
348 tlid = 0; /* tapeid of first entry */
349 /* Volume's dump-level. May not be same as dump's dump-level. This
350 * value gets incremented everytime the volume is found on a dump.
354 /* Do from level 0 dump to highest level dump */
355 for (lv = (lvl - 1); lv >= 0; lv--) {
356 foundvolume = 0; /* If found volume on this dump */
358 (dlevels[lv].initialDumpId ? dlevels[lv].
359 initialDumpId : dlevels[lv].DumpId);
361 /* Cycle through the volume fragments for this volume on the tape */
362 for (last = 0, c = 0; last >= 0; last = next, c++) {
364 bcdb_FindVolumes(dlevels[lv].DumpId, vi->volname,
365 volumeEntries, last, &next,
366 MAXTAPESATONCE, &vecount);
368 /* Volume wasn't found on the tape - so continue.
369 * This can happen when volume doesn't exist during level 0 dump
370 * but exists for incremental dump.
372 if (code == BUDB_ENDOFLIST) {
377 com_err(whoami, code,
378 "; Can't find volume %s in DumpID %u",
379 vi->volname, dlevels[lv].DumpId);
383 /* If we have made the Findvolumes call more than once for the same
384 * volume, then begin the search at the top. This sort assumes that
385 * we are collecting information from lowest level dump (full dump)
386 * to the highest level dump (highest incremental)
387 * and from first fragment to last fragment at each level. If
388 * there is more than one call to FindVolumes, this is not true
397 /* For each of the volume fragments that we read, sort them into
398 * the list of tapes they are on. Sort by tapeid, then tape sequence.
399 * Do from first fragment (where volume begins) to last fragment.
401 for (ve = (vecount - 1); ve >= 0; ve--) {
402 foundvolume = 1; /* Found the volume on this dump */
404 tapeseq = volumeEntries[ve].tapeSeq;
406 /* Determine where in the list of tapes this should go */
407 for (; tle; ptle = tle, tle = tle->next) {
409 (tle->initialDumpID ? tle->
410 initialDumpID : tle->dumpID);
412 /* Sort by tapeids. BUT, we don't want add an entry in the middle
413 * of a dumpset (might split a volume fragmented across tapes).
414 * So make sure we step to next dumpset. tlid is the tapeid of
415 * the last tape we added a volume to. This can happen when an
416 * incremental was appended to a tape created prior its parent-
417 * dump's tape, and needs to be restored after it.
423 } /* Allocate and insert a tape entry */
426 /* Found the tapeid (the dumpset). Check if its the correct
429 else if (tapeid == tid) {
430 if (tapeseq < tle->tapeNumber) {
434 /* Allocate and insert a tape entry */
435 if (tapeseq == tle->tapeNumber) {
438 /* Don't allocate tape entry */
439 foundtape = 1; /* Found dumpset but not the tape */
442 /* Prevously found the tapeid (the dumpset) but this tape
443 * sequence not included (the tapeid has changed). So add
444 * this tape entry to the end of its dumpset.
446 else if (foundtape) {
450 /* Allocate and insert a tape entry */
453 /* Allocate a new tapelist entry if one not found */
455 tle = (struct bc_tapeList *)
456 malloc(sizeof(struct bc_tapeList));
458 com_err(whoami, BC_NOMEM, "");
461 memset(tle, 0, sizeof(struct bc_tapeList));
464 (char *)malloc(strlen(volumeEntries[ve].tape)
466 if (!tle->tapeName) {
468 com_err(whoami, BC_NOMEM, "");
472 strcpy(tle->tapeName, volumeEntries[ve].tape);
473 tle->dumpID = dlevels[lv].DumpId;
474 tle->initialDumpID = dlevels[lv].initialDumpId;
475 tle->tapeNumber = tapeseq;
477 /* Now thread the tape onto the list */
479 tle->next = tapeList;
482 tle->next = ptle->next;
487 (tle->initialDumpID ? tle->initialDumpID : tle->
490 /* Now place the volume fragment into the correct position on
491 * this tapelist entry. Duplicate entries are ignored.
493 for (pti = 0, ti = tle->restoreList; ti;
494 pti = ti, ti = ti->next) {
495 if (volumeEntries[ve].position < ti->position) {
499 /* Allocate an entry */
500 else if (volumeEntries[ve].position ==
503 } /* Duplicate entry */
506 /* If didn't find one, allocate and thread onto list.
507 * Remember the server and partition.
510 ti = (struct bc_tapeItem *)
511 malloc(sizeof(struct bc_tapeItem));
513 com_err(whoami, BC_NOMEM, "");
516 memset(ti, 0, sizeof(struct bc_tapeItem));
519 (char *)malloc(strlen(volumeEntries[ve].name)
521 if (!ti->volumeName) {
523 com_err(whoami, BC_NOMEM, "");
527 strcpy(ti->volumeName, volumeEntries[ve].name);
528 ti->server = vi->server;
529 ti->partition = vi->partition;
530 ti->oid = volumeEntries[ve].id;
531 ti->position = volumeEntries[ve].position;
532 ti->vollevel = voldumplevel;
533 ti->dumplevel = dlevels[lv].level;
534 ti->lastdump = ((lv == 0) ? 1 : 0);
537 ti->next = tle->restoreList;
538 tle->restoreList = ti;
540 ti->next = pti->next;
546 } /* ve: for each returned volume fragment by bcdb_FindVolumes() */
547 } /* last: Multiple calls to bcdb_FindVolumes() */
551 } /* lv: For each dump level */
552 } /* vi: For each volume to search for in the dump hierarchy */
553 } /* di: For each dump */
556 com_err(whoami, 0, "No volumes to restore");
560 if (dumpTaskPtr->dontExecute) {
561 for (tle = tapeList; tle; tle = tle->next) {
562 for (ti = tle->restoreList; ti; ti = ti->next) {
564 (tle->initialDumpID ? tle->initialDumpID : tle->dumpID);
565 strcpy(vname, ti->volumeName);
567 if (dumpTaskPtr->newExt)
568 strcat(vname, dumpTaskPtr->newExt);
570 /* print volume to restore and the tape and position its on */
573 ("Restore volume %s (ID %d) from tape %s (%u), position %d",
574 ti->volumeName, ti->oid, tle->tapeName, tapedumpid,
577 if (strcmp(ti->volumeName, vname) != 0)
578 printf(" as %s", vname);
583 /* print in format recognized by volsetrestore */
587 gethostbyaddr((char *)&haddr, sizeof(haddr), AF_INET);
589 printf("%s", hostPtr->h_name);
591 printf("%u", ti->server);
594 viceName(ti->partition);
596 printf(" %s # as %s; %s (%u); pos %d;", ti->volumeName,
597 vname, tle->tapeName, tapedumpid, ti->position);
600 printf(" %s", ctime(&did));
608 /* Allocate a list of volumes to restore */
610 (struct tc_restoreDesc *)malloc(nentries *
611 sizeof(struct tc_restoreDesc));
613 com_err(whoami, BC_NOMEM, "");
616 memset(tcarray, 0, nentries * sizeof(struct tc_restoreDesc));
618 /* Fill in the array with the list above */
620 for (tle = tapeList; tle; tle = tle->next) {
621 for (ti = tle->restoreList; ti; ti = ti->next) {
622 tcarray[i].origVid = ti->oid; /* means unknown */
624 tcarray[i].flags = 0;
625 if (ti->vollevel == 0)
626 tcarray[i].flags |= RDFLAG_FIRSTDUMP;
628 tcarray[i].flags |= RDFLAG_LASTDUMP;
629 tcarray[i].position = ti->position;
630 tcarray[i].dbDumpId = tle->dumpID;
631 tcarray[i].initialDumpId = tle->initialDumpID;
632 tcarray[i].hostAddr = ti->server; /* just the internet address */
633 tcarray[i].partition = ti->partition;
635 strcpy(tcarray[i].tapeName, tle->tapeName);
636 strcpy(tcarray[i].oldName, ti->volumeName);
637 strcpy(tcarray[i].newName, ti->volumeName);
638 StripBackup(tcarray[i].newName);
639 if (dumpTaskPtr->newExt)
640 strcat(tcarray[i].newName, dumpTaskPtr->newExt);
642 tcarray[i].dumpLevel = ti->dumplevel;
647 printf("Starting restore\n");
649 /* Loop until all of the tape entries are used */
650 for (startentry = 0; startentry < nentries; startentry += todump) {
651 /* Get all the next tape entries with the same port offset */
652 if (dumpTaskPtr->portCount == 0) {
654 todump = nentries - startentry;
655 } else if (dumpTaskPtr->portCount == 1) {
656 port = dumpTaskPtr->portOffset[0];
657 todump = nentries - startentry;
659 level = tcarray[startentry].dumpLevel;
663 dumpTaskPtr->portCount -
664 1 ? level : dumpTaskPtr->portCount - 1)];
666 for (todump = 1; ((startentry + todump) < nentries); todump++) {
667 level = tcarray[startentry + todump].dumpLevel;
671 dumpTaskPtr->portCount -
672 1 ? level : dumpTaskPtr->portCount - 1)];
673 if (port != nextport)
674 break; /* break if we change ports */
678 rpcArray.tc_restoreArray_len = todump;
679 rpcArray.tc_restoreArray_val = &tcarray[startentry];
681 code = ConnectButc(dumpTaskPtr->config, port, &aconn);
685 if (tcarray[startentry].dumpLevel == 0)
686 printf("\nFull restore being processed on port %d\n", port);
688 printf("\nIncremental restore being processed on port %d\n",
692 TC_PerformRestore(aconn, "DumpSetName", &rpcArray,
693 &dumpTaskPtr->dumpID);
695 com_err(whoami, code, "; Failed to start restore");
699 /* setup status monitor node */
700 statusPtr = createStatusNode();
702 statusPtr->port = port;
703 statusPtr->taskId = dumpTaskPtr->dumpID;
704 statusPtr->jobNumber = bc_jobNumber();
705 statusPtr->flags &= ~STARTING; /* clearStatus to examine */
706 if (tcarray[startentry].dumpLevel == 0)
707 sprintf(statusPtr->taskName, "Full Restore");
709 sprintf(statusPtr->taskName, "Incremental Restore");
712 /* Wait until this restore pass is complete before starting the next
713 * pass. Query the statusWatcher for the status
717 newStatusPtr = findStatus(dumpTaskPtr->dumpID);
720 flags & (ABORT_REQUEST | ABORT_SENT | ABORT_DONE |
724 } else if (newStatusPtr->flags & (TASK_DONE)) {
734 rx_DestroyConnection(aconn);
735 aconn = (struct rx_connection *)0;
738 /* free up everything */
741 rx_DestroyConnection(aconn);
746 /* Free the dumpinfo list and its volumes */
747 for (di = dumpinfolist; di; di = ndi) {
748 for (vi = di->volinfolist; vi; vi = nvi) {
758 /* Free the tape lists and items */
759 for (tle = tapeList; tle; tle = ntle) {
760 for (ti = tle->restoreList; ti; ti = nti) {
763 free(ti->volumeName);
772 /* free local-like things we alloacted to save stack space */