vos: Unrecognized or ambiguous switch '-c'
[openafs.git] / src / volser / vos.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
13 #include <roken.h>
14
15 #ifdef IGNORE_SOME_GCC_WARNINGS
16 # pragma GCC diagnostic warning "-Wimplicit-function-declaration"
17 #endif
18
19 #ifdef AFS_NT40_ENV
20 #include <WINNT/afsreg.h>
21 #endif
22
23 #ifdef AFS_AIX_ENV
24 #include <sys/statfs.h>
25 #endif
26
27 #include <lock.h>
28 #include <afs/stds.h>
29 #include <rx/rx_queue.h>
30 #include <rx/xdr.h>
31 #include <rx/rx.h>
32 #include <rx/rx_globals.h>
33 #include <afs/nfs.h>
34 #include <afs/vlserver.h>
35 #include <afs/cellconfig.h>
36 #include <afs/keys.h>
37 #include <afs/afsutil.h>
38 #include <ubik.h>
39 #include <afs/afsint.h>
40 #include <afs/cmd.h>
41 #include <afs/usd.h>
42 #include "volser.h"
43 #include "volint.h"
44 #include <afs/ihandle.h>
45 #include <afs/vnode.h>
46 #include <afs/volume.h>
47 #include <afs/com_err.h>
48 #include "dump.h"
49 #include "lockdata.h"
50
51 #include "volser_internal.h"
52 #include "volser_prototypes.h"
53 #include "vsutils_prototypes.h"
54 #include "lockprocs_prototypes.h"
55
56 #ifdef HAVE_POSIX_REGEX
57 #include <regex.h>
58 #endif
59
60 /* Local Prototypes */
61 int PrintDiagnostics(char *astring, afs_int32 acode);
62 int GetVolumeInfo(afs_uint32 volid, afs_uint32 *server, afs_int32 *part,
63                   afs_int32 *voltype, struct nvldbentry *rentry);
64
65 struct tqElem {
66     afs_uint32 volid;
67     struct tqElem *next;
68 };
69
70 struct tqHead {
71     afs_int32 count;
72     struct tqElem *next;
73 };
74
75 #define COMMONPARMS     cmd_Seek(ts, 12);\
76 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cell name");\
77 cmd_AddParmAlias(ts, 1, "-c");   /* original -cell option */ \
78 cmd_AddParm(ts, "-noauth", CMD_FLAG, CMD_OPTIONAL, "don't authenticate");\
79 cmd_AddParm(ts, "-localauth",CMD_FLAG,CMD_OPTIONAL,"use server tickets");\
80 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, "verbose");\
81 cmd_AddParm(ts, "-encrypt", CMD_FLAG, CMD_OPTIONAL, "encrypt commands");\
82 cmd_AddParm(ts, "-noresolve", CMD_FLAG, CMD_OPTIONAL, "don't resolve addresses"); \
83 cmd_AddParm(ts, "-config", CMD_SINGLE, CMD_OPTIONAL, "config location"); \
84
85 #define ERROR_EXIT(code) do { \
86     error = (code); \
87     goto error_exit; \
88 } while (0)
89
90 int rxInitDone = 0;
91 extern struct ubik_client *cstruct;
92 const char *confdir;
93
94 static struct tqHead busyHead, notokHead;
95
96 static void
97 qInit(struct tqHead *ahead)
98 {
99     memset(ahead, 0, sizeof(struct tqHead));
100     return;
101 }
102
103
104 static void
105 qPut(struct tqHead *ahead, afs_uint32 volid)
106 {
107     struct tqElem *elem;
108
109     elem = malloc(sizeof(struct tqElem));
110     elem->next = ahead->next;
111     elem->volid = volid;
112     ahead->next = elem;
113     ahead->count++;
114     return;
115 }
116
117 static void
118 qGet(struct tqHead *ahead, afs_uint32 *volid)
119 {
120     struct tqElem *tmp;
121
122     if (ahead->count <= 0)
123         return;
124     *volid = ahead->next->volid;
125     tmp = ahead->next;
126     ahead->next = tmp->next;
127     ahead->count--;
128     free(tmp);
129     return;
130 }
131
132 /* returns 1 if <filename> exists else 0 */
133 static int
134 FileExists(char *filename)
135 {
136     usd_handle_t ufd;
137     int code;
138     afs_int64 size;
139
140     code = usd_Open(filename, USD_OPEN_RDONLY, 0, &ufd);
141     if (code) {
142         return 0;
143     }
144     code = USD_IOCTL(ufd, USD_IOCTL_GETSIZE, &size);
145     USD_CLOSE(ufd);
146     if (code) {
147         return 0;
148     }
149     return 1;
150 }
151
152 /* returns 1 if <name> doesnot end in .readonly or .backup, else 0 */
153 static int
154 VolNameOK(char *name)
155 {
156     size_t total;
157
158
159     total = strlen(name);
160     if (!strcmp(&name[total - 9], ".readonly")) {
161         return 0;
162     } else if (!strcmp(&name[total - 7], ".backup")) {
163         return 0;
164     } else {
165         return 1;
166     }
167 }
168
169 /* return 1 if name is a number else 0 */
170 static int
171 IsNumeric(char *name)
172 {
173     int result, i;
174     size_t len;
175     char *ptr;
176
177     result = 1;
178     ptr = name;
179     len = strlen(name);
180     for (i = 0; i < len; i++) {
181         if (*ptr < '0' || *ptr > '9') {
182             result = 0;
183             break;
184         }
185         ptr++;
186
187     }
188     return result;
189 }
190
191
192 /*
193  * Parse a server dotted address and return the address in network byte order
194  */
195 afs_uint32
196 GetServerNoresolve(char *aname)
197 {
198     int b1, b2, b3, b4;
199     afs_uint32 addr;
200     afs_int32 code;
201
202     code = sscanf(aname, "%d.%d.%d.%d", &b1, &b2, &b3, &b4);
203     if (code == 4) {
204         addr = (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
205         addr = htonl(addr);     /* convert to network byte order */
206         return addr;
207     } else
208         return 0;
209 }
210 /*
211  * Parse a server name/address and return the address in network byte order
212  */
213 afs_uint32
214 GetServer(char *aname)
215 {
216     struct hostent *th;
217     afs_uint32 addr; /* in network byte order */
218     afs_int32 code;
219     char hostname[MAXHOSTCHARS];
220
221     if ((addr = GetServerNoresolve(aname)) == 0) {
222         th = gethostbyname(aname);
223         if (!th)
224             return 0;
225         memcpy(&addr, th->h_addr, sizeof(addr));
226     }
227
228     if (rx_IsLoopbackAddr(ntohl(addr))) {       /* local host */
229         code = gethostname(hostname, MAXHOSTCHARS);
230         if (code)
231             return 0;
232         th = gethostbyname(hostname);
233         if (!th)
234             return 0;
235         memcpy(&addr, th->h_addr, sizeof(addr));
236     }
237
238     return (addr);
239 }
240
241 afs_int32
242 GetVolumeType(char *aname)
243 {
244
245     if (!strcmp(aname, "ro"))
246         return (ROVOL);
247     else if (!strcmp(aname, "rw"))
248         return (RWVOL);
249     else if (!strcmp(aname, "bk"))
250         return (BACKVOL);
251     else
252         return (-1);
253 }
254
255 int
256 IsPartValid(afs_int32 partId, afs_uint32 server, afs_int32 *code)
257 {
258     struct partList dummyPartList;
259     int i, success, cnt;
260
261     success = 0;
262     *code = 0;
263
264     *code = UV_ListPartitions(server, &dummyPartList, &cnt);
265     if (*code)
266         return success;
267     for (i = 0; i < cnt; i++) {
268         if (dummyPartList.partFlags[i] & PARTVALID)
269             if (dummyPartList.partId[i] == partId)
270                 success = 1;
271     }
272     return success;
273 }
274
275
276
277  /*sends the contents of file associated with <fd> and <blksize>  to Rx Stream
278   * associated  with <call> */
279 int
280 SendFile(usd_handle_t ufd, struct rx_call *call, long blksize)
281 {
282     char *buffer = (char *)0;
283     afs_int32 error = 0;
284     afs_uint32 nbytes;
285
286     buffer = malloc(blksize);
287     if (!buffer) {
288         fprintf(STDERR, "malloc failed\n");
289         return -1;
290     }
291
292     while (!error) {
293 #ifndef AFS_NT40_ENV            /* NT csn't select on non-socket fd's */
294         fd_set in;
295         FD_ZERO(&in);
296         FD_SET((intptr_t)(ufd->handle), &in);
297         /* don't timeout if read blocks */
298 #if defined(AFS_PTHREAD_ENV)
299         select(((intptr_t)(ufd->handle)) + 1, &in, 0, 0, 0);
300 #else
301         IOMGR_Select(((intptr_t)(ufd->handle)) + 1, &in, 0, 0, 0);
302 #endif
303 #endif
304         error = USD_READ(ufd, buffer, blksize, &nbytes);
305         if (error) {
306             fprintf(STDERR, "File system read failed: %s\n",
307                     afs_error_message(error));
308             break;
309         }
310
311         if (nbytes == 0)
312             break;
313
314         if (rx_Write(call, buffer, nbytes) != nbytes) {
315             error = -1;
316             break;
317         }
318     }
319     if (buffer)
320         free(buffer);
321     return error;
322 }
323
324 /* function invoked by UV_RestoreVolume, reads the data from rx_trx_stream and
325  * writes it out to the volume. */
326 afs_int32
327 WriteData(struct rx_call *call, void *rock)
328 {
329     char *filename = (char *) rock;
330     usd_handle_t ufd;
331     long blksize;
332     afs_int32 error, code;
333     int ufdIsOpen = 0;
334     afs_int64 currOffset;
335     afs_uint32 buffer;
336     afs_uint32 got;
337
338     error = 0;
339
340     if (!filename || !*filename) {
341         usd_StandardInput(&ufd);
342         blksize = 4096;
343         ufdIsOpen = 1;
344     } else {
345         code = usd_Open(filename, USD_OPEN_RDONLY, 0, &ufd);
346         if (code == 0) {
347             ufdIsOpen = 1;
348             code = USD_IOCTL(ufd, USD_IOCTL_GETBLKSIZE, &blksize);
349         }
350         if (code) {
351             fprintf(STDERR, "Could not access file '%s': %s\n", filename,
352                     afs_error_message(code));
353             error = VOLSERBADOP;
354             goto wfail;
355         }
356         /* test if we have a valid dump */
357         USD_SEEK(ufd, 0, SEEK_END, &currOffset);
358         USD_SEEK(ufd, currOffset - sizeof(afs_uint32), SEEK_SET, &currOffset);
359         USD_READ(ufd, (char *)&buffer, sizeof(afs_uint32), &got);
360         if ((got != sizeof(afs_uint32)) || (ntohl(buffer) != DUMPENDMAGIC)) {
361             fprintf(STDERR, "Signature missing from end of file '%s'\n", filename);
362             error = VOLSERBADOP;
363             goto wfail;
364         }
365         USD_SEEK(ufd, 0, SEEK_SET, &currOffset);
366     }
367     code = SendFile(ufd, call, blksize);
368     if (code) {
369         error = code;
370         goto wfail;
371     }
372   wfail:
373     if (ufdIsOpen) {
374         code = USD_CLOSE(ufd);
375         if (code) {
376             fprintf(STDERR, "Could not close dump file %s\n",
377                     (filename && *filename) ? filename : "STDOUT");
378             if (!error)
379                 error = code;
380         }
381     }
382     return error;
383 }
384
385 /* Receive data from <call> stream into file associated
386  * with <fd> <blksize>
387  */
388 int
389 ReceiveFile(usd_handle_t ufd, struct rx_call *call, long blksize)
390 {
391     char *buffer = NULL;
392     afs_int32 bytesread;
393     afs_uint32 bytesleft, w;
394     afs_int32 error = 0;
395
396     buffer = malloc(blksize);
397     if (!buffer) {
398         fprintf(STDERR, "memory allocation failed\n");
399         ERROR_EXIT(-1);
400     }
401
402     while ((bytesread = rx_Read(call, buffer, blksize)) > 0) {
403         for (bytesleft = bytesread; bytesleft; bytesleft -= w) {
404 #ifndef AFS_NT40_ENV            /* NT csn't select on non-socket fd's */
405             fd_set out;
406             FD_ZERO(&out);
407             FD_SET((intptr_t)(ufd->handle), &out);
408             /* don't timeout if write blocks */
409 #if defined(AFS_PTHREAD_ENV)
410             select(((intptr_t)(ufd->handle)) + 1, &out, 0, 0, 0);
411 #else
412             IOMGR_Select(((intptr_t)(ufd->handle)) + 1, 0, &out, 0, 0);
413 #endif
414 #endif
415             error =
416                 USD_WRITE(ufd, &buffer[bytesread - bytesleft], bytesleft, &w);
417             if (error) {
418                 fprintf(STDERR, "File system write failed: %s\n",
419                         afs_error_message(error));
420                 ERROR_EXIT(-1);
421             }
422         }
423     }
424
425   error_exit:
426     if (buffer)
427         free(buffer);
428     return (error);
429 }
430
431 afs_int32
432 DumpFunction(struct rx_call *call, void *rock)
433 {
434     char *filename = (char *)rock;
435     usd_handle_t ufd;           /* default is to stdout */
436     afs_int32 error = 0, code;
437     afs_int64 size;
438     long blksize;
439     int ufdIsOpen = 0;
440
441     /* Open the output file */
442     if (!filename || !*filename) {
443         usd_StandardOutput(&ufd);
444         blksize = 4096;
445         ufdIsOpen = 1;
446     } else {
447         code =
448             usd_Open(filename, USD_OPEN_CREATE | USD_OPEN_RDWR, 0666, &ufd);
449         if (code == 0) {
450             ufdIsOpen = 1;
451             size = 0;
452             code = USD_IOCTL(ufd, USD_IOCTL_SETSIZE, &size);
453         }
454         if (code == 0) {
455             code = USD_IOCTL(ufd, USD_IOCTL_GETBLKSIZE, &blksize);
456         }
457         if (code) {
458             fprintf(STDERR, "Could not create file '%s': %s\n", filename,
459                     afs_error_message(code));
460             ERROR_EXIT(VOLSERBADOP);
461         }
462     }
463
464     code = ReceiveFile(ufd, call, blksize);
465     if (code)
466         ERROR_EXIT(code);
467
468   error_exit:
469     /* Close the output file */
470     if (ufdIsOpen) {
471         code = USD_CLOSE(ufd);
472         if (code) {
473             fprintf(STDERR, "Could not close dump file %s\n",
474                     (filename && *filename) ? filename : "STDIN");
475             if (!error)
476                 error = code;
477         }
478     }
479
480     return (error);
481 }
482
483 static void
484 DisplayFormat(volintInfo *pntr, afs_uint32 server, afs_int32 part,
485               int *totalOK, int *totalNotOK, int *totalBusy, int fast,
486               int longlist, int disp)
487 {
488     char pname[10];
489     time_t t;
490
491     if (fast) {
492         fprintf(STDOUT, "%-10lu\n", (unsigned long)pntr->volid);
493     } else if (longlist) {
494         if (pntr->status == VOK) {
495             fprintf(STDOUT, "%-32s ", pntr->name);
496             fprintf(STDOUT, "%10lu ", (unsigned long)pntr->volid);
497             if (pntr->type == 0)
498                 fprintf(STDOUT, "RW ");
499             if (pntr->type == 1)
500                 fprintf(STDOUT, "RO ");
501             if (pntr->type == 2)
502                 fprintf(STDOUT, "BK ");
503             fprintf(STDOUT, "%10d K  ", pntr->size);
504             if (pntr->inUse == 1) {
505                 fprintf(STDOUT, "On-line");
506                 *totalOK += 1;
507             } else {
508                 fprintf(STDOUT, "Off-line");
509                 *totalNotOK += 1;
510             }
511             if (pntr->needsSalvaged == 1)
512                 fprintf(STDOUT, "**needs salvage**");
513             fprintf(STDOUT, "\n");
514             MapPartIdIntoName(part, pname);
515             fprintf(STDOUT, "    %s %s \n", hostutil_GetNameByINet(server),
516                     pname);
517             fprintf(STDOUT, "    RWrite %10lu ROnly %10lu Backup %10lu \n",
518                     (unsigned long)pntr->parentID,
519                     (unsigned long)pntr->cloneID,
520                     (unsigned long)pntr->backupID);
521             fprintf(STDOUT, "    MaxQuota %10d K \n", pntr->maxquota);
522             t = pntr->creationDate;
523             fprintf(STDOUT, "    Creation    %s",
524                     ctime(&t));
525             t = pntr->copyDate;
526             fprintf(STDOUT, "    Copy        %s",
527                     ctime(&t));
528
529             t = pntr->backupDate;
530             if (!t)
531                 fprintf(STDOUT, "    Backup      Never\n");
532             else
533                 fprintf(STDOUT, "    Backup      %s",
534                         ctime(&t));
535
536             t = pntr->accessDate;
537             if (t)
538                 fprintf(STDOUT, "    Last Access %s",
539                         ctime(&t));
540
541             t = pntr->updateDate;
542             if (!t)
543                 fprintf(STDOUT, "    Last Update Never\n");
544             else
545                 fprintf(STDOUT, "    Last Update %s",
546                         ctime(&t));
547             fprintf(STDOUT,
548                     "    %d accesses in the past day (i.e., vnode references)\n",
549                     pntr->dayUse);
550         } else if (pntr->status == VBUSY) {
551             *totalBusy += 1;
552             qPut(&busyHead, pntr->volid);
553             if (disp)
554                 fprintf(STDOUT, "**** Volume %lu is busy ****\n",
555                         (unsigned long)pntr->volid);
556         } else {
557             *totalNotOK += 1;
558             qPut(&notokHead, pntr->volid);
559             if (disp)
560                 fprintf(STDOUT, "**** Could not attach volume %lu ****\n",
561                         (unsigned long)pntr->volid);
562         }
563         fprintf(STDOUT, "\n");
564     } else {                    /* default listing */
565         if (pntr->status == VOK) {
566             fprintf(STDOUT, "%-32s ", pntr->name);
567             fprintf(STDOUT, "%10lu ", (unsigned long)pntr->volid);
568             if (pntr->type == 0)
569                 fprintf(STDOUT, "RW ");
570             if (pntr->type == 1)
571                 fprintf(STDOUT, "RO ");
572             if (pntr->type == 2)
573                 fprintf(STDOUT, "BK ");
574             fprintf(STDOUT, "%10d K ", pntr->size);
575             if (pntr->inUse == 1) {
576                 fprintf(STDOUT, "On-line");
577                 *totalOK += 1;
578             } else {
579                 fprintf(STDOUT, "Off-line");
580                 *totalNotOK += 1;
581             }
582             if (pntr->needsSalvaged == 1)
583                 fprintf(STDOUT, "**needs salvage**");
584             fprintf(STDOUT, "\n");
585         } else if (pntr->status == VBUSY) {
586             *totalBusy += 1;
587             qPut(&busyHead, pntr->volid);
588             if (disp)
589                 fprintf(STDOUT, "**** Volume %lu is busy ****\n",
590                         (unsigned long)pntr->volid);
591         } else {
592             *totalNotOK += 1;
593             qPut(&notokHead, pntr->volid);
594             if (disp)
595                 fprintf(STDOUT, "**** Could not attach volume %lu ****\n",
596                         (unsigned long)pntr->volid);
597         }
598     }
599 }
600
601 /*------------------------------------------------------------------------
602  * PRIVATE XDisplayFormat
603  *
604  * Description:
605  *      Display the contents of one extended volume info structure.
606  *
607  * Arguments:
608  *      a_xInfoP        : Ptr to extended volume info struct to print.
609  *      a_servID        : Server ID to print.
610  *      a_partID        : Partition ID to print.
611  *      a_totalOKP      : Ptr to total-OK counter.
612  *      a_totalNotOKP   : Ptr to total-screwed counter.
613  *      a_totalBusyP    : Ptr to total-busy counter.
614  *      a_fast          : Fast listing?
615  *      a_int32         : Int32 listing?
616  *      a_showProblems  : Show volume problems?
617  *
618  * Returns:
619  *      Nothing.
620  *
621  * Environment:
622  *      Nothing interesting.
623  *
624  * Side Effects:
625  *      As advertised.
626  *------------------------------------------------------------------------*/
627
628 static void
629 XDisplayFormat(volintXInfo *a_xInfoP, afs_uint32 a_servID, afs_int32 a_partID,
630                int *a_totalOKP, int *a_totalNotOKP, int *a_totalBusyP,
631                int a_fast, int a_int32, int a_showProblems)
632 {                               /*XDisplayFormat */
633     time_t t;
634     char pname[10];
635
636     if (a_fast) {
637         /*
638          * Short & sweet.
639          */
640         fprintf(STDOUT, "%-10lu\n", (unsigned long)a_xInfoP->volid);
641     } else if (a_int32) {
642         /*
643          * Fully-detailed listing.
644          */
645         if (a_xInfoP->status == VOK) {
646             /*
647              * Volume's status is OK - all the fields are valid.
648              */
649             fprintf(STDOUT, "%-32s ", a_xInfoP->name);
650             fprintf(STDOUT, "%10lu ", (unsigned long)a_xInfoP->volid);
651             if (a_xInfoP->type == 0)
652                 fprintf(STDOUT, "RW ");
653             if (a_xInfoP->type == 1)
654                 fprintf(STDOUT, "RO ");
655             if (a_xInfoP->type == 2)
656                 fprintf(STDOUT, "BK ");
657             fprintf(STDOUT, "%10d K used ", a_xInfoP->size);
658             fprintf(STDOUT, "%d files ", a_xInfoP->filecount);
659             if (a_xInfoP->inUse == 1) {
660                 fprintf(STDOUT, "On-line");
661                 (*a_totalOKP)++;
662             } else {
663                 fprintf(STDOUT, "Off-line");
664                 (*a_totalNotOKP)++;
665             }
666             fprintf(STDOUT, "\n");
667             MapPartIdIntoName(a_partID, pname);
668             fprintf(STDOUT, "    %s %s \n", hostutil_GetNameByINet(a_servID),
669                     pname);
670             fprintf(STDOUT, "    RWrite %10lu ROnly %10lu Backup %10lu \n",
671                     (unsigned long)a_xInfoP->parentID,
672                     (unsigned long)a_xInfoP->cloneID,
673                     (unsigned long)a_xInfoP->backupID);
674             fprintf(STDOUT, "    MaxQuota %10d K \n", a_xInfoP->maxquota);
675
676             t = a_xInfoP->creationDate;
677             fprintf(STDOUT, "    Creation    %s",
678                     ctime(&t));
679
680             t = a_xInfoP->copyDate;
681             fprintf(STDOUT, "    Copy        %s",
682                     ctime(&t));
683
684             t = a_xInfoP->backupDate;
685             if (!t)
686                 fprintf(STDOUT, "    Backup      Never\n");
687             else
688                 fprintf(STDOUT, "    Backup      %s",
689                         ctime(&t));
690
691             t = a_xInfoP->accessDate;
692             if (t)
693                 fprintf(STDOUT, "    Last Access %s",
694                         ctime(&t));
695
696             t = a_xInfoP->updateDate;
697             if (!t)
698                 fprintf(STDOUT, "    Last Update Never\n");
699             else
700                 fprintf(STDOUT, "    Last Update %s",
701                         ctime(&t));
702             fprintf(STDOUT,
703                     "    %d accesses in the past day (i.e., vnode references)\n",
704                     a_xInfoP->dayUse);
705
706             /*
707              * Print all the read/write and authorship stats.
708              */
709             fprintf(STDOUT, "\n                      Raw Read/Write Stats\n");
710             fprintf(STDOUT,
711                     "          |-------------------------------------------|\n");
712             fprintf(STDOUT,
713                     "          |    Same Network     |    Diff Network     |\n");
714             fprintf(STDOUT,
715                     "          |----------|----------|----------|----------|\n");
716             fprintf(STDOUT,
717                     "          |  Total   |   Auth   |   Total  |   Auth   |\n");
718             fprintf(STDOUT,
719                     "          |----------|----------|----------|----------|\n");
720             fprintf(STDOUT, "Reads     | %8d | %8d | %8d | %8d |\n",
721                     a_xInfoP->stat_reads[VOLINT_STATS_SAME_NET],
722                     a_xInfoP->stat_reads[VOLINT_STATS_SAME_NET_AUTH],
723                     a_xInfoP->stat_reads[VOLINT_STATS_DIFF_NET],
724                     a_xInfoP->stat_reads[VOLINT_STATS_DIFF_NET_AUTH]);
725             fprintf(STDOUT, "Writes    | %8d | %8d | %8d | %8d |\n",
726                     a_xInfoP->stat_writes[VOLINT_STATS_SAME_NET],
727                     a_xInfoP->stat_writes[VOLINT_STATS_SAME_NET_AUTH],
728                     a_xInfoP->stat_writes[VOLINT_STATS_DIFF_NET],
729                     a_xInfoP->stat_writes[VOLINT_STATS_DIFF_NET_AUTH]);
730             fprintf(STDOUT,
731                     "          |-------------------------------------------|\n\n");
732
733             fprintf(STDOUT,
734                     "                   Writes Affecting Authorship\n");
735             fprintf(STDOUT,
736                     "          |-------------------------------------------|\n");
737             fprintf(STDOUT,
738                     "          |   File Authorship   | Directory Authorship|\n");
739             fprintf(STDOUT,
740                     "          |----------|----------|----------|----------|\n");
741             fprintf(STDOUT,
742                     "          |   Same   |   Diff   |    Same  |   Diff   |\n");
743             fprintf(STDOUT,
744                     "          |----------|----------|----------|----------|\n");
745             fprintf(STDOUT, "0-60 sec  | %8d | %8d | %8d | %8d |\n",
746                     a_xInfoP->stat_fileSameAuthor[VOLINT_STATS_TIME_IDX_0],
747                     a_xInfoP->stat_fileDiffAuthor[VOLINT_STATS_TIME_IDX_0],
748                     a_xInfoP->stat_dirSameAuthor[VOLINT_STATS_TIME_IDX_0],
749                     a_xInfoP->stat_dirDiffAuthor[VOLINT_STATS_TIME_IDX_0]);
750             fprintf(STDOUT, "1-10 min  | %8d | %8d | %8d | %8d |\n",
751                     a_xInfoP->stat_fileSameAuthor[VOLINT_STATS_TIME_IDX_1],
752                     a_xInfoP->stat_fileDiffAuthor[VOLINT_STATS_TIME_IDX_1],
753                     a_xInfoP->stat_dirSameAuthor[VOLINT_STATS_TIME_IDX_1],
754                     a_xInfoP->stat_dirDiffAuthor[VOLINT_STATS_TIME_IDX_1]);
755             fprintf(STDOUT, "10min-1hr | %8d | %8d | %8d | %8d |\n",
756                     a_xInfoP->stat_fileSameAuthor[VOLINT_STATS_TIME_IDX_2],
757                     a_xInfoP->stat_fileDiffAuthor[VOLINT_STATS_TIME_IDX_2],
758                     a_xInfoP->stat_dirSameAuthor[VOLINT_STATS_TIME_IDX_2],
759                     a_xInfoP->stat_dirDiffAuthor[VOLINT_STATS_TIME_IDX_2]);
760             fprintf(STDOUT, "1hr-1day  | %8d | %8d | %8d | %8d |\n",
761                     a_xInfoP->stat_fileSameAuthor[VOLINT_STATS_TIME_IDX_3],
762                     a_xInfoP->stat_fileDiffAuthor[VOLINT_STATS_TIME_IDX_3],
763                     a_xInfoP->stat_dirSameAuthor[VOLINT_STATS_TIME_IDX_3],
764                     a_xInfoP->stat_dirDiffAuthor[VOLINT_STATS_TIME_IDX_3]);
765             fprintf(STDOUT, "1day-1wk  | %8d | %8d | %8d | %8d |\n",
766                     a_xInfoP->stat_fileSameAuthor[VOLINT_STATS_TIME_IDX_4],
767                     a_xInfoP->stat_fileDiffAuthor[VOLINT_STATS_TIME_IDX_4],
768                     a_xInfoP->stat_dirSameAuthor[VOLINT_STATS_TIME_IDX_4],
769                     a_xInfoP->stat_dirDiffAuthor[VOLINT_STATS_TIME_IDX_4]);
770             fprintf(STDOUT, "> 1wk     | %8d | %8d | %8d | %8d |\n",
771                     a_xInfoP->stat_fileSameAuthor[VOLINT_STATS_TIME_IDX_5],
772                     a_xInfoP->stat_fileDiffAuthor[VOLINT_STATS_TIME_IDX_5],
773                     a_xInfoP->stat_dirSameAuthor[VOLINT_STATS_TIME_IDX_5],
774                     a_xInfoP->stat_dirDiffAuthor[VOLINT_STATS_TIME_IDX_5]);
775             fprintf(STDOUT,
776                     "          |-------------------------------------------|\n");
777         } /*Volume status OK */
778         else if (a_xInfoP->status == VBUSY) {
779             (*a_totalBusyP)++;
780             qPut(&busyHead, a_xInfoP->volid);
781             if (a_showProblems)
782                 fprintf(STDOUT, "**** Volume %lu is busy ****\n",
783                         (unsigned long)a_xInfoP->volid);
784         } /*Busy volume */
785         else {
786             (*a_totalNotOKP)++;
787             qPut(&notokHead, a_xInfoP->volid);
788             if (a_showProblems)
789                 fprintf(STDOUT, "**** Could not attach volume %lu ****\n",
790                         (unsigned long)a_xInfoP->volid);
791         }                       /*Screwed volume */
792         fprintf(STDOUT, "\n");
793     } /*Long listing */
794     else {
795         /*
796          * Default listing.
797          */
798         if (a_xInfoP->status == VOK) {
799             fprintf(STDOUT, "%-32s ", a_xInfoP->name);
800             fprintf(STDOUT, "%10lu ", (unsigned long)a_xInfoP->volid);
801             if (a_xInfoP->type == 0)
802                 fprintf(STDOUT, "RW ");
803             if (a_xInfoP->type == 1)
804                 fprintf(STDOUT, "RO ");
805             if (a_xInfoP->type == 2)
806                 fprintf(STDOUT, "BK ");
807             fprintf(STDOUT, "%10d K ", a_xInfoP->size);
808             if (a_xInfoP->inUse == 1) {
809                 fprintf(STDOUT, "On-line");
810                 (*a_totalOKP)++;
811             } else {
812                 fprintf(STDOUT, "Off-line");
813                 (*a_totalNotOKP)++;
814             }
815             fprintf(STDOUT, "\n");
816         } /*Volume OK */
817         else if (a_xInfoP->status == VBUSY) {
818             (*a_totalBusyP)++;
819             qPut(&busyHead, a_xInfoP->volid);
820             if (a_showProblems)
821                 fprintf(STDOUT, "**** Volume %lu is busy ****\n",
822                         (unsigned long)a_xInfoP->volid);
823         } /*Busy volume */
824         else {
825             (*a_totalNotOKP)++;
826             qPut(&notokHead, a_xInfoP->volid);
827             if (a_showProblems)
828                 fprintf(STDOUT, "**** Could not attach volume %lu ****\n",
829                         (unsigned long)a_xInfoP->volid);
830         }                       /*Screwed volume */
831     }                           /*Default listing */
832 }                               /*XDisplayFormat */
833
834 /*------------------------------------------------------------------------
835  * PRIVATE XDisplayFormat2
836  *
837  * Description:
838  *      Display the formated contents of one extended volume info structure.
839  *
840  * Arguments:
841  *      a_xInfoP        : Ptr to extended volume info struct to print.
842  *      a_servID        : Server ID to print.
843  *      a_partID        : Partition ID to print.
844  *      a_totalOKP      : Ptr to total-OK counter.
845  *      a_totalNotOKP   : Ptr to total-screwed counter.
846  *      a_totalBusyP    : Ptr to total-busy counter.
847  *      a_fast          : Fast listing?
848  *      a_int32         : Int32 listing?
849  *      a_showProblems  : Show volume problems?
850  *
851  * Returns:
852  *      Nothing.
853  *
854  * Environment:
855  *      Nothing interesting.
856  *
857  * Side Effects:
858  *      As advertised.
859  *------------------------------------------------------------------------*/
860
861 static void
862 XDisplayFormat2(volintXInfo *a_xInfoP, afs_uint32 a_servID, afs_int32 a_partID,
863                 int *a_totalOKP, int *a_totalNotOKP, int *a_totalBusyP,
864                 int a_fast, int a_int32, int a_showProblems)
865 {                               /*XDisplayFormat */
866     time_t t;
867     if (a_fast) {
868         /*
869          * Short & sweet.
870          */
871         fprintf(STDOUT, "vold_id\t%-10lu\n", (unsigned long)a_xInfoP->volid);
872     } else if (a_int32) {
873         /*
874          * Fully-detailed listing.
875          */
876         if (a_xInfoP->status == VOK) {
877             /*
878              * Volume's status is OK - all the fields are valid.
879              */
880
881                 static long server_cache = -1, partition_cache = -1;
882                 static char hostname[256], address[32], pname[16];
883                 int i,ai[] = {VOLINT_STATS_TIME_IDX_0,VOLINT_STATS_TIME_IDX_1,VOLINT_STATS_TIME_IDX_2,
884                               VOLINT_STATS_TIME_IDX_3,VOLINT_STATS_TIME_IDX_4,VOLINT_STATS_TIME_IDX_5};
885
886                 if (a_servID != server_cache) {
887                         struct in_addr s;
888
889                         s.s_addr = a_servID;
890                         strcpy(hostname, hostutil_GetNameByINet(a_servID));
891                         strcpy(address, inet_ntoa(s));
892                         server_cache = a_servID;
893                 }
894                 if (a_partID != partition_cache) {
895                         MapPartIdIntoName(a_partID, pname);
896                         partition_cache = a_partID;
897                 }
898
899                 fprintf(STDOUT, "name\t\t%s\n", a_xInfoP->name);
900                 fprintf(STDOUT, "id\t\t%lu\n", afs_printable_uint32_lu(a_xInfoP->volid));
901                 fprintf(STDOUT, "serv\t\t%s\t%s\n", address, hostname);
902                 fprintf(STDOUT, "part\t\t%s\n", pname);
903                 fprintf(STDOUT, "status\t\tOK\n");
904                 fprintf(STDOUT, "backupID\t%lu\n",
905                         afs_printable_uint32_lu(a_xInfoP->backupID));
906                 fprintf(STDOUT, "parentID\t%lu\n",
907                         afs_printable_uint32_lu(a_xInfoP->parentID));
908                 fprintf(STDOUT, "cloneID\t\t%lu\n",
909                         afs_printable_uint32_lu(a_xInfoP->cloneID));
910                 fprintf(STDOUT, "inUse\t\t%s\n", a_xInfoP->inUse ? "Y" : "N");
911                 switch (a_xInfoP->type) {
912                 case 0:
913                         fprintf(STDOUT, "type\t\tRW\n");
914                         break;
915                 case 1:
916                         fprintf(STDOUT, "type\t\tRO\n");
917                         break;
918                 case 2:
919                         fprintf(STDOUT, "type\t\tBK\n");
920                         break;
921                 default:
922                         fprintf(STDOUT, "type\t\t?\n");
923                         break;
924                 }
925                 t = a_xInfoP->creationDate;
926                 fprintf(STDOUT, "creationDate\t%-9lu\t%s",
927                         afs_printable_uint32_lu(a_xInfoP->creationDate),
928                         ctime(&t));
929
930                 t = a_xInfoP->accessDate;
931                 fprintf(STDOUT, "accessDate\t%-9lu\t%s",
932                         afs_printable_uint32_lu(a_xInfoP->accessDate),
933                         ctime(&t));
934
935                 t = a_xInfoP->updateDate;
936                 fprintf(STDOUT, "updateDate\t%-9lu\t%s",
937                         afs_printable_uint32_lu(a_xInfoP->updateDate),
938                         ctime(&t));
939
940                 t = a_xInfoP->backupDate;
941                 fprintf(STDOUT, "backupDate\t%-9lu\t%s",
942                         afs_printable_uint32_lu(a_xInfoP->backupDate),
943                         ctime(&t));
944
945                 t = a_xInfoP->copyDate;
946                 fprintf(STDOUT, "copyDate\t%-9lu\t%s",
947                         afs_printable_uint32_lu(a_xInfoP->copyDate),
948                         ctime(&t));
949
950                 fprintf(STDOUT, "diskused\t%u\n", a_xInfoP->size);
951                 fprintf(STDOUT, "maxquota\t%u\n", a_xInfoP->maxquota);
952
953                 fprintf(STDOUT, "filecount\t%u\n", a_xInfoP->filecount);
954                 fprintf(STDOUT, "dayUse\t\t%u\n", a_xInfoP->dayUse);
955
956
957
958                 fprintf(STDOUT,"reads_same_net\t%8d\n",a_xInfoP->stat_reads[VOLINT_STATS_SAME_NET]);
959                 fprintf(STDOUT,"reads_same_net_auth\t%8d\n",a_xInfoP->stat_reads[VOLINT_STATS_SAME_NET_AUTH]);
960                 fprintf(STDOUT,"reads_diff_net\t%8d\n",a_xInfoP->stat_reads[VOLINT_STATS_DIFF_NET]);
961                 fprintf(STDOUT,"reads_diff_net_auth\t%8d\n",a_xInfoP->stat_reads[VOLINT_STATS_DIFF_NET_AUTH]);
962
963                 fprintf(STDOUT,"writes_same_net\t%8d\n",a_xInfoP->stat_writes[VOLINT_STATS_SAME_NET]);
964                 fprintf(STDOUT,"writes_same_net_auth\t%8d\n",a_xInfoP->stat_writes[VOLINT_STATS_SAME_NET_AUTH]);
965                 fprintf(STDOUT,"writes_diff_net\t%8d\n",a_xInfoP->stat_writes[VOLINT_STATS_DIFF_NET]);
966                 fprintf(STDOUT,"writes_diff_net_auth\t%8d\n",a_xInfoP->stat_writes[VOLINT_STATS_DIFF_NET_AUTH]);
967
968                 for(i=0;i<5;i++)
969                 {
970                         fprintf(STDOUT,"file_same_author_idx_%d\t%8d\n",i+1,a_xInfoP->stat_fileSameAuthor[ai[i]]);
971                         fprintf(STDOUT,"file_diff_author_idx_%d\t%8d\n",i+1,a_xInfoP->stat_fileDiffAuthor[ai[i]]);
972                         fprintf(STDOUT,"dir_same_author_idx_%d\t%8d\n",i+1,a_xInfoP->stat_dirSameAuthor[ai[i]]);
973                         fprintf(STDOUT,"dir_dif_author_idx_%d\t%8d\n",i+1,a_xInfoP->stat_dirDiffAuthor[ai[i]]);
974                 }
975
976         } /*Volume status OK */
977         else if (a_xInfoP->status == VBUSY) {
978             (*a_totalBusyP)++;
979             qPut(&busyHead, a_xInfoP->volid);
980             if (a_showProblems)
981                 fprintf(STDOUT, "BUSY_VOL\t%lu\n",
982                         (unsigned long)a_xInfoP->volid);
983         } /*Busy volume */
984         else {
985             (*a_totalNotOKP)++;
986             qPut(&notokHead, a_xInfoP->volid);
987             if (a_showProblems)
988                 fprintf(STDOUT, "COULD_NOT_ATTACH\t%lu\n",
989                         (unsigned long)a_xInfoP->volid);
990         }                       /*Screwed volume */
991     } /*Long listing */
992     else {
993         /*
994          * Default listing.
995          */
996         if (a_xInfoP->status == VOK) {
997             fprintf(STDOUT, "name\t%-32s\n", a_xInfoP->name);
998             fprintf(STDOUT, "volID\t%10lu\n", (unsigned long)a_xInfoP->volid);
999             if (a_xInfoP->type == 0)
1000                 fprintf(STDOUT, "type\tRW\n");
1001             if (a_xInfoP->type == 1)
1002                 fprintf(STDOUT, "type\tRO\n");
1003             if (a_xInfoP->type == 2)
1004                 fprintf(STDOUT, "type\tBK\n");
1005             fprintf(STDOUT, "size\t%10dK\n", a_xInfoP->size);
1006
1007             fprintf(STDOUT, "inUse\t%d\n",a_xInfoP->inUse);
1008             if (a_xInfoP->inUse == 1)
1009                 (*a_totalOKP)++;
1010             else
1011                 (*a_totalNotOKP)++;
1012
1013         } /*Volume OK */
1014         else if (a_xInfoP->status == VBUSY) {
1015             (*a_totalBusyP)++;
1016             qPut(&busyHead, a_xInfoP->volid);
1017             if (a_showProblems)
1018                 fprintf(STDOUT, "VOLUME_BUSY\t%lu\n",
1019                         (unsigned long)a_xInfoP->volid);
1020         } /*Busy volume */
1021         else {
1022             (*a_totalNotOKP)++;
1023             qPut(&notokHead, a_xInfoP->volid);
1024             if (a_showProblems)
1025                 fprintf(STDOUT, "COULD_NOT_ATTACH_VOLUME\t%lu\n",
1026                         (unsigned long)a_xInfoP->volid);
1027         }                       /*Screwed volume */
1028     }                           /*Default listing */
1029 }                               /*XDisplayFormat */
1030
1031 static void
1032 DisplayFormat2(long server, long partition, volintInfo *pntr)
1033 {
1034     static long server_cache = -1, partition_cache = -1;
1035     static char hostname[256], address[32], pname[16];
1036     time_t t;
1037
1038     if (server != server_cache) {
1039         struct in_addr s;
1040
1041         s.s_addr = server;
1042         strcpy(hostname, hostutil_GetNameByINet(server));
1043         strcpy(address, inet_ntoa(s));
1044         server_cache = server;
1045     }
1046     if (partition != partition_cache) {
1047         MapPartIdIntoName(partition, pname);
1048         partition_cache = partition;
1049     }
1050
1051     if (pntr->status == VOK)
1052         fprintf(STDOUT, "name\t\t%s\n", pntr->name);
1053
1054     fprintf(STDOUT, "id\t\t%lu\n",
1055             afs_printable_uint32_lu(pntr->volid));
1056     fprintf(STDOUT, "serv\t\t%s\t%s\n", address, hostname);
1057     fprintf(STDOUT, "part\t\t%s\n", pname);
1058     switch (pntr->status) {
1059     case VOK:
1060         fprintf(STDOUT, "status\t\tOK\n");
1061         break;
1062     case VBUSY:
1063         fprintf(STDOUT, "status\t\tBUSY\n");
1064         return;
1065     default:
1066         fprintf(STDOUT, "status\t\tUNATTACHABLE\n");
1067         return;
1068     }
1069     fprintf(STDOUT, "backupID\t%lu\n",
1070             afs_printable_uint32_lu(pntr->backupID));
1071     fprintf(STDOUT, "parentID\t%lu\n",
1072             afs_printable_uint32_lu(pntr->parentID));
1073     fprintf(STDOUT, "cloneID\t\t%lu\n",
1074             afs_printable_uint32_lu(pntr->cloneID));
1075     fprintf(STDOUT, "inUse\t\t%s\n", pntr->inUse ? "Y" : "N");
1076     fprintf(STDOUT, "needsSalvaged\t%s\n", pntr->needsSalvaged ? "Y" : "N");
1077     /* 0xD3 is from afs/volume.h since I had trouble including the file */
1078     fprintf(STDOUT, "destroyMe\t%s\n", pntr->destroyMe == 0xD3 ? "Y" : "N");
1079     switch (pntr->type) {
1080     case 0:
1081         fprintf(STDOUT, "type\t\tRW\n");
1082         break;
1083     case 1:
1084         fprintf(STDOUT, "type\t\tRO\n");
1085         break;
1086     case 2:
1087         fprintf(STDOUT, "type\t\tBK\n");
1088         break;
1089     default:
1090         fprintf(STDOUT, "type\t\t?\n");
1091         break;
1092     }
1093     t = pntr->creationDate;
1094     fprintf(STDOUT, "creationDate\t%-9lu\t%s",
1095             afs_printable_uint32_lu(pntr->creationDate),
1096             ctime(&t));
1097
1098     t = pntr->accessDate;
1099     fprintf(STDOUT, "accessDate\t%-9lu\t%s",
1100             afs_printable_uint32_lu(pntr->accessDate),
1101             ctime(&t));
1102
1103     t = pntr->updateDate;
1104     fprintf(STDOUT, "updateDate\t%-9lu\t%s",
1105             afs_printable_uint32_lu(pntr->updateDate),
1106             ctime(&t));
1107
1108     t = pntr->backupDate;
1109     fprintf(STDOUT, "backupDate\t%-9lu\t%s",
1110             afs_printable_uint32_lu(pntr->backupDate),
1111             ctime(&t));
1112
1113     t = pntr->copyDate;
1114     fprintf(STDOUT, "copyDate\t%-9lu\t%s",
1115             afs_printable_uint32_lu(pntr->copyDate),
1116             ctime(&t));
1117
1118     fprintf(STDOUT, "flags\t\t%#lx\t(Optional)\n",
1119             afs_printable_uint32_lu(pntr->flags));
1120     fprintf(STDOUT, "diskused\t%u\n", pntr->size);
1121     fprintf(STDOUT, "maxquota\t%u\n", pntr->maxquota);
1122     fprintf(STDOUT, "minquota\t%lu\t(Optional)\n",
1123             afs_printable_uint32_lu(pntr->spare0));
1124     fprintf(STDOUT, "filecount\t%u\n", pntr->filecount);
1125     fprintf(STDOUT, "dayUse\t\t%u\n", pntr->dayUse);
1126     fprintf(STDOUT, "weekUse\t\t%lu\t(Optional)\n",
1127             afs_printable_uint32_lu(pntr->spare1));
1128     fprintf(STDOUT, "spare2\t\t%lu\t(Optional)\n",
1129             afs_printable_uint32_lu(pntr->spare2));
1130     fprintf(STDOUT, "spare3\t\t%lu\t(Optional)\n",
1131             afs_printable_uint32_lu(pntr->spare3));
1132     return;
1133 }
1134
1135 static void
1136 DisplayVolumes2(long server, long partition, volintInfo *pntr, long count)
1137 {
1138     long i;
1139
1140     for (i = 0; i < count; i++) {
1141         fprintf(STDOUT, "BEGIN_OF_ENTRY\n");
1142         DisplayFormat2(server, partition, pntr);
1143         fprintf(STDOUT, "END_OF_ENTRY\n\n");
1144         pntr++;
1145     }
1146     return;
1147 }
1148
1149 static void
1150 DisplayVolumes(afs_uint32 server, afs_int32 part, volintInfo *pntr,
1151                afs_int32 count, afs_int32 longlist, afs_int32 fast,
1152                int quiet)
1153 {
1154     int totalOK, totalNotOK, totalBusy, i;
1155     afs_uint32 volid = 0;
1156
1157     totalOK = 0;
1158     totalNotOK = 0;
1159     totalBusy = 0;
1160     qInit(&busyHead);
1161     qInit(&notokHead);
1162     for (i = 0; i < count; i++) {
1163         DisplayFormat(pntr, server, part, &totalOK, &totalNotOK, &totalBusy,
1164                       fast, longlist, 0);
1165         pntr++;
1166     }
1167     if (totalBusy) {
1168         while (busyHead.count) {
1169             qGet(&busyHead, &volid);
1170             fprintf(STDOUT, "**** Volume %lu is busy ****\n",
1171                     (unsigned long)volid);
1172         }
1173     }
1174     if (totalNotOK) {
1175         while (notokHead.count) {
1176             qGet(&notokHead, &volid);
1177             fprintf(STDOUT, "**** Could not attach volume %lu ****\n",
1178                     (unsigned long)volid);
1179         }
1180     }
1181     if (!quiet) {
1182         fprintf(STDOUT, "\n");
1183         if (!fast) {
1184             fprintf(STDOUT,
1185                     "Total volumes onLine %d ; Total volumes offLine %d ; Total busy %d\n\n",
1186                     totalOK, totalNotOK, totalBusy);
1187         }
1188     }
1189 }
1190 /*------------------------------------------------------------------------
1191  * PRIVATE XDisplayVolumes
1192  *
1193  * Description:
1194  *      Display extended volume information.
1195  *
1196  * Arguments:
1197  *      a_servID : Pointer to the Rx call we're performing.
1198  *      a_partID : Partition for which we want the extended list.
1199  *      a_xInfoP : Ptr to extended volume info.
1200  *      a_count  : Number of volume records contained above.
1201  *      a_int32   : Int32 listing generated?
1202  *      a_fast   : Fast listing generated?
1203  *      a_quiet  : Quiet listing generated?
1204  *
1205  * Returns:
1206  *      Nothing.
1207  *
1208  * Environment:
1209  *      Nothing interesting.
1210  *
1211  * Side Effects:
1212  *      As advertised.
1213  *------------------------------------------------------------------------*/
1214
1215 static void
1216 XDisplayVolumes(afs_uint32 a_servID, afs_int32 a_partID, volintXInfo *a_xInfoP,
1217                 afs_int32 a_count, afs_int32 a_int32, afs_int32 a_fast,
1218                 int a_quiet)
1219 {                               /*XDisplayVolumes */
1220
1221     int totalOK;                /*Total OK volumes */
1222     int totalNotOK;             /*Total screwed volumes */
1223     int totalBusy;              /*Total busy volumes */
1224     int i;                      /*Loop variable */
1225     afs_uint32 volid = 0;       /*Current volume ID */
1226
1227     /*
1228      * Initialize counters and (global!!) queues.
1229      */
1230     totalOK = 0;
1231     totalNotOK = 0;
1232     totalBusy = 0;
1233     qInit(&busyHead);
1234     qInit(&notokHead);
1235
1236     /*
1237      * Display each volume in the list.
1238      */
1239     for (i = 0; i < a_count; i++) {
1240         XDisplayFormat(a_xInfoP, a_servID, a_partID, &totalOK, &totalNotOK,
1241                        &totalBusy, a_fast, a_int32, 0);
1242         a_xInfoP++;
1243     }
1244
1245     /*
1246      * If any volumes were found to be busy or screwed, display them.
1247      */
1248     if (totalBusy) {
1249         while (busyHead.count) {
1250             qGet(&busyHead, &volid);
1251             fprintf(STDOUT, "**** Volume %lu is busy ****\n",
1252                     (unsigned long)volid);
1253         }
1254     }
1255     if (totalNotOK) {
1256         while (notokHead.count) {
1257             qGet(&notokHead, &volid);
1258             fprintf(STDOUT, "**** Could not attach volume %lu ****\n",
1259                     (unsigned long)volid);
1260         }
1261     }
1262
1263     if (!a_quiet) {
1264         fprintf(STDOUT, "\n");
1265         if (!a_fast) {
1266             fprintf(STDOUT,
1267                     "Total volumes: %d on-line, %d off-line, %d  busyd\n\n",
1268                     totalOK, totalNotOK, totalBusy);
1269         }
1270     }
1271
1272 }                               /*XDisplayVolumes */
1273
1274 /*------------------------------------------------------------------------
1275  * PRIVATE XDisplayVolumes2
1276  *
1277  * Description:
1278  *      Display extended formated volume information.
1279  *
1280  * Arguments:
1281  *      a_servID : Pointer to the Rx call we're performing.
1282  *      a_partID : Partition for which we want the extended list.
1283  *      a_xInfoP : Ptr to extended volume info.
1284  *      a_count  : Number of volume records contained above.
1285  *      a_int32   : Int32 listing generated?
1286  *      a_fast   : Fast listing generated?
1287  *      a_quiet  : Quiet listing generated?
1288  *
1289  * Returns:
1290  *      Nothing.
1291  *
1292  * Environment:
1293  *      Nothing interesting.
1294  *
1295  * Side Effects:
1296  *      As advertised.
1297  *------------------------------------------------------------------------*/
1298
1299 static void
1300 XDisplayVolumes2(afs_uint32 a_servID, afs_int32 a_partID, volintXInfo *a_xInfoP,
1301                  afs_int32 a_count, afs_int32 a_int32, afs_int32 a_fast,
1302                  int a_quiet)
1303 {                               /*XDisplayVolumes */
1304
1305     int totalOK;                /*Total OK volumes */
1306     int totalNotOK;             /*Total screwed volumes */
1307     int totalBusy;              /*Total busy volumes */
1308     int i;                      /*Loop variable */
1309     afs_uint32 volid = 0;       /*Current volume ID */
1310
1311     /*
1312      * Initialize counters and (global!!) queues.
1313      */
1314     totalOK = 0;
1315     totalNotOK = 0;
1316     totalBusy = 0;
1317     qInit(&busyHead);
1318     qInit(&notokHead);
1319
1320     /*
1321      * Display each volume in the list.
1322      */
1323     for (i = 0; i < a_count; i++) {
1324         fprintf(STDOUT, "BEGIN_OF_ENTRY\n");
1325         XDisplayFormat2(a_xInfoP, a_servID, a_partID, &totalOK, &totalNotOK,
1326                        &totalBusy, a_fast, a_int32, 0);
1327         fprintf(STDOUT, "END_OF_ENTRY\n");
1328         a_xInfoP++;
1329     }
1330
1331     /*
1332      * If any volumes were found to be busy or screwed, display them.
1333      */
1334     if (totalBusy) {
1335         while (busyHead.count) {
1336             qGet(&busyHead, &volid);
1337             fprintf(STDOUT, "BUSY_VOL\t%lu\n",
1338                     (unsigned long)volid);
1339         }
1340     }
1341     if (totalNotOK) {
1342         while (notokHead.count) {
1343             qGet(&notokHead, &volid);
1344             fprintf(STDOUT, "COULD_NOT_ATTACH\t%lu\n",
1345                     (unsigned long)volid);
1346         }
1347     }
1348
1349     if (!a_quiet) {
1350         fprintf(STDOUT, "\n");
1351         if (!a_fast) {
1352             fprintf(STDOUT,
1353                     "VOLUMES_ONLINE\t%d\nVOLUMES_OFFLINE\t%d\nVOLUMES_BUSY\t%d\n",
1354                     totalOK, totalNotOK, totalBusy);
1355         }
1356     }
1357
1358 }                               /*XDisplayVolumes2 */
1359
1360
1361 /* set <server> and <part> to the correct values depending on
1362  * <voltype> and <entry> */
1363 static void
1364 GetServerAndPart(struct nvldbentry *entry, int voltype, afs_uint32 *server,
1365                  afs_int32 *part, int *previdx)
1366 {
1367     int i, istart, vtype;
1368
1369     *server = -1;
1370     *part = -1;
1371
1372     /* Doesn't check for non-existance of backup volume */
1373     if ((voltype == RWVOL) || (voltype == BACKVOL)) {
1374         vtype = ITSRWVOL;
1375         istart = 0;             /* seach the entire entry */
1376     } else {
1377         vtype = ITSROVOL;
1378         /* Seach from beginning of entry or pick up where we left off */
1379         istart = ((*previdx < 0) ? 0 : *previdx + 1);
1380     }
1381
1382     for (i = istart; i < entry->nServers; i++) {
1383         if (entry->serverFlags[i] & vtype) {
1384             *server = entry->serverNumber[i];
1385             *part = entry->serverPartition[i];
1386             *previdx = i;
1387             return;
1388         }
1389     }
1390
1391     /* Didn't find any, return -1 */
1392     *previdx = -1;
1393     return;
1394 }
1395
1396 static void
1397 PrintLocked(afs_int32 aflags)
1398 {
1399     afs_int32 flags = aflags & VLOP_ALLOPERS;
1400
1401     if (flags) {
1402         fprintf(STDOUT, "    Volume is currently LOCKED  \n");
1403
1404         if (flags & VLOP_MOVE) {
1405             fprintf(STDOUT, "    Volume is locked for a move operation\n");
1406         }
1407         if (flags & VLOP_RELEASE) {
1408             fprintf(STDOUT, "    Volume is locked for a release operation\n");
1409         }
1410         if (flags & VLOP_BACKUP) {
1411             fprintf(STDOUT, "    Volume is locked for a backup operation\n");
1412         }
1413         if (flags & VLOP_DELETE) {
1414             fprintf(STDOUT, "    Volume is locked for a delete/misc operation\n");
1415         }
1416         if (flags & VLOP_DUMP) {
1417             fprintf(STDOUT, "    Volume is locked for a dump/restore operation\n");
1418         }
1419     }
1420 }
1421
1422 static void
1423 PostVolumeStats(struct nvldbentry *entry)
1424 {
1425     SubEnumerateEntry(entry);
1426     /* Check for VLOP_ALLOPERS */
1427     PrintLocked(entry->flags);
1428     return;
1429 }
1430
1431 /*------------------------------------------------------------------------
1432  * PRIVATE XVolumeStats
1433  *
1434  * Description:
1435  *      Display extended volume information.
1436  *
1437  * Arguments:
1438  *      a_xInfoP  : Ptr to extended volume info.
1439  *      a_entryP  : Ptr to the volume's VLDB entry.
1440  *      a_srvID   : Server ID.
1441  *      a_partID  : Partition ID.
1442  *      a_volType : Type of volume to print.
1443  *
1444  * Returns:
1445  *      Nothing.
1446  *
1447  * Environment:
1448  *      Nothing interesting.
1449  *
1450  * Side Effects:
1451  *      As advertised.
1452  *------------------------------------------------------------------------*/
1453
1454 static void
1455 XVolumeStats(volintXInfo *a_xInfoP, struct nvldbentry *a_entryP,
1456              afs_int32 a_srvID, afs_int32 a_partID, int a_volType)
1457 {                               /*XVolumeStats */
1458
1459     int totalOK, totalNotOK, totalBusy; /*Dummies - we don't really count here */
1460
1461     XDisplayFormat(a_xInfoP,    /*Ptr to extended volume info */
1462                    a_srvID,     /*Server ID to print */
1463                    a_partID,    /*Partition ID to print */
1464                    &totalOK,    /*Ptr to total-OK counter */
1465                    &totalNotOK, /*Ptr to total-screwed counter */
1466                    &totalBusy,  /*Ptr to total-busy counter */
1467                    0,           /*Don't do a fast listing */
1468                    1,           /*Do a long listing */
1469                    1);          /*Show volume problems */
1470     return;
1471
1472 }                               /*XVolumeStats */
1473
1474 static void
1475 VolumeStats_int(volintInfo *pntr, struct nvldbentry *entry, afs_uint32 server,
1476              afs_int32 part, int voltype)
1477 {
1478     int totalOK = 0;
1479     int totalNotOK = 0;
1480     int totalBusy = 0;
1481
1482     DisplayFormat(pntr, server, part, &totalOK, &totalNotOK, &totalBusy, 0, 1,
1483                   1);
1484     return;
1485 }
1486
1487 /* command to forcibly remove a volume */
1488 static int
1489 NukeVolume(struct cmd_syndesc *as)
1490 {
1491     afs_int32 code;
1492     afs_uint32 volID;
1493     afs_int32  err;
1494     afs_int32 partID;
1495     afs_uint32 server;
1496     char *tp;
1497
1498     server = GetServer(tp = as->parms[0].items->data);
1499     if (!server) {
1500         fprintf(STDERR, "vos: server '%s' not found in host table\n", tp);
1501         return 1;
1502     }
1503
1504     partID = volutil_GetPartitionID(tp = as->parms[1].items->data);
1505     if (partID == -1) {
1506         fprintf(STDERR, "vos: could not parse '%s' as a partition name", tp);
1507         return 1;
1508     }
1509
1510     volID = vsu_GetVolumeID(tp = as->parms[2].items->data, cstruct, &err);
1511     if (volID == 0) {
1512         if (err)
1513             PrintError("", err);
1514         else
1515             fprintf(STDERR,
1516                     "vos: could not parse '%s' as a numeric volume ID", tp);
1517         return 1;
1518     }
1519
1520     fprintf(STDOUT,
1521             "vos: forcibly removing all traces of volume %d, please wait...",
1522             volID);
1523     fflush(STDOUT);
1524     code = UV_NukeVolume(server, partID, volID);
1525     if (code == 0)
1526         fprintf(STDOUT, "done.\n");
1527     else
1528         fprintf(STDOUT, "failed with code %d.\n", code);
1529     return code;
1530 }
1531
1532
1533 /*------------------------------------------------------------------------
1534  * PRIVATE ExamineVolume
1535  *
1536  * Description:
1537  *      Routine used to examine a single volume, contacting the VLDB as
1538  *      well as the Volume Server.
1539  *
1540  * Arguments:
1541  *      as : Ptr to parsed command line arguments.
1542  *
1543  * Returns:
1544  *      0 for a successful operation,
1545  *      Otherwise, one of the ubik or VolServer error values.
1546  *
1547  * Environment:
1548  *      Nothing interesting.
1549  *
1550  * Side Effects:
1551  *      As advertised.
1552  *------------------------------------------------------------------------
1553  */
1554 static int
1555 ExamineVolume(struct cmd_syndesc *as, void *arock)
1556 {
1557     struct nvldbentry entry;
1558     afs_int32 vcode = 0;
1559     volintInfo *pntr = (volintInfo *) 0;
1560     volintXInfo *xInfoP = (volintXInfo *) 0;
1561     afs_uint32 volid;
1562     afs_int32 code, err, error = 0;
1563     int voltype, foundserv = 0, foundentry = 0;
1564     afs_uint32 aserver;
1565     afs_int32 apart;
1566     int previdx = -1;
1567     int wantExtendedInfo;       /*Do we want extended vol info? */
1568     int isSubEnum=0;            /* Keep track whether sub enumerate called. */
1569     wantExtendedInfo = (as->parms[1].items ? 1 : 0);    /* -extended */
1570
1571     volid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);   /* -id */
1572     if (volid == 0) {
1573         if (err)
1574             PrintError("", err);
1575         else
1576             fprintf(STDERR, "Unknown volume ID or name '%s'\n",
1577                     as->parms[0].items->data);
1578         return -1;
1579     }
1580
1581     if (verbose) {
1582         fprintf(STDOUT, "Fetching VLDB entry for %lu .. ",
1583                 (unsigned long)volid);
1584         fflush(STDOUT);
1585     }
1586     vcode = VLDB_GetEntryByID(volid, -1, &entry);
1587     if (vcode) {
1588         fprintf(STDERR,
1589                 "Could not fetch the entry for volume number %lu from VLDB \n",
1590                 (unsigned long)volid);
1591         return (vcode);
1592     }
1593     if (verbose)
1594         fprintf(STDOUT, "done\n");
1595     MapHostToNetwork(&entry);
1596
1597     if (entry.volumeId[RWVOL] == volid)
1598         voltype = RWVOL;
1599     else if (entry.volumeId[BACKVOL] == volid)
1600         voltype = BACKVOL;
1601     else                        /* (entry.volumeId[ROVOL] == volid) */
1602         voltype = ROVOL;
1603
1604     do {                        /* do {...} while (voltype == ROVOL) */
1605         /* Get the entry for the volume. If its a RW vol, get the RW entry.
1606          * It its a BK vol, get the RW entry (even if VLDB may say the BK doen't exist).
1607          * If its a RO vol, get the next RO entry.
1608          */
1609         GetServerAndPart(&entry, ((voltype == ROVOL) ? ROVOL : RWVOL),
1610                          &aserver, &apart, &previdx);
1611         if (previdx == -1) {    /* searched all entries */
1612             if (!foundentry) {
1613                 fprintf(STDERR, "Volume %s does not exist in VLDB\n\n",
1614                         as->parms[0].items->data);
1615                 error = ENOENT;
1616             }
1617             break;
1618         }
1619         foundentry = 1;
1620
1621         /* Get information about the volume from the server */
1622         if (verbose) {
1623             fprintf(STDOUT, "Getting volume listing from the server %s .. ",
1624                     hostutil_GetNameByINet(aserver));
1625             fflush(STDOUT);
1626         }
1627         if (wantExtendedInfo)
1628             code = UV_XListOneVolume(aserver, apart, volid, &xInfoP);
1629         else
1630             code = UV_ListOneVolume(aserver, apart, volid, &pntr);
1631         if (verbose)
1632             fprintf(STDOUT, "done\n");
1633
1634         if (code) {
1635             error = code;
1636             if (code == ENODEV) {
1637                 if ((voltype == BACKVOL) && !(entry.flags & BACK_EXISTS)) {
1638                     /* The VLDB says there is no backup volume and its not on disk */
1639                     fprintf(STDERR, "Volume %s does not exist\n",
1640                             as->parms[0].items->data);
1641                     error = ENOENT;
1642                 } else {
1643                     fprintf(STDERR,
1644                             "Volume does not exist on server %s as indicated by the VLDB\n",
1645                             hostutil_GetNameByINet(aserver));
1646                 }
1647             } else {
1648                 PrintDiagnostics("examine", code);
1649             }
1650             fprintf(STDOUT, "\n");
1651         } else {
1652             foundserv = 1;
1653             if (wantExtendedInfo)
1654                 XVolumeStats(xInfoP, &entry, aserver, apart, voltype);
1655             else if (as->parms[2].items) {
1656                 DisplayFormat2(aserver, apart, pntr);
1657                 EnumerateEntry(&entry);
1658                 isSubEnum = 1;
1659             } else
1660                 VolumeStats_int(pntr, &entry, aserver, apart, voltype);
1661
1662             if ((voltype == BACKVOL) && !(entry.flags & BACK_EXISTS)) {
1663                 /* The VLDB says there is no backup volume yet we found one on disk */
1664                 fprintf(STDERR, "Volume %s does not exist in VLDB\n",
1665                         as->parms[0].items->data);
1666                 error = ENOENT;
1667             }
1668         }
1669
1670         if (pntr)
1671             free(pntr);
1672         if (xInfoP)
1673             free(xInfoP);
1674     } while (voltype == ROVOL);
1675
1676     if (!foundserv) {
1677         fprintf(STDERR, "Dump only information from VLDB\n\n");
1678         fprintf(STDOUT, "%s \n", entry.name);   /* PostVolumeStats doesn't print name */
1679     }
1680
1681     if (!isSubEnum)
1682         PostVolumeStats(&entry);
1683
1684     return (error);
1685 }
1686
1687 /*------------------------------------------------------------------------
1688  * PRIVATE SetFields
1689  *
1690  * Description:
1691  *      Routine used to change the status of a single volume.
1692  *
1693  * Arguments:
1694  *      as : Ptr to parsed command line arguments.
1695  *
1696  * Returns:
1697  *      0 for a successful operation,
1698  *      Otherwise, one of the ubik or VolServer error values.
1699  *
1700  * Environment:
1701  *      Nothing interesting.
1702  *
1703  * Side Effects:
1704  *      As advertised.
1705  *------------------------------------------------------------------------
1706  */
1707 static int
1708 SetFields(struct cmd_syndesc *as, void *arock)
1709 {
1710     struct nvldbentry entry;
1711     volintInfo info;
1712     afs_uint32 volid;
1713     afs_int32 code, err;
1714     afs_uint32 aserver;
1715     afs_int32 apart;
1716     int previdx = -1;
1717
1718     volid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);   /* -id */
1719     if (volid == 0) {
1720         if (err)
1721             PrintError("", err);
1722         else
1723             fprintf(STDERR, "Unknown volume ID or name '%s'\n",
1724                     as->parms[0].items->data);
1725         return -1;
1726     }
1727
1728     code = VLDB_GetEntryByID(volid, RWVOL, &entry);
1729     if (code) {
1730         fprintf(STDERR,
1731                 "Could not fetch the entry for volume number %lu from VLDB \n",
1732                 (unsigned long)volid);
1733         return (code);
1734     }
1735     MapHostToNetwork(&entry);
1736
1737     GetServerAndPart(&entry, RWVOL, &aserver, &apart, &previdx);
1738     if (previdx == -1) {
1739         fprintf(STDERR, "Volume %s does not exist in VLDB\n\n",
1740                 as->parms[0].items->data);
1741         return (ENOENT);
1742     }
1743
1744     init_volintInfo(&info);
1745     info.volid = volid;
1746     info.type = RWVOL;
1747
1748     if (as->parms[1].items) {
1749         /* -max <quota> */
1750         code = util_GetHumanInt32(as->parms[1].items->data, &info.maxquota);
1751         if (code) {
1752             fprintf(STDERR, "invalid quota value\n");
1753             return code;
1754         }
1755     }
1756     if (as->parms[2].items) {
1757         /* -clearuse */
1758         info.dayUse = 0;
1759     }
1760     if (as->parms[3].items) {
1761         /* -clearVolUpCounter */
1762         info.spare2 = 0;
1763     }
1764     code = UV_SetVolumeInfo(aserver, apart, volid, &info);
1765     if (code)
1766         fprintf(STDERR,
1767                 "Could not update volume info fields for volume number %lu\n",
1768                 (unsigned long)volid);
1769     return (code);
1770 }
1771
1772 /*------------------------------------------------------------------------
1773  * PRIVATE volOnline
1774  *
1775  * Description:
1776  *      Brings a volume online.
1777  *
1778  * Arguments:
1779  *      as : Ptr to parsed command line arguments.
1780  *
1781  * Returns:
1782  *      0 for a successful operation,
1783  *
1784  * Environment:
1785  *      Nothing interesting.
1786  *
1787  * Side Effects:
1788  *      As advertised.
1789  *------------------------------------------------------------------------
1790  */
1791 static int
1792 volOnline(struct cmd_syndesc *as, void *arock)
1793 {
1794     afs_uint32 server;
1795     afs_int32 partition;
1796     afs_uint32 volid;
1797     afs_int32 code, err = 0;
1798
1799     server = GetServer(as->parms[0].items->data);
1800     if (server == 0) {
1801         fprintf(STDERR, "vos: server '%s' not found in host table\n",
1802                 as->parms[0].items->data);
1803         return -1;
1804     }
1805
1806     partition = volutil_GetPartitionID(as->parms[1].items->data);
1807     if (partition < 0) {
1808         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
1809                 as->parms[1].items->data);
1810         return ENOENT;
1811     }
1812
1813     volid = vsu_GetVolumeID(as->parms[2].items->data, cstruct, &err);   /* -id */
1814     if (!volid) {
1815         if (err)
1816             PrintError("", err);
1817         else
1818             fprintf(STDERR, "Unknown volume ID or name '%s'\n",
1819                     as->parms[0].items->data);
1820         return -1;
1821     }
1822
1823     code = UV_SetVolume(server, partition, volid, ITOffline, 0 /*online */ ,
1824                         0 /*sleep */ );
1825     if (code) {
1826         fprintf(STDERR, "Failed to set volume. Code = %d\n", code);
1827         return -1;
1828     }
1829
1830     return 0;
1831 }
1832
1833 /*------------------------------------------------------------------------
1834  * PRIVATE volOffline
1835  *
1836  * Description:
1837  *      Brings a volume offline.
1838  *
1839  * Arguments:
1840  *      as : Ptr to parsed command line arguments.
1841  *
1842  * Returns:
1843  *      0 for a successful operation,
1844  *
1845  * Environment:
1846  *      Nothing interesting.
1847  *
1848  * Side Effects:
1849  *      As advertised.
1850  *------------------------------------------------------------------------
1851  */
1852 static int
1853 volOffline(struct cmd_syndesc *as, void *arock)
1854 {
1855     afs_uint32 server;
1856     afs_int32 partition;
1857     afs_uint32 volid;
1858     afs_int32 code, err = 0;
1859     afs_int32 transflag, sleeptime, transdone;
1860
1861     server = GetServer(as->parms[0].items->data);
1862     if (server == 0) {
1863         fprintf(STDERR, "vos: server '%s' not found in host table\n",
1864                 as->parms[0].items->data);
1865         return -1;
1866     }
1867
1868     partition = volutil_GetPartitionID(as->parms[1].items->data);
1869     if (partition < 0) {
1870         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
1871                 as->parms[1].items->data);
1872         return ENOENT;
1873     }
1874
1875     volid = vsu_GetVolumeID(as->parms[2].items->data, cstruct, &err);   /* -id */
1876     if (!volid) {
1877         if (err)
1878             PrintError("", err);
1879         else
1880             fprintf(STDERR, "Unknown volume ID or name '%s'\n",
1881                     as->parms[0].items->data);
1882         return -1;
1883     }
1884
1885     transflag = (as->parms[4].items ? ITBusy : ITOffline);
1886     sleeptime = (as->parms[3].items ? atol(as->parms[3].items->data) : 0);
1887     transdone = ((sleeptime || as->parms[4].items) ? 0 /*online */ : VTOutOfService);
1888     if (as->parms[4].items && !as->parms[3].items) {
1889         fprintf(STDERR, "-sleep option must be used with -busy flag\n");
1890         return -1;
1891     }
1892
1893     code =
1894         UV_SetVolume(server, partition, volid, transflag, transdone,
1895                      sleeptime);
1896     if (code) {
1897         fprintf(STDERR, "Failed to set volume. Code = %d\n", code);
1898         return -1;
1899     }
1900
1901     return 0;
1902 }
1903
1904 static int
1905 CreateVolume(struct cmd_syndesc *as, void *arock)
1906 {
1907     afs_int32 pnum;
1908     char part[10];
1909     afs_uint32 volid = 0, rovolid = 0, bkvolid = 0;
1910     afs_uint32 *arovolid;
1911     afs_int32 code;
1912     struct nvldbentry entry;
1913     afs_int32 vcode;
1914     afs_int32 quota;
1915     afs_uint32 tserver;
1916
1917     arovolid = &rovolid;
1918
1919     quota = 5000;
1920     tserver = GetServer(as->parms[0].items->data);
1921     if (!tserver) {
1922         fprintf(STDERR, "vos: host '%s' not found in host table\n",
1923                 as->parms[0].items->data);
1924         return ENOENT;
1925     }
1926     pnum = volutil_GetPartitionID(as->parms[1].items->data);
1927     if (pnum < 0) {
1928         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
1929                 as->parms[1].items->data);
1930         return ENOENT;
1931     }
1932     if (!IsPartValid(pnum, tserver, &code)) {   /*check for validity of the partition */
1933         if (code)
1934             PrintError("", code);
1935         else
1936             fprintf(STDERR,
1937                     "vos : partition %s does not exist on the server\n",
1938                     as->parms[1].items->data);
1939         return ENOENT;
1940     }
1941     if (!ISNAMEVALID(as->parms[2].items->data)) {
1942         fprintf(STDERR,
1943                 "vos: the name of the root volume %s exceeds the size limit of %d\n",
1944                 as->parms[2].items->data, VOLSER_OLDMAXVOLNAME - 10);
1945         return E2BIG;
1946     }
1947     if (!VolNameOK(as->parms[2].items->data)) {
1948         fprintf(STDERR,
1949                 "Illegal volume name %s, should not end in .readonly or .backup\n",
1950                 as->parms[2].items->data);
1951         return EINVAL;
1952     }
1953     if (IsNumeric(as->parms[2].items->data)) {
1954         fprintf(STDERR, "Illegal volume name %s, should not be a number\n",
1955                 as->parms[2].items->data);
1956         return EINVAL;
1957     }
1958     vcode = VLDB_GetEntryByName(as->parms[2].items->data, &entry);
1959     if (!vcode) {
1960         fprintf(STDERR, "Volume %s already exists\n",
1961                 as->parms[2].items->data);
1962         PrintDiagnostics("create", code);
1963         return EEXIST;
1964     }
1965
1966     if (as->parms[3].items) {
1967         code = util_GetHumanInt32(as->parms[3].items->data, &quota);
1968         if (code) {
1969             fprintf(STDERR, "vos: bad integer specified for quota.\n");
1970             return code;
1971         }
1972     }
1973
1974     if (as->parms[4].items) {
1975         if (!IsNumeric(as->parms[4].items->data)) {
1976             fprintf(STDERR, "vos: Given volume ID %s should be numeric.\n",
1977                     as->parms[4].items->data);
1978             return EINVAL;
1979         }
1980
1981         code = util_GetUInt32(as->parms[4].items->data, &volid);
1982         if (code) {
1983             fprintf(STDERR, "vos: bad integer specified for volume ID.\n");
1984             return code;
1985         }
1986     }
1987
1988     if (as->parms[5].items) {
1989         if (!IsNumeric(as->parms[5].items->data)) {
1990             fprintf(STDERR, "vos: Given RO volume ID %s should be numeric.\n",
1991                     as->parms[5].items->data);
1992             return EINVAL;
1993         }
1994
1995         code = util_GetUInt32(as->parms[5].items->data, &rovolid);
1996         if (code) {
1997             fprintf(STDERR, "vos: bad integer specified for volume ID.\n");
1998             return code;
1999         }
2000
2001         if (rovolid == 0) {
2002             arovolid = NULL;
2003         }
2004     }
2005
2006     code =
2007         UV_CreateVolume3(tserver, pnum, as->parms[2].items->data, quota, 0,
2008                          0, 0, 0, &volid, arovolid, &bkvolid);
2009     if (code) {
2010         PrintDiagnostics("create", code);
2011         return code;
2012     }
2013     MapPartIdIntoName(pnum, part);
2014     fprintf(STDOUT, "Volume %lu created on partition %s of %s\n",
2015             (unsigned long)volid, part, as->parms[0].items->data);
2016
2017     return 0;
2018 }
2019
2020 #if 0
2021 static afs_int32
2022 DeleteAll(struct nvldbentry *entry)
2023 {
2024     int i;
2025     afs_int32 error, code, curserver, curpart;
2026     afs_uint32 volid;
2027
2028     MapHostToNetwork(entry);
2029     error = 0;
2030     for (i = 0; i < entry->nServers; i++) {
2031         curserver = entry->serverNumber[i];
2032         curpart = entry->serverPartition[i];
2033         if (entry->serverFlags[i] & ITSROVOL) {
2034             volid = entry->volumeId[ROVOL];
2035         } else {
2036             volid = entry->volumeId[RWVOL];
2037         }
2038         code = UV_DeleteVolume(curserver, curpart, volid);
2039         if (code && !error)
2040             error = code;
2041     }
2042     return error;
2043 }
2044 #endif
2045
2046 static int
2047 DeleteVolume(struct cmd_syndesc *as, void *arock)
2048 {
2049     afs_int32 err, code = 0;
2050     afs_uint32 server = 0;
2051     afs_int32 partition = -1;
2052     afs_uint32 volid;
2053     char pname[10];
2054     afs_int32 idx, j;
2055
2056     if (as->parms[0].items) {
2057         server = GetServer(as->parms[0].items->data);
2058         if (!server) {
2059             fprintf(STDERR, "vos: server '%s' not found in host table\n",
2060                     as->parms[0].items->data);
2061             return ENOENT;
2062         }
2063     }
2064
2065     if (as->parms[1].items) {
2066         partition = volutil_GetPartitionID(as->parms[1].items->data);
2067         if (partition < 0) {
2068             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2069                     as->parms[1].items->data);
2070             return EINVAL;
2071         }
2072
2073         /* Check for validity of the partition */
2074         if (!IsPartValid(partition, server, &code)) {
2075             if (code) {
2076                 PrintError("", code);
2077             } else {
2078                 fprintf(STDERR,
2079                         "vos : partition %s does not exist on the server\n",
2080                         as->parms[1].items->data);
2081             }
2082             return ENOENT;
2083         }
2084     }
2085
2086     volid = vsu_GetVolumeID(as->parms[2].items->data, cstruct, &err);
2087     if (volid == 0) {
2088         fprintf(STDERR, "Can't find volume name '%s' in VLDB\n",
2089                 as->parms[2].items->data);
2090         if (err)
2091             PrintError("", err);
2092         return ENOENT;
2093     }
2094
2095     /* If the server or partition option are not complete, try to fill
2096      * them in from the VLDB entry.
2097      */
2098     if ((partition == -1) || !server) {
2099         struct nvldbentry entry;
2100
2101         code = VLDB_GetEntryByID(volid, -1, &entry);
2102         if (code) {
2103             fprintf(STDERR,
2104                     "Could not fetch the entry for volume %lu from VLDB\n",
2105                     (unsigned long)volid);
2106             PrintError("", code);
2107             return (code);
2108         }
2109
2110         if (((volid == entry.volumeId[RWVOL]) && (entry.flags & RW_EXISTS))
2111             || ((volid == entry.volumeId[BACKVOL])
2112                 && (entry.flags & BACK_EXISTS))) {
2113             idx = Lp_GetRwIndex(&entry);
2114             if ((idx == -1) || (server && (server != entry.serverNumber[idx]))
2115                 || ((partition != -1)
2116                     && (partition != entry.serverPartition[idx]))) {
2117                 fprintf(STDERR, "VLDB: Volume '%s' no match\n",
2118                         as->parms[2].items->data);
2119                 return ENOENT;
2120             }
2121         } else if ((volid == entry.volumeId[ROVOL])
2122                    && (entry.flags & RO_EXISTS)) {
2123             for (idx = -1, j = 0; j < entry.nServers; j++) {
2124                 if (entry.serverFlags[j] != ITSROVOL)
2125                     continue;
2126
2127                 if (((server == 0) || (server == entry.serverNumber[j]))
2128                     && ((partition == -1)
2129                         || (partition == entry.serverPartition[j]))) {
2130                     if (idx != -1) {
2131                         fprintf(STDERR,
2132                                 "VLDB: Volume '%s' matches more than one RO\n",
2133                                 as->parms[2].items->data);
2134                         return ENOENT;
2135                     }
2136                     idx = j;
2137                 }
2138             }
2139             if (idx == -1) {
2140                 fprintf(STDERR, "VLDB: Volume '%s' no match\n",
2141                         as->parms[2].items->data);
2142                 return ENOENT;
2143             }
2144         } else {
2145             fprintf(STDERR, "VLDB: Volume '%s' no match\n",
2146                     as->parms[2].items->data);
2147             return ENOENT;
2148         }
2149
2150         server = htonl(entry.serverNumber[idx]);
2151         partition = entry.serverPartition[idx];
2152     }
2153
2154
2155     code = UV_DeleteVolume(server, partition, volid);
2156     if (code) {
2157         PrintDiagnostics("remove", code);
2158         return code;
2159     }
2160
2161     MapPartIdIntoName(partition, pname);
2162     fprintf(STDOUT, "Volume %lu on partition %s server %s deleted\n",
2163             (unsigned long)volid, pname, hostutil_GetNameByINet(server));
2164     return 0;
2165 }
2166
2167 #define TESTM   0               /* set for move space tests, clear for production */
2168 static int
2169 MoveVolume(struct cmd_syndesc *as, void *arock)
2170 {
2171
2172     afs_uint32 volid;
2173     afs_uint32 fromserver, toserver;
2174     afs_int32 frompart, topart;
2175     afs_int32 flags, code, err;
2176     char fromPartName[10], toPartName[10];
2177
2178     struct diskPartition64 partition;   /* for space check */
2179     volintInfo *p;
2180
2181     volid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
2182     if (volid == 0) {
2183         if (err)
2184             PrintError("", err);
2185         else
2186             fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2187                     as->parms[0].items->data);
2188         return ENOENT;
2189     }
2190     fromserver = GetServer(as->parms[1].items->data);
2191     if (fromserver == 0) {
2192         fprintf(STDERR, "vos: server '%s' not found in host table\n",
2193                 as->parms[1].items->data);
2194         return ENOENT;
2195     }
2196     toserver = GetServer(as->parms[3].items->data);
2197     if (toserver == 0) {
2198         fprintf(STDERR, "vos: server '%s' not found in host table\n",
2199                 as->parms[3].items->data);
2200         return ENOENT;
2201     }
2202     frompart = volutil_GetPartitionID(as->parms[2].items->data);
2203     if (frompart < 0) {
2204         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2205                 as->parms[2].items->data);
2206         return EINVAL;
2207     }
2208     if (!IsPartValid(frompart, fromserver, &code)) {    /*check for validity of the partition */
2209         if (code)
2210             PrintError("", code);
2211         else
2212             fprintf(STDERR,
2213                     "vos : partition %s does not exist on the server\n",
2214                     as->parms[2].items->data);
2215         return ENOENT;
2216     }
2217     topart = volutil_GetPartitionID(as->parms[4].items->data);
2218     if (topart < 0) {
2219         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2220                 as->parms[4].items->data);
2221         return EINVAL;
2222     }
2223     if (!IsPartValid(topart, toserver, &code)) {        /*check for validity of the partition */
2224         if (code)
2225             PrintError("", code);
2226         else
2227             fprintf(STDERR,
2228                     "vos : partition %s does not exist on the server\n",
2229                     as->parms[4].items->data);
2230         return ENOENT;
2231     }
2232
2233     flags = 0;
2234     if (as->parms[5].items) flags |= RV_NOCLONE;
2235
2236     /*
2237      * check source partition for space to clone volume
2238      */
2239
2240     MapPartIdIntoName(topart, toPartName);
2241     MapPartIdIntoName(frompart, fromPartName);
2242
2243     /*
2244      * check target partition for space to move volume
2245      */
2246
2247     code = UV_PartitionInfo64(toserver, toPartName, &partition);
2248     if (code) {
2249         fprintf(STDERR, "vos: cannot access partition %s\n", toPartName);
2250         exit(1);
2251     }
2252     if (TESTM)
2253         fprintf(STDOUT, "target partition %s free space %" AFS_INT64_FMT "\n", toPartName,
2254                 partition.free);
2255
2256     p = (volintInfo *) 0;
2257     code = UV_ListOneVolume(fromserver, frompart, volid, &p);
2258     if (code) {
2259         fprintf(STDERR, "vos:cannot access volume %lu\n",
2260                 (unsigned long)volid);
2261         exit(1);
2262     }
2263     if (TESTM)
2264         fprintf(STDOUT, "volume %lu size %d\n", (unsigned long)volid,
2265                 p->size);
2266     if (partition.free <= p->size) {
2267         fprintf(STDERR,
2268                 "vos: no space on target partition %s to move volume %lu\n",
2269                 toPartName, (unsigned long)volid);
2270         free(p);
2271         exit(1);
2272     }
2273     free(p);
2274
2275     if (TESTM) {
2276         fprintf(STDOUT, "size test - don't do move\n");
2277         exit(0);
2278     }
2279
2280     /* successful move still not guaranteed but shoot for it */
2281
2282     code =
2283         UV_MoveVolume2(volid, fromserver, frompart, toserver, topart, flags);
2284     if (code) {
2285         PrintDiagnostics("move", code);
2286         return code;
2287     }
2288     MapPartIdIntoName(topart, toPartName);
2289     MapPartIdIntoName(frompart, fromPartName);
2290     fprintf(STDOUT, "Volume %lu moved from %s %s to %s %s \n",
2291             (unsigned long)volid, as->parms[1].items->data, fromPartName,
2292             as->parms[3].items->data, toPartName);
2293
2294     return 0;
2295 }
2296
2297 static int
2298 CopyVolume(struct cmd_syndesc *as, void *arock)
2299 {
2300     afs_uint32 volid;
2301     afs_uint32 fromserver, toserver;
2302     afs_int32 frompart, topart, code, err, flags;
2303     char fromPartName[10], toPartName[10], *tovolume;
2304     struct nvldbentry entry;
2305     struct diskPartition64 partition;   /* for space check */
2306     volintInfo *p;
2307
2308     volid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
2309     if (volid == 0) {
2310         if (err)
2311             PrintError("", err);
2312         else
2313             fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2314                     as->parms[0].items->data);
2315         return ENOENT;
2316     }
2317     fromserver = GetServer(as->parms[1].items->data);
2318     if (fromserver == 0) {
2319         fprintf(STDERR, "vos: server '%s' not found in host table\n",
2320                 as->parms[1].items->data);
2321         return ENOENT;
2322     }
2323
2324     toserver = GetServer(as->parms[4].items->data);
2325     if (toserver == 0) {
2326         fprintf(STDERR, "vos: server '%s' not found in host table\n",
2327                 as->parms[4].items->data);
2328         return ENOENT;
2329     }
2330
2331     tovolume = as->parms[3].items->data;
2332     if (!ISNAMEVALID(tovolume)) {
2333         fprintf(STDERR,
2334                 "vos: the name of the root volume %s exceeds the size limit of %d\n",
2335                 tovolume, VOLSER_OLDMAXVOLNAME - 10);
2336         return E2BIG;
2337     }
2338     if (!VolNameOK(tovolume)) {
2339         fprintf(STDERR,
2340                 "Illegal volume name %s, should not end in .readonly or .backup\n",
2341                 tovolume);
2342         return EINVAL;
2343     }
2344     if (IsNumeric(tovolume)) {
2345         fprintf(STDERR, "Illegal volume name %s, should not be a number\n",
2346                 tovolume);
2347         return EINVAL;
2348     }
2349     code = VLDB_GetEntryByName(tovolume, &entry);
2350     if (!code) {
2351         fprintf(STDERR, "Volume %s already exists\n", tovolume);
2352         PrintDiagnostics("copy", code);
2353         return EEXIST;
2354     }
2355
2356     frompart = volutil_GetPartitionID(as->parms[2].items->data);
2357     if (frompart < 0) {
2358         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2359                 as->parms[2].items->data);
2360         return EINVAL;
2361     }
2362     if (!IsPartValid(frompart, fromserver, &code)) {    /*check for validity of the partition */
2363         if (code)
2364             PrintError("", code);
2365         else
2366             fprintf(STDERR,
2367                     "vos : partition %s does not exist on the server\n",
2368                     as->parms[2].items->data);
2369         return ENOENT;
2370     }
2371
2372     topart = volutil_GetPartitionID(as->parms[5].items->data);
2373     if (topart < 0) {
2374         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2375                 as->parms[5].items->data);
2376         return EINVAL;
2377     }
2378     if (!IsPartValid(topart, toserver, &code)) {        /*check for validity of the partition */
2379         if (code)
2380             PrintError("", code);
2381         else
2382             fprintf(STDERR,
2383                     "vos : partition %s does not exist on the server\n",
2384                     as->parms[5].items->data);
2385         return ENOENT;
2386     }
2387
2388     flags = 0;
2389     if (as->parms[6].items) flags |= RV_OFFLINE;
2390     if (as->parms[7].items) flags |= RV_RDONLY;
2391     if (as->parms[8].items) flags |= RV_NOCLONE;
2392
2393     MapPartIdIntoName(topart, toPartName);
2394     MapPartIdIntoName(frompart, fromPartName);
2395
2396     /*
2397      * check target partition for space to move volume
2398      */
2399
2400     code = UV_PartitionInfo64(toserver, toPartName, &partition);
2401     if (code) {
2402         fprintf(STDERR, "vos: cannot access partition %s\n", toPartName);
2403         exit(1);
2404     }
2405     if (TESTM)
2406         fprintf(STDOUT, "target partition %s free space %" AFS_INT64_FMT "\n", toPartName,
2407                 partition.free);
2408
2409     p = (volintInfo *) 0;
2410     code = UV_ListOneVolume(fromserver, frompart, volid, &p);
2411     if (code) {
2412         fprintf(STDERR, "vos:cannot access volume %lu\n",
2413                 (unsigned long)volid);
2414         exit(1);
2415     }
2416
2417     if (partition.free <= p->size) {
2418         fprintf(STDERR,
2419                 "vos: no space on target partition %s to copy volume %lu\n",
2420                 toPartName, (unsigned long)volid);
2421         free(p);
2422         exit(1);
2423     }
2424     free(p);
2425
2426     /* successful copy still not guaranteed but shoot for it */
2427
2428     code =
2429         UV_CopyVolume2(volid, fromserver, frompart, tovolume, toserver,
2430                        topart, 0, flags);
2431     if (code) {
2432         PrintDiagnostics("copy", code);
2433         return code;
2434     }
2435     MapPartIdIntoName(topart, toPartName);
2436     MapPartIdIntoName(frompart, fromPartName);
2437     fprintf(STDOUT, "Volume %lu copied from %s %s to %s on %s %s \n",
2438             (unsigned long)volid, as->parms[1].items->data, fromPartName,
2439             tovolume, as->parms[4].items->data, toPartName);
2440
2441     return 0;
2442 }
2443
2444
2445 static int
2446 ShadowVolume(struct cmd_syndesc *as, void *arock)
2447 {
2448     afs_uint32 volid, tovolid;
2449     afs_uint32 fromserver, toserver;
2450     afs_int32 frompart, topart;
2451     afs_int32 code, err, flags;
2452     char fromPartName[10], toPartName[10], toVolName[32], *tovolume;
2453     struct diskPartition64 partition;   /* for space check */
2454     volintInfo *p, *q;
2455
2456     p = (volintInfo *) 0;
2457     q = (volintInfo *) 0;
2458
2459     volid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
2460     if (volid == 0) {
2461         if (err)
2462             PrintError("", err);
2463         else
2464             fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2465                     as->parms[0].items->data);
2466         return ENOENT;
2467     }
2468     fromserver = GetServer(as->parms[1].items->data);
2469     if (fromserver == 0) {
2470         fprintf(STDERR, "vos: server '%s' not found in host table\n",
2471                 as->parms[1].items->data);
2472         return ENOENT;
2473     }
2474
2475     toserver = GetServer(as->parms[3].items->data);
2476     if (toserver == 0) {
2477         fprintf(STDERR, "vos: server '%s' not found in host table\n",
2478                 as->parms[3].items->data);
2479         return ENOENT;
2480     }
2481
2482     frompart = volutil_GetPartitionID(as->parms[2].items->data);
2483     if (frompart < 0) {
2484         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2485                 as->parms[2].items->data);
2486         return EINVAL;
2487     }
2488     if (!IsPartValid(frompart, fromserver, &code)) {    /*check for validity of the partition */
2489         if (code)
2490             PrintError("", code);
2491         else
2492             fprintf(STDERR,
2493                     "vos : partition %s does not exist on the server\n",
2494                     as->parms[2].items->data);
2495         return ENOENT;
2496     }
2497
2498     topart = volutil_GetPartitionID(as->parms[4].items->data);
2499     if (topart < 0) {
2500         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2501                 as->parms[4].items->data);
2502         return EINVAL;
2503     }
2504     if (!IsPartValid(topart, toserver, &code)) {        /*check for validity of the partition */
2505         if (code)
2506             PrintError("", code);
2507         else
2508             fprintf(STDERR,
2509                     "vos : partition %s does not exist on the server\n",
2510                     as->parms[4].items->data);
2511         return ENOENT;
2512     }
2513
2514     if (as->parms[5].items) {
2515         tovolume = as->parms[5].items->data;
2516         if (!ISNAMEVALID(tovolume)) {
2517             fprintf(STDERR,
2518                 "vos: the name of the root volume %s exceeds the size limit of %d\n",
2519                 tovolume, VOLSER_OLDMAXVOLNAME - 10);
2520             return E2BIG;
2521         }
2522         if (!VolNameOK(tovolume)) {
2523             fprintf(STDERR,
2524                 "Illegal volume name %s, should not end in .readonly or .backup\n",
2525                 tovolume);
2526             return EINVAL;
2527         }
2528         if (IsNumeric(tovolume)) {
2529             fprintf(STDERR,
2530                 "Illegal volume name %s, should not be a number\n",
2531                 tovolume);
2532             return EINVAL;
2533         }
2534     } else {
2535         /* use actual name of source volume */
2536         code = UV_ListOneVolume(fromserver, frompart, volid, &p);
2537         if (code) {
2538             fprintf(STDERR, "vos:cannot access volume %lu\n",
2539                 (unsigned long)volid);
2540             exit(1);
2541         }
2542         strcpy(toVolName, p->name);
2543         tovolume = toVolName;
2544         /* save p for size checks later */
2545     }
2546
2547     if (as->parms[6].items) {
2548         tovolid = vsu_GetVolumeID(as->parms[6].items->data, cstruct, &err);
2549         if (tovolid == 0) {
2550             if (err)
2551                 PrintError("", err);
2552             else
2553                 fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2554                         as->parms[6].items->data);
2555             if (p)
2556                 free(p);
2557             return ENOENT;
2558         }
2559     } else {
2560         tovolid = vsu_GetVolumeID(tovolume, cstruct, &err);
2561         if (tovolid == 0) {
2562             if (err)
2563                 PrintError("", err);
2564             else
2565                 fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2566                         tovolume);
2567             if (p)
2568                 free(p);
2569             return ENOENT;
2570         }
2571     }
2572
2573     flags = RV_NOVLDB;
2574     if (as->parms[7].items) flags |= RV_OFFLINE;
2575     if (as->parms[8].items) flags |= RV_RDONLY;
2576     if (as->parms[9].items) flags |= RV_NOCLONE;
2577     if (as->parms[10].items) flags |= RV_CPINCR;
2578
2579     MapPartIdIntoName(topart, toPartName);
2580     MapPartIdIntoName(frompart, fromPartName);
2581
2582     /*
2583      * check target partition for space to move volume
2584      */
2585
2586     code = UV_PartitionInfo64(toserver, toPartName, &partition);
2587     if (code) {
2588         fprintf(STDERR, "vos: cannot access partition %s\n", toPartName);
2589         exit(1);
2590     }
2591     if (TESTM)
2592         fprintf(STDOUT, "target partition %s free space %" AFS_INT64_FMT "\n", toPartName,
2593                 partition.free);
2594
2595     /* Don't do this again if we did it above */
2596     if (!p) {
2597         code = UV_ListOneVolume(fromserver, frompart, volid, &p);
2598         if (code) {
2599             fprintf(STDERR, "vos:cannot access volume %lu\n",
2600                 (unsigned long)volid);
2601             exit(1);
2602         }
2603     }
2604
2605     /* OK if this fails */
2606     code = UV_ListOneVolume(toserver, topart, tovolid, &q);
2607
2608     /* Treat existing volume size as "free" */
2609     if (q)
2610         p->size = (q->size < p->size) ? p->size - q->size : 0;
2611
2612     if (partition.free <= p->size) {
2613         fprintf(STDERR,
2614                 "vos: no space on target partition %s to copy volume %lu\n",
2615                 toPartName, (unsigned long)volid);
2616         free(p);
2617         if (q) free(q);
2618         exit(1);
2619     }
2620     free(p);
2621     if (q) free(q);
2622
2623     /* successful copy still not guaranteed but shoot for it */
2624
2625     code =
2626         UV_CopyVolume2(volid, fromserver, frompart, tovolume, toserver,
2627                        topart, tovolid, flags);
2628     if (code) {
2629         PrintDiagnostics("shadow", code);
2630         return code;
2631     }
2632     MapPartIdIntoName(topart, toPartName);
2633     MapPartIdIntoName(frompart, fromPartName);
2634     fprintf(STDOUT, "Volume %lu shadowed from %s %s to %s %s \n",
2635             (unsigned long)volid, as->parms[1].items->data, fromPartName,
2636             as->parms[3].items->data, toPartName);
2637
2638     return 0;
2639 }
2640
2641
2642 static int
2643 CloneVolume(struct cmd_syndesc *as, void *arock)
2644 {
2645     afs_uint32 volid, cloneid;
2646     afs_uint32 server;
2647     afs_int32 part, voltype;
2648     char partName[10], *volname;
2649     afs_int32 code, err, flags;
2650     struct nvldbentry entry;
2651
2652     volid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
2653     if (volid == 0) {
2654         if (err)
2655             PrintError("", err);
2656         else
2657             fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2658                     as->parms[0].items->data);
2659         return ENOENT;
2660     }
2661
2662     if (as->parms[1].items || as->parms[2].items) {
2663         if (!as->parms[1].items || !as->parms[2].items) {
2664             fprintf(STDERR,
2665                     "Must specify both -server and -partition options\n");
2666             return -1;
2667         }
2668         server = GetServer(as->parms[1].items->data);
2669         if (server == 0) {
2670             fprintf(STDERR, "vos: server '%s' not found in host table\n",
2671                     as->parms[1].items->data);
2672             return ENOENT;
2673         }
2674         part = volutil_GetPartitionID(as->parms[2].items->data);
2675         if (part < 0) {
2676             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2677                     as->parms[2].items->data);
2678             return EINVAL;
2679         }
2680         if (!IsPartValid(part, server, &code)) {        /*check for validity of the partition */
2681             if (code)
2682                 PrintError("", code);
2683             else
2684                 fprintf(STDERR,
2685                     "vos : partition %s does not exist on the server\n",
2686                     as->parms[2].items->data);
2687             return ENOENT;
2688         }
2689     } else {
2690         code = GetVolumeInfo(volid, &server, &part, &voltype, &entry);
2691         if (code)
2692             return code;
2693     }
2694
2695     volname = 0;
2696     if (as->parms[3].items) {
2697         volname = as->parms[3].items->data;
2698         if (strlen(volname) > VOLSER_OLDMAXVOLNAME - 1) {
2699             fprintf(STDERR,
2700                 "vos: the name of the root volume %s exceeds the size limit of %d\n",
2701                 volname, VOLSER_OLDMAXVOLNAME - 1);
2702             return E2BIG;
2703         }
2704 #if 0
2705         /*
2706          * In order that you be able to make clones of RO or BK, this
2707          * check must be omitted.
2708          */
2709         if (!VolNameOK(volname)) {
2710             fprintf(STDERR,
2711                 "Illegal volume name %s, should not end in .readonly or .backup\n",
2712                 volname);
2713             return EINVAL;
2714         }
2715 #endif
2716         if (IsNumeric(volname)) {
2717             fprintf(STDERR,
2718                 "Illegal volume name %s, should not be a number\n",
2719                 volname);
2720             return EINVAL;
2721         }
2722     }
2723
2724     cloneid = 0;
2725     if (as->parms[4].items) {
2726         cloneid = vsu_GetVolumeID(as->parms[4].items->data, cstruct, &err);
2727         if (cloneid == 0) {
2728             if (err)
2729                 PrintError("", err);
2730             else
2731                 fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2732                         as->parms[4].items->data);
2733             return ENOENT;
2734         }
2735     }
2736
2737     flags = 0;
2738     if (as->parms[5].items) flags |= RV_OFFLINE;
2739     if (as->parms[6].items && as->parms[7].items) {
2740         fprintf(STDERR, "vos: cannot specify that a volume be -readwrite and -readonly\n");
2741         return EINVAL;
2742     }
2743     if (as->parms[6].items) flags |= RV_RDONLY;
2744     if (as->parms[7].items) flags |= RV_RWONLY;
2745
2746
2747     code =
2748         UV_CloneVolume(server, part, volid, cloneid, volname, flags);
2749
2750     if (code) {
2751         PrintDiagnostics("clone", code);
2752         return code;
2753     }
2754     MapPartIdIntoName(part, partName);
2755     fprintf(STDOUT, "Created clone for volume %s\n",
2756             as->parms[0].items->data);
2757
2758     return 0;
2759 }
2760
2761
2762 static int
2763 BackupVolume(struct cmd_syndesc *as, void *arock)
2764 {
2765     afs_uint32 avolid;
2766     afs_uint32 aserver;
2767     afs_int32 apart, vtype, code, err;
2768     struct nvldbentry entry;
2769
2770     afs_uint32 buvolid;
2771     afs_uint32 buserver;
2772     afs_int32 bupart, butype;
2773     struct nvldbentry buentry;
2774
2775     avolid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
2776     if (avolid == 0) {
2777         if (err)
2778             PrintError("", err);
2779         else
2780             fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2781                     as->parms[0].items->data);
2782         return ENOENT;
2783     }
2784     code = GetVolumeInfo(avolid, &aserver, &apart, &vtype, &entry);
2785     if (code)
2786         exit(1);
2787
2788     /* verify this is a readwrite volume */
2789
2790     if (vtype != RWVOL) {
2791         fprintf(STDERR, "%s not RW volume\n", as->parms[0].items->data);
2792         exit(1);
2793     }
2794
2795     /* is there a backup volume already? */
2796
2797     if (entry.flags & BACK_EXISTS) {
2798         /* yep, where is it? */
2799
2800         buvolid = entry.volumeId[BACKVOL];
2801         code = GetVolumeInfo(buvolid, &buserver, &bupart, &butype, &buentry);
2802         if (code)
2803             exit(1);
2804
2805         /* is it local? */
2806         code = VLDB_IsSameAddrs(buserver, aserver, &err);
2807         if (err) {
2808             fprintf(STDERR,
2809                     "Failed to get info about server's %d address(es) from vlserver; aborting call!\n",
2810                     buserver);
2811             exit(1);
2812         }
2813         if (!code) {
2814             fprintf(STDERR,
2815                     "FATAL ERROR: backup volume %lu exists on server %lu\n",
2816                     (unsigned long)buvolid, (unsigned long)buserver);
2817             exit(1);
2818         }
2819     }
2820
2821     /* nope, carry on */
2822
2823     code = UV_BackupVolume(aserver, apart, avolid);
2824
2825     if (code) {
2826         PrintDiagnostics("backup", code);
2827         return code;
2828     }
2829     fprintf(STDOUT, "Created backup volume for %s \n",
2830             as->parms[0].items->data);
2831     return 0;
2832 }
2833
2834 static int
2835 ReleaseVolume(struct cmd_syndesc *as, void *arock)
2836 {
2837
2838     struct nvldbentry entry;
2839     afs_uint32 avolid;
2840     afs_uint32 aserver;
2841     afs_int32 apart, vtype, code, err;
2842     int force = 0;
2843     int stayUp = 0;
2844
2845     if (as->parms[1].items)
2846         force = 1;
2847     if (as->parms[2].items)
2848         stayUp = 1;
2849     avolid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
2850     if (avolid == 0) {
2851         if (err)
2852             PrintError("", err);
2853         else
2854             fprintf(STDERR, "vos: can't find volume '%s'\n",
2855                     as->parms[0].items->data);
2856         return ENOENT;
2857     }
2858     code = GetVolumeInfo(avolid, &aserver, &apart, &vtype, &entry);
2859     if (code)
2860         return code;
2861
2862     if (vtype != RWVOL) {
2863         fprintf(STDERR, "%s not a RW volume\n", as->parms[0].items->data);
2864         return (ENOENT);
2865     }
2866
2867     if (!ISNAMEVALID(entry.name)) {
2868         fprintf(STDERR,
2869                 "Volume name %s is too long, rename before releasing\n",
2870                 entry.name);
2871         return E2BIG;
2872     }
2873
2874     code = UV_ReleaseVolume(avolid, aserver, apart, force, stayUp);
2875
2876     if (code) {
2877         PrintDiagnostics("release", code);
2878         return code;
2879     }
2880     fprintf(STDOUT, "Released volume %s successfully\n",
2881             as->parms[0].items->data);
2882     return 0;
2883 }
2884
2885 static int
2886 DumpVolumeCmd(struct cmd_syndesc *as, void *arock)
2887 {
2888     afs_uint32 avolid;
2889     afs_uint32 aserver;
2890     afs_int32 apart, voltype, fromdate = 0, code, err, i, flags;
2891     char filename[MAXPATHLEN];
2892     struct nvldbentry entry;
2893
2894     rx_SetRxDeadTime(60 * 10);
2895     for (i = 0; i < MAXSERVERS; i++) {
2896         struct rx_connection *rxConn = ubik_GetRPCConn(cstruct, i);
2897         if (rxConn == 0)
2898             break;
2899         rx_SetConnDeadTime(rxConn, rx_connDeadTime);
2900         if (rx_ServiceOf(rxConn))
2901             rx_ServiceOf(rxConn)->connDeadTime = rx_connDeadTime;
2902     }
2903
2904     avolid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
2905     if (avolid == 0) {
2906         if (err)
2907             PrintError("", err);
2908         else
2909             fprintf(STDERR, "vos: can't find volume '%s'\n",
2910                     as->parms[0].items->data);
2911         return ENOENT;
2912     }
2913
2914     if (as->parms[3].items || as->parms[4].items) {
2915         if (!as->parms[3].items || !as->parms[4].items) {
2916             fprintf(STDERR,
2917                     "Must specify both -server and -partition options\n");
2918             return -1;
2919         }
2920         aserver = GetServer(as->parms[3].items->data);
2921         if (aserver == 0) {
2922             fprintf(STDERR, "Invalid server name\n");
2923             return -1;
2924         }
2925         apart = volutil_GetPartitionID(as->parms[4].items->data);
2926         if (apart < 0) {
2927             fprintf(STDERR, "Invalid partition name\n");
2928             return -1;
2929         }
2930     } else {
2931         code = GetVolumeInfo(avolid, &aserver, &apart, &voltype, &entry);
2932         if (code)
2933             return code;
2934     }
2935
2936     if (as->parms[1].items && strcmp(as->parms[1].items->data, "0")) {
2937         code = ktime_DateToInt32(as->parms[1].items->data, &fromdate);
2938         if (code) {
2939             fprintf(STDERR, "vos: failed to parse date '%s' (error=%d))\n",
2940                     as->parms[1].items->data, code);
2941             return code;
2942         }
2943     }
2944     if (as->parms[2].items) {
2945         strcpy(filename, as->parms[2].items->data);
2946     } else {
2947         strcpy(filename, "");
2948     }
2949
2950     flags = as->parms[6].items ? VOLDUMPV2_OMITDIRS : 0;
2951 retry_dump:
2952     if (as->parms[5].items) {
2953         code =
2954             UV_DumpClonedVolume(avolid, aserver, apart, fromdate,
2955                                 DumpFunction, filename, flags);
2956     } else {
2957         code =
2958             UV_DumpVolume(avolid, aserver, apart, fromdate, DumpFunction,
2959                           filename, flags);
2960     }
2961     if ((code == RXGEN_OPCODE) && (as->parms[6].items)) {
2962         flags &= ~VOLDUMPV2_OMITDIRS;
2963         goto retry_dump;
2964     }
2965     if (code) {
2966         PrintDiagnostics("dump", code);
2967         return code;
2968     }
2969     if (strcmp(filename, ""))
2970         fprintf(STDERR, "Dumped volume %s in file %s\n",
2971                 as->parms[0].items->data, filename);
2972     else
2973         fprintf(STDERR, "Dumped volume %s in stdout \n",
2974                 as->parms[0].items->data);
2975     return 0;
2976 }
2977
2978 #define ASK   0
2979 #define ABORT 1
2980 #define FULL  2
2981 #define INC   3
2982
2983 #define TS_DUMP 1
2984 #define TS_KEEP 2
2985 #define TS_NEW  3
2986
2987 static int
2988 RestoreVolumeCmd(struct cmd_syndesc *as, void *arock)
2989 {
2990     afs_uint32 avolid, aparentid;
2991     afs_uint32 aserver;
2992     afs_int32 apart, code, vcode, err;
2993     afs_int32 aoverwrite = ASK;
2994     afs_int32 acreation = 0, alastupdate = 0;
2995     int restoreflags = 0;
2996     int readonly = 0, offline = 0, voltype = RWVOL;
2997     char afilename[MAXPATHLEN], avolname[VOLSER_MAXVOLNAME + 1], apartName[10];
2998     char volname[VOLSER_MAXVOLNAME + 1];
2999     struct nvldbentry entry;
3000
3001     aparentid = 0;
3002     if (as->parms[4].items) {
3003         avolid = vsu_GetVolumeID(as->parms[4].items->data, cstruct, &err);
3004         if (avolid == 0) {
3005             if (err)
3006                 PrintError("", err);
3007             else
3008                 fprintf(STDERR, "vos: can't find volume '%s'\n",
3009                         as->parms[4].items->data);
3010             exit(1);
3011         }
3012     } else
3013         avolid = 0;
3014
3015     if (as->parms[5].items) {
3016         if ((strcmp(as->parms[5].items->data, "a") == 0)
3017             || (strcmp(as->parms[5].items->data, "abort") == 0)) {
3018             aoverwrite = ABORT;
3019         } else if ((strcmp(as->parms[5].items->data, "f") == 0)
3020                    || (strcmp(as->parms[5].items->data, "full") == 0)) {
3021             aoverwrite = FULL;
3022         } else if ((strcmp(as->parms[5].items->data, "i") == 0)
3023                    || (strcmp(as->parms[5].items->data, "inc") == 0)
3024                    || (strcmp(as->parms[5].items->data, "increment") == 0)
3025                    || (strcmp(as->parms[5].items->data, "incremental") == 0)) {
3026             aoverwrite = INC;
3027         } else {
3028             fprintf(STDERR, "vos: %s is not a valid argument to -overwrite\n",
3029                     as->parms[5].items->data);
3030             exit(1);
3031         }
3032     }
3033     if (as->parms[6].items)
3034         offline = 1;
3035     if (as->parms[7].items) {
3036         readonly = 1;
3037         voltype = ROVOL;
3038     }
3039
3040     if (as->parms[8].items) {
3041         if ((strcmp(as->parms[8].items->data, "d") == 0)
3042             || (strcmp(as->parms[8].items->data, "dump") == 0)) {
3043             acreation = TS_DUMP;
3044         } else if ((strcmp(as->parms[8].items->data, "k") == 0)
3045             || (strcmp(as->parms[8].items->data, "keep") == 0)) {
3046             acreation = TS_KEEP;
3047         } else if ((strcmp(as->parms[8].items->data, "n") == 0)
3048             || (strcmp(as->parms[8].items->data, "new") == 0)) {
3049             acreation = TS_NEW;
3050         } else {
3051             fprintf(STDERR, "vos: %s is not a valid argument to -creation\n",
3052                     as->parms[8].items->data);
3053             exit(1);
3054         }
3055     }
3056
3057     if (as->parms[9].items) {
3058         if ((strcmp(as->parms[9].items->data, "d") == 0)
3059             || (strcmp(as->parms[9].items->data, "dump") == 0)) {
3060             alastupdate = TS_DUMP;
3061         } else if ((strcmp(as->parms[9].items->data, "k") == 0)
3062             || (strcmp(as->parms[9].items->data, "keep") == 0)) {
3063             alastupdate = TS_KEEP;
3064         } else if ((strcmp(as->parms[9].items->data, "n") == 0)
3065             || (strcmp(as->parms[9].items->data, "new") == 0)) {
3066             alastupdate = TS_NEW;
3067         } else {
3068             fprintf(STDERR, "vos: %s is not a valid argument to -lastupdate\n",
3069                     as->parms[9].items->data);
3070             exit(1);
3071         }
3072     }
3073
3074     aserver = GetServer(as->parms[0].items->data);
3075     if (aserver == 0) {
3076         fprintf(STDERR, "vos: server '%s' not found in host table\n",
3077                 as->parms[0].items->data);
3078         exit(1);
3079     }
3080     apart = volutil_GetPartitionID(as->parms[1].items->data);
3081     if (apart < 0) {
3082         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3083                 as->parms[1].items->data);
3084         exit(1);
3085     }
3086     if (!IsPartValid(apart, aserver, &code)) {  /*check for validity of the partition */
3087         if (code)
3088             PrintError("", code);
3089         else
3090             fprintf(STDERR,
3091                     "vos : partition %s does not exist on the server\n",
3092                     as->parms[1].items->data);
3093         exit(1);
3094     }
3095     strcpy(avolname, as->parms[2].items->data);
3096     if (!ISNAMEVALID(avolname)) {
3097         fprintf(STDERR,
3098                 "vos: the name of the volume %s exceeds the size limit\n",
3099                 avolname);
3100         exit(1);
3101     }
3102     if (!VolNameOK(avolname)) {
3103         fprintf(STDERR,
3104                 "Illegal volume name %s, should not end in .readonly or .backup\n",
3105                 avolname);
3106         exit(1);
3107     }
3108     if (as->parms[3].items) {
3109         strcpy(afilename, as->parms[3].items->data);
3110         if (!FileExists(afilename)) {
3111             fprintf(STDERR, "Can't access file %s\n", afilename);
3112             exit(1);
3113         }
3114     } else {
3115         strcpy(afilename, "");
3116     }
3117
3118     /* Check if volume exists or not */
3119
3120     vsu_ExtractName(volname, avolname);
3121     vcode = VLDB_GetEntryByName(volname, &entry);
3122     if (vcode) {                /* no volume - do a full restore */
3123         restoreflags = RV_FULLRST;
3124         if ((aoverwrite == INC) || (aoverwrite == ABORT))
3125             fprintf(STDERR,
3126                     "Volume does not exist; Will perform a full restore\n");
3127     }
3128
3129     else if ((!readonly && Lp_GetRwIndex(&entry) == -1) /* RW volume does not exist - do a full */
3130              ||(readonly && !Lp_ROMatch(0, 0, &entry))) {       /* RO volume does not exist - do a full */
3131         restoreflags = RV_FULLRST;
3132         if ((aoverwrite == INC) || (aoverwrite == ABORT))
3133             fprintf(STDERR,
3134                     "%s Volume does not exist; Will perform a full restore\n",
3135                     readonly ? "RO" : "RW");
3136
3137         if (avolid == 0) {
3138             avolid = entry.volumeId[voltype];
3139         } else if (entry.volumeId[voltype] != 0
3140                    && entry.volumeId[voltype] != avolid) {
3141             avolid = entry.volumeId[voltype];
3142         }
3143         aparentid = entry.volumeId[RWVOL];
3144     }
3145
3146     else {                      /* volume exists - do we do a full incremental or abort */
3147         afs_uint32 Oserver;
3148         afs_int32 Opart, Otype, vol_elsewhere = 0;
3149         struct nvldbentry Oentry;
3150         int c, dc;
3151
3152         if (avolid == 0) {
3153             avolid = entry.volumeId[voltype];
3154         } else if (entry.volumeId[voltype] != 0
3155                    && entry.volumeId[voltype] != avolid) {
3156             avolid = entry.volumeId[voltype];
3157         }
3158         aparentid = entry.volumeId[RWVOL];
3159
3160         /* A file name was specified  - check if volume is on another partition */
3161         vcode = GetVolumeInfo(avolid, &Oserver, &Opart, &Otype, &Oentry);
3162         if (vcode)
3163             exit(1);
3164
3165         vcode = VLDB_IsSameAddrs(Oserver, aserver, &err);
3166         if (err) {
3167             fprintf(STDERR,
3168                     "Failed to get info about server's %d address(es) from vlserver (err=%d); aborting call!\n",
3169                     Oserver, err);
3170             exit(1);
3171         }
3172         if (!vcode || (Opart != apart))
3173             vol_elsewhere = 1;
3174
3175         if (aoverwrite == ASK) {
3176             if (strcmp(afilename, "") == 0) {   /* The file is from standard in */
3177                 fprintf(STDERR,
3178                         "Volume exists and no -overwrite option specified; Aborting restore command\n");
3179                 exit(1);
3180             }
3181
3182             /* Ask what to do */
3183             if (vol_elsewhere) {
3184                 fprintf(STDERR,
3185                         "The volume %s %u already exists on a different server/part\n",
3186                         volname, entry.volumeId[voltype]);
3187                 fprintf(STDERR,
3188                         "Do you want to do a full restore or abort? [fa](a): ");
3189             } else {
3190                 fprintf(STDERR,
3191                         "The volume %s %u already exists in the VLDB\n",
3192                         volname, entry.volumeId[voltype]);
3193                 fprintf(STDERR,
3194                         "Do you want to do a full/incremental restore or abort? [fia](a): ");
3195             }
3196             dc = c = getchar();
3197             while (!(dc == EOF || dc == '\n'))
3198                 dc = getchar(); /* goto end of line */
3199             if ((c == 'f') || (c == 'F'))
3200                 aoverwrite = FULL;
3201             else if ((c == 'i') || (c == 'I'))
3202                 aoverwrite = INC;
3203             else
3204                 aoverwrite = ABORT;
3205         }
3206
3207         if (aoverwrite == ABORT) {
3208             fprintf(STDERR, "Volume exists; Aborting restore command\n");
3209             exit(1);
3210         } else if (aoverwrite == FULL) {
3211             restoreflags = RV_FULLRST;
3212             fprintf(STDERR,
3213                     "Volume exists; Will delete and perform full restore\n");
3214         } else if (aoverwrite == INC) {
3215             restoreflags = 0;
3216             if (vol_elsewhere) {
3217                 fprintf(STDERR,
3218                         "%s volume %lu already exists on a different server/part; not allowed\n",
3219                         readonly ? "RO" : "RW", (unsigned long)avolid);
3220                 exit(1);
3221             }
3222         }
3223     }
3224     if (offline)
3225         restoreflags |= RV_OFFLINE;
3226     if (readonly)
3227         restoreflags |= RV_RDONLY;
3228
3229     switch (acreation) {
3230         case TS_DUMP:
3231             restoreflags |= RV_CRDUMP;
3232             break;
3233         case TS_KEEP:
3234             restoreflags |= RV_CRKEEP;
3235             break;
3236         case TS_NEW:
3237             restoreflags |= RV_CRNEW;
3238             break;
3239         default:
3240             if (aoverwrite == FULL)
3241                 restoreflags |= RV_CRNEW;
3242             else
3243                 restoreflags |= RV_CRKEEP;
3244     }
3245
3246     switch (alastupdate) {
3247         case TS_DUMP:
3248             restoreflags |= RV_LUDUMP;
3249             break;
3250         case TS_KEEP:
3251             restoreflags |= RV_LUKEEP;
3252             break;
3253         case TS_NEW:
3254             restoreflags |= RV_LUNEW;
3255             break;
3256         default:
3257             restoreflags |= RV_LUDUMP;
3258     }
3259     if (as->parms[10].items) {
3260         restoreflags |= RV_NODEL;
3261     }
3262
3263
3264     code =
3265         UV_RestoreVolume2(aserver, apart, avolid, aparentid,
3266                           avolname, restoreflags, WriteData, afilename);
3267     if (code) {
3268         PrintDiagnostics("restore", code);
3269         exit(1);
3270     }
3271     MapPartIdIntoName(apart, apartName);
3272
3273     /*
3274      * patch typo here - originally "parms[1]", should be "parms[0]"
3275      */
3276
3277     fprintf(STDOUT, "Restored volume %s on %s %s\n", avolname,
3278             as->parms[0].items->data, apartName);
3279     return 0;
3280 }
3281
3282 static int
3283 LockReleaseCmd(struct cmd_syndesc *as, void *arock)
3284 {
3285     afs_uint32 avolid;
3286     afs_int32 code, err;
3287
3288     avolid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
3289     if (avolid == 0) {
3290         if (err)
3291             PrintError("", err);
3292         else
3293             fprintf(STDERR, "vos: can't find volume '%s'\n",
3294                     as->parms[0].items->data);
3295         exit(1);
3296     }
3297
3298     code = UV_LockRelease(avolid);
3299     if (code) {
3300         PrintDiagnostics("unlock", code);
3301         exit(1);
3302     }
3303     fprintf(STDOUT, "Released lock on vldb entry for volume %s\n",
3304             as->parms[0].items->data);
3305     return 0;
3306 }
3307
3308 static int
3309 AddSite(struct cmd_syndesc *as, void *arock)
3310 {
3311     afs_uint32 avolid;
3312     afs_uint32 aserver;
3313     afs_int32 apart, code, err, arovolid, valid = 0;
3314     char apartName[10], avolname[VOLSER_MAXVOLNAME + 1];
3315
3316     vsu_ExtractName(avolname, as->parms[2].items->data);;
3317     avolid = vsu_GetVolumeID(avolname, cstruct, &err);
3318     if (avolid == 0) {
3319         if (err)
3320             PrintError("", err);
3321         else
3322             fprintf(STDERR, "vos: can't find volume '%s'\n",
3323                     as->parms[2].items->data);
3324         exit(1);
3325     }
3326     arovolid = 0;
3327     if (as->parms[3].items) {
3328         vsu_ExtractName(avolname, as->parms[3].items->data);
3329         arovolid = vsu_GetVolumeID(avolname, cstruct, &err);
3330         if (!arovolid) {
3331             fprintf(STDERR, "vos: invalid ro volume id '%s'\n",
3332                     as->parms[3].items->data);
3333             exit(1);
3334         }
3335     }
3336     aserver = GetServer(as->parms[0].items->data);
3337     if (aserver == 0) {
3338         fprintf(STDERR, "vos: server '%s' not found in host table\n",
3339                 as->parms[0].items->data);
3340         exit(1);
3341     }
3342     apart = volutil_GetPartitionID(as->parms[1].items->data);
3343     if (apart < 0) {
3344         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3345                 as->parms[1].items->data);
3346         exit(1);
3347     }
3348     if (!IsPartValid(apart, aserver, &code)) {  /*check for validity of the partition */
3349         if (code)
3350             PrintError("", code);
3351         else
3352             fprintf(STDERR,
3353                     "vos : partition %s does not exist on the server\n",
3354                     as->parms[1].items->data);
3355         exit(1);
3356     }
3357     if (as->parms[4].items) {
3358         valid = 1;
3359     }
3360     code = UV_AddSite2(aserver, apart, avolid, arovolid, valid);
3361     if (code) {
3362         PrintDiagnostics("addsite", code);
3363         exit(1);
3364     }
3365     MapPartIdIntoName(apart, apartName);
3366     fprintf(STDOUT, "Added replication site %s %s for volume %s\n",
3367             as->parms[0].items->data, apartName, as->parms[2].items->data);
3368     return 0;
3369 }
3370
3371 static int
3372 RemoveSite(struct cmd_syndesc *as, void *arock)
3373 {
3374
3375     afs_uint32 avolid;
3376     afs_uint32 aserver;
3377     afs_int32 apart, code, err;
3378     char apartName[10], avolname[VOLSER_MAXVOLNAME + 1];
3379
3380     vsu_ExtractName(avolname, as->parms[2].items->data);
3381     avolid = vsu_GetVolumeID(avolname, cstruct, &err);
3382     if (avolid == 0) {
3383         if (err)
3384             PrintError("", err);
3385         else
3386             fprintf(STDERR, "vos: can't find volume '%s'\n",
3387                     as->parms[2].items->data);
3388         exit(1);
3389     }
3390     aserver = GetServer(as->parms[0].items->data);
3391     if (aserver == 0) {
3392         fprintf(STDERR, "vos: server '%s' not found in host table\n",
3393                 as->parms[0].items->data);
3394         exit(1);
3395     }
3396     apart = volutil_GetPartitionID(as->parms[1].items->data);
3397     if (apart < 0) {
3398         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3399                 as->parms[1].items->data);
3400         exit(1);
3401     }
3402 /*
3403  *skip the partition validity check, since it is possible that the partition
3404  *has since been decomissioned.
3405  */
3406 /*
3407         if (!IsPartValid(apart,aserver,&code)){
3408             if(code) PrintError("",code);
3409             else fprintf(STDERR,"vos : partition %s does not exist on the server\n",as->parms[1].items->data);
3410             exit(1);
3411         }
3412 */
3413     code = UV_RemoveSite(aserver, apart, avolid);
3414     if (code) {
3415         PrintDiagnostics("remsite", code);
3416         exit(1);
3417     }
3418     MapPartIdIntoName(apart, apartName);
3419     fprintf(STDOUT, "Removed replication site %s %s for volume %s\n",
3420             as->parms[0].items->data, apartName, as->parms[2].items->data);
3421     return 0;
3422 }
3423
3424 static int
3425 ChangeLocation(struct cmd_syndesc *as, void *arock)
3426 {
3427     afs_uint32 avolid;
3428     afs_uint32 aserver;
3429     afs_int32 apart, code, err;
3430     char apartName[10];
3431
3432     avolid = vsu_GetVolumeID(as->parms[2].items->data, cstruct, &err);
3433     if (avolid == 0) {
3434         if (err)
3435             PrintError("", err);
3436         else
3437             fprintf(STDERR, "vos: can't find volume '%s'\n",
3438                     as->parms[2].items->data);
3439         exit(1);
3440     }
3441     aserver = GetServer(as->parms[0].items->data);
3442     if (aserver == 0) {
3443         fprintf(STDERR, "vos: server '%s' not found in host table\n",
3444                 as->parms[0].items->data);
3445         exit(1);
3446     }
3447     apart = volutil_GetPartitionID(as->parms[1].items->data);
3448     if (apart < 0) {
3449         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3450                 as->parms[1].items->data);
3451         exit(1);
3452     }
3453     if (!IsPartValid(apart, aserver, &code)) {  /*check for validity of the partition */
3454         if (code)
3455             PrintError("", code);
3456         else
3457             fprintf(STDERR,
3458                     "vos : partition %s does not exist on the server\n",
3459                     as->parms[1].items->data);
3460         exit(1);
3461     }
3462     code = UV_ChangeLocation(aserver, apart, avolid);
3463     if (code) {
3464         PrintDiagnostics("changeloc", code);
3465         exit(1);
3466     }
3467     MapPartIdIntoName(apart, apartName);
3468     fprintf(STDOUT, "Changed location to %s %s for volume %s\n",
3469             as->parms[0].items->data, apartName, as->parms[2].items->data);
3470     return 0;
3471 }
3472
3473 static int
3474 ListPartitions(struct cmd_syndesc *as, void *arock)
3475 {
3476     afs_uint32 aserver;
3477     afs_int32 code;
3478     struct partList dummyPartList;
3479     int i;
3480     char pname[10];
3481     int total, cnt;
3482
3483     aserver = GetServer(as->parms[0].items->data);
3484     if (aserver == 0) {
3485         fprintf(STDERR, "vos: server '%s' not found in host table\n",
3486                 as->parms[0].items->data);
3487         exit(1);
3488     }
3489
3490
3491     code = UV_ListPartitions(aserver, &dummyPartList, &cnt);
3492     if (code) {
3493         PrintDiagnostics("listpart", code);
3494         exit(1);
3495     }
3496     total = 0;
3497     fprintf(STDOUT, "The partitions on the server are:\n");
3498     for (i = 0; i < cnt; i++) {
3499         if (dummyPartList.partFlags[i] & PARTVALID) {
3500             memset(pname, 0, sizeof(pname));
3501             MapPartIdIntoName(dummyPartList.partId[i], pname);
3502             fprintf(STDOUT, " %10s ", pname);
3503             total++;
3504             if ((i % 5) == 0 && (i != 0))
3505                 fprintf(STDOUT, "\n");
3506         }
3507     }
3508     fprintf(STDOUT, "\n");
3509     fprintf(STDOUT, "Total: %d\n", total);
3510     return 0;
3511
3512 }
3513
3514 static int
3515 CompareVolName(const void *p1, const void *p2)
3516 {
3517     volintInfo *arg1, *arg2;
3518
3519     arg1 = (volintInfo *) p1;
3520     arg2 = (volintInfo *) p2;
3521     return (strcmp(arg1->name, arg2->name));
3522
3523 }
3524
3525 /*------------------------------------------------------------------------
3526  * PRIVATE XCompareVolName
3527  *
3528  * Description:
3529  *      Comparison routine for volume names coming from an extended
3530  *      volume listing.
3531  *
3532  * Arguments:
3533  *      a_obj1P : Char ptr to first extended vol info object
3534  *      a_obj1P : Char ptr to second extended vol info object
3535  *
3536  * Returns:
3537  *      The value of strcmp() on the volume names within the passed
3538  *      objects (i,e., -1, 0, or 1).
3539  *
3540  * Environment:
3541  *      Passed to qsort() as the designated comparison routine.
3542  *
3543  * Side Effects:
3544  *      As advertised.
3545  *------------------------------------------------------------------------*/
3546
3547 static int
3548 XCompareVolName(const void *a_obj1P, const void *a_obj2P)
3549 {                               /*XCompareVolName */
3550
3551     return (strcmp
3552             (((struct volintXInfo *)(a_obj1P))->name,
3553              ((struct volintXInfo *)(a_obj2P))->name));
3554
3555 }                               /*XCompareVolName */
3556
3557 static int
3558 CompareVolID(const void *p1, const void *p2)
3559 {
3560     volintInfo *arg1, *arg2;
3561
3562     arg1 = (volintInfo *) p1;
3563     arg2 = (volintInfo *) p2;
3564     if (arg1->volid == arg2->volid)
3565         return 0;
3566     if (arg1->volid > arg2->volid)
3567         return 1;
3568     else
3569         return -1;
3570
3571 }
3572
3573 /*------------------------------------------------------------------------
3574  * PRIVATE XCompareVolID
3575  *
3576  * Description:
3577  *      Comparison routine for volume IDs coming from an extended
3578  *      volume listing.
3579  *
3580  * Arguments:
3581  *      a_obj1P : Char ptr to first extended vol info object
3582  *      a_obj1P : Char ptr to second extended vol info object
3583  *
3584  * Returns:
3585  *      The value of strcmp() on the volume names within the passed
3586  *      objects (i,e., -1, 0, or 1).
3587  *
3588  * Environment:
3589  *      Passed to qsort() as the designated comparison routine.
3590  *
3591  * Side Effects:
3592  *      As advertised.
3593  *------------------------------------------------------------------------*/
3594
3595 static int
3596 XCompareVolID(const void *a_obj1P, const void *a_obj2P)
3597 {                               /*XCompareVolID */
3598
3599     afs_int32 id1, id2;         /*Volume IDs we're comparing */
3600
3601     id1 = ((struct volintXInfo *)(a_obj1P))->volid;
3602     id2 = ((struct volintXInfo *)(a_obj2P))->volid;
3603     if (id1 == id2)
3604         return (0);
3605     else if (id1 > id2)
3606         return (1);
3607     else
3608         return (-1);
3609
3610 }                               /*XCompareVolID */
3611
3612 /*------------------------------------------------------------------------
3613  * PRIVATE ListVolumes
3614  *
3615  * Description:
3616  *      Routine used to list volumes, contacting the Volume Server
3617  *      directly, bypassing the VLDB.
3618  *
3619  * Arguments:
3620  *      as : Ptr to parsed command line arguments.
3621  *
3622  * Returns:
3623  *      0                       Successful operation
3624  *
3625  * Environment:
3626  *      Nothing interesting.
3627  *
3628  * Side Effects:
3629  *      As advertised.
3630  *------------------------------------------------------------------------*/
3631
3632 static int
3633 ListVolumes(struct cmd_syndesc *as, void *arock)
3634 {
3635     afs_int32 apart, int32list, fast;
3636     afs_uint32 aserver;
3637     afs_int32 code;
3638     volintInfo *pntr;
3639     volintInfo *oldpntr = NULL;
3640     afs_int32 count;
3641     int i;
3642     char *base;
3643     volintXInfo *xInfoP;
3644     volintXInfo *origxInfoP = NULL; /*Ptr to current/orig extended vol info */
3645     int wantExtendedInfo;       /*Do we want extended vol info? */
3646
3647     char pname[10];
3648     struct partList dummyPartList;
3649     int all;
3650     int quiet, cnt;
3651
3652     apart = -1;
3653     fast = 0;
3654     int32list = 0;
3655
3656     if (as->parms[3].items)
3657         int32list = 1;
3658     if (as->parms[4].items)
3659         quiet = 1;
3660     else
3661         quiet = 0;
3662     if (as->parms[2].items)
3663         fast = 1;
3664     if (fast)
3665         all = 0;
3666     else
3667         all = 1;
3668     if (as->parms[5].items) {
3669         /*
3670          * We can't coexist with the fast flag.
3671          */
3672         if (fast) {
3673             fprintf(STDERR,
3674                     "vos: Can't use the -fast and -extended flags together\n");
3675             exit(1);
3676         }
3677
3678         /*
3679          * We need to turn on ``long'' listings to get the full effect.
3680          */
3681         wantExtendedInfo = 1;
3682         int32list = 1;
3683     } else
3684         wantExtendedInfo = 0;
3685     if (as->parms[1].items) {
3686         apart = volutil_GetPartitionID(as->parms[1].items->data);
3687         if (apart < 0) {
3688             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3689                     as->parms[1].items->data);
3690             exit(1);
3691         }
3692         dummyPartList.partId[0] = apart;
3693         dummyPartList.partFlags[0] = PARTVALID;
3694         cnt = 1;
3695     }
3696     aserver = GetServer(as->parms[0].items->data);
3697     if (aserver == 0) {
3698         fprintf(STDERR, "vos: server '%s' not found in host table\n",
3699                 as->parms[0].items->data);
3700         exit(1);
3701     }
3702
3703     if (apart != -1) {
3704         if (!IsPartValid(apart, aserver, &code)) {      /*check for validity of the partition */
3705             if (code)
3706                 PrintError("", code);
3707             else
3708                 fprintf(STDERR,
3709                         "vos : partition %s does not exist on the server\n",
3710                         as->parms[1].items->data);
3711             exit(1);
3712         }
3713     } else {
3714         code = UV_ListPartitions(aserver, &dummyPartList, &cnt);
3715         if (code) {
3716             PrintDiagnostics("listvol", code);
3717             exit(1);
3718         }
3719     }
3720     for (i = 0; i < cnt; i++) {
3721         if (dummyPartList.partFlags[i] & PARTVALID) {
3722             if (wantExtendedInfo)
3723                 code =
3724                     UV_XListVolumes(aserver, dummyPartList.partId[i], all,
3725                                     &xInfoP, &count);
3726             else
3727                 code =
3728                     UV_ListVolumes(aserver, dummyPartList.partId[i], all,
3729                                    &pntr, &count);
3730             if (code) {
3731                 PrintDiagnostics("listvol", code);
3732                 exit(1);
3733             }
3734             if (wantExtendedInfo) {
3735                 origxInfoP = xInfoP;
3736                 base = (char *)xInfoP;
3737             } else {
3738                 oldpntr = pntr;
3739                 base = (char *)pntr;
3740             }
3741
3742             if (!fast) {
3743                 if (wantExtendedInfo)
3744                     qsort(base, count, sizeof(volintXInfo), XCompareVolName);
3745                 else
3746                     qsort(base, count, sizeof(volintInfo), CompareVolName);
3747             } else {
3748                 if (wantExtendedInfo)
3749                     qsort(base, count, sizeof(volintXInfo), XCompareVolID);
3750                 else
3751                     qsort(base, count, sizeof(volintInfo), CompareVolID);
3752             }
3753             MapPartIdIntoName(dummyPartList.partId[i], pname);
3754             if (!quiet)
3755                 fprintf(STDOUT,
3756                         "Total number of volumes on server %s partition %s: %lu \n",
3757                         as->parms[0].items->data, pname,
3758                         (unsigned long)count);
3759             if (wantExtendedInfo) {
3760                 if (as->parms[6].items)
3761                     XDisplayVolumes2(aserver, dummyPartList.partId[i], origxInfoP,
3762                                 count, int32list, fast, quiet);
3763                 else
3764                     XDisplayVolumes(aserver, dummyPartList.partId[i], origxInfoP,
3765                                 count, int32list, fast, quiet);
3766                 if (xInfoP)
3767                     free(xInfoP);
3768                 xInfoP = (volintXInfo *) 0;
3769             } else {
3770                 if (as->parms[6].items)
3771                     DisplayVolumes2(aserver, dummyPartList.partId[i], oldpntr,
3772                                     count);
3773                 else
3774                     DisplayVolumes(aserver, dummyPartList.partId[i], oldpntr,
3775                                    count, int32list, fast, quiet);
3776                 if (pntr)
3777                     free(pntr);
3778                 pntr = (volintInfo *) 0;
3779             }
3780         }
3781     }
3782     return 0;
3783 }
3784
3785 static int
3786 SyncVldb(struct cmd_syndesc *as, void *arock)
3787 {
3788     afs_int32 pnum = 0, code;   /* part name */
3789     char part[10];
3790     int flags = 0;
3791     char *volname = 0;
3792     afs_uint32 tserver;
3793
3794     tserver = 0;
3795     if (as->parms[0].items) {
3796         tserver = GetServer(as->parms[0].items->data);
3797         if (!tserver) {
3798             fprintf(STDERR, "vos: host '%s' not found in host table\n",
3799                     as->parms[0].items->data);
3800             exit(1);
3801         }
3802     }
3803
3804     if (as->parms[1].items) {
3805         pnum = volutil_GetPartitionID(as->parms[1].items->data);
3806         if (pnum < 0) {
3807             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3808                     as->parms[1].items->data);
3809             exit(1);
3810         }
3811         if (!IsPartValid(pnum, tserver, &code)) {       /*check for validity of the partition */
3812             if (code)
3813                 PrintError("", code);
3814             else
3815                 fprintf(STDERR,
3816                         "vos: partition %s does not exist on the server\n",
3817                         as->parms[1].items->data);
3818             exit(1);
3819         }
3820         flags = 1;
3821
3822         if (!tserver) {
3823             fprintf(STDERR,
3824                     "The -partition option requires a -server option\n");
3825             exit(1);
3826         }
3827     }
3828
3829     if (as->parms[3].items) {
3830         flags |= 2; /* don't update */
3831     }
3832
3833     if (as->parms[2].items) {
3834         /* Synchronize an individual volume */
3835         volname = as->parms[2].items->data;
3836         code = UV_SyncVolume(tserver, pnum, volname, flags);
3837     } else {
3838         if (!tserver) {
3839             fprintf(STDERR,
3840                     "Without a -volume option, the -server option is required\n");
3841             exit(1);
3842         }
3843         code = UV_SyncVldb(tserver, pnum, flags, 0 /*unused */ );
3844     }
3845
3846     if (code) {
3847         PrintDiagnostics("syncvldb", code);
3848         exit(1);
3849     }
3850
3851     /* Print a summary of what we did */
3852     if (volname)
3853         fprintf(STDOUT, "VLDB volume %s synchronized", volname);
3854     else
3855         fprintf(STDOUT, "VLDB synchronized");
3856     if (tserver) {
3857         fprintf(STDOUT, " with state of server %s", as->parms[0].items->data);
3858     }
3859     if (flags & 1) {
3860         MapPartIdIntoName(pnum, part);
3861         fprintf(STDOUT, " partition %s\n", part);
3862     }
3863     fprintf(STDOUT, "\n");
3864
3865     return 0;
3866 }
3867
3868 static int
3869 SyncServer(struct cmd_syndesc *as, void *arock)
3870 {
3871     afs_int32 pnum, code;       /* part name */
3872     char part[10];
3873     afs_uint32 tserver;
3874
3875     int flags = 0;
3876
3877     tserver = GetServer(as->parms[0].items->data);
3878     if (!tserver) {
3879         fprintf(STDERR, "vos: host '%s' not found in host table\n",
3880                 as->parms[0].items->data);
3881         exit(1);
3882     }
3883     if (as->parms[1].items) {
3884         pnum = volutil_GetPartitionID(as->parms[1].items->data);
3885         if (pnum < 0) {
3886             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3887                     as->parms[1].items->data);
3888             exit(1);
3889         }
3890         if (!IsPartValid(pnum, tserver, &code)) {       /*check for validity of the partition */
3891             if (code)
3892                 PrintError("", code);
3893             else
3894                 fprintf(STDERR,
3895                         "vos : partition %s does not exist on the server\n",
3896                         as->parms[1].items->data);
3897             exit(1);
3898         }
3899         flags = 1;
3900     } else {
3901         pnum = -1;
3902     }
3903
3904     if (as->parms[2].items) {
3905         flags |= 2; /* don't update */
3906     }
3907     code = UV_SyncServer(tserver, pnum, flags, 0 /*unused */ );
3908     if (code) {
3909         PrintDiagnostics("syncserv", code);
3910         exit(1);
3911     }
3912     if (flags & 1) {
3913         MapPartIdIntoName(pnum, part);
3914         fprintf(STDOUT, "Server %s partition %s synchronized with VLDB\n",
3915                 as->parms[0].items->data, part);
3916     } else
3917         fprintf(STDOUT, "Server %s synchronized with VLDB\n",
3918                 as->parms[0].items->data);
3919     return 0;
3920
3921 }
3922
3923 static int
3924 VolumeInfoCmd(char *name)
3925 {
3926     struct nvldbentry entry;
3927     afs_int32 vcode;
3928
3929     /* The vlserver will handle names with the .readonly
3930      * and .backup extension as well as volume ids.
3931      */
3932     vcode = VLDB_GetEntryByName(name, &entry);
3933     if (vcode) {
3934         PrintError("", vcode);
3935         exit(1);
3936     }
3937     MapHostToNetwork(&entry);
3938     EnumerateEntry(&entry);
3939
3940     /* Defect #3027: grubby check to handle locked volume.
3941      * If VLOP_ALLOPERS is set, the entry is locked.
3942      * Leave this routine as is, but put in correct check.
3943      */
3944     PrintLocked(entry.flags);
3945
3946     return 0;
3947 }
3948
3949 static int
3950 VolumeZap(struct cmd_syndesc *as, void *arock)
3951 {
3952     struct nvldbentry entry;
3953     afs_uint32 volid, zapbackupid = 0, backupid = 0;
3954     afs_int32 code, server, part, err;
3955
3956     if (as->parms[3].items) {
3957         /* force flag is on, use the other version */
3958         return NukeVolume(as);
3959     }
3960
3961     if (as->parms[4].items) {
3962         zapbackupid = 1;
3963     }
3964
3965     volid = vsu_GetVolumeID(as->parms[2].items->data, cstruct, &err);
3966     if (volid == 0) {
3967         if (err)
3968             PrintError("", err);
3969         else
3970             fprintf(STDERR, "vos: can't find volume '%s'\n",
3971                     as->parms[2].items->data);
3972         exit(1);
3973     }
3974     part = volutil_GetPartitionID(as->parms[1].items->data);
3975     if (part < 0) {
3976         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3977                 as->parms[1].items->data);
3978         exit(1);
3979     }
3980     server = GetServer(as->parms[0].items->data);
3981     if (!server) {
3982         fprintf(STDERR, "vos: host '%s' not found in host table\n",
3983                 as->parms[0].items->data);
3984         exit(1);
3985     }
3986     if (!IsPartValid(part, server, &code)) {    /*check for validity of the partition */
3987         if (code)
3988             PrintError("", code);
3989         else
3990             fprintf(STDERR,
3991                     "vos : partition %s does not exist on the server\n",
3992                     as->parms[1].items->data);
3993         exit(1);
3994     }
3995     code = VLDB_GetEntryByID(volid, -1, &entry);
3996     if (!code) {
3997         if (volid == entry.volumeId[RWVOL])
3998             backupid = entry.volumeId[BACKVOL];
3999         fprintf(STDERR,
4000                 "Warning: Entry for volume number %lu exists in VLDB (but we're zapping it anyway!)\n",
4001                 (unsigned long)volid);
4002     }
4003     if (zapbackupid) {
4004         volintInfo *pntr = (volintInfo *) 0;
4005
4006         if (!backupid) {
4007             code = UV_ListOneVolume(server, part, volid, &pntr);
4008             if (!code) {
4009                 if (volid == pntr->parentID)
4010                     backupid = pntr->backupID;
4011                 if (pntr)
4012                     free(pntr);
4013             }
4014         }
4015         if (backupid) {
4016             code = UV_VolumeZap(server, part, backupid);
4017             if (code) {
4018                 PrintDiagnostics("zap", code);
4019                 exit(1);
4020             }
4021             fprintf(STDOUT, "Backup Volume %lu deleted\n",
4022                     (unsigned long)backupid);
4023         }
4024     }
4025     code = UV_VolumeZap(server, part, volid);
4026     if (code) {
4027         PrintDiagnostics("zap", code);
4028         exit(1);
4029     }
4030     fprintf(STDOUT, "Volume %lu deleted\n", (unsigned long)volid);
4031
4032     return 0;
4033 }
4034
4035 static int
4036 VolserStatus(struct cmd_syndesc *as, void *arock)
4037 {
4038     afs_uint32 server;
4039     afs_int32 code;
4040     transDebugInfo *pntr, *oldpntr;
4041     afs_int32 count;
4042     int i;
4043     char pname[10];
4044     time_t t;
4045
4046     server = GetServer(as->parms[0].items->data);
4047     if (!server) {
4048         fprintf(STDERR, "vos: host '%s' not found in host table\n",
4049                 as->parms[0].items->data);
4050         exit(1);
4051     }
4052     code = UV_VolserStatus(server, &pntr, &count);
4053     if (code) {
4054         PrintDiagnostics("status", code);
4055         exit(1);
4056     }
4057     oldpntr = pntr;
4058     if (count == 0)
4059         fprintf(STDOUT, "No active transactions on %s\n",
4060                 as->parms[0].items->data);
4061     else {
4062         fprintf(STDOUT, "Total transactions: %d\n", count);
4063     }
4064     for (i = 0; i < count; i++) {
4065         /*print out the relevant info */
4066         fprintf(STDOUT, "--------------------------------------\n");
4067         t = pntr->creationTime;
4068         fprintf(STDOUT, "transaction: %lu  created: %s",
4069                 (unsigned long)pntr->tid, ctime(&t));
4070         t = pntr->time;
4071         fprintf(STDOUT, "lastActiveTime: %s", ctime(&t));
4072         if (pntr->returnCode) {
4073             fprintf(STDOUT, "returnCode: %lu\n",
4074                     (unsigned long)pntr->returnCode);
4075         }
4076         if (pntr->iflags) {
4077             fprintf(STDOUT, "attachFlags:  ");
4078             switch (pntr->iflags) {
4079             case ITOffline:
4080                 fprintf(STDOUT, "offline ");
4081                 break;
4082             case ITBusy:
4083                 fprintf(STDOUT, "busy ");
4084                 break;
4085             case ITReadOnly:
4086                 fprintf(STDOUT, "readonly ");
4087                 break;
4088             case ITCreate:
4089                 fprintf(STDOUT, "create ");
4090                 break;
4091             case ITCreateVolID:
4092                 fprintf(STDOUT, "create volid ");
4093                 break;
4094             }
4095             fprintf(STDOUT, "\n");
4096         }
4097         if (pntr->vflags) {
4098             fprintf(STDOUT, "volumeStatus: ");
4099             switch (pntr->vflags) {
4100             case VTDeleteOnSalvage:
4101                 fprintf(STDOUT, "deleteOnSalvage ");
4102             case VTOutOfService:
4103                 fprintf(STDOUT, "outOfService ");
4104             case VTDeleted:
4105                 fprintf(STDOUT, "deleted ");
4106             }
4107             fprintf(STDOUT, "\n");
4108         }
4109         if (pntr->tflags) {
4110             fprintf(STDOUT, "transactionFlags: ");
4111             fprintf(STDOUT, "delete\n");
4112         }
4113         MapPartIdIntoName(pntr->partition, pname);
4114         fprintf(STDOUT, "volume: %lu  partition: %s  procedure: %s\n",
4115                 (unsigned long)pntr->volid, pname, pntr->lastProcName);
4116         if (pntr->callValid) {
4117             t = pntr->lastReceiveTime;
4118             fprintf(STDOUT, "packetRead: %lu  lastReceiveTime: %s",
4119                     (unsigned long)pntr->readNext, ctime(&t));
4120             t = pntr->lastSendTime;
4121             fprintf(STDOUT, "packetSend: %lu  lastSendTime: %s",
4122                     (unsigned long)pntr->transmitNext, ctime(&t));
4123         }
4124         pntr++;
4125         fprintf(STDOUT, "--------------------------------------\n");
4126         fprintf(STDOUT, "\n");
4127     }
4128     if (oldpntr)
4129         free(oldpntr);
4130     return 0;
4131 }
4132
4133 static int
4134 RenameVolume(struct cmd_syndesc *as, void *arock)
4135 {
4136     afs_int32 code1, code2, code;
4137     struct nvldbentry entry;
4138
4139     code1 = VLDB_GetEntryByName(as->parms[0].items->data, &entry);
4140     if (code1) {
4141         fprintf(STDERR, "vos: Could not find entry for volume %s\n",
4142                 as->parms[0].items->data);
4143         exit(1);
4144     }
4145     code2 = VLDB_GetEntryByName(as->parms[1].items->data, &entry);
4146     if ((!code1) && (!code2)) { /*the newname already exists */
4147         fprintf(STDERR, "vos: volume %s already exists\n",
4148                 as->parms[1].items->data);
4149         exit(1);
4150     }
4151
4152     if (code1 && code2) {
4153         fprintf(STDERR, "vos: Could not find entry for volume %s or %s\n",
4154                 as->parms[0].items->data, as->parms[1].items->data);
4155         exit(1);
4156     }
4157     if (!VolNameOK(as->parms[0].items->data)) {
4158         fprintf(STDERR,
4159                 "Illegal volume name %s, should not end in .readonly or .backup\n",
4160                 as->parms[0].items->data);
4161         exit(1);
4162     }
4163     if (!ISNAMEVALID(as->parms[1].items->data)) {
4164         fprintf(STDERR,
4165                 "vos: the new volume name %s exceeds the size limit of %d\n",
4166                 as->parms[1].items->data, VOLSER_OLDMAXVOLNAME - 10);
4167         exit(1);
4168     }
4169     if (!VolNameOK(as->parms[1].items->data)) {
4170         fprintf(STDERR,
4171                 "Illegal volume name %s, should not end in .readonly or .backup\n",
4172                 as->parms[1].items->data);
4173         exit(1);
4174     }
4175     if (IsNumeric(as->parms[1].items->data)) {
4176         fprintf(STDERR, "Illegal volume name %s, should not be a number\n",
4177                 as->parms[1].items->data);
4178         exit(1);
4179     }
4180     MapHostToNetwork(&entry);
4181     code =
4182         UV_RenameVolume(&entry, as->parms[0].items->data,
4183                         as->parms[1].items->data);
4184     if (code) {
4185         PrintDiagnostics("rename", code);
4186         exit(1);
4187     }
4188     fprintf(STDOUT, "Renamed volume %s to %s\n", as->parms[0].items->data,
4189             as->parms[1].items->data);
4190     return 0;
4191 }
4192
4193 int
4194 GetVolumeInfo(afs_uint32 volid, afs_uint32 *server, afs_int32 *part, afs_int32 *voltype,
4195               struct nvldbentry *rentry)
4196 {
4197     afs_int32 vcode;
4198     int i, index = -1;
4199
4200     vcode = VLDB_GetEntryByID(volid, -1, rentry);
4201     if (vcode) {
4202         fprintf(STDERR,
4203                 "Could not fetch the entry for volume %lu from VLDB \n",
4204                 (unsigned long)volid);
4205         PrintError("", vcode);
4206         return (vcode);
4207     }
4208     MapHostToNetwork(rentry);
4209     if (volid == rentry->volumeId[ROVOL]) {
4210         *voltype = ROVOL;
4211         for (i = 0; i < rentry->nServers; i++) {
4212             if ((index == -1) && (rentry->serverFlags[i] & ITSROVOL)
4213                 && !(rentry->serverFlags[i] & RO_DONTUSE))
4214                 index = i;
4215         }
4216         if (index == -1) {
4217             fprintf(STDERR,
4218                     "RO volume is not found in VLDB entry for volume %lu\n",
4219                     (unsigned long)volid);
4220             return -1;
4221         }
4222
4223         *server = rentry->serverNumber[index];
4224         *part = rentry->serverPartition[index];
4225         return 0;
4226     }
4227
4228     index = Lp_GetRwIndex(rentry);
4229     if (index == -1) {
4230         fprintf(STDERR,
4231                 "RW Volume is not found in VLDB entry for volume %lu\n",
4232                 (unsigned long)volid);
4233         return -1;
4234     }
4235     if (volid == rentry->volumeId[RWVOL]) {
4236         *voltype = RWVOL;
4237         *server = rentry->serverNumber[index];
4238         *part = rentry->serverPartition[index];
4239         return 0;
4240     }
4241     if (volid == rentry->volumeId[BACKVOL]) {
4242         *voltype = BACKVOL;
4243         *server = rentry->serverNumber[index];
4244         *part = rentry->serverPartition[index];
4245         return 0;
4246     }
4247     fprintf(STDERR,
4248             "unexpected volume type for volume %lu\n",
4249             (unsigned long)volid);
4250     return -1;
4251 }
4252
4253 static int
4254 DeleteEntry(struct cmd_syndesc *as, void *arock)
4255 {
4256     afs_int32 apart = 0;
4257     afs_uint32 avolid;
4258     afs_int32 vcode;
4259     struct VldbListByAttributes attributes;
4260     nbulkentries arrayEntries;
4261     struct nvldbentry *vllist;
4262     struct cmd_item *itp;
4263     afs_int32 nentries;
4264     int j;
4265     char prefix[VOLSER_MAXVOLNAME + 1];
4266     int seenprefix = 0;
4267     afs_int32 totalBack = 0, totalFail = 0, err;
4268
4269     if (as->parms[0].items) {   /* -id */
4270         if (as->parms[1].items || as->parms[2].items || as->parms[3].items) {
4271             fprintf(STDERR,
4272                     "You cannot use -server, -partition, or -prefix with the -id argument\n");
4273             exit(-2);
4274         }
4275         for (itp = as->parms[0].items; itp; itp = itp->next) {
4276             avolid = vsu_GetVolumeID(itp->data, cstruct, &err);
4277             if (avolid == 0) {
4278                 if (err)
4279                     PrintError("", err);
4280                 else
4281                     fprintf(STDERR, "vos: can't find volume '%s'\n",
4282                             itp->data);
4283                 continue;
4284             }
4285             if (as->parms[4].items || as->parms[5].items) {
4286                 /* -noexecute (hidden) or -dryrun */
4287                 fprintf(STDOUT, "Would have deleted VLDB entry for %s \n",
4288                         itp->data);
4289                 fflush(STDOUT);
4290                 continue;
4291             }
4292             vcode = ubik_VL_DeleteEntry(cstruct, 0, avolid, RWVOL);
4293             if (vcode) {
4294                 fprintf(STDERR, "Could not delete entry for volume %s\n",
4295                         itp->data);
4296                 fprintf(STDERR,
4297                         "You must specify a RW volume name or ID "
4298                         "(the entire VLDB entry will be deleted)\n");
4299                 PrintError("", vcode);
4300                 totalFail++;
4301                 continue;
4302             }
4303             totalBack++;
4304         }
4305         fprintf(STDOUT, "Deleted %d VLDB entries\n", totalBack);
4306         return (totalFail);
4307     }
4308
4309     if (!as->parms[1].items && !as->parms[2].items && !as->parms[3].items) {
4310         fprintf(STDERR, "You must specify an option\n");
4311         exit(-2);
4312     }
4313
4314     /* Zero out search attributes */
4315     memset(&attributes, 0, sizeof(struct VldbListByAttributes));
4316
4317     if (as->parms[1].items) {   /* -prefix */
4318         strncpy(prefix, as->parms[1].items->data, VOLSER_MAXVOLNAME);
4319         seenprefix = 1;
4320         if (!as->parms[2].items && !as->parms[3].items) {       /* a single entry only */
4321             fprintf(STDERR,
4322                     "You must provide -server with the -prefix argument\n");
4323             exit(-2);
4324         }
4325     }
4326
4327     if (as->parms[2].items) {   /* -server */
4328         afs_uint32 aserver;
4329         aserver = GetServer(as->parms[2].items->data);
4330         if (aserver == 0) {
4331             fprintf(STDERR, "vos: server '%s' not found in host table\n",
4332                     as->parms[2].items->data);
4333             exit(-1);
4334         }
4335         attributes.server = ntohl(aserver);
4336         attributes.Mask |= VLLIST_SERVER;
4337     }
4338
4339     if (as->parms[3].items) {   /* -partition */
4340         if (!as->parms[2].items) {
4341             fprintf(STDERR,
4342                     "You must provide -server with the -partition argument\n");
4343             exit(-2);
4344         }
4345         apart = volutil_GetPartitionID(as->parms[3].items->data);
4346         if (apart < 0) {
4347             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
4348                     as->parms[3].items->data);
4349             exit(-1);
4350         }
4351         attributes.partition = apart;
4352         attributes.Mask |= VLLIST_PARTITION;
4353     }
4354
4355     /* Print status line of what we are doing */
4356     fprintf(STDOUT, "Deleting VLDB entries for ");
4357     if (as->parms[2].items) {
4358         fprintf(STDOUT, "server %s ", as->parms[2].items->data);
4359     }
4360     if (as->parms[3].items) {
4361         char pname[10];
4362         MapPartIdIntoName(apart, pname);
4363         fprintf(STDOUT, "partition %s ", pname);
4364     }
4365     if (seenprefix) {
4366         fprintf(STDOUT, "which are prefixed with %s ", prefix);
4367     }
4368     fprintf(STDOUT, "\n");
4369     fflush(STDOUT);
4370
4371     /* Get all the VLDB entries on a server and/or partition */
4372     memset(&arrayEntries, 0, sizeof(arrayEntries));
4373     vcode = VLDB_ListAttributes(&attributes, &nentries, &arrayEntries);
4374     if (vcode) {
4375         fprintf(STDERR, "Could not access the VLDB for attributes\n");
4376         PrintError("", vcode);
4377         exit(-1);
4378     }
4379
4380     /* Process each entry */
4381     for (j = 0; j < nentries; j++) {
4382         vllist = &arrayEntries.nbulkentries_val[j];
4383         if (seenprefix) {
4384             /* It only deletes the RW volumes */
4385             if (strncmp(vllist->name, prefix, strlen(prefix))) {
4386                 if (verbose) {
4387                     fprintf(STDOUT,
4388                             "Omitting to delete %s due to prefix %s mismatch\n",
4389                             vllist->name, prefix);
4390                 }
4391                 fflush(STDOUT);
4392                 continue;
4393             }
4394         }
4395
4396         if (as->parms[4].items || as->parms[5].items) {
4397             /* -noexecute (hidden) or -dryrun */
4398             fprintf(STDOUT, "Would have deleted VLDB entry for %s \n",
4399                     vllist->name);
4400             fflush(STDOUT);
4401             continue;
4402         }
4403
4404         /* Only matches the RW volume name */
4405         avolid = vllist->volumeId[RWVOL];
4406         vcode = ubik_VL_DeleteEntry(cstruct, 0, avolid, RWVOL);
4407         if (vcode) {
4408             fprintf(STDOUT, "Could not delete VLDB entry for  %s\n",
4409                     vllist->name);
4410             totalFail++;
4411             PrintError("", vcode);
4412             continue;
4413         } else {
4414             totalBack++;
4415             if (verbose)
4416                 fprintf(STDOUT, "Deleted VLDB entry for %s \n", vllist->name);
4417         }
4418         fflush(STDOUT);
4419     }                           /*for */
4420
4421     fprintf(STDOUT, "----------------------\n");
4422     fprintf(STDOUT,
4423             "Total VLDB entries deleted: %lu; failed to delete: %lu\n",
4424             (unsigned long)totalBack, (unsigned long)totalFail);
4425
4426     xdr_free((xdrproc_t) xdr_nbulkentries, &arrayEntries);
4427     return 0;
4428 }
4429
4430
4431 static int
4432 CompareVldbEntryByName(const void *p1, const void *p2)
4433 {
4434     struct nvldbentry *arg1, *arg2;
4435
4436     arg1 = (struct nvldbentry *)p1;
4437     arg2 = (struct nvldbentry *)p2;
4438     return (strcmp(arg1->name, arg2->name));
4439 }
4440
4441 /*
4442 static int CompareVldbEntry(char *p1, char *p2)
4443 {
4444     struct nvldbentry *arg1,*arg2;
4445     int i;
4446     int pos1, pos2;
4447     char comp1[100],comp2[100];
4448     char temp1[20],temp2[20];
4449
4450     arg1 = (struct nvldbentry *)p1;
4451     arg2 = (struct nvldbentry *)p2;
4452     pos1 = -1;
4453     pos2 = -1;
4454
4455     for(i = 0; i < arg1->nServers; i++)
4456         if(arg1->serverFlags[i] & ITSRWVOL) pos1 = i;
4457     for(i = 0; i < arg2->nServers; i++)
4458         if(arg2->serverFlags[i] & ITSRWVOL) pos2 = i;
4459     if(pos1 == -1 || pos2 == -1){
4460         pos1 = 0;
4461         pos2 = 0;
4462     }
4463     sprintf(comp1,"%10u",arg1->serverNumber[pos1]);
4464     sprintf(comp2,"%10u",arg2->serverNumber[pos2]);
4465     sprintf(temp1,"%10u",arg1->serverPartition[pos1]);
4466     sprintf(temp2,"%10u",arg2->serverPartition[pos2]);
4467     strcat(comp1,temp1);
4468     strcat(comp2,temp2);
4469     strcat(comp1,arg1->name);
4470     strcat(comp1,arg2->name);
4471     return(strcmp(comp1,comp2));
4472
4473 }
4474
4475 */
4476 static int
4477 ListVLDB(struct cmd_syndesc *as, void *arock)
4478 {
4479     afs_int32 apart;
4480     afs_int32 code;
4481     afs_int32 vcode;
4482     struct VldbListByAttributes attributes;
4483     nbulkentries arrayEntries;
4484     struct nvldbentry *vllist, *tarray = 0, *ttarray;
4485     afs_int32 centries, nentries = 0;
4486     afs_int32 tarraysize = 0;
4487     afs_int32 parraysize;
4488     int j;
4489     char pname[10];
4490     int quiet, sort, lock;
4491     afs_int32 thisindex, nextindex;
4492
4493     apart = 0;
4494
4495     attributes.Mask = 0;
4496     lock = (as->parms[3].items ? 1 : 0);        /* -lock   flag */
4497     quiet = (as->parms[4].items ? 1 : 0);       /* -quit   flag */
4498     sort = (as->parms[5].items ? 0 : 1);        /* -nosort flag */
4499
4500     /* If the volume name is given, Use VolumeInfoCmd to look it up
4501      * and not ListAttributes.
4502      */
4503     if (as->parms[0].items) {
4504         if (lock) {
4505             fprintf(STDERR,
4506                     "vos: illegal use of '-locked' switch, need to specify server and/or partition\n");
4507             exit(1);
4508         }
4509         code = VolumeInfoCmd(as->parms[0].items->data);
4510         if (code) {
4511             PrintError("", code);
4512             exit(1);
4513         }
4514         return 0;
4515     }
4516
4517     /* Server specified */
4518     if (as->parms[1].items) {
4519         afs_uint32 aserver;
4520
4521         aserver = GetServer(as->parms[1].items->data);
4522         if (aserver == 0) {
4523             fprintf(STDERR, "vos: server '%s' not found in host table\n",
4524                     as->parms[1].items->data);
4525             exit(1);
4526         }
4527         attributes.server = ntohl(aserver);
4528         attributes.Mask |= VLLIST_SERVER;
4529     }
4530
4531     /* Partition specified */
4532     if (as->parms[2].items) {
4533         apart = volutil_GetPartitionID(as->parms[2].items->data);
4534         if (apart < 0) {
4535             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
4536                     as->parms[2].items->data);
4537             exit(1);
4538         }
4539         attributes.partition = apart;
4540         attributes.Mask |= VLLIST_PARTITION;
4541     }
4542
4543     if (lock) {
4544         attributes.Mask |= VLLIST_FLAG;
4545         attributes.flag = VLOP_ALLOPERS;
4546     }
4547
4548     /* Print header information */
4549     if (!quiet) {
4550         MapPartIdIntoName(apart, pname);
4551         fprintf(STDOUT, "VLDB entries for %s %s%s%s %s\n",
4552                 (as->parms[1].items ? "server" : "all"),
4553                 (as->parms[1].items ? as->parms[1].items->data : "servers"),
4554                 (as->parms[2].items ? " partition " : ""),
4555                 (as->parms[2].items ? pname : ""),
4556                 (lock ? "which are locked:" : ""));
4557     }
4558
4559     for (thisindex = 0; (thisindex != -1); thisindex = nextindex) {
4560         memset(&arrayEntries, 0, sizeof(arrayEntries));
4561         centries = 0;
4562         nextindex = -1;
4563
4564         vcode =
4565             VLDB_ListAttributesN2(&attributes, 0, thisindex, &centries,
4566                                   &arrayEntries, &nextindex);
4567         if (vcode == RXGEN_OPCODE) {
4568             /* Vlserver not running with ListAttributesN2. Fall back */
4569             vcode =
4570                 VLDB_ListAttributes(&attributes, &centries, &arrayEntries);
4571             nextindex = -1;
4572         }
4573         if (vcode) {
4574             fprintf(STDERR, "Could not access the VLDB for attributes\n");
4575             PrintError("", vcode);
4576             exit(1);
4577         }
4578         nentries += centries;
4579
4580         /* We don't sort, so just print the entries now */
4581         if (!sort) {
4582             for (j = 0; j < centries; j++) {    /* process each entry */
4583                 vllist = &arrayEntries.nbulkentries_val[j];
4584                 MapHostToNetwork(vllist);
4585                 EnumerateEntry(vllist);
4586
4587                 PrintLocked(vllist->flags);
4588             }
4589         }
4590
4591         /* So we sort. First we must collect all the entries and keep
4592          * them in memory.
4593          */
4594         else if (centries > 0) {
4595             if (!tarray) {
4596                 /* malloc the first bulk entries array */
4597                 tarraysize = centries * sizeof(struct nvldbentry);
4598                 tarray = malloc(tarraysize);
4599                 if (!tarray) {
4600                     fprintf(STDERR,
4601                             "Could not allocate enough space for the VLDB entries\n");
4602                     goto bypass;
4603                 }
4604                 memcpy((char*)tarray, arrayEntries.nbulkentries_val, tarraysize);
4605             } else {
4606                 /* Grow the tarray to keep the extra entries */
4607                 parraysize = (centries * sizeof(struct nvldbentry));
4608                 ttarray = realloc(tarray, tarraysize + parraysize);
4609                 if (!ttarray) {
4610                     fprintf(STDERR,
4611                             "Could not allocate enough space for  the VLDB entries\n");
4612                     goto bypass;
4613                 }
4614                 tarray = ttarray;
4615
4616                 /* Copy them in */
4617                 memcpy(((char *)tarray) + tarraysize,
4618                        (char *)arrayEntries.nbulkentries_val, parraysize);
4619                 tarraysize += parraysize;
4620             }
4621         }
4622
4623         /* Free the bulk array */
4624         xdr_free((xdrproc_t) xdr_nbulkentries, &arrayEntries);
4625     }
4626
4627     /* Here is where we now sort all the entries and print them */
4628     if (sort && (nentries > 0)) {
4629         qsort((char *)tarray, nentries, sizeof(struct nvldbentry),
4630               CompareVldbEntryByName);
4631         for (vllist = tarray, j = 0; j < nentries; j++, vllist++) {
4632             MapHostToNetwork(vllist);
4633             EnumerateEntry(vllist);
4634
4635             PrintLocked(vllist->flags);
4636         }
4637     }
4638
4639   bypass:
4640     if (!quiet)
4641         fprintf(STDOUT, "\nTotal entries: %lu\n", (unsigned long)nentries);
4642     if (tarray)
4643         free(tarray);
4644     return 0;
4645 }
4646
4647 static int
4648 BackSys(struct cmd_syndesc *as, void *arock)
4649 {
4650     afs_uint32 avolid;
4651     afs_int32 apart = 0;
4652     afs_uint32 aserver = 0, aserver1;
4653     afs_int32 code, apart1;
4654     afs_int32 vcode;
4655     struct VldbListByAttributes attributes;
4656     nbulkentries arrayEntries;
4657     struct nvldbentry *vllist;
4658     afs_int32 nentries;
4659     int j;
4660     char pname[10];
4661     int seenprefix, seenxprefix, exclude, ex, exp, noaction;
4662     afs_int32 totalBack = 0;
4663     afs_int32 totalFail = 0;
4664     int previdx = -1;
4665     int error;
4666     int same = 0;
4667     struct cmd_item *ti;
4668     int match = 0;
4669 #ifndef HAVE_POSIX_REGEX
4670     char *ccode;
4671 #endif
4672
4673     memset(&attributes, 0, sizeof(struct VldbListByAttributes));
4674     attributes.Mask = 0;
4675
4676     seenprefix = (as->parms[0].items ? 1 : 0);
4677     exclude = (as->parms[3].items ? 1 : 0);
4678     seenxprefix = (as->parms[4].items ? 1 : 0);
4679     noaction = (as->parms[5].items ? 1 : 0);
4680
4681     if (as->parms[1].items) {   /* -server */
4682         aserver = GetServer(as->parms[1].items->data);
4683         if (aserver == 0) {
4684             fprintf(STDERR, "vos: server '%s' not found in host table\n",
4685                     as->parms[1].items->data);
4686             exit(1);
4687         }
4688         attributes.server = ntohl(aserver);
4689         attributes.Mask |= VLLIST_SERVER;
4690     }
4691
4692     if (as->parms[2].items) {   /* -partition */
4693         apart = volutil_GetPartitionID(as->parms[2].items->data);
4694         if (apart < 0) {
4695             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
4696                     as->parms[2].items->data);
4697             exit(1);
4698         }
4699         attributes.partition = apart;
4700         attributes.Mask |= VLLIST_PARTITION;
4701     }
4702
4703     /* Check to make sure the prefix and xprefix expressions compile ok */
4704     if (seenprefix) {
4705         for (ti = as->parms[0].items; ti; ti = ti->next) {
4706             if (strncmp(ti->data, "^", 1) == 0) {
4707 #ifdef HAVE_POSIX_REGEX
4708                 regex_t re;
4709                 char errbuf[256];
4710
4711                 code = regcomp(&re, ti->data, REG_NOSUB);
4712                 if (code != 0) {
4713                     regerror(code, &re, errbuf, sizeof errbuf);
4714                     fprintf(STDERR,
4715                             "Unrecognizable -prefix regular expression: '%s': %s\n",
4716                             ti->data, errbuf);
4717                     exit(1);
4718                 }
4719                 regfree(&re);
4720 #else
4721                 ccode = (char *)re_comp(ti->data);
4722                 if (ccode) {
4723                     fprintf(STDERR,
4724                             "Unrecognizable -prefix regular expression: '%s': %s\n",
4725                             ti->data, ccode);
4726                     exit(1);
4727                 }
4728 #endif
4729             }
4730         }
4731     }
4732     if (seenxprefix) {
4733         for (ti = as->parms[4].items; ti; ti = ti->next) {
4734             if (strncmp(ti->data, "^", 1) == 0) {
4735 #ifdef HAVE_POSIX_REGEX
4736                 regex_t re;
4737                 char errbuf[256];
4738
4739                 code = regcomp(&re, ti->data, REG_NOSUB);
4740                 if (code != 0) {
4741                     regerror(code, &re, errbuf, sizeof errbuf);
4742                     fprintf(STDERR,
4743                             "Unrecognizable -xprefix regular expression: '%s': %s\n",
4744                             ti->data, errbuf);
4745                     exit(1);
4746                 }
4747                 regfree(&re);
4748 #else
4749                 ccode = (char *)re_comp(ti->data);
4750                 if (ccode) {
4751                     fprintf(STDERR,
4752                             "Unrecognizable -xprefix regular expression: '%s': %s\n",
4753                             ti->data, ccode);
4754                     exit(1);
4755                 }
4756 #endif
4757             }
4758         }
4759     }
4760
4761     memset(&arrayEntries, 0, sizeof(arrayEntries));     /* initialize to hint the stub to alloc space */
4762     vcode = VLDB_ListAttributes(&attributes, &nentries, &arrayEntries);
4763     if (vcode) {
4764         fprintf(STDERR, "Could not access the VLDB for attributes\n");
4765         PrintError("", vcode);
4766         exit(1);
4767     }
4768
4769     if (as->parms[1].items || as->parms[2].items || verbose) {
4770         fprintf(STDOUT, "%s up volumes",
4771                 (noaction ? "Would have backed" : "Backing"));
4772
4773         if (as->parms[1].items) {
4774             fprintf(STDOUT, " on server %s", as->parms[1].items->data);
4775         } else if (as->parms[2].items) {
4776             fprintf(STDOUT, " for all servers");
4777         }
4778
4779         if (as->parms[2].items) {
4780             MapPartIdIntoName(apart, pname);
4781             fprintf(STDOUT, " partition %s", pname);
4782         }
4783
4784         if (seenprefix || (!seenprefix && seenxprefix)) {
4785             ti = (seenprefix ? as->parms[0].items : as->parms[4].items);
4786             ex = (seenprefix ? exclude : !exclude);
4787             exp = (strncmp(ti->data, "^", 1) == 0);
4788             fprintf(STDOUT, " which %smatch %s '%s'", (ex ? "do not " : ""),
4789                     (exp ? "expression" : "prefix"), ti->data);
4790             for (ti = ti->next; ti; ti = ti->next) {
4791                 exp = (strncmp(ti->data, "^", 1) == 0);
4792                 printf(" %sor %s '%s'", (ex ? "n" : ""),
4793                        (exp ? "expression" : "prefix"), ti->data);
4794             }
4795         }
4796
4797         if (seenprefix && seenxprefix) {
4798             ti = as->parms[4].items;
4799             exp = (strncmp(ti->data, "^", 1) == 0);
4800             fprintf(STDOUT, " %swhich match %s '%s'",
4801                     (exclude ? "adding those " : "removing those "),
4802                     (exp ? "expression" : "prefix"), ti->data);
4803             for (ti = ti->next; ti; ti = ti->next) {
4804                 exp = (strncmp(ti->data, "^", 1) == 0);
4805                 printf(" or %s '%s'", (exp ? "expression" : "prefix"),
4806                        ti->data);
4807             }
4808         }
4809         fprintf(STDOUT, " .. ");
4810         if (verbose)
4811             fprintf(STDOUT, "\n");
4812         fflush(STDOUT);
4813     }
4814
4815     for (j = 0; j < nentries; j++) {    /* process each vldb entry */
4816         vllist = &arrayEntries.nbulkentries_val[j];
4817
4818         if (seenprefix) {
4819             for (ti = as->parms[0].items; ti; ti = ti->next) {
4820                 if (strncmp(ti->data, "^", 1) == 0) {
4821 #ifdef HAVE_POSIX_REGEX
4822                     regex_t re;
4823                     char errbuf[256];
4824
4825                     /* XXX -- should just do the compile once! */
4826                     code = regcomp(&re, ti->data, REG_NOSUB);
4827                     if (code != 0) {
4828                         regerror(code, &re, errbuf, sizeof errbuf);
4829                         fprintf(STDERR,
4830                                 "Error in -prefix regular expression: '%s': %s\n",
4831                                 ti->data, errbuf);
4832                         exit(1);
4833                     }
4834                     match = (regexec(&re, vllist->name, 0, NULL, 0) == 0);
4835                     regfree(&re);
4836 #else
4837                     ccode = (char *)re_comp(ti->data);
4838                     if (ccode) {
4839                         fprintf(STDERR,
4840                                 "Error in -prefix regular expression: '%s': %s\n",
4841                                 ti->data, ccode);
4842                         exit(1);
4843                     }
4844                     match = (re_exec(vllist->name) == 1);
4845 #endif
4846                 } else {
4847                     match =
4848                         (strncmp(vllist->name, ti->data, strlen(ti->data)) ==
4849                          0);
4850                 }
4851                 if (match)
4852                     break;
4853             }
4854         } else {
4855             match = 1;
4856         }
4857
4858         /* Without the -exclude flag: If it matches the prefix, then
4859          *    check if we want to exclude any from xprefix.
4860          * With the -exclude flag: If it matches the prefix, then
4861          *    check if we want to add any from xprefix.
4862          */
4863         if (match && seenxprefix) {
4864             for (ti = as->parms[4].items; ti; ti = ti->next) {
4865                 if (strncmp(ti->data, "^", 1) == 0) {
4866 #ifdef HAVE_POSIX_REGEX
4867                     regex_t re;
4868                     char errbuf[256];
4869
4870                     /* XXX -- should just do the compile once! */
4871                     code = regcomp(&re, ti->data, REG_NOSUB);
4872                     if (code != 0) {
4873                         regerror(code, &re, errbuf, sizeof errbuf);
4874                         fprintf(STDERR,
4875                                 "Error in -xprefix regular expression: '%s': %s\n",
4876                                 ti->data, errbuf);
4877                         exit(1);
4878                     }
4879                     if (regexec(&re, vllist->name, 0, NULL, 0) == 0)
4880                             match = 0;
4881                     regfree(&re);
4882 #else
4883                     ccode = (char *)re_comp(ti->data);
4884                     if (ccode) {
4885                         fprintf(STDERR,
4886                                 "Error in -xprefix regular expression: '%s': %s\n",
4887                                 ti->data, ccode);
4888                         exit(1);
4889                     }
4890                     if (re_exec(vllist->name) == 1) {
4891                         match = 0;
4892                         break;
4893                     }
4894 #endif
4895                 } else {
4896                     if (strncmp(vllist->name, ti->data, strlen(ti->data)) ==
4897                         0) {
4898                         match = 0;
4899                         break;
4900                     }
4901                 }
4902             }
4903         }
4904
4905         if (exclude)
4906             match = !match;     /* -exclude will reverse the match */
4907         if (!match)
4908             continue;           /* Skip if no match */
4909
4910         /* Print list of volumes to backup */
4911         if (noaction) {
4912             fprintf(STDOUT, "     %s\n", vllist->name);
4913             continue;
4914         }
4915
4916         if (!(vllist->flags & RW_EXISTS)) {
4917             if (verbose) {
4918                 fprintf(STDOUT,
4919                         "Omitting to backup %s since RW volume does not exist \n",
4920                         vllist->name);
4921                 fprintf(STDOUT, "\n");
4922             }
4923             fflush(STDOUT);
4924             continue;
4925         }
4926
4927         avolid = vllist->volumeId[RWVOL];
4928         MapHostToNetwork(vllist);
4929         GetServerAndPart(vllist, RWVOL, &aserver1, &apart1, &previdx);
4930         if (aserver1 == -1 || apart1 == -1) {
4931             fprintf(STDOUT, "could not backup %s, invalid VLDB entry\n",
4932                     vllist->name);
4933             totalFail++;
4934             continue;
4935         }
4936         if (aserver) {
4937             same = VLDB_IsSameAddrs(aserver, aserver1, &error);
4938             if (error) {
4939                 fprintf(STDERR,
4940                         "Failed to get info about server's %d address(es) from vlserver (err=%d); aborting call!\n",
4941                         aserver, error);
4942                 totalFail++;
4943                 continue;
4944             }
4945         }
4946         if ((aserver && !same) || (apart && (apart != apart1))) {
4947             if (verbose) {
4948                 fprintf(STDOUT,
4949                         "Omitting to backup %s since the RW is in a different location\n",
4950                         vllist->name);
4951             }
4952             continue;
4953         }
4954         if (verbose) {
4955             time_t now = time(0);
4956             fprintf(STDOUT, "Creating backup volume for %s on %s",
4957                     vllist->name, ctime(&now));
4958             fflush(STDOUT);
4959         }
4960
4961         code = UV_BackupVolume(aserver1, apart1, avolid);
4962         if (code) {
4963             fprintf(STDOUT, "Could not backup %s\n", vllist->name);
4964             totalFail++;
4965         } else {
4966             totalBack++;
4967         }
4968         if (verbose)
4969             fprintf(STDOUT, "\n");
4970         fflush(STDOUT);
4971     }                           /* process each vldb entry */
4972     fprintf(STDOUT, "done\n");
4973     fprintf(STDOUT, "Total volumes backed up: %lu; failed to backup: %lu\n",
4974             (unsigned long)totalBack, (unsigned long)totalFail);
4975     fflush(STDOUT);
4976     xdr_free((xdrproc_t) xdr_nbulkentries, &arrayEntries);
4977     return 0;
4978 }
4979
4980 static int
4981 UnlockVLDB(struct cmd_syndesc *as, void *arock)
4982 {
4983     afs_int32 apart;
4984     afs_uint32 aserver = 0;
4985     afs_int32 code;
4986     afs_int32 vcode;
4987     struct VldbListByAttributes attributes;
4988     nbulkentries arrayEntries;
4989     struct nvldbentry *vllist;
4990     afs_int32 nentries;
4991     int j;
4992     afs_uint32 volid;
4993     afs_int32 totalE;
4994     char pname[10];
4995
4996     apart = -1;
4997     totalE = 0;
4998     attributes.Mask = 0;
4999
5000     if (as->parms[0].items) {   /* server specified */
5001         aserver = GetServer(as->parms[0].items->data);
5002         if (aserver == 0) {
5003             fprintf(STDERR, "vos: server '%s' not found in host table\n",
5004                     as->parms[0].items->data);
5005             exit(1);
5006         }
5007         attributes.server = ntohl(aserver);
5008         attributes.Mask |= VLLIST_SERVER;
5009     }
5010     if (as->parms[1].items) {   /* partition specified */
5011         apart = volutil_GetPartitionID(as->parms[1].items->data);
5012         if (apart < 0) {
5013             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
5014                     as->parms[1].items->data);
5015             exit(1);
5016         }
5017         if (!IsPartValid(apart, aserver, &code)) {      /*check for validity of the partition */
5018             if (code)
5019                 PrintError("", code);
5020             else
5021                 fprintf(STDERR,
5022                         "vos : partition %s does not exist on the server\n",
5023                         as->parms[1].items->data);
5024             exit(1);
5025         }
5026         attributes.partition = apart;
5027         attributes.Mask |= VLLIST_PARTITION;
5028     }
5029     attributes.flag = VLOP_ALLOPERS;
5030     attributes.Mask |= VLLIST_FLAG;
5031     memset(&arrayEntries, 0, sizeof(arrayEntries));     /*initialize to hint the stub  to alloc space */
5032     vcode = VLDB_ListAttributes(&attributes, &nentries, &arrayEntries);
5033     if (vcode) {
5034         fprintf(STDERR, "Could not access the VLDB for attributes\n");
5035         PrintError("", vcode);
5036         exit(1);
5037     }
5038     for (j = 0; j < nentries; j++) {    /* process each entry */
5039         vllist = &arrayEntries.nbulkentries_val[j];
5040         volid = vllist->volumeId[RWVOL];
5041         vcode =
5042             ubik_VL_ReleaseLock(cstruct, 0, volid, -1,
5043                                 LOCKREL_OPCODE | LOCKREL_AFSID |
5044                                 LOCKREL_TIMESTAMP);
5045         if (vcode) {
5046             fprintf(STDERR, "Could not unlock entry for volume %s\n",
5047                     vllist->name);
5048             PrintError("", vcode);
5049             totalE++;
5050         }
5051
5052     }
5053     MapPartIdIntoName(apart, pname);
5054     if (totalE)
5055         fprintf(STDOUT,
5056                 "Could not lock %lu VLDB entries of %lu locked entries\n",
5057                 (unsigned long)totalE, (unsigned long)nentries);
5058     else {
5059         if (as->parms[0].items) {
5060             fprintf(STDOUT,
5061                     "Unlocked all the VLDB entries for volumes on server %s ",
5062                     as->parms[0].items->data);
5063             if (as->parms[1].items) {
5064                 MapPartIdIntoName(apart, pname);
5065                 fprintf(STDOUT, "partition %s\n", pname);
5066             } else
5067                 fprintf(STDOUT, "\n");
5068
5069         } else if (as->parms[1].items) {
5070             MapPartIdIntoName(apart, pname);
5071             fprintf(STDOUT,
5072                     "Unlocked all the VLDB entries for volumes on partition %s on all servers\n",
5073                     pname);
5074         }
5075     }
5076
5077     xdr_free((xdrproc_t) xdr_nbulkentries, &arrayEntries);
5078     return 0;
5079 }
5080
5081 static char *
5082 PrintInt64Size(afs_uint64 in)
5083 {
5084     afs_uint32 hi, lo;
5085     char * units;
5086     static char output[16];
5087
5088     SplitInt64(in,hi,lo);
5089
5090     if (hi == 0) {
5091         units = "KB";
5092     } else if (!(hi & 0xFFFFFC00)) {
5093         units = "MB";
5094         lo = (hi << 22) | (lo >> 10);
5095     } else if (!(hi & 0xFFF00000)) {
5096         units = "GB";
5097         lo = (hi << 12) | (lo >> 20);
5098     } else if (!(hi & 0xC0000000)) {
5099         units = "TB";
5100         lo = (hi << 2) | (lo >> 30);
5101     } else {
5102         units = "PB";
5103         lo = (hi >> 8);
5104     }
5105     sprintf(output,"%u %s", lo, units);
5106     return output;
5107 }
5108
5109 static int
5110 PartitionInfo(struct cmd_syndesc *as, void *arock)
5111 {
5112     afs_int32 apart;
5113     afs_uint32 aserver;
5114     afs_int32 code;
5115     char pname[10];
5116     struct diskPartition64 partition;
5117     struct partList dummyPartList;
5118     int i, cnt;
5119     int printSummary=0, sumPartitions=0;
5120     afs_uint64 sumFree, sumStorage;
5121
5122     ZeroInt64(sumFree);
5123     ZeroInt64(sumStorage);
5124     apart = -1;
5125     aserver = GetServer(as->parms[0].items->data);
5126     if (aserver == 0) {
5127         fprintf(STDERR, "vos: server '%s' not found in host table\n",
5128                 as->parms[0].items->data);
5129         exit(1);
5130     }
5131     if (as->parms[1].items) {
5132         apart = volutil_GetPartitionID(as->parms[1].items->data);
5133         if (apart < 0) {
5134             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
5135                     as->parms[1].items->data);
5136             exit(1);
5137         }
5138         dummyPartList.partId[0] = apart;
5139         dummyPartList.partFlags[0] = PARTVALID;
5140         cnt = 1;
5141     }
5142     if (as->parms[2].items) {
5143         printSummary = 1;
5144     }
5145     if (apart != -1) {
5146         if (!IsPartValid(apart, aserver, &code)) {      /*check for validity of the partition */
5147             if (code)
5148                 PrintError("", code);
5149             else
5150                 fprintf(STDERR,
5151                         "vos : partition %s does not exist on the server\n",
5152                         as->parms[1].items->data);
5153             exit(1);
5154         }
5155     } else {
5156         code = UV_ListPartitions(aserver, &dummyPartList, &cnt);
5157         if (code) {
5158             PrintDiagnostics("listpart", code);
5159             exit(1);
5160         }
5161     }
5162     for (i = 0; i < cnt; i++) {
5163         if (dummyPartList.partFlags[i] & PARTVALID) {
5164             MapPartIdIntoName(dummyPartList.partId[i], pname);
5165             code = UV_PartitionInfo64(aserver, pname, &partition);
5166             if (code) {
5167                 fprintf(STDERR, "Could not get information on partition %s\n",
5168                         pname);
5169                 PrintError("", code);
5170                 exit(1);
5171             }
5172             fprintf(STDOUT,
5173                     "Free space on partition %s: %" AFS_INT64_FMT " K blocks out of total %" AFS_INT64_FMT "\n",
5174                     pname, partition.free, partition.minFree);
5175             sumPartitions++;
5176             AddUInt64(sumFree,partition.free,&sumFree);
5177             AddUInt64(sumStorage,partition.minFree,&sumStorage);
5178         }
5179     }
5180     if (printSummary) {
5181         fprintf(STDOUT,
5182                 "Summary: %s free out of ",
5183                 PrintInt64Size(sumFree));
5184         fprintf(STDOUT,
5185                 "%s on %d partitions\n",
5186                 PrintInt64Size(sumStorage),
5187                 sumPartitions);
5188     }
5189     return 0;
5190 }
5191
5192 static int
5193 ChangeAddr(struct cmd_syndesc *as, void *arock)
5194 {
5195     afs_int32 ip1, ip2, vcode;
5196     int remove = 0;
5197
5198     if (noresolve)
5199         ip1 = GetServerNoresolve(as->parms[0].items->data);
5200     else
5201         ip1 = GetServer(as->parms[0].items->data);
5202     if (!ip1) {
5203         fprintf(STDERR, "vos: invalid host address\n");
5204         return (EINVAL);
5205     }
5206
5207     if ((as->parms[1].items && as->parms[2].items)
5208         || (!as->parms[1].items && !as->parms[2].items)) {
5209         fprintf(STDERR,
5210                 "vos: Must specify either '-newaddr <addr>' or '-remove' flag\n");
5211         return (EINVAL);
5212     }
5213
5214     if (as->parms[1].items) {
5215         if (noresolve)
5216             ip2 = GetServerNoresolve(as->parms[1].items->data);
5217         else
5218             ip2 = GetServer(as->parms[1].items->data);
5219         if (!ip2) {
5220             fprintf(STDERR, "vos: invalid host address\n");
5221             return (EINVAL);
5222         }
5223     } else {
5224         /* Play a trick here. If we are removing an address, ip1 will be -1
5225          * and ip2 will be the original address. This switch prevents an
5226          * older revision vlserver from removing the IP address.
5227          */
5228         remove = 1;
5229         ip2 = ip1;
5230         ip1 = 0xffffffff;
5231     }
5232
5233     vcode = ubik_VL_ChangeAddr(cstruct, UBIK_CALL_NEW, ntohl(ip1), ntohl(ip2));
5234     if (vcode) {
5235         char hoststr1[16], hoststr2[16];
5236         if (remove) {
5237             afs_inet_ntoa_r(ip2, hoststr2);
5238             fprintf(STDERR, "Could not remove server %s from the VLDB\n",
5239                     hoststr2);
5240             if (vcode == VL_NOENT) {
5241                 fprintf(STDERR,
5242                         "vlserver does not support the remove flag or ");
5243             }
5244         } else {
5245             afs_inet_ntoa_r(ip1, hoststr1);
5246             afs_inet_ntoa_r(ip2, hoststr2);
5247             fprintf(STDERR, "Could not change server %s to server %s\n",
5248                     hoststr1, hoststr2);
5249         }
5250         PrintError("", vcode);
5251         return (vcode);
5252     }
5253
5254     if (remove) {
5255         fprintf(STDOUT, "Removed server %s from the VLDB\n",
5256                 as->parms[0].items->data);
5257     } else {
5258         fprintf(STDOUT, "Changed server %s to server %s\n",
5259                 as->parms[0].items->data, as->parms[1].items->data);
5260     }
5261     return 0;
5262 }
5263
5264 static void
5265 print_addrs(const bulkaddrs * addrs, afsUUID * m_uuid, int nentries,
5266             int print)
5267 {
5268     int i;
5269     afs_uint32 *addrp;
5270     char buf[1024];
5271
5272     if (print) {
5273         afsUUID_to_string(m_uuid, buf, sizeof(buf));
5274         printf("UUID: %s\n", buf);
5275     }
5276
5277     /* print out the list of all the server */
5278     addrp = (afs_uint32 *) addrs->bulkaddrs_val;
5279     for (i = 0; i < nentries; i++, addrp++) {
5280         *addrp = htonl(*addrp);
5281         if (noresolve) {
5282             char hoststr[16];
5283             printf("%s\n", afs_inet_ntoa_r(*addrp, hoststr));
5284         } else {
5285             printf("%s\n", hostutil_GetNameByINet(*addrp));
5286         }
5287     }
5288
5289     if (print) {
5290         printf("\n");
5291     }
5292     return;
5293 }
5294
5295 static int
5296 ListAddrs(struct cmd_syndesc *as, void *arock)
5297 {
5298     afs_int32 vcode, m_uniq=0;
5299     afs_int32 i, printuuid = 0;
5300     struct VLCallBack vlcb;
5301     afs_int32 nentries;
5302     bulkaddrs m_addrs;
5303     ListAddrByAttributes m_attrs;
5304     afsUUID m_uuid, askuuid;
5305     afs_int32 m_nentries;
5306
5307     if (as->parms[0].items && as->parms[1].items) {
5308         fprintf(STDERR, "vos: Can't use the -uuid and -host flags together\n");
5309         exit(-1);
5310     }
5311
5312     memset(&m_attrs, 0, sizeof(struct ListAddrByAttributes));
5313     memset(&askuuid, 0, sizeof(afsUUID));
5314     if (as->parms[0].items) {
5315         /* -uuid */
5316         if (afsUUID_from_string(as->parms[0].items->data, &askuuid) < 0) {
5317             fprintf(STDERR, "vos: invalid UUID '%s'\n",
5318                     as->parms[0].items->data);
5319             exit(-1);
5320         }
5321         m_attrs.Mask = VLADDR_UUID;
5322         m_attrs.uuid = askuuid;
5323     } else if (as->parms[1].items) {
5324         /* -host */
5325         struct hostent *he;
5326         afs_uint32 saddr;
5327         he = hostutil_GetHostByName((char *)as->parms[1].items->data);
5328         if (he == NULL) {
5329             fprintf(STDERR, "vos: Can't get host info for '%s'\n",
5330                     as->parms[1].items->data);
5331             exit(-1);
5332         }
5333         memcpy(&saddr, he->h_addr, 4);
5334         m_attrs.Mask = VLADDR_IPADDR;
5335         m_attrs.ipaddr = ntohl(saddr);
5336     } else {
5337         /* by index */
5338         m_attrs.Mask = VLADDR_INDEX;
5339     }
5340
5341     if (as->parms[2].items) {
5342         printuuid = 1;
5343     }
5344
5345     memset(&m_addrs, 0, sizeof(bulkaddrs));
5346     memset(&vlcb, 0, sizeof(struct VLCallBack));
5347
5348     vcode =
5349         ubik_VL_GetAddrs(cstruct, UBIK_CALL_NEW, 0, 0, &vlcb, &nentries,
5350                          &m_addrs);
5351     if (vcode) {
5352         fprintf(STDERR, "vos: could not list the server addresses\n");
5353         PrintError("", vcode);
5354         goto out;
5355     }
5356
5357     for (i = 1, m_nentries = 0; nentries; i++) {
5358         m_attrs.index = i;
5359
5360         xdr_free((xdrproc_t)xdr_bulkaddrs, &m_addrs); /* reset addr list */
5361         vcode =
5362             ubik_VL_GetAddrsU(cstruct, UBIK_CALL_NEW, &m_attrs, &m_uuid,
5363                               &m_uniq, &m_nentries, &m_addrs);
5364         switch (vcode) {
5365         case 0: /* success */
5366             print_addrs(&m_addrs, &m_uuid, m_nentries, printuuid);
5367             nentries--;
5368             break;
5369
5370         case VL_NOENT:
5371             if (m_attrs.Mask == VLADDR_UUID) {
5372                 fprintf(STDERR, "vos: no entry for UUID '%s' found in VLDB\n",
5373                         as->parms[0].items->data);
5374                 exit(-1);
5375             } else if (m_attrs.Mask == VLADDR_IPADDR) {
5376                 fprintf(STDERR, "vos: no entry for host '%s' [0x%08x] found in VLDB\n",
5377                         as->parms[1].items->data, m_attrs.ipaddr);
5378                 exit(-1);
5379             }
5380             continue;
5381
5382         case VL_INDEXERANGE:
5383             vcode = 0; /* not an error, just means we're done */
5384             goto out;
5385
5386         default: /* any other error */
5387             fprintf(STDERR, "vos: could not list the server addresses\n");
5388             PrintError("", vcode);
5389             goto out;
5390         }
5391
5392         /* if -uuid or -host, only list one response */
5393         if (as->parms[1].items || as->parms[0].items)
5394             break;
5395     }
5396
5397 out:
5398     xdr_free((xdrproc_t)xdr_bulkaddrs, &m_addrs);
5399     return vcode;
5400 }
5401
5402
5403 static int
5404 SetAddrs(struct cmd_syndesc *as, void *arock)
5405 {
5406     afs_int32 vcode;
5407     bulkaddrs m_addrs;
5408     afsUUID askuuid;
5409     afs_uint32 FS_HostAddrs_HBO[ADDRSPERSITE];
5410
5411     memset(&m_addrs, 0, sizeof(bulkaddrs));
5412     memset(&askuuid, 0, sizeof(afsUUID));
5413     if (as->parms[0].items) {
5414         /* -uuid */
5415         if (afsUUID_from_string(as->parms[0].items->data, &askuuid) < 0) {
5416             fprintf(STDERR, "vos: invalid UUID '%s'\n",
5417                     as->parms[0].items->data);
5418             exit(-1);
5419         }
5420     }
5421     if (as->parms[1].items) {
5422         /* -host */
5423         struct cmd_item *ti;
5424         afs_uint32 saddr;
5425         int i = 0;
5426
5427         for (ti = as->parms[1].items; ti && i < ADDRSPERSITE; ti = ti->next) {
5428
5429             if (noresolve)
5430                 saddr = GetServerNoresolve(ti->data);
5431             else
5432                 saddr = GetServer(ti->data);
5433
5434             if (!saddr) {
5435                 fprintf(STDERR, "vos: Can't get host info for '%s'\n",
5436                         ti->data);
5437                 exit(-1);
5438             }
5439             /* Convert it to host byte order */
5440             FS_HostAddrs_HBO[i] = ntohl(saddr);
5441             i++;
5442         }
5443         m_addrs.bulkaddrs_len = i;
5444         m_addrs.bulkaddrs_val = FS_HostAddrs_HBO;
5445     }
5446
5447     vcode = ubik_VL_RegisterAddrs(cstruct, 0, &askuuid, 0, &m_addrs);
5448
5449     if (vcode) {
5450         if (vcode == VL_MULTIPADDR) {
5451             fprintf(STDERR, "vos: VL_RegisterAddrs rpc failed; The IP address exists on a different server; repair it\n");
5452         } else if (vcode == RXGEN_OPCODE) {
5453             fprintf(STDERR, "vlserver doesn't support VL_RegisterAddrs rpc; ignored\n");
5454         } else {
5455             fprintf(STDERR, "vos: VL_RegisterAddrs rpc failed\n");
5456         }
5457         PrintError("", vcode);
5458         return vcode;
5459     }
5460     if (verbose) {
5461         fprintf(STDOUT, "vos: Changed UUID with addresses:\n");
5462         print_addrs(&m_addrs, &askuuid, m_addrs.bulkaddrs_len, 1);
5463     }
5464     return 0;
5465 }
5466
5467 static int
5468 LockEntry(struct cmd_syndesc *as, void *arock)
5469 {
5470     afs_uint32 avolid;
5471     afs_int32 vcode, err;
5472
5473     avolid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
5474     if (avolid == 0) {
5475         if (err)
5476             PrintError("", err);
5477         else
5478             fprintf(STDERR, "vos: can't find volume '%s'\n",
5479                     as->parms[0].items->data);
5480         exit(1);
5481     }
5482     vcode = ubik_VL_SetLock(cstruct, 0, avolid, -1, VLOP_DELETE);
5483     if (vcode) {
5484         fprintf(STDERR, "Could not lock VLDB entry for volume %s\n",
5485                 as->parms[0].items->data);
5486         PrintError("", vcode);
5487         exit(1);
5488     }
5489     fprintf(STDOUT, "Locked VLDB entry for volume %s\n",
5490             as->parms[0].items->data);
5491     return 0;
5492 }
5493
5494 static int
5495 ConvertRO(struct cmd_syndesc *as, void *arock)
5496 {
5497     afs_int32 partition = -1;
5498     afs_uint32 volid;
5499     afs_uint32 server;
5500     afs_int32 code, i, same;
5501     struct nvldbentry entry, checkEntry, storeEntry;
5502     afs_int32 vcode;
5503     afs_int32 rwindex = 0;
5504     afs_uint32 rwserver = 0;
5505     afs_int32 rwpartition = 0;
5506     afs_int32 roindex = 0;
5507     afs_uint32 roserver = 0;
5508     afs_int32 ropartition = 0;
5509     int force = 0;
5510     struct rx_connection *aconn;
5511     int c, dc;
5512
5513     server = GetServer(as->parms[0].items->data);
5514     if (!server) {
5515         fprintf(STDERR, "vos: host '%s' not found in host table\n",
5516                 as->parms[0].items->data);
5517         return ENOENT;
5518     }
5519     partition = volutil_GetPartitionID(as->parms[1].items->data);
5520     if (partition < 0) {
5521         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
5522                 as->parms[1].items->data);
5523         return ENOENT;
5524     }
5525     if (!IsPartValid(partition, server, &code)) {
5526         if (code)
5527             PrintError("", code);
5528         else
5529             fprintf(STDERR,
5530                     "vos : partition %s does not exist on the server\n",
5531                     as->parms[1].items->data);
5532         return ENOENT;
5533     }
5534     volid = vsu_GetVolumeID(as->parms[2].items->data, cstruct, &code);
5535     if (volid == 0) {
5536         if (code)
5537             PrintError("", code);
5538         else
5539             fprintf(STDERR, "Unknown volume ID or name '%s'\n",
5540                     as->parms[2].items->data);
5541         return -1;
5542     }
5543     if (as->parms[3].items)
5544         force = 1;
5545
5546     memset(&entry, 0, sizeof(entry));
5547     vcode = VLDB_GetEntryByID(volid, -1, &entry);
5548     if (vcode) {
5549         fprintf(STDERR,
5550                 "Could not fetch the entry for volume %lu from VLDB\n",
5551                 (unsigned long)volid);
5552         PrintError("convertROtoRW ", vcode);
5553         return vcode;
5554     }
5555
5556     /* use RO volid even if user specified RW or BK volid */
5557     if (volid != entry.volumeId[ROVOL])
5558         volid = entry.volumeId[ROVOL];
5559
5560     MapHostToNetwork(&entry);
5561     for (i = 0; i < entry.nServers; i++) {
5562         if (entry.serverFlags[i] & ITSRWVOL) {
5563             rwindex = i;
5564             rwserver = entry.serverNumber[i];
5565             rwpartition = entry.serverPartition[i];
5566             if (roserver)
5567                 break;
5568         } else if ((entry.serverFlags[i] & ITSROVOL) && !roserver) {
5569             same = VLDB_IsSameAddrs(server, entry.serverNumber[i], &code);
5570             if (code) {
5571                 fprintf(STDERR,
5572                         "Failed to get info about server's %d address(es) from vlserver (err=%d); aborting call!\n",
5573                         server, code);
5574                 return ENOENT;
5575             }
5576             if (same) {
5577                 roindex = i;
5578                 roserver = entry.serverNumber[i];
5579                 ropartition = entry.serverPartition[i];
5580                 if (rwserver)
5581                      break;
5582             }
5583         }
5584     }
5585     if (!roserver) {
5586         fprintf(STDERR, "Warning: RO volume didn't exist in vldb!\n");
5587     }
5588     if (roserver && (ropartition != partition)) {
5589         fprintf(STDERR,
5590                 "Warning: RO volume should be in partition %d instead of %d (vldb)\n",
5591                 ropartition, partition);
5592     }
5593
5594     if (rwserver) {
5595         fprintf(STDERR,
5596                 "VLDB indicates that a RW volume exists already on %s in partition %s.\n",
5597                 hostutil_GetNameByINet(rwserver),
5598                 volutil_PartitionName(rwpartition));
5599         if (!force) {
5600             fprintf(STDERR, "Overwrite this VLDB entry? [y|n] (n)\n");
5601             dc = c = getchar();
5602             while (!(dc == EOF || dc == '\n'))
5603                 dc = getchar(); /* goto end of line */
5604             if ((c != 'y') && (c != 'Y')) {
5605                 fprintf(STDERR, "aborted.\n");
5606                 return -1;
5607             }
5608         }
5609     }
5610
5611     vcode =
5612         ubik_VL_SetLock(cstruct, 0, entry.volumeId[RWVOL], RWVOL,
5613                   VLOP_MOVE);
5614     if (vcode) {
5615         fprintf(STDERR,
5616                 "Unable to lock volume %lu, code %d\n",
5617                 (unsigned long)entry.volumeId[RWVOL],vcode);
5618         PrintError("", vcode);
5619         return -1;
5620     }
5621
5622     /* make sure the VLDB entry hasn't changed since we started */
5623     memset(&checkEntry, 0, sizeof(checkEntry));
5624     vcode = VLDB_GetEntryByID(volid, -1, &checkEntry);
5625     if (vcode) {
5626         fprintf(STDERR,
5627                 "Could not fetch the entry for volume %lu from VLDB\n",
5628                 (unsigned long)volid);
5629         PrintError("convertROtoRW ", vcode);
5630         code = vcode;
5631         goto error_exit;
5632     }
5633
5634     MapHostToNetwork(&checkEntry);
5635     entry.flags &= ~VLOP_ALLOPERS;  /* clear any stale lock operation flags */
5636     entry.flags |= VLOP_MOVE;        /* set to match SetLock operation above */
5637     if (memcmp(&entry, &checkEntry, sizeof(entry)) != 0) {
5638         fprintf(STDERR,
5639                 "VLDB entry for volume %lu has changed; please reissue the command.\n",
5640                 (unsigned long)volid);
5641         code = -1;
5642         goto error_exit;
5643     }
5644
5645     aconn = UV_Bind(server, AFSCONF_VOLUMEPORT);
5646     code = AFSVolConvertROtoRWvolume(aconn, partition, volid);
5647     if (code) {
5648         fprintf(STDERR,
5649                 "Converting RO volume %lu to RW volume failed with code %d\n",
5650                 (unsigned long)volid, code);
5651         PrintError("convertROtoRW ", code);
5652         goto error_exit;
5653     }
5654     /* Update the VLDB to match what we did on disk as much as possible.  */
5655     /* If the converted RO was in the VLDB, make it look like the new RW. */
5656     if (roserver) {
5657         entry.serverFlags[roindex] = ITSRWVOL;
5658     } else {
5659         /* Add a new site entry for the newly created RW.  It's possible
5660          * (but unlikely) that we are already at MAXNSERVERS and that this
5661          * new site will invalidate the whole VLDB entry;  however,
5662          * VLDB_ReplaceEntry will detect this and return VL_BADSERVER,
5663          * so we need no extra guard logic here.
5664          */
5665         afs_int32 newrwindex = entry.nServers;
5666         (entry.nServers)++;
5667         entry.serverNumber[newrwindex] = server;
5668         entry.serverPartition[newrwindex] = partition;
5669         entry.serverFlags[newrwindex] = ITSRWVOL;
5670     }
5671     entry.flags |= RW_EXISTS;
5672     entry.flags &= ~BACK_EXISTS;
5673
5674     /* if the old RW was in the VLDB, remove it by decrementing the number */
5675     /* of servers, replacing the RW entry with the last entry, and zeroing */
5676     /* out the last entry. */
5677     if (rwserver) {
5678         (entry.nServers)--;
5679         if (rwindex != entry.nServers) {
5680             entry.serverNumber[rwindex] = entry.serverNumber[entry.nServers];
5681             entry.serverPartition[rwindex] =
5682                 entry.serverPartition[entry.nServers];
5683             entry.serverFlags[rwindex] = entry.serverFlags[entry.nServers];
5684             entry.serverNumber[entry.nServers] = 0;
5685             entry.serverPartition[entry.nServers] = 0;
5686             entry.serverFlags[entry.nServers] = 0;
5687         }
5688     }
5689     entry.flags &= ~RO_EXISTS;
5690     for (i = 0; i < entry.nServers; i++) {
5691         if (entry.serverFlags[i] & ITSROVOL) {
5692             if (!(entry.serverFlags[i] & (RO_DONTUSE | NEW_REPSITE)))
5693                 entry.flags |= RO_EXISTS;
5694         }
5695     }
5696     MapNetworkToHost(&entry, &storeEntry);
5697     code =
5698         VLDB_ReplaceEntry(entry.volumeId[RWVOL], RWVOL, &storeEntry,
5699                           (LOCKREL_OPCODE | LOCKREL_AFSID |
5700                            LOCKREL_TIMESTAMP));
5701     if (code) {
5702         fprintf(STDERR,
5703                 "Warning: volume converted, but vldb update failed with code %d!\n",
5704                 code);
5705     }
5706
5707   error_exit:
5708     vcode = UV_LockRelease(entry.volumeId[RWVOL]);
5709     if (vcode) {
5710         fprintf(STDERR,
5711                 "Unable to unlock volume %lu, code %d\n",
5712                 (unsigned long)entry.volumeId[RWVOL],vcode);
5713         PrintError("", vcode);
5714     }
5715     return code;
5716 }
5717
5718 static int
5719 Sizes(struct cmd_syndesc *as, void *arock)
5720 {
5721     afs_uint32 avolid;
5722     afs_uint32 aserver;
5723     afs_int32 apart, voltype, fromdate = 0, code, err, i;
5724     struct nvldbentry entry;
5725     volintSize vol_size;
5726
5727     rx_SetRxDeadTime(60 * 10);
5728     for (i = 0; i < MAXSERVERS; i++) {
5729         struct rx_connection *rxConn = ubik_GetRPCConn(cstruct, i);
5730         if (rxConn == 0)
5731             break;
5732         rx_SetConnDeadTime(rxConn, rx_connDeadTime);
5733         if (rx_ServiceOf(rxConn))
5734             rx_ServiceOf(rxConn)->connDeadTime = rx_connDeadTime;
5735     }
5736
5737     avolid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
5738     if (avolid == 0) {
5739         if (err)
5740             PrintError("", err);
5741         else
5742             fprintf(STDERR, "vos: can't find volume '%s'\n",
5743                     as->parms[0].items->data);
5744         return ENOENT;
5745     }
5746
5747     if (as->parms[1].items || as->parms[2].items) {
5748         if (!as->parms[1].items || !as->parms[2].items) {
5749             fprintf(STDERR,
5750                     "Must specify both -server and -partition options\n");
5751             return -1;
5752         }
5753         aserver = GetServer(as->parms[2].items->data);
5754         if (aserver == 0) {
5755             fprintf(STDERR, "Invalid server name\n");
5756             return -1;
5757         }
5758         apart = volutil_GetPartitionID(as->parms[1].items->data);
5759         if (apart < 0) {
5760             fprintf(STDERR, "Invalid partition name\n");
5761             return -1;
5762         }
5763     } else {
5764         code = GetVolumeInfo(avolid, &aserver, &apart, &voltype, &entry);
5765         if (code)
5766             return code;
5767     }
5768
5769     fromdate = 0;
5770
5771     if (as->parms[4].items && strcmp(as->parms[4].items->data, "0")) {
5772         code = ktime_DateToInt32(as->parms[4].items->data, &fromdate);
5773         if (code) {
5774             fprintf(STDERR, "vos: failed to parse date '%s' (error=%d))\n",
5775                     as->parms[4].items->data, code);
5776             return code;
5777         }
5778     }
5779
5780     fprintf(STDOUT, "Volume: %s\n", as->parms[0].items->data);
5781
5782     if (as->parms[3].items) {   /* do the dump estimate */
5783         vol_size.dump_size = 0;
5784         code = UV_GetSize(avolid, aserver, apart, fromdate, &vol_size);
5785         if (code) {
5786             PrintDiagnostics("size", code);
5787             return code;
5788         }
5789         /* presumably the size info is now gathered in pntr */
5790         /* now we display it */
5791
5792         fprintf(STDOUT, "dump_size: %llu\n", vol_size.dump_size);
5793     }
5794
5795     /* Display info */
5796
5797     return 0;
5798 }
5799
5800 static int
5801 EndTrans(struct cmd_syndesc *as, void *arock)
5802 {
5803     afs_uint32 server;
5804     afs_int32 code, tid, rcode;
5805     struct rx_connection *aconn;
5806
5807     server = GetServer(as->parms[0].items->data);
5808     if (!server) {
5809         fprintf(STDERR, "vos: host '%s' not found in host table\n",
5810                 as->parms[0].items->data);
5811         return EINVAL;
5812     }
5813
5814     code = util_GetInt32(as->parms[1].items->data, &tid);
5815     if (code) {
5816         fprintf(STDERR, "vos: bad integer specified for transaction ID.\n");
5817         return code;
5818     }
5819
5820     aconn = UV_Bind(server, AFSCONF_VOLUMEPORT);
5821     code = AFSVolEndTrans(aconn, tid, &rcode);
5822     if (!code) {
5823         code = rcode;
5824     }
5825
5826     if (code) {
5827         PrintDiagnostics("endtrans", code);
5828         return 1;
5829     }
5830
5831     return 0;
5832 }
5833
5834 int
5835 PrintDiagnostics(char *astring, afs_int32 acode)
5836 {
5837     if (acode == EACCES) {
5838         fprintf(STDERR,
5839                 "You are not authorized to perform the 'vos %s' command (%d)\n",
5840                 astring, acode);
5841     } else {
5842         fprintf(STDERR, "Error in vos %s command.\n", astring);
5843         PrintError("", acode);
5844     }
5845     return 0;
5846 }
5847
5848
5849 #ifdef AFS_NT40_ENV
5850 static DWORD
5851 win32_enableCrypt(void)
5852 {
5853     HKEY parmKey;
5854     DWORD dummyLen;
5855     DWORD cryptall = 0;
5856     DWORD code;
5857
5858     /* Look up configuration parameters in Registry */
5859     code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
5860                         0, (IsWow64()?KEY_WOW64_64KEY:0)|KEY_QUERY_VALUE, &parmKey);
5861     if (code != ERROR_SUCCESS) {
5862         dummyLen = sizeof(cryptall);
5863         RegQueryValueEx(parmKey, "SecurityLevel", NULL, NULL,
5864                         (BYTE *) &cryptall, &dummyLen);
5865     }
5866     RegCloseKey (parmKey);
5867
5868     return cryptall;
5869 }
5870 #endif /* AFS_NT40_ENV */
5871
5872 static int
5873 MyBeforeProc(struct cmd_syndesc *as, void *arock)
5874 {
5875     char *tcell;
5876     afs_int32 code;
5877     int secFlags;
5878
5879     /* Initialize the ubik_client connection */
5880     rx_SetRxDeadTime(90);
5881     cstruct = NULL;
5882     secFlags = AFSCONF_SECOPTS_FALLBACK_NULL;
5883
5884     tcell = NULL;
5885     if (as->parms[12].items)    /* if -cell specified */
5886         tcell = as->parms[12].items->data;
5887
5888     if (as->parms[13].items)
5889         secFlags |= AFSCONF_SECOPTS_NOAUTH;
5890
5891     if (as->parms[14].items) {  /* -localauth specified */
5892         secFlags |= AFSCONF_SECOPTS_LOCALAUTH;
5893         confdir = AFSDIR_SERVER_ETC_DIRPATH;
5894     }
5895
5896     if (as->parms[16].items     /* -encrypt specified */
5897 #ifdef AFS_NT40_ENV
5898         || win32_enableCrypt()
5899 #endif /* AFS_NT40_ENV */
5900          )
5901         secFlags |= AFSCONF_SECOPTS_ALWAYSENCRYPT;
5902
5903     if (as->parms[18].items)   /* -config flag set */
5904         confdir = as->parms[18].items->data;
5905
5906     if ((code = vsu_ClientInit(confdir, tcell, secFlags, UV_SetSecurity,
5907                                &cstruct))) {
5908         fprintf(STDERR, "could not initialize VLDB library (code=%lu) \n",
5909                 (unsigned long)code);
5910         exit(1);
5911     }
5912     rxInitDone = 1;
5913     if (as->parms[15].items)    /* -verbose flag set */
5914         verbose = 1;
5915     else
5916         verbose = 0;
5917     if (as->parms[17].items)    /* -noresolve flag set */
5918         noresolve = 1;
5919     else
5920         noresolve = 0;
5921
5922     return 0;
5923 }
5924
5925 int
5926 osi_audit(void)
5927 {
5928 /* this sucks but it works for now.
5929 */
5930     return 0;
5931 }
5932
5933 #include "AFS_component_version_number.c"
5934
5935 int
5936 main(int argc, char **argv)
5937 {
5938     afs_int32 code;
5939
5940     struct cmd_syndesc *ts;
5941
5942 #ifdef  AFS_AIX32_ENV
5943     /*
5944      * The following signal action for AIX is necessary so that in case of a
5945      * crash (i.e. core is generated) we can include the user's data section
5946      * in the core dump. Unfortunately, by default, only a partial core is
5947      * generated which, in many cases, isn't too useful.
5948      */
5949     struct sigaction nsa;
5950
5951     sigemptyset(&nsa.sa_mask);
5952     nsa.sa_handler = SIG_DFL;
5953     nsa.sa_flags = SA_FULLDUMP;
5954     sigaction(SIGSEGV, &nsa, NULL);
5955 #endif
5956
5957     confdir = AFSDIR_CLIENT_ETC_DIRPATH;
5958
5959     cmd_SetBeforeProc(MyBeforeProc, NULL);
5960
5961     ts = cmd_CreateSyntax("create", CreateVolume, NULL, "create a new volume");
5962     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
5963     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "partition name");
5964     cmd_AddParm(ts, "-name", CMD_SINGLE, 0, "volume name");
5965     cmd_AddParm(ts, "-maxquota", CMD_SINGLE, CMD_OPTIONAL,
5966                 "initial quota (KB)");
5967     cmd_AddParm(ts, "-id", CMD_SINGLE, CMD_OPTIONAL, "volume ID");
5968     cmd_AddParm(ts, "-roid", CMD_SINGLE, CMD_OPTIONAL, "readonly volume ID");
5969 #ifdef notdef
5970     cmd_AddParm(ts, "-minquota", CMD_SINGLE, CMD_OPTIONAL, "");
5971 #endif
5972     COMMONPARMS;
5973
5974     ts = cmd_CreateSyntax("remove", DeleteVolume, NULL, "delete a volume");
5975     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "machine name");
5976     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
5977     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
5978
5979     COMMONPARMS;
5980
5981     ts = cmd_CreateSyntax("move", MoveVolume, NULL, "move a volume");
5982     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
5983     cmd_AddParm(ts, "-fromserver", CMD_SINGLE, 0, "machine name on source");
5984     cmd_AddParm(ts, "-frompartition", CMD_SINGLE, 0,
5985                 "partition name on source");
5986     cmd_AddParm(ts, "-toserver", CMD_SINGLE, 0,
5987                 "machine name on destination");
5988     cmd_AddParm(ts, "-topartition", CMD_SINGLE, 0,
5989                 "partition name on destination");
5990     cmd_AddParm(ts, "-live", CMD_FLAG, CMD_OPTIONAL,
5991                 "copy live volume without cloning");
5992     COMMONPARMS;
5993
5994     ts = cmd_CreateSyntax("copy", CopyVolume, NULL, "copy a volume");
5995     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID on source");
5996     cmd_AddParm(ts, "-fromserver", CMD_SINGLE, 0, "machine name on source");
5997     cmd_AddParm(ts, "-frompartition", CMD_SINGLE, 0,
5998                 "partition name on source");
5999     cmd_AddParm(ts, "-toname", CMD_SINGLE, 0, "volume name on destination");
6000     cmd_AddParm(ts, "-toserver", CMD_SINGLE, 0,
6001                 "machine name on destination");
6002     cmd_AddParm(ts, "-topartition", CMD_SINGLE, 0,
6003                 "partition name on destination");
6004     cmd_AddParm(ts, "-offline", CMD_FLAG, CMD_OPTIONAL,
6005                 "leave new volume offline");
6006     cmd_AddParm(ts, "-readonly", CMD_FLAG, CMD_OPTIONAL,
6007                 "make new volume read-only");
6008     cmd_AddParm(ts, "-live", CMD_FLAG, CMD_OPTIONAL,
6009                 "copy live volume without cloning");
6010     COMMONPARMS;
6011
6012     ts = cmd_CreateSyntax("shadow", ShadowVolume, NULL,
6013                           "make or update a shadow volume");
6014     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID on source");
6015     cmd_AddParm(ts, "-fromserver", CMD_SINGLE, 0, "machine name on source");
6016     cmd_AddParm(ts, "-frompartition", CMD_SINGLE, 0,
6017                 "partition name on source");
6018     cmd_AddParm(ts, "-toserver", CMD_SINGLE, 0,
6019                 "machine name on destination");
6020     cmd_AddParm(ts, "-topartition", CMD_SINGLE, 0,
6021                 "partition name on destination");
6022     cmd_AddParm(ts, "-toname", CMD_SINGLE, CMD_OPTIONAL,
6023                 "volume name on destination");
6024     cmd_AddParm(ts, "-toid", CMD_SINGLE, CMD_OPTIONAL,
6025                 "volume ID on destination");
6026     cmd_AddParm(ts, "-offline", CMD_FLAG, CMD_OPTIONAL,
6027                 "leave shadow volume offline");
6028     cmd_AddParm(ts, "-readonly", CMD_FLAG, CMD_OPTIONAL,
6029                 "make shadow volume read-only");
6030     cmd_AddParm(ts, "-live", CMD_FLAG, CMD_OPTIONAL,
6031                 "copy live volume without cloning");
6032     cmd_AddParm(ts, "-incremental", CMD_FLAG, CMD_OPTIONAL,
6033                 "do incremental update if target exists");
6034     COMMONPARMS;
6035
6036     ts = cmd_CreateSyntax("backup", BackupVolume, NULL,
6037                           "make backup of a volume");
6038     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6039     COMMONPARMS;
6040
6041     ts = cmd_CreateSyntax("clone", CloneVolume, NULL,
6042                           "make clone of a volume");
6043     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6044     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "server");
6045     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition");
6046     cmd_AddParm(ts, "-toname", CMD_SINGLE, CMD_OPTIONAL,
6047                 "volume name on destination");
6048     cmd_AddParm(ts, "-toid", CMD_SINGLE, CMD_OPTIONAL,
6049                 "volume ID on destination");
6050     cmd_AddParm(ts, "-offline", CMD_FLAG, CMD_OPTIONAL,
6051                 "leave clone volume offline");
6052     cmd_AddParm(ts, "-readonly", CMD_FLAG, CMD_OPTIONAL,
6053                 "make clone volume read-only, not readwrite");
6054     cmd_AddParm(ts, "-readwrite", CMD_FLAG, CMD_OPTIONAL,
6055                 "make clone volume readwrite, not read-only");
6056     COMMONPARMS;
6057
6058     ts = cmd_CreateSyntax("release", ReleaseVolume, NULL, "release a volume");
6059     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6060     cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL,
6061                 "force a complete release");
6062     cmd_AddParm(ts, "-stayonline", CMD_FLAG, CMD_OPTIONAL,
6063                 "release to cloned temp vol, then clone back to repsite RO");
6064     COMMONPARMS;
6065
6066     ts = cmd_CreateSyntax("dump", DumpVolumeCmd, NULL, "dump a volume");
6067     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6068     cmd_AddParm(ts, "-time", CMD_SINGLE, CMD_OPTIONAL, "dump from time");
6069     cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL, "dump file");
6070     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "server");
6071     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition");
6072     cmd_AddParm(ts, "-clone", CMD_FLAG, CMD_OPTIONAL,
6073                 "dump a clone of the volume");
6074     cmd_AddParm(ts, "-omitdirs", CMD_FLAG, CMD_OPTIONAL,
6075                 "omit unchanged directories from an incremental dump");
6076     COMMONPARMS;
6077
6078     ts = cmd_CreateSyntax("restore", RestoreVolumeCmd, NULL,
6079                           "restore a volume");
6080     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
6081     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "partition name");
6082     cmd_AddParm(ts, "-name", CMD_SINGLE, 0, "name of volume to be restored");
6083     cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL, "dump file");
6084     cmd_AddParm(ts, "-id", CMD_SINGLE, CMD_OPTIONAL, "volume ID");
6085     cmd_AddParm(ts, "-overwrite", CMD_SINGLE, CMD_OPTIONAL,
6086                 "abort | full | incremental");
6087     cmd_AddParm(ts, "-offline", CMD_FLAG, CMD_OPTIONAL,
6088                 "leave restored volume offline");
6089     cmd_AddParm(ts, "-readonly", CMD_FLAG, CMD_OPTIONAL,
6090                 "make restored volume read-only");
6091     cmd_AddParm(ts, "-creation", CMD_SINGLE, CMD_OPTIONAL,
6092                 "dump | keep | new");
6093     cmd_AddParm(ts, "-lastupdate", CMD_SINGLE, CMD_OPTIONAL,
6094                 "dump | keep | new");
6095     cmd_AddParm(ts, "-nodelete", CMD_FLAG, CMD_OPTIONAL,
6096                 "do not delete old site when restoring to a new site");
6097     COMMONPARMS;
6098
6099     ts = cmd_CreateSyntax("unlock", LockReleaseCmd, NULL,
6100                           "release lock on VLDB entry for a volume");
6101     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6102     COMMONPARMS;
6103
6104     ts = cmd_CreateSyntax("changeloc", ChangeLocation, NULL,
6105                           "change an RW volume's location in the VLDB");
6106     cmd_AddParm(ts, "-server", CMD_SINGLE, 0,
6107                 "machine name for new location");
6108     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0,
6109                 "partition name for new location");
6110     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6111     COMMONPARMS;
6112
6113     ts = cmd_CreateSyntax("addsite", AddSite, NULL, "add a replication site");
6114     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name for new site");
6115     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0,
6116                 "partition name for new site");
6117     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6118     cmd_AddParm(ts, "-roid", CMD_SINGLE, CMD_OPTIONAL, "volume name or ID for RO");
6119     cmd_AddParm(ts, "-valid", CMD_FLAG, CMD_OPTIONAL, "publish as an up-to-date site in VLDB");
6120     COMMONPARMS;
6121
6122     ts = cmd_CreateSyntax("remsite", RemoveSite, NULL,
6123                           "remove a replication site");
6124     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
6125     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "partition name");
6126     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6127     COMMONPARMS;
6128
6129     ts = cmd_CreateSyntax("listpart", ListPartitions, NULL, "list partitions");
6130     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
6131     COMMONPARMS;
6132
6133     ts = cmd_CreateSyntax("listvol", ListVolumes, NULL,
6134                           "list volumes on server (bypass VLDB)");
6135     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
6136     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
6137     cmd_AddParm(ts, "-fast", CMD_FLAG, CMD_OPTIONAL, "minimal listing");
6138     cmd_AddParm(ts, "-long", CMD_FLAG, CMD_OPTIONAL,
6139                 "list all normal volume fields");
6140     cmd_AddParm(ts, "-quiet", CMD_FLAG, CMD_OPTIONAL,
6141                 "generate minimal information");
6142     cmd_AddParm(ts, "-extended", CMD_FLAG, CMD_OPTIONAL,
6143                 "list extended volume fields");
6144     cmd_AddParm(ts, "-format", CMD_FLAG, CMD_OPTIONAL,
6145                 "machine readable format");
6146     COMMONPARMS;
6147
6148     ts = cmd_CreateSyntax("syncvldb", SyncVldb, NULL,
6149                           "synchronize VLDB with server");
6150     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "machine name");
6151     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
6152     cmd_AddParm(ts, "-volume", CMD_SINGLE, CMD_OPTIONAL, "volume name or ID");
6153     cmd_AddParm(ts, "-dryrun", CMD_FLAG, CMD_OPTIONAL, "list what would be done, don't do it");
6154     COMMONPARMS;
6155
6156     ts = cmd_CreateSyntax("syncserv", SyncServer, NULL,
6157                           "synchronize server with VLDB");
6158     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
6159     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
6160     cmd_AddParm(ts, "-dryrun", CMD_FLAG, CMD_OPTIONAL, "list what would be done, don't do it");
6161     COMMONPARMS;
6162
6163     ts = cmd_CreateSyntax("examine", ExamineVolume, NULL,
6164                           "everything about the volume");
6165     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6166     cmd_AddParm(ts, "-extended", CMD_FLAG, CMD_OPTIONAL,
6167                 "list extended volume fields");
6168     cmd_AddParm(ts, "-format", CMD_FLAG, CMD_OPTIONAL,
6169                 "machine readable format");
6170     COMMONPARMS;
6171     cmd_CreateAlias(ts, "volinfo");
6172
6173     ts = cmd_CreateSyntax("setfields", SetFields, NULL,
6174                           "change volume info fields");
6175     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6176     cmd_AddParm(ts, "-maxquota", CMD_SINGLE, CMD_OPTIONAL, "quota (KB)");
6177     cmd_AddParm(ts, "-clearuse", CMD_FLAG, CMD_OPTIONAL, "clear dayUse");
6178     cmd_AddParm(ts, "-clearVolUpCounter", CMD_FLAG, CMD_OPTIONAL, "clear volUpdateCounter");
6179     COMMONPARMS;
6180
6181     ts = cmd_CreateSyntax("offline", volOffline, NULL, "force the volume status to offline");
6182     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "server name");
6183     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "partition name");
6184     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6185     cmd_AddParm(ts, "-sleep", CMD_SINGLE, CMD_OPTIONAL, "seconds to sleep");
6186     cmd_AddParm(ts, "-busy", CMD_FLAG, CMD_OPTIONAL, "busy volume");
6187     COMMONPARMS;
6188
6189     ts = cmd_CreateSyntax("online", volOnline, NULL, "force the volume status to online");
6190     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "server name");
6191     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "partition name");
6192     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6193     COMMONPARMS;
6194
6195     ts = cmd_CreateSyntax("zap", VolumeZap, NULL,
6196                           "delete the volume, don't bother with VLDB");
6197     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
6198     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "partition name");
6199     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume ID");
6200     cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL,
6201                 "force deletion of bad volumes");
6202     cmd_AddParm(ts, "-backup", CMD_FLAG, CMD_OPTIONAL,
6203                 "also delete backup volume if one is found");
6204     COMMONPARMS;
6205
6206     ts = cmd_CreateSyntax("status", VolserStatus, NULL,
6207                           "report on volser status");
6208     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
6209     COMMONPARMS;
6210
6211     ts = cmd_CreateSyntax("rename", RenameVolume, NULL, "rename a volume");
6212     cmd_AddParm(ts, "-oldname", CMD_SINGLE, 0, "old volume name ");
6213     cmd_AddParm(ts, "-newname", CMD_SINGLE, 0, "new volume name ");
6214     COMMONPARMS;
6215
6216     ts = cmd_CreateSyntax("listvldb", ListVLDB, NULL,
6217                           "list volumes in the VLDB");
6218     cmd_AddParm(ts, "-name", CMD_SINGLE, CMD_OPTIONAL, "volume name or ID");
6219     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "machine name");
6220     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
6221     cmd_AddParm(ts, "-locked", CMD_FLAG, CMD_OPTIONAL, "locked volumes only");
6222     cmd_AddParm(ts, "-quiet", CMD_FLAG, CMD_OPTIONAL,
6223                 "generate minimal information");
6224     cmd_AddParm(ts, "-nosort", CMD_FLAG, CMD_OPTIONAL,
6225                 "do not alphabetically sort the volume names");
6226     COMMONPARMS;
6227
6228     ts = cmd_CreateSyntax("backupsys", BackSys, NULL, "en masse backups");
6229     cmd_AddParm(ts, "-prefix", CMD_LIST, CMD_OPTIONAL,
6230                 "common prefix on volume(s)");
6231     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "machine name");
6232     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
6233     cmd_AddParm(ts, "-exclude", CMD_FLAG, CMD_OPTIONAL,
6234                 "exclude common prefix volumes");
6235     cmd_AddParm(ts, "-xprefix", CMD_LIST, CMD_OPTIONAL,
6236                 "negative prefix on volume(s)");
6237     cmd_AddParm(ts, "-dryrun", CMD_FLAG, CMD_OPTIONAL, "list what would be done, don't do it");
6238     COMMONPARMS;
6239
6240     ts = cmd_CreateSyntax("delentry", DeleteEntry, NULL,
6241                           "delete VLDB entry for a volume");
6242     cmd_AddParm(ts, "-id", CMD_LIST, CMD_OPTIONAL, "volume name or ID");
6243     cmd_AddParm(ts, "-prefix", CMD_SINGLE, CMD_OPTIONAL,
6244                 "prefix of the volume whose VLDB entry is to be deleted");
6245     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "machine name");
6246     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
6247     cmd_AddParm(ts, "-noexecute", CMD_FLAG, CMD_OPTIONAL|CMD_HIDDEN, "");
6248     cmd_AddParm(ts, "-dryrun", CMD_FLAG, CMD_OPTIONAL,
6249                 "list what would be done, don't do it");
6250     COMMONPARMS;
6251
6252     ts = cmd_CreateSyntax("partinfo", PartitionInfo, NULL,
6253                           "list partition information");
6254     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
6255     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
6256     cmd_AddParm(ts, "-summary", CMD_FLAG, CMD_OPTIONAL,
6257                 "print storage summary");
6258     COMMONPARMS;
6259
6260     ts = cmd_CreateSyntax("unlockvldb", UnlockVLDB, NULL,
6261                           "unlock all the locked entries in the VLDB");
6262     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "machine name");
6263     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
6264     COMMONPARMS;
6265
6266     ts = cmd_CreateSyntax("lock", LockEntry, NULL,
6267                           "lock VLDB entry for a volume");
6268     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6269     COMMONPARMS;
6270
6271     ts = cmd_CreateSyntax("changeaddr", ChangeAddr, NULL,
6272                           "change the IP address of a file server");
6273     cmd_AddParm(ts, "-oldaddr", CMD_SINGLE, 0, "original IP address");
6274     cmd_AddParm(ts, "-newaddr", CMD_SINGLE, CMD_OPTIONAL, "new IP address");
6275     cmd_AddParm(ts, "-remove", CMD_FLAG, CMD_OPTIONAL,
6276                 "remove the IP address from the VLDB");
6277     COMMONPARMS;
6278
6279     ts = cmd_CreateSyntax("listaddrs", ListAddrs, NULL,
6280                           "list the IP address of all file servers registered in the VLDB");
6281     cmd_AddParm(ts, "-uuid", CMD_SINGLE, CMD_OPTIONAL, "uuid of server");
6282     cmd_AddParm(ts, "-host", CMD_SINGLE, CMD_OPTIONAL, "address of host");
6283     cmd_AddParm(ts, "-printuuid", CMD_FLAG, CMD_OPTIONAL,
6284                 "print uuid of hosts");
6285     COMMONPARMS;
6286
6287     ts = cmd_CreateSyntax("convertROtoRW", ConvertRO, NULL,
6288                           "convert a RO volume into a RW volume (after loss of old RW volume)");
6289     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
6290     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "partition name");
6291     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6292     cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL, "don't ask");
6293     COMMONPARMS;
6294
6295     ts = cmd_CreateSyntax("size", Sizes, NULL,
6296                           "obtain various sizes of the volume.");
6297     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6298     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
6299     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "machine name");
6300     cmd_AddParm(ts, "-dump", CMD_FLAG, CMD_OPTIONAL,
6301                 "Obtain the size of the dump");
6302     cmd_AddParm(ts, "-time", CMD_SINGLE, CMD_OPTIONAL, "dump from time");
6303     COMMONPARMS;
6304
6305     ts = cmd_CreateSyntax("endtrans", EndTrans, NULL,
6306                           "end a volserver transaction");
6307     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
6308     cmd_AddParm(ts, "-transaction", CMD_SINGLE, 0,
6309                 "transaction ID");
6310     COMMONPARMS;
6311
6312     ts = cmd_CreateSyntax("setaddrs", SetAddrs, NULL,
6313                           "set the list of IP address for a given UUID in the VLDB");
6314     cmd_AddParm(ts, "-uuid", CMD_SINGLE, 0, "uuid of server");
6315     cmd_AddParm(ts, "-host", CMD_LIST, 0, "address of host");
6316
6317     COMMONPARMS;
6318     code = cmd_Dispatch(argc, argv);
6319     if (rxInitDone) {
6320         /* Shut down the ubik_client and rx connections */
6321         if (cstruct) {
6322             (void)ubik_ClientDestroy(cstruct);
6323             cstruct = 0;
6324         }
6325         rx_Finalize();
6326     }
6327
6328     exit((code ? -1 : 0));
6329 }