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