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