7d13d64b1f877a1d14e21ec3a52cba0d05f767d8
[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
79 #if defined(AFS_LINUX24_ENV)
80 #define VN_HOLD(V) atomic_inc(&((vnode_t *) V)->i_count)
81 #else
82 #define VN_HOLD(V) ((vnode_t *) V)->i_count++
83 #endif
84
85 #if defined(AFS_LINUX24_ENV)
86 #define VN_RELE(V) iput((struct inode *) V)
87 #else
88 #define VN_RELE(V) osi_iput((struct inode *) V)
89 #endif
90
91 #define osi_AllocSmall afs_osi_Alloc
92 #define osi_FreeSmall afs_osi_Free
93
94 #define afs_suser(x) capable(CAP_SYS_ADMIN)
95 #define wakeup afs_osi_Wakeup
96
97 #undef vType
98 #define vType(V) ((( vnode_t *)V)->v_type & S_IFMT)
99
100 /* IsAfsVnode relies on the fast that there is only one vnodeop table for AFS.
101  * Use the same type of test as other OS's for compatibility.
102  */
103 #undef IsAfsVnode
104 extern struct vnodeops afs_file_iops, afs_dir_iops, afs_symlink_iops;
105 #define IsAfsVnode(v) (((v)->v_op == &afs_file_iops) ? 1 : \
106                         ((v)->v_op == &afs_dir_iops) ? 1 : \
107                         ((v)->v_op == &afs_symlink_iops))
108 #undef SetAfsVnode
109 #define SetAfsVnode(v)
110
111 /* We often need to pretend we're in user space to get memory transfers
112  * right for the kernel calls we use.
113  */
114 #include <asm/uaccess.h>
115
116 #ifdef KERNEL_SPACE_DECL
117 #undef KERNEL_SPACE_DECL
118 #undef TO_USER_SPACE
119 #undef TO_KERNEL_SPACE
120 #endif
121 #define KERNEL_SPACE_DECL mm_segment_t _fs_space_decl
122 #define TO_USER_SPACE() { _fs_space_decl = get_fs(); set_fs(get_ds()); }
123 #define TO_KERNEL_SPACE() set_fs(_fs_space_decl)
124
125 #define copyin(F, T, C)  (copy_from_user ((char*)(T), (char*)(F), (C)) > 0 ? EFAULT : 0)
126 static inline long copyinstr(char *from, char *to, int count, int *length) {
127     long tmp;
128     tmp = strncpy_from_user(to, from, count);
129     if (tmp < 0)
130             return EFAULT;
131     *length = tmp;
132     return 0;
133 }
134 #define copyout(F, T, C) (copy_to_user ((char*)(T), (char*)(F), (C)) > 0 ? EFAULT : 0)
135
136 /* kernel print statements */
137 #define printf printk
138 #define uprintf printk
139
140
141 #define PAGESIZE PAGE_SIZE
142 #ifndef NGROUPS
143 #define NGROUPS NGROUPS_SMALL
144 #endif
145
146 /* cred struct */
147 typedef struct cred {           /* maps to task field: */
148     int cr_ref;
149     uid_t cr_uid;               /* euid */
150     uid_t cr_ruid;              /* uid */
151     gid_t cr_gid;               /* egid */
152     gid_t cr_rgid;              /* gid */
153 #if defined(AFS_LINUX26_ENV)
154     struct group_info *cr_group_info;
155 #else
156     gid_t cr_groups[NGROUPS];   /* 32 groups - empty set to NOGROUP */
157     int cr_ngroups;
158 #endif
159     struct cred *cr_next;
160 } cred_t;
161 #define AFS_UCRED cred
162 #define AFS_PROC struct task_struct
163 #define crhold(c) (c)->cr_ref++
164
165 /* UIO manipulation */
166 typedef enum { AFS_UIOSYS, AFS_UIOUSER } uio_seg_t;
167 typedef enum { UIO_READ, UIO_WRITE } uio_flag_t;
168 typedef struct uio {
169     struct iovec *uio_iov;
170     int uio_iovcnt;
171     afs_offs_t uio_offset;
172     uio_seg_t uio_seg;
173     int uio_resid;
174     uio_flag_t uio_flag;
175 } uio_t;
176 #define afsio_iov       uio_iov
177 #define afsio_iovcnt    uio_iovcnt
178 #define afsio_offset    uio_offset
179 #define afsio_seg       uio_segflg
180 #define afsio_fmode     uio_fmode
181 #define afsio_resid     uio_resid
182
183 /* Get/set the inode in the osifile struct. */
184 #define FILE_INODE(F) (F)->f_dentry->d_inode
185
186 #ifdef AFS_LINUX24_ENV
187 #define OSIFILE_INODE(a) FILE_INODE((a)->filp)
188 #else
189 #define OSIFILE_INODE(a) FILE_INODE(&(a)->file)
190 #endif
191
192 #if defined(AFS_LINUX_64BIT_KERNEL) && !defined(AFS_ALPHA_LINUX20_ENV) && !defined(AFS_IA64_LINUX20_ENV)
193 #define NEED_IOCTL32
194 #endif
195
196 /* page offset is obtained and stored here during module initialization 
197  * We need a variable to do this because, the PAGE_OFFSET macro defined in
198  * include/asm/page.h can change from kernel to kernel and we cannot use
199  * the hardcoded version.
200  */
201 extern unsigned long afs_linux_page_offset;
202
203 /* function to help with the page offset stuff */
204 #define afs_linux_page_address(page) (afs_linux_page_offset + PAGE_SIZE * (page - mem_map))
205
206 #if defined(__KERNEL__)
207 #include "../h/sched.h"
208 #include "linux/wait.h"
209
210 extern struct semaphore afs_global_lock;
211 extern int afs_global_owner;
212
213 #define AFS_GLOCK() \
214 do { \
215          down(&afs_global_lock); \
216          if (afs_global_owner) \
217              osi_Panic("afs_global_lock already held by pid %d", \
218                        afs_global_owner); \
219          afs_global_owner = current->pid; \
220 } while (0)
221
222 #define ISAFS_GLOCK() (afs_global_owner == current->pid)
223
224 #define AFS_GUNLOCK() \
225 do { \
226     if (!ISAFS_GLOCK()) \
227         osi_Panic("afs global lock not held at %s:%d", __FILE__, __LINE__); \
228     afs_global_owner = 0; \
229     up(&afs_global_lock); \
230 } while (0)
231
232
233 #else
234 #define AFS_GLOCK()
235 #define AFS_GUNLOCK()
236 #define ISAFS_GLOCK() 1
237 #define AFS_ASSERT_GLOCK()
238 #endif
239
240 #endif /* OSI_MACHDEP_H_ */