56f1181982b06a76df229e27c9d7d0af4bb65c17
[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->f.fid))
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             /* Avoid potentially re-entering on this lock */
83             if (0 == NBObtainWriteLock(&tvc->lock, 234)) {
84                 tvc->f.states |= CEvent;
85                 AFS_GUNLOCK();
86                 vnode_setattr(vp, &va, afs_osi_ctxtp);
87                 tvc->f.states &= ~CEvent;
88                 vnode_put(vp);
89                 AFS_GLOCK();
90                 ReleaseWriteLock(&tvc->lock);
91             }
92             ObtainReadLock(&afs_xvcache);
93             /* our tvc ptr is still good until now */
94             AFS_FAST_RELE(tvc);
95         }
96     }
97     ReleaseReadLock(&afs_xvcache);
98     if (isctxtowner)
99         put_vfs_context();
100     if (!isglock)
101         AFS_GUNLOCK();
102 }
103
104 int
105 osi_lookupname_user(user_addr_t aname, enum uio_seg seg, int followlink,
106                     struct vnode **vpp) {
107     char tname[PATHBUFLEN];
108     size_t len;
109     int code;
110
111     if (seg == AFS_UIOUSER) { /* XXX 64bit */
112         AFS_COPYINSTR(aname, tname, sizeof(tname), &len, code);
113         if (code)
114             return code;
115         return osi_lookupname(tname, seg, followlink, vpp);
116     } else
117         return osi_lookupname(CAST_DOWN(char *, aname), seg, followlink, vpp);
118
119 }
120
121 int
122 osi_lookupname(char *aname, enum uio_seg seg, int followlink,
123                struct vnode **vpp) {
124     vfs_context_t ctx;
125     int code, flags;
126
127     flags = 0;
128     if (!followlink)
129         flags |= VNODE_LOOKUP_NOFOLLOW;
130     ctx=vfs_context_create(NULL);
131     code = vnode_lookup(aname, flags, vpp, ctx);
132     if (!code) { /* get a usecount */
133         vnode_ref(*vpp);
134         vnode_put(*vpp);
135     }
136     vfs_context_rele(ctx);
137     return code;
138 }
139 #else
140 int
141 osi_lookupname(char *aname, enum uio_seg seg, int followlink,
142                struct vnode **vpp)
143 {
144     struct nameidata n;
145     int flags, error;
146     flags = 0;
147     flags = LOCKLEAF;
148     if (followlink)
149         flags |= FOLLOW;
150     else
151         flags |= NOFOLLOW;
152     NDINIT(&n, LOOKUP, flags, seg, aname, current_proc());
153     if (error = namei(&n))
154         return error;
155     *vpp = n.ni_vp;
156    /* should we do this? */
157     VOP_UNLOCK(n.ni_vp, 0, current_proc());
158     return 0;
159 }
160 #endif
161
162 /*
163  * afs_suser() returns true if the caller is superuser, false otherwise.
164  *
165  * Note that it must NOT set errno.
166  */
167 int
168 afs_suser(void *credp)
169 {
170     int error;
171     struct proc *p = current_proc();
172
173 #ifdef AFS_DARWIN80_ENV
174     if ((error = proc_suser(p)) == 0) {
175         return (1);
176     }
177     return (0);
178 #else
179     if ((error = suser(p->p_ucred, &p->p_acflag)) == 0) {
180         return (1);
181     }
182     return (0);
183 #endif
184 }
185
186 #ifdef AFS_DARWIN80_ENV
187 uio_t
188 afsio_darwin_partialcopy(uio_t auio, int size)
189 {
190     uio_t res;
191     int i;
192     user_addr_t iovaddr;
193     user_size_t iovsize;
194
195     if (proc_is64bit(current_proc())) {
196         res = uio_create(uio_iovcnt(auio), uio_offset(auio),
197                          uio_isuserspace(auio) ? UIO_USERSPACE64 : UIO_SYSSPACE32,
198                          uio_rw(auio));
199     } else {
200         res = uio_create(uio_iovcnt(auio), uio_offset(auio),
201                          uio_isuserspace(auio) ? UIO_USERSPACE32 : UIO_SYSSPACE32,
202                          uio_rw(auio));
203     }
204
205     for (i = 0;i < uio_iovcnt(auio) && size > 0;i++) {
206         if (uio_getiov(auio, i, &iovaddr, &iovsize))
207             break;
208         if (iovsize > size)
209             iovsize = size;
210         if (uio_addiov(res, iovaddr, iovsize))
211             break;
212         size -= iovsize;
213     }
214     return res;
215 }
216
217 vfs_context_t afs_osi_ctxtp;
218 int afs_osi_ctxtp_initialized;
219 static proc_t vfs_context_curproc;
220 int vfs_context_ref;
221
222 void
223 get_vfs_context(void)
224 {
225     int isglock = ISAFS_GLOCK();
226
227     if (!isglock)
228         AFS_GLOCK();
229     if (afs_osi_ctxtp_initialized) {
230         if (!isglock)
231             AFS_GUNLOCK();
232         return;
233     }
234     osi_Assert(vfs_context_owner != current_thread());
235     if (afs_osi_ctxtp && current_proc() == vfs_context_curproc) {
236         vfs_context_ref++;
237         vfs_context_owner = current_thread();
238         if (!isglock)
239             AFS_GUNLOCK();
240         return;
241     }
242     while (afs_osi_ctxtp && vfs_context_ref) {
243         afs_osi_Sleep(&afs_osi_ctxtp);
244         if (afs_osi_ctxtp_initialized) {
245             if (!isglock)
246                 AFS_GUNLOCK();
247             return;
248         }
249     }
250     vfs_context_rele(afs_osi_ctxtp);
251     vfs_context_ref=1;
252     afs_osi_ctxtp = vfs_context_create(NULL);
253     vfs_context_owner = current_thread();
254     vfs_context_curproc = current_proc();
255     if (!isglock)
256         AFS_GUNLOCK();
257 }
258
259 void
260 put_vfs_context(void)
261 {
262     int isglock = ISAFS_GLOCK();
263
264     if (!isglock)
265         AFS_GLOCK();
266     if (afs_osi_ctxtp_initialized) {
267         if (!isglock)
268             AFS_GUNLOCK();
269         return;
270     }
271     if (vfs_context_owner == current_thread())
272         vfs_context_owner = (thread_t)0;
273     vfs_context_ref--;
274     afs_osi_Wakeup(&afs_osi_ctxtp);
275     if (!isglock)
276         AFS_GUNLOCK();
277 }
278
279 int
280 afs_cdev_nop_openclose(dev_t dev, int flags, int devtype,struct proc *p)
281 {
282     return 0;
283 }
284
285 int
286 afs_cdev_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct proc *p) {
287     unsigned int retval=0;
288     int code, is64 = proc_is64bit(p);
289     struct afssysargs *a = (struct afssysargs *)data;
290     struct afssysargs64 *a64 = (struct afssysargs64 *)data;
291
292     if (((unsigned int)cmd != VIOC_SYSCALL) &&
293         ((unsigned int)cmd != VIOC_SYSCALL64))
294         return EINVAL;
295
296     if (((unsigned int)cmd == VIOC_SYSCALL64) && (is64 == 0))
297         return EINVAL;
298
299     if (((unsigned int)cmd == VIOC_SYSCALL) && (is64 != 0))
300         return EINVAL;
301     
302     code=afs3_syscall(p, data, &retval);
303     if (code)
304         return code;
305
306     if ((!is64) && retval && a->syscall != AFSCALL_CALL
307         && a->param1 != AFSOP_CACHEINODE)
308     {
309         printf("SSCall(%d,%d) is returning non-error value %d\n", a->syscall, a->param1, retval);
310     }
311     if ((is64) && retval && a64->syscall != AFSCALL_CALL
312         && a64->param1 != AFSOP_CACHEINODE)
313     {
314         printf("SSCall(%d,%llx) is returning non-error value %d\n", a64->syscall, a64->param1, retval);
315     }
316
317     if (!is64)
318         a->retval = retval;
319     else
320         a64->retval = retval;
321     return 0; 
322 }
323
324 #endif