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