openafs-string-header-cleanup-20071030
[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
1459 NukeVolume(as)
1460      register struct cmd_syndesc *as;
1461 {
1462     register afs_int32 code;
1463     afs_int32 volID, err;
1464     afs_int32 partID;
1465     afs_int32 server;
1466     register char *tp;
1467
1468     server = GetServer(tp = as->parms[0].items->data);
1469     if (!server) {
1470         fprintf(STDERR, "vos: server '%s' not found in host table\n", tp);
1471         return 1;
1472     }
1473
1474     partID = volutil_GetPartitionID(tp = as->parms[1].items->data);
1475     if (partID == -1) {
1476         fprintf(STDERR, "vos: could not parse '%s' as a partition name", tp);
1477         return 1;
1478     }
1479
1480     volID = vsu_GetVolumeID(tp = as->parms[2].items->data, cstruct, &err);
1481     if (volID == 0) {
1482         if (err)
1483             PrintError("", err);
1484         else
1485             fprintf(STDERR,
1486                     "vos: could not parse '%s' as a numeric volume ID", tp);
1487         return 1;
1488     }
1489
1490     fprintf(STDOUT,
1491             "vos: forcibly removing all traces of volume %d, please wait...",
1492             volID);
1493     fflush(STDOUT);
1494     code = UV_NukeVolume(server, partID, volID);
1495     if (code == 0)
1496         fprintf(STDOUT, "done.\n");
1497     else
1498         fprintf(STDOUT, "failed with code %d.\n", code);
1499     return code;
1500 }
1501
1502
1503 /*------------------------------------------------------------------------
1504  * PRIVATE ExamineVolume
1505  *
1506  * Description:
1507  *      Routine used to examine a single volume, contacting the VLDB as
1508  *      well as the Volume Server.
1509  *
1510  * Arguments:
1511  *      as : Ptr to parsed command line arguments.
1512  *
1513  * Returns:
1514  *      0 for a successful operation,
1515  *      Otherwise, one of the ubik or VolServer error values.
1516  *
1517  * Environment:
1518  *      Nothing interesting.
1519  *
1520  * Side Effects:
1521  *      As advertised.
1522  *------------------------------------------------------------------------
1523  */
1524 static
1525 ExamineVolume(as)
1526      register struct cmd_syndesc *as;
1527 {
1528     struct nvldbentry entry;
1529     afs_int32 vcode = 0;
1530     volintInfo *pntr = (volintInfo *) 0;
1531     volintXInfo *xInfoP = (volintXInfo *) 0;
1532     afs_int32 volid;
1533     afs_int32 code, err, error = 0;
1534     int voltype, foundserv = 0, foundentry = 0;
1535     afs_int32 aserver, apart;
1536     int previdx = -1;
1537     int wantExtendedInfo;       /*Do we want extended vol info? */
1538
1539     wantExtendedInfo = (as->parms[1].items ? 1 : 0);    /* -extended */
1540
1541     volid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);   /* -id */
1542     if (volid == 0) {
1543         if (err)
1544             PrintError("", err);
1545         else
1546             fprintf(STDERR, "Unknown volume ID or name '%s'\n",
1547                     as->parms[0].items->data);
1548         return -1;
1549     }
1550
1551     if (verbose) {
1552         fprintf(STDOUT, "Fetching VLDB entry for %lu .. ",
1553                 (unsigned long)volid);
1554         fflush(STDOUT);
1555     }
1556     vcode = VLDB_GetEntryByID(volid, -1, &entry);
1557     if (vcode) {
1558         fprintf(STDERR,
1559                 "Could not fetch the entry for volume number %lu from VLDB \n",
1560                 (unsigned long)volid);
1561         return (vcode);
1562     }
1563     if (verbose)
1564         fprintf(STDOUT, "done\n");
1565     MapHostToNetwork(&entry);
1566
1567     if (entry.volumeId[RWVOL] == volid)
1568         voltype = RWVOL;
1569     else if (entry.volumeId[BACKVOL] == volid)
1570         voltype = BACKVOL;
1571     else                        /* (entry.volumeId[ROVOL] == volid) */
1572         voltype = ROVOL;
1573
1574     do {                        /* do {...} while (voltype == ROVOL) */
1575         /* Get the entry for the volume. If its a RW vol, get the RW entry.
1576          * It its a BK vol, get the RW entry (even if VLDB may say the BK doen't exist).
1577          * If its a RO vol, get the next RO entry.
1578          */
1579         GetServerAndPart(&entry, ((voltype == ROVOL) ? ROVOL : RWVOL),
1580                          &aserver, &apart, &previdx);
1581         if (previdx == -1) {    /* searched all entries */
1582             if (!foundentry) {
1583                 fprintf(STDERR, "Volume %s does not exist in VLDB\n\n",
1584                         as->parms[0].items->data);
1585                 error = ENOENT;
1586             }
1587             break;
1588         }
1589         foundentry = 1;
1590
1591         /* Get information about the volume from the server */
1592         if (verbose) {
1593             fprintf(STDOUT, "Getting volume listing from the server %s .. ",
1594                     hostutil_GetNameByINet(aserver));
1595             fflush(STDOUT);
1596         }
1597         if (wantExtendedInfo)
1598             code = UV_XListOneVolume(aserver, apart, volid, &xInfoP);
1599         else
1600             code = UV_ListOneVolume(aserver, apart, volid, &pntr);
1601         if (verbose)
1602             fprintf(STDOUT, "done\n");
1603
1604         if (code) {
1605             error = code;
1606             if (code == ENODEV) {
1607                 if ((voltype == BACKVOL) && !(entry.flags & BACK_EXISTS)) {
1608                     /* The VLDB says there is no backup volume and its not on disk */
1609                     fprintf(STDERR, "Volume %s does not exist\n",
1610                             as->parms[0].items->data);
1611                     error = ENOENT;
1612                 } else {
1613                     fprintf(STDERR,
1614                             "Volume does not exist on server %s as indicated by the VLDB\n",
1615                             hostutil_GetNameByINet(aserver));
1616                 }
1617             } else {
1618                 PrintDiagnostics("examine", code);
1619             }
1620             fprintf(STDOUT, "\n");
1621         } else {
1622             foundserv = 1;
1623             if (wantExtendedInfo)
1624                 XVolumeStats(xInfoP, &entry, aserver, apart, voltype);
1625             else
1626 #ifdef FULL_LISTVOL_SWITCH
1627             if (as->parms[2].items) {
1628                 DisplayFormat2(aserver, apart, pntr);
1629                 EnumerateEntry(&entry);
1630             } else
1631 #endif /* FULL_LISTVOL_SWITCH */
1632                 VolumeStats(pntr, &entry, aserver, apart, voltype);
1633
1634             if ((voltype == BACKVOL) && !(entry.flags & BACK_EXISTS)) {
1635                 /* The VLDB says there is no backup volume yet we found one on disk */
1636                 fprintf(STDERR, "Volume %s does not exist in VLDB\n",
1637                         as->parms[0].items->data);
1638                 error = ENOENT;
1639             }
1640         }
1641
1642         if (pntr)
1643             free(pntr);
1644         if (xInfoP)
1645             free(xInfoP);
1646     } while (voltype == ROVOL);
1647
1648     if (!foundserv) {
1649         fprintf(STDERR, "Dump only information from VLDB\n\n");
1650         fprintf(STDOUT, "%s \n", entry.name);   /* PostVolumeStats doesn't print name */
1651     }
1652     PostVolumeStats(&entry);
1653
1654     return (error);
1655 }
1656
1657 /*------------------------------------------------------------------------
1658  * PRIVATE SetFields
1659  *
1660  * Description:
1661  *      Routine used to change the status of a single volume.
1662  *
1663  * Arguments:
1664  *      as : Ptr to parsed command line arguments.
1665  *
1666  * Returns:
1667  *      0 for a successful operation,
1668  *      Otherwise, one of the ubik or VolServer error values.
1669  *
1670  * Environment:
1671  *      Nothing interesting.
1672  *
1673  * Side Effects:
1674  *      As advertised.
1675  *------------------------------------------------------------------------
1676  */
1677 static
1678 SetFields(as)
1679      register struct cmd_syndesc *as;
1680 {
1681     struct nvldbentry entry;
1682     afs_int32 vcode = 0;
1683     volintInfo info;
1684     afs_int32 volid;
1685     afs_int32 code, err;
1686     afs_int32 aserver, apart;
1687     int previdx = -1;
1688
1689     volid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);   /* -id */
1690     if (volid == 0) {
1691         if (err)
1692             PrintError("", err);
1693         else
1694             fprintf(STDERR, "Unknown volume ID or name '%s'\n",
1695                     as->parms[0].items->data);
1696         return -1;
1697     }
1698
1699     code = VLDB_GetEntryByID(volid, RWVOL, &entry);
1700     if (code) {
1701         fprintf(STDERR,
1702                 "Could not fetch the entry for volume number %lu from VLDB \n",
1703                 (unsigned long)volid);
1704         return (code);
1705     }
1706     MapHostToNetwork(&entry);
1707
1708     GetServerAndPart(&entry, RWVOL, &aserver, &apart, &previdx);
1709     if (previdx == -1) {
1710         fprintf(STDERR, "Volume %s does not exist in VLDB\n\n",
1711                 as->parms[0].items->data);
1712         return (ENOENT);
1713     }
1714
1715     init_volintInfo(&info);
1716     info.volid = volid;
1717     info.type = RWVOL;
1718
1719     if (as->parms[1].items) {
1720         /* -max <quota> */
1721         code = util_GetInt32(as->parms[1].items->data, &info.maxquota);
1722         if (code) {
1723             fprintf(STDERR, "invalid quota value\n");
1724             return code;
1725         }
1726     }
1727     if (as->parms[2].items) {
1728         /* -clearuse */
1729         info.dayUse = 0;
1730     }
1731     if (as->parms[3].items) {
1732         /* -clearVolUpCounter */
1733         info.spare2 = 0;
1734     }
1735     code = UV_SetVolumeInfo(aserver, apart, volid, &info);
1736     if (code)
1737         fprintf(STDERR,
1738                 "Could not update volume info fields for volume number %lu\n",
1739                 (unsigned long)volid);
1740     return (code);
1741 }
1742
1743 /*------------------------------------------------------------------------
1744  * PRIVATE volOnline
1745  *
1746  * Description:
1747  *      Brings a volume online.
1748  *
1749  * Arguments:
1750  *      as : Ptr to parsed command line arguments.
1751  *
1752  * Returns:
1753  *      0 for a successful operation,
1754  *
1755  * Environment:
1756  *      Nothing interesting.
1757  *
1758  * Side Effects:
1759  *      As advertised.
1760  *------------------------------------------------------------------------
1761  */
1762 static
1763 volOnline(as)
1764      register struct cmd_syndesc *as;
1765 {
1766     afs_int32 server, partition, volid;
1767     afs_int32 code, err = 0;
1768
1769     server = GetServer(as->parms[0].items->data);
1770     if (server == 0) {
1771         fprintf(STDERR, "vos: server '%s' not found in host table\n",
1772                 as->parms[0].items->data);
1773         return -1;
1774     }
1775
1776     partition = volutil_GetPartitionID(as->parms[1].items->data);
1777     if (partition < 0) {
1778         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
1779                 as->parms[1].items->data);
1780         return ENOENT;
1781     }
1782
1783     volid = vsu_GetVolumeID(as->parms[2].items->data, cstruct, &err);   /* -id */
1784     if (!volid) {
1785         if (err)
1786             PrintError("", err);
1787         else
1788             fprintf(STDERR, "Unknown volume ID or name '%s'\n",
1789                     as->parms[0].items->data);
1790         return -1;
1791     }
1792
1793     code = UV_SetVolume(server, partition, volid, ITOffline, 0 /*online */ ,
1794                         0 /*sleep */ );
1795     if (code) {
1796         fprintf(STDERR, "Failed to set volume. Code = %d\n", code);
1797         return -1;
1798     }
1799
1800     return 0;
1801 }
1802
1803 /*------------------------------------------------------------------------
1804  * PRIVATE volOffline
1805  *
1806  * Description:
1807  *      Brings a volume offline.
1808  *
1809  * Arguments:
1810  *      as : Ptr to parsed command line arguments.
1811  *
1812  * Returns:
1813  *      0 for a successful operation,
1814  *
1815  * Environment:
1816  *      Nothing interesting.
1817  *
1818  * Side Effects:
1819  *      As advertised.
1820  *------------------------------------------------------------------------
1821  */
1822 static int
1823 volOffline(register struct cmd_syndesc *as)
1824 {
1825     afs_int32 server, partition, volid;
1826     afs_int32 code, err = 0;
1827     afs_int32 transflag, sleeptime, transdone;
1828
1829     server = GetServer(as->parms[0].items->data);
1830     if (server == 0) {
1831         fprintf(STDERR, "vos: server '%s' not found in host table\n",
1832                 as->parms[0].items->data);
1833         return -1;
1834     }
1835
1836     partition = volutil_GetPartitionID(as->parms[1].items->data);
1837     if (partition < 0) {
1838         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
1839                 as->parms[1].items->data);
1840         return ENOENT;
1841     }
1842
1843     volid = vsu_GetVolumeID(as->parms[2].items->data, cstruct, &err);   /* -id */
1844     if (!volid) {
1845         if (err)
1846             PrintError("", err);
1847         else
1848             fprintf(STDERR, "Unknown volume ID or name '%s'\n",
1849                     as->parms[0].items->data);
1850         return -1;
1851     }
1852
1853     transflag = (as->parms[4].items ? ITBusy : ITOffline);
1854     sleeptime = (as->parms[3].items ? atol(as->parms[3].items->data) : 0);
1855     transdone = (sleeptime ? 0 /*online */ : VTOutOfService);
1856     if (as->parms[4].items && !as->parms[3].items) {
1857         fprintf(STDERR, "-sleep option must be used with -busy flag\n");
1858         return -1;
1859     }
1860
1861     code =
1862         UV_SetVolume(server, partition, volid, transflag, transdone,
1863                      sleeptime);
1864     if (code) {
1865         fprintf(STDERR, "Failed to set volume. Code = %d\n", code);
1866         return -1;
1867     }
1868
1869     return 0;
1870 }
1871
1872 static int
1873 CreateVolume(register struct cmd_syndesc *as)
1874 {
1875     afs_int32 pnum;
1876     char part[10];
1877     afs_int32 volid, code;
1878     struct nvldbentry entry;
1879     afs_int32 vcode;
1880     afs_int32 quota;
1881
1882     quota = 5000;
1883     tserver = GetServer(as->parms[0].items->data);
1884     if (!tserver) {
1885         fprintf(STDERR, "vos: host '%s' not found in host table\n",
1886                 as->parms[0].items->data);
1887         return ENOENT;
1888     }
1889     pnum = volutil_GetPartitionID(as->parms[1].items->data);
1890     if (pnum < 0) {
1891         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
1892                 as->parms[1].items->data);
1893         return ENOENT;
1894     }
1895     if (!IsPartValid(pnum, tserver, &code)) {   /*check for validity of the partition */
1896         if (code)
1897             PrintError("", code);
1898         else
1899             fprintf(STDERR,
1900                     "vos : partition %s does not exist on the server\n",
1901                     as->parms[1].items->data);
1902         return ENOENT;
1903     }
1904     if (!ISNAMEVALID(as->parms[2].items->data)) {
1905         fprintf(STDERR,
1906                 "vos: the name of the root volume %s exceeds the size limit of %d\n",
1907                 as->parms[2].items->data, VOLSER_OLDMAXVOLNAME - 10);
1908         return E2BIG;
1909     }
1910     if (!VolNameOK(as->parms[2].items->data)) {
1911         fprintf(STDERR,
1912                 "Illegal volume name %s, should not end in .readonly or .backup\n",
1913                 as->parms[2].items->data);
1914         return EINVAL;
1915     }
1916     if (IsNumeric(as->parms[2].items->data)) {
1917         fprintf(STDERR, "Illegal volume name %s, should not be a number\n",
1918                 as->parms[2].items->data);
1919         return EINVAL;
1920     }
1921     vcode = VLDB_GetEntryByName(as->parms[2].items->data, &entry);
1922     if (!vcode) {
1923         fprintf(STDERR, "Volume %s already exists\n",
1924                 as->parms[2].items->data);
1925         PrintDiagnostics("create", code);
1926         return EEXIST;
1927     }
1928
1929     if (as->parms[3].items) {
1930         if (!IsNumeric(as->parms[3].items->data)) {
1931             fprintf(STDERR, "Initial quota %s should be numeric.\n",
1932                     as->parms[3].items->data);
1933             return EINVAL;
1934         }
1935
1936         code = util_GetInt32(as->parms[3].items->data, &quota);
1937         if (code) {
1938             fprintf(STDERR, "vos: bad integer specified for quota.\n");
1939             return code;
1940         }
1941     }
1942
1943     code =
1944         UV_CreateVolume2(tserver, pnum, as->parms[2].items->data, quota, 0,
1945                          0, 0, 0, &volid);
1946     if (code) {
1947         PrintDiagnostics("create", code);
1948         return code;
1949     }
1950     MapPartIdIntoName(pnum, part);
1951     fprintf(STDOUT, "Volume %lu created on partition %s of %s\n",
1952             (unsigned long)volid, part, as->parms[0].items->data);
1953
1954     return 0;
1955 }
1956
1957 static afs_int32
1958 DeleteAll(entry)
1959      struct nvldbentry *entry;
1960 {
1961     int i;
1962     afs_int32 error, code, curserver, curpart, volid;
1963
1964     MapHostToNetwork(entry);
1965     error = 0;
1966     for (i = 0; i < entry->nServers; i++) {
1967         curserver = entry->serverNumber[i];
1968         curpart = entry->serverPartition[i];
1969         if (entry->serverFlags[i] & ITSROVOL) {
1970             volid = entry->volumeId[ROVOL];
1971         } else {
1972             volid = entry->volumeId[RWVOL];
1973         }
1974         code = UV_DeleteVolume(curserver, curpart, volid);
1975         if (code && !error)
1976             error = code;
1977     }
1978     return error;
1979 }
1980
1981 static
1982 DeleteVolume(as)
1983      struct cmd_syndesc *as;
1984 {
1985     afs_int32 err, code = 0;
1986     afs_int32 server = 0, partition = -1, volid;
1987     char pname[10];
1988     afs_int32 idx, j;
1989
1990     if (as->parms[0].items) {
1991         server = GetServer(as->parms[0].items->data);
1992         if (!server) {
1993             fprintf(STDERR, "vos: server '%s' not found in host table\n",
1994                     as->parms[0].items->data);
1995             return ENOENT;
1996         }
1997     }
1998
1999     if (as->parms[1].items) {
2000         partition = volutil_GetPartitionID(as->parms[1].items->data);
2001         if (partition < 0) {
2002             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2003                     as->parms[1].items->data);
2004             return EINVAL;
2005         }
2006
2007         /* Check for validity of the partition */
2008         if (!IsPartValid(partition, server, &code)) {
2009             if (code) {
2010                 PrintError("", code);
2011             } else {
2012                 fprintf(STDERR,
2013                         "vos : partition %s does not exist on the server\n",
2014                         as->parms[1].items->data);
2015             }
2016             return ENOENT;
2017         }
2018     }
2019
2020     volid = vsu_GetVolumeID(as->parms[2].items->data, cstruct, &err);
2021     if (volid == 0) {
2022         fprintf(STDERR, "Can't find volume name '%s' in VLDB\n",
2023                 as->parms[2].items->data);
2024         if (err)
2025             PrintError("", err);
2026         return ENOENT;
2027     }
2028
2029     /* If the server or partition option are not complete, try to fill
2030      * them in from the VLDB entry.
2031      */
2032     if ((partition == -1) || !server) {
2033         struct nvldbentry entry;
2034
2035         code = VLDB_GetEntryByID(volid, -1, &entry);
2036         if (code) {
2037             fprintf(STDERR,
2038                     "Could not fetch the entry for volume %lu from VLDB\n",
2039                     (unsigned long)volid);
2040             PrintError("", code);
2041             return (code);
2042         }
2043
2044         if (((volid == entry.volumeId[RWVOL]) && (entry.flags & RW_EXISTS))
2045             || ((volid == entry.volumeId[BACKVOL])
2046                 && (entry.flags & BACK_EXISTS))) {
2047             idx = Lp_GetRwIndex(&entry);
2048             if ((idx == -1) || (server && (server != entry.serverNumber[idx]))
2049                 || ((partition != -1)
2050                     && (partition != entry.serverPartition[idx]))) {
2051                 fprintf(STDERR, "VLDB: Volume '%s' no match\n",
2052                         as->parms[2].items->data);
2053                 return ENOENT;
2054             }
2055         } else if ((volid == entry.volumeId[ROVOL])
2056                    && (entry.flags & RO_EXISTS)) {
2057             for (idx = -1, j = 0; j < entry.nServers; j++) {
2058                 if (entry.serverFlags[j] != ITSROVOL)
2059                     continue;
2060
2061                 if (((server == 0) || (server == entry.serverNumber[j]))
2062                     && ((partition == -1)
2063                         || (partition == entry.serverPartition[j]))) {
2064                     if (idx != -1) {
2065                         fprintf(STDERR,
2066                                 "VLDB: Volume '%s' matches more than one RO\n",
2067                                 as->parms[2].items->data);
2068                         return ENOENT;
2069                     }
2070                     idx = j;
2071                 }
2072             }
2073             if (idx == -1) {
2074                 fprintf(STDERR, "VLDB: Volume '%s' no match\n",
2075                         as->parms[2].items->data);
2076                 return ENOENT;
2077             }
2078         } else {
2079             fprintf(STDERR, "VLDB: Volume '%s' no match\n",
2080                     as->parms[2].items->data);
2081             return ENOENT;
2082         }
2083
2084         server = htonl(entry.serverNumber[idx]);
2085         partition = entry.serverPartition[idx];
2086     }
2087
2088
2089     code = UV_DeleteVolume(server, partition, volid);
2090     if (code) {
2091         PrintDiagnostics("remove", code);
2092         return code;
2093     }
2094
2095     MapPartIdIntoName(partition, pname);
2096     fprintf(STDOUT, "Volume %lu on partition %s server %s deleted\n",
2097             (unsigned long)volid, pname, hostutil_GetNameByINet(server));
2098     return 0;
2099 }
2100
2101 #define TESTM   0               /* set for move space tests, clear for production */
2102 static
2103 MoveVolume(as)
2104      register struct cmd_syndesc *as;
2105 {
2106
2107     afs_int32 volid, fromserver, toserver, frompart, topart;
2108     afs_int32 flags, code, err;
2109     char fromPartName[10], toPartName[10];
2110
2111     struct diskPartition partition;     /* for space check */
2112     volintInfo *p;
2113
2114     volid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
2115     if (volid == 0) {
2116         if (err)
2117             PrintError("", err);
2118         else
2119             fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2120                     as->parms[0].items->data);
2121         return ENOENT;
2122     }
2123     fromserver = GetServer(as->parms[1].items->data);
2124     if (fromserver == 0) {
2125         fprintf(STDERR, "vos: server '%s' not found in host table\n",
2126                 as->parms[1].items->data);
2127         return ENOENT;
2128     }
2129     toserver = GetServer(as->parms[3].items->data);
2130     if (toserver == 0) {
2131         fprintf(STDERR, "vos: server '%s' not found in host table\n",
2132                 as->parms[3].items->data);
2133         return ENOENT;
2134     }
2135     frompart = volutil_GetPartitionID(as->parms[2].items->data);
2136     if (frompart < 0) {
2137         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2138                 as->parms[2].items->data);
2139         return EINVAL;
2140     }
2141     if (!IsPartValid(frompart, fromserver, &code)) {    /*check for validity of the partition */
2142         if (code)
2143             PrintError("", code);
2144         else
2145             fprintf(STDERR,
2146                     "vos : partition %s does not exist on the server\n",
2147                     as->parms[2].items->data);
2148         return ENOENT;
2149     }
2150     topart = volutil_GetPartitionID(as->parms[4].items->data);
2151     if (topart < 0) {
2152         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2153                 as->parms[4].items->data);
2154         return EINVAL;
2155     }
2156     if (!IsPartValid(topart, toserver, &code)) {        /*check for validity of the partition */
2157         if (code)
2158             PrintError("", code);
2159         else
2160             fprintf(STDERR,
2161                     "vos : partition %s does not exist on the server\n",
2162                     as->parms[4].items->data);
2163         return ENOENT;
2164     }
2165
2166     flags = 0;
2167     if (as->parms[5].items) flags |= RV_NOCLONE;
2168
2169     /*
2170      * check source partition for space to clone volume
2171      */
2172
2173     MapPartIdIntoName(topart, toPartName);
2174     MapPartIdIntoName(frompart, fromPartName);
2175
2176     /*
2177      * check target partition for space to move volume
2178      */
2179
2180     code = UV_PartitionInfo(toserver, toPartName, &partition);
2181     if (code) {
2182         fprintf(STDERR, "vos: cannot access partition %s\n", toPartName);
2183         exit(1);
2184     }
2185     if (TESTM)
2186         fprintf(STDOUT, "target partition %s free space %d\n", toPartName,
2187                 partition.free);
2188
2189     p = (volintInfo *) 0;
2190     code = UV_ListOneVolume(fromserver, frompart, volid, &p);
2191     if (code) {
2192         fprintf(STDERR, "vos:cannot access volume %lu\n",
2193                 (unsigned long)volid);
2194         exit(1);
2195     }
2196     if (TESTM)
2197         fprintf(STDOUT, "volume %lu size %d\n", (unsigned long)volid,
2198                 p->size);
2199     if (partition.free <= p->size) {
2200         fprintf(STDERR,
2201                 "vos: no space on target partition %s to move volume %lu\n",
2202                 toPartName, (unsigned long)volid);
2203         free(p);
2204         exit(1);
2205     }
2206     free(p);
2207
2208     if (TESTM) {
2209         fprintf(STDOUT, "size test - don't do move\n");
2210         exit(0);
2211     }
2212
2213     /* successful move still not guaranteed but shoot for it */
2214
2215     code =
2216         UV_MoveVolume2(volid, fromserver, frompart, toserver, topart, flags);
2217     if (code) {
2218         PrintDiagnostics("move", code);
2219         return code;
2220     }
2221     MapPartIdIntoName(topart, toPartName);
2222     MapPartIdIntoName(frompart, fromPartName);
2223     fprintf(STDOUT, "Volume %lu moved from %s %s to %s %s \n",
2224             (unsigned long)volid, as->parms[1].items->data, fromPartName,
2225             as->parms[3].items->data, toPartName);
2226
2227     return 0;
2228 }
2229
2230 static
2231 CopyVolume(as)
2232      register struct cmd_syndesc *as;
2233 {
2234     afs_int32 volid, fromserver, toserver, frompart, topart, code, err, flags;
2235     char fromPartName[10], toPartName[10], *tovolume;
2236     struct nvldbentry entry;
2237     struct diskPartition partition;     /* for space check */
2238     volintInfo *p;
2239
2240     volid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
2241     if (volid == 0) {
2242         if (err)
2243             PrintError("", err);
2244         else
2245             fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2246                     as->parms[0].items->data);
2247         return ENOENT;
2248     }
2249     fromserver = GetServer(as->parms[1].items->data);
2250     if (fromserver == 0) {
2251         fprintf(STDERR, "vos: server '%s' not found in host table\n",
2252                 as->parms[1].items->data);
2253         return ENOENT;
2254     }
2255
2256     toserver = GetServer(as->parms[4].items->data);
2257     if (toserver == 0) {
2258         fprintf(STDERR, "vos: server '%s' not found in host table\n",
2259                 as->parms[4].items->data);
2260         return ENOENT;
2261     }
2262
2263     tovolume = as->parms[3].items->data;
2264     if (!ISNAMEVALID(tovolume)) {
2265         fprintf(STDERR,
2266                 "vos: the name of the root volume %s exceeds the size limit of %d\n",
2267                 tovolume, VOLSER_OLDMAXVOLNAME - 10);
2268         return E2BIG;
2269     }
2270     if (!VolNameOK(tovolume)) {
2271         fprintf(STDERR,
2272                 "Illegal volume name %s, should not end in .readonly or .backup\n",
2273                 tovolume);
2274         return EINVAL;
2275     }
2276     if (IsNumeric(tovolume)) {
2277         fprintf(STDERR, "Illegal volume name %s, should not be a number\n",
2278                 tovolume);
2279         return EINVAL;
2280     }
2281     code = VLDB_GetEntryByName(tovolume, &entry);
2282     if (!code) {
2283         fprintf(STDERR, "Volume %s already exists\n", tovolume);
2284         PrintDiagnostics("copy", code);
2285         return EEXIST;
2286     }
2287
2288     frompart = volutil_GetPartitionID(as->parms[2].items->data);
2289     if (frompart < 0) {
2290         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2291                 as->parms[2].items->data);
2292         return EINVAL;
2293     }
2294     if (!IsPartValid(frompart, fromserver, &code)) {    /*check for validity of the partition */
2295         if (code)
2296             PrintError("", code);
2297         else
2298             fprintf(STDERR,
2299                     "vos : partition %s does not exist on the server\n",
2300                     as->parms[2].items->data);
2301         return ENOENT;
2302     }
2303
2304     topart = volutil_GetPartitionID(as->parms[5].items->data);
2305     if (topart < 0) {
2306         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2307                 as->parms[5].items->data);
2308         return EINVAL;
2309     }
2310     if (!IsPartValid(topart, toserver, &code)) {        /*check for validity of the partition */
2311         if (code)
2312             PrintError("", code);
2313         else
2314             fprintf(STDERR,
2315                     "vos : partition %s does not exist on the server\n",
2316                     as->parms[5].items->data);
2317         return ENOENT;
2318     }
2319
2320     flags = 0;
2321     if (as->parms[6].items) flags |= RV_OFFLINE;
2322     if (as->parms[7].items) flags |= RV_RDONLY;
2323     if (as->parms[8].items) flags |= RV_NOCLONE;
2324
2325     MapPartIdIntoName(topart, toPartName);
2326     MapPartIdIntoName(frompart, fromPartName);
2327
2328     /*
2329      * check target partition for space to move volume
2330      */
2331
2332     code = UV_PartitionInfo(toserver, toPartName, &partition);
2333     if (code) {
2334         fprintf(STDERR, "vos: cannot access partition %s\n", toPartName);
2335         exit(1);
2336     }
2337     if (TESTM)
2338         fprintf(STDOUT, "target partition %s free space %d\n", toPartName,
2339                 partition.free);
2340
2341     p = (volintInfo *) 0;
2342     code = UV_ListOneVolume(fromserver, frompart, volid, &p);
2343     if (code) {
2344         fprintf(STDERR, "vos:cannot access volume %lu\n",
2345                 (unsigned long)volid);
2346         exit(1);
2347     }
2348
2349     if (partition.free <= p->size) {
2350         fprintf(STDERR,
2351                 "vos: no space on target partition %s to copy volume %lu\n",
2352                 toPartName, (unsigned long)volid);
2353         free(p);
2354         exit(1);
2355     }
2356     free(p);
2357
2358     /* successful copy still not guaranteed but shoot for it */
2359
2360     code =
2361         UV_CopyVolume2(volid, fromserver, frompart, tovolume, toserver,
2362                        topart, 0, flags);
2363     if (code) {
2364         PrintDiagnostics("copy", code);
2365         return code;
2366     }
2367     MapPartIdIntoName(topart, toPartName);
2368     MapPartIdIntoName(frompart, fromPartName);
2369     fprintf(STDOUT, "Volume %lu copied from %s %s to %s on %s %s \n",
2370             (unsigned long)volid, as->parms[1].items->data, fromPartName,
2371             tovolume, as->parms[4].items->data, toPartName);
2372
2373     return 0;
2374 }
2375
2376
2377 static
2378 ShadowVolume(as)
2379      register struct cmd_syndesc *as;
2380 {
2381     afs_int32 volid, fromserver, toserver, frompart, topart, tovolid;
2382     afs_int32 code, err, flags;
2383     char fromPartName[10], toPartName[10], toVolName[32], *tovolume;
2384     struct nvldbentry entry;
2385     struct diskPartition partition;     /* for space check */
2386     volintInfo *p, *q;
2387
2388     p = (volintInfo *) 0;
2389     q = (volintInfo *) 0;
2390
2391     volid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
2392     if (volid == 0) {
2393         if (err)
2394             PrintError("", err);
2395         else
2396             fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2397                     as->parms[0].items->data);
2398         return ENOENT;
2399     }
2400     fromserver = GetServer(as->parms[1].items->data);
2401     if (fromserver == 0) {
2402         fprintf(STDERR, "vos: server '%s' not found in host table\n",
2403                 as->parms[1].items->data);
2404         return ENOENT;
2405     }
2406
2407     toserver = GetServer(as->parms[3].items->data);
2408     if (toserver == 0) {
2409         fprintf(STDERR, "vos: server '%s' not found in host table\n",
2410                 as->parms[3].items->data);
2411         return ENOENT;
2412     }
2413
2414     frompart = volutil_GetPartitionID(as->parms[2].items->data);
2415     if (frompart < 0) {
2416         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2417                 as->parms[2].items->data);
2418         return EINVAL;
2419     }
2420     if (!IsPartValid(frompart, fromserver, &code)) {    /*check for validity of the partition */
2421         if (code)
2422             PrintError("", code);
2423         else
2424             fprintf(STDERR,
2425                     "vos : partition %s does not exist on the server\n",
2426                     as->parms[2].items->data);
2427         return ENOENT;
2428     }
2429
2430     topart = volutil_GetPartitionID(as->parms[4].items->data);
2431     if (topart < 0) {
2432         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2433                 as->parms[4].items->data);
2434         return EINVAL;
2435     }
2436     if (!IsPartValid(topart, toserver, &code)) {        /*check for validity of the partition */
2437         if (code)
2438             PrintError("", code);
2439         else
2440             fprintf(STDERR,
2441                     "vos : partition %s does not exist on the server\n",
2442                     as->parms[4].items->data);
2443         return ENOENT;
2444     }
2445
2446     if (as->parms[5].items) {
2447         tovolume = as->parms[5].items->data;
2448         if (!ISNAMEVALID(tovolume)) {
2449             fprintf(STDERR,
2450                 "vos: the name of the root volume %s exceeds the size limit of %d\n",
2451                 tovolume, VOLSER_OLDMAXVOLNAME - 10);
2452             return E2BIG;
2453         }
2454         if (!VolNameOK(tovolume)) {
2455             fprintf(STDERR,
2456                 "Illegal volume name %s, should not end in .readonly or .backup\n",
2457                 tovolume);
2458             return EINVAL;
2459         }
2460         if (IsNumeric(tovolume)) {
2461             fprintf(STDERR,
2462                 "Illegal volume name %s, should not be a number\n",
2463                 tovolume);
2464             return EINVAL;
2465         }
2466     } else {
2467         /* use actual name of source volume */
2468         code = UV_ListOneVolume(fromserver, frompart, volid, &p);
2469         if (code) {
2470             fprintf(STDERR, "vos:cannot access volume %lu\n",
2471                 (unsigned long)volid);
2472             exit(1);
2473         }
2474         strcpy(toVolName, p->name);
2475         tovolume = toVolName;
2476         /* save p for size checks later */
2477     }
2478
2479     if (as->parms[6].items) {
2480         tovolid = vsu_GetVolumeID(as->parms[6].items->data, cstruct, &err);
2481         if (tovolid == 0) {
2482             if (err)
2483                 PrintError("", err);
2484             else
2485                 fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2486                         as->parms[6].items->data);
2487             if (p)
2488                 free(p);
2489             return ENOENT;
2490         }
2491     } else {
2492         tovolid = vsu_GetVolumeID(tovolume, cstruct, &err);
2493         if (tovolid == 0) {
2494             if (err)
2495                 PrintError("", err);
2496             else
2497                 fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2498                         tovolume);
2499             if (p)
2500                 free(p);
2501             return ENOENT;
2502         }
2503     }
2504
2505     flags = RV_NOVLDB;
2506     if (as->parms[7].items) flags |= RV_OFFLINE;
2507     if (as->parms[8].items) flags |= RV_RDONLY;
2508     if (as->parms[9].items) flags |= RV_NOCLONE;
2509     if (as->parms[10].items) flags |= RV_CPINCR;
2510
2511     MapPartIdIntoName(topart, toPartName);
2512     MapPartIdIntoName(frompart, fromPartName);
2513
2514     /*
2515      * check target partition for space to move volume
2516      */
2517
2518     code = UV_PartitionInfo(toserver, toPartName, &partition);
2519     if (code) {
2520         fprintf(STDERR, "vos: cannot access partition %s\n", toPartName);
2521         exit(1);
2522     }
2523     if (TESTM)
2524         fprintf(STDOUT, "target partition %s free space %d\n", toPartName,
2525                 partition.free);
2526
2527     /* Don't do this again if we did it above */
2528     if (!p) {
2529         code = UV_ListOneVolume(fromserver, frompart, volid, &p);
2530         if (code) {
2531             fprintf(STDERR, "vos:cannot access volume %lu\n",
2532                 (unsigned long)volid);
2533             exit(1);
2534         }
2535     }
2536
2537     /* OK if this fails */
2538     code = UV_ListOneVolume(toserver, topart, tovolid, &q);
2539
2540     /* Treat existing volume size as "free" */
2541     if (q)
2542         p->size = (q->size < p->size) ? p->size - q->size : 0;
2543
2544     if (partition.free <= p->size) {
2545         fprintf(STDERR,
2546                 "vos: no space on target partition %s to copy volume %lu\n",
2547                 toPartName, (unsigned long)volid);
2548         free(p);
2549         if (q) free(q);
2550         exit(1);
2551     }
2552     free(p);
2553     if (q) free(q);
2554
2555     /* successful copy still not guaranteed but shoot for it */
2556
2557     code =
2558         UV_CopyVolume2(volid, fromserver, frompart, tovolume, toserver,
2559                        topart, tovolid, flags);
2560     if (code) {
2561         PrintDiagnostics("shadow", code);
2562         return code;
2563     }
2564     MapPartIdIntoName(topart, toPartName);
2565     MapPartIdIntoName(frompart, fromPartName);
2566     fprintf(STDOUT, "Volume %lu shadowed from %s %s to %s %s \n",
2567             (unsigned long)volid, as->parms[1].items->data, fromPartName,
2568             as->parms[3].items->data, toPartName);
2569
2570     return 0;
2571 }
2572
2573
2574 static
2575 CloneVolume(as)
2576      register struct cmd_syndesc *as;
2577 {
2578     afs_int32 server, part, volid, cloneid, voltype;
2579     char partName[10], *volname;
2580     afs_int32 code, err, flags;
2581     struct nvldbentry entry;
2582
2583     volid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
2584     if (volid == 0) {
2585         if (err)
2586             PrintError("", err);
2587         else
2588             fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2589                     as->parms[0].items->data);
2590         return ENOENT;
2591     }
2592
2593     if (as->parms[1].items || as->parms[2].items) {
2594         if (!as->parms[1].items || !as->parms[2].items) {
2595             fprintf(STDERR,
2596                     "Must specify both -server and -partition options\n");
2597             return -1;
2598         }
2599         server = GetServer(as->parms[1].items->data);
2600         if (server == 0) {
2601             fprintf(STDERR, "vos: server '%s' not found in host table\n",
2602                     as->parms[1].items->data);
2603             return ENOENT;
2604         }
2605         part = volutil_GetPartitionID(as->parms[2].items->data);
2606         if (part < 0) {
2607             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2608                     as->parms[2].items->data);
2609             return EINVAL;
2610         }
2611         if (!IsPartValid(part, server, &code)) {        /*check for validity of the partition */
2612             if (code)
2613                 PrintError("", code);
2614             else
2615                 fprintf(STDERR,
2616                     "vos : partition %s does not exist on the server\n",
2617                     as->parms[2].items->data);
2618             return ENOENT;
2619         }
2620     } else {
2621         code = GetVolumeInfo(volid, &server, &part, &voltype, &entry);
2622         if (code)
2623             return code;
2624     }
2625
2626     volname = 0;
2627     if (as->parms[3].items) {
2628         volname = as->parms[3].items->data;
2629         if (strlen(volname) > VOLSER_OLDMAXVOLNAME - 1) {
2630             fprintf(STDERR,
2631                 "vos: the name of the root volume %s exceeds the size limit of %d\n",
2632                 volname, VOLSER_OLDMAXVOLNAME - 1);
2633             return E2BIG;
2634         }
2635         if (!VolNameOK(volname)) {
2636             fprintf(STDERR,
2637                 "Illegal volume name %s, should not end in .readonly or .backup\n",
2638                 volname);
2639             return EINVAL;
2640         }
2641         if (IsNumeric(volname)) {
2642             fprintf(STDERR,
2643                 "Illegal volume name %s, should not be a number\n",
2644                 volname);
2645             return EINVAL;
2646         }
2647     }
2648
2649     cloneid = 0;
2650     if (as->parms[4].items) {
2651         cloneid = vsu_GetVolumeID(as->parms[4].items->data, cstruct, &err);
2652         if (cloneid == 0) {
2653             if (err)
2654                 PrintError("", err);
2655             else
2656                 fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2657                         as->parms[4].items->data);
2658             return ENOENT;
2659         }
2660     }
2661
2662     flags = 0;
2663     if (as->parms[5].items) flags |= RV_OFFLINE;
2664     if (as->parms[6].items) flags |= RV_RDONLY;
2665
2666
2667     code = 
2668         UV_CloneVolume(server, part, volid, cloneid, volname, flags);
2669
2670     if (code) {
2671         PrintDiagnostics("clone", code);
2672         return code;
2673     }
2674     MapPartIdIntoName(part, partName);
2675     fprintf(STDOUT, "Created clone for volume %lu\n",
2676             as->parms[0].items->data);
2677
2678     return 0;
2679 }
2680
2681
2682 static
2683 BackupVolume(as)
2684      register struct cmd_syndesc *as;
2685 {
2686     afs_int32 avolid, aserver, apart, vtype, code, err;
2687     struct nvldbentry entry;
2688
2689     afs_int32 buvolid, buserver, bupart, butype;
2690     struct nvldbentry buentry;
2691
2692     avolid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
2693     if (avolid == 0) {
2694         if (err)
2695             PrintError("", err);
2696         else
2697             fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2698                     as->parms[0].items->data);
2699         return ENOENT;
2700     }
2701     code = GetVolumeInfo(avolid, &aserver, &apart, &vtype, &entry);
2702     if (code)
2703         exit(1);
2704
2705     /* verify this is a readwrite volume */
2706
2707     if (vtype != RWVOL) {
2708         fprintf(STDERR, "%s not RW volume\n", as->parms[0].items->data);
2709         exit(1);
2710     }
2711
2712     /* is there a backup volume already? */
2713
2714     if (entry.flags & BACK_EXISTS) {
2715         /* yep, where is it? */
2716
2717         buvolid = entry.volumeId[BACKVOL];
2718         code = GetVolumeInfo(buvolid, &buserver, &bupart, &butype, &buentry);
2719         if (code)
2720             exit(1);
2721
2722         /* is it local? */
2723         code = VLDB_IsSameAddrs(buserver, aserver, &err);
2724         if (err) {
2725             fprintf(STDERR,
2726                     "Failed to get info about server's %d address(es) from vlserver; aborting call!\n",
2727                     buserver);
2728             exit(1);
2729         }
2730         if (!code) {
2731             fprintf(STDERR,
2732                     "FATAL ERROR: backup volume %lu exists on server %lu\n",
2733                     (unsigned long)buvolid, (unsigned long)buserver);
2734             exit(1);
2735         }
2736     }
2737
2738     /* nope, carry on */
2739
2740     code = UV_BackupVolume(aserver, apart, avolid);
2741
2742     if (code) {
2743         PrintDiagnostics("backup", code);
2744         return code;
2745     }
2746     fprintf(STDOUT, "Created backup volume for %s \n",
2747             as->parms[0].items->data);
2748     return 0;
2749 }
2750
2751 static
2752 ReleaseVolume(as)
2753      register struct cmd_syndesc *as;
2754 {
2755
2756     struct nvldbentry entry;
2757     afs_int32 avolid, aserver, apart, vtype, code, err;
2758     int force = 0;
2759
2760     if (as->parms[1].items)
2761         force = 1;
2762     avolid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
2763     if (avolid == 0) {
2764         if (err)
2765             PrintError("", err);
2766         else
2767             fprintf(STDERR, "vos: can't find volume '%s'\n",
2768                     as->parms[0].items->data);
2769         return ENOENT;
2770     }
2771     code = GetVolumeInfo(avolid, &aserver, &apart, &vtype, &entry);
2772     if (code)
2773         return code;
2774
2775     if (vtype != RWVOL) {
2776         fprintf(STDERR, "%s not a RW volume\n", as->parms[0].items->data);
2777         return (ENOENT);
2778     }
2779
2780     if (!ISNAMEVALID(entry.name)) {
2781         fprintf(STDERR,
2782                 "Volume name %s is too long, rename before releasing\n",
2783                 entry.name);
2784         return E2BIG;
2785     }
2786
2787     code = UV_ReleaseVolume(avolid, aserver, apart, force);
2788     if (code) {
2789         PrintDiagnostics("release", code);
2790         return code;
2791     }
2792     fprintf(STDOUT, "Released volume %s successfully\n",
2793             as->parms[0].items->data);
2794     return 0;
2795 }
2796
2797 static
2798 DumpVolume(as)
2799      register struct cmd_syndesc *as;
2800
2801 {
2802     afs_int32 avolid, aserver, apart, voltype, fromdate = 0, code, err, i, flags;
2803     char filename[MAXPATHLEN];
2804     struct nvldbentry entry;
2805
2806     rx_SetRxDeadTime(60 * 10);
2807     for (i = 0; i < MAXSERVERS; i++) {
2808         struct rx_connection *rxConn = ubik_GetRPCConn(cstruct, i);
2809         if (rxConn == 0)
2810             break;
2811         rx_SetConnDeadTime(rxConn, rx_connDeadTime);
2812         if (rxConn->service)
2813             rxConn->service->connDeadTime = rx_connDeadTime;
2814     }
2815
2816     avolid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
2817     if (avolid == 0) {
2818         if (err)
2819             PrintError("", err);
2820         else
2821             fprintf(STDERR, "vos: can't find volume '%s'\n",
2822                     as->parms[0].items->data);
2823         return ENOENT;
2824     }
2825
2826     if (as->parms[3].items || as->parms[4].items) {
2827         if (!as->parms[3].items || !as->parms[4].items) {
2828             fprintf(STDERR,
2829                     "Must specify both -server and -partition options\n");
2830             return -1;
2831         }
2832         aserver = GetServer(as->parms[3].items->data);
2833         if (aserver == 0) {
2834             fprintf(STDERR, "Invalid server name\n");
2835             return -1;
2836         }
2837         apart = volutil_GetPartitionID(as->parms[4].items->data);
2838         if (apart < 0) {
2839             fprintf(STDERR, "Invalid partition name\n");
2840             return -1;
2841         }
2842     } else {
2843         code = GetVolumeInfo(avolid, &aserver, &apart, &voltype, &entry);
2844         if (code)
2845             return code;
2846     }
2847
2848     if (as->parms[1].items && strcmp(as->parms[1].items->data, "0")) {
2849         code = ktime_DateToInt32(as->parms[1].items->data, &fromdate);
2850         if (code) {
2851             fprintf(STDERR, "vos: failed to parse date '%s' (error=%d))\n",
2852                     as->parms[1].items->data, code);
2853             return code;
2854         }
2855     }
2856     if (as->parms[2].items) {
2857         strcpy(filename, as->parms[2].items->data);
2858     } else {
2859         strcpy(filename, "");
2860     }
2861
2862     flags = as->parms[6].items ? VOLDUMPV2_OMITDIRS : 0;
2863 retry_dump:
2864     if (as->parms[5].items) {
2865         code =
2866             UV_DumpClonedVolume(avolid, aserver, apart, fromdate,
2867                                 DumpFunction, filename, flags);
2868     } else {
2869         code =
2870             UV_DumpVolume(avolid, aserver, apart, fromdate, DumpFunction,
2871                           filename, flags);
2872     }
2873     if ((code == RXGEN_OPCODE) && (as->parms[6].items)) {
2874         flags &= ~VOLDUMPV2_OMITDIRS;
2875         goto retry_dump;
2876     }
2877     if (code) {
2878         PrintDiagnostics("dump", code);
2879         return code;
2880     }
2881     if (strcmp(filename, ""))
2882         fprintf(STDERR, "Dumped volume %s in file %s\n",
2883                 as->parms[0].items->data, filename);
2884     else
2885         fprintf(STDERR, "Dumped volume %s in stdout \n",
2886                 as->parms[0].items->data);
2887     return 0;
2888 }
2889
2890 #define ASK   0
2891 #define ABORT 1
2892 #define FULL  2
2893 #define INC   3
2894
2895 #define TS_DUMP 1
2896 #define TS_KEEP 2
2897 #define TS_NEW  3
2898
2899 static
2900 RestoreVolume(as)
2901      register struct cmd_syndesc *as;
2902
2903 {
2904     afs_int32 avolid, aparentid, aserver, apart, code, vcode, err;
2905     afs_int32 aoverwrite = ASK;
2906     afs_int32 acreation = 0, alastupdate = 0;
2907     int restoreflags, readonly = 0, offline = 0, voltype = RWVOL;
2908     char prompt;
2909     char afilename[MAXPATHLEN], avolname[VOLSER_MAXVOLNAME + 1], apartName[10];
2910     char volname[VOLSER_MAXVOLNAME + 1];
2911     struct nvldbentry entry;
2912
2913     prompt = 'n';
2914
2915     aparentid = 0;
2916     if (as->parms[4].items) {
2917         avolid = vsu_GetVolumeID(as->parms[4].items->data, cstruct, &err);
2918         if (avolid == 0) {
2919             if (err)
2920                 PrintError("", err);
2921             else
2922                 fprintf(STDERR, "vos: can't find volume '%s'\n",
2923                         as->parms[4].items->data);
2924             exit(1);
2925         }
2926     } else
2927         avolid = 0;
2928
2929     if (as->parms[5].items) {
2930         if ((strcmp(as->parms[5].items->data, "a") == 0)
2931             || (strcmp(as->parms[5].items->data, "abort") == 0)) {
2932             aoverwrite = ABORT;
2933         } else if ((strcmp(as->parms[5].items->data, "f") == 0)
2934                    || (strcmp(as->parms[5].items->data, "full") == 0)) {
2935             aoverwrite = FULL;
2936         } else if ((strcmp(as->parms[5].items->data, "i") == 0)
2937                    || (strcmp(as->parms[5].items->data, "inc") == 0)
2938                    || (strcmp(as->parms[5].items->data, "increment") == 0)
2939                    || (strcmp(as->parms[5].items->data, "incremental") == 0)) {
2940             aoverwrite = INC;
2941         } else {
2942             fprintf(STDERR, "vos: %s is not a valid argument to -overwrite\n",
2943                     as->parms[5].items->data);
2944             exit(1);
2945         }
2946     }
2947     if (as->parms[6].items)
2948         offline = 1;
2949     if (as->parms[7].items) {
2950         readonly = 1;
2951         voltype = ROVOL;
2952     }
2953
2954     if (as->parms[8].items) {
2955         if ((strcmp(as->parms[8].items->data, "d") == 0)
2956             || (strcmp(as->parms[8].items->data, "dump") == 0)) {
2957             acreation = TS_DUMP;
2958         } else if ((strcmp(as->parms[8].items->data, "k") == 0)
2959             || (strcmp(as->parms[8].items->data, "keep") == 0)) {
2960             acreation = TS_KEEP;
2961         } else if ((strcmp(as->parms[8].items->data, "n") == 0)
2962             || (strcmp(as->parms[8].items->data, "new") == 0)) {
2963             acreation = TS_NEW;
2964         } else {
2965             fprintf(STDERR, "vos: %s is not a valid argument to -creation\n",
2966                     as->parms[8].items->data);
2967             exit(1);
2968         }
2969     }
2970
2971     if (as->parms[9].items) {
2972         if ((strcmp(as->parms[9].items->data, "d") == 0)
2973             || (strcmp(as->parms[9].items->data, "dump") == 0)) {
2974             alastupdate = TS_DUMP;
2975         } else if ((strcmp(as->parms[9].items->data, "k") == 0)
2976             || (strcmp(as->parms[9].items->data, "keep") == 0)) {
2977             alastupdate = TS_KEEP;
2978         } else if ((strcmp(as->parms[9].items->data, "n") == 0)
2979             || (strcmp(as->parms[9].items->data, "new") == 0)) {
2980             alastupdate = TS_NEW;
2981         } else {
2982             fprintf(STDERR, "vos: %s is not a valid argument to -lastupdate\n",
2983                     as->parms[9].items->data);
2984             exit(1);
2985         }
2986     }
2987
2988     aserver = GetServer(as->parms[0].items->data);
2989     if (aserver == 0) {
2990         fprintf(STDERR, "vos: server '%s' not found in host table\n",
2991                 as->parms[0].items->data);
2992         exit(1);
2993     }
2994     apart = volutil_GetPartitionID(as->parms[1].items->data);
2995     if (apart < 0) {
2996         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2997                 as->parms[1].items->data);
2998         exit(1);
2999     }
3000     if (!IsPartValid(apart, aserver, &code)) {  /*check for validity of the partition */
3001         if (code)
3002             PrintError("", code);
3003         else
3004             fprintf(STDERR,
3005                     "vos : partition %s does not exist on the server\n",
3006                     as->parms[1].items->data);
3007         exit(1);
3008     }
3009     strcpy(avolname, as->parms[2].items->data);
3010     if (!ISNAMEVALID(avolname)) {
3011         fprintf(STDERR,
3012                 "vos: the name of the volume %s exceeds the size limit\n",
3013                 avolname);
3014         exit(1);
3015     }
3016     if (!VolNameOK(avolname)) {
3017         fprintf(STDERR,
3018                 "Illegal volume name %s, should not end in .readonly or .backup\n",
3019                 avolname);
3020         exit(1);
3021     }
3022     if (as->parms[3].items) {
3023         strcpy(afilename, as->parms[3].items->data);
3024         if (!FileExists(afilename)) {
3025             fprintf(STDERR, "Can't access file %s\n", afilename);
3026             exit(1);
3027         }
3028     } else {
3029         strcpy(afilename, "");
3030     }
3031
3032     /* Check if volume exists or not */
3033
3034     vsu_ExtractName(volname, avolname);
3035     vcode = VLDB_GetEntryByName(volname, &entry);
3036     if (vcode) {                /* no volume - do a full restore */
3037         restoreflags = RV_FULLRST;
3038         if ((aoverwrite == INC) || (aoverwrite == ABORT))
3039             fprintf(STDERR,
3040                     "Volume does not exist; Will perform a full restore\n");
3041     }
3042
3043     else if ((!readonly && Lp_GetRwIndex(&entry) == -1) /* RW volume does not exist - do a full */
3044              ||(readonly && !Lp_ROMatch(0, 0, &entry))) {       /* RO volume does not exist - do a full */
3045         restoreflags = RV_FULLRST;
3046         if ((aoverwrite == INC) || (aoverwrite == ABORT))
3047             fprintf(STDERR,
3048                     "%s Volume does not exist; Will perform a full restore\n",
3049                     readonly ? "RO" : "RW");
3050
3051         if (avolid == 0) {
3052             avolid = entry.volumeId[voltype];
3053         } else if (entry.volumeId[voltype] != 0
3054                    && entry.volumeId[voltype] != avolid) {
3055             avolid = entry.volumeId[voltype];
3056         }
3057         aparentid = entry.volumeId[RWVOL];
3058     }
3059
3060     else {                      /* volume exists - do we do a full incremental or abort */
3061         int Oserver, Opart, Otype, vol_elsewhere = 0;
3062         struct nvldbentry Oentry;
3063         int c, dc;
3064
3065         if (avolid == 0) {
3066             avolid = entry.volumeId[voltype];
3067         } else if (entry.volumeId[voltype] != 0
3068                    && entry.volumeId[voltype] != avolid) {
3069             avolid = entry.volumeId[voltype];
3070         }
3071         aparentid = entry.volumeId[RWVOL];
3072
3073         /* A file name was specified  - check if volume is on another partition */
3074         vcode = GetVolumeInfo(avolid, &Oserver, &Opart, &Otype, &Oentry);
3075         if (vcode)
3076             exit(1);
3077
3078         vcode = VLDB_IsSameAddrs(Oserver, aserver, &err);
3079         if (err) {
3080             fprintf(STDERR,
3081                     "Failed to get info about server's %d address(es) from vlserver (err=%d); aborting call!\n",
3082                     Oserver, err);
3083             exit(1);
3084         }
3085         if (!vcode || (Opart != apart))
3086             vol_elsewhere = 1;
3087
3088         if (aoverwrite == ASK) {
3089             if (strcmp(afilename, "") == 0) {   /* The file is from standard in */
3090                 fprintf(STDERR,
3091                         "Volume exists and no -overwrite option specified; Aborting restore command\n");
3092                 exit(1);
3093             }
3094
3095             /* Ask what to do */
3096             if (vol_elsewhere) {
3097                 fprintf(STDERR,
3098                         "The volume %s %u already exists on a different server/part\n",
3099                         volname, entry.volumeId[voltype]);
3100                 fprintf(STDERR,
3101                         "Do you want to do a full restore or abort? [fa](a): ");
3102             } else {
3103                 fprintf(STDERR,
3104                         "The volume %s %u already exists in the VLDB\n",
3105                         volname, entry.volumeId[voltype]);
3106                 fprintf(STDERR,
3107                         "Do you want to do a full/incremental restore or abort? [fia](a): ");
3108             }
3109             dc = c = getchar();
3110             while (!(dc == EOF || dc == '\n'))
3111                 dc = getchar(); /* goto end of line */
3112             if ((c == 'f') || (c == 'F'))
3113                 aoverwrite = FULL;
3114             else if ((c == 'i') || (c == 'I'))
3115                 aoverwrite = INC;
3116             else
3117                 aoverwrite = ABORT;
3118         }
3119
3120         if (aoverwrite == ABORT) {
3121             fprintf(STDERR, "Volume exists; Aborting restore command\n");
3122             exit(1);
3123         } else if (aoverwrite == FULL) {
3124             restoreflags = RV_FULLRST;
3125             fprintf(STDERR,
3126                     "Volume exists; Will delete and perform full restore\n");
3127         } else if (aoverwrite == INC) {
3128             restoreflags = 0;
3129             if (vol_elsewhere) {
3130                 fprintf(STDERR,
3131                         "%s volume %lu already exists on a different server/part; not allowed\n",
3132                         readonly ? "RO" : "RW", (unsigned long)avolid);
3133                 exit(1);
3134             }
3135         }
3136     }
3137     if (offline)
3138         restoreflags |= RV_OFFLINE;
3139     if (readonly)
3140         restoreflags |= RV_RDONLY;
3141
3142     switch (acreation) {
3143         case TS_DUMP:
3144             restoreflags |= RV_CRDUMP;
3145             break;
3146         case TS_KEEP:
3147             restoreflags |= RV_CRKEEP;
3148             break;
3149         case TS_NEW:
3150             restoreflags |= RV_CRNEW;
3151             break;
3152         default:
3153             if (aoverwrite == FULL)
3154                 restoreflags |= RV_CRNEW;
3155             else
3156                 restoreflags |= RV_CRKEEP;
3157     }
3158
3159     switch (alastupdate) {
3160         case TS_DUMP:
3161             restoreflags |= RV_LUDUMP;
3162             break;
3163         case TS_KEEP:
3164             restoreflags |= RV_LUKEEP;
3165             break;
3166         case TS_NEW:
3167             restoreflags |= RV_LUNEW;
3168             break;
3169         default:
3170             restoreflags |= RV_LUDUMP;
3171     }
3172
3173     code =
3174         UV_RestoreVolume2(aserver, apart, avolid, aparentid,
3175                           avolname, restoreflags, WriteData, afilename);
3176     if (code) {
3177         PrintDiagnostics("restore", code);
3178         exit(1);
3179     }
3180     MapPartIdIntoName(apart, apartName);
3181
3182     /*
3183      * patch typo here - originally "parms[1]", should be "parms[0]"
3184      */
3185
3186     fprintf(STDOUT, "Restored volume %s on %s %s\n", avolname,
3187             as->parms[0].items->data, apartName);
3188     return 0;
3189 }
3190
3191 static
3192 LockReleaseCmd(as)
3193      register struct cmd_syndesc *as;
3194
3195 {
3196     afs_int32 avolid, code, err;
3197
3198     avolid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
3199     if (avolid == 0) {
3200         if (err)
3201             PrintError("", err);
3202         else
3203             fprintf(STDERR, "vos: can't find volume '%s'\n",
3204                     as->parms[0].items->data);
3205         exit(1);
3206     }
3207
3208     code = UV_LockRelease(avolid);
3209     if (code) {
3210         PrintDiagnostics("unlock", code);
3211         exit(1);
3212     }
3213     fprintf(STDOUT, "Released lock on vldb entry for volume %s\n",
3214             as->parms[0].items->data);
3215     return 0;
3216 }
3217
3218 static
3219 AddSite(as)
3220      register struct cmd_syndesc *as;
3221 {
3222     afs_int32 avolid, aserver, apart, code, err;
3223     char apartName[10], avolname[VOLSER_MAXVOLNAME + 1];
3224
3225     vsu_ExtractName(avolname, as->parms[2].items->data);;
3226     avolid = vsu_GetVolumeID(avolname, cstruct, &err);
3227     if (avolid == 0) {
3228         if (err)
3229             PrintError("", err);
3230         else
3231             fprintf(STDERR, "vos: can't find volume '%s'\n",
3232                     as->parms[2].items->data);
3233         exit(1);
3234     }
3235     aserver = GetServer(as->parms[0].items->data);
3236     if (aserver == 0) {
3237         fprintf(STDERR, "vos: server '%s' not found in host table\n",
3238                 as->parms[0].items->data);
3239         exit(1);
3240     }
3241     apart = volutil_GetPartitionID(as->parms[1].items->data);
3242     if (apart < 0) {
3243         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3244                 as->parms[1].items->data);
3245         exit(1);
3246     }
3247     if (!IsPartValid(apart, aserver, &code)) {  /*check for validity of the partition */
3248         if (code)
3249             PrintError("", code);
3250         else
3251             fprintf(STDERR,
3252                     "vos : partition %s does not exist on the server\n",
3253                     as->parms[1].items->data);
3254         exit(1);
3255     }
3256     code = UV_AddSite(aserver, apart, avolid);
3257     if (code) {
3258         PrintDiagnostics("addsite", code);
3259         exit(1);
3260     }
3261     MapPartIdIntoName(apart, apartName);
3262     fprintf(STDOUT, "Added replication site %s %s for volume %s\n",
3263             as->parms[0].items->data, apartName, as->parms[2].items->data);
3264     return 0;
3265 }
3266
3267 static
3268 RemoveSite(as)
3269      register struct cmd_syndesc *as;
3270 {
3271
3272     afs_int32 avolid, aserver, apart, code, err;
3273     char apartName[10], avolname[VOLSER_MAXVOLNAME + 1];
3274
3275     vsu_ExtractName(avolname, as->parms[2].items->data);
3276     avolid = vsu_GetVolumeID(avolname, cstruct, &err);
3277     if (avolid == 0) {
3278         if (err)
3279             PrintError("", err);
3280         else
3281             fprintf(STDERR, "vos: can't find volume '%s'\n",
3282                     as->parms[2].items->data);
3283         exit(1);
3284     }
3285     aserver = GetServer(as->parms[0].items->data);
3286     if (aserver == 0) {
3287         fprintf(STDERR, "vos: server '%s' not found in host table\n",
3288                 as->parms[0].items->data);
3289         exit(1);
3290     }
3291     apart = volutil_GetPartitionID(as->parms[1].items->data);
3292     if (apart < 0) {
3293         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3294                 as->parms[1].items->data);
3295         exit(1);
3296     }
3297 /*
3298  *skip the partition validity check, since it is possible that the partition
3299  *has since been decomissioned.
3300  */
3301 /*
3302         if (!IsPartValid(apart,aserver,&code)){
3303             if(code) PrintError("",code);
3304             else fprintf(STDERR,"vos : partition %s does not exist on the server\n",as->parms[1].items->data);
3305             exit(1);
3306         }
3307 */
3308     code = UV_RemoveSite(aserver, apart, avolid);
3309     if (code) {
3310         PrintDiagnostics("remsite", code);
3311         exit(1);
3312     }
3313     MapPartIdIntoName(apart, apartName);
3314     fprintf(STDOUT, "Removed replication site %s %s for volume %s\n",
3315             as->parms[0].items->data, apartName, as->parms[2].items->data);
3316     return 0;
3317 }
3318
3319 static
3320 ChangeLocation(as)
3321      register struct cmd_syndesc *as;
3322 {
3323     afs_int32 avolid, aserver, apart, code, err;
3324     char apartName[10];
3325
3326     avolid = vsu_GetVolumeID(as->parms[2].items->data, cstruct, &err);
3327     if (avolid == 0) {
3328         if (err)
3329             PrintError("", err);
3330         else
3331             fprintf(STDERR, "vos: can't find volume '%s'\n",
3332                     as->parms[2].items->data);
3333         exit(1);
3334     }
3335     aserver = GetServer(as->parms[0].items->data);
3336     if (aserver == 0) {
3337         fprintf(STDERR, "vos: server '%s' not found in host table\n",
3338                 as->parms[0].items->data);
3339         exit(1);
3340     }
3341     apart = volutil_GetPartitionID(as->parms[1].items->data);
3342     if (apart < 0) {
3343         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3344                 as->parms[1].items->data);
3345         exit(1);
3346     }
3347     if (!IsPartValid(apart, aserver, &code)) {  /*check for validity of the partition */
3348         if (code)
3349             PrintError("", code);
3350         else
3351             fprintf(STDERR,
3352                     "vos : partition %s does not exist on the server\n",
3353                     as->parms[1].items->data);
3354         exit(1);
3355     }
3356     code = UV_ChangeLocation(aserver, apart, avolid);
3357     if (code) {
3358         PrintDiagnostics("addsite", code);
3359         exit(1);
3360     }
3361     MapPartIdIntoName(apart, apartName);
3362     fprintf(STDOUT, "Changed location to %s %s for volume %s\n",
3363             as->parms[0].items->data, apartName, as->parms[2].items->data);
3364     return 0;
3365 }
3366
3367 static
3368 ListPartitions(as)
3369      register struct cmd_syndesc *as;
3370 {
3371     afs_int32 aserver, code;
3372     struct partList dummyPartList;
3373     int i;
3374     char pname[10];
3375     int total, cnt;
3376
3377     aserver = GetServer(as->parms[0].items->data);
3378     if (aserver == 0) {
3379         fprintf(STDERR, "vos: server '%s' not found in host table\n",
3380                 as->parms[0].items->data);
3381         exit(1);
3382     }
3383
3384
3385     code = UV_ListPartitions(aserver, &dummyPartList, &cnt);
3386     if (code) {
3387         PrintDiagnostics("listpart", code);
3388         exit(1);
3389     }
3390     total = 0;
3391     fprintf(STDOUT, "The partitions on the server are:\n");
3392     for (i = 0; i < cnt; i++) {
3393         if (dummyPartList.partFlags[i] & PARTVALID) {
3394             memset(pname, 0, sizeof(pname));
3395             MapPartIdIntoName(dummyPartList.partId[i], pname);
3396             fprintf(STDOUT, " %10s ", pname);
3397             total++;
3398             if ((i % 5) == 0 && (i != 0))
3399                 fprintf(STDOUT, "\n");
3400         }
3401     }
3402     fprintf(STDOUT, "\n");
3403     fprintf(STDOUT, "Total: %d\n", total);
3404     return 0;
3405
3406 }
3407
3408 static int
3409 CompareVolName(p1, p2)
3410      char *p1, *p2;
3411 {
3412     volintInfo *arg1, *arg2;
3413
3414     arg1 = (volintInfo *) p1;
3415     arg2 = (volintInfo *) p2;
3416     return (strcmp(arg1->name, arg2->name));
3417
3418 }
3419
3420 /*------------------------------------------------------------------------
3421  * PRIVATE XCompareVolName
3422  *
3423  * Description:
3424  *      Comparison routine for volume names coming from an extended
3425  *      volume listing.
3426  *
3427  * Arguments:
3428  *      a_obj1P : Char ptr to first extended vol info object
3429  *      a_obj1P : Char ptr to second extended vol info object
3430  *
3431  * Returns:
3432  *      The value of strcmp() on the volume names within the passed
3433  *      objects (i,e., -1, 0, or 1).
3434  *
3435  * Environment:
3436  *      Passed to qsort() as the designated comparison routine.
3437  *
3438  * Side Effects:
3439  *      As advertised.
3440  *------------------------------------------------------------------------*/
3441
3442 static int
3443 XCompareVolName(a_obj1P, a_obj2P)
3444      char *a_obj1P, *a_obj2P;
3445
3446 {                               /*XCompareVolName */
3447
3448     return (strcmp
3449             (((struct volintXInfo *)(a_obj1P))->name,
3450              ((struct volintXInfo *)(a_obj2P))->name));
3451
3452 }                               /*XCompareVolName */
3453
3454 static int
3455 CompareVolID(p1, p2)
3456      char *p1, *p2;
3457 {
3458     volintInfo *arg1, *arg2;
3459
3460     arg1 = (volintInfo *) p1;
3461     arg2 = (volintInfo *) p2;
3462     if (arg1->volid == arg2->volid)
3463         return 0;
3464     if (arg1->volid > arg2->volid)
3465         return 1;
3466     else
3467         return -1;
3468
3469 }
3470
3471 /*------------------------------------------------------------------------
3472  * PRIVATE XCompareVolID
3473  *
3474  * Description:
3475  *      Comparison routine for volume IDs coming from an extended
3476  *      volume listing.
3477  *
3478  * Arguments:
3479  *      a_obj1P : Char ptr to first extended vol info object
3480  *      a_obj1P : Char ptr to second extended vol info object
3481  *
3482  * Returns:
3483  *      The value of strcmp() on the volume names within the passed
3484  *      objects (i,e., -1, 0, or 1).
3485  *
3486  * Environment:
3487  *      Passed to qsort() as the designated comparison routine.
3488  *
3489  * Side Effects:
3490  *      As advertised.
3491  *------------------------------------------------------------------------*/
3492
3493 static int
3494 XCompareVolID(a_obj1P, a_obj2P)
3495      char *a_obj1P, *a_obj2P;
3496
3497 {                               /*XCompareVolID */
3498
3499     afs_int32 id1, id2;         /*Volume IDs we're comparing */
3500
3501     id1 = ((struct volintXInfo *)(a_obj1P))->volid;
3502     id2 = ((struct volintXInfo *)(a_obj2P))->volid;
3503     if (id1 == id2)
3504         return (0);
3505     else if (id1 > id2)
3506         return (1);
3507     else
3508         return (-1);
3509
3510 }                               /*XCompareVolID */
3511
3512 /*------------------------------------------------------------------------
3513  * PRIVATE ListVolumes
3514  *
3515  * Description:
3516  *      Routine used to list volumes, contacting the Volume Server
3517  *      directly, bypassing the VLDB.
3518  *
3519  * Arguments:
3520  *      as : Ptr to parsed command line arguments.
3521  *
3522  * Returns:
3523  *      0                       Successful operation
3524  *
3525  * Environment:
3526  *      Nothing interesting.
3527  *
3528  * Side Effects:
3529  *      As advertised.
3530  *------------------------------------------------------------------------*/
3531
3532 static
3533 ListVolumes(as)
3534      register struct cmd_syndesc *as;
3535 {
3536     afs_int32 apart, int32list, fast;
3537     afs_int32 aserver, code;
3538     volintInfo *pntr, *oldpntr;
3539     afs_int32 count;
3540     int i;
3541     char *base;
3542     volintXInfo *xInfoP, *origxInfoP;   /*Ptr to current/orig extended vol info */
3543     int wantExtendedInfo;       /*Do we want extended vol info? */
3544
3545     char pname[10];
3546     struct partList dummyPartList;
3547     int all;
3548     int quiet, cnt;
3549
3550     apart = -1;
3551     fast = 0;
3552     int32list = 0;
3553
3554     if (as->parms[3].items)
3555         int32list = 1;
3556     if (as->parms[4].items)
3557         quiet = 1;
3558     else
3559         quiet = 0;
3560     if (as->parms[2].items)
3561         fast = 1;
3562     if (fast)
3563         all = 0;
3564     else
3565         all = 1;
3566     if (as->parms[5].items) {
3567         /*
3568          * We can't coexist with the fast flag.
3569          */
3570         if (fast) {
3571             fprintf(STDERR,
3572                     "vos: Can't use the -fast and -extended flags together\n");
3573             exit(1);
3574         }
3575
3576         /*
3577          * We need to turn on ``long'' listings to get the full effect.
3578          */
3579         wantExtendedInfo = 1;
3580         int32list = 1;
3581     } else
3582         wantExtendedInfo = 0;
3583     if (as->parms[1].items) {
3584         apart = volutil_GetPartitionID(as->parms[1].items->data);
3585         if (apart < 0) {
3586             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3587                     as->parms[1].items->data);
3588             exit(1);
3589         }
3590         dummyPartList.partId[0] = apart;
3591         dummyPartList.partFlags[0] = PARTVALID;
3592         cnt = 1;
3593     }
3594     aserver = GetServer(as->parms[0].items->data);
3595     if (aserver == 0) {
3596         fprintf(STDERR, "vos: server '%s' not found in host table\n",
3597                 as->parms[0].items->data);
3598         exit(1);
3599     }
3600
3601     if (apart != -1) {
3602         if (!IsPartValid(apart, aserver, &code)) {      /*check for validity of the partition */
3603             if (code)
3604                 PrintError("", code);
3605             else
3606                 fprintf(STDERR,
3607                         "vos : partition %s does not exist on the server\n",
3608                         as->parms[1].items->data);
3609             exit(1);
3610         }
3611     } else {
3612         code = UV_ListPartitions(aserver, &dummyPartList, &cnt);
3613         if (code) {
3614             PrintDiagnostics("listvol", code);
3615             exit(1);
3616         }
3617     }
3618     for (i = 0; i < cnt; i++) {
3619         if (dummyPartList.partFlags[i] & PARTVALID) {
3620             if (wantExtendedInfo)
3621                 code =
3622                     UV_XListVolumes(aserver, dummyPartList.partId[i], all,
3623                                     &xInfoP, &count);
3624             else
3625                 code =
3626                     UV_ListVolumes(aserver, dummyPartList.partId[i], all,
3627                                    &pntr, &count);
3628             if (code) {
3629                 PrintDiagnostics("listvol", code);
3630                 if (pntr)
3631                     free(pntr);
3632                 exit(1);
3633             }
3634             if (wantExtendedInfo) {
3635                 origxInfoP = xInfoP;
3636                 base = (char *)xInfoP;
3637             } else {
3638                 oldpntr = pntr;
3639                 base = (char *)pntr;
3640             }
3641
3642             if (!fast) {
3643                 if (wantExtendedInfo)
3644                     qsort(base, count, sizeof(volintXInfo), XCompareVolName);
3645                 else
3646                     qsort(base, count, sizeof(volintInfo), CompareVolName);
3647             } else {
3648                 if (wantExtendedInfo)
3649                     qsort(base, count, sizeof(volintXInfo), XCompareVolID);
3650                 else
3651                     qsort(base, count, sizeof(volintInfo), CompareVolID);
3652             }
3653             MapPartIdIntoName(dummyPartList.partId[i], pname);
3654             if (!quiet)
3655                 fprintf(STDOUT,
3656                         "Total number of volumes on server %s partition %s: %lu \n",
3657                         as->parms[0].items->data, pname,
3658                         (unsigned long)count);
3659             if (wantExtendedInfo) {
3660 #ifdef FULL_LISTVOL_SWITCH
3661                 if (as->parms[6].items)
3662                     XDisplayVolumes2(aserver, dummyPartList.partId[i], origxInfoP,
3663                                 count, int32list, fast, quiet);
3664                 else
3665 #endif /* FULL_LISTVOL_SWITCH */
3666                 XDisplayVolumes(aserver, dummyPartList.partId[i], origxInfoP,
3667                                 count, int32list, fast, quiet);
3668                 if (xInfoP)
3669                     free(xInfoP);
3670                 xInfoP = (volintXInfo *) 0;
3671             } else {
3672 #ifdef FULL_LISTVOL_SWITCH
3673                 if (as->parms[6].items)
3674                     DisplayVolumes2(aserver, dummyPartList.partId[i], oldpntr,
3675                                     count);
3676                 else
3677 #endif /* FULL_LISTVOL_SWITCH */
3678                     DisplayVolumes(aserver, dummyPartList.partId[i], oldpntr,
3679                                    count, int32list, fast, quiet);
3680                 if (pntr)
3681                     free(pntr);
3682                 pntr = (volintInfo *) 0;
3683             }
3684         }
3685     }
3686     return 0;
3687 }
3688
3689 static
3690 SyncVldb(as)
3691      register struct cmd_syndesc *as;
3692 {
3693     afs_int32 pnum = 0, code;   /* part name */
3694     char part[10];
3695     int flags = 0;
3696     char *volname = 0;
3697
3698     tserver = 0;
3699     if (as->parms[0].items) {
3700         tserver = GetServer(as->parms[0].items->data);
3701         if (!tserver) {
3702             fprintf(STDERR, "vos: host '%s' not found in host table\n",
3703                     as->parms[0].items->data);
3704             exit(1);
3705         }
3706     }
3707
3708     if (as->parms[1].items) {
3709         pnum = volutil_GetPartitionID(as->parms[1].items->data);
3710         if (pnum < 0) {
3711             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3712                     as->parms[1].items->data);
3713             exit(1);
3714         }
3715         if (!IsPartValid(pnum, tserver, &code)) {       /*check for validity of the partition */
3716             if (code)
3717                 PrintError("", code);
3718             else
3719                 fprintf(STDERR,
3720                         "vos: partition %s does not exist on the server\n",
3721                         as->parms[1].items->data);
3722             exit(1);
3723         }
3724         flags = 1;
3725
3726         if (!tserver) {
3727             fprintf(STDERR,
3728                     "The -partition option requires a -server option\n");
3729             exit(1);
3730         }
3731     }
3732
3733     if (as->parms[2].items) {
3734         /* Synchronize an individual volume */
3735         volname = as->parms[2].items->data;
3736         code = UV_SyncVolume(tserver, pnum, volname, flags);
3737     } else {
3738         if (!tserver) {
3739             fprintf(STDERR,
3740                     "Without a -volume option, the -server option is required\n");
3741             exit(1);
3742         }
3743         code = UV_SyncVldb(tserver, pnum, flags, 0 /*unused */ );
3744     }
3745
3746     if (code) {
3747         PrintDiagnostics("syncvldb", code);
3748         exit(1);
3749     }
3750
3751     /* Print a summary of what we did */
3752     if (volname)
3753         fprintf(STDOUT, "VLDB volume %s synchronized", volname);
3754     else
3755         fprintf(STDOUT, "VLDB synchronized");
3756     if (tserver) {
3757         fprintf(STDOUT, " with state of server %s", as->parms[0].items->data);
3758     }
3759     if (flags) {
3760         MapPartIdIntoName(pnum, part);
3761         fprintf(STDOUT, " partition %s\n", part);
3762     }
3763     fprintf(STDOUT, "\n");
3764
3765     return 0;
3766 }
3767
3768 static
3769 SyncServer(as)
3770      register struct cmd_syndesc *as;
3771
3772 {
3773     afs_int32 pnum, code;       /* part name */
3774     char part[10];
3775
3776     int flags = 0;
3777
3778     tserver = GetServer(as->parms[0].items->data);
3779     if (!tserver) {
3780         fprintf(STDERR, "vos: host '%s' not found in host table\n",
3781                 as->parms[0].items->data);
3782         exit(1);
3783     }
3784     if (as->parms[1].items) {
3785         pnum = volutil_GetPartitionID(as->parms[1].items->data);
3786         if (pnum < 0) {
3787             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3788                     as->parms[1].items->data);
3789             exit(1);
3790         }
3791         if (!IsPartValid(pnum, tserver, &code)) {       /*check for validity of the partition */
3792             if (code)
3793                 PrintError("", code);
3794             else
3795                 fprintf(STDERR,
3796                         "vos : partition %s does not exist on the server\n",
3797                         as->parms[1].items->data);
3798             exit(1);
3799         }
3800         flags = 1;
3801     } else {
3802         pnum = -1;
3803     }
3804
3805     code = UV_SyncServer(tserver, pnum, flags, 0 /*unused */ );
3806     if (code) {
3807         PrintDiagnostics("syncserv", code);
3808         exit(1);
3809     }
3810     if (flags) {
3811         MapPartIdIntoName(pnum, part);
3812         fprintf(STDOUT, "Server %s partition %s synchronized with VLDB\n",
3813                 as->parms[0].items->data, part);
3814     } else
3815         fprintf(STDOUT, "Server %s synchronized with VLDB\n",
3816                 as->parms[0].items->data);
3817     return 0;
3818
3819 }
3820
3821 static
3822 VolumeInfoCmd(name)
3823      char *name;
3824 {
3825     struct nvldbentry entry;
3826     afs_int32 vcode;
3827
3828     /* The vlserver will handle names with the .readonly
3829      * and .backup extension as well as volume ids.
3830      */
3831     vcode = VLDB_GetEntryByName(name, &entry);
3832     if (vcode) {
3833         PrintError("", vcode);
3834         exit(1);
3835     }
3836     MapHostToNetwork(&entry);
3837     EnumerateEntry(&entry);
3838
3839     /* Defect #3027: grubby check to handle locked volume.
3840      * If VLOP_ALLOPERS is set, the entry is locked.
3841      * Leave this routine as is, but put in correct check.
3842      */
3843     if (entry.flags & VLOP_ALLOPERS)
3844         fprintf(STDOUT, "    Volume is currently LOCKED  \n");
3845
3846     return 0;
3847 }
3848
3849 static
3850 VolumeZap(as)
3851      register struct cmd_syndesc *as;
3852
3853 {
3854     struct nvldbentry entry;
3855     afs_int32 volid, code, server, part, zapbackupid = 0, backupid = 0, err;
3856
3857     if (as->parms[3].items) {
3858         /* force flag is on, use the other version */
3859         return NukeVolume(as);
3860     }
3861
3862     if (as->parms[4].items) {
3863         zapbackupid = 1;
3864     }
3865
3866     volid = vsu_GetVolumeID(as->parms[2].items->data, cstruct, &err);
3867     if (volid == 0) {
3868         if (err)
3869             PrintError("", err);
3870         else
3871             fprintf(STDERR, "vos: can't find volume '%s'\n",
3872                     as->parms[2].items->data);
3873         exit(1);
3874     }
3875     part = volutil_GetPartitionID(as->parms[1].items->data);
3876     if (part < 0) {
3877         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3878                 as->parms[1].items->data);
3879         exit(1);
3880     }
3881     server = GetServer(as->parms[0].items->data);
3882     if (!server) {
3883         fprintf(STDERR, "vos: host '%s' not found in host table\n",
3884                 as->parms[0].items->data);
3885         exit(1);
3886     }
3887     if (!IsPartValid(part, server, &code)) {    /*check for validity of the partition */
3888         if (code)
3889             PrintError("", code);
3890         else
3891             fprintf(STDERR,
3892                     "vos : partition %s does not exist on the server\n",
3893                     as->parms[1].items->data);
3894         exit(1);
3895     }
3896     code = VLDB_GetEntryByID(volid, -1, &entry);
3897     if (!code) {
3898         if (volid == entry.volumeId[RWVOL])
3899             backupid = entry.volumeId[BACKVOL];
3900         fprintf(STDERR,
3901                 "Warning: Entry for volume number %lu exists in VLDB (but we're zapping it anyway!)\n",
3902                 (unsigned long)volid);
3903     }
3904     if (zapbackupid) {
3905         volintInfo *pntr = (volintInfo *) 0;
3906
3907         if (!backupid) {
3908             code = UV_ListOneVolume(server, part, volid, &pntr);
3909             if (!code) {
3910                 if (volid == pntr->parentID)
3911                     backupid = pntr->backupID;
3912                 if (pntr)
3913                     free(pntr);
3914             }
3915         }
3916         if (backupid) {
3917             code = UV_VolumeZap(server, part, backupid);
3918             if (code) {
3919                 PrintDiagnostics("zap", code);
3920                 exit(1);
3921             }
3922             fprintf(STDOUT, "Backup Volume %lu deleted\n",
3923                     (unsigned long)backupid);
3924         }
3925     }
3926     code = UV_VolumeZap(server, part, volid);
3927     if (code) {
3928         PrintDiagnostics("zap", code);
3929         exit(1);
3930     }
3931     fprintf(STDOUT, "Volume %lu deleted\n", (unsigned long)volid);
3932
3933     return 0;
3934 }
3935
3936 static
3937 VolserStatus(as)
3938      register struct cmd_syndesc *as;
3939
3940 {
3941     afs_int32 server, code;
3942     transDebugInfo *pntr, *oldpntr;
3943     afs_int32 count;
3944     int i;
3945     char pname[10];
3946
3947     server = GetServer(as->parms[0].items->data);
3948     if (!server) {
3949         fprintf(STDERR, "vos: host '%s' not found in host table\n",
3950                 as->parms[0].items->data);
3951         exit(1);
3952     }
3953     code = UV_VolserStatus(server, &pntr, &count);
3954     if (code) {
3955         PrintDiagnostics("status", code);
3956         exit(1);
3957     }
3958     oldpntr = pntr;
3959     if (count == 0)
3960         fprintf(STDOUT, "No active transactions on %s\n",
3961                 as->parms[0].items->data);
3962     else {
3963         fprintf(STDOUT, "Total transactions: %d\n", count);
3964     }
3965     for (i = 0; i < count; i++) {
3966         /*print out the relevant info */
3967         fprintf(STDOUT, "--------------------------------------\n");
3968         fprintf(STDOUT, "transaction: %lu  created: %s",
3969                 (unsigned long)pntr->tid, vos_ctime( & pntr->time));
3970         if (pntr->returnCode) {
3971             fprintf(STDOUT, "returnCode: %lu\n",
3972                     (unsigned long)pntr->returnCode);
3973         }
3974         if (pntr->iflags) {
3975             fprintf(STDOUT, "attachFlags:  ");
3976             switch (pntr->iflags) {
3977             case ITOffline:
3978                 fprintf(STDOUT, "offline ");
3979                 break;
3980             case ITBusy:
3981                 fprintf(STDOUT, "busy ");
3982                 break;
3983             case ITReadOnly:
3984                 fprintf(STDOUT, "readonly ");
3985                 break;
3986             case ITCreate:
3987                 fprintf(STDOUT, "create ");
3988                 break;
3989             case ITCreateVolID:
3990                 fprintf(STDOUT, "create volid ");
3991                 break;
3992             }
3993             fprintf(STDOUT, "\n");
3994         }
3995         if (pntr->vflags) {
3996             fprintf(STDOUT, "volumeStatus: ");
3997             switch (pntr->vflags) {
3998             case VTDeleteOnSalvage:
3999                 fprintf(STDOUT, "deleteOnSalvage ");
4000             case VTOutOfService:
4001                 fprintf(STDOUT, "outOfService ");
4002             case VTDeleted:
4003                 fprintf(STDOUT, "deleted ");
4004             }
4005             fprintf(STDOUT, "\n");
4006         }
4007         if (pntr->tflags) {
4008             fprintf(STDOUT, "transactionFlags: ");
4009             fprintf(STDOUT, "delete\n");
4010         }
4011         MapPartIdIntoName(pntr->partition, pname);
4012         fprintf(STDOUT, "volume: %lu  partition: %s  procedure: %s\n",
4013                 (unsigned long)pntr->volid, pname, pntr->lastProcName);
4014         if (pntr->callValid) {
4015             fprintf(STDOUT,
4016                     "packetRead: %lu  lastReceiveTime: %d  packetSend: %lu  lastSendTime: %d\n",
4017                     (unsigned long)pntr->readNext, pntr->lastReceiveTime,
4018                     (unsigned long)pntr->transmitNext, pntr->lastSendTime);
4019         }
4020         pntr++;
4021         fprintf(STDOUT, "--------------------------------------\n");
4022         fprintf(STDOUT, "\n");
4023     }
4024     if (oldpntr)
4025         free(oldpntr);
4026     return 0;
4027 }
4028
4029 static
4030 RenameVolume(as)
4031      register struct cmd_syndesc *as;
4032 {
4033     afs_int32 code1, code2, code;
4034     struct nvldbentry entry;
4035
4036     code1 = VLDB_GetEntryByName(as->parms[0].items->data, &entry);
4037     if (code1) {
4038         fprintf(STDERR, "vos: Could not find entry for volume %s\n",
4039                 as->parms[0].items->data);
4040         exit(1);
4041     }
4042     code2 = VLDB_GetEntryByName(as->parms[1].items->data, &entry);
4043     if ((!code1) && (!code2)) { /*the newname already exists */
4044         fprintf(STDERR, "vos: volume %s already exists\n",
4045                 as->parms[1].items->data);
4046         exit(1);
4047     }
4048
4049     if (code1 && code2) {
4050         fprintf(STDERR, "vos: Could not find entry for volume %s or %s\n",
4051                 as->parms[0].items->data, as->parms[1].items->data);
4052         exit(1);
4053     }
4054     if (!VolNameOK(as->parms[0].items->data)) {
4055         fprintf(STDERR,
4056                 "Illegal volume name %s, should not end in .readonly or .backup\n",
4057                 as->parms[0].items->data);
4058         exit(1);
4059     }
4060     if (!ISNAMEVALID(as->parms[1].items->data)) {
4061         fprintf(STDERR,
4062                 "vos: the new volume name %s exceeds the size limit of %d\n",
4063                 as->parms[1].items->data, VOLSER_OLDMAXVOLNAME - 10);
4064         exit(1);
4065     }
4066     if (!VolNameOK(as->parms[1].items->data)) {
4067         fprintf(STDERR,
4068                 "Illegal volume name %s, should not end in .readonly or .backup\n",
4069                 as->parms[1].items->data);
4070         exit(1);
4071     }
4072     if (IsNumeric(as->parms[1].items->data)) {
4073         fprintf(STDERR, "Illegal volume name %s, should not be a number\n",
4074                 as->parms[1].items->data);
4075         exit(1);
4076     }
4077     MapHostToNetwork(&entry);
4078     code =
4079         UV_RenameVolume(&entry, as->parms[0].items->data,
4080                         as->parms[1].items->data);
4081     if (code) {
4082         PrintDiagnostics("rename", code);
4083         exit(1);
4084     }
4085     fprintf(STDOUT, "Renamed volume %s to %s\n", as->parms[0].items->data,
4086             as->parms[1].items->data);
4087     return 0;
4088 }
4089
4090 GetVolumeInfo(volid, server, part, voltype, rentry)
4091      afs_int32 *server, volid, *part, *voltype;
4092      register struct nvldbentry *rentry;
4093 {
4094     afs_int32 vcode;
4095     int i, index = -1;
4096
4097     vcode = VLDB_GetEntryByID(volid, -1, rentry);
4098     if (vcode) {
4099         fprintf(STDERR,
4100                 "Could not fetch the entry for volume %lu from VLDB \n",
4101                 (unsigned long)volid);
4102         PrintError("", vcode);
4103         return (vcode);
4104     }
4105     MapHostToNetwork(rentry);
4106     if (volid == rentry->volumeId[ROVOL]) {
4107         *voltype = ROVOL;
4108         for (i = 0; i < rentry->nServers; i++) {
4109             if ((index == -1) && (rentry->serverFlags[i] & ITSROVOL)
4110                 && !(rentry->serverFlags[i] & RO_DONTUSE))
4111                 index = i;
4112         }
4113         if (index == -1) {
4114             fprintf(STDERR,
4115                     "RO volume is not found in VLDB entry for volume %lu\n",
4116                     (unsigned long)volid);
4117             return -1;
4118         }
4119
4120         *server = rentry->serverNumber[index];
4121         *part = rentry->serverPartition[index];
4122         return 0;
4123     }
4124
4125     index = Lp_GetRwIndex(rentry);
4126     if (index == -1) {
4127         fprintf(STDERR,
4128                 "RW Volume is not found in VLDB entry for volume %lu\n",
4129                 (unsigned long)volid);
4130         return -1;
4131     }
4132     if (volid == rentry->volumeId[RWVOL]) {
4133         *voltype = RWVOL;
4134         *server = rentry->serverNumber[index];
4135         *part = rentry->serverPartition[index];
4136         return 0;
4137     }
4138     if (volid == rentry->volumeId[BACKVOL]) {
4139         *voltype = BACKVOL;
4140         *server = rentry->serverNumber[index];
4141         *part = rentry->serverPartition[index];
4142         return 0;
4143     }
4144     fprintf(STDERR,
4145             "unexpected volume type for volume %lu\n",
4146             (unsigned long)volid);
4147     return -1;
4148 }
4149
4150 static
4151 DeleteEntry(as)
4152      register struct cmd_syndesc *as;
4153
4154 {
4155     afs_int32 apart;
4156     afs_int32 avolid;
4157     afs_int32 vcode;
4158     struct VldbListByAttributes attributes;
4159     nbulkentries arrayEntries;
4160     register struct nvldbentry *vllist;
4161     struct cmd_item *itp;
4162     afs_int32 nentries;
4163     int j;
4164     char prefix[VOLSER_MAXVOLNAME + 1];
4165     int seenprefix = 0;
4166     afs_int32 totalBack = 0, totalFail = 0, err;
4167
4168     if (as->parms[0].items) {   /* -id */
4169         if (as->parms[1].items || as->parms[2].items || as->parms[3].items) {
4170             fprintf(STDERR,
4171                     "You cannot use -server, -partition, or -prefix with the -id argument\n");
4172             exit(-2);
4173         }
4174         for (itp = as->parms[0].items; itp; itp = itp->next) {
4175             avolid = vsu_GetVolumeID(itp->data, cstruct, &err);
4176             if (avolid == 0) {
4177                 if (err)
4178                     PrintError("", err);
4179                 else
4180                     fprintf(STDERR, "vos: can't find volume '%s'\n",
4181                             itp->data);
4182                 continue;
4183             }
4184             if (as->parms[4].items) {   /* -noexecute */
4185                 fprintf(STDOUT, "Would have deleted VLDB entry for %s \n",
4186                         itp->data);
4187                 fflush(STDOUT);
4188                 continue;
4189             }
4190             vcode = ubik_VL_DeleteEntry(cstruct, 0, avolid, RWVOL);
4191             if (vcode) {
4192                 fprintf(STDERR, "Could not delete entry for volume %s\n",
4193                         itp->data);
4194                 fprintf(STDERR,
4195                         "You must specify a RW volume name or ID "
4196                         "(the entire VLDB entry will be deleted)\n");
4197                 PrintError("", vcode);
4198                 totalFail++;
4199                 continue;
4200             }
4201             totalBack++;
4202         }
4203         fprintf(STDOUT, "Deleted %d VLDB entries\n", totalBack);
4204         return (totalFail);
4205     }
4206
4207     if (!as->parms[1].items && !as->parms[2].items && !as->parms[3].items) {
4208         fprintf(STDERR, "You must specify an option\n");
4209         exit(-2);
4210     }
4211
4212     /* Zero out search attributes */
4213     memset(&attributes, 0, sizeof(struct VldbListByAttributes));
4214
4215     if (as->parms[1].items) {   /* -prefix */
4216         strncpy(prefix, as->parms[1].items->data, VOLSER_MAXVOLNAME);
4217         seenprefix = 1;
4218         if (!as->parms[2].items && !as->parms[3].items) {       /* a single entry only */
4219             fprintf(STDERR,
4220                     "You must provide -server with the -prefix argument\n");
4221             exit(-2);
4222         }
4223     }
4224
4225     if (as->parms[2].items) {   /* -server */
4226         afs_int32 aserver;
4227         aserver = GetServer(as->parms[2].items->data);
4228         if (aserver == 0) {
4229             fprintf(STDERR, "vos: server '%s' not found in host table\n",
4230                     as->parms[2].items->data);
4231             exit(-1);
4232         }
4233         attributes.server = ntohl(aserver);
4234         attributes.Mask |= VLLIST_SERVER;
4235     }
4236
4237     if (as->parms[3].items) {   /* -partition */
4238         if (!as->parms[2].items) {
4239             fprintf(STDERR,
4240                     "You must provide -server with the -partition argument\n");
4241             exit(-2);
4242         }
4243         apart = volutil_GetPartitionID(as->parms[3].items->data);
4244         if (apart < 0) {
4245             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
4246                     as->parms[3].items->data);
4247             exit(-1);
4248         }
4249         attributes.partition = apart;
4250         attributes.Mask |= VLLIST_PARTITION;
4251     }
4252
4253     /* Print status line of what we are doing */
4254     fprintf(STDOUT, "Deleting VLDB entries for ");
4255     if (as->parms[2].items) {
4256         fprintf(STDOUT, "server %s ", as->parms[2].items->data);
4257     }
4258     if (as->parms[3].items) {
4259         char pname[10];
4260         MapPartIdIntoName(apart, pname);
4261         fprintf(STDOUT, "partition %s ", pname);
4262     }
4263     if (seenprefix) {
4264         fprintf(STDOUT, "which are prefixed with %s ", prefix);
4265     }
4266     fprintf(STDOUT, "\n");
4267     fflush(STDOUT);
4268
4269     /* Get all the VLDB entries on a server and/or partition */
4270     memset(&arrayEntries, 0, sizeof(arrayEntries));
4271     vcode = VLDB_ListAttributes(&attributes, &nentries, &arrayEntries);
4272     if (vcode) {
4273         fprintf(STDERR, "Could not access the VLDB for attributes\n");
4274         PrintError("", vcode);
4275         exit(-1);
4276     }
4277
4278     /* Process each entry */
4279     for (j = 0; j < nentries; j++) {
4280         vllist = &arrayEntries.nbulkentries_val[j];
4281         if (seenprefix) {
4282             /* It only deletes the RW volumes */
4283             if (strncmp(vllist->name, prefix, strlen(prefix))) {
4284                 if (verbose) {
4285                     fprintf(STDOUT,
4286                             "Omitting to delete %s due to prefix %s mismatch\n",
4287                             vllist->name, prefix);
4288                 }
4289                 fflush(STDOUT);
4290                 continue;
4291             }
4292         }
4293
4294         if (as->parms[4].items) {       /* -noexecute */
4295             fprintf(STDOUT, "Would have deleted VLDB entry for %s \n",
4296                     vllist->name);
4297             fflush(STDOUT);
4298             continue;
4299         }
4300
4301         /* Only matches the RW volume name */
4302         avolid = vllist->volumeId[RWVOL];
4303         vcode = ubik_VL_DeleteEntry(cstruct, 0, avolid, RWVOL);
4304         if (vcode) {
4305             fprintf(STDOUT, "Could not delete VDLB entry for  %s\n",
4306                     vllist->name);
4307             totalFail++;
4308             PrintError("", vcode);
4309             continue;
4310         } else {
4311             totalBack++;
4312             if (verbose)
4313                 fprintf(STDOUT, "Deleted VLDB entry for %s \n", vllist->name);
4314         }
4315         fflush(STDOUT);
4316     }                           /*for */
4317
4318     fprintf(STDOUT, "----------------------\n");
4319     fprintf(STDOUT,
4320             "Total VLDB entries deleted: %lu; failed to delete: %lu\n",
4321             (unsigned long)totalBack, (unsigned long)totalFail);
4322     if (arrayEntries.nbulkentries_val)
4323         free(arrayEntries.nbulkentries_val);
4324     return 0;
4325 }
4326
4327
4328 static int
4329 CompareVldbEntryByName(p1, p2)
4330      char *p1, *p2;
4331 {
4332     struct nvldbentry *arg1, *arg2;
4333
4334     arg1 = (struct nvldbentry *)p1;
4335     arg2 = (struct nvldbentry *)p2;
4336     return (strcmp(arg1->name, arg2->name));
4337 }
4338
4339 /*
4340 static int CompareVldbEntry(p1,p2)
4341 char *p1,*p2;
4342 {
4343     struct nvldbentry *arg1,*arg2;
4344     int i;
4345     int pos1, pos2;
4346     char comp1[100],comp2[100];
4347     char temp1[20],temp2[20];
4348
4349     arg1 = (struct nvldbentry *)p1;
4350     arg2 = (struct nvldbentry *)p2;
4351     pos1 = -1;
4352     pos2 = -1;
4353
4354     for(i = 0; i < arg1->nServers; i++)
4355         if(arg1->serverFlags[i] & ITSRWVOL) pos1 = i;
4356     for(i = 0; i < arg2->nServers; i++)
4357         if(arg2->serverFlags[i] & ITSRWVOL) pos2 = i;
4358     if(pos1 == -1 || pos2 == -1){
4359         pos1 = 0;
4360         pos2 = 0;
4361     }
4362     sprintf(comp1,"%10u",arg1->serverNumber[pos1]);
4363     sprintf(comp2,"%10u",arg2->serverNumber[pos2]);
4364     sprintf(temp1,"%10u",arg1->serverPartition[pos1]);
4365     sprintf(temp2,"%10u",arg2->serverPartition[pos2]);
4366     strcat(comp1,temp1);
4367     strcat(comp2,temp2);
4368     strcat(comp1,arg1->name);
4369     strcat(comp1,arg2->name);
4370     return(strcmp(comp1,comp2));
4371
4372 }
4373
4374 */
4375 static
4376 ListVLDB(as)
4377      struct cmd_syndesc *as;
4378 {
4379     afs_int32 apart;
4380     afs_int32 aserver, code;
4381     afs_int32 vcode;
4382     struct VldbListByAttributes attributes;
4383     nbulkentries arrayEntries;
4384     struct nvldbentry *vllist, *tarray = 0, *ttarray;
4385     afs_int32 centries, nentries = 0, tarraysize, parraysize;
4386     int j;
4387     char pname[10];
4388     int quiet, sort, lock;
4389     afs_int32 thisindex, nextindex;
4390
4391     aserver = 0;
4392     apart = 0;
4393
4394     attributes.Mask = 0;
4395     lock = (as->parms[3].items ? 1 : 0);        /* -lock   flag */
4396     quiet = (as->parms[4].items ? 1 : 0);       /* -quit   flag */
4397     sort = (as->parms[5].items ? 0 : 1);        /* -nosort flag */
4398
4399     /* If the volume name is given, Use VolumeInfoCmd to look it up
4400      * and not ListAttributes.
4401      */
4402     if (as->parms[0].items) {
4403         if (lock) {
4404             fprintf(STDERR,
4405                     "vos: illegal use of '-locked' switch, need to specify server and/or partition\n");
4406             exit(1);
4407         }
4408         code = VolumeInfoCmd(as->parms[0].items->data);
4409         if (code) {
4410             PrintError("", code);
4411             exit(1);
4412         }
4413         return 0;
4414     }
4415
4416     /* Server specified */
4417     if (as->parms[1].items) {
4418         aserver = GetServer(as->parms[1].items->data);
4419         if (aserver == 0) {
4420             fprintf(STDERR, "vos: server '%s' not found in host table\n",
4421                     as->parms[1].items->data);
4422             exit(1);
4423         }
4424         attributes.server = ntohl(aserver);
4425         attributes.Mask |= VLLIST_SERVER;
4426     }
4427
4428     /* Partition specified */
4429     if (as->parms[2].items) {
4430         apart = volutil_GetPartitionID(as->parms[2].items->data);
4431         if (apart < 0) {
4432             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
4433                     as->parms[2].items->data);
4434             exit(1);
4435         }
4436         attributes.partition = apart;
4437         attributes.Mask |= VLLIST_PARTITION;
4438     }
4439
4440     if (lock) {
4441         attributes.Mask |= VLLIST_FLAG;
4442         attributes.flag = VLOP_ALLOPERS;
4443     }
4444
4445     /* Print header information */
4446     if (!quiet) {
4447         MapPartIdIntoName(apart, pname);
4448         fprintf(STDOUT, "VLDB entries for %s %s%s%s %s\n",
4449                 (as->parms[1].items ? "server" : "all"),
4450                 (as->parms[1].items ? as->parms[1].items->data : "servers"),
4451                 (as->parms[2].items ? " partition " : ""),
4452                 (as->parms[2].items ? pname : ""),
4453                 (lock ? "which are locked:" : ""));
4454     }
4455
4456     for (thisindex = 0; (thisindex != -1); thisindex = nextindex) {
4457         memset(&arrayEntries, 0, sizeof(arrayEntries));
4458         centries = 0;
4459         nextindex = -1;
4460
4461         vcode =
4462             VLDB_ListAttributesN2(&attributes, 0, thisindex, &centries,
4463                                   &arrayEntries, &nextindex);
4464         if (vcode == RXGEN_OPCODE) {
4465             /* Vlserver not running with ListAttributesN2. Fall back */
4466             vcode =
4467                 VLDB_ListAttributes(&attributes, &centries, &arrayEntries);
4468             nextindex = -1;
4469         }
4470         if (vcode) {
4471             fprintf(STDERR, "Could not access the VLDB for attributes\n");
4472             PrintError("", vcode);
4473             exit(1);
4474         }
4475         nentries += centries;
4476
4477         /* We don't sort, so just print the entries now */
4478         if (!sort) {
4479             for (j = 0; j < centries; j++) {    /* process each entry */
4480                 vllist = &arrayEntries.nbulkentries_val[j];
4481                 MapHostToNetwork(vllist);
4482                 EnumerateEntry(vllist);
4483
4484                 if (vllist->flags & VLOP_ALLOPERS)
4485                     fprintf(STDOUT, "    Volume is currently LOCKED  \n");
4486             }
4487         }
4488
4489         /* So we sort. First we must collect all the entries and keep
4490          * them in memory.
4491          */
4492         else if (centries > 0) {
4493             if (!tarray) {
4494                 /* steal away the first bulk entries array */
4495                 tarray = (struct nvldbentry *)arrayEntries.nbulkentries_val;
4496                 tarraysize = centries * sizeof(struct nvldbentry);
4497                 arrayEntries.nbulkentries_val = 0;
4498             } else {
4499                 /* Grow the tarray to keep the extra entries */
4500                 parraysize = (centries * sizeof(struct nvldbentry));
4501                 ttarray =
4502                     (struct nvldbentry *)realloc(tarray,
4503                                                  tarraysize + parraysize);
4504                 if (!ttarray) {
4505                     fprintf(STDERR,
4506                             "Could not allocate enough space for  the VLDB entries\n");
4507                     goto bypass;
4508                 }
4509                 tarray = ttarray;
4510
4511                 /* Copy them in */
4512                 memcpy(((char *)tarray) + tarraysize,
4513                        (char *)arrayEntries.nbulkentries_val, parraysize);
4514                 tarraysize += parraysize;
4515             }
4516         }
4517
4518         /* Free the bulk array */
4519         if (arrayEntries.nbulkentries_val) {
4520             free(arrayEntries.nbulkentries_val);
4521             arrayEntries.nbulkentries_val = 0;
4522         }
4523     }
4524
4525     /* Here is where we now sort all the entries and print them */
4526     if (sort && (nentries > 0)) {
4527         qsort((char *)tarray, nentries, sizeof(struct nvldbentry),
4528               CompareVldbEntryByName);
4529         for (vllist = tarray, j = 0; j < nentries; j++, vllist++) {
4530             MapHostToNetwork(vllist);
4531             EnumerateEntry(vllist);
4532
4533             if (vllist->flags & VLOP_ALLOPERS)
4534                 fprintf(STDOUT, "    Volume is currently LOCKED  \n");
4535         }
4536     }
4537
4538   bypass:
4539     if (!quiet)
4540         fprintf(STDOUT, "\nTotal entries: %lu\n", (unsigned long)nentries);
4541     if (tarray)
4542         free(tarray);
4543     return 0;
4544 }
4545
4546 static
4547 BackSys(as)
4548      register struct cmd_syndesc *as;
4549 {
4550     afs_int32 apart = 0, avolid;
4551     afs_int32 aserver = 0, code, aserver1, apart1;
4552     afs_int32 vcode;
4553     struct VldbListByAttributes attributes;
4554     nbulkentries arrayEntries;
4555     register struct nvldbentry *vllist;
4556     afs_int32 nentries;
4557     int j;
4558     char pname[10];
4559     int seenprefix, seenxprefix, exclude, ex, exp, noaction;
4560     afs_int32 totalBack = 0;
4561     afs_int32 totalFail = 0;
4562     int previdx = -1, error, same;
4563     int comp = 0;
4564     struct cmd_item *ti;
4565     char *ccode;
4566     int match;
4567
4568     memset(&attributes, 0, sizeof(struct VldbListByAttributes));
4569     attributes.Mask = 0;
4570
4571     seenprefix = (as->parms[0].items ? 1 : 0);
4572     exclude = (as->parms[3].items ? 1 : 0);
4573     seenxprefix = (as->parms[4].items ? 1 : 0);
4574     noaction = (as->parms[5].items ? 1 : 0);
4575
4576     if (as->parms[1].items) {   /* -server */
4577         aserver = GetServer(as->parms[1].items->data);
4578         if (aserver == 0) {
4579             fprintf(STDERR, "vos: server '%s' not found in host table\n",
4580                     as->parms[1].items->data);
4581             exit(1);
4582         }
4583         attributes.server = ntohl(aserver);
4584         attributes.Mask |= VLLIST_SERVER;
4585     }
4586
4587     if (as->parms[2].items) {   /* -partition */
4588         apart = volutil_GetPartitionID(as->parms[2].items->data);
4589         if (apart < 0) {
4590             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
4591                     as->parms[2].items->data);
4592             exit(1);
4593         }
4594         attributes.partition = apart;
4595         attributes.Mask |= VLLIST_PARTITION;
4596     }
4597
4598     /* Check to make sure the prefix and xprefix expressions compile ok */
4599     if (seenprefix) {
4600         for (ti = as->parms[0].items; ti; ti = ti->next) {
4601             if (strncmp(ti->data, "^", 1) == 0) {
4602 #ifdef HAVE_POSIX_REGEX
4603                 regex_t re;
4604                 char errbuf[256];
4605
4606                 code = regcomp(&re, ti->data, REG_NOSUB);
4607                 if (code != 0) {
4608                     regerror(code, &re, errbuf, sizeof errbuf);
4609                     fprintf(STDERR,
4610                             "Unrecognizable -prefix regular expression: '%s': %s\n",
4611                             ti->data, errbuf);
4612                     exit(1);
4613                 }
4614                 regfree(&re);
4615 #else
4616                 ccode = (char *)re_comp(ti->data);
4617                 if (ccode) {
4618                     fprintf(STDERR,
4619                             "Unrecognizable -prefix regular expression: '%s': %s\n",
4620                             ti->data, ccode);
4621                     exit(1);
4622                 }
4623 #endif
4624             }
4625         }
4626     }
4627     if (seenxprefix) {
4628         for (ti = as->parms[4].items; ti; ti = ti->next) {
4629             if (strncmp(ti->data, "^", 1) == 0) {
4630 #ifdef HAVE_POSIX_REGEX
4631                 regex_t re;
4632                 char errbuf[256];
4633
4634                 code = regcomp(&re, ti->data, REG_NOSUB);
4635                 if (code != 0) {
4636                     regerror(code, &re, errbuf, sizeof errbuf);
4637                     fprintf(STDERR,
4638                             "Unrecognizable -xprefix regular expression: '%s': %s\n",
4639                             ti->data, errbuf);
4640                     exit(1);
4641                 }
4642                 regfree(&re);
4643 #else
4644                 ccode = (char *)re_comp(ti->data);
4645                 if (ccode) {
4646                     fprintf(STDERR,
4647                             "Unrecognizable -xprefix regular expression: '%s': %s\n",
4648                             ti->data, ccode);
4649                     exit(1);
4650                 }
4651 #endif
4652             }
4653         }
4654     }
4655
4656     memset(&arrayEntries, 0, sizeof(arrayEntries));     /* initialize to hint the stub to alloc space */
4657     vcode = VLDB_ListAttributes(&attributes, &nentries, &arrayEntries);
4658     if (vcode) {
4659         fprintf(STDERR, "Could not access the VLDB for attributes\n");
4660         PrintError("", vcode);
4661         exit(1);
4662     }
4663
4664     if (as->parms[1].items || as->parms[2].items || verbose) {
4665         fprintf(STDOUT, "%s up volumes",
4666                 (noaction ? "Would have backed" : "Backing"));
4667
4668         if (as->parms[1].items) {
4669             fprintf(STDOUT, " on server %s", as->parms[1].items->data);
4670         } else if (as->parms[2].items) {
4671             fprintf(STDOUT, " for all servers");
4672         }
4673
4674         if (as->parms[2].items) {
4675             MapPartIdIntoName(apart, pname);
4676             fprintf(STDOUT, " partition %s", pname);
4677         }
4678
4679         if (seenprefix || (!seenprefix && seenxprefix)) {
4680             ti = (seenprefix ? as->parms[0].items : as->parms[4].items);
4681             ex = (seenprefix ? exclude : !exclude);
4682             exp = (strncmp(ti->data, "^", 1) == 0);
4683             fprintf(STDOUT, " which %smatch %s '%s'", (ex ? "do not " : ""),
4684                     (exp ? "expression" : "prefix"), ti->data);
4685             for (ti = ti->next; ti; ti = ti->next) {
4686                 exp = (strncmp(ti->data, "^", 1) == 0);
4687                 printf(" %sor %s '%s'", (ex ? "n" : ""),
4688                        (exp ? "expression" : "prefix"), ti->data);
4689             }
4690         }
4691
4692         if (seenprefix && seenxprefix) {
4693             ti = as->parms[4].items;
4694             exp = (strncmp(ti->data, "^", 1) == 0);
4695             fprintf(STDOUT, " %swhich match %s '%s'",
4696                     (exclude ? "adding those " : "removing those "),
4697                     (exp ? "expression" : "prefix"), ti->data);
4698             for (ti = ti->next; ti; ti = ti->next) {
4699                 exp = (strncmp(ti->data, "^", 1) == 0);
4700                 printf(" or %s '%s'", (exp ? "expression" : "prefix"),
4701                        ti->data);
4702             }
4703         }
4704         fprintf(STDOUT, " .. ");
4705         if (verbose)
4706             fprintf(STDOUT, "\n");
4707         fflush(STDOUT);
4708     }
4709
4710     for (j = 0; j < nentries; j++) {    /* process each vldb entry */
4711         vllist = &arrayEntries.nbulkentries_val[j];
4712
4713         if (seenprefix) {
4714             for (ti = as->parms[0].items; ti; ti = ti->next) {
4715                 if (strncmp(ti->data, "^", 1) == 0) {
4716 #ifdef HAVE_POSIX_REGEX
4717                     regex_t re;
4718                     char errbuf[256];
4719
4720                     /* XXX -- should just do the compile once! */
4721                     code = regcomp(&re, ti->data, REG_NOSUB);
4722                     if (code != 0) {
4723                         regerror(code, &re, errbuf, sizeof errbuf);
4724                         fprintf(STDERR,
4725                                 "Error in -prefix regular expression: '%s': %s\n",
4726                                 ti->data, errbuf);
4727                         exit(1);
4728                     }
4729                     match = (regexec(&re, vllist->name, 0, NULL, 0) == 0);
4730                     regfree(&re);
4731 #else
4732                     ccode = (char *)re_comp(ti->data);
4733                     if (ccode) {
4734                         fprintf(STDERR,
4735                                 "Error in -prefix regular expression: '%s': %s\n",
4736                                 ti->data, ccode);
4737                         exit(1);
4738                     }
4739                     match = (re_exec(vllist->name) == 1);
4740 #endif
4741                 } else {
4742                     match =
4743                         (strncmp(vllist->name, ti->data, strlen(ti->data)) ==
4744                          0);
4745                 }
4746                 if (match)
4747                     break;
4748             }
4749         } else {
4750             match = 1;
4751         }
4752
4753         /* Without the -exclude flag: If it matches the prefix, then
4754          *    check if we want to exclude any from xprefix.
4755          * With the -exclude flag: If it matches the prefix, then
4756          *    check if we want to add any from xprefix.
4757          */
4758         if (match && seenxprefix) {
4759             for (ti = as->parms[4].items; ti; ti = ti->next) {
4760                 if (strncmp(ti->data, "^", 1) == 0) {
4761 #ifdef HAVE_POSIX_REGEX
4762                     regex_t re;
4763                     char errbuf[256];
4764
4765                     /* XXX -- should just do the compile once! */
4766                     code = regcomp(&re, ti->data, REG_NOSUB);
4767                     if (code != 0) {
4768                         regerror(code, &re, errbuf, sizeof errbuf);
4769                         fprintf(STDERR,
4770                                 "Error in -xprefix regular expression: '%s': %s\n",
4771                                 ti->data, errbuf);
4772                         exit(1);
4773                     }
4774                     if (regexec(&re, vllist->name, 0, NULL, 0) == 0)
4775                             match = 0;
4776                     regfree(&re);
4777 #else
4778                     ccode = (char *)re_comp(ti->data);
4779                     if (ccode) {
4780                         fprintf(STDERR,
4781                                 "Error in -xprefix regular expression: '%s': %s\n",
4782                                 ti->data, ccode);
4783                         exit(1);
4784                     }
4785                     if (re_exec(vllist->name) == 1) {
4786                         match = 0;
4787                         break;
4788                     }
4789 #endif
4790                 } else {
4791                     if (strncmp(vllist->name, ti->data, strlen(ti->data)) ==
4792                         0) {
4793                         match = 0;
4794                         break;
4795                     }
4796                 }
4797             }
4798         }
4799
4800         if (exclude)
4801             match = !match;     /* -exclude will reverse the match */
4802         if (!match)
4803             continue;           /* Skip if no match */
4804
4805         /* Print list of volumes to backup */
4806         if (noaction) {
4807             fprintf(STDOUT, "     %s\n", vllist->name);
4808             continue;
4809         }
4810
4811         if (!(vllist->flags & RW_EXISTS)) {
4812             if (verbose) {
4813                 fprintf(STDOUT,
4814                         "Omitting to backup %s since RW volume does not exist \n",
4815                         vllist->name);
4816                 fprintf(STDOUT, "\n");
4817             }
4818             fflush(STDOUT);
4819             continue;
4820         }
4821
4822         avolid = vllist->volumeId[RWVOL];
4823         MapHostToNetwork(vllist);
4824         GetServerAndPart(vllist, RWVOL, &aserver1, &apart1, &previdx);
4825         if (aserver1 == -1 || apart1 == -1) {
4826             fprintf(STDOUT, "could not backup %s, invalid VLDB entry\n",
4827                     vllist->name);
4828             totalFail++;
4829             continue;
4830         }
4831         if (aserver) {
4832             same = VLDB_IsSameAddrs(aserver, aserver1, &error);
4833             if (error) {
4834                 fprintf(STDERR,
4835                         "Failed to get info about server's %d address(es) from vlserver (err=%d); aborting call!\n",
4836                         aserver, error);
4837                 totalFail++;
4838                 continue;
4839             }
4840         }
4841         if ((aserver && !same) || (apart && (apart != apart1))) {
4842             if (verbose) {
4843                 fprintf(STDOUT,
4844                         "Omitting to backup %s since the RW is in a different location\n",
4845                         vllist->name);
4846             }
4847             continue;
4848         }
4849         if (verbose) {
4850             time_t now = time(0);
4851             fprintf(STDOUT, "Creating backup volume for %s on %s",
4852                     vllist->name, ctime(&now));
4853             fflush(STDOUT);
4854         }
4855
4856         code = UV_BackupVolume(aserver1, apart1, avolid);
4857         if (code) {
4858             fprintf(STDOUT, "Could not backup %s\n", vllist->name);
4859             totalFail++;
4860         } else {
4861             totalBack++;
4862         }
4863         if (verbose)
4864             fprintf(STDOUT, "\n");
4865         fflush(STDOUT);
4866     }                           /* process each vldb entry */
4867     fprintf(STDOUT, "done\n");
4868     fprintf(STDOUT, "Total volumes backed up: %lu; failed to backup: %lu\n",
4869             (unsigned long)totalBack, (unsigned long)totalFail);
4870     fflush(STDOUT);
4871     if (arrayEntries.nbulkentries_val)
4872         free(arrayEntries.nbulkentries_val);
4873     return 0;
4874 }
4875
4876 static
4877 UnlockVLDB(as)
4878      register struct cmd_syndesc *as;
4879 {
4880     afs_int32 apart;
4881     afs_int32 aserver, code;
4882     afs_int32 vcode;
4883     struct VldbListByAttributes attributes;
4884     nbulkentries arrayEntries;
4885     register struct nvldbentry *vllist;
4886     afs_int32 nentries;
4887     int j;
4888     afs_int32 volid;
4889     afs_int32 totalE;
4890     char pname[10];
4891
4892     apart = -1;
4893     totalE = 0;
4894     attributes.Mask = 0;
4895
4896     if (as->parms[0].items) {   /* server specified */
4897         aserver = GetServer(as->parms[0].items->data);
4898         if (aserver == 0) {
4899             fprintf(STDERR, "vos: server '%s' not found in host table\n",
4900                     as->parms[0].items->data);
4901             exit(1);
4902         }
4903         attributes.server = ntohl(aserver);
4904         attributes.Mask |= VLLIST_SERVER;
4905     }
4906     if (as->parms[1].items) {   /* partition specified */
4907         apart = volutil_GetPartitionID(as->parms[1].items->data);
4908         if (apart < 0) {
4909             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
4910                     as->parms[1].items->data);
4911             exit(1);
4912         }
4913         if (!IsPartValid(apart, aserver, &code)) {      /*check for validity of the partition */
4914             if (code)
4915                 PrintError("", code);
4916             else
4917                 fprintf(STDERR,
4918                         "vos : partition %s does not exist on the server\n",
4919                         as->parms[1].items->data);
4920             exit(1);
4921         }
4922         attributes.partition = apart;
4923         attributes.Mask |= VLLIST_PARTITION;
4924     }
4925     attributes.flag = VLOP_ALLOPERS;
4926     attributes.Mask |= VLLIST_FLAG;
4927     memset(&arrayEntries, 0, sizeof(arrayEntries));     /*initialize to hint the stub  to alloc space */
4928     vcode = VLDB_ListAttributes(&attributes, &nentries, &arrayEntries);
4929     if (vcode) {
4930         fprintf(STDERR, "Could not access the VLDB for attributes\n");
4931         PrintError("", vcode);
4932         exit(1);
4933     }
4934     for (j = 0; j < nentries; j++) {    /* process each entry */
4935         vllist = &arrayEntries.nbulkentries_val[j];
4936         volid = vllist->volumeId[RWVOL];
4937         vcode =
4938             ubik_VL_ReleaseLock(cstruct, 0, volid, -1,
4939                                 LOCKREL_OPCODE | LOCKREL_AFSID | 
4940                                 LOCKREL_TIMESTAMP);
4941         if (vcode) {
4942             fprintf(STDERR, "Could not unlock entry for volume %s\n",
4943                     vllist->name);
4944             PrintError("", vcode);
4945             totalE++;
4946         }
4947
4948     }
4949     MapPartIdIntoName(apart, pname);
4950     if (totalE)
4951         fprintf(STDOUT,
4952                 "Could not lock %lu VLDB entries of %lu locked entries\n",
4953                 (unsigned long)totalE, (unsigned long)nentries);
4954     else {
4955         if (as->parms[0].items) {
4956             fprintf(STDOUT,
4957                     "Unlocked all the VLDB entries for volumes on server %s ",
4958                     as->parms[0].items->data);
4959             if (as->parms[1].items) {
4960                 MapPartIdIntoName(apart, pname);
4961                 fprintf(STDOUT, "partition %s\n", pname);
4962             } else
4963                 fprintf(STDOUT, "\n");
4964
4965         } else if (as->parms[1].items) {
4966             MapPartIdIntoName(apart, pname);
4967             fprintf(STDOUT,
4968                     "Unlocked all the VLDB entries for volumes on partition %s on all servers\n",
4969                     pname);
4970         }
4971     }
4972
4973     if (arrayEntries.nbulkentries_val)
4974         free(arrayEntries.nbulkentries_val);
4975     return 0;
4976 }
4977
4978 static char *
4979 PrintInt64Size(afs_uint64 in)
4980 {
4981     register afs_uint32 hi, lo;
4982     register char * units;
4983     static char output[16];
4984
4985     SplitInt64(in,hi,lo);
4986
4987     if (hi == 0) {
4988         units = "KB";
4989     } else if (!(hi & 0xFFFFFC00)) {
4990         units = "MB";
4991         lo = (hi << 22) | (lo >> 10);
4992     } else if (!(hi & 0xFFF00000)) {
4993         units = "GB";
4994         lo = (hi << 12) | (lo >> 20);
4995     } else if (!(hi & 0xC0000000)) {
4996         units = "TB";
4997         lo = (hi << 2) | (lo >> 30);
4998     } else {
4999         units = "PB";
5000         lo = (hi >> 8);
5001     }
5002     sprintf(output,"%u %s", lo, units);
5003     return output;
5004 }
5005
5006 static
5007 PartitionInfo(as)
5008      register struct cmd_syndesc *as;
5009 {
5010     afs_int32 apart;
5011     afs_int32 aserver, code;
5012     char pname[10];
5013     struct diskPartition partition;
5014     struct partList dummyPartList;
5015     int i, cnt;
5016     int printSummary=0, sumPartitions=0;
5017     afs_uint64 sumFree, sumStorage, tmp;
5018
5019     ZeroInt64(sumFree);
5020     ZeroInt64(sumStorage);
5021     apart = -1;
5022     aserver = GetServer(as->parms[0].items->data);
5023     if (aserver == 0) {
5024         fprintf(STDERR, "vos: server '%s' not found in host table\n",
5025                 as->parms[0].items->data);
5026         exit(1);
5027     }
5028     if (as->parms[1].items) {
5029         apart = volutil_GetPartitionID(as->parms[1].items->data);
5030         if (apart < 0) {
5031             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
5032                     as->parms[1].items->data);
5033             exit(1);
5034         }
5035         dummyPartList.partId[0] = apart;
5036         dummyPartList.partFlags[0] = PARTVALID;
5037         cnt = 1;
5038     }
5039     if (as->parms[2].items) {
5040         printSummary = 1;
5041     }
5042     if (apart != -1) {
5043         if (!IsPartValid(apart, aserver, &code)) {      /*check for validity of the partition */
5044             if (code)
5045                 PrintError("", code);
5046             else
5047                 fprintf(STDERR,
5048                         "vos : partition %s does not exist on the server\n",
5049                         as->parms[1].items->data);
5050             exit(1);
5051         }
5052     } else {
5053         code = UV_ListPartitions(aserver, &dummyPartList, &cnt);
5054         if (code) {
5055             PrintDiagnostics("listpart", code);
5056             exit(1);
5057         }
5058     }
5059     for (i = 0; i < cnt; i++) {
5060         if (dummyPartList.partFlags[i] & PARTVALID) {
5061             MapPartIdIntoName(dummyPartList.partId[i], pname);
5062             code = UV_PartitionInfo(aserver, pname, &partition);
5063             if (code) {
5064                 fprintf(STDERR, "Could not get information on partition %s\n",
5065                         pname);
5066                 PrintError("", code);
5067                 exit(1);
5068             }
5069             fprintf(STDOUT,
5070                     "Free space on partition %s: %d K blocks out of total %d\n",
5071                     pname, partition.free, partition.minFree);
5072             sumPartitions++;
5073             FillInt64(tmp,0,partition.free);
5074             AddUInt64(sumFree,tmp,&sumFree);
5075             FillInt64(tmp,0,partition.minFree);
5076             AddUInt64(sumStorage,tmp,&sumStorage);
5077         }
5078     }
5079     if (printSummary) {
5080         fprintf(STDOUT,
5081                 "Summary: %s free out of ",
5082                 PrintInt64Size(sumFree));
5083         fprintf(STDOUT,
5084                 "%s on %d partitions\n",
5085                 PrintInt64Size(sumStorage), 
5086                 sumPartitions);
5087     }
5088     return 0;
5089 }
5090
5091 static
5092 ChangeAddr(as)
5093      register struct cmd_syndesc *as;
5094
5095 {
5096     afs_int32 ip1, ip2, vcode;
5097     int remove = 0;
5098
5099     ip1 = GetServer(as->parms[0].items->data);
5100     if (!ip1) {
5101         fprintf(STDERR, "vos: invalid host address\n");
5102         return (EINVAL);
5103     }
5104
5105     if ((as->parms[1].items && as->parms[2].items)
5106         || (!as->parms[1].items && !as->parms[2].items)) {
5107         fprintf(STDERR,
5108                 "vos: Must specify either '-newaddr <addr>' or '-remove' flag\n");
5109         return (EINVAL);
5110     }
5111
5112     if (as->parms[1].items) {
5113         ip2 = GetServer(as->parms[1].items->data);
5114         if (!ip2) {
5115             fprintf(STDERR, "vos: invalid host address\n");
5116             return (EINVAL);
5117         }
5118     } else {
5119         /* Play a trick here. If we are removing an address, ip1 will be -1
5120          * and ip2 will be the original address. This switch prevents an 
5121          * older revision vlserver from removing the IP address.
5122          */
5123         remove = 1;
5124         ip2 = ip1;
5125         ip1 = 0xffffffff;
5126     }
5127
5128     vcode = ubik_Call_New(VL_ChangeAddr, cstruct, 0, ntohl(ip1), ntohl(ip2));
5129     if (vcode) {
5130         if (remove) {
5131             fprintf(STDERR, "Could not remove server %s from the VLDB\n",
5132                     as->parms[0].items->data);
5133             if (vcode == VL_NOENT) {
5134                 fprintf(STDERR,
5135                         "vlserver does not support the remove flag or ");
5136             }
5137         } else {
5138             fprintf(STDERR, "Could not change server %s to server %s\n",
5139                     as->parms[0].items->data, as->parms[1].items->data);
5140         }
5141         PrintError("", vcode);
5142         return (vcode);
5143     }
5144
5145     if (remove) {
5146         fprintf(STDOUT, "Removed server %s from the VLDB\n",
5147                 as->parms[0].items->data);
5148     } else {
5149         fprintf(STDOUT, "Changed server %s to server %s\n",
5150                 as->parms[0].items->data, as->parms[1].items->data);
5151     }
5152     return 0;
5153 }
5154
5155 static void
5156 print_addrs(const bulkaddrs * addrs, const afsUUID * m_uuid, int nentries,
5157             int print, int noresolve)
5158 {
5159     afs_int32 vcode;
5160     afs_int32 i, j;
5161     struct VLCallBack vlcb;
5162     afs_int32 *addrp;
5163     bulkaddrs m_addrs;
5164     ListAddrByAttributes m_attrs;
5165     afs_int32 m_nentries, *m_addrp;
5166     afs_int32 base, index;
5167     char buf[1024];
5168
5169     if (print) {
5170         afsUUID_to_string(m_uuid, buf, sizeof(buf));
5171         printf("UUID: %s\n", buf);
5172     }
5173
5174     /* print out the list of all the server */
5175     addrp = (afs_int32 *) addrs->bulkaddrs_val;
5176     for (i = 0; i < nentries; i++, addrp++) {
5177         /* If it is a multihomed address, then we will need to 
5178          * get the addresses for this multihomed server from
5179          * the vlserver and print them.
5180          */
5181         if (((*addrp & 0xff000000) == 0xff000000) && ((*addrp) & 0xffff)) {
5182             /* Get the list of multihomed fileservers */
5183             base = (*addrp >> 16) & 0xff;
5184             index = (*addrp) & 0xffff;
5185
5186             if ((base >= 0) && (base <= VL_MAX_ADDREXTBLKS) && (index >= 1)
5187                 && (index <= VL_MHSRV_PERBLK)) {
5188                 m_attrs.Mask = VLADDR_INDEX;
5189                 m_attrs.index = (base * VL_MHSRV_PERBLK) + index;
5190                 m_nentries = 0;
5191                 m_addrs.bulkaddrs_val = 0;
5192                 m_addrs.bulkaddrs_len = 0;
5193                 vcode =
5194                     ubik_VL_GetAddrsU(cstruct, 0, &m_attrs, &m_uuid,
5195                                       &vlcb, &m_nentries, &m_addrs);
5196                 if (vcode) {
5197                     fprintf(STDERR,
5198                             "vos: could not list the multi-homed server addresses\n");
5199                     PrintError("", vcode);
5200                 }
5201
5202                 /* Print the list */
5203                 m_addrp = (afs_int32 *) m_addrs.bulkaddrs_val;
5204                 for (j = 0; j < m_nentries; j++, m_addrp++) {
5205                     *m_addrp = htonl(*m_addrp);
5206                     if (noresolve) {
5207                         char hoststr[16];
5208                         printf("%s ", afs_inet_ntoa_r(*m_addrp, hoststr));
5209                     } else {
5210                         printf("%s ", hostutil_GetNameByINet(*m_addrp));
5211                     }
5212                 }
5213                 if (j == 0) {
5214                     printf("<unknown>\n");
5215                 } else {
5216                     printf("\n");
5217                 }
5218
5219                 continue;
5220             }
5221         }
5222
5223         /* Otherwise, it is a non-multihomed entry and contains
5224          * the IP address of the server - print it.
5225          */
5226         *addrp = htonl(*addrp);
5227         if (noresolve) {
5228             char hoststr[16];
5229             printf("%s\n", afs_inet_ntoa_r(*addrp, hoststr));
5230         } else {
5231             printf("%s\n", hostutil_GetNameByINet(*addrp));
5232         }
5233     }
5234
5235     if (print) {
5236         printf("\n");
5237     }
5238     return;
5239 }
5240
5241 static
5242 ListAddrs(as)
5243      register struct cmd_syndesc *as;
5244 {
5245     afs_int32 vcode;
5246     afs_int32 i, noresolve = 0, printuuid = 0;
5247     struct VLCallBack vlcb;
5248     afs_int32 nentries;
5249     bulkaddrs m_addrs;
5250     ListAddrByAttributes m_attrs;
5251     afsUUID m_uuid, askuuid;
5252     afs_int32 m_nentries;
5253
5254     memset(&m_attrs, 0, sizeof(struct ListAddrByAttributes));
5255     m_attrs.Mask = VLADDR_INDEX;
5256
5257     memset(&m_addrs, 0, sizeof(bulkaddrs));
5258     memset(&askuuid, 0, sizeof(afsUUID));
5259     if (as->parms[0].items) {
5260         /* -uuid */
5261         if (afsUUID_from_string(as->parms[0].items->data, &askuuid) < 0) {
5262             fprintf(STDERR, "vos: invalid UUID '%s'\n", 
5263                     as->parms[0].items->data);
5264             exit(-1);
5265         }
5266         m_attrs.Mask = VLADDR_UUID;
5267         m_attrs.uuid = askuuid;
5268     }
5269     if (as->parms[1].items) {
5270         /* -host */
5271         struct hostent *he;
5272         afs_int32 saddr;
5273         he = hostutil_GetHostByName((char *)as->parms[1].items->data);
5274         if (he == NULL) {
5275             fprintf(STDERR, "vos: Can't get host info for '%s'\n",
5276                     as->parms[1].items->data);
5277             exit(-1);
5278         }
5279         memcpy(&saddr, he->h_addr, 4);
5280         m_attrs.Mask = VLADDR_IPADDR;
5281         m_attrs.ipaddr = ntohl(saddr);
5282     }
5283     if (as->parms[2].items) {
5284         noresolve = 1;
5285     }
5286     if (as->parms[3].items) {
5287         printuuid = 1;
5288     }
5289
5290     m_addrs.bulkaddrs_val = 0;
5291     m_addrs.bulkaddrs_len = 0;
5292
5293     vcode =
5294         ubik_Call_New(VL_GetAddrs, cstruct, 0, 0, 0, &vlcb, &nentries,
5295                       &m_addrs);
5296     if (vcode) {
5297         fprintf(STDERR, "vos: could not list the server addresses\n");
5298         PrintError("", vcode);
5299         return (vcode);
5300     }
5301
5302     m_nentries = 0;
5303     m_addrs.bulkaddrs_val = 0;
5304     m_addrs.bulkaddrs_len = 0;
5305     i = 1;
5306     while (1) {
5307         m_attrs.index = i;
5308
5309         vcode =
5310             ubik_Call_New(VL_GetAddrsU, cstruct, 0, &m_attrs, &m_uuid,
5311                           &vlcb, &m_nentries, &m_addrs);
5312
5313         if (vcode == VL_NOENT) {
5314             if (m_attrs.Mask == VLADDR_UUID) {
5315                 fprintf(STDERR, "vos: no entry for UUID '%s' found in VLDB\n",
5316                         as->parms[0].items->data);
5317                 exit(-1);
5318             } else if (m_attrs.Mask == VLADDR_IPADDR) {
5319                 fprintf(STDERR, "vos: no entry for host '%s' [0x%08x] found in VLDB\n",
5320                         as->parms[1].items->data, m_attrs.ipaddr);
5321                 exit(-1);
5322             } else {
5323                 i++;
5324                 nentries++;
5325                 continue;
5326             }
5327         }
5328
5329         if (vcode == VL_INDEXERANGE) {
5330             break;
5331         }
5332
5333         if (vcode) {
5334             fprintf(STDERR, "vos: could not list the server addresses\n");
5335             PrintError("", vcode);
5336             return (vcode);
5337         }
5338
5339         print_addrs(&m_addrs, &m_uuid, m_nentries, printuuid, noresolve);
5340         i++;
5341
5342         if ((as->parms[1].items) || (as->parms[0].items) || (i > nentries))
5343             break;
5344     }
5345
5346     return 0;
5347 }
5348
5349 static
5350 LockEntry(as)
5351      register struct cmd_syndesc *as;
5352
5353 {
5354     afs_int32 avolid, vcode, err;
5355
5356     avolid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
5357     if (avolid == 0) {
5358         if (err)
5359             PrintError("", err);
5360         else
5361             fprintf(STDERR, "vos: can't find volume '%s'\n",
5362                     as->parms[0].items->data);
5363         exit(1);
5364     }
5365     vcode = ubik_VL_SetLock(cstruct, 0, avolid, -1, VLOP_DELETE);
5366     if (vcode) {
5367         fprintf(STDERR, "Could not lock VLDB entry for volume %s\n",
5368                 as->parms[0].items->data);
5369         PrintError("", vcode);
5370         exit(1);
5371     }
5372     fprintf(STDOUT, "Locked VLDB entry for volume %s\n",
5373             as->parms[0].items->data);
5374     return 0;
5375 }
5376
5377 static
5378 ConvertRO(as)
5379      register struct cmd_syndesc *as;
5380
5381 {
5382     afs_int32 partition = -1;
5383     afs_int32 server, volid, code, i, same;
5384     struct nvldbentry entry, storeEntry;
5385     afs_int32 vcode;
5386     afs_int32 rwindex;
5387     afs_int32 rwserver = 0;
5388     afs_int32 rwpartition;
5389     afs_int32 roindex;
5390     afs_int32 roserver = 0;
5391     afs_int32 ropartition;
5392     int force = 0;
5393     struct rx_connection *aconn;
5394     char c, dc;
5395
5396     server = GetServer(as->parms[0].items->data);
5397     if (!server) {
5398         fprintf(STDERR, "vos: host '%s' not found in host table\n",
5399                 as->parms[0].items->data);
5400         return ENOENT;
5401     }
5402     partition = volutil_GetPartitionID(as->parms[1].items->data);
5403     if (partition < 0) {
5404         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
5405                 as->parms[1].items->data);
5406         return ENOENT;
5407     }
5408     if (!IsPartValid(partition, server, &code)) {
5409         if (code)
5410             PrintError("", code);
5411         else
5412             fprintf(STDERR,
5413                     "vos : partition %s does not exist on the server\n",
5414                     as->parms[1].items->data);
5415         return ENOENT;
5416     }
5417     volid = vsu_GetVolumeID(as->parms[2].items->data, cstruct, &code);
5418     if (volid == 0) {
5419         if (code)
5420             PrintError("", code);
5421         else
5422             fprintf(STDERR, "Unknown volume ID or name '%s'\n",
5423                     as->parms[0].items->data);
5424         return -1;
5425     }
5426     if (as->parms[3].items)
5427         force = 1;
5428
5429     vcode = VLDB_GetEntryByID(volid, -1, &entry);
5430     if (vcode) {
5431         fprintf(STDERR,
5432                 "Could not fetch the entry for volume %lu from VLDB\n",
5433                 (unsigned long)volid);
5434         PrintError("convertROtoRW", code);
5435         return vcode;
5436     }
5437
5438     /* use RO volid even if user specified RW or BK volid */
5439
5440     if (volid != entry.volumeId[ROVOL])
5441         volid = entry.volumeId[ROVOL];
5442
5443     MapHostToNetwork(&entry);
5444     for (i = 0; i < entry.nServers; i++) {
5445         if (entry.serverFlags[i] & ITSRWVOL) {
5446             rwindex = i;
5447             rwserver = entry.serverNumber[i];
5448             rwpartition = entry.serverPartition[i];
5449         }
5450         if (entry.serverFlags[i] & ITSROVOL) {
5451             same = VLDB_IsSameAddrs(server, entry.serverNumber[i], &code);
5452             if (code) {
5453                 fprintf(STDERR,
5454                         "Failed to get info about server's %d address(es) from vlserver (err=%d); aborting call!\n",
5455                         server, code);
5456                 return ENOENT;
5457             }
5458             if (same) {
5459                 roindex = i;
5460                 roserver = entry.serverNumber[i];
5461                 ropartition = entry.serverPartition[i];
5462                 break;
5463             }
5464         }
5465     }
5466     if (!roserver) {
5467         fprintf(STDERR, "Warning: RO volume didn't exist in vldb!\n");
5468     }
5469     if (ropartition != partition) {
5470         fprintf(STDERR,
5471                 "Warning: RO volume should be in partition %d instead of %d (vldb)\n",
5472                 ropartition, partition);
5473     }
5474
5475     if (rwserver) {
5476         fprintf(STDERR,
5477                 "VLDB indicates that a RW volume exists already on %s in partition %s.\n",
5478                 hostutil_GetNameByINet(rwserver),
5479                 volutil_PartitionName(rwpartition));
5480         if (!force) {
5481             fprintf(STDERR, "Overwrite this VLDB entry? [y|n] (n)\n");
5482             dc = c = getchar();
5483             while (!(dc == EOF || dc == '\n'))
5484                 dc = getchar(); /* goto end of line */
5485             if ((c != 'y') && (c != 'Y')) {
5486                 fprintf(STDERR, "aborted.\n");
5487                 return -1;
5488             }
5489         }
5490     }
5491
5492     vcode =
5493         ubik_VL_SetLock(cstruct, 0, entry.volumeId[RWVOL], RWVOL,
5494                   VLOP_MOVE);
5495     aconn = UV_Bind(server, AFSCONF_VOLUMEPORT);
5496     code = AFSVolConvertROtoRWvolume(aconn, partition, volid);
5497     if (code) {
5498         fprintf(STDERR,
5499                 "Converting RO volume %lu to RW volume failed with code %d\n",
5500                 (unsigned long)volid, code);
5501         PrintError("convertROtoRW ", code);
5502         return -1;
5503     }
5504     entry.serverFlags[roindex] = ITSRWVOL;
5505     entry.flags |= RW_EXISTS;
5506     entry.flags &= ~BACK_EXISTS;
5507     if (rwserver) {
5508         (entry.nServers)--;
5509         if (rwindex != entry.nServers) {
5510             entry.serverNumber[rwindex] = entry.serverNumber[entry.nServers];
5511             entry.serverPartition[rwindex] =
5512                 entry.serverPartition[entry.nServers];
5513             entry.serverFlags[rwindex] = entry.serverFlags[entry.nServers];
5514             entry.serverNumber[entry.nServers] = 0;
5515             entry.serverPartition[entry.nServers] = 0;
5516             entry.serverFlags[entry.nServers] = 0;
5517         }
5518     }
5519     entry.flags &= ~RO_EXISTS;
5520     for (i = 0; i < entry.nServers; i++) {
5521         if (entry.serverFlags[i] & ITSROVOL) {
5522             if (!(entry.serverFlags[i] & (RO_DONTUSE | NEW_REPSITE)))
5523                 entry.flags |= RO_EXISTS;
5524         }
5525     }
5526     MapNetworkToHost(&entry, &storeEntry);
5527     code =
5528         VLDB_ReplaceEntry(entry.volumeId[RWVOL], RWVOL, &storeEntry,
5529                           (LOCKREL_OPCODE | LOCKREL_AFSID |
5530                            LOCKREL_TIMESTAMP));
5531     if (code) {
5532         fprintf(STDERR,
5533                 "Warning: volume converted, but vldb update failed with code %d!\n",
5534                 code);
5535     }
5536     vcode = UV_LockRelease(entry.volumeId[RWVOL]);
5537     if (vcode) {
5538         PrintDiagnostics("unlock", vcode);
5539     }
5540     return code;
5541 }
5542
5543 static
5544 Sizes(as)
5545      register struct cmd_syndesc *as;
5546 {
5547     afs_int32 avolid, aserver, apart, voltype, fromdate = 0, code, err, i;
5548     struct nvldbentry entry;
5549     volintSize vol_size;
5550
5551     rx_SetRxDeadTime(60 * 10);
5552     for (i = 0; i < MAXSERVERS; i++) {
5553         struct rx_connection *rxConn = ubik_GetRPCConn(cstruct, i);
5554         if (rxConn == 0)
5555             break;
5556         rx_SetConnDeadTime(rxConn, rx_connDeadTime);
5557         if (rxConn->service)
5558             rxConn->service->connDeadTime = rx_connDeadTime;
5559     }
5560
5561     avolid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
5562     if (avolid == 0) {
5563         if (err)
5564             PrintError("", err);
5565         else
5566             fprintf(STDERR, "vos: can't find volume '%s'\n",
5567                     as->parms[0].items->data);
5568         return ENOENT;
5569     }
5570
5571     if (as->parms[1].items || as->parms[2].items) {
5572         if (!as->parms[1].items || !as->parms[2].items) {
5573             fprintf(STDERR,
5574                     "Must specify both -server and -partition options\n");
5575             return -1;
5576         }
5577         aserver = GetServer(as->parms[2].items->data);
5578         if (aserver == 0) {
5579             fprintf(STDERR, "Invalid server name\n");
5580             return -1;
5581         }
5582         apart = volutil_GetPartitionID(as->parms[1].items->data);
5583         if (apart < 0) {
5584             fprintf(STDERR, "Invalid partition name\n");
5585             return -1;
5586         }
5587     } else {
5588         code = GetVolumeInfo(avolid, &aserver, &apart, &voltype, &entry);
5589         if (code)
5590             return code;
5591     }
5592
5593     fromdate = 0;
5594
5595     if (as->parms[4].items && strcmp(as->parms[4].items->data, "0")) {
5596         code = ktime_DateToInt32(as->parms[4].items->data, &fromdate);
5597         if (code) {
5598             fprintf(STDERR, "vos: failed to parse date '%s' (error=%d))\n",
5599                     as->parms[4].items->data, code);
5600             return code;
5601         }
5602     }
5603
5604     fprintf(STDOUT, "Volume: %s\n", as->parms[0].items->data);
5605
5606     if (as->parms[3].items) {   /* do the dump estimate */
5607 #ifdef AFS_64BIT_ENV
5608         vol_size.dump_size = 0;
5609 #else
5610    FillInt64(vol_size.dump_size,0, 1);
5611 #endif
5612         code = UV_GetSize(avolid, aserver, apart, fromdate, &vol_size);
5613         if (code) {
5614             PrintDiagnostics("size", code);
5615             return code;
5616         }
5617         /* presumably the size info is now gathered in pntr */
5618         /* now we display it */
5619
5620         fprintf(STDOUT, "dump_size: %llu\n", vol_size.dump_size);
5621     }
5622
5623     /* Display info */
5624
5625     return 0;
5626 }
5627
5628 PrintDiagnostics(astring, acode)
5629      char *astring;
5630      afs_int32 acode;
5631 {
5632     if (acode == EACCES) {
5633         fprintf(STDERR,
5634                 "You are not authorized to perform the 'vos %s' command (%d)\n",
5635                 astring, acode);
5636     } else {
5637         fprintf(STDERR, "Error in vos %s command.\n", astring);
5638         PrintError("", acode);
5639     }
5640     return 0;
5641 }
5642
5643
5644 static
5645 MyBeforeProc(as, arock)
5646      struct cmd_syndesc *as;
5647      char *arock;
5648 {
5649     register char *tcell;
5650     register afs_int32 code;
5651     register afs_int32 sauth;
5652
5653     /* Initialize the ubik_client connection */
5654     rx_SetRxDeadTime(90);
5655     cstruct = (struct ubik_client *)0;
5656
5657     sauth = 0;
5658     tcell = NULL;
5659     if (as->parms[12].items)    /* if -cell specified */
5660         tcell = as->parms[12].items->data;
5661     if (as->parms[14].items)    /* -serverauth specified */
5662         sauth = 1;
5663     if (as->parms[16].items)    /* -crypt specified */
5664         vsu_SetCrypt(1);
5665     if ((code =
5666          vsu_ClientInit((as->parms[13].items != 0), confdir, tcell, sauth,
5667                         &cstruct, UV_SetSecurity))) {
5668         fprintf(STDERR, "could not initialize VLDB library (code=%lu) \n",
5669                 (unsigned long)code);
5670         exit(1);
5671     }
5672     rxInitDone = 1;
5673     if (as->parms[15].items)    /* -verbose flag set */
5674         verbose = 1;
5675     else
5676         verbose = 0;
5677     return 0;
5678 }
5679
5680 int
5681 osi_audit()
5682 {
5683 /* this sucks but it works for now.
5684 */
5685     return 0;
5686 }
5687
5688 #include "AFS_component_version_number.c"
5689
5690 main(argc, argv)
5691      int argc;
5692      char **argv;
5693 {
5694     register afs_int32 code;
5695
5696     register struct cmd_syndesc *ts;
5697
5698 #ifdef  AFS_AIX32_ENV
5699     /*
5700      * The following signal action for AIX is necessary so that in case of a 
5701      * crash (i.e. core is generated) we can include the user's data section 
5702      * in the core dump. Unfortunately, by default, only a partial core is
5703      * generated which, in many cases, isn't too useful.
5704      */
5705     struct sigaction nsa;
5706
5707     sigemptyset(&nsa.sa_mask);
5708     nsa.sa_handler = SIG_DFL;
5709     nsa.sa_flags = SA_FULLDUMP;
5710     sigaction(SIGSEGV, &nsa, NULL);
5711 #endif
5712
5713     confdir = AFSDIR_CLIENT_ETC_DIRPATH;
5714
5715     cmd_SetBeforeProc(MyBeforeProc, NULL);
5716
5717     ts = cmd_CreateSyntax("create", CreateVolume, 0, "create a new volume");
5718     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
5719     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "partition name");
5720     cmd_AddParm(ts, "-name", CMD_SINGLE, 0, "volume name");
5721     cmd_AddParm(ts, "-maxquota", CMD_SINGLE, CMD_OPTIONAL,
5722                 "initial quota (KB)");
5723 #ifdef notdef
5724     cmd_AddParm(ts, "-minquota", CMD_SINGLE, CMD_OPTIONAL, "");
5725 #endif
5726     COMMONPARMS;
5727
5728     ts = cmd_CreateSyntax("remove", DeleteVolume, 0, "delete a volume");
5729     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "machine name");
5730     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
5731     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
5732
5733     COMMONPARMS;
5734
5735     ts = cmd_CreateSyntax("move", MoveVolume, 0, "move a volume");
5736     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
5737     cmd_AddParm(ts, "-fromserver", CMD_SINGLE, 0, "machine name on source");
5738     cmd_AddParm(ts, "-frompartition", CMD_SINGLE, 0,
5739                 "partition name on source");
5740     cmd_AddParm(ts, "-toserver", CMD_SINGLE, 0,
5741                 "machine name on destination");
5742     cmd_AddParm(ts, "-topartition", CMD_SINGLE, 0,
5743                 "partition name on destination");
5744     cmd_AddParm(ts, "-live", CMD_FLAG, CMD_OPTIONAL,
5745                 "copy live volume without cloning");
5746     COMMONPARMS;
5747
5748     ts = cmd_CreateSyntax("copy", CopyVolume, 0, "copy a volume");
5749     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID on source");
5750     cmd_AddParm(ts, "-fromserver", CMD_SINGLE, 0, "machine name on source");
5751     cmd_AddParm(ts, "-frompartition", CMD_SINGLE, 0,
5752                 "partition name on source");
5753     cmd_AddParm(ts, "-toname", CMD_SINGLE, 0, "volume name on destination");
5754     cmd_AddParm(ts, "-toserver", CMD_SINGLE, 0,
5755                 "machine name on destination");
5756     cmd_AddParm(ts, "-topartition", CMD_SINGLE, 0,
5757                 "partition name on destination");
5758     cmd_AddParm(ts, "-offline", CMD_FLAG, CMD_OPTIONAL,
5759                 "leave new volume offline");
5760     cmd_AddParm(ts, "-readonly", CMD_FLAG, CMD_OPTIONAL,
5761                 "make new volume read-only");
5762     cmd_AddParm(ts, "-live", CMD_FLAG, CMD_OPTIONAL,
5763                 "copy live volume without cloning");
5764     COMMONPARMS;
5765
5766     ts = cmd_CreateSyntax("shadow", ShadowVolume, 0,
5767                           "make or update a shadow volume");
5768     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID on source");
5769     cmd_AddParm(ts, "-fromserver", CMD_SINGLE, 0, "machine name on source");
5770     cmd_AddParm(ts, "-frompartition", CMD_SINGLE, 0,
5771                 "partition name on source");
5772     cmd_AddParm(ts, "-toserver", CMD_SINGLE, 0,
5773                 "machine name on destination");
5774     cmd_AddParm(ts, "-topartition", CMD_SINGLE, 0,
5775                 "partition name on destination");
5776     cmd_AddParm(ts, "-toname", CMD_SINGLE, CMD_OPTIONAL,
5777                 "volume name on destination");
5778     cmd_AddParm(ts, "-toid", CMD_SINGLE, CMD_OPTIONAL,
5779                 "volume ID on destination");
5780     cmd_AddParm(ts, "-offline", CMD_FLAG, CMD_OPTIONAL,
5781                 "leave shadow volume offline");
5782     cmd_AddParm(ts, "-readonly", CMD_FLAG, CMD_OPTIONAL,
5783                 "make shadow volume read-only");
5784     cmd_AddParm(ts, "-live", CMD_FLAG, CMD_OPTIONAL,
5785                 "copy live volume without cloning");
5786     cmd_AddParm(ts, "-incremental", CMD_FLAG, CMD_OPTIONAL,
5787                 "do incremental update if target exists");
5788     COMMONPARMS;
5789
5790     ts = cmd_CreateSyntax("backup", BackupVolume, 0,
5791                           "make backup of a volume");
5792     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
5793     COMMONPARMS;
5794
5795     ts = cmd_CreateSyntax("clone", CloneVolume, 0,
5796                           "make clone of a volume");
5797     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
5798     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "server");
5799     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition");
5800     cmd_AddParm(ts, "-toname", CMD_SINGLE, CMD_OPTIONAL,
5801                 "volume name on destination");
5802     cmd_AddParm(ts, "-toid", CMD_SINGLE, CMD_OPTIONAL,
5803                 "volume ID on destination");
5804     cmd_AddParm(ts, "-offline", CMD_FLAG, CMD_OPTIONAL,
5805                 "leave clone volume offline");
5806     cmd_AddParm(ts, "-readonly", CMD_FLAG, CMD_OPTIONAL,
5807                 "make clone volume read-only, not readwrite");
5808     COMMONPARMS;
5809
5810     ts = cmd_CreateSyntax("release", ReleaseVolume, 0, "release a volume");
5811     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
5812     cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL,
5813                 "force a complete release");
5814     COMMONPARMS;
5815
5816     ts = cmd_CreateSyntax("dump", DumpVolume, 0, "dump a volume");
5817     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
5818     cmd_AddParm(ts, "-time", CMD_SINGLE, CMD_OPTIONAL, "dump from time");
5819     cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL, "dump file");
5820     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "server");
5821     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition");
5822     cmd_AddParm(ts, "-clone", CMD_FLAG, CMD_OPTIONAL,
5823                 "dump a clone of the volume");
5824     cmd_AddParm(ts, "-omitdirs", CMD_FLAG, CMD_OPTIONAL,
5825                 "omit unchanged directories from an incremental dump");
5826     COMMONPARMS;
5827
5828     ts = cmd_CreateSyntax("restore", RestoreVolume, 0, "restore a volume");
5829     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
5830     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "partition name");
5831     cmd_AddParm(ts, "-name", CMD_SINGLE, 0, "name of volume to be restored");
5832     cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL, "dump file");
5833     cmd_AddParm(ts, "-id", CMD_SINGLE, CMD_OPTIONAL, "volume ID");
5834     cmd_AddParm(ts, "-overwrite", CMD_SINGLE, CMD_OPTIONAL,
5835                 "abort | full | incremental");
5836     cmd_AddParm(ts, "-offline", CMD_FLAG, CMD_OPTIONAL,
5837                 "leave restored volume offline");
5838     cmd_AddParm(ts, "-readonly", CMD_FLAG, CMD_OPTIONAL,
5839                 "make restored volume read-only");
5840     cmd_AddParm(ts, "-creation", CMD_SINGLE, CMD_OPTIONAL,
5841                 "dump | keep | new");
5842     cmd_AddParm(ts, "-lastupdate", CMD_SINGLE, CMD_OPTIONAL,
5843                 "dump | keep | new");
5844     COMMONPARMS;
5845
5846     ts = cmd_CreateSyntax("unlock", LockReleaseCmd, 0,
5847                           "release lock on VLDB entry for a volume");
5848     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
5849     COMMONPARMS;
5850
5851     ts = cmd_CreateSyntax("changeloc", ChangeLocation, 0,
5852                           "change an RW volume's location in the VLDB");
5853     cmd_AddParm(ts, "-server", CMD_SINGLE, 0,
5854                 "machine name for new location");
5855     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0,
5856                 "partition name for new location");
5857     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
5858     COMMONPARMS;
5859
5860     ts = cmd_CreateSyntax("addsite", AddSite, 0, "add a replication site");
5861     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name for new site");
5862     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0,
5863                 "partition name for new site");
5864     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
5865     COMMONPARMS;
5866
5867     ts = cmd_CreateSyntax("remsite", RemoveSite, 0,
5868                           "remove a replication site");
5869     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
5870     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "partition name");
5871     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
5872     COMMONPARMS;
5873
5874     ts = cmd_CreateSyntax("listpart", ListPartitions, 0, "list partitions");
5875     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
5876     COMMONPARMS;
5877
5878     ts = cmd_CreateSyntax("listvol", ListVolumes, 0,
5879                           "list volumes on server (bypass VLDB)");
5880     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
5881     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
5882     cmd_AddParm(ts, "-fast", CMD_FLAG, CMD_OPTIONAL, "minimal listing");
5883     cmd_AddParm(ts, "-long", CMD_FLAG, CMD_OPTIONAL,
5884                 "list all normal volume fields");
5885     cmd_AddParm(ts, "-quiet", CMD_FLAG, CMD_OPTIONAL,
5886                 "generate minimal information");
5887     cmd_AddParm(ts, "-extended", CMD_FLAG, CMD_OPTIONAL,
5888                 "list extended volume fields");
5889 #ifdef FULL_LISTVOL_SWITCH
5890     cmd_AddParm(ts, "-format", CMD_FLAG, CMD_OPTIONAL,
5891                 "machine readable format");
5892 #endif /* FULL_LISTVOL_SWITCH */
5893     COMMONPARMS;
5894
5895     ts = cmd_CreateSyntax("syncvldb", SyncVldb, 0,
5896                           "synchronize VLDB with server");
5897     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "machine name");
5898     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
5899     cmd_AddParm(ts, "-volume", CMD_SINGLE, CMD_OPTIONAL, "volume name or ID");
5900     COMMONPARMS;
5901
5902     ts = cmd_CreateSyntax("syncserv", SyncServer, 0,
5903                           "synchronize server with VLDB");
5904     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
5905     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
5906     COMMONPARMS;
5907
5908     ts = cmd_CreateSyntax("examine", ExamineVolume, 0,
5909                           "everything about the volume");
5910     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
5911     cmd_AddParm(ts, "-extended", CMD_FLAG, CMD_OPTIONAL,
5912                 "list extended volume fields");
5913 #ifdef FULL_LISTVOL_SWITCH
5914     cmd_AddParm(ts, "-format", CMD_FLAG, CMD_OPTIONAL,
5915                 "machine readable format");
5916 #endif /* FULL_LISTVOL_SWITCH */
5917     COMMONPARMS;
5918     cmd_CreateAlias(ts, "volinfo");
5919
5920     ts = cmd_CreateSyntax("setfields", SetFields, 0,
5921                           "change volume info fields");
5922     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
5923     cmd_AddParm(ts, "-maxquota", CMD_SINGLE, CMD_OPTIONAL, "quota (KB)");
5924     cmd_AddParm(ts, "-clearuse", CMD_FLAG, CMD_OPTIONAL, "clear dayUse");
5925     cmd_AddParm(ts, "-clearVolUpCounter", CMD_FLAG, CMD_OPTIONAL, "clear volUpdateCounter");
5926     COMMONPARMS;
5927
5928     ts = cmd_CreateSyntax("offline", volOffline, 0, (char *)CMD_HIDDEN);
5929     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "server name");
5930     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "partition name");
5931     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
5932     cmd_AddParm(ts, "-sleep", CMD_SINGLE, CMD_OPTIONAL, "seconds to sleep");
5933     cmd_AddParm(ts, "-busy", CMD_FLAG, CMD_OPTIONAL, "busy volume");
5934     COMMONPARMS;
5935
5936     ts = cmd_CreateSyntax("online", volOnline, 0, (char *)CMD_HIDDEN);
5937     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "server name");
5938     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "partition name");
5939     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
5940     COMMONPARMS;
5941
5942     ts = cmd_CreateSyntax("zap", VolumeZap, 0,
5943                           "delete the volume, don't bother with VLDB");
5944     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
5945     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "partition name");
5946     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume ID");
5947     cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL,
5948                 "force deletion of bad volumes");
5949     cmd_AddParm(ts, "-backup", CMD_FLAG, CMD_OPTIONAL,
5950                 "also delete backup volume if one is found");
5951     COMMONPARMS;
5952
5953     ts = cmd_CreateSyntax("status", VolserStatus, 0,
5954                           "report on volser status");
5955     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
5956     COMMONPARMS;
5957
5958     ts = cmd_CreateSyntax("rename", RenameVolume, 0, "rename a volume");
5959     cmd_AddParm(ts, "-oldname", CMD_SINGLE, 0, "old volume name ");
5960     cmd_AddParm(ts, "-newname", CMD_SINGLE, 0, "new volume name ");
5961     COMMONPARMS;
5962
5963     ts = cmd_CreateSyntax("listvldb", ListVLDB, 0,
5964                           "list volumes in the VLDB");
5965     cmd_AddParm(ts, "-name", CMD_SINGLE, CMD_OPTIONAL, "volume name or ID");
5966     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "machine name");
5967     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
5968     cmd_AddParm(ts, "-locked", CMD_FLAG, CMD_OPTIONAL, "locked volumes only");
5969     cmd_AddParm(ts, "-quiet", CMD_FLAG, CMD_OPTIONAL,
5970                 "generate minimal information");
5971     cmd_AddParm(ts, "-nosort", CMD_FLAG, CMD_OPTIONAL,
5972                 "do not alphabetically sort the volume names");
5973     COMMONPARMS;
5974
5975     ts = cmd_CreateSyntax("backupsys", BackSys, 0, "en masse backups");
5976     cmd_AddParm(ts, "-prefix", CMD_LIST, CMD_OPTIONAL,
5977                 "common prefix on volume(s)");
5978     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "machine name");
5979     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
5980     cmd_AddParm(ts, "-exclude", CMD_FLAG, CMD_OPTIONAL,
5981                 "exclude common prefix volumes");
5982     cmd_AddParm(ts, "-xprefix", CMD_LIST, CMD_OPTIONAL,
5983                 "negative prefix on volume(s)");
5984     cmd_AddParm(ts, "-dryrun", CMD_FLAG, CMD_OPTIONAL, "no action");
5985     COMMONPARMS;
5986
5987     ts = cmd_CreateSyntax("delentry", DeleteEntry, 0,
5988                           "delete VLDB entry for a volume");
5989     cmd_AddParm(ts, "-id", CMD_LIST, CMD_OPTIONAL, "volume name or ID");
5990     cmd_AddParm(ts, "-prefix", CMD_SINGLE, CMD_OPTIONAL,
5991                 "prefix of the volume whose VLDB entry is to be deleted");
5992     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "machine name");
5993     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
5994     cmd_AddParm(ts, "-noexecute", CMD_FLAG, CMD_OPTIONAL | CMD_HIDE,
5995                 "no execute");
5996     COMMONPARMS;
5997
5998     ts = cmd_CreateSyntax("partinfo", PartitionInfo, 0,
5999                           "list partition information");
6000     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
6001     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
6002     cmd_AddParm(ts, "-summary", CMD_FLAG, CMD_OPTIONAL,
6003                 "print storage summary");
6004     COMMONPARMS;
6005
6006     ts = cmd_CreateSyntax("unlockvldb", UnlockVLDB, 0,
6007                           "unlock all the locked entries in the VLDB");
6008     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "machine name");
6009     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
6010     COMMONPARMS;
6011
6012     ts = cmd_CreateSyntax("lock", LockEntry, 0,
6013                           "lock VLDB entry for a volume");
6014     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6015     COMMONPARMS;
6016
6017     ts = cmd_CreateSyntax("changeaddr", ChangeAddr, 0,
6018                           "change the IP address of a file server");
6019     cmd_AddParm(ts, "-oldaddr", CMD_SINGLE, 0, "original IP address");
6020     cmd_AddParm(ts, "-newaddr", CMD_SINGLE, CMD_OPTIONAL, "new IP address");
6021     cmd_AddParm(ts, "-remove", CMD_FLAG, CMD_OPTIONAL,
6022                 "remove the IP address from the VLDB");
6023     COMMONPARMS;
6024
6025     ts = cmd_CreateSyntax("listaddrs", ListAddrs, 0,
6026                           "list the IP address of all file servers registered in the VLDB");
6027     cmd_AddParm(ts, "-uuid", CMD_SINGLE, CMD_OPTIONAL, "uuid of server");
6028     cmd_AddParm(ts, "-host", CMD_SINGLE, CMD_OPTIONAL, "address of host");
6029     cmd_AddParm(ts, "-noresolve", CMD_FLAG, CMD_OPTIONAL,
6030                 "don't resolve addresses");
6031     cmd_AddParm(ts, "-printuuid", CMD_FLAG, CMD_OPTIONAL,
6032                 "print uuid of hosts");
6033     COMMONPARMS;
6034
6035     ts = cmd_CreateSyntax("convertROtoRW", ConvertRO, 0,
6036                           "convert a RO volume into a RW volume (after loss of old RW volume)");
6037     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
6038     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "partition name");
6039     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6040     cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL, "don't ask");
6041     COMMONPARMS;
6042
6043     ts = cmd_CreateSyntax("size", Sizes, 0,
6044                           "obtain various sizes of the volume.");
6045     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6046     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
6047     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "machine name");
6048     cmd_AddParm(ts, "-dump", CMD_FLAG, CMD_OPTIONAL,
6049                 "Obtain the size of the dump");
6050     cmd_AddParm(ts, "-time", CMD_SINGLE, CMD_OPTIONAL, "dump from time");
6051     COMMONPARMS;
6052
6053     code = cmd_Dispatch(argc, argv);
6054     if (rxInitDone) {
6055         /* Shut down the ubik_client and rx connections */
6056         if (cstruct) {
6057             (void)ubik_ClientDestroy(cstruct);
6058             cstruct = 0;
6059         }
6060         rx_Finalize();
6061     }
6062
6063     exit((code ? -1 : 0));
6064 }