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