missing-comerr-header-includes-20081130
[openafs.git] / src / venus / fs.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 RCSID
14     ("$Header$");
15
16 #include <afs/afs_args.h>
17 #include <rx/xdr.h>
18 #include <sys/ioctl.h>
19 #include <sys/socket.h>
20 #include <netdb.h>
21 #include <errno.h>
22 #include <stdio.h>
23 #include <netinet/in.h>
24 #include <sys/stat.h>
25 #include <afs/stds.h>
26 #include <afs/vice.h>
27 #include <afs/venus.h>
28 #include <afs/com_err.h>
29 #ifdef  AFS_AIX32_ENV
30 #include <signal.h>
31 #endif
32
33 #include <string.h>
34
35 #undef VIRTUE
36 #undef VICE
37 #include "afs/prs_fs.h"
38 #include <afs/afsint.h>
39 #include <afs/cellconfig.h>
40 #include <ubik.h>
41 #include <rx/rxkad.h>
42 #include <rx/rx_globals.h>
43 #include <afs/vldbint.h>
44 #include <afs/volser.h>
45 #include <afs/vlserver.h>
46 #include <afs/cmd.h>
47 #include <afs/afsutil.h>
48 #include <afs/com_err.h>
49 #include <stdlib.h>
50 #include <assert.h>
51 #include <afs/ptclient.h>
52
53
54 #define MAXHOSTS 13
55 #define OMAXHOSTS 8
56 #define MAXCELLHOSTS 8
57 #define MAXNAME 100
58 #define MAXSIZE 2048
59 #define MAXINSIZE 1300          /* pioctl complains if data is larger than this */
60 #define VMSGSIZE 128            /* size of msg buf in volume hdr */
61
62 static char space[MAXSIZE];
63 static char tspace[1024];
64 static struct ubik_client *uclient;
65
66 static int GetClientAddrsCmd(), SetClientAddrsCmd(), FlushMountCmd();
67 static int RxStatProcCmd(), RxStatPeerCmd(), GetFidCmd(), UuidCmd();
68
69 extern char *hostutil_GetNameByINet();
70 extern struct hostent *hostutil_GetHostByName();
71
72
73 static char pn[] = "fs";
74 static int rxInitDone = 0;
75
76 static void ZapList();
77 static int PruneList();
78 static CleanAcl();
79 static int SetVolCmd(struct cmd_syndesc *as, void *arock);
80 static GetCellName();
81 static VLDBInit();
82 static void Die();
83
84 /*
85  * Character to use between name and rights in printed representation for
86  * DFS ACL's.
87  */
88 #define DFS_SEPARATOR   ' '
89
90 typedef char sec_rgy_name_t[1025];      /* A DCE definition */
91
92 struct Acl {
93     int dfs;                    /* Originally true if a dfs acl; now also the type
94                                  * of the acl (1, 2, or 3, corresponding to object,
95                                  * initial dir, or initial object). */
96     sec_rgy_name_t cell;        /* DFS cell name */
97     int nplus;
98     int nminus;
99     struct AclEntry *pluslist;
100     struct AclEntry *minuslist;
101 };
102
103 struct AclEntry {
104     struct AclEntry *next;
105     char name[MAXNAME];
106     afs_int32 rights;
107 };
108
109 struct vcxstat2 {
110     afs_int32 callerAccess;
111     afs_int32 cbExpires;
112     afs_int32 anyAccess;
113     char mvstat;
114 };
115
116 static void
117 ZapAcl(acl)
118      struct Acl *acl;
119 {
120     if (!acl)
121         return;
122     ZapList(acl->pluslist);
123     ZapList(acl->minuslist);
124     free(acl);
125 }
126
127 static
128 foldcmp(a, b)
129      char *a;
130      char *b;
131 {
132     char t, u;
133     while (1) {
134         t = *a++;
135         u = *b++;
136         if (t >= 'A' && t <= 'Z')
137             t += 0x20;
138         if (u >= 'A' && u <= 'Z')
139             u += 0x20;
140         if (t != u)
141             return 1;
142         if (t == 0)
143             return 0;
144     }
145 }
146
147 /*
148  * Mods for the AFS/DFS protocol translator.
149  *
150  * DFS rights. It's ugly to put these definitions here, but they
151  * *cannot* change, because they're part of the wire protocol.
152  * In any event, the protocol translator will guarantee these
153  * assignments for AFS cache managers.
154  */
155 #define DFS_READ          0x01
156 #define DFS_WRITE         0x02
157 #define DFS_EXECUTE       0x04
158 #define DFS_CONTROL       0x08
159 #define DFS_INSERT        0x10
160 #define DFS_DELETE        0x20
161
162 /* the application definable ones (backwards from AFS) */
163 #define DFS_USR0 0x80000000     /* "A" bit */
164 #define DFS_USR1 0x40000000     /* "B" bit */
165 #define DFS_USR2 0x20000000     /* "C" bit */
166 #define DFS_USR3 0x10000000     /* "D" bit */
167 #define DFS_USR4 0x08000000     /* "E" bit */
168 #define DFS_USR5 0x04000000     /* "F" bit */
169 #define DFS_USR6 0x02000000     /* "G" bit */
170 #define DFS_USR7 0x01000000     /* "H" bit */
171 #define DFS_USRALL      (DFS_USR0 | DFS_USR1 | DFS_USR2 | DFS_USR3 |\
172                          DFS_USR4 | DFS_USR5 | DFS_USR6 | DFS_USR7)
173
174 /*
175  * Offset of -id switch in command structure for various commands.
176  * The -if switch is the next switch always.
177  */
178 static int parm_setacl_id, parm_copyacl_id, parm_listacl_id;
179
180 /*
181  * Determine whether either the -id or -if switches are present, and
182  * return 0, 1 or 2, as appropriate. Abort if both switches are present.
183  */
184 /*    int id;   Offset of -id switch; -if is next switch */
185 static int
186 getidf(struct cmd_syndesc *as, int id)
187 {
188     int idf = 0;
189
190     if (as->parms[id].items) {
191         idf |= 1;
192     }
193     if (as->parms[id + 1].items) {
194         idf |= 2;
195     }
196     if (idf == 3) {
197         fprintf(stderr,
198                 "%s: you may specify either -id or -if, but not both switches\n",
199                 pn);
200         exit(1);
201     }
202     return idf;
203 }
204
205 static int
206 PRights(afs_int32 arights, int dfs)
207 {
208     if (!dfs) {
209         if (arights & PRSFS_READ)
210             printf("r");
211         if (arights & PRSFS_LOOKUP)
212             printf("l");
213         if (arights & PRSFS_INSERT)
214             printf("i");
215         if (arights & PRSFS_DELETE)
216             printf("d");
217         if (arights & PRSFS_WRITE)
218             printf("w");
219         if (arights & PRSFS_LOCK)
220             printf("k");
221         if (arights & PRSFS_ADMINISTER)
222             printf("a");
223         if (arights & PRSFS_USR0)
224             printf("A");
225         if (arights & PRSFS_USR1)
226             printf("B");
227         if (arights & PRSFS_USR2)
228             printf("C");
229         if (arights & PRSFS_USR3)
230             printf("D");
231         if (arights & PRSFS_USR4)
232             printf("E");
233         if (arights & PRSFS_USR5)
234             printf("F");
235         if (arights & PRSFS_USR6)
236             printf("G");
237         if (arights & PRSFS_USR7)
238             printf("H");
239     } else {
240         if (arights & DFS_READ)
241             printf("r");
242         else
243             printf("-");
244         if (arights & DFS_WRITE)
245             printf("w");
246         else
247             printf("-");
248         if (arights & DFS_EXECUTE)
249             printf("x");
250         else
251             printf("-");
252         if (arights & DFS_CONTROL)
253             printf("c");
254         else
255             printf("-");
256         if (arights & DFS_INSERT)
257             printf("i");
258         else
259             printf("-");
260         if (arights & DFS_DELETE)
261             printf("d");
262         else
263             printf("-");
264         if (arights & (DFS_USRALL))
265             printf("+");
266         if (arights & DFS_USR0)
267             printf("A");
268         if (arights & DFS_USR1)
269             printf("B");
270         if (arights & DFS_USR2)
271             printf("C");
272         if (arights & DFS_USR3)
273             printf("D");
274         if (arights & DFS_USR4)
275             printf("E");
276         if (arights & DFS_USR5)
277             printf("F");
278         if (arights & DFS_USR6)
279             printf("G");
280         if (arights & DFS_USR7)
281             printf("H");
282     }
283     return 0;
284 }
285
286 /* this function returns TRUE (1) if the file is in AFS, otherwise false (0) */
287 static int
288 InAFS(char *apath)
289 {
290     struct ViceIoctl blob;
291     afs_int32 code;
292
293     blob.in_size = 0;
294     blob.out_size = MAXSIZE;
295     blob.out = space;
296
297     code = pioctl(apath, VIOC_FILE_CELL_NAME, &blob, 1);
298     if (code) {
299         if ((errno == EINVAL) || (errno == ENOENT))
300             return 0;
301     }
302     return 1;
303 }
304
305 /* return a static pointer to a buffer */
306 static char *
307 Parent(char *apath)
308 {
309     char *tp;
310     strcpy(tspace, apath);
311     tp = strrchr(tspace, '/');
312     if (tp) {
313         *tp = 0;
314     } else
315         strcpy(tspace, ".");
316     return tspace;
317 }
318
319 enum rtype { add, destroy, deny };
320
321 static afs_int32
322 Convert(char *arights, int dfs, enum rtype *rtypep)
323 {
324     int i, len;
325     afs_int32 mode;
326     char tc;
327
328     *rtypep = add;              /* add rights, by default */
329
330     if (dfs) {
331         if (!strcmp(arights, "null")) {
332             *rtypep = deny;
333             return 0;
334         }
335         if (!strcmp(arights, "read"))
336             return DFS_READ | DFS_EXECUTE;
337         if (!strcmp(arights, "write"))
338             return DFS_READ | DFS_EXECUTE | DFS_INSERT | DFS_DELETE |
339                 DFS_WRITE;
340         if (!strcmp(arights, "all"))
341             return DFS_READ | DFS_EXECUTE | DFS_INSERT | DFS_DELETE |
342                 DFS_WRITE | DFS_CONTROL;
343     } else {
344         if (!strcmp(arights, "read"))
345             return PRSFS_READ | PRSFS_LOOKUP;
346         if (!strcmp(arights, "write"))
347             return PRSFS_READ | PRSFS_LOOKUP | PRSFS_INSERT | PRSFS_DELETE |
348                 PRSFS_WRITE | PRSFS_LOCK;
349         if (!strcmp(arights, "mail"))
350             return PRSFS_INSERT | PRSFS_LOCK | PRSFS_LOOKUP;
351         if (!strcmp(arights, "all"))
352             return PRSFS_READ | PRSFS_LOOKUP | PRSFS_INSERT | PRSFS_DELETE |
353                 PRSFS_WRITE | PRSFS_LOCK | PRSFS_ADMINISTER;
354     }
355     if (!strcmp(arights, "none")) {
356         *rtypep = destroy;      /* Remove entire entry */
357         return 0;
358     }
359     len = strlen(arights);
360     mode = 0;
361     for (i = 0; i < len; i++) {
362         tc = *arights++;
363         if (dfs) {
364             if (tc == '-')
365                 continue;
366             else if (tc == 'r')
367                 mode |= DFS_READ;
368             else if (tc == 'w')
369                 mode |= DFS_WRITE;
370             else if (tc == 'x')
371                 mode |= DFS_EXECUTE;
372             else if (tc == 'c')
373                 mode |= DFS_CONTROL;
374             else if (tc == 'i')
375                 mode |= DFS_INSERT;
376             else if (tc == 'd')
377                 mode |= DFS_DELETE;
378             else if (tc == 'A')
379                 mode |= DFS_USR0;
380             else if (tc == 'B')
381                 mode |= DFS_USR1;
382             else if (tc == 'C')
383                 mode |= DFS_USR2;
384             else if (tc == 'D')
385                 mode |= DFS_USR3;
386             else if (tc == 'E')
387                 mode |= DFS_USR4;
388             else if (tc == 'F')
389                 mode |= DFS_USR5;
390             else if (tc == 'G')
391                 mode |= DFS_USR6;
392             else if (tc == 'H')
393                 mode |= DFS_USR7;
394             else {
395                 fprintf(stderr, "%s: illegal DFS rights character '%c'.\n",
396                         pn, tc);
397                 exit(1);
398             }
399         } else {
400             if (tc == 'r')
401                 mode |= PRSFS_READ;
402             else if (tc == 'l')
403                 mode |= PRSFS_LOOKUP;
404             else if (tc == 'i')
405                 mode |= PRSFS_INSERT;
406             else if (tc == 'd')
407                 mode |= PRSFS_DELETE;
408             else if (tc == 'w')
409                 mode |= PRSFS_WRITE;
410             else if (tc == 'k')
411                 mode |= PRSFS_LOCK;
412             else if (tc == 'a')
413                 mode |= PRSFS_ADMINISTER;
414             else if (tc == 'A')
415                 mode |= PRSFS_USR0;
416             else if (tc == 'B')
417                 mode |= PRSFS_USR1;
418             else if (tc == 'C')
419                 mode |= PRSFS_USR2;
420             else if (tc == 'D')
421                 mode |= PRSFS_USR3;
422             else if (tc == 'E')
423                 mode |= PRSFS_USR4;
424             else if (tc == 'F')
425                 mode |= PRSFS_USR5;
426             else if (tc == 'G')
427                 mode |= PRSFS_USR6;
428             else if (tc == 'H')
429                 mode |= PRSFS_USR7;
430             else {
431                 fprintf(stderr, "%s: illegal rights character '%c'.\n", pn,
432                         tc);
433                 exit(1);
434             }
435         }
436     }
437     return mode;
438 }
439
440 static struct AclEntry *
441 FindList(struct AclEntry *alist, char *aname)
442 {
443     while (alist) {
444         if (!foldcmp(alist->name, aname))
445             return alist;
446         alist = alist->next;
447     }
448     return 0;
449 }
450
451 /* if no parm specified in a particular slot, set parm to be "." instead */
452 static void
453 SetDotDefault(struct cmd_item **aitemp)
454 {
455     struct cmd_item *ti;
456     if (*aitemp)
457         return;                 /* already has value */
458     /* otherwise, allocate an item representing "." */
459     ti = (struct cmd_item *)malloc(sizeof(struct cmd_item));
460     assert(ti);
461     ti->next = (struct cmd_item *)0;
462     ti->data = (char *)malloc(2);
463     assert(ti->data);
464     strcpy(ti->data, ".");
465     *aitemp = ti;
466 }
467
468 static void
469 ChangeList(struct Acl *al, afs_int32 plus, char *aname, afs_int32 arights)
470 {
471     struct AclEntry *tlist;
472     tlist = (plus ? al->pluslist : al->minuslist);
473     tlist = FindList(tlist, aname);
474     if (tlist) {
475         /* Found the item already in the list. */
476         tlist->rights = arights;
477         if (plus)
478             al->nplus -= PruneList(&al->pluslist, al->dfs);
479         else
480             al->nminus -= PruneList(&al->minuslist, al->dfs);
481         return;
482     }
483     /* Otherwise we make a new item and plug in the new data. */
484     tlist = (struct AclEntry *)malloc(sizeof(struct AclEntry));
485     assert(tlist);
486     strcpy(tlist->name, aname);
487     tlist->rights = arights;
488     if (plus) {
489         tlist->next = al->pluslist;
490         al->pluslist = tlist;
491         al->nplus++;
492         if (arights == 0 || arights == -1)
493             al->nplus -= PruneList(&al->pluslist, al->dfs);
494     } else {
495         tlist->next = al->minuslist;
496         al->minuslist = tlist;
497         al->nminus++;
498         if (arights == 0)
499             al->nminus -= PruneList(&al->minuslist, al->dfs);
500     }
501 }
502
503 static void
504 ZapList(struct AclEntry *alist)
505 {
506     struct AclEntry *tp, *np;
507     for (tp = alist; tp; tp = np) {
508         np = tp->next;
509         free(tp);
510     }
511 }
512
513 static int
514 PruneList(struct AclEntry **ae, int dfs)
515 {
516     struct AclEntry **lp;
517     struct AclEntry *te, *ne;
518     afs_int32 ctr;
519     ctr = 0;
520     lp = ae;
521     for (te = *ae; te; te = ne) {
522         if ((!dfs && te->rights == 0) || te->rights == -1) {
523             *lp = te->next;
524             ne = te->next;
525             free(te);
526             ctr++;
527         } else {
528             ne = te->next;
529             lp = &te->next;
530         }
531     }
532     return ctr;
533 }
534
535 static char *
536 SkipLine(char *astr)
537 {
538     while (*astr != '\n')
539         astr++;
540     astr++;
541     return astr;
542 }
543
544 /*
545  * Create an empty acl, taking into account whether the acl pointed
546  * to by astr is an AFS or DFS acl. Only parse this minimally, so we
547  * can recover from problems caused by bogus ACL's (in that case, always
548  * assume that the acl is AFS: for DFS, the user can always resort to
549  * acl_edit, but for AFS there may be no other way out).
550  */
551 static struct Acl *
552 EmptyAcl(char *astr)
553 {
554     struct Acl *tp;
555     int junk;
556
557     tp = (struct Acl *)malloc(sizeof(struct Acl));
558     assert(tp);
559     tp->nplus = tp->nminus = 0;
560     tp->pluslist = tp->minuslist = 0;
561     tp->dfs = 0;
562     sscanf(astr, "%d dfs:%d %s", &junk, &tp->dfs, tp->cell);
563     return tp;
564 }
565
566 static struct Acl *
567 ParseAcl(char *astr)
568 {
569     int nplus, nminus, i, trights;
570     char tname[MAXNAME];
571     struct AclEntry *first, *last, *tl;
572     struct Acl *ta;
573
574     ta = (struct Acl *)malloc(sizeof(struct Acl));
575     assert(ta);
576     ta->dfs = 0;
577     sscanf(astr, "%d dfs:%d %s", &ta->nplus, &ta->dfs, ta->cell);
578     astr = SkipLine(astr);
579     sscanf(astr, "%d", &ta->nminus);
580     astr = SkipLine(astr);
581
582     nplus = ta->nplus;
583     nminus = ta->nminus;
584
585     last = 0;
586     first = 0;
587     for (i = 0; i < nplus; i++) {
588         sscanf(astr, "%100s %d", tname, &trights);
589         astr = SkipLine(astr);
590         tl = (struct AclEntry *)malloc(sizeof(struct AclEntry));
591         assert(tl);
592         if (!first)
593             first = tl;
594         strcpy(tl->name, tname);
595         tl->rights = trights;
596         tl->next = 0;
597         if (last)
598             last->next = tl;
599         last = tl;
600     }
601     ta->pluslist = first;
602
603     last = 0;
604     first = 0;
605     for (i = 0; i < nminus; i++) {
606         sscanf(astr, "%100s %d", tname, &trights);
607         astr = SkipLine(astr);
608         tl = (struct AclEntry *)malloc(sizeof(struct AclEntry));
609         assert(tl);
610         if (!first)
611             first = tl;
612         strcpy(tl->name, tname);
613         tl->rights = trights;
614         tl->next = 0;
615         if (last)
616             last->next = tl;
617         last = tl;
618     }
619     ta->minuslist = first;
620
621     return ta;
622 }
623
624 static int
625 PrintStatus(VolumeStatus * status, char *name, char *offmsg)
626 {
627     printf("Volume status for vid = %u named %s\n", status->Vid, name);
628     if (*offmsg != 0)
629         printf("Current offline message is %s\n", offmsg);
630     printf("Current disk quota is ");
631     if (status->MaxQuota != 0)
632         printf("%d\n", status->MaxQuota);
633     else
634         printf("unlimited\n");
635     printf("Current blocks used are %d\n", status->BlocksInUse);
636     printf("The partition has %d blocks available out of %d\n\n",
637            status->PartBlocksAvail, status->PartMaxBlocks);
638     return 0;
639 }
640
641 static int
642 QuickPrintStatus(VolumeStatus * status, char *name)
643 {
644     double QuotaUsed = 0.0;
645     double PartUsed = 0.0;
646     int WARN = 0;
647     printf("%-25.25s", name);
648
649     if (status->MaxQuota != 0) {
650         printf("%10d%10d", status->MaxQuota, status->BlocksInUse);
651         QuotaUsed =
652             ((((double)status->BlocksInUse) / status->MaxQuota) * 100.0);
653     } else {
654         printf("  no limit%10d", status->BlocksInUse);
655     }
656     if (QuotaUsed > 90.0) {
657         printf("%5.0f%%<<", QuotaUsed);
658         WARN = 1;
659     } else
660         printf("%5.0f%%  ", QuotaUsed);
661     PartUsed =
662         (100.0 -
663          ((((double)status->PartBlocksAvail) / status->PartMaxBlocks) *
664           100.0));
665     if (PartUsed > 97.0) {
666         printf("%9.0f%%<<", PartUsed);
667         WARN = 1;
668     } else
669         printf("%9.0f%%  ", PartUsed);
670     if (WARN) {
671         printf("  <<WARNING\n");
672     } else
673         printf("\n");
674     return 0;
675 }
676
677 static int
678 QuickPrintSpace(VolumeStatus * status, char *name)
679 {
680     double PartUsed = 0.0;
681     int WARN = 0;
682     printf("%-25.25s", name);
683
684     printf("%10d%10d%10d", status->PartMaxBlocks,
685            status->PartMaxBlocks - status->PartBlocksAvail,
686            status->PartBlocksAvail);
687
688     PartUsed =
689         (100.0 -
690          ((((double)status->PartBlocksAvail) / status->PartMaxBlocks) *
691           100.0));
692     if (PartUsed > 90.0) {
693         printf(" %4.0f%%<<", PartUsed);
694         WARN = 1;
695     } else
696         printf(" %4.0f%%  ", PartUsed);
697     if (WARN) {
698         printf("  <<WARNING\n");
699     } else
700         printf("\n");
701     return 0;
702 }
703
704 static char *
705 AclToString(struct Acl *acl)
706 {
707     static char mydata[MAXSIZE];
708     char tstring[MAXSIZE];
709     char dfsstring[30];
710     struct AclEntry *tp;
711
712     if (acl->dfs)
713         sprintf(dfsstring, " dfs:%d %s", acl->dfs, acl->cell);
714     else
715         dfsstring[0] = '\0';
716     sprintf(mydata, "%d%s\n%d\n", acl->nplus, dfsstring, acl->nminus);
717     for (tp = acl->pluslist; tp; tp = tp->next) {
718         sprintf(tstring, "%s %d\n", tp->name, tp->rights);
719         strcat(mydata, tstring);
720     }
721     for (tp = acl->minuslist; tp; tp = tp->next) {
722         sprintf(tstring, "%s %d\n", tp->name, tp->rights);
723         strcat(mydata, tstring);
724     }
725     return mydata;
726 }
727
728 static int
729 SetACLCmd(struct cmd_syndesc *as, void *arock)
730 {
731     afs_int32 code;
732     struct ViceIoctl blob;
733     struct Acl *ta = 0;
734     struct cmd_item *ti, *ui;
735     int plusp;
736     afs_int32 rights;
737     int clear;
738     int idf = getidf(as, parm_setacl_id);
739     int error = 0;
740
741     if (as->parms[2].items)
742         clear = 1;
743     else
744         clear = 0;
745     plusp = !(as->parms[3].items);
746     for (ti = as->parms[0].items; ti; ti = ti->next) {
747         blob.out_size = MAXSIZE;
748         blob.in_size = idf;
749         blob.in = blob.out = space;
750         code = pioctl(ti->data, VIOCGETAL, &blob, 1);
751         if (code) {
752             Die(errno, ti->data);
753             error = 1;
754             continue;
755         }
756
757         if (ta)
758             ZapAcl(ta);
759         ta = ParseAcl(space);
760         if (!plusp && ta->dfs) {
761             fprintf(stderr,
762                     "%s: %s: you may not use the -negative switch with DFS acl's.\n%s",
763                     pn, ti->data,
764                     "(you may specify \"null\" to revoke all rights, however)\n");
765             error = 1;
766             continue;
767         }
768
769         if (ta)
770             ZapAcl(ta);
771         if (clear)
772             ta = EmptyAcl(space);
773         else
774             ta = ParseAcl(space);
775         CleanAcl(ta, ti->data);
776         for (ui = as->parms[1].items; ui; ui = ui->next->next) {
777             enum rtype rtype;
778             if (!ui->next) {
779                 fprintf(stderr,
780                         "%s: Missing second half of user/access pair.\n", pn);
781                 ZapAcl(ta);
782                 return 1;
783             }
784             rights = Convert(ui->next->data, ta->dfs, &rtype);
785             if (rtype == destroy && !ta->dfs) {
786                 struct AclEntry *tlist;
787
788                 tlist = (plusp ? ta->pluslist : ta->minuslist);
789                 if (!FindList(tlist, ui->data))
790                     continue;
791             }
792             if (rtype == deny && !ta->dfs)
793                 plusp = 0;
794             if (rtype == destroy && ta->dfs)
795                 rights = -1;
796             ChangeList(ta, plusp, ui->data, rights);
797         }
798         blob.in = AclToString(ta);
799         blob.out_size = 0;
800         blob.in_size = 1 + strlen(blob.in);
801         code = pioctl(ti->data, VIOCSETAL, &blob, 1);
802         if (code) {
803             if (errno == EINVAL) {
804                 if (ta->dfs) {
805                     static char *fsenv = 0;
806                     if (!fsenv) {
807                         fsenv = (char *)getenv("FS_EXPERT");
808                     }
809                     fprintf(stderr,
810                             "%s: \"Invalid argument\" was returned when you tried to store a DFS access list.\n",
811                             pn);
812                     if (!fsenv) {
813                         fprintf(stderr,
814                                 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
815                                 "\nPossible reasons for this include:\n\n",
816                                 " -You may have specified an inappropriate combination of rights.\n",
817                                 "  For example, some DFS-supported filesystems may not allow you to\n",
818                                 "  drop the \"c\" right from \"user_obj\".\n\n",
819                                 " -A mask_obj may be required (it is likely required by the underlying\n",
820                                 "  filesystem if you try to set anything other than the basic \"user_obj\"\n",
821                                 "  \"mask_obj\", or \"group_obj\" entries). Unlike acl_edit, the fs command\n",
822                                 "  does not automatically create or update the mask_obj. Try setting\n",
823                                 "  the rights \"mask_obj all\" with \"fs sa\" before adding any explicit\n",
824                                 "  users or groups. You can do this with a single command, such as\n",
825                                 "  \"fs sa mask_obj all user:somename read\"\n\n",
826                                 " -A specified user or group may not exist.\n\n",
827                                 " -You may have tried to delete \"user_obj\", \"group_obj\", or \"other_obj\".\n",
828                                 "  This is probably not allowed by the underlying file system.\n\n",
829                                 " -If you add a user or group to a DFS ACL, remember that it must be\n",
830                                 "  fully specified as \"user:username\" or \"group:groupname\". In addition, there\n",
831                                 "  may be local requirements on the format of the user or group name.\n",
832                                 "  Check with your cell administrator.\n\n",
833                                 " -Or numerous other possibilities. It would be great if we could be more\n",
834                                 "  precise about the actual problem, but for various reasons, this is\n",
835                                 "  impractical via this interface.  If you can't figure it out, you\n",
836                                 "  might try logging into a DCE-equipped machine and use acl_edit (or\n",
837                                 "  whatever is provided). You may get better results. Good luck!\n\n",
838                                 " (You may inhibit this message by setting \"FS_EXPERT\" in your environment)\n");
839                     }
840                 } else {
841                     fprintf(stderr,
842                             "%s: Invalid argument, possible reasons include:\n",
843                             pn);
844                     fprintf(stderr, "\t-File not in AFS\n");
845                     fprintf(stderr,
846                             "\t-Too many users on access control list\n");
847                     fprintf(stderr,
848                             "\t-Tried to add non-existent user to access control list\n");
849                 }
850             } else {
851                 Die(errno, ti->data);
852             }
853             error = 1;
854         }
855     }
856     if (ta)
857         ZapAcl(ta);
858     return error;
859 }
860
861
862 static int
863 CopyACLCmd(struct cmd_syndesc *as, void *arock)
864 {
865     afs_int32 code;
866     struct ViceIoctl blob;
867     struct Acl *fa, *ta = 0;
868     struct AclEntry *tp;
869     struct cmd_item *ti, *ui;
870     int plusp;
871     afs_int32 rights;
872     int clear;
873     int idf = getidf(as, parm_copyacl_id);
874     int error = 0;
875
876     if (as->parms[2].items)
877         clear = 1;
878     else
879         clear = 0;
880     blob.out_size = MAXSIZE;
881     blob.in_size = idf;
882     blob.in = blob.out = space;
883     code = pioctl(as->parms[0].items->data, VIOCGETAL, &blob, 1);
884     if (code) {
885         Die(errno, as->parms[0].items->data);
886         return 1;
887     }
888     fa = ParseAcl(space);
889     CleanAcl(fa, as->parms[0].items->data);
890     for (ti = as->parms[1].items; ti; ti = ti->next) {
891         blob.out_size = MAXSIZE;
892         blob.in_size = idf;
893         blob.in = blob.out = space;
894         code = pioctl(ti->data, VIOCGETAL, &blob, 1);
895         if (code) {
896             Die(errno, ti->data);
897             error = 1;
898             continue;
899         }
900
901         if (ta)
902             ZapAcl(ta);
903         if (clear)
904             ta = EmptyAcl(space);
905         else
906             ta = ParseAcl(space);
907         CleanAcl(ta, ti->data);
908         if (ta->dfs != fa->dfs) {
909             fprintf(stderr,
910                     "%s: incompatible file system types: acl not copied to %s; aborted\n",
911                     pn, ti->data);
912             error = 1;
913             continue;
914         }
915         if (ta->dfs) {
916             if (!clear && strcmp(ta->cell, fa->cell) != 0) {
917                 fprintf(stderr,
918                         "%s: default DCE cell differs for file %s: use \"-clear\" switch; acl not merged\n",
919                         pn, ti->data);
920                 error = 1;
921                 continue;
922             }
923             strcpy(ta->cell, fa->cell);
924         }
925         for (tp = fa->pluslist; tp; tp = tp->next)
926             ChangeList(ta, 1, tp->name, tp->rights);
927         for (tp = fa->minuslist; tp; tp = tp->next)
928             ChangeList(ta, 0, tp->name, tp->rights);
929         blob.in = AclToString(ta);
930         blob.out_size = 0;
931         blob.in_size = 1 + strlen(blob.in);
932         code = pioctl(ti->data, VIOCSETAL, &blob, 1);
933         if (code) {
934             if (errno == EINVAL) {
935                 fprintf(stderr,
936                         "%s: Invalid argument, possible reasons include:\n",
937                         pn);
938                 fprintf(stderr, "\t-File not in AFS\n");
939             } else {
940                 Die(errno, ti->data);
941             }
942             error = 1;
943         }
944     }
945     if (ta)
946         ZapAcl(ta);
947     ZapAcl(fa);
948     return error;
949 }
950
951 /* pioctl() call to get the cellname of a pathname */
952 static afs_int32
953 GetCell(char *fname, char *cellname)
954 {
955     afs_int32 code;
956     struct ViceIoctl blob;
957
958     blob.in_size = 0;
959     blob.out_size = MAXCELLCHARS;
960     blob.out = cellname;
961
962     code = pioctl(fname, VIOC_FILE_CELL_NAME, &blob, 1);
963     return code ? errno : 0;
964 }
965
966 /* Check if a username is valid: If it contains only digits (or a
967  * negative sign), then it might be bad. We then query the ptserver
968  * to see.
969  */
970 static
971 BadName(char *aname, char *fname)
972 {
973     afs_int32 tc, code, id;
974     char *nm;
975     char cell[MAXCELLCHARS];
976
977     for (nm = aname; tc = *nm; nm++) {
978         /* all must be '-' or digit to be bad */
979         if (tc != '-' && (tc < '0' || tc > '9'))
980             return 0;
981     }
982
983     /* Go to the PRDB and see if this all number username is valid */
984     code = GetCell(fname, cell);
985     if (code)
986         return 0;
987
988     pr_Initialize(1, AFSDIR_CLIENT_ETC_DIRPATH, cell);
989     code = pr_SNameToId(aname, &id);
990     pr_End();
991
992     /* 1=>Not-valid; 0=>Valid */
993     return ((!code && (id == ANONYMOUSID)) ? 1 : 0);
994 }
995
996
997 /* clean up an access control list of its bad entries; return 1 if we made
998    any changes to the list, and 0 otherwise */
999 static
1000 CleanAcl(struct Acl *aa, char *fname)
1001 {
1002     struct AclEntry *te, **le, *ne;
1003     int changes;
1004
1005     /* Don't correct DFS ACL's for now */
1006     if (aa->dfs)
1007         return 0;
1008
1009     /* prune out bad entries */
1010     changes = 0;                /* count deleted entries */
1011     le = &aa->pluslist;
1012     for (te = aa->pluslist; te; te = ne) {
1013         ne = te->next;
1014         if (BadName(te->name, fname)) {
1015             /* zap this dude */
1016             *le = te->next;
1017             aa->nplus--;
1018             free(te);
1019             changes++;
1020         } else {
1021             le = &te->next;
1022         }
1023     }
1024     le = &aa->minuslist;
1025     for (te = aa->minuslist; te; te = ne) {
1026         ne = te->next;
1027         if (BadName(te->name, fname)) {
1028             /* zap this dude */
1029             *le = te->next;
1030             aa->nminus--;
1031             free(te);
1032             changes++;
1033         } else {
1034             le = &te->next;
1035         }
1036     }
1037     return changes;
1038 }
1039
1040
1041 /* clean up an acl to not have bogus entries */
1042 static int
1043 CleanACLCmd(struct cmd_syndesc *as, void *arock)
1044 {
1045     afs_int32 code;
1046     struct Acl *ta = 0;
1047     struct ViceIoctl blob;
1048     int changes;
1049     struct cmd_item *ti;
1050     struct AclEntry *te;
1051     int error = 0;
1052
1053     SetDotDefault(&as->parms[0].items);
1054     for (ti = as->parms[0].items; ti; ti = ti->next) {
1055         blob.out_size = MAXSIZE;
1056         blob.in_size = 0;
1057         blob.out = space;
1058         code = pioctl(ti->data, VIOCGETAL, &blob, 1);
1059         if (code) {
1060             Die(errno, ti->data);
1061             error = 1;
1062             continue;
1063         }
1064
1065         if (ta)
1066             ZapAcl(ta);
1067         ta = ParseAcl(space);
1068         if (ta->dfs) {
1069             fprintf(stderr,
1070                     "%s: cleanacl is not supported for DFS access lists.\n",
1071                     pn);
1072             error = 1;
1073             continue;
1074         }
1075
1076         changes = CleanAcl(ta, ti->data);
1077
1078         if (changes) {
1079             /* now set the acl */
1080             blob.in = AclToString(ta);
1081             blob.in_size = strlen(blob.in) + 1;
1082             blob.out_size = 0;
1083             code = pioctl(ti->data, VIOCSETAL, &blob, 1);
1084             if (code) {
1085                 if (errno == EINVAL) {
1086                     fprintf(stderr,
1087                             "%s: Invalid argument, possible reasons include\n",
1088                             pn);
1089                     fprintf(stderr, "%s: File not in vice or\n", pn);
1090                     fprintf(stderr,
1091                             "%s: Too many users on access control list or\n",
1092                             pn);
1093                 } else {
1094                     Die(errno, ti->data);
1095                 }
1096                 error = 1;
1097                 continue;
1098             }
1099
1100             /* now list the updated acl */
1101             printf("Access list for %s is now\n", ti->data);
1102             if (ta->nplus > 0) {
1103                 if (!ta->dfs)
1104                     printf("Normal rights:\n");
1105                 for (te = ta->pluslist; te; te = te->next) {
1106                     printf("  %s ", te->name);
1107                     PRights(te->rights, ta->dfs);
1108                     printf("\n");
1109                 }
1110             }
1111             if (ta->nminus > 0) {
1112                 printf("Negative rights:\n");
1113                 for (te = ta->minuslist; te; te = te->next) {
1114                     printf("  %s ", te->name);
1115                     PRights(te->rights, ta->dfs);
1116                     printf("\n");
1117                 }
1118             }
1119             if (ti->next)
1120                 printf("\n");
1121         } else
1122             printf("Access list for %s is fine.\n", ti->data);
1123     }
1124     if (ta)
1125         ZapAcl(ta);
1126     return error;
1127 }
1128
1129 static int
1130 ListACLCmd(struct cmd_syndesc *as, void *arock)
1131 {
1132     afs_int32 code;
1133     struct Acl *ta;
1134     struct ViceIoctl blob;
1135     struct AclEntry *te;
1136     struct cmd_item *ti;
1137     int idf = getidf(as, parm_listacl_id);
1138     int error = 0;
1139
1140     SetDotDefault(&as->parms[0].items);
1141     for (ti = as->parms[0].items; ti; ti = ti->next) {
1142         blob.out_size = MAXSIZE;
1143         blob.in_size = idf;
1144         blob.in = blob.out = space;
1145         code = pioctl(ti->data, VIOCGETAL, &blob, 1);
1146         if (code) {
1147             Die(errno, ti->data);
1148             error = 1;
1149             continue;
1150         }
1151         ta = ParseAcl(space);
1152         switch (ta->dfs) {
1153         case 0:
1154             printf("Access list for %s is\n", ti->data);
1155             break;
1156         case 1:
1157             printf("DFS access list for %s is\n", ti->data);
1158             break;
1159         case 2:
1160             printf("DFS initial directory access list of %s is\n", ti->data);
1161             break;
1162         case 3:
1163             printf("DFS initial file access list of %s is\n", ti->data);
1164             break;
1165         }
1166         if (ta->dfs) {
1167             printf("  Default cell = %s\n", ta->cell);
1168         }
1169         if (ta->nplus > 0) {
1170             if (!ta->dfs)
1171                 printf("Normal rights:\n");
1172             for (te = ta->pluslist; te; te = te->next) {
1173                 printf("  %s ", te->name);
1174                 PRights(te->rights, ta->dfs);
1175                 printf("\n");
1176             }
1177         }
1178         if (ta->nminus > 0) {
1179             printf("Negative rights:\n");
1180             for (te = ta->minuslist; te; te = te->next) {
1181                 printf("  %s ", te->name);
1182                 PRights(te->rights, ta->dfs);
1183                 printf("\n");
1184             }
1185         }
1186         if (ti->next)
1187             printf("\n");
1188         ZapAcl(ta);
1189     }
1190     return error;
1191 }
1192
1193 static int
1194 GetCallerAccess(struct cmd_syndesc *as, void *arock)
1195 {
1196     struct cmd_item *ti;
1197     int error = 0;
1198
1199     SetDotDefault(&as->parms[0].items);
1200     for (ti = as->parms[0].items; ti; ti = ti->next) {
1201         afs_int32 code;
1202         struct ViceIoctl blob;
1203         struct vcxstat2 stat;
1204         blob.out_size = sizeof(struct vcxstat2);
1205         blob.in_size = 0;
1206         blob.out = (void *)&stat;
1207         code = pioctl(ti->data, VIOC_GETVCXSTATUS2, &blob, 1);
1208         if (code) {
1209             Die(errno, ti->data);
1210             error = 1;
1211             continue;
1212         }
1213         printf("Callers access to %s is ", ti->data);
1214         PRights(stat.callerAccess, 0);
1215         printf("\n");
1216     }
1217     return error;
1218 }
1219
1220 static int
1221 FlushVolumeCmd(struct cmd_syndesc *as, void *arock)
1222 {
1223     afs_int32 code;
1224     struct ViceIoctl blob;
1225     struct cmd_item *ti;
1226     int error = 0;
1227
1228     SetDotDefault(&as->parms[0].items);
1229     for (ti = as->parms[0].items; ti; ti = ti->next) {
1230         blob.in_size = blob.out_size = 0;
1231         code = pioctl(ti->data, VIOC_FLUSHVOLUME, &blob, 0);
1232         if (code) {
1233             fprintf(stderr, "Error flushing volume ");
1234             perror(ti->data);
1235             error = 1;
1236             continue;
1237         }
1238     }
1239     return error;
1240 }
1241
1242 /* 
1243  * The Windows version of UuidCmd displays the UUID.
1244  * When the UNIX version is updated to do the same
1245  * be sure to replace the CMD_REQUIRED flag with
1246  * CMD_OPTIONAL in the cmd_AddParam(-generate) call 
1247  */
1248 static int
1249 UuidCmd(struct cmd_syndesc *as, void *arock)
1250 {
1251     afs_int32 code;
1252     struct ViceIoctl blob;
1253
1254     blob.in_size = 0;
1255     blob.out_size = 0;
1256     
1257     if (as->parms[0].items) {
1258         if (geteuid()) {
1259             fprintf (stderr, "Permission denied: requires root access.\n");
1260             return EACCES;
1261         }
1262
1263         /* generate new UUID */
1264         code = pioctl(0, VIOC_NEWUUID, &blob, 1);
1265
1266         if (code) {
1267             Die(errno, 0);
1268             return 1;
1269         }
1270
1271         printf("New uuid generated.\n");
1272     } else {
1273         /* This will never execute */
1274         printf("Please add the '-generate' option to generate a new UUID.\n");
1275     }
1276     return 0;
1277 }
1278
1279 #if defined(AFS_CACHE_BYPASS)
1280 /*
1281  * Set cache-bypass threshold.  Files larger than this size will not be cached.
1282  * With a threshold of 0, the cache is always bypassed.  With a threshold of -1,
1283  * cache bypass is disabled.
1284  */
1285  
1286 static int
1287 BypassThresholdCmd(struct cmd_syndesc *as, char *arock)
1288 {
1289     afs_int32 code;
1290     afs_int32 size;
1291     struct ViceIoctl blob;
1292     afs_int32 threshold_i, threshold_o;
1293     char *tp;   
1294     
1295     /* if new threshold supplied, then set and confirm, else,
1296      * get current threshold and print
1297      */
1298          
1299     if(as->parms[0].items) {
1300         int digit, ix, len;
1301                                 
1302         tp = as->parms[0].items->data;
1303         len = strlen(tp);
1304         digit = 1; 
1305         for(ix = 0; ix < len; ++ix) {
1306             if(!isdigit(tp[0])) {
1307                 digit = 0;
1308                 break;
1309             }
1310         }
1311         if (digit == 0) {
1312             fprintf(stderr, "fs bypassthreshold -size: %s must be an undecorated digit string.\n", tp);
1313             return EINVAL;
1314         }
1315         threshold_i = atoi(tp);
1316         if(ix > 9 && threshold_i < 2147483647) 
1317             threshold_i = 2147483647;
1318         blob.in = (char *) &threshold_i;
1319         blob.in_size = sizeof(threshold_i);
1320     } else {
1321         blob.in = NULL;
1322         blob.in_size = 0;
1323     }
1324
1325     blob.out = (char *) &threshold_o;
1326     blob.out_size = sizeof(threshold_o);
1327     code = pioctl(0, VIOC_SETBYPASS_THRESH, &blob, 1);
1328     if (code) {
1329         Die(errno, NULL);
1330         return 1;
1331     } else {            
1332         printf("Cache bypass threshold %d", threshold_o);
1333         if(threshold_o ==  -1)
1334             printf(" (disabled)");
1335         printf("\n");
1336     }
1337
1338     return 0;
1339 }
1340
1341 #endif
1342
1343 static int
1344 FlushCmd(struct cmd_syndesc *as, void *arock)
1345 {
1346     afs_int32 code;
1347     struct ViceIoctl blob;
1348     struct cmd_item *ti;
1349     int error = 0;
1350
1351     for (ti = as->parms[0].items; ti; ti = ti->next) {
1352         blob.in_size = blob.out_size = 0;
1353         code = pioctl(ti->data, VIOCFLUSH, &blob, 0);
1354         if (code) {
1355             if (errno == EMFILE) {
1356                 fprintf(stderr, "%s: Can't flush active file %s\n", pn,
1357                         ti->data);
1358             } else {
1359                 fprintf(stderr, "%s: Error flushing file ", pn);
1360                 perror(ti->data);
1361             }
1362             error = 1;
1363             continue;
1364         }
1365     }
1366     return error;
1367 }
1368
1369 /* all this command does is repackage its args and call SetVolCmd */
1370 static int
1371 SetQuotaCmd(struct cmd_syndesc *as, void *arock)
1372 {
1373     struct cmd_syndesc ts;
1374
1375     /* copy useful stuff from our command slot; we may later have to reorder */
1376     memcpy(&ts, as, sizeof(ts));        /* copy whole thing */
1377     return SetVolCmd(&ts, arock);
1378 }
1379
1380 static int
1381 SetVolCmd(struct cmd_syndesc *as, void *arock)
1382 {
1383     afs_int32 code;
1384     struct ViceIoctl blob;
1385     struct cmd_item *ti;
1386     struct VolumeStatus *status;
1387     char *offmsg, *input;
1388     int error = 0;
1389
1390     SetDotDefault(&as->parms[0].items);
1391     for (ti = as->parms[0].items; ti; ti = ti->next) {
1392         /* once per file */
1393         blob.out_size = MAXSIZE;
1394         blob.in_size = sizeof(*status) + 3;     /* for the three terminating nulls */
1395         blob.out = space;
1396         blob.in = space;
1397         status = (VolumeStatus *) space;
1398         status->MinQuota = status->MaxQuota = -1;
1399         offmsg = NULL;
1400         if (as->parms[1].items) {
1401             code = util_GetInt32(as->parms[1].items->data, &status->MaxQuota);
1402             if (code) {
1403                 fprintf(stderr, "%s: bad integer specified for quota.\n", pn);
1404                 error = 1;
1405                 continue;
1406             }
1407         }
1408         if (as->parms[2].items)
1409             offmsg = as->parms[2].items->data;
1410         input = (char *)status + sizeof(*status);
1411         *(input++) = '\0';      /* never set name: this call doesn't change vldb */
1412         if (offmsg) {
1413             if (strlen(offmsg) >= VMSGSIZE) {
1414                 fprintf(stderr,
1415                         "%s: message must be shorter than %d characters\n",
1416                         pn, VMSGSIZE);
1417                 error = 1;
1418                 continue;
1419             }
1420             strcpy(input, offmsg);
1421             blob.in_size += strlen(offmsg);
1422             input += strlen(offmsg) + 1;
1423         } else
1424             *(input++) = '\0';
1425         *(input++) = '\0';      /* Pad for old style volume "motd" */
1426         code = pioctl(ti->data, VIOCSETVOLSTAT, &blob, 1);
1427         if (code) {
1428             Die(errno, ti->data);
1429             error = 1;
1430         }
1431     }
1432     return error;
1433 }
1434
1435 /*
1436  * Why is VenusFid declared in the kernel-only section of afs.h,
1437  * if it's the exported interface of the cache manager?
1438  */
1439 struct VenusFid {
1440     afs_int32 Cell;
1441     AFSFid Fid;
1442 };
1443
1444 static int
1445 ExamineCmd(struct cmd_syndesc *as, void *arock)
1446 {
1447     afs_int32 code;
1448     struct ViceIoctl blob;
1449     struct cmd_item *ti;
1450     struct VolumeStatus *status;
1451     char *name, *offmsg;
1452     int error = 0;
1453
1454     SetDotDefault(&as->parms[0].items);
1455     for (ti = as->parms[0].items; ti; ti = ti->next) {
1456         struct VenusFid vfid;
1457
1458         /* once per file */
1459         blob.out_size = MAXSIZE;
1460         blob.in_size = 0;
1461         blob.out = space;
1462         code = pioctl(ti->data, VIOCGETVOLSTAT, &blob, 1);
1463         if (code) {
1464             Die(errno, ti->data);
1465             error = 1;
1466             continue;
1467         }
1468         status = (VolumeStatus *) space;
1469         name = (char *)status + sizeof(*status);
1470         offmsg = name + strlen(name) + 1;
1471
1472         blob.out_size = sizeof(struct VenusFid);
1473         blob.out = (char *) &vfid;
1474         if (0 == pioctl(ti->data, VIOCGETFID, &blob, 1)) {
1475             printf("File %s (%u.%u.%u) contained in volume %u\n",
1476                    ti->data, vfid.Fid.Volume, vfid.Fid.Vnode, vfid.Fid.Unique,
1477                    vfid.Fid.Volume);
1478         }
1479
1480         PrintStatus(status, name, offmsg);
1481     }
1482     return error;
1483 }
1484
1485 static int
1486 ListQuotaCmd(struct cmd_syndesc *as, void *arock)
1487 {
1488     afs_int32 code;
1489     struct ViceIoctl blob;
1490     struct cmd_item *ti;
1491     struct VolumeStatus *status;
1492     char *name;
1493     int error = 0;
1494
1495     printf("%-25s%-10s%-10s%-7s%-11s\n", "Volume Name", "     Quota",
1496            "      Used", " %Used", "  Partition");
1497     SetDotDefault(&as->parms[0].items);
1498     for (ti = as->parms[0].items; ti; ti = ti->next) {
1499         /* once per file */
1500         blob.out_size = MAXSIZE;
1501         blob.in_size = 0;
1502         blob.out = space;
1503         code = pioctl(ti->data, VIOCGETVOLSTAT, &blob, 1);
1504         if (code) {
1505             Die(errno, ti->data);
1506             error = 1;
1507             continue;
1508         }
1509         status = (VolumeStatus *) space;
1510         name = (char *)status + sizeof(*status);
1511         QuickPrintStatus(status, name);
1512     }
1513     return error;
1514 }
1515
1516 static int
1517 WhereIsCmd(struct cmd_syndesc *as, void *arock)
1518 {
1519     afs_int32 code;
1520     struct ViceIoctl blob;
1521     struct cmd_item *ti;
1522     int j;
1523     afs_int32 *hosts;
1524     char *tp;
1525     int error = 0;
1526
1527     SetDotDefault(&as->parms[0].items);
1528     for (ti = as->parms[0].items; ti; ti = ti->next) {
1529         /* once per file */
1530         blob.out_size = MAXSIZE;
1531         blob.in_size = 0;
1532         blob.out = space;
1533         memset(space, 0, sizeof(space));
1534         code = pioctl(ti->data, VIOCWHEREIS, &blob, 1);
1535         if (code) {
1536             Die(errno, ti->data);
1537             error = 1;
1538             continue;
1539         }
1540         hosts = (afs_int32 *) space;
1541         printf("File %s is on host%s ", ti->data,
1542                (hosts[0] && !hosts[1]) ? "" : "s");
1543         for (j = 0; j < MAXHOSTS; j++) {
1544             if (hosts[j] == 0)
1545                 break;
1546             tp = hostutil_GetNameByINet(hosts[j]);
1547             printf("%s ", tp);
1548         }
1549         printf("\n");
1550     }
1551     return error;
1552 }
1553
1554
1555 static int
1556 DiskFreeCmd(struct cmd_syndesc *as, void *arock)
1557 {
1558     afs_int32 code;
1559     struct ViceIoctl blob;
1560     struct cmd_item *ti;
1561     char *name;
1562     struct VolumeStatus *status;
1563     int error = 0;
1564
1565     printf("%-25s%-10s%-10s%-10s%-6s\n", "Volume Name", "    kbytes",
1566            "      used", "     avail", " %used");
1567     SetDotDefault(&as->parms[0].items);
1568     for (ti = as->parms[0].items; ti; ti = ti->next) {
1569         /* once per file */
1570         blob.out_size = MAXSIZE;
1571         blob.in_size = 0;
1572         blob.out = space;
1573         code = pioctl(ti->data, VIOCGETVOLSTAT, &blob, 1);
1574         if (code) {
1575             Die(errno, ti->data);
1576             error = 1;
1577             continue;
1578         }
1579         status = (VolumeStatus *) space;
1580         name = (char *)status + sizeof(*status);
1581         QuickPrintSpace(status, name);
1582     }
1583     return error;
1584 }
1585
1586 static int
1587 QuotaCmd(struct cmd_syndesc *as, void *arock)
1588 {
1589     afs_int32 code;
1590     struct ViceIoctl blob;
1591     struct cmd_item *ti;
1592     double quotaPct;
1593     struct VolumeStatus *status;
1594     int error = 0;
1595
1596     SetDotDefault(&as->parms[0].items);
1597     for (ti = as->parms[0].items; ti; ti = ti->next) {
1598         /* once per file */
1599         blob.out_size = MAXSIZE;
1600         blob.in_size = 0;
1601         blob.out = space;
1602         code = pioctl(ti->data, VIOCGETVOLSTAT, &blob, 1);
1603         if (code) {
1604             Die(errno, ti->data);
1605             error = 1;
1606             continue;
1607         }
1608         status = (VolumeStatus *) space;
1609         if (status->MaxQuota)
1610             quotaPct =
1611                 ((((double)status->BlocksInUse) / status->MaxQuota) * 100.0);
1612         else
1613             quotaPct = 0.0;
1614         printf("%2.0f%% of quota used.\n", quotaPct);
1615     }
1616     return error;
1617 }
1618
1619 static int
1620 ListMountCmd(struct cmd_syndesc *as, void *arock)
1621 {
1622     afs_int32 code;
1623     struct ViceIoctl blob;
1624     struct cmd_item *ti;
1625     char orig_name[1024];       /*Original name, may be modified */
1626     char true_name[1024];       /*``True'' dirname (e.g., symlink target) */
1627     char parent_dir[1024];      /*Parent directory of true name */
1628     char *last_component;       /*Last component of true name */
1629     struct stat statbuff;       /*Buffer for status info */
1630     int link_chars_read;        /*Num chars read in readlink() */
1631     int thru_symlink;           /*Did we get to a mount point via a symlink? */
1632     int error = 0;
1633
1634     for (ti = as->parms[0].items; ti; ti = ti->next) {
1635         /* once per file */
1636         thru_symlink = 0;
1637         sprintf(orig_name, "%s%s", (ti->data[0] == '/') ? "" : "./",
1638                 ti->data);
1639
1640         if (lstat(orig_name, &statbuff) < 0) {
1641             /* if lstat fails, we should still try the pioctl, since it
1642              * may work (for example, lstat will fail, but pioctl will
1643              * work if the volume of offline (returning ENODEV). */
1644             statbuff.st_mode = S_IFDIR; /* lie like pros */
1645         }
1646
1647         /*
1648          * The lstat succeeded.  If the given file is a symlink, substitute
1649          * the file name with the link name.
1650          */
1651         if ((statbuff.st_mode & S_IFMT) == S_IFLNK) {
1652             thru_symlink = 1;
1653             /*
1654              * Read name of resolved file.
1655              */
1656             link_chars_read = readlink(orig_name, true_name, 1024);
1657             if (link_chars_read <= 0) {
1658                 fprintf(stderr,
1659                         "%s: Can't read target name for '%s' symbolic link!\n",
1660                         pn, orig_name);
1661                 error = 1;
1662                 continue;
1663             }
1664
1665             /*
1666              * Add a trailing null to what was read, bump the length.
1667              */
1668             true_name[link_chars_read++] = 0;
1669
1670             /*
1671              * If the symlink is an absolute pathname, we're fine.  Otherwise, we
1672              * have to create a full pathname using the original name and the
1673              * relative symlink name.  Find the rightmost slash in the original
1674              * name (we know there is one) and splice in the symlink value.
1675              */
1676             if (true_name[0] != '/') {
1677                 last_component = (char *)strrchr(orig_name, '/');
1678                 strcpy(++last_component, true_name);
1679                 strcpy(true_name, orig_name);
1680             }
1681         } else
1682             strcpy(true_name, orig_name);
1683
1684         /*
1685          * Find rightmost slash, if any.
1686          */
1687         last_component = (char *)strrchr(true_name, '/');
1688         if (last_component) {
1689             /*
1690              * Found it.  Designate everything before it as the parent directory,
1691              * everything after it as the final component.
1692              */
1693             strncpy(parent_dir, true_name, last_component - true_name);
1694             parent_dir[last_component - true_name] = 0;
1695             last_component++;   /*Skip the slash */
1696         } else {
1697             /*
1698              * No slash appears in the given file name.  Set parent_dir to the current
1699              * directory, and the last component as the given name.
1700              */
1701             strcpy(parent_dir, ".");
1702             last_component = true_name;
1703         }
1704
1705         if (strcmp(last_component, ".") == 0
1706             || strcmp(last_component, "..") == 0) {
1707             fprintf(stderr,
1708                     "%s: you may not use '.' or '..' as the last component\n",
1709                     pn);
1710             fprintf(stderr, "%s: of a name in the 'fs lsmount' command.\n",
1711                     pn);
1712             error = 1;
1713             continue;
1714         }
1715
1716         blob.in = last_component;
1717         blob.in_size = strlen(last_component) + 1;
1718         blob.out_size = MAXSIZE;
1719         blob.out = space;
1720         memset(space, 0, MAXSIZE);
1721
1722         code = pioctl(parent_dir, VIOC_AFS_STAT_MT_PT, &blob, 1);
1723
1724         if (code == 0) {
1725             printf("'%s' is a %smount point for volume '%s'\n", ti->data,
1726                    (thru_symlink ? "symbolic link, leading to a " : ""),
1727                    space);
1728         } else {
1729             if (errno == EINVAL) {
1730                 fprintf(stderr, "'%s' is not a mount point.\n", ti->data);
1731             } else {
1732                 Die(errno, (ti->data ? ti->data : parent_dir));
1733             }
1734             error = 1;
1735         }
1736     }
1737     return error;
1738 }
1739
1740 static int
1741 MakeMountCmd(struct cmd_syndesc *as, void *arock)
1742 {
1743     afs_int32 code;
1744     char *cellName, *volName, *tmpName;
1745     struct afsconf_cell info;
1746     struct vldbentry vldbEntry;
1747     struct ViceIoctl blob;
1748
1749 /*
1750
1751 defect #3069
1752
1753     if (as->parms[5].items && !as->parms[2].items) {
1754         fprintf(stderr, "%s: must provide cell when creating cellular mount point.\n", pn);
1755         return 1;
1756     }
1757 */
1758
1759     if (as->parms[2].items)     /* cell name specified */
1760         cellName = as->parms[2].items->data;
1761     else
1762         cellName = NULL;
1763     volName = as->parms[1].items->data;
1764
1765     if (strlen(volName) >= 64) {
1766         fprintf(stderr,
1767                 "%s: volume name too long (length must be < 64 characters)\n",
1768                 pn);
1769         return 1;
1770     }
1771
1772     /* Check for a cellname in the volume specification, and complain
1773      * if it doesn't match what was specified with -cell */
1774     if (tmpName = strchr(volName, ':')) {
1775         *tmpName = '\0';
1776         if (cellName) {
1777             if (strcasecmp(cellName, volName)) {
1778                 fprintf(stderr, "%s: cellnames do not match.\n", pn);
1779                 return 1;
1780             }
1781         }
1782         cellName = volName;
1783         volName = ++tmpName;
1784     }
1785
1786     if (!InAFS(Parent(as->parms[0].items->data))) {
1787         fprintf(stderr,
1788                 "%s: mount points must be created within the AFS file system\n",
1789                 pn);
1790         return 1;
1791     }
1792
1793     if (!cellName) {
1794         blob.in_size = 0;
1795         blob.out_size = MAXSIZE;
1796         blob.out = space;
1797         code =
1798             pioctl(Parent(as->parms[0].items->data), VIOC_FILE_CELL_NAME,
1799                    &blob, 1);
1800     }
1801
1802     code = GetCellName(cellName ? cellName : space, &info);
1803     if (code) {
1804         return 1;
1805     }
1806     if (!(as->parms[4].items)) {
1807         /* not fast, check which cell the mountpoint is being created in */
1808         /* not fast, check name with VLDB */
1809         code = VLDBInit(1, &info);
1810         if (code == 0) {
1811             /* make the check.  Don't complain if there are problems with init */
1812             code =
1813                 ubik_VL_GetEntryByNameO(uclient, 0, volName,
1814                           &vldbEntry);
1815             if (code == VL_NOENT) {
1816                 fprintf(stderr,
1817                         "%s: warning, volume %s does not exist in cell %s.\n",
1818                         pn, volName, cellName ? cellName : space);
1819             }
1820         }
1821     }
1822
1823     if (as->parms[3].items)     /* if -rw specified */
1824         strcpy(space, "%");
1825     else
1826         strcpy(space, "#");
1827     if (cellName) {
1828         /* cellular mount point, prepend cell prefix */
1829         strcat(space, info.name);
1830         strcat(space, ":");
1831     }
1832     strcat(space, volName);     /* append volume name */
1833     strcat(space, ".");         /* stupid convention; these end with a period */
1834     code = symlink(space, as->parms[0].items->data);
1835     if (code) {
1836         Die(errno, as->parms[0].items->data);
1837         return 1;
1838     }
1839     return 0;
1840 }
1841
1842 /*
1843  * Delete AFS mount points.  Variables are used as follows:
1844  *      tbuffer: Set to point to the null-terminated directory name of the mount point
1845  *          (or ``.'' if none is provided)
1846  *      tp: Set to point to the actual name of the mount point to nuke.
1847  */
1848 static int
1849 RemoveMountCmd(struct cmd_syndesc *as, void *arock)
1850 {
1851     afs_int32 code = 0;
1852     struct ViceIoctl blob;
1853     struct cmd_item *ti;
1854     char tbuffer[1024];
1855     char lsbuffer[1024];
1856     char *tp;
1857     int error = 0;
1858
1859     for (ti = as->parms[0].items; ti; ti = ti->next) {
1860         /* once per file */
1861         tp = (char *)strrchr(ti->data, '/');
1862         if (tp) {
1863             strncpy(tbuffer, ti->data, code = tp - ti->data);   /* the dir name */
1864             tbuffer[code] = 0;
1865             tp++;               /* skip the slash */
1866         } else {
1867             strcpy(tbuffer, ".");
1868             tp = ti->data;
1869         }
1870         blob.in = tp;
1871         blob.in_size = strlen(tp) + 1;
1872         blob.out = lsbuffer;
1873         blob.out_size = sizeof(lsbuffer);
1874         code = pioctl(tbuffer, VIOC_AFS_STAT_MT_PT, &blob, 0);
1875         if (code) {
1876             if (errno == EINVAL) {
1877                 fprintf(stderr, "%s: '%s' is not a mount point.\n", pn,
1878                         ti->data);
1879             } else {
1880                 Die(errno, ti->data);
1881             }
1882             error = 1;
1883             continue;           /* don't bother trying */
1884         }
1885         blob.out_size = 0;
1886         blob.in = tp;
1887         blob.in_size = strlen(tp) + 1;
1888         code = pioctl(tbuffer, VIOC_AFS_DELETE_MT_PT, &blob, 0);
1889         if (code) {
1890             Die(errno, ti->data);
1891             error = 1;
1892         }
1893     }
1894     return error;
1895 }
1896
1897 /*
1898 */
1899
1900 static int
1901 CheckServersCmd(struct cmd_syndesc *as, void *arock)
1902 {
1903     afs_int32 code;
1904     struct ViceIoctl blob;
1905     afs_int32 j;
1906     afs_int32 temp;
1907     char *tp;
1908     char tbuffer[128];
1909     afs_int32 interval;
1910     struct afsconf_cell info;
1911     struct chservinfo checkserv;
1912
1913     memset(&checkserv, 0, sizeof(struct chservinfo));
1914     blob.in_size = sizeof(struct chservinfo);
1915     blob.in = (caddr_t) & checkserv;
1916
1917     blob.out_size = MAXSIZE;
1918     blob.out = space;
1919     memset(space, 0, sizeof(afs_int32));        /* so we assure zero when nothing is copied back */
1920
1921     /* prepare flags for checkservers command */
1922     temp = 2;                   /* default to checking local cell only */
1923     if (as->parms[2].items)
1924         temp |= 1;              /* set fast flag */
1925     if (as->parms[1].items)
1926         temp &= ~2;             /* turn off local cell check */
1927
1928     checkserv.magic = 0x12345678;       /* XXX */
1929     checkserv.tflags = temp;
1930
1931     /* now copy in optional cell name, if specified */
1932     if (as->parms[0].items) {
1933         code = GetCellName(as->parms[0].items->data, &info);
1934         if (code) {
1935             return 1;
1936         }
1937         strcpy(checkserv.tbuffer, info.name);
1938         checkserv.tsize = strlen(info.name) + 1;
1939     } else {
1940         strcpy(checkserv.tbuffer, "\0");
1941         checkserv.tsize = 0;
1942     }
1943
1944     if (as->parms[3].items) {
1945         checkserv.tinterval = atol(as->parms[3].items->data);
1946
1947         /* sanity check */
1948         if (checkserv.tinterval < 0) {
1949             printf
1950                 ("Warning: The negative -interval is ignored; treated as an inquiry\n");
1951             checkserv.tinterval = 0;
1952         } else if (checkserv.tinterval > 600) {
1953             printf
1954                 ("Warning: The maximum -interval value is 10 mins (600 secs)\n");
1955             checkserv.tinterval = 600;  /* 10 min max interval */
1956         }
1957     } else {
1958         checkserv.tinterval = -1;       /* don't change current interval */
1959     }
1960
1961     code = pioctl(0, VIOCCKSERV, &blob, 1);
1962     if (code) {
1963         if ((errno == EACCES) && (checkserv.tinterval > 0)) {
1964             printf("Must be root to change -interval\n");
1965             return 1;
1966         }
1967         Die(errno, 0);
1968         return 1;
1969     }
1970     memcpy(&temp, space, sizeof(afs_int32));
1971     if (checkserv.tinterval >= 0) {
1972         if (checkserv.tinterval > 0)
1973             printf
1974                 ("The new down server probe interval (%d secs) is now in effect (old interval was %d secs)\n",
1975                  checkserv.tinterval, temp);
1976         else
1977             printf("The current down server probe interval is %d secs\n",
1978                    temp);
1979         return 0;
1980     }
1981     if (temp == 0) {
1982         printf("All servers are running.\n");
1983     } else {
1984         printf
1985             ("These servers unavailable due to network or server problems: ");
1986         for (j = 0;; j++) {
1987             memcpy(&temp, space + j * sizeof(afs_int32), sizeof(afs_int32));
1988             if (temp == 0)
1989                 break;
1990             tp = hostutil_GetNameByINet(temp);
1991             printf(" %s", tp);
1992         }
1993         printf(".\n");
1994         code = 1;               /* XXX */
1995     }
1996     return code;
1997 }
1998
1999 static int
2000 MessagesCmd(struct cmd_syndesc *as, void *arock)
2001 {
2002     afs_int32 code = 0;
2003     struct ViceIoctl blob;
2004     afs_int32 j;
2005     afs_int32 temp;
2006     struct gaginfo gagflags;
2007     struct cmd_item *show;
2008
2009     memset(&gagflags, 0, sizeof(struct gaginfo));
2010     blob.in_size = sizeof(struct gaginfo);
2011     blob.in = (caddr_t) & gagflags;
2012     blob.out_size = MAXSIZE;
2013     blob.out = space;
2014     memset(space, 0, sizeof(afs_int32));        /* so we assure zero when nothing is copied back */
2015
2016     if (show = as->parms[0].items) {
2017         if (!strcasecmp(show->data, "user"))
2018             gagflags.showflags |= GAGUSER;
2019         else if (!strcasecmp(show->data, "console"))
2020             gagflags.showflags |= GAGCONSOLE;
2021         else if (!strcasecmp(show->data, "all"))
2022             gagflags.showflags |= GAGCONSOLE | GAGUSER;
2023         else if (!strcasecmp(show->data, "none"))
2024             /* do nothing */ ;
2025         else {
2026             fprintf(stderr,
2027                     "unrecognized flag %s: must be in {user,console,all,none}\n",
2028                     show->data);
2029             code = EINVAL;
2030         }
2031     }
2032
2033     if (code)
2034         return 1;
2035
2036     code = pioctl(0, VIOC_GAG, &blob, 1);
2037     if (code) {
2038         Die(errno, 0);
2039         return 1;
2040     }
2041
2042     return 0;
2043 }
2044
2045 static int
2046 CheckVolumesCmd(struct cmd_syndesc *as, void *arock)
2047 {
2048     afs_int32 code;
2049     struct ViceIoctl blob;
2050
2051     blob.in_size = 0;
2052     blob.out_size = 0;
2053     code = pioctl(0, VIOCCKBACK, &blob, 1);
2054     if (code) {
2055         Die(errno, 0);
2056         return 1;
2057     }
2058
2059     printf("All volumeID/name mappings checked.\n");
2060     return 0;
2061 }
2062
2063 static int
2064 PreCacheCmd(struct cmd_syndesc *as, char *arock)
2065 {
2066     afs_int32 code;
2067     struct ViceIoctl blob;
2068     afs_int32 temp;
2069     
2070     if (!as->parms[0].items && !as->parms[1].items) {
2071         fprintf(stderr, "%s: syntax error in precache cmd.\n", pn);
2072         return 1;
2073     }
2074     if (as->parms[0].items) {
2075         code = util_GetInt32(as->parms[0].items->data, &temp);
2076         if (code) {
2077             fprintf(stderr, "%s: bad integer specified for precache size.\n",
2078                     pn);
2079             return 1;
2080         }
2081     } else
2082         temp = 0;
2083     blob.in = (char *)&temp;
2084     blob.in_size = sizeof(afs_int32);
2085     blob.out_size = 0;
2086     code = pioctl(0, VIOCPRECACHE, &blob, 1);
2087     if (code) {
2088         Die(errno, NULL);
2089         return 1;
2090     }
2091     
2092     printf("New precache size set.\n");
2093     return 0;
2094 }
2095
2096 static int
2097 SetCacheSizeCmd(struct cmd_syndesc *as, void *arock)
2098 {
2099     afs_int32 code;
2100     struct ViceIoctl blob;
2101     afs_int32 temp;
2102
2103     if (!as->parms[0].items && !as->parms[1].items) {
2104         fprintf(stderr, "%s: syntax error in setcachesize cmd.\n", pn);
2105         return 1;
2106     }
2107     if (as->parms[0].items) {
2108         code = util_GetInt32(as->parms[0].items->data, &temp);
2109         if (code) {
2110             fprintf(stderr, "%s: bad integer specified for cache size.\n",
2111                     pn);
2112             return 1;
2113         }
2114     } else
2115         temp = 0;
2116     blob.in = (char *)&temp;
2117     blob.in_size = sizeof(afs_int32);
2118     blob.out_size = 0;
2119     code = pioctl(0, VIOCSETCACHESIZE, &blob, 1);
2120     if (code) {
2121         if (errno == EROFS) {
2122             printf
2123                 ("'fs setcache' not allowed on memory cache based cache managers.\n");
2124         } else {
2125             Die(errno, NULL);
2126         }
2127         return 1;
2128     }
2129
2130     printf("New cache size set.\n");
2131     return 0;
2132 }
2133
2134 #define MAXGCSIZE       16
2135 static int
2136 GetCacheParmsCmd(struct cmd_syndesc *as, void *arock)
2137 {
2138     afs_int32 code, filesUsed;
2139     struct ViceIoctl blob;
2140     afs_int32 parms[MAXGCSIZE];
2141     double percentFiles, percentBlocks;
2142     afs_int32 flags = 0;
2143
2144     if (as->parms[0].items){ /* -files */
2145         flags = 1;
2146     } else if (as->parms[1].items){ /* -excessive */
2147         flags = 2;
2148     } else {
2149         flags = 0;
2150     }
2151
2152     memset(parms, '\0', sizeof parms);  /* avoid Purify UMR error */
2153     if (flags){
2154         blob.in = (char *)&flags;
2155         blob.in_size = sizeof(afs_int32);
2156     } else {    /* be backward compatible */
2157         blob.in = NULL;
2158         blob.in_size = 0;
2159     }
2160     blob.out_size = sizeof(parms);
2161     blob.out = (char *)parms;
2162     code = pioctl(0, VIOCGETCACHEPARMS, &blob, 1);
2163     if (code) {
2164         Die(errno, NULL);
2165         return 1;
2166     }
2167
2168     if (!flags){
2169         printf("AFS using %d of the cache's available %d 1K byte blocks.\n",
2170                 parms[1], parms[0]);
2171         if (parms[1] > parms[0])
2172                 printf("[Cache guideline temporarily deliberately exceeded; it will be adjusted down but you may wish to increase the cache size.]\n");
2173         return 0;
2174     }
2175
2176     percentBlocks = ((double)parms[1]/parms[0]) * 100;
2177     printf("AFS using %5.0f%% of cache blocks (%d of %d 1k blocks)\n",
2178            percentBlocks, parms[1], parms[0]);
2179
2180     if (parms[2] == 0)
2181         return 0;
2182
2183     filesUsed = parms[2] - parms[3];
2184     percentFiles = ((double)filesUsed/parms[2]) * 100;
2185     printf("          %5.0f%% of the cache files (%d of %d files)\n",
2186             percentFiles, filesUsed, parms[2]);
2187     if (flags == 2){
2188         printf("        afs_cacheFiles: %10d\n", parms[2]);
2189         printf("        IFFree:         %10d\n", parms[3]); 
2190         printf("        IFEverUsed:     %10d\n", parms[4]); 
2191         printf("        IFDataMod:      %10d\n", parms[5]); 
2192         printf("        IFDirtyPages:   %10d\n", parms[6]);
2193         printf("        IFAnyPages:     %10d\n", parms[7]); 
2194         printf("        IFDiscarded:    %10d\n", parms[8]);
2195         printf("        DCentries:  %10d\n", parms[9]);
2196         printf("          0k-   4K: %10d\n", parms[10]); 
2197         printf("          4k-  16k: %10d\n", parms[11]); 
2198         printf("         16k-  64k: %10d\n", parms[12]); 
2199         printf("         64k- 256k: %10d\n", parms[13]); 
2200         printf("        256k-   1M: %10d\n", parms[14]); 
2201         printf("              >=1M: %10d\n", parms[15]); 
2202     }
2203
2204     if (percentBlocks > 90)
2205         printf("[cache size usage over 90%, consider increasing cache size]\n");
2206     if (percentFiles > 90)
2207         printf("[cache file usage over 90%, consider increasing '-files' argument to afsd]\n");
2208          
2209     return 0;
2210 }
2211
2212 static int
2213 ListCellsCmd(struct cmd_syndesc *as, void *arock)
2214 {
2215     afs_int32 code;
2216     afs_int32 i, j;
2217     char *tcp, *tp;
2218     afs_int32 clear;
2219     struct ViceIoctl blob;
2220     int resolve;
2221
2222     resolve = !(as->parms[0].items);    /* -numeric */
2223
2224     for (i = 0;; i++) {
2225         tp = space;
2226         memcpy(tp, &i, sizeof(afs_int32));
2227         blob.out_size = MAXSIZE;
2228         blob.in_size = sizeof(afs_int32);
2229         blob.in = space;
2230         blob.out = space;
2231         code = pioctl(0, VIOCGETCELL, &blob, 1);
2232         if (code < 0) {
2233             if (errno == EDOM)
2234                 break;          /* done with the list */
2235             Die(errno, 0);
2236             return 1;
2237         }
2238         tp = space;
2239         printf("Cell %s on hosts", tp + MAXCELLHOSTS * sizeof(afs_int32));
2240         for (j = 0; j < MAXCELLHOSTS; j++) {
2241             afs_int32 addr;
2242             char *name, tbuffer[20];
2243
2244             memcpy(&addr, tp + j * sizeof(afs_int32), sizeof(afs_int32));
2245             if (addr == 0)
2246                 break;
2247
2248             if (resolve) {
2249                 name = hostutil_GetNameByINet(addr);
2250             } else {
2251                 addr = ntohl(addr);
2252                 sprintf(tbuffer, "%d.%d.%d.%d", (addr >> 24) & 0xff,
2253                         (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff);
2254                 name = tbuffer;
2255             }
2256             printf(" %s", name);
2257         }
2258         printf(".\n");
2259     }
2260     return 0;
2261 }
2262
2263 static int
2264 ListAliasesCmd(struct cmd_syndesc *as, void *arock)
2265 {
2266     afs_int32 code, i;
2267     char *tp, *aliasName, *realName;
2268     struct ViceIoctl blob;
2269
2270     for (i = 0;; i++) {
2271         tp = space;
2272         memcpy(tp, &i, sizeof(afs_int32));
2273         blob.out_size = MAXSIZE;
2274         blob.in_size = sizeof(afs_int32);
2275         blob.in = space;
2276         blob.out = space;
2277         code = pioctl(0, VIOC_GETALIAS, &blob, 1);
2278         if (code < 0) {
2279             if (errno == EDOM)
2280                 break;          /* done with the list */
2281             Die(errno, 0);
2282             return 1;
2283         }
2284         tp = space;
2285         aliasName = tp;
2286         tp += strlen(aliasName) + 1;
2287         realName = tp;
2288         printf("Alias %s for cell %s\n", aliasName, realName);
2289     }
2290     return 0;
2291 }
2292
2293 static int
2294 CallBackRxConnCmd(struct cmd_syndesc *as, void *arock)
2295 {
2296     afs_int32 code;
2297     struct ViceIoctl blob;
2298     struct cmd_item *ti;
2299     afs_int32 hostAddr;
2300     struct hostent *thp;
2301     char *tp;
2302     int setp;
2303     
2304     ti = as->parms[0].items;
2305     setp = 1;
2306     if (ti) {
2307         thp = hostutil_GetHostByName(ti->data);
2308         if (!thp) {
2309             fprintf(stderr, "host %s not found in host table.\n", ti->data);
2310             return 1;
2311         }
2312         else memcpy(&hostAddr, thp->h_addr, sizeof(afs_int32));
2313     } else {
2314         hostAddr = 0;   /* means don't set host */
2315         setp = 0;       /* aren't setting host */
2316     }
2317     
2318     /* now do operation */
2319     blob.in_size = sizeof(afs_int32);
2320     blob.out_size = sizeof(afs_int32);
2321     blob.in = (char *) &hostAddr;
2322     blob.out = (char *) &hostAddr;
2323     
2324     code = pioctl(0, VIOC_CBADDR, &blob, 1);
2325     if (code < 0) {
2326         Die(errno, 0);
2327         return 1;
2328     }
2329     return 0;
2330 }
2331
2332 static int
2333 NukeNFSCredsCmd(struct cmd_syndesc *as, void *arock)
2334 {
2335     afs_int32 code;
2336     struct ViceIoctl blob;
2337     struct cmd_item *ti;
2338     afs_int32 hostAddr;
2339     struct hostent *thp;
2340     
2341     ti = as->parms[0].items;
2342     thp = hostutil_GetHostByName(ti->data);
2343     if (!thp) {
2344         fprintf(stderr, "host %s not found in host table.\n", ti->data);
2345         return 1;
2346     }
2347     else memcpy(&hostAddr, thp->h_addr, sizeof(afs_int32));
2348     
2349     /* now do operation */
2350     blob.in_size = sizeof(afs_int32);
2351     blob.out_size = sizeof(afs_int32);
2352     blob.in = (char *) &hostAddr;
2353     blob.out = (char *) &hostAddr;
2354     
2355     code = pioctl(0, VIOC_NFS_NUKE_CREDS, &blob, 1);
2356     if (code < 0) {
2357         Die(errno, 0);
2358         return 1;
2359     }
2360     return 0;
2361 }
2362
2363 static int
2364 NewCellCmd(struct cmd_syndesc *as, void *arock)
2365 {
2366     afs_int32 code, linkedstate = 0, size = 0, *lp;
2367     struct ViceIoctl blob;
2368     struct cmd_item *ti;
2369     char *tp, *cellname = 0;
2370     struct hostent *thp;
2371     afs_int32 fsport = 0, vlport = 0;
2372     afs_int32 magic, scount;    /* Number of servers to pass in pioctl call */
2373
2374     /* Yuck!
2375      * With the NEWCELL pioctl call, 3.4 clients take an array of
2376      * MAXHOSTS (13) servers while 3.5 clients take an array of
2377      * MAXCELLHOSTS (8) servers. To determine which we are talking to,
2378      * do a GETCELL pioctl and pass it a magic number. If an array of
2379      * 8 comes back, its a 3.5 client. If not, its a 3.4 client.
2380      * If we get back EDOM, there are no cells in the kernel yet,
2381      * and we'll assume a 3.5 client.
2382      */
2383     tp = space;
2384     lp = (afs_int32 *) tp;
2385     *lp++ = 0;                  /* first cell entry */
2386     *lp = 0x12345678;           /* magic */
2387     blob.out_size = MAXSIZE;
2388     blob.in_size = sizeof(afs_int32) + sizeof(afs_int32);
2389     blob.in = space;
2390     blob.out = space;
2391     code = pioctl(0, VIOCGETCELL, &blob, 1);
2392     if (code < 0 && errno != EDOM) {
2393         Die(errno, 0);
2394         return 1;
2395     }
2396     if (code < 1 && errno == EDOM) {
2397         scount = MAXHOSTS;
2398     } else {
2399         tp = space;
2400         cellname = tp + MAXCELLHOSTS * sizeof(afs_int32);
2401         scount = ((cellname[0] != '\0') ? MAXCELLHOSTS : MAXHOSTS);
2402     }
2403
2404     /* Now setup and do the NEWCELL pioctl call */
2405     memset(space, 0, (scount + 1) * sizeof(afs_int32));
2406     tp = space;
2407     lp = (afs_int32 *) tp;
2408     *lp++ = 0x12345678;
2409     tp += sizeof(afs_int32);
2410     for (ti = as->parms[1].items; ti; ti = ti->next) {
2411         thp = hostutil_GetHostByName(ti->data);
2412         if (!thp) {
2413             fprintf(stderr,
2414                     "%s: Host %s not found in host table, skipping it.\n", pn,
2415                     ti->data);
2416         } else {
2417             memcpy(tp, thp->h_addr, sizeof(afs_int32));
2418             tp += sizeof(afs_int32);
2419         }
2420     }
2421     if (as->parms[2].items) {
2422         /*
2423          * Link the cell, for the purposes of volume location, to the specified
2424          * cell.
2425          */
2426         cellname = as->parms[2].items->data;
2427         linkedstate = 1;
2428     }
2429 #ifdef FS_ENABLE_SERVER_DEBUG_PORTS
2430     if (as->parms[3].items) {
2431         code = util_GetInt32(as->parms[3].items->data, &vlport);
2432         if (code) {
2433             fprintf(stderr,
2434                     "%s: bad integer specified for the fileserver port.\n",
2435                     pn);
2436             return 1;
2437         }
2438     }
2439     if (as->parms[4].items) {
2440         code = util_GetInt32(as->parms[4].items->data, &fsport);
2441         if (code) {
2442             fprintf(stderr,
2443                     "%s: bad integer specified for the vldb server port.\n",
2444                     pn);
2445             return 1;
2446         }
2447     }
2448 #endif
2449     tp = (char *)(space + (scount + 1) * sizeof(afs_int32));
2450     lp = (afs_int32 *) tp;
2451     *lp++ = fsport;
2452     *lp++ = vlport;
2453     *lp = linkedstate;
2454     strcpy(space + ((scount + 4) * sizeof(afs_int32)),
2455            as->parms[0].items->data);
2456     size = ((scount + 4) * sizeof(afs_int32))
2457         + strlen(as->parms[0].items->data)
2458         + 1 /* for null */ ;
2459     tp = (char *)(space + size);
2460     if (linkedstate) {
2461         strcpy(tp, cellname);
2462         size += strlen(cellname) + 1;
2463     }
2464     blob.in_size = size;
2465     blob.in = space;
2466     blob.out_size = 0;
2467     code = pioctl(0, VIOCNEWCELL, &blob, 1);
2468     if (code < 0) {
2469         Die(errno, 0);
2470         return 1;
2471     }
2472     return 0;
2473 }
2474
2475 static int
2476 NewAliasCmd(struct cmd_syndesc *as, void *arock)
2477 {
2478     afs_int32 code;
2479     struct ViceIoctl blob;
2480     char *tp;
2481     char *aliasName, *realName;
2482
2483     /* Now setup and do the NEWCELL pioctl call */
2484     aliasName = as->parms[0].items->data;
2485     realName = as->parms[1].items->data;
2486     tp = space;
2487     strcpy(tp, aliasName);
2488     tp += strlen(aliasName) + 1;
2489     strcpy(tp, realName);
2490     tp += strlen(realName) + 1;
2491
2492     blob.in_size = tp - space;
2493     blob.in = space;
2494     blob.out_size = 0;
2495     blob.out = space;
2496     code = pioctl(0, VIOC_NEWALIAS, &blob, 1);
2497     if (code < 0) {
2498         if (errno == EEXIST) {
2499             fprintf(stderr,
2500                     "%s: cell name `%s' in use by an existing cell.\n", pn,
2501                     aliasName);
2502         } else {
2503             Die(errno, 0);
2504         }
2505         return 1;
2506     }
2507     return 0;
2508 }
2509
2510 static int
2511 WhichCellCmd(struct cmd_syndesc *as, void *arock)
2512 {
2513     afs_int32 code;
2514     struct cmd_item *ti;
2515     int error = 0;
2516     char cell[MAXCELLCHARS];
2517
2518     SetDotDefault(&as->parms[0].items);
2519     for (ti = as->parms[0].items; ti; ti = ti->next) {
2520         code = GetCell(ti->data, cell);
2521         if (code) {
2522             if (errno == ENOENT)
2523                 fprintf(stderr, "%s: no such cell as '%s'\n", pn, ti->data);
2524             else
2525                 Die(errno, ti->data);
2526             error = 1;
2527             continue;
2528         }
2529
2530         printf("File %s lives in cell '%s'\n", ti->data, cell);
2531     }
2532     return error;
2533 }
2534
2535 static int
2536 WSCellCmd(struct cmd_syndesc *as, void *arock)
2537 {
2538     afs_int32 code;
2539     struct ViceIoctl blob;
2540
2541     blob.in_size = 0;
2542     blob.in = NULL;
2543     blob.out_size = MAXSIZE;
2544     blob.out = space;
2545
2546     code = pioctl(NULL, VIOC_GET_WS_CELL, &blob, 1);
2547     if (code) {
2548         Die(errno, NULL);
2549         return 1;
2550     }
2551
2552     printf("This workstation belongs to cell '%s'\n", space);
2553     return 0;
2554 }
2555
2556 /*
2557 static PrimaryCellCmd(as)
2558     struct cmd_syndesc *as;
2559 {
2560     fprintf(stderr, "This command is obsolete, as is the concept of a primary token.\n");
2561     return 0;
2562 }
2563 */
2564
2565 static int
2566 MonitorCmd(struct cmd_syndesc *as, void *arock)
2567 {
2568     afs_int32 code;
2569     struct ViceIoctl blob;
2570     struct cmd_item *ti;
2571     afs_int32 hostAddr;
2572     struct hostent *thp;
2573     char *tp;
2574     int setp;
2575
2576     ti = as->parms[0].items;
2577     setp = 1;
2578     if (ti) {
2579         /* set the host */
2580         if (!strcmp(ti->data, "off"))
2581             hostAddr = 0xffffffff;
2582         else {
2583             thp = hostutil_GetHostByName(ti->data);
2584             if (!thp) {
2585                 if (!strcmp(ti->data, "localhost")) {
2586                     fprintf(stderr,
2587                             "localhost not in host table, assuming 127.0.0.1\n");
2588                     hostAddr = htonl(0x7f000001);
2589                 } else {
2590                     fprintf(stderr, "host %s not found in host table.\n",
2591                             ti->data);
2592                     return 1;
2593                 }
2594             } else
2595                 memcpy(&hostAddr, thp->h_addr, sizeof(afs_int32));
2596         }
2597     } else {
2598         hostAddr = 0;           /* means don't set host */
2599         setp = 0;               /* aren't setting host */
2600     }
2601
2602     /* now do operation */
2603     blob.in_size = sizeof(afs_int32);
2604     blob.out_size = sizeof(afs_int32);
2605     blob.in = (char *)&hostAddr;
2606     blob.out = (char *)&hostAddr;
2607     code = pioctl(0, VIOC_AFS_MARINER_HOST, &blob, 1);
2608     if (code) {
2609         Die(errno, 0);
2610         return 1;
2611     }
2612     if (setp) {
2613         printf("%s: new monitor host set.\n", pn);
2614     } else {
2615         /* now decode old address */
2616         if (hostAddr == 0xffffffff) {
2617             printf("Cache monitoring is currently disabled.\n");
2618         } else {
2619             tp = hostutil_GetNameByINet(hostAddr);
2620             printf("Using host %s for monitor services.\n", tp);
2621         }
2622     }
2623     return 0;
2624 }
2625
2626 static int
2627 SysNameCmd(struct cmd_syndesc *as, void *arock)
2628 {
2629     afs_int32 code;
2630     struct ViceIoctl blob;
2631     struct cmd_item *ti;
2632     char *input = space;
2633     afs_int32 setp = 0;
2634
2635     ti = as->parms[0].items;
2636     blob.in = space;
2637     blob.out = space;
2638     blob.out_size = MAXSIZE;
2639     blob.in_size = sizeof(afs_int32);
2640     input += sizeof(afs_int32);
2641     for (; ti; ti = ti->next) {
2642         setp++;
2643         blob.in_size += strlen(ti->data) + 1;
2644         if (blob.in_size > MAXSIZE) {
2645             fprintf(stderr, "%s: sysname%s too long.\n", pn,
2646                     setp > 1 ? "s" : "");
2647             return 1;
2648         }
2649         strcpy(input, ti->data);
2650         input += strlen(ti->data);
2651         *(input++) = '\0';
2652     }
2653     memcpy(space, &setp, sizeof(afs_int32));
2654     code = pioctl(0, VIOC_AFS_SYSNAME, &blob, 1);
2655     if (code) {
2656         Die(errno, 0);
2657         return 1;
2658     }
2659     if (setp) {
2660         printf("%s: new sysname%s set.\n", pn, setp > 1 ? " list" : "");
2661         return 0;
2662     }
2663     input = space;
2664     memcpy(&setp, input, sizeof(afs_int32));
2665     input += sizeof(afs_int32);
2666     if (!setp) {
2667         fprintf(stderr, "No sysname name value was found\n");
2668         return 1;
2669     }
2670     printf("Current sysname%s is", setp > 1 ? " list" : "");
2671     for (; setp > 0; --setp) {
2672         printf(" \'%s\'", input);
2673         input += strlen(input) + 1;
2674     }
2675     printf("\n");
2676     return 0;
2677 }
2678
2679 static char *exported_types[] = { "null", "nfs", "" };
2680 static int
2681 ExportAfsCmd(struct cmd_syndesc *as, void *arock)
2682 {
2683     afs_int32 code;
2684     struct ViceIoctl blob;
2685     struct cmd_item *ti;
2686     int export = 0, type = 0, mode = 0, exp = 0, exportcall, pwsync =
2687         0, smounts = 0, clipags = 0, pagcb = 0;
2688
2689     ti = as->parms[0].items;
2690     if (strcmp(ti->data, "nfs") == 0)
2691         type = 0x71;            /* NFS */
2692     else {
2693         fprintf(stderr,
2694                 "Invalid exporter type, '%s', Only the 'nfs' exporter is currently supported\n",
2695                 ti->data);
2696         return 1;
2697     }
2698     ti = as->parms[1].items;
2699     if (ti) {
2700         if (strcmp(ti->data, "on") == 0)
2701             export = 3;
2702         else if (strcmp(ti->data, "off") == 0)
2703             export = 2;
2704         else {
2705             fprintf(stderr, "Illegal argument %s\n", ti->data);
2706             return 1;
2707         }
2708         exp = 1;
2709     }
2710     if (ti = as->parms[2].items) {      /* -noconvert */
2711         if (strcmp(ti->data, "on") == 0)
2712             mode = 2;
2713         else if (strcmp(ti->data, "off") == 0)
2714             mode = 3;
2715         else {
2716             fprintf(stderr, "Illegal argument %s\n", ti->data);
2717             return 1;
2718         }
2719     }
2720     if (ti = as->parms[3].items) {      /* -uidcheck */
2721         if (strcmp(ti->data, "on") == 0)
2722             pwsync = 3;
2723         else if (strcmp(ti->data, "off") == 0)
2724             pwsync = 2;
2725         else {
2726             fprintf(stderr, "Illegal argument %s\n", ti->data);
2727             return 1;
2728         }
2729     }
2730     if (ti = as->parms[4].items) {      /* -submounts */
2731         if (strcmp(ti->data, "on") == 0)
2732             smounts = 3;
2733         else if (strcmp(ti->data, "off") == 0)
2734             smounts = 2;
2735         else {
2736             fprintf(stderr, "Illegal argument %s\n", ti->data);
2737             return 1;
2738         }
2739     }
2740     if (ti = as->parms[5].items) {      /* -clipags */
2741         if (strcmp(ti->data, "on") == 0)
2742             clipags = 3;
2743         else if (strcmp(ti->data, "off") == 0)
2744             clipags = 2;
2745         else {
2746             fprintf(stderr, "Illegal argument %s\n", ti->data);
2747             return 1;
2748         }
2749     }
2750     if (ti = as->parms[6].items) {      /* -pagcb */
2751         if (strcmp(ti->data, "on") == 0)
2752             pagcb = 3;
2753         else if (strcmp(ti->data, "off") == 0)
2754             pagcb = 2;
2755         else {
2756             fprintf(stderr, "Illegal argument %s\n", ti->data);
2757             return 1;
2758         }
2759     }
2760     exportcall =
2761         (type << 24) | (pagcb << 10) | (clipags << 8) |
2762         (mode << 6) | (pwsync << 4) | (smounts << 2) | export;
2763     type &= ~0x70;
2764     /* make the call */
2765     blob.in = (char *)&exportcall;
2766     blob.in_size = sizeof(afs_int32);
2767     blob.out = (char *)&exportcall;
2768     blob.out_size = sizeof(afs_int32);
2769     code = pioctl(0, VIOC_EXPORTAFS, &blob, 1);
2770     if (code) {
2771         if (errno == ENODEV) {
2772             fprintf(stderr,
2773                     "Sorry, the %s-exporter type is currently not supported on this AFS client\n",
2774                     exported_types[type]);
2775         } else {
2776             Die(errno, 0);
2777         }
2778         return 1;
2779     }
2780
2781     if (exportcall & 1) {
2782         printf("'%s' translator is enabled with the following options:\n",
2783                exported_types[type]);
2784         printf("\tRunning in %s mode\n",
2785                (exportcall & 2 ? "strict unix" :
2786                 "convert owner mode bits to world/other"));
2787         printf("\tRunning in %s mode\n",
2788                (exportcall & 4 ? "strict 'passwd sync'" :
2789                 "no 'passwd sync'"));
2790         printf("\t%s\n",
2791                (exportcall & 8 ? "Allow mounts of /afs/.. subdirs" :
2792                 "Only mounts to /afs allowed"));
2793         printf("\t%s\n",
2794                (exportcall & 16 ? "Client-assigned PAG's are used" :
2795                 "Client-assigned PAG's are not used"));
2796         printf("\t%s\n",
2797                (exportcall & 32 ?
2798                 "Callbacks are made to get creds from new clients" :
2799                 "Callbacks are not made to get creds from new clients"));
2800     } else {
2801         printf("'%s' translator is disabled\n", exported_types[type]);
2802     }
2803     return 0;
2804 }
2805
2806
2807 static int
2808 GetCellCmd(struct cmd_syndesc *as, void *arock)
2809 {
2810     afs_int32 code;
2811     struct ViceIoctl blob;
2812     struct afsconf_cell info;
2813     struct cmd_item *ti;
2814     struct a {
2815         afs_int32 stat;
2816         afs_int32 junk;
2817     } args;
2818     int error = 0;
2819
2820     memset(&args, '\0', sizeof args);   /* avoid Purify UMR error */
2821     for (ti = as->parms[0].items; ti; ti = ti->next) {
2822         /* once per cell */
2823         blob.out_size = sizeof(args);
2824         blob.out = (caddr_t) & args;
2825         code = GetCellName(ti->data, &info);
2826         if (code) {
2827             error = 1;
2828             continue;
2829         }
2830         blob.in_size = 1 + strlen(info.name);
2831         blob.in = info.name;
2832         code = pioctl(0, VIOC_GETCELLSTATUS, &blob, 1);
2833         if (code) {
2834             if (errno == ENOENT)
2835                 fprintf(stderr, "%s: the cell named '%s' does not exist\n",
2836                         pn, info.name);
2837             else
2838                 Die(errno, info.name);
2839             error = 1;
2840             continue;
2841         }
2842         printf("Cell %s status: ", info.name);
2843 #ifdef notdef
2844         if (args.stat & 1)
2845             printf("primary ");
2846 #endif
2847         if (args.stat & 2)
2848             printf("no setuid allowed");
2849         else
2850             printf("setuid allowed");
2851         if (args.stat & 4)
2852             printf(", using old VLDB");
2853         printf("\n");
2854     }
2855     return error;
2856 }
2857
2858 static int
2859 SetCellCmd(struct cmd_syndesc *as, void *arock)
2860 {
2861     afs_int32 code;
2862     struct ViceIoctl blob;
2863     struct afsconf_cell info;
2864     struct cmd_item *ti;
2865     struct a {
2866         afs_int32 stat;
2867         afs_int32 junk;
2868         char cname[64];
2869     } args;
2870     int error = 0;
2871
2872     /* Check arguments. */
2873     if (as->parms[1].items && as->parms[2].items) {
2874         fprintf(stderr, "Cannot specify both -suid and -nosuid.\n");
2875         return 1;
2876     }
2877
2878     /* figure stuff to set */
2879     args.stat = 0;
2880     args.junk = 0;
2881
2882     if (!as->parms[1].items)
2883         args.stat |= 2;         /* default to -nosuid */
2884
2885     /* set stat for all listed cells */
2886     for (ti = as->parms[0].items; ti; ti = ti->next) {
2887         /* once per cell */
2888         code = GetCellName(ti->data, &info);
2889         if (code) {
2890             error = 1;
2891             continue;
2892         }
2893         strcpy(args.cname, info.name);
2894         blob.in_size = sizeof(args);
2895         blob.in = (caddr_t) & args;
2896         blob.out_size = 0;
2897         blob.out = (caddr_t) 0;
2898         code = pioctl(0, VIOC_SETCELLSTATUS, &blob, 1);
2899         if (code) {
2900             Die(errno, info.name);      /* XXX added cell name to Die() call */
2901             error = 1;
2902         }
2903     }
2904     return error;
2905 }
2906
2907 static int
2908 GetCellName(char *cellName, struct afsconf_cell *info)
2909 {
2910     struct afsconf_dir *tdir;
2911     int code;
2912
2913     tdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH);
2914     if (!tdir) {
2915         fprintf(stderr,
2916                 "Could not process files in configuration directory (%s).\n",
2917                 AFSDIR_CLIENT_ETC_DIRPATH);
2918         return -1;
2919     }
2920
2921     code = afsconf_GetCellInfo(tdir, cellName, AFSCONF_VLDBSERVICE, info);
2922     if (code) {
2923         fprintf(stderr, "%s: cell %s not in %s\n", pn, cellName,
2924                 AFSDIR_CLIENT_CELLSERVDB_FILEPATH);
2925         return code;
2926     }
2927
2928     return 0;
2929 }
2930
2931
2932 static int
2933 VLDBInit(int noAuthFlag, struct afsconf_cell *info)
2934 {
2935     afs_int32 code;
2936
2937     code = ugen_ClientInit(noAuthFlag, AFSDIR_CLIENT_ETC_DIRPATH, 
2938                            info->name, 0, &uclient, 
2939                            NULL, pn, rxkad_clear,
2940                            VLDB_MAXSERVERS, AFSCONF_VLDBSERVICE, 50,
2941                            0, 0, USER_SERVICE_ID);
2942     rxInitDone = 1;
2943     return code;
2944 }
2945
2946 static struct ViceIoctl gblob;
2947 static int debug = 0;
2948 /*
2949  * here follow some routines in suport of the setserverprefs and
2950  * getserverprefs commands.  They are:
2951  * SetPrefCmd  "top-level" routine
2952  * addServer   adds a server to the list of servers to be poked into the
2953  *             kernel.  Will poke the list into the kernel if it threatens
2954  *             to get too large.
2955  * pokeServers pokes the existing list of servers and ranks into the kernel
2956  * GetPrefCmd  reads the Cache Manager's current list of server ranks
2957  */
2958
2959 /*
2960  * returns -1 if error message printed,
2961  * 0 on success,
2962  * errno value if error and no error message printed
2963  */
2964 static int
2965 pokeServers(void)
2966 {
2967     int code;
2968
2969     code = pioctl(0, VIOC_SETSPREFS, &gblob, 1);
2970     if (code && (errno == EINVAL)) {
2971         struct setspref *ssp;
2972         ssp = (struct setspref *)gblob.in;
2973         if (!(ssp->flags & DBservers)) {
2974             gblob.in = (void *)&(ssp->servers[0]);
2975             gblob.in_size -= ((char *)&(ssp->servers[0])) - (char *)ssp;
2976             code = pioctl(0, VIOC_SETSPREFS33, &gblob, 1);
2977             return code ? errno : 0;
2978         }
2979         fprintf(stderr,
2980                 "This cache manager does not support VL server preferences.\n");
2981         return -1;
2982     }
2983
2984     return code ? errno : 0;
2985 }
2986
2987 /*
2988  * returns -1 if error message printed,
2989  * 0 on success,
2990  * errno value if error and no error message printed
2991  */
2992 static int
2993 addServer(char *name, afs_int32 rank)
2994 {
2995     int t, code;
2996     struct setspref *ssp;
2997     struct spref *sp;
2998     struct hostent *thostent;
2999     afs_uint32 addr;
3000     int error = 0;
3001
3002 #ifndef MAXUSHORT
3003 #ifdef MAXSHORT
3004 #define MAXUSHORT ((unsigned short) 2*MAXSHORT+1)       /* assumes two's complement binary system */
3005 #else
3006 #define MAXUSHORT ((unsigned short) ~0)
3007 #endif
3008 #endif
3009
3010     thostent = hostutil_GetHostByName(name);
3011     if (!thostent) {
3012         fprintf(stderr, "%s: couldn't resolve name.\n", name);
3013         return -1;
3014     }
3015
3016     ssp = (struct setspref *)(gblob.in);
3017
3018     for (t = 0; thostent->h_addr_list[t]; t++) {
3019         if (gblob.in_size > MAXINSIZE - sizeof(struct spref)) {
3020             code = pokeServers();
3021             if (code)
3022                 error = code;
3023             ssp->num_servers = 0;
3024         }
3025
3026         sp = (struct spref *)(gblob.in + gblob.in_size);
3027         memcpy(&(sp->server.s_addr), thostent->h_addr_list[t],
3028                sizeof(afs_uint32));
3029         sp->rank = (rank > MAXUSHORT ? MAXUSHORT : rank);
3030         gblob.in_size += sizeof(struct spref);
3031         ssp->num_servers++;
3032
3033         if (debug)
3034             fprintf(stderr, "adding server %s, rank %d, ip addr 0x%lx\n",
3035                     name, sp->rank, sp->server.s_addr);
3036     }
3037
3038     return error;
3039 }
3040
3041
3042 static int
3043 SetPrefCmd(struct cmd_syndesc *as, void *arock)
3044 {
3045     FILE *infd;
3046     afs_int32 code;
3047     struct cmd_item *ti;
3048     char name[80];
3049     afs_int32 rank;
3050     struct setspref *ssp;
3051     int error = 0;              /* -1 means error message printed,
3052                                  * >0 means errno value for unprinted message */
3053
3054     ssp = (struct setspref *)space;
3055     ssp->flags = 0;
3056     ssp->num_servers = 0;
3057     gblob.in_size = ((char *)&(ssp->servers[0])) - (char *)ssp;
3058     gblob.in = space;
3059     gblob.out = space;
3060     gblob.out_size = MAXSIZE;
3061
3062
3063     if (geteuid()) {
3064         fprintf(stderr, "Permission denied: requires root access.\n");
3065         return 1;
3066     }
3067
3068     ti = as->parms[2].items;    /* -file */
3069     if (ti) {
3070         if (debug)
3071             fprintf(stderr, "opening file %s\n", ti->data);
3072         if (!(infd = fopen(ti->data, "r"))) {
3073             perror(ti->data);
3074             error = -1;
3075         } else {
3076             while (fscanf(infd, "%79s%ld", name, &rank) != EOF) {
3077                 code = addServer(name, (unsigned short)rank);
3078                 if (code)
3079                     error = code;
3080             }
3081         }
3082     }
3083
3084     ti = as->parms[3].items;    /* -stdin */
3085     if (ti) {
3086         while (scanf("%79s%ld", name, &rank) != EOF) {
3087             code = addServer(name, (unsigned short)rank);
3088             if (code)
3089                 error = code;
3090         }
3091     }
3092
3093     for (ti = as->parms[0].items; ti; ti = ti->next) {  /* list of servers, ranks */
3094         if (ti) {
3095             if (!ti->next) {
3096                 break;
3097             }
3098             code = addServer(ti->data, (unsigned short)atol(ti->next->data));
3099             if (code)
3100                 error = code;
3101             if (debug)
3102                 printf("set fs prefs %s %s\n", ti->data, ti->next->data);
3103             ti = ti->next;
3104         }
3105     }
3106     code = pokeServers();
3107     if (code)
3108         error = code;
3109     if (debug)
3110         printf("now working on vlservers, code=%d\n", code);
3111
3112     ssp = (struct setspref *)space;
3113     ssp->flags = DBservers;
3114     ssp->num_servers = 0;
3115     gblob.in_size = ((char *)&(ssp->servers[0])) - (char *)ssp;
3116     gblob.in = space;
3117
3118     for (ti = as->parms[1].items; ti; ti = ti->next) {  /* list of dbservers, ranks */
3119         if (ti) {
3120             if (!ti->next) {
3121                 break;
3122             }
3123             code = addServer(ti->data, (unsigned short)atol(ti->next->data));
3124             if (code)
3125                 error = code;
3126             if (debug)
3127                 printf("set vl prefs %s %s\n", ti->data, ti->next->data);
3128             ti = ti->next;
3129         }
3130     }
3131
3132     if (as->parms[1].items) {
3133         if (debug)
3134             printf("now poking vlservers\n");
3135         code = pokeServers();
3136         if (code)
3137             error = code;
3138     }
3139
3140     if (error > 0)
3141         Die(error, 0);
3142
3143     return error ? 1 : 0;
3144 }
3145
3146
3147
3148 static int
3149 GetPrefCmd(struct cmd_syndesc *as, void *arock)
3150 {
3151     afs_int32 code;
3152     struct cmd_item *ti;
3153     char *name, tbuffer[20];
3154     afs_int32 rank, addr;
3155     FILE *outfd;
3156     int resolve;
3157     int vlservers = 0;
3158     struct ViceIoctl blob;
3159     struct sprefrequest *in;
3160     struct sprefinfo *out;
3161     int i;
3162
3163     ti = as->parms[0].items;    /* -file */
3164     if (ti) {
3165         if (debug)
3166             fprintf(stderr, "opening file %s\n", ti->data);
3167         if (!(outfd = freopen(ti->data, "w", stdout))) {
3168             perror(ti->data);
3169             return 1;
3170         }
3171     }
3172
3173     ti = as->parms[1].items;    /* -numeric */
3174     resolve = !(ti);
3175     ti = as->parms[2].items;    /* -vlservers */
3176     vlservers |= (ti ? DBservers : 0);
3177     /*  ti = as->parms[3].items;   -cell */
3178
3179     in = (struct sprefrequest *)space;
3180     in->offset = 0;
3181
3182     do {
3183         blob.in_size = sizeof(struct sprefrequest);
3184         blob.in = (char *)in;
3185         blob.out = space;
3186         blob.out_size = MAXSIZE;
3187
3188         in->num_servers =
3189             (MAXSIZE - 2 * sizeof(short)) / sizeof(struct spref);
3190         in->flags = vlservers;
3191
3192         code = pioctl(0, VIOC_GETSPREFS, &blob, 1);
3193         if (code) {
3194             perror("getserverprefs pioctl");
3195             return 1;
3196         }
3197
3198         out = (struct sprefinfo *)blob.out;
3199
3200         for (i = 0; i < out->num_servers; i++) {
3201             if (resolve) {
3202                 name = hostutil_GetNameByINet(out->servers[i].server.s_addr);
3203             } else {
3204                 addr = ntohl(out->servers[i].server.s_addr);
3205                 sprintf(tbuffer, "%d.%d.%d.%d", (addr >> 24) & 0xff,
3206                         (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff);
3207                 name = tbuffer;
3208             }
3209             printf("%-50s %5u\n", name, out->servers[i].rank);
3210         }
3211
3212         in->offset = out->next_offset;
3213     } while (out->next_offset > 0);
3214
3215     return 0;
3216 }
3217
3218 static int
3219 StoreBehindCmd(struct cmd_syndesc *as, void *arock)
3220 {
3221     afs_int32 code = 0;
3222     struct ViceIoctl blob;
3223     struct cmd_item *ti;
3224     struct sbstruct tsb, tsb2;
3225     int verbose = 0;
3226     afs_int32 allfiles;
3227     char *t;
3228     int error = 0;
3229
3230     tsb.sb_thisfile = -1;
3231     ti = as->parms[0].items;    /* -kbytes */
3232     if (ti) {
3233         if (!as->parms[1].items) {
3234             fprintf(stderr, "%s: you must specify -files with -kbytes.\n",
3235                     pn);
3236             return 1;
3237         }
3238         tsb.sb_thisfile = strtol(ti->data, &t, 10) * 1024;
3239         if ((tsb.sb_thisfile < 0) || (t != ti->data + strlen(ti->data))) {
3240             fprintf(stderr, "%s: %s must be 0 or a positive number.\n", pn,
3241                     ti->data);
3242             return 1;
3243         }
3244     }
3245
3246     allfiles = tsb.sb_default = -1;     /* Don't set allfiles yet */
3247     ti = as->parms[2].items;    /* -allfiles */
3248     if (ti) {
3249         allfiles = strtol(ti->data, &t, 10) * 1024;
3250         if ((allfiles < 0) || (t != ti->data + strlen(ti->data))) {
3251             fprintf(stderr, "%s: %s must be 0 or a positive number.\n", pn,
3252                     ti->data);
3253             return 1;
3254         }
3255     }
3256
3257     /* -verbose or -file only or no options */
3258     if (as->parms[3].items || (as->parms[1].items && !as->parms[0].items)
3259         || (!as->parms[0].items && !as->parms[1].items
3260             && !as->parms[2].items))
3261         verbose = 1;
3262
3263     blob.in = (char *)&tsb;
3264     blob.out = (char *)&tsb2;
3265     blob.in_size = blob.out_size = sizeof(struct sbstruct);
3266     memset(&tsb2, 0, sizeof(tsb2));
3267
3268     /* once per -file */
3269     for (ti = as->parms[1].items; ti; ti = ti->next) {
3270         /* Do this solely to see if the file is there */
3271         code = pioctl(ti->data, VIOCWHEREIS, &blob, 1);
3272         if (code) {
3273             Die(errno, ti->data);
3274             error = 1;
3275             continue;
3276         }
3277
3278         code = pioctl(ti->data, VIOC_STORBEHIND, &blob, 1);
3279         if (code) {
3280             Die(errno, ti->data);
3281             error = 1;
3282             continue;
3283         }
3284
3285         if (verbose && (blob.out_size == sizeof(tsb2))) {
3286             if (tsb2.sb_thisfile == -1) {
3287                 fprintf(stdout, "Will store %s according to default.\n",
3288                         ti->data);
3289             } else {
3290                 fprintf(stdout,
3291                         "Will store up to %d kbytes of %s asynchronously.\n",
3292                         (tsb2.sb_thisfile / 1024), ti->data);
3293             }
3294         }
3295     }
3296
3297     /* If no files - make at least one pioctl call, or
3298      * set the allfiles default if we need to.
3299      */
3300     if (!as->parms[1].items || (allfiles != -1)) {
3301         tsb.sb_default = allfiles;
3302         code = pioctl(0, VIOC_STORBEHIND, &blob, 1);
3303         if (code) {
3304             Die(errno, ((allfiles == -1) ? 0 : "-allfiles"));
3305             error = 1;
3306         }
3307     }
3308
3309     /* Having no arguments also reports the default store asynchrony */
3310     if (verbose && (blob.out_size == sizeof(tsb2))) {
3311         fprintf(stdout, "Default store asynchrony is %d kbytes.\n",
3312                 (tsb2.sb_default / 1024));
3313     }
3314
3315     return error;
3316 }
3317
3318
3319 static int
3320 SetCryptCmd(struct cmd_syndesc *as, void *arock)
3321 {
3322     afs_int32 code = 0, flag;
3323     struct ViceIoctl blob;
3324     char *tp;
3325
3326     tp = as->parms[0].items->data;
3327     if (strcmp(tp, "on") == 0)
3328         flag = 1;
3329     else if (strcmp(tp, "off") == 0)
3330         flag = 0;
3331     else {
3332         fprintf(stderr, "%s: %s must be \"on\" or \"off\".\n", pn, tp);
3333         return EINVAL;
3334     }
3335
3336     blob.in = (char *)&flag;
3337     blob.in_size = sizeof(flag);
3338     blob.out_size = 0;
3339     code = pioctl(0, VIOC_SETRXKCRYPT, &blob, 1);
3340     if (code)
3341         Die(errno, NULL);
3342     return 0;
3343 }
3344
3345
3346 static int
3347 GetCryptCmd(struct cmd_syndesc *as, void *arock)
3348 {
3349     afs_int32 code = 0, flag;
3350     struct ViceIoctl blob;
3351     char *tp;
3352
3353     blob.in = NULL;
3354     blob.in_size = 0;
3355     blob.out_size = sizeof(flag);
3356     blob.out = space;
3357
3358     code = pioctl(0, VIOC_GETRXKCRYPT, &blob, 1);
3359
3360     if (code)
3361         Die(errno, NULL);
3362     else {
3363         tp = space;
3364         memcpy(&flag, tp, sizeof(afs_int32));
3365         printf("Security level is currently ");
3366         if (flag == 1)
3367             printf("crypt (data security).\n");
3368         else
3369             printf("clear.\n");
3370     }
3371     return 0;
3372 }
3373
3374 #ifdef AFS_DISCON_ENV
3375 static char *modenames[] = {
3376     "offline",
3377     "online",
3378     "readonly",  /* Not currently supported */
3379     "fetchonly", /* Not currently supported */
3380     "partial",   /* Not currently supported */
3381     NULL
3382 };
3383
3384 static char *policynames[] = {
3385     "client",
3386     "server",
3387     "closer",  /* Not currently supported. */
3388     "manual",  /* Not currently supported. */
3389     NULL
3390 };
3391
3392 static int
3393 DisconCmd(struct cmd_syndesc *as, void *arock)
3394 {
3395     struct cmd_item *ti;
3396     char *modename;
3397     char *policyname;
3398     int modelen, policylen;
3399     afs_int32 mode, policy, code;
3400     struct ViceIoctl blob;
3401
3402     blob.in = NULL;
3403     blob.in_size = 0;
3404
3405     space[0] = space[1] = space[2] = 0;
3406
3407     ti = as->parms[0].items;
3408     if (ti) {
3409         modename = ti->data;
3410         modelen = strlen(modename);
3411         for (mode = 0; modenames[mode] != NULL; mode++)
3412             if (!strncasecmp(modename, modenames[mode], modelen))
3413                 break;
3414         if (modenames[mode] == NULL)
3415             printf("Unknown discon mode \"%s\"\n", modename);
3416         else {
3417             space[0] = mode + 1;
3418         }
3419     }
3420     ti = as->parms[1].items;
3421     if (ti) {
3422         policyname = ti->data;
3423         policylen = strlen(policyname);
3424         for (policy = 0; policynames[policy] != NULL; policy++)
3425             if (!strncasecmp(policyname, policynames[policy], policylen))
3426                 break;
3427         if (policynames[policy] == NULL)
3428             printf("Unknown discon mode \"%s\"\n", policyname);
3429         else {
3430             space[1] = policy + 1;
3431         }
3432     }
3433
3434     if (as->parms[2].items) {
3435         space[2] = 1;
3436         printf("force on\n");
3437     }
3438
3439     blob.in = space;
3440     blob.in_size = 3 * sizeof(afs_int32);
3441
3442     blob.out_size = sizeof(mode);
3443     blob.out = space;
3444     code = pioctl(0, VIOC_DISCON, &blob, 1);
3445     if (code)
3446         Die(errno, NULL);
3447     else {
3448         memcpy(&mode, space, sizeof mode);
3449         if (mode < sizeof modenames / sizeof (char *))
3450             printf("Discon mode is now \"%s\"\n", modenames[mode]);
3451         else
3452             printf("Unknown discon mode %d\n", mode);
3453     }
3454
3455     return 0;
3456 }
3457 #endif
3458
3459 #include "AFS_component_version_number.c"
3460
3461 int
3462 main(int argc, char **argv)
3463 {
3464     afs_int32 code;
3465     struct cmd_syndesc *ts;
3466
3467 #ifdef  AFS_AIX32_ENV
3468     /*
3469      * The following signal action for AIX is necessary so that in case of a
3470      * crash (i.e. core is generated) we can include the user's data section
3471      * in the core dump. Unfortunately, by default, only a partial core is
3472      * generated which, in many cases, isn't too useful.
3473      */
3474     struct sigaction nsa;
3475
3476     sigemptyset(&nsa.sa_mask);
3477     nsa.sa_handler = SIG_DFL;
3478     nsa.sa_flags = SA_FULLDUMP;
3479     sigaction(SIGSEGV, &nsa, NULL);
3480 #endif
3481
3482     /* try to find volume location information */
3483     ts = cmd_CreateSyntax("getclientaddrs", GetClientAddrsCmd, NULL,
3484                           "get client network interface addresses");
3485     cmd_CreateAlias(ts, "gc");
3486
3487     ts = cmd_CreateSyntax("setclientaddrs", SetClientAddrsCmd, NULL,
3488                           "set client network interface addresses");
3489     cmd_AddParm(ts, "-address", CMD_LIST, CMD_OPTIONAL | CMD_EXPANDS,
3490                 "client network interfaces");
3491     cmd_CreateAlias(ts, "sc");
3492
3493     ts = cmd_CreateSyntax("setserverprefs", SetPrefCmd, NULL,
3494                           "set server ranks");
3495     cmd_AddParm(ts, "-servers", CMD_LIST, CMD_OPTIONAL | CMD_EXPANDS,
3496                 "fileserver names and ranks");
3497     cmd_AddParm(ts, "-vlservers", CMD_LIST, CMD_OPTIONAL | CMD_EXPANDS,
3498                 "VL server names and ranks");
3499     cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL,
3500                 "input from named file");
3501     cmd_AddParm(ts, "-stdin", CMD_FLAG, CMD_OPTIONAL, "input from stdin");
3502     cmd_CreateAlias(ts, "sp");
3503
3504     ts = cmd_CreateSyntax("getserverprefs", GetPrefCmd, NULL,
3505                           "get server ranks");
3506     cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL,
3507                 "output to named file");
3508     cmd_AddParm(ts, "-numeric", CMD_FLAG, CMD_OPTIONAL, "addresses only");
3509     cmd_AddParm(ts, "-vlservers", CMD_FLAG, CMD_OPTIONAL, "VL servers");
3510 /*    cmd_AddParm(ts, "-cell", CMD_FLAG, CMD_OPTIONAL, "cellname"); */
3511     cmd_CreateAlias(ts, "gp");
3512
3513     ts = cmd_CreateSyntax("setacl", SetACLCmd, NULL, "set access control list");
3514     cmd_AddParm(ts, "-dir", CMD_LIST, 0, "directory");
3515     cmd_AddParm(ts, "-acl", CMD_LIST, 0, "access list entries");
3516     cmd_AddParm(ts, "-clear", CMD_FLAG, CMD_OPTIONAL, "clear access list");
3517     cmd_AddParm(ts, "-negative", CMD_FLAG, CMD_OPTIONAL,
3518                 "apply to negative rights");
3519     parm_setacl_id = ts->nParms;
3520     cmd_AddParm(ts, "-id", CMD_FLAG, CMD_OPTIONAL,
3521                 "initial directory acl (DFS only)");
3522     cmd_AddParm(ts, "-if", CMD_FLAG, CMD_OPTIONAL,
3523                 "initial file acl (DFS only)");
3524     cmd_CreateAlias(ts, "sa");
3525
3526     ts = cmd_CreateSyntax("listacl", ListACLCmd, NULL,
3527                           "list access control list");
3528     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3529     parm_listacl_id = ts->nParms;
3530     cmd_AddParm(ts, "-id", CMD_FLAG, CMD_OPTIONAL, "initial directory acl");
3531     cmd_AddParm(ts, "-if", CMD_FLAG, CMD_OPTIONAL, "initial file acl");
3532     cmd_CreateAlias(ts, "la");
3533
3534     ts = cmd_CreateSyntax("getcalleraccess", GetCallerAccess, NULL,
3535             "list callers access");
3536     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3537     cmd_CreateAlias(ts, "gca");
3538
3539     ts = cmd_CreateSyntax("cleanacl", CleanACLCmd, NULL,
3540                           "clean up access control list");
3541     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3542
3543     ts = cmd_CreateSyntax("copyacl", CopyACLCmd, NULL,
3544                           "copy access control list");
3545     cmd_AddParm(ts, "-fromdir", CMD_SINGLE, 0,
3546                 "source directory (or DFS file)");
3547     cmd_AddParm(ts, "-todir", CMD_LIST, 0,
3548                 "destination directory (or DFS file)");
3549     cmd_AddParm(ts, "-clear", CMD_FLAG, CMD_OPTIONAL,
3550                 "first clear dest access list");
3551     parm_copyacl_id = ts->nParms;
3552     cmd_AddParm(ts, "-id", CMD_FLAG, CMD_OPTIONAL, "initial directory acl");
3553     cmd_AddParm(ts, "-if", CMD_FLAG, CMD_OPTIONAL, "initial file acl");
3554
3555     cmd_CreateAlias(ts, "ca");
3556
3557     ts = cmd_CreateSyntax("flush", FlushCmd, NULL, "flush file from cache");
3558     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3559     ts = cmd_CreateSyntax("flushmount", FlushMountCmd, NULL,
3560                           "flush mount symlink from cache");
3561     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3562
3563     ts = cmd_CreateSyntax("setvol", SetVolCmd, NULL, "set volume status");
3564     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3565     cmd_AddParm(ts, "-max", CMD_SINGLE, CMD_OPTIONAL,
3566                 "disk space quota in 1K units");
3567 #ifdef notdef
3568     cmd_AddParm(ts, "-min", CMD_SINGLE, CMD_OPTIONAL,
3569                 "disk space guaranteed");
3570     cmd_AddParm(ts, "-motd", CMD_SINGLE, CMD_OPTIONAL, "message of the day");
3571 #endif
3572     cmd_AddParm(ts, "-offlinemsg", CMD_SINGLE, CMD_OPTIONAL,
3573                 "offline message");
3574     cmd_CreateAlias(ts, "sv");
3575
3576     ts = cmd_CreateSyntax("messages", MessagesCmd, NULL,
3577                           "control Cache Manager messages");
3578     cmd_AddParm(ts, "-show", CMD_SINGLE, CMD_OPTIONAL,
3579                 "[user|console|all|none]");
3580
3581     ts = cmd_CreateSyntax("examine", ExamineCmd, NULL, "display file/volume status");
3582     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3583     cmd_CreateAlias(ts, "lv");
3584     cmd_CreateAlias(ts, "listvol");
3585
3586     ts = cmd_CreateSyntax("listquota", ListQuotaCmd, NULL, "list volume quota");
3587     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3588     cmd_CreateAlias(ts, "lq");
3589
3590     ts = cmd_CreateSyntax("diskfree", DiskFreeCmd, NULL,
3591                           "show server disk space usage");
3592     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3593     cmd_CreateAlias(ts, "df");
3594
3595     ts = cmd_CreateSyntax("quota", QuotaCmd, NULL, "show volume quota usage");
3596     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3597
3598     ts = cmd_CreateSyntax("lsmount", ListMountCmd, NULL, "list mount point");
3599     cmd_AddParm(ts, "-dir", CMD_LIST, 0, "directory");
3600
3601     ts = cmd_CreateSyntax("mkmount", MakeMountCmd, NULL, "make mount point");
3602     cmd_AddParm(ts, "-dir", CMD_SINGLE, 0, "directory");
3603     cmd_AddParm(ts, "-vol", CMD_SINGLE, 0, "volume name");
3604     cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cell name");
3605     cmd_AddParm(ts, "-rw", CMD_FLAG, CMD_OPTIONAL, "force r/w volume");
3606     cmd_AddParm(ts, "-fast", CMD_FLAG, CMD_OPTIONAL,
3607                 "don't check name with VLDB");
3608
3609 #if defined(AFS_CACHE_BYPASS)
3610         ts = cmd_CreateSyntax("bypassthreshold", BypassThresholdCmd, 0,
3611                 "get/set cache bypass file size threshold");
3612         cmd_AddParm(ts, "-size", CMD_SINGLE, CMD_OPTIONAL, "file size");
3613 #endif
3614
3615 /*
3616
3617 defect 3069
3618
3619     cmd_AddParm(ts, "-root", CMD_FLAG, CMD_OPTIONAL, "create cellular mount point");
3620 */
3621
3622
3623     ts = cmd_CreateSyntax("rmmount", RemoveMountCmd, NULL, "remove mount point");
3624     cmd_AddParm(ts, "-dir", CMD_LIST, 0, "directory");
3625
3626     ts = cmd_CreateSyntax("checkservers", CheckServersCmd, NULL,
3627                           "check local cell's servers");
3628     cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cell to check");
3629     cmd_AddParm(ts, "-all", CMD_FLAG, CMD_OPTIONAL, "check all cells");
3630     cmd_AddParm(ts, "-fast", CMD_FLAG, CMD_OPTIONAL,
3631                 "just list, don't check");
3632     cmd_AddParm(ts, "-interval", CMD_SINGLE, CMD_OPTIONAL,
3633                 "seconds between probes");
3634
3635     ts = cmd_CreateSyntax("checkvolumes", CheckVolumesCmd, NULL,
3636                           "check volumeID/name mappings");
3637     cmd_CreateAlias(ts, "checkbackups");
3638
3639
3640     ts = cmd_CreateSyntax("setcachesize", SetCacheSizeCmd, NULL,
3641                           "set cache size");
3642     cmd_AddParm(ts, "-blocks", CMD_SINGLE, CMD_OPTIONAL,
3643                 "size in 1K byte blocks (0 => reset)");
3644     cmd_CreateAlias(ts, "cachesize");
3645
3646     cmd_AddParm(ts, "-reset", CMD_FLAG, CMD_OPTIONAL,
3647                 "reset size back to boot value");
3648
3649     ts = cmd_CreateSyntax("getcacheparms", GetCacheParmsCmd, NULL,
3650                           "get cache usage info");
3651     cmd_AddParm(ts, "-files", CMD_FLAG, CMD_OPTIONAL, "Show cach files used as well");
3652     cmd_AddParm(ts, "-excessive", CMD_FLAG, CMD_OPTIONAL, "excessively verbose cache stats");
3653
3654     ts = cmd_CreateSyntax("listcells", ListCellsCmd, NULL,
3655                           "list configured cells");
3656     cmd_AddParm(ts, "-numeric", CMD_FLAG, CMD_OPTIONAL, "addresses only");
3657
3658     ts = cmd_CreateSyntax("listaliases", ListAliasesCmd, NULL,
3659                           "list configured cell aliases");
3660
3661     ts = cmd_CreateSyntax("setquota", SetQuotaCmd, NULL, "set volume quota");
3662     cmd_AddParm(ts, "-path", CMD_SINGLE, CMD_OPTIONAL, "dir/file path");
3663     cmd_AddParm(ts, "-max", CMD_SINGLE, 0, "max quota in kbytes");
3664 #ifdef notdef
3665     cmd_AddParm(ts, "-min", CMD_SINGLE, CMD_OPTIONAL, "min quota in kbytes");
3666 #endif
3667     cmd_CreateAlias(ts, "sq");
3668
3669     ts = cmd_CreateSyntax("newcell", NewCellCmd, NULL, "configure new cell");
3670     cmd_AddParm(ts, "-name", CMD_SINGLE, 0, "cell name");
3671     cmd_AddParm(ts, "-servers", CMD_LIST, CMD_REQUIRED, "primary servers");
3672     cmd_AddParm(ts, "-linkedcell", CMD_SINGLE, CMD_OPTIONAL,
3673                 "linked cell name");
3674
3675     ts = cmd_CreateSyntax("newalias", NewAliasCmd, NULL,
3676                           "configure new cell alias");
3677     cmd_AddParm(ts, "-alias", CMD_SINGLE, 0, "alias name");
3678     cmd_AddParm(ts, "-name", CMD_SINGLE, 0, "real name of cell");
3679
3680 #ifdef FS_ENABLE_SERVER_DEBUG_PORTS
3681 /*
3682  * Turn this on only if you wish to be able to talk to a server which is listening
3683  * on alternative ports. This is not intended for general use and may not be
3684  * supported in the cache manager. It is not a way to run two servers at the
3685  * same host, since the cache manager cannot properly distinguish those two hosts.
3686  */
3687     cmd_AddParm(ts, "-fsport", CMD_SINGLE, CMD_OPTIONAL,
3688                 "cell's fileserver port");
3689     cmd_AddParm(ts, "-vlport", CMD_SINGLE, CMD_OPTIONAL,
3690                 "cell's vldb server port");
3691 #endif
3692
3693     ts = cmd_CreateSyntax("whichcell", WhichCellCmd, NULL, "list file's cell");
3694     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3695
3696     ts = cmd_CreateSyntax("whereis", WhereIsCmd, NULL, "list file's location");
3697     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3698
3699     ts = cmd_CreateSyntax("wscell", WSCellCmd, NULL, "list workstation's cell");
3700
3701 /*
3702     ts = cmd_CreateSyntax("primarycell", PrimaryCellCmd, NULL, "obsolete (listed primary cell)");
3703 */
3704
3705     /* set cache monitor host address */
3706     ts = cmd_CreateSyntax("monitor", MonitorCmd, NULL, (char *)CMD_HIDDEN);
3707     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_OPTIONAL,
3708                 "host name or 'off'");
3709     cmd_CreateAlias(ts, "mariner");
3710
3711     ts = cmd_CreateSyntax("getcellstatus", GetCellCmd, NULL, "get cell status");
3712     cmd_AddParm(ts, "-cell", CMD_LIST, 0, "cell name");
3713
3714     ts = cmd_CreateSyntax("setcell", SetCellCmd, NULL, "set cell status");
3715     cmd_AddParm(ts, "-cell", CMD_LIST, 0, "cell name");
3716     cmd_AddParm(ts, "-suid", CMD_FLAG, CMD_OPTIONAL, "allow setuid programs");
3717     cmd_AddParm(ts, "-nosuid", CMD_FLAG, CMD_OPTIONAL,
3718                 "disallow setuid programs");
3719
3720     ts = cmd_CreateSyntax("flushvolume", FlushVolumeCmd, NULL,
3721                           "flush all data in volume");
3722     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3723
3724     ts = cmd_CreateSyntax("sysname", SysNameCmd, NULL,
3725                           "get/set sysname (i.e. @sys) value");
3726     cmd_AddParm(ts, "-newsys", CMD_LIST, CMD_OPTIONAL, "new sysname");
3727
3728     ts = cmd_CreateSyntax("exportafs", ExportAfsCmd, NULL,
3729                           "enable/disable translators to AFS");
3730     cmd_AddParm(ts, "-type", CMD_SINGLE, 0, "exporter name");
3731     cmd_AddParm(ts, "-start", CMD_SINGLE, CMD_OPTIONAL,
3732                 "start/stop translator (on | off)");
3733     cmd_AddParm(ts, "-convert", CMD_SINGLE, CMD_OPTIONAL,
3734                 "convert from afs to unix mode (on | off)");
3735     cmd_AddParm(ts, "-uidcheck", CMD_SINGLE, CMD_OPTIONAL,
3736                 "run on strict 'uid check' mode (on | off)");
3737     cmd_AddParm(ts, "-submounts", CMD_SINGLE, CMD_OPTIONAL,
3738                 "allow nfs mounts to subdirs of /afs/.. (on  | off)");
3739     cmd_AddParm(ts, "-clipags", CMD_SINGLE, CMD_OPTIONAL,
3740                 "enable use of client-assigned PAG's (on  | off)");
3741     cmd_AddParm(ts, "-pagcb", CMD_SINGLE, CMD_OPTIONAL,
3742                 "enable callbacks to get creds from new clients (on  | off)");
3743
3744
3745     ts = cmd_CreateSyntax("storebehind", StoreBehindCmd, NULL,
3746                           "store to server after file close");
3747     cmd_AddParm(ts, "-kbytes", CMD_SINGLE, CMD_OPTIONAL,
3748                 "asynchrony for specified names");
3749     cmd_AddParm(ts, "-files", CMD_LIST, CMD_OPTIONAL, "specific pathnames");
3750     cmd_AddParm(ts, "-allfiles", CMD_SINGLE, CMD_OPTIONAL,
3751                 "new default (KB)");
3752     cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, "show status");
3753     cmd_CreateAlias(ts, "sb");
3754
3755     ts = cmd_CreateSyntax("setcrypt", SetCryptCmd, NULL,
3756                           "set cache manager encryption flag");
3757     cmd_AddParm(ts, "-crypt", CMD_SINGLE, 0, "on or off");
3758
3759     ts = cmd_CreateSyntax("getcrypt", GetCryptCmd, NULL,
3760                           "get cache manager encryption flag");
3761
3762     ts = cmd_CreateSyntax("rxstatproc", RxStatProcCmd, NULL,
3763                           "Manage per process RX statistics");
3764     cmd_AddParm(ts, "-enable", CMD_FLAG, CMD_OPTIONAL, "Enable RX stats");
3765     cmd_AddParm(ts, "-disable", CMD_FLAG, CMD_OPTIONAL, "Disable RX stats");
3766     cmd_AddParm(ts, "-clear", CMD_FLAG, CMD_OPTIONAL, "Clear RX stats");
3767
3768     ts = cmd_CreateSyntax("rxstatpeer", RxStatPeerCmd, NULL,
3769                           "Manage per peer RX statistics");
3770     cmd_AddParm(ts, "-enable", CMD_FLAG, CMD_OPTIONAL, "Enable RX stats");
3771     cmd_AddParm(ts, "-disable", CMD_FLAG, CMD_OPTIONAL, "Disable RX stats");
3772     cmd_AddParm(ts, "-clear", CMD_FLAG, CMD_OPTIONAL, "Clear RX stats");
3773
3774     ts = cmd_CreateSyntax("setcbaddr", CallBackRxConnCmd, NULL, "configure callback connection address");
3775     cmd_AddParm(ts, "-addr", CMD_SINGLE, CMD_OPTIONAL, "host name or address");
3776
3777     /* try to find volume location information */
3778     ts = cmd_CreateSyntax("getfid", GetFidCmd, NULL,
3779                           "get fid for file(s)");
3780     cmd_AddParm(ts, "-path", CMD_LIST, CMD_OPTIONAL, "dir/file path");
3781
3782 #ifdef AFS_DISCON_ENV
3783     ts = cmd_CreateSyntax("discon", DisconCmd, NULL,
3784                           "disconnection mode");
3785     cmd_AddParm(ts, "-mode", CMD_SINGLE, CMD_REQUIRED, "offline | online");
3786     cmd_AddParm(ts, "-policy", CMD_SINGLE, CMD_OPTIONAL, "client | server");
3787     cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL, "Force reconnection, despite any synchronization issues.");
3788 #endif
3789
3790     ts = cmd_CreateSyntax("nukenfscreds", NukeNFSCredsCmd, NULL, "nuke credentials for NFS client");
3791     cmd_AddParm(ts, "-addr", CMD_SINGLE, 0, "host name or address");
3792
3793     ts = cmd_CreateSyntax("uuid", UuidCmd, NULL, "manage the UUID for the cache manager");
3794     cmd_AddParm(ts, "-generate", CMD_FLAG, CMD_REQUIRED, "generate a new UUID");
3795
3796     ts = cmd_CreateSyntax("precache", PreCacheCmd, 0,
3797                           "set precache size");
3798     cmd_AddParm(ts, "-blocks", CMD_SINGLE, CMD_OPTIONAL,
3799                 "size in 1K byte blocks (0 => disable)");
3800
3801     code = cmd_Dispatch(argc, argv);
3802     if (rxInitDone)
3803         rx_Finalize();
3804
3805     return code;
3806 }
3807
3808 static void
3809 Die(int errnum, char *filename)
3810 {
3811     switch (errnum) {
3812     case EINVAL:
3813         if (filename)
3814             fprintf(stderr,
3815                     "%s: Invalid argument; it is possible that %s is not in AFS.\n",
3816                     pn, filename);
3817         else
3818             fprintf(stderr, "%s: Invalid argument.\n", pn);
3819         break;
3820     case ENOENT:
3821         if (filename)
3822             fprintf(stderr, "%s: File '%s' doesn't exist\n", pn, filename);
3823         else
3824             fprintf(stderr, "%s: no such file returned\n", pn);
3825         break;
3826     case EROFS:
3827         fprintf(stderr,
3828                 "%s: You can not change a backup or readonly volume\n", pn);
3829         break;
3830     case EACCES:
3831     case EPERM:
3832         if (filename)
3833             fprintf(stderr,
3834                     "%s: You don't have the required access rights on '%s'\n",
3835                     pn, filename);
3836         else
3837             fprintf(stderr,
3838                     "%s: You do not have the required rights to do this operation\n",
3839                     pn);
3840         break;
3841     default:
3842         if (filename)
3843             fprintf(stderr, "%s:'%s'", pn, filename);
3844         else
3845             fprintf(stderr, "%s", pn);
3846         fprintf(stderr, ": %s\n", afs_error_message(errnum));
3847         break;
3848     }
3849 }
3850
3851 /* get clients interface addresses */
3852 static int
3853 GetClientAddrsCmd(struct cmd_syndesc *as, void *arock)
3854 {
3855     afs_int32 code;
3856     struct cmd_item *ti;
3857     char *name;
3858     struct ViceIoctl blob;
3859     struct sprefrequest *in;
3860     struct sprefinfo *out;
3861
3862     in = (struct sprefrequest *)space;
3863     in->offset = 0;
3864
3865     do {
3866         blob.in_size = sizeof(struct sprefrequest);
3867         blob.in = (char *)in;
3868         blob.out = space;
3869         blob.out_size = MAXSIZE;
3870
3871         in->num_servers =
3872             (MAXSIZE - 2 * sizeof(short)) / sizeof(struct spref);
3873         /* returns addr in network byte order */
3874         code = pioctl(0, VIOC_GETCPREFS, &blob, 1);
3875         if (code) {
3876             perror("getClientInterfaceAddr pioctl");
3877             return 1;
3878         }
3879
3880         {
3881             int i;
3882             out = (struct sprefinfo *)blob.out;
3883             for (i = 0; i < out->num_servers; i++) {
3884                 afs_int32 addr;
3885                 char tbuffer[32];
3886                 addr = ntohl(out->servers[i].server.s_addr);
3887                 sprintf(tbuffer, "%d.%d.%d.%d", (addr >> 24) & 0xff,
3888                         (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff);
3889                 printf("%-50s\n", tbuffer);
3890             }
3891             in->offset = out->next_offset;
3892         }
3893     } while (out->next_offset > 0);
3894
3895     return 0;
3896 }
3897
3898 static int
3899 SetClientAddrsCmd(struct cmd_syndesc *as, void *arock)
3900 {
3901     afs_int32 code, addr;
3902     struct cmd_item *ti;
3903     char name[80];
3904     struct ViceIoctl blob;
3905     struct setspref *ssp;
3906     int sizeUsed = 0, i, flag;
3907     afs_int32 existingAddr[1024];       /* existing addresses on this host */
3908     int existNu;
3909     int error = 0;
3910
3911     ssp = (struct setspref *)space;
3912     ssp->num_servers = 0;
3913     blob.in = space;
3914     blob.out = space;
3915     blob.out_size = MAXSIZE;
3916
3917     if (geteuid()) {
3918         fprintf(stderr, "Permission denied: requires root access.\n");
3919         return 1;
3920     }
3921
3922     /* extract all existing interface addresses */
3923     existNu = rx_getAllAddr(existingAddr, 1024);
3924     if (existNu < 0)
3925         return 1;
3926
3927     sizeUsed = sizeof(struct setspref); /* space used in ioctl buffer */
3928     for (ti = as->parms[0].items; ti; ti = ti->next) {
3929         if (sizeUsed >= sizeof(space)) {
3930             fprintf(stderr, "No more space\n");
3931             return 1;
3932         }
3933         addr = extractAddr(ti->data, 20);       /* network order */
3934         if ((addr == AFS_IPINVALID) || (addr == AFS_IPINVALIDIGNORE)) {
3935             fprintf(stderr, "Error in specifying address: %s..ignoring\n",
3936                     ti->data);
3937             error = 1;
3938             continue;
3939         }
3940         /* see if it is an address that really exists */
3941         for (flag = 0, i = 0; i < existNu; i++)
3942             if (existingAddr[i] == addr) {
3943                 flag = 1;
3944                 break;
3945             }
3946         if (!flag) {            /* this is an nonexistent address */
3947             fprintf(stderr, "Nonexistent address: 0x%08x..ignoring\n", addr);
3948             error = 1;
3949             continue;
3950         }
3951         /* copy all specified addr into ioctl buffer */
3952         (ssp->servers[ssp->num_servers]).server.s_addr = addr;
3953         printf("Adding 0x%08x\n", addr);
3954         ssp->num_servers++;
3955         sizeUsed += sizeof(struct spref);
3956     }
3957     if (ssp->num_servers < 1) {
3958         fprintf(stderr, "No addresses specified\n");
3959         return 1;
3960     }
3961     blob.in_size = sizeUsed - sizeof(struct spref);
3962
3963     code = pioctl(0, VIOC_SETCPREFS, &blob, 1); /* network order */
3964     if (code) {
3965         Die(errno, 0);
3966         error = 1;
3967     }
3968
3969     return error;
3970 }
3971
3972 static int
3973 FlushMountCmd(struct cmd_syndesc *as, void *arock)
3974 {
3975     afs_int32 code;
3976     struct ViceIoctl blob;
3977     struct cmd_item *ti;
3978     char orig_name[1024];       /*Original name, may be modified */
3979     char true_name[1024];       /*``True'' dirname (e.g., symlink target) */
3980     char parent_dir[1024];      /*Parent directory of true name */
3981     char *last_component;       /*Last component of true name */
3982     struct stat statbuff;       /*Buffer for status info */
3983     int link_chars_read;        /*Num chars read in readlink() */
3984     int thru_symlink;           /*Did we get to a mount point via a symlink? */
3985     int error = 0;
3986
3987     for (ti = as->parms[0].items; ti; ti = ti->next) {
3988         /* once per file */
3989         thru_symlink = 0;
3990         sprintf(orig_name, "%s%s", (ti->data[0] == '/') ? "" : "./",
3991                 ti->data);
3992
3993         if (lstat(orig_name, &statbuff) < 0) {
3994             /* if lstat fails, we should still try the pioctl, since it
3995              * may work (for example, lstat will fail, but pioctl will
3996              * work if the volume of offline (returning ENODEV). */
3997             statbuff.st_mode = S_IFDIR; /* lie like pros */
3998         }
3999
4000         /*
4001          * The lstat succeeded.  If the given file is a symlink, substitute
4002          * the file name with the link name.
4003          */
4004         if ((statbuff.st_mode & S_IFMT) == S_IFLNK) {
4005             thru_symlink = 1;
4006             /*
4007              * Read name of resolved file.
4008              */
4009             link_chars_read = readlink(orig_name, true_name, 1024);
4010             if (link_chars_read <= 0) {
4011                 fprintf(stderr,
4012                         "%s: Can't read target name for '%s' symbolic link!\n",
4013                         pn, orig_name);
4014                 error = 1;
4015                 continue;
4016             }
4017
4018             /*
4019              * Add a trailing null to what was read, bump the length.
4020              */
4021             true_name[link_chars_read++] = 0;
4022
4023             /*
4024              * If the symlink is an absolute pathname, we're fine.  Otherwise, we
4025              * have to create a full pathname using the original name and the
4026              * relative symlink name.  Find the rightmost slash in the original
4027              * name (we know there is one) and splice in the symlink value.
4028              */
4029             if (true_name[0] != '/') {
4030                 last_component = (char *)strrchr(orig_name, '/');
4031                 strcpy(++last_component, true_name);
4032                 strcpy(true_name, orig_name);
4033             }
4034         } else
4035             strcpy(true_name, orig_name);
4036
4037         /*
4038          * Find rightmost slash, if any.
4039          */
4040         last_component = (char *)strrchr(true_name, '/');
4041         if (last_component) {
4042             /*
4043              * Found it.  Designate everything before it as the parent directory,
4044              * everything after it as the final component.
4045              */
4046             strncpy(parent_dir, true_name, last_component - true_name);
4047             parent_dir[last_component - true_name] = 0;
4048             last_component++;   /*Skip the slash */
4049         } else {
4050             /*
4051              * No slash appears in the given file name.  Set parent_dir to the current
4052              * directory, and the last component as the given name.
4053              */
4054             strcpy(parent_dir, ".");
4055             last_component = true_name;
4056         }
4057
4058         if (strcmp(last_component, ".") == 0
4059             || strcmp(last_component, "..") == 0) {
4060             fprintf(stderr,
4061                     "%s: you may not use '.' or '..' as the last component\n",
4062                     pn);
4063             fprintf(stderr, "%s: of a name in the 'fs flushmount' command.\n",
4064                     pn);
4065             error = 1;
4066             continue;
4067         }
4068
4069         blob.in = last_component;
4070         blob.in_size = strlen(last_component) + 1;
4071         blob.out_size = 0;
4072         memset(space, 0, MAXSIZE);
4073
4074         code = pioctl(parent_dir, VIOC_AFS_FLUSHMOUNT, &blob, 1);
4075
4076         if (code != 0) {
4077             if (errno == EINVAL) {
4078                 fprintf(stderr, "'%s' is not a mount point.\n", ti->data);
4079             } else {
4080                 Die(errno, (ti->data ? ti->data : parent_dir));
4081             }
4082             error = 1;
4083         }
4084     }
4085     return error;
4086 }
4087
4088 static int
4089 RxStatProcCmd(struct cmd_syndesc *as, void *arock)
4090 {
4091     afs_int32 code;
4092     afs_int32 flags = 0;
4093     struct ViceIoctl blob;
4094     struct cmd_item *ti;
4095
4096     if (as->parms[0].items) {   /* -enable */
4097         flags |= AFSCALL_RXSTATS_ENABLE;
4098     }
4099     if (as->parms[1].items) {   /* -disable */
4100         flags |= AFSCALL_RXSTATS_DISABLE;
4101     }
4102     if (as->parms[2].items) {   /* -clear */
4103         flags |= AFSCALL_RXSTATS_CLEAR;
4104     }
4105     if (flags == 0) {
4106         fprintf(stderr, "You must specify at least one argument\n");
4107         return 1;
4108     }
4109
4110     blob.in = (char *)&flags;
4111     blob.in_size = sizeof(afs_int32);
4112     blob.out_size = 0;
4113
4114     code = pioctl(NULL, VIOC_RXSTAT_PROC, &blob, 1);
4115     if (code != 0) {
4116         Die(errno, NULL);
4117         return 1;
4118     }
4119
4120     return 0;
4121 }
4122
4123 static int
4124 RxStatPeerCmd(struct cmd_syndesc *as, void *arock)
4125 {
4126     afs_int32 code;
4127     afs_int32 flags = 0;
4128     struct ViceIoctl blob;
4129     struct cmd_item *ti;
4130
4131     if (as->parms[0].items) {   /* -enable */
4132         flags |= AFSCALL_RXSTATS_ENABLE;
4133     }
4134     if (as->parms[1].items) {   /* -disable */
4135         flags |= AFSCALL_RXSTATS_DISABLE;
4136     }
4137     if (as->parms[2].items) {   /* -clear */
4138         flags |= AFSCALL_RXSTATS_CLEAR;
4139     }
4140     if (flags == 0) {
4141         fprintf(stderr, "You must specify at least one argument\n");
4142         return 1;
4143     }
4144
4145     blob.in = (char *)&flags;
4146     blob.in_size = sizeof(afs_int32);
4147     blob.out_size = 0;
4148
4149     code = pioctl(NULL, VIOC_RXSTAT_PEER, &blob, 1);
4150     if (code != 0) {
4151         Die(errno, NULL);
4152         return 1;
4153     }
4154
4155     return 0;
4156 }
4157
4158 static int
4159 GetFidCmd(struct cmd_syndesc *as, void *arock)
4160 {
4161     struct ViceIoctl blob;
4162     struct cmd_item *ti;
4163     for (ti = as->parms[0].items; ti; ti = ti->next) {
4164       struct VenusFid vfid;
4165       
4166       blob.out_size = sizeof(struct VenusFid);
4167       blob.out = (char *) &vfid;
4168       blob.in_size = 0;
4169       
4170       if (0 == pioctl(ti->data, VIOCGETFID, &blob, 1)) {
4171         printf("File %s (%u.%u.%u) contained in volume %u\n",
4172                ti->data, vfid.Fid.Volume, vfid.Fid.Vnode, vfid.Fid.Unique,
4173                vfid.Fid.Volume);
4174       }
4175     }
4176
4177     return 0;
4178 }
4179