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