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