vos: fix name availability check in vos rename
[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[0].items) {
2085         server = GetServer(as->parms[0].items->data);
2086         if (!server) {
2087             fprintf(STDERR, "vos: server '%s' not found in host table\n",
2088                     as->parms[0].items->data);
2089             return ENOENT;
2090         }
2091     }
2092
2093     if (as->parms[1].items) {
2094         partition = volutil_GetPartitionID(as->parms[1].items->data);
2095         if (partition < 0) {
2096             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2097                     as->parms[1].items->data);
2098             return EINVAL;
2099         }
2100
2101         /* Check for validity of the partition */
2102         if (!IsPartValid(partition, server, &code)) {
2103             if (code) {
2104                 PrintError("", code);
2105             } else {
2106                 fprintf(STDERR,
2107                         "vos : partition %s does not exist on the server\n",
2108                         as->parms[1].items->data);
2109             }
2110             return ENOENT;
2111         }
2112     }
2113
2114     volid = vsu_GetVolumeID(as->parms[2].items->data, cstruct, &err);
2115     if (volid == 0) {
2116         fprintf(STDERR, "Can't find volume name '%s' in VLDB\n",
2117                 as->parms[2].items->data);
2118         if (err)
2119             PrintError("", err);
2120         return ENOENT;
2121     }
2122
2123     /* If the server or partition option are not complete, try to fill
2124      * them in from the VLDB entry.
2125      */
2126     if ((partition == -1) || !server) {
2127         struct nvldbentry entry;
2128
2129         code = VLDB_GetEntryByID(volid, -1, &entry);
2130         if (code) {
2131             fprintf(STDERR,
2132                     "Could not fetch the entry for volume %lu from VLDB\n",
2133                     (unsigned long)volid);
2134             PrintError("", code);
2135             return (code);
2136         }
2137
2138         if (((volid == entry.volumeId[RWVOL]) && (entry.flags & VLF_RWEXISTS))
2139             || ((volid == entry.volumeId[BACKVOL])
2140                 && (entry.flags & VLF_BACKEXISTS))) {
2141             idx = Lp_GetRwIndex(&entry);
2142             if ((idx == -1) || (server && (server != entry.serverNumber[idx]))
2143                 || ((partition != -1)
2144                     && (partition != entry.serverPartition[idx]))) {
2145                 fprintf(STDERR, "VLDB: Volume '%s' no match\n",
2146                         as->parms[2].items->data);
2147                 return ENOENT;
2148             }
2149         } else if ((volid == entry.volumeId[ROVOL])
2150                    && (entry.flags & VLF_ROEXISTS)) {
2151             for (idx = -1, j = 0; j < entry.nServers; j++) {
2152                 if (!(entry.serverFlags[j] & VLSF_ROVOL))
2153                     continue;
2154
2155                 if (((server == 0) || (server == entry.serverNumber[j]))
2156                     && ((partition == -1)
2157                         || (partition == entry.serverPartition[j]))) {
2158                     if (idx != -1) {
2159                         fprintf(STDERR,
2160                                 "VLDB: Volume '%s' matches more than one RO\n",
2161                                 as->parms[2].items->data);
2162                         return ENOENT;
2163                     }
2164                     idx = j;
2165                 }
2166             }
2167             if (idx == -1) {
2168                 fprintf(STDERR, "VLDB: Volume '%s' no match\n",
2169                         as->parms[2].items->data);
2170                 return ENOENT;
2171             }
2172         } else {
2173             fprintf(STDERR, "VLDB: Volume '%s' no match\n",
2174                     as->parms[2].items->data);
2175             return ENOENT;
2176         }
2177
2178         server = htonl(entry.serverNumber[idx]);
2179         partition = entry.serverPartition[idx];
2180     }
2181
2182
2183     code = UV_DeleteVolume(server, partition, volid);
2184     if (code) {
2185         PrintDiagnostics("remove", code);
2186         return code;
2187     }
2188
2189     MapPartIdIntoName(partition, pname);
2190     fprintf(STDOUT, "Volume %lu on partition %s server %s deleted\n",
2191             (unsigned long)volid, pname, hostutil_GetNameByINet(server));
2192     return 0;
2193 }
2194
2195 #define TESTM   0               /* set for move space tests, clear for production */
2196 static int
2197 MoveVolume(struct cmd_syndesc *as, void *arock)
2198 {
2199
2200     afs_uint32 volid;
2201     afs_uint32 fromserver, toserver;
2202     afs_int32 frompart, topart;
2203     afs_int32 flags, code, err;
2204     char fromPartName[10], toPartName[10];
2205
2206     struct diskPartition64 partition;   /* for space check */
2207     volintInfo *p;
2208
2209     volid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
2210     if (volid == 0) {
2211         if (err)
2212             PrintError("", err);
2213         else
2214             fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2215                     as->parms[0].items->data);
2216         return ENOENT;
2217     }
2218     fromserver = GetServer(as->parms[1].items->data);
2219     if (fromserver == 0) {
2220         fprintf(STDERR, "vos: server '%s' not found in host table\n",
2221                 as->parms[1].items->data);
2222         return ENOENT;
2223     }
2224     toserver = GetServer(as->parms[3].items->data);
2225     if (toserver == 0) {
2226         fprintf(STDERR, "vos: server '%s' not found in host table\n",
2227                 as->parms[3].items->data);
2228         return ENOENT;
2229     }
2230     frompart = volutil_GetPartitionID(as->parms[2].items->data);
2231     if (frompart < 0) {
2232         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2233                 as->parms[2].items->data);
2234         return EINVAL;
2235     }
2236     if (!IsPartValid(frompart, fromserver, &code)) {    /*check for validity of the partition */
2237         if (code)
2238             PrintError("", code);
2239         else
2240             fprintf(STDERR,
2241                     "vos : partition %s does not exist on the server\n",
2242                     as->parms[2].items->data);
2243         return ENOENT;
2244     }
2245     topart = volutil_GetPartitionID(as->parms[4].items->data);
2246     if (topart < 0) {
2247         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2248                 as->parms[4].items->data);
2249         return EINVAL;
2250     }
2251     if (!IsPartValid(topart, toserver, &code)) {        /*check for validity of the partition */
2252         if (code)
2253             PrintError("", code);
2254         else
2255             fprintf(STDERR,
2256                     "vos : partition %s does not exist on the server\n",
2257                     as->parms[4].items->data);
2258         return ENOENT;
2259     }
2260
2261     flags = 0;
2262     if (as->parms[5].items) flags |= RV_NOCLONE;
2263
2264     /*
2265      * check source partition for space to clone volume
2266      */
2267
2268     MapPartIdIntoName(topart, toPartName);
2269     MapPartIdIntoName(frompart, fromPartName);
2270
2271     /*
2272      * check target partition for space to move volume
2273      */
2274
2275     code = UV_PartitionInfo64(toserver, toPartName, &partition);
2276     if (code) {
2277         fprintf(STDERR, "vos: cannot access partition %s\n", toPartName);
2278         exit(1);
2279     }
2280     if (TESTM)
2281         fprintf(STDOUT, "target partition %s free space %" AFS_INT64_FMT "\n", toPartName,
2282                 partition.free);
2283
2284     p = (volintInfo *) 0;
2285     code = UV_ListOneVolume(fromserver, frompart, volid, &p);
2286     if (code) {
2287         fprintf(STDERR, "vos:cannot access volume %lu\n",
2288                 (unsigned long)volid);
2289         exit(1);
2290     }
2291     if (TESTM)
2292         fprintf(STDOUT, "volume %lu size %d\n", (unsigned long)volid,
2293                 p->size);
2294     if (partition.free <= p->size) {
2295         fprintf(STDERR,
2296                 "vos: no space on target partition %s to move volume %lu\n",
2297                 toPartName, (unsigned long)volid);
2298         free(p);
2299         exit(1);
2300     }
2301     free(p);
2302
2303     if (TESTM) {
2304         fprintf(STDOUT, "size test - don't do move\n");
2305         exit(0);
2306     }
2307
2308     /* successful move still not guaranteed but shoot for it */
2309
2310     code =
2311         UV_MoveVolume2(volid, fromserver, frompart, toserver, topart, flags);
2312     if (code) {
2313         PrintDiagnostics("move", code);
2314         return code;
2315     }
2316     MapPartIdIntoName(topart, toPartName);
2317     MapPartIdIntoName(frompart, fromPartName);
2318     fprintf(STDOUT, "Volume %lu moved from %s %s to %s %s \n",
2319             (unsigned long)volid, as->parms[1].items->data, fromPartName,
2320             as->parms[3].items->data, toPartName);
2321
2322     return 0;
2323 }
2324
2325 static int
2326 CopyVolume(struct cmd_syndesc *as, void *arock)
2327 {
2328     afs_uint32 volid;
2329     afs_uint32 fromserver, toserver;
2330     afs_int32 frompart, topart, code, err, flags;
2331     char fromPartName[10], toPartName[10], *tovolume;
2332     struct nvldbentry entry;
2333     struct diskPartition64 partition;   /* for space check */
2334     volintInfo *p;
2335
2336     volid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
2337     if (volid == 0) {
2338         if (err)
2339             PrintError("", err);
2340         else
2341             fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2342                     as->parms[0].items->data);
2343         return ENOENT;
2344     }
2345     fromserver = GetServer(as->parms[1].items->data);
2346     if (fromserver == 0) {
2347         fprintf(STDERR, "vos: server '%s' not found in host table\n",
2348                 as->parms[1].items->data);
2349         return ENOENT;
2350     }
2351
2352     toserver = GetServer(as->parms[4].items->data);
2353     if (toserver == 0) {
2354         fprintf(STDERR, "vos: server '%s' not found in host table\n",
2355                 as->parms[4].items->data);
2356         return ENOENT;
2357     }
2358
2359     tovolume = as->parms[3].items->data;
2360     if (!ISNAMEVALID(tovolume)) {
2361         fprintf(STDERR,
2362                 "vos: the name of the root volume %s exceeds the size limit of %d\n",
2363                 tovolume, VOLSER_OLDMAXVOLNAME - 10);
2364         return E2BIG;
2365     }
2366     if (!VolNameOK(tovolume)) {
2367         fprintf(STDERR,
2368                 "Illegal volume name %s, should not end in .readonly or .backup\n",
2369                 tovolume);
2370         return EINVAL;
2371     }
2372     if (IsNumeric(tovolume)) {
2373         fprintf(STDERR, "Illegal volume name %s, should not be a number\n",
2374                 tovolume);
2375         return EINVAL;
2376     }
2377     code = VLDB_GetEntryByName(tovolume, &entry);
2378     if (!code) {
2379         fprintf(STDERR, "Volume %s already exists\n", tovolume);
2380         PrintDiagnostics("copy", code);
2381         return EEXIST;
2382     }
2383
2384     frompart = volutil_GetPartitionID(as->parms[2].items->data);
2385     if (frompart < 0) {
2386         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2387                 as->parms[2].items->data);
2388         return EINVAL;
2389     }
2390     if (!IsPartValid(frompart, fromserver, &code)) {    /*check for validity of the partition */
2391         if (code)
2392             PrintError("", code);
2393         else
2394             fprintf(STDERR,
2395                     "vos : partition %s does not exist on the server\n",
2396                     as->parms[2].items->data);
2397         return ENOENT;
2398     }
2399
2400     topart = volutil_GetPartitionID(as->parms[5].items->data);
2401     if (topart < 0) {
2402         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2403                 as->parms[5].items->data);
2404         return EINVAL;
2405     }
2406     if (!IsPartValid(topart, toserver, &code)) {        /*check for validity of the partition */
2407         if (code)
2408             PrintError("", code);
2409         else
2410             fprintf(STDERR,
2411                     "vos : partition %s does not exist on the server\n",
2412                     as->parms[5].items->data);
2413         return ENOENT;
2414     }
2415
2416     flags = 0;
2417     if (as->parms[6].items) flags |= RV_OFFLINE;
2418     if (as->parms[7].items) flags |= RV_RDONLY;
2419     if (as->parms[8].items) flags |= RV_NOCLONE;
2420
2421     MapPartIdIntoName(topart, toPartName);
2422     MapPartIdIntoName(frompart, fromPartName);
2423
2424     /*
2425      * check target partition for space to move volume
2426      */
2427
2428     code = UV_PartitionInfo64(toserver, toPartName, &partition);
2429     if (code) {
2430         fprintf(STDERR, "vos: cannot access partition %s\n", toPartName);
2431         exit(1);
2432     }
2433     if (TESTM)
2434         fprintf(STDOUT, "target partition %s free space %" AFS_INT64_FMT "\n", toPartName,
2435                 partition.free);
2436
2437     p = (volintInfo *) 0;
2438     code = UV_ListOneVolume(fromserver, frompart, volid, &p);
2439     if (code) {
2440         fprintf(STDERR, "vos:cannot access volume %lu\n",
2441                 (unsigned long)volid);
2442         exit(1);
2443     }
2444
2445     if (partition.free <= p->size) {
2446         fprintf(STDERR,
2447                 "vos: no space on target partition %s to copy volume %lu\n",
2448                 toPartName, (unsigned long)volid);
2449         free(p);
2450         exit(1);
2451     }
2452     free(p);
2453
2454     /* successful copy still not guaranteed but shoot for it */
2455
2456     code =
2457         UV_CopyVolume2(volid, fromserver, frompart, tovolume, toserver,
2458                        topart, 0, flags);
2459     if (code) {
2460         PrintDiagnostics("copy", code);
2461         return code;
2462     }
2463     MapPartIdIntoName(topart, toPartName);
2464     MapPartIdIntoName(frompart, fromPartName);
2465     fprintf(STDOUT, "Volume %lu copied from %s %s to %s on %s %s \n",
2466             (unsigned long)volid, as->parms[1].items->data, fromPartName,
2467             tovolume, as->parms[4].items->data, toPartName);
2468
2469     return 0;
2470 }
2471
2472
2473 static int
2474 ShadowVolume(struct cmd_syndesc *as, void *arock)
2475 {
2476     afs_uint32 volid, tovolid;
2477     afs_uint32 fromserver, toserver;
2478     afs_int32 frompart, topart;
2479     afs_int32 code, err, flags;
2480     char fromPartName[10], toPartName[10], toVolName[32], *tovolume;
2481     struct diskPartition64 partition;   /* for space check */
2482     volintInfo *p, *q;
2483
2484     p = (volintInfo *) 0;
2485     q = (volintInfo *) 0;
2486
2487     volid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
2488     if (volid == 0) {
2489         if (err)
2490             PrintError("", err);
2491         else
2492             fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2493                     as->parms[0].items->data);
2494         return ENOENT;
2495     }
2496     fromserver = GetServer(as->parms[1].items->data);
2497     if (fromserver == 0) {
2498         fprintf(STDERR, "vos: server '%s' not found in host table\n",
2499                 as->parms[1].items->data);
2500         return ENOENT;
2501     }
2502
2503     toserver = GetServer(as->parms[3].items->data);
2504     if (toserver == 0) {
2505         fprintf(STDERR, "vos: server '%s' not found in host table\n",
2506                 as->parms[3].items->data);
2507         return ENOENT;
2508     }
2509
2510     frompart = volutil_GetPartitionID(as->parms[2].items->data);
2511     if (frompart < 0) {
2512         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2513                 as->parms[2].items->data);
2514         return EINVAL;
2515     }
2516     if (!IsPartValid(frompart, fromserver, &code)) {    /*check for validity of the partition */
2517         if (code)
2518             PrintError("", code);
2519         else
2520             fprintf(STDERR,
2521                     "vos : partition %s does not exist on the server\n",
2522                     as->parms[2].items->data);
2523         return ENOENT;
2524     }
2525
2526     topart = volutil_GetPartitionID(as->parms[4].items->data);
2527     if (topart < 0) {
2528         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2529                 as->parms[4].items->data);
2530         return EINVAL;
2531     }
2532     if (!IsPartValid(topart, toserver, &code)) {        /*check for validity of the partition */
2533         if (code)
2534             PrintError("", code);
2535         else
2536             fprintf(STDERR,
2537                     "vos : partition %s does not exist on the server\n",
2538                     as->parms[4].items->data);
2539         return ENOENT;
2540     }
2541
2542     if (as->parms[5].items) {
2543         tovolume = as->parms[5].items->data;
2544         if (!ISNAMEVALID(tovolume)) {
2545             fprintf(STDERR,
2546                 "vos: the name of the root volume %s exceeds the size limit of %d\n",
2547                 tovolume, VOLSER_OLDMAXVOLNAME - 10);
2548             return E2BIG;
2549         }
2550         if (!VolNameOK(tovolume)) {
2551             fprintf(STDERR,
2552                 "Illegal volume name %s, should not end in .readonly or .backup\n",
2553                 tovolume);
2554             return EINVAL;
2555         }
2556         if (IsNumeric(tovolume)) {
2557             fprintf(STDERR,
2558                 "Illegal volume name %s, should not be a number\n",
2559                 tovolume);
2560             return EINVAL;
2561         }
2562     } else {
2563         /* use actual name of source volume */
2564         code = UV_ListOneVolume(fromserver, frompart, volid, &p);
2565         if (code) {
2566             fprintf(STDERR, "vos:cannot access volume %lu\n",
2567                 (unsigned long)volid);
2568             exit(1);
2569         }
2570         strcpy(toVolName, p->name);
2571         tovolume = toVolName;
2572         /* save p for size checks later */
2573     }
2574
2575     if (as->parms[6].items) {
2576         tovolid = vsu_GetVolumeID(as->parms[6].items->data, cstruct, &err);
2577         if (tovolid == 0) {
2578             if (err)
2579                 PrintError("", err);
2580             else
2581                 fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2582                         as->parms[6].items->data);
2583             if (p)
2584                 free(p);
2585             return ENOENT;
2586         }
2587     } else {
2588         tovolid = vsu_GetVolumeID(tovolume, cstruct, &err);
2589         if (tovolid == 0) {
2590             if (err)
2591                 PrintError("", err);
2592             else
2593                 fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2594                         tovolume);
2595             if (p)
2596                 free(p);
2597             return ENOENT;
2598         }
2599     }
2600
2601     flags = RV_NOVLDB;
2602     if (as->parms[7].items) flags |= RV_OFFLINE;
2603     if (as->parms[8].items) flags |= RV_RDONLY;
2604     if (as->parms[9].items) flags |= RV_NOCLONE;
2605     if (as->parms[10].items) flags |= RV_CPINCR;
2606
2607     MapPartIdIntoName(topart, toPartName);
2608     MapPartIdIntoName(frompart, fromPartName);
2609
2610     /*
2611      * check target partition for space to move volume
2612      */
2613
2614     code = UV_PartitionInfo64(toserver, toPartName, &partition);
2615     if (code) {
2616         fprintf(STDERR, "vos: cannot access partition %s\n", toPartName);
2617         exit(1);
2618     }
2619     if (TESTM)
2620         fprintf(STDOUT, "target partition %s free space %" AFS_INT64_FMT "\n", toPartName,
2621                 partition.free);
2622
2623     /* Don't do this again if we did it above */
2624     if (!p) {
2625         code = UV_ListOneVolume(fromserver, frompart, volid, &p);
2626         if (code) {
2627             fprintf(STDERR, "vos:cannot access volume %lu\n",
2628                 (unsigned long)volid);
2629             exit(1);
2630         }
2631     }
2632
2633     /* OK if this fails */
2634     code = UV_ListOneVolume(toserver, topart, tovolid, &q);
2635
2636     /* Treat existing volume size as "free" */
2637     if (q)
2638         p->size = (q->size < p->size) ? p->size - q->size : 0;
2639
2640     if (partition.free <= p->size) {
2641         fprintf(STDERR,
2642                 "vos: no space on target partition %s to copy volume %lu\n",
2643                 toPartName, (unsigned long)volid);
2644         free(p);
2645         if (q) free(q);
2646         exit(1);
2647     }
2648     free(p);
2649     if (q) free(q);
2650
2651     /* successful copy still not guaranteed but shoot for it */
2652
2653     code =
2654         UV_CopyVolume2(volid, fromserver, frompart, tovolume, toserver,
2655                        topart, tovolid, flags);
2656     if (code) {
2657         PrintDiagnostics("shadow", code);
2658         return code;
2659     }
2660     MapPartIdIntoName(topart, toPartName);
2661     MapPartIdIntoName(frompart, fromPartName);
2662     fprintf(STDOUT, "Volume %lu shadowed from %s %s to %s %s \n",
2663             (unsigned long)volid, as->parms[1].items->data, fromPartName,
2664             as->parms[3].items->data, toPartName);
2665
2666     return 0;
2667 }
2668
2669
2670 static int
2671 CloneVolume(struct cmd_syndesc *as, void *arock)
2672 {
2673     afs_uint32 volid, cloneid;
2674     afs_uint32 server;
2675     afs_int32 part, voltype;
2676     char partName[10], *volname;
2677     afs_int32 code, err, flags;
2678     struct nvldbentry entry;
2679
2680     volid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
2681     if (volid == 0) {
2682         if (err)
2683             PrintError("", err);
2684         else
2685             fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2686                     as->parms[0].items->data);
2687         return ENOENT;
2688     }
2689
2690     if (as->parms[1].items || as->parms[2].items) {
2691         if (!as->parms[1].items || !as->parms[2].items) {
2692             fprintf(STDERR,
2693                     "Must specify both -server and -partition options\n");
2694             return -1;
2695         }
2696         server = GetServer(as->parms[1].items->data);
2697         if (server == 0) {
2698             fprintf(STDERR, "vos: server '%s' not found in host table\n",
2699                     as->parms[1].items->data);
2700             return ENOENT;
2701         }
2702         part = volutil_GetPartitionID(as->parms[2].items->data);
2703         if (part < 0) {
2704             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2705                     as->parms[2].items->data);
2706             return EINVAL;
2707         }
2708         if (!IsPartValid(part, server, &code)) {        /*check for validity of the partition */
2709             if (code)
2710                 PrintError("", code);
2711             else
2712                 fprintf(STDERR,
2713                     "vos : partition %s does not exist on the server\n",
2714                     as->parms[2].items->data);
2715             return ENOENT;
2716         }
2717     } else {
2718         code = GetVolumeInfo(volid, &server, &part, &voltype, &entry);
2719         if (code)
2720             return code;
2721     }
2722
2723     volname = 0;
2724     if (as->parms[3].items) {
2725         volname = as->parms[3].items->data;
2726         if (strlen(volname) > VOLSER_OLDMAXVOLNAME - 1) {
2727             fprintf(STDERR,
2728                 "vos: the name of the root volume %s exceeds the size limit of %d\n",
2729                 volname, VOLSER_OLDMAXVOLNAME - 1);
2730             return E2BIG;
2731         }
2732         if (IsNumeric(volname)) {
2733             fprintf(STDERR,
2734                 "Illegal volume name %s, should not be a number\n",
2735                 volname);
2736             return EINVAL;
2737         }
2738     }
2739
2740     cloneid = 0;
2741     if (as->parms[4].items) {
2742         cloneid = vsu_GetVolumeID(as->parms[4].items->data, cstruct, &err);
2743         if (cloneid == 0) {
2744             if (err)
2745                 PrintError("", err);
2746             else
2747                 fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2748                         as->parms[4].items->data);
2749             return ENOENT;
2750         }
2751     }
2752
2753     flags = 0;
2754     if (as->parms[5].items) flags |= RV_OFFLINE;
2755     if (as->parms[6].items && as->parms[7].items) {
2756         fprintf(STDERR, "vos: cannot specify that a volume be -readwrite and -readonly\n");
2757         return EINVAL;
2758     }
2759     if (as->parms[6].items) flags |= RV_RDONLY;
2760     if (as->parms[7].items) flags |= RV_RWONLY;
2761
2762
2763     code =
2764         UV_CloneVolume(server, part, volid, cloneid, volname, flags);
2765
2766     if (code) {
2767         PrintDiagnostics("clone", code);
2768         return code;
2769     }
2770     MapPartIdIntoName(part, partName);
2771     fprintf(STDOUT, "Created clone for volume %s\n",
2772             as->parms[0].items->data);
2773
2774     return 0;
2775 }
2776
2777
2778 static int
2779 BackupVolume(struct cmd_syndesc *as, void *arock)
2780 {
2781     afs_uint32 avolid;
2782     afs_uint32 aserver;
2783     afs_int32 apart, vtype, code, err;
2784     struct nvldbentry entry;
2785
2786     afs_uint32 buvolid;
2787     afs_uint32 buserver;
2788     afs_int32 bupart, butype;
2789     struct nvldbentry buentry;
2790
2791     avolid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
2792     if (avolid == 0) {
2793         if (err)
2794             PrintError("", err);
2795         else
2796             fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2797                     as->parms[0].items->data);
2798         return ENOENT;
2799     }
2800     code = GetVolumeInfo(avolid, &aserver, &apart, &vtype, &entry);
2801     if (code)
2802         exit(1);
2803
2804     /* verify this is a readwrite volume */
2805
2806     if (vtype != RWVOL) {
2807         fprintf(STDERR, "%s not RW volume\n", as->parms[0].items->data);
2808         exit(1);
2809     }
2810
2811     /* is there a backup volume already? */
2812
2813     if (entry.flags & VLF_BACKEXISTS) {
2814         /* yep, where is it? */
2815
2816         buvolid = entry.volumeId[BACKVOL];
2817         code = GetVolumeInfo(buvolid, &buserver, &bupart, &butype, &buentry);
2818         if (code)
2819             exit(1);
2820
2821         /* is it local? */
2822         code = VLDB_IsSameAddrs(buserver, aserver, &err);
2823         if (err) {
2824             fprintf(STDERR,
2825                     "Failed to get info about server's %d address(es) from vlserver; aborting call!\n",
2826                     buserver);
2827             exit(1);
2828         }
2829         if (!code) {
2830             fprintf(STDERR,
2831                     "FATAL ERROR: backup volume %lu exists on server %lu\n",
2832                     (unsigned long)buvolid, (unsigned long)buserver);
2833             exit(1);
2834         }
2835     }
2836
2837     /* nope, carry on */
2838
2839     code = UV_BackupVolume(aserver, apart, avolid);
2840
2841     if (code) {
2842         PrintDiagnostics("backup", code);
2843         return code;
2844     }
2845     fprintf(STDOUT, "Created backup volume for %s \n",
2846             as->parms[0].items->data);
2847     return 0;
2848 }
2849
2850 static int
2851 ReleaseVolume(struct cmd_syndesc *as, void *arock)
2852 {
2853
2854     struct nvldbentry entry;
2855     afs_uint32 avolid;
2856     afs_uint32 aserver;
2857     afs_int32 apart, vtype, code, err;
2858     int flags = 0;
2859
2860     if (as->parms[1].items) /* -force */
2861         flags |= (REL_COMPLETE | REL_FULLDUMPS);
2862     if (as->parms[2].items) { /* -stayonline */
2863         fprintf(STDERR, "vos: -stayonline not supported\n");
2864         return EINVAL;
2865     }
2866     if (as->parms[3].items) /* -force-reclone */
2867         flags |= REL_COMPLETE;
2868
2869     avolid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
2870     if (avolid == 0) {
2871         if (err)
2872             PrintError("", err);
2873         else
2874             fprintf(STDERR, "vos: can't find volume '%s'\n",
2875                     as->parms[0].items->data);
2876         return ENOENT;
2877     }
2878     code = GetVolumeInfo(avolid, &aserver, &apart, &vtype, &entry);
2879     if (code)
2880         return code;
2881
2882     if (vtype != RWVOL) {
2883         fprintf(STDERR, "%s not a RW volume\n", as->parms[0].items->data);
2884         return (ENOENT);
2885     }
2886
2887     if (!ISNAMEVALID(entry.name)) {
2888         fprintf(STDERR,
2889                 "Volume name %s is too long, rename before releasing\n",
2890                 entry.name);
2891         return E2BIG;
2892     }
2893
2894     code = UV_ReleaseVolume(avolid, aserver, apart, flags);
2895
2896     if (code) {
2897         PrintDiagnostics("release", code);
2898         return code;
2899     }
2900     fprintf(STDOUT, "Released volume %s successfully\n",
2901             as->parms[0].items->data);
2902     return 0;
2903 }
2904
2905 static int
2906 DumpVolumeCmd(struct cmd_syndesc *as, void *arock)
2907 {
2908     afs_uint32 avolid;
2909     afs_uint32 aserver;
2910     afs_int32 apart, voltype, fromdate = 0, code, err, i, flags;
2911     char filename[MAXPATHLEN];
2912     struct nvldbentry entry;
2913
2914     rx_SetRxDeadTime(60 * 10);
2915     for (i = 0; i < MAXSERVERS; i++) {
2916         struct rx_connection *rxConn = ubik_GetRPCConn(cstruct, i);
2917         if (rxConn == 0)
2918             break;
2919         rx_SetConnDeadTime(rxConn, rx_connDeadTime);
2920         if (rx_ServiceOf(rxConn))
2921             rx_ServiceOf(rxConn)->connDeadTime = rx_connDeadTime;
2922     }
2923
2924     avolid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
2925     if (avolid == 0) {
2926         if (err)
2927             PrintError("", err);
2928         else
2929             fprintf(STDERR, "vos: can't find volume '%s'\n",
2930                     as->parms[0].items->data);
2931         return ENOENT;
2932     }
2933
2934     if (as->parms[3].items || as->parms[4].items) {
2935         if (!as->parms[3].items || !as->parms[4].items) {
2936             fprintf(STDERR,
2937                     "Must specify both -server and -partition options\n");
2938             return -1;
2939         }
2940         aserver = GetServer(as->parms[3].items->data);
2941         if (aserver == 0) {
2942             fprintf(STDERR, "Invalid server name\n");
2943             return -1;
2944         }
2945         apart = volutil_GetPartitionID(as->parms[4].items->data);
2946         if (apart < 0) {
2947             fprintf(STDERR, "Invalid partition name\n");
2948             return -1;
2949         }
2950     } else {
2951         code = GetVolumeInfo(avolid, &aserver, &apart, &voltype, &entry);
2952         if (code)
2953             return code;
2954     }
2955
2956     if (as->parms[1].items && strcmp(as->parms[1].items->data, "0")) {
2957         code = ktime_DateToInt32(as->parms[1].items->data, &fromdate);
2958         if (code) {
2959             fprintf(STDERR, "vos: failed to parse date '%s' (error=%d))\n",
2960                     as->parms[1].items->data, code);
2961             return code;
2962         }
2963     }
2964     if (as->parms[2].items) {
2965         strcpy(filename, as->parms[2].items->data);
2966     } else {
2967         strcpy(filename, "");
2968     }
2969
2970     flags = as->parms[6].items ? VOLDUMPV2_OMITDIRS : 0;
2971 retry_dump:
2972     if (as->parms[5].items) {
2973         code =
2974             UV_DumpClonedVolume(avolid, aserver, apart, fromdate,
2975                                 DumpFunction, filename, flags);
2976     } else {
2977         code =
2978             UV_DumpVolume(avolid, aserver, apart, fromdate, DumpFunction,
2979                           filename, flags);
2980     }
2981     if ((code == RXGEN_OPCODE) && (as->parms[6].items)) {
2982         flags &= ~VOLDUMPV2_OMITDIRS;
2983         goto retry_dump;
2984     }
2985     if (code) {
2986         PrintDiagnostics("dump", code);
2987         return code;
2988     }
2989     if (strcmp(filename, ""))
2990         fprintf(STDERR, "Dumped volume %s in file %s\n",
2991                 as->parms[0].items->data, filename);
2992     else
2993         fprintf(STDERR, "Dumped volume %s in stdout \n",
2994                 as->parms[0].items->data);
2995     return 0;
2996 }
2997
2998 #define ASK   0
2999 #define ABORT 1
3000 #define FULL  2
3001 #define INC   3
3002
3003 #define TS_DUMP 1
3004 #define TS_KEEP 2
3005 #define TS_NEW  3
3006
3007 static int
3008 RestoreVolumeCmd(struct cmd_syndesc *as, void *arock)
3009 {
3010     afs_uint32 avolid, aparentid;
3011     afs_uint32 aserver;
3012     afs_int32 apart, code, vcode, err;
3013     afs_int32 aoverwrite = ASK;
3014     afs_int32 acreation = 0, alastupdate = 0;
3015     int restoreflags = 0;
3016     int readonly = 0, offline = 0, voltype = RWVOL;
3017     char afilename[MAXPATHLEN], avolname[VOLSER_MAXVOLNAME + 1], apartName[10];
3018     char volname[VOLSER_MAXVOLNAME + 1];
3019     struct nvldbentry entry;
3020
3021     aparentid = 0;
3022     if (as->parms[4].items) {
3023         avolid = vsu_GetVolumeID(as->parms[4].items->data, cstruct, &err);
3024         if (avolid == 0) {
3025             if (err)
3026                 PrintError("", err);
3027             else
3028                 fprintf(STDERR, "vos: can't find volume '%s'\n",
3029                         as->parms[4].items->data);
3030             exit(1);
3031         }
3032     } else
3033         avolid = 0;
3034
3035     if (as->parms[5].items) {
3036         if ((strcmp(as->parms[5].items->data, "a") == 0)
3037             || (strcmp(as->parms[5].items->data, "abort") == 0)) {
3038             aoverwrite = ABORT;
3039         } else if ((strcmp(as->parms[5].items->data, "f") == 0)
3040                    || (strcmp(as->parms[5].items->data, "full") == 0)) {
3041             aoverwrite = FULL;
3042         } else if ((strcmp(as->parms[5].items->data, "i") == 0)
3043                    || (strcmp(as->parms[5].items->data, "inc") == 0)
3044                    || (strcmp(as->parms[5].items->data, "increment") == 0)
3045                    || (strcmp(as->parms[5].items->data, "incremental") == 0)) {
3046             aoverwrite = INC;
3047         } else {
3048             fprintf(STDERR, "vos: %s is not a valid argument to -overwrite\n",
3049                     as->parms[5].items->data);
3050             exit(1);
3051         }
3052     }
3053     if (as->parms[6].items)
3054         offline = 1;
3055     if (as->parms[7].items) {
3056         readonly = 1;
3057         voltype = ROVOL;
3058     }
3059
3060     if (as->parms[8].items) {
3061         if ((strcmp(as->parms[8].items->data, "d") == 0)
3062             || (strcmp(as->parms[8].items->data, "dump") == 0)) {
3063             acreation = TS_DUMP;
3064         } else if ((strcmp(as->parms[8].items->data, "k") == 0)
3065             || (strcmp(as->parms[8].items->data, "keep") == 0)) {
3066             acreation = TS_KEEP;
3067         } else if ((strcmp(as->parms[8].items->data, "n") == 0)
3068             || (strcmp(as->parms[8].items->data, "new") == 0)) {
3069             acreation = TS_NEW;
3070         } else {
3071             fprintf(STDERR, "vos: %s is not a valid argument to -creation\n",
3072                     as->parms[8].items->data);
3073             exit(1);
3074         }
3075     }
3076
3077     if (as->parms[9].items) {
3078         if ((strcmp(as->parms[9].items->data, "d") == 0)
3079             || (strcmp(as->parms[9].items->data, "dump") == 0)) {
3080             alastupdate = TS_DUMP;
3081         } else if ((strcmp(as->parms[9].items->data, "k") == 0)
3082             || (strcmp(as->parms[9].items->data, "keep") == 0)) {
3083             alastupdate = TS_KEEP;
3084         } else if ((strcmp(as->parms[9].items->data, "n") == 0)
3085             || (strcmp(as->parms[9].items->data, "new") == 0)) {
3086             alastupdate = TS_NEW;
3087         } else {
3088             fprintf(STDERR, "vos: %s is not a valid argument to -lastupdate\n",
3089                     as->parms[9].items->data);
3090             exit(1);
3091         }
3092     }
3093
3094     aserver = GetServer(as->parms[0].items->data);
3095     if (aserver == 0) {
3096         fprintf(STDERR, "vos: server '%s' not found in host table\n",
3097                 as->parms[0].items->data);
3098         exit(1);
3099     }
3100     apart = volutil_GetPartitionID(as->parms[1].items->data);
3101     if (apart < 0) {
3102         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3103                 as->parms[1].items->data);
3104         exit(1);
3105     }
3106     if (!IsPartValid(apart, aserver, &code)) {  /*check for validity of the partition */
3107         if (code)
3108             PrintError("", code);
3109         else
3110             fprintf(STDERR,
3111                     "vos : partition %s does not exist on the server\n",
3112                     as->parms[1].items->data);
3113         exit(1);
3114     }
3115     strcpy(avolname, as->parms[2].items->data);
3116     if (!ISNAMEVALID(avolname)) {
3117         fprintf(STDERR,
3118                 "vos: the name of the volume %s exceeds the size limit\n",
3119                 avolname);
3120         exit(1);
3121     }
3122     if (!VolNameOK(avolname)) {
3123         fprintf(STDERR,
3124                 "Illegal volume name %s, should not end in .readonly or .backup\n",
3125                 avolname);
3126         exit(1);
3127     }
3128     if (as->parms[3].items) {
3129         strcpy(afilename, as->parms[3].items->data);
3130         if (!FileExists(afilename)) {
3131             fprintf(STDERR, "Can't access file %s\n", afilename);
3132             exit(1);
3133         }
3134     } else {
3135         strcpy(afilename, "");
3136     }
3137
3138     /* Check if volume exists or not */
3139
3140     vsu_ExtractName(volname, avolname);
3141     vcode = VLDB_GetEntryByName(volname, &entry);
3142     if (vcode) {                /* no volume - do a full restore */
3143         restoreflags = RV_FULLRST;
3144         if ((aoverwrite == INC) || (aoverwrite == ABORT))
3145             fprintf(STDERR,
3146                     "Volume does not exist; Will perform a full restore\n");
3147     }
3148
3149     else if ((!readonly && Lp_GetRwIndex(&entry) == -1) /* RW volume does not exist - do a full */
3150              ||(readonly && !Lp_ROMatch(0, 0, &entry))) {       /* RO volume does not exist - do a full */
3151         restoreflags = RV_FULLRST;
3152         if ((aoverwrite == INC) || (aoverwrite == ABORT))
3153             fprintf(STDERR,
3154                     "%s Volume does not exist; Will perform a full restore\n",
3155                     readonly ? "RO" : "RW");
3156
3157         if (avolid == 0) {
3158             avolid = entry.volumeId[voltype];
3159         } else if (entry.volumeId[voltype] != 0
3160                    && entry.volumeId[voltype] != avolid) {
3161             avolid = entry.volumeId[voltype];
3162         }
3163         aparentid = entry.volumeId[RWVOL];
3164     }
3165
3166     else {                      /* volume exists - do we do a full incremental or abort */
3167         afs_uint32 Oserver;
3168         afs_int32 Opart, Otype, vol_elsewhere = 0;
3169         struct nvldbentry Oentry;
3170         int c, dc;
3171
3172         if (avolid == 0) {
3173             avolid = entry.volumeId[voltype];
3174         } else if (entry.volumeId[voltype] != 0
3175                    && entry.volumeId[voltype] != avolid) {
3176             avolid = entry.volumeId[voltype];
3177         }
3178         aparentid = entry.volumeId[RWVOL];
3179
3180         /* A file name was specified  - check if volume is on another partition */
3181         vcode = GetVolumeInfo(avolid, &Oserver, &Opart, &Otype, &Oentry);
3182         if (vcode)
3183             exit(1);
3184
3185         vcode = VLDB_IsSameAddrs(Oserver, aserver, &err);
3186         if (err) {
3187             fprintf(STDERR,
3188                     "Failed to get info about server's %d address(es) from vlserver (err=%d); aborting call!\n",
3189                     Oserver, err);
3190             exit(1);
3191         }
3192         if (!vcode || (Opart != apart))
3193             vol_elsewhere = 1;
3194
3195         if (aoverwrite == ASK) {
3196             if (strcmp(afilename, "") == 0) {   /* The file is from standard in */
3197                 fprintf(STDERR,
3198                         "Volume exists and no -overwrite option specified; Aborting restore command\n");
3199                 exit(1);
3200             }
3201
3202             /* Ask what to do */
3203             if (vol_elsewhere) {
3204                 fprintf(STDERR,
3205                         "The volume %s %u already exists on a different server/part\n",
3206                         volname, entry.volumeId[voltype]);
3207                 fprintf(STDERR,
3208                         "Do you want to do a full restore or abort? [fa](a): ");
3209             } else {
3210                 fprintf(STDERR,
3211                         "The volume %s %u already exists in the VLDB\n",
3212                         volname, entry.volumeId[voltype]);
3213                 fprintf(STDERR,
3214                         "Do you want to do a full/incremental restore or abort? [fia](a): ");
3215             }
3216             dc = c = getchar();
3217             while (!(dc == EOF || dc == '\n'))
3218                 dc = getchar(); /* goto end of line */
3219             if ((c == 'f') || (c == 'F'))
3220                 aoverwrite = FULL;
3221             else if ((c == 'i') || (c == 'I'))
3222                 aoverwrite = INC;
3223             else
3224                 aoverwrite = ABORT;
3225         }
3226
3227         if (aoverwrite == ABORT) {
3228             fprintf(STDERR, "Volume exists; Aborting restore command\n");
3229             exit(1);
3230         } else if (aoverwrite == FULL) {
3231             restoreflags = RV_FULLRST;
3232             fprintf(STDERR,
3233                     "Volume exists; Will delete and perform full restore\n");
3234         } else if (aoverwrite == INC) {
3235             restoreflags = 0;
3236             if (vol_elsewhere) {
3237                 fprintf(STDERR,
3238                         "%s volume %lu already exists on a different server/part; not allowed\n",
3239                         readonly ? "RO" : "RW", (unsigned long)avolid);
3240                 exit(1);
3241             }
3242         }
3243     }
3244     if (offline)
3245         restoreflags |= RV_OFFLINE;
3246     if (readonly)
3247         restoreflags |= RV_RDONLY;
3248
3249     switch (acreation) {
3250         case TS_DUMP:
3251             restoreflags |= RV_CRDUMP;
3252             break;
3253         case TS_KEEP:
3254             restoreflags |= RV_CRKEEP;
3255             break;
3256         case TS_NEW:
3257             restoreflags |= RV_CRNEW;
3258             break;
3259         default:
3260             if (aoverwrite == FULL)
3261                 restoreflags |= RV_CRNEW;
3262             else
3263                 restoreflags |= RV_CRKEEP;
3264     }
3265
3266     switch (alastupdate) {
3267         case TS_DUMP:
3268             restoreflags |= RV_LUDUMP;
3269             break;
3270         case TS_KEEP:
3271             restoreflags |= RV_LUKEEP;
3272             break;
3273         case TS_NEW:
3274             restoreflags |= RV_LUNEW;
3275             break;
3276         default:
3277             restoreflags |= RV_LUDUMP;
3278     }
3279     if (as->parms[10].items) {
3280         restoreflags |= RV_NODEL;
3281     }
3282
3283
3284     code =
3285         UV_RestoreVolume2(aserver, apart, avolid, aparentid,
3286                           avolname, restoreflags, WriteData, afilename);
3287     if (code) {
3288         PrintDiagnostics("restore", code);
3289         exit(1);
3290     }
3291     MapPartIdIntoName(apart, apartName);
3292
3293     /*
3294      * patch typo here - originally "parms[1]", should be "parms[0]"
3295      */
3296
3297     fprintf(STDOUT, "Restored volume %s on %s %s\n", avolname,
3298             as->parms[0].items->data, apartName);
3299     return 0;
3300 }
3301
3302 static int
3303 LockReleaseCmd(struct cmd_syndesc *as, void *arock)
3304 {
3305     afs_uint32 avolid;
3306     afs_int32 code, err;
3307
3308     avolid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
3309     if (avolid == 0) {
3310         if (err)
3311             PrintError("", err);
3312         else
3313             fprintf(STDERR, "vos: can't find volume '%s'\n",
3314                     as->parms[0].items->data);
3315         exit(1);
3316     }
3317
3318     code = UV_LockRelease(avolid);
3319     if (code) {
3320         PrintDiagnostics("unlock", code);
3321         exit(1);
3322     }
3323     fprintf(STDOUT, "Released lock on vldb entry for volume %s\n",
3324             as->parms[0].items->data);
3325     return 0;
3326 }
3327
3328 static int
3329 AddSite(struct cmd_syndesc *as, void *arock)
3330 {
3331     afs_uint32 avolid;
3332     afs_uint32 aserver;
3333     afs_int32 apart, code, err, arovolid, valid = 0;
3334     char apartName[10], avolname[VOLSER_MAXVOLNAME + 1];
3335
3336     vsu_ExtractName(avolname, as->parms[2].items->data);;
3337     avolid = vsu_GetVolumeID(avolname, cstruct, &err);
3338     if (avolid == 0) {
3339         if (err)
3340             PrintError("", err);
3341         else
3342             fprintf(STDERR, "vos: can't find volume '%s'\n",
3343                     as->parms[2].items->data);
3344         exit(1);
3345     }
3346     arovolid = 0;
3347     if (as->parms[3].items) {
3348         vsu_ExtractName(avolname, as->parms[3].items->data);
3349         arovolid = vsu_GetVolumeID(avolname, cstruct, &err);
3350         if (!arovolid) {
3351             fprintf(STDERR, "vos: invalid ro volume id '%s'\n",
3352                     as->parms[3].items->data);
3353             exit(1);
3354         }
3355     }
3356     aserver = GetServer(as->parms[0].items->data);
3357     if (aserver == 0) {
3358         fprintf(STDERR, "vos: server '%s' not found in host table\n",
3359                 as->parms[0].items->data);
3360         exit(1);
3361     }
3362     apart = volutil_GetPartitionID(as->parms[1].items->data);
3363     if (apart < 0) {
3364         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3365                 as->parms[1].items->data);
3366         exit(1);
3367     }
3368     if (!IsPartValid(apart, aserver, &code)) {  /*check for validity of the partition */
3369         if (code)
3370             PrintError("", code);
3371         else
3372             fprintf(STDERR,
3373                     "vos : partition %s does not exist on the server\n",
3374                     as->parms[1].items->data);
3375         exit(1);
3376     }
3377     if (as->parms[4].items) {
3378         valid = 1;
3379     }
3380     code = UV_AddSite2(aserver, apart, avolid, arovolid, valid);
3381     if (code) {
3382         PrintDiagnostics("addsite", code);
3383         exit(1);
3384     }
3385     MapPartIdIntoName(apart, apartName);
3386     fprintf(STDOUT, "Added replication site %s %s for volume %s\n",
3387             as->parms[0].items->data, apartName, as->parms[2].items->data);
3388     return 0;
3389 }
3390
3391 static int
3392 RemoveSite(struct cmd_syndesc *as, void *arock)
3393 {
3394
3395     afs_uint32 avolid;
3396     afs_uint32 aserver;
3397     afs_int32 apart, code, err;
3398     char apartName[10], avolname[VOLSER_MAXVOLNAME + 1];
3399
3400     vsu_ExtractName(avolname, as->parms[2].items->data);
3401     avolid = vsu_GetVolumeID(avolname, cstruct, &err);
3402     if (avolid == 0) {
3403         if (err)
3404             PrintError("", err);
3405         else
3406             fprintf(STDERR, "vos: can't find volume '%s'\n",
3407                     as->parms[2].items->data);
3408         exit(1);
3409     }
3410     aserver = GetServer(as->parms[0].items->data);
3411     if (aserver == 0) {
3412         fprintf(STDERR, "vos: server '%s' not found in host table\n",
3413                 as->parms[0].items->data);
3414         exit(1);
3415     }
3416     apart = volutil_GetPartitionID(as->parms[1].items->data);
3417     if (apart < 0) {
3418         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3419                 as->parms[1].items->data);
3420         exit(1);
3421     }
3422 /*
3423  *skip the partition validity check, since it is possible that the partition
3424  *has since been decomissioned.
3425  */
3426 /*
3427         if (!IsPartValid(apart,aserver,&code)){
3428             if(code) PrintError("",code);
3429             else fprintf(STDERR,"vos : partition %s does not exist on the server\n",as->parms[1].items->data);
3430             exit(1);
3431         }
3432 */
3433     code = UV_RemoveSite(aserver, apart, avolid);
3434     if (code) {
3435         PrintDiagnostics("remsite", code);
3436         exit(1);
3437     }
3438     MapPartIdIntoName(apart, apartName);
3439     fprintf(STDOUT, "Removed replication site %s %s for volume %s\n",
3440             as->parms[0].items->data, apartName, as->parms[2].items->data);
3441     return 0;
3442 }
3443
3444 static int
3445 ChangeLocation(struct cmd_syndesc *as, void *arock)
3446 {
3447     afs_uint32 avolid;
3448     afs_uint32 aserver;
3449     afs_int32 apart, code, err;
3450     char apartName[10];
3451
3452     avolid = vsu_GetVolumeID(as->parms[2].items->data, cstruct, &err);
3453     if (avolid == 0) {
3454         if (err)
3455             PrintError("", err);
3456         else
3457             fprintf(STDERR, "vos: can't find volume '%s'\n",
3458                     as->parms[2].items->data);
3459         exit(1);
3460     }
3461     aserver = GetServer(as->parms[0].items->data);
3462     if (aserver == 0) {
3463         fprintf(STDERR, "vos: server '%s' not found in host table\n",
3464                 as->parms[0].items->data);
3465         exit(1);
3466     }
3467     apart = volutil_GetPartitionID(as->parms[1].items->data);
3468     if (apart < 0) {
3469         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3470                 as->parms[1].items->data);
3471         exit(1);
3472     }
3473     if (!IsPartValid(apart, aserver, &code)) {  /*check for validity of the partition */
3474         if (code)
3475             PrintError("", code);
3476         else
3477             fprintf(STDERR,
3478                     "vos : partition %s does not exist on the server\n",
3479                     as->parms[1].items->data);
3480         exit(1);
3481     }
3482     code = UV_ChangeLocation(aserver, apart, avolid);
3483     if (code) {
3484         PrintDiagnostics("changeloc", code);
3485         exit(1);
3486     }
3487     MapPartIdIntoName(apart, apartName);
3488     fprintf(STDOUT, "Changed location to %s %s for volume %s\n",
3489             as->parms[0].items->data, apartName, as->parms[2].items->data);
3490     return 0;
3491 }
3492
3493 static int
3494 ListPartitions(struct cmd_syndesc *as, void *arock)
3495 {
3496     afs_uint32 aserver;
3497     afs_int32 code;
3498     struct partList dummyPartList;
3499     int i;
3500     char pname[10];
3501     int total, cnt;
3502
3503     aserver = GetServer(as->parms[0].items->data);
3504     if (aserver == 0) {
3505         fprintf(STDERR, "vos: server '%s' not found in host table\n",
3506                 as->parms[0].items->data);
3507         exit(1);
3508     }
3509
3510
3511     code = UV_ListPartitions(aserver, &dummyPartList, &cnt);
3512     if (code) {
3513         PrintDiagnostics("listpart", code);
3514         exit(1);
3515     }
3516     total = 0;
3517     fprintf(STDOUT, "The partitions on the server are:\n");
3518     for (i = 0; i < cnt; i++) {
3519         if (dummyPartList.partFlags[i] & PARTVALID) {
3520             memset(pname, 0, sizeof(pname));
3521             MapPartIdIntoName(dummyPartList.partId[i], pname);
3522             fprintf(STDOUT, " %10s ", pname);
3523             total++;
3524             if ((i % 5) == 0 && (i != 0))
3525                 fprintf(STDOUT, "\n");
3526         }
3527     }
3528     fprintf(STDOUT, "\n");
3529     fprintf(STDOUT, "Total: %d\n", total);
3530     return 0;
3531
3532 }
3533
3534 static int
3535 CompareVolName(const void *p1, const void *p2)
3536 {
3537     volintInfo *arg1, *arg2;
3538
3539     arg1 = (volintInfo *) p1;
3540     arg2 = (volintInfo *) p2;
3541     return (strcmp(arg1->name, arg2->name));
3542
3543 }
3544
3545 /*------------------------------------------------------------------------
3546  * PRIVATE XCompareVolName
3547  *
3548  * Description:
3549  *      Comparison routine for volume names coming from an extended
3550  *      volume listing.
3551  *
3552  * Arguments:
3553  *      a_obj1P : Char ptr to first extended vol info object
3554  *      a_obj1P : Char ptr to second extended vol info object
3555  *
3556  * Returns:
3557  *      The value of strcmp() on the volume names within the passed
3558  *      objects (i,e., -1, 0, or 1).
3559  *
3560  * Environment:
3561  *      Passed to qsort() as the designated comparison routine.
3562  *
3563  * Side Effects:
3564  *      As advertised.
3565  *------------------------------------------------------------------------*/
3566
3567 static int
3568 XCompareVolName(const void *a_obj1P, const void *a_obj2P)
3569 {                               /*XCompareVolName */
3570
3571     return (strcmp
3572             (((struct volintXInfo *)(a_obj1P))->name,
3573              ((struct volintXInfo *)(a_obj2P))->name));
3574
3575 }                               /*XCompareVolName */
3576
3577 static int
3578 CompareVolID(const void *p1, const void *p2)
3579 {
3580     volintInfo *arg1, *arg2;
3581
3582     arg1 = (volintInfo *) p1;
3583     arg2 = (volintInfo *) p2;
3584     if (arg1->volid == arg2->volid)
3585         return 0;
3586     if (arg1->volid > arg2->volid)
3587         return 1;
3588     else
3589         return -1;
3590
3591 }
3592
3593 /*------------------------------------------------------------------------
3594  * PRIVATE XCompareVolID
3595  *
3596  * Description:
3597  *      Comparison routine for volume IDs coming from an extended
3598  *      volume listing.
3599  *
3600  * Arguments:
3601  *      a_obj1P : Char ptr to first extended vol info object
3602  *      a_obj1P : Char ptr to second extended vol info object
3603  *
3604  * Returns:
3605  *      The value of strcmp() on the volume names within the passed
3606  *      objects (i,e., -1, 0, or 1).
3607  *
3608  * Environment:
3609  *      Passed to qsort() as the designated comparison routine.
3610  *
3611  * Side Effects:
3612  *      As advertised.
3613  *------------------------------------------------------------------------*/
3614
3615 static int
3616 XCompareVolID(const void *a_obj1P, const void *a_obj2P)
3617 {                               /*XCompareVolID */
3618
3619     afs_int32 id1, id2;         /*Volume IDs we're comparing */
3620
3621     id1 = ((struct volintXInfo *)(a_obj1P))->volid;
3622     id2 = ((struct volintXInfo *)(a_obj2P))->volid;
3623     if (id1 == id2)
3624         return (0);
3625     else if (id1 > id2)
3626         return (1);
3627     else
3628         return (-1);
3629
3630 }                               /*XCompareVolID */
3631
3632 /*------------------------------------------------------------------------
3633  * PRIVATE ListVolumes
3634  *
3635  * Description:
3636  *      Routine used to list volumes, contacting the Volume Server
3637  *      directly, bypassing the VLDB.
3638  *
3639  * Arguments:
3640  *      as : Ptr to parsed command line arguments.
3641  *
3642  * Returns:
3643  *      0                       Successful operation
3644  *
3645  * Environment:
3646  *      Nothing interesting.
3647  *
3648  * Side Effects:
3649  *      As advertised.
3650  *------------------------------------------------------------------------*/
3651
3652 static int
3653 ListVolumes(struct cmd_syndesc *as, void *arock)
3654 {
3655     afs_int32 apart, int32list, fast;
3656     afs_uint32 aserver;
3657     afs_int32 code;
3658     volintInfo *pntr;
3659     volintInfo *oldpntr = NULL;
3660     afs_int32 count;
3661     int i;
3662     char *base;
3663     volintXInfo *xInfoP;
3664     volintXInfo *origxInfoP = NULL; /*Ptr to current/orig extended vol info */
3665     int wantExtendedInfo;       /*Do we want extended vol info? */
3666
3667     char pname[10];
3668     struct partList dummyPartList;
3669     int all;
3670     int quiet, cnt;
3671
3672     apart = -1;
3673     fast = 0;
3674     int32list = 0;
3675
3676     if (as->parms[3].items)
3677         int32list = 1;
3678     if (as->parms[4].items)
3679         quiet = 1;
3680     else
3681         quiet = 0;
3682     if (as->parms[2].items)
3683         fast = 1;
3684     if (fast)
3685         all = 0;
3686     else
3687         all = 1;
3688     if (as->parms[5].items) {
3689         /*
3690          * We can't coexist with the fast flag.
3691          */
3692         if (fast) {
3693             fprintf(STDERR,
3694                     "vos: Can't use the -fast and -extended flags together\n");
3695             exit(1);
3696         }
3697
3698         /*
3699          * We need to turn on ``long'' listings to get the full effect.
3700          */
3701         wantExtendedInfo = 1;
3702         int32list = 1;
3703     } else
3704         wantExtendedInfo = 0;
3705     if (as->parms[1].items) {
3706         apart = volutil_GetPartitionID(as->parms[1].items->data);
3707         if (apart < 0) {
3708             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3709                     as->parms[1].items->data);
3710             exit(1);
3711         }
3712         dummyPartList.partId[0] = apart;
3713         dummyPartList.partFlags[0] = PARTVALID;
3714         cnt = 1;
3715     }
3716     aserver = GetServer(as->parms[0].items->data);
3717     if (aserver == 0) {
3718         fprintf(STDERR, "vos: server '%s' not found in host table\n",
3719                 as->parms[0].items->data);
3720         exit(1);
3721     }
3722
3723     if (apart != -1) {
3724         if (!IsPartValid(apart, aserver, &code)) {      /*check for validity of the partition */
3725             if (code)
3726                 PrintError("", code);
3727             else
3728                 fprintf(STDERR,
3729                         "vos : partition %s does not exist on the server\n",
3730                         as->parms[1].items->data);
3731             exit(1);
3732         }
3733     } else {
3734         code = UV_ListPartitions(aserver, &dummyPartList, &cnt);
3735         if (code) {
3736             PrintDiagnostics("listvol", code);
3737             exit(1);
3738         }
3739     }
3740     for (i = 0; i < cnt; i++) {
3741         if (dummyPartList.partFlags[i] & PARTVALID) {
3742             if (wantExtendedInfo)
3743                 code =
3744                     UV_XListVolumes(aserver, dummyPartList.partId[i], all,
3745                                     &xInfoP, &count);
3746             else
3747                 code =
3748                     UV_ListVolumes(aserver, dummyPartList.partId[i], all,
3749                                    &pntr, &count);
3750             if (code) {
3751                 PrintDiagnostics("listvol", code);
3752                 exit(1);
3753             }
3754             if (wantExtendedInfo) {
3755                 origxInfoP = xInfoP;
3756                 base = (char *)xInfoP;
3757             } else {
3758                 oldpntr = pntr;
3759                 base = (char *)pntr;
3760             }
3761
3762             if (!fast) {
3763                 if (wantExtendedInfo)
3764                     qsort(base, count, sizeof(volintXInfo), XCompareVolName);
3765                 else
3766                     qsort(base, count, sizeof(volintInfo), CompareVolName);
3767             } else {
3768                 if (wantExtendedInfo)
3769                     qsort(base, count, sizeof(volintXInfo), XCompareVolID);
3770                 else
3771                     qsort(base, count, sizeof(volintInfo), CompareVolID);
3772             }
3773             MapPartIdIntoName(dummyPartList.partId[i], pname);
3774             if (!quiet)
3775                 fprintf(STDOUT,
3776                         "Total number of volumes on server %s partition %s: %lu \n",
3777                         as->parms[0].items->data, pname,
3778                         (unsigned long)count);
3779             if (wantExtendedInfo) {
3780                 if (as->parms[6].items)
3781                     XDisplayVolumes2(aserver, dummyPartList.partId[i], origxInfoP,
3782                                 count, int32list, fast, quiet);
3783                 else
3784                     XDisplayVolumes(aserver, dummyPartList.partId[i], origxInfoP,
3785                                 count, int32list, fast, quiet);
3786                 if (xInfoP)
3787                     free(xInfoP);
3788                 xInfoP = (volintXInfo *) 0;
3789             } else {
3790                 if (as->parms[6].items)
3791                     DisplayVolumes2(aserver, dummyPartList.partId[i], oldpntr,
3792                                     count);
3793                 else
3794                     DisplayVolumes(aserver, dummyPartList.partId[i], oldpntr,
3795                                    count, int32list, fast, quiet);
3796                 if (pntr)
3797                     free(pntr);
3798                 pntr = (volintInfo *) 0;
3799             }
3800         }
3801     }
3802     return 0;
3803 }
3804
3805 static int
3806 SyncVldb(struct cmd_syndesc *as, void *arock)
3807 {
3808     afs_int32 pnum = 0, code;   /* part name */
3809     char part[10];
3810     int flags = 0;
3811     char *volname = 0;
3812     afs_uint32 tserver;
3813
3814     tserver = 0;
3815     if (as->parms[0].items) {
3816         tserver = GetServer(as->parms[0].items->data);
3817         if (!tserver) {
3818             fprintf(STDERR, "vos: host '%s' not found in host table\n",
3819                     as->parms[0].items->data);
3820             exit(1);
3821         }
3822     }
3823
3824     if (as->parms[1].items) {
3825         pnum = volutil_GetPartitionID(as->parms[1].items->data);
3826         if (pnum < 0) {
3827             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3828                     as->parms[1].items->data);
3829             exit(1);
3830         }
3831         if (!IsPartValid(pnum, tserver, &code)) {       /*check for validity of the partition */
3832             if (code)
3833                 PrintError("", code);
3834             else
3835                 fprintf(STDERR,
3836                         "vos: partition %s does not exist on the server\n",
3837                         as->parms[1].items->data);
3838             exit(1);
3839         }
3840         flags = 1;
3841
3842         if (!tserver) {
3843             fprintf(STDERR,
3844                     "The -partition option requires a -server option\n");
3845             exit(1);
3846         }
3847     }
3848
3849     if (as->parms[3].items) {
3850         flags |= 2; /* don't update */
3851     }
3852
3853     if (as->parms[2].items) {
3854         /* Synchronize an individual volume */
3855         volname = as->parms[2].items->data;
3856         code = UV_SyncVolume(tserver, pnum, volname, flags);
3857     } else {
3858         if (!tserver) {
3859             fprintf(STDERR,
3860                     "Without a -volume option, the -server option is required\n");
3861             exit(1);
3862         }
3863         code = UV_SyncVldb(tserver, pnum, flags, 0 /*unused */ );
3864     }
3865
3866     if (code) {
3867         PrintDiagnostics("syncvldb", code);
3868         exit(1);
3869     }
3870
3871     /* Print a summary of what we did */
3872     if (volname)
3873         fprintf(STDOUT, "VLDB volume %s synchronized", volname);
3874     else
3875         fprintf(STDOUT, "VLDB synchronized");
3876     if (tserver) {
3877         fprintf(STDOUT, " with state of server %s", as->parms[0].items->data);
3878     }
3879     if (flags & 1) {
3880         MapPartIdIntoName(pnum, part);
3881         fprintf(STDOUT, " partition %s\n", part);
3882     }
3883     fprintf(STDOUT, "\n");
3884
3885     return 0;
3886 }
3887
3888 static int
3889 SyncServer(struct cmd_syndesc *as, void *arock)
3890 {
3891     afs_int32 pnum, code;       /* part name */
3892     char part[10];
3893     afs_uint32 tserver;
3894
3895     int flags = 0;
3896
3897     tserver = GetServer(as->parms[0].items->data);
3898     if (!tserver) {
3899         fprintf(STDERR, "vos: host '%s' not found in host table\n",
3900                 as->parms[0].items->data);
3901         exit(1);
3902     }
3903     if (as->parms[1].items) {
3904         pnum = volutil_GetPartitionID(as->parms[1].items->data);
3905         if (pnum < 0) {
3906             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3907                     as->parms[1].items->data);
3908             exit(1);
3909         }
3910         if (!IsPartValid(pnum, tserver, &code)) {       /*check for validity of the partition */
3911             if (code)
3912                 PrintError("", code);
3913             else
3914                 fprintf(STDERR,
3915                         "vos : partition %s does not exist on the server\n",
3916                         as->parms[1].items->data);
3917             exit(1);
3918         }
3919         flags = 1;
3920     } else {
3921         pnum = -1;
3922     }
3923
3924     if (as->parms[2].items) {
3925         flags |= 2; /* don't update */
3926     }
3927     code = UV_SyncServer(tserver, pnum, flags, 0 /*unused */ );
3928     if (code) {
3929         PrintDiagnostics("syncserv", code);
3930         exit(1);
3931     }
3932     if (flags & 1) {
3933         MapPartIdIntoName(pnum, part);
3934         fprintf(STDOUT, "Server %s partition %s synchronized with VLDB\n",
3935                 as->parms[0].items->data, part);
3936     } else
3937         fprintf(STDOUT, "Server %s synchronized with VLDB\n",
3938                 as->parms[0].items->data);
3939     return 0;
3940
3941 }
3942
3943 static int
3944 VolumeInfoCmd(char *name)
3945 {
3946     struct nvldbentry entry;
3947     afs_int32 vcode;
3948
3949     /* The vlserver will handle names with the .readonly
3950      * and .backup extension as well as volume ids.
3951      */
3952     vcode = VLDB_GetEntryByName(name, &entry);
3953     if (vcode) {
3954         PrintError("", vcode);
3955         exit(1);
3956     }
3957     MapHostToNetwork(&entry);
3958     EnumerateEntry(&entry);
3959
3960     /* Defect #3027: grubby check to handle locked volume.
3961      * If VLOP_ALLOPERS is set, the entry is locked.
3962      * Leave this routine as is, but put in correct check.
3963      */
3964     PrintLocked(entry.flags);
3965
3966     return 0;
3967 }
3968
3969 static int
3970 VolumeZap(struct cmd_syndesc *as, void *arock)
3971 {
3972     struct nvldbentry entry;
3973     afs_uint32 volid, zapbackupid = 0, backupid = 0;
3974     afs_int32 code, server, part, err;
3975
3976     if (as->parms[3].items) {
3977         /* force flag is on, use the other version */
3978         return NukeVolume(as);
3979     }
3980
3981     if (as->parms[4].items) {
3982         zapbackupid = 1;
3983     }
3984
3985     volid = vsu_GetVolumeID(as->parms[2].items->data, cstruct, &err);
3986     if (volid == 0) {
3987         if (err)
3988             PrintError("", err);
3989         else
3990             fprintf(STDERR, "vos: can't find volume '%s'\n",
3991                     as->parms[2].items->data);
3992         exit(1);
3993     }
3994     part = volutil_GetPartitionID(as->parms[1].items->data);
3995     if (part < 0) {
3996         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3997                 as->parms[1].items->data);
3998         exit(1);
3999     }
4000     server = GetServer(as->parms[0].items->data);
4001     if (!server) {
4002         fprintf(STDERR, "vos: host '%s' not found in host table\n",
4003                 as->parms[0].items->data);
4004         exit(1);
4005     }
4006     if (!IsPartValid(part, server, &code)) {    /*check for validity of the partition */
4007         if (code)
4008             PrintError("", code);
4009         else
4010             fprintf(STDERR,
4011                     "vos : partition %s does not exist on the server\n",
4012                     as->parms[1].items->data);
4013         exit(1);
4014     }
4015     code = VLDB_GetEntryByID(volid, -1, &entry);
4016     if (!code) {
4017         if (volid == entry.volumeId[RWVOL])
4018             backupid = entry.volumeId[BACKVOL];
4019         fprintf(STDERR,
4020                 "Warning: Entry for volume number %lu exists in VLDB (but we're zapping it anyway!)\n",
4021                 (unsigned long)volid);
4022     }
4023     if (zapbackupid) {
4024         volintInfo *pntr = (volintInfo *) 0;
4025
4026         if (!backupid) {
4027             code = UV_ListOneVolume(server, part, volid, &pntr);
4028             if (!code) {
4029                 if (volid == pntr->parentID)
4030                     backupid = pntr->backupID;
4031                 if (pntr)
4032                     free(pntr);
4033             }
4034         }
4035         if (backupid) {
4036             code = UV_VolumeZap(server, part, backupid);
4037             if (code) {
4038                 PrintDiagnostics("zap", code);
4039                 exit(1);
4040             }
4041             fprintf(STDOUT, "Backup Volume %lu deleted\n",
4042                     (unsigned long)backupid);
4043         }
4044     }
4045     code = UV_VolumeZap(server, part, volid);
4046     if (code) {
4047         PrintDiagnostics("zap", code);
4048         exit(1);
4049     }
4050     fprintf(STDOUT, "Volume %lu deleted\n", (unsigned long)volid);
4051
4052     return 0;
4053 }
4054
4055 static int
4056 VolserStatus(struct cmd_syndesc *as, void *arock)
4057 {
4058     afs_uint32 server;
4059     afs_int32 code;
4060     transDebugInfo *pntr, *oldpntr;
4061     afs_int32 count;
4062     int i;
4063     char pname[10];
4064     time_t t;
4065
4066     server = GetServer(as->parms[0].items->data);
4067     if (!server) {
4068         fprintf(STDERR, "vos: host '%s' not found in host table\n",
4069                 as->parms[0].items->data);
4070         exit(1);
4071     }
4072     code = UV_VolserStatus(server, &pntr, &count);
4073     if (code) {
4074         PrintDiagnostics("status", code);
4075         exit(1);
4076     }
4077     oldpntr = pntr;
4078     if (count == 0)
4079         fprintf(STDOUT, "No active transactions on %s\n",
4080                 as->parms[0].items->data);
4081     else {
4082         fprintf(STDOUT, "Total transactions: %d\n", count);
4083     }
4084     for (i = 0; i < count; i++) {
4085         /*print out the relevant info */
4086         fprintf(STDOUT, "--------------------------------------\n");
4087         t = pntr->creationTime;
4088         fprintf(STDOUT, "transaction: %lu  created: %s",
4089                 (unsigned long)pntr->tid, ctime(&t));
4090         t = pntr->time;
4091         fprintf(STDOUT, "lastActiveTime: %s", ctime(&t));
4092         if (pntr->returnCode) {
4093             fprintf(STDOUT, "returnCode: %lu\n",
4094                     (unsigned long)pntr->returnCode);
4095         }
4096         if (pntr->iflags) {
4097             fprintf(STDOUT, "attachFlags:  ");
4098             switch (pntr->iflags) {
4099             case ITOffline:
4100                 fprintf(STDOUT, "offline ");
4101                 break;
4102             case ITBusy:
4103                 fprintf(STDOUT, "busy ");
4104                 break;
4105             case ITReadOnly:
4106                 fprintf(STDOUT, "readonly ");
4107                 break;
4108             case ITCreate:
4109                 fprintf(STDOUT, "create ");
4110                 break;
4111             case ITCreateVolID:
4112                 fprintf(STDOUT, "create volid ");
4113                 break;
4114             }
4115             fprintf(STDOUT, "\n");
4116         }
4117         if (pntr->vflags) {
4118             fprintf(STDOUT, "volumeStatus: ");
4119             switch (pntr->vflags) {
4120             case VTDeleteOnSalvage:
4121                 fprintf(STDOUT, "deleteOnSalvage ");
4122             case VTOutOfService:
4123                 fprintf(STDOUT, "outOfService ");
4124             case VTDeleted:
4125                 fprintf(STDOUT, "deleted ");
4126             }
4127             fprintf(STDOUT, "\n");
4128         }
4129         if (pntr->tflags) {
4130             fprintf(STDOUT, "transactionFlags: ");
4131             fprintf(STDOUT, "delete\n");
4132         }
4133         MapPartIdIntoName(pntr->partition, pname);
4134         fprintf(STDOUT, "volume: %lu  partition: %s  procedure: %s\n",
4135                 (unsigned long)pntr->volid, pname, pntr->lastProcName);
4136         if (pntr->callValid) {
4137             t = pntr->lastReceiveTime;
4138             fprintf(STDOUT, "packetRead: %lu  lastReceiveTime: %s",
4139                     (unsigned long)pntr->readNext, ctime(&t));
4140             t = pntr->lastSendTime;
4141             fprintf(STDOUT, "packetSend: %lu  lastSendTime: %s",
4142                     (unsigned long)pntr->transmitNext, ctime(&t));
4143         }
4144         pntr++;
4145         fprintf(STDOUT, "--------------------------------------\n");
4146         fprintf(STDOUT, "\n");
4147     }
4148     if (oldpntr)
4149         free(oldpntr);
4150     return 0;
4151 }
4152
4153 static int
4154 RenameVolume(struct cmd_syndesc *as, void *arock)
4155 {
4156     afs_int32 code;
4157     struct nvldbentry entry;
4158     struct nvldbentry entry2;
4159
4160     /* Get the entry of the volume to be renamed (-oldname), by name or id. */
4161     code = VLDB_GetEntryByName(as->parms[0].items->data, &entry);
4162     if (code) {
4163         fprintf(STDERR, "vos: Could not find entry for volume %s\n",
4164                 as->parms[0].items->data);
4165         PrintError("", code);
4166         exit(1);
4167     }
4168
4169     /*
4170      * Verify the new name is available before attempting to rename.
4171      * Allow renaming of the same volume in order to complete a
4172      * previously interrupted rename.
4173      */
4174     code = VLDB_GetEntryByName(as->parms[1].items->data, &entry2);
4175     if (code != 0 && code != VL_NOENT) {
4176         fprintf(STDERR, "vos: Could not check entry for volume %s\n",
4177                 as->parms[1].items->data);
4178         PrintError("", code);
4179         exit(1);
4180     }
4181     if (code == 0 && entry.volumeId[RWVOL] != entry2.volumeId[RWVOL]) {
4182         fprintf(STDERR, "vos: Cannot rename volume %s (%lu) to %s;"
4183                         " volume %s (%lu) already exists\n",
4184                         as->parms[0].items->data,
4185                         (unsigned long)entry.volumeId[RWVOL],
4186                         as->parms[1].items->data,
4187                         as->parms[1].items->data,
4188                         (unsigned long)entry2.volumeId[RWVOL]);
4189         exit(1);
4190     }
4191     if (!VolNameOK(as->parms[0].items->data)) {
4192         fprintf(STDERR,
4193                 "Illegal volume name %s, should not end in .readonly or .backup\n",
4194                 as->parms[0].items->data);
4195         exit(1);
4196     }
4197     if (!ISNAMEVALID(as->parms[1].items->data)) {
4198         fprintf(STDERR,
4199                 "vos: the new volume name %s exceeds the size limit of %d\n",
4200                 as->parms[1].items->data, VOLSER_OLDMAXVOLNAME - 10);
4201         exit(1);
4202     }
4203     if (!VolNameOK(as->parms[1].items->data)) {
4204         fprintf(STDERR,
4205                 "Illegal volume name %s, should not end in .readonly or .backup\n",
4206                 as->parms[1].items->data);
4207         exit(1);
4208     }
4209     if (IsNumeric(as->parms[1].items->data)) {
4210         fprintf(STDERR, "Illegal volume name %s, should not be a number\n",
4211                 as->parms[1].items->data);
4212         exit(1);
4213     }
4214     MapHostToNetwork(&entry);
4215     code =
4216         UV_RenameVolume(&entry, as->parms[0].items->data,
4217                         as->parms[1].items->data);
4218     if (code) {
4219         PrintDiagnostics("rename", code);
4220         exit(1);
4221     }
4222     fprintf(STDOUT, "Renamed volume %s to %s\n", as->parms[0].items->data,
4223             as->parms[1].items->data);
4224     return 0;
4225 }
4226
4227 int
4228 GetVolumeInfo(afs_uint32 volid, afs_uint32 *server, afs_int32 *part, afs_int32 *voltype,
4229               struct nvldbentry *rentry)
4230 {
4231     afs_int32 vcode;
4232     int i, index = -1;
4233
4234     vcode = VLDB_GetEntryByID(volid, -1, rentry);
4235     if (vcode) {
4236         fprintf(STDERR,
4237                 "Could not fetch the entry for volume %lu from VLDB \n",
4238                 (unsigned long)volid);
4239         PrintError("", vcode);
4240         return (vcode);
4241     }
4242     MapHostToNetwork(rentry);
4243     if (volid == rentry->volumeId[ROVOL]) {
4244         *voltype = ROVOL;
4245         for (i = 0; i < rentry->nServers; i++) {
4246             if ((index == -1) && (rentry->serverFlags[i] & VLSF_ROVOL)
4247                 && !(rentry->serverFlags[i] & VLSF_DONTUSE))
4248                 index = i;
4249         }
4250         if (index == -1) {
4251             fprintf(STDERR,
4252                     "RO volume is not found in VLDB entry for volume %lu\n",
4253                     (unsigned long)volid);
4254             return -1;
4255         }
4256
4257         *server = rentry->serverNumber[index];
4258         *part = rentry->serverPartition[index];
4259         return 0;
4260     }
4261
4262     index = Lp_GetRwIndex(rentry);
4263     if (index == -1) {
4264         fprintf(STDERR,
4265                 "RW Volume is not found in VLDB entry for volume %lu\n",
4266                 (unsigned long)volid);
4267         return -1;
4268     }
4269     if (volid == rentry->volumeId[RWVOL]) {
4270         *voltype = RWVOL;
4271         *server = rentry->serverNumber[index];
4272         *part = rentry->serverPartition[index];
4273         return 0;
4274     }
4275     if (volid == rentry->volumeId[BACKVOL]) {
4276         *voltype = BACKVOL;
4277         *server = rentry->serverNumber[index];
4278         *part = rentry->serverPartition[index];
4279         return 0;
4280     }
4281     fprintf(STDERR,
4282             "unexpected volume type for volume %lu\n",
4283             (unsigned long)volid);
4284     return -1;
4285 }
4286
4287 static int
4288 DeleteEntry(struct cmd_syndesc *as, void *arock)
4289 {
4290     afs_int32 apart = 0;
4291     afs_uint32 avolid;
4292     afs_int32 vcode;
4293     struct VldbListByAttributes attributes;
4294     nbulkentries arrayEntries;
4295     struct nvldbentry *vllist;
4296     struct cmd_item *itp;
4297     afs_int32 nentries;
4298     int j;
4299     char prefix[VOLSER_MAXVOLNAME + 1];
4300     int seenprefix = 0;
4301     afs_int32 totalBack = 0, totalFail = 0, err;
4302
4303     if (as->parms[0].items) {   /* -id */
4304         if (as->parms[1].items || as->parms[2].items || as->parms[3].items) {
4305             fprintf(STDERR,
4306                     "You cannot use -server, -partition, or -prefix with the -id argument\n");
4307             exit(-2);
4308         }
4309         for (itp = as->parms[0].items; itp; itp = itp->next) {
4310             avolid = vsu_GetVolumeID(itp->data, cstruct, &err);
4311             if (avolid == 0) {
4312                 if (err)
4313                     PrintError("", err);
4314                 else
4315                     fprintf(STDERR, "vos: can't find volume '%s'\n",
4316                             itp->data);
4317                 continue;
4318             }
4319             if (as->parms[4].items || as->parms[5].items) {
4320                 /* -noexecute (hidden) or -dryrun */
4321                 fprintf(STDOUT, "Would have deleted VLDB entry for %s \n",
4322                         itp->data);
4323                 fflush(STDOUT);
4324                 continue;
4325             }
4326             vcode = ubik_VL_DeleteEntry(cstruct, 0, avolid, RWVOL);
4327             if (vcode) {
4328                 fprintf(STDERR, "Could not delete entry for volume %s\n",
4329                         itp->data);
4330                 fprintf(STDERR,
4331                         "You must specify a RW volume name or ID "
4332                         "(the entire VLDB entry will be deleted)\n");
4333                 PrintError("", vcode);
4334                 totalFail++;
4335                 continue;
4336             }
4337             totalBack++;
4338         }
4339         fprintf(STDOUT, "Deleted %d VLDB entries\n", totalBack);
4340         return (totalFail);
4341     }
4342
4343     if (!as->parms[1].items && !as->parms[2].items && !as->parms[3].items) {
4344         fprintf(STDERR, "You must specify an option\n");
4345         exit(-2);
4346     }
4347
4348     /* Zero out search attributes */
4349     memset(&attributes, 0, sizeof(struct VldbListByAttributes));
4350
4351     if (as->parms[1].items) {   /* -prefix */
4352         strncpy(prefix, as->parms[1].items->data, VOLSER_MAXVOLNAME);
4353         seenprefix = 1;
4354         if (!as->parms[2].items && !as->parms[3].items) {       /* a single entry only */
4355             fprintf(STDERR,
4356                     "You must provide -server with the -prefix argument\n");
4357             exit(-2);
4358         }
4359     }
4360
4361     if (as->parms[2].items) {   /* -server */
4362         afs_uint32 aserver;
4363         aserver = GetServer(as->parms[2].items->data);
4364         if (aserver == 0) {
4365             fprintf(STDERR, "vos: server '%s' not found in host table\n",
4366                     as->parms[2].items->data);
4367             exit(-1);
4368         }
4369         attributes.server = ntohl(aserver);
4370         attributes.Mask |= VLLIST_SERVER;
4371     }
4372
4373     if (as->parms[3].items) {   /* -partition */
4374         if (!as->parms[2].items) {
4375             fprintf(STDERR,
4376                     "You must provide -server with the -partition argument\n");
4377             exit(-2);
4378         }
4379         apart = volutil_GetPartitionID(as->parms[3].items->data);
4380         if (apart < 0) {
4381             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
4382                     as->parms[3].items->data);
4383             exit(-1);
4384         }
4385         attributes.partition = apart;
4386         attributes.Mask |= VLLIST_PARTITION;
4387     }
4388
4389     /* Print status line of what we are doing */
4390     fprintf(STDOUT, "Deleting VLDB entries for ");
4391     if (as->parms[2].items) {
4392         fprintf(STDOUT, "server %s ", as->parms[2].items->data);
4393     }
4394     if (as->parms[3].items) {
4395         char pname[10];
4396         MapPartIdIntoName(apart, pname);
4397         fprintf(STDOUT, "partition %s ", pname);
4398     }
4399     if (seenprefix) {
4400         fprintf(STDOUT, "which are prefixed with %s ", prefix);
4401     }
4402     fprintf(STDOUT, "\n");
4403     fflush(STDOUT);
4404
4405     /* Get all the VLDB entries on a server and/or partition */
4406     memset(&arrayEntries, 0, sizeof(arrayEntries));
4407     vcode = VLDB_ListAttributes(&attributes, &nentries, &arrayEntries);
4408     if (vcode) {
4409         fprintf(STDERR, "Could not access the VLDB for attributes\n");
4410         PrintError("", vcode);
4411         exit(-1);
4412     }
4413
4414     /* Process each entry */
4415     for (j = 0; j < nentries; j++) {
4416         vllist = &arrayEntries.nbulkentries_val[j];
4417         if (seenprefix) {
4418             /* It only deletes the RW volumes */
4419             if (strncmp(vllist->name, prefix, strlen(prefix))) {
4420                 if (verbose) {
4421                     fprintf(STDOUT,
4422                             "Omitting to delete %s due to prefix %s mismatch\n",
4423                             vllist->name, prefix);
4424                 }
4425                 fflush(STDOUT);
4426                 continue;
4427             }
4428         }
4429
4430         if (as->parms[4].items || as->parms[5].items) {
4431             /* -noexecute (hidden) or -dryrun */
4432             fprintf(STDOUT, "Would have deleted VLDB entry for %s \n",
4433                     vllist->name);
4434             fflush(STDOUT);
4435             continue;
4436         }
4437
4438         /* Only matches the RW volume name */
4439         avolid = vllist->volumeId[RWVOL];
4440         vcode = ubik_VL_DeleteEntry(cstruct, 0, avolid, RWVOL);
4441         if (vcode) {
4442             fprintf(STDOUT, "Could not delete VLDB entry for  %s\n",
4443                     vllist->name);
4444             totalFail++;
4445             PrintError("", vcode);
4446             continue;
4447         } else {
4448             totalBack++;
4449             if (verbose)
4450                 fprintf(STDOUT, "Deleted VLDB entry for %s \n", vllist->name);
4451         }
4452         fflush(STDOUT);
4453     }                           /*for */
4454
4455     fprintf(STDOUT, "----------------------\n");
4456     fprintf(STDOUT,
4457             "Total VLDB entries deleted: %lu; failed to delete: %lu\n",
4458             (unsigned long)totalBack, (unsigned long)totalFail);
4459
4460     xdr_free((xdrproc_t) xdr_nbulkentries, &arrayEntries);
4461     return 0;
4462 }
4463
4464
4465 static int
4466 CompareVldbEntryByName(const void *p1, const void *p2)
4467 {
4468     struct nvldbentry *arg1, *arg2;
4469
4470     arg1 = (struct nvldbentry *)p1;
4471     arg2 = (struct nvldbentry *)p2;
4472     return (strcmp(arg1->name, arg2->name));
4473 }
4474
4475 /*
4476 static int CompareVldbEntry(char *p1, char *p2)
4477 {
4478     struct nvldbentry *arg1,*arg2;
4479     int i;
4480     int pos1, pos2;
4481     char comp1[100],comp2[100];
4482     char temp1[20],temp2[20];
4483
4484     arg1 = (struct nvldbentry *)p1;
4485     arg2 = (struct nvldbentry *)p2;
4486     pos1 = -1;
4487     pos2 = -1;
4488
4489     for(i = 0; i < arg1->nServers; i++)
4490         if(arg1->serverFlags[i] & VLSF_RWVOL) pos1 = i;
4491     for(i = 0; i < arg2->nServers; i++)
4492         if(arg2->serverFlags[i] & VLSF_RWVOL) pos2 = i;
4493     if(pos1 == -1 || pos2 == -1){
4494         pos1 = 0;
4495         pos2 = 0;
4496     }
4497     sprintf(comp1,"%10u",arg1->serverNumber[pos1]);
4498     sprintf(comp2,"%10u",arg2->serverNumber[pos2]);
4499     sprintf(temp1,"%10u",arg1->serverPartition[pos1]);
4500     sprintf(temp2,"%10u",arg2->serverPartition[pos2]);
4501     strcat(comp1,temp1);
4502     strcat(comp2,temp2);
4503     strcat(comp1,arg1->name);
4504     strcat(comp1,arg2->name);
4505     return(strcmp(comp1,comp2));
4506
4507 }
4508
4509 */
4510 static int
4511 ListVLDB(struct cmd_syndesc *as, void *arock)
4512 {
4513     afs_int32 apart;
4514     afs_int32 code;
4515     afs_int32 vcode;
4516     struct VldbListByAttributes attributes;
4517     nbulkentries arrayEntries;
4518     struct nvldbentry *vllist, *tarray = 0, *ttarray;
4519     afs_int32 centries, nentries = 0;
4520     afs_int32 tarraysize = 0;
4521     afs_int32 parraysize;
4522     int j;
4523     char pname[10];
4524     int quiet, sort, lock;
4525     afs_int32 thisindex, nextindex;
4526
4527     apart = 0;
4528
4529     memset(&attributes, 0, sizeof(attributes));
4530     lock = (as->parms[3].items ? 1 : 0);        /* -lock   flag */
4531     quiet = (as->parms[4].items ? 1 : 0);       /* -quit   flag */
4532     sort = (as->parms[5].items ? 0 : 1);        /* -nosort flag */
4533
4534     /* If the volume name is given, Use VolumeInfoCmd to look it up
4535      * and not ListAttributes.
4536      */
4537     if (as->parms[0].items) {
4538         if (lock) {
4539             fprintf(STDERR,
4540                     "vos: illegal use of '-locked' switch, need to specify server and/or partition\n");
4541             exit(1);
4542         }
4543         code = VolumeInfoCmd(as->parms[0].items->data);
4544         if (code) {
4545             PrintError("", code);
4546             exit(1);
4547         }
4548         return 0;
4549     }
4550
4551     /* Server specified */
4552     if (as->parms[1].items) {
4553         afs_uint32 aserver;
4554
4555         aserver = GetServer(as->parms[1].items->data);
4556         if (aserver == 0) {
4557             fprintf(STDERR, "vos: server '%s' not found in host table\n",
4558                     as->parms[1].items->data);
4559             exit(1);
4560         }
4561         attributes.server = ntohl(aserver);
4562         attributes.Mask |= VLLIST_SERVER;
4563     }
4564
4565     /* Partition specified */
4566     if (as->parms[2].items) {
4567         apart = volutil_GetPartitionID(as->parms[2].items->data);
4568         if (apart < 0) {
4569             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
4570                     as->parms[2].items->data);
4571             exit(1);
4572         }
4573         attributes.partition = apart;
4574         attributes.Mask |= VLLIST_PARTITION;
4575     }
4576
4577     if (lock) {
4578         attributes.Mask |= VLLIST_FLAG;
4579         attributes.flag = VLOP_ALLOPERS;
4580     }
4581
4582     /* Print header information */
4583     if (!quiet) {
4584         MapPartIdIntoName(apart, pname);
4585         fprintf(STDOUT, "VLDB entries for %s %s%s%s %s\n",
4586                 (as->parms[1].items ? "server" : "all"),
4587                 (as->parms[1].items ? as->parms[1].items->data : "servers"),
4588                 (as->parms[2].items ? " partition " : ""),
4589                 (as->parms[2].items ? pname : ""),
4590                 (lock ? "which are locked:" : ""));
4591     }
4592
4593     for (thisindex = 0; (thisindex != -1); thisindex = nextindex) {
4594         memset(&arrayEntries, 0, sizeof(arrayEntries));
4595         centries = 0;
4596         nextindex = -1;
4597
4598         vcode =
4599             VLDB_ListAttributesN2(&attributes, 0, thisindex, &centries,
4600                                   &arrayEntries, &nextindex);
4601         if (vcode == RXGEN_OPCODE) {
4602             /* Vlserver not running with ListAttributesN2. Fall back */
4603             vcode =
4604                 VLDB_ListAttributes(&attributes, &centries, &arrayEntries);
4605             nextindex = -1;
4606         }
4607         if (vcode) {
4608             fprintf(STDERR, "Could not access the VLDB for attributes\n");
4609             PrintError("", vcode);
4610             exit(1);
4611         }
4612         nentries += centries;
4613
4614         /* We don't sort, so just print the entries now */
4615         if (!sort) {
4616             for (j = 0; j < centries; j++) {    /* process each entry */
4617                 vllist = &arrayEntries.nbulkentries_val[j];
4618                 MapHostToNetwork(vllist);
4619                 EnumerateEntry(vllist);
4620
4621                 PrintLocked(vllist->flags);
4622             }
4623         }
4624
4625         /* So we sort. First we must collect all the entries and keep
4626          * them in memory.
4627          */
4628         else if (centries > 0) {
4629             if (!tarray) {
4630                 /* malloc the first bulk entries array */
4631                 tarraysize = centries * sizeof(struct nvldbentry);
4632                 tarray = malloc(tarraysize);
4633                 if (!tarray) {
4634                     fprintf(STDERR,
4635                             "Could not allocate enough space for the VLDB entries\n");
4636                     goto bypass;
4637                 }
4638                 memcpy((char*)tarray, arrayEntries.nbulkentries_val, tarraysize);
4639             } else {
4640                 /* Grow the tarray to keep the extra entries */
4641                 parraysize = (centries * sizeof(struct nvldbentry));
4642                 ttarray = realloc(tarray, tarraysize + parraysize);
4643                 if (!ttarray) {
4644                     fprintf(STDERR,
4645                             "Could not allocate enough space for  the VLDB entries\n");
4646                     goto bypass;
4647                 }
4648                 tarray = ttarray;
4649
4650                 /* Copy them in */
4651                 memcpy(((char *)tarray) + tarraysize,
4652                        (char *)arrayEntries.nbulkentries_val, parraysize);
4653                 tarraysize += parraysize;
4654             }
4655         }
4656
4657         /* Free the bulk array */
4658         xdr_free((xdrproc_t) xdr_nbulkentries, &arrayEntries);
4659     }
4660
4661     /* Here is where we now sort all the entries and print them */
4662     if (sort && (nentries > 0)) {
4663         qsort((char *)tarray, nentries, sizeof(struct nvldbentry),
4664               CompareVldbEntryByName);
4665         for (vllist = tarray, j = 0; j < nentries; j++, vllist++) {
4666             MapHostToNetwork(vllist);
4667             EnumerateEntry(vllist);
4668
4669             PrintLocked(vllist->flags);
4670         }
4671     }
4672
4673   bypass:
4674     if (!quiet)
4675         fprintf(STDOUT, "\nTotal entries: %lu\n", (unsigned long)nentries);
4676     if (tarray)
4677         free(tarray);
4678     return 0;
4679 }
4680
4681 static int
4682 BackSys(struct cmd_syndesc *as, void *arock)
4683 {
4684     afs_uint32 avolid;
4685     afs_int32 apart = 0;
4686     afs_uint32 aserver = 0, aserver1;
4687     afs_int32 code, apart1;
4688     afs_int32 vcode;
4689     struct VldbListByAttributes attributes;
4690     nbulkentries arrayEntries;
4691     struct nvldbentry *vllist;
4692     afs_int32 nentries;
4693     int j;
4694     char pname[10];
4695     int seenprefix, seenxprefix, exclude, ex, exp, noaction;
4696     afs_int32 totalBack = 0;
4697     afs_int32 totalFail = 0;
4698     int previdx = -1;
4699     int error;
4700     int same = 0;
4701     struct cmd_item *ti;
4702     int match = 0;
4703 #ifndef HAVE_POSIX_REGEX
4704     char *ccode;
4705 #endif
4706
4707     memset(&attributes, 0, sizeof(struct VldbListByAttributes));
4708     attributes.Mask = 0;
4709
4710     seenprefix = (as->parms[0].items ? 1 : 0);
4711     exclude = (as->parms[3].items ? 1 : 0);
4712     seenxprefix = (as->parms[4].items ? 1 : 0);
4713     noaction = (as->parms[5].items ? 1 : 0);
4714
4715     if (as->parms[1].items) {   /* -server */
4716         aserver = GetServer(as->parms[1].items->data);
4717         if (aserver == 0) {
4718             fprintf(STDERR, "vos: server '%s' not found in host table\n",
4719                     as->parms[1].items->data);
4720             exit(1);
4721         }
4722         attributes.server = ntohl(aserver);
4723         attributes.Mask |= VLLIST_SERVER;
4724     }
4725
4726     if (as->parms[2].items) {   /* -partition */
4727         apart = volutil_GetPartitionID(as->parms[2].items->data);
4728         if (apart < 0) {
4729             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
4730                     as->parms[2].items->data);
4731             exit(1);
4732         }
4733         attributes.partition = apart;
4734         attributes.Mask |= VLLIST_PARTITION;
4735     }
4736
4737     /* Check to make sure the prefix and xprefix expressions compile ok */
4738     if (seenprefix) {
4739         for (ti = as->parms[0].items; ti; ti = ti->next) {
4740             if (strncmp(ti->data, "^", 1) == 0) {
4741 #ifdef HAVE_POSIX_REGEX
4742                 regex_t re;
4743                 char errbuf[256];
4744
4745                 code = regcomp(&re, ti->data, REG_NOSUB);
4746                 if (code != 0) {
4747                     regerror(code, &re, errbuf, sizeof errbuf);
4748                     fprintf(STDERR,
4749                             "Unrecognizable -prefix regular expression: '%s': %s\n",
4750                             ti->data, errbuf);
4751                     exit(1);
4752                 }
4753                 regfree(&re);
4754 #else
4755                 ccode = (char *)re_comp(ti->data);
4756                 if (ccode) {
4757                     fprintf(STDERR,
4758                             "Unrecognizable -prefix regular expression: '%s': %s\n",
4759                             ti->data, ccode);
4760                     exit(1);
4761                 }
4762 #endif
4763             }
4764         }
4765     }
4766     if (seenxprefix) {
4767         for (ti = as->parms[4].items; ti; ti = ti->next) {
4768             if (strncmp(ti->data, "^", 1) == 0) {
4769 #ifdef HAVE_POSIX_REGEX
4770                 regex_t re;
4771                 char errbuf[256];
4772
4773                 code = regcomp(&re, ti->data, REG_NOSUB);
4774                 if (code != 0) {
4775                     regerror(code, &re, errbuf, sizeof errbuf);
4776                     fprintf(STDERR,
4777                             "Unrecognizable -xprefix regular expression: '%s': %s\n",
4778                             ti->data, errbuf);
4779                     exit(1);
4780                 }
4781                 regfree(&re);
4782 #else
4783                 ccode = (char *)re_comp(ti->data);
4784                 if (ccode) {
4785                     fprintf(STDERR,
4786                             "Unrecognizable -xprefix regular expression: '%s': %s\n",
4787                             ti->data, ccode);
4788                     exit(1);
4789                 }
4790 #endif
4791             }
4792         }
4793     }
4794
4795     memset(&arrayEntries, 0, sizeof(arrayEntries));     /* initialize to hint the stub to alloc space */
4796     vcode = VLDB_ListAttributes(&attributes, &nentries, &arrayEntries);
4797     if (vcode) {
4798         fprintf(STDERR, "Could not access the VLDB for attributes\n");
4799         PrintError("", vcode);
4800         exit(1);
4801     }
4802
4803     if (as->parms[1].items || as->parms[2].items || verbose) {
4804         fprintf(STDOUT, "%s up volumes",
4805                 (noaction ? "Would have backed" : "Backing"));
4806
4807         if (as->parms[1].items) {
4808             fprintf(STDOUT, " on server %s", as->parms[1].items->data);
4809         } else if (as->parms[2].items) {
4810             fprintf(STDOUT, " for all servers");
4811         }
4812
4813         if (as->parms[2].items) {
4814             MapPartIdIntoName(apart, pname);
4815             fprintf(STDOUT, " partition %s", pname);
4816         }
4817
4818         if (seenprefix || (!seenprefix && seenxprefix)) {
4819             ti = (seenprefix ? as->parms[0].items : as->parms[4].items);
4820             ex = (seenprefix ? exclude : !exclude);
4821             exp = (strncmp(ti->data, "^", 1) == 0);
4822             fprintf(STDOUT, " which %smatch %s '%s'", (ex ? "do not " : ""),
4823                     (exp ? "expression" : "prefix"), ti->data);
4824             for (ti = ti->next; ti; ti = ti->next) {
4825                 exp = (strncmp(ti->data, "^", 1) == 0);
4826                 printf(" %sor %s '%s'", (ex ? "n" : ""),
4827                        (exp ? "expression" : "prefix"), ti->data);
4828             }
4829         }
4830
4831         if (seenprefix && seenxprefix) {
4832             ti = as->parms[4].items;
4833             exp = (strncmp(ti->data, "^", 1) == 0);
4834             fprintf(STDOUT, " %swhich match %s '%s'",
4835                     (exclude ? "adding those " : "removing those "),
4836                     (exp ? "expression" : "prefix"), ti->data);
4837             for (ti = ti->next; ti; ti = ti->next) {
4838                 exp = (strncmp(ti->data, "^", 1) == 0);
4839                 printf(" or %s '%s'", (exp ? "expression" : "prefix"),
4840                        ti->data);
4841             }
4842         }
4843         fprintf(STDOUT, " .. ");
4844         if (verbose)
4845             fprintf(STDOUT, "\n");
4846         fflush(STDOUT);
4847     }
4848
4849     for (j = 0; j < nentries; j++) {    /* process each vldb entry */
4850         vllist = &arrayEntries.nbulkentries_val[j];
4851
4852         if (seenprefix) {
4853             for (ti = as->parms[0].items; ti; ti = ti->next) {
4854                 if (strncmp(ti->data, "^", 1) == 0) {
4855 #ifdef HAVE_POSIX_REGEX
4856                     regex_t re;
4857                     char errbuf[256];
4858
4859                     /* XXX -- should just do the compile once! */
4860                     code = regcomp(&re, ti->data, REG_NOSUB);
4861                     if (code != 0) {
4862                         regerror(code, &re, errbuf, sizeof errbuf);
4863                         fprintf(STDERR,
4864                                 "Error in -prefix regular expression: '%s': %s\n",
4865                                 ti->data, errbuf);
4866                         exit(1);
4867                     }
4868                     match = (regexec(&re, vllist->name, 0, NULL, 0) == 0);
4869                     regfree(&re);
4870 #else
4871                     ccode = (char *)re_comp(ti->data);
4872                     if (ccode) {
4873                         fprintf(STDERR,
4874                                 "Error in -prefix regular expression: '%s': %s\n",
4875                                 ti->data, ccode);
4876                         exit(1);
4877                     }
4878                     match = (re_exec(vllist->name) == 1);
4879 #endif
4880                 } else {
4881                     match =
4882                         (strncmp(vllist->name, ti->data, strlen(ti->data)) ==
4883                          0);
4884                 }
4885                 if (match)
4886                     break;
4887             }
4888         } else {
4889             match = 1;
4890         }
4891
4892         /* Without the -exclude flag: If it matches the prefix, then
4893          *    check if we want to exclude any from xprefix.
4894          * With the -exclude flag: If it matches the prefix, then
4895          *    check if we want to add any from xprefix.
4896          */
4897         if (match && seenxprefix) {
4898             for (ti = as->parms[4].items; ti; ti = ti->next) {
4899                 if (strncmp(ti->data, "^", 1) == 0) {
4900 #ifdef HAVE_POSIX_REGEX
4901                     regex_t re;
4902                     char errbuf[256];
4903
4904                     /* XXX -- should just do the compile once! */
4905                     code = regcomp(&re, ti->data, REG_NOSUB);
4906                     if (code != 0) {
4907                         regerror(code, &re, errbuf, sizeof errbuf);
4908                         fprintf(STDERR,
4909                                 "Error in -xprefix regular expression: '%s': %s\n",
4910                                 ti->data, errbuf);
4911                         exit(1);
4912                     }
4913                     if (regexec(&re, vllist->name, 0, NULL, 0) == 0)
4914                             match = 0;
4915                     regfree(&re);
4916 #else
4917                     ccode = (char *)re_comp(ti->data);
4918                     if (ccode) {
4919                         fprintf(STDERR,
4920                                 "Error in -xprefix regular expression: '%s': %s\n",
4921                                 ti->data, ccode);
4922                         exit(1);
4923                     }
4924                     if (re_exec(vllist->name) == 1) {
4925                         match = 0;
4926                         break;
4927                     }
4928 #endif
4929                 } else {
4930                     if (strncmp(vllist->name, ti->data, strlen(ti->data)) ==
4931                         0) {
4932                         match = 0;
4933                         break;
4934                     }
4935                 }
4936             }
4937         }
4938
4939         if (exclude)
4940             match = !match;     /* -exclude will reverse the match */
4941         if (!match)
4942             continue;           /* Skip if no match */
4943
4944         /* Print list of volumes to backup */
4945         if (noaction) {
4946             fprintf(STDOUT, "     %s\n", vllist->name);
4947             continue;
4948         }
4949
4950         if (!(vllist->flags & VLF_RWEXISTS)) {
4951             if (verbose) {
4952                 fprintf(STDOUT,
4953                         "Omitting to backup %s since RW volume does not exist \n",
4954                         vllist->name);
4955                 fprintf(STDOUT, "\n");
4956             }
4957             fflush(STDOUT);
4958             continue;
4959         }
4960
4961         avolid = vllist->volumeId[RWVOL];
4962         MapHostToNetwork(vllist);
4963         GetServerAndPart(vllist, RWVOL, &aserver1, &apart1, &previdx);
4964         if (aserver1 == -1 || apart1 == -1) {
4965             fprintf(STDOUT, "could not backup %s, invalid VLDB entry\n",
4966                     vllist->name);
4967             totalFail++;
4968             continue;
4969         }
4970         if (aserver) {
4971             same = VLDB_IsSameAddrs(aserver, aserver1, &error);
4972             if (error) {
4973                 fprintf(STDERR,
4974                         "Failed to get info about server's %d address(es) from vlserver (err=%d); aborting call!\n",
4975                         aserver, error);
4976                 totalFail++;
4977                 continue;
4978             }
4979         }
4980         if ((aserver && !same) || (apart && (apart != apart1))) {
4981             if (verbose) {
4982                 fprintf(STDOUT,
4983                         "Omitting to backup %s since the RW is in a different location\n",
4984                         vllist->name);
4985             }
4986             continue;
4987         }
4988         if (verbose) {
4989             time_t now = time(0);
4990             fprintf(STDOUT, "Creating backup volume for %s on %s",
4991                     vllist->name, ctime(&now));
4992             fflush(STDOUT);
4993         }
4994
4995         code = UV_BackupVolume(aserver1, apart1, avolid);
4996         if (code) {
4997             fprintf(STDOUT, "Could not backup %s\n", vllist->name);
4998             totalFail++;
4999         } else {
5000             totalBack++;
5001         }
5002         if (verbose)
5003             fprintf(STDOUT, "\n");
5004         fflush(STDOUT);
5005     }                           /* process each vldb entry */
5006     fprintf(STDOUT, "done\n");
5007     fprintf(STDOUT, "Total volumes backed up: %lu; failed to backup: %lu\n",
5008             (unsigned long)totalBack, (unsigned long)totalFail);
5009     fflush(STDOUT);
5010     xdr_free((xdrproc_t) xdr_nbulkentries, &arrayEntries);
5011     return 0;
5012 }
5013
5014 static int
5015 UnlockVLDB(struct cmd_syndesc *as, void *arock)
5016 {
5017     afs_int32 apart;
5018     afs_uint32 aserver = 0;
5019     afs_int32 code;
5020     afs_int32 vcode;
5021     struct VldbListByAttributes attributes;
5022     nbulkentries arrayEntries;
5023     struct nvldbentry *vllist;
5024     afs_int32 nentries;
5025     int j;
5026     afs_uint32 volid;
5027     afs_int32 totalE;
5028     char pname[10];
5029
5030     apart = -1;
5031     totalE = 0;
5032     memset(&attributes, 0, sizeof(attributes));
5033
5034     if (as->parms[0].items) {   /* server specified */
5035         aserver = GetServer(as->parms[0].items->data);
5036         if (aserver == 0) {
5037             fprintf(STDERR, "vos: server '%s' not found in host table\n",
5038                     as->parms[0].items->data);
5039             exit(1);
5040         }
5041         attributes.server = ntohl(aserver);
5042         attributes.Mask |= VLLIST_SERVER;
5043     }
5044     if (as->parms[1].items) {   /* partition specified */
5045         apart = volutil_GetPartitionID(as->parms[1].items->data);
5046         if (apart < 0) {
5047             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
5048                     as->parms[1].items->data);
5049             exit(1);
5050         }
5051         if (!IsPartValid(apart, aserver, &code)) {      /*check for validity of the partition */
5052             if (code)
5053                 PrintError("", code);
5054             else
5055                 fprintf(STDERR,
5056                         "vos : partition %s does not exist on the server\n",
5057                         as->parms[1].items->data);
5058             exit(1);
5059         }
5060         attributes.partition = apart;
5061         attributes.Mask |= VLLIST_PARTITION;
5062     }
5063     attributes.flag = VLOP_ALLOPERS;
5064     attributes.Mask |= VLLIST_FLAG;
5065     memset(&arrayEntries, 0, sizeof(arrayEntries));     /*initialize to hint the stub  to alloc space */
5066     vcode = VLDB_ListAttributes(&attributes, &nentries, &arrayEntries);
5067     if (vcode) {
5068         fprintf(STDERR, "Could not access the VLDB for attributes\n");
5069         PrintError("", vcode);
5070         exit(1);
5071     }
5072     for (j = 0; j < nentries; j++) {    /* process each entry */
5073         vllist = &arrayEntries.nbulkentries_val[j];
5074         volid = vllist->volumeId[RWVOL];
5075         vcode =
5076             ubik_VL_ReleaseLock(cstruct, 0, volid, -1,
5077                                 LOCKREL_OPCODE | LOCKREL_AFSID |
5078                                 LOCKREL_TIMESTAMP);
5079         if (vcode) {
5080             fprintf(STDERR, "Could not unlock entry for volume %s\n",
5081                     vllist->name);
5082             PrintError("", vcode);
5083             totalE++;
5084         }
5085
5086     }
5087     MapPartIdIntoName(apart, pname);
5088     if (totalE)
5089         fprintf(STDOUT,
5090                 "Could not lock %lu VLDB entries of %lu locked entries\n",
5091                 (unsigned long)totalE, (unsigned long)nentries);
5092     else {
5093         if (as->parms[0].items) {
5094             fprintf(STDOUT,
5095                     "Unlocked all the VLDB entries for volumes on server %s ",
5096                     as->parms[0].items->data);
5097             if (as->parms[1].items) {
5098                 MapPartIdIntoName(apart, pname);
5099                 fprintf(STDOUT, "partition %s\n", pname);
5100             } else
5101                 fprintf(STDOUT, "\n");
5102
5103         } else if (as->parms[1].items) {
5104             MapPartIdIntoName(apart, pname);
5105             fprintf(STDOUT,
5106                     "Unlocked all the VLDB entries for volumes on partition %s on all servers\n",
5107                     pname);
5108         }
5109     }
5110
5111     xdr_free((xdrproc_t) xdr_nbulkentries, &arrayEntries);
5112     return 0;
5113 }
5114
5115 static char *
5116 PrintInt64Size(afs_uint64 in)
5117 {
5118     afs_uint32 hi, lo;
5119     char * units;
5120     static char output[16];
5121
5122     SplitInt64(in,hi,lo);
5123
5124     if (hi == 0) {
5125         units = "KB";
5126     } else if (!(hi & 0xFFFFFC00)) {
5127         units = "MB";
5128         lo = (hi << 22) | (lo >> 10);
5129     } else if (!(hi & 0xFFF00000)) {
5130         units = "GB";
5131         lo = (hi << 12) | (lo >> 20);
5132     } else if (!(hi & 0xC0000000)) {
5133         units = "TB";
5134         lo = (hi << 2) | (lo >> 30);
5135     } else {
5136         units = "PB";
5137         lo = (hi >> 8);
5138     }
5139     sprintf(output,"%u %s", lo, units);
5140     return output;
5141 }
5142
5143 static int
5144 PartitionInfo(struct cmd_syndesc *as, void *arock)
5145 {
5146     afs_int32 apart;
5147     afs_uint32 aserver;
5148     afs_int32 code;
5149     char pname[10];
5150     struct diskPartition64 partition;
5151     struct partList dummyPartList;
5152     int i, cnt;
5153     int printSummary=0, sumPartitions=0;
5154     afs_uint64 sumFree, sumStorage;
5155
5156     ZeroInt64(sumFree);
5157     ZeroInt64(sumStorage);
5158     apart = -1;
5159     aserver = GetServer(as->parms[0].items->data);
5160     if (aserver == 0) {
5161         fprintf(STDERR, "vos: server '%s' not found in host table\n",
5162                 as->parms[0].items->data);
5163         exit(1);
5164     }
5165     if (as->parms[1].items) {
5166         apart = volutil_GetPartitionID(as->parms[1].items->data);
5167         if (apart < 0) {
5168             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
5169                     as->parms[1].items->data);
5170             exit(1);
5171         }
5172         dummyPartList.partId[0] = apart;
5173         dummyPartList.partFlags[0] = PARTVALID;
5174         cnt = 1;
5175     }
5176     if (as->parms[2].items) {
5177         printSummary = 1;
5178     }
5179     if (apart != -1) {
5180         if (!IsPartValid(apart, aserver, &code)) {      /*check for validity of the partition */
5181             if (code)
5182                 PrintError("", code);
5183             else
5184                 fprintf(STDERR,
5185                         "vos : partition %s does not exist on the server\n",
5186                         as->parms[1].items->data);
5187             exit(1);
5188         }
5189     } else {
5190         code = UV_ListPartitions(aserver, &dummyPartList, &cnt);
5191         if (code) {
5192             PrintDiagnostics("listpart", code);
5193             exit(1);
5194         }
5195     }
5196     for (i = 0; i < cnt; i++) {
5197         if (dummyPartList.partFlags[i] & PARTVALID) {
5198             MapPartIdIntoName(dummyPartList.partId[i], pname);
5199             code = UV_PartitionInfo64(aserver, pname, &partition);
5200             if (code) {
5201                 fprintf(STDERR, "Could not get information on partition %s\n",
5202                         pname);
5203                 PrintError("", code);
5204                 exit(1);
5205             }
5206             fprintf(STDOUT,
5207                     "Free space on partition %s: %" AFS_INT64_FMT " K blocks out of total %" AFS_INT64_FMT "\n",
5208                     pname, partition.free, partition.minFree);
5209             sumPartitions++;
5210             AddUInt64(sumFree,partition.free,&sumFree);
5211             AddUInt64(sumStorage,partition.minFree,&sumStorage);
5212         }
5213     }
5214     if (printSummary) {
5215         fprintf(STDOUT,
5216                 "Summary: %s free out of ",
5217                 PrintInt64Size(sumFree));
5218         fprintf(STDOUT,
5219                 "%s on %d partitions\n",
5220                 PrintInt64Size(sumStorage),
5221                 sumPartitions);
5222     }
5223     return 0;
5224 }
5225
5226 static int
5227 ChangeAddr(struct cmd_syndesc *as, void *arock)
5228 {
5229     afs_int32 ip1, ip2, vcode;
5230     int remove = 0;
5231     int force = 0;
5232
5233     if (noresolve)
5234         ip1 = GetServerNoresolve(as->parms[0].items->data);
5235     else
5236         ip1 = GetServer(as->parms[0].items->data);
5237     if (!ip1) {
5238         fprintf(STDERR, "vos: invalid host address\n");
5239         return (EINVAL);
5240     }
5241
5242     if ((as->parms[1].items && as->parms[2].items)
5243         || (!as->parms[1].items && !as->parms[2].items)) {
5244         fprintf(STDERR,
5245                 "vos: Must specify either '-newaddr <addr>' or '-remove' flag\n");
5246         return (EINVAL);
5247     }
5248
5249     if (as->parms[3].items) {
5250         force = 1;
5251     }
5252
5253     if (as->parms[1].items) {
5254         if (noresolve)
5255             ip2 = GetServerNoresolve(as->parms[1].items->data);
5256         else
5257             ip2 = GetServer(as->parms[1].items->data);
5258         if (!ip2) {
5259             fprintf(STDERR, "vos: invalid host address\n");
5260             return (EINVAL);
5261         }
5262     } else {
5263         /* Play a trick here. If we are removing an address, ip1 will be -1
5264          * and ip2 will be the original address. This switch prevents an
5265          * older revision vlserver from removing the IP address.
5266          */
5267         remove = 1;
5268         ip2 = ip1;
5269         ip1 = 0xffffffff;
5270     }
5271
5272     if (!remove && !force) {
5273         afs_int32 m_nentries;
5274         bulkaddrs m_addrs;
5275         afs_int32 m_uniq = 0;
5276         afsUUID m_uuid;
5277         ListAddrByAttributes m_attrs;
5278         struct uuid_fmtbuf buffer;
5279
5280         memset(&m_attrs, 0, sizeof(m_attrs));
5281         memset(&m_uuid, 0, sizeof(m_uuid));
5282         memset(&m_addrs, 0, sizeof(m_addrs));
5283
5284         m_attrs.Mask = VLADDR_IPADDR;
5285         m_attrs.ipaddr = ntohl(ip1);    /* -oldaddr */
5286
5287         vcode =
5288             ubik_VL_GetAddrsU(cstruct, UBIK_CALL_NEW, &m_attrs, &m_uuid,
5289                               &m_uniq, &m_nentries, &m_addrs);
5290         xdr_free((xdrproc_t) xdr_bulkaddrs, &m_addrs);
5291         switch (vcode) {
5292         case 0:         /* mh entry detected */
5293             afsUUID_to_string(&m_uuid, &buffer);
5294             fprintf(STDERR, "vos: Refusing to change address in multi-homed server entry.\n");
5295             fprintf(STDERR, "     -oldaddr address is registered to file server UUID %s\n",
5296                             buffer.buffer);
5297             fprintf(STDERR, "     Please restart the file server or use vos setaddrs.\n");
5298             return EINVAL;
5299         case VL_NOENT:
5300             break;
5301         default:
5302             fprintf(STDERR, "vos: could not list the server addresses\n");
5303             PrintError("", vcode);
5304             return vcode;
5305         }
5306     }
5307
5308     vcode = ubik_VL_ChangeAddr(cstruct, UBIK_CALL_NEW, ntohl(ip1), ntohl(ip2));
5309     if (vcode) {
5310         char hoststr1[16], hoststr2[16];
5311         if (remove) {
5312             afs_inet_ntoa_r(ip2, hoststr2);
5313             fprintf(STDERR, "Could not remove server %s from the VLDB\n",
5314                     hoststr2);
5315             if (vcode == VL_NOENT) {
5316                 fprintf(STDERR,
5317                         "vlserver does not support the remove flag or ");
5318             }
5319         } else {
5320             afs_inet_ntoa_r(ip1, hoststr1);
5321             afs_inet_ntoa_r(ip2, hoststr2);
5322             fprintf(STDERR, "Could not change server %s to server %s\n",
5323                     hoststr1, hoststr2);
5324         }
5325         PrintError("", vcode);
5326         return (vcode);
5327     }
5328
5329     if (remove) {
5330         fprintf(STDOUT, "Removed server %s from the VLDB\n",
5331                 as->parms[0].items->data);
5332     } else {
5333         fprintf(STDOUT, "Changed server %s to server %s\n",
5334                 as->parms[0].items->data, as->parms[1].items->data);
5335     }
5336     return 0;
5337 }
5338
5339 static void
5340 print_addrs(const bulkaddrs * addrs, afsUUID * m_uuid, int nentries,
5341             int print)
5342 {
5343     int i;
5344     afs_uint32 addr;
5345     struct uuid_fmtbuf buf;
5346
5347     if (print) {
5348         afsUUID_to_string(m_uuid, &buf);
5349         printf("UUID: %s\n", buf.buffer);
5350     }
5351
5352     /* print out the list of all the server */
5353     for (i = 0; i < addrs->bulkaddrs_len; i++) {
5354         addr = htonl(addrs->bulkaddrs_val[i]);
5355         if (noresolve) {
5356             char hoststr[16];
5357             printf("%s\n", afs_inet_ntoa_r(addr, hoststr));
5358         } else {
5359             printf("%s\n", hostutil_GetNameByINet(addr));
5360         }
5361     }
5362
5363     if (print) {
5364         printf("\n");
5365     }
5366     return;
5367 }
5368
5369 static int
5370 ListAddrs(struct cmd_syndesc *as, void *arock)
5371 {
5372     afs_int32 vcode, m_uniq=0;
5373     afs_int32 i, printuuid = 0;
5374     struct VLCallBack vlcb;
5375     afs_int32 nentries;
5376     bulkaddrs m_addrs;
5377     ListAddrByAttributes m_attrs;
5378     afsUUID m_uuid, askuuid;
5379     afs_int32 m_nentries;
5380
5381     if (as->parms[0].items && as->parms[1].items) {
5382         fprintf(STDERR, "vos: Can't use the -uuid and -host flags together\n");
5383         exit(-1);
5384     }
5385
5386     memset(&m_attrs, 0, sizeof(struct ListAddrByAttributes));
5387     memset(&askuuid, 0, sizeof(afsUUID));
5388     if (as->parms[0].items) {
5389         /* -uuid */
5390         if (afsUUID_from_string(as->parms[0].items->data, &askuuid) < 0) {
5391             fprintf(STDERR, "vos: invalid UUID '%s'\n",
5392                     as->parms[0].items->data);
5393             exit(-1);
5394         }
5395         m_attrs.Mask = VLADDR_UUID;
5396         m_attrs.uuid = askuuid;
5397     } else if (as->parms[1].items) {
5398         /* -host */
5399         struct hostent *he;
5400         afs_uint32 saddr;
5401         he = hostutil_GetHostByName((char *)as->parms[1].items->data);
5402         if (he == NULL) {
5403             fprintf(STDERR, "vos: Can't get host info for '%s'\n",
5404                     as->parms[1].items->data);
5405             exit(-1);
5406         }
5407         memcpy(&saddr, he->h_addr, 4);
5408         m_attrs.Mask = VLADDR_IPADDR;
5409         m_attrs.ipaddr = ntohl(saddr);
5410     } else {
5411         /* by index */
5412         m_attrs.Mask = VLADDR_INDEX;
5413     }
5414
5415     if (as->parms[2].items) {
5416         printuuid = 1;
5417     }
5418
5419     memset(&m_addrs, 0, sizeof(bulkaddrs));
5420     memset(&vlcb, 0, sizeof(struct VLCallBack));
5421
5422     vcode =
5423         ubik_VL_GetAddrs(cstruct, UBIK_CALL_NEW, 0, 0, &vlcb, &nentries,
5424                          &m_addrs);
5425     if (vcode) {
5426         fprintf(STDERR, "vos: could not list the server addresses\n");
5427         PrintError("", vcode);
5428         goto out;
5429     }
5430
5431     for (i = 1, m_nentries = 0; nentries; i++) {
5432         m_attrs.index = i;
5433
5434         xdr_free((xdrproc_t)xdr_bulkaddrs, &m_addrs); /* reset addr list */
5435         vcode =
5436             ubik_VL_GetAddrsU(cstruct, UBIK_CALL_NEW, &m_attrs, &m_uuid,
5437                               &m_uniq, &m_nentries, &m_addrs);
5438         switch (vcode) {
5439         case 0: /* success */
5440             print_addrs(&m_addrs, &m_uuid, m_nentries, printuuid);
5441             nentries--;
5442             break;
5443
5444         case VL_NOENT:
5445             if (m_attrs.Mask == VLADDR_UUID) {
5446                 fprintf(STDERR, "vos: no entry for UUID '%s' found in VLDB\n",
5447                         as->parms[0].items->data);
5448                 exit(-1);
5449             } else if (m_attrs.Mask == VLADDR_IPADDR) {
5450                 fprintf(STDERR, "vos: no entry for host '%s' [0x%08x] found in VLDB\n",
5451                         as->parms[1].items->data, m_attrs.ipaddr);
5452                 exit(-1);
5453             }
5454             continue;
5455
5456         case VL_INDEXERANGE:
5457             vcode = 0; /* not an error, just means we're done */
5458             goto out;
5459
5460         default: /* any other error */
5461             fprintf(STDERR, "vos: could not list the server addresses\n");
5462             PrintError("", vcode);
5463             goto out;
5464         }
5465
5466         /* if -uuid or -host, only list one response */
5467         if (as->parms[1].items || as->parms[0].items)
5468             break;
5469     }
5470
5471 out:
5472     xdr_free((xdrproc_t)xdr_bulkaddrs, &m_addrs);
5473     return vcode;
5474 }
5475
5476
5477 static int
5478 SetAddrs(struct cmd_syndesc *as, void *arock)
5479 {
5480     afs_int32 vcode;
5481     bulkaddrs m_addrs;
5482     afsUUID askuuid;
5483     afs_uint32 FS_HostAddrs_HBO[ADDRSPERSITE];
5484
5485     memset(&m_addrs, 0, sizeof(bulkaddrs));
5486     memset(&askuuid, 0, sizeof(afsUUID));
5487     if (as->parms[0].items) {
5488         /* -uuid */
5489         if (afsUUID_from_string(as->parms[0].items->data, &askuuid) < 0) {
5490             fprintf(STDERR, "vos: invalid UUID '%s'\n",
5491                     as->parms[0].items->data);
5492             exit(-1);
5493         }
5494     }
5495     if (as->parms[1].items) {
5496         /* -host */
5497         struct cmd_item *ti;
5498         afs_uint32 saddr;
5499         int i = 0;
5500
5501         for (ti = as->parms[1].items; ti && i < ADDRSPERSITE; ti = ti->next) {
5502
5503             if (noresolve)
5504                 saddr = GetServerNoresolve(ti->data);
5505             else
5506                 saddr = GetServer(ti->data);
5507
5508             if (!saddr) {
5509                 fprintf(STDERR, "vos: Can't get host info for '%s'\n",
5510                         ti->data);
5511                 exit(-1);
5512             }
5513             /* Convert it to host byte order */
5514             FS_HostAddrs_HBO[i] = ntohl(saddr);
5515             i++;
5516         }
5517         m_addrs.bulkaddrs_len = i;
5518         m_addrs.bulkaddrs_val = FS_HostAddrs_HBO;
5519     }
5520
5521     vcode = ubik_VL_RegisterAddrs(cstruct, 0, &askuuid, 0, &m_addrs);
5522
5523     if (vcode) {
5524         if (vcode == VL_MULTIPADDR) {
5525             fprintf(STDERR, "vos: VL_RegisterAddrs rpc failed; The IP address exists on a different server; repair it\n");
5526         } else if (vcode == RXGEN_OPCODE) {
5527             fprintf(STDERR, "vlserver doesn't support VL_RegisterAddrs rpc; ignored\n");
5528         } else {
5529             fprintf(STDERR, "vos: VL_RegisterAddrs rpc failed\n");
5530         }
5531         PrintError("", vcode);
5532         return vcode;
5533     }
5534     if (verbose) {
5535         fprintf(STDOUT, "vos: Changed UUID with addresses:\n");
5536         print_addrs(&m_addrs, &askuuid, m_addrs.bulkaddrs_len, 1);
5537     }
5538     return 0;
5539 }
5540
5541 static int
5542 RemoveAddrs(struct cmd_syndesc *as, void *arock)
5543 {
5544     afs_int32 code;
5545     ListAddrByAttributes attrs;
5546     afsUUID uuid;
5547     afs_int32 uniq = 0;
5548     afs_int32 nentries = 0;
5549     bulkaddrs addrs;
5550     afs_uint32 ip1;
5551     afs_uint32 ip2;
5552
5553     memset(&attrs, 0, sizeof(ListAddrByAttributes));
5554     memset(&addrs, 0, sizeof(bulkaddrs));
5555     memset(&uuid, 0, sizeof(afsUUID));
5556     attrs.Mask = VLADDR_UUID;
5557
5558     if (as->parms[0].items) {   /* -uuid */
5559         if (afsUUID_from_string(as->parms[0].items->data, &attrs.uuid) < 0) {
5560             fprintf(STDERR, "vos: invalid UUID '%s'\n",
5561                     as->parms[0].items->data);
5562             return EINVAL;
5563         }
5564     }
5565
5566     code =
5567         ubik_VL_GetAddrsU(cstruct, UBIK_CALL_NEW, &attrs, &uuid, &uniq,
5568                           &nentries, &addrs);
5569     if (code == VL_NOENT) {
5570         fprintf(STDERR, "vos: UUID not found\n");
5571         goto out;
5572     }
5573     if (code != 0) {
5574         fprintf(STDERR, "vos: could not list the server addresses\n");
5575         PrintError("", code);
5576         goto out;
5577     }
5578     if (addrs.bulkaddrs_len == 0) {
5579         fprintf(STDERR, "vos: no addresses found for UUID\n");
5580         goto out;
5581     }
5582
5583     ip2 = addrs.bulkaddrs_val[0]; /* network byte order */
5584     ip1 = 0xffffffff;             /* indicates removal mode */
5585
5586     if (verbose) {
5587         printf("vos: Removing UUID with hosts:\n");
5588         print_addrs(&addrs, &uuid, nentries, 1);
5589     }
5590
5591     code = ubik_VL_ChangeAddr(cstruct, UBIK_CALL_NEW, ip1, ip2);
5592     if (code) {
5593         fprintf(STDERR, "Could not remove server entry from the VLDB.\n");
5594         PrintError("", code);
5595     }
5596
5597   out:
5598     xdr_free((xdrproc_t) xdr_bulkaddrs, &addrs);
5599     return code;
5600 }
5601
5602
5603 static int
5604 LockEntry(struct cmd_syndesc *as, void *arock)
5605 {
5606     afs_uint32 avolid;
5607     afs_int32 vcode, err;
5608
5609     avolid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
5610     if (avolid == 0) {
5611         if (err)
5612             PrintError("", err);
5613         else
5614             fprintf(STDERR, "vos: can't find volume '%s'\n",
5615                     as->parms[0].items->data);
5616         exit(1);
5617     }
5618     vcode = ubik_VL_SetLock(cstruct, 0, avolid, -1, VLOP_DELETE);
5619     if (vcode) {
5620         fprintf(STDERR, "Could not lock VLDB entry for volume %s\n",
5621                 as->parms[0].items->data);
5622         PrintError("", vcode);
5623         exit(1);
5624     }
5625     fprintf(STDOUT, "Locked VLDB entry for volume %s\n",
5626             as->parms[0].items->data);
5627     return 0;
5628 }
5629
5630 static int
5631 ConvertRO(struct cmd_syndesc *as, void *arock)
5632 {
5633     afs_int32 partition = -1;
5634     afs_uint32 volid;
5635     afs_uint32 server;
5636     afs_int32 code, i, same;
5637     struct nvldbentry entry;
5638     afs_int32 vcode;
5639     afs_uint32 rwserver = 0;
5640     afs_int32 rwpartition = 0;
5641     afs_uint32 roserver = 0;
5642     afs_int32 ropartition = 0;
5643     int force = 0;
5644     int c, dc;
5645
5646     server = GetServer(as->parms[0].items->data);
5647     if (!server) {
5648         fprintf(STDERR, "vos: host '%s' not found in host table\n",
5649                 as->parms[0].items->data);
5650         return ENOENT;
5651     }
5652     partition = volutil_GetPartitionID(as->parms[1].items->data);
5653     if (partition < 0) {
5654         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
5655                 as->parms[1].items->data);
5656         return ENOENT;
5657     }
5658     if (!IsPartValid(partition, server, &code)) {
5659         if (code)
5660             PrintError("", code);
5661         else
5662             fprintf(STDERR,
5663                     "vos : partition %s does not exist on the server\n",
5664                     as->parms[1].items->data);
5665         return ENOENT;
5666     }
5667     volid = vsu_GetVolumeID(as->parms[2].items->data, cstruct, &code);
5668     if (volid == 0) {
5669         if (code)
5670             PrintError("", code);
5671         else
5672             fprintf(STDERR, "Unknown volume ID or name '%s'\n",
5673                     as->parms[2].items->data);
5674         return -1;
5675     }
5676     if (as->parms[3].items)
5677         force = 1;
5678
5679     memset(&entry, 0, sizeof(entry));
5680     vcode = VLDB_GetEntryByID(volid, -1, &entry);
5681     if (vcode) {
5682         fprintf(STDERR,
5683                 "Could not fetch the entry for volume %lu from VLDB\n",
5684                 (unsigned long)volid);
5685         PrintError("convertROtoRW ", vcode);
5686         return vcode;
5687     }
5688
5689     /* use RO volid even if user specified RW or BK volid */
5690     if (volid != entry.volumeId[ROVOL])
5691         volid = entry.volumeId[ROVOL];
5692
5693     MapHostToNetwork(&entry);
5694     for (i = 0; i < entry.nServers; i++) {
5695         if (entry.serverFlags[i] & VLSF_RWVOL) {
5696             rwserver = entry.serverNumber[i];
5697             rwpartition = entry.serverPartition[i];
5698             if (roserver)
5699                 break;
5700         } else if ((entry.serverFlags[i] & VLSF_ROVOL) && !roserver) {
5701             same = VLDB_IsSameAddrs(server, entry.serverNumber[i], &code);
5702             if (code) {
5703                 fprintf(STDERR,
5704                         "Failed to get info about server's %d address(es) from vlserver (err=%d); aborting call!\n",
5705                         server, code);
5706                 return ENOENT;
5707             }
5708             if (same) {
5709                 roserver = entry.serverNumber[i];
5710                 ropartition = entry.serverPartition[i];
5711                 if (rwserver)
5712                      break;
5713             }
5714         }
5715     }
5716     if (!roserver) {
5717         fprintf(STDERR, "Warning: RO volume didn't exist in vldb!\n");
5718     }
5719     if (roserver && (ropartition != partition)) {
5720         fprintf(STDERR,
5721                 "Warning: RO volume should be in partition %d instead of %d (vldb)\n",
5722                 ropartition, partition);
5723     }
5724
5725     if (rwserver) {
5726         fprintf(STDERR,
5727                 "VLDB indicates that a RW volume exists already on %s in partition %s.\n",
5728                 hostutil_GetNameByINet(rwserver),
5729                 volutil_PartitionName(rwpartition));
5730         if (!force) {
5731             fprintf(STDERR, "Overwrite this VLDB entry? [y|n] (n)\n");
5732             dc = c = getchar();
5733             while (!(dc == EOF || dc == '\n'))
5734                 dc = getchar(); /* goto end of line */
5735             if ((c != 'y') && (c != 'Y')) {
5736                 fprintf(STDERR, "aborted.\n");
5737                 return -1;
5738             }
5739         }
5740     }
5741
5742     code = UV_ConvertRO(server, partition, volid, &entry);
5743
5744     return code;
5745 }
5746
5747 static int
5748 Sizes(struct cmd_syndesc *as, void *arock)
5749 {
5750     afs_uint32 avolid;
5751     afs_uint32 aserver;
5752     afs_int32 apart, voltype, fromdate = 0, code, err, i;
5753     struct nvldbentry entry;
5754     volintSize vol_size;
5755
5756     rx_SetRxDeadTime(60 * 10);
5757     for (i = 0; i < MAXSERVERS; i++) {
5758         struct rx_connection *rxConn = ubik_GetRPCConn(cstruct, i);
5759         if (rxConn == 0)
5760             break;
5761         rx_SetConnDeadTime(rxConn, rx_connDeadTime);
5762         if (rx_ServiceOf(rxConn))
5763             rx_ServiceOf(rxConn)->connDeadTime = rx_connDeadTime;
5764     }
5765
5766     avolid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
5767     if (avolid == 0) {
5768         if (err)
5769             PrintError("", err);
5770         else
5771             fprintf(STDERR, "vos: can't find volume '%s'\n",
5772                     as->parms[0].items->data);
5773         return ENOENT;
5774     }
5775
5776     if (as->parms[1].items || as->parms[2].items) {
5777         if (!as->parms[1].items || !as->parms[2].items) {
5778             fprintf(STDERR,
5779                     "Must specify both -server and -partition options\n");
5780             return -1;
5781         }
5782         aserver = GetServer(as->parms[2].items->data);
5783         if (aserver == 0) {
5784             fprintf(STDERR, "Invalid server name\n");
5785             return -1;
5786         }
5787         apart = volutil_GetPartitionID(as->parms[1].items->data);
5788         if (apart < 0) {
5789             fprintf(STDERR, "Invalid partition name\n");
5790             return -1;
5791         }
5792     } else {
5793         code = GetVolumeInfo(avolid, &aserver, &apart, &voltype, &entry);
5794         if (code)
5795             return code;
5796     }
5797
5798     fromdate = 0;
5799
5800     if (as->parms[4].items && strcmp(as->parms[4].items->data, "0")) {
5801         code = ktime_DateToInt32(as->parms[4].items->data, &fromdate);
5802         if (code) {
5803             fprintf(STDERR, "vos: failed to parse date '%s' (error=%d))\n",
5804                     as->parms[4].items->data, code);
5805             return code;
5806         }
5807     }
5808
5809     fprintf(STDOUT, "Volume: %s\n", as->parms[0].items->data);
5810
5811     if (as->parms[3].items) {   /* do the dump estimate */
5812         vol_size.dump_size = 0;
5813         code = UV_GetSize(avolid, aserver, apart, fromdate, &vol_size);
5814         if (code) {
5815             PrintDiagnostics("size", code);
5816             return code;
5817         }
5818         /* presumably the size info is now gathered in pntr */
5819         /* now we display it */
5820
5821         fprintf(STDOUT, "dump_size: %llu\n", vol_size.dump_size);
5822     }
5823
5824     /* Display info */
5825
5826     return 0;
5827 }
5828
5829 static int
5830 EndTrans(struct cmd_syndesc *as, void *arock)
5831 {
5832     afs_uint32 server;
5833     afs_int32 code, tid, rcode;
5834     struct rx_connection *aconn;
5835
5836     server = GetServer(as->parms[0].items->data);
5837     if (!server) {
5838         fprintf(STDERR, "vos: host '%s' not found in host table\n",
5839                 as->parms[0].items->data);
5840         return EINVAL;
5841     }
5842
5843     code = util_GetInt32(as->parms[1].items->data, &tid);
5844     if (code) {
5845         fprintf(STDERR, "vos: bad integer specified for transaction ID.\n");
5846         return code;
5847     }
5848
5849     aconn = UV_Bind(server, AFSCONF_VOLUMEPORT);
5850     code = AFSVolEndTrans(aconn, tid, &rcode);
5851     if (!code) {
5852         code = rcode;
5853     }
5854
5855     if (code) {
5856         PrintDiagnostics("endtrans", code);
5857         return 1;
5858     }
5859
5860     return 0;
5861 }
5862
5863 int
5864 PrintDiagnostics(char *astring, afs_int32 acode)
5865 {
5866     switch (acode) {
5867     case EACCES:
5868         fprintf(STDERR,
5869                 "You are not authorized to perform the 'vos %s' command (%d)\n",
5870                 astring, acode);
5871         break;
5872     case EXDEV:
5873         fprintf(STDERR, "Error in vos %s command.\n", astring);
5874         fprintf(STDERR, "Clone volume is not in the same partition as the read-write volume.\n");
5875         break;
5876     default:
5877         fprintf(STDERR, "Error in vos %s command.\n", astring);
5878         PrintError("", acode);
5879         break;
5880     }
5881     return 0;
5882 }
5883
5884
5885 #ifdef AFS_NT40_ENV
5886 static DWORD
5887 win32_enableCrypt(void)
5888 {
5889     HKEY parmKey;
5890     DWORD dummyLen;
5891     DWORD cryptall = 0;
5892     DWORD code;
5893
5894     /* Look up configuration parameters in Registry */
5895     code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
5896                         0, (IsWow64()?KEY_WOW64_64KEY:0)|KEY_QUERY_VALUE, &parmKey);
5897     if (code != ERROR_SUCCESS) {
5898         dummyLen = sizeof(cryptall);
5899         RegQueryValueEx(parmKey, "SecurityLevel", NULL, NULL,
5900                         (BYTE *) &cryptall, &dummyLen);
5901     }
5902     RegCloseKey (parmKey);
5903
5904     return cryptall;
5905 }
5906 #endif /* AFS_NT40_ENV */
5907
5908 static int
5909 MyBeforeProc(struct cmd_syndesc *as, void *arock)
5910 {
5911     char *tcell;
5912     char *rxgk_seclevel_str = NULL;
5913     afs_int32 code;
5914     int secFlags;
5915
5916     /* Initialize the ubik_client connection */
5917     rx_SetRxDeadTime(90);
5918     cstruct = NULL;
5919     secFlags = AFSCONF_SECOPTS_FALLBACK_NULL;
5920
5921     tcell = NULL;
5922     if (as->parms[COMMONPARM_OFFSET_CELL].items)        /* if -cell specified */
5923         tcell = as->parms[COMMONPARM_OFFSET_CELL].items->data;
5924
5925     if (as->parms[COMMONPARM_OFFSET_NOAUTH].items)
5926         secFlags |= AFSCONF_SECOPTS_NOAUTH;
5927
5928     if (as->parms[COMMONPARM_OFFSET_LOCALAUTH].items) { /* -localauth specified */
5929         secFlags |= AFSCONF_SECOPTS_LOCALAUTH;
5930         confdir = AFSDIR_SERVER_ETC_DIRPATH;
5931     }
5932
5933     if (as->parms[COMMONPARM_OFFSET_ENCRYPT].items     /* -encrypt specified */
5934 #ifdef AFS_NT40_ENV
5935         || win32_enableCrypt()
5936 #endif /* AFS_NT40_ENV */
5937          )
5938         secFlags |= AFSCONF_SECOPTS_ALWAYSENCRYPT;
5939
5940     if (as->parms[COMMONPARM_OFFSET_CONFIG].items)   /* -config flag set */
5941         confdir = as->parms[COMMONPARM_OFFSET_CONFIG].items->data;
5942
5943     if (cmd_OptionAsString(as, COMMONPARM_OFFSET_RXGK, &rxgk_seclevel_str) == 0) {
5944         if (strcmp(rxgk_seclevel_str, "clear") == 0)
5945             secFlags |= AFSCONF_SECOPTS_ALWAYSCLEAR;
5946         else if (strcmp(rxgk_seclevel_str, "auth") == 0)
5947             secFlags |= AFSCONF_SECOPTS_NEVERENCRYPT;
5948         else if (strcmp(rxgk_seclevel_str, "crypt") == 0) {
5949             /* don't need to set any flags; this is the default for rxgk */
5950         } else {
5951             fprintf(STDERR, "Invalid argument to -rxgk: %s\n", rxgk_seclevel_str);
5952             exit(1);
5953         }
5954         secFlags |= AFSCONF_SECOPTS_RXGK;
5955
5956         free(rxgk_seclevel_str);
5957         rxgk_seclevel_str = NULL;
5958     }
5959
5960     if ((code = vsu_ClientInit(confdir, tcell, secFlags, UV_SetSecurity,
5961                                &cstruct))) {
5962         fprintf(STDERR, "could not initialize VLDB library (code=%lu) \n",
5963                 (unsigned long)code);
5964         exit(1);
5965     }
5966     rxInitDone = 1;
5967     if (as->parms[COMMONPARM_OFFSET_VERBOSE].items)     /* -verbose flag set */
5968         verbose = 1;
5969     else
5970         verbose = 0;
5971     if (as->parms[COMMONPARM_OFFSET_NORESOLVE].items)   /* -noresolve flag set */
5972         noresolve = 1;
5973     else
5974         noresolve = 0;
5975
5976     return 0;
5977 }
5978
5979 int
5980 osi_audit(void)
5981 {
5982 /* this sucks but it works for now.
5983 */
5984     return 0;
5985 }
5986
5987 #include "AFS_component_version_number.c"
5988
5989 int
5990 main(int argc, char **argv)
5991 {
5992     afs_int32 code;
5993
5994     struct cmd_syndesc *ts;
5995
5996 #ifdef  AFS_AIX32_ENV
5997     /*
5998      * The following signal action for AIX is necessary so that in case of a
5999      * crash (i.e. core is generated) we can include the user's data section
6000      * in the core dump. Unfortunately, by default, only a partial core is
6001      * generated which, in many cases, isn't too useful.
6002      */
6003     struct sigaction nsa;
6004
6005     sigemptyset(&nsa.sa_mask);
6006     nsa.sa_handler = SIG_DFL;
6007     nsa.sa_flags = SA_FULLDUMP;
6008     sigaction(SIGSEGV, &nsa, NULL);
6009 #endif
6010
6011     confdir = AFSDIR_CLIENT_ETC_DIRPATH;
6012
6013     cmd_SetBeforeProc(MyBeforeProc, NULL);
6014
6015     ts = cmd_CreateSyntax("create", CreateVolume, NULL, 0, "create a new volume");
6016     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
6017     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "partition name");
6018     cmd_AddParm(ts, "-name", CMD_SINGLE, 0, "volume name");
6019     cmd_AddParm(ts, "-maxquota", CMD_SINGLE, CMD_OPTIONAL,
6020                 "initial quota (KB)");
6021     cmd_AddParm(ts, "-id", CMD_SINGLE, CMD_OPTIONAL, "volume ID");
6022     cmd_AddParm(ts, "-roid", CMD_SINGLE, CMD_OPTIONAL, "readonly volume ID");
6023     COMMONPARMS;
6024
6025     ts = cmd_CreateSyntax("remove", DeleteVolume, NULL, 0, "delete a volume");
6026     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "machine name");
6027     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
6028     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6029
6030     COMMONPARMS;
6031
6032     ts = cmd_CreateSyntax("move", MoveVolume, NULL, 0, "move a volume");
6033     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6034     cmd_AddParm(ts, "-fromserver", CMD_SINGLE, 0, "machine name on source");
6035     cmd_AddParm(ts, "-frompartition", CMD_SINGLE, 0,
6036                 "partition name on source");
6037     cmd_AddParm(ts, "-toserver", CMD_SINGLE, 0,
6038                 "machine name on destination");
6039     cmd_AddParm(ts, "-topartition", CMD_SINGLE, 0,
6040                 "partition name on destination");
6041     cmd_AddParm(ts, "-live", CMD_FLAG, CMD_OPTIONAL,
6042                 "copy live volume without cloning");
6043     COMMONPARMS;
6044
6045     ts = cmd_CreateSyntax("copy", CopyVolume, NULL, 0, "copy a volume");
6046     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID on source");
6047     cmd_AddParm(ts, "-fromserver", CMD_SINGLE, 0, "machine name on source");
6048     cmd_AddParm(ts, "-frompartition", CMD_SINGLE, 0,
6049                 "partition name on source");
6050     cmd_AddParm(ts, "-toname", CMD_SINGLE, 0, "volume name on destination");
6051     cmd_AddParm(ts, "-toserver", CMD_SINGLE, 0,
6052                 "machine name on destination");
6053     cmd_AddParm(ts, "-topartition", CMD_SINGLE, 0,
6054                 "partition name on destination");
6055     cmd_AddParm(ts, "-offline", CMD_FLAG, CMD_OPTIONAL,
6056                 "leave new volume offline");
6057     cmd_AddParm(ts, "-readonly", CMD_FLAG, CMD_OPTIONAL,
6058                 "make new volume read-only");
6059     cmd_AddParm(ts, "-live", CMD_FLAG, CMD_OPTIONAL,
6060                 "copy live volume without cloning");
6061     COMMONPARMS;
6062
6063     ts = cmd_CreateSyntax("shadow", ShadowVolume, NULL, 0,
6064                           "make or update a shadow volume");
6065     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID on source");
6066     cmd_AddParm(ts, "-fromserver", CMD_SINGLE, 0, "machine name on source");
6067     cmd_AddParm(ts, "-frompartition", CMD_SINGLE, 0,
6068                 "partition name on source");
6069     cmd_AddParm(ts, "-toserver", CMD_SINGLE, 0,
6070                 "machine name on destination");
6071     cmd_AddParm(ts, "-topartition", CMD_SINGLE, 0,
6072                 "partition name on destination");
6073     cmd_AddParm(ts, "-toname", CMD_SINGLE, CMD_OPTIONAL,
6074                 "volume name on destination");
6075     cmd_AddParm(ts, "-toid", CMD_SINGLE, CMD_OPTIONAL,
6076                 "volume ID on destination");
6077     cmd_AddParm(ts, "-offline", CMD_FLAG, CMD_OPTIONAL,
6078                 "leave shadow volume offline");
6079     cmd_AddParm(ts, "-readonly", CMD_FLAG, CMD_OPTIONAL,
6080                 "make shadow volume read-only");
6081     cmd_AddParm(ts, "-live", CMD_FLAG, CMD_OPTIONAL,
6082                 "copy live volume without cloning");
6083     cmd_AddParm(ts, "-incremental", CMD_FLAG, CMD_OPTIONAL,
6084                 "do incremental update if target exists");
6085     COMMONPARMS;
6086
6087     ts = cmd_CreateSyntax("backup", BackupVolume, NULL, 0,
6088                           "make backup of a volume");
6089     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6090     COMMONPARMS;
6091
6092     ts = cmd_CreateSyntax("clone", CloneVolume, NULL, 0,
6093                           "make clone of a volume");
6094     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6095     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "server");
6096     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition");
6097     cmd_AddParm(ts, "-toname", CMD_SINGLE, CMD_OPTIONAL,
6098                 "volume name on destination");
6099     cmd_AddParm(ts, "-toid", CMD_SINGLE, CMD_OPTIONAL,
6100                 "volume ID on destination");
6101     cmd_AddParm(ts, "-offline", CMD_FLAG, CMD_OPTIONAL,
6102                 "leave clone volume offline");
6103     cmd_AddParm(ts, "-readonly", CMD_FLAG, CMD_OPTIONAL,
6104                 "make clone volume read-only, not readwrite");
6105     cmd_AddParm(ts, "-readwrite", CMD_FLAG, CMD_OPTIONAL,
6106                 "make clone volume readwrite, not read-only");
6107     COMMONPARMS;
6108
6109     ts = cmd_CreateSyntax("release", ReleaseVolume, NULL, 0, "release a volume");
6110     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6111     cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL,
6112                 "force a complete release and full dumps");
6113     cmd_AddParmAlias(ts, 1, "-f"); /* original force option */
6114     /* keep this reserved */
6115     cmd_AddParm(ts, "-stayonline", CMD_FLAG, CMD_OPTIONAL|CMD_HIDDEN,
6116                 "release to cloned temp vol, then clone back to repsite RO");
6117     cmd_AddParm(ts, "-force-reclone", CMD_FLAG, CMD_OPTIONAL,
6118                 "force a reclone and complete release with incremental dumps");
6119     COMMONPARMS;
6120
6121     ts = cmd_CreateSyntax("dump", DumpVolumeCmd, NULL, 0, "dump a volume");
6122     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6123     cmd_AddParm(ts, "-time", CMD_SINGLE, CMD_OPTIONAL, "dump from time");
6124     cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL, "dump file");
6125     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "server");
6126     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition");
6127     cmd_AddParm(ts, "-clone", CMD_FLAG, CMD_OPTIONAL,
6128                 "dump a clone of the volume");
6129     cmd_AddParm(ts, "-omitdirs", CMD_FLAG, CMD_OPTIONAL,
6130                 "omit unchanged directories from an incremental dump");
6131     COMMONPARMS;
6132
6133     ts = cmd_CreateSyntax("restore", RestoreVolumeCmd, NULL, 0,
6134                           "restore a volume");
6135     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
6136     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "partition name");
6137     cmd_AddParm(ts, "-name", CMD_SINGLE, 0, "name of volume to be restored");
6138     cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL, "dump file");
6139     cmd_AddParm(ts, "-id", CMD_SINGLE, CMD_OPTIONAL, "volume ID");
6140     cmd_AddParm(ts, "-overwrite", CMD_SINGLE, CMD_OPTIONAL,
6141                 "abort | full | incremental");
6142     cmd_AddParm(ts, "-offline", CMD_FLAG, CMD_OPTIONAL,
6143                 "leave restored volume offline");
6144     cmd_AddParm(ts, "-readonly", CMD_FLAG, CMD_OPTIONAL,
6145                 "make restored volume read-only");
6146     cmd_AddParm(ts, "-creation", CMD_SINGLE, CMD_OPTIONAL,
6147                 "dump | keep | new");
6148     cmd_AddParm(ts, "-lastupdate", CMD_SINGLE, CMD_OPTIONAL,
6149                 "dump | keep | new");
6150     cmd_AddParm(ts, "-nodelete", CMD_FLAG, CMD_OPTIONAL,
6151                 "do not delete old site when restoring to a new site");
6152     COMMONPARMS;
6153
6154     ts = cmd_CreateSyntax("unlock", LockReleaseCmd, NULL, 0,
6155                           "release lock on VLDB entry for a volume");
6156     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6157     COMMONPARMS;
6158
6159     ts = cmd_CreateSyntax("changeloc", ChangeLocation, NULL, 0,
6160                           "change an RW volume's location in the VLDB");
6161     cmd_AddParm(ts, "-server", CMD_SINGLE, 0,
6162                 "machine name for new location");
6163     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0,
6164                 "partition name for new location");
6165     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6166     COMMONPARMS;
6167
6168     ts = cmd_CreateSyntax("addsite", AddSite, NULL, 0, "add a replication site");
6169     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name for new site");
6170     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0,
6171                 "partition name for new site");
6172     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6173     cmd_AddParm(ts, "-roid", CMD_SINGLE, CMD_OPTIONAL, "volume name or ID for RO");
6174     cmd_AddParm(ts, "-valid", CMD_FLAG, CMD_OPTIONAL, "publish as an up-to-date site in VLDB");
6175     COMMONPARMS;
6176
6177     ts = cmd_CreateSyntax("remsite", RemoveSite, NULL, 0,
6178                           "remove a replication site");
6179     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
6180     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "partition name");
6181     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6182     COMMONPARMS;
6183
6184     ts = cmd_CreateSyntax("listpart", ListPartitions, NULL, 0, "list partitions");
6185     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
6186     COMMONPARMS;
6187
6188     ts = cmd_CreateSyntax("listvol", ListVolumes, NULL, 0,
6189                           "list volumes on server (bypass VLDB)");
6190     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
6191     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
6192     cmd_AddParm(ts, "-fast", CMD_FLAG, CMD_OPTIONAL, "minimal listing");
6193     cmd_AddParm(ts, "-long", CMD_FLAG, CMD_OPTIONAL,
6194                 "list all normal volume fields");
6195     cmd_AddParm(ts, "-quiet", CMD_FLAG, CMD_OPTIONAL,
6196                 "generate minimal information");
6197     cmd_AddParm(ts, "-extended", CMD_FLAG, CMD_OPTIONAL,
6198                 "list extended volume fields");
6199     cmd_AddParm(ts, "-format", CMD_FLAG, CMD_OPTIONAL,
6200                 "machine readable format");
6201     COMMONPARMS;
6202
6203     ts = cmd_CreateSyntax("syncvldb", SyncVldb, NULL, 0,
6204                           "synchronize VLDB with server");
6205     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "machine name");
6206     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
6207     cmd_AddParm(ts, "-volume", CMD_SINGLE, CMD_OPTIONAL, "volume name or ID");
6208     cmd_AddParm(ts, "-dryrun", CMD_FLAG, CMD_OPTIONAL, "list what would be done, don't do it");
6209     COMMONPARMS;
6210
6211     ts = cmd_CreateSyntax("syncserv", SyncServer, NULL, 0,
6212                           "synchronize server with VLDB");
6213     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
6214     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
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("examine", ExamineVolume, NULL, 0,
6219                           "everything about the volume");
6220     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6221     cmd_AddParm(ts, "-extended", CMD_FLAG, CMD_OPTIONAL,
6222                 "list extended volume fields");
6223     cmd_AddParm(ts, "-format", CMD_FLAG, CMD_OPTIONAL,
6224                 "machine readable format");
6225     COMMONPARMS;
6226     cmd_CreateAlias(ts, "volinfo");
6227     cmd_CreateAlias(ts, "e");
6228
6229     ts = cmd_CreateSyntax("setfields", SetFields, NULL, 0,
6230                           "change volume info fields");
6231     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6232     cmd_AddParm(ts, "-maxquota", CMD_SINGLE, CMD_OPTIONAL, "quota (KB)");
6233     cmd_AddParm(ts, "-clearuse", CMD_FLAG, CMD_OPTIONAL, "clear dayUse");
6234     cmd_AddParm(ts, "-clearVolUpCounter", CMD_FLAG, CMD_OPTIONAL, "clear volUpdateCounter");
6235     COMMONPARMS;
6236
6237     ts = cmd_CreateSyntax("offline", volOffline, NULL, 0, "force the volume status to offline");
6238     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "server name");
6239     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "partition name");
6240     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6241     cmd_AddParm(ts, "-sleep", CMD_SINGLE, CMD_OPTIONAL, "seconds to sleep");
6242     cmd_AddParm(ts, "-busy", CMD_FLAG, CMD_OPTIONAL, "busy volume");
6243     COMMONPARMS;
6244
6245     ts = cmd_CreateSyntax("online", volOnline, NULL, 0, "force the volume status to online");
6246     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "server name");
6247     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "partition name");
6248     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6249     COMMONPARMS;
6250
6251     ts = cmd_CreateSyntax("zap", VolumeZap, NULL, 0,
6252                           "delete the volume, don't bother with VLDB");
6253     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
6254     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "partition name");
6255     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume ID");
6256     cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL,
6257                 "force deletion of bad volumes");
6258     cmd_AddParm(ts, "-backup", CMD_FLAG, CMD_OPTIONAL,
6259                 "also delete backup volume if one is found");
6260     COMMONPARMS;
6261
6262     ts = cmd_CreateSyntax("status", VolserStatus, NULL, 0,
6263                           "report on volser status");
6264     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
6265     COMMONPARMS;
6266
6267     ts = cmd_CreateSyntax("rename", RenameVolume, NULL, 0, "rename a volume");
6268     cmd_AddParm(ts, "-oldname", CMD_SINGLE, 0, "old volume name ");
6269     cmd_AddParm(ts, "-newname", CMD_SINGLE, 0, "new volume name ");
6270     COMMONPARMS;
6271
6272     ts = cmd_CreateSyntax("listvldb", ListVLDB, NULL, 0,
6273                           "list volumes in the VLDB");
6274     cmd_AddParm(ts, "-name", CMD_SINGLE, CMD_OPTIONAL, "volume name or ID");
6275     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "machine name");
6276     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
6277     cmd_AddParm(ts, "-locked", CMD_FLAG, CMD_OPTIONAL, "locked volumes only");
6278     cmd_AddParm(ts, "-quiet", CMD_FLAG, CMD_OPTIONAL,
6279                 "generate minimal information");
6280     cmd_AddParm(ts, "-nosort", CMD_FLAG, CMD_OPTIONAL,
6281                 "do not alphabetically sort the volume names");
6282     COMMONPARMS;
6283
6284     ts = cmd_CreateSyntax("backupsys", BackSys, NULL, 0, "en masse backups");
6285     cmd_AddParm(ts, "-prefix", CMD_LIST, CMD_OPTIONAL,
6286                 "common prefix on volume(s)");
6287     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "machine name");
6288     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
6289     cmd_AddParm(ts, "-exclude", CMD_FLAG, CMD_OPTIONAL,
6290                 "exclude common prefix volumes");
6291     cmd_AddParm(ts, "-xprefix", CMD_LIST, CMD_OPTIONAL,
6292                 "negative prefix on volume(s)");
6293     cmd_AddParm(ts, "-dryrun", CMD_FLAG, CMD_OPTIONAL, "list what would be done, don't do it");
6294     COMMONPARMS;
6295
6296     ts = cmd_CreateSyntax("delentry", DeleteEntry, NULL, 0,
6297                           "delete VLDB entry for a volume");
6298     cmd_AddParm(ts, "-id", CMD_LIST, CMD_OPTIONAL, "volume name or ID");
6299     cmd_AddParm(ts, "-prefix", CMD_SINGLE, CMD_OPTIONAL,
6300                 "prefix of the volume whose VLDB entry is to be deleted");
6301     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "machine name");
6302     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
6303     cmd_AddParm(ts, "-noexecute", CMD_FLAG, CMD_OPTIONAL|CMD_HIDDEN, "");
6304     cmd_AddParm(ts, "-dryrun", CMD_FLAG, CMD_OPTIONAL,
6305                 "list what would be done, don't do it");
6306     COMMONPARMS;
6307
6308     ts = cmd_CreateSyntax("partinfo", PartitionInfo, NULL, 0,
6309                           "list partition information");
6310     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
6311     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
6312     cmd_AddParm(ts, "-summary", CMD_FLAG, CMD_OPTIONAL,
6313                 "print storage summary");
6314     COMMONPARMS;
6315
6316     ts = cmd_CreateSyntax("unlockvldb", UnlockVLDB, NULL, 0,
6317                           "unlock all the locked entries in the VLDB");
6318     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "machine name");
6319     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
6320     COMMONPARMS;
6321
6322     ts = cmd_CreateSyntax("lock", LockEntry, NULL, 0,
6323                           "lock VLDB entry for a volume");
6324     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6325     COMMONPARMS;
6326
6327     ts = cmd_CreateSyntax("changeaddr", ChangeAddr, NULL, 0,
6328                           "change the IP address of a file server");
6329     cmd_AddParm(ts, "-oldaddr", CMD_SINGLE, 0, "original IP address");
6330     cmd_AddParm(ts, "-newaddr", CMD_SINGLE, CMD_OPTIONAL, "new IP address");
6331     cmd_AddParm(ts, "-remove", CMD_FLAG, CMD_OPTIONAL,
6332                 "remove the IP address from the VLDB");
6333     cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL,
6334                 "allow multi-homed server entry change (not recommended)");
6335     COMMONPARMS;
6336
6337     ts = cmd_CreateSyntax("listaddrs", ListAddrs, NULL, 0,
6338                           "list the IP address of all file servers registered in the VLDB");
6339     cmd_AddParm(ts, "-uuid", CMD_SINGLE, CMD_OPTIONAL, "uuid of server");
6340     cmd_AddParm(ts, "-host", CMD_SINGLE, CMD_OPTIONAL, "address of host");
6341     cmd_AddParm(ts, "-printuuid", CMD_FLAG, CMD_OPTIONAL,
6342                 "print uuid of hosts");
6343     COMMONPARMS;
6344
6345     ts = cmd_CreateSyntax("convertROtoRW", ConvertRO, NULL, 0,
6346                           "convert a RO volume into a RW volume (after loss of old RW volume)");
6347     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
6348     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "partition name");
6349     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6350     cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL, "don't ask");
6351     COMMONPARMS;
6352
6353     ts = cmd_CreateSyntax("size", Sizes, NULL, 0,
6354                           "obtain various sizes of the volume.");
6355     cmd_AddParm(ts, "-id", CMD_SINGLE, 0, "volume name or ID");
6356     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name");
6357     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL, "machine name");
6358     cmd_AddParm(ts, "-dump", CMD_FLAG, CMD_OPTIONAL,
6359                 "Obtain the size of the dump");
6360     cmd_AddParm(ts, "-time", CMD_SINGLE, CMD_OPTIONAL, "dump from time");
6361     COMMONPARMS;
6362
6363     ts = cmd_CreateSyntax("endtrans", EndTrans, NULL, 0,
6364                           "end a volserver transaction");
6365     cmd_AddParm(ts, "-server", CMD_SINGLE, 0, "machine name");
6366     cmd_AddParm(ts, "-transaction", CMD_SINGLE, 0,
6367                 "transaction ID");
6368     COMMONPARMS;
6369
6370     ts = cmd_CreateSyntax("setaddrs", SetAddrs, NULL, 0,
6371                           "set the list of IP addresses for a given UUID in the VLDB");
6372     cmd_AddParm(ts, "-uuid", CMD_SINGLE, 0, "uuid of server");
6373     cmd_AddParm(ts, "-host", CMD_LIST, 0, "address of host");
6374     COMMONPARMS;
6375
6376     ts = cmd_CreateSyntax("remaddrs", RemoveAddrs, NULL, 0,
6377                           "remove the list of IP addresses for a given UUID in the VLDB");
6378     cmd_AddParm(ts, "-uuid", CMD_SINGLE, 0, "uuid of server");
6379     COMMONPARMS;
6380
6381     code = cmd_Dispatch(argc, argv);
6382     if (rxInitDone) {
6383         /* Shut down the ubik_client and rx connections */
6384         if (cstruct) {
6385             (void)ubik_ClientDestroy(cstruct);
6386             cstruct = 0;
6387         }
6388         rx_Finalize();
6389     }
6390
6391     exit((code ? -1 : 0));
6392 }