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