2008-02-28 00:52:31 +00:00
|
|
|
#ifndef _SPL_MUTEX_H
|
|
|
|
#define _SPL_MUTEX_H
|
2008-02-26 20:36:04 +00:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2008-02-27 19:09:51 +00:00
|
|
|
#include <linux/module.h>
|
2008-04-11 17:03:57 +00:00
|
|
|
#include <linux/hardirq.h>
|
2008-03-01 00:45:59 +00:00
|
|
|
#include <sys/types.h>
|
2008-02-27 19:09:51 +00:00
|
|
|
|
2008-02-26 20:36:04 +00:00
|
|
|
/* See the "Big Theory Statement" in solaris mutex.c.
|
|
|
|
*
|
|
|
|
* Spin mutexes apparently aren't needed by zfs so we assert
|
|
|
|
* if ibc is non-zero.
|
|
|
|
*
|
|
|
|
* Our impementation of adaptive mutexes aren't really adaptive.
|
|
|
|
* They go to sleep every time.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MUTEX_DEFAULT 0
|
|
|
|
#define MUTEX_HELD(x) (mutex_owned(x))
|
|
|
|
|
|
|
|
#define KM_MAGIC 0x42424242
|
|
|
|
#define KM_POISON 0x84
|
2008-02-27 19:09:51 +00:00
|
|
|
|
2008-02-26 20:36:04 +00:00
|
|
|
typedef struct {
|
|
|
|
int km_magic;
|
|
|
|
char *km_name;
|
|
|
|
struct task_struct *km_owner;
|
|
|
|
struct semaphore km_sem;
|
2008-04-15 20:53:36 +00:00
|
|
|
spinlock_t km_lock;
|
2008-02-26 20:36:04 +00:00
|
|
|
} kmutex_t;
|
|
|
|
|
|
|
|
#undef mutex_init
|
|
|
|
static __inline__ void
|
|
|
|
mutex_init(kmutex_t *mp, char *name, int type, void *ibc)
|
|
|
|
{
|
2008-04-25 22:10:47 +00:00
|
|
|
ENTRY;
|
2008-04-21 17:29:47 +00:00
|
|
|
ASSERT(mp);
|
|
|
|
ASSERT(ibc == NULL); /* XXX - Spin mutexes not needed */
|
|
|
|
ASSERT(type == MUTEX_DEFAULT); /* XXX - Only default type supported */
|
2008-02-26 20:36:04 +00:00
|
|
|
|
|
|
|
mp->km_magic = KM_MAGIC;
|
2008-04-15 20:53:36 +00:00
|
|
|
spin_lock_init(&mp->km_lock);
|
2008-02-26 20:36:04 +00:00
|
|
|
sema_init(&mp->km_sem, 1);
|
|
|
|
mp->km_owner = NULL;
|
|
|
|
mp->km_name = NULL;
|
|
|
|
|
|
|
|
if (name) {
|
|
|
|
mp->km_name = kmalloc(strlen(name) + 1, GFP_KERNEL);
|
|
|
|
if (mp->km_name)
|
|
|
|
strcpy(mp->km_name, name);
|
|
|
|
}
|
2008-04-25 22:10:47 +00:00
|
|
|
EXIT;
|
2008-02-26 20:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef mutex_destroy
|
|
|
|
static __inline__ void
|
|
|
|
mutex_destroy(kmutex_t *mp)
|
|
|
|
{
|
2008-04-25 22:10:47 +00:00
|
|
|
ENTRY;
|
2008-04-21 17:29:47 +00:00
|
|
|
ASSERT(mp);
|
|
|
|
ASSERT(mp->km_magic == KM_MAGIC);
|
2008-04-15 20:53:36 +00:00
|
|
|
spin_lock(&mp->km_lock);
|
2008-02-26 20:36:04 +00:00
|
|
|
|
|
|
|
if (mp->km_name)
|
|
|
|
kfree(mp->km_name);
|
|
|
|
|
|
|
|
memset(mp, KM_POISON, sizeof(*mp));
|
2008-04-15 20:53:36 +00:00
|
|
|
spin_unlock(&mp->km_lock);
|
2008-04-25 22:10:47 +00:00
|
|
|
EXIT;
|
2008-02-26 20:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ void
|
|
|
|
mutex_enter(kmutex_t *mp)
|
|
|
|
{
|
2008-04-25 22:10:47 +00:00
|
|
|
ENTRY;
|
2008-04-21 17:29:47 +00:00
|
|
|
ASSERT(mp);
|
|
|
|
ASSERT(mp->km_magic == KM_MAGIC);
|
2008-04-15 20:53:36 +00:00
|
|
|
spin_lock(&mp->km_lock);
|
2008-04-11 17:03:57 +00:00
|
|
|
|
|
|
|
if (unlikely(in_atomic() && !current->exit_state)) {
|
2008-04-15 20:53:36 +00:00
|
|
|
spin_unlock(&mp->km_lock);
|
2008-04-24 17:41:23 +00:00
|
|
|
__CDEBUG_LIMIT(S_MUTEX, D_ERROR,
|
|
|
|
"May schedule while atomic: %s/0x%08x/%d\n",
|
|
|
|
current->comm, preempt_count(), current->pid);
|
|
|
|
SBUG();
|
2008-04-11 17:03:57 +00:00
|
|
|
}
|
|
|
|
|
2008-04-15 20:53:36 +00:00
|
|
|
spin_unlock(&mp->km_lock);
|
|
|
|
|
|
|
|
down(&mp->km_sem);
|
|
|
|
|
|
|
|
spin_lock(&mp->km_lock);
|
2008-04-21 17:29:47 +00:00
|
|
|
ASSERT(mp->km_owner == NULL);
|
2008-02-26 20:36:04 +00:00
|
|
|
mp->km_owner = current;
|
2008-04-15 20:53:36 +00:00
|
|
|
spin_unlock(&mp->km_lock);
|
2008-04-25 22:10:47 +00:00
|
|
|
EXIT;
|
2008-02-26 20:36:04 +00:00
|
|
|
}
|
|
|
|
|
2008-04-15 20:53:36 +00:00
|
|
|
/* Return 1 if we acquired the mutex, else zero. */
|
2008-02-26 20:36:04 +00:00
|
|
|
static __inline__ int
|
|
|
|
mutex_tryenter(kmutex_t *mp)
|
|
|
|
{
|
2008-04-15 20:53:36 +00:00
|
|
|
int rc;
|
2008-04-25 22:10:47 +00:00
|
|
|
ENTRY;
|
2008-02-26 20:36:04 +00:00
|
|
|
|
2008-04-21 17:29:47 +00:00
|
|
|
ASSERT(mp);
|
|
|
|
ASSERT(mp->km_magic == KM_MAGIC);
|
2008-04-15 20:53:36 +00:00
|
|
|
spin_lock(&mp->km_lock);
|
2008-04-11 17:03:57 +00:00
|
|
|
|
|
|
|
if (unlikely(in_atomic() && !current->exit_state)) {
|
2008-04-15 20:53:36 +00:00
|
|
|
spin_unlock(&mp->km_lock);
|
2008-04-24 17:41:23 +00:00
|
|
|
__CDEBUG_LIMIT(S_MUTEX, D_ERROR,
|
|
|
|
"May schedule while atomic: %s/0x%08x/%d\n",
|
|
|
|
current->comm, preempt_count(), current->pid);
|
|
|
|
SBUG();
|
2008-04-11 17:03:57 +00:00
|
|
|
}
|
|
|
|
|
2008-04-15 20:53:36 +00:00
|
|
|
spin_unlock(&mp->km_lock);
|
|
|
|
rc = down_trylock(&mp->km_sem); /* returns 0 if acquired */
|
|
|
|
if (rc == 0) {
|
|
|
|
spin_lock(&mp->km_lock);
|
2008-04-21 17:29:47 +00:00
|
|
|
ASSERT(mp->km_owner == NULL);
|
2008-02-26 20:36:04 +00:00
|
|
|
mp->km_owner = current;
|
2008-04-15 20:53:36 +00:00
|
|
|
spin_unlock(&mp->km_lock);
|
2008-04-25 22:10:47 +00:00
|
|
|
RETURN(1);
|
2008-02-26 20:36:04 +00:00
|
|
|
}
|
2008-04-25 22:10:47 +00:00
|
|
|
|
|
|
|
RETURN(0);
|
2008-02-26 20:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ void
|
|
|
|
mutex_exit(kmutex_t *mp)
|
|
|
|
{
|
2008-04-25 22:10:47 +00:00
|
|
|
ENTRY;
|
2008-04-21 17:29:47 +00:00
|
|
|
ASSERT(mp);
|
|
|
|
ASSERT(mp->km_magic == KM_MAGIC);
|
2008-04-15 20:53:36 +00:00
|
|
|
spin_lock(&mp->km_lock);
|
2008-04-21 17:29:47 +00:00
|
|
|
|
|
|
|
ASSERT(mp->km_owner == current);
|
2008-02-26 20:36:04 +00:00
|
|
|
mp->km_owner = NULL;
|
2008-04-15 20:53:36 +00:00
|
|
|
spin_unlock(&mp->km_lock);
|
2008-02-26 20:36:04 +00:00
|
|
|
up(&mp->km_sem);
|
2008-04-25 22:10:47 +00:00
|
|
|
EXIT;
|
2008-02-26 20:36:04 +00:00
|
|
|
}
|
|
|
|
|
2008-04-15 20:53:36 +00:00
|
|
|
/* Return 1 if mutex is held by current process, else zero. */
|
2008-02-26 20:36:04 +00:00
|
|
|
static __inline__ int
|
|
|
|
mutex_owned(kmutex_t *mp)
|
|
|
|
{
|
2008-04-15 20:53:36 +00:00
|
|
|
int rc;
|
2008-04-25 22:10:47 +00:00
|
|
|
ENTRY;
|
2008-04-15 20:53:36 +00:00
|
|
|
|
2008-04-21 17:29:47 +00:00
|
|
|
ASSERT(mp);
|
|
|
|
ASSERT(mp->km_magic == KM_MAGIC);
|
2008-04-15 20:53:36 +00:00
|
|
|
spin_lock(&mp->km_lock);
|
|
|
|
rc = (mp->km_owner == current);
|
|
|
|
spin_unlock(&mp->km_lock);
|
|
|
|
|
2008-04-25 22:10:47 +00:00
|
|
|
RETURN(rc);
|
2008-02-26 20:36:04 +00:00
|
|
|
}
|
|
|
|
|
2008-04-15 20:53:36 +00:00
|
|
|
/* Return owner if mutex is owned, else NULL. */
|
2008-02-26 20:36:04 +00:00
|
|
|
static __inline__ kthread_t *
|
|
|
|
mutex_owner(kmutex_t *mp)
|
|
|
|
{
|
2008-04-15 20:53:36 +00:00
|
|
|
kthread_t *thr;
|
2008-04-25 22:10:47 +00:00
|
|
|
ENTRY;
|
2008-04-15 20:53:36 +00:00
|
|
|
|
2008-04-21 17:29:47 +00:00
|
|
|
ASSERT(mp);
|
|
|
|
ASSERT(mp->km_magic == KM_MAGIC);
|
2008-04-15 20:53:36 +00:00
|
|
|
spin_lock(&mp->km_lock);
|
|
|
|
thr = mp->km_owner;
|
|
|
|
spin_unlock(&mp->km_lock);
|
|
|
|
|
2008-04-25 22:10:47 +00:00
|
|
|
RETURN(thr);
|
2008-02-26 20:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-02-28 00:52:31 +00:00
|
|
|
#endif /* _SPL_MUTEX_H */
|