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