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