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