7335c0b496c63cd77c572f86387ae24db60f65f8
[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_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 #define osi_vnhold(V, N) do { VN_HOLD(AFSTOV(V)); } while (0)
107 #define VN_HOLD(V) osi_Assert(igrab((V)) == (V))
108 #define VN_RELE(V) iput((V))
109
110 #define afs_suser(x) capable(CAP_SYS_ADMIN)
111 #define wakeup afs_osi_Wakeup
112
113 #undef vType
114 #define vType(V) ((AFSTOV((V)))->i_mode & S_IFMT)
115 #undef vSetType
116 #define vSetType(V, type) AFSTOV((V))->i_mode = ((type) | (AFSTOV((V))->i_mode & ~S_IFMT))      /* preserve mode */
117
118 #undef IsAfsVnode
119 #define IsAfsVnode(V) ((V)->i_sb == afs_globalVFS)      /* test superblock instead */
120 #undef SetAfsVnode
121 #define SetAfsVnode(V)                                  /* unnecessary */
122
123 /* We often need to pretend we're in user space to get memory transfers
124  * right for the kernel calls we use.
125  */
126 #include <asm/uaccess.h>
127
128 #ifdef KERNEL_SPACE_DECL
129 #undef KERNEL_SPACE_DECL
130 #undef TO_USER_SPACE
131 #undef TO_KERNEL_SPACE
132 #endif
133 #define KERNEL_SPACE_DECL mm_segment_t _fs_space_decl={0}
134 #define TO_USER_SPACE() { _fs_space_decl = get_fs(); set_fs(get_ds()); }
135 #define TO_KERNEL_SPACE() set_fs(_fs_space_decl)
136
137 #define copyin(F, T, C)  (copy_from_user ((char*)(T), (char*)(F), (C)) > 0 ? EFAULT : 0)
138 static inline long copyinstr(char *from, char *to, int count, int *length) {
139     long tmp;
140     tmp = strncpy_from_user(to, from, count);
141     if (tmp < 0)
142             return EFAULT;
143     *length = tmp;
144     return 0;
145 }
146 #define copyout(F, T, C) (copy_to_user ((char*)(T), (char*)(F), (C)) > 0 ? EFAULT : 0)
147
148 /* kernel print statements */
149 #define printf printk
150 #define uprintf printk
151
152
153 #ifndef NGROUPS
154 #define NGROUPS NGROUPS_SMALL
155 #endif
156
157 /* cred struct */
158 typedef struct afs_cred {               /* maps to task field: */
159     int cr_ref;
160     uid_t cr_uid;               /* euid */
161     uid_t cr_ruid;              /* uid */
162     gid_t cr_gid;               /* egid */
163     gid_t cr_rgid;              /* gid */
164 #if defined(AFS_LINUX26_ENV)
165     struct group_info *cr_group_info;
166 #else
167     gid_t cr_groups[NGROUPS];   /* 32 groups - empty set to NOGROUP */
168     int cr_ngroups;
169 #endif
170     struct afs_cred *cr_next;
171 } cred_t;
172 #define AFS_UCRED afs_cred
173 #define AFS_PROC struct task_struct
174 #if !defined(current_cred)
175 #define current_gid() (current->gid)
176 #define current_uid() (current->uid)
177 #define current_fsgid() (current->fsgid)
178 #define current_fsuid() (current->fsuid)
179 #endif
180 #if defined(STRUCT_TASK_HAS_CRED)
181 #define current_group_info() (current->cred->group_info)
182 #define task_gid(task) (task->cred->gid)
183 #define task_user(task) (task->cred->user)
184 #define task_session_keyring(task) (task->cred->tgcred->session_keyring)
185 #define current_session_keyring() (current->cred->tgcred->session_keyring)
186 #else
187 #define current_group_info() (current->group_info)
188 #if !defined(task_gid)
189 #define task_gid(task) (task->gid)
190 #endif
191 #if !defined(task_uid)
192 #define task_uid(task) (task->uid)
193 #endif
194 #define task_user(task) (task->user)
195 #define task_session_keyring(task) (task->signal->session_keyring)
196 #define current_session_keyring() (current->signal->session_keyring)
197 #endif
198 #define crhold(c) (c)->cr_ref++
199
200 /* UIO manipulation */
201 typedef enum { AFS_UIOSYS, AFS_UIOUSER } uio_seg_t;
202 typedef enum { UIO_READ, UIO_WRITE } uio_flag_t;
203 typedef struct uio {
204     struct iovec *uio_iov;
205     int uio_iovcnt;
206     afs_offs_t uio_offset;
207     uio_seg_t uio_seg;
208     int uio_resid;
209     uio_flag_t uio_flag;
210 } uio_t;
211 #define afsio_iov       uio_iov
212 #define afsio_iovcnt    uio_iovcnt
213 #define afsio_offset    uio_offset
214 #define afsio_seg       uio_segflg
215 #define afsio_fmode     uio_fmode
216 #define afsio_resid     uio_resid
217
218 /* Get/set the inode in the osifile struct. */
219 #define FILE_INODE(F) (F)->f_dentry->d_inode
220
221 #ifdef AFS_LINUX26_ENV
222 #define OSIFILE_INODE(a) FILE_INODE((a)->filp)
223 #else
224 #define OSIFILE_INODE(a) FILE_INODE(&(a)->file)
225 #endif
226
227 #if defined(AFS_LINUX_64BIT_KERNEL) && !defined(AFS_ALPHA_LINUX20_ENV) && !defined(AFS_IA64_LINUX20_ENV)
228 #define NEED_IOCTL32
229 #endif
230
231 /* page offset is obtained and stored here during module initialization 
232  * We need a variable to do this because, the PAGE_OFFSET macro defined in
233  * include/asm/page.h can change from kernel to kernel and we cannot use
234  * the hardcoded version.
235  */
236 extern unsigned long afs_linux_page_offset;
237
238 /* function to help with the page offset stuff */
239 #define afs_linux_page_address(page) (afs_linux_page_offset + PAGE_SIZE * (page - mem_map))
240
241 #if defined(__KERNEL__)
242 #include <linux/version.h>
243 #include <linux/sched.h>
244 #include <linux/wait.h>
245
246 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
247 extern struct mutex afs_global_lock;
248 #else
249 extern struct semaphore afs_global_lock;
250 #define mutex_lock(lock) down(lock)
251 #define mutex_unlock(lock) up(lock)
252 #endif
253 extern int afs_global_owner;
254
255 #define AFS_GLOCK() \
256 do { \
257          mutex_lock(&afs_global_lock); \
258          if (afs_global_owner) \
259              osi_Panic("afs_global_lock already held by pid %d", \
260                        afs_global_owner); \
261          afs_global_owner = current->pid; \
262 } while (0)
263
264 #define ISAFS_GLOCK() (afs_global_owner == current->pid)
265
266 #define AFS_GUNLOCK() \
267 do { \
268     if (!ISAFS_GLOCK()) \
269         osi_Panic("afs global lock not held at %s:%d", __FILE__, __LINE__); \
270     afs_global_owner = 0; \
271     mutex_unlock(&afs_global_lock); \
272 } while (0)
273 #else
274 #define AFS_GLOCK()
275 #define AFS_GUNLOCK()
276 #define ISAFS_GLOCK() 1
277 #define AFS_ASSERT_GLOCK()
278 #endif
279
280 #ifdef AFS_AMD64_LINUX20_ENV
281 /* RHEL5 beta's kernel doesn't define these. They aren't gonna change, so... */
282
283 #ifndef __NR_ia32_afs_syscall
284 #define __NR_ia32_afs_syscall 137
285 #endif
286 #ifndef __NR_ia32_setgroups
287 #define __NR_ia32_setgroups 81
288 #endif
289 #ifndef __NR_ia32_setgroups32
290 #define __NR_ia32_setgroups32 206
291 #endif
292 #ifndef __NR_ia32_close
293 #define __NR_ia32_close 6
294 #endif
295 #ifndef __NR_ia32_chdir
296 #define __NR_ia32_chdir 12
297 #endif
298 #ifndef __NR_ia32_break
299 #define __NR_ia32_break 17
300 #endif
301 #ifndef __NR_ia32_stty
302 #define __NR_ia32_stty 31
303 #endif
304 #ifndef __NR_ia32_gtty
305 #define __NR_ia32_gtty 32
306 #endif
307 #ifndef __NR_ia32_ftime
308 #define __NR_ia32_ftime 35
309 #endif
310 #ifndef __NR_ia32_prof
311 #define __NR_ia32_prof 44
312 #endif
313 #ifndef __NR_ia32_lock
314 #define __NR_ia32_lock 53
315 #endif
316 #ifndef __NR_ia32_mpx
317 #define __NR_ia32_mpx 56
318 #endif
319 #ifndef __NR_ia32_exit
320 #define __NR_ia32_exit 1
321 #endif
322 #ifndef __NR_ia32_mount
323 #define __NR_ia32_mount 21
324 #endif
325 #ifndef __NR_ia32_read
326 #define __NR_ia32_read 3
327 #endif
328 #ifndef __NR_ia32_write
329 #define __NR_ia32_write 4
330 #endif
331 #ifndef __NR_ia32_open
332 #define __NR_ia32_open 5
333 #endif
334 #ifndef __NR_ia32_close
335 #define __NR_ia32_close 6
336 #endif
337 #ifndef __NR_ia32_unlink
338 #define __NR_ia32_unlink 10
339 #endif
340 #endif
341
342 #endif /* OSI_MACHDEP_H_ */