LINUX: defer afs_remunlink when current->fs==NULL
[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 #ifdef STRUCT_TASK_STRUCT_HAS_TGID
27 # define getpid() current->tgid
28 # ifdef STRUCT_TASK_STRUCT_HAS_REAL_PARENT
29 #  define getppid() current->real_parent->tgid
30 # elif defined(STRUCT_TASK_STRUCT_HAS_PARENT)
31 #  define getppid() current->parent->tgid
32 # else
33 #  define getppid() current->p_opptr->tgid
34 # endif
35 #else /* !STRUCT_TASK_STRUCT_HAS_TGID */
36 # define getpid() current->pid
37 # ifdef STRUCT_TASK_STRUCT_HAS_REAL_PARENT
38 #  define getppid() current->real_parent->pid
39 # elif defined(STRUCT_TASK_STRUCT_HAS_PARENT)
40 #  define getppid() current->parent->pid
41 # else
42 #  define getppid() current->p_opptr->pid
43 # endif
44 #endif /* STRUCT_TASK_STRUCT_HAS_TGID */
45
46 #ifdef RECALC_SIGPENDING_TAKES_VOID
47 # define RECALC_SIGPENDING(X) recalc_sigpending()
48 #else
49 # define RECALC_SIGPENDING(X) recalc_sigpending(X)
50 #endif
51
52 #if defined (STRUCT_TASK_STRUCT_HAS_SIGMASK_LOCK)
53 # define SIG_LOCK(X) spin_lock_irq(&X->sigmask_lock)
54 # define SIG_UNLOCK(X) spin_unlock_irq(&X->sigmask_lock)
55 #elif defined (STRUCT_TASK_STRUCT_HAS_SIGHAND)
56 # define SIG_LOCK(X) spin_lock_irq(&X->sighand->siglock)
57 # define SIG_UNLOCK(X) spin_unlock_irq(&X->sighand->siglock)
58 #else
59 # define SIG_LOCK(X) spin_lock_irq(&X->sig->siglock)
60 # define SIG_UNLOCK(X) spin_unlock_irq(&X->sig->siglock)
61 #endif
62
63 #if defined (STRUCT_TASK_STRUCT_HAS_RLIM)
64 # define TASK_STRUCT_RLIM rlim
65 #elif defined (STRUCT_TASK_STRUCT_HAS_SIGNAL_RLIM)
66 # define TASK_STRUCT_RLIM signal->rlim
67 #else
68 # error Not sure what to do about rlim (should be in the Linux task struct somewhere....)
69 #endif
70
71
72 #define afs_hz HZ
73 #include "h/sched.h"
74 /* in case cred.h is present but not included in sched.h */
75 #if defined(HAVE_LINUX_CRED_H)
76 #include "h/cred.h"
77 #endif
78
79 #include "afs/sysincludes.h"
80
81 #if !defined(HAVE_LINUX_TIME_T)
82 typedef time64_t time_t;
83 #endif
84
85 #if defined(HAVE_LINUX_KTIME_GET_COARSE_REAL_TS64)
86 static inline time_t osi_Time(void) {
87     struct timespec64 xtime;
88     ktime_get_coarse_real_ts64(&xtime);
89     return xtime.tv_sec;
90 }
91 #elif defined(HAVE_LINUX_CURRENT_KERNEL_TIME)
92 static inline time_t osi_Time(void) {
93     struct timespec xtime;
94     xtime = current_kernel_time();
95     return xtime.tv_sec;
96 }
97 #else
98 # define osi_Time() (xtime.tv_sec)
99 #endif
100
101 #if defined(HAVE_LINUX_KTIME_GET_REAL_TS64)
102 static inline void
103 osi_GetTime(osi_timeval32_t *atv)
104 {
105     struct timespec64 now;
106     ktime_get_real_ts64(&now);
107     atv->tv_sec = now.tv_sec;
108     atv->tv_usec = now.tv_nsec / 1000;
109 }
110 #else
111 static inline void
112 osi_GetTime(osi_timeval32_t *atv)
113 {
114     struct timeval now;
115     do_gettimeofday(&now);
116     atv->tv_sec = now.tv_sec;
117     atv->tv_usec = now.tv_usec;
118 }
119 #endif
120
121 #undef gop_lookupname
122 #define gop_lookupname osi_lookupname
123
124 #undef gop_lookupname_user
125 #define gop_lookupname_user osi_lookupname
126
127 #define VN_HOLD(V) osi_Assert(igrab((V)) == (V))
128 #define VN_RELE(V) iput((V))
129
130 #define afs_suser(x) capable(CAP_SYS_ADMIN)
131 extern int afs_osi_Wakeup(void *event);
132 static inline void
133 wakeup(void *event)
134 {
135     afs_osi_Wakeup(event);
136 }
137
138 #define vType(V) ((AFSTOV((V)))->i_mode & S_IFMT)
139 #define vSetType(V, type) AFSTOV((V))->i_mode = ((type) | (AFSTOV((V))->i_mode & ~S_IFMT))      /* preserve mode */
140 #define vSetVfsp(V, vfsp)                               /* unused */
141 #define IsAfsVnode(V) ((V)->i_sb == afs_globalVFS)      /* test superblock instead */
142 #define SetAfsVnode(V)                                  /* unnecessary */
143
144 #if defined(HAVE_LINUX_UACCESS_H)
145 #include <linux/uaccess.h>
146 #else
147 #include <asm/uaccess.h>
148 #endif
149
150 #define copyin(F, T, C)  (copy_from_user ((char*)(T), (char*)(F), (C)) > 0 ? EFAULT : 0)
151 static inline long copyinstr(char *from, char *to, int count, int *length) {
152     long tmp;
153     tmp = strncpy_from_user(to, from, count);
154     if (tmp < 0)
155             return EFAULT;
156     *length = tmp;
157     return 0;
158 }
159 #define copyout(F, T, C) (copy_to_user ((char*)(T), (char*)(F), (C)) > 0 ? EFAULT : 0)
160
161 /*
162  * Test to see for 64/32bit compatibility mode
163  * Return non-zero if in a 64bit kernel and handing a 32bit syscall
164  */
165 #if defined(AFS_LINUX_64BIT_KERNEL) && !defined(AFS_ALPHA_LINUX_ENV) && !defined(AFS_IA64_LINUX_ENV)
166 static inline int
167 afs_in_compat_syscall(void)
168 {
169 # if defined(HAVE_LINUX_IN_COMPAT_SYSCALL)
170     return in_compat_syscall();
171 # elif defined(AFS_SPARC64_LINUX_ENV)
172     return test_thread_flag(TIF_32BIT);
173 # elif defined(AFS_AMD64_LINUX_ENV)
174     return test_thread_flag(TIF_IA32);
175 # elif defined(AFS_PPC64_LINUX_ENV)
176 #  if defined(STRUCT_TASK_STRUCT_HAS_THREAD_INFO)
177     return (current->thread_info->flags & _TIF_32BIT) != 0;
178 #  else
179     return (task_thread_info(current)->flags & _TIF_32BIT) != 0;
180 #  endif
181 # elif defined(AFS_S390X_LINUX_ENV)
182    return test_thread_flag(TIF_31BIT);
183 # elif defined(AFS_ARM64_LINUX_ENV)
184   return is_compat_task();
185 # else
186 #  error afs_in_compat_syscall not done for this linux
187 # endif
188 }
189 #endif /* AFS_LINUX_64BIT_KERNEL */
190
191 /* kernel print statements */
192 #define printf(args...) printk(args)
193 #define uprintf(args...) printk(args)
194
195
196 #ifndef NGROUPS
197 #define NGROUPS NGROUPS_SMALL
198 #endif
199
200 #ifdef STRUCT_GROUP_INFO_HAS_GID
201 /* compat macro for Linux 4.9 */
202 #define GROUP_AT(gi,x)  ((gi)->gid[x])
203 #endif
204
205 typedef struct task_struct afs_proc_t;
206
207 #ifdef HAVE_LINUX_KUID_T
208
209 #include <linux/uidgid.h>
210 typedef kuid_t afs_kuid_t;
211 typedef kgid_t afs_kgid_t;
212 extern struct user_namespace *afs_ns;
213 # ifdef CONFIG_USER_NS
214 #  define afs_current_user_ns() current_user_ns()
215 # else
216 /* Here current_user_ns() expands to GPL-only init_user_ns symbol! */
217 #  define afs_current_user_ns() ((struct user_namespace *)NULL)
218 # endif
219
220 static inline kuid_t afs_make_kuid(uid_t uid) {
221     return make_kuid(afs_ns, uid);
222 }
223 static inline kgid_t afs_make_kgid(gid_t gid) {
224     return make_kgid(afs_ns, gid);
225 }
226 static inline uid_t afs_from_kuid(kuid_t kuid) {
227     return from_kuid(afs_ns, kuid);
228 }
229 static inline uid_t afs_from_kgid(kgid_t kgid) {
230     return from_kgid(afs_ns, kgid);
231 }
232
233 #else
234
235 typedef uid_t afs_kuid_t;
236 typedef gid_t afs_kgid_t;
237
238 static inline afs_kuid_t afs_make_kuid(uid_t uid) {return uid;}
239 static inline afs_kgid_t afs_make_kgid(gid_t gid) {return gid;}
240 static inline uid_t afs_from_kuid(afs_kuid_t kuid) {return kuid;}
241 static inline gid_t afs_from_kgid(afs_kgid_t kgid) {return kgid;}
242 static inline unsigned char uid_eq(uid_t a, uid_t b) {return a == b;}
243 static inline unsigned char gid_eq(gid_t a, gid_t b) {return a == b;}
244 static inline unsigned char uid_lt(uid_t a, uid_t b) {return a < b;}
245 static inline unsigned char gid_lt(gid_t a, gid_t b) {return a < b;}
246 #define GLOBAL_ROOT_UID ((afs_kuid_t) 0)
247 #define GLOBAL_ROOT_GID ((afs_kgid_t) 0)
248
249 #endif
250
251 /* Credentials.  For newer kernels we use the kernel structure directly. */
252 #if defined(STRUCT_TASK_STRUCT_HAS_CRED)
253
254 typedef struct cred afs_ucred_t;
255 typedef struct cred cred_t;
256
257 # define afs_cr_uid(cred) (afs_from_kuid((cred)->fsuid))
258 # define afs_cr_gid(cred) (afs_from_kgid((cred)->fsgid))
259 # define afs_cr_ruid(cred) (afs_from_kuid((cred)->uid))
260 # define afs_cr_rgid(cred) (afs_from_kgid((cred)->gid))
261 # define afs_cr_group_info(cred) ((cred)->group_info)
262 # define crhold(c) (get_cred(c))
263 static inline void
264 afs_set_cr_uid(cred_t *cred, uid_t uid) {
265     cred->fsuid = afs_make_kuid(uid);
266 }
267 static inline void
268 afs_set_cr_gid(cred_t *cred, gid_t gid) {
269     cred->fsgid = afs_make_kgid(gid);
270 }
271 static inline void
272 afs_set_cr_ruid(cred_t *cred, uid_t uid) {
273     cred->uid = afs_make_kuid(uid);
274 }
275 static inline void
276 afs_set_cr_rgid(cred_t *cred, gid_t gid) {
277     cred->gid = afs_make_kgid(gid);
278 }
279 static inline void
280 afs_set_cr_group_info(cred_t *cred, struct group_info *group_info) {
281     cred->group_info = group_info;
282 }
283
284 # define current_group_info() (current->cred->group_info)
285 # define task_gid(task) (task->cred->gid)
286 # define task_user(task) (task->cred->user)
287 # if defined(STRUCT_CRED_HAS_SESSION_KEYRING)
288 #  define task_session_keyring(task) (task->cred->session_keyring)
289 #  define current_session_keyring() (current->cred->session_keyring)
290 # else
291 #  define task_session_keyring(task) (task->cred->tgcred->session_keyring)
292 #  define current_session_keyring() (current->cred->tgcred->session_keyring)
293 # endif
294
295 #else
296
297 typedef struct afs_cred {
298     atomic_t cr_ref;
299     uid_t cr_uid;
300     uid_t cr_ruid;
301     gid_t cr_gid;
302     gid_t cr_rgid;
303     struct group_info *cr_group_info;
304 } cred_t;
305
306 typedef struct afs_cred afs_ucred_t;
307 # define afs_cr_group_info(cred) ((cred)->cr_group_info)
308 static inline void
309 afs_set_cr_group_info(cred_t *cred, struct group_info *group_info) {
310     cred->cr_group_info = group_info;
311 }
312
313 # define current_group_info() (current->group_info)
314 # if !defined(task_gid)
315 #  define task_gid(task) (task->gid)
316 # endif
317 # if !defined(task_uid)
318 #  define task_uid(task) (task->uid)
319 # endif
320 # define task_user(task) (task->user)
321 # define task_session_keyring(task) (task->signal->session_keyring)
322 # define current_session_keyring() (current->signal->session_keyring)
323 # define crhold(c) atomic_inc(&(c)->cr_ref)
324
325 #endif /* defined(STRUCT_TASK_STRUCT_HAS_CRED) */
326
327 #if !defined(current_cred)
328 # define current_gid() (current->gid)
329 # define current_uid() (current->uid)
330 # define current_fsgid() (current->fsgid)
331 # define current_fsuid() (current->fsuid)
332 #endif
333
334 /* UIO manipulation */
335 typedef enum { AFS_UIOSYS, AFS_UIOUSER } uio_seg_t;
336 typedef enum { UIO_READ, UIO_WRITE } uio_flag_t;
337 struct uio {
338     struct iovec *uio_iov;
339     int uio_iovcnt;
340     afs_offs_t uio_offset;
341     uio_seg_t uio_seg;
342     int uio_resid;
343     uio_flag_t uio_flag;
344 };
345 #define afsio_iov       uio_iov
346 #define afsio_iovcnt    uio_iovcnt
347 #define afsio_offset    uio_offset
348 #define afsio_seg       uio_segflg
349 #define afsio_fmode     uio_fmode
350 #define afsio_resid     uio_resid
351
352 /* Get/set the inode in the osifile struct. */
353 #define FILE_INODE(F) (F)->f_dentry->d_inode
354
355 #define OSIFILE_INODE(a) FILE_INODE((a)->filp)
356
357 #if defined(AFS_LINUX_64BIT_KERNEL) && !defined(AFS_ALPHA_LINUX_ENV) && !defined(AFS_IA64_LINUX_ENV)
358 # define NEED_IOCTL32
359 #endif
360
361 #include <linux/version.h>
362 #include <linux/sched.h>
363 #include <linux/wait.h>
364
365 extern struct mutex afs_global_lock;
366 extern int afs_global_owner;
367
368 #define AFS_GLOCK() \
369 do { \
370          mutex_lock(&afs_global_lock); \
371          if (afs_global_owner) \
372              osi_Panic("afs_global_lock already held by pid %d", \
373                        afs_global_owner); \
374          afs_global_owner = current->pid; \
375 } while (0)
376
377 #define ISAFS_GLOCK() (afs_global_owner == current->pid)
378
379 #define AFS_GUNLOCK() \
380 do { \
381     if (!ISAFS_GLOCK()) \
382         osi_Panic("afs global lock not held at %s:%d", __FILE__, __LINE__); \
383     afs_global_owner = 0; \
384     mutex_unlock(&afs_global_lock); \
385 } while (0)
386
387 #define osi_InitGlock()
388
389 #ifdef AFS_AMD64_LINUX_ENV
390 /* RHEL5 beta's kernel doesn't define these. They aren't gonna change, so... */
391
392 # ifndef __NR_ia32_afs_syscall
393 #  define __NR_ia32_afs_syscall 137
394 # endif
395 # ifndef __NR_ia32_setgroups
396 #  define __NR_ia32_setgroups 81
397 # endif
398 # ifndef __NR_ia32_setgroups32
399 #  define __NR_ia32_setgroups32 206
400 # endif
401 # ifndef __NR_ia32_close
402 #  define __NR_ia32_close 6
403 # endif
404 # ifndef __NR_ia32_chdir
405 #  define __NR_ia32_chdir 12
406 # endif
407 # ifndef __NR_ia32_break
408 #  define __NR_ia32_break 17
409 # endif
410 # ifndef __NR_ia32_stty
411 #  define __NR_ia32_stty 31
412 # endif
413 # ifndef __NR_ia32_gtty
414 #  define __NR_ia32_gtty 32
415 # endif
416 # ifndef __NR_ia32_ftime
417 #  define __NR_ia32_ftime 35
418 # endif
419 # ifndef __NR_ia32_prof
420 #  define __NR_ia32_prof 44
421 # endif
422 # ifndef __NR_ia32_lock
423 #  define __NR_ia32_lock 53
424 # endif
425 # ifndef __NR_ia32_mpx
426 #  define __NR_ia32_mpx 56
427 # endif
428 # ifndef __NR_ia32_exit
429 #  define __NR_ia32_exit 1
430 # endif
431 # ifndef __NR_ia32_mount
432 #  define __NR_ia32_mount 21
433 # endif
434 # ifndef __NR_ia32_read
435 #  define __NR_ia32_read 3
436 # endif
437 # ifndef __NR_ia32_write
438 #  define __NR_ia32_write 4
439 # endif
440 # ifndef __NR_ia32_open
441 #  define __NR_ia32_open 5
442 # endif
443 # ifndef __NR_ia32_close
444 #  define __NR_ia32_close 6
445 # endif
446 # ifndef __NR_ia32_unlink
447 #  define __NR_ia32_unlink 10
448 # endif
449 #endif
450
451 #define osi_procname(procname, size) strncpy(procname, current->comm, size)
452
453 #endif /* OSI_MACHDEP_H_ */