Remove the RCSID macro
[openafs.git] / src / afs / DARWIN / osi_misc.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
14 #include "afs/sysincludes.h"
15 #include "afsincludes.h"
16 #include <sys/namei.h>
17
18 #ifndef PATHBUFLEN
19 #define PATHBUFLEN 256
20 #endif
21
22 #ifdef AFS_DARWIN80_ENV
23 int
24 osi_lookupname(char *aname, enum uio_seg seg, int followlink,
25                struct vnode **vpp) {
26     vfs_context_t ctx;
27     char tname[PATHBUFLEN];
28     int code, flags;
29     size_t len;
30     
31     if (seg == AFS_UIOUSER) { /* XXX 64bit */
32         AFS_COPYINSTR(aname, tname, sizeof(tname), &len, code);
33         if (code)
34             return code;
35         aname=tname;
36     }
37     flags = 0;
38     if (!followlink)
39         flags |= VNODE_LOOKUP_NOFOLLOW;
40     ctx=vfs_context_create(NULL);
41     code = vnode_lookup(aname, flags, vpp, ctx);
42     if (!code) { /* get a usecount */
43         vnode_ref(*vpp);
44         vnode_put(*vpp);
45     }
46     vfs_context_rele(ctx);
47     return code;
48 }
49 #else
50 int
51 osi_lookupname(char *aname, enum uio_seg seg, int followlink,
52                struct vnode **vpp)
53 {
54     struct nameidata n;
55     int flags, error;
56     flags = 0;
57     flags = LOCKLEAF;
58     if (followlink)
59         flags |= FOLLOW;
60     else
61         flags |= NOFOLLOW;
62     NDINIT(&n, LOOKUP, flags, seg, aname, current_proc());
63     if (error = namei(&n))
64         return error;
65     *vpp = n.ni_vp;
66    /* should we do this? */
67     VOP_UNLOCK(n.ni_vp, 0, current_proc());
68     return 0;
69 }
70 #endif
71
72 /*
73  * afs_suser() returns true if the caller is superuser, false otherwise.
74  *
75  * Note that it must NOT set errno.
76  */
77 int
78 afs_suser(void *credp)
79 {
80     int error;
81     struct proc *p = current_proc();
82
83 #ifdef AFS_DARWIN80_ENV
84     if ((error = proc_suser(p)) == 0) {
85         return (1);
86     }
87     return (0);
88 #else
89     if ((error = suser(p->p_ucred, &p->p_acflag)) == 0) {
90         return (1);
91     }
92     return (0);
93 #endif
94 }
95
96 #ifdef AFS_DARWIN80_ENV
97 uio_t afsio_darwin_partialcopy(uio_t auio, int size) {
98    uio_t res;
99    int i;
100    user_addr_t iovaddr;
101    user_size_t iovsize;
102
103    if (proc_is64bit(current_proc())) {
104        res = uio_create(uio_iovcnt(auio), uio_offset(auio),
105                         uio_isuserspace(auio) ? UIO_USERSPACE64 : UIO_SYSSPACE32,
106                         uio_rw(auio));
107    } else {
108        res = uio_create(uio_iovcnt(auio), uio_offset(auio),
109                         uio_isuserspace(auio) ? UIO_USERSPACE32 : UIO_SYSSPACE32,
110                         uio_rw(auio));
111    }
112
113    for (i = 0;i < uio_iovcnt(auio) && size > 0;i++) {
114        if (uio_getiov(auio, i, &iovaddr, &iovsize))
115            break;
116        if (iovsize > size)
117           iovsize = size;
118        if (uio_addiov(res, iovaddr, iovsize))
119           break;
120        size -= iovsize;
121    }
122    return res;
123 }
124
125 vfs_context_t afs_osi_ctxtp;
126 int afs_osi_ctxtp_initialized;
127 static thread_t vfs_context_owner;
128 static proc_t vfs_context_curproc;
129 int vfs_context_ref;
130 void get_vfs_context(void) {
131   int isglock = ISAFS_GLOCK();
132
133   if (!isglock)
134      AFS_GLOCK();
135   if (afs_osi_ctxtp_initialized) {
136      if (!isglock)
137         AFS_GUNLOCK();
138       return;
139   }
140   osi_Assert(vfs_context_owner != current_thread());
141   if (afs_osi_ctxtp && current_proc() == vfs_context_curproc) {
142      vfs_context_ref++;
143      vfs_context_owner = current_thread();
144      if (!isglock)
145         AFS_GUNLOCK();
146      return;
147   }
148   while (afs_osi_ctxtp && vfs_context_ref) {
149      afs_osi_Sleep(&afs_osi_ctxtp);
150      if (afs_osi_ctxtp_initialized) {
151        if (!isglock)
152           AFS_GUNLOCK();
153        return;
154      }
155   }
156   vfs_context_rele(afs_osi_ctxtp);
157   vfs_context_ref=1;
158   afs_osi_ctxtp = vfs_context_create(NULL);
159   vfs_context_owner = current_thread();
160   vfs_context_curproc = current_proc();
161   if (!isglock)
162      AFS_GUNLOCK();
163 }
164
165 void put_vfs_context(void) {
166   int isglock = ISAFS_GLOCK();
167
168   if (!isglock)
169      AFS_GLOCK();
170   if (afs_osi_ctxtp_initialized) {
171      if (!isglock)
172         AFS_GUNLOCK();
173       return;
174   }
175   if (vfs_context_owner == current_thread())
176       vfs_context_owner = (thread_t)0;
177   vfs_context_ref--;
178   afs_osi_Wakeup(&afs_osi_ctxtp);
179      if (!isglock)
180         AFS_GUNLOCK();
181 }
182
183 extern int afs3_syscall();
184
185 int afs_cdev_nop_openclose(dev_t dev, int flags, int devtype,struct proc *p) {
186   return 0;
187 }
188 extern int afs3_syscall(struct proc *p, void *data, unsigned long *retval);
189
190 int
191 afs_cdev_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct proc *p) {
192    unsigned long retval=0;
193    int code;
194    struct afssysargs *a = (struct afssysargs *)data;
195    if (proc_is64bit(p))
196      return EINVAL;
197
198   if (cmd != VIOC_SYSCALL) {
199      return EINVAL;
200   }
201
202  code=afs3_syscall(p, data, &retval);
203  if (code)
204     return code;
205  if (retval && a->syscall != AFSCALL_CALL && a->param1 != AFSOP_CACHEINODE) { printf("SSCall(%d,%d) is returning non-error value %d\n", a->syscall, a->param1, retval); }
206  a->retval = retval;
207  return 0; 
208 }
209
210 #endif