fix-linux-build-20040415
[openafs.git] / src / afs / afs_pioctl.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/sysincludes.h"    /* Standard vendor system headers */
17 #ifdef AFS_OBSD_ENV
18 #include "h/syscallargs.h"
19 #endif
20 #ifdef AFS_FBSD50_ENV
21 #include "h/sysproto.h"
22 #endif
23 #include "afsincludes.h"        /* Afs-based standard headers */
24 #include "afs/afs_stats.h"      /* afs statistics */
25 #include "afs/vice.h"
26 #include "rx/rx_globals.h"
27
28 struct VenusFid afs_rootFid;
29 afs_int32 afs_waitForever = 0;
30 short afs_waitForeverCount = 0;
31 afs_int32 afs_showflags = GAGUSER | GAGCONSOLE; /* show all messages */
32
33 #define DECL_PIOCTL(x) static int x(struct vcache *avc, int afun, struct vrequest *areq, \
34         char *ain, char *aout, afs_int32 ainSize, afs_int32 *aoutSize, \
35         struct AFS_UCRED **acred)
36
37 /* Prototypes for pioctl routines */
38 DECL_PIOCTL(PGetFID);
39 DECL_PIOCTL(PSetAcl);
40 DECL_PIOCTL(PStoreBehind);
41 DECL_PIOCTL(PGCPAGs);
42 DECL_PIOCTL(PGetAcl);
43 DECL_PIOCTL(PNoop);
44 DECL_PIOCTL(PBogus);
45 DECL_PIOCTL(PGetFileCell);
46 DECL_PIOCTL(PGetWSCell);
47 DECL_PIOCTL(PGetUserCell);
48 DECL_PIOCTL(PSetTokens);
49 DECL_PIOCTL(PGetVolumeStatus);
50 DECL_PIOCTL(PSetVolumeStatus);
51 DECL_PIOCTL(PFlush);
52 DECL_PIOCTL(PNewStatMount);
53 DECL_PIOCTL(PGetTokens);
54 DECL_PIOCTL(PUnlog);
55 DECL_PIOCTL(PMariner);
56 DECL_PIOCTL(PCheckServers);
57 DECL_PIOCTL(PCheckVolNames);
58 DECL_PIOCTL(PCheckAuth);
59 DECL_PIOCTL(PFindVolume);
60 DECL_PIOCTL(PViceAccess);
61 DECL_PIOCTL(PSetCacheSize);
62 DECL_PIOCTL(PGetCacheSize);
63 DECL_PIOCTL(PRemoveCallBack);
64 DECL_PIOCTL(PNewCell);
65 DECL_PIOCTL(PNewAlias);
66 DECL_PIOCTL(PListCells);
67 DECL_PIOCTL(PListAliases);
68 DECL_PIOCTL(PRemoveMount);
69 DECL_PIOCTL(PVenusLogging);
70 DECL_PIOCTL(PGetCellStatus);
71 DECL_PIOCTL(PSetCellStatus);
72 DECL_PIOCTL(PFlushVolumeData);
73 DECL_PIOCTL(PGetVnodeXStatus);
74 DECL_PIOCTL(PSetSysName);
75 DECL_PIOCTL(PSetSPrefs);
76 DECL_PIOCTL(PSetSPrefs33);
77 DECL_PIOCTL(PGetSPrefs);
78 DECL_PIOCTL(PExportAfs);
79 DECL_PIOCTL(PGag);
80 DECL_PIOCTL(PTwiddleRx);
81 DECL_PIOCTL(PGetInitParams);
82 DECL_PIOCTL(PGetRxkcrypt);
83 DECL_PIOCTL(PSetRxkcrypt);
84 DECL_PIOCTL(PGetCPrefs);
85 DECL_PIOCTL(PSetCPrefs);
86 DECL_PIOCTL(PFlushMount);
87 DECL_PIOCTL(PRxStatProc);
88 DECL_PIOCTL(PRxStatPeer);
89 DECL_PIOCTL(PPrefetchFromTape);
90 DECL_PIOCTL(PResidencyCmd);
91
92 /*
93  * A macro that says whether we're going to need HandleClientContext().
94  * This is currently used only by the nfs translator.
95  */
96 #if !defined(AFS_NONFSTRANS) || defined(AFS_AIX_IAUTH_ENV)
97 #define AFS_NEED_CLIENTCONTEXT
98 #endif
99
100 /* Prototypes for private routines */
101 #ifdef AFS_NEED_CLIENTCONTEXT
102 static int HandleClientContext(struct afs_ioctl *ablob, int *com,
103                                struct AFS_UCRED **acred,
104                                struct AFS_UCRED *credp);
105 #endif
106 int HandleIoctl(register struct vcache *avc, register afs_int32 acom,
107                 struct afs_ioctl *adata);
108 int afs_HandlePioctl(struct vnode *avp, afs_int32 acom,
109                      register struct afs_ioctl *ablob, int afollow,
110                      struct AFS_UCRED **acred);
111 static int Prefetch(char *apath, struct afs_ioctl *adata, int afollow,
112                     struct AFS_UCRED *acred);
113
114
115 static int (*(VpioctlSw[])) () = {
116     PBogus,                     /* 0 */
117         PSetAcl,                /* 1 */
118         PGetAcl,                /* 2 */
119         PSetTokens,             /* 3 */
120         PGetVolumeStatus,       /* 4 */
121         PSetVolumeStatus,       /* 5 */
122         PFlush,                 /* 6 */
123         PBogus,                 /* 7 */
124         PGetTokens,             /* 8 */
125         PUnlog,                 /* 9 */
126         PCheckServers,          /* 10 */
127         PCheckVolNames,         /* 11 */
128         PCheckAuth,             /* 12 */
129         PBogus,                 /* 13 -- used to be quick check time */
130         PFindVolume,            /* 14 */
131         PBogus,                 /* 15 -- prefetch is now special-cased; see pioctl code! */
132         PBogus,                 /* 16 -- used to be testing code */
133         PNoop,                  /* 17 -- used to be enable group */
134         PNoop,                  /* 18 -- used to be disable group */
135         PBogus,                 /* 19 -- used to be list group */
136         PViceAccess,            /* 20 */
137         PUnlog,                 /* 21 -- unlog *is* unpag in this system */
138         PGetFID,                /* 22 -- get file ID */
139         PBogus,                 /* 23 -- used to be waitforever */
140         PSetCacheSize,          /* 24 */
141         PRemoveCallBack,        /* 25 -- flush only the callback */
142         PNewCell,               /* 26 */
143         PListCells,             /* 27 */
144         PRemoveMount,           /* 28 -- delete mount point */
145         PNewStatMount,          /* 29 -- new style mount point stat */
146         PGetFileCell,           /* 30 -- get cell name for input file */
147         PGetWSCell,             /* 31 -- get cell name for workstation */
148         PMariner,               /* 32 - set/get mariner host */
149         PGetUserCell,           /* 33 -- get cell name for user */
150         PVenusLogging,          /* 34 -- Enable/Disable logging */
151         PGetCellStatus,         /* 35 */
152         PSetCellStatus,         /* 36 */
153         PFlushVolumeData,       /* 37 -- flush all data from a volume */
154         PSetSysName,            /* 38 - Set system name */
155         PExportAfs,             /* 39 - Export Afs to remote nfs clients */
156         PGetCacheSize,          /* 40 - get cache size and usage */
157         PGetVnodeXStatus,       /* 41 - get vcache's special status */
158         PSetSPrefs33,           /* 42 - Set CM Server preferences... */
159         PGetSPrefs,             /* 43 - Get CM Server preferences... */
160         PGag,                   /* 44 - turn off/on all CM messages */
161         PTwiddleRx,             /* 45 - adjust some RX params       */
162         PSetSPrefs,             /* 46 - Set CM Server preferences... */
163         PStoreBehind,           /* 47 - set degree of store behind to be done */
164         PGCPAGs,                /* 48 - disable automatic pag gc-ing */
165         PGetInitParams,         /* 49 - get initial cm params */
166         PGetCPrefs,             /* 50 - get client interface addresses */
167         PSetCPrefs,             /* 51 - set client interface addresses */
168         PFlushMount,            /* 52 - flush mount symlink data */
169         PRxStatProc,            /* 53 - control process RX statistics */
170         PRxStatPeer,            /* 54 - control peer RX statistics */
171         PGetRxkcrypt,           /* 55 -- Get rxkad encryption flag */
172         PSetRxkcrypt,           /* 56 -- Set rxkad encryption flag */
173         PBogus,                 /* 57 -- arla: set file prio */
174         PBogus,                 /* 58 -- arla: fallback getfh */
175         PBogus,                 /* 59 -- arla: fallback fhopen */
176         PBogus,                 /* 60 -- arla: controls xfsdebug */
177         PBogus,                 /* 61 -- arla: controls arla debug */
178         PBogus,                 /* 62 -- arla: debug interface */
179         PBogus,                 /* 63 -- arla: print xfs status */
180         PBogus,                 /* 64 -- arla: force cache check */
181         PBogus,                 /* 65 -- arla: break callback */
182         PPrefetchFromTape,      /* 66 -- MR-AFS: prefetch file from tape */
183         PResidencyCmd,          /* 67 -- MR-AFS: generic commnd interface */
184         PBogus,                 /* 68 -- arla: fetch stats */
185 };
186
187 static int (*(CpioctlSw[])) () = {
188     PBogus,                     /* 0 */
189         PNewAlias,              /* 1 -- create new cell alias */
190         PListAliases,           /* 2 -- list cell aliases */
191 };
192
193 #define PSetClientContext 99    /*  Special pioctl to setup caller's creds  */
194 int afs_nobody = NFS_NOBODY;
195
196 static void
197 afs_ioctl32_to_afs_ioctl(const struct afs_ioctl32 *src, struct afs_ioctl *dst)
198 {
199     dst->in = (char *)(unsigned long)src->in;
200     dst->out = (char *)(unsigned long)src->out;
201     dst->in_size = src->in_size;
202     dst->out_size = src->out_size;
203 }
204
205 /*
206  * If you need to change copyin_afs_ioctl(), you may also need to change
207  * copyin_iparam().
208  */
209
210 static int
211 copyin_afs_ioctl(caddr_t cmarg, struct afs_ioctl *dst)
212 {
213     int code;
214 #if defined(AFS_AIX51_ENV) && defined(AFS_64BIT_KERNEL)
215     struct afs_ioctl32 dst32;
216
217     if (!(IS64U)) {
218         AFS_COPYIN(cmarg, (caddr_t) & dst32, sizeof dst32, code);
219         if (!code)
220             afs_ioctl32_to_afs_ioctl(&dst32, dst);
221         return code;
222     }
223 #endif /* defined(AFS_AIX51_ENV) && defined(AFS_64BIT_KERNEL) */
224
225
226 #if defined(AFS_HPUX_64BIT_ENV)
227     struct afs_ioctl32 dst32;
228
229     if (is_32bit(u.u_procp)) {  /* is_32bit() in proc_iface.h */
230         AFS_COPYIN(cmarg, (caddr_t) & dst32, sizeof dst32, code);
231         if (!code)
232             afs_ioctl32_to_afs_ioctl(&dst32, dst);
233         return code;
234     }
235 #endif /* defined(AFS_HPUX_64BIT_ENV) */
236
237 #if defined(AFS_SUN57_64BIT_ENV)
238     struct afs_ioctl32 dst32;
239
240     if (get_udatamodel() == DATAMODEL_ILP32) {
241         AFS_COPYIN(cmarg, (caddr_t) & dst32, sizeof dst32, code);
242         if (!code)
243             afs_ioctl32_to_afs_ioctl(&dst32, dst);
244         return code;
245     }
246 #endif /* defined(AFS_SUN57_64BIT_ENV) */
247
248 #if defined(AFS_SGI_ENV) && (_MIPS_SZLONG==64)
249     struct afs_ioctl32 dst32;
250
251     if (!ABI_IS_64BIT(get_current_abi())) {
252         AFS_COPYIN(cmarg, (caddr_t) & dst32, sizeof dst32, code);
253         if (!code)
254             afs_ioctl32_to_afs_ioctl(&dst32, dst);
255         return code;
256     }
257 #endif /* defined(AFS_SGI_ENV) && (_MIPS_SZLONG==64) */
258
259 #if defined(AFS_LINUX_64BIT_KERNEL) && !defined(AFS_ALPHA_LINUX20_ENV) && !defined(AFS_IA64_LINUX20_ENV) && !defined(AFS_AMD64_LINUX20_ENV)
260     struct afs_ioctl32 dst32;
261
262 #ifdef AFS_SPARC64_LINUX24_ENV
263     if (current->thread.flags & SPARC_FLAG_32BIT)
264 #elif defined(AFS_SPARC64_LINUX20_ENV)
265     if (current->tss.flags & SPARC_FLAG_32BIT)
266 #elif defined(AFS_AMD64_LINUX20_ENV)
267     if (current->thread.flags & THREAD_IA32)
268 #elif defined(AFS_PPC64_LINUX20_ENV)
269     if (current->thread.flags & PPC_FLAG_32BIT)
270 #else
271 #error Not done for this linux type
272 #endif
273     {
274         AFS_COPYIN(cmarg, (caddr_t) & dst32, sizeof dst32, code);
275         if (!code)
276             afs_ioctl32_to_afs_ioctl(&dst32, dst);
277         return code;
278     }
279 #endif /* defined(AFS_LINUX_64BIT_KERNEL) */
280
281     AFS_COPYIN(cmarg, (caddr_t) dst, sizeof *dst, code);
282     return code;
283 }
284
285 int
286 HandleIoctl(register struct vcache *avc, register afs_int32 acom,
287             struct afs_ioctl *adata)
288 {
289     register afs_int32 code;
290
291     code = 0;
292     AFS_STATCNT(HandleIoctl);
293
294     switch (acom & 0xff) {
295     case 1:
296         avc->states |= CSafeStore;
297         avc->asynchrony = 0;
298         break;
299
300         /* case 2 used to be abort store, but this is no longer provided,
301          * since it is impossible to implement under normal Unix.
302          */
303
304     case 3:{
305             /* return the name of the cell this file is open on */
306             register struct cell *tcell;
307             register afs_int32 i;
308
309             tcell = afs_GetCell(avc->fid.Cell, READ_LOCK);
310             if (tcell) {
311                 i = strlen(tcell->cellName) + 1;        /* bytes to copy out */
312
313                 if (i > adata->out_size) {
314                     /* 0 means we're not interested in the output */
315                     if (adata->out_size != 0)
316                         code = EFAULT;
317                 } else {
318                     /* do the copy */
319                     AFS_COPYOUT(tcell->cellName, adata->out, i, code);
320                 }
321                 afs_PutCell(tcell, READ_LOCK);
322             } else
323                 code = ENOTTY;
324         }
325         break;
326
327     case 49:                    /* VIOC_GETINITPARAMS */
328         if (adata->out_size < sizeof(struct cm_initparams)) {
329             code = EFAULT;
330         } else {
331             AFS_COPYOUT(&cm_initParams, adata->out,
332                         sizeof(struct cm_initparams), code);
333         }
334         break;
335
336     default:
337
338         code = EINVAL;
339 #ifdef AFS_AIX51_ENV
340         code = ENOSYS;
341 #endif
342         break;
343     }
344     return code;                /* so far, none implemented */
345 }
346
347
348 #ifdef  AFS_AIX_ENV
349 /* For aix we don't temporarily bypass ioctl(2) but rather do our
350  * thing directly in the vnode layer call, VNOP_IOCTL; thus afs_ioctl
351  * is now called from afs_gn_ioctl.
352  */
353 int
354 afs_ioctl(struct vcache *tvc, int cmd, int arg)
355 {
356     struct afs_ioctl data;
357     int error = 0;
358
359     AFS_STATCNT(afs_ioctl);
360     if (((cmd >> 8) & 0xff) == 'V') {
361         /* This is a VICEIOCTL call */
362         AFS_COPYIN(arg, (caddr_t) & data, sizeof(data), error);
363         if (error)
364             return (error);
365         error = HandleIoctl(tvc, cmd, &data);
366         return (error);
367     } else {
368         /* No-op call; just return. */
369         return (ENOTTY);
370     }
371 }
372 #endif /* AFS_AIX_ENV */
373
374 #if defined(AFS_SGI_ENV)
375 afs_ioctl(OSI_VN_DECL(tvc), int cmd, void *arg, int flag, cred_t * cr,
376           rval_t * rvalp
377 #ifdef AFS_SGI65_ENV
378           , struct vopbd * vbds
379 #endif
380     )
381 {
382     struct afs_ioctl data;
383     int error = 0;
384     int locked;
385
386     OSI_VN_CONVERT(tvc);
387
388     AFS_STATCNT(afs_ioctl);
389     if (((cmd >> 8) & 0xff) == 'V') {
390         /* This is a VICEIOCTL call */
391         error = copyin_afs_ioctl(arg, &data);
392         if (error)
393             return (error);
394         locked = ISAFS_GLOCK();
395         if (!locked)
396             AFS_GLOCK();
397         error = HandleIoctl(tvc, cmd, &data);
398         if (!locked)
399             AFS_GUNLOCK();
400         return (error);
401     } else {
402         /* No-op call; just return. */
403         return (ENOTTY);
404     }
405 }
406 #endif /* AFS_SGI_ENV */
407
408 /* unlike most calls here, this one uses u.u_error to return error conditions,
409    since this is really an intercepted chapter 2 call, rather than a vnode
410    interface call.
411    */
412 /* AFS_HPUX102 and up uses VNODE ioctl instead */
413 #ifndef AFS_HPUX102_ENV
414 #if !defined(AFS_SGI_ENV)
415 #ifdef  AFS_AIX32_ENV
416 #ifdef AFS_AIX51_ENV
417 #ifdef __64BIT__
418 kioctl(fdes, com, arg, ext, arg2, arg3)
419 #else /* __64BIT__ */
420 kioctl32(fdes, com, arg, ext, arg2, arg3)
421 #endif /* __64BIT__ */
422      caddr_t arg2, arg3;
423 #else
424 kioctl(fdes, com, arg, ext)
425 #endif
426      int fdes, com;
427      caddr_t arg, ext;
428 {
429     struct a {
430         int fd, com;
431         caddr_t arg, ext;
432 #ifdef AFS_AIX51_ENV
433         caddr_t arg2, arg3;
434 #endif
435     } u_uap, *uap = &u_uap;
436 #else
437 #if defined(AFS_SUN5_ENV)
438
439 struct afs_ioctl_sys {
440     int fd;
441     int com;
442     int arg;
443 };
444
445 afs_xioctl(uap, rvp)
446      struct afs_ioctl_sys *uap;
447      rval_t *rvp;
448 {
449 #elif defined(AFS_OSF_ENV)
450 afs_xioctl(p, args, retval)
451      struct proc *p;
452      void *args;
453      long *retval;
454 {
455     struct a {
456         long fd;
457         u_long com;
458         caddr_t arg;
459     } *uap = (struct a *)args;
460 #elif defined(AFS_FBSD50_ENV)
461 #define arg data
462 int
463 afs_xioctl(td, uap, retval)
464      struct thread *td;
465      register struct ioctl_args *uap;
466      register_t *retval;
467 {
468     struct proc *p = td->td_proc;
469 #elif defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
470 struct ioctl_args {
471     int fd;
472     u_long com;
473     caddr_t arg;
474 };
475
476 int
477 afs_xioctl(p, uap, retval)
478      struct proc *p;
479      register struct ioctl_args *uap;
480      register_t *retval;
481 {
482 #elif defined(AFS_LINUX22_ENV)
483 struct afs_ioctl_sys {
484     unsigned int com;
485     unsigned long arg;
486 };
487 asmlinkage int
488 afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
489            unsigned long arg)
490 {
491     struct afs_ioctl_sys ua, *uap = &ua;
492 #else
493 int
494 afs_xioctl(void)
495 {
496     register struct a {
497         int fd;
498         int com;
499         caddr_t arg;
500     } *uap = (struct a *)u.u_ap;
501 #endif /* AFS_SUN5_ENV */
502 #endif
503 #if defined(AFS_AIX32_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV)
504     struct file *fd;
505 #elif !defined(AFS_LINUX22_ENV)
506     register struct file *fd;
507 #endif
508 #if defined(AFS_XBSD_ENV)
509     register struct filedesc *fdp;
510 #endif
511     register struct vcache *tvc;
512     register int ioctlDone = 0, code = 0;
513
514     AFS_STATCNT(afs_xioctl);
515 #if defined(AFS_XBSD_ENV)
516     fdp = p->p_fd;
517     if ((u_int) uap->fd >= fdp->fd_nfiles
518         || (fd = fdp->fd_ofiles[uap->fd]) == NULL)
519         return EBADF;
520     if ((fd->f_flag & (FREAD | FWRITE)) == 0)
521         return EBADF;
522 #elif defined(AFS_DARWIN_ENV)
523     if ((code = fdgetf(p, uap->fd, &fd)))
524         return code;
525 #elif defined(AFS_LINUX22_ENV)
526     ua.com = com;
527     ua.arg = arg;
528 #elif defined(AFS_AIX32_ENV)
529     uap->fd = fdes;
530     uap->com = com;
531     uap->arg = arg;
532 #ifdef AFS_AIX51_ENV
533     uap->arg2 = arg2;
534     uap->arg3 = arg3;
535 #endif
536     if (setuerror(getf(uap->fd, &fd))) {
537         return -1;
538     }
539 #elif defined(AFS_OSF_ENV)
540     fd = NULL;
541     if (code = getf(&fd, uap->fd, FILE_FLAGS_NULL, &u.u_file_state))
542         return code;
543 #elif defined(AFS_SUN5_ENV)
544 # if defined(AFS_SUN57_ENV)
545     fd = getf(uap->fd);
546     if (!fd)
547         return (EBADF);
548 # elif defined(AFS_SUN54_ENV)
549     fd = GETF(uap->fd);
550     if (!fd)
551         return (EBADF);
552 # else
553     if (code = getf(uap->fd, &fd)) {
554         return (code);
555     }
556 # endif /* AFS_SUN57_ENV */
557 #else
558     fd = getf(uap->fd);
559     if (!fd)
560         return (EBADF);
561 #endif
562     /* first determine whether this is any sort of vnode */
563 #if defined(AFS_LINUX22_ENV)
564     tvc = VTOAFS(ip);
565     {
566 #else
567 #ifdef AFS_SUN5_ENV
568     if (fd->f_vnode->v_type == VREG || fd->f_vnode->v_type == VDIR) {
569 #else
570     if (fd->f_type == DTYPE_VNODE) {
571 #endif
572         /* good, this is a vnode; next see if it is an AFS vnode */
573 #if     defined(AFS_AIX32_ENV) || defined(AFS_SUN5_ENV)
574         tvc = VTOAFS(fd->f_vnode);      /* valid, given a vnode */
575 #elif defined(AFS_OBSD_ENV)
576         tvc =
577             IsAfsVnode((struct vnode *)fd->
578                        f_data) ? VTOAFS((struct vnode *)fd->f_data) : NULL;
579 #else
580         tvc = VTOAFS((struct vnode *)fd->f_data);       /* valid, given a vnode */
581 #endif
582 #endif /* AFS_LINUX22_ENV */
583         if (tvc && IsAfsVnode(AFSTOV(tvc))) {
584 #ifdef AFS_DEC_ENV
585             tvc = VTOAFS(afs_gntovn((struct gnode *)tvc));
586             if (!tvc) {         /* shouldn't happen with held gnodes */
587                 u.u_error = ENOENT;
588                 return;
589             }
590 #endif
591             /* This is an AFS vnode */
592             if (((uap->com >> 8) & 0xff) == 'V') {
593                 register struct afs_ioctl *datap;
594                 AFS_GLOCK();
595                 datap =
596                     (struct afs_ioctl *)osi_AllocSmallSpace(AFS_SMALLOCSIZ);
597                 AFS_COPYIN((char *)uap->arg, (caddr_t) datap,
598                            sizeof(struct afs_ioctl), code);
599                 if (code) {
600                     osi_FreeSmallSpace(datap);
601                     AFS_GUNLOCK();
602 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
603                     return code;
604 #else
605 #if     defined(AFS_SUN5_ENV)
606 #ifdef  AFS_SUN54_ENV
607                     releasef(uap->fd);
608 #else
609                     releasef(fd);
610 #endif
611                     return (EFAULT);
612 #else
613 #ifdef  AFS_OSF_ENV
614 #ifdef  AFS_OSF30_ENV
615                     FP_UNREF_ALWAYS(fd);
616 #else
617                     FP_UNREF(fd);
618 #endif
619                     return code;
620 #else /* AFS_OSF_ENV */
621 #ifdef  AFS_AIX41_ENV
622                     ufdrele(uap->fd);
623 #endif
624 #ifdef AFS_LINUX22_ENV
625                     return -code;
626 #else
627                     setuerror(code);
628                     return;
629 #endif
630 #endif
631 #endif
632 #endif
633                 }
634                 code = HandleIoctl(tvc, uap->com, datap);
635                 osi_FreeSmallSpace(datap);
636                 AFS_GUNLOCK();
637                 ioctlDone = 1;
638 #ifdef  AFS_AIX41_ENV
639                 ufdrele(uap->fd);
640 #endif
641 #ifdef  AFS_OSF_ENV
642 #ifdef  AFS_OSF30_ENV
643                 FP_UNREF_ALWAYS(fd);
644 #else
645                 FP_UNREF(fd);
646 #endif
647 #endif
648             }
649 #if defined(AFS_LINUX22_ENV)
650             else
651                 code = EINVAL;
652 #endif
653         }
654     }
655
656     if (!ioctlDone) {
657 #ifdef  AFS_AIX41_ENV
658         ufdrele(uap->fd);
659 #ifdef AFS_AIX51_ENV
660 #ifdef __64BIT__
661         code = okioctl(fdes, com, arg, ext, arg2, arg3);
662 #else /* __64BIT__ */
663         code = okioctl32(fdes, com, arg, ext, arg2, arg3);
664 #endif /* __64BIT__ */
665 #else /* !AFS_AIX51_ENV */
666         code = okioctl(fdes, com, arg, ext);
667 #endif /* AFS_AIX51_ENV */
668         return code;
669 #else /* !AFS_AIX41_ENV */
670 #ifdef  AFS_AIX32_ENV
671         okioctl(fdes, com, arg, ext);
672 #elif defined(AFS_SUN5_ENV)
673 #if defined(AFS_SUN57_ENV)
674         releasef(uap->fd);
675 #elif defined(AFS_SUN54_ENV)
676         RELEASEF(uap->fd);
677 #else
678         releasef(fd);
679 #endif
680         code = ioctl(uap, rvp);
681 #elif defined(AFS_FBSD50_ENV)
682         return ioctl(td, uap);
683 #elif defined(AFS_FBSD_ENV)
684         return ioctl(p, uap);
685 #elif defined(AFS_OBSD_ENV)
686         code = sys_ioctl(p, uap, retval);
687 #elif defined(AFS_DARWIN_ENV)
688         return ioctl(p, uap, retval);
689 #elif defined(AFS_OSF_ENV)
690         code = ioctl(p, args, retval);
691 #ifdef  AFS_OSF30_ENV
692         FP_UNREF_ALWAYS(fd);
693 #else
694         FP_UNREF(fd);
695 #endif
696         return code;
697 #elif !defined(AFS_LINUX22_ENV)
698         ioctl();
699 #endif
700 #endif
701     }
702 #ifdef  AFS_SUN5_ENV
703     if (ioctlDone)
704 #ifdef  AFS_SUN54_ENV
705         releasef(uap->fd);
706 #else
707         releasef(fd);
708 #endif
709     return (code);
710 #else
711 #ifdef AFS_LINUX22_ENV
712     return -code;
713 #else
714 #if defined(KERNEL_HAVE_UERROR)
715     if (!getuerror())
716         setuerror(code);
717 #if     defined(AFS_AIX32_ENV) && !defined(AFS_AIX41_ENV)
718     return (getuerror()? -1 : u.u_ioctlrv);
719 #else
720     return getuerror()? -1 : 0;
721 #endif
722 #endif
723 #endif /* AFS_LINUX22_ENV */
724 #endif /* AFS_SUN5_ENV */
725 #if defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
726     return (code);
727 #endif
728 }
729 #endif /* AFS_SGI_ENV */
730 #endif /* AFS_HPUX102_ENV */
731
732 #if defined(AFS_SGI_ENV)
733   /* "pioctl" system call entry point; just pass argument to the parameterized
734    * call below */
735 struct pioctlargs {
736     char *path;
737     sysarg_t cmd;
738     caddr_t cmarg;
739     sysarg_t follow;
740 };
741 int
742 afs_pioctl(struct pioctlargs *uap, rval_t * rvp)
743 {
744     int code;
745
746     AFS_STATCNT(afs_pioctl);
747     AFS_GLOCK();
748     code = afs_syscall_pioctl(uap->path, uap->cmd, uap->cmarg, uap->follow);
749     AFS_GUNLOCK();
750 #ifdef AFS_SGI64_ENV
751     return code;
752 #else
753     return u.u_error;
754 #endif
755 }
756
757 #elif defined(AFS_OSF_ENV)
758 afs_pioctl(p, args, retval)
759      struct proc *p;
760      void *args;
761      int *retval;
762 {
763     struct a {
764         char *path;
765         int cmd;
766         caddr_t cmarg;
767         int follow;
768     } *uap = (struct a *)args;
769
770     AFS_STATCNT(afs_pioctl);
771     return (afs_syscall_pioctl(uap->path, uap->cmd, uap->cmarg, uap->follow));
772 }
773
774 #elif defined(AFS_FBSD50_ENV)
775 int
776 afs_pioctl(td, args, retval)
777      struct thread *td;
778      void *args;
779      int *retval;
780 {
781     struct a {
782         char *path;
783         int cmd;
784         caddr_t cmarg;
785         int follow;
786     } *uap = (struct a *)args;
787
788     AFS_STATCNT(afs_pioctl);
789     return (afs_syscall_pioctl
790             (uap->path, uap->cmd, uap->cmarg, uap->follow, td->td_ucred));
791 }
792
793 #elif defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
794 int
795 afs_pioctl(p, args, retval)
796      struct proc *p;
797      void *args;
798      int *retval;
799 {
800     struct a {
801         char *path;
802         int cmd;
803         caddr_t cmarg;
804         int follow;
805     } *uap = (struct a *)args;
806
807     AFS_STATCNT(afs_pioctl);
808     return (afs_syscall_pioctl
809             (uap->path, uap->cmd, uap->cmarg, uap->follow,
810              p->p_cred->pc_ucred));
811 }
812
813 #endif
814
815 /* macro to avoid adding any more #ifdef's to pioctl code. */
816 #if defined(AFS_LINUX22_ENV) || defined(AFS_AIX41_ENV)
817 #define PIOCTL_FREE_CRED() crfree(credp)
818 #else
819 #define PIOCTL_FREE_CRED()
820 #endif
821
822 int
823 #ifdef  AFS_SUN5_ENV
824 afs_syscall_pioctl(path, com, cmarg, follow, rvp, credp)
825      rval_t *rvp;
826      struct AFS_UCRED *credp;
827 #else
828 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
829 afs_syscall_pioctl(path, com, cmarg, follow, credp)
830      struct AFS_UCRED *credp;
831 #else
832 afs_syscall_pioctl(path, com, cmarg, follow)
833 #endif
834 #endif
835      char *path;
836      unsigned int com;
837      caddr_t cmarg;
838      int follow;
839 {
840     struct afs_ioctl data;
841     struct AFS_UCRED *tmpcred, *foreigncreds = NULL;
842     register afs_int32 code = 0;
843     struct vnode *vp;
844 #ifdef AFS_DEC_ENV
845     struct vnode *gp;
846 #endif
847 #ifdef  AFS_AIX41_ENV
848     struct ucred *credp = crref();      /* don't free until done! */
849 #endif
850 #ifdef AFS_LINUX22_ENV
851     cred_t *credp = crref();    /* don't free until done! */
852     struct dentry *dp;
853 #endif
854
855     AFS_STATCNT(afs_syscall_pioctl);
856     if (follow)
857         follow = 1;             /* compat. with old venus */
858     code = copyin_afs_ioctl(cmarg, &data);
859     if (code) {
860         PIOCTL_FREE_CRED();
861 #if defined(KERNEL_HAVE_UERROR)
862         setuerror(code);
863 #endif
864         return (code);
865     }
866     if ((com & 0xff) == PSetClientContext) {
867 #ifdef AFS_NEED_CLIENTCONTEXT
868 #if defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV)
869         code = HandleClientContext(&data, &com, &foreigncreds, credp);
870 #else
871         code = HandleClientContext(&data, &com, &foreigncreds, osi_curcred());
872 #endif
873         if (code) {
874             if (foreigncreds) {
875                 crfree(foreigncreds);
876             }
877             PIOCTL_FREE_CRED();
878 #if defined(KERNEL_HAVE_UERROR)
879             return (setuerror(code), code);
880 #else
881             return (code);
882 #endif
883         }
884 #else /* AFS_NEED_CLIENTCONTEXT */
885         return EINVAL;
886 #endif /* AFS_NEED_CLIENTCONTEXT */
887     }
888 #ifdef AFS_NEED_CLIENTCONTEXT
889     if (foreigncreds) {
890         /*
891          * We could have done without temporary setting the u.u_cred below
892          * (foreigncreds could be passed as param the pioctl modules)
893          * but calls such as afs_osi_suser() doesn't allow that since it
894          * references u.u_cred directly.  We could, of course, do something
895          * like afs_osi_suser(cred) which, I think, is better since it
896          * generalizes and supports multi cred environments...
897          */
898 #ifdef  AFS_SUN5_ENV
899         tmpcred = credp;
900         credp = foreigncreds;
901 #elif defined(AFS_AIX41_ENV)
902         tmpcred = crref();      /* XXX */
903         crset(foreigncreds);
904 #elif defined(AFS_HPUX101_ENV)
905         tmpcred = p_cred(u.u_procp);
906         set_p_cred(u.u_procp, foreigncreds);
907 #elif defined(AFS_SGI_ENV)
908         tmpcred = OSI_GET_CURRENT_CRED();
909         OSI_SET_CURRENT_CRED(foreigncreds);
910 #else
911         tmpcred = u.u_cred;
912         u.u_cred = foreigncreds;
913 #endif
914     }
915 #endif /* AFS_NEED_CLIENTCONTEXT */
916     if ((com & 0xff) == 15) {
917         /* special case prefetch so entire pathname eval occurs in helper process.
918          * otherwise, the pioctl call is essentially useless */
919 #if     defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV) || defined(AFS_LINUX22_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
920         code =
921             Prefetch(path, &data, follow,
922                      foreigncreds ? foreigncreds : credp);
923 #else
924         code = Prefetch(path, &data, follow, osi_curcred());
925 #endif
926         vp = NULL;
927 #if defined(KERNEL_HAVE_UERROR)
928         setuerror(code);
929 #endif
930         goto rescred;
931     }
932     if (path) {
933         AFS_GUNLOCK();
934 #ifdef  AFS_AIX41_ENV
935         code =
936             lookupname(path, USR, follow, NULL, &vp,
937                        foreigncreds ? foreigncreds : credp);
938 #else
939 #ifdef AFS_LINUX22_ENV
940         code = gop_lookupname(path, AFS_UIOUSER, follow, NULL, &dp);
941         if (!code)
942             vp = (struct vnode *)dp->d_inode;
943 #else
944         code = gop_lookupname(path, AFS_UIOUSER, follow, NULL, &vp);
945 #endif /* AFS_LINUX22_ENV */
946 #endif /* AFS_AIX41_ENV */
947         AFS_GLOCK();
948         if (code) {
949             vp = NULL;
950 #if defined(KERNEL_HAVE_UERROR)
951             setuerror(code);
952 #endif
953             goto rescred;
954         }
955     } else
956         vp = NULL;
957
958     /* now make the call if we were passed no file, or were passed an AFS file */
959     if (!vp || IsAfsVnode(vp)) {
960 #if defined(AFS_DEC_ENV)
961         /* Ultrix 4.0: can't get vcache entry unless we've got an AFS gnode.
962          * So, we must test in this part of the code.  Also, must arrange to
963          * GRELE the original gnode pointer when we're done, since in Ultrix 4.0,
964          * we hold gnodes, whose references hold our vcache entries.
965          */
966         if (vp) {
967             gp = vp;            /* remember for "put" */
968             vp = (struct vnode *)afs_gntovn(vp);        /* get vcache from gp */
969         } else
970             gp = NULL;
971 #elif defined(AFS_SUN5_ENV)
972         code = afs_HandlePioctl(vp, com, &data, follow, &credp);
973 #elif defined(AFS_AIX41_ENV)
974         {
975             struct ucred *cred1, *cred2;
976
977             if (foreigncreds) {
978                 cred1 = cred2 = foreigncreds;
979             } else {
980                 cred1 = cred2 = credp;
981             }
982             code = afs_HandlePioctl(vp, com, &data, follow, &cred1);
983             if (cred1 != cred2) {
984                 /* something changed the creds */
985                 crset(cred1);
986             }
987         }
988 #elif defined(AFS_HPUX101_ENV)
989         {
990             struct ucred *cred = p_cred(u.u_procp);
991             code = afs_HandlePioctl(vp, com, &data, follow, &cred);
992         }
993 #elif defined(AFS_SGI_ENV)
994         {
995             struct cred *credp;
996             credp = OSI_GET_CURRENT_CRED();
997             code = afs_HandlePioctl(vp, com, &data, follow, &credp);
998         }
999 #elif defined(AFS_LINUX22_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
1000         code = afs_HandlePioctl(vp, com, &data, follow, &credp);
1001 #else
1002         code = afs_HandlePioctl(vp, com, &data, follow, &u.u_cred);
1003 #endif
1004     } else {
1005 #if defined(KERNEL_HAVE_UERROR)
1006         setuerror(EINVAL);
1007 #else
1008         code = EINVAL;          /* not in /afs */
1009 #endif
1010 #ifdef AFS_DEC_ENV
1011         if (vp) {
1012             GRELE(vp);
1013             vp = NULL;
1014         }
1015 #endif
1016     }
1017
1018   rescred:
1019 #if defined(AFS_NEED_CLIENTCONTEXT)
1020     if (foreigncreds) {
1021 #ifdef  AFS_AIX41_ENV
1022         crset(tmpcred);         /* restore original credentials */
1023 #else
1024 #if     defined(AFS_HPUX101_ENV)
1025         set_p_cred(u.u_procp, tmpcred); /* restore original credentials */
1026 #elif   defined(AFS_SGI_ENV)
1027         OSI_SET_CURRENT_CRED(tmpcred);  /* restore original credentials */
1028 #elif   !defined(AFS_SUN5_ENV)
1029         osi_curcred() = tmpcred;        /* restore original credentials */
1030 #endif /* AFS_HPUX101_ENV */
1031         crfree(foreigncreds);
1032 #endif /* AIX41 */
1033     }
1034 #endif /* AFS_NEED_CLIENTCONTEXT */
1035     if (vp) {
1036 #ifdef AFS_LINUX22_ENV
1037         dput(dp);
1038 #else
1039         AFS_RELE(vp);           /* put vnode back */
1040 #endif
1041     }
1042     PIOCTL_FREE_CRED();
1043 #if defined(KERNEL_HAVE_UERROR)
1044     if (!getuerror())
1045         setuerror(code);
1046     return (getuerror());
1047 #else
1048     return (code);
1049 #endif
1050 }
1051
1052 #define MAXPIOCTLTOKENLEN \
1053 (3*sizeof(afs_int32)+MAXKTCTICKETLEN+sizeof(struct ClearToken)+MAXKTCREALMLEN)
1054
1055 int
1056 afs_HandlePioctl(struct vnode *avp, afs_int32 acom,
1057                  register struct afs_ioctl *ablob, int afollow,
1058                  struct AFS_UCRED **acred)
1059 {
1060     struct vcache *avc;
1061     struct vrequest treq;
1062     register afs_int32 code;
1063     register afs_int32 function, device;
1064     afs_int32 inSize, outSize, outSizeMax;
1065     char *inData, *outData;
1066     int (*(*pioctlSw)) ();
1067     int pioctlSwSize;
1068     struct afs_fakestat_state fakestate;
1069
1070     avc = avp ? VTOAFS(avp) : NULL;
1071     afs_Trace3(afs_iclSetp, CM_TRACE_PIOCTL, ICL_TYPE_INT32, acom & 0xff,
1072                ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, afollow);
1073     AFS_STATCNT(HandlePioctl);
1074     if ((code = afs_InitReq(&treq, *acred)))
1075         return code;
1076     afs_InitFakeStat(&fakestate);
1077     if (avc) {
1078         code = afs_EvalFakeStat(&avc, &fakestate, &treq);
1079         if (code) {
1080             afs_PutFakeStat(&fakestate);
1081             return code;
1082         }
1083     }
1084     device = (acom & 0xff00) >> 8;
1085     switch (device) {
1086     case 'V':                   /* Original pioctls */
1087         pioctlSw = VpioctlSw;
1088         pioctlSwSize = sizeof(VpioctlSw);
1089         break;
1090     case 'C':                   /* Coordinated/common pioctls */
1091         pioctlSw = CpioctlSw;
1092         pioctlSwSize = sizeof(CpioctlSw);
1093         break;
1094     default:
1095         afs_PutFakeStat(&fakestate);
1096         return EINVAL;
1097     }
1098     function = acom & 0xff;
1099     if (function >= (pioctlSwSize / sizeof(char *))) {
1100         afs_PutFakeStat(&fakestate);
1101         return EINVAL;          /* out of range */
1102     }
1103     inSize = ablob->in_size;
1104
1105     /* Do all range checking before continuing */
1106     if (inSize > MAXPIOCTLTOKENLEN || inSize < 0 || ablob->out_size < 0)
1107         return E2BIG;
1108
1109     if (inSize > AFS_LRALLOCSIZ) {
1110         inData = osi_AllocLargeSpace(inSize+1);
1111     } else {
1112         inData = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
1113     }
1114     if (!inData)
1115         return ENOMEM;
1116     if (inSize > 0) {
1117         AFS_COPYIN(ablob->in, inData, inSize, code);
1118         inData[inSize] = '\0';
1119     } else
1120         code = 0;
1121     if (code) {
1122     if (inSize > AFS_LRALLOCSIZ) {
1123         osi_Free(inData, inSize+1);
1124     } else {
1125         osi_FreeLargeSpace(inData);
1126     }
1127     afs_PutFakeStat(&fakestate);
1128     return code;
1129     }
1130     if (function == 8 && device == 'V') { /* PGetTokens */
1131         outSizeMax = MAXPIOCTLTOKENLEN;
1132         outData = osi_Alloc(outSizeMax);
1133     } else {
1134         outSizeMax = AFS_LRALLOCSIZ;
1135         outData = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
1136     }
1137     if (!outData) {
1138         if (inSize > AFS_LRALLOCSIZ) {
1139             osi_Free(inData, inSize+1);
1140         } else {
1141             osi_FreeLargeSpace(inData);
1142         }
1143         return ENOMEM;
1144     }
1145     outSize = 0;
1146     code =
1147         (*pioctlSw[function]) (avc, function, &treq, inData, outData, inSize,
1148                                &outSize, acred);
1149     if (inSize > AFS_LRALLOCSIZ) {
1150         osi_Free(inData, inSize+1);
1151     } else {
1152         osi_FreeLargeSpace(inData);
1153     }
1154     if (code == 0 && ablob->out_size > 0) {
1155         if (outSize > ablob->out_size) {
1156             code = E2BIG; /* data wont fit in user buffer */
1157         } else if (outSize) {
1158             AFS_COPYOUT(outData, ablob->out, outSize, code);
1159         }
1160     }
1161     if (outSizeMax > AFS_LRALLOCSIZ) {
1162         osi_Free(outData, outSizeMax);
1163     } else {
1164         osi_FreeLargeSpace(outData);
1165     }
1166     afs_PutFakeStat(&fakestate);
1167     return afs_CheckCode(code, &treq, 41);
1168 }
1169
1170 DECL_PIOCTL(PGetFID)
1171 {
1172     AFS_STATCNT(PGetFID);
1173     if (!avc)
1174         return EINVAL;
1175     memcpy(aout, (char *)&avc->fid, sizeof(struct VenusFid));
1176     *aoutSize = sizeof(struct VenusFid);
1177     return 0;
1178 }
1179
1180 DECL_PIOCTL(PSetAcl)
1181 {
1182     register afs_int32 code;
1183     struct conn *tconn;
1184     struct AFSOpaque acl;
1185     struct AFSVolSync tsync;
1186     struct AFSFetchStatus OutStatus;
1187     XSTATS_DECLS;
1188
1189     AFS_STATCNT(PSetAcl);
1190     if (!avc)
1191         return EINVAL;
1192     if ((acl.AFSOpaque_len = strlen(ain) + 1) > 1000)
1193         return EINVAL;
1194
1195     acl.AFSOpaque_val = ain;
1196     do {
1197         tconn = afs_Conn(&avc->fid, areq, SHARED_LOCK);
1198         if (tconn) {
1199             XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STOREACL);
1200             RX_AFS_GUNLOCK();
1201             code =
1202                 RXAFS_StoreACL(tconn->id, (struct AFSFid *)&avc->fid.Fid,
1203                                &acl, &OutStatus, &tsync);
1204             RX_AFS_GLOCK();
1205             XSTATS_END_TIME;
1206         } else
1207             code = -1;
1208     } while (afs_Analyze
1209              (tconn, code, &avc->fid, areq, AFS_STATS_FS_RPCIDX_STOREACL,
1210               SHARED_LOCK, NULL));
1211
1212     /* now we've forgotten all of the access info */
1213     ObtainWriteLock(&afs_xcbhash, 455);
1214     avc->callback = 0;
1215     afs_DequeueCallback(avc);
1216     avc->states &= ~(CStatd | CUnique);
1217     ReleaseWriteLock(&afs_xcbhash);
1218     if (avc->fid.Fid.Vnode & 1 || (vType(avc) == VDIR))
1219         osi_dnlc_purgedp(avc);
1220     return code;
1221 }
1222
1223 int afs_defaultAsynchrony = 0;
1224
1225 DECL_PIOCTL(PStoreBehind)
1226 {
1227     afs_int32 code = 0;
1228     struct sbstruct *sbr;
1229
1230     sbr = (struct sbstruct *)ain;
1231     if (sbr->sb_default != -1) {
1232         if (afs_osi_suser(*acred))
1233             afs_defaultAsynchrony = sbr->sb_default;
1234         else
1235             code = EPERM;
1236     }
1237
1238     if (avc && (sbr->sb_thisfile != -1)) {
1239         if (afs_AccessOK
1240             (avc, PRSFS_WRITE | PRSFS_ADMINISTER, areq, DONT_CHECK_MODE_BITS))
1241             avc->asynchrony = sbr->sb_thisfile;
1242         else
1243             code = EACCES;
1244     }
1245
1246     *aoutSize = sizeof(struct sbstruct);
1247     sbr = (struct sbstruct *)aout;
1248     sbr->sb_default = afs_defaultAsynchrony;
1249     if (avc) {
1250         sbr->sb_thisfile = avc->asynchrony;
1251     }
1252
1253     return code;
1254 }
1255
1256 DECL_PIOCTL(PGCPAGs)
1257 {
1258     if (!afs_osi_suser(*acred)) {
1259         return EACCES;
1260     }
1261     afs_gcpags = AFS_GCPAGS_USERDISABLED;
1262     return 0;
1263 }
1264
1265 DECL_PIOCTL(PGetAcl)
1266 {
1267     struct AFSOpaque acl;
1268     struct AFSVolSync tsync;
1269     struct AFSFetchStatus OutStatus;
1270     afs_int32 code;
1271     struct conn *tconn;
1272     struct AFSFid Fid;
1273     XSTATS_DECLS;
1274
1275     AFS_STATCNT(PGetAcl);
1276     if (!avc)
1277         return EINVAL;
1278     Fid.Volume = avc->fid.Fid.Volume;
1279     Fid.Vnode = avc->fid.Fid.Vnode;
1280     Fid.Unique = avc->fid.Fid.Unique;
1281     if (avc->states & CForeign) {
1282         /*
1283          * For a dfs xlator acl we have a special hack so that the
1284          * xlator will distinguish which type of acl will return. So
1285          * we currently use the top 2-bytes (vals 0-4) to tell which
1286          * type of acl to bring back. Horrible hack but this will
1287          * cause the least number of changes to code size and interfaces.
1288          */
1289         if (Fid.Vnode & 0xc0000000)
1290             return ERANGE;
1291         Fid.Vnode |= (ainSize << 30);
1292     }
1293     acl.AFSOpaque_val = aout;
1294     do {
1295         tconn = afs_Conn(&avc->fid, areq, SHARED_LOCK);
1296         if (tconn) {
1297             *aout = 0;
1298             XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHACL);
1299             RX_AFS_GUNLOCK();
1300             code = RXAFS_FetchACL(tconn->id, &Fid, &acl, &OutStatus, &tsync);
1301             RX_AFS_GLOCK();
1302             XSTATS_END_TIME;
1303         } else
1304             code = -1;
1305     } while (afs_Analyze
1306              (tconn, code, &avc->fid, areq, AFS_STATS_FS_RPCIDX_FETCHACL,
1307               SHARED_LOCK, NULL));
1308
1309     if (code == 0) {
1310         *aoutSize = (acl.AFSOpaque_len == 0 ? 1 : acl.AFSOpaque_len);
1311     }
1312     return code;
1313 }
1314
1315 DECL_PIOCTL(PNoop)
1316 {
1317     AFS_STATCNT(PNoop);
1318     return 0;
1319 }
1320
1321 DECL_PIOCTL(PBogus)
1322 {
1323     AFS_STATCNT(PBogus);
1324     return EINVAL;
1325 }
1326
1327 DECL_PIOCTL(PGetFileCell)
1328 {
1329     register struct cell *tcell;
1330
1331     AFS_STATCNT(PGetFileCell);
1332     if (!avc)
1333         return EINVAL;
1334     tcell = afs_GetCell(avc->fid.Cell, READ_LOCK);
1335     if (!tcell)
1336         return ESRCH;
1337     strcpy(aout, tcell->cellName);
1338     afs_PutCell(tcell, READ_LOCK);
1339     *aoutSize = strlen(aout) + 1;
1340     return 0;
1341 }
1342
1343 DECL_PIOCTL(PGetWSCell)
1344 {
1345     struct cell *tcell = NULL;
1346
1347     AFS_STATCNT(PGetWSCell);
1348     if (!afs_resourceinit_flag) /* afs daemons haven't started yet */
1349         return EIO;             /* Inappropriate ioctl for device */
1350
1351     tcell = afs_GetPrimaryCell(READ_LOCK);
1352     if (!tcell)                 /* no primary cell? */
1353         return ESRCH;
1354     strcpy(aout, tcell->cellName);
1355     *aoutSize = strlen(aout) + 1;
1356     afs_PutCell(tcell, READ_LOCK);
1357     return 0;
1358 }
1359
1360 DECL_PIOCTL(PGetUserCell)
1361 {
1362     register afs_int32 i;
1363     register struct unixuser *tu;
1364     register struct cell *tcell;
1365
1366     AFS_STATCNT(PGetUserCell);
1367     if (!afs_resourceinit_flag) /* afs daemons haven't started yet */
1368         return EIO;             /* Inappropriate ioctl for device */
1369
1370     /* return the cell name of the primary cell for this user */
1371     i = UHash(areq->uid);
1372     ObtainWriteLock(&afs_xuser, 224);
1373     for (tu = afs_users[i]; tu; tu = tu->next) {
1374         if (tu->uid == areq->uid && (tu->states & UPrimary)) {
1375             tu->refCount++;
1376             ReleaseWriteLock(&afs_xuser);
1377             break;
1378         }
1379     }
1380     if (tu) {
1381         tcell = afs_GetCell(tu->cell, READ_LOCK);
1382         afs_PutUser(tu, WRITE_LOCK);
1383         if (!tcell)
1384             return ESRCH;
1385         else {
1386             strcpy(aout, tcell->cellName);
1387             afs_PutCell(tcell, READ_LOCK);
1388             *aoutSize = strlen(aout) + 1;       /* 1 for the null */
1389         }
1390     } else {
1391         ReleaseWriteLock(&afs_xuser);
1392         *aout = 0;
1393         *aoutSize = 1;
1394     }
1395     return 0;
1396 }
1397
1398 DECL_PIOCTL(PSetTokens)
1399 {
1400     afs_int32 i;
1401     register struct unixuser *tu;
1402     struct ClearToken clear;
1403     register struct cell *tcell;
1404     char *stp;
1405     int stLen;
1406     struct vrequest treq;
1407     afs_int32 flag, set_parent_pag = 0;
1408
1409     AFS_STATCNT(PSetTokens);
1410     if (!afs_resourceinit_flag) {
1411         return EIO;
1412     }
1413     memcpy((char *)&i, ain, sizeof(afs_int32));
1414     ain += sizeof(afs_int32);
1415     stp = ain;                  /* remember where the ticket is */
1416     if (i < 0 || i > MAXKTCTICKETLEN)
1417         return EINVAL;          /* malloc may fail */
1418     stLen = i;
1419     ain += i;                   /* skip over ticket */
1420     memcpy((char *)&i, ain, sizeof(afs_int32));
1421     ain += sizeof(afs_int32);
1422     if (i != sizeof(struct ClearToken)) {
1423         return EINVAL;
1424     }
1425     memcpy((char *)&clear, ain, sizeof(struct ClearToken));
1426     if (clear.AuthHandle == -1)
1427         clear.AuthHandle = 999; /* more rxvab compat stuff */
1428     ain += sizeof(struct ClearToken);
1429     if (ainSize != 2 * sizeof(afs_int32) + stLen + sizeof(struct ClearToken)) {
1430         /* still stuff left?  we've got primary flag and cell name.  Set these */
1431         memcpy((char *)&flag, ain, sizeof(afs_int32));  /* primary id flag */
1432         ain += sizeof(afs_int32);       /* skip id field */
1433         /* rest is cell name, look it up */
1434         /* some versions of gcc appear to need != 0 in order to get this right */
1435         if ((flag & 0x8000) != 0) {     /* XXX Use Constant XXX */
1436             flag &= ~0x8000;
1437             set_parent_pag = 1;
1438         }
1439         tcell = afs_GetCellByName(ain, READ_LOCK);
1440         if (!tcell)
1441             goto nocell;
1442     } else {
1443         /* default to primary cell, primary id */
1444         flag = 1;               /* primary id */
1445         tcell = afs_GetPrimaryCell(READ_LOCK);
1446         if (!tcell)
1447             goto nocell;
1448     }
1449     i = tcell->cellNum;
1450     afs_PutCell(tcell, READ_LOCK);
1451     if (set_parent_pag) {
1452         afs_int32 pag;
1453 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
1454 #if defined(AFS_DARWIN_ENV)
1455         struct proc *p = current_proc();        /* XXX */
1456 #else
1457         struct proc *p = curproc;       /* XXX */
1458 #endif
1459         uprintf("Process %d (%s) tried to change pags in PSetTokens\n",
1460                 p->p_pid, p->p_comm);
1461         if (!setpag(p, acred, -1, &pag, 1)) {
1462 #else
1463 #ifdef  AFS_OSF_ENV
1464         if (!setpag(u.u_procp, acred, -1, &pag, 1)) {   /* XXX u.u_procp is a no-op XXX */
1465 #else
1466         if (!setpag(acred, -1, &pag, 1)) {
1467 #endif
1468 #endif
1469             afs_InitReq(&treq, *acred);
1470             areq = &treq;
1471         }
1472     }
1473     /* now we just set the tokens */
1474     tu = afs_GetUser(areq->uid, i, WRITE_LOCK); /* i has the cell # */
1475     tu->vid = clear.ViceId;
1476     if (tu->stp != NULL) {
1477         afs_osi_Free(tu->stp, tu->stLen);
1478     }
1479     tu->stp = (char *)afs_osi_Alloc(stLen);
1480     tu->stLen = stLen;
1481     memcpy(tu->stp, stp, stLen);
1482     tu->ct = clear;
1483 #ifndef AFS_NOSTATS
1484     afs_stats_cmfullperf.authent.TicketUpdates++;
1485     afs_ComputePAGStats();
1486 #endif /* AFS_NOSTATS */
1487     tu->states |= UHasTokens;
1488     tu->states &= ~UTokensBad;
1489     afs_SetPrimary(tu, flag);
1490     tu->tokenTime = osi_Time();
1491     afs_ResetUserConns(tu);
1492     afs_PutUser(tu, WRITE_LOCK);
1493
1494     return 0;
1495
1496   nocell:
1497     {
1498         int t1;
1499         t1 = afs_initState;
1500         if (t1 < 101)
1501             return EIO;
1502         else
1503             return ESRCH;
1504     }
1505 }
1506
1507 DECL_PIOCTL(PGetVolumeStatus)
1508 {
1509     char volName[32];
1510     char *offLineMsg = afs_osi_Alloc(256);
1511     char *motd = afs_osi_Alloc(256);
1512     register struct conn *tc;
1513     register afs_int32 code = 0;
1514     struct VolumeStatus volstat;
1515     register char *cp;
1516     char *Name, *OfflineMsg, *MOTD;
1517     XSTATS_DECLS;
1518
1519     AFS_STATCNT(PGetVolumeStatus);
1520     if (!avc) {
1521         code = EINVAL;
1522         goto out;
1523     }
1524     Name = volName;
1525     OfflineMsg = offLineMsg;
1526     MOTD = motd;
1527     do {
1528         tc = afs_Conn(&avc->fid, areq, SHARED_LOCK);
1529         if (tc) {
1530             XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_GETVOLUMESTATUS);
1531             RX_AFS_GUNLOCK();
1532             code =
1533                 RXAFS_GetVolumeStatus(tc->id, avc->fid.Fid.Volume, &volstat,
1534                                       &Name, &OfflineMsg, &MOTD);
1535             RX_AFS_GLOCK();
1536             XSTATS_END_TIME;
1537         } else
1538             code = -1;
1539     } while (afs_Analyze
1540              (tc, code, &avc->fid, areq, AFS_STATS_FS_RPCIDX_GETVOLUMESTATUS,
1541               SHARED_LOCK, NULL));
1542
1543     if (code)
1544         goto out;
1545     /* Copy all this junk into msg->im_data, keeping track of the lengths. */
1546     cp = aout;
1547     memcpy(cp, (char *)&volstat, sizeof(VolumeStatus));
1548     cp += sizeof(VolumeStatus);
1549     strcpy(cp, volName);
1550     cp += strlen(volName) + 1;
1551     strcpy(cp, offLineMsg);
1552     cp += strlen(offLineMsg) + 1;
1553     strcpy(cp, motd);
1554     cp += strlen(motd) + 1;
1555     *aoutSize = (cp - aout);
1556   out:
1557     afs_osi_Free(offLineMsg, 256);
1558     afs_osi_Free(motd, 256);
1559     return code;
1560 }
1561
1562 DECL_PIOCTL(PSetVolumeStatus)
1563 {
1564     char volName[32];
1565     char *offLineMsg = afs_osi_Alloc(256);
1566     char *motd = afs_osi_Alloc(256);
1567     register struct conn *tc;
1568     register afs_int32 code = 0;
1569     struct AFSFetchVolumeStatus volstat;
1570     struct AFSStoreVolumeStatus storeStat;
1571     register struct volume *tvp;
1572     register char *cp;
1573     XSTATS_DECLS;
1574
1575     AFS_STATCNT(PSetVolumeStatus);
1576     if (!avc)
1577         return EINVAL;
1578
1579     tvp = afs_GetVolume(&avc->fid, areq, READ_LOCK);
1580     if (tvp) {
1581         if (tvp->states & (VRO | VBackup)) {
1582             afs_PutVolume(tvp, READ_LOCK);
1583             return EROFS;
1584         }
1585         afs_PutVolume(tvp, READ_LOCK);
1586     } else
1587         return ENODEV;
1588     /* Copy the junk out, using cp as a roving pointer. */
1589     cp = ain;
1590     memcpy((char *)&volstat, cp, sizeof(AFSFetchVolumeStatus));
1591     cp += sizeof(AFSFetchVolumeStatus);
1592     if (strlen(cp) >= sizeof(volName))
1593         return E2BIG;
1594     strcpy(volName, cp);
1595     cp += strlen(volName) + 1;
1596     if (strlen(cp) >= sizeof(offLineMsg))
1597         return E2BIG;
1598     strcpy(offLineMsg, cp);
1599     cp += strlen(offLineMsg) + 1;
1600     if (strlen(cp) >= sizeof(motd))
1601         return E2BIG;
1602     strcpy(motd, cp);
1603     storeStat.Mask = 0;
1604     if (volstat.MinQuota != -1) {
1605         storeStat.MinQuota = volstat.MinQuota;
1606         storeStat.Mask |= AFS_SETMINQUOTA;
1607     }
1608     if (volstat.MaxQuota != -1) {
1609         storeStat.MaxQuota = volstat.MaxQuota;
1610         storeStat.Mask |= AFS_SETMAXQUOTA;
1611     }
1612     do {
1613         tc = afs_Conn(&avc->fid, areq, SHARED_LOCK);
1614         if (tc) {
1615             XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_SETVOLUMESTATUS);
1616             RX_AFS_GUNLOCK();
1617             code =
1618                 RXAFS_SetVolumeStatus(tc->id, avc->fid.Fid.Volume, &storeStat,
1619                                       volName, offLineMsg, motd);
1620             RX_AFS_GLOCK();
1621             XSTATS_END_TIME;
1622         } else
1623             code = -1;
1624     } while (afs_Analyze
1625              (tc, code, &avc->fid, areq, AFS_STATS_FS_RPCIDX_SETVOLUMESTATUS,
1626               SHARED_LOCK, NULL));
1627
1628     if (code)
1629         goto out;
1630     /* we are sending parms back to make compat. with prev system.  should
1631      * change interface later to not ask for current status, just set new status */
1632     cp = aout;
1633     memcpy(cp, (char *)&volstat, sizeof(VolumeStatus));
1634     cp += sizeof(VolumeStatus);
1635     strcpy(cp, volName);
1636     cp += strlen(volName) + 1;
1637     strcpy(cp, offLineMsg);
1638     cp += strlen(offLineMsg) + 1;
1639     strcpy(cp, motd);
1640     cp += strlen(motd) + 1;
1641     *aoutSize = cp - aout;
1642   out:
1643     afs_osi_Free(offLineMsg, 256);
1644     afs_osi_Free(motd, 256);
1645     return code;
1646 }
1647
1648 DECL_PIOCTL(PFlush)
1649 {
1650     AFS_STATCNT(PFlush);
1651     if (!avc)
1652         return EINVAL;
1653 #if     defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
1654     afs_BozonLock(&avc->pvnLock, avc);  /* Since afs_TryToSmush will do a pvn_vptrunc */
1655 #endif
1656     ObtainWriteLock(&avc->lock, 225);
1657     ObtainWriteLock(&afs_xcbhash, 456);
1658     afs_DequeueCallback(avc);
1659     avc->states &= ~(CStatd | CDirty);  /* next reference will re-stat cache entry */
1660     ReleaseWriteLock(&afs_xcbhash);
1661     /* now find the disk cache entries */
1662     afs_TryToSmush(avc, *acred, 1);
1663     osi_dnlc_purgedp(avc);
1664     afs_symhint_inval(avc);
1665     if (avc->linkData && !(avc->states & CCore)) {
1666         afs_osi_Free(avc->linkData, strlen(avc->linkData) + 1);
1667         avc->linkData = NULL;
1668     }
1669     ReleaseWriteLock(&avc->lock);
1670 #if     defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
1671     afs_BozonUnlock(&avc->pvnLock, avc);
1672 #endif
1673     return 0;
1674 }
1675
1676 DECL_PIOCTL(PNewStatMount)
1677 {
1678     register afs_int32 code;
1679     register struct vcache *tvc;
1680     register struct dcache *tdc;
1681     struct VenusFid tfid;
1682     char *bufp;
1683     struct sysname_info sysState;
1684     afs_size_t offset, len;
1685
1686     AFS_STATCNT(PNewStatMount);
1687     if (!avc)
1688         return EINVAL;
1689     code = afs_VerifyVCache(avc, areq);
1690     if (code)
1691         return code;
1692     if (vType(avc) != VDIR) {
1693         return ENOTDIR;
1694     }
1695     tdc = afs_GetDCache(avc, (afs_size_t) 0, areq, &offset, &len, 1);
1696     if (!tdc)
1697         return ENOENT;
1698     Check_AtSys(avc, ain, &sysState, areq);
1699     ObtainReadLock(&tdc->lock);
1700     do {
1701         code = afs_dir_Lookup(&tdc->f.inode, sysState.name, &tfid.Fid);
1702     } while (code == ENOENT && Next_AtSys(avc, areq, &sysState));
1703     ReleaseReadLock(&tdc->lock);
1704     afs_PutDCache(tdc);         /* we're done with the data */
1705     bufp = sysState.name;
1706     if (code) {
1707         goto out;
1708     }
1709     tfid.Cell = avc->fid.Cell;
1710     tfid.Fid.Volume = avc->fid.Fid.Volume;
1711     if (!tfid.Fid.Unique && (avc->states & CForeign)) {
1712         tvc = afs_LookupVCache(&tfid, areq, NULL, avc, bufp);
1713     } else {
1714         tvc = afs_GetVCache(&tfid, areq, NULL, NULL);
1715     }
1716     if (!tvc) {
1717         code = ENOENT;
1718         goto out;
1719     }
1720     if (tvc->mvstat != 1) {
1721         afs_PutVCache(tvc);
1722         code = EINVAL;
1723         goto out;
1724     }
1725     ObtainWriteLock(&tvc->lock, 226);
1726     code = afs_HandleLink(tvc, areq);
1727     if (code == 0) {
1728         if (tvc->linkData) {
1729             if ((tvc->linkData[0] != '#') && (tvc->linkData[0] != '%'))
1730                 code = EINVAL;
1731             else {
1732                 /* we have the data */
1733                 strcpy(aout, tvc->linkData);
1734                 *aoutSize = strlen(tvc->linkData) + 1;
1735             }
1736         } else
1737             code = EIO;
1738     }
1739     ReleaseWriteLock(&tvc->lock);
1740     afs_PutVCache(tvc);
1741   out:
1742     if (sysState.allocked)
1743         osi_FreeLargeSpace(bufp);
1744     return code;
1745 }
1746
1747 DECL_PIOCTL(PGetTokens)
1748 {
1749     register struct cell *tcell;
1750     register afs_int32 i;
1751     register struct unixuser *tu;
1752     register char *cp;
1753     afs_int32 iterator;
1754     int newStyle;
1755
1756     AFS_STATCNT(PGetTokens);
1757     if (!afs_resourceinit_flag) /* afs daemons haven't started yet */
1758         return EIO;             /* Inappropriate ioctl for device */
1759
1760     /* weird interface.  If input parameter is present, it is an integer and
1761      * we're supposed to return the parm'th tokens for this unix uid.
1762      * If not present, we just return tokens for cell 1.
1763      * If counter out of bounds, return EDOM.
1764      * If no tokens for the particular cell, return ENOTCONN.
1765      * Also, if this mysterious parm is present, we return, along with the
1766      * tokens, the primary cell indicator (an afs_int32 0) and the cell name
1767      * at the end, in that order.
1768      */
1769     if ((newStyle = (ainSize > 0))) {
1770         memcpy((char *)&iterator, ain, sizeof(afs_int32));
1771     }
1772     i = UHash(areq->uid);
1773     ObtainReadLock(&afs_xuser);
1774     for (tu = afs_users[i]; tu; tu = tu->next) {
1775         if (newStyle) {
1776             if (tu->uid == areq->uid && (tu->states & UHasTokens)) {
1777                 if (iterator-- == 0)
1778                     break;      /* are we done yet? */
1779             }
1780         } else {
1781             if (tu->uid == areq->uid && afs_IsPrimaryCellNum(tu->cell))
1782                 break;
1783         }
1784     }
1785     if (tu) {
1786         /*
1787          * No need to hold a read lock on each user entry
1788          */
1789         tu->refCount++;
1790     }
1791     ReleaseReadLock(&afs_xuser);
1792
1793     if (!tu) {
1794         return EDOM;
1795     }
1796     if (((tu->states & UHasTokens) == 0)
1797         || (tu->ct.EndTimestamp < osi_Time())) {
1798         tu->states |= (UTokensBad | UNeedsReset);
1799         afs_PutUser(tu, READ_LOCK);
1800         return ENOTCONN;
1801     }
1802     /* use iterator for temp */
1803     cp = aout;
1804     iterator = tu->stLen;       /* for compat, we try to return 56 byte tix if they fit */
1805     if (iterator < 56)
1806         iterator = 56;          /* # of bytes we're returning */
1807     memcpy(cp, (char *)&iterator, sizeof(afs_int32));
1808     cp += sizeof(afs_int32);
1809     memcpy(cp, tu->stp, tu->stLen);     /* copy out st */
1810     cp += iterator;
1811     iterator = sizeof(struct ClearToken);
1812     memcpy(cp, (char *)&iterator, sizeof(afs_int32));
1813     cp += sizeof(afs_int32);
1814     memcpy(cp, (char *)&tu->ct, sizeof(struct ClearToken));
1815     cp += sizeof(struct ClearToken);
1816     if (newStyle) {
1817         /* put out primary id and cell name, too */
1818         iterator = (tu->states & UPrimary ? 1 : 0);
1819         memcpy(cp, (char *)&iterator, sizeof(afs_int32));
1820         cp += sizeof(afs_int32);
1821         tcell = afs_GetCell(tu->cell, READ_LOCK);
1822         if (tcell) {
1823             strcpy(cp, tcell->cellName);
1824             cp += strlen(tcell->cellName) + 1;
1825             afs_PutCell(tcell, READ_LOCK);
1826         } else
1827             *cp++ = 0;
1828     }
1829     *aoutSize = cp - aout;
1830     afs_PutUser(tu, READ_LOCK);
1831     return 0;
1832 }
1833
1834 DECL_PIOCTL(PUnlog)
1835 {
1836     register afs_int32 i;
1837     register struct unixuser *tu;
1838
1839     AFS_STATCNT(PUnlog);
1840     if (!afs_resourceinit_flag) /* afs daemons haven't started yet */
1841         return EIO;             /* Inappropriate ioctl for device */
1842
1843     i = UHash(areq->uid);
1844     ObtainWriteLock(&afs_xuser, 227);
1845     for (tu = afs_users[i]; tu; tu = tu->next) {
1846         if (tu->uid == areq->uid) {
1847             tu->vid = UNDEFVID;
1848             tu->states &= ~UHasTokens;
1849             /* security is not having to say you're sorry */
1850             memset((char *)&tu->ct, 0, sizeof(struct ClearToken));
1851             tu->refCount++;
1852             ReleaseWriteLock(&afs_xuser);
1853             /* We have to drop the lock over the call to afs_ResetUserConns, since
1854              * it obtains the afs_xvcache lock.  We could also keep the lock, and
1855              * modify ResetUserConns to take parm saying we obtained the lock
1856              * already, but that is overkill.  By keeping the "tu" pointer
1857              * held over the released lock, we guarantee that we won't lose our
1858              * place, and that we'll pass over every user conn that existed when
1859              * we began this call.
1860              */
1861             afs_ResetUserConns(tu);
1862             tu->refCount--;
1863             ObtainWriteLock(&afs_xuser, 228);
1864 #ifdef UKERNEL
1865             /* set the expire times to 0, causes
1866              * afs_GCUserData to remove this entry
1867              */
1868             tu->ct.EndTimestamp = 0;
1869             tu->tokenTime = 0;
1870 #endif /* UKERNEL */
1871         }
1872     }
1873     ReleaseWriteLock(&afs_xuser);
1874     return 0;
1875 }
1876
1877 DECL_PIOCTL(PMariner)
1878 {
1879     afs_int32 newHostAddr;
1880     afs_int32 oldHostAddr;
1881
1882     AFS_STATCNT(PMariner);
1883     if (afs_mariner)
1884         memcpy((char *)&oldHostAddr, (char *)&afs_marinerHost,
1885                sizeof(afs_int32));
1886     else
1887         oldHostAddr = 0xffffffff;       /* disabled */
1888
1889     memcpy((char *)&newHostAddr, ain, sizeof(afs_int32));
1890     if (newHostAddr == 0xffffffff) {
1891         /* disable mariner operations */
1892         afs_mariner = 0;
1893     } else if (newHostAddr) {
1894         afs_mariner = 1;
1895         afs_marinerHost = newHostAddr;
1896     }
1897     memcpy(aout, (char *)&oldHostAddr, sizeof(afs_int32));
1898     *aoutSize = sizeof(afs_int32);
1899     return 0;
1900 }
1901
1902 DECL_PIOCTL(PCheckServers)
1903 {
1904     register char *cp = 0;
1905     register int i;
1906     register struct server *ts;
1907     afs_int32 temp, *lp = (afs_int32 *) ain, havecell = 0;
1908     struct cell *cellp;
1909     struct chservinfo *pcheck;
1910
1911     AFS_STATCNT(PCheckServers);
1912
1913     if (!afs_resourceinit_flag) /* afs daemons haven't started yet */
1914         return EIO;             /* Inappropriate ioctl for device */
1915
1916     if (*lp == 0x12345678) {    /* For afs3.3 version */
1917         pcheck = (struct chservinfo *)ain;
1918         if (pcheck->tinterval >= 0) {
1919             cp = aout;
1920             memcpy(cp, (char *)&PROBE_INTERVAL, sizeof(afs_int32));
1921             *aoutSize = sizeof(afs_int32);
1922             if (pcheck->tinterval > 0) {
1923                 if (!afs_osi_suser(*acred))
1924                     return EACCES;
1925                 PROBE_INTERVAL = pcheck->tinterval;
1926             }
1927             return 0;
1928         }
1929         if (pcheck->tsize)
1930             havecell = 1;
1931         temp = pcheck->tflags;
1932         cp = pcheck->tbuffer;
1933     } else {                    /* For pre afs3.3 versions */
1934         memcpy((char *)&temp, ain, sizeof(afs_int32));
1935         cp = ain + sizeof(afs_int32);
1936         if (ainSize > sizeof(afs_int32))
1937             havecell = 1;
1938     }
1939
1940     /*
1941      * 1: fast check, don't contact servers.
1942      * 2: local cell only.
1943      */
1944     if (havecell) {
1945         /* have cell name, too */
1946         cellp = afs_GetCellByName(cp, READ_LOCK);
1947         if (!cellp)
1948             return ENOENT;
1949     } else
1950         cellp = NULL;
1951     if (!cellp && (temp & 2)) {
1952         /* use local cell */
1953         cellp = afs_GetPrimaryCell(READ_LOCK);
1954     }
1955     if (!(temp & 1)) {          /* if not fast, call server checker routine */
1956         afs_CheckServers(1, cellp);     /* check down servers */
1957         afs_CheckServers(0, cellp);     /* check up servers */
1958     }
1959     /* now return the current down server list */
1960     cp = aout;
1961     ObtainReadLock(&afs_xserver);
1962     for (i = 0; i < NSERVERS; i++) {
1963         for (ts = afs_servers[i]; ts; ts = ts->next) {
1964             if (cellp && ts->cell != cellp)
1965                 continue;       /* cell spec'd and wrong */
1966             if ((ts->flags & SRVR_ISDOWN)
1967                 && ts->addr->sa_portal != ts->cell->vlport) {
1968                 memcpy(cp, (char *)&ts->addr->sa_ip, sizeof(afs_int32));
1969                 cp += sizeof(afs_int32);
1970             }
1971         }
1972     }
1973     ReleaseReadLock(&afs_xserver);
1974     if (cellp)
1975         afs_PutCell(cellp, READ_LOCK);
1976     *aoutSize = cp - aout;
1977     return 0;
1978 }
1979
1980 DECL_PIOCTL(PCheckVolNames)
1981 {
1982     AFS_STATCNT(PCheckVolNames);
1983     if (!afs_resourceinit_flag) /* afs daemons haven't started yet */
1984         return EIO;             /* Inappropriate ioctl for device */
1985
1986     afs_CheckRootVolume();
1987     afs_CheckVolumeNames(AFS_VOLCHECK_FORCE | AFS_VOLCHECK_EXPIRED |
1988                          AFS_VOLCHECK_BUSY | AFS_VOLCHECK_MTPTS);
1989     return 0;
1990 }
1991
1992 DECL_PIOCTL(PCheckAuth)
1993 {
1994     int i;
1995     struct srvAddr *sa;
1996     struct conn *tc;
1997     struct unixuser *tu;
1998     afs_int32 retValue;
1999
2000     AFS_STATCNT(PCheckAuth);
2001     if (!afs_resourceinit_flag) /* afs daemons haven't started yet */
2002         return EIO;             /* Inappropriate ioctl for device */
2003
2004     retValue = 0;
2005     tu = afs_GetUser(areq->uid, 1, READ_LOCK);  /* check local cell authentication */
2006     if (!tu)
2007         retValue = EACCES;
2008     else {
2009         /* we have a user */
2010         ObtainReadLock(&afs_xsrvAddr);
2011         ObtainReadLock(&afs_xconn);
2012
2013         /* any tokens set? */
2014         if ((tu->states & UHasTokens) == 0)
2015             retValue = EACCES;
2016         /* all connections in cell 1 working? */
2017         for (i = 0; i < NSERVERS; i++) {
2018             for (sa = afs_srvAddrs[i]; sa; sa = sa->next_bkt) {
2019                 for (tc = sa->conns; tc; tc = tc->next) {
2020                     if (tc->user == tu && (tu->states & UTokensBad))
2021                         retValue = EACCES;
2022                 }
2023             }
2024         }
2025         ReleaseReadLock(&afs_xsrvAddr);
2026         ReleaseReadLock(&afs_xconn);
2027         afs_PutUser(tu, READ_LOCK);
2028     }
2029     memcpy(aout, (char *)&retValue, sizeof(afs_int32));
2030     *aoutSize = sizeof(afs_int32);
2031     return 0;
2032 }
2033
2034 static int
2035 Prefetch(char *apath, struct afs_ioctl *adata, int afollow,
2036          struct AFS_UCRED *acred)
2037 {
2038     register char *tp;
2039     register afs_int32 code;
2040 #if defined(AFS_SGI61_ENV) || defined(AFS_SUN57_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
2041     size_t bufferSize;
2042 #else
2043     u_int bufferSize;
2044 #endif
2045
2046     AFS_STATCNT(Prefetch);
2047     if (!apath)
2048         return EINVAL;
2049     tp = osi_AllocLargeSpace(1024);
2050     AFS_COPYINSTR(apath, tp, 1024, &bufferSize, code);
2051     if (code) {
2052         osi_FreeLargeSpace(tp);
2053         return code;
2054     }
2055     if (afs_BBusy()) {          /* do this as late as possible */
2056         osi_FreeLargeSpace(tp);
2057         return EWOULDBLOCK;     /* pretty close */
2058     }
2059     afs_BQueue(BOP_PATH, (struct vcache *)0, 0, 0, acred, (afs_size_t) 0,
2060                (afs_size_t) 0, tp);
2061     return 0;
2062 }
2063
2064 DECL_PIOCTL(PFindVolume)
2065 {
2066     register struct volume *tvp;
2067     register struct server *ts;
2068     register afs_int32 i;
2069     register char *cp;
2070
2071     AFS_STATCNT(PFindVolume);
2072     if (!avc)
2073         return EINVAL;
2074     tvp = afs_GetVolume(&avc->fid, areq, READ_LOCK);
2075     if (tvp) {
2076         cp = aout;
2077         for (i = 0; i < MAXHOSTS; i++) {
2078             ts = tvp->serverHost[i];
2079             if (!ts)
2080                 break;
2081             memcpy(cp, (char *)&ts->addr->sa_ip, sizeof(afs_int32));
2082             cp += sizeof(afs_int32);
2083         }
2084         if (i < MAXHOSTS) {
2085             /* still room for terminating NULL, add it on */
2086             ainSize = 0;        /* reuse vbl */
2087             memcpy(cp, (char *)&ainSize, sizeof(afs_int32));
2088             cp += sizeof(afs_int32);
2089         }
2090         *aoutSize = cp - aout;
2091         afs_PutVolume(tvp, READ_LOCK);
2092         return 0;
2093     }
2094     return ENODEV;
2095 }
2096
2097 DECL_PIOCTL(PViceAccess)
2098 {
2099     register afs_int32 code;
2100     afs_int32 temp;
2101
2102     AFS_STATCNT(PViceAccess);
2103     if (!avc)
2104         return EINVAL;
2105     code = afs_VerifyVCache(avc, areq);
2106     if (code)
2107         return code;
2108     memcpy((char *)&temp, ain, sizeof(afs_int32));
2109     code = afs_AccessOK(avc, temp, areq, CHECK_MODE_BITS);
2110     if (code)
2111         return 0;
2112     else
2113         return EACCES;
2114 }
2115
2116 DECL_PIOCTL(PSetCacheSize)
2117 {
2118     afs_int32 newValue;
2119     int waitcnt = 0;
2120
2121     AFS_STATCNT(PSetCacheSize);
2122     if (!afs_osi_suser(*acred))
2123         return EACCES;
2124     /* too many things are setup initially in mem cache version */
2125     if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
2126         return EROFS;
2127     memcpy((char *)&newValue, ain, sizeof(afs_int32));
2128     if (newValue == 0)
2129         afs_cacheBlocks = afs_stats_cmperf.cacheBlocksOrig;
2130     else {
2131         if (newValue < afs_min_cache)
2132             afs_cacheBlocks = afs_min_cache;
2133         else
2134             afs_cacheBlocks = newValue;
2135     }
2136     afs_stats_cmperf.cacheBlocksTotal = afs_cacheBlocks;
2137     afs_ComputeCacheParms();    /* recompute basic cache parameters */
2138     afs_MaybeWakeupTruncateDaemon();
2139     while (waitcnt++ < 100 && afs_cacheBlocks < afs_blocksUsed) {
2140         afs_osi_Wait(1000, 0, 0);
2141         afs_MaybeWakeupTruncateDaemon();
2142     }
2143     return 0;
2144 }
2145
2146 #define MAXGCSTATS      16
2147 DECL_PIOCTL(PGetCacheSize)
2148 {
2149     afs_int32 results[MAXGCSTATS];
2150
2151     AFS_STATCNT(PGetCacheSize);
2152     memset((char *)results, 0, sizeof(results));
2153     results[0] = afs_cacheBlocks;
2154     results[1] = afs_blocksUsed;
2155     memcpy(aout, (char *)results, sizeof(results));
2156     *aoutSize = sizeof(results);
2157     return 0;
2158 }
2159
2160 DECL_PIOCTL(PRemoveCallBack)
2161 {
2162     register struct conn *tc;
2163     register afs_int32 code = 0;
2164     struct AFSCallBack CallBacks_Array[1];
2165     struct AFSCBFids theFids;
2166     struct AFSCBs theCBs;
2167     XSTATS_DECLS;
2168
2169     AFS_STATCNT(PRemoveCallBack);
2170     if (!avc)
2171         return EINVAL;
2172     if (avc->states & CRO)
2173         return 0;               /* read-only-ness can't change */
2174     ObtainWriteLock(&avc->lock, 229);
2175     theFids.AFSCBFids_len = 1;
2176     theCBs.AFSCBs_len = 1;
2177     theFids.AFSCBFids_val = (struct AFSFid *)&avc->fid.Fid;
2178     theCBs.AFSCBs_val = CallBacks_Array;
2179     CallBacks_Array[0].CallBackType = CB_DROPPED;
2180     if (avc->callback) {
2181         do {
2182             tc = afs_Conn(&avc->fid, areq, SHARED_LOCK);
2183             if (tc) {
2184                 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_GIVEUPCALLBACKS);
2185                 RX_AFS_GUNLOCK();
2186                 code = RXAFS_GiveUpCallBacks(tc->id, &theFids, &theCBs);
2187                 RX_AFS_GLOCK();
2188                 XSTATS_END_TIME;
2189             }
2190             /* don't set code on failure since we wouldn't use it */
2191         } while (afs_Analyze
2192                  (tc, code, &avc->fid, areq,
2193                   AFS_STATS_FS_RPCIDX_GIVEUPCALLBACKS, SHARED_LOCK, NULL));
2194
2195         ObtainWriteLock(&afs_xcbhash, 457);
2196         afs_DequeueCallback(avc);
2197         avc->callback = 0;
2198         avc->states &= ~(CStatd | CUnique);
2199         ReleaseWriteLock(&afs_xcbhash);
2200         if (avc->fid.Fid.Vnode & 1 || (vType(avc) == VDIR))
2201             osi_dnlc_purgedp(avc);
2202     }
2203     ReleaseWriteLock(&avc->lock);
2204     return 0;
2205 }
2206
2207 DECL_PIOCTL(PNewCell)
2208 {
2209     /* create a new cell */
2210     afs_int32 cellHosts[MAXCELLHOSTS], *lp, magic = 0;
2211     char *newcell = 0, *linkedcell = 0, *tp = ain;
2212     register afs_int32 code, linkedstate = 0, ls;
2213     u_short fsport = 0, vlport = 0;
2214     afs_int32 scount;
2215
2216     AFS_STATCNT(PNewCell);
2217     if (!afs_resourceinit_flag) /* afs daemons haven't started yet */
2218         return EIO;             /* Inappropriate ioctl for device */
2219
2220     if (!afs_osi_suser(*acred))
2221         return EACCES;
2222
2223     memcpy((char *)&magic, tp, sizeof(afs_int32));
2224     tp += sizeof(afs_int32);
2225     if (magic != 0x12345678)
2226         return EINVAL;
2227
2228     /* A 3.4 fs newcell command will pass an array of MAXCELLHOSTS
2229      * server addresses while the 3.5 fs newcell command passes
2230      * MAXHOSTS. To figure out which is which, check if the cellname
2231      * is good.
2232      */
2233     newcell = tp + (MAXCELLHOSTS + 3) * sizeof(afs_int32);
2234     scount = ((newcell[0] != '\0') ? MAXCELLHOSTS : MAXHOSTS);
2235
2236     /* MAXCELLHOSTS (=8) is less than MAXHOSTS (=13) */
2237     memcpy((char *)cellHosts, tp, MAXCELLHOSTS * sizeof(afs_int32));
2238     tp += (scount * sizeof(afs_int32));
2239
2240     lp = (afs_int32 *) tp;
2241     fsport = *lp++;
2242     vlport = *lp++;
2243     if (fsport < 1024)
2244         fsport = 0;             /* Privileged ports not allowed */
2245     if (vlport < 1024)
2246         vlport = 0;             /* Privileged ports not allowed */
2247     tp += (3 * sizeof(afs_int32));
2248     newcell = tp;
2249     if ((ls = *lp) & 1) {
2250         linkedcell = tp + strlen(newcell) + 1;
2251         linkedstate |= CLinkedCell;
2252     }
2253
2254     linkedstate |= CNoSUID;     /* setuid is disabled by default for fs newcell */
2255     code =
2256         afs_NewCell(newcell, cellHosts, linkedstate, linkedcell, fsport,
2257                     vlport, (int)0);
2258     return code;
2259 }
2260
2261 DECL_PIOCTL(PNewAlias)
2262 {
2263     /* create a new cell alias */
2264     char *tp = ain;
2265     register afs_int32 code;
2266     char *realName, *aliasName;
2267
2268     if (!afs_resourceinit_flag) /* afs daemons haven't started yet */
2269         return EIO;             /* Inappropriate ioctl for device */
2270
2271     if (!afs_osi_suser(*acred))
2272         return EACCES;
2273
2274     aliasName = tp;
2275     tp += strlen(aliasName) + 1;
2276     realName = tp;
2277
2278     code = afs_NewCellAlias(aliasName, realName);
2279     *aoutSize = 0;
2280     return code;
2281 }
2282
2283 DECL_PIOCTL(PListCells)
2284 {
2285     afs_int32 whichCell;
2286     register struct cell *tcell = 0;
2287     register afs_int32 i;
2288     register char *cp, *tp = ain;
2289
2290     AFS_STATCNT(PListCells);
2291     if (!afs_resourceinit_flag) /* afs daemons haven't started yet */
2292         return EIO;             /* Inappropriate ioctl for device */
2293
2294     memcpy((char *)&whichCell, tp, sizeof(afs_int32));
2295     tp += sizeof(afs_int32);
2296     tcell = afs_GetCellByIndex(whichCell, READ_LOCK);
2297     if (tcell) {
2298         cp = aout;
2299         memset(cp, 0, MAXCELLHOSTS * sizeof(afs_int32));
2300         for (i = 0; i < MAXCELLHOSTS; i++) {
2301             if (tcell->cellHosts[i] == 0)
2302                 break;
2303             memcpy(cp, (char *)&tcell->cellHosts[i]->addr->sa_ip,
2304                    sizeof(afs_int32));
2305             cp += sizeof(afs_int32);
2306         }
2307         cp = aout + MAXCELLHOSTS * sizeof(afs_int32);
2308         strcpy(cp, tcell->cellName);
2309         cp += strlen(tcell->cellName) + 1;
2310         *aoutSize = cp - aout;
2311         afs_PutCell(tcell, READ_LOCK);
2312     }
2313     if (tcell)
2314         return 0;
2315     else
2316         return EDOM;
2317 }
2318
2319 DECL_PIOCTL(PListAliases)
2320 {
2321     afs_int32 whichAlias;
2322     register struct cell_alias *tcalias = 0;
2323     register char *cp, *tp = ain;
2324
2325     if (!afs_resourceinit_flag) /* afs daemons haven't started yet */
2326         return EIO;             /* Inappropriate ioctl for device */
2327     if (ainSize < sizeof(afs_int32))
2328         return EINVAL;
2329
2330     memcpy((char *)&whichAlias, tp, sizeof(afs_int32));
2331     tp += sizeof(afs_int32);
2332
2333     tcalias = afs_GetCellAlias(whichAlias);
2334     if (tcalias) {
2335         cp = aout;
2336         strcpy(cp, tcalias->alias);
2337         cp += strlen(tcalias->alias) + 1;
2338         strcpy(cp, tcalias->cell);
2339         cp += strlen(tcalias->cell) + 1;
2340         *aoutSize = cp - aout;
2341         afs_PutCellAlias(tcalias);
2342     }
2343     if (tcalias)
2344         return 0;
2345     else
2346         return EDOM;
2347 }
2348
2349 DECL_PIOCTL(PRemoveMount)
2350 {
2351     register afs_int32 code;
2352     char *bufp;
2353     struct sysname_info sysState;
2354     afs_size_t offset, len;
2355     register struct conn *tc;
2356     register struct dcache *tdc;
2357     register struct vcache *tvc;
2358     struct AFSFetchStatus OutDirStatus;
2359     struct VenusFid tfid;
2360     struct AFSVolSync tsync;
2361     XSTATS_DECLS;
2362
2363
2364     /* "ain" is the name of the file in this dir to remove */
2365
2366     AFS_STATCNT(PRemoveMount);
2367     if (!avc)
2368         return EINVAL;
2369     code = afs_VerifyVCache(avc, areq);
2370     if (code)
2371         return code;
2372     if (vType(avc) != VDIR)
2373         return ENOTDIR;
2374
2375     tdc = afs_GetDCache(avc, (afs_size_t) 0, areq, &offset, &len, 1);   /* test for error below */
2376     if (!tdc)
2377         return ENOENT;
2378     Check_AtSys(avc, ain, &sysState, areq);
2379     ObtainReadLock(&tdc->lock);
2380     do {
2381         code = afs_dir_Lookup(&tdc->f.inode, sysState.name, &tfid.Fid);
2382     } while (code == ENOENT && Next_AtSys(avc, areq, &sysState));
2383     ReleaseReadLock(&tdc->lock);
2384     bufp = sysState.name;
2385     if (code) {
2386         afs_PutDCache(tdc);
2387         goto out;
2388     }
2389     tfid.Cell = avc->fid.Cell;
2390     tfid.Fid.Volume = avc->fid.Fid.Volume;
2391     if (!tfid.Fid.Unique && (avc->states & CForeign)) {
2392         tvc = afs_LookupVCache(&tfid, areq, NULL, avc, bufp);
2393     } else {
2394         tvc = afs_GetVCache(&tfid, areq, NULL, NULL);
2395     }
2396     if (!tvc) {
2397         code = ENOENT;
2398         afs_PutDCache(tdc);
2399         goto out;
2400     }
2401     if (tvc->mvstat != 1) {
2402         afs_PutDCache(tdc);
2403         afs_PutVCache(tvc);
2404         code = EINVAL;
2405         goto out;
2406     }
2407     ObtainWriteLock(&tvc->lock, 230);
2408     code = afs_HandleLink(tvc, areq);
2409     if (!code) {
2410         if (tvc->linkData) {
2411             if ((tvc->linkData[0] != '#') && (tvc->linkData[0] != '%'))
2412                 code = EINVAL;
2413         } else
2414             code = EIO;
2415     }
2416     ReleaseWriteLock(&tvc->lock);
2417     osi_dnlc_purgedp(tvc);
2418     afs_PutVCache(tvc);
2419     if (code) {
2420         afs_PutDCache(tdc);
2421         goto out;
2422     }
2423     ObtainWriteLock(&avc->lock, 231);
2424     osi_dnlc_remove(avc, bufp, tvc);
2425     do {
2426         tc = afs_Conn(&avc->fid, areq, SHARED_LOCK);
2427         if (tc) {
2428             XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEFILE);
2429             RX_AFS_GUNLOCK();
2430             code =
2431                 RXAFS_RemoveFile(tc->id, (struct AFSFid *)&avc->fid.Fid, bufp,
2432                                  &OutDirStatus, &tsync);
2433             RX_AFS_GLOCK();
2434             XSTATS_END_TIME;
2435         } else
2436             code = -1;
2437     } while (afs_Analyze
2438              (tc, code, &avc->fid, areq, AFS_STATS_FS_RPCIDX_REMOVEFILE,
2439               SHARED_LOCK, NULL));
2440
2441     if (code) {
2442         if (tdc)
2443             afs_PutDCache(tdc);
2444         ReleaseWriteLock(&avc->lock);
2445         goto out;
2446     }
2447     if (tdc) {
2448         /* we have the thing in the cache */
2449         ObtainWriteLock(&tdc->lock, 661);
2450         if (afs_LocalHero(avc, tdc, &OutDirStatus, 1)) {
2451             /* we can do it locally */
2452             code = afs_dir_Delete(&tdc->f.inode, bufp);
2453             if (code) {
2454                 ZapDCE(tdc);    /* surprise error -- invalid value */
2455                 DZap(&tdc->f.inode);
2456             }
2457         }
2458         ReleaseWriteLock(&tdc->lock);
2459         afs_PutDCache(tdc);     /* drop ref count */
2460     }
2461     avc->states &= ~CUnique;    /* For the dfs xlator */
2462     ReleaseWriteLock(&avc->lock);
2463     code = 0;
2464   out:
2465     if (sysState.allocked)
2466         osi_FreeLargeSpace(bufp);
2467     return code;
2468 }
2469
2470 DECL_PIOCTL(PVenusLogging)
2471 {
2472     return EINVAL;              /* OBSOLETE */
2473 }
2474
2475 DECL_PIOCTL(PGetCellStatus)
2476 {
2477     register struct cell *tcell;
2478     afs_int32 temp;
2479
2480     AFS_STATCNT(PGetCellStatus);
2481     if (!afs_resourceinit_flag) /* afs daemons haven't started yet */
2482         return EIO;             /* Inappropriate ioctl for device */
2483
2484     tcell = afs_GetCellByName(ain, READ_LOCK);
2485     if (!tcell)
2486         return ENOENT;
2487     temp = tcell->states;
2488     afs_PutCell(tcell, READ_LOCK);
2489     memcpy(aout, (char *)&temp, sizeof(afs_int32));
2490     *aoutSize = sizeof(afs_int32);
2491     return 0;
2492 }
2493
2494 DECL_PIOCTL(PSetCellStatus)
2495 {
2496     register struct cell *tcell;
2497     afs_int32 temp;
2498
2499     if (!afs_osi_suser(*acred))
2500         return EACCES;
2501     if (!afs_resourceinit_flag) /* afs daemons haven't started yet */
2502         return EIO;             /* Inappropriate ioctl for device */
2503
2504     tcell = afs_GetCellByName(ain + 2 * sizeof(afs_int32), WRITE_LOCK);
2505     if (!tcell)
2506         return ENOENT;
2507     memcpy((char *)&temp, ain, sizeof(afs_int32));
2508     if (temp & CNoSUID)
2509         tcell->states |= CNoSUID;
2510     else
2511         tcell->states &= ~CNoSUID;
2512     afs_PutCell(tcell, WRITE_LOCK);
2513     return 0;
2514 }
2515
2516 DECL_PIOCTL(PFlushVolumeData)
2517 {
2518     register afs_int32 i;
2519     register struct dcache *tdc;
2520     register struct vcache *tvc;
2521     register struct volume *tv;
2522     afs_int32 cell, volume;
2523
2524     AFS_STATCNT(PFlushVolumeData);
2525     if (!avc)
2526         return EINVAL;
2527     if (!afs_resourceinit_flag) /* afs daemons haven't started yet */
2528         return EIO;             /* Inappropriate ioctl for device */
2529
2530     volume = avc->fid.Fid.Volume;       /* who to zap */
2531     cell = avc->fid.Cell;
2532
2533     /*
2534      * Clear stat'd flag from all vnodes from this volume; this will invalidate all
2535      * the vcaches associated with the volume.
2536      */
2537     ObtainReadLock(&afs_xvcache);
2538     for (i = 0; i < VCSIZE; i++) {
2539         for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2540             if (tvc->fid.Fid.Volume == volume && tvc->fid.Cell == cell) {
2541 #if     defined(AFS_SGI_ENV) || defined(AFS_ALPHA_ENV)  || defined(AFS_SUN5_ENV)  || defined(AFS_HPUX_ENV) || defined(AFS_LINUX20_ENV)
2542                 VN_HOLD(AFSTOV(tvc));
2543 #else
2544 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
2545                 osi_vnhold(tvc, 0);
2546 #else
2547                 VREFCOUNT_INC(tvc);
2548 #endif
2549 #endif
2550                 ReleaseReadLock(&afs_xvcache);
2551 #if     defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
2552                 afs_BozonLock(&tvc->pvnLock, tvc);      /* Since afs_TryToSmush will do a pvn_vptrunc */
2553 #endif
2554                 ObtainWriteLock(&tvc->lock, 232);
2555
2556                 ObtainWriteLock(&afs_xcbhash, 458);
2557                 afs_DequeueCallback(tvc);
2558                 tvc->states &= ~(CStatd | CDirty);
2559                 ReleaseWriteLock(&afs_xcbhash);
2560                 if (tvc->fid.Fid.Vnode & 1 || (vType(tvc) == VDIR))
2561                     osi_dnlc_purgedp(tvc);
2562                 afs_TryToSmush(tvc, *acred, 1);
2563                 ReleaseWriteLock(&tvc->lock);
2564 #if     defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
2565                 afs_BozonUnlock(&tvc->pvnLock, tvc);
2566 #endif
2567                 ObtainReadLock(&afs_xvcache);
2568                 /* our tvc ptr is still good until now */
2569                 AFS_FAST_RELE(tvc);
2570             }
2571         }
2572     }
2573     ReleaseReadLock(&afs_xvcache);
2574
2575
2576     MObtainWriteLock(&afs_xdcache, 328);        /* needed if you're going to flush any stuff */
2577     for (i = 0; i < afs_cacheFiles; i++) {
2578         if (!(afs_indexFlags[i] & IFEverUsed))
2579             continue;           /* never had any data */
2580         tdc = afs_GetDSlot(i, NULL);
2581         if (tdc->refCount <= 1) {       /* too high, in use by running sys call */
2582             ReleaseReadLock(&tdc->tlock);
2583             if (tdc->f.fid.Fid.Volume == volume && tdc->f.fid.Cell == cell) {
2584                 if (!(afs_indexFlags[i] & IFDataMod)) {
2585                     /* if the file is modified, but has a ref cnt of only 1, then
2586                      * someone probably has the file open and is writing into it.
2587                      * Better to skip flushing such a file, it will be brought back
2588                      * immediately on the next write anyway.
2589                      * 
2590                      * If we *must* flush, then this code has to be rearranged to call
2591                      * afs_storeAllSegments() first */
2592                     afs_FlushDCache(tdc);
2593                 }
2594             }
2595         } else {
2596             ReleaseReadLock(&tdc->tlock);
2597         }
2598         afs_PutDCache(tdc);     /* bumped by getdslot */
2599     }
2600     MReleaseWriteLock(&afs_xdcache);
2601
2602     ObtainReadLock(&afs_xvolume);
2603     for (i = 0; i < NVOLS; i++) {
2604         for (tv = afs_volumes[i]; tv; tv = tv->next) {
2605             if (tv->volume == volume) {
2606                 afs_ResetVolumeInfo(tv);
2607                 break;
2608             }
2609         }
2610     }
2611     ReleaseReadLock(&afs_xvolume);
2612
2613     /* probably, a user is doing this, probably, because things are screwed up.
2614      * maybe it's the dnlc's fault? */
2615     osi_dnlc_purge();
2616     return 0;
2617 }
2618
2619
2620
2621 DECL_PIOCTL(PGetVnodeXStatus)
2622 {
2623     register afs_int32 code;
2624     struct vcxstat stat;
2625     afs_int32 mode, i;
2626
2627 /*  AFS_STATCNT(PGetVnodeXStatus); */
2628     if (!avc)
2629         return EINVAL;
2630     code = afs_VerifyVCache(avc, areq);
2631     if (code)
2632         return code;
2633     if (vType(avc) == VDIR)
2634         mode = PRSFS_LOOKUP;
2635     else
2636         mode = PRSFS_READ;
2637     if (!afs_AccessOK(avc, mode, areq, CHECK_MODE_BITS))
2638         return EACCES;
2639     stat.fid = avc->fid;
2640     hset32(stat.DataVersion, hgetlo(avc->m.DataVersion));
2641     stat.lock = avc->lock;
2642     stat.parentVnode = avc->parentVnode;
2643     stat.parentUnique = avc->parentUnique;
2644     hset(stat.flushDV, avc->flushDV);
2645     hset(stat.mapDV, avc->mapDV);
2646     stat.truncPos = avc->truncPos;
2647     {                           /* just grab the first two - won't break anything... */
2648         struct axscache *ac;
2649
2650         for (i = 0, ac = avc->Access; ac && i < CPSIZE; i++, ac = ac->next) {
2651             stat.randomUid[i] = ac->uid;
2652             stat.randomAccess[i] = ac->axess;
2653         }
2654     }
2655     stat.callback = afs_data_pointer_to_int32(avc->callback);
2656     stat.cbExpires = avc->cbExpires;
2657     stat.anyAccess = avc->anyAccess;
2658     stat.opens = avc->opens;
2659     stat.execsOrWriters = avc->execsOrWriters;
2660     stat.flockCount = avc->flockCount;
2661     stat.mvstat = avc->mvstat;
2662     stat.states = avc->states;
2663     memcpy(aout, (char *)&stat, sizeof(struct vcxstat));
2664     *aoutSize = sizeof(struct vcxstat);
2665     return 0;
2666 }
2667
2668
2669 /* We require root for local sysname changes, but not for remote */
2670 /* (since we don't really believe remote uids anyway) */
2671  /* outname[] shouldn't really be needed- this is left as an excercise */
2672  /* for the reader.  */
2673 DECL_PIOCTL(PSetSysName)
2674 {
2675     char *cp, *cp2, inname[MAXSYSNAME], outname[MAXSYSNAME];
2676     int setsysname, foundname = 0;
2677     register struct afs_exporter *exporter;
2678     register struct unixuser *au;
2679     register afs_int32 pag, error;
2680     int t, count, num = 0;
2681     char **sysnamelist[MAXSYSNAME];
2682
2683     AFS_STATCNT(PSetSysName);
2684     if (!afs_globalVFS) {
2685         /* Afsd is NOT running; disable it */
2686 #if defined(KERNEL_HAVE_UERROR)
2687         return (setuerror(EINVAL), EINVAL);
2688 #else
2689         return (EINVAL);
2690 #endif
2691     }
2692     memset(inname, 0, MAXSYSNAME);
2693     memcpy((char *)&setsysname, ain, sizeof(afs_int32));
2694     ain += sizeof(afs_int32);
2695     if (setsysname) {
2696
2697         /* Check my args */
2698         if (setsysname < 0 || setsysname > MAXNUMSYSNAMES)
2699             return EINVAL;
2700         cp2 = ain;
2701         for (cp = ain, count = 0; count < setsysname; count++) {
2702             /* won't go past end of ain since maxsysname*num < ain length */
2703             t = strlen(cp);
2704             if (t >= MAXSYSNAME || t <= 0)
2705                 return EINVAL;
2706             /* check for names that can shoot us in the foot */
2707             if (*cp == '.' && (cp[1] == 0 || (cp[1] == '.' && cp[2] == 0)))
2708                 return EINVAL;
2709             cp += t + 1;
2710         }
2711         /* args ok */
2712
2713         /* inname gets first entry in case we're being a translator */
2714         t = strlen(ain);
2715         memcpy(inname, ain, t + 1);     /* include terminating null */
2716         ain += t + 1;
2717         num = count;
2718     }
2719     if ((*acred)->cr_gid == RMTUSER_REQ) {      /* Handles all exporters */
2720         pag = PagInCred(*acred);
2721         if (pag == NOPAG) {
2722             return EINVAL;      /* Better than panicing */
2723         }
2724         if (!(au = afs_FindUser(pag, -1, READ_LOCK))) {
2725             return EINVAL;      /* Better than panicing */
2726         }
2727         if (!(exporter = au->exporter)) {
2728             afs_PutUser(au, READ_LOCK);
2729             return EINVAL;      /* Better than panicing */
2730         }
2731         error = EXP_SYSNAME(exporter, (setsysname ? cp2 : NULL), sysnamelist,
2732                             &num);
2733         if (error) {
2734             if (error == ENODEV)
2735                 foundname = 0;  /* sysname not set yet! */
2736             else {
2737                 afs_PutUser(au, READ_LOCK);
2738                 return error;
2739             }
2740         } else {
2741             foundname = num;
2742             strcpy(outname, (*sysnamelist)[0]);
2743         }
2744         afs_PutUser(au, READ_LOCK);
2745     } else {
2746         /* Not xlating, so local case */
2747         if (!afs_sysname)
2748             osi_Panic("PSetSysName: !afs_sysname\n");
2749         if (!setsysname) {      /* user just wants the info */
2750             strcpy(outname, afs_sysname);
2751             foundname = afs_sysnamecount;
2752             *sysnamelist = afs_sysnamelist;
2753         } else {                /* Local guy; only root can change sysname */
2754             if (!afs_osi_suser(*acred))
2755                 return EACCES;
2756
2757             /* clear @sys entries from the dnlc, once afs_lookup can
2758              * do lookups of @sys entries and thinks it can trust them */
2759             /* privs ok, store the entry, ... */
2760             strcpy(afs_sysname, inname);
2761             if (setsysname > 1) {       /* ... or list */
2762                 cp = ain;
2763                 for (count = 1; count < setsysname; ++count) {
2764                     if (!afs_sysnamelist[count])
2765                         osi_Panic
2766                             ("PSetSysName: no afs_sysnamelist entry to write\n");
2767                     t = strlen(cp);
2768                     memcpy(afs_sysnamelist[count], cp, t + 1);  /* include null */
2769                     cp += t + 1;
2770                 }
2771             }
2772             afs_sysnamecount = setsysname;
2773         }
2774     }
2775     if (!setsysname) {
2776         cp = aout;              /* not changing so report back the count and ... */
2777         memcpy(cp, (char *)&foundname, sizeof(afs_int32));
2778         cp += sizeof(afs_int32);
2779         if (foundname) {
2780             strcpy(cp, outname);        /* ... the entry, ... */
2781             cp += strlen(outname) + 1;
2782             for (count = 1; count < foundname; ++count) {       /* ... or list. */
2783                 if (!(*sysnamelist)[count])
2784                     osi_Panic
2785                         ("PSetSysName: no afs_sysnamelist entry to read\n");
2786                 t = strlen((*sysnamelist)[count]);
2787                 if (t >= MAXSYSNAME)
2788                     osi_Panic("PSetSysName: sysname entry garbled\n");
2789                 strcpy(cp, (*sysnamelist)[count]);
2790                 cp += t + 1;
2791             }
2792         }
2793         *aoutSize = cp - aout;
2794     }
2795     return 0;
2796 }
2797
2798 /* sequential search through the list of touched cells is not a good
2799  * long-term solution here. For small n, though, it should be just
2800  * fine.  Should consider special-casing the local cell for large n.
2801  * Likewise for PSetSPrefs.
2802  *
2803  * s - number of ids in array l[] -- NOT index of last id
2804  * l - array of cell ids which have volumes that need to be sorted
2805  * vlonly - sort vl servers or file servers?
2806  */
2807 static void *
2808 ReSortCells_cb(struct cell *cell, void *arg)
2809 {
2810     afs_int32 *p = (afs_int32 *) arg;
2811     afs_int32 *l = p + 1;
2812     int i, s = p[0];
2813
2814     for (i = 0; i < s; i++) {
2815         if (l[i] == cell->cellNum) {
2816             ObtainWriteLock(&cell->lock, 690);
2817             afs_SortServers(cell->cellHosts, MAXCELLHOSTS);
2818             ReleaseWriteLock(&cell->lock);
2819         }
2820     }
2821
2822     return NULL;
2823 }
2824
2825 static void
2826 ReSortCells(int s, afs_int32 * l, int vlonly)
2827 {
2828     int i;
2829     struct volume *j;
2830     register int k;
2831
2832     if (vlonly) {
2833         afs_int32 *p;
2834         p = (afs_int32 *) afs_osi_Alloc(sizeof(afs_int32) * (s + 1));
2835         p[0] = s;
2836         memcpy(p + 1, l, s * sizeof(afs_int32));
2837         afs_TraverseCells(&ReSortCells_cb, p);
2838         afs_osi_Free(p, sizeof(afs_int32) * (s + 1));
2839         return;
2840     }
2841
2842     ObtainReadLock(&afs_xvolume);
2843     for (i = 0; i < NVOLS; i++) {
2844         for (j = afs_volumes[i]; j; j = j->next) {
2845             for (k = 0; k < s; k++)
2846                 if (j->cell == l[k]) {
2847                     ObtainWriteLock(&j->lock, 233);
2848                     afs_SortServers(j->serverHost, MAXHOSTS);
2849                     ReleaseWriteLock(&j->lock);
2850                     break;
2851                 }
2852         }
2853     }
2854     ReleaseReadLock(&afs_xvolume);
2855 }
2856
2857
2858 static int debugsetsp = 0;
2859 static int
2860 afs_setsprefs(sp, num, vlonly)
2861      struct spref *sp;
2862      unsigned int num;
2863      unsigned int vlonly;
2864 {
2865     struct srvAddr *sa;
2866     int i, j, k, matches, touchedSize;
2867     struct server *srvr = NULL;
2868     afs_int32 touched[34];
2869     int isfs;
2870
2871     touchedSize = 0;
2872     for (k = 0; k < num; sp++, k++) {
2873         if (debugsetsp) {
2874             printf("sp host=%x, rank=%d\n", sp->host.s_addr, sp->rank);
2875         }
2876         matches = 0;
2877         ObtainReadLock(&afs_xserver);
2878
2879         i = SHash(sp->host.s_addr);
2880         for (sa = afs_srvAddrs[i]; sa; sa = sa->next_bkt) {
2881             if (sa->sa_ip == sp->host.s_addr) {
2882                 srvr = sa->server;
2883                 isfs = (srvr->cell && (sa->sa_portal == srvr->cell->fsport))
2884                     || (sa->sa_portal == AFS_FSPORT);
2885                 if ((!vlonly && isfs) || (vlonly && !isfs)) {
2886                     matches++;
2887                     break;
2888                 }
2889             }
2890         }
2891
2892         if (sa && matches) {    /* found one! */
2893             if (debugsetsp) {
2894                 printf("sa ip=%x, ip_rank=%d\n", sa->sa_ip, sa->sa_iprank);
2895             }
2896             sa->sa_iprank = sp->rank + afs_randomMod15();
2897             afs_SortOneServer(sa->server);
2898
2899             if (srvr->cell) {
2900                 /* if we don't know yet what cell it's in, this is moot */
2901                 for (j = touchedSize - 1;
2902                      j >= 0 && touched[j] != srvr->cell->cellNum; j--)
2903                     /* is it in our list of touched cells ?  */ ;
2904                 if (j < 0) {    /* no, it's not */
2905                     touched[touchedSize++] = srvr->cell->cellNum;
2906                     if (touchedSize >= 32) {    /* watch for ovrflow */
2907                         ReleaseReadLock(&afs_xserver);
2908                         ReSortCells(touchedSize, touched, vlonly);
2909                         touchedSize = 0;
2910                         ObtainReadLock(&afs_xserver);
2911                     }
2912                 }
2913             }
2914         }
2915
2916         ReleaseReadLock(&afs_xserver);
2917         /* if we didn't find one, start to create one. */
2918         /* Note that it doesn't have a cell yet...     */
2919         if (!matches) {
2920             afs_uint32 temp = sp->host.s_addr;
2921             srvr =
2922                 afs_GetServer(&temp, 1, 0, (vlonly ? AFS_VLPORT : AFS_FSPORT),
2923                               WRITE_LOCK, (afsUUID *) 0, 0);
2924             srvr->addr->sa_iprank = sp->rank + afs_randomMod15();
2925             afs_PutServer(srvr, WRITE_LOCK);
2926         }
2927     }                           /* for all cited preferences */
2928
2929     ReSortCells(touchedSize, touched, vlonly);
2930     return 0;
2931 }
2932
2933  /* Note that this may only be performed by the local root user.
2934   */
2935 DECL_PIOCTL(PSetSPrefs)
2936 {
2937     struct setspref *ssp;
2938     AFS_STATCNT(PSetSPrefs);
2939
2940     if (!afs_resourceinit_flag) /* afs daemons haven't started yet */
2941         return EIO;             /* Inappropriate ioctl for device */
2942
2943     if (!afs_osi_suser(*acred))
2944         return EACCES;
2945
2946     if (ainSize < sizeof(struct setspref))
2947         return EINVAL;
2948
2949     ssp = (struct setspref *)ain;
2950     if (ainSize < sizeof(struct spref) * ssp->num_servers)
2951         return EINVAL;
2952
2953     afs_setsprefs(&(ssp->servers[0]), ssp->num_servers,
2954                   (ssp->flags & DBservers));
2955     return 0;
2956 }
2957
2958 DECL_PIOCTL(PSetSPrefs33)
2959 {
2960     struct spref *sp;
2961     AFS_STATCNT(PSetSPrefs);
2962     if (!afs_resourceinit_flag) /* afs daemons haven't started yet */
2963         return EIO;             /* Inappropriate ioctl for device */
2964
2965
2966     if (!afs_osi_suser(*acred))
2967         return EACCES;
2968
2969     sp = (struct spref *)ain;
2970     afs_setsprefs(sp, ainSize / (sizeof(struct spref)), 0 /*!vlonly */ );
2971     return 0;
2972 }
2973
2974 /* some notes on the following code...
2975  * in the hash table of server structs, all servers with the same IP address
2976  * will be on the same overflow chain.
2977  * This could be sped slightly in some circumstances by having it cache the
2978  * immediately previous slot in the hash table and some supporting information
2979  * Only reports file servers now.
2980  */
2981 DECL_PIOCTL(PGetSPrefs)
2982 {
2983     struct sprefrequest *spin;  /* input */
2984     struct sprefinfo *spout;    /* output */
2985     struct spref *srvout;       /* one output component */
2986     int i, j;                   /* counters for hash table traversal */
2987     struct server *srvr;        /* one of CM's server structs */
2988     struct srvAddr *sa;
2989     int vlonly;                 /* just return vlservers ? */
2990     int isfs;
2991
2992     AFS_STATCNT(PGetSPrefs);
2993     if (!afs_resourceinit_flag) /* afs daemons haven't started yet */
2994         return EIO;             /* Inappropriate ioctl for device */
2995
2996
2997     if (ainSize < sizeof(struct sprefrequest_33)) {
2998         return ENOENT;
2999     } else {
3000         spin = ((struct sprefrequest *)ain);
3001     }
3002
3003     if (ainSize > sizeof(struct sprefrequest_33)) {
3004         vlonly = (spin->flags & DBservers);
3005     } else
3006         vlonly = 0;
3007
3008     /* struct sprefinfo includes 1 server struct...  that size gets added
3009      * in during the loop that follows.
3010      */
3011     *aoutSize = sizeof(struct sprefinfo) - sizeof(struct spref);
3012     spout = (struct sprefinfo *)aout;
3013     spout->next_offset = spin->offset;
3014     spout->num_servers = 0;
3015     srvout = spout->servers;
3016
3017     ObtainReadLock(&afs_xserver);
3018     for (i = 0, j = 0; j < NSERVERS; j++) {     /* sift through hash table */
3019         for (sa = afs_srvAddrs[j]; sa; sa = sa->next_bkt, i++) {
3020             if (spin->offset > (unsigned short)i) {
3021                 continue;       /* catch up to where we left off */
3022             }
3023             spout->next_offset++;
3024
3025             srvr = sa->server;
3026             isfs = (srvr->cell && (sa->sa_portal == srvr->cell->fsport))
3027                 || (sa->sa_portal == AFS_FSPORT);
3028
3029             if ((vlonly && isfs) || (!vlonly && !isfs)) {
3030                 /* only report ranks for vl servers */
3031                 continue;
3032             }
3033
3034             srvout->host.s_addr = sa->sa_ip;
3035             srvout->rank = sa->sa_iprank;
3036             *aoutSize += sizeof(struct spref);
3037             spout->num_servers++;
3038             srvout++;
3039
3040             if (*aoutSize > (PIGGYSIZE - sizeof(struct spref))) {
3041                 ReleaseReadLock(&afs_xserver);  /* no more room! */
3042                 return 0;
3043             }
3044         }
3045     }
3046     ReleaseReadLock(&afs_xserver);
3047
3048     spout->next_offset = 0;     /* start over from the beginning next time */
3049     return 0;
3050 }
3051
3052 /* Enable/Disable the specified exporter. Must be root to disable an exporter */
3053 int afs_NFSRootOnly = 1;
3054 DECL_PIOCTL(PExportAfs)
3055 {
3056     afs_int32 export, newint =
3057         0, type, changestate, handleValue, convmode, pwsync, smounts;
3058     register struct afs_exporter *exporter;
3059
3060     AFS_STATCNT(PExportAfs);
3061     memcpy((char *)&handleValue, ain, sizeof(afs_int32));
3062     type = handleValue >> 24;
3063     if (type == 0x71) {
3064         newint = 1;
3065         type = 1;               /* nfs */
3066     }
3067     exporter = exporter_find(type);
3068     if (newint) {
3069         export = handleValue & 3;
3070         changestate = handleValue & 0xff;
3071         smounts = (handleValue >> 2) & 3;
3072         pwsync = (handleValue >> 4) & 3;
3073         convmode = (handleValue >> 6) & 3;
3074     } else {
3075         changestate = (handleValue >> 16) & 0x1;
3076         convmode = (handleValue >> 16) & 0x2;
3077         pwsync = (handleValue >> 16) & 0x4;
3078         smounts = (handleValue >> 16) & 0x8;
3079         export = handleValue & 0xff;
3080     }
3081     if (!exporter) {
3082         /*  Failed finding desired exporter; */
3083         return ENODEV;
3084     }
3085     if (!changestate) {
3086         handleValue = exporter->exp_states;
3087         memcpy(aout, (char *)&handleValue, sizeof(afs_int32));
3088         *aoutSize = sizeof(afs_int32);
3089     } else {
3090         if (!afs_osi_suser(*acred))
3091             return EACCES;      /* Only superuser can do this */
3092         if (newint) {
3093             if (export & 2) {
3094                 if (export & 1)
3095                     exporter->exp_states |= EXP_EXPORTED;
3096                 else
3097                     exporter->exp_states &= ~EXP_EXPORTED;
3098             }
3099             if (convmode & 2) {
3100                 if (convmode & 1)
3101                     exporter->exp_states |= EXP_UNIXMODE;
3102                 else
3103                     exporter->exp_states &= ~EXP_UNIXMODE;
3104             }
3105             if (pwsync & 2) {
3106                 if (pwsync & 1)
3107                     exporter->exp_states |= EXP_PWSYNC;
3108                 else
3109                     exporter->exp_states &= ~EXP_PWSYNC;
3110             }
3111             if (smounts & 2) {
3112                 if (smounts & 1) {
3113                     afs_NFSRootOnly = 0;
3114                     exporter->exp_states |= EXP_SUBMOUNTS;
3115                 } else {
3116                     afs_NFSRootOnly = 1;
3117                     exporter->exp_states &= ~EXP_SUBMOUNTS;
3118                 }
3119             }
3120             handleValue = exporter->exp_states;
3121             memcpy(aout, (char *)&handleValue, sizeof(afs_int32));
3122             *aoutSize = sizeof(afs_int32);
3123         } else {
3124             if (export)
3125                 exporter->exp_states |= EXP_EXPORTED;
3126             else
3127                 exporter->exp_states &= ~EXP_EXPORTED;
3128             if (convmode)
3129                 exporter->exp_states |= EXP_UNIXMODE;
3130             else
3131                 exporter->exp_states &= ~EXP_UNIXMODE;
3132             if (pwsync)
3133                 exporter->exp_states |= EXP_PWSYNC;
3134             else
3135                 exporter->exp_states &= ~EXP_PWSYNC;
3136             if (smounts) {
3137                 afs_NFSRootOnly = 0;
3138                 exporter->exp_states |= EXP_SUBMOUNTS;
3139             } else {
3140                 afs_NFSRootOnly = 1;
3141                 exporter->exp_states &= ~EXP_SUBMOUNTS;
3142             }
3143         }
3144     }
3145
3146     return 0;
3147 }
3148
3149 DECL_PIOCTL(PGag)
3150 {
3151     struct gaginfo *gagflags;
3152
3153     if (!afs_osi_suser(*acred))
3154         return EACCES;
3155
3156     gagflags = (struct gaginfo *)ain;
3157     afs_showflags = gagflags->showflags;
3158
3159     return 0;
3160 }
3161
3162
3163 DECL_PIOCTL(PTwiddleRx)
3164 {
3165     struct rxparams *rxp;
3166
3167     if (!afs_osi_suser(*acred))
3168         return EACCES;
3169
3170     rxp = (struct rxparams *)ain;
3171
3172     if (rxp->rx_initReceiveWindow)
3173         rx_initReceiveWindow = rxp->rx_initReceiveWindow;
3174     if (rxp->rx_maxReceiveWindow)
3175         rx_maxReceiveWindow = rxp->rx_maxReceiveWindow;
3176     if (rxp->rx_initSendWindow)
3177         rx_initSendWindow = rxp->rx_initSendWindow;
3178     if (rxp->rx_maxSendWindow)
3179         rx_maxSendWindow = rxp->rx_maxSendWindow;
3180     if (rxp->rxi_nSendFrags)
3181         rxi_nSendFrags = rxp->rxi_nSendFrags;
3182     if (rxp->rxi_nRecvFrags)
3183         rxi_nRecvFrags = rxp->rxi_nRecvFrags;
3184     if (rxp->rxi_OrphanFragSize)
3185         rxi_OrphanFragSize = rxp->rxi_OrphanFragSize;
3186     if (rxp->rx_maxReceiveSize) {
3187         rx_maxReceiveSize = rxp->rx_maxReceiveSize;
3188         rx_maxReceiveSizeUser = rxp->rx_maxReceiveSize;
3189     }
3190     if (rxp->rx_MyMaxSendSize)
3191         rx_MyMaxSendSize = rxp->rx_MyMaxSendSize;
3192
3193     return 0;
3194 }
3195
3196 DECL_PIOCTL(PGetInitParams)
3197 {
3198     if (sizeof(struct cm_initparams) > PIGGYSIZE)
3199         return E2BIG;
3200
3201     memcpy(aout, (char *)&cm_initParams, sizeof(struct cm_initparams));
3202     *aoutSize = sizeof(struct cm_initparams);
3203     return 0;
3204 }
3205
3206 #ifdef AFS_SGI65_ENV
3207 /* They took crget() from us, so fake it. */
3208 static cred_t *
3209 crget(void)
3210 {
3211     cred_t *cr;
3212     cr = crdup(get_current_cred());
3213     memset((char *)cr, 0, sizeof(cred_t));
3214 #if CELL || CELL_PREPARE
3215     cr->cr_id = -1;
3216 #endif
3217     return cr;
3218 }
3219 #endif
3220
3221 DECL_PIOCTL(PGetRxkcrypt)
3222 {
3223     memcpy(aout, (char *)&cryptall, sizeof(afs_int32));
3224     *aoutSize = sizeof(afs_int32);
3225     return 0;
3226 }
3227
3228 DECL_PIOCTL(PSetRxkcrypt)
3229 {
3230     afs_int32 tmpval;
3231
3232     if (!afs_osi_suser(*acred))
3233         return EPERM;
3234     if (ainSize != sizeof(afs_int32) || ain == NULL)
3235         return EINVAL;
3236     memcpy((char *)&tmpval, ain, sizeof(afs_int32));
3237     /* if new mappings added later this will need to be changed */
3238     if (tmpval != 0 && tmpval != 1)
3239         return EINVAL;
3240     cryptall = tmpval;
3241     return 0;
3242 }
3243
3244 #ifdef AFS_NEED_CLIENTCONTEXT
3245 /*
3246  * Create new credentials to correspond to a remote user with given
3247  * <hostaddr, uid, g0, g1>.  This allows a server running as root to
3248  * provide pioctl (and other) services to foreign clients (i.e. nfs
3249  * clients) by using this call to `become' the client.
3250  */
3251 #define PSETPAG         110
3252 #define PIOCTL_HEADER   6
3253 static int
3254 HandleClientContext(struct afs_ioctl *ablob, int *com,
3255                     struct AFS_UCRED **acred, struct AFS_UCRED *credp)
3256 {
3257     char *ain, *inData;
3258     afs_uint32 hostaddr;
3259     afs_int32 uid, g0, g1, i, code, pag, exporter_type;
3260     struct afs_exporter *exporter, *outexporter;
3261     struct AFS_UCRED *newcred;
3262     struct unixuser *au;
3263
3264 #if defined(AFS_SGIMP_ENV)
3265     osi_Assert(ISAFS_GLOCK());
3266 #endif
3267     AFS_STATCNT(HandleClientContext);
3268     if (ablob->in_size < PIOCTL_HEADER * sizeof(afs_int32)) {
3269         /* Must at least include the PIOCTL_HEADER header words required by the protocol */
3270         return EINVAL;          /* Too small to be good  */
3271     }
3272     ain = inData = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
3273     AFS_COPYIN(ablob->in, ain, PIOCTL_HEADER * sizeof(afs_int32), code);
3274     if (code) {
3275         osi_FreeLargeSpace(inData);
3276         return code;
3277     }
3278
3279     /* Extract information for remote user */
3280     hostaddr = *((afs_uint32 *) ain);
3281     ain += sizeof(hostaddr);
3282     uid = *((afs_uint32 *) ain);
3283     ain += sizeof(uid);
3284     g0 = *((afs_uint32 *) ain);
3285     ain += sizeof(g0);
3286     g1 = *((afs_uint32 *) ain);
3287     ain += sizeof(g1);
3288     *com = *((afs_uint32 *) ain);
3289     ain += sizeof(afs_int32);
3290     exporter_type = *((afs_uint32 *) ain);      /* In case we support more than NFS */
3291
3292     /*
3293      * Of course, one must be root for most of these functions, but
3294      * we'll allow (for knfs) you to set things if the pag is 0 and
3295      * you're setting tokens or unlogging.
3296      */
3297     i = (*com) & 0xff;
3298     if (!afs_osi_suser(credp)) {
3299 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI64_ENV)
3300         /* Since SGI's suser() returns explicit failure after the call.. */
3301         u.u_error = 0;
3302 #endif
3303         /* check for acceptable opcodes for normal folks, which are, so far,
3304          * set tokens and unlog.
3305          */
3306         if (i != 9 && i != 3 && i != 38 && i != 8) {
3307             osi_FreeLargeSpace(inData);
3308             return EACCES;
3309         }
3310     }
3311
3312     ablob->in_size -= PIOCTL_HEADER * sizeof(afs_int32);
3313     ablob->in += PIOCTL_HEADER * sizeof(afs_int32);
3314     osi_FreeLargeSpace(inData);
3315     if (uid == 0) {
3316         /*
3317          * We map uid 0 to nobody to match the mapping that the nfs
3318          * server does and to ensure that the suser() calls in the afs
3319          * code fails for remote client roots.
3320          */
3321         uid = afs_nobody;       /* NFS_NOBODY == -2 */
3322     }
3323     newcred = crget();
3324 #ifdef  AFS_AIX41_ENV
3325     setuerror(0);
3326 #endif
3327     newcred->cr_gid = RMTUSER_REQ;
3328 #ifdef AFS_AIX51_ENV
3329     newcred->cr_groupset.gs_union.un_groups[0] = g0;
3330     newcred->cr_groupset.gs_union.un_groups[1] = g1;
3331 #else
3332     newcred->cr_groups[0] = g0;
3333     newcred->cr_groups[1] = g1;
3334 #endif
3335 #ifdef AFS_AIX_ENV
3336     newcred->cr_ngrps = 2;
3337 #else
3338 #if defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV)
3339     newcred->cr_ngroups = 2;
3340 #else
3341     for (i = 2; i < NGROUPS; i++)
3342         newcred->cr_groups[i] = NOGROUP;
3343 #endif
3344 #endif
3345 #if     !defined(AFS_OSF_ENV) && !defined(AFS_DEC_ENV)
3346     afs_nfsclient_init();       /* before looking for exporter, ensure one exists */
3347 #endif
3348     if (!(exporter = exporter_find(exporter_type))) {
3349         /* Exporter wasn't initialized or an invalid exporter type */
3350         crfree(newcred);
3351         return EINVAL;
3352     }
3353     if (exporter->exp_states & EXP_PWSYNC) {
3354         if (uid != credp->cr_uid) {
3355             crfree(newcred);
3356             return ENOEXEC;     /* XXX Find a better errno XXX */
3357         }
3358     }
3359     newcred->cr_uid = uid;      /* Only temporary  */
3360     code = EXP_REQHANDLER(exporter, &newcred, hostaddr, &pag, &outexporter);
3361     /* The client's pag is the only unique identifier for it */
3362     newcred->cr_uid = pag;
3363     *acred = newcred;
3364     if (!code && *com == PSETPAG) {
3365         /* Special case for 'setpag' */
3366         afs_uint32 pagvalue = genpag();
3367
3368         au = afs_GetUser(pagvalue, -1, WRITE_LOCK);     /* a new unixuser struct */
3369         /*
3370          * Note that we leave the 'outexporter' struct held so it won't
3371          * dissappear on us
3372          */
3373         au->exporter = outexporter;
3374         if (ablob->out_size >= 4) {
3375             AFS_COPYOUT((char *)&pagvalue, ablob->out, sizeof(afs_int32),
3376                         code);
3377         }
3378         afs_PutUser(au, WRITE_LOCK);
3379         if (code)
3380             return code;
3381         return PSETPAG;         /*  Special return for setpag  */
3382     } else if (!code) {
3383         EXP_RELE(outexporter);
3384     }
3385     return code;
3386 }
3387 #endif /* AFS_NEED_CLIENTCONTEXT */
3388
3389 /* get all interface addresses of this client */
3390
3391 DECL_PIOCTL(PGetCPrefs)
3392 {
3393     struct sprefrequest *spin;  /* input */
3394     struct sprefinfo *spout;    /* output */
3395     struct spref *srvout;       /* one output component */
3396     int maxNumber;
3397     int i, j;
3398
3399     AFS_STATCNT(PGetCPrefs);
3400     if (!afs_resourceinit_flag) /* afs daemons haven't started yet */
3401         return EIO;             /* Inappropriate ioctl for device */
3402
3403     if (ainSize < sizeof(struct sprefrequest))
3404         return EINVAL;
3405
3406     spin = (struct sprefrequest *)ain;
3407     spout = (struct sprefinfo *)aout;
3408
3409     maxNumber = spin->num_servers;      /* max addrs this time */
3410     srvout = spout->servers;
3411
3412     ObtainReadLock(&afs_xinterface);
3413
3414     /* copy out the client interface information from the
3415      ** kernel data structure "interface" to the output buffer
3416      */
3417     for (i = spin->offset, j = 0; (i < afs_cb_interface.numberOfInterfaces)
3418          && (j < maxNumber); i++, j++, srvout++)
3419         srvout->host.s_addr = afs_cb_interface.addr_in[i];
3420
3421     spout->num_servers = j;
3422     *aoutSize = sizeof(struct sprefinfo) + (j - 1) * sizeof(struct spref);
3423
3424     if (i >= afs_cb_interface.numberOfInterfaces)
3425         spout->next_offset = 0; /* start from beginning again */
3426     else
3427         spout->next_offset = spin->offset + j;
3428
3429     ReleaseReadLock(&afs_xinterface);
3430     return 0;
3431 }
3432
3433 DECL_PIOCTL(PSetCPrefs)
3434 {
3435     struct setspref *sin;
3436     int i;
3437
3438     AFS_STATCNT(PSetCPrefs);
3439     if (!afs_resourceinit_flag) /* afs daemons haven't started yet */
3440         return EIO;             /* Inappropriate ioctl for device */
3441
3442     sin = (struct setspref *)ain;
3443
3444     if (ainSize < sizeof(struct setspref))
3445         return EINVAL;
3446 #if 0                           /* num_servers is unsigned */
3447     if (sin->num_servers < 0)
3448         return EINVAL;
3449 #endif
3450     if (sin->num_servers > AFS_MAX_INTERFACE_ADDR)
3451         return ENOMEM;
3452
3453     ObtainWriteLock(&afs_xinterface, 412);
3454     afs_cb_interface.numberOfInterfaces = sin->num_servers;
3455     for (i = 0; (unsigned short)i < sin->num_servers; i++)
3456         afs_cb_interface.addr_in[i] = sin->servers[i].host.s_addr;
3457
3458     ReleaseWriteLock(&afs_xinterface);
3459     return 0;
3460 }
3461
3462 DECL_PIOCTL(PFlushMount)
3463 {
3464     register afs_int32 code;
3465     register struct vcache *tvc;
3466     register struct dcache *tdc;
3467     struct VenusFid tfid;
3468     char *bufp;
3469     struct sysname_info sysState;
3470     afs_size_t offset, len;
3471
3472     AFS_STATCNT(PFlushMount);
3473     if (!avc)
3474         return EINVAL;
3475     code = afs_VerifyVCache(avc, areq);
3476     if (code)
3477         return code;
3478     if (vType(avc) != VDIR) {
3479         return ENOTDIR;
3480     }
3481     tdc = afs_GetDCache(avc, (afs_size_t) 0, areq, &offset, &len, 1);
3482     if (!tdc)
3483         return ENOENT;
3484     Check_AtSys(avc, ain, &sysState, areq);
3485     ObtainReadLock(&tdc->lock);
3486     do {
3487         code = afs_dir_Lookup(&tdc->f.inode, sysState.name, &tfid.Fid);
3488     } while (code == ENOENT && Next_AtSys(avc, areq, &sysState));
3489     ReleaseReadLock(&tdc->lock);
3490     afs_PutDCache(tdc);         /* we're done with the data */
3491     bufp = sysState.name;
3492     if (code) {
3493         goto out;
3494     }
3495     tfid.Cell = avc->fid.Cell;
3496     tfid.Fid.Volume = avc->fid.Fid.Volume;
3497     if (!tfid.Fid.Unique && (avc->states & CForeign)) {
3498         tvc = afs_LookupVCache(&tfid, areq, NULL, avc, bufp);
3499     } else {
3500         tvc = afs_GetVCache(&tfid, areq, NULL, NULL);
3501     }
3502     if (!tvc) {
3503         code = ENOENT;
3504         goto out;
3505     }
3506     if (tvc->mvstat != 1) {
3507         afs_PutVCache(tvc);
3508         code = EINVAL;
3509         goto out;
3510     }
3511 #if     defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
3512     afs_BozonLock(&tvc->pvnLock, tvc);  /* Since afs_TryToSmush will do a pvn_vptrunc */
3513 #endif
3514     ObtainWriteLock(&tvc->lock, 649);
3515     ObtainWriteLock(&afs_xcbhash, 650);
3516     afs_DequeueCallback(tvc);
3517     tvc->states &= ~(CStatd | CDirty);  /* next reference will re-stat cache entry */
3518     ReleaseWriteLock(&afs_xcbhash);
3519     /* now find the disk cache entries */
3520     afs_TryToSmush(tvc, *acred, 1);
3521     osi_dnlc_purgedp(tvc);
3522     afs_symhint_inval(tvc);
3523     if (tvc->linkData && !(tvc->states & CCore)) {
3524         afs_osi_Free(tvc->linkData, strlen(tvc->linkData) + 1);
3525         tvc->linkData = NULL;
3526     }
3527     ReleaseWriteLock(&tvc->lock);
3528 #if     defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
3529     afs_BozonUnlock(&tvc->pvnLock, tvc);
3530 #endif
3531     afs_PutVCache(tvc);
3532   out:
3533     if (sysState.allocked)
3534         osi_FreeLargeSpace(bufp);
3535     return code;
3536 }
3537
3538 DECL_PIOCTL(PRxStatProc)
3539 {
3540     int code = 0;
3541     afs_int32 flags;
3542
3543     if (!afs_osi_suser(*acred)) {
3544         code = EACCES;
3545         goto out;
3546     }
3547     if (ainSize != sizeof(afs_int32)) {
3548         code = EINVAL;
3549         goto out;
3550     }
3551     memcpy((char *)&flags, ain, sizeof(afs_int32));
3552     if (!(flags & AFSCALL_RXSTATS_MASK) || (flags & ~AFSCALL_RXSTATS_MASK)) {
3553         code = EINVAL;
3554         goto out;
3555     }
3556     if (flags & AFSCALL_RXSTATS_ENABLE) {
3557         rx_enableProcessRPCStats();
3558     }
3559     if (flags & AFSCALL_RXSTATS_DISABLE) {
3560         rx_disableProcessRPCStats();
3561     }
3562     if (flags & AFSCALL_RXSTATS_CLEAR) {
3563         rx_clearProcessRPCStats(AFS_RX_STATS_CLEAR_ALL);
3564     }
3565   out:
3566     *aoutSize = 0;
3567     return code;
3568 }
3569
3570
3571 DECL_PIOCTL(PRxStatPeer)
3572 {
3573     int code = 0;
3574     afs_int32 flags;
3575
3576     if (!afs_osi_suser(*acred)) {
3577         code = EACCES;
3578         goto out;
3579     }
3580     if (ainSize != sizeof(afs_int32)) {
3581         code = EINVAL;
3582         goto out;
3583     }
3584     memcpy((char *)&flags, ain, sizeof(afs_int32));
3585     if (!(flags & AFSCALL_RXSTATS_MASK) || (flags & ~AFSCALL_RXSTATS_MASK)) {
3586         code = EINVAL;
3587         goto out;
3588     }
3589     if (flags & AFSCALL_RXSTATS_ENABLE) {
3590         rx_enablePeerRPCStats();
3591     }
3592     if (flags & AFSCALL_RXSTATS_DISABLE) {
3593         rx_disablePeerRPCStats();
3594     }
3595     if (flags & AFSCALL_RXSTATS_CLEAR) {
3596         rx_clearPeerRPCStats(AFS_RX_STATS_CLEAR_ALL);
3597     }
3598   out:
3599     *aoutSize = 0;
3600     return code;
3601 }
3602
3603 DECL_PIOCTL(PPrefetchFromTape)
3604 {
3605     register afs_int32 code, code1;
3606     afs_int32 bytes;
3607     struct conn *tc;
3608     struct rx_call *tcall;
3609     struct AFSVolSync tsync;
3610     struct AFSFetchStatus OutStatus;
3611     struct AFSCallBack CallBack;
3612     struct VenusFid tfid;
3613     struct AFSFid *Fid;
3614     struct vcache *tvc;
3615
3616     AFS_STATCNT(PSetAcl);
3617     if (!avc)
3618         return EINVAL;
3619
3620     if (ain && (ainSize == 3 * sizeof(afs_int32)))
3621         Fid = (struct AFSFid *)ain;
3622     else
3623         Fid = &avc->fid.Fid;
3624     tfid.Cell = avc->fid.Cell;
3625     tfid.Fid.Volume = Fid->Volume;
3626     tfid.Fid.Vnode = Fid->Vnode;
3627     tfid.Fid.Unique = Fid->Unique;
3628
3629     tvc = afs_GetVCache(&tfid, areq, NULL, NULL);
3630     if (!tvc) {
3631         afs_Trace3(afs_iclSetp, CM_TRACE_PREFETCHCMD, ICL_TYPE_POINTER, tvc,
3632                    ICL_TYPE_FID, &tfid, ICL_TYPE_FID, &avc->fid);
3633         return ENOENT;
3634     }
3635     afs_Trace3(afs_iclSetp, CM_TRACE_PREFETCHCMD, ICL_TYPE_POINTER, tvc,
3636                ICL_TYPE_FID, &tfid, ICL_TYPE_FID, &tvc->fid);
3637
3638     do {
3639         tc = afs_Conn(&tvc->fid, areq, SHARED_LOCK);
3640         if (tc) {
3641
3642             RX_AFS_GUNLOCK();
3643             tcall = rx_NewCall(tc->id);
3644             code =
3645                 StartRXAFS_FetchData(tcall, (struct AFSFid *)&tvc->fid.Fid, 0,
3646                                      0);
3647             if (!code) {
3648                 bytes = rx_Read(tcall, (char *)aout, sizeof(afs_int32));
3649                 code =
3650                     EndRXAFS_FetchData(tcall, &OutStatus, &CallBack, &tsync);
3651             }
3652             code1 = rx_EndCall(tcall, code);
3653             RX_AFS_GLOCK();
3654         } else
3655             code = -1;
3656     } while (afs_Analyze
3657              (tc, code, &tvc->fid, areq, AFS_STATS_FS_RPCIDX_RESIDENCYRPCS,
3658               SHARED_LOCK, NULL));
3659     /* This call is done only to have the callback things handled correctly */
3660     afs_FetchStatus(tvc, &tfid, areq, &OutStatus);
3661     afs_PutVCache(tvc);
3662
3663     if (!code) {
3664         *aoutSize = sizeof(afs_int32);
3665     }
3666     return code;
3667 }
3668
3669 DECL_PIOCTL(PResidencyCmd)
3670 {
3671     register afs_int32 code;
3672     struct conn *tc;
3673     struct vcache *tvc;
3674     struct ResidencyCmdInputs *Inputs;
3675     struct ResidencyCmdOutputs *Outputs;
3676     struct VenusFid tfid;
3677     struct AFSFid *Fid;
3678
3679     Inputs = (struct ResidencyCmdInputs *)ain;
3680     Outputs = (struct ResidencyCmdOutputs *)aout;
3681     if (!avc)
3682         return EINVAL;
3683     if (!ain || ainSize != sizeof(struct ResidencyCmdInputs))
3684         return EINVAL;
3685
3686     Fid = &Inputs->fid;
3687     if (!Fid->Volume)
3688         Fid = &avc->fid.Fid;
3689
3690     tfid.Cell = avc->fid.Cell;
3691     tfid.Fid.Volume = Fid->Volume;
3692     tfid.Fid.Vnode = Fid->Vnode;
3693     tfid.Fid.Unique = Fid->Unique;
3694
3695     tvc = afs_GetVCache(&tfid, areq, NULL, NULL);
3696     afs_Trace3(afs_iclSetp, CM_TRACE_RESIDCMD, ICL_TYPE_POINTER, tvc,
3697                ICL_TYPE_INT32, Inputs->command, ICL_TYPE_FID, &tfid);
3698     if (!tvc)
3699         return ENOENT;
3700
3701     if (Inputs->command) {
3702         do {
3703             tc = afs_Conn(&tvc->fid, areq, SHARED_LOCK);
3704             if (tc) {
3705                 RX_AFS_GUNLOCK();
3706                 code =
3707                     RXAFS_ResidencyCmd(tc->id, Fid, Inputs,
3708                                        (struct ResidencyCmdOutputs *)aout);
3709                 RX_AFS_GLOCK();
3710             } else
3711                 code = -1;
3712         } while (afs_Analyze
3713                  (tc, code, &tvc->fid, areq,
3714                   AFS_STATS_FS_RPCIDX_RESIDENCYRPCS, SHARED_LOCK, NULL));
3715         /* This call is done to have the callback things handled correctly */
3716         afs_FetchStatus(tvc, &tfid, areq, &Outputs->status);
3717     } else {                    /* just a status request, return also link data */
3718         code = 0;
3719         Outputs->code = afs_FetchStatus(tvc, &tfid, areq, &Outputs->status);
3720         Outputs->chars[0] = 0;
3721         if (vType(tvc) == VLNK) {
3722             ObtainWriteLock(&tvc->lock, 555);
3723             if (afs_HandleLink(tvc, areq) == 0)
3724                 strncpy((char *)&Outputs->chars, tvc->linkData, MAXCMDCHARS);
3725             ReleaseWriteLock(&tvc->lock);
3726         }
3727     }
3728
3729     afs_PutVCache(tvc);
3730
3731     if (!code) {
3732         *aoutSize = sizeof(struct ResidencyCmdOutputs);
3733     }
3734     return code;
3735 }