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