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