darwin notify avoid reentrant vfs context panic
[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 static thread_t vfs_context_owner;
24
25 /* works like PFlushVolumeData */
26 void
27 darwin_notify_perms(struct unixuser *auser, int event)
28 {
29     int i;
30     struct afs_q *tq, *uq = NULL;
31     struct vcache *tvc, *hnext;
32     int isglock = ISAFS_GLOCK();
33     struct vnode *vp;
34     struct vnode_attr va;
35     int isctxtowner = 0;
36
37     if (!afs_darwin_fsevents)
38         return;
39
40     VATTR_INIT(&va);
41     VATTR_SET(&va, va_mode, 0777);
42     if (event & UTokensObtained)
43         VATTR_SET(&va, va_uid, auser->uid);
44     else
45         VATTR_SET(&va, va_uid, -2); /* nobody */
46
47     if (!isglock)
48         AFS_GLOCK();
49     if (!(vfs_context_owner == current_thread())) {
50         get_vfs_context();
51         isctxtowner = 1;
52     }
53 loop:
54     ObtainReadLock(&afs_xvcache);
55     for (i = 0; i < VCSIZE; i++) {
56         for (tq = afs_vhashTV[i].prev; tq != &afs_vhashTV[i]; tq = uq) {
57             uq = QPrev(tq);
58             tvc = QTOVH(tq);
59             if (tvc->f.states & CDeadVnode) {
60                 /* we can afford to be best-effort */
61                 continue;
62             }
63             /* no per-file acls, so only notify on directories */
64             if (!(vp = AFSTOV(tvc)) || !vnode_isdir(AFSTOV(tvc)))
65                 continue;
66             /* dynroot object. no callbacks. anonymous ACL. just no. */
67             if (afs_IsDynrootFid(tvc))
68                 continue;
69             /* no fake fsevents on mount point sources. leaks refs */
70             if (tvc->mvstat == 1)
71                 continue;
72             /* if it's being reclaimed, just pass */
73             if (vnode_get(vp))
74                 continue;
75             if (vnode_ref(vp)) {
76                 AFS_GUNLOCK();
77                 vnode_put(vp);
78                 AFS_GLOCK();
79                 continue;
80             }
81             ReleaseReadLock(&afs_xvcache);
82             ObtainWriteLock(&tvc->lock, 234);
83             tvc->f.states |= CEvent;
84             AFS_GUNLOCK();
85             vnode_setattr(vp, &va, afs_osi_ctxtp);
86             tvc->f.states &= ~CEvent;
87             vnode_put(vp);
88             AFS_GLOCK();
89             ReleaseWriteLock(&tvc->lock);
90             ObtainReadLock(&afs_xvcache);
91             /* our tvc ptr is still good until now */
92             AFS_FAST_RELE(tvc);
93         }
94     }
95     ReleaseReadLock(&afs_xvcache);
96     if (isctxtowner)
97         put_vfs_context();
98     if (!isglock)
99         AFS_GUNLOCK();
100 }
101
102 int
103 osi_lookupname_user(user_addr_t aname, enum uio_seg seg, int followlink,
104                     struct vnode **vpp) {
105     char tname[PATHBUFLEN];
106     size_t len;
107     int code;
108
109     if (seg == AFS_UIOUSER) { /* XXX 64bit */
110         AFS_COPYINSTR(aname, tname, sizeof(tname), &len, code);
111         if (code)
112             return code;
113         return osi_lookupname(tname, seg, followlink, vpp);
114     } else
115         return osi_lookupname(CAST_DOWN(char *, aname), seg, followlink, vpp);
116
117 }
118
119 int
120 osi_lookupname(char *aname, enum uio_seg seg, int followlink,
121                struct vnode **vpp) {
122     vfs_context_t ctx;
123     int code, flags;
124
125     flags = 0;
126     if (!followlink)
127         flags |= VNODE_LOOKUP_NOFOLLOW;
128     ctx=vfs_context_create(NULL);
129     code = vnode_lookup(aname, flags, vpp, ctx);
130     if (!code) { /* get a usecount */
131         vnode_ref(*vpp);
132         vnode_put(*vpp);
133     }
134     vfs_context_rele(ctx);
135     return code;
136 }
137 #else
138 int
139 osi_lookupname(char *aname, enum uio_seg seg, int followlink,
140                struct vnode **vpp)
141 {
142     struct nameidata n;
143     int flags, error;
144     flags = 0;
145     flags = LOCKLEAF;
146     if (followlink)
147         flags |= FOLLOW;
148     else
149         flags |= NOFOLLOW;
150     NDINIT(&n, LOOKUP, flags, seg, aname, current_proc());
151     if (error = namei(&n))
152         return error;
153     *vpp = n.ni_vp;
154    /* should we do this? */
155     VOP_UNLOCK(n.ni_vp, 0, current_proc());
156     return 0;
157 }
158 #endif
159
160 /*
161  * afs_suser() returns true if the caller is superuser, false otherwise.
162  *
163  * Note that it must NOT set errno.
164  */
165 int
166 afs_suser(void *credp)
167 {
168     int error;
169     struct proc *p = current_proc();
170
171 #ifdef AFS_DARWIN80_ENV
172     if ((error = proc_suser(p)) == 0) {
173         return (1);
174     }
175     return (0);
176 #else
177     if ((error = suser(p->p_ucred, &p->p_acflag)) == 0) {
178         return (1);
179     }
180     return (0);
181 #endif
182 }
183
184 #ifdef AFS_DARWIN80_ENV
185 uio_t
186 afsio_darwin_partialcopy(uio_t auio, int size)
187 {
188     uio_t res;
189     int i;
190     user_addr_t iovaddr;
191     user_size_t iovsize;
192
193     if (proc_is64bit(current_proc())) {
194         res = uio_create(uio_iovcnt(auio), uio_offset(auio),
195                          uio_isuserspace(auio) ? UIO_USERSPACE64 : UIO_SYSSPACE32,
196                          uio_rw(auio));
197     } else {
198         res = uio_create(uio_iovcnt(auio), uio_offset(auio),
199                          uio_isuserspace(auio) ? UIO_USERSPACE32 : UIO_SYSSPACE32,
200                          uio_rw(auio));
201     }
202
203     for (i = 0;i < uio_iovcnt(auio) && size > 0;i++) {
204         if (uio_getiov(auio, i, &iovaddr, &iovsize))
205             break;
206         if (iovsize > size)
207             iovsize = size;
208         if (uio_addiov(res, iovaddr, iovsize))
209             break;
210         size -= iovsize;
211     }
212     return res;
213 }
214
215 vfs_context_t afs_osi_ctxtp;
216 int afs_osi_ctxtp_initialized;
217 static proc_t vfs_context_curproc;
218 int vfs_context_ref;
219
220 void
221 get_vfs_context(void)
222 {
223     int isglock = ISAFS_GLOCK();
224
225     if (!isglock)
226         AFS_GLOCK();
227     if (afs_osi_ctxtp_initialized) {
228         if (!isglock)
229             AFS_GUNLOCK();
230         return;
231     }
232     osi_Assert(vfs_context_owner != current_thread());
233     if (afs_osi_ctxtp && current_proc() == vfs_context_curproc) {
234         vfs_context_ref++;
235         vfs_context_owner = current_thread();
236         if (!isglock)
237             AFS_GUNLOCK();
238         return;
239     }
240     while (afs_osi_ctxtp && vfs_context_ref) {
241         afs_osi_Sleep(&afs_osi_ctxtp);
242         if (afs_osi_ctxtp_initialized) {
243             if (!isglock)
244                 AFS_GUNLOCK();
245             return;
246         }
247     }
248     vfs_context_rele(afs_osi_ctxtp);
249     vfs_context_ref=1;
250     afs_osi_ctxtp = vfs_context_create(NULL);
251     vfs_context_owner = current_thread();
252     vfs_context_curproc = current_proc();
253     if (!isglock)
254         AFS_GUNLOCK();
255 }
256
257 void
258 put_vfs_context(void)
259 {
260     int isglock = ISAFS_GLOCK();
261
262     if (!isglock)
263         AFS_GLOCK();
264     if (afs_osi_ctxtp_initialized) {
265         if (!isglock)
266             AFS_GUNLOCK();
267         return;
268     }
269     if (vfs_context_owner == current_thread())
270         vfs_context_owner = (thread_t)0;
271     vfs_context_ref--;
272     afs_osi_Wakeup(&afs_osi_ctxtp);
273     if (!isglock)
274         AFS_GUNLOCK();
275 }
276
277 int
278 afs_cdev_nop_openclose(dev_t dev, int flags, int devtype,struct proc *p)
279 {
280     return 0;
281 }
282
283 int
284 afs_cdev_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct proc *p) {
285     unsigned int retval=0;
286     int code, is64 = proc_is64bit(p);
287     struct afssysargs *a = (struct afssysargs *)data;
288     struct afssysargs64 *a64 = (struct afssysargs64 *)data;
289
290     if (((unsigned int)cmd != VIOC_SYSCALL) &&
291         ((unsigned int)cmd != VIOC_SYSCALL64))
292         return EINVAL;
293
294     if (((unsigned int)cmd == VIOC_SYSCALL64) && (is64 == 0))
295         return EINVAL;
296
297     if (((unsigned int)cmd == VIOC_SYSCALL) && (is64 != 0))
298         return EINVAL;
299     
300     code=afs3_syscall(p, data, &retval);
301     if (code)
302         return code;
303
304     if ((!is64) && retval && a->syscall != AFSCALL_CALL
305         && a->param1 != AFSOP_CACHEINODE)
306     {
307         printf("SSCall(%d,%d) is returning non-error value %d\n", a->syscall, a->param1, retval);
308     }
309     if ((is64) && retval && a64->syscall != AFSCALL_CALL
310         && a64->param1 != AFSOP_CACHEINODE)
311     {
312         printf("SSCall(%d,%llx) is returning non-error value %d\n", a64->syscall, a64->param1, retval);
313     }
314
315     if (!is64)
316         a->retval = retval;
317     else
318         a64->retval = retval;
319     return 0; 
320 }
321
322 #endif