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