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