4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * libcfs/libcfs/user-prim.c
38 * Implementations of portable APIs for liblustre
40 * Author: Nikita Danilov <nikita@clusterfs.com>
44 * liblustre is single-threaded, so most "synchronization" APIs are trivial.
50 #include <libcfs/libcfs.h>
53 * Wait queue. No-op implementation.
56 void init_waitqueue_head(struct cfs_waitq *waitq)
58 LASSERT(waitq != NULL);
62 void init_waitqueue_entry_current(struct cfs_waitlink *link)
64 LASSERT(link != NULL);
68 void add_wait_queue(struct cfs_waitq *waitq, struct cfs_waitlink *link)
70 LASSERT(waitq != NULL);
71 LASSERT(link != NULL);
76 void add_wait_queue_exclusive(struct cfs_waitq *waitq, struct cfs_waitlink *link)
78 LASSERT(waitq != NULL);
79 LASSERT(link != NULL);
84 void add_wait_queue_exclusive_head(struct cfs_waitq *waitq, struct cfs_waitlink *link)
86 add_wait_queue_exclusive(waitq, link);
89 void remove_wait_queue(struct cfs_waitq *waitq, struct cfs_waitlink *link)
91 LASSERT(waitq != NULL);
92 LASSERT(link != NULL);
97 int waitqueue_active(struct cfs_waitq *waitq)
99 LASSERT(waitq != NULL);
104 void wake_up(struct cfs_waitq *waitq)
106 LASSERT(waitq != NULL);
110 void wake_up_nr(struct cfs_waitq *waitq, int nr)
112 LASSERT(waitq != NULL);
116 void wake_up_all(struct cfs_waitq *waitq)
118 LASSERT(waitq != NULL);
122 void waitq_wait(struct cfs_waitlink *link, long state)
124 LASSERT(link != NULL);
127 /* well, wait for something to happen */
128 call_wait_handler(0);
131 int64_t waitq_timedwait(struct cfs_waitlink *link, long state,
134 LASSERT(link != NULL);
136 call_wait_handler(timeout);
140 void schedule_timeout_and_set_state(long state, int64_t timeout)
143 /* sleep(timeout) here instead? */
144 waitq_timedwait(&l, state, timeout);
148 cfs_pause(cfs_duration_t d)
152 cfs_duration_nsec(d, &s);
156 int need_resched(void)
161 void cond_resched(void)
169 void cfs_init_timer(struct timer_list *t)
171 INIT_LIST_HEAD(&t->tl_list);
174 void cfs_timer_init(struct timer_list *l, cfs_timer_func_t *func, void *arg)
176 INIT_LIST_HEAD(&l->tl_list);
178 l->data = (ulong_ptr_t)arg;
182 int cfs_timer_is_armed(struct timer_list *l)
184 if (cfs_time_before(cfs_time_current(), l->expires))
190 void cfs_timer_arm(struct timer_list *l, cfs_time_t deadline)
192 l->expires = deadline;
195 void cfs_timer_disarm(struct timer_list *l)
198 cfs_time_t cfs_timer_deadline(struct timer_list *l)
204 #ifdef HAVE_LIBPTHREAD
210 struct lustre_thread_arg {
214 static void *cfs_thread_helper(void *data)
216 struct lustre_thread_arg *targ = data;
217 cfs_thread_t f = targ->f;
218 void *arg = targ->arg;
226 void *kthread_run(cfs_thread_t func, void *arg, const char namefmt[], ...)
229 pthread_attr_t tattr;
231 struct lustre_thread_arg *targ_p =
232 malloc(sizeof(struct lustre_thread_arg));
235 return ERR_PTR(-ENOMEM);
240 pthread_attr_init(&tattr);
241 pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
242 rc = pthread_create(&tid, &tattr, cfs_thread_helper, targ_p);
243 pthread_attr_destroy(&tattr);
248 uid_t current_uid(void)
253 gid_t current_gid(void)
258 uid_t current_fsuid(void)
263 gid_t current_fsgid(void)
268 #ifndef HAVE_STRLCPY /* not in glibc for RHEL 5.x, remove when obsolete */
269 size_t strlcpy(char *tgt, const char *src, size_t tgt_len)
271 int src_len = strlen(src);
273 strncpy(tgt, src, tgt_len - 1);
274 tgt[tgt_len - 1] = '\0';
280 #ifndef HAVE_STRLCAT /* not in glibc for RHEL 5.x, remove when obsolete */
281 size_t strlcat(char *tgt, const char *src, size_t size)
283 size_t tgt_len = strlen(tgt);
285 if (size > tgt_len) {
286 strncat(tgt, src, size - tgt_len - 1);
287 tgt[size - 1] = '\0';
290 return tgt_len + strlen(src);
294 /* Read the environment variable of current process specified by @key. */
295 int cfs_get_environ(const char *key, char *value, int *val_len)
304 len = strlcpy(value, entry, *val_len);
311 void cfs_enter_debugger(void)
318 int unshare_fs_struct()
323 sigset_t cfs_block_allsigs(void)
330 rc = sigprocmask(SIG_BLOCK, &all, &old);
336 sigset_t cfs_block_sigs(unsigned long sigs)
339 sigset_t blocks = { { sigs } }; /* kludge */
342 rc = sigprocmask(SIG_BLOCK, &blocks, &old);
348 /* Block all signals except for the @sigs. It's only used in
349 * Linux kernel, just a dummy here. */
350 sigset_t cfs_block_sigsinv(unsigned long sigs)
355 /* Return old blocked sigs */
356 rc = sigprocmask(SIG_SETMASK, NULL, &old);
362 void cfs_restore_sigs(sigset_t old)
364 int rc = sigprocmask(SIG_SETMASK, &old, NULL);
369 int cfs_signal_pending(void)
375 rc = sigpending(&set);
380 return !memcmp(&empty, &set, sizeof(set));
383 void cfs_clear_sigpending(void)
391 * In glibc (NOT in Linux, so check above is not right), implement
392 * stack-back-tracing through backtrace() function.
394 #include <execinfo.h>
396 void cfs_stack_trace_fill(struct cfs_stack_trace *trace)
398 backtrace(trace->frame, ARRAY_SIZE(trace->frame));
401 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no)
403 if (0 <= frame_no && frame_no < ARRAY_SIZE(trace->frame))
404 return trace->frame[frame_no];
411 void cfs_stack_trace_fill(struct cfs_stack_trace *trace)
413 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no)
421 void lbug_with_loc(struct libcfs_debug_msg_data *msgdata)
423 /* No libcfs_catastrophe in userspace! */
424 libcfs_debug_msg(msgdata, "LBUG\n");
433 * c-indentation-style: "K&R"