Whamcloud - gitweb
eb5bb5a3132a6bd923748ad2834454311d2e5da4
[fs/lustre-release.git] / libcfs / include / libcfs / user-prim.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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * libcfs/include/libcfs/user-prim.h
37  *
38  * Author: Nikita Danilov <nikita@clusterfs.com>
39  */
40
41 #ifndef __LIBCFS_USER_PRIM_H__
42 #define __LIBCFS_USER_PRIM_H__
43
44 #ifndef __LIBCFS_LIBCFS_H__
45 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
46 #endif
47
48 /* Implementations of portable APIs for liblustre */
49
50 /*
51  * liblustre is single-threaded, so most "synchronization" APIs are trivial.
52  */
53
54 #ifndef EXPORT_SYMBOL
55 # define EXPORT_SYMBOL(s)
56 #endif
57
58 /*
59  * Just present a single processor until will add thread support.
60  */
61 #ifndef smp_processor_id
62 # define smp_processor_id() 0
63 #endif
64 #ifndef num_online_cpus
65 # define num_online_cpus() 1
66 #endif
67 #ifndef num_possible_cpus
68 # define num_possible_cpus() 1
69 #endif
70 #ifndef get_cpu
71 # define get_cpu() 0
72 #endif
73 #ifndef put_cpu
74 # define put_cpu() do {} while (0)
75 #endif
76 #ifndef NR_CPUS
77 # define NR_CPUS 1
78 #endif
79 #ifndef for_each_possible_cpu
80 # define for_each_possible_cpu(cpu) for ((cpu) = 0; (cpu) < 1; (cpu)++)
81 #endif
82
83 /*
84  * Wait Queue.
85  */
86
87 typedef struct cfs_waitlink {
88         struct list_head sleeping;
89         void *process;
90 } wait_queue_t;
91
92 typedef struct cfs_waitq {
93         struct list_head sleepers;
94 } wait_queue_head_t;
95
96 #define CFS_DECL_WAITQ(wq) wait_queue_head_t wq
97 void init_waitqueue_head(struct cfs_waitq *waitq);
98 void init_waitqueue_entry_current(struct cfs_waitlink *link);
99 void add_wait_queue(struct cfs_waitq *waitq, struct cfs_waitlink *link);
100 void add_wait_queue_exclusive(struct cfs_waitq *waitq, struct cfs_waitlink *link);
101 void add_wait_queue_exclusive_head(struct cfs_waitq *waitq, struct cfs_waitlink *link);
102 void remove_wait_queue(struct cfs_waitq *waitq, struct cfs_waitlink *link);
103 int waitqueue_active(struct cfs_waitq *waitq);
104 void wake_up(struct cfs_waitq *waitq);
105 void wake_up_nr(struct cfs_waitq *waitq, int nr);
106 void wake_up_all(struct cfs_waitq *waitq);
107 void waitq_wait(struct cfs_waitlink *link, long state);
108 int64_t waitq_timedwait(struct cfs_waitlink *link, long state, int64_t timeout);
109 void schedule_timeout_and_set_state(long state, int64_t timeout);
110 void cfs_pause(cfs_duration_t d);
111 int need_resched(void);
112 void cond_resched(void);
113
114 /*
115  * Task states
116  */
117 #define TASK_INTERRUPTIBLE  (0)
118 #define TASK_UNINTERRUPTIBLE          (1)
119 #define TASK_RUNNING        (2)
120
121 static inline void schedule(void)                       {}
122 static inline void schedule_timeout(int64_t t)  {}
123 static inline void set_current_state(int state)
124 {
125 }
126
127 /*
128  * Lproc
129  */
130 typedef int (read_proc_t)(char *page, char **start, off_t off,
131                                 int count, int *eof, void *data);
132
133 struct file; /* forward ref */
134 typedef int (write_proc_t)(struct file *file, const char *buffer,
135                                unsigned long count, void *data);
136
137 /*
138  * Signal
139  */
140
141 /*
142  * Timer
143  */
144
145 struct timer_list {
146         struct list_head tl_list;
147         void (*function)(ulong_ptr_t unused);
148         ulong_ptr_t data;
149         long expires;
150 };
151
152
153 #define in_interrupt()    (0)
154
155 struct miscdevice{
156 };
157
158 static inline int misc_register(struct miscdevice *foo)
159 {
160         return 0;
161 }
162
163 static inline int misc_deregister(struct miscdevice *foo)
164 {
165         return 0;
166 }
167
168 #define cfs_recalc_sigpending(l)        do {} while (0)
169
170 #define DAEMON_FLAGS                0
171
172 #define L1_CACHE_ALIGN(x)               (x)
173
174 #ifdef HAVE_LIBPTHREAD
175 typedef int (*cfs_thread_t)(void *);
176 void *kthread_run(cfs_thread_t func, void *arg, const char namefmt[], ...);
177 #else
178 /* Fine, crash, but stop giving me compile warnings */
179 #define kthread_run(f, a, n, ...) LBUG()
180 #endif
181
182 uid_t current_uid(void);
183 gid_t current_gid(void);
184 uid_t current_fsuid(void);
185 gid_t current_fsgid(void);
186
187 #ifndef HAVE_STRLCPY /* not in glibc for RHEL 5.x, remove when obsolete */
188 size_t strlcpy(char *tgt, const char *src, size_t tgt_len);
189 #endif
190
191 #ifndef HAVE_STRLCAT /* not in glibc for RHEL 5.x, remove when obsolete */
192 size_t strlcat(char *tgt, const char *src, size_t tgt_len);
193 #endif
194
195 #define LIBCFS_REALLOC(ptr, size) realloc(ptr, size)
196
197 #define cfs_online_cpus() sysconf(_SC_NPROCESSORS_ONLN)
198
199 // static inline void local_irq_save(unsigned long flag) {return;}
200 // static inline void local_irq_restore(unsigned long flag) {return;}
201
202 enum {
203         CFS_STACK_TRACE_DEPTH = 16
204 };
205
206 struct cfs_stack_trace {
207         void *frame[CFS_STACK_TRACE_DEPTH];
208 };
209
210 /*
211  * arithmetic
212  */
213 #ifndef do_div /* gcc only, platform-specific will override */
214 #define do_div(a,b)                     \
215         ({                              \
216                 unsigned long remainder;\
217                 remainder = (a) % (b);  \
218                 (a) = (a) / (b);        \
219                 (remainder);            \
220         })
221 #endif
222
223 /*
224  * Groups
225  */
226 struct group_info{ };
227
228 #ifndef min
229 # define min(x,y) ((x)<(y) ? (x) : (y))
230 #endif
231
232 #ifndef max
233 # define max(x,y) ((x)>(y) ? (x) : (y))
234 #endif
235
236 #define get_random_bytes(val, size)     (*val) = 0
237
238 /* utility libcfs init/fini entries */
239 static inline int libcfs_arch_init(void) {
240         return 0;
241 }
242 static inline void libcfs_arch_cleanup(void) {
243 }
244
245 #endif /* __LIBCFS_USER_PRIM_H__ */