dlm: add node slots and generation
[linux-block.git] / fs / dlm / lockspace.c
CommitLineData
e7fd4179
DT
1/******************************************************************************
2*******************************************************************************
3**
4** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
0f8e0d9a 5** Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
e7fd4179
DT
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"
e7fd4179
DT
18#include "dir.h"
19#include "lowcomms.h"
20#include "config.h"
21#include "memory.h"
22#include "lock.h"
c56b39cd 23#include "recover.h"
2896ee37 24#include "requestqueue.h"
0f8e0d9a 25#include "user.h"
23e8e1aa 26#include "ast.h"
e7fd4179 27
e7fd4179 28static int ls_count;
90135925 29static struct mutex ls_lock;
e7fd4179
DT
30static 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
e2de7f56
PC
40 ls = dlm_find_lockspace_local(ls->ls_local_handle);
41 if (!ls)
42 return -EINVAL;
43
e7fd4179
DT
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 }
e2de7f56 54 dlm_put_lockspace(ls);
e7fd4179
DT
55 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{
a1d144c7 68 return snprintf(buf, PAGE_SIZE, "%u\n", ls->ls_global_id);
e7fd4179
DT
69}
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
c56b39cd
DT
77static ssize_t dlm_recover_status_show(struct dlm_ls *ls, char *buf)
78{
79 uint32_t status = dlm_recover_status(ls);
a1d144c7 80 return snprintf(buf, PAGE_SIZE, "%x\n", status);
c56b39cd
DT
81}
82
faa0f267
DT
83static ssize_t dlm_recover_nodeid_show(struct dlm_ls *ls, char *buf)
84{
a1d144c7 85 return snprintf(buf, PAGE_SIZE, "%d\n", ls->ls_recover_nodeid);
faa0f267
DT
86}
87
e7fd4179
DT
88struct 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
c56b39cd
DT
110static struct dlm_attr dlm_attr_recover_status = {
111 .attr = {.name = "recover_status", .mode = S_IRUGO},
112 .show = dlm_recover_status_show
113};
114
faa0f267
DT
115static struct dlm_attr dlm_attr_recover_nodeid = {
116 .attr = {.name = "recover_nodeid", .mode = S_IRUGO},
117 .show = dlm_recover_nodeid_show
118};
119
e7fd4179
DT
120static struct attribute *dlm_attrs[] = {
121 &dlm_attr_control.attr,
122 &dlm_attr_event.attr,
123 &dlm_attr_id.attr,
c56b39cd 124 &dlm_attr_recover_status.attr,
faa0f267 125 &dlm_attr_recover_nodeid.attr,
e7fd4179
DT
126 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
ba542e3b
PC
145static 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
52cf25d0 151static const struct sysfs_ops dlm_attr_ops = {
e7fd4179
DT
152 .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,
ba542e3b 159 .release = lockspace_kobj_release,
e7fd4179
DT
160};
161
d405936b 162static struct kset *dlm_kset;
e7fd4179 163
e7fd4179
DT
164static 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
8b0e7b2c
DT
173 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
e7fd4179
DT
178 error = wait_event_interruptible(ls->ls_uevent_wait,
179 test_and_clear_bit(LSFL_UEVENT_WAIT, &ls->ls_flags));
8b0e7b2c
DT
180
181 log_debug(ls, "group event done %d %d", error, ls->ls_uevent_result);
182
e7fd4179
DT
183 if (error)
184 goto out;
185
186 error = ls->ls_uevent_result;
187 out:
8b0e7b2c
DT
188 if (error)
189 log_error(ls, "group %s failed %d %d", in ? "join" : "leave",
190 error, ls->ls_uevent_result);
e7fd4179
DT
191 return error;
192}
193
b4a5d4bc
SW
194static 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};
e7fd4179 206
30727174 207int __init dlm_lockspace_init(void)
e7fd4179 208{
e7fd4179 209 ls_count = 0;
90135925 210 mutex_init(&ls_lock);
e7fd4179
DT
211 INIT_LIST_HEAD(&lslist);
212 spin_lock_init(&lslist_lock);
213
b4a5d4bc 214 dlm_kset = kset_create_and_add("dlm", &dlm_uevent_ops, kernel_kobj);
d405936b 215 if (!dlm_kset) {
8e24eea7 216 printk(KERN_WARNING "%s: can not create kset\n", __func__);
d405936b
GKH
217 return -ENOMEM;
218 }
219 return 0;
e7fd4179
DT
220}
221
222void dlm_lockspace_exit(void)
223{
d405936b 224 kset_unregister(dlm_kset);
e7fd4179
DT
225}
226
c1dcf65f
DT
227static struct dlm_ls *find_ls_to_scan(void)
228{
229 struct dlm_ls *ls;
230
231 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
e7fd4179
DT
243static int dlm_scand(void *data)
244{
245 struct dlm_ls *ls;
246
247 while (!kthread_should_stop()) {
c1dcf65f
DT
248 ls = find_ls_to_scan();
249 if (ls) {
85e86edf 250 if (dlm_lock_recovery_try(ls)) {
c1dcf65f 251 ls->ls_scan_time = jiffies;
85e86edf 252 dlm_scan_rsbs(ls);
3ae1acf9 253 dlm_scan_timeout(ls);
c6ff669b 254 dlm_scan_waiters(ls);
85e86edf 255 dlm_unlock_recovery(ls);
c1dcf65f
DT
256 } else {
257 ls->ls_scan_time += HZ;
85e86edf 258 }
c6ff669b 259 continue;
85e86edf 260 }
c6ff669b 261 schedule_timeout_interruptible(dlm_config.ci_scan_secs * HZ);
e7fd4179
DT
262 }
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
e7fd4179
DT
284struct 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
597d0cae 302struct dlm_ls *dlm_find_lockspace_local(dlm_lockspace_t *lockspace)
e7fd4179 303{
597d0cae 304 struct dlm_ls *ls;
e7fd4179
DT
305
306 spin_lock(&lslist_lock);
597d0cae
DT
307 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:
e7fd4179
DT
332 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) {
0f8e0d9a 348 WARN_ON(ls->ls_create_count != 0);
e7fd4179
DT
349 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
e7fd4179
DT
362 error = dlm_scand_start();
363 if (error) {
364 log_print("cannot start dlm_scand thread %d", error);
23e8e1aa 365 goto fail;
e7fd4179
DT
366 }
367
368 /* Thread for sending/receiving messages for all lockspace's */
369 error = dlm_lowcomms_start();
370 if (error) {
371 log_print("cannot start dlm lowcomms %d", error);
372 goto scand_fail;
373 }
374
375 return 0;
376
377 scand_fail:
378 dlm_scand_stop();
e7fd4179
DT
379 fail:
380 return error;
381}
382
383static void threads_stop(void)
384{
385 dlm_scand_stop();
386 dlm_lowcomms_stop();
e7fd4179
DT
387}
388
08ce4c91 389static int new_lockspace(const char *name, int namelen, void **lockspace,
e7fd4179
DT
390 uint32_t flags, int lvblen)
391{
392 struct dlm_ls *ls;
0f8e0d9a 393 int i, size, error;
79d72b54 394 int do_unreg = 0;
e7fd4179
DT
395
396 if (namelen > DLM_LOCKSPACE_LEN)
397 return -EINVAL;
398
399 if (!lvblen || (lvblen % 8))
400 return -EINVAL;
401
402 if (!try_module_get(THIS_MODULE))
403 return -EINVAL;
404
dc68c7ed
DT
405 if (!dlm_user_daemon_available()) {
406 module_put(THIS_MODULE);
407 return -EUNATCH;
408 }
409
0f8e0d9a
DT
410 error = 0;
411
412 spin_lock(&lslist_lock);
413 list_for_each_entry(ls, &lslist, ls_list) {
414 WARN_ON(ls->ls_create_count <= 0);
415 if (ls->ls_namelen != namelen)
416 continue;
417 if (memcmp(ls->ls_name, name, namelen))
418 continue;
419 if (flags & DLM_LSFL_NEWEXCL) {
420 error = -EEXIST;
421 break;
422 }
423 ls->ls_create_count++;
8511a272
DT
424 *lockspace = ls;
425 error = 1;
0f8e0d9a 426 break;
e7fd4179 427 }
0f8e0d9a
DT
428 spin_unlock(&lslist_lock);
429
0f8e0d9a 430 if (error)
8511a272 431 goto out;
0f8e0d9a
DT
432
433 error = -ENOMEM;
e7fd4179 434
573c24c4 435 ls = kzalloc(sizeof(struct dlm_ls) + namelen, GFP_NOFS);
e7fd4179
DT
436 if (!ls)
437 goto out;
e7fd4179
DT
438 memcpy(ls->ls_name, name, namelen);
439 ls->ls_namelen = namelen;
e7fd4179
DT
440 ls->ls_lvblen = lvblen;
441 ls->ls_count = 0;
442 ls->ls_flags = 0;
c1dcf65f 443 ls->ls_scan_time = jiffies;
e7fd4179 444
3ae1acf9
DT
445 if (flags & DLM_LSFL_TIMEWARN)
446 set_bit(LSFL_TIMEWARN, &ls->ls_flags);
3ae1acf9 447
fad59c13 448 /* ls_exflags are forced to match among nodes, and we don't
0f8e0d9a
DT
449 need to require all nodes to have some flags set */
450 ls->ls_exflags = (flags & ~(DLM_LSFL_TIMEWARN | DLM_LSFL_FS |
451 DLM_LSFL_NEWEXCL));
fad59c13 452
68c817a1 453 size = dlm_config.ci_rsbtbl_size;
e7fd4179
DT
454 ls->ls_rsbtbl_size = size;
455
c282af49 456 ls->ls_rsbtbl = vmalloc(sizeof(struct dlm_rsbtable) * size);
e7fd4179
DT
457 if (!ls->ls_rsbtbl)
458 goto out_lsfree;
459 for (i = 0; i < size; i++) {
9beb3bf5
BP
460 ls->ls_rsbtbl[i].keep.rb_node = NULL;
461 ls->ls_rsbtbl[i].toss.rb_node = NULL;
c7be761a 462 spin_lock_init(&ls->ls_rsbtbl[i].lock);
e7fd4179
DT
463 }
464
3d6aa675
DT
465 idr_init(&ls->ls_lkbidr);
466 spin_lock_init(&ls->ls_lkbidr_spin);
e7fd4179 467
68c817a1 468 size = dlm_config.ci_dirtbl_size;
e7fd4179
DT
469 ls->ls_dirtbl_size = size;
470
c282af49 471 ls->ls_dirtbl = vmalloc(sizeof(struct dlm_dirtable) * size);
e7fd4179
DT
472 if (!ls->ls_dirtbl)
473 goto out_lkbfree;
474 for (i = 0; i < size; i++) {
475 INIT_LIST_HEAD(&ls->ls_dirtbl[i].list);
305a47b1 476 spin_lock_init(&ls->ls_dirtbl[i].lock);
e7fd4179
DT
477 }
478
479 INIT_LIST_HEAD(&ls->ls_waiters);
90135925 480 mutex_init(&ls->ls_waiters_mutex);
ef0c2bb0
DT
481 INIT_LIST_HEAD(&ls->ls_orphans);
482 mutex_init(&ls->ls_orphans_mutex);
3ae1acf9
DT
483 INIT_LIST_HEAD(&ls->ls_timeout);
484 mutex_init(&ls->ls_timeout_mutex);
e7fd4179 485
3881ac04
DT
486 INIT_LIST_HEAD(&ls->ls_new_rsb);
487 spin_lock_init(&ls->ls_new_rsb_spin);
488
e7fd4179
DT
489 INIT_LIST_HEAD(&ls->ls_nodes);
490 INIT_LIST_HEAD(&ls->ls_nodes_gone);
491 ls->ls_num_nodes = 0;
492 ls->ls_low_nodeid = 0;
493 ls->ls_total_weight = 0;
494 ls->ls_node_array = NULL;
495
496 memset(&ls->ls_stub_rsb, 0, sizeof(struct dlm_rsb));
497 ls->ls_stub_rsb.res_ls = ls;
498
5de6319b
DT
499 ls->ls_debug_rsb_dentry = NULL;
500 ls->ls_debug_waiters_dentry = NULL;
e7fd4179
DT
501
502 init_waitqueue_head(&ls->ls_uevent_wait);
503 ls->ls_uevent_result = 0;
8b0e7b2c
DT
504 init_completion(&ls->ls_members_done);
505 ls->ls_members_result = -1;
e7fd4179 506
23e8e1aa
DT
507 mutex_init(&ls->ls_cb_mutex);
508 INIT_LIST_HEAD(&ls->ls_cb_delay);
509
e7fd4179 510 ls->ls_recoverd_task = NULL;
90135925 511 mutex_init(&ls->ls_recoverd_active);
e7fd4179 512 spin_lock_init(&ls->ls_recover_lock);
98f176fb
DT
513 spin_lock_init(&ls->ls_rcom_spin);
514 get_random_bytes(&ls->ls_rcom_seq, sizeof(uint64_t));
e7fd4179
DT
515 ls->ls_recover_status = 0;
516 ls->ls_recover_seq = 0;
517 ls->ls_recover_args = NULL;
518 init_rwsem(&ls->ls_in_recovery);
c36258b5 519 init_rwsem(&ls->ls_recv_active);
e7fd4179 520 INIT_LIST_HEAD(&ls->ls_requestqueue);
90135925 521 mutex_init(&ls->ls_requestqueue_mutex);
597d0cae 522 mutex_init(&ls->ls_clear_proc_locks);
e7fd4179 523
573c24c4 524 ls->ls_recover_buf = kmalloc(dlm_config.ci_buffer_size, GFP_NOFS);
e7fd4179
DT
525 if (!ls->ls_recover_buf)
526 goto out_dirfree;
527
757a4271
DT
528 ls->ls_slot = 0;
529 ls->ls_num_slots = 0;
530 ls->ls_slots_size = 0;
531 ls->ls_slots = NULL;
532
e7fd4179
DT
533 INIT_LIST_HEAD(&ls->ls_recover_list);
534 spin_lock_init(&ls->ls_recover_list_lock);
535 ls->ls_recover_list_count = 0;
597d0cae 536 ls->ls_local_handle = ls;
e7fd4179
DT
537 init_waitqueue_head(&ls->ls_wait_general);
538 INIT_LIST_HEAD(&ls->ls_root_list);
539 init_rwsem(&ls->ls_root_sem);
540
541 down_write(&ls->ls_in_recovery);
542
5f88f1ea 543 spin_lock(&lslist_lock);
0f8e0d9a 544 ls->ls_create_count = 1;
5f88f1ea
DT
545 list_add(&ls->ls_list, &lslist);
546 spin_unlock(&lslist_lock);
547
23e8e1aa
DT
548 if (flags & DLM_LSFL_FS) {
549 error = dlm_callback_start(ls);
550 if (error) {
551 log_error(ls, "can't start dlm_callback %d", error);
552 goto out_delist;
553 }
554 }
555
5f88f1ea 556 /* needs to find ls in lslist */
e7fd4179
DT
557 error = dlm_recoverd_start(ls);
558 if (error) {
559 log_error(ls, "can't start dlm_recoverd %d", error);
23e8e1aa 560 goto out_callback;
e7fd4179
DT
561 }
562
901195ed
GKH
563 ls->ls_kobj.kset = dlm_kset;
564 error = kobject_init_and_add(&ls->ls_kobj, &dlm_ktype, NULL,
565 "%s", ls->ls_name);
e7fd4179 566 if (error)
23e8e1aa 567 goto out_recoverd;
901195ed 568 kobject_uevent(&ls->ls_kobj, KOBJ_ADD);
79d72b54
DT
569
570 /* let kobject handle freeing of ls if there's an error */
571 do_unreg = 1;
e7fd4179 572
8b0e7b2c
DT
573 /* This uevent triggers dlm_controld in userspace to add us to the
574 group of nodes that are members of this lockspace (managed by the
575 cluster infrastructure.) Once it's done that, it tells us who the
576 current lockspace members are (via configfs) and then tells the
577 lockspace to start running (via sysfs) in dlm_ls_start(). */
578
e7fd4179
DT
579 error = do_uevent(ls, 1);
580 if (error)
23e8e1aa 581 goto out_recoverd;
79d72b54 582
8b0e7b2c
DT
583 wait_for_completion(&ls->ls_members_done);
584 error = ls->ls_members_result;
585 if (error)
586 goto out_members;
587
79d72b54
DT
588 dlm_create_debug_file(ls);
589
590 log_debug(ls, "join complete");
e7fd4179
DT
591 *lockspace = ls;
592 return 0;
593
8b0e7b2c
DT
594 out_members:
595 do_uevent(ls, 0);
596 dlm_clear_members(ls);
597 kfree(ls->ls_node_array);
23e8e1aa 598 out_recoverd:
5f88f1ea 599 dlm_recoverd_stop(ls);
23e8e1aa
DT
600 out_callback:
601 dlm_callback_stop(ls);
79d72b54 602 out_delist:
e7fd4179
DT
603 spin_lock(&lslist_lock);
604 list_del(&ls->ls_list);
605 spin_unlock(&lslist_lock);
e7fd4179
DT
606 kfree(ls->ls_recover_buf);
607 out_dirfree:
c282af49 608 vfree(ls->ls_dirtbl);
e7fd4179 609 out_lkbfree:
3d6aa675 610 idr_destroy(&ls->ls_lkbidr);
c282af49 611 vfree(ls->ls_rsbtbl);
e7fd4179 612 out_lsfree:
79d72b54 613 if (do_unreg)
197b12d6 614 kobject_put(&ls->ls_kobj);
79d72b54
DT
615 else
616 kfree(ls);
e7fd4179
DT
617 out:
618 module_put(THIS_MODULE);
619 return error;
620}
621
08ce4c91 622int dlm_new_lockspace(const char *name, int namelen, void **lockspace,
e7fd4179
DT
623 uint32_t flags, int lvblen)
624{
625 int error = 0;
626
90135925 627 mutex_lock(&ls_lock);
e7fd4179
DT
628 if (!ls_count)
629 error = threads_start();
630 if (error)
631 goto out;
632
633 error = new_lockspace(name, namelen, lockspace, flags, lvblen);
634 if (!error)
635 ls_count++;
8511a272
DT
636 if (error > 0)
637 error = 0;
638 if (!ls_count)
8b0e7b2c 639 threads_stop();
e7fd4179 640 out:
90135925 641 mutex_unlock(&ls_lock);
e7fd4179
DT
642 return error;
643}
644
3d6aa675 645static int lkb_idr_is_local(int id, void *p, void *data)
e7fd4179 646{
3d6aa675
DT
647 struct dlm_lkb *lkb = p;
648
649 if (!lkb->lkb_nodeid)
650 return 1;
651 return 0;
652}
653
654static int lkb_idr_is_any(int id, void *p, void *data)
655{
656 return 1;
657}
658
659static int lkb_idr_free(int id, void *p, void *data)
660{
661 struct dlm_lkb *lkb = p;
662
3d6aa675
DT
663 if (lkb->lkb_lvbptr && lkb->lkb_flags & DLM_IFL_MSTCPY)
664 dlm_free_lvb(lkb->lkb_lvbptr);
665
666 dlm_free_lkb(lkb);
667 return 0;
668}
669
670/* NOTE: We check the lkbidr here rather than the resource table.
671 This is because there may be LKBs queued as ASTs that have been unlinked
672 from their RSBs and are pending deletion once the AST has been delivered */
673
674static int lockspace_busy(struct dlm_ls *ls, int force)
675{
676 int rv;
677
678 spin_lock(&ls->ls_lkbidr_spin);
679 if (force == 0) {
680 rv = idr_for_each(&ls->ls_lkbidr, lkb_idr_is_any, ls);
681 } else if (force == 1) {
682 rv = idr_for_each(&ls->ls_lkbidr, lkb_idr_is_local, ls);
683 } else {
684 rv = 0;
e7fd4179 685 }
3d6aa675
DT
686 spin_unlock(&ls->ls_lkbidr_spin);
687 return rv;
e7fd4179
DT
688}
689
690static int release_lockspace(struct dlm_ls *ls, int force)
691{
e7fd4179 692 struct dlm_rsb *rsb;
9beb3bf5 693 struct rb_node *n;
0f8e0d9a
DT
694 int i, busy, rv;
695
3d6aa675 696 busy = lockspace_busy(ls, force);
0f8e0d9a
DT
697
698 spin_lock(&lslist_lock);
699 if (ls->ls_create_count == 1) {
3d6aa675 700 if (busy) {
0f8e0d9a 701 rv = -EBUSY;
3d6aa675 702 } else {
0f8e0d9a
DT
703 /* remove_lockspace takes ls off lslist */
704 ls->ls_create_count = 0;
705 rv = 0;
706 }
707 } else if (ls->ls_create_count > 1) {
708 rv = --ls->ls_create_count;
709 } else {
710 rv = -EINVAL;
711 }
712 spin_unlock(&lslist_lock);
713
714 if (rv) {
715 log_debug(ls, "release_lockspace no remove %d", rv);
716 return rv;
717 }
e7fd4179 718
0f8e0d9a 719 dlm_device_deregister(ls);
e7fd4179 720
dc68c7ed 721 if (force < 3 && dlm_user_daemon_available())
e7fd4179
DT
722 do_uevent(ls, 0);
723
724 dlm_recoverd_stop(ls);
725
23e8e1aa
DT
726 dlm_callback_stop(ls);
727
e7fd4179
DT
728 remove_lockspace(ls);
729
730 dlm_delete_debug_file(ls);
731
e7fd4179
DT
732 kfree(ls->ls_recover_buf);
733
734 /*
735 * Free direntry structs.
736 */
737
738 dlm_dir_clear(ls);
c282af49 739 vfree(ls->ls_dirtbl);
e7fd4179
DT
740
741 /*
3d6aa675 742 * Free all lkb's in idr
e7fd4179
DT
743 */
744
3d6aa675
DT
745 idr_for_each(&ls->ls_lkbidr, lkb_idr_free, ls);
746 idr_remove_all(&ls->ls_lkbidr);
747 idr_destroy(&ls->ls_lkbidr);
e7fd4179 748
e7fd4179
DT
749 /*
750 * Free all rsb's on rsbtbl[] lists
751 */
752
753 for (i = 0; i < ls->ls_rsbtbl_size; i++) {
9beb3bf5
BP
754 while ((n = rb_first(&ls->ls_rsbtbl[i].keep))) {
755 rsb = rb_entry(n, struct dlm_rsb, res_hashnode);
756 rb_erase(n, &ls->ls_rsbtbl[i].keep);
52bda2b5 757 dlm_free_rsb(rsb);
e7fd4179
DT
758 }
759
9beb3bf5
BP
760 while ((n = rb_first(&ls->ls_rsbtbl[i].toss))) {
761 rsb = rb_entry(n, struct dlm_rsb, res_hashnode);
762 rb_erase(n, &ls->ls_rsbtbl[i].toss);
52bda2b5 763 dlm_free_rsb(rsb);
e7fd4179
DT
764 }
765 }
766
c282af49 767 vfree(ls->ls_rsbtbl);
e7fd4179 768
3881ac04
DT
769 while (!list_empty(&ls->ls_new_rsb)) {
770 rsb = list_first_entry(&ls->ls_new_rsb, struct dlm_rsb,
771 res_hashchain);
772 list_del(&rsb->res_hashchain);
773 dlm_free_rsb(rsb);
774 }
775
e7fd4179
DT
776 /*
777 * Free structures on any other lists
778 */
779
2896ee37 780 dlm_purge_requestqueue(ls);
e7fd4179
DT
781 kfree(ls->ls_recover_args);
782 dlm_clear_free_entries(ls);
783 dlm_clear_members(ls);
784 dlm_clear_members_gone(ls);
785 kfree(ls->ls_node_array);
0f8e0d9a 786 log_debug(ls, "release_lockspace final free");
197b12d6 787 kobject_put(&ls->ls_kobj);
79d72b54 788 /* The ls structure will be freed when the kobject is done with */
e7fd4179 789
e7fd4179
DT
790 module_put(THIS_MODULE);
791 return 0;
792}
793
794/*
795 * Called when a system has released all its locks and is not going to use the
796 * lockspace any longer. We free everything we're managing for this lockspace.
797 * Remaining nodes will go through the recovery process as if we'd died. The
798 * lockspace must continue to function as usual, participating in recoveries,
799 * until this returns.
800 *
801 * Force has 4 possible values:
802 * 0 - don't destroy locksapce if it has any LKBs
803 * 1 - destroy lockspace if it has remote LKBs but not if it has local LKBs
804 * 2 - destroy lockspace regardless of LKBs
805 * 3 - destroy lockspace as part of a forced shutdown
806 */
807
808int dlm_release_lockspace(void *lockspace, int force)
809{
810 struct dlm_ls *ls;
0f8e0d9a 811 int error;
e7fd4179
DT
812
813 ls = dlm_find_lockspace_local(lockspace);
814 if (!ls)
815 return -EINVAL;
816 dlm_put_lockspace(ls);
0f8e0d9a
DT
817
818 mutex_lock(&ls_lock);
819 error = release_lockspace(ls, force);
820 if (!error)
821 ls_count--;
278afcbf 822 if (!ls_count)
0f8e0d9a
DT
823 threads_stop();
824 mutex_unlock(&ls_lock);
825
826 return error;
e7fd4179
DT
827}
828
dc68c7ed
DT
829void dlm_stop_lockspaces(void)
830{
831 struct dlm_ls *ls;
832
833 restart:
834 spin_lock(&lslist_lock);
835 list_for_each_entry(ls, &lslist, ls_list) {
836 if (!test_bit(LSFL_RUNNING, &ls->ls_flags))
837 continue;
838 spin_unlock(&lslist_lock);
839 log_error(ls, "no userland control daemon, stopping lockspace");
840 dlm_ls_stop(ls);
841 goto restart;
842 }
843 spin_unlock(&lslist_lock);
844}
845