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