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