54efc99b627dc171a306aae2a06061918535428d
[openafs.git] / src / afs / LINUX / 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  * Linux implementation.
12  *
13  */
14
15 #ifndef OSI_MACHDEP_H_
16 #define OSI_MACHDEP_H_
17
18 #include <linux/version.h>
19 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,4)
20 # define AFS_LINUX26_ONEGROUP_ENV 1
21 #endif
22
23 /* Only needed for xdr.h in glibc 2.1.x */
24 #ifndef quad_t
25 # define quad_t __quad_t
26 # define u_quad_t __u_quad_t
27 #endif
28
29 #undef getuerror
30
31 #ifdef STRUCT_TASK_STRUCT_HAS_TGID
32 # define getpid() current->tgid
33 # ifdef STRUCT_TASK_STRUCT_HAS_REAL_PARENT
34 #  define getppid() current->real_parent->tgid
35 # elif defined(STRUCT_TASK_STRUCT_HAS_PARENT)
36 #  define getppid() current->parent->tgid
37 # else
38 #  define getppid() current->p_opptr->tgid
39 # endif
40 #else /* !STRUCT_TASK_STRUCT_HAS_TGID */
41 # define getpid() current->pid
42 # ifdef STRUCT_TASK_STRUCT_HAS_REAL_PARENT
43 #  define getppid() current->real_parent->pid
44 # elif defined(STRUCT_TASK_STRUCT_HAS_PARENT)
45 #  define getppid() current->parent->pid
46 # else
47 #  define getppid() current->p_opptr->pid
48 # endif
49 #endif /* STRUCT_TASK_STRUCT_HAS_TGID */
50
51 #ifdef RECALC_SIGPENDING_TAKES_VOID
52 # define RECALC_SIGPENDING(X) recalc_sigpending()
53 #else
54 # define RECALC_SIGPENDING(X) recalc_sigpending(X)
55 #endif
56
57 #if defined (STRUCT_TASK_STRUCT_HAS_SIGMASK_LOCK)
58 # define SIG_LOCK(X) spin_lock_irq(&X->sigmask_lock)
59 # define SIG_UNLOCK(X) spin_unlock_irq(&X->sigmask_lock)
60 #elif defined (STRUCT_TASK_STRUCT_HAS_SIGHAND)
61 # define SIG_LOCK(X) spin_lock_irq(&X->sighand->siglock)
62 # define SIG_UNLOCK(X) spin_unlock_irq(&X->sighand->siglock)
63 #else
64 # define SIG_LOCK(X) spin_lock_irq(&X->sig->siglock)
65 # define SIG_UNLOCK(X) spin_unlock_irq(&X->sig->siglock)
66 #endif
67
68 #if defined (STRUCT_TASK_STRUCT_HAS_RLIM)
69 # define TASK_STRUCT_RLIM rlim
70 #elif defined (STRUCT_TASK_STRUCT_HAS_SIGNAL_RLIM)
71 # define TASK_STRUCT_RLIM signal->rlim
72 #else
73 # error Not sure what to do about rlim (should be in the Linux task struct somewhere....)
74 #endif
75
76
77 #define afs_hz HZ
78 #include "h/sched.h"
79 #if defined(HAVE_LINUX_CURRENT_KERNEL_TIME)
80 static inline time_t osi_Time(void) {
81     struct timespec xtime;
82     xtime = current_kernel_time();
83     return xtime.tv_sec;
84 }
85 #else
86 # define osi_Time() (xtime.tv_sec)
87 #endif
88
89
90
91 #ifdef AFS_LINUX_64BIT_KERNEL
92 # define osi_GetTime(V)                                 \
93     do {                                               \
94        struct timeval __afs_tv;                              \
95        do_gettimeofday(&__afs_tv);                           \
96        (V)->tv_sec = (afs_int32)__afs_tv.tv_sec;             \
97        (V)->tv_usec = (afs_int32)__afs_tv.tv_usec;           \
98     } while (0)
99 #else
100 # define osi_GetTime(V) do_gettimeofday((V))
101 #endif
102
103 #undef gop_lookupname
104 #define gop_lookupname osi_lookupname
105
106 #undef gop_lookupname_user
107 #define gop_lookupname_user osi_lookupname
108
109 #define osi_vnhold(V, N) do { VN_HOLD(AFSTOV(V)); } while (0)
110 #define VN_HOLD(V) osi_Assert(igrab((V)) == (V))
111 #define VN_RELE(V) iput((V))
112
113 #define afs_suser(x) capable(CAP_SYS_ADMIN)
114 extern int afs_osi_Wakeup(void *event);
115 static inline void
116 wakeup(void *event)
117 {
118     afs_osi_Wakeup(event);
119 }
120
121 #define vType(V) ((AFSTOV((V)))->i_mode & S_IFMT)
122 #define vSetType(V, type) AFSTOV((V))->i_mode = ((type) | (AFSTOV((V))->i_mode & ~S_IFMT))      /* preserve mode */
123 #define vSetVfsp(V, vfsp)                               /* unused */
124 #define IsAfsVnode(V) ((V)->i_sb == afs_globalVFS)      /* test superblock instead */
125 #define SetAfsVnode(V)                                  /* unnecessary */
126
127 #include <asm/uaccess.h>
128
129 #define copyin(F, T, C)  (copy_from_user ((char*)(T), (char*)(F), (C)) > 0 ? EFAULT : 0)
130 static inline long copyinstr(char *from, char *to, int count, int *length) {
131     long tmp;
132     tmp = strncpy_from_user(to, from, count);
133     if (tmp < 0)
134             return EFAULT;
135     *length = tmp;
136     return 0;
137 }
138 #define copyout(F, T, C) (copy_to_user ((char*)(T), (char*)(F), (C)) > 0 ? EFAULT : 0)
139
140 /* kernel print statements */
141 #define printf(args...) printk(args)
142 #define uprintf(args...) printk(args)
143
144
145 #ifndef NGROUPS
146 #define NGROUPS NGROUPS_SMALL
147 #endif
148
149 typedef struct task_struct afs_proc_t;
150
151 #ifdef HAVE_LINUX_KUID_T
152
153 #include <linux/uidgid.h>
154 typedef kuid_t afs_kuid_t;
155 typedef kgid_t afs_kgid_t;
156 extern struct user_namespace *afs_ns;
157 # ifdef CONFIG_USER_NS
158 #  define afs_current_user_ns() current_user_ns()
159 # else
160 /* Here current_user_ns() expands to GPL-only init_user_ns symbol! */
161 #  define afs_current_user_ns() ((struct user_namespace *)NULL)
162 # endif
163
164 static inline kuid_t afs_make_kuid(uid_t uid) {
165     return make_kuid(afs_ns, uid);
166 }
167 static inline kgid_t afs_make_kgid(gid_t gid) {
168     return make_kgid(afs_ns, gid);
169 }
170 static inline uid_t afs_from_kuid(kuid_t kuid) {
171     return from_kuid(afs_ns, kuid);
172 }
173 static inline uid_t afs_from_kgid(kgid_t kgid) {
174     return from_kgid(afs_ns, kgid);
175 }
176
177 #else
178
179 typedef uid_t afs_kuid_t;
180 typedef gid_t afs_kgid_t;
181
182 static inline afs_kuid_t afs_make_kuid(uid_t uid) {return uid;}
183 static inline afs_kgid_t afs_make_kgid(gid_t gid) {return gid;}
184 static inline uid_t afs_from_kuid(afs_kuid_t kuid) {return kuid;}
185 static inline gid_t afs_from_kgid(afs_kgid_t kgid) {return kgid;}
186 static inline unsigned char uid_eq(uid_t a, uid_t b) {return a == b;}
187 static inline unsigned char gid_eq(gid_t a, gid_t b) {return a == b;}
188 static inline unsigned char uid_lt(uid_t a, uid_t b) {return a < b;}
189 static inline unsigned char gid_lt(gid_t a, gid_t b) {return a < b;}
190 #define GLOBAL_ROOT_UID ((afs_kuid_t) 0)
191 #define GLOBAL_ROOT_GID ((afs_kgid_t) 0)
192
193 #endif
194
195 /* Credentials.  For newer kernels we use the kernel structure directly. */
196 #if defined(STRUCT_TASK_STRUCT_HAS_CRED)
197
198 typedef struct cred afs_ucred_t;
199 typedef struct cred cred_t;
200
201 # define afs_cr_uid(cred) (afs_from_kuid((cred)->fsuid))
202 # define afs_cr_gid(cred) (afs_from_kgid((cred)->fsgid))
203 # define afs_cr_ruid(cred) (afs_from_kuid((cred)->uid))
204 # define afs_cr_rgid(cred) (afs_from_kgid((cred)->gid))
205 # define afs_cr_group_info(cred) ((cred)->group_info)
206 # define crhold(c) (get_cred(c))
207 static inline void
208 afs_set_cr_uid(cred_t *cred, uid_t uid) {
209     cred->fsuid = afs_make_kuid(uid);
210 }
211 static inline void
212 afs_set_cr_gid(cred_t *cred, gid_t gid) {
213     cred->fsgid = afs_make_kgid(gid);
214 }
215 static inline void
216 afs_set_cr_ruid(cred_t *cred, uid_t uid) {
217     cred->uid = afs_make_kuid(uid);
218 }
219 static inline void
220 afs_set_cr_rgid(cred_t *cred, gid_t gid) {
221     cred->gid = afs_make_kgid(gid);
222 }
223 static inline void
224 afs_set_cr_group_info(cred_t *cred, struct group_info *group_info) {
225     cred->group_info = group_info;
226 }
227
228 # define current_group_info() (current->cred->group_info)
229 # define task_gid(task) (task->cred->gid)
230 # define task_user(task) (task->cred->user)
231 # if defined(STRUCT_CRED_HAS_SESSION_KEYRING)
232 #  define task_session_keyring(task) (task->cred->session_keyring)
233 #  define current_session_keyring() (current->cred->session_keyring)
234 # else
235 #  define task_session_keyring(task) (task->cred->tgcred->session_keyring)
236 #  define current_session_keyring() (current->cred->tgcred->session_keyring)
237 # endif
238
239 #else
240
241 typedef struct afs_cred {
242     atomic_t cr_ref;
243     uid_t cr_uid;
244     uid_t cr_ruid;
245     gid_t cr_gid;
246     gid_t cr_rgid;
247     struct group_info *cr_group_info;
248 } cred_t;
249
250 typedef struct afs_cred afs_ucred_t;
251 # define afs_cr_group_info(cred) ((cred)->cr_group_info)
252 static inline void
253 afs_set_cr_group_info(cred_t *cred, struct group_info *group_info) {
254     cred->cr_group_info = group_info;
255 }
256
257 # define current_group_info() (current->group_info)
258 # if !defined(task_gid)
259 #  define task_gid(task) (task->gid)
260 # endif
261 # if !defined(task_uid)
262 #  define task_uid(task) (task->uid)
263 # endif
264 # define task_user(task) (task->user)
265 # define task_session_keyring(task) (task->signal->session_keyring)
266 # define current_session_keyring() (current->signal->session_keyring)
267 # define crhold(c) atomic_inc(&(c)->cr_ref)
268
269 #endif /* defined(STRUCT_TASK_STRUCT_HAS_CRED) */
270
271 #if !defined(current_cred)
272 # define current_gid() (current->gid)
273 # define current_uid() (current->uid)
274 # define current_fsgid() (current->fsgid)
275 # define current_fsuid() (current->fsuid)
276 #endif
277
278 /* UIO manipulation */
279 typedef enum { AFS_UIOSYS, AFS_UIOUSER } uio_seg_t;
280 typedef enum { UIO_READ, UIO_WRITE } uio_flag_t;
281 struct uio {
282     struct iovec *uio_iov;
283     int uio_iovcnt;
284     afs_offs_t uio_offset;
285     uio_seg_t uio_seg;
286     int uio_resid;
287     uio_flag_t uio_flag;
288 };
289 #define afsio_iov       uio_iov
290 #define afsio_iovcnt    uio_iovcnt
291 #define afsio_offset    uio_offset
292 #define afsio_seg       uio_segflg
293 #define afsio_fmode     uio_fmode
294 #define afsio_resid     uio_resid
295
296 /* Get/set the inode in the osifile struct. */
297 #define FILE_INODE(F) (F)->f_dentry->d_inode
298
299 #define OSIFILE_INODE(a) FILE_INODE((a)->filp)
300
301 #if defined(AFS_LINUX_64BIT_KERNEL) && !defined(AFS_ALPHA_LINUX20_ENV) && !defined(AFS_IA64_LINUX20_ENV)
302 # define NEED_IOCTL32
303 #endif
304
305 #include <linux/version.h>
306 #include <linux/sched.h>
307 #include <linux/wait.h>
308
309 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
310 extern struct mutex afs_global_lock;
311 #else
312 extern struct semaphore afs_global_lock;
313 # define mutex_lock(lock) down(lock)
314 # define mutex_unlock(lock) up(lock)
315 #endif
316 extern int afs_global_owner;
317
318 #define AFS_GLOCK() \
319 do { \
320          mutex_lock(&afs_global_lock); \
321          if (afs_global_owner) \
322              osi_Panic("afs_global_lock already held by pid %d", \
323                        afs_global_owner); \
324          afs_global_owner = current->pid; \
325 } while (0)
326
327 #define ISAFS_GLOCK() (afs_global_owner == current->pid)
328
329 #define AFS_GUNLOCK() \
330 do { \
331     if (!ISAFS_GLOCK()) \
332         osi_Panic("afs global lock not held at %s:%d", __FILE__, __LINE__); \
333     afs_global_owner = 0; \
334     mutex_unlock(&afs_global_lock); \
335 } while (0)
336
337 #define osi_InitGlock()
338
339 #ifdef AFS_AMD64_LINUX20_ENV
340 /* RHEL5 beta's kernel doesn't define these. They aren't gonna change, so... */
341
342 # ifndef __NR_ia32_afs_syscall
343 #  define __NR_ia32_afs_syscall 137
344 # endif
345 # ifndef __NR_ia32_setgroups
346 #  define __NR_ia32_setgroups 81
347 # endif
348 # ifndef __NR_ia32_setgroups32
349 #  define __NR_ia32_setgroups32 206
350 # endif
351 # ifndef __NR_ia32_close
352 #  define __NR_ia32_close 6
353 # endif
354 # ifndef __NR_ia32_chdir
355 #  define __NR_ia32_chdir 12
356 # endif
357 # ifndef __NR_ia32_break
358 #  define __NR_ia32_break 17
359 # endif
360 # ifndef __NR_ia32_stty
361 #  define __NR_ia32_stty 31
362 # endif
363 # ifndef __NR_ia32_gtty
364 #  define __NR_ia32_gtty 32
365 # endif
366 # ifndef __NR_ia32_ftime
367 #  define __NR_ia32_ftime 35
368 # endif
369 # ifndef __NR_ia32_prof
370 #  define __NR_ia32_prof 44
371 # endif
372 # ifndef __NR_ia32_lock
373 #  define __NR_ia32_lock 53
374 # endif
375 # ifndef __NR_ia32_mpx
376 #  define __NR_ia32_mpx 56
377 # endif
378 # ifndef __NR_ia32_exit
379 #  define __NR_ia32_exit 1
380 # endif
381 # ifndef __NR_ia32_mount
382 #  define __NR_ia32_mount 21
383 # endif
384 # ifndef __NR_ia32_read
385 #  define __NR_ia32_read 3
386 # endif
387 # ifndef __NR_ia32_write
388 #  define __NR_ia32_write 4
389 # endif
390 # ifndef __NR_ia32_open
391 #  define __NR_ia32_open 5
392 # endif
393 # ifndef __NR_ia32_close
394 #  define __NR_ia32_close 6
395 # endif
396 # ifndef __NR_ia32_unlink
397 #  define __NR_ia32_unlink 10
398 # endif
399 #endif
400
401 #define osi_procname(procname, size) strncpy(procname, current->comm, size)
402
403 #endif /* OSI_MACHDEP_H_ */