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