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