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