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>
22 #include <afs/bubasics.h>
24 #include <afs/com_err.h>
27 #include <afs/vlserver.h>
29 #include "error_macros.h"
30 #include "bucoord_internal.h"
31 #include "bucoord_prototypes.h"
33 extern struct bc_dumpTask bc_dumpTasks[BC_MAXSIMDUMPS];
36 #define MAXTAPESATONCE 10
38 #define HOSTADDR(sockaddr) (sockaddr)->sin_addr.s_addr
40 /* local structure to keep track of volumes and the dumps from which
41 * they should be restored
44 struct dumpinfo *next;
45 struct volinfo *volinfolist;
46 struct volinfo *lastinlist;
48 afs_int32 initialDumpId;
49 afs_int32 parentDumpId;
60 /* local structure used to keep track of a tape, including a list of
61 * volumes to restore from that tape (bc_tapeItem)
64 struct bc_tapeList *next; /* next guy in list */
65 char *tapeName; /* name of the tape */
66 afs_int32 dumpID; /* dump located on this tape */
67 afs_int32 initialDumpID; /* initial dump located on this tape */
68 afs_int32 tapeNumber; /* which tape in the dump */
69 struct bc_tapeItem *restoreList; /* volumes to restore from this tape */
72 /* each tape has a list of volumes to restore; hangs off of a struct
73 bc_tapeList. Kept sorted so that we can read the tape once to do
74 everything we need to do. */
76 struct bc_tapeItem *next;
77 char *volumeName; /* volume to restore */
78 afs_int32 position; /* file slot on this tape */
79 afs_int32 oid; /* id of the volume */
80 afs_int32 dumplevel; /* level # of the containing dump (0 == top level) */
81 afs_int32 vollevel; /* level # of the containing volume (0 == full dump) */
84 int lastdump; /* The last incremental to restore */
87 /* strip .backup from the end of a name */
89 StripBackup(char *aname)
93 if ((j = BackupName(aname))) {
101 BackupName(char *aname)
106 if ((j > 7) && (strcmp(".backup", aname + j - 7) == 0))
108 if ((j > 9) && (strcmp(".readonly", aname + j - 9) == 0))
114 extractTapeSeq(char *tapename)
118 sptr = strrchr(tapename, '.');
131 alph = "abcdefjhijklmnopqrstuvwxyz";
134 viceName(value / 26 - 1);
137 printf("%c", alph[r]);
142 * aindex - index into bc_dumpTasks that describes this dump.
145 bc_Restorer(afs_int32 aindex)
147 struct bc_dumpTask *dumpTaskPtr;
149 char vname[BU_MAXNAMELEN];
150 struct budb_dumpEntry *dumpDescr, dumpDescr1, dumpDescr2;
151 struct budb_volumeEntry *volumeEntries;
152 struct bc_volumeDump *tvol;
153 afs_int32 code = 0, tcode;
154 afs_int32 tapedumpid, parent;
156 afs_int32 nentries = 0;
158 afs_int32 next, ve, vecount;
159 struct bc_tapeItem *ti, *pti, *nti;
160 struct bc_tapeList *tapeList = (struct bc_tapeList *)0;
161 struct bc_tapeList *tle, *ptle, *ntle;
163 afs_int32 tapeid, tid;
165 struct tc_restoreArray rpcArray; /* the rpc structure we use */
166 struct tc_restoreDesc *tcarray = (struct tc_restoreDesc *)0;
168 afs_int32 i, startentry, todump;
169 afs_int32 port, nextport;
170 afs_int32 level, tapeseq;
171 afs_int32 foundvolume, voldumplevel;
172 struct rx_connection *aconn = (struct rx_connection *)0;
173 statusP statusPtr, newStatusPtr;
175 struct dumpinfo *dumpinfolist = NULL;
176 struct dumpinfo *pdi, *ndi, *di, *dlevels;
177 struct volinfo *nvi, *vi;
179 int num_dlevels = 20;
181 afs_uint32 serverAll; /* The server to which all volumes are to be restore to */
182 afs_int32 partitionAll; /* Likewise for partition */
183 struct hostent *hostPtr;
188 dlevels = (struct dumpinfo *) malloc(num_dlevels * sizeof(*dlevels));
190 dumpTaskPtr = &bc_dumpTasks[aindex];
191 serverAll = HOSTADDR(&dumpTaskPtr->destServer);
192 partitionAll = dumpTaskPtr->destPartition;
194 volumeEntries = (struct budb_volumeEntry *)
195 malloc(MAXTAPESATONCE * sizeof(struct budb_volumeEntry));
196 if (!volumeEntries) {
197 afs_com_err(whoami, BC_NOMEM, NULL);
201 /* For each volume to restore, find which dump it's most recent full or
202 * incremental is on and thread onto our dump list (from oldest to newest
203 * dump). Also hang the volume off of the dump (no particular order).
205 for (tvol = dumpTaskPtr->volumes; tvol; tvol = tvol->next) { /*tvol */
206 strcpy(vname, tvol->name);
207 dumpDescr = &dumpDescr1;
208 if (dumpTaskPtr->parentDumpID > 0) /* Told which dump to try */
210 /* Right now, this assumes that all volumes listed will be
211 * from the given dumpID. FIXME
213 code = bcdb_FindDumpByID(dumpTaskPtr->parentDumpID, dumpDescr);
216 afs_com_err(whoami, code, "Couldn't look up info for dump %d\n",
217 dumpTaskPtr->parentDumpID);
220 code = bcdb_FindVolumes(dumpTaskPtr->parentDumpID, vname, volumeEntries,
221 last, &next, MAXTAPESATONCE, &vecount);
224 if (!BackupName(vname))
226 strcat(vname, ".backup");
227 code = bcdb_FindVolumes(dumpTaskPtr->parentDumpID, vname, volumeEntries,
228 last, &next, MAXTAPESATONCE, &vecount);
234 code = bcdb_FindDump(vname, dumpTaskPtr->fromDate, dumpDescr);
235 if (!BackupName(vname)) { /* See if backup volume is there */
236 strcat(vname, ".backup");
237 dumpDescr = &dumpDescr2;
239 code = bcdb_FindDump(vname, dumpTaskPtr->fromDate, dumpDescr);
241 if (code) { /* Can't find backup, go with first results */
242 strcpy(vname, tvol->name);
243 dumpDescr = &dumpDescr1;
245 } else if (!tcode) { /* Both found an entry, go with latest result */
246 if (dumpDescr1.created > dumpDescr2.created) {
247 strcpy(vname, tvol->name);
248 dumpDescr = &dumpDescr1;
255 if (code) { /* If FindDump took an error */
256 afs_com_err(whoami, code, "; Can't find any dump for volume %s",
261 /* look to see if this dump has already been found */
262 for (pdi = 0, di = dumpinfolist; di; pdi = di, di = di->next) {
263 if (di->DumpId < dumpDescr->id) {
266 } else if (di->DumpId == dumpDescr->id) {
271 /* If didn't find it, create one and thread into list */
273 di = calloc(1, sizeof(struct dumpinfo));
275 afs_com_err(whoami, BC_NOMEM, NULL);
279 di->DumpId = dumpDescr->id;
280 di->initialDumpId = dumpDescr->initialDumpID;
281 di->parentDumpId = dumpDescr->parent;
282 di->level = dumpDescr->level;
285 di->next = dumpinfolist;
288 di->next = pdi->next;
293 /* Create one and thread into list */
294 vi = calloc(1, sizeof(struct volinfo));
296 afs_com_err(whoami, BC_NOMEM, NULL);
300 vi->volname = strdup(vname);
303 afs_com_err(whoami, BC_NOMEM, NULL);
308 vi->server = serverAll;
309 vi->partition = partitionAll;
311 vi->server = HOSTADDR(&tvol->server);
312 vi->partition = tvol->partition;
315 /* thread onto end of list */
317 di->volinfolist = vi;
319 di->lastinlist->next = vi;
323 /* For each of the above dumps we found (they could be increments), find
324 * the dump's lineage (up to the full dump).
326 for (di = dumpinfolist; di; di = di->next) {
327 /* Find each of the parent dumps */
328 memcpy(&dlevels[0], di, sizeof(struct dumpinfo));
329 for (lvl = 1, parent = dlevels[0].parentDumpId; parent;
330 parent = dlevels[lvl].parentDumpId, lvl++) {
331 if (lvl >= num_dlevels) { /* running out of dump levels */
332 struct dumpinfo *tdl = dlevels;
334 num_dlevels += num_dlevels; /* double */
335 dlevels = (struct dumpinfo *) malloc(num_dlevels * sizeof(*dlevels));
336 memcpy(dlevels, tdl, (num_dlevels/2) * sizeof(*dlevels));
339 code = bcdb_FindDumpByID(parent, &dumpDescr1);
341 for (vi = di->volinfolist; vi; vi = vi->next) {
342 afs_com_err(whoami, code,
343 "; Can't find parent DumpID %u for volume %s",
344 parent, vi->volname);
349 dlevels[lvl].DumpId = dumpDescr1.id;
350 dlevels[lvl].initialDumpId = dumpDescr1.initialDumpID;
351 dlevels[lvl].parentDumpId = dumpDescr1.parent;
352 dlevels[lvl].level = dumpDescr1.level;
355 /* For each of the volumes that has a dump in this lineage (vi),
356 * find where it is in each dump level (lv) starting at level 0 and
357 * going to higest. Each dump level could contain one or more
358 * fragments (vecount) of the volume (volume fragments span tapes).
359 * Each volume fragment is sorted by tapeid, tape sequence, and tape
362 for (vi = di->volinfolist; vi; vi = vi->next) {
363 tle = tapeList; /* Use for searching the tapelist */
364 ptle = 0; /* The previous tape list entry */
365 tlid = 0; /* tapeid of first entry */
366 /* Volume's dump-level. May not be same as dump's dump-level. This
367 * value gets incremented everytime the volume is found on a dump.
371 /* Do from level 0 dump to highest level dump */
372 for (lv = (lvl - 1); lv >= 0; lv--) {
373 foundvolume = 0; /* If found volume on this dump */
375 (dlevels[lv].initialDumpId ? dlevels[lv].
376 initialDumpId : dlevels[lv].DumpId);
378 /* Cycle through the volume fragments for this volume on the tape */
379 for (last = 0, c = 0; last >= 0; last = next, c++) {
381 bcdb_FindVolumes(dlevels[lv].DumpId, vi->volname,
382 volumeEntries, last, &next,
383 MAXTAPESATONCE, &vecount);
385 /* Volume wasn't found on the tape - so continue.
386 * This can happen when volume doesn't exist during level 0 dump
387 * but exists for incremental dump.
389 if (code == BUDB_ENDOFLIST) {
394 afs_com_err(whoami, code,
395 "; Can't find volume %s in DumpID %u",
396 vi->volname, dlevels[lv].DumpId);
400 /* If we have made the Findvolumes call more than once for the same
401 * volume, then begin the search at the top. This sort assumes that
402 * we are collecting information from lowest level dump (full dump)
403 * to the highest level dump (highest incremental)
404 * and from first fragment to last fragment at each level. If
405 * there is more than one call to FindVolumes, this is not true
414 /* For each of the volume fragments that we read, sort them into
415 * the list of tapes they are on. Sort by tapeid, then tape sequence.
416 * Do from first fragment (where volume begins) to last fragment.
418 for (ve = (vecount - 1); ve >= 0; ve--) {
419 foundvolume = 1; /* Found the volume on this dump */
421 tapeseq = volumeEntries[ve].tapeSeq;
423 /* Determine where in the list of tapes this should go */
424 for (; tle; ptle = tle, tle = tle->next) {
426 (tle->initialDumpID ? tle->
427 initialDumpID : tle->dumpID);
429 /* Sort by tapeids. BUT, we don't want add an entry in the middle
430 * of a dumpset (might split a volume fragmented across tapes).
431 * So make sure we step to next dumpset. tlid is the tapeid of
432 * the last tape we added a volume to. This can happen when an
433 * incremental was appended to a tape created prior its parent-
434 * dump's tape, and needs to be restored after it.
440 } /* Allocate and insert a tape entry */
443 /* Found the tapeid (the dumpset). Check if its the correct
446 else if (tapeid == tid) {
447 if (tapeseq < tle->tapeNumber) {
451 /* Allocate and insert a tape entry */
452 if (tapeseq == tle->tapeNumber) {
455 /* Don't allocate tape entry */
456 foundtape = 1; /* Found dumpset but not the tape */
459 /* Prevously found the tapeid (the dumpset) but this tape
460 * sequence not included (the tapeid has changed). So add
461 * this tape entry to the end of its dumpset.
463 else if (foundtape) {
467 /* Allocate and insert a tape entry */
470 /* Allocate a new tapelist entry if one not found */
472 tle = calloc(1, sizeof(struct bc_tapeList));
474 afs_com_err(whoami, BC_NOMEM, NULL);
478 tle->tapeName = strdup(volumeEntries[ve].tape);
479 if (!tle->tapeName) {
481 afs_com_err(whoami, BC_NOMEM, NULL);
485 tle->dumpID = dlevels[lv].DumpId;
486 tle->initialDumpID = dlevels[lv].initialDumpId;
487 tle->tapeNumber = tapeseq;
489 /* Now thread the tape onto the list */
491 tle->next = tapeList;
494 tle->next = ptle->next;
499 (tle->initialDumpID ? tle->initialDumpID : tle->
502 /* Now place the volume fragment into the correct position on
503 * this tapelist entry. Duplicate entries are ignored.
505 for (pti = 0, ti = tle->restoreList; ti;
506 pti = ti, ti = ti->next) {
507 if (volumeEntries[ve].position < ti->position) {
511 /* Allocate an entry */
512 else if (volumeEntries[ve].position ==
515 } /* Duplicate entry */
518 /* If didn't find one, allocate and thread onto list.
519 * Remember the server and partition.
522 ti = calloc(1, sizeof(struct bc_tapeItem));
524 afs_com_err(whoami, BC_NOMEM, NULL);
528 ti->volumeName = strdup(volumeEntries[ve].name);
529 if (!ti->volumeName) {
531 afs_com_err(whoami, BC_NOMEM, NULL);
535 ti->server = vi->server;
536 ti->partition = vi->partition;
537 ti->oid = volumeEntries[ve].id;
538 ti->position = volumeEntries[ve].position;
539 ti->vollevel = voldumplevel;
540 ti->dumplevel = dlevels[lv].level;
541 ti->lastdump = ((lv == 0) ? 1 : 0);
544 ti->next = tle->restoreList;
545 tle->restoreList = ti;
547 ti->next = pti->next;
553 } /* ve: for each returned volume fragment by bcdb_FindVolumes() */
554 } /* last: Multiple calls to bcdb_FindVolumes() */
558 } /* lv: For each dump level */
559 } /* vi: For each volume to search for in the dump hierarchy */
560 } /* di: For each dump */
563 afs_com_err(whoami, 0, "No volumes to restore");
567 if (dumpTaskPtr->dontExecute) {
568 for (tle = tapeList; tle; tle = tle->next) {
569 for (ti = tle->restoreList; ti; ti = ti->next) {
571 (tle->initialDumpID ? tle->initialDumpID : tle->dumpID);
572 strcpy(vname, ti->volumeName);
574 if (dumpTaskPtr->newExt)
575 strcat(vname, dumpTaskPtr->newExt);
577 /* print volume to restore and the tape and position its on */
580 ("Restore volume %s (ID %d) from tape %s (%u), position %d",
581 ti->volumeName, ti->oid, tle->tapeName, tapedumpid,
584 if (strcmp(ti->volumeName, vname) != 0)
585 printf(" as %s", vname);
590 /* print in format recognized by volsetrestore */
594 gethostbyaddr((char *)&haddr, sizeof(haddr), AF_INET);
596 printf("%s", hostPtr->h_name);
598 printf("%u", ti->server);
601 viceName(ti->partition);
603 printf(" %s # as %s; %s (%u); pos %d;", ti->volumeName,
604 vname, tle->tapeName, tapedumpid, ti->position);
607 printf(" %s", ctime(&did));
615 /* Allocate a list of volumes to restore */
616 tcarray = calloc(nentries, sizeof(struct tc_restoreDesc));
618 afs_com_err(whoami, BC_NOMEM, NULL);
622 /* Fill in the array with the list above */
624 for (tle = tapeList; tle; tle = tle->next) {
625 for (ti = tle->restoreList; ti; ti = ti->next) {
626 tcarray[i].origVid = ti->oid; /* means unknown */
628 tcarray[i].flags = 0;
629 if (ti->vollevel == 0)
630 tcarray[i].flags |= RDFLAG_FIRSTDUMP;
632 tcarray[i].flags |= RDFLAG_LASTDUMP;
633 tcarray[i].position = ti->position;
634 tcarray[i].dbDumpId = tle->dumpID;
635 tcarray[i].initialDumpId = tle->initialDumpID;
636 tcarray[i].hostAddr = ti->server; /* just the internet address */
637 tcarray[i].partition = ti->partition;
639 strcpy(tcarray[i].tapeName, tle->tapeName);
640 strcpy(tcarray[i].oldName, ti->volumeName);
641 strcpy(tcarray[i].newName, ti->volumeName);
642 StripBackup(tcarray[i].newName);
643 if (dumpTaskPtr->newExt)
644 strcat(tcarray[i].newName, dumpTaskPtr->newExt);
646 tcarray[i].dumpLevel = ti->dumplevel;
651 printf("Starting restore\n");
653 /* Loop until all of the tape entries are used */
654 for (startentry = 0; startentry < nentries; startentry += todump) {
655 /* Get all the next tape entries with the same port offset */
656 if (dumpTaskPtr->portCount == 0) {
658 todump = nentries - startentry;
659 } else if (dumpTaskPtr->portCount == 1) {
660 port = dumpTaskPtr->portOffset[0];
661 todump = nentries - startentry;
663 level = tcarray[startentry].dumpLevel;
667 dumpTaskPtr->portCount -
668 1 ? level : dumpTaskPtr->portCount - 1)];
670 for (todump = 1; ((startentry + todump) < nentries); todump++) {
671 level = tcarray[startentry + todump].dumpLevel;
675 dumpTaskPtr->portCount -
676 1 ? level : dumpTaskPtr->portCount - 1)];
677 if (port != nextport)
678 break; /* break if we change ports */
682 rpcArray.tc_restoreArray_len = todump;
683 rpcArray.tc_restoreArray_val = &tcarray[startentry];
685 code = ConnectButc(dumpTaskPtr->config, port, &aconn);
689 if (tcarray[startentry].dumpLevel == 0)
690 printf("\nFull restore being processed on port %d\n", port);
692 printf("\nIncremental restore being processed on port %d\n",
696 TC_PerformRestore(aconn, "DumpSetName", &rpcArray,
697 &dumpTaskPtr->dumpID);
699 afs_com_err(whoami, code, "; Failed to start restore");
703 /* setup status monitor node */
704 statusPtr = createStatusNode();
706 statusPtr->port = port;
707 statusPtr->taskId = dumpTaskPtr->dumpID;
708 statusPtr->jobNumber = bc_jobNumber();
709 statusPtr->flags &= ~STARTING; /* clearStatus to examine */
710 if (tcarray[startentry].dumpLevel == 0)
711 sprintf(statusPtr->taskName, "Full Restore");
713 sprintf(statusPtr->taskName, "Incremental Restore");
716 /* Wait until this restore pass is complete before starting the next
717 * pass. Query the statusWatcher for the status
721 newStatusPtr = findStatus(dumpTaskPtr->dumpID);
724 flags & (ABORT_REQUEST | ABORT_SENT | ABORT_DONE |
728 } else if (newStatusPtr->flags & (TASK_DONE)) {
738 rx_DestroyConnection(aconn);
739 aconn = (struct rx_connection *)0;
742 /* free up everything */
745 rx_DestroyConnection(aconn);
750 /* Free the dumpinfo list and its volumes */
751 for (di = dumpinfolist; di; di = ndi) {
752 for (vi = di->volinfolist; vi; vi = nvi) {
762 /* Free the tape lists and items */
763 for (tle = tapeList; tle; tle = ntle) {
764 for (ti = tle->restoreList; ti; ti = nti) {
767 free(ti->volumeName);
776 /* free local-like things we alloacted to save stack space */