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