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