David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | ******************************************************************************* |
| 3 | ** |
| 4 | ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. |
David Teigland | fd22a51 | 2008-12-09 11:55:46 -0600 | [diff] [blame^] | 5 | ** Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved. |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 6 | ** |
| 7 | ** This copyrighted material is made available to anyone wishing to use, |
| 8 | ** modify, copy, or redistribute it subject to the terms and conditions |
| 9 | ** of the GNU General Public License v.2. |
| 10 | ** |
| 11 | ******************************************************************************* |
| 12 | ******************************************************************************/ |
| 13 | |
| 14 | #include "dlm_internal.h" |
| 15 | #include "lock.h" |
David Teigland | 597d0ca | 2006-07-12 16:44:04 -0500 | [diff] [blame] | 16 | #include "user.h" |
Adrian Bunk | 8fa1de3 | 2007-04-04 17:25:29 +0200 | [diff] [blame] | 17 | #include "ast.h" |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 18 | |
| 19 | #define WAKE_ASTS 0 |
| 20 | |
| 21 | static struct list_head ast_queue; |
| 22 | static spinlock_t ast_queue_lock; |
| 23 | static struct task_struct * astd_task; |
| 24 | static unsigned long astd_wakeflags; |
David Teigland | 9013592 | 2006-01-20 08:47:07 +0000 | [diff] [blame] | 25 | static struct mutex astd_running; |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 26 | |
| 27 | |
| 28 | void dlm_del_ast(struct dlm_lkb *lkb) |
| 29 | { |
| 30 | spin_lock(&ast_queue_lock); |
| 31 | if (lkb->lkb_ast_type & (AST_COMP | AST_BAST)) |
| 32 | list_del(&lkb->lkb_astqueue); |
| 33 | spin_unlock(&ast_queue_lock); |
| 34 | } |
| 35 | |
David Teigland | fd22a51 | 2008-12-09 11:55:46 -0600 | [diff] [blame^] | 36 | void dlm_add_ast(struct dlm_lkb *lkb, int type, int bastmode) |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 37 | { |
David Teigland | 597d0ca | 2006-07-12 16:44:04 -0500 | [diff] [blame] | 38 | if (lkb->lkb_flags & DLM_IFL_USER) { |
David Teigland | fd22a51 | 2008-12-09 11:55:46 -0600 | [diff] [blame^] | 39 | dlm_user_add_ast(lkb, type, bastmode); |
David Teigland | 597d0ca | 2006-07-12 16:44:04 -0500 | [diff] [blame] | 40 | return; |
| 41 | } |
| 42 | |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 43 | spin_lock(&ast_queue_lock); |
| 44 | if (!(lkb->lkb_ast_type & (AST_COMP | AST_BAST))) { |
| 45 | kref_get(&lkb->lkb_ref); |
| 46 | list_add_tail(&lkb->lkb_astqueue, &ast_queue); |
| 47 | } |
| 48 | lkb->lkb_ast_type |= type; |
David Teigland | fd22a51 | 2008-12-09 11:55:46 -0600 | [diff] [blame^] | 49 | if (bastmode) |
| 50 | lkb->lkb_bastmode = bastmode; |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 51 | spin_unlock(&ast_queue_lock); |
| 52 | |
| 53 | set_bit(WAKE_ASTS, &astd_wakeflags); |
| 54 | wake_up_process(astd_task); |
| 55 | } |
| 56 | |
| 57 | static void process_asts(void) |
| 58 | { |
| 59 | struct dlm_ls *ls = NULL; |
| 60 | struct dlm_rsb *r = NULL; |
| 61 | struct dlm_lkb *lkb; |
David Teigland | e5dae54 | 2008-02-06 00:35:45 -0600 | [diff] [blame] | 62 | void (*cast) (void *astparam); |
| 63 | void (*bast) (void *astparam, int mode); |
David Teigland | fd22a51 | 2008-12-09 11:55:46 -0600 | [diff] [blame^] | 64 | int type = 0, found, bastmode; |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 65 | |
| 66 | for (;;) { |
David Teigland | 9013592 | 2006-01-20 08:47:07 +0000 | [diff] [blame] | 67 | found = 0; |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 68 | spin_lock(&ast_queue_lock); |
| 69 | list_for_each_entry(lkb, &ast_queue, lkb_astqueue) { |
| 70 | r = lkb->lkb_resource; |
| 71 | ls = r->res_ls; |
| 72 | |
| 73 | if (dlm_locking_stopped(ls)) |
| 74 | continue; |
| 75 | |
| 76 | list_del(&lkb->lkb_astqueue); |
| 77 | type = lkb->lkb_ast_type; |
| 78 | lkb->lkb_ast_type = 0; |
David Teigland | fd22a51 | 2008-12-09 11:55:46 -0600 | [diff] [blame^] | 79 | bastmode = lkb->lkb_bastmode; |
David Teigland | 9013592 | 2006-01-20 08:47:07 +0000 | [diff] [blame] | 80 | found = 1; |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 81 | break; |
| 82 | } |
| 83 | spin_unlock(&ast_queue_lock); |
| 84 | |
| 85 | if (!found) |
| 86 | break; |
| 87 | |
David Teigland | e5dae54 | 2008-02-06 00:35:45 -0600 | [diff] [blame] | 88 | cast = lkb->lkb_astfn; |
| 89 | bast = lkb->lkb_bastfn; |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 90 | |
| 91 | if ((type & AST_COMP) && cast) |
| 92 | cast(lkb->lkb_astparam); |
| 93 | |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 94 | if ((type & AST_BAST) && bast) |
David Teigland | fd22a51 | 2008-12-09 11:55:46 -0600 | [diff] [blame^] | 95 | bast(lkb->lkb_astparam, bastmode); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 96 | |
| 97 | /* this removes the reference added by dlm_add_ast |
| 98 | and may result in the lkb being freed */ |
| 99 | dlm_put_lkb(lkb); |
| 100 | |
Steven Whitehouse | d61e9aa | 2008-12-10 09:31:02 -0600 | [diff] [blame] | 101 | cond_resched(); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
| 105 | static inline int no_asts(void) |
| 106 | { |
| 107 | int ret; |
| 108 | |
| 109 | spin_lock(&ast_queue_lock); |
| 110 | ret = list_empty(&ast_queue); |
| 111 | spin_unlock(&ast_queue_lock); |
| 112 | return ret; |
| 113 | } |
| 114 | |
| 115 | static int dlm_astd(void *data) |
| 116 | { |
| 117 | while (!kthread_should_stop()) { |
| 118 | set_current_state(TASK_INTERRUPTIBLE); |
| 119 | if (!test_bit(WAKE_ASTS, &astd_wakeflags)) |
| 120 | schedule(); |
| 121 | set_current_state(TASK_RUNNING); |
| 122 | |
David Teigland | 9013592 | 2006-01-20 08:47:07 +0000 | [diff] [blame] | 123 | mutex_lock(&astd_running); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 124 | if (test_and_clear_bit(WAKE_ASTS, &astd_wakeflags)) |
| 125 | process_asts(); |
David Teigland | 9013592 | 2006-01-20 08:47:07 +0000 | [diff] [blame] | 126 | mutex_unlock(&astd_running); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 127 | } |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | void dlm_astd_wake(void) |
| 132 | { |
| 133 | if (!no_asts()) { |
| 134 | set_bit(WAKE_ASTS, &astd_wakeflags); |
| 135 | wake_up_process(astd_task); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | int dlm_astd_start(void) |
| 140 | { |
| 141 | struct task_struct *p; |
| 142 | int error = 0; |
| 143 | |
| 144 | INIT_LIST_HEAD(&ast_queue); |
| 145 | spin_lock_init(&ast_queue_lock); |
David Teigland | 9013592 | 2006-01-20 08:47:07 +0000 | [diff] [blame] | 146 | mutex_init(&astd_running); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 147 | |
| 148 | p = kthread_run(dlm_astd, NULL, "dlm_astd"); |
| 149 | if (IS_ERR(p)) |
| 150 | error = PTR_ERR(p); |
| 151 | else |
| 152 | astd_task = p; |
| 153 | return error; |
| 154 | } |
| 155 | |
| 156 | void dlm_astd_stop(void) |
| 157 | { |
| 158 | kthread_stop(astd_task); |
| 159 | } |
| 160 | |
| 161 | void dlm_astd_suspend(void) |
| 162 | { |
David Teigland | 9013592 | 2006-01-20 08:47:07 +0000 | [diff] [blame] | 163 | mutex_lock(&astd_running); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | void dlm_astd_resume(void) |
| 167 | { |
David Teigland | 9013592 | 2006-01-20 08:47:07 +0000 | [diff] [blame] | 168 | mutex_unlock(&astd_running); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 169 | } |
| 170 | |