Whamcloud - gitweb
LU-9859 libcfs: make signal-blocking functions inline
[fs/lustre-release.git] / libcfs / include / libcfs / libcfs.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #ifndef __LIBCFS_LIBCFS_H__
34 #define __LIBCFS_LIBCFS_H__
35
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/workqueue.h>
39 #include <linux/sched.h>
40 #ifdef HAVE_SCHED_HEADERS
41 #include <linux/sched/signal.h>
42 #endif
43
44 #include <libcfs/linux/linux-misc.h>
45 #include <libcfs/linux/linux-time.h>
46 #include <libcfs/linux/linux-wait.h>
47
48 #include <uapi/linux/lnet/libcfs_ioctl.h>
49 #include <libcfs/libcfs_debug.h>
50 #include <libcfs/libcfs_private.h>
51 #include <libcfs/bitmap.h>
52 #include <libcfs/libcfs_cpu.h>
53 #include <libcfs/libcfs_prim.h>
54 #include <libcfs/libcfs_string.h>
55 #include <libcfs/libcfs_workitem.h>
56 #include <libcfs/libcfs_hash.h>
57 #include <libcfs/libcfs_heap.h>
58 #include <libcfs/libcfs_fail.h>
59 #include "curproc.h"
60
61 #define LIBCFS_VERSION  "0.7.1"
62
63 #define PO2_ROUNDUP_TYPED(x, po2, type) (-(-(type)(x) & -(type)(po2)))
64 #define LOWEST_BIT_SET(x) ((x) & ~((x) - 1))
65
66 /* Sparse annotations */
67 #if !defined(__must_hold)
68 # ifdef __CHECKER__
69 #  define __must_hold(x) __attribute__((context(x, 1, 1)))
70 # else  /* __CHECKER__ */
71 #  define __must_hold(x)
72 # endif /* !__CHECKER__ */
73 #endif /* !__must_hold */
74
75 #ifdef HAVE_TOTALRAM_PAGES_AS_FUNC
76  #ifndef cfs_totalram_pages
77   #define cfs_totalram_pages() totalram_pages()
78  #endif
79 #else
80  #ifndef cfs_totalram_pages
81   #define cfs_totalram_pages() totalram_pages
82  #endif
83 #endif
84
85 #ifdef HAVE_FORCE_SIG_WITH_TASK
86 #define cfs_force_sig(sig, task)        force_sig((sig), (task))
87 #else
88 #define cfs_force_sig(sig, task)                                        \
89 do {                                                                    \
90         unsigned long flags;                                            \
91                                                                         \
92         spin_lock_irqsave(&task->sighand->siglock, flags);              \
93         task->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;         \
94         send_sig(sig, task, 1);                                         \
95         spin_unlock_irqrestore(&task->sighand->siglock, flags);         \
96 } while (0)
97 #endif
98
99 typedef s32 timeout_t;
100
101 /* Block all signals except for the @sigs */
102 static inline void cfs_block_sigsinv(unsigned long sigs, sigset_t *old)
103 {
104         sigset_t new;
105
106         siginitsetinv(&new, sigs);
107         sigorsets(&new, &current->blocked, &new);
108         sigprocmask(SIG_BLOCK, &new, old);
109 }
110
111 static inline void
112 cfs_restore_sigs(sigset_t *old)
113 {
114         sigprocmask(SIG_SETMASK, old, NULL);
115 }
116
117 /* need both kernel and user-land acceptor */
118 #define LNET_ACCEPTOR_MIN_RESERVED_PORT    512
119 #define LNET_ACCEPTOR_MAX_RESERVED_PORT    1023
120
121 extern struct blocking_notifier_head libcfs_ioctl_list;
122 static inline int notifier_from_ioctl_errno(int err)
123 {
124         if (err == -EINVAL)
125                 return NOTIFY_OK;
126         return notifier_from_errno(err) | NOTIFY_STOP_MASK;
127 }
128
129 /*
130  * Defined by platform
131  */
132 int unshare_fs_struct(void);
133 void cfs_block_sigsinv(unsigned long sigs, sigset_t *sigset);
134 void cfs_restore_sigs(sigset_t *sigset);
135
136 int libcfs_ioctl_data_adjust(struct libcfs_ioctl_data *data);
137
138 #define container_of0(ptr, type, member) container_of_safe(ptr, type, member)
139
140 extern struct workqueue_struct *cfs_rehash_wq;
141
142 struct lnet_debugfs_symlink_def {
143         const char *name;
144         const char *target;
145 };
146
147 void lnet_insert_debugfs(struct ctl_table *table);
148 void lnet_remove_debugfs(struct ctl_table *table);
149
150 /* helper for sysctl handlers */
151 int lprocfs_call_handler(void *data, int write, loff_t *ppos,
152                          void __user *buffer, size_t *lenp,
153                          int (*handler)(void *data, int write, loff_t pos,
154                                         void __user *buffer, int len));
155
156
157 #define wait_var_event_warning(var, condition, format, ...)             \
158 do {                                                                    \
159         int counter = 4;                                                \
160         might_sleep();                                                  \
161         if (condition)                                                  \
162                 break;                                                  \
163         ___wait_var_event(var, condition, TASK_UNINTERRUPTIBLE, 0, 0,   \
164                           if (schedule_timeout(cfs_time_seconds(1)) == 0)\
165                                   CDEBUG(is_power_of_2(counter++) ?     \
166                                          D_WARNING : D_NET,             \
167                                          format, ## __VA_ARGS__)        \
168                 );                                                      \
169 } while (0)
170
171 #endif /* _LIBCFS_LIBCFS_H_ */