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