Adding osi_procname macro to different platforms
[openafs.git] / src / afs / DARWIN / osi_machdep.h
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  *
11  * MACOS OSI header file. Extends afs_osi.h.
12  *
13  * afs_osi.h includes this file, which is the only way this file should
14  * be included in a source file. This file can redefine macros declared in
15  * afs_osi.h.
16  */
17
18 #ifndef _OSI_MACHDEP_H_
19 #define _OSI_MACHDEP_H_
20
21 #ifdef XAFS_DARWIN_ENV
22 #ifndef _MACH_ETAP_H_
23 #define _MACH_ETAP_H_
24 typedef unsigned short etap_event_t;
25 #endif
26 #endif
27
28 #ifdef AFS_DARWIN80_ENV
29 #include <kern/locks.h>
30 #include <sys/mount.h>
31 #include <h/vnode.h>
32 #else
33 #include <sys/lock.h>
34 #include <sys/user.h>
35 #include <sys/vnode.h>
36 #endif
37 #include <kern/thread.h>
38
39 #ifdef AFS_DARWIN80_ENV
40 #define vop_proc vfs_context_proc(ap->a_context)
41 #define vop_cred vfs_context_ucred(ap->a_context)
42 #define cn_proc(cnp) vfs_context_proc(ap->a_context)
43 #define cn_cred(cnp) vfs_context_ucred(ap->a_context)
44 #define vop_cn_proc vfs_context_proc(ap->a_context)
45 #define vop_cn_cred vfs_context_ucred(ap->a_context)
46 #define getpid()                proc_selfpid()
47 #define getppid()               proc_selfppid()
48 #else
49 #define vop_proc ap->a_p
50 #define vop_cred ap->a_cred
51 #define cn_proc(cnp) (cnp)->cn_proc
52 #define cn_cred(cnp) (cnp)->cn_cred
53 #define vop_cn_proc cn_proc(ap->a_cnp)
54 #define vop_cn_cred cn_cred(ap->a_cnp)
55 #define getpid()                current_proc()->p_pid
56 #define getppid()               current_proc()->p_pptr->p_pid
57 #endif
58 #undef gop_lookupname
59 #define gop_lookupname osi_lookupname
60 #undef gop_lookupname_user
61 #define gop_lookupname_user osi_lookupname_user
62
63 #define FTRUNC 0
64
65 /* vcexcl - used only by afs_create */
66 enum vcexcl { EXCL, NONEXCL };
67
68 #ifndef AFS_DARWIN80_ENV
69 #define vnode_clearfsnode(x) ((x)->v_data = 0)
70 #define vnode_fsnode(x) (x)->v_data
71 #define vnode_lock(x) vn_lock(x, LK_EXCLUSIVE | LK_RETRY, current_proc());
72 #define vnode_isvroot(x) (((x)->v_flag & VROOT)?1:0)
73 #define vnode_vtype(x) (x)->v_type
74 #define vnode_isdir(x) ((x)->v_type == VDIR)
75
76 #define vfs_flags(x) (x)->v_flags
77 #define vfs_setflags(x, y) (x)->mnt_flag |= (y)
78 #define vfs_clearflags(x, y) (x)->mnt_flag &= (~(y))
79 #define vfs_isupdate(x) ((x)->mnt_flag & MNT_UPDATE)
80 #define vfs_fsprivate(x) (x)->mnt_data
81 #define vfs_setfsprivate(x,y) (x)->mnt_data = (y)
82 #define vfs_typenum(x) (x)->mnt_vfc->vfc_typenum
83 #endif
84
85 #ifdef AFS_DARWIN80_ENV
86 #define vrele vnode_rele
87 #define vput vnode_rele
88 #define vref vnode_ref
89 #define vattr vnode_attr
90 #if 0
91 #define vn_lock(v, unused1, unused2) vnode_get((v))
92 #define VOP_LOCK(v, unused1, unused2) vnode_get((v))
93 #define VOP_UNLOCK(v, unused1, unused2) vnode_put((v))
94 #endif
95
96 #define va_size va_data_size
97 #define va_atime va_access_time
98 #define va_mtime va_modify_time
99 #define va_ctime va_change_time
100 #define va_bytes va_total_alloc 
101 #define va_blocksize va_iosize
102 #define va_nodeid va_fileid
103
104 #define crref kauth_cred_get_with_ref
105 #define crhold kauth_cred_ref
106 #ifdef AFS_DARWIN100_ENV
107 #define crfree(X) kauth_cred_unref(&X)
108 #else
109 #define crfree kauth_cred_rele
110 #endif
111 #define crdup kauth_cred_dup
112 #ifdef AFS_DARWIN100_ENV
113 #define ubc_msync_range(X,Y,Z,A) ubc_msync(X,Y,Z,NULL,A)
114 #else
115 #define ubc_msync_range(X,Y,Z,A) ubc_sync_range(X,Y,Z,A)
116 #endif
117 extern vfs_context_t afs_osi_ctxtp;
118 extern int afs_osi_ctxtp_initialized;
119 #endif
120 extern u_int32_t afs_darwin_realmodes;
121 extern u_int32_t afs_darwin_fsevents;
122
123 /* 
124  * Time related macros
125  */
126 #ifdef AFS_DARWIN80_ENV
127 static inline time_t osi_Time(void) {
128     struct timeval _now;
129     microtime(&_now);
130     return _now.tv_sec;
131 }
132 #else
133 #define osi_Time() (time.tv_sec)
134 #endif
135 #define afs_hz      hz
136 #ifdef AFS_DARWIN80_ENV
137 extern int hz;
138 #endif
139
140 typedef struct ucred afs_ucred_t;
141 typedef struct proc afs_proc_t;
142
143 #define osi_vnhold(avc,r)       VN_HOLD(AFSTOV(avc))
144 #define VN_HOLD(vp) darwin_vn_hold(vp)
145 #define VN_RELE(vp) vrele(vp);
146
147 void darwin_vn_hold(struct vnode *vp);
148 #ifdef AFS_DARWIN80_ENV
149 void darwin_vn_rele(struct vnode *vp);
150 #endif
151
152 #define gop_rdwr(rw,gp,base,len,offset,segflg,unit,cred,aresid) \
153   vn_rdwr((rw),(gp),(base),(len),(offset),(segflg),(unit),(cred),(aresid),current_proc())
154
155 #undef afs_suser
156
157 extern thread_t afs_global_owner;
158 /* simple locks cannot be used since sleep can happen at any time */
159 #ifdef AFS_DARWIN80_ENV
160 /* mach locks still don't have an exported try, but we are forced to use them */
161 extern lck_mtx_t  *afs_global_lock;
162 #define AFS_GLOCK() \
163     do { \
164         osi_Assert(afs_global_owner != current_thread()); \
165         lck_mtx_lock(afs_global_lock); \
166         osi_Assert(afs_global_owner == 0); \
167         afs_global_owner = current_thread(); \
168     } while (0)
169 #define AFS_GUNLOCK() \
170     do { \
171         osi_Assert(afs_global_owner == current_thread()); \
172         afs_global_owner = 0; \
173         lck_mtx_unlock(afs_global_lock); \
174     } while(0)
175 #define osi_InitGlock() \
176     do { \
177         afs_global_owner = 0; \
178     } while (0)
179 #else
180 /* Should probably use mach locks rather than bsd locks, since we use the
181    mach thread control api's elsewhere (mach locks not used for consistency
182    with rx, since rx needs lock_write_try() in order to use mach locks
183  */
184 extern struct lock__bsd__ afs_global_lock;
185 #define AFS_GLOCK() \
186     do { \
187         lockmgr(&afs_global_lock, LK_EXCLUSIVE, 0, current_proc()); \
188         osi_Assert(afs_global_owner == 0); \
189         afs_global_owner = current_thread(); \
190     } while (0)
191 #define AFS_GUNLOCK() \
192     do { \
193         osi_Assert(afs_global_owner == current_thread()); \
194         afs_global_owner = 0; \
195         lockmgr(&afs_global_lock, LK_RELEASE, 0, current_proc()); \
196     } while(0)
197 #define osi_InitGlock() \
198     do { \
199         lockinit(&afs_global_lock, PLOCK, "afs global lock", 0, 0); \
200         afs_global_owner = 0; \
201     } while (0)
202 #endif
203 #define ISAFS_GLOCK() (afs_global_owner == current_thread())
204
205 #define SPLVAR
206 #define NETPRI
207 #define USERPRI
208 #if 0
209 #undef SPLVAR
210 #define SPLVAR int x;
211 #undef NETPRI
212 #define NETPRI x=splnet();
213 #undef USERPRI
214 #define USERPRI splx(x);
215 #endif
216
217 #define AFS_APPL_UFS_CACHE 1
218 #define AFS_APPL_HFS_CACHE 2
219
220 extern ino_t VnodeToIno(vnode_t avp);
221 extern dev_t VnodeToDev(vnode_t vp);
222 extern int igetinode(mount_t vfsp, dev_t dev , ino_t inode, vnode_t *vpp,
223               struct vattr *va, int *perror);
224
225 #define osi_curproc() current_proc()
226
227 /* FIXME */
228 #define osi_curcred() &afs_osi_cred 
229
230 #ifdef AFS_DARWIN80_ENV
231 uio_t afsio_darwin_partialcopy(uio_t auio, int size);
232
233 #define uprintf printf
234 #endif
235
236 /* Vnode related macros */
237
238 #if defined(AFS_DARWIN80_ENV)
239 extern int afs_vfs_typenum;
240 # define vType(vc)               vnode_vtype(AFSTOV(vc))
241 # define vSetVfsp(vc, vfsp)
242 # define vSetType(vc, type)      (vc)->f.m.Type = (type)
243 # define SetAfsVnode(vn)         /* nothing; done in getnewvnode() */
244 # define IsAfsVnode(v) (vfs_typenum(vnode_mount((v))) == afs_vfs_typenum)
245 #else
246 extern int (**afs_vnodeop_p) ();
247 # define vType(vc)               AFSTOV(vc)->v_type
248 # define vSetVfsp(vc, vfsp)      AFSTOV(vc)->v_mount = (vfsp)
249 # define vSetType(vc, type)      AFSTOV(vc)->v_type = (type)
250 # define IsAfsVnode(v)      ((v)->v_op == afs_vnodeop_p)
251 # define SetAfsVnode(v)     /* nothing; done in getnewvnode() */
252 #endif
253
254 #ifdef AFS_DARWIN80_ENV
255 #define osi_procname(procname, size) proc_selfname(procname, size)
256 #else
257 #define osi_procname(procname, size) strncpy(procname, curproc->p_comm, size)
258 #endif
259
260 #endif /* _OSI_MACHDEP_H_ */