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