1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002 Cluster File Systems, Inc.
5 * Author: Phil Schwan <phil@clusterfs.com>
7 * This file is part of Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Lustre; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * Darwin porting library
23 * Make things easy to port
25 #define DEBUG_SUBSYSTEM S_LNET
27 #include <mach/mach_types.h>
32 #include <sys/filedesc.h>
33 #include <sys/namei.h>
34 #include <miscfs/devfs/devfs.h>
35 #include <kern/thread.h>
37 #include <libcfs/libcfs.h>
40 * cfs pseudo device, actually pseudo char device in darwin
42 #define KLNET_MAJOR -1
44 kern_return_t cfs_psdev_register(cfs_psdev_t *dev) {
45 dev->index = cdevsw_add(KLNET_MAJOR, dev->devsw);
47 printf("libcfs_init: failed to allocate a major number!\n");
50 dev->handle = devfs_make_node(makedev (dev->index, 0),
52 GID_WHEEL, 0666, (char *)dev->name, 0);
56 kern_return_t cfs_psdev_deregister(cfs_psdev_t *dev) {
57 devfs_remove(dev->handle);
58 cdevsw_remove(dev->index, dev->devsw);
63 * KPortal symbol register / unregister support
65 struct rw_semaphore cfs_symbol_lock;
66 struct list_head cfs_symbol_list;
69 cfs_symbol_get(const char *name)
71 struct list_head *walker;
72 struct cfs_symbol *sym = NULL;
74 down_read(&cfs_symbol_lock);
75 list_for_each(walker, &cfs_symbol_list) {
76 sym = list_entry (walker, struct cfs_symbol, sym_list);
77 if (!strcmp(sym->name, name)) {
82 up_read(&cfs_symbol_lock);
89 cfs_symbol_put(const char *name)
91 struct list_head *walker;
92 struct cfs_symbol *sym = NULL;
94 down_read(&cfs_symbol_lock);
95 list_for_each(walker, &cfs_symbol_list) {
96 sym = list_entry (walker, struct cfs_symbol, sym_list);
97 if (!strcmp(sym->name, name)) {
99 LASSERT(sym->ref >= 0);
103 up_read(&cfs_symbol_lock);
104 LASSERT(sym != NULL);
110 cfs_symbol_register(const char *name, const void *value)
112 struct list_head *walker;
113 struct cfs_symbol *sym = NULL;
114 struct cfs_symbol *new = NULL;
116 MALLOC(new, struct cfs_symbol *, sizeof(struct cfs_symbol), M_TEMP, M_WAITOK|M_ZERO);
117 strncpy(new->name, name, CFS_SYMBOL_LEN);
118 new->value = (void *)value;
120 CFS_INIT_LIST_HEAD(&new->sym_list);
122 down_write(&cfs_symbol_lock);
123 list_for_each(walker, &cfs_symbol_list) {
124 sym = list_entry (walker, struct cfs_symbol, sym_list);
125 if (!strcmp(sym->name, name)) {
126 up_write(&cfs_symbol_lock);
128 return KERN_NAME_EXISTS;
132 list_add_tail(&new->sym_list, &cfs_symbol_list);
133 up_write(&cfs_symbol_lock);
139 cfs_symbol_unregister(const char *name)
141 struct list_head *walker;
142 struct list_head *nxt;
143 struct cfs_symbol *sym = NULL;
145 down_write(&cfs_symbol_lock);
146 list_for_each_safe(walker, nxt, &cfs_symbol_list) {
147 sym = list_entry (walker, struct cfs_symbol, sym_list);
148 if (!strcmp(sym->name, name)) {
149 LASSERT(sym->ref == 0);
150 list_del (&sym->sym_list);
155 up_write(&cfs_symbol_lock);
163 CFS_INIT_LIST_HEAD(&cfs_symbol_list);
164 init_rwsem(&cfs_symbol_lock);
170 struct list_head *walker;
171 struct cfs_symbol *sym = NULL;
173 down_write(&cfs_symbol_lock);
174 list_for_each(walker, &cfs_symbol_list) {
175 sym = list_entry (walker, struct cfs_symbol, sym_list);
176 LASSERT(sym->ref == 0);
177 list_del (&sym->sym_list);
180 up_write(&cfs_symbol_lock);
182 fini_rwsem(&cfs_symbol_lock);
186 struct kernel_thread_arg
194 struct kernel_thread_arg cfs_thread_arg;
196 #define THREAD_ARG_FREE 0
197 #define THREAD_ARG_HOLD 1
198 #define THREAD_ARG_RECV 2
200 #define set_targ_stat(a, v) atomic_set(&(a)->inuse, v)
201 #define get_targ_stat(a) atomic_read(&(a)->inuse)
204 * Hold the thread argument and set the status of thread_status
205 * to THREAD_ARG_HOLD, if the thread argument is held by other
206 * threads (It's THREAD_ARG_HOLD already), current-thread has to wait.
208 #define thread_arg_hold(pta, _func, _arg) \
210 spin_lock(&(pta)->lock); \
211 if (get_targ_stat(pta) == THREAD_ARG_FREE) { \
212 set_targ_stat((pta), THREAD_ARG_HOLD); \
213 (pta)->arg = (void *)_arg; \
214 (pta)->func = _func; \
215 spin_unlock(&(pta)->lock); \
218 spin_unlock(&(pta)->lock); \
223 * Release the thread argument if the thread argument has been
224 * received by the child-thread (Status of thread_args is
225 * THREAD_ARG_RECV), otherwise current-thread has to wait.
226 * After release, the thread_args' status will be set to
227 * THREAD_ARG_FREE, and others can re-use the thread_args to
228 * create new kernel_thread.
230 #define thread_arg_release(pta) \
232 spin_lock(&(pta)->lock); \
233 if (get_targ_stat(pta) == THREAD_ARG_RECV) { \
235 (pta)->func = NULL; \
236 set_targ_stat(pta, THREAD_ARG_FREE); \
237 spin_unlock(&(pta)->lock); \
240 spin_unlock(&(pta)->lock); \
245 * Receive thread argument (Used in child thread), set the status
246 * of thread_args to THREAD_ARG_RECV.
248 #define __thread_arg_recv_fin(pta, _func, _arg, fin) \
250 spin_lock(&(pta)->lock); \
251 if (get_targ_stat(pta) == THREAD_ARG_HOLD) { \
253 set_targ_stat(pta, THREAD_ARG_RECV);\
255 _func = (pta)->func; \
256 spin_unlock(&(pta)->lock); \
259 spin_unlock(&(pta)->lock); \
264 * Just set the thread_args' status to THREAD_ARG_RECV
266 #define thread_arg_fin(pta) \
268 spin_lock(&(pta)->lock); \
269 assert( get_targ_stat(pta) == THREAD_ARG_HOLD); \
270 set_targ_stat(pta, THREAD_ARG_RECV); \
271 spin_unlock(&(pta)->lock); \
274 #define thread_arg_recv(pta, f, a) __thread_arg_recv_fin(pta, f, a, 1)
275 #define thread_arg_keep(pta, f, a) __thread_arg_recv_fin(pta, f, a, 0)
278 cfs_thread_agent_init(void)
280 set_targ_stat(&cfs_thread_arg, THREAD_ARG_FREE);
281 spin_lock_init(&cfs_thread_arg.lock);
282 cfs_thread_arg.arg = NULL;
283 cfs_thread_arg.func = NULL;
287 cfs_thread_agent_fini(void)
289 assert(get_targ_stat(&cfs_thread_arg) == THREAD_ARG_FREE);
291 spin_lock_done(&cfs_thread_arg.lock);
296 * All requests to create kernel thread will create a new
297 * thread instance of cfs_thread_agent, one by one.
298 * cfs_thread_agent will call the caller's thread function
299 * with argument supplied by caller.
302 cfs_thread_agent (void)
304 cfs_thread_t func = NULL;
307 thread_arg_recv(&cfs_thread_arg, func, arg);
308 /* printf("entry of thread agent (func: %08lx).\n", (void *)func); */
309 assert(func != NULL);
311 /* printf("thread agent exit. (func: %08lx)\n", (void *)func); */
312 (void) thread_terminate(current_thread());
315 extern thread_t kernel_thread(task_t task, void (*start)(void));
318 cfs_kernel_thread(cfs_thread_t func, void *arg, int flag)
323 thread_arg_hold(&cfs_thread_arg, func, arg);
324 th = kernel_thread(kernel_task, cfs_thread_agent);
325 thread_arg_release(&cfs_thread_arg);
326 if (th == THREAD_NULL)
331 void cfs_daemonize(char *str)
333 snprintf(cfs_curproc_comm(), CFS_CURPROC_COMM_MAX, "%s", str);
338 * XXX Liang: kexts cannot access sigmask in Darwin8.
339 * it's almost impossible for us to get/set signal mask
340 * without patching kernel.
341 * Should we provide these functions in xnu?
343 * These signal functions almost do nothing now, we
344 * need to investigate more about signal in Darwin.
346 cfs_sigset_t cfs_get_blockedsigs()
348 return (cfs_sigset_t)0;
351 extern int block_procsigmask(struct proc *p, int bit);
353 cfs_sigset_t cfs_block_allsigs()
355 cfs_sigset_t old = 0;
358 block_procsigmask(current_proc(), -1);
363 cfs_sigset_t cfs_block_sigs(sigset_t bit)
365 cfs_sigset_t old = 0;
368 block_procsigmask(current_proc(), bit);
373 void cfs_restore_sigs(cfs_sigset_t old)
377 int cfs_signal_pending(void)
381 extern int thread_issignal(proc_t, thread_t, sigset_t);
382 return thread_issignal(current_proc(), current_thread(), (sigset_t)-1);
384 return SHOULDissignal(current_proc(), current_uthread())
388 void cfs_clear_sigpending(void)
392 clear_procsiglist(current_proc(), -1);
398 #else /* !__DARWIN8__ */
400 void lustre_cone_in(boolean_t *state, funnel_t **cone)
402 *cone = thread_funnel_get();
403 if (*cone == network_flock)
404 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
405 else if (*cone == NULL)
406 *state = thread_funnel_set(kernel_flock, TRUE);
409 void lustre_cone_ex(boolean_t state, funnel_t *cone)
411 if (cone == network_flock)
412 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
413 else if (cone == NULL)
414 (void) thread_funnel_set(kernel_flock, state);
417 void lustre_net_in(boolean_t *state, funnel_t **cone)
419 *cone = thread_funnel_get();
420 if (*cone == kernel_flock)
421 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
422 else if (*cone == NULL)
423 *state = thread_funnel_set(network_flock, TRUE);
426 void lustre_net_ex(boolean_t state, funnel_t *cone)
428 if (cone == kernel_flock)
429 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
430 else if (cone == NULL)
431 (void) thread_funnel_set(network_flock, state);
433 #endif /* !__DARWIN8__ */
435 void cfs_waitq_init(struct cfs_waitq *waitq)
437 ksleep_chan_init(&waitq->wq_ksleep_chan);
440 void cfs_waitlink_init(struct cfs_waitlink *link)
442 ksleep_link_init(&link->wl_ksleep_link);
445 void cfs_waitq_add(struct cfs_waitq *waitq, struct cfs_waitlink *link)
447 link->wl_waitq = waitq;
448 ksleep_add(&waitq->wq_ksleep_chan, &link->wl_ksleep_link);
451 void cfs_waitq_add_exclusive(struct cfs_waitq *waitq,
452 struct cfs_waitlink *link)
454 link->wl_waitq = waitq;
455 link->wl_ksleep_link.flags |= KSLEEP_EXCLUSIVE;
456 ksleep_add(&waitq->wq_ksleep_chan, &link->wl_ksleep_link);
459 void cfs_waitq_forward(struct cfs_waitlink *link,
460 struct cfs_waitq *waitq)
462 link->wl_ksleep_link.forward = &waitq->wq_ksleep_chan;
465 void cfs_waitq_del(struct cfs_waitq *waitq,
466 struct cfs_waitlink *link)
468 ksleep_del(&waitq->wq_ksleep_chan, &link->wl_ksleep_link);
471 int cfs_waitq_active(struct cfs_waitq *waitq)
476 void cfs_waitq_signal(struct cfs_waitq *waitq)
479 * XXX nikita: do NOT call libcfs_debug_msg() (CDEBUG/ENTRY/EXIT)
480 * from here: this will lead to infinite recursion.
482 ksleep_wake(&waitq->wq_ksleep_chan);
485 void cfs_waitq_signal_nr(struct cfs_waitq *waitq, int nr)
487 ksleep_wake_nr(&waitq->wq_ksleep_chan, nr);
490 void cfs_waitq_broadcast(struct cfs_waitq *waitq)
492 ksleep_wake_all(&waitq->wq_ksleep_chan);
495 void cfs_waitq_wait(struct cfs_waitlink *link, cfs_task_state_t state)
497 ksleep_wait(&link->wl_waitq->wq_ksleep_chan, state);
500 cfs_duration_t cfs_waitq_timedwait(struct cfs_waitlink *link,
501 cfs_task_state_t state,
502 cfs_duration_t timeout)
504 return ksleep_timedwait(&link->wl_waitq->wq_ksleep_chan,
508 typedef void (*ktimer_func_t)(void *);
509 void cfs_timer_init(cfs_timer_t *t, void (* func)(unsigned long), void *arg)
511 ktimer_init(&t->t, (ktimer_func_t)func, arg);
514 void cfs_timer_done(struct cfs_timer *t)
519 void cfs_timer_arm(struct cfs_timer *t, cfs_time_t deadline)
521 ktimer_arm(&t->t, deadline);
524 void cfs_timer_disarm(struct cfs_timer *t)
526 ktimer_disarm(&t->t);
529 int cfs_timer_is_armed(struct cfs_timer *t)
531 return ktimer_is_armed(&t->t);
534 cfs_time_t cfs_timer_deadline(struct cfs_timer *t)
536 return ktimer_deadline(&t->t);
539 void cfs_enter_debugger(void)
542 extern void Debugger(const char * reason);
545 extern void PE_enter_debugger(char *cause);
546 PE_enter_debugger("CFS");
550 int cfs_online_cpus(void)
557 sysctlbyname("hw.activecpu", &activecpu, &size, NULL, 0);
560 host_basic_info_data_t hinfo;
562 int count = HOST_BASIC_INFO_COUNT;
564 kret = host_info(BSD_HOST, HOST_BASIC_INFO, &hinfo, &count);
565 if (kret == KERN_SUCCESS)
566 return (hinfo.avail_cpus);
578 sysctlbyname("hw.ncpu", &ncpu, &size, NULL, 0);