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