52488af124f98c60f0d49c51b376f7ae3bb4dd65
[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 #define getpid() current->pid
32 #ifdef STRUCT_TASK_STRUCT_HAS_REAL_PARENT
33 #define getppid() current->real_parent->pid
34 #elif defined(STRUCT_TASK_STRUCT_HAS_PARENT)
35 #define getppid() current->parent->pid
36 #else
37 #define getppid() current->p_opptr->pid
38 #endif
39
40 #ifdef RECALC_SIGPENDING_TAKES_VOID
41 #define RECALC_SIGPENDING(X) recalc_sigpending()
42 #else
43 #define RECALC_SIGPENDING(X) recalc_sigpending(X)
44 #endif
45
46 #if defined (STRUCT_TASK_STRUCT_HAS_SIGMASK_LOCK)
47 #define SIG_LOCK(X) spin_lock_irq(&X->sigmask_lock)
48 #define SIG_UNLOCK(X) spin_unlock_irq(&X->sigmask_lock)
49 #elif defined (STRUCT_TASK_STRUCT_HAS_SIGHAND)
50 #define SIG_LOCK(X) spin_lock_irq(&X->sighand->siglock)
51 #define SIG_UNLOCK(X) spin_unlock_irq(&X->sighand->siglock)
52 #else
53 #define SIG_LOCK(X) spin_lock_irq(&X->sig->siglock)
54 #define SIG_UNLOCK(X) spin_unlock_irq(&X->sig->siglock)
55 #endif
56
57 #if defined (STRUCT_TASK_STRUCT_HAS_RLIM)
58 #define TASK_STRUCT_RLIM rlim
59 #elif defined (STRUCT_TASK_STRUCT_HAS_SIGNAL_RLIM)
60 #define TASK_STRUCT_RLIM signal->rlim
61 #else
62 #error Not sure what to do about rlim (should be in the Linux task struct somewhere....)
63 #endif
64
65
66 #define afs_hz HZ
67 #include "h/sched.h"
68 #define osi_Time() (xtime.tv_sec)
69 #ifdef AFS_LINUX_64BIT_KERNEL
70 #define osi_GetTime(V)                                 \
71     do {                                               \
72        struct timeval __afs_tv;                              \
73        do_gettimeofday(&__afs_tv);                           \
74        (V)->tv_sec = (afs_int32)__afs_tv.tv_sec;             \
75        (V)->tv_usec = (afs_int32)__afs_tv.tv_usec;           \
76     } while (0)
77 #else
78 #define osi_GetTime(V) do_gettimeofday((V))
79 #endif
80
81 #undef gop_lookupname
82 #define gop_lookupname osi_lookupname
83
84 #define osi_vnhold(V, N) do { VN_HOLD(AFSTOV(V)); } while (0)
85 #define VN_HOLD(V) osi_Assert(igrab((V)) == (V))
86 #define VN_RELE(V) iput((V))
87
88 #define afs_suser(x) capable(CAP_SYS_ADMIN)
89 #define wakeup afs_osi_Wakeup
90
91 #undef vType
92 #define vType(V) ((AFSTOV((V)))->i_mode & S_IFMT)
93 #undef vSetType
94 #define vSetType(V, type) AFSTOV((V))->i_mode = ((type) | (AFSTOV((V))->i_mode & ~S_IFMT))      /* preserve mode */
95
96 #undef IsAfsVnode
97 #define IsAfsVnode(V) ((V)->i_sb == afs_globalVFS)      /* test superblock instead */
98 #undef SetAfsVnode
99 #define SetAfsVnode(V)                                  /* unnecessary */
100
101 /* We often need to pretend we're in user space to get memory transfers
102  * right for the kernel calls we use.
103  */
104 #include <asm/uaccess.h>
105
106 #ifdef KERNEL_SPACE_DECL
107 #undef KERNEL_SPACE_DECL
108 #undef TO_USER_SPACE
109 #undef TO_KERNEL_SPACE
110 #endif
111 #define KERNEL_SPACE_DECL mm_segment_t _fs_space_decl
112 #define TO_USER_SPACE() { _fs_space_decl = get_fs(); set_fs(get_ds()); }
113 #define TO_KERNEL_SPACE() set_fs(_fs_space_decl)
114
115 #define copyin(F, T, C)  (copy_from_user ((char*)(T), (char*)(F), (C)) > 0 ? EFAULT : 0)
116 static inline long copyinstr(char *from, char *to, int count, int *length) {
117     long tmp;
118     tmp = strncpy_from_user(to, from, count);
119     if (tmp < 0)
120             return EFAULT;
121     *length = tmp;
122     return 0;
123 }
124 #define copyout(F, T, C) (copy_to_user ((char*)(T), (char*)(F), (C)) > 0 ? EFAULT : 0)
125
126 /* kernel print statements */
127 #define printf printk
128 #define uprintf printk
129
130
131 #ifndef NGROUPS
132 #define NGROUPS NGROUPS_SMALL
133 #endif
134
135 /* cred struct */
136 typedef struct cred {           /* maps to task field: */
137     int cr_ref;
138     uid_t cr_uid;               /* euid */
139     uid_t cr_ruid;              /* uid */
140     gid_t cr_gid;               /* egid */
141     gid_t cr_rgid;              /* gid */
142 #if defined(AFS_LINUX26_ENV)
143     struct group_info *cr_group_info;
144 #else
145     gid_t cr_groups[NGROUPS];   /* 32 groups - empty set to NOGROUP */
146     int cr_ngroups;
147 #endif
148     struct cred *cr_next;
149 } cred_t;
150 #define AFS_UCRED cred
151 #define AFS_PROC struct task_struct
152 #define crhold(c) (c)->cr_ref++
153
154 /* UIO manipulation */
155 typedef enum { AFS_UIOSYS, AFS_UIOUSER } uio_seg_t;
156 typedef enum { UIO_READ, UIO_WRITE } uio_flag_t;
157 typedef struct uio {
158     struct iovec *uio_iov;
159     int uio_iovcnt;
160     afs_offs_t uio_offset;
161     uio_seg_t uio_seg;
162     int uio_resid;
163     uio_flag_t uio_flag;
164 } uio_t;
165 #define afsio_iov       uio_iov
166 #define afsio_iovcnt    uio_iovcnt
167 #define afsio_offset    uio_offset
168 #define afsio_seg       uio_segflg
169 #define afsio_fmode     uio_fmode
170 #define afsio_resid     uio_resid
171
172 /* Get/set the inode in the osifile struct. */
173 #define FILE_INODE(F) (F)->f_dentry->d_inode
174
175 #ifdef AFS_LINUX26_ENV
176 #define OSIFILE_INODE(a) FILE_INODE((a)->filp)
177 #else
178 #define OSIFILE_INODE(a) FILE_INODE(&(a)->file)
179 #endif
180
181 #if defined(AFS_LINUX_64BIT_KERNEL) && !defined(AFS_ALPHA_LINUX20_ENV) && !defined(AFS_IA64_LINUX20_ENV)
182 #define NEED_IOCTL32
183 #endif
184
185 /* page offset is obtained and stored here during module initialization 
186  * We need a variable to do this because, the PAGE_OFFSET macro defined in
187  * include/asm/page.h can change from kernel to kernel and we cannot use
188  * the hardcoded version.
189  */
190 extern unsigned long afs_linux_page_offset;
191
192 /* function to help with the page offset stuff */
193 #define afs_linux_page_address(page) (afs_linux_page_offset + PAGE_SIZE * (page - mem_map))
194
195 #if defined(__KERNEL__)
196 #include <linux/version.h>
197 #include <linux/sched.h>
198 #include <linux/wait.h>
199
200 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
201 extern struct mutex afs_global_lock;
202 #else
203 extern struct semaphore afs_global_lock;
204 #define mutex_lock(lock) down(lock)
205 #define mutex_unlock(lock) up(lock)
206 #endif
207 extern int afs_global_owner;
208
209 #define AFS_GLOCK() \
210 do { \
211          mutex_lock(&afs_global_lock); \
212          if (afs_global_owner) \
213              osi_Panic("afs_global_lock already held by pid %d", \
214                        afs_global_owner); \
215          afs_global_owner = current->pid; \
216 } while (0)
217
218 #define ISAFS_GLOCK() (afs_global_owner == current->pid)
219
220 #define AFS_GUNLOCK() \
221 do { \
222     if (!ISAFS_GLOCK()) \
223         osi_Panic("afs global lock not held at %s:%d", __FILE__, __LINE__); \
224     afs_global_owner = 0; \
225     mutex_unlock(&afs_global_lock); \
226 } while (0)
227 #else
228 #define AFS_GLOCK()
229 #define AFS_GUNLOCK()
230 #define ISAFS_GLOCK() 1
231 #define AFS_ASSERT_GLOCK()
232 #endif
233
234 #ifdef AFS_AMD64_LINUX20_ENV
235 /* RHEL5 beta's kernel doesn't define these. They aren't gonna change, so... */
236
237 #ifndef __NR_ia32_afs_syscall
238 #define __NR_ia32_afs_syscall 137
239 #endif
240 #ifndef __NR_ia32_setgroups
241 #define __NR_ia32_setgroups 81
242 #endif
243 #ifndef __NR_ia32_setgroups32
244 #define __NR_ia32_setgroups32 206
245 #endif
246 #ifndef __NR_ia32_close
247 #define __NR_ia32_close 6
248 #endif
249 #ifndef __NR_ia32_chdir
250 #define __NR_ia32_chdir 12
251 #endif
252 #ifndef __NR_ia32_break
253 #define __NR_ia32_break 17
254 #endif
255 #ifndef __NR_ia32_stty
256 #define __NR_ia32_stty 31
257 #endif
258 #ifndef __NR_ia32_gtty
259 #define __NR_ia32_gtty 32
260 #endif
261 #ifndef __NR_ia32_ftime
262 #define __NR_ia32_ftime 35
263 #endif
264 #ifndef __NR_ia32_prof
265 #define __NR_ia32_prof 44
266 #endif
267 #ifndef __NR_ia32_lock
268 #define __NR_ia32_lock 53
269 #endif
270 #ifndef __NR_ia32_mpx
271 #define __NR_ia32_mpx 56
272 #endif
273 #ifndef __NR_ia32_exit
274 #define __NR_ia32_exit 1
275 #endif
276 #ifndef __NR_ia32_mount
277 #define __NR_ia32_mount 21
278 #endif
279 #ifndef __NR_ia32_read
280 #define __NR_ia32_read 3
281 #endif
282 #ifndef __NR_ia32_write
283 #define __NR_ia32_write 4
284 #endif
285 #ifndef __NR_ia32_open
286 #define __NR_ia32_open 5
287 #endif
288 #ifndef __NR_ia32_close
289 #define __NR_ia32_close 6
290 #endif
291 #ifndef __NR_ia32_unlink
292 #define __NR_ia32_unlink 10
293 #endif
294 #endif
295
296 #endif /* OSI_MACHDEP_H_ */