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