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