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