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