b4de99441be0d026686419dea896b4f908f2a9be
[openafs.git] / src / bucoord / commands.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
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
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12 #include <afs/stds.h>
13
14 #include <roken.h>
15
16 #ifdef HAVE_POSIX_REGEX         /* use POSIX regexp library */
17 #include <regex.h>
18 #endif
19
20 #include <afs/cmd.h>
21 #include <afs/com_err.h>
22 #include <afs/afsutil.h>
23 #include <afs/budb.h>
24 #include <afs/budb_prototypes.h>
25 #include <afs/butc.h>
26 #include <afs/bubasics.h>       /* PA */
27 #include <afs/volser.h>
28 #include <afs/voldefs.h>        /* PA */
29 #include <afs/vldbint.h>        /* PA */
30 #include <afs/ktime.h>          /* PA */
31 #include <ubik.h>
32 #include <lock.h>
33 #include <afs/tcdata.h>
34 #include <afs/butx.h>
35 #include <afs/vsutils_prototypes.h>
36
37 #include "bc.h"
38 #include "error_macros.h"
39 #include "bucoord_internal.h"
40 #include "bucoord_prototypes.h"
41
42 extern struct bc_config *bc_globalConfig;
43 extern struct bc_dumpTask bc_dumpTasks[BC_MAXSIMDUMPS];
44 extern struct ubik_client *cstruct;
45 extern char *whoami;
46
47 char *loadFile;
48 extern afs_int32 lastTaskCode;
49
50 #define HOSTADDR(sockaddr) (sockaddr)->sin_addr.s_addr
51
52 static int EvalVolumeSet1(struct bc_config *aconfig, struct bc_volumeSet *avs,
53                           struct bc_volumeDump **avols,
54                           struct ubik_client *uclient);
55
56 static int EvalVolumeSet2(struct bc_config *aconfig, struct bc_volumeSet *avs,
57                           struct bc_volumeDump **avols,
58                           struct ubik_client *uclient);
59 static int DBLookupByVolume(char *volumeName);
60
61 int
62 bc_EvalVolumeSet(struct bc_config *aconfig,
63                  struct bc_volumeSet *avs,
64                  struct bc_volumeDump **avols,
65                  struct ubik_client *uclient)
66 {                               /*bc_EvalVolumeSet */
67     int code = -1;
68     static afs_int32 use = 2;
69
70     if (use == 2) {             /* Use EvalVolumeSet2() */
71         code = EvalVolumeSet2(aconfig, avs, avols, uclient);
72         if (code == RXGEN_OPCODE)
73             use = 1;
74     }
75     if (use == 1) {             /* Use EvalVolumeSet1() */
76         code = EvalVolumeSet1(aconfig, avs, avols, uclient);
77     }
78     return code;
79 }                               /*bc_EvalVolumeSet */
80
81 struct partitionsort {
82     afs_int32 part;
83     struct bc_volumeDump *vdlist;
84     struct bc_volumeDump *lastvdlist;
85     struct bc_volumeDump *dupvdlist;
86     struct bc_volumeEntry *vole;
87     struct partitionsort *next;
88 };
89 struct serversort {
90     afs_uint32 ipaddr;
91     struct partitionsort *partitions;
92     struct serversort *next;
93 };
94
95 afs_int32
96 getSPEntries(afs_uint32 server, afs_int32 partition,
97              struct serversort **serverlist,
98              struct serversort **ss,
99              struct partitionsort **ps)
100 {
101     if (!(*ss) || ((*ss)->ipaddr != server)) {
102         *ps = 0;
103         for ((*ss) = *serverlist; (*ss); *ss = (*ss)->next) {
104             if ((*ss)->ipaddr == server)
105                 break;
106         }
107     }
108     /* No server entry added. Add one */
109     if (!(*ss)) {
110         *ss = (struct serversort *)malloc(sizeof(struct serversort));
111         if (!(*ss)) {
112             afs_com_err(whoami, BC_NOMEM, NULL);
113             *ss = 0;
114             return (BC_NOMEM);
115         }
116         memset(*ss, 0, sizeof(struct serversort));
117         (*ss)->ipaddr = server;
118         (*ss)->next = *serverlist;
119         *serverlist = *ss;
120     }
121
122
123     if (!(*ps) || ((*ps)->part != partition)) {
124         for (*ps = (*ss)->partitions; *ps; *ps = (*ps)->next) {
125             if ((*ps)->part == partition)
126                 break;
127         }
128     }
129     /* No partition entry added. Add one */
130     if (!(*ps)) {
131         *ps = (struct partitionsort *)malloc(sizeof(struct partitionsort));
132         if (!(*ps)) {
133             afs_com_err(whoami, BC_NOMEM, NULL);
134             free(*ss);
135             *ps = 0;
136             *ss = 0;
137             return (BC_NOMEM);
138         }
139         memset(*ps, 0, sizeof(struct partitionsort));
140         (*ps)->part = partition;
141         (*ps)->next = (*ss)->partitions;
142         (*ss)->partitions = *ps;
143     }
144     return 0;
145 }
146
147 afs_int32
148 randSPEntries(struct serversort *serverlist,
149               struct bc_volumeDump **avols)
150 {
151     struct serversort *ss, **pss;
152     struct partitionsort *ps, **pps;
153     afs_int32 r;
154     afs_int32 scount, pcount;
155
156     *avols = 0;
157
158     /* Seed random number generator */
159     r = time(0) + getpid();
160     srand(r);
161
162     /* Count number of servers, remove one at a time */
163     for (scount = 0, ss = serverlist; ss; ss = ss->next, scount++);
164     for (; scount; scount--) {
165         /* Pick a random server in list and remove it */
166         r = (rand() >> 4) % scount;
167         for (pss = &serverlist, ss = serverlist; r;
168              pss = &ss->next, ss = ss->next, r--);
169         *pss = ss->next;
170
171         /* Count number of partitions, remove one at a time */
172         for (pcount = 0, ps = ss->partitions; ps; ps = ps->next, pcount++);
173         for (; pcount; pcount--) {
174             /* Pick a random parition in list and remove it */
175             r = (rand() >> 4) % pcount;
176             for (pps = &ss->partitions, ps = ss->partitions; r;
177                  pps = &ps->next, ps = ps->next, r--);
178             *pps = ps->next;
179
180             ps->lastvdlist->next = *avols;
181             *avols = ps->vdlist;
182             free(ps);
183         }
184         free(ss);
185     }
186     return 0;
187 }
188
189 static int
190 EvalVolumeSet2(struct bc_config *aconfig,
191                struct bc_volumeSet *avs,
192                struct bc_volumeDump **avols,
193                struct ubik_client *uclient)
194 {                               /*EvalVolumeSet2 */
195     struct bc_volumeEntry *tve;
196     struct bc_volumeDump *tavols;
197     struct VldbListByAttributes attributes;
198     afs_int32 si, nsi;          /* startIndex and nextStartIndex */
199     afs_int32 nentries, e, ei, et, add, l;
200     nbulkentries bulkentries;
201     struct nvldbentry *entries = 0;
202     struct bc_volumeDump *tvd;
203     afs_int32 code = 0, tcode;
204     afs_int32 count = 0;
205     struct serversort *servers = 0, *ss = 0;
206     struct partitionsort *ps = 0;
207
208     *avols = (struct bc_volumeDump *)0;
209     bulkentries.nbulkentries_len = 0;
210     bulkentries.nbulkentries_val = 0;
211
212     /* For each of the volume set entries - collect the volumes that match it */
213     for (tve = avs->ventries; tve; tve = tve->next) {
214         /* Put together a call to the vlserver for this vlentry. The
215          * performance gain is from letting the vlserver expand the
216          * volumeset and not this routine.
217          */
218         attributes.Mask = 0;
219         if (tve->server.sin_addr.s_addr) {      /* The server */
220             attributes.Mask |= VLLIST_SERVER;
221             attributes.server = tve->server.sin_addr.s_addr;
222         }
223         if (tve->partition != -1) {     /* The partition */
224             attributes.Mask |= VLLIST_PARTITION;
225             attributes.partition = tve->partition;
226         }
227
228         /* Now make the call to the vlserver */
229         for (si = 0; si != -1; si = nsi) {
230             nentries = 0;
231             bulkentries.nbulkentries_len = 0;
232             bulkentries.nbulkentries_val = 0;
233             nsi = -1;
234             tcode =
235                 ubik_VL_ListAttributesN2(uclient, 0, &attributes,
236                           tve->name, si, &nentries, &bulkentries, &nsi);
237             if (tcode)
238                 ERROR(tcode);
239
240             /* The 3.4 vlserver has a VL_ListAttributesN2() RPC call, but
241              * it is not complete. The only way to tell if it is not complete
242              * is if et == 0 (which we check for below). Also, if the call didn't
243              * match any entries, then we don't know what version the vlserver
244              * is. In both cases, we return RXGEN_OPCODE and the calling routine
245              * will switch to the EvalVolumeSet1() call.
246              */
247             if (nentries == 0)
248                 ERROR(RXGEN_OPCODE);    /* Use EvalVolumeSet1 */
249
250             if (nentries < 0)
251                 nentries = 0;
252             if (nentries > bulkentries.nbulkentries_len)
253                 nentries = bulkentries.nbulkentries_len;
254
255             /* Step through each entry and add it to the list of volumes */
256             entries = bulkentries.nbulkentries_val;
257             for (e = 0; e < nentries; e++) {
258                 ei = entries[e].matchindex & 0xffff;
259                 et = (entries[e].matchindex >> 16) & 0xffff;
260                 switch (et) {
261                 case ITSRWVOL:{
262                         et = RWVOL;
263                         break;
264                     }
265                 case ITSBACKVOL:{
266                         et = BACKVOL;
267                         break;
268                     }
269                 case ITSROVOL:{
270                         et = ROVOL;
271                         break;
272                     }
273                 default:
274                     ERROR(RXGEN_OPCODE);        /* Use EvalVolumeSet1 */
275                 }
276
277                 /* Find server and partiton structure to hang the entry off of */
278                 tcode =
279                     getSPEntries(entries[e].serverNumber[ei],
280                                  entries[e].serverPartition[ei], &servers,
281                                  &ss, &ps);
282                 if (tcode) {
283                     afs_com_err(whoami, tcode, NULL);
284                     ERROR(tcode);
285                 }
286
287                 /* Detect if this entry should be added (not a duplicate).
288                  * Use ps->dupvdlist and ps->vole to only search volumes from
289                  * previous volume set entries.
290                  */
291                 add = 1;
292                 if (tve != avs->ventries) {
293                     l = strlen(entries[e].name);
294                     if (ps->vole != tve) {
295                         ps->vole = tve;
296                         ps->dupvdlist = ps->vdlist;
297                     }
298                     for (tavols = ps->dupvdlist; add && tavols;
299                          tavols = tavols->next) {
300                         if (strncmp(tavols->name, entries[e].name, l) == 0) {
301                             if ((strcmp(&entries[e].name[l], ".backup") == 0)
302                                 || (strcmp(&entries[e].name[l], ".readonly")
303                                     == 0)
304                                 || (strcmp(&entries[e].name[l], "") == 0))
305                                 add = 0;
306                         }
307                     }
308                 }
309
310                 if (add) {
311                     /* Allocate a volume dump structure and its name */
312                     tvd = (struct bc_volumeDump *)
313                         malloc(sizeof(struct bc_volumeDump));
314                     if (!tvd) {
315                         afs_com_err(whoami, BC_NOMEM, NULL);
316                         ERROR(BC_NOMEM);
317                     }
318                     memset(tvd, 0, sizeof(*tvd));
319
320                     tvd->name = (char *)malloc(strlen(entries[e].name) + 10);
321                     if (!(tvd->name)) {
322                         afs_com_err(whoami, BC_NOMEM, NULL);
323                         free(tvd);
324                         ERROR(BC_NOMEM);
325                     }
326
327                     /* Fill it in and thread onto avols list */
328                     strcpy(tvd->name, entries[e].name);
329                     if (et == BACKVOL)
330                         strcat(tvd->name, ".backup");
331                     else if (et == ROVOL)
332                         strcat(tvd->name, ".readonly");
333                     tvd->vid = entries[e].volumeId[et];
334                     tvd->entry = tve;
335                     tvd->volType = et;
336                     tvd->partition = entries[e].serverPartition[ei];
337                     tvd->server.sin_addr.s_addr = entries[e].serverNumber[ei];
338                     tvd->server.sin_port = 0;   /* default FS port */
339                     tvd->server.sin_family = AF_INET;
340 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
341                     tvd->server.sin_len = sizeof(struct sockaddr_in);
342 #endif
343
344                     /* String tvd off of partition struct */
345                     tvd->next = ps->vdlist;
346                     ps->vdlist = tvd;
347                     if (!tvd->next)
348                         ps->lastvdlist = tvd;
349
350                     count++;
351                 }
352             }
353
354             /* Free memory allocated during VL call */
355             if (bulkentries.nbulkentries_val) {
356                 free((char *)bulkentries.nbulkentries_val);
357                 bulkentries.nbulkentries_val = 0;
358                 entries = 0;
359             }
360         }
361     }
362
363     /* Randomly link the volumedump entries together */
364     randSPEntries(servers, avols);
365     fprintf(stderr, "Total number of volumes : %u\n", count);
366
367   error_exit:
368     if (bulkentries.nbulkentries_val) {
369         free((char *)bulkentries.nbulkentries_val);
370     }
371     return (code);
372 }                               /*EvalVolumeSet2 */
373
374 /*-----------------------------------------------------------------------------
375  * EvalVolumeSetOld
376  *
377  * Description:
378  *      Takes the entries in a volumeset and expands them into a list of
379  *      volumes. Every VLDB volume entry is looked at and compared to the
380  *      volumeset entries.
381  *
382  *      When matching a VLDB volume entry to a volumeset entry,
383  *       1. If the RW volume entry matches, that RW volume is used.
384  *       2. Otherwise, if the BK volume entry matches, the BK volume is used.
385  *       3. Finally, if the RO volume entry matches, the RO volume is used.
386  *      For instance: A volumeset entry of ".* .* user.t.*" will match volume
387  *                    "user.troy" and "user.troy.backup". The rules will use
388  *                    the RW volume "user.troy".
389  *
390  *      When a VLDB volume entry matches a volumeset entry (be it RW, BK or RO),
391  *      that volume is used and matches against any remaining volumeset entries
392  *      are not even done.
393  *      For instance: A 1st volumeset entry ".* .* .*.backup" will match with
394  *                    "user.troy.backup". Its 2nd volumeset entry ".* .* .*"
395  *                    would have matched its RW volume "user.troy", but the first
396  *                    match is used and the second match isn't even done.
397  *
398  * Arguments:
399  *      aconfig : Global configuration info.
400  *      avs     :
401  *      avols   : Ptr to linked list of entries describing volumes to dump.
402  *      uclient : Ptr to Ubik client structure.
403  *
404  * Returns:
405  *      0 on successful volume set evaluation,
406  *      Lower-level codes otherwise.
407  *
408  * Environment:
409  *      Expand only the single volume set provided (avs); don't go down its chain.
410  *
411  * Side Effects:
412  *      None.
413  *-----------------------------------------------------------------------------
414  */
415 static int
416 EvalVolumeSet1(struct bc_config *aconfig,
417                struct bc_volumeSet *avs,
418                struct bc_volumeDump **avols,
419                struct ubik_client *uclient)
420 {                               /*EvalVolumeSet1 */
421     afs_int32 code;             /*Result of various calls */
422     struct bc_volumeDump *tvd;  /*Ptr to new dump instance */
423     struct bc_volumeEntry *tve, *ctve;  /*Ptr to new volume entry instance */
424     char patt[256];             /*Composite regex; also, target string */
425     int volType = 0;            /*Type of volume that worked */
426     afs_int32 index;            /*Current VLDB entry index */
427     afs_int32 count;            /*Needed by VL_ListEntry() */
428     afs_int32 next_index;       /*Next index to list */
429     struct vldbentry entry;     /*VLDB entry */
430     int srvpartpair;            /*Loop counter: server/partition pair */
431     afs_int32 total = 0;
432     int found;
433     int foundentry = 0;
434     struct serversort *servers = 0, *ss = 0;
435     struct partitionsort *ps = 0;
436 #ifdef HAVE_POSIX_REGEX
437     regex_t re;
438     int need_regfree = 0;
439 #else
440     char *errm;
441 #endif
442
443     *avols = (struct bc_volumeDump *)0;
444     ctve = (struct bc_volumeEntry *)0;  /* no compiled entry */
445
446     /* For each vldb entry.
447      * Variable next_index is set to the index of the next VLDB entry
448      * in the enumeration.
449      */
450     for (index = 0; 1; index = next_index) {    /*w */
451         memset(&entry, 0, sizeof(entry));
452         code = ubik_VL_ListEntry(uclient,       /*Ubik client structure */
453                          0,     /*Ubik flags */
454                          index, /*Current index */
455                          &count,        /*Ptr to working variable */
456                          &next_index,   /*Ptr to next index value to list */
457                          &entry);       /*Ptr to entry to fill */
458         if (code)
459             return code;
460         if (!next_index)
461             break;              /* If the next index is invalid, bail out now. */
462
463         /* For each entry in the volume set */
464         found = 0;              /* No match in volume set yet */
465         for (tve = avs->ventries; tve; tve = tve->next) {       /*ve */
466             /* for each server in the vldb entry */
467             for (srvpartpair = 0; srvpartpair < entry.nServers; srvpartpair++) {        /*s */
468                 /* On the same server */
469                 if (tve->server.sin_addr.s_addr
470                     && !VLDB_IsSameAddrs(tve->server.sin_addr.s_addr,
471                                          entry.serverNumber[srvpartpair],
472                                          &code)) {
473                     if (code)
474                         return (code);
475                     continue;
476                 }
477
478
479                 /* On the same partition */
480                 if ((tve->partition != -1)
481                     && (tve->partition != entry.serverPartition[srvpartpair]))
482                     continue;
483
484                 /* If the volume entry is not compiled, then compile it */
485                 if (ctve != tve) {
486                     sprintf(patt, "^%s$", tve->name);
487 #ifdef HAVE_POSIX_REGEX
488                     if (regcomp(&re, patt, REG_NOSUB) != 0) {
489                       afs_com_err(whoami, 0, "Can't compile regular expression '%s'", patt);
490                       return (-1);
491                     }
492                     need_regfree = 1;
493 #else
494                     errm = (char *)re_comp(patt);
495                     if (errm) {
496                         afs_com_err(whoami, 0,
497                                 "Can't compile regular expression '%s': %s",
498                                 patt, errm);
499                         return (-1);
500                     }
501 #endif
502                     ctve = tve;
503                 }
504
505                 /* If the RW name matches the volume set entry, take
506                  * it and exit. First choice is to use the RW volume.
507                  */
508                 if (entry.serverFlags[srvpartpair] & ITSRWVOL) {
509                     if (entry.flags & RW_EXISTS) {
510                         sprintf(patt, "%s", entry.name);
511 #ifdef HAVE_POSIX_REGEX
512                         code = regexec(&re, patt, 0, NULL, 0);
513                         if (code == 0) {
514 #else
515                         code = re_exec(patt);
516                         if (code == 1) {
517 #endif
518                             found = 1;
519                             foundentry = srvpartpair;
520                             volType = RWVOL;
521                             break;
522                         }
523                     }
524
525                     /* If the BK name matches the volume set entry, take
526                      * it and exit. Second choice is to use the BK volume.
527                      */
528                     if (entry.flags & BACK_EXISTS) {
529                         sprintf(patt, "%s.backup", entry.name);
530 #ifdef HAVE_POSIX_REGEX
531                         code = regexec(&re, patt, 0, NULL, 0);
532                         if (code == 0) {
533 #else
534                         code = re_exec(patt);
535                         if (code == 1) {
536 #endif
537                             found = 1;
538                             foundentry = srvpartpair;
539                             volType = BACKVOL;
540                             break;
541                         }
542                     }
543                 }
544
545                 /* If the RO name matches the volume set entry, remember
546                  * it, but continue searching. Further entries may be
547                  * RW or backup entries that will match.
548                  */
549                 else if (!found && (entry.serverFlags[srvpartpair] & ITSROVOL)
550                          && (entry.flags & RO_EXISTS)) {
551                     sprintf(patt, "%s.readonly", entry.name);
552 #ifdef HAVE_POSIX_REGEX
553                     code = regexec(&re, patt, 0, NULL, 0);
554                     if (code == 0) {
555 #else
556                     code = re_exec(patt);
557                     if (code == 1) {
558 #endif
559                         found = 1;
560                         foundentry = srvpartpair;
561                         volType = ROVOL;
562                     }
563                 }
564
565                 if (code < 0)
566                     afs_com_err(whoami, 0, "Internal error in regex package");
567             }                   /*s */
568
569             /* If found a match, then create a new volume dump entry */
570             if (found) {        /*f */
571                 /* Find server and partition structure to hang the entry off of */
572                 code =
573                     getSPEntries(entry.serverNumber[foundentry],
574                                  entry.serverPartition[foundentry], &servers,
575                                  &ss, &ps);
576                 if (code) {
577                     afs_com_err(whoami, code, NULL);
578                     return (code);
579                 }
580
581                 total++;
582                 tvd = (struct bc_volumeDump *)
583                     malloc(sizeof(struct bc_volumeDump));
584                 if (!tvd) {
585                     afs_com_err(whoami, BC_NOMEM, NULL);
586                     return (BC_NOMEM);
587                 }
588                 memset(tvd, 0, sizeof(*tvd));
589
590                 tvd->name = (char *)malloc(strlen(entry.name) + 10);
591                 if (!(tvd->name)) {
592                     afs_com_err(whoami, BC_NOMEM, NULL);
593                     free(tvd);
594                     return (BC_NOMEM);
595                 }
596
597                 strcpy(tvd->name, entry.name);
598                 if (volType == BACKVOL)
599                     strcat(tvd->name, ".backup");
600                 else if (volType == ROVOL)
601                     strcat(tvd->name, ".readonly");
602                 tvd->vid = entry.volumeId[volType];
603                 tvd->entry = tve;
604                 tvd->volType = volType;
605                 tvd->partition = entry.serverPartition[foundentry];
606                 tvd->server.sin_addr.s_addr = entry.serverNumber[foundentry];
607                 tvd->server.sin_port = 0;       /* default FS port */
608                 tvd->server.sin_family = AF_INET;
609
610                 /* String tvd off of partition struct */
611                 tvd->next = ps->vdlist;
612                 ps->vdlist = tvd;
613                 if (!tvd->next)
614                     ps->lastvdlist = tvd;
615
616                 break;
617             }                   /*f */
618         }                       /*ve */
619     }                           /*w */
620 #ifdef HAVE_POSIX_REGEX
621     if (need_regfree)
622         regfree(&re);
623 #endif
624
625     /* Randomly link the volumedump entries together */
626     randSPEntries(servers, avols);
627
628     fprintf(stderr, "Total number of volumes : %u\n", total);
629     return (0);
630 }                               /*EvalVolumeSet1 */
631
632 char *
633 compactTimeString(time_t *date, char *string, afs_int32 size)
634 {
635     struct tm *ltime;
636
637     if (!string)
638         return NULL;
639
640     if (*date == NEVERDATE) {
641         sprintf(string, "NEVER");
642     } else {
643         ltime = localtime(date);
644         strftime(string, size, "%m/%d/%Y %H:%M", ltime);
645     }
646     return (string);
647 }
648
649 /* compactDateString
650  *      print out a date in compact format, 16 chars, format is
651  *      mm/dd/yyyy hh:mm
652  * entry:
653  *      date_long - ptr to a long containing the time
654  * exit:
655  *      ptr to a string containing a representation of the date
656  */
657 char *
658 compactDateString(afs_uint32 *date_long, char *string, afs_int32 size)
659 {
660     time_t t = *date_long;
661     return compactTimeString(&t, string, size);
662 }
663
664
665 afs_int32
666 bc_SafeATOI(char *anum)
667 {
668     afs_int32 total = 0;
669
670     for (; *anum; anum++) {
671         if ((*anum < '0') || (*anum > '9'))
672             return -1;
673         total = (10 * total) + (afs_int32) (*anum - '0');
674     }
675     return total;
676 }
677
678 /* bc_FloatATOI:
679  *    Take a string and parse it for a number (could be float) followed
680  *    by a character representing the units (K,M,G,T). Default is 'K'.
681  *    Return the size in KBytes.
682  */
683 afs_int32
684 bc_FloatATOI(char *anum)
685 {
686     float total = 0;
687     afs_int32 rtotal;
688     afs_int32 fraction = 0;     /* > 0 if past the decimal */
689
690     for (; *anum; anum++) {
691         if ((*anum == 't') || (*anum == 'T')) {
692             total *= 1024 * 1024 * 1024;
693             break;
694         }
695         if ((*anum == 'g') || (*anum == 'G')) {
696             total *= 1024 * 1024;
697             break;
698         }
699         if ((*anum == 'm') || (*anum == 'M')) {
700             total *= 1024;
701             break;
702         }
703         if ((*anum == 'k') || (*anum == 'K')) {
704             break;
705         }
706         if (*anum == '.') {
707             fraction = 10;
708             continue;
709         }
710         if ((*anum < '0') || (*anum > '9'))
711             return -1;
712
713         if (!fraction) {
714             total = (10. * total) + (float)(*anum - '0');
715         } else {
716             total += ((float)(*anum - '0')) / (float)fraction;
717             fraction *= 10;
718         }
719     }
720
721     total += 0.5;               /* Round up */
722     if (total > 0x7fffffff)     /* Don't go over 2G */
723         total = 0x7fffffff;
724     rtotal = (afs_int32) total;
725     return (rtotal);
726 }
727
728 /* make a copy of a string so that it can be freed later */
729 char *
730 bc_CopyString(char *astring)
731 {
732     char *tp;
733
734     if (!astring)
735         return (NULL);          /* propagate null strings easily */
736     tp = strdup(astring);
737     if (!tp) {
738         afs_com_err(whoami, BC_NOMEM, NULL);
739         return (tp);
740     }
741     return tp;
742 }
743
744 /* concatParams
745  *
746  *    Concatenates the parameters of an option and returns the string.
747  *
748  */
749
750 char *
751 concatParams(struct cmd_item *itemPtr)
752 {
753     struct cmd_item *tempPtr;
754     afs_int32 length = 0;
755     char *string;
756
757     /* compute the length of string required */
758     for (tempPtr = itemPtr; tempPtr; tempPtr = tempPtr->next) {
759         length += strlen(tempPtr->data);
760         length++;               /* space or null terminator */
761     }
762
763     if (length == 0) {          /* no string (0 length) */
764         afs_com_err(whoami, 0, "Can't have zero length date and time string");
765         return (NULL);
766     }
767
768     string = (char *)malloc(length);    /* allocate the string */
769     if (!string) {
770         afs_com_err(whoami, BC_NOMEM, NULL);
771         return (NULL);
772     }
773     string[0] = 0;
774
775     tempPtr = itemPtr;          /* now assemble the string */
776     while (tempPtr) {
777         strcat(string, tempPtr->data);
778         tempPtr = tempPtr->next;
779         if (tempPtr)
780             strcat(string, " ");
781     }
782
783     return (string);            /* return the string */
784 }
785
786 /* printIfStatus
787  *      print out an interface status node as received from butc
788  */
789
790 void
791 printIfStatus(struct tciStatusS *statusPtr)
792 {
793     printf("Task %d: %s: ", statusPtr->taskId, statusPtr->taskName);
794     if (statusPtr->nKBytes)
795         printf("%ld Kbytes transferred", (long unsigned int) statusPtr->nKBytes);
796     if (strlen(statusPtr->volumeName) != 0) {
797         if (statusPtr->nKBytes)
798             printf(", ");
799         printf("volume %s", statusPtr->volumeName);
800     }
801
802     /* orphan */
803
804     if (statusPtr->flags & ABORT_REQUEST)
805         printf(" [abort request rcvd]");
806
807     if (statusPtr->flags & ABORT_DONE)
808         printf(" [abort complete]");
809
810     if (statusPtr->flags & OPR_WAIT)
811         printf(" [operator wait]");
812
813     if (statusPtr->flags & CALL_WAIT)
814         printf(" [callout in progress]");
815
816     if (statusPtr->flags & DRIVE_WAIT)
817         printf(" [drive wait]");
818
819     if (statusPtr->flags & TASK_DONE)
820         printf(" [done]");
821     printf("\n");
822 }
823
824 afs_int32
825 getPortOffset(char *port)
826 {
827     afs_int32 portOffset;
828
829     portOffset = bc_SafeATOI(port);
830
831     if (portOffset < 0) {
832         afs_com_err(whoami, 0, "Can't decode port offset '%s'", port);
833         return (-1);
834     } else if (portOffset > BC_MAXPORTOFFSET) {
835         afs_com_err(whoami, 0, "%u exceeds max port offset %u", portOffset,
836                 BC_MAXPORTOFFSET);
837         return (-1);
838     }
839     return (portOffset);
840 }
841
842 /* bc_GetTapeStatusCmd
843  *      display status of all tasks on a particular tape coordinator
844  */
845 int
846 bc_GetTapeStatusCmd(struct cmd_syndesc *as, void *arock)
847 {
848     afs_int32 code;
849     struct rx_connection *tconn;
850     afs_int32 portOffset = 0;
851
852     int ntasks;
853     afs_uint32 flags;
854     afs_uint32 taskId;
855     struct tciStatusS status;
856
857     code = bc_UpdateHosts();
858     if (code) {
859         afs_com_err(whoami, code, "; Can't retrieve tape hosts");
860         return (code);
861     }
862
863     if (as->parms[0].items) {
864         portOffset = getPortOffset(as->parms[0].items->data);
865         if (portOffset < 0)
866             return (BC_BADARG);
867     }
868
869     code = ConnectButc(bc_globalConfig, portOffset, &tconn);
870     if (code)
871         return (code);
872
873     flags = TSK_STAT_FIRST;
874     taskId = 0;
875     ntasks = 0;
876
877     while ((flags & TSK_STAT_END) == 0) {
878         code = TC_ScanStatus(tconn, &taskId, &status, &flags);
879         if (code) {
880             if (code == TC_NOTASKS)
881                 break;
882             afs_com_err(whoami, code, "; Can't get status from butc");
883             return (-1);
884         }
885         if ((flags & TSK_STAT_NOTFOUND))
886             break;              /* Can't find the task id */
887         flags &= ~TSK_STAT_FIRST;       /* turn off flag */
888
889         printIfStatus(&status);
890         ntasks++;
891     }
892
893     if (ntasks == 0)
894         printf("Tape coordinator is idle\n");
895
896     if (flags & TSK_STAT_ADSM)
897         printf("TSM Tape coordinator\n");
898     else if (flags & TSK_STAT_XBSA)
899         printf("XBSA Tape coordinator\n");
900
901     return (0);
902 }
903
904 extern struct Lock dispatchLock;
905
906 /* bc_WaitForNoJobs
907  *      wait for all jobs to terminate
908  */
909 int
910 bc_WaitForNoJobs(void)
911 {
912     int i;
913     int usefulJobRunning = 1;
914     int printWaiting = 1;
915
916     extern dlqlinkT statusHead;
917
918     while (usefulJobRunning) {
919         usefulJobRunning = (dlqEmpty(&statusHead) ? 0 : 1);
920         if (dispatchLock.excl_locked)
921             usefulJobRunning = 1;
922         for (i = 0; (!usefulJobRunning && (i < BC_MAXSIMDUMPS)); i++) {
923             if (bc_dumpTasks[i].flags & BC_DI_INUSE)
924                 usefulJobRunning = 1;
925         }
926
927         /* Wait 5 seconds and check again */
928         if (usefulJobRunning) {
929             if (printWaiting) {
930                 afs_com_err(whoami, 0, "waiting for job termination");
931                 printWaiting = 0;
932             }
933             IOMGR_Sleep(5);
934         }
935     }
936     return (lastTaskCode);
937 }
938
939 /* bc_JobsCmd
940  *      print status on running jobs
941  * parameters
942  *      ignored - a null "as" prints only jobs.
943  */
944 int
945 bc_JobsCmd(struct cmd_syndesc *as, void *arock)
946 {
947     afs_int32 prevTime;
948     dlqlinkP ptr;
949     statusP statusPtr;
950     char ds[50];
951
952     statusP youngest;
953     dlqlinkT atJobsHead;
954     extern dlqlinkT statusHead;
955
956     dlqInit(&atJobsHead);
957
958     lock_Status();
959     ptr = (&statusHead)->dlq_next;
960     while (ptr != &statusHead) {
961         statusPtr = (statusP) ptr;
962         ptr = ptr->dlq_next;
963
964         if (statusPtr->scheduledDump) {
965             dlqUnlink((dlqlinkP) statusPtr);
966             dlqLinkb(&atJobsHead, (dlqlinkP) statusPtr);
967         } else {
968             printf("Job %d:", statusPtr->jobNumber);
969
970             printf(" %s", statusPtr->taskName);
971
972             if (statusPtr->dbDumpId)
973                 printf(": DumpID %u", statusPtr->dbDumpId);
974             if (statusPtr->nKBytes)
975                 printf(", %ld Kbytes", afs_printable_int32_ld(statusPtr->nKBytes));
976             if (strlen(statusPtr->volumeName) != 0)
977                 printf(", volume %s", statusPtr->volumeName);
978
979             if (statusPtr->flags & CONTACT_LOST)
980                 printf(" [butc contact lost]");
981
982             if (statusPtr->flags & ABORT_REQUEST)
983                 printf(" [abort request]");
984
985             if (statusPtr->flags & ABORT_SENT)
986                 printf(" [abort sent]");
987
988             if (statusPtr->flags & OPR_WAIT)
989                 printf(" [operator wait]");
990
991             if (statusPtr->flags & CALL_WAIT)
992                 printf(" [callout in progress]");
993
994             if (statusPtr->flags & DRIVE_WAIT)
995                 printf(" [drive wait]");
996             printf("\n");
997         }
998     }
999
1000     /*
1001      * Now print the scheduled dumps.
1002      */
1003     if (!dlqEmpty(&statusHead) && as)
1004         printf("\n");           /* blank line between running and scheduled dumps */
1005
1006     prevTime = 0;
1007     while (!dlqEmpty(&atJobsHead)) {
1008         ptr = (&atJobsHead)->dlq_next;
1009         youngest = (statusP) ptr;
1010
1011         ptr = ptr->dlq_next;
1012         while (ptr != &atJobsHead) {    /* Find the dump that starts the earliest */
1013             statusPtr = (statusP) ptr;
1014             if (statusPtr->scheduledDump < youngest->scheduledDump)
1015                 youngest = statusPtr;
1016             ptr = ptr->dlq_next;
1017         }
1018
1019         /* Print token expiration time */
1020         if ((tokenExpires > prevTime)
1021             && (tokenExpires <= youngest->scheduledDump) && as
1022             && (tokenExpires != NEVERDATE)) {
1023             if (tokenExpires > time(0)) {
1024                 compactTimeString(&tokenExpires, ds, 50);
1025                 printf("       %16s: TOKEN EXPIRATION\n", ds);
1026             } else {
1027                 printf("       TOKEN HAS EXPIRED\n");
1028             }
1029         }
1030         prevTime = youngest->scheduledDump;
1031
1032         /* Print the info */
1033         compactDateString(&youngest->scheduledDump, ds, 50);
1034         printf("Job %d:", youngest->jobNumber);
1035         printf(" %16s: %s", ds, youngest->cmdLine);
1036         printf("\n");
1037
1038         /* return to original list */
1039         dlqUnlink((dlqlinkP) youngest);
1040         dlqLinkb(&statusHead, (dlqlinkP) youngest);
1041     }
1042
1043     /* Print token expiration time if havn't already */
1044     if ((tokenExpires == NEVERDATE) && as)
1045         printf("     : TOKEN NEVER EXPIRES\n");
1046     else if ((tokenExpires > prevTime) && as) {
1047         if (tokenExpires > time(0)) {
1048             compactTimeString(&tokenExpires, ds, 50);
1049             printf("       %16s: TOKEN EXPIRATION\n", ds);
1050         } else {
1051             printf("     : TOKEN HAS EXPIRED\n");
1052         }
1053     }
1054
1055     unlock_Status();
1056     return 0;
1057 }
1058
1059 int
1060 bc_KillCmd(struct cmd_syndesc *as, void *arock)
1061 {
1062     afs_int32 i;
1063     afs_int32 slot;
1064     struct bc_dumpTask *td;
1065     char *tp;
1066     char tbuffer[256];
1067
1068     dlqlinkP ptr;
1069     statusP statusPtr;
1070
1071     extern dlqlinkT statusHead;
1072
1073     tp = as->parms[0].items->data;
1074     if (strchr(tp, '.') == 0) {
1075         slot = bc_SafeATOI(tp);
1076         if (slot == -1) {
1077             afs_com_err(whoami, 0, "Bad syntax for number '%s'", tp);
1078             return -1;
1079         }
1080
1081         lock_Status();
1082         ptr = (&statusHead)->dlq_next;
1083         while (ptr != &statusHead) {
1084             statusPtr = (statusP) ptr;
1085             if (statusPtr->jobNumber == slot) {
1086                 statusPtr->flags |= ABORT_REQUEST;
1087                 unlock_Status();
1088                 return (0);
1089             }
1090             ptr = ptr->dlq_next;
1091         }
1092         unlock_Status();
1093
1094         fprintf(stderr, "Job %d not found\n", slot);
1095         return (-1);
1096     } else {
1097         /* vol.dump */
1098         td = bc_dumpTasks;
1099         for (i = 0; i < BC_MAXSIMDUMPS; i++, td++) {
1100             if (td->flags & BC_DI_INUSE) {
1101                 /* compute name */
1102                 strcpy(tbuffer, td->volSetName);
1103                 strcat(tbuffer, ".");
1104                 strcat(tbuffer, tailCompPtr(td->dumpName));
1105                 if (strcmp(tbuffer, tp) == 0)
1106                     break;
1107             }
1108         }
1109         if (i >= BC_MAXSIMDUMPS) {
1110             afs_com_err(whoami, 0, "Can't find job %s", tp);
1111             return -1;
1112         }
1113
1114         lock_Status();
1115         statusPtr = findStatus(td->dumpID);
1116
1117         if (statusPtr == 0) {
1118             afs_com_err(whoami, 0, "Can't locate status - internal error");
1119             unlock_Status();
1120             return (-1);
1121         }
1122         statusPtr->flags |= ABORT_REQUEST;
1123         unlock_Status();
1124     }
1125     return 0;
1126 }
1127
1128 /* restore a volume or volumes */
1129 int
1130 bc_VolRestoreCmd(struct cmd_syndesc *as, void *arock)
1131 {
1132     /*
1133      * parm 0 is the new server to restore to
1134      * parm 1 is the new partition to restore to
1135      * parm 2 is volume(s) to restore
1136      * parm 3 is the new extension, if any, for the volume name.
1137      * parm 4 gives the new volume # to restore this volume as (removed).
1138      * parm 4 date is a string representing the date
1139      *
1140      * We handle four types of restores.  If old is set, then we restore the
1141      * volume with the same name and ID.  If old is not set, we allocate
1142      * a new volume ID for the restored volume.  If a new extension is specified,
1143      * we add that extension to the volume name of the restored volume.
1144      */
1145     struct bc_volumeEntry tvolumeEntry; /* entry within the volume set */
1146     struct bc_volumeDump *volsToRestore = (struct bc_volumeDump *)0;
1147     struct bc_volumeDump *lastVol = (struct bc_volumeDump *)0;
1148     struct bc_volumeDump *tvol; /* temp for same */
1149     struct sockaddr_in destServ;        /* machine to which to restore volumes */
1150     afs_int32 destPartition;    /* partition to which to restore volumes */
1151     char *tp;
1152     struct cmd_item *ti;
1153     afs_int32 code;
1154     int oldFlag;
1155     afs_int32 fromDate;
1156     afs_int32 dumpID = 0;
1157     char *newExt, *timeString;
1158     afs_int32 i;
1159     afs_int32 *ports = NULL;
1160     afs_int32 portCount = 0;
1161     int dontExecute;
1162
1163     code = bc_UpdateHosts();
1164     if (code) {
1165         afs_com_err(whoami, code, "; Can't retrieve tape hosts");
1166         return (code);
1167     }
1168
1169     /* specified other destination host */
1170     if (as->parms[0].items) {
1171         tp = as->parms[0].items->data;
1172         if (bc_ParseHost(tp, &destServ)) {
1173             afs_com_err(whoami, 0, "Failed to locate destination host '%s'", tp);
1174             return -1;
1175         }
1176     }
1177
1178     /* specified other destination partition */
1179     if (as->parms[1].items) {
1180         tp = as->parms[1].items->data;
1181         if (bc_GetPartitionID(tp, &destPartition)) {
1182             afs_com_err(whoami, 0, "Can't parse destination partition '%s'", tp);
1183             return -1;
1184         }
1185     }
1186
1187     for (ti = as->parms[2].items; ti; ti = ti->next) {
1188         /* build list of volume items */
1189         tvol = (struct bc_volumeDump *)malloc(sizeof(struct bc_volumeDump));
1190         if (!tvol) {
1191             afs_com_err(whoami, BC_NOMEM, NULL);
1192             return BC_NOMEM;
1193         }
1194         memset(tvol, 0, sizeof(struct bc_volumeDump));
1195
1196         tvol->name = (char *)malloc(VOLSER_MAXVOLNAME + 1);
1197         if (!tvol->name) {
1198             afs_com_err(whoami, BC_NOMEM, NULL);
1199             return BC_NOMEM;
1200         }
1201         strncpy(tvol->name, ti->data, VOLSER_OLDMAXVOLNAME);
1202         tvol->entry = &tvolumeEntry;
1203
1204         if (lastVol)
1205             lastVol->next = tvol;       /* thread onto end of list */
1206         else
1207             volsToRestore = tvol;
1208         lastVol = tvol;
1209     }
1210
1211     if (as->parms[4].items) {
1212         timeString = concatParams(as->parms[4].items);
1213         if (!timeString)
1214             return (-1);
1215
1216         code = ktime_DateToLong(timeString, &fromDate);
1217         free(timeString);
1218         if (code) {
1219             afs_com_err(whoami, 0, "Can't parse restore date and time");
1220             afs_com_err(whoami, 0, "%s", ktime_GetDateUsage());
1221             return code;
1222         }
1223     } else {
1224         fromDate = 0x7fffffff;  /* latest one */
1225     }
1226
1227     newExt = (as->parms[3].items ? as->parms[3].items->data : NULL);
1228     oldFlag = 0;
1229
1230     /* Read all the port offsets into the ports array. The first element in the
1231      * array is for full restore and the rest are for incremental restores
1232      */
1233     if (as->parms[5].items) {
1234         for (ti = as->parms[5].items; ti; ti = ti->next)
1235             portCount++;
1236         ports = (afs_int32 *) malloc(portCount * sizeof(afs_int32));
1237         if (!ports) {
1238             afs_com_err(whoami, BC_NOMEM, NULL);
1239             return BC_NOMEM;
1240         }
1241
1242         for (ti = as->parms[5].items, i = 0; ti; ti = ti->next, i++) {
1243             ports[i] = getPortOffset(ti->data);
1244             if (ports[i] < 0)
1245                 return (BC_BADARG);
1246         }
1247     }
1248
1249     dontExecute = (as->parms[6].items ? 1 : 0); /* -n */
1250
1251     if (as->parms[7].items)
1252       {
1253         dumpID = atoi(as->parms[7].items->data);
1254         if (dumpID <= 0)
1255           dumpID = 0;
1256       }
1257
1258     /*
1259      * Perform the call to start the restore.
1260      */
1261     code =
1262         bc_StartDmpRst(bc_globalConfig, "volume", "restore", volsToRestore,
1263                        &destServ, destPartition, fromDate, newExt, oldFlag,
1264                        /*parentDump */ dumpID, /*dumpLevel */ 0,
1265                        bc_Restorer, ports, portCount,
1266                        /*dumpSched */ NULL, /*append */ 0, dontExecute);
1267     if (code)
1268         afs_com_err(whoami, code, "; Failed to queue restore");
1269
1270     return (code);
1271 }
1272
1273 /* restore a whole partition or server */
1274
1275 /* bc_DiskRestoreCmd
1276  *      restore a whole partition
1277  * params:
1278  *      first, reqd - machine (server) to restore
1279  *      second, reqd - partition to restore
1280  *      various optional
1281  */
1282
1283 int
1284 bc_DiskRestoreCmd(struct cmd_syndesc *as, void *arock)
1285 {
1286     struct bc_volumeSet tvolumeSet;     /* temporary volume set for EvalVolumeSet call */
1287     struct bc_volumeEntry tvolumeEntry; /* entry within the volume set */
1288     struct bc_volumeDump *volsToRestore = (struct bc_volumeDump *)0;
1289     struct sockaddr_in destServ;        /* machine to which to restore volumes */
1290     afs_int32 destPartition;    /* partition to which to restore volumes */
1291     char *tp;
1292     afs_int32 code;
1293     int oldFlag;
1294     afs_int32 fromDate;
1295     char *newExt;
1296     afs_int32 *ports = NULL;
1297     afs_int32 portCount = 0;
1298     int dontExecute;
1299     struct bc_volumeDump *prev, *tvol, *nextvol;
1300     struct cmd_item *ti;
1301     afs_int32 i;
1302
1303     /* parm 0 is the server to restore
1304      * parm 1 is the partition to restore
1305
1306      * parm 8 and above as in VolRestoreCmd:
1307      * parm 8 is the new server to restore to
1308      * parm 9 is the new partition to restore to
1309      */
1310
1311     code = bc_UpdateVolumeSet();
1312     if (code) {
1313         afs_com_err(whoami, code, "; Can't retrieve volume sets");
1314         return (code);
1315     }
1316     code = bc_UpdateHosts();
1317     if (code) {
1318         afs_com_err(whoami, code, "; Can't retrieve tape hosts");
1319         return (code);
1320     }
1321
1322     /* create a volume set corresponding to the volume pattern we've been given */
1323     memset(&tvolumeSet, 0, sizeof(tvolumeSet));
1324     memset(&tvolumeEntry, 0, sizeof(tvolumeEntry));
1325     tvolumeSet.name = "TempVolumeSet";
1326     tvolumeSet.ventries = &tvolumeEntry;
1327     tvolumeEntry.serverName = as->parms[0].items->data;
1328     tvolumeEntry.partname = as->parms[1].items->data;
1329
1330     if (bc_GetPartitionID(tvolumeEntry.partname, &tvolumeEntry.partition)) {
1331         afs_com_err(whoami, 0, "Can't parse partition '%s'",
1332                 tvolumeEntry.partname);
1333         return -1;
1334     }
1335
1336     if (bc_ParseHost(tvolumeEntry.serverName, &tvolumeEntry.server)) {
1337         afs_com_err(whoami, 0, "Can't locate host '%s'", tvolumeEntry.serverName);
1338         return -1;
1339     }
1340
1341     /* specified other destination host */
1342     if (as->parms[8].items) {
1343         tp = as->parms[8].items->data;
1344         if (bc_ParseHost(tp, &destServ)) {
1345             afs_com_err(whoami, 0, "Can't locate destination host '%s'", tp);
1346             return -1;
1347         }
1348     } else                      /* use destination host == original host */
1349         memcpy(&destServ, &tvolumeEntry.server, sizeof(destServ));
1350
1351     /* specified other destination partition */
1352     if (as->parms[9].items) {
1353         tp = as->parms[9].items->data;
1354         if (bc_GetPartitionID(tp, &destPartition)) {
1355             afs_com_err(whoami, 0, "Can't parse destination partition '%s'", tp);
1356             return -1;
1357         }
1358     } else                      /* use original partition */
1359         destPartition = tvolumeEntry.partition;
1360
1361     tvolumeEntry.name = ".*";   /* match all volumes (this should be a parameter) */
1362
1363     if (as->parms[2].items) {
1364         for (ti = as->parms[2].items; ti; ti = ti->next)
1365             portCount++;
1366         ports = (afs_int32 *) malloc(portCount * sizeof(afs_int32));
1367         if (!ports) {
1368             afs_com_err(whoami, BC_NOMEM, NULL);
1369             return BC_NOMEM;
1370         }
1371
1372         for (ti = as->parms[2].items, i = 0; ti; ti = ti->next, i++) {
1373             ports[i] = getPortOffset(ti->data);
1374             if (ports[i] < 0)
1375                 return (BC_BADARG);
1376         }
1377     }
1378
1379     newExt = (as->parms[10].items ? as->parms[10].items->data : NULL);
1380     dontExecute = (as->parms[11].items ? 1 : 0);        /* -n */
1381
1382     /*
1383      * Expand out the volume set into its component list of volumes, by calling VLDB server.
1384      */
1385     code =
1386         bc_EvalVolumeSet(bc_globalConfig, &tvolumeSet, &volsToRestore,
1387                          cstruct);
1388     if (code) {
1389         afs_com_err(whoami, code, "; Failed to evaluate volume set");
1390         return (-1);
1391     }
1392
1393     /* Since we want only RW volumes, remove any
1394      * BK or RO volumes from the list.
1395      */
1396     for (prev = 0, tvol = volsToRestore; tvol; tvol = nextvol) {
1397         nextvol = tvol->next;
1398         if (BackupName(tvol->name)) {
1399             if (tvol->name)
1400                 fprintf(stderr,
1401                         "Will not restore volume %s (its RW does not reside on the partition)\n",
1402                         tvol->name);
1403
1404             if (tvol == volsToRestore) {
1405                 volsToRestore = nextvol;
1406             } else {
1407                 prev->next = nextvol;
1408             }
1409             if (tvol->name)
1410                 free(tvol->name);
1411             free(tvol);
1412         } else {
1413             prev = tvol;
1414         }
1415     }
1416
1417     fromDate = 0x7fffffff;      /* last one before this date */
1418     oldFlag = 1;                /* do restore same volid (and name) */
1419
1420     /*
1421      * Perform the call to start the dump.
1422      */
1423     code =
1424         bc_StartDmpRst(bc_globalConfig, "disk", "restore", volsToRestore,
1425                        &destServ, destPartition, fromDate, newExt, oldFlag,
1426                        /*parentDump */ 0, /*dumpLevel */ 0,
1427                        bc_Restorer, ports, portCount,
1428                        /*dumpSched */ NULL, /*append */ 0, dontExecute);
1429     if (code)
1430         afs_com_err(whoami, code, "; Failed to queue restore");
1431
1432     return (code);
1433 }
1434
1435 /* bc_VolsetRestoreCmd
1436  *      restore a volumeset or list of volumes.
1437  */
1438
1439 int
1440 bc_VolsetRestoreCmd(struct cmd_syndesc *as, void *arock)
1441 {
1442     int oldFlag;
1443     long fromDate;
1444     char *newExt;
1445
1446     int dontExecute;
1447     afs_int32 *ports = NULL;
1448     afs_int32 portCount = 0;
1449     afs_int32 code = 0;
1450     char *volsetName;
1451     struct bc_volumeSet *volsetPtr;     /* Ptr to list of generated volume info */
1452     struct bc_volumeDump *volsToRestore = (struct bc_volumeDump *)0;
1453     struct bc_volumeDump *lastVol = (struct bc_volumeDump *)0;
1454     struct sockaddr_in destServer;      /* machine to which to restore volume */
1455     afs_int32 destPartition;    /* partition to which to restore volumes */
1456     struct cmd_item *ti;
1457     afs_int32 i;
1458
1459     code = bc_UpdateVolumeSet();
1460     if (code) {
1461         afs_com_err(whoami, code, "; Can't retrieve volume sets");
1462         return (code);
1463     }
1464     code = bc_UpdateHosts();
1465     if (code) {
1466         afs_com_err(whoami, code, "; Can't retrieve tape hosts");
1467         return (code);
1468     }
1469
1470     if (as->parms[0].items) {
1471         if (as->parms[1].items) {
1472             afs_com_err(whoami, 0, "Can't have both -name and -file options");
1473             return (-1);
1474         }
1475
1476         volsetName = as->parms[0].items->data;
1477         volsetPtr = bc_FindVolumeSet(bc_globalConfig, volsetName);
1478         if (!volsetPtr) {
1479             afs_com_err(whoami, 0,
1480                     "Can't find volume set '%s' in backup database",
1481                     volsetName);
1482             return (-1);
1483         }
1484
1485         /* Expand out the volume set into its component list of volumes. */
1486         code =
1487             bc_EvalVolumeSet(bc_globalConfig, volsetPtr, &volsToRestore,
1488                              cstruct);
1489         if (code) {
1490             afs_com_err(whoami, code, "; Failed to evaluate volume set");
1491             return (-1);
1492         }
1493     } else if (as->parms[1].items) {
1494         FILE *fd;
1495         char line[256];
1496         char server[50], partition[50], volume[50], rest[256];
1497         long count;
1498         struct bc_volumeDump *tvol;
1499
1500         fd = fopen(as->parms[1].items->data, "r");
1501         if (!fd) {
1502             afs_com_err(whoami, errno, "; Cannot open file '%s'",
1503                     as->parms[1].items->data);
1504             return (-1);
1505         }
1506
1507         while (fgets(line, 255, fd)) {
1508             count =
1509                 sscanf(line, "%s %s %s %s", server, partition, volume, rest);
1510
1511             if (count <= 0)
1512                 continue;
1513             if (count < 3) {
1514                 fprintf(stderr,
1515                         "Invalid volumeset file format: Wrong number of arguments: Ignoring\n");
1516                 fprintf(stderr, "     %s", line);
1517                 continue;
1518             }
1519
1520             if (bc_ParseHost(server, &destServer)) {
1521                 afs_com_err(whoami, 0, "Failed to locate host '%s'", server);
1522                 continue;
1523             }
1524
1525             if (bc_GetPartitionID(partition, &destPartition)) {
1526                 afs_com_err(whoami, 0,
1527                         "Failed to parse destination partition '%s'",
1528                         partition);
1529                 continue;
1530             }
1531
1532             /* Allocate a volumeDump structure and link it in */
1533             tvol =
1534                 (struct bc_volumeDump *)malloc(sizeof(struct bc_volumeDump));
1535             memset(tvol, 0, sizeof(struct bc_volumeDump));
1536
1537             tvol->name = (char *)malloc(VOLSER_MAXVOLNAME + 1);
1538             if (!tvol->name) {
1539                 afs_com_err(whoami, BC_NOMEM, NULL);
1540                 return BC_NOMEM;
1541             }
1542             strncpy(tvol->name, volume, VOLSER_OLDMAXVOLNAME);
1543             memcpy(&tvol->server, &destServer, sizeof(destServer));
1544             tvol->partition = destPartition;
1545
1546             if (lastVol)
1547                 lastVol->next = tvol;   /* thread onto end of list */
1548             else
1549                 volsToRestore = tvol;
1550             lastVol = tvol;
1551         }
1552         fclose(fd);
1553     } else {
1554         afs_com_err(whoami, 0, "-name or -file option required");
1555         return (-1);
1556     }
1557
1558
1559     /* Get the port offset for the restore */
1560     if (as->parms[2].items) {
1561         for (ti = as->parms[2].items; ti; ti = ti->next)
1562             portCount++;
1563         ports = (afs_int32 *) malloc(portCount * sizeof(afs_int32));
1564         if (!ports) {
1565             afs_com_err(whoami, BC_NOMEM, NULL);
1566             return BC_NOMEM;
1567         }
1568
1569         for (ti = as->parms[2].items, i = 0; ti; ti = ti->next, i++) {
1570             ports[i] = getPortOffset(ti->data);
1571             if (ports[i] < 0)
1572                 return (BC_BADARG);
1573         }
1574     }
1575
1576     newExt = (as->parms[3].items ? as->parms[3].items->data : NULL);
1577     dontExecute = (as->parms[4].items ? 1 : 0);
1578
1579     fromDate = 0x7fffffff;      /* last one before this date */
1580     oldFlag = 1;                /* do restore same volid (and name) */
1581
1582     /* Perform the call to start the restore */
1583     code = bc_StartDmpRst(bc_globalConfig, "disk", "restore", volsToRestore,
1584                           /*destserver */ NULL, /*destpartition */ 0, fromDate,
1585                           newExt, oldFlag,
1586                           /*parentDump */ 0, /*dumpLevel */ 0,
1587                           bc_Restorer, ports, portCount,
1588                           /*dumpSched */ NULL, /*append */ 0, dontExecute);
1589     if (code)
1590         afs_com_err(whoami, code, "; Failed to queue restore");
1591
1592     return code;
1593 }
1594
1595 /*-----------------------------------------------------------------------------
1596  * bc_DumpCmd
1597  *
1598  * Description:
1599  *      Perform a dump of the set of volumes provided.
1600  *      user specifies: -volumeset .. -dump .. [-portoffset ..] [-n]
1601  *
1602  * Arguments:
1603  *      as      : Parsed command line information.
1604  *      arock   : Ptr to misc stuff; not used.
1605  *
1606  * Returns:
1607  *      -1 on errors,
1608  *      The result of bc_StartDump() otherwise
1609  *
1610  * Environment:
1611  *      Nothing special.
1612  *
1613  * Side Effects:
1614  *      As advertised.
1615  *---------------------------------------------------------------------------
1616  */
1617 int dontExecute;
1618
1619 int
1620 bc_DumpCmd(struct cmd_syndesc *as, void *arock)
1621 {                               /*bc_DumpCmd */
1622     char *dumpPath = NULL;
1623     char *vsName = NULL;      /*Ptrs to various names */
1624     struct bc_volumeSet *tvs = NULL; /*Ptr to list of generated volume info */
1625     struct bc_dumpSchedule *tds;
1626     struct bc_dumpSchedule *baseds = NULL; /*Ptr to dump schedule node */
1627     struct bc_volumeDump *tve, *volsToDump;     /*Ptr to individual vols to be dumped */
1628     struct budb_dumpEntry dumpEntry, de, fde;   /* dump entry */
1629     afs_uint32 d;
1630
1631     afs_int32 parent;           /* parent dump */
1632     afs_int32 level;            /* this dump's level # */
1633     afs_int32 problemFindingDump;       /* can't find parent(s) */
1634
1635     afs_int32 *portp = NULL;
1636     afs_int32 doAt, atTime;     /* Time a timed-dump is to start at */
1637     afs_int32 length;
1638     char *timeString;
1639     int doAppend = 0;           /* Append the dump to dump set */
1640     afs_int32 code;             /* Return code */
1641     int loadfile;               /* whether to load a file or not */
1642
1643     statusP statusPtr;
1644
1645     extern struct bc_dumpTask bc_dumpTasks[];
1646
1647     code = bc_UpdateDumpSchedule();
1648     if (code) {
1649         afs_com_err(whoami, code, "; Can't retrieve dump schedule");
1650         return (code);
1651     }
1652     code = bc_UpdateVolumeSet();
1653     if (code) {
1654         afs_com_err(whoami, code, "; Can't retrieve volume sets");
1655         return (code);
1656     }
1657     code = bc_UpdateHosts();
1658     if (code) {
1659         afs_com_err(whoami, code, "; Can't retrieve tape hosts");
1660         return (code);
1661     }
1662
1663     /*
1664      * Some parameters cannot be specified together
1665      * The "-file" option cannot exist with the "-volume", "-dump",
1666      * "-portoffset", or "-append" option
1667      */
1668     if (as->parms[6].items) {
1669         loadfile = 1;
1670         if (as->parms[0].items || as->parms[1].items || as->parms[2].items
1671             || as->parms[4].items) {
1672             afs_com_err(whoami, 0, "Invalid option specified with -file option");
1673             return -1;
1674         }
1675     } else {
1676         loadfile = 0;
1677         if (!as->parms[0].items || !as->parms[1].items) {
1678             afs_com_err(whoami, 0,
1679                     "Must specify volume set name and dump level name");
1680             return -1;
1681         }
1682     }
1683
1684     /*
1685      * Get the time we are to perform this dump
1686      */
1687     if (as->parms[3].items) {
1688         doAt = 1;
1689
1690         timeString = concatParams(as->parms[3].items);
1691         if (!timeString)
1692             return (-1);
1693
1694         /*
1695          * Now parse this string for the time to start.
1696          */
1697         code = ktime_DateToLong(timeString, &atTime);
1698         free(timeString);
1699         if (code) {
1700             afs_com_err(whoami, 0, "Can't parse dump start date and time");
1701             afs_com_err(whoami, 0, "%s", ktime_GetDateUsage());
1702             return (1);
1703         }
1704     } else
1705         doAt = 0;
1706
1707     dontExecute = (as->parms[5].items ? 1 : 0); /* -n */
1708
1709     /*
1710      * If this dump is not a load file, then check the parameters.
1711      */
1712     if (!loadfile) {            /*6 */
1713         vsName = as->parms[0].items->data;      /* get volume set name */
1714         dumpPath = as->parms[1].items->data;    /* get dump path */
1715
1716         /* get the port number, if one was specified */
1717         if (as->parms[2].items) {
1718             portp = (afs_int32 *) malloc(sizeof(afs_int32));
1719             if (!portp) {
1720                 afs_com_err(whoami, BC_NOMEM, NULL);
1721                 return BC_NOMEM;
1722             }
1723
1724             *portp = getPortOffset(as->parms[2].items->data);
1725             if (*portp < 0)
1726                 return (BC_BADARG);
1727         }
1728
1729         doAppend = (as->parms[4].items ? 1 : 0);        /* -append */
1730
1731         /*
1732          * Get a hold of the given volume set and dump set.
1733          */
1734         tvs = bc_FindVolumeSet(bc_globalConfig, vsName);
1735         if (!tvs) {
1736             afs_com_err(whoami, 0,
1737                     "Can't find volume set '%s' in backup database", vsName);
1738             return (-1);
1739         }
1740         baseds = bc_FindDumpSchedule(bc_globalConfig, dumpPath);
1741         if (!baseds) {
1742             afs_com_err(whoami, 0,
1743                     "Can't find dump schedule '%s' in backup database",
1744                     dumpPath);
1745             return (-1);
1746         }
1747
1748     }
1749
1750     /*6 */
1751     /*
1752      * If given the "-at" option, then add this to the jobs list and return
1753      * with no error.
1754      *
1755      * Create a status node for this timed dump.
1756      * Fill in the time to dump and the cmd line for the dump leaving off
1757      * the -at option.  If the -n option is there, it is scheduled with
1758      * the Timed dump as opposed to not scheduling the time dump at all.
1759      */
1760     if (doAt) {
1761         if (atTime < time(0)) {
1762             afs_com_err(whoami, 0,
1763                     "Time of dump is earlier then current time - not added");
1764         } else {
1765             statusPtr = createStatusNode();
1766             lock_Status();
1767             statusPtr->scheduledDump = atTime;
1768
1769             /* Determine length of the dump command */
1770             length = 0;
1771             length += 4;        /* "dump" */
1772             if (loadfile) {
1773                 length += 6;    /* " -file" */
1774                 length += 1 + strlen(as->parms[6].items->data); /* " <file>" */
1775             } else {
1776                 /* length += 11; *//* " -volumeset" */
1777                 length += 1 + strlen(as->parms[0].items->data); /* " <volset> */
1778
1779                 /* length += 6; *//* " -dump" */
1780                 length += 1 + strlen(as->parms[1].items->data); /* " <dumpset> */
1781
1782                 if (as->parms[2].items) {
1783                     /* length += 12; *//* " -portoffset" */
1784                     length += 1 + strlen(as->parms[2].items->data);     /* " <port>" */
1785                 }
1786
1787                 if (as->parms[4].items)
1788                     length += 8;        /* " -append" */
1789             }
1790
1791             if (dontExecute)
1792                 length += 3;    /* " -n" */
1793             length++;           /* end-of-line */
1794
1795             /* Allocate status block for this timed dump */
1796             sprintf(statusPtr->taskName, "Scheduled Dump");
1797             statusPtr->jobNumber = bc_jobNumber();
1798             statusPtr->scheduledDump = atTime;
1799             statusPtr->cmdLine = (char *)malloc(length);
1800             if (!statusPtr->cmdLine) {
1801                 afs_com_err(whoami, BC_NOMEM, NULL);
1802                 return BC_NOMEM;
1803             }
1804
1805             /* Now reconstruct the dump command */
1806             statusPtr->cmdLine[0] = 0;
1807             strcat(statusPtr->cmdLine, "dump");
1808             if (loadfile) {
1809                 strcat(statusPtr->cmdLine, " -file");
1810                 strcat(statusPtr->cmdLine, " ");
1811                 strcat(statusPtr->cmdLine, as->parms[6].items->data);
1812             } else {
1813                 /* strcat(statusPtr->cmdLine, " -volumeset"); */
1814                 strcat(statusPtr->cmdLine, " ");
1815                 strcat(statusPtr->cmdLine, as->parms[0].items->data);
1816
1817                 /* strcat(statusPtr->cmdLine, " -dump"); */
1818                 strcat(statusPtr->cmdLine, " ");
1819                 strcat(statusPtr->cmdLine, as->parms[1].items->data);
1820
1821                 if (as->parms[2].items) {
1822                     /* strcat(statusPtr->cmdLine, " -portoffset"); */
1823                     strcat(statusPtr->cmdLine, " ");
1824                     strcat(statusPtr->cmdLine, as->parms[2].items->data);
1825                 }
1826
1827                 if (as->parms[4].items)
1828                     strcat(statusPtr->cmdLine, " -append");
1829             }
1830             if (dontExecute)
1831                 strcat(statusPtr->cmdLine, " -n");
1832
1833             printf("Add scheduled dump as job %d\n", statusPtr->jobNumber);
1834             if ((atTime > tokenExpires) && (tokenExpires != NEVERDATE))
1835                 afs_com_err(whoami, 0,
1836                         "Warning: job %d starts after expiration of AFS token",
1837                         statusPtr->jobNumber);
1838
1839             unlock_Status();
1840         }
1841
1842         return (0);
1843     }
1844
1845     /*
1846      * Read and execute the load file if specified.  The work of reading is done
1847      * in the main routine prior the dispatch call. loadFile and dontExecute are
1848      * global variables so this can take place in main.
1849      */
1850     if (loadfile) {
1851         loadFile = strdup(as->parms[6].items->data);
1852         if (!loadFile) {
1853             afs_com_err(whoami, BC_NOMEM, NULL);
1854             return BC_NOMEM;
1855         }
1856         return 0;
1857     }
1858
1859     /*
1860      * We are doing a real dump (no load file or timed dump).
1861      */
1862     printf("Starting dump of volume set '%s' (dump level '%s')\n", vsName,
1863            dumpPath);
1864
1865     /* For each dump-level above this one, see if the volumeset was dumped
1866      * at the level. We search all dump-levels since a higher dump-level
1867      * may have actually been done AFTER a lower dump level.
1868      */
1869     parent = level = problemFindingDump = 0;
1870     for (tds = baseds->parent; tds; tds = tds->parent) {
1871         /* Find the most recent dump of the volume-set at this dump-level.
1872          * Raise problem flag if didn't find a dump and a parent not yet found.
1873          */
1874         code = bcdb_FindLatestDump(vsName, tds->name, &dumpEntry);
1875         if (code) {
1876             if (!parent)
1877                 problemFindingDump = 1; /* skipping a dump level */
1878             continue;
1879         }
1880
1881         /* We found the most recent dump at this level. Now check
1882          * if we should use it by seeing if its full dump hierarchy
1883          * exists. If it doesn't, we don't want to base our incremental
1884          * off of this dump.
1885          */
1886         if (!parent || (dumpEntry.id > parent)) {
1887             /* Follow the parent dumps to see if they are all there */
1888             for (d = dumpEntry.parent; d; d = de.parent) {
1889                 code = bcdb_FindDumpByID(d, &de);
1890                 if (code)
1891                     break;
1892             }
1893
1894             /* If we found the entire level, remember it. Otherwise raise flag.
1895              * If we had already found a dump, raise the problem flag.
1896              */
1897             if (!d && !code) {
1898                 if (parent)
1899                     problemFindingDump = 1;
1900                 parent = dumpEntry.id;
1901                 level = dumpEntry.level + 1;
1902                 memcpy(&fde, &dumpEntry, sizeof(dumpEntry));
1903             } else {
1904                 /* Dump hierarchy not complete so can't base off the latest */
1905                 problemFindingDump = 1;
1906             }
1907         }
1908     }
1909
1910     /* If the problemflag was raise, it means we are not doing the
1911      * dump at the level we requested it be done at.
1912      */
1913     if (problemFindingDump) {
1914         afs_com_err(whoami, 0,
1915                 "Warning: Doing level %d dump due to missing higher-level dumps",
1916                 level);
1917         if (parent) {
1918             printf("Parent dump: dump %s (DumpID %u)\n", fde.name, parent);
1919         }
1920     } else if (parent) {
1921         printf("Found parent: dump %s (DumpID %u)\n", fde.name, parent);
1922     }
1923
1924     /* Expand out the volume set into its component list of volumes. */
1925     code = bc_EvalVolumeSet(bc_globalConfig, tvs, &volsToDump, cstruct);
1926     if (code) {
1927         afs_com_err(whoami, code, "; Failed to evaluate volume set");
1928         return (-1);
1929     }
1930     if (!volsToDump) {
1931         printf("No volumes to dump\n");
1932         return (0);
1933     }
1934
1935     /* Determine what the clone time of the volume was when it was
1936      * last dumped (tve->date). This is the time from when an
1937      * incremental should be done (remains zero if a full dump).
1938      */
1939     if (parent) {
1940         for (tve = volsToDump; tve; tve = tve->next) {
1941             code = bcdb_FindClone(parent, tve->name, &tve->date);
1942             if (code)
1943                 tve->date = 0;
1944
1945             /* Get the time the volume was last cloned and see if the volume has
1946              * changed since then. Only do this when the "-n" flag is specified
1947              * because butc will get the cloneDate at time of dump.
1948              */
1949             if (dontExecute) {
1950                 code =
1951                     volImageTime(tve->server.sin_addr.s_addr, tve->partition,
1952                                  tve->vid, tve->volType, &tve->cloneDate);
1953                 if (code)
1954                     tve->cloneDate = 0;
1955
1956                 if (tve->cloneDate && (tve->cloneDate == tve->date)) {
1957                     afs_com_err(whoami, 0,
1958                             "Warning: Timestamp on volume %s unchanged from previous dump",
1959                             tve->name);
1960                 }
1961             }
1962         }
1963     }
1964
1965     if (dontExecute)
1966         printf("Would have dumped the following volumes:\n");
1967     else
1968         printf("Preparing to dump the following volumes:\n");
1969     for (tve = volsToDump; tve; tve = tve->next) {
1970         printf("\t%s (%u)\n", tve->name, tve->vid);
1971     }
1972     if (dontExecute)
1973         return (0);
1974
1975     code = bc_StartDmpRst(bc_globalConfig, dumpPath, vsName, volsToDump,
1976                           /*destServer */ NULL, /*destPartition */ 0,
1977                           /*fromDate */ 0,
1978                           /*newExt */ NULL, /*oldFlag */ 0,
1979                           parent, level, bc_Dumper, portp, /*portCount */ 1,
1980                           baseds, doAppend, dontExecute);
1981     if (code)
1982         afs_com_err(whoami, code, "; Failed to queue dump");
1983
1984     return (code);
1985 }                               /*bc_DumpCmd */
1986
1987
1988 /* bc_QuitCmd
1989  *      terminate the backup process. Insists that that all running backup
1990  *      jobs be terminated before it will quit
1991  * parameters:
1992  *      ignored
1993  */
1994 int
1995 bc_QuitCmd(struct cmd_syndesc *as, void *arock)
1996 {
1997     int i;
1998     struct bc_dumpTask *td;
1999     extern dlqlinkT statusHead;
2000     dlqlinkP ptr;
2001     statusP statusPtr;
2002
2003     /* Check the status list for outstanding jobs */
2004     lock_Status();
2005     for (ptr = (&statusHead)->dlq_next; ptr != &statusHead;
2006          ptr = ptr->dlq_next) {
2007         statusPtr = (statusP) ptr;
2008         if (!(statusPtr->flags & ABORT_REQUEST)) {
2009             unlock_Status();
2010             afs_com_err(whoami, 0, "Job %d still running (and not aborted)",
2011                     statusPtr->jobNumber);
2012             afs_com_err(whoami, 0,
2013                     "You must at least 'kill' all running jobs before quitting");
2014             return -1;
2015         }
2016     }
2017     unlock_Status();
2018
2019     /* A job still being initialized (but no status structure or job number since it
2020      * has not been handed to a butc process yet)
2021      */
2022     for (td = bc_dumpTasks, i = 0; i < BC_MAXSIMDUMPS; i++, td++) {
2023         if (td->flags & BC_DI_INUSE) {
2024             afs_com_err(whoami, 0, "A job is still running");
2025             afs_com_err(whoami, 0,
2026                     "You must at least 'kill' all running jobs before quitting");
2027             return -1;
2028         }
2029     }
2030
2031 #ifdef AFS_NT40_ENV
2032     /* close the all temp text files before quitting */
2033     for (i = 0; i < TB_NUM; i++)
2034         bc_closeTextFile(&bc_globalConfig->configText[i],
2035                          &bc_globalConfig->tmpTextFileNames[i][0]);
2036 #endif
2037     exit(lastTaskCode);
2038 }
2039
2040 /* bc_LabelTapeCmd
2041  *      Labels a tape i.e. request the tape coordinator to perform this
2042  *      operation
2043  */
2044 int
2045 bc_LabelTapeCmd(struct cmd_syndesc *as, void *arock)
2046 {
2047     char *tapename = 0, *pname = 0;
2048     afs_int32 size;
2049     afs_int32 code;
2050     afs_int32 port = 0;
2051
2052     code = bc_UpdateHosts();
2053     if (code) {
2054         afs_com_err(whoami, code, "; Can't retrieve tape hosts");
2055         return (code);
2056     }
2057
2058     if (as->parms[0].items) {   /* -name */
2059         tapename = as->parms[0].items->data;
2060         if (strlen(tapename) >= TC_MAXTAPELEN) {
2061             afs_com_err(whoami, 0, "AFS tape name '%s' is too long", tapename);
2062             return -1;
2063         }
2064     }
2065
2066     if (as->parms[3].items) {   /* -pname */
2067         if (tapename) {
2068             afs_com_err(whoami, 0, "Can only specify -name or -pname");
2069             return -1;
2070         }
2071         pname = as->parms[3].items->data;
2072         if (strlen(pname) >= TC_MAXTAPELEN) {
2073             afs_com_err(whoami, 0, "Permanent tape name '%s' is too long", pname);
2074             return -1;
2075         }
2076     }
2077
2078     if (as->parms[1].items) {
2079         size = bc_FloatATOI(as->parms[1].items->data);
2080         if (size == -1) {
2081             afs_com_err(whoami, 0, "Bad syntax for tape size '%s'",
2082                     as->parms[1].items->data);
2083             return -1;
2084         }
2085     } else
2086         size = 0;
2087
2088     if (as->parms[2].items) {
2089         port = getPortOffset(as->parms[2].items->data);
2090         if (port < 0)
2091             return (BC_BADARG);
2092     }
2093
2094     code = bc_LabelTape(tapename, pname, size, bc_globalConfig, port);
2095     if (code)
2096         return code;
2097     return 0;
2098 }
2099
2100 /* bc_ReadLabelCmd
2101  *      read the label on a tape
2102  * params:
2103  *      optional port number
2104  */
2105 int
2106 bc_ReadLabelCmd(struct cmd_syndesc *as, void *arock)
2107 {
2108     afs_int32 code;
2109     afs_int32 port = 0;
2110
2111     code = bc_UpdateHosts();
2112     if (code) {
2113         afs_com_err(whoami, code, "; Can't retrieve tape hosts");
2114         return (code);
2115     }
2116
2117     if (as->parms[0].items) {
2118         port = getPortOffset(as->parms[0].items->data);
2119         if (port < 0)
2120             return (BC_BADARG);
2121     }
2122
2123     code = bc_ReadLabel(bc_globalConfig, port);
2124     if (code)
2125         return code;
2126     return 0;
2127 }
2128
2129 /* bc_ScanDumpsCmd
2130  *      read content information from dump tapes, and if user desires,
2131  *      add it to the database
2132  */
2133 int
2134 bc_ScanDumpsCmd(struct cmd_syndesc *as, void *arock)
2135 {
2136     afs_int32 port = 0;
2137     afs_int32 dbAddFlag = 0;
2138     afs_int32 code;
2139
2140     code = bc_UpdateHosts();
2141     if (code) {
2142         afs_com_err(whoami, code, "; Can't retrieve tape hosts");
2143         return (code);
2144     }
2145
2146     /* check for flag */
2147     if (as->parms[0].items != 0) {      /* add scan to database */
2148         dbAddFlag++;
2149     }
2150
2151     /* check for port */
2152     if (as->parms[1].items) {
2153         port = getPortOffset(as->parms[1].items->data);
2154         if (port < 0)
2155             return (BC_BADARG);
2156     }
2157
2158     code = bc_ScanDumps(bc_globalConfig, dbAddFlag, port);
2159     return (code);
2160 }
2161
2162 /* bc_ParseExpiration
2163  *
2164  * Notes:
2165  *      dates are specified as absolute or relative, the syntax is:
2166  *      absolute:       at %d/%d/%d [%d:%d]     where [..] is optional
2167  *      relative:       in [%dy][%dm][%dd]      where at least one component
2168  *                                              must be specified
2169  */
2170
2171 afs_int32
2172 bc_ParseExpiration(struct cmd_parmdesc *paramPtr, afs_int32 *expType,
2173                    afs_int32 *expDate)
2174 {
2175     struct cmd_item *itemPtr;
2176     struct ktime_date kt;
2177     char *dateString = 0;
2178     afs_int32 code = 0;
2179
2180     *expType = BC_NO_EXPDATE;
2181     *expDate = 0;
2182
2183     if (!paramPtr->items)
2184         ERROR(0);               /* no expiration specified */
2185
2186     /* some form of expiration date specified. First validate the prefix */
2187     itemPtr = paramPtr->items;
2188
2189     if (strcmp(itemPtr->data, "at") == 0) {
2190         *expType = BC_ABS_EXPDATE;
2191
2192         dateString = concatParams(itemPtr->next);
2193         if (!dateString)
2194             ERROR(1);
2195
2196         code = ktime_DateToLong(dateString, expDate);
2197         if (code)
2198             ERROR(1);
2199     } else if (strcmp(itemPtr->data, "in") == 0) {
2200         *expType = BC_REL_EXPDATE;
2201
2202         dateString = concatParams(itemPtr->next);
2203         if (!dateString)
2204             ERROR(1);
2205
2206         code = ParseRelDate(dateString, &kt);
2207         if (code)
2208             ERROR(1);
2209         *expDate = ktimeRelDate_ToLong(&kt);
2210     } else {
2211         dateString = concatParams(itemPtr);
2212         if (!dateString)
2213             ERROR(1);
2214
2215         if (ktime_DateToLong(dateString, expDate) == 0) {
2216             *expType = BC_ABS_EXPDATE;
2217             code = ktime_DateToLong(dateString, expDate);
2218             if (code)
2219                 ERROR(1);
2220         } else if (ParseRelDate(dateString, &kt) == 0) {
2221             *expType = BC_REL_EXPDATE;
2222             *expDate = ktimeRelDate_ToLong(&kt);
2223         } else {
2224             ERROR(1);
2225         }
2226     }
2227
2228   error_exit:
2229     if (dateString)
2230         free(dateString);
2231     return (code);
2232 }
2233
2234 /* database lookup command and routines */
2235
2236 /* bc_dblookupCmd
2237  *      Currently a single option, volumename to search for. Reports
2238  *      all dumps containing the specified volume
2239  */
2240 int
2241 bc_dblookupCmd(struct cmd_syndesc *as, void *arock)
2242 {
2243     struct cmd_item *ciptr;
2244     afs_int32 code;
2245
2246     ciptr = as->parms[0].items;
2247     if (ciptr == 0)             /* no argument specified */
2248         return (-1);
2249
2250     code = DBLookupByVolume(ciptr->data);
2251     return (code);
2252 }
2253
2254
2255
2256 /* for ubik version */
2257 int
2258 bc_dbVerifyCmd(struct cmd_syndesc *as, void *arock)
2259 {
2260     afs_int32 status;
2261     afs_int32 orphans;
2262     afs_int32 host;
2263
2264     struct hostent *hostPtr;
2265     int detail;
2266     afs_int32 code = 0;
2267
2268     extern struct udbHandleS udbHandle;
2269
2270     detail = (as->parms[0].items ? 1 : 0);      /* print more details */
2271
2272     code =
2273         ubik_BUDB_DbVerify(udbHandle.uh_client, 0, &status, &orphans,
2274                   &host);
2275
2276     if (code) {
2277         afs_com_err(whoami, code, "; Unable to verify database");
2278         return (-1);
2279     }
2280
2281     /* verification call succeeded */
2282
2283     if (status == 0)
2284         printf("Database OK\n");
2285     else
2286         afs_com_err(whoami, status, "; Database is NOT_OK");
2287
2288     if (detail) {
2289         printf("Orphan blocks %d\n", orphans);
2290
2291         if (!host)
2292             printf("Unable to lookup host id\n");
2293         else {
2294             hostPtr = gethostbyaddr((char *)&host, sizeof(host), AF_INET);
2295             if (hostPtr == 0)
2296                 printf("Database checker was %d.%d.%d.%d\n",
2297                        ((host & 0xFF000000) >> 24), ((host & 0xFF0000) >> 16),
2298                        ((host & 0xFF00) >> 8), (host & 0xFF));
2299             else
2300                 printf("Database checker was %s\n", hostPtr->h_name);
2301         }
2302     }
2303     return ((status ? -1 : 0));
2304 }
2305
2306 /* deleteDump:
2307  * Delete a dump. If port is >= 0, it means try to delete from XBSA server
2308  */
2309 int
2310 deleteDump(afs_uint32 dumpid,           /* The dumpid to delete */
2311            afs_int32 port,              /* port==-1 means don't go to butc */
2312            afs_int32 force)
2313 {
2314     afs_int32 code = 0, tcode;
2315     struct budb_dumpEntry dumpEntry;
2316     struct rx_connection *tconn = 0;
2317     afs_int32 i, taskflag, xbsadump;
2318     statusP statusPtr = 0;
2319     budb_dumpsList dumps;
2320     afs_uint32 taskId;
2321
2322     /* If the port is set, we will try to send a delete request to the butc */
2323     if (port >= 0) {
2324         tcode = bc_UpdateHosts();
2325         if (tcode) {
2326             afs_com_err(whoami, tcode, "; Can't retrieve tape hosts");
2327             ERROR(tcode);
2328         }
2329
2330         /* Find the dump in the backup database */
2331         tcode = bcdb_FindDumpByID(dumpid, &dumpEntry);
2332         if (tcode) {
2333             afs_com_err(whoami, tcode, "; Unable to locate dumpID %u in database",
2334                     dumpid);
2335             ERROR(tcode);
2336         }
2337         xbsadump = (dumpEntry.flags & (BUDB_DUMP_ADSM | BUDB_DUMP_BUTA));
2338
2339         /* If dump is to an XBSA server, connect to butc and send it
2340          * the dump to delete. Butc will contact the XBSA server.
2341          * The dump will not be an appended dump because XBSA butc
2342          * does not support the append option.
2343          */
2344         if (xbsadump && dumpEntry.nVolumes) {
2345             tcode = ConnectButc(bc_globalConfig, port, &tconn);
2346             if (tcode)
2347                 ERROR(tcode);
2348
2349             tcode = TC_DeleteDump(tconn, dumpid, &taskId);
2350             if (tcode) {
2351                 if (tcode == RXGEN_OPCODE)
2352                     tcode = BC_VERSIONFAIL;
2353                 afs_com_err(whoami, tcode,
2354                         "; Unable to delete dumpID %u via butc", dumpid);
2355                 ERROR(tcode);
2356             }
2357
2358             statusPtr = createStatusNode();
2359             lock_Status();
2360             statusPtr->taskId = taskId;
2361             statusPtr->port = port;
2362             statusPtr->jobNumber = bc_jobNumber();
2363             statusPtr->flags |= (SILENT | NOREMOVE);    /* No msg & keep statusPtr */
2364             statusPtr->flags &= ~STARTING;      /* clearstatus to examine */
2365             sprintf(statusPtr->taskName, "DeleteDump");
2366             unlock_Status();
2367
2368             /* Wait for task to finish */
2369             taskflag = waitForTask(taskId);
2370             if (taskflag & (TASK_ERROR | ABORT_DONE)) {
2371                 afs_com_err(whoami, BUTX_DELETEOBJFAIL,
2372                         "; Unable to delete dumpID %u via butc", dumpid);
2373                 ERROR(BUTX_DELETEOBJFAIL);      /* the task failed */
2374             }
2375         }
2376     }
2377
2378   error_exit:
2379     if (statusPtr)
2380         deleteStatusNode(statusPtr);    /* Clean up statusPtr - because NOREMOVE */
2381     if (tconn)
2382         rx_DestroyConnection(tconn);    /* Destroy the connection */
2383
2384     /* Remove the dump from the backup database */
2385     if (!code || force) {
2386         dumps.budb_dumpsList_len = 0;
2387         dumps.budb_dumpsList_val = 0;
2388
2389         tcode = bcdb_deleteDump(dumpid, 0, 0, &dumps);
2390         if (tcode) {
2391             afs_com_err(whoami, tcode,
2392                     "; Unable to delete dumpID %u from database", dumpid);
2393             dumps.budb_dumpsList_len = 0;
2394             if (!code)
2395                 code = tcode;
2396         }
2397
2398         /* Display the dumps that were deleted - includes appended dumps */
2399         for (i = 0; i < dumps.budb_dumpsList_len; i++)
2400             printf("     %u%s\n", dumps.budb_dumpsList_val[i],
2401                    (i > 0) ? " Appended Dump" : NULL);
2402         if (dumps.budb_dumpsList_val)
2403             free(dumps.budb_dumpsList_val);
2404     }
2405
2406     return code;
2407 }
2408
2409 /* bc_deleteDumpCmd
2410  *      Delete a specified dump from the database
2411  * entry:
2412  *      dump id - single required arg as param 0.
2413  */
2414 int
2415 bc_deleteDumpCmd(struct cmd_syndesc *as, void *arock)
2416 {
2417     afs_uint32 dumpid;
2418     afs_int32 code = 0;
2419     afs_int32 rcode = 0;
2420     afs_int32 groupId = 0, havegroupid, sflags, noexecute;
2421     struct cmd_item *ti;
2422     afs_int32 fromTime = 0, toTime = 0, havetime = 0;
2423     char *timeString;
2424     budb_dumpsList dumps, flags;
2425     int i;
2426     afs_int32 port = -1, force;
2427
2428     /* Must specify at least one of -dumpid, -from, or -to */
2429     if (!as->parms[0].items && !as->parms[1].items && !as->parms[2].items
2430         && !as->parms[4].items) {
2431         afs_com_err(whoami, 0, "Must specify at least one field");
2432         return (-1);
2433     }
2434
2435     /* Must have -to option with -from option */
2436     if (as->parms[1].items && !as->parms[2].items) {
2437         afs_com_err(whoami, 0, "Must specify '-to' field with '-from' field");
2438         return (-1);
2439     }
2440
2441     /* Get the time to delete from */
2442     if (as->parms[1].items) {   /* -from */
2443         timeString = concatParams(as->parms[1].items);
2444         if (!timeString)
2445             return (-1);
2446
2447         /*
2448          * Now parse this string for the time to start.
2449          */
2450         code = ktime_DateToLong(timeString, &fromTime);
2451         free(timeString);
2452         if (code) {
2453             afs_com_err(whoami, 0, "Can't parse 'from' date and time");
2454             afs_com_err(whoami, 0, "%s", ktime_GetDateUsage());
2455             return (-1);
2456         }
2457         havetime = 1;
2458     }
2459
2460     port = (as->parms[3].items ? getPortOffset(as->parms[3].items->data) : 0);  /* -port */
2461     if (as->parms[5].items)     /* -dbonly */
2462         port = -1;
2463
2464     force = (as->parms[6].items ? 1 : 0);
2465
2466     havegroupid = (as->parms[4].items ? 1 : 0);
2467     if (havegroupid)
2468         groupId = atoi(as->parms[4].items->data);
2469
2470     if (as->parms[7].items || as->parms[8].items) {
2471         /* -noexecute (hidden) or -dryrun used */
2472         noexecute = 1;
2473     } else {
2474         noexecute = 0;
2475     }
2476
2477     /* Get the time to delete to */
2478     if (as->parms[2].items) {   /* -to */
2479         timeString = concatParams(as->parms[2].items);
2480         if (!timeString)
2481             return (-1);
2482
2483         /*
2484          * Now parse this string for the time to start. Simce
2485          * times are at minute granularity, add 59 seconds.
2486          */
2487         code = ktime_DateToLong(timeString, &toTime);
2488         free(timeString);
2489         if (code) {
2490             afs_com_err(whoami, 0, "Can't parse 'to' date and time");
2491             afs_com_err(whoami, 0, "%s", ktime_GetDateUsage());
2492             return (-1);
2493         }
2494         toTime += 59;
2495         havetime = 1;
2496     }
2497
2498     if (fromTime > toTime) {
2499         afs_com_err(whoami, 0,
2500                 "'-from' date/time cannot be later than '-to' date/time");
2501         return (-1);
2502     }
2503
2504     /* Remove speicific dump ids - if any */
2505     printf("The following dumps %s deleted:\n",
2506            (noexecute ? "would have been" : "were"));
2507     for (ti = as->parms[0].items; ti != 0; ti = ti->next) {     /* -dumpid */
2508         dumpid = atoi(ti->data);
2509         if (!noexecute) {
2510             code = deleteDump(dumpid, port, force);
2511         } else {
2512             printf("     %u\n", dumpid);
2513         }
2514     }
2515
2516     /*
2517      * Now remove dumps between to and from dates.
2518      */
2519     if (havegroupid || havetime) {
2520         dumps.budb_dumpsList_len = 0;
2521         dumps.budb_dumpsList_val = 0;
2522         flags.budb_dumpsList_len = 0;
2523         flags.budb_dumpsList_val = 0;
2524         sflags = 0;
2525         if (havegroupid)
2526             sflags |= BUDB_OP_GROUPID;
2527         if (havetime)
2528             sflags |= BUDB_OP_DATES;
2529
2530         code =
2531             bcdb_listDumps(sflags, groupId, fromTime, toTime, &dumps, &flags);
2532         if (code) {
2533             afs_com_err(whoami, code,
2534                     "; Error while deleting dumps from %u to %u", fromTime,
2535                     toTime);
2536             rcode = -1;
2537         }
2538
2539         for (i = 0; i < dumps.budb_dumpsList_len; i++) {
2540             if (flags.budb_dumpsList_val[i] & BUDB_OP_DBDUMP)
2541                 continue;
2542
2543             if (!noexecute) {
2544                 if (flags.budb_dumpsList_val[i] & BUDB_OP_APPDUMP)
2545                     continue;
2546                 code = deleteDump(dumps.budb_dumpsList_val[i], port, force);
2547                 /* Ignore code and continue */
2548             } else {
2549                 printf("     %u%s%s\n", dumps.budb_dumpsList_val[i],
2550                        (flags.
2551                         budb_dumpsList_val[i] & BUDB_OP_APPDUMP) ?
2552                        " Appended Dump" : "",
2553                        (flags.
2554                         budb_dumpsList_val[i] & BUDB_OP_DBDUMP) ?
2555                        " Database Dump" : "");
2556
2557             }
2558         }
2559
2560         if (dumps.budb_dumpsList_val)
2561             free(dumps.budb_dumpsList_val);
2562         dumps.budb_dumpsList_len = 0;
2563         dumps.budb_dumpsList_val = 0;
2564         if (flags.budb_dumpsList_val)
2565             free(flags.budb_dumpsList_val);
2566         flags.budb_dumpsList_len = 0;
2567         flags.budb_dumpsList_val = 0;
2568     }
2569
2570     return (rcode);
2571 }
2572
2573 int
2574 bc_saveDbCmd(struct cmd_syndesc *as, void *arock)
2575 {
2576     struct rx_connection *tconn;
2577     afs_int32 portOffset = 0;
2578     statusP statusPtr;
2579     afs_uint32 taskId;
2580     afs_int32 code;
2581     afs_uint32 toTime;
2582     char *timeString;
2583
2584     code = bc_UpdateHosts();
2585     if (code) {
2586         afs_com_err(whoami, code, "; Can't retrieve tape hosts");
2587         return (code);
2588     }
2589
2590     if (as->parms[0].items) {
2591         portOffset = getPortOffset(as->parms[0].items->data);
2592         if (portOffset < 0)
2593             return (BC_BADARG);
2594     }
2595
2596     /* Get the time to delete to */
2597     if (as->parms[1].items) {
2598         timeString = concatParams(as->parms[1].items);
2599         if (!timeString)
2600             return (-1);
2601
2602         /*
2603          * Now parse this string for the time. Since
2604          * times are at minute granularity, add 59 seconds.
2605          */
2606         code = ktime_DateToLong(timeString, &toTime);
2607         free(timeString);
2608         if (code) {
2609             afs_com_err(whoami, 0, "Can't parse '-archive' date and time");
2610             afs_com_err(whoami, 0, "%s", ktime_GetDateUsage());
2611             return (-1);
2612         }
2613         toTime += 59;
2614     } else
2615         toTime = 0;
2616
2617     code = ConnectButc(bc_globalConfig, portOffset, &tconn);
2618     if (code)
2619         return (code);
2620
2621     code = TC_SaveDb(tconn, toTime, &taskId);
2622     if (code) {
2623         afs_com_err(whoami, code, "; Failed to save database");
2624         goto exit;
2625     }
2626
2627     /* create status monitor block */
2628     statusPtr = createStatusNode();
2629     lock_Status();
2630     statusPtr->taskId = taskId;
2631     statusPtr->port = portOffset;
2632     statusPtr->jobNumber = bc_jobNumber();
2633     statusPtr->flags &= ~STARTING;      /* clearstatus to examine */
2634     sprintf(statusPtr->taskName, "SaveDb");
2635     unlock_Status();
2636
2637   exit:
2638     rx_DestroyConnection(tconn);
2639     return (code);
2640 }
2641
2642 int
2643 bc_restoreDbCmd(struct cmd_syndesc *as, void *arock)
2644 {
2645     struct rx_connection *tconn;
2646     afs_int32 portOffset = 0;
2647     statusP statusPtr;
2648     afs_uint32 taskId;
2649     afs_int32 code;
2650
2651     code = bc_UpdateHosts();
2652     if (code) {
2653         afs_com_err(whoami, code, "; Can't retrieve tape hosts");
2654         return (code);
2655     }
2656
2657     if (as->parms[0].items) {
2658         portOffset = getPortOffset(as->parms[0].items->data);
2659         if (portOffset < 0)
2660             return (BC_BADARG);
2661     }
2662
2663     code = ConnectButc(bc_globalConfig, portOffset, &tconn);
2664     if (code)
2665         return (code);
2666
2667     code = TC_RestoreDb(tconn, &taskId);
2668     if (code) {
2669         afs_com_err(whoami, code, "; Failed to restore database");
2670         goto exit;
2671     }
2672
2673     /* create status monitor block */
2674     statusPtr = createStatusNode();
2675     lock_Status();
2676     statusPtr->taskId = taskId;
2677     statusPtr->port = portOffset;
2678     statusPtr->jobNumber = bc_jobNumber();
2679     statusPtr->flags &= ~STARTING;      /* clearstatus to examine */
2680     sprintf(statusPtr->taskName, "RestoreDb");
2681     unlock_Status();
2682
2683   exit:
2684     rx_DestroyConnection(tconn);
2685     return (code);
2686 }
2687
2688 /* ----------------------------------
2689  * supporting routines for database examination
2690  * ----------------------------------
2691  */
2692
2693 /* structures and defines for DBLookupByVolume */
2694
2695 #define DBL_MAX_VOLUMES 20      /* max. for each dump */
2696
2697 /* dumpedVol - saves interesting information so that we can print it out
2698  *      later
2699  */
2700
2701 struct dumpedVol {
2702     struct dumpedVol *next;
2703     afs_int32 dumpID;
2704     afs_int32 initialDumpID;
2705     char tapeName[BU_MAXTAPELEN];
2706     afs_int32 level;
2707     afs_int32 parent;
2708     afs_int32 createTime;
2709     afs_int32 incTime;          /* actually the clone time */
2710 };
2711
2712 /* -----------------------------------------
2713  * routines for examining the database
2714  * -----------------------------------------
2715  */
2716
2717 /* DBLookupByVolume
2718  *      Lookup the volumename in the backup database and print the results
2719  * entry:
2720  *      volumeName - volume to lookup
2721  */
2722
2723 static int
2724 DBLookupByVolume(char *volumeName)
2725 {
2726     struct budb_dumpEntry dumpEntry;
2727     struct budb_volumeEntry volumeEntry[DBL_MAX_VOLUMES];
2728     afs_int32 numEntries;
2729     afs_int32 tapedumpid;
2730     afs_int32 last, next;
2731
2732     struct dumpedVol *dvptr = 0;
2733     struct dumpedVol *tempPtr = 0;
2734     afs_int32 code = 0;
2735     int i, pass;
2736     char vname[BU_MAXNAMELEN];
2737     char ds[50];
2738
2739     for (pass = 0; pass < 2; pass++) {
2740         /*p */
2741         /* On second pass, search for backup volume */
2742         if (pass == 1) {
2743             if (!BackupName(volumeName)) {
2744                 strcpy(vname, volumeName);
2745                 strcat(vname, ".backup");
2746                 volumeName = vname;
2747             } else {
2748                 continue;
2749             }
2750         }
2751
2752         last = next = 0;
2753         while (next != -1) {    /*w */
2754             code =
2755                 bcdb_LookupVolume(volumeName, &volumeEntry[0], last, &next,
2756                                   DBL_MAX_VOLUMES, &numEntries);
2757             if (code)
2758                 break;
2759
2760             /* add the volumes to the list */
2761             for (i = 0; i < numEntries; i++) {  /*f */
2762                 struct dumpedVol *insPtr, **prevPtr;
2763
2764                 tempPtr =
2765                     (struct dumpedVol *)malloc(sizeof(struct dumpedVol));
2766                 if (!tempPtr)
2767                     ERROR(BC_NOMEM);
2768
2769                 memset(tempPtr, 0, sizeof(*tempPtr));
2770                 tempPtr->incTime = volumeEntry[i].clone;
2771                 tempPtr->dumpID = volumeEntry[i].dump;
2772                 strncpy(tempPtr->tapeName, volumeEntry[i].tape,
2773                         BU_MAXTAPELEN);
2774
2775                 /* check if we need to null terminate it - just for safety */
2776                 if (strlen(volumeEntry[i].tape) >= BU_MAXTAPELEN)
2777                     tempPtr->tapeName[BU_MAXTAPELEN - 1] = 0;
2778
2779                 code = bcdb_FindDumpByID(tempPtr->dumpID, &dumpEntry);
2780                 if (code) {
2781                     free(tempPtr);
2782                     ERROR(code);
2783                 }
2784
2785                 tempPtr->initialDumpID = dumpEntry.initialDumpID;
2786                 tempPtr->parent = dumpEntry.parent;
2787                 tempPtr->level = dumpEntry.level;
2788                 tempPtr->createTime = dumpEntry.created;
2789
2790                 /* add volume to list in reverse chronological order */
2791                 prevPtr = &dvptr;
2792                 insPtr = dvptr;
2793
2794                 while ((insPtr != 0)
2795                        && (insPtr->createTime > tempPtr->createTime)
2796                     ) {
2797                     prevPtr = &insPtr->next;
2798                     insPtr = insPtr->next;
2799                 }
2800
2801                 /* now at the right place - insert the block */
2802                 tempPtr->next = *prevPtr;
2803                 *prevPtr = tempPtr;
2804             }                   /*f */
2805
2806             last = next;
2807         }                       /*w */
2808     }                           /*p */
2809
2810     if (dvptr) {
2811         printf
2812             ("DumpID    lvl parentID creation date     clone date       tape name\n");
2813         for (tempPtr = dvptr; tempPtr; tempPtr = tempPtr->next) {
2814             /* For the user, the tape name is its name and initial dump id */
2815             tapedumpid =
2816                 (tempPtr->initialDumpID ? tempPtr->initialDumpID : tempPtr->
2817                  dumpID);
2818
2819             /* beware the static items in compactDateString */
2820             compactDateString(&tempPtr->createTime, ds, 50);
2821             printf("%-9d %-2d %-9d %16s", tempPtr->dumpID, tempPtr->level,
2822                    tempPtr->parent, ds);
2823             compactDateString(&tempPtr->incTime, ds, 50);
2824             printf("  %16s %s (%u)\n", ds, tempPtr->tapeName, tapedumpid);
2825         }
2826         code = 0;
2827     }
2828
2829   error_exit:
2830     for (tempPtr = dvptr; tempPtr; tempPtr = dvptr) {
2831         dvptr = dvptr->next;
2832         free(tempPtr);
2833     }
2834
2835     if (code)
2836         afs_com_err(whoami, code, NULL);
2837     return (code);
2838 }
2839
2840 /* structures for dumpInfo */
2841
2842 struct volumeLink {
2843     struct volumeLink *nextVolume;
2844     struct budb_volumeEntry volumeEntry;
2845 };
2846
2847 struct tapeLink {
2848     struct tapeLink *nextTape;
2849     struct budb_tapeEntry tapeEntry;
2850     struct volumeLink *firstVolume;
2851 };
2852
2853
2854 /* dumpInfo
2855  *      print information about a dump and all its tapes and volumes.
2856  */
2857
2858 afs_int32
2859 dumpInfo(afs_int32 dumpid, afs_int32 detailFlag)
2860 {
2861     struct budb_dumpEntry dumpEntry;
2862     struct tapeLink *head = 0;
2863     struct tapeLink *tapeLinkPtr, *lastTapeLinkPtr;
2864     struct volumeLink **link, *volumeLinkPtr, *lastVolumeLinkPtr;
2865
2866     budb_volumeList vl;
2867     afs_int32 last, next, dbTime;
2868     afs_int32 tapedumpid;
2869
2870     int tapeNumber;
2871     int i;
2872     int dbDump;
2873     afs_int32 code = 0;
2874     char ds[50];
2875
2876     extern struct udbHandleS udbHandle;
2877
2878     tapeLinkPtr = 0;
2879     lastTapeLinkPtr = 0;
2880     volumeLinkPtr = 0;
2881     lastVolumeLinkPtr = 0;
2882
2883     /* first get information about the dump */
2884
2885     code = bcdb_FindDumpByID(dumpid, &dumpEntry);
2886     if (code)
2887         ERROR(code);
2888
2889     /* For the user, the tape name is its name and initial dump id */
2890     tapedumpid = (dumpEntry.initialDumpID ? dumpEntry.initialDumpID : dumpid);
2891
2892     /* Is this a database dump id or not */
2893     if (strcmp(dumpEntry.name, DUMP_TAPE_NAME) == 0)
2894         dbDump = 1;
2895     else
2896         dbDump = 0;
2897
2898     /* print out the information about the dump */
2899     if (detailFlag) {
2900         printf("\nDump\n");
2901         printf("----\n");
2902         printDumpEntry(&dumpEntry);
2903     } else {
2904         time_t t = dumpEntry.created;
2905         if (dbDump)
2906             printf("Dump: id %u, created: %s\n", dumpEntry.id,
2907                    ctime(&t));
2908         else
2909             printf("Dump: id %u, level %d, volumes %d, created: %s\n",
2910                    dumpEntry.id, dumpEntry.level, dumpEntry.nVolumes,
2911                    ctime(&t));
2912     }
2913
2914     if (!detailFlag && (strlen(dumpEntry.tapes.tapeServer) > 0)
2915         && (dumpEntry.flags & (BUDB_DUMP_ADSM | BUDB_DUMP_BUTA))) {
2916         printf("Backup Service: TSM: Server: %s\n",
2917                dumpEntry.tapes.tapeServer);
2918     }
2919
2920     /* now get the list of tapes */
2921     for (tapeNumber = dumpEntry.tapes.b; tapeNumber <= dumpEntry.tapes.maxTapes; tapeNumber++) {        /*f */
2922         tapeLinkPtr = (struct tapeLink *)malloc(sizeof(struct tapeLink));
2923         if (!tapeLinkPtr) {
2924             afs_com_err(whoami, BC_NOMEM, NULL);
2925             ERROR(BC_NOMEM);
2926         }
2927
2928         memset(tapeLinkPtr, 0, sizeof(*tapeLinkPtr));
2929         code = bcdb_FindTapeSeq(dumpid, tapeNumber, &tapeLinkPtr->tapeEntry);
2930         if (code) {
2931             code = 0;
2932             free(tapeLinkPtr);
2933             continue;
2934         }
2935
2936         /* add this tape to  previous chain */
2937         if (lastTapeLinkPtr) {
2938             lastTapeLinkPtr->nextTape = tapeLinkPtr;
2939             lastTapeLinkPtr = tapeLinkPtr;
2940         }
2941
2942         if (head == 0) {
2943             head = tapeLinkPtr;
2944             lastTapeLinkPtr = head;
2945         }
2946
2947         next = 0;
2948         while (next != -1) {    /*wn */
2949             vl.budb_volumeList_len = 0;
2950             vl.budb_volumeList_val = 0;
2951             last = next;
2952
2953             /* now get all the volumes in this dump. */
2954             code = ubik_Call_SingleServer(BUDB_GetVolumes, udbHandle.uh_client, UF_SINGLESERVER, BUDB_MAJORVERSION, BUDB_OP_DUMPID | BUDB_OP_TAPENAME, tapeLinkPtr->tapeEntry.name,     /* tape name */
2955                                           dumpid,       /* dumpid (not initial dumpid) */
2956                                           0,    /* end */
2957                                           last, /* last */
2958                                           &next,        /* nextindex */
2959                                           &dbTime,      /* update time */
2960                                           &vl);
2961
2962             if (code) {
2963                 if (code == BUDB_ENDOFLIST) {   /* 0 volumes on tape */
2964                     code = 0;
2965                     break;
2966                 }
2967                 ERROR(code);
2968             }
2969
2970             for (i = 0; i < vl.budb_volumeList_len; i++) {
2971                 link = &tapeLinkPtr->firstVolume;
2972
2973                 volumeLinkPtr =
2974                     (struct volumeLink *)malloc(sizeof(struct volumeLink));
2975                 if (!volumeLinkPtr) {
2976                     afs_com_err(whoami, BC_NOMEM, NULL);
2977                     ERROR(BC_NOMEM);
2978                 }
2979                 memset(volumeLinkPtr, 0, sizeof(*volumeLinkPtr));
2980
2981                 memcpy(&volumeLinkPtr->volumeEntry,
2982                        &vl.budb_volumeList_val[i],
2983                        sizeof(struct budb_volumeEntry));
2984
2985                 /* now insert it onto the right place */
2986                 while ((*link != 0)
2987                        && (volumeLinkPtr->volumeEntry.position >
2988                            (*link)->volumeEntry.position)) {
2989                     link = &((*link)->nextVolume);
2990                 }
2991
2992                 /* now link it in */
2993                 volumeLinkPtr->nextVolume = *link;
2994                 *link = volumeLinkPtr;
2995             }
2996
2997             if (vl.budb_volumeList_val)
2998                 free(vl.budb_volumeList_val);
2999         }                       /*wn */
3000     }                           /*f */
3001
3002     for (tapeLinkPtr = head; tapeLinkPtr; tapeLinkPtr = tapeLinkPtr->nextTape) {
3003         if (detailFlag) {
3004             printf("\nTape\n");
3005             printf("----\n");
3006             printTapeEntry(&tapeLinkPtr->tapeEntry);
3007         } else {
3008             printf("Tape: name %s (%u)\n", tapeLinkPtr->tapeEntry.name,
3009                    tapedumpid);
3010             printf("nVolumes %d, ", tapeLinkPtr->tapeEntry.nVolumes);
3011             compactDateString(&tapeLinkPtr->tapeEntry.written, ds, 50);
3012             printf("created %16s", ds);
3013             if (tapeLinkPtr->tapeEntry.expires != 0) {
3014                 compactDateString(&tapeLinkPtr->tapeEntry.expires, ds, 50);
3015                 printf(", expires %16s", ds);
3016             }
3017             printf("\n\n");
3018         }
3019
3020         /* print out all the volumes */
3021
3022         /* print header for volume listing - db dumps have no volumes */
3023         if ((detailFlag == 0) && !dbDump)
3024             printf("%4s %16s %9s %-s\n", "Pos", "Clone time", "Nbytes",
3025                    "Volume");
3026
3027         for (volumeLinkPtr = tapeLinkPtr->firstVolume; volumeLinkPtr;
3028              volumeLinkPtr = volumeLinkPtr->nextVolume) {
3029             if (detailFlag) {
3030                 printf("\nVolume\n");
3031                 printf("------\n");
3032                 printVolumeEntry(&volumeLinkPtr->volumeEntry);
3033             } else {
3034                 compactDateString(&volumeLinkPtr->volumeEntry.clone, ds, 50),
3035                     printf("%4d %s %10u %16s\n",
3036                            volumeLinkPtr->volumeEntry.position, ds,
3037                            volumeLinkPtr->volumeEntry.nBytes,
3038                            volumeLinkPtr->volumeEntry.name);
3039             }
3040         }
3041     }
3042
3043   error_exit:
3044     if (code)
3045         afs_com_err("dumpInfo", code, "; Can't get dump information");
3046
3047     /* free all allocated structures */
3048     tapeLinkPtr = head;
3049     while (tapeLinkPtr) {
3050         volumeLinkPtr = tapeLinkPtr->firstVolume;
3051         while (volumeLinkPtr) {
3052             lastVolumeLinkPtr = volumeLinkPtr;
3053             volumeLinkPtr = volumeLinkPtr->nextVolume;
3054             free(lastVolumeLinkPtr);
3055         }
3056
3057         lastTapeLinkPtr = tapeLinkPtr;
3058         tapeLinkPtr = tapeLinkPtr->nextTape;
3059         free(lastTapeLinkPtr);
3060     }
3061     return (code);
3062 }
3063
3064 int
3065 compareDump(struct budb_dumpEntry *ptr1, struct budb_dumpEntry *ptr2)
3066 {
3067     if (ptr1->created < ptr2->created)
3068         return (-1);
3069     else if (ptr1->created > ptr2->created)
3070         return (1);
3071     return (0);
3072 }
3073
3074 afs_int32
3075 printRecentDumps(int ndumps)
3076 {
3077     afs_int32 code = 0;
3078     afs_int32 nextindex, index = 0;
3079     afs_int32 dbTime;
3080     budb_dumpList dl;
3081     struct budb_dumpEntry *dumpPtr;
3082     int i;
3083     char ds[50];
3084
3085     extern struct udbHandleS udbHandle;
3086
3087     do {                        /* while (nextindex != -1) */
3088         /* initialize the dump list */
3089         dl.budb_dumpList_len = 0;
3090         dl.budb_dumpList_val = 0;
3091
3092         /* outline algorithm */
3093         code = ubik_BUDB_GetDumps(udbHandle.uh_client, 0, BUDB_MAJORVERSION, BUDB_OP_NPREVIOUS, "",     /* no name */
3094                          0,     /* start */
3095                          ndumps,        /* end */
3096                          index, /* index */
3097                          &nextindex, &dbTime, &dl);
3098         if (code) {
3099             if (code == BUDB_ENDOFLIST)
3100                 return 0;
3101             afs_com_err("dumpInfo", code, "; Can't get dump information");
3102             return (code);
3103         }
3104
3105         /* No need to sort, it's already sorted */
3106
3107         if (dl.budb_dumpList_len && (index == 0))
3108             printf("%10s %10s %2s %-16s %2s %5s dump name\n", "dumpid",
3109                    "parentid", "lv", "created", "nt", "nvols");
3110
3111         dumpPtr = dl.budb_dumpList_val;
3112         for (i = 1; i <= dl.budb_dumpList_len; i++) {
3113             compactDateString(&dumpPtr->created, ds, 50),
3114                 printf("%10u %10u %-2d %16s %2d %5d %s", dumpPtr->id,
3115                        dumpPtr->parent, dumpPtr->level, ds,
3116                        dumpPtr->tapes.maxTapes - dumpPtr->tapes.b + 1,
3117                        dumpPtr->nVolumes, dumpPtr->name);
3118             if (dumpPtr->initialDumpID) /* an appended dump */
3119                 printf(" (%u)", dumpPtr->initialDumpID);
3120             else if (dumpPtr->appendedDumpID)   /* has appended dumps */
3121                 printf(" (%u)", dumpPtr->id);
3122             printf("\n");
3123
3124             dumpPtr++;
3125         }
3126
3127         if (dl.budb_dumpList_val)
3128             free(dl.budb_dumpList_val);
3129         index = nextindex;
3130     } while (nextindex != -1);
3131
3132     return (code);
3133 }
3134
3135 /* bc_dumpInfoCmd
3136  *      list the dumps and contens of the dumps.
3137  * params:
3138  *      as - name of tape
3139  *      arock -
3140  */
3141 int
3142 bc_dumpInfoCmd(struct cmd_syndesc *as, void *arock)
3143 {
3144     afs_int32 dumpid;
3145     afs_int32 detailFlag;
3146     afs_int32 ndumps;
3147     afs_int32 code = 0;
3148
3149     if (as->parms[0].items) {
3150         if (as->parms[1].items) {
3151             afs_com_err(whoami, 0,
3152                     "These options are exclusive - select only one");
3153             return (BC_BADARG);
3154         }
3155         ndumps = atoi(as->parms[0].items->data);
3156         if (ndumps <= 0) {
3157             afs_com_err(whoami, 0, "Must provide a positive number");
3158             return -1;
3159         }
3160
3161         code = printRecentDumps(ndumps);
3162     } else if (as->parms[1].items) {
3163         detailFlag = (as->parms[2].items ? 1 : 0);      /* 1 = detailed listing */
3164         dumpid = atoi(as->parms[1].items->data);
3165         code = dumpInfo(dumpid, detailFlag);
3166     } else {
3167         code = printRecentDumps(10);
3168     }
3169
3170     return (code);
3171 }