Cleanup usage of LINUX_VERSION_CODE for older kernels
[openafs.git] / src / afs / LINUX / osi_machdep.h
index c09d7b4..87d351c 100644 (file)
 #ifndef OSI_MACHDEP_H_
 #define OSI_MACHDEP_H_
 
-#include <linux/version.h>
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,4)
-# define AFS_LINUX26_ONEGROUP_ENV 1
-#endif
-
 /* Only needed for xdr.h in glibc 2.1.x */
 #ifndef quad_t
 # define quad_t __quad_t
 
 #define afs_hz HZ
 #include "h/sched.h"
-#if defined(HAVE_LINUX_CURRENT_KERNEL_TIME)
+/* in case cred.h is present but not included in sched.h */
+#if defined(HAVE_LINUX_CRED_H)
+#include "h/cred.h"
+#endif
+
+#include "afs/sysincludes.h"
+
+#if !defined(HAVE_LINUX_TIME_T)
+typedef time64_t time_t;
+#endif
+
+#if defined(HAVE_LINUX_KTIME_GET_COARSE_REAL_TS64)
+static inline time_t osi_Time(void) {
+    struct timespec64 xtime;
+    ktime_get_coarse_real_ts64(&xtime);
+    return xtime.tv_sec;
+}
+#elif defined(HAVE_LINUX_CURRENT_KERNEL_TIME)
 static inline time_t osi_Time(void) {
     struct timespec xtime;
     xtime = current_kernel_time();
@@ -86,18 +98,24 @@ static inline time_t osi_Time(void) {
 # define osi_Time() (xtime.tv_sec)
 #endif
 
-
-
-#ifdef AFS_LINUX_64BIT_KERNEL
-# define osi_GetTime(V)                                 \
-    do {                                               \
-       struct timeval __afs_tv;                              \
-       do_gettimeofday(&__afs_tv);                           \
-       (V)->tv_sec = (afs_int32)__afs_tv.tv_sec;             \
-       (V)->tv_usec = (afs_int32)__afs_tv.tv_usec;           \
-    } while (0)
+#if defined(HAVE_LINUX_KTIME_GET_REAL_TS64)
+static inline void
+osi_GetTime(osi_timeval32_t *atv)
+{
+    struct timespec64 now;
+    ktime_get_real_ts64(&now);
+    atv->tv_sec = now.tv_sec;
+    atv->tv_usec = now.tv_nsec / 1000;
+}
 #else
-# define osi_GetTime(V) do_gettimeofday((V))
+static inline void
+osi_GetTime(osi_timeval32_t *atv)
+{
+    struct timeval now;
+    do_gettimeofday(&now);
+    atv->tv_sec = now.tv_sec;
+    atv->tv_usec = now.tv_usec;
+}
 #endif
 
 #undef gop_lookupname
@@ -106,7 +124,6 @@ static inline time_t osi_Time(void) {
 #undef gop_lookupname_user
 #define gop_lookupname_user osi_lookupname
 
-#define osi_vnhold(V, N) do { VN_HOLD(AFSTOV(V)); } while (0)
 #define VN_HOLD(V) osi_Assert(igrab((V)) == (V))
 #define VN_RELE(V) iput((V))
 
@@ -124,7 +141,11 @@ wakeup(void *event)
 #define IsAfsVnode(V) ((V)->i_sb == afs_globalVFS)     /* test superblock instead */
 #define SetAfsVnode(V)                                 /* unnecessary */
 
+#if defined(HAVE_LINUX_UACCESS_H)
+#include <linux/uaccess.h>
+#else
 #include <asm/uaccess.h>
+#endif
 
 #define copyin(F, T, C)  (copy_from_user ((char*)(T), (char*)(F), (C)) > 0 ? EFAULT : 0)
 static inline long copyinstr(char *from, char *to, int count, int *length) {
@@ -137,6 +158,36 @@ static inline long copyinstr(char *from, char *to, int count, int *length) {
 }
 #define copyout(F, T, C) (copy_to_user ((char*)(T), (char*)(F), (C)) > 0 ? EFAULT : 0)
 
+/*
+ * Test to see for 64/32bit compatibility mode
+ * Return non-zero if in a 64bit kernel and handing a 32bit syscall
+ */
+#if defined(AFS_LINUX_64BIT_KERNEL) && !defined(AFS_ALPHA_LINUX_ENV) && !defined(AFS_IA64_LINUX_ENV)
+static inline int
+afs_in_compat_syscall(void)
+{
+# if defined(HAVE_LINUX_IN_COMPAT_SYSCALL)
+    return in_compat_syscall();
+# elif defined(AFS_SPARC64_LINUX_ENV)
+    return test_thread_flag(TIF_32BIT);
+# elif defined(AFS_AMD64_LINUX_ENV)
+    return test_thread_flag(TIF_IA32);
+# elif defined(AFS_PPC64_LINUX_ENV)
+#  if defined(STRUCT_TASK_STRUCT_HAS_THREAD_INFO)
+    return (current->thread_info->flags & _TIF_32BIT) != 0;
+#  else
+    return (task_thread_info(current)->flags & _TIF_32BIT) != 0;
+#  endif
+# elif defined(AFS_S390X_LINUX_ENV)
+   return test_thread_flag(TIF_31BIT);
+# elif defined(AFS_ARM64_LINUX_ENV)
+  return is_compat_task();
+# else
+#  error afs_in_compat_syscall not done for this linux
+# endif
+}
+#endif /* AFS_LINUX_64BIT_KERNEL */
+
 /* kernel print statements */
 #define printf(args...) printk(args)
 #define uprintf(args...) printk(args)
@@ -146,11 +197,16 @@ static inline long copyinstr(char *from, char *to, int count, int *length) {
 #define NGROUPS NGROUPS_SMALL
 #endif
 
+#ifdef STRUCT_GROUP_INFO_HAS_GID
+/* compat macro for Linux 4.9 */
+#define GROUP_AT(gi,x)  ((gi)->gid[x])
+#endif
+
 typedef struct task_struct afs_proc_t;
 
 #ifdef HAVE_LINUX_KUID_T
 
-#include<linux/uidgid.h>
+#include <linux/uidgid.h>
 typedef kuid_t afs_kuid_t;
 typedef kgid_t afs_kgid_t;
 extern struct user_namespace *afs_ns;
@@ -298,7 +354,7 @@ struct uio {
 
 #define OSIFILE_INODE(a) FILE_INODE((a)->filp)
 
-#if defined(AFS_LINUX_64BIT_KERNEL) && !defined(AFS_ALPHA_LINUX20_ENV) && !defined(AFS_IA64_LINUX20_ENV)
+#if defined(AFS_LINUX_64BIT_KERNEL) && !defined(AFS_ALPHA_LINUX_ENV) && !defined(AFS_IA64_LINUX_ENV)
 # define NEED_IOCTL32
 #endif
 
@@ -306,13 +362,7 @@ struct uio {
 #include <linux/sched.h>
 #include <linux/wait.h>
 
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
 extern struct mutex afs_global_lock;
-#else
-extern struct semaphore afs_global_lock;
-# define mutex_lock(lock) down(lock)
-# define mutex_unlock(lock) up(lock)
-#endif
 extern int afs_global_owner;
 
 #define AFS_GLOCK() \
@@ -336,7 +386,7 @@ do { \
 
 #define osi_InitGlock()
 
-#ifdef AFS_AMD64_LINUX20_ENV
+#ifdef AFS_AMD64_LINUX_ENV
 /* RHEL5 beta's kernel doesn't define these. They aren't gonna change, so... */
 
 # ifndef __NR_ia32_afs_syscall