ast.c 3.5 KB
Newer Older
1 2 3 4
/******************************************************************************
*******************************************************************************
**
**  Copyright (C) Sistina Software, Inc.  1997-2003  All rights reserved.
5
**  Copyright (C) 2004-2008 Red Hat, Inc.  All rights reserved.
6 7 8 9 10 11 12 13 14 15
**
**  This copyrighted material is made available to anyone wishing to use,
**  modify, copy, or redistribute it subject to the terms and conditions
**  of the GNU General Public License v.2.
**
*******************************************************************************
******************************************************************************/

#include "dlm_internal.h"
#include "lock.h"
D
David Teigland 已提交
16
#include "user.h"
17
#include "ast.h"
18 19 20 21 22 23 24

#define WAKE_ASTS  0

static struct list_head		ast_queue;
static spinlock_t		ast_queue_lock;
static struct task_struct *	astd_task;
static unsigned long		astd_wakeflags;
25
static struct mutex		astd_running;
26 27 28 29 30 31 32 33 34 35


void dlm_del_ast(struct dlm_lkb *lkb)
{
	spin_lock(&ast_queue_lock);
	if (lkb->lkb_ast_type & (AST_COMP | AST_BAST))
		list_del(&lkb->lkb_astqueue);
	spin_unlock(&ast_queue_lock);
}

36
void dlm_add_ast(struct dlm_lkb *lkb, int type, int bastmode)
37
{
D
David Teigland 已提交
38
	if (lkb->lkb_flags & DLM_IFL_USER) {
39
		dlm_user_add_ast(lkb, type, bastmode);
D
David Teigland 已提交
40 41 42
		return;
	}

43 44 45 46 47 48
	spin_lock(&ast_queue_lock);
	if (!(lkb->lkb_ast_type & (AST_COMP | AST_BAST))) {
		kref_get(&lkb->lkb_ref);
		list_add_tail(&lkb->lkb_astqueue, &ast_queue);
	}
	lkb->lkb_ast_type |= type;
49 50
	if (bastmode)
		lkb->lkb_bastmode = bastmode;
51 52 53 54 55 56 57 58 59 60 61
	spin_unlock(&ast_queue_lock);

	set_bit(WAKE_ASTS, &astd_wakeflags);
	wake_up_process(astd_task);
}

static void process_asts(void)
{
	struct dlm_ls *ls = NULL;
	struct dlm_rsb *r = NULL;
	struct dlm_lkb *lkb;
62 63
	void (*cast) (void *astparam);
	void (*bast) (void *astparam, int mode);
A
Andrew Morton 已提交
64 65 66 67 68 69 70
	int type = 0, bastmode;

repeat:
	spin_lock(&ast_queue_lock);
	list_for_each_entry(lkb, &ast_queue, lkb_astqueue) {
		r = lkb->lkb_resource;
		ls = r->res_ls;
71

A
Andrew Morton 已提交
72 73
		if (dlm_locking_stopped(ls))
			continue;
74

A
Andrew Morton 已提交
75 76 77 78 79 80
		list_del(&lkb->lkb_astqueue);
		type = lkb->lkb_ast_type;
		lkb->lkb_ast_type = 0;
		bastmode = lkb->lkb_bastmode;

		spin_unlock(&ast_queue_lock);
81 82
		cast = lkb->lkb_astfn;
		bast = lkb->lkb_bastfn;
83 84 85 86 87

		if ((type & AST_COMP) && cast)
			cast(lkb->lkb_astparam);

		if ((type & AST_BAST) && bast)
88
			bast(lkb->lkb_astparam, bastmode);
89 90 91 92 93

		/* this removes the reference added by dlm_add_ast
		   and may result in the lkb being freed */
		dlm_put_lkb(lkb);

94
		cond_resched();
A
Andrew Morton 已提交
95
		goto repeat;
96
	}
A
Andrew Morton 已提交
97
	spin_unlock(&ast_queue_lock);
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
}

static inline int no_asts(void)
{
	int ret;

	spin_lock(&ast_queue_lock);
	ret = list_empty(&ast_queue);
	spin_unlock(&ast_queue_lock);
	return ret;
}

static int dlm_astd(void *data)
{
	while (!kthread_should_stop()) {
		set_current_state(TASK_INTERRUPTIBLE);
		if (!test_bit(WAKE_ASTS, &astd_wakeflags))
			schedule();
		set_current_state(TASK_RUNNING);

118
		mutex_lock(&astd_running);
119 120
		if (test_and_clear_bit(WAKE_ASTS, &astd_wakeflags))
			process_asts();
121
		mutex_unlock(&astd_running);
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
	}
	return 0;
}

void dlm_astd_wake(void)
{
	if (!no_asts()) {
		set_bit(WAKE_ASTS, &astd_wakeflags);
		wake_up_process(astd_task);
	}
}

int dlm_astd_start(void)
{
	struct task_struct *p;
	int error = 0;

	INIT_LIST_HEAD(&ast_queue);
	spin_lock_init(&ast_queue_lock);
141
	mutex_init(&astd_running);
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

	p = kthread_run(dlm_astd, NULL, "dlm_astd");
	if (IS_ERR(p))
		error = PTR_ERR(p);
	else
		astd_task = p;
	return error;
}

void dlm_astd_stop(void)
{
	kthread_stop(astd_task);
}

void dlm_astd_suspend(void)
{
158
	mutex_lock(&astd_running);
159 160 161 162
}

void dlm_astd_resume(void)
{
163
	mutex_unlock(&astd_running);
164 165
}