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 | 60f98d1 | 2011-11-02 14:30:58 -0500 | [diff] [blame] | 5 | ** Copyright (C) 2004-2011 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 "lockspace.h" |
| 16 | #include "member.h" |
| 17 | #include "recoverd.h" |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 18 | #include "dir.h" |
| 19 | #include "lowcomms.h" |
| 20 | #include "config.h" |
| 21 | #include "memory.h" |
| 22 | #include "lock.h" |
David Teigland | c56b39c | 2006-04-28 10:51:53 -0400 | [diff] [blame] | 23 | #include "recover.h" |
David Teigland | 2896ee3 | 2006-11-27 11:31:22 -0600 | [diff] [blame] | 24 | #include "requestqueue.h" |
David Teigland | 0f8e0d9 | 2008-08-06 13:30:24 -0500 | [diff] [blame] | 25 | #include "user.h" |
David Teigland | 23e8e1a | 2011-04-05 13:16:24 -0500 | [diff] [blame] | 26 | #include "ast.h" |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 27 | |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 28 | static int ls_count; |
David Teigland | 9013592 | 2006-01-20 08:47:07 +0000 | [diff] [blame] | 29 | static struct mutex ls_lock; |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 30 | static struct list_head lslist; |
| 31 | static spinlock_t lslist_lock; |
| 32 | static struct task_struct * scand_task; |
| 33 | |
| 34 | |
| 35 | static ssize_t dlm_control_store(struct dlm_ls *ls, const char *buf, size_t len) |
| 36 | { |
| 37 | ssize_t ret = len; |
| 38 | int n = simple_strtol(buf, NULL, 0); |
| 39 | |
Patrick Caulfield | e2de7f5 | 2006-11-06 08:53:28 +0000 | [diff] [blame] | 40 | ls = dlm_find_lockspace_local(ls->ls_local_handle); |
| 41 | if (!ls) |
| 42 | return -EINVAL; |
| 43 | |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 44 | switch (n) { |
| 45 | case 0: |
| 46 | dlm_ls_stop(ls); |
| 47 | break; |
| 48 | case 1: |
| 49 | dlm_ls_start(ls); |
| 50 | break; |
| 51 | default: |
| 52 | ret = -EINVAL; |
| 53 | } |
Patrick Caulfield | e2de7f5 | 2006-11-06 08:53:28 +0000 | [diff] [blame] | 54 | dlm_put_lockspace(ls); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 55 | return ret; |
| 56 | } |
| 57 | |
| 58 | static ssize_t dlm_event_store(struct dlm_ls *ls, const char *buf, size_t len) |
| 59 | { |
| 60 | ls->ls_uevent_result = simple_strtol(buf, NULL, 0); |
| 61 | set_bit(LSFL_UEVENT_WAIT, &ls->ls_flags); |
| 62 | wake_up(&ls->ls_uevent_wait); |
| 63 | return len; |
| 64 | } |
| 65 | |
| 66 | static ssize_t dlm_id_show(struct dlm_ls *ls, char *buf) |
| 67 | { |
David Teigland | a1d144c | 2006-09-06 17:01:40 -0500 | [diff] [blame] | 68 | return snprintf(buf, PAGE_SIZE, "%u\n", ls->ls_global_id); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | static ssize_t dlm_id_store(struct dlm_ls *ls, const char *buf, size_t len) |
| 72 | { |
| 73 | ls->ls_global_id = simple_strtoul(buf, NULL, 0); |
| 74 | return len; |
| 75 | } |
| 76 | |
David Teigland | 4875647 | 2012-04-26 15:54:29 -0500 | [diff] [blame] | 77 | static ssize_t dlm_nodir_show(struct dlm_ls *ls, char *buf) |
| 78 | { |
| 79 | return snprintf(buf, PAGE_SIZE, "%u\n", dlm_no_directory(ls)); |
| 80 | } |
| 81 | |
| 82 | static ssize_t dlm_nodir_store(struct dlm_ls *ls, const char *buf, size_t len) |
| 83 | { |
| 84 | int val = simple_strtoul(buf, NULL, 0); |
| 85 | if (val == 1) |
| 86 | set_bit(LSFL_NODIR, &ls->ls_flags); |
| 87 | return len; |
| 88 | } |
| 89 | |
David Teigland | c56b39c | 2006-04-28 10:51:53 -0400 | [diff] [blame] | 90 | static ssize_t dlm_recover_status_show(struct dlm_ls *ls, char *buf) |
| 91 | { |
| 92 | uint32_t status = dlm_recover_status(ls); |
David Teigland | a1d144c | 2006-09-06 17:01:40 -0500 | [diff] [blame] | 93 | return snprintf(buf, PAGE_SIZE, "%x\n", status); |
David Teigland | c56b39c | 2006-04-28 10:51:53 -0400 | [diff] [blame] | 94 | } |
| 95 | |
David Teigland | faa0f26 | 2006-08-08 17:08:42 -0500 | [diff] [blame] | 96 | static ssize_t dlm_recover_nodeid_show(struct dlm_ls *ls, char *buf) |
| 97 | { |
David Teigland | a1d144c | 2006-09-06 17:01:40 -0500 | [diff] [blame] | 98 | return snprintf(buf, PAGE_SIZE, "%d\n", ls->ls_recover_nodeid); |
David Teigland | faa0f26 | 2006-08-08 17:08:42 -0500 | [diff] [blame] | 99 | } |
| 100 | |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 101 | struct dlm_attr { |
| 102 | struct attribute attr; |
| 103 | ssize_t (*show)(struct dlm_ls *, char *); |
| 104 | ssize_t (*store)(struct dlm_ls *, const char *, size_t); |
| 105 | }; |
| 106 | |
| 107 | static struct dlm_attr dlm_attr_control = { |
| 108 | .attr = {.name = "control", .mode = S_IWUSR}, |
| 109 | .store = dlm_control_store |
| 110 | }; |
| 111 | |
| 112 | static struct dlm_attr dlm_attr_event = { |
| 113 | .attr = {.name = "event_done", .mode = S_IWUSR}, |
| 114 | .store = dlm_event_store |
| 115 | }; |
| 116 | |
| 117 | static struct dlm_attr dlm_attr_id = { |
| 118 | .attr = {.name = "id", .mode = S_IRUGO | S_IWUSR}, |
| 119 | .show = dlm_id_show, |
| 120 | .store = dlm_id_store |
| 121 | }; |
| 122 | |
David Teigland | 4875647 | 2012-04-26 15:54:29 -0500 | [diff] [blame] | 123 | static struct dlm_attr dlm_attr_nodir = { |
| 124 | .attr = {.name = "nodir", .mode = S_IRUGO | S_IWUSR}, |
| 125 | .show = dlm_nodir_show, |
| 126 | .store = dlm_nodir_store |
| 127 | }; |
| 128 | |
David Teigland | c56b39c | 2006-04-28 10:51:53 -0400 | [diff] [blame] | 129 | static struct dlm_attr dlm_attr_recover_status = { |
| 130 | .attr = {.name = "recover_status", .mode = S_IRUGO}, |
| 131 | .show = dlm_recover_status_show |
| 132 | }; |
| 133 | |
David Teigland | faa0f26 | 2006-08-08 17:08:42 -0500 | [diff] [blame] | 134 | static struct dlm_attr dlm_attr_recover_nodeid = { |
| 135 | .attr = {.name = "recover_nodeid", .mode = S_IRUGO}, |
| 136 | .show = dlm_recover_nodeid_show |
| 137 | }; |
| 138 | |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 139 | static struct attribute *dlm_attrs[] = { |
| 140 | &dlm_attr_control.attr, |
| 141 | &dlm_attr_event.attr, |
| 142 | &dlm_attr_id.attr, |
David Teigland | 4875647 | 2012-04-26 15:54:29 -0500 | [diff] [blame] | 143 | &dlm_attr_nodir.attr, |
David Teigland | c56b39c | 2006-04-28 10:51:53 -0400 | [diff] [blame] | 144 | &dlm_attr_recover_status.attr, |
David Teigland | faa0f26 | 2006-08-08 17:08:42 -0500 | [diff] [blame] | 145 | &dlm_attr_recover_nodeid.attr, |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 146 | NULL, |
| 147 | }; |
| 148 | |
| 149 | static ssize_t dlm_attr_show(struct kobject *kobj, struct attribute *attr, |
| 150 | char *buf) |
| 151 | { |
| 152 | struct dlm_ls *ls = container_of(kobj, struct dlm_ls, ls_kobj); |
| 153 | struct dlm_attr *a = container_of(attr, struct dlm_attr, attr); |
| 154 | return a->show ? a->show(ls, buf) : 0; |
| 155 | } |
| 156 | |
| 157 | static ssize_t dlm_attr_store(struct kobject *kobj, struct attribute *attr, |
| 158 | const char *buf, size_t len) |
| 159 | { |
| 160 | struct dlm_ls *ls = container_of(kobj, struct dlm_ls, ls_kobj); |
| 161 | struct dlm_attr *a = container_of(attr, struct dlm_attr, attr); |
| 162 | return a->store ? a->store(ls, buf, len) : len; |
| 163 | } |
| 164 | |
Patrick Caulfield | ba542e3 | 2006-11-02 14:41:23 +0000 | [diff] [blame] | 165 | static void lockspace_kobj_release(struct kobject *k) |
| 166 | { |
| 167 | struct dlm_ls *ls = container_of(k, struct dlm_ls, ls_kobj); |
| 168 | kfree(ls); |
| 169 | } |
| 170 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 171 | static const struct sysfs_ops dlm_attr_ops = { |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 172 | .show = dlm_attr_show, |
| 173 | .store = dlm_attr_store, |
| 174 | }; |
| 175 | |
| 176 | static struct kobj_type dlm_ktype = { |
| 177 | .default_attrs = dlm_attrs, |
| 178 | .sysfs_ops = &dlm_attr_ops, |
Patrick Caulfield | ba542e3 | 2006-11-02 14:41:23 +0000 | [diff] [blame] | 179 | .release = lockspace_kobj_release, |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 180 | }; |
| 181 | |
Greg Kroah-Hartman | d405936 | 2007-10-29 20:13:17 +0100 | [diff] [blame] | 182 | static struct kset *dlm_kset; |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 183 | |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 184 | static int do_uevent(struct dlm_ls *ls, int in) |
| 185 | { |
| 186 | int error; |
| 187 | |
| 188 | if (in) |
| 189 | kobject_uevent(&ls->ls_kobj, KOBJ_ONLINE); |
| 190 | else |
| 191 | kobject_uevent(&ls->ls_kobj, KOBJ_OFFLINE); |
| 192 | |
David Teigland | 8b0e7b2 | 2007-05-18 09:03:35 -0500 | [diff] [blame] | 193 | log_debug(ls, "%s the lockspace group...", in ? "joining" : "leaving"); |
| 194 | |
| 195 | /* dlm_controld will see the uevent, do the necessary group management |
| 196 | and then write to sysfs to wake us */ |
| 197 | |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 198 | error = wait_event_interruptible(ls->ls_uevent_wait, |
| 199 | test_and_clear_bit(LSFL_UEVENT_WAIT, &ls->ls_flags)); |
David Teigland | 8b0e7b2 | 2007-05-18 09:03:35 -0500 | [diff] [blame] | 200 | |
| 201 | log_debug(ls, "group event done %d %d", error, ls->ls_uevent_result); |
| 202 | |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 203 | if (error) |
| 204 | goto out; |
| 205 | |
| 206 | error = ls->ls_uevent_result; |
| 207 | out: |
David Teigland | 8b0e7b2 | 2007-05-18 09:03:35 -0500 | [diff] [blame] | 208 | if (error) |
| 209 | log_error(ls, "group %s failed %d %d", in ? "join" : "leave", |
| 210 | error, ls->ls_uevent_result); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 211 | return error; |
| 212 | } |
| 213 | |
Steven Whitehouse | b4a5d4b | 2010-02-17 09:41:34 +0000 | [diff] [blame] | 214 | static int dlm_uevent(struct kset *kset, struct kobject *kobj, |
| 215 | struct kobj_uevent_env *env) |
| 216 | { |
| 217 | struct dlm_ls *ls = container_of(kobj, struct dlm_ls, ls_kobj); |
| 218 | |
| 219 | add_uevent_var(env, "LOCKSPACE=%s", ls->ls_name); |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | static struct kset_uevent_ops dlm_uevent_ops = { |
| 224 | .uevent = dlm_uevent, |
| 225 | }; |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 226 | |
Denis Cheng | 3072717 | 2008-02-02 01:53:46 +0800 | [diff] [blame] | 227 | int __init dlm_lockspace_init(void) |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 228 | { |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 229 | ls_count = 0; |
David Teigland | 9013592 | 2006-01-20 08:47:07 +0000 | [diff] [blame] | 230 | mutex_init(&ls_lock); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 231 | INIT_LIST_HEAD(&lslist); |
| 232 | spin_lock_init(&lslist_lock); |
| 233 | |
Steven Whitehouse | b4a5d4b | 2010-02-17 09:41:34 +0000 | [diff] [blame] | 234 | dlm_kset = kset_create_and_add("dlm", &dlm_uevent_ops, kernel_kobj); |
Greg Kroah-Hartman | d405936 | 2007-10-29 20:13:17 +0100 | [diff] [blame] | 235 | if (!dlm_kset) { |
Harvey Harrison | 8e24eea | 2008-04-30 00:55:09 -0700 | [diff] [blame] | 236 | printk(KERN_WARNING "%s: can not create kset\n", __func__); |
Greg Kroah-Hartman | d405936 | 2007-10-29 20:13:17 +0100 | [diff] [blame] | 237 | return -ENOMEM; |
| 238 | } |
| 239 | return 0; |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | void dlm_lockspace_exit(void) |
| 243 | { |
Greg Kroah-Hartman | d405936 | 2007-10-29 20:13:17 +0100 | [diff] [blame] | 244 | kset_unregister(dlm_kset); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 245 | } |
| 246 | |
David Teigland | c1dcf65 | 2008-08-18 14:03:25 -0500 | [diff] [blame] | 247 | static struct dlm_ls *find_ls_to_scan(void) |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 248 | { |
| 249 | struct dlm_ls *ls; |
| 250 | |
David Teigland | c1dcf65 | 2008-08-18 14:03:25 -0500 | [diff] [blame] | 251 | spin_lock(&lslist_lock); |
| 252 | list_for_each_entry(ls, &lslist, ls_list) { |
| 253 | if (time_after_eq(jiffies, ls->ls_scan_time + |
| 254 | dlm_config.ci_scan_secs * HZ)) { |
| 255 | spin_unlock(&lslist_lock); |
| 256 | return ls; |
| 257 | } |
| 258 | } |
| 259 | spin_unlock(&lslist_lock); |
| 260 | return NULL; |
| 261 | } |
| 262 | |
| 263 | static int dlm_scand(void *data) |
| 264 | { |
| 265 | struct dlm_ls *ls; |
David Teigland | c1dcf65 | 2008-08-18 14:03:25 -0500 | [diff] [blame] | 266 | |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 267 | while (!kthread_should_stop()) { |
David Teigland | c1dcf65 | 2008-08-18 14:03:25 -0500 | [diff] [blame] | 268 | ls = find_ls_to_scan(); |
| 269 | if (ls) { |
David Teigland | 85e86ed | 2007-05-18 08:58:15 -0500 | [diff] [blame] | 270 | if (dlm_lock_recovery_try(ls)) { |
David Teigland | c1dcf65 | 2008-08-18 14:03:25 -0500 | [diff] [blame] | 271 | ls->ls_scan_time = jiffies; |
David Teigland | 85e86ed | 2007-05-18 08:58:15 -0500 | [diff] [blame] | 272 | dlm_scan_rsbs(ls); |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 273 | dlm_scan_timeout(ls); |
David Teigland | c6ff669 | 2011-03-28 14:17:26 -0500 | [diff] [blame] | 274 | dlm_scan_waiters(ls); |
David Teigland | 85e86ed | 2007-05-18 08:58:15 -0500 | [diff] [blame] | 275 | dlm_unlock_recovery(ls); |
David Teigland | c1dcf65 | 2008-08-18 14:03:25 -0500 | [diff] [blame] | 276 | } else { |
| 277 | ls->ls_scan_time += HZ; |
David Teigland | 85e86ed | 2007-05-18 08:58:15 -0500 | [diff] [blame] | 278 | } |
David Teigland | c6ff669 | 2011-03-28 14:17:26 -0500 | [diff] [blame] | 279 | continue; |
David Teigland | 85e86ed | 2007-05-18 08:58:15 -0500 | [diff] [blame] | 280 | } |
David Teigland | c6ff669 | 2011-03-28 14:17:26 -0500 | [diff] [blame] | 281 | schedule_timeout_interruptible(dlm_config.ci_scan_secs * HZ); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 282 | } |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | static int dlm_scand_start(void) |
| 287 | { |
| 288 | struct task_struct *p; |
| 289 | int error = 0; |
| 290 | |
| 291 | p = kthread_run(dlm_scand, NULL, "dlm_scand"); |
| 292 | if (IS_ERR(p)) |
| 293 | error = PTR_ERR(p); |
| 294 | else |
| 295 | scand_task = p; |
| 296 | return error; |
| 297 | } |
| 298 | |
| 299 | static void dlm_scand_stop(void) |
| 300 | { |
| 301 | kthread_stop(scand_task); |
| 302 | } |
| 303 | |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 304 | struct dlm_ls *dlm_find_lockspace_global(uint32_t id) |
| 305 | { |
| 306 | struct dlm_ls *ls; |
| 307 | |
| 308 | spin_lock(&lslist_lock); |
| 309 | |
| 310 | list_for_each_entry(ls, &lslist, ls_list) { |
| 311 | if (ls->ls_global_id == id) { |
| 312 | ls->ls_count++; |
| 313 | goto out; |
| 314 | } |
| 315 | } |
| 316 | ls = NULL; |
| 317 | out: |
| 318 | spin_unlock(&lslist_lock); |
| 319 | return ls; |
| 320 | } |
| 321 | |
David Teigland | 597d0ca | 2006-07-12 16:44:04 -0500 | [diff] [blame] | 322 | struct dlm_ls *dlm_find_lockspace_local(dlm_lockspace_t *lockspace) |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 323 | { |
David Teigland | 597d0ca | 2006-07-12 16:44:04 -0500 | [diff] [blame] | 324 | struct dlm_ls *ls; |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 325 | |
| 326 | spin_lock(&lslist_lock); |
David Teigland | 597d0ca | 2006-07-12 16:44:04 -0500 | [diff] [blame] | 327 | list_for_each_entry(ls, &lslist, ls_list) { |
| 328 | if (ls->ls_local_handle == lockspace) { |
| 329 | ls->ls_count++; |
| 330 | goto out; |
| 331 | } |
| 332 | } |
| 333 | ls = NULL; |
| 334 | out: |
| 335 | spin_unlock(&lslist_lock); |
| 336 | return ls; |
| 337 | } |
| 338 | |
| 339 | struct dlm_ls *dlm_find_lockspace_device(int minor) |
| 340 | { |
| 341 | struct dlm_ls *ls; |
| 342 | |
| 343 | spin_lock(&lslist_lock); |
| 344 | list_for_each_entry(ls, &lslist, ls_list) { |
| 345 | if (ls->ls_device.minor == minor) { |
| 346 | ls->ls_count++; |
| 347 | goto out; |
| 348 | } |
| 349 | } |
| 350 | ls = NULL; |
| 351 | out: |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 352 | spin_unlock(&lslist_lock); |
| 353 | return ls; |
| 354 | } |
| 355 | |
| 356 | void dlm_put_lockspace(struct dlm_ls *ls) |
| 357 | { |
| 358 | spin_lock(&lslist_lock); |
| 359 | ls->ls_count--; |
| 360 | spin_unlock(&lslist_lock); |
| 361 | } |
| 362 | |
| 363 | static void remove_lockspace(struct dlm_ls *ls) |
| 364 | { |
| 365 | for (;;) { |
| 366 | spin_lock(&lslist_lock); |
| 367 | if (ls->ls_count == 0) { |
David Teigland | 0f8e0d9 | 2008-08-06 13:30:24 -0500 | [diff] [blame] | 368 | WARN_ON(ls->ls_create_count != 0); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 369 | list_del(&ls->ls_list); |
| 370 | spin_unlock(&lslist_lock); |
| 371 | return; |
| 372 | } |
| 373 | spin_unlock(&lslist_lock); |
| 374 | ssleep(1); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | static int threads_start(void) |
| 379 | { |
| 380 | int error; |
| 381 | |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 382 | error = dlm_scand_start(); |
| 383 | if (error) { |
| 384 | log_print("cannot start dlm_scand thread %d", error); |
David Teigland | 23e8e1a | 2011-04-05 13:16:24 -0500 | [diff] [blame] | 385 | goto fail; |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | /* Thread for sending/receiving messages for all lockspace's */ |
| 389 | error = dlm_lowcomms_start(); |
| 390 | if (error) { |
| 391 | log_print("cannot start dlm lowcomms %d", error); |
| 392 | goto scand_fail; |
| 393 | } |
| 394 | |
| 395 | return 0; |
| 396 | |
| 397 | scand_fail: |
| 398 | dlm_scand_stop(); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 399 | fail: |
| 400 | return error; |
| 401 | } |
| 402 | |
| 403 | static void threads_stop(void) |
| 404 | { |
| 405 | dlm_scand_stop(); |
| 406 | dlm_lowcomms_stop(); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 407 | } |
| 408 | |
David Teigland | 60f98d1 | 2011-11-02 14:30:58 -0500 | [diff] [blame] | 409 | static int new_lockspace(const char *name, const char *cluster, |
| 410 | uint32_t flags, int lvblen, |
| 411 | const struct dlm_lockspace_ops *ops, void *ops_arg, |
| 412 | int *ops_result, dlm_lockspace_t **lockspace) |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 413 | { |
| 414 | struct dlm_ls *ls; |
David Teigland | 0f8e0d9 | 2008-08-06 13:30:24 -0500 | [diff] [blame] | 415 | int i, size, error; |
David Teigland | 79d72b5 | 2007-05-18 09:02:20 -0500 | [diff] [blame] | 416 | int do_unreg = 0; |
David Teigland | 60f98d1 | 2011-11-02 14:30:58 -0500 | [diff] [blame] | 417 | int namelen = strlen(name); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 418 | |
| 419 | if (namelen > DLM_LOCKSPACE_LEN) |
| 420 | return -EINVAL; |
| 421 | |
| 422 | if (!lvblen || (lvblen % 8)) |
| 423 | return -EINVAL; |
| 424 | |
| 425 | if (!try_module_get(THIS_MODULE)) |
| 426 | return -EINVAL; |
| 427 | |
David Teigland | dc68c7e | 2008-08-18 11:43:30 -0500 | [diff] [blame] | 428 | if (!dlm_user_daemon_available()) { |
David Teigland | 60f98d1 | 2011-11-02 14:30:58 -0500 | [diff] [blame] | 429 | log_print("dlm user daemon not available"); |
| 430 | error = -EUNATCH; |
| 431 | goto out; |
| 432 | } |
| 433 | |
| 434 | if (ops && ops_result) { |
| 435 | if (!dlm_config.ci_recover_callbacks) |
| 436 | *ops_result = -EOPNOTSUPP; |
| 437 | else |
| 438 | *ops_result = 0; |
| 439 | } |
| 440 | |
| 441 | if (dlm_config.ci_recover_callbacks && cluster && |
| 442 | strncmp(cluster, dlm_config.ci_cluster_name, DLM_LOCKSPACE_LEN)) { |
| 443 | log_print("dlm cluster name %s mismatch %s", |
| 444 | dlm_config.ci_cluster_name, cluster); |
| 445 | error = -EBADR; |
| 446 | goto out; |
David Teigland | dc68c7e | 2008-08-18 11:43:30 -0500 | [diff] [blame] | 447 | } |
| 448 | |
David Teigland | 0f8e0d9 | 2008-08-06 13:30:24 -0500 | [diff] [blame] | 449 | error = 0; |
| 450 | |
| 451 | spin_lock(&lslist_lock); |
| 452 | list_for_each_entry(ls, &lslist, ls_list) { |
| 453 | WARN_ON(ls->ls_create_count <= 0); |
| 454 | if (ls->ls_namelen != namelen) |
| 455 | continue; |
| 456 | if (memcmp(ls->ls_name, name, namelen)) |
| 457 | continue; |
| 458 | if (flags & DLM_LSFL_NEWEXCL) { |
| 459 | error = -EEXIST; |
| 460 | break; |
| 461 | } |
| 462 | ls->ls_create_count++; |
David Teigland | 8511a27 | 2009-04-08 15:38:43 -0500 | [diff] [blame] | 463 | *lockspace = ls; |
| 464 | error = 1; |
David Teigland | 0f8e0d9 | 2008-08-06 13:30:24 -0500 | [diff] [blame] | 465 | break; |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 466 | } |
David Teigland | 0f8e0d9 | 2008-08-06 13:30:24 -0500 | [diff] [blame] | 467 | spin_unlock(&lslist_lock); |
| 468 | |
David Teigland | 0f8e0d9 | 2008-08-06 13:30:24 -0500 | [diff] [blame] | 469 | if (error) |
David Teigland | 8511a27 | 2009-04-08 15:38:43 -0500 | [diff] [blame] | 470 | goto out; |
David Teigland | 0f8e0d9 | 2008-08-06 13:30:24 -0500 | [diff] [blame] | 471 | |
| 472 | error = -ENOMEM; |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 473 | |
David Teigland | 573c24c | 2009-11-30 16:34:43 -0600 | [diff] [blame] | 474 | ls = kzalloc(sizeof(struct dlm_ls) + namelen, GFP_NOFS); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 475 | if (!ls) |
| 476 | goto out; |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 477 | memcpy(ls->ls_name, name, namelen); |
| 478 | ls->ls_namelen = namelen; |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 479 | ls->ls_lvblen = lvblen; |
| 480 | ls->ls_count = 0; |
| 481 | ls->ls_flags = 0; |
David Teigland | c1dcf65 | 2008-08-18 14:03:25 -0500 | [diff] [blame] | 482 | ls->ls_scan_time = jiffies; |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 483 | |
David Teigland | 60f98d1 | 2011-11-02 14:30:58 -0500 | [diff] [blame] | 484 | if (ops && dlm_config.ci_recover_callbacks) { |
| 485 | ls->ls_ops = ops; |
| 486 | ls->ls_ops_arg = ops_arg; |
| 487 | } |
| 488 | |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 489 | if (flags & DLM_LSFL_TIMEWARN) |
| 490 | set_bit(LSFL_TIMEWARN, &ls->ls_flags); |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 491 | |
David Teigland | fad59c1 | 2007-06-11 10:47:18 -0500 | [diff] [blame] | 492 | /* ls_exflags are forced to match among nodes, and we don't |
David Teigland | 0f8e0d9 | 2008-08-06 13:30:24 -0500 | [diff] [blame] | 493 | need to require all nodes to have some flags set */ |
| 494 | ls->ls_exflags = (flags & ~(DLM_LSFL_TIMEWARN | DLM_LSFL_FS | |
| 495 | DLM_LSFL_NEWEXCL)); |
David Teigland | fad59c1 | 2007-06-11 10:47:18 -0500 | [diff] [blame] | 496 | |
David Teigland | 68c817a | 2007-01-09 09:41:48 -0600 | [diff] [blame] | 497 | size = dlm_config.ci_rsbtbl_size; |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 498 | ls->ls_rsbtbl_size = size; |
| 499 | |
Bryn M. Reeves | c282af4 | 2011-07-01 15:49:23 -0500 | [diff] [blame] | 500 | ls->ls_rsbtbl = vmalloc(sizeof(struct dlm_rsbtable) * size); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 501 | if (!ls->ls_rsbtbl) |
| 502 | goto out_lsfree; |
| 503 | for (i = 0; i < size; i++) { |
Bob Peterson | 9beb3bf | 2011-10-26 15:24:55 -0500 | [diff] [blame] | 504 | ls->ls_rsbtbl[i].keep.rb_node = NULL; |
| 505 | ls->ls_rsbtbl[i].toss.rb_node = NULL; |
David Teigland | c7be761 | 2009-01-07 16:50:41 -0600 | [diff] [blame] | 506 | spin_lock_init(&ls->ls_rsbtbl[i].lock); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 507 | } |
| 508 | |
David Teigland | 05c32f4 | 2012-06-14 12:17:32 -0500 | [diff] [blame] | 509 | spin_lock_init(&ls->ls_remove_spin); |
| 510 | |
| 511 | for (i = 0; i < DLM_REMOVE_NAMES_MAX; i++) { |
| 512 | ls->ls_remove_names[i] = kzalloc(DLM_RESNAME_MAXLEN+1, |
| 513 | GFP_KERNEL); |
| 514 | if (!ls->ls_remove_names[i]) |
| 515 | goto out_rsbtbl; |
| 516 | } |
| 517 | |
David Teigland | 3d6aa67 | 2011-07-06 17:00:54 -0500 | [diff] [blame] | 518 | idr_init(&ls->ls_lkbidr); |
| 519 | spin_lock_init(&ls->ls_lkbidr_spin); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 520 | |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 521 | INIT_LIST_HEAD(&ls->ls_waiters); |
David Teigland | 9013592 | 2006-01-20 08:47:07 +0000 | [diff] [blame] | 522 | mutex_init(&ls->ls_waiters_mutex); |
David Teigland | ef0c2bb | 2007-03-28 09:56:46 -0500 | [diff] [blame] | 523 | INIT_LIST_HEAD(&ls->ls_orphans); |
| 524 | mutex_init(&ls->ls_orphans_mutex); |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 525 | INIT_LIST_HEAD(&ls->ls_timeout); |
| 526 | mutex_init(&ls->ls_timeout_mutex); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 527 | |
David Teigland | 3881ac0 | 2011-07-07 14:05:03 -0500 | [diff] [blame] | 528 | INIT_LIST_HEAD(&ls->ls_new_rsb); |
| 529 | spin_lock_init(&ls->ls_new_rsb_spin); |
| 530 | |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 531 | INIT_LIST_HEAD(&ls->ls_nodes); |
| 532 | INIT_LIST_HEAD(&ls->ls_nodes_gone); |
| 533 | ls->ls_num_nodes = 0; |
| 534 | ls->ls_low_nodeid = 0; |
| 535 | ls->ls_total_weight = 0; |
| 536 | ls->ls_node_array = NULL; |
| 537 | |
| 538 | memset(&ls->ls_stub_rsb, 0, sizeof(struct dlm_rsb)); |
| 539 | ls->ls_stub_rsb.res_ls = ls; |
| 540 | |
David Teigland | 5de6319 | 2006-07-25 13:44:31 -0500 | [diff] [blame] | 541 | ls->ls_debug_rsb_dentry = NULL; |
| 542 | ls->ls_debug_waiters_dentry = NULL; |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 543 | |
| 544 | init_waitqueue_head(&ls->ls_uevent_wait); |
| 545 | ls->ls_uevent_result = 0; |
David Teigland | 8b0e7b2 | 2007-05-18 09:03:35 -0500 | [diff] [blame] | 546 | init_completion(&ls->ls_members_done); |
| 547 | ls->ls_members_result = -1; |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 548 | |
David Teigland | 23e8e1a | 2011-04-05 13:16:24 -0500 | [diff] [blame] | 549 | mutex_init(&ls->ls_cb_mutex); |
| 550 | INIT_LIST_HEAD(&ls->ls_cb_delay); |
| 551 | |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 552 | ls->ls_recoverd_task = NULL; |
David Teigland | 9013592 | 2006-01-20 08:47:07 +0000 | [diff] [blame] | 553 | mutex_init(&ls->ls_recoverd_active); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 554 | spin_lock_init(&ls->ls_recover_lock); |
David Teigland | 98f176f | 2006-11-27 13:19:28 -0600 | [diff] [blame] | 555 | spin_lock_init(&ls->ls_rcom_spin); |
| 556 | get_random_bytes(&ls->ls_rcom_seq, sizeof(uint64_t)); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 557 | ls->ls_recover_status = 0; |
| 558 | ls->ls_recover_seq = 0; |
| 559 | ls->ls_recover_args = NULL; |
| 560 | init_rwsem(&ls->ls_in_recovery); |
David Teigland | c36258b | 2007-09-27 15:53:38 -0500 | [diff] [blame] | 561 | init_rwsem(&ls->ls_recv_active); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 562 | INIT_LIST_HEAD(&ls->ls_requestqueue); |
David Teigland | 9013592 | 2006-01-20 08:47:07 +0000 | [diff] [blame] | 563 | mutex_init(&ls->ls_requestqueue_mutex); |
David Teigland | 597d0ca | 2006-07-12 16:44:04 -0500 | [diff] [blame] | 564 | mutex_init(&ls->ls_clear_proc_locks); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 565 | |
David Teigland | 573c24c | 2009-11-30 16:34:43 -0600 | [diff] [blame] | 566 | ls->ls_recover_buf = kmalloc(dlm_config.ci_buffer_size, GFP_NOFS); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 567 | if (!ls->ls_recover_buf) |
David Teigland | 05c32f4 | 2012-06-14 12:17:32 -0500 | [diff] [blame] | 568 | goto out_lkbidr; |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 569 | |
David Teigland | 757a427 | 2011-10-20 13:26:28 -0500 | [diff] [blame] | 570 | ls->ls_slot = 0; |
| 571 | ls->ls_num_slots = 0; |
| 572 | ls->ls_slots_size = 0; |
| 573 | ls->ls_slots = NULL; |
| 574 | |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 575 | INIT_LIST_HEAD(&ls->ls_recover_list); |
| 576 | spin_lock_init(&ls->ls_recover_list_lock); |
David Teigland | 1d7c484 | 2012-05-15 16:07:49 -0500 | [diff] [blame] | 577 | idr_init(&ls->ls_recover_idr); |
| 578 | spin_lock_init(&ls->ls_recover_idr_lock); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 579 | ls->ls_recover_list_count = 0; |
David Teigland | 597d0ca | 2006-07-12 16:44:04 -0500 | [diff] [blame] | 580 | ls->ls_local_handle = ls; |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 581 | init_waitqueue_head(&ls->ls_wait_general); |
| 582 | INIT_LIST_HEAD(&ls->ls_root_list); |
| 583 | init_rwsem(&ls->ls_root_sem); |
| 584 | |
David Teigland | 5f88f1e | 2006-08-24 14:47:20 -0500 | [diff] [blame] | 585 | spin_lock(&lslist_lock); |
David Teigland | 0f8e0d9 | 2008-08-06 13:30:24 -0500 | [diff] [blame] | 586 | ls->ls_create_count = 1; |
David Teigland | 5f88f1e | 2006-08-24 14:47:20 -0500 | [diff] [blame] | 587 | list_add(&ls->ls_list, &lslist); |
| 588 | spin_unlock(&lslist_lock); |
| 589 | |
David Teigland | 23e8e1a | 2011-04-05 13:16:24 -0500 | [diff] [blame] | 590 | if (flags & DLM_LSFL_FS) { |
| 591 | error = dlm_callback_start(ls); |
| 592 | if (error) { |
| 593 | log_error(ls, "can't start dlm_callback %d", error); |
| 594 | goto out_delist; |
| 595 | } |
| 596 | } |
| 597 | |
David Teigland | 475f230 | 2012-08-02 11:08:21 -0500 | [diff] [blame] | 598 | init_waitqueue_head(&ls->ls_recover_lock_wait); |
| 599 | |
| 600 | /* |
| 601 | * Once started, dlm_recoverd first looks for ls in lslist, then |
| 602 | * initializes ls_in_recovery as locked in "down" mode. We need |
| 603 | * to wait for the wakeup from dlm_recoverd because in_recovery |
| 604 | * has to start out in down mode. |
| 605 | */ |
| 606 | |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 607 | error = dlm_recoverd_start(ls); |
| 608 | if (error) { |
| 609 | log_error(ls, "can't start dlm_recoverd %d", error); |
David Teigland | 23e8e1a | 2011-04-05 13:16:24 -0500 | [diff] [blame] | 610 | goto out_callback; |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 611 | } |
| 612 | |
David Teigland | 475f230 | 2012-08-02 11:08:21 -0500 | [diff] [blame] | 613 | wait_event(ls->ls_recover_lock_wait, |
| 614 | test_bit(LSFL_RECOVER_LOCK, &ls->ls_flags)); |
| 615 | |
Greg Kroah-Hartman | 901195e | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 616 | ls->ls_kobj.kset = dlm_kset; |
| 617 | error = kobject_init_and_add(&ls->ls_kobj, &dlm_ktype, NULL, |
| 618 | "%s", ls->ls_name); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 619 | if (error) |
David Teigland | 23e8e1a | 2011-04-05 13:16:24 -0500 | [diff] [blame] | 620 | goto out_recoverd; |
Greg Kroah-Hartman | 901195e | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 621 | kobject_uevent(&ls->ls_kobj, KOBJ_ADD); |
David Teigland | 79d72b5 | 2007-05-18 09:02:20 -0500 | [diff] [blame] | 622 | |
| 623 | /* let kobject handle freeing of ls if there's an error */ |
| 624 | do_unreg = 1; |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 625 | |
David Teigland | 8b0e7b2 | 2007-05-18 09:03:35 -0500 | [diff] [blame] | 626 | /* This uevent triggers dlm_controld in userspace to add us to the |
| 627 | group of nodes that are members of this lockspace (managed by the |
| 628 | cluster infrastructure.) Once it's done that, it tells us who the |
| 629 | current lockspace members are (via configfs) and then tells the |
| 630 | lockspace to start running (via sysfs) in dlm_ls_start(). */ |
| 631 | |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 632 | error = do_uevent(ls, 1); |
| 633 | if (error) |
David Teigland | 23e8e1a | 2011-04-05 13:16:24 -0500 | [diff] [blame] | 634 | goto out_recoverd; |
David Teigland | 79d72b5 | 2007-05-18 09:02:20 -0500 | [diff] [blame] | 635 | |
David Teigland | 8b0e7b2 | 2007-05-18 09:03:35 -0500 | [diff] [blame] | 636 | wait_for_completion(&ls->ls_members_done); |
| 637 | error = ls->ls_members_result; |
| 638 | if (error) |
| 639 | goto out_members; |
| 640 | |
David Teigland | 79d72b5 | 2007-05-18 09:02:20 -0500 | [diff] [blame] | 641 | dlm_create_debug_file(ls); |
| 642 | |
| 643 | log_debug(ls, "join complete"); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 644 | *lockspace = ls; |
| 645 | return 0; |
| 646 | |
David Teigland | 8b0e7b2 | 2007-05-18 09:03:35 -0500 | [diff] [blame] | 647 | out_members: |
| 648 | do_uevent(ls, 0); |
| 649 | dlm_clear_members(ls); |
| 650 | kfree(ls->ls_node_array); |
David Teigland | 23e8e1a | 2011-04-05 13:16:24 -0500 | [diff] [blame] | 651 | out_recoverd: |
David Teigland | 5f88f1e | 2006-08-24 14:47:20 -0500 | [diff] [blame] | 652 | dlm_recoverd_stop(ls); |
David Teigland | 23e8e1a | 2011-04-05 13:16:24 -0500 | [diff] [blame] | 653 | out_callback: |
| 654 | dlm_callback_stop(ls); |
David Teigland | 79d72b5 | 2007-05-18 09:02:20 -0500 | [diff] [blame] | 655 | out_delist: |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 656 | spin_lock(&lslist_lock); |
| 657 | list_del(&ls->ls_list); |
| 658 | spin_unlock(&lslist_lock); |
David Teigland | 1d7c484 | 2012-05-15 16:07:49 -0500 | [diff] [blame] | 659 | idr_destroy(&ls->ls_recover_idr); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 660 | kfree(ls->ls_recover_buf); |
David Teigland | 05c32f4 | 2012-06-14 12:17:32 -0500 | [diff] [blame] | 661 | out_lkbidr: |
David Teigland | 3d6aa67 | 2011-07-06 17:00:54 -0500 | [diff] [blame] | 662 | idr_destroy(&ls->ls_lkbidr); |
David Teigland | 05c32f4 | 2012-06-14 12:17:32 -0500 | [diff] [blame] | 663 | for (i = 0; i < DLM_REMOVE_NAMES_MAX; i++) { |
| 664 | if (ls->ls_remove_names[i]) |
| 665 | kfree(ls->ls_remove_names[i]); |
| 666 | } |
| 667 | out_rsbtbl: |
Bryn M. Reeves | c282af4 | 2011-07-01 15:49:23 -0500 | [diff] [blame] | 668 | vfree(ls->ls_rsbtbl); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 669 | out_lsfree: |
David Teigland | 79d72b5 | 2007-05-18 09:02:20 -0500 | [diff] [blame] | 670 | if (do_unreg) |
Greg Kroah-Hartman | 197b12d | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 671 | kobject_put(&ls->ls_kobj); |
David Teigland | 79d72b5 | 2007-05-18 09:02:20 -0500 | [diff] [blame] | 672 | else |
| 673 | kfree(ls); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 674 | out: |
| 675 | module_put(THIS_MODULE); |
| 676 | return error; |
| 677 | } |
| 678 | |
David Teigland | 60f98d1 | 2011-11-02 14:30:58 -0500 | [diff] [blame] | 679 | int dlm_new_lockspace(const char *name, const char *cluster, |
| 680 | uint32_t flags, int lvblen, |
| 681 | const struct dlm_lockspace_ops *ops, void *ops_arg, |
| 682 | int *ops_result, dlm_lockspace_t **lockspace) |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 683 | { |
| 684 | int error = 0; |
| 685 | |
David Teigland | 9013592 | 2006-01-20 08:47:07 +0000 | [diff] [blame] | 686 | mutex_lock(&ls_lock); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 687 | if (!ls_count) |
| 688 | error = threads_start(); |
| 689 | if (error) |
| 690 | goto out; |
| 691 | |
David Teigland | 60f98d1 | 2011-11-02 14:30:58 -0500 | [diff] [blame] | 692 | error = new_lockspace(name, cluster, flags, lvblen, ops, ops_arg, |
| 693 | ops_result, lockspace); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 694 | if (!error) |
| 695 | ls_count++; |
David Teigland | 8511a27 | 2009-04-08 15:38:43 -0500 | [diff] [blame] | 696 | if (error > 0) |
| 697 | error = 0; |
| 698 | if (!ls_count) |
David Teigland | 8b0e7b2 | 2007-05-18 09:03:35 -0500 | [diff] [blame] | 699 | threads_stop(); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 700 | out: |
David Teigland | 9013592 | 2006-01-20 08:47:07 +0000 | [diff] [blame] | 701 | mutex_unlock(&ls_lock); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 702 | return error; |
| 703 | } |
| 704 | |
David Teigland | 3d6aa67 | 2011-07-06 17:00:54 -0500 | [diff] [blame] | 705 | static int lkb_idr_is_local(int id, void *p, void *data) |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 706 | { |
David Teigland | 3d6aa67 | 2011-07-06 17:00:54 -0500 | [diff] [blame] | 707 | struct dlm_lkb *lkb = p; |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 708 | |
David Teigland | 3d6aa67 | 2011-07-06 17:00:54 -0500 | [diff] [blame] | 709 | if (!lkb->lkb_nodeid) |
| 710 | return 1; |
| 711 | return 0; |
| 712 | } |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 713 | |
David Teigland | 3d6aa67 | 2011-07-06 17:00:54 -0500 | [diff] [blame] | 714 | static int lkb_idr_is_any(int id, void *p, void *data) |
| 715 | { |
| 716 | return 1; |
| 717 | } |
| 718 | |
| 719 | static int lkb_idr_free(int id, void *p, void *data) |
| 720 | { |
| 721 | struct dlm_lkb *lkb = p; |
| 722 | |
David Teigland | 3d6aa67 | 2011-07-06 17:00:54 -0500 | [diff] [blame] | 723 | if (lkb->lkb_lvbptr && lkb->lkb_flags & DLM_IFL_MSTCPY) |
| 724 | dlm_free_lvb(lkb->lkb_lvbptr); |
| 725 | |
| 726 | dlm_free_lkb(lkb); |
| 727 | return 0; |
| 728 | } |
| 729 | |
| 730 | /* NOTE: We check the lkbidr here rather than the resource table. |
| 731 | This is because there may be LKBs queued as ASTs that have been unlinked |
| 732 | from their RSBs and are pending deletion once the AST has been delivered */ |
| 733 | |
| 734 | static int lockspace_busy(struct dlm_ls *ls, int force) |
| 735 | { |
| 736 | int rv; |
| 737 | |
| 738 | spin_lock(&ls->ls_lkbidr_spin); |
| 739 | if (force == 0) { |
| 740 | rv = idr_for_each(&ls->ls_lkbidr, lkb_idr_is_any, ls); |
| 741 | } else if (force == 1) { |
| 742 | rv = idr_for_each(&ls->ls_lkbidr, lkb_idr_is_local, ls); |
| 743 | } else { |
| 744 | rv = 0; |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 745 | } |
David Teigland | 3d6aa67 | 2011-07-06 17:00:54 -0500 | [diff] [blame] | 746 | spin_unlock(&ls->ls_lkbidr_spin); |
| 747 | return rv; |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | static int release_lockspace(struct dlm_ls *ls, int force) |
| 751 | { |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 752 | struct dlm_rsb *rsb; |
Bob Peterson | 9beb3bf | 2011-10-26 15:24:55 -0500 | [diff] [blame] | 753 | struct rb_node *n; |
David Teigland | 0f8e0d9 | 2008-08-06 13:30:24 -0500 | [diff] [blame] | 754 | int i, busy, rv; |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 755 | |
David Teigland | 3d6aa67 | 2011-07-06 17:00:54 -0500 | [diff] [blame] | 756 | busy = lockspace_busy(ls, force); |
David Teigland | 0f8e0d9 | 2008-08-06 13:30:24 -0500 | [diff] [blame] | 757 | |
| 758 | spin_lock(&lslist_lock); |
| 759 | if (ls->ls_create_count == 1) { |
David Teigland | 3d6aa67 | 2011-07-06 17:00:54 -0500 | [diff] [blame] | 760 | if (busy) { |
David Teigland | 0f8e0d9 | 2008-08-06 13:30:24 -0500 | [diff] [blame] | 761 | rv = -EBUSY; |
David Teigland | 3d6aa67 | 2011-07-06 17:00:54 -0500 | [diff] [blame] | 762 | } else { |
David Teigland | 0f8e0d9 | 2008-08-06 13:30:24 -0500 | [diff] [blame] | 763 | /* remove_lockspace takes ls off lslist */ |
| 764 | ls->ls_create_count = 0; |
| 765 | rv = 0; |
| 766 | } |
| 767 | } else if (ls->ls_create_count > 1) { |
| 768 | rv = --ls->ls_create_count; |
| 769 | } else { |
| 770 | rv = -EINVAL; |
| 771 | } |
| 772 | spin_unlock(&lslist_lock); |
| 773 | |
| 774 | if (rv) { |
| 775 | log_debug(ls, "release_lockspace no remove %d", rv); |
| 776 | return rv; |
| 777 | } |
| 778 | |
| 779 | dlm_device_deregister(ls); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 780 | |
David Teigland | dc68c7e | 2008-08-18 11:43:30 -0500 | [diff] [blame] | 781 | if (force < 3 && dlm_user_daemon_available()) |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 782 | do_uevent(ls, 0); |
| 783 | |
| 784 | dlm_recoverd_stop(ls); |
| 785 | |
David Teigland | 23e8e1a | 2011-04-05 13:16:24 -0500 | [diff] [blame] | 786 | dlm_callback_stop(ls); |
| 787 | |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 788 | remove_lockspace(ls); |
| 789 | |
| 790 | dlm_delete_debug_file(ls); |
| 791 | |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 792 | kfree(ls->ls_recover_buf); |
| 793 | |
| 794 | /* |
David Teigland | 3d6aa67 | 2011-07-06 17:00:54 -0500 | [diff] [blame] | 795 | * Free all lkb's in idr |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 796 | */ |
| 797 | |
David Teigland | 3d6aa67 | 2011-07-06 17:00:54 -0500 | [diff] [blame] | 798 | idr_for_each(&ls->ls_lkbidr, lkb_idr_free, ls); |
| 799 | idr_remove_all(&ls->ls_lkbidr); |
| 800 | idr_destroy(&ls->ls_lkbidr); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 801 | |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 802 | /* |
| 803 | * Free all rsb's on rsbtbl[] lists |
| 804 | */ |
| 805 | |
| 806 | for (i = 0; i < ls->ls_rsbtbl_size; i++) { |
Bob Peterson | 9beb3bf | 2011-10-26 15:24:55 -0500 | [diff] [blame] | 807 | while ((n = rb_first(&ls->ls_rsbtbl[i].keep))) { |
| 808 | rsb = rb_entry(n, struct dlm_rsb, res_hashnode); |
| 809 | rb_erase(n, &ls->ls_rsbtbl[i].keep); |
David Teigland | 52bda2b | 2007-11-07 09:06:49 -0600 | [diff] [blame] | 810 | dlm_free_rsb(rsb); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 811 | } |
| 812 | |
Bob Peterson | 9beb3bf | 2011-10-26 15:24:55 -0500 | [diff] [blame] | 813 | while ((n = rb_first(&ls->ls_rsbtbl[i].toss))) { |
| 814 | rsb = rb_entry(n, struct dlm_rsb, res_hashnode); |
| 815 | rb_erase(n, &ls->ls_rsbtbl[i].toss); |
David Teigland | 52bda2b | 2007-11-07 09:06:49 -0600 | [diff] [blame] | 816 | dlm_free_rsb(rsb); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 817 | } |
| 818 | } |
| 819 | |
Bryn M. Reeves | c282af4 | 2011-07-01 15:49:23 -0500 | [diff] [blame] | 820 | vfree(ls->ls_rsbtbl); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 821 | |
David Teigland | 05c32f4 | 2012-06-14 12:17:32 -0500 | [diff] [blame] | 822 | for (i = 0; i < DLM_REMOVE_NAMES_MAX; i++) |
| 823 | kfree(ls->ls_remove_names[i]); |
| 824 | |
David Teigland | 3881ac0 | 2011-07-07 14:05:03 -0500 | [diff] [blame] | 825 | while (!list_empty(&ls->ls_new_rsb)) { |
| 826 | rsb = list_first_entry(&ls->ls_new_rsb, struct dlm_rsb, |
| 827 | res_hashchain); |
| 828 | list_del(&rsb->res_hashchain); |
| 829 | dlm_free_rsb(rsb); |
| 830 | } |
| 831 | |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 832 | /* |
| 833 | * Free structures on any other lists |
| 834 | */ |
| 835 | |
David Teigland | 2896ee3 | 2006-11-27 11:31:22 -0600 | [diff] [blame] | 836 | dlm_purge_requestqueue(ls); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 837 | kfree(ls->ls_recover_args); |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 838 | dlm_clear_members(ls); |
| 839 | dlm_clear_members_gone(ls); |
| 840 | kfree(ls->ls_node_array); |
David Teigland | 0f8e0d9 | 2008-08-06 13:30:24 -0500 | [diff] [blame] | 841 | log_debug(ls, "release_lockspace final free"); |
Greg Kroah-Hartman | 197b12d | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 842 | kobject_put(&ls->ls_kobj); |
David Teigland | 79d72b5 | 2007-05-18 09:02:20 -0500 | [diff] [blame] | 843 | /* The ls structure will be freed when the kobject is done with */ |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 844 | |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 845 | module_put(THIS_MODULE); |
| 846 | return 0; |
| 847 | } |
| 848 | |
| 849 | /* |
| 850 | * Called when a system has released all its locks and is not going to use the |
| 851 | * lockspace any longer. We free everything we're managing for this lockspace. |
| 852 | * Remaining nodes will go through the recovery process as if we'd died. The |
| 853 | * lockspace must continue to function as usual, participating in recoveries, |
| 854 | * until this returns. |
| 855 | * |
| 856 | * Force has 4 possible values: |
| 857 | * 0 - don't destroy locksapce if it has any LKBs |
| 858 | * 1 - destroy lockspace if it has remote LKBs but not if it has local LKBs |
| 859 | * 2 - destroy lockspace regardless of LKBs |
| 860 | * 3 - destroy lockspace as part of a forced shutdown |
| 861 | */ |
| 862 | |
| 863 | int dlm_release_lockspace(void *lockspace, int force) |
| 864 | { |
| 865 | struct dlm_ls *ls; |
David Teigland | 0f8e0d9 | 2008-08-06 13:30:24 -0500 | [diff] [blame] | 866 | int error; |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 867 | |
| 868 | ls = dlm_find_lockspace_local(lockspace); |
| 869 | if (!ls) |
| 870 | return -EINVAL; |
| 871 | dlm_put_lockspace(ls); |
David Teigland | 0f8e0d9 | 2008-08-06 13:30:24 -0500 | [diff] [blame] | 872 | |
| 873 | mutex_lock(&ls_lock); |
| 874 | error = release_lockspace(ls, force); |
| 875 | if (!error) |
| 876 | ls_count--; |
David Teigland | 278afcb | 2008-11-13 13:22:34 -0600 | [diff] [blame] | 877 | if (!ls_count) |
David Teigland | 0f8e0d9 | 2008-08-06 13:30:24 -0500 | [diff] [blame] | 878 | threads_stop(); |
| 879 | mutex_unlock(&ls_lock); |
| 880 | |
| 881 | return error; |
David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 882 | } |
| 883 | |
David Teigland | dc68c7e | 2008-08-18 11:43:30 -0500 | [diff] [blame] | 884 | void dlm_stop_lockspaces(void) |
| 885 | { |
| 886 | struct dlm_ls *ls; |
| 887 | |
| 888 | restart: |
| 889 | spin_lock(&lslist_lock); |
| 890 | list_for_each_entry(ls, &lslist, ls_list) { |
| 891 | if (!test_bit(LSFL_RUNNING, &ls->ls_flags)) |
| 892 | continue; |
| 893 | spin_unlock(&lslist_lock); |
| 894 | log_error(ls, "no userland control daemon, stopping lockspace"); |
| 895 | dlm_ls_stop(ls); |
| 896 | goto restart; |
| 897 | } |
| 898 | spin_unlock(&lslist_lock); |
| 899 | } |
| 900 | |