blob: 871fe6deb5fa07dd8c47b6f44361249c1f255725 [file] [log] [blame]
David Teiglande7fd4172006-01-18 09:30:29 +00001/******************************************************************************
2*******************************************************************************
3**
4** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
David Teigland0f8e0d92008-08-06 13:30:24 -05005** Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
David Teiglande7fd4172006-01-18 09:30:29 +00006**
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"
18#include "ast.h"
19#include "dir.h"
20#include "lowcomms.h"
21#include "config.h"
22#include "memory.h"
23#include "lock.h"
David Teiglandc56b39c2006-04-28 10:51:53 -040024#include "recover.h"
David Teigland2896ee32006-11-27 11:31:22 -060025#include "requestqueue.h"
David Teigland0f8e0d92008-08-06 13:30:24 -050026#include "user.h"
David Teiglande7fd4172006-01-18 09:30:29 +000027
David Teiglande7fd4172006-01-18 09:30:29 +000028static int ls_count;
David Teigland90135922006-01-20 08:47:07 +000029static struct mutex ls_lock;
David Teiglande7fd4172006-01-18 09:30:29 +000030static struct list_head lslist;
31static spinlock_t lslist_lock;
32static struct task_struct * scand_task;
33
34
35static 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 Caulfielde2de7f52006-11-06 08:53:28 +000040 ls = dlm_find_lockspace_local(ls->ls_local_handle);
41 if (!ls)
42 return -EINVAL;
43
David Teiglande7fd4172006-01-18 09:30:29 +000044 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 Caulfielde2de7f52006-11-06 08:53:28 +000054 dlm_put_lockspace(ls);
David Teiglande7fd4172006-01-18 09:30:29 +000055 return ret;
56}
57
58static 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
66static ssize_t dlm_id_show(struct dlm_ls *ls, char *buf)
67{
David Teiglanda1d144c2006-09-06 17:01:40 -050068 return snprintf(buf, PAGE_SIZE, "%u\n", ls->ls_global_id);
David Teiglande7fd4172006-01-18 09:30:29 +000069}
70
71static 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 Teiglandc56b39c2006-04-28 10:51:53 -040077static ssize_t dlm_recover_status_show(struct dlm_ls *ls, char *buf)
78{
79 uint32_t status = dlm_recover_status(ls);
David Teiglanda1d144c2006-09-06 17:01:40 -050080 return snprintf(buf, PAGE_SIZE, "%x\n", status);
David Teiglandc56b39c2006-04-28 10:51:53 -040081}
82
David Teiglandfaa0f262006-08-08 17:08:42 -050083static ssize_t dlm_recover_nodeid_show(struct dlm_ls *ls, char *buf)
84{
David Teiglanda1d144c2006-09-06 17:01:40 -050085 return snprintf(buf, PAGE_SIZE, "%d\n", ls->ls_recover_nodeid);
David Teiglandfaa0f262006-08-08 17:08:42 -050086}
87
David Teiglande7fd4172006-01-18 09:30:29 +000088struct dlm_attr {
89 struct attribute attr;
90 ssize_t (*show)(struct dlm_ls *, char *);
91 ssize_t (*store)(struct dlm_ls *, const char *, size_t);
92};
93
94static struct dlm_attr dlm_attr_control = {
95 .attr = {.name = "control", .mode = S_IWUSR},
96 .store = dlm_control_store
97};
98
99static struct dlm_attr dlm_attr_event = {
100 .attr = {.name = "event_done", .mode = S_IWUSR},
101 .store = dlm_event_store
102};
103
104static struct dlm_attr dlm_attr_id = {
105 .attr = {.name = "id", .mode = S_IRUGO | S_IWUSR},
106 .show = dlm_id_show,
107 .store = dlm_id_store
108};
109
David Teiglandc56b39c2006-04-28 10:51:53 -0400110static struct dlm_attr dlm_attr_recover_status = {
111 .attr = {.name = "recover_status", .mode = S_IRUGO},
112 .show = dlm_recover_status_show
113};
114
David Teiglandfaa0f262006-08-08 17:08:42 -0500115static struct dlm_attr dlm_attr_recover_nodeid = {
116 .attr = {.name = "recover_nodeid", .mode = S_IRUGO},
117 .show = dlm_recover_nodeid_show
118};
119
David Teiglande7fd4172006-01-18 09:30:29 +0000120static struct attribute *dlm_attrs[] = {
121 &dlm_attr_control.attr,
122 &dlm_attr_event.attr,
123 &dlm_attr_id.attr,
David Teiglandc56b39c2006-04-28 10:51:53 -0400124 &dlm_attr_recover_status.attr,
David Teiglandfaa0f262006-08-08 17:08:42 -0500125 &dlm_attr_recover_nodeid.attr,
David Teiglande7fd4172006-01-18 09:30:29 +0000126 NULL,
127};
128
129static ssize_t dlm_attr_show(struct kobject *kobj, struct attribute *attr,
130 char *buf)
131{
132 struct dlm_ls *ls = container_of(kobj, struct dlm_ls, ls_kobj);
133 struct dlm_attr *a = container_of(attr, struct dlm_attr, attr);
134 return a->show ? a->show(ls, buf) : 0;
135}
136
137static ssize_t dlm_attr_store(struct kobject *kobj, struct attribute *attr,
138 const char *buf, size_t len)
139{
140 struct dlm_ls *ls = container_of(kobj, struct dlm_ls, ls_kobj);
141 struct dlm_attr *a = container_of(attr, struct dlm_attr, attr);
142 return a->store ? a->store(ls, buf, len) : len;
143}
144
Patrick Caulfieldba542e32006-11-02 14:41:23 +0000145static void lockspace_kobj_release(struct kobject *k)
146{
147 struct dlm_ls *ls = container_of(k, struct dlm_ls, ls_kobj);
148 kfree(ls);
149}
150
Emese Revfy52cf25d2010-01-19 02:58:23 +0100151static const struct sysfs_ops dlm_attr_ops = {
David Teiglande7fd4172006-01-18 09:30:29 +0000152 .show = dlm_attr_show,
153 .store = dlm_attr_store,
154};
155
156static struct kobj_type dlm_ktype = {
157 .default_attrs = dlm_attrs,
158 .sysfs_ops = &dlm_attr_ops,
Patrick Caulfieldba542e32006-11-02 14:41:23 +0000159 .release = lockspace_kobj_release,
David Teiglande7fd4172006-01-18 09:30:29 +0000160};
161
Greg Kroah-Hartmand4059362007-10-29 20:13:17 +0100162static struct kset *dlm_kset;
David Teiglande7fd4172006-01-18 09:30:29 +0000163
David Teiglande7fd4172006-01-18 09:30:29 +0000164static int do_uevent(struct dlm_ls *ls, int in)
165{
166 int error;
167
168 if (in)
169 kobject_uevent(&ls->ls_kobj, KOBJ_ONLINE);
170 else
171 kobject_uevent(&ls->ls_kobj, KOBJ_OFFLINE);
172
David Teigland8b0e7b22007-05-18 09:03:35 -0500173 log_debug(ls, "%s the lockspace group...", in ? "joining" : "leaving");
174
175 /* dlm_controld will see the uevent, do the necessary group management
176 and then write to sysfs to wake us */
177
David Teiglande7fd4172006-01-18 09:30:29 +0000178 error = wait_event_interruptible(ls->ls_uevent_wait,
179 test_and_clear_bit(LSFL_UEVENT_WAIT, &ls->ls_flags));
David Teigland8b0e7b22007-05-18 09:03:35 -0500180
181 log_debug(ls, "group event done %d %d", error, ls->ls_uevent_result);
182
David Teiglande7fd4172006-01-18 09:30:29 +0000183 if (error)
184 goto out;
185
186 error = ls->ls_uevent_result;
187 out:
David Teigland8b0e7b22007-05-18 09:03:35 -0500188 if (error)
189 log_error(ls, "group %s failed %d %d", in ? "join" : "leave",
190 error, ls->ls_uevent_result);
David Teiglande7fd4172006-01-18 09:30:29 +0000191 return error;
192}
193
Steven Whitehouseb4a5d4b2010-02-17 09:41:34 +0000194static int dlm_uevent(struct kset *kset, struct kobject *kobj,
195 struct kobj_uevent_env *env)
196{
197 struct dlm_ls *ls = container_of(kobj, struct dlm_ls, ls_kobj);
198
199 add_uevent_var(env, "LOCKSPACE=%s", ls->ls_name);
200 return 0;
201}
202
203static struct kset_uevent_ops dlm_uevent_ops = {
204 .uevent = dlm_uevent,
205};
David Teiglande7fd4172006-01-18 09:30:29 +0000206
Denis Cheng30727172008-02-02 01:53:46 +0800207int __init dlm_lockspace_init(void)
David Teiglande7fd4172006-01-18 09:30:29 +0000208{
David Teiglande7fd4172006-01-18 09:30:29 +0000209 ls_count = 0;
David Teigland90135922006-01-20 08:47:07 +0000210 mutex_init(&ls_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000211 INIT_LIST_HEAD(&lslist);
212 spin_lock_init(&lslist_lock);
213
Steven Whitehouseb4a5d4b2010-02-17 09:41:34 +0000214 dlm_kset = kset_create_and_add("dlm", &dlm_uevent_ops, kernel_kobj);
Greg Kroah-Hartmand4059362007-10-29 20:13:17 +0100215 if (!dlm_kset) {
Harvey Harrison8e24eea2008-04-30 00:55:09 -0700216 printk(KERN_WARNING "%s: can not create kset\n", __func__);
Greg Kroah-Hartmand4059362007-10-29 20:13:17 +0100217 return -ENOMEM;
218 }
219 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +0000220}
221
222void dlm_lockspace_exit(void)
223{
Greg Kroah-Hartmand4059362007-10-29 20:13:17 +0100224 kset_unregister(dlm_kset);
David Teiglande7fd4172006-01-18 09:30:29 +0000225}
226
David Teiglandc1dcf652008-08-18 14:03:25 -0500227static struct dlm_ls *find_ls_to_scan(void)
David Teiglande7fd4172006-01-18 09:30:29 +0000228{
229 struct dlm_ls *ls;
230
David Teiglandc1dcf652008-08-18 14:03:25 -0500231 spin_lock(&lslist_lock);
232 list_for_each_entry(ls, &lslist, ls_list) {
233 if (time_after_eq(jiffies, ls->ls_scan_time +
234 dlm_config.ci_scan_secs * HZ)) {
235 spin_unlock(&lslist_lock);
236 return ls;
237 }
238 }
239 spin_unlock(&lslist_lock);
240 return NULL;
241}
242
243static int dlm_scand(void *data)
244{
245 struct dlm_ls *ls;
David Teiglandc1dcf652008-08-18 14:03:25 -0500246
David Teiglande7fd4172006-01-18 09:30:29 +0000247 while (!kthread_should_stop()) {
David Teiglandc1dcf652008-08-18 14:03:25 -0500248 ls = find_ls_to_scan();
249 if (ls) {
David Teigland85e86ed2007-05-18 08:58:15 -0500250 if (dlm_lock_recovery_try(ls)) {
David Teiglandc1dcf652008-08-18 14:03:25 -0500251 ls->ls_scan_time = jiffies;
David Teigland85e86ed2007-05-18 08:58:15 -0500252 dlm_scan_rsbs(ls);
David Teigland3ae1acf2007-05-18 08:59:31 -0500253 dlm_scan_timeout(ls);
David Teiglandc6ff6692011-03-28 14:17:26 -0500254 dlm_scan_waiters(ls);
David Teigland85e86ed2007-05-18 08:58:15 -0500255 dlm_unlock_recovery(ls);
David Teiglandc1dcf652008-08-18 14:03:25 -0500256 } else {
257 ls->ls_scan_time += HZ;
David Teigland85e86ed2007-05-18 08:58:15 -0500258 }
David Teiglandc6ff6692011-03-28 14:17:26 -0500259 continue;
David Teigland85e86ed2007-05-18 08:58:15 -0500260 }
David Teiglandc6ff6692011-03-28 14:17:26 -0500261 schedule_timeout_interruptible(dlm_config.ci_scan_secs * HZ);
David Teiglande7fd4172006-01-18 09:30:29 +0000262 }
263 return 0;
264}
265
266static int dlm_scand_start(void)
267{
268 struct task_struct *p;
269 int error = 0;
270
271 p = kthread_run(dlm_scand, NULL, "dlm_scand");
272 if (IS_ERR(p))
273 error = PTR_ERR(p);
274 else
275 scand_task = p;
276 return error;
277}
278
279static void dlm_scand_stop(void)
280{
281 kthread_stop(scand_task);
282}
283
David Teiglande7fd4172006-01-18 09:30:29 +0000284struct dlm_ls *dlm_find_lockspace_global(uint32_t id)
285{
286 struct dlm_ls *ls;
287
288 spin_lock(&lslist_lock);
289
290 list_for_each_entry(ls, &lslist, ls_list) {
291 if (ls->ls_global_id == id) {
292 ls->ls_count++;
293 goto out;
294 }
295 }
296 ls = NULL;
297 out:
298 spin_unlock(&lslist_lock);
299 return ls;
300}
301
David Teigland597d0ca2006-07-12 16:44:04 -0500302struct dlm_ls *dlm_find_lockspace_local(dlm_lockspace_t *lockspace)
David Teiglande7fd4172006-01-18 09:30:29 +0000303{
David Teigland597d0ca2006-07-12 16:44:04 -0500304 struct dlm_ls *ls;
David Teiglande7fd4172006-01-18 09:30:29 +0000305
306 spin_lock(&lslist_lock);
David Teigland597d0ca2006-07-12 16:44:04 -0500307 list_for_each_entry(ls, &lslist, ls_list) {
308 if (ls->ls_local_handle == lockspace) {
309 ls->ls_count++;
310 goto out;
311 }
312 }
313 ls = NULL;
314 out:
315 spin_unlock(&lslist_lock);
316 return ls;
317}
318
319struct dlm_ls *dlm_find_lockspace_device(int minor)
320{
321 struct dlm_ls *ls;
322
323 spin_lock(&lslist_lock);
324 list_for_each_entry(ls, &lslist, ls_list) {
325 if (ls->ls_device.minor == minor) {
326 ls->ls_count++;
327 goto out;
328 }
329 }
330 ls = NULL;
331 out:
David Teiglande7fd4172006-01-18 09:30:29 +0000332 spin_unlock(&lslist_lock);
333 return ls;
334}
335
336void dlm_put_lockspace(struct dlm_ls *ls)
337{
338 spin_lock(&lslist_lock);
339 ls->ls_count--;
340 spin_unlock(&lslist_lock);
341}
342
343static void remove_lockspace(struct dlm_ls *ls)
344{
345 for (;;) {
346 spin_lock(&lslist_lock);
347 if (ls->ls_count == 0) {
David Teigland0f8e0d92008-08-06 13:30:24 -0500348 WARN_ON(ls->ls_create_count != 0);
David Teiglande7fd4172006-01-18 09:30:29 +0000349 list_del(&ls->ls_list);
350 spin_unlock(&lslist_lock);
351 return;
352 }
353 spin_unlock(&lslist_lock);
354 ssleep(1);
355 }
356}
357
358static int threads_start(void)
359{
360 int error;
361
362 /* Thread which process lock requests for all lockspace's */
363 error = dlm_astd_start();
364 if (error) {
365 log_print("cannot start dlm_astd thread %d", error);
366 goto fail;
367 }
368
369 error = dlm_scand_start();
370 if (error) {
371 log_print("cannot start dlm_scand thread %d", error);
372 goto astd_fail;
373 }
374
375 /* Thread for sending/receiving messages for all lockspace's */
376 error = dlm_lowcomms_start();
377 if (error) {
378 log_print("cannot start dlm lowcomms %d", error);
379 goto scand_fail;
380 }
381
382 return 0;
383
384 scand_fail:
385 dlm_scand_stop();
386 astd_fail:
387 dlm_astd_stop();
388 fail:
389 return error;
390}
391
392static void threads_stop(void)
393{
394 dlm_scand_stop();
395 dlm_lowcomms_stop();
396 dlm_astd_stop();
397}
398
Geert Uytterhoeven08ce4c92009-04-07 23:40:39 +0200399static int new_lockspace(const char *name, int namelen, void **lockspace,
David Teiglande7fd4172006-01-18 09:30:29 +0000400 uint32_t flags, int lvblen)
401{
402 struct dlm_ls *ls;
David Teigland0f8e0d92008-08-06 13:30:24 -0500403 int i, size, error;
David Teigland79d72b52007-05-18 09:02:20 -0500404 int do_unreg = 0;
David Teiglande7fd4172006-01-18 09:30:29 +0000405
406 if (namelen > DLM_LOCKSPACE_LEN)
407 return -EINVAL;
408
409 if (!lvblen || (lvblen % 8))
410 return -EINVAL;
411
412 if (!try_module_get(THIS_MODULE))
413 return -EINVAL;
414
David Teiglanddc68c7e2008-08-18 11:43:30 -0500415 if (!dlm_user_daemon_available()) {
416 module_put(THIS_MODULE);
417 return -EUNATCH;
418 }
419
David Teigland0f8e0d92008-08-06 13:30:24 -0500420 error = 0;
421
422 spin_lock(&lslist_lock);
423 list_for_each_entry(ls, &lslist, ls_list) {
424 WARN_ON(ls->ls_create_count <= 0);
425 if (ls->ls_namelen != namelen)
426 continue;
427 if (memcmp(ls->ls_name, name, namelen))
428 continue;
429 if (flags & DLM_LSFL_NEWEXCL) {
430 error = -EEXIST;
431 break;
432 }
433 ls->ls_create_count++;
David Teigland8511a272009-04-08 15:38:43 -0500434 *lockspace = ls;
435 error = 1;
David Teigland0f8e0d92008-08-06 13:30:24 -0500436 break;
David Teiglande7fd4172006-01-18 09:30:29 +0000437 }
David Teigland0f8e0d92008-08-06 13:30:24 -0500438 spin_unlock(&lslist_lock);
439
David Teigland0f8e0d92008-08-06 13:30:24 -0500440 if (error)
David Teigland8511a272009-04-08 15:38:43 -0500441 goto out;
David Teigland0f8e0d92008-08-06 13:30:24 -0500442
443 error = -ENOMEM;
David Teiglande7fd4172006-01-18 09:30:29 +0000444
David Teigland573c24c2009-11-30 16:34:43 -0600445 ls = kzalloc(sizeof(struct dlm_ls) + namelen, GFP_NOFS);
David Teiglande7fd4172006-01-18 09:30:29 +0000446 if (!ls)
447 goto out;
David Teiglande7fd4172006-01-18 09:30:29 +0000448 memcpy(ls->ls_name, name, namelen);
449 ls->ls_namelen = namelen;
David Teiglande7fd4172006-01-18 09:30:29 +0000450 ls->ls_lvblen = lvblen;
451 ls->ls_count = 0;
452 ls->ls_flags = 0;
David Teiglandc1dcf652008-08-18 14:03:25 -0500453 ls->ls_scan_time = jiffies;
David Teiglande7fd4172006-01-18 09:30:29 +0000454
David Teigland3ae1acf2007-05-18 08:59:31 -0500455 if (flags & DLM_LSFL_TIMEWARN)
456 set_bit(LSFL_TIMEWARN, &ls->ls_flags);
David Teigland3ae1acf2007-05-18 08:59:31 -0500457
David Teiglandfad59c12007-06-11 10:47:18 -0500458 /* ls_exflags are forced to match among nodes, and we don't
David Teigland0f8e0d92008-08-06 13:30:24 -0500459 need to require all nodes to have some flags set */
460 ls->ls_exflags = (flags & ~(DLM_LSFL_TIMEWARN | DLM_LSFL_FS |
461 DLM_LSFL_NEWEXCL));
David Teiglandfad59c12007-06-11 10:47:18 -0500462
David Teigland68c817a2007-01-09 09:41:48 -0600463 size = dlm_config.ci_rsbtbl_size;
David Teiglande7fd4172006-01-18 09:30:29 +0000464 ls->ls_rsbtbl_size = size;
465
Bryn M. Reevesc282af42011-07-01 15:49:23 -0500466 ls->ls_rsbtbl = vmalloc(sizeof(struct dlm_rsbtable) * size);
David Teiglande7fd4172006-01-18 09:30:29 +0000467 if (!ls->ls_rsbtbl)
468 goto out_lsfree;
469 for (i = 0; i < size; i++) {
470 INIT_LIST_HEAD(&ls->ls_rsbtbl[i].list);
471 INIT_LIST_HEAD(&ls->ls_rsbtbl[i].toss);
David Teiglandc7be7612009-01-07 16:50:41 -0600472 spin_lock_init(&ls->ls_rsbtbl[i].lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000473 }
474
David Teigland3d6aa672011-07-06 17:00:54 -0500475 idr_init(&ls->ls_lkbidr);
476 spin_lock_init(&ls->ls_lkbidr_spin);
David Teiglande7fd4172006-01-18 09:30:29 +0000477
David Teigland68c817a2007-01-09 09:41:48 -0600478 size = dlm_config.ci_dirtbl_size;
David Teiglande7fd4172006-01-18 09:30:29 +0000479 ls->ls_dirtbl_size = size;
480
Bryn M. Reevesc282af42011-07-01 15:49:23 -0500481 ls->ls_dirtbl = vmalloc(sizeof(struct dlm_dirtable) * size);
David Teiglande7fd4172006-01-18 09:30:29 +0000482 if (!ls->ls_dirtbl)
483 goto out_lkbfree;
484 for (i = 0; i < size; i++) {
485 INIT_LIST_HEAD(&ls->ls_dirtbl[i].list);
Steven Whitehouse305a47b2009-01-16 16:21:12 +0000486 spin_lock_init(&ls->ls_dirtbl[i].lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000487 }
488
489 INIT_LIST_HEAD(&ls->ls_waiters);
David Teigland90135922006-01-20 08:47:07 +0000490 mutex_init(&ls->ls_waiters_mutex);
David Teiglandef0c2bb2007-03-28 09:56:46 -0500491 INIT_LIST_HEAD(&ls->ls_orphans);
492 mutex_init(&ls->ls_orphans_mutex);
David Teigland3ae1acf2007-05-18 08:59:31 -0500493 INIT_LIST_HEAD(&ls->ls_timeout);
494 mutex_init(&ls->ls_timeout_mutex);
David Teiglande7fd4172006-01-18 09:30:29 +0000495
496 INIT_LIST_HEAD(&ls->ls_nodes);
497 INIT_LIST_HEAD(&ls->ls_nodes_gone);
498 ls->ls_num_nodes = 0;
499 ls->ls_low_nodeid = 0;
500 ls->ls_total_weight = 0;
501 ls->ls_node_array = NULL;
502
503 memset(&ls->ls_stub_rsb, 0, sizeof(struct dlm_rsb));
504 ls->ls_stub_rsb.res_ls = ls;
505
David Teigland5de63192006-07-25 13:44:31 -0500506 ls->ls_debug_rsb_dentry = NULL;
507 ls->ls_debug_waiters_dentry = NULL;
David Teiglande7fd4172006-01-18 09:30:29 +0000508
509 init_waitqueue_head(&ls->ls_uevent_wait);
510 ls->ls_uevent_result = 0;
David Teigland8b0e7b22007-05-18 09:03:35 -0500511 init_completion(&ls->ls_members_done);
512 ls->ls_members_result = -1;
David Teiglande7fd4172006-01-18 09:30:29 +0000513
514 ls->ls_recoverd_task = NULL;
David Teigland90135922006-01-20 08:47:07 +0000515 mutex_init(&ls->ls_recoverd_active);
David Teiglande7fd4172006-01-18 09:30:29 +0000516 spin_lock_init(&ls->ls_recover_lock);
David Teigland98f176f2006-11-27 13:19:28 -0600517 spin_lock_init(&ls->ls_rcom_spin);
518 get_random_bytes(&ls->ls_rcom_seq, sizeof(uint64_t));
David Teiglande7fd4172006-01-18 09:30:29 +0000519 ls->ls_recover_status = 0;
520 ls->ls_recover_seq = 0;
521 ls->ls_recover_args = NULL;
522 init_rwsem(&ls->ls_in_recovery);
David Teiglandc36258b2007-09-27 15:53:38 -0500523 init_rwsem(&ls->ls_recv_active);
David Teiglande7fd4172006-01-18 09:30:29 +0000524 INIT_LIST_HEAD(&ls->ls_requestqueue);
David Teigland90135922006-01-20 08:47:07 +0000525 mutex_init(&ls->ls_requestqueue_mutex);
David Teigland597d0ca2006-07-12 16:44:04 -0500526 mutex_init(&ls->ls_clear_proc_locks);
David Teiglande7fd4172006-01-18 09:30:29 +0000527
David Teigland573c24c2009-11-30 16:34:43 -0600528 ls->ls_recover_buf = kmalloc(dlm_config.ci_buffer_size, GFP_NOFS);
David Teiglande7fd4172006-01-18 09:30:29 +0000529 if (!ls->ls_recover_buf)
530 goto out_dirfree;
531
532 INIT_LIST_HEAD(&ls->ls_recover_list);
533 spin_lock_init(&ls->ls_recover_list_lock);
534 ls->ls_recover_list_count = 0;
David Teigland597d0ca2006-07-12 16:44:04 -0500535 ls->ls_local_handle = ls;
David Teiglande7fd4172006-01-18 09:30:29 +0000536 init_waitqueue_head(&ls->ls_wait_general);
537 INIT_LIST_HEAD(&ls->ls_root_list);
538 init_rwsem(&ls->ls_root_sem);
539
540 down_write(&ls->ls_in_recovery);
541
David Teigland5f88f1e2006-08-24 14:47:20 -0500542 spin_lock(&lslist_lock);
David Teigland0f8e0d92008-08-06 13:30:24 -0500543 ls->ls_create_count = 1;
David Teigland5f88f1e2006-08-24 14:47:20 -0500544 list_add(&ls->ls_list, &lslist);
545 spin_unlock(&lslist_lock);
546
547 /* needs to find ls in lslist */
David Teiglande7fd4172006-01-18 09:30:29 +0000548 error = dlm_recoverd_start(ls);
549 if (error) {
550 log_error(ls, "can't start dlm_recoverd %d", error);
David Teigland79d72b52007-05-18 09:02:20 -0500551 goto out_delist;
David Teiglande7fd4172006-01-18 09:30:29 +0000552 }
553
Greg Kroah-Hartman901195e2007-12-17 15:54:39 -0400554 ls->ls_kobj.kset = dlm_kset;
555 error = kobject_init_and_add(&ls->ls_kobj, &dlm_ktype, NULL,
556 "%s", ls->ls_name);
David Teiglande7fd4172006-01-18 09:30:29 +0000557 if (error)
David Teigland79d72b52007-05-18 09:02:20 -0500558 goto out_stop;
Greg Kroah-Hartman901195e2007-12-17 15:54:39 -0400559 kobject_uevent(&ls->ls_kobj, KOBJ_ADD);
David Teigland79d72b52007-05-18 09:02:20 -0500560
561 /* let kobject handle freeing of ls if there's an error */
562 do_unreg = 1;
David Teiglande7fd4172006-01-18 09:30:29 +0000563
David Teigland8b0e7b22007-05-18 09:03:35 -0500564 /* This uevent triggers dlm_controld in userspace to add us to the
565 group of nodes that are members of this lockspace (managed by the
566 cluster infrastructure.) Once it's done that, it tells us who the
567 current lockspace members are (via configfs) and then tells the
568 lockspace to start running (via sysfs) in dlm_ls_start(). */
569
David Teiglande7fd4172006-01-18 09:30:29 +0000570 error = do_uevent(ls, 1);
571 if (error)
David Teigland79d72b52007-05-18 09:02:20 -0500572 goto out_stop;
573
David Teigland8b0e7b22007-05-18 09:03:35 -0500574 wait_for_completion(&ls->ls_members_done);
575 error = ls->ls_members_result;
576 if (error)
577 goto out_members;
578
David Teigland79d72b52007-05-18 09:02:20 -0500579 dlm_create_debug_file(ls);
580
581 log_debug(ls, "join complete");
David Teiglande7fd4172006-01-18 09:30:29 +0000582 *lockspace = ls;
583 return 0;
584
David Teigland8b0e7b22007-05-18 09:03:35 -0500585 out_members:
586 do_uevent(ls, 0);
587 dlm_clear_members(ls);
588 kfree(ls->ls_node_array);
David Teigland79d72b52007-05-18 09:02:20 -0500589 out_stop:
David Teigland5f88f1e2006-08-24 14:47:20 -0500590 dlm_recoverd_stop(ls);
David Teigland79d72b52007-05-18 09:02:20 -0500591 out_delist:
David Teiglande7fd4172006-01-18 09:30:29 +0000592 spin_lock(&lslist_lock);
593 list_del(&ls->ls_list);
594 spin_unlock(&lslist_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000595 kfree(ls->ls_recover_buf);
596 out_dirfree:
Bryn M. Reevesc282af42011-07-01 15:49:23 -0500597 vfree(ls->ls_dirtbl);
David Teiglande7fd4172006-01-18 09:30:29 +0000598 out_lkbfree:
David Teigland3d6aa672011-07-06 17:00:54 -0500599 idr_destroy(&ls->ls_lkbidr);
Bryn M. Reevesc282af42011-07-01 15:49:23 -0500600 vfree(ls->ls_rsbtbl);
David Teiglande7fd4172006-01-18 09:30:29 +0000601 out_lsfree:
David Teigland79d72b52007-05-18 09:02:20 -0500602 if (do_unreg)
Greg Kroah-Hartman197b12d2007-12-20 08:13:05 -0800603 kobject_put(&ls->ls_kobj);
David Teigland79d72b52007-05-18 09:02:20 -0500604 else
605 kfree(ls);
David Teiglande7fd4172006-01-18 09:30:29 +0000606 out:
607 module_put(THIS_MODULE);
608 return error;
609}
610
Geert Uytterhoeven08ce4c92009-04-07 23:40:39 +0200611int dlm_new_lockspace(const char *name, int namelen, void **lockspace,
David Teiglande7fd4172006-01-18 09:30:29 +0000612 uint32_t flags, int lvblen)
613{
614 int error = 0;
615
David Teigland90135922006-01-20 08:47:07 +0000616 mutex_lock(&ls_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000617 if (!ls_count)
618 error = threads_start();
619 if (error)
620 goto out;
621
622 error = new_lockspace(name, namelen, lockspace, flags, lvblen);
623 if (!error)
624 ls_count++;
David Teigland8511a272009-04-08 15:38:43 -0500625 if (error > 0)
626 error = 0;
627 if (!ls_count)
David Teigland8b0e7b22007-05-18 09:03:35 -0500628 threads_stop();
David Teiglande7fd4172006-01-18 09:30:29 +0000629 out:
David Teigland90135922006-01-20 08:47:07 +0000630 mutex_unlock(&ls_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000631 return error;
632}
633
David Teigland3d6aa672011-07-06 17:00:54 -0500634static int lkb_idr_is_local(int id, void *p, void *data)
David Teiglande7fd4172006-01-18 09:30:29 +0000635{
David Teigland3d6aa672011-07-06 17:00:54 -0500636 struct dlm_lkb *lkb = p;
David Teiglande7fd4172006-01-18 09:30:29 +0000637
David Teigland3d6aa672011-07-06 17:00:54 -0500638 if (!lkb->lkb_nodeid)
639 return 1;
640 return 0;
641}
David Teiglande7fd4172006-01-18 09:30:29 +0000642
David Teigland3d6aa672011-07-06 17:00:54 -0500643static int lkb_idr_is_any(int id, void *p, void *data)
644{
645 return 1;
646}
647
648static int lkb_idr_free(int id, void *p, void *data)
649{
650 struct dlm_lkb *lkb = p;
651
652 dlm_del_ast(lkb);
653
654 if (lkb->lkb_lvbptr && lkb->lkb_flags & DLM_IFL_MSTCPY)
655 dlm_free_lvb(lkb->lkb_lvbptr);
656
657 dlm_free_lkb(lkb);
658 return 0;
659}
660
661/* NOTE: We check the lkbidr here rather than the resource table.
662 This is because there may be LKBs queued as ASTs that have been unlinked
663 from their RSBs and are pending deletion once the AST has been delivered */
664
665static int lockspace_busy(struct dlm_ls *ls, int force)
666{
667 int rv;
668
669 spin_lock(&ls->ls_lkbidr_spin);
670 if (force == 0) {
671 rv = idr_for_each(&ls->ls_lkbidr, lkb_idr_is_any, ls);
672 } else if (force == 1) {
673 rv = idr_for_each(&ls->ls_lkbidr, lkb_idr_is_local, ls);
674 } else {
675 rv = 0;
David Teiglande7fd4172006-01-18 09:30:29 +0000676 }
David Teigland3d6aa672011-07-06 17:00:54 -0500677 spin_unlock(&ls->ls_lkbidr_spin);
678 return rv;
David Teiglande7fd4172006-01-18 09:30:29 +0000679}
680
681static int release_lockspace(struct dlm_ls *ls, int force)
682{
David Teiglande7fd4172006-01-18 09:30:29 +0000683 struct dlm_rsb *rsb;
684 struct list_head *head;
David Teigland0f8e0d92008-08-06 13:30:24 -0500685 int i, busy, rv;
David Teiglande7fd4172006-01-18 09:30:29 +0000686
David Teigland3d6aa672011-07-06 17:00:54 -0500687 busy = lockspace_busy(ls, force);
David Teigland0f8e0d92008-08-06 13:30:24 -0500688
689 spin_lock(&lslist_lock);
690 if (ls->ls_create_count == 1) {
David Teigland3d6aa672011-07-06 17:00:54 -0500691 if (busy) {
David Teigland0f8e0d92008-08-06 13:30:24 -0500692 rv = -EBUSY;
David Teigland3d6aa672011-07-06 17:00:54 -0500693 } else {
David Teigland0f8e0d92008-08-06 13:30:24 -0500694 /* remove_lockspace takes ls off lslist */
695 ls->ls_create_count = 0;
696 rv = 0;
697 }
698 } else if (ls->ls_create_count > 1) {
699 rv = --ls->ls_create_count;
700 } else {
701 rv = -EINVAL;
702 }
703 spin_unlock(&lslist_lock);
704
705 if (rv) {
706 log_debug(ls, "release_lockspace no remove %d", rv);
707 return rv;
708 }
709
710 dlm_device_deregister(ls);
David Teiglande7fd4172006-01-18 09:30:29 +0000711
David Teiglanddc68c7e2008-08-18 11:43:30 -0500712 if (force < 3 && dlm_user_daemon_available())
David Teiglande7fd4172006-01-18 09:30:29 +0000713 do_uevent(ls, 0);
714
715 dlm_recoverd_stop(ls);
716
717 remove_lockspace(ls);
718
719 dlm_delete_debug_file(ls);
720
721 dlm_astd_suspend();
722
723 kfree(ls->ls_recover_buf);
724
725 /*
726 * Free direntry structs.
727 */
728
729 dlm_dir_clear(ls);
Bryn M. Reevesc282af42011-07-01 15:49:23 -0500730 vfree(ls->ls_dirtbl);
David Teiglande7fd4172006-01-18 09:30:29 +0000731
732 /*
David Teigland3d6aa672011-07-06 17:00:54 -0500733 * Free all lkb's in idr
David Teiglande7fd4172006-01-18 09:30:29 +0000734 */
735
David Teigland3d6aa672011-07-06 17:00:54 -0500736 idr_for_each(&ls->ls_lkbidr, lkb_idr_free, ls);
737 idr_remove_all(&ls->ls_lkbidr);
738 idr_destroy(&ls->ls_lkbidr);
David Teiglande7fd4172006-01-18 09:30:29 +0000739
David Teiglande7fd4172006-01-18 09:30:29 +0000740 dlm_astd_resume();
741
David Teiglande7fd4172006-01-18 09:30:29 +0000742 /*
743 * Free all rsb's on rsbtbl[] lists
744 */
745
746 for (i = 0; i < ls->ls_rsbtbl_size; i++) {
747 head = &ls->ls_rsbtbl[i].list;
748 while (!list_empty(head)) {
749 rsb = list_entry(head->next, struct dlm_rsb,
750 res_hashchain);
751
752 list_del(&rsb->res_hashchain);
David Teigland52bda2b2007-11-07 09:06:49 -0600753 dlm_free_rsb(rsb);
David Teiglande7fd4172006-01-18 09:30:29 +0000754 }
755
756 head = &ls->ls_rsbtbl[i].toss;
757 while (!list_empty(head)) {
758 rsb = list_entry(head->next, struct dlm_rsb,
759 res_hashchain);
760 list_del(&rsb->res_hashchain);
David Teigland52bda2b2007-11-07 09:06:49 -0600761 dlm_free_rsb(rsb);
David Teiglande7fd4172006-01-18 09:30:29 +0000762 }
763 }
764
Bryn M. Reevesc282af42011-07-01 15:49:23 -0500765 vfree(ls->ls_rsbtbl);
David Teiglande7fd4172006-01-18 09:30:29 +0000766
767 /*
768 * Free structures on any other lists
769 */
770
David Teigland2896ee32006-11-27 11:31:22 -0600771 dlm_purge_requestqueue(ls);
David Teiglande7fd4172006-01-18 09:30:29 +0000772 kfree(ls->ls_recover_args);
773 dlm_clear_free_entries(ls);
774 dlm_clear_members(ls);
775 dlm_clear_members_gone(ls);
776 kfree(ls->ls_node_array);
David Teigland0f8e0d92008-08-06 13:30:24 -0500777 log_debug(ls, "release_lockspace final free");
Greg Kroah-Hartman197b12d2007-12-20 08:13:05 -0800778 kobject_put(&ls->ls_kobj);
David Teigland79d72b52007-05-18 09:02:20 -0500779 /* The ls structure will be freed when the kobject is done with */
David Teiglande7fd4172006-01-18 09:30:29 +0000780
David Teiglande7fd4172006-01-18 09:30:29 +0000781 module_put(THIS_MODULE);
782 return 0;
783}
784
785/*
786 * Called when a system has released all its locks and is not going to use the
787 * lockspace any longer. We free everything we're managing for this lockspace.
788 * Remaining nodes will go through the recovery process as if we'd died. The
789 * lockspace must continue to function as usual, participating in recoveries,
790 * until this returns.
791 *
792 * Force has 4 possible values:
793 * 0 - don't destroy locksapce if it has any LKBs
794 * 1 - destroy lockspace if it has remote LKBs but not if it has local LKBs
795 * 2 - destroy lockspace regardless of LKBs
796 * 3 - destroy lockspace as part of a forced shutdown
797 */
798
799int dlm_release_lockspace(void *lockspace, int force)
800{
801 struct dlm_ls *ls;
David Teigland0f8e0d92008-08-06 13:30:24 -0500802 int error;
David Teiglande7fd4172006-01-18 09:30:29 +0000803
804 ls = dlm_find_lockspace_local(lockspace);
805 if (!ls)
806 return -EINVAL;
807 dlm_put_lockspace(ls);
David Teigland0f8e0d92008-08-06 13:30:24 -0500808
809 mutex_lock(&ls_lock);
810 error = release_lockspace(ls, force);
811 if (!error)
812 ls_count--;
David Teigland278afcb2008-11-13 13:22:34 -0600813 if (!ls_count)
David Teigland0f8e0d92008-08-06 13:30:24 -0500814 threads_stop();
815 mutex_unlock(&ls_lock);
816
817 return error;
David Teiglande7fd4172006-01-18 09:30:29 +0000818}
819
David Teiglanddc68c7e2008-08-18 11:43:30 -0500820void dlm_stop_lockspaces(void)
821{
822 struct dlm_ls *ls;
823
824 restart:
825 spin_lock(&lslist_lock);
826 list_for_each_entry(ls, &lslist, ls_list) {
827 if (!test_bit(LSFL_RUNNING, &ls->ls_flags))
828 continue;
829 spin_unlock(&lslist_lock);
830 log_error(ls, "no userland control daemon, stopping lockspace");
831 dlm_ls_stop(ls);
832 goto restart;
833 }
834 spin_unlock(&lslist_lock);
835}
836