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