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