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