reindent-20030715
[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                         itp->data);
3526                 PrintError("", vcode);
3527                 totalFail++;
3528                 continue;
3529             }
3530             totalBack++;
3531         }
3532         fprintf(STDOUT, "Deleted %d VLDB entries\n", totalBack);
3533         return (totalFail);
3534     }
3535
3536     if (!as->parms[1].items && !as->parms[2].items && !as->parms[3].items) {
3537         fprintf(STDERR, "You must specify an option\n");
3538         exit(-2);
3539     }
3540
3541     /* Zero out search attributes */
3542     memset(&attributes, 0, sizeof(struct VldbListByAttributes));
3543
3544     if (as->parms[1].items) {   /* -prefix */
3545         strncpy(prefix, as->parms[1].items->data, VOLSER_MAXVOLNAME);
3546         seenprefix = 1;
3547         if (!as->parms[2].items && !as->parms[3].items) {       /* a single entry only */
3548             fprintf(STDERR,
3549                     "You must provide -server with the -prefix argument\n");
3550             exit(-2);
3551         }
3552     }
3553
3554     if (as->parms[2].items) {   /* -server */
3555         afs_int32 aserver;
3556         aserver = GetServer(as->parms[2].items->data);
3557         if (aserver == 0) {
3558             fprintf(STDERR, "vos: server '%s' not found in host table\n",
3559                     as->parms[2].items->data);
3560             exit(-1);
3561         }
3562         attributes.server = ntohl(aserver);
3563         attributes.Mask |= VLLIST_SERVER;
3564     }
3565
3566     if (as->parms[3].items) {   /* -partition */
3567         if (!as->parms[2].items) {
3568             fprintf(STDERR,
3569                     "You must provide -server with the -partition argument\n");
3570             exit(-2);
3571         }
3572         apart = volutil_GetPartitionID(as->parms[3].items->data);
3573         if (apart < 0) {
3574             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3575                     as->parms[3].items->data);
3576             exit(-1);
3577         }
3578         attributes.partition = apart;
3579         attributes.Mask |= VLLIST_PARTITION;
3580     }
3581
3582     /* Print status line of what we are doing */
3583     fprintf(STDOUT, "Deleting VLDB entries for ");
3584     if (as->parms[2].items) {
3585         fprintf(STDOUT, "server %s ", as->parms[2].items->data);
3586     }
3587     if (as->parms[3].items) {
3588         char pname[10];
3589         MapPartIdIntoName(apart, pname);
3590         fprintf(STDOUT, "partition %s ", pname);
3591     }
3592     if (seenprefix) {
3593         fprintf(STDOUT, "which are prefixed with %s ", prefix);
3594     }
3595     fprintf(STDOUT, "\n");
3596     fflush(STDOUT);
3597
3598     /* Get all the VLDB entries on a server and/or partition */
3599     memset(&arrayEntries, 0, sizeof(arrayEntries));
3600     vcode = VLDB_ListAttributes(&attributes, &nentries, &arrayEntries);
3601     if (vcode) {
3602         fprintf(STDERR, "Could not access the VLDB for attributes\n");
3603         PrintError("", vcode);
3604         exit(-1);
3605     }
3606
3607     /* Process each entry */
3608     for (j = 0; j < nentries; j++) {
3609         vllist = &arrayEntries.nbulkentries_val[j];
3610         if (seenprefix) {
3611             /* It only deletes the RW volumes */
3612             if (strncmp(vllist->name, prefix, strlen(prefix))) {
3613                 if (verbose) {
3614                     fprintf(STDOUT,
3615                             "Omitting to delete %s due to prefix %s mismatch\n",
3616                             vllist->name, prefix);
3617                 }
3618                 fflush(STDOUT);
3619                 continue;
3620             }
3621         }
3622
3623         if (as->parms[4].items) {       /* -noexecute */
3624             fprintf(STDOUT, "Would have deleted VLDB entry for %s \n",
3625                     vllist->name);
3626             fflush(STDOUT);
3627             continue;
3628         }
3629
3630         /* Only matches the RW volume name */
3631         avolid = vllist->volumeId[RWVOL];
3632         vcode = ubik_Call(VL_DeleteEntry, cstruct, 0, avolid, RWVOL);
3633         if (vcode) {
3634             fprintf(STDOUT, "Could not delete VDLB entry for  %s\n",
3635                     vllist->name);
3636             totalFail++;
3637             PrintError("", vcode);
3638             continue;
3639         } else {
3640             totalBack++;
3641             if (verbose)
3642                 fprintf(STDOUT, "Deleted VLDB entry for %s \n", vllist->name);
3643         }
3644         fflush(STDOUT);
3645     }                           /*for */
3646
3647     fprintf(STDOUT, "----------------------\n");
3648     fprintf(STDOUT,
3649             "Total VLDB entries deleted: %lu; failed to delete: %lu\n",
3650             (unsigned long)totalBack, (unsigned long)totalFail);
3651     if (arrayEntries.nbulkentries_val)
3652         free(arrayEntries.nbulkentries_val);
3653     return 0;
3654 }
3655
3656
3657 static int
3658 CompareVldbEntryByName(p1, p2)
3659      char *p1, *p2;
3660 {
3661     struct nvldbentry *arg1, *arg2;
3662
3663     arg1 = (struct nvldbentry *)p1;
3664     arg2 = (struct nvldbentry *)p2;
3665     return (strcmp(arg1->name, arg2->name));
3666 }
3667
3668 /*
3669 static int CompareVldbEntry(p1,p2)
3670 char *p1,*p2;
3671 {
3672     struct nvldbentry *arg1,*arg2;
3673     int i;
3674     int pos1, pos2;
3675     char comp1[100],comp2[100];
3676     char temp1[20],temp2[20];
3677
3678     arg1 = (struct nvldbentry *)p1;
3679     arg2 = (struct nvldbentry *)p2;
3680     pos1 = -1;
3681     pos2 = -1;
3682
3683     for(i = 0; i < arg1->nServers; i++)
3684         if(arg1->serverFlags[i] & ITSRWVOL) pos1 = i;
3685     for(i = 0; i < arg2->nServers; i++)
3686         if(arg2->serverFlags[i] & ITSRWVOL) pos2 = i;
3687     if(pos1 == -1 || pos2 == -1){
3688         pos1 = 0;
3689         pos2 = 0;
3690     }
3691     sprintf(comp1,"%10u",arg1->serverNumber[pos1]);
3692     sprintf(comp2,"%10u",arg2->serverNumber[pos2]);
3693     sprintf(temp1,"%10u",arg1->serverPartition[pos1]);
3694     sprintf(temp2,"%10u",arg2->serverPartition[pos2]);
3695     strcat(comp1,temp1);
3696     strcat(comp2,temp2);
3697     strcat(comp1,arg1->name);
3698     strcat(comp1,arg2->name);
3699     return(strcmp(comp1,comp2));
3700
3701 }
3702
3703 */
3704 static
3705 ListVLDB(as)
3706      struct cmd_syndesc *as;
3707 {
3708     afs_int32 apart;
3709     afs_int32 aserver, code;
3710     afs_int32 vcode;
3711     struct VldbListByAttributes attributes;
3712     nbulkentries arrayEntries;
3713     struct nvldbentry *vllist, *tarray = 0, *ttarray;
3714     afs_int32 centries, nentries = 0, tarraysize, parraysize;
3715     int j;
3716     char pname[10];
3717     int quiet, sort, lock;
3718     afs_int32 thisindex, nextindex;
3719
3720     aserver = 0;
3721     apart = 0;
3722
3723     attributes.Mask = 0;
3724     lock = (as->parms[3].items ? 1 : 0);        /* -lock   flag */
3725     quiet = (as->parms[4].items ? 1 : 0);       /* -quit   flag */
3726     sort = (as->parms[5].items ? 0 : 1);        /* -nosort flag */
3727
3728     /* If the volume name is given, Use VolumeInfoCmd to look it up
3729      * and not ListAttributes.
3730      */
3731     if (as->parms[0].items) {
3732         if (lock) {
3733             fprintf(STDERR,
3734                     "vos: illegal use of '-locked' switch, need to specify server and/or partition\n");
3735             exit(1);
3736         }
3737         code = VolumeInfoCmd(as->parms[0].items->data);
3738         if (code) {
3739             PrintError("", code);
3740             exit(1);
3741         }
3742         return 0;
3743     }
3744
3745     /* Server specified */
3746     if (as->parms[1].items) {
3747         aserver = GetServer(as->parms[1].items->data);
3748         if (aserver == 0) {
3749             fprintf(STDERR, "vos: server '%s' not found in host table\n",
3750                     as->parms[1].items->data);
3751             exit(1);
3752         }
3753         attributes.server = ntohl(aserver);
3754         attributes.Mask |= VLLIST_SERVER;
3755     }
3756
3757     /* Partition specified */
3758     if (as->parms[2].items) {
3759         apart = volutil_GetPartitionID(as->parms[2].items->data);
3760         if (apart < 0) {
3761             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3762                     as->parms[2].items->data);
3763             exit(1);
3764         }
3765         attributes.partition = apart;
3766         attributes.Mask |= VLLIST_PARTITION;
3767     }
3768
3769     if (lock) {
3770         attributes.Mask |= VLLIST_FLAG;
3771         attributes.flag = VLOP_ALLOPERS;
3772     }
3773
3774     /* Print header information */
3775     if (!quiet) {
3776         MapPartIdIntoName(apart, pname);
3777         fprintf(STDOUT, "VLDB entries for %s %s%s%s %s\n",
3778                 (as->parms[1].items ? "server" : "all"),
3779                 (as->parms[1].items ? as->parms[1].items->data : "servers"),
3780                 (as->parms[2].items ? " partition " : ""),
3781                 (as->parms[2].items ? pname : ""),
3782                 (lock ? "which are locked:" : ""));
3783     }
3784
3785     for (thisindex = 0; (thisindex != -1); thisindex = nextindex) {
3786         memset(&arrayEntries, 0, sizeof(arrayEntries));
3787         centries = 0;
3788         nextindex = -1;
3789
3790         vcode =
3791             VLDB_ListAttributesN2(&attributes, 0, thisindex, &centries,
3792                                   &arrayEntries, &nextindex);
3793         if (vcode == RXGEN_OPCODE) {
3794             /* Vlserver not running with ListAttributesN2. Fall back */
3795             vcode =
3796                 VLDB_ListAttributes(&attributes, &centries, &arrayEntries);
3797             nextindex = -1;
3798         }
3799         if (vcode) {
3800             fprintf(STDERR, "Could not access the VLDB for attributes\n");
3801             PrintError("", vcode);
3802             exit(1);
3803         }
3804         nentries += centries;
3805
3806         /* We don't sort, so just print the entries now */
3807         if (!sort) {
3808             for (j = 0; j < centries; j++) {    /* process each entry */
3809                 vllist = &arrayEntries.nbulkentries_val[j];
3810                 MapHostToNetwork(vllist);
3811                 EnumerateEntry(vllist);
3812
3813                 if (vllist->flags & VLOP_ALLOPERS)
3814                     fprintf(STDOUT, "    Volume is currently LOCKED  \n");
3815             }
3816         }
3817
3818         /* So we sort. First we must collect all the entries and keep
3819          * them in memory.
3820          */
3821         else if (centries > 0) {
3822             if (!tarray) {
3823                 /* steal away the first bulk entries array */
3824                 tarray = (struct nvldbentry *)arrayEntries.nbulkentries_val;
3825                 tarraysize = centries * sizeof(struct nvldbentry);
3826                 arrayEntries.nbulkentries_val = 0;
3827             } else {
3828                 /* Grow the tarray to keep the extra entries */
3829                 parraysize = (centries * sizeof(struct nvldbentry));
3830                 ttarray =
3831                     (struct nvldbentry *)realloc(tarray,
3832                                                  tarraysize + parraysize);
3833                 if (!ttarray) {
3834                     fprintf(STDERR,
3835                             "Could not allocate enough space for  the VLDB entries\n");
3836                     goto bypass;
3837                 }
3838                 tarray = ttarray;
3839
3840                 /* Copy them in */
3841                 memcpy(((char *)tarray) + tarraysize,
3842                        (char *)arrayEntries.nbulkentries_val, parraysize);
3843                 tarraysize += parraysize;
3844             }
3845         }
3846
3847         /* Free the bulk array */
3848         if (arrayEntries.nbulkentries_val) {
3849             free(arrayEntries.nbulkentries_val);
3850             arrayEntries.nbulkentries_val = 0;
3851         }
3852     }
3853
3854     /* Here is where we now sort all the entries and print them */
3855     if (sort && (nentries > 0)) {
3856         qsort((char *)tarray, nentries, sizeof(struct nvldbentry),
3857               CompareVldbEntryByName);
3858         for (vllist = tarray, j = 0; j < nentries; j++, vllist++) {
3859             MapHostToNetwork(vllist);
3860             EnumerateEntry(vllist);
3861
3862             if (vllist->flags & VLOP_ALLOPERS)
3863                 fprintf(STDOUT, "    Volume is currently LOCKED  \n");
3864         }
3865     }
3866
3867   bypass:
3868     if (!quiet)
3869         fprintf(STDOUT, "\nTotal entries: %lu\n", (unsigned long)nentries);
3870     if (tarray)
3871         free(tarray);
3872     return 0;
3873 }
3874
3875 static
3876 BackSys(as)
3877      register struct cmd_syndesc *as;
3878 {
3879     afs_int32 apart = 0, avolid;
3880     afs_int32 aserver = 0, code, aserver1, apart1;
3881     afs_int32 vcode;
3882     struct VldbListByAttributes attributes;
3883     nbulkentries arrayEntries;
3884     register struct nvldbentry *vllist;
3885     afs_int32 nentries;
3886     int j;
3887     char pname[10];
3888     int seenprefix, seenxprefix, exclude, ex, exp, noaction;
3889     afs_int32 totalBack = 0;
3890     afs_int32 totalFail = 0;
3891     int previdx = -1, error, same;
3892     int comp = 0;
3893     char compstr[50];
3894     struct cmd_item *ti;
3895     char *ccode;
3896     int match;
3897
3898     memset(&attributes, 0, sizeof(struct VldbListByAttributes));
3899     attributes.Mask = 0;
3900
3901     seenprefix = (as->parms[0].items ? 1 : 0);
3902     exclude = (as->parms[3].items ? 1 : 0);
3903     seenxprefix = (as->parms[4].items ? 1 : 0);
3904     noaction = (as->parms[5].items ? 1 : 0);
3905
3906     if (as->parms[1].items) {   /* -server */
3907         aserver = GetServer(as->parms[1].items->data);
3908         if (aserver == 0) {
3909             fprintf(STDERR, "vos: server '%s' not found in host table\n",
3910                     as->parms[1].items->data);
3911             exit(1);
3912         }
3913         attributes.server = ntohl(aserver);
3914         attributes.Mask |= VLLIST_SERVER;
3915     }
3916
3917     if (as->parms[2].items) {   /* -partition */
3918         apart = volutil_GetPartitionID(as->parms[2].items->data);
3919         if (apart < 0) {
3920             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3921                     as->parms[2].items->data);
3922             exit(1);
3923         }
3924         attributes.partition = apart;
3925         attributes.Mask |= VLLIST_PARTITION;
3926     }
3927
3928     /* Check to make sure the prefix and xprefix expressions compile ok */
3929     if (seenprefix) {
3930         for (ti = as->parms[0].items; ti; ti = ti->next) {
3931             if (strncmp(ti->data, "^", 1) == 0) {
3932                 ccode = (char *)re_comp(ti->data);
3933                 if (ccode) {
3934                     fprintf(STDERR,
3935                             "Unrecognizable -prefix regular expression: '%s': %s\n",
3936                             ti->data, ccode);
3937                     exit(1);
3938                 }
3939             }
3940         }
3941     }
3942     if (seenxprefix) {
3943         for (ti = as->parms[4].items; ti; ti = ti->next) {
3944             if (strncmp(ti->data, "^", 1) == 0) {
3945                 ccode = (char *)re_comp(ti->data);
3946                 if (ccode) {
3947                     fprintf(STDERR,
3948                             "Unrecognizable -xprefix regular expression: '%s': %s\n",
3949                             ti->data, ccode);
3950                     exit(1);
3951                 }
3952             }
3953         }
3954     }
3955
3956     memset(&arrayEntries, 0, sizeof(arrayEntries));     /* initialize to hint the stub to alloc space */
3957     vcode = VLDB_ListAttributes(&attributes, &nentries, &arrayEntries);
3958     if (vcode) {
3959         fprintf(STDERR, "Could not access the VLDB for attributes\n");
3960         PrintError("", vcode);
3961         exit(1);
3962     }
3963
3964     if (as->parms[1].items || as->parms[2].items || verbose) {
3965         fprintf(STDOUT, "%s up volumes",
3966                 (noaction ? "Would have backed" : "Backing"));
3967
3968         if (as->parms[1].items) {
3969             fprintf(STDOUT, " on server %s", as->parms[1].items->data);
3970         } else if (as->parms[2].items) {
3971             fprintf(STDOUT, " for all servers");
3972         }
3973
3974         if (as->parms[2].items) {
3975             MapPartIdIntoName(apart, pname);
3976             fprintf(STDOUT, " partition %s", pname);
3977         }
3978
3979         if (seenprefix || (!seenprefix && seenxprefix)) {
3980             ti = (seenprefix ? as->parms[0].items : as->parms[4].items);
3981             ex = (seenprefix ? exclude : !exclude);
3982             exp = (strncmp(ti->data, "^", 1) == 0);
3983             fprintf(STDOUT, " which %smatch %s '%s'", (ex ? "do not " : ""),
3984                     (exp ? "expression" : "prefix"), ti->data);
3985             for (ti = ti->next; ti; ti = ti->next) {
3986                 exp = (strncmp(ti->data, "^", 1) == 0);
3987                 printf(" %sor %s '%s'", (ex ? "n" : ""),
3988                        (exp ? "expression" : "prefix"), ti->data);
3989             }
3990         }
3991
3992         if (seenprefix && seenxprefix) {
3993             ti = as->parms[4].items;
3994             exp = (strncmp(ti->data, "^", 1) == 0);
3995             fprintf(STDOUT, " %swhich match %s '%s'",
3996                     (exclude ? "adding those " : "removing those "),
3997                     (exp ? "expression" : "prefix"), ti->data);
3998             for (ti = ti->next; ti; ti = ti->next) {
3999                 exp = (strncmp(ti->data, "^", 1) == 0);
4000                 printf(" or %s '%s'", (exp ? "expression" : "prefix"),
4001                        ti->data);
4002             }
4003         }
4004         fprintf(STDOUT, " .. ");
4005         if (verbose)
4006             fprintf(STDOUT, "\n");
4007         fflush(STDOUT);
4008     }
4009
4010     for (j = 0; j < nentries; j++) {    /* process each vldb entry */
4011         vllist = &arrayEntries.nbulkentries_val[j];
4012
4013         if (seenprefix) {
4014             for (ti = as->parms[0].items; ti; ti = ti->next) {
4015                 if (strncmp(ti->data, "^", 1) == 0) {
4016                     ccode = (char *)re_comp(ti->data);
4017                     if (ccode) {
4018                         fprintf(STDERR,
4019                                 "Error in -prefix regular expression: '%s': %s\n",
4020                                 ti->data, ccode);
4021                         exit(1);
4022                     }
4023                     match = (re_exec(vllist->name) == 1);
4024                 } else {
4025                     match =
4026                         (strncmp(vllist->name, ti->data, strlen(ti->data)) ==
4027                          0);
4028                 }
4029                 if (match)
4030                     break;
4031             }
4032         } else {
4033             match = 1;
4034         }
4035
4036         /* Without the -exclude flag: If it matches the prefix, then
4037          *    check if we want to exclude any from xprefix.
4038          * With the -exclude flag: If it matches the prefix, then
4039          *    check if we want to add any from xprefix.
4040          */
4041         if (match && seenxprefix) {
4042             for (ti = as->parms[4].items; ti; ti = ti->next) {
4043                 if (strncmp(ti->data, "^", 1) == 0) {
4044                     ccode = (char *)re_comp(ti->data);
4045                     if (ccode) {
4046                         fprintf(STDERR,
4047                                 "Error in -xprefix regular expression: '%s': %s\n",
4048                                 ti->data, ccode);
4049                         exit(1);
4050                     }
4051                     if (re_exec(vllist->name) == 1) {
4052                         match = 0;
4053                         break;
4054                     }
4055                 } else {
4056                     if (strncmp(vllist->name, ti->data, strlen(ti->data)) ==
4057                         0) {
4058                         match = 0;
4059                         break;
4060                     }
4061                 }
4062             }
4063         }
4064
4065         if (exclude)
4066             match = !match;     /* -exclude will reverse the match */
4067         if (!match)
4068             continue;           /* Skip if no match */
4069
4070         /* Print list of volumes to backup */
4071         if (noaction) {
4072             fprintf(STDOUT, "     %s\n", vllist->name);
4073             continue;
4074         }
4075
4076         if (!(vllist->flags & RW_EXISTS)) {
4077             if (verbose) {
4078                 fprintf(STDOUT,
4079                         "Omitting to backup %s since RW volume does not exist \n",
4080                         vllist->name);
4081                 fprintf(STDOUT, "\n");
4082             }
4083             fflush(STDOUT);
4084             continue;
4085         }
4086
4087         avolid = vllist->volumeId[RWVOL];
4088         MapHostToNetwork(vllist);
4089         GetServerAndPart(vllist, RWVOL, &aserver1, &apart1, &previdx);
4090         if (aserver1 == -1 || apart1 == -1) {
4091             fprintf(STDOUT, "could not backup %s, invalid VLDB entry\n",
4092                     vllist->name);
4093             totalFail++;
4094             continue;
4095         }
4096         if (aserver) {
4097             same = VLDB_IsSameAddrs(aserver, aserver1, &error);
4098             if (error) {
4099                 fprintf(STDERR,
4100                         "Failed to get info about server's %d address(es) from vlserver (err=%d); aborting call!\n",
4101                         aserver, error);
4102                 totalFail++;
4103                 continue;
4104             }
4105         }
4106         if ((aserver && !same) || (apart && (apart != apart1))) {
4107             if (verbose) {
4108                 fprintf(STDOUT,
4109                         "Omitting to backup %s since the RW is in a different location\n",
4110                         vllist->name);
4111             }
4112             continue;
4113         }
4114         if (verbose) {
4115             time_t now = time(0);
4116             fprintf(STDOUT, "Creating backup volume for %s on %s",
4117                     vllist->name, ctime(&now));
4118             fflush(STDOUT);
4119         }
4120
4121         code = UV_BackupVolume(aserver1, apart1, avolid);
4122         if (code) {
4123             fprintf(STDOUT, "Could not backup %s\n", vllist->name);
4124             totalFail++;
4125         } else {
4126             totalBack++;
4127         }
4128         if (verbose)
4129             fprintf(STDOUT, "\n");
4130         fflush(STDOUT);
4131     }                           /* process each vldb entry */
4132     fprintf(STDOUT, "done\n");
4133     fprintf(STDOUT, "Total volumes backed up: %lu; failed to backup: %lu\n",
4134             (unsigned long)totalBack, (unsigned long)totalFail);
4135     fflush(STDOUT);
4136     if (arrayEntries.nbulkentries_val)
4137         free(arrayEntries.nbulkentries_val);
4138     return 0;
4139 }
4140
4141 static
4142 UnlockVLDB(as)
4143      register struct cmd_syndesc *as;
4144 {
4145     afs_int32 apart;
4146     afs_int32 aserver, code;
4147     afs_int32 vcode;
4148     struct VldbListByAttributes attributes;
4149     nbulkentries arrayEntries;
4150     register struct nvldbentry *vllist;
4151     afs_int32 nentries;
4152     int j;
4153     afs_int32 volid;
4154     afs_int32 totalE;
4155     char pname[10];
4156
4157     apart = -1;
4158     totalE = 0;
4159     attributes.Mask = 0;
4160
4161     if (as->parms[0].items) {   /* server specified */
4162         aserver = GetServer(as->parms[0].items->data);
4163         if (aserver == 0) {
4164             fprintf(STDERR, "vos: server '%s' not found in host table\n",
4165                     as->parms[0].items->data);
4166             exit(1);
4167         }
4168         attributes.server = ntohl(aserver);
4169         attributes.Mask |= VLLIST_SERVER;
4170     }
4171     if (as->parms[1].items) {   /* partition specified */
4172         apart = volutil_GetPartitionID(as->parms[1].items->data);
4173         if (apart < 0) {
4174             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
4175                     as->parms[1].items->data);
4176             exit(1);
4177         }
4178         if (!IsPartValid(apart, aserver, &code)) {      /*check for validity of the partition */
4179             if (code)
4180                 PrintError("", code);
4181             else
4182                 fprintf(STDERR,
4183                         "vos : partition %s does not exist on the server\n",
4184                         as->parms[1].items->data);
4185             exit(1);
4186         }
4187         attributes.partition = apart;
4188         attributes.Mask |= VLLIST_PARTITION;
4189     }
4190     attributes.flag = VLOP_ALLOPERS;
4191     attributes.Mask |= VLLIST_FLAG;
4192     memset(&arrayEntries, 0, sizeof(arrayEntries));     /*initialize to hint the stub  to alloc space */
4193     vcode = VLDB_ListAttributes(&attributes, &nentries, &arrayEntries);
4194     if (vcode) {
4195         fprintf(STDERR, "Could not access the VLDB for attributes\n");
4196         PrintError("", vcode);
4197         exit(1);
4198     }
4199     for (j = 0; j < nentries; j++) {    /* process each entry */
4200         vllist = &arrayEntries.nbulkentries_val[j];
4201         volid = vllist->volumeId[RWVOL];
4202         vcode =
4203             ubik_Call(VL_ReleaseLock, cstruct, 0, volid, -1,
4204                       LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP);
4205         if (vcode) {
4206             fprintf(STDERR, "Could not unlock entry for volume %s\n",
4207                     vllist->name);
4208             PrintError("", vcode);
4209             totalE++;
4210         }
4211
4212     }
4213     MapPartIdIntoName(apart, pname);
4214     if (totalE)
4215         fprintf(STDOUT,
4216                 "Could not lock %lu VLDB entries of %lu locked entries\n",
4217                 (unsigned long)totalE, (unsigned long)nentries);
4218     else {
4219         if (as->parms[0].items) {
4220             fprintf(STDOUT,
4221                     "Unlocked all the VLDB entries for volumes on server %s ",
4222                     as->parms[0].items->data);
4223             if (as->parms[1].items) {
4224                 MapPartIdIntoName(apart, pname);
4225                 fprintf(STDOUT, "partition %s\n", pname);
4226             } else
4227                 fprintf(STDOUT, "\n");
4228
4229         } else if (as->parms[1].items) {
4230             MapPartIdIntoName(apart, pname);
4231             fprintf(STDOUT,
4232                     "Unlocked all the VLDB entries for volumes on partition %s on all servers\n",
4233                     pname);
4234         }
4235     }
4236
4237     if (arrayEntries.nbulkentries_val)
4238         free(arrayEntries.nbulkentries_val);
4239     return 0;
4240 }
4241
4242 static
4243 PartitionInfo(as)
4244      register struct cmd_syndesc *as;
4245 {
4246     afs_int32 apart;
4247     afs_int32 aserver, code;
4248     char pname[10];
4249     struct diskPartition partition;
4250     struct partList dummyPartList;
4251     int i, cnt;
4252
4253     apart = -1;
4254     aserver = GetServer(as->parms[0].items->data);
4255     if (aserver == 0) {
4256         fprintf(STDERR, "vos: server '%s' not found in host table\n",
4257                 as->parms[0].items->data);
4258         exit(1);
4259     }
4260     if (as->parms[1].items) {
4261         apart = volutil_GetPartitionID(as->parms[1].items->data);
4262         if (apart < 0) {
4263             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
4264                     as->parms[1].items->data);
4265             exit(1);
4266         }
4267         dummyPartList.partId[0] = apart;
4268         dummyPartList.partFlags[0] = PARTVALID;
4269         cnt = 1;
4270     }
4271     if (apart != -1) {
4272         if (!IsPartValid(apart, aserver, &code)) {      /*check for validity of the partition */
4273             if (code)
4274                 PrintError("", code);
4275             else
4276                 fprintf(STDERR,
4277                         "vos : partition %s does not exist on the server\n",
4278                         as->parms[1].items->data);
4279             exit(1);
4280         }
4281     } else {
4282         code = UV_ListPartitions(aserver, &dummyPartList, &cnt);
4283         if (code) {
4284             PrintDiagnostics("listpart", code);
4285             exit(1);
4286         }
4287     }
4288     for (i = 0; i < cnt; i++) {
4289         if (dummyPartList.partFlags[i] & PARTVALID) {
4290             MapPartIdIntoName(dummyPartList.partId[i], pname);
4291             code = UV_PartitionInfo(aserver, pname, &partition);
4292             if (code) {
4293                 fprintf(STDERR, "Could not get information on partition %s\n",
4294                         pname);
4295                 PrintError("", code);
4296                 exit(1);
4297             }
4298             fprintf(STDOUT,
4299                     "Free space on partition %s: %d K blocks out of total %d\n",
4300                     pname, partition.free, partition.minFree);
4301         }
4302     }
4303     return 0;
4304 }
4305
4306 static
4307 ChangeAddr(as)
4308      register struct cmd_syndesc *as;
4309
4310 {
4311     afs_int32 ip1, ip2, vcode;
4312     int remove = 0;
4313
4314     ip1 = GetServer(as->parms[0].items->data);
4315     if (!ip1) {
4316         fprintf(STDERR, "vos: invalid host address\n");
4317         return (EINVAL);
4318     }
4319
4320     if ((as->parms[1].items && as->parms[2].items)
4321         || (!as->parms[1].items && !as->parms[2].items)) {
4322         fprintf(STDERR,
4323                 "vos: Must specify either '-newaddr <addr>' or '-remove' flag\n");
4324         return (EINVAL);
4325     }
4326
4327     if (as->parms[1].items) {
4328         ip2 = GetServer(as->parms[1].items->data);
4329         if (!ip2) {
4330             fprintf(STDERR, "vos: invalid host address\n");
4331             return (EINVAL);
4332         }
4333     } else {
4334         /* Play a trick here. If we are removing an address, ip1 will be -1
4335          * and ip2 will be the original address. This switch prevents an 
4336          * older revision vlserver from removing the IP address.
4337          */
4338         remove = 1;
4339         ip2 = ip1;
4340         ip1 = 0xffffffff;
4341     }
4342
4343     vcode = ubik_Call_New(VL_ChangeAddr, cstruct, 0, ntohl(ip1), ntohl(ip2));
4344     if (vcode) {
4345         if (remove) {
4346             fprintf(STDERR, "Could not remove server %s from the VLDB\n",
4347                     as->parms[0].items->data);
4348             if (vcode == VL_NOENT) {
4349                 fprintf(STDERR,
4350                         "vlserver does not support the remove flag or ");
4351             }
4352         } else {
4353             fprintf(STDERR, "Could not change server %s to server %s\n",
4354                     as->parms[0].items->data, as->parms[1].items->data);
4355         }
4356         PrintError("", vcode);
4357         return (vcode);
4358     }
4359
4360     if (remove) {
4361         fprintf(STDOUT, "Removed server %s from the VLDB\n",
4362                 as->parms[0].items->data);
4363     } else {
4364         fprintf(STDOUT, "Changed server %s to server %s\n",
4365                 as->parms[0].items->data, as->parms[1].items->data);
4366     }
4367     return 0;
4368 }
4369
4370 static void
4371 print_addrs(const bulkaddrs * addrs, const afsUUID * m_uuid, int nentries,
4372             int print, int noresolve)
4373 {
4374     afs_int32 vcode;
4375     afs_int32 i, j;
4376     struct VLCallBack unused;
4377     afs_int32 *addrp;
4378     bulkaddrs m_addrs;
4379     ListAddrByAttributes m_attrs;
4380     afs_int32 m_unique, m_nentries, *m_addrp;
4381     afs_int32 base, index;
4382     char buf[1024];
4383
4384     if (print) {
4385         afsUUID_to_string(m_uuid, buf, sizeof(buf));
4386         printf("UUID: %s\n", buf);
4387     }
4388
4389     /* print out the list of all the server */
4390     addrp = (afs_int32 *) addrs->bulkaddrs_val;
4391     for (i = 0; i < nentries; i++, addrp++) {
4392         /* If it is a multihomed address, then we will need to 
4393          * get the addresses for this multihomed server from
4394          * the vlserver and print them.
4395          */
4396         if (((*addrp & 0xff000000) == 0xff000000) && ((*addrp) & 0xffff)) {
4397             /* Get the list of multihomed fileservers */
4398             base = (*addrp >> 16) & 0xff;
4399             index = (*addrp) & 0xffff;
4400
4401             if ((base >= 0) && (base <= VL_MAX_ADDREXTBLKS) && (index >= 1)
4402                 && (index <= VL_MHSRV_PERBLK)) {
4403                 m_attrs.Mask = VLADDR_INDEX;
4404                 m_attrs.index = (base * VL_MHSRV_PERBLK) + index;
4405                 m_nentries = 0;
4406                 m_addrs.bulkaddrs_val = 0;
4407                 m_addrs.bulkaddrs_len = 0;
4408                 vcode =
4409                     ubik_Call(VL_GetAddrsU, cstruct, 0, &m_attrs, &m_uuid,
4410                               &m_unique, &m_nentries, &m_addrs);
4411                 if (vcode) {
4412                     fprintf(STDERR,
4413                             "vos: could not list the multi-homed server addresses\n");
4414                     PrintError("", vcode);
4415                 }
4416
4417                 /* Print the list */
4418                 m_addrp = (afs_int32 *) m_addrs.bulkaddrs_val;
4419                 for (j = 0; j < m_nentries; j++, m_addrp++) {
4420                     *m_addrp = htonl(*m_addrp);
4421                     if (noresolve) {
4422                         char hoststr[16];
4423                         printf("%s ", afs_inet_ntoa_r(*m_addrp, hoststr));
4424                     } else {
4425                         printf("%s ", hostutil_GetNameByINet(*m_addrp));
4426                     }
4427                 }
4428                 if (j == 0) {
4429                     printf("<unknown>\n");
4430                 } else {
4431                     printf("\n");
4432                 }
4433
4434                 continue;
4435             }
4436         }
4437
4438         /* Otherwise, it is a non-multihomed entry and contains
4439          * the IP address of the server - print it.
4440          */
4441         *addrp = htonl(*addrp);
4442         if (noresolve) {
4443             char hoststr[16];
4444             printf("%s\n", afs_inet_ntoa_r(*addrp, hoststr));
4445         } else {
4446             printf("%s\n", hostutil_GetNameByINet(*addrp));
4447         }
4448     }
4449
4450     if (print) {
4451         printf("\n");
4452     }
4453     return;
4454 }
4455
4456 static
4457 ListAddrs(as)
4458      register struct cmd_syndesc *as;
4459 {
4460     afs_int32 vcode;
4461     afs_int32 i, j, noresolve = 0, printuuid = 0;
4462     struct VLCallBack unused;
4463     afs_int32 nentries, *addrp;
4464     bulkaddrs addrs, m_addrs;
4465     ListAddrByAttributes m_attrs;
4466     afsUUID m_uuid, askuuid;
4467     afs_int32 m_unique, m_nentries, *m_addrp;
4468     afs_int32 base, index;
4469
4470     memset(&m_attrs, 0, sizeof(struct ListAddrByAttributes));
4471     m_attrs.Mask = VLADDR_INDEX;
4472
4473     memset(&m_addrs, 0, sizeof(bulkaddrs));
4474     memset(&askuuid, 0, sizeof(afsUUID));
4475     if (as->parms[0].items) {
4476         /* -uuid */
4477         afsUUID_from_string(as->parms[0].items->data, &askuuid);
4478         m_attrs.Mask = VLADDR_UUID;
4479         m_attrs.uuid = askuuid;
4480     }
4481     if (as->parms[1].items) {
4482         /* -host */
4483         struct hostent *he;
4484         afs_int32 saddr;
4485         he = hostutil_GetHostByName((char *)as->parms[1].items->data);
4486         if (he == NULL) {
4487             fprintf(stderr, "Can't get host info for '%s'\n",
4488                     as->parms[1].items->data);
4489             exit(-1);
4490         }
4491         memcpy(&saddr, he->h_addr, 4);
4492         m_attrs.Mask = VLADDR_IPADDR;
4493         m_attrs.ipaddr = ntohl(saddr);
4494     }
4495     if (as->parms[2].items) {
4496         noresolve = 1;
4497     }
4498     if (as->parms[3].items) {
4499         printuuid = 1;
4500     }
4501
4502     m_addrs.bulkaddrs_val = 0;
4503     m_addrs.bulkaddrs_len = 0;
4504
4505     vcode =
4506         ubik_Call_New(VL_GetAddrs, cstruct, 0, 0, 0, &m_unique, &nentries,
4507                       &m_addrs);
4508     if (vcode) {
4509         fprintf(STDERR, "vos: could not list the server addresses\n");
4510         PrintError("", vcode);
4511         return (vcode);
4512     }
4513
4514     m_nentries = 0;
4515     m_addrs.bulkaddrs_val = 0;
4516     m_addrs.bulkaddrs_len = 0;
4517     i = 1;
4518     while (1) {
4519         m_attrs.index = i;
4520
4521         vcode =
4522             ubik_Call_New(VL_GetAddrsU, cstruct, 0, &m_attrs, &m_uuid,
4523                           &m_unique, &m_nentries, &m_addrs);
4524         if (vcode == VL_NOENT) {
4525             i++;
4526             nentries++;
4527             continue;
4528         }
4529
4530         if (vcode == VL_INDEXERANGE) {
4531             break;
4532         }
4533
4534         if (vcode) {
4535             fprintf(STDERR, "vos: could not list the server addresses\n");
4536             PrintError("", vcode);
4537             return (vcode);
4538         }
4539
4540         print_addrs(&m_addrs, &m_uuid, m_nentries, printuuid, noresolve);
4541         i++;
4542
4543         if ((as->parms[1].items) || (as->parms[0].items) || (i > nentries))
4544             break;
4545     }
4546
4547     return 0;
4548 }
4549
4550 static
4551 LockEntry(as)
4552      register struct cmd_syndesc *as;
4553
4554 {
4555     afs_int32 avolid, vcode, err;
4556
4557     avolid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
4558     if (avolid == 0) {
4559         if (err)
4560             PrintError("", err);
4561         else
4562             fprintf(STDERR, "vos: can't find volume '%s'\n",
4563                     as->parms[0].items->data);
4564         exit(1);
4565     }
4566     vcode = ubik_Call(VL_SetLock, cstruct, 0, avolid, -1, VLOP_DELETE);
4567     if (vcode) {
4568         fprintf(STDERR, "Could not lock VLDB entry for volume %s\n",
4569                 as->parms[0].items->data);
4570         PrintError("", vcode);
4571         exit(1);
4572     }
4573     fprintf(STDOUT, "Locked VLDB entry for volume %s\n",
4574             as->parms[0].items->data);
4575     return 0;
4576 }
4577
4578 static
4579 ConvertRO(as)
4580      register struct cmd_syndesc *as;
4581
4582 {
4583     afs_int32 partition = -1;
4584     afs_int32 server, volid, code, i, same;
4585     struct nvldbentry entry, storeEntry;
4586     afs_int32 vcode;
4587     afs_int32 rwindex;
4588     afs_int32 rwserver = 0;
4589     afs_int32 rwpartition;
4590     afs_int32 roindex;
4591     afs_int32 roserver = 0;
4592     afs_int32 ropartition;
4593     int force = 0;
4594     struct rx_connection *aconn;
4595     char c, dc;
4596
4597     server = GetServer(as->parms[0].items->data);
4598     if (!server) {
4599         fprintf(STDERR, "vos: host '%s' not found in host table\n",
4600                 as->parms[0].items->data);
4601         return ENOENT;
4602     }
4603     partition = volutil_GetPartitionID(as->parms[1].items->data);
4604     if (partition < 0) {
4605         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
4606                 as->parms[1].items->data);
4607         return ENOENT;
4608     }
4609     if (!IsPartValid(partition, server, &code)) {
4610         if (code)
4611             PrintError("", code);
4612         else
4613             fprintf(STDERR,
4614                     "vos : partition %s does not exist on the server\n",
4615                     as->parms[1].items->data);
4616         return ENOENT;
4617     }
4618     volid = vsu_GetVolumeID(as->parms[2].items->data, cstruct, &code);
4619     if (volid == 0) {
4620         if (code)
4621             PrintError("", code);
4622         else
4623             fprintf(STDERR, "Unknown volume ID or name '%s'\n",
4624                     as->parms[0].items->data);
4625         return -1;
4626     }
4627     if (as->parms[3].items)
4628         force = 1;
4629
4630     vcode = VLDB_GetEntryByID(volid, -1, &entry);
4631     if (vcode) {
4632         fprintf(STDERR,
4633                 "Could not fetch the entry for volume %lu from VLDB\n",
4634                 (unsigned long)volid);
4635         PrintError("convertROtoRW", code);
4636         return vcode;
4637     }
4638
4639     /* use RO volid even if user specified RW or BK volid */
4640
4641     if (volid != entry.volumeId[ROVOL])
4642         volid = entry.volumeId[ROVOL];
4643
4644     MapHostToNetwork(&entry);
4645     for (i = 0; i < entry.nServers; i++) {
4646         if (entry.serverFlags[i] & ITSRWVOL) {
4647             rwindex = i;
4648             rwserver = entry.serverNumber[i];
4649             rwpartition = entry.serverPartition[i];
4650         }
4651         if (entry.serverFlags[i] & ITSROVOL) {
4652             same = VLDB_IsSameAddrs(server, entry.serverNumber[i], &code);
4653             if (code) {
4654                 fprintf(STDERR,
4655                         "Failed to get info about server's %d address(es) from vlserver (err=%d); aborting call!\n",
4656                         server, code);
4657                 return ENOENT;
4658             }
4659             if (same) {
4660                 roindex = i;
4661                 roserver = entry.serverNumber[i];
4662                 ropartition = entry.serverPartition[i];
4663                 break;
4664             }
4665         }
4666     }
4667     if (!roserver) {
4668         fprintf(STDERR, "Warning: RO volume didn't exist in vldb!\n");
4669     }
4670     if (ropartition != partition) {
4671         fprintf(STDERR,
4672                 "Warning: RO volume should be in partition %d instead of %d (vldb)\n",
4673                 ropartition, partition);
4674     }
4675
4676     if (rwserver) {
4677         fprintf(STDERR,
4678                 "VLDB indicates that a RW volume exists already on %s in partition %s.\n",
4679                 hostutil_GetNameByINet(rwserver),
4680                 volutil_PartitionName(rwpartition));
4681         if (!force) {
4682             fprintf(STDERR, "Overwrite this VLDB entry? [y|n] (n)\n");
4683             dc = c = getchar();
4684             while (!(dc == EOF || dc == '\n'))
4685                 dc = getchar(); /* goto end of line */
4686             if ((c != 'y') && (c != 'Y')) {
4687                 fprintf(STDERR, "aborted.\n");
4688                 return -1;
4689             }
4690         }
4691     }
4692
4693     vcode =
4694         ubik_Call(VL_SetLock, cstruct, 0, entry.volumeId[RWVOL], RWVOL,
4695                   VLOP_MOVE);
4696     aconn = UV_Bind(server, AFSCONF_VOLUMEPORT);
4697     code = AFSVolConvertROtoRWvolume(aconn, partition, volid);
4698     if (code) {
4699         fprintf(STDERR,
4700                 "Converting RO volume %lu to RW volume failed with code %d\n",
4701                 (unsigned long)volid, code);
4702         PrintError("convertROtoRW ", code);
4703         return -1;
4704     }
4705     entry.serverFlags[roindex] = ITSRWVOL;
4706     entry.flags |= RW_EXISTS;
4707     entry.flags &= ~BACK_EXISTS;
4708     if (rwserver) {
4709         (entry.nServers)--;
4710         if (rwindex != entry.nServers) {
4711             entry.serverNumber[rwindex] = entry.serverNumber[entry.nServers];
4712             entry.serverPartition[rwindex] =
4713                 entry.serverPartition[entry.nServers];
4714             entry.serverFlags[rwindex] = entry.serverFlags[entry.nServers];
4715             entry.serverNumber[entry.nServers] = 0;
4716             entry.serverPartition[entry.nServers] = 0;
4717             entry.serverFlags[entry.nServers] = 0;
4718         }
4719     }
4720     entry.flags &= ~RO_EXISTS;
4721     for (i = 0; i < entry.nServers; i++) {
4722         if (entry.serverFlags[i] & ITSROVOL) {
4723             if (!(entry.serverFlags[i] & (RO_DONTUSE | NEW_REPSITE)))
4724                 entry.flags |= RO_EXISTS;
4725         }
4726     }
4727     MapNetworkToHost(&entry, &storeEntry);
4728     code =
4729         VLDB_ReplaceEntry(entry.volumeId[RWVOL], RWVOL, &storeEntry,
4730                           (LOCKREL_OPCODE | LOCKREL_AFSID |
4731                            LOCKREL_TIMESTAMP));
4732     if (code) {
4733         fprintf(STDERR,
4734                 "Warning: volume converted, but vldb update failed with code %d!\n",
4735                 code);
4736     }
4737     vcode = UV_LockRelease(entry.volumeId[RWVOL]);
4738     if (vcode) {
4739         PrintDiagnostics("unlock", vcode);
4740     }
4741     return code;
4742 }
4743
4744 static
4745 Sizes(as)
4746      register struct cmd_syndesc *as;
4747 {
4748     afs_int32 avolid, aserver, apart, voltype, fromdate = 0, code, err, i;
4749     struct nvldbentry entry;
4750     volintSize vol_size;
4751
4752     rx_SetRxDeadTime(60 * 10);
4753     for (i = 0; i < MAXSERVERS; i++) {
4754         struct rx_connection *rxConn = ubik_GetRPCConn(cstruct, i);
4755         if (rxConn == 0)
4756             break;
4757         rx_SetConnDeadTime(rxConn, rx_connDeadTime);
4758         if (rxConn->service)
4759             rxConn->service->connDeadTime = rx_connDeadTime;
4760     }
4761
4762     avolid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
4763     if (avolid == 0) {
4764         if (err)
4765             PrintError("", err);
4766         else
4767             fprintf(STDERR, "vos: can't find volume '%s'\n",
4768                     as->parms[0].items->data);
4769         return ENOENT;
4770     }
4771
4772     if (as->parms[1].items || as->parms[2].items) {
4773         if (!as->parms[1].items || !as->parms[2].items) {
4774             fprintf(STDERR,
4775                     "Must specify both -server and -partition options\n");
4776             return -1;
4777         }
4778         aserver = GetServer(as->parms[2].items->data);
4779         if (aserver == 0) {
4780             fprintf(STDERR, "Invalid server name\n");
4781             return -1;
4782         }
4783         apart = volutil_GetPartitionID(as->parms[1].items->data);
4784         if (apart < 0) {
4785             fprintf(STDERR, "Invalid partition name\n");
4786             return -1;
4787         }
4788     } else {
4789         code = GetVolumeInfo(avolid, &aserver, &apart, &voltype, &entry);
4790         if (code)
4791             return code;
4792     }
4793
4794     fromdate = 0;
4795
4796     if (as->parms[4].items && strcmp(as->parms[4].items->data, "0")) {
4797         code = ktime_DateToInt32(as->parms[4].items->data, &fromdate);
4798         if (code) {
4799             fprintf(STDERR, "vos: failed to parse date '%s' (error=%d))\n",
4800                     as->parms[1].items->data, code);
4801             return code;
4802         }
4803     }
4804
4805     fprintf(STDOUT, "Volume: %s\n", as->parms[0].items->data);
4806
4807     if (as->parms[3].items) {   /* do the dump estimate */
4808         vol_size.dump_size = 0;
4809         code = UV_GetSize(avolid, aserver, apart, fromdate, &vol_size);
4810         if (code) {
4811             PrintDiagnostics("size", code);
4812             return code;
4813         }
4814         /* presumably the size info is now gathered in pntr */
4815         /* now we display it */
4816
4817         fprintf(STDOUT, "dump_size: %llu\n", vol_size.dump_size);
4818     }
4819
4820     /* Display info */
4821
4822     return 0;
4823 }
4824
4825 PrintDiagnostics(astring, acode)
4826      char *astring;
4827      afs_int32 acode;
4828 {
4829     if (acode == EACCES) {
4830         fprintf(STDERR,
4831                 "You are not authorized to perform the 'vos %s' command (%d)\n",
4832                 astring, acode);
4833     } else {
4834         fprintf(STDERR, "Error in vos %s command.\n", astring);
4835         PrintError("", acode);
4836     }
4837     return 0;
4838 }
4839
4840
4841 static
4842 MyBeforeProc(as, arock)
4843      struct cmd_syndesc *as;
4844      char *arock;
4845 {
4846     register char *tcell;
4847     register afs_int32 code;
4848     register afs_int32 sauth;
4849
4850     /* Initialize the ubik_client connection */
4851     rx_SetRxDeadTime(90);
4852     cstruct = (struct ubik_client *)0;
4853
4854     sauth = 0;
4855     tcell = NULL;
4856     if (as->parms[12].items)    /* if -cell specified */
4857         tcell = as->parms[12].items->data;
4858     if (as->parms[14].items)    /* -serverauth specified */
4859         sauth = 1;
4860     if (as->parms[16].items)    /* -crypt specified */
4861         vsu_SetCrypt(1);
4862     if ((code =
4863          vsu_ClientInit((as->parms[13].items != 0), confdir, tcell, sauth,
4864                         &cstruct, UV_SetSecurity))) {
4865         fprintf(STDERR, "could not initialize VLDB library (code=%lu) \n",
4866                 (unsigned long)code);
4867         exit(1);
4868     }
4869     rxInitDone = 1;
4870     if (as->parms[15].items)    /* -verbose flag set */
4871         verbose = 1;
4872     else
4873         verbose = 0;
4874     return 0;
4875 }
4876
4877 int
4878 osi_audit()
4879 {
4880 /* this sucks but it works for now.
4881 */
4882     return 0;
4883 }
4884
4885 #include "AFS_component_version_number.c"
4886
4887 main(argc, argv)
4888      int argc;
4889      char **argv;
4890 {
4891     register afs_int32 code;
4892
4893     register struct cmd_syndesc *ts;
4894
4895 #ifdef  AFS_AIX32_ENV
4896     /*
4897      * The following signal action for AIX is necessary so that in case of a 
4898      * crash (i.e. core is generated) we can include the user's data section 
4899      * in the core dump. Unfortunately, by default, only a partial core is
4900      * generated which, in many cases, isn't too useful.
4901      */
4902     struct sigaction nsa;
4903
4904     sigemptyset(&nsa.sa_mask);
4905     nsa.sa_handler = SIG_DFL;
4906     nsa.sa_flags = SA_FULLDUMP;
4907     sigaction(SIGSEGV, &nsa, NULL);
4908 #endif
4909
4910     confdir = AFSDIR_CLIENT_ETC_DIRPATH;
4911
4912     cmd_SetBeforeProc(MyBeforeProc, NULL);
4913
4914     ts = cmd_CreateSyntax("create", CreateVolume, 0, "create a new volume");
4915     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
4916     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "partition name");
4917     cmd_AddParm(ts, "-name", CMD_SINGLE, 0, "volume name");
4918     cmd_AddParm(ts, "-maxquota", CMD_SINGLE, CMD_OPTIONAL,
4919                 "initial quota (KB)");
4920 #ifdef notdef
4921     cmd_AddParm(ts, "-minquota", CMD_SINGLE, CMD_OPTIONAL, "");
4922 #endif
4923     COMMONPARMS;
4924
4925     ts = cmd_CreateSyntax("remove", DeleteVolume, 0, "delete a volume");
4926     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "machine name");
4927     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
4928     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
4929
4930     COMMONPARMS;
4931
4932     ts = cmd_CreateSyntax("move", MoveVolume, 0, "move a volume");
4933     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
4934     cmd_AddParm(ts, "-fromserver", CMD_SINGLE, 0, "machine name on source");
4935     cmd_AddParm(ts, "-frompartition", CMD_SINGLE, 0,
4936                 "partition name on source");
4937     cmd_AddParm(ts, "-toserver", CMD_SINGLE, 0,
4938                 "machine name on destination");
4939     cmd_AddParm(ts, "-topartition", CMD_SINGLE, 0,
4940                 "partition name on destination");
4941     COMMONPARMS;
4942
4943     ts = cmd_CreateSyntax("copy", CopyVolume, 0, "copy a volume");
4944     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID on source");
4945     cmd_AddParm(ts, "-fromserver", CMD_SINGLE, 0, "machine name on source");
4946     cmd_AddParm(ts, "-frompartition", CMD_SINGLE, 0,
4947                 "partition name on source");
4948     cmd_AddParm(ts, "-toname", CMD_SINGLE, 0, "volume name on destination");
4949     cmd_AddParm(ts, "-toserver", CMD_SINGLE, 0,
4950                 "machine name on destination");
4951     cmd_AddParm(ts, "-topartition", CMD_SINGLE, 0,
4952                 "partition name on destination");
4953     COMMONPARMS;
4954
4955     ts = cmd_CreateSyntax("backup", BackupVolume, 0,
4956                           "make backup of a volume");
4957     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
4958     COMMONPARMS;
4959
4960     ts = cmd_CreateSyntax("release", ReleaseVolume, 0, "release a volume");
4961     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
4962     cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL,
4963                 "force a complete release");
4964     COMMONPARMS;
4965
4966     ts = cmd_CreateSyntax("dump", DumpVolume, 0, "dump a volume");
4967     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
4968     cmd_AddParm(ts, "-time", CMD_SINGLE, CMD_OPTIONAL, "dump from time");
4969     cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL, "dump file");
4970     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "server");
4971     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition");
4972     cmd_AddParm(ts, "-clone", CMD_FLAG, CMD_OPTIONAL,
4973                 "dump a clone of the volume");
4974     COMMONPARMS;
4975
4976     ts = cmd_CreateSyntax("restore", RestoreVolume, 0, "restore a volume");
4977     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
4978     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "partition name");
4979     cmd_AddParm(ts, "-name", CMD_SINGLE, 0, "name of volume to be restored");
4980     cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL, "dump file");
4981     cmd_AddParm(ts, "-id", CMD_SINGLE, CMD_OPTIONAL, "volume ID");
4982     cmd_AddParm(ts, "-overwrite", CMD_SINGLE, CMD_OPTIONAL,
4983                 "abort | full | incremental");
4984     cmd_AddParm(ts, "-offline", CMD_FLAG, CMD_OPTIONAL,
4985                 "leave restored volume offline");
4986     cmd_AddParm(ts, "-readonly", CMD_FLAG, CMD_OPTIONAL,
4987                 "make restored volume read-only");
4988     COMMONPARMS;
4989
4990     ts = cmd_CreateSyntax("unlock", LockReleaseCmd, 0,
4991                           "release lock on VLDB entry for a volume");
4992     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
4993     COMMONPARMS;
4994
4995     ts = cmd_CreateSyntax("changeloc", ChangeLocation, 0,
4996                           "change an RW volume's location in the VLDB");
4997     cmd_AddParm(ts, "-server", CMD_SINGLE, 0,
4998                 "machine name for new location");
4999     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0,
5000                 "partition name for new location");
5001     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
5002     COMMONPARMS;
5003
5004     ts = cmd_CreateSyntax("addsite", AddSite, 0, "add a replication site");
5005     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name for new site");
5006     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0,
5007                 "partition name for new site");
5008     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
5009     COMMONPARMS;
5010
5011     ts = cmd_CreateSyntax("remsite", RemoveSite, 0,
5012                           "remove a replication site");
5013     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
5014     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "partition name");
5015     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
5016     COMMONPARMS;
5017
5018     ts = cmd_CreateSyntax("listpart", ListPartitions, 0, "list partitions");
5019     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
5020     COMMONPARMS;
5021
5022     ts = cmd_CreateSyntax("listvol", ListVolumes, 0,
5023                           "list volumes on server (bypass VLDB)");
5024     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
5025     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
5026     cmd_AddParm(ts, "-fast", CMD_FLAG, CMD_OPTIONAL, "minimal listing");
5027     cmd_AddParm(ts, "-long", CMD_FLAG, CMD_OPTIONAL,
5028                 "list all normal volume fields");
5029     cmd_AddParm(ts, "-quiet", CMD_FLAG, CMD_OPTIONAL,
5030                 "generate minimal information");
5031     cmd_AddParm(ts, "-extended", CMD_FLAG, CMD_OPTIONAL,
5032                 "list extended volume fields");
5033 #ifdef FULL_LISTVOL_SWITCH
5034     cmd_AddParm(ts, "-format", CMD_FLAG, CMD_OPTIONAL,
5035                 "machine readable format");
5036 #endif /* FULL_LISTVOL_SWITCH */
5037     COMMONPARMS;
5038
5039     ts = cmd_CreateSyntax("syncvldb", SyncVldb, 0,
5040                           "synchronize VLDB with server");
5041     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "machine name");
5042     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
5043     cmd_AddParm(ts, "-volume", CMD_SINGLE, CMD_OPTIONAL, "volume name or ID");
5044     COMMONPARMS;
5045
5046     ts = cmd_CreateSyntax("syncserv", SyncServer, 0,
5047                           "synchronize server with VLDB");
5048     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
5049     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
5050     COMMONPARMS;
5051
5052     ts = cmd_CreateSyntax("examine", ExamineVolume, 0,
5053                           "everything about the volume");
5054     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
5055     cmd_AddParm(ts, "-extended", CMD_FLAG, CMD_OPTIONAL,
5056                 "list extended volume fields");
5057 #ifdef FULL_LISTVOL_SWITCH
5058     cmd_AddParm(ts, "-format", CMD_FLAG, CMD_OPTIONAL,
5059                 "machine readable format");
5060 #endif /* FULL_LISTVOL_SWITCH */
5061     COMMONPARMS;
5062     cmd_CreateAlias(ts, "volinfo");
5063
5064     ts = cmd_CreateSyntax("setfields", SetFields, 0,
5065                           "change volume info fields");
5066     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
5067     cmd_AddParm(ts, "-maxquota", CMD_SINGLE, CMD_OPTIONAL, "quota (KB)");
5068     cmd_AddParm(ts, "-clearuse", CMD_FLAG, CMD_OPTIONAL, "clear dayUse");
5069     COMMONPARMS;
5070
5071     ts = cmd_CreateSyntax("offline", volOffline, 0, (char *)CMD_HIDDEN);
5072     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "server name");
5073     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "partition name");
5074     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
5075     cmd_AddParm(ts, "-sleep", CMD_SINGLE, CMD_OPTIONAL, "seconds to sleep");
5076     cmd_AddParm(ts, "-busy", CMD_FLAG, CMD_OPTIONAL, "busy volume");
5077     COMMONPARMS;
5078
5079     ts = cmd_CreateSyntax("online", volOnline, 0, (char *)CMD_HIDDEN);
5080     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "server name");
5081     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "partition name");
5082     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
5083     COMMONPARMS;
5084
5085     ts = cmd_CreateSyntax("zap", VolumeZap, 0,
5086                           "delete the volume, don't bother with VLDB");
5087     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
5088     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "partition name");
5089     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume ID");
5090     cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL,
5091                 "force deletion of bad volumes");
5092     cmd_AddParm(ts, "-backup", CMD_FLAG, CMD_OPTIONAL,
5093                 "also delete backup volume if one is found");
5094     COMMONPARMS;
5095
5096     ts = cmd_CreateSyntax("status", VolserStatus, 0,
5097                           "report on volser status");
5098     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
5099     COMMONPARMS;
5100
5101     ts = cmd_CreateSyntax("rename", RenameVolume, 0, "rename a volume");
5102     cmd_AddParm(ts, "-oldname", CMD_SINGLE, 0, "old volume name ");
5103     cmd_AddParm(ts, "-newname", CMD_SINGLE, 0, "new volume name ");
5104     COMMONPARMS;
5105
5106     ts = cmd_CreateSyntax("listvldb", ListVLDB, 0,
5107                           "list volumes in the VLDB");
5108     cmd_AddParm(ts, "-name", CMD_SINGLE, CMD_OPTIONAL, "volume name or ID");
5109     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "machine name");
5110     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
5111     cmd_AddParm(ts, "-locked", CMD_FLAG, CMD_OPTIONAL, "locked volumes only");
5112     cmd_AddParm(ts, "-quiet", CMD_FLAG, CMD_OPTIONAL,
5113                 "generate minimal information");
5114     cmd_AddParm(ts, "-nosort", CMD_FLAG, CMD_OPTIONAL,
5115                 "do not alphabetically sort the volume names");
5116     COMMONPARMS;
5117
5118     ts = cmd_CreateSyntax("backupsys", BackSys, 0, "en masse backups");
5119     cmd_AddParm(ts, "-prefix", CMD_LIST, CMD_OPTIONAL,
5120                 "common prefix on volume(s)");
5121     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "machine name");
5122     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
5123     cmd_AddParm(ts, "-exclude", CMD_FLAG, CMD_OPTIONAL,
5124                 "exclude common prefix volumes");
5125     cmd_AddParm(ts, "-xprefix", CMD_LIST, CMD_OPTIONAL,
5126                 "negative prefix on volume(s)");
5127     cmd_AddParm(ts, "-dryrun", CMD_FLAG, CMD_OPTIONAL, "no action");
5128     COMMONPARMS;
5129
5130     ts = cmd_CreateSyntax("delentry", DeleteEntry, 0,
5131                           "delete VLDB entry for a volume");
5132     cmd_AddParm(ts, "-id", CMD_LIST, CMD_OPTIONAL, "volume name or ID");
5133     cmd_AddParm(ts, "-prefix", CMD_SINGLE, CMD_OPTIONAL,
5134                 "prefix of the volume whose VLDB entry is to be deleted");
5135     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "machine name");
5136     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
5137     cmd_AddParm(ts, "-noexecute", CMD_FLAG, CMD_OPTIONAL | CMD_HIDE,
5138                 "no execute");
5139     COMMONPARMS;
5140
5141     ts = cmd_CreateSyntax("partinfo", PartitionInfo, 0,
5142                           "list partition information");
5143     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
5144     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
5145     COMMONPARMS;
5146
5147     ts = cmd_CreateSyntax("unlockvldb", UnlockVLDB, 0,
5148                           "unlock all the locked entries in the VLDB");
5149     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "machine name");
5150     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
5151     COMMONPARMS;
5152
5153     ts = cmd_CreateSyntax("lock", LockEntry, 0,
5154                           "lock VLDB entry for a volume");
5155     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
5156     COMMONPARMS;
5157
5158     ts = cmd_CreateSyntax("changeaddr", ChangeAddr, 0,
5159                           "change the IP address of a file server");
5160     cmd_AddParm(ts, "-oldaddr", CMD_SINGLE, 0, "original IP address");
5161     cmd_AddParm(ts, "-newaddr", CMD_SINGLE, CMD_OPTIONAL, "new IP address");
5162     cmd_AddParm(ts, "-remove", CMD_FLAG, CMD_OPTIONAL,
5163                 "remove the IP address from the VLDB");
5164     COMMONPARMS;
5165
5166     ts = cmd_CreateSyntax("listaddrs", ListAddrs, 0,
5167                           "list the IP address of all file servers registered in the VLDB");
5168     cmd_AddParm(ts, "-uuid", CMD_SINGLE, CMD_OPTIONAL, "uuid of server");
5169     cmd_AddParm(ts, "-host", CMD_SINGLE, CMD_OPTIONAL, "address of host");
5170     cmd_AddParm(ts, "-noresolve", CMD_FLAG, CMD_OPTIONAL,
5171                 "don't resolve addresses");
5172     cmd_AddParm(ts, "-printuuid", CMD_FLAG, CMD_OPTIONAL,
5173                 "print uuid of hosts");
5174     COMMONPARMS;
5175
5176     ts = cmd_CreateSyntax("convertROtoRW", ConvertRO, 0,
5177                           "convert a RO volume into a RW volume (after loss of old RW volume)");
5178     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
5179     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "partition name");
5180     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
5181     cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL, "don't ask");
5182     COMMONPARMS;
5183
5184     ts = cmd_CreateSyntax("size", Sizes, 0,
5185                           "obtain various sizes of the volume.");
5186     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
5187     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
5188     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "machine name");
5189     cmd_AddParm(ts, "-dump", CMD_FLAG, CMD_OPTIONAL,
5190                 "Obtain the size of the dump");
5191     cmd_AddParm(ts, "-time", CMD_SINGLE, CMD_OPTIONAL, "dump from time");
5192     COMMONPARMS;
5193
5194     code = cmd_Dispatch(argc, argv);
5195     if (rxInitDone) {
5196         /* Shut down the ubik_client and rx connections */
5197         if (cstruct) {
5198             (void)ubik_ClientDestroy(cstruct);
5199             cstruct = 0;
5200         }
5201         rx_Finalize();
5202     }
5203
5204     exit((code ? -1 : 0));
5205 }