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