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