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