78b8cc41a60d7756a00e357104deeaf075593ef0
[openafs.git] / src / volser / vos.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 #include <roken.h>
14
15 #ifdef IGNORE_SOME_GCC_WARNINGS
16 # pragma GCC diagnostic warning "-Wimplicit-function-declaration"
17 #endif
18
19 #ifdef AFS_NT40_ENV
20 #include <WINNT/afsreg.h>
21 #endif
22
23 #ifdef AFS_AIX_ENV
24 #include <sys/statfs.h>
25 #endif
26
27 #include <lock.h>
28 #include <afs/stds.h>
29 #include <rx/rx_queue.h>
30 #include <rx/xdr.h>
31 #include <rx/rx.h>
32 #include <rx/rx_globals.h>
33 #include <afs/nfs.h>
34 #include <afs/vlserver.h>
35 #include <afs/cellconfig.h>
36 #include <afs/keys.h>
37 #include <afs/afsutil.h>
38 #include <ubik.h>
39 #include <afs/afsint.h>
40 #include <afs/cmd.h>
41 #include <afs/usd.h>
42 #include "volser.h"
43 #include "volint.h"
44 #include <afs/ihandle.h>
45 #include <afs/vnode.h>
46 #include <afs/volume.h>
47 #include <afs/com_err.h>
48 #include "dump.h"
49 #include "lockdata.h"
50
51 #include "volser_internal.h"
52 #include "volser_prototypes.h"
53 #include "vsutils_prototypes.h"
54 #include "lockprocs_prototypes.h"
55
56 #ifdef HAVE_POSIX_REGEX
57 #include <regex.h>
58 #endif
59
60 /* Local Prototypes */
61 int PrintDiagnostics(char *astring, afs_int32 acode);
62 int GetVolumeInfo(afs_uint32 volid, afs_uint32 *server, afs_int32 *part,
63                   afs_int32 *voltype, struct nvldbentry *rentry);
64
65 struct tqElem {
66     afs_uint32 volid;
67     struct tqElem *next;
68 };
69
70 struct tqHead {
71     afs_int32 count;
72     struct tqElem *next;
73 };
74
75 #define COMMONPARMS     cmd_Seek(ts, 12);\
76 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cell name");\
77 cmd_AddParmAlias(ts, 12, "-c");   /* original -cell option */ \
78 cmd_AddParm(ts, "-noauth", CMD_FLAG, CMD_OPTIONAL, "don't authenticate");\
79 cmd_AddParm(ts, "-localauth",CMD_FLAG,CMD_OPTIONAL,"use server tickets");\
80 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, "verbose");\
81 cmd_AddParm(ts, "-encrypt", CMD_FLAG, CMD_OPTIONAL, "encrypt commands");\
82 cmd_AddParm(ts, "-noresolve", CMD_FLAG, CMD_OPTIONAL, "don't resolve addresses"); \
83 cmd_AddParm(ts, "-config", CMD_SINGLE, CMD_OPTIONAL, "config location"); \
84
85 #define ERROR_EXIT(code) do { \
86     error = (code); \
87     goto error_exit; \
88 } while (0)
89
90 int rxInitDone = 0;
91 extern struct ubik_client *cstruct;
92 const char *confdir;
93
94 static struct tqHead busyHead, notokHead;
95
96 static void
97 qInit(struct tqHead *ahead)
98 {
99     memset(ahead, 0, sizeof(struct tqHead));
100     return;
101 }
102
103
104 static void
105 qPut(struct tqHead *ahead, afs_uint32 volid)
106 {
107     struct tqElem *elem;
108
109     elem = malloc(sizeof(struct tqElem));
110     elem->next = ahead->next;
111     elem->volid = volid;
112     ahead->next = elem;
113     ahead->count++;
114     return;
115 }
116
117 static void
118 qGet(struct tqHead *ahead, afs_uint32 *volid)
119 {
120     struct tqElem *tmp;
121
122     if (ahead->count <= 0)
123         return;
124     *volid = ahead->next->volid;
125     tmp = ahead->next;
126     ahead->next = tmp->next;
127     ahead->count--;
128     free(tmp);
129     return;
130 }
131
132 /* returns 1 if <filename> exists else 0 */
133 static int
134 FileExists(char *filename)
135 {
136     usd_handle_t ufd;
137     int code;
138     afs_int64 size;
139
140     code = usd_Open(filename, USD_OPEN_RDONLY, 0, &ufd);
141     if (code) {
142         return 0;
143     }
144     code = USD_IOCTL(ufd, USD_IOCTL_GETSIZE, &size);
145     USD_CLOSE(ufd);
146     if (code) {
147         return 0;
148     }
149     return 1;
150 }
151
152 /* returns 1 if <name> doesnot end in .readonly or .backup, else 0 */
153 static int
154 VolNameOK(char *name)
155 {
156     size_t total;
157
158
159     total = strlen(name);
160     if (!strcmp(&name[total - 9], ".readonly")) {
161         return 0;
162     } else if (!strcmp(&name[total - 7], ".backup")) {
163         return 0;
164     } else {
165         return 1;
166     }
167 }
168
169 /* return 1 if name is a number else 0 */
170 static int
171 IsNumeric(char *name)
172 {
173     int result, i;
174     size_t len;
175     char *ptr;
176
177     result = 1;
178     ptr = name;
179     len = strlen(name);
180     for (i = 0; i < len; i++) {
181         if (*ptr < '0' || *ptr > '9') {
182             result = 0;
183             break;
184         }
185         ptr++;
186
187     }
188     return result;
189 }
190
191
192 /*
193  * Parse a server dotted address and return the address in network byte order
194  */
195 afs_uint32
196 GetServerNoresolve(char *aname)
197 {
198     int b1, b2, b3, b4;
199     afs_uint32 addr;
200     afs_int32 code;
201
202     code = sscanf(aname, "%d.%d.%d.%d", &b1, &b2, &b3, &b4);
203     if (code == 4) {
204         addr = (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
205         addr = htonl(addr);     /* convert to network byte order */
206         return addr;
207     } else
208         return 0;
209 }
210 /*
211  * Parse a server name/address and return the address in network byte order
212  */
213 afs_uint32
214 GetServer(char *aname)
215 {
216     struct hostent *th;
217     afs_uint32 addr; /* in network byte order */
218     afs_int32 code;
219     char hostname[MAXHOSTCHARS];
220
221     if ((addr = GetServerNoresolve(aname)) == 0) {
222         th = gethostbyname(aname);
223         if (!th)
224             return 0;
225         memcpy(&addr, th->h_addr, sizeof(addr));
226     }
227
228     if (rx_IsLoopbackAddr(ntohl(addr))) {       /* local host */
229         code = gethostname(hostname, MAXHOSTCHARS);
230         if (code)
231             return 0;
232         th = gethostbyname(hostname);
233         if (!th)
234             return 0;
235         memcpy(&addr, th->h_addr, sizeof(addr));
236     }
237
238     return (addr);
239 }
240
241 afs_int32
242 GetVolumeType(char *aname)
243 {
244
245     if (!strcmp(aname, "ro"))
246         return (ROVOL);
247     else if (!strcmp(aname, "rw"))
248         return (RWVOL);
249     else if (!strcmp(aname, "bk"))
250         return (BACKVOL);
251     else
252         return (-1);
253 }
254
255 int
256 IsPartValid(afs_int32 partId, afs_uint32 server, afs_int32 *code)
257 {
258     struct partList dummyPartList;
259     int i, success, cnt;
260
261     success = 0;
262     *code = 0;
263
264     *code = UV_ListPartitions(server, &dummyPartList, &cnt);
265     if (*code)
266         return success;
267     for (i = 0; i < cnt; i++) {
268         if (dummyPartList.partFlags[i] & PARTVALID)
269             if (dummyPartList.partId[i] == partId)
270                 success = 1;
271     }
272     return success;
273 }
274
275
276
277  /*sends the contents of file associated with <fd> and <blksize>  to Rx Stream
278   * associated  with <call> */
279 int
280 SendFile(usd_handle_t ufd, struct rx_call *call, long blksize)
281 {
282     char *buffer = (char *)0;
283     afs_int32 error = 0;
284     afs_uint32 nbytes;
285
286     buffer = malloc(blksize);
287     if (!buffer) {
288         fprintf(STDERR, "malloc failed\n");
289         return -1;
290     }
291
292     while (!error) {
293 #if !defined(AFS_NT40_ENV) && !defined(AFS_PTHREAD_ENV)
294         /* Only for this for non-NT, non-pthread. For NT, we can't select on
295          * non-socket FDs. For pthread environments, we don't need to select at
296          * all, since the following read() will block. */
297         fd_set in;
298         FD_ZERO(&in);
299         FD_SET((intptr_t)(ufd->handle), &in);
300         /* don't timeout if read blocks */
301         IOMGR_Select(((intptr_t)(ufd->handle)) + 1, &in, 0, 0, 0);
302 #endif
303         error = USD_READ(ufd, buffer, blksize, &nbytes);
304         if (error) {
305             fprintf(STDERR, "File system read failed: %s\n",
306                     afs_error_message(error));
307             break;
308         }
309
310         if (nbytes == 0)
311             break;
312
313         if (rx_Write(call, buffer, nbytes) != nbytes) {
314             error = -1;
315             break;
316         }
317     }
318     if (buffer)
319         free(buffer);
320     return error;
321 }
322
323 /* function invoked by UV_RestoreVolume, reads the data from rx_trx_stream and
324  * writes it out to the volume. */
325 afs_int32
326 WriteData(struct rx_call *call, void *rock)
327 {
328     char *filename = (char *) rock;
329     usd_handle_t ufd;
330     long blksize;
331     afs_int32 error, code;
332     int ufdIsOpen = 0;
333     afs_int64 currOffset;
334     afs_uint32 buffer;
335     afs_uint32 got;
336
337     error = 0;
338
339     if (!filename || !*filename) {
340         usd_StandardInput(&ufd);
341         blksize = 4096;
342         ufdIsOpen = 1;
343     } else {
344         code = usd_Open(filename, USD_OPEN_RDONLY, 0, &ufd);
345         if (code == 0) {
346             ufdIsOpen = 1;
347             code = USD_IOCTL(ufd, USD_IOCTL_GETBLKSIZE, &blksize);
348         }
349         if (code) {
350             fprintf(STDERR, "Could not access file '%s': %s\n", filename,
351                     afs_error_message(code));
352             error = VOLSERBADOP;
353             goto wfail;
354         }
355         /* test if we have a valid dump */
356         USD_SEEK(ufd, 0, SEEK_END, &currOffset);
357         USD_SEEK(ufd, currOffset - sizeof(afs_uint32), SEEK_SET, &currOffset);
358         USD_READ(ufd, (char *)&buffer, sizeof(afs_uint32), &got);
359         if ((got != sizeof(afs_uint32)) || (ntohl(buffer) != DUMPENDMAGIC)) {
360             fprintf(STDERR, "Signature missing from end of file '%s'\n", filename);
361             error = VOLSERBADOP;
362             goto wfail;
363         }
364         USD_SEEK(ufd, 0, SEEK_SET, &currOffset);
365     }
366     code = SendFile(ufd, call, blksize);
367     if (code) {
368         error = code;
369         goto wfail;
370     }
371   wfail:
372     if (ufdIsOpen) {
373         code = USD_CLOSE(ufd);
374         if (code) {
375             fprintf(STDERR, "Could not close dump file %s\n",
376                     (filename && *filename) ? filename : "STDOUT");
377             if (!error)
378                 error = code;
379         }
380     }
381     return error;
382 }
383
384 /* Receive data from <call> stream into file associated
385  * with <fd> <blksize>
386  */
387 int
388 ReceiveFile(usd_handle_t ufd, struct rx_call *call, long blksize)
389 {
390     char *buffer = NULL;
391     afs_int32 bytesread;
392     afs_uint32 bytesleft, w;
393     afs_int32 error = 0;
394
395     buffer = malloc(blksize);
396     if (!buffer) {
397         fprintf(STDERR, "memory allocation failed\n");
398         ERROR_EXIT(-1);
399     }
400
401     while ((bytesread = rx_Read(call, buffer, blksize)) > 0) {
402         for (bytesleft = bytesread; bytesleft; bytesleft -= w) {
403 #if !defined(AFS_NT40_ENV) && !defined(AFS_PTHREAD_ENV)
404             /* Only for this for non-NT, non-pthread. For NT, we can't select
405              * on non-socket FDs. For pthread environments, we don't need to
406              * select at all, since the following write() will block. */
407             fd_set out;
408             FD_ZERO(&out);
409             FD_SET((intptr_t)(ufd->handle), &out);
410             /* don't timeout if write blocks */
411             IOMGR_Select(((intptr_t)(ufd->handle)) + 1, 0, &out, 0, 0);
412 #endif
413             error =
414                 USD_WRITE(ufd, &buffer[bytesread - bytesleft], bytesleft, &w);
415             if (error) {
416                 fprintf(STDERR, "File system write failed: %s\n",
417                         afs_error_message(error));
418                 ERROR_EXIT(-1);
419             }
420         }
421     }
422
423   error_exit:
424     if (buffer)
425         free(buffer);
426     return (error);
427 }
428
429 afs_int32
430 DumpFunction(struct rx_call *call, void *rock)
431 {
432     char *filename = (char *)rock;
433     usd_handle_t ufd;           /* default is to stdout */
434     afs_int32 error = 0, code;
435     afs_int64 size;
436     long blksize;
437     int ufdIsOpen = 0;
438
439     /* Open the output file */
440     if (!filename || !*filename) {
441         usd_StandardOutput(&ufd);
442         blksize = 4096;
443         ufdIsOpen = 1;
444     } else {
445         code =
446             usd_Open(filename, USD_OPEN_CREATE | USD_OPEN_RDWR, 0666, &ufd);
447         if (code == 0) {
448             ufdIsOpen = 1;
449             size = 0;
450             code = USD_IOCTL(ufd, USD_IOCTL_SETSIZE, &size);
451         }
452         if (code == 0) {
453             code = USD_IOCTL(ufd, USD_IOCTL_GETBLKSIZE, &blksize);
454         }
455         if (code) {
456             fprintf(STDERR, "Could not create file '%s': %s\n", filename,
457                     afs_error_message(code));
458             ERROR_EXIT(VOLSERBADOP);
459         }
460     }
461
462     code = ReceiveFile(ufd, call, blksize);
463     if (code)
464         ERROR_EXIT(code);
465
466   error_exit:
467     /* Close the output file */
468     if (ufdIsOpen) {
469         code = USD_CLOSE(ufd);
470         if (code) {
471             fprintf(STDERR, "Could not close dump file %s\n",
472                     (filename && *filename) ? filename : "STDIN");
473             if (!error)
474                 error = code;
475         }
476     }
477
478     return (error);
479 }
480
481 static void
482 DisplayFormat(volintInfo *pntr, afs_uint32 server, afs_int32 part,
483               int *totalOK, int *totalNotOK, int *totalBusy, int fast,
484               int longlist, int disp)
485 {
486     char pname[10];
487     time_t t;
488
489     if (fast) {
490         fprintf(STDOUT, "%-10lu\n", (unsigned long)pntr->volid);
491     } else if (longlist) {
492         if (pntr->status == VOK) {
493             fprintf(STDOUT, "%-32s ", pntr->name);
494             fprintf(STDOUT, "%10lu ", (unsigned long)pntr->volid);
495             if (pntr->type == 0)
496                 fprintf(STDOUT, "RW ");
497             if (pntr->type == 1)
498                 fprintf(STDOUT, "RO ");
499             if (pntr->type == 2)
500                 fprintf(STDOUT, "BK ");
501             fprintf(STDOUT, "%10d K  ", pntr->size);
502             if (pntr->inUse == 1) {
503                 fprintf(STDOUT, "On-line");
504                 *totalOK += 1;
505             } else {
506                 fprintf(STDOUT, "Off-line");
507                 *totalNotOK += 1;
508             }
509             if (pntr->needsSalvaged == 1)
510                 fprintf(STDOUT, "**needs salvage**");
511             fprintf(STDOUT, "\n");
512             MapPartIdIntoName(part, pname);
513             fprintf(STDOUT, "    %s %s \n", hostutil_GetNameByINet(server),
514                     pname);
515             fprintf(STDOUT, "    RWrite %10lu ROnly %10lu Backup %10lu \n",
516                     (unsigned long)pntr->parentID,
517                     (unsigned long)pntr->cloneID,
518                     (unsigned long)pntr->backupID);
519             fprintf(STDOUT, "    MaxQuota %10d K \n", pntr->maxquota);
520             t = pntr->creationDate;
521             fprintf(STDOUT, "    Creation    %s",
522                     ctime(&t));
523             t = pntr->copyDate;
524             fprintf(STDOUT, "    Copy        %s",
525                     ctime(&t));
526
527             t = pntr->backupDate;
528             if (!t)
529                 fprintf(STDOUT, "    Backup      Never\n");
530             else
531                 fprintf(STDOUT, "    Backup      %s",
532                         ctime(&t));
533
534             t = pntr->accessDate;
535             if (t)
536                 fprintf(STDOUT, "    Last Access %s",
537                         ctime(&t));
538
539             t = pntr->updateDate;
540             if (!t)
541                 fprintf(STDOUT, "    Last Update Never\n");
542             else
543                 fprintf(STDOUT, "    Last Update %s",
544                         ctime(&t));
545             fprintf(STDOUT,
546                     "    %d accesses in the past day (i.e., vnode references)\n",
547                     pntr->dayUse);
548         } else if (pntr->status == VBUSY) {
549             *totalBusy += 1;
550             qPut(&busyHead, pntr->volid);
551             if (disp)
552                 fprintf(STDOUT, "**** Volume %lu is busy ****\n",
553                         (unsigned long)pntr->volid);
554         } else {
555             *totalNotOK += 1;
556             qPut(&notokHead, pntr->volid);
557             if (disp)
558                 fprintf(STDOUT, "**** Could not attach volume %lu ****\n",
559                         (unsigned long)pntr->volid);
560         }
561         fprintf(STDOUT, "\n");
562     } else {                    /* default listing */
563         if (pntr->status == VOK) {
564             fprintf(STDOUT, "%-32s ", pntr->name);
565             fprintf(STDOUT, "%10lu ", (unsigned long)pntr->volid);
566             if (pntr->type == 0)
567                 fprintf(STDOUT, "RW ");
568             if (pntr->type == 1)
569                 fprintf(STDOUT, "RO ");
570             if (pntr->type == 2)
571                 fprintf(STDOUT, "BK ");
572             fprintf(STDOUT, "%10d K ", pntr->size);
573             if (pntr->inUse == 1) {
574                 fprintf(STDOUT, "On-line");
575                 *totalOK += 1;
576             } else {
577                 fprintf(STDOUT, "Off-line");
578                 *totalNotOK += 1;
579             }
580             if (pntr->needsSalvaged == 1)
581                 fprintf(STDOUT, "**needs salvage**");
582             fprintf(STDOUT, "\n");
583         } else if (pntr->status == VBUSY) {
584             *totalBusy += 1;
585             qPut(&busyHead, pntr->volid);
586             if (disp)
587                 fprintf(STDOUT, "**** Volume %lu is busy ****\n",
588                         (unsigned long)pntr->volid);
589         } else {
590             *totalNotOK += 1;
591             qPut(&notokHead, pntr->volid);
592             if (disp)
593                 fprintf(STDOUT, "**** Could not attach volume %lu ****\n",
594                         (unsigned long)pntr->volid);
595         }
596     }
597 }
598
599 /*------------------------------------------------------------------------
600  * PRIVATE XDisplayFormat
601  *
602  * Description:
603  *      Display the contents of one extended volume info structure.
604  *
605  * Arguments:
606  *      a_xInfoP        : Ptr to extended volume info struct to print.
607  *      a_servID        : Server ID to print.
608  *      a_partID        : Partition ID to print.
609  *      a_totalOKP      : Ptr to total-OK counter.
610  *      a_totalNotOKP   : Ptr to total-screwed counter.
611  *      a_totalBusyP    : Ptr to total-busy counter.
612  *      a_fast          : Fast listing?
613  *      a_int32         : Int32 listing?
614  *      a_showProblems  : Show volume problems?
615  *
616  * Returns:
617  *      Nothing.
618  *
619  * Environment:
620  *      Nothing interesting.
621  *
622  * Side Effects:
623  *      As advertised.
624  *------------------------------------------------------------------------*/
625
626 static void
627 XDisplayFormat(volintXInfo *a_xInfoP, afs_uint32 a_servID, afs_int32 a_partID,
628                int *a_totalOKP, int *a_totalNotOKP, int *a_totalBusyP,
629                int a_fast, int a_int32, int a_showProblems)
630 {                               /*XDisplayFormat */
631     time_t t;
632     char pname[10];
633
634     if (a_fast) {
635         /*
636          * Short & sweet.
637          */
638         fprintf(STDOUT, "%-10lu\n", (unsigned long)a_xInfoP->volid);
639     } else if (a_int32) {
640         /*
641          * Fully-detailed listing.
642          */
643         if (a_xInfoP->status == VOK) {
644             /*
645              * Volume's status is OK - all the fields are valid.
646              */
647             fprintf(STDOUT, "%-32s ", a_xInfoP->name);
648             fprintf(STDOUT, "%10lu ", (unsigned long)a_xInfoP->volid);
649             if (a_xInfoP->type == 0)
650                 fprintf(STDOUT, "RW ");
651             if (a_xInfoP->type == 1)
652                 fprintf(STDOUT, "RO ");
653             if (a_xInfoP->type == 2)
654                 fprintf(STDOUT, "BK ");
655             fprintf(STDOUT, "%10d K used ", a_xInfoP->size);
656             fprintf(STDOUT, "%d files ", a_xInfoP->filecount);
657             if (a_xInfoP->inUse == 1) {
658                 fprintf(STDOUT, "On-line");
659                 (*a_totalOKP)++;
660             } else {
661                 fprintf(STDOUT, "Off-line");
662                 (*a_totalNotOKP)++;
663             }
664             fprintf(STDOUT, "\n");
665             MapPartIdIntoName(a_partID, pname);
666             fprintf(STDOUT, "    %s %s \n", hostutil_GetNameByINet(a_servID),
667                     pname);
668             fprintf(STDOUT, "    RWrite %10lu ROnly %10lu Backup %10lu \n",
669                     (unsigned long)a_xInfoP->parentID,
670                     (unsigned long)a_xInfoP->cloneID,
671                     (unsigned long)a_xInfoP->backupID);
672             fprintf(STDOUT, "    MaxQuota %10d K \n", a_xInfoP->maxquota);
673
674             t = a_xInfoP->creationDate;
675             fprintf(STDOUT, "    Creation    %s",
676                     ctime(&t));
677
678             t = a_xInfoP->copyDate;
679             fprintf(STDOUT, "    Copy        %s",
680                     ctime(&t));
681
682             t = a_xInfoP->backupDate;
683             if (!t)
684                 fprintf(STDOUT, "    Backup      Never\n");
685             else
686                 fprintf(STDOUT, "    Backup      %s",
687                         ctime(&t));
688
689             t = a_xInfoP->accessDate;
690             if (t)
691                 fprintf(STDOUT, "    Last Access %s",
692                         ctime(&t));
693
694             t = a_xInfoP->updateDate;
695             if (!t)
696                 fprintf(STDOUT, "    Last Update Never\n");
697             else
698                 fprintf(STDOUT, "    Last Update %s",
699                         ctime(&t));
700             fprintf(STDOUT,
701                     "    %d accesses in the past day (i.e., vnode references)\n",
702                     a_xInfoP->dayUse);
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 /*------------------------------------------------------------------------
833  * PRIVATE XDisplayFormat2
834  *
835  * Description:
836  *      Display the formated contents of one extended volume info structure.
837  *
838  * Arguments:
839  *      a_xInfoP        : Ptr to extended volume info struct to print.
840  *      a_servID        : Server ID to print.
841  *      a_partID        : Partition ID to print.
842  *      a_totalOKP      : Ptr to total-OK counter.
843  *      a_totalNotOKP   : Ptr to total-screwed counter.
844  *      a_totalBusyP    : Ptr to total-busy counter.
845  *      a_fast          : Fast listing?
846  *      a_int32         : Int32 listing?
847  *      a_showProblems  : Show volume problems?
848  *
849  * Returns:
850  *      Nothing.
851  *
852  * Environment:
853  *      Nothing interesting.
854  *
855  * Side Effects:
856  *      As advertised.
857  *------------------------------------------------------------------------*/
858
859 static void
860 XDisplayFormat2(volintXInfo *a_xInfoP, afs_uint32 a_servID, afs_int32 a_partID,
861                 int *a_totalOKP, int *a_totalNotOKP, int *a_totalBusyP,
862                 int a_fast, int a_int32, int a_showProblems)
863 {                               /*XDisplayFormat */
864     time_t t;
865     if (a_fast) {
866         /*
867          * Short & sweet.
868          */
869         fprintf(STDOUT, "vold_id\t%-10lu\n", (unsigned long)a_xInfoP->volid);
870     } else if (a_int32) {
871         /*
872          * Fully-detailed listing.
873          */
874         if (a_xInfoP->status == VOK) {
875             /*
876              * Volume's status is OK - all the fields are valid.
877              */
878
879                 static long server_cache = -1, partition_cache = -1;
880                 static char hostname[256], address[32], pname[16];
881                 int i,ai[] = {VOLINT_STATS_TIME_IDX_0,VOLINT_STATS_TIME_IDX_1,VOLINT_STATS_TIME_IDX_2,
882                               VOLINT_STATS_TIME_IDX_3,VOLINT_STATS_TIME_IDX_4,VOLINT_STATS_TIME_IDX_5};
883
884                 if (a_servID != server_cache) {
885                         struct in_addr s;
886
887                         s.s_addr = a_servID;
888                         strcpy(hostname, hostutil_GetNameByINet(a_servID));
889                         strcpy(address, inet_ntoa(s));
890                         server_cache = a_servID;
891                 }
892                 if (a_partID != partition_cache) {
893                         MapPartIdIntoName(a_partID, pname);
894                         partition_cache = a_partID;
895                 }
896
897                 fprintf(STDOUT, "name\t\t%s\n", a_xInfoP->name);
898                 fprintf(STDOUT, "id\t\t%lu\n", afs_printable_uint32_lu(a_xInfoP->volid));
899                 fprintf(STDOUT, "serv\t\t%s\t%s\n", address, hostname);
900                 fprintf(STDOUT, "part\t\t%s\n", pname);
901                 fprintf(STDOUT, "status\t\tOK\n");
902                 fprintf(STDOUT, "backupID\t%lu\n",
903                         afs_printable_uint32_lu(a_xInfoP->backupID));
904                 fprintf(STDOUT, "parentID\t%lu\n",
905                         afs_printable_uint32_lu(a_xInfoP->parentID));
906                 fprintf(STDOUT, "cloneID\t\t%lu\n",
907                         afs_printable_uint32_lu(a_xInfoP->cloneID));
908                 fprintf(STDOUT, "inUse\t\t%s\n", a_xInfoP->inUse ? "Y" : "N");
909                 switch (a_xInfoP->type) {
910                 case 0:
911                         fprintf(STDOUT, "type\t\tRW\n");
912                         break;
913                 case 1:
914                         fprintf(STDOUT, "type\t\tRO\n");
915                         break;
916                 case 2:
917                         fprintf(STDOUT, "type\t\tBK\n");
918                         break;
919                 default:
920                         fprintf(STDOUT, "type\t\t?\n");
921                         break;
922                 }
923                 t = a_xInfoP->creationDate;
924                 fprintf(STDOUT, "creationDate\t%-9lu\t%s",
925                         afs_printable_uint32_lu(a_xInfoP->creationDate),
926                         ctime(&t));
927
928                 t = a_xInfoP->accessDate;
929                 fprintf(STDOUT, "accessDate\t%-9lu\t%s",
930                         afs_printable_uint32_lu(a_xInfoP->accessDate),
931                         ctime(&t));
932
933                 t = a_xInfoP->updateDate;
934                 fprintf(STDOUT, "updateDate\t%-9lu\t%s",
935                         afs_printable_uint32_lu(a_xInfoP->updateDate),
936                         ctime(&t));
937
938                 t = a_xInfoP->backupDate;
939                 fprintf(STDOUT, "backupDate\t%-9lu\t%s",
940                         afs_printable_uint32_lu(a_xInfoP->backupDate),
941                         ctime(&t));
942
943                 t = a_xInfoP->copyDate;
944                 fprintf(STDOUT, "copyDate\t%-9lu\t%s",
945                         afs_printable_uint32_lu(a_xInfoP->copyDate),
946                         ctime(&t));
947
948                 fprintf(STDOUT, "diskused\t%u\n", a_xInfoP->size);
949                 fprintf(STDOUT, "maxquota\t%u\n", a_xInfoP->maxquota);
950
951                 fprintf(STDOUT, "filecount\t%u\n", a_xInfoP->filecount);
952                 fprintf(STDOUT, "dayUse\t\t%u\n", a_xInfoP->dayUse);
953
954
955
956                 fprintf(STDOUT,"reads_same_net\t%8d\n",a_xInfoP->stat_reads[VOLINT_STATS_SAME_NET]);
957                 fprintf(STDOUT,"reads_same_net_auth\t%8d\n",a_xInfoP->stat_reads[VOLINT_STATS_SAME_NET_AUTH]);
958                 fprintf(STDOUT,"reads_diff_net\t%8d\n",a_xInfoP->stat_reads[VOLINT_STATS_DIFF_NET]);
959                 fprintf(STDOUT,"reads_diff_net_auth\t%8d\n",a_xInfoP->stat_reads[VOLINT_STATS_DIFF_NET_AUTH]);
960
961                 fprintf(STDOUT,"writes_same_net\t%8d\n",a_xInfoP->stat_writes[VOLINT_STATS_SAME_NET]);
962                 fprintf(STDOUT,"writes_same_net_auth\t%8d\n",a_xInfoP->stat_writes[VOLINT_STATS_SAME_NET_AUTH]);
963                 fprintf(STDOUT,"writes_diff_net\t%8d\n",a_xInfoP->stat_writes[VOLINT_STATS_DIFF_NET]);
964                 fprintf(STDOUT,"writes_diff_net_auth\t%8d\n",a_xInfoP->stat_writes[VOLINT_STATS_DIFF_NET_AUTH]);
965
966                 for(i=0;i<5;i++)
967                 {
968                         fprintf(STDOUT,"file_same_author_idx_%d\t%8d\n",i+1,a_xInfoP->stat_fileSameAuthor[ai[i]]);
969                         fprintf(STDOUT,"file_diff_author_idx_%d\t%8d\n",i+1,a_xInfoP->stat_fileDiffAuthor[ai[i]]);
970                         fprintf(STDOUT,"dir_same_author_idx_%d\t%8d\n",i+1,a_xInfoP->stat_dirSameAuthor[ai[i]]);
971                         fprintf(STDOUT,"dir_dif_author_idx_%d\t%8d\n",i+1,a_xInfoP->stat_dirDiffAuthor[ai[i]]);
972                 }
973
974         } /*Volume status OK */
975         else if (a_xInfoP->status == VBUSY) {
976             (*a_totalBusyP)++;
977             qPut(&busyHead, a_xInfoP->volid);
978             if (a_showProblems)
979                 fprintf(STDOUT, "BUSY_VOL\t%lu\n",
980                         (unsigned long)a_xInfoP->volid);
981         } /*Busy volume */
982         else {
983             (*a_totalNotOKP)++;
984             qPut(&notokHead, a_xInfoP->volid);
985             if (a_showProblems)
986                 fprintf(STDOUT, "COULD_NOT_ATTACH\t%lu\n",
987                         (unsigned long)a_xInfoP->volid);
988         }                       /*Screwed volume */
989     } /*Long listing */
990     else {
991         /*
992          * Default listing.
993          */
994         if (a_xInfoP->status == VOK) {
995             fprintf(STDOUT, "name\t%-32s\n", a_xInfoP->name);
996             fprintf(STDOUT, "volID\t%10lu\n", (unsigned long)a_xInfoP->volid);
997             if (a_xInfoP->type == 0)
998                 fprintf(STDOUT, "type\tRW\n");
999             if (a_xInfoP->type == 1)
1000                 fprintf(STDOUT, "type\tRO\n");
1001             if (a_xInfoP->type == 2)
1002                 fprintf(STDOUT, "type\tBK\n");
1003             fprintf(STDOUT, "size\t%10dK\n", a_xInfoP->size);
1004
1005             fprintf(STDOUT, "inUse\t%d\n",a_xInfoP->inUse);
1006             if (a_xInfoP->inUse == 1)
1007                 (*a_totalOKP)++;
1008             else
1009                 (*a_totalNotOKP)++;
1010
1011         } /*Volume OK */
1012         else if (a_xInfoP->status == VBUSY) {
1013             (*a_totalBusyP)++;
1014             qPut(&busyHead, a_xInfoP->volid);
1015             if (a_showProblems)
1016                 fprintf(STDOUT, "VOLUME_BUSY\t%lu\n",
1017                         (unsigned long)a_xInfoP->volid);
1018         } /*Busy volume */
1019         else {
1020             (*a_totalNotOKP)++;
1021             qPut(&notokHead, a_xInfoP->volid);
1022             if (a_showProblems)
1023                 fprintf(STDOUT, "COULD_NOT_ATTACH_VOLUME\t%lu\n",
1024                         (unsigned long)a_xInfoP->volid);
1025         }                       /*Screwed volume */
1026     }                           /*Default listing */
1027 }                               /*XDisplayFormat */
1028
1029 static void
1030 DisplayFormat2(long server, long partition, volintInfo *pntr)
1031 {
1032     static long server_cache = -1, partition_cache = -1;
1033     static char hostname[256], address[32], pname[16];
1034     time_t t;
1035
1036     if (server != server_cache) {
1037         struct in_addr s;
1038
1039         s.s_addr = server;
1040         strcpy(hostname, hostutil_GetNameByINet(server));
1041         strcpy(address, inet_ntoa(s));
1042         server_cache = server;
1043     }
1044     if (partition != partition_cache) {
1045         MapPartIdIntoName(partition, pname);
1046         partition_cache = partition;
1047     }
1048
1049     if (pntr->status == VOK)
1050         fprintf(STDOUT, "name\t\t%s\n", pntr->name);
1051
1052     fprintf(STDOUT, "id\t\t%lu\n",
1053             afs_printable_uint32_lu(pntr->volid));
1054     fprintf(STDOUT, "serv\t\t%s\t%s\n", address, hostname);
1055     fprintf(STDOUT, "part\t\t%s\n", pname);
1056     switch (pntr->status) {
1057     case VOK:
1058         fprintf(STDOUT, "status\t\tOK\n");
1059         break;
1060     case VBUSY:
1061         fprintf(STDOUT, "status\t\tBUSY\n");
1062         return;
1063     default:
1064         fprintf(STDOUT, "status\t\tUNATTACHABLE\n");
1065         return;
1066     }
1067     fprintf(STDOUT, "backupID\t%lu\n",
1068             afs_printable_uint32_lu(pntr->backupID));
1069     fprintf(STDOUT, "parentID\t%lu\n",
1070             afs_printable_uint32_lu(pntr->parentID));
1071     fprintf(STDOUT, "cloneID\t\t%lu\n",
1072             afs_printable_uint32_lu(pntr->cloneID));
1073     fprintf(STDOUT, "inUse\t\t%s\n", pntr->inUse ? "Y" : "N");
1074     fprintf(STDOUT, "needsSalvaged\t%s\n", pntr->needsSalvaged ? "Y" : "N");
1075     /* 0xD3 is from afs/volume.h since I had trouble including the file */
1076     fprintf(STDOUT, "destroyMe\t%s\n", pntr->destroyMe == 0xD3 ? "Y" : "N");
1077     switch (pntr->type) {
1078     case 0:
1079         fprintf(STDOUT, "type\t\tRW\n");
1080         break;
1081     case 1:
1082         fprintf(STDOUT, "type\t\tRO\n");
1083         break;
1084     case 2:
1085         fprintf(STDOUT, "type\t\tBK\n");
1086         break;
1087     default:
1088         fprintf(STDOUT, "type\t\t?\n");
1089         break;
1090     }
1091     t = pntr->creationDate;
1092     fprintf(STDOUT, "creationDate\t%-9lu\t%s",
1093             afs_printable_uint32_lu(pntr->creationDate),
1094             ctime(&t));
1095
1096     t = pntr->accessDate;
1097     fprintf(STDOUT, "accessDate\t%-9lu\t%s",
1098             afs_printable_uint32_lu(pntr->accessDate),
1099             ctime(&t));
1100
1101     t = pntr->updateDate;
1102     fprintf(STDOUT, "updateDate\t%-9lu\t%s",
1103             afs_printable_uint32_lu(pntr->updateDate),
1104             ctime(&t));
1105
1106     t = pntr->backupDate;
1107     fprintf(STDOUT, "backupDate\t%-9lu\t%s",
1108             afs_printable_uint32_lu(pntr->backupDate),
1109             ctime(&t));
1110
1111     t = pntr->copyDate;
1112     fprintf(STDOUT, "copyDate\t%-9lu\t%s",
1113             afs_printable_uint32_lu(pntr->copyDate),
1114             ctime(&t));
1115
1116     fprintf(STDOUT, "flags\t\t%#lx\t(Optional)\n",
1117             afs_printable_uint32_lu(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_printable_uint32_lu(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_printable_uint32_lu(pntr->spare1));
1126     fprintf(STDOUT, "spare2\t\t%lu\t(Optional)\n",
1127             afs_printable_uint32_lu(pntr->spare2));
1128     fprintf(STDOUT, "spare3\t\t%lu\t(Optional)\n",
1129             afs_printable_uint32_lu(pntr->spare3));
1130     return;
1131 }
1132
1133 static void
1134 DisplayVolumes2(long server, long partition, volintInfo *pntr, long count)
1135 {
1136     long i;
1137
1138     for (i = 0; i < count; i++) {
1139         fprintf(STDOUT, "BEGIN_OF_ENTRY\n");
1140         DisplayFormat2(server, partition, pntr);
1141         fprintf(STDOUT, "END_OF_ENTRY\n\n");
1142         pntr++;
1143     }
1144     return;
1145 }
1146
1147 static void
1148 DisplayVolumes(afs_uint32 server, afs_int32 part, volintInfo *pntr,
1149                afs_int32 count, afs_int32 longlist, afs_int32 fast,
1150                int quiet)
1151 {
1152     int totalOK, totalNotOK, totalBusy, i;
1153     afs_uint32 volid = 0;
1154
1155     totalOK = 0;
1156     totalNotOK = 0;
1157     totalBusy = 0;
1158     qInit(&busyHead);
1159     qInit(&notokHead);
1160     for (i = 0; i < count; i++) {
1161         DisplayFormat(pntr, server, part, &totalOK, &totalNotOK, &totalBusy,
1162                       fast, longlist, 0);
1163         pntr++;
1164     }
1165     if (totalBusy) {
1166         while (busyHead.count) {
1167             qGet(&busyHead, &volid);
1168             fprintf(STDOUT, "**** Volume %lu is busy ****\n",
1169                     (unsigned long)volid);
1170         }
1171     }
1172     if (totalNotOK) {
1173         while (notokHead.count) {
1174             qGet(&notokHead, &volid);
1175             fprintf(STDOUT, "**** Could not attach volume %lu ****\n",
1176                     (unsigned long)volid);
1177         }
1178     }
1179     if (!quiet) {
1180         fprintf(STDOUT, "\n");
1181         if (!fast) {
1182             fprintf(STDOUT,
1183                     "Total volumes onLine %d ; Total volumes offLine %d ; Total busy %d\n\n",
1184                     totalOK, totalNotOK, totalBusy);
1185         }
1186     }
1187 }
1188 /*------------------------------------------------------------------------
1189  * PRIVATE XDisplayVolumes
1190  *
1191  * Description:
1192  *      Display extended volume information.
1193  *
1194  * Arguments:
1195  *      a_servID : Pointer to the Rx call we're performing.
1196  *      a_partID : Partition for which we want the extended list.
1197  *      a_xInfoP : Ptr to extended volume info.
1198  *      a_count  : Number of volume records contained above.
1199  *      a_int32   : Int32 listing generated?
1200  *      a_fast   : Fast listing generated?
1201  *      a_quiet  : Quiet listing generated?
1202  *
1203  * Returns:
1204  *      Nothing.
1205  *
1206  * Environment:
1207  *      Nothing interesting.
1208  *
1209  * Side Effects:
1210  *      As advertised.
1211  *------------------------------------------------------------------------*/
1212
1213 static void
1214 XDisplayVolumes(afs_uint32 a_servID, afs_int32 a_partID, volintXInfo *a_xInfoP,
1215                 afs_int32 a_count, afs_int32 a_int32, afs_int32 a_fast,
1216                 int a_quiet)
1217 {                               /*XDisplayVolumes */
1218
1219     int totalOK;                /*Total OK volumes */
1220     int totalNotOK;             /*Total screwed volumes */
1221     int totalBusy;              /*Total busy volumes */
1222     int i;                      /*Loop variable */
1223     afs_uint32 volid = 0;       /*Current volume ID */
1224
1225     /*
1226      * Initialize counters and (global!!) queues.
1227      */
1228     totalOK = 0;
1229     totalNotOK = 0;
1230     totalBusy = 0;
1231     qInit(&busyHead);
1232     qInit(&notokHead);
1233
1234     /*
1235      * Display each volume in the list.
1236      */
1237     for (i = 0; i < a_count; i++) {
1238         XDisplayFormat(a_xInfoP, a_servID, a_partID, &totalOK, &totalNotOK,
1239                        &totalBusy, a_fast, a_int32, 0);
1240         a_xInfoP++;
1241     }
1242
1243     /*
1244      * If any volumes were found to be busy or screwed, display them.
1245      */
1246     if (totalBusy) {
1247         while (busyHead.count) {
1248             qGet(&busyHead, &volid);
1249             fprintf(STDOUT, "**** Volume %lu is busy ****\n",
1250                     (unsigned long)volid);
1251         }
1252     }
1253     if (totalNotOK) {
1254         while (notokHead.count) {
1255             qGet(&notokHead, &volid);
1256             fprintf(STDOUT, "**** Could not attach volume %lu ****\n",
1257                     (unsigned long)volid);
1258         }
1259     }
1260
1261     if (!a_quiet) {
1262         fprintf(STDOUT, "\n");
1263         if (!a_fast) {
1264             fprintf(STDOUT,
1265                     "Total volumes: %d on-line, %d off-line, %d  busyd\n\n",
1266                     totalOK, totalNotOK, totalBusy);
1267         }
1268     }
1269
1270 }                               /*XDisplayVolumes */
1271
1272 /*------------------------------------------------------------------------
1273  * PRIVATE XDisplayVolumes2
1274  *
1275  * Description:
1276  *      Display extended formated volume information.
1277  *
1278  * Arguments:
1279  *      a_servID : Pointer to the Rx call we're performing.
1280  *      a_partID : Partition for which we want the extended list.
1281  *      a_xInfoP : Ptr to extended volume info.
1282  *      a_count  : Number of volume records contained above.
1283  *      a_int32   : Int32 listing generated?
1284  *      a_fast   : Fast listing generated?
1285  *      a_quiet  : Quiet listing generated?
1286  *
1287  * Returns:
1288  *      Nothing.
1289  *
1290  * Environment:
1291  *      Nothing interesting.
1292  *
1293  * Side Effects:
1294  *      As advertised.
1295  *------------------------------------------------------------------------*/
1296
1297 static void
1298 XDisplayVolumes2(afs_uint32 a_servID, afs_int32 a_partID, volintXInfo *a_xInfoP,
1299                  afs_int32 a_count, afs_int32 a_int32, afs_int32 a_fast,
1300                  int a_quiet)
1301 {                               /*XDisplayVolumes */
1302
1303     int totalOK;                /*Total OK volumes */
1304     int totalNotOK;             /*Total screwed volumes */
1305     int totalBusy;              /*Total busy volumes */
1306     int i;                      /*Loop variable */
1307     afs_uint32 volid = 0;       /*Current volume ID */
1308
1309     /*
1310      * Initialize counters and (global!!) queues.
1311      */
1312     totalOK = 0;
1313     totalNotOK = 0;
1314     totalBusy = 0;
1315     qInit(&busyHead);
1316     qInit(&notokHead);
1317
1318     /*
1319      * Display each volume in the list.
1320      */
1321     for (i = 0; i < a_count; i++) {
1322         fprintf(STDOUT, "BEGIN_OF_ENTRY\n");
1323         XDisplayFormat2(a_xInfoP, a_servID, a_partID, &totalOK, &totalNotOK,
1324                        &totalBusy, a_fast, a_int32, 0);
1325         fprintf(STDOUT, "END_OF_ENTRY\n");
1326         a_xInfoP++;
1327     }
1328
1329     /*
1330      * If any volumes were found to be busy or screwed, display them.
1331      */
1332     if (totalBusy) {
1333         while (busyHead.count) {
1334             qGet(&busyHead, &volid);
1335             fprintf(STDOUT, "BUSY_VOL\t%lu\n",
1336                     (unsigned long)volid);
1337         }
1338     }
1339     if (totalNotOK) {
1340         while (notokHead.count) {
1341             qGet(&notokHead, &volid);
1342             fprintf(STDOUT, "COULD_NOT_ATTACH\t%lu\n",
1343                     (unsigned long)volid);
1344         }
1345     }
1346
1347     if (!a_quiet) {
1348         fprintf(STDOUT, "\n");
1349         if (!a_fast) {
1350             fprintf(STDOUT,
1351                     "VOLUMES_ONLINE\t%d\nVOLUMES_OFFLINE\t%d\nVOLUMES_BUSY\t%d\n",
1352                     totalOK, totalNotOK, totalBusy);
1353         }
1354     }
1355
1356 }                               /*XDisplayVolumes2 */
1357
1358
1359 /* set <server> and <part> to the correct values depending on
1360  * <voltype> and <entry> */
1361 static void
1362 GetServerAndPart(struct nvldbentry *entry, int voltype, afs_uint32 *server,
1363                  afs_int32 *part, int *previdx)
1364 {
1365     int i, istart, vtype;
1366
1367     *server = -1;
1368     *part = -1;
1369
1370     /* Doesn't check for non-existance of backup volume */
1371     if ((voltype == RWVOL) || (voltype == BACKVOL)) {
1372         vtype = ITSRWVOL;
1373         istart = 0;             /* seach the entire entry */
1374     } else {
1375         vtype = ITSROVOL;
1376         /* Seach from beginning of entry or pick up where we left off */
1377         istart = ((*previdx < 0) ? 0 : *previdx + 1);
1378     }
1379
1380     for (i = istart; i < entry->nServers; i++) {
1381         if (entry->serverFlags[i] & vtype) {
1382             *server = entry->serverNumber[i];
1383             *part = entry->serverPartition[i];
1384             *previdx = i;
1385             return;
1386         }
1387     }
1388
1389     /* Didn't find any, return -1 */
1390     *previdx = -1;
1391     return;
1392 }
1393
1394 static void
1395 PrintLocked(afs_int32 aflags)
1396 {
1397     afs_int32 flags = aflags & VLOP_ALLOPERS;
1398
1399     if (flags) {
1400         fprintf(STDOUT, "    Volume is currently LOCKED  \n");
1401
1402         if (flags & VLOP_MOVE) {
1403             fprintf(STDOUT, "    Volume is locked for a move operation\n");
1404         }
1405         if (flags & VLOP_RELEASE) {
1406             fprintf(STDOUT, "    Volume is locked for a release operation\n");
1407         }
1408         if (flags & VLOP_BACKUP) {
1409             fprintf(STDOUT, "    Volume is locked for a backup operation\n");
1410         }
1411         if (flags & VLOP_DELETE) {
1412             fprintf(STDOUT, "    Volume is locked for a delete/misc operation\n");
1413         }
1414         if (flags & VLOP_DUMP) {
1415             fprintf(STDOUT, "    Volume is locked for a dump/restore operation\n");
1416         }
1417     }
1418 }
1419
1420 static void
1421 PostVolumeStats(struct nvldbentry *entry)
1422 {
1423     SubEnumerateEntry(entry);
1424     /* Check for VLOP_ALLOPERS */
1425     PrintLocked(entry->flags);
1426     return;
1427 }
1428
1429 /*------------------------------------------------------------------------
1430  * PRIVATE XVolumeStats
1431  *
1432  * Description:
1433  *      Display extended volume information.
1434  *
1435  * Arguments:
1436  *      a_xInfoP  : Ptr to extended volume info.
1437  *      a_entryP  : Ptr to the volume's VLDB entry.
1438  *      a_srvID   : Server ID.
1439  *      a_partID  : Partition ID.
1440  *      a_volType : Type of volume to print.
1441  *
1442  * Returns:
1443  *      Nothing.
1444  *
1445  * Environment:
1446  *      Nothing interesting.
1447  *
1448  * Side Effects:
1449  *      As advertised.
1450  *------------------------------------------------------------------------*/
1451
1452 static void
1453 XVolumeStats(volintXInfo *a_xInfoP, struct nvldbentry *a_entryP,
1454              afs_int32 a_srvID, afs_int32 a_partID, int a_volType)
1455 {                               /*XVolumeStats */
1456
1457     int totalOK, totalNotOK, totalBusy; /*Dummies - we don't really count here */
1458
1459     XDisplayFormat(a_xInfoP,    /*Ptr to extended volume info */
1460                    a_srvID,     /*Server ID to print */
1461                    a_partID,    /*Partition ID to print */
1462                    &totalOK,    /*Ptr to total-OK counter */
1463                    &totalNotOK, /*Ptr to total-screwed counter */
1464                    &totalBusy,  /*Ptr to total-busy counter */
1465                    0,           /*Don't do a fast listing */
1466                    1,           /*Do a long listing */
1467                    1);          /*Show volume problems */
1468     return;
1469
1470 }                               /*XVolumeStats */
1471
1472 static void
1473 VolumeStats_int(volintInfo *pntr, struct nvldbentry *entry, afs_uint32 server,
1474              afs_int32 part, int voltype)
1475 {
1476     int totalOK = 0;
1477     int totalNotOK = 0;
1478     int totalBusy = 0;
1479
1480     DisplayFormat(pntr, server, part, &totalOK, &totalNotOK, &totalBusy, 0, 1,
1481                   1);
1482     return;
1483 }
1484
1485 /* command to forcibly remove a volume */
1486 static int
1487 NukeVolume(struct cmd_syndesc *as)
1488 {
1489     afs_int32 code;
1490     afs_uint32 volID;
1491     afs_int32  err;
1492     afs_int32 partID;
1493     afs_uint32 server;
1494     char *tp;
1495
1496     server = GetServer(tp = as->parms[0].items->data);
1497     if (!server) {
1498         fprintf(STDERR, "vos: server '%s' not found in host table\n", tp);
1499         return 1;
1500     }
1501
1502     partID = volutil_GetPartitionID(tp = as->parms[1].items->data);
1503     if (partID == -1) {
1504         fprintf(STDERR, "vos: could not parse '%s' as a partition name", tp);
1505         return 1;
1506     }
1507
1508     volID = vsu_GetVolumeID(tp = as->parms[2].items->data, cstruct, &err);
1509     if (volID == 0) {
1510         if (err)
1511             PrintError("", err);
1512         else
1513             fprintf(STDERR,
1514                     "vos: could not parse '%s' as a numeric volume ID", tp);
1515         return 1;
1516     }
1517
1518     fprintf(STDOUT,
1519             "vos: forcibly removing all traces of volume %d, please wait...",
1520             volID);
1521     fflush(STDOUT);
1522     code = UV_NukeVolume(server, partID, volID);
1523     if (code == 0)
1524         fprintf(STDOUT, "done.\n");
1525     else
1526         fprintf(STDOUT, "failed with code %d.\n", code);
1527     return code;
1528 }
1529
1530
1531 /*------------------------------------------------------------------------
1532  * PRIVATE ExamineVolume
1533  *
1534  * Description:
1535  *      Routine used to examine a single volume, contacting the VLDB as
1536  *      well as the Volume Server.
1537  *
1538  * Arguments:
1539  *      as : Ptr to parsed command line arguments.
1540  *
1541  * Returns:
1542  *      0 for a successful operation,
1543  *      Otherwise, one of the ubik or VolServer error values.
1544  *
1545  * Environment:
1546  *      Nothing interesting.
1547  *
1548  * Side Effects:
1549  *      As advertised.
1550  *------------------------------------------------------------------------
1551  */
1552 static int
1553 ExamineVolume(struct cmd_syndesc *as, void *arock)
1554 {
1555     struct nvldbentry entry;
1556     afs_int32 vcode = 0;
1557     volintInfo *pntr = (volintInfo *) 0;
1558     volintXInfo *xInfoP = (volintXInfo *) 0;
1559     afs_uint32 volid;
1560     afs_int32 code, err, error = 0;
1561     int voltype, foundserv = 0, foundentry = 0;
1562     afs_uint32 aserver;
1563     afs_int32 apart;
1564     int previdx = -1;
1565     int wantExtendedInfo;       /*Do we want extended vol info? */
1566     int isSubEnum=0;            /* Keep track whether sub enumerate called. */
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 if (as->parms[2].items) {
1654                 DisplayFormat2(aserver, apart, pntr);
1655                 EnumerateEntry(&entry);
1656                 isSubEnum = 1;
1657             } else
1658                 VolumeStats_int(pntr, &entry, aserver, apart, voltype);
1659
1660             if ((voltype == BACKVOL) && !(entry.flags & BACK_EXISTS)) {
1661                 /* The VLDB says there is no backup volume yet we found one on disk */
1662                 fprintf(STDERR, "Volume %s does not exist in VLDB\n",
1663                         as->parms[0].items->data);
1664                 error = ENOENT;
1665             }
1666         }
1667
1668         if (pntr)
1669             free(pntr);
1670         if (xInfoP)
1671             free(xInfoP);
1672     } while (voltype == ROVOL);
1673
1674     if (!foundserv) {
1675         fprintf(STDERR, "Dump only information from VLDB\n\n");
1676         fprintf(STDOUT, "%s \n", entry.name);   /* PostVolumeStats doesn't print name */
1677     }
1678
1679     if (!isSubEnum)
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(struct cmd_syndesc *as, void *arock)
1707 {
1708     struct nvldbentry entry;
1709     volintInfo info;
1710     afs_uint32 volid;
1711     afs_int32 code, err;
1712     afs_uint32 aserver;
1713     afs_int32 apart;
1714     int previdx = -1;
1715     int have_field = 0;
1716
1717     volid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);   /* -id */
1718     if (volid == 0) {
1719         if (err)
1720             PrintError("", err);
1721         else
1722             fprintf(STDERR, "Unknown volume ID or name '%s'\n",
1723                     as->parms[0].items->data);
1724         return -1;
1725     }
1726
1727     code = VLDB_GetEntryByID(volid, RWVOL, &entry);
1728     if (code) {
1729         fprintf(STDERR,
1730                 "Could not fetch the entry for volume number %lu from VLDB \n",
1731                 (unsigned long)volid);
1732         return (code);
1733     }
1734     MapHostToNetwork(&entry);
1735
1736     GetServerAndPart(&entry, RWVOL, &aserver, &apart, &previdx);
1737     if (previdx == -1) {
1738         fprintf(STDERR, "Volume %s does not exist in VLDB\n\n",
1739                 as->parms[0].items->data);
1740         return (ENOENT);
1741     }
1742
1743     init_volintInfo(&info);
1744     info.volid = volid;
1745     info.type = RWVOL;
1746
1747     if (as->parms[1].items) {
1748         /* -max <quota> */
1749         have_field = 1;
1750         code = util_GetHumanInt32(as->parms[1].items->data, &info.maxquota);
1751         if (code) {
1752             fprintf(STDERR, "invalid quota value\n");
1753             return code;
1754         }
1755     }
1756     if (as->parms[2].items) {
1757         /* -clearuse */
1758         have_field = 1;
1759         info.dayUse = 0;
1760     }
1761     if (as->parms[3].items) {
1762         /* -clearVolUpCounter */
1763         have_field = 1;
1764         info.spare2 = 0;
1765     }
1766     if (!have_field) {
1767         fprintf(STDERR,"Nothing to set.\n");
1768         return (1);
1769     }
1770     code = UV_SetVolumeInfo(aserver, apart, volid, &info);
1771     if (code)
1772         fprintf(STDERR,
1773                 "Could not update volume info fields for volume number %lu\n",
1774                 (unsigned long)volid);
1775     return (code);
1776 }
1777
1778 /*------------------------------------------------------------------------
1779  * PRIVATE volOnline
1780  *
1781  * Description:
1782  *      Brings a volume online.
1783  *
1784  * Arguments:
1785  *      as : Ptr to parsed command line arguments.
1786  *
1787  * Returns:
1788  *      0 for a successful operation,
1789  *
1790  * Environment:
1791  *      Nothing interesting.
1792  *
1793  * Side Effects:
1794  *      As advertised.
1795  *------------------------------------------------------------------------
1796  */
1797 static int
1798 volOnline(struct cmd_syndesc *as, void *arock)
1799 {
1800     afs_uint32 server;
1801     afs_int32 partition;
1802     afs_uint32 volid;
1803     afs_int32 code, err = 0;
1804
1805     server = GetServer(as->parms[0].items->data);
1806     if (server == 0) {
1807         fprintf(STDERR, "vos: server '%s' not found in host table\n",
1808                 as->parms[0].items->data);
1809         return -1;
1810     }
1811
1812     partition = volutil_GetPartitionID(as->parms[1].items->data);
1813     if (partition < 0) {
1814         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
1815                 as->parms[1].items->data);
1816         return ENOENT;
1817     }
1818
1819     volid = vsu_GetVolumeID(as->parms[2].items->data, cstruct, &err);   /* -id */
1820     if (!volid) {
1821         if (err)
1822             PrintError("", err);
1823         else
1824             fprintf(STDERR, "Unknown volume ID or name '%s'\n",
1825                     as->parms[0].items->data);
1826         return -1;
1827     }
1828
1829     code = UV_SetVolume(server, partition, volid, ITOffline, 0 /*online */ ,
1830                         0 /*sleep */ );
1831     if (code) {
1832         fprintf(STDERR, "Failed to set volume. Code = %d\n", code);
1833         return -1;
1834     }
1835
1836     return 0;
1837 }
1838
1839 /*------------------------------------------------------------------------
1840  * PRIVATE volOffline
1841  *
1842  * Description:
1843  *      Brings a volume offline.
1844  *
1845  * Arguments:
1846  *      as : Ptr to parsed command line arguments.
1847  *
1848  * Returns:
1849  *      0 for a successful operation,
1850  *
1851  * Environment:
1852  *      Nothing interesting.
1853  *
1854  * Side Effects:
1855  *      As advertised.
1856  *------------------------------------------------------------------------
1857  */
1858 static int
1859 volOffline(struct cmd_syndesc *as, void *arock)
1860 {
1861     afs_uint32 server;
1862     afs_int32 partition;
1863     afs_uint32 volid;
1864     afs_int32 code, err = 0;
1865     afs_int32 transflag, sleeptime, transdone;
1866
1867     server = GetServer(as->parms[0].items->data);
1868     if (server == 0) {
1869         fprintf(STDERR, "vos: server '%s' not found in host table\n",
1870                 as->parms[0].items->data);
1871         return -1;
1872     }
1873
1874     partition = volutil_GetPartitionID(as->parms[1].items->data);
1875     if (partition < 0) {
1876         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
1877                 as->parms[1].items->data);
1878         return ENOENT;
1879     }
1880
1881     volid = vsu_GetVolumeID(as->parms[2].items->data, cstruct, &err);   /* -id */
1882     if (!volid) {
1883         if (err)
1884             PrintError("", err);
1885         else
1886             fprintf(STDERR, "Unknown volume ID or name '%s'\n",
1887                     as->parms[0].items->data);
1888         return -1;
1889     }
1890
1891     transflag = (as->parms[4].items ? ITBusy : ITOffline);
1892     sleeptime = (as->parms[3].items ? atol(as->parms[3].items->data) : 0);
1893     transdone = ((sleeptime || as->parms[4].items) ? 0 /*online */ : VTOutOfService);
1894     if (as->parms[4].items && !as->parms[3].items) {
1895         fprintf(STDERR, "-sleep option must be used with -busy flag\n");
1896         return -1;
1897     }
1898
1899     code =
1900         UV_SetVolume(server, partition, volid, transflag, transdone,
1901                      sleeptime);
1902     if (code) {
1903         fprintf(STDERR, "Failed to set volume. Code = %d\n", code);
1904         return -1;
1905     }
1906
1907     return 0;
1908 }
1909
1910 static int
1911 CreateVolume(struct cmd_syndesc *as, void *arock)
1912 {
1913     afs_int32 pnum;
1914     char part[10];
1915     afs_uint32 volid = 0, rovolid = 0, bkvolid = 0;
1916     afs_uint32 *arovolid;
1917     afs_int32 code;
1918     struct nvldbentry entry;
1919     afs_int32 vcode;
1920     afs_int32 quota;
1921     afs_uint32 tserver;
1922
1923     arovolid = &rovolid;
1924
1925     quota = 5000;
1926     tserver = GetServer(as->parms[0].items->data);
1927     if (!tserver) {
1928         fprintf(STDERR, "vos: host '%s' not found in host table\n",
1929                 as->parms[0].items->data);
1930         return ENOENT;
1931     }
1932     pnum = volutil_GetPartitionID(as->parms[1].items->data);
1933     if (pnum < 0) {
1934         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
1935                 as->parms[1].items->data);
1936         return ENOENT;
1937     }
1938     if (!IsPartValid(pnum, tserver, &code)) {   /*check for validity of the partition */
1939         if (code)
1940             PrintError("", code);
1941         else
1942             fprintf(STDERR,
1943                     "vos : partition %s does not exist on the server\n",
1944                     as->parms[1].items->data);
1945         return ENOENT;
1946     }
1947     if (!ISNAMEVALID(as->parms[2].items->data)) {
1948         fprintf(STDERR,
1949                 "vos: the name of the root volume %s exceeds the size limit of %d\n",
1950                 as->parms[2].items->data, VOLSER_OLDMAXVOLNAME - 10);
1951         return E2BIG;
1952     }
1953     if (!VolNameOK(as->parms[2].items->data)) {
1954         fprintf(STDERR,
1955                 "Illegal volume name %s, should not end in .readonly or .backup\n",
1956                 as->parms[2].items->data);
1957         return EINVAL;
1958     }
1959     if (IsNumeric(as->parms[2].items->data)) {
1960         fprintf(STDERR, "Illegal volume name %s, should not be a number\n",
1961                 as->parms[2].items->data);
1962         return EINVAL;
1963     }
1964     vcode = VLDB_GetEntryByName(as->parms[2].items->data, &entry);
1965     if (!vcode) {
1966         fprintf(STDERR, "Volume %s already exists\n",
1967                 as->parms[2].items->data);
1968         PrintDiagnostics("create", code);
1969         return EEXIST;
1970     }
1971
1972     if (as->parms[3].items) {
1973         code = util_GetHumanInt32(as->parms[3].items->data, &quota);
1974         if (code) {
1975             fprintf(STDERR, "vos: bad integer specified for quota.\n");
1976             return code;
1977         }
1978     }
1979
1980     if (as->parms[4].items) {
1981         if (!IsNumeric(as->parms[4].items->data)) {
1982             fprintf(STDERR, "vos: Given volume ID %s should be numeric.\n",
1983                     as->parms[4].items->data);
1984             return EINVAL;
1985         }
1986
1987         code = util_GetUInt32(as->parms[4].items->data, &volid);
1988         if (code) {
1989             fprintf(STDERR, "vos: bad integer specified for volume ID.\n");
1990             return code;
1991         }
1992     }
1993
1994     if (as->parms[5].items) {
1995         if (!IsNumeric(as->parms[5].items->data)) {
1996             fprintf(STDERR, "vos: Given RO volume ID %s should be numeric.\n",
1997                     as->parms[5].items->data);
1998             return EINVAL;
1999         }
2000
2001         code = util_GetUInt32(as->parms[5].items->data, &rovolid);
2002         if (code) {
2003             fprintf(STDERR, "vos: bad integer specified for volume ID.\n");
2004             return code;
2005         }
2006
2007         if (rovolid == 0) {
2008             arovolid = NULL;
2009         }
2010     }
2011
2012     code =
2013         UV_CreateVolume3(tserver, pnum, as->parms[2].items->data, quota, 0,
2014                          0, 0, 0, &volid, arovolid, &bkvolid);
2015     if (code) {
2016         PrintDiagnostics("create", code);
2017         return code;
2018     }
2019     MapPartIdIntoName(pnum, part);
2020     fprintf(STDOUT, "Volume %lu created on partition %s of %s\n",
2021             (unsigned long)volid, part, as->parms[0].items->data);
2022
2023     return 0;
2024 }
2025
2026 #if 0
2027 static afs_int32
2028 DeleteAll(struct nvldbentry *entry)
2029 {
2030     int i;
2031     afs_int32 error, code, curserver, curpart;
2032     afs_uint32 volid;
2033
2034     MapHostToNetwork(entry);
2035     error = 0;
2036     for (i = 0; i < entry->nServers; i++) {
2037         curserver = entry->serverNumber[i];
2038         curpart = entry->serverPartition[i];
2039         if (entry->serverFlags[i] & ITSROVOL) {
2040             volid = entry->volumeId[ROVOL];
2041         } else {
2042             volid = entry->volumeId[RWVOL];
2043         }
2044         code = UV_DeleteVolume(curserver, curpart, volid);
2045         if (code && !error)
2046             error = code;
2047     }
2048     return error;
2049 }
2050 #endif
2051
2052 static int
2053 DeleteVolume(struct cmd_syndesc *as, void *arock)
2054 {
2055     afs_int32 err, code = 0;
2056     afs_uint32 server = 0;
2057     afs_int32 partition = -1;
2058     afs_uint32 volid;
2059     char pname[10];
2060     afs_int32 idx, j;
2061
2062     if (as->parms[0].items) {
2063         server = GetServer(as->parms[0].items->data);
2064         if (!server) {
2065             fprintf(STDERR, "vos: server '%s' not found in host table\n",
2066                     as->parms[0].items->data);
2067             return ENOENT;
2068         }
2069     }
2070
2071     if (as->parms[1].items) {
2072         partition = volutil_GetPartitionID(as->parms[1].items->data);
2073         if (partition < 0) {
2074             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2075                     as->parms[1].items->data);
2076             return EINVAL;
2077         }
2078
2079         /* Check for validity of the partition */
2080         if (!IsPartValid(partition, server, &code)) {
2081             if (code) {
2082                 PrintError("", code);
2083             } else {
2084                 fprintf(STDERR,
2085                         "vos : partition %s does not exist on the server\n",
2086                         as->parms[1].items->data);
2087             }
2088             return ENOENT;
2089         }
2090     }
2091
2092     volid = vsu_GetVolumeID(as->parms[2].items->data, cstruct, &err);
2093     if (volid == 0) {
2094         fprintf(STDERR, "Can't find volume name '%s' in VLDB\n",
2095                 as->parms[2].items->data);
2096         if (err)
2097             PrintError("", err);
2098         return ENOENT;
2099     }
2100
2101     /* If the server or partition option are not complete, try to fill
2102      * them in from the VLDB entry.
2103      */
2104     if ((partition == -1) || !server) {
2105         struct nvldbentry entry;
2106
2107         code = VLDB_GetEntryByID(volid, -1, &entry);
2108         if (code) {
2109             fprintf(STDERR,
2110                     "Could not fetch the entry for volume %lu from VLDB\n",
2111                     (unsigned long)volid);
2112             PrintError("", code);
2113             return (code);
2114         }
2115
2116         if (((volid == entry.volumeId[RWVOL]) && (entry.flags & RW_EXISTS))
2117             || ((volid == entry.volumeId[BACKVOL])
2118                 && (entry.flags & BACK_EXISTS))) {
2119             idx = Lp_GetRwIndex(&entry);
2120             if ((idx == -1) || (server && (server != entry.serverNumber[idx]))
2121                 || ((partition != -1)
2122                     && (partition != entry.serverPartition[idx]))) {
2123                 fprintf(STDERR, "VLDB: Volume '%s' no match\n",
2124                         as->parms[2].items->data);
2125                 return ENOENT;
2126             }
2127         } else if ((volid == entry.volumeId[ROVOL])
2128                    && (entry.flags & RO_EXISTS)) {
2129             for (idx = -1, j = 0; j < entry.nServers; j++) {
2130                 if (entry.serverFlags[j] != ITSROVOL)
2131                     continue;
2132
2133                 if (((server == 0) || (server == entry.serverNumber[j]))
2134                     && ((partition == -1)
2135                         || (partition == entry.serverPartition[j]))) {
2136                     if (idx != -1) {
2137                         fprintf(STDERR,
2138                                 "VLDB: Volume '%s' matches more than one RO\n",
2139                                 as->parms[2].items->data);
2140                         return ENOENT;
2141                     }
2142                     idx = j;
2143                 }
2144             }
2145             if (idx == -1) {
2146                 fprintf(STDERR, "VLDB: Volume '%s' no match\n",
2147                         as->parms[2].items->data);
2148                 return ENOENT;
2149             }
2150         } else {
2151             fprintf(STDERR, "VLDB: Volume '%s' no match\n",
2152                     as->parms[2].items->data);
2153             return ENOENT;
2154         }
2155
2156         server = htonl(entry.serverNumber[idx]);
2157         partition = entry.serverPartition[idx];
2158     }
2159
2160
2161     code = UV_DeleteVolume(server, partition, volid);
2162     if (code) {
2163         PrintDiagnostics("remove", code);
2164         return code;
2165     }
2166
2167     MapPartIdIntoName(partition, pname);
2168     fprintf(STDOUT, "Volume %lu on partition %s server %s deleted\n",
2169             (unsigned long)volid, pname, hostutil_GetNameByINet(server));
2170     return 0;
2171 }
2172
2173 #define TESTM   0               /* set for move space tests, clear for production */
2174 static int
2175 MoveVolume(struct cmd_syndesc *as, void *arock)
2176 {
2177
2178     afs_uint32 volid;
2179     afs_uint32 fromserver, toserver;
2180     afs_int32 frompart, topart;
2181     afs_int32 flags, code, err;
2182     char fromPartName[10], toPartName[10];
2183
2184     struct diskPartition64 partition;   /* for space check */
2185     volintInfo *p;
2186
2187     volid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
2188     if (volid == 0) {
2189         if (err)
2190             PrintError("", err);
2191         else
2192             fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2193                     as->parms[0].items->data);
2194         return ENOENT;
2195     }
2196     fromserver = GetServer(as->parms[1].items->data);
2197     if (fromserver == 0) {
2198         fprintf(STDERR, "vos: server '%s' not found in host table\n",
2199                 as->parms[1].items->data);
2200         return ENOENT;
2201     }
2202     toserver = GetServer(as->parms[3].items->data);
2203     if (toserver == 0) {
2204         fprintf(STDERR, "vos: server '%s' not found in host table\n",
2205                 as->parms[3].items->data);
2206         return ENOENT;
2207     }
2208     frompart = volutil_GetPartitionID(as->parms[2].items->data);
2209     if (frompart < 0) {
2210         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2211                 as->parms[2].items->data);
2212         return EINVAL;
2213     }
2214     if (!IsPartValid(frompart, fromserver, &code)) {    /*check for validity of the partition */
2215         if (code)
2216             PrintError("", code);
2217         else
2218             fprintf(STDERR,
2219                     "vos : partition %s does not exist on the server\n",
2220                     as->parms[2].items->data);
2221         return ENOENT;
2222     }
2223     topart = volutil_GetPartitionID(as->parms[4].items->data);
2224     if (topart < 0) {
2225         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2226                 as->parms[4].items->data);
2227         return EINVAL;
2228     }
2229     if (!IsPartValid(topart, toserver, &code)) {        /*check for validity of the partition */
2230         if (code)
2231             PrintError("", code);
2232         else
2233             fprintf(STDERR,
2234                     "vos : partition %s does not exist on the server\n",
2235                     as->parms[4].items->data);
2236         return ENOENT;
2237     }
2238
2239     flags = 0;
2240     if (as->parms[5].items) flags |= RV_NOCLONE;
2241
2242     /*
2243      * check source partition for space to clone volume
2244      */
2245
2246     MapPartIdIntoName(topart, toPartName);
2247     MapPartIdIntoName(frompart, fromPartName);
2248
2249     /*
2250      * check target partition for space to move volume
2251      */
2252
2253     code = UV_PartitionInfo64(toserver, toPartName, &partition);
2254     if (code) {
2255         fprintf(STDERR, "vos: cannot access partition %s\n", toPartName);
2256         exit(1);
2257     }
2258     if (TESTM)
2259         fprintf(STDOUT, "target partition %s free space %" AFS_INT64_FMT "\n", toPartName,
2260                 partition.free);
2261
2262     p = (volintInfo *) 0;
2263     code = UV_ListOneVolume(fromserver, frompart, volid, &p);
2264     if (code) {
2265         fprintf(STDERR, "vos:cannot access volume %lu\n",
2266                 (unsigned long)volid);
2267         exit(1);
2268     }
2269     if (TESTM)
2270         fprintf(STDOUT, "volume %lu size %d\n", (unsigned long)volid,
2271                 p->size);
2272     if (partition.free <= p->size) {
2273         fprintf(STDERR,
2274                 "vos: no space on target partition %s to move volume %lu\n",
2275                 toPartName, (unsigned long)volid);
2276         free(p);
2277         exit(1);
2278     }
2279     free(p);
2280
2281     if (TESTM) {
2282         fprintf(STDOUT, "size test - don't do move\n");
2283         exit(0);
2284     }
2285
2286     /* successful move still not guaranteed but shoot for it */
2287
2288     code =
2289         UV_MoveVolume2(volid, fromserver, frompart, toserver, topart, flags);
2290     if (code) {
2291         PrintDiagnostics("move", code);
2292         return code;
2293     }
2294     MapPartIdIntoName(topart, toPartName);
2295     MapPartIdIntoName(frompart, fromPartName);
2296     fprintf(STDOUT, "Volume %lu moved from %s %s to %s %s \n",
2297             (unsigned long)volid, as->parms[1].items->data, fromPartName,
2298             as->parms[3].items->data, toPartName);
2299
2300     return 0;
2301 }
2302
2303 static int
2304 CopyVolume(struct cmd_syndesc *as, void *arock)
2305 {
2306     afs_uint32 volid;
2307     afs_uint32 fromserver, toserver;
2308     afs_int32 frompart, topart, code, err, flags;
2309     char fromPartName[10], toPartName[10], *tovolume;
2310     struct nvldbentry entry;
2311     struct diskPartition64 partition;   /* for space check */
2312     volintInfo *p;
2313
2314     volid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
2315     if (volid == 0) {
2316         if (err)
2317             PrintError("", err);
2318         else
2319             fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2320                     as->parms[0].items->data);
2321         return ENOENT;
2322     }
2323     fromserver = GetServer(as->parms[1].items->data);
2324     if (fromserver == 0) {
2325         fprintf(STDERR, "vos: server '%s' not found in host table\n",
2326                 as->parms[1].items->data);
2327         return ENOENT;
2328     }
2329
2330     toserver = GetServer(as->parms[4].items->data);
2331     if (toserver == 0) {
2332         fprintf(STDERR, "vos: server '%s' not found in host table\n",
2333                 as->parms[4].items->data);
2334         return ENOENT;
2335     }
2336
2337     tovolume = as->parms[3].items->data;
2338     if (!ISNAMEVALID(tovolume)) {
2339         fprintf(STDERR,
2340                 "vos: the name of the root volume %s exceeds the size limit of %d\n",
2341                 tovolume, VOLSER_OLDMAXVOLNAME - 10);
2342         return E2BIG;
2343     }
2344     if (!VolNameOK(tovolume)) {
2345         fprintf(STDERR,
2346                 "Illegal volume name %s, should not end in .readonly or .backup\n",
2347                 tovolume);
2348         return EINVAL;
2349     }
2350     if (IsNumeric(tovolume)) {
2351         fprintf(STDERR, "Illegal volume name %s, should not be a number\n",
2352                 tovolume);
2353         return EINVAL;
2354     }
2355     code = VLDB_GetEntryByName(tovolume, &entry);
2356     if (!code) {
2357         fprintf(STDERR, "Volume %s already exists\n", tovolume);
2358         PrintDiagnostics("copy", code);
2359         return EEXIST;
2360     }
2361
2362     frompart = volutil_GetPartitionID(as->parms[2].items->data);
2363     if (frompart < 0) {
2364         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2365                 as->parms[2].items->data);
2366         return EINVAL;
2367     }
2368     if (!IsPartValid(frompart, fromserver, &code)) {    /*check for validity of the partition */
2369         if (code)
2370             PrintError("", code);
2371         else
2372             fprintf(STDERR,
2373                     "vos : partition %s does not exist on the server\n",
2374                     as->parms[2].items->data);
2375         return ENOENT;
2376     }
2377
2378     topart = volutil_GetPartitionID(as->parms[5].items->data);
2379     if (topart < 0) {
2380         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2381                 as->parms[5].items->data);
2382         return EINVAL;
2383     }
2384     if (!IsPartValid(topart, toserver, &code)) {        /*check for validity of the partition */
2385         if (code)
2386             PrintError("", code);
2387         else
2388             fprintf(STDERR,
2389                     "vos : partition %s does not exist on the server\n",
2390                     as->parms[5].items->data);
2391         return ENOENT;
2392     }
2393
2394     flags = 0;
2395     if (as->parms[6].items) flags |= RV_OFFLINE;
2396     if (as->parms[7].items) flags |= RV_RDONLY;
2397     if (as->parms[8].items) flags |= RV_NOCLONE;
2398
2399     MapPartIdIntoName(topart, toPartName);
2400     MapPartIdIntoName(frompart, fromPartName);
2401
2402     /*
2403      * check target partition for space to move volume
2404      */
2405
2406     code = UV_PartitionInfo64(toserver, toPartName, &partition);
2407     if (code) {
2408         fprintf(STDERR, "vos: cannot access partition %s\n", toPartName);
2409         exit(1);
2410     }
2411     if (TESTM)
2412         fprintf(STDOUT, "target partition %s free space %" AFS_INT64_FMT "\n", toPartName,
2413                 partition.free);
2414
2415     p = (volintInfo *) 0;
2416     code = UV_ListOneVolume(fromserver, frompart, volid, &p);
2417     if (code) {
2418         fprintf(STDERR, "vos:cannot access volume %lu\n",
2419                 (unsigned long)volid);
2420         exit(1);
2421     }
2422
2423     if (partition.free <= p->size) {
2424         fprintf(STDERR,
2425                 "vos: no space on target partition %s to copy volume %lu\n",
2426                 toPartName, (unsigned long)volid);
2427         free(p);
2428         exit(1);
2429     }
2430     free(p);
2431
2432     /* successful copy still not guaranteed but shoot for it */
2433
2434     code =
2435         UV_CopyVolume2(volid, fromserver, frompart, tovolume, toserver,
2436                        topart, 0, flags);
2437     if (code) {
2438         PrintDiagnostics("copy", code);
2439         return code;
2440     }
2441     MapPartIdIntoName(topart, toPartName);
2442     MapPartIdIntoName(frompart, fromPartName);
2443     fprintf(STDOUT, "Volume %lu copied from %s %s to %s on %s %s \n",
2444             (unsigned long)volid, as->parms[1].items->data, fromPartName,
2445             tovolume, as->parms[4].items->data, toPartName);
2446
2447     return 0;
2448 }
2449
2450
2451 static int
2452 ShadowVolume(struct cmd_syndesc *as, void *arock)
2453 {
2454     afs_uint32 volid, tovolid;
2455     afs_uint32 fromserver, toserver;
2456     afs_int32 frompart, topart;
2457     afs_int32 code, err, flags;
2458     char fromPartName[10], toPartName[10], toVolName[32], *tovolume;
2459     struct diskPartition64 partition;   /* for space check */
2460     volintInfo *p, *q;
2461
2462     p = (volintInfo *) 0;
2463     q = (volintInfo *) 0;
2464
2465     volid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
2466     if (volid == 0) {
2467         if (err)
2468             PrintError("", err);
2469         else
2470             fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2471                     as->parms[0].items->data);
2472         return ENOENT;
2473     }
2474     fromserver = GetServer(as->parms[1].items->data);
2475     if (fromserver == 0) {
2476         fprintf(STDERR, "vos: server '%s' not found in host table\n",
2477                 as->parms[1].items->data);
2478         return ENOENT;
2479     }
2480
2481     toserver = GetServer(as->parms[3].items->data);
2482     if (toserver == 0) {
2483         fprintf(STDERR, "vos: server '%s' not found in host table\n",
2484                 as->parms[3].items->data);
2485         return ENOENT;
2486     }
2487
2488     frompart = volutil_GetPartitionID(as->parms[2].items->data);
2489     if (frompart < 0) {
2490         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2491                 as->parms[2].items->data);
2492         return EINVAL;
2493     }
2494     if (!IsPartValid(frompart, fromserver, &code)) {    /*check for validity of the partition */
2495         if (code)
2496             PrintError("", code);
2497         else
2498             fprintf(STDERR,
2499                     "vos : partition %s does not exist on the server\n",
2500                     as->parms[2].items->data);
2501         return ENOENT;
2502     }
2503
2504     topart = volutil_GetPartitionID(as->parms[4].items->data);
2505     if (topart < 0) {
2506         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2507                 as->parms[4].items->data);
2508         return EINVAL;
2509     }
2510     if (!IsPartValid(topart, toserver, &code)) {        /*check for validity of the partition */
2511         if (code)
2512             PrintError("", code);
2513         else
2514             fprintf(STDERR,
2515                     "vos : partition %s does not exist on the server\n",
2516                     as->parms[4].items->data);
2517         return ENOENT;
2518     }
2519
2520     if (as->parms[5].items) {
2521         tovolume = as->parms[5].items->data;
2522         if (!ISNAMEVALID(tovolume)) {
2523             fprintf(STDERR,
2524                 "vos: the name of the root volume %s exceeds the size limit of %d\n",
2525                 tovolume, VOLSER_OLDMAXVOLNAME - 10);
2526             return E2BIG;
2527         }
2528         if (!VolNameOK(tovolume)) {
2529             fprintf(STDERR,
2530                 "Illegal volume name %s, should not end in .readonly or .backup\n",
2531                 tovolume);
2532             return EINVAL;
2533         }
2534         if (IsNumeric(tovolume)) {
2535             fprintf(STDERR,
2536                 "Illegal volume name %s, should not be a number\n",
2537                 tovolume);
2538             return EINVAL;
2539         }
2540     } else {
2541         /* use actual name of source volume */
2542         code = UV_ListOneVolume(fromserver, frompart, volid, &p);
2543         if (code) {
2544             fprintf(STDERR, "vos:cannot access volume %lu\n",
2545                 (unsigned long)volid);
2546             exit(1);
2547         }
2548         strcpy(toVolName, p->name);
2549         tovolume = toVolName;
2550         /* save p for size checks later */
2551     }
2552
2553     if (as->parms[6].items) {
2554         tovolid = vsu_GetVolumeID(as->parms[6].items->data, cstruct, &err);
2555         if (tovolid == 0) {
2556             if (err)
2557                 PrintError("", err);
2558             else
2559                 fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2560                         as->parms[6].items->data);
2561             if (p)
2562                 free(p);
2563             return ENOENT;
2564         }
2565     } else {
2566         tovolid = vsu_GetVolumeID(tovolume, cstruct, &err);
2567         if (tovolid == 0) {
2568             if (err)
2569                 PrintError("", err);
2570             else
2571                 fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2572                         tovolume);
2573             if (p)
2574                 free(p);
2575             return ENOENT;
2576         }
2577     }
2578
2579     flags = RV_NOVLDB;
2580     if (as->parms[7].items) flags |= RV_OFFLINE;
2581     if (as->parms[8].items) flags |= RV_RDONLY;
2582     if (as->parms[9].items) flags |= RV_NOCLONE;
2583     if (as->parms[10].items) flags |= RV_CPINCR;
2584
2585     MapPartIdIntoName(topart, toPartName);
2586     MapPartIdIntoName(frompart, fromPartName);
2587
2588     /*
2589      * check target partition for space to move volume
2590      */
2591
2592     code = UV_PartitionInfo64(toserver, toPartName, &partition);
2593     if (code) {
2594         fprintf(STDERR, "vos: cannot access partition %s\n", toPartName);
2595         exit(1);
2596     }
2597     if (TESTM)
2598         fprintf(STDOUT, "target partition %s free space %" AFS_INT64_FMT "\n", toPartName,
2599                 partition.free);
2600
2601     /* Don't do this again if we did it above */
2602     if (!p) {
2603         code = UV_ListOneVolume(fromserver, frompart, volid, &p);
2604         if (code) {
2605             fprintf(STDERR, "vos:cannot access volume %lu\n",
2606                 (unsigned long)volid);
2607             exit(1);
2608         }
2609     }
2610
2611     /* OK if this fails */
2612     code = UV_ListOneVolume(toserver, topart, tovolid, &q);
2613
2614     /* Treat existing volume size as "free" */
2615     if (q)
2616         p->size = (q->size < p->size) ? p->size - q->size : 0;
2617
2618     if (partition.free <= p->size) {
2619         fprintf(STDERR,
2620                 "vos: no space on target partition %s to copy volume %lu\n",
2621                 toPartName, (unsigned long)volid);
2622         free(p);
2623         if (q) free(q);
2624         exit(1);
2625     }
2626     free(p);
2627     if (q) free(q);
2628
2629     /* successful copy still not guaranteed but shoot for it */
2630
2631     code =
2632         UV_CopyVolume2(volid, fromserver, frompart, tovolume, toserver,
2633                        topart, tovolid, flags);
2634     if (code) {
2635         PrintDiagnostics("shadow", code);
2636         return code;
2637     }
2638     MapPartIdIntoName(topart, toPartName);
2639     MapPartIdIntoName(frompart, fromPartName);
2640     fprintf(STDOUT, "Volume %lu shadowed from %s %s to %s %s \n",
2641             (unsigned long)volid, as->parms[1].items->data, fromPartName,
2642             as->parms[3].items->data, toPartName);
2643
2644     return 0;
2645 }
2646
2647
2648 static int
2649 CloneVolume(struct cmd_syndesc *as, void *arock)
2650 {
2651     afs_uint32 volid, cloneid;
2652     afs_uint32 server;
2653     afs_int32 part, voltype;
2654     char partName[10], *volname;
2655     afs_int32 code, err, flags;
2656     struct nvldbentry entry;
2657
2658     volid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
2659     if (volid == 0) {
2660         if (err)
2661             PrintError("", err);
2662         else
2663             fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2664                     as->parms[0].items->data);
2665         return ENOENT;
2666     }
2667
2668     if (as->parms[1].items || as->parms[2].items) {
2669         if (!as->parms[1].items || !as->parms[2].items) {
2670             fprintf(STDERR,
2671                     "Must specify both -server and -partition options\n");
2672             return -1;
2673         }
2674         server = GetServer(as->parms[1].items->data);
2675         if (server == 0) {
2676             fprintf(STDERR, "vos: server '%s' not found in host table\n",
2677                     as->parms[1].items->data);
2678             return ENOENT;
2679         }
2680         part = volutil_GetPartitionID(as->parms[2].items->data);
2681         if (part < 0) {
2682             fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
2683                     as->parms[2].items->data);
2684             return EINVAL;
2685         }
2686         if (!IsPartValid(part, server, &code)) {        /*check for validity of the partition */
2687             if (code)
2688                 PrintError("", code);
2689             else
2690                 fprintf(STDERR,
2691                     "vos : partition %s does not exist on the server\n",
2692                     as->parms[2].items->data);
2693             return ENOENT;
2694         }
2695     } else {
2696         code = GetVolumeInfo(volid, &server, &part, &voltype, &entry);
2697         if (code)
2698             return code;
2699     }
2700
2701     volname = 0;
2702     if (as->parms[3].items) {
2703         volname = as->parms[3].items->data;
2704         if (strlen(volname) > VOLSER_OLDMAXVOLNAME - 1) {
2705             fprintf(STDERR,
2706                 "vos: the name of the root volume %s exceeds the size limit of %d\n",
2707                 volname, VOLSER_OLDMAXVOLNAME - 1);
2708             return E2BIG;
2709         }
2710 #if 0
2711         /*
2712          * In order that you be able to make clones of RO or BK, this
2713          * check must be omitted.
2714          */
2715         if (!VolNameOK(volname)) {
2716             fprintf(STDERR,
2717                 "Illegal volume name %s, should not end in .readonly or .backup\n",
2718                 volname);
2719             return EINVAL;
2720         }
2721 #endif
2722         if (IsNumeric(volname)) {
2723             fprintf(STDERR,
2724                 "Illegal volume name %s, should not be a number\n",
2725                 volname);
2726             return EINVAL;
2727         }
2728     }
2729
2730     cloneid = 0;
2731     if (as->parms[4].items) {
2732         cloneid = vsu_GetVolumeID(as->parms[4].items->data, cstruct, &err);
2733         if (cloneid == 0) {
2734             if (err)
2735                 PrintError("", err);
2736             else
2737                 fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2738                         as->parms[4].items->data);
2739             return ENOENT;
2740         }
2741     }
2742
2743     flags = 0;
2744     if (as->parms[5].items) flags |= RV_OFFLINE;
2745     if (as->parms[6].items && as->parms[7].items) {
2746         fprintf(STDERR, "vos: cannot specify that a volume be -readwrite and -readonly\n");
2747         return EINVAL;
2748     }
2749     if (as->parms[6].items) flags |= RV_RDONLY;
2750     if (as->parms[7].items) flags |= RV_RWONLY;
2751
2752
2753     code =
2754         UV_CloneVolume(server, part, volid, cloneid, volname, flags);
2755
2756     if (code) {
2757         PrintDiagnostics("clone", code);
2758         return code;
2759     }
2760     MapPartIdIntoName(part, partName);
2761     fprintf(STDOUT, "Created clone for volume %s\n",
2762             as->parms[0].items->data);
2763
2764     return 0;
2765 }
2766
2767
2768 static int
2769 BackupVolume(struct cmd_syndesc *as, void *arock)
2770 {
2771     afs_uint32 avolid;
2772     afs_uint32 aserver;
2773     afs_int32 apart, vtype, code, err;
2774     struct nvldbentry entry;
2775
2776     afs_uint32 buvolid;
2777     afs_uint32 buserver;
2778     afs_int32 bupart, butype;
2779     struct nvldbentry buentry;
2780
2781     avolid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
2782     if (avolid == 0) {
2783         if (err)
2784             PrintError("", err);
2785         else
2786             fprintf(STDERR, "vos: can't find volume ID or name '%s'\n",
2787                     as->parms[0].items->data);
2788         return ENOENT;
2789     }
2790     code = GetVolumeInfo(avolid, &aserver, &apart, &vtype, &entry);
2791     if (code)
2792         exit(1);
2793
2794     /* verify this is a readwrite volume */
2795
2796     if (vtype != RWVOL) {
2797         fprintf(STDERR, "%s not RW volume\n", as->parms[0].items->data);
2798         exit(1);
2799     }
2800
2801     /* is there a backup volume already? */
2802
2803     if (entry.flags & BACK_EXISTS) {
2804         /* yep, where is it? */
2805
2806         buvolid = entry.volumeId[BACKVOL];
2807         code = GetVolumeInfo(buvolid, &buserver, &bupart, &butype, &buentry);
2808         if (code)
2809             exit(1);
2810
2811         /* is it local? */
2812         code = VLDB_IsSameAddrs(buserver, aserver, &err);
2813         if (err) {
2814             fprintf(STDERR,
2815                     "Failed to get info about server's %d address(es) from vlserver; aborting call!\n",
2816                     buserver);
2817             exit(1);
2818         }
2819         if (!code) {
2820             fprintf(STDERR,
2821                     "FATAL ERROR: backup volume %lu exists on server %lu\n",
2822                     (unsigned long)buvolid, (unsigned long)buserver);
2823             exit(1);
2824         }
2825     }
2826
2827     /* nope, carry on */
2828
2829     code = UV_BackupVolume(aserver, apart, avolid);
2830
2831     if (code) {
2832         PrintDiagnostics("backup", code);
2833         return code;
2834     }
2835     fprintf(STDOUT, "Created backup volume for %s \n",
2836             as->parms[0].items->data);
2837     return 0;
2838 }
2839
2840 static int
2841 ReleaseVolume(struct cmd_syndesc *as, void *arock)
2842 {
2843
2844     struct nvldbentry entry;
2845     afs_uint32 avolid;
2846     afs_uint32 aserver;
2847     afs_int32 apart, vtype, code, err;
2848     int force = 0;
2849     int stayUp = 0;
2850
2851     if (as->parms[1].items)
2852         force = 1;
2853     if (as->parms[2].items)
2854         stayUp = 1;
2855     avolid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
2856     if (avolid == 0) {
2857         if (err)
2858             PrintError("", err);
2859         else
2860             fprintf(STDERR, "vos: can't find volume '%s'\n",
2861                     as->parms[0].items->data);
2862         return ENOENT;
2863     }
2864     code = GetVolumeInfo(avolid, &aserver, &apart, &vtype, &entry);
2865     if (code)
2866         return code;
2867
2868     if (vtype != RWVOL) {
2869         fprintf(STDERR, "%s not a RW volume\n", as->parms[0].items->data);
2870         return (ENOENT);
2871     }
2872
2873     if (!ISNAMEVALID(entry.name)) {
2874         fprintf(STDERR,
2875                 "Volume name %s is too long, rename before releasing\n",
2876                 entry.name);
2877         return E2BIG;
2878     }
2879
2880     code = UV_ReleaseVolume(avolid, aserver, apart, force, stayUp);
2881
2882     if (code) {
2883         PrintDiagnostics("release", code);
2884         return code;
2885     }
2886     fprintf(STDOUT, "Released volume %s successfully\n",
2887             as->parms[0].items->data);
2888     return 0;
2889 }
2890
2891 static int
2892 DumpVolumeCmd(struct cmd_syndesc *as, void *arock)
2893 {
2894     afs_uint32 avolid;
2895     afs_uint32 aserver;
2896     afs_int32 apart, voltype, fromdate = 0, code, err, i, flags;
2897     char filename[MAXPATHLEN];
2898     struct nvldbentry entry;
2899
2900     rx_SetRxDeadTime(60 * 10);
2901     for (i = 0; i < MAXSERVERS; i++) {
2902         struct rx_connection *rxConn = ubik_GetRPCConn(cstruct, i);
2903         if (rxConn == 0)
2904             break;
2905         rx_SetConnDeadTime(rxConn, rx_connDeadTime);
2906         if (rx_ServiceOf(rxConn))
2907             rx_ServiceOf(rxConn)->connDeadTime = rx_connDeadTime;
2908     }
2909
2910     avolid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
2911     if (avolid == 0) {
2912         if (err)
2913             PrintError("", err);
2914         else
2915             fprintf(STDERR, "vos: can't find volume '%s'\n",
2916                     as->parms[0].items->data);
2917         return ENOENT;
2918     }
2919
2920     if (as->parms[3].items || as->parms[4].items) {
2921         if (!as->parms[3].items || !as->parms[4].items) {
2922             fprintf(STDERR,
2923                     "Must specify both -server and -partition options\n");
2924             return -1;
2925         }
2926         aserver = GetServer(as->parms[3].items->data);
2927         if (aserver == 0) {
2928             fprintf(STDERR, "Invalid server name\n");
2929             return -1;
2930         }
2931         apart = volutil_GetPartitionID(as->parms[4].items->data);
2932         if (apart < 0) {
2933             fprintf(STDERR, "Invalid partition name\n");
2934             return -1;
2935         }
2936     } else {
2937         code = GetVolumeInfo(avolid, &aserver, &apart, &voltype, &entry);
2938         if (code)
2939             return code;
2940     }
2941
2942     if (as->parms[1].items && strcmp(as->parms[1].items->data, "0")) {
2943         code = ktime_DateToInt32(as->parms[1].items->data, &fromdate);
2944         if (code) {
2945             fprintf(STDERR, "vos: failed to parse date '%s' (error=%d))\n",
2946                     as->parms[1].items->data, code);
2947             return code;
2948         }
2949     }
2950     if (as->parms[2].items) {
2951         strcpy(filename, as->parms[2].items->data);
2952     } else {
2953         strcpy(filename, "");
2954     }
2955
2956     flags = as->parms[6].items ? VOLDUMPV2_OMITDIRS : 0;
2957 retry_dump:
2958     if (as->parms[5].items) {
2959         code =
2960             UV_DumpClonedVolume(avolid, aserver, apart, fromdate,
2961                                 DumpFunction, filename, flags);
2962     } else {
2963         code =
2964             UV_DumpVolume(avolid, aserver, apart, fromdate, DumpFunction,
2965                           filename, flags);
2966     }
2967     if ((code == RXGEN_OPCODE) && (as->parms[6].items)) {
2968         flags &= ~VOLDUMPV2_OMITDIRS;
2969         goto retry_dump;
2970     }
2971     if (code) {
2972         PrintDiagnostics("dump", code);
2973         return code;
2974     }
2975     if (strcmp(filename, ""))
2976         fprintf(STDERR, "Dumped volume %s in file %s\n",
2977                 as->parms[0].items->data, filename);
2978     else
2979         fprintf(STDERR, "Dumped volume %s in stdout \n",
2980                 as->parms[0].items->data);
2981     return 0;
2982 }
2983
2984 #define ASK   0
2985 #define ABORT 1
2986 #define FULL  2
2987 #define INC   3
2988
2989 #define TS_DUMP 1
2990 #define TS_KEEP 2
2991 #define TS_NEW  3
2992
2993 static int
2994 RestoreVolumeCmd(struct cmd_syndesc *as, void *arock)
2995 {
2996     afs_uint32 avolid, aparentid;
2997     afs_uint32 aserver;
2998     afs_int32 apart, code, vcode, err;
2999     afs_int32 aoverwrite = ASK;
3000     afs_int32 acreation = 0, alastupdate = 0;
3001     int restoreflags = 0;
3002     int readonly = 0, offline = 0, voltype = RWVOL;
3003     char afilename[MAXPATHLEN], avolname[VOLSER_MAXVOLNAME + 1], apartName[10];
3004     char volname[VOLSER_MAXVOLNAME + 1];
3005     struct nvldbentry entry;
3006
3007     aparentid = 0;
3008     if (as->parms[4].items) {
3009         avolid = vsu_GetVolumeID(as->parms[4].items->data, cstruct, &err);
3010         if (avolid == 0) {
3011             if (err)
3012                 PrintError("", err);
3013             else
3014                 fprintf(STDERR, "vos: can't find volume '%s'\n",
3015                         as->parms[4].items->data);
3016             exit(1);
3017         }
3018     } else
3019         avolid = 0;
3020
3021     if (as->parms[5].items) {
3022         if ((strcmp(as->parms[5].items->data, "a") == 0)
3023             || (strcmp(as->parms[5].items->data, "abort") == 0)) {
3024             aoverwrite = ABORT;
3025         } else if ((strcmp(as->parms[5].items->data, "f") == 0)
3026                    || (strcmp(as->parms[5].items->data, "full") == 0)) {
3027             aoverwrite = FULL;
3028         } else if ((strcmp(as->parms[5].items->data, "i") == 0)
3029                    || (strcmp(as->parms[5].items->data, "inc") == 0)
3030                    || (strcmp(as->parms[5].items->data, "increment") == 0)
3031                    || (strcmp(as->parms[5].items->data, "incremental") == 0)) {
3032             aoverwrite = INC;
3033         } else {
3034             fprintf(STDERR, "vos: %s is not a valid argument to -overwrite\n",
3035                     as->parms[5].items->data);
3036             exit(1);
3037         }
3038     }
3039     if (as->parms[6].items)
3040         offline = 1;
3041     if (as->parms[7].items) {
3042         readonly = 1;
3043         voltype = ROVOL;
3044     }
3045
3046     if (as->parms[8].items) {
3047         if ((strcmp(as->parms[8].items->data, "d") == 0)
3048             || (strcmp(as->parms[8].items->data, "dump") == 0)) {
3049             acreation = TS_DUMP;
3050         } else if ((strcmp(as->parms[8].items->data, "k") == 0)
3051             || (strcmp(as->parms[8].items->data, "keep") == 0)) {
3052             acreation = TS_KEEP;
3053         } else if ((strcmp(as->parms[8].items->data, "n") == 0)
3054             || (strcmp(as->parms[8].items->data, "new") == 0)) {
3055             acreation = TS_NEW;
3056         } else {
3057             fprintf(STDERR, "vos: %s is not a valid argument to -creation\n",
3058                     as->parms[8].items->data);
3059             exit(1);
3060         }
3061     }
3062
3063     if (as->parms[9].items) {
3064         if ((strcmp(as->parms[9].items->data, "d") == 0)
3065             || (strcmp(as->parms[9].items->data, "dump") == 0)) {
3066             alastupdate = TS_DUMP;
3067         } else if ((strcmp(as->parms[9].items->data, "k") == 0)
3068             || (strcmp(as->parms[9].items->data, "keep") == 0)) {
3069             alastupdate = TS_KEEP;
3070         } else if ((strcmp(as->parms[9].items->data, "n") == 0)
3071             || (strcmp(as->parms[9].items->data, "new") == 0)) {
3072             alastupdate = TS_NEW;
3073         } else {
3074             fprintf(STDERR, "vos: %s is not a valid argument to -lastupdate\n",
3075                     as->parms[9].items->data);
3076             exit(1);
3077         }
3078     }
3079
3080     aserver = GetServer(as->parms[0].items->data);
3081     if (aserver == 0) {
3082         fprintf(STDERR, "vos: server '%s' not found in host table\n",
3083                 as->parms[0].items->data);
3084         exit(1);
3085     }
3086     apart = volutil_GetPartitionID(as->parms[1].items->data);
3087     if (apart < 0) {
3088         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3089                 as->parms[1].items->data);
3090         exit(1);
3091     }
3092     if (!IsPartValid(apart, aserver, &code)) {  /*check for validity of the partition */
3093         if (code)
3094             PrintError("", code);
3095         else
3096             fprintf(STDERR,
3097                     "vos : partition %s does not exist on the server\n",
3098                     as->parms[1].items->data);
3099         exit(1);
3100     }
3101     strcpy(avolname, as->parms[2].items->data);
3102     if (!ISNAMEVALID(avolname)) {
3103         fprintf(STDERR,
3104                 "vos: the name of the volume %s exceeds the size limit\n",
3105                 avolname);
3106         exit(1);
3107     }
3108     if (!VolNameOK(avolname)) {
3109         fprintf(STDERR,
3110                 "Illegal volume name %s, should not end in .readonly or .backup\n",
3111                 avolname);
3112         exit(1);
3113     }
3114     if (as->parms[3].items) {
3115         strcpy(afilename, as->parms[3].items->data);
3116         if (!FileExists(afilename)) {
3117             fprintf(STDERR, "Can't access file %s\n", afilename);
3118             exit(1);
3119         }
3120     } else {
3121         strcpy(afilename, "");
3122     }
3123
3124     /* Check if volume exists or not */
3125
3126     vsu_ExtractName(volname, avolname);
3127     vcode = VLDB_GetEntryByName(volname, &entry);
3128     if (vcode) {                /* no volume - do a full restore */
3129         restoreflags = RV_FULLRST;
3130         if ((aoverwrite == INC) || (aoverwrite == ABORT))
3131             fprintf(STDERR,
3132                     "Volume does not exist; Will perform a full restore\n");
3133     }
3134
3135     else if ((!readonly && Lp_GetRwIndex(&entry) == -1) /* RW volume does not exist - do a full */
3136              ||(readonly && !Lp_ROMatch(0, 0, &entry))) {       /* RO volume does not exist - do a full */
3137         restoreflags = RV_FULLRST;
3138         if ((aoverwrite == INC) || (aoverwrite == ABORT))
3139             fprintf(STDERR,
3140                     "%s Volume does not exist; Will perform a full restore\n",
3141                     readonly ? "RO" : "RW");
3142
3143         if (avolid == 0) {
3144             avolid = entry.volumeId[voltype];
3145         } else if (entry.volumeId[voltype] != 0
3146                    && entry.volumeId[voltype] != avolid) {
3147             avolid = entry.volumeId[voltype];
3148         }
3149         aparentid = entry.volumeId[RWVOL];
3150     }
3151
3152     else {                      /* volume exists - do we do a full incremental or abort */
3153         afs_uint32 Oserver;
3154         afs_int32 Opart, Otype, vol_elsewhere = 0;
3155         struct nvldbentry Oentry;
3156         int c, dc;
3157
3158         if (avolid == 0) {
3159             avolid = entry.volumeId[voltype];
3160         } else if (entry.volumeId[voltype] != 0
3161                    && entry.volumeId[voltype] != avolid) {
3162             avolid = entry.volumeId[voltype];
3163         }
3164         aparentid = entry.volumeId[RWVOL];
3165
3166         /* A file name was specified  - check if volume is on another partition */
3167         vcode = GetVolumeInfo(avolid, &Oserver, &Opart, &Otype, &Oentry);
3168         if (vcode)
3169             exit(1);
3170
3171         vcode = VLDB_IsSameAddrs(Oserver, aserver, &err);
3172         if (err) {
3173             fprintf(STDERR,
3174                     "Failed to get info about server's %d address(es) from vlserver (err=%d); aborting call!\n",
3175                     Oserver, err);
3176             exit(1);
3177         }
3178         if (!vcode || (Opart != apart))
3179             vol_elsewhere = 1;
3180
3181         if (aoverwrite == ASK) {
3182             if (strcmp(afilename, "") == 0) {   /* The file is from standard in */
3183                 fprintf(STDERR,
3184                         "Volume exists and no -overwrite option specified; Aborting restore command\n");
3185                 exit(1);
3186             }
3187
3188             /* Ask what to do */
3189             if (vol_elsewhere) {
3190                 fprintf(STDERR,
3191                         "The volume %s %u already exists on a different server/part\n",
3192                         volname, entry.volumeId[voltype]);
3193                 fprintf(STDERR,
3194                         "Do you want to do a full restore or abort? [fa](a): ");
3195             } else {
3196                 fprintf(STDERR,
3197                         "The volume %s %u already exists in the VLDB\n",
3198                         volname, entry.volumeId[voltype]);
3199                 fprintf(STDERR,
3200                         "Do you want to do a full/incremental restore or abort? [fia](a): ");
3201             }
3202             dc = c = getchar();
3203             while (!(dc == EOF || dc == '\n'))
3204                 dc = getchar(); /* goto end of line */
3205             if ((c == 'f') || (c == 'F'))
3206                 aoverwrite = FULL;
3207             else if ((c == 'i') || (c == 'I'))
3208                 aoverwrite = INC;
3209             else
3210                 aoverwrite = ABORT;
3211         }
3212
3213         if (aoverwrite == ABORT) {
3214             fprintf(STDERR, "Volume exists; Aborting restore command\n");
3215             exit(1);
3216         } else if (aoverwrite == FULL) {
3217             restoreflags = RV_FULLRST;
3218             fprintf(STDERR,
3219                     "Volume exists; Will delete and perform full restore\n");
3220         } else if (aoverwrite == INC) {
3221             restoreflags = 0;
3222             if (vol_elsewhere) {
3223                 fprintf(STDERR,
3224                         "%s volume %lu already exists on a different server/part; not allowed\n",
3225                         readonly ? "RO" : "RW", (unsigned long)avolid);
3226                 exit(1);
3227             }
3228         }
3229     }
3230     if (offline)
3231         restoreflags |= RV_OFFLINE;
3232     if (readonly)
3233         restoreflags |= RV_RDONLY;
3234
3235     switch (acreation) {
3236         case TS_DUMP:
3237             restoreflags |= RV_CRDUMP;
3238             break;
3239         case TS_KEEP:
3240             restoreflags |= RV_CRKEEP;
3241             break;
3242         case TS_NEW:
3243             restoreflags |= RV_CRNEW;
3244             break;
3245         default:
3246             if (aoverwrite == FULL)
3247                 restoreflags |= RV_CRNEW;
3248             else
3249                 restoreflags |= RV_CRKEEP;
3250     }
3251
3252     switch (alastupdate) {
3253         case TS_DUMP:
3254             restoreflags |= RV_LUDUMP;
3255             break;
3256         case TS_KEEP:
3257             restoreflags |= RV_LUKEEP;
3258             break;
3259         case TS_NEW:
3260             restoreflags |= RV_LUNEW;
3261             break;
3262         default:
3263             restoreflags |= RV_LUDUMP;
3264     }
3265     if (as->parms[10].items) {
3266         restoreflags |= RV_NODEL;
3267     }
3268
3269
3270     code =
3271         UV_RestoreVolume2(aserver, apart, avolid, aparentid,
3272                           avolname, restoreflags, WriteData, afilename);
3273     if (code) {
3274         PrintDiagnostics("restore", code);
3275         exit(1);
3276     }
3277     MapPartIdIntoName(apart, apartName);
3278
3279     /*
3280      * patch typo here - originally "parms[1]", should be "parms[0]"
3281      */
3282
3283     fprintf(STDOUT, "Restored volume %s on %s %s\n", avolname,
3284             as->parms[0].items->data, apartName);
3285     return 0;
3286 }
3287
3288 static int
3289 LockReleaseCmd(struct cmd_syndesc *as, void *arock)
3290 {
3291     afs_uint32 avolid;
3292     afs_int32 code, err;
3293
3294     avolid = vsu_GetVolumeID(as->parms[0].items->data, cstruct, &err);
3295     if (avolid == 0) {
3296         if (err)
3297             PrintError("", err);
3298         else
3299             fprintf(STDERR, "vos: can't find volume '%s'\n",
3300                     as->parms[0].items->data);
3301         exit(1);
3302     }
3303
3304     code = UV_LockRelease(avolid);
3305     if (code) {
3306         PrintDiagnostics("unlock", code);
3307         exit(1);
3308     }
3309     fprintf(STDOUT, "Released lock on vldb entry for volume %s\n",
3310             as->parms[0].items->data);
3311     return 0;
3312 }
3313
3314 static int
3315 AddSite(struct cmd_syndesc *as, void *arock)
3316 {
3317     afs_uint32 avolid;
3318     afs_uint32 aserver;
3319     afs_int32 apart, code, err, arovolid, valid = 0;
3320     char apartName[10], avolname[VOLSER_MAXVOLNAME + 1];
3321
3322     vsu_ExtractName(avolname, as->parms[2].items->data);;
3323     avolid = vsu_GetVolumeID(avolname, cstruct, &err);
3324     if (avolid == 0) {
3325         if (err)
3326             PrintError("", err);
3327         else
3328             fprintf(STDERR, "vos: can't find volume '%s'\n",
3329                     as->parms[2].items->data);
3330         exit(1);
3331     }
3332     arovolid = 0;
3333     if (as->parms[3].items) {
3334         vsu_ExtractName(avolname, as->parms[3].items->data);
3335         arovolid = vsu_GetVolumeID(avolname, cstruct, &err);
3336         if (!arovolid) {
3337             fprintf(STDERR, "vos: invalid ro volume id '%s'\n",
3338                     as->parms[3].items->data);
3339             exit(1);
3340         }
3341     }
3342     aserver = GetServer(as->parms[0].items->data);
3343     if (aserver == 0) {
3344         fprintf(STDERR, "vos: server '%s' not found in host table\n",
3345                 as->parms[0].items->data);
3346         exit(1);
3347     }
3348     apart = volutil_GetPartitionID(as->parms[1].items->data);
3349     if (apart < 0) {
3350         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3351                 as->parms[1].items->data);
3352         exit(1);
3353     }
3354     if (!IsPartValid(apart, aserver, &code)) {  /*check for validity of the partition */
3355         if (code)
3356             PrintError("", code);
3357         else
3358             fprintf(STDERR,
3359                     "vos : partition %s does not exist on the server\n",
3360                     as->parms[1].items->data);
3361         exit(1);
3362     }
3363     if (as->parms[4].items) {
3364         valid = 1;
3365     }
3366     code = UV_AddSite2(aserver, apart, avolid, arovolid, valid);
3367     if (code) {
3368         PrintDiagnostics("addsite", code);
3369         exit(1);
3370     }
3371     MapPartIdIntoName(apart, apartName);
3372     fprintf(STDOUT, "Added replication site %s %s for volume %s\n",
3373             as->parms[0].items->data, apartName, as->parms[2].items->data);
3374     return 0;
3375 }
3376
3377 static int
3378 RemoveSite(struct cmd_syndesc *as, void *arock)
3379 {
3380
3381     afs_uint32 avolid;
3382     afs_uint32 aserver;
3383     afs_int32 apart, code, err;
3384     char apartName[10], avolname[VOLSER_MAXVOLNAME + 1];
3385
3386     vsu_ExtractName(avolname, as->parms[2].items->data);
3387     avolid = vsu_GetVolumeID(avolname, cstruct, &err);
3388     if (avolid == 0) {
3389         if (err)
3390             PrintError("", err);
3391         else
3392             fprintf(STDERR, "vos: can't find volume '%s'\n",
3393                     as->parms[2].items->data);
3394         exit(1);
3395     }
3396     aserver = GetServer(as->parms[0].items->data);
3397     if (aserver == 0) {
3398         fprintf(STDERR, "vos: server '%s' not found in host table\n",
3399                 as->parms[0].items->data);
3400         exit(1);
3401     }
3402     apart = volutil_GetPartitionID(as->parms[1].items->data);
3403     if (apart < 0) {
3404         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3405                 as->parms[1].items->data);
3406         exit(1);
3407     }
3408 /*
3409  *skip the partition validity check, since it is possible that the partition
3410  *has since been decomissioned.
3411  */
3412 /*
3413         if (!IsPartValid(apart,aserver,&code)){
3414             if(code) PrintError("",code);
3415             else fprintf(STDERR,"vos : partition %s does not exist on the server\n",as->parms[1].items->data);
3416             exit(1);
3417         }
3418 */
3419     code = UV_RemoveSite(aserver, apart, avolid);
3420     if (code) {
3421         PrintDiagnostics("remsite", code);
3422         exit(1);
3423     }
3424     MapPartIdIntoName(apart, apartName);
3425     fprintf(STDOUT, "Removed replication site %s %s for volume %s\n",
3426             as->parms[0].items->data, apartName, as->parms[2].items->data);
3427     return 0;
3428 }
3429
3430 static int
3431 ChangeLocation(struct cmd_syndesc *as, void *arock)
3432 {
3433     afs_uint32 avolid;
3434     afs_uint32 aserver;
3435     afs_int32 apart, code, err;
3436     char apartName[10];
3437
3438     avolid = vsu_GetVolumeID(as->parms[2].items->data, cstruct, &err);
3439     if (avolid == 0) {
3440         if (err)
3441             PrintError("", err);
3442         else
3443             fprintf(STDERR, "vos: can't find volume '%s'\n",
3444                     as->parms[2].items->data);
3445         exit(1);
3446     }
3447     aserver = GetServer(as->parms[0].items->data);
3448     if (aserver == 0) {
3449         fprintf(STDERR, "vos: server '%s' not found in host table\n",
3450                 as->parms[0].items->data);
3451         exit(1);
3452     }
3453     apart = volutil_GetPartitionID(as->parms[1].items->data);
3454     if (apart < 0) {
3455         fprintf(STDERR, "vos: could not interpret partition name '%s'\n",
3456                 as->parms[1].items->data);
3457         exit(1);
3458     }
3459     if (!IsPartValid(apart, aserver, &code)) {  /*check for validity of the partition */
3460         if (code)
3461             PrintError("", code);
3462         else
3463             fprintf(STDERR,
3464                     "vos : partition %s does not exist on the server\n",
3465                     as->parms[1].items->data);
3466         exit(1);
3467     }
3468     code = UV_ChangeLocation(aserver, apart, avolid);
3469     if (code) {
3470         PrintDiagnostics("changeloc", code);
3471         exit(1);
3472     }
3473     MapPartIdIntoName(apart, apartName);
3474     fprintf(STDOUT, "Changed location to %s %s for volume %s\n",
3475             as->parms[0].items->data, apartName, as->parms[2].items->data);
3476     return 0;
3477 }
3478
3479 static int
3480 ListPartitions(struct cmd_syndesc *as, void *arock)
3481 {
3482     afs_uint32 aserver;
3483     afs_int32 code;
3484     struct partList dummyPartList;
3485     int i;
3486     char pname[10];
3487     int total, cnt;
3488
3489     aserver = GetServer(as->parms[0].items->data);
3490     if (aserver == 0) {
3491         fprintf(STDERR, "vos: server '%s' not found in host table\n",
3492                 as->parms[0].items->data);
3493         exit(1);
3494     }
3495
3496
3497     code = UV_ListPartitions(aserver, &dummyPartList, &cnt);
3498     if (code) {
3499         PrintDiagnostics("listpart", code);
3500         exit(1);
3501     }
3502     total = 0;
3503     fprintf(STDOUT, "The partitions on the server are:\n");
3504     for (i = 0; i < cnt; i++) {
3505         if (dummyPartList.partFlags[i] & PARTVALID) {
3506             memset(pname, 0, sizeof(pname));
3507             MapPartIdIntoName(dummyPartList.partId[i], pname);
3508             fprintf(STDOUT, " %10s ", pname);
3509             total++;
3510             if ((i % 5) == 0 && (i != 0))
3511                 fprintf(STDOUT, "\n");
3512         }
3513     }
3514     fprintf(STDOUT, "\n");
3515     fprintf(STDOUT, "Total: %d\n", total);
3516     return 0;
3517
3518 }
3519
3520 static int
3521 CompareVolName(const void *p1, const void *p2)
3522 {
3523     volintInfo *arg1, *arg2;
3524
3525     arg1 = (volintInfo *) p1;
3526     arg2 = (volintInfo *) p2;
3527     return (strcmp(arg1->name, arg2->name));
3528
3529 }
3530
3531 /*------------------------------------------------------------------------
3532  * PRIVATE XCompareVolName
3533  *
3534  * Description:
3535  *      Comparison routine for volume names coming from an extended
3536  *      volume listing.
3537  *
3538  * Arguments:
3539  *      a_obj1P : Char ptr to first extended vol info object
3540  *      a_obj1P : Char ptr to second extended vol info object
3541  *
3542  * Returns:
3543  *      The value of strcmp() on the volume names within the passed
3544  *      objects (i,e., -1, 0, or 1).
3545  *
3546  * Environment:
3547  *      Passed to qsort() as the designated comparison routine.
3548  *
3549  * Side Effects:
3550  *      As advertised.
3551  *------------------------------------------------------------------------*/
3552
3553 static int
3554 XCompareVolName(const void *a_obj1P, const void *a_obj2P)
3555 {                               /*XCompareVolName */
3556
3557     return (strcmp
3558             (((struct volintXInfo *)(a_obj1P))->name,
3559              ((struct volintXInfo *)(a_obj2P))->name));
3560
3561 }                               /*XCompareVolName */
3562
3563 static int
3564 CompareVolID(const void *p1, const void *p2)
3565 {
3566     volintInfo *arg1, *arg2;
3567
3568     arg1 = (volintInfo *) p1;
3569     arg2 = (volintInfo *) p2;
3570     if (arg1->volid == arg2->volid)
3571         return 0;
3572     if (arg1->volid > arg2->volid)
3573         return 1;
3574     else
3575         return -1;
3576
3577 }
3578
3579 /*------------------------------------------------------------------------
3580  * PRIVATE XCompareVolID
3581  *
3582  * Description:
3583  *      Comparison routine for volume IDs coming from an extended
3584  *      volume listing.
3585  *
3586  * Arguments:
3587  *      a_obj1P : Char ptr to first extended vol info object
3588  *      a_obj1P : Char ptr to second extended vol info object
3589  *
3590  * Returns:
3591  *      The value of strcmp() on the volume names within the passed
3592  *      objects (i,e., -1, 0, or 1).
3593  *
3594  * Environment:
3595  *      Passed to qsort() as the designated comparison routine.
3596  *
3597  * Side Effects:
3598  *      As advertised.
3599  *------------------------------------------------------------------------*/
3600
3601 static int
3602 XCompareVolID(const void *a_obj1P, const void *a_obj2P)
3603 {                               /*XCompareVolID */
3604
3605     afs_int32 id1, id2;         /*Volume IDs we're comparing */
3606
3607     id1 = ((struct volintXInfo *)(a_obj1P))->volid;
3608     id2 = ((struct volintXInfo *)(a_obj2P))->volid;
3609     if (id1 == id2)
3610         return (0);
3611     else if (id1 > id2)
3612         return (1);
3613     else
3614         return (-1);
3615
3616 }                               /*XCompareVolID */
3617
3618 /*------------------------------------------------------------------------
3619  * PRIVATE ListVolumes
3620  *
3621  * Description:
3622  *      Routine used to list volumes, contacting the Volume Server
3623  *      directly, bypassing the VLDB.
3624  *
3625  * Arguments:
3626  *      as : Ptr to parsed command line arguments.
3627  *
3628  * Returns:
3629  *      0                       Successful operation
3630  *
3631  * Environment:
3632  *      Nothing interesting.
3633  *
3634  * Side Effects:
3635  *      As advertised.
3636  *------------------------------------------------------------------------*/
3637
3638 static int
3639 ListVolumes(struct cmd_syndesc *as, void *arock)
3640 {
3641     afs_int32 apart, int32list, fast;
3642     afs_uint32 aserver;
3643     afs_int32 code;
3644     volintInfo *pntr;
3645     volintInfo *oldpntr = NULL;
3646     afs_int32 count;
3647     int i;
3648     char *base;
3649     volintXInfo *xInfoP;
3650     volintXInfo *origxInfoP = NULL; /*Ptr to current/orig extended vol info */
3651     int wantExtendedInfo;       /*Do we want extended vol info? */
3652
3653     char pname[10];
3654     struct partList dummyPartList;
3655     int all;
3656     int quiet, cnt;
3657
3658     apart = -1;
3659     fast = 0;
3660     int32list = 0;
3661
3662     if (as->parms[3].items)
3663         int32list = 1;
3664     if (as->parms[4].items)
3665         quiet = 1;
3666     else
3667         quiet = 0;
3668     if (as->parms[2].items)
3669         fast = 1;
3670     if (fast)
3671    &