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