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