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