2735ac11e5d3d5aa7c48baade68eb2f48bd90dac
[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, totalNotOK, totalBusy;
1478
1479     DisplayFormat(pntr, server, part, &totalOK, &totalNotOK, &totalBusy, 0, 1,
1480                   1);
1481     return;
1482 }
1483
1484 /* command to forcibly remove a volume */
1485 static int
1486 NukeVolume(struct cmd_syndesc *as)
1487 {
1488     afs_int32 code;
1489     afs_uint32 volID;
1490     afs_int32  err;
1491     afs_int32 partID;
1492     afs_uint32 server;
1493     char *tp;
1494
1495     server = GetServer(tp = as->parms[0].items->data);
1496     if (!server) {
1497         fprintf(STDERR, "vos: server '%s' not found in host table\n", tp);
1498         return 1;
1499     }
1500
1501     partID = volutil_GetPartitionID(tp = as->parms[1].items->data);
1502     if (partID == -1) {
1503         fprintf(STDERR, "vos: could not parse '%s' as a partition name", tp);
1504         return 1;
1505     }
1506
1507     volID = vsu_GetVolumeID(tp = as->parms[2].items->data, cstruct, &err);
1508     if (volID == 0) {
1509         if (err)
1510             PrintError("", err);
1511         else
1512             fprintf(STDERR,
1513                     "vos: could not parse '%s' as a numeric volume ID", tp);
1514         return 1;
1515     }
1516
1517     fprintf(STDOUT,
1518             "vos: forcibly removing all traces of volume %d, please wait...",
1519             volID);
1520     fflush(STDOUT);
1521     code = UV_NukeVolume(server, partID, volID);
1522     if (code == 0)
1523         fprintf(STDOUT, "done.\n");
1524     else
1525         fprintf(STDOUT, "failed with code %d.\n", code);
1526     return code;
1527 }
1528
1529
1530 /*------------------------------------------------------------------------
1531  * PRIVATE ExamineVolume
1532  *
1533  * Description:
1534  *      Routine used to examine a single volume, contacting the VLDB as
1535  *      well as the Volume Server.
1536  *
1537  * Arguments:
1538  *      as : Ptr to parsed command line arguments.
1539  *
1540  * Returns:
1541  *      0 for a successful operation,
1542  *      Otherwise, one of the ubik or VolServer error values.
1543  *
1544  * Environment:
1545  *      Nothing interesting.
1546  *
1547  * Side Effects:
1548  *      As advertised.
1549  *------------------------------------------------------------------------
1550  */
1551 static int
1552 ExamineVolume(struct cmd_syndesc *as, void *arock)
1553 {
1554     struct nvldbentry entry;
1555     afs_int32 vcode = 0;
1556     volintInfo *pntr = (volintInfo *) 0;
1557     volintXInfo *xInfoP = (volintXInfo *) 0;
1558     afs_uint32 volid;
1559     afs_int32 code, err, error = 0;
1560     int voltype, foundserv = 0, foundentry = 0;
1561     afs_uint32 aserver;
1562     afs_int32 apart;
1563     int previdx = -1;
1564     int wantExtendedInfo;       /*Do we want extended vol info? */
1565     int isSubEnum=0;            /* Keep track whether sub enumerate called. */
1566     wantExtendedInfo = (as->parms[1].items ? 1 : 0);    /* -extended */
1567
1568     volid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);   /* -id */
1569     if (volid == 0) {
1570         if (err)
1571             PrintError("", err);
1572         else
1573             fprintf(STDERR, "Unknown volume ID or name '%s'\n",
1574                     as->parms[0].items->data);
1575         return -1;
1576     }
1577
1578     if (verbose) {
1579         fprintf(STDOUT, "Fetching VLDB entry for %lu .. ",
1580                 (unsigned long)volid);
1581         fflush(STDOUT);
1582     }
1583     vcode = VLDB_GetEntryByID(volid, -1, &entry);
1584     if (vcode) {
1585         fprintf(STDERR,
1586                 "Could not fetch the entry for volume number %lu from VLDB \n",
1587                 (unsigned long)volid);
1588         return (vcode);
1589     }
1590     if (verbose)
1591         fprintf(STDOUT, "done\n");
1592     MapHostToNetwork(&entry);
1593
1594     if (entry.volumeId[RWVOL] == volid)
1595         voltype = RWVOL;
1596     else if (entry.volumeId[BACKVOL] == volid)
1597         voltype = BACKVOL;
1598     else                        /* (entry.volumeId[ROVOL] == volid) */
1599         voltype = ROVOL;
1600
1601     do {                        /* do {...} while (voltype == ROVOL) */
1602         /* Get the entry for the volume. If its a RW vol, get the RW entry.
1603          * It its a BK vol, get the RW entry (even if VLDB may say the BK doen't exist).
1604          * If its a RO vol, get the next RO entry.
1605          */
1606         GetServerAndPart(&entry, ((voltype == ROVOL) ? ROVOL : RWVOL),
1607                          &aserver, &apart, &previdx);
1608         if (previdx == -1) {    /* searched all entries */
1609             if (!foundentry) {
1610                 fprintf(STDERR, "Volume %s does not exist in VLDB\n\n",
1611                         as->parms[0].items->data);
1612                 error = ENOENT;
1613             }
1614             break;
1615         }
1616         foundentry = 1;
1617
1618         /* Get information about the volume from the server */
1619         if (verbose) {
1620             fprintf(STDOUT, "Getting volume listing from the server %s .. ",
1621                     hostutil_GetNameByINet(aserver));
1622             fflush(STDOUT);
1623         }
1624         if (wantExtendedInfo)
1625             code = UV_XListOneVolume(aserver, apart, volid, &xInfoP);
1626         else
1627             code = UV_ListOneVolume(aserver, apart, volid, &pntr);
1628         if (verbose)
1629             fprintf(STDOUT, "done\n");
1630
1631         if (code) {
1632             error = code;
1633             if (code == ENODEV) {
1634                 if ((voltype == BACKVOL) && !(entry.flags & BACK_EXISTS)) {
1635                     /* The VLDB says there is no backup volume and its not on disk */
1636                     fprintf(STDERR, "Volume %s does not exist\n",
1637                             as->parms[0].items->data);
1638                     error = ENOENT;
1639                 } else {
1640                     fprintf(STDERR,
1641                             "Volume does not exist on server %s as indicated by the VLDB\n",
1642                             hostutil_GetNameByINet(aserver));
1643                 }
1644             } else {
1645                 PrintDiagnostics("examine", code);
1646             }
1647             fprintf(STDOUT, "\n");
1648         } else {
1649             foundserv = 1;
1650             if (wantExtendedInfo)
1651                 XVolumeStats(xInfoP, &entry, aserver, apart, voltype);
1652             else if (as->parms[2].items) {
1653                 DisplayFormat2(aserver, apart, pntr);
1654                 EnumerateEntry(&entry);
1655                 isSubEnum = 1;
1656             } else
1657                 VolumeStats_int(pntr, &entry, aserver, apart, voltype);
1658
1659             if ((voltype == BACKVOL) && !(entry.flags & BACK_EXISTS)) {
1660                 /* The VLDB says there is no backup volume yet we found one on disk */
1661                 fprintf(STDERR, "Volume %s does not exist in VLDB\n",
1662                         as->parms[0].items->data);
1663                 error = ENOENT;
1664             }
1665         }
1666
1667         if (pntr)
1668             free(pntr);
1669         if (xInfoP)
1670             free(xInfoP);
1671     } while (voltype == ROVOL);
1672
1673     if (!foundserv) {
1674         fprintf(STDERR, "Dump only information from VLDB\n\n");
1675         fprintf(STDOUT, "%s \n", entry.name);   /* PostVolumeStats doesn't print name */
1676     }
1677
1678     if (!isSubEnum)
1679         PostVolumeStats(&entry);
1680
1681     return (error);
1682 }
1683
1684 /*------------------------------------------------------------------------
1685  * PRIVATE SetFields
1686  *
1687  * Description:
1688  *      Routine used to change the status of a single volume.
1689  *
1690  * Arguments:
1691  *      as : Ptr to parsed command line arguments.
1692  *
1693  * Returns:
1694  *      0 for a successful operation,
1695  *      Otherwise, one of the ubik or VolServer error values.
1696  *
1697  * Environment:
1698  *      Nothing interesting.
1699  *
1700  * Side Effects:
1701  *      As advertised.
1702  *------------------------------------------------------------------------
1703  */
1704 static int
1705 SetFields(struct cmd_syndesc *as, void *arock)
1706 {
1707     struct nvldbentry entry;
1708     volintInfo info;
1709     afs_uint32 volid;
1710     afs_int32 code, err;
1711     afs_uint32 aserver;
1712     afs_int32 apart;
1713     int previdx = -1;
1714
1715     volid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);   /* -id */
1716     if (volid == 0) {
1717         if (err)
1718             PrintError("", err);
1719         else
1720             fprintf(STDERR, "Unknown volume ID or name '%s'\n",
1721                     as->parms[0].items->data);
1722         return -1;
1723     }
1724
1725     code = VLDB_GetEntryByID(volid, RWVOL, &entry);
1726     if (code) {
1727         fprintf(STDERR,
1728                 "Could not fetch the entry for volume number %lu from VLDB \n",
1729                 (unsigned long)volid);
1730         return (code);
1731     }
1732     MapHostToNetwork(&entry);
1733
1734     GetServerAndPart(&entry, RWVOL, &aserver, &apart, &previdx);
1735     if (previdx == -1) {
1736         fprintf(STDERR, "Volume %s does not exist in VLDB\n\n",
1737                 as->parms[0].items->data);
1738         return (ENOENT);
1739     }
1740
1741     init_volintInfo(&info);
1742     info.volid = volid;
1743     info.type = RWVOL;
1744
1745     if (as->parms[1].items) {
1746         /* -max <quota> */
1747         code = util_GetHumanInt32(as->parms[1].items->data, &info.maxquota);
1748         if (code) {
1749             fprintf(STDERR, "invalid quota value\n");
1750             return code;
1751         }
1752     }
1753     if (as->parms[2].items) {
1754         /* -clearuse */
1755         info.dayUse = 0;
1756     }
1757     if (as->parms[3].items) {
1758         /* -clearVolUpCounter */
1759         info.spare2 = 0;
1760     }
1761     code = UV_SetVolumeInfo(aserver, apart, volid, &info);
1762     if (code)
1763         fprintf(STDERR,
1764                 "Could not update volume info fields for volume number %lu\n",
1765                 (unsigned long)volid);
1766     return (code);
1767 }
1768
1769 /*------------------------------------------------------------------------
1770  * PRIVATE volOnline
1771  *
1772  * Description:
1773  *      Brings a volume online.
1774  *
1775  * Arguments:
1776  *      as : Ptr to parsed command line arguments.
1777  *
1778  * Returns:
1779  *      0 for a successful operation,
1780  *
1781  * Environment:
1782  *      Nothing interesting.
1783  *
1784  * Side Effects:
1785  *      As advertised.
1786  *------------------------------------------------------------------------
1787  */
1788 static int
1789 volOnline(struct cmd_syndesc *as, void *arock)
1790 {
1791     afs_uint32 server;
1792     afs_int32 partition;
1793     afs_uint32 volid;
1794     afs_int32 code, err = 0;
1795
1796     server = GetServer(as->parms[0].items->data);
1797     if (server == 0) {
1798         fprintf(STDERR, "vos: server '%s' not found in host table\n",
1799                 as->parms[0].items->data);
1800         return -1;
1801     }
1802
1803     partition = volutil_GetPartitionID(as->parms[1].items->data);
1804     if (partition < 0) {
1805         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
1806                 as->parms[1].items->data);
1807         return ENOENT;
1808     }
1809
1810     volid = vsu_GetVolumeID(as->parms[2].items->data, cstruct, &err);   /* -id */
1811     if (!volid) {
1812         if (err)
1813             PrintError("", err);
1814         else
1815             fprintf(STDERR, "Unknown volume ID or name '%s'\n",
1816                     as->parms[0].items->data);
1817         return -1;
1818     }
1819
1820     code = UV_SetVolume(server, partition, volid, ITOffline, 0 /*online */ ,
1821                         0 /*sleep */ );
1822     if (code) {
1823         fprintf(STDERR, "Failed to set volume. Code = %d\n", code);
1824         return -1;
1825     }
1826
1827     return 0;
1828 }
1829
1830 /*------------------------------------------------------------------------
1831  * PRIVATE volOffline
1832  *
1833  * Description:
1834  *      Brings a volume offline.
1835  *
1836  * Arguments:
1837  *      as : Ptr to parsed command line arguments.
1838  *
1839  * Returns:
1840  *      0 for a successful operation,
1841  *
1842  * Environment:
1843  *      Nothing interesting.
1844  *
1845  * Side Effects:
1846  *      As advertised.
1847  *------------------------------------------------------------------------
1848  */
1849 static int
1850 volOffline(struct cmd_syndesc *as, void *arock)
1851 {
1852     afs_uint32 server;
1853     afs_int32 partition;
1854     afs_uint32 volid;
1855     afs_int32 code, err = 0;
1856     afs_int32 transflag, sleeptime, transdone;
1857
1858     server = GetServer(as->parms[0].items->data);
1859     if (server == 0) {
1860         fprintf(STDERR, "vos: server '%s' not found in host table\n",
1861                 as->parms[0].items->data);
1862         return -1;
1863     }
1864
1865     partition = volutil_GetPartitionID(as->parms[1].items->data);
1866     if (partition < 0) {
1867         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
1868                 as->parms[1].items->data);
1869         return ENOENT;
1870     }
1871
1872     volid = vsu_GetVolumeID(as->parms[2].items->data, cstruct, &err);   /* -id */
1873     if (!volid) {
1874         if (err)
1875             PrintError("", err);
1876         else
1877             fprintf(STDERR, "Unknown volume ID or name '%s'\n",
1878                     as->parms[0].items->data);
1879         return -1;
1880     }
1881
1882     transflag = (as->parms[4].items ? ITBusy : ITOffline);
1883     sleeptime = (as->parms[3].items ? atol(as->parms[3].items->data) : 0);
1884     transdone = ((sleeptime || as->parms[4].items) ? 0 /*online */ : VTOutOfService);
1885     if (as->parms[4].items && !as->parms[3].items) {
1886         fprintf(STDERR, "-sleep option must be used with -busy flag\n");
1887         return -1;
1888     }
1889
1890     code =
1891         UV_SetVolume(server, partition, volid, transflag, transdone,
1892                      sleeptime);
1893     if (code) {
1894         fprintf(STDERR, "Failed to set volume. Code = %d\n", code);
1895         return -1;
1896     }
1897
1898     return 0;
1899 }
1900
1901 static int
1902 CreateVolume(struct cmd_syndesc *as, void *arock)
1903 {
1904     afs_int32 pnum;
1905     char part[10];
1906     afs_uint32 volid = 0, rovolid = 0, bkvolid = 0;
1907     afs_uint32 *arovolid;
1908     afs_int32 code;
1909     struct nvldbentry entry;
1910     afs_int32 vcode;
1911     afs_int32 quota;
1912     afs_uint32 tserver;
1913
1914     arovolid = &rovolid;
1915
1916     quota = 5000;
1917     tserver = GetServer(as->parms[0].items->data);
1918     if (!tserver) {
1919         fprintf(STDERR, "vos: host '%s' not found in host table\n",
1920                 as->parms[0].items->data);
1921         return ENOENT;
1922     }
1923     pnum = volutil_GetPartitionID(as->parms[1].items->data);
1924     if (pnum < 0) {
1925         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
1926                 as->parms[1].items->data);
1927         return ENOENT;
1928     }
1929     if (!IsPartValid(pnum, tserver, &code)) {   /*check for validity of the partition */
1930         if (code)
1931             PrintError("", code);
1932         else
1933             fprintf(STDERR,
1934                     "vos : partition %s does not exist on the server\n",
1935                     as->parms[1].items->data);
1936         return ENOENT;
1937     }
1938     if (!ISNAMEVALID(as->parms[2].items->data)) {
1939         fprintf(STDERR,
1940                 "vos: the name of the root volume %s exceeds the size limit of %d\n",
1941                 as->parms[2].items->data, VOLSER_OLDMAXVOLNAME - 10);
1942         return E2BIG;
1943     }
1944     if (!VolNameOK(as->parms[2].items->data)) {
1945         fprintf(STDERR,
1946                 "Illegal volume name %s, should not end in .readonly or .backup\n",
1947                 as->parms[2].items->data);
1948         return EINVAL;
1949     }
1950     if (IsNumeric(as->parms[2].items->data)) {
1951         fprintf(STDERR, "Illegal volume name %s, should not be a number\n",
1952                 as->parms[2].items->data);
1953         return EINVAL;
1954     }
1955     vcode = VLDB_GetEntryByName(as->parms[2].items->data, &entry);
1956     if (!vcode) {
1957         fprintf(STDERR, "Volume %s already exists\n",
1958                 as->parms[2].items->data);
1959         PrintDiagnostics("create", code);
1960         return EEXIST;
1961     }
1962
1963     if (as->parms[3].items) {
1964         code = util_GetHumanInt32(as->parms[3].items->data, &quota);
1965         if (code) {
1966             fprintf(STDERR, "vos: bad integer specified for quota.\n");
1967             return code;
1968         }
1969     }
1970
1971     if (as->parms[4].items) {
1972         if (!IsNumeric(as->parms[4].items->data)) {
1973             fprintf(STDERR, "vos: Given volume ID %s should be numeric.\n",
1974                     as->parms[4].items->data);
1975             return EINVAL;
1976         }
1977
1978         code = util_GetUInt32(as->parms[4].items->data, &volid);
1979         if (code) {
1980             fprintf(STDERR, "vos: bad integer specified for volume ID.\n");
1981             return code;
1982         }
1983     }
1984
1985     if (as->parms[5].items) {
1986         if (!IsNumeric(as->parms[5].items->data)) {
1987             fprintf(STDERR, "vos: Given RO volume ID %s should be numeric.\n",
1988                     as->parms[5].items->data);
1989             return EINVAL;
1990         }
1991
1992         code = util_GetUInt32(as->parms[5].items->data, &rovolid);
1993         if (code) {
1994             fprintf(STDERR, "vos: bad integer specified for volume ID.\n");
1995             return code;
1996         }
1997
1998         if (rovolid == 0) {
1999             arovolid = NULL;
2000         }
2001     }
2002
2003     code =
2004         UV_CreateVolume3(tserver, pnum, as->parms[2].items->data, quota, 0,
2005                          0, 0, 0, &volid, arovolid, &bkvolid);
2006     if (code) {
2007         PrintDiagnostics("create", code);
2008         return code;
2009     }
2010     MapPartIdIntoName(pnum, part);
2011     fprintf(STDOUT, "Volume %lu created on partition %s of %s\n",
2012             (unsigned long)volid, part, as->parms[0].items->data);
2013
2014     return 0;
2015 }
2016
2017 #if 0
2018 static afs_int32
2019 DeleteAll(struct nvldbentry *entry)
2020 {
2021     int i;
2022     afs_int32 error, code, curserver, curpart;
2023     afs_uint32 volid;
2024
2025     MapHostToNetwork(entry);
2026     error = 0;
2027     for (i = 0; i < entry->nServers; i++) {
2028         curserver = entry->serverNumber[i];
2029         curpart = entry->serverPartition[i];
2030         if (entry->serverFlags[i] & ITSROVOL) {
2031             volid = entry->volumeId[ROVOL];
2032         } else {
2033             volid = entry->volumeId[RWVOL];
2034         }
2035         code = UV_DeleteVolume(curserver, curpart, volid);
2036         if (code && !error)
2037             error = code;
2038     }
2039     return error;
2040 }
2041 #endif
2042
2043 static int
2044 DeleteVolume(struct cmd_syndesc *as, void *arock)
2045 {
2046     afs_int32 err, code = 0;
2047     afs_uint32 server = 0;
2048     afs_int32 partition = -1;
2049     afs_uint32 volid;
2050     char pname[10];
2051     afs_int32 idx, j;
2052
2053     if (as->parms[0].items) {
2054         server = GetServer(as->parms[0].items->data);
2055         if (!server) {
2056             fprintf(STDERR, "vos: server '%s' not found in host table\n",
2057                     as->parms[0].items->data);
2058             return ENOENT;
2059         }
2060     }
2061
2062     if (as->parms[1].items) {
2063         partition = volutil_GetPartitionID(as->parms[1].items->data);
2064         if (partition < 0) {
2065             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2066                     as->parms[1].items->data);
2067             return EINVAL;
2068         }
2069
2070         /* Check for validity of the partition */
2071         if (!IsPartValid(partition, server, &code)) {
2072             if (code) {
2073                 PrintError("", code);
2074             } else {
2075                 fprintf(STDERR,
2076                         "vos : partition %s does not exist on the server\n",
2077                         as->parms[1].items->data);
2078             }
2079             return ENOENT;
2080         }
2081     }
2082
2083     volid = vsu_GetVolumeID(as->parms[2].items->data, cstruct, &err);
2084     if (volid == 0) {
2085         fprintf(STDERR, "Can't find volume name '%s' in VLDB\n",
2086                 as->parms[2].items->data);
2087         if (err)
2088             PrintError("", err);
2089         return ENOENT;
2090     }
2091
2092     /* If the server or partition option are not complete, try to fill
2093      * them in from the VLDB entry.
2094      */
2095     if ((partition == -1) || !server) {
2096         struct nvldbentry entry;
2097
2098         code = VLDB_GetEntryByID(volid, -1, &entry);
2099         if (code) {
2100             fprintf(STDERR,
2101                     "Could not fetch the entry for volume %lu from VLDB\n",
2102                     (unsigned long)volid);
2103             PrintError("", code);
2104             return (code);
2105         }
2106
2107         if (((volid == entry.volumeId[RWVOL]) && (entry.flags & RW_EXISTS))
2108             || ((volid == entry.volumeId[BACKVOL])
2109                 && (entry.flags & BACK_EXISTS))) {
2110             idx = Lp_GetRwIndex(&entry);
2111             if ((idx == -1) || (server && (server != entry.serverNumber[idx]))
2112                 || ((partition != -1)
2113                     && (partition != entry.serverPartition[idx]))) {
2114                 fprintf(STDERR, "VLDB: Volume '%s' no match\n",
2115                         as->parms[2].items->data);
2116                 return ENOENT;
2117             }
2118         } else if ((volid == entry.volumeId[ROVOL])
2119                    && (entry.flags & RO_EXISTS)) {
2120             for (idx = -1, j = 0; j < entry.nServers; j++) {
2121                 if (entry.serverFlags[j] != ITSROVOL)
2122                     continue;
2123
2124                 if (((server == 0) || (server == entry.serverNumber[j]))
2125                     && ((partition == -1)
2126                         || (partition == entry.serverPartition[j]))) {
2127                     if (idx != -1) {
2128                         fprintf(STDERR,
2129                                 "VLDB: Volume '%s' matches more than one RO\n",
2130                                 as->parms[2].items->data);
2131                         return ENOENT;
2132                     }
2133                     idx = j;
2134                 }
2135             }
2136             if (idx == -1) {
2137                 fprintf(STDERR, "VLDB: Volume '%s' no match\n",
2138                         as->parms[2].items->data);
2139                 return ENOENT;
2140             }
2141         } else {
2142             fprintf(STDERR, "VLDB: Volume '%s' no match\n",
2143                     as->parms[2].items->data);
2144             return ENOENT;
2145         }
2146
2147         server = htonl(entry.serverNumber[idx]);
2148         partition = entry.serverPartition[idx];
2149     }
2150
2151
2152     code = UV_DeleteVolume(server, partition, volid);
2153     if (code) {
2154         PrintDiagnostics("remove", code);
2155         return code;
2156     }
2157
2158     MapPartIdIntoName(partition, pname);
2159     fprintf(STDOUT, "Volume %lu on partition %s server %s deleted\n",
2160             (unsigned long)volid, pname, hostutil_GetNameByINet(server));
2161     return 0;
2162 }
2163
2164 #define TESTM   0               /* set for move space tests, clear for production */
2165 static int
2166 MoveVolume(struct cmd_syndesc *as, void *arock)
2167 {
2168
2169     afs_uint32 volid;
2170     afs_uint32 fromserver, toserver;
2171     afs_int32 frompart, topart;
2172     afs_int32 flags, code, err;
2173     char fromPartName[10], toPartName[10];
2174
2175     struct diskPartition64 partition;   /* for space check */
2176     volintInfo *p;
2177
2178     volid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
2179     if (volid == 0) {
2180         if (err)
2181             PrintError("", err);
2182         else
2183             fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2184                     as->parms[0].items->data);
2185         return ENOENT;
2186     }
2187     fromserver = GetServer(as->parms[1].items->data);
2188     if (fromserver == 0) {
2189         fprintf(STDERR, "vos: server '%s' not found in host table\n",
2190                 as->parms[1].items->data);
2191         return ENOENT;
2192     }
2193     toserver = GetServer(as->parms[3].items->data);
2194     if (toserver == 0) {
2195         fprintf(STDERR, "vos: server '%s' not found in host table\n",
2196                 as->parms[3].items->data);
2197         return ENOENT;
2198     }
2199     frompart = volutil_GetPartitionID(as->parms[2].items->data);
2200     if (frompart < 0) {
2201         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2202                 as->parms[2].items->data);
2203         return EINVAL;
2204     }
2205     if (!IsPartValid(frompart, fromserver, &code)) {    /*check for validity of the partition */
2206         if (code)
2207             PrintError("", code);
2208         else
2209             fprintf(STDERR,
2210                     "vos : partition %s does not exist on the server\n",
2211                     as->parms[2].items->data);
2212         return ENOENT;
2213     }
2214     topart = volutil_GetPartitionID(as->parms[4].items->data);
2215     if (topart < 0) {
2216         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2217                 as->parms[4].items->data);
2218         return EINVAL;
2219     }
2220     if (!IsPartValid(topart, toserver, &code)) {        /*check for validity of the partition */
2221         if (code)
2222             PrintError("", code);
2223         else
2224             fprintf(STDERR,
2225                     "vos : partition %s does not exist on the server\n",
2226                     as->parms[4].items->data);
2227         return ENOENT;
2228     }
2229
2230     flags = 0;
2231     if (as->parms[5].items) flags |= RV_NOCLONE;
2232
2233     /*
2234      * check source partition for space to clone volume
2235      */
2236
2237     MapPartIdIntoName(topart, toPartName);
2238     MapPartIdIntoName(frompart, fromPartName);
2239
2240     /*
2241      * check target partition for space to move volume
2242      */
2243
2244     code = UV_PartitionInfo64(toserver, toPartName, &partition);
2245     if (code) {
2246         fprintf(STDERR, "vos: cannot access partition %s\n", toPartName);
2247         exit(1);
2248     }
2249     if (TESTM)
2250         fprintf(STDOUT, "target partition %s free space %" AFS_INT64_FMT "\n", toPartName,
2251                 partition.free);
2252
2253     p = (volintInfo *) 0;
2254     code = UV_ListOneVolume(fromserver, frompart, volid, &p);
2255     if (code) {
2256         fprintf(STDERR, "vos:cannot access volume %lu\n",
2257                 (unsigned long)volid);
2258         exit(1);
2259     }
2260     if (TESTM)
2261         fprintf(STDOUT, "volume %lu size %d\n", (unsigned long)volid,
2262                 p->size);
2263     if (partition.free <= p->size) {
2264         fprintf(STDERR,
2265                 "vos: no space on target partition %s to move volume %lu\n",
2266                 toPartName, (unsigned long)volid);
2267         free(p);
2268         exit(1);
2269     }
2270     free(p);
2271
2272     if (TESTM) {
2273         fprintf(STDOUT, "size test - don't do move\n");
2274         exit(0);
2275     }
2276
2277     /* successful move still not guaranteed but shoot for it */
2278
2279     code =
2280         UV_MoveVolume2(volid, fromserver, frompart, toserver, topart, flags);
2281     if (code) {
2282         PrintDiagnostics("move", code);
2283         return code;
2284     }
2285     MapPartIdIntoName(topart, toPartName);
2286     MapPartIdIntoName(frompart, fromPartName);
2287     fprintf(STDOUT, "Volume %lu moved from %s %s to %s %s \n",
2288             (unsigned long)volid, as->parms[1].items->data, fromPartName,
2289             as->parms[3].items->data, toPartName);
2290
2291     return 0;
2292 }
2293
2294 static int
2295 CopyVolume(struct cmd_syndesc *as, void *arock)
2296 {
2297     afs_uint32 volid;
2298     afs_uint32 fromserver, toserver;
2299     afs_int32 frompart, topart, code, err, flags;
2300     char fromPartName[10], toPartName[10], *tovolume;
2301     struct nvldbentry entry;
2302     struct diskPartition64 partition;   /* for space check */
2303     volintInfo *p;
2304
2305     volid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
2306     if (volid == 0) {
2307         if (err)
2308             PrintError("", err);
2309         else
2310             fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2311                     as->parms[0].items->data);
2312         return ENOENT;
2313     }
2314     fromserver = GetServer(as->parms[1].items->data);
2315     if (fromserver == 0) {
2316         fprintf(STDERR, "vos: server '%s' not found in host table\n",
2317                 as->parms[1].items->data);
2318         return ENOENT;
2319     }
2320
2321     toserver = GetServer(as->parms[4].items->data);
2322     if (toserver == 0) {
2323         fprintf(STDERR, "vos: server '%s' not found in host table\n",
2324                 as->parms[4].items->data);
2325         return ENOENT;
2326     }
2327
2328     tovolume = as->parms[3].items->data;
2329     if (!ISNAMEVALID(tovolume)) {
2330         fprintf(STDERR,
2331                 "vos: the name of the root volume %s exceeds the size limit of %d\n",
2332                 tovolume, VOLSER_OLDMAXVOLNAME - 10);
2333         return E2BIG;
2334     }
2335     if (!VolNameOK(tovolume)) {
2336         fprintf(STDERR,
2337                 "Illegal volume name %s, should not end in .readonly or .backup\n",
2338                 tovolume);
2339         return EINVAL;
2340     }
2341     if (IsNumeric(tovolume)) {
2342         fprintf(STDERR, "Illegal volume name %s, should not be a number\n",
2343                 tovolume);
2344         return EINVAL;
2345     }
2346     code = VLDB_GetEntryByName(tovolume, &entry);
2347     if (!code) {
2348         fprintf(STDERR, "Volume %s already exists\n", tovolume);
2349         PrintDiagnostics("copy", code);
2350         return EEXIST;
2351     }
2352
2353     frompart = volutil_GetPartitionID(as->parms[2].items->data);
2354     if (frompart < 0) {
2355         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2356                 as->parms[2].items->data);
2357         return EINVAL;
2358     }
2359     if (!IsPartValid(frompart, fromserver, &code)) {    /*check for validity of the partition */
2360         if (code)
2361             PrintError("", code);
2362         else
2363             fprintf(STDERR,
2364                     "vos : partition %s does not exist on the server\n",
2365                     as->parms[2].items->data);
2366         return ENOENT;
2367     }
2368
2369     topart = volutil_GetPartitionID(as->parms[5].items->data);
2370     if (topart < 0) {
2371         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2372                 as->parms[5].items->data);
2373         return EINVAL;
2374     }
2375     if (!IsPartValid(topart, toserver, &code)) {        /*check for validity of the partition */
2376         if (code)
2377             PrintError("", code);
2378         else
2379             fprintf(STDERR,
2380                     "vos : partition %s does not exist on the server\n",
2381                     as->parms[5].items->data);
2382         return ENOENT;
2383     }
2384
2385     flags = 0;
2386     if (as->parms[6].items) flags |= RV_OFFLINE;
2387     if (as->parms[7].items) flags |= RV_RDONLY;
2388     if (as->parms[8].items) flags |= RV_NOCLONE;
2389
2390     MapPartIdIntoName(topart, toPartName);
2391     MapPartIdIntoName(frompart, fromPartName);
2392
2393     /*
2394      * check target partition for space to move volume
2395      */
2396
2397     code = UV_PartitionInfo64(toserver, toPartName, &partition);
2398     if (code) {
2399         fprintf(STDERR, "vos: cannot access partition %s\n", toPartName);
2400         exit(1);
2401     }
2402     if (TESTM)
2403         fprintf(STDOUT, "target partition %s free space %" AFS_INT64_FMT "\n", toPartName,
2404                 partition.free);
2405
2406     p = (volintInfo *) 0;
2407     code = UV_ListOneVolume(fromserver, frompart, volid, &p);
2408     if (code) {
2409         fprintf(STDERR, "vos:cannot access volume %lu\n",
2410                 (unsigned long)volid);
2411         exit(1);
2412     }
2413
2414     if (partition.free <= p->size) {
2415         fprintf(STDERR,
2416                 "vos: no space on target partition %s to copy volume %lu\n",
2417                 toPartName, (unsigned long)volid);
2418         free(p);
2419         exit(1);
2420     }
2421     free(p);
2422
2423     /* successful copy still not guaranteed but shoot for it */
2424
2425     code =
2426         UV_CopyVolume2(volid, fromserver, frompart, tovolume, toserver,
2427                        topart, 0, flags);
2428     if (code) {
2429         PrintDiagnostics("copy", code);
2430         return code;
2431     }
2432     MapPartIdIntoName(topart, toPartName);
2433     MapPartIdIntoName(frompart, fromPartName);
2434     fprintf(STDOUT, "Volume %lu copied from %s %s to %s on %s %s \n",
2435             (unsigned long)volid, as->parms[1].items->data, fromPartName,
2436             tovolume, as->parms[4].items->data, toPartName);
2437
2438     return 0;
2439 }
2440
2441
2442 static int
2443 ShadowVolume(struct cmd_syndesc *as, void *arock)
2444 {
2445     afs_uint32 volid, tovolid;
2446     afs_uint32 fromserver, toserver;
2447     afs_int32 frompart, topart;
2448     afs_int32 code, err, flags;
2449     char fromPartName[10], toPartName[10], toVolName[32], *tovolume;
2450     struct diskPartition64 partition;   /* for space check */
2451     volintInfo *p, *q;
2452
2453     p = (volintInfo *) 0;
2454     q = (volintInfo *) 0;
2455
2456     volid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
2457     if (volid == 0) {
2458         if (err)
2459             PrintError("", err);
2460         else
2461             fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2462                     as->parms[0].items->data);
2463         return ENOENT;
2464     }
2465     fromserver = GetServer(as->parms[1].items->data);
2466     if (fromserver == 0) {
2467         fprintf(STDERR, "vos: server '%s' not found in host table\n",
2468                 as->parms[1].items->data);
2469         return ENOENT;
2470     }
2471
2472     toserver = GetServer(as->parms[3].items->data);
2473     if (toserver == 0) {
2474         fprintf(STDERR, "vos: server '%s' not found in host table\n",
2475                 as->parms[3].items->data);
2476         return ENOENT;
2477     }
2478
2479     frompart = volutil_GetPartitionID(as->parms[2].items->data);
2480     if (frompart < 0) {
2481         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2482                 as->parms[2].items->data);
2483         return EINVAL;
2484     }
2485     if (!IsPartValid(frompart, fromserver, &code)) {    /*check for validity of the partition */
2486         if (code)
2487             PrintError("", code);
2488         else
2489             fprintf(STDERR,
2490                     "vos : partition %s does not exist on the server\n",
2491                     as->parms[2].items->data);
2492         return ENOENT;
2493     }
2494
2495     topart = volutil_GetPartitionID(as->parms[4].items->data);
2496     if (topart < 0) {
2497         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2498                 as->parms[4].items->data);
2499         return EINVAL;
2500     }
2501     if (!IsPartValid(topart, toserver, &code)) {        /*check for validity of the partition */
2502         if (code)
2503             PrintError("", code);
2504         else
2505             fprintf(STDERR,
2506                     "vos : partition %s does not exist on the server\n",
2507                     as->parms[4].items->data);
2508         return ENOENT;
2509     }
2510
2511     if (as->parms[5].items) {
2512         tovolume = as->parms[5].items->data;
2513         if (!ISNAMEVALID(tovolume)) {
2514             fprintf(STDERR,
2515                 "vos: the name of the root volume %s exceeds the size limit of %d\n",
2516                 tovolume, VOLSER_OLDMAXVOLNAME - 10);
2517             return E2BIG;
2518         }
2519         if (!VolNameOK(tovolume)) {
2520             fprintf(STDERR,
2521                 "Illegal volume name %s, should not end in .readonly or .backup\n",
2522                 tovolume);
2523             return EINVAL;
2524         }
2525         if (IsNumeric(tovolume)) {
2526             fprintf(STDERR,
2527                 "Illegal volume name %s, should not be a number\n",
2528                 tovolume);
2529             return EINVAL;
2530         }
2531     } else {
2532         /* use actual name of source volume */
2533         code = UV_ListOneVolume(fromserver, frompart, volid, &p);
2534         if (code) {
2535             fprintf(STDERR, "vos:cannot access volume %lu\n",
2536                 (unsigned long)volid);
2537             exit(1);
2538         }
2539         strcpy(toVolName, p->name);
2540         tovolume = toVolName;
2541         /* save p for size checks later */
2542     }
2543
2544     if (as->parms[6].items) {
2545         tovolid = vsu_GetVolumeID(as->parms[6].items->data, cstruct, &err);
2546         if (tovolid == 0) {
2547             if (err)
2548                 PrintError("", err);
2549             else
2550                 fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2551                         as->parms[6].items->data);
2552             if (p)
2553                 free(p);
2554             return ENOENT;
2555         }
2556     } else {
2557         tovolid = vsu_GetVolumeID(tovolume, cstruct, &err);
2558         if (tovolid == 0) {
2559             if (err)
2560                 PrintError("", err);
2561             else
2562                 fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2563                         tovolume);
2564             if (p)
2565                 free(p);
2566             return ENOENT;
2567         }
2568     }
2569
2570     flags = RV_NOVLDB;
2571     if (as->parms[7].items) flags |= RV_OFFLINE;
2572     if (as->parms[8].items) flags |= RV_RDONLY;
2573     if (as->parms[9].items) flags |= RV_NOCLONE;
2574     if (as->parms[10].items) flags |= RV_CPINCR;
2575
2576     MapPartIdIntoName(topart, toPartName);
2577     MapPartIdIntoName(frompart, fromPartName);
2578
2579     /*
2580      * check target partition for space to move volume
2581      */
2582
2583     code = UV_PartitionInfo64(toserver, toPartName, &partition);
2584     if (code) {
2585         fprintf(STDERR, "vos: cannot access partition %s\n", toPartName);
2586         exit(1);
2587     }
2588     if (TESTM)
2589         fprintf(STDOUT, "target partition %s free space %" AFS_INT64_FMT "\n", toPartName,
2590                 partition.free);
2591
2592     /* Don't do this again if we did it above */
2593     if (!p) {
2594         code = UV_ListOneVolume(fromserver, frompart, volid, &p);
2595         if (code) {
2596             fprintf(STDERR, "vos:cannot access volume %lu\n",
2597                 (unsigned long)volid);
2598             exit(1);
2599         }
2600     }
2601
2602     /* OK if this fails */
2603     code = UV_ListOneVolume(toserver, topart, tovolid, &q);
2604
2605     /* Treat existing volume size as "free" */
2606     if (q)
2607         p->size = (q->size < p->size) ? p->size - q->size : 0;
2608
2609     if (partition.free <= p->size) {
2610         fprintf(STDERR,
2611                 "vos: no space on target partition %s to copy volume %lu\n",
2612                 toPartName, (unsigned long)volid);
2613         free(p);
2614         if (q) free(q);
2615         exit(1);
2616     }
2617     free(p);
2618     if (q) free(q);
2619
2620     /* successful copy still not guaranteed but shoot for it */
2621
2622     code =
2623         UV_CopyVolume2(volid, fromserver, frompart, tovolume, toserver,
2624                        topart, tovolid, flags);
2625     if (code) {
2626         PrintDiagnostics("shadow", code);
2627         return code;
2628     }
2629     MapPartIdIntoName(topart, toPartName);
2630     MapPartIdIntoName(frompart, fromPartName);
2631     fprintf(STDOUT, "Volume %lu shadowed from %s %s to %s %s \n",
2632             (unsigned long)volid, as->parms[1].items->data, fromPartName,
2633             as->parms[3].items->data, toPartName);
2634
2635     return 0;
2636 }
2637
2638
2639 static int
2640 CloneVolume(struct cmd_syndesc *as, void *arock)
2641 {
2642     afs_uint32 volid, cloneid;
2643     afs_uint32 server;
2644     afs_int32 part, voltype;
2645     char partName[10], *volname;
2646     afs_int32 code, err, flags;
2647     struct nvldbentry entry;
2648
2649     volid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
2650     if (volid == 0) {
2651         if (err)
2652             PrintError("", err);
2653         else
2654             fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2655                     as->parms[0].items->data);
2656         return ENOENT;
2657     }
2658
2659     if (as->parms[1].items || as->parms[2].items) {
2660         if (!as->parms[1].items || !as->parms[2].items) {
2661             fprintf(STDERR,
2662                     "Must specify both -server and -partition options\n");
2663             return -1;
2664         }
2665         server = GetServer(as->parms[1].items->data);
2666         if (server == 0) {
2667             fprintf(STDERR, "vos: server '%s' not found in host table\n",
2668                     as->parms[1].items->data);
2669             return ENOENT;
2670         }
2671         part = volutil_GetPartitionID(as->parms[2].items->data);
2672         if (part < 0) {
2673             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2674                     as->parms[2].items->data);
2675             return EINVAL;
2676         }
2677         if (!IsPartValid(part, server, &code)) {        /*check for validity of the partition */
2678             if (code)
2679                 PrintError("", code);
2680             else
2681                 fprintf(STDERR,
2682                     "vos : partition %s does not exist on the server\n",
2683                     as->parms[2].items->data);
2684             return ENOENT;
2685         }
2686     } else {
2687         code = GetVolumeInfo(volid, &server, &part, &voltype, &entry);
2688         if (code)
2689             return code;
2690     }
2691
2692     volname = 0;
2693     if (as->parms[3].items) {
2694         volname = as->parms[3].items->data;
2695         if (strlen(volname) > VOLSER_OLDMAXVOLNAME - 1) {
2696             fprintf(STDERR,
2697                 "vos: the name of the root volume %s exceeds the size limit of %d\n",
2698                 volname, VOLSER_OLDMAXVOLNAME - 1);
2699             return E2BIG;
2700         }
2701 #if 0
2702         /*
2703          * In order that you be able to make clones of RO or BK, this
2704          * check must be omitted.
2705          */
2706         if (!VolNameOK(volname)) {
2707             fprintf(STDERR,
2708                 "Illegal volume name %s, should not end in .readonly or .backup\n",
2709                 volname);
2710             return EINVAL;
2711         }
2712 #endif
2713         if (IsNumeric(volname)) {
2714             fprintf(STDERR,
2715                 "Illegal volume name %s, should not be a number\n",
2716                 volname);
2717             return EINVAL;
2718         }
2719     }
2720
2721     cloneid = 0;
2722     if (as->parms[4].items) {
2723         cloneid = vsu_GetVolumeID(as->parms[4].items->data, cstruct, &err);
2724         if (cloneid == 0) {
2725             if (err)
2726                 PrintError("", err);
2727             else
2728                 fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2729                         as->parms[4].items->data);
2730             return ENOENT;
2731         }
2732     }
2733
2734     flags = 0;
2735     if (as->parms[5].items) flags |= RV_OFFLINE;
2736     if (as->parms[6].items && as->parms[7].items) {
2737         fprintf(STDERR, "vos: cannot specify that a volume be -readwrite and -readonly\n");
2738         return EINVAL;
2739     }
2740     if (as->parms[6].items) flags |= RV_RDONLY;
2741     if (as->parms[7].items) flags |= RV_RWONLY;
2742
2743
2744     code =
2745         UV_CloneVolume(server, part, volid, cloneid, volname, flags);
2746
2747     if (code) {
2748         PrintDiagnostics("clone", code);
2749         return code;
2750     }
2751     MapPartIdIntoName(part, partName);
2752     fprintf(STDOUT, "Created clone for volume %s\n",
2753             as->parms[0].items->data);
2754
2755     return 0;
2756 }
2757
2758
2759 static int
2760 BackupVolume(struct cmd_syndesc *as, void *arock)
2761 {
2762     afs_uint32 avolid;
2763     afs_uint32 aserver;
2764     afs_int32 apart, vtype, code, err;
2765     struct nvldbentry entry;
2766
2767     afs_uint32 buvolid;
2768     afs_uint32 buserver;
2769     afs_int32 bupart, butype;
2770     struct nvldbentry buentry;
2771
2772     avolid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
2773     if (avolid == 0) {
2774         if (err)
2775             PrintError("", err);
2776         else
2777             fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2778                     as->parms[0].items->data);
2779         return ENOENT;
2780     }
2781     code = GetVolumeInfo(avolid, &aserver, &apart, &vtype, &entry);
2782     if (code)
2783         exit(1);
2784
2785     /* verify this is a readwrite volume */
2786
2787     if (vtype != RWVOL) {
2788         fprintf(STDERR, "%s not RW volume\n", as->parms[0].items->data);
2789         exit(1);
2790     }
2791
2792     /* is there a backup volume already? */
2793
2794     if (entry.flags & BACK_EXISTS) {
2795         /* yep, where is it? */
2796
2797         buvolid = entry.volumeId[BACKVOL];
2798         code = GetVolumeInfo(buvolid, &buserver, &bupart, &butype, &buentry);
2799         if (code)
2800             exit(1);
2801
2802         /* is it local? */
2803         code = VLDB_IsSameAddrs(buserver, aserver, &err);
2804         if (err) {
2805             fprintf(STDERR,
2806                     "Failed to get info about server's %d address(es) from vlserver; aborting call!\n",
2807                     buserver);
2808             exit(1);
2809         }
2810         if (!code) {
2811             fprintf(STDERR,
2812                     "FATAL ERROR: backup volume %lu exists on server %lu\n",
2813                     (unsigned long)buvolid, (unsigned long)buserver);
2814             exit(1);
2815         }
2816     }
2817
2818     /* nope, carry on */
2819
2820     code = UV_BackupVolume(aserver, apart, avolid);
2821
2822     if (code) {
2823         PrintDiagnostics("backup", code);
2824         return code;
2825     }
2826     fprintf(STDOUT, "Created backup volume for %s \n",
2827             as->parms[0].items->data);
2828     return 0;
2829 }
2830
2831 static int
2832 ReleaseVolume(struct cmd_syndesc *as, void *arock)
2833 {
2834
2835     struct nvldbentry entry;
2836     afs_uint32 avolid;
2837     afs_uint32 aserver;
2838     afs_int32 apart, vtype, code, err;
2839     int force = 0;
2840
2841     if (as->parms[1].items)
2842         force = 1;
2843     avolid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
2844     if (avolid == 0) {
2845         if (err)
2846             PrintError("", err);
2847         else
2848             fprintf(STDERR, "vos: can't find volume '%s'\n",
2849                     as->parms[0].items->data);
2850         return ENOENT;
2851     }
2852     code = GetVolumeInfo(avolid, &aserver, &apart, &vtype, &entry);
2853     if (code)
2854         return code;
2855
2856     if (vtype != RWVOL) {
2857         fprintf(STDERR, "%s not a RW volume\n", as->parms[0].items->data);
2858         return (ENOENT);
2859     }
2860
2861     if (!ISNAMEVALID(entry.name)) {
2862         fprintf(STDERR,
2863                 "Volume name %s is too long, rename before releasing\n",
2864                 entry.name);
2865         return E2BIG;
2866     }
2867
2868     code = UV_ReleaseVolume(avolid, aserver, apart, force);
2869     if (code) {
2870         PrintDiagnostics("release", code);
2871         return code;
2872     }
2873     fprintf(STDOUT, "Released volume %s successfully\n",
2874             as->parms[0].items->data);
2875     return 0;
2876 }
2877
2878 static int
2879 DumpVolumeCmd(struct cmd_syndesc *as, void *arock)
2880 {
2881     afs_uint32 avolid;
2882     afs_uint32 aserver;
2883     afs_int32 apart, voltype, fromdate = 0, code, err, i, flags;
2884     char filename[MAXPATHLEN];
2885     struct nvldbentry entry;
2886
2887     rx_SetRxDeadTime(60 * 10);
2888     for (i = 0; i < MAXSERVERS; i++) {
2889         struct rx_connection *rxConn = ubik_GetRPCConn(cstruct, i);
2890         if (rxConn == 0)
2891             break;
2892         rx_SetConnDeadTime(rxConn, rx_connDeadTime);
2893         if (rxConn->service)
2894             rxConn->service->connDeadTime = rx_connDeadTime;
2895     }
2896
2897     avolid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
2898     if (avolid == 0) {
2899         if (err)
2900             PrintError("", err);
2901         else
2902             fprintf(STDERR, "vos: can't find volume '%s'\n",
2903                     as->parms[0].items->data);
2904         return ENOENT;
2905     }
2906
2907     if (as->parms[3].items || as->parms[4].items) {
2908         if (!as->parms[3].items || !as->parms[4].items) {
2909             fprintf(STDERR,
2910                     "Must specify both -server and -partition options\n");
2911             return -1;
2912         }
2913         aserver = GetServer(as->parms[3].items->data);
2914         if (aserver == 0) {
2915             fprintf(STDERR, "Invalid server name\n");
2916             return -1;
2917         }
2918         apart = volutil_GetPartitionID(as->parms[4].items->data);
2919         if (apart < 0) {
2920             fprintf(STDERR, "Invalid partition name\n");
2921             return -1;
2922         }
2923     } else {
2924         code = GetVolumeInfo(avolid, &aserver, &apart, &voltype, &entry);
2925         if (code)
2926             return code;
2927     }
2928
2929     if (as->parms[1].items && strcmp(as->parms[1].items->data, "0")) {
2930         code = ktime_DateToInt32(as->parms[1].items->data, &fromdate);
2931         if (code) {
2932             fprintf(STDERR, "vos: failed to parse date '%s' (error=%d))\n",
2933                     as->parms[1].items->data, code);
2934             return code;
2935         }
2936     }
2937     if (as->parms[2].items) {
2938         strcpy(filename, as->parms[2].items->data);
2939     } else {
2940         strcpy(filename, "");
2941     }
2942
2943     flags = as->parms[6].items ? VOLDUMPV2_OMITDIRS : 0;
2944 retry_dump:
2945     if (as->parms[5].items) {
2946         code =
2947             UV_DumpClonedVolume(avolid, aserver, apart, fromdate,
2948                                 DumpFunction, filename, flags);
2949     } else {
2950         code =
2951             UV_DumpVolume(avolid, aserver, apart, fromdate, DumpFunction,
2952                           filename, flags);
2953     }
2954     if ((code == RXGEN_OPCODE) && (as->parms[6].items)) {
2955         flags &= ~VOLDUMPV2_OMITDIRS;
2956         goto retry_dump;
2957     }
2958     if (code) {
2959         PrintDiagnostics("dump", code);
2960         return code;
2961     }
2962     if (strcmp(filename, ""))
2963         fprintf(STDERR, "Dumped volume %s in file %s\n",
2964                 as->parms[0].items->data, filename);
2965     else
2966         fprintf(STDERR, "Dumped volume %s in stdout \n",
2967                 as->parms[0].items->data);
2968     return 0;
2969 }
2970
2971 #define ASK   0
2972 #define ABORT 1
2973 #define FULL  2
2974 #define INC   3
2975
2976 #define TS_DUMP 1
2977 #define TS_KEEP 2
2978 #define TS_NEW  3
2979
2980 static int
2981 RestoreVolumeCmd(struct cmd_syndesc *as, void *arock)
2982 {
2983     afs_uint32 avolid, aparentid;
2984     afs_uint32 aserver;
2985     afs_int32 apart, code, vcode, err;
2986     afs_int32 aoverwrite = ASK;
2987     afs_int32 acreation = 0, alastupdate = 0;
2988     int restoreflags = 0;
2989     int readonly = 0, offline = 0, voltype = RWVOL;
2990     char afilename[MAXPATHLEN], avolname[VOLSER_MAXVOLNAME + 1], apartName[10];
2991     char volname[VOLSER_MAXVOLNAME + 1];
2992     struct nvldbentry entry;
2993
2994     aparentid = 0;
2995     if (as->parms[4].items) {
2996         avolid = vsu_GetVolumeID(as->parms[4].items->data, cstruct, &err);
2997         if (avolid == 0) {
2998             if (err)
2999                 PrintError("", err);
3000             else
3001                 fprintf(STDERR, "vos: can't find volume '%s'\n",
3002                         as->parms[4].items->data);
3003             exit(1);
3004         }
3005     } else
3006         avolid = 0;
3007
3008     if (as->parms[5].items) {
3009         if ((strcmp(as->parms[5].items->data, "a") == 0)
3010             || (strcmp(as->parms[5].items->data, "abort") == 0)) {
3011             aoverwrite = ABORT;
3012         } else if ((strcmp(as->parms[5].items->data, "f") == 0)
3013                    || (strcmp(as->parms[5].items->data, "full") == 0)) {
3014             aoverwrite = FULL;
3015         } else if ((strcmp(as->parms[5].items->data, "i") == 0)
3016                    || (strcmp(as->parms[5].items->data, "inc") == 0)
3017                    || (strcmp(as->parms[5].items->data, "increment") == 0)
3018                    || (strcmp(as->parms[5].items->data, "incremental") == 0)) {
3019             aoverwrite = INC;
3020         } else {
3021             fprintf(STDERR, "vos: %s is not a valid argument to -overwrite\n",
3022                     as->parms[5].items->data);
3023             exit(1);
3024         }
3025     }
3026     if (as->parms[6].items)
3027         offline = 1;
3028     if (as->parms[7].items) {
3029         readonly = 1;
3030         voltype = ROVOL;
3031     }
3032
3033     if (as->parms[8].items) {
3034         if ((strcmp(as->parms[8].items->data, "d") == 0)
3035             || (strcmp(as->parms[8].items->data, "dump") == 0)) {
3036             acreation = TS_DUMP;
3037         } else if ((strcmp(as->parms[8].items->data, "k") == 0)
3038             || (strcmp(as->parms[8].items->data, "keep") == 0)) {
3039             acreation = TS_KEEP;
3040         } else if ((strcmp(as->parms[8].items->data, "n") == 0)
3041             || (strcmp(as->parms[8].items->data, "new") == 0)) {
3042             acreation = TS_NEW;
3043         } else {
3044             fprintf(STDERR, "vos: %s is not a valid argument to -creation\n",
3045                     as->parms[8].items->data);
3046             exit(1);
3047         }
3048     }
3049
3050     if (as->parms[9].items) {
3051         if ((strcmp(as->parms[9].items->data, "d") == 0)
3052             || (strcmp(as->parms[9].items->data, "dump") == 0)) {
3053             alastupdate = TS_DUMP;
3054         } else if ((strcmp(as->parms[9].items->data, "k") == 0)
3055             || (strcmp(as->parms[9].items->data, "keep") == 0)) {
3056             alastupdate = TS_KEEP;
3057         } else if ((strcmp(as->parms[9].items->data, "n") == 0)
3058             || (strcmp(as->parms[9].items->data, "new") == 0)) {
3059             alastupdate = TS_NEW;
3060         } else {
3061             fprintf(STDERR, "vos: %s is not a valid argument to -lastupdate\n",
3062                     as->parms[9].items->data);
3063             exit(1);
3064         }
3065     }
3066
3067     aserver = GetServer(as->parms[0].items->data);
3068     if (aserver == 0) {
3069         fprintf(STDERR, "vos: server '%s' not found in host table\n",
3070                 as->parms[0].items->data);
3071         exit(1);
3072     }
3073     apart = volutil_GetPartitionID(as->parms[1].items->data);
3074     if (apart < 0) {
3075         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3076                 as->parms[1].items->data);
3077         exit(1);
3078     }
3079     if (!IsPartValid(apart, aserver, &code)) {  /*check for validity of the partition */
3080         if (code)
3081             PrintError("", code);
3082         else
3083             fprintf(STDERR,
3084                     "vos : partition %s does not exist on the server\n",
3085                     as->parms[1].items->data);
3086         exit(1);
3087     }
3088     strcpy(avolname, as->parms[2].items->data);
3089     if (!ISNAMEVALID(avolname)) {
3090         fprintf(STDERR,
3091                 "vos: the name of the volume %s exceeds the size limit\n",
3092                 avolname);
3093         exit(1);
3094     }
3095     if (!VolNameOK(avolname)) {
3096         fprintf(STDERR,
3097                 "Illegal volume name %s, should not end in .readonly or .backup\n",
3098                 avolname);
3099         exit(1);
3100     }
3101     if (as->parms[3].items) {
3102         strcpy(afilename, as->parms[3].items->data);
3103         if (!FileExists(afilename)) {
3104             fprintf(STDERR, "Can't access file %s\n", afilename);
3105             exit(1);
3106         }
3107     } else {
3108         strcpy(afilename, "");
3109     }
3110
3111     /* Check if volume exists or not */
3112
3113     vsu_ExtractName(volname, avolname);
3114     vcode = VLDB_GetEntryByName(volname, &entry);
3115     if (vcode) {                /* no volume - do a full restore */
3116         restoreflags = RV_FULLRST;
3117         if ((aoverwrite == INC) || (aoverwrite == ABORT))
3118             fprintf(STDERR,
3119                     "Volume does not exist; Will perform a full restore\n");
3120     }
3121
3122     else if ((!readonly && Lp_GetRwIndex(&entry) == -1) /* RW volume does not exist - do a full */
3123              ||(readonly && !Lp_ROMatch(0, 0, &entry))) {       /* RO volume does not exist - do a full */
3124         restoreflags = RV_FULLRST;
3125         if ((aoverwrite == INC) || (aoverwrite == ABORT))
3126             fprintf(STDERR,
3127                     "%s Volume does not exist; Will perform a full restore\n",
3128                     readonly ? "RO" : "RW");
3129
3130         if (avolid == 0) {
3131             avolid = entry.volumeId[voltype];
3132         } else if (entry.volumeId[voltype] != 0
3133                    && entry.volumeId[voltype] != avolid) {
3134             avolid = entry.volumeId[voltype];
3135         }
3136         aparentid = entry.volumeId[RWVOL];
3137     }
3138
3139     else {                      /* volume exists - do we do a full incremental or abort */
3140         afs_uint32 Oserver;
3141         afs_int32 Opart, Otype, vol_elsewhere = 0;
3142         struct nvldbentry Oentry;
3143         int c, dc;
3144
3145         if (avolid == 0) {
3146             avolid = entry.volumeId[voltype];
3147         } else if (entry.volumeId[voltype] != 0
3148                    && entry.volumeId[voltype] != avolid) {
3149             avolid = entry.volumeId[voltype];
3150         }
3151         aparentid = entry.volumeId[RWVOL];
3152
3153         /* A file name was specified  - check if volume is on another partition */
3154         vcode = GetVolumeInfo(avolid, &Oserver, &Opart, &Otype, &Oentry);
3155         if (vcode)
3156             exit(1);
3157
3158         vcode = VLDB_IsSameAddrs(Oserver, aserver, &err);
3159         if (err) {
3160             fprintf(STDERR,
3161                     "Failed to get info about server's %d address(es) from vlserver (err=%d); aborting call!\n",
3162                     Oserver, err);
3163             exit(1);
3164         }
3165         if (!vcode || (Opart != apart))
3166             vol_elsewhere = 1;
3167
3168         if (aoverwrite == ASK) {
3169             if (strcmp(afilename, "") == 0) {   /* The file is from standard in */
3170                 fprintf(STDERR,
3171                         "Volume exists and no -overwrite option specified; Aborting restore command\n");
3172                 exit(1);
3173             }
3174
3175             /* Ask what to do */
3176             if (vol_elsewhere) {
3177                 fprintf(STDERR,
3178                         "The volume %s %u already exists on a different server/part\n",
3179                         volname, entry.volumeId[voltype]);
3180                 fprintf(STDERR,
3181                         "Do you want to do a full restore or abort? [fa](a): ");
3182             } else {
3183                 fprintf(STDERR,
3184                         "The volume %s %u already exists in the VLDB\n",
3185                         volname, entry.volumeId[voltype]);
3186                 fprintf(STDERR,
3187                         "Do you want to do a full/incremental restore or abort? [fia](a): ");
3188             }
3189             dc = c = getchar();
3190             while (!(dc == EOF || dc == '\n'))
3191                 dc = getchar(); /* goto end of line */
3192             if ((c == 'f') || (c == 'F'))
3193                 aoverwrite = FULL;
3194             else if ((c == 'i') || (c == 'I'))
3195                 aoverwrite = INC;
3196             else
3197                 aoverwrite = ABORT;
3198         }
3199
3200         if (aoverwrite == ABORT) {
3201             fprintf(STDERR, "Volume exists; Aborting restore command\n");
3202             exit(1);
3203         } else if (aoverwrite == FULL) {
3204             restoreflags = RV_FULLRST;
3205             fprintf(STDERR,
3206                     "Volume exists; Will delete and perform full restore\n");
3207         } else if (aoverwrite == INC) {
3208             restoreflags = 0;
3209             if (vol_elsewhere) {
3210                 fprintf(STDERR,
3211                         "%s volume %lu already exists on a different server/part; not allowed\n",
3212                         readonly ? "RO" : "RW", (unsigned long)avolid);
3213                 exit(1);
3214             }
3215         }
3216     }
3217     if (offline)
3218         restoreflags |= RV_OFFLINE;
3219     if (readonly)
3220         restoreflags |= RV_RDONLY;
3221
3222     switch (acreation) {
3223         case TS_DUMP:
3224             restoreflags |= RV_CRDUMP;
3225             break;
3226         case TS_KEEP:
3227             restoreflags |= RV_CRKEEP;
3228             break;
3229         case TS_NEW:
3230             restoreflags |= RV_CRNEW;
3231             break;
3232         default:
3233             if (aoverwrite == FULL)
3234                 restoreflags |= RV_CRNEW;
3235             else
3236                 restoreflags |= RV_CRKEEP;
3237     }
3238
3239     switch (alastupdate) {
3240         case TS_DUMP:
3241             restoreflags |= RV_LUDUMP;
3242             break;
3243         case TS_KEEP:
3244             restoreflags |= RV_LUKEEP;
3245             break;
3246         case TS_NEW:
3247             restoreflags |= RV_LUNEW;
3248             break;
3249         default:
3250             restoreflags |= RV_LUDUMP;
3251     }
3252     if (as->parms[10].items) {
3253         restoreflags |= RV_NODEL;
3254     }
3255
3256
3257     code =
3258         UV_RestoreVolume2(aserver, apart, avolid, aparentid,
3259                           avolname, restoreflags, WriteData, afilename);
3260     if (code) {
3261         PrintDiagnostics("restore", code);
3262         exit(1);
3263     }
3264     MapPartIdIntoName(apart, apartName);
3265
3266     /*
3267      * patch typo here - originally "parms[1]", should be "parms[0]"
3268      */
3269
3270     fprintf(STDOUT, "Restored volume %s on %s %s\n", avolname,
3271             as->parms[0].items->data, apartName);
3272     return 0;
3273 }
3274
3275 static int
3276 LockReleaseCmd(struct cmd_syndesc *as, void *arock)
3277 {
3278     afs_uint32 avolid;
3279     afs_int32 code, err;
3280
3281     avolid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
3282     if (avolid == 0) {
3283         if (err)
3284             PrintError("", err);
3285         else
3286             fprintf(STDERR, "vos: can't find volume '%s'\n",
3287                     as->parms[0].items->data);
3288         exit(1);
3289     }
3290
3291     code = UV_LockRelease(avolid);
3292     if (code) {
3293         PrintDiagnostics("unlock", code);
3294         exit(1);
3295     }
3296     fprintf(STDOUT, "Released lock on vldb entry for volume %s\n",
3297             as->parms[0].items->data);
3298     return 0;
3299 }
3300
3301 static int
3302 AddSite(struct cmd_syndesc *as, void *arock)
3303 {
3304     afs_uint32 avolid;
3305     afs_uint32 aserver;
3306     afs_int32 apart, code, err, arovolid, valid = 0;
3307     char apartName[10], avolname[VOLSER_MAXVOLNAME + 1];
3308
3309     vsu_ExtractName(avolname, as->parms[2].items->data);;
3310     avolid = vsu_GetVolumeID(avolname, cstruct, &err);
3311     if (avolid == 0) {
3312         if (err)
3313             PrintError("", err);
3314         else
3315             fprintf(STDERR, "vos: can't find volume '%s'\n",
3316                     as->parms[2].items->data);
3317         exit(1);
3318     }
3319     arovolid = 0;
3320     if (as->parms[3].items) {
3321         vsu_ExtractName(avolname, as->parms[3].items->data);
3322         arovolid = vsu_GetVolumeID(avolname, cstruct, &err);
3323         if (!arovolid) {
3324             fprintf(STDERR, "vos: invalid ro volume id '%s'\n",
3325                     as->parms[3].items->data);
3326             exit(1);
3327         }
3328     }
3329     aserver = GetServer(as->parms[0].items->data);
3330     if (aserver == 0) {
3331         fprintf(STDERR, "vos: server '%s' not found in host table\n",
3332                 as->parms[0].items->data);
3333         exit(1);
3334     }
3335     apart = volutil_GetPartitionID(as->parms[1].items->data);
3336     if (apart < 0) {
3337         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3338                 as->parms[1].items->data);
3339         exit(1);
3340     }
3341     if (!IsPartValid(apart, aserver, &code)) {  /*check for validity of the partition */
3342         if (code)
3343             PrintError("", code);
3344         else
3345             fprintf(STDERR,
3346                     "vos : partition %s does not exist on the server\n",
3347                     as->parms[1].items->data);
3348         exit(1);
3349     }
3350     if (as->parms[4].items) {
3351         valid = 1;
3352     }
3353     code = UV_AddSite2(aserver, apart, avolid, arovolid, valid);
3354     if (code) {
3355         PrintDiagnostics("addsite", code);
3356         exit(1);
3357     }
3358     MapPartIdIntoName(apart, apartName);
3359     fprintf(STDOUT, "Added replication site %s %s for volume %s\n",
3360             as->parms[0].items->data, apartName, as->parms[2].items->data);
3361     return 0;
3362 }
3363
3364 static int
3365 RemoveSite(struct cmd_syndesc *as, void *arock)
3366 {
3367
3368     afs_uint32 avolid;
3369     afs_uint32 aserver;
3370     afs_int32 apart, code, err;
3371     char apartName[10], avolname[VOLSER_MAXVOLNAME + 1];
3372
3373     vsu_ExtractName(avolname, as->parms[2].items->data);
3374     avolid = vsu_GetVolumeID(avolname, cstruct, &err);
3375     if (avolid == 0) {
3376         if (err)
3377             PrintError("", err);
3378         else
3379             fprintf(STDERR, "vos: can't find volume '%s'\n",
3380                     as->parms[2].items->data);
3381         exit(1);
3382     }
3383     aserver = GetServer(as->parms[0].items->data);
3384     if (aserver == 0) {
3385         fprintf(STDERR, "vos: server '%s' not found in host table\n",
3386                 as->parms[0].items->data);
3387         exit(1);
3388     }
3389     apart = volutil_GetPartitionID(as->parms[1].items->data);
3390     if (apart < 0) {
3391         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3392                 as->parms[1].items->data);
3393         exit(1);
3394     }
3395 /*
3396  *skip the partition validity check, since it is possible that the partition
3397  *has since been decomissioned.
3398  */
3399 /*
3400         if (!IsPartValid(apart,aserver,&code)){
3401             if(code) PrintError("",code);
3402             else fprintf(STDERR,"vos : partition %s does not exist on the server\n",as->parms[1].items->data);
3403             exit(1);
3404         }
3405 */
3406     code = UV_RemoveSite(aserver, apart, avolid);
3407     if (code) {
3408         PrintDiagnostics("remsite", code);
3409         exit(1);
3410     }
3411     MapPartIdIntoName(apart, apartName);
3412     fprintf(STDOUT, "Removed replication site %s %s for volume %s\n",
3413             as->parms[0].items->data, apartName, as->parms[2].items->data);
3414     return 0;
3415 }
3416
3417 static int
3418 ChangeLocation(struct cmd_syndesc *as, void *arock)
3419 {
3420     afs_uint32 avolid;
3421     afs_uint32 aserver;
3422     afs_int32 apart, code, err;
3423     char apartName[10];
3424
3425     avolid = vsu_GetVolumeID(as->parms[2].items->data, cstruct, &err);
3426     if (avolid == 0) {
3427         if (err)
3428             PrintError("", err);
3429         else
3430             fprintf(STDERR, "vos: can't find volume '%s'\n",
3431                     as->parms[2].items->data);
3432         exit(1);
3433     }
3434     aserver = GetServer(as->parms[0].items->data);
3435     if (aserver == 0) {
3436         fprintf(STDERR, "vos: server '%s' not found in host table\n",
3437                 as->parms[0].items->data);
3438         exit(1);
3439     }
3440     apart = volutil_GetPartitionID(as->parms[1].items->data);
3441     if (apart < 0) {
3442         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3443                 as->parms[1].items->data);
3444         exit(1);
3445     }
3446     if (!IsPartValid(apart, aserver, &code)) {  /*check for validity of the partition */
3447         if (code)
3448             PrintError("", code);
3449         else
3450             fprintf(STDERR,
3451                     "vos : partition %s does not exist on the server\n",
3452                     as->parms[1].items->data);
3453         exit(1);
3454     }
3455     code = UV_ChangeLocation(aserver, apart, avolid);
3456     if (code) {
3457         PrintDiagnostics("changeloc", code);
3458         exit(1);
3459     }
3460     MapPartIdIntoName(apart, apartName);
3461     fprintf(STDOUT, "Changed location to %s %s for volume %s\n",
3462             as->parms[0].items->data, apartName, as->parms[2].items->data);
3463     return 0;
3464 }
3465
3466 static int
3467 ListPartitions(struct cmd_syndesc *as, void *arock)
3468 {
3469     afs_uint32 aserver;
3470     afs_int32 code;
3471     struct partList dummyPartList;
3472     int i;
3473     char pname[10];
3474     int total, cnt;
3475
3476     aserver = GetServer(as->parms[0].items->data);
3477     if (aserver == 0) {
3478         fprintf(STDERR, "vos: server '%s' not found in host table\n",
3479                 as->parms[0].items->data);
3480         exit(1);
3481     }
3482
3483
3484     code = UV_ListPartitions(aserver, &dummyPartList, &cnt);
3485     if (code) {
3486         PrintDiagnostics("listpart", code);
3487         exit(1);
3488     }
3489     total = 0;
3490     fprintf(STDOUT, "The partitions on the server are:\n");
3491     for (i = 0; i < cnt; i++) {
3492         if (dummyPartList.partFlags[i] & PARTVALID) {
3493             memset(pname, 0, sizeof(pname));
3494             MapPartIdIntoName(dummyPartList.partId[i], pname);
3495             fprintf(STDOUT, " %10s ", pname);
3496             total++;
3497             if ((i % 5) == 0 && (i != 0))
3498                 fprintf(STDOUT, "\n");
3499         }
3500     }
3501     fprintf(STDOUT, "\n");
3502     fprintf(STDOUT, "Total: %d\n", total);
3503     return 0;
3504
3505 }
3506
3507 static int
3508 CompareVolName(const void *p1, const void *p2)
3509 {
3510     volintInfo *arg1, *arg2;
3511
3512     arg1 = (volintInfo *) p1;
3513     arg2 = (volintInfo *) p2;
3514     return (strcmp(arg1->name, arg2->name));
3515
3516 }
3517
3518 /*------------------------------------------------------------------------
3519  * PRIVATE XCompareVolName
3520  *
3521  * Description:
3522  *      Comparison routine for volume names coming from an extended
3523  *      volume listing.
3524  *
3525  * Arguments:
3526  *      a_obj1P : Char ptr to first extended vol info object
3527  *      a_obj1P : Char ptr to second extended vol info object
3528  *
3529  * Returns:
3530  *      The value of strcmp() on the volume names within the passed
3531  *      objects (i,e., -1, 0, or 1).
3532  *
3533  * Environment:
3534  *      Passed to qsort() as the designated comparison routine.
3535  *
3536  * Side Effects:
3537  *      As advertised.
3538  *------------------------------------------------------------------------*/
3539
3540 static int
3541 XCompareVolName(const void *a_obj1P, const void *a_obj2P)
3542 {                               /*XCompareVolName */
3543
3544     return (strcmp
3545             (((struct volintXInfo *)(a_obj1P))->name,
3546              ((struct volintXInfo *)(a_obj2P))->name));
3547
3548 }                               /*XCompareVolName */
3549
3550 static int
3551 CompareVolID(const void *p1, const void *p2)
3552 {
3553     volintInfo *arg1, *arg2;
3554
3555     arg1 = (volintInfo *) p1;
3556     arg2 = (volintInfo *) p2;
3557     if (arg1->volid == arg2->volid)
3558         return 0;
3559     if (arg1->volid > arg2->volid)
3560         return 1;
3561     else
3562         return -1;
3563
3564 }
3565
3566 /*------------------------------------------------------------------------
3567  * PRIVATE XCompareVolID
3568  *
3569  * Description:
3570  *      Comparison routine for volume IDs coming from an extended
3571  *      volume listing.
3572  *
3573  * Arguments:
3574  *      a_obj1P : Char ptr to first extended vol info object
3575  *      a_obj1P : Char ptr to second extended vol info object
3576  *
3577  * Returns:
3578  *      The value of strcmp() on the volume names within the passed
3579  *      objects (i,e., -1, 0, or 1).
3580  *
3581  * Environment:
3582  *      Passed to qsort() as the designated comparison routine.
3583  *
3584  * Side Effects:
3585  *      As advertised.
3586  *------------------------------------------------------------------------*/
3587
3588 static int
3589 XCompareVolID(const void *a_obj1P, const void *a_obj2P)
3590 {                               /*XCompareVolID */
3591
3592     afs_int32 id1, id2;         /*Volume IDs we're comparing */
3593
3594     id1 = ((struct volintXInfo *)(a_obj1P))->volid;
3595     id2 = ((struct volintXInfo *)(a_obj2P))->volid;
3596     if (id1 == id2)
3597         return (0);
3598     else if (id1 > id2)
3599         return (1);
3600     else
3601         return (-1);
3602
3603 }                               /*XCompareVolID */
3604
3605 /*------------------------------------------------------------------------
3606  * PRIVATE ListVolumes
3607  *
3608  * Description:
3609  *      Routine used to list volumes, contacting the Volume Server
3610  *      directly, bypassing the VLDB.
3611  *
3612  * Arguments:
3613  *      as : Ptr to parsed command line arguments.
3614  *
3615  * Returns:
3616  *      0                       Successful operation
3617  *
3618  * Environment:
3619  *      Nothing interesting.
3620  *
3621  * Side Effects:
3622  *      As advertised.
3623  *------------------------------------------------------------------------*/
3624
3625 static int
3626 ListVolumes(struct cmd_syndesc *as, void *arock)
3627 {
3628     afs_int32 apart, int32list, fast;
3629     afs_uint32 aserver;
3630     afs_int32 code;
3631     volintInfo *pntr;
3632     volintInfo *oldpntr = NULL;
3633     afs_int32 count;
3634     int i;
3635     char *base;
3636     volintXInfo *xInfoP;
3637     volintXInfo *origxInfoP = NULL; /*Ptr to current/orig extended vol info */
3638     int wantExtendedInfo;       /*Do we want extended vol info? */
3639
3640     char pname[10];
3641     struct partList dummyPartList;
3642     int all;
3643     int quiet, cnt;
3644
3645     apart = -1;
3646     fast = 0;
3647     int32list = 0;
3648
3649     if (as->parms[3].items)
3650         int32list = 1;
3651     if (as->parms[4].items)
3652         quiet = 1;
3653     else
3654         quiet = 0;
3655     if (as->parms[2].items)
3656         fast = 1;
3657     if (fast)
3658         all = 0;
3659     else
3660         all = 1;
3661     if (as->parms[5].items) {
3662         /*
3663          * We can't coexist with the fast flag.
3664          */
3665         if (fast) {
3666             fprintf(STDERR,
3667                     "vos: Can't use the -fast and -extended flags together\n");
3668             exit(1);
3669         }
3670
3671         /*
3672          * We need to turn on ``long'' listings to get the full effect.
3673          */
3674         wantExtendedInfo = 1;
3675         int32list = 1;
3676     } else
3677         wantExtendedInfo = 0;
3678     if (as->parms[1].items) {
3679         apart = volutil_GetPartitionID(as->parms[1].items->data);
3680         if (apart < 0) {
3681             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3682                     as->parms[1].items->data);
3683             exit(1);
3684         }
3685         dummyPartList.partId[0] = apart;
3686         dummyPartList.partFlags[0] = PARTVALID;
3687         cnt = 1;
3688     }
3689     aserver = GetServer(as->parms[0].items->data);
3690     if (aserver == 0) {
3691         fprintf(STDERR, "vos: server '%s' not found in host table\n",
3692                 as->parms[0].items->data);
3693         exit(1);
3694     }
3695
3696     if (apart != -1) {
3697         if (!IsPartValid(apart, aserver, &code)) {      /*check for validity of the partition */
3698             if (code)
3699                 PrintError("", code);
3700             else
3701                 fprintf(STDERR,
3702                         "vos : partition %s does not exist on the server\n",
3703                         as->parms[1].items->data);
3704             exit(1);
3705         }
3706     } else {
3707         code = UV_ListPartitions(aserver, &dummyPartList, &cnt);
3708         if (code) {
3709             PrintDiagnostics("listvol", code);
3710             exit(1);
3711         }
3712     }
3713     for (i = 0; i < cnt; i++) {
3714         if (dummyPartList.partFlags[i] & PARTVALID) {
3715             if (wantExtendedInfo)
3716                 code =
3717                     UV_XListVolumes(aserver, dummyPartList.partId[i], all,
3718                                     &xInfoP, &count);
3719             else
3720                 code =
3721                     UV_ListVolumes(aserver, dummyPartList.partId[i], all,
3722                                    &pntr, &count);
3723             if (code) {
3724                 PrintDiagnostics("listvol", code);
3725                 exit(1);
3726             }
3727             if (wantExtendedInfo) {
3728                 origxInfoP = xInfoP;
3729                 base = (char *)xInfoP;
3730             } else {
3731                 oldpntr = pntr;
3732                 base = (char *)pntr;
3733             }
3734
3735             if (!fast) {
3736                 if (wantExtendedInfo)
3737                     qsort(base, count, sizeof(volintXInfo), XCompareVolName);
3738                 else
3739                     qsort(base, count, sizeof(volintInfo), CompareVolName);
3740             } else {
3741                 if (wantExtendedInfo)
3742                     qsort(base, count, sizeof(volintXInfo), XCompareVolID);
3743                 else
3744                     qsort(base, count, sizeof(volintInfo), CompareVolID);
3745             }
3746             MapPartIdIntoName(dummyPartList.partId[i], pname);
3747             if (!quiet)
3748                 fprintf(STDOUT,
3749                         "Total number of volumes on server %s partition %s: %lu \n",
3750                         as->parms[0].items->data, pname,
3751                         (unsigned long)count);
3752             if (wantExtendedInfo) {
3753                 if (as->parms[6].items)
3754                     XDisplayVolumes2(aserver, dummyPartList.partId[i], origxInfoP,
3755                                 count, int32list, fast, quiet);
3756                 else
3757                     XDisplayVolumes(aserver, dummyPartList.partId[i], origxInfoP,
3758                                 count, int32list, fast, quiet);
3759                 if (xInfoP)
3760                     free(xInfoP);
3761                 xInfoP = (volintXInfo *) 0;
3762             } else {
3763                 if (as->parms[6].items)
3764                     DisplayVolumes2(aserver, dummyPartList.partId[i], oldpntr,
3765                                     count);
3766                 else
3767                     DisplayVolumes(aserver, dummyPartList.partId[i], oldpntr,
3768                                    count, int32list, fast, quiet);
3769                 if (pntr)
3770                     free(pntr);
3771                 pntr = (volintInfo *) 0;
3772             }
3773         }
3774     }
3775     return 0;
3776 }
3777
3778 static int
3779 SyncVldb(struct cmd_syndesc *as, void *arock)
3780 {
3781     afs_int32 pnum = 0, code;   /* part name */
3782     char part[10];
3783     int flags = 0;
3784     char *volname = 0;
3785     afs_uint32 tserver;
3786
3787     tserver = 0;
3788     if (as->parms[0].items) {
3789         tserver = GetServer(as->parms[0].items->data);
3790         if (!tserver) {
3791             fprintf(STDERR, "vos: host '%s' not found in host table\n",
3792                     as->parms[0].items->data);
3793             exit(1);
3794         }
3795     }
3796
3797     if (as->parms[1].items) {
3798         pnum = volutil_GetPartitionID(as->parms[1].items->data);
3799         if (pnum < 0) {
3800             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3801                     as->parms[1].items->data);
3802             exit(1);
3803         }
3804         if (!IsPartValid(pnum, tserver, &code)) {       /*check for validity of the partition */
3805             if (code)
3806                 PrintError("", code);
3807             else
3808                 fprintf(STDERR,
3809                         "vos: partition %s does not exist on the server\n",
3810                         as->parms[1].items->data);
3811             exit(1);
3812         }
3813         flags = 1;
3814
3815         if (!tserver) {
3816             fprintf(STDERR,
3817                     "The -partition option requires a -server option\n");
3818             exit(1);
3819         }
3820     }
3821
3822     if (as->parms[3].items) {
3823         flags |= 2; /* don't update */
3824     }
3825
3826     if (as->parms[2].items) {
3827         /* Synchronize an individual volume */
3828         volname = as->parms[2].items->data;
3829         code = UV_SyncVolume(tserver, pnum, volname, flags);
3830     } else {
3831         if (!tserver) {
3832             fprintf(STDERR,
3833                     "Without a -volume option, the -server option is required\n");
3834             exit(1);
3835         }
3836         code = UV_SyncVldb(tserver, pnum, flags, 0 /*unused */ );
3837     }
3838
3839     if (code) {
3840         PrintDiagnostics("syncvldb", code);
3841         exit(1);
3842     }
3843
3844     /* Print a summary of what we did */
3845     if (volname)
3846         fprintf(STDOUT, "VLDB volume %s synchronized", volname);
3847     else
3848         fprintf(STDOUT, "VLDB synchronized");
3849     if (tserver) {
3850         fprintf(STDOUT, " with state of server %s", as->parms[0].items->data);
3851     }
3852     if (flags & 1) {
3853         MapPartIdIntoName(pnum, part);
3854         fprintf(STDOUT, " partition %s\n", part);
3855     }
3856     fprintf(STDOUT, "\n");
3857
3858     return 0;
3859 }
3860
3861 static int
3862 SyncServer(struct cmd_syndesc *as, void *arock)
3863 {
3864     afs_int32 pnum, code;       /* part name */
3865     char part[10];
3866     afs_uint32 tserver;
3867
3868     int flags = 0;
3869
3870     tserver = GetServer(as->parms[0].items->data);
3871     if (!tserver) {
3872         fprintf(STDERR, "vos: host '%s' not found in host table\n",
3873                 as->parms[0].items->data);
3874         exit(1);
3875     }
3876     if (as->parms[1].items) {
3877         pnum = volutil_GetPartitionID(as->parms[1].items->data);
3878         if (pnum < 0) {
3879             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3880                     as->parms[1].items->data);
3881             exit(1);
3882         }
3883         if (!IsPartValid(pnum, tserver, &code)) {       /*check for validity of the partition */
3884             if (code)
3885                 PrintError("", code);
3886             else
3887                 fprintf(STDERR,
3888                         "vos : partition %s does not exist on the server\n",
3889                         as->parms[1].items->data);
3890             exit(1);
3891         }
3892         flags = 1;
3893     } else {
3894         pnum = -1;
3895     }
3896
3897     if (as->parms[2].items) {
3898         flags |= 2; /* don't update */
3899     }
3900     code = UV_SyncServer(tserver, pnum, flags, 0 /*unused */ );
3901     if (code) {
3902         PrintDiagnostics("syncserv", code);
3903         exit(1);
3904     }
3905     if (flags & 1) {
3906         MapPartIdIntoName(pnum, part);
3907         fprintf(STDOUT, "Server %s partition %s synchronized with VLDB\n",
3908                 as->parms[0].items->data, part);
3909     } else
3910         fprintf(STDOUT, "Server %s synchronized with VLDB\n",
3911                 as->parms[0].items->data);
3912     return 0;
3913
3914 }
3915
3916 static int
3917 VolumeInfoCmd(char *name)
3918 {
3919     struct nvldbentry entry;
3920     afs_int32 vcode;
3921
3922     /* The vlserver will handle names with the .readonly
3923      * and .backup extension as well as volume ids.
3924      */
3925     vcode = VLDB_GetEntryByName(name, &entry);
3926     if (vcode) {
3927         PrintError("", vcode);
3928         exit(1);
3929     }
3930     MapHostToNetwork(&entry);
3931     EnumerateEntry(&entry);
3932
3933     /* Defect #3027: grubby check to handle locked volume.
3934      * If VLOP_ALLOPERS is set, the entry is locked.
3935      * Leave this routine as is, but put in correct check.
3936      */
3937     PrintLocked(entry.flags);
3938
3939     return 0;
3940 }
3941
3942 static int
3943 VolumeZap(struct cmd_syndesc *as, void *arock)
3944 {
3945     struct nvldbentry entry;
3946     afs_uint32 volid, zapbackupid = 0, backupid = 0;
3947     afs_int32 code, server, part, err;
3948
3949     if (as->parms[3].items) {
3950         /* force flag is on, use the other version */
3951         return NukeVolume(as);
3952     }
3953
3954     if (as->parms[4].items) {
3955         zapbackupid = 1;
3956     }
3957
3958     volid = vsu_GetVolumeID(as->parms[2].items->data, cstruct, &err);
3959     if (volid == 0) {
3960         if (err)
3961             PrintError("", err);
3962         else
3963             fprintf(STDERR, "vos: can't find volume '%s'\n",
3964                     as->parms[2].items->data);
3965         exit(1);
3966     }
3967     part = volutil_GetPartitionID(as->parms[1].items->data);
3968     if (part < 0) {
3969         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3970                 as->parms[1].items->data);
3971         exit(1);
3972     }
3973     server = GetServer(as->parms[0].items->data);
3974     if (!server) {
3975         fprintf(STDERR, "vos: host '%s' not found in host table\n",
3976                 as->parms[0].items->data);
3977         exit(1);
3978     }
3979     if (!IsPartValid(part, server, &code)) {    /*check for validity of the partition */
3980         if (code)
3981             PrintError("", code);
3982         else
3983             fprintf(STDERR,
3984                     "vos : partition %s does not exist on the server\n",
3985                     as->parms[1].items->data);
3986         exit(1);
3987     }
3988     code = VLDB_GetEntryByID(volid, -1, &entry);
3989     if (!code) {
3990         if (volid == entry.volumeId[RWVOL])
3991             backupid = entry.volumeId[BACKVOL];
3992         fprintf(STDERR,
3993                 "Warning: Entry for volume number %lu exists in VLDB (but we're zapping it anyway!)\n",
3994                 (unsigned long)volid);
3995     }
3996     if (zapbackupid) {
3997         volintInfo *pntr = (volintInfo *) 0;
3998
3999         if (!backupid) {
4000             code = UV_ListOneVolume(server, part, volid, &pntr);
4001             if (!code) {
4002                 if (volid == pntr->parentID)
4003                     backupid = pntr->backupID;
4004                 if (pntr)
4005                     free(pntr);
4006             }
4007         }
4008         if (backupid) {
4009             code = UV_VolumeZap(server, part, backupid);
4010             if (code) {
4011                 PrintDiagnostics("zap", code);
4012                 exit(1);
4013             }
4014             fprintf(STDOUT, "Backup Volume %lu deleted\n",
4015                     (unsigned long)backupid);
4016         }
4017     }
4018     code = UV_VolumeZap(server, part, volid);
4019     if (code) {
4020         PrintDiagnostics("zap", code);
4021         exit(1);
4022     }
4023     fprintf(STDOUT, "Volume %lu deleted\n", (unsigned long)volid);
4024
4025     return 0;
4026 }
4027
4028 static int
4029 VolserStatus(struct cmd_syndesc *as, void *arock)
4030 {
4031     afs_uint32 server;
4032     afs_int32 code;
4033     transDebugInfo *pntr, *oldpntr;
4034     afs_int32 count;
4035     int i;
4036     char pname[10];
4037     time_t t;
4038
4039     server = GetServer(as->parms[0].items->data);
4040     if (!server) {
4041         fprintf(STDERR, "vos: host '%s' not found in host table\n",
4042                 as->parms[0].items->data);
4043         exit(1);
4044     }
4045     code = UV_VolserStatus(server, &pntr, &count);
4046     if (code) {
4047         PrintDiagnostics("status", code);
4048         exit(1);
4049     }
4050     oldpntr = pntr;
4051     if (count == 0)
4052         fprintf(STDOUT, "No active transactions on %s\n",
4053                 as->parms[0].items->data);
4054     else {
4055         fprintf(STDOUT, "Total transactions: %d\n", count);
4056     }
4057     for (i = 0; i < count; i++) {
4058         /*print out the relevant info */
4059         fprintf(STDOUT, "--------------------------------------\n");
4060         t = pntr->creationTime;
4061         fprintf(STDOUT, "transaction: %lu  created: %s",
4062                 (unsigned long)pntr->tid, ctime(&t));
4063         t = pntr->time;
4064         fprintf(STDOUT, "lastActiveTime: %s", ctime(&t));
4065         if (pntr->returnCode) {
4066             fprintf(STDOUT, "returnCode: %lu\n",
4067                     (unsigned long)pntr->returnCode);
4068         }
4069         if (pntr->iflags) {
4070             fprintf(STDOUT, "attachFlags:  ");
4071             switch (pntr->iflags) {
4072             case ITOffline:
4073                 fprintf(STDOUT, "offline ");
4074                 break;
4075             case ITBusy:
4076                 fprintf(STDOUT, "busy ");
4077                 break;
4078             case ITReadOnly:
4079                 fprintf(STDOUT, "readonly ");
4080                 break;
4081             case ITCreate:
4082                 fprintf(STDOUT, "create ");
4083                 break;
4084             case ITCreateVolID:
4085                 fprintf(STDOUT, "create volid ");
4086                 break;
4087             }
4088             fprintf(STDOUT, "\n");
4089         }
4090         if (pntr->vflags) {
4091             fprintf(STDOUT, "volumeStatus: ");
4092             switch (pntr->vflags) {
4093             case VTDeleteOnSalvage:
4094                 fprintf(STDOUT, "deleteOnSalvage ");
4095             case VTOutOfService:
4096                 fprintf(STDOUT, "outOfService ");
4097             case VTDeleted:
4098                 fprintf(STDOUT, "deleted ");
4099             }
4100             fprintf(STDOUT, "\n");
4101         }
4102         if (pntr->tflags) {
4103             fprintf(STDOUT, "transactionFlags: ");
4104             fprintf(STDOUT, "delete\n");
4105         }
4106         MapPartIdIntoName(pntr->partition, pname);
4107         fprintf(STDOUT, "volume: %lu  partition: %s  procedure: %s\n",
4108                 (unsigned long)pntr->volid, pname, pntr->lastProcName);
4109         if (pntr->callValid) {
4110             t = pntr->lastReceiveTime;
4111             fprintf(STDOUT, "packetRead: %lu  lastReceiveTime: %s",
4112                     (unsigned long)pntr->readNext, ctime(&t));
4113             t = pntr->lastSendTime;
4114             fprintf(STDOUT, "packetSend: %lu  lastSendTime: %s",
4115                     (unsigned long)pntr->transmitNext, ctime(&t));
4116         }
4117         pntr++;
4118         fprintf(STDOUT, "--------------------------------------\n");
4119         fprintf(STDOUT, "\n");
4120     }
4121     if (oldpntr)
4122         free(oldpntr);
4123     return 0;
4124 }
4125
4126 static int
4127 RenameVolume(struct cmd_syndesc *as, void *arock)
4128 {
4129     afs_int32 code1, code2, code;
4130     struct nvldbentry entry;
4131
4132     code1 = VLDB_GetEntryByName(as->parms[0].items->data, &entry);
4133     if (code1) {
4134         fprintf(STDERR, "vos: Could not find entry for volume %s\n",
4135                 as->parms[0].items->data);
4136         exit(1);
4137     }
4138     code2 = VLDB_GetEntryByName(as->parms[1].items->data, &entry);
4139     if ((!code1) && (!code2)) { /*the newname already exists */
4140         fprintf(STDERR, "vos: volume %s already exists\n",
4141                 as->parms[1].items->data);
4142         exit(1);
4143     }
4144
4145     if (code1 && code2) {
4146         fprintf(STDERR, "vos: Could not find entry for volume %s or %s\n",
4147                 as->parms[0].items->data, as->parms[1].items->data);
4148         exit(1);
4149     }
4150     if (!VolNameOK(as->parms[0].items->data)) {
4151         fprintf(STDERR,
4152                 "Illegal volume name %s, should not end in .readonly or .backup\n",
4153                 as->parms[0].items->data);
4154         exit(1);
4155     }
4156     if (!ISNAMEVALID(as->parms[1].items->data)) {
4157         fprintf(STDERR,
4158                 "vos: the new volume name %s exceeds the size limit of %d\n",
4159                 as->parms[1].items->data, VOLSER_OLDMAXVOLNAME - 10);
4160         exit(1);
4161     }
4162     if (!VolNameOK(as->parms[1].items->data)) {
4163         fprintf(STDERR,
4164                 "Illegal volume name %s, should not end in .readonly or .backup\n",
4165                 as->parms[1].items->data);
4166         exit(1);
4167     }
4168     if (IsNumeric(as->parms[1].items->data)) {
4169         fprintf(STDERR, "Illegal volume name %s, should not be a number\n",
4170                 as->parms[1].items->data);
4171         exit(1);
4172     }
4173     MapHostToNetwork(&entry);
4174     code =
4175         UV_RenameVolume(&entry, as->parms[0].items->data,
4176                         as->parms[1].items->data);
4177     if (code) {
4178         PrintDiagnostics("rename", code);
4179         exit(1);
4180     }
4181     fprintf(STDOUT, "Renamed volume %s to %s\n", as->parms[0].items->data,
4182             as->parms[1].items->data);
4183     return 0;
4184 }
4185
4186 int
4187 GetVolumeInfo(afs_uint32 volid, afs_uint32 *server, afs_int32 *part, afs_int32 *voltype,
4188               struct nvldbentry *rentry)
4189 {
4190     afs_int32 vcode;
4191     int i, index = -1;
4192
4193     vcode = VLDB_GetEntryByID(volid, -1, rentry);
4194     if (vcode) {
4195         fprintf(STDERR,
4196                 "Could not fetch the entry for volume %lu from VLDB \n",
4197                 (unsigned long)volid);
4198         PrintError("", vcode);
4199         return (vcode);
4200     }
4201     MapHostToNetwork(rentry);
4202     if (volid == rentry->volumeId[ROVOL]) {
4203         *voltype = ROVOL;
4204         for (i = 0; i < rentry->nServers; i++) {
4205             if ((index == -1) && (rentry->serverFlags[i] & ITSROVOL)
4206                 && !(rentry->serverFlags[i] & RO_DONTUSE))
4207                 index = i;
4208         }
4209         if (index == -1) {
4210             fprintf(STDERR,
4211                     "RO volume is not found in VLDB entry for volume %lu\n",
4212                     (unsigned long)volid);
4213             return -1;
4214         }
4215
4216         *server = rentry->serverNumber[index];
4217         *part = rentry->serverPartition[index];
4218         return 0;
4219     }
4220
4221     index = Lp_GetRwIndex(rentry);
4222     if (index == -1) {
4223         fprintf(STDERR,
4224                 "RW Volume is not found in VLDB entry for volume %lu\n",
4225                 (unsigned long)volid);
4226         return -1;
4227     }
4228     if (volid == rentry->volumeId[RWVOL]) {
4229         *voltype = RWVOL;
4230         *server = rentry->serverNumber[index];
4231         *part = rentry->serverPartition[index];
4232         return 0;
4233     }
4234     if (volid == rentry->volumeId[BACKVOL]) {
4235         *voltype = BACKVOL;
4236         *server = rentry->serverNumber[index];
4237         *part = rentry->serverPartition[index];
4238         return 0;
4239     }
4240     fprintf(STDERR,
4241             "unexpected volume type for volume %lu\n",
4242             (unsigned long)volid);
4243     return -1;
4244 }
4245
4246 static int
4247 DeleteEntry(struct cmd_syndesc *as, void *arock)
4248 {
4249     afs_int32 apart = 0;
4250     afs_uint32 avolid;
4251     afs_int32 vcode;
4252     struct VldbListByAttributes attributes;
4253     nbulkentries arrayEntries;
4254     struct nvldbentry *vllist;
4255     struct cmd_item *itp;
4256     afs_int32 nentries;
4257     int j;
4258     char prefix[VOLSER_MAXVOLNAME + 1];
4259     int seenprefix = 0;
4260     afs_int32 totalBack = 0, totalFail = 0, err;
4261
4262     if (as->parms[0].items) {   /* -id */
4263         if (as->parms[1].items || as->parms[2].items || as->parms[3].items) {
4264             fprintf(STDERR,
4265                     "You cannot use -server, -partition, or -prefix with the -id argument\n");
4266             exit(-2);
4267         }
4268         for (itp = as->parms[0].items; itp; itp = itp->next) {
4269             avolid = vsu_GetVolumeID(itp->data, cstruct, &err);
4270             if (avolid == 0) {
4271                 if (err)
4272                     PrintError("", err);
4273                 else
4274                     fprintf(STDERR, "vos: can't find volume '%s'\n",
4275                             itp->data);
4276                 continue;
4277             }
4278             if (as->parms[4].items || as->parms[5].items) {
4279                 /* -noexecute (hidden) or -dryrun */
4280                 fprintf(STDOUT, "Would have deleted VLDB entry for %s \n",
4281                         itp->data);
4282                 fflush(STDOUT);
4283                 continue;
4284             }
4285             vcode = ubik_VL_DeleteEntry(cstruct, 0, avolid, RWVOL);
4286             if (vcode) {
4287                 fprintf(STDERR, "Could not delete entry for volume %s\n",
4288                         itp->data);
4289                 fprintf(STDERR,
4290                         "You must specify a RW volume name or ID "
4291                         "(the entire VLDB entry will be deleted)\n");
4292                 PrintError("", vcode);
4293                 totalFail++;
4294                 continue;
4295             }
4296             totalBack++;
4297         }
4298         fprintf(STDOUT, "Deleted %d VLDB entries\n", totalBack);
4299         return (totalFail);
4300     }
4301
4302     if (!as->parms[1].items && !as->parms[2].items && !as->parms[3].items) {
4303         fprintf(STDERR, "You must specify an option\n");
4304         exit(-2);
4305     }
4306
4307     /* Zero out search attributes */
4308     memset(&attributes, 0, sizeof(struct VldbListByAttributes));
4309
4310     if (as->parms[1].items) {   /* -prefix */
4311         strncpy(prefix, as->parms[1].items->data, VOLSER_MAXVOLNAME);
4312         seenprefix = 1;
4313         if (!as->parms[2].items && !as->parms[3].items) {       /* a single entry only */
4314             fprintf(STDERR,
4315                     "You must provide -server with the -prefix argument\n");
4316             exit(-2);
4317         }
4318     }
4319
4320     if (as->parms[2].items) {   /* -server */
4321         afs_uint32 aserver;
4322         aserver = GetServer(as->parms[2].items->data);
4323         if (aserver == 0) {
4324             fprintf(STDERR, "vos: server '%s' not found in host table\n",
4325                     as->parms[2].items->data);
4326             exit(-1);
4327         }
4328         attributes.server = ntohl(aserver);
4329         attributes.Mask |= VLLIST_SERVER;
4330     }
4331
4332     if (as->parms[3].items) {   /* -partition */
4333         if (!as->parms[2].items) {
4334             fprintf(STDERR,
4335                     "You must provide -server with the -partition argument\n");
4336             exit(-2);
4337         }
4338         apart = volutil_GetPartitionID(as->parms[3].items->data);
4339         if (apart < 0) {
4340             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
4341                     as->parms[3].items->data);
4342             exit(-1);
4343         }
4344         attributes.partition = apart;
4345         attributes.Mask |= VLLIST_PARTITION;
4346     }
4347
4348     /* Print status line of what we are doing */
4349     fprintf(STDOUT, "Deleting VLDB entries for ");
4350     if (as->parms[2].items) {
4351         fprintf(STDOUT, "server %s ", as->parms[2].items->data);
4352     }
4353     if (as->parms[3].items) {
4354         char pname[10];
4355         MapPartIdIntoName(apart, pname);
4356         fprintf(STDOUT, "partition %s ", pname);
4357     }
4358     if (seenprefix) {
4359         fprintf(STDOUT, "which are prefixed with %s ", prefix);
4360     }
4361     fprintf(STDOUT, "\n");
4362     fflush(STDOUT);
4363
4364     /* Get all the VLDB entries on a server and/or partition */
4365     memset(&arrayEntries, 0, sizeof(arrayEntries));
4366     vcode = VLDB_ListAttributes(&attributes, &nentries, &arrayEntries);
4367     if (vcode) {
4368         fprintf(STDERR, "Could not access the VLDB for attributes\n");
4369         PrintError("", vcode);
4370         exit(-1);
4371     }
4372
4373     /* Process each entry */
4374     for (j = 0; j < nentries; j++) {
4375         vllist = &arrayEntries.nbulkentries_val[j];
4376         if (seenprefix) {
4377             /* It only deletes the RW volumes */
4378             if (strncmp(vllist->name, prefix, strlen(prefix))) {
4379                 if (verbose) {
4380                     fprintf(STDOUT,
4381                             "Omitting to delete %s due to prefix %s mismatch\n",
4382                             vllist->name, prefix);
4383                 }
4384                 fflush(STDOUT);
4385                 continue;
4386             }
4387         }
4388
4389         if (as->parms[4].items || as->parms[5].items) {
4390             /* -noexecute (hidden) or -dryrun */
4391             fprintf(STDOUT, "Would have deleted VLDB entry for %s \n",
4392                     vllist->name);
4393             fflush(STDOUT);
4394             continue;
4395         }
4396
4397         /* Only matches the RW volume name */
4398         avolid = vllist->volumeId[RWVOL];
4399         vcode = ubik_VL_DeleteEntry(cstruct, 0, avolid, RWVOL);
4400         if (vcode) {
4401             fprintf(STDOUT, "Could not delete VLDB entry for  %s\n",
4402                     vllist->name);
4403             totalFail++;
4404             PrintError("", vcode);
4405             continue;
4406         } else {
4407             totalBack++;
4408             if (verbose)
4409                 fprintf(STDOUT, "Deleted VLDB entry for %s \n", vllist->name);
4410         }
4411         fflush(STDOUT);
4412     }                           /*for */
4413
4414     fprintf(STDOUT, "----------------------\n");
4415     fprintf(STDOUT,
4416             "Total VLDB entries deleted: %lu; failed to delete: %lu\n",
4417             (unsigned long)totalBack, (unsigned long)totalFail);
4418
4419     xdr_free((xdrproc_t) xdr_nbulkentries, &arrayEntries);
4420     return 0;
4421 }
4422
4423
4424 static int
4425 CompareVldbEntryByName(const void *p1, const void *p2)
4426 {
4427     struct nvldbentry *arg1, *arg2;
4428
4429     arg1 = (struct nvldbentry *)p1;
4430     arg2 = (struct nvldbentry *)p2;
4431     return (strcmp(arg1->name, arg2->name));
4432 }
4433
4434 /*
4435 static int CompareVldbEntry(char *p1, char *p2)
4436 {
4437     struct nvldbentry *arg1,*arg2;
4438     int i;
4439     int pos1, pos2;
4440     char comp1[100],comp2[100];
4441     char temp1[20],temp2[20];
4442
4443     arg1 = (struct nvldbentry *)p1;
4444     arg2 = (struct nvldbentry *)p2;
4445     pos1 = -1;
4446     pos2 = -1;
4447
4448     for(i = 0; i < arg1->nServers; i++)
4449         if(arg1->serverFlags[i] & ITSRWVOL) pos1 = i;
4450     for(i = 0; i < arg2->nServers; i++)
4451         if(arg2->serverFlags[i] & ITSRWVOL) pos2 = i;
4452     if(pos1 == -1 || pos2 == -1){
4453         pos1 = 0;
4454         pos2 = 0;
4455     }
4456     sprintf(comp1,"%10u",arg1->serverNumber[pos1]);
4457     sprintf(comp2,"%10u",arg2->serverNumber[pos2]);
4458     sprintf(temp1,"%10u",arg1->serverPartition[pos1]);
4459     sprintf(temp2,"%10u",arg2->serverPartition[pos2]);
4460     strcat(comp1,temp1);
4461     strcat(comp2,temp2);
4462     strcat(comp1,arg1->name);
4463     strcat(comp1,arg2->name);
4464     return(strcmp(comp1,comp2));
4465
4466 }
4467
4468 */
4469 static int
4470 ListVLDB(struct cmd_syndesc *as, void *arock)
4471 {
4472     afs_int32 apart;
4473     afs_uint32 aserver;
4474     afs_int32 code;
4475     afs_int32 vcode;
4476     struct VldbListByAttributes attributes;
4477     nbulkentries arrayEntries;
4478     struct nvldbentry *vllist, *tarray = 0, *ttarray;
4479     afs_int32 centries, nentries = 0;
4480     afs_int32 tarraysize = 0;
4481     afs_int32 parraysize;
4482     int j;
4483     char pname[10];
4484     int quiet, sort, lock;
4485     afs_int32 thisindex, nextindex;
4486
4487     aserver = 0;
4488     apart = 0;
4489
4490     attributes.Mask = 0;
4491     lock = (as->parms[3].items ? 1 : 0);        /* -lock   flag */
4492     quiet = (as->parms[4].items ? 1 : 0);       /* -quit   flag */
4493     sort = (as->parms[5].items ? 0 : 1);        /* -nosort flag */
4494
4495     /* If the volume name is given, Use VolumeInfoCmd to look it up
4496      * and not ListAttributes.
4497      */
4498     if (as->parms[0].items) {
4499         if (lock) {
4500             fprintf(STDERR,
4501                     "vos: illegal use of '-locked' switch, need to specify server and/or partition\n");
4502             exit(1);
4503         }
4504         code = VolumeInfoCmd(as->parms[0].items->data);
4505         if (code) {
4506             PrintError("", code);
4507             exit(1);
4508         }
4509         return 0;
4510     }
4511
4512     /* Server specified */
4513     if (as->parms[1].items) {
4514         aserver = GetServer(as->parms[1].items->data);
4515         if (aserver == 0) {
4516             fprintf(STDERR, "vos: server '%s' not found in host table\n",
4517                     as->parms[1].items->data);
4518             exit(1);
4519         }
4520         attributes.server = ntohl(aserver);
4521         attributes.Mask |= VLLIST_SERVER;
4522     }
4523
4524     /* Partition specified */
4525     if (as->parms[2].items) {
4526         apart = volutil_GetPartitionID(as->parms[2].items->data);
4527         if (apart < 0) {
4528             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
4529                     as->parms[2].items->data);
4530             exit(1);
4531         }
4532         attributes.partition = apart;
4533         attributes.Mask |= VLLIST_PARTITION;
4534     }
4535
4536     if (lock) {
4537         attributes.Mask |= VLLIST_FLAG;
4538         attributes.flag = VLOP_ALLOPERS;
4539     }
4540
4541     /* Print header information */
4542     if (!quiet) {
4543         MapPartIdIntoName(apart, pname);
4544         fprintf(STDOUT, "VLDB entries for %s %s%s%s %s\n",
4545                 (as->parms[1].items ? "server" : "all"),
4546                 (as->parms[1].items ? as->parms[1].items->data : "servers"),
4547                 (as->parms[2].items ? " partition " : ""),
4548                 (as->parms[2].items ? pname : ""),
4549                 (lock ? "which are locked:" : ""));
4550     }
4551
4552     for (thisindex = 0; (thisindex != -1); thisindex = nextindex) {
4553         memset(&arrayEntries, 0, sizeof(arrayEntries));
4554         centries = 0;
4555         nextindex = -1;
4556
4557         vcode =
4558             VLDB_ListAttributesN2(&attributes, 0, thisindex, &centries,
4559                                   &arrayEntries, &nextindex);
4560         if (vcode == RXGEN_OPCODE) {
4561             /* Vlserver not running with ListAttributesN2. Fall back */
4562             vcode =
4563                 VLDB_ListAttributes(&attributes, &centries, &arrayEntries);
4564             nextindex = -1;
4565         }
4566         if (vcode) {
4567             fprintf(STDERR, "Could not access the VLDB for attributes\n");
4568             PrintError("", vcode);
4569             exit(1);
4570         }
4571         nentries += centries;
4572
4573         /* We don't sort, so just print the entries now */
4574         if (!sort) {
4575             for (j = 0; j < centries; j++) {    /* process each entry */
4576                 vllist = &arrayEntries.nbulkentries_val[j];
4577                 MapHostToNetwork(vllist);
4578                 EnumerateEntry(vllist);
4579
4580                 PrintLocked(vllist->flags);
4581             }
4582         }
4583
4584         /* So we sort. First we must collect all the entries and keep
4585          * them in memory.
4586          */
4587         else if (centries > 0) {
4588             if (!tarray) {
4589                 /* malloc the first bulk entries array */
4590                 tarraysize = centries * sizeof(struct nvldbentry);
4591                 tarray = malloc(tarraysize);
4592                 if (!tarray) {
4593                     fprintf(STDERR,
4594                             "Could not allocate enough space for the VLDB entries\n");
4595                     goto bypass;
4596                 }
4597                 memcpy((char*)tarray, arrayEntries.nbulkentries_val, tarraysize);
4598             } else {
4599                 /* Grow the tarray to keep the extra entries */
4600                 parraysize = (centries * sizeof(struct nvldbentry));
4601                 ttarray =
4602                     (struct nvldbentry *)realloc(tarray,
4603                                                  tarraysize + parraysize);
4604                 if (!ttarray) {
4605                     fprintf(STDERR,
4606                             "Could not allocate enough space for  the VLDB entries\n");
4607                     goto bypass;
4608                 }
4609                 tarray = ttarray;
4610
4611                 /* Copy them in */
4612                 memcpy(((char *)tarray) + tarraysize,
4613                        (char *)arrayEntries.nbulkentries_val, parraysize);
4614                 tarraysize += parraysize;
4615             }
4616         }
4617
4618         /* Free the bulk array */
4619         xdr_free((xdrproc_t) xdr_nbulkentries, &arrayEntries);
4620     }
4621
4622     /* Here is where we now sort all the entries and print them */
4623     if (sort && (nentries > 0)) {
4624         qsort((char *)tarray, nentries, sizeof(struct nvldbentry),
4625               CompareVldbEntryByName);
4626         for (vllist = tarray, j = 0; j < nentries; j++, vllist++) {
4627             MapHostToNetwork(vllist);
4628             EnumerateEntry(vllist);
4629
4630             PrintLocked(vllist->flags);
4631         }
4632     }
4633
4634   bypass:
4635     if (!quiet)
4636         fprintf(STDOUT, "\nTotal entries: %lu\n", (unsigned long)nentries);
4637     if (tarray)
4638         free(tarray);
4639     return 0;
4640 }
4641
4642 static int
4643 BackSys(struct cmd_syndesc *as, void *arock)
4644 {
4645     afs_uint32 avolid;
4646     afs_int32 apart = 0;
4647     afs_uint32 aserver = 0, aserver1;
4648     afs_int32 code, apart1;
4649     afs_int32 vcode;
4650     struct VldbListByAttributes attributes;
4651     nbulkentries arrayEntries;
4652     struct nvldbentry *vllist;
4653     afs_int32 nentries;
4654     int j;
4655     char pname[10];
4656     int seenprefix, seenxprefix, exclude, ex, exp, noaction;
4657     afs_int32 totalBack = 0;
4658     afs_int32 totalFail = 0;
4659     int previdx = -1;
4660     int error;
4661     int same = 0;
4662     struct cmd_item *ti;
4663     int match = 0;
4664 #ifndef HAVE_POSIX_REGEX
4665     char *ccode;
4666 #endif
4667
4668     memset(&attributes, 0, sizeof(struct VldbListByAttributes));
4669     attributes.Mask = 0;
4670
4671     seenprefix = (as->parms[0].items ? 1 : 0);
4672     exclude = (as->parms[3].items ? 1 : 0);
4673     seenxprefix = (as->parms[4].items ? 1 : 0);
4674     noaction = (as->parms[5].items ? 1 : 0);
4675
4676     if (as->parms[1].items) {   /* -server */
4677         aserver = GetServer(as->parms[1].items->data);
4678         if (aserver == 0) {
4679             fprintf(STDERR, "vos: server '%s' not found in host table\n",
4680                     as->parms[1].items->data);
4681             exit(1);
4682         }
4683         attributes.server = ntohl(aserver);
4684         attributes.Mask |= VLLIST_SERVER;
4685     }
4686
4687     if (as->parms[2].items) {   /* -partition */
4688         apart = volutil_GetPartitionID(as->parms[2].items->data);
4689         if (apart < 0) {
4690             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
4691                     as->parms[2].items->data);
4692             exit(1);
4693         }
4694         attributes.partition = apart;
4695         attributes.Mask |= VLLIST_PARTITION;
4696     }
4697
4698     /* Check to make sure the prefix and xprefix expressions compile ok */
4699     if (seenprefix) {
4700         for (ti = as->parms[0].items; ti; ti = ti->next) {
4701             if (strncmp(ti->data, "^", 1) == 0) {
4702 #ifdef HAVE_POSIX_REGEX
4703                 regex_t re;
4704                 char errbuf[256];
4705
4706                 code = regcomp(&re, ti->data, REG_NOSUB);
4707                 if (code != 0) {
4708                     regerror(code, &re, errbuf, sizeof errbuf);
4709                     fprintf(STDERR,
4710                             "Unrecognizable -prefix regular expression: '%s': %s\n",
4711                             ti->data, errbuf);
4712                     exit(1);
4713                 }
4714                 regfree(&re);
4715 #else
4716                 ccode = (char *)re_comp(ti->data);
4717                 if (ccode) {
4718                     fprintf(STDERR,
4719                             "Unrecognizable -prefix regular expression: '%s': %s\n",
4720                             ti->data, ccode);
4721                     exit(1);
4722                 }
4723 #endif
4724             }
4725         }
4726     }
4727     if (seenxprefix) {
4728         for (ti = as->parms[4].items; ti; ti = ti->next) {
4729             if (strncmp(ti->data, "^", 1) == 0) {
4730 #ifdef HAVE_POSIX_REGEX
4731                 regex_t re;
4732                 char errbuf[256];
4733
4734                 code = regcomp(&re, ti->data, REG_NOSUB);
4735                 if (code != 0) {
4736                     regerror(code, &re, errbuf, sizeof errbuf);
4737                     fprintf(STDERR,
4738                             "Unrecognizable -xprefix regular expression: '%s': %s\n",
4739                             ti->data, errbuf);
4740                     exit(1);
4741                 }
4742                 regfree(&re);
4743 #else
4744                 ccode = (char *)re_comp(ti->data);
4745                 if (ccode) {
4746                     fprintf(STDERR,
4747                             "Unrecognizable -xprefix regular expression: '%s': %s\n",
4748                             ti->data, ccode);
4749                     exit(1);
4750                 }
4751 #endif
4752             }
4753         }
4754     }
4755
4756     memset(&arrayEntries, 0, sizeof(arrayEntries));     /* initialize to hint the stub to alloc space */
4757     vcode = VLDB_ListAttributes(&attributes, &nentries, &arrayEntries);
4758     if (vcode) {
4759         fprintf(STDERR, "Could not access the VLDB for attributes\n");
4760         PrintError("", vcode);
4761         exit(1);
4762     }
4763
4764     if (as->parms[1].items || as->parms[2].items || verbose) {
4765         fprintf(STDOUT, "%s up volumes",
4766                 (noaction ? "Would have backed" : "Backing"));
4767
4768         if (as->parms[1].items) {
4769             fprintf(STDOUT, " on server %s", as->parms[1].items->data);
4770         } else if (as->parms[2].items) {
4771             fprintf(STDOUT, " for all servers");
4772         }
4773
4774         if (as->parms[2].items) {
4775             MapPartIdIntoName(apart, pname);
4776             fprintf(STDOUT, " partition %s", pname);
4777         }
4778
4779         if (seenprefix || (!seenprefix && seenxprefix)) {
4780             ti = (seenprefix ? as->parms[0].items : as->parms[4].items);
4781             ex = (seenprefix ? exclude : !exclude);
4782             exp = (strncmp(ti->data, "^", 1) == 0);
4783             fprintf(STDOUT, " which %smatch %s '%s'", (ex ? "do not " : ""),
4784                     (exp ? "expression" : "prefix"), ti->data);
4785             for (ti = ti->next; ti; ti = ti->next) {
4786                 exp = (strncmp(ti->data, "^", 1) == 0);
4787                 printf(" %sor %s '%s'", (ex ? "n" : ""),
4788                        (exp ? "expression" : "prefix"), ti->data);
4789             }
4790         }
4791
4792         if (seenprefix && seenxprefix) {
4793             ti = as->parms[4].items;
4794             exp = (strncmp(ti->data, "^", 1) == 0);
4795             fprintf(STDOUT, " %swhich match %s '%s'",
4796                     (exclude ? "adding those " : "removing those "),
4797                     (exp ? "expression" : "prefix"), ti->data);
4798             for (ti = ti->next; ti; ti = ti->next) {
4799                 exp = (strncmp(ti->data, "^", 1) == 0);
4800                 printf(" or %s '%s'", (exp ? "expression" : "prefix"),
4801                        ti->data);
4802             }
4803         }
4804         fprintf(STDOUT, " .. ");
4805         if (verbose)
4806             fprintf(STDOUT, "\n");
4807         fflush(STDOUT);
4808     }
4809
4810     for (j = 0; j < nentries; j++) {    /* process each vldb entry */
4811         vllist = &arrayEntries.nbulkentries_val[j];
4812
4813         if (seenprefix) {
4814             for (ti = as->parms[0].items; ti; ti = ti->next) {
4815                 if (strncmp(ti->data, "^", 1) == 0) {
4816 #ifdef HAVE_POSIX_REGEX
4817                     regex_t re;
4818                     char errbuf[256];
4819
4820                     /* XXX -- should just do the compile once! */
4821                     code = regcomp(&re, ti->data, REG_NOSUB);
4822                     if (code != 0) {
4823                         regerror(code, &re, errbuf, sizeof errbuf);
4824                         fprintf(STDERR,
4825                                 "Error in -prefix regular expression: '%s': %s\n",
4826                                 ti->data, errbuf);
4827                         exit(1);
4828                     }
4829                     match = (regexec(&re, vllist->name, 0, NULL, 0) == 0);
4830                     regfree(&re);
4831 #else
4832                     ccode = (char *)re_comp(ti->data);
4833                     if (ccode) {
4834                         fprintf(STDERR,
4835                                 "Error in -prefix regular expression: '%s': %s\n",
4836                                 ti->data, ccode);
4837                         exit(1);
4838                     }
4839                     match = (re_exec(vllist->name) == 1);
4840 #endif
4841                 } else {
4842                     match =
4843                         (strncmp(vllist->name, ti->data, strlen(ti->data)) ==
4844                          0);
4845                 }
4846                 if (match)
4847                     break;
4848             }
4849         } else {
4850             match = 1;
4851         }
4852
4853         /* Without the -exclude flag: If it matches the prefix, then
4854          *    check if we want to exclude any from xprefix.
4855          * With the -exclude flag: If it matches the prefix, then
4856          *    check if we want to add any from xprefix.
4857          */
4858         if (match && seenxprefix) {
4859             for (ti = as->parms[4].items; ti; ti = ti->next) {
4860                 if (strncmp(ti->data, "^", 1) == 0) {
4861 #ifdef HAVE_POSIX_REGEX
4862                     regex_t re;
4863                     char errbuf[256];
4864
4865                     /* XXX -- should just do the compile once! */
4866                     code = regcomp(&re, ti->data, REG_NOSUB);
4867                     if (code != 0) {
4868                         regerror(code, &re, errbuf, sizeof errbuf);
4869                         fprintf(STDERR,
4870                                 "Error in -xprefix regular expression: '%s': %s\n",
4871                                 ti->data, errbuf);
4872                         exit(1);
4873                     }
4874                     if (regexec(&re, vllist->name, 0, NULL, 0) == 0)
4875                             match = 0;
4876                     regfree(&re);
4877 #else
4878                     ccode = (char *)re_comp(ti->data);
4879                     if (ccode) {
4880                         fprintf(STDERR,
4881                                 "Error in -xprefix regular expression: '%s': %s\n",
4882                                 ti->data, ccode);
4883                         exit(1);
4884                     }
4885                     if (re_exec(vllist->name) == 1) {
4886                         match = 0;
4887                         break;
4888                     }
4889 #endif
4890                 } else {
4891                     if (strncmp(vllist->name, ti->data, strlen(ti->data)) ==
4892                         0) {
4893                         match = 0;
4894                         break;
4895                     }
4896                 }
4897             }
4898         }
4899
4900         if (exclude)
4901             match = !match;     /* -exclude will reverse the match */
4902         if (!match)
4903             continue;           /* Skip if no match */
4904
4905         /* Print list of volumes to backup */
4906         if (noaction) {
4907             fprintf(STDOUT, "     %s\n", vllist->name);
4908             continue;
4909         }
4910
4911         if (!(vllist->flags & RW_EXISTS)) {
4912             if (verbose) {
4913                 fprintf(STDOUT,
4914                         "Omitting to backup %s since RW volume does not exist \n",
4915                         vllist->name);
4916                 fprintf(STDOUT, "\n");
4917             }
4918             fflush(STDOUT);
4919             continue;
4920         }
4921
4922         avolid = vllist->volumeId[RWVOL];
4923         MapHostToNetwork(vllist);
4924         GetServerAndPart(vllist, RWVOL, &aserver1, &apart1, &previdx);
4925         if (aserver1 == -1 || apart1 == -1) {
4926             fprintf(STDOUT, "could not backup %s, invalid VLDB entry\n",
4927                     vllist->name);
4928             totalFail++;
4929             continue;
4930         }
4931         if (aserver) {
4932             same = VLDB_IsSameAddrs(aserver, aserver1, &error);
4933             if (error) {
4934                 fprintf(STDERR,
4935                         "Failed to get info about server's %d address(es) from vlserver (err=%d); aborting call!\n",
4936                         aserver, error);
4937                 totalFail++;
4938                 continue;
4939             }
4940         }
4941         if ((aserver && !same) || (apart && (apart != apart1))) {
4942             if (verbose) {
4943                 fprintf(STDOUT,
4944                         "Omitting to backup %s since the RW is in a different location\n",
4945                         vllist->name);
4946             }
4947             continue;
4948         }
4949         if (verbose) {
4950             time_t now = time(0);
4951             fprintf(STDOUT, "Creating backup volume for %s on %s",
4952                     vllist->name, ctime(&now));
4953             fflush(STDOUT);
4954         }
4955
4956         code = UV_BackupVolume(aserver1, apart1, avolid);
4957         if (code) {
4958             fprintf(STDOUT, "Could not backup %s\n", vllist->name);
4959             totalFail++;
4960         } else {
4961             totalBack++;
4962         }
4963         if (verbose)
4964             fprintf(STDOUT, "\n");
4965         fflush(STDOUT);
4966     }                           /* process each vldb entry */
4967     fprintf(STDOUT, "done\n");
4968     fprintf(STDOUT, "Total volumes backed up: %lu; failed to backup: %lu\n",
4969             (unsigned long)totalBack, (unsigned long)totalFail);
4970     fflush(STDOUT);
4971     xdr_free((xdrproc_t) xdr_nbulkentries, &arrayEntries);
4972     return 0;
4973 }
4974
4975 static int
4976 UnlockVLDB(struct cmd_syndesc *as, void *arock)
4977 {
4978     afs_int32 apart;
4979     afs_uint32 aserver = 0;
4980     afs_int32 code;
4981     afs_int32 vcode;
4982     struct VldbListByAttributes attributes;
4983     nbulkentries arrayEntries;
4984     struct nvldbentry *vllist;
4985     afs_int32 nentries;
4986     int j;
4987     afs_uint32 volid;
4988     afs_int32 totalE;
4989     char pname[10];
4990
4991     apart = -1;
4992     totalE = 0;
4993     attributes.Mask = 0;
4994
4995     if (as->parms[0].items) {   /* server specified */
4996         aserver = GetServer(as->parms[0].items->data);
4997         if (aserver == 0) {
4998             fprintf(STDERR, "vos: server '%s' not found in host table\n",
4999                     as->parms[0].items->data);
5000             exit(1);
5001         }
5002         attributes.server = ntohl(aserver);
5003         attributes.Mask |= VLLIST_SERVER;
5004     }
5005     if (as->parms[1].items) {   /* partition specified */
5006         apart = volutil_GetPartitionID(as->parms[1].items->data);
5007         if (apart < 0) {
5008             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
5009                     as->parms[1].items->data);
5010             exit(1);
5011         }
5012         if (!IsPartValid(apart, aserver, &code)) {      /*check for validity of the partition */
5013             if (code)
5014                 PrintError("", code);
5015             else
5016                 fprintf(STDERR,
5017                         "vos : partition %s does not exist on the server\n",
5018                         as->parms[1].items->data);
5019             exit(1);
5020         }
5021         attributes.partition = apart;
5022         attributes.Mask |= VLLIST_PARTITION;
5023     }
5024     attributes.flag = VLOP_ALLOPERS;
5025     attributes.Mask |= VLLIST_FLAG;
5026     memset(&arrayEntries, 0, sizeof(arrayEntries));     /*initialize to hint the stub  to alloc space */
5027     vcode = VLDB_ListAttributes(&attributes, &nentries, &arrayEntries);
5028     if (vcode) {
5029         fprintf(STDERR, "Could not access the VLDB for attributes\n");
5030         PrintError("", vcode);
5031         exit(1);
5032     }
5033     for (j = 0; j < nentries; j++) {    /* process each entry */
5034         vllist = &arrayEntries.nbulkentries_val[j];
5035         volid = vllist->volumeId[RWVOL];
5036         vcode =
5037             ubik_VL_ReleaseLock(cstruct, 0, volid, -1,
5038                                 LOCKREL_OPCODE | LOCKREL_AFSID |
5039                                 LOCKREL_TIMESTAMP);
5040         if (vcode) {
5041             fprintf(STDERR, "Could not unlock entry for volume %s\n",
5042                     vllist->name);
5043             PrintError("", vcode);
5044             totalE++;
5045         }
5046
5047     }
5048     MapPartIdIntoName(apart, pname);
5049     if (totalE)
5050         fprintf(STDOUT,
5051                 "Could not lock %lu VLDB entries of %lu locked entries\n",
5052                 (unsigned long)totalE, (unsigned long)nentries);
5053     else {
5054         if (as->parms[0].items) {
5055             fprintf(STDOUT,
5056                     "Unlocked all the VLDB entries for volumes on server %s ",
5057                     as->parms[0].items->data);
5058             if (as->parms[1].items) {
5059                 MapPartIdIntoName(apart, pname);
5060                 fprintf(STDOUT, "partition %s\n", pname);
5061             } else
5062                 fprintf(STDOUT, "\n");
5063
5064         } else if (as->parms[1].items) {
5065             MapPartIdIntoName(apart, pname);
5066             fprintf(STDOUT,
5067                     "Unlocked all the VLDB entries for volumes on partition %s on all servers\n",
5068                     pname);
5069         }
5070     }
5071
5072     xdr_free((xdrproc_t) xdr_nbulkentries, &arrayEntries);
5073     return 0;
5074 }
5075
5076 static char *
5077 PrintInt64Size(afs_uint64 in)
5078 {
5079     afs_uint32 hi, lo;
5080     char * units;
5081     static char output[16];
5082
5083     SplitInt64(in,hi,lo);
5084
5085     if (hi == 0) {
5086         units = "KB";
5087     } else if (!(hi & 0xFFFFFC00)) {
5088         units = "MB";
5089         lo = (hi << 22) | (lo >> 10);
5090     } else if (!(hi & 0xFFF00000)) {
5091         units = "GB";
5092         lo = (hi << 12) | (lo >> 20);
5093     } else if (!(hi & 0xC0000000)) {
5094         units = "TB";
5095         lo = (hi << 2) | (lo >> 30);
5096     } else {
5097         units = "PB";
5098         lo = (hi >> 8);
5099     }
5100     sprintf(output,"%u %s", lo, units);
5101     return output;
5102 }
5103
5104 static int
5105 PartitionInfo(struct cmd_syndesc *as, void *arock)
5106 {
5107     afs_int32 apart;
5108     afs_uint32 aserver;
5109     afs_int32 code;
5110     char pname[10];
5111     struct diskPartition64 partition;
5112     struct partList dummyPartList;
5113     int i, cnt;
5114     int printSummary=0, sumPartitions=0;
5115     afs_uint64 sumFree, sumStorage;
5116
5117     ZeroInt64(sumFree);
5118     ZeroInt64(sumStorage);
5119     apart = -1;
5120     aserver = GetServer(as->parms[0].items->data);
5121     if (aserver == 0) {
5122         fprintf(STDERR, "vos: server '%s' not found in host table\n",
5123                 as->parms[0].items->data);
5124         exit(1);
5125     }
5126     if (as->parms[1].items) {
5127         apart = volutil_GetPartitionID(as->parms[1].items->data);
5128         if (apart < 0) {
5129             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
5130                     as->parms[1].items->data);
5131             exit(1);
5132         }
5133         dummyPartList.partId[0] = apart;
5134         dummyPartList.partFlags[0] = PARTVALID;
5135         cnt = 1;
5136     }
5137     if (as->parms[2].items) {
5138         printSummary = 1;
5139     }
5140     if (apart != -1) {
5141         if (!IsPartValid(apart, aserver, &code)) {      /*check for validity of the partition */
5142             if (code)
5143                 PrintError("", code);
5144             else
5145                 fprintf(STDERR,
5146                         "vos : partition %s does not exist on the server\n",
5147                         as->parms[1].items->data);
5148             exit(1);
5149         }
5150     } else {
5151         code = UV_ListPartitions(aserver, &dummyPartList, &cnt);
5152         if (code) {
5153             PrintDiagnostics("listpart", code);
5154             exit(1);
5155         }
5156     }
5157     for (i = 0; i < cnt; i++) {
5158         if (dummyPartList.partFlags[i] & PARTVALID) {
5159             MapPartIdIntoName(dummyPartList.partId[i], pname);
5160             code = UV_PartitionInfo64(aserver, pname, &partition);
5161             if (code) {
5162                 fprintf(STDERR, "Could not get information on partition %s\n",
5163                         pname);
5164                 PrintError("", code);
5165                 exit(1);
5166             }
5167             fprintf(STDOUT,
5168                     "Free space on partition %s: %" AFS_INT64_FMT " K blocks out of total %" AFS_INT64_FMT "\n",
5169                     pname, partition.free, partition.minFree);
5170             sumPartitions++;
5171             AddUInt64(sumFree,partition.free,&sumFree);
5172             AddUInt64(sumStorage,partition.minFree,&sumStorage);
5173         }
5174     }
5175     if (printSummary) {
5176         fprintf(STDOUT,
5177                 "Summary: %s free out of ",
5178                 PrintInt64Size(sumFree));
5179         fprintf(STDOUT,
5180                 "%s on %d partitions\n",
5181                 PrintInt64Size(sumStorage),
5182                 sumPartitions);
5183     }
5184     return 0;
5185 }
5186
5187 static int
5188 ChangeAddr(struct cmd_syndesc *as, void *arock)
5189 {
5190     afs_int32 ip1, ip2, vcode;
5191     int remove = 0;
5192
5193     if (noresolve)
5194         ip1 = GetServerNoresolve(as->parms[0].items->data);
5195     else
5196         ip1 = GetServer(as->parms[0].items->data);
5197     if (!ip1) {
5198         fprintf(STDERR, "vos: invalid host address\n");
5199         return (EINVAL);
5200     }
5201
5202     if ((as->parms[1].items && as->parms[2].items)
5203         || (!as->parms[1].items && !as->parms[2].items)) {
5204         fprintf(STDERR,
5205                 "vos: Must specify either '-newaddr <addr>' or '-remove' flag\n");
5206         return (EINVAL);
5207     }
5208
5209     if (as->parms[1].items) {
5210         if (noresolve)
5211             ip2 = GetServerNoresolve(as->parms[1].items->data);
5212         else
5213             ip2 = GetServer(as->parms[1].items->data);
5214         if (!ip2) {
5215             fprintf(STDERR, "vos: invalid host address\n");
5216             return (EINVAL);
5217         }
5218     } else {
5219         /* Play a trick here. If we are removing an address, ip1 will be -1
5220          * and ip2 will be the original address. This switch prevents an
5221          * older revision vlserver from removing the IP address.
5222          */
5223         remove = 1;
5224         ip2 = ip1;
5225         ip1 = 0xffffffff;
5226     }
5227
5228     vcode = ubik_VL_ChangeAddr(cstruct, UBIK_CALL_NEW, ntohl(ip1), ntohl(ip2));
5229     if (vcode) {
5230         char hoststr1[16], hoststr2[16];
5231         if (remove) {
5232             afs_inet_ntoa_r(ip2, hoststr2);
5233             fprintf(STDERR, "Could not remove server %s from the VLDB\n",
5234                     hoststr2);
5235             if (vcode == VL_NOENT) {
5236                 fprintf(STDERR,
5237                         "vlserver does not support the remove flag or ");
5238             }
5239         } else {
5240             afs_inet_ntoa_r(ip1, hoststr1);
5241             afs_inet_ntoa_r(ip2, hoststr2);
5242             fprintf(STDERR, "Could not change server %s to server %s\n",
5243                     hoststr1, hoststr2);
5244         }
5245         PrintError("", vcode);
5246         return (vcode);
5247     }
5248
5249     if (remove) {
5250         fprintf(STDOUT, "Removed server %s from the VLDB\n",
5251                 as->parms[0].items->data);
5252     } else {
5253         fprintf(STDOUT, "Changed server %s to server %s\n",
5254                 as->parms[0].items->data, as->parms[1].items->data);
5255     }
5256     return 0;
5257 }
5258
5259 static void
5260 print_addrs(const bulkaddrs * addrs, afsUUID * m_uuid, int nentries,
5261             int print)
5262 {
5263     int i;
5264     afs_uint32 *addrp;
5265     char buf[1024];
5266
5267     if (print) {
5268         afsUUID_to_string(m_uuid, buf, sizeof(buf));
5269         printf("UUID: %s\n", buf);
5270     }
5271
5272     /* print out the list of all the server */
5273     addrp = (afs_uint32 *) addrs->bulkaddrs_val;
5274     for (i = 0; i < nentries; i++, addrp++) {
5275         *addrp = htonl(*addrp);
5276         if (noresolve) {
5277             char hoststr[16];
5278             printf("%s\n", afs_inet_ntoa_r(*addrp, hoststr));
5279         } else {
5280             printf("%s\n", hostutil_GetNameByINet(*addrp));
5281         }
5282     }
5283
5284     if (print) {
5285         printf("\n");
5286     }
5287     return;
5288 }
5289
5290 static int
5291 ListAddrs(struct cmd_syndesc *as, void *arock)
5292 {
5293     afs_int32 vcode, m_uniq=0;
5294     afs_int32 i, printuuid = 0;
5295     struct VLCallBack vlcb;
5296     afs_int32 nentries;
5297     bulkaddrs m_addrs;
5298     ListAddrByAttributes m_attrs;
5299     afsUUID m_uuid, askuuid;
5300     afs_int32 m_nentries;
5301
5302     if (as->parms[0].items && as->parms[1].items) {
5303         fprintf(STDERR, "vos: Can't use the -uuid and -host flags together\n");
5304         exit(-1);
5305     }
5306
5307     memset(&m_attrs, 0, sizeof(struct ListAddrByAttributes));
5308     memset(&askuuid, 0, sizeof(afsUUID));
5309     if (as->parms[0].items) {
5310         /* -uuid */
5311         if (afsUUID_from_string(as->parms[0].items->data, &askuuid) < 0) {
5312             fprintf(STDERR, "vos: invalid UUID '%s'\n",
5313                     as->parms[0].items->data);
5314             exit(-1);
5315         }
5316         m_attrs.Mask = VLADDR_UUID;
5317         m_attrs.uuid = askuuid;
5318     } else if (as->parms[1].items) {
5319         /* -host */
5320         struct hostent *he;
5321         afs_uint32 saddr;
5322         he = hostutil_GetHostByName((char *)as->parms[1].items->data);
5323         if (he == NULL) {
5324             fprintf(STDERR, "vos: Can't get host info for '%s'\n",
5325                     as->parms[1].items->data);
5326             exit(-1);
5327         }
5328         memcpy(&saddr, he->h_addr, 4);
5329         m_attrs.Mask = VLADDR_IPADDR;
5330         m_attrs.ipaddr = ntohl(saddr);
5331     } else {
5332         /* by index */
5333         m_attrs.Mask = VLADDR_INDEX;
5334     }
5335
5336     if (as->parms[2].items) {
5337         printuuid = 1;
5338     }
5339
5340     memset(&m_addrs, 0, sizeof(bulkaddrs));
5341     memset(&vlcb, 0, sizeof(struct VLCallBack));
5342
5343     vcode =
5344         ubik_VL_GetAddrs(cstruct, UBIK_CALL_NEW, 0, 0, &vlcb, &nentries,
5345                          &m_addrs);
5346     if (vcode) {
5347         fprintf(STDERR, "vos: could not list the server addresses\n");
5348         PrintError("", vcode);
5349         goto out;
5350     }
5351
5352     for (i = 1, m_nentries = 0; nentries; i++) {
5353         m_attrs.index = i;
5354
5355         xdr_free((xdrproc_t)xdr_bulkaddrs, &m_addrs); /* reset addr list */
5356         vcode =
5357             ubik_VL_GetAddrsU(cstruct, UBIK_CALL_NEW, &m_attrs, &m_uuid,
5358                               &m_uniq, &m_nentries, &m_addrs);
5359         switch (vcode) {
5360         case 0: /* success */
5361             print_addrs(&m_addrs, &m_uuid, m_nentries, printuuid);
5362             nentries--;
5363             break;
5364
5365         case VL_NOENT:
5366             if (m_attrs.Mask == VLADDR_UUID) {
5367                 fprintf(STDERR, "vos: no entry for UUID '%s' found in VLDB\n",
5368                         as->parms[0].items->data);
5369                 exit(-1);
5370             } else if (m_attrs.Mask == VLADDR_IPADDR) {
5371                 fprintf(STDERR, "vos: no entry for host '%s' [0x%08x] found in VLDB\n",
5372                         as->parms[1].items->data, m_attrs.ipaddr);
5373                 exit(-1);
5374             }
5375             continue;
5376
5377         case VL_INDEXERANGE:
5378             vcode = 0; /* not an error, just means we're done */
5379             goto out;
5380
5381         default: /* any other error */
5382             fprintf(STDERR, "vos: could not list the server addresses\n");
5383             PrintError("", vcode);
5384             goto out;
5385         }
5386
5387         /* if -uuid or -host, only list one response */
5388         if (as->parms[1].items || as->parms[0].items)
5389             break;
5390     }
5391
5392 out:
5393     xdr_free((xdrproc_t)xdr_bulkaddrs, &m_addrs);
5394     return vcode;
5395 }
5396
5397
5398 static int
5399 SetAddrs(struct cmd_syndesc *as, void *arock)
5400 {
5401     afs_int32 vcode;
5402     bulkaddrs m_addrs;
5403     afsUUID askuuid;
5404     afs_uint32 FS_HostAddrs_HBO[ADDRSPERSITE];
5405
5406     memset(&m_addrs, 0, sizeof(bulkaddrs));
5407     memset(&askuuid, 0, sizeof(afsUUID));
5408     if (as->parms[0].items) {
5409         /* -uuid */
5410         if (afsUUID_from_string(as->parms[0].items->data, &askuuid) < 0) {
5411             fprintf(STDERR, "vos: invalid UUID '%s'\n",
5412                     as->parms[0].items->data);
5413             exit(-1);
5414         }
5415     }
5416     if (as->parms[1].items) {
5417         /* -host */
5418         struct cmd_item *ti;
5419         afs_uint32 saddr;
5420         int i = 0;
5421
5422         for (ti = as->parms[1].items; ti && i < ADDRSPERSITE; ti = ti->next) {
5423
5424             if (noresolve)
5425                 saddr = GetServerNoresolve(ti->data);
5426             else
5427                 saddr = GetServer(ti->data);
5428
5429             if (!saddr) {
5430                 fprintf(STDERR, "vos: Can't get host info for '%s'\n",
5431                         ti->data);
5432                 exit(-1);
5433             }
5434             /* Convert it to host byte order */
5435             FS_HostAddrs_HBO[i] = ntohl(saddr);
5436             i++;
5437         }
5438         m_addrs.bulkaddrs_len = i;
5439         m_addrs.bulkaddrs_val = FS_HostAddrs_HBO;
5440     }
5441
5442     vcode = ubik_VL_RegisterAddrs(cstruct, 0, &askuuid, 0, &m_addrs);
5443
5444     if (vcode) {
5445         if (vcode == VL_MULTIPADDR) {
5446             fprintf(STDERR, "vos: VL_RegisterAddrs rpc failed; The IP address exists on a different server; repair it\n");
5447             PrintError("", vcode);
5448             return vcode;
5449         } else if (vcode == RXGEN_OPCODE) {
5450             fprintf(STDERR, "vlserver doesn't support VL_RegisterAddrs rpc; ignored\n");
5451             PrintError("", vcode);
5452             return vcode;
5453         }
5454     }
5455     if (verbose) {
5456         fprintf(STDOUT, "vos: Changed UUID with addresses:\n");
5457         print_addrs(&m_addrs, &askuuid, m_addrs.bulkaddrs_len, 1);
5458     }
5459     return 0;
5460 }
5461
5462 static int
5463 LockEntry(struct cmd_syndesc *as, void *arock)
5464 {
5465     afs_uint32 avolid;
5466     afs_int32 vcode, err;
5467
5468     avolid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
5469     if (avolid == 0) {
5470         if (err)
5471             PrintError("", err);
5472         else
5473             fprintf(STDERR, "vos: can't find volume '%s'\n",
5474                     as->parms[0].items->data);
5475         exit(1);
5476     }
5477     vcode = ubik_VL_SetLock(cstruct, 0, avolid, -1, VLOP_DELETE);
5478     if (vcode) {
5479         fprintf(STDERR, "Could not lock VLDB entry for volume %s\n",
5480                 as->parms[0].items->data);
5481         PrintError("", vcode);
5482         exit(1);
5483     }
5484     fprintf(STDOUT, "Locked VLDB entry for volume %s\n",
5485             as->parms[0].items->data);
5486     return 0;
5487 }
5488
5489 static int
5490 ConvertRO(struct cmd_syndesc *as, void *arock)
5491 {
5492     afs_int32 partition = -1;
5493     afs_uint32 volid;
5494     afs_uint32 server;
5495     afs_int32 code, i, same;
5496     struct nvldbentry entry, storeEntry;
5497     afs_int32 vcode;
5498     afs_int32 rwindex = 0;
5499     afs_uint32 rwserver = 0;
5500     afs_int32 rwpartition = 0;
5501     afs_int32 roindex = 0;
5502     afs_uint32 roserver = 0;
5503     afs_int32 ropartition = 0;
5504     int force = 0;
5505     struct rx_connection *aconn;
5506     int c, dc;
5507
5508     server = GetServer(as->parms[0].items->data);
5509     if (!server) {
5510         fprintf(STDERR, "vos: host '%s' not found in host table\n",
5511                 as->parms[0].items->data);
5512         return ENOENT;
5513     }
5514     partition = volutil_GetPartitionID(as->parms[1].items->data);
5515     if (partition < 0) {
5516         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
5517                 as->parms[1].items->data);
5518         return ENOENT;
5519     }
5520     if (!IsPartValid(partition, server, &code)) {
5521         if (code)
5522             PrintError("", code);
5523         else
5524             fprintf(STDERR,
5525                     "vos : partition %s does not exist on the server\n",
5526                     as->parms[1].items->data);
5527         return ENOENT;
5528     }
5529     volid = vsu_GetVolumeID(as->parms[2].items->data, cstruct, &code);
5530     if (volid == 0) {
5531         if (code)
5532             PrintError("", code);
5533         else
5534             fprintf(STDERR, "Unknown volume ID or name '%s'\n",
5535                     as->parms[0].items->data);
5536         return -1;
5537     }
5538     if (as->parms[3].items)
5539         force = 1;
5540
5541     vcode = VLDB_GetEntryByID(volid, -1, &entry);
5542     if (vcode) {
5543         fprintf(STDERR,
5544                 "Could not fetch the entry for volume %lu from VLDB\n",
5545                 (unsigned long)volid);
5546         PrintError("convertROtoRW", code);
5547         return vcode;
5548     }
5549
5550     /* use RO volid even if user specified RW or BK volid */
5551
5552     if (volid != entry.volumeId[ROVOL])
5553         volid = entry.volumeId[ROVOL];
5554
5555     MapHostToNetwork(&entry);
5556     for (i = 0; i < entry.nServers; i++) {
5557         if (entry.serverFlags[i] & ITSRWVOL) {
5558             rwindex = i;
5559             rwserver = entry.serverNumber[i];
5560             rwpartition = entry.serverPartition[i];
5561         }
5562         if (entry.serverFlags[i] & ITSROVOL) {
5563             same = VLDB_IsSameAddrs(server, entry.serverNumber[i], &code);
5564             if (code) {
5565                 fprintf(STDERR,
5566                         "Failed to get info about server's %d address(es) from vlserver (err=%d); aborting call!\n",
5567                         server, code);
5568                 return ENOENT;
5569             }
5570             if (same) {
5571                 roindex = i;
5572                 roserver = entry.serverNumber[i];
5573                 ropartition = entry.serverPartition[i];
5574                 break;
5575             }
5576         }
5577     }
5578     if (!roserver) {
5579         fprintf(STDERR, "Warning: RO volume didn't exist in vldb!\n");
5580     }
5581     if (ropartition != partition) {
5582         fprintf(STDERR,
5583                 "Warning: RO volume should be in partition %d instead of %d (vldb)\n",
5584                 ropartition, partition);
5585     }
5586
5587     if (rwserver) {
5588         fprintf(STDERR,
5589                 "VLDB indicates that a RW volume exists already on %s in partition %s.\n",
5590                 hostutil_GetNameByINet(rwserver),
5591                 volutil_PartitionName(rwpartition));
5592         if (!force) {
5593             fprintf(STDERR, "Overwrite this VLDB entry? [y|n] (n)\n");
5594             dc = c = getchar();
5595             while (!(dc == EOF || dc == '\n'))
5596                 dc = getchar(); /* goto end of line */
5597             if ((c != 'y') && (c != 'Y')) {
5598                 fprintf(STDERR, "aborted.\n");
5599                 return -1;
5600             }
5601         }
5602     }
5603
5604     vcode =
5605         ubik_VL_SetLock(cstruct, 0, entry.volumeId[RWVOL], RWVOL,
5606                   VLOP_MOVE);
5607     aconn = UV_Bind(server, AFSCONF_VOLUMEPORT);
5608     code = AFSVolConvertROtoRWvolume(aconn, partition, volid);
5609     if (code) {
5610         fprintf(STDERR,
5611                 "Converting RO volume %lu to RW volume failed with code %d\n",
5612                 (unsigned long)volid, code);
5613         PrintError("convertROtoRW ", code);
5614         return -1;
5615     }
5616     entry.serverFlags[roindex] = ITSRWVOL;
5617     entry.flags |= RW_EXISTS;
5618     entry.flags &= ~BACK_EXISTS;
5619     if (rwserver) {
5620         (entry.nServers)--;
5621         if (rwindex != entry.nServers) {
5622             entry.serverNumber[rwindex] = entry.serverNumber[entry.nServers];
5623             entry.serverPartition[rwindex] =
5624                 entry.serverPartition[entry.nServers];
5625             entry.serverFlags[rwindex] = entry.serverFlags[entry.nServers];
5626             entry.serverNumber[entry.nServers] = 0;
5627             entry.serverPartition[entry.nServers] = 0;
5628             entry.serverFlags[entry.nServers] = 0;
5629         }
5630     }
5631     entry.flags &= ~RO_EXISTS;
5632     for (i = 0; i < entry.nServers; i++) {
5633         if (entry.serverFlags[i] & ITSROVOL) {
5634             if (!(entry.serverFlags[i] & (RO_DONTUSE | NEW_REPSITE)))
5635                 entry.flags |= RO_EXISTS;
5636         }
5637     }
5638     MapNetworkToHost(&entry, &storeEntry);
5639     code =
5640         VLDB_ReplaceEntry(entry.volumeId[RWVOL], RWVOL, &storeEntry,
5641                           (LOCKREL_OPCODE | LOCKREL_AFSID |
5642                            LOCKREL_TIMESTAMP));
5643     if (code) {
5644         fprintf(STDERR,
5645                 "Warning: volume converted, but vldb update failed with code %d!\n",
5646                 code);
5647     }
5648     vcode = UV_LockRelease(entry.volumeId[RWVOL]);
5649     if (vcode) {
5650         PrintDiagnostics("unlock", vcode);
5651     }
5652     return code;
5653 }
5654
5655 static int
5656 Sizes(struct cmd_syndesc *as, void *arock)
5657 {
5658     afs_uint32 avolid;
5659     afs_uint32 aserver;
5660     afs_int32 apart, voltype, fromdate = 0, code, err, i;
5661     struct nvldbentry entry;
5662     volintSize vol_size;
5663
5664     rx_SetRxDeadTime(60 * 10);
5665     for (i = 0; i < MAXSERVERS; i++) {
5666         struct rx_connection *rxConn = ubik_GetRPCConn(cstruct, i);
5667         if (rxConn == 0)
5668             break;
5669         rx_SetConnDeadTime(rxConn, rx_connDeadTime);
5670         if (rxConn->service)
5671             rxConn->service->connDeadTime = rx_connDeadTime;
5672     }
5673
5674     avolid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
5675     if (avolid == 0) {
5676         if (err)
5677             PrintError("", err);
5678         else
5679             fprintf(STDERR, "vos: can't find volume '%s'\n",
5680                     as->parms[0].items->data);
5681         return ENOENT;
5682     }
5683
5684     if (as->parms[1].items || as->parms[2].items) {
5685         if (!as->parms[1].items || !as->parms[2].items) {
5686             fprintf(STDERR,
5687                     "Must specify both -server and -partition options\n");
5688             return -1;
5689         }
5690         aserver = GetServer(as->parms[2].items->data);
5691         if (aserver == 0) {
5692             fprintf(STDERR, "Invalid server name\n");
5693             return -1;
5694         }
5695         apart = volutil_GetPartitionID(as->parms[1].items->data);
5696         if (apart < 0) {
5697             fprintf(STDERR, "Invalid partition name\n");
5698             return -1;
5699         }
5700     } else {
5701         code = GetVolumeInfo(avolid, &aserver, &apart, &voltype, &entry);
5702         if (code)
5703             return code;
5704     }
5705
5706     fromdate = 0;
5707
5708     if (as->parms[4].items && strcmp(as->parms[4].items->data, "0")) {
5709         code = ktime_DateToInt32(as->parms[4].items->data, &fromdate);
5710         if (code) {
5711             fprintf(STDERR, "vos: failed to parse date '%s' (error=%d))\n",
5712                     as->parms[4].items->data, code);
5713             return code;
5714         }
5715     }
5716
5717     fprintf(STDOUT, "Volume: %s\n", as->parms[0].items->data);
5718
5719     if (as->parms[3].items) {   /* do the dump estimate */
5720         vol_size.dump_size = 0;
5721         code = UV_GetSize(avolid, aserver, apart, fromdate, &vol_size);
5722         if (code) {
5723             PrintDiagnostics("size", code);
5724             return code;
5725         }
5726         /* presumably the size info is now gathered in pntr */
5727         /* now we display it */
5728
5729         fprintf(STDOUT, "dump_size: %llu\n", vol_size.dump_size);
5730     }
5731
5732     /* Display info */
5733
5734     return 0;
5735 }
5736
5737 static int
5738 EndTrans(struct cmd_syndesc *as, void *arock)
5739 {
5740     afs_uint32 server;
5741     afs_int32 code, tid, rcode;
5742     struct rx_connection *aconn;
5743
5744     server = GetServer(as->parms[0].items->data);
5745     if (!server) {
5746         fprintf(STDERR, "vos: host '%s' not found in host table\n",
5747                 as->parms[0].items->data);
5748         return EINVAL;
5749     }
5750
5751     code = util_GetInt32(as->parms[1].items->data, &tid);
5752     if (code) {
5753         fprintf(STDERR, "vos: bad integer specified for transaction ID.\n");
5754         return code;
5755     }
5756
5757     aconn = UV_Bind(server, AFSCONF_VOLUMEPORT);
5758     code = AFSVolEndTrans(aconn, tid, &rcode);
5759     if (!code) {
5760         code = rcode;
5761     }
5762
5763     if (code) {
5764         PrintDiagnostics("endtrans", code);
5765         return 1;
5766     }
5767
5768     return 0;
5769 }
5770
5771 int
5772 PrintDiagnostics(char *astring, afs_int32 acode)
5773 {
5774     if (acode == EACCES) {
5775         fprintf(STDERR,
5776                 "You are not authorized to perform the 'vos %s' command (%d)\n",
5777                 astring, acode);
5778     } else {
5779         fprintf(STDERR, "Error in vos %s command.\n", astring);
5780         PrintError("", acode);
5781     }
5782     return 0;
5783 }
5784
5785
5786 #ifdef AFS_NT40_ENV
5787 static DWORD
5788 win32_enableCrypt(void)
5789 {
5790     HKEY parmKey;
5791     DWORD dummyLen;
5792     DWORD cryptall = 0;
5793     DWORD code;
5794
5795     /* Look up configuration parameters in Registry */
5796     code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
5797                         0, (IsWow64()?KEY_WOW64_64KEY:0)|KEY_QUERY_VALUE, &parmKey);
5798     if (code != ERROR_SUCCESS) {
5799         dummyLen = sizeof(cryptall);
5800         RegQueryValueEx(parmKey, "SecurityLevel", NULL, NULL,
5801                         (BYTE *) &cryptall, &dummyLen);
5802     }
5803     RegCloseKey (parmKey);
5804
5805     return cryptall;
5806 }
5807 #endif /* AFS_NT40_ENV */
5808
5809 static int
5810 MyBeforeProc(struct cmd_syndesc *as, void *arock)
5811 {
5812     char *tcell;
5813     afs_int32 code;
5814     afs_int32 sauth;
5815
5816     /* Initialize the ubik_client connection */
5817     rx_SetRxDeadTime(90);
5818     cstruct = (struct ubik_client *)0;
5819
5820     sauth = 0;
5821     tcell = NULL;
5822     if (as->parms[12].items)    /* if -cell specified */
5823         tcell = as->parms[12].items->data;
5824     if (as->parms[14].items)    /* -serverauth specified */
5825         sauth = 1;
5826     if (as->parms[16].items     /* -encrypt specified */
5827 #ifdef AFS_NT40_ENV
5828         || win32_enableCrypt()
5829 #endif /* AFS_NT40_ENV */
5830          )
5831         vsu_SetCrypt(1);
5832
5833     if (as->parms[18].items)   /* -config flag set */
5834         confdir = as->parms[18].items->data;
5835
5836     if ((code =
5837          vsu_ClientInit((as->parms[13].items != 0), confdir, tcell, sauth,
5838                         &cstruct, UV_SetSecurity))) {
5839         fprintf(STDERR, "could not initialize VLDB library (code=%lu) \n",
5840                 (unsigned long)code);
5841         exit(1);
5842     }
5843     rxInitDone = 1;
5844     if (as->parms[15].items)    /* -verbose flag set */
5845         verbose = 1;
5846     else
5847         verbose = 0;
5848     if (as->parms[17].items)    /* -noresolve flag set */
5849         noresolve = 1;
5850     else
5851         noresolve = 0;
5852
5853     return 0;
5854 }
5855
5856 int
5857 osi_audit(void)
5858 {
5859 /* this sucks but it works for now.
5860 */
5861     return 0;
5862 }
5863
5864 #include "AFS_component_version_number.c"
5865
5866 int
5867 main(int argc, char **argv)
5868 {
5869     afs_int32 code;
5870
5871     struct cmd_syndesc *ts;
5872
5873 #ifdef  AFS_AIX32_ENV
5874     /*
5875      * The following signal action for AIX is necessary so that in case of a
5876      * crash (i.e. core is generated) we can include the user's data section
5877      * in the core dump. Unfortunately, by default, only a partial core is
5878      * generated which, in many cases, isn't too useful.
5879      */
5880     struct sigaction nsa;
5881
5882     sigemptyset(&nsa.sa_mask);
5883     nsa.sa_handler = SIG_DFL;
5884     nsa.sa_flags = SA_FULLDUMP;
5885     sigaction(SIGSEGV, &nsa, NULL);
5886 #endif
5887
5888     confdir = AFSDIR_CLIENT_ETC_DIRPATH;
5889
5890     cmd_SetBeforeProc(MyBeforeProc, NULL);
5891
5892     ts = cmd_CreateSyntax("create", CreateVolume, NULL, "create a new volume");
5893     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
5894     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "partition name");
5895     cmd_AddParm(ts, "-name", CMD_SINGLE, 0, "volume name");
5896     cmd_AddParm(ts, "-maxquota", CMD_SINGLE, CMD_OPTIONAL,
5897                 "initial quota (KB)");
5898     cmd_AddParm(ts, "-id", CMD_SINGLE, CMD_OPTIONAL, "volume ID");
5899     cmd_AddParm(ts, "-roid", CMD_SINGLE, CMD_OPTIONAL, "readonly volume ID");
5900 #ifdef notdef
5901     cmd_AddParm(ts, "-minquota", CMD_SINGLE, CMD_OPTIONAL, "");
5902 #endif
5903     COMMONPARMS;
5904
5905     ts = cmd_CreateSyntax("remove", DeleteVolume, NULL, "delete a volume");
5906     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "machine name");
5907     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
5908     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
5909
5910     COMMONPARMS;
5911
5912     ts = cmd_CreateSyntax("move", MoveVolume, NULL, "move a volume");
5913     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
5914     cmd_AddParm(ts, "-fromserver", CMD_SINGLE, 0, "machine name on source");
5915     cmd_AddParm(ts, "-frompartition", CMD_SINGLE, 0,
5916                 "partition name on source");
5917     cmd_AddParm(ts, "-toserver", CMD_SINGLE, 0,
5918                 "machine name on destination");
5919     cmd_AddParm(ts, "-topartition", CMD_SINGLE, 0,
5920                 "partition name on destination");
5921     cmd_AddParm(ts, "-live", CMD_FLAG, CMD_OPTIONAL,
5922                 "copy live volume without cloning");
5923     COMMONPARMS;
5924
5925     ts = cmd_CreateSyntax("copy", CopyVolume, NULL, "copy a volume");
5926     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID on source");
5927     cmd_AddParm(ts, "-fromserver", CMD_SINGLE, 0, "machine name on source");
5928     cmd_AddParm(ts, "-frompartition", CMD_SINGLE, 0,
5929                 "partition name on source");
5930     cmd_AddParm(ts, "-toname", CMD_SINGLE, 0, "volume name on destination");
5931     cmd_AddParm(ts, "-toserver", CMD_SINGLE, 0,
5932                 "machine name on destination");
5933     cmd_AddParm(ts, "-topartition", CMD_SINGLE, 0,
5934                 "partition name on destination");
5935     cmd_AddParm(ts, "-offline", CMD_FLAG, CMD_OPTIONAL,
5936                 "leave new volume offline");
5937     cmd_AddParm(ts, "-readonly", CMD_FLAG, CMD_OPTIONAL,
5938                 "make new volume read-only");
5939     cmd_AddParm(ts, "-live", CMD_FLAG, CMD_OPTIONAL,
5940                 "copy live volume without cloning");
5941     COMMONPARMS;
5942
5943     ts = cmd_CreateSyntax("shadow", ShadowVolume, NULL,
5944                           "make or update a shadow volume");
5945     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID on source");
5946     cmd_AddParm(ts, "-fromserver", CMD_SINGLE, 0, "machine name on source");
5947     cmd_AddParm(ts, "-frompartition", CMD_SINGLE, 0,
5948                 "partition name on source");
5949     cmd_AddParm(ts, "-toserver", CMD_SINGLE, 0,
5950                 "machine name on destination");
5951     cmd_AddParm(ts, "-topartition", CMD_SINGLE, 0,
5952                 "partition name on destination");
5953     cmd_AddParm(ts, "-toname", CMD_SINGLE, CMD_OPTIONAL,
5954                 "volume name on destination");
5955     cmd_AddParm(ts, "-toid", CMD_SINGLE, CMD_OPTIONAL,
5956                 "volume ID on destination");
5957     cmd_AddParm(ts, "-offline", CMD_FLAG, CMD_OPTIONAL,
5958                 "leave shadow volume offline");
5959     cmd_AddParm(ts, "-readonly", CMD_FLAG, CMD_OPTIONAL,
5960                 "make shadow volume read-only");
5961     cmd_AddParm(ts, "-live", CMD_FLAG, CMD_OPTIONAL,
5962                 "copy live volume without cloning");
5963     cmd_AddParm(ts, "-incremental", CMD_FLAG, CMD_OPTIONAL,
5964                 "do incremental update if target exists");
5965     COMMONPARMS;
5966
5967     ts = cmd_CreateSyntax("backup", BackupVolume, NULL,
5968                           "make backup of a volume");
5969     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
5970     COMMONPARMS;
5971
5972     ts = cmd_CreateSyntax("clone", CloneVolume, NULL,
5973                           "make clone of a volume");
5974     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
5975     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "server");
5976     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition");
5977     cmd_AddParm(ts, "-toname", CMD_SINGLE, CMD_OPTIONAL,
5978                 "volume name on destination");
5979     cmd_AddParm(ts, "-toid", CMD_SINGLE, CMD_OPTIONAL,
5980                 "volume ID on destination");
5981     cmd_AddParm(ts, "-offline", CMD_FLAG, CMD_OPTIONAL,
5982                 "leave clone volume offline");
5983     cmd_AddParm(ts, "-readonly", CMD_FLAG, CMD_OPTIONAL,
5984                 "make clone volume read-only, not readwrite");
5985     cmd_AddParm(ts, "-readwrite", CMD_FLAG, CMD_OPTIONAL,
5986                 "make clone volume readwrite, not read-only");
5987     COMMONPARMS;
5988
5989     ts = cmd_CreateSyntax("release", ReleaseVolume, NULL, "release a volume");
5990     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
5991     cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL,
5992                 "force a complete release");
5993     COMMONPARMS;
5994
5995     ts = cmd_CreateSyntax("dump", DumpVolumeCmd, NULL, "dump a volume");
5996     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
5997     cmd_AddParm(ts, "-time", CMD_SINGLE, CMD_OPTIONAL, "dump from time");
5998     cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL, "dump file");
5999     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "server");
6000     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition");
6001     cmd_AddParm(ts, "-clone", CMD_FLAG, CMD_OPTIONAL,
6002                 "dump a clone of the volume");
6003     cmd_AddParm(ts, "-omitdirs", CMD_FLAG, CMD_OPTIONAL,
6004                 "omit unchanged directories from an incremental dump");
6005     COMMONPARMS;
6006
6007     ts = cmd_CreateSyntax("restore", RestoreVolumeCmd, NULL,
6008                           "restore a volume");
6009     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
6010     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "partition name");
6011     cmd_AddParm(ts, "-name", CMD_SINGLE, 0, "name of volume to be restored");
6012     cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL, "dump file");
6013     cmd_AddParm(ts, "-id", CMD_SINGLE, CMD_OPTIONAL, "volume ID");
6014     cmd_AddParm(ts, "-overwrite", CMD_SINGLE, CMD_OPTIONAL,
6015                 "abort | full | incremental");
6016     cmd_AddParm(ts, "-offline", CMD_FLAG, CMD_OPTIONAL,
6017                 "leave restored volume offline");
6018     cmd_AddParm(ts, "-readonly", CMD_FLAG, CMD_OPTIONAL,
6019                 "make restored volume read-only");
6020     cmd_AddParm(ts, "-creation", CMD_SINGLE, CMD_OPTIONAL,
6021                 "dump | keep | new");
6022     cmd_AddParm(ts, "-lastupdate", CMD_SINGLE, CMD_OPTIONAL,
6023                 "dump | keep | new");
6024     cmd_AddParm(ts, "-nodelete", CMD_FLAG, CMD_OPTIONAL,
6025                 "do not delete old site when restoring to a new site");
6026     COMMONPARMS;
6027
6028     ts = cmd_CreateSyntax("unlock", LockReleaseCmd, NULL,
6029                           "release lock on VLDB entry for a volume");
6030     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6031     COMMONPARMS;
6032
6033     ts = cmd_CreateSyntax("changeloc", ChangeLocation, NULL,
6034                           "change an RW volume's location in the VLDB");
6035     cmd_AddParm(ts, "-server", CMD_SINGLE, 0,
6036                 "machine name for new location");
6037     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0,
6038                 "partition name for new location");
6039     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6040     COMMONPARMS;
6041
6042     ts = cmd_CreateSyntax("addsite", AddSite, NULL, "add a replication site");
6043     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name for new site");
6044     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0,
6045                 "partition name for new site");
6046     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6047     cmd_AddParm(ts, "-roid", CMD_SINGLE, CMD_OPTIONAL, "volume name or ID for RO");
6048     cmd_AddParm(ts, "-valid", CMD_FLAG, CMD_OPTIONAL, "publish as an up-to-date site in VLDB");
6049     COMMONPARMS;
6050
6051     ts = cmd_CreateSyntax("remsite", RemoveSite, NULL,
6052                           "remove a replication site");
6053     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
6054     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "partition name");
6055     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6056     COMMONPARMS;
6057
6058     ts = cmd_CreateSyntax("listpart", ListPartitions, NULL, "list partitions");
6059     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
6060     COMMONPARMS;
6061
6062     ts = cmd_CreateSyntax("listvol", ListVolumes, NULL,
6063                           "list volumes on server (bypass VLDB)");
6064     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
6065     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
6066     cmd_AddParm(ts, "-fast", CMD_FLAG, CMD_OPTIONAL, "minimal listing");
6067     cmd_AddParm(ts, "-long", CMD_FLAG, CMD_OPTIONAL,
6068                 "list all normal volume fields");
6069     cmd_AddParm(ts, "-quiet", CMD_FLAG, CMD_OPTIONAL,
6070                 "generate minimal information");
6071     cmd_AddParm(ts, "-extended", CMD_FLAG, CMD_OPTIONAL,
6072                 "list extended volume fields");
6073     cmd_AddParm(ts, "-format", CMD_FLAG, CMD_OPTIONAL,
6074                 "machine readable format");
6075     COMMONPARMS;
6076
6077     ts = cmd_CreateSyntax("syncvldb", SyncVldb, NULL,
6078                           "synchronize VLDB with server");
6079     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "machine name");
6080     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
6081     cmd_AddParm(ts, "-volume", CMD_SINGLE, CMD_OPTIONAL, "volume name or ID");
6082     cmd_AddParm(ts, "-dryrun", CMD_FLAG, CMD_OPTIONAL, "list what would be done, don't do it");
6083     COMMONPARMS;
6084
6085     ts = cmd_CreateSyntax("syncserv", SyncServer, NULL,
6086                           "synchronize server with VLDB");
6087     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
6088     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
6089     cmd_AddParm(ts, "-dryrun", CMD_FLAG, CMD_OPTIONAL, "list what would be done, don't do it");
6090     COMMONPARMS;
6091
6092     ts = cmd_CreateSyntax("examine", ExamineVolume, NULL,
6093                           "everything about the volume");
6094     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6095     cmd_AddParm(ts, "-extended", CMD_FLAG, CMD_OPTIONAL,
6096                 "list extended volume fields");
6097     cmd_AddParm(ts, "-format", CMD_FLAG, CMD_OPTIONAL,
6098                 "machine readable format");
6099     COMMONPARMS;
6100     cmd_CreateAlias(ts, "volinfo");
6101
6102     ts = cmd_CreateSyntax("setfields", SetFields, NULL,
6103                           "change volume info fields");
6104     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6105     cmd_AddParm(ts, "-maxquota", CMD_SINGLE, CMD_OPTIONAL, "quota (KB)");
6106     cmd_AddParm(ts, "-clearuse", CMD_FLAG, CMD_OPTIONAL, "clear dayUse");
6107     cmd_AddParm(ts, "-clearVolUpCounter", CMD_FLAG, CMD_OPTIONAL, "clear volUpdateCounter");
6108     COMMONPARMS;
6109
6110     ts = cmd_CreateSyntax("offline", volOffline, NULL, "force the volume status to offline");
6111     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "server name");
6112     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "partition name");
6113     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6114     cmd_AddParm(ts, "-sleep", CMD_SINGLE, CMD_OPTIONAL, "seconds to sleep");
6115     cmd_AddParm(ts, "-busy", CMD_FLAG, CMD_OPTIONAL, "busy volume");
6116     COMMONPARMS;
6117
6118     ts = cmd_CreateSyntax("online", volOnline, NULL, "force the volume status to online");
6119     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "server name");
6120     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "partition name");
6121     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6122     COMMONPARMS;
6123
6124     ts = cmd_CreateSyntax("zap", VolumeZap, NULL,
6125                           "delete the volume, don't bother with VLDB");
6126     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
6127     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "partition name");
6128     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume ID");
6129     cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL,
6130                 "force deletion of bad volumes");
6131     cmd_AddParm(ts, "-backup", CMD_FLAG, CMD_OPTIONAL,
6132                 "also delete backup volume if one is found");
6133     COMMONPARMS;
6134
6135     ts = cmd_CreateSyntax("status", VolserStatus, NULL,
6136                           "report on volser status");
6137     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
6138     COMMONPARMS;
6139
6140     ts = cmd_CreateSyntax("rename", RenameVolume, NULL, "rename a volume");
6141     cmd_AddParm(ts, "-oldname", CMD_SINGLE, 0, "old volume name ");
6142     cmd_AddParm(ts, "-newname", CMD_SINGLE, 0, "new volume name ");
6143     COMMONPARMS;
6144
6145     ts = cmd_CreateSyntax("listvldb", ListVLDB, NULL,
6146                           "list volumes in the VLDB");
6147     cmd_AddParm(ts, "-name", CMD_SINGLE, CMD_OPTIONAL, "volume name or ID");
6148     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "machine name");
6149     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
6150     cmd_AddParm(ts, "-locked", CMD_FLAG, CMD_OPTIONAL, "locked volumes only");
6151     cmd_AddParm(ts, "-quiet", CMD_FLAG, CMD_OPTIONAL,
6152                 "generate minimal information");
6153     cmd_AddParm(ts, "-nosort", CMD_FLAG, CMD_OPTIONAL,
6154                 "do not alphabetically sort the volume names");
6155     COMMONPARMS;
6156
6157     ts = cmd_CreateSyntax("backupsys", BackSys, NULL, "en masse backups");
6158     cmd_AddParm(ts, "-prefix", CMD_LIST, CMD_OPTIONAL,
6159                 "common prefix on volume(s)");
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, "-exclude", CMD_FLAG, CMD_OPTIONAL,
6163                 "exclude common prefix volumes");
6164     cmd_AddParm(ts, "-xprefix", CMD_LIST, CMD_OPTIONAL,
6165                 "negative prefix on volume(s)");
6166     cmd_AddParm(ts, "-dryrun", CMD_FLAG, CMD_OPTIONAL, "list what would be done, don't do it");
6167     COMMONPARMS;
6168
6169     ts = cmd_CreateSyntax("delentry", DeleteEntry, NULL,
6170                           "delete VLDB entry for a volume");
6171     cmd_AddParm(ts, "-id", CMD_LIST, CMD_OPTIONAL, "volume name or ID");
6172     cmd_AddParm(ts, "-prefix", CMD_SINGLE, CMD_OPTIONAL,
6173                 "prefix of the volume whose VLDB entry is to be deleted");
6174     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "machine name");
6175     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
6176     cmd_AddParm(ts, "-noexecute", CMD_FLAG, CMD_OPTIONAL|CMD_HIDDEN, "");
6177     cmd_AddParm(ts, "-dryrun", CMD_FLAG, CMD_OPTIONAL,
6178                 "list what would be done, don't do it");
6179     COMMONPARMS;
6180
6181     ts = cmd_CreateSyntax("partinfo", PartitionInfo, NULL,
6182                           "list partition information");
6183     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
6184     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
6185     cmd_AddParm(ts, "-summary", CMD_FLAG, CMD_OPTIONAL,
6186                 "print storage summary");
6187     COMMONPARMS;
6188
6189     ts = cmd_CreateSyntax("unlockvldb", UnlockVLDB, NULL,
6190                           "unlock all the locked entries in the VLDB");
6191     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "machine name");
6192     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
6193     COMMONPARMS;
6194
6195     ts = cmd_CreateSyntax("lock", LockEntry, NULL,
6196                           "lock VLDB entry for a volume");
6197     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6198     COMMONPARMS;
6199
6200     ts = cmd_CreateSyntax("changeaddr", ChangeAddr, NULL,
6201                           "change the IP address of a file server");
6202     cmd_AddParm(ts, "-oldaddr", CMD_SINGLE, 0, "original IP address");
6203     cmd_AddParm(ts, "-newaddr", CMD_SINGLE, CMD_OPTIONAL, "new IP address");
6204     cmd_AddParm(ts, "-remove", CMD_FLAG, CMD_OPTIONAL,
6205                 "remove the IP address from the VLDB");
6206     COMMONPARMS;
6207
6208     ts = cmd_CreateSyntax("listaddrs", ListAddrs, NULL,
6209                           "list the IP address of all file servers registered in the VLDB");
6210     cmd_AddParm(ts, "-uuid", CMD_SINGLE, CMD_OPTIONAL, "uuid of server");
6211     cmd_AddParm(ts, "-host", CMD_SINGLE, CMD_OPTIONAL, "address of host");
6212     cmd_AddParm(ts, "-printuuid", CMD_FLAG, CMD_OPTIONAL,
6213                 "print uuid of hosts");
6214     COMMONPARMS;
6215
6216     ts = cmd_CreateSyntax("convertROtoRW", ConvertRO, NULL,
6217                           "convert a RO volume into a RW volume (after loss of old RW volume)");
6218     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
6219     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "partition name");
6220     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6221     cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL, "don't ask");
6222     COMMONPARMS;
6223
6224     ts = cmd_CreateSyntax("size", Sizes, NULL,
6225                           "obtain various sizes of the volume.");
6226     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6227     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
6228     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "machine name");
6229     cmd_AddParm(ts, "-dump", CMD_FLAG, CMD_OPTIONAL,
6230                 "Obtain the size of the dump");
6231     cmd_AddParm(ts, "-time", CMD_SINGLE, CMD_OPTIONAL, "dump from time");
6232     COMMONPARMS;
6233
6234     ts = cmd_CreateSyntax("endtrans", EndTrans, NULL,
6235                           "end a volserver transaction");
6236     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
6237     cmd_AddParm(ts, "-transaction", CMD_SINGLE, 0,
6238                 "transaction ID");
6239     COMMONPARMS;
6240
6241     ts = cmd_CreateSyntax("setaddrs", SetAddrs, NULL,
6242                           "set the list of IP address for a given UUID in the VLDB");
6243     cmd_AddParm(ts, "-uuid", CMD_SINGLE, 0, "uuid of server");
6244     cmd_AddParm(ts, "-host", CMD_LIST, 0, "address of host");
6245
6246     COMMONPARMS;
6247     code = cmd_Dispatch(argc, argv);
6248     if (rxInitDone) {
6249         /* Shut down the ubik_client and rx connections */
6250         if (cstruct) {
6251             (void)ubik_ClientDestroy(cstruct);
6252             cstruct = 0;
6253         }
6254         rx_Finalize();
6255     }
6256
6257     exit((code ? -1 : 0));
6258 }