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