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