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/param.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
33 #include <afs/bubasics.h>
35 #include <afs/com_err.h>
36 #include <lock.h> /* for checking TC_ABORTFAILED. PA */
37 #include <afs/tcdata.h> /* for checking TC_ABORTFAILED. PA */
41 #include "error_macros.h"
43 struct bc_dumpTask bc_dumpTasks[BC_MAXSIMDUMPS];
45 extern struct bc_config *bc_globalConfig;
46 extern char *bc_CopyString();
47 extern void bc_HandleMisc();
49 extern char *tailCompPtr();
50 extern statusP createStatusNode();
51 extern afs_int32 bc_jobNumber();
53 extern afs_int32 lastTaskCode;
56 #define HOSTADDR(sockaddr) (sockaddr)->sin_addr.S_un.S_addr
58 #define HOSTADDR(sockaddr) (sockaddr)->sin_addr.s_addr
62 * called (indirectly) to make a dump
64 * aindex - index into dumpTask array, contains all the information
65 * relevant to the dump
69 struct rx_connection *tconn;
70 struct tc_dumpStat dumpStat;
71 register struct bc_volumeDump *tde;
72 afs_int32 count, port;
74 struct tc_dumpDesc *volDesc = 0;
75 struct tc_dumpArray volArray;
79 struct tc_dumpInterface dumpInterface;
80 struct tc_dumpInterface *tcdiPtr = &dumpInterface;
81 register struct bc_dumpTask *dumpTaskPtr;
83 register afs_int32 code = 0;
85 dumpTaskPtr = &bc_dumpTasks[aindex];
87 if (!dumpTaskPtr->portOffset || (dumpTaskPtr->portCount == 0))
90 port = dumpTaskPtr->portOffset[0];
92 code = ConnectButc(dumpTaskPtr->config, port, &tconn);
93 if (code) return(code);
95 /* count number of volumes to be dumped and
96 * build array of volumes to be sent to backup system
98 for (count=0, tde=dumpTaskPtr->volumes; tde; tde=tde->next, count++);
99 volDesc = (struct tc_dumpDesc *) malloc(count*sizeof(struct tc_dumpDesc));
102 com_err(whoami,BC_NOMEM,"");
106 for (count=0, tde=dumpTaskPtr->volumes; tde; tde=tde->next, count++)
108 strcpy(volDesc[count].name, tde->name);
109 volDesc[count].vid = tde->vid;
110 volDesc[count].vtype = tde->volType;
111 volDesc[count].partition = tde->partition;
112 volDesc[count].hostAddr = HOSTADDR(&tde->server); /* the internet address */
113 volDesc[count].date = tde->date;
114 volDesc[count].cloneDate = tde->cloneDate; /* Not yet known */
117 volArray.tc_dumpArray_len = count; /* element count */
118 volArray.tc_dumpArray_val = volDesc; /* and data */
120 baseNamePtr = tailCompPtr(dumpTaskPtr->dumpName);
122 /* setup the interface structure */
123 bzero(tcdiPtr, sizeof(*tcdiPtr));
126 strcpy(tcdiPtr->dumpPath, dumpTaskPtr->dumpName);
127 strcpy(tcdiPtr->volumeSetName, dumpTaskPtr->volSetName);
130 strcpy(tcdiPtr->tapeSet.format, dumpTaskPtr->volSetName);
131 strcat(tcdiPtr->tapeSet.format, ".");
132 strcat(tcdiPtr->tapeSet.format, baseNamePtr);
133 strcat(tcdiPtr->tapeSet.format, ".%d");
134 tcdiPtr->tapeSet.a = 1;
135 tcdiPtr->tapeSet.b = 1;
136 tcdiPtr->tapeSet.maxTapes = 1000000000;
137 tcdiPtr->tapeSet.expDate = dumpTaskPtr->expDate; /* PA */
138 tcdiPtr->tapeSet.expType = dumpTaskPtr->expType;
140 /* construct dump set name */
141 strcpy(tcdiPtr->dumpName, dumpTaskPtr->volSetName);
142 strcat(tcdiPtr->dumpName, ".");
143 strcat(tcdiPtr->dumpName, baseNamePtr);
145 tcdiPtr->parentDumpId = dumpTaskPtr->parentDumpID;
146 tcdiPtr->dumpLevel = dumpTaskPtr->dumpLevel;
147 tcdiPtr->doAppend = dumpTaskPtr->doAppend;
149 /* start the dump on the tape coordinator */
150 printf("Starting dump\n");
151 code = TC_PerformDump(tconn, tcdiPtr, &volArray, &dumpTaskPtr->dumpID);
154 com_err(whoami, code, "; Failed to start dump");
158 com_err(whoami,0,"Task %u: Dump (%s)", dumpTaskPtr->dumpID, tcdiPtr->dumpName);
160 /* create status monitor block */
161 statusPtr = createStatusNode();
163 statusPtr->taskId = dumpTaskPtr->dumpID;
164 statusPtr->port = port;
165 statusPtr->jobNumber = bc_jobNumber();
166 statusPtr->volsTotal = volArray.tc_dumpArray_len;
167 statusPtr->flags &= ~STARTING;
168 sprintf(statusPtr->taskName, "Dump (%s.%s)", dumpTaskPtr->volSetName, baseNamePtr);
172 /* locally allocated resources */
173 if (volDesc) free(volDesc);
176 rx_DestroyConnection(tconn);
181 /* freeDumpTaskVolumeList
182 * free the list of volumes used for dumps
185 freeDumpTaskVolumeList(vdptr)
186 struct bc_volumeDump *vdptr;
188 struct bc_volumeDump *nextVdPtr;
192 nextVdPtr = vdptr->next;
203 * The other half of the dump/restore create process call. In bc_StartDmpRst,
204 * we allocated a dumpTask entry. Here we do the task and then free the entry.
206 bc_DmpRstStart(aindex)
209 register struct bc_dumpTask *tdump;
210 register afs_int32 code;
212 tdump = &bc_dumpTasks[aindex];
214 code = (tdump->callProc)(aindex);
215 if (code) lastTaskCode = code;
217 /* Cleanup allocated data structures */
218 freeDumpTaskVolumeList(tdump->volumes);
220 if (tdump->dumpName) free(tdump->dumpName);
221 if (tdump->newExt) free(tdump->newExt);
222 if (tdump->volSetName) free(tdump->volSetName);
223 if (tdump->portOffset) free(tdump->portOffset);
224 tdump->flags &= ~BC_DI_INUSE;
230 * function to start dump running. Packages the relevant information
231 * (from params) into any free dumpTask structure (globally allocated),
232 * and then invokes bc_DmpRstStart to do the work, passing it a single
233 * parameter, the index into the dumpTask array.
236 * aconfig - normally is bc_globalConfig
237 * aproc - bc_Dumper for doing dumps
238 * bc_Restorer for doing restores
241 bc_StartDmpRst(aconfig, adname, avname, avolsToDump, adestServer, adestPartition,
242 afromDate, anewExt, aoldFlag, aparent, alevel, aproc, ports,
243 portCount, dsptr, append, dontExecute)
244 struct bc_config *aconfig;
247 struct bc_volumeDump *avolsToDump;
248 struct sockaddr_in *adestServer;
249 afs_int32 adestPartition;
253 afs_int32 aparent, alevel;
257 struct bc_dumpSchedule *dsptr;
258 int append, dontExecute;
261 register afs_int32 code;
264 struct bc_volumeDump *tvol, *temp;
266 for (i=0; i<BC_MAXSIMDUMPS; i++)
267 if (!(bc_dumpTasks[i].flags & BC_DI_INUSE)) break;
269 if (i >= BC_MAXSIMDUMPS)
271 com_err(whoami, BC_NOTLOCKED,
272 "All of the dump/restore slots are in use, try again later");
273 return(BC_NOTLOCKED);
276 bzero(&bc_dumpTasks[i], sizeof(struct bc_dumpTask));
277 bc_dumpTasks[i].callProc = aproc;
278 bc_dumpTasks[i].config = aconfig;
279 bc_dumpTasks[i].volumes = avolsToDump;
280 bc_dumpTasks[i].flags = BC_DI_INUSE;
281 bc_dumpTasks[i].dumpName = bc_CopyString(adname);
282 bc_dumpTasks[i].volSetName = bc_CopyString(avname);
283 bc_dumpTasks[i].newExt = bc_CopyString(anewExt);
284 bc_dumpTasks[i].dumpLevel = alevel;
285 bc_dumpTasks[i].parentDumpID = aparent;
286 bc_dumpTasks[i].oldFlag = aoldFlag;
287 bc_dumpTasks[i].fromDate = afromDate;
288 bc_dumpTasks[i].destPartition = adestPartition;
289 bc_dumpTasks[i].portOffset = ports;
290 bc_dumpTasks[i].portCount = portCount;
291 bc_dumpTasks[i].doAppend = append;
292 bc_dumpTasks[i].dontExecute = dontExecute;
296 /* This should be specified for dumps, but will be 0 for restores */
297 bc_dumpTasks[i].expDate = dsptr->expDate;
298 bc_dumpTasks[i].expType = dsptr->expType;
301 bcopy(adestServer, &bc_dumpTasks[i].destServer, sizeof(struct sockaddr_in));
303 bzero(&bc_dumpTasks[i].destServer, sizeof(struct sockaddr_in));
305 code = LWP_CreateProcess(bc_DmpRstStart, 20480, LWP_NORMAL_PRIORITY, i, "helper", &junk);
309 com_err(whoami,code,"; Can't start thread");
311 /* Cleanup allocated data structures */
312 freeDumpTaskVolumeList(bc_dumpTasks[i].volumes);
313 bc_dumpTasks[i].dumpID = 0;
314 if (bc_dumpTasks[i].dumpName) free(bc_dumpTasks[i].dumpName);
315 if (bc_dumpTasks[i].newExt) free(bc_dumpTasks[i].newExt);
316 if (bc_dumpTasks[i].volSetName) free(bc_dumpTasks[i].volSetName);
317 if (bc_dumpTasks[i].portOffset) free(bc_dumpTasks[i].portOffset);
318 bc_dumpTasks[i].flags &= ~BC_DI_INUSE;
327 * Returns the dump slot of the dump with dumpID
329 * dumpID - id to look for
330 * port - portoffset for tape coordinator
332 * 0-n - i.e. 0 or positive number, is the dump slot
333 * -1 - failed to find dumpID
337 bc_FindDumpSlot(dumpID, port)
343 for ( i = 0; i < BC_MAXSIMDUMPS; i++ )
345 if ( (bc_dumpTasks[i].flags & BC_DI_INUSE) &&
346 (bc_dumpTasks[i].dumpID == dumpID) &&
347 ((afs_int32)bc_dumpTasks[i].portOffset == port) )
357 * opens a connection to the tape coordinator and requests that it
361 bc_LabelTape (afsname, pname, size, config, port)
362 char *afsname, *pname;
363 struct bc_config *config;
367 struct rx_connection *tconn;
369 struct tc_tapeLabel label;
373 code = ConnectButc(config, port, &tconn);
374 if (code) return(code);
376 bzero(&label,sizeof(label));
378 strcpy(label.afsname, afsname);
380 strcpy(label.pname, (strcmp(pname,"")?pname:"<NULL>"));
383 code = TC_LabelTape(tconn, &label, &taskId);
386 com_err(whoami,code,"; Failed to start labeltape");
390 /* create status monitor block */
391 statusPtr = createStatusNode();
393 statusPtr->taskId = taskId;
394 statusPtr->port = port;
395 statusPtr->jobNumber = bc_jobNumber();
396 /* statusPtr->flags |= SILENT; */ /* don't report termination */
397 statusPtr->flags &= ~STARTING; /* ok to examine */
399 sprintf(statusPtr->taskName, "Labeltape (%s)",
400 (pname ? pname : (afsname ? afsname : "<NULL>")));
407 * open a connection to the tape coordinator and read the label on
411 bc_ReadLabel(config,port)
412 struct bc_config *config;
415 struct rx_connection *tconn;
416 struct tc_tapeLabel label;
421 code = ConnectButc(config, port, &tconn);
422 if (code) return(code);
424 bzero(&label,sizeof(label));
425 code = TC_ReadLabel(tconn, &label, &taskId);
427 if (code == BUTM_NOLABEL) {
428 printf("Tape read was unlabelled\n");
431 com_err(whoami,code,"; Failed to start readlabel");
435 if (strcmp(label.pname,""))
437 else if (strcmp(label.afsname,""))
438 tname = label.afsname;
441 printf("Tape read was labelled : <NULL> size : %u\n",label.size);
443 else if (!label.tapeId) {
444 printf("Tape read was labelled : %s size : %lu Kbytes\n", tname, label.size);
447 printf("Tape read was labelled : %s (%lu) size : %lu Kbytes\n",
448 tname, label.tapeId, label.size);
454 bc_ScanDumps(config, dbAddFlag, port)
455 struct bc_config *config;
459 struct rx_connection *tconn;
464 code = ConnectButc(config, port, &tconn);
465 if (code) return(code);
467 code = TC_ScanDumps(tconn, dbAddFlag, &taskId);
470 com_err(whoami, code, "; Failed to start scantape");
474 /* create status monitor block */
475 statusPtr = createStatusNode();
477 statusPtr->taskId = taskId;
478 statusPtr->port = port;
479 statusPtr->jobNumber = bc_jobNumber();
480 statusPtr->flags &= ~STARTING; /* ok to examine */
481 sprintf(statusPtr->taskName, "Scantape");
491 /* get a connection to the tape controller */
493 bc_GetConn (aconfig, aport, tconn)
494 struct bc_config *aconfig;
496 struct rx_connection **tconn;
501 static struct rx_securityClass *rxsc;
502 struct bc_hostEntry *te;
504 *tconn = (struct rx_connection *)0;
506 /* use non-secure connections to butc */
508 rxsc = (struct rx_securityClass *) rxnull_NewClientSecurityObject();
509 if (!rxsc || !aconfig) return(-1);
511 for (te = aconfig->tapeHosts; te; te = te->next)
513 if ( te->portOffset == aport )
515 /* found the right port */
516 host = te->addr.sin_addr.s_addr;
517 if (!host) return (BC_NOHOSTENTRY); /* gethostbyname in bc_ParseHosts failed */
519 port = htons(BC_TAPEPORT + aport);
521 /* servers is 1; sec index is 0 */
522 *tconn = rx_NewConnection(host, port, 1, rxsc, 0);
523 return((*tconn ? 0 : -1));
526 return(BC_NOHOSTENTRY);
530 * make sure we are talking to a compatible butc process.
533 * -1 - not compatible
536 CheckTCVersion(tconn)
537 struct rx_connection *tconn;
539 struct tc_tcInfo tci;
542 code = TC_TCInfo(tconn, &tci);
543 if (code) return(code);
545 if (tci.tcVersion != CUR_BUTC_VERSION)
546 return(BC_VERSIONFAIL);
550 ConnectButc(config, port, tconn)
551 struct bc_config *config;
553 struct rx_connection **tconn;
557 code = bc_GetConn(config, port, tconn);
560 com_err(whoami,code,"; Can't connect to tape coordinator at port %d", port);
564 code = CheckTCVersion(*tconn);
567 rx_DestroyConnection(*tconn);
569 if (code == BC_VERSIONFAIL)
570 com_err(whoami, code, "; Backup and butc are not the same version");
572 com_err(whoami, code, "; Can't access tape coordinator at port %d", port);